Skip to content

Commit 5ac0baf

Browse files
committed
fix(daemon): take the owner identity from host-kit and refuse unproven records
Address review on #3102. The daemon asserted its own ownership identity with a second copy of the host-kit constructor, so the daemon.json fence and the device-claim fence could drift apart; it now calls readCurrentOwnerIdentity() like every other ownership fence does. That exposed a weaker rule than the issue needs: two records that both failed to read a start time agreed on nothing, and agreeing on nothing was reported as a match — the pid-only fallback the fence exists to refuse. Ownership now returns a third verdict, unproven, for a record that matches on pid and can be proved neither way. removeInfoOwnedBy declines it, because leaving a record a dead pid left behind costs a client one liveness probe while deleting a successor's metadata is unrecoverable; the loss watch declines it too, since nothing proves a takeover happened. Reporting it as `replaced` would announce an event with no evidence of one. Also from review: the startup-failure exit now tears the loss watch down before touching daemon.json, so a failed runtime cannot report a loss for a record it just removed itself; the watch stops when the state dir is pruned rather than polling a directory that no longer exists; the state `decodable` is renamed `ownerless`, because it names the record that has no owner rather than the one that fails to decode; and the dead readProcessStartTime re-export is gone.
1 parent 183da83 commit 5ac0baf

9 files changed

Lines changed: 143 additions & 71 deletions

‎.github/workflows/ios.yml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ jobs:
164164
# runner lives in the daemon this step leaves running, and a per-step state dir would rebuild it
165165
# per step. That also means this daemon outlives the step and can be superseded while it lives,
166166
# which used to make its 5-minute idle reap delete the metadata of the daemon then serving
167-
# clients (#3087). It now removes daemon.json only when the file names its own pid — the rule
168-
# releasing the lock already applied — so the reap costs nothing but a daemon.log line.
167+
# clients (#3087). It now removes daemon.json only when the file names its own pid and process
168+
# start time, so a recycled pid cannot be mistaken for the owner, and the reap costs nothing
169+
# but a daemon.log line.
169170
- name: Preflight iOS runner through public CLI
170171
run: |
171172
pnpm clean:daemon

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,25 @@ test('ownership is decided by the identity in the record, not by its pid alone',
9898
publishDaemonRegistration(stateDir, owner);
9999
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), { state: 'match' });
100100

101-
publishDaemonRegistration(stateDir, { pid: 4242, startTime: 'successor-start' });
102-
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), {
103-
state: 'replaced',
104-
identity: { pid: 4242, startTime: 'successor-start' },
101+
// Two unreadable start times agree, and agreeing on nothing is not proof of ownership. Nor is this
102+
// a proved takeover, so it lands between the two rather than defaulting either way.
103+
publishDaemonRegistration(stateDir, { pid: process.pid, startTime: null });
104+
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, { pid: process.pid, startTime: null }), {
105+
state: 'unproven',
105106
});
106107

107-
// A recycled pid is a different process, and the only proof available is the start time.
108+
// Same pid with a readable start time on the record but not on this side is still unproven: the
109+
// proof of a recycle has to run on two readable birth times, and `ownerIdentityDiffers` is one-way
110+
// by design so no caller mistakes one owner for two.
108111
publishDaemonRegistration(stateDir, { pid: process.pid, startTime: 'recycled' });
112+
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, { pid: process.pid, startTime: null }), {
113+
state: 'unproven',
114+
});
115+
116+
publishDaemonRegistration(stateDir, { pid: 4242, startTime: 'successor-start' });
109117
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), {
110118
state: 'replaced',
111-
identity: { pid: process.pid, startTime: 'recycled' },
119+
identity: { pid: 4242, startTime: 'successor-start' },
112120
});
113121
});
114122

@@ -120,19 +128,32 @@ test('ownership names each way a record yields no owner', () => {
120128
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), { state: 'absent' });
121129

122130
fs.writeFileSync(infoPath, '{not json');
123-
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), { state: 'decodable' });
131+
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), { state: 'ownerless' });
124132

