fix(loop): re-request after ANY proxy tool, not just mutating ones (acp_status/search_context hang)

When the model calls a non-mutating proxy tool (acp_status, search_context),
the V2 loop consumed the call but returned to the client with
finish_reason='tool_calls' and NO tool_calls in the body — causing the
client (pi/opencode) to hang indefinitely waiting for tool calls that
never arrive.

Root cause: the re-request condition was 'sawMutating && realCalls === 0',
which only re-requested for compress/decompress. Non-mutating proxy tools
fell through to emitCompletion with the model's original finish_reason
('tool_calls'), producing a degenerate response.

Fix: re-request whenever ANY proxy tool was consumed and no real tools
were emitted: 'proxyResults.length > 0 && realCalls === 0'. This feeds
the proxy result back to the model so it can continue, matching baseline
behavior. MAX_LOOP_ROUNDS=10 still bounds the loop if the model keeps
calling proxy tools without progress.

Updated 4 tests that asserted the old no-re-request behavior.
246/246 pass.
This commit is contained in:
ranxianglei
2026-08-11 09:48:50 +08:00
parent bf6690b1d6
commit d8a1e0dba5
3 changed files with 24 additions and 16 deletions
+1 -4
View File
@@ -8,7 +8,6 @@ import {
} from "acp-kernel";
import type { Session } from "../session.js";
import {
MUTATING_PROXY_TOOLS,
parseCompressInput,
PROXY_TOOL_NAMES,
} from "../compress-tool.js";
@@ -193,7 +192,6 @@ export async function* runCompressLoop(
yield adapter.emitText(resolvedText);
}
let sawMutating = false;
let realCalls = 0;
const realToolCalls: ToolCallEmit[] = [];
const proxyResults: { name: string; callId: string; result: string; arguments: string }[] = [];
@@ -209,7 +207,6 @@ export async function* runCompressLoop(
const result = executeProxyTool(call.name, parsedArgs, ctx, call.callId);
proxyResults.push({ name: call.name, callId: call.callId, result, arguments: call.arguments });
yield adapter.emitMarker(call.name, result);
if (MUTATING_PROXY_TOOLS.has(call.name)) sawMutating = true;
} else {
realToolCalls.push(call);
realCalls += 1;
@@ -269,7 +266,7 @@ export async function* runCompressLoop(
yield adapter.emitToolCall(tc);
}
const reRequest = sawMutating && realCalls === 0;
const reRequest = proxyResults.length > 0 && realCalls === 0;
if (!reRequest) {
yield adapter.emitCompletion({ finishReason, usage });
return;
+15 -6
View File
@@ -67,16 +67,20 @@ test("openai adapter: plain text round-trips live + [DONE] termination", async (
assert.ok(/finish_reason/.test(out), "finish chunk present");
});
test("openai adapter: acp_status-only round → marker + graceful completion, no re-request, no crash", async () => {
test("openai adapter: acp_status-only round → marker + re-request, no crash", async () => {
const round1 = [
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }] })}\n\n`,
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_s", type: "function", function: { name: "acp_status", arguments: "{}" } }] }, finish_reason: null }] })}\n\n`,
`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`,
`data: [DONE]\n\n`,
].join("");
const round2 = [
`data: ${JSON.stringify({ id: "c2", object: "chat.completion.chunk", created: 2, model: "gpt", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`,
`data: [DONE]\n\n`,
].join("");
let fetchCalls = 0;
const orig = globalThis.fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round1, { status: 200 }); }) as typeof fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
try {
const out = await drain(
new Response(round1, { status: 200 }).body!,
@@ -86,7 +90,7 @@ test("openai adapter: acp_status-only round → marker + graceful completion, no
);
assert.ok(out.includes("[ACP]"), "acp_status marker surfaced");
assert.ok(out.includes("[DONE]"), "graceful completion ([DONE])");
assert.equal(fetchCalls, 0, "no re-request for read-only acp_status");
assert.equal(fetchCalls, 1, "re-request after acp_status (avoids tool_calls-no-body hang)");
} finally {
globalThis.fetch = orig;
}
@@ -112,7 +116,7 @@ test("anthropic adapter: plain text round-trips live + message_delta/message_sto
assert.ok(out.includes("message_stop"), "message_stop terminal present");
});
test("anthropic adapter: acp_status-only round → marker + graceful completion, no re-request, no crash", async () => {
test("anthropic adapter: acp_status-only round → marker + re-request, no crash", async () => {
const round1 = [
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_1", usage: { input_tokens: 3 } } })}\n\n`,
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_s", name: "acp_status", input: {} } })}\n\n`,
@@ -121,9 +125,14 @@ test("anthropic adapter: acp_status-only round → marker + graceful completion,
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } })}\n\n`,
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
].join("");
const round2 = [
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "msg_2", usage: { input_tokens: 3 } } })}\n\n`,
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } })}\n\n`,
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`,
].join("");
let fetchCalls = 0;
const orig = globalThis.fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round1, { status: 200 }); }) as typeof fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
try {
const out = await drain(
new Response(round1, { status: 200 }).body!,
@@ -133,7 +142,7 @@ test("anthropic adapter: acp_status-only round → marker + graceful completion,
);
assert.ok(out.includes("[ACP]"), "acp_status marker surfaced");
assert.ok(out.includes("message_stop"), "graceful completion (message_stop)");
assert.equal(fetchCalls, 0, "no re-request for read-only acp_status");
assert.equal(fetchCalls, 1, "re-request after acp_status (avoids tool_use-no-body hang)");
} finally {
globalThis.fetch = orig;
}
+8 -6
View File
@@ -63,15 +63,16 @@ function fcEvents(outputIndex: number, callId: string, name: string, args: strin
const COMPLETED = sse("response.completed", { response: { id: "resp_done", status: "completed", output: [] } });
test("loop #1: acp_status-only round → marker surfaced, NO re-request, graceful completion", async () => {
test("loop #1: acp_status-only round → marker surfaced + re-request (avoids tool_calls-no-body hang)", async () => {
const round1 = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
fcEvents(0, "call_status", "acp_status", "{}"),
COMPLETED,
].join("");
const round2 = COMPLETED;
let fetchCalls = 0;
const orig = globalThis.fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round1, { status: 200 }); }) as typeof fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
try {
const out = await drain(
new Response(round1, { status: 200 }).body!,
@@ -81,21 +82,22 @@ test("loop #1: acp_status-only round → marker surfaced, NO re-request, gracefu
);
assert.ok(out.includes("[ACP]"), "acp_status visibility marker surfaced to client");
assert.ok(/response\.completed/.test(out), "graceful completion present (no 炸锅)");
assert.equal(fetchCalls, 0, "NO re-request: acp_status is read-only, turn ends");
assert.equal(fetchCalls, 1, "re-request after acp_status so model can continue (not finish_reason=tool_calls with no body)");
} finally {
globalThis.fetch = orig;
}
});
test("loop #2: search_context-only round → marker surfaced, NO re-request, graceful completion", async () => {
test("loop #2: search_context-only round → marker surfaced + re-request", async () => {
const round1 = [
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),
fcEvents(0, "call_search", "search_context", JSON.stringify({ query: "auth", limit: 3 })),
COMPLETED,
].join("");
const round2 = COMPLETED;
let fetchCalls = 0;
const orig = globalThis.fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round1, { status: 200 }); }) as typeof fetch;
globalThis.fetch = (async () => { fetchCalls++; return new Response(round2, { status: 200 }); }) as typeof fetch;
try {
const out = await drain(
new Response(round1, { status: 200 }).body!,
@@ -105,7 +107,7 @@ test("loop #2: search_context-only round → marker surfaced, NO re-request, gra
);
assert.ok(out.includes("[ACP]"), "search_context marker surfaced");
assert.ok(/response\.completed/.test(out), "graceful completion present");
assert.equal(fetchCalls, 0, "NO re-request: search_context is read-only");
assert.equal(fetchCalls, 1, "re-request after search_context so model can use results");
} finally {
globalThis.fetch = orig;
}