@@ -21,6 +21,8 @@ import {
2121 type EffortLevel ,
2222 type ForkSessionOptions ,
2323 type ForkSessionResult ,
24+ getSessionInfo ,
25+ getSessionMessages ,
2426 getSubagentMessages ,
2527 query ,
2628 type Options as ClaudeQueryOptions ,
@@ -464,6 +466,10 @@ export interface ClaudeAgentSdkQueryRunnerShape {
464466 readonly subagentLaunchToolUseId : (
465467 input : ClaudeAgentSdkSubagentLookupInput ,
466468 ) => Effect . Effect < string | null , ClaudeAgentSdkQueryRunnerError > ;
469+ /** Whether the CLI's session storage holds a transcript with no message to resume. */
470+ readonly sessionIsEmpty ?: (
471+ input : ClaudeAgentSdkSessionLookupInput ,
472+ ) => Effect . Effect < boolean , ClaudeAgentSdkQueryRunnerError > ;
467473 readonly assertComplete : Effect . Effect < void , ClaudeAgentSdkQueryRunnerError > ;
468474}
469475
@@ -479,6 +485,12 @@ export interface ClaudeAgentSdkSessionForkInput {
479485 readonly providerSessionId : OrchestrationV2ProviderSession [ "id" ] ;
480486}
481487
488+ export interface ClaudeAgentSdkSessionLookupInput {
489+ readonly sessionId : string ;
490+ /** The instance's `CLAUDE_CONFIG_DIR`, where the CLI keeps its sessions. */
491+ readonly configDir : string | undefined ;
492+ }
493+
482494export interface ClaudeAgentSdkSubagentLookupInput {
483495 readonly sessionId : string ;
484496 readonly agentId : string ;
@@ -713,6 +725,7 @@ export const layerQueryRunner: Layer.Layer<
713725 const { native : nativeEventLogger } = yield * ProviderEventLoggers . ProviderEventLoggers ;
714726 const agentScope = yield * AgentScope ;
715727 const platform = yield * HostProcess . Platform ;
728+ const hostEnvironment = yield * HostProcess . Environment ;
716729
717730 return ClaudeAgentSdkQueryRunner . of ( {
718731 allocateSessionId : crypto
@@ -1019,6 +1032,26 @@ export const layerQueryRunner: Layer.Layer<
10191032 return toolUseId ;
10201033 } ,
10211034 ) ,
1035+ sessionIsEmpty : Effect . fn ( "ClaudeAgentSdkQueryRunner.sessionIsEmpty" ) ( function * (
1036+ input : ClaudeAgentSdkSessionLookupInput ,
1037+ ) {
1038+ // The SDK reads sessions from this process's config dir only.
1039+ if ( input . configDir !== hostEnvironment . CLAUDE_CONFIG_DIR ) {
1040+ return false ;
1041+ }
1042+ const info = yield * Effect . tryPromise ( {
1043+ try : ( ) => getSessionInfo ( input . sessionId ) ,
1044+ catch : ( cause ) => queryRunnerError ( cause , "getSessionInfo" ) ,
1045+ } ) ;
1046+ if ( info === undefined ) {
1047+ return false ;
1048+ }
1049+ const messages = yield * Effect . tryPromise ( {
1050+ try : ( ) => getSessionMessages ( input . sessionId , { limit : 1 } ) ,
1051+ catch : ( cause ) => queryRunnerError ( cause , "getSessionMessages" ) ,
1052+ } ) ;
1053+ return messages . length === 0 ;
1054+ } ) ,
10221055 assertComplete : Effect . void ,
10231056 } ) ;
10241057 } ) ,
@@ -9182,6 +9215,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91829215 ) ,
91839216 resumeThread : Effect . fn ( "ClaudeAdapterV2.resumeThread" ) (
91849217 function * ( threadInput : { readonly providerThread : OrchestrationV2ProviderThread } ) {
9218+ // A CLI stopped before it saved the first prompt can leave a
9219+ // transcript with no messages. Claude can neither resume that id
9220+ // nor create it again, so fail and let the caller bind a fresh
9221+ // native session with a context handoff.
9222+ const nativeThreadId = threadInput . providerThread . nativeThreadRef ?. nativeId ;
9223+ if (
9224+ nativeThreadId !== undefined &&
9225+ nativeThreadId !== null &&
9226+ queryRunner . sessionIsEmpty !== undefined &&
9227+ ( yield * Ref . get ( queryContext ) ) ?. nativeThreadId !== nativeThreadId &&
9228+ ! ( yield * Ref . get ( openedNativeThreads ) ) . has ( nativeThreadId ) &&
9229+ // An unreadable store leaves the resume to the CLI, as before.
9230+ ( yield * queryRunner
9231+ . sessionIsEmpty ( {
9232+ sessionId : nativeThreadId ,
9233+ configDir : adapterOptions . environment . CLAUDE_CONFIG_DIR ,
9234+ } )
9235+ . pipe ( Effect . orElseSucceed ( ( ) => false ) ) )
9236+ ) {
9237+ return yield * new ProviderAdapter . ProviderAdapterProtocolError ( {
9238+ driver : CLAUDE_PROVIDER ,
9239+ detail : `Claude session ${ nativeThreadId } has no conversation to resume.` ,
9240+ } ) ;
9241+ }
91859242 const updatedAt = yield * DateTime . now ;
91869243 return {
91879244 ...threadInput . providerThread ,
0 commit comments