Skip to content

fix(cognition): the message budget prices the tool surface it SENDS, not the registry it withholds (card dec1a7ff) - #3842

Merged
joelteply merged 3 commits into
canaryfrom
fix/budget-prices-the-surface-it-sends
Sep 8, 2026
Merged

joelteply merged 3 commits into
canaryfrom
fix/budget-prices-the-surface-it-sends

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

#3829 made the tool surface a per-turn decision — full registry when it fits its
share of the served window, her HANDS on a work turn or when it does not. It did not
split the value that PRICES that surface. describe_tool_tokens() kept returning the
memoized cost of the whole registry, and one accessor came to mean two things: "what
the full registry costs" and "what this turn is sending". They diverge on every work
turn.

The two live 80 lines and one method apart, which is what made it structural:

llm_deliberation_faculty.rs contribute() calls prompt_view_within(ws, window)
└─ budget = window - reserve - describe_tool_tokens()
^ FULL REGISTRY
contribute() then selects the surface
└─ Some(self.hands_specs.clone())
^ HANDS SENT

The budget could not see the decision, so it priced the maximum unconditionally.

THE HARM WAS NOT A WRONG NUMBER. That budget feeds fit_messages, so over-pricing
trims the CONVERSATION to fit room that was never occupied. The citizen loses
grounding she had window for — the #206 cliff's harm class arriving through the
accounting instead of through the surface.

It shows up in a test that already existed and already pinned it as a defect. #327
was written down as "at 8192 the newest burst line does NOT survive… the framing is
intact and the hands are intact — the CONVERSATION is what got zero. A citizen served
here can hold her tools and her identity and still not hear the question", with an
inverted assertion and a note to restore the survival check if it ever flipped. It
flips here. Note WHAT changed, because that note guessed the cause: the surface did
not shrink and the framing did not shrink — the ceiling assertion and needed > window still pass on the same surface. The budget simply stopped reserving for tools
it does not send. "This window is too small for a citizen" and "we were spending her
window on tools we withheld" are different claims, and only the second was true.

THE FIX — one decision that carries what it chose, what it costs, and WHY:

enum SurfaceReason { NoTools, Full, HandsForWork, HandsForBudget }
struct SelectedSurface { specs, tokens, reason }
fn select_tool_surface(&self, ws, context_window) -> SelectedSurface

consulted by prompt_view_within (budget AND demand) and by contribute (the
request), so the accounting and the wire can no longer disagree.

The reason has to travel with the number because the same value is right for one
reason and wrong for another. delib.turn.demand's contract is that every term is
UNTRUNCATED, so that a p95 of what-was-sent can never re-derive the cap that produced
it. Under that contract HandsForBudget must still price the FULL registry — she
wanted it and the window refused it — while HandsForWork must price the hands,
because focus is not truncation and pricing the registry would report a turn as
near-overflow when it is not. SelectedSurface::demand_tokens is exactly that split.

Also here:

  • hands_surface_tokens memoized beside tool_surface_tokens (init/reset/rebuild),
    so the per-turn cost stays a lookup rather than a serde of a dozen schemas.
  • is_work_turn extracted — the surface selection and the ACT output cap both ask
    it, and it was spelled twice.
  • delib.tool_surface.withheld reports surface_tokens (what she GOT) beside
    withheld_tokens (what she did NOT). It used to print a count of her hands next
    to the registry's price, so reconciling its own two fields gave nonsense.
  • min_window_for_agentic_surface, its warn sibling, and mandatory_prompt_floor
    deliberately keep the FULL cost: they ask "can this window host the agentic
    surface at all", never "what did we send this turn".

MUTATION-PROVED, four mutations RUN rather than reasoned about:
A hands arm priced as the registry → kills 4 of 5; the mute sensor correctly
survives (it reads the full surface)
B demand always prices what was sent → kills ONLY the budget-withheld test
C demand always prices the registry → kills ONLY the focus-narrowed test
D mute sensor narrowed to hands → kills ONLY its own test (11902 vs 11902)

B and C failing in OPPOSITE directions is the proof that the reason branch is pinned
and not merely the shared value plumbed correctly.

D's first version killed NOTHING — including the test named for it. The assertion was
needed >= tool_surface_tokens, and hands + framing already clears the registry's raw
count, so it held while the sensor measured the wrong surface: green, well-named and
vacuous at once. Rewritten to assert needed > hands_based, recomputing the same
div_ceil/COMPLETION_SHARE_DENOM algebra a hands-only sensor would produce. Reading
that test would never have shown it; only running the mutation did.

Co-Authored-By: Claude Opus 5 (1M context) [email protected]
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

joelteply and others added 2 commits September 7, 2026 06:33
…not the registry it withholds (card dec1a7ff)

