Skip to content

Commit 355a289

Browse files
t3dotggclaude
andcommitted
Address round-4 review feedback
- Serialize wake claim transitions with a semaphore so the post-dispatch cursor ack and a concurrent release can no longer interleave; the ack and wake-count increment are now one critical section with the claim check (Macroscope) - Rewind the cursor on session-set(error): the provider reactor emits it before provider.turn.start.failed, so dropping the claim there left the failure handler nothing to restore and acked events were lost. A clean session finish still just frees the claim (Bugbot, high) - Only mark boot reconciliation complete when every orphan ended successfully, so failures are retried on the next poll (Bugbot) - Drop the registration when session teardown runs before the engine is bound, instead of leaving a monitor that can wake a dead session (Bugbot) Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 0c1de35 commit 355a289

3 files changed

Lines changed: 123 additions & 31 deletions

File tree

‎apps/server/src/monitor/MonitorRegistry.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,16 @@ export const layer = Layer.effect(
149149
export const endActiveMonitorForSession = (threadId: ThreadId): Effect.Effect<void> =>
150150
Effect.gen(function* () {
151151
const registration = yield* make.get(threadId);
152-
if (Option.isNone(registration) || activeEngine === undefined) return;
152+
if (Option.isNone(registration)) return;
153+
if (activeEngine === undefined) {
154+
// The poller hasn't bound an engine yet, so the monitor end can't be
155+
// projected — but leaving the registration would let the poller wake a
156+
// thread whose session is already gone. Drop it and log the projection
157+
// gap; boot reconcile ends the orphaned "monitoring" projection.
158+
yield* make.remove(threadId, registration.value.generation);
159+
yield* Effect.logWarning("monitor registration dropped before engine bind", { threadId });
160+
return;
161+
}
153162
const endedAt = DateTime.formatIso(yield* DateTime.now);
154163
const commandId = CommandId.make(`monitor-session-ended:${threadId}:${endedAt}`);
155164
yield* activeEngine.dispatch({

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,47 @@ describe("PullRequestMonitor dispatch protocol", () => {
484484
}),
485485
);
486486

487+
it.effect("session error rewinds the acked wake cursor", () =>
488+
Effect.gen(function* () {
489+
// The provider reactor emits session-set(error) BEFORE
490+
// provider.turn.start.failed, so this handler must rewind rather than
491+
// silently drop the claim — otherwise the acked events never re-diff.
492+
const initial = snapshot();
493+
const h = yield* harness({
494+
initial,
495+
current: { value: snapshot({ reviewThreads: [comment("one")] }) },
496+
});
497+
yield* h.monitor.pollOnce;
498+
assert.strictEqual(h.getRegistration()?.wakeCount, 1);
499+
yield* h.monitor.handleDomainEvent({
500+
sequence: 2,
501+
eventId: EventId.make("session-error"),
502+
aggregateKind: "thread",
503+
aggregateId: threadId,
504+
occurredAt: now,
505+
commandId: CommandId.make("session-error"),
506+
causationEventId: null,
507+
correlationId: null,
508+
metadata: {},
509+
type: "thread.session-set",
510+
payload: {
511+
threadId,
512+
session: {
513+
threadId,
514+
status: "error",
515+
providerName: "claude",
516+
runtimeMode: "full-access",
517+
activeTurnId: null,
518+
lastError: "boom",
519+
updatedAt: now,
520+
},
521+
},
522+
});
523+
assert.deepStrictEqual(h.getRegistration()?.cursor, cursorFromSnapshot(initial));
524+
assert.strictEqual(h.getRegistration()?.wakeCount, 0);
525+
}),
526+
);
527+
487528
it.effect("conditional ack preserves a user-turn release", () =>
488529
Effect.gen(function* () {
489530
const initial = snapshot();

‎apps/server/src/monitor/PullRequestMonitor.ts‎

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Effect from "effect/Effect";
77
import * as Layer from "effect/Layer";
88
import * as Option from "effect/Option";
99
import * as Ref from "effect/Ref";
10+
import * as Semaphore from "effect/Semaphore";
1011
import * as Stream from "effect/Stream";
1112
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
1213

@@ -100,25 +101,55 @@ export const make = Effect.gen(function* () {
100101
return next;
101102
});
102103

104+
// Claim transitions and the registry mutations they authorize must be one
105+
// critical section: reading the claim and then mutating the cursor leaves a
106+
// window where a concurrent release rewinds and the stale ack re-advances,
107+
// losing the superseded wake's events (I5).
108+
const claimLock = yield* Semaphore.make(1);
109+
103110
// Drop a claim matching `shouldRelease`; if its wake had already advanced
104111
// the cursor, rewind to the pre-wake cursor so those events re-diff (I5).
105112
const releaseClaim = (threadId: ThreadId, shouldRelease: (claim: WakeClaim) => boolean) =>
106-
Ref.modify(claims, (current) => {
107-
const claim = current.get(threadId);
108-
if (claim === undefined || !shouldRelease(claim)) {
109-
return [undefined, current] as const;
110-
}
111-
const next = new Map(current);
112-
next.delete(threadId);
113-
return [claim, next] as const;
114-
}).pipe(
115-
Effect.flatMap((released) =>
116-
released === undefined || released.phase !== "in-flight"
117-
? Effect.void
118-
: Effect.all([
119-
registry.updateCursor(threadId, released.previousCursor, released.generation),
120-
registry.setWakeCount(threadId, released.previousWakeCount, released.generation),
121-
]).pipe(Effect.asVoid),
113+
claimLock.withPermits(1)(
114+
Ref.modify(claims, (current) => {
115+
const claim = current.get(threadId);
116+
if (claim === undefined || !shouldRelease(claim)) {
117+
return [undefined, current] as const;
118+
}
119+
const next = new Map(current);
120+
next.delete(threadId);
121+
return [claim, next] as const;
122+
}).pipe(
123+
Effect.flatMap((released) =>
124+
released === undefined || released.phase !== "in-flight"
125+
? Effect.void
126+
: Effect.all([
127+
registry.updateCursor(threadId, released.previousCursor, released.generation),
128+
registry.setWakeCount(threadId, released.previousWakeCount, released.generation),
129+
]).pipe(Effect.asVoid),
130+
),
131+
),
132+
);
133+
134+
// Acknowledge a dispatched wake: advance the cursor and count the wake only
135+
// while this wake still owns the claim. Runs under `claimLock` so a release
136+
// arriving mid-ack either fully precedes it (claim gone → no ack) or fully
137+
// follows it (rewinds both cursor and wake count).
138+
const ackWake = (
139+
threadId: ThreadId,
140+
commandId: CommandId,
141+
generation: number,
142+
nextCursor: PullRequestMonitorCursor,
143+
) =>
144+
claimLock.withPermits(1)(
145+
Ref.get(claims).pipe(
146+
Effect.flatMap((current) =>
147+
current.get(threadId)?.commandId === commandId
148+
? registry
149+
.updateCursor(threadId, nextCursor, generation)
150+
.pipe(Effect.andThen(registry.incrementWake(threadId, generation)))
151+
: Effect.succeed(0),
152+
),
122153
),
123154
);
124155

@@ -285,22 +316,21 @@ export const make = Effect.gen(function* () {
285316
: claim,
286317
),
287318
),
288-
Effect.tap(() =>
289-
Ref.get(claims).pipe(
290-
Effect.flatMap((currentClaims) =>
291-
currentClaims.get(registration.threadId)?.commandId === commandId
292-
? registry.updateCursor(registration.threadId, nextCursor, registration.generation)
293-
: Effect.void,
294-
),
295-
),
296-
),
297319
Effect.tapError(() =>
298320
mutateClaim(registration.threadId, (claim) =>
299321
claim?.commandId === commandId ? undefined : claim,
300322
),
301323
),
302324
);
303-
const wakeCount = yield* registry.incrementWake(registration.threadId, registration.generation);
325+
// Cursor ack + wake count are one atomic step with the claim check: a
326+
// release that lands between them would otherwise be undone.
327+
const wakeCount = yield* ackWake(
328+
registration.threadId,
329+
commandId,
330+
registration.generation,
331+
nextCursor,
332+
);
333+
if (wakeCount === 0) return;
304334
yield* dispatchUpdate(
305335
registration.threadId,
306336
registration.generation,
@@ -373,7 +403,11 @@ export const make = Effect.gen(function* () {
373403
const reconcile = Effect.fn("PullRequestMonitor.reconcile")(function* () {
374404
if (yield* Ref.get(reconciled)) return;
375405
const shell = yield* snapshots.getShellSnapshot();
376-
yield* Effect.forEach(
406+
// Per-thread failures are isolated so one bad orphan can't abort the
407+
// sweep, but any failure leaves `reconciled` unset so the next poll
408+
// retries the stragglers — otherwise they stay projected as monitoring
409+
// with nothing polling them, forever.
410+
const outcomes = yield* Effect.forEach(
377411
shell.threads.filter((thread) => thread.monitor?.status === "monitoring"),
378412
(thread) =>
379413
registry.get(thread.id).pipe(
@@ -392,16 +426,16 @@ export const make = Effect.gen(function* () {
392426
});
393427
});
394428
}),
429+
Effect.as(true),
395430
Effect.catchCause((cause) =>
396431
Effect.logWarning("pull request monitor orphan reconcile failed", {
397432
threadId: thread.id,
398433
cause,
399-
}),
434+
}).pipe(Effect.as(false)),
400435
),
401436
),
402-
{ discard: true },
403437
);
404-
yield* Ref.set(reconciled, true);
438+
if (outcomes.every(Boolean)) yield* Ref.set(reconciled, true);
405439
});
406440

407441
const pollOnce = Ref.set(lastPollFailed, false).pipe(
@@ -513,6 +547,14 @@ export const make = Effect.gen(function* () {
513547
}
514548
if (event.type === "thread.session-set") {
515549
const status = event.payload.session.status;
550+
// A provider failure surfaces as session-set(error) BEFORE
551+
// provider.turn.start.failed, so dropping the claim here would leave
552+
// that later handler nothing to rewind and the acked events would never
553+
// re-diff (I5). Rewind on error; a clean finish (ready/idle/stopped)
554+
// just frees the claim — its events were genuinely delivered.
555+
if (status === "error") {
556+
return releaseClaim(threadId, (claim) => claim.phase === "in-flight");
557+
}
516558
if (status !== "starting" && status !== "running") {
517559
return mutateClaim(threadId, (claim) => (claim?.phase === "in-flight" ? undefined : claim));
518560
}

0 commit comments

Comments
 (0)