@exodus/stasis-core 1.0.0-beta.15 → 1.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/state.js CHANGED
@@ -10,54 +10,35 @@ import { Lockfile } from './lockfile.js'
10
10
  import { parseShard, serializeShard } from './shard.js'
11
11
  import { bundledIntegrity, canonicalizePath, inScope, sha512integrity, readFileSyncMaybe, noupsert, realContained } from './state-util.js'
12
12
  import { compressBundleText, parseBundleBytes } from './brotli.js'
13
- import { CODE_EXTENSIONS, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, fileMapToObject, hasNodeModulesSegment, importTargetsEqual, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isPlainObject, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, objectToMaps, observeExecutable, pathExt, reconcileFormat, relativeEscapes, sortPaths, splitNodeModulesPath, toPosix, withMetadataOf } from './util.js'
13
+ import { ATTESTED_METADATA, attestsAny, BUNDLE_FILE, CODE_EXTENSIONS, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, canObserveExecuteBits, classifyFormat, erasedTypeScriptFormat, hasNodeModulesSegment, heldToAccounts, importTargetsEqual, installedAccounts, LOCK_FILE, memoized, mergeModuleMaps, inResources, isBinaryPlist, isNativeArtifact, isPackageString, isPathWithin, isPlainObject, isStatFormat, isSubpackage, moduleFileKey, moduleInfo, narrowExecutable, nestedMap, nodeExtFormat, observeExecutable, relativeEscapes, sortedByPath, splitNodeModulesPath, toPosix, upsert, upsertFormat, withMetadataOf } from './util.js'
14
14
  import { detectRepo, invalidPackageConfig, namedManifestOf, packageJSONStat, packageJSONText, packageRepo, readModuleManifest, scopeManifestOf, vendorDirOf, vendoredPackageOf } from './bundle-util.js'
15
15
  import { expandExtra, validationError } from './add.js'
16
+ import { linksOn } from './policy.js'
16
17
  import { diskHost } from './host.js'
17
18
  import corePackage from './package.cjs'
18
19
 
19
- // Destructure off the namespace: captures the real fns at eval time, so writes survive --mock's
20
- // syncBuiltinESMExports() remock of node:fs.
21
- const { existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } = fs
20
+ // Snapshotted, so writes survive --mock's syncBuiltinESMExports() remock of node:fs.
21
+ const { mkdirSync, rmSync, writeFileSync } = fs
22
22
 
23
- const FILE_CONFIG = 'stasis.config.json'
24
- const FILE_LOCK = 'stasis.lock.json'
25
- const FILE_CODE = 'stasis.code.br'
26
-
27
- // Via src/package.cjs: a bundler-safe re-export of package.json.
28
23
  const VERSION = corePackage.version
29
-
30
24
  const CAN_OBSERVE_EXECUTE_BITS = canObserveExecuteBits()
25
+ const REBUNDLE = 'Re-bundle with the current stasis or `bundle=replace` against a v0-free starting point to upgrade.'
31
26
 
32
- // A package.json's fields; one that is no JSON object is refused as Node refuses it.
33
27
  function readPackageJSON(host, pkgAbsolute) {
34
28
  const buf = host.readFile(pkgAbsolute)
35
29
  assert.ok(isUtf8(buf))
36
30
  let json
37
- try {
38
- json = JSON.parse(packageJSONText(buf))
39
- } catch (cause) {
40
- throw invalidPackageConfig(pkgAbsolute, cause)
41
- }
31
+ try { json = JSON.parse(packageJSONText(buf)) } catch (cause) { throw invalidPackageConfig(pkgAbsolute, cause) }
42
32
  if (!isPlainObject(json)) throw invalidPackageConfig(pkgAbsolute)
43
33
  return json
44
34
  }
45
35
 
46
- // A weak 'stat:*' yields to a real format; two real formats or a stat kind flip are fatal.
47
- function upsertFormat(map, file, format) {
48
- const currentFormat = map.get(file)
49
- map.set(file, currentFormat === undefined ? format : reconcileFormat(format, currentFormat, file))
50
- }
36
+ const cloneMaps = (map) => new Map(Array.from(map, ([key, value]) => [key, value instanceof Map ? cloneMaps(value) : value]))
51
37
 
52
- // Loader formats the extension alone decides; `.js`/`.ts` follow the nearest package `type`.
53
- const EXT_FORMATS = { __proto__: null, '.json': 'json', '.mjs': 'module', '.cjs': 'commonjs', '.mts': 'module-typescript', '.cts': 'commonjs-typescript' }
54
38
  // With no `type`, Node decides .js/.ts by syntax detection: either variant is possible, the first is the default.
55
39
  const UNTYPED_VARIANTS = { __proto__: null, '.js': ['commonjs', 'module'], '.ts': ['commonjs-typescript', 'module-typescript'] }
56
40
 
