Skip to content

Commit 7d78066

Browse files
committed
fix(daemon): keep timeout recovery out of the CLI's eager closure
The Coverage lane caught two gates the earlier heads owed: - src/cli.ts evaluated two extra modules on import because the timeout handler statically reached the probe and the registration fence. Both now load behind a function-scoped await import, matching the module family's existing pattern: a timed-out request is their only caller, so a healthy invocation no longer pays for the recovery path (ADR 0019's loading-shape probe). - daemon-client-lifecycle.test.ts had grown past its merge-base length on the 1,000-line tripwire. Its sendRequest-timeout row tested the timeout route, not the lifecycle module, so it moved to the route's own file — where the reset-path stand-ins and path-resolution claim already fit — and the hanging-daemon fixture went with it. The lifecycle file now sits under its merge-base length.
1 parent 2fe23f7 commit 7d78066

3 files changed

Lines changed: 52 additions & 129 deletions

File tree

‎src/daemon-client/__tests__/daemon-client-lifecycle.test.ts‎

Lines changed: 1 addition & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ vi.mock('@agent-device/host-kit/retry', async (importOriginal) => ({
1919
}));
2020

2121
import { resolveDaemonPaths, type DaemonPaths } from '../../daemon-resolution.ts';
22-
import { sendToDaemon, type DaemonRequest, type DaemonResponse } from '../daemon-client.ts';
22+
import { sendToDaemon, type DaemonResponse } from '../daemon-client.ts';
2323
import { attachActiveSessionAddressHint } from '../daemon-client-lifecycle.ts';
24-
import { sendRequest } from '../daemon-client-transport.ts';
2524
import {
2625
closeLoopbackServer,
2726
listenOnLoopback,
@@ -160,48 +159,6 @@ function installSpawnedHttpDaemonAtOwnedStateDir(
160159
});
161160
}
162161

