Skip to content

Commit d17f435

Browse files
committed
fix(anthropic): guard continuation abort-race + plan-only false positives (#394 review)
Address two review blockers on the terminal-guard PR: 1. Bind the terminal-guard continuation response body to the abort signal via cancelBodyOnAbort (matching the initial response), so a late client abort during the continuation cannot reopen the Bun fetch-to-reader race (#390/366e3053). Also redact continuation parse-error messages and map a genuine abort to 499. Regression: tests/abort-race.test.ts. 2. Recognize ordinary plan/proposal/draft requests (EN + zh) in PLAN_ONLY_REQUEST_RE so a plain 'write a concise implementation plan' ask is not forced into tool execution. Regression: tests/terminal-guard.test.ts.
1 parent 68d738e commit d17f435

5 files changed

Lines changed: 120 additions & 8 deletions

File tree

‎src/server/responses/core.ts‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,15 +1718,27 @@ export async function handleResponses(
17181718
}
17191719

17201720
try {
1721-
if (nextParsed.stream) {
1722-
yield* activeAdapter.parseStream(response);
1723-
} else if (activeAdapter.parseResponse) {
1724-
yield* await activeAdapter.parseResponse(response);
1725-
} else {
1726-
yield { type: "error", message: "Provider continuation does not support response parsing" };
1721+
// Protect the continuation body against a client abort landing between fetch resolution and
1722+
// reader attach, exactly as the initial response is guarded above (#390/366e3053). Without
1723+
// this, a client cancel during the continuation reopens the Bun fetch-to-reader abort race.
1724+
const detachContinuationBodyGuard = cancelBodyOnAbort(response.body, upstream.signal);
1725+
try {
1726+
if (nextParsed.stream) {
1727+
yield* activeAdapter.parseStream(response);
1728+
} else if (activeAdapter.parseResponse) {
1729+
yield* await activeAdapter.parseResponse(response);
1730+
} else {
1731+
yield { type: "error", message: "Provider continuation does not support response parsing" };
1732+
}
1733+
} finally {
1734+
detachContinuationBodyGuard();
17271735
}
17281736
} catch (error) {
1729-
yield { type: "error", message: `Provider continuation parse failed: ${error instanceof Error ? error.message : String(error)}` };
1737+
if (options.abortSignal?.aborted) {
1738+
yield { type: "error", message: "client closed request during terminal continuation", status: 499 };
1739+
} else {
1740+
yield { type: "error", message: `Provider continuation parse failed: ${redactSecretString(error instanceof Error ? error.message : String(error))}` };
1741+
}
17301742
}
17311743
};
17321744

‎src/server/responses/terminal-guard.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "../../types";
88

99
const ACTIONABLE_REQUEST_RE = /(?:\b(?:add|change|check|continue|create|debug|delete|deploy|edit|execute|fix|implement|inspect|keep going|modify|patch|proceed|refactor|remove|review|run|test|update|write)\b|继续|接着|往下|升级|修改|改(?:一下|下)?|修复|实现|添加|新增|删除|重构|更新|运行|执行|检查|查看|排查|调试|创建|写入|提交|推送|部署|看下|改成|修一下)/iu;
10-
const PLAN_ONLY_REQUEST_RE = /(?:只(?:给|要|需)(?:我)?(?:一个|个|一下)?(?:计划|方案)|先(?:给|说)(?:我)?(?:一个|个|一下)?(?:计划|方案)|暂时不要(?:调用|使用)工具|不要调用工具|不用执行|只回复(?:计划|方案)|\b(?:just give|provide)\s+(?:me\s+)?(?:a\s+)?plan\b|\b(?:do not|don't)\s+(?:use|call)\s+tools?\b)/iu;
10+
const PLAN_ONLY_REQUEST_RE = /(?:暂时不要(?:调用|使用)工具|不要调用工具|不用执行|只回复(?:计划|方案)|(?:只|先|给|说|写|出|来)(?:我)?(?:一个|个|一下|份)?[^。!?\n]{0,8}?(?:计划|方案|草案|提案|思路)|(?:计划|方案|草案|提案|思路)(?:就(?:行|好|可以)|即可)|\b(?:just give|provide)\s+(?:me\s+)?(?:a\s+)?plan\b|\b(?:do not|don't)\s+(?:use|call)\s+tools?\b|\b(?:write|draft|outline|propose|give|provide|create|make|share|suggest|sketch)\s+(?:me\s+)?(?:a\s+|an\s+|the\s+|your\s+)?(?:(?:brief|concise|short|detailed|high[-\s]?level|rough|quick|step[-\s]?by[-\s]?step|implementation|migration|refactor(?:ing)?|design|technical)\s+)*(?:plan|proposal|draft|outline|approach|strategy|design\s+doc)\b)/iu;
1111
const PLAN_INTENT_RE = /(?:\b(?:i(?:'|’)m going to|i will|i(?:'|’)ll|let me|next i)\b|我(?:先|会|将|接下来)|下一步)/iu;
1212
const PLAN_OR_COMPLETION_RE = /(?:\b(?:i(?:'|’)m going to|i will|i(?:'|’)ll|let me|next i|i(?:'|’)ve (?:already )?(?:added|applied|changed|completed|fixed|implemented|modified|updated))\b|\b(?:done|completed|fixed|implemented|updated|applied)\b|我(?:先|会|将|接下来)|下一步|已(?:经)?(?:修改|修复|完成|应用|更新|实现|处理)|完成了|已经好了)/iu;
1313
const WAITING_FOR_USER_RE = /(?:[??]\s*$|需要我|请(?:确认|选择|提供)|是否|要不要|可以吗|\b(?:do you want|should i|which file|please confirm|please provide)\b)/iu;

‎tests/abort-race.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,81 @@ describe("Responses abort guards", () => {
113113
process.off("unhandledRejection", onUnhandledRejection);
114114
}
115115
});
116+
117+
test("terminal-guard continuation body is cancelled on a late client abort (#394 review)", async () => {
118+
// The terminal guard opens a SECOND upstream response (the continuation). Its body must be
119+
// bound to the abort signal exactly like the initial response, or the fetch-to-reader race
120+
// (#390/366e3053) reopens on the continuation path. First turn: an anthropic end_turn with no
121+
// tool call (triggers exactly one continuation). Continuation: a body whose reader attaches
122+
// only after the client has aborted; cancelBodyOnAbort must cancel it.
123+
const clientAbort = new AbortController();
124+
const unhandledRejections: unknown[] = [];
125+
const onUnhandledRejection = (reason: unknown) => { unhandledRejections.push(reason); };
126+
let continuationBodyCancelled = false;
127+
let fetches = 0;
128+
129+
process.on("unhandledRejection", onUnhandledRejection);
130+
try {
131+
adapterFactory = provider => ({
132+
name: "anthropic",
133+
buildRequest: () => ({ url: provider.baseUrl, method: "POST", headers: {}, body: "" }),
134+
async fetchResponse() {
135+
fetches += 1;
136+
if (fetches === 1) {
137+
// First turn: a complete anthropic-style stream that ends without a tool call.
138+
return new Response(new ReadableStream<Uint8Array>({
139+
start(controller) {
140+
controller.enqueue(new TextEncoder().encode(
141+
'event: message_start\ndata: {"type":"message_start","message":{"usage":{"input_tokens":10,"output_tokens":1}}}\n\n' +
142+
'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}\n\n' +
143+
'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"我接下来会修改相关文件。"}}\n\n' +
144+
'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n' +
145+
'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}\n\n' +
146+
'event: message_stop\ndata: {"type":"message_stop"}\n\n',
147+
));
148+
controller.close();
149+
},
150+
}), { status: 200, headers: { "content-type": "text/event-stream" } });
151+
}
152+
// Continuation turn: reader attaches only after the client aborts.
153+
return new Response(new ReadableStream<Uint8Array>({
154+
start(controller) { controller.enqueue(new Uint8Array([1])); },
155+
cancel() { continuationBodyCancelled = true; },
156+
}), { status: 200, headers: { "content-type": "text/event-stream" } });
157+
},
158+
async *parseStream(response): AsyncGenerator<AdapterEvent> {
159+
if (fetches >= 2) {
160+
// We are now parsing the continuation stream — abort before attaching the reader.
161+
clientAbort.abort(new DOMException("client disconnected", "AbortError"));
162+
await Promise.resolve();
163+
const reader = response.body!.getReader();
164+
try { await reader.read(); } finally { reader.releaseLock(); }
165+
return;
166+
}
167+
// First turn: emit an end_turn with no tool call so the guard opens a continuation.
168+
yield { type: "text_delta", text: "我接下来会修改相关文件。" };
169+
yield { type: "done", usage: { inputTokens: 10, outputTokens: 2 } };
170+
},
171+
});
172+
173+
const response = await handleResponses(new Request("http://localhost/v1/responses", {
174+
method: "POST",
175+
headers: { "content-type": "application/json" },
176+
body: JSON.stringify({
177+
model: "fixture/model",
178+
input: "请检查这个问题并修复代码",
179+
stream: true,
180+
tools: [{ type: "function", name: "exec_command", description: "run a command", parameters: { type: "object" } }],
181+
}),
182+
}), config("anthropic"), { model: "", provider: "" }, { abortSignal: clientAbort.signal });
183+
await response.text().catch(() => {});
184+
await new Promise<void>(resolve => setImmediate(resolve));
185+
186+
expect(fetches).toBe(2);
187+
expect(continuationBodyCancelled).toBe(true);
188+
expect(unhandledRejections).toEqual([]);
189+
} finally {
190+
process.off("unhandledRejection", onUnhandledRejection);
191+
}
192+
});
116193
});

‎tests/terminal-guard-server.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ describe("server terminal guard integration", () => {
7777
expect(messages.at(-1)?.role).toBe("user");
7878
expect(messages.at(-1)?.content?.[0]?.text).toContain("你刚才只描述了计划");
7979
});
80+
8081
});

‎tests/terminal-guard.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ describe("terminal guard", () => {
4848
expect(analysis.decision).toBe("pass");
4949
});
5050

51+
test("does not force tools for an ordinary plan/proposal request without explicit tool prohibition", () => {
52+
// Regression (#394 review blocker 2): a plain 'write a concise implementation plan' request
53+
// must NOT be treated as a suspicious no-tool completion, even though it contains the
54+
// actionable verbs 'write'/'implementation'. Otherwise the guard nudges Claude to run tools
55+
// against a plan-only ask, causing side effects.
56+
for (const ask of [
57+
"Write a concise implementation plan for this change",
58+
"Give me a high-level plan before we start",
59+
"Draft a migration plan for the schema",
60+
"Propose an approach for refactoring the router",
61+
"先写一个实现方案",
62+
"给我一个重构计划",
63+
]) {
64+
const analysis = analyzeTerminalTurn(parsed(ask), [
65+
{ type: "text_delta", text: "Here is the plan: 1) ... 2) ... 3) ..." },
66+
{ type: "done" },
67+
]);
68+
expect(analysis.decision).toBe("pass");
69+
expect(analysis.reason).toBe("no_actionable_request");
70+
}
71+
});
72+
5173
test("does not auto-repeat an explicit continue after a recent tool-backed turn", () => {
5274
const request = parsed("继续");
5375
request.context.messages = [

0 commit comments

Comments
 (0)