Skip to content

Commit 8e78bfa

Browse files
committed
fix(daemon): make the code-signature cache sound across resolution changes
Extensionless specifiers resolve .ts before .js, so adding dep.ts to a graph whose './dep' resolved to dep.js changed a fresh walk while leaving every cached stamp untouched — the stale signature was silently reused. The walk now records every candidate probed-and-missed ahead of a winner (and every candidate of an unresolved specifier) as absent paths; the cache revalidates that each is still missing and re-walks when any appears. Red-first: cached-vs-fresh dep.js -> newly added dep.ts regression, plus lower-precedence appearance staying warm and a nothing-resolving specifier gaining a target.
1 parent ec9b328 commit 8e78bfa

3 files changed

Lines changed: 217 additions & 85 deletions

File tree

‎src/daemon/__tests__/code-signature-cache.test.ts‎

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,31 @@ afterEach(() => {
1717
* buckets on `size:mtime`, not content (see the sibling walk tests), so a
1818
* same-length rewrite can land in the same bucket on a fast filesystem.
1919
*
20+
* `dependency` names how the entry imports that file: extension-full by
21+
* default, so that resolution has exactly one candidate, and extensionless for
22+
* the tests about which candidate wins.
23+
*
2024
* The cache's temporary directory is redirected inside the fixture, so the
2125
* documents a test sees are exactly the ones its own fixture published — never
2226
* a document a parallel worker owns in the run's shared `TMPDIR`.
2327
*/
24-
function writeGraphFixture(prefix: string): {
28+
function writeGraphFixture(
29+
prefix: string,
30+
dependency: { specifier: string; fileName: string } = {
31+
specifier: './dep.ts',
32+
fileName: 'dep.ts',
33+
},
34+
): {
2535
root: string;
2636
entryPath: string;
2737
depPath: string;
2838
cacheHome: string;
2939
} {
3040
const root = mkdtempForTestSync(prefix);
3141
const entryPath = path.join(root, 'src', 'daemon.ts');
32-
const depPath = path.join(root, 'src', 'dep.ts');
42+
const depPath = path.join(root, 'src', dependency.fileName);
3343
fs.mkdirSync(path.dirname(entryPath), { recursive: true });
34-
fs.writeFileSync(entryPath, "import './dep.ts';\n", 'utf8');
44+
fs.writeFileSync(entryPath, `import '${dependency.specifier}';\n`, 'utf8');
3545
fs.writeFileSync(depPath, 'export const dep = 1;\n', 'utf8');
3646
const cacheHome = path.join(root, 'cache-home');
3747
fs.mkdirSync(cacheHome, { recursive: true });
@@ -114,6 +124,76 @@ test('resolveCachedDaemonCodeSignature discovers an import edge added after the
114124
}
115125
});
116126

127+
test('resolveCachedDaemonCodeSignature re-walks when a higher-precedence extension appears', () => {
128+
const { root, entryPath } = writeGraphFixture('agent-device-signature-cache-precedence-', {
129+
specifier: './dep',
130+
fileName: 'dep.js',
131+
});
132+
try {
133+
const initial = resolveCachedDaemonCodeSignature(entryPath, root);
134+
assert.equal(initial, computeDaemonCodeSignature(entryPath, root));
135+
136+
// Resolution probes `.ts` before `.js`, so `./dep` now means this file and
137+
// `dep.js` has left the graph — while every file the cache recorded still
138+
// stamps identically. A cache that only revalidates recorded stamps hands
139+
// back a signature the daemon this client would reuse never reports.
140+
fs.writeFileSync(path.join(root, 'src', 'dep.ts'), 'export const dep = 20000;\n', 'utf8');
141+
142+
const fresh = computeDaemonCodeSignature(entryPath, root);
143+
assert.notEqual(fresh, initial);
144+
assert.equal(resolveCachedDaemonCodeSignature(entryPath, root), fresh);
145+
} finally {
146+
fs.rmSync(root, { recursive: true, force: true });
147+
}
148+
});
149+
150+
test('resolveCachedDaemonCodeSignature stays warm when a lower-precedence extension appears', () => {
151+
const { root, entryPath } = writeGraphFixture('agent-device-signature-cache-behind-', {
152+
specifier: './dep',
153+
fileName: 'dep.ts',
154+
});
155+
try {
156+
const expected = resolveCachedDaemonCodeSignature(entryPath, root);
157+
158+
// `.ts` already won, so resolution never probes this far and the graph is
159+
// unchanged. Recording candidates BEHIND the winner would spend the whole
160+
// point of the cache re-walking edits that cannot move an edge.
161+
fs.writeFileSync(path.join(root, 'src', 'dep.js'), 'export const dep = 20000;\n', 'utf8');
162+
163+
// Uncached first, so the walk this comparison needs is not itself counted
164+
// as a read the cached call made.
165+
assert.equal(computeDaemonCodeSignature(entryPath, root), expected);
166+
167+
const readPaths = spyOnReads();
168+
assert.equal(resolveCachedDaemonCodeSignature(entryPath, root), expected);
169+
assert.deepEqual(
170+
readPaths().filter((target) => target.startsWith(root)),
171+
[],
172+
);
173+
} finally {
174+
fs.rmSync(root, { recursive: true, force: true });
175+
}
176+
});
177+
178+
test('resolveCachedDaemonCodeSignature re-walks when a specifier that resolved to nothing gains a target', () => {
179+
const { root, entryPath } = writeGraphFixture('agent-device-signature-cache-appears-');
180+
try {
181+
fs.appendFileSync(entryPath, "import './later';\n", 'utf8');
182+
const initial = resolveCachedDaemonCodeSignature(entryPath, root);
183+
assert.match(initial, /^graph:2:/);
184+
185+
// The entry is not rewritten to add this edge: the edge was always there,
186+
// and only its target was missing when the document was published.
187+
fs.writeFileSync(path.join(root, 'src', 'later.ts'), 'export const later = 1;\n', 'utf8');
188+
189+
const grown = resolveCachedDaemonCodeSignature(entryPath, root);
190+
assert.match(grown, /^graph:3:/);
191+
assert.equal(grown, computeDaemonCodeSignature(entryPath, root));
192+
} finally {
193+
fs.rmSync(root, { recursive: true, force: true });
194+
}
195+
});
196+
117197
test('resolveCachedDaemonCodeSignature re-walks when a cached graph file is deleted', () => {
118198
const { root, entryPath, depPath } = writeGraphFixture('agent-device-signature-cache-delete-');
119199
try {
@@ -137,10 +217,12 @@ test('resolveCachedDaemonCodeSignature re-walks when a cached graph file is dele
137217
// signature, because a wrong signature restarts a healthy daemon on every
138218
// invocation.
139219
const UNUSABLE_CACHE_DOCUMENTS = [
140-
'{"version":1,"files":[["src/dep.ts",12,34]]',
141-
'{"version":1,"files":[["src/dep.ts"]]}',
142-
'{"version":99,"files":[]}',
143-
'{"version":1,"files":[]}',
220+
'{"version":2,"files":[["src/dep.ts",12,34]],"absent":[]',
221+
'{"version":2,"files":[["src/dep.ts"]],"absent":[]}',
222+
'{"version":2,"files":[["src/daemon.ts",1,2]]}',
223+
'{"version":2,"files":[["src/daemon.ts",1,2]],"absent":[7]}',
224+
'{"version":99,"files":[],"absent":[]}',
225+
'{"version":2,"files":[],"absent":[]}',
144226
'null',
145227
];
146228

@@ -162,7 +244,7 @@ for (const document of UNUSABLE_CACHE_DOCUMENTS) {
162244

163245
test('resolveCachedDaemonCodeSignature republishes over a zero-entry document instead of restarting the daemon forever', () => {
164246
const { root, entryPath, cacheHome } = writeGraphFixture('agent-device-signature-cache-empty-');
165-
const emptyDocument = '{"version":1,"files":[]}';
247+
const emptyDocument = '{"version":2,"files":[],"absent":[]}';
166248
try {
167249
const expected = resolveCachedDaemonCodeSignature(entryPath, root);
168250
const documents = listCacheDocuments(cacheHome);
@@ -197,10 +279,11 @@ test('resolveCachedDaemonCodeSignature refuses a document that omits the entry i
197279
for (const document of documents) {
198280
const parsed = JSON.parse(fs.readFileSync(document, 'utf8')) as {
199281
files: [string, number, number][];
282+
absent: string[];
200283
};
201284
const files = parsed.files.filter(([label]) => label !== path.join('src', 'daemon.ts'));
202285
assert.equal(files.length, parsed.files.length - 1);
203-
fs.writeFileSync(document, JSON.stringify({ version: 1, files }), 'utf8');
286+
fs.writeFileSync(document, JSON.stringify({ ...parsed, files }), 'utf8');
204287
}
205288

206289
assert.equal(resolveCachedDaemonCodeSignature(entryPath, root), expected);

‎src/daemon/code-signature-cache.ts‎

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
formatDaemonCodeSignature,
99
walkDaemonCodeGraph,
1010
type DaemonCodeFileStamp,
11+
type DaemonCodeGraphWalk,
1112
} from './code-signature.ts';
1213

1314
// Bump when the stored shape changes; old documents then miss instead of
1415
// parsing wrong. Only this module needs the guard: `code-signature.ts` is
1516
// itself inside the graph it walks, so changing the WALK invalidates every
1617
// document through the ordinary stamp comparison.
17-
const CACHE_FORMAT_VERSION = 1;
18+
const CACHE_FORMAT_VERSION = 2;
1819
// Per-user, and readable only by that user: `os.tmpdir()` is per-user on
1920
// macOS but the shared `/tmp` on Linux, where a single directory would mean
2021
// whichever uid ran first owns it and every other uid's publish fails EACCES
@@ -27,6 +28,7 @@ const CACHE_DOCUMENT_MODE = 0o600;
2728
type CacheDocument = {
2829
version: typeof CACHE_FORMAT_VERSION;
2930
files: DaemonCodeFileStamp[];
31+
absent: string[];
3032
};
3133

3234
/**
@@ -36,61 +38,66 @@ type CacheDocument = {
3638
* its edges means reading all ~800 files: ~30ms of a ~245ms invocation, for a
3739
* graph that is identical on every invocation but the first one after an edit.
3840
*
39-
* So the walk is cached and revalidated by `statSync` alone. For an edit to a
40-
* file ALREADY in the graph that is exactly as strong as the walk it
41-
* replaces: the signature itself treats a file's `size:mtime` as the stand-in
42-
* for its contents (`DaemonCodeFileStamp`), so a still-matching pair means an
43-
* unchanged file, therefore an unchanged specifier list, therefore an
44-
* unchanged graph. It is deliberately weaker in one direction, because the
45-
* set of files to revalidate is read out of the document: a module that JOINS
46-
* the graph without touching any recorded file — an import whose target did
47-
* not exist when the document was published and is created afterwards — stays
48-
* invisible until some recorded file changes. The cost is a source-checkout
49-
* dev loop reusing a daemon one edit stale; the next edit to any graph file
50-
* republishes.
41+
* So the walk is cached and revalidated by `statSync` alone, which is exactly
42+
* as strong as the walk it replaces because a document records the walk's
43+
* whole input, not just its output. Every recorded file still matching its
44+
* `size:mtime` means an unchanged file — that pair is what the signature
45+
* already treats as the stand-in for contents (`DaemonCodeFileStamp`) —
46+
* therefore an unchanged specifier list; and every path in `absent` still
47+
* being absent means each of those specifiers still resolves where it did
48+
* (`DaemonCodeGraphWalk`). Unchanged specifiers resolving unchanged is an
49+
* unchanged graph, so re-walking could only rediscover the recorded stamps.
5150
*
52-
* Any mismatched, vanished, or newly non-file entry, and any unreadable,
53-
* malformed, foreign, or entry-less cache document, falls back to the full
54-
* walk, which republishes. The cache is a best-effort artifact under
55-
* `os.tmpdir()`, like the Swift toolchain cache: failing to read or write one
56-
* only costs the walk.
51+
* Any mismatched, vanished, or newly non-file entry, any path recorded as
52+
* absent that now exists, and any unreadable, malformed, foreign, or
53+
* entry-less cache document, falls back to the full walk, which republishes.
54+
* The cache is a best-effort artifact under `os.tmpdir()`, like the Swift
55+
* toolchain cache: failing to read or write one only costs the walk.
5756
*/
5857
export function resolveCachedDaemonCodeSignature(entryPath: string, root: string): string {
5958
const cachePath = resolveCachePath(entryPath, root);
6059
const entryLabel = buildDaemonCodeFileLabel(root, entryPath);
61-
const validated = readValidatedStamps(cachePath, root, entryLabel);
62-
if (validated) return formatDaemonCodeSignature(validated);
60+
const validated = readValidatedWalk(cachePath, root, entryLabel);
61+
if (validated) return formatDaemonCodeSignature(validated.files);
6362

64-
let stamps: DaemonCodeFileStamp[];
63+
let walk: DaemonCodeGraphWalk;
6564
try {
66-
stamps = walkDaemonCodeGraph(entryPath, root);
65+
walk = walkDaemonCodeGraph(entryPath, root);
6766
} catch {
6867
return 'unknown';
6968
}
70-
if (describesEntry(stamps, entryLabel)) publishStamps(cachePath, stamps);
71-
return formatDaemonCodeSignature(stamps);
69+
if (describesEntry(walk.files, entryLabel)) publishWalk(cachePath, walk);
70+
return formatDaemonCodeSignature(walk.files);
7271
}
7372

74-
/** The recorded stamps when every one of them still describes the file on disk. */
75-
function readValidatedStamps(
73+
/** The recorded walk when the filesystem still answers every probe the way it did. */
74+
function readValidatedWalk(
7675
cachePath: string,
7776
root: string,
7877
entryLabel: string,
79-
): DaemonCodeFileStamp[] | undefined {
80-
const stamps = readCachedStamps(cachePath);
81-
if (!stamps || !describesEntry(stamps, entryLabel)) return undefined;
82-
for (const [label, size, mtimeMs] of stamps) {
83-
let stat: fs.Stats;
84-
try {
85-
stat = fs.statSync(path.resolve(root, label));
86-
} catch {
87-
return undefined;
88-
}
89-
if (!stat.isFile() || stat.size !== size || Math.trunc(stat.mtimeMs) !== mtimeMs) {
78+
): DaemonCodeGraphWalk | undefined {
79+
const walk = readCachedWalk(cachePath);
80+
if (!walk || !describesEntry(walk.files, entryLabel)) return undefined;
81+
for (const [label, size, mtimeMs] of walk.files) {
82+
const stat = statCandidate(path.resolve(root, label));
83+
if (!stat?.isFile() || stat.size !== size || Math.trunc(stat.mtimeMs) !== mtimeMs) {
9084
return undefined;
9185
}
9286
}
93-
return stamps;
87+
for (const label of walk.absentPaths) {
88+
// A directory is still a resolution miss, so only a FILE appearing here
89+
// moves an edge; that is the same question `walkDaemonCodeGraph` asked.
90+
if (statCandidate(path.resolve(root, label))?.isFile()) return undefined;
91+
}
92+
return walk;
93+
}
94+
95+
function statCandidate(candidatePath: string): fs.Stats | undefined {
96+
try {
97+
return fs.statSync(candidatePath);
98+
} catch {
99+
return undefined;
100+
}
94101
}
95102

96103
/**
@@ -105,20 +112,23 @@ function describesEntry(stamps: readonly DaemonCodeFileStamp[], entryLabel: stri
105112
return stamps.some(([label]) => label === entryLabel);
106113
}
107114

108-
function readCachedStamps(cachePath: string): DaemonCodeFileStamp[] | undefined {
109-
const entries = readCachedEntries(cachePath);
110-
if (!entries) return undefined;
111-
const stamps: DaemonCodeFileStamp[] = [];
112-
for (const entry of entries) {
115+
function readCachedWalk(cachePath: string): DaemonCodeGraphWalk | undefined {
116+
const document = readCachedDocument(cachePath);
117+
if (!document) return undefined;
118+
const files: DaemonCodeFileStamp[] = [];
119+
for (const entry of document.files) {
113120
const stamp = readStamp(entry);
114121
if (!stamp) return undefined;
115-
stamps.push(stamp);
122+
files.push(stamp);
116123
}
117-
return stamps;
124+
if (!document.absent.every((label) => typeof label === 'string')) return undefined;
125+
return { files, absentPaths: document.absent as string[] };
118126
}
119127

120-
/** The document's raw entries, or `undefined` for anything this format cannot own. */
121-
function readCachedEntries(cachePath: string): unknown[] | undefined {
128+
/** The document's raw arrays, or `undefined` for anything this format cannot own. */
129+
function readCachedDocument(
130+
cachePath: string,
131+
): { files: unknown[]; absent: unknown[] } | undefined {
122132
const contents = readOwnedDocument(cachePath);
123133
if (contents === undefined) return undefined;
124134
let parsed: unknown;
@@ -130,7 +140,8 @@ function readCachedEntries(cachePath: string): unknown[] | undefined {
130140
if (!parsed || typeof parsed !== 'object') return undefined;
131141
const document = parsed as Partial<CacheDocument>;
132142
if (document.version !== CACHE_FORMAT_VERSION) return undefined;
133-
return Array.isArray(document.files) ? document.files : undefined;
143+
if (!Array.isArray(document.files) || !Array.isArray(document.absent)) return undefined;
144+
return { files: document.files, absent: document.absent };
134145
}
135146

136147
/** The document's text, but only when this user is the one who wrote it. */
@@ -164,8 +175,12 @@ function readStamp(entry: unknown): DaemonCodeFileStamp | undefined {
164175
return [label, size, mtimeMs];
165176
}
166177

167-
function publishStamps(cachePath: string, files: DaemonCodeFileStamp[]): void {
168-
const document: CacheDocument = { version: CACHE_FORMAT_VERSION, files };
178+
function publishWalk(cachePath: string, walk: DaemonCodeGraphWalk): void {
179+
const document: CacheDocument = {
180+
version: CACHE_FORMAT_VERSION,
181+
files: [...walk.files],
182+
absent: [...walk.absentPaths],
183+
};
169184
try {
170185
fs.mkdirSync(path.dirname(cachePath), { recursive: true, mode: CACHE_DIRECTORY_MODE });
171186
publishFileSync({

0 commit comments

Comments
 (0)