// crawlcheck-verify.mjs — verifies signed CrawlCheck documents: Resolve answers, decision receipts and evidence bundles.
// Zero dependencies. Node 20+. It reads the document's kind and verifies whichever it is:
//   node crawlcheck-verify.mjs --online answer.json       a signed Resolve answer or decision receipt, keys read from crawlcheck.io now
//   node crawlcheck-verify.mjs --keys keys.json answer.json   the same with a saved key directory, no network
//   node crawlcheck-verify.mjs bundle.json                an evidence bundle, from its own bytes
// Exit codes: 0 ACCEPTED; 1 a check FAILED; 3 intact but NOT ACCEPTED (issuer unknown offline, or not trusted); 2 not verifiable.
//   --act domain:action exits 0 only when the receipt is eligible to act on.
// Browser or Node module:        import { verifyBundle } from "./crawlcheck-verify.mjs"
//
// Also verifies a remediation receipt (node crawlcheck-verify.mjs receipt.json): receipt_hash, key_id, signature, order;
// and an evidence export (node crawlcheck-verify.mjs export.json): export_hash, key_id, signature, then every bundle and receipt inside it.
// And a dispute resolution (node crawlcheck-verify.mjs resolution.json): resolution_hash, key_id, signature,
// verdict_derivable (re-runs the published decision table over the recorded readings), readings_after_dispute.
// And a data inventory, export part or deletion record (node crawlcheck-verify.mjs deletion.json): document_hash, key_id, signature.
// What it checks, each from the bytes inside the bundle:
//   manifest_hash      sha256(manifest.body) equals manifest.sha256
//   key_id             the RFC 7638 thumbprint of the public key equals the signature's key id
//   signature          Ed25519 over "crawlcheck-meg-manifest-v1\n" + manifest sha256
//   subject_hash       sha256(record.subject) equals the record digest (the leaf)
//   subject_binds      the subject names this report, and its meg field names this manifest
//   merkle_path        the leaf folds along the path to the day's root
//   ots_commits_root   the .ots file timestamps exactly that root
//   ots_attestations   the op chain evaluated: pending calendars, or the Bitcoin block and the merkle root it commits to
//   experience_root    (manifest v3) the manifest's sealed root over its fetches recomputes from the fetches it lists
//   decision_root      (manifest v3) the sealed root over the decisions recomputes from the decision leaves (licence bundles)
//   lineage_root       (manifest v3) the sealed root over the extracted business facts recomputes from their leaves (licence bundles)
// A check that cannot run (a withheld subject, an unsealed day) is null with the reason, never a pass.
// Every result also separates the questions a single "verified" used to answer at once (vt1, 2026-10-06):
//   integrity_valid   no check over the bytes failed (hashes, signature, proof paths)
//   issuer_trusted    the signing key is in CrawlCheck's published key directory: true / false, or null offline
//   accepted          integrity_valid AND issuer_trusted === true. Act on this, never on verified alone.
//   accepted_strict   accepted AND every check ran (legacy and pre-manifest records stay false: less was sealed)
//   assurance         "complete" or "partial", with unavailable[] naming the checks that could not run
//   eligibleToAct(receipt, { domain, action }, { publishedKids, allowWarn, confirmed }) answers, for a decision receipt,
//   each question an agent must settle before acting, apart: integrity_valid, signature_valid, issuer_trusted, fresh,
//   scope_valid, decision_permits, and eligible_to_act only when all of them hold (ea1, 2026-10-10).
//   CLI: node crawlcheck-verify.mjs receipt.json --act example.com:read [--allow-warn] [--confirmed]  (exit 0 = eligible)
//   ga1 (2026-10-10): eligibleToAct also checks the nonce (want.nonce / --nonce), caps clock skew at 300 s, refuses a
//   receipt that outlives its action's maximum lifetime, and treats www. as the same site (https://crawlcheck.io/spec/guard).
//   op1 (2026-10-10): want.operation / --operation '{"method":"GET","url":"..."}': the receipt must name exactly that operation.
//   kp3 (2026-10-10): keys separated by purpose. A decision's resolve answer and an answer's log head may be signed by
//   another key; online every key a document leans on must be published (relatedKids, key_published).
//   conf2 (2026-10-18): key_policy_valid. The signing policy (https://crawlcheck.io/keys#policy) as code: which key
//   purpose may sign which object at which time, retired keys (rotation record) and compromised keys. accepted needs it.
// Three modes, and what each needs:
//   offline (default)  no network, no keys: integrity only. issuer_trusted and key_policy_valid are null, accepted is false.
//   --keys dir.json [--rotation rot.json]   still no network: checked against a directory (and rotation record) you saved
//                      earlier. Only as current as that copy: a key retired or compromised after you saved it is unknown.
//   --online           fetches /.well-known/http-message-signatures-directory and /.well-known/security-key-rotation.json now.
// None of them says whether to act: ACCEPTED means authentic. What to do is the decision inside (see --act).
// What it cannot do offline: read a Bitcoin block header. It prints the block height and the merkle root the
// proof commits to; compare that value with any Bitcoin node or block explorer.

const MEG_SIG_PREFIX = "crawlcheck-meg-manifest-v1\n";
const OTS_MAGIC = [0x00, 0x4f, 0x70, 0x65, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x73, 0x00, 0x00, 0x50, 0x72, 0x6f, 0x6f, 0x66, 0x00, 0xbf, 0x89, 0xe2, 0xe8, 0x84, 0xe8, 0x92, 0x94];
const TAG_BITCOIN = "0588960d73d71901", TAG_PENDING = "83dfe30d2ef90c8e", TAG_LITECOIN = "06869a0d73d71b45";

const subtle = globalThis.crypto && globalThis.crypto.subtle;
const enc = new TextEncoder();
const hex = (u8) => Array.from(u8, (b) => b.toString(16).padStart(2, "0")).join("");
const unhex = (h) => { const s = String(h || ""); const o = new Uint8Array(s.length >> 1); for (let i = 0; i < o.length; i++) o[i] = parseInt(s.substr(i * 2, 2), 16); return o; };
const sha256 = async (u8) => new Uint8Array(await subtle.digest("SHA-256", u8));
const b64 = (s) => { const t = String(s || "").replace(/-/g, "+").replace(/_/g, "/"); const p = t.padEnd(Math.ceil(t.length / 4) * 4, "="); const bin = atob(p); const o = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) o[i] = bin.charCodeAt(i); return o; };
const b64u = (u8) => { let s = ""; for (const b of u8) s += String.fromCharCode(b); return btoa(s).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); };
const cat = (a, b) => { const o = new Uint8Array(a.length + b.length); o.set(a, 0); o.set(b, a.length); return o; };
// The same canonical JSON the observer hashes: keys sorted at every level, no whitespace, undefined as null.
export function canon(v) {
  if (v === null || typeof v !== "object") return JSON.stringify(v === undefined ? null : v);
  if (Array.isArray(v)) return "[" + v.map(canon).join(",") + "]";
  return "{" + Object.keys(v).sort().map((k) => JSON.stringify(k) + ":" + canon(v[k])).join(",") + "}";
}
// The same Merkle tree the daily seal uses: leaves sorted as hex, parent = SHA-256(left || right), odd node paired with itself.
export async function merkleRootHex(hexLeaves) {
  if (!hexLeaves.length) return "";
  let level = hexLeaves.slice().sort();
  while (level.length > 1) {
    const next = [];
    for (let i = 0; i < level.length; i += 2) next.push(hex(await sha256(cat(unhex(level[i]), unhex(level[i + 1] === undefined ? level[i] : level[i + 1])))));
    level = next;
  }
  return level[0];
}
const leafHex = async (o) => hex(await sha256(enc.encode(canon(o))));

// ── OpenTimestamps: parse the detached proof and evaluate every branch ─────────
function reader(u8) {
  let i = 0;
  return {
    left: () => u8.length - i,
    byte() { if (i >= u8.length) throw new Error("ots: truncated"); return u8[i++]; },
    bytes(n) { if (i + n > u8.length) throw new Error("ots: truncated"); const o = u8.slice(i, i + n); i += n; return o; },
    varuint() { let v = 0, s = 0, b; do { b = this.byte(); v += (b & 0x7f) * Math.pow(2, s); s += 7; } while (b & 0x80); return v; },
    varbytes() { return this.bytes(this.varuint()); }
  };
}
async function otsBranch(rd, msg, out, depth) {
  if (depth > 256) throw new Error("ots: too deep");
  const one = async (tag) => {
    if (tag === 0x00) {
      const t = hex(rd.bytes(8)), payload = rd.varbytes();
      if (t === TAG_BITCOIN) { const pr = reader(payload); out.push({ kind: "bitcoin", height: pr.varuint(), commits_to_le: hex(msg), merkle_root: hex(msg.slice().reverse()) }); }
      else if (t === TAG_PENDING) { const pr = reader(payload); out.push({ kind: "pending", calendar: new TextDecoder().decode(pr.varbytes()) }); }
      else if (t === TAG_LITECOIN) { const pr = reader(payload); out.push({ kind: "litecoin", height: pr.varuint() }); }
      else out.push({ kind: "unknown", tag: t });
      return;
    }
    let next;
    if (tag === 0x08) next = await sha256(msg);
    else if (tag === 0xf0) next = cat(msg, rd.varbytes());
    else if (tag === 0xf1) next = cat(rd.varbytes(), msg);
    else if (tag === 0xf2) next = msg.slice().reverse();
    else if (tag === 0xf3) next = enc.encode(hex(msg));
    else throw new Error("ots: op 0x" + tag.toString(16) + " is not supported by this verifier");
    await otsBranch(rd, next, out, depth + 1);
  };
  let tag = rd.byte();
  while (tag === 0xff) { await one(rd.byte()); tag = rd.byte(); }
  await one(tag);
}
export async function otsInspect(u8) {
  const rd = reader(u8);
  const magic = rd.bytes(OTS_MAGIC.length);
  if (!OTS_MAGIC.every((b, i) => magic[i] === b)) throw new Error("not an OpenTimestamps proof (magic bytes differ)");
  const version = rd.varuint();
  if (version !== 1) throw new Error("ots: unsupported version " + version);
  const op = rd.byte();
  if (op !== 0x08) throw new Error("ots: file hash is not sha256");
  const digest = rd.bytes(32);
  const attestations = [];
  await otsBranch(rd, digest, attestations, 0);
  return { digest: hex(digest), attestations };
}

