{"record":{"id":"a2bad5059838c0fc","repo":"JuliusBrussee/caveman","slug":"cave-conversation-invalid","errorCode":"cave_conversation_invalid","errorMessage":"cave_conversation_invalid","messagePattern":"cave_conversation_invalid","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":281,"sourceCode":"}\n\nconst conversationStates = new WeakMap<Conversation, InternalConversationState>();\n\nexport class Conversation {\n  readonly sessionId: string;\n\n  constructor() {\n    this.sessionId = `conversation-${crypto.randomUUID()}`;\n    conversationStates.set(this, {\n      messages: [],\n      sessionId: this.sessionId,\n      busy: false,\n    });\n  }\n\n  snapshot(): { readonly messages: readonly unknown[] } {\n    const state = conversationStates.get(this);\n    if (!state) throw new Error(\"cave_conversation_invalid\");\n    return Object.freeze({ messages: structuredClone(state.messages) });\n  }\n}\n\nexport type ConversationState = Conversation;\n\nexport function createConversation(): Conversation {\n  return new Conversation();\n}\n\ntype ConversationTransaction = {\n  readonly owner: Conversation;\n  readonly sessionId: string;\n  messages: AgentMessage[];\n  fingerprint: string | undefined;\n  cachePrefixDigest: string | undefined;\n  providerFrozen: ProviderFrozenView | undefined;\n  originalFrozen: ProviderFrozenView | undefined;","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L263-L299","documentation":"Conversation.snapshot() looks up the conversation's internal state in a module-level WeakMap keyed by the instance and throws code \"cave_conversation_invalid\" when no state entry exists. This means snapshot() was called on an object that is not a live, properly initialized Conversation of this runtime — e.g. a copy, a stale instance, or a foreign object. The message doubles as the error code, so catchers can match on it.","triggerScenarios":"Calling snapshot() on a Conversation-like object not produced by this runtime module (different module instance after bundling/dual-package duplication), on an object that was never initialized through the runtime constructor path, or on a structuredClone/spread copy of a real Conversation (the WeakMap link does not survive cloning).","commonSituations":"See trigger scenarios.","solutions":["Call snapshot() on the original Conversation instance returned by the runtime, not a clone or spread copy.","Take the snapshot before cloning/serializing; serialize state.messages yourself instead of copying the instance.","Deduplicate the runtime module import (single version, avoid dual ESM/CJS instances) so the WeakMap keying matches."],"exampleFix":"// before\nconst copy = { ...conversation };\nconst snap = copy.snapshot(); // throws cave_conversation_invalid\n// after\nconst snap = conversation.snapshot(); // original instance holds WeakMap state","handlingStrategy":"try-catch","validationCode":"function canSnapshot(c) { return c instanceof Conversation && conversationStates.has(c); } // internal check; for users: only snapshot instances obtained directly from the runtime\nif (typeof c?.snapshot !== \"function\") throw new Error(\"not a live Conversation instance\");","typeGuard":"function isLiveConversation(c: unknown): c is Conversation { return c instanceof Conversation; } // note: clones/copies pass instanceof but still lack WeakMap state","tryCatchPattern":"let snap;\ntry { snap = conversation.snapshot(); }\ncatch (e) { if (String(e.message).includes(\"cave_conversation_invalid\")) { conversation = runtime.getConversation(id); snap = conversation.snapshot(); } else throw e; }","preventionTips":["Never spread or structuredClone a Conversation and call methods on the copy.","Take snapshots before serializing; serialize state.messages, not the instance.","Ensure the runtime module is imported from a single instance (beware dual ESM/CJS bundles).","Re-acquire conversations from the runtime instead of passing them across workers/processes."],"tags":["runtime","invalid-state","conversation","internal-state"],"backgroundTag":"internal-invariant-violation","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"}