{"record":{"id":"49cdc95d184d0c36","repo":"JuliusBrussee/caveman","slug":"journal-bytes-do-not-match-declared-transaction-st","errorCode":null,"errorMessage":"journal bytes do not match declared transaction state","messagePattern":"journal bytes do not match declared transaction state","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":12782,"sourceCode":"    || !validOptionalHash(value.config_after_sha256)\n    || !validOptionalHash(value.marker_before_sha256)\n    || !validOptionalHash(value.marker_after_sha256)) {\n    throw new Error(`pending ${label} transaction is malformed; refusing recovery`);\n  }\n  try {\n    const configBefore = decodePendingBytes(value.config_before_base64, \"config_before_base64\");\n    const markerBefore = decodePendingBytes(value.marker_before_base64, \"marker_before_base64\");\n    const markerAfter = decodePendingBytes(value.marker_after_base64, \"marker_after_base64\");\n    if (optionalBytesHash(configBefore) !== value.config_before_sha256\n      || optionalBytesHash(markerBefore) !== value.marker_before_sha256\n      || optionalBytesHash(markerAfter) !== value.marker_after_sha256\n      || (markerBefore !== null && !validMcpMarkerBytes(markerBefore, journalAgent, journalServer))\n      || (markerAfter !== null && !validMcpMarkerBytes(markerAfter, journalAgent, journalServer))\n      || (markerBefore !== null && parseMcpServerMarkerBytes(journalAgent, journalServer, markerBefore)?.config_path !== undefined\n        && parseMcpServerMarkerBytes(journalAgent, journalServer, markerBefore)?.config_path !== value.config_path)\n      || (markerAfter !== null && parseMcpServerMarkerBytes(journalAgent, journalServer, markerAfter)?.config_path !== value.config_path)\n      || (value.action === \"install\") !== (markerAfter !== null)) {\n      throw new Error(\"journal bytes do not match declared transaction state\");\n    }\n    return {\n      journal: value as OwnedMcpPendingJournal,\n      configBefore,\n      markerBefore,\n      markerAfter,\n      path,\n      bytes,\n    };\n  } catch (error) {\n    throw new Error(`pending ${label} transaction is malformed: ${(error as Error).message}`);\n  }\n}\n\nfunction readOwnedMcpPendingLocator(agent: \"kilo\" | \"qwen\", serverName: string): ReadOwnedMcpPendingJournal | null {\n  const path = canonicalMcpConfigPath(mcpPendingJournalPath(agent, serverName));\n  return readOwnedMcpPendingJournalAt(path, { agent, serverName, locatorPath: path });\n}","sourceCodeStart":12764,"sourceCodeEnd":12800,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L12764-L12800","documentation":"Beyond field types, the journal's declared state must be consistent with the actual byte snapshots it carries: marker bytes must parse as valid server markers for the declared agent/server, marker config_path must match the journal's config_path, and the action must agree with marker presence (install implies marker_after non-null). A mismatch means the journal bytes were tampered with or mixed from different transactions.","triggerScenarios":"Editing config_path or server_name in the journal without re-encoding the marker snapshots; combining fields from two different transactions; a journal where action='install' but marker_after_base64 is null (or vice versa); marker bytes that fail parseMcpServerMarkerBytes for the declared agent.","commonSituations":"Manual repair attempts that changed one field but not its dependent snapshots; restoring an old journal for a different server/config; interrupted multi-step edits leaving inconsistent state.","solutions":["Delete the inconsistent journal and re-run the MCP install/update command to create a coherent one","If repairing by hand, re-encode marker snapshots so they parse for the declared agent/server and match config_path, and align action with marker_after presence","Restore a backup of the journal from before manual edits","Verify hashes: recompute sha256 of the decoded base64 fields and ensure they equal the *_sha256 fields"],"exampleFix":"// before\n{ \"action\": \"install\", \"marker_after_base64\": null }\n// after\n{ \"action\": \"install\", \"marker_after_base64\": \"<base64 of valid marker for this agent/server>\" }","handlingStrategy":"validation","validationCode":"import { createHash } from 'crypto';\nfunction snapshotHashesMatch(j: Record<string, unknown>): boolean {\n  const h = (b: Buffer | null) => b === null ? null : 'sha256:' + createHash('sha256').update(b).digest('hex');\n  return h(j.config_before_base64 ? Buffer.from(j.config_before_base64 as string, 'base64') : null) === j.config_before_sha256;\n}","typeGuard":"function markerConsistentWithAction(j: { action: string; marker_after_base64: string | null }): boolean {\n  return (j.action === 'install') === (j.marker_after_base64 !== null);\n}","tryCatchPattern":"try {\n  const txn = loadPendingJournal(path);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('journal bytes do not match')) {\n    // journal inconsistent: delete and regenerate via the CLI\n  } else throw e;\n}","preventionTips":["Edit journal fields as a set — always update snapshots together with config_path/server_name","Recompute sha256 hashes whenever any base64 field changes","Never merge fields across different transaction journals","Verify action/marker_after agreement (install ⇔ marker present) after edits"],"tags":["journal","integrity","consistency","mcp"],"backgroundTag":"checksum-mismatch","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}