{"version":3,"file":"engine-node.mjs","names":[],"sources":["../src/engine/node.ts"],"sourcesContent":["/**\n * `kya-os-engine` WASM bridge for Node: the synchronous engine API\n * (`./engine/node`).\n *\n * It runs on the ESM Node build (`wasm/kya-os-engine-node-esm/`,\n * wasm-bindgen's `experimental-nodejs-module` target), the same module the\n * `./orchestrator` Node entries load lazily (`./node-loader.ts`). A module\n * is evaluated once per process, so this API and the orchestrators share one\n * engine instance: the vendor keys and feed state one adopts are the ones the\n * other sees. The glue instantiates synchronously when it is evaluated and has\n * no top-level await, so the CommonJS build of this entry `require()`s it,\n * which needs Node ^20.19 or >=22.12.\n *\n * The glue reads its wasm with `readFileSync(new URL('kya_os_engine_bg.wasm',\n * import.meta.url))` and stays external to the dist build (`commonOpts.external`\n * in tsdown.config.ts), so it resolves next to its wasm in the installed\n * package; bundlers trace that `new URL` as one static asset.\n *\n * Direct .wasm path (for callers building their own loaders):\n *   `@kya-os/checkpoint-wasm-runtime/wasm/kya-os-engine-node-esm/kya_os_engine_bg.wasm`\n *\n * For Edge runtime (Vercel Edge, Cloudflare Workers), use the\n * `./edge` subpath which uses `--target web` with a host-passed module.\n */\n\nimport type {\n  AdoptedVendorKeys,\n  AgentRequest,\n  ContextSpec,\n  DelegationVerifyInput,\n  FeedAttemptOutcome,\n  FeedDemandReason,\n  RuleBundleReport,\n  VendorKeyFeedStatus,\n  VendorKeyMissReason,\n  VerifyResult,\n} from './types';\nimport {\n  unwrapRenderEnvelope,\n  type EngineManifest,\n  type EngineRenderEnvelope,\n  type EngineRenderOptions,\n  type EngineRenderedResponse,\n} from './render-types';\nimport type { StatusListVerification } from './adapters/status-list-cache';\n\n// SDK-WASM-Bundler-Loader-1: namespace-import + lazy property access\n// (`wasmModule.verify` read at call time), so a bundler's interop helper\n// never snapshots an export as `(void 0)`.\n// Package self-import — see `./index.ts` for the rationale (keeps\n// the import string stable across source-vs-dist location boundary).\n// The same specifier as `./node-loader.ts`, so both resolve to one module.\nimport * as wasmModule from '@kya-os/checkpoint-wasm-runtime/wasm/kya-os-engine-node-esm/kya_os_engine.js';\n\n/**\n * Verify an AgentRequest synchronously against a pre-fetched\n * [`ContextSpec`]. Identical contract to the bundler-target entry\n * (`./index.ts`); runs on the ESM Node build.\n *\n * @throws {Error} if the WASM glue rejects the input (deserialisation\n *   failure) or the engine returns a `VerifyError` (resolver /\n *   status-list / reputation / policy infrastructure failure).\n */\nexport function engineVerify(input: AgentRequest, ctx: ContextSpec): VerifyResult {\n  // Read .verify at call time, not at module-load time — defeats\n  // esbuild's static substitution to `(void 0)`. See file header\n  // for full rationale.\n  const verify = (wasmModule as unknown as { verify: (i: AgentRequest, c: ContextSpec) => unknown })\n    .verify;\n  return verify(input, ctx) as VerifyResult;\n}\n\n/**\n * Standalone Layer-2-only delegation verify (Node target). Identical contract\n * to the bundler-target [`engineVerifyDelegation`](./index.ts); routes through\n * the Node glue's `verifyDelegation` export. Same lazy call-time member\n * access as [`engineVerify`] to defeat esbuild's CJS tree-shake.\n */\nexport function engineVerifyDelegation(\n  input: DelegationVerifyInput,\n  ctx: ContextSpec\n): VerifyResult {\n  const verifyDelegation = (\n    wasmModule as unknown as {\n      verifyDelegation: (i: DelegationVerifyInput, c: ContextSpec) => unknown;\n    }\n  ).verifyDelegation;\n  return verifyDelegation(input, ctx) as VerifyResult;\n}\n\n/**\n * Render the kya-os/http answer in the engine (Node target). Identical\n * contract to the bundler-target [`engineRender`](./index.ts). Same lazy\n * call-time member access as [`engineVerify`] to defeat esbuild's CJS\n * tree-shake.\n *\n * @throws {EngineRenderError} when the registered challenge context fails\n *   validation.\n */\nexport function engineRender(\n  result: VerifyResult,\n  options: EngineRenderOptions = {}\n): EngineRenderedResponse {\n  const render = (wasmModule as unknown as { render: (input: unknown) => EngineRenderEnvelope })\n    .render;\n  return unwrapRenderEnvelope(render({ result, options }));\n}\n\n/** This engine build's capability manifest (Node target). */\nexport function engineInfo(): EngineManifest {\n  const info = (wasmModule as unknown as { engineInfo: () => EngineManifest }).engineInfo;\n  return info();\n}\n\nexport {\n  EngineRenderError,\n  type EngineCapability,\n  type EngineManifest,\n  type EngineRenderEnvelope,\n  type EngineRenderErrorCode,\n  type EngineRenderOptions,\n  type EngineRenderedResponse,\n} from './render-types';\n\nexport type {\n  AdoptedVendorKey,\n  AdoptedVendorKeys,\n  AgentRequest,\n  A2ARequest,\n  A2PRequest,\n  BehaviouralSignals,\n  BlockReason,\n  ChallengeParams,\n  ClientSignalAttribute,\n  ClientIntegrityInput,\n  ClientSignals,\n  ContextSpec,\n  Decision,\n  DelegationVerifyInput,\n  DetectionDetail,\n  DidDocument,\n  EngineConfig,\n  EngineInfo,\n  EnforcementMode,\n  FeedAttemptOutcome,\n  FeedDemandReason,\n  HttpSignedRequest,\n  InstructPayload,\n  IpReputation,\n  KeySetOutcome,\n  KeyType,\n  McpIPayload,\n  McpIRequest,\n  NetworkContext,\n  PlainHttpRequest,\n  ProxyInfo,\n  ProxyType,\n  RedirectTarget,\n  RoutingType,\n  RuleBundleApplied,\n  RuleBundleRejected,\n  RuleBundleRejectionCode,\n  RuleBundleReport,\n  SignalsEnvelope,\n  StabilityTier,\n  SuggestedAction,\n  Tier1PolicyMode,\n  Tier3Action,\n  TransportSignals,\n  VendorKeyFeedKeyStatus,\n  VendorKeyFeedStatus,\n  VendorKeyMissReason,\n  VerificationMethod,\n  VerifyResult,\n} from './types';\n\n/**\n * Verify a fetched status list against its issuer's DID document (Node\n * target). Same contract as the bundler-target `engineVerifyStatusList`.\n */\nexport function engineVerifyStatusList(requestJson: string): StatusListVerification {\n  const verifyStatusList = (\n    wasmModule as unknown as { verifyStatusList: (json: string) => unknown }\n  ).verifyStatusList;\n  return verifyStatusList(requestJson) as StatusListVerification;\n}\n\n/**\n * The trusted roots' DID-document needs (Node target). Same contract as the\n * bundler-target `engineRootNeeds`.\n */\nexport function engineRootNeeds(contextJson: string): import('./types').RootNeedsResult {\n  const rootNeeds = (wasmModule as unknown as { rootNeeds: (json: string) => unknown }).rootNeeds;\n  return rootNeeds(contextJson) as import('./types').RootNeedsResult;\n}\n\nexport function engineApplyRuleBundle(bundleJson: string, nowUnix: number): RuleBundleReport {\n  const applyRuleBundle = (\n    wasmModule as unknown as { applyRuleBundle: (json: string, now: number) => unknown }\n  ).applyRuleBundle;\n  return applyRuleBundle(bundleJson, nowUnix) as RuleBundleReport;\n}\n\nexport function engineFeedDemand(nowUnix: number): FeedDemandReason | undefined {\n  const feedDemand = (wasmModule as unknown as { feedDemand: (now: number) => unknown }).feedDemand;\n  return feedDemand(nowUnix) as FeedDemandReason | undefined;\n}\n\nexport function engineFeedAttemptFinished(outcome: FeedAttemptOutcome): boolean {\n  const feedAttemptFinished = (\n    wasmModule as unknown as { feedAttemptFinished: (outcome: string) => unknown }\n  ).feedAttemptFinished;\n  return feedAttemptFinished(outcome) as boolean;\n}\n\nexport function engineFeedStatus(nowUnix: number): VendorKeyFeedStatus | null {\n  const feedStatus = (wasmModule as unknown as { feedStatus: (now: number) => unknown }).feedStatus;\n  return feedStatus(nowUnix) as VendorKeyFeedStatus | null;\n}\n\nexport function engineFeedNoteMiss(keyid: string, reason: VendorKeyMissReason): boolean {\n  const feedNoteMiss = (\n    wasmModule as unknown as { feedNoteMiss: (keyid: string, reason: string) => unknown }\n  ).feedNoteMiss;\n  return feedNoteMiss(keyid, reason) as boolean;\n}\n\nexport function engineVendorKeys(): AdoptedVendorKeys {\n  const vendorKeys = (wasmModule as unknown as { vendorKeys: () => unknown }).vendorKeys;\n  return vendorKeys() as AdoptedVendorKeys;\n}\n"],"mappings":";;;;;;;;;;;;;AA+DA,SAAgB,aAAa,OAAqB,KAAgC;CAIhF,MAAM,SAAU,WACb;CACH,OAAO,OAAO,OAAO,GAAG;AAC1B;;;;;;;AAQA,SAAgB,uBACd,OACA,KACc;CACd,MAAM,mBACJ,WAGA;CACF,OAAO,iBAAiB,OAAO,GAAG;AACpC;;;;;;;;;;AAWA,SAAgB,aACd,QACA,UAA+B,CAAC,GACR;CACxB,MAAM,SAAU,WACb;CACH,OAAO,qBAAqB,OAAO;EAAE;EAAQ;CAAQ,CAAC,CAAC;AACzD;;AAGA,SAAgB,aAA6B;CAC3C,MAAM,OAAQ,WAA+D;CAC7E,OAAO,KAAK;AACd;;;;;AAoEA,SAAgB,uBAAuB,aAA6C;CAClF,MAAM,mBACJ,WACA;CACF,OAAO,iBAAiB,WAAW;AACrC;;;;;AAMA,SAAgB,gBAAgB,aAAwD;CACtF,MAAM,YAAa,WAAmE;CACtF,OAAO,UAAU,WAAW;AAC9B;AAEA,SAAgB,sBAAsB,YAAoB,SAAmC;CAC3F,MAAM,kBACJ,WACA;CACF,OAAO,gBAAgB,YAAY,OAAO;AAC5C;AAEA,SAAgB,iBAAiB,SAA+C;CAC9E,MAAM,aAAc,WAAmE;CACvF,OAAO,WAAW,OAAO;AAC3B;AAEA,SAAgB,0BAA0B,SAAsC;CAC9E,MAAM,sBACJ,WACA;CACF,OAAO,oBAAoB,OAAO;AACpC;AAEA,SAAgB,iBAAiB,SAA6C;CAC5E,MAAM,aAAc,WAAmE;CACvF,OAAO,WAAW,OAAO;AAC3B;AAEA,SAAgB,mBAAmB,OAAe,QAAsC;CACtF,MAAM,eACJ,WACA;CACF,OAAO,aAAa,OAAO,MAAM;AACnC;AAEA,SAAgB,mBAAsC;CACpD,MAAM,aAAc,WAAwD;CAC5E,OAAO,WAAW;AACpB"}