Skip to content

Commit 266a1ee

Browse files
committed
fix(host-kit): follow workspace subpaths when fingerprinting daemon source
`walkDaemonCodeGraph` followed relative specifiers only, so a source checkout's signature covered whatever the daemon still imported by relative path. That was already lossy and the leaf moves made it wrong: the walker itself, the overlay, the recorded-fill and the stability loop all left the graph, so editing them no longer changed the signature a client compares a running daemon against, and the cache's format guard lost the "the walk invalidates every document" property its comment rests on. Measured from `src/daemon.ts`: 619 modules on main with the walker stamped, 611 after the moves with it gone. Resolve a scoped specifier through the owning workspace package's `exports` map and walk into the file it names. The manifest is stamped, not merely probed, so an `exports` retarget invalidates without either endpoint changing; an uninstalled package is recorded as an absent path. Installed dependencies are still not followed — they change on install, not on edit — and the test is structural rather than a name pattern. The graph is now 1459 modules and ~112ms cold, which is what the stat-validated cache exists to absorb. Regression: five of the six new walker tests fail against the previous walker, including one that stamps the real daemon graph and asserts the walker is in it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BPFjVXwbDrXPqp5W6K2iDK
1 parent c4f5633 commit 266a1ee

4 files changed

Lines changed: 361 additions & 23 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path';
55
import { afterEach, test, vi } from 'vitest';
66
import { computeDaemonCodeSignature } from './code-signature.ts';
77
import { resolveCachedDaemonCodeSignature } from './code-signature-cache.ts';
8+
import { writeWorkspaceFixture } from './code-signature.fixtures.ts';
89
import { mkdtempForTestSync } from './internal/tmp-dir.fixtures.ts';
910

