TypeScript
fix(threads): preserve snooze when an existing run completes (#17118)
Yasin Kavaklib14120fbc8ff
diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.tsindex 867555244fa..893dd7da0fe 100644--- a/apps/server/src/mcp/toolkits/orchestrator/tools.ts+++ b/apps/server/src/mcp/toolkits/orchestrator/tools.ts@@ -192,7 +192,7 @@ export const CreateThreadsTool = Tool.make("create_threads", {const ThreadListTool = Tool.make("t3_thread_list", {description:- "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.",+ "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.",parameters: OrchestratorMcpThreadListInput,success: OrchestratorMcpThreadListResult,failure: OrchestratorMcpFailure,diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.tsindex b22ff6a311e..dff91b8c85d 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts@@ -163,7 +163,7 @@ describe("isAutoSettlementCandidate", () => {).toBe(true);});- it("keeps snoozed threads parked until they wake early on error or completion", () => {+ it("keeps snoozed threads parked until a fresh failure or timer wake", () => {const snoozed = shell({snoozedUntil: at(60 * 60 * 1_000),snoozedAt: at(-60 * 60 * 1_000),@@ -175,19 +175,6 @@ describe("isAutoSettlementCandidate", () => {NOW_MS,),).toBe(true);- expect(- ThreadSettlementService.isAutoSettlementCandidate(- shell({ ...snoozed, status: "completed", latestRunCompletedAt: at(-30 * 60 * 1_000) }),- NOW_MS,- ),- ).toBe(true);- // An interrupted run leaves the thread snoozed, as the sidebar shows it.- expect(- ThreadSettlementService.isAutoSettlementCandidate(- shell({ ...snoozed, status: "interrupted", latestRunCompletedAt: at(-30 * 60 * 1_000) }),- NOW_MS,- ),- ).toBe(false);expect(ThreadSettlementService.isAutoSettlementCandidate(shell({ ...snoozed, status: "failed", latestRunCompletedAt: at(-2 * 60 * 60 * 1_000) }),@@ -1418,7 +1405,37 @@ describe("ThreadSettlementServiceV2 single-thread sweeps", () => {describe("isSnoozed", () => {const snoozed = { snoozedAt: at(-2 * DAY_MS), snoozedUntil: at(DAY_MS) };- it("wakes for completed work after the snooze, but not an interrupted run", () => {+ it.each([+ { boundary: "before snooze", requestedAt: at(-2 * DAY_MS - 1), snoozed: true },+ { boundary: "at snooze", requestedAt: snoozed.snoozedAt, snoozed: true },+ { boundary: "unknown", requestedAt: null, snoozed: true },+ { boundary: "after snooze", requestedAt: at(-2 * DAY_MS + 1), snoozed: false },+ ])(+ "agrees on snooze and settlement with completed request time $boundary",+ ({ requestedAt, snoozed: parked }) => {+ const thread = shell({+ ...snoozed,+ status: "completed",+ latestRunRequestedAt: requestedAt,+ latestRunStartedAt: at(-DAY_MS - 1),+ latestRunCompletedAt: at(-DAY_MS),+ });++ expect(ThreadSettlementService.isSnoozed(thread, NOW_MS)).toBe(parked);+ expect(ThreadSettlementService.isAutoSettlementCandidate(thread, NOW_MS)).toBe(!parked);+ expect(+ ThreadSettlementService.resolveAutoSettlementAt({+ thread,+ pullRequest: { state: "merged", mergedAt: DateTime.formatIso(at(-1)) },+ nowMs: NOW_MS,+ autoSettleAfterDays: null,+ autoSettleOnMerge: true,+ }),+ ).toEqual(parked ? null : at(-DAY_MS));+ },+ );++ it("keeps quiet and interrupted threads snoozed until the timer expires", () => {expect(ThreadSettlementService.isSnoozed(shell(snoozed), NOW_MS)).toBe(true);expect(ThreadSettlementService.isSnoozed(@@ -1426,12 +1443,6 @@ describe("isSnoozed", () => {NOW_MS,),).toBe(true);- expect(- ThreadSettlementService.isSnoozed(- shell({ ...snoozed, status: "completed", latestRunCompletedAt: at(-DAY_MS) }),- NOW_MS,- ),- ).toBe(false);expect(ThreadSettlementService.isSnoozed(shell({ ...snoozed, snoozedUntil: at(-1) }), NOW_MS),).toBe(false);diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.tsindex 64a54e92a59..fe0e57e71d1 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts@@ -177,7 +177,12 @@ export function isAutoSettlementCandidate(export function isSnoozed(thread: Pick<ProjectionStore.ProjectionSettlementCandidate,- "snoozedUntil" | "snoozedAt" | "latestRunCompletedAt" | "status" | "pendingRuntimeRequest"+ | "snoozedUntil"+ | "snoozedAt"+ | "latestRunRequestedAt"+ | "latestRunCompletedAt"+ | "status"+ | "pendingRuntimeRequest">,nowMs: number,): boolean {@@ -185,6 +190,7 @@ export function isSnoozed(if (snoozedUntilMs === null || snoozedUntilMs <= nowMs) return false;if (thread.pendingRuntimeRequest !== null) return false;const snoozedAtMs = toMillis(thread.snoozedAt);+ const requestedAtMs = toMillis(thread.latestRunRequestedAt);const completedAtMs = toMillis(thread.latestRunCompletedAt);const wokeOnError =thread.status === "failed" &&@@ -192,6 +198,8 @@ export function isSnoozed(const wokeOnCompletion =thread.status === "completed" &&snoozedAtMs !== null &&+ requestedAtMs !== null &&+ requestedAtMs > snoozedAtMs &&completedAtMs !== null &&completedAtMs > snoozedAtMs;return !wokeOnError && !wokeOnCompletion;diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.mdindex 0672baf891d..6fc4e5005ce 100644--- a/docs/orchestration-v2/orchestrator-mcp-server.md+++ b/docs/orchestration-v2/orchestrator-mcp-server.md@@ -391,7 +391,8 @@ relabels local matches; cross-environment links can retain the written label.List and read results report `snoozed` and `snoozedUntil`, and`t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows theclient's `effectiveSnoozed`, so agents and the sidebar agree: a snoozed thread-wakes early when it has a pending request, fails, or completes after the snooze.+wakes early when it has a pending request, fails after snoozing, or completes a+run requested after snoozing.### `t3_thread_update`diff --git a/packages/client-runtime/src/state/threadSettled.ts b/packages/client-runtime/src/state/threadSettled.tsindex a48f22b28e3..d298e43235e 100644--- a/packages/client-runtime/src/state/threadSettled.ts+++ b/packages/client-runtime/src/state/threadSettled.ts@@ -83,7 +83,6 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): booleanif (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;if (shell.source?.pendingRuntimeRequest != null) return true;if (shell.pendingSubagentRequest != null) return true;- const latestRun = shell.latestRun ?? shell.latestTurn ?? null;const failedAt = failureEdgeAt(shell);if (failedAt !== undefined &&@@ -92,15 +91,23 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean) {return true;}+ return completionWakeAt(shell) !== null;+}++/** A snooze acknowledges the run already requested, including its eventual result. */+function completionWakeAt(shell: ThreadSnoozeShell): string | null {+ const latestRun = shell.latestRun ?? shell.latestTurn ?? null;if (shell.snoozedAt != null &&(latestRun?.state === "completed" || latestRun?.status === "completed") &&+ latestRun.requestedAt != null &&+ Date.parse(latestRun.requestedAt) > Date.parse(shell.snoozedAt) &&latestRun.completedAt != null &&Date.parse(latestRun.completedAt) > Date.parse(shell.snoozedAt)) {- return true;+ return latestRun.completedAt;}- return false;+ return null;}// V2 runtime.updatedAt includes metadata writes; prefer the failed run's completion.@@ -161,15 +168,8 @@ export function threadWokeAt(// indicator the user already cleared by visiting (snoozedUntil is newer// than that visit's lastVisitedAt).if (threadRaisedHandWhileSnoozed(shell)) {- const latestRun = shell.latestRun ?? shell.latestTurn ?? null;- if (- shell.snoozedAt != null &&- (latestRun?.state === "completed" || latestRun?.status === "completed") &&- latestRun.completedAt != null &&- Date.parse(latestRun.completedAt) > Date.parse(shell.snoozedAt)- ) {- return latestRun.completedAt;- }+ const completedAt = completionWakeAt(shell);+ if (completedAt !== null) return completedAt;// An old failure must not replace the activity time of an approval or input wake.const failedAt = failureEdgeAt(shell);if (diff --git a/packages/client-runtime/src/state/threadSnoozed.test.ts b/packages/client-runtime/src/state/threadSnoozed.test.tsindex d3b11c82a3f..90711f9d051 100644--- a/packages/client-runtime/src/state/threadSnoozed.test.ts+++ b/packages/client-runtime/src/state/threadSnoozed.test.ts@@ -28,6 +28,7 @@ function makeShell(input: {readonly snoozedAt?: string | null;readonly sessionStatus?: "starting" | "running" | "ready" | "error";readonly pending?: "approval" | "user-input";+ readonly turnRequestedAt?: string | null;readonly turnCompletedAt?: string | null;}): ThreadSnoozeShell {const threadId = ThreadId.make("thread-1");@@ -54,7 +55,7 @@ function makeShell(input: {: {turnId: TurnId.make("turn-1"),state: "completed",- requestedAt: SNOOZED_AT,+ requestedAt: input.turnRequestedAt === undefined ? SNOOZED_AT : input.turnRequestedAt,startedAt: null,completedAt: input.turnCompletedAt,assistantMessageId: null,@@ -167,14 +168,29 @@ describe("effectiveSnoozed", () => {).toBe(true);});- it("wakes early when a run completes after the snooze was set", () => {- expect(- effectiveSnoozed(- makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),- { now: NOW },- ),- ).toBe(false);- });+ it.each([+ { boundary: "before snooze", requestedAt: "2026-04-10T08:59:59.999Z", snoozed: true },+ { boundary: "at snooze", requestedAt: SNOOZED_AT, snoozed: true },+ { boundary: "unknown", requestedAt: null, snoozed: true },+ { boundary: "after snooze", requestedAt: "2026-04-10T09:00:00.001Z", snoozed: false },+ ])(+ "classifies completion and its wake time with request time $boundary",+ ({ requestedAt, snoozed }) => {+ const completedAt = "2026-04-10T10:30:00.000Z";+ const shell = {+ ...makeShell({ snoozedUntil: FUTURE_WAKE }),+ latestRun: {+ status: "completed",+ requestedAt,+ startedAt: "2026-04-10T09:15:00.000Z",+ completedAt,+ },+ };++ expect(effectiveSnoozed(shell, { now: NOW })).toBe(snoozed);+ expect(threadWokeAt(shell, { now: NOW })).toBe(snoozed ? null : completedAt);+ },+ );it("ignores runs that completed before the snooze — the user saw that result", () => {expect(@@ -306,31 +322,38 @@ describe("threadWokeAt", () => {expect(threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE }), { now: NOW })).toBe(null);});- it("reports the wake time for a timer wake", () => {+ it("reports the timer wake even after an already-requested run completes", () => {expect(threadWokeAt(makeShell({ snoozedUntil: PAST_WAKE }), { now: NOW })).toBe(PAST_WAKE);+ const shell = makeShell({+ snoozedUntil: PAST_WAKE,+ turnCompletedAt: "2026-04-10T09:30:00.000Z",+ });+ expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);+ expect(threadWokeAt(shell, { now: NOW })).toBe(PAST_WAKE);});- it("reports the completion time for an early run-completed wake", () => {- expect(- threadWokeAt(- makeShell({ snoozedUntil: FUTURE_WAKE, turnCompletedAt: "2026-04-10T10:30:00.000Z" }),- { now: NOW },- ),- ).toBe("2026-04-10T10:30:00.000Z");- });-- it("falls back to session activity for blocked/failed early wakes", () => {- expect(- threadWokeAt(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "error" }), {- now: NOW,- }),- ).toBe("2026-04-10T11:00:00.000Z");+ it.each([+ { reason: "failure", sessionStatus: "error" },+ { reason: "approval", sessionStatus: "ready", pending: "approval" },+ { reason: "user input", sessionStatus: "ready", pending: "user-input" },+ ] as const)("reports $reason activity instead of an already-requested completion", (activity) => {+ const shell = makeShell({+ ...activity,+ snoozedUntil: FUTURE_WAKE,+ turnCompletedAt: "2026-04-10T10:30:00.000Z",+ });+ expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);+ expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T11:00:00.000Z");});it("keeps the early wake authoritative after the scheduled time passes", () => {expect(threadWokeAt(- makeShell({ snoozedUntil: PAST_WAKE, turnCompletedAt: "2026-04-10T09:30:00.000Z" }),+ makeShell({+ snoozedUntil: PAST_WAKE,+ turnRequestedAt: "2026-04-10T09:15:00.000Z",+ turnCompletedAt: "2026-04-10T09:30:00.000Z",+ }),{ now: NOW },),).toBe("2026-04-10T09:30:00.000Z");