Skip to content

Commit 9493849

Browse files
fix(threads): verify snooze timestamps across background updates
1 parent ad35d95 commit 9493849

5 files changed

Lines changed: 105 additions & 121 deletions

File tree

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

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,6 @@ it.effect("memory projection keeps restart-cancelled work through a stale run.up
658658
restartCancelledWorkSurvivesStaleRunUpdate.pipe(Effect.provide(ProjectionStore.layerMemory)),
659659
);
660660

661-
// Clients read a failed thread's updatedAt as its failure edge, so background
662-
// writes must keep the orchestrator's timestamp in both stores.
663661
const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {
664662
const store = yield* ProjectionStore.ProjectionStoreV2;
665663
const createdAt = DateTime.makeUnsafe("2026-10-06T14:00:00.000Z");
@@ -694,24 +692,80 @@ const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {
694692
occurredAt: createdAt,
695693
payload: thread,
696694
});
695+
const readActivity = Effect.gen(function* () {
696+
const projection = yield* store.getThreadProjection(threadId);
697+
const thread = yield* store.getThread(threadId);
698+
const shell = yield* store.getThreadShell(threadId);
699+
const listed = (yield* store.getShellSnapshot()).threads.find((entry) => entry.id === threadId);
700+
assert.deepEqual(projection.thread.updatedAt, createdAt);
701+
assert.deepEqual(thread.updatedAt, createdAt);
702+
assert.deepEqual(shell?.updatedAt, createdAt);
703+
assert.deepEqual(listed?.updatedAt, createdAt);
704+
return projection;
705+
});
706+
const link = {
707+
host: "github.com",
708+
repository: "owner/repository",
709+
number: 42,
710+
url: "https://github.com/owner/repository/pull/42",
711+
source: "manual" as const,
712+
linkedAt: DateTime.formatIso(createdAt),
713+
snapshot: null,
714+
stack: null,
715+
};
697716
yield* store.apply({
698717
id: EventId.make("event:background-updates:pull-request-synced"),
699718
type: "thread.pull-request-synced",
700719
threadId,
701720
occurredAt: DateTime.add(createdAt, { minutes: 45 }),
702-
payload: thread,
721+
payload: { ...thread, pullRequests: [link] },
722+
});
723+
const synced = yield* readActivity;
724+
assert.deepEqual(synced.updatedAt, createdAt);
725+
assert.equal(synced.thread.pullRequests?.[0]?.number, 42);
726+
727+
const snapshot = {
728+
state: "open" as const,
729+
title: "Ship it",
730+
headBranch: "feature",
731+
baseBranch: "main",
732+
isDraft: false,
733+
updatedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 46 })),
734+
syncedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 47 })),
735+
closedAt: null,
736+
mergedAt: null,
737+
};
738+
yield* store.apply({
739+
id: EventId.make("event:background-updates:pull-request-link-updated"),
740+
type: "thread.pull-request-link-updated",
741+
threadId,
742+
occurredAt: DateTime.add(createdAt, { minutes: 48 }),
743+
payload: { ...link, snapshot },
703744
});
745+
const refreshed = yield* readActivity;
746+
assert.deepEqual(refreshed.updatedAt, createdAt);
747+
assert.deepEqual(refreshed.thread.pullRequests?.[0]?.snapshot, snapshot);
748+
704749
yield* store.apply({
705750
id: EventId.make("event:background-updates:active-reordered"),
706751
type: "thread.active-reordered",
707752
threadId,
708753
occurredAt: DateTime.add(createdAt, { minutes: 50 }),
709-
payload: { ...thread, activeOrderKey: "a" },
754+
payload: { ...refreshed.thread, activeOrderKey: "a" },
755+
});
756+
const reordered = yield* readActivity;
757+
assert.deepEqual(reordered.updatedAt, createdAt);
758+
assert.equal(reordered.thread.activeOrderKey, "a");
759+
760+
// An already-unpinned thread keeps its activity time even when replay's envelope advances.
761+
yield* store.apply({
762+
id: EventId.make("event:background-updates:already-unpinned"),
763+
type: "thread.unpinned",
764+
threadId,
765+
occurredAt: DateTime.add(createdAt, { minutes: 55 }),
766+
payload: { ...reordered.thread, pinnedAt: null, pinOrderKey: null },
710767
});
711-
const projection = yield* store.getThreadProjection(threadId);
712-
assert.deepEqual(projection.thread.updatedAt, createdAt);
713-
assert.deepEqual(projection.updatedAt, createdAt);
714-
assert.deepEqual((yield* store.getThreadShell(threadId))?.updatedAt, createdAt);
768+
yield* readActivity;
715769
});
716770

