mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-11-23 01:58:16 +08:00
fix(loop): feed native function-call results back as function_call_output
textProtocol mode (codex Responses) fed ALL proxy-tool results back as role:system developer messages (buildVisibilityMarker), never as a proper function_call_output. The Responses API expects function_call -> function_call_output pairing; a bare system message is ignored by the model, so it re-calls the same tool (#119 run-on loop, hit MAX_LOOP_ROUNDS). Fix: track which calls were native function_calls (from parseStream) vs text-protocol markers (from extractTextTriggers). Feed native function-call results back as a proper assistant tool-call + tool function_call_output pair even under textProtocol; markers keep the system-message visibility form. Also feed the model's raw assistantText (not the marker-stripped resolvedText) so the model can see it already acted. Verified end-to-end (codex acp_status via bili): round-2 body now carries function_call_output; model stops at round 2 (was rounds 3-9 run-on).
This commit is contained in:
+11
-10
@@ -221,6 +221,7 @@ export async function* runCompressLoop(
|
||||
resolvedText = extracted.clean;
|
||||
allCalls = [...calls, ...extracted.calls];
|
||||
}
|
||||
const functionCallIds = new Set(calls.map(c => c.callId));
|
||||
|
||||
if (ctx.textProtocol && resolvedText.length > 0) {
|
||||
yield adapter.emitText(resolvedText);
|
||||
@@ -280,23 +281,16 @@ export async function* runCompressLoop(
|
||||
// never in coreMessages), and hideConsumedCompressCalls runs each
|
||||
// round so consumed compress records cannot re-prime the model.
|
||||
if (proxyResults.length > 0) {
|
||||
if (resolvedText.length > 0) {
|
||||
if (assistantText.length > 0) {
|
||||
coreMessages.push({
|
||||
id: `acp_loop_r${round}_asst`,
|
||||
role: "assistant",
|
||||
contentType: "text",
|
||||
text: resolvedText,
|
||||
text: assistantText,
|
||||
});
|
||||
}
|
||||
for (const pr of proxyResults) {
|
||||
if (ctx.textProtocol) {
|
||||
coreMessages.push({
|
||||
id: `acp_loop_r${round}_marker_${pr.callId}`,
|
||||
role: "system",
|
||||
contentType: "text",
|
||||
text: buildVisibilityMarker(pr.name, pr.result),
|
||||
});
|
||||
} else {
|
||||
if (functionCallIds.has(pr.callId)) {
|
||||
coreMessages.push({
|
||||
id: `acp_loop_r${round}_asst_tc_${pr.callId}`,
|
||||
role: "assistant",
|
||||
@@ -312,6 +306,13 @@ export async function* runCompressLoop(
|
||||
toolCallId: pr.callId,
|
||||
text: pr.result,
|
||||
});
|
||||
} else {
|
||||
coreMessages.push({
|
||||
id: `acp_loop_r${round}_marker_${pr.callId}`,
|
||||
role: "system",
|
||||
contentType: "text",
|
||||
text: buildVisibilityMarker(pr.name, pr.result),
|
||||
});
|
||||
}
|
||||
}
|
||||
if (!ctx.textProtocol) {
|
||||
|
||||
@@ -323,6 +323,47 @@ test("loop #12 (guard): a SECOND acp_status in the same request is a no-op (prev
|
||||
}
|
||||
});
|
||||
|
||||
test("loop #13 (feedback fix): textProtocol + native function_call → round-2 re-request carries a proper function_call_output (not a bare developer message)", async () => {
|
||||
// Before the fix, textProtocol mode fed ALL proxy-tool results back as
|
||||
// role:system developer messages (buildVisibilityMarker), never as a proper
|
||||
// function_call_output. The Responses API expects function_call →
|
||||
// function_call_output pairing; a bare system message gets ignored, so the
|
||||
// model re-calls the same tool (#119 run-on). The fix tracks which calls were
|
||||
// native function_calls and feeds those back as a proper function_call +
|
||||
// function_call_output pair even under textProtocol.
|
||||
const ctx = makeCtx([
|
||||
textMsg("m00001", "user", "hello"),
|
||||
textMsg("m00002", "assistant", "hi"),
|
||||
]);
|
||||
ctx.textProtocol = true;
|
||||
const round1 = [
|
||||
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
|
||||
fcEvents(0, "call_s", "acp_status", "{}"),
|
||||
COMPLETED,
|
||||
].join("");
|
||||
const bodies: string[] = [];
|
||||
const orig = globalThis.fetch;
|
||||
globalThis.fetch = (async (_u: unknown, init?: RequestInit) => {
|
||||
if (init?.body) bodies.push(String(init.body));
|
||||
return new Response(REFETCH_DONE, { status: 200, headers: { "content-type": "text/event-stream" } });
|
||||
}) as typeof fetch;
|
||||
try {
|
||||
await drain(
|
||||
new Response(round1, { status: 200 }).body!,
|
||||
ctx,
|
||||
{ model: "gpt-4o", input: [], stream: true },
|
||||
{ url: "http://mock", headers: {} },
|
||||
createResponsesAdapter(true),
|
||||
);
|
||||
assert.ok(bodies.length >= 1, "re-request fires after acp_status");
|
||||
const body = bodies[0];
|
||||
assert.ok(body.includes('"function_call"'), "round-2 body includes the assistant function_call item");
|
||||
assert.ok(body.includes('"function_call_output"'), "round-2 body includes a proper function_call_output (the fix); previously only a developer system message was sent");
|
||||
} finally {
|
||||
globalThis.fetch = orig;
|
||||
}
|
||||
});
|
||||
|
||||
test("loop #9 (S2): responses round yields usage → session.stats populated (nudge/stat tracking)", async () => {
|
||||
const ctx = makeCtx([
|
||||
textMsg("m00001", "user", "hello"),
|
||||
|
||||
Reference in New Issue
Block a user