@orkestrel/scaffold 0.0.48 → 0.0.49

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.
@@ -1,8 +1,9 @@
1
- import { andOf, arrayOf, attempt, boundsOf, holds, isArray, isBoolean, isError, isFunction, isInteger, isRecord, isString, parseJSON, parseJSONAs, parseStringField, recordOf, stringOf } from "@orkestrel/contract";
2
- import { CATALOG_AGENT_PATH, CONTROL_CHARACTER_PATTERN, EXECUTABLE_PATHS, HOST_PATHS, MAX_ARTIFACT_BYTES, MAX_COLLECTION_ITEMS, MAX_DEPENDENCY_NAME_LENGTH, MAX_MANIFEST_BYTES, MAX_PATH_LENGTH, MAX_RANGE_LENGTH, MAX_REGISTRY_BYTES, MAX_TOTAL_ARTIFACT_BYTES, MAX_TOTAL_REGISTRY_BYTES, ScaffoldError, WORKSPACE_OWNED_PATHS, bytesToHex, catalogToLayers, cloneValue, compareVersions, computeBytes, contentToHex, extractRangeMajor, extractVersion, inferGroup, isAudit, isCatalogEntry, isCollection, isDependency, isDependencyName, isMirror, isPath, isPlan, isSnapshot, matchesDriftReachability, matchesRange, nameToGuide, planToFindings, replaceManifestRanges } from "../core/index.js";
1
+ import { andOf, arrayOf, attempt, boundsOf, holds, isArray, isBoolean, isError, isFunction, isInteger, isRecord, isString, parseJSON, parseJSONAs, parseStringField, recordOf, stringOf, unionOf } from "@orkestrel/contract";
2
+ import { CATALOG_AGENT_PATH, CONTROL_CHARACTER_PATTERN, EXECUTABLE_PATHS, HOST_INVENTORY_PATH, HOST_PATHS, MAX_ARTIFACT_BYTES, MAX_COLLECTION_ITEMS, MAX_DEPENDENCY_NAME_LENGTH, MAX_MANIFEST_BYTES, MAX_PATH_LENGTH, MAX_RANGE_LENGTH, MAX_REGISTRY_BYTES, MAX_TOTAL_ARTIFACT_BYTES, MAX_TOTAL_REGISTRY_BYTES, ScaffoldError, WORKSPACE_OWNED_PATHS, bytesToHex, catalogToLayers, cloneValue, compareVersions, computeBytes, contentToHex, extractRangeMajor, extractVersion, inferGroup, isAudit, isCatalogEntry, isCollection, isDeferredPath, isDependency, isDependencyName, isHex, isMirror, isPath, isPlan, isSnapshot, matchesDriftReachability, matchesRange, nameToGuide, planToFindings, replaceManifestRanges } from "../core/index.js";
3
3
  import { createHash, randomUUID } from "node:crypto";
4
- import { chmodSync, closeSync, constants, copyFileSync, fstatSync, linkSync, lstatSync, mkdirSync, openSync, opendirSync, readSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, writeFileSync } from "node:fs";
5
- import { basename, dirname, join, parse, relative, resolve, sep } from "node:path";
4
+ import { chmodSync, closeSync, constants, copyFileSync, fstatSync, linkSync, lstatSync, mkdirSync, mkdtempSync, openSync, opendirSync, readSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, writeFileSync } from "node:fs";
5
+ import { tmpdir } from "node:os";
6
+ import { basename, dirname, extname, join, parse, relative, resolve, sep } from "node:path";
6
7
  import { fileURLToPath } from "node:url";
7
8
  import { Emitter } from "@orkestrel/emitter";
8
9
  //#region src/server/constants.ts
