JuliusBrussee/caveman · error · Error

cave_harness_wire_contract_invalid

cave_harness_wire_contract_invalid

Error message

cave_harness_wire_contract_invalid

What it means

createHarnessAdapter canonicalizes the wireContract by stable-stringifying it into the frozen manifest. This throw site fires when stableStringify itself fails — in practice a value inside the wireContract that cannot be serialized, such as a circular reference or a BigInt.

Solutions

  1. Keep wireContract a small, flat, JSON-only literal (the built-in adapters use { kind, version, request, response } style literals)
  2. Remove circular links and BigInt values; convert hashes to hex strings
  3. Pre-check the contract with a dry-run stable/JSON stringify before calling createHarnessAdapter

Example fix

// before
const contract = { kind: "my-harness", version: 1 };
contract.self = contract; // circular
createHarnessAdapter(id, identity, contract, invoke);

// after
const contract = { kind: "my-harness", version: 1 };
createHarnessAdapter(id, identity, contract, invoke);
Defensive patterns

Strategy: validation

Validate before calling

// Dry-run serialization before creating the adapter.
function isSerializableContract(contract: unknown): boolean {
  const seen = new WeakSet();
  try {
    JSON.stringify(contract, (_key, value) => {
      if (typeof value === "bigint") throw new Error("bigint");
      if (value !== null && typeof value === "object") {
        if (seen.has(value)) throw new Error("circular");
        seen.add(value);
      }
      return value;
    });
    return true;
  } catch {
    return false;
  }
}
if (!isSerializableContract(wireContract)) throw new Error("wireContract not serializable");

Try / catch

try {
  const adapter = createHarnessAdapter(id, identity, wireContract, invoke);
} catch (error) {
  if (error instanceof Error && error.message === "cave_harness_wire_contract_invalid") {
    // wireContract is not losslessly serializable — remove circular refs,
    // BigInts, and other non-JSON values, then recreate the adapter.
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling createHarnessAdapter (directly or via createPiAdapter/createClaudeAdapter) with a wireContract object that references itself directly or through a nested object, or contains a BigInt; the stringify call throws and is mapped to cave_harness_wire_contract_invalid.

Common situations: Building wireContract by assigning nested objects that link back to the parent; stuffing a config object with computed fields that capture other structures; including bigint counters or hashes in the contract.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18). Data as JSON: /api/errors/b5b55159040d475e. Report an issue: GitHub.

Appendix: source

Thrown at packages/agent/src/adapters.ts:601

  // longer authorize execution.
  const installed = installedPackageVersion(pkg);
  if (installed === undefined) {
    throw new Error(`cave_${harness}_upstream_version_unresolvable`);
  }
  if (installed !== expected) {
    throw new Error(`cave_${harness}_upstream_version_unsupported`);
  }
  if (identity.upstreamVersion !== installed) {
    throw new Error(`cave_${harness}_upstream_version_mismatch`);
  }
}

function canonicalRecord(value: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> {
  let encoded: string;
  try {
    encoded = stableStringify(value);
  } catch {
    throw new Error("cave_harness_wire_contract_invalid");
  }
  const decoded = JSON.parse(encoded) as unknown;
  if (!isRecord(decoded)) throw new Error("cave_harness_wire_contract_invalid");
  return decoded;
}

function deepFreeze<T>(value: T): T {
  if (value !== null && typeof value === "object") {
    Object.freeze(value);
    for (const child of Object.values(value)) deepFreeze(child);
  }
  return value;
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}

View on GitHub (pinned to 766dce6b13)