Skip to content

Commit b8436f6

Browse files
committed
fix(anthropic): guard premature no-tool completions
1 parent 4c8b09a commit b8436f6

7 files changed

Lines changed: 668 additions & 2 deletions

File tree

‎docs-site/src/content/docs/reference/adapters.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ streams the response back **untranslated**.
6464
- Always sends `anthropic-version: 2023-06-01`. Streams `content_block_delta` (`text_delta`,
6565
`thinking_delta`, compatible `reasoning_delta`, `input_json_delta`). The SSE decoder preserves
6666
event state across fetch chunks and accepts a terminal `message_stop` without a trailing newline.
67+
- For routed Anthropic Responses turns with client tools, a bounded terminal guard detects the
68+
high-confidence case where the user requested an action but Claude ends with an execution claim
69+
and no tool call. It performs at most one internal continuation; normal answers, clarification
70+
questions, tool-using turns, and transport-incomplete responses are not auto-retried.
6771

6872
## `google`
6973

‎src/bridge.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,17 @@ export function bridgeToResponsesSSE(
458458
if (event.type !== "done" && event.type !== "incomplete" && event.type !== "error") continue;
459459
}
460460
switch (event.type) {
461+
case "assistant_boundary": {
462+
// A guarded continuation starts a fresh assistant output item while keeping the
463+
// intermediate, suspicious text in the same Responses turn.
464+
if (currentMsg) closeCurrentMessage();
465+
if (currentReasoning) closeCurrentReasoning();
466+
if (currentRawReasoning) closeCurrentRawReasoning();
467+
flushHiddenRawReasoning();
468+
if (currentToolCall) closeCurrentToolCall();
469+
flushHiddenReasoningEnvelope();
470+
break;
471+
}
461472
case "text_delta": {
462473
if (currentReasoning) closeCurrentReasoning();
463474
if (currentRawReasoning) closeCurrentRawReasoning();
@@ -903,6 +914,12 @@ export function buildResponseJSON(
903914

904915
for (const e of events) {
905916
switch (e.type) {
917+
case "assistant_boundary":
918+
flushText();
919+
flushSummaryReasoning();
920+
flushRawReasoning();
921+
flushToolCall();
922+
break;
906923
case "text_delta":
907924
if (currentText && currentTextPhase !== e.phase) flushText();
908925
if (currentSummaryReasoning) flushSummaryReasoning();

‎src/server/responses/core.ts‎

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import type { EffectiveSubagentRoster, SpawnAgentSurface } from "../../codex/cat
104104
import { buildToolBridgeMaps, collabSurface, injectDeveloperMessage, multiAgentGuidanceText } from "./collaboration";
105105
import { hasUnreadableEncryptedAgentTask, looksLikeBackendCiphertext, sanitizeEncryptedContentInPlace } from "./encrypted-payload";
106106
import { fetchWithHeaderTimeout, providerFetch, safeHostLabel } from "./fetch-helpers";
107+
import { guardTerminalEventStream } from "./terminal-guard";
107108

108109
/**
109110
* Adapters whose continuation state must survive Codex's store:false requests.
@@ -1620,8 +1621,124 @@ export async function handleResponses(
16201621
}
16211622
}
16221623

1624+
// Claude can return a clean end_turn after announcing an edit without emitting any tool call.
1625+
// Keep the normal request/recovery path above intact, and use this bounded callback only for the
1626+
// one internal continuation pass. A continuation failure becomes an in-stream adapter error so
1627+
// the client never sees a second hidden HTTP response or an unbounded retry loop.
1628+
const terminalGuardEnabled = activeAdapter.name === "anthropic" && !options.comboAttempt && !routedCompaction;
1629+
const fetchTerminalGuardContinuation = async function* (nextParsed: OcxParsedRequest): AsyncGenerator<AdapterEvent> {
1630+
let imageTierBias = 0;
1631+
let response: Response | undefined;
1632+
while (true) {
1633+
try {
1634+
const continuationRequest = await activeAdapter.buildRequest(nextParsed, {
1635+
headers: selectedForwardHeaders,
1636+
...(imageTierBias > 0 ? { imageTierBias } : {}),
1637+
});
1638+
const continuationEstimate = typeof continuationRequest.usageLog?.inputTokens === "number"
1639+
? continuationRequest.usageLog.inputTokens
1640+
: undefined;
1641+
if (continuationEstimate !== undefined) logCtx.usageLogInputTokens = continuationEstimate;
1642+
if (activeAdapter.fetchResponse) {
1643+
noteAttemptSend(logCtx.activeAttempt, continuationEstimate);
1644+
response = await activeAdapter.fetchResponse(continuationRequest, {
1645+
abortSignal: upstream.signal,
1646+
timeoutMs: connectMs,
1647+
stream: nextParsed.stream,
1648+
});
1649+
} else {
1650+
response = await fetchWithResetRetry(
1651+
recovery => {
1652+
noteAttemptSend(logCtx.activeAttempt, continuationEstimate, recovery);
1653+
return fetchWithHeaderTimeout(
1654+
continuationRequest.url,
1655+
applyUpstreamRecoveryInit({
1656+
method: continuationRequest.method,
1657+
headers: continuationRequest.headers,
1658+
body: continuationRequest.body,
1659+
}, recovery),
1660+
upstream.signal,
1661+
connectMs,
1662+
nextParsed.stream,
1663+
providerFetch(route.provider),
1664+
);
1665+
},
1666+
{ abortSignal: upstream.signal, label: safeHostLabel(continuationRequest.url) },
1667+
);
1668+
}
1669+
} catch (error) {
1670+
if (options.abortSignal?.aborted) {
1671+
yield { type: "error", message: "client closed request during terminal continuation", status: 499 };
1672+
} else {
1673+
yield { type: "error", message: `Provider continuation failed: ${error instanceof Error ? error.message : String(error)}` };
1674+
}
1675+
return;
1676+
}
1677+
1678+
if (response.status === 429 && hasKeyPoolFailover(route.provider)) {
1679+
const rotated = rotateProviderTransportOn429(config, route.providerName, {
1680+
retryAfter: response.headers.get("retry-after"),
1681+
now: Date.now(),
1682+
attemptedKey: route.provider.apiKey,
1683+
promptCacheKey: nextParsed.options.promptCacheKey,
1684+
});
1685+
if (rotated) {
1686+
try { void response.body?.cancel().catch(() => {}); } catch { /* already closed */ }
1687+
route.provider = rotated;
1688+
activeAdapter = resolveAdapter(
1689+
resolveWireProtocolOverride(route.providerName, route.modelId, route.provider),
1690+
config.cacheRetention,
1691+
);
1692+
continue;
1693+
}
1694+
}
1695+
if (shouldAttemptImageTierRetry({
1696+
status: response.status,
1697+
adapterName: activeAdapter.name,
1698+
parsed: nextParsed,
1699+
alreadyAttempted: imageTierBias > 0,
1700+
})) {
1701+
imageTierBias = 1;
1702+
try { void response.body?.cancel().catch(() => {}); } catch { /* already closed */ }
1703+
continue;
1704+
}
1705+
break;
1706+
}
1707+
1708+
if (!response.ok) {
1709+
const errorText = await response.text().catch(() => "unknown error");
1710+
yield {
1711+
type: "error",
1712+
status: response.status,
1713+
message: `Provider continuation error ${response.status}: ${redactSecretString(errorText.slice(0, 500))}`,
1714+
};
1715+
return;
1716+
}
1717+
1718+
try {
1719+
if (nextParsed.stream) {
1720+
yield* activeAdapter.parseStream(response);
1721+
} else if (activeAdapter.parseResponse) {
1722+
yield* await activeAdapter.parseResponse(response);
1723+
} else {
1724+
yield { type: "error", message: "Provider continuation does not support response parsing" };
1725+
}
1726+
} catch (error) {
1727+
yield { type: "error", message: `Provider continuation parse failed: ${error instanceof Error ? error.message : String(error)}` };
1728+
}
1729+
};
1730+
16231731
if (parsed.stream) {
1624-
const eventStream = activeAdapter.parseStream(upstreamResponse);
1732+
const initialEventStream = activeAdapter.parseStream(upstreamResponse);
1733+
const eventStream = terminalGuardEnabled
1734+
? guardTerminalEventStream({
1735+
parsed,
1736+
firstEvents: initialEventStream,
1737+
adapterName: activeAdapter.name,
1738+
maxAutoContinuations: 1,
1739+
continuation: fetchTerminalGuardContinuation,
1740+
})
1741+
: initialEventStream;
16251742
const { toolNsMap, freeformToolNames, toolSearchToolNames } = buildToolBridgeMaps(parsed);
16261743
const sseStream = bridgeToResponsesSSE(
16271744
eventStream, parsed.modelId, toolNsMap, freeformToolNames, toolSearchToolNames,
@@ -1656,7 +1773,19 @@ export async function handleResponses(
16561773
if (activeAdapter.parseResponse) {
16571774
let events: AdapterEvent[];
16581775
try {
1659-
events = await activeAdapter.parseResponse(upstreamResponse);
1776+
const initialEvents = await activeAdapter.parseResponse(upstreamResponse);
1777+
if (terminalGuardEnabled) {
1778+
events = [];
1779+
for await (const event of guardTerminalEventStream({
1780+
parsed,
1781+
firstEvents: (async function* () { yield* initialEvents; })(),
1782+
adapterName: activeAdapter.name,
1783+
maxAutoContinuations: 1,
1784+
continuation: fetchTerminalGuardContinuation,
1785+
})) events.push(event);
1786+
} else {
1787+
events = initialEvents;
1788+
}
16601789
} finally {
16611790
cleanupUpstreamAbort();
16621791
}

0 commit comments

Comments
 (0)