@devries-cameron20 / t3code Mirror · read-only / commits / b14120fbc8
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 22:00 UTC

fix(threads): preserve snooze when an existing run completes (#17118)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Yasin Kavakli committed Oct 20, 2026 at 07:07 UTC b14120fbc8ff47d6e090e64e31ddb8b2bff75c2f
6 files changed +105 -62
apps/server/src/mcp/toolkits/orchestrator/tools.ts
+1 -1
@@ -192,7 +192,7 @@ export const CreateThreadsTool = Tool.make("create_threads", {
192
193 const ThreadListTool = Tool.make("t3_thread_list", {
194 description:
195 - "List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early when it asks for something, fails, or completes. pullRequests lists every pull request linked to each thread; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
195 + "List T3 threads in a project, newest first. Omit projectId for the calling thread's project. Filter by durable run status, title, or settled state (settled=true lists threads the user or auto-settlement moved out of the active list), or snoozed state, and paginate with the returned cursor. A snoozed thread wakes early when it asks for something, fails after snoozing, or completes a run requested after snoozing. pullRequests lists every pull request linked to each thread; linkedPullRequest is a legacy single link and can be null while pullRequests still has entries. To link a thread for the user, write `[title](t3-thread://v1/<threadId>)` with the threadId exactly as returned, not URL-encoded; T3 Code shows the thread's current title.",
196 parameters: OrchestratorMcpThreadListInput,
197 success: OrchestratorMcpThreadListResult,
198 failure: OrchestratorMcpFailure,
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+32 -21
@@ -163,7 +163,7 @@ describe("isAutoSettlementCandidate", () => {
163 ).toBe(true);
164 });
165
166 - it("keeps snoozed threads parked until they wake early on error or completion", () => {
166 + it("keeps snoozed threads parked until a fresh failure or timer wake", () => {
167 const snoozed = shell({
168 snoozedUntil: at(60 * 60 * 1_000),
169 snoozedAt: at(-60 * 60 * 1_000),
@@ -175,19 +175,6 @@ describe("isAutoSettlementCandidate", () => {
175 NOW_MS,
176 ),
177 ).toBe(true);
178 - expect(
179 - ThreadSettlementService.isAutoSettlementCandidate(
180 - shell({ ...snoozed, status: "completed", latestRunCompletedAt: at(-30 * 60 * 1_000) }),
181 - NOW_MS,
182 - ),
183 - ).toBe(true);
184 - // An interrupted run leaves the thread snoozed, as the sidebar shows it.
185 - expect(
186 - ThreadSettlementService.isAutoSettlementCandidate(
187 - shell({ ...snoozed, status: "interrupted", latestRunCompletedAt: at(-30 * 60 * 1_000) }),
188 - NOW_MS,
189 - ),
190 - ).toBe(false);
178 expect(
179 ThreadSettlementService.isAutoSettlementCandidate(
180 shell({ ...snoozed, status: "failed", latestRunCompletedAt: at(-2 * 60 * 60 * 1_000) }),
@@ -1418,7 +1405,37 @@ describe("ThreadSettlementServiceV2 single-thread sweeps", () => {
1405
1406 describe("isSnoozed", () => {
1407 const snoozed = { snoozedAt: at(-2 * DAY_MS), snoozedUntil: at(DAY_MS) };
1421 - it("wakes for completed work after the snooze, but not an interrupted run", () => {
1408 + it.each([
1409 + { boundary: "before snooze", requestedAt: at(-2 * DAY_MS - 1), snoozed: true },
1410 + { boundary: "at snooze", requestedAt: snoozed.snoozedAt, snoozed: true },
1411 + { boundary: "unknown", requestedAt: null, snoozed: true },
1412 + { boundary: "after snooze", requestedAt: at(-2 * DAY_MS + 1), snoozed: false },
1413 + ])(
1414 + "agrees on snooze and settlement with completed request time $boundary",
1415 + ({ requestedAt, snoozed: parked }) => {
1416 + const thread = shell({
1417 + ...snoozed,
1418 + status: "completed",
1419 + latestRunRequestedAt: requestedAt,
1420 + latestRunStartedAt: at(-DAY_MS - 1),
1421 + latestRunCompletedAt: at(-DAY_MS),
1422 + });
1423 +
1424 + expect(ThreadSettlementService.isSnoozed(thread, NOW_MS)).toBe(parked);
1425 + expect(ThreadSettlementService.isAutoSettlementCandidate(thread, NOW_MS)).toBe(!parked);
1426 + expect(
1427 + ThreadSettlementService.resolveAutoSettlementAt({
1428 + thread,
1429 + pullRequest: { state: "merged", mergedAt: DateTime.formatIso(at(-1)) },
1430 + nowMs: NOW_MS,
1431 + autoSettleAfterDays: null,
1432 + autoSettleOnMerge: true,
1433 + }),
1434 + ).toEqual(parked ? null : at(-DAY_MS));
1435 + },
1436 + );
1437 +
1438 + it("keeps quiet and interrupted threads snoozed until the timer expires", () => {
1439 expect(ThreadSettlementService.isSnoozed(shell(snoozed), NOW_MS)).toBe(true);
1440 expect(
1441 ThreadSettlementService.isSnoozed(
@@ -1426,12 +1443,6 @@ describe("isSnoozed", () => {
1443 NOW_MS,
1444 ),
1445 ).toBe(true);
1429 - expect(
1430 - ThreadSettlementService.isSnoozed(
1431 - shell({ ...snoozed, status: "completed", latestRunCompletedAt: at(-DAY_MS) }),
1432 - NOW_MS,
1433 - ),
1434 - ).toBe(false);
1446 expect(
1447 ThreadSettlementService.isSnoozed(shell({ ...snoozed, snoozedUntil: at(-1) }), NOW_MS),
1448 ).toBe(false);
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+9 -1
@@ -177,7 +177,12 @@ export function isAutoSettlementCandidate(
177 export function isSnoozed(
178 thread: Pick<
179 ProjectionStore.ProjectionSettlementCandidate,
180 - "snoozedUntil" | "snoozedAt" | "latestRunCompletedAt" | "status" | "pendingRuntimeRequest"
180 + | "snoozedUntil"
181 + | "snoozedAt"
182 + | "latestRunRequestedAt"
183 + | "latestRunCompletedAt"
184 + | "status"
185 + | "pendingRuntimeRequest"
186 >,
187 nowMs: number,
188 ): boolean {
@@ -185,6 +190,7 @@ export function isSnoozed(
190 if (snoozedUntilMs === null || snoozedUntilMs <= nowMs) return false;
191 if (thread.pendingRuntimeRequest !== null) return false;
192 const snoozedAtMs = toMillis(thread.snoozedAt);
193 + const requestedAtMs = toMillis(thread.latestRunRequestedAt);
194 const completedAtMs = toMillis(thread.latestRunCompletedAt);
195 const wokeOnError =
196 thread.status === "failed" &&
@@ -192,6 +198,8 @@ export function isSnoozed(
198 const wokeOnCompletion =
199 thread.status === "completed" &&
200 snoozedAtMs !== null &&
201 + requestedAtMs !== null &&
202 + requestedAtMs > snoozedAtMs &&
203 completedAtMs !== null &&
204 completedAtMs > snoozedAtMs;
205 return !wokeOnError && !wokeOnCompletion;
docs/orchestration-v2/orchestrator-mcp-server.md
+2 -1
@@ -391,7 +391,8 @@ relabels local matches; cross-environment links can retain the written label.
391 List and read results report `snoozed` and `snoozedUntil`, and
392 `t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows the
393 client's `effectiveSnoozed`, so agents and the sidebar agree: a snoozed thread
394 -wakes early when it has a pending request, fails, or completes after the snooze.
394 +wakes early when it has a pending request, fails after snoozing, or completes a
395 +run requested after snoozing.
396
397 ### `t3_thread_update`
398
packages/client-runtime/src/state/threadSettled.ts
+12 -12
@@ -83,7 +83,6 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean
83 if (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;
84 if (shell.source?.pendingRuntimeRequest != null) return true;
85 if (shell.pendingSubagentRequest != null) return true;
86 - const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
86 const failedAt = failureEdgeAt(shell);
87 if (
88 failedAt !== undefined &&
@@ -92,15 +91,23 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean
91 ) {
92 return true;
93 }
94 + return completionWakeAt(shell) !== null;
95 +}
96 +
97 +/** A snooze acknowledges the run already requested, including its eventual result. */
98 +function completionWakeAt(shell: ThreadSnoozeShell): string | null {
99 + const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
100 if (
101 shell.snoozedAt != null &&
102 (latestRun?.state === "completed" || latestRun?.status === "completed") &&
103 + latestRun.requestedAt != null &&
104 + Date.parse(latestRun.requestedAt) > Date.parse(shell.snoozedAt) &&
105 latestRun.completedAt != null &&
106 Date.parse(latestRun.completedAt) > Date.parse(shell.snoozedAt)
107 ) {
101 - return true;
108 + return latestRun.completedAt;
109 }
103 - return false;
110 + return null;
111 }
112
113 // V2 runtime.updatedAt includes metadata writes; prefer the failed run's completion.
@@ -161,15 +168,8 @@ export function threadWokeAt(
168 // indicator the user already cleared by visiting (snoozedUntil is newer
169 // than that visit's lastVisitedAt).
170 if (threadRaisedHandWhileSnoozed(shell)) {
164 - const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
165 - if (
166 - shell.snoozedAt != null &&
167 - (latestRun?.state === "completed" || latestRun?.status === "completed") &&
168 - latestRun.completedAt != null &&
169 - Date.parse(latestRun.completedAt) > Date.parse(shell.snoozedAt)
170 - ) {
171 - return latestRun.completedAt;
172 - }
171 + const completedAt = completionWakeAt(shell);
172 + if (completedAt !== null) return completedAt;
173 // An old failure must not replace the activity time of an approval or input wake.
174 const failedAt = failureEdgeAt(shell);
175 if (
packages/client-runtime/src/state/threadSnoozed.test.ts
+49 -26
@@ -28,6 +28,7 @@ function makeShell(input: {
28 readonly snoozedAt?: string | null;
29 readonly sessionStatus?: "starting" | "running" | "ready" | "error";
30 readonly pending?: "approval" | "user-input";
31 + readonly turnRequestedAt?: string | null;
32 readonly turnCompletedAt?: string | null;
33 }): ThreadSnoozeShell {
34 const threadId = ThreadId.make("thread-1");
@@ -54,7 +55,7 @@ function makeShell(input: {
55 : {
56 turnId: TurnId.make("turn-1"),
57 state: "completed",
57 - requestedAt: SNOOZED_AT,
58 + requestedAt: input.turnRequestedAt === undefined ? SNOOZED_AT : input.turnRequestedAt,
59 startedAt: null,
60 completedAt: input.turnCompletedAt,
61 assistantMessageId: null,
@@ -167,14 +168,29 @@ describe("effectiveSnoozed", () => {
168 ).toBe(true);
169 });
170
170 - it("wakes early when a run completes after the snooze was set", () => {
171 - expect(
172 - effectiveSnoozed(
173 - makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
174 - { now: NOW },
175 - ),
176 - ).toBe(false);
177 - });
171 + it.each([
172 + { boundary: "before snooze", requestedAt: "2026-04-10T08:59:59.999Z", snoozed: true },
173 + { boundary: "at snooze", requestedAt: SNOOZED_AT, snoozed: true },
174 + { boundary: "unknown", requestedAt: null, snoozed: true },
175 + { boundary: "after snooze", requestedAt: "2026-04-10T09:00:00.001Z", snoozed: false },
176 + ])(
177 + "classifies completion and its wake time with request time $boundary",
178 + ({ requestedAt, snoozed }) => {
179 + const completedAt = "2026-04-10T10:30:00.000Z";
180 + const shell = {
181 + ...makeShell({ snoozedUntil: FUTURE_WAKE }),
182 + latestRun: {
183 + status: "completed",
184 + requestedAt,
185 + startedAt: "2026-04-10T09:15:00.000Z",
186 + completedAt,
187 + },
188 + };
189 +
190 + expect(effectiveSnoozed(shell, { now: NOW })).toBe(snoozed);
191 + expect(threadWokeAt(shell, { now: NOW })).toBe(snoozed ? null : completedAt);
192 + },
193 + );
194
195 it("ignores runs that completed before the snooze — the user saw that result", () => {
196 expect(
@@ -306,31 +322,38 @@ describe("threadWokeAt", () => {
322 expect(threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(null);
323 });
324
309 - it("reports the wake time for a timer wake", () => {
325 + it("reports the timer wake even after an already-requested run completes", () => {
326 expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);
327 + const shell = makeShell({
328 + snoozedUntil: PAST_WAKE,
329 + turnCompletedAt: "2026-04-10T09:30:00.000Z",
330 + });
331 + expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
332 + expect(threadWokeAt(shell, { now: NOW })).toBe(PAST_WAKE);
333 });
334
313 - it("reports the completion time for an early run-completed wake", () => {
314 - expect(
315 - threadWokeAt(
316 - makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),
317 - { now: NOW },
318 - ),
319 - ).toBe("2026-04-10T10:30:00.000Z");
320 - });
321 -
322 - it("falls back to session activity for blocked/failed early wakes", () => {
323 - expect(
324 - threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {
325 - now: NOW,
326 - }),
327 - ).toBe("2026-04-10T11:00:00.000Z");
335 + it.each([
336 + { reason: "failure", sessionStatus: "error" },
337 + { reason: "approval", sessionStatus: "ready", pending: "approval" },
338 + { reason: "user input", sessionStatus: "ready", pending: "user-input" },
339 + ] as const)("reports $reason activity instead of an already-requested completion", (activity) => {
340 + const shell = makeShell({
341 + ...activity,
342 + snoozedUntil: FUTURE_WAKE,
343 + turnCompletedAt: "2026-04-10T10:30:00.000Z",
344 + });
345 + expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
346 + expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T11:00:00.000Z");
347 });
348
349 it("keeps the early wake authoritative after the scheduled time passes", () => {
350 expect(
351 threadWokeAt(
333 - makeShell({ snoozedUntil: PAST_WAKE, turnCompletedAt: "2026-04-10T09:30:00.000Z" }),
352 + makeShell({
353 + snoozedUntil: PAST_WAKE,
354 + turnRequestedAt: "2026-04-10T09:15:00.000Z",
355 + turnCompletedAt: "2026-04-10T09:30:00.000Z",
356 + }),
357 { now: NOW },
358 ),
359 ).toBe("2026-04-10T09:30:00.000Z");