125133
fs.writeFileSync(infoPath, JSON.stringify({ pid: 4242 }));
126134
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), {
127135
state: 'replaced',
128136
identity: { pid: 4242, startTime: null },
129137
});
130-
131-
if (process.getuid?.() === 0) return;
132-
fs.chmodSync(infoPath, 0o000);
133-
try {
134-
assert.deepEqual(readRegisteredDaemonOwnership(infoPath, owner), { state: 'unreadable' });
135-
} finally {
136-
fs.chmodSync(infoPath, 0o600);
137-
}
138138
});
139+
140+
test.skipIf(process.getuid?.() === 0)(
141+
'a registration this process cannot read is unreadable, not absent',
142+
() => {
143+
// EACCES leaves the file on disk: only a root-owned CI host reads mode 000 anyway, and reading it
144+
// there — or reading the failure as absence — would repeat #3087 from the other side.
145+
const infoPath = infoPathOf(useStateDir());
146+
fs.writeFileSync(infoPath, JSON.stringify({ pid: process.pid, processStartTime: 'ours' }));
147+
fs.chmodSync(infoPath, 0o000);
148+
try {
149+
assert.deepEqual(
150+
readRegisteredDaemonOwnership(infoPath, { pid: process.pid, startTime: 'ours' }),
151+
{
152+
state: 'unreadable',
153+
},
154+
);
155+
} finally {
156+
fs.chmodSync(infoPath, 0o600);
157+
}
158+
},
159+
);

