Skip to content

cal: ASSEMBLE says what its budget dropped - #213

Merged
sathish-mg merged 3 commits into
mainfrom
fix/assemble-budget-says-what-it-dropped
Sep 8, 2026
Merged

sathish-mg merged 3 commits into
mainfrom
fix/assemble-budget-says-what-it-dropped

Conversation

@sathish-mg

Copy link
Copy Markdown
Contributor

Closes #208.

The bug

ASSEMBLE applies a token budget whether or not the caller writes one — DEFAULT_BUDGET_TOKENS is 4000 — and when it bound it discarded the tail of each source in silence. The payload actively hid it: total_available reported the post-budget count, so a truncated assembly was arithmetically indistinguishable from a complete one — grains.len() == total_available held either way.

Reproduced on a seeded memory (200 grains, LIMIT 400, no BUDGET clause):

ASSEMBLE "e" FROM e: (RECALL facts WHERE namespace = "b" LIMIT 400)
→ 70 grains, warnings: None, total_available: 70      # 130 grains gone, silently

… BUDGET 16000 tokens  → 200 grains                   # the ceiling
RECALL (same predicate) → 200 grains, total_available: 200

This is the same cut RECALL has announced as CAL-W015 since 1.5.1, made without saying so. A host composing a prompt from an assembly had no way to detect that its rules, its policies or its recent turns were trimmed — it would publish a number produced from a truncated prompt and never know. That is what happened in areev-bench: wrapping a selection in an ASSEMBLE returned 79 of 229 error grains, and the small parity fixture was too small to notice.

The fix

1. CAL-W017 when the budget drops grains, naming the sources and the counts, and distinguishing a budget the caller wrote from the default that applied because none was — the two lead to different fixes:

CAL-W017: the default BUDGET 4000 tokens dropped 130 of 200 grains from
source(s) [e] — this assembly is a window, not the whole match. No BUDGET
clause was written, so the 4000-token default applied — write one
(ceiling 16000 tokens), or narrow the source.

The labels matter: "130 grains went" is not actionable on a four-source assembly without knowing which section lost them.

2. total_available is the pre-budget count. Each source's grain_count still reports what survived, so the drop is computable rather than announced only in prose. The console already renders total_available > shown as "N match in total", so it starts telling the truth for assemblies with no change there.

3. Docs state the numbers. docs/cal-reference.md now gives the 4000 default and the 16000 ceiling where BUDGET is documented — neither appeared there, so "no BUDGET clause" read as "no budget".

On the ticket's open question

The issue asks whether an unbudgeted ASSEMBLE should default to no trimming. I kept the default, deliberately:

  • An unbudgeted assembly that returned everything could overflow the context window it is being composed for — the worse failure, and silent in its own way.
  • Progressive disclosure degrading a section is the point of having a budget at all.
  • It would change behaviour for every existing caller, where the warning fixes the actual defect without moving anyone's results.

Silence was the defect, not the number.

Detail

The post-dedup 2000-grain cap already warns for itself, so its tail is folded into the omitted set for ELEMENT_OMIT without double-reporting under W017.

Tests

crates/areev-cal/tests/assemble_budget_visibility.rs, four cases:

  • an unbudgeted assemble reports what its default dropped — total_available is 200 not 70, CAL-W017 present, naming [e] and the default;
  • an explicit BUDGET 4000 names itself, not the default;
  • a budget that fits stays silent — so silence keeps meaning "nothing was cut";
  • the same selection without ASSEMBLE is whole (the control: if plain RECALL also came back short, the assembly's cut would not be the budget's doing).

The fixture seeds 200 grains past the default on purpose — a fixture too small to bind is exactly why this shipped.

cargo test --workspace: 134 suites green. cargo clippy --workspace --all-targets: clean.

Known gap, not introduced here

The console's Query page does not display CAL warnings — any of them, W001 through W017. The warning does reach POST /api/cal's payload; it is the UI that drops it. That is a pre-existing gap across all seventeen warnings rather than something this PR adds, and closing it is a console change (with the screenshot re-shoot that implies), so I left it out. Worth its own issue if wanted.

Docs

docs/cal-reference.md §3.1 (the BUDGET default, the ceiling, and the warning), ERROR_CODES.md (CAL-W017, append-only), crates/areev-cal/CLAUDE.md, CHANGELOG.md.

https://claude.ai/code/session_01JEcv3k8mMwZ5YAa26jdLc2

@sathish-mg

Copy link
Copy Markdown
Contributor Author

Added: the console now shows CAL warnings — closing the gap I flagged above rather than leaving it as a follow-up.

It was never W017-specific: the console dropped all seventeen (CAL-W001–W017). The warning reaches POST /api/cal's payload; the UI ignored it. That is the worst surface to lose them on — it is where a person reads an answer, and this PR's own case rendered as a plain "70 memories" with nothing to say the other 130 existed.

Shown above the result in the console's existing warning style. The CAL-Wnnn code is stripped in plain mode and kept in Developer mode — the code is developer chrome, but "130 of 200 grains dropped" is news for whoever is reading.

Verified in a real browser against areev ui (Playwright, four cases):

unbudgeted assemble (plain)        dev=false → the default BUDGET 4000 tokens dropped 130 of 200 grains fro…
unbudgeted assemble (developer)    dev=true  → CAL-W017: the default BUDGET 4000 tokens dropped 130 of 200 …
plain RECALL, nothing dropped      dev=false → (no banner)
BUDGET 16000 — fits                dev=false → (no banner)

