{"record":{"id":"e50328aa7e5a14c1","repo":"JuliusBrussee/caveman","slug":"cachebench-nil-cache-engine","errorCode":null,"errorMessage":"cachebench: nil cache engine","messagePattern":"cachebench: nil cache engine","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cacheengine/cachebench/corpus.go","lineNumber":439,"sourceCode":"\t\t}\n\t}\n\treturn total\n}\n\nfunc corpusDigest(rows []CorpusRow) string {\n\thash := sha256.New()\n\tencoder := json.NewEncoder(hash)\n\tencoder.SetEscapeHTML(false)\n\tfor _, row := range rows {\n\t\t_ = encoder.Encode(row)\n\t}\n\treturn hex.EncodeToString(hash.Sum(nil))\n}\n\n// RunCorpus evaluates one validated public corpus across provider compilers.\nfunc RunCorpus(ctx context.Context, engine *cacheengine.Engine, corpus AgentCorpus, providers []ProviderConfig, target Target) (Report, error) {\n\tif engine == nil {\n\t\treturn Report{}, errors.New(\"cachebench: nil cache engine\")\n\t}\n\tif err := validateTarget(target); err != nil {\n\t\treturn Report{}, err\n\t}\n\tif len(corpus.Rows) == 0 || corpus.SHA256 == \"\" || strings.TrimSpace(corpus.Metadata.Name) == \"\" {\n\t\treturn Report{}, errors.New(\"cachebench: invalid corpus\")\n\t}\n\tif len(providers) == 0 {\n\t\treturn Report{}, errors.New(\"cachebench: no providers\")\n\t}\n\tif len(providers) > 1024 {\n\t\treturn Report{}, errors.New(\"cachebench: provider population exceeds 1024\")\n\t}\n\tcounter := tokens.Default()\n\tcachedCounter := &corpusTokenCounter{Counter: counter, counts: map[[sha256.Size]byte]int{}}\n\tsummary, err := analyzeCorpus(corpus, cachedCounter)\n\tif err != nil {\n\t\treturn Report{}, err","sourceCodeStart":421,"sourceCodeEnd":457,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/cacheengine/cachebench/corpus.go#L421-L457","documentation":"RunCorpus requires a non-nil *cacheengine.Engine because it compiles requests through that engine for cache simulation. A nil engine means there is no cache model to evaluate against, so the function refuses immediately instead of panicking later.","triggerScenarios":"Calling cachebench.RunCorpus(ctx, nil, corpus, providers, target) — usually because the engine constructor returned nil after a swallowed error, or a struct field was never initialized before the benchmark ran.","commonSituations":"Engine construction error ignored (err checked, nil engine kept); wiring the benchmark in a test where the engine dependency was omitted; refactoring that moved engine creation behind a condition that did not run.","solutions":["Check the error from the engine constructor and abort on failure instead of using a nil engine","Pass the same engine instance you use elsewhere in the pipeline into RunCorpus","Add a nil-engine unit test guard so regressions in wiring fail loudly"],"exampleFix":"// before\nengine, _ := cacheengine.New(cfg) // err swallowed, engine may be nil\nreport, err := cachebench.RunCorpus(ctx, engine, corpus, providers, target)\n\n// after\nengine, err := cacheengine.New(cfg)\nif err != nil { return err }\nreport, err := cachebench.RunCorpus(ctx, engine, corpus, providers, target)","handlingStrategy":"validation","validationCode":"engine, err := cacheengine.New(cfg)\nif err != nil {\n    return fmt.Errorf(\"engine construction failed: %w\", err)\n}\nif engine == nil {\n    return fmt.Errorf(\"engine constructor returned nil without error\")\n}","typeGuard":"func hasEngine(e *cacheengine.Engine) bool { return e != nil }","tryCatchPattern":"report, err := cachebench.RunCorpus(ctx, engine, corpus, providers, target)\nif err != nil {\n    if err.Error() == \"cachebench: nil cache engine\" {\n        return fmt.Errorf(\"engine wiring bug: %w\", err)\n    }\n    return err\n}","preventionTips":["Never discard the error from engine constructors; check err and use the engine only on success","Add a startup assertion that the engine dependency is non-nil before any benchmark runs","Initialize dependencies in one constructor so a nil engine cannot be constructed partway"],"tags":["go","nil-safety","corpus","cache-engine"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}