JuliusBrussee/caveman · error

cacheengine: cache economics overflow

Error message

cacheengine: cache economics overflow

What it means

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).

Source

Thrown at cacheengine/engine.go:391

		if calls == 0 {
			calls = defaultCalls
		}
		if calls > previousCalls {
			return nil, false, false, errors.New("cacheengine: longer prefix cannot have higher expected reuse")
		}
		previousCalls = calls
		if calls < 2 {
			continue
		}
		if cumulativeTokens > 0 && cumulativeTokens < profile.MinPrefixTokens {
			belowMinimum = true
			continue
		}
		net := 0.0
		if cumulativeTokens > 0 && profile.EconomicsKnown {
			rawNet := float64(cumulativeTokens) * (float64(calls) - profile.WriteMultiplier - float64(calls-1)*profile.ReadMultiplier)
			if math.IsNaN(rawNet) || math.IsInf(rawNet, 0) {
				return nil, false, false, errors.New("cacheengine: cache economics overflow")
			}
			net = roundUnits(rawNet)
			if net <= 0 {
				negative = true
				continue
			}
		}
		sum := sha256.Sum256(prefix)
		candidate := Breakpoint{
			AfterSegment:              segment.Name,
			PrefixSHA256:              hex.EncodeToString(sum[:]),
			PrefixTokens:              cumulativeTokens,
			ExpectedCalls:             calls,
			BreakEvenCalls:            breakEvenCalls(profile),
			ExpectedNetInputRateUnits: net,
			index:                     index,
		}
		if len(candidates) > 0 && candidates[len(candidates)-1].ExpectedCalls == calls {

View on GitHub (pinned to 27d5a3981a)

Solutions

  1. Sanity-cap segment Tokens and ExpectedCalls to realistic bounds before planning (both < 10^9)
  2. Validate multipliers are modest positive numbers, not just finite
  3. Investigate the measurement source if real traffic ever reaches this — it indicates corrupted metrics

Example fix

// before
seg := cacheengine.Segment{Name: "x", Stable: true, Cacheable: true, Content: b, Tokens: 1 << 900} // overflowed literal intent

// after
const maxPlausibleTokens = 1_000_000_000
if seg.Tokens > maxPlausibleTokens { seg.Tokens = maxPlausibleTokens }
Defensive patterns

Strategy: validation

Validate before calling

for _, s := range segs {
    if s.Tokens > 1e9 || s.ExpectedCalls > 1e9 { return errors.New("implausible measurements") }
}

Type guard

// n/a

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@27d5a3981a (2026-08-15). Data as JSON: /api/errors/5752f9c2970ca22a. Report an issue: GitHub.