57
- // Nested package.json files known to disagree with their package root's name/version because of
58
- // upstream packaging, exempt from #locateModule's consistency check. Matched as a segment-aligned
59
- // suffix of the project-relative path, so hoisted, nested and .pnpm-store copies all match.
60
- // Maintained list: add only a confirmed upstream false positive, with its reason.
41
+ // Nested package.json files upstream leaves disagreeing with their root's. Add only a confirmed false positive.
61
42
  const INCONSISTENT_PACKAGE_JSON_EXCEPTIONS = [
62
43
  'node_modules/@redis/client/dist/package.json', // stale build-time copy
63
44
  'node_modules/hashids/esm/package.json', // a `type` marker named `hashids-esm`, in every 2.x release
@@ -65,9 +46,7 @@ const INCONSISTENT_PACKAGE_JSON_EXCEPTIONS = [
65
46
  const isInconsistentPackageJsonException = (file) =>
66
47
  INCONSISTENT_PACKAGE_JSON_EXCEPTIONS.some((suffix) => file === suffix || file.endsWith(`/${suffix}`))
67
48
 
68
- // The resolution context a condition list or imports key ('*', or a ', '-joined list with an
69
- // optional ' (with: {json})' suffix) asserts: 'import' or 'require'. Null for '*' and for a set
70
- // naming both or neither (a platform's, a source language's).
49
+ // 'import' or 'require', as a condition list or #conditionsKey asserts; null for '*' or a set naming both or neither.
71
50
  function resolutionContext(conditions) {
72
51
  const list = typeof conditions === 'string' ? conditions.split(' (with: ')[0].split(', ') : conditions
73
52
  const asImport = list.includes('import')
@@ -75,8 +54,7 @@ function resolutionContext(conditions) {
75
54
  return asImport ? 'import' : 'require'
76
55
  }
77
56
 
78
- // Whether resolving `specifier` reads conditions: a bare one does (a package's `exports`, its own
79
- // name's included), as does a `#` one (its `imports`); a relative, absolute or URL one never does.
57
+ // A bare specifier reads `exports` conditions (its own name's included), a `#` one `imports`'.
80
58
  function readsConditions(specifier) {
81
59
  if (typeof specifier !== 'string') return false
82
60
  if (specifier.startsWith('#')) return true
@@ -96,95 +74,59 @@ export class State {
96
74
  formats = new Map()
97
75
  modules = new Map()
98
76
  imports = new Map()
99
- // Project-relative files carrying a POSIX execute bit; disk is authoritative (a file that lost
100
- // the bit drops out). Emitted as the artifacts' `executable` list.
77
+ // Disk is authoritative: a file that lost its execute bit drops out.
101
78
  executable = new Set()
102
79
  config
103
80
  root
104
81
  #parent
82
+ // Shared with sidecars: every record's `dirs`, the `files` of each absorbed with ATTESTED_METADATA, and the
83
+ // `bundles`' own record of each package they attest something of, which the run's record may not take.
84
+ #absorbed = { dirs: new Set(), files: new Map(), bundles: new Map() }
85
+ #attestedByBundle = new Set()
105
86
  #isPreload = false
106
87
  #host
107
-
108
- // Claimed write-target paths (canonicalized -> label), for cross-State collision detection; a
109
- // State built with `claim: false`, which never writes (a static build), claims none.
88
+ #policy = null
89
+ #linksOf = null
110
90
  #claim
111
91
  #claims = new Map()
112
-
113
- // WRITE-mode sidecars: lockfile CONTRIBUTORS (#mergedImports/#mergedFormats union theirs in).
92
+ // Write-mode sidecars contribute to the lockfile; load-mode ones are only served from (getFs*Family).
114
93
  #sidecars = new Set()
115
- // LOAD-mode sidecars: read-only, NOT lockfile contributors (getFs*Family serves bytes from them).
116
94
  #readSidecars = new Set()
117
-
118
- // Lockfile-attested resolutions (conditions -> parent -> specifier -> file) and loader formats
119
- // (file -> format), or null; may be supersets of what a run observes, so kept apart from the live maps.
95
+ // May be supersets of what a run observes, so kept apart from the live maps.
120
96
  #lockImports = null
121
97
  #lockFormats = null
122
- // Loaded a lockfile at construction: #mergeBundleMetadata cross-checks bundle metadata against
123
- // it instead of absorbing it as source of truth.
124
98
  #lockfileLoaded = false
125
-
126
- // Keys THIS process recorded this run (not the seeded baseline); shardSnapshot forwards only these.
99
+ // What THIS process recorded, not the seeded baseline: shardSnapshot forwards only these.
127
100
  #observed = new Set()
128
- // file -> consumers that recorded it, for the bundle's informational `reason` field.
129
101
  #reasonFiles = new Map()
130
- // 'run' files it imported or fs-read before any plugin attached (its own), and ones it fs-read
131
- // after one attached: see #reasonClaims.
102
+ // 'run' files it loaded itself, and ones it only fs-read after a plugin attached (#reasonClaims).
132
103
  #runOwn = new Set()
133
104
  #fsReadPostPlugin = new Set()
134
105
  #pluginAttached = false
135
-
136
- // Attested by a frozen bundle (bundle=frozen), or null: closes the attested set, so anything the
137
- // bundle never recorded is rejected. Separate from the live maps.
106
+ // A frozen bundle's attestation, apart from the live maps: anything it never recorded is rejected.
138
107
  #bundleSources = null
139
108
  #bundleResources = null
140
109
  #bundleImports = null
141
110
  #bundleFormats = null
142
-
143
- // Lazily-built dirs IMPLIED by recorded files, so getFsStat answers isDirectory() at bundle=load.
144
111
  #impliedDirIndex = null
145
-
146
- // Native resolutions (parent URL -> specifier -> resolved URL) from the Module._resolveFilename
147
- // shim: require.resolve()/CJS require()s bypassing the resolve hook.
148
112
  #observedResolutions = new Map()
149
-
150
- // Files a require() actually EXECUTED (from the Module._load shim) -- unlike a resolve-only
151
- // edge, an executed file with no captured bytes is a hole in the attestation, so write()
152
- // backfills it from disk (a transplanted require.extensions handler, e.g. a transforming
153
- // preload's CJS TypeScript pipeline, loads files without ever hitting the load hook).
154
113
  #executedFiles = new Set()
155
-
156
- // Files whose BYTES this run captured via addFile. Distinct from this.hashes, which also
157
- // absorbs lockfile-seeded entries: #backfillExecutedFiles must key on "observed this run", or
158
- // a frozen/add run would skip verifying an executed file merely because a hash is on record.
114
+ // Unlike this.hashes, never lockfile-seeded: a seeded hash must not exempt an executed file from verification.
159
115
  #filesCapturedThisRun = new Set()
160
-
161
- // Node's require-condition set, from the first require()-context addImport;
162
- // #backfillObservedResolutions keys native edges under it so the lockfile is Node-version-stable.
116
+ // Keys backfilled native edges, so the lockfile is Node-version-stable.
163
117
  #requireConditions = null
164
-
165
- // Root of a package Node loaded BEFORE our hooks (stasis-core's own); write()'s backfill
166
- // statically parses files under it to recover edges the resolve hook never observed.
167
118
  #preloadRoot = null
168
-
169
- // Per-artifact "last serialized" caches (the bundles' by path): write() skips brotli + writeFileSync on unchanged text.
170
119
  #lastLockData = null
171
120
  #lastBundleText = new Map()
172
- // Lazy `{ value: detectRepo(this.root, this.#host) }`.
173
121
  #detectedRepoCache
174
- // Half ('code' | 'resources') -> `repo` of the bundle absorbed there for adding to.
175
122
  #absorbedRepos = new Map()
176
-
177
- // Memoized #canonicalUncached (per URL, State-lifetime): a realpathSync per node_modules URL, and
178
- // hot callers repeat relentlessly. Sound only because the tree holds still mid-run; the cached
179
- // result is shared -- never mutate it.
123
+ // Sound only because the tree holds still mid-run; the cached result is shared: never mutate it.
180
124
  #canonicalCache = new Map()
181
125
 
182
- // Options: `preload` (the unique preload State), `parent` (run as a sidecar sharing the parent's
183
- // hashes/entries/modules, with its own sources/formats/imports/resources and bundle) and
184
- // `skipDiscovery` (a caller-populated in-memory State, NO filesystem discovery; read-only modes
185
- // only). All other keys forward to Config.
126
+ // `preload`: the unique preload State. `parent`: a sidecar sharing the parent's hashes/entries/modules.
127
+ // `skipDiscovery`: caller-populated, read-only, NO filesystem discovery. The rest go to Config.
186
128
  constructor(root, options = {}) {
187
- const { preload: isPreload = false, parent: parentState, preloadRoot, skipDiscovery = false, host = diskHost, env = process.env, claim = true, ...configOptions } = options
129
+ const { preload: isPreload = false, parent: parentState, preloadRoot, skipDiscovery = false, host = diskHost, env = process.env, claim = true, checkPolicy = true, ...configOptions } = options
188
130
  this.config = new Config({ ...configOptions, env, host })
189
131
  this.#host = host
190
132
  this.#claim = claim
@@ -193,73 +135,58 @@ export class State {
193
135
  this.#isPreload = isPreload
194
136
 
195
137
  if (parentState) {
196
- // A sidecar shares the parent's maps and inherits its attestation, so a version skew is
197
- // unsound (a different module copy is fine -- it reads only the parent's PUBLIC surface).
198
- assert.equal(parentState.version, VERSION,
199
- `child State version '${VERSION}' must exactly match parent version '${parentState.version}'`)
138
+ // A sidecar shares the parent's maps and attestation: a version skew is unsound (another copy is fine).
139
+ assert.equal(parentState.version, VERSION, `child State version '${VERSION}' must exactly match parent version '${parentState.version}'`)
200
140
  this.#parent = parentState
201
141
  this.root = parentState.root
202
142
  this.hashes = parentState.hashes
203
143
  this.entries = parentState.entries
204
144
  this.modules = parentState.modules
205
- // Public sidecarInheritance() (works across copies): the lockfile attestation must reach the
206
- // sidecar, else a tampered bundle could redirect a resolution on hash-valid bytes.
145
+ // The lockfile attestation must reach the sidecar, else a tampered bundle could redirect hash-valid bytes.
207
146
  const inherited = parentState.sidecarInheritance()
208
147
  this.#preloadRoot = inherited.preloadRoot
209
148
  this.#lockImports = inherited.lockImports
210
149
  this.#lockFormats = inherited.lockFormats
211
150
  this.#lockfileLoaded = inherited.lockfileLoaded
151
+ this.#policy = inherited.policy
152
+ this.#absorbed = inherited.absorbed
212
153
  assert.ok(this.config.bundleFile, 'sidecar State requires bundleFile')
213
- // Only writing States claim, so two write-intent States can't silently target the same file.
214
- if (this.config.writeBundle) {
215
- this.#claimWritePath('bundleFile', 'sidecar ')
216
- this.#claimWritePath('resourcesBundleFile', 'sidecar ')
217
- }
218
- // frozenBundle MUST go through this branch too -- it has its own attestation snapshot to build.
154
+ if (this.config.writeBundle) for (const key of ['bundleFile', 'resourcesBundleFile']) this.#claimWritePath(key, 'sidecar ')
155
+ // frozenBundle too: it has its own attestation snapshot to build. What it absorbs reaches the parent.
219
156
  if (this.config.bundle) {
220
- const sourcesPath = this.config.bundleFile
221
- const sources = this.#readMaybe(sourcesPath)
222
- if (sources && !this.config.replaceBundle) {
223
- const bundle = parseBundleBytes(sources)
224
- // v0 bundle has no per-file formats or import map -- serving one via a sidecar widens trust.
225
- assert.equal(bundle.version, Bundle.VERSION,
226
- `stasis sidecar requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
227
- `Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point.`)
228
- assert.equal(bundle.config.scope, this.config.scope)
229
- // Cross-checked against the parent's lockfile, or absorbed if none (shared hashes/entries/
230
- // modules mean those writes propagate to the parent).
231
- this.#absorbCodeBundle(bundle, this.#lockfileLoaded)
232
- }
233
- this.#absorbResourcesBundleFile(this.#lockfileLoaded)
234
- }
235
- if (this.config.frozenBundle) {
236
- assert.ok(this.#bundleSources !== null, 'No bundle, but attempting to run in frozen bundle mode')
157
+ const path = this.config.bundleFile
158
+ const sources = this.#readMaybe(path)
159
+ if (sources && !this.config.replaceBundle) this.#absorbCodeBundle(this.#parseServedBundle(sources, path, 'stasis sidecar'))
160
+ this.#absorbResourcesBundleFile()
237
161
  }
238
- // Register AFTER every fallible step so a throw leaves no dead reference. Frozen sidecars
239
- // join neither registry (--fs neither captures nor serves in frozen-bundle mode).
162
+ if (this.config.frozenBundle) this.#freezeBundle()
163
+ if (this.#attestedByBundle.size > 0) this.#holdToInstalled(relative(this.root, this.#bundlePath))
164
+ // AFTER every fallible step, so a throw leaves no dead reference. A frozen sidecar joins neither registry.
240
165
  if (this.config.writeBundle) parentState.registerSidecar(this)
241
166
  else if (this.config.loadBundle) parentState.registerReadSidecar(this)
242
167
  if (this.#claims.size > 0) liveStates().add(this)
243
168
  return
244
169
  }
245
170
 
171
+ if (preloadRoot !== undefined) {
172
+ assert.equal(typeof preloadRoot, 'string', 'preloadRoot must be a string')
173
+ this.#preloadRoot = preloadRoot
174
+ }
246
175
  if (skipDiscovery) {
247
176
  assert.ok(!this.config.frozen && !this.config.frozenBundle && !this.config.writeBundle && !this.config.writeLockfile,
248
177
  'skipDiscovery is read-only: incompatible with frozen and write (add/replace) lock/bundle modes')
249
178
  this.root = resolve(root)
250
- this.#setPreloadRoot(preloadRoot)
251
179
  if (isPreload) liveStates().add(this)
252
180
  return
253
181
  }
254
182
 
255
- // Capture and load MUST agree on where the upward walk stops (PROJECT_CWD/.git/
256
- // pnpm-workspace.yaml), else they commit different roots and fail "outside the project root".
183
+ // Capture and load MUST agree on where the upward walk stops, else they commit different roots.
257
184
  const potentialRoots = []
258
185
  for (let cursor = root; cursor; cursor = dirname(cursor)) {
259
- // One there that can't be read is refused, not walked past to a root above (packageJSONStat).
186
+ // One there that can't be read is refused, not walked past (packageJSONStat).
260
187
  if (packageJSONStat(this.#host, join(cursor, 'package.json')) !== null) {
261
188
  potentialRoots.push(cursor)
262
- } else if ([FILE_CONFIG, FILE_LOCK, FILE_CODE].some((file) => this.#exists(join(cursor, file)))) {
189
+ } else if ([CONFIG_FILE, LOCK_FILE, BUNDLE_FILE].some((file) => this.#exists(join(cursor, file)))) {
263
190
  throw new Error('Unexpected stasis config without package.json')
264
191
  }
265
192
  if (cursor === env.PROJECT_CWD) break // e.g. yarn sets this
@@ -267,75 +194,56 @@ export class State {
267
194
  if (dirname(cursor) === cursor) break
268
195
  }
269
196
 
270
- // default root is top-level package.json, to opt-in to per-dir create stasis.config.json
197
+ // The top-level package.json by default; a stasis.config.json opts a dir in.
271
198
  this.root = potentialRoots.at(-1)
272
199
  assert.ok(this.root, `stasis: no package.json found at or above ${resolve(root)}; run stasis from within a project`)
273
200
 
274
201
  let loaded = false
275
- let lockfileLoaded = false
276
- // Explicit lockFile: suppress the per-rootDir probe so a stale project-root lockfile can't
277
- // shadow it; substituted below.
202
+ // An explicit lockFile suppresses the per-rootDir probe, so a stale project-root lockfile can't shadow it.
278
203
  const explicitLockPath = this.config.lockFile
279
- // A construction-time (flag/env) `bundleFile` is rootDir-INDEPENDENT, so it must NOT be a
280
- // root-detection signal: picking the innermost dir would diverge load's root from the capture
281
- // root. A config-only bundleFile is NOT suppressed (its dir already carries the config signal).
204
+ // A flag/env `bundleFile` is rootDir-INDEPENDENT, so NOT a root signal: else load's root could diverge.
282
205
  const explicitBundlePath = this.config.bundleFile
283
206
  for (const rootDir of potentialRoots) {
284
- const config = readFileSyncMaybe(rootDir, FILE_CONFIG, 'utf-8', this.#host)
285
- const lockProbe = explicitLockPath ? null : readFileSyncMaybe(rootDir, FILE_LOCK, 'utf-8', this.#host)
286
- let sourcesPath = explicitBundlePath || join(rootDir, FILE_CODE)
207
+ const config = readFileSyncMaybe(rootDir, CONFIG_FILE, 'utf-8', this.#host)
208
+ const lockProbe = explicitLockPath ? null : readFileSyncMaybe(rootDir, LOCK_FILE, 'utf-8', this.#host)
209
+ let sourcesPath = explicitBundlePath || join(rootDir, BUNDLE_FILE)
287
210
  let sources = this.#readMaybe(sourcesPath)
288
- // Root-SELECTION signals: a config, a lockfile, or only the DEFAULT <rootDir>/stasis.code.br
289
- // (see explicitBundlePath; an explicit bundleFile is still loaded via `sources`).
211
+ // Root signals: a config, a lockfile, or the DEFAULT bundle path (an explicit one still loads via `sources`).
290
212
  if (config === null && lockProbe === null && (explicitBundlePath || sources === null)) continue
291
- if (loaded) throw new Error('Stasis config already loaded')
292
213
  loaded = true
293
214
  this.root = rootDir
294
215
 
295
216
  if (config) {
296
217
  this.config.loadConfig(config)
297
- // stasis.config.json may set a `bundleFile` the probe couldn't see -- re-resolve and re-read.
298
- const configuredPath = this.config.bundleFile || join(rootDir, FILE_CODE)
299
- if (configuredPath !== sourcesPath) {
300
- sourcesPath = configuredPath
218
+ // false: a static build checks the whole bundle itself, every offender at once.
219
+ if (checkPolicy) this.#policy = this.config.policy
220
+ // stasis.config.json may set a `bundleFile` the probe couldn't see.
221
+ if (this.#bundlePath !== sourcesPath) {
222
+ sourcesPath = this.#bundlePath
301
223
  sources = this.#readMaybe(sourcesPath)
302
224
  }
303
225
  }
304
226
 
305
227
  const lock = explicitLockPath ? this.#readMaybe(explicitLockPath, 'utf-8') : lockProbe
306
- lockfileLoaded = this.#absorbLockfile(lock, explicitLockPath || join(rootDir, FILE_LOCK))
307
- this.#loadBundleArtifacts(sources, sourcesPath, lockfileLoaded)
228
+ this.#absorbLockfile(lock, this.#lockPath)
229
+ this.#loadBundleArtifacts(sources, sourcesPath)
308
230
 
309
- // Innermost matching rootDir wins (per-dir opt-in); without this break an outer root
310
- // re-detects a rootDir-independent bundleFile and trips `loaded`.
231
+ // The innermost wins: else an outer root re-detects a rootDir-independent bundleFile.
311
232
  break
312
233
  }
313
234
 
314
- // Explicit lockfile but no discovery indicator: the loop never ran, so absorb here against
315
- // `this.root` (outermost) so the run still has attestation.
316
- if (!loaded && explicitLockPath) {
317
- lockfileLoaded = this.#absorbLockfile(this.#readMaybe(explicitLockPath, 'utf-8'), explicitLockPath)
318
- }
319
-
320
- // Explicit bundleFile suppressed as a root signal above: load against `this.root` (outermost).
235
+ // With no root signal, explicit artifacts load against the outermost root.
236
+ if (!loaded && explicitLockPath) this.#absorbLockfile(this.#readMaybe(explicitLockPath, 'utf-8'), explicitLockPath)
321
237
  if (!loaded && (explicitBundlePath || this.config.resourcesBundleFile)) {
322
- this.#loadBundleArtifacts(explicitBundlePath ? this.#readMaybe(explicitBundlePath) : null, explicitBundlePath, lockfileLoaded)
238
+ this.#loadBundleArtifacts(explicitBundlePath ? this.#readMaybe(explicitBundlePath) : null, explicitBundlePath)
323
239
  }
324
240
 
325
- // Post-loop: with no stasis files the loop never runs, and a frozen run must fail closed.
326
- if (this.config.frozen) assert.ok(lockfileLoaded, 'No lockfile, but attempting to run in frozen mode')
327
- if (this.config.frozenBundle) assert.ok(this.#bundleSources !== null, 'No bundle, but attempting to run in frozen bundle mode')
241
+ if (this.config.frozen) assert.ok(this.#lockfileLoaded, 'No lockfile, but attempting to run in frozen mode')
242
+ if (this.config.frozenBundle) this.#freezeBundle()
243
+ this.#holdToInstalled(relative(this.root, this.#lockfileLoaded ? this.#lockPath : this.#bundlePath))
328
244
 
329
- // Claim write targets so no other live State (any copy) silently targets the same file.
330
- if (this.config.writeBundle) {
331
- this.#claimWritePath('bundleFile')
332
- this.#claimWritePath('resourcesBundleFile')
333
- }
245
+ if (this.config.writeBundle) for (const key of ['bundleFile', 'resourcesBundleFile']) this.#claimWritePath(key)
334
246
  if (this.config.writeLockfile) this.#claimWritePath('lockFile')
335
-
336
- this.#setPreloadRoot(preloadRoot)
337
-
338
- // The registry answers for the preload and for write claims; any other State stays out of it.
339
247
  if (isPreload || this.#claims.size > 0) liveStates().add(this)
340
248
  }
341
249
 
@@ -343,116 +251,145 @@ export class State {
343
251
  return this.#host.stat(p) !== null
344
252
  }
345
253
 
346
- // The file at `path` through the host, or null when there is none.
347
- #readMaybe(path, encoding) {
348
- return readFileSyncMaybe(dirname(path), basename(path), encoding, this.#host)
254
+ // The ATTESTED_METADATA absorbed of the artifact at `label`, and what each bundle attests, held to how each
255
+ // package was installed; kept as recorded where no account tells (nothing installed, as where a bundle runs).
256
+ #holdToInstalled(label) {
257
+ const { files, bundles } = this.#absorbed
258
+ // What only its bundles attest of a package is its record's too where they hold every file of it; else
259
+ // it stays theirs, which a run writing its bundle again would drop: refused.
260
+ for (const dir of this.#attestedByBundle) {
261
+ const [module, own] = [this.modules.get(dir), bundles.get(dir)]
262
+ const alone = Object.fromEntries(ATTESTED_METADATA.filter((field) => module[field] === undefined && own[field] !== undefined).map((field) => [field, own[field]]))
263
+ const beyond = Object.keys(module.files).find((rel) => !Object.hasOwn(own.files, rel))
264
+ if (beyond === undefined) this.modules.set(dir, moduleInfo({ ...module, ...alone }))
265
+ else assert.ok(!this.config.writeBundle || Object.keys(alone).length === 0, `bundle module ${dir} ${Object.keys(alone)[0]} is recorded of its files, not of '${moduleFileKey(dir, beyond)}' its record holds: record it anew (lock = replace)`)
266
+ }
267
+ const attested = new Map([...this.modules].filter(([, module]) => attestsAny(module)))
268
+ for (const [dir, module] of attested) if (!files.has(dir)) files.set(dir, new Set(Object.keys(module.files)))
269
+ const accounts = installedAccounts(new Map([...bundles, ...attested]), this.root)
270
+ heldToAccounts(attested, accounts, `${label}: `)
271
+ heldToAccounts(bundles, accounts, `${relative(this.root, this.#bundlePath)}: `)
272
+ }
273
+
274
+ // A bundle's record of a package merged into the State's (`record`, if any). What it alone attests agrees
275
+ // with every other bundle's of the files it covers (mergeModuleMaps), and is taken once all are absorbed.
276
+ #absorbBundleRecord(dir, info, record, what) {
277
+ const { dirs, files, bundles } = this.#absorbed
278
+ dirs.add(dir)
279
+ if (attestsAny(info)) {
280
+ this.#attestedByBundle.add(dir)
281
+ const known = bundles.get(dir)
282
+ bundles.set(dir, known === undefined ? info : mergeModuleMaps(new Map([[dir, known]]), new Map([[dir, info]]), 'bundle').get(dir))
283
+ // A later bundle's files (a sidecar's) of what it attests alike are absorbed, not this run's.
284
+ const baseline = files.get(dir)
285
+ if (baseline && ATTESTED_METADATA.every((field) => record?.[field] === undefined || JSON.stringify(record[field]) === JSON.stringify(info[field]))) for (const rel of Object.keys(info.files)) baseline.add(rel)
286
+ }
287
+ // One attesting none of what the record took already (a sidecar's, after its parent's) adds no
288
+ // file to it where its files are the record's too: the value was recorded without them.
289
+ const lacked = this.config.writeBundle && record ? ATTESTED_METADATA.find((field) => record[field] !== undefined && info[field] === undefined) : undefined
290
+ const added = lacked === undefined ? undefined : Object.keys(info.files).find((rel) => !(files.get(dir)?.has(rel) ?? Object.hasOwn(record.files, rel)))
291
+ if (added !== undefined) assert.fail(`bundle: module '${dir}' ${lacked} is recorded of one side's files, not of '${moduleFileKey(dir, added)}' the other adds`)
292
+ // What a bundle alone attests is the record's once all are absorbed, where they cover its files (#holdToInstalled).
293
+ const alone = Object.fromEntries(ATTESTED_METADATA.filter((field) => record?.[field] === undefined).map((field) => [field, undefined]))
294
+ return moduleInfo(record === undefined ? { ...info, ...alone, files: Object.create(null) } : { ...withMetadataOf(record, info, what), ...alone })
295
+ }
296
+
297
+ // Not what a lockfile alone locks (what an install would lay out, not what one did), nor onto a record
298
+ // absorbed, whose other files this run may not have read: of a file this run adds, an install record tells.
299
+ #recordInstalled() {
300
+ const accounts = installedAccounts(this.modules, this.root)
301
+ heldToAccounts(this.modules, accounts)
302
+ for (const [dir, account] of accounts) if (!account.locked && !this.#absorbed.dirs.has(dir)) this.modules.set(dir, withMetadataOf(this.modules.get(dir), account))
303
+ for (const [dir, files] of this.#absorbed.files) {
304
+ const [module, account] = [this.modules.get(dir), accounts.get(dir)]
305
+ const added = account && !account.locked ? undefined : Object.keys(module.files).find((rel) => !files.has(rel))
306
+ if (added === undefined) continue
307
+ const fields = ATTESTED_METADATA.filter((field) => module[field] !== undefined).join(' and ')
308
+ assert.fail(`module '${dir}': its ${fields} is recorded of the files it was read with, not of ${moduleFileKey(dir, added)}, which this run adds and no install record tells of: record it anew (lock = replace)`)
309
+ }
349
310
  }
350
311
 
351
- #setPreloadRoot(preloadRoot) {
352
- if (preloadRoot === undefined) return
353
- assert.equal(typeof preloadRoot, 'string', 'preloadRoot must be a string')
354
- this.#preloadRoot = preloadRoot
312
+ #readMaybe(path, encoding) {
313
+ return readFileSyncMaybe(dirname(path), basename(path), encoding, this.#host)
355
314
  }
356
315
 
357
- // Absorb a lockfile's attestation; returns whether it was actually absorbed. Lockfile.parse
358
- // requires both imports and formats, so an absorbed lockfile always attests them.
316
+ // Lockfile.parse requires both imports and formats, so an absorbed lockfile always attests them.
359
317
  #absorbLockfile(lock, lockPath) {
360
- if (lock && !this.config.useLockfile && !this.config.ignoreLockfile) {
361
- throw new Error(`Unexpected ${lockPath} with config.lock = 'none'`)
362
- }
363
- if (!lock || !this.config.useLockfile || this.config.replaceLockfile) return false
318
+ if (lock && !this.config.useLockfile && !this.config.ignoreLockfile) throw new Error(`Unexpected ${lockPath} with config.lock = 'none'`)
319
+ if (!lock || !this.config.useLockfile || this.config.replaceLockfile) return
364
320
  const lockfile = Lockfile.parse(lock)
365
321
  if (this.config.frozen) assert.equal(lockfile.config.scope, this.config.scope)
322
+ this.#assertLoaded(lockfile, lockPath)
366
323
 
367
324
  const includeSources = lockfile.config.scope === 'full' && this.config.full
368
325
  for (const [dir, info] of lockfile.modules) {
369
326
  if (!hasNodeModulesSegment(dir) && !includeSources) continue
370
327
  this.modules.set(dir, info)
328
+ this.#absorbed.dirs.add(dir)
329
+ for (const [name, hash] of Object.entries(info.files)) noupsert(this.hashes, moduleFileKey(dir, name), hash)
371
330
  }
372
331
  if (includeSources) this.entries = lockfile.entries
373
-
374
- for (const [dir, { files }] of this.modules) {
375
- for (const [name, hash] of Object.entries(files)) noupsert(this.hashes, join(dir, name), hash)
376
- }
377
332
  this.#lockImports = lockfile.imports
378
333
  this.#lockFormats = lockfile.formats
379
- // Seeded into the live set (not a separate baseline) so re-reading a file this run can CLEAR
380
- // a stale exec bit.
381
- this.#absorbExecutable(lockfile)
334
+ // Into the live set, not a baseline, so re-reading a file this run can CLEAR a stale exec bit.
335
+ for (const file of lockfile.executable) this.executable.add(file)
382
336
  this.#lockfileLoaded = true
383
- return true
384
337
  }
385
338
 
386
- // An on-disk bundle is read in under add/load/frozen; replace starts over.
387
339
  get #absorbsBundles() {
388
340
  return this.config.bundle && !this.config.replaceBundle
389
341
  }
390
342
 
391
- // Gate and load the bundle artifacts for a committed root; `sources` may be null (no bundle, or
392
- // resources-only).
393
- #loadBundleArtifacts(sources, sourcesPath, lockfileLoaded) {
394
- if (sources && this.config.bundleMode === 'none') {
395
- throw new Error(`Unexpected ${sourcesPath} with config.bundle = 'none'`)
396
- }
397
- // A frozen bundle is self-attesting, so it needs no sibling lockfile and is exempt here.
398
- if (sources && !lockfileLoaded && this.config.useLockfile && !this.config.replaceLockfile && !this.config.frozenBundle) {
343
+ #loadBundleArtifacts(sources, sourcesPath) {
344
+ if (sources && this.config.bundleMode === 'none') throw new Error(`Unexpected ${sourcesPath} with config.bundle = 'none'`)
345
+ // A frozen bundle is self-attesting, so it needs no sibling lockfile.
346
+ if (sources && !this.#lockfileLoaded && this.config.useLockfile && !this.config.replaceLockfile && !this.config.frozenBundle) {
399
347
  throw new Error('stasis.lock.json missing, can not use sources')
400
348
  }
401
- if (sources && this.#absorbsBundles) this.#loadCodeBundle(sources, sourcesPath, lockfileLoaded)
402
- this.#absorbResourcesBundleFile(lockfileLoaded)
349
+ if (sources && this.#absorbsBundles) {
350
+ const bundle = this.#parseServedBundle(sources, sourcesPath, 'stasis run',
351
+ 'Re-bundle with the current stasis (`stasis bundle`) or `stasis run --bundle=replace` against a v0-free starting point to upgrade.')
352
+ // assertEntry passes anything on an empty set: else any bundled file could run as the root.
353
+ assert.ok(!this.config.full || bundle.entries.size > 0 || !bundle.hasCode,
354
+ `${sourcesPath}: a full-scope bundle carrying code must declare an entry to run ` +
355
+ `(an add-only attestation has none; run a bundle built by \`stasis bundle\`/\`stasis run\`)`)
356
+ this.#absorbCodeBundle(bundle)
357
+ }
358
+ this.#absorbResourcesBundleFile()
403
359
  }
404
360
 
405
- // The resources half of a split layout, when configured and present on disk.
406
- #absorbResourcesBundleFile(lockfileLoaded) {
407
- const resourcesPath = this.config.resourcesBundleFile
408
- const resourcesData = resourcesPath ? this.#readMaybe(resourcesPath) : null
409
- if (resourcesData && this.#absorbsBundles) this.#absorbResourcesBundle(resourcesData, { lockfileLoaded, resourcesPath })
361
+ // v0 has no per-file formats or import map: serving one widens the trust boundary.
362
+ #parseServedBundle(bytes, path, runner = 'stasis run', remedy = REBUNDLE) {
363
+ const bundle = parseBundleBytes(bytes)
364
+ assert.equal(bundle.version, Bundle.VERSION, `${runner} requires a v1 bundle; ${path} is v${bundle.version}. ${remedy}`)
365
+ assert.equal(bundle.config.scope, this.config.scope)
366
+ this.#assertLoaded(bundle, path)
367
+ return bundle
410
368
  }
411
369
 
412
- #loadCodeBundle(sources, sourcesPath, lockfileLoaded) {
413
- const bundle = parseBundleBytes(sources)
414
- // Bundle.parse accepts v0 for offline tooling; the runtime refuses it -- v0 has no per-file
415
- // formats or import map, so serving one widens the trust boundary.
416
- assert.equal(bundle.version, Bundle.VERSION,
417
- `stasis run requires a v1 bundle; ${sourcesPath} is v${bundle.version}. ` +
418
- `Re-bundle with the current stasis (\`stasis bundle\`) or \`stasis run --bundle=replace\` ` +
419
- `against a v0-free starting point to upgrade.`)
420
- assert.equal(bundle.config.scope, this.config.scope)
421
- // assertEntry short-circuits on an empty set, so a runnable full-scope code bundle must declare
422
- // an entry here -- else any bundled file could run as the root. Add-only bundles have none.
423
- assert.ok(!this.config.full || bundle.entries.size > 0 || !bundle.hasCode,
424
- `${sourcesPath}: a full-scope bundle carrying code must declare an entry to run ` +
425
- `(an add-only attestation has none; run a bundle built by \`stasis bundle\`/\`stasis run\`)`)
426
- this.#absorbCodeBundle(bundle, lockfileLoaded)
427
- }
428
-
429
- // Take a parsed code bundle (unified, or the code half) as the live maps, cross-checked against
430
- // the loaded lockfile, and snapshot it when it is the frozen attestation.
431
- #absorbCodeBundle(bundle, lockfileLoaded) {
432
- this.#mergeBundleMetadata(bundle, { lockfileLoaded })
370
+ #absorbCodeBundle(bundle) {
371
+ this.#mergeBundleMetadata(bundle)
433
372
  for (const [file, content] of bundle.sources) {
434
373
  if (Bundle.isResourceFormat(bundle.formats.get(file))) this.resources.set(file, content)
435
374
  else this.sources.set(file, content)
436
375
  }
437
376
  this.formats = bundle.formats
438
377
  this.imports = bundle.imports
439
- this.#absorbExecutable(bundle)
440
- this.#seedReasonFromBundle(bundle)
441
- this.#absorbRepo('code', bundle)
442
- if (this.config.frozenBundle) {
443
- // Snapshot before addFile/addImport mutate the live maps; imports is deep-cloned because
444
- // this.imports shares bundle.imports's nested Maps.
445
- this.#bundleSources = new Set(this.sources.keys())
446
- this.#bundleResources = new Set(this.resources.keys())
447
- this.#bundleImports = objectToMaps(fileMapToObject(bundle.imports))
448
- this.#bundleFormats = new Map(bundle.formats)
449
- }
378
+ this.#absorbBundleExtras('code', bundle)
450
379
  }
451
380
 
452
- // Cross-check bundle metadata (entries/modules) against the loaded lockfile, or absorb it as
453
- // source of truth when none. v0 buckets carry no version, so unverifiable entries are skipped.
454
- #mergeBundleMetadata(bundle, { lockfileLoaded }) {
455
- if (lockfileLoaded) {
381
+ // Before addFile/addImport mutate the live maps; imports deep, as this.imports shares the bundle's Maps.
382
+ #freezeBundle() {
383
+ assert.ok(this.#absorbedRepos.size > 0, 'No bundle, but attempting to run in frozen bundle mode')
384
+ this.#bundleSources = new Set(this.sources.keys())
385
+ this.#bundleResources = new Set(this.resources.keys())
386
+ this.#bundleImports = cloneMaps(this.imports)
387
+ this.#bundleFormats = new Map(this.formats)
388
+ }
389
+
390
+ // Cross-checked against the loaded lockfile, else absorbed as the source of truth.
391
+ #mergeBundleMetadata(bundle) {
392
+ if (this.#lockfileLoaded) {
456
393
  if (bundle.entries.size > 0) {
457
394
  assert.deepStrictEqual([...bundle.entries].toSorted(), [...this.entries].toSorted(), 'bundle/lockfile entries mismatch')
458
395
  }
@@ -461,67 +398,45 @@ export class State {
461
398
  assert.ok(this.modules.has(dir), `bundle module ${dir} missing in lockfile`)
462
399
  const lockModule = this.modules.get(dir)
463
400
  assert.equal(info.name, lockModule.name)
464
- // Strict, absent included: every absorb site requires a v1 bundle, so a missing version is
465
- // a real workspace identity (or a stripped field), never v0 partial metadata -- a
466
- // version-less bundle bucket must not dodge the lockfile consistency check.
401
+ // Strict, absent included: every absorb site requires v1, so a missing version is a real identity.
467
402
  assert.equal(info.version, lockModule.version, `bundle module ${dir} version mismatch with lockfile`)
468
- // `repo` and `vendored` are metadata, which a lockfile never records: the bundle's are taken, to be written again.
469
- const withMetadata = withMetadataOf(lockModule, info)
470
- if (withMetadata !== lockModule) this.modules.set(dir, withMetadata)
471
- for (const rel of Object.keys(info.files)) {
472
- assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
473
- }
403
+ // The bundle's metadata is taken where the lockfile records none, to be written again.
404
+ this.modules.set(dir, this.#absorbBundleRecord(dir, info, lockModule, `bundle module ${dir} (against the lockfile)`))
405
+ for (const rel of Object.keys(info.files)) assert.ok(Object.hasOwn(lockModule.files, rel), `bundle file ${dir}/${rel} missing in lockfile`)
474
406
  }
475
- // Hashes alone don't stop a redirect to a different hash-valid file, so every edge the bundle
476
- // could serve must match the lockfile's attested target (a lockfile superset is fine).
477
- if (this.#lockImports !== null) {
478
- for (const [conditions, byParent] of bundle.imports) {
479
- for (const [parent, specifiers] of byParent) {
480
- for (const [specifier, file] of specifiers) {
481
- this.#assertAttestedResolution(this.#lockImports, conditions, parent, specifier, file, { what: 'bundle', source: 'lockfile' })
482
- }
407
+ // Hashes alone don't stop a redirect to another hash-valid file: every edge must match the lockfile's.
408
+ for (const [conditions, byParent] of bundle.imports) {
409
+ for (const [parent, specifiers] of byParent) {
410
+ for (const [specifier, file] of specifiers) {
411
+ this.#assertAttestedResolution(this.#lockImports, conditions, parent, specifier, file, { what: 'bundle', source: 'lockfile' })
483
412
  }
484
413
  }
485
414
  }
486
- // A format flip changes how hash-valid bytes parse (module<->commonjs) without touching a
487
- // hash, so every bundle format must match the lockfile's; unattested is fatal.
488
- if (this.#lockFormats !== null) {
489
- for (const [file, format] of bundle.formats) {
490
- this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'bundle', source: 'lockfile' })
491
- }
492
- // Inverse: a lockfile-tagged resource MUST be tagged in the bundle too, else omitting the
493
- // tag routes the base64 payload through this.sources (code) to the loader.
494
- for (const [dir, { files }] of bundle.modules) {
495
- for (const rel of Object.keys(files)) {
496
- const file = moduleFileKey(dir, rel)
497
- const lockFormat = this.#lockFormats.get(file)
498
- if (!Bundle.isResourceFormat(lockFormat)) continue
499
- assert.equal(bundle.formats.get(file), lockFormat,
500
- `bundle file ${file} must declare format='${lockFormat}' to match the lockfile`)
501
- }
415
+ // A format flip changes how hash-valid bytes parse (module<->commonjs) without touching a hash.
416
+ for (const [file, format] of bundle.formats) this.#assertAttestedFormat(this.#lockFormats, file, format, 'bundle', 'lockfile')
417
+ // Inverse: omitting a resource tag would route the base64 payload through this.sources to the loader.
418
+ for (const [dir, { files }] of bundle.modules) {
419
+ for (const rel of Object.keys(files)) {
420
+ const file = moduleFileKey(dir, rel)
421
+ const lockFormat = this.#lockFormats.get(file)
422
+ if (!Bundle.isResourceFormat(lockFormat)) continue
423
+ assert.equal(bundle.formats.get(file), lockFormat, `bundle file ${file} must declare format='${lockFormat}' to match the lockfile`)
502
424
  }
503
425
  }
504
426
  } else {
505
- // Mutate in place: this.entries may be shared by reference with a sidecar's parent.
427
+ // In place: this.entries may be a sidecar's parent's.
506
428
  for (const e of bundle.entries) this.entries.add(e)
507
429
  for (const [dir, info] of bundle.modules) {
508
- // Skip v0 partial metadata (nameless workspace buckets, version-less node_modules
509
- // buckets); a v1 workspace bucket may omit version, so its identity is absorbed anyway.
430
+ // v0 partial metadata; a v1 workspace bucket may omit version.
510
431
  if (!info.name || (!info.version && hasNodeModulesSegment(dir))) continue
511
432
  const existing = this.modules.get(dir)
512
- if (existing === undefined) {
513
- this.modules.set(dir, moduleInfo({ ...info, files: Object.create(null) }))
514
- } else {
515
- // A dir may be added twice (code + resource entries), and both must agree.
433
+ if (existing !== undefined) {
434
+ // A dir may be added twice (code + resource halves).
516
435
  assert.equal(info.name, existing.name, `bundle ${dir} name mismatch`)
517
436
  assert.equal(info.version, existing.version, `bundle ${dir} version mismatch`)
518
- // `repo` and `vendored` are metadata, held to nothing: the first half's that records one.
519
- const withMetadata = withMetadataOf(existing, info)
520
- if (withMetadata !== existing) this.modules.set(dir, withMetadata)
521
437
  }
522
- // A bundle=add run writes these buckets back, and #bundleModules carries only the files a
523
- // bucket lists: seed them as #absorbLockfile does, else a bundled file this run doesn't
524
- // load again drops out of the rewritten bundle.
438
+ this.modules.set(dir, this.#absorbBundleRecord(dir, info, existing, `bundle ${dir}`))
439
+ // A bundle=add run writes these back with only the files they list: else an unloaded bundled file drops out.
525
440
  if (!this.config.writeBundle) continue
526
441
  const { files } = this.modules.get(dir)
527
442
  for (const [rel, content] of Object.entries(info.files)) {
@@ -535,59 +450,47 @@ export class State {
535
450
  }
536
451
  }
537
452
 
538
- // Load the resources half of a split-bundle layout: ONLY resource-format files, unioned into the
539
- // bundleFile state.
540
- #absorbResourcesBundle(resourcesData, { lockfileLoaded, resourcesPath }) {
541
- const bundle = parseBundleBytes(resourcesData)
542
- assert.equal(bundle.version, Bundle.VERSION,
543
- `stasis run requires a v1 resources bundle; ${resourcesPath} is v${bundle.version}. ` +
544
- `Re-bundle with the current stasis or \`bundle=replace\` against a v0-free starting point to upgrade.`)
545
- assert.equal(bundle.config.scope, this.config.scope)
546
- // A populated imports/entries map means mixed halves or tampering -- widens trust on the code side.
453
+ // The resources half of a split layout, when configured and on disk: ONLY resource-format files.
454
+ #absorbResourcesBundleFile() {
455
+ const resourcesPath = this.config.resourcesBundleFile
456
+ const resourcesData = resourcesPath ? this.#readMaybe(resourcesPath) : null
457
+ if (!resourcesData || !this.#absorbsBundles) return
458
+ const bundle = this.#parseServedBundle(resourcesData, resourcesPath)
459
+ // Populated imports/entries mean mixed halves or tampering, widening trust on the code side.
547
460
  assert.equal(bundle.imports.size, 0, `resources bundle ${resourcesPath} must have empty imports`)
548
461
  assert.equal(bundle.entries.size, 0, `resources bundle ${resourcesPath} must have empty entries`)
549
462
  for (const [file, format] of bundle.formats) {
550
- assert.ok(Bundle.isResourceFormat(format),
551
- `resources bundle ${resourcesPath} declares non-resource format='${format}' for ${file}`)
463
+ assert.ok(Bundle.isResourceFormat(format), `resources bundle ${resourcesPath} declares non-resource format='${format}' for ${file}`)
552
464
  }
553
- this.#mergeBundleMetadata(bundle, { lockfileLoaded })
465
+ this.#mergeBundleMetadata(bundle)
554
466
  for (const [file, content] of bundle.sources) this.resources.set(file, content)
555
- // Union formats: the two halves' file sets must be disjoint.
556
467
  for (const [file, format] of bundle.formats) {
557
468
  const existing = this.formats.get(file)
558
469
  assert.ok(existing === undefined || existing === format,
559
470
  `format conflict for ${file}: bundleFile declares '${existing}', resourcesBundleFile declares '${format}'`)
560
471
  this.formats.set(file, format)
561
472
  }
562
- this.#absorbExecutable(bundle)
563
- this.#seedReasonFromBundle(bundle)
564
- this.#absorbRepo('resources', bundle)
565
- if (this.config.frozenBundle) {
566
- // Extend the frozen snapshot, init lazily: resources-only deployments are legal, so
567
- // bundleFile may not have populated these.
568
- this.#bundleResources ??= new Set()
569
- this.#bundleFormats ??= new Map()
570
- this.#bundleImports ??= objectToMaps(fileMapToObject(this.imports))
571
- this.#bundleSources ??= new Set(this.sources.keys())
572
- for (const f of bundle.sources.keys()) this.#bundleResources.add(f)
573
- for (const [f, fmt] of bundle.formats) this.#bundleFormats.set(f, fmt)
574
- }
473
+ this.#absorbBundleExtras('resources', bundle)
575
474
  }
576
475
 
577
- // Union (never replace) an absorbed artifact's `executable` list into the live set: a lockfile
578
- // seed, or the code half of a split layout, may already have contributed.
579
- #absorbExecutable(artifact) {
580
- for (const file of artifact.executable) this.executable.add(file)
476
+ // A bundle half's `executable` (unioned: a lockfile or the other half may have contributed), `repo` and `reason`.
477
+ #absorbBundleExtras(half, bundle) {
478
+ for (const file of bundle.executable) this.executable.add(file)
479
+ this.#absorbedRepos.set(half, this.#absorbedRepos.has(half) ? mergeRepo(this.#absorbedRepos.get(half), bundle.repo) : bundle.repo)
480
+ // So a bundle=add re-run preserves other consumers' attribution.
481
+ if (!this.config.writeBundle || bundle.reason === undefined) return
482
+ for (const [consumer, files] of Object.entries(bundle.reason)) {
483
+ for (const file of files) this.#recordReason(consumer, file)
484
+ }
581
485
  }
582
486
 
583
- // Verify one resolution edge against an attestation map: exact conditions key first, else accept
584
- // only if every condition set agrees on one target. Unknown edges fatal unless tolerateUnknown.
585
- #assertAttestedResolution(attestation, conditions, parent, specifier, file, { what, source = 'lockfile', tolerateUnknown = false, targetPlatform }) {
487
+ // Under the exact conditions key, else only where every condition set agrees on one target.
488
+ #assertAttestedResolution(attestation, conditions, parent, specifier, file, { what, source, tolerateUnknown = false, targetPlatform }) {
586
489
  const edge = `'${specifier}' from ${parent} (${conditions})`
587
490
  let attested = attestation.get(conditions)?.get(parent)?.get(specifier)
588
491
  if (attested === undefined) {
589
492
  const targets = new Set()
590
- for (const [, parents] of attestation) {
493
+ for (const parents of attestation.values()) {
591
494
  const target = parents.get(parent)?.get(specifier)
592
495
  if (target !== undefined) targets.add(target)
593
496
  }
@@ -596,52 +499,39 @@ export class State {
596
499
  assert.ok(targets.size === 1, `${what} resolution ${edge} is attested inconsistently across condition sets in the ${source}`)
597
500
  ;[attested] = targets
598
501
  }
599
- // A resolution made for one platform checks that platform's target of a per-platform edge.
600
502
  if (targetPlatform !== undefined && attested instanceof Map && !(file instanceof Map)) {
601
503
  attested = attested.get(targetPlatform)
602
504
  assert.ok(attested !== undefined, `${what} resolution ${edge} is not attested for platform '${targetPlatform}' by the ${source}`)
603
505
  }
604
- // A per-platform edge is a Map<platform, target>, compared structurally.
605
506
  assert.ok(importTargetsEqual(file, attested), `${what} resolution ${edge} mismatches the ${source}`)
606
507
  }
607
508
 
608
- // A frozen lockfile's and frozen bundle's checks of a `noun` ('File', or a 'Directory' listing) this
609
- // run records, in the zone they attest (node_modules, or all in full scope): each attests it, as
610
- // `module`'s `rel` and among the bundle's `bundled` (its sources or resources), and its `format`,
611
- // where it has one. This closes the attested set; the bytes are checked by the caller's noupsert.
612
- // `type` is usually not hash-attested, so a format flip would recategorize a hash-valid file
613
- // (commonjs<->module) with no hash mismatch.
509
+ // Closes the attested set of a frozen lockfile and bundle; the caller's noupsert checks the bytes.
614
510
  #assertFrozenAttests(noun, file, module, rel, bundled, format) {
615
511
  if (this.config.frozen) assert.ok(Object.hasOwn(module.files, rel), `${noun} not attested by the frozen lockfile: ${file}`)
616
512
  if (this.config.frozenBundle) assert.ok(bundled?.has(file), `${noun} not attested by the frozen bundle: ${file}`)
513
+ // `type` is usually not hash-attested, so a format flip would recategorize a hash-valid file.
617
514
  if (format == null) return
618
- if (this.config.frozen && this.#lockFormats !== null) {
619
- this.#assertAttestedFormat(this.#lockFormats, file, format, { what: 'observed', source: 'lockfile' })
620
- }
621
- if (this.config.frozenBundle && this.#bundleFormats !== null) {
622
- this.#assertAttestedFormat(this.#bundleFormats, file, format, { what: 'observed', source: 'frozen bundle' })
623
- }
515
+ if (this.config.frozen && this.#lockFormats !== null) this.#assertAttestedFormat(this.#lockFormats, file, format, 'observed', 'lockfile')
516
+ if (this.config.frozenBundle) this.#assertAttestedFormat(this.#bundleFormats, file, format, 'observed', 'frozen bundle')
624
517
  }
625
518
 
626
- // Verify one file's loader format. Callers only pass files that must be attested, so an
627
- // unattested file means a forged/extra entry -- fatal.
628
- #assertAttestedFormat(attestation, file, format, { what, source = 'lockfile' }) {
519
+ // Callers only pass files that must be attested: an unattested one is a forged/extra entry.
520
+ #assertAttestedFormat(attestation, file, format, what, source) {
629
521
  const attested = attestation.get(file)
630
522
  assert.ok(attested !== undefined, `${what} format for ${file} is not attested by the ${source}`)
631
523
  assert.equal(format, attested, `${what} format for ${file} mismatches the ${source}`)
632
524
  }
633
525
 
634
526
  assertEntry(url) {
635
- // No entry attestation to check against (lock=none/ignore, fresh build) -- #loadCodeBundle
636
- // already refuses a runnable code bundle that declares none.
527
+ // #loadCodeBundle already refuses a runnable code bundle that declares none.
637
528
  if (this.entries.size === 0) return
638
529
  const file = this.#canonicalFile(url)
639
530
  assert.ok(this.entries.has(file), `Unknown entry point: ${file}`)
640
531
  }
641
532
 
642
533
  static get preload() {
643
- for (const state of liveStates()) if (state.isPreload) return state
644
- return undefined
534
+ return liveStates().values().find((state) => state.isPreload)
645
535
  }
646
536
 
647
537
  // Public so State.preload works across copies.
@@ -654,8 +544,7 @@ export class State {
654
544
  return this.#claims.get(canonical)
655
545
  }
656
546
 
657
- // Refuse a path another live State (ANY copy) already claims; canonicalized so `./x`/`x`/symlinks
658
- // compare equal. `key` names the config option; nothing is claimed when it is unset.
547
+ // Refuse a path another live State (ANY copy) already claims; canonicalized so `./x`/`x`/symlinks compare equal.
659
548
  #claimWritePath(key, prefix = '') {
660
549
  const value = this.config[key]
661
550
  if (!value || !this.#claim) return
@@ -673,23 +562,40 @@ export class State {
673
562
  return VERSION
674
563
  }
675
564
 
676
- // PUBLIC (not private-field reads) so a sidecar from a different copy can read it across the
677
- // class brand.
565
+ // Public, so a sidecar from another copy can read it across the class brand.
678
566
  sidecarInheritance() {
679
567
  return {
680
568
  preloadRoot: this.#preloadRoot,
681
569
  lockImports: this.#lockImports,
682
570
  lockFormats: this.#lockFormats,
683
571
  lockfileLoaded: this.#lockfileLoaded,
572
+ policy: this.#policy,
573
+ absorbed: this.#absorbed,
684
574
  }
685
575
  }
686
576
 
687
- // WRITE-mode sidecar: this parent's lockData unions its imports/formats. Public (cross-copy).
577
+ // linksOn() of the root: what the State records under a link is its target's.
578
+ get #links() {
579
+ return (this.#linksOf ??= linksOn(this.root, this.#host))
580
+ }
581
+
582
+ // The Policy's path rules on a path recorded without reading it, in the attested zone and recorded nowhere yet.
583
+ #checkUnreadPath(file) {
584
+ if (this.#policy !== null && (this.config.full || hasNodeModulesSegment(file)) && !this.hashes.has(file) && !this.formats.has(file)) {
585
+ this.#policy.assertPath(file, undefined, this.#links(file))
586
+ }
587
+ }
588
+
589
+ // Before a run verifies against, serves or (adding to it) rewrites an artifact the Policy refuses.
590
+ #assertLoaded(artifact, path) {
591
+ this.#policy?.assertArtifact(artifact, { hint: ` -- recorded in ${relative(this.root, resolve(path))}; rebuild it (lock=replace / bundle=replace)`, links: this.#links })
592
+ }
593
+
594
+ // Public, cross-copy.
688
595
  registerSidecar(sidecar) {
689
596
  this.#sidecars.add(sidecar)
690
597
  }
691
598
 
692
- // LOAD-mode sidecar: served by getFs*Family, never a lockfile contributor. Public (cross-copy).
693
599
  registerReadSidecar(sidecar) {
694
600
  this.#readSidecars.add(sidecar)
695
601
  }
@@ -709,79 +615,74 @@ export class State {
709
615
  return file === '' ? '.' : file
710
616
  }
711
617
 
618
+ #urlOf(file) {
619
+ return pathToFileURL(resolve(this.root, file)).toString()
620
+ }
621
+
622
+ // As opposed to known by a stat record or a seeded hash alone.
623
+ #bundled(file) {
624
+ return this.sources.has(file) || this.resources.has(file)
625
+ }
626
+
627
+ // { url, absolute, file }: `file` the project-relative key (this.relative's), undefined outside the root.
712
628
  #canonical(url) {
713
629
  let result = this.#canonicalCache.get(url)
714
630
  if (result === undefined) {
715
631
  result = this.#canonicalUncached(url)
632
+ const file = relative(this.root, result.absolute)
633
+ result.file = relativeEscapes(file) ? undefined : file === '' ? '.' : file
716
634
  this.#canonicalCache.set(url, result)
717
635
  }
718
636
  return result
719
637
  }
720
638
 
721
- // A node_modules file whose REAL path is outside node_modules is a linked-in workspace source
722
- // (pnpm): canonicalize so it's recorded as a source, not a dependency. Everything else passes through.
639
+ // A node_modules file whose REAL path is outside node_modules is a linked-in workspace source (pnpm).
723
640
  #canonicalUncached(url) {
724
641
  const absolute = this.absolute(url)
725
642
  const file = relative(this.root, absolute)
726
643
  if (relativeEscapes(file) || !splitNodeModulesPath(file)) return { url, absolute }
727
644
  let real
728
- try {
729
- real = this.#host.realpath(absolute)
730
- } catch {
731
- return { url, absolute }
732
- }
645
+ try { real = this.#host.realpath(absolute) } catch { return { url, absolute } }
733
646
  if (real === absolute) return { url, absolute }
734
647
  const realFile = relative(this.root, real)
735
648
  if (relativeEscapes(realFile) || splitNodeModulesPath(realFile)) return { url, absolute }
736
649
  return { url: pathToFileURL(real).toString(), absolute: real }
737
650
  }
738
651
 
652
+ // Throws, as this.relative, for a URL outside the project root.
739
653
  #canonicalFile(url) {
740
- return this.relative(this.#canonical(url).absolute)
654
+ const { absolute, file } = this.#canonical(url)
655
+ return file ?? this.relative(absolute)
741
656
  }
742
657
 
743
- // #canonicalFile, or undefined for a URL outside the project root (the shims then pass through).
658
+ // Undefined outside the project root: the shims then pass through.
744
659
  #canonicalFileMaybe(url) {
745
- try {
746
- return this.#canonicalFile(url)
747
- } catch {
748
- return undefined
749
- }
660
+ try { return this.#canonical(url).file } catch { return undefined }
750
661
  }
751
662
 
752
- // True when `url` resolves (through any workspace symlink) into node_modules -- not a raw
753
- // `/node_modules/` substring test, so a symlinked workspace source is read from disk.
663
+ // Through any workspace symlink, not a raw substring: a symlinked workspace source is read from disk.
754
664
  inNodeModules(url) {
755
665
  const file = this.#canonicalFileMaybe(url)
756
666
  return file !== undefined && splitNodeModulesPath(file) !== null
757
667
  }
758
668
 
759
- // Nearest package.json at or above a directory (findPackageJSON is unreliable for a directory
760
- // URL, see #locateModule), refusing one there that can't be read as findPackageJSON does
761
- // (packageJSONStat). Bounded by the project root.
669
+ // findPackageJSON is unreliable for a directory (EISDIR, or the parent's); an unreadable one is refused as it does.
762
670
  #nearestPackageJsonFor(dirAbsolute) {
763
671
  for (let dir = dirAbsolute; ; dir = dirname(dir)) {
764
672
  const candidate = join(dir, 'package.json')
765
673
  if (packageJSONStat(this.#host, candidate)?.isFile()) return candidate
766
- // The root's package.json was just checked; never escape root.
767
674
  if (dir === this.root || dirname(dir) === dir) break
768
675
  }
769
676
  // Unreachable: state.root is resolved to a dir that has package.json.
770
677
  assert.fail(`no package.json at or above directory ${this.relative(dirAbsolute)}`)
771
678
  }
772
679
 
773
- // Canonicalize `url` and resolve/register the owning package bucket. `directory: true` walks up
774
- // ourselves -- Node's findPackageJSON is unreliable for a directory URL (EISDIR, or the parent's).
775
- // `synthetic: true` (EMPTY_MODULE_PATH) is never on disk: it must NOT exist, and walks up from its dir.
680
+ // The owning package bucket, registered. `synthetic` (EMPTY_MODULE_PATH) must NOT exist on disk.
776
681
  #locateModule(url, { directory = false, synthetic = false } = {}) {
777
- // Canonicalize first: a linked-in workspace source is recorded under its real path (a source).
778
682
  const { absolute } = this.#canonical(url)
779
- const file = this.relative(absolute)
780
- if (synthetic) {
781
- assert.ok(this.#host.stat(absolute) === null, `the reserved path ${file} is needed for a synthetic empty module, but the project has a real file there`)
782
- } else {
783
- assert.ok(this.#host.stat(absolute) !== null)
784
- }
683
+ const file = this.#canonicalFile(url)
684
+ if (synthetic) assert.ok(this.#host.stat(absolute) === null, `the reserved path ${file} is needed for a synthetic empty module, but the project has a real file there`)
685
+ else assert.ok(this.#host.stat(absolute) !== null)
785
686
 
786
687
  const closestPkgAbsolute = directory ? this.#nearestPackageJsonFor(absolute)
787
688
  : synthetic ? this.#nearestPackageJsonFor(dirname(absolute)) : this.#host.findPackageJSON(absolute)
@@ -790,18 +691,17 @@ export class State {
790
691
  const closestType = closestPkg.type
791
692
  assert.ok(closestType === undefined || closestType === 'module' || closestType === 'commonjs')
792
693
 
793
- // findPackageJSON may land on a `{"type":"module"}` sub-bucket marker lacking name/version.
694
+ // findPackageJSON may land on a `{"type":"module"}` marker lacking name/version.
794
695
  const { dir: nmRoot, rel: nmRel } = splitNodeModulesPath(file) ?? {}
795
- let pkgAbsolute, name, version, repo, vendored, subpackage
696
+ // rootPkg: a dependency's package.json, whose repo is read only where a record lacks one.
697
+ let pkgAbsolute, rootPkg, name, version, vendored, subpackage
796
698
  if (nmRoot) {
797
699
  pkgAbsolute = resolve(this.root, nmRoot, 'package.json')
798
- const rootPkg = pkgAbsolute === closestPkgAbsolute ? closestPkg : readPackageJSON(this.#host, pkgAbsolute)
700
+ rootPkg = pkgAbsolute === closestPkgAbsolute ? closestPkg : readPackageJSON(this.#host, pkgAbsolute)
799
701
  ;({ name, version } = rootPkg)
800
- repo = packageRepo(rootPkg)
801
702
  assert.ok(name, `Missing name in ${this.relative(pkgAbsolute)}`)
802
703
  assert.ok(version, `Missing version in ${this.relative(pkgAbsolute)}`)
803
- // A package.json in a package vendored into this one (bundle-util's vendor dirs) names that
804
- // package: the file is this one's, listed as that package's in `vendored`.
704
+ // A package.json of a package vendored into this one names that package: the file is listed in `vendored`.
805
705
  const vendorDir = vendorDirOf(name, nmRel)
806
706
  const vendor = vendorDir === undefined ? undefined : vendoredPackageOf(nmRel, vendorDir, this.#manifestsBelow(nmRoot))
807
707
  vendored = vendor?.vendored
@@ -809,8 +709,7 @@ export class State {
809
709
  const message = `Inconsistent data between ${this.relative(closestPkgAbsolute)} and ${this.relative(pkgAbsolute)}`
810
710
  // Allow fake module-name subpaths: the real module owns the prefix (npm wouldn't publish this).
811
711
  if (closestPkg.name !== undefined && closestPkg.name !== name) assert.ok(closestPkg.name.startsWith(`${name}/`), message)
812
- // A subpackage is the package's, listed in `subpackages`; where the closest package.json is its
813
- // own (no marker below it), its version is its own too.
712
+ // A subpackage whose own package.json is the closest has its own version.
814
713
  subpackage = this.#subpackageOf(nmRoot, nmRel, closestPkg, name)
815
714
  const ownVersion = subpackage !== undefined && closestPkg.name !== undefined
816
715
  if (closestPkg.version !== undefined && !ownVersion) assert.equal(closestPkg.version, version, message)
@@ -818,8 +717,7 @@ export class State {
818
717
  } else {
819
718
  pkgAbsolute = closestPkgAbsolute
820
719
  let json = closestPkg
821
- // A workspace package outside node_modules may omit version (private/unpublished): the
822
- // name alone claims the bucket. node_modules buckets (above) still require both.
720
+ // A workspace package may omit version (private/unpublished): the name alone claims the bucket.
823
721
  while (json.name === undefined) {
824
722
  assert.ok(Object.keys(json).every((k) => k === 'type'))
825
723
  const dir = dirname(pkgAbsolute)
@@ -828,34 +726,31 @@ export class State {
828
726
  json = readPackageJSON(this.#host, pkgAbsolute)
829
727
  }
830
728
  name = json.name
831
- // A literal `"version": null` folds to undefined here too: the parsers normalize the same
832
- // way, and recording null verbatim would split identity on the next run's re-read.
729
+ // `"version": null` folds to undefined as the parsers do, else identity splits on the next re-read.
833
730
  version = json.version ?? undefined
834
731
  }
835
732
  const pkg = this.relative(pkgAbsolute)
836
733
  assert.ok(pkg === 'package.json' || pkg.endsWith('/package.json'))
837
734
  const dir = dirname(pkg)
838
735
  if (nmRoot) assert.equal(dir, nmRoot)
839
- if (!this.modules.has(dir)) {
840
- // Tag node_modules buckets `npm`; workspace/top-level buckets carry no ecosystem.
841
- this.modules.set(dir, moduleInfo({ name, version, ecosystem: nmRoot ? 'npm' : undefined, repo, files: Object.create(null) }))
842
- }
736
+ // Checked as the run reaches it, before it is recorded; a hashed file was checked when recorded or loaded.
737
+ if (this.#policy !== null && (nmRoot || this.config.full) && !this.hashes.has(file)) this.#policy.assertPath(file, nmRoot ? { dir, name, version } : undefined, synthetic ? undefined : this.#links(file))
738
+ if (!this.modules.has(dir)) this.modules.set(dir, moduleInfo({ name, version, ecosystem: nmRoot ? 'npm' : undefined, files: Object.create(null) }))
843
739
  const module = this.modules.get(dir)
844
740
  if (module.name !== name || module.version !== version) {
845
741
  // Message built only on failure: addFile is hot, and the mismatch is a migration/drift event.
846
- const hint = (module.version == null) === (version == null) ? '' :
847
- ' -- an artifact from an older stasis may record a placeholder version for a workspace ' +
848
- 'package without one; regenerate it (lock=replace / bundle=replace)'
742
+ const hint = (module.version == null) === (version == null) ? '' : ' -- an artifact from an older stasis may record a ' +
743
+ 'placeholder version for a workspace package without one; regenerate it (lock=replace / bundle=replace)'
849
744
  assert.fail(`module identity mismatch for '${dir}': artifact records ` +
850
745
  `'${module.name}@${module.version ?? '(none)'}', package.json has '${name}@${version ?? '(none)'}'${hint}`)
851
746
  }
852
747
  // A dependency's repo is metadata, its package.json's, held to nothing: a record with none gets it.
853
- if (module.repo === undefined && repo !== undefined) this.modules.set(dir, moduleInfo({ ...module, repo }))
748
+ const repo = module.repo === undefined && rootPkg ? packageRepo(rootPkg) : undefined
749
+ if (repo !== undefined) this.modules.set(dir, moduleInfo({ ...module, repo }))
854
750
 
855
751
  return { absolute, file, dir, module: this.modules.get(dir), closestType, vendored, subpackage }
856
752
  }
857
753
 
858
- // The package.json in the directory `sub` of the package at `nmRoot`, parsed, or null where there is none.
859
754
  #manifestsBelow(nmRoot) {
860
755
  return (sub) => {
861
756
  const candidate = resolve(this.root, nmRoot, sub, 'package.json')
@@ -863,11 +758,8 @@ export class State {
863
758
  }
864
759
  }
865
760
 
866
- // The subpackage (isSubpackage) of the package `name` at `nmRoot` that its file `nmRel` is in: the nearest
867
- // package.json above the file with a name, past `{"type":"module"}` markers (namedManifestOf), walked by
868
- // the file's own path, not the real one findPackageJSON resolves through a symlinked install -> { dir, name,
869
- // version? }, or undefined. Looked for only where the closest package.json, `closest`, is such a marker or
870
- // names a subpath.
761
+ // The nearest named package.json above `nmRel`, walked by the file's own path, not findPackageJSON's real one
762
+ // through a symlinked install. Only where `closest` is a `{"type":"module"}` marker or names a subpath.
871
763
  #subpackageOf(nmRoot, nmRel, closest, name) {
872
764
  if (closest.name !== undefined && !(typeof closest.name === 'string' && closest.name.startsWith(`${name}/`))) return undefined
873
765
  const found = namedManifestOf(nmRel, this.#manifestsBelow(nmRoot))
@@ -875,35 +767,26 @@ export class State {
875
767
  return { dir: found.dir, name: found.pkg.name, ...(isPackageString(found.pkg.version) ? { version: found.pkg.version } : {}) }
876
768
  }
877
769
 
878
- // `resource: true` (legacy alias `isBinary: true`): format derived from bytes. `inferFormat: false`
879
- // records bytes without imposing a loader format (defers module-vs-commonjs to the loader).
770
+ // `resource: true` (legacy `isBinary`): format derived from bytes. `inferFormat: false`: the loader decides.
880
771
  addFile(url, options = {}) {
881
772
  this.#addFile(url, options)
882
773
  }
883
774
 
884
- // Record the empty module a disabled import resolves to (EMPTY_MODULE_PATH, the file `stasis bundle`
885
- // emits for a `browser` field `false`), attested like any file but never read from disk. -> its URL
775
+ // The empty module a disabled import (`browser` field `false`) resolves to, never read from disk. -> its URL
886
776
  addEmptyModule({ reason = 'run' } = {}) {
887
- const url = pathToFileURL(resolve(this.root, EMPTY_MODULE_PATH)).toString()
777
+ const url = this.#urlOf(EMPTY_MODULE_PATH)
888
778
  this.#addFile(url, { source: '', format: 'commonjs', inferFormat: false, reason }, { synthetic: true })
889
779
  return url
890
780
  }
891
781
 
892
782
  #addFile(url, { source, format, isEntry, isBinary, resource, inferFormat = true, reason = 'run', fsRead = false } = {}, { synthetic = false } = {}) {
893
783
  const asResource = resource === true || isBinary === true
894
- if (!asResource && Bundle.isResourceFormat(format)) {
895
- throw new Error(`addFile: format '${format}' requires resource: true`)
896
- }
897
- if (isStatFormat(format)) {
898
- throw new Error(`addFile: format '${format}' is a payload-free stat record; use addFsStat`)
899
- }
900
- if (asResource && isEntry) {
901
- throw new Error(`addFile: a resource can't be an entry (resource:true + isEntry:true)`)
902
- }
784
+ if (!asResource && Bundle.isResourceFormat(format)) throw new Error(`addFile: format '${format}' requires resource: true`)
785
+ if (isStatFormat(format)) throw new Error(`addFile: format '${format}' is a payload-free stat record; use addFsStat`)
786
+ if (asResource && isEntry) throw new Error(`addFile: a resource can't be an entry (resource:true + isEntry:true)`)
903
787
  const { absolute, file, dir, module, closestType, vendored, subpackage } = this.#locateModule(url, { synthetic })
904
788
 
905
- // Real content supersedes a payload-free stat record: drop it so the noupsert below records the
906
- // actual format instead of conflicting with 'stat:*'.
789
+ // Real content supersedes a payload-free stat record.
907
790
  if (isStatFormat(this.formats.get(file))) this.formats.delete(file)
908
791
 
909
792
  const ext = extname(file)
@@ -912,26 +795,20 @@ export class State {
912
795
  }
913
796
 
914
797
  if (!asResource && inferFormat) {
915
- const inferredFormat = EXT_FORMATS[ext] ??
798
+ const inferredFormat = nodeExtFormat(file) ??
916
799
  (closestType === undefined ? undefined : ext === '.js' ? closestType : ext === '.ts' ? `${closestType}-typescript` : undefined)
917
800
  if (inferredFormat !== undefined) {
918
- // A transforming preload (`stasis run --import tsx`) loads a TypeScript file as its
919
- // post-erasure family ('module-typescript' arrives as 'module'): attest the on-disk
920
- // format, not the transformer's runtime view. Anything else must still match exactly.
921
- if (format != null && format === erasedTypeScriptFormat(inferredFormat)) format = inferredFormat
922
- if (format != null) assert.equal(format, inferredFormat)
923
- else format = inferredFormat
801
+ // A transforming preload (`--import tsx`) loads TypeScript as its post-erasure family: attest the on-disk one.
802
+ if (format == null || format === erasedTypeScriptFormat(inferredFormat)) format = inferredFormat
803
+ else assert.equal(format, inferredFormat)
924
804
  } else if (format == null) {
925
- // No `type`: reuse an extension-appropriate format already recorded this session (the
926
- // loader's call, or the parent's for a sidecar) rather than re-defaulting to commonjs and mis-attesting.
805
+ // No `type`: one already recorded (the loader's, or a sidecar's parent's), not a commonjs re-default.
927
806
  const variants = UNTYPED_VARIANTS[ext]
928
807
  const known = this.formats.get(file) ?? this.#parent?.formats.get(file)
929
808
  format = variants?.includes(known) ? known : variants?.[0]
930
809
  } else if (ext === '.ts' && (format === 'commonjs' || format === 'module')) {
931
- // No `type`, but a transforming preload reported this .ts as its post-erasure family:
932
- // attest the on-disk '-typescript' variant, keeping same-stack replays self-consistent.
933
- // Stacks can still honestly disagree about a no-`type` .ts (tsx defaults to commonjs,
934
- // Node syntax-detects) -- that rightly surfaces as a format flip, not silently here.
810
+ // A transforming preload's post-erasure family again. Stacks can still disagree on a no-`type` .ts (tsx
811
+ // defaults to commonjs, Node syntax-detects): that rightly surfaces as a format flip.
935
812
  format = `${format}-typescript`
936
813
  }
937
814
  }
@@ -949,7 +826,6 @@ export class State {
949
826
  }
950
827
 
951
828
  const buf = typeof source === 'string' ? Buffer.from(source) : source
952
- // Verify a CALLER-PROVIDED source against disk; tautological when we read it ourselves.
953
829
  if (!sourceFromDisk && !synthetic) assert.deepStrictEqual(this.#host.readFile(absolute), buf)
954
830
 
955
831
  if (asResource) {
@@ -959,27 +835,22 @@ export class State {
959
835
  }
960
836
 
961
837
  if (isEntry) this.entries.add(file)
962
- const integrity = sha512integrity(source)
838
+ const integrity = sha512integrity(buf)
963
839
  noupsert(this.hashes, file, integrity)
964
840
  this.#filesCapturedThisRun.add(file)
965
- if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it; skip when the channel is off
966
- // Provenance for the bundle's `reason` field (null = the caller attributes it, see includePackageJson).
841
+ if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it
842
+ // A null reason: the caller attributes it (includePackageJson).
967
843
  if (this.config.bundle && reason !== null) {
968
844
  this.#recordReason(reason, file)
969
- // Track HOW 'run' saw this file so #reasonClaims can drop files run merely fs-READ post-plugin.
970
- if (reason === 'run') {
971
- if (fsRead && this.#pluginAttached) this.#fsReadPostPlugin.add(file)
972
- else this.#runOwn.add(file)
973
- }
845
+ if (reason === 'run') (fsRead && this.#pluginAttached ? this.#fsReadPostPlugin : this.#runOwn).add(file)
974
846
  }
975
847
  const rel = relative(dir, file)
976
848
  assert.ok(!relativeEscapes(rel))
977
849
 
978
- const inAttestedZone = hasNodeModulesSegment(dir) || this.config.full
850
+ const inAttestedZone = this.config.full || hasNodeModulesSegment(dir)
979
851
  if (inAttestedZone) this.#assertFrozenAttests('File', file, module, rel, asResource ? this.#bundleResources : this.#bundleSources, format)
980
852
 
981
- // Catch a format flip on already-attested bytes here; at write()'s #mergedFormats it would
982
- // surface only as a bare `Conflict`.
853
+ // Caught here: at write()'s #mergedFormats a format flip would surface only as a bare `Conflict`.
983
854
  if (this.config.writeLockfile && this.#lockFormats !== null && format != null && inAttestedZone) {
984
855
  const attested = this.#lockFormats.get(file)
985
856
  // A 'stat:*' baseline is not a flip: real content this run UPGRADES the record (#mergedFormats).
@@ -989,8 +860,7 @@ export class State {
989
860
 
990
861
  if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
991
862
  assert.equal(module.files[rel], integrity)
992
- // The vendored package or subpackage this file is in is reached: listed in the host's `vendored` or
993
- // `subpackages` (metadata, the first identity recorded for its dir standing).
863
+ // The first identity recorded for a vendored package's or subpackage's dir stands.
994
864
  for (const [field, entry] of [['vendored', vendored], ['subpackages', subpackage]]) {
995
865
  if (entry === undefined || Object.hasOwn(this.modules.get(dir)[field] ?? {}, entry.dir)) continue
996
866
  const { dir: sub, ...identity } = entry
@@ -1007,10 +877,8 @@ export class State {
1007
877
  if (!synthetic) this.#recordExecutable(file, absolute)
1008
878
  }
1009
879
 
1010
- // Observe `file` on disk and record or refute its execute bit; disk wins over whatever a lockfile/
1011
- // bundle seeded, but only a real observation refutes (`undefined` and Windows mean "unknowable").
1012
- // A record touches only this State; a REFUTATION must reach the whole family, else
1013
- // #mergedExecutable's union resurrects the bit from a sibling's seeded entry.
880
+ // Disk wins over a seeded lockfile/bundle, but only a real observation refutes (Windows can't). A REFUTATION
881
+ // must reach the whole family, else #mergedExecutable's union resurrects the bit from a sibling's seed.
1014
882
  #recordExecutable(file, absolute) {
1015
883
  const executable = observeExecutable(absolute, this.#host)
1016
884
  if (executable === true) {
@@ -1024,13 +892,12 @@ export class State {
1024
892
  for (const sidecar of root.sidecars()) sidecar.executable.delete(file)
1025
893
  }
1026
894
 
1027
- // Public so #recordExecutable reaches them across the class brand (duplicate stasis-core copies).
895
+ // Public, so #recordExecutable reaches them across the class brand.
1028
896
  sidecars() {
1029
897
  return this.#sidecars
1030
898
  }
1031
899
 
1032
- // Record an `fs.readFileSync` capture (--fs), carried as #readFileOptions says, else THROW -- an
1033
- // undeclared file would widen the attested set.
900
+ // An undeclared file THROWS: it would widen the attested set.
1034
901
  addFsFile(url, source) {
1035
902
  assert.ok(Buffer.isBuffer(source), 'addFsFile requires a Buffer source')
1036
903
  const path = fileURLToPath(url)
@@ -1041,22 +908,17 @@ export class State {
1041
908
  this.addFile(url, { source, ...options, fsRead: true })
1042
909
  }
1043
910
 
1044
- // The addFile options for a file read rather than loaded: classifyFormat routes the bytes -- a
1045
- // concrete format is code, null defers to the loader, and undefined (or a binary plist, whose bytes
1046
- // can't be the UTF-8 code its format implies) is a resource IFF in `resources`, else null.
911
+ // For a file read rather than loaded: a concrete format is code, null defers to the loader, and undefined (or a
912
+ // binary plist, which can't be the UTF-8 code its format implies) is a resource IFF in `resources`, else null.
1047
913
  #readFileOptions(path, source, resources = this.config.resources) {
1048
914
  const format = classifyFormat(path, { content: source })
1049
915
  if (format !== undefined && !isBinaryPlist(path, source)) return { format: format ?? undefined, inferFormat: false }
1050
- if (resources.has(pathExt(path) || basename(path).toLowerCase())) return { resource: true }
916
+ if (inResources(path, resources)) return { resource: true }
1051
917
  return null
1052
918
  }
1053
919
 
1054
- // Attest the files stasis.config.json's `extra` names (expandExtra: paths, directories and globs,
1055
- // as `stasis add` takes them, swept through the State's host) beside what the run reached, each
1056
- // carried as an fs read is (#readFileOptions) unless the run captured it itself, whose record
1057
- // stands. The whole set is validated before a file is added, every offender named at once.
1058
- // `outputs` are the paths written, never taken in: by default the ones write() writes. `resources`
1059
- // is the allowlist, the config's by default.
920
+ // Each file `extra` names is carried as an fs read is, unless the run captured it itself. The whole set is
921
+ // validated first, naming every offender at once; `outputs` never go in.
1060
922
  includeExtra({ outputs = this.#writeTargets(), resources = this.config.resources } = {}) {
1061
923
  if (this.config.extra.length === 0) return
1062
924
  // Resolved as write() writes them: a relative one from the cwd.
@@ -1095,56 +957,56 @@ export class State {
1095
957
  if (this.config.bundle) for (const file of captured) this.#recordReason('extra', file)
1096
958
  }
1097
959
 
1098
- // The artifacts write() writes: the lockfile (never a sidecar's) and the bundle(s), as configured.
960
+ get #lockPath() {
961
+ return this.config.lockFile || join(this.root, LOCK_FILE)
962
+ }
963
+
964
+ get #bundlePath() {
965
+ return this.config.bundleFile || join(this.root, BUNDLE_FILE)
966
+ }
967
+
968
+ // A sidecar never writes the lockfile.
1099
969
  #writeTargets() {
1100
- const lock = this.config.writeLockfile && !this.#parent ? [this.config.lockFile || join(this.root, FILE_LOCK)] : []
1101
- const bundles = this.config.writeBundle ? [this.config.bundleFile || join(this.root, FILE_CODE), this.config.resourcesBundleFile] : []
970
+ const lock = this.config.writeLockfile && !this.#parent ? [this.#lockPath] : []
971
+ const bundles = this.config.writeBundle ? [this.#bundlePath, this.config.resourcesBundleFile] : []
1102
972
  return [...lock, ...bundles].filter((file) => file !== undefined)
1103
973
  }
1104
974
 
1105
- // Record an `fs.readdirSync` capture: the listing is SORTED for reproducibility and stored as a
1106
- // 'directory'-format payload.
975
+ // The listing is SORTED, for reproducibility.
1107
976
  addFsDir(url, names) {
1108
- assert.ok(Array.isArray(names) && names.every((n) => typeof n === 'string'),
1109
- 'addFsDir requires an array of string names')
977
+ assert.ok(Array.isArray(names) && names.every((n) => typeof n === 'string'), 'addFsDir requires an array of string names')
1110
978
  const { file, dir, module } = this.#locateModule(url, { directory: true })
1111
- // As in addFile: a real record supersedes a payload-free stat record.
1112
979
  if (isStatFormat(this.formats.get(file))) this.formats.delete(file)
1113
980
  const content = JSON.stringify(names.toSorted())
1114
- const format = 'directory'
1115
981
  const integrity = sha512integrity(content)
1116
982
  noupsert(this.hashes, file, integrity)
1117
- if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it; skip when the channel is off
983
+ if (this.config.childProcess) this.#observed.add(file) // only a child's shardSnapshot reads it
1118
984
  if (this.config.bundle) this.#recordReason('run', file)
1119
985
  const rel = relative(dir, file)
1120
986
  assert.ok(!relativeEscapes(rel))
1121
987
 
1122
- if (hasNodeModulesSegment(dir) || this.config.full) this.#assertFrozenAttests('Directory', file, module, rel, this.#bundleResources, format)
988
+ if (this.config.full || hasNodeModulesSegment(dir)) this.#assertFrozenAttests('Directory', file, module, rel, this.#bundleResources, 'directory')
1123
989
 
1124
990
  if (!Object.hasOwn(module.files, rel)) module.files[rel] = integrity
1125
991
  assert.equal(module.files[rel], integrity)
1126
992
  if (this.config.bundle) noupsert(this.resources, file, content)
1127
- noupsert(this.formats, file, format)
1128
- // A listing is never executable (both artifacts' parsers refuse one in `executable`), so drop
1129
- // any bit a prior content record left on this path.
993
+ noupsert(this.formats, file, 'directory')
994
+ // Both artifacts' parsers refuse a listing in `executable`.
1130
995
  this.executable.delete(file)
1131
996
  }
1132
997
 
1133
- // Record a stat capture as a PAYLOAD-FREE record (`formats[file] = 'stat:file'|'stat:directory'`,
1134
- // no bytes/hash/module-files entry), so getFsStat answers isFile()/isDirectory() at load. Skipped
1135
- // when a content record already exists; upsertFormat lets a real format win, a kind flip is fatal.
998
+ // A PAYLOAD-FREE record (no bytes/hash/module-files entry), so getFsStat answers at load. A real format wins.
1136
999
  addFsStat(url, kind) {
1137
1000
  assert.ok(kind === 'file' || kind === 'directory', `addFsStat: unsupported kind '${kind}'`)
1138
1001
  const file = this.#canonicalFile(url)
1139
- if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) return
1002
+ if (this.hashes.has(file) || this.#bundled(file)) return
1003
+ this.#checkUnreadPath(file)
1140
1004
  upsertFormat(this.formats, file, `stat:${kind}`)
1141
- // Only forward when a stat record actually landed -- upsertFormat may have yielded to a real
1142
- // format, and forwarding THAT would ship an entry mergeShard's stat replay ignores.
1005
+ // Only where a stat record landed: one yielded to a real format is an entry mergeShard's stat replay ignores.
1143
1006
  if (this.config.childProcess && isStatFormat(this.formats.get(file))) this.#observed.add(file)
1144
1007
  }
1145
1008
 
1146
- // Serve an `fs.readFileSync` from the bundle: raw bytes, or undefined when uncaptured (the hook
1147
- // falls back to disk) or a captured directory. Served by presence, not format.
1009
+ // Undefined (the hook falls back to disk) when uncaptured or a directory. Served by presence, not format.
1148
1010
  getFsFile(url) {
1149
1011
  if (!this.hasFsFileContent(url)) return undefined
1150
1012
  const { source } = this.getFile(url)
@@ -1159,58 +1021,45 @@ export class State {
1159
1021
  return names
1160
1022
  }
1161
1023
 
1162
- // Every ancestor directory implied by recorded keys; built once.
1024
+ // Built once, at load: stat records prove existence too.
1163
1025
  #impliedDirs() {
1164
1026
  if (this.#impliedDirIndex) return this.#impliedDirIndex
1165
- const dirs = new Set(['.'])
1166
- const add = (key) => {
1027
+ const dirs = (this.#impliedDirIndex = new Set(['.']))
1028
+ const stats = this.formats.entries().filter(([, format]) => isStatFormat(format)).map(([key]) => key)
1029
+ for (const key of [...this.sources.keys(), ...this.resources.keys(), ...stats]) {
1167
1030
  const parts = key.split('/')
1168
1031
  for (let i = 1; i < parts.length; i++) dirs.add(parts.slice(0, i).join('/'))
1169
1032
  }
1170
- for (const key of this.sources.keys()) add(key)
1171
- for (const key of this.resources.keys()) add(key)
1172
- // Stat records prove existence too, so they imply their ancestor dirs.
1173
- for (const [key, format] of this.formats) if (isStatFormat(format)) add(key)
1174
- this.#impliedDirIndex = dirs
1175
1033
  return dirs
1176
1034
  }
1177
1035
 
1178
- // Classify a captured path for the stat shim: 'directory', 'file', or undefined (uncaptured -> the
1179
- // shim falls back to disk). Order matters: 'directory' first, content before stat records, implied
1180
- // dirs last. NB: 'file' means "recorded", NOT "bytes serveable" (use hasFsFileContent).
1036
+ // Undefined: uncaptured, the shim falls back to disk. 'file' means "recorded", NOT "bytes serveable".
1181
1037
  getFsStat(url) {
1182
1038
  const file = this.#canonicalFileMaybe(url)
1183
1039
  if (file === undefined) return undefined
1184
1040
  const format = this.formats.get(file)
1185
1041
  if (format === 'directory') return 'directory'
1186
- if (this.sources.has(file) || this.resources.has(file) || format === 'stat:file') return 'file'
1042
+ if (this.#bundled(file) || format === 'stat:file') return 'file'
1187
1043
  if (format === 'stat:directory' || this.#impliedDirs().has(file)) return 'directory'
1188
1044
  return undefined
1189
1045
  }
1190
1046
 
1191
- // True when the bundle carries actual BYTE content for `url` (not a directory/stat-only record).
1192
1047
  hasFsFileContent(url) {
1193
1048
  const file = this.#canonicalFileMaybe(url)
1194
- return file !== undefined && this.formats.get(file) !== 'directory' && (this.sources.has(file) || this.resources.has(file))
1049
+ return file !== undefined && this.formats.get(file) !== 'directory' && this.#bundled(file)
1195
1050
  }
1196
1051
 
1197
- // --fs <-> bundler-plugin coordination: CAPTURE skips a read a write-mode sidecar already attests,
1198
- // else --fs re-records the bundler's whole module graph into the main bundle and defeats the split.
1052
+ // Capture skips these, else --fs re-records the bundler's module graph into the main bundle. Not getFsStat:
1053
+ // skipping an implied dir or stat-only record would leave a real read unattested.
1199
1054
  attestedBySidecar(url) {
1200
- // hasFsFileContent, not getFsStat: over-skipping on an implied dir or stat-only record would
1201
- // leave a real read unattested.
1202
- for (const s of this.#sidecars) if (s.hasFsFileContent(url)) return true
1203
- return false
1055
+ return this.#sidecars.values().some((s) => s.hasFsFileContent(url))
1204
1056
  }
1205
1057
 
1206
- // getFs* over this State first, then its load-mode sidecars. Only #readSidecars -- write-mode
1207
- // sidecars only capture.
1058
+ // Write-mode sidecars only capture.
1208
1059
  #fromFamily(method, url) {
1209
- const own = this[method](url)
1210
- if (own !== undefined) return own
1211
- for (const s of this.#readSidecars) {
1212
- const v = s[method](url)
1213
- if (v !== undefined) return v
1060
+ for (const state of [this, ...this.#readSidecars]) {
1061
+ const value = state[method](url)
1062
+ if (value !== undefined) return value
1214
1063
  }
1215
1064
  return undefined
1216
1065
  }
@@ -1228,37 +1077,26 @@ export class State {
1228
1077
  }
1229
1078
 
1230
1079
  getFile(url) {
1231
- // Wrap #canonicalFile so a URL escaping state.root gives a contextual error, not a bare assert.
1232
1080
  let file
1233
1081
  try { file = this.#canonicalFile(url) }
1234
1082
  catch (cause) { throw new Error(`stasis: file is outside the project root: ${url}`, { cause }) }
1235
1083
  const format = this.formats.get(file) // might be undefined e.g. for some bundlers
1236
- // A missing entry throws: this single getFile call is the fail-closed gate.
1237
- let source
1238
- if (Bundle.isResourceFormat(format)) {
1239
- source = this.resources.get(file)
1240
- if (source === undefined) throw new Error(`stasis: file not attested in bundle: ${url}`)
1241
- if (format === 'resource:base64') source = Buffer.from(source, 'base64')
1242
- } else {
1243
- source = this.sources.get(file)
1244
- if (source === undefined) {
1245
- // Stat-record case: the bundle knows the path but not its bytes -- "not attested" would misdiagnose it.
1246
- if (isStatFormat(format)) {
1247
- throw new Error(
1248
- `stasis: only a payload-free stat record ('${format}') is attested for ${url} -- ` +
1249
- `the capture stat'd this path but never read or imported it, so the bundle has no bytes to serve`
1250
- )
1251
- }
1252
- throw new Error(`stasis: file not attested in bundle: ${url}`)
1084
+ // The fail-closed gate.
1085
+ let source = (Bundle.isResourceFormat(format) ? this.resources : this.sources).get(file)
1086
+ if (source === undefined) {
1087
+ if (isStatFormat(format)) {
1088
+ throw new Error(`stasis: only a payload-free stat record ('${format}') is attested for ${url} -- the capture ` +
1089
+ "stat'd this path but never read or imported it, so the bundle has no bytes to serve")
1253
1090
  }
1091
+ throw new Error(`stasis: file not attested in bundle: ${url}`)
1254
1092
  }
1255
- // Without a lockfile the bundle is self-attesting -- verifying its hash against its own bytes is a tautology.
1093
+ if (format === 'resource:base64') source = Buffer.from(source, 'base64')
1094
+ // Without a lockfile the bundle is self-attesting.
1256
1095
  if (this.config.useLockfile) assert.equal(this.hashes.get(file), sha512integrity(source))
1257
1096
  return { source, format }
1258
1097
  }
1259
1098
 
1260
- // The recorded LOADER format, or undefined. Masks 'stat:*' (existence, not how bytes parse) so a
1261
- // stat'd require.resolve target isn't refused by the resolve hook's format gate; getFile does NOT mask.
1099
+ // Masks 'stat:*' (existence, not parsing), so the resolve hook's format gate passes a stat'd target; getFile doesn't.
1262
1100
  #loaderFormat(file) {
1263
1101
  const format = this.formats.get(file)
1264
1102
  return isStatFormat(format) ? undefined : format
@@ -1278,9 +1116,7 @@ export class State {
1278
1116
  return `${cond} (with: ${JSON.stringify(sorted)})`
1279
1117
  }
1280
1118
 
1281
- // An already-resolved ABSOLUTE require()/import() target as the specifier would be a
1282
- // machine-specific lockfile key, so renormalize in-root ones against the IMPORTING FILE's dir.
1283
- // Must be applied identically in addImport and getImport so keys written and queried agree.
1119
+ // An ABSOLUTE in-root specifier would be a machine-specific key: renormalized against the IMPORTING FILE's dir.
1284
1120
  #canonicalSpecifier(parentURL, specifier) {
1285
1121
  if (typeof specifier !== 'string') return specifier
1286
1122
  const path = specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier
@@ -1293,36 +1129,31 @@ export class State {
1293
1129
 
1294
1130
  addImport(parentURL, specifier, url, { conditions = '*', format, importAttributes } = {}) {
1295
1131
  if (conditions !== '*') assert.ok(Array.isArray(conditions))
1296
- // Capture Node's require-condition set on the first require()-context edge (see #requireConditions).
1297
- if (this.#requireConditions === null && Array.isArray(conditions) &&
1298
- conditions.includes('require') && !conditions.includes('import')) {
1299
- this.#requireConditions = conditions
1300
- }
1132
+ if (this.#requireConditions === null && resolutionContext(conditions) === 'require') this.#requireConditions = conditions
1301
1133
  assert.ok(parentURL, 'addImport requires a parent (entries go through addFile)')
1302
1134
  const parent = this.#canonicalFile(parentURL)
1303
1135
  const file = this.#canonicalFile(url)
1304
1136
  specifier = this.#canonicalSpecifier(parentURL, specifier)
1305
1137
  const key = this.#conditionsKey(conditions, importAttributes)
1306
1138
 
1307
- // Frozen runs verify disk resolutions against the lockfile: hashes can't see a redirect to
1308
- // another attested file. Unknown edges are tolerated only for workspace parents in nm scope.
1139
+ // Hashes can't see a redirect to another attested file. Workspace parents in nm scope may be unknown.
1309
1140
  const tolerateUnknown = !this.config.full && !hasNodeModulesSegment(parent)
1310
1141
  if (this.config.frozen && this.#lockImports !== null) {
1311
1142
  this.#assertAttestedResolution(this.#lockImports, key, parent, specifier, file, { what: 'observed', source: 'lockfile', tolerateUnknown })
1312
1143
  }
1313
- if (this.config.frozenBundle && this.#bundleImports !== null) {
1144
+ if (this.config.frozenBundle) {
1314
1145
  this.#assertAttestedResolution(this.#bundleImports, key, parent, specifier, file, { what: 'observed', source: 'frozen bundle', tolerateUnknown })
1315
1146
  }
1316
1147
 
1317
1148
  noupsert(nestedMap(nestedMap(this.imports, key), parent), specifier, file)
1318
- // upsertFormat, not noupsert: a real format must replace a weak 'stat:*' record from an --fs stat.
1319
- if (format) upsertFormat(this.formats, file, format)
1149
+ // A real format replaces a weak 'stat:*' record.
1150
+ if (format) {
1151
+ this.#checkUnreadPath(file)
1152
+ upsertFormat(this.formats, file, format)
1153
+ }
1320
1154
  }
1321
1155
 
1322
- // Under bundle=load, verify a resolution a bundler made on its own (Metro resolves the app's
1323
- // imports itself, where the loader and the esbuild/webpack plugins take them from the bundle): it
1324
- // must be the edge the bundle attests, for `platform` where the edge is per-platform. Unknown
1325
- // edges are tolerated only where addImport tolerates them, for workspace parents in nm scope.
1156
+ // A resolution a bundler made itself (Metro's) must be the attested edge, `platform`'s where per-platform.
1326
1157
  assertImport(parentURL, specifier, url, { conditions = '*', importAttributes, platform: targetPlatform } = {}) {
1327
1158
  assert.ok(this.config.loadBundle, 'assertImport checks a resolution against a loaded bundle (bundle=load)')
1328
1159
  if (conditions !== '*') assert.ok(Array.isArray(conditions))
@@ -1337,28 +1168,23 @@ export class State {
1337
1168
 
1338
1169
  getImport(parentURL, specifier, { conditions = '*', importAttributes, platform } = {}) {
1339
1170
  if (conditions !== '*') assert.ok(Array.isArray(conditions))
1340
- assert.ok(parentURL, 'getImport requires a parent') // matches addImport; #canonicalSpecifier needs it
1171
+ assert.ok(parentURL, 'getImport requires a parent')
1341
1172
  const parent = this.#canonicalFile(parentURL)
1342
1173
  specifier = this.#canonicalSpecifier(parentURL, specifier)
1343
1174
  const key = this.#conditionsKey(conditions, importAttributes)
1344
- // Static bundles store edges under '*' (Node's condition set is unpredictable); the runtime
1345
- // loader records precise conditions, so the specific lookup wins.
1175
+ // Static bundles store edges under '*' (Node's condition set is unpredictable); the precise key wins.
1346
1176
  let file = this.imports.get(key)?.get(parent)?.get(specifier)
1347
1177
  if (file === undefined && key !== '*') file = this.imports.get('*')?.get(parent)?.get(specifier)
1348
1178
  if (file === undefined) {
1349
- // The edge may be recorded under a DIFFERENT conditions bucket -- resolveBundled scans every
1350
- // bucket related to these conditions.
1179
+ // The edge may be under another conditions bucket.
1351
1180
  if (this.config.loadBundle) {
1352
1181
  const abs = this.resolveBundled(parentURL, specifier, { conditions })
1353
1182
  if (abs !== undefined) return { url: pathToFileURL(abs).toString(), format: this.#loaderFormat(relative(this.root, abs)) }
1354
1183
  }
1355
- // Node's ERR_MODULE_NOT_FOUND shape, not a bare assert: dynamic-import callers guard on that
1356
- // code, which ERR_ASSERTION escapes.
1184
+ // Not a bare assert: dynamic-import callers guard on ERR_MODULE_NOT_FOUND.
1357
1185
  throw Object.assign(new Error(`Cannot find module '${specifier}' imported from ${parent}`), { code: 'ERR_MODULE_NOT_FOUND' })
1358
1186
  }
1359
- // A --metro per-platform edge is a { platform: file } Map: `platform`'s target where the caller
1360
- // builds for one (`stasis build --metro=ios`), which the edge must record. Plain bundle=load has
1361
- // no platform context to pick one, so fail closed rather than feed a Map where a path is expected.
1187
+ // A --metro per-platform edge is a { platform: file } Map; plain bundle=load has no platform to pick by.
1362
1188
  if (typeof file !== 'string') {
1363
1189
  if (!file.has(platform)) {
1364
1190
  const message = platform === undefined
@@ -1368,24 +1194,18 @@ export class State {
1368
1194
  }
1369
1195
  file = file.get(platform)
1370
1196
  }
1371
- return { url: pathToFileURL(resolve(this.root, file)).toString(), format: this.#loaderFormat(file) }
1197
+ return { url: this.#urlOf(file), format: this.#loaderFormat(file) }
1372
1198
  }
1373
1199
 
1374
- // Resolve a CJS require() target to a bundled absolute path, or undefined to defer to Node (the
1375
- // hooks.js CJS shim needs this: registerHooks can't intercept Module._resolveFilename). Matches
1376
- // under the buckets related to `conditions`: every one, except that where conditions decide the
1377
- // resolution (readsConditions), a key of the other context (see resolutionContext) is another
1378
- // resolution, never served. Node asks an edge under its own context's set on every version and
1379
- // code path, the rest varying by flag alone (--conditions, --no-addons, require(esm) off); a
1380
- // relative edge resolves alike under either, as a capture's replay can ask it (tsx's CJS lane does).
1381
- // Divergent buckets are settled by those of the lookup's context where they agree -- a
1382
- // --conditions build's require() and import() targets, each under a key plain node never asks --
1383
- // else defer.
1200
+ // A CJS require() target as a bundled path, or undefined to defer to Node. Every conditions bucket matches,
1201
+ // but where conditions decide (readsConditions), the other context's keys are another resolution.
1384
1202
  resolveBundled(parentURL, specifier, { conditions = '*' } = {}) {
1385
1203
  const parent = this.#canonicalFileMaybe(parentURL)
1386
1204
  if (parent === undefined) return undefined
1387
1205
  const spec = this.#canonicalSpecifier(parentURL, specifier)
1388
1206
  const context = resolutionContext(conditions)
1207
+ // Node's own context's set is stable, the rest varying by flag; a relative edge resolves alike under either,
1208
+ // as a capture's replay can ask it (tsx's CJS lane does).
1389
1209
  const contextual = context !== null && readsConditions(spec)
1390
1210
  const matches = new Set()
1391
1211
  const ownContext = new Set()
@@ -1397,18 +1217,22 @@ export class State {
1397
1217
  matches.add(file)
1398
1218
  if (context !== null && keyContext === context) ownContext.add(file)
1399
1219
  }
1220
+ // Divergent buckets are settled by the lookup's own context where those agree, else defer.
1400
1221
  const candidates = matches.size === 1 ? matches : ownContext
1401
1222
  if (candidates.size !== 1) return undefined
1402
1223
  const [only] = candidates
1403
- // Per-platform { platform: file } Map (--metro) has no single path: defer to native, not resolve()'s TypeError.
1224
+ // A per-platform Map (--metro) has no single path.
1404
1225
  return typeof only === 'string' ? resolve(this.root, only) : undefined
1405
1226
  }
1406
1227
 
1407
- // Union of lockfile-attested and live resolutions, so a partial lock=add run extends without
1408
- // dropping edges. Conflicting targets are fatal (add semantics).
1228
+ get #contributors() {
1229
+ return [this, ...this.#sidecars]
1230
+ }
1231
+
1232
+ // So a partial lock=add run extends without dropping edges.
1409
1233
  #mergedImports() {
1410
1234
  const merged = new Map()
1411
- const mergeIn = (imports) => {
1235
+ for (const imports of [this.#lockImports ?? [], ...this.#contributors.map((state) => state.imports)]) {
1412
1236
  for (const [conditions, byParent] of imports) {
1413
1237
  const mergedParents = nestedMap(merged, conditions)
1414
1238
  for (const [parent, specifiers] of byParent) {
@@ -1417,44 +1241,27 @@ export class State {
1417
1241
  }
1418
1242
  }
1419
1243
  }
1420
- if (this.#lockImports) mergeIn(this.#lockImports)
1421
- mergeIn(this.imports)
1422
- // Write-mode sidecars too, so the lockfile attests their edges for frozen verify.
1423
- for (const sidecar of this.#sidecars) mergeIn(sidecar.imports)
1424
1244
  return merged
1425
1245
  }
1426
1246
 
1427
- // Union of lockfile-attested and observed formats (+ sidecars'), append-only like #mergedImports,
1428
- // except that a weak 'stat:*' is UPGRADED by real content; two real formats stay fatal.
1247
+ // Append-only like #mergedImports, except that a weak 'stat:*' is UPGRADED by real content.
1429
1248
  #mergedFormats() {
1430
1249
  const merged = new Map(this.#lockFormats ?? [])
1431
- for (const [file, format] of this.formats) upsertFormat(merged, file, format)
1432
- for (const sidecar of this.#sidecars) {
1433
- for (const [file, format] of sidecar.formats) upsertFormat(merged, file, format)
1434
- }
1435
- // A payload-free 'stat:*' must never survive on a file that now carries a content hash: a stat
1436
- // entry re-seeded from #lockFormats is beyond addFile's delete, and an ambiguous .js/.ts yields
1437
- // no real format to override it above.
1438
- for (const [file, format] of merged) {
1439
- if (isStatFormat(format) && this.hashes.has(file)) merged.delete(file)
1250
+ for (const { formats } of this.#contributors) {
1251
+ for (const [file, format] of formats) upsertFormat(merged, file, format)
1440
1252
  }
1253
+ // Never on a file carrying a content hash: one re-seeded from #lockFormats is beyond addFile's delete,
1254
+ // and an ambiguous .js/.ts yields no real format to override it.
1255
+ for (const [file, format] of merged) if (isStatFormat(format) && this.hashes.has(file)) merged.delete(file)
1441
1256
  return merged
1442
1257
  }
1443
1258
 
1444
- // The executable list the LOCKFILE attests (this State's + write-mode sidecars'), narrowed by
1445
- // scope to the keys it will SERIALIZE: a non-full-scope lockfile drops its workspace buckets, and
1446
- // an entry for a dropped bucket writes a lockfile Lockfile.parse then refuses.
1259
+ // Narrowed to the keys the lockfile SERIALIZES: an entry for a dropped bucket would be refused at parse.
1447
1260
  #mergedExecutable(formats) {
1448
- const all = new Set(this.executable)
1449
- for (const sidecar of this.#sidecars) for (const file of sidecar.executable) all.add(file)
1261
+ const all = new Set(this.#contributors.flatMap((state) => [...state.executable]))
1450
1262
  return narrowExecutable(all, { modules: this.modules, formats, scope: this.config.scope })
1451
1263
  }
1452
1264
 
1453
- // The executable list ONE bundle declares, narrowed to that half's own buckets and formats.
1454
- #bundleExecutable(modules, formats) {
1455
- return narrowExecutable(this.executable, { modules, formats, scope: this.config.scope })
1456
- }
1457
-
1458
1265
  get lockfile() {
1459
1266
  const formats = this.#mergedFormats()
1460
1267
  return new Lockfile({
@@ -1471,7 +1278,6 @@ export class State {
1471
1278
  return this.lockfile.serialize()
1472
1279
  }
1473
1280
 
1474
- // Pair each module's recorded file list with content from perFile, dropping modules with none.
1475
1281
  #bundleModules(perFile) {
1476
1282
  const modules = new Map()
1477
1283
  for (const [dir, info] of this.modules) {
@@ -1485,51 +1291,32 @@ export class State {
1485
1291
  return modules
1486
1292
  }
1487
1293
 
1488
- // Files fs-READ after this point for a plugin (one it bundles, or a manifest of a package it
1489
- // bundles) are the plugin's, not run's (see #reasonClaims). Public, cross-copy.
1294
+ // Files fs-READ after this point for a plugin are the plugin's, not run's (#reasonClaims). Public, cross-copy.
1490
1295
  markPluginAttached() {
1491
1296
  this.#pluginAttached = true
1492
1297
  }
1493
1298
 
1494
1299
  #recordReason(reason, file) {
1495
- let set = this.#reasonFiles.get(reason)
1496
- if (set === undefined) this.#reasonFiles.set(reason, set = new Set())
1497
- set.add(file)
1498
- }
1499
-
1500
- // Seed `reason` from an absorbed on-disk bundle so a bundle=add re-run preserves other consumers'
1501
- // attribution. Called from every absorb site, including the sidecar's.
1502
- #seedReasonFromBundle(bundle) {
1503
- if (!this.config.writeBundle || bundle.reason === undefined) return
1504
- for (const [consumer, files] of Object.entries(bundle.reason)) {
1505
- for (const file of files) this.#recordReason(consumer, file)
1506
- }
1300
+ upsert(this.#reasonFiles, reason, () => new Set()).add(file)
1507
1301
  }
1508
1302
 
1509
- // Consumer -> the files it claims: #reasonFiles, less what run merely fs-READ after a plugin
1510
- // attached on a plugin's behalf (never its own: #runOwn). That is a file a plugin also bundles,
1511
- // which is the plugin's. A package.json fs-read after a plugin attached, of a package a plugin
1512
- // bundles, is one a bundler reads to resolve (Metro's resolver does) but never bundles itself:
1513
- // it is that plugin's too, and stays run's only where run has its own claim on it or claims
1514
- // another file of the package. `extra` names files and bundles none, so it is no plugin here.
1303
+ // #reasonFiles less what run only fs-READ after a plugin attached that a plugin bundles. `extra` bundles nothing.
1515
1304
  #reasonClaims() {
1516
- const pluginFiles = new Set()
1517
- for (const [who, recorded] of this.#reasonFiles) {
1518
- if (who !== 'run' && who !== 'extra') for (const file of recorded) pluginFiles.add(file)
1519
- }
1305
+ const isPlugin = (who) => who !== 'run' && who !== 'extra'
1306
+ const pluginFiles = new Set(this.#reasonFiles.entries().filter(([who]) => isPlugin(who)).flatMap(([, recorded]) => recorded))
1520
1307
  const fsReadOnly = (file) => this.#fsReadPostPlugin.has(file) && !this.#runOwn.has(file)
1521
- const claims = new Map()
1522
- for (const [who, recorded] of this.#reasonFiles) {
1523
- claims.set(who, new Set(who === 'run' ? [...recorded].filter((file) => !(fsReadOnly(file) && pluginFiles.has(file))) : recorded))
1524
- }
1308
+ const claimOf = (who, recorded) => (who === 'run' ? [...recorded].filter((file) => !(fsReadOnly(file) && pluginFiles.has(file))) : recorded)
1309
+ const claims = new Map(this.#reasonFiles.entries().map(([who, recorded]) => [who, new Set(claimOf(who, recorded))]))
1525
1310
  const run = claims.get('run')
1526
1311
  if (run === undefined || pluginFiles.size === 0 || this.#fsReadPostPlugin.size === 0) return claims
1312
+ // A package.json fs-read post-plugin, of a package a plugin bundles, is one a bundler reads to resolve but
1313
+ // never bundles: that plugin's too, and run's only where run has its own claim on it or another file.
1527
1314
  for (const [dir, module] of this.modules) {
1528
1315
  const rels = Object.keys(module.files)
1529
1316
  const read = new Set(rels.filter((rel) => basename(rel) === 'package.json').map((rel) => moduleFileKey(dir, rel)).filter((file) => this.#fsReadPostPlugin.has(file) && !pluginFiles.has(file)))
1530
1317
  if (read.size === 0) continue
1531
1318
  const files = rels.map((rel) => moduleFileKey(dir, rel)).filter((file) => !read.has(file))
1532
- const plugins = [...claims].filter(([who, claimed]) => who !== 'run' && who !== 'extra' && files.some((file) => claimed.has(file))).map(([, claimed]) => claimed)
1319
+ const plugins = [...claims].filter(([who, claimed]) => isPlugin(who) && files.some((file) => claimed.has(file))).map(([, claimed]) => claimed)
1533
1320
  if (plugins.length === 0) continue // no plugin bundles the package: run's own read
1534
1321
  const runClaimsPackage = files.some((file) => run.has(file))
1535
1322
  for (const file of read) {
@@ -1540,54 +1327,36 @@ export class State {
1540
1327
  return claims
1541
1328
  }
1542
1329
 
1543
- // The bundle's informational `reason` map, restricted to `bundledFiles`; only when >1 consumer
1544
- // contributed. Never attested.
1545
- #bundleReason(bundledFiles) {
1546
- const inBundle = bundledFiles instanceof Set ? bundledFiles : new Set(bundledFiles)
1330
+ // Informational, never attested.
1331
+ #bundleReason(contents) {
1547
1332
  const claims = this.#reasonClaims()
1548
-
1549
1333
  const reason = {}
1550
- // Sort keys and file lists: the JSON must be byte-reproducible regardless of record order, else
1551
- // #emitBundle's compare-and-skip sees spurious diffs.
1334
+ // Sorted: else #emitBundle's compare-and-skip sees spurious diffs.
1552
1335
  for (const who of [...claims.keys()].toSorted()) {
1553
- const files = [...claims.get(who)].filter((file) => inBundle.has(file))
1554
- if (files.length > 0) reason[who] = files.toSorted(sortPaths)
1336
+ const files = [...claims.get(who)].filter((file) => contents.has(file))
1337
+ if (files.length > 0) reason[who] = sortedByPath(files)
1555
1338
  }
1556
1339
  return Object.keys(reason).length > 1 ? reason : undefined
1557
1340
  }
1558
1341
 
1559
- // The formats a bundle declares: `this.formats`, minus a 'stat:*' the run's unified attestation
1560
- // upgraded to a real format (else the bundle fails its lockfile cross-check at load).
1342
+ // this.formats less a 'stat:*' the unified attestation upgraded, else the bundle fails its lockfile check at load.
1561
1343
  #formatsForBundle() {
1562
- if (this.#sidecars.size === 0) return this.formats
1563
1344
  const merged = this.#mergedFormats()
1564
- let out = null // copy-on-write: clone only when an entry must be dropped
1565
- for (const [file, format] of this.formats) {
1566
- if (isStatFormat(format) && merged.get(file) !== format) {
1567
- out ??= new Map(this.formats)
1568
- out.delete(file)
1569
- }
1570
- }
1571
- return out ?? this.formats
1572
- }
1573
-
1574
- #absorbRepo(half, bundle) {
1575
- this.#absorbedRepos.set(half, this.#absorbedRepos.has(half) ? mergeRepo(this.#absorbedRepos.get(half), bundle.repo) : bundle.repo)
1345
+ const upgraded = ([file, format]) => isStatFormat(format) && merged.get(file) !== format
1346
+ if (!this.formats.entries().some(upgraded)) return this.formats
1347
+ return new Map(this.formats.entries().filter((entry) => !upgraded(entry)))
1576
1348
  }
1577
1349
 
1578
1350
  get #detectedRepo() {
1579
1351
  return (this.#detectedRepoCache ??= { value: detectRepo(this.root, this.#host) }).value
1580
1352
  }
1581
1353
 
1582
- // `repo` to write for a half: detected, merged with that half's absorbed one when adding to it.
1583
1354
  #repoFor(half) {
1584
1355
  if (!this.#absorbedRepos.has(half)) return this.#detectedRepo
1585
1356
  const absorbed = this.#absorbedRepos.get(half)
1586
1357
  return absorbed && mergeRepo(absorbed, this.#detectedRepo)
1587
1358
  }
1588
1359
 
1589
- // One bundle artifact over `contents` (file -> stored content) with the `formats` it declares;
1590
- // the code side carries entries/imports, the resources half none.
1591
1360
  #bundle(half, contents, formats, { entries = this.entries, imports = this.imports } = {}) {
1592
1361
  const modules = this.#bundleModules(contents)
1593
1362
  return new Bundle({
@@ -1596,15 +1365,13 @@ export class State {
1596
1365
  modules,
1597
1366
  formats,
1598
1367
  imports,
1599
- executable: this.#bundleExecutable(modules, formats),
1600
- reason: this.#bundleReason(contents.keys()),
1368
+ executable: narrowExecutable(this.executable, { modules, formats, scope: this.config.scope }),
1369
+ reason: this.#bundleReason(contents),
1601
1370
  repo: this.#repoFor(half),
1602
1371
  })
1603
1372
  }
1604
1373
 
1605
1374
  get sourceBundle() {
1606
- // One bundle holds code + resources, with `formats` tagging which; the key sets must be
1607
- // disjoint, asserted so an overlap fails locally rather than silently.
1608
1375
  const contents = new Map(this.sources)
1609
1376
  for (const [file, content] of this.resources) {
1610
1377
  assert.ok(!contents.has(file), `state invariant: file ${file} in both sources and resources`)
@@ -1617,10 +1384,7 @@ export class State {
1617
1384
  return this.sourceBundle.serialize()
1618
1385
  }
1619
1386
 
1620
- // Split-bundle counterparts to sourceBundle/sourceData: the code half owns entries/imports + code
1621
- // formats (stat records too: they aren't resource formats, so the resources half's shape check
1622
- // would reject them), the resources half owns resource formats + bytes. Both declare per-dir
1623
- // module identity so each file verifies alone.
1387
+ // Split-bundle halves. Stat records go in the code half: the resources half's shape check would reject them.
1624
1388
  get codeBundle() {
1625
1389
  const codeFormats = new Map([...this.#formatsForBundle()].filter(([, format]) => !Bundle.isResourceFormat(format)))
1626
1390
  return this.#bundle('code', this.sources, codeFormats)
@@ -1628,60 +1392,45 @@ export class State {
1628
1392
 
1629
1393
  get resourcesBundle() {
1630
1394
  const resourceFormats = new Map([...this.formats].filter(([, format]) => Bundle.isResourceFormat(format)))
1631
- // No entries/imports (Bundle.parse waives the entries requirement when no code).
1395
+ // Bundle.parse waives the entries requirement with no code.
1632
1396
  return this.#bundle('resources', this.resources, resourceFormats, { entries: new Set(), imports: new Map() })
1633
1397
  }
1634
1398
 
1635
- // Record a native resolution verbatim (last-write-wins per parent+specifier);
1636
- // #backfillObservedResolutions decides at write() which to add.
1399
+ // Verbatim, last-write-wins: #backfillObservedResolutions decides which to add.
1637
1400
  observeResolution(parentURL, specifier, resolvedURL) {
1638
1401
  nestedMap(this.#observedResolutions, parentURL).set(specifier, resolvedURL)
1639
1402
  }
1640
1403
 
1641
- // Record that a require() executed `url` (from the Module._load shim). Out-of-root targets are
1642
- // skipped like #backfillObservedResolutions' -- the live hooks correctly ignore them too.
1643
1404
  observeExecution(url) {
1644
1405
  const file = this.#canonicalFileMaybe(url)
1645
1406
  if (file !== undefined) this.#executedFiles.add(file)
1646
1407
  }
1647
1408
 
1648
- // Whether the bundle carries source bytes for `url` (the hooks' bundle-bypass reconciliation).
1649
1409
  isBundledSource(url) {
1650
1410
  const file = this.#canonicalFileMaybe(url)
1651
1411
  return file !== undefined && this.sources.has(file)
1652
1412
  }
1653
1413
 
1654
- // Attest every executed-but-never-captured file from disk. A resolve-only edge stays byte-less
1655
- // by design (see #backfillBeforeWrite), but an EXECUTED file the load hook never saw is a hole:
1656
- // leaving it silent would ship an artifact that misses code the run provably evaluated, and a
1657
- // frozen replay would never verify it. addFile re-checks bytes against the lockfile/bundle in
1658
- // frozen modes, so tampering still fails the run -- at write time, but loudly.
1414
+ // A resolve-only edge stays byte-less by design, but an EXECUTED file the load hook never saw is a hole a
1415
+ // frozen replay would never verify. addFile re-checks bytes in frozen modes, so tampering fails loudly.
1659
1416
  #backfillExecutedFiles() {
1660
1417
  for (const file of this.#executedFiles) {
1661
- // "Captured this run", NOT this.hashes: a lockfile-seeded hash must still be VERIFIED
1662
- // against disk here when the run executed the file without the load hook observing it.
1663
1418
  if (this.#filesCapturedThisRun.has(file)) continue
1664
- // A require()d native addon never passes the load hook and is deliberately not captured
1665
- // (non-deterministic across installs, like the rest of NATIVE_ARTIFACT_EXTS).
1419
+ // A native addon is deliberately not captured: non-deterministic across installs.
1666
1420
  if (isNativeArtifact(file)) continue
1667
- const url = pathToFileURL(resolve(this.root, file)).toString()
1668
- if (!inScope(this, url)) continue
1669
- this.addFile(url, { reason: 'run' })
1421
+ const url = this.#urlOf(file)
1422
+ if (inScope(this, url)) this.addFile(url, { reason: 'run' })
1670
1423
  }
1671
1424
  }
1672
1425
 
1673
- // Backfill edges the live resolve hook never observed (require.resolve()/native CJS require()).
1674
- // The resolution is attested but its bytes are NOT seeded -- that would widen trust to everything
1675
- // that merely resolves. Skips out-of-scope targets and dedups across all condition buckets.
1426
+ // Attested, but the bytes are NOT seeded: that would widen trust to everything that merely resolves.
1676
1427
  #backfillObservedResolutions() {
1677
1428
  for (const [parentURL, specs] of this.#observedResolutions) {
1678
1429
  const parent = this.#canonicalFileMaybe(parentURL)
1679
1430
  if (parent === undefined) continue
1680
1431
  for (const [specifier, resolvedURL] of specs) {
1681
1432
  const file = this.#canonicalFileMaybe(resolvedURL)
1682
- if (file === undefined) continue
1683
- const bundled = this.sources.has(file) || this.resources.has(file)
1684
- if (!bundled && !inScope(this, resolvedURL)) continue
1433
+ if (file === undefined || (!this.#bundled(file) && !inScope(this, resolvedURL))) continue
1685
1434
  const spec = this.#canonicalSpecifier(parentURL, specifier)
1686
1435
  const recorded = this.imports.values().some((byParent) => byParent.get(parent)?.get(spec) !== undefined)
1687
1436
  if (!recorded) this.addImport(parentURL, specifier, resolvedURL, { conditions: this.#requireConditions ?? '*' })
@@ -1689,19 +1438,13 @@ export class State {
1689
1438
  }
1690
1439
  }
1691
1440
 
1692
- // Add every bundled module's `<dir>/package.json` (config.packageJSON) even if the run never
1693
- // reached it, so a load/prune can read each dependency's manifest, and the nearer one a bundled
1694
- // file of it has (scopeManifestOf: a `{"type":"module"}` marker such as `esm/package.json`), which
1695
- // Node and Metro's resolver read for the file. The skip keys on bundle membership, NOT this.hashes
1696
- // (which also holds lockfile-absorbed entries and is shared by reference across sidecars).
1441
+ // Every bundled module's package.json, reached or not, for a load/prune to read, and a bundled file's nearer
1442
+ // one (an `esm/package.json` marker). Keyed on bundle membership, NOT this.hashes (lockfile-seeded, shared).
1697
1443
  includePackageJson() {
1698
- // Gather first: addFile mutates this.modules, so don't addFile while iterating it.
1444
+ // Gathered first: addFile mutates this.modules.
1699
1445
  const realRoot = this.#host.realpath(this.root)
1700
1446
  const claims = this.#reasonClaims()
1701
- const bundled = (rel) => this.sources.has(rel) || this.resources.has(rel)
1702
- const found = new Map()
1703
- const exists = (rel) => found.get(rel) ?? found.set(rel, packageJSONStat(this.#host, resolve(this.root, rel))?.isFile() ?? false).get(rel)
1704
- // A manifest's bytes and parsed fields, or null where it's gone or malformed (skipped).
1447
+ const exists = memoized((rel) => packageJSONStat(this.#host, resolve(this.root, rel))?.isFile() ?? false)
1705
1448
  const read = (rel) => {
1706
1449
  const buf = readModuleManifest({ baseDir: this.root, realBase: realRoot, rel, host: this.#host })
1707
1450
  if (!buf) return null
@@ -1715,76 +1458,66 @@ export class State {
1715
1458
  const toAdd = []
1716
1459
  for (const [dir, module] of this.modules) {
1717
1460
  const own = moduleFileKey(dir, 'package.json')
1718
- // Only carry the manifests of a bucket whose on-disk identity still matches: under
1719
- // bundle=add a dep may have drifted, and addFile -> #locateModule would crash on it.
1461
+ // Under bundle=add a dep may have drifted, and #locateModule would crash on it.
1720
1462
  const manifest = read(own)
1721
1463
  if (manifest === null || manifest.pkg.name !== module.name || manifest.pkg.version !== module.version) continue
1722
- // Each manifest -> the files it is the package.json of: the bucket's own, of every file; a
1723
- // nearer one, of the bundled files below it.
1724
1464
  const files = Object.keys(module.files).map((f) => moduleFileKey(dir, f))
1725
1465
  const scopes = new Map([[own, files]])
1726
1466
  for (const file of files) {
1727
- const nearest = bundled(file) ? scopeManifestOf(file, dir, exists) : undefined
1728
- if (nearest !== undefined) (scopes.get(nearest) ?? scopes.set(nearest, []).get(nearest)).push(file)
1467
+ const nearest = this.#bundled(file) ? scopeManifestOf(file, dir, exists) : undefined
1468
+ if (nearest !== undefined) upsert(scopes, nearest, () => []).push(file)
1729
1469
  }
1730
1470
  for (const [rel, of] of scopes) {
1731
- if (bundled(rel)) continue // already in this bundle
1471
+ if (this.#bundled(rel)) continue // already in this bundle
1732
1472
  const buf = rel === own ? manifest.buf : read(rel)?.buf
1733
1473
  if (buf === undefined) continue
1734
- // The manifest rides along BECAUSE some consumer bundled its files, so it inherits every
1735
- // consumer that claims one (#reasonClaims: not run for a file it merely fs-read for a
1736
- // plugin) -- nothing observed the manifest itself, but an unattributed file can't be
1737
- // sorted by provenance downstream. Derived BEFORE the adds below, so one fold's manifest
1738
- // can't count as another bucket's consumer evidence.
1474
+ // It rides along BECAUSE a consumer bundled its files, so it inherits each. Derived BEFORE the adds
1475
+ // below, so one fold's manifest can't count as another bucket's consumer evidence.
1739
1476
  const consumers = [...claims].filter(([, claimed]) => of.some((file) => claimed.has(file))).map(([who]) => who)
1740
1477
  toAdd.push({ rel, buf, consumers })
1741
1478
  }
1742
1479
  }
1743
1480
  for (const { rel, buf, consumers } of toAdd) {
1744
- this.addFile(pathToFileURL(resolve(this.root, rel)).toString(), { source: buf, reason: null })
1481
+ this.addFile(this.#urlOf(rel), { source: buf, reason: null })
1745
1482
  for (const who of consumers) this.#recordReason(who, rel)
1746
1483
  }
1747
1484
  }
1748
1485
 
1749
- write() {
1750
- // BEFORE the stasis-core BFS: a resolve-only edge to a stasis-core submodule is added only here,
1751
- // and the BFS must see it to seed the file's bytes, else the bundle ships a dangling edge.
1486
+ // Native resolutions BEFORE the stasis-core BFS, which must see an edge they add to seed its bytes.
1487
+ #backfill() {
1752
1488
  this.#backfillObservedResolutions()
1753
1489
  this.#backfillExecutedFiles()
1754
1490
  this.#backfillBeforeWrite()
1755
- // Only when writing (a frozen run verifies what it reached, not `extra`); a sidecar loads no config.
1756
- if (this.config.writeLockfile || this.config.writeBundle) this.includeExtra()
1491
+ }
1492
+
1493
+ write() {
1494
+ this.#backfill()
1495
+ const writing = this.config.writeLockfile || this.config.writeBundle
1496
+ // A frozen run verifies what it reached, not `extra`.
1497
+ if (writing) this.includeExtra()
1757
1498
  // AFTER the backfills and `extra`, so buckets they create are covered too.
1758
1499
  if (this.config.writeBundle && this.config.packageJSON) this.includePackageJson()
1500
+ if (writing) this.#recordInstalled()
1759
1501
 
1760
- // Every artifact is serialized before the first is written, so one that can't be (no bundle may carry
1761
- // the lockfile) leaves the others as they were. Sidecars never write the lockfile (parent owns it).
1502
+ // All serialized before the first is written, so one that can't be leaves the others as they were.
1762
1503
  const lockText = this.config.writeLockfile && !this.#parent ? this.lockData : null
1763
- const sourcesPath = this.config.bundleFile || join(this.root, FILE_CODE)
1764
- // Each bundle written, [path, text]: the code bundle, with the resources too unless they are split
1765
- // into resourcesBundleFile. hasContent gates each so an empty one is dropped under bundle=replace.
1504
+ const sourcesPath = this.#bundlePath
1766
1505
  const bundles = !this.config.writeBundle ? [] : this.config.resourcesBundleFile
1767
1506
  ? [[sourcesPath, this.#bundleText(this.sources.size > 0, () => this.codeBundle.serialize())],
1768
1507
  [this.config.resourcesBundleFile, this.#bundleText(this.resources.size > 0, () => this.resourcesBundle.serialize())]]
1769
1508
  : [[sourcesPath, this.#bundleText(this.sources.size > 0 || this.resources.size > 0, () => this.sourceData)]]
1770
1509
 
1771
1510
  if (lockText !== null && lockText !== this.#lastLockData) {
1772
- const lockPath = this.config.lockFile || join(this.root, FILE_LOCK)
1773
- mkdirSync(dirname(lockPath), { recursive: true })
1774
- writeFileSync(lockPath, lockText)
1511
+ mkdirSync(dirname(this.#lockPath), { recursive: true })
1512
+ writeFileSync(this.#lockPath, lockText)
1775
1513
  this.#lastLockData = lockText
1776
1514
  }
1777
1515
  for (const [path, text] of bundles) this.#emitBundle(path, text)
1778
1516
  }
1779
1517
 
1780
- // Snapshot of what THIS process observed, for a forked child to hand back to the root. Content is
1781
- // omitted -- the root re-reads bytes from disk.
1518
+ // For a forked child to hand back; the root re-reads bytes. Entries are dropped: a child's is its fork target.
1782
1519
  shardSnapshot() {
1783
- this.#backfillObservedResolutions()
1784
- this.#backfillExecutedFiles()
1785
- this.#backfillBeforeWrite()
1786
- // Only what THIS process observed, never the seeded baseline (shard-size bloat). Entries are
1787
- // dropped: a child's "entry" is its fork target, not a root's.
1520
+ this.#backfill()
1788
1521
  const files = []
1789
1522
  const formats = new Map()
1790
1523
  for (const file of this.#observed) {
@@ -1792,7 +1525,7 @@ export class State {
1792
1525
  if (format !== undefined) formats.set(file, format)
1793
1526
  if (this.hashes.has(file)) files.push(file)
1794
1527
  }
1795
- // Only edges whose PARENT was observed, dropping the bundle-seeded ones the root already attests.
1528
+ // Only edges whose parent was observed: the root attests the bundle-seeded rest already.
1796
1529
  const imports = new Map()
1797
1530
  for (const [conditions, byParent] of this.imports) {
1798
1531
  for (const parent of this.#observed) {
@@ -1803,101 +1536,74 @@ export class State {
1803
1536
  return serializeShard({ scope: this.config.scope, files, formats, imports })
1804
1537
  }
1805
1538
 
1806
- // Merge a child's shardSnapshot() as if this process read those files itself: replay each file
1807
- // (re-reading bytes/listings from disk) and each edge. Best-effort -- a gone/out-of-scope record is
1808
- // skipped rather than aborting the merge.
1539
+ // As if this process read those files itself, re-read from disk. Best-effort: a gone/out-of-scope record is skipped.
1809
1540
  mergeShard(shardText) {
1810
1541
  const shard = parseShard(shardText)
1811
- // Defense-in-depth: cross-scope is signature-rejected upstream, but a mismatch here means a
1812
- // malformed/foreign shard -- refuse.
1813
1542
  assert.equal(shard.scope, this.config.scope, `shard scope "${shard.scope}" != root scope "${this.config.scope}"`)
1814
- // An in-root shard KEY may resolve (through a symlink) OUTSIDE the root on THIS disk, so every
1815
- // replay re-checks real-path containment (realContained), the boundary fs.js enforces on live --fs reads.
1816
- // Files first, then edges. Merged files are NEVER marked entries -- a child's "entry" is its
1817
- // fork-target main, not a root entry.
1543
+ // An in-root KEY may resolve through a symlink OUTSIDE the root on THIS disk: real containment is re-checked.
1818
1544
  for (const file of shard.files) {
1819
1545
  const absolute = resolve(this.root, file)
1820
- // Skip a key whose on-disk path escapes the root through a symlink.
1821
1546
  if (!realContained(this.root, absolute)) continue
1822
- // ABOVE the already-attested fast path below: a child that re-read a file and found no bit
1823
- // refutes it by OMISSION, so every file the shard RECORDS needs an observation. Re-derived
1824
- // from THIS process's disk, so the shard cannot inject a forged bit.
1547
+ // ABOVE the fast path: a child refutes a bit by OMISSION. Re-derived from THIS disk, never forged.
1825
1548
  this.#recordExecutable(file, absolute)
1826
- // Skip a file the root already attests (the byte re-read dominates merge cost), EXCEPT to
1827
- // carry a concrete format the root LACKS, else a later frozen run rejects the unattested format.
1549
+ // Skip one the root attests (the re-read dominates merge cost), unless carrying a format it LACKS.
1828
1550
  const format = shard.formats.get(file)
1829
1551
  if (this.hashes.has(file) && (format === undefined || (this.formats.get(file) ?? this.#lockFormats?.get(file)) !== undefined)) continue
1830
1552
  const url = pathToFileURL(absolute).toString()
1831
1553
  try {
1832
1554
  if (format === 'directory') {
1833
- // Range-check FIRST so a child-forged `..` key can't make us list it.
1555
+ // Range-checked FIRST, so a child-forged `..` key can't make us list it.
1834
1556
  if (!isPathWithin(this.root, absolute)) continue
1835
- this.addFsDir(url, readdirSync(absolute))
1836
- } else if (format === 'resource' || format === 'resource:base64') {
1837
- // Let addFile re-derive the exact resource tag from the re-read bytes rather than trusting
1838
- // the shard's and risking a mismatch.
1557
+ this.addFsDir(url, this.#host.readdir(absolute).map((entry) => entry.name))
1558
+ } else if (Bundle.isResourceFormat(format)) {
1559
+ // The resource tag is re-derived from the re-read bytes.
1839
1560
  this.addFile(url, { resource: true })
1840
1561
  } else if (format === undefined) {
1841
- // No format recorded (an fs-READ .js/.ts): inferFormat:false so we don't bake in the
1842
- // commonjs default and collide with the `module` the loader records if it's later imported.
1562
+ // An fs-READ .js/.ts: a commonjs default would collide with the `module` a later import records.
1843
1563
  this.addFile(url, { inferFormat: false })
1844
1564
  } else {
1845
- // Carry the child's format, else a child-only ESM .js under a no-`type` package re-defaults
1846
- // to commonjs and a frozen run rejects the mismatch.
1565
+ // Else a child-only ESM .js under a no-`type` package re-defaults to commonjs.
1847
1566
  this.addFile(url, { format })
1848
1567
  }
1849
- } catch {
1850
- // Gone/unreadable, or addFile-rejected (out of scope): skip, like the edge replay below.
1851
- }
1568
+ } catch { /* gone/unreadable, or out of scope */ }
1852
1569
  }
1853
- // Stat records live ONLY in the shard's formats map, so replay each here, re-deriving the kind
1854
- // from DISK (a shard can't inject a forged kind). Range-check BEFORE touching disk.
1570
+ // Stat records live ONLY in the shard's formats; the kind is re-derived from DISK, range-checked first.
1855
1571
  for (const [file, format] of shard.formats) {
1856
1572
  if (!isStatFormat(format)) continue
1857
1573
  const absolute = resolve(this.root, file)
1858
1574
  if (!isPathWithin(this.root, absolute)) continue
1859
1575
  try {
1860
- // statSync, NOT lstatSync: the child's capture followed symlinks, so this reproduces the kind
1861
- // a legitimate shard carries where lstatSync would report the link and drop it.
1862
- const stats = statSync(absolute)
1863
- const kind = stats.isDirectory() ? 'directory' : stats.isFile() ? 'file' : null
1864
- // statSync follows links: a symlink pointing OUT must not attest an external kind under an
1865
- // in-root key.
1576
+ // A stat, NOT an lstat, as the child's capture; a symlink pointing OUT must not attest an external kind.
1577
+ const stats = this.#host.stat(absolute)
1578
+ const kind = stats?.isDirectory() ? 'directory' : stats?.isFile() ? 'file' : null
1866
1579
  if (kind === null || !realContained(this.root, absolute)) continue
1867
1580
  this.addFsStat(pathToFileURL(absolute).toString(), kind)
1868
- } catch {
1869
- // Gone/dangling, or a kind conflict with the root's capture: skip.
1870
- }
1581
+ } catch { /* gone/dangling, or a kind conflict with the root's capture */ }
1871
1582
  }
1872
1583
  for (const [conditions, byParent] of shard.imports) {
1873
- // Reverse #conditionsKey: '*', else a ', '-joined list with an optional ' (with: {json})'
1874
- // suffix -- parse it so attributed edges merge under the child's key.
1875
- let condStr = conditions
1876
- let importAttributes
1584
+ // Reverse #conditionsKey, so attributed edges merge under the child's key.
1877
1585
  const withAt = conditions.indexOf(' (with: ')
1586
+ const condStr = withAt === -1 ? conditions : conditions.slice(0, withAt)
1587
+ let importAttributes
1878
1588
  if (withAt !== -1) {
1879
- condStr = conditions.slice(0, withAt)
1880
1589
  try { importAttributes = JSON.parse(conditions.slice(withAt + ' (with: '.length, -1)) } catch { /* leave undefined */ }
1881
1590
  }
1882
1591
  const cond = condStr === '*' ? '*' : condStr.split(', ')
1883
1592
  for (const [parent, specs] of byParent) {
1884
- const parentURL = pathToFileURL(resolve(this.root, parent)).toString()
1593
+ const parentURL = this.#urlOf(parent)
1885
1594
  for (const [specifier, file] of specs) {
1886
- const url = pathToFileURL(resolve(this.root, file)).toString()
1887
- // Same best-effort stance as addFile above.
1595
+ const url = this.#urlOf(file)
1888
1596
  try { this.addImport(parentURL, specifier, url, { conditions: cond, importAttributes }) } catch { /* skipped */ }
1889
1597
  }
1890
1598
  }
1891
1599
  }
1892
1600
  }
1893
1601
 
1894
- // What a bundle write leaves at its path: the serialized bundle, or null to drop an empty one under bundle=replace.
1895
1602
  #bundleText(hasContent, serialize) {
1896
1603
  return !hasContent && this.config.replaceBundle ? null : serialize()
1897
1604
  }
1898
1605
 
1899
- // Emit one bundle artifact. Null (empty under bundle=replace) DELETES the file (a replace run is
1900
- // authoritative); text unchanged since this State last wrote it skips brotli + writeFileSync.
1606
+ // Null DELETES the file: a replace run is authoritative.
1901
1607
  #emitBundle(path, text) {
1902
1608
  if (text === null) {
1903
1609
  rmSync(path, { force: true })
@@ -1912,18 +1618,13 @@ export class State {
1912
1618
  return this.#parent
1913
1619
  }
1914
1620
 
1915
- // Capture stasis-core's own files + internal edges the live hooks missed: for a preload-cached
1916
- // module the resolve hook records the edge but the load hook never fires. The BFS below is scoped
1917
- // to the stasis-core src shape.
1621
+ // For a preload-cached stasis-core module the resolve hook records the edge but the load hook never fires.
1918
1622
  #backfillBeforeWrite() {
1919
- const missing = this.#collectMissingImportedFiles()
1920
- if (missing.size === 0) return
1921
-
1922
1623
  const isStasisCoreFile = (file) => STASIS_CORE_FILE_RE.test(`/${file}`)
1923
-
1924
- // Only stasis-core files are reconstructed; everything else stays a resolution attested without
1925
- // bytes (the load-time getFile gate, not this pass, enforces the trust boundary).
1926
- const stasisCoreMissing = [...missing].filter((file) => isStasisCoreFile(file))
1624
+ // Anything else stays a resolution attested without bytes: the load-time getFile gate enforces trust.
1625
+ const imported = new Set(this.imports.values().flatMap((byParent) => byParent.values()).flatMap((specifiers) => specifiers.values()))
1626
+ const stasisCoreMissing = [...imported].filter((file) => typeof file === 'string' && isStasisCoreFile(file) &&
1627
+ !this.hashes.has(file) && !this.#bundled(file) && inScope(this, this.#urlOf(file)))
1927
1628
  if (stasisCoreMissing.length === 0) return
1928
1629
 
1929
1630
  const preloadRel = this.#preloadRoot ? relative(this.root, this.#preloadRoot) : null
@@ -1933,35 +1634,21 @@ export class State {
1933
1634
  `investigate rather than silently patch: ${stasisCoreMissing.slice(0, 3).join(', ')}`)
1934
1635
 
1935
1636
  const PRELOAD_CONDITIONS = ['node', 'import', 'module-sync', 'node-addons']
1936
- const stasisAddFile = (file) => {
1937
- assert.ok(isStasisCoreFile(file),
1938
- `state.write() backfill refused: '${file}' is not a stasis-core source file ` +
1939
- `(expected '.../@exodus/stasis-core/src/<name>.js')`)
1940
- // reason 'run': Node evaluated these before registerHooks could observe it (that blindness is
1941
- // why this backfill exists), so they're run-loaded modules like any other. A consumer-less
1942
- // record can't be sorted by provenance downstream (app code vs toolchain).
1943
- this.addFile(pathToFileURL(resolve(this.root, file)).toString(), { reason: 'run' })
1944
- }
1945
-
1946
- // Seeded from the missing files + every stasis-core file already in sources, to walk their
1947
- // transitive imports too.
1948
- const queue = [...stasisCoreMissing, ...[...this.sources.keys()].filter(isStasisCoreFile)]
1949
- const processed = new Set()
1950
- while (queue.length > 0) {
1951
- const file = queue.shift()
1952
- if (processed.has(file)) continue
1953
- processed.add(file)
1954
- if (!this.sources.has(file) && !this.resources.has(file)) stasisAddFile(file)
1637
+ // Every stasis-core file already in sources too, to walk their transitive imports.
1638
+ const queue = new Set([...stasisCoreMissing, ...[...this.sources.keys()].filter(isStasisCoreFile)])
1639
+ for (const file of queue) {
1640
+ // 'run', as Node evaluated these before registerHooks could see: a consumer-less record can't be sorted.
1641
+ if (!this.#bundled(file)) this.addFile(this.#urlOf(file), { reason: 'run' })
1955
1642
  const source = this.sources.get(file)
1956
1643
  if (typeof source !== 'string') continue
1957
- const baseURL = pathToFileURL(resolve(this.root, file)).toString()
1644
+ const baseURL = this.#urlOf(file)
1958
1645
  const recordEdge = (specifier) => {
1959
1646
  const targetURL = new URL(specifier, baseURL).toString()
1960
1647
  const targetAbsolute = fileURLToPath(targetURL)
1961
1648
  let targetFile
1962
1649
  try { targetFile = this.relative(targetAbsolute) } catch { return }
1963
- if (!isStasisCoreFile(targetFile) || !existsSync(targetAbsolute)) return
1964
- queue.push(targetFile)
1650
+ if (!isStasisCoreFile(targetFile) || !this.#exists(targetAbsolute)) return
1651
+ queue.add(targetFile)
1965
1652
  this.addImport(baseURL, specifier, targetURL, { conditions: PRELOAD_CONDITIONS })
1966
1653
  }
1967
1654
  // stasis-core's ESM import shape, tight enough to ignore JSDoc/template-literal false positives.
@@ -1970,26 +1657,7 @@ export class State {
1970
1657
  for (const m of source.matchAll(/require\('(\.\.\/package\.json)'\)/g)) recordEdge(m[1])
1971
1658
  }
1972
1659
  }
1973
-
1974
- // Files referenced by this.imports but not captured, restricted to the in-scope set (out-of-scope
1975
- // files are excluded -- the live hooks correctly skip them).
1976
- #collectMissingImportedFiles() {
1977
- const missing = new Set()
1978
- for (const [, byParent] of this.imports) {
1979
- for (const [, specifiers] of byParent) {
1980
- for (const [, file] of specifiers) {
1981
- if (typeof file !== 'string' || missing.has(file)) continue
1982
- if (this.hashes.has(file) || this.sources.has(file) || this.resources.has(file)) continue
1983
- const url = pathToFileURL(resolve(this.root, file)).toString()
1984
- if (!inScope(this, url)) continue
1985
- missing.add(file)
1986
- }
1987
- }
1988
- }
1989
- return missing
1990
- }
1991
1660
  }
1992
1661
 
1993
- // Backfillable stasis-core paths. Anchored against `/${file}` so a project-root file merely ending
1994
- // in `@exodus/stasis-core/...` isn't matched.
1662
+ // Anchored against `/${file}`, so a project-root file merely ending in `@exodus/stasis-core/...` isn't matched.
1995
1663
  const STASIS_CORE_FILE_RE = /\/@exodus\/stasis-core\/(?:src\/[a-zA-Z-]+\.c?js|package\.json)$/