Silence keeps its meaning: a query that warns about nothing still shows nothing.

No README screenshot is affected — the five shots are graph, workflow, runs, suggestions and analytics; the Query page is not among them, so no re-shoot. scripts/repo_stats.py --check passes.

cargo test --workspace: 134 suites green. cargo clippy --workspace --all-targets: clean.

@sathish-mg
sathish-mg force-pushed the fix/assemble-budget-says-what-it-dropped branch from 33b0d4f to c081920 Compare September 8, 2026 16:32
ASSEMBLE applies a token budget whether or not the caller writes one
(DEFAULT_BUDGET_TOKENS, 4000; parser ceiling 16000), and when it bound it
discarded the tail of each source in silence. The payload actively hid it:
total_available reported the POST-budget count, so `grains.len() ==
total_available` held for a truncated assembly exactly as it did for a
complete one. Measured on a real memory, 229 matching grains came back as 80
with warnings: None.

That is the same cut RECALL has announced as CAL-W015 since 1.5.1, made
silently. A host composing a prompt from an assembly had no way to detect
that its rules, its policies or its recent turns were trimmed — it would
publish a number produced from a truncated prompt and never know.

Three changes:

- CAL-W017 when the budget drops grains, naming the sources and the counts,
  and distinguishing a budget the caller wrote from the default that applied
  because none was — the two lead to different fixes.
- total_available is the PRE-budget count. Per-source grain_count still
  reports what survived, so the drop is computable rather than announced only
  in prose.
- docs/cal-reference.md states the default and the ceiling where BUDGET is
  documented. Neither number appeared there, so "no BUDGET clause" read as
  "no budget".

The default itself stays. An unbudgeted assembly that returned everything
could overflow the context window it is being composed for, which is the
worse failure; and progressive disclosure degrading a section is the point of
having a budget at all. Silence was the defect, not the number.

The post-dedup 2000-grain cap already warned for itself, so its tail is
folded into the omitted set for ELEMENT_OMIT without double-reporting.

Tests seed 200 grains past the default — the small parity fixture that let
this ship is exactly why the bug was invisible — and pin that a budget which
fits stays silent, so silence keeps meaning "nothing was cut".

Claude-Session: https://claude.ai/code/session_01JEcv3k8mMwZ5YAa26jdLc2
Every other surface honours the contract that silence means the query did
what you asked (docs/cal-reference.md §5): the bindings and the MCP tool
return `warnings`, the CLI prints them to stderr. The console received them
from POST /api/cal and dropped them on the floor -- all seventeen, W001
through W017, not just the new one.

That is the worst surface to lose them on. It is where a person reads an
answer, and a warning is precisely the news that the answer is a window
rather than the whole match. The result this commit's own CAL-W017 describes
-- 70 of 200 grains -- rendered as a plain "70 memories" with nothing to say
the other 130 existed.

Shown above the result, in the console's existing warning style. The
CAL-Wnnn code is stripped in plain mode and kept in Developer mode: the code
is developer chrome, but "130 of 200 grains dropped" is news for whoever is
reading. A query that warns about nothing still shows nothing, so silence
keeps its meaning here too.

No README screenshot is affected -- the five shots are graph, workflow, runs,
suggestions and analytics; the Query page is not among them.

Claude-Session: https://claude.ai/code/session_01JEcv3k8mMwZ5YAa26jdLc2
…sed heading

Two mechanical defects that reached main through #212 and #215.

Ten `'"'"'` sequences -- shell single-quote escaping -- had leaked verbatim
into CHANGELOG.md and docs/cal-reference.md, so prose read "a tool'"'"'s
input" where it should read "a tool's input".

And rebasing this branch past #215 appended a second `### Fixed` under
`## [Unreleased]` rather than merging into the first. One heading now, entries
in order.

Claude-Session: https://claude.ai/code/session_01JEcv3k8mMwZ5YAa26jdLc2
@sathish-mg
sathish-mg force-pushed the fix/assemble-budget-says-what-it-dropped branch from c081920 to 9951f02 Compare September 8, 2026 16:54
@sathish-mg

Copy link
Copy Markdown
Contributor Author

Rebased onto main after #212 and #215 merged, and picked up two mechanical fixes on the way:

Shell-escaping artifacts. Ten ' sequences had leaked verbatim into CHANGELOG.md and docs/cal-reference.md — my own doing, from heredoc quoting — so prose read "a tool's input" instead of "a tool's input". Eight of them reached main via #212 and #215; this branch cleans all ten. The tree is now clear (grep -rn over *.md/*.rs/*.html: 0).

A duplicate ### Fixed heading under ## [Unreleased], from the rebase appending rather than merging. One heading now, entries in order.

Verified the two PRs' assemble.rs changes compose — #215 removed the duplicate extract_grains and added the per-source pipeline, this PR added the budget warning and the pre-budget total_available. cargo test --workspace: 135 suites green on the rebased result.

@sathish-mg
sathish-mg merged commit ae0f0c9 into main Sep 8, 2026
19 checks passed
@sathish-mg
sathish-mg deleted the fix/assemble-budget-says-what-it-dropped branch September 8, 2026 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CAL: ASSEMBLE silently drops grains to its default 4000-token budget, and total_available hides it

1 participant