@@ -49,6 +49,8 @@ interface Adapter {
4949 /** The adapter process, so a case can say what the resource still observes. */
5050 readonly child : ChildProcess ;
5151 readonly log : string ;
52+ readonly closed : Operation < { code : number | null ; signal : NodeJS . Signals | null } > ;
53+ readonly replies : Array < Record < string , unknown > > ;
5254 request ( method : string , params : unknown ) : Operation < Record < string , unknown > > ;
5355 /**
5456 * Every `session/update` notification the adapter sent, in arrival order.
@@ -75,15 +77,28 @@ function useAdapter(provider: string, environment: Record<string, string>): Oper
7577 const entry = adapters . executablePath ( provider ) ;
7678
7779 let child : ChildProcess | undefined ;
78- const closed = withResolvers < void > ( ) ;
79- const onClose = ( ) : void => closed . resolve ( ) ;
80+ const closed = withResolvers < { code : number | null ; signal : NodeJS . Signals | null } > ( ) ;
81+ let outcome : { code : number | null ; signal : NodeJS . Signals | null } | undefined ;
82+ let onStdout : ( ( chunk : Buffer ) => void ) | undefined ;
83+ const onClose = ( code : number | null , signal : NodeJS . Signals | null ) : void => {
84+ outcome = { code, signal } ;
85+ closed . resolve ( outcome ) ;
86+ } ;
8087 yield * ensure ( function * ( ) {
8188 if ( child !== undefined ) {
8289 try {
83- child . stdin ?. end ( ) ;
84- child . kill ( ) ;
90+ if ( outcome === undefined ) {
91+ child . stdin ?. end ( ) ;
92+ // Claude disposes pending Queries on EOF; SIGTERM can preempt that shutdown.
93+ if ( provider !== "claude" ) {
94+ child . kill ( ) ;
95+ }
96+ }
8597 yield * closed . operation ;
8698 } finally {
99+ if ( onStdout !== undefined ) {
100+ child . stdout ?. off ( "data" , onStdout ) ;
101+ }
87102 child . off ( "close" , onClose ) ;
88103 }
89104 }
@@ -99,10 +114,11 @@ function useAdapter(provider: string, environment: Record<string, string>): Oper
99114
100115 const pending = new Map < number , ( message : Record < string , unknown > ) => void > ( ) ;
101116 const updates : Array < Record < string , unknown > > = [ ] ;
117+ const replies : Array < Record < string , unknown > > = [ ] ;
102118 let next = 1 ;
103119 let buffer = "" ;
104120
105- const onStdout = ( chunk : Buffer ) : void => {
121+ onStdout = ( chunk : Buffer ) : void => {
106122 buffer += chunk . toString ( "utf8" ) ;
107123 let index = buffer . indexOf ( "\n" ) ;
108124 while ( index >= 0 ) {
@@ -119,6 +135,9 @@ function useAdapter(provider: string, environment: Record<string, string>): Oper
119135 continue ;
120136 }
121137 const id = message [ "id" ] ;
138+ if ( typeof id === "number" && message [ "method" ] === undefined ) {
139+ replies . push ( message ) ;
140+ }
122141 if ( typeof id === "number" && pending . has ( id ) ) {
123142 pending . get ( id ) ?.( message ) ;
124143 pending . delete ( id ) ;
@@ -139,19 +158,13 @@ function useAdapter(provider: string, environment: Record<string, string>): Oper
139158 }
140159 } ;
141160
142- // Detached before the kill above, because destructors unwind in reverse:
143- // the adapter stops being read before the process it is reading is ended.
144- // Established before the subscription, because `yield* ensure(...)` is
145- // itself a suspension an owner can be halted at.
146- yield * ensure ( ( ) => {
147- processChild . stdout ?. off ( "data" , onStdout ) ;
148- } ) ;
149-
150- processChild . stdout ?. on ( "data" , onStdout ) ;
161+ child . stdout ?. on ( "data" , onStdout ) ;
151162
152163 yield * provide ( {
153164 child : processChild ,
154165 log,
166+ closed : closed . operation ,
167+ replies,
155168 updates,
156169 * request ( method : string , params : unknown ) : Operation < Record < string , unknown > > {
157170 const id = next ++ ;
@@ -512,23 +525,23 @@ describe("Tier EA — the embedded adapters' prompt-response metadata", () => {
512525 }
513526
514527 it ( "HC Claude cancellation joins the held startup Query with no late response" , function * ( ) {
515- let child : ChildProcess | undefined ;
528+ let adapter : Adapter | undefined ;
516529 let answered = false ;
517530 yield * scoped ( function * ( ) {
518- const adapter = yield * useAdapter ( "claude" , {
531+ const live = yield * useAdapter ( "claude" , {
519532 ...CLAUDE_ENV ,
520533 FAKE_CLAUDE_CONFIRMATION : "held" ,
521534 } ) ;
522- child = adapter . child ;
523- yield * initialize ( adapter ) ;
535+ adapter = live ;
536+ yield * initialize ( live ) ;
524537 const opening = yield * effectionSpawn ( function * ( ) {
525- yield * openSession ( adapter ) ;
538+ yield * openSession ( live ) ;
526539 answered = true ;
527540 } ) ;
528541 let started = false ;
529542 while ( ! started ) {
530543 try {
531- started = ( yield * readTextFile ( adapter . log ) ) . includes ( '"control_request"' ) ;
544+ started = ( yield * readTextFile ( live . log ) ) . includes ( '"control_request"' ) ;
532545 } catch {
533546 /* The CLI has not created its log yet. */
534547 }
@@ -537,12 +550,60 @@ describe("Tier EA — the embedded adapters' prompt-response metadata", () => {
537550 }
538551 }
539552 yield * opening . halt ( ) ;
540- expect ( acceptances ( adapter ) ) . toEqual ( [ ] ) ;
553+ expect ( acceptances ( live ) ) . toEqual ( [ ] ) ;
541554 } ) ;
542- expect ( child ?. exitCode ) . toBe ( 0 ) ;
555+ if ( adapter === undefined ) {
556+ throw new Error ( "the adapter never started" ) ;
557+ }
558+ expect ( yield * adapter . closed ) . toEqual ( { code : 0 , signal : null } ) ;
559+ expect ( acceptances ( adapter ) ) . toEqual ( [ ] ) ;
560+ expect (
561+ adapter . replies . some ( ( reply ) => {
562+ const result = reply [ "result" ] ;
563+ return (
564+ typeof result === "object" &&
565+ result !== null &&
566+ typeof Reflect . get ( result , "sessionId" ) === "string"
567+ ) ;
568+ } ) ,
569+ ) . toBe ( false ) ;
543570 expect ( answered ) . toBe ( false ) ;
544571 } ) ;
545572
573+ it ( "HC Claude startup disposal joins the backend and its held helper before returning" , function * ( ) {
574+ const root = yield * until ( mkdtemp ( join ( tmpdir ( ) , "xmd disposal " ) ) ) ;
575+ yield * ensure ( ( ) => rm ( root , { recursive : true , force : true } ) ) ;
576+ const adapters = createEmbeddedAdapters ( join ( root , "adapters" ) ) ;
577+ yield * adapters . materialize ( "claude" ) ;
578+ const done = withResolvers < { error : string | undefined ; stdout : string ; stderr : string } > ( ) ;
579+ let observer : ChildProcess | undefined ;
580+ yield * ensure ( function * ( ) {
581+ if ( observer ) {
582+ observer . kill ( ) ;
583+ yield * done . operation ;
584+ }
585+ } ) ;
586+ observer = execFile (
587+ "node" ,
588+ [ join ( FIXTURES , "claude-startup-disposal.mjs" ) , adapters . executablePath ( "claude" ) , root ] ,
589+ {
590+ env : { ...process . env , CLAUDE_CODE_EXECUTABLE : FAKE_CLAUDE } ,
591+ } ,
592+ ( error , stdout , stderr ) => done . resolve ( { error : error ?. message , stdout, stderr } ) ,
593+ ) ;
594+ const result = yield * done . operation ;
595+ expect ( result . error ) . toBeUndefined ( ) ;
596+ const observation = JSON . parse ( result . stdout ) ;
597+ expect ( observation . events ) . toEqual ( [ "dispose-start" , "backend-close" , "dispose-return" ] ) ;
598+ expect ( observation . closedWhenDisposeReturned ) . toBe ( true ) ;
599+ expect ( observation . outcome ) . toEqual ( { code : 0 , signal : null } ) ;
600+ expect ( observation . helperClosed ) . toBe ( true ) ;
601+ expect ( observation . result . status ) . toBe ( "refused" ) ;
602+ expect ( observation . updates ) . toEqual ( [ ] ) ;
603+ expect ( observation . userMessages ) . toBe ( 0 ) ;
604+ expect ( observation . remainingSessions ) . toBe ( 0 ) ;
605+ } ) ;
606+
546607 it ( "HC Claude preserves settings and runs only a fixed internal hook with its Node" , function * ( ) {
547608 const adapter = yield * useAdapter ( "claude" , CLAUDE_ENV ) ;
548609 yield * initialize ( adapter ) ;
0 commit comments