// ── the bundle ────────────────────────────────────────────────────────────────
async function verifyBundle0(b) {
  if (!subtle) throw new Error("this runtime has no WebCrypto (crypto.subtle)");
  const checks = [];
  const C = (id, ok, why, extra) => checks.push(Object.assign({ id, ok, why }, extra || {}));
  if (!b || b.kind !== "crawlcheck-evidence-bundle") throw new Error("not a CrawlCheck evidence bundle");
  const rep = b.report || {}, man = b.manifest || null, rec = b.record || {}, seal = b.seal || {};

  // 1. manifest bytes
  let manObj = null;
  if (!man) C("manifest_hash", null, "this record predates evidence manifests (2026-09-20)");
  else if (!man.body) C("manifest_hash", null, man.body_missing || "manifest body not included");
  else {
    const h = hex(await sha256(enc.encode(man.body)));
    C("manifest_hash", h === man.sha256, h === man.sha256 ? "manifest body hashes to " + h.slice(0, 16) + "…" : "manifest body hashes to " + h + ", bundle says " + man.sha256);
    try { manObj = JSON.parse(man.body); } catch (e) { manObj = null; }
  }
  // 2. key id + signature
  const sg = man && man.signature, key = man && man.key;
  if (!sg) C("key_id", null, "the manifest is unsigned (recorded before signing began, 2026-09-23)"), C("signature", null, "no signature to check");
  else if (!key || !key.jwk) C("key_id", null, "the signing key is not in the bundle (retired or unknown); look it up by id " + sg.kid), C("signature", null, "no key to check against");
  else {
    const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: key.jwk.crv, kty: key.jwk.kty, x: key.jwk.x }))));
    C("key_id", thumb === sg.kid, thumb === sg.kid ? "public key thumbprint (RFC 7638) is the key id " + sg.kid.slice(0, 12) + "…; the same id is published in " + (key.published_in || "the key directory") : "thumbprint " + thumb + " does not match key id " + sg.kid);
    const msg = MEG_SIG_PREFIX + man.sha256;
    if (sg.message && sg.message !== msg) C("signature", false, "the signed message does not name this manifest");
    else {
      let ok = false;
      try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x: key.jwk.x }, { name: "Ed25519" }, false, ["verify"]); ok = await subtle.verify({ name: "Ed25519" }, pk, b64(sg.sig), enc.encode(msg)); }
      catch (e) { C("signature", null, "this runtime cannot verify Ed25519: " + (e && e.message || e)); ok = null; }
      if (ok !== null) C("signature", ok, ok ? "Ed25519 signature over the manifest digest verifies" : "signature does NOT verify");
    }
  }
  // 3. subject
  let subj = null;
  if (!rec.digest) C("subject_hash", null, "no record digest in the bundle");
  else if (!rec.subject) C("subject_hash", null, rec.subject_withheld || "subject not included");
  else {
    const h = hex(await sha256(enc.encode(rec.subject)));
    C("subject_hash", h === rec.digest, h === rec.digest ? "the sealed subject hashes to the record digest" : "subject hashes to " + h + ", digest is " + rec.digest);
    try { subj = JSON.parse(rec.subject); } catch (e) { subj = null; }
  }
  if (!subj) C("subject_binds", null, "needs the subject");
  else {
    const idOk = !rep.id || subj.id === rep.id, domOk = !rep.domain || subj.domain === rep.domain;
    const mOk = man ? subj.meg === man.sha256 : !("meg" in subj);
    C("subject_binds", idOk && domOk && mOk, (idOk ? "names report " + subj.id : "names report " + subj.id + ", bundle says " + rep.id) + (domOk ? ", domain " + subj.domain : ", domain mismatch") +
      (man ? (mOk ? ", and commits to this manifest" : ", but its meg field " + subj.meg + " is not this manifest") : ""));
  }
  // 4. merkle path
  if (seal.state !== "sealed") C("merkle_path", null, "the day is not sealed: " + (seal.why || seal.state || "unknown"));
  else {
    let h = unhex(rec.digest);
    for (const s of seal.path || []) h = await sha256(s.side === "right" ? cat(h, unhex(s.hash)) : cat(unhex(s.hash), h));
    const got = hex(h);
    C("merkle_path", got === seal.root, got === seal.root ? "the digest folds to the root of " + seal.day + " (" + (seal.path || []).length + " steps, " + (seal.leaves_that_day || "?") + " leaves that day)" : "the path folds to " + got + ", not the root " + seal.root);
  }
  // 5. timestamp
  let att = [];
  if (seal.state !== "sealed") C("ots_commits_root", null, "no seal yet"), C("ots_attestations", null, "no seal yet");
  else if (!seal.ots_base64) C("ots_commits_root", null, "the day's .ots bytes are not in the bundle"), C("ots_attestations", null, "no .ots");
  else {
    try {
      const o = await otsInspect(b64(seal.ots_base64)); att = o.attestations;
      C("ots_commits_root", o.digest === seal.root, o.digest === seal.root ? "the timestamp proof is over exactly this root" : "the .ots timestamps " + o.digest + ", not the root " + seal.root);
      const btc = att.filter((a) => a.kind === "bitcoin"), pend = att.filter((a) => a.kind === "pending");
      if (btc.length) C("ots_attestations", true, "anchored in Bitcoin block " + Array.from(new Set(btc.map((a) => a.height))).join(", ") + "; the proof commits the root into merkle root " + btc[0].merkle_root + " - compare it with that block's header on any Bitcoin node", { bitcoin: btc });
      else if (pend.length) C("ots_attestations", null, "submitted to " + pend.map((a) => a.calendar).join(", ") + ", not yet confirmed in a Bitcoin block (usually within a few hours); download the bundle again later", { pending: pend });
      else C("ots_attestations", false, "the proof carries no attestation");
    } catch (e) { C("ots_commits_root", false, String(e && e.message || e)); C("ots_attestations", null, "could not parse the proof"); }
  }
  // 6. sealed roots (manifest v3)
  const roots = manObj && manObj.roots;
  const rootCheck = async (id, label, sealed, objs, missingWhy) => {
    if (!roots) return C(id, null, "the manifest predates sealed roots (manifest v3, 2026-09-30)");
    if (!sealed) return C(id, null, "the manifest carries no " + label + " root");
    if (!Array.isArray(objs)) return C(id, null, missingWhy);
    const got = await merkleRootHex(await Promise.all(objs.map(leafHex)));
    const want = sealed.root || "";
    C(id, got === want && objs.length === sealed.n, got === want && objs.length === sealed.n ? objs.length + " " + label + " leaves recompute to the sealed root" : "the " + label + " leaves give " + (got || "(empty)").slice(0, 16) + "… over " + objs.length + ", the manifest sealed " + want.slice(0, 16) + "… over " + sealed.n);
  };
  await rootCheck("experience_root", "fetch", roots && roots.experiences, manObj && Array.isArray(manObj.experiences) ? manObj.experiences : null, "the manifest body is not in the bundle");
  await rootCheck("decision_root", "decision", roots && roots.decisions, rec.decision_leaves, rec.leaves_withheld || "decision leaves not included");
  await rootCheck("lineage_root", "lineage", roots && roots.lineage, rec.lineage_leaves, rec.leaves_withheld || "lineage leaves not included");
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  return {
    verified: ran.length > 0 && failed.length === 0,
    summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed; " + (checks.length - ran.length) + " could not run (reasons given)",
    report: rep, checks,
    experiences: manObj && Array.isArray(manObj.experiences) ? manObj.experiences.length : null,
    note: "Every check above used only bytes inside the bundle. Only the Bitcoin block header has to come from outside, and from any source you trust."
  };
}

// ── remediation receipts ─────────────────────────────────────────────────────────────────────────────────
// A receipt joins the record before a fix, the declared fix, when it went live, the record after it and the
// verification verdict, and is signed with the observer's Ed25519 key over sha256(canonical JSON of everything but
// receipt_id and signature). Change any field and receipt_hash fails; swap the key and key_id fails.
const RCPT_SIG_PREFIX = "crawlcheck-receipt-v1\n";
async function verifyReceipt0(rc) {
  if (!subtle) throw new Error("this runtime has no WebCrypto (crypto.subtle)");
  const checks = [];
  const C = (id, ok, why) => checks.push({ id, ok, why });
  if (!rc || rc.kind !== "crawlcheck-remediation-receipt" || !rc.signature) throw new Error("not a CrawlCheck remediation receipt");
  const body = {}; for (const k of Object.keys(rc)) if (k !== "receipt_id" && k !== "signature") body[k] = rc[k];
  const sha = hex(await sha256(enc.encode(canon(body))));
  C("receipt_hash", sha === rc.signature.receipt_sha256 && rc.receipt_id === "rc1:" + sha, sha === rc.signature.receipt_sha256 ? "the receipt hashes to its id " + sha.slice(0, 16) + "…" : "the receipt hashes to " + sha + ", it claims " + rc.signature.receipt_sha256 + " - a field was changed");
  const jwk = rc.issuer && rc.issuer.key && rc.issuer.key.jwk;
  if (!jwk || !jwk.x) { C("key_id", null, "no key in the receipt"); C("signature", null, "no key to check against"); }
  else {
    const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: jwk.crv, kty: jwk.kty, x: jwk.x }))));
    C("key_id", thumb === rc.signature.kid, thumb === rc.signature.kid ? "public key thumbprint (RFC 7638) is the key id " + thumb.slice(0, 12) + "…; the same id is published in " + (rc.issuer.key.published_in || "the key directory") : "thumbprint " + thumb + " does not match key id " + rc.signature.kid);
    const msg = RCPT_SIG_PREFIX + sha;
    let ok = null;
    try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x: jwk.x }, { name: "Ed25519" }, false, ["verify"]); ok = rc.signature.message === msg && await subtle.verify({ name: "Ed25519" }, pk, b64(rc.signature.sig), enc.encode(msg)); }
    catch (e) { ok = null; C("signature", null, "this runtime cannot verify Ed25519: " + (e && e.message || e)); }
    if (ok !== null) C("signature", ok, ok ? "Ed25519 signature over the receipt digest verifies" : "signature does NOT verify over this content");
  }
  C("key_published", null, "offline this cannot be decided: compare key id " + (rc.signature.kid || "?") + " with the ids published at " + ((rc.issuer && rc.issuer.key && rc.issuer.key.published_in) || "the key directory") + " (a receipt re-signed with any other key passes every other check here)");
  const b = rc.before || {}, a = rc.after || {};
  C("order", !!(b.scanned_at && a.scanned_at && Date.parse(b.scanned_at) < Date.parse(a.scanned_at)), b.scanned_at && a.scanned_at ? "before " + b.scanned_at + " precedes after " + a.scanned_at : "a scan time is missing");
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  return { verified: ran.length > 0 && failed.length === 0, summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed",
    receipt: { id: rc.receipt_id, domain: rc.subject && rc.subject.domain, verdict: rc.verification && rc.verification.verdict, before: b.report_id, after: a.report_id }, checks,
    note: "Every check used only the receipt itself. To confirm the key is CrawlCheck's, compare its id with " + ((rc.issuer && rc.issuer.key && rc.issuer.key.published_in) || "the published key directory") + "; to see the evidence behind each side, verify the two reports' bundles." };
}