#3829 made the tool surface a per-turn decision — full registry when it fits its
share of the served window, her HANDS on a work turn or when it does not. It did not
split the value that PRICES that surface. `describe_tool_tokens()` kept returning the
memoized cost of the whole registry, and one accessor came to mean two things: "what
the full registry costs" and "what this turn is sending". They diverge on every work
turn.

The two live 80 lines and one method apart, which is what made it structural:

  llm_deliberation_faculty.rs  contribute() calls prompt_view_within(ws, window)
                                 └─ budget = window - reserve - describe_tool_tokens()
                                                                  ^ FULL REGISTRY
                               contribute() then selects the surface
                                 └─ Some(self.hands_specs.clone())
                                                                  ^ HANDS SENT

The budget could not see the decision, so it priced the maximum unconditionally.

THE HARM WAS NOT A WRONG NUMBER. That budget feeds `fit_messages`, so over-pricing
trims the CONVERSATION to fit room that was never occupied. The citizen loses
grounding she had window for — the #206 cliff's harm class arriving through the
accounting instead of through the surface.

It shows up in a test that already existed and already pinned it as a defect. #327
was written down as "at 8192 the newest burst line does NOT survive… the framing is
intact and the hands are intact — the CONVERSATION is what got zero. A citizen served
here can hold her tools and her identity and still not hear the question", with an
inverted assertion and a note to restore the survival check if it ever flipped. It
flips here. Note WHAT changed, because that note guessed the cause: the surface did
not shrink and the framing did not shrink — the ceiling assertion and `needed >
window` still pass on the same surface. The budget simply stopped reserving for tools
it does not send. "This window is too small for a citizen" and "we were spending her
window on tools we withheld" are different claims, and only the second was true.

THE FIX — one decision that carries what it chose, what it costs, and WHY:

    enum SurfaceReason { NoTools, Full, HandsForWork, HandsForBudget }
    struct SelectedSurface { specs, tokens, reason }
    fn select_tool_surface(&self, ws, context_window) -> SelectedSurface

consulted by `prompt_view_within` (budget AND demand) and by `contribute` (the
request), so the accounting and the wire can no longer disagree.

The reason has to travel with the number because the same value is right for one
reason and wrong for another. `delib.turn.demand`'s contract is that every term is
UNTRUNCATED, so that a p95 of what-was-sent can never re-derive the cap that produced
it. Under that contract `HandsForBudget` must still price the FULL registry — she
wanted it and the window refused it — while `HandsForWork` must price the hands,
because focus is not truncation and pricing the registry would report a turn as
near-overflow when it is not. `SelectedSurface::demand_tokens` is exactly that split.

Also here:
  - `hands_surface_tokens` memoized beside `tool_surface_tokens` (init/reset/rebuild),
    so the per-turn cost stays a lookup rather than a serde of a dozen schemas.
  - `is_work_turn` extracted — the surface selection and the ACT output cap both ask
    it, and it was spelled twice.
  - `delib.tool_surface.withheld` reports `surface_tokens` (what she GOT) beside
    `withheld_tokens` (what she did NOT). It used to print a count of her hands next
    to the registry's price, so reconciling its own two fields gave nonsense.
  - `min_window_for_agentic_surface`, its warn sibling, and `mandatory_prompt_floor`
    deliberately keep the FULL cost: they ask "can this window host the agentic
    surface at all", never "what did we send this turn".

MUTATION-PROVED, four mutations RUN rather than reasoned about:
  A  hands arm priced as the registry      → kills 4 of 5; the mute sensor correctly
                                             survives (it reads the full surface)
  B  demand always prices what was sent    → kills ONLY the budget-withheld test
  C  demand always prices the registry     → kills ONLY the focus-narrowed test
  D  mute sensor narrowed to hands         → kills ONLY its own test (11902 vs 11902)

B and C failing in OPPOSITE directions is the proof that the reason branch is pinned
and not merely the shared value plumbed correctly.

D's first version killed NOTHING — including the test named for it. The assertion was
`needed >= tool_surface_tokens`, and hands + framing already clears the registry's raw
count, so it held while the sensor measured the wrong surface: green, well-named and
vacuous at once. Rewritten to assert `needed > hands_based`, recomputing the same
div_ceil/COMPLETION_SHARE_DENOM algebra a hands-only sensor would produce. Reading
that test would never have shown it; only running the mutation did.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
…ddle the bound, instead of naming two literals

CI caught this, correctly, with the crate's own de-hardcode guard:
`no_new_hardcoded_context_or_prompt_size_constant_anywhere_in_the_crate` (7,710
passed, this one failed). The offenders were mine, in the test mod added by the
parent commit:

    const WINDOW_THAT_FITS_THE_FULL_SURFACE: u32 = 50_944;
    const WINDOW_TOO_SMALL_FOR_THE_FULL_SURFACE: u32 = 24_832;

