Clear pending wake claims on session error

The domain-event fiber can observe session-set(error) after the
turn.start command is accepted but before dispatchWake stamps the claim
in-flight. Phase-guarding the release left that pending claim behind
with nothing else to clear it, blocking every future wake for the
thread. Release regardless of phase — releaseClaim only rewinds
in-flight claims, so a pending release is just a drop.

Regression test verified to fail against the phase-guarded release.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-07-26 20:00:20 -07:00
1 parent 355a2893d4
commit d1faaa16ff
2 files changed
+58 -1

No files matched your search

@@ -125,6 +125,7 @@ const harness = Effect.fn("PullRequestMonitor.testHarness")(function* (input: {
readonly reconcileThreadIds?: ReadonlyArray<ThreadId>;
readonly failReconcileThreadId?: ThreadId;
readonly releaseWakeOnDispatch?: boolean;
readonly sessionErrorOnDispatch?: boolean;
readonly busy?: { value: boolean };
readonly queuedUserTurn?: boolean;
readonly afterUpdate?: () => void;
@@ -219,6 +220,38 @@ const harness = Effect.fn("PullRequestMonitor.testHarness")(function* (input: {
}
if (command.type === "thread.turn.start") {
ordering.push("dispatch");
// Simulates the event-stream fiber observing session-set(error)
// after the turn.start command is accepted but before dispatchWake
// stamps the claim in-flight — the window where a pending claim
// could be left behind forever.
if (input.sessionErrorOnDispatch && monitorService) {
return monitorService
.handleDomainEvent({
sequence: 1,
eventId: EventId.make("session-error-early"),
aggregateKind: "thread",
aggregateId: threadId,
occurredAt: now,
commandId: CommandId.make("session-error-early"),
causationEventId: null,
correlationId: null,
metadata: {},
type: "thread.session-set",
payload: {
threadId,
session: {
threadId,
status: "error",
providerName: "claude",
runtimeMode: "full-access",
activeTurnId: null,
lastError: "boom",
updatedAt: now,
},
},
})
.pipe(Effect.as({ sequence: commands.length }));
}
if (input.releaseWakeOnDispatch && monitorService) {
return monitorService
.handleDomainEvent({
@@ -525,6 +558,24 @@ describe("PullRequestMonitor dispatch protocol", () => {
}),
);
it.effect("session error clears a claim that is still pending", () =>
Effect.gen(function* () {
// The event fiber can see the error before the claim is stamped
// in-flight. A pending claim left behind is cleared by nothing else,
// so it would block every subsequent wake for this thread.
const current = { value: snapshot({ reviewThreads: [comment("one")] }) };
const h = yield* harness({ initial: snapshot(), current, sessionErrorOnDispatch: true });
yield* h.monitor.pollOnce;
// A later poll must still be able to wake — proving no stuck claim.
current.value = snapshot({ reviewThreads: [comment("one"), comment("two")] });
yield* h.monitor.pollOnce;
assert.strictEqual(
h.commands.filter((command) => command.type === "thread.turn.start").length,
2,
);
}),
);
it.effect("conditional ack preserves a user-turn release", () =>
Effect.gen(function* () {
const initial = snapshot();
@@ -553,7 +553,13 @@ export const make = Effect.gen(function* () {
// re-diff (I5). Rewind on error; a clean finish (ready/idle/stopped)
// just frees the claim — its events were genuinely delivered.
if (status === "error") {
return releaseClaim(threadId, (claim) => claim.phase === "in-flight");
// Release regardless of phase: this handler runs on the event-stream
// fiber and can observe the error before dispatchWake stamps the
// claim in-flight. A claim left behind in `pending` is never cleared
// by any other path, which would block every future wake for this
// thread. releaseClaim only rewinds in-flight claims, so a pending
// release is just a claim drop (it never acked a cursor).
return releaseClaim(threadId, () => true);
}
if (status !== "starting" && status !== "running") {
return mutateClaim(threadId, (claim) => (claim?.phase === "in-flight" ? undefined : claim));