// ── evidence exports ─────────────────────────────────────────────────────────────────────────────────────
// An export is one signed file for a domain and a period: records[] each carrying a full evidence bundle,
// receipts[], the certificate days and the seals. The file is signed over sha256(canonical JSON of everything but
// export_sha256 and signature); then every bundle and every receipt inside it is verified exactly as it would be on
// its own. One altered byte anywhere fails export_hash; one altered bundle fails its own checks.
const EXPORT_SIG_PREFIX = "crawlcheck-evidence-export-v1\n";
async function verifyExport0(x) {
  if (!subtle) throw new Error("this runtime has no WebCrypto (crypto.subtle)");
  const checks = [];
  const C = (id, ok, why) => checks.push({ id, ok, why });
  if (!x || x.kind !== "crawlcheck-evidence-export") throw new Error("not a CrawlCheck evidence export");
  const body = {}; for (const k of Object.keys(x)) if (k !== "export_sha256" && k !== "signature") body[k] = x[k];
  const sha = hex(await sha256(enc.encode(canon(body))));
  if (!x.signature) { C("export_hash", sha === x.export_sha256, sha === x.export_sha256 ? "the file hashes to its stated digest " + sha.slice(0, 16) + "…" : "the file hashes to " + sha + ", it states " + x.export_sha256 + " - a field was changed"); C("signature", null, "the export is unsigned (the observer had no signing key when it was made)"); }
  else {
    C("export_hash", sha === x.export_sha256 && x.signature.export_sha256 === sha, sha === x.export_sha256 ? "the file hashes to its stated digest " + sha.slice(0, 16) + "…" : "the file hashes to " + sha + ", it states " + x.export_sha256 + " - a field was changed");
    const jwk = x.signature.key && x.signature.key.jwk;
    if (!jwk || !jwk.x) { C("key_id", null, "no key in the export"); C("signature", null, "no key to check against"); }
    else {
      const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: jwk.crv, kty: jwk.kty, x: jwk.x }))));
      C("key_id", thumb === x.signature.kid, thumb === x.signature.kid ? "public key thumbprint (RFC 7638) is the key id " + thumb.slice(0, 12) + "…; the same id is published in " + (x.signature.key.published_in || "the key directory") : "thumbprint " + thumb + " does not match key id " + x.signature.kid);
      const msg = EXPORT_SIG_PREFIX + sha;
      let ok = null;
      try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x: jwk.x }, { name: "Ed25519" }, false, ["verify"]); ok = x.signature.message === msg && await subtle.verify({ name: "Ed25519" }, pk, b64(x.signature.sig), enc.encode(msg)); }
      catch (e) { ok = null; C("signature", null, "this runtime cannot verify Ed25519: " + (e && e.message || e)); }
      if (ok !== null) C("signature", ok, ok ? "Ed25519 signature over the export digest verifies" : "signature does NOT verify over this content");
    }
    C("key_published", null, "offline this cannot be decided: compare key id " + (x.signature.kid || "?") + " with the ids published at " + ((x.signature.key && x.signature.key.published_in) || "the key directory"));
  }
  const records = [];
  for (const rec of (Array.isArray(x.records) ? x.records : [])) {
    if (!rec || !rec.bundle) { records.push({ id: rec && rec.id, verified: null, failed: [], why: (rec && rec.missing) || "no bundle" }); continue; }
    let v = null, why = null;
    try { v = await verifyBundle(rec.bundle); } catch (e) { why = String(e && e.message || e); }
    const same = !!(v && rec.bundle.report && rec.bundle.report.id === rec.id && rec.bundle.report.domain === x.domain);
    records.push({ id: rec.id, scanned_at: rec.scanned_at, verified: v ? (v.verified && same) : null, failed: v ? v.checks.filter((c) => c.ok === false).map((c) => c.id).concat(same ? [] : ["bundle_names_another_record"]) : [], ran: v ? v.checks.filter((c) => c.ok !== null).length : 0, why });
  }
  const receipts = [];
  for (const rc of (Array.isArray(x.receipts) ? x.receipts : [])) {
    let v = null, why = null;
    try { v = await verifyReceipt(rc); } catch (e) { why = String(e && e.message || e); }
    receipts.push({ id: rc && rc.receipt_id, verified: v ? v.verified : null, failed: v ? v.checks.filter((c) => c.ok === false).map((c) => c.id) : [], why });
  }
  const rf = records.filter((r) => r.verified === false).length, cf = receipts.filter((r) => r.verified === false).length;
  C("records", records.length ? rf === 0 : null, records.length ? (rf ? rf + " of " + records.length + " bundles FAILED" : records.length + " bundle" + (records.length === 1 ? "" : "s") + " inside the file verified on their own (" + records.filter((r) => r.verified === true).length + " fully, " + records.filter((r) => r.verified === null).length + " with checks that could not run)") : "the file carries no records");
  C("receipts", receipts.length ? cf === 0 : null, receipts.length ? (cf ? cf + " of " + receipts.length + " receipts FAILED" : receipts.length + " receipt" + (receipts.length === 1 ? "" : "s") + " verified") : "the file carries no receipts");
  const cnt = x.counts || {};
  C("counts", (Array.isArray(x.records) ? x.records.length : 0) === ((cnt.records || 0) + (cnt.missing || 0)) && (Array.isArray(x.receipts) ? x.receipts.length : 0) === (cnt.receipts || 0), "the file's own counts match what it carries (" + (Array.isArray(x.records) ? x.records.length : 0) + " records, " + (Array.isArray(x.receipts) ? x.receipts.length : 0) + " receipts)");
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  return { verified: ran.length > 0 && failed.length === 0, summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed",
    export: { domain: x.domain, window: x.window, records: records.length, receipts: receipts.length, generated_at: x.generated_at }, checks, records, receipts,
    note: "Every check used only the file itself. To confirm the key is CrawlCheck's, compare its id with " + ((x.signature && x.signature.key && x.signature.key.published_in) || "the published key directory") + "." };
}

// ── dispute resolutions ──────────────────────────────────────────────────────────────────────────────────
// A resolution records a dispute against one finding, the three readings that settled it (the rule replayed over
// the disputed record's sealed bytes, the primary observer's fresh audit, every independent observer's reading) and
// the verdict. It is signed like a receipt. verdict_derivable runs the published decision table (dspVerdict, the
// same function the Worker runs) over the recorded readings: a resolution whose verdict was edited, or whose
// readings were edited to fit it, fails here even before the signature does.
const DSP_SIG_PREFIX = "crawlcheck-dispute-resolution-v1\n";
export function dspVerdict(m) {
  // The published decision table. Inputs are only what the resolution itself records, so a verifier can re-derive
  // the verdict from the record: replay of the rule over the bytes the disputed scan received, the primary
  // observer's fresh re-measurement, and every independent observer's reading after the dispute was filed.
  const rp = m && m.replay ? m.replay.reproduces : null;
  const obs = (m && Array.isArray(m.observers)) ? m.observers : [];
  const prim = obs.filter(function (o) { return o && o.role === "primary"; })[0] || null;
  const ind = obs.filter(function (o) { return o && o.role === "independent"; });
  if (rp === false) return { verdict: "corrected", effect: "withdrawn", rule: "the rule, run again over the exact bytes the disputed scan received, does not hold: the finding was wrong when it was made" };
  if (!prim || prim.finding_present === null || prim.finding_present === undefined) return { verdict: "inconclusive", effect: "stands_unresolved", rule: "the primary observer could not re-measure the site" };
  const decided = ind.filter(function (o) { return o.finding_present === true || o.finding_present === false; });
  const agreeIn = ind.filter(function (o) { return o.finding_present !== true && o.finding_present !== false && o.inputs_agree === true; });
  const differIn = ind.filter(function (o) { return o.finding_present !== true && o.finding_present !== false && o.inputs_agree === false; });
  if (!decided.length && !agreeIn.length && !differIn.length) return { verdict: "inconclusive", effect: "stands_unresolved", rule: "no independent observer gave a usable reading before the deadline" };
  if (decided.some(function (o) { return o.finding_present !== prim.finding_present; }) || differIn.length) return { verdict: "vantage_dependent", effect: "stands_with_scope", rule: "an independent network received something different from the primary observer: the finding holds only for the network that saw it" };
  if (prim.finding_present === true) return { verdict: "upheld", effect: "stands", rule: "the rule holds over the original bytes and the condition is still present from every network that re-measured it" };
  if (rp === true) return { verdict: "fixed_since", effect: "stands_for_record", rule: "the rule holds over the original bytes, and every network now finds the condition gone: correct when made, fixed since" };
  return { verdict: "no_longer_observed", effect: "stands_for_record", rule: "every network now finds the condition gone, and the original bytes could not be re-checked, so whether it was ever wrong cannot be decided" };
}
async function verifyResolution0(x) {
  if (!subtle) throw new Error("this runtime has no WebCrypto (crypto.subtle)");
  const checks = [];
  const C = (id, ok, why) => checks.push({ id, ok, why });
  if (!x || x.kind !== "crawlcheck-dispute-resolution" || !x.signature) throw new Error("not a CrawlCheck dispute resolution");
  const body = {}; for (const k of Object.keys(x)) if (k !== "resolution_id" && k !== "signature") body[k] = x[k];
  const sha = hex(await sha256(enc.encode(canon(body))));
  C("resolution_hash", sha === x.signature.resolution_sha256 && x.resolution_id === "dr1:" + sha, sha === x.signature.resolution_sha256 ? "the resolution hashes to its id " + sha.slice(0, 16) + "…" : "the resolution hashes to " + sha + ", it claims " + x.signature.resolution_sha256 + " - a field was changed");
  const jwk = x.issuer && x.issuer.key && x.issuer.key.jwk;
  if (!jwk || !jwk.x) { C("key_id", null, "no key in the resolution"); C("signature", null, "no key to check against"); }
  else {
    const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: jwk.crv, kty: jwk.kty, x: jwk.x }))));
    C("key_id", thumb === x.signature.kid, thumb === x.signature.kid ? "public key thumbprint (RFC 7638) is the key id " + thumb.slice(0, 12) + "…; the same id is published in " + (x.issuer.key.published_in || "the key directory") : "thumbprint " + thumb + " does not match key id " + x.signature.kid);
    const msg = DSP_SIG_PREFIX + sha;
    let ok = null;
    try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x: jwk.x }, { name: "Ed25519" }, false, ["verify"]); ok = x.signature.message === msg && await subtle.verify({ name: "Ed25519" }, pk, b64(x.signature.sig), enc.encode(msg)); }
    catch (e) { ok = null; C("signature", null, "this runtime cannot verify Ed25519: " + (e && e.message || e)); }
    if (ok !== null) C("signature", ok, ok ? "Ed25519 signature over the resolution digest verifies" : "signature does NOT verify over this content");
  }
  C("key_published", null, "offline this cannot be decided: compare key id " + (x.signature.kid || "?") + " with the ids published at " + ((x.issuer && x.issuer.key && x.issuer.key.published_in) || "the key directory"));
  const dv = dspVerdict(x.remeasurement || {});
  const said = x.decision || {};
  C("verdict_derivable", dv.verdict === said.verdict && dv.effect === said.effect, "the decision table over the recorded readings gives " + dv.verdict + " / " + dv.effect + "; the record says " + (said.verdict || "?") + " / " + (said.effect || "?"));
  const obs = (x.remeasurement && x.remeasurement.observers) || [];
  const t0 = x.dispute && Date.parse(x.dispute.ownership && x.dispute.ownership.at || x.dispute.filed_at);
  const late = obs.filter((o) => o && o.at && isFinite(t0) && Date.parse(o.at) < t0);
  C("readings_after_dispute", obs.length ? late.length === 0 : null, obs.length ? (late.length ? late.length + " reading(s) predate the dispute: a re-measurement must come after it" : "every reading (" + obs.length + ") was taken after the dispute was accepted") : "no readings recorded");
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  return { verified: ran.length > 0 && failed.length === 0, summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed",
    resolution: { id: x.resolution_id, dispute: x.dispute_id, domain: x.subject && x.subject.domain, code: x.subject && x.subject.finding && x.subject.finding.code, verdict: said.verdict, effect: said.effect, observers: obs.map((o) => o.observer_id + "@" + (o.network_class || "?")) }, checks,
    note: "Every check used only the resolution itself. To confirm the key is CrawlCheck's, compare its id with " + ((x.issuer && x.issuer.key && x.issuer.key.published_in) || "the published key directory") + "; to check a reading, open the record or the signed observation it links." };
}

