diff --git a/apps/server/src/monitor/PullRequestMonitor.test.ts b/apps/server/src/monitor/PullRequestMonitor.test.ts index 6dd15dac46..d2a89d583d 100644 --- a/apps/server/src/monitor/PullRequestMonitor.test.ts +++ b/apps/server/src/monitor/PullRequestMonitor.test.ts @@ -125,6 +125,7 @@ const harness = Effect.fn("PullRequestMonitor.testHarness")(function* (input: { readonly reconcileThreadIds?: ReadonlyArray; 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(); diff --git a/apps/server/src/monitor/PullRequestMonitor.ts b/apps/server/src/monitor/PullRequestMonitor.ts index 0d496636de..c86d4e6fbb 100644 --- a/apps/server/src/monitor/PullRequestMonitor.ts +++ b/apps/server/src/monitor/PullRequestMonitor.ts @@ -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));