‎src/daemon/daemon-registration.ts‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function readRegistration(
2424
infoPath: string,
2525
):
2626
| Readonly<{ status: 'registered'; identity: OwnerIdentity }>
27-
| Readonly<{ status: 'absent' | 'unreadable' | 'decodable' }> {
27+
| Readonly<{ status: 'absent' | 'unreadable' | 'ownerless' }> {
2828
let raw: string;
2929
try {
3030
raw = fs.readFileSync(infoPath, 'utf8');
@@ -41,10 +41,10 @@ function readRegistration(
4141
try {
4242
parsed = parseRegistration(JSON.parse(raw) as { pid?: unknown; processStartTime?: unknown });
4343
} catch {
44-
return { status: 'decodable' };
44+
return { status: 'ownerless' };
4545
}
4646
return parsed.pid === null
47-
? { status: 'decodable' }
47+
? { status: 'ownerless' }
4848
: { status: 'registered', identity: { pid: parsed.pid, startTime: parsed.startTime } };
4949
}
5050

@@ -62,28 +62,37 @@ function parseRegistration(parsed: {
6262
/**
6363
* Whether `daemon.json` still names `owner` as the daemon serving its state dir, in one read.
6464
*
65-
* The comparison is the proof-oriented one this repo uses elsewhere: a pid cannot tell the owner from
66-
* the process that recycled its number, so a match needs a start time readable on both sides to
67-
* agree. A record that does not prove itself is `replaced`, never a default of "still us", because
68-
* every caller acts by removing or reporting.
65+
* Three verdicts, because ownership here is proved rather than assumed, and a pid alone proves
66+
* nothing: it cannot tell the owner from the process that recycled its number. `match` needs a start
67+
* time readable on both sides to agree, since every caller acts on it by deleting the record.
68+
* `replaced` is the opposite proof, from {@link ownerIdentityDiffers}. `unproven` is neither, and is
69+
* kept distinct from `replaced` because it describes a record this daemon cannot place — ours by pid
70+
* and unreadable by birth — so a caller that reports a takeover would be reporting an event that did
71+
* not happen.
6972
*
70-
* `absent`, `unreadable`, and `decodable` stay separate: only `absent` describes a record somebody
71-
* removed, while a file that is present but unreadable or unparseable is still on disk.
73+
* `absent`, `unreadable`, and `ownerless` stay separate too: only `absent` describes a record somebody
74+
* removed, while a file that is present but unreadable or names no owner is still on disk.
7275
*/
7376
export type RegisteredDaemonOwnership =
7477
| Readonly<{ state: 'match' }>
7578
| Readonly<{ state: 'replaced'; identity: OwnerIdentity }>
76-
| Readonly<{ state: 'absent' | 'unreadable' | 'decodable' }>;
79+
| Readonly<{ state: 'unproven' | 'absent' | 'unreadable' | 'ownerless' }>;
7780

7881
export function readRegisteredDaemonOwnership(
7982
infoPath: string,
8083
owner: OwnerIdentity,
8184
): RegisteredDaemonOwnership {
8285
const record = readRegistration(infoPath);
8386
if (record.status !== 'registered') return { state: record.status };
84-
return ownerIdentityMatches(record.identity, owner)
85-
? { state: 'match' }
86-
: { state: 'replaced', identity: record.identity };
87+
if (ownerIdentityDiffers(record.identity, owner)) {
88+
return { state: 'replaced', identity: record.identity };
89+
}
90+
// Two missing start times agree, and agreeing on nothing is not ownership: this is the pid-only
91+
// fallback the fence exists to refuse, so it falls to `unproven` rather than to `match`.
92+
if (ownerIdentityMatches(record.identity, owner) && record.identity.startTime !== null) {
93+
return { state: 'match' };
94+
}
95+
return { state: 'unproven' };
8796
}
8897

8998
function readableStartTime(value: unknown): string | null {

‎src/daemon/server/daemon-metadata-loss.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ describe('readDaemonMetadataLoss', () => {
8585
},
8686
);
8787

88+
test('a record that agrees on pid alone is ours by neither proof, so it is not reported lost', () => {
89+
// The shape a host whose start-time probe failed publishes. Reporting it would announce a takeover
90+
// with no evidence of one, while `removeInfoOwnedBy` refuses the same record: the two callers read
91+
// one verdict, and neither gets to default in its own direction.
92+
const { stateDir, infoPath } = scratch();
93+
publish(stateDir, OWN_PID, null);
94+
95+
assert.equal(readDaemonMetadataLoss({ infoPath, stateDir, owner: OWN }), undefined);
96+
});
97+
8898
test('a corrupt record is not evidence that the registration was taken', () => {
8999
const { stateDir, infoPath } = scratch();
90100
fs.writeFileSync(infoPath, '{not json');
@@ -132,6 +142,28 @@ describe('watchDaemonMetadataLoss', () => {
132142
}
133143
});
134144

145+
test('a pruned state dir ends the watch instead of polling a directory that no longer exists', () => {
146+
vi.useFakeTimers();
147+
try {
148+
const { stateDir, infoPath } = scratch();
149+
publish(stateDir, OWN_PID, 'own-start');
150+
const watch = startWatch(stateDir, infoPath);
151+
152+
fs.rmSync(stateDir, { recursive: true, force: true });
153+
watch.advance();
154+
assert.deepEqual(watch.losses, [], 'a pruned dir is not a loss to report');
155+
156+
// The dir is gone, so a later record under that path cannot be this daemon's metadata to lose.
157+
fs.mkdirSync(stateDir, { recursive: true });
158+
publish(stateDir, FOREIGN_PID, 'successor-start');
159+
watch.advance();
160+
assert.deepEqual(watch.losses, [], 'the watch stopped at the prune');
161+
fs.rmSync(stateDir, { recursive: true, force: true });
162+
} finally {
163+
vi.useRealTimers();
164+
}
165+
});
166+
135167
test('a watch cancelled at shutdown stops reporting', () => {
136168
vi.useFakeTimers();
137169
try {

‎src/daemon/server/daemon-metadata-loss.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@ export type DaemonMetadataLoss = Readonly<{
1515
* Which ownership findings are a loss. A table so a state added to `RegisteredDaemonOwnership` has
1616
* to declare itself here rather than default its way into a report that also cancels the watch.
1717
*
18-
* A record that is present but unreadable or unparseable is NOT a loss: the file is on disk and this
19-
* read learned nothing about its owner, so nothing proves it was taken.
18+
* A record that is present but unreadable or names no owner is NOT a loss: the file is on disk and
19+
* this read learned nothing about its owner, so nothing proves it was taken.
2020
*/
2121
const REPORTED_LOSSES: Readonly<Record<RegisteredDaemonOwnership['state'], boolean>> =
2222
Object.freeze({
2323
match: false,
2424
replaced: true,
2525
absent: true,
2626
unreadable: false,
27-
decodable: false,
27+
ownerless: false,
28+
unproven: false,
2829
});
2930

3031
/**
@@ -73,6 +74,12 @@ export function watchDaemonMetadataLoss(params: {
7374
timer = undefined;
7475
};
7576
const poll = (): void => {
77+
// A pruned state dir ends the watch on its own terms: there is no record left to lose, and a
78+
// daemon whose directory the operator removed has nothing left to report about it.
79+
if (stateDirGone(params.stateDir)) {
80+
cancel();
81+
return;
82+
}
7683
const loss = readDaemonMetadataLoss(params);
7784
if (!loss) return;
7885
cancel();

‎src/daemon/server/daemon-runtime-metadata-ownership.test.ts‎

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ test('a shutdown whose daemon.json names a successor keeps the file and logs the
108108
await runtime?.shutdown();
109109

110110
expect(exits).toEqual([0]);
111+
expect(lifecycleEvents).toContain('gateway-shutdown');
111112
expect(fs.existsSync(paths.infoPath), 'the serving daemon keeps its metadata').toBe(true);
112113
expect(fs.existsSync(paths.lockPath), 'the exiting daemon still releases its own lock').toBe(
113114
false,
@@ -141,28 +142,6 @@ test('a shutdown that still owns its daemon.json removes it without a decline',
141142
}
142143
});
143144

144-
test('a shutdown whose record a successor recycled with the same pid keeps the file', async () => {
145-
// The pid is not an identity: a successor that reuses it after the exiting daemon's start time is a
146-
// different process, and removing its record is the same failure the issue describes.
147-
const stateDir = mkdtempForTestSync('agent-device-daemon-info-pid-recycled-');
148-
const paths = resolveDaemonPaths(stateDir);
149-
try {
150-
const runtime = await startRuntime(stateDir, () => {});
151-
expect(runtime).not.toBeNull();
152-
const own = JSON.parse(fs.readFileSync(paths.infoPath, 'utf8')) as Record<string, unknown>;
153-
154-
fs.writeFileSync(
155-
paths.infoPath,
156-
JSON.stringify({ ...own, pid: process.pid, processStartTime: 'recycled-start' }),
157-
);
158-
await runtime?.shutdown();
159-
160-
expect(fs.existsSync(paths.infoPath), 'a recycled pid is not the same owner').toBe(true);
161-
} finally {
162-
fs.rmSync(stateDir, { recursive: true, force: true });
163-
}
164-
});
165-
166145
test('a startup that fails before publication removes no record and writes no log', async () => {
167146
// The other removal site. Client cleanup deletes daemon.json routinely, so this path must stay
168147
// silent: reporting an "absent" record here would create a daemon.log on every such startup.
@@ -239,3 +218,17 @@ test('the loss watch is armed only after this daemon publishes its own record',
239218
expect(published).toBeGreaterThanOrEqual(0);
240219
expect(armed).toBeGreaterThan(published);
241220
});
221+
222+
test('both exits tear the watch down before they touch daemon.json', () => {
223+
// A watch still armed when the removal runs can report a loss for a record its own process just
224+
// deleted. The startup-failure half of this is unreachable from a test — everything after
225+
// publication needs a real toolchain — so the invariant is read off the source, the same way the
226+
// arming order above is.
227+
const source = fs.readFileSync(new URL('./daemon-runtime.ts', import.meta.url), 'utf8');
228+
const stopped = source.indexOf('stopMetadataLossWatch();');
229+
const removal = source.indexOf('await removeOwnDaemonInfo(');
230+
231+
expect(stopped).toBeGreaterThanOrEqual(0);
232+
expect(removal).toBeGreaterThan(stopped);
233+
expect(source.match(/stopMetadataLossWatch\(\);/g)).toHaveLength(2);
234+
});

‎src/daemon/server/daemon-runtime.ts‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ import {
6060
import {
6161
createOwnedProcessRecordStore,
6262
type OwnedProcessRecordStore,
63+
readCurrentOwnerIdentity,
6364
reapOwnedProcessRecordsAtStartup,
65+
type OwnerIdentity,
6466
} from '@agent-device/host-kit/process';
6567
import { isEnvTruthy, sleep } from '@agent-device/host-kit/retry';
6668

6769
import {
6870
acquireDaemonLock,
6971
parseIntegerEnv,
70-
readProcessStartTime,
7172
readVersion,
72-
type OwnerIdentity,
7373
releaseDaemonLock,
7474
removeInfoOwnedBy,
7575
resolveDaemonCodeOrigin,
@@ -240,14 +240,6 @@ export async function flushDaemonStartupDiagnostics(
240240
);
241241
}
242242

243-
/**
244-
* The identity this process asserts as the owner of `daemon.json`. A bare pid is not ownership: after a
245-
* pid is recycled it names an unrelated process, so the start time is what makes the pair unique.
246-
*/
247-
function createDaemonOwnerIdentity(): OwnerIdentity {
248-
return { pid: process.pid, startTime: readProcessStartTime(process.pid) ?? null };
249-
}
250-
251243
/**
252244
* Records one daemon-level event. These run outside any request, so there is no request scope and no
253245
* resolved debug level to inherit; debug is forced on for the same reason the #2681 handoff forces it —
@@ -351,7 +343,7 @@ export async function startDaemonRuntime(
351343
const screenRecordingAdmissionLedger = createScreenRecordingAdmissionLedger();
352344
const version = readVersion();
353345
const token = crypto.randomBytes(24).toString('hex');
354-
const daemonIdentity = createDaemonOwnerIdentity();
346+
const daemonIdentity = readCurrentOwnerIdentity();
355347
const daemonProcessStartTime = daemonIdentity.startTime ?? undefined;
356348
const daemonCodeOrigin = resolveDaemonCodeOrigin();
357349
const daemonCodeSignature = resolveDaemonCodeSignature();
@@ -741,6 +733,7 @@ export async function startDaemonRuntime(
741733
const appErr = asAppError(error);
742734
stderr.write(`Daemon error: ${appErr.message}\n`);
743735
closeServersBestEffort(servers);
736+
stopMetadataLossWatch();
744737
await removeOwnDaemonInfo({ infoPath, logPath, owner: daemonIdentity });
745738
releaseDaemonLock(lockPath);
746739
await platformDaemonLifecycleOwners.clearDaemonLockConfiguration();

‎src/daemon/server/server-lifecycle.test.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import fs from 'node:fs';
33
import path from 'node:path';
44
import { test, vi } from 'vitest';
55
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
6-
import { removeInfoOwnedBy, writeInfo, type OwnerIdentity } from './server-lifecycle.ts';
6+
import type { OwnerIdentity } from '@agent-device/host-kit/process';
7+
import { removeInfoOwnedBy, writeInfo } from './server-lifecycle.ts';
78
import { readRegisteredDaemonOwnership } from '../daemon-registration.ts';
89

910
const OWN_PID = process.pid;
@@ -71,6 +72,18 @@ test('a record recycling our pid after our start time is not ours to remove', ()
7172
assert.equal(fs.existsSync(infoPath), true);
7273
});
7374

75+
test('a record agreeing on pid alone is refused, not removed on the strength of the pid', () => {
76+
// One side cannot read its own start time, so nothing proves this record is ours. Removing it would
77+
// be the pid-only rule the fence exists to replace; keeping it costs a client one liveness probe.
78+
const [, infoPath] = writeRegistration(OWN_PID, null);
79+
80+
assert.deepEqual(removeInfoOwnedBy(infoPath, OWN_IDENTITY), {
81+
removed: false,
82+
reason: 'unproven',
83+
});
84+
assert.equal(fs.existsSync(infoPath), true);
85+
});
86+
7487
test('an absent registration is nothing to remove', () => {
7588
const [, infoPath] = scratchInfoPath();
7689

@@ -82,7 +95,7 @@ test('a corrupt or pid-less registration names no owner, so it is refused and ke
8295
fs.writeFileSync(corrupt, '{not json');
8396
assert.deepEqual(removeInfoOwnedBy(corrupt, OWN_IDENTITY), {
8497
removed: false,
85-
reason: 'decodable',
98+
reason: 'ownerless',
8699
});
87100
assert.equal(
88101
fs.existsSync(corrupt),
@@ -95,7 +108,7 @@ test('a corrupt or pid-less registration names no owner, so it is refused and ke
95108
fs.writeFileSync(pidLess, JSON.stringify({ pid, token: 'token' }));
96109
assert.deepEqual(removeInfoOwnedBy(pidLess, OWN_IDENTITY), {
97110
removed: false,
98-
reason: 'decodable',
111+
reason: 'ownerless',
99112
});
100113
assert.equal(fs.existsSync(pidLess), true);
101114
}

0 commit comments

Comments
 (0)