// tl1: the Resolve log's leaf content and RFC 9162 inclusion check (the same rules the log publishes).
function tlContentV(doc) {
  const a = doc && doc.actions || {}, acts = {};
  Object.keys(a).sort().forEach((k) => { const x = a[k] || {}; acts[k] = { decision: x.decision || null, doubts: (Array.isArray(x.doubts) ? x.doubts : []).slice().sort() }; });
  return { log: "crawlcheck-resolve-log-v1", domain: doc && doc.domain || null, status: doc && doc.status || null, actions: acts,
    report: doc && doc.evidence && doc.evidence.report || null, observed_at: doc && doc.freshness && doc.freshness.observed_at || null,
    incident: doc && doc.incident && doc.incident.since || null, impersonation: doc && doc.impersonation && doc.impersonation.verdict === "suspected" ? "suspected" : null,
    high_risk_pending: doc && doc.high_risk_fields && Array.isArray(doc.high_risk_fields.pending) ? doc.high_risk_fields.pending.map((p) => p.field).sort() : [] };
}
export async function rfcRootV(leafHex, index, size, path) {
  if (!(size > 0) || !(index >= 0) || index >= size) return null;
  let fn = index, sn = size - 1, r = unhex(leafHex);
  for (const p of path) {
    if (sn === 0) return null;
    if ((fn & 1) === 1 || fn === sn) { r = await sha256(cat(cat(new Uint8Array([1]), unhex(p)), r)); if ((fn & 1) === 0) { while ((fn & 1) === 0 && fn !== 0) { fn >>= 1; sn >>= 1; } } }
    else r = await sha256(cat(cat(new Uint8Array([1]), r), unhex(p)));
    fn >>= 1; sn >>= 1;
  }
  return sn === 0 ? hex(r) : null;
}
// tv1: threshold-signed verdicts. Every signer is checked on its own: its Ed25519 signature over its envelope, the
// binding of its key to its identity (GitHub's RS256 token, a self-signed enrolment, or the published key directory),
// and the field values recomputed from its envelope. The verdict holds when signers on at least k distinct networks pass.
const TV_OBS_PREFIX = "crawlcheck-observation-v1\n", TV_ENROL_PREFIX = "crawlcheck-observer-enrol-v1\n";
function tvFinalV(u) { try { const x = new URL(u); return x.hostname.toLowerCase().replace(/^www\./, "") + (x.pathname.replace(/\/+$/, "") || "/"); } catch (e) { return null; } }
export function tvReachV(envelope) {
  return (envelope && Array.isArray(envelope.fetches) ? envelope.fetches : []).map((f) => ({ identity: String(f.identity), status: typeof f.status === "number" ? f.status : null, final: f.url_final ? tvFinalV(f.url_final) : null, failed: !!f.error }))
    .sort((a, b) => (a.identity < b.identity ? -1 : a.identity > b.identity ? 1 : 0));
}
async function tvEd(x, msg, sig) { try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x }, { name: "Ed25519" }, false, ["verify"]); return await subtle.verify({ name: "Ed25519" }, pk, b64(sig), enc.encode(msg)); } catch (e) { return null; } }
async function tvJwt(token, jwk) {
  const p = String(token || "").split("."); if (p.length !== 3 || !jwk || jwk.kty !== "RSA") return { ok: false };
  let h, c; try { h = JSON.parse(new TextDecoder().decode(b64(p[0]))); c = JSON.parse(new TextDecoder().decode(b64(p[1]))); } catch (e) { return { ok: false }; }
  if (h.alg !== "RS256" || (jwk.kid && h.kid !== jwk.kid)) return { ok: false, claims: c };
  try { const k = await subtle.importKey("jwk", { kty: "RSA", n: jwk.n, e: jwk.e, alg: "RS256", ext: true }, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, false, ["verify"]); return { ok: await subtle.verify("RSASSA-PKCS1-v1_5", k, b64(p[2]), enc.encode(p[0] + "." + p[1])), claims: c, kid: h.kid }; } catch (e) { return { ok: null, claims: c }; }
}
export async function verifyThresholdSigners(x) {
  const st = x.statement || {}, want = canon(st.values || []), rows = [];
  for (const s of Array.isArray(x.signers) ? x.signers : []) {
    const r = { observer_id: s.observer_id, network: s.network && s.network.class || null, signature: false, binding: false, values_match: false, why: [] };
    const ev = s.envelope || {}, key = ev.key || {};
    r.signature = !!(await tvEd(key.x, TV_OBS_PREFIX + canon(ev), s.sig)); if (!r.signature) r.why.push("signature does not verify under the envelope key");
    const b = s.binding || {};
    if (b.kind === "github_oidc") {
      const j = await tvJwt(b.token, b.github_key), c = j.claims || {};
      const aud = "crawlcheck-observer:" + b64u(await sha256(b64(key.x || "")));
      r.binding = j.ok === true && c.iss === "https://token.actions.githubusercontent.com" && c.repository === b.repository && String(c.job_workflow_ref || c.workflow_ref || "").indexOf(b.workflow_ref_prefix || "\u0000") === 0 && (Array.isArray(c.aud) ? c.aud : [c.aud]).includes(aud) && (!c.runner_environment || c.runner_environment === "github-hosted");
      r.github_kid = j.kid || null; if (!r.binding) r.why.push("GitHub token does not bind this key to the observer workflow");
    } else if (b.kind === "enrolled_ed25519") {
      const q = b.enrolment && b.enrolment.request;
      r.binding = !!(b.key && b.key.x === key.x && q && q.observer_id === s.observer_id && q.key && q.key.x === key.x && await tvEd(key.x, TV_ENROL_PREFIX + canon(q), b.enrolment.sig));
      if (!r.binding) r.why.push("the key is not the one this observer enrolled with");
    } else if (b.kind === "published_key_directory") {
      const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: "Ed25519", kty: "OKP", x: key.x }))));
      r.binding = thumb === b.kid && thumb === (x.signature && x.signature.kid); r.kid = thumb; if (!r.binding) r.why.push("key thumbprint is not the stated (published) key id");
    } else r.why.push("unknown binding " + b.kind);
    r.values_match = canon(tvReachV(ev)) === want; if (!r.values_match) r.why.push("the values in this envelope differ from the statement");
    r.passes = r.signature && r.binding && r.values_match; rows.push(r);
  }
  return rows;
}
// ── data inventories, export parts and deletion records ─────────────────────────────────────────────────────────
// Signed over sha256(canonical JSON of every field except sha256 and signature) with the prefix below. A deletion
// record carries counts per family and the digest of the sorted deleted-key list, never the data.
const DATA_SIG_PREFIX = "crawlcheck-data-v1\n";
async function verifyDataDoc0(x) {
  if (!subtle) throw new Error("this runtime has no WebCrypto (crypto.subtle)");
  const checks = [];
  const C = (id, ok, why) => checks.push({ id, ok, why });
  if (!x || !/^crawlcheck-(data-inventory|data-export-part|deletion-record|resolve|decision|snapshot|visitor|conduct|traffic-index|anchor|anchor-check|content-clock|origin|claims|read-equivalence|rights|rights-feed|mcp-scan|mcp-lock|mcp-lock-check|mcp-shadow|mcp-auth|impersonation|task-canaries|agent-safety-benchmark|compliance-pack|share-of-preflight|incident|revocations|high-risk|log-sth|threshold-verdict|resolve-as-of|sbom|query|mcp-reliability-server|mcp-reliability-host|removal-record|status-entry|backup-manifest|backup-key-check|business-truth)$/.test(String(x.kind)) && !(/^crawlcheck-[a-z0-9-]{2,60}$/.test(String(x && x.kind)) && x.signature && String(x.signature.message || "").indexOf(DATA_SIG_PREFIX) === 0) || !x.signature) throw new Error("not a CrawlCheck data inventory, export part, deletion record, resolve answer, decision receipt, snapshot manifest, visitor verdict, conduct record, traffic index, citation anchor, content clock, origin decision, claim ledger, read-equivalence certificate, rights answer, rights-feed manifest, MCP tool scan, MCP lockfile or lockfile check");
  const body = {}; for (const k of Object.keys(x)) if (k !== "sha256" && k !== "signature") body[k] = x[k];
  const sha = hex(await sha256(enc.encode(canon(body))));
  C("document_hash", sha === x.sha256, sha === x.sha256 ? "the document hashes to its stated digest " + sha.slice(0, 16) + "…" : "the document hashes to " + sha + ", it states " + x.sha256 + " - a field was changed");
  const jwk = x.signature.key && x.signature.key.jwk;
  if (!jwk || !jwk.x) { C("key_id", null, "no key in the document"); C("signature", null, "no key to check against"); }
  else {
    const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: jwk.crv, kty: jwk.kty, x: jwk.x }))));
    C("key_id", thumb === x.signature.kid, thumb === x.signature.kid ? "public key thumbprint (RFC 7638) is the key id " + thumb.slice(0, 12) + "…" : "thumbprint " + thumb + " does not match key id " + x.signature.kid);
    const msg = DATA_SIG_PREFIX + sha;
    let ok = null;
    try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x: jwk.x }, { name: "Ed25519" }, false, ["verify"]); ok = x.signature.message === msg && await subtle.verify({ name: "Ed25519" }, pk, b64(x.signature.sig), enc.encode(msg)); }
    catch (e) { ok = null; C("signature", null, "this runtime cannot verify Ed25519: " + (e && e.message || e)); }
    if (ok !== null) C("signature", ok, ok ? "Ed25519 signature over the document digest verifies" : "signature does NOT verify over this content");
  }
  C("key_published", null, "offline this cannot be decided: compare key id " + (x.signature.kid || "?") + " with the ids published at " + ((x.signature.key && x.signature.key.published_in) || "the key directory"));
  if (x.kind === "crawlcheck-data-export-part" && x.inventory) { let iv = null; try { iv = await verifyDataDoc(x.inventory); } catch (e) { iv = null; } C("inventory", !!(iv && iv.verified && x.inventory.sha256 === x.inventory_sha256), iv && iv.verified ? "the part carries the signed inventory it names" : "the inventory inside this part does not verify or is not the one named"); }
  if (x.kind === "crawlcheck-decision") { const r = x.resolve || {}; C("resolve_bound", /^[0-9a-f]{64}$/.test(String(r.sha256 || "")) && typeof r.kid === "string" && r.kid.length > 0, /^[0-9a-f]{64}$/.test(String(r.sha256 || "")) ? "the decision names the resolve answer it was made from (" + String(r.sha256).slice(0, 16) + "…), signed by key " + String(r.kid).slice(0, 12) + "… (online, it must be a published key)" : "the decision does not name a resolve answer digest"); C("decision_known", ["allow", "warn", "require_confirmation", "block", "unsupported"].indexOf(x.decision) >= 0, "decision is " + x.decision); }
  if (x.kind === "crawlcheck-snapshot") { const ps = Array.isArray(x.parts) ? x.parts : []; C("parts", ps.length > 0 && ps.every((p) => /^part-\d{4}\.ndjson\.gz$/.test(String(p.path)) && /^[0-9a-f]{64}$/.test(String(p.sha256)) && p.records > 0) && ps.reduce((a, p) => a + p.records, 0) === x.records, ps.reduce((a, p) => a + (p.records || 0), 0) === x.records ? ps.length + " parts, " + x.records + " records; check each downloaded part with sha256sum against its sha256, then each line with this tool" : "the parts' record counts do not add up to records (" + x.records + ")"); }
  if (x.kind === "crawlcheck-resolve" && Array.isArray(x.absences)) {   // sa1: every absence states what was checked, from where and when
    const bad = x.absences.filter((a) => !(a && a.what && (a.result === "absent" || a.result === "none") && a.at && !isNaN(Date.parse(a.at)) && a.from && a.from.observer && typeof a.scope === "string" && Array.isArray(a.checked) && a.checked.length && a.checked.every((c) => c && c.method && (a.result !== "absent" || c.status === undefined || c.status === 404 || c.status === 410))));
    C("absences_scoped", bad.length === 0, x.absences.length + " absence record(s)" + (x.absences.length ? " (" + x.absences.map((a) => a.what).join(", ") + ")" : "") + (bad.length ? "; " + bad.length + " lack a definite check, a time, a vantage or a scope" : ", each naming what was requested, the answer, the vantage and the time"));
  }
  if (x.kind === "crawlcheck-resolve" && x.transparency) {   // tl1: the answer's place in the Resolve transparency log
    const t = x.transparency, leaf = hex(await sha256(cat(new Uint8Array([0]), enc.encode(canon(tlContentV(x))))));
    C("log_leaf", leaf === t.leaf_hash, leaf === t.leaf_hash ? "the answer's decision content hashes to its log leaf " + leaf.slice(0, 16) + "…" : "the decision content hashes to " + leaf + ", the answer names " + t.leaf_hash);
    if (t.state === "included" && t.sth) {
      const root = await rfcRootV(leaf, t.index, t.batch_size, t.path || []);
      C("log_inclusion", root !== null && root === t.sth.batch_root, root === t.sth.batch_root ? "the leaf folds along its " + (t.path || []).length + "-step path to batch " + t.batch + "'s root (RFC 9162)" : "the inclusion path does NOT lead to the signed batch root");
      let sv = null; try { sv = await verifyDataDoc0(t.sth); } catch (e) { sv = null; }
      const same = true;   // kp3: the log head may be signed by the log key; online, its key must be published (trustOf)
      C("log_head_signature", !!(sv && sv.verified) && t.sth.kind === "crawlcheck-log-sth" && t.sth.batch === t.batch && same, sv && sv.verified ? "batch " + t.batch + "'s signed tree head verifies (tree size " + t.sth.tree_size + ", chained to " + String(t.sth.prev_sth_sha256 || "genesis").slice(0, 16) + "…)" : "the signed tree head does NOT verify");
    } else C("log_inclusion", null, "pending: the leaf is queued and merged within " + (t.merged_within_minutes || 30) + " minutes; fetch the proof at " + (t.proof || "the log"));
  }
  if (x.kind === "crawlcheck-business-record") {   // bbr1: the record's own hash, its log leaf, and its inclusion in the signed log
    const rs = hex(await sha256(enc.encode(canon(x.record || {}))));
    C("record_hash", rs === x.record_sha256, rs === x.record_sha256 ? "the record hashes to its record_sha256 " + rs.slice(0, 16) + "…" : "the record hashes to " + rs + ", the document says " + x.record_sha256);
    const t = x.transparency || {};
    const leaf = hex(await sha256(cat(new Uint8Array([0]), enc.encode(canon({ log: t.log_id, kind: "business-record", domain: x.domain, version: x.version, record_sha256: rs })))));
    C("log_leaf", leaf === t.leaf_hash, leaf === t.leaf_hash ? "domain, version and record hash make log leaf " + leaf.slice(0, 16) + "…" : "they make leaf " + leaf + ", the document names " + t.leaf_hash);
    if (t.state === "included" && t.sth && typeof t.sth === "object") {
      const root = await rfcRootV(leaf, t.index, t.batch_size, t.path || []);
      C("log_inclusion", root !== null && root === t.sth.batch_root, root === t.sth.batch_root ? "the leaf folds along its " + (t.path || []).length + "-step path to batch " + t.batch + "'s root (RFC 9162)" : "the inclusion path does NOT lead to the signed batch root");
      let sv = null; try { sv = await verifyDataDoc0(t.sth); } catch (e) { sv = null; }
      const same = true;   // kp3: the log head may be signed by the log key; online, its key must be published (trustOf)
      C("log_head_signature", !!(sv && sv.verified) && t.sth.kind === "crawlcheck-log-sth" && t.sth.batch === t.batch && same, sv && sv.verified ? "batch " + t.batch + "'s signed tree head verifies (tree size " + t.sth.tree_size + ")" : "the signed tree head does NOT verify");
    } else C("log_inclusion", null, "pending: the leaf is queued and merged within " + (t.merged_within_minutes || 30) + " minutes; fetch the proof at " + (t.proof || "the log"));
  }
  if (x.kind === "crawlcheck-resolve-as-of") {   // ah1: the answer in force at as_of, as signed then
    let av = null; try { av = await verifyDataDoc0(x.answer); } catch (e) { av = null; }
    C("answer_signature", !!(av && av.verified) && !!x.answer && x.answer.kind === "crawlcheck-resolve" && x.answer.domain === x.domain, av && av.verified ? "the archived answer verifies on its own (signed " + (x.answer.signature && x.answer.signature.signed_at) + ")" : "the archived answer does NOT verify");
    const leaf = x.answer ? hex(await sha256(cat(new Uint8Array([0]), enc.encode(canon(tlContentV(x.answer)))))) : null;
    C("answer_leaf", leaf === x.leaf_hash, leaf === x.leaf_hash ? "the answer's decision content is leaf " + String(leaf).slice(0, 16) + "…" : "the answer's content hashes to " + leaf + ", not the stated leaf");
    const w = x.in_force || {}, t = Date.parse(x.as_of);
    C("in_force_window", Date.parse(w.from) <= t && (w.until == null || t < Date.parse(w.until)), "as_of " + x.as_of + " falls in [" + w.from + ", " + (w.until || "now") + ")");
    const pr = x.log_inclusion;
    if (pr && pr.sth) { const root = await rfcRootV(x.leaf_hash, pr.index, pr.batch_size, pr.path || []); let sv = null; try { sv = await verifyDataDoc0(pr.sth); } catch (e) { sv = null; }
      C("log_inclusion", root !== null && root === pr.sth.batch_root && !!(sv && sv.verified), root === pr.sth.batch_root ? "the leaf is in batch " + pr.batch + " of the signed log (head created " + pr.sth.created_at + ")" : "the inclusion proof does NOT reach the signed batch root"); }
    else C("log_inclusion", null, "not merged into the log yet; merged within 30 minutes of first appearing");
  }
  if (x.kind === "crawlcheck-threshold-verdict") {   // tv1
    const rows = await verifyThresholdSigners(x), k = x.threshold && x.threshold.k || 2;
    rows.forEach((r) => C("signer:" + r.observer_id, r.passes, r.passes ? "signature, key binding (" + (x.signers.find((s) => s.observer_id === r.observer_id).binding.kind) + ") and recomputed " + (x.statement && x.statement.family) + " values all check (network " + r.network + ")" : r.why.join("; ")));
    const nets = new Set(rows.filter((r) => r.passes).map((r) => r.network));
    C("threshold", nets.size >= k, nets.size + " distinct network(s) signed identical values (" + [...nets].join(", ") + "); the verdict needs " + k);
    C("key_bindings_online", null, "offline the bindings are checked against keys the document carries; online, compare the GitHub key id with " + "https://token.actions.githubusercontent.com/.well-known/jwks and enrolled keys with https://crawlcheck.io/.well-known/crawlcheck-observers.json");
  }
  if (x.kind === "crawlcheck-sbom") {   // sbom1: the embedded CycloneDX document is the one the signature covers, and it describes the named artifact
    const sb = x.sbom || {}, root = sb.metadata && sb.metadata.component || {}, rh = (root.hashes || []).filter((h) => h.alg === "SHA-256").map((h) => h.content)[0];
    C("sbom_hash", x.sbom_sha256 === hex(await sha256(enc.encode(canon(sb)))), "sha256 of the canonical CycloneDX document equals sbom_sha256");
    C("artifact_bound", !!x.artifact && /^[0-9a-f]{64}$/.test(String(x.artifact.sha256 || "")) && rh === x.artifact.sha256, "artifact.sha256 equals the SBOM root component's SHA-256 (" + String(x.artifact && x.artifact.sha256 || "").slice(0, 16) + "\u2026)");
    C("cyclonedx", sb.bomFormat === "CycloneDX" && /^1\.[4-9]$/.test(String(sb.specVersion || "")) && Array.isArray(root.components), "CycloneDX " + sb.specVersion + ", " + (root.components || []).length + " files hashed, runtime deps: " + ((((sb.dependencies || [])[0] || {}).dependsOn || []).join(", ") || "none"));
  }
  if (x.kind === "crawlcheck-deletion-record") C("counts", (x.deleted || []).reduce((a, f) => a + (f.keys || 0), 0) === x.deleted_total, "the per-family counts add up to deleted_total (" + x.deleted_total + ")");
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  return { verified: ran.length > 0 && failed.length === 0, summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed", document: { kind: x.kind, domain: x.domain, sha256: x.sha256 }, checks };
}

