Repository navigation
Expand file tree
/
Copy pathsmoke-daemon-clean.test.ts
More file actions
164 lines (153 loc) · 6.82 KB
/
Copy pathsmoke-daemon-clean.test.ts
File metadata and controls
164 lines (153 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { cleanupDaemonTestState } from './support/daemon-test-cleanup.ts';
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { skipWhenLoopbackUnavailable } from '@agent-device/test-kit';
import { runCmdSync } from '@agent-device/host-kit/command';
import { isProcessAlive, readProcessStartTime } from '@agent-device/host-kit/process';
import { stopAndRetireDaemon } from '@agent-device/daemon-contracts/daemon-registration-owner';
import { resolveDaemonPaths } from '@agent-device/daemon-contracts/daemon-resolution';
import { waitForDaemonExit } from '@agent-device/daemon-contracts/daemon-process';
import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts';
import { runCliJson } from './test-helpers.ts';
type DaemonInfo = {
pid: number;
processStartTime?: string;
};
test('clean daemon retains metadata when a live recorded process is not a verified daemon', () => {
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-clean-retained-'));
const identity = { pid: process.pid, processStartTime: readProcessStartTime(process.pid) };
assert.ok(identity.processStartTime);
const contents = JSON.stringify(identity);
fs.writeFileSync(path.join(stateDir, 'daemon.json'), contents);
fs.writeFileSync(path.join(stateDir, 'daemon.lock'), contents);
try {
const cleanup = runCmdSync(
process.execPath,
['--experimental-strip-types', 'scripts/clean-daemon.ts'],
{
env: { ...process.env, AGENT_DEVICE_STATE_DIR: stateDir },
timeoutMs: 5_000,
allowFailure: true,
},
);
assert.notEqual(cleanup.exitCode, 0, 'unconfirmed exit cannot complete cleanup');
assert.match(
cleanup.stderr,
/Daemon cleanup retained state because retirement could not be confirmed/,
);
assert.match(cleanup.stderr, /exit-unconfirmed/);
assert.equal(fs.readFileSync(path.join(stateDir, 'daemon.json'), 'utf8'), contents);
assert.equal(fs.readFileSync(path.join(stateDir, 'daemon.lock'), 'utf8'), contents);
assert.equal(isProcessAlive(process.pid), true);
} finally {
fs.rmSync(stateDir, { recursive: true, force: true });
}
});
test('clean daemon script stops a live daemon before removing metadata', async (t) => {
if (await skipWhenLoopbackUnavailable(t)) {
return;
}
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-clean-daemon-'));
let info: DaemonInfo | null = null;
try {
const cli = runCliJson(['session', 'list', '--json', '--state-dir', stateDir]);
assert.equal(cli.status, 0, `${cli.stderr}\n${cli.stdout}`);
assert.equal(cli.json?.success, true, JSON.stringify(cli.json));
info = readDaemonInfo(stateDir);
assert.equal(isProcessAlive(info.pid), true);
const infoPath = path.join(stateDir, 'daemon.json');
const original = fs.readFileSync(infoPath, 'utf8');
for (const ownership of ['embedded', 'unrecognized']) {
const record = JSON.stringify({ ...JSON.parse(original), ownership });
fs.writeFileSync(infoPath, record);
const refused = runCmdSync(
process.execPath,
['--experimental-strip-types', 'scripts/clean-daemon.ts'],
{
env: { ...process.env, AGENT_DEVICE_STATE_DIR: stateDir },
timeoutMs: 5_000,
allowFailure: true,
},
);
assert.notEqual(refused.exitCode, 0);
assert.match(refused.stderr, ownership === 'embedded' ? /embedded-owner/ : /owner-unproven/);
assert.equal(
isProcessAlive(info.pid),
true,
'the same otherwise signalable daemon stays alive',
);
assert.equal(fs.readFileSync(infoPath, 'utf8'), record);
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.lock')), true);
}
const legacy = JSON.parse(original);
delete legacy.ownership;
fs.writeFileSync(infoPath, JSON.stringify(legacy));
const cleanup = runCmdSync(
process.execPath,
['--experimental-strip-types', 'scripts/clean-daemon.ts'],
{
env: { ...process.env, AGENT_DEVICE_STATE_DIR: stateDir },
timeoutMs: 30_000,
},
);
assert.equal(cleanup.exitCode, 0, cleanup.stderr);
await assertDaemonLifetimeEnded(info);
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.json')), false);
assert.equal(fs.existsSync(path.join(stateDir, 'daemon.lock')), false);
// #1781 B1: the stopped daemon must take every process it owned with it and
// leave only classified artifacts in its state dir.
await assertNoDaemonLeaks({ stateDir, daemonPids: [info.pid], phase: 'after-shutdown' });
} finally {
await cleanupDaemonTestState(stateDir, info ?? null);
}
});
test('forced retirement waits for a real daemon and reclaims its abandoned registration claim', async (t) => {
if (await skipWhenLoopbackUnavailable(t)) return;
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-forced-retirement-'));
let info: DaemonInfo | undefined;
try {
const cli = runCliJson(['session', 'list', '--json', '--state-dir', stateDir]);
assert.equal(cli.status, 0, `${cli.stderr}\n${cli.stdout}`);
info = readDaemonInfo(stateDir);
const paths = resolveDaemonPaths(stateDir);
assert.equal(fs.existsSync(paths.lockPath), true);
const result = await stopAndRetireDaemon({
paths,
// The record is the one this row's CLI-started daemon published of itself, which is a detached
// daemon: what the row retires is that lifetime, under that owner.
observed: {
pid: info.pid,
startTime: info.processStartTime ?? null,
ownership: 'detached',
},
mode: 'force',
});
assert.equal(result.status, 'retired', JSON.stringify(result));
if (result.status !== 'retired') assert.fail('retirement not confirmed');
assert.equal(result.termination.mode, 'forced');
assert.equal(result.removedInfo, true);
await assertDaemonLifetimeEnded(info);
assert.equal(fs.existsSync(paths.infoPath), false);
assert.equal(fs.existsSync(paths.lockPath), false);
} finally {
await cleanupDaemonTestState(stateDir, info ?? null);
}
});
/**
* Checks the daemon lifetime with the exit predicate that retirement confirms. A killed detached
* daemon is a zombie until its new parent reaps it, and `kill(pid, 0)` still finds a zombie.
*/
async function assertDaemonLifetimeEnded(info: DaemonInfo): Promise<void> {
assert.ok(info.processStartTime, 'the daemon record names its process start time');
const wait = await waitForDaemonExit(
{ pid: info.pid, startTime: info.processStartTime },
{ timeoutMs: 0 },
);
assert.equal(wait.exited, true, `daemon pid ${info.pid} still runs its recorded lifetime`);
}
function readDaemonInfo(stateDir: string): DaemonInfo {
return JSON.parse(fs.readFileSync(path.join(stateDir, 'daemon.json'), 'utf8')) as DaemonInfo;
}