Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
const out: Record<string, unknown> = {};
for (const k of Object.keys(src).sort()) out[k] = sortKeys(src[k]);
return out;
}
return v;
}

export interface LoopCtx {
core: CompressionCore;
config: Config;
Expand Down Expand Up @@ -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<string>();

try {
for (let round = 1; round <= MAX_LOOP_ROUNDS; round++) {
if (signal?.aborted) break;
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion tests/fix-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
53 changes: 52 additions & 1 deletion tests/loop-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
Loading