@@ -322,6 +322,7 @@ interface ClaudeSessionContext {
322322 lastThreadStartedId : string | undefined ;
323323 resumeHandshakePending : boolean ;
324324 resumeUsageRefreshNeeded : boolean ;
325+ stopInProgress : boolean ;
325326 stopped : boolean ;
326327}
327328
@@ -2377,7 +2378,6 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
23772378 context . resumeHandshakePending = false ;
23782379 }
23792380 const resultContextWindow = maxClaudeContextWindowFromModelUsage ( result ?. modelUsage ) ;
2380- const maxTokens = resultContextWindow ?? context . lastKnownContextWindow ;
23812381 const reportedTotalProcessedTokens = claudeTotalProcessedTokens ( result ?. usage ) ;
23822382 const accumulatedTotalProcessedTokens =
23832383 reportedTotalProcessedTokens !== undefined
@@ -2395,6 +2395,10 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
23952395 accumulatedTotalProcessedTokens ?? context . lastKnownTotalProcessedTokens ,
23962396 modelRevision ,
23972397 ) ;
2398+ const maxTokens =
2399+ contextUsageObservation ?. contextWindow ??
2400+ resultContextWindow ??
2401+ context . lastKnownContextWindow ;
23982402 const resultUsageRecord =
23992403 result ?. usage && typeof result . usage === "object" && ! Array . isArray ( result . usage )
24002404 ? ( result . usage as Record < string , unknown > )
@@ -2449,10 +2453,13 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
24492453 : { } ) ,
24502454 }
24512455 : undefined ) ;
2452- const usageObservation : ClaudeTokenUsageObservation = contextUsageObservation ?? {
2453- modelRevision,
2456+ const observationContextWindow = contextUsageObservation ?. contextWindow ?? resultContextWindow ;
2457+ const usageObservation : ClaudeTokenUsageObservation = {
2458+ modelRevision : contextUsageObservation ?. modelRevision ?? modelRevision ,
24542459 usage : usageSnapshot ,
2455- ...( resultContextWindow !== undefined ? { contextWindow : resultContextWindow } : { } ) ,
2460+ ...( observationContextWindow !== undefined
2461+ ? { contextWindow : observationContextWindow }
2462+ : { } ) ,
24562463 } ;
24572464
24582465 const turnState = resumeHandshake ? undefined : context . turnState ;
@@ -2464,7 +2471,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
24642471 } ) ;
24652472 if (
24662473 resumeHandshake &&
2467- contextUsageObservation !== undefined &&
2474+ contextUsageObservation ?. usage !== undefined &&
24682475 usageAccepted &&
24692476 ! context . modelTransitionInFlight &&
24702477 context . modelRevision === usageObservation . modelRevision
@@ -3843,40 +3850,47 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
38433850 readonly terminalOnCloseFailure ?: boolean ;
38443851 } ,
38453852 ) {
3846- if ( context . stopped ) return ;
3853+ if ( context . stopped || context . stopInProgress ) return ;
38473854
38483855 // Schedule process termination before any cleanup that can wait on the
38493856 // provider. The SDK closes stdin, then escalates from SIGTERM to SIGKILL.
38503857 const terminalOnCloseFailure = options ?. terminalOnCloseFailure === true ;
3851- const closeResult = yield * Effect . uninterruptible (
3858+ const closeError = yield * Effect . uninterruptible (
38523859 Effect . gen ( function * ( ) {
3853- context . stopped = true ;
38543860 if ( terminalOnCloseFailure ) {
3861+ context . stopped = true ;
38553862 yield * Deferred . succeed ( context . stopSignal , undefined ) ;
3863+ } else {
3864+ context . stopInProgress = true ;
38563865 }
3857- const result = yield * Effect . try ( {
3858- try : ( ) => context . query . close ( ) ,
3859- catch : ( cause ) =>
3860- new ProviderAdapterProcessError ( {
3866+ const error = yield * Effect . sync ( ( ) => {
3867+ try {
3868+ context . query . close ( ) ;
3869+ context . stopped = true ;
3870+ return undefined ;
3871+ } catch ( cause ) {
3872+ return new ProviderAdapterProcessError ( {
38613873 provider : PROVIDER ,
38623874 threadId : context . session . threadId ,
38633875 detail : "Failed to close Claude runtime query." ,
38643876 cause,
3865- } ) ,
3866- } ) . pipe ( Effect . result ) ;
3867- if ( result . _tag === "Success" && ! terminalOnCloseFailure ) {
3877+ } ) ;
3878+ } finally {
3879+ context . stopInProgress = false ;
3880+ }
3881+ } ) ;
3882+ if ( ! error && ! terminalOnCloseFailure ) {
38683883 yield * Deferred . succeed ( context . stopSignal , undefined ) ;
38693884 }
3870- return result ;
3885+ return error ;
38713886 } ) ,
38723887 ) ;
3873- if ( closeResult . _tag === "Failure" ) {
3888+ if ( closeError ) {
38743889 if ( ! terminalOnCloseFailure ) {
3875- context . stopped = false ;
3876- return yield * closeResult . failure ;
3890+ return yield * closeError ;
38773891 }
38783892 yield * Effect . logError ( "Failed to close interrupted Claude model control." , {
3879- cause : closeResult . failure ,
3893+ cause : closeError ,
38803894 } ) ;
38813895 }
38823896
@@ -3980,7 +3994,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
39803994 } ) ,
39813995 ) ;
39823996 }
3983- if ( context . stopped || context . session . status === "closed" ) {
3997+ if ( context . stopInProgress || context . stopped || context . session . status === "closed" ) {
39843998 return Effect . fail (
39853999 new ProviderAdapterSessionClosedError ( {
39864000 provider : PROVIDER ,
@@ -3995,7 +4009,9 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
39954009 context : ClaudeSessionContext ,
39964010 ) : Effect . Effect < void , ProviderAdapterSessionClosedError > =>
39974011 Effect . suspend ( ( ) =>
3998- context . stopped || sessions . get ( context . session . threadId ) !== context
4012+ context . stopInProgress ||
4013+ context . stopped ||
4014+ sessions . get ( context . session . threadId ) !== context
39994015 ? Effect . fail (
40004016 new ProviderAdapterSessionClosedError ( {
40014017 provider : PROVIDER ,
@@ -4654,6 +4670,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
46544670 lastThreadStartedId : undefined ,
46554671 resumeHandshakePending : existingResumeSessionId !== undefined ,
46564672 resumeUsageRefreshNeeded : existingResumeSessionId !== undefined ,
4673+ stopInProgress : false ,
46574674 stopped : false ,
46584675 } ;
46594676 yield * Ref . set ( contextRef , context ) ;
0 commit comments