163-
// A daemon that accepts the RPC but never answers it, and then refuses every later connection —
164-
// the wedged daemon #3177's reset path exists for. The client's timeout route probes `/health`
165-
// before deciding to reset (so a merely-slow daemon is preserved); a daemon that answers THAT too
166-
// would correctly be kept alive and its metadata left in place, which is the opposite of what this
167-
// fixture's reset-path callers assert. Refusing the probe (rather than hanging it) fails the probe
168-
// fast instead of spending its full window.
169-
async function startHangingHttpDaemonFixture(): Promise<HttpDaemonFixture> {
170-
const seenPaths: string[] = [];
171-
const rpcRequests: Record<string, any>[] = [];
172-
let requests = 0;
173-
const server = http.createServer((req, res) => {
174-
const url = new URL(req.url || '/', 'http://127.0.0.1');
175-
seenPaths.push(`${req.method ?? 'GET'} ${url.pathname}`);
176-
requests += 1;
177-
178-
if (req.method === 'POST' && url.pathname === '/rpc') {
179-
const chunks: Buffer[] = [];
180-
req.on('data', (chunk) => {
181-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
182-
});
183-
req.on('end', () => {
184-
rpcRequests.push(JSON.parse(Buffer.concat(chunks).toString('utf8')) as Record<string, any>);
185-
});
186-
// Never respond: the client's own envelope is the only completion path.
187-
return;
188-
}
189-
190-
// The liveness probe's fresh request after the RPC timed out: refuse it, so the route sees an
191-
// unresponsive daemon and takes its reset branch.
192-
if (requests > 1) {
193-
res.destroy();
194-
return;
195-
}
196-
197-
res.writeHead(404);
198-
res.end('not found');
199-
});
200-
server.on('clientError', (_err, socket) => socket.destroy());
201-
const port = await listenOnLoopback(server);
202-
return { server, port, seenPaths, rpcRequests };
203-
}
204-
205162
function installSpawnedHttpDaemon(paths: DaemonPaths, httpPort: number): void {
206163
mockRunCmdDetached.mockImplementation((_command, _args, options) => {
207164
assert.equal(options?.env?.AGENT_DEVICE_STATE_DIR, paths.baseDir);
@@ -602,82 +559,6 @@ test('sendToDaemon replaces socket-only daemon metadata when HTTP transport is r
602559
}
603560
});
604561

605-
test('sendRequest timeout cleanup uses resolved daemon paths instead of request flags', async (t) => {
606-
if (!(await supportsLoopbackBind())) {
607-
t.skip('loopback listeners are not permitted in this environment');
608-
return;
609-
}
610-
611-
const daemonStateDir = makeTempStateDir('agent-device-daemon-timeout-active-');
612-
const requestFlagStateDir = makeTempStateDir('agent-device-daemon-timeout-request-');
613-
const daemonPaths = resolveDaemonPaths(daemonStateDir);
614-
const requestFlagPaths = resolveDaemonPaths(requestFlagStateDir);
615-
const daemon = await startHangingHttpDaemonFixture();
616-
// The cleanup fence only clears a registration that still MATCHES the timed-out daemon on pid
617-
// and start time (#3125), so this fixture names one identity on both sides: the record it
618-
// publishes and the info `sendRequest` times out on.
619-
const timedOutDaemonStartTime = 'timeout-daemon-start';
620-
writeDaemonInfo(daemonPaths, {
621-
httpPort: daemon.port,
622-
transport: 'http',
623-
pid: 999_999,
624-
processStartTime: timedOutDaemonStartTime,
625-
});
626-
writeDaemonLock(daemonPaths, { pid: 999_999 });
627-
writeDaemonInfo(requestFlagPaths, {
628-
httpPort: daemon.port,
629-
transport: 'http',
630-
pid: 999_998,
631-
});
632-
writeDaemonLock(requestFlagPaths, { pid: 999_998 });
633-
634-
const request: DaemonRequest = {
635-
session: 'default',
636-
command: 'replay',
637-
positionals: [],
638-
flags: { stateDir: requestFlagStateDir, daemonTransport: 'http' },
639-
token: 'local-secret',
640-
meta: { requestId: 'req-timeout-paths' },
641-
};
642-
643-
try {
644-
let thrown: unknown;
645-
try {
646-
await sendRequest(
647-
{
648-
token: 'local-secret',
649-
pid: 999_999,
650-
httpPort: daemon.port,
651-
transport: 'http',
652-
processStartTime: timedOutDaemonStartTime,
653-
},
654-
request,
655-
'http',
656-
daemonPaths,
657-
50,
658-
);
659-
} catch (error) {
660-
thrown = error;
661-
}
662-
663-
assert.ok(thrown instanceof AppError);
664-
assert.equal(thrown.message, 'Daemon request timed out');
665-
// The RPC first, then the liveness probe the timeout route asks before it is allowed to
666-
// reset. The fixture refused that probe, which is what authorized the reset below.
667-
assert.deepEqual(daemon.seenPaths, ['POST /rpc', 'GET /health']);
668-
assert.equal(fs.existsSync(daemonPaths.infoPath), false);
669-
// The reset owns its registration, not the protocol lock: reclaim belongs to the acquirer
670-
// under ADR 0030's mutation guard, so neither state dir's lock is ever swept.
671-
assert.equal(fs.existsSync(daemonPaths.lockPath), true);
672-
assert.equal(fs.existsSync(requestFlagPaths.infoPath), true);
673-
assert.equal(fs.existsSync(requestFlagPaths.lockPath), true);
674-
} finally {
675-
await closeLoopbackServer(daemon.server);
676-
fs.rmSync(daemonStateDir, { recursive: true, force: true });
677-
fs.rmSync(requestFlagStateDir, { recursive: true, force: true });
678-
}
679-
});
680-
681562
test('sendToDaemon falls back from failed socket transport to HTTP using daemon metadata ports', async (t) => {
682563
if (!(await supportsLoopbackBind())) {
683564
t.skip('loopback listeners are not permitted in this environment');

‎src/daemon-client/__tests__/daemon-client-timeout-route.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,40 @@ test('a refused timeout fallback preserves the timeout without an unhandled reje
435435
await closeLoopbackServer(daemon.server);
436436
}
437437
});
438+
439+
test('request-timeout route: the reset acts on the resolved state paths, not the request flags', async (t) => {
440+
if (await skipWhenLoopbackUnavailable(t)) return;
441+
// Path resolution happens upstream of `sendRequest`, which receives the state paths the caller
442+
// resolved. The reset must clear THAT registration and leave every other state dir alone — even
443+
// one a request flag names, which this route never reads. (Moved from daemon-client-lifecycle
444+
// coverage, where the timeout route lived before #3177 scoped it.)
445+
const daemon = await startStandIn('http', 'refuse');
446+
const resolvedPaths = dummyStatePaths();
447+
const requestFlagPaths = dummyStatePaths();
448+
seedRegistration(resolvedPaths, { pid: process.pid, startTime: TEST_DAEMON_START_TIME });
449+
seedRegistration(requestFlagPaths, { pid: REPLACEMENT_DAEMON_PID, startTime: 'other-owner' });
450+
try {
451+
await expectRouteError(
452+
sendRequest(
453+
{
454+
httpPort: daemon.port,
455+
token: 'test-token',
456+
pid: process.pid,
457+
processStartTime: TEST_DAEMON_START_TIME,
458+
},
459+
{
460+
...buildRequest(RESET_POLICY_COMMAND, undefined),
461+
flags: { stateDir: requestFlagPaths.baseDir },
462+
},
463+
'http',
464+
resolvedPaths,
465+
TIMEOUT_MS,
466+
),
467+
/The daemon did not answer the liveness probe and was reset after the timeout/,
468+
);
469+
assert.ok(!fs.existsSync(resolvedPaths.infoPath), 'the resolved registration is cleared');
470+
assert.ok(fs.existsSync(requestFlagPaths.infoPath), 'the flag-named state dir is untouched');
471+
} finally {
472+
await closeLoopbackServer(daemon.server);
473+
}
474+
});

‎src/daemon-client/daemon-client-timeout.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import type { OwnerIdentity } from '@agent-device/host-kit/process';
55
import { isAgentDeviceDaemonProcess } from '../daemon-process.ts';
66
import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog';
77
import { resolveCommandTimeoutPolicy } from '@agent-device/command-registry/registry';
8-
import {
9-
readRegisteredDaemonOwnership,
10-
type RegisteredDaemonOwnership,
11-
} from '../daemon-registration.ts';
8+
import type { RegisteredDaemonOwnership } from '../daemon-registration.ts';
129
import type { DaemonPaths } from '../daemon-resolution.ts';
1310
import type { PlatformSelector } from '@agent-device/kernel/device';
1411
import {
1512
removeDaemonInfo,
1613
stopDaemonProcessForTakeover,
1714
type DaemonInfo,
1815
} from './daemon-client-metadata.ts';
19-
import { probeDaemonResponsive } from './daemon-client-liveness-probe.ts';
2016

2117
// Recovery for a timed-out request is scoped to that request or to nothing. The daemon owns
2218
// request-scoped recovery (#3177): destroying the timed-out connection cancels exactly that
@@ -25,7 +21,9 @@ import { probeDaemonResponsive } from './daemon-client-liveness-probe.ts';
2521
// which sabotaged that cancel path (a swept kill reads as a host failure, not a canceled request)
2622
// and duplicated cleanup the daemon scopes to its own leases — plus a daemon SIGKILL that ended
2723
// every sibling session. The SIGKILL survives only behind the liveness probe below: the question
28-
// the reset used to assume.
24+
// the reset used to assume. That probe and the registration fence load on demand: a timed-out
25+
// request is their only caller, and the CLI's eager closure (the ADR 0019 loading-shape probe)
26+
// must not pay for a recovery path a healthy invocation never runs.
2927

3028
export async function handleRequestTimeout(
3129
params: Readonly<{
@@ -52,10 +50,16 @@ export async function handleRequestTimeout(
5250
// transport performed when this client's connection died is the recovery that request needed,
5351
// and sibling sessions survive. Only a daemon answering neither endpoint in the probe window is
5452
// the hung daemon a reset was designed for.
55-
const probeAnswered = resetEligible ? await probeDaemonResponsive(info, { session }) : undefined;
53+
const probeAnswered = resetEligible
54+
? await (
55+
await import('./daemon-client-liveness-probe.ts')
56+
).probeDaemonResponsive(info, {
57+
session,
58+
})
59+
: undefined;
5660
const unresponsive = probeAnswered === false;
5761
const daemonReset = unresponsive
58-
? resetDaemonAfterTimeout(info, statePaths)
62+
? await resetDaemonAfterTimeout(info, statePaths)
5963
: { performedReset: false, forcedKill: false, registration: undefined };
6064
emitDiagnostic({
6165
level: 'error',
@@ -176,7 +180,7 @@ type DaemonReset = Readonly<{
176180
// out-of-band is the legacy-reclaimer pattern the ADR refuses. A reset therefore ends the process
177181
// and its own registration only; the next acquirer reclaims the dead holder's lock through the
178182
// protocol.
179-
function resetDaemonAfterTimeout(info: DaemonInfo, paths: DaemonPaths): DaemonReset {
183+
async function resetDaemonAfterTimeout(info: DaemonInfo, paths: DaemonPaths): Promise<DaemonReset> {
180184
let forcedKill = false;
181185
try {
182186
if (isAgentDeviceDaemonProcess(info.pid, info.processStartTime)) {
@@ -192,6 +196,7 @@ function resetDaemonAfterTimeout(info: DaemonInfo, paths: DaemonPaths): DaemonRe
192196
});
193197
});
194198
}
199+
const { readRegisteredDaemonOwnership } = await import('../daemon-registration.ts');
195200
const owner: OwnerIdentity = {
196201
pid: info.pid,
197202
startTime: info.processStartTime ?? null,

0 commit comments

Comments
 (0)