@@ -7,6 +7,7 @@ import * as Effect from "effect/Effect";
77import * as Layer from "effect/Layer" ;
88import * as Option from "effect/Option" ;
99import * as Ref from "effect/Ref" ;
10+ import * as Semaphore from "effect/Semaphore" ;
1011import * as Stream from "effect/Stream" ;
1112import * 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