@@ -19,9 +19,8 @@ vi.mock('@agent-device/host-kit/retry', async (importOriginal) => ({
1919} ) ) ;
2020
2121import { resolveDaemonPaths , type DaemonPaths } from '../../daemon-resolution.ts' ;
22- import { sendToDaemon , type DaemonRequest , type DaemonResponse } from '../daemon-client.ts' ;
22+ import { sendToDaemon , type DaemonResponse } from '../daemon-client.ts' ;
2323import { attachActiveSessionAddressHint } from '../daemon-client-lifecycle.ts' ;
24- import { sendRequest } from '../daemon-client-transport.ts' ;
2524import {
2625 closeLoopbackServer ,
2726 listenOnLoopback ,
@@ -160,48 +159,6 @@ function installSpawnedHttpDaemonAtOwnedStateDir(
160159 } ) ;
161160}
162161
163- // A daemon that accepts the RPC but never answers it, and then refuses every later connection —
164- // the wedged daemon #3177's reset path exists for. The client's timeout route probes `/health`
165- // before deciding to reset (so a merely-slow daemon is preserved); a daemon that answers THAT too
166- // would correctly be kept alive and its metadata left in place, which is the opposite of what this
167- // fixture's reset-path callers assert. Refusing the probe (rather than hanging it) fails the probe
168- // fast instead of spending its full window.
169- async function startHangingHttpDaemonFixture ( ) : Promise < HttpDaemonFixture > {
170- const seenPaths : string [ ] = [ ] ;
171- const rpcRequests : Record < string , any > [ ] = [ ] ;
172- let requests = 0 ;
173- const server = http . createServer ( ( req , res ) => {
174- const url = new URL ( req . url || '/' , 'http://127.0.0.1' ) ;
175- seenPaths . push ( `${ req . method ?? 'GET' } ${ url . pathname } ` ) ;
176- requests += 1 ;
177-
178- if ( req . method === 'POST' && url . pathname === '/rpc' ) {
179- const chunks : Buffer [ ] = [ ] ;
180- req . on ( 'data' , ( chunk ) => {
181- chunks . push ( Buffer . isBuffer ( chunk ) ? chunk : Buffer . from ( chunk ) ) ;
182- } ) ;
183- req . on ( 'end' , ( ) => {
184- rpcRequests . push ( JSON . parse ( Buffer . concat ( chunks ) . toString ( 'utf8' ) ) as Record < string , any > ) ;
185- } ) ;
186- // Never respond: the client's own envelope is the only completion path.
187- return ;
188- }
189-
190- // The liveness probe's fresh request after the RPC timed out: refuse it, so the route sees an
191- // unresponsive daemon and takes its reset branch.
192- if ( requests > 1 ) {
193- res . destroy ( ) ;
194- return ;
195- }
196-
197- res . writeHead ( 404 ) ;
198- res . end ( 'not found' ) ;
199- } ) ;
200- server . on ( 'clientError' , ( _err , socket ) => socket . destroy ( ) ) ;
201- const port = await listenOnLoopback ( server ) ;
202- return { server, port, seenPaths, rpcRequests } ;
203- }
204-
205162function installSpawnedHttpDaemon ( paths : DaemonPaths , httpPort : number ) : void {
206163 mockRunCmdDetached . mockImplementation ( ( _command , _args , options ) => {
207164 assert . equal ( options ?. env ?. AGENT_DEVICE_STATE_DIR , paths . baseDir ) ;
@@ -602,82 +559,6 @@ test('sendToDaemon replaces socket-only daemon metadata when HTTP transport is r
602559 }
603560} ) ;
604561
605- test ( 'sendRequest timeout cleanup uses resolved daemon paths instead of request flags' , async ( t ) => {
606- if ( ! ( await supportsLoopbackBind ( ) ) ) {
607- t . skip ( 'loopback listeners are not permitted in this environment' ) ;
608- return ;
609- }
610-
611- const daemonStateDir = makeTempStateDir ( 'agent-device-daemon-timeout-active-' ) ;
612- const requestFlagStateDir = makeTempStateDir ( 'agent-device-daemon-timeout-request-' ) ;
613- const daemonPaths = resolveDaemonPaths ( daemonStateDir ) ;
614- const requestFlagPaths = resolveDaemonPaths ( requestFlagStateDir ) ;
615- const daemon = await startHangingHttpDaemonFixture ( ) ;
616- // The cleanup fence only clears a registration that still MATCHES the timed-out daemon on pid
617- // and start time (#3125), so this fixture names one identity on both sides: the record it
618- // publishes and the info `sendRequest` times out on.
619- const timedOutDaemonStartTime = 'timeout-daemon-start' ;
620- writeDaemonInfo ( daemonPaths , {
621- httpPort : daemon . port ,
622- transport : 'http' ,
623- pid : 999_999 ,
624- processStartTime : timedOutDaemonStartTime ,
625- } ) ;
626- writeDaemonLock ( daemonPaths , { pid : 999_999 } ) ;
627- writeDaemonInfo ( requestFlagPaths , {
628- httpPort : daemon . port ,
629- transport : 'http' ,
630- pid : 999_998 ,
631- } ) ;
632- writeDaemonLock ( requestFlagPaths , { pid : 999_998 } ) ;
633-
634- const request : DaemonRequest = {
635- session : 'default' ,
636- command : 'replay' ,
637- positionals : [ ] ,
638- flags : { stateDir : requestFlagStateDir , daemonTransport : 'http' } ,
639- token : 'local-secret' ,
640- meta : { requestId : 'req-timeout-paths' } ,
641- } ;
642-
643- try {
644- let thrown : unknown ;
645- try {
646- await sendRequest (
647- {
648- token : 'local-secret' ,
649- pid : 999_999 ,
650- httpPort : daemon . port ,
651- transport : 'http' ,
652- processStartTime : timedOutDaemonStartTime ,
653- } ,
654- request ,
655- 'http' ,
656- daemonPaths ,
657- 50 ,
658- ) ;
659- } catch ( error ) {
660- thrown = error ;
661- }
662-
663- assert . ok ( thrown instanceof AppError ) ;
664- assert . equal ( thrown . message , 'Daemon request timed out' ) ;
665- // The RPC first, then the liveness probe the timeout route asks before it is allowed to
666- // reset. The fixture refused that probe, which is what authorized the reset below.
667- assert . deepEqual ( daemon . seenPaths , [ 'POST /rpc' , 'GET /health' ] ) ;
668- assert . equal ( fs . existsSync ( daemonPaths . infoPath ) , false ) ;
669- // The reset owns its registration, not the protocol lock: reclaim belongs to the acquirer
670- // under ADR 0030's mutation guard, so neither state dir's lock is ever swept.
671- assert . equal ( fs . existsSync ( daemonPaths . lockPath ) , true ) ;
672- assert . equal ( fs . existsSync ( requestFlagPaths . infoPath ) , true ) ;
673- assert . equal ( fs . existsSync ( requestFlagPaths . lockPath ) , true ) ;
674- } finally {
675- await closeLoopbackServer ( daemon . server ) ;
676- fs . rmSync ( daemonStateDir , { recursive : true , force : true } ) ;
677- fs . rmSync ( requestFlagStateDir , { recursive : true , force : true } ) ;
678- }
679- } ) ;
680-
681562test ( 'sendToDaemon falls back from failed socket transport to HTTP using daemon metadata ports' , async ( t ) => {
682563 if ( ! ( await supportsLoopbackBind ( ) ) ) {
683564 t . skip ( 'loopback listeners are not permitted in this environment' ) ;
0 commit comments