Repository navigation
fix(cognition): the tool surface is a share of the WINDOW, not a property of the turn kind - #3829
Merged
joelteply merged 1 commit intoSep 7, 2026
Conversation
…erty of the turn kind
A 0.5B citizen published her prompt preamble to the room instead of
answering. Her `delib.turn.demand` row said why (IntelMac, 2026-09-07
04:1xZ):
context_window 32,768
demand_tokens 31,895 (over_window 0.973)
completion_reserve 16,384
framing_tokens 3,189
grounding_tokens 36
conversation_tokens 3,551
31,895 - (16,384 + 3,189 + 36 + 3,551) = 8,735 unaccounted
Those 8,735 tokens are the 37-schema tool surface — the same row reports
`tools_n=37`, and `describe_tool_tokens`' own comment independently puts 37
schemas at ~8.5k. So the MENU was larger than the framing and the entire
conversation combined (8,735 vs 6,740), and demand reached 97.3% of the
window before the model wrote a token.
The cost was already memoized and compared against nothing. Narrowing to her
hands was gated on the kind of turn:
let is_work_turn = ws.workspace_deliverable && !hands_specs.is_empty();
let tools = if native_specs.is_empty() { None }
else if is_work_turn { hands } else { ALL 37 };
A work turn got the small surface; every MESSAGE turn got the whole
registry. So the tier least able to afford 37 schemas received them, on the
commonest turn type.
Which surface she is offered is a question about her WINDOW. `ContextBudget`
already owns every "how much may I re-inject" bound as a fraction of the
served window, so the answer goes there: `tool_surface_tokens()`, 1/4.
Calibrated, not invented — the denominator is chosen so the lanes where the
full surface demonstrably works keep it, and only the ones where it crowds
out the conversation narrow. Against the same 8,735-token surface:
50,944 (M5 Ornith lane) -> 12,736 allowed -> unchanged
32,768 (IntelMac 0.5B) -> 8,192 allowed -> narrows
24,832 (5090 per-slot) -> 6,208 allowed -> narrows
4 is also the denominator this module already uses for its two other "one
big thing may take a quarter" bounds, so the menu is held to the same share
as the largest single piece of content beside it.
The fallback is her HANDS, never nothing. `hands_surface` keeps the
`commands/` prefix, so the discovery pair survives and a withheld verb stays
one `commands/list` away. Amputating the surface is the #206 cliff — 14/14
SWE acts spent on `commands/help` with 0 edits — and this must never become
that. Bound the menu; never remove the index.
`delib.tool_surface.withheld` fires only on the budget path: a work turn
narrowing is routine, but a message turn narrowing means she cannot see
verbs she would otherwise have been offered, and "why did she never call X"
has to be answerable.
Both arms of the calibration test are proved by mutation: denominator 8
fails on "a 50,944-token lane must still be offered the whole surface", and
denominator 2 fails on "a 32768-token window cannot afford 37 schemas and
must narrow to hands". A one-sided test would have passed under one of them.
Card e20e064f.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
Contributor
Author
|
No objection (M5). This is the shape I described in the room and the right layer: ContextBudget::tool_surface_tokens (window/4, unknown → never narrows), the narrowing keyed on |
joelteply
deleted the
fix/the-tool-surface-is-a-share-of-the-window-not-a-turn-kind
branch
September 7, 2026 05:28
joelteply
added a commit
that referenced
this pull request
Sep 7, 2026
…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
joelteply
added a commit
that referenced
this pull request
Sep 8, 2026
…not the registry it withholds (card dec1a7ff) (#3842) * fix(cognition): the message budget prices the tool surface it SENDS, 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 * fix(cognition): the test windows ASK ContextBudget which windows straddle 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 * perf(cognition): borrow tool surfaces until request ownership boundary (#3889) Card 6635555a. Follow up PR3842: budget selection borrows cached schemas and the owned request copies once, preserving selected costs and demand reasons. --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A 0.5B citizen published her prompt preamble to the room instead of
answering. Her
delib.turn.demandrow said why (IntelMac, 2026-09-0704:1xZ):
Those 8,735 tokens are the 37-schema tool surface — the same row reports
tools_n=37, anddescribe_tool_tokens' own comment independently puts 37schemas at ~8.5k. So the MENU was larger than the framing and the entire
conversation combined (8,735 vs 6,740), and demand reached 97.3% of the
window before the model wrote a token.
The cost was already memoized and compared against nothing. Narrowing to her
hands was gated on the kind of turn:
A work turn got the small surface; every MESSAGE turn got the whole
registry. So the tier least able to afford 37 schemas received them, on the
commonest turn type.
Which surface she is offered is a question about her WINDOW.
ContextBudgetalready owns every "how much may I re-inject" bound as a fraction of the
served window, so the answer goes there:
tool_surface_tokens(), 1/4.Calibrated, not invented — the denominator is chosen so the lanes where the
full surface demonstrably works keep it, and only the ones where it crowds
out the conversation narrow. Against the same 8,735-token surface:
4 is also the denominator this module already uses for its two other "one
big thing may take a quarter" bounds, so the menu is held to the same share
as the largest single piece of content beside it.
The fallback is her HANDS, never nothing.
hands_surfacekeeps thecommands/prefix, so the discovery pair survives and a withheld verb staysone
commands/listaway. Amputating the surface is the #206 cliff — 14/14SWE acts spent on
commands/helpwith 0 edits — and this must never becomethat. Bound the menu; never remove the index.
delib.tool_surface.withheldfires only on the budget path: a work turnnarrowing is routine, but a message turn narrowing means she cannot see
verbs she would otherwise have been offered, and "why did she never call X"
has to be answerable.
Both arms of the calibration test are proved by mutation: denominator 8
fails on "a 50,944-token lane must still be offered the whole surface", and
denominator 2 fails on "a 32768-token window cannot afford 37 schemas and
must narrow to hands". A one-sided test would have passed under one of them.
Card e20e064f.
Co-Authored-By: Claude Opus 5 (1M context) [email protected]
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE