@@ -2,7 +2,10 @@ import assert from 'node:assert/strict';
22import { afterEach , test , vi } from 'vitest' ;
33import { closeLoopbackServer , supportsLoopbackBind } from '@agent-device/test-kit' ;
44import { AppError } from '@agent-device/kernel/errors' ;
5- import { isProcessAlive } from '@agent-device/host-kit/process' ;
5+ import {
6+ isProcessAlive ,
7+ readHostProcessIdentityObservations ,
8+ } from '@agent-device/host-kit/process' ;
69import { finishRegisteredDaemonFixtures } from '../../__tests__/test-utils/registered-daemon-fixture.ts' ;
710import {
811 draining ,
@@ -23,6 +26,7 @@ vi.mock('@agent-device/host-kit/process', async (importOriginal) => {
2326 const actual = await importOriginal < typeof import ( '@agent-device/host-kit/process' ) > ( ) ;
2427 return {
2528 ...actual ,
29+ readHostProcessIdentityObservations : vi . fn ( actual . readHostProcessIdentityObservations ) ,
2630 readProcessCommand : ( pid : number ) =>
2731 hostedByAnotherProcess ? 'node /opt/app/server.js' : actual . readProcessCommand ( pid ) ,
2832 } ;
@@ -43,6 +47,9 @@ const spawn = vi.mocked(runCmdDetachedMonitored);
4347const actualRetry = await vi . importActual < typeof import ( '@agent-device/host-kit/retry' ) > (
4448 '@agent-device/host-kit/retry' ,
4549) ;
50+ const actualProcess = await vi . importActual < typeof import ( '@agent-device/host-kit/process' ) > (
51+ '@agent-device/host-kit/process' ,
52+ ) ;
4653const realNow = Date . now . bind ( Date ) ;
4754const actualKill = process . kill . bind ( process ) ;
4855
@@ -56,6 +63,9 @@ afterEach(async () => {
5663 vi . restoreAllMocks ( ) ;
5764 spawn . mockReset ( ) ;
5865 vi . mocked ( sleep ) . mockReset ( ) ;
66+ vi . mocked ( readHostProcessIdentityObservations ) . mockImplementation (
67+ actualProcess . readHostProcessIdentityObservations ,
68+ ) ;
5969 await finishRegisteredDaemonFixtures ( ) ;
6070} ) ;
6171
@@ -75,6 +85,14 @@ type VirtualDrain = {
7585 */
7686function installVirtualDrain ( pid : ( ) => number ) : VirtualDrain {
7787 let virtual = false ;
88+ // The fixture holds this child lifetime until it exits or is signaled; later probes use the host.
89+ let drainIdentity : ReturnType < typeof readHostProcessIdentityObservations > | undefined ;
90+ vi . mocked ( readHostProcessIdentityObservations ) . mockImplementation ( ( pids ) => {
91+ const selected = [ ...pids ] ;
92+ if ( virtual && drainIdentity && selected . length === 1 && selected [ 0 ] === pid ( ) )
93+ return drainIdentity ;
94+ return actualProcess . readHostProcessIdentityObservations ( selected ) ;
95+ } ) ;
7896 let now = 0 ;
7997 let startedAt = 0 ;
8098 let leavesAt : number | undefined ;
@@ -103,6 +121,7 @@ function installVirtualDrain(pid: () => number): VirtualDrain {
103121 return {
104122 elapsedMs : ( ) => now - startedAt ,
105123 begin : ( leavesAfterMs ) => {
124+ drainIdentity = actualProcess . readHostProcessIdentityObservations ( [ pid ( ) ] ) ;
106125 now = realNow ( ) ;
107126 startedAt = now ;
108127 leavesAt = leavesAfterMs === undefined ? undefined : now + leavesAfterMs ;
0 commit comments