TypeScript
fix(threads): snoozed threads stay hidden through background wakes (#22202)
Theo Browned2fb83a973b5
diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.tsindex 893dd7da0fe..db291aac7b7 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 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.",+ "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 only when it asks for something or fails after snoozing; completed runs and background wakes keep it snoozed. 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/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 94ced3e9922..462bf6c9e1b 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -4922,7 +4922,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio});projection = yield* getProjectionWithPendingEvents(command.threadId, events);}- if (projection.thread.snoozedUntil != null) {+ // Background wakes and delegated completions continue work the user already snoozed, so+ // only a message someone actually sent ends the snooze.+ const automaticWake =+ command.notification !== undefined || command.delegatedCompletion !== undefined;+ if (projection.thread.snoozedUntil != null && !automaticWake) {const now = yield* DateTime.now;const thread: OrchestrationV2AppThread = {...projection.thread,diff --git a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts b/apps/server/src/orchestration-v2/ProjectionSettlement.test.tsindex 0c31bcafc9b..46579313cda 100644--- a/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts+++ b/apps/server/src/orchestration-v2/ProjectionSettlement.test.ts@@ -216,7 +216,7 @@ it.effect.each([snoozedUntil: DateTime.add(now, { days: 1 }),snoozedAt: DateTime.subtract(old, { days: 1 }),});- yield* createRun(woke);+ yield* createRun(woke, "failed");const background = yield* createThread("idle-background");yield* createItem(background, yield* createRun(background), "idle");const persistent = yield* createThread("persistent-monitor");diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.test.tsindex dff91b8c85d..28a62cdfe45 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.test.ts@@ -1405,35 +1405,27 @@ describe("ThreadSettlementServiceV2 single-thread sweeps", () => {describe("isSnoozed", () => {const snoozed = { snoozedAt: at(-2 * DAY_MS), snoozedUntil: at(DAY_MS) };- 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 a run requested and completed after snoozing parked until the timer", () => {+ const thread = shell({+ ...snoozed,+ status: "completed",+ latestRunRequestedAt: at(-2 * DAY_MS + 1),+ latestRunStartedAt: at(-DAY_MS - 1),+ latestRunCompletedAt: at(-DAY_MS),+ });++ expect(ThreadSettlementService.isSnoozed(thread, NOW_MS)).toBe(true);+ expect(ThreadSettlementService.isAutoSettlementCandidate(thread, NOW_MS)).toBe(false);+ expect(+ ThreadSettlementService.resolveAutoSettlementAt({+ thread,+ pullRequest: { state: "merged", mergedAt: DateTime.formatIso(at(-1)) },+ nowMs: NOW_MS,+ autoSettleAfterDays: null,+ autoSettleOnMerge: true,+ }),+ ).toBeNull();+ });it("keeps quiet and interrupted threads snoozed until the timer expires", () => {expect(ThreadSettlementService.isSnoozed(shell(snoozed), NOW_MS)).toBe(true);diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.tsindex fe0e57e71d1..6d46ce113e2 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts@@ -177,12 +177,7 @@ export function isAutoSettlementCandidate(export function isSnoozed(thread: Pick<ProjectionStore.ProjectionSettlementCandidate,- | "snoozedUntil"- | "snoozedAt"- | "latestRunRequestedAt"- | "latestRunCompletedAt"- | "status"- | "pendingRuntimeRequest"+ "snoozedUntil" | "snoozedAt" | "latestRunCompletedAt" | "status" | "pendingRuntimeRequest">,nowMs: number,): boolean {@@ -190,19 +185,11 @@ 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" &&(snoozedAtMs === null || (completedAtMs !== null && completedAtMs > snoozedAtMs));- const wokeOnCompletion =- thread.status === "completed" &&- snoozedAtMs !== null &&- requestedAtMs !== null &&- requestedAtMs > snoozedAtMs &&- completedAtMs !== null &&- completedAtMs > snoozedAtMs;- return !wokeOnError && !wokeOnCompletion;+ return !wokeOnError;}export function resolveAutoSettlementAt(input: {diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.tsindex e6ddf57b7e6..c295011736c 100644--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts@@ -5812,6 +5812,63 @@ it.layer(layerTest)("snooze queued runs", (it) => {}}),);++ it.effect.each(["notification", "user"] as const)(+ "a %s message on a snoozed thread keeps the snooze only when automatic",+ (kind) =>+ Effect.gen(function* () {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const threadId = ThreadId.make(`snooze-wake:${kind}`);+ const snoozedUntil = "2099-07-25T09:00:00.000Z";+ yield* orchestrator.dispatch({+ type: "thread.create",+ commandId: CommandId.make(`create:${threadId}`),+ threadId,+ projectId: ProjectId.make("snooze-wake-project"),+ title: "Snoozed long-running thread",+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: "/tmp/snooze-wake",+ createdBy: "user",+ creationSource: "web",+ });+ yield* orchestrator.dispatch({+ type: "thread.snooze",+ commandId: CommandId.make(`snooze:${threadId}`),+ threadId,+ snoozedUntil,+ });+ yield* orchestrator.dispatch({+ type: "message.dispatch",+ commandId: CommandId.make(`wake:${threadId}`),+ threadId,+ messageId: MessageId.make(`wake:${threadId}`),+ text: "Continue",+ attachments: [],+ modelSelection,+ dispatchMode: { type: "queue_after_active" },+ createdBy: kind === "user" ? "user" : "agent",+ creationSource: kind === "user" ? "web" : "provider",+ ...(kind === "notification"+ ? {+ notification: {+ source: { kind: "background_task" as const },+ outcome: "updated" as const,+ summary: "Background activity updated",+ },+ }+ : {}),+ });+ const thread = (yield* orchestrator.getThreadProjection(threadId)).thread;+ if (kind === "user") {+ assert.isNull(thread.snoozedUntil);+ } else {+ assert.equal(DateTime.formatIso(thread.snoozedUntil!), snoozedUntil);+ }+ }),+ );});it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.mdindex 6fc4e5005ce..44aa2492a82 100644--- a/docs/orchestration-v2/orchestrator-mcp-server.md+++ b/docs/orchestration-v2/orchestrator-mcp-server.md@@ -391,8 +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 after snoozing, or completes a-run requested after snoozing.+wakes early only when it has a pending request or fails after snoozing. Completed+runs, background wakes, and delegated completions keep it snoozed.### `t3_thread_update`diff --git a/docs/user/thread-sidebar.md b/docs/user/thread-sidebar.mdindex d08db265792..33dfbb66ca2 100644--- a/docs/user/thread-sidebar.md+++ b/docs/user/thread-sidebar.md@@ -233,3 +233,6 @@ Choose **Snooze → Custom…** from a thread's menu to pick a date and time inlocal time zone, or a duration in minutes, hours, or days. Durations start whenyou confirm; one day means 24 hours. On web and desktop, you can also snoozeseveral selected threads together. Choose **Wake thread** to bring a thread back early.+A snoozed thread keeps working and stays hidden until its wake time, even while+its agent wakes itself for background work. It comes back early only when it needs+an approval or answer from you, when a run fails, or when you send it a message.diff --git a/packages/client-runtime/src/state/threadSettled.ts b/packages/client-runtime/src/state/threadSettled.tsindex d298e43235e..86f4914f6c3 100644--- a/packages/client-runtime/src/state/threadSettled.ts+++ b/packages/client-runtime/src/state/threadSettled.ts@@ -79,35 +79,21 @@ export interface ThreadSnoozeShell extends QueuedThreadShell {readonly pendingSubagentRequest?: PendingSubagentRequest | null;}+/**+ * Only a request for the user or a fresh failure ends a snooze early. Completed runs never do:+ * a snoozed long-running thread keeps waking itself for background work and stays hidden+ * until its timer.+ */export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean {if (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;if (shell.source?.pendingRuntimeRequest != null) return true;if (shell.pendingSubagentRequest != null) return true;const failedAt = failureEdgeAt(shell);- if (+ return (failedAt !== undefined &&(shell.snoozedAt == null ||(failedAt != null && Date.parse(failedAt) > Date.parse(shell.snoozedAt)))- ) {- 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 latestRun.completedAt;- }- return null;+ );}// V2 runtime.updatedAt includes metadata writes; prefer the failed run's completion.@@ -168,8 +154,6 @@ export function threadWokeAt(// indicator the user already cleared by visiting (snoozedUntil is newer// than that visit's lastVisitedAt).if (threadRaisedHandWhileSnoozed(shell)) {- 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 90711f9d051..5b7960ea980 100644--- a/packages/client-runtime/src/state/threadSnoozed.test.ts+++ b/packages/client-runtime/src/state/threadSnoozed.test.ts@@ -168,29 +168,20 @@ describe("effectiveSnoozed", () => {).toBe(true);});- 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,- },- };+ it("stays snoozed when a run requested after the snooze completes", () => {+ const shell = {+ ...makeShell({ snoozedUntil: FUTURE_WAKE }),+ latestRun: {+ status: "completed",+ requestedAt: "2026-04-10T09:00:00.001Z",+ startedAt: "2026-04-10T09:15:00.000Z",+ completedAt: "2026-04-10T10:30:00.000Z",+ },+ };- expect(effectiveSnoozed(shell, { now: NOW })).toBe(snoozed);- expect(threadWokeAt(shell, { now: NOW })).toBe(snoozed ? null : completedAt);- },- );+ expect(effectiveSnoozed(shell, { now: NOW })).toBe(true);+ expect(threadWokeAt(shell, { now: NOW })).toBe(null);+ });it("ignores runs that completed before the snooze — the user saw that result", () => {expect(@@ -347,16 +338,16 @@ describe("threadWokeAt", () => {});it("keeps the early wake authoritative after the scheduled time passes", () => {- expect(- threadWokeAt(- 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");+ const shell: ThreadSnoozeShell = {+ ...makeShell({ snoozedUntil: PAST_WAKE }),+ runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },+ latestRun: {+ status: "failed",+ requestedAt: "2026-04-10T09:15:00.000Z",+ completedAt: "2026-04-10T09:30:00.000Z",+ },+ };+ expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T09:30:00.000Z");});});