TypeScript
fix(server): re-snoozing a woken thread to the same wake time hides it again (#14887)
Jonas B.699c1c5d4ce7
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex bc4f673ef0b..7d25775b50e 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -2878,17 +2878,15 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio};}case "thread.snooze": {- const sameWakeTime =- thread.snoozedUntil != null &&- snoozedUntil !== null &&- DateTime.toEpochMillis(thread.snoozedUntil) === DateTime.toEpochMillis(snoozedUntil);- const existingSnoozedAt = sameWakeTime ? (thread.snoozedAt ?? null) : null;+ // Always restamp, even for the same wake time: a re-snooze after a+ // failure or completion woke the thread means "I saw it, not now".+ // Retries are deduplicated by commandId before reaching here.return {...thread,snoozedUntil,limitRecovery: thread.limitRecovery ? { ...thread.limitRecovery, snooze: false } : null,- snoozedAt: existingSnoozedAt ?? now,- updatedAt: existingSnoozedAt === null ? now : thread.updatedAt,+ snoozedAt: now,+ updatedAt: now,};}case "thread.unsnooze": {diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.tsindex 0c924ff10b5..1b6a7de8a94 100644--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts@@ -5191,9 +5191,11 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {const firstProjection = yield* orchestrator.getThreadProjection(threadId);const firstSnoozedAt = firstProjection.thread.snoozedAt;- const firstUpdatedAt = firstProjection.thread.updatedAt;assert.isNotNull(firstSnoozedAt);+ // Repeating a calendar preset sends the same wake time. It must still+ // restamp snoozedAt, or a failure that woke the thread keeps it awake.+ yield* TestClock.adjust("1 second");yield* orchestrator.dispatch({type: "thread.snooze",commandId: CommandId.make("runtime-layer-snoozed-thread-snooze-again"),@@ -5205,8 +5207,11 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {const thread = shell.threads.find((candidate) => candidate.id === threadId);assert.isDefined(thread);assert.equal(DateTime.formatIso(thread.snoozedUntil!), snoozedUntil);- assert.deepEqual(thread.snoozedAt, firstSnoozedAt);- assert.deepEqual(thread.updatedAt, firstUpdatedAt);+ assert.equal(+ DateTime.toEpochMillis(thread.snoozedAt!),+ DateTime.toEpochMillis(firstSnoozedAt!) + 1_000,+ );+ assert.deepEqual(thread.updatedAt, thread.snoozedAt);yield* orchestrator.dispatch({type: "thread.unsnooze",diff --git a/packages/client-runtime/src/state/threadCommands.test.ts b/packages/client-runtime/src/state/threadCommands.test.tsindex cf36760baec..d6f81964bde 100644--- a/packages/client-runtime/src/state/threadCommands.test.ts+++ b/packages/client-runtime/src/state/threadCommands.test.ts@@ -182,6 +182,28 @@ describe("remote thread lifecycle commands", () => {}),);+ it.effect("restamps snoozedAt when re-snoozing to the same wake time", () =>+ Effect.gen(function* () {+ const h = yield* makeHarness();+ const earlierSnoozedAt = DateTime.makeUnsafe("2000-01-01T00:00:00.000Z");+ h.registry.set(h.snapshotAtom(ENVIRONMENT_ID), {+ ...SNAPSHOT,+ threads: [{ ...SNAPSHOT.threads[0]!, snoozedUntil: FUTURE, snoozedAt: earlierSnoozedAt }],+ });+ const result = h.commands.snooze.run(h.registry, {+ environmentId: ENVIRONMENT_ID,+ input: { threadId: THREAD_ID, snoozedUntil: DateTime.formatIso(FUTURE) },+ });+ const snoozedAt = h.registry.get(h.visibleAtom)?.threads[0]?.snoozedAt;+ expect(DateTime.toEpochMillis(snoozedAt!)).toBeGreaterThan(+ DateTime.toEpochMillis(earlierSnoozedAt),+ );+ const request = yield* Queue.take(h.requests);+ yield* Deferred.succeed(request.reply, { sequence: 2 });+ expect((yield* Effect.promise(() => result))._tag).toBe("Success");+ }),+ );+it.effect("keeps the preview after acknowledgement until the matching shell update arrives", () =>Effect.gen(function* () {const h = yield* makeHarness();diff --git a/packages/client-runtime/src/state/threadCommands.ts b/packages/client-runtime/src/state/threadCommands.tsindex 7ba723ab353..c0b99600467 100644--- a/packages/client-runtime/src/state/threadCommands.ts+++ b/packages/client-runtime/src/state/threadCommands.ts@@ -443,11 +443,7 @@ export function createThreadEnvironmentAtoms<R, E>(...thread,pendingRuntimeRequest: null,snoozedUntil: DateTime.makeUnsafe(input.snoozedUntil),- snoozedAt:- thread.snoozedUntil != null &&- DateTime.formatIso(thread.snoozedUntil) === input.snoozedUntil- ? (thread.snoozedAt ?? now)- : now,+ snoozedAt: now,},),unsnooze: optimistic.wrap(commands.unsnooze, (thread) => ({