// ── command line ─────────────────────────────────────────────────────────────
// Run as a script under ANY file name (a browser saves a second download as "crawlcheck-verify (1).mjs"); imported, it
// stays a library. The old test matched the file name only, so a renamed copy exited 0 having checked nothing.
// vt1: integrity, issuer trust and completeness, reported apart. Same semantics as @crawlcheck/sdk and the Python package.
// kp3 (2026-10-10): keys a document leans on besides its own signer (the resolve answer a decision names, the log head
// an answer carries). Since keys are separated by purpose these can differ; online, every one must be published.
export function relatedKids(x) {
  const out = [];
  try {
    if (x && x.kind === "crawlcheck-decision" && x.resolve && x.resolve.kid) out.push(x.resolve.kid);
    const t = x && x.transparency; if (t && t.sth && t.sth.signature && t.sth.signature.kid) out.push(t.sth.signature.kid);
    if (x && x.kind === "crawlcheck-resolve-as-of") { if (x.answer && x.answer.signature && x.answer.signature.kid) out.push(x.answer.signature.kid); const p = x.log_inclusion; if (p && p.sth && p.sth.signature && p.sth.signature.kid) out.push(p.sth.signature.kid); }
  } catch (e) { }
  return out.filter((k, i) => typeof k === "string" && out.indexOf(k) === i);
}
// conf2: the signing policy (https://crawlcheck.io/keys#policy, machine copy spec/key-policy/policy.json) as code. Which key
// PURPOSE may sign which object at which time, retired keys, compromised keys. Same rule in the Python package.
export const KEY_POLICY = { v: 2,
  object_purpose: { "crawlcheck-decision": "decisions", "crawlcheck-log-sth": "log heads", "*": "documents" },
  chains: { documents: ["documents", "requests"], decisions: ["decisions", "documents", "requests"], "log heads": ["log heads", "documents", "requests"] },
  starts: { requests: null, documents: "2026-10-11T00:00:00Z", decisions: "2026-11-09T17:00:00Z", "log heads": "2026-11-09T17:00:00Z" },
  bound: { "crawlcheck-resolve": "transparency.sth", "crawlcheck-decision": "resolve" },
  purpose_aliases: { "requests and documents": "requests" } };
