{"record":{"id":"e7ef2d1c6e45b18b","repo":"JuliusBrussee/caveman","slug":"store-recovery-for-oversized-memory-s-w","errorCode":null,"errorMessage":"store recovery for oversized memory %s: %w","messagePattern":"store recovery for oversized memory (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem/store.go","lineNumber":554,"sourceCode":"\n// headHit builds a single recall hit for a memory whose compressed form exceeds\n// the entire token budget: it truncates the compressed text to a head that fits\n// and guarantees a CCR handle to the byte-exact original. When the engine passed\n// the payload through without compressing (so it left no handle), the original\n// is stored here — a head drops the tail, so the dropped detail must stay\n// recoverable, upholding cavemem's reversible invariant.\nfunc (s *Store) headHit(m Memory, compressedText []byte, handle string, score float64, budget int) (Hit, error) {\n\thead := truncateToTokens(compressedText, budget)\n\tif handle == \"\" {\n\t\th, err := s.ccr.Put(ccr.Recovery{\n\t\t\tContentType:  \"text\",\n\t\t\tCompressor:   \"cavemem-head\",\n\t\t\tTokensBefore: tokenCounter.Count([]byte(m.Text)),\n\t\t\tTokensAfter:  tokenCounter.Count(head),\n\t\t\tOriginal:     []byte(m.Text),\n\t\t})\n\t\tif err != nil {\n\t\t\treturn Hit{}, fmt.Errorf(\"store recovery for oversized memory %s: %w\", m.ID, err)\n\t\t}\n\t\thandle = h\n\t}\n\treturn Hit{\n\t\tID:             m.ID,\n\t\tText:           string(head),\n\t\tScore:          score,\n\t\tTokensAdded:    tokenCounter.Count(head),\n\t\tBasis:          engine.BasisInferred,\n\t\tRecoveryHandle: handle,\n\t}, nil\n}\n\n// truncateToTokens returns the longest byte prefix of b whose inferred token\n// count does not exceed budget, ending on a UTF-8 rune boundary. Recovery of the\n// dropped tail is byte-exact through the hit's CCR handle.\nfunc truncateToTokens(b []byte, budget int) []byte {\n\tif budget <= 0 {","sourceCodeStart":536,"sourceCodeEnd":572,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/mem/store.go#L536-L572","documentation":"In headHit, when a memory's compressed form exceeded the whole budget and no CCR handle existed, storing the byte-exact original for recovery failed. The head would drop the tail irrecoverably, so the hit is abandoned to uphold the reversible invariant.","triggerScenarios":"Thrown at mem/store.go:554 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check ccr.db writability and space","Retry the recall","Inspect the wrapped storage error"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}