{"record":{"id":"2a4f8d0e42f396c9","repo":"JuliusBrussee/caveman","slug":"cacheengine-negative-traffic-expectation","errorCode":null,"errorMessage":"cacheengine: negative traffic expectation","messagePattern":"cacheengine: negative traffic expectation","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cacheengine/engine.go","lineNumber":279,"sourceCode":"\t\tPrefixSHA256:   result.PrefixSHA256,\n\t\tEconomicsBasis: economicsBasis,\n\t\tKeyShardCount:  1,\n\t\tWarnings:       warnings,\n\t}, nil\n}\n\nfunc validatePlanRequest(request PlanRequest) error {\n\tif !validIdentity(request.Scope, 4096, false) {\n\t\treturn errors.New(\"cacheengine: invalid scope\")\n\t}\n\tif !validIdentity(request.Epoch, 4096, false) {\n\t\treturn errors.New(\"cacheengine: invalid epoch\")\n\t}\n\tif !validIdentity(request.PartitionKey, 4096, true) {\n\t\treturn errors.New(\"cacheengine: invalid partition key\")\n\t}\n\tif request.ExpectedCalls < 0 || request.ExpectedRequestsPerMinute < 0 {\n\t\treturn errors.New(\"cacheengine: negative traffic expectation\")\n\t}\n\tprofile := normalizedProfile(request.Profile)\n\tif !validIdentity(profile.ID, 256, false) || !validIdentity(profile.Provider, 64, true) || !validIdentity(profile.OptimizerID, 256, true) {\n\t\treturn errors.New(\"cacheengine: invalid profile identity\")\n\t}\n\tif profile.Mode != ModeUnsupported && profile.Mode != ModeImplicit && profile.Mode != ModeAffinity && profile.Mode != ModeExplicit {\n\t\treturn fmt.Errorf(\"cacheengine: unknown mode %q\", profile.Mode)\n\t}\n\tif profile.Mode != ModeUnsupported {\n\t\tif profile.MaxBreakpoints <= 0 || profile.MinPrefixTokens < 0 || profile.MaxRPMPerKey < 0 || profile.TTL < 0 {\n\t\t\treturn errors.New(\"cacheengine: invalid cache thresholds\")\n\t\t}\n\t\tswitch profile.Attribution {\n\t\tcase AttributionNone, AttributionOrganic, AttributionAffinity, AttributionCausal:\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"cacheengine: unknown attribution %q\", profile.Attribution)\n\t\t}\n\t\tif profile.EconomicsKnown && (!finiteNonNegative(profile.WriteMultiplier) || !finiteNonNegative(profile.ReadMultiplier)) {","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/cacheengine/engine.go#L261-L297","documentation":"Thrown by validatePlanRequest when request.ExpectedCalls or request.ExpectedRequestsPerMinute is negative. These fields size the cache plan (breakpoint economics, key sharding); negative traffic estimates would produce nonsensical plans.","triggerScenarios":"Calling Plan with ExpectedCalls < 0 or ExpectedRequestsPerMinute < 0, e.g. after a subtraction or a delta computation that underflows, or by confusing 'unknown' (0) with 'none' (-1).","commonSituations":"Computing expected calls as thisPeriod - lastPeriod and going negative during a traffic drop; passing -1 as a 'not set' sentinel; unmarshalling a negative value from user-supplied JSON config.","solutions":["Clamp or recompute the estimate so both fields are >= 0 before calling Plan","Use 0 (not -1) to mean 'no estimate'; the engine substitutes defaults such as defaultCalls","Audit where the negative value originates if it comes from your own metrics pipeline"],"exampleFix":"// before\nreq.ExpectedCalls = currentCalls - previousCalls // can go negative\n\n// after\nif d := currentCalls - previousCalls; d > 0 {\n    req.ExpectedCalls = d\n} else {\n    req.ExpectedCalls = 0\n}","handlingStrategy":"validation","validationCode":"if req.ExpectedCalls < 0 || req.ExpectedRequestsPerMinute < 0 { req.ExpectedCalls, req.ExpectedRequestsPerMinute = 0, 0 }","typeGuard":"func nonNegativeTraffic(req cacheengine.PlanRequest) bool { return req.ExpectedCalls >= 0 && req.ExpectedRequestsPerMinute >= 0 }","tryCatchPattern":"if err := engine.Plan(req); err != nil && strings.Contains(err.Error(), \"negative traffic\") { logMetricsBug(); return err }","preventionTips":["Treat 0 as 'unknown' everywhere; forbid -1 sentinels","Clamp delta-based estimates at zero"],"tags":["cacheengine","validation","traffic-estimation","go"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}