Skip to content

Commit 22a9756

Browse files
radroidclaude
authored andcommitted
fix(t3x): don't cancel auto-resume as thread-advanced when the limited turn settles
The projection populates projection_threads.latest_turn_id only while a turn is active, so a usage limit that lands mid-turn captures the running turn's id in the guard baseline — and by wake time (turn settled, session idle) the snapshot reports latestTurn: null. cancelReason treated that null as evidence the thread had advanced and cancelled the pending resume, which killed every real-world resume (observed 3/3 on 2026-07-25; firedAtMs was empty across all threads — the feature had never fired). Advancement now requires positive evidence: a different, NON-NULL turn id. Null means "no active turn" — the expected state after a settled limit. User takeovers stay covered by the user-took-over guard (checked first) and active work by the progressing guard. Regression coverage: a guards.test.ts unit case (null-at-fire vs non-null baseline must not cancel) plus a Reactor.test.ts integration case replaying the production shape (schedule mid-running-turn, settle to latestTurn: null, assert the resume fires with no thread-advanced note). Both verified to fail against the previous guard. Fixes #6 Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 7057813 commit 22a9756

3 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎apps/server/src/t3x/autoResume/Reactor.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const readModel = (o: {
3434
messages?: Array<{ id: string; role: string }>;
3535
status?: string;
3636
latestTurnId?: string;
37+
/** Explicit latestTurn override; pass null to model an idle thread whose
38+
* projection row has no latest_turn_id (see radroid/t3code#6). */
39+
latestTurn?: { turnId: string; state: string } | null;
3740
}): OrchestrationReadModel =>
3841
({
3942
snapshotSequence: 1,
@@ -51,7 +54,10 @@ const readModel = (o: {
5154
settledOverride: null,
5255
messages: o.messages ?? [{ id: "u1", role: "user" }],
5356
activities: [],
54-
latestTurn: { turnId: o.latestTurnId ?? "turn-1", state: "completed" },
57+
latestTurn:
58+
o.latestTurn !== undefined
59+
? o.latestTurn
60+
: { turnId: o.latestTurnId ?? "turn-1", state: "completed" },
5561
session: { status: o.status ?? "ready", providerName: "claudeAgent" },
5662
},
5763
],
@@ -201,6 +207,40 @@ describe("AutoResumeReactor (integration)", () => {
201207
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, TestClock.layer()))),
202208
);
203209

210+
// Regression for radroid/t3code#6 — the incident shape observed in production:
211+
// the limit lands while the turn is RUNNING (baseline captures its id); by wake
212+
// time the turn has settled and the projection row has no latest_turn_id, so the
213+
// snapshot reports latestTurn: null. That must NOT read as "thread-advanced".
214+
it.effect("resumes when the limited turn has settled away by wake time (latestTurn null)", () =>
215+
Effect.gen(function* () {
216+
const { dispatched, modelRef, deps } = yield* harness(
217+
readModel({ status: "running", latestTurn: { turnId: "turn-1", state: "running" } }),
218+
[rejectedEvent(100)],
219+
);
220+
221+
yield* Effect.gen(function* () {
222+
yield* settle; // detection schedules; baseline.latestTurnId === "turn-1"
223+
224+
// The limited turn settles and the session stops during the wait — the
225+
// projection's latest_turn_id empties out, so the snapshot's latestTurn is null.
226+
yield* Ref.set(modelRef, readModel({ status: "stopped", latestTurn: null }));
227+
228+
yield* advancePastResume;
229+
230+
const commands = yield* Ref.get(dispatched);
231+
const turnStarts = commands.filter((c) => c.type === "thread.turn.start");
232+
assert.strictEqual(turnStarts.length, 1, "the settled thread must resume, not cancel");
233+
const summaries = commands
234+
.filter((c) => c.type === "thread.activity.append")
235+
.map((c) => (c as unknown as { activity: { summary: string } }).activity.summary);
236+
assert.isFalse(
237+
summaries.some((s) => s.includes("thread-advanced")),
238+
"no thread-advanced cancellation may be posted",
239+
);
240+
}).pipe(Effect.provide(AutoResumeReactorLive.pipe(Layer.provideMerge(deps))));
241+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, TestClock.layer()))),
242+
);
243+
204244
it.effect("does NOT resume when the user takes over before the window reopens", () =>
205245
Effect.gen(function* () {
206246
const { dispatched, modelRef, deps } = yield* harness(readModel({}), [rejectedEvent(100)]);

‎apps/server/src/t3x/autoResume/guards.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ describe("cancelReason", () => {
166166
expect(cancelReason(thread, baseline())).toBe("thread-advanced");
167167
});
168168

169+
// Regression for radroid/t3code#6: the projection populates latest_turn_id only while
170+
// a turn is active, so a limit captured mid-turn (baseline has the running turn's id)
171+
// always sees latestTurn: null once that turn settles. Null is "no active turn", not
172+
// advancement — cancelling here killed every real-world resume.
173+
it("does NOT treat a settled-away turn (latestTurn null at fire) as advancement", () => {
174+
const thread = makeThread({
175+
messages: [{ id: "u1", role: "user" }],
176+
status: "stopped",
177+
});
178+
expect(thread.latestTurn).toBeNull();
179+
expect(cancelReason(thread, baseline())).toBeNull();
180+
});
181+
182+
it("still detects advancement when a turn exists but the baseline had none", () => {
183+
const noTurnBaseline = captureBaseline(makeThread({ messages: [{ id: "u1", role: "user" }] }));
184+
const thread = makeThread({ messages: [{ id: "u1", role: "user" }], latestTurnId: "turn-9" });
185+
expect(cancelReason(thread, noTurnBaseline)).toBe("thread-advanced");
186+
});
187+
169188
it("blocks when awaiting input", () => {
170189
const thread = makeThread({
171190
messages: [{ id: "u1", role: "user" }],

‎apps/server/src/t3x/autoResume/guards.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ export function cancelReason(
130130
if (threadIsProgressing(thread)) return "progressing";
131131
if (hasOpenBlockingRequest(thread.activities)) return "awaiting-input";
132132
if (newestUserMessageId(thread) !== baseline.newestUserMessageId) return "user-took-over";
133-
if ((thread.latestTurn?.turnId ?? null) !== baseline.latestTurnId) return "thread-advanced";
133+
// Advancement needs POSITIVE evidence: a different, non-null turn id. The snapshot's
134+
// `latestTurn` is joined on `projection_threads.latest_turn_id`, which is populated
135+
// only while a turn is active — so a usage limit that lands mid-turn captures the
136+
// running turn's id in the baseline, and by fire time (turn settled, session idle)
137+
// the snapshot reports `latestTurn: null`. That null means "no active turn", not
138+
// "the thread moved on"; treating it as advancement cancelled every real resume
139+
// (radroid/t3code#6). A genuine user takeover is caught above via the newest user
140+
// message; active work is caught by `progressing`.
141+
const currentTurnId = thread.latestTurn?.turnId ?? null;
142+
if (currentTurnId !== null && currentTurnId !== baseline.latestTurnId) {
143+
return "thread-advanced";
144+
}
134145
return null;
135146
}

0 commit comments

Comments
 (0)