@@ -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;
2728type 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 */
5857export 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