11import type { RequestProgressEvent } from '@agent-device/contracts/progress' ;
2- import { test , vi } from 'vitest' ;
2+ import { test } from 'vitest' ;
33import assert from 'node:assert/strict' ;
44import http from 'node:http' ;
55import net from 'node:net' ;
@@ -11,57 +11,18 @@ import {
1111 listenOnLoopback ,
1212 supportsLoopbackBind ,
1313} from '../../__tests__/test-utils/loopback.ts' ;
14- import { runCmdBackground } from '@agent-device/host-kit/command' ;
15- import {
16- isProcessAlive ,
17- readProcessCommand ,
18- readProcessStartTime ,
19- waitForProcessExit ,
20- } from '@agent-device/host-kit/process' ;
14+ import { readProcessStartTime } from '@agent-device/host-kit/process' ;
2115import { sendToDaemon } from '../daemon-client.ts' ;
2216import { currentDaemonCodeSignature } from '../../__tests__/test-utils/daemon-http-fixture.ts' ;
2317import { computeDaemonCodeSignature } from '@agent-device/host-kit/code-signature' ;
2418import { downloadRemoteArtifact } from '../../remote/daemon-artifacts.ts' ;
25- import {
26- cleanupFailedDaemonStartupMetadata ,
27- resolveDaemonStartupHint ,
28- } from '../daemon-client-metadata.ts' ;
19+ import { resolveDaemonStartupHint } from '../daemon-client-metadata.ts' ;
2920import { canConnectSocket } from '../daemon-client-transport.ts' ;
3021import { DAEMON_RPC_PROTOCOL_VERSION } from '@agent-device/contracts/daemon-http' ;
3122import { resolveDaemonPaths } from '../../daemon-resolution.ts' ;
3223import { readVersion } from '@agent-device/host-kit/version' ;
3324import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts' ;
3425
35- // readProcessStartTime/readProcessCommand shell out to `ps` with a 1s
36- // timeout (see host-process.ts). isAgentDeviceDaemonProcess re-reads both for
37- // every liveness check, so a spawned-daemon fixture that is proven live once
38- // (a real read, right after the process starts) can still be misclassified
39- // as dead later if a *subsequent* `ps` call happens to miss its deadline
40- // under full-suite CPU contention. mockReadProcessStartTime/mockReadProcessCommand
41- // default to `undefined`, which falls through to the real implementation for
42- // every pid in every test in this file; only the one test below that needs a
43- // stable answer for its spawned pid configures an override, and clears it
44- // afterward.
45- const { mockReadProcessStartTime, mockReadProcessCommand } = vi . hoisted ( ( ) => ( {
46- mockReadProcessStartTime : vi . fn < ( pid : number ) => string | null | undefined > ( ) ,
47- mockReadProcessCommand : vi . fn < ( pid : number ) => string | null | undefined > ( ) ,
48- } ) ) ;
49-
50- vi . mock ( '@agent-device/host-kit/process' , async ( importOriginal ) => {
51- const actual = await importOriginal < typeof import ( '@agent-device/host-kit/process' ) > ( ) ;
52- return {
53- ...actual ,
54- readProcessStartTime : ( pid : number ) => {
55- const overridden = mockReadProcessStartTime ( pid ) ;
56- return overridden !== undefined ? overridden : actual . readProcessStartTime ( pid ) ;
57- } ,
58- readProcessCommand : ( pid : number ) => {
59- const overridden = mockReadProcessCommand ( pid ) ;
60- return overridden !== undefined ? overridden : actual . readProcessCommand ( pid ) ;
61- } ,
62- } ;
63- } ) ;
64-
6526type MockHttpResponse = EventEmitter & {
6627 headers ?: Record < string , string > ;
6728 statusCode ?: number ;
@@ -219,144 +180,6 @@ test('resolveDaemonStartupHint shell-quotes cleanup paths', () => {
219180 ) ;
220181} ) ;
221182
222- test ( 'cleanupFailedDaemonStartupMetadata removes partial startup metadata' , async ( ) => {
223- const stateDir = mkdtempForTestSync ( 'agent-device-daemon-cleanup-' ) ;
224- const paths = resolveDaemonPaths ( stateDir ) ;
225- try {
226- fs . mkdirSync ( paths . baseDir , { recursive : true } ) ;
227- fs . writeFileSync ( paths . infoPath , '{"invalid":true}\n' , 'utf8' ) ;
228- fs . writeFileSync ( paths . lockPath , 'not-json\n' , 'utf8' ) ;
229-
230- const result = await cleanupFailedDaemonStartupMetadata ( paths , 'startup_timeout' ) ;
231-
232- assert . deepEqual ( result , {
233- reason : 'startup_timeout' ,
234- removedInfo : true ,
235- removedLock : true ,
236- stoppedInfoProcess : false ,
237- stoppedLockProcess : false ,
238- } ) ;
239- assert . equal ( fs . existsSync ( paths . infoPath ) , false ) ;
240- assert . equal ( fs . existsSync ( paths . lockPath ) , false ) ;
241- } finally {
242- fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
243- }
244- } ) ;
245-
246- test ( 'cleanupFailedDaemonStartupMetadata retains live startup daemon on timeout' , async ( t ) => {
247- const stateDir = mkdtempForTestSync ( 'agent-device-daemon-live-cleanup-' ) ;
248- const root = mkdtempForTestSync ( 'agent-device-live-daemon-' ) ;
249- const daemonDir = path . join ( root , 'agent-device' , 'dist' , 'src' , 'internal' ) ;
250- const daemonScriptPath = path . join ( daemonDir , 'daemon.js' ) ;
251- fs . mkdirSync ( daemonDir , { recursive : true } ) ;
252- fs . writeFileSync ( daemonScriptPath , 'setInterval(() => {}, 1000);\n' , 'utf8' ) ;
253- const daemonProcess = runCmdBackground ( process . execPath , [ daemonScriptPath ] , {
254- stdio : 'ignore' ,
255- allowFailure : true ,
256- captureOutput : false ,
257- } ) ;
258- void daemonProcess . wait . catch ( ( ) => { } ) ;
259- const pid = daemonProcess . child . pid ;
260- assert . ok ( pid , 'spawned child should have a pid' ) ;
261-
262- try {
263- await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
264- // Read the spawned daemon's real identity once (ground truth: it is
265- // genuinely alive, with this real start time and command line), then
266- // pin readProcessStartTime/readProcessCommand to keep returning these
267- // same proven-real values for this pid. isAgentDeviceDaemonProcess reads
268- // both again internally on every call inside cleanupFailedDaemonStartupMetadata;
269- // without pinning, a second real `ps` call could miss its 1s timeout
270- // under load and misclassify this genuinely-live daemon as dead.
271- const processStartTime = readProcessStartTime ( pid ) ?? undefined ;
272- const command = readProcessCommand ( pid ) ;
273- if ( command === null || processStartTime === undefined ) {
274- t . skip ( 'process command/start inspection is unavailable in this environment' ) ;
275- return ;
276- }
277- mockReadProcessStartTime . mockImplementation ( ( queriedPid : number ) =>
278- queriedPid === pid ? processStartTime : undefined ,
279- ) ;
280- mockReadProcessCommand . mockImplementation ( ( queriedPid : number ) =>
281- queriedPid === pid ? command : undefined ,
282- ) ;
283-
284- const paths = resolveDaemonPaths ( stateDir ) ;
285- fs . mkdirSync ( paths . baseDir , { recursive : true } ) ;
286- fs . writeFileSync (
287- paths . infoPath ,
288- `${ JSON . stringify ( {
289- token : 'startup-secret' ,
290- port : 65530 ,
291- transport : 'socket' ,
292- pid,
293- processStartTime,
294- } ) } \n`,
295- 'utf8' ,
296- ) ;
297- fs . writeFileSync (
298- paths . lockPath ,
299- `${ JSON . stringify ( { pid, processStartTime, startedAt : Date . now ( ) } ) } \n` ,
300- 'utf8' ,
301- ) ;
302-
303- const result = await cleanupFailedDaemonStartupMetadata ( paths , 'startup_timeout' , {
304- stopLiveProcesses : false ,
305- } ) ;
306-
307- assert . equal ( result . retainedInfoProcess , true ) ;
308- assert . equal ( result . retainedLockProcess , true ) ;
309- assert . equal ( result . removedInfo , false ) ;
310- assert . equal ( result . removedLock , false ) ;
311- assert . equal ( isProcessAlive ( pid ) , true ) ;
312- assert . equal ( fs . existsSync ( paths . infoPath ) , true ) ;
313- assert . equal ( fs . existsSync ( paths . lockPath ) , true ) ;
314- } finally {
315- mockReadProcessStartTime . mockReset ( ) ;
316- mockReadProcessCommand . mockReset ( ) ;
317- if ( isProcessAlive ( pid ) ) {
318- process . kill ( pid , 'SIGKILL' ) ;
319- await waitForProcessExit ( pid , 1_500 ) ;
320- }
321- fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
322- fs . rmSync ( root , { recursive : true , force : true } ) ;
323- }
324- } ) ;
325-
326- test ( 'cleanupFailedDaemonStartupMetadata removes stale daemon metadata on timeout' , async ( ) => {
327- const stateDir = mkdtempForTestSync ( 'agent-device-daemon-stale-cleanup-' ) ;
328- const paths = resolveDaemonPaths ( stateDir ) ;
329- try {
330- fs . mkdirSync ( paths . baseDir , { recursive : true } ) ;
331- fs . writeFileSync (
332- paths . infoPath ,
333- `${ JSON . stringify ( {
334- token : 'startup-secret' ,
335- port : 65530 ,
336- transport : 'socket' ,
337- pid : 999_999 ,
338- } ) } \n`,
339- 'utf8' ,
340- ) ;
341- fs . writeFileSync (
342- paths . lockPath ,
343- `${ JSON . stringify ( { pid : 999_999 , startedAt : Date . now ( ) } ) } \n` ,
344- 'utf8' ,
345- ) ;
346-
347- const result = await cleanupFailedDaemonStartupMetadata ( paths , 'startup_timeout' , {
348- stopLiveProcesses : false ,
349- } ) ;
350-
351- assert . equal ( result . removedInfo , true ) ;
352- assert . equal ( result . removedLock , true ) ;
353- assert . equal ( fs . existsSync ( paths . infoPath ) , false ) ;
354- assert . equal ( fs . existsSync ( paths . lockPath ) , false ) ;
355- } finally {
356- fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
357- }
358- } ) ;
359-
360183test ( 'canConnectSocket times out stalled local daemon probes' , async ( ) => {
361184 const originalCreateConnection = net . createConnection ;
362185 let timeoutMs : number | undefined ;
0 commit comments