Skip to content

Commit 9e9fa10

Browse files
fix(server): clear snooze when settling threads (pingdotgg#17547)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 863bc01 commit 9e9fa10

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as DateTime from "effect/DateTime";
2222
import * as Effect from "effect/Effect";
2323
import * as Layer from "effect/Layer";
2424
import * as Result from "effect/Result";
25+
import * as Stream from "effect/Stream";
2526
import * as TestClock from "effect/testing/TestClock";
2627
import * as SqlClient from "effect/sql/SqlClient";
2728
import * as SqlitePersistence from "../persistence/Sqlite.ts";
@@ -1100,3 +1101,80 @@ it.effect("replays a thread.create receipt without changing the thread", () =>
11001101
assert.equal(yield* orchestrator.getThreadEventSequence(threadId), original.sequence);
11011102
}).pipe(Effect.provide(layerTest)),
11021103
);
1104+
1105+
it.effect("explicit settle clears snooze atomically and stays idempotent after re-snoozing", () =>
1106+
Effect.gen(function* () {
1107+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1108+
const projections = yield* ProjectionStore.ProjectionStoreV2;
1109+
const events = yield* EventSink.EventSinkV2;
1110+
const threadId = ThreadId.make("thread:settle-snoozed");
1111+
yield* orchestrator.dispatch({
1112+
type: "thread.create",
1113+
commandId: CommandId.make("create-settle-snoozed"),
1114+
threadId,
1115+
projectId: ProjectId.make("project:settle-snoozed"),
1116+
title: "Snoozed thread",
1117+
modelSelection,
1118+
runtimeMode: "full-access",
1119+
interactionMode: "default",
1120+
branch: null,
1121+
worktreePath: null,
1122+
createdBy: "user",
1123+
creationSource: "web",
1124+
});
1125+
const now = yield* DateTime.now;
1126+
const settledAt = DateTime.add(now, { seconds: 1 });
1127+
const snoozedUntil = DateTime.add(now, { hours: 1 });
1128+
for (const round of [1, 2]) {
1129+
yield* orchestrator.dispatch({
1130+
type: "thread.snooze",
1131+
commandId: CommandId.make(`snooze-before-settle-${round}`),
1132+
threadId,
1133+
snoozedUntil: DateTime.formatIso(snoozedUntil),
1134+
});
1135+
assert.deepInclude(yield* projections.getThread(threadId), {
1136+
snoozedUntil,
1137+
snoozedAt: yield* DateTime.now,
1138+
});
1139+
yield* TestClock.adjust("1 second");
1140+
const commandId = CommandId.make(`settle-snoozed-${round}`);
1141+
yield* orchestrator.dispatch({ type: "thread.settle", commandId, threadId });
1142+
const expected = {
1143+
settledOverride: "settled",
1144+
settledAt,
1145+
snoozedUntil: null,
1146+
snoozedAt: null,
1147+
updatedAt: yield* DateTime.now,
1148+
} as const;
1149+
const committed = yield* events.readByCommandId({ commandId }).pipe(Stream.runCollect);
1150+
assert.lengthOf(committed, 1);
1151+
assert.equal(committed[0]?.event.type, "thread.settled");
1152+
assert.deepInclude(committed[0]?.event.payload, expected);
1153+
const settled = yield* projections.getThreadShell(threadId);
1154+
assert.deepInclude(settled, expected);
1155+
assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, expected);
1156+
1157+
yield* TestClock.adjust("1 second");
1158+
yield* orchestrator.dispatch({
1159+
type: "thread.settle",
1160+
commandId: CommandId.make(`settle-again-${round}`),
1161+
threadId,
1162+
});
1163+
assert.deepEqual(yield* projections.getThreadShell(threadId), settled);
1164+
}
1165+
yield* orchestrator.dispatch({
1166+
type: "thread.unsettle",
1167+
commandId: CommandId.make("unsettle-after-snooze"),
1168+
threadId,
1169+
reason: "user",
1170+
});
1171+
const active = {
1172+
settledOverride: "active",
1173+
settledAt: null,
1174+
snoozedUntil: null,
1175+
snoozedAt: null,
1176+
} as const;
1177+
assert.deepInclude(yield* projections.getThreadShell(threadId), active);
1178+
assert.deepInclude((yield* projections.getThreadProjection(threadId)).thread, active);
1179+
}).pipe(Effect.provide(layerTest)),
1180+
);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2889,7 +2889,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
28892889
pinnedAt: null,
28902890
pinOrderKey: null,
28912891
activeOrderKey: null,
2892-
updatedAt: alreadySettled ? thread.updatedAt : now,
2892+
snoozedUntil: null,
2893+
snoozedAt: null,
2894+
updatedAt:
2895+
alreadySettled && thread.snoozedUntil == null && thread.snoozedAt == null
2896+
? thread.updatedAt
2897+
: now,
28932898
};
28942899
}
28952900
case "thread.unsettle": {

0 commit comments

Comments
 (0)