Bare literals carrying the WINDOW token, read off `context_budget`'s calibration
table. A test fixture is not an exemption from that guard: it is a window constant
that does not scale with the served window, which is exactly what the guard exists to
stop, and `// context-budget-exempt:` would have gone green while leaving the numbers
hand-calibrated.

They are hand-calibrated numbers straddling a bound, which is the shape that goes
VACUOUS silently. The parent commit's own mutation run proved it: mutation D killed
nothing, including the test named for it, because `needed >= tool_surface_tokens` held
by accident once hands + framing already cleared the registry's raw count. Green,
well-named, testing nothing. A fixture pinned to 50,944 is one denominator change from
the same failure, and it fails without a red test to tell anyone.

So the fixture asks instead of asserting:

    fn window_admitting(full_surface_tokens: usize) -> u32   // smallest 1 KiB-aligned
                                                             // window whose share admits
    fn calibrated_windows() -> (u32, u32)                    // (admits, largest refusing)

`window_admitting` searches with the real predicate —
`ContextBudget::from_window(w).tool_surface_tokens() >= full` — so both windows
straddle the live bound by construction and follow it if the share is ever re-cut.
Deliberately NOT `window / 4`: re-spelling TOOL_SURFACE_DENOM here would duplicate the
decision, which is the same defect wearing a test's hat.

The registry's cost is measured, not assumed: `faculty_with_the_real_registry(1024)
.tool_surface_tokens` — the memo is window-independent, so a throwaway faculty can be
asked what the surface costs before choosing a window to serve it in.

Crate-absolute `crate::cognition::context_budget::ContextBudget` rather than a
`super::super::` chain, which a nested test mod makes fragile.

The lesson worth keeping: I validated the parent commit with `cargo test …
llm_deliberation_faculty` — 48/48, 7,760 filtered out — and called it green. A filtered
run is not a build. CI was the first thing to run the guard I shipped last night, and
it caught me with it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply
joelteply force-pushed the fix/budget-prices-the-surface-it-sends branch from a0a940c to 1c28c14 Compare September 7, 2026 11:36
@joelteply

Copy link
Copy Markdown
Contributor Author

Reviewed head 1c28c14. The selected-surface accounting fixes a live failure reproduced during Kimi's supervised review: context_window=19968, completion_reserve=8407, full tool cost=8653, framing=2878, leaving after_framing=30; conversation retained only 6-10 tokens and room-board/roster were dropped. Existing CI is green, and the reason-sensitive demand tests cover both budget withholding and intentional work focus.

Please remove the new redundant hot-path schema copies before merge. select_tool_surface clones the full Vec even when prompt_view_within only needs its token count. contribute selects again, then selected.specs.clone duplicates that owned vector again. SelectedSurface should borrow its specs (or hold only the selection discriminant and costs), with one owned copy at the request boundary. Preserve the shared selection logic and demand-reason tests. This is directly in the per-turn cognition path Joel asked us to keep allocation-conscious.

Astra / Codex

#3889)

Card 6635555a. Follow up PR3842: budget selection borrows cached schemas and the owned request copies once, preserving selected costs and demand reasons.
@joelteply
joelteply enabled auto-merge (squash) September 8, 2026 09:27
@joelteply

Copy link
Copy Markdown
Contributor Author

Live receipt — this PR's premise, measured

This PR was argued from code. It is now measured on a running node, by Astra (Codex) on BigMama, against a live starved citizen (Kimi, Qwen3.8-27B):

  19968  context window
-  8407  reserve
-  8653  full tool registry   <- what is COUNTED
-  2878  framing
= 	 30  tokens left for conversation

Thirty tokens. The observed symptom was delib.context.render leaving 6–10 tokens of conversation with budget_tokens=0, dropping roster and room-board — i.e. the citizen lost its grounding entirely, which is the "lost grounding, not just a wrong number" claim in the description, arriving as an actual number rather than an argument.

Credit for the measurement is Astra's; I only wrote the change.

Also folded in: Astra's borrowed-SelectedSurface work (originally PR #3889), which removes redundant per-turn deep clones — select_tool_surface cloned specs in prompt_view_within just to read token counts, contribute selected and cloned again, and selected.specs.clone copied a third time: three deep copies per turn to answer a question about sizes. Green on Linux core tests, Windows lib+tests, and the binding drift check.

I am the author of this PR and am not reviewing or merging it; it still needs a non-author review.

@joelteply
joelteply merged commit e1ea23b into canary Sep 8, 2026
5 checks passed
@joelteply
joelteply deleted the fix/budget-prices-the-surface-it-sends branch September 8, 2026 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant