Skip to content

Commit 4a614a9

Browse files
t3dotggclaude
andcommitted
fix(server): settle cleanup cannot kill a session started mid-settle
Review follow-up: the settle-originated session stop is now conditional (onlyIfSettled) and decided in the decider against the serialized read model, so a turn started between the settle and the stop survives. Archive stops stay unconditional. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 40fbae2 commit 4a614a9

5 files changed

Lines changed: 86 additions & 1 deletion

File tree

‎apps/server/src/orchestration/decider.settled.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,55 @@ it.layer(NodeServices.layer)("settled thread decider", (it) => {
518518
expect(routineEvents.map((event) => event.type)).toEqual(["thread.activity-appended"]);
519519
}),
520520
);
521+
522+
it.effect("drops an onlyIfSettled session stop when the thread was re-engaged", () =>
523+
Effect.gen(function* () {
524+
const stopCommand = (commandId: string) =>
525+
({
526+
type: "thread.session.stop",
527+
commandId: CommandId.make(commandId),
528+
threadId: ThreadId.make("thread-1"),
529+
createdAt: NOW,
530+
onlyIfSettled: true,
531+
}) as const;
532+
533+
// Still settled with an idle session: the cleanup stop goes through.
534+
const stopped = yield* decideOrchestrationCommand({
535+
command: stopCommand("cmd-stop-settled-idle"),
536+
readModel: makeReadModel("settled", null, makeSession("ready")),
537+
});
538+
const stoppedEvents = Array.isArray(stopped) ? stopped : [stopped];
539+
expect(stoppedEvents.map((event) => event.type)).toEqual(["thread.session-stop-requested"]);
540+
541+
// Re-engaged before the stop was decided (a turn start unsettles the
542+
// thread): the stale cleanup stop must not kill the new session.
543+
const unsettledError = yield* decideOrchestrationCommand({
544+
command: stopCommand("cmd-stop-unsettled"),
545+
readModel: makeReadModel(null, null, makeSession("starting")),
546+
}).pipe(Effect.flip);
547+
expect(unsettledError._tag).toBe("OrchestrationCommandInvariantError");
548+
549+
// Still settled but the session is already coming alive: same drop.
550+
const aliveError = yield* decideOrchestrationCommand({
551+
command: stopCommand("cmd-stop-session-alive"),
552+
readModel: makeReadModel("settled", null, makeSession("starting")),
553+
}).pipe(Effect.flip);
554+
expect(aliveError._tag).toBe("OrchestrationCommandInvariantError");
555+
556+
// Without the flag the stop stays unconditional (archive, stop button).
557+
const unconditional = yield* decideOrchestrationCommand({
558+
command: {
559+
type: "thread.session.stop",
560+
commandId: CommandId.make("cmd-stop-unconditional"),
561+
threadId: ThreadId.make("thread-1"),
562+
createdAt: NOW,
563+
},
564+
readModel: makeReadModel(null, null, makeSession("starting")),
565+
});
566+
const unconditionalEvents = Array.isArray(unconditional) ? unconditional : [unconditional];
567+
expect(unconditionalEvents.map((event) => event.type)).toEqual([
568+
"thread.session-stop-requested",
569+
]);
570+
}),
571+
);
521572
});

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,11 +1116,32 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
11161116
}
11171117

11181118
case "thread.session.stop": {
1119-
yield* requireThread({
1119+
const thread = yield* requireThread({
11201120
readModel,
11211121
command,
11221122
threadId: command.threadId,
11231123
});
1124+
// Settle-cleanup stops are conditional: between the settle landing and
1125+
// this command, another client may have re-engaged the thread (a turn
1126+
// start unsettles it and brings the session alive). Commands are
1127+
// decided serially against this read model, so checking here — not in
1128+
// the dispatcher's pre-settle snapshot — closes that race.
1129+
if (command.onlyIfSettled === true) {
1130+
const sessionComingAlive =
1131+
thread.session?.status === "starting" || thread.session?.status === "running";
1132+
if (
1133+
thread.settledOverride !== "settled" ||
1134+
sessionComingAlive ||
1135+
threadHasQueuedTurnStart(thread, command.createdAt)
1136+
) {
1137+
return yield* Effect.fail(
1138+
new OrchestrationCommandInvariantError({
1139+
commandType: command.type,
1140+
detail: `thread ${command.threadId} was re-engaged after settle; skipping session stop`,
1141+
}),
1142+
);
1143+
}
1144+
}
11241145
return {
11251146
...(yield* withEventBase({
11261147
aggregateKind: "thread",

‎apps/server/src/server.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7084,6 +7084,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
70847084
if (sessionStopCommand?.type === "thread.session.stop") {
70857085
assert.equal(sessionStopCommand.threadId, threadId);
70867086
assert.equal(sessionStopCommand.commandId, "session-stop-for-settle:cmd-thread-settle");
7087+
assert.equal(sessionStopCommand.onlyIfSettled, true);
70877088
}
70887089
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
70897090
);

‎apps/server/src/ws.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,12 @@ const makeWsRpcLayer = (
10801080
),
10811081
threadId: parkingCommand.threadId,
10821082
createdAt: yield* nowIso,
1083+
// A settled thread can be re-engaged before this stop is
1084+
// decided; the decider then drops the stop instead of
1085+
// killing the new session. Archive stops stay
1086+
// unconditional: turn starts on archived threads are
1087+
// rejected, so there is no new session to protect.
1088+
...(parkingKind === "settle" ? { onlyIfSettled: true } : {}),
10831089
});
10841090

10851091
yield* dispatchNormalizedCommand(stopCommand);

‎packages/contracts/src/orchestration.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,12 @@ const ThreadSessionStopCommand = Schema.Struct({
870870
commandId: CommandId,
871871
threadId: ThreadId,
872872
createdAt: IsoDateTime,
873+
// Settle-cleanup stops are conditional: the decider drops the stop if the
874+
// thread was re-engaged (unsettled, session starting/running, or a queued
875+
// turn start) between the settle and this command. Guarding in the decider
876+
// closes the race a post-settle snapshot read cannot: commands are decided
877+
// serially against the authoritative read model.
878+
onlyIfSettled: Schema.optional(Schema.Boolean),
873879
});
874880

875881
const DispatchableClientOrchestrationCommand = Schema.Union([

0 commit comments

Comments
 (0)