@adcp/sdk 17.0.0-beta.30 → 17.0.0-beta.32

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.
Files changed (38) hide show
  1. package/dist/lib/identity-3.3/trust.js +10 -4
  2. package/dist/lib/identity-3.3/trust.mjs +10 -4
  3. package/dist/lib/server/create-adcp-server.js +7 -8
  4. package/dist/lib/server/create-adcp-server.mjs +9 -10
  5. package/dist/lib/server/error-arm-tools.d.mts +23 -14
  6. package/dist/lib/server/error-arm-tools.d.ts +23 -14
  7. package/dist/lib/server/error-arm-tools.js +82 -34
  8. package/dist/lib/server/error-arm-tools.mjs +81 -34
  9. package/dist/lib/testing/storyboard/compliance.js +53 -24
  10. package/dist/lib/testing/storyboard/compliance.mjs +53 -24
  11. package/dist/lib/testing/storyboard/context.js +14 -10
  12. package/dist/lib/testing/storyboard/context.mjs +14 -10
  13. package/dist/lib/testing/storyboard/loader.js +2 -2
  14. package/dist/lib/testing/storyboard/loader.mjs +2 -2
  15. package/dist/lib/testing/storyboard/validations.d.mts +1 -1
  16. package/dist/lib/testing/storyboard/validations.d.ts +1 -1
  17. package/dist/lib/testing/storyboard/yaml-cache.d.mts +21 -0
  18. package/dist/lib/testing/storyboard/yaml-cache.d.ts +21 -0
  19. package/dist/lib/testing/storyboard/yaml-cache.js +168 -0
  20. package/dist/lib/testing/storyboard/yaml-cache.mjs +139 -0
  21. package/dist/lib/validation/schema-diagnostic-origins.js +55 -25
  22. package/dist/lib/validation/schema-diagnostic-origins.mjs +55 -25
  23. package/dist/lib/validation/schema-loader.js +499 -44
  24. package/dist/lib/validation/schema-loader.mjs +499 -46
  25. package/dist/lib/validation/schema-validator.js +4 -6
  26. package/dist/lib/validation/schema-validator.mjs +6 -7
  27. package/dist/lib/validation/validator-cache.d.mts +45 -0
  28. package/dist/lib/validation/validator-cache.d.ts +45 -0
  29. package/dist/lib/validation/validator-cache.js +249 -0
  30. package/dist/lib/validation/validator-cache.mjs +227 -0
  31. package/dist/lib/version.d.mts +3 -3
  32. package/dist/lib/version.d.ts +3 -3
  33. package/dist/lib/version.js +3 -3
  34. package/dist/lib/version.mjs +3 -3
  35. package/docs/TYPE-SUMMARY.md +2 -2
  36. package/docs/llms.txt +2 -2
  37. package/docs/migration-14.x-rc-worksheet.md +4 -4
  38. package/package.json +1 -1
@@ -6,10 +6,12 @@ const require2 = __adcpCreateRequire(import.meta.url);
6
6
  import Ajv from "ajv";
7
7
  import addFormats from "ajv-formats";
8
8
  import { AsyncLocalStorage } from "async_hooks";
9
- import { readdirSync, readFileSync, existsSync } from "fs";
9
+ import { readdirSync, readFileSync, existsSync, statSync } from "fs";
10
10
  import path from "path";
11
+ import { createRequire } from "module";
12
+ import { createHash } from "node:crypto";
11
13
  import { isDeepStrictEqual } from "node:util";
12
- import { ADCP_VERSION } from "../version.mjs";
14
+ import { ADCP_VERSION, LIBRARY_VERSION } from "../version.mjs";
13
15
  import { ConfigurationError } from "../errors/index.mjs";
14
16
  import { bindEnvelopeSchemaLoader } from "../utils/envelope-status-compat.mjs";
