Skip to content

Commit df38a36

Browse files
committed
fix: centralize confirmed daemon retirement under the startup lock
1 parent aea5196 commit df38a36

6 files changed

Lines changed: 400 additions & 81 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pnpm test-app:typecheck
3737
`pnpm build` compiles the TypeScript CLI and library. If a running development daemon must pick up
3838
that build, use `pnpm rebuild:cli`; it builds and then stops the worktree-scoped daemon.
3939

40+
`pnpm clean:daemon` retains state when it cannot confirm the recorded daemon exited. Restore
41+
process inspection or stop the verified owner before retrying. Its `--prune-dev` option retires
42+
confirmed abandoned registrations older than 14 days; it keeps session artifacts and state
43+
directories for inspection.
44+
4045
Build only the Apple runner target you changed:
4146

4247
```bash

‎scripts/clean-daemon.ts‎

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,60 @@ import os from 'node:os';
33
import path from 'node:path';
44
import { AppError } from '@agent-device/kernel/errors';
55
import { resolveDaemonPaths } from '../src/daemon-resolution.ts';
6-
import { isAgentDeviceDaemonProcess, stopDaemonProcess } from '../src/daemon-process.ts';
6+
import { readRegisteredDaemonIdentity } from '../src/daemon-registration.ts';
7+
import {
8+
stopAndRetireDaemon,
9+
recoverAbandonedDaemonRegistration,
10+
} from '../src/daemon-registration-owner.ts';
711

812
const DAEMON_TERM_TIMEOUT_MS = 15_000;
913
const DAEMON_KILL_TIMEOUT_MS = 2_000;
1014
const PRUNE_DEV_FLAG = '--prune-dev';
1115
const PRUNE_DEV_MAX_AGE_MS = 14 * 24 * 60 * 60 * 1000;
1216

13-
type DaemonInfo = {
14-
pid?: number;
15-
processStartTime?: string;
16-
};
17-
1817
const paths = resolveDaemonPaths(process.env.AGENT_DEVICE_STATE_DIR);
19-
const info = readDaemonInfo(paths.infoPath);
20-
const daemonPid = readPositivePid(info?.pid);
21-
22-
if (daemonPid !== null) {
23-
const termination = await stopDaemonProcess(
24-
{ pid: daemonPid, startTime: info?.processStartTime ?? null },
18+
const retirement = await stopAndRetireDaemon({
19+
paths,
20+
observed: readRegisteredDaemonIdentity(paths.infoPath),
21+
mode: 'graceful',
22+
termTimeoutMs: DAEMON_TERM_TIMEOUT_MS,
23+
killTimeoutMs: DAEMON_KILL_TIMEOUT_MS,
24+
});
25+
if (retirement.status === 'retained') {
26+
throw new AppError(
27+
'COMMAND_FAILED',
28+
'Daemon cleanup retained state because retirement could not be confirmed.',
2529
{
26-
mode: 'graceful',
27-
termTimeoutMs: DAEMON_TERM_TIMEOUT_MS,
28-
killTimeoutMs: DAEMON_KILL_TIMEOUT_MS,
30+
reason: 'daemon_retirement_unconfirmed',
31+
retirement,
32+
hint: retirement.error?.hint,
2933
},
3034
);
31-
if (termination.status !== 'exited') {
32-
throw new AppError(
33-
'COMMAND_FAILED',
34-
'Daemon cleanup retained state because exit could not be confirmed.',
35-
{
36-
reason: 'daemon_exit_unconfirmed',
37-
termination,
38-
},
39-
);
40-
}
35+
}
36+
if (retirement.status === 'retired') {
4137
const { cleanupRunnerLeasesForOwner } =
4238
await import('@agent-device/platform-apple/runner/operations');
43-
await cleanupRunnerLeasesForOwner({ pid: daemonPid, startTime: info?.processStartTime });
39+
await cleanupRunnerLeasesForOwner(retirement.termination.identity);
4440
}
4541

46-
removeIfPresent(paths.infoPath);
47-
removeIfPresent(paths.lockPath);
48-
4942
if (process.argv.includes(PRUNE_DEV_FLAG)) {
50-
pruneStaleDevStateDirs();
51-
}
52-
53-
function readDaemonInfo(infoPath: string): DaemonInfo | null {
54-
try {
55-
return JSON.parse(fs.readFileSync(infoPath, 'utf8')) as DaemonInfo;
56-
} catch {
57-
return null;
58-
}
59-
}
60-
61-
function removeIfPresent(filePath: string): void {
62-
try {
63-
fs.unlinkSync(filePath);
64-
} catch (error) {
65-
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
66-
throw error;
67-
}
68-
}
43+
await pruneStaleDevStateDirs();
6944
}
7045

71-
// Removes worktree-scoped state dirs under ~/.agent-device/dev/ that no live daemon
72-
// owns and that have been idle past the retention threshold. Never touches the
73-
// global ~/.agent-device root contents.
74-
function pruneStaleDevStateDirs(): void {
46+
async function pruneStaleDevStateDirs(): Promise<void> {
7547
const devRoot = path.join(os.homedir(), '.agent-device', 'dev');
7648
const cutoffMs = Date.now() - PRUNE_DEV_MAX_AGE_MS;
7749
for (const dirPath of listDevStateDirs(devRoot)) {
78-
if (hasLiveDaemon(dirPath) || newestMtimeMs(dirPath) > cutoffMs) continue;
79-
fs.rmSync(dirPath, { recursive: true, force: true });
80-
process.stdout.write(`Removed stale daemon state dir: ${dirPath}\n`);
50+
if (newestMtimeMs(dirPath) > cutoffMs) continue;
51+
const paths = resolveDaemonPaths(dirPath);
52+
const result = await recoverAbandonedDaemonRegistration({
53+
paths,
54+
observed: readRegisteredDaemonIdentity(paths.infoPath),
55+
});
56+
if (result.status === 'retired')
57+
process.stdout.write(
58+
`Retired stale daemon registration: ${dirPath} (session artifacts retained)\n`,
59+
);
8160
}
8261
}
8362

@@ -93,17 +72,6 @@ function listDevStateDirs(devRoot: string): string[] {
9372
.map((entry) => path.join(devRoot, entry.name));
9473
}
9574

96-
function hasLiveDaemon(stateDir: string): boolean {
97-
const dirInfo = readDaemonInfo(path.join(stateDir, 'daemon.json'));
98-
const pid = readPositivePid(dirInfo?.pid);
99-
return pid !== null && isAgentDeviceDaemonProcess(pid, dirInfo?.processStartTime);
100-
}
101-
102-
function readPositivePid(pid: number | undefined): number | null {
103-
if (typeof pid !== 'number') return null;
104-
return Number.isInteger(pid) && pid > 0 ? pid : null;
105-
}
106-
10775
function newestMtimeMs(dirPath: string): number {
10876
let newest = statMtimeMs(dirPath);
10977
let children: fs.Dirent[];

‎src/__tests__/daemon-registration-owner.test.ts‎

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import { afterEach, test, vi } from 'vitest';
44
import { readCurrentOwnerIdentity } from '@agent-device/host-kit/process';
5-
import { tryAcquireDaemonRegistration } from '../daemon-registration-owner.ts';
5+
import {
6+
tryAcquireDaemonRegistration,
7+
stopAndRetireDaemon,
8+
recoverAbandonedDaemonRegistration,
9+
} from '../daemon-registration-owner.ts';
610
import { resolveDaemonPaths, type DaemonPaths } from '../daemon-resolution.ts';
711
import { readRegisteredDaemonOwnership } from '../daemon-registration.ts';
812
import { readDaemonShutdownReport } from '../daemon-shutdown-report.ts';
@@ -159,3 +163,131 @@ test('unlink disappearance is settled, other failures remain primary even when r
159163
assert.equal(fs.existsSync(second.paths.infoPath), true);
160164
assert.match(fs.readFileSync(second.paths.logPath, 'utf8'), /daemon_registration_release_failed/);
161165
});
166+
167+
const deadIdentity = { pid: 999_999_999, startTime: 'dead-start' };
168+
for (const scenario of [
169+
{
170+
name: 'missing-start',
171+
observed: { pid: process.pid, startTime: null },
172+
published: ownIdentity,
173+
retired: false,
174+
},
175+
{ name: 'live-unverified', observed: ownIdentity, published: ownIdentity, retired: false },
176+
{ name: 'no-observation', observed: null, published: deadIdentity, retired: false },
177+
{ name: 'replaced', observed: deadIdentity, published: ownIdentity, retired: false },
178+
{ name: 'dead', observed: deadIdentity, published: deadIdentity, retired: true },
179+
] as const) {
180+
test(`retirement respects the captured lifetime (${scenario.name})`, async () => {
181+
const paths = resolveDaemonPaths(mkdtempForTestSync('agent-device-retirement-'));
182+
replaceInfo(paths, scenario.published.pid, scenario.published.startTime);
183+
const before = fs.readFileSync(paths.infoPath, 'utf8');
184+
const result = await stopAndRetireDaemon({
185+
paths,
186+
observed: scenario.observed,
187+
mode: 'graceful',
188+
lockTimeoutMs: 0,
189+
});
190+
if (scenario.retired) {
191+
assert.equal(result.status, 'retired');
192+
assert.equal(result.removedInfo, true);
193+
assert.equal(fs.existsSync(paths.infoPath), false);
194+
} else {
195+
assert.equal(result.status, 'retained');
196+
assert.equal(fs.readFileSync(paths.infoPath, 'utf8'), before);
197+
}
198+
});
199+
}
200+
201+
test('abandoned recovery never signals a live owner and absence is inspected under the lock', async () => {
202+
const { paths, owner } = await acquire();
203+
owner.publish(fields);
204+
const before = fs.readFileSync(paths.infoPath, 'utf8');
205+
const result = await recoverAbandonedDaemonRegistration({ paths, observed: ownIdentity });
206+
assert.equal(result.status, 'retained');
207+
assert.equal(fs.readFileSync(paths.infoPath, 'utf8'), before);
208+
await owner.finish();
209+
assert.deepEqual(await recoverAbandonedDaemonRegistration({ paths, observed: null }), {
210+
status: 'absent',
211+
removedInfo: false,
212+
});
213+
});
214+
215+
test('retirement preserves a winner holding the startup lock instead of stopping or unlinking it', async () => {
216+
const { paths, owner } = await acquire();
217+
owner.publish({ ...fields, token: 'winning-token' });
218+
const before = fs.readFileSync(paths.infoPath, 'utf8');
219+
const result = await stopAndRetireDaemon({
220+
paths,
221+
observed: { pid: 999_999_999, startTime: 'dead-start' },
222+
mode: 'force',
223+
lockTimeoutMs: 0,
224+
});
225+
assert.equal(result.status, 'retained');
226+
if (result.status !== 'retained') throw new Error('retirement unexpectedly completed');
227+
assert.equal(result.reason, 'lock-busy');
228+
assert.equal(result.error?.details?.reason, 'process_lock_timeout');
229+
assert.match(result.error?.hint ?? '', /Restore process inspection/);
230+
assert.equal(fs.readFileSync(paths.infoPath, 'utf8'), before);
231+
assert.equal((await tryAcquireDaemonRegistration(paths)).status, 'busy');
232+
await owner.finish();
233+
});
234+
235+
test('metadata failure remains primary and normalized through a secondary release failure', async () => {
236+
const paths = resolveDaemonPaths(mkdtempForTestSync('agent-device-retirement-failures-'));
237+
const observed = { pid: 999_999_999, startTime: 'dead-start' };
238+
replaceInfo(paths, observed.pid, observed.startTime);
239+
const primary = Object.assign(new Error('metadata failure'), { code: 'EIO' });
240+
const originalUnlink = fs.unlinkSync;
241+
vi.spyOn(fs, 'unlinkSync').mockImplementation((target) => {
242+
if (target === paths.infoPath) throw primary;
243+
return originalUnlink(target);
244+
});
245+
const originalRmdir = fs.rmdirSync;
246+
vi.spyOn(fs, 'rmdirSync').mockImplementation((target, options) => {
247+
if (target === paths.lockPath) throw Object.assign(new Error('busy'), { code: 'EBUSY' });
248+
return originalRmdir(target, options);
249+
});
250+
const result = await stopAndRetireDaemon({ paths, observed, mode: 'force' });
251+
assert.equal(result.status, 'retained');
252+
if (result.status !== 'retained') throw new Error('retirement unexpectedly completed');
253+
assert.equal(result.error?.message, 'metadata failure');
254+
assert.equal(result.error?.code, 'UNKNOWN');
255+
assert.equal(result.error?.cause?.code, 'EIO');
256+
assert.equal(fs.existsSync(paths.infoPath), true);
257+
});
258+
259+
test('release failure reports partial retirement rather than success', async () => {
260+
const paths = resolveDaemonPaths(mkdtempForTestSync('agent-device-retirement-partial-'));
261+
const observed = { pid: 999_999_999, startTime: 'dead-start' };
262+
replaceInfo(paths, observed.pid, observed.startTime);
263+
const original = fs.rmdirSync;
264+
vi.spyOn(fs, 'rmdirSync').mockImplementation((target, options) => {
265+
if (target === paths.lockPath) throw Object.assign(new Error('busy'), { code: 'EBUSY' });
266+
return original(target, options);
267+
});
268+
const result = await stopAndRetireDaemon({ paths, observed, mode: 'force' });
269+
assert.equal(result.status, 'retained');
270+
assert.equal(result.removedInfo, true);
271+
assert.equal(fs.existsSync(paths.infoPath), false);
272+
assert.equal(fs.existsSync(paths.lockPath), true);
273+
});
274+
275+
test.skipIf(process.getuid?.() === 0)(
276+
'an acquisition I/O failure retains metadata without reporting contention',
277+
async () => {
278+
const paths = resolveDaemonPaths(mkdtempForTestSync('agent-device-retirement-permission-'));
279+
replaceInfo(paths, deadIdentity.pid, deadIdentity.startTime);
280+
const before = fs.readFileSync(paths.infoPath, 'utf8');
281+
fs.chmodSync(paths.baseDir, 0o500);
282+
try {
283+
const result = await recoverAbandonedDaemonRegistration({ paths, observed: deadIdentity });
284+
assert.equal(result.status, 'retained');
285+
if (result.status !== 'retained') throw new Error('retirement unexpectedly completed');
286+
assert.equal(result.reason, 'retirement-unconfirmed');
287+
assert.equal(fs.readFileSync(paths.infoPath, 'utf8'), before);
288+
assert.equal(fs.existsSync(paths.lockPath), false);
289+
} finally {
290+
fs.chmodSync(paths.baseDir, 0o700);
291+
}
292+
},
293+
);

0 commit comments

Comments
 (0)