JuliusBrussee/caveman · error · Error

cave_conversation_invalid

cave_conversation_invalid

Error message

cave_conversation_invalid

What it means

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.

Solutions

  1. Call snapshot() on the original Conversation instance returned by the runtime, not a clone or spread copy.
  2. Take the snapshot before cloning/serializing; serialize state.messages yourself instead of copying the instance.
  3. Deduplicate the runtime module import (single version, avoid dual ESM/CJS instances) so the WeakMap keying matches.

Example fix

// before
const copy = { ...conversation };
const snap = copy.snapshot(); // throws cave_conversation_invalid
// after
const snap = conversation.snapshot(); // original instance holds WeakMap state
Defensive patterns

Strategy: try-catch

Validate before calling

function canSnapshot(c) { return c instanceof Conversation && conversationStates.has(c); } // internal check; for users: only snapshot instances obtained directly from the runtime
if (typeof c?.snapshot !== "function") throw new Error("not a live Conversation instance");

Type guard

function isLiveConversation(c: unknown): c is Conversation { return c instanceof Conversation; } // note: clones/copies pass instanceof but still lack WeakMap state

Try / catch

let snap;
try { snap = conversation.snapshot(); }
catch (e) { if (String(e.message).includes("cave_conversation_invalid")) { conversation = runtime.getConversation(id); snap = conversation.snapshot(); } else throw e; }

Prevention

When it happens

Trigger: 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).

Common situations: See trigger scenarios.

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 JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/a2bad5059838c0fc. Report an issue: GitHub.

Appendix: source

Thrown at packages/agent/src/runtime.ts:281

}

const conversationStates = new WeakMap<Conversation, InternalConversationState>();

export class Conversation {
  readonly sessionId: string;

  constructor() {
    this.sessionId = `conversation-${crypto.randomUUID()}`;
    conversationStates.set(this, {
      messages: [],
      sessionId: this.sessionId,
      busy: false,
    });
  }

  snapshot(): { readonly messages: readonly unknown[] } {
    const state = conversationStates.get(this);
    if (!state) throw new Error("cave_conversation_invalid");
    return Object.freeze({ messages: structuredClone(state.messages) });
  }
}

export type ConversationState = Conversation;

export function createConversation(): Conversation {
  return new Conversation();
}

type ConversationTransaction = {
  readonly owner: Conversation;
  readonly sessionId: string;
  messages: AgentMessage[];
  fingerprint: string | undefined;
  cachePrefixDigest: string | undefined;
  providerFrozen: ProviderFrozenView | undefined;
  originalFrozen: ProviderFrozenView | undefined;

View on GitHub (pinned to 3ee70a1026)