717771
it.effect("memory projection keeps background thread updates out of updatedAt", () =>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,6 @@ export function applyToProjection(
730730
};
731731

732732
switch (event.type) {
733-
// A background PR refresh is not activity: keep the thread's timestamp, as
734-
// the SQL projection does.
735733
case "thread.pull-request-link-updated":
736734
return {
737735
...projection,
@@ -758,8 +756,7 @@ export function applyToProjection(
758756
...base,
759757
thread: event.payload,
760758
};
761-
// Arranging and background PR refreshes are not activity: keep the
762-
// timestamp the orchestrator chose, as the SQL projection does.
759+
// Reordering and PR refreshes preserve the orchestrator's activity timestamp.
763760
case "thread.active-reordered":
764761
case "thread.pull-request-synced":
765762
return {

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

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5249,76 +5249,6 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
52495249
assert.isNull(awakened.thread.snoozedAt);
52505250
}),
52515251
);
5252-
5253-
// Clients treat a failed thread's updatedAt as its failure edge, so a
5254-
// background refresh bumping it would wake the thread from snooze.
5255-
it.effect("keeps background pull request sync out of the persisted updatedAt", () =>
5256-
Effect.gen(function* () {
5257-
const projects = yield* ProjectService.ProjectService;
5258-
const orchestrator = yield* Orchestrator.OrchestratorV2;
5259-
const projectId = ProjectId.make("runtime-layer-pr-sync-project");
5260-
const threadId = ThreadId.make("runtime-layer-pr-sync-thread");
5261-
const key = { host: "github.com", repository: "owner/repository", number: 42 };
5262-
5263-
yield* projects.create({
5264-
commandId: CommandId.make("runtime-layer-pr-sync-project-create"),
5265-
projectId,
5266-
title: "PR sync projection",
5267-
workspaceRoot: "/tmp/runtime-layer-pr-sync-project",
5268-
});
5269-
yield* orchestrator.dispatch({
5270-
type: "thread.create",
5271-
createdBy: "user",
5272-
creationSource: "web",
5273-
commandId: CommandId.make("runtime-layer-pr-sync-thread-create"),
5274-
threadId,
5275-
projectId,
5276-
title: "PR sync thread",
5277-
modelSelection,
5278-
runtimeMode: "full-access",
5279-
interactionMode: "default",
5280-
branch: null,
5281-
worktreePath: null,
5282-
});
5283-
yield* orchestrator.dispatch({
5284-
type: "thread.pull-request.link",
5285-
commandId: CommandId.make("runtime-layer-pr-sync-link"),
5286-
threadId,
5287-
...key,
5288-
url: "https://github.com/owner/repository/pull/42",
5289-
source: "manual",
5290-
});
5291-
const linkedShell = yield* orchestrator.getShellSnapshot();
5292-
const linkedUpdatedAt = linkedShell.threads.find((t) => t.id === threadId)?.updatedAt;
5293-
assert.isDefined(linkedUpdatedAt);
5294-
5295-
yield* TestClock.adjust("1 minute");
5296-
yield* orchestrator.dispatch({
5297-
type: "thread.pull-request-link.sync",
5298-
commandId: CommandId.make("runtime-layer-pr-sync-sync"),
5299-
threadId,
5300-
...key,
5301-
snapshot: {
5302-
state: "open",
5303-
title: "Ship it",
5304-
headBranch: "feature",
5305-
baseBranch: "main",
5306-
isDraft: false,
5307-
updatedAt: "2026-08-27T00:00:00.000Z",
5308-
syncedAt: "2026-08-27T00:00:00.000Z",
5309-
closedAt: null,
5310-
mergedAt: null,
5311-
},
5312-
stack: null,
5313-
});
5314-
5315-
const shell = yield* orchestrator.getShellSnapshot();
5316-
const thread = shell.threads.find((candidate) => candidate.id === threadId);
5317-
assert.isDefined(thread);
5318-
assert.equal(thread.pullRequests?.[0]?.snapshot?.title, "Ship it");
5319-
assert.deepEqual(thread.updatedAt, linkedUpdatedAt);
5320-
}),
5321-
);
53225252
});
53235253

