{"record":{"id":"b5b55159040d475e","repo":"JuliusBrussee/caveman","slug":"cave-harness-wire-contract-invalid","errorCode":"cave_harness_wire_contract_invalid","errorMessage":"cave_harness_wire_contract_invalid","messagePattern":"cave_harness_wire_contract_invalid","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/adapters.ts","lineNumber":601,"sourceCode":"  // longer authorize execution.\n  const installed = installedPackageVersion(pkg);\n  if (installed === undefined) {\n    throw new Error(`cave_${harness}_upstream_version_unresolvable`);\n  }\n  if (installed !== expected) {\n    throw new Error(`cave_${harness}_upstream_version_unsupported`);\n  }\n  if (identity.upstreamVersion !== installed) {\n    throw new Error(`cave_${harness}_upstream_version_mismatch`);\n  }\n}\n\nfunction canonicalRecord(value: Readonly<Record<string, unknown>>): Readonly<Record<string, unknown>> {\n  let encoded: string;\n  try {\n    encoded = stableStringify(value);\n  } catch {\n    throw new Error(\"cave_harness_wire_contract_invalid\");\n  }\n  const decoded = JSON.parse(encoded) as unknown;\n  if (!isRecord(decoded)) throw new Error(\"cave_harness_wire_contract_invalid\");\n  return decoded;\n}\n\nfunction deepFreeze<T>(value: T): T {\n  if (value !== null && typeof value === \"object\") {\n    Object.freeze(value);\n    for (const child of Object.values(value)) deepFreeze(child);\n  }\n  return value;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n  return value !== null && typeof value === \"object\" && !Array.isArray(value);\n}\n","sourceCodeStart":583,"sourceCodeEnd":619,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/packages/agent/src/adapters.ts#L583-L619","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Keep wireContract a small, flat, JSON-only literal (the built-in adapters use { kind, version, request, response } style literals)","Remove circular links and BigInt values; convert hashes to hex strings","Pre-check the contract with a dry-run stable/JSON stringify before calling createHarnessAdapter"],"exampleFix":"// before\nconst contract = { kind: \"my-harness\", version: 1 };\ncontract.self = contract; // circular\ncreateHarnessAdapter(id, identity, contract, invoke);\n\n// after\nconst contract = { kind: \"my-harness\", version: 1 };\ncreateHarnessAdapter(id, identity, contract, invoke);","handlingStrategy":"validation","validationCode":"// Dry-run serialization before creating the adapter.\nfunction isSerializableContract(contract: unknown): boolean {\n  const seen = new WeakSet();\n  try {\n    JSON.stringify(contract, (_key, value) => {\n      if (typeof value === \"bigint\") throw new Error(\"bigint\");\n      if (value !== null && typeof value === \"object\") {\n        if (seen.has(value)) throw new Error(\"circular\");\n        seen.add(value);\n      }\n      return value;\n    });\n    return true;\n  } catch {\n    return false;\n  }\n}\nif (!isSerializableContract(wireContract)) throw new Error(\"wireContract not serializable\");","typeGuard":null,"tryCatchPattern":"try {\n  const adapter = createHarnessAdapter(id, identity, wireContract, invoke);\n} catch (error) {\n  if (error instanceof Error && error.message === \"cave_harness_wire_contract_invalid\") {\n    // wireContract is not losslessly serializable — remove circular refs,\n    // BigInts, and other non-JSON values, then recreate the adapter.\n  }\n  throw error;\n}","preventionTips":["Keep wireContract a small flat literal of strings and numbers","Convert hashes to hex strings, never BigInt","Never attach back-references or parent pointers inside a contract object"],"tags":["wire-contract","serialization","adapter","circular-reference"],"backgroundTag":"circular-reference-serialization","analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}