JuliusBrussee/caveman · error
cave_runtime_segment_id_collision
cave_runtime_segment_id_collision
Error message
cave_runtime_segment_id_collision
What it means
appendRuntimeContextSegment detects when a runtime segment id already exists with a different provenance digest — i.e. the same id carries different bytes. Because that would let compaction substitute an old message's compressed body into a new message, it fails closed with cave_runtime_segment_id_collision instead of returning the stale segment.
Solutions
- Generate a unique segment id per distinct content (include the digest, a uuid, or a monotonic counter)
- If re-appending identical content, derive the id from the content digest so identical bytes collide safely and return the existing segment
- Check for an existing segment before appending and create a new id when bytes differ
Example fix
// before
appendRuntimeContextSegment(ir, { id: "msg-1", body: newBody, ... });
// after
const digest = await sha256Hex(newBody);
appendRuntimeContextSegment(ir, { id: `msg-1:${digest}`, body: newBody, ... }); Defensive patterns
Strategy: try-catch
Validate before calling
const digest = await sha256Hex(body);
if (ir.segments.some((s) => s.id === id)) {
const existing = ir.segments.find((s) => s.id === id)!;
if (existing.provenanceDigest !== digest) {
throw new Error(`id ${id} already used for different content; pick a new id`);
}
} Type guard
null
Try / catch
try {
appendRuntimeContextSegment(ir, segment);
} catch (err) {
if ((err as Error).message === "cave_runtime_segment_id_collision") {
appendRuntimeContextSegment(ir, { ...segment, id: `${segment.id}:${Date.now()}` });
} else throw err;
} Prevention
- Derive segment ids from the content digest or use uuids/monotonic counters — never reuse ids across different bytes
- Reuse the same id only when content is byte-identical (the library then returns the existing segment)
- Include a turn/message ordinal in runtime segment ids to guarantee uniqueness across a session
When it happens
Trigger: Reusing a segment id (e.g. message id) for new content with different bytes; generating ids non-uniquely across turns (e.g. 'user-turn' for every user message); content-addressed digest changing while the caller reuses a hand-picked id.
Common situations: Migrating from sequential counters to content hashes; replaying a session where the same id is appended with regenerated bodies; test fixtures that reuse ids across cases within one session.
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
- assembly slot changed after being declared stable
- cannot read agent-native bundle journal
- cave_budget_reservation_double_settle
- cave_budget_revoked
- cave_harness_incomplete_evidence
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/38f4499a967e878a.
Report an issue: GitHub.
Appendix: source
Thrown at packages/agent/src/context-ir.ts:287
return { ir: { schemaVersion: 1, segments }, bodies };
}
export function appendRuntimeContextSegment(
lowered: LoweredContext,
segment: RuntimeContextSegment,
): ContextSegment {
const digest = sha256(segment.body);
const existing = lowered.ir.segments.find((item) => item.id === segment.id);
if (existing) {
// Segment identity is content-derived, so the same id must
// carry the same bytes. If it does not, the id has been reused for
// different content — the exact confusion that once let compaction
// substitute an OLD message's compressed body into a NEW message. Fail
// closed rather than returning the stale segment and poisoning the CCR
// restore map.
if (existing.provenanceDigest !== digest) {
throw new Error("cave_runtime_segment_id_collision");
}
return existing;
}
const bodyHandle = `cave_local_sha256:${digest}`;
(lowered.bodies as Map<string, Uint8Array>).set(bodyHandle, segment.body.slice());
const appended: ContextSegment = {
id: segment.id,
kind: segment.kind,
stability: "turn",
safety: "S4",
priority: "normal",
recovery: "exact_ccr",
cacheRegion: "live_zone",
privacy: "local_sensitive",
opaque: opaquePayload(segment.body),
provenanceDigest: digest,
tokenCount: estimateTokens(segment.body),
bodyHandle,View on GitHub (pinned to 3ee70a1026)