JuliusBrussee/caveman · error

cacheengine: longer prefix cannot have higher expected reuse

Error message

cacheengine: longer prefix cannot have higher expected reuse

What it means

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.

Solutions

  1. Correct expected-calls measurements so longer prefixes reuse no more than shorter ones
  2. Set ExpectedCalls to zero on segments without a measurement instead of an inflated guess
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at cacheengine/engine.go:377 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of JuliusBrussee/caveman@766dce6b13 (2026-08-18). Data as JSON: /api/errors/9ab36c7097290177. Report an issue: GitHub.

Appendix: source

Thrown at cacheengine/engine.go:377

func breakpointCandidates(segments []Segment, defaultCalls int, profile Profile) ([]Breakpoint, bool, bool, error) {
	var candidates []Breakpoint
	var prefix []byte
	cumulativeTokens := 0
	previousCalls := math.MaxInt
	belowMinimum := false
	negative := false
	for index, segment := range segments {
		prefix = appendFrame(prefix, segment.Name, segment.Content)
		if segment.Tokens > math.MaxInt-cumulativeTokens {
			return nil, false, false, errors.New("cacheengine: cumulative token count overflow")
		}
		cumulativeTokens += segment.Tokens
		calls := segment.ExpectedCalls
		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

View on GitHub (pinned to 766dce6b13)