Skip to content

Commit 86dee15

Browse files
thymikeeclaude
andcommitted
fix(daemon): share owner refusal hints and cover takeover without start time
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7dc1986 commit 86dee15

6 files changed

Lines changed: 39 additions & 23 deletions

File tree

‎packages/daemon-contracts/src/daemon-resolution.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,5 +297,5 @@ export function readDaemonLifecycleCensus(details: unknown): DaemonLifecycleCens
297297

298298
/** Directs embedded runtime callers to the host that owns close. */
299299
export function embeddedCloseHint(pid: number | undefined): string {
300-
return `${pid === undefined ? 'The runtime is owned by its host process.' : `The runtime is owned by host process ${pid}.`} Close it there with the handle returned by startAgentDevice(). If the host has exited, use daemon stop or ensureDaemon() for this state directory to recover its abandoned registration.`;
300+
return `${pid === undefined ? 'The runtime is owned by its host process.' : `The runtime is owned by host process ${pid}.`} Close it there with the handle returned by startAgentDevice(). Recovery with daemon stop or ensureDaemon() for this state directory retires the abandoned registration only when the recorded host lifetime has provably exited.`;
301301
}

‎packages/daemon-contracts/src/daemon-stop.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ export async function stopDaemon(params: {
105105
* those two retained reasons become a stop's own reason: a caller that may not signal this process
106106
* hears the owner's rule, not a retirement that could not be confirmed.
107107
*/
108-
function ownerRefusal(
108+
export function ownerRefusal(
109109
reason: 'embedded-owner' | 'owner-unproven',
110-
pid: number,
110+
pid: number | undefined,
111111
): Readonly<{ reason: string; hint: string }> {
112112
return reason === 'embedded-owner'
113113
? {

‎src/cli/commands/__tests__/daemon.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,14 @@ test.each([
330330
true,
331331
JSON.stringify({ pid: 555, processStartTime: 'start', ownership: 'embedded' }),
332332
'daemon-clean-not-detached',
333-
/Stop it without --clean/,
333+
/owned by host process 555\. Close it there with the handle returned by startAgentDevice\(\)\..*only when the recorded host lifetime has provably exited/,
334334
],
335335
[
336336
'unknown',
337337
true,
338338
JSON.stringify({ pid: 555, processStartTime: 'start', ownership: 'sideways' }),
339339
'daemon-clean-not-detached',
340-
/Stop it without --clean/,
340+
/Nothing was signaled\. Check what owns that process, then repair the record: stop it by its own means and remove its daemon\.json/,
341341
],
342342
['unreadable', true, '{not json', 'daemon-clean-not-detached', /remove .*daemon\.json/],
343343
['unreadable', false, '{not json', 'daemon-record-unreadable', /Inspect or remove that record/],

‎src/cli/commands/daemon.ts‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
type DaemonStatus,
1313
type DaemonStopIdentity,
1414
} from '@agent-device/daemon-contracts/daemon-registration';
15-
import type { DaemonStopResult } from '@agent-device/daemon-contracts/daemon-stop';
15+
import { ownerRefusal, type DaemonStopResult } from '@agent-device/daemon-contracts/daemon-stop';
1616
import { AppError } from '@agent-device/kernel/errors';
1717
import { shellQuoteIfNeeded } from '@agent-device/kernel/device-shell';
1818
import { writeCommandOutput } from './shared.ts';
@@ -41,7 +41,7 @@ export const daemonCommand: ClientCommandHandler = async ({ positionals, flags,
4141
const record = readDaemonStopIdentity(paths.infoPath);
4242
// Cleanup may walk only a detached daemon's process tree; a host may own unrelated work.
4343
if (flags.clean === true && record.ownership !== 'absent' && record.ownership !== 'detached') {
44-
throw daemonCleanRefusedForUnprovenOwner(paths, record.ownership);
44+
throw daemonCleanRefusedForUnprovenOwner(paths, record);
4545
}
4646
const result = await stopRecordedDaemon(record, { paths, host });
4747
const cleanupOwner = readStopCleanupOwner(record);
@@ -60,37 +60,32 @@ export const daemonCommand: ClientCommandHandler = async ({ positionals, flags,
6060
/** Refuses process-tree cleanup unless the record proves detached ownership. */
6161
function daemonCleanRefusedForUnprovenOwner(
6262
paths: DaemonPaths,
63-
record: DaemonStopIdentity['ownership'],
63+
record: DaemonStopIdentity,
6464
): AppError {
6565
return new AppError(
6666
'COMMAND_FAILED',
6767
'This daemon record does not prove the process is a daemon of its own, so its retained-runner cleanup cannot be run from here.',
6868
{
6969
reason: 'daemon-clean-not-detached',
7070
stateDir: paths.baseDir,
71-
ownership: record,
71+
ownership: record.ownership,
7272
hint: daemonCleanRefusalHint(paths, record),
7373
},
7474
);
7575
}
7676

7777
/**
78-
* What to do next, which depends on whether a plain `daemon stop` has a route left for this record.
79-
* A readable record naming a host or an unfamiliar owner does: that stop asks the running process to
80-
* leave and never signals it. A record that cannot be read has none — the same stop refuses it for the
81-
* same reason — so its hint names the record repair instead of sending the operator to a second refusal.
78+
* Directs host-owned and unrecognized records to their owner's remedy. Only a registration whose
79+
* host has provably exited can be retired by a plain stop. Unreadable records need repair first.
8280
*/
83-
function daemonCleanRefusalHint(
84-
paths: DaemonPaths,
85-
record: DaemonStopIdentity['ownership'],
86-
): string {
87-
if (record === 'unreadable') {
81+
function daemonCleanRefusalHint(paths: DaemonPaths, record: DaemonStopIdentity): string {
82+
if (record.ownership === 'unreadable') {
8883
return `Nothing was signaled. Check what owns that process, then repair the record: stop it by its own means and remove ${shellQuoteIfNeeded(`${paths.baseDir}/daemon.json`)} so a daemon can register again.`;
8984
}
90-
if (record === 'embedded') {
91-
return 'Stop it without --clean, which drains it through its own close, or let the host that started it close it.';
92-
}
93-
return 'Stop it without --clean, which asks the running process to leave and reports what it answers.';
85+
return ownerRefusal(
86+
record.ownership === 'embedded' ? 'embedded-owner' : 'owner-unproven',
87+
record.identity?.pid,
88+
).hint;
9489
}
9590

9691
function renderDaemonStop(

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ for (const [ownership, reason, hint] of [
170170
}
171171
});
172172

173+
test('takeover refuses an embedded registration without a recorded host start time', async () => {
174+
const { paths } = stateDirAndPaths();
175+
await registerDaemon(paths, { httpPort: 1, ownership: 'embedded' });
176+
const record = JSON.parse(fs.readFileSync(paths.infoPath, 'utf8'));
177+
delete record.processStartTime;
178+
fs.writeFileSync(paths.infoPath, JSON.stringify(record));
179+
const info = readDaemonInfo(paths.infoPath);
180+
assert.ok(info);
181+
assert.equal(info.processStartTime, undefined);
182+
const before = fs.readFileSync(paths.infoPath, 'utf8');
183+
const killSpy = vi.spyOn(process, 'kill');
184+
185+
const refusal = await refusalFrom(startup.retireDaemonForTakeover(info, paths));
186+
187+
assert.equal(refusal.details?.reason, DAEMON_LIFECYCLE_REASONS.ownerEmbedded);
188+
assert.match(String(refusal.details?.hint), /owned by host process \d+\. Close it there/);
189+
assert.equal(fs.readFileSync(paths.infoPath, 'utf8'), before);
190+
assert.deepEqual(signalsSent(killSpy), []);
191+
assert.equal(spawn.mock.calls.length, 0);
192+
});
193+
173194
test('the same record with no host owner is still the takeover this client performs', async (t) => {
174195
if (await skipWhenLoopbackUnavailable(t)) return;
175196
// One field differs from the row above, and it changes everything: a detached contender of the wrong

‎src/host.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ test('an outside stop refuses and the host keeps serving until its owner closes
320320
).rejects.toMatchObject({
321321
details: {
322322
reason: 'daemon-owner-embedded',
323-
hint: `The runtime is owned by host process ${process.pid}. Close it there with the handle returned by startAgentDevice(). If the host has exited, use daemon stop or ensureDaemon() for this state directory to recover its abandoned registration.`,
323+
hint: `The runtime is owned by host process ${process.pid}. Close it there with the handle returned by startAgentDevice(). Recovery with daemon stop or ensureDaemon() for this state directory retires the abandoned registration only when the recorded host lifetime has provably exited.`,
324324
},
325325
});
326326
expect(hostSignals(signals)).toEqual([]);

0 commit comments

Comments
 (0)