{"record":{"id":"8482ccfe5aed5e22","repo":"JuliusBrussee/caveman","slug":"token-budget-must-be-positive-zero-default-or","errorCode":null,"errorMessage":"token budget must be positive, zero (default), or UnlimitedTokenBudget","messagePattern":"token budget must be positive, zero \\(default\\), or UnlimitedTokenBudget","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem/store.go","lineNumber":425,"sourceCode":"// Recall returns the memories most relevant to query, ranked by BM25, filtered\n// by the threshold, and compressed for injection. It fails toward no recall: a\n// query with no term overlap (or that clears nothing above the threshold)\n// returns an empty slice, never a guess.\nfunc (s *Store) Recall(query string, opts RecallOptions) ([]Hit, error) {\n\tlimit := opts.Limit\n\tif limit <= 0 {\n\t\tlimit = DefaultLimit\n\t}\n\tthreshold := opts.Threshold\n\tif threshold <= 0 {\n\t\tthreshold = DefaultThreshold\n\t}\n\tbudget := opts.TokenBudget\n\tunlimited := budget == UnlimitedTokenBudget\n\tif budget == 0 {\n\t\tbudget = DefaultTokenBudget\n\t} else if budget < 0 && !unlimited {\n\t\treturn nil, fmt.Errorf(\"token budget must be positive, zero (default), or UnlimitedTokenBudget\")\n\t}\n\n\tall, err := s.all()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tscores := bm25Scores(query, all)\n\n\ttype scored struct {\n\t\tmem   Memory\n\t\tscore float64\n\t}\n\tranked := make([]scored, 0, len(all))\n\tfor _, m := range all {\n\t\tif sc := scores[m.ID]; sc >= threshold {\n\t\t\tranked = append(ranked, scored{mem: m, score: sc})\n\t\t}\n\t}","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/mem/store.go#L407-L443","documentation":"Recall validates opts.TokenBudget before ranking results: only positive values, zero (meaning DefaultTokenBudget), or the UnlimitedTokenBudget sentinel are accepted. Any other negative value is rejected because the budget would make the selection logic behave incorrectly. This is a pure input-validation error, not a storage failure.","triggerScenarios":"Calling Store.Recall(ctx, query, RecallOptions{TokenBudget: -N}) for any negative N other than mem.UnlimitedTokenBudget — e.g. TokenBudget: -1 or -5000.","commonSituations":"Developers use -1 hoping to mean 'unlimited' (a common sentinel convention elsewhere); it must be mem.UnlimitedTokenBudget instead. Others compute budgets from a subtraction that underflows below zero.","solutions":["Use 0 for the default budget or mem.UnlimitedTokenBudget for unbounded recall.","If the intent was unlimited, replace the negative literal with the UnlimitedTokenBudget constant.","Clamp or validate any computed budget to >= 0 before constructing RecallOptions.","Fix the upstream calculation producing a negative budget (e.g. max(0, limit - used))."],"exampleFix":"// before\nhits, err := store.Recall(ctx, q, mem.RecallOptions{TokenBudget: -1}) // 'unlimited' attempt\n// after\nhits, err := store.Recall(ctx, q, mem.RecallOptions{TokenBudget: mem.UnlimitedTokenBudget})","handlingStrategy":"validation","validationCode":"b := opts.TokenBudget\nif b < 0 && b != mem.UnlimitedTokenBudget {\n    return errors.New(\"token budget must be >= 0 or UnlimitedTokenBudget\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use mem.UnlimitedTokenBudget, never -1, for unbounded recall.","Clamp computed budgets: if b < 0 { b = mem.UnlimitedTokenBudget }.","Centralize RecallOptions construction in one helper that validates the budget."],"tags":["validation","arguments","token-budget"],"backgroundTag":"invalid-argument-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}