From bae19fa711e25c0a25c3226a84e5a4e431038d9d Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 4 Sep 2026 20:47:22 +0800 Subject: [PATCH] fix: break the small-model same-parameter compress failure loop early (#156) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rebased onto current master (v0.1.84); core.ts had evolved (+168/-44 since v0.1.65) but kept the same run-to-MAX_LOOP_ROUNDS behavior with no repeat-detection, so the issue still reproduces on the live version. A small model can get stuck re-submitting the EXACT same failing compress call (identical args) every round. Compress validation is deterministic, so an identical call fails identically — re-requesting only burns a real upstream call for an identical result. Previously the loop ran to MAX_LOOP_ROUNDS (10 real upstream calls) before completing gracefully. runCompressLoop (src/loop/core.ts) now tracks the canonical args of every compress/decompress that fails this loop. If a round is ONLY failed compress/decompress calls and one of them re-submits args that already failed, the loop breaks early (graceful completion) instead of running to the limit. - Match is on canonicalized args (parsed + keys sorted), so key-order/whitespace differences do not defeat it. - Fires only when the model is doing nothing but repeating failures: a successful compress or any other proxy call in the round suppresses the break. - The model still gets one full chance to see a failure and correct it (break fires on the SECOND identical submission, verified at round 2 in tests). Files: - src/loop/core.ts: canonicalArgs/sortKeys + failedSignatures + early break - tests/loop-core.test.ts: regression test (breaks early) + guard test - tests/fix-stream.test.ts: abort-signal control test varies summary per round --- src/loop/core.ts | 43 ++++++++++++++++++++++++++++++++ tests/fix-stream.test.ts | 13 +++++++++- tests/loop-core.test.ts | 53 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/src/loop/core.ts b/src/loop/core.ts index 1c3178c150..6b7d04a4b6 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -34,6 +34,27 @@ function stripLoopThinking(messages: CoreMessage[]): CoreMessage[] { return messages.filter((m) => !isLoopThinking(m)); } +// Canonical args for repeat-detection: key-order/whitespace differences must not +// defeat an identical-call match, so parse + re-stringify with sorted keys. +function canonicalArgs(args: string): string { + try { + return JSON.stringify(sortKeys(JSON.parse(args))); + } catch { + return args; + } +} + +function sortKeys(v: unknown): unknown { + if (Array.isArray(v)) return v.map(sortKeys); + if (v !== null && typeof v === "object") { + const src = v as Record; + const out: Record = {}; + for (const k of Object.keys(src).sort()) out[k] = sortKeys(src[k]); + return out; + } + return v; +} + export interface LoopCtx { core: CompressionCore; config: Config; @@ -243,6 +264,11 @@ export async function* runCompressLoop( }, ); + // #156: compress/decompress calls already failed this loop. Validation is + // deterministic (identical args → identical failure), so a byte-identical + // re-submission can never succeed — break early instead of at MAX_LOOP_ROUNDS. + const failedSignatures = new Set(); + try { for (let round = 1; round <= MAX_LOOP_ROUNDS; round++) { if (signal?.aborted) break; @@ -562,6 +588,23 @@ export async function* runCompressLoop( return; } + // #156: if this round is only failed compress/decompress and one of + // them re-submits args that already failed, the model is stuck in a + // deterministic failure loop — break early instead of burning rounds. + const failedMutating = proxyResults.filter( + (pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"), + ); + if (reRequest && failedMutating.length > 0 && failedMutating.length === proxyResults.length) { + const repeated = failedMutating.some((pr) => failedSignatures.has(`${pr.name}\u0000${canonicalArgs(pr.arguments)}`)); + if (repeated) { + ctx.log(`[acp-loop] round ${round}: model re-submitted an already-failed compress with identical arguments; stopping after ${round} round(s) instead of ${MAX_LOOP_ROUNDS}`); + loggerLog("warn", `[acp-loop] repeated identical compress failure at round ${round}; breaking early (#156)`); + yield adapter.emitCompletion({ finishReason: "length", usage }); + return; + } + } + for (const pr of failedMutating) failedSignatures.add(`${pr.name}\u0000${canonicalArgs(pr.arguments)}`); + ctx.log(`[acp-loop] round ${round}: proxy tool executed; re-requesting so the model sees the result`); if (signal?.aborted) break; diff --git a/tests/fix-stream.test.ts b/tests/fix-stream.test.ts index 3b378863fd..121850f8ed 100644 --- a/tests/fix-stream.test.ts +++ b/tests/fix-stream.test.ts @@ -112,7 +112,18 @@ test("client-abort: signal aborted during a re-request stops further re-requests test("client-abort control: without a signal, mutating rounds keep re-requesting up to the loop limit", async () => { let fetchCalls = 0; const orig = globalThis.fetch; - globalThis.fetch = (() => { fetchCalls++; return new Response(mutatingRound(), { status: 200 }); }) as typeof fetch; + globalThis.fetch = (() => { + fetchCalls++; + // Vary the summary each round so no two are byte-identical — the #156 + // early-break must not fire; this control is about the abort signal, not + // the loop limit. + const round = [ + sse("response.created", { response: { id: "resp_1", status: "in_progress" } }), + fcEvents(0, "call_c", "compress", JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: `s${fetchCalls}` }] })), + COMPLETED, + ].join(""); + return new Response(round, { status: 200 }); + }) as typeof fetch; try { await drainSig(new Response(mutatingRound(), { status: 200 }).body!, makeCtx(), undefined); assert.ok(fetchCalls > 1, `control path re-requested multiple times (fetchCalls=${fetchCalls}); abort is what stops it`); diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index 7f4a7f32f6..a83b97c148 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -3,7 +3,7 @@ import assert from "node:assert/strict"; import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState } from "acp-kernel"; import type { Session } from "../src/session.ts"; -import { runCompressLoop, createResponsesAdapter } from "../src/loop/index.ts"; +import { runCompressLoop, createResponsesAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; import type { WireProtocol } from "../src/util.ts"; import { isTransientUpstreamError, REPLAY_MAX_ATTEMPTS, replayBackoffMs, replayMaxAttempts } from "../src/fetch-util.ts"; @@ -195,6 +195,57 @@ test("loop #5: limit-hit graceful — 10 mutating rounds never degenerate empty, } }); +// #156: identical failing compress re-submitted every round is a deterministic +// failure — re-requesting only burns real upstream calls, so break early. +test("loop #156: identical failing compress re-submitted → breaks early, not MAX_LOOP_ROUNDS", async () => { + const failingRound = [ + sse("response.created", { response: { id: "resp_1", status: "in_progress" } }), + fcEvents(0, "call_c", "compress", JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "s" }] })), + COMPLETED, + ].join(""); + let fetchCalls = 0; + const orig = globalThis.fetch; + globalThis.fetch = (async () => { fetchCalls++; return new Response(failingRound, { status: 200 }); }) as typeof fetch; + try { + const out = await drain( + new Response(failingRound, { status: 200 }).body!, + makeCtx(), + { model: "gpt-4o", input: [], stream: true }, + { url: "http://mock", headers: {} }, + ); + assert.ok(/response\.completed/.test(out), "graceful completion present (not degenerate empty)"); + assert.ok(fetchCalls < MAX_LOOP_ROUNDS - 1, `broke early (fetchCalls=${fetchCalls} < ${MAX_LOOP_ROUNDS - 1}) instead of burning all rounds`); + } finally { + globalThis.fetch = orig; + } +}); + +// #156 guard: a failing compress that is NOT the model's only action this round +// must not trigger the early break (the model may still be making progress). +test("loop #156 guard: failing compress + other proxy call → no early break", async () => { + const mixedRound = [ + sse("response.created", { response: { id: "resp_1", status: "in_progress" } }), + fcEvents(0, "call_c", "compress", JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "s" }] })), + fcEvents(1, "call_status", "acp_status", "{}"), + COMPLETED, + ].join(""); + let fetchCalls = 0; + const orig = globalThis.fetch; + globalThis.fetch = (async () => { fetchCalls++; return new Response(mixedRound, { status: 200 }); }) as typeof fetch; + try { + const out = await drain( + new Response(mixedRound, { status: 200 }).body!, + makeCtx(), + { model: "gpt-4o", input: [], stream: true }, + { url: "http://mock", headers: {} }, + ); + assert.ok(/response\.completed/.test(out), "graceful completion present"); + assert.equal(fetchCalls, MAX_LOOP_ROUNDS - 1, "NOT broken early: ran to the normal loop limit"); + } finally { + globalThis.fetch = orig; + } +}); + // Regression guard: the server's runCompressLoop caller used to build LoopCtx // WITHOUT `protocol`, so recordUsage treated every protocol as Anthropic-style // (prompt + cached) and double-counted cached tokens for OpenAI/Responses