Repository navigation
The view starts by the caller's token oracle: a start keeps its grant as the final prompt prices it - #5819
Merged
Conversation
joelteply
force-pushed
the
fable/view-start-token-oracle
branch
from
November 7, 2026 14:19
2654bad to
cb3b9a1
Compare
… as the final prompt prices it Codex's defect: View::start converted a source's TOKEN grant into characters at the source's AVERAGE tokens per character (start_grants: share * full_chars / requested_max), so a source of cheap long old turns and dear short new ones bought characters enough for new turns whose own cost overran the grant. continuum-context::cost::Cost<T> is the caller's price of a block (block, stored), with Chars for callers whose grants are characters and cost::whole as the one owner of an item's whole price (source::whole_chars deleted). View::start / turn_over / start_would_restore / readmit, view::claims, view::floor, the allotment (allot, fold_of) and depth::collapsible all price by it; Claim's sizes (floor, full, newest, logged), depth's Collapsible (whole, saves) and Growing (base, logged) are in the oracle's unit, and Growing::per_char is gone. Motion's order stays in characters. Cognition's oracle is ViewCost(calibration): each block at calibration.charge(message_cost), the function the final prompt is charged with (charged per block it errs high by under a token a block, never low). view_claims prices the plan's claims by the same oracle through view::claims (its own floor/whole pricing removed, view.rs no longer re-charges them); start_grants hands back token shares (the share itself, or the claim's full when granted all), takes one act's growth in tokens from fit_view's next_act_tokens, and reads Claim::newest directly; admit_view hands readmit its token budget. Removed char conversions: start_grants' grant mapping and its newest_of and wanted_tokens/ wanted_chars ratio; depth's per_char (claims[i].growing(share, requested_max / full_chars)); admit_view's budget * view.chars() / view_tokens limit; view_claims' raw-then-charge path; the fold decision in chars (fold_of now decides in the oracle the view allots by). Tests: Codex's regression (previous commit) passes on this base before and after: #5814 removed the per-message template tokens, so his short "ok" turns cost nothing here. a_view_start_keeps_its_token_grant_when_her_newest_turns_are_denser is the same defect on this base (CJK newest turns: a byte-counted estimate makes them three times as dense per char): red before (36 tokens in 3 messages against 32), green after. a_view_start_with_uniform_message_sizes_ keeps_what_it_kept: five newest turns before and after. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…er allocation estimate #5814 removed PER_MESSAGE_TEMPLATE_TOKENS (no constant) but nothing measured in its place, so an "ok" turn cost 0 and a start priced by text alone kept twenty of them against a 32-token grant (Codex's 100 > 32). Engine (llama_server/template_overhead.rs): the serving lane's template is measured once per engine process and model, from the reconcile that already reads its build (observe_template beside observe_served_engine). Probe messages per kind (system, user, assistant, native tool result, native tool call with 0-3 parameters) are rendered in the middle of a conversation through /apply-template on the request wire (request_body::wire_messages) and counted by /tokenize (HTTP-thread calls, no slot, no decode); each records the tokens it added. MessageShape is the one owner of how a message is classified (kind, calls or results, parameters). TemplateMeasurement::Unmeasured is a type; the adapter seam reports it (AIProviderAdapter::template_overhead, OpenAI adapter for the serving lane, the placement switch forwards). Probe: serving.template_overhead. Cognition (prompt/template.rs): TemplateEstimate derives each kind's share against her own text estimate (message_cost) at its HIGHEST observation, a call's as the least-overcharging line over its parameters on or above every observation, so the estimate errs high on every observation and states its spread. PromptCalibration carries it (fit_within, from the adapter); estimate(message) = message_cost + its kind's share (unmeasured: text alone, density carries the template, probed on delib.prompt.density); estimated_prompt_tokens counts it, so density calibrates the rest. The view's oracle is renamed for what it is: AllocationEstimate (cognition) over continuum_context::cost::Estimate (the trait, was Cost). Its docs say it decides what to keep, errs high, claims no exact count or token prefix; dispatch still rests on the engine's exact quote. Measured on the M5 engine (Qwen3.6-35B-A3B-Q4_K_M, build b11017-444f386cf, 2026-11-07T11:38Z), tokens each probe added: plain turns 6/12/10/7/11 for every role, tool results 10/16/14/11/15, calls 17/31/43/57 (0/1/2/3 parameters). Beyond her text estimate: system/user/assistant highest 9, lowest 1 (spread 8); tool result 13, lowest 5 (spread 8); call line 17 + 8 per parameter (base lowest 14, spread 3). Tests: Codex's regression stays byte-identical but for the View::start cost argument, and stays green (its "ok" turns are priced by message_cost, which counts text). The template's red/green is a_view_start_keeps_its_token_grant_with_the_measured_template_overhead on his conversation: priced by text alone the start keeps 20 "ok" turns, 120 tokens as the recorded engine counts them (6 each) against 32; priced by the allocation estimate it keeps within every grant 32..512, and the recorded engine count is under the estimate. the_allocation_estimate_errs_high_on_the_recorded_engine: every probe is estimated at or above the engine's count, and a six-message multi-role conversation counted at 97 (92 without the 5-token generation suffix) is estimated at 118. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…l's refit all estimate through PromptCalibration::estimate The view's start priced each block by her allocation estimate (text plus the measured template share) while the final fit and a refusal's refit priced text alone (LlmDeliberationFaculty:: messages_cost): two pricing paths for one fact. Now PromptCalibration::estimate / estimate_all is the one owner, and every prompt-assembly reader goes through it: - PromptMessages carries its pricing (set by its one builder from the faculty's binding, and by the act's view build to that act's calibration); required_parts, tokens, recent_move_evidence, fit_messages (each message and the quiet-tick minimum) and the newest result's levels price with it. - DeliberationPromptView carries the pricing its messages were estimated with; refit_rejected_prompt reprices and rebuilds with it. - The view build (view tokens, the quiet minimum, the tail budget, the sent sample, the assembled context's used tokens), fit_view (view and admitted tokens) and the budget ledger (estimate_system for the system message) price through it. Removed: LlmDeliberationFaculty::messages_cost (no production caller left; tests use PromptCalibration::default().estimate_all, numerically the same text-only estimate when unmeasured). message_cost stays as the text part estimate builds on, documented as never a price by itself. PromptCalibration derives Debug (the view snapshot carries it). Codex's regression changes one more call for the removal: messages_cost(&rendered) -> PromptCalibration::default().estimate_all(&rendered) (same number; his test stays green). Test: the_final_fit_and_a_view_start_price_a_multi_role_sequence_identically: a user turn, a native call, its result, an assistant turn and a CJK user turn, priced at the recorded engine's template: the view's start (AllocationEstimate per block) and the final fit (its receipt's history_tokens) give the same number, and a budget of that number keeps all of it. The engine's exact quote of the finalized body before generation stays the authority for dispatch. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
…board tests start and turn over at a cost Rebasing onto canary (7b7d277) brought tests written against the character-unit view API (#5815 shed, #5827 collapsed navigation). They now pass the cost the API requires and read Claim::full. The rebase's conflict in context_streams/tests.rs had dropped this branch's own updates to that file; they are re-applied, minus the tally test #5827 moved into blocks.rs. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo
joelteply
force-pushed
the
fable/view-start-token-oracle
branch
from
November 7, 2026 17:36
cb3b9a1 to
bf0e87c
Compare
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.
How this serves the minds: when her view starts a new epoch, it now stays within its token grant. The final fit no longer has to cut what she was just shown. Plan line 2 (a mind's context as an epoch), goal 7 (one budget).
Stacked on #5814 (
fable/every-claim-collapses). The merge gate holds this PR until #5814 lands.The defect (Codex, reproduced on Windows)
View::start→start_grantsturned each source's TOKEN grant into characters, using the source's AVERAGE tokens per character (share * full_chars / requested_max). Long old turns cost few tokens per character and short new ones cost many. So the average bought enough characters for several new turns whose own cost was over the grant (100 tokens against a 32-token grant on his base).The fix: the view decides with the caller's own cost function
continuum-context::cost::Cost<T>is the caller's price of a block (block, plusstoredfor a body evicted from RAM).Charsserves callers whose grants are in characters.cost::wholeis now the only place an item's whole price is computed;source::whole_charsis deleted.View::start,turn_over,start_would_restore,readmit,view::claims,view::floor, the allotment (allot,fold_of) anddepth::collapsible.Claimsizes (floor,full,newest,logged),depth::Collapsible(whole,saves) anddepth::Growing(base,logged) are all in that cost's units now.Growing::per_charis gone.Motion) is still measured in characters.ViewCost(calibration): each block costscalibration.charge(message_cost(..)), the same function the final prompt is charged with.view_claimsbuilds the plan's claims with the same cost function throughview::claims. Its own floor and whole pricing is removed, andview.rsno longer charges the claims a second time.start_grantsreturns token shares: the share itself, or the claim's full cost when the source was granted everything it asked for.fit_view'snext_act_tokens, and readsClaim::newestdirectly.admit_viewpasses its token budget straight toreadmit.Character conversions removed:
start_grants' grant mapping, itsnewest_of, and itswanted_tokens / wanted_charsratioper_char(growing(share, requested_max / full_chars))admit_view'sbudget * view.chars() / view_tokenslimitview_claims' price-raw-then-charge pathTests
a_view_start_keeps_its_token_grant_with_uneven_message_sizes: cherry-picked byte for byte as the first commit (c0cc361, from 9f1b18c). It passes on this base both before and after the fix.PER_MESSAGE_TEMPLATE_TOKENS, so his short"ok"turns cost 0 tokens here and nothing overruns.View::startcall, to pass the cost function (the API now requires it).a_view_start_keeps_its_token_grant_when_her_newest_turns_are_denser: the same defect on this base.a_view_start_with_uniform_message_sizes_keeps_what_it_kept: with uniform sizes the start keeps its 5 newest turns, before and after.a_measured_depth_never_starts_her_under_her_newest_item: the fixture is rewritten in tokens. The rounding gap it guarded came from the removed per-char conversion; its invariant still holds.Second commit (b9ccbd0): the template's share, measured per kind (card 484d0c47)
#5814 was right to remove
PER_MESSAGE_TEMPLATE_TOKENS: it was a constant. But it measured nothing in its place, so an"ok"turn now costs 0 tokens. A start priced on text alone keeps twenty of them against a 32-token grant, which is Codex's 100 > 32.Engine side (
llama_server/template_overhead.rs):/apply-templatein the same wire format real requests use, and is counted by/tokenize. These are HTTP-thread calls: no slot, no decode.MessageShapeis now the only place a message is classified: its kind, how many calls or results it carries, and how many parameters.TemplateMeasurement::Unmeasuredis a type, not a number. It reaches the caller throughAIProviderAdapter::template_overhead.Cognition side (
prompt/template.rs):TemplateEstimateworks out each kind's share as the highest amount the engine was observed adding beyond her own text estimate. For tool calls it is a line over the parameter count, the one that sits on or above every observation while overcharging least.PromptCalibrationcarries it, andestimate(message)adds it to the text estimate.estimated_prompt_tokensincludes it, so the measured density calibrates only the remainder.delib.prompt.densityprobe reports which case applied.The view's cost function is renamed for what it is:
AllocationEstimatein cognition, implementing thecontinuum_context::cost::Estimatetrait (formerlyCost). Its docs say it decides what the view keeps, errs high, and claims no exact token count or token prefix. Dispatch does not go through it; the engine's exact quote of the finalized body is still the authority there.Measured on the M5 engine (Qwen3.6-35B-A3B-Q4_K_M, build
b11017-444f386cf, 2026-11-07T11:38Z, via/apply-template+/tokenize; recorded as the fixturetemplate::recorded_qwen36):Tokens each probe added to the conversation:
Per-kind share beyond her text estimate:
Error bound, tested (
the_allocation_estimate_errs_high_on_the_recorded_engine):Red/green for the template:
View::startcost argument the new API requires. It stays green before and after, because its"ok"turns are priced bymessage_cost, which counts text only.a_view_start_keeps_its_token_grant_with_the_measured_template_overheadruns on his conversation:"ok"turns. The recorded engine counts those at 120 tokens (6 each), against a 32-token grant.Third commit (2654bad): one price for a message (card 484d0c47)
The view's start, the final fit (
fit_messages) and the retry after an engine refusal (refit_rejected_prompt) now all price a message throughPromptCalibration::estimate/estimate_all. Before, the start used the allocation estimate while the fit and the retry used text-onlymessages_cost: two pricing paths for one fact.PromptMessagesandDeliberationPromptViewcarry the pricing they were estimated with.fit_viewand the budget ledger.LlmDeliberationFaculty::messages_costis removed: no production caller was left. Tests now usePromptCalibration::default().estimate_all, which gives the same number when the template is unmeasured.message_coststays, as the text-only part thatestimatebuilds on.messages_cost(&rendered)becomesPromptCalibration::default().estimate_all(&rendered), same number). It stays green.the_final_fit_and_a_view_start_price_a_multi_role_sequence_identically. On a user turn, a native call, its result, an assistant turn and a CJK user turn, at the recorded engine's template, the view's start and the final fit's receipt give the same number, and a budget of exactly that number keeps the whole sequence.Measured
Run on the M5 (local, nice 19, own target dir), commit 2654bad (re-run after the third commit), 2026-11-07:
cargo test -p continuum-context(lib + replay example): 98 + 21 passed. Same counts as the base; no tests added or removed in this crate.cargo test -p continuum-cognition --lib --features test-fixtures,metal: 970 passed, 0 failed, 1 ignored. That includes the 3 tests above, the two template tests and the same-price test.cargo test -p continuum-engine --lib -- llama_server: 96 passed (including the template module test).cargo test -p continuum-core --lib --features test-fixtures,metal -- deliberation(includesevery_claim_collapses): 18 passed, 1 failed.pinned_act_result_reaches_the_prompt_even_with_an_empty_broadcast(panics atcustody.handle().expect("saved inventory")).tools/scripts/oversized-edits.sh: exit 0ratchets: exit 0, every count at or below the basecargo clippy -p continuum-context --all-targets: no warnings in the crate.delib.view.sizedagainstdelib.promptfit receipts over a 15-minute read.Rebased onto canary 7b7d277 (2026-11-07), head bf0e87c
#5814 merged as e5f57d9, so this branch now holds only its own four commits plus one rebase commit.
Conflicts, resolved in favor of canary's newer behavior:
epoch.rs: kept canary'sView::withholdof shed sources (A shed source's backlog waits for a start that funds it (Atlas: 270k chars appended, a turnover every other act) #5815), with this PR'scostparameter added.context_streams/tests.rs: kept canary's removal of the tally test, which Views are state: the board and the wall are keyed items in collapsed navigation (card 00f719c0) #5827 moved intoblocks.rs. This PR's other updates to that file were re-applied.Rebase commit bf0e87c: canary's new tests were written against the character-based API (
view/tests/shed.rsfrom #5815,context_streams/blocks.rsfrom #5827). They now pass the cost and readClaim::full.Measured on the M5 (local, nice 19, own target dir, serving untouched) at bf0e87c:
cargo test -p continuum-context --lib --examples: 103 + 21 passed.cargo test -p continuum-cognition --lib --features test-fixtures,metal: 973 passed, 0 failed, 1 ignored.cargo test -p continuum-engine --lib --features test-fixtures,metal -- llama_server: 101 passed.cargo test -p continuum-core --lib --features test-fixtures,metal -- deliberation: 19 passed, 0 failed. The earlierpinned_act_result...failure is fixed on canary by The pinned-act prompt test installs its own writer fixture (card 4f57ad95) #5825.ratchets origin/canary: exit 0, every count unchanged.oversized-edits.sh origin/canary: clean.🤖 Generated with Claude Code
https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo