Skip to content

Commit 0a47c4e

Browse files
authored
fix(daemon): let an installed version stand as its whole code identity (callstack#2569)
* fix(daemon): let an installed version stand as its whole code identity Two installs of one published version signed their identical bytes differently, because the fingerprint is size:mtime and an installer stamps a fresh mtime on every file. The second client therefore read "code-signature mismatch" and replaced a daemon that already ran exactly its code, dropping the live session on it (callstack#2458). An installed tree's version already fixes its bytes, so it now answers with no fingerprint and the version alone decides reuse. A source checkout, where code does move under an unchanged version, keeps the check. The predicate that tells the two trees apart moves beside the project-root walk it shares, and the takeover ladder moves beside the answer it consumes so the order lives in one place. The packaging gate proves the premise against a clean install: the published tree ships no src/daemon.ts. Closes callstack#2458 * fix(daemon): scope the fingerprint bypass to two installed trees An installed client declined the code comparison on the strength of its own tree alone, which left whatever is already running unverified: a source checkout that shares an explicit --state-dir can publish any code it likes under a published version string, and the installed client would have run it (callstack#2458 review). A daemon now records which tree it started from beside its signature, and the bypass is pairwise — it holds only where both sides say they are installs of that version. A checkout still judges a checkout by signature; a pair whose trees cannot match at all restarts the daemon rather than run code it cannot identify, which is also the rule a daemon too old to report an origin is judged by. Closes callstack#2458
1 parent 6db1a42 commit 0a47c4e

16 files changed

Lines changed: 498 additions & 68 deletions

File tree

‎packages/host-kit/src/code-signature.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import crypto from 'node:crypto';
22
import fs from 'node:fs';
33
import path from 'node:path';
4-
import { findProjectRoot } from './version.ts';
4+
import { findProjectRoot, isSourceCheckoutProjectRoot } from './version.ts';
55

66
// Any quoted, relative-path-shaped string literal is treated as a module
77
// specifier, rather than matching the `import`/`export`/`from` grammar
@@ -61,6 +61,23 @@ export function resolveDaemonCodeSignature(): string {
6161
return computeDaemonCodeSignature(entryPath);
6262
}
6363

64+
/**
65+
* Which tree a daemon's code came from, and so what could have changed it since the
66+
* version it reports.
67+
*
68+
* A signature only means something against a tree that can be re-read, so this answers
69+
* first: two installed trees of one version hold the same artifact and have nothing to
70+
* compare (#2458), while a checkout's code moves under a version that does not move
71+
* with it. A running daemon publishes its own answer beside its signature
72+
* (`src/daemon/server/server-lifecycle.ts`) because the question is about the code that
73+
* is RUNNING, not only about the client asking it.
74+
*/
75+
export type DaemonCodeOrigin = 'installed' | 'checkout';
76+
77+
export function resolveDaemonCodeOrigin(root: string = findProjectRoot()): DaemonCodeOrigin {
78+
return isSourceCheckoutProjectRoot(root) ? 'checkout' : 'installed';
79+
}
80+
6481
export function computeDaemonCodeSignature(
6582
entryPath: string,
6683
root: string = findProjectRoot(),
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import { test } from 'vitest';
5+
import { mkdtempForTestSync } from './tmp-dir.fixtures.ts';
6+
import { isSourceCheckoutProjectRoot, resolveAgentDeviceProjectRoot } from './project-root.ts';
7+
8+
/**
9+
* The shape that separates a tree whose code can be rebuilt under its own version
10+
* from an installed copy of a published one (`daemon-launch-spec.ts` stops asking a
11+
* running daemon for its fingerprint on the installed side of that line).
12+
*/
13+
test('an installed package root is not a source checkout', () => {
14+
const root = mkdtempForTestSync('agent-device-installed-root-');
15+
try {
16+
fs.writeFileSync(path.join(root, 'package.json'), '{"name":"agent-device"}\n', 'utf8');
17+
fs.mkdirSync(path.join(root, 'dist', 'src', 'internal'), { recursive: true });
18+
fs.writeFileSync(path.join(root, 'dist', 'src', 'internal', 'daemon.js'), '', 'utf8');
19+
20+
assert.equal(isSourceCheckoutProjectRoot(root), false);
21+
} finally {
22+
fs.rmSync(root, { recursive: true, force: true });
23+
}
24+
});
25+
26+
test('a checkout carrying the daemon source is a source checkout', () => {
27+
const root = mkdtempForTestSync('agent-device-source-root-');
28+
try {
29+
fs.writeFileSync(path.join(root, 'package.json'), '{"name":"agent-device"}\n', 'utf8');
30+
fs.mkdirSync(path.join(root, 'src'), { recursive: true });
31+
fs.writeFileSync(path.join(root, 'src', 'daemon.ts'), 'export {};\n', 'utf8');
32+
33+
assert.equal(isSourceCheckoutProjectRoot(root), true);
34+
} finally {
35+
fs.rmSync(root, { recursive: true, force: true });
36+
}
37+
});
38+
39+
test('a root with neither a manifest nor a daemon source is neither', () => {
40+
const root = mkdtempForTestSync('agent-device-bare-root-');
41+
try {
42+
assert.equal(isSourceCheckoutProjectRoot(root), false);
43+
44+
fs.mkdirSync(path.join(root, 'src'), { recursive: true });
45+
fs.writeFileSync(path.join(root, 'src', 'daemon.ts'), 'export {};\n', 'utf8');
46+
assert.equal(
47+
isSourceCheckoutProjectRoot(root),
48+
false,
49+
'a daemon source alone does not make a checkout',
50+
);
51+
} finally {
52+
fs.rmSync(root, { recursive: true, force: true });
53+
}
54+
});
55+
56+
test('this checkout is the source checkout its resolver reports', () => {
57+
assert.equal(isSourceCheckoutProjectRoot(resolveAgentDeviceProjectRoot(process.cwd())), true);
58+
});

‎packages/host-kit/src/internal/project-root.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,24 @@ export function resolveAgentDeviceProjectRoot(startDirectory: string): string {
1717
}
1818
return nearest ?? startDirectory;
1919
}
20+
21+
/**
22+
* The daemon's own source, relative to a project root. One path answers two questions
23+
* with it: whether a tree is a source checkout at all, and which entry a checkout
24+
* launches its daemon from (`src/daemon-client/daemon-launch-spec.ts`).
25+
*/
26+
export const DAEMON_SOURCE_ENTRY = 'src/daemon.ts';
27+
28+
/**
29+
* Whether `root` is a source checkout rather than an installed copy of a published
30+
* version. The published package ships its `bin` and `dist` and no `src`, so the
31+
* daemon's own source is what tells the two apart — and that is the difference between
32+
* a tree whose code can be rebuilt under its own version and one whose version already
33+
* fixes its bytes.
34+
*/
35+
export function isSourceCheckoutProjectRoot(root: string): boolean {
36+
return (
37+
fs.existsSync(path.join(root, 'package.json')) &&
38+
fs.existsSync(path.join(root, DAEMON_SOURCE_ENTRY))
39+
);
40+
}

‎packages/host-kit/src/version.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { findProjectRoot, readVersion } from './internal/version.ts';
2+
export { DAEMON_SOURCE_ENTRY, isSourceCheckoutProjectRoot } from './internal/project-root.ts';

‎scripts/check-package.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* 3. runtime dependency closure — every bare specifier the shipped files import is a Node builtin
1212
* or a declared `dependencies` entry, and every declared entry is actually imported.
1313
* 4. every `exports` subpath imports, and the `bin` runs, from outside the workspace.
14+
* 5. the installed tree carries no daemon source, which is what lets an installed
15+
* client treat its version as the whole of its code identity (#2458).
1416
*
1517
* Step 3 is the static half and step 4 the runtime half of the same question: nothing the package
1618
* imports may depend on workspace linking. Keep both — a specifier reachable only through a lazy
@@ -27,6 +29,7 @@ import {
2729
type PackedManifest as PackedDependencies,
2830
} from './lib/shipped-imports.ts';
2931
import { assertInstalledSnapshotBridge } from './size-report-install.mjs';
32+
import { DAEMON_SOURCE_ENTRY, isSourceCheckoutProjectRoot } from '@agent-device/host-kit/version';
3033

3134
type PackedManifest = PackedDependencies & {
3235
exports: Record<string, unknown>;
@@ -122,8 +125,25 @@ function installIntoCleanConsumer(tarball: string): string {
122125
return path.join(consumerDir, 'node_modules', 'agent-device');
123126
}
124127

125-
/** Imports every documented entry point in one process so a failure names the subpath that broke. */
126-
function importEveryExport(manifest: PackedManifest): void {
128+
/**
129+
* An installed client treats its version as the whole of its code identity, because a
130+
* published tree carries no daemon source to rebuild from (`daemon-launch-spec.ts`).
131+
* This is the only check here that sees the tree npm would actually install, so it is
132+
* where that premise gets proven (#2458).
133+
*/
134+
function assertInstalledTreeIsNotASourceCheckout(installedRoot: string): void {
135+
if (isSourceCheckoutProjectRoot(installedRoot)) {
136+
throw new Error(
137+
`${installedRoot} ships ${DAEMON_SOURCE_ENTRY}, so installs of one version would ` +
138+
"fingerprint their code again and replace each other's running daemon (#2458).",
139+
);
140+
}
141+
step('Confirmed the installed tree carries no daemon source, so its version pins its code.');
142+
}
143+
144+
/** Imports every documented entry point in one process so a failure names the subpath that broke. */ function importEveryExport(
145+
manifest: PackedManifest,
146+
): void {
127147
const specifiers = Object.keys(manifest.exports).map((subpath) =>
128148
path.posix.join('agent-device', subpath),
129149
);
@@ -182,6 +202,7 @@ try {
182202
lintTarball(tarball);
183203
const installedRoot = installIntoCleanConsumer(tarball);
184204
assertInstalledSnapshotBridge(installedRoot);
205+
assertInstalledTreeIsNotASourceCheckout(installedRoot);
185206
if (verifySnapshotBridgePreparation) {
186207
if (process.platform !== 'darwin') {
187208
throw new Error('--verify-snapshot-bridge-preparation requires macOS and Xcode.');
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import assert from 'node:assert/strict';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import { test } from 'vitest';
5+
import type { DaemonCodeOrigin } from '@agent-device/host-kit/code-signature';
6+
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
7+
import { writeInfo } from '../../daemon/server/server-lifecycle.ts';
8+
import { readDaemonInfo } from '../daemon-client-metadata.ts';
9+
10+
// The reuse decision is only as good as the identity that survives the round trip
11+
// through `daemon.json`: a client cannot compare what the file lost (#2458).
12+
13+
function scratchStateDir(): [stateDir: string, infoPath: string] {
14+
const stateDir = mkdtempForTestSync('agent-device-daemon-identity-');
15+
return [stateDir, path.join(stateDir, 'daemon.json')];
16+
}
17+
18+
function publishInfo(codeOrigin: DaemonCodeOrigin): string {
19+
const [stateDir, infoPath] = scratchStateDir();
20+
writeInfo(stateDir, infoPath, path.join(stateDir, 'daemon.log'), {
21+
httpPort: 41_234,
22+
token: 'local-secret',
23+
version: '0.0.0-test',
24+
codeOrigin,
25+
codeSignature: 'graph:1:abc',
26+
processStartTime: 'start',
27+
});
28+
return infoPath;
29+
}
30+
31+
test('a daemon publishes the code origin its client reads back', () => {
32+
for (const codeOrigin of ['installed', 'checkout'] as const) {
33+
assert.equal(readDaemonInfo(publishInfo(codeOrigin))?.codeOrigin, codeOrigin);
34+
}
35+
});
36+
37+
test('a registration this version did not write reads back unreported', () => {
38+
// Shaped like a daemon published before the field existed, and like a value no
39+
// version of this writer produces.
40+
for (const contents of [
41+
{ httpPort: 41_234, token: 'local-secret', pid: 7 },
42+
{ httpPort: 41_234, token: 'local-secret', pid: 7, codeOrigin: 'something-else' },
43+
]) {
44+
const [, infoPath] = scratchStateDir();
45+
fs.writeFileSync(infoPath, JSON.stringify(contents));
46+
47+
assert.equal(readDaemonInfo(infoPath)?.codeOrigin, undefined);
48+
}
49+
});

0 commit comments

Comments
 (0)