@aiwg/cli 2026.8.4 → 2026.8.5

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.
@@ -49,6 +49,13 @@ export interface WebReleaseOptions {
49
49
  credentialProvider?: () => Promise<string | null>;
50
50
  /** Test/development escape hatch. HTTP remains restricted to loopback. */
51
51
  allowInsecureLoopbackHttp?: boolean;
52
+ /** Structured cache diagnostics; never includes URLs, headers, or credentials. */
53
+ onDiagnostic?: (diagnostic: WebReleaseDiagnostic) => void;
54
+ }
55
+ export interface WebReleaseDiagnostic {
56
+ resource: "channel" | "version-index";
57
+ outcome: "conditional-hit" | "revalidated" | "unconditional";
58
+ validator: "etag" | "last-modified" | "none";
52
59
  }
53
60
  export interface VerifiedReleaseDescriptor {
54
61
  path: string;
@@ -386,6 +386,69 @@ async function fetchBytes(fetcher, url, label, maxBytes, bearerToken) {
386
386
  clearTimeout(timeout);
387
387
  }
388
388
  }
389
+ function validatorFromResponse(response, payloadSha256) {
390
+ // Preserve the origin's ETag octets, including the W/ prefix for weak tags.
391
+ // HTTP validators only suppress transfer; Ed25519 and SHA-256 remain the
392
+ // authority for every cached representation accepted by this module.
393
+ const etag = response.headers.get("etag")?.trim();
394
+ const lastModified = response.headers.get("last-modified")?.trim();
395
+ if (!etag && !lastModified)
396
+ return undefined;
397
+ return {
398
+ schemaVersion: "aiwg.http-validator/v1",
399
+ payloadSha256,
400
+ ...(etag ? { etag } : { lastModified: lastModified }),
401
+ };
402
+ }
403
+ function readMetadataValidator(pathname, payloadSha256) {
404
+ if (!fs.existsSync(pathname))
405
+ return undefined;
406
+ const value = parseJson(readVerifiedRegularFile(pathname, {
407
+ label: "cached HTTP metadata validator",
408
+ maxBytes: MAX_COMPLETION_MARKER_BYTES,
409
+ }), "cached HTTP metadata validator");
410
+ if (!isRecord(value) || value.schemaVersion !== "aiwg.http-validator/v1" || value.payloadSha256 !== payloadSha256) {
411
+ throw new Error("cached HTTP metadata validator does not match the verified signed payload");
412
+ }
413
+ const etag = typeof value.etag === "string" && value.etag.trim() ? value.etag.trim() : undefined;
414
+ const lastModified = typeof value.lastModified === "string" && value.lastModified.trim() ? value.lastModified.trim() : undefined;
415
+ if (!etag && !lastModified)
416
+ throw new Error("cached HTTP metadata validator is empty");
417
+ return { schemaVersion: "aiwg.http-validator/v1", payloadSha256, ...(etag ? { etag } : { lastModified }) };
418
+ }
419
+ async function fetchMetadata(fetcher, url, label, maxBytes, cachedValidator) {
420
+ const headers = { "accept-encoding": "identity" };
421
+ if (cachedValidator?.etag)
422
+ headers["if-none-match"] = cachedValidator.etag;
423
+ else if (cachedValidator?.lastModified)
424
+ headers["if-modified-since"] = cachedValidator.lastModified;
425
+ const controller = new AbortController();
426
+ const timeout = setTimeout(() => controller.abort(), RESOURCE_FETCH_TIMEOUT_MS);
427
+ let response;
428
+ try {
429
+ response = await fetcher(url, { redirect: "error", headers, signal: controller.signal });
430
+ }
431
+ catch (error) {
432
+ if (controller.signal.aborted)
433
+ throw new Error(`${label} request timed out after ${RESOURCE_FETCH_TIMEOUT_MS}ms`);
434
+ throw error;
435
+ }
436
+ finally {
437
+ clearTimeout(timeout);
438
+ }
439
+ if (response.status === 304) {
440
+ if (!cachedValidator)
441
+ throw new Error(`${label} returned 304 without a verified cached representation`);
442
+ return {
443
+ notModified: true,
444
+ validator: validatorFromResponse(response, cachedValidator.payloadSha256) ?? cachedValidator,
445
+ };
446
+ }
447
+ if (!response.ok)
448
+ throw new Error(`${label} fetch failed (${response.status}): ${url}`);
449
+ const bytes = await fetchBytes(async () => response, url, label, maxBytes);
450
+ return { notModified: false, bytes, validator: validatorFromResponse(response, sha256(bytes)) };
451
+ }
389
452
  function verifyDescriptor(bytes, descriptor, label = descriptor.path) {
390
453
  if (bytes.byteLength !== descriptor.size || sha256(bytes) !== descriptor.sha256) {
391
454
  throw new Error(`release descriptor size or digest verification failed: ${label}`);
@@ -623,7 +686,14 @@ function readCachedChannel(cacheRoot, channel, publicKeyPem) {
623
686
  if (candidate.name !== `${manifest.sequence}-${digest}`) {
624
687
  throw new Error(`cached channel ${channel} generation name does not match its signed metadata`);
625
688
  }
626
- valid.push({ manifest, bytes, signatureBytes, digest });
689
+ let validator;
690
+ try {
691
+ validator = readMetadataValidator(path.join(dir, "http-validator.json"), digest);
692
+ }
693
+ catch {
694
+ validator = undefined;
695
+ }
696
+ valid.push({ manifest, bytes, signatureBytes, digest, validator });
627
697
  }
628
698
  catch {
629
699
  corrupt = true;
@@ -658,10 +728,23 @@ function readCachedVersionIndex(cacheRoot, publicKeyPem) {
658
728
  label: "cached resource version index signature",
659
729
  maxBytes: MAX_SIGNATURE_BYTES,
660
730
  });
661
- verifySignedResourceBytes(bytes, signatureBytes, publicKeyPem, "cached resource version index");
662
- return validateVersionIndex(parseJson(bytes, "cached resource version index"));
731
+ const digest = verifySignedResourceBytes(bytes, signatureBytes, publicKeyPem, "cached resource version index");
732
+ return {
733
+ index: validateVersionIndex(parseJson(bytes, "cached resource version index")),
734
+ bytes,
735
+ signatureBytes,
736
+ digest,
737
+ validator: (() => {
738
+ try {
739
+ return readMetadataValidator(path.join(dir, "http-validator.json"), digest);
740
+ }
741
+ catch {
742
+ return undefined;
743
+ }
744
+ })(),
745
+ };
663
746
  }
664
- function cacheVersionIndex(cacheRoot, bytes, signatureBytes) {
747
+ function cacheVersionIndex(cacheRoot, bytes, signatureBytes, validator) {
665
748
  const target = versionIndexCacheDir(cacheRoot);
666
749
  const stagingRoot = path.join(cacheRoot, ".staging", "versions");
667
750
  fs.mkdirSync(stagingRoot, { recursive: true });
@@ -669,6 +752,8 @@ function cacheVersionIndex(cacheRoot, bytes, signatureBytes) {
669
752
  try {
670
753
  fs.writeFileSync(path.join(stage, "versions.json"), bytes, { flag: "wx" });
671
754
  fs.writeFileSync(path.join(stage, "versions.sig"), signatureBytes, { flag: "wx" });
755
+ if (validator)
756
+ fs.writeFileSync(path.join(stage, "http-validator.json"), `${JSON.stringify(validator)}\n`, { flag: "wx" });
672
757
  if (fs.existsSync(target))
673
758
  fs.rmSync(target, { recursive: true, force: true });
674
759
  installGeneration(stage, target);
@@ -678,19 +763,33 @@ function cacheVersionIndex(cacheRoot, bytes, signatureBytes) {
678
763
  throw error;
679
764
  }
680
765
  }
681
- async function fetchAndCacheVersionIndex(base, fetcher, cacheRoot, publicKeyPem) {
682
- const indexBytes = await fetchBytes(fetcher, resourceUrl(base, "resources/versions.json"), "resource version index", MAX_VERSION_INDEX_BYTES);
766
+ async function fetchAndCacheVersionIndex(base, fetcher, cacheRoot, publicKeyPem, diagnostic) {
767
+ let cached = null;
768
+ try {
769
+ cached = readCachedVersionIndex(cacheRoot, publicKeyPem);
770
+ }
771
+ catch {
772
+ cached = null;
773
+ }
774
+ const fetched = await fetchMetadata(fetcher, resourceUrl(base, "resources/versions.json"), "resource version index", MAX_VERSION_INDEX_BYTES, cached?.validator);
775
+ if (fetched.notModified) {
776
+ cacheVersionIndex(cacheRoot, cached.bytes, cached.signatureBytes, fetched.validator);
777
+ diagnostic?.({ resource: "version-index", outcome: "conditional-hit", validator: cached.validator.etag ? "etag" : "last-modified" });
778
+ return cached.index;
779
+ }
780
+ const indexBytes = fetched.bytes;
683
781
  const signatureBytes = await fetchBytes(fetcher, resourceUrl(base, "resources/versions.sig"), "resource version index signature", MAX_SIGNATURE_BYTES);
684
782
  verifySignedResourceBytes(indexBytes, signatureBytes, publicKeyPem, "resource version index");
685
783
  const index = validateVersionIndex(parseJson(indexBytes, "resource version index"));
686
- cacheVersionIndex(cacheRoot, indexBytes, signatureBytes);
784
+ cacheVersionIndex(cacheRoot, indexBytes, signatureBytes, fetched.validator);
785
+ diagnostic?.({ resource: "version-index", outcome: cached?.validator ? "revalidated" : "unconditional", validator: fetched.validator?.etag ? "etag" : fetched.validator?.lastModified ? "last-modified" : "none" });
687
786
  return index;
688
787
  }
689
- async function resolveVersionIndex(base, fetcher, cacheRoot, publicKeyPem, offline) {
788
+ async function resolveVersionIndex(base, fetcher, cacheRoot, publicKeyPem, offline, diagnostic) {
690
789
  if (offline) {
691
790
  const cached = readCachedVersionIndex(cacheRoot, publicKeyPem);
692
791
  if (cached)
693
- return cached;
792
+ return cached.index;
694
793
  const versions = cachedReleaseVersions(cacheRoot).flatMap((version) => cachedDigests(cacheRoot, version).map((digest) => ({
695
794
  version,
696
795
  releaseManifest: `/resources/${version}/manifest.json`,
@@ -703,12 +802,14 @@ async function resolveVersionIndex(base, fetcher, cacheRoot, publicKeyPem, offli
703
802
  if (!fetcher)
704
803
  throw new Error("No fetch implementation is available for AIWG web resources");
705
804
  try {
706
- return await fetchAndCacheVersionIndex(base, fetcher, cacheRoot, publicKeyPem);
805
+ return await fetchAndCacheVersionIndex(base, fetcher, cacheRoot, publicKeyPem, diagnostic);
707
806
  }
708
807
  catch (error) {
808
+ if (error instanceof Error && /fetch failed \((?:401|403|429|5\d\d)\)/.test(error.message))
809
+ throw error;
709
810
  const cached = readCachedVersionIndex(cacheRoot, publicKeyPem);
710
811
  if (cached)
711
- return cached;
812
+ return cached.index;
712
813
  throw error;
713
814
  }
714
815
  }
@@ -776,7 +877,7 @@ function fsyncTree(root) {
776
877
  }
777
878
  fsyncDirectory(root);
778
879
  }
779
- function cacheChannel(cacheRoot, manifest, bytes, signatureBytes, digest) {
880
+ function cacheChannel(cacheRoot, manifest, bytes, signatureBytes, digest, validator) {
780
881
  const root = channelGenerationRoot(cacheRoot, manifest.channel);
781
882
  const stagingRoot = path.join(cacheRoot, ".staging", "channels");
782
883
  fs.mkdirSync(stagingRoot, { recursive: true });
@@ -784,6 +885,8 @@ function cacheChannel(cacheRoot, manifest, bytes, signatureBytes, digest) {
784
885
  try {
785
886
  fs.writeFileSync(path.join(stage, "channel.json"), bytes, { flag: "wx" });
786
887
  fs.writeFileSync(path.join(stage, "channel.sig"), signatureBytes, { flag: "wx" });
888
+ if (validator)
889
+ fs.writeFileSync(path.join(stage, "http-validator.json"), `${JSON.stringify(validator)}\n`, { flag: "wx" });
787
890
  const target = path.join(root, `${manifest.sequence}-${digest}`);
788
891
  if (fs.existsSync(target)) {
789
892
  let matches = false;
@@ -797,7 +900,11 @@ function cacheChannel(cacheRoot, manifest, bytes, signatureBytes, digest) {
797
900
  readVerifiedRegularFile(path.join(target, "channel.sig"), {
798
901
  label: `cached channel ${manifest.channel} signature`,
799
902
  maxBytes: MAX_SIGNATURE_BYTES,
800
- }).equals(Buffer.from(signatureBytes));
903
+ }).equals(Buffer.from(signatureBytes)) &&
904
+ (validator
905
+ ? readMetadataValidator(path.join(target, "http-validator.json"), digest)?.etag === validator.etag &&
906
+ readMetadataValidator(path.join(target, "http-validator.json"), digest)?.lastModified === validator.lastModified
907
+ : !fs.existsSync(path.join(target, "http-validator.json")));
801
908
  }
802
909
  catch {
803
910
  matches = false;
@@ -921,8 +1028,20 @@ export async function resolveWebRelease(options = {}) {
921
1028
  return fetchAndCacheRelease(base, fetcher, cacheRoot, selector, selector.value, publicKeyPem);
922
1029
  }
923
1030
  if (selector.kind === "range" || selector.kind === "digest") {
1031
+ if (!options.offline && selector.kind === "digest") {
1032
+ for (const version of cachedReleaseVersions(cacheRoot)) {
1033
+ if (!cachedDigests(cacheRoot, version).includes(selector.digest))
1034
+ continue;
1035
+ try {
1036
+ return verifyCachedGeneration(cacheRoot, version, selector.digest, selector, publicKeyPem, base, selector.digest);
1037
+ }
1038
+ catch {
1039
+ // A corrupt immutable generation cannot bypass signed index resolution.
1040
+ }
1041
+ }
1042
+ }
924
1043
  const fetcher = authorize;
925
- const index = await resolveVersionIndex(base, fetcher, cacheRoot, publicKeyPem, options.offline);
1044
+ const index = await resolveVersionIndex(base, fetcher, cacheRoot, publicKeyPem, options.offline, options.onDiagnostic);
926
1045
  const selected = selectVersionFromIndex(index, selector);
927
1046
  if (options.offline) {
928
1047
  return resolveOfflineExact(cacheRoot, selector, selected.version, publicKeyPem, base, selected.releaseManifestSha256);
@@ -941,11 +1060,23 @@ export async function resolveWebRelease(options = {}) {
941
1060
  if (!fetcher)
942
1061
  throw new Error("No fetch implementation is available for AIWG web resources");
943
1062
  const channelPrefix = `resources/channels/${selector.value}`;
944
- const channelBytes = await fetchBytes(fetcher, resourceUrl(base, `${channelPrefix}.json`), `channel ${selector.value}`, MAX_SIGNED_METADATA_BYTES);
1063
+ let prior = null;
1064
+ try {
1065
+ prior = readCachedChannel(cacheRoot, selector.value, publicKeyPem);
1066
+ }
1067
+ catch {
1068
+ prior = null;
1069
+ }
1070
+ const fetched = await fetchMetadata(fetcher, resourceUrl(base, `${channelPrefix}.json`), `channel ${selector.value}`, MAX_SIGNED_METADATA_BYTES, prior?.validator);
1071
+ if (fetched.notModified) {
1072
+ cacheChannel(cacheRoot, prior.manifest, prior.bytes, prior.signatureBytes, prior.digest, fetched.validator);
1073
+ options.onDiagnostic?.({ resource: "channel", outcome: "conditional-hit", validator: prior.validator.etag ? "etag" : "last-modified" });
1074
+ return fetchAndCacheRelease(base, fetcher, cacheRoot, selector, prior.manifest.version, publicKeyPem, prior.manifest.releaseManifestSha256, prior.manifest.sequence);
1075
+ }
1076
+ const channelBytes = fetched.bytes;
945
1077
  const channelSignatureBytes = await fetchBytes(fetcher, resourceUrl(base, `${channelPrefix}.sig`), `channel ${selector.value} signature`, MAX_SIGNATURE_BYTES);
946
1078
  const channelDigest = verifySignedResourceBytes(channelBytes, channelSignatureBytes, publicKeyPem, `channel ${selector.value}`);
947
1079
  const channel = validateChannelManifest(parseJson(channelBytes, `channel ${selector.value}`), selector.value);
948
- const prior = readCachedChannel(cacheRoot, selector.value, publicKeyPem);
949
1080
  if (prior && channel.sequence < prior.manifest.sequence) {
950
1081
  throw new Error(`channel ${selector.value} sequence rollback detected (${channel.sequence} < ${prior.manifest.sequence})`);
951
1082
  }
@@ -957,7 +1088,8 @@ export async function resolveWebRelease(options = {}) {
957
1088
  throw new Error(`channel ${selector.value} sequence ${channel.sequence} has conflicting signed metadata`);
958
1089
  }
959
1090
  const release = await fetchAndCacheRelease(base, fetcher, cacheRoot, selector, channel.version, publicKeyPem, channel.releaseManifestSha256, channel.sequence);
960
- cacheChannel(cacheRoot, channel, channelBytes, channelSignatureBytes, channelDigest);
1091
+ cacheChannel(cacheRoot, channel, channelBytes, channelSignatureBytes, channelDigest, fetched.validator);
1092
+ options.onDiagnostic?.({ resource: "channel", outcome: prior?.validator ? "revalidated" : "unconditional", validator: fetched.validator?.etag ? "etag" : fetched.validator?.lastModified ? "last-modified" : "none" });
961
1093
  return release;
962
1094
  }
963
1095
  export async function fetchVerifiedRawResource(release, resourcePath, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiwg/cli",
3
- "version": "2026.8.4",
3
+ "version": "2026.8.5",
4
4
  "description": "Lightweight AIWG CLI for signed, versioned web-backed resources.",
5
5
  "type": "module",
6
6
  "license": "MIT",