TypeScript
fix(server): mark unread no longer brings back a dismissed wake (#24080)
fixfoncb5fac907a21
diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.tsindex c36dfc365af..61515a053ac 100644--- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts@@ -260,6 +260,120 @@ it.effect("marks a never-visited thread's first completion unread", () =>}).pipe(Effect.provide(layerTest)),);+const completeAndSnoozeThread = Effect.fn("test.completeAndSnoozeThread")(function* (+ name: string,+ snoozeMinutes: number,+) {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const projections = yield* ProjectionStore.ProjectionStoreV2;+ const threadId = ThreadId.make(`thread:${name}`);+ yield* orchestrator.dispatch({+ type: "thread.create",+ commandId: CommandId.make(`create-${name}`),+ threadId,+ projectId: ProjectId.make(`project:${name}`),+ title: "Snoozed completion",+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ createdBy: "user",+ creationSource: "web",+ });+ yield* orchestrator.dispatch({+ type: "message.dispatch",+ commandId: CommandId.make(`dispatch-${name}`),+ threadId,+ messageId: MessageId.make(`dispatch-${name}`),+ text: "Run in the background",+ attachments: [],+ dispatchMode: { type: "defer_start" },+ createdBy: "agent",+ creationSource: "mcp",+ });+ yield* orchestrator.dispatch({+ type: "run.interrupt",+ commandId: CommandId.make(`interrupt-${name}`),+ threadId,+ runId: (yield* projections.getThreadProjection(threadId)).runs[0]!.id,+ });+ const completedAt = (yield* projections.getThreadProjection(threadId)).runs.at(-1)?.completedAt;+ assert.ok(completedAt);+ yield* TestClock.adjust("1 second");+ const snoozedUntil = DateTime.add(yield* DateTime.now, { minutes: snoozeMinutes });+ yield* orchestrator.dispatch({+ type: "thread.snooze",+ commandId: CommandId.make(`snooze-${name}`),+ threadId,+ snoozedUntil: DateTime.formatIso(snoozedUntil),+ });+ return { threadId, completedAt, snoozedUntil };+});++it.effect("mark unread retires an expired snooze so a dismissed wake stays dismissed", () =>+ Effect.gen(function* () {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const projections = yield* ProjectionStore.ProjectionStoreV2;+ const { threadId, completedAt, snoozedUntil } = yield* completeAndSnoozeThread(+ "mark-unread-expired-snooze",+ 1,+ );+ yield* TestClock.adjust("2 minutes");+ yield* orchestrator.dispatch({+ type: "thread.visit",+ commandId: CommandId.make("dismiss-woke"),+ threadId,+ visitedAt: DateTime.formatIso(snoozedUntil),+ });+ const dismissed = yield* projections.getThread(threadId);+ assert.deepEqual(dismissed.lastVisitedAt, snoozedUntil);++ yield* orchestrator.dispatch({+ type: "thread.mark-unread",+ commandId: CommandId.make("mark-unread-expired-snooze"),+ threadId,+ });+ assert.deepEqual(yield* projections.getThread(threadId), {+ ...dismissed,+ lastVisitedAt: DateTime.subtract(completedAt, { milliseconds: 1 }),+ snoozedUntil: null,+ snoozedAt: null,+ lastSnoozeWakeAt: snoozedUntil,+ });+ }).pipe(Effect.provide(layerTest)),+);++it.effect("mark unread leaves a future snooze untouched", () =>+ Effect.gen(function* () {+ const orchestrator = yield* Orchestrator.OrchestratorV2;+ const projections = yield* ProjectionStore.ProjectionStoreV2;+ const { threadId, completedAt } = yield* completeAndSnoozeThread(+ "mark-unread-future-snooze",+ 60,+ );+ yield* TestClock.adjust("1 minute");+ yield* orchestrator.dispatch({+ type: "thread.visit",+ commandId: CommandId.make("visit-snoozed"),+ threadId,+ visitedAt: DateTime.formatIso(yield* DateTime.now),+ });+ const visited = yield* projections.getThread(threadId);+ assert.isNotNull(visited.snoozedUntil);++ yield* orchestrator.dispatch({+ type: "thread.mark-unread",+ commandId: CommandId.make("mark-unread-future-snooze"),+ threadId,+ });+ assert.deepEqual(yield* projections.getThread(threadId), {+ ...visited,+ lastVisitedAt: DateTime.subtract(completedAt, { milliseconds: 1 }),+ });+ }).pipe(Effect.provide(layerTest)),+);+it.effect("dispatches metadata, queue resume and request controls without hydrating unrelated history",() =>diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 94de805b465..76bcc00bbcb 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -3360,8 +3360,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functioupdatedAt: thread.updatedAt,};}- case "thread.mark-unread":- return { ...thread, lastVisitedAt: markUnreadVisitedAt };+ case "thread.mark-unread": {+ // Retire the wake before rewinding the visit, preserving its settlement timer.+ const snoozeExpired =+ thread.snoozedUntil != null &&+ DateTime.toEpochMillis(thread.snoozedUntil) <= DateTime.toEpochMillis(now);+ return {+ ...thread,+ lastVisitedAt: markUnreadVisitedAt,+ ...(snoozeExpired+ ? { snoozedUntil: null, snoozedAt: null, lastSnoozeWakeAt: thread.snoozedUntil }+ : {}),+ };+ }case "thread.metadata.update": {const previousRecovery =thread.limitRecovery?.runId === command.limitRecovery?.runId &&diff --git a/packages/client-runtime/src/state/threadVisits.test.ts b/packages/client-runtime/src/state/threadVisits.test.tsindex a99417534ea..adcac7139e8 100644--- a/packages/client-runtime/src/state/threadVisits.test.ts+++ b/packages/client-runtime/src/state/threadVisits.test.ts@@ -111,6 +111,24 @@ it("keeps a mark-unread on the open thread until new activity lands", () => {expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(30) }]);});+it("keeps mark unread sticky after an expired wake is retired", () => {+ using t = setup("thread-unread-retired-wake", {+ updatedAt: at(10),+ lastVisitedAt: at(0),+ latestRun: { completedAt: at(9) },+ wokeAt: at(15),+ });+ t.mount();+ expect(t.visits).toEqual([{ threadId: t.ref.threadId, visitedAt: at(15), opened: true }]);+ t.update({ lastVisitedAt: at(15) });+ t.update({ lastVisitedAt: at(8), wokeAt: null });+ vi.advanceTimersByTime(VISIT_DISPATCH_THROTTLE_MS);+ expect(t.visits).toHaveLength(1);++ t.update({ updatedAt: at(30) });+ expect(t.visits.slice(1)).toEqual([{ threadId: t.ref.threadId, visitedAt: at(30) }]);+});+it("waits for the shell and records local visits for servers without tracking", () => {using t = setup("thread-local", null);t.mount();diff --git a/packages/client-runtime/src/state/threadVisits.ts b/packages/client-runtime/src/state/threadVisits.tsindex 08d05909a2d..c08f47ed014 100644--- a/packages/client-runtime/src/state/threadVisits.ts+++ b/packages/client-runtime/src/state/threadVisits.ts@@ -115,7 +115,8 @@ export function createThreadVisitTracker(options: ThreadVisitTrackerOptions) {}const lastVisitedAtMs = shell.lastVisitedAt ? Date.parse(shell.lastVisitedAt) : NaN;if (!Number.isNaN(lastVisitedAtMs) && lastVisitedAtMs >= updatedAtMs) return;- if (lastDispatchedUpdatedAt === visitedAt) return;+ if (lastDispatchedUpdatedAt !== null && Date.parse(lastDispatchedUpdatedAt) >= updatedAtMs)+ return;const completedAtMs = shell.latestRun?.completedAt? Date.parse(shell.latestRun.completedAt)