@@ -1632,16 +1632,17 @@ export const layerWithOptions = (
16321632 yield * Queue . clear ( queue ) ;
16331633 yield * Queue . end ( queue ) ;
16341634 }
1635- // Completion can restore its cached receipt after an idle invalidation was persisted.
1636- if ( entry !== undefined ) {
1637- yield * Effect . forEach (
1638- entry . instructionInvalidations . values ( ) ,
1639- ( offered ) => persistInstructionInvalidation ( entry , offered ) ,
1640- { discard : true } ,
1641- ) ;
1642- }
16431635 } ) ;
16441636 yield * entry === undefined ? release : entry . requestEventPermit . withPermits ( 1 ) ( release ) ;
1637+ // Completion can restore its cached receipt after an idle invalidation was persisted.
1638+ // Retry outside the cleanup lock so release can stop a competing runtime writer.
1639+ if ( entry !== undefined ) {
1640+ yield * Effect . forEach (
1641+ [ ...entry . instructionInvalidations . values ( ) ] ,
1642+ ( offered ) => persistInstructionInvalidation ( entry , offered ) ,
1643+ { discard : true } ,
1644+ ) ;
1645+ }
16451646 } ) . pipe ( Effect . orDie ) ;
16461647 const events = Stream . fromQueue ( queue ) . pipe (
16471648 Stream . mapEffect ( ( signal ) =>
@@ -1875,6 +1876,7 @@ export const layerWithOptions = (
18751876 if ( threadId === null || offered . nativeMetadata ?. acpInstructions !== null ) return ;
18761877 // Idle updates outlive run subscribers; invalidate only the receipt, not newer thread state.
18771878 yield * Effect . gen ( function * ( ) {
1879+ if ( entry . instructionInvalidations . get ( offered . id ) !== offered ) return true ;
18781880 if ( ! entry . attachedThreadIds . has ( threadId ) ) return true ;
18791881 const projection = yield * projectionStore . getThreadRecords ( threadId , [ "providerThreads" ] ) ;
18801882 const stored = projection . providerThreads . find (
@@ -1903,7 +1905,12 @@ export const layerWithOptions = (
19031905 } ) ;
19041906 return ( yield * eventSink . writeIfProviderThreadUnchanged ( { expected : stored , events } ) )
19051907 . committed ;
1906- } ) . pipe ( Effect . repeat ( { until : ( committed ) => committed } ) ) ;
1908+ } ) . pipe (
1909+ // Each comparison owns the permit; retries must let cleanup and newer receipts through.
1910+ entry . requestEventPermit . withPermits ( 1 ) ,
1911+ Effect . tap ( ( committed ) => ( committed ? Effect . void : Effect . yieldNow ) ) ,
1912+ Effect . repeat ( { until : ( committed ) => committed } ) ,
1913+ ) ;
19071914 } ) ;
19081915
19091916 const startEventPump = ( entry : LiveSessionEntry ) => {
@@ -1970,7 +1977,7 @@ export const layerWithOptions = (
19701977 event . type === "provider_thread.updated" &&
19711978 event . providerThread . nativeMetadata ?. acpInstructions !== undefined
19721979 ) {
1973- yield * Effect . gen ( function * ( ) {
1980+ yield * Effect . sync ( ( ) => {
19741981 if ( event . providerThread . nativeMetadata ?. acpInstructions === null ) {
19751982 entry . instructionInvalidations . set (
19761983 event . providerThread . id ,
@@ -1979,18 +1986,18 @@ export const layerWithOptions = (
19791986 } else {
19801987 entry . instructionInvalidations . delete ( event . providerThread . id ) ;
19811988 }
1982- yield * persistInstructionInvalidation ( entry , event . providerThread ) . pipe (
1983- Effect . mapError (
1984- ( cause ) =>
1985- new ProviderAdapter . ProviderAdapterEventStreamError ( {
1986- driver : entry . runtime . driver ,
1987- providerSessionId : entry . runtime . providerSessionId ,
1988- cause,
1989- } ) ,
1990- ) ,
1991- ) ;
1992- yield * publishToSubscribers ( entry . eventSubscribers , { type : "event" , event } ) ;
19931989 } ) . pipe ( entry . requestEventPermit . withPermits ( 1 ) ) ;
1990+ yield * persistInstructionInvalidation ( entry , event . providerThread ) . pipe (
1991+ Effect . mapError (
1992+ ( cause ) =>
1993+ new ProviderAdapter . ProviderAdapterEventStreamError ( {
1994+ driver : entry . runtime . driver ,
1995+ providerSessionId : entry . runtime . providerSessionId ,
1996+ cause,
1997+ } ) ,
1998+ ) ,
1999+ ) ;
2000+ yield * publishToSubscribers ( entry . eventSubscribers , { type : "event" , event } ) ;
19942001 return ;
19952002 }
19962003 // Some providers can block before a run subscriber exists
0 commit comments