53245254
it.layer(layerSharedApplicationDataPlaneTest)("visited projection", (it) => {

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,8 @@ export function threadRaisedHandWhileSnoozed(shell: ThreadSnoozeShell): boolean
101101
return false;
102102
}
103103

104-
/**
105-
* When the thread's current failure happened, or undefined when it is not
106-
* failed. A failed run's completion is the failure edge; V2 runtime
107-
* summaries reuse the thread's updatedAt, which also moves on unrelated
108-
* metadata writes (PR sync, recovery choices) and would otherwise read as a
109-
* fresh failure. Legacy sessions stamp updatedAt on the status edge itself.
110-
*/
104+
// V2 runtime.updatedAt includes metadata writes; prefer the failed run's completion.
105+
// Legacy sessions only expose the runtime timestamp.
111106
function failureEdgeAt(shell: ThreadSnoozeShell): string | null | undefined {
112107
const runtime = shell.runtime ?? shell.session ?? null;
113108
if (runtime?.status !== "error" && runtime?.status !== "failed") return undefined;
@@ -170,8 +165,7 @@ export function threadWokeAt(
170165
) {
171166
return latestRun.completedAt;
172167
}
173-
// A failure from before the snooze did not cause this wake (an approval
174-
// or user input did), so its time would read as a wake the user already saw.
168+
// An old failure must not replace the activity time of an approval or input wake.
175169
const failedAt = failureEdgeAt(shell);
176170
if (
177171
failedAt != null &&

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

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

122-
it("stays snoozed when only metadata moved after snoozing a failed run", () => {
123-
// V2 runtime summaries reuse thread.updatedAt, which PR sync or recovery
124-
// choices can bump long after the failure the user already saw.
125-
const shell: ThreadSnoozeShell = {
126-
...makeShell({ snoozedUntil: FUTURE_WAKE }),
127-
runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
128-
latestRun: { status: "failed", completedAt: "2026-04-08T13:00:00.000Z" },
129-
};
130-
expect(effectiveSnoozed(shell, { now: NOW })).toBe(true);
131-
});
132-
133-
it("wakes early when the run failed after the snooze", () => {
134-
const shell: ThreadSnoozeShell = {
135-
...makeShell({ snoozedUntil: FUTURE_WAKE }),
136-
runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
137-
latestRun: { status: "failed", completedAt: "2026-04-10T10:00:00.000Z" },
138-
};
139-
expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
140-
expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T10:00:00.000Z");
141-
});
142-
143-
it("dates an approval wake after the snooze, not at an earlier failure", () => {
144-
const shell: ThreadSnoozeShell = {
145-
...makeShell({ snoozedUntil: FUTURE_WAKE, pending: "approval" }),
146-
runtime: { status: "failed", updatedAt: "2026-04-10T11:00:00.000Z" },
147-
latestRun: { status: "failed", completedAt: "2026-04-08T13:00:00.000Z" },
148-
};
149-
expect(effectiveSnoozed(shell, { now: NOW })).toBe(false);
150-
expect(threadWokeAt(shell, { now: NOW })).toBe("2026-04-10T11:00:00.000Z");
151-
});
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+
);
152161

153162
it("stays snoozed while the session keeps working — snooze never pauses the agent", () => {
154163
expect(

0 commit comments

Comments
 (0)