Skip to content

Commit d2fb83a

Browse files
t3dotggclaude
andauthored
fix(threads): snoozed threads stay hidden through background wakes (#22202)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 91d7bf3 commit d2fb83a

10 files changed

Lines changed: 122 additions & 104 deletions

File tree

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export const CreateThreadsTool = Tool.make("create_threads", {
192192

193193
const ThreadListTool = Tool.make("t3_thread_list", {
194194
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 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.",
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 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.",
196196
parameters: OrchestratorMcpThreadListInput,
197197
success: OrchestratorMcpThreadListResult,
198198
failure: OrchestratorMcpFailure,

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4922,7 +4922,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
49224922
});
49234923
projection = yield* getProjectionWithPendingEvents(command.threadId, events);
49244924
}
4925-
if (projection.thread.snoozedUntil != null) {
4925+
// Background wakes and delegated completions continue work the user already snoozed, so
4926+
// only a message someone actually sent ends the snooze.
4927+
const automaticWake =
4928+
command.notification !== undefined || command.delegatedCompletion !== undefined;
4929+
if (projection.thread.snoozedUntil != null && !automaticWake) {
49264930
const now = yield* DateTime.now;
49274931
const thread: OrchestrationV2AppThread = {
49284932
...projection.thread,

‎apps/server/src/orchestration-v2/ProjectionSettlement.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ it.effect.each([
216216
snoozedUntil: DateTime.add(now, { days: 1 }),
217217
snoozedAt: DateTime.subtract(old, { days: 1 }),
218218
});
219-
yield* createRun(woke);
219+
yield* createRun(woke, "failed");
220220
const background = yield* createThread("idle-background");
221221
yield* createItem(background, yield* createRun(background), "idle");
222222
const persistent = yield* createThread("persistent-monitor");

‎apps/server/src/orchestration-v2/ThreadSettlementService.test.ts‎

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,35 +1405,27 @@ describe("ThreadSettlementServiceV2 single-thread sweeps", () => {
14051405

14061406
describe("isSnoozed", () => {
14071407
const snoozed = { snoozedAt: at(-2 * DAY_MS), snoozedUntil: at(DAY_MS) };
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-
);
1408+
it("keeps a run requested and completed after snoozing parked until the timer", () => {
1409+
const thread = shell({
1410+
...snoozed,
1411+
status: "completed",
1412+
latestRunRequestedAt: at(-2 * DAY_MS + 1),
1413+
latestRunStartedAt: at(-DAY_MS - 1),
1414+
latestRunCompletedAt: at(-DAY_MS),
1415+
});
1416+
1417+
expect(ThreadSettlementService.isSnoozed(thread, NOW_MS)).toBe(true);
1418+
expect(ThreadSettlementService.isAutoSettlementCandidate(thread, NOW_MS)).toBe(false);
1419+
expect(
1420+
ThreadSettlementService.resolveAutoSettlementAt({
1421+
thread,
1422+
pullRequest: { state: "merged", mergedAt: DateTime.formatIso(at(-1)) },
1423+
nowMs: NOW_MS,
1424+
autoSettleAfterDays: null,
1425+
autoSettleOnMerge: true,
1426+
}),
1427+
).toBeNull();
1428+
});
14371429

14381430
it("keeps quiet and interrupted threads snoozed until the timer expires", () => {
14391431
expect(ThreadSettlementService.isSnoozed(shell(snoozed), NOW_MS)).toBe(true);

‎apps/server/src/orchestration-v2/ThreadSettlementService.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,19 @@ export function isAutoSettlementCandidate(
177177
export function isSnoozed(
178178
thread: Pick<
179179
ProjectionStore.ProjectionSettlementCandidate,
180-
| "snoozedUntil"
181-
| "snoozedAt"
182-
| "latestRunRequestedAt"
183-
| "latestRunCompletedAt"
184-
| "status"
185-
| "pendingRuntimeRequest"
180+
"snoozedUntil" | "snoozedAt" | "latestRunCompletedAt" | "status" | "pendingRuntimeRequest"
186181
>,
187182
nowMs: number,
188183
): boolean {
189184
const snoozedUntilMs = toMillis(thread.snoozedUntil);
190185
if (snoozedUntilMs === null || snoozedUntilMs <= nowMs) return false;
191186
if (thread.pendingRuntimeRequest !== null) return false;
192187
const snoozedAtMs = toMillis(thread.snoozedAt);
193-
const requestedAtMs = toMillis(thread.latestRunRequestedAt);
194188
const completedAtMs = toMillis(thread.latestRunCompletedAt);
195189
const wokeOnError =
196190
thread.status === "failed" &&
197191
(snoozedAtMs === null || (completedAtMs !== null && completedAtMs > snoozedAtMs));
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;
192+
return !wokeOnError;
206193
}
207194

208195
export function resolveAutoSettlementAt(input: {

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,6 +5812,63 @@ it.layer(layerTest)("snooze queued runs", (it) => {
58125812
}
58135813
}),
58145814
);
5815+
5816+
it.effect.each(["notification", "user"] as const)(
5817+
"a %s message on a snoozed thread keeps the snooze only when automatic",
5818+
(kind) =>
5819+
Effect.gen(function* () {
5820+
const orchestrator = yield* Orchestrator.OrchestratorV2;
5821+
const threadId = ThreadId.make(`snooze-wake:${kind}`);
5822+
const snoozedUntil = "2099-07-25T09:00:00.000Z";
5823+
yield* orchestrator.dispatch({
5824+
type: "thread.create",
5825+
commandId: CommandId.make(`create:${threadId}`),
5826+
threadId,
5827+
projectId: ProjectId.make("snooze-wake-project"),
5828+
title: "Snoozed long-running thread",
5829+
modelSelection,
5830+
runtimeMode: "full-access",
5831+
interactionMode: "default",
5832+
branch: null,
5833+
worktreePath: "/tmp/snooze-wake",
5834+
createdBy: "user",
5835+
creationSource: "web",
5836+
});
5837+
yield* orchestrator.dispatch({
5838+
type: "thread.snooze",
5839+
commandId: CommandId.make(`snooze:${threadId}`),
5840+
threadId,
5841+
snoozedUntil,
5842+
});
5843+
yield* orchestrator.dispatch({
5844+
type: "message.dispatch",
5845+
commandId: CommandId.make(`wake:${threadId}`),
5846+
threadId,
5847+
messageId: MessageId.make(`wake:${threadId}`),
5848+
text: "Continue",
5849+
attachments: [],
5850+
modelSelection,
5851+
dispatchMode: { type: "queue_after_active" },
5852+
createdBy: kind === "user" ? "user" : "agent",
5853+
creationSource: kind === "user" ? "web" : "provider",
5854+
...(kind === "notification"
5855+
? {
5856+
notification: {
5857+
source: { kind: "background_task" as const },
5858+
outcome: "updated" as const,
5859+
summary: "Background activity updated",
5860+
},
5861+
}
5862+
: {}),
5863+
});
5864+
const thread = (yield* orchestrator.getThreadProjection(threadId)).thread;
5865+
if (kind === "user") {
5866+
assert.isNull(thread.snoozedUntil);
5867+
} else {
5868+
assert.equal(DateTime.formatIso(thread.snoozedUntil!), snoozedUntil);
5869+
}
5870+
}),
5871+
);
58155872
});
58165873

58175874
it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {

‎docs/orchestration-v2/orchestrator-mcp-server.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ relabels local matches; cross-environment links can retain the written label.
391391
List and read results report `snoozed` and `snoozedUntil`, and
392392
`t3_thread_list` filters on `snoozed`. The server's `isSnoozed` follows the
393393
client's `effectiveSnoozed`, so agents and the sidebar agree: a snoozed thread
394-
wakes early when it has a pending request, fails after snoozing, or completes a
395-
run requested after snoozing.
394+
wakes early only when it has a pending request or fails after snoozing. Completed
395+
runs, background wakes, and delegated completions keep it snoozed.
396396

397397
### `t3_thread_update`
398398

‎docs/user/thread-sidebar.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,6 @@ Choose **Snooze → Custom…** from a thread's menu to pick a date and time in
233233
local time zone, or a duration in minutes, hours, or days. Durations start when
234234
you confirm; one day means 24 hours. On web and desktop, you can also snooze
235235
several selected threads together. Choose **Wake thread** to bring a thread back early.
236+
A snoozed thread keeps working and stays hidden until its wake time, even while
237+
its agent wakes itself for background work. It comes back early only when it needs
238+
an approval or answer from you, when a run fails, or when you send it a message.

‎packages/client-runtime/src/state/threadSettled.ts‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,21 @@ export interface ThreadSnoozeShell extends QueuedThreadShell {
7979
readonly pendingSubagentRequest?: PendingSubagentRequest | null;
8080
}
8181

82+
/**
83+
* Only a request for the user or a fresh failure ends a snooze early. Completed runs never do:
84+
* a snoozed long-running thread keeps waking itself for background work and stays hidden
85+
* until its timer.
86+
*/
8287
export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean {
8388
if (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;
8489
if (shell.source?.pendingRuntimeRequest != null) return true;
8590
if (shell.pendingSubagentRequest != null) return true;
8691
const failedAt = failureEdgeAt(shell);
87-
if (
92+
return (
8893
failedAt !== undefined &&
8994
(shell.snoozedAt == null ||
9095
(failedAt != null && Date.parse(failedAt) > Date.parse(shell.snoozedAt)))
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-
) {
108-
return latestRun.completedAt;
109-
}
110-
return null;
96+
);
11197
}
11298

11399
// V2 runtime.updatedAt includes metadata writes; prefer the failed run's completion.
@@ -168,8 +154,6 @@ export function threadWokeAt(
168154
// indicator the user already cleared by visiting (snoozedUntil is newer
169155
// than that visit's lastVisitedAt).
170156
if (threadRaisedHandWhileSnoozed(shell)) {
171-
const completedAt = completionWakeAt(shell);
172-
if (completedAt !== null) return completedAt;
173157
// An old failure must not replace the activity time of an approval or input wake.
174158
const failedAt = failureEdgeAt(shell);
175159
if (

‎packages/client-runtime/src/state/threadSnoozed.test.ts‎

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -168,29 +168,20 @@ describe("effectiveSnoozed", () => {
168168
).toBe(true);
169169
});
170170

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-
};
171+
it("stays snoozed when a run requested after the snooze completes", () => {
172+
const shell = {
173+
...makeShell({ snoozedUntil: FUTURE_WAKE }),
174+
latestRun: {
175+
status: "completed",
176+
requestedAt: "2026-04-10T09:00:00.001Z",
177+
startedAt: "2026-04-10T09:15:00.000Z",
178+
completedAt: "2026-04-10T10:30:00.000Z",
179+
},
180+
};
189181

190-
expect(effectiveSnoozed(shell, { now: NOW })).toBe(snoozed);
191-
expect(threadWokeAt(shell, { now: NOW })).toBe(snoozed ? null : completedAt);
192-
},
193-
);
182+
expect(effectiveSnoozed(shell, { now: NOW })).toBe(true);
183+
expect(threadWokeAt(shell, { now: NOW })).toBe(null);
184+
});
194185

195186
it("ignores runs that completed before the snooze — the user saw that result", () => {
196187
expect(
@@ -347,16 +338,16 @@ describe("threadWokeAt", () => {
347338
});
348339

349340
it("keeps the early wake authoritative after the scheduled time passes", () => {
350-
expect(
351-
threadWokeAt(
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");
341+
const shell: ThreadSnoozeShell = {
342+
...makeShell({ snoozedUntil: PAST_WAKE }),
343+
runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
344+
latestRun: {
345+
status: "failed",
346+
requestedAt: "2026-04-10T09:15:00.000Z",
347+
completedAt: "2026-04-10T09:30:00.000Z",
348+
},
349+
};
350+
expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T09:30:00.000Z");
360351
});
361352
});
362353

0 commit comments

Comments
 (0)