Skip to content

Commit bbabe7e

Browse files
committed
fix(agents): classify churn no-progress outcomes
1 parent ac04643 commit bbabe7e

6 files changed

Lines changed: 99 additions & 60 deletions

‎src/agents/agent-tools.before-tool-call.e2e.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,12 @@ describe("before_tool_call loop detection behavior", () => {
590590
});
591591
const history = getDiagnosticSessionState({ sessionId, sessionKey }).toolCallHistory;
592592
expect(history?.[0]?.resultHash).toBeTypeOf("string");
593-
expect(history?.[0]?.resultHash).toBe(history?.[1]?.resultHash);
593+
expect(history?.[0]?.resultHash).not.toBe(history?.[1]?.resultHash);
594+
const noProgressHashes = (history ?? [])
595+
.filter((record) => record.noProgress)
596+
.map((record) => record.resultHash);
597+
expect(noProgressHashes.length).toBeGreaterThanOrEqual(6);
598+
expect(new Set(noProgressHashes).size).toBe(1);
594599
expect(getDiagnosticSessionActivitySnapshot({ sessionId, sessionKey })).toMatchObject({
595600
lastProgressReason: "tool_loop:argument_churn",
596601
});

‎src/agents/tool-loop-argument-churn.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export function getArgumentChurnNoProgressStreak(
1616
if (!record.resultHash) {
1717
continue;
1818
}
19+
if (record.noProgress !== true) {
20+
break;
21+
}
1922
const previous = outcomes.get(record.argsHash);
2023
if (previous && previous.resultHash !== record.resultHash) {
2124
break;

‎src/agents/tool-loop-detection.test.ts‎

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,36 @@ describe("tool-loop-detection", () => {
536536
expect(escapeResult.stuck).toBe(false);
537537
});
538538

539-
it("normalizes built-in write results that only differ by echoed path", () => {
539+
it("normalizes built-in write no-ops that only differ by echoed path", () => {
540+
const state = createState();
541+
const content = "same content";
542+
const paths = ["/tmp/a.md", "/tmp/b.md"];
543+
544+
for (const [index, targetPath] of paths.entries()) {
545+
recordSuccessfulCall(
546+
state,
547+
"write",
548+
{ path: targetPath, content },
549+
{
550+
content: [
551+
{
552+
type: "text",
553+
text: `No changes made to ${targetPath}. The file already has identical content.`,
554+
},
555+
],
556+
details: { changed: false },
557+
},
558+
index,
559+
);
560+
}
561+
562+
const hashes = state.toolCallHistory?.map((record) => record.resultHash);
563+
expect(hashes?.[0]).toBeTypeOf("string");
564+
expect(hashes?.[0]).toBe(hashes?.[1]);
565+
expect(state.toolCallHistory?.every((record) => record.noProgress === true)).toBe(true);
566+
});
567+
568+
it("preserves target identity for successful write outcomes", () => {
540569
const state = createState();
541570
const content = "same content";
542571
const paths = ["/tmp/a.md", "/tmp/b.md"];
@@ -564,9 +593,9 @@ describe("tool-loop-detection", () => {
564593
);
565594
}
566595

567-
const hashes = state.toolCallHistory?.map((record) => record.resultHash);
568-
expect(hashes?.[0]).toBeTypeOf("string");
569-
expect(hashes?.[0]).toBe(hashes?.[1]);
596+
const history = state.toolCallHistory ?? [];
597+
expect(history[0]?.resultHash).not.toBe(history[1]?.resultHash);
598+
expect(history.every((record) => record.noProgress === undefined)).toBe(true);
570599
});
571600

572601
it("uses the supplied warning threshold when reconciling rewritten calls", () => {
@@ -640,28 +669,28 @@ describe("tool-loop-detection", () => {
640669
});
641670
});
642671

643-
it("does not treat distinct stable successful outcomes as semantic no-progress", () => {
672+
it("does not treat generic stable successes as semantic no-progress", () => {
644673
const state = createState();
645674
const paths = ["/tmp/a.md", "/tmp/b.md", "/tmp/a.md", "/tmp/a.md", "/tmp/b.md"];
646675

647-
for (let index = 0; index < GLOBAL_CIRCUIT_BREAKER_THRESHOLD; index += 1) {
676+
for (let index = 0; index < CRITICAL_THRESHOLD; index += 1) {
648677
const targetPath = paths[index % paths.length]!;
649678
recordSuccessfulCall(
650679
state,
651-
"write",
652-
{ path: targetPath, content: "same content" },
680+
"side_effect",
681+
{ path: targetPath },
653682
{
654-
content: [{ type: "text", text: `wrote ${targetPath}` }],
655-
details: { ok: true, path: targetPath },
683+
content: [{ type: "text", text: "done" }],
684+
details: { ok: true },
656685
},
657686
index,
658687
);
659688
}
660689

661690
const loopResult = detectToolCallLoop(
662691
state,
663-
"write",
664-
{ path: "/tmp/a.md", content: "same content" },
692+
"side_effect",
693+
{ path: "/tmp/a.md" },
665694
enabledLoopDetectionConfig,
666695
);
667696

@@ -675,6 +704,39 @@ describe("tool-loop-detection", () => {
675704
}
676705
});
677706

707+
it("keeps repeated stable errors eligible for argument-churn liveness", () => {
708+
const state = createState();
709+
const paths = ["/tmp/a.md", "/tmp/b.md", "/tmp/a.md", "/tmp/a.md", "/tmp/b.md"];
710+
711+
for (let index = 0; index < CRITICAL_THRESHOLD; index += 1) {
712+
const targetPath = paths[index % paths.length]!;
713+
const toolCallId = `failed-${index}`;
714+
const params = { path: targetPath };
715+
recordToolCall(state, "side_effect", params, toolCallId, enabledLoopDetectionConfig);
716+
recordToolCallOutcome(state, {
717+
toolName: "side_effect",
718+
toolParams: params,
719+
toolCallId,
720+
error: new Error("permission denied"),
721+
config: enabledLoopDetectionConfig,
722+
});
723+
}
724+
725+
const loopResult = detectToolCallLoop(
726+
state,
727+
"side_effect",
728+
{ path: "/tmp/a.md" },
729+
enabledLoopDetectionConfig,
730+
);
731+
732+
expect(loopResult).toMatchObject({
733+
stuck: true,
734+
level: "warning",
735+
detector: "argument_churn",
736+
livenessSignal: "argument_churn",
737+
});
738+
});
739+
678740
it("keeps generic critical repeats ahead of warning-only argument churn", () => {
679741
const state = createState();
680742

‎src/agents/tool-loop-detection.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from "./tool-loop-argument-churn.js";
2121
import { isKnownPollToolCall } from "./tool-loop-call-kind.js";
2222
import { TOOL_LOOP_WARNING_THRESHOLD } from "./tool-loop-thresholds.js";
23-
import { normalizeWriteToolOutcomeForLoopDetection } from "./tool-loop-write-outcome.js";
23+
import { isWriteNoProgressOutcome } from "./tool-loop-write-outcome.js";
2424

2525
const log = createSubsystemLogger("agents/loop-detection");
2626

@@ -286,11 +286,12 @@ function hashToolOutcome(
286286
params: unknown,
287287
result: unknown,
288288
error: unknown,
289-
): { resultHash?: string; unknownToolName?: string } {
289+
): { resultHash?: string; noProgress?: true; unknownToolName?: string } {
290290
if (error !== undefined) {
291291
const unknownToolName = extractUnknownToolName(error);
292292
return {
293293
resultHash: `error:${digestStable(formatErrorForHash(error))}`,
294+
noProgress: true,
294295
unknownToolName,
295296
};
296297
}
@@ -311,11 +312,8 @@ function hashToolOutcome(
311312
return { resultHash: execHash };
312313
}
313314
}
314-
if (toolName === "write") {
315-
const writeOutcome = normalizeWriteToolOutcomeForLoopDetection(params, details, text);
316-
if (writeOutcome) {
317-
return { resultHash: digestStable(writeOutcome) };
318-
}
315+
if (toolName === "write" && isWriteNoProgressOutcome(details)) {
316+
return { resultHash: digestStable({ status: "unchanged" }), noProgress: true };
319317
}
320318
if (isKnownPollToolCall(toolName, params) && toolName === "process" && isPlainObject(params)) {
321319
const action = params.action;
@@ -770,6 +768,11 @@ export function recordToolCallOutcome(
770768
continue;
771769
}
772770
call.resultHash = resultHash;
771+
if (outcome.noProgress) {
772+
call.noProgress = true;
773+
} else {
774+
delete call.noProgress;
775+
}
773776
call.unknownToolName = outcome.unknownToolName;
774777
matched = true;
775778
recordedOutcome = call;
@@ -783,6 +786,7 @@ export function recordToolCallOutcome(
783786
toolCallId: params.toolCallId,
784787
...(runId && { runId }),
785788
resultHash,
789+
...(outcome.noProgress ? { noProgress: true as const } : {}),
786790
unknownToolName: outcome.unknownToolName,
787791
timestamp: Date.now(),
788792
};
Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
import { isPlainObject } from "../utils.js";
2-
3-
function normalizeWriteDetails(details: Record<string, unknown>): Record<string, unknown> {
4-
return {
5-
changed: details.changed,
6-
created: details.created,
7-
diff: details.diff,
8-
firstChangedLine: details.firstChangedLine,
9-
};
10-
}
11-
12-
export function normalizeWriteToolOutcomeForLoopDetection(
13-
params: unknown,
14-
details: Record<string, unknown>,
15-
text: string,
16-
): Record<string, unknown> | undefined {
17-
if (!isPlainObject(params)) {
18-
return undefined;
19-
}
20-
const path = typeof params.path === "string" ? params.path : undefined;
21-
const content = typeof params.content === "string" ? params.content : undefined;
22-
if (path === undefined || content === undefined) {
23-
return undefined;
24-
}
25-
const successPrefix = "Successfully wrote ";
26-
const successSuffix = ` bytes to ${path}`;
27-
if (text.startsWith(successPrefix) && text.endsWith(successSuffix)) {
28-
const reportedLength = Number(text.slice(successPrefix.length, -successSuffix.length));
29-
const byteLength = Buffer.byteLength(content, "utf8");
30-
if (reportedLength !== byteLength && reportedLength !== content.length) {
31-
return undefined;
32-
}
33-
// Built-in write results echo the path in both display text and the unified patch.
34-
// Churn still requires each argument variant to repeat; one-time fan-out remains progress.
35-
return { status: "written", byteLength, details: normalizeWriteDetails(details) };
36-
}
37-
if (text === `No changes made to ${path}. The file already has identical content.`) {
38-
return { status: "unchanged", details: normalizeWriteDetails(details) };
39-
}
40-
return undefined;
1+
export function isWriteNoProgressOutcome(details: Record<string, unknown>): boolean {
2+
// The built-in no-op result echoes the requested path in display text.
3+
// Its structured `changed: false` flag is the semantic no-progress contract.
4+
return details.changed === false;
415
}

‎src/logging/diagnostic-session-state.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type ToolCallRecord = {
2525
toolCallId?: string;
2626
runId?: string;
2727
resultHash?: string;
28+
noProgress?: true;
2829
unknownToolName?: string;
2930
timestamp: number;
3031
};

0 commit comments

Comments
 (0)