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
9 changes: 5 additions & 4 deletions pm-stack/aggregator/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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}).`;
Expand Down
69 changes: 69 additions & 0 deletions pm-stack/aggregator/test/attention-classification.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});