diff --git a/src/loop/core.ts b/src/loop/core.ts index 269bb7a600..a82c50a705 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -33,7 +33,7 @@ import { applyRanges } from "../stream.js"; import { applyConfiguredCompression } from "../external-summary-compress.js"; import { executeAcpRetire, executeAcpRestore, executeAcpRewrite, effectiveBlockOpsConfig } from "../acp-block-ops.js"; import { executeSearchContextTarget, resolveDecompress } from "../decompress-shared.js"; -import { toolFail, toolOk, type ProxyToolResult } from "../proxy-tool-result.js"; +import { toolFail, toolOk, type ProxyToolResult, type ToolOutcomeKind } from "../proxy-tool-result.js"; import { fetchWithRetry, UpstreamHttpError } from "../fetch-util.js"; import { noteUpstreamThrottle429, noteUpstreamThrottleResolved, throttle429ModelScope } from "../upstream-throttle.js"; import { classifyUpstreamFailure, type SkippedProxyInfo, type UpstreamFailureKind } from "../upstream-fail.js"; @@ -195,11 +195,18 @@ function isReasoningRequiredError(e: unknown): boolean { return e instanceof UpstreamHttpError && /must be passed back/i.test(e.body); } -function proxyToolResultFailed(result: string): boolean { - // A successful compression receipt embeds the model-authored summary in later fingerprint - // lines. That summary may legitimately quote an earlier FAILED tool result; only the status - // line is authoritative for whether this tool call itself failed. - return (result.trimStart().split("\n", 1)[0] ?? "").includes("FAILED"); +function proxyToolResultFailed(pr: { name: string; outcome?: ToolOutcomeKind; result: string }): boolean { + // #5383: the structured outcome is authoritative when the dispatcher carries + // it — a compress is "failed" only when refused (applied/partial both folded + // something); every other tool fails on "failure". Only when no outcome is + // present do we fall back to the first status line: a successful compression + // receipt embeds the model-authored summary in later fingerprint lines, which + // may legitimately quote an earlier FAILED result, so the whole body is not + // authoritative for whether this call itself failed. + if (pr.outcome !== undefined) { + return pr.name === "compress" ? pr.outcome === "refused" : pr.outcome === "failure"; + } + return (pr.result.trimStart().split("\n", 1)[0] ?? "").includes("FAILED"); } // #1453 client-facing labels for transport failures that end the turn in-band. @@ -1426,7 +1433,7 @@ export async function* runCompressLoop( let realCalls = 0; const realToolCalls: ToolCallEmit[] = []; - const proxyResults: { name: string; callId: string; result: string; arguments: string; wireArguments: string; signature?: string; image?: { base64: string; mediaType: string } }[] = []; + const proxyResults: { name: string; callId: string; result: string; outcome?: ToolOutcomeKind; arguments: string; wireArguments: string; signature?: string; image?: { base64: string; mediaType: string } }[] = []; // #5220: the marker lines ACTUALLY streamed to the client this round // (deduped, same order as the stream). On a failed-only round this IS // the recovery receipt the retry body must carry — reconstructing it @@ -1457,7 +1464,7 @@ export async function* runCompressLoop( parsedArgs = {}; } const result = await withSessionLock(ctx.session, () => executeProxyToolAsync(call.name, parsedArgs, ctx, call.callId, rawArgs, signal)); - proxyResults.push({ name: call.name, callId: call.callId, result: result.text, arguments: call.arguments, wireArguments: rawArgs === undefined ? call.arguments : MALFORMED_PROXY_TOOL_ARGS_REPLAY, signature: call.signature, ...(result.image ? { image: result.image } : {}) }); + proxyResults.push({ name: call.name, callId: call.callId, result: result.text, outcome: result.outcome, arguments: call.arguments, wireArguments: rawArgs === undefined ? call.arguments : MALFORMED_PROXY_TOOL_ARGS_REPLAY, signature: call.signature, ...(result.image ? { image: result.image } : {}) }); if (ctx.visibilityMarkers !== false) { const markerKey = `${call.name}\u0000${result.text}`; if (seenMarkers.has(markerKey)) { @@ -1650,7 +1657,7 @@ export async function* runCompressLoop( if (threadContinue) threadDelta.push(injection); } const anyCompressFailed = proxyResults.some( - (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), + (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr), ); if (!ctx.textProtocol && !anyCompressFailed) { const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool); @@ -1670,7 +1677,7 @@ export async function* runCompressLoop( const absorbName = activeAbsorbToolName(ctx.session, ctx.config); if ( ctx.refreshFolded && - proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !proxyToolResultFailed(pr.result)) + proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !proxyToolResultFailed(pr)) ) { try { const refreshed = await ctx.refreshFolded(coreMessages); @@ -1759,7 +1766,7 @@ export async function* runCompressLoop( // carry server-invisible bytes upstream — exactly the seam #4186 // forbids. const failedMutating = proxyResults.filter( - (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), + (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr), ); if (!probeWarned) { @@ -1880,14 +1887,14 @@ export async function* runCompressLoop( let committedTokens = 0; let committedBlocks = 0; for (const pr of proxyResults) { - if (pr.name === "compress" && !proxyToolResultFailed(pr.result)) { + if (pr.name === "compress" && !proxyToolResultFailed(pr)) { const saved = /~(\d[\d,]*) tokens saved/.exec(pr.result); if (saved) committedTokens += Number(saved[1]!.replace(/,/g, "")); const blocks = /(\d+) block\(s\)/.exec(pr.result); if (blocks) committedBlocks += Number(blocks[1]); } } - const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !proxyToolResultFailed(pr.result)); + const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !proxyToolResultFailed(pr)); const note = committedTokens > 0 ? `compression committed (~${committedTokens.toLocaleString("en-US")} tokens saved${committedBlocks > 0 ? ` in ${committedBlocks} block(s)` : ""}); ` : absorbed ? "context work committed; " : ""; diff --git a/tests/issue564-responses-ordering.test.ts b/tests/issue564-responses-ordering.test.ts index 25ba088981..c90a970aab 100644 --- a/tests/issue564-responses-ordering.test.ts +++ b/tests/issue564-responses-ordering.test.ts @@ -160,7 +160,7 @@ test("#564 path 2: compress re-request keeps Responses assistant run ordering va const endRef = refOf(fcoA!); assert.ok(startRef && endRef, "refs resolved for the bash_a interaction"); - const args = JSON.stringify({ content: [{ startId: startRef!, endId: endRef!, summary: "Inspected files A and B." }] }); + const args = JSON.stringify({ content: [{ startId: startRef!, endId: endRef!, summary: "ok" }] }); const round1 = [ sse("response.created", { response: { id: "resp_1", status: "in_progress" } }), fcEvents(0, "call_c", "compress", args), diff --git a/tests/plugin-protocol.test.ts b/tests/plugin-protocol.test.ts index ead40e5411..cb19813278 100644 --- a/tests/plugin-protocol.test.ts +++ b/tests/plugin-protocol.test.ts @@ -84,6 +84,25 @@ function compressToolScript(summary = "plugin e2e compress summary with enough c ]; } +function decompressToolScript(blockId = "synthetic-missing-block"): string[] { + const decompressArgs = JSON.stringify({ blockId }); + return [ + anthropicSse("message_start", { + type: "message_start", + message: { id: "msg_plug_3", role: "assistant", usage: { input_tokens: 55 } }, + }), + anthropicSse("content_block_start", { + type: "content_block_start", + index: 0, + content_block: { type: "tool_use", id: "toolu_d_1", name: "decompress", input: {} }, + }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: decompressArgs } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use", stop_sequence: null }, usage: { output_tokens: 12 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + async function startHarness( scripts: string[][], kernelConfig?: Config, @@ -901,6 +920,27 @@ test("a successful compression summary containing FAILED still continues with ma } }); +test("a genuine decompress failure without the FAILED keyword is classified as failed", async () => { + const h = await startHarness([decompressToolScript("synthetic-missing-block")], undefined, { visibilityMarkers: false }); + try { + const { raw } = await callPluginAnthropic(h, "plug-conv-decompress-notfound", sizedPluginHistory(), { + agent: "hermes", + tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({ + name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} }, + })), + compressionProxy: true, + }); + + // A not-found decompress returns outcome:"failure" with no "FAILED" on the first + // line. With the structured outcome it is classified as failed, so a failed-only + // round with visibility markers off ends the turn instead of re-requesting (the + // pre-fix first-line classifier saw no FAILED and re-requested a second time). + assert.equal(h.captured.length, 1, `a genuine decompress failure must be classified as failed and end the turn; raw=${raw}`); + } finally { + await h.close(); + } +}); + // Hermes defers plugin toolsets through tool_search, so a healthy plugin session sends requests // whose tools array carries NONE of the ACP names. Those requests must still bind the plugin // identity and budget (that is exactly what /acp reads) while compression falls back to the proxy —