Skip to content

Commit 69ba80d

Browse files
RakshithBhat03claudejuliusmarminge
authored
fix(threads): snoozed threads no longer wake early after background updates (pingdotgg#16517)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 7ee6e92 commit 69ba80d

4 files changed

Lines changed: 195 additions & 8 deletions

File tree

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,124 @@ it.effect("memory projection keeps restart-cancelled work through a stale run.up
664664
restartCancelledWorkSurvivesStaleRunUpdate.pipe(Effect.provide(ProjectionStore.layerMemory)),
665665
);
666666

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+
667785
it.effect("memory recovery selection ignores unfinished items from rolled-back runs", () =>
668786
Effect.gen(function* () {
669787
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ export function applyToProjection(
734734
switch (event.type) {
735735
case "thread.pull-request-link-updated":
736736
return {
737-
...base,
737+
...projection,
738738
thread: applyThreadPullRequestLinkUpdate(projection.thread, event.payload),
739739
};
740740
case "thread.created":
@@ -749,9 +749,7 @@ export function applyToProjection(
749749
case "thread.pinned":
750750
case "thread.unpinned":
751751
case "thread.pin-reordered":
752-
case "thread.active-reordered":
753752
case "thread.metadata-updated":
754-
case "thread.pull-request-synced":
755753
case "thread.runtime-mode-updated":
756754
case "thread.interaction-mode-updated":
757755
case "thread.model-selection-updated":
@@ -760,6 +758,14 @@ export function applyToProjection(
760758
...base,
761759
thread: event.payload,
762760
};
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+
};
763769
case "thread.visited":
764770
case "thread.marked-unread":
765771
return {
@@ -1568,7 +1574,7 @@ export function threadShellFromProjection(
15681574
itemCount: activeLocalTurnItems(projection).length,
15691575
visibleItemCount: projection.visibleTurnItems.length,
15701576
createdAt: projection.thread.createdAt,
1571-
updatedAt: projection.updatedAt,
1577+
updatedAt: projection.thread.updatedAt,
15721578
archivedAt: projection.thread.archivedAt,
15731579
settledOverride: projection.thread.settledOverride,
15741580
settledAt: projection.thread.settledAt,
@@ -2705,9 +2711,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
27052711
event.type !== "thread.pinned" &&
27062712
event.type !== "thread.unpinned" &&
27072713
event.type !== "thread.pin-reordered" &&
2714+
event.type !== "thread.active-reordered" &&
27082715
event.type !== "thread.visited" &&
27092716
event.type !== "thread.marked-unread" &&
27102717
event.type !== "thread.metadata-updated" &&
2718+
event.type !== "thread.pull-request-synced" &&
2719+
event.type !== "thread.pull-request-link-updated" &&
27112720
event.type !== "thread.runtime-mode-updated" &&
27122721
event.type !== "thread.interaction-mode-updated" &&
27132722
event.type !== "thread.model-selection-updated" &&

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ export interface ThreadSnoozeShell extends QueuedThreadShell {
8181
export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean {
8282
if (shell.hasPendingApprovals || shell.hasPendingUserInput) return true;
8383
if (shell.pendingSubagentRequest != null) return true;
84-
const runtime = shell.runtime ?? shell.session ?? null;
8584
const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
85+
const failedAt = failureEdgeAt(shell);
8686
if (
87-
(runtime?.status === "error" || runtime?.status === "failed") &&
87+
failedAt !== undefined &&
8888
(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)))
9090
) {
9191
return true;
9292
}
@@ -101,6 +101,18 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean
101101
return false;
102102
}
103103

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+
104116
export function canSnooze(
105117
shell: Pick<
106118
ThreadSnoozeShell,
@@ -145,7 +157,6 @@ export function threadWokeAt(
145157
// than that visit's lastVisitedAt).
146158
if (threadRaisedHandWhileSnoozed(shell)) {
147159
const latestRun = shell.latestRun ?? shell.latestTurn ?? null;
148-
const runtime = shell.runtime ?? shell.session ?? null;
149160
if (
150161
shell.snoozedAt != null &&
151162
(latestRun?.state === "completed" || latestRun?.status === "completed") &&
@@ -154,6 +165,15 @@ export function threadWokeAt(
154165
) {
155166
return latestRun.completedAt;
156167
}
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;
157177
return runtime?.updatedAt ?? shell.snoozedAt ?? null;
158178
}
159179
return wakeAtMs <= Date.parse(options.now) ? shell.snoozedUntil : null;

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,46 @@ describe("effectiveSnoozed", () => {
119119
).toBe(true);
120120
});
121121

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+
122162
it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
123163
expect(
124164
effectiveSnoozed(makeShell({ snoozedUntil: FUTURE_WAKE, sessionStatus: "running" }), {

0 commit comments

Comments
 (0)