Hmbown/CodeWhale · error · Error

cannot parse exactly one Rust TRUSTED_KEYS table

Error message

cannot parse exactly one Rust TRUSTED_KEYS table

What it means

parseRustKeys() extracts the single `pub const TRUSTED_KEYS: &[TrustedKey] = &[...]` table from Rust source via regex. It throws when the regex matches zero or more than one table, because the checker assumes exactly one canonical trusted-keys table to diff against the TypeScript facts.

Solutions

  1. Grep the Rust source for `TRUSTED_KEYS` and confirm exactly one `pub const ... : &[TrustedKey] = &[...]` declaration exists.
  2. Restore the canonical declaration form the regex expects (pub const, &[TrustedKey] type, &[...] initializer, semicolon).
  3. If the table legitimately moved, update the script to read the correct file/path.
  4. If a second table was added by mistake, delete it.

Example fix

// before (Rust)
static TRUSTED_KEYS: &[TrustedKey] = &[ ... ];
// after
pub const TRUSTED_KEYS: &[TrustedKey] = &[ ... ];
Defensive patterns

Strategy: validation

Validate before calling

const count = (src.match(/pub\s+const\s+TRUSTED_KEYS\s*:\s*&\s*\[TrustedKey\]/g) || []).length; if (count !== 1) throw new Error(`expected 1 TRUSTED_KEYS table, found ${count}`);

Try / catch

try { parseRustKeys(src); } catch (e) { if (e.message.includes("exactly one")) grepForTrustedKeysDeclaration(); throw e; }

Prevention

When it happens

Trigger: The Rust constant was renamed, its type/signature reformatted (e.g. `&[TrustedKey; N]` or `static` instead of `pub const`), moved into a module not included in the scanned text, duplicated, or removed.

Common situations: A refactor changed `pub const TRUSTED_KEYS: &[TrustedKey]` to a new declaration style; a second table was accidentally added; the script reads the wrong file so it sees zero tables.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/e769e85e17912206. Report an issue: GitHub.

Appendix: source

Thrown at web/scripts/check-cloud-facts.mjs:14

#!/usr/bin/env node
/** Local source, release, pinned-key parity and public-fixture gate. */
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { validateSource, verifyEnvelope, parseTsKeys, validateTrustedKeys, readBoundedFile } from "./facts-publish.mjs";

const WEB_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const REPO_ROOT = resolve(WEB_ROOT, "..");
export { parseTsKeys };

export function parseRustKeys(text) {
  const source = text.replace(/\/\*[\s\S]*?\*\//g, "").replace(/^\s*\/\/.*$/gm, "");
  const tables = [...source.matchAll(/^\s*pub\s+const\s+TRUSTED_KEYS\s*:\s*&\s*\[TrustedKey\]\s*=\s*&\s*\[([\s\S]*?)\]\s*;/gm)];
  if (tables.length !== 1) throw new Error("cannot parse exactly one Rust TRUSTED_KEYS table");
  const table = tables[0];
  const body = table[1].replace(/^\s*\/\/.*$/gm, "");
  const keys = [];
  const remainder = body.replace(/TrustedKey\s*\{\s*key_id:\s*"([^"]+)",\s*public_key:\s*\[([^\]]*)\],\s*status:\s*KeyStatus::(Active|Retired)\s*,?\s*\}/g, (_, keyId, encoded, status) => {
    const pieces = encoded.split(",").map((piece) => piece.trim()).filter(Boolean);
    if (pieces.length !== 32 || pieces.some((piece) => !/^(?:\d+|0x[0-9a-fA-F]+)$/.test(piece))) throw new Error("Rust public key must contain 32 literal bytes");
    const bytes = pieces.map(Number);
    if (bytes.some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)) throw new Error("Rust public key byte out of range");
    keys.push({ keyId, publicKey: Buffer.from(bytes).toString("base64"), status: status.toLowerCase() });
    return "";
  });
  if (remainder.replace(/[\s,]/g, "")) throw new Error("unparsed Rust TRUSTED_KEYS entry");
  return validateTrustedKeys(keys);
}

function text(path) { return readBoundedFile(path).toString("utf8"); }
function json(path) { return JSON.parse(text(path)); }

View on GitHub (pinned to 433685b202)