@@ -43,13 +44,13 @@ var RESERVED_SEGMENT_PATTERN = /^(?:con|prn|aux|nul|com[1-9]|lpt[1-9]|conin\$|co
43
44
  */
44
45
  var DIGEST_PATTERN = /^[0-9a-f]{64}$/;
45
46
  /**
46
- * The Git branch syntax the guide endpoint accepts.
47
+ * The Git branch syntax the repository endpoint accepts.
47
48
  *
48
49
  * @remarks
49
50
  * A branch is caller-supplied and reaches a URL path, so it is closed to
50
51
  * alphanumerics, dot, underscore, hyphen, and the separator, must open with an
51
52
  * alphanumeric, and may carry no `..` anywhere. That last refusal is what stops
52
- * a branch from walking out of the guide directory it addresses.
53
+ * a branch from walking out of the repository path it addresses.
53
54
  */
54
55
  var BRANCH_PATTERN = /^(?!.*\.\.)[A-Za-z0-9][A-Za-z0-9._/-]*$/;
55
56
  /**
@@ -83,7 +84,7 @@ var MAX_PATH_DEPTH = 64;
83
84
  var MAX_INVENTORY_PATHS = 1e5;
84
85
  /** Maximum characters one caller-supplied upstream endpoint may carry. */
85
86
  var MAX_ENDPOINT_LENGTH = 2048;
86
- /** Maximum characters one guide branch may carry. */
87
+ /** Maximum characters one repository branch may carry. */
87
88
  var MAX_BRANCH_LENGTH = 255;
88
89
  /**
89
90
  * Maximum simultaneous upstream requests.
@@ -225,10 +226,10 @@ var isEndpoint = stringOf({
225
226
  max: MAX_ENDPOINT_LENGTH
226
227
  });
227
228
  /**
228
- * Narrow a value to a Git branch the guide endpoint accepts.
229
+ * Narrow a value to a Git branch the repository endpoint accepts.
229
230
  *
230
231
  * @remarks
231
- * A branch reaches the guide URL's path, so the syntax is closed rather than
232
+ * A branch reaches the repository URL's path, so the syntax is closed rather than
232
233
  * merely bounded and no `..` is admitted anywhere in it.
233
234
  *
234
235
  * @example
@@ -270,6 +271,24 @@ var isTimeout = andOf(isInteger, boundsOf(1, MAX_UPSTREAM_TIMEOUT));
270
271
  * ```
271
272
  */
272
273
  var isDependencyNames = andOf(isCollection, arrayOf(isDependencyName));
274
+ /**
275
+ * Narrow a value to a bounded list of target-relative paths.
276
+ *
277
+ * @remarks
278
+ * Composed from the core collection and path guards rather than restated, so
279
+ * the containment law that keeps a caller-supplied path inside its target has
280
+ * exactly one home. It bounds what a caller may hand a public method, which is
281
+ * why it is not {@link isInventory}: that one bounds what a checkout may hold.
282
+ *
283
+ * @example
284
+ * ```ts
285
+ * import { isPaths } from '@orkestrel/scaffold/server'
286
+ *
287
+ * isPaths(['AGENTS.md']) // true
288
+ * isPaths(['../secrets']) // false
289
+ * ```
290
+ */
291
+ var isPaths = andOf(isCollection, arrayOf(isPath));
273
292
  /** Narrow a value to a bounded list of declared runtime dependencies. */
274
293
  var isDependencies = andOf(isCollection, arrayOf(isDependency));
275
294
  /** Narrow a value to a bounded list of fetched guide mirrors. */
@@ -289,13 +308,19 @@ var isCatalogEntries = andOf(isCollection, arrayOf(isCatalogEntry));
289
308
  * ```ts
290
309
  * import { isManifestEntry } from '@orkestrel/scaffold/server'
291
310
  *
292
- * isManifestEntry({ storage: 'AGENTS.md', destination: 'AGENTS.md', executable: false }) // true
311
+ * isManifestEntry({
312
+ * storage: 'AGENTS.md',
313
+ * destination: 'AGENTS.md',
314
+ * executable: false,
315
+ * digest: 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
316
+ * }) // true
293
317
  * ```
294
318
  */
295
319
  var isManifestEntry = recordOf({
296
320
  storage: isPath,
297
321
  destination: isPath,
298
- executable: isBoolean
322
+ executable: isBoolean,
323
+ digest: isDigest
299
324
  });
300
325
  /**
301
326
  * Narrow a value to one {@link HostManifest}.
@@ -311,7 +336,33 @@ var isHostManifest = recordOf({
311
336
  digest: isDigest
312
337
  });
313
338
  /**
314
- * Narrow a value to a {@link Repository}.
339
+ * Narrow a value to one {@link Host}.
340
+ *
341
+ * @remarks
342
+ * A whole vendored host handed in as a value is as untrusted as one read from a
343
+ * directory a caller named, so both halves are guarded: the manifest by the same
344
+ * membership law a read root is held to, and the bytes by the core snapshot law,
345
+ * which bounds the fill and reads every key as a path and every value as exact
346
+ * lowercase hexadecimal. Whether those halves agree with each other is the
347
+ * reader's question rather than this one's, because a guard has only `false` to
348
+ * say and a mismatch has a path to name.
349
+ *
350
+ * @example
351
+ * ```ts
352
+ * import { isHost } from '@orkestrel/scaffold/server'
353
+ *
354
+ * const digest = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
355
+ *
356
+ * isHost({ manifest: { entries: [], roots: [], digest }, bytes: {} }) // true
357
+ * isHost({ manifest: { entries: [], roots: [], digest } }) // false
358
+ * ```
359
+ */
360
+ var isHost = recordOf({
361
+ manifest: isHostManifest,
362
+ bytes: isSnapshot
363
+ });
364
+ /**
365
+ * Narrow a value to a {@link Worktree}.
315
366
  *
316
367
  * @remarks
317
368
  * Both path lists are target-relative, so both are measured by the core path
@@ -321,13 +372,13 @@ var isHostManifest = recordOf({
321
372
  *
322
373
  * @example
323
374
  * ```ts
324
- * import { isRepository } from '@orkestrel/scaffold/server'
375
+ * import { isWorktree } from '@orkestrel/scaffold/server'
325
376
  *
326
- * isRepository({ tracked: ['AGENTS.md'], dirty: [] }) // true
327
- * isRepository({ tracked: ['../secrets'], dirty: [] }) // false
377
+ * isWorktree({ tracked: ['AGENTS.md'], dirty: [] }) // true
378
+ * isWorktree({ tracked: ['../secrets'], dirty: [] }) // false
328
379
  * ```
329
380
  */
330
- var isRepository = recordOf({
381
+ var isWorktree = recordOf({
331
382
  tracked: andOf(isInventory, arrayOf(isPath)),
332
383
  dirty: andOf(isInventory, arrayOf(isPath))
333
384
  });
@@ -349,6 +400,12 @@ var isMaterializerHooks = recordOf({
349
400
  /**
350
401
  * Narrow a value to {@link MaterializerOptions}.
351
402
  *
403
+ * @remarks
404
+ * `host` admits both representations of one vendored root: a directory path and
405
+ * a whole {@link Host} value. They share a key because they are one setting
406
+ * stated two ways rather than two settings, so nothing downstream has to
407
+ * reconcile a pair that could disagree.
408
+ *
352
409
  * @example
353
410
  * ```ts
354
411
  * import { isMaterializerOptions } from '@orkestrel/scaffold/server'
@@ -358,7 +415,7 @@ var isMaterializerHooks = recordOf({
358
415
  * ```
359
416
  */
360
417
  var isMaterializerOptions = recordOf({
361
- host: isFilesystemPath,
418
+ host: unionOf(isFilesystemPath, isHost),
362
419
  on: isMaterializerHooks,
363
420
  error: isFunction
364
421
  }, true);
@@ -372,6 +429,7 @@ var isMaterializerOptions = recordOf({
372
429
  var isUpstreamHooks = recordOf({
373
430
  release: isFunction,
374
431
  mirror: isFunction,
432
+ file: isFunction,
375
433
  error: isFunction,
376
434
  destroy: isFunction
377
435
  }, true);
@@ -391,12 +449,12 @@ var isUpstreamHooks = recordOf({
391
449
  * ```ts
392
450
  * import { isUpstreamOptions } from '@orkestrel/scaffold/server'
393
451
  *
394
- * isUpstreamOptions({ guides: { branch: 'main' }, concurrency: 4 }) // true
452
+ * isUpstreamOptions({ repository: { branch: 'main' }, concurrency: 4 }) // true
395
453
  * isUpstreamOptions({ concurrency: 0 }) // false
396
454
  * ```
397
455
  */
398
456
  var isUpstreamOptions = recordOf({
399
- guides: recordOf({
457
+ repository: recordOf({
400
458
  base: isEndpoint,
401
459
  branch: isBranch,
402
460
  timeout: isTimeout
@@ -473,7 +531,7 @@ function matchesGitPath(path) {
473
531
  * directories. It is the inversion the contract asks for: the candidate set
474
532
  * is re-derived from the plan and narrowed by what git tracks, and the audit
475
533
  * must agree with that derivation rather than supply the set itself.
476
- * Repository metadata is protected because losing history is not a repair,
534
+ * Git metadata is protected because losing history is not a repair,
477
535
  * and a target's own `src` and `app` trees are protected because a
478
536
  * workspace's source is the one thing scaffold never plans and never owns. A
479
537
  * plan the compiler emits never maps a protected root, so this guard exists
@@ -504,7 +562,7 @@ function matchesProtectedPath(path) {
504
562
  * The vendoring deny-list. A host root is staged from a real checkout, so the
505
563
  * refusal is stated over the path rather than over the file's content: a
506
564
  * credential is recognizable by where it sits and what it is called long before
507
- * anything reads it. Repository metadata is included through
565
+ * anything reads it. Git metadata is included through
508
566
  * {@link matchesGitPath}, so one call answers the whole question and no caller
509
567
  * has to remember to ask twice.
510
568
  *
@@ -601,6 +659,25 @@ function computeDigest(content) {
601
659
  return createHash("sha256").update(content, "utf8").digest("hex");
602
660
  }
603
661
  /**
662
+ * Projects exact bytes stated in hexadecimal to their SHA-256 digest.
663
+ *
664
+ * @param hex - The exact lowercase hexadecimal bytes to digest.
665
+ * @returns Sixty-four lowercase hexadecimal digits.
666
+ * @throws `ScaffoldError('INVALID', …)` when `hex` is not exact bounded
667
+ * lowercase hexadecimal text.
668
+ *
669
+ * @example
670
+ * ```ts
671
+ * import { hexToDigest } from '@orkestrel/scaffold/server'
672
+ *
673
+ * hexToDigest('68690a') // '98ea6e4f216f2fb4b69fff9b3a44842c38686ca685f3f55dc48c5d3fb1107be4'
674
+ * ```
675
+ */
676
+ function hexToDigest(hex) {
677
+ if (!isHex(hex)) throw new ScaffoldError("INVALID", "Digest input is not exact hexadecimal bytes", { hex });
678
+ return createHash("sha256").update(Buffer.from(hex, "hex")).digest("hex");
679
+ }
680
+ /**
604
681
  * Compute the digest of a vendored host's declared membership.
605
682
  *
606
683
  * @param entries - The ordered file membership declarations.
@@ -627,7 +704,8 @@ function computeManifestDigest(entries, roots) {
627
704
  entries: entries.map((entry) => ({
628
705
  storage: entry.storage,
629
706
  destination: entry.destination,
630
- executable: entry.executable
707
+ executable: entry.executable,
708
+ digest: entry.digest
631
709
  })),
632
710
  roots: [...roots]
633
711
  }));
@@ -1269,6 +1347,7 @@ function readSnapshot(target, paths) {
1269
1347
  * Read a vendored host's manifest, when it carries one.
1270
1348
  *
1271
1349
  * @param host - The vendored host root to read.
1350
+ * @param name - The root-relative manifest path. Default: `manifest.json`.
1272
1351
  * @returns The manifest, or `undefined` when the host carries none.
1273
1352
  * @throws `ScaffoldError('INVALID', …)` when `host` is not a host path.
1274
1353
  * @throws `ScaffoldError('TARGET', …)` when the manifest is there but cannot be
@@ -1294,9 +1373,8 @@ function readSnapshot(target, paths) {
1294
1373
  * readHostManifest('./dist/host') // the manifest, or undefined for a raw root
1295
1374
  * ```
1296
1375
  */
1297
- function readHostManifest(host) {
1376
+ function readHostManifest(host, name = MANIFEST_NAME) {
1298
1377
  if (!isFilesystemPath(host)) throw new ScaffoldError("INVALID", "Host root is not a host path", { host });
1299
- const name = MANIFEST_NAME;
1300
1378
  const full = resolveContainedPath(host, name);
1301
1379
  if (full === void 0) throw new ScaffoldError("INVALID", `Host manifest leaves its root at ${host}`, { host });
1302
1380
  const status = attempt(() => lstatSync(full));
@@ -1315,6 +1393,55 @@ function readHostManifest(host) {
1315
1393
  return manifest;
1316
1394
  }
1317
1395
  /**
1396
+ * Reads the installed vendored host floor as a value.
1397
+ *
1398
+ * @param root - The vendored host root. Default: the installed package's
1399
+ * vendored root, resolved from this module's location.
1400
+ * @returns The verified manifest and the exact bytes of every declared entry.
1401
+ * @throws `ScaffoldError('TARGET', …)` when the root is not a readable physical
1402
+ * directory, its manifest is absent or unreadable, the manifest does not verify,
1403
+ * or a declared file is unreadable or misses its digest.
1404
+ *
1405
+ * @remarks
1406
+ * Reads the same default floor the {@link Materializer} uses. Each declared
1407
+ * file is addressed through the manifest's storage name and retained under its
1408
+ * destination, so the returned value has the same shape as the installed root.
1409
+ * When this module executes from TypeScript source, the committed inventory is
1410
+ * the manifest and each checkout destination supplies its bytes. The emitted
1411
+ * module reads the staged `manifest.json` file and each storage path instead.
1412
+ *
1413
+ * @example
1414
+ * ```ts
1415
+ * import { readHostFloor } from '@orkestrel/scaffold/server'
1416
+ *
1417
+ * readHostFloor().manifest // the installed floor's verified membership
1418
+ * ```
1419
+ */
1420
+ function readHostFloor(root) {
1421
+ const location = fileURLToPath(import.meta.url);
1422
+ const module = dirname(location);
1423
+ const source = root === void 0 && extname(location) === ".ts";
1424
+ const host = root ?? resolve(module, source ? "../.." : "../../host");
1425
+ if (!isPhysicalDirectory(host)) throw new ScaffoldError("TARGET", `The vendored host root is not readable at ${host}`, { host });
1426
+ const manifest = readHostManifest(host, source ? HOST_INVENTORY_PATH : MANIFEST_NAME);
1427
+ if (manifest === void 0) throw new ScaffoldError("TARGET", `The vendored host carries no manifest at ${host}`, { host });
1428
+ const bytes = {};
1429
+ for (const entry of manifest.entries) {
1430
+ const path = source ? entry.destination : entry.storage;
1431
+ const hex = readFileHex(host, path);
1432
+ if (hex === void 0 || hexToDigest(hex) !== entry.digest) throw new ScaffoldError("TARGET", `The vendored host cannot read the declared file at ${path}`, {
1433
+ host,
1434
+ path,
1435
+ destination: entry.destination
1436
+ });
1437
+ bytes[entry.destination] = hex;
1438
+ }
1439
+ return {
1440
+ manifest,
1441
+ bytes
1442
+ };
1443
+ }
1444
+ /**
1318
1445
  * Derive one vendored-host manifest entry from a file in a checkout.
1319
1446
  *
1320
1447
  * @param destination - The target-relative path the file is written to.
@@ -1338,20 +1465,155 @@ function readHostManifest(host) {
1338
1465
  * import { readManifestEntry } from '@orkestrel/scaffold/server'
1339
1466
  *
1340
1467
  * readManifestEntry('.gitignore', '/tmp/checkout/.gitignore')
1341
- * // { storage: 'dotfiles/gitignore', destination: '.gitignore', executable: false }
1468
+ * // { storage: 'dotfiles/gitignore', destination: '.gitignore', executable: false, digest: '...' }
1342
1469
  * ```
1343
1470
  */
1344
1471
  function readManifestEntry(destination, source) {
1345
- if (!isPhysicalFile(source)) return void 0;
1346
- const status = attempt(() => lstatSync(source));
1347
- if (!status.success || status.value.size > MAX_ARTIFACT_BYTES) return void 0;
1472
+ const digest = computeFileDigest(source);
1473
+ if (digest === void 0) return void 0;
1348
1474
  return {
1349
1475
  storage: pathToStorage(destination),
1350
1476
  destination,
1351
- executable: matchesExecutablePath(destination)
1477
+ executable: matchesExecutablePath(destination),
1478
+ digest
1479
+ };
1480
+ }
1481
+ /**
1482
+ * Assemble a whole vendored host from live files and the installed floor.
1483
+ *
1484
+ * @param files - The host-owned vendored files read from the repository, one
1485
+ * row per path.
1486
+ * @param floor - The installed host floor, which fixes the membership a fill
1487
+ * may draw from and supplies the bytes owned by another surface.
1488
+ * @returns The assembled host, or `undefined` when any row produced no answer or
1489
+ * names a path the floor does not declare, or when a host-owned path is absent.
1490
+ *
1491
+ * @remarks
1492
+ * The one place the host-owned all-or-nothing rule is decided, so no verb
1493
+ * restates it. The host surface contributes one baseline: a fill carries live
1494
+ * bytes for every path that surface writes, or it is nothing. A row that failed,
1495
+ * went missing, names an undeclared path, or leaves a host-owned path absent
1496
+ * answers `undefined`. Deferred paths are presence-only and retain the installed
1497
+ * floor bytes that their catalog or mirror surface owns; repair never writes
1498
+ * those floor bytes. One `Host` can therefore carry live host bytes beside floor
1499
+ * bytes without mixing baselines within a surface.
1500
+ *
1501
+ * The emitted entries keep the release's own order and its storage and
1502
+ * executable declarations, and carry digests recomputed over the bytes the fill
1503
+ * actually holds. That is what lets a reader verify the value against itself,
1504
+ * and it is why an undeclared path is refused rather than added: membership
1505
+ * moves with a release, never with a fetch.
1506
+ *
1507
+ * @example
1508
+ * ```ts
1509
+ * import { filesToHost } from '@orkestrel/scaffold/server'
1510
+ *
1511
+ * filesToHost([{ path: 'AGENTS.md', lookup: 'found', hex: '23204167656e74730a' }], floor)
1512
+ * // { manifest: { entries: [ … ], roots: [ … ], digest: '…' }, bytes: { 'AGENTS.md': '…' } }
1513
+ * ```
1514
+ */
1515
+ function filesToHost(files, floor) {
1516
+ const declared = new Set(floor.manifest.entries.map((entry) => entry.destination));
1517
+ const held = /* @__PURE__ */ new Map();
1518
+ for (const file of files) {
1519
+ if (file.lookup !== "found" || !declared.has(file.path)) return void 0;
1520
+ if (!isDeferredPath(file.path)) held.set(file.path, file.hex);
1521
+ }
1522
+ const entries = [];
1523
+ const bytes = {};
1524
+ for (const entry of floor.manifest.entries) {
1525
+ const hex = isDeferredPath(entry.destination) ? floor.bytes[entry.destination] : held.get(entry.destination);
1526
+ if (hex === void 0) return void 0;
1527
+ entries.push({
1528
+ storage: entry.storage,
1529
+ destination: entry.destination,
1530
+ executable: entry.executable,
1531
+ digest: hexToDigest(hex)
1532
+ });
1533
+ bytes[entry.destination] = hex;
1534
+ }
1535
+ return {
1536
+ manifest: {
1537
+ entries,
1538
+ roots: floor.manifest.roots,
1539
+ digest: computeManifestDigest(entries, floor.manifest.roots)
1540
+ },
1541
+ bytes
1352
1542
  };
1353
1543
  }
1354
1544
  /**
1545
+ * Stage the named destinations of a value host into a private root.
1546
+ *
1547
+ * @param host - The host whose bytes are written, keyed by destination.
1548
+ * @param root - The private directory to fill; it must already be a directory
1549
+ * this process may write into.
1550
+ * @param destinations - The destinations to stage, each declared by `host`.
1551
+ * @returns The entry staged for each destination, in the order requested.
1552
+ * @throws `ScaffoldError('INVALID', …)` when `root` is not a host path or a
1553
+ * storage name leaves it.
1554
+ * @throws `ScaffoldError('TARGET', …)` when a destination is one the host does
1555
+ * not declare, carries no bytes, or carries bytes that miss its declared digest.
1556
+ * @throws `ScaffoldError('WRITE', …)` when a file cannot be written or does not
1557
+ * read back as the bytes it was given.
1558
+ *
1559
+ * @remarks
1560
+ * Each file lands under the storage name the manifest declares and takes the
1561
+ * executable bit that manifest records, so a root filled from a value is the
1562
+ * same shape as one staged from a checkout and a reader cannot tell them apart.
1563
+ * That is what lets a mutation copy real files with real modes from bytes a
1564
+ * caller supplied, instead of degrading them to plain text writes.
1565
+ *
1566
+ * The bytes are digested before the write and the staged file after it, so a
1567
+ * value that disagrees with its own manifest is told apart from a write that
1568
+ * did not land.
1569
+ *
1570
+ * @example
1571
+ * ```ts
1572
+ * import { stageBytes } from '@orkestrel/scaffold/server'
1573
+ *
1574
+ * stageBytes(host, '/tmp/orkestrel-host-a1b2', ['scripts/codex.sh'])
1575
+ * // [{ storage: 'scripts/codex.sh', destination: 'scripts/codex.sh', executable: true, digest: '…' }]
1576
+ * ```
1577
+ */
1578
+ function stageBytes(host, root, destinations) {
1579
+ if (!isFilesystemPath(root)) throw new ScaffoldError("INVALID", "Staging host root is not a host path", { host: root });
1580
+ const declared = new Map(host.manifest.entries.map((entry) => [entry.destination, entry]));
1581
+ const staged = [];
1582
+ for (const destination of destinations) {
1583
+ const entry = declared.get(destination);
1584
+ const hex = host.bytes[destination];
1585
+ if (entry === void 0 || hex === void 0) throw new ScaffoldError("TARGET", `The host carries no bytes for ${destination}`, {
1586
+ host: root,
1587
+ destination
1588
+ });
1589
+ if (hexToDigest(hex) !== entry.digest) throw new ScaffoldError("TARGET", `The host bytes for ${destination} miss its digest`, {
1590
+ host: root,
1591
+ destination
1592
+ });
1593
+ const full = resolveContainedPath(root, entry.storage);
1594
+ if (full === void 0) throw new ScaffoldError("INVALID", `Host storage leaves its root at ${entry.storage}`, {
1595
+ host: root,
1596
+ storage: entry.storage
1597
+ });
1598
+ const written = attempt(() => {
1599
+ mkdirSync(dirname(full), { recursive: true });
1600
+ writeFileSync(full, Buffer.from(hex, "hex"), { flag: "wx" });
1601
+ if (entry.executable) chmodSync(full, 493);
1602
+ });
1603
+ if (!written.success) throw new ScaffoldError("WRITE", `Host bytes could not be staged at ${entry.storage}`, {
1604
+ host: root,
1605
+ storage: entry.storage,
1606
+ error: written.error
1607
+ });
1608
+ if (computeFileDigest(full) !== entry.digest) throw new ScaffoldError("WRITE", `Staged host bytes at ${entry.storage} did not read back`, {
1609
+ host: root,
1610
+ storage: entry.storage
1611
+ });
1612
+ staged.push(entry);
1613
+ }
1614
+ return staged;
1615
+ }
1616
+ /**
1355
1617
  * Stage a vendored host root from a real checkout.
1356
1618
  *
1357
1619
  * @param checkout - The checkout the vendored paths are read from.
@@ -1441,7 +1703,7 @@ function stageHost(checkout, host) {
1441
1703
  missing
1442
1704
  });
1443
1705
  const stored = /* @__PURE__ */ new Set([MANIFEST_NAME]);
1444
- const entries = [];
1706
+ const candidates = [];
1445
1707
  for (const destination of vendored) {
1446
1708
  const full = resolveContainedPath(source, destination);
1447
1709
  if (full === void 0) throw new ScaffoldError("INVALID", `Vendored path leaves its checkout at ${destination}`, {
@@ -1460,9 +1722,9 @@ function stageHost(checkout, host) {
1460
1722
  storage: entry.storage
1461
1723
  });
1462
1724
  stored.add(entry.storage);
1463
- entries.push(entry);
1725
+ candidates.push(entry);
1464
1726
  }
1465
- entries.sort((first, second) => first.storage < second.storage ? -1 : 1);
1727
+ candidates.sort((first, second) => first.storage < second.storage ? -1 : 1);
1466
1728
  roots.sort();
1467
1729
  const root = resolve(host);
1468
1730
  const established = attempt(() => mkdirSync(root, { recursive: true }));
@@ -1470,7 +1732,8 @@ function stageHost(checkout, host) {
1470
1732
  host: root,
1471
1733
  ...established.success ? {} : { error: established.error }
1472
1734
  });
1473
- for (const entry of entries) {
1735
+ const entries = [];
1736
+ for (const entry of candidates) {
1474
1737
  const origin = resolveContainedPath(source, entry.destination);
1475
1738
  const destination = resolveContainedPath(root, entry.storage);
1476
1739
  if (origin === void 0 || destination === void 0) throw new ScaffoldError("INVALID", `Vendored path leaves its root at ${entry.destination}`, {
@@ -1489,6 +1752,15 @@ function stageHost(checkout, host) {
1489
1752
  storage: entry.storage,
1490
1753
  error: copied.error
1491
1754
  });
1755
+ const digest = computeFileDigest(destination);
1756
+ if (digest === void 0) throw new ScaffoldError("WRITE", `Vendored file could not be verified at ${entry.storage}`, {
1757
+ host: root,
1758
+ storage: entry.storage
1759
+ });
1760
+ entries.push({
1761
+ ...entry,
1762
+ digest
1763
+ });
1492
1764
  }
1493
1765
  const manifest = {
1494
1766
  entries,
@@ -1510,6 +1782,67 @@ function stageHost(checkout, host) {
1510
1782
  return entries;
1511
1783
  }
1512
1784
  /**
1785
+ * Stages the committed inventory of the files a vendored host carries.
1786
+ *
1787
+ * @param checkout - The checkout whose vendored paths are inventoried.
1788
+ * @param path - The host path where the JSON inventory is written.
1789
+ * @returns The validated manifest written to `path`.
1790
+ * @throws `ScaffoldError('INVALID', …)` when `path` is not a host path.
1791
+ * @throws `ScaffoldError('WRITE', …)` when a temporary host or the inventory
1792
+ * cannot be written or removed.
1793
+ * @throws `ScaffoldError('TARGET', …)` when the staged inventory does not read
1794
+ * back through the manifest validator.
1795
+ *
1796
+ * @remarks
1797
+ * Uses {@link stageHost} as the single vendored-path expansion. The temporary
1798
+ * host supplies the same entries, roots, per-file digests, and membership
1799
+ * digest as the published host while the requested output remains one JSON
1800
+ * file.
1801
+ *
1802
+ * @example
1803
+ * ```ts
1804
+ * import { stageInventory } from '@orkestrel/scaffold/server'
1805
+ *
1806
+ * stageInventory(process.cwd(), 'host.json') // the committed host inventory
1807
+ * ```
1808
+ */
1809
+ function stageInventory(checkout, path) {
1810
+ if (!isFilesystemPath(path)) throw new ScaffoldError("INVALID", "Inventory destination is not a host path", { path });
1811
+ const temporary = attempt(() => mkdtempSync(join(tmpdir(), "orkestrel-scaffold-host-")));
1812
+ if (!temporary.success) throw new ScaffoldError("WRITE", "Inventory staging root could not be established", {
1813
+ path,
1814
+ error: temporary.error
1815
+ });
1816
+ const staged = attempt(() => {
1817
+ stageHost(checkout, temporary.value);
1818
+ const manifest = readHostManifest(temporary.value);
1819
+ if (manifest === void 0) throw new ScaffoldError("TARGET", "The staged inventory carries no manifest", { path });
1820
+ const target = resolve(path);
1821
+ const published = attempt(() => {
1822
+ mkdirSync(dirname(target), { recursive: true });
1823
+ writeFileSync(target, `${JSON.stringify(manifest, null, " ")}\n`, "utf8");
1824
+ });
1825
+ if (!published.success) throw new ScaffoldError("WRITE", `Inventory could not be written at ${target}`, {
1826
+ path: target,
1827
+ error: published.error
1828
+ });
1829
+ const text = readFileText(dirname(target), basename(target), MAX_MANIFEST_BYTES);
1830
+ const verified = text === void 0 ? void 0 : parseJSONAs(text, isHostManifest);
1831
+ if (verified === void 0 || verified.digest !== computeManifestDigest(verified.entries, verified.roots)) throw new ScaffoldError("TARGET", `Inventory does not read back at ${target}`, { path: target });
1832
+ return verified;
1833
+ });
1834
+ const removed = attempt(() => rmSync(temporary.value, {
1835
+ recursive: true,
1836
+ force: true
1837
+ }));
1838
+ if (!removed.success) throw new ScaffoldError("WRITE", `Inventory staging root could not be removed`, {
1839
+ path: temporary.value,
1840
+ error: removed.error
1841
+ });
1842
+ if (!staged.success) throw staged.error;
1843
+ return staged.value;
1844
+ }
1845
+ /**
1513
1846
  * Capture one directory's physical identity.
1514
1847
  *
1515
1848
  * @param path - The resolved directory path to capture.
@@ -2195,6 +2528,12 @@ var WriteTransaction = class {
2195
2528
  * case, so a manifest naming `agents.md` for a stored `AGENTS.md` is refused on
2196
2529
  * a case-insensitive filesystem rather than silently resolved.
2197
2530
  *
2531
+ * That host arrives as a directory path or as a whole {@link Host} value, and
2532
+ * every verb reads one immutable host either way. A value is owned, verified
2533
+ * against its own membership and digests, and read in memory; a write fills it
2534
+ * into a private root and copies from there, so the executable declarations the
2535
+ * release fixed reach the target from either representation.
2536
+ *
2198
2537
  * What a mutation guarantees is exactly what {@link WriteTransaction}
2199
2538
  * guarantees, and no more: a caught failure part way through a commit rolls the
2200
2539
  * whole commit back, no destination ever receives half-written bytes, and a
@@ -2220,25 +2559,34 @@ var Materializer = class Materializer {
2220
2559
  static #opening = "<!-- orkestrel:catalog -->";
2221
2560
  static #closing = "<!-- /orkestrel:catalog -->";
2222
2561
  #emitter;
2223
- #host;
2562
+ #root;
2563
+ #value;
2224
2564
  #manifest;
2225
2565
  #entries;
2226
2566
  #destroyed = false;
2227
2567
  /**
2228
2568
  * Construct a materializer over one vendored host root.
2229
2569
  *
2230
- * @param options - The vendored host root, the initial listeners, and the
2231
- * listener-error handler.
2570
+ * @param options - The vendored host, in either representation, the initial
2571
+ * listeners, and the listener-error handler.
2232
2572
  * @throws {@link ScaffoldError} coded `INVALID` when `options` is present but
2233
2573
  * is not an option bag this materializer accepts, and `TARGET` when the host
2234
- * carries a manifest that cannot be read or does not match what it stores.
2574
+ * carries a manifest that cannot be read, does not match what it stores, or
2575
+ * is a value that does not agree with the bytes beside it.
2235
2576
  *
2236
2577
  * @remarks
2237
- * `host` defaults to this package's own vendored root, resolved from this
2238
- * module's own location so it never depends on the caller's working
2239
- * directory. A host carrying no manifest is read as a raw checkout and every
2578
+ * A `host` path defaults to this package's own vendored root, resolved from
2579
+ * this module's own location so it never depends on the caller's working
2580
+ * directory. A root carrying no manifest is read as a raw checkout and every
2240
2581
  * artifact maps onto it one to one.
2241
2582
  *
2583
+ * A `host` value is owned before it is read and then held immutable, so the
2584
+ * bytes this check measured are the bytes every later read returns. It is
2585
+ * verified the way a root is, against the same membership law: the manifest
2586
+ * digest must cover the membership beside it, no two entries may claim one
2587
+ * destination, and the fill must carry exactly one hashing byte string per
2588
+ * declared entry.
2589
+ *
2242
2590
  * The host is read here rather than on first use, so a broken vendored root
2243
2591
  * fails at construction where the caller can still act on it, and so nothing
2244
2592
  * has to carry a second flag recording whether the read has happened yet.
@@ -2249,15 +2597,27 @@ var Materializer = class Materializer {
2249
2597
  ...options?.on === void 0 ? {} : { on: options.on },
2250
2598
  ...options?.error === void 0 ? {} : { error: options.error }
2251
2599
  });
2252
- this.#host = options?.host ?? resolve(dirname(fileURLToPath(import.meta.url)), "../../host");
2253
- const read = attempt(() => readHostManifest(this.#host));
2254
- if (!read.success) throw this.#error("TARGET", "The vendored host carries a manifest that cannot be read.", {
2255
- host: this.#host,
2256
- error: read.error
2257
- });
2258
- this.#manifest = read.value;
2259
- this.#entries = new Map((read.value?.entries ?? []).map((entry) => [entry.destination, entry]));
2260
- if (read.value !== void 0) this.#reconcile(read.value);
2600
+ const supplied = options?.host ?? readHostFloor();
2601
+ if (supplied !== void 0 && !isFilesystemPath(supplied)) {
2602
+ const value = this.#own(supplied);
2603
+ this.#value = value;
2604
+ this.#root = void 0;
2605
+ this.#manifest = value.manifest;
2606
+ this.#entries = new Map(value.manifest.entries.map((entry) => [entry.destination, entry]));
2607
+ this.#verify(value);
2608
+ } else {
2609
+ const root = supplied;
2610
+ this.#value = void 0;
2611
+ this.#root = root;
2612
+ const read = attempt(() => readHostManifest(root));
2613
+ if (!read.success) throw this.#error("TARGET", "The vendored host carries a manifest that cannot be read.", {
2614
+ host: root,
2615
+ error: read.error
2616
+ });
2617
+ this.#manifest = read.value;
2618
+ this.#entries = new Map((read.value?.entries ?? []).map((entry) => [entry.destination, entry]));
2619
+ if (read.value !== void 0) this.#reconcile(read.value, root);
2620
+ }
2261
2621
  }
2262
2622
  /** The materializer's observation channel. */
2263
2623
  get emitter() {
@@ -2453,7 +2813,7 @@ var Materializer = class Materializer {
2453
2813
  *
2454
2814
  * @param plan - The compiled plan that decides which paths are foreign.
2455
2815
  * @param audit - The preview returned by this materializer's `audit` method; it must agree with the candidate set this call re-derives.
2456
- * @param repository - The target's git state; only a tracked path is ever deleted.
2816
+ * @param worktree - The target's git state; only a tracked path is ever deleted.
2457
2817
  * @param target - The directory to delete from.
2458
2818
  * @returns The paths removed.
2459
2819
  * @throws {@link ScaffoldError} coded `INVALID` when an argument is not the
@@ -2474,11 +2834,11 @@ var Materializer = class Materializer {
2474
2834
  * any foreign finding, including one the deletion itself would skip, because a
2475
2835
  * preview stale anywhere is stale evidence.
2476
2836
  */
2477
- remove(plan, audit, repository, target) {
2837
+ remove(plan, audit, worktree, target) {
2478
2838
  this.#assertAlive();
2479
2839
  const accepted = this.#accept(plan, isPlan, "plan");
2480
2840
  const preview = this.#accept(audit, isAudit, "audit");
2481
- const state = this.#accept(repository, isRepository, "repository");
2841
+ const state = this.#accept(worktree, isWorktree, "worktree");
2482
2842
  const directory = this.#accept(target, isFilesystemPath, "target");
2483
2843
  if (state.dirty.length > 0) throw this.#error("TARGET", `The target at ${directory} carries uncommitted changes.`, {
2484
2844
  target: directory,
@@ -2521,20 +2881,41 @@ var Materializer = class Materializer {
2521
2881
  this.#emitter.emit("destroy");
2522
2882
  this.#emitter.destroy();
2523
2883
  }
2524
- #reconcile(manifest) {
2525
- const walked = attempt(() => listFiles(this.#host));
2884
+ #own(host) {
2885
+ const owned = cloneValue(host);
2886
+ if (isHost(owned)) return owned;
2887
+ throw this.#error("INVALID", "The host argument is not the exact shape this materializer accepts.", { field: "host" });
2888
+ }
2889
+ #verify(host) {
2890
+ const { entries, roots } = host.manifest;
2891
+ if (host.manifest.digest !== computeManifestDigest(entries, roots)) throw this.#error("TARGET", "The vendored host manifest does not cover the membership beside it.");
2892
+ if (this.#entries.size !== entries.length) throw this.#error("TARGET", "The vendored host manifest maps two files to one destination.");
2893
+ if (new Set(entries.map((entry) => entry.storage)).size !== entries.length) throw this.#error("TARGET", "The vendored host manifest maps two destinations to one file.");
2894
+ const held = Object.keys(host.bytes);
2895
+ if (held.length !== entries.length) throw this.#error("TARGET", "The vendored host does not carry what its manifest declares.", {
2896
+ held: held.length,
2897
+ declared: entries.length
2898
+ });
2899
+ for (const entry of entries) {
2900
+ const hex = host.bytes[entry.destination];
2901
+ if (hex === void 0) throw this.#error("TARGET", `The vendored host carries no bytes for ${entry.destination}.`, { destination: entry.destination });
2902
+ if (hexToDigest(hex) !== entry.digest) throw this.#error("TARGET", `The vendored host carries bytes for ${entry.destination} that miss its digest.`, { destination: entry.destination });
2903
+ }
2904
+ }
2905
+ #reconcile(manifest, root) {
2906
+ const walked = attempt(() => listFiles(root));
2526
2907
  if (!walked.success) throw this.#error("TARGET", "The vendored host cannot be inventoried.", {
2527
- host: this.#host,
2908
+ host: root,
2528
2909
  error: walked.error
2529
2910
  });
2530
2911
  const declared = [...manifest.entries.map((entry) => entry.storage), "manifest.json"].sort();
2531
2912
  const stored = walked.value;
2532
2913
  if (stored.length !== declared.length || stored.some((name, index) => name !== declared[index])) throw this.#error("TARGET", "The vendored host does not store what its manifest declares.", {
2533
- host: this.#host,
2914
+ host: root,
2534
2915
  stored: stored.length,
2535
2916
  declared: declared.length
2536
2917
  });
2537
- if (this.#entries.size !== manifest.entries.length) throw this.#error("TARGET", "The vendored host manifest maps two files to one destination.", { host: this.#host });
2918
+ if (this.#entries.size !== manifest.entries.length) throw this.#error("TARGET", "The vendored host manifest maps two files to one destination.", { host: root });
2538
2919
  }
2539
2920
  #hydrate(plan) {
2540
2921
  const artifacts = [];
@@ -2550,7 +2931,7 @@ var Materializer = class Materializer {
2550
2931
  artifacts.push(...expanded);
2551
2932
  }
2552
2933
  if (remaining < 0) throw this.#error("TARGET", "The hydrated plan retains more bytes than one plan may.", {
2553
- host: this.#host,
2934
+ host: this.#root,
2554
2935
  limit: MAX_TOTAL_ARTIFACT_BYTES
2555
2936
  });
2556
2937
  return {
@@ -2584,7 +2965,8 @@ var Materializer = class Materializer {
2584
2965
  continue;
2585
2966
  }
2586
2967
  if (this.#manifest !== void 0) continue;
2587
- const directory = resolveContainedPath(this.#host, source);
2968
+ const root = this.#root;
2969
+ const directory = root === void 0 ? void 0 : resolveContainedPath(root, source);
2588
2970
  if (directory !== void 0 && isPhysicalDirectory(directory)) roots.add(artifact.path);
2589
2971
  }
2590
2972
  return [...roots];
@@ -2596,7 +2978,7 @@ var Materializer = class Materializer {
2596
2978
  const matched = manifest.entries.filter((entry) => entry.destination === source || entry.destination.startsWith(`${source}/`));
2597
2979
  const rooted = manifest.roots.some((root) => root === source || root.startsWith(`${source}/`));
2598
2980
  if (matched.length === 0 && !rooted) throw this.#error("TARGET", `The vendored host does not carry ${source}.`, {
2599
- host: this.#host,
2981
+ host: this.#root,
2600
2982
  source
2601
2983
  });
2602
2984
  const expanded = [];
@@ -2607,29 +2989,30 @@ var Materializer = class Materializer {
2607
2989
  expanded.push(this.#presence(artifact, path, entry.destination));
2608
2990
  continue;
2609
2991
  }
2610
- if (this.#deferred(path)) {
2992
+ if (isDeferredPath(path)) {
2611
2993
  expanded.push(this.#presence(artifact, path, entry.destination));
2612
2994
  continue;
2613
2995
  }
2614
- const hex = this.#read(entry.storage, budget);
2996
+ const hex = this.#read(entry, budget);
2615
2997
  budget -= hex.length / 2;
2616
2998
  expanded.push(this.#hydrated(artifact, path, entry.destination, hex));
2617
2999
  }
2618
3000
  return expanded;
2619
3001
  }
2620
3002
  #expandRaw(artifact, source, remaining) {
2621
- const full = resolveContainedPath(this.#host, source);
3003
+ const root = this.#root;
3004
+ const full = root === void 0 ? void 0 : resolveContainedPath(root, source);
2622
3005
  if (full === void 0) throw this.#error("TARGET", `The host source at ${source} leaves its root.`, {
2623
- host: this.#host,
3006
+ host: this.#root,
2624
3007
  source
2625
3008
  });
2626
3009
  if (isPhysicalFile(full)) {
2627
3010
  if (WORKSPACE_OWNED_PATHS.includes(artifact.path)) return [this.#presence(artifact, artifact.path, source)];
2628
- if (this.#deferred(artifact.path)) return [this.#presence(artifact, artifact.path, source)];
2629
- return [this.#hydrated(artifact, artifact.path, source, this.#read(source, remaining))];
3011
+ if (isDeferredPath(artifact.path)) return [this.#presence(artifact, artifact.path, source)];
3012
+ return [this.#hydrated(artifact, artifact.path, source, this.#readRoot(source, remaining))];
2630
3013
  }
2631
3014
  if (!isPhysicalDirectory(full)) throw this.#error("TARGET", `The host source at ${source} is not a readable file.`, {
2632
- host: this.#host,
3015
+ host: this.#root,
2633
3016
  source
2634
3017
  });
2635
3018
  const expanded = [];
@@ -2641,11 +3024,11 @@ var Materializer = class Materializer {
2641
3024
  expanded.push(this.#presence(artifact, path, destination));
2642
3025
  continue;
2643
3026
  }
2644
- if (this.#deferred(path)) {
3027
+ if (isDeferredPath(path)) {
2645
3028
  expanded.push(this.#presence(artifact, path, destination));
2646
3029
  continue;
2647
3030
  }
2648
- const hex = this.#read(destination, budget);
3031
+ const hex = this.#readRoot(destination, budget);
2649
3032
  budget -= hex.length / 2;
2650
3033
  expanded.push(this.#hydrated(artifact, path, destination, hex));
2651
3034
  }
@@ -2669,15 +3052,12 @@ var Materializer = class Materializer {
2669
3052
  const source = artifact.source ?? artifact.path;
2670
3053
  if (destination === source) return artifact.path;
2671
3054
  if (!destination.startsWith(`${source}/`)) throw this.#error("TARGET", `The vendored destination ${destination} is outside ${source}.`, {
2672
- host: this.#host,
3055
+ host: this.#root,
2673
3056
  source,
2674
3057
  destination
2675
3058
  });
2676
3059
  return `${artifact.path}/${destination.slice(source.length + 1)}`;
2677
3060
  }
2678
- #deferred(path) {
2679
- return path === CATALOG_AGENT_PATH || path.startsWith("guides/") && path.endsWith(".md");
2680
- }
2681
3061
  #presence(artifact, path, destination) {
2682
3062
  return {
2683
3063
  path,
@@ -2699,10 +3079,20 @@ var Materializer = class Materializer {
2699
3079
  hex
2700
3080
  };
2701
3081
  }
2702
- #read(storage, budget) {
2703
- const hex = readFileHex(this.#host, storage, Math.max(0, Math.min(MAX_ARTIFACT_BYTES, budget)));
3082
+ #read(entry, budget) {
3083
+ const held = this.#value?.bytes[entry.destination];
3084
+ if (held === void 0) return this.#readRoot(entry.storage, budget);
3085
+ if (held.length / 2 > Math.max(0, Math.min(MAX_ARTIFACT_BYTES, budget))) throw this.#error("TARGET", `The vendored host cannot be read at ${entry.destination}.`, {
3086
+ destination: entry.destination,
3087
+ limit: MAX_ARTIFACT_BYTES
3088
+ });
3089
+ return held;
3090
+ }
3091
+ #readRoot(storage, budget) {
3092
+ const root = this.#root;
3093
+ const hex = root === void 0 ? void 0 : readFileHex(root, storage, Math.max(0, Math.min(MAX_ARTIFACT_BYTES, budget)));
2704
3094
  if (hex === void 0) throw this.#error("TARGET", `The vendored host cannot be read at ${storage}.`, {
2705
- host: this.#host,
3095
+ host: this.#root,
2706
3096
  storage
2707
3097
  });
2708
3098
  return hex;
@@ -2808,20 +3198,49 @@ var Materializer = class Materializer {
2808
3198
  skipped,
2809
3199
  removed: []
2810
3200
  });
2811
- const transaction = this.#open(target, paths, preconditions);
2812
- const staged = attempt(() => {
2813
- for (const artifact of writes) if (artifact.origin === "host") this.#copy(transaction, artifact);
2814
- else transaction.write(artifact.path, artifact.content);
2815
- for (const path of directories) transaction.establish(path);
2816
- });
2817
- const written = this.#close(transaction, staged, target);
2818
- for (const path of written) this.#emitter.emit("write", path);
2819
- return this.#finish({
2820
- target,
2821
- written,
2822
- skipped,
2823
- removed: []
3201
+ const filled = this.#fill(writes);
3202
+ try {
3203
+ const transaction = this.#open(target, paths, preconditions);
3204
+ const staged = attempt(() => {
3205
+ for (const artifact of writes) if (artifact.origin === "host") this.#copy(transaction, artifact, filled ?? this.#root);
3206
+ else transaction.write(artifact.path, artifact.content);
3207
+ for (const path of directories) transaction.establish(path);
3208
+ });
3209
+ const written = this.#close(transaction, staged, target);
3210
+ for (const path of written) this.#emitter.emit("write", path);
3211
+ return this.#finish({
3212
+ target,
3213
+ written,
3214
+ skipped,
3215
+ removed: []
3216
+ });
3217
+ } finally {
3218
+ if (filled !== void 0) rmSync(filled, {
3219
+ recursive: true,
3220
+ force: true
3221
+ });
3222
+ }
3223
+ }
3224
+ #fill(writes) {
3225
+ const value = this.#value;
3226
+ if (value === void 0) return void 0;
3227
+ const destinations = /* @__PURE__ */ new Set();
3228
+ for (const artifact of writes) {
3229
+ if (artifact.origin !== "host") continue;
3230
+ destinations.add(artifact.source ?? artifact.path);
3231
+ }
3232
+ if (destinations.size === 0) return void 0;
3233
+ const opened = attempt(() => mkdtempSync(join(tmpdir(), "orkestrel-scaffold-fill-")));
3234
+ if (!opened.success) throw this.#error("WRITE", "The supplied host could not be filled into a private root.", { error: opened.error });
3235
+ const root = opened.value;
3236
+ const stored = attempt(() => stageBytes(value, root, [...destinations]));
3237
+ if (stored.success) return root;
3238
+ rmSync(root, {
3239
+ recursive: true,
3240
+ force: true
2824
3241
  });
3242
+ this.#emitter.emit("error", stored.error);
3243
+ throw stored.error;
2825
3244
  }
2826
3245
  #purge(target, removals, skipped, preconditions) {
2827
3246
  if (removals.length === 0) return this.#finish({
@@ -2865,13 +3284,13 @@ var Materializer = class Materializer {
2865
3284
  this.#emitter.emit("error", committed.error);
2866
3285
  throw committed.error;
2867
3286
  }
2868
- #copy(transaction, artifact) {
3287
+ #copy(transaction, artifact, root) {
2869
3288
  const destination = artifact.source ?? artifact.path;
2870
3289
  const entry = this.#entries.get(destination);
2871
3290
  const storage = entry === void 0 ? destination : entry.storage;
2872
- const source = resolveContainedPath(this.#host, storage);
3291
+ const source = root === void 0 ? void 0 : resolveContainedPath(root, storage);
2873
3292
  if (source === void 0) throw this.#error("TARGET", `The vendored source at ${storage} leaves its root.`, {
2874
- host: this.#host,
3293
+ host: root,
2875
3294
  storage
2876
3295
  });
2877
3296
  transaction.copy(artifact.path, source, entry?.executable === true);
@@ -2968,6 +3387,11 @@ var Materializer = class Materializer {
2968
3387
  * there is no fleet to report, so an unreachable or malformed list is a coded
2969
3388
  * `FETCH` failure.
2970
3389
  *
3390
+ * The vendored-file inventory is the other read a whole call rests on, and it
3391
+ * fails the other way: it fails every row of its call rather than throwing, so
3392
+ * the caller receives one whole dead answer it can replace with one whole
3393
+ * baseline instead of a mixture it would have to reconcile.
3394
+ *
2971
3395
  * Requests are unauthenticated because every fleet repository is public, and
2972
3396
  * they follow no redirect, so a misconfigured or hostile endpoint cannot move a
2973
3397
  * read to another host. Each one is bounded by its endpoint's timeout and by the
@@ -2988,19 +3412,20 @@ var Materializer = class Materializer {
2988
3412
  * ```
2989
3413
  */
2990
3414
  var Upstream = class Upstream {
2991
- static #defaultGuide = "https://raw.githubusercontent.com";
3415
+ static #defaultRepository = "https://raw.githubusercontent.com";
2992
3416
  static #defaultRegistry = "https://registry.npmjs.org";
2993
3417
  static #defaultBranch = "main";
2994
3418
  static #defaultTimeout = 1e4;
2995
3419
  static #defaultConcurrency = 6;
2996
3420
  static #defaultRetries = 0;
2997
3421
  static #scope = "orkestrel";
3422
+ static #vendor = "scaffold";
2998
3423
  static #unreadable = "the answer carries no readable latest version";
2999
3424
  static #packument = "application/vnd.npm.install-v1+json";
3000
3425
  #emitter;
3001
- #guideBase;
3002
- #guideBranch;
3003
- #guideTimeout;
3426
+ #repositoryBase;
3427
+ #repositoryBranch;
3428
+ #repositoryTimeout;
3004
3429
  #registryBase;
3005
3430
  #registryTimeout;
3006
3431
  #concurrency;
@@ -3010,7 +3435,7 @@ var Upstream = class Upstream {
3010
3435
  #controller = new AbortController();
3011
3436
  #destroyed = false;
3012
3437
  /**
3013
- * Construct a reader over one guide host and one registry.
3438
+ * Construct a reader over one raw content host and one registry.
3014
3439
  *
3015
3440
  * @param options - The endpoints, the request bounds, the initial
3016
3441
  * listeners, and the listener-error handler.
@@ -3034,9 +3459,9 @@ var Upstream = class Upstream {
3034
3459
  ...options?.on === void 0 ? {} : { on: options.on },
3035
3460
  ...options?.error === void 0 ? {} : { error: options.error }
3036
3461
  });
3037
- this.#guideBase = this.#endpoint(options?.guides?.base ?? Upstream.#defaultGuide, "guides");
3038
- this.#guideBranch = options?.guides?.branch ?? Upstream.#defaultBranch;
3039
- this.#guideTimeout = options?.guides?.timeout ?? Upstream.#defaultTimeout;
3462
+ this.#repositoryBase = this.#endpoint(options?.repository?.base ?? Upstream.#defaultRepository, "repository");
3463
+ this.#repositoryBranch = options?.repository?.branch ?? Upstream.#defaultBranch;
3464
+ this.#repositoryTimeout = options?.repository?.timeout ?? Upstream.#defaultTimeout;
3040
3465
  this.#registryBase = this.#endpoint(options?.registry?.base ?? Upstream.#defaultRegistry, "registry");
3041
3466
  this.#registryTimeout = options?.registry?.timeout ?? Upstream.#defaultTimeout;
3042
3467
  this.#concurrency = options?.concurrency ?? Upstream.#defaultConcurrency;
@@ -3114,6 +3539,54 @@ var Upstream = class Upstream {
3114
3539
  return this.#gather(accepted, (name) => this.#mirror(name, observed, allowance));
3115
3540
  }
3116
3541
  /**
3542
+ * Read each named vendored file from the repository, beside the target bytes it answers for.
3543
+ *
3544
+ * @param paths - The target-relative vendored paths to read.
3545
+ * @param current - The target files as exact bytes, keyed by the same paths.
3546
+ * @returns One file verdict per path, in input order.
3547
+ * @throws {@link ScaffoldError} coded `INVALID` when `paths` is not a bounded
3548
+ * list of target-relative paths or `current` is not a snapshot, and
3549
+ * `DESTROYED` when the reader is torn down before or during the call.
3550
+ *
3551
+ * @remarks
3552
+ * The committed inventory is read once per call and decides every row, so a
3553
+ * path whose declared digest already matches the target's own bytes is `found`
3554
+ * without a request and the call spends nothing on it. An inventory that
3555
+ * produces no answer fails every row of the call rather than leaving some rows
3556
+ * live and some dead, which is what leaves the caller one whole baseline to
3557
+ * fall back to. A path the inventory does not name is `missing`.
3558
+ *
3559
+ * A fetched response's decoded content is verified against the digest the
3560
+ * inventory declares for that path, before any character decoding. Transport
3561
+ * encoding is transparent and does not enter the comparison, so content that
3562
+ * does not hash to the inventory's claim fails its row rather than reaching a
3563
+ * write. That is integrity against a single committed baseline, not
3564
+ * authenticity: it detects truncated, substituted, or stale content, and it
3565
+ * says nothing about who published the inventory.
3566
+ *
3567
+ * A guide mirror is never answered here whatever the caller asks for and
3568
+ * whatever the target holds, because those bytes belong to `fetch` and to the
3569
+ * mirror verb that writes them.
3570
+ *
3571
+ * @example
3572
+ * ```ts
3573
+ * import { Upstream } from '@orkestrel/scaffold/server'
3574
+ *
3575
+ * const upstream = new Upstream()
3576
+ * await upstream.read(['AGENTS.md'], { 'AGENTS.md': '2320416745' })
3577
+ * upstream.destroy()
3578
+ * ```
3579
+ */
3580
+ async read(paths, current) {
3581
+ this.#assertAlive();
3582
+ const accepted = this.#accept(paths, isPaths, "paths");
3583
+ const observed = this.#accept(current, isSnapshot, "current");
3584
+ if (accepted.length === 0) return [];
3585
+ const allowance = { remaining: this.#budget };
3586
+ const inventory = await this.#inventory(allowance);
3587
+ return this.#gather(accepted, (path) => this.#file(path, inventory, observed, allowance));
3588
+ }
3589
+ /**
3117
3590
  * Catalog the published fleet from the registry's organization package list.
3118
3591
  *
3119
3592
  * @returns One row per published package, sorted by name.
@@ -3190,14 +3663,14 @@ var Upstream = class Upstream {
3190
3663
  return parsed.href.replace(/\/+$/u, "");
3191
3664
  }
3192
3665
  async #release(dependency, allowance) {
3193
- const outcome = await this.#read(this.#registryURL(dependency.name), this.#registryTimeout, allowance, Upstream.#packument);
3666
+ const outcome = await this.#readWithRetries(this.#registryURL(dependency.name), this.#registryTimeout, allowance, Upstream.#packument);
3194
3667
  const latest = outcome.lookup === "found" ? this.#releaseVersion(outcome.content, dependency.range) : void 0;
3195
3668
  const tagged = outcome.lookup === "found" ? this.#latest(outcome.content) : void 0;
3196
3669
  const major = tagged === void 0 ? void 0 : extractVersion(tagged)?.[0];
3197
3670
  const release = latest === void 0 ? {
3198
3671
  name: dependency.name,
3199
3672
  range: dependency.range,
3200
- lookup: outcome.lookup === "missing" ? "missing" : "failed",
3673
+ lookup: outcome.lookup === "found" ? "unmatched" : outcome.lookup,
3201
3674
  note: outcome.lookup === "found" ? Upstream.#unreadable : outcome.note,
3202
3675
  ...major === void 0 ? {} : { major }
3203
3676
  } : {
@@ -3212,7 +3685,7 @@ var Upstream = class Upstream {
3212
3685
  }
3213
3686
  async #mirror(name, current, allowance) {
3214
3687
  const path = nameToGuide(name);
3215
- const outcome = await this.#read(this.#guideURL(name), this.#guideTimeout, allowance);
3688
+ const outcome = await this.#readWithRetries(this.#guideURL(name), this.#repositoryTimeout, allowance);
3216
3689
  const observed = current[path];
3217
3690
  const mirror = outcome.lookup === "found" ? {
3218
3691
  name,
@@ -3230,8 +3703,102 @@ var Upstream = class Upstream {
3230
3703
  this.#emitter.emit("mirror", mirror);
3231
3704
  return mirror;
3232
3705
  }
3706
+ async #file(path, inventory, current, allowance) {
3707
+ const file = await this.#answer(path, inventory, current[path], allowance);
3708
+ this.#emitter.emit("file", file);
3709
+ return file;
3710
+ }
3711
+ async #answer(path, inventory, observed, allowance) {
3712
+ const carried = observed === void 0 ? {} : { observed };
3713
+ if (inventory.lookup !== "found") return {
3714
+ path,
3715
+ lookup: inventory.lookup,
3716
+ note: inventory.note,
3717
+ ...carried
3718
+ };
3719
+ if (inferGroup(path) === "guides") return {
3720
+ path,
3721
+ lookup: "missing",
3722
+ note: `${path} is a guide mirror the fleet serves`,
3723
+ ...carried
3724
+ };
3725
+ if (inventory.duplicates.has(path)) return {
3726
+ path,
3727
+ lookup: "failed",
3728
+ note: `the inventory names ${path} more than once`,
3729
+ ...carried
3730
+ };
3731
+ const digest = inventory.digests.get(path);
3732
+ if (digest === void 0) return {
3733
+ path,
3734
+ lookup: "missing",
3735
+ note: `the inventory does not name ${path}`,
3736
+ ...carried
3737
+ };
3738
+ if (observed !== void 0 && hexToDigest(observed) === digest) return {
3739
+ path,
3740
+ lookup: "found",
3741
+ hex: observed,
3742
+ ...carried
3743
+ };
3744
+ const outcome = await this.#readWithRetries(this.#vendorURL(path), this.#repositoryTimeout, allowance, void 0, true);
3745
+ if (outcome.lookup !== "found") return {
3746
+ path,
3747
+ lookup: outcome.lookup,
3748
+ note: outcome.note,
3749
+ ...carried
3750
+ };
3751
+ if (hexToDigest(outcome.hex) !== digest) return {
3752
+ path,
3753
+ lookup: "failed",
3754
+ note: `the bytes served for ${path} do not match the digest the inventory declares`,
3755
+ ...carried
3756
+ };
3757
+ return {
3758
+ path,
3759
+ lookup: "found",
3760
+ hex: outcome.hex,
3761
+ ...carried
3762
+ };
3763
+ }
3764
+ async #inventory(allowance) {
3765
+ const url = this.#vendorURL(HOST_INVENTORY_PATH);
3766
+ const empty = {
3767
+ digests: /* @__PURE__ */ new Map(),
3768
+ duplicates: /* @__PURE__ */ new Set()
3769
+ };
3770
+ const outcome = await this.#readWithRetries(url, this.#repositoryTimeout, allowance);
3771
+ if (outcome.lookup !== "found") return {
3772
+ ...empty,
3773
+ lookup: outcome.lookup,
3774
+ note: outcome.lookup === "missing" ? `the vendored inventory at ${url} is not published there` : `the vendored inventory at ${url} produced no answer: ${outcome.note}`
3775
+ };
3776
+ const manifest = parseJSONAs(outcome.content, isHostManifest);
3777
+ if (manifest === void 0) return {
3778
+ ...empty,
3779
+ lookup: "failed",
3780
+ note: `the vendored inventory at ${url} is not a readable manifest`
3781
+ };
3782
+ if (manifest.digest !== computeManifestDigest(manifest.entries, manifest.roots)) return {
3783
+ ...empty,
3784
+ lookup: "failed",
3785
+ note: `the vendored inventory at ${url} does not match its own membership digest`
3786
+ };
3787
+ const digests = /* @__PURE__ */ new Map();
3788
+ const duplicates = /* @__PURE__ */ new Set();
3789
+ for (const entry of manifest.entries) {
3790
+ if (digests.has(entry.destination)) duplicates.add(entry.destination);
3791
+ digests.set(entry.destination, entry.digest);
3792
+ }
3793
+ return {
3794
+ lookup: "found",
3795
+ digests,
3796
+ duplicates,
3797
+ note: ""
3798
+ };
3799
+ }
3233
3800
  async #entry(name, allowance) {
3234
- const outcome = await this.#read(this.#registryURL(name), this.#registryTimeout, allowance, Upstream.#packument);
3801
+ const outcome = await this.#readWithRetries(this.#registryURL(name), this.#registryTimeout, allowance, Upstream.#packument);
3235
3802
  const version = outcome.lookup === "found" ? this.#latest(outcome.content) : void 0;
3236
3803
  if (version !== void 0) return {
3237
3804
  name,
@@ -3241,7 +3808,7 @@ var Upstream = class Upstream {
3241
3808
  };
3242
3809
  return {
3243
3810
  name,
3244
- lookup: outcome.lookup === "missing" ? "missing" : "failed",
3811
+ lookup: outcome.lookup === "found" ? "unmatched" : outcome.lookup,
3245
3812
  note: outcome.lookup === "found" ? Upstream.#unreadable : outcome.note
3246
3813
  };
3247
3814
  }
@@ -3269,7 +3836,7 @@ var Upstream = class Upstream {
3269
3836
  }
3270
3837
  async #packages(allowance) {
3271
3838
  const url = `${this.#registryBase}/-/org/${Upstream.#scope}/package`;
3272
- const outcome = await this.#read(url, this.#registryTimeout, allowance);
3839
+ const outcome = await this.#readWithRetries(url, this.#registryTimeout, allowance);
3273
3840
  if (outcome.lookup !== "found") throw this.#error("FETCH", `The organization package list at ${url} produced no answer.`, {
3274
3841
  url,
3275
3842
  note: outcome.note
@@ -3315,15 +3882,22 @@ var Upstream = class Upstream {
3315
3882
  return `${this.#registryBase}/${encodeURIComponent(name).replaceAll("%40", "@")}`;
3316
3883
  }
3317
3884
  #guideURL(name) {
3318
- const branch = this.#guideBranch.split("/").map((segment) => encodeURIComponent(segment)).join("/");
3885
+ const branch = this.#encode(this.#repositoryBranch);
3319
3886
  const repository = encodeURIComponent(name.slice(name.lastIndexOf("/") + 1));
3320
- return `${this.#guideBase}/${Upstream.#scope}/${repository}/refs/heads/${branch}/${nameToGuide(name)}`;
3887
+ return `${this.#repositoryBase}/${Upstream.#scope}/${repository}/refs/heads/${branch}/${nameToGuide(name)}`;
3888
+ }
3889
+ #vendorURL(path) {
3890
+ const branch = this.#encode(this.#repositoryBranch);
3891
+ return `${this.#repositoryBase}/${Upstream.#scope}/${Upstream.#vendor}/refs/heads/${branch}/${this.#encode(path)}`;
3321
3892
  }
3322
- async #read(url, timeout, allowance, accept) {
3893
+ #encode(path) {
3894
+ return path.split("/").map((segment) => encodeURIComponent(segment)).join("/");
3895
+ }
3896
+ async #readWithRetries(url, timeout, allowance, accept, binary = false) {
3323
3897
  let note = "";
3324
- for (let attempt = 0; attempt <= this.#retries; attempt += 1) {
3898
+ for (let round = 0; round <= this.#retries; round += 1) {
3325
3899
  this.#assertAlive();
3326
- const outcome = await this.#request(url, timeout, allowance, accept);
3900
+ const outcome = binary ? await this.#request(url, timeout, allowance, accept, true) : await this.#request(url, timeout, allowance, accept);
3327
3901
  if (outcome.lookup !== "failed") return outcome;
3328
3902
  note = outcome.note;
3329
3903
  }
@@ -3331,18 +3905,29 @@ var Upstream = class Upstream {
3331
3905
  url,
3332
3906
  note
3333
3907
  });
3334
- return {
3908
+ return binary ? {
3335
3909
  lookup: "failed",
3336
- content: "",
3910
+ hex: "",
3337
3911
  note
3338
- };
3339
- }
3340
- async #request(url, timeout, allowance, accept) {
3341
- if (allowance.remaining <= 0) return {
3912
+ } : {
3342
3913
  lookup: "failed",
3343
3914
  content: "",
3344
- note: `the call spent its ${String(this.#budget)}-byte allowance`
3915
+ note
3345
3916
  };
3917
+ }
3918
+ async #request(url, timeout, allowance, accept, binary = false) {
3919
+ if (allowance.remaining <= 0) {
3920
+ const note = `the call spent its ${String(this.#budget)}-byte allowance`;
3921
+ return binary ? {
3922
+ lookup: "failed",
3923
+ hex: "",
3924
+ note
3925
+ } : {
3926
+ lookup: "failed",
3927
+ content: "",
3928
+ note
3929
+ };
3930
+ }
3346
3931
  try {
3347
3932
  const response = await fetch(url, {
3348
3933
  signal: AbortSignal.any([this.#controller.signal, AbortSignal.timeout(timeout)]),
@@ -3351,7 +3936,11 @@ var Upstream = class Upstream {
3351
3936
  });
3352
3937
  if (response.status === 404) {
3353
3938
  await response.body?.cancel();
3354
- return {
3939
+ return binary ? {
3940
+ lookup: "missing",
3941
+ hex: "",
3942
+ note: "HTTP 404"
3943
+ } : {
3355
3944
  lookup: "missing",
3356
3945
  content: "",
3357
3946
  note: "HTTP 404"
@@ -3359,39 +3948,61 @@ var Upstream = class Upstream {
3359
3948
  }
3360
3949
  if (response.status >= 300 && response.status < 400) {
3361
3950
  await response.body?.cancel();
3362
- return {
3951
+ const note = `HTTP ${String(response.status)}, and a redirect is never followed`;
3952
+ return binary ? {
3953
+ lookup: "failed",
3954
+ hex: "",
3955
+ note
3956
+ } : {
3363
3957
  lookup: "failed",
3364
3958
  content: "",
3365
- note: `HTTP ${String(response.status)}, and a redirect is never followed`
3959
+ note
3366
3960
  };
3367
3961
  }
3368
3962
  if (!response.ok) {
3369
3963
  await response.body?.cancel();
3370
- return {
3964
+ const note = `HTTP ${String(response.status)}`;
3965
+ return binary ? {
3966
+ lookup: "failed",
3967
+ hex: "",
3968
+ note
3969
+ } : {
3371
3970
  lookup: "failed",
3372
3971
  content: "",
3373
- note: `HTTP ${String(response.status)}`
3972
+ note
3374
3973
  };
3375
3974
  }
3376
- return await this.#body(response, allowance);
3975
+ return binary ? await this.#body(response, allowance, true) : await this.#body(response, allowance);
3377
3976
  } catch (error) {
3378
3977
  this.#assertAlive();
3379
- return {
3978
+ const note = this.#note(error);
3979
+ return binary ? {
3980
+ lookup: "failed",
3981
+ hex: "",
3982
+ note
3983
+ } : {
3380
3984
  lookup: "failed",
3381
3985
  content: "",
3382
- note: this.#note(error)
3986
+ note
3383
3987
  };
3384
3988
  }
3385
3989
  }
3386
- async #body(response, allowance) {
3990
+ async #body(response, allowance, binary = false) {
3387
3991
  const body = response.body;
3388
- if (body === null) return {
3389
- lookup: "failed",
3390
- content: "",
3391
- note: `HTTP ${String(response.status)}, and the answer carries no body`
3392
- };
3992
+ if (body === null) {
3993
+ const note = `HTTP ${String(response.status)}, and the answer carries no body`;
3994
+ return binary ? {
3995
+ lookup: "failed",
3996
+ hex: "",
3997
+ note
3998
+ } : {
3999
+ lookup: "failed",
4000
+ content: "",
4001
+ note
4002
+ };
4003
+ }
3393
4004
  const reader = body.getReader();
3394
- const decoder = new TextDecoder("utf-8", { fatal: true });
4005
+ const decoder = binary ? void 0 : new TextDecoder("utf-8", { fatal: true });
3395
4006
  const chunks = [];
3396
4007
  let total = 0;
3397
4008
  try {
@@ -3402,30 +4013,44 @@ var Upstream = class Upstream {
3402
4013
  allowance.remaining -= chunk.value.byteLength;
3403
4014
  if (total > this.#limit) {
3404
4015
  await reader.cancel();
3405
- return {
4016
+ const note = `the response passed the ${String(this.#limit)}-byte response limit`;
4017
+ return binary ? {
4018
+ lookup: "failed",
4019
+ hex: "",
4020
+ note
4021
+ } : {
3406
4022
  lookup: "failed",
3407
4023
  content: "",
3408
- note: `the response passed the ${String(this.#limit)}-byte response limit`
4024
+ note
3409
4025
  };
3410
4026
  }
3411
4027
  if (allowance.remaining < 0) {
3412
4028
  await reader.cancel();
3413
- return {
4029
+ const note = `the call spent its ${String(this.#budget)}-byte allowance`;
4030
+ return binary ? {
4031
+ lookup: "failed",
4032
+ hex: "",
4033
+ note
4034
+ } : {
3414
4035
  lookup: "failed",
3415
4036
  content: "",
3416
- note: `the call spent its ${String(this.#budget)}-byte allowance`
4037
+ note
3417
4038
  };
3418
4039
  }
3419
- chunks.push(decoder.decode(chunk.value, { stream: true }));
4040
+ chunks.push(decoder === void 0 ? Buffer.from(chunk.value).toString("hex") : decoder.decode(chunk.value, { stream: true }));
3420
4041
  }
3421
- chunks.push(decoder.decode());
4042
+ if (decoder !== void 0) chunks.push(decoder.decode());
3422
4043
  } catch (error) {
3423
4044
  await reader.cancel().catch(() => void 0);
3424
4045
  throw error;
3425
4046
  } finally {
3426
4047
  reader.releaseLock();
3427
4048
  }
3428
- return {
4049
+ return binary ? {
4050
+ lookup: "found",
4051
+ hex: chunks.join(""),
4052
+ note: ""
4053
+ } : {
3429
4054
  lookup: "found",
3430
4055
  content: chunks.join(""),
3431
4056
  note: ""
@@ -3480,6 +4105,6 @@ var Upstream = class Upstream {
3480
4105
  }
3481
4106
  };
3482
4107
  //#endregion
3483
- export { BRANCH_PATTERN, DIGEST_PATTERN, DRIVE_PATTERN, INVALID_SEGMENT_CHARACTER_PATTERN, MANIFEST_NAME, MAX_BRANCH_LENGTH, MAX_ENDPOINT_LENGTH, MAX_INVENTORY_PATHS, MAX_PATH_DEPTH, MAX_PATH_SEGMENT_BYTES, MAX_UPSTREAM_CONCURRENCY, MAX_UPSTREAM_RETRIES, MAX_UPSTREAM_TIMEOUT, Materializer, RESERVED_SEGMENT_PATTERN, Upstream, WriteTransaction, computeDigest, computeFileDigest, computeManifestDigest, isBranch, isCatalogEntries, isDependencies, isDependencyNames, isDigest, isEndpoint, isExactCaseFile, isFilesystemPath, isHostManifest, isInventory, isManifestEntry, isMaterializerHooks, isMaterializerOptions, isMirrors, isPhysicalDirectory, isPhysicalFile, isRepository, isTimeout, isUpstreamHooks, isUpstreamOptions, isVacant, listDirectories, listFiles, matchesAnchor, matchesExecutablePath, matchesExpectation, matchesGitPath, matchesMissingPath, matchesPrecondition, matchesProtectedPath, matchesSensitivePath, pathToStorage, readAnchor, readExpectation, readFileHex, readFileText, readHostManifest, readManifestEntry, readSnapshot, resolveContainedPath, resolveRealPath, stageHost };
4108
+ export { BRANCH_PATTERN, DIGEST_PATTERN, DRIVE_PATTERN, INVALID_SEGMENT_CHARACTER_PATTERN, MANIFEST_NAME, MAX_BRANCH_LENGTH, MAX_ENDPOINT_LENGTH, MAX_INVENTORY_PATHS, MAX_PATH_DEPTH, MAX_PATH_SEGMENT_BYTES, MAX_UPSTREAM_CONCURRENCY, MAX_UPSTREAM_RETRIES, MAX_UPSTREAM_TIMEOUT, Materializer, RESERVED_SEGMENT_PATTERN, Upstream, WriteTransaction, computeDigest, computeFileDigest, computeManifestDigest, filesToHost, hexToDigest, isBranch, isCatalogEntries, isDependencies, isDependencyNames, isDigest, isEndpoint, isExactCaseFile, isFilesystemPath, isHost, isHostManifest, isInventory, isManifestEntry, isMaterializerHooks, isMaterializerOptions, isMirrors, isPaths, isPhysicalDirectory, isPhysicalFile, isTimeout, isUpstreamHooks, isUpstreamOptions, isVacant, isWorktree, listDirectories, listFiles, matchesAnchor, matchesExecutablePath, matchesExpectation, matchesGitPath, matchesMissingPath, matchesPrecondition, matchesProtectedPath, matchesSensitivePath, pathToStorage, readAnchor, readExpectation, readFileHex, readFileText, readHostFloor, readHostManifest, readManifestEntry, readSnapshot, resolveContainedPath, resolveRealPath, stageBytes, stageHost, stageInventory };
3484
4109
 
3485
4110
  //# sourceMappingURL=index.js.map