Skip to content

Commit 3bf2abb

Browse files
fix(server): defer settle when the thread lookup fails
If reading the thread shell failed, Settle fell back to a plain thread.settle, which the decider rejects for a working thread. Defer it to settle-when-idle instead, since waiting never stops running work. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 0c53149 commit 3bf2abb

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎apps/server/src/orchestration/Normalizer.settle.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Layer from "effect/Layer";
1313
import * as Option from "effect/Option";
1414

1515
import * as ServerConfig from "../config.ts";
16+
import { PersistenceSqlError } from "../persistence/Errors.ts";
1617
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
1718
import { normalizeDispatchCommand } from "./Normalizer.ts";
1819
import { decideOrchestrationCommand } from "./decider.ts";
@@ -72,7 +73,9 @@ const testLayer = Layer.mergeAll(
7273
ServerConfig.layerTest(process.cwd(), { prefix: "t3-normalizer-settle-" }),
7374
Layer.mock(ProjectionSnapshotQuery)({
7475
getThreadShellById: (threadId) =>
75-
Effect.succeed(Option.fromUndefinedOr(threads.find((thread) => thread.id === threadId))),
76+
threadId === "unreadable"
77+
? Effect.fail(new PersistenceSqlError({ operation: "getThreadShellById" }))
78+
: Effect.succeed(Option.fromUndefinedOr(threads.find((thread) => thread.id === threadId))),
7679
}),
7780
).pipe(Layer.provideMerge(NodeServices.layer));
7881

@@ -88,6 +91,8 @@ it.layer(testLayer)("normalizeDispatchCommand settle", (it) => {
8891
expect(yield* settle("idle")).toBe("thread.settle");
8992
expect(yield* settle("running")).toBe("thread.settle-when-idle");
9093
expect(yield* settle("watching")).toBe("thread.settle-when-idle");
94+
// An unreadable thread may still be working, so it waits as well.
95+
expect(yield* settle("unreadable")).toBe("thread.settle-when-idle");
9196
}),
9297
);
9398

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,15 @@ export const normalizeDispatchCommand = (command: ClientOrchestrationCommand) =>
138138
if (canonicalCommand.type === "thread.settle") {
139139
// Settling stops the session. A thread still working, including
140140
// background work, files away now and settles once idle instead.
141+
// A failed lookup defers too: waiting never stops running work.
141142
const snapshots = yield* ProjectionSnapshotQuery;
142-
const thread = yield* snapshots
143+
const lookup = yield* snapshots
143144
.getThreadShellById(canonicalCommand.threadId)
144-
.pipe(Effect.orElseSucceed(() => Option.none()));
145-
if (Option.isSome(thread) && isThreadWorkingForSettlement(thread.value, receivedAt)) {
145+
.pipe(Effect.option);
146+
if (
147+
Option.isNone(lookup) ||
148+
Option.exists(lookup.value, (thread) => isThreadWorkingForSettlement(thread, receivedAt))
149+
) {
146150
return {
147151
...canonicalCommand,
148152
type: "thread.settle-when-idle",

0 commit comments

Comments
 (0)