@devries-cameron20 / t3code-1 / commits / 69ba80dfae
Fork 0

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

fix(threads): snoozed threads no longer wake early after background updates (#16517)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Rakshith Bhat committed Oct 20, 2026 at 00:23 UTC 69ba80dfae93707d0d09091bea3266a5a0c8eec0
4 files changed +195 -8
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+118
@@ -664,6 +664,124 @@ it.effect("memory projection keeps restart-cancelled work through a stale run.up
664 restartCancelledWorkSurvivesStaleRunUpdate.pipe(Effect.provide(ProjectionStore.layerMemory)),
665 );
666
667 +const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {
668 + const store = yield* ProjectionStore.ProjectionStoreV2;
669 + const createdAt = DateTime.makeUnsafe("2026-10-06T14:00:00.000Z");
670 + const threadId = ThreadId.make("thread:background-updates");
671 + const thread = {
672 + createdBy: "user" as const,
673 + creationSource: "web" as const,
674 + id: threadId,
675 + projectId: ProjectId.make("project:background-updates"),
676 + title: "Background updates",
677 + providerInstanceId,
678 + modelSelection,
679 + runtimeMode: "full-access" as const,
680 + interactionMode: "default" as const,
681 + branch: null,
682 + worktreePath: null,
683 + activeProviderThreadId: null,
684 + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
685 + forkedFrom: null,
686 + createdAt,
687 + updatedAt: createdAt,
688 + archivedAt: null,
689 + settledOverride: null,
690 + settledAt: null,
691 + lastVisitedAt: null,
692 + deletedAt: null,
693 + };
694 + yield* store.apply({
695 + id: EventId.make("event:background-updates:created"),
696 + type: "thread.created",
697 + threadId,
698 + occurredAt: createdAt,
699 + payload: thread,
700 + });
701 + const readActivity = Effect.gen(function* () {
702 + const projection = yield* store.getThreadProjection(threadId);
703 + const thread = yield* store.getThread(threadId);
704 + const shell = yield* store.getThreadShell(threadId);
705 + const listed = (yield* store.getShellSnapshot()).threads.find((entry) => entry.id === threadId);
706 + assert.deepEqual(projection.thread.updatedAt, createdAt);
707 + assert.deepEqual(thread.updatedAt, createdAt);
708 + assert.deepEqual(shell?.updatedAt, createdAt);
709 + assert.deepEqual(listed?.updatedAt, createdAt);
710 + return projection;
711 + });
712 + const link = {
713 + host: "github.com",
714 + repository: "owner/repository",
715 + number: 42,
716 + url: "https://github.com/owner/repository/pull/42",
717 + source: "manual" as const,
718 + linkedAt: DateTime.formatIso(createdAt),
719 + snapshot: null,
720 + stack: null,
721 + };
722 + yield* store.apply({
723 + id: EventId.make("event:background-updates:pull-request-synced"),
724 + type: "thread.pull-request-synced",
725 + threadId,
726 + occurredAt: DateTime.add(createdAt, { minutes: 45 }),
727 + payload: { ...thread, pullRequests: [link] },
728 + });
729 + const synced = yield* readActivity;
730 + assert.deepEqual(synced.updatedAt, createdAt);
731 + assert.equal(synced.thread.pullRequests?.[0]?.number, 42);
732 +
733 + const snapshot = {
734 + state: "open" as const,
735 + title: "Ship it",
736 + headBranch: "feature",
737 + baseBranch: "main",
738 + isDraft: false,
739 + updatedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 46 })),
740 + syncedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 47 })),
741 + closedAt: null,
742 + mergedAt: null,
743 + };
744 + yield* store.apply({
745 + id: EventId.make("event:background-updates:pull-request-link-updated"),
746 + type: "thread.pull-request-link-updated",
747 + threadId,
748 + occurredAt: DateTime.add(createdAt, { minutes: 48 }),
749 + payload: { ...link, snapshot },
750 + });
751 + const refreshed = yield* readActivity;
752 + assert.deepEqual(refreshed.updatedAt, createdAt);
753 + assert.deepEqual(refreshed.thread.pullRequests?.[0]?.snapshot, snapshot);
754 +
755 + yield* store.apply({
756 + id: EventId.make("event:background-updates:active-reordered"),
757 + type: "thread.active-reordered",
758 + threadId,
759 + occurredAt: DateTime.add(createdAt, { minutes: 50 }),
760 + payload: { ...refreshed.thread, activeOrderKey: "a" },
761 + });
762 + const reordered = yield* readActivity;
763 + assert.deepEqual(reordered.updatedAt, createdAt);
764 + assert.equal(reordered.thread.activeOrderKey, "a");
765 +
766 + // An already-unpinned thread keeps its activity time even when replay's envelope advances.
767 + yield* store.apply({
768 + id: EventId.make("event:background-updates:already-unpinned"),
769 + type: "thread.unpinned",
770 + threadId,
771 + occurredAt: DateTime.add(createdAt, { minutes: 55 }),
772 + payload: { ...reordered.thread, pinnedAt: null, pinOrderKey: null },
773 + });
774 + yield* readActivity;
775 +});
776 +
777 +it.effect("memory projection keeps background thread updates out of updatedAt", () =>
778 + backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(ProjectionStore.layerMemory)),
779 +);
780 +
781 +it.effect("SQL projection keeps background thread updates out of updatedAt", () =>
782 + backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(layerTest)),
783 +);
784 +
785 it.effect("memory recovery selection ignores unfinished items from rolled-back runs", () =>
786 Effect.gen(function* () {
787 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+13 -4
@@ -734,7 +734,7 @@ export function applyToProjection(
734 switch (event.type) {
735 case "thread.pull-request-link-updated":
736 return {
737 - ...base,
737 + ...projection,
738 thread: applyThreadPullRequestLinkUpdate(projection.thread, event.payload),
739 };
740 case "thread.created":
@@ -749,9 +749,7 @@ export function applyToProjection(
749 case "thread.pinned":
750 case "thread.unpinned":
751 case "thread.pin-reordered":
752 - case "thread.active-reordered":
752 case "thread.metadata-updated":
754 - case "thread.pull-request-synced":
753 case "thread.runtime-mode-updated":
754 case "thread.interaction-mode-updated":
755 case "thread.model-selection-updated":
@@ -760,6 +758,14 @@ export function applyToProjection(
758 ...base,
759 thread: event.payload,
760 };
761 + // Reordering and PR refreshes preserve the orchestrator's activity timestamp.
762 + case "thread.active-reordered":
763 + case "thread.pull-request-synced":
764 + return {
765 + ...base,
766 + thread: event.payload,
767 + updatedAt: event.payload.updatedAt,
768 + };
769 case "thread.visited":
770 case "thread.marked-unread":
771 return {
@@ -1568,7 +1574,7 @@ export function threadShellFromProjection(
1574 itemCount: activeLocalTurnItems(projection).length,
1575 visibleItemCount: projection.visibleTurnItems.length,
1576 createdAt: projection.thread.createdAt,
1571 - updatedAt: projection.updatedAt,
1577 + updatedAt: projection.thread.updatedAt,
1578 archivedAt: projection.thread.archivedAt,
1579 settledOverride: projection.thread.settledOverride,
1580 settledAt: projection.thread.settledAt,
@@ -2705,9 +2711,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2711 event.type !== "thread.pinned" &&
2712 event.type !== "thread.unpinned" &&
2713 event.type !== "thread.pin-reordered" &&
2714 + event.type !== "thread.active-reordered" &&
2715 event.type !== "thread.visited" &&
2716 event.type !== "thread.marked-unread" &&
2717 event.type !== "thread.metadata-updated" &&
2718 + event.type !== "thread.pull-request-synced" &&
2719 + event.type !== "thread.pull-request-link-updated" &&
2720 event.type !== "thread.runtime-mode-updated" &&
2721 event.type !== "thread.interaction-mode-updated" &&
2722 event.type !== "thread.model-selection-updated" &&
packages/client-runtime/src/state/threadSettled.ts
+24 -4
@@ -81,12 +81,12 @@ export interface ThreadSnoozeShell extends QueuedThreadShell {
81 export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean {
82 if (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;
83 if (shell.pendingSubagentRequest != null) return true;
84 - const runtime = shell.runtime ?? shell.session ?? null;
84 const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
85 + const failedAt = failureEdgeAt(shell);
86 if (
87 - (runtime?.status === "error" || runtime?.status === "failed") &&
87 + failedAt !== undefined &&
88 (shell.snoozedAt == null ||
89 - (runtime.updatedAt != null && Date.parse(runtime.updatedAt) > Date.parse(shell.snoozedAt)))
89 + (failedAt != null && Date.parse(failedAt) > Date.parse(shell.snoozedAt)))
90 ) {
91 return true;
92 }
@@ -101,6 +101,18 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean
101 return false;
102 }
103
104 +// V2 runtime.updatedAt includes metadata writes; prefer the failed run's completion.
105 +// Legacy sessions only expose the runtime timestamp.
106 +function failureEdgeAt(shell: ThreadSnoozeShell): string | null | undefined {
107 + const runtime = shell.runtime ?? shell.session ?? null;
108 + if (runtime?.status !== "error" && runtime?.status !== "failed") return undefined;
109 + const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
110 + if (latestRun?.status === "failed" && latestRun.completedAt != null) {
111 + return latestRun.completedAt;
112 + }
113 + return runtime.updatedAt ?? null;
114 +}
115 +
116 export function canSnooze(
117 shell: Pick<
118 ThreadSnoozeShell,
@@ -145,7 +157,6 @@ export function threadWokeAt(
157 // than that visit's lastVisitedAt).
158 if (threadRaisedHandWhileSnoozed(shell)) {
159 const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
148 - const runtime = shell.runtime ?? shell.session ?? null;
160 if (
161 shell.snoozedAt != null &&
162 (latestRun?.state === "completed" || latestRun?.status === "completed") &&
@@ -154,6 +165,15 @@ export function threadWokeAt(
165 ) {
166 return latestRun.completedAt;
167 }
168 + // An old failure must not replace the activity time of an approval or input wake.
169 + const failedAt = failureEdgeAt(shell);
170 + if (
171 + failedAt != null &&
172 + (shell.snoozedAt == null || Date.parse(failedAt) > Date.parse(shell.snoozedAt))
173 + ) {
174 + return failedAt;
175 + }
176 + const runtime = shell.runtime ?? shell.session ?? null;
177 return runtime?.updatedAt ?? shell.snoozedAt ?? null;
178 }
179 return wakeAtMs <= Date.parse(options.now) ? shell.snoozedUntil : null;
packages/client-runtime/src/state/threadSnoozed.test.ts
+40
@@ -119,6 +119,46 @@ describe("effectiveSnoozed", () => {
119 ).toBe(true);
120 });
121
122 + it.each([
123 + {
124 + reason: "stale failure",
125 + completedAt: "2026-04-10T08:00:00.000Z",
126 + pending: undefined,
127 + wokeAt: null,
128 + },
129 + {
130 + reason: "fresh failure",
131 + completedAt: "2026-04-10T10:00:00.000Z",
132 + pending: undefined,
133 + wokeAt: "2026-04-10T10:00:00.000Z",
134 + },
135 + {
136 + reason: "approval after a stale failure",
137 + completedAt: "2026-04-10T08:00:00.000Z",
138 + pending: "approval",
139 + wokeAt: "2026-04-10T11:00:00.000Z",
140 + },
141 + {
142 + reason: "input after a stale failure",
143 + completedAt: "2026-04-10T08:00:00.000Z",
144 + pending: "user-input",
145 + wokeAt: "2026-04-10T11:00:00.000Z",
146 + },
147 + ] as const)(
148 + "uses the wake event time for $reason despite newer metadata",
149 + ({ completedAt, pending, wokeAt }) => {
150 + const shell: ThreadSnoozeShell = {
151 + ...makeShell({ snoozedUntil: FUTURE_WAKE }),
152 + hasPendingApprovals: pending === "approval",
153 + hasPendingUserInput: pending === "user-input",
154 + runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
155 + latestRun: { status: "failed", requestedAt: "2026-04-10T07:00:00.000Z", completedAt },
156 + };
157 + expect(effectiveSnoozed(shell, { now: NOW })).toBe(wokeAt === null);
158 + expect(threadWokeAt(shell, { now: NOW })).toBe(wokeAt);
159 + },
160 + );
161 +
162 it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
163 expect(
164 effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {