Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(evidence): an implied-unit budget that is not a positive number still means no budget

garrytan35ef5d96917f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c65b7fbec6..a9a937cd3e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,7 +40,7 @@ gbrain config set search.auto_packing off # the old uncapped beha
- **The cap** (`src/core/search/evidence-delivery.ts`). An explicit budget under `auto` (on `query`, `search`, `recall`'s results arm and `assemble_evidence`; `think` passes none) is a hard cap on the recount of every result's title plus `chunk_text`. Rank one, note or conversation, is reserved first and cut at a piece boundary with the marker `\n\n[…]` if it alone exceeds the budget; the other non-conversation chunks keep the rank-order prefix that fits (the rest are `budget_note`); a conversation that does not fit is dropped (`budget_floor`), never spilled. Redaction and an explicit `snippet_chars` run before the final recount, a snippet's recovery marker is paid from the row's own allocation (`snippet_marker_omitted` when it cannot fit), and `budget_used` is that recount (`budget_recount` drops anything still over). `delivery.auto_packing` names the packing when the cap ran.
- **Three packings** (`search.auto_packing`, registered and validated at `config set`): `cap_only` (default: every matching span first, then growth in rank order), `breadth_capped` (the longest rank-order prefix of conversations whose title, matching span and `return_window` target window fit; the rest `breadth_cap`), `depth_first` (each conversation whole if it fits, else as much around its match as fits, else skipped). `off` keeps the uncapped behavior. `assembleEvidenceForHits` takes a library-only `auto_packing` that wins over config per call, for evaluations on one frozen hit list.
-- **The minimum.** An explicit `auto` budget below 32 tokens, or zero, negative or non-finite, fails with `invalid_params` naming the minimum and the parameter (`token_budget`, or `budget_tokens` on `recall`). Above it, a non-empty readable hit list always returns non-empty evidence.
+- **The minimum.** An explicit `auto` budget below 32 tokens fails with `invalid_params` naming the minimum and the parameter (`token_budget`, or `budget_tokens` on `recall`), and so does a zero, negative or non-finite one passed with `return_unit: "auto"`; with the unit omitted such a budget still means no budget. Above it, a non-empty readable hit list always returns non-empty evidence.
- **No budget, no change.** The evidence plan now records whether the caller passed the budget (`budgetExplicit`) and the resolved packing; without an explicit budget the allocator runs the previous code. A structural property test over random corpora pins every packing to the previous bytes, and the off-path golden is unchanged.
- **Frozen-hit dates.** `resolveFrozenHits` projects `effective_date` and `effective_date_source` through the same normalizer live search rows use (`applyEffectiveDate` in `src/core/utils.ts`), so frozen and live delivery hand a reader the same dates.
- Docs: `docs/evidence-delivery.md` ("Explicit budgets (the cap)", drop and fallback codes, errors, the frozen-candidate interface); KEY_FILES entries for `evidence-delivery.ts` and the new `src/core/search/evidence-packing.ts`.
diff --git a/docs/evidence-delivery.md b/docs/evidence-delivery.md
index b76a200088..23b40e5fc6 100644
--- a/docs/evidence-delivery.md
+++ b/docs/evidence-delivery.md
@@ -179,8 +179,10 @@ passes no budget) and every call without a budget are byte-identical whatever
it is set to.
The smallest explicit `auto` budget is **32 tokens** (room for a cut marker, a
-shortened title and a non-empty body). A smaller, zero, negative or
-non-finite budget fails with `invalid_params` naming the minimum; above it, a
+shortened title and a non-empty body). A smaller budget fails with
+`invalid_params` naming the minimum, and so does a zero, negative or
+non-finite one when the call passes `return_unit: "auto"` (with the unit
+omitted such a budget still means no budget, as before). Above the minimum, a
non-empty, readable hit list always returns non-empty evidence. A remote
budget is clamped first (`search.return_budget_max_remote`), and the cap
applies to the clamped value.
@@ -298,8 +300,8 @@ An unknown `return_unit` or an out-of-range `return_window` fails with
return_window must be an integer from 1 to 3 (got 7). Example: {"query": "launch date", "return_unit": "window", "return_window": 2}
```
-An explicit `auto` budget below 32 tokens (or zero, negative or non-finite)
-fails with `invalid_params`, e.g.
+An explicit `auto` budget below 32 tokens (or a zero, negative or non-finite
+one passed with `return_unit: "auto"`) fails with `invalid_params`, e.g.
```
token_budget must be at least 32 tokens under return_unit auto (got 10): an explicit budget is a hard cap, and a smaller one cannot hold a title, a cut marker and any evidence.
diff --git a/src/core/search/evidence-delivery.ts b/src/core/search/evidence-delivery.ts
index c99ddc772f..4c0d02609e 100644
--- a/src/core/search/evidence-delivery.ts
+++ b/src/core/search/evidence-delivery.ts
@@ -222,7 +222,9 @@ export async function resolveEvidencePlan(engine: BrainEngine, input: ResolvePla
if (input.legacyBudget === true) unit = 'chunk';
}
if (unit === 'chunk') return null;
- const budgetExplicit = typeof input.budget === 'number';
+ // A budget that is not a positive number keeps its old meaning (no budget)
+ // unless the call also named its unit; then it is validated below.
+ const budgetExplicit = typeof input.budget === 'number' && (explicit !== undefined || (Number.isFinite(input.budget) && input.budget > 0));
const packing = parseAutoPacking(input.autoPacking) ?? await configPacking(engine);
if (unit === 'auto' && budgetExplicit && packing !== 'off') {
const raw = input.budget as number;
diff --git a/test/evidence-delivery.test.ts b/test/evidence-delivery.test.ts
index 06b8480fa8..e951b90fb8 100644
--- a/test/evidence-delivery.test.ts
+++ b/test/evidence-delivery.test.ts
@@ -720,6 +720,10 @@ describe('explicit budget: the cap', () => {
expect(await plan(5, { autoPacking: 'off' })).toMatchObject({ budgetTokens: 5, budgetExplicit: true, packing: 'off' });
expect(await plan(5, { returnUnit: 'page' })).toMatchObject({ unit: 'page', budgetTokens: 5 });
expect(await plan(undefined)).toMatchObject({ budgetTokens: 24000, budgetExplicit: false });
+ // With the unit implied, a budget that is not a positive number still means no budget, as before.
+ for (const budget of [0, -1, Number.NaN, Number.POSITIVE_INFINITY]) {
+ expect(await plan(budget, { returnUnit: undefined, op: 'recall' })).toMatchObject({ unit: 'auto', budgetTokens: 24000, budgetExplicit: false });
+ }
await expect(plan(4000, { autoPacking: 'widest' })).rejects.toMatchObject({ code: 'invalid_params', message: expect.stringContaining('auto_packing must be one of off, cap_only, breadth_capped, depth_first') });
// At the minimum a non-empty hit list yields non-empty evidence within it.
const r = rng(77);