{"record":{"id":"9ab36c7097290177","repo":"JuliusBrussee/caveman","slug":"cacheengine-longer-prefix-cannot-have-higher-expe","errorCode":null,"errorMessage":"cacheengine: longer prefix cannot have higher expected reuse","messagePattern":"cacheengine: longer prefix cannot have higher expected reuse","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cacheengine/engine.go","lineNumber":377,"sourceCode":"func 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\n\t\tif cumulativeTokens > 0 && profile.EconomicsKnown {\n\t\t\trawNet := float64(cumulativeTokens) * (float64(calls) - profile.WriteMultiplier - float64(calls-1)*profile.ReadMultiplier)\n\t\t\tif math.IsNaN(rawNet) || math.IsInf(rawNet, 0) {\n\t\t\t\treturn nil, false, false, errors.New(\"cacheengine: cache economics overflow\")\n\t\t\t}\n\t\t\tnet = roundUnits(rawNet)\n\t\t\tif net <= 0 {\n\t\t\t\tnegative = true","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/cacheengine/engine.go#L359-L395","documentation":"A longer prefix segment reported strictly higher ExpectedCalls than the shorter prefix before it. Cache reuse semantics require expected reuse to be monotonically non-increasing as the prefix grows; an inversion means inconsistent measurements that would misplace breakpoints.","triggerScenarios":"Thrown at cacheengine/engine.go:377 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Correct expected-calls measurements so longer prefixes reuse no more than shorter ones","Set ExpectedCalls to zero on segments without a measurement instead of an inflated guess"],"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"}