{"record":{"id":"d33c202147a5dc3b","repo":"JuliusBrussee/caveman","slug":"cacheengine-cumulative-token-count-overflow","errorCode":null,"errorMessage":"cacheengine: cumulative token count overflow","messagePattern":"cacheengine: cumulative token count overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cacheengine/engine.go","lineNumber":369,"sourceCode":"\tvar lengths [8]byte\n\tbinary.BigEndian.PutUint32(lengths[:4], uint32(len(name)))\n\tbinary.BigEndian.PutUint32(lengths[4:], uint32(len(content)))\n\tdst = append(dst, lengths[:]...)\n\tdst = append(dst, name...)\n\treturn append(dst, content...)\n}\n\nfunc breakpointCandidates(segments []Segment, defaultCalls int, profile Profile) ([]Breakpoint, bool, bool, error) {\n\tvar candidates []Breakpoint\n\tvar prefix []byte\n\tcumulativeTokens := 0\n\tpreviousCalls := math.MaxInt\n\tbelowMinimum := false\n\tnegative := false\n\tfor index, segment := range segments {\n\t\tprefix = appendFrame(prefix, segment.Name, segment.Content)\n\t\tif segment.Tokens > math.MaxInt-cumulativeTokens {\n\t\t\treturn nil, false, false, errors.New(\"cacheengine: cumulative token count overflow\")\n\t\t}\n\t\tcumulativeTokens += segment.Tokens\n\t\tcalls := segment.ExpectedCalls\n\t\tif calls == 0 {\n\t\t\tcalls = defaultCalls\n\t\t}\n\t\tif calls > previousCalls {\n\t\t\treturn nil, false, false, errors.New(\"cacheengine: longer prefix cannot have higher expected reuse\")\n\t\t}\n\t\tpreviousCalls = calls\n\t\tif calls < 2 {\n\t\t\tcontinue\n\t\t}\n\t\tif cumulativeTokens > 0 && cumulativeTokens < profile.MinPrefixTokens {\n\t\t\tbelowMinimum = true\n\t\t\tcontinue\n\t\t}\n\t\tnet := 0.0","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/cacheengine/engine.go#L351-L387","documentation":"breakpointCandidates accumulates segment token counts and detects overflow of the cumulative counter; an overflow means the prefix is too large to plan breakpoints for, so planning fails rather than producing wrong cache metadata.","triggerScenarios":"Thrown at cacheengine/engine.go:369 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fix the segment token measurements — realistic counts are many orders of magnitude below MaxInt","Reject telemetry that reports near-overflow token values at ingestion"],"exampleFix":null,"handlingStrategy":"validation","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-23T08:17:48.524Z"}