const kpPurpose = (p) => { p = String(p || "requests"); return KEY_POLICY.purpose_aliases[p] || p; };
const kpMs = (v) => { if (v === null || v === undefined || v === "") return null; if (typeof v === "number") return v < 1e11 ? v * 1000 : v; const t = Date.parse(String(v)); return isFinite(t) ? t : null; };
// The key directory (and, when you have it, the rotation record) as the input every key-policy check reads.
export async function keyPolicyFrom(directory, rotation) {
  const keys = {}, retired = {}, compromised = {};
  for (const k of (directory && directory.keys) || []) {
    if (!k || !k.x) continue;
    const kid = b64u(await sha256(enc.encode(JSON.stringify({ crv: k.crv || "Ed25519", kty: k.kty || "OKP", x: k.x }))));
    const purpose = kpPurpose(k.crawlcheck_purpose);
    keys[kid] = { purpose, start: kpMs(k.crawlcheck_signs_from) ?? kpMs(k.nbf) ?? kpMs(KEY_POLICY.starts[purpose]) };
  }
  for (const r of (rotation && rotation.retired) || []) if (r && r.thumbprint) retired[r.thumbprint] = { purpose: kpPurpose(r.purpose), start: kpMs(r.crawlcheck_signs_from) ?? kpMs(r.not_before) ?? kpMs(KEY_POLICY.starts[kpPurpose(r.purpose)]), stopped: kpMs(r.stopped_signing_at) ?? kpMs(r.retired_at) };   // conf3: a retired key keeps its signing window
  for (const c of (rotation && rotation.compromised) || []) if (c && c.thumbprint) compromised[c.thumbprint] = kpMs(c.compromised_since) ?? 0;
  return { v: 2, kids: Object.keys(keys), keys, retired, compromised, rotation: !!rotation };
}
export function signedAtOf(x) {
  const s = x && x.signature, c = [s && s.signed_at, x && x.decided_at, x && x.answered_at, x && x.sealed_at, x && x.generated_at, x && x.issued_at, x && x.at];
  for (const v of c) { const t = kpMs(v); if (t !== null) return t; }
  return null;
}
function kpBoundKid(x) {
  const path = KEY_POLICY.bound[x && x.kind]; if (!path) return null;
  const o = path.split(".").reduce((a, p) => (a && typeof a === "object" ? a[p] : undefined), x);
  return (o && o.signature && o.signature.kid) || (o && typeof o.kid === "string" ? o.kid : null);
}
// One key against the policy at time t. Returns { known, ok, purpose, why } (ok null: could not be decided).
function kpKey(kid, t, kp) {
  if (!kid) return { known: false, ok: false, code: "key_unknown", why: "no signing key named" };
  if (kp.compromised[kid] !== undefined) return { known: false, ok: false, code: "key_compromised", why: "key " + kid + " is listed as compromised since " + new Date(kp.compromised[kid]).toISOString() + ": its signature alone proves nothing (" + (t !== null && t >= kp.compromised[kid] ? "signed after that moment" : "only an outside anchor still proves documents signed before it") + ")" };
  const k = kp.keys[kid];
  if (k) { if (t !== null && k.start !== null && t < k.start) return { known: true, ok: false, purpose: k.purpose, code: "key_not_yet_active", why: "key " + kid + " (" + k.purpose + ") signs nothing before " + new Date(k.start).toISOString() }; return { known: true, ok: true, purpose: k.purpose, start: k.start }; }
  const r = kp.retired[kid];
  if (r) { if (t === null) return { known: true, ok: null, purpose: r.purpose, code: "no_signing_time", why: "retired key, and the document carries no signing time" }; return r.stopped !== null && t <= r.stopped ? { known: true, ok: true, purpose: r.purpose, retired: true, why: "retired key, signed before it stopped signing (" + new Date(r.stopped).toISOString() + ")" } : { known: false, ok: false, code: "key_retired", why: "key " + kid + " stopped signing at " + (r.stopped !== null ? new Date(r.stopped).toISOString() : "an unknown time") + "; this was signed after" }; }
  return { known: false, ok: false, code: "key_unknown", why: "key " + kid + " is not in the published key directory" + (kp.rotation ? " or the rotation record" : "") };
}
function kpExpected(objPurpose, t, kp) {
  for (const p of KEY_POLICY.chains[objPurpose]) {
    if (p === "requests") return p;
    if (Object.keys(kp.keys).some((kid) => kp.keys[kid].purpose === p && kp.compromised[kid] === undefined && (t === null || kp.keys[kid].start === null || kp.keys[kid].start <= t))) return p;
    // conf3: a key retired since still covered its own signing window: a decision made while a decision key was live expects that purpose
    if (t !== null && Object.keys(kp.retired || {}).some((kid) => { const r = kp.retired[kid]; return r.purpose === p && kp.compromised[kid] === undefined && (r.start === null || r.start === undefined || r.start <= t) && r.stopped !== null && t <= r.stopped; })) return p;
  }
  return "requests";
}
// Does the policy accept this signature on this document? { ok: true | false | null, code, purpose, expected, why }
// code (stable): ok, ok_bound, key_wrong_purpose, key_not_yet_active, key_retired, key_compromised, key_unknown, no_signing_time;
// applyKeyPolicy adds related_<code> when the key a document leans on (its answer, its log head) fails.
export function keyPolicyCheck(x, kp) {
  const kid = x && x.signature && x.signature.kid, t = signedAtOf(x);
  const objPurpose = KEY_POLICY.object_purpose[x && x.kind] || KEY_POLICY.object_purpose["*"];
  const k = kpKey(kid, t, kp);
  if (k.ok !== true) return { ok: k.ok, code: k.code, known: k.known, purpose: k.purpose || null, expected: null, why: k.why };
  if (t === null) return { ok: null, code: "no_signing_time", known: true, purpose: k.purpose, expected: null, why: "the document carries no signing time, so the key's purpose cannot be checked against the policy" };
  const expected = kpExpected(objPurpose, t, kp), chain = KEY_POLICY.chains[objPurpose], at = new Date(t).toISOString();
  if (k.purpose === expected) return { ok: true, code: "ok", known: true, purpose: k.purpose, expected, why: "a " + k.purpose + " key signs " + objPurpose + " at " + at + (k.retired ? " (" + k.why + ")" : "") };
  const later = chain.indexOf(k.purpose) > chain.indexOf(expected);
  const bound = kpBoundKid(x), decisionKeyLive = objPurpose === "decisions" && expected === "decisions";
  if (later && bound && bound === kid && !decisionKeyLive) return { ok: true, code: "ok_bound", known: true, purpose: k.purpose, expected, why: "signed by the " + k.purpose + " key that signed the object it is bound to (" + KEY_POLICY.bound[x.kind] + "), as the policy allows" };
  return { ok: false, code: "key_wrong_purpose", known: true, purpose: k.purpose, expected, why: "a " + k.purpose + " key does not sign " + objPurpose + " at " + at + ": the " + expected + " key does" + (chain.indexOf(k.purpose) < 0 ? " (" + k.purpose + " keys never sign them)" : "") };
}
// Apply the policy to a verification result: key_published (retired keys included), key_purpose, and the trust fields.
export function applyKeyPolicy(res, x, kp) {
  if (!res || !kp) return res;
  const main = keyPolicyCheck(x, kp), t = signedAtOf(x), rel = [];
  let relCode = null;
  if (x && x.kind === "crawlcheck-decision" && x.resolve && x.resolve.kid) { const r = kpKey(x.resolve.kid, t, kp); if (r.ok !== true) { rel.push("the Resolve answer it names: " + r.why); relCode = relCode || "related_" + (r.code || "key_unknown"); } }
  const sth = x && x.transparency && x.transparency.sth;
  if (sth && sth.signature) { const r = keyPolicyCheck(sth, kp); if (r.ok === false) { rel.push("its log head: " + r.why); relCode = relCode || "related_" + r.code; } }
  const kpub = { id: "key_published", ok: main.known && !rel.length, why: main.known && !rel.length ? "key " + (x.signature && x.signature.kid) + " is " + (kp.keys[x.signature && x.signature.kid] ? "in the published key directory" : "a retired key listed in the rotation record") + ((x && (x.kind === "crawlcheck-decision" || sth)) ? ", and so is every key it leans on" : "") : (main.known ? "it leans on a key the policy rejects: " + rel.join("; ") : main.why) + ": reject this document" };
  const kpur = { id: "key_purpose", ok: rel.length ? false : main.ok, code: rel.length && main.ok !== false ? relCode : main.code, why: rel.length ? rel.join("; ") : main.why };
  let checks = (res.checks || []).filter((c) => c.id !== "key_published" && c.id !== "key_purpose").concat([kpub, kpur]);
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  const own = checks.filter((c) => c.id !== "key_published" && c.id !== "key_purpose");
  const integrity_valid = own.some((c) => c.ok !== null) && own.every((c) => c.ok !== false);
  const unavailable = checks.filter((c) => c.ok === null).map((c) => c.id);
  const accepted = integrity_valid && kpub.ok === true && kpur.ok !== false;
  return Object.assign({}, res, { checks, verified: ran.length > 0 && failed.length === 0, summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed",
    integrity_valid, issuer_trusted: kpub.ok, key_policy_valid: kpur.ok, key_policy_code: kpur.code, accepted, accepted_strict: accepted && unavailable.length === 0, assurance: unavailable.length ? "partial" : "complete", unavailable });
}
// The directory and the rotation record, read now (online). The rotation record is optional: without it retired and
// compromised keys are unknown, and only keys still in the directory are accepted.
export async function publishedKeyPolicy(base) {
  const b = String(base || "https://crawlcheck.io").replace(/\/+$/, "");
  const r = await fetch(b + "/.well-known/http-message-signatures-directory");
  if (!r.ok) throw new Error("key directory answered HTTP " + r.status);
  const d = await r.json();
  let rot = null; try { const q = await fetch(b + "/.well-known/security-key-rotation.json"); if (q.ok) rot = await q.json(); } catch (e) { rot = null; }
  return Object.assign(await keyPolicyFrom(d, rot), { read_at: new Date().toISOString(), source: "online" });
}
export function trustOf(res, kid, publishedKids, related) {
  let checks = Array.isArray(res && res.checks) ? res.checks : [];
  let issuer = null;
  if (Array.isArray(publishedKids)) {
    const __rel = (related || []).filter((k) => k !== kid), __miss = __rel.filter((k) => publishedKids.indexOf(k) < 0);
    issuer = !!kid && publishedKids.indexOf(kid) > -1 && __miss.length === 0;
    if (!!kid && publishedKids.indexOf(kid) > -1 && __miss.length) { const kc0 = { id: "key_published", ok: false, why: "the document is signed by a published key, but it leans on key " + __miss.join(", ") + ", which is NOT published: reject this document" }; checks = checks.some((c) => c.id === "key_published") ? checks.map((c) => c.id === "key_published" ? kc0 : c) : checks.concat([kc0]); }
    else {
    const kc = { id: "key_published", ok: issuer, why: issuer ? "key id " + kid + " is in the published key directory" : !kid ? "this document carries no signature, so its issuer cannot be authenticated" : "key id " + kid + " is NOT in the published key directory: reject this document" };
    checks = checks.some((c) => c.id === "key_published") ? checks.map((c) => c.id === "key_published" ? kc : c) : checks.concat([kc]);
    }
  }
  const ran = checks.filter((c) => c.ok !== null), failed = checks.filter((c) => c.ok === false);
  const unavailable = checks.filter((c) => c.ok === null && c.id !== "key_published").map((c) => c.id);
  const verified = ran.length > 0 && failed.length === 0;
  const integrity_valid = ran.some((c) => c.id !== "key_published") && checks.filter((c) => c.id !== "key_published").every((c) => c.ok !== false);
  const accepted = integrity_valid && issuer === true;
  return Object.assign({}, res, { checks, verified, summary: failed.length ? failed.length + " check(s) FAILED" : ran.length + " of " + checks.length + " checks ran and passed",
    integrity_valid, issuer_trusted: issuer, accepted, accepted_strict: accepted && unavailable.length === 0,
    assurance: unavailable.length ? "partial" : "complete", unavailable });
}
// The published Ed25519 key ids (RFC 7638 thumbprints) from the observer's key directory.
export async function publishedKeyIds(base) {
  const r = await fetch(String(base || "https://crawlcheck.io").replace(/\/+$/, "") + "/.well-known/http-message-signatures-directory");
  if (!r.ok) throw new Error("key directory answered HTTP " + r.status);
  const d = await r.json(), out = [];
  for (const k of (d && d.keys) || []) { if (!k || !k.x) continue; out.push(b64u(await sha256(enc.encode(JSON.stringify({ crv: k.crv, kty: k.kty, x: k.x }))))); }
  return out;
}
const kidOf = (o) => (o && o.signature && o.signature.kid) || (o && o.manifest && o.manifest.signature && o.manifest.signature.kid) || undefined;
export async function verifyBundle(b, opts) { return trustOf(await verifyBundle0(b), b && b.manifest && b.manifest.signature && b.manifest.signature.kid, opts && opts.publishedKids); }
export async function verifyReceipt(rc, opts) { return trustOf(await verifyReceipt0(rc), rc && rc.signature && rc.signature.kid, opts && opts.publishedKids); }
export async function verifyExport(x, opts) { return trustOf(await verifyExport0(x), kidOf(x), opts && opts.publishedKids); }
export async function verifyResolution(x, opts) { return trustOf(await verifyResolution0(x), kidOf(x), opts && opts.publishedKids); }
// dns1: a Resolve-over-DNS TXT record (ccr1). Strings joined without spaces; quotes from dig output are stripped.
async function verifyDnsTxt0(txt) {
  if (!subtle) throw new Error("this runtime has no WebCrypto (crypto.subtle)");
  const checks = [], C = (id, ok, why) => checks.push({ id, ok, why });
  const t = String(txt || "").trim().replace(/"\s*"/g, "").replace(/^"|"$/g, "").replace(/\;/g, ";");
  const i = t.lastIndexOf(";s="), body = i > 0 ? t.slice(0, i) : "", sig = i > 0 ? t.slice(i + 3) : "";
  const f = {}; body.split(";").forEach((p) => { const j = p.indexOf("="); if (j > 0) f[p.slice(0, j)] = p.slice(j + 1); });
  C("format", f.v === "ccr1" && !!f.d && !!f.k && !!f.x && !!sig, f.v === "ccr1" ? "ccr1 record for " + (f.d || "?") + " (" + (f.st || "?") + ")" : "not a ccr1 record");
  if (f.x && f.k) {
    const thumb = b64u(await sha256(enc.encode(JSON.stringify({ crv: "Ed25519", kty: "OKP", x: f.x }))));
    C("key_id", thumb === f.k, thumb === f.k ? "public key thumbprint (RFC 7638) is the key id " + thumb.slice(0, 12) + "…" : "thumbprint " + thumb + " does not match key id " + f.k);
    let ok = null; try { const pk = await subtle.importKey("jwk", { kty: "OKP", crv: "Ed25519", x: f.x }, { name: "Ed25519" }, false, ["verify"]); ok = await subtle.verify({ name: "Ed25519" }, pk, b64(sig), enc.encode("crawlcheck-dns-v1\n" + body)); } catch (e) { ok = null; C("signature", null, "this runtime cannot verify Ed25519: " + (e && e.message || e)); }
    if (ok !== null) C("signature", ok, ok ? "Ed25519 signature over the record verifies" : "signature does NOT verify over this record");
  }
  const fu = Date.parse(f.f || ""); C("fresh", isFinite(fu) ? fu > Date.now() : false, isFinite(fu) ? (fu > Date.now() ? "fresh until " + f.f : "expired at " + f.f + ": ask again") : "no fresh_until");
  C("key_published", null, "offline this cannot be decided: compare key id " + (f.k || "?") + " with the ids published at https://crawlcheck.io/.well-known/http-message-signatures-directory");
  return { record: f, decisions: { read: f.read || null, cite: f.cite || null, connect: f.connect || null, transact: f.transact || null }, checks };
}
export async function verifyDnsTxt(txt, opts) { const r = await verifyDnsTxt0(txt); return trustOf(r, r.record && r.record.k, opts && opts.publishedKids); }
export async function verifyDataDoc(x, opts) { return trustOf(await verifyDataDoc0(x), kidOf(x), opts && opts.publishedKids, relatedKids(x)); }

