@@ -5031,6 +5031,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
50315031 }
50325032 } ) ;
50335033 const requestedContinuations = yield * Ref . make ( new Set < string > ( ) ) ;
5034+ const absorbedWakes = yield * Ref . make ( new Set < string > ( ) ) ;
50345035 const heldProposedPlansByToolUseId = new Map < string , string > ( ) ;
50355036 const runtimeContext = yield * Effect . context < never > ( ) ;
50365037 const runPromise = Effect . runPromiseWith ( runtimeContext ) ;
@@ -5466,6 +5467,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
54665467 yield * endClaudeMonitorTasks ( ( _taskId , task ) => task . nativeThreadId === nativeThreadId ) ;
54675468 } ) ;
54685469
5470+ const forgetAbsorbedWake = ( nativeThreadId : string ) =>
5471+ Ref . update ( absorbedWakes , ( current ) => {
5472+ if ( ! current . has ( nativeThreadId ) ) return current ;
5473+ const updated = new Set ( current ) ;
5474+ updated . delete ( nativeThreadId ) ;
5475+ return updated ;
5476+ } ) ;
5477+
54695478 const clearWakeStateForNativeThread =
54705479 /* Provider events include streaming deltas; avoid a span for each update. */
54715480 Effect . fnUntraced ( function * ( nativeThreadId : string ) {
@@ -5486,8 +5495,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
54865495 updated . delete ( nativeThreadId ) ;
54875496 return updated ;
54885497 } ) ;
5498+ yield * forgetAbsorbedWake ( nativeThreadId ) ;
54895499 } ) ;
54905500
5501+ // Claude runs one root turn at a time, so a user turn's result also ends a wake turn it
5502+ // folded in. The continuation replays that wake's buffered frames but gets no result of its own.
5503+ const markAbsorbedWake = Effect . fnUntraced ( function * ( nativeThreadId : string ) {
5504+ const messages = ( yield * Ref . get ( wakeBuffers ) ) . get ( nativeThreadId ) ?. messages ?? [ ] ;
5505+ const lastResult = messages . findLastIndex ( ( entry ) => entry . type === "result" ) ;
5506+ if ( ! messages . slice ( lastResult + 1 ) . some ( isClaudeRootTurnFrame ) ) return ;
5507+ yield * Ref . update ( absorbedWakes , ( current ) => new Set ( current ) . add ( nativeThreadId ) ) ;
5508+ } ) ;
5509+
54915510 // The SDK emits no initial roster; reset roster and wake eligibility when opening a live query.
54925511 const resetBackgroundTaskStateForNativeThreadProcess = Effect . fnUntraced ( function * (
54935512 nativeThreadId : string ,
@@ -8070,6 +8089,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
80708089 if ( message . type === "result" ) {
80718090 bufferedResultArrivals . set ( message , yield * DateTime . now ) ;
80728091 }
8092+ if ( isClaudeRootTurnFrame ( message ) ) {
8093+ yield * forgetAbsorbedWake ( wakeInput . nativeThreadId ) ;
8094+ }
80738095 yield * Ref . update ( wakeBuffers , ( current ) => {
80748096 const existing = current . get ( wakeInput . nativeThreadId ) ;
80758097 const updated = new Map ( current ) ;
@@ -9496,6 +9518,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
94969518 next . delete ( context . providerTurnId ) ;
94979519 return next ;
94989520 } ) ;
9521+ if ( ! isClaudeProviderContinuationTurn ( context . input ) ) {
9522+ yield * markAbsorbedWake ( liveQuery . nativeThreadId ) ;
9523+ }
94999524 const usageLimited =
95009525 context . authenticationFailureMessage === undefined &&
95019526 ( context . rejectedRateLimitTypes . size > 0 ||
@@ -10793,6 +10818,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1079310818 updated . delete ( nativeThreadId ) ;
1079410819 return updated ;
1079510820 } ) ;
10821+ const wakeAbsorbed = ( yield * Ref . get ( absorbedWakes ) ) . has ( nativeThreadId ) ;
10822+ yield * forgetAbsorbedWake ( nativeThreadId ) ;
1079610823 if ( drained . length === 0 ) {
1079710824 const completedAt = yield * DateTime . now ;
1079810825 yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
@@ -10861,7 +10888,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1086110888 yield * handleSdkMessage ( { query : querySession . query , message : lastResult } ) ;
1086210889 return ;
1086310890 }
10864- if ( hasOpaqueTaskNotification && ! context . nativeWakeStarted ) {
10891+ if ( wakeAbsorbed || ( hasOpaqueTaskNotification && ! context . nativeWakeStarted ) ) {
1086510892 const completedAt = yield * DateTime . now ;
1086610893 yield * finalizeActiveTurn ( { context, status : "completed" , completedAt } ) ;
1086710894 } else if ( ! context . nativeWakeStarted ) {
0 commit comments