{"record":{"id":"5752f9c2970ca22a","repo":"JuliusBrussee/caveman","slug":"cacheengine-cache-economics-overflow","errorCode":null,"errorMessage":"cacheengine: cache economics overflow","messagePattern":"cacheengine: cache economics overflow","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cacheengine/engine.go","lineNumber":391,"sourceCode":"\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\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\tsum := sha256.Sum256(prefix)\n\t\tcandidate := Breakpoint{\n\t\t\tAfterSegment:              segment.Name,\n\t\t\tPrefixSHA256:              hex.EncodeToString(sum[:]),\n\t\t\tPrefixTokens:              cumulativeTokens,\n\t\t\tExpectedCalls:             calls,\n\t\t\tBreakEvenCalls:            breakEvenCalls(profile),\n\t\t\tExpectedNetInputRateUnits: net,\n\t\t\tindex:                     index,\n\t\t}\n\t\tif len(candidates) > 0 && candidates[len(candidates)-1].ExpectedCalls == calls {","sourceCodeStart":373,"sourceCodeEnd":409,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/cacheengine/engine.go#L373-L409","documentation":"Thrown by breakpointCandidates when the net-gain formula rawNet = cumulativeTokens * (calls - WriteMultiplier - (calls-1)*ReadMultiplier) evaluates to NaN or Inf. Inputs were already validated as finite, so this fires only when the multiplication overflows float64 range (cumulativeTokens * calls near 1.8e308).","triggerScenarios":"Very large token totals combined with large ExpectedCalls or multipliers whose product exceeds float64 max; e.g. cumulativeTokens ~1e300 from corrupted tokenizer data times multipliers ~1e10.","commonSituations":"Garbage or adversarial measurements (tokenizer returning astronomically large token counts); test fixtures with extreme values; never occurs with realistic token counts (< 10^7) and sane multipliers.","solutions":["Sanity-cap segment Tokens and ExpectedCalls to realistic bounds before planning (both < 10^9)","Validate multipliers are modest positive numbers, not just finite","Investigate the measurement source if real traffic ever reaches this — it indicates corrupted metrics"],"exampleFix":"// before\nseg := cacheengine.Segment{Name: \"x\", Stable: true, Cacheable: true, Content: b, Tokens: 1 << 900} // overflowed literal intent\n\n// after\nconst maxPlausibleTokens = 1_000_000_000\nif seg.Tokens > maxPlausibleTokens { seg.Tokens = maxPlausibleTokens }","handlingStrategy":"validation","validationCode":"for _, s := range segs {\n    if s.Tokens > 1e9 || s.ExpectedCalls > 1e9 { return errors.New(\"implausible measurements\") }\n}","typeGuard":"// n/a","tryCatchPattern":null,"preventionTips":["Reject astronomically large token/call counts at ingestion","Investigate the metrics source rather than catching this error"],"tags":["cacheengine","float-safety","overflow","economics","go"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}