@@ -5,41 +5,6 @@ import { afterEach, beforeEach, test, vi } from 'vitest';
55import { AppError , normalizeError } from '@agent-device/kernel/errors' ;
66import * as diagnostics from './diagnostics.ts' ;
77
8- const { zombiePids, processProbe } = vi . hoisted ( ( ) => ( {
9- zombiePids : new Set < number > ( ) ,
10- processProbe : { observe : undefined as ( ( ) => void ) | undefined } ,
11- } ) ) ;
12-
13- vi . mock ( './host-process.ts' , async ( importOriginal ) => {
14- const actual = await importOriginal < typeof import ( './host-process.ts' ) > ( ) ;
15- return {
16- ...actual ,
17- isProcessZombie : ( pid : number ) => {
18- processProbe . observe ?.( ) ;
19- return zombiePids . has ( pid ) ;
20- } ,
21- readProcessStartTime : ( pid : number ) => {
22- processProbe . observe ?.( ) ;
23- return actual . readProcessStartTime ( pid ) ;
24- } ,
25- // Classification judges from one snapshot; the fixture answers it from the
26- // same zombie state and real start times as the per-field probes.
27- readHostProcessIdentityObservations : ( pids : Iterable < number > ) => {
28- const observations = new Map < number , { state : string ; startTime : string } > ( ) ;
29- for ( const pid of pids ) {
30- processProbe . observe ?.( ) ;
31- const startTime = actual . readProcessStartTime ( pid ) ;
32- if ( ! zombiePids . has ( pid ) && startTime === null ) continue ;
33- observations . set ( pid , {
34- state : zombiePids . has ( pid ) ? 'ZN' : 'Ss' ,
35- startTime : startTime ?? 'zombie-snapshot' ,
36- } ) ;
37- }
38- return observations ;
39- } ,
40- } ;
41- } ) ;
42-
438import {
449 acquireProcessLock ,
4510 acquireProcessLockAcquisition ,
@@ -69,7 +34,6 @@ beforeEach(() => {
6934} ) ;
7035
7136afterEach ( ( ) => {
72- processProbe . observe = undefined ;
7337 fs . rmSync ( tmpDir , { recursive : true , force : true } ) ;
7438} ) ;
7539
@@ -102,55 +66,6 @@ test('acquireProcessLock reclaims locks owned by dead processes', async () => {
10266 assert . equal ( fs . existsSync ( lockDirPath ) , false ) ;
10367} ) ;
10468
105- test ( 'acquireProcessLock reclaims locks owned by zombie processes' , async ( ) => {
106- const lockDirPath = path . join ( tmpDir , 'zombie.lock' ) ;
107- fs . mkdirSync ( lockDirPath ) ;
108- // The owner passes kill(pid, 0) and matches its recorded start time; only
109- // the zombie state reveals it already terminated.
110- fs . writeFileSync ( path . join ( lockDirPath , 'owner.json' ) , JSON . stringify ( currentProcessOwner ( ) ) ) ;
111- zombiePids . add ( process . pid ) ;
112-
113- try {
114- const release = await acquireProcessLock ( {
115- lockDirPath,
116- owner : currentProcessOwner ( ) ,
117- timeoutMs : 3_000 ,
118- pollMs : 1 ,
119- } ) ;
120- await release ( ) ;
121- assert . equal ( fs . existsSync ( lockDirPath ) , false ) ;
122- } finally {
123- zombiePids . delete ( process . pid ) ;
124- }
125- } ) ;
126-
127- test ( 'acquireProcessLock never steals a null-start-time lock from an alive pid' , async ( ) => {
128- const lockDirPath = path . join ( tmpDir , 'null-start.lock' ) ;
129- // An acquiredAtMs far older than this process simulates what a wall-clock
130- // step makes a live null-start owner look like; age is not proof of death,
131- // so the waiter must time out instead of reclaiming the held lock.
132- writeLockOwnerFixture ( lockDirPath , {
133- pid : process . pid ,
134- startTime : null ,
135- acquiredAtMs : Date . now ( ) - 365 * 24 * 60 * 60_000 ,
136- } ) ;
137-
138- await assert . rejects (
139- ( ) =>
140- acquireProcessLock ( {
141- lockDirPath,
142- owner : currentProcessOwner ( ) ,
143- timeoutMs : 50 ,
144- pollMs : 1 ,
145- } ) ,
146- ( error : unknown ) => {
147- assert . ok ( error instanceof AppError ) ;
148- assert . equal ( error . details ?. ownerLiveness , 'live' ) ;
149- return true ;
150- } ,
151- ) ;
152- } ) ;
153-
15469test ( 'acquireProcessLock reports live lock owner details on timeout' , async ( ) => {
15570 const lockDirPath = path . join ( tmpDir , 'busy.lock' ) ;
15671 const owner = currentProcessOwner ( ) ;
@@ -989,27 +904,3 @@ test('release waits for a guard another process holds for a filesystem step', as
989904 assert . equal ( next . status , 'acquired' ) ;
990905 if ( next . status === 'acquired' ) await next . acquisition . release ( ) ;
991906} ) ;
992-
993- test ( 'a contender judges a live owner without holding the guard' , async ( ) => {
994- const lockDirPath = path . join ( tmpDir , 'probed-outside-guard.lock' ) ;
995- const mutexPath = path . join ( tmpDir , 'probed-outside-guard.reclaim.lock' ) ;
996- writeLockOwnerFixture ( lockDirPath , {
997- pid : process . ppid ,
998- startTime : null ,
999- acquiredAtMs : Date . now ( ) ,
1000- claimToken : 'live-rival' ,
1001- } ) ;
1002- let probes = 0 ;
1003- let probesUnderGuard = 0 ;
1004- processProbe . observe = ( ) => {
1005- probes += 1 ;
1006- if ( fs . existsSync ( mutexPath ) ) probesUnderGuard += 1 ;
1007- } ;
1008-
1009- const attempt = tryAcquireProcessLock ( { lockDirPath, owner : currentProcessOwner ( ) } ) ;
1010-
1011- assert . equal ( attempt . status , 'busy' ) ;
1012- assert . ok ( probes > 0 , 'the live owner was never probed' ) ;
1013- assert . equal ( probesUnderGuard , 0 ) ;
1014- assert . equal ( fs . existsSync ( mutexPath ) , false ) ;
1015- } ) ;
0 commit comments