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]>
This commit is contained in:
1 parent
0c1de35f5a
commit
355a2893d4
3 files changed
+123
-31
No files matched your search
@@ -149,7 +149,16 @@ export const layer = Layer.effect(
|
||||
export const endActiveMonitorForSession = (threadId: ThreadId): Effect.Effect<void> =>
|
||||
Effect.gen(function* () {
|
||||
const registration = yield* make.get(threadId);
|
||||
if (Option.isNone(registration) || activeEngine === undefined) return;
|
||||
if (Option.isNone(registration)) return;
|
||||
if (activeEngine === undefined) {
|
||||
// The poller hasn't bound an engine yet, so the monitor end can't be
|
||||
// projected — but leaving the registration would let the poller wake a
|
||||
// thread whose session is already gone. Drop it and log the projection
|
||||
// gap; boot reconcile ends the orphaned "monitoring" projection.
|
||||
yield* make.remove(threadId, registration.value.generation);
|
||||
yield* Effect.logWarning("monitor registration dropped before engine bind", { threadId });
|
||||
return;
|
||||
}
|
||||
const endedAt = DateTime.formatIso(yield* DateTime.now);
|
||||
const commandId = CommandId.make(`monitor-session-ended:${threadId}:${endedAt}`);
|
||||
yield* activeEngine.dispatch({
|
||||
|
||||
@@ -484,6 +484,47 @@ describe("PullRequestMonitor dispatch protocol", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("session error rewinds the acked wake cursor", () =>
|
||||
Effect.gen(function* () {
|
||||
// The provider reactor emits session-set(error) BEFORE
|
||||
// provider.turn.start.failed, so this handler must rewind rather than
|
||||
// silently drop the claim — otherwise the acked events never re-diff.
|
||||
const initial = snapshot();
|
||||
const h = yield* harness({
|
||||
initial,
|
||||
current: { value: snapshot({ reviewThreads: [comment("one")] }) },
|
||||
});
|
||||
yield* h.monitor.pollOnce;
|
||||
assert.strictEqual(h.getRegistration()?.wakeCount, 1);
|
||||
yield* h.monitor.handleDomainEvent({
|
||||
sequence: 2,
|
||||
eventId: EventId.make("session-error"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: threadId,
|
||||
occurredAt: now,
|
||||
commandId: CommandId.make("session-error"),
|
||||
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,
|
||||
},
|
||||
},
|
||||
});
|
||||
assert.deepStrictEqual(h.getRegistration()?.cursor, cursorFromSnapshot(initial));
|
||||
assert.strictEqual(h.getRegistration()?.wakeCount, 0);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("conditional ack preserves a user-turn release", () =>
|
||||
Effect.gen(function* () {
|
||||
const initial = snapshot();
|
||||
|
||||
@@ -7,6 +7,7 @@ import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import * as Stream from "effect/Stream";
|
||||
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
|
||||
|
||||
@@ -100,25 +101,55 @@ export const make = Effect.gen(function* () {
|
||||
return next;
|
||||
});
|
||||
|
||||
// Claim transitions and the registry mutations they authorize must be one
|
||||
// critical section: reading the claim and then mutating the cursor leaves a
|
||||
// window where a concurrent release rewinds and the stale ack re-advances,
|
||||
// losing the superseded wake's events (I5).
|
||||
const claimLock = yield* Semaphore.make(1);
|
||||
|
||||
// Drop a claim matching `shouldRelease`; if its wake had already advanced
|
||||
// the cursor, rewind to the pre-wake cursor so those events re-diff (I5).
|
||||
const releaseClaim = (threadId: ThreadId, shouldRelease: (claim: WakeClaim) => boolean) =>
|
||||
Ref.modify(claims, (current) => {
|
||||
const claim = current.get(threadId);
|
||||
if (claim === undefined || !shouldRelease(claim)) {
|
||||
return [undefined, current] as const;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.delete(threadId);
|
||||
return [claim, next] as const;
|
||||
}).pipe(
|
||||
Effect.flatMap((released) =>
|
||||
released === undefined || released.phase !== "in-flight"
|
||||
? Effect.void
|
||||
: Effect.all([
|
||||
registry.updateCursor(threadId, released.previousCursor, released.generation),
|
||||
registry.setWakeCount(threadId, released.previousWakeCount, released.generation),
|
||||
]).pipe(Effect.asVoid),
|
||||
claimLock.withPermits(1)(
|
||||
Ref.modify(claims, (current) => {
|
||||
const claim = current.get(threadId);
|
||||
if (claim === undefined || !shouldRelease(claim)) {
|
||||
return [undefined, current] as const;
|
||||
}
|
||||
const next = new Map(current);
|
||||
next.delete(threadId);
|
||||
return [claim, next] as const;
|
||||
}).pipe(
|
||||
Effect.flatMap((released) =>
|
||||
released === undefined || released.phase !== "in-flight"
|
||||
? Effect.void
|
||||
: Effect.all([
|
||||
registry.updateCursor(threadId, released.previousCursor, released.generation),
|
||||
registry.setWakeCount(threadId, released.previousWakeCount, released.generation),
|
||||
]).pipe(Effect.asVoid),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Acknowledge a dispatched wake: advance the cursor and count the wake only
|
||||
// while this wake still owns the claim. Runs under `claimLock` so a release
|
||||
// arriving mid-ack either fully precedes it (claim gone → no ack) or fully
|
||||
// follows it (rewinds both cursor and wake count).
|
||||
const ackWake = (
|
||||
threadId: ThreadId,
|
||||
commandId: CommandId,
|
||||
generation: number,
|
||||
nextCursor: PullRequestMonitorCursor,
|
||||
) =>
|
||||
claimLock.withPermits(1)(
|
||||
Ref.get(claims).pipe(
|
||||
Effect.flatMap((current) =>
|
||||
current.get(threadId)?.commandId === commandId
|
||||
? registry
|
||||
.updateCursor(threadId, nextCursor, generation)
|
||||
.pipe(Effect.andThen(registry.incrementWake(threadId, generation)))
|
||||
: Effect.succeed(0),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -285,22 +316,21 @@ export const make = Effect.gen(function* () {
|
||||
: claim,
|
||||
),
|
||||
),
|
||||
Effect.tap(() =>
|
||||
Ref.get(claims).pipe(
|
||||
Effect.flatMap((currentClaims) =>
|
||||
currentClaims.get(registration.threadId)?.commandId === commandId
|
||||
? registry.updateCursor(registration.threadId, nextCursor, registration.generation)
|
||||
: Effect.void,
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.tapError(() =>
|
||||
mutateClaim(registration.threadId, (claim) =>
|
||||
claim?.commandId === commandId ? undefined : claim,
|
||||
),
|
||||
),
|
||||
);
|
||||
const wakeCount = yield* registry.incrementWake(registration.threadId, registration.generation);
|
||||
// Cursor ack + wake count are one atomic step with the claim check: a
|
||||
// release that lands between them would otherwise be undone.
|
||||
const wakeCount = yield* ackWake(
|
||||
registration.threadId,
|
||||
commandId,
|
||||
registration.generation,
|
||||
nextCursor,
|
||||
);
|
||||
if (wakeCount === 0) return;
|
||||
yield* dispatchUpdate(
|
||||
registration.threadId,
|
||||
registration.generation,
|
||||
@@ -373,7 +403,11 @@ export const make = Effect.gen(function* () {
|
||||
const reconcile = Effect.fn("PullRequestMonitor.reconcile")(function* () {
|
||||
if (yield* Ref.get(reconciled)) return;
|
||||
const shell = yield* snapshots.getShellSnapshot();
|
||||
yield* Effect.forEach(
|
||||
// Per-thread failures are isolated so one bad orphan can't abort the
|
||||
// sweep, but any failure leaves `reconciled` unset so the next poll
|
||||
// retries the stragglers — otherwise they stay projected as monitoring
|
||||
// with nothing polling them, forever.
|
||||
const outcomes = yield* Effect.forEach(
|
||||
shell.threads.filter((thread) => thread.monitor?.status === "monitoring"),
|
||||
(thread) =>
|
||||
registry.get(thread.id).pipe(
|
||||
@@ -392,16 +426,16 @@ export const make = Effect.gen(function* () {
|
||||
});
|
||||
});
|
||||
}),
|
||||
Effect.as(true),
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("pull request monitor orphan reconcile failed", {
|
||||
threadId: thread.id,
|
||||
cause,
|
||||
}),
|
||||
}).pipe(Effect.as(false)),
|
||||
),
|
||||
),
|
||||
{ discard: true },
|
||||
);
|
||||
yield* Ref.set(reconciled, true);
|
||||
if (outcomes.every(Boolean)) yield* Ref.set(reconciled, true);
|
||||
});
|
||||
|
||||
const pollOnce = Ref.set(lastPollFailed, false).pipe(
|
||||
@@ -513,6 +547,14 @@ export const make = Effect.gen(function* () {
|
||||
}
|
||||
if (event.type === "thread.session-set") {
|
||||
const status = event.payload.session.status;
|
||||
// A provider failure surfaces as session-set(error) BEFORE
|
||||
// provider.turn.start.failed, so dropping the claim here would leave
|
||||
// that later handler nothing to rewind and the acked events would never
|
||||
// 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");
|
||||
}
|
||||
if (status !== "starting" && status !== "running") {
|
||||
return mutateClaim(threadId, (claim) => (claim?.phase === "in-flight" ? undefined : claim));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user