From 08248f95d66ef1d6f68c038b0fec011224b35b6b Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 03:51:02 -0600 Subject: [PATCH] Fix stuck-retrying attention precedence --- pm-stack/aggregator/src/derive.ts | 9 +-- .../test/attention-classification.test.ts | 69 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 pm-stack/aggregator/test/attention-classification.test.ts diff --git a/pm-stack/aggregator/src/derive.ts b/pm-stack/aggregator/src/derive.ts index 609f86536f..efb59eae6b 100644 --- a/pm-stack/aggregator/src/derive.ts +++ b/pm-stack/aggregator/src/derive.ts @@ -192,7 +192,8 @@ export const RETRY_LOOP_THRESHOLD = 3; /** * Classify one session feed entry. Ordering matters: the first rule that - * matches wins, so a walled-spin with a question still surfaces the question. + * matches wins. Questions stay first; a retry loop outranks its last-turn + * error once the repeated-error threshold is reached. */ export function classifyAttention(e: SessionFeedEntry, now = Date.now()): AttentionEntry { let state: AttentionState = "in-motion"; @@ -201,12 +202,12 @@ export function classifyAttention(e: SessionFeedEntry, now = Date.now()): Attent if (e.awaitingQuestion) { state = "stopped-for-question"; evidence = `Waiting on: ${e.awaitingQuestion}`; - } else if (e.lastTurnError) { - state = "errored"; - evidence = `Last turn error: ${e.lastTurnError}`; } else if ((e.repeatedErrorCount ?? 0) >= RETRY_LOOP_THRESHOLD) { state = "stuck-retrying"; evidence = `${e.repeatedErrorCount} consecutive identical errors, no progress.`; + } else if (e.lastTurnError) { + state = "errored"; + evidence = `Last turn error: ${e.lastTurnError}`; } else if (e.longRunningCall && Date.parse(e.longRunningCall.startedAt) + CLI_STALL_MS < now) { state = "stalled-on-cli"; evidence = `CLI running long: ${e.longRunningCall.command ?? "unknown"} (since ${e.longRunningCall.startedAt}).`; diff --git a/pm-stack/aggregator/test/attention-classification.test.ts b/pm-stack/aggregator/test/attention-classification.test.ts new file mode 100644 index 0000000000..684ff31b1c --- /dev/null +++ b/pm-stack/aggregator/test/attention-classification.test.ts @@ -0,0 +1,69 @@ +import { describe, expect, test } from "bun:test"; +import { classifyAttention, tierEntry } from "../src/derive.ts"; +import { FIXTURE_ATTENTION } from "../src/sources/fixtures.ts"; + +describe("attention classification", () => { + const now = Date.parse("2026-10-25T02:00:00Z"); + + test("retry-loop threshold outranks turn errors, while questions remain first", () => { + const mapperShaped = classifyAttention( + { + sessionId: "retry-with-error", + lastActivityAt: "2026-10-25T01:00:00Z", + lastTurnError: "boom", + repeatedErrorCount: 4, + }, + now, + ); + expect(mapperShaped.state).toBe("stuck-retrying"); + + expect( + classifyAttention( + { + sessionId: "threshold-minus-one", + lastActivityAt: "2026-10-25T01:00:00Z", + lastTurnError: "boom", + repeatedErrorCount: 2, + }, + now, + ).state, + ).toBe("errored"); + expect( + classifyAttention( + { sessionId: "single-error", lastActivityAt: "2026-10-25T01:00:00Z", lastTurnError: "boom" }, + now, + ).state, + ).toBe("errored"); + + expect( + classifyAttention( + { + sessionId: "question-and-retry", + lastActivityAt: "2026-10-25T01:00:00Z", + awaitingQuestion: "which repo first?", + lastTurnError: "boom", + repeatedErrorCount: 4, + }, + now, + ).state, + ).toBe("stopped-for-question"); + + const credentialRetry = tierEntry( + classifyAttention( + { + sessionId: "credential-retry", + lastActivityAt: "2026-10-25T01:00:00Z", + lastTurnError: "verification code required", + repeatedErrorCount: 4, + }, + now, + ), + ); + expect(credentialRetry.state).toBe("stuck-retrying"); + expect(credentialRetry.tier).toBe("needs-drew"); + + const phase13 = FIXTURE_ATTENTION.find((entry) => entry.sessionId === "ses_f_phase13"); + expect(phase13).toBeDefined(); + expect(classifyAttention(phase13!, now).state).toBe("stuck-retrying"); + }); +});