15
17
  import {
@@ -156,11 +158,26 @@ function compareBundleNamesDesc(a, b) {
156
158
  }
157
159
  const states = /* @__PURE__ */ new Map();
158
160
  const validatorDiagnosticOrigins = /* @__PURE__ */ new WeakMap();
161
+ const pendingDiagnosticOrigins = /* @__PURE__ */ new WeakMap();
162
+ const cachedValidatorRootSchemaIds = /* @__PURE__ */ new WeakMap();
163
+ const modularOriginStates = /* @__PURE__ */ new WeakMap();
164
+ function diagnosticOriginsFor(validator) {
165
+ pendingDiagnosticOrigins.get(validator)?.();
166
+ const origins = validatorDiagnosticOrigins.get(validator);
167
+ const state = origins && modularOriginStates.get(origins);
168
+ if (state) flushDeferredCompiles(state);
169
+ return origins;
170
+ }
159
171
  function resolveValidatorDiagnosticPath(validator, schemaPath) {
160
- return validatorDiagnosticOrigins.get(validator)?.resolve(schemaPath);
172
+ return diagnosticOriginsFor(validator)?.resolve(schemaPath);
161
173
  }
162
174
  function getValidatorDiagnosticOrigin(validator, schemaPath) {
163
- return validatorDiagnosticOrigins.get(validator)?.origin(schemaPath);
175
+ return diagnosticOriginsFor(validator)?.origin(schemaPath);
176
+ }
177
+ function getValidatorRootSchemaId(validator) {
178
+ if (cachedValidatorRootSchemaIds.has(validator)) return cachedValidatorRootSchemaIds.get(validator);
179
+ const root = validator.schema;
180
+ return root && typeof root === "object" && typeof root.$id === "string" ? root.$id : void 0;
164
181
  }
165
182
  const schemaRefValidators = /* @__PURE__ */ new Map();
166
183
  const keywordSchemaRefValidators = /* @__PURE__ */ new WeakMap();
@@ -173,8 +190,10 @@ const scopedExternalSchemaRoots = new AsyncLocalStorage();
173
190
  function stateCacheKey(bundleKey, root) {
174
191
  return `${bundleKey}\0${root}`;
175
192
  }
193
+ const epochRootFingerprints = /* @__PURE__ */ new Map();
176
194
  function clearStatesForBundle(bundleKey) {
177
195
  keywordCacheGeneration++;
196
+ epochRootFingerprints.delete(bundleKey);
178
197
  for (const [stateKey, state] of states) {
179
198
  if (state.version === bundleKey) states.delete(stateKey);
180
199
  }
@@ -182,13 +201,156 @@ function clearStatesForBundle(bundleKey) {
182
201
  if (cacheKey.startsWith(`${bundleKey}\0`)) schemaRefValidators.delete(cacheKey);
183
202
  }
184
203
  }
185
- function hasSchemaRootShape(root) {
204
+ function enterSchemaRootEpoch(aliases, root, fingerprint) {
205
+ for (const alias of aliases) {
206
+ if (fingerprint !== void 0 && epochRootFingerprints.get(alias)?.get(root) === fingerprint) continue;
207
+ clearStatesForBundle(alias);
208
+ if (fingerprint !== void 0) epochRootFingerprints.set(alias, /* @__PURE__ */ new Map([[root, fingerprint]]));
209
+ }
210
+ }
211
+ function newRootCheckReads(root) {
212
+ return { identities: /* @__PURE__ */ new Map(), stats: /* @__PURE__ */ new Map(), root };
213
+ }
214
+ function persistedRootIdentityKey(root) {
215
+ const { validatorCacheKey } = require2("./validator-cache");
216
+ return validatorCacheKey(["schema-root-identities", ...bundledValidatorCacheStaticKey(), path.resolve(root)]);
217
+ }
218
+ function persistedRootIdentities(reads) {
219
+ if (reads.persisted !== void 0) return reads.persisted;
220
+ reads.persisted = null;
221
+ const cache = openLoaderDataCache();
222
+ if (!cache) return null;
223
+ const value = cache.readData(persistedRootIdentityKey(reads.root));
224
+ if (!value || value.root !== path.resolve(reads.root) || !Array.isArray(value.documents)) return null;
225
+ const identities = /* @__PURE__ */ new Map();
226
+ for (const record of value.documents) {
227
+ if (!Array.isArray(record) || typeof record[0] !== "string" || typeof record[1] !== "string") return null;
228
+ const [file, stat, kind, $id, $schema] = record;
229
+ const identity = kind === "null" ? null : kind === "object" ? {
230
+ ...typeof $id === "string" && { $id },
231
+ ...typeof $schema === "string" && { $schema }
232
+ } : void 0;
233
+ identities.set(file, { stat, identity });
234
+ }
235
+ reads.persisted = identities;
236
+ return identities;
237
+ }
238
+ function persistRootCheck(reads) {
239
+ if (!reads.stats || reads.stats.size === 0) return;
240
+ const cache = openLoaderDataCache();
241
+ if (!cache) return;
242
+ const stored = persistedRootIdentities(reads);
243
+ let changed = stored === null || stored.size !== reads.stats.size;
244
+ const documents = [];
245
+ for (const [file, { stat, identity }] of reads.stats) {
246
+ documents.push([
247
+ file,
248
+ stat,
249
+ identity === void 0 ? "unparseable" : identity === null ? "null" : "object",
250
+ identity?.$id ?? null,
251
+ identity?.$schema ?? null
252
+ ]);
253
+ const previous = stored?.get(file);
254
+ if (!changed && (previous?.stat !== stat || !isDeepStrictEqual(previous.identity, identity))) changed = true;
255
+ }
256
+ if (changed) cache.writeData(persistedRootIdentityKey(reads.root), { root: path.resolve(reads.root), documents });
257
+ }
258
+ function rootFingerprint(root, reads) {
259
+ if (!reads.stats || reads.stats.size === 0 || path.basename(root) === "bundled") return void 0;
260
+ const hash = createHash("sha256");
261
+ for (const [file, { stat }] of [...reads.stats].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
262
+ hash.update(file).update("\0").update(stat).update("\0");
263
+ }
264
+ return hash.digest("hex");
265
+ }
266
+ const ROOT_DOCUMENT_SETTLE_NS = 3000000000n;
267
+ const ROOT_DOCUMENT_CACHE_LIMIT = 5e4;
268
+ function rootDocumentIdentityCache() {
269
+ const key = /* @__PURE__ */ Symbol.for(`@adcp/sdk.schema-root-document-identities.${LIBRARY_VERSION}`);
270
+ const registry = globalThis;
271
+ return registry[key] ??= /* @__PURE__ */ new Map();
272
+ }
273
+ function rootDocumentIdentity(schema) {
274
+ if (schema === null) return null;
275
+ const record = schema;
276
+ return {
277
+ ...typeof record.$id === "string" && { $id: record.$id },
278
+ ...typeof record.$schema === "string" && { $schema: record.$schema }
279
+ };
280
+ }
281
+ function readRootDocument(file, reads) {
282
+ let identity = reads.identities.get(file);
283
+ if (identity === void 0) {
284
+ try {
285
+ identity = readRootDocumentUncached(file, reads);
286
+ } catch (error) {
287
+ identity = error instanceof Error ? error : new Error(String(error));
288
+ }
289
+ reads.identities.set(file, identity);
290
+ }
291
+ if (identity instanceof Error) throw identity;
292
+ return identity;
293
+ }
294
+ function rememberRootDocumentIdentity(cache, file, entry) {
295
+ cache.delete(file);
296
+ cache.set(file, entry);
297
+ if (cache.size > ROOT_DOCUMENT_CACHE_LIMIT) {
298
+ const oldest = cache.keys().next().value;
299
+ if (oldest !== void 0) cache.delete(oldest);
300
+ }
301
+ }
302
+ function readRootDocumentUncached(file, reads) {
303
+ const bundled = file.includes(`${path.sep}bundled${path.sep}`);
304
+ const nowNs = BigInt(Date.now()) * 1000000n;
305
+ let stat;
306
+ try {
307
+ stat = statSync(file, { bigint: true });
308
+ } catch {
309
+ stat = void 0;
310
+ }
311
+ if (!stat?.isFile()) {
312
+ reads.stats = void 0;
313
+ return rootDocumentIdentity(loadJson(file));
314
+ }
315
+ const cache = rootDocumentIdentityCache();
316
+ const cacheKey = path.resolve(file);
317
+ const statKey = `${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;
318
+ let known = cache.get(cacheKey);
319
+ if (known?.stat !== statKey) {
320
+ const persisted = persistedRootIdentities(reads)?.get(cacheKey);
321
+ known = persisted?.stat === statKey ? persisted : void 0;
322
+ if (known) rememberRootDocumentIdentity(cache, cacheKey, known);
323
+ }
324
+ if (known) {
325
+ reads.stats?.set(cacheKey, known);
326
+ if (known.identity === void 0) throw new Error(`Schema document does not parse: ${file}`);
327
+ return known.identity;
328
+ }
329
+ let identity;
330
+ try {
331
+ const schema = (bundled ? loadBundledSchemaFile(file) : void 0) ?? JSON.parse(readFileSync(file, "utf-8"));
332
+ identity = rootDocumentIdentity(schema);
333
+ } catch {
334
+ identity = void 0;
335
+ }
336
+ const settled = stat.mtimeNs < nowNs - ROOT_DOCUMENT_SETTLE_NS && stat.ctimeNs < nowNs - ROOT_DOCUMENT_SETTLE_NS;
337
+ if (settled) {
338
+ const entry = { stat: statKey, identity };
339
+ reads.stats?.set(cacheKey, entry);
340
+ rememberRootDocumentIdentity(cache, cacheKey, entry);
341
+ } else {
342
+ reads.stats = void 0;
343
+ }
344
+ if (identity === void 0) throw new Error(`Schema document does not parse: ${file}`);
345
+ return identity;
346
+ }
347
+ function hasSchemaRootShape(root, reads = newRootCheckReads(root)) {
186
348
  if (!existsSync(root)) return false;
187
349
  const bundledRoot = path.join(root, "bundled");
188
350
  const files = hasBundledSchemaStore(bundledRoot) ? walkJsonFiles(bundledRoot) : walkJsonFiles(root);
189
351
  return files.some((file) => {
190
352
  try {
191
- const schema = loadJson(file);
353
+ const schema = readRootDocument(file, reads);
192
354
  return typeof schema.$id === "string" || typeof schema.$schema === "string";
193
355
  } catch {
194
356
  return false;
@@ -198,22 +360,6 @@ function hasSchemaRootShape(root) {
198
360
  function schemaIdVersionHint(schemaId) {
199
361
  return schemaId.match(/\/schemas\/([^/]+)\//)?.[1];
200
362
  }
201
- function collectSchemaRootVersionAliases(root) {
202
- const aliases = /* @__PURE__ */ new Set();
203
- for (const file of walkJsonFiles(root)) {
204
- let schema;
205
- try {
206
- schema = loadJson(file);
207
- } catch {
208
- continue;
209
- }
210
- if (typeof schema.$id !== "string") continue;
211
- const hint = schemaIdVersionHint(schema.$id);
212
- if (!hint) continue;
213
- for (const alias of schemaRootVersionAliases(hint)) aliases.add(alias);
214
- }
215
- return aliases;
216
- }
217
363
  function schemaRootVersionAliases(version) {
218
364
  const aliases = /* @__PURE__ */ new Set([version]);
219
365
  try {
@@ -232,19 +378,25 @@ function schemaRootVersionAliases(version) {
232
378
  function prereleaseFamilyAlias(releasePrecisionVersion) {
233
379
  return releasePrecisionVersion.match(/^(\d+\.\d+-[0-9A-Za-z-]+)\.\d+$/)?.[1];
234
380
  }
235
- function collectSchemaRootVersionAliasGroups(root) {
381
+ function collectSchemaRootVersionAliasGroups(root, reads) {
236
382
  const groups = [];
383
+ const aliasesByHint = /* @__PURE__ */ new Map();
237
384
  for (const file of walkJsonFiles(root)) {
238
385
  let schema;
239
386
  try {
240
- schema = loadJson(file);
387
+ schema = readRootDocument(file, reads);
241
388
  } catch {
242
389
  continue;
243
390
  }
244
391
  if (typeof schema.$id !== "string") continue;
245
392
  const hint = schemaIdVersionHint(schema.$id);
246
393
  if (!hint) continue;
247
- groups.push({ hint, aliases: schemaRootVersionAliases(hint) });
394
+ let aliases = aliasesByHint.get(hint);
395
+ if (!aliases) {
396
+ aliases = schemaRootVersionAliases(hint);
397
+ aliasesByHint.set(hint, aliases);
398
+ }
399
+ groups.push({ hint, aliases });
248
400
  }
249
401
  return groups;
250
402
  }
@@ -263,10 +415,10 @@ function readSchemaRootDeclaredVersion(root) {
263
415
  }
264
416
  return void 0;
265
417
  }
266
- function assertSchemaRootMatchesVersion(version, root) {
418
+ function assertSchemaRootMatchesVersion(version, root, reads = newRootCheckReads(root)) {
267
419
  const expectedAliases = schemaRootVersionAliases(version);
268
- const rootGroups = collectSchemaRootVersionAliasGroups(root);
269
- const rootAliases = collectSchemaRootVersionAliases(root);
420
+ const rootGroups = collectSchemaRootVersionAliasGroups(root, reads);
421
+ const rootAliases = new Set(rootGroups.flatMap((group) => [...group.aliases]));
270
422
  const declaredVersion = readSchemaRootDeclaredVersion(root);
271
423
  const declaredAliases = declaredVersion ? schemaRootVersionAliases(declaredVersion) : /* @__PURE__ */ new Set();
272
424
  const latestIsPinnedByIndex = [...declaredAliases].some((alias) => expectedAliases.has(alias));
@@ -284,15 +436,15 @@ const externalSchemaRootAliases = /* @__PURE__ */ new Map();
284
436
  function registerExternalSchemaRoot(version, root) {
285
437
  const key = resolveBundleKey(version);
286
438
  clearBundledSchemaStoreCache(path.join(root, "bundled"));
287
- if (!hasSchemaRootShape(root)) {
439
+ const reads = newRootCheckReads(root);
440
+ if (!hasSchemaRootShape(root, reads)) {
288
441
  throw new Error(`External AdCP schema root for version "${version}" not found or empty at ${root}`);
289
442
  }
290
- const aliases = assertSchemaRootMatchesVersion(version, root);
443
+ const aliases = assertSchemaRootMatchesVersion(version, root, reads);
444
+ persistRootCheck(reads);
291
445
  externalSchemaRootAliases.set(key, aliases);
292
- for (const alias of aliases) {
293
- externalSchemaRoots.set(alias, root);
294
- clearStatesForBundle(alias);
295
- }
446
+ for (const alias of aliases) externalSchemaRoots.set(alias, root);
447
+ enterSchemaRootEpoch(aliases, root, rootFingerprint(root, reads));
296
448
  }
297
449
  function unregisterExternalSchemaRoot(version) {
298
450
  const key = resolveBundleKey(version);
@@ -311,16 +463,16 @@ function unregisterExternalSchemaRoot(version) {
311
463
  function withExternalSchemaRoot(version, root, fn) {
312
464
  if (root === void 0) return fn();
313
465
  clearBundledSchemaStoreCache(path.join(root, "bundled"));
314
- if (!hasSchemaRootShape(root)) {
466
+ const reads = newRootCheckReads(root);
467
+ if (!hasSchemaRootShape(root, reads)) {
315
468
  throw new Error(`External AdCP schema root for version "${version}" not found or empty at ${root}`);
316
469
  }
317
- const aliases = assertSchemaRootMatchesVersion(version, root);
470
+ const aliases = assertSchemaRootMatchesVersion(version, root, reads);
471
+ persistRootCheck(reads);
318
472
  const parent = scopedExternalSchemaRoots.getStore();
319
473
  const next = new Map(parent);
320
- for (const alias of aliases) {
321
- next.set(alias, root);
322
- clearStatesForBundle(alias);
323
- }
474
+ for (const alias of aliases) next.set(alias, root);
475
+ enterSchemaRootEpoch(aliases, root, rootFingerprint(root, reads));
324
476
  return scopedExternalSchemaRoots.run(next, fn);
325
477
  }
326
478
  function getActiveSchemaArtifactLocation(version) {
@@ -516,12 +668,29 @@ function ensureInit(version) {
516
668
  code: { process: diagnosticOrigins.process }
517
669
  });
518
670
  addFormats(ajv);
671
+ const bundledCache = openBundledValidatorCache();
519
672
  const bundledAjv = new Ajv({
520
673
  inlineRefs: false,
521
674
  strict: false,
522
675
  allErrors: true,
523
676
  allowUnionTypes: true,
524
- code: { process: bundledDiagnosticOrigins.process }
677
+ code: bundledCache ? {
678
+ // Ajv keeps the code from before code.process; the cache records
679
+ // the processed code so stored modules carry the origin rewrite.
680
+ source: true,
681
+ process: (source, environment) => {
682
+ const output = bundledDiagnosticOrigins.process(
683
+ source,
684
+ environment
685
+ );
686
+ bundledCache.compileLog?.push({
687
+ env: environment,
688
+ source,
689
+ output
690
+ });
691
+ return output;
692
+ }
693
+ } : { process: bundledDiagnosticOrigins.process }
525
694
  });
526
695
  addFormats(bundledAjv);
527
696
  const state = {
@@ -536,13 +705,24 @@ function ensureInit(version) {
536
705
  entityPaths: /* @__PURE__ */ new Map(),
537
706
  root,
538
707
  coreLoaded: false,
539
- version: key
708
+ version: key,
709
+ ...bundledCache && { bundledCache }
540
710
  };
711
+ modularOriginStates.set(diagnosticOrigins, state);
541
712
  states.set(cacheKey, state);
542
713
  return state;
543
714
  }
544
715
  function ensureCoreLoaded(s) {
545
716
  if (s.coreLoaded) return;
717
+ try {
718
+ registerCoreDocuments(s);
719
+ } catch (error) {
720
+ s.coreLoadFailed = true;
721
+ throw error;
722
+ }
723
+ s.coreLoaded = true;
724
+ }
725
+ function registerCoreDocuments(s) {
546
726
  const selectedToolsById = /* @__PURE__ */ new Map();
547
727
  const responseToolFiles = /* @__PURE__ */ new Set();
548
728
  for (const [key, indexedFile] of s.fileIndex) {
@@ -574,10 +754,11 @@ function ensureCoreLoaded(s) {
574
754
  }
575
755
  }
576
756
  }
577
- s.coreLoaded = true;
578
757
  }
579
758
  function getValidator(toolName, direction, version = ADCP_VERSION) {
580
- const s = ensureInit(version);
759
+ return validatorForState(ensureInit(version), toolName, direction);
760
+ }
761
+ function validatorForState(s, toolName, direction) {
581
762
  const cacheKey = `${toolName}::${direction}`;
582
763
  const cached = s.validators.get(cacheKey);
583
764
  if (cached) return cached;
@@ -589,6 +770,12 @@ function getValidator(toolName, direction, version = ADCP_VERSION) {
589
770
  const rawSchema = loadJson(file);
590
771
  const prepared = fromBundled ? stripNestedIds(rawSchema) : rawSchema;
591
772
  const envelope = direction === "request" ? prepared : acceptEnvelopeOnClosedResponseRoot(prepared, s.root);
773
+ if (fromBundled && s.bundledCache) {
774
+ const resolved = resolveBundledValidatorWithCache(s, s.bundledCache, cacheKey, envelope);
775
+ s.validators.set(cacheKey, resolved);
776
+ validatorDiagnosticOrigins.set(resolved, s.bundledDiagnosticOrigins);
777
+ return resolved;
778
+ }
592
779
  const schema = fromBundled ? compactSchemaForValidation(envelope) : envelope;
593
780
  const ajv = fromBundled ? s.bundledAjv : s.ajv;
594
781
  const existing = typeof schema.$id === "string" ? ajv.getSchema(schema.$id) : void 0;
@@ -597,6 +784,265 @@ function getValidator(toolName, direction, version = ADCP_VERSION) {
597
784
  validatorDiagnosticOrigins.set(compiled, fromBundled ? s.bundledDiagnosticOrigins : s.diagnosticOrigins);
598
785
  return compiled;
599
786
  }
787
+ function definesToolRequest(toolName, version = ADCP_VERSION) {
788
+ const s = ensureInit(version);
789
+ const key = `${toolName}::request`;
790
+ if (s.validators.has(key) || s.cachedCompiles?.has(key)) return true;
791
+ const indexedFile = s.fileIndex.get(key);
792
+ if (!indexedFile) return false;
793
+ const modular = !runtimeToolSchemaFile(s, indexedFile, "request").includes(`${path.sep}bundled${path.sep}`);
794
+ const cache = modular ? openLoaderDataCache() : void 0;
795
+ const digest = cache ? stateContentDigest(s) : void 0;
796
+ if (!cache || digest === void 0 || s.coreLoadFailed) return compilesForState(s, toolName, "request");
797
+ const { validatorCacheKey } = require2("./validator-cache");
798
+ const outcomeKey = validatorCacheKey([
799
+ "modular-request-compiles",
800
+ ...bundledValidatorCacheStaticKey(),
801
+ process.versions.v8,
802
+ process.versions.unicode ?? null,
803
+ s.version,
804
+ digest,
805
+ toolName
806
+ ]);
807
+ if (cache.readData(outcomeKey)?.compiles === true) {
808
+ (s.cachedCompiles ??= /* @__PURE__ */ new Set()).add(key);
809
+ (s.deferredCompiles ??= /* @__PURE__ */ new Set()).add(key);
810
+ return true;
811
+ }
812
+ const compiles = compilesForState(s, toolName, "request");
813
+ if (compiles && !s.coreLoadFailed && contentUnchangedSinceDigest(s)) {
814
+ cache.writeData(outcomeKey, { compiles: true });
815
+ }
816
+ return compiles;
817
+ }
818
+ function compilesForState(s, toolName, direction) {
819
+ try {
820
+ return validatorForState(s, toolName, direction) !== void 0;
821
+ } catch {
822
+ return false;
823
+ }
824
+ }
825
+ function flushDeferredCompiles(s) {
826
+ const deferred = s.deferredCompiles;
827
+ if (!deferred) return;
828
+ s.deferredCompiles = void 0;
829
+ for (const key of deferred) {
830
+ if (!s.validators.has(key)) compilesForState(s, key.slice(0, -"::request".length), "request");
831
+ }
832
+ }
833
+ function stateContentDigest(s) {
834
+ if (s.contentDigest === void 0) s.contentDigest = s.coreLoaded ? null : rootContentDigest(s);
835
+ return s.contentDigest ?? void 0;
836
+ }
837
+ function contentUnchangedSinceDigest(s) {
838
+ if (!s.contentDigestChecked) {
839
+ s.contentDigestChecked = true;
840
+ if (rootContentDigest(s) !== s.contentDigest) s.contentDigest = null;
841
+ }
842
+ return typeof s.contentDigest === "string";
843
+ }
844
+ function rootContentDigest(s) {
845
+ const files = /* @__PURE__ */ new Set();
846
+ for (const entry of readdirSync(s.root, { withFileTypes: true })) {
847
+ if (entry.isDirectory()) {
848
+ if (isRuntimeSchemaDirectory(entry.name)) {
849
+ for (const file of walkJsonFiles(path.join(s.root, entry.name))) files.add(file);
850
+ }
851
+ } else if (entry.isFile() && entry.name.endsWith(".json")) {
852
+ files.add(path.join(s.root, entry.name));
853
+ }
854
+ }
855
+ for (const indexedFile of s.fileIndex.values()) files.add(authoredToolSchemaFile(s, indexedFile));
856
+ const byPath = (a, b) => a < b ? -1 : a > b ? 1 : 0;
857
+ const hash = createHash("sha256");
858
+ for (const [key, file] of [...s.fileIndex].sort(([a], [b]) => byPath(a, b))) {
859
+ hash.update(`index\0${key}\0${path.relative(s.root, file)}\0`);
860
+ }
861
+ for (const file of [...files].sort(byPath)) {
862
+ hash.update(`file\0${path.relative(s.root, file)}\0`);
863
+ hash.update(documentBytes(file));
864
+ hash.update("\0");
865
+ }
866
+ return hash.digest("hex");
867
+ }
868
+ function documentBytes(file) {
869
+ try {
870
+ return readFileSync(file);
871
+ } catch (error) {
872
+ if (file.includes(`${path.sep}bundled${path.sep}`)) {
873
+ try {
874
+ const entry = loadBundledSchemaFile(file);
875
+ if (entry) return `archive\0${JSON.stringify(entry)}`;
876
+ } catch {
877
+ }
878
+ }
879
+ return `unreadable\0${error.code ?? ""}`;
880
+ }
881
+ }
882
+ const VALIDATOR_CACHE_CODE_FILES = [
883
+ "schema-loader",
884
+ "compact-schema",
885
+ "schema-diagnostic-origins",
886
+ "bundled-schema-store",
887
+ "validator-cache"
888
+ ];
889
+ const BUNDLED_AJV_KEY = {
890
+ inlineRefs: false,
891
+ strict: false,
892
+ allErrors: true,
893
+ allowUnionTypes: true,
894
+ formats: "ajv-formats"
895
+ };
896
+ let validatorCacheStaticKey;
897
+ function openBundledValidatorCache() {
898
+ const dir = validatorCacheDir();
899
+ if (dir === void 0) return void 0;
900
+ const { openValidatorCache } = require2("./validator-cache");
901
+ const cache = openValidatorCache(dir, path.join(__dirname, "schema-loader.js"));
902
+ return cache ? { cache, compileLog: void 0, byId: /* @__PURE__ */ new Map() } : void 0;
903
+ }
904
+ function validatorCacheDir() {
905
+ const dir = process.env.ADCP_VALIDATOR_CACHE_DIR;
906
+ return dir && path.isAbsolute(dir) ? dir : void 0;
907
+ }
908
+ let loaderDataCache;
909
+ function openLoaderDataCache() {
910
+ const dir = validatorCacheDir();
911
+ if (dir === void 0) return void 0;
912
+ if (loaderDataCache?.dir !== dir) {
913
+ const { openValidatorCache } = require2("./validator-cache");
914
+ loaderDataCache = { dir, cache: openValidatorCache(dir, path.join(__dirname, "schema-loader.js")) };
915
+ }
916
+ return loaderDataCache.cache;
917
+ }
918
+ function bundledValidatorCacheStaticKey() {
919
+ if (validatorCacheStaticKey) return validatorCacheStaticKey;
920
+ const { filesDigest } = require2("./validator-cache");
921
+ const loaderRequire = createRequire(path.join(__dirname, "schema-loader.js"));
922
+ const packageVersion = (name, from) => {
923
+ try {
924
+ return from(`${name}/package.json`).version ?? null;
925
+ } catch {
926
+ return null;
927
+ }
928
+ };
929
+ let ajvRequire = loaderRequire;
930
+ try {
931
+ ajvRequire = createRequire(loaderRequire.resolve("ajv"));
932
+ } catch {
933
+ }
934
+ validatorCacheStaticKey = [
935
+ LIBRARY_VERSION,
936
+ filesDigest(
937
+ VALIDATOR_CACHE_CODE_FILES.flatMap((name) => [`${name}.js`, `${name}.mjs`]).map((file) => path.join(__dirname, file))
938
+ ),
939
+ packageVersion("ajv", loaderRequire),
940
+ packageVersion("ajv-formats", loaderRequire),
941
+ packageVersion("fast-uri", ajvRequire),
942
+ packageVersion("json-schema-traverse", ajvRequire),
943
+ packageVersion("fast-deep-equal", ajvRequire),
944
+ BUNDLED_AJV_KEY
945
+ ];
946
+ return validatorCacheStaticKey;
947
+ }
948
+ function compileBundledRecorded(s, bc, schema, wantModule) {
949
+ const log = [];
950
+ bc.compileLog = log;
951
+ let compiled;
952
+ try {
953
+ compiled = s.bundledAjv.compile(schema);
954
+ } finally {
955
+ bc.compileLog = void 0;
956
+ }
957
+ let moduleCode;
958
+ try {
959
+ let exact = wantModule && !compiled.$async && log.length > 0;
960
+ for (const entry of log) {
961
+ const fn = entry.env.validate;
962
+ const validateCode = fn?.source?.validateCode;
963
+ if (!fn || typeof validateCode !== "string" || !entry.source.endsWith(validateCode)) {
964
+ exact = false;
965
+ continue;
966
+ }
967
+ const prefixLength = entry.source.length - validateCode.length;
968
+ if (!entry.source.slice(0, prefixLength).endsWith("return ") || entry.output.slice(0, prefixLength) !== entry.source.slice(0, prefixLength)) {
969
+ exact = false;
970
+ continue;
971
+ }
972
+ fn.source.validateCode = entry.output.slice(prefixLength);
973
+ }
974
+ if (exact) {
975
+ const standaloneCode = require2("ajv/dist/standalone").default;
976
+ const code = standaloneCode(s.bundledAjv, compiled);
977
+ if (!code.includes("urn:adcp:schema-origin:")) moduleCode = code;
978
+ }
979
+ } catch {
980
+ moduleCode = void 0;
981
+ } finally {
982
+ for (const entry of log) delete entry.env.validate?.source;
983
+ }
984
+ return { compiled, ...moduleCode !== void 0 && { moduleCode } };
985
+ }
986
+ function resolveBundledValidatorWithCache(s, bc, toolKey, envelope) {
987
+ const id = typeof envelope.$id === "string" ? envelope.$id : void 0;
988
+ const known = id !== void 0 ? bc.byId.get(id) : void 0;
989
+ if (known) return known;
990
+ const { validatorCacheKey } = require2("./validator-cache");
991
+ const key = validatorCacheKey([...bundledValidatorCacheStaticKey(), s.version, toolKey, envelope]);
992
+ const entry = bc.cache.read(key);
993
+ if (entry) {
994
+ const fn = entry.validate;
995
+ let compacted;
996
+ const compactedSchema = () => compacted ??= compactSchemaForValidation(envelope);
997
+ const materialize = () => {
998
+ pendingDiagnosticOrigins.delete(fn);
999
+ try {
1000
+ const schema2 = compactedSchema();
1001
+ const existing2 = typeof schema2.$id === "string" ? s.bundledAjv.getSchema(schema2.$id) : void 0;
1002
+ return existing2 ?? compileBundledRecorded(s, bc, schema2, false).compiled;
1003
+ } catch {
1004
+ return void 0;
1005
+ }
1006
+ };
1007
+ pendingDiagnosticOrigins.set(fn, () => void materialize());
1008
+ Object.defineProperty(fn, "schema", {
1009
+ configurable: true,
1010
+ enumerable: true,
1011
+ get() {
1012
+ const value = compactedSchema();
1013
+ Object.defineProperty(fn, "schema", { value, writable: true, enumerable: true, configurable: true });
1014
+ return value;
1015
+ }
1016
+ });
1017
+ Object.defineProperty(fn, "schemaEnv", {
1018
+ configurable: true,
1019
+ enumerable: true,
1020
+ get() {
1021
+ const live = (pendingDiagnosticOrigins.has(fn) ? materialize() : void 0) ?? (typeof compactedSchema().$id === "string" ? s.bundledAjv.getSchema(compactedSchema().$id) : void 0);
1022
+ const value = live?.schemaEnv;
1023
+ Object.defineProperty(fn, "schemaEnv", { value, writable: true, enumerable: true, configurable: true });
1024
+ return value;
1025
+ }
1026
+ });
1027
+ cachedValidatorRootSchemaIds.set(fn, entry.rootSchemaId);
1028
+ if (id !== void 0) bc.byId.set(id, fn);
1029
+ return fn;
1030
+ }
1031
+ const schema = compactSchemaForValidation(envelope);
1032
+ const existing = typeof schema.$id === "string" ? s.bundledAjv.getSchema(schema.$id) : void 0;
1033
+ let compiled;
1034
+ if (existing) {
1035
+ compiled = existing;
1036
+ } else {
1037
+ const result = compileBundledRecorded(s, bc, schema, true);
1038
+ compiled = result.compiled;
1039
+ if (result.moduleCode !== void 0) {
1040
+ bc.cache.write(key, result.moduleCode, typeof schema.$id === "string" ? schema.$id : void 0);
1041
+ }
1042
+ }
1043
+ if (id !== void 0) bc.byId.set(id, compiled);
1044
+ return compiled;
1045
+ }
600
1046
  function authoredToolSchemaFile(state, indexedFile) {
601
1047
  const bundledRoot = path.join(state.root, "bundled");
602
1048
  const relative = path.relative(bundledRoot, indexedFile);
@@ -839,6 +1285,7 @@ function listValidatorKeys(version = ADCP_VERSION) {
839
1285
  }
840
1286
  function getRegisteredSchemaIds(version = ADCP_VERSION) {
841
1287
  const s = ensureInit(version);
1288
+ flushDeferredCompiles(s);
842
1289
  const registry = s.ajv.schemas;
843
1290
  const bundledRegistry = s.bundledAjv.schemas;
844
1291
  return [.../* @__PURE__ */ new Set([...Object.keys(registry ?? {}), ...Object.keys(bundledRegistry ?? {})])];
@@ -881,10 +1328,12 @@ function withServedReleaseSchemaRoots(roots, fn) {
881
1328
  const memoKey = `${version}\0${root}`;
882
1329
  let aliases = servedReleaseRootAliases.get(memoKey);
883
1330
  if (!aliases) {
884
- if (!hasSchemaRootShape(root)) {
1331
+ const reads = newRootCheckReads(root);
1332
+ if (!hasSchemaRootShape(root, reads)) {
885
1333
  throw new Error(`Served-release AdCP schema root for version "${version}" not found or empty at ${root}`);
886
1334
  }
887
- aliases = assertSchemaRootMatchesVersion(version, root);
1335
+ aliases = assertSchemaRootMatchesVersion(version, root, reads);
1336
+ persistRootCheck(reads);
888
1337
  servedReleaseRootAliases.set(memoKey, aliases);
889
1338
  }
890
1339
  const conflict = entries.find((entry) => entry.root !== root && [...aliases].some((alias) => entry.aliases.has(alias)));
@@ -1041,6 +1490,8 @@ function _resetValidationLoader(version) {
1041
1490
  if (version === void 0) {
1042
1491
  states.clear();
1043
1492
  schemaRefValidators.clear();
1493
+ epochRootFingerprints.clear();
1494
+ rootDocumentIdentityCache().clear();
1044
1495
  keywordCacheGeneration++;
1045
1496
  } else {
1046
1497
  clearStatesForBundle(resolveBundleKey(version));
@@ -1218,6 +1669,7 @@ export {
1218
1669
  acceptEnvelopeOnClosedResponseRoot,
1219
1670
  acceptEnvelopeOnClosedResponseRootForVersion,
1220
1671
  bindIdentity33SchemaScope,
1672
+ definesToolRequest,
1221
1673
  getActiveSchemaArtifactLocation,
1222
1674
  getCanonicalToolValidatorForVersion,
1223
1675
  getMcpProfileInputSchema,
@@ -1233,6 +1685,7 @@ export {
1233
1685
  getToolSchemaDocument,
1234
1686
  getValidator,
1235
1687
  getValidatorDiagnosticOrigin,
1688
+ getValidatorRootSchemaId,
1236
1689
  hasSchemaBundle,
1237
1690
  isExternalSchemaRootActive,
1238
1691
  isIdentity33SchemaScope,