Skip to content
TypeScript

fix(server): clear snooze when settling threads (#17547)

Rick Heere9e9fa10449a5
diff --git a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
index d68fd950a20..7237c16a277 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
@@ -22,6 +22,7 @@ import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Result from "effect/Result";
+import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import * as SqlClient from "effect/sql/SqlClient";
import * as SqlitePersistence from "../persistence/Sqlite.ts";
@@ -1100,3 +1101,80 @@ it.effect("replays a thread.create receipt without changing the thread", () =>
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), original.sequence);
}).pipe(Effect.provide(layerTest)),
);
+
+it.effect("explicit settle clears snooze atomically and stays idempotent after re-snoozing", () =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
+ const events = yield* EventSink.EventSinkV2;
+ const threadId = ThreadId.make("thread:settle-snoozed");
+ yield* orchestrator.dispatch({
+ type: "thread.create",
+ commandId: CommandId.make("create-settle-snoozed"),
+ threadId,
+ projectId: ProjectId.make("project:settle-snoozed"),
+ title: "Snoozed thread",
+ modelSelection,
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ branch: null,
+ worktreePath: null,
+ createdBy: "user",
+ creationSource: "web",
+ });
+ const now = yield* DateTime.now;
+ const settledAt = DateTime.add(now, { seconds: 1 });
+ const snoozedUntil = DateTime.add(now, { hours: 1 });
+ for (const round of [1, 2]) {
+ yield* orchestrator.dispatch({
+ type: "thread.snooze",
+ commandId: CommandId.make(`snooze-before-settle-${round}`),
+ threadId,
+ snoozedUntil: DateTime.formatIso(snoozedUntil),
+ });
+ assert.deepInclude(yield* projections.getThread(threadId), {
+ snoozedUntil,
+ snoozedAt: yield* DateTime.now,
+ });
+ yield* TestClock.adjust("1 second");
+ const commandId = CommandId.make(`settle-snoozed-${round}`);
+ yield* orchestrator.dispatch({ type: "thread.settle", commandId, threadId });
+ const expected = {
+ settledOverride: "settled",
+ settledAt,
+ snoozedUntil: null,
+ snoozedAt: null,
+ updatedAt: yield* DateTime.now,
+ } as const;
+ const committed = yield* events.readByCommandId({ commandId }).pipe(Stream.runCollect);
+ assert.lengthOf(committed, 1);
+ assert.equal(committed[0]?.event.type, "thread.settled");
+ assert.deepInclude(committed[0]?.event.payload, expected);
+ const settled = yield* projections.getThreadShell(threadId);
+ assert.deepInclude(settled, expected);
+ assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, expected);
+
+ yield* TestClock.adjust("1 second");
+ yield* orchestrator.dispatch({
+ type: "thread.settle",
+ commandId: CommandId.make(`settle-again-${round}`),
+ threadId,
+ });
+ assert.deepEqual(yield* projections.getThreadShell(threadId), settled);
+ }
+ yield* orchestrator.dispatch({
+ type: "thread.unsettle",
+ commandId: CommandId.make("unsettle-after-snooze"),
+ threadId,
+ reason: "user",
+ });
+ const active = {
+ settledOverride: "active",
+ settledAt: null,
+ snoozedUntil: null,
+ snoozedAt: null,
+ } as const;
+ assert.deepInclude(yield* projections.getThreadShell(threadId), active);
+ assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, active);
+ }).pipe(Effect.provide(layerTest)),
+);
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 96b6bab3c03..7e82d714917 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -2889,7 +2889,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
pinnedAt: null,
pinOrderKey: null,
activeOrderKey: null,
- updatedAt: alreadySettled ? thread.updatedAt : now,
+ snoozedUntil: null,
+ snoozedAt: null,
+ updatedAt:
+ alreadySettled && thread.snoozedUntil == null && thread.snoozedAt == null
+ ? thread.updatedAt
+ : now,
};
}
case "thread.unsettle": {