// ea1 (2026-10-10): may an agent act on this decision receipt? Integrity, issuer, freshness, scope and the decision
// itself are separate answers; eligible_to_act is true only when every one holds. A valid signature from an unknown key,
// an expired receipt, a receipt for another domain or action, warn without allowWarn, and require_confirmation without
// a human's confirmation are each refused, with the reason.
export async function eligibleToAct(receipt, want, opts) {
  want = want || {}; opts = opts || {};
  const why = [];
  const isDecision = !!receipt && receipt.kind === "crawlcheck-decision" && receipt.v === 1;
  if (!isDecision) why.push("not a v1 decision receipt (kind " + (receipt && receipt.kind) + ", v " + (receipt && receipt.v) + "): ask POST /api/v1/preflight for one");
  let v = null; try { v = isDecision ? await verifyDataDoc(receipt, opts) : null; if (v && opts.keyPolicy) v = applyKeyPolicy(v, receipt, opts.keyPolicy); } catch (e) { why.push("could not be verified: " + (e && e.message || e)); }
  const sig = v ? (v.checks.find((c) => c.id === "signature") || {}).ok : null;
  // ga1: skew capped at 300 s; a receipt may not outlive its action's maximum lifetime (https://crawlcheck.io/spec/guard)
  const now = opts.now ? (typeof opts.now === "number" ? opts.now : Date.parse(opts.now)) : Date.now(), skew = Math.min(Math.max(Number(receipt && receipt.clock_skew_s) || 0, 0), 300) * 1000;
  const exp = Date.parse((receipt && receipt.expires_at) || ""), at = Date.parse((receipt && receipt.decided_at) || "");
  const LIFE = { read: 86400, cite: 86400, connect: 3600, transact: 300, administer: 300 }, max = LIFE[String(receipt && receipt.action)];
  const withinLife = isFinite(exp) && isFinite(at) && max !== undefined && exp - at <= max * 1000 + 1000;
  const fresh = isFinite(exp) && isFinite(at) && now <= exp + skew && at <= now + skew && withinLife;
  if (isFinite(exp) && isFinite(at) && !withinLife) why.push("valid for " + Math.round((exp - at) / 1000) + " s; a " + (receipt && receipt.action) + " receipt may last at most " + max + " s");
  if (isFinite(at) && at > now + skew) why.push("decided in the future (" + receipt.decided_at + "): check the clock");
  const nonceOk = !want.nonce || (receipt && receipt.nonce) === want.nonce;
  // op1: a receipt names the exact operation it allows (method + url, or an MCP tool, its definition digest and scopes)
  const opOk = !want.operation || (!!receipt && receipt.operation != null && canon(receipt.operation) === canon(want.operation));
  if (!opOk) why.push(receipt && receipt.operation ? "the receipt is for another operation (" + canon(receipt.operation).slice(0, 120) + ")" : "the receipt is not scoped to an operation, and this request named one");
  if (!nonceOk) why.push(receipt && receipt.nonce ? "the receipt carries another request's nonce (replayed)" : "the receipt does not carry the nonce this request sent");
  const norm = (d) => String(d || "").trim().toLowerCase().replace(/^https?:\/\//, "").replace(/^www\./, "").replace(/[\/?#].*$/, "").replace(/:\d+$/, "").replace(/\.$/, "");
  let scope = null;
  if (want.domain || want.action) {
    scope = (!want.domain || norm(want.domain) === norm(receipt && receipt.domain)) && (!want.action || String(want.action).toLowerCase() === String(receipt && receipt.action).toLowerCase());
    if (!scope) why.push("the receipt is for " + (receipt && receipt.domain) + " / " + (receipt && receipt.action) + ", not " + (want.domain || "(any)") + " / " + (want.action || "(any)"));
  } else why.push("name the domain and action you are about to act on: a receipt is only valid for its own");
  const d = receipt && receipt.decision;
  const permits = d === "allow" || (d === "warn" && !!opts.allowWarn) || (d === "require_confirmation" && opts.confirmed === true);
  if (d === "warn" && !opts.allowWarn) why.push("decision is warn: proceed only if your policy accepts the stated doubt (allowWarn)");
  if (d === "require_confirmation" && opts.confirmed !== true) why.push("decision is require_confirmation: a human must confirm first (confirmed: true)");
  if (d === "block" || d === "unsupported") why.push("decision is " + d + ": do not act");
  if (v && !v.integrity_valid) why.push("integrity failed: " + v.checks.filter((c) => c.ok === false).map((c) => c.id).join(", "));
  if (v && v.issuer_trusted !== true) why.push(v.issuer_trusted === false ? "signed by a key that is not CrawlCheck's published key" : "issuer not checked: pass publishedKids or keyPolicy (or use --online)");
  if (v && v.key_policy_valid === false) why.push("the signing policy rejects this key for a decision receipt: " + ((v.checks.find((c) => c.id === "key_purpose") || {}).why || ""));   // conf2
  if (!fresh) why.push(isFinite(exp) ? "expired at " + receipt.expires_at + ": ask again" : "no expires_at");
  const eligible = isDecision && !!v && v.integrity_valid && v.issuer_trusted === true && v.key_policy_valid !== false && fresh && scope === true && nonceOk && opOk && permits;
  return { kind: "crawlcheck-eligibility", v: 1, domain: receipt && receipt.domain || null, action: receipt && receipt.action || null, decision: d || null,
    integrity_valid: v ? v.integrity_valid : false, signature_valid: sig === undefined ? null : sig, issuer_trusted: v ? v.issuer_trusted : null, key_policy_valid: v && v.key_policy_valid !== undefined ? v.key_policy_valid : null,
    fresh, expires_at: receipt && receipt.expires_at || null, scope_valid: scope, nonce_valid: nonceOk, operation_valid: opOk, decision_permits: permits,
    human_confirmation_required: d === "require_confirmation", eligible_to_act: eligible, why: eligible ? [] : why, checks: v ? v.checks : [] };
}

const isMain = typeof process !== "undefined" && !!(process.argv && process.argv[1]) && await (async () => { try { const { pathToFileURL } = await import("node:url"); const { realpathSync } = await import("node:fs"); return import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href; } catch (e) { return false; } })();
if (isMain) {
  const online = process.argv.includes("--online");
  const __kfi = process.argv.indexOf("--keys"), keysFile = __kfi > -1 ? process.argv[__kfi + 1] : null;   // conf1: offline, against a saved key directory
  const __rfi = process.argv.indexOf("--rotation"), rotFile = __rfi > -1 ? process.argv[__rfi + 1] : null;
  // conf2: offline = a key directory you saved earlier (and, optionally, the rotation record saved with it). Nothing is
  // fetched, so a key retired or declared compromised after you saved them is not known to this run.
  const keysFromFile = async () => { const { readFileSync, statSync } = await import("node:fs"); const d = JSON.parse(readFileSync(keysFile, "utf8")); const dir = d && d.directory ? d.directory : d; const rot = rotFile ? JSON.parse(readFileSync(rotFile, "utf8")) : (d && d.rotation) || null;
    return Object.assign(await keyPolicyFrom(dir, rot), { source: "saved", read_at: (d && d.saved_at) || statSync(keysFile).mtime.toISOString() }); };
  const keyPolicyNow = async () => { if (online) return publishedKeyPolicy(); if (keysFile) return keysFromFile(); return null; };
  const keySourceLine = (kp) => !kp ? "keys: none given (offline): integrity only; issuer and key policy are not decided. Run with --online, or --keys <saved directory.json> [--rotation <saved rotation.json>]"
    : kp.source === "online" ? "keys: read online at " + kp.read_at + " from crawlcheck.io (directory" + (kp.rotation ? " and rotation record" : "; rotation record unavailable, so retired and compromised keys are unknown") + ")"
    : "keys: saved copy as of " + kp.read_at + (kp.rotation ? " (directory and rotation record)" : " (directory only: retired and compromised keys unknown)") + "; anything revoked after that is not known to this run";
  const __ai = process.argv.indexOf("--act");
  if (__ai > -1) {   // ea1: may I act on this receipt? exit 0 = eligible, 2 = not
    const fs = await import("node:fs"), spec = String(process.argv[__ai + 1] || ""), k = spec.lastIndexOf(":");
    const __ni = process.argv.indexOf("--nonce"), file = process.argv.slice(2).filter((a, n) => !a.startsWith("--") && n + 2 !== __ai + 1 && (__ni < 0 || n + 2 !== __ni + 1) && (process.argv.indexOf("--operation") < 0 || n + 2 !== process.argv.indexOf("--operation") + 1))[0];
    let __rc; try { __rc = JSON.parse(fs.readFileSync(file, "utf8")); } catch (e) { console.log("NOT VERIFIABLE: " + file + (e && e.code === "ENOENT" ? " does not exist." : " is not JSON, so it is not a decision receipt.")); process.exit(2); }
    const r = await eligibleToAct(__rc, { domain: k > 0 ? spec.slice(0, k) : spec, action: k > 0 ? spec.slice(k + 1) : null, nonce: process.argv.indexOf("--nonce") > -1 ? process.argv[process.argv.indexOf("--nonce") + 1] : null, operation: process.argv.indexOf("--operation") > -1 ? JSON.parse(process.argv[process.argv.indexOf("--operation") + 1]) : null },
      { keyPolicy: await (async () => { const kp = await keyPolicyNow() || await publishedKeyPolicy(); return kp; })(), allowWarn: process.argv.includes("--allow-warn"), confirmed: process.argv.includes("--confirmed") });
    const out = Object.assign({}, r); delete out.checks; console.log(JSON.stringify(out, null, 2)); process.exit(r.eligible_to_act ? 0 : 2);
  }
  if (process.argv.includes("--dns")) {   // dns1: a Resolve-over-DNS TXT record
    let txt = process.argv.slice(2).filter((a) => a !== "--online" && a !== "--dns").join(" ");
    if (!txt) { const { readFileSync } = await import("node:fs"); txt = readFileSync(0, "utf8"); }
    let kd; if (online) { try { kd = await publishedKeyIds(); } catch (e) { console.log("could not read the published key directory: " + e.message); } }
    const r = await verifyDnsTxt(txt, { publishedKids: kd });
    console.log("CrawlCheck Resolve over DNS: " + (r.record.d || "?") + " " + (r.record.st || "?") + " · read " + r.decisions.read + " · cite " + r.decisions.cite + " · connect " + r.decisions.connect + " · transact " + r.decisions.transact);
    for (const c of r.checks) console.log((c.ok === true ? "PASS" : c.ok === false ? "FAIL" : " -- ") + "  " + c.id.padEnd(23) + c.why);
    console.log(r.summary); console.log(r.accepted ? "ACCEPTED" : "NOT ACCEPTED" + (r.integrity_valid && r.issuer_trusted === null ? ": issuer unknown offline (run with --online)" : ""));
    process.exit(r.checks.some((c) => c.ok === false) ? 1 : r.accepted ? 0 : 3);
  }
  const f = process.argv.slice(2).filter((a, n) => a !== "--online" && a !== "--keys" && a !== "--rotation" && !(__kfi > -1 && n + 2 === __kfi + 1) && !(__rfi > -1 && n + 2 === __rfi + 1))[0];
  if (!f) { console.log("usage: node crawlcheck-verify.mjs [--online | --keys saved-directory.json [--rotation saved-rotation.json]] [--dns \"<ccr1 TXT record>\"] <bundle.json | receipt.json | export.json | resolution.json | deletion.json | answer.json | decision.json | manifest.json>"); process.exit(2); }
  const { readFile } = await import("node:fs/promises");
  // ver2: input that is not a signed CrawlCheck document gets a one-line answer and exit 2, never a stack trace.
  let doc; try { doc = JSON.parse(await readFile(f, "utf8")); } catch (e) {
    console.log("NOT VERIFIABLE: " + f + (e && e.code === "ENOENT" ? " does not exist." : " is not JSON, so it is not a signed CrawlCheck document. Nothing to verify.")); process.exit(2); }
  if (!doc || typeof doc !== "object" || Array.isArray(doc)) { console.log("NOT VERIFIABLE: " + f + " is JSON but not an object, so it is not a signed CrawlCheck document. Nothing to verify."); process.exit(2); }
  const isRc = doc && (doc.kind === "crawlcheck-remediation-receipt" || (doc.receipt && doc.receipt.kind === "crawlcheck-remediation-receipt"));
  const isEx = doc && doc.kind === "crawlcheck-evidence-export";
  const isDr = doc && doc.kind === "crawlcheck-dispute-resolution";
  const isDt = doc && /^crawlcheck-(data-inventory|data-export-part|deletion-record|resolve|decision|snapshot|visitor|conduct|traffic-index|anchor|anchor-check|content-clock|origin|claims|read-equivalence|rights|rights-feed|mcp-scan|mcp-lock|mcp-lock-check|mcp-shadow|mcp-auth|impersonation|task-canaries|agent-safety-benchmark|compliance-pack|share-of-preflight|incident|revocations|high-risk|log-sth|threshold-verdict|resolve-as-of|sbom|query|mcp-reliability-server|mcp-reliability-host|removal-record|status-entry|backup-manifest|backup-key-check|business-truth)$/.test(String(doc.kind)) || !!(doc && /^crawlcheck-[a-z0-9-]{2,60}$/.test(String(doc.kind)) && doc.signature && String(doc.signature.message || "").indexOf(DATA_SIG_PREFIX) === 0);
  let kp = null; try { kp = await keyPolicyNow(); } catch (e) { console.log("could not read the " + (online ? "published key directory: " : "saved key directory " + keysFile + ": ") + e.message); }
  const vo = { publishedKids: kp ? kp.kids : undefined };
  let res; try { res = isDt ? await verifyDataDoc(doc, vo) : isDr ? await verifyResolution(doc, vo) : isEx ? await verifyExport(doc, vo) : isRc ? await verifyReceipt(doc.kind ? doc : doc.receipt, vo) : await verifyBundle(doc, vo); }
  catch (e) { console.log("NOT VERIFIABLE: " + (doc.kind ? "kind \"" + String(doc.kind).slice(0, 80) + "\"" : "no kind field") + " is not a signed CrawlCheck document this verifier knows (" + String(e && e.message || e).slice(0, 200) + "). Nothing was checked, so nothing passed."); process.exit(2); }
  if (kp && isDt && doc.signature) res = applyKeyPolicy(res, doc, kp);   // conf2: the signing policy (key purpose, retired, compromised)
  const mark = (ok) => ok === true ? "PASS" : ok === false ? "FAIL" : " -- ";
  if (isDt) console.log("CrawlCheck " + doc.kind.replace("crawlcheck-", "").replace(/-/g, " ") + ": " + (doc.domain || "?") + " " + String(doc.sha256 || "").slice(0, 16) + "…");
  else if (isDr) console.log("CrawlCheck dispute resolution: " + (res.resolution.id || "?").slice(0, 24) + "… " + (res.resolution.domain || "") + " " + (res.resolution.code || "") + " verdict " + (res.resolution.verdict || "?"));
  else if (isEx) console.log("CrawlCheck evidence export: " + (res.export.domain || "?") + " " + (res.export.window ? res.export.window.from + " to " + res.export.window.to : "") + " (" + res.export.records + " records, " + res.export.receipts + " receipts)");
  else if (isRc) console.log("CrawlCheck remediation receipt: " + (res.receipt.id || "?") + " " + (res.receipt.domain || "") + " verdict " + (res.receipt.verdict || "?"));
  else console.log("CrawlCheck evidence bundle: " + (res.report.id || "?") + " " + (res.report.domain || "") + " scanned " + (res.report.scanned_at || "?"));
  for (const c of res.checks) console.log(mark(c.ok) + "  " + c.id.padEnd(23) + c.why);
  if (isEx) for (const r of res.records) console.log(mark(r.verified) + "    record " + String(r.id || "?").padEnd(14) + (r.failed && r.failed.length ? "failed: " + r.failed.join(", ") : (r.why || (r.ran + " checks ran"))));
  if (isEx) for (const r of res.receipts) console.log(mark(r.verified) + "    receipt " + String(r.id || "?").slice(0, 20) + (r.failed && r.failed.length ? " failed: " + r.failed.join(", ") : ""));
  console.log(res.summary);
  console.log(keySourceLine(kp));
  console.log("integrity " + (res.integrity_valid ? "valid" : "NOT valid") + " · issuer " + (res.issuer_trusted === true ? "trusted (key published)" : res.issuer_trusted === false ? "NOT trusted (key not published)" : "not checked: run with --online, or --keys with a saved key directory") + (res.key_policy_valid === undefined ? "" : " · key policy " + (res.key_policy_valid === true ? "valid" : res.key_policy_valid === false ? "VIOLATED" : "not decided")) + " · assurance " + res.assurance + (res.unavailable.length ? " (" + res.unavailable.join(", ") + " could not run)" : ""));
  console.log(res.accepted ? (res.accepted_strict ? "ACCEPTED" : "ACCEPTED, partial assurance") : "NOT ACCEPTED" + (res.integrity_valid && res.issuer_trusted === null ? ": issuer unknown offline" : ""));
  // conf2: ACCEPTED means authentic and signed by CrawlCheck under its signing policy. It is not permission to act.
  if (res.accepted && (doc.kind === "crawlcheck-decision" || doc.kind === "crawlcheck-resolve")) console.log("ACCEPTED is about who signed this, not what to do. " + (doc.kind === "crawlcheck-decision" ? "The decision inside is " + doc.decision + " for " + doc.action + " on " + doc.domain + "; before acting, check it is yours with --act " + doc.domain + ":" + doc.action + " (domain, action, expiry, nonce)." : "What to do is in actions.<read|cite|connect|transact>.decision; ask POST /api/v1/preflight for a receipt scoped to the one action you are about to take."));
  process.exit(res.checks.some((c) => c.ok === false) ? 1 : res.accepted ? 0 : 3);
}