1011
afterEach(() => {
@@ -323,3 +324,29 @@ test('resolveCachedDaemonCodeSignature reports an unreadable entry as unknown',
323324
fs.rmSync(root, { recursive: true, force: true });
324325
}
325326
});
327+
328+
/**
329+
* The cached arm has to see a package-owned edit for the same reason the walk
330+
* does: a source checkout runs most of its own implementation from workspace
331+
* packages, and a client that replays a stale document reuses a daemon running
332+
* superseded code (#2340).
333+
*/
334+
test('resolveCachedDaemonCodeSignature re-walks when a package-owned module changes', () => {
335+
const { root, entryPath, ownedPath } = writeWorkspaceFixture(
336+
'agent-device-signature-cache-workspace-',
337+
);
338+
const cacheHome = path.join(root, 'cache-home');
339+
fs.mkdirSync(cacheHome, { recursive: true });
340+
vi.spyOn(os, 'tmpdir').mockReturnValue(cacheHome);
341+
try {
342+
const initial = resolveCachedDaemonCodeSignature(entryPath, root);
343+
344+
fs.writeFileSync(ownedPath, 'export const owned = 20000;\n', 'utf8');
345+
const changed = resolveCachedDaemonCodeSignature(entryPath, root);
346+
347+
assert.notEqual(changed, initial);
348+
assert.equal(changed, computeDaemonCodeSignature(entryPath, root));
349+
} finally {
350+
fs.rmSync(root, { recursive: true, force: true });
351+
}
352+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { mkdtempForTestSync } from './internal/tmp-dir.fixtures.ts';
4+
5+
/**
6+
* A source checkout that imports its own implementation the way this one does:
7+
* an entry under `src/`, a workspace package under `packages/`, and the
8+
* `node_modules` link that makes `@scope/pkg` name it. The link is what
9+
* separates a workspace package from an installed dependency, so a fixture
10+
* that wrote the directory in place instead would be testing nothing.
11+
*/
12+
export function writeWorkspaceFixture(prefix: string): {
13+
root: string;
14+
entryPath: string;
15+
packageDir: string;
16+
manifestPath: string;
17+
ownedPath: string;
18+
} {
19+
const root = mkdtempForTestSync(prefix);
20+
const entryPath = path.join(root, 'src', 'daemon.ts');
21+
const packageDir = path.join(root, 'packages', 'kit');
22+
const ownedPath = path.join(packageDir, 'src', 'owned.ts');
23+
const manifestPath = path.join(packageDir, 'package.json');
24+
25+
fs.mkdirSync(path.dirname(entryPath), { recursive: true });
26+
fs.mkdirSync(path.dirname(ownedPath), { recursive: true });
27+
fs.writeFileSync(entryPath, "import '@scope/kit/owned';\n", 'utf8');
28+
fs.writeFileSync(ownedPath, 'export const owned = 1;\n', 'utf8');
29+
fs.writeFileSync(
30+
manifestPath,
31+
JSON.stringify({
32+
name: '@scope/kit',
33+
exports: { './owned': { types: './src/owned.ts', default: './src/owned.ts' } },
34+
}),
35+
'utf8',
36+
);
37+
38+
const linkDir = path.join(root, 'node_modules', '@scope');
39+
fs.mkdirSync(linkDir, { recursive: true });
40+
fs.symlinkSync(packageDir, path.join(linkDir, 'kit'), 'dir');
41+
return { root, entryPath, packageDir, manifestPath, ownedPath };
42+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 { computeDaemonCodeSignature, walkDaemonCodeGraph } from './code-signature.ts';
6+
import { writeWorkspaceFixture } from './code-signature.fixtures.ts';
7+
import { mkdtempForTestSync } from './internal/tmp-dir.fixtures.ts';
8+
9+
function labelsOf(entryPath: string, root: string): string[] {
10+
return walkDaemonCodeGraph(entryPath, root).files.map(([label]) => label);
11+
}
12+
13+
test('a workspace subpath is walked, and its file is stamped under the package path', () => {
14+
const { root, entryPath } = writeWorkspaceFixture('agent-device-signature-workspace-');
15+
try {
16+
assert.deepEqual(labelsOf(entryPath, root).sort(), [
17+
'packages/kit/package.json',
18+
'packages/kit/src/owned.ts',
19+
'src/daemon.ts',
20+
]);
21+
} finally {
22+
fs.rmSync(root, { recursive: true, force: true });
23+
}
24+
});
25+
26+
test('an edit to a package-owned module changes the source signature', () => {
27+
const { root, entryPath, ownedPath } = writeWorkspaceFixture('agent-device-signature-edit-');
28+
try {
29+
const before = computeDaemonCodeSignature(entryPath, root);
30+
31+
fs.writeFileSync(ownedPath, 'export const owned = 20000;\n', 'utf8');
32+
33+
assert.notEqual(computeDaemonCodeSignature(entryPath, root), before);
34+
} finally {
35+
fs.rmSync(root, { recursive: true, force: true });
36+
}
37+
});
38+
39+
test('retargeting the exports map moves the edge without either endpoint changing', () => {
40+
const { root, entryPath, packageDir, manifestPath } = writeWorkspaceFixture(
41+
'agent-device-signature-exports-',
42+
);
43+
try {
44+
const before = computeDaemonCodeSignature(entryPath, root);
45+
fs.writeFileSync(path.join(packageDir, 'src', 'other.ts'), 'export const other = 1;\n', 'utf8');
46+
fs.writeFileSync(
47+
manifestPath,
48+
JSON.stringify({
49+
name: '@scope/kit',
50+
exports: { './owned': { types: './src/other.ts', default: './src/other.ts' } },
51+
}),
52+
'utf8',
53+
);
54+
55+
assert.notEqual(computeDaemonCodeSignature(entryPath, root), before);
56+
assert.ok(labelsOf(entryPath, root).includes('packages/kit/src/other.ts'));
57+
} finally {
58+
fs.rmSync(root, { recursive: true, force: true });
59+
}
60+
});
61+
62+
test('an installed dependency is not followed into', () => {
63+
const root = mkdtempForTestSync('agent-device-signature-installed-');
64+
try {
65+
const entryPath = path.join(root, 'src', 'daemon.ts');
66+
const packageDir = path.join(root, 'node_modules', '@scope', 'vendor');
67+
fs.mkdirSync(path.dirname(entryPath), { recursive: true });
68+
fs.mkdirSync(path.join(packageDir, 'src'), { recursive: true });
69+
fs.writeFileSync(entryPath, "import '@scope/vendor/thing';\n", 'utf8');
70+
fs.writeFileSync(path.join(packageDir, 'src', 'thing.js'), 'export const thing = 1;\n', 'utf8');
71+
fs.writeFileSync(
72+
path.join(packageDir, 'package.json'),
73+
JSON.stringify({ name: '@scope/vendor', exports: { './thing': './src/thing.js' } }),
74+
'utf8',
75+
);
76+
77+
assert.deepEqual(labelsOf(entryPath, root), ['src/daemon.ts']);
78+
} finally {
79+
fs.rmSync(root, { recursive: true, force: true });
80+
}
81+
});
82+
83+
test('a workspace package linked after the walk is an absent path, not a silent miss', () => {
84+
const root = mkdtempForTestSync('agent-device-signature-absent-');
85+
try {
86+
const entryPath = path.join(root, 'src', 'daemon.ts');
87+
fs.mkdirSync(path.dirname(entryPath), { recursive: true });
88+
fs.writeFileSync(entryPath, "import '@scope/kit/owned';\n", 'utf8');
89+
90+
assert.ok(
91+
walkDaemonCodeGraph(entryPath, root).absentPaths.includes(
92+
path.join('node_modules', '@scope', 'kit', 'package.json'),
93+
),
94+
);
95+
} finally {
96+
fs.rmSync(root, { recursive: true, force: true });
97+
}
98+
});
99+
100+
/**
101+
* The cache's format guard covers only its own stored shape; it relies on the
102+
* WALK invalidating every document by being inside the graph it walks. That
103+
* held while the walker sat in `src/daemon`, and workspace resolution is what
104+
* keeps it true now that it is a package (#2340). A stamped label is a
105+
* `size:mtime` input to the signature, so membership is the whole claim.
106+
*/
107+
test('the daemon source graph stamps the walker that produced it', () => {
108+
const repoRoot = path.resolve(import.meta.dirname, '..', '..', '..');
109+
const entryPath = path.join(repoRoot, 'src', 'daemon.ts');
110+
if (!fs.existsSync(entryPath)) return;
111+
112+
assert.ok(labelsOf(entryPath, repoRoot).includes('packages/host-kit/src/code-signature.ts'));
113+
});

0 commit comments

Comments
 (0)