Skip to content

Commit 5932300

Browse files
committed
test(host-kit): split lock-owner liveness cases into their own module
process-lock.test.ts crossed the 1,000-line tripwire once the snapshot-aware fixture landed, and the ratchet refuses growth. The three owner-liveness judgment cases (zombie reclaim, null-start fail-closed, guard-free live judgment) mirror the classification seam, not lock mechanics, so they move — unchanged — to process-lock-owner-liveness.test.ts with the probe fixture they consume. Lock-mechanics cases stay. No assertions changed.
1 parent 1869aed commit 5932300

2 files changed

Lines changed: 128 additions & 109 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import { afterEach, beforeEach, test, vi } from 'vitest';
5+
import { AppError } from '@agent-device/kernel/errors';
6+
7+
const { zombiePids, processProbe } = vi.hoisted(() => ({
8+
zombiePids: new Set<number>(),
9+
processProbe: { observe: undefined as (() => void) | undefined },
10+
}));
11+
12+
vi.mock('./host-process.ts', async (importOriginal) => {
13+
const actual = await importOriginal<typeof import('./host-process.ts')>();
14+
return {
15+
...actual,
16+
isProcessZombie: (pid: number) => {
17+
processProbe.observe?.();
18+
return zombiePids.has(pid);
19+
},
20+
readProcessStartTime: (pid: number) => {
21+
processProbe.observe?.();
22+
return actual.readProcessStartTime(pid);
23+
},
24+
// Classification judges from one snapshot; the fixture answers it from the
25+
// same zombie state and real start times as the per-field probes.
26+
readHostProcessIdentityObservations: (pids: Iterable<number>) => {
27+
const observations = new Map<number, { state: string; startTime: string }>();
28+
for (const pid of pids) {
29+
processProbe.observe?.();
30+
const startTime = actual.readProcessStartTime(pid);
31+
if (!zombiePids.has(pid) && startTime === null) continue;
32+
observations.set(pid, {
33+
state: zombiePids.has(pid) ? 'ZN' : 'Ss',
34+
startTime: startTime ?? 'zombie-snapshot',
35+
});
36+
}
37+
return observations;
38+
},
39+
};
40+
});
41+
42+
import { acquireProcessLock, tryAcquireProcessLock } from './process-lock.ts';
43+
import { mkdtempForTestSync } from './tmp-dir.fixtures.ts';
44+
import { currentProcessOwner, writeLockOwnerFixture } from './process-lock.fixtures.ts';
45+
46+
let tmpDir: string;
47+
48+
beforeEach(() => {
49+
tmpDir = mkdtempForTestSync('agent-device-process-lock-liveness-');
50+
});
51+
52+
afterEach(() => {
53+
processProbe.observe = undefined;
54+
fs.rmSync(tmpDir, { recursive: true, force: true });
55+
});
56+
57+
test('acquireProcessLock reclaims locks owned by zombie processes', async () => {
58+
const lockDirPath = path.join(tmpDir, 'zombie.lock');
59+
fs.mkdirSync(lockDirPath);
60+
// The owner passes kill(pid, 0) and matches its recorded start time; only
61+
// the zombie state reveals it already terminated.
62+
fs.writeFileSync(path.join(lockDirPath, 'owner.json'), JSON.stringify(currentProcessOwner()));
63+
zombiePids.add(process.pid);
64+
65+
try {
66+
const release = await acquireProcessLock({
67+
lockDirPath,
68+
owner: currentProcessOwner(),
69+
timeoutMs: 3_000,
70+
pollMs: 1,
71+
});
72+
await release();
73+
assert.equal(fs.existsSync(lockDirPath), false);
74+
} finally {
75+
zombiePids.delete(process.pid);
76+
}
77+
});
78+
79+
test('acquireProcessLock never steals a null-start-time lock from an alive pid', async () => {
80+
const lockDirPath = path.join(tmpDir, 'null-start.lock');
81+
// An acquiredAtMs far older than this process simulates what a wall-clock
82+
// step makes a live null-start owner look like; age is not proof of death,
83+
// so the waiter must time out instead of reclaiming the held lock.
84+
writeLockOwnerFixture(lockDirPath, {
85+
pid: process.pid,
86+
startTime: null,
87+
acquiredAtMs: Date.now() - 365 * 24 * 60 * 60_000,
88+
});
89+
90+
await assert.rejects(
91+
() =>
92+
acquireProcessLock({
93+
lockDirPath,
94+
owner: currentProcessOwner(),
95+
timeoutMs: 50,
96+
pollMs: 1,
97+
}),
98+
(error: unknown) => {
99+
assert.ok(error instanceof AppError);
100+
assert.equal(error.details?.ownerLiveness, 'live');
101+
return true;
102+
},
103+
);
104+
});
105+
106+
test('a contender judges a live owner without holding the guard', async () => {
107+
const lockDirPath = path.join(tmpDir, 'probed-outside-guard.lock');
108+
const mutexPath = path.join(tmpDir, 'probed-outside-guard.reclaim.lock');
109+
writeLockOwnerFixture(lockDirPath, {
110+
pid: process.ppid,
111+
startTime: null,
112+
acquiredAtMs: Date.now(),
113+
claimToken: 'live-rival',
114+
});
115+
let probes = 0;
116+
let probesUnderGuard = 0;
117+
processProbe.observe = () => {
118+
probes += 1;
119+
if (fs.existsSync(mutexPath)) probesUnderGuard += 1;
120+
};
121+
122+
const attempt = tryAcquireProcessLock({ lockDirPath, owner: currentProcessOwner() });
123+
124+
assert.equal(attempt.status, 'busy');
125+
assert.ok(probes > 0, 'the live owner was never probed');
126+
assert.equal(probesUnderGuard, 0);
127+
assert.equal(fs.existsSync(mutexPath), false);
128+
});

‎packages/host-kit/src/internal/process-lock.test.ts‎

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,6 @@ import { afterEach, beforeEach, test, vi } from 'vitest';
55
import { AppError, normalizeError } from '@agent-device/kernel/errors';
66
import * 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-
438
import {
449
acquireProcessLock,
4510
acquireProcessLockAcquisition,
@@ -69,7 +34,6 @@ beforeEach(() => {
6934
});
7035

7136
afterEach(() => {
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-
15469
test('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

Comments
 (0)