Part of the v0.58.7 → HEAD TinyAgents-migration audit (see parent issue). Replaces deleted harness/token_budget.rs (696 lines) semantics that regressed in the crate trim path (TA summarization/mod.rs:43, MessageTrimMiddleware installed at src/openhuman/tinyagents/mod.rs:1330).
Regressions
- Image marker token cost dropped. Legacy priced image markers at
IMAGE_MARKER_TOKEN_COST = 1200 tokens. The crate estimates chars/4, so a large base64 image counts as ~2M tokens → the trim believes the context is massively over budget and can drop everything, including system messages. (The crate trim also reorders system messages to front and is allowed to drop them — both legacy regression guards lost.)
- Trim reserve formula regressed. Legacy reserve:
clamp(window/10, ≥512, ≤max(8192, window/4)) — proportional. New: fixed window − 16384 floored at 1024. An 8k local model's input budget collapses from ~7373 to 1024 tokens, gutting small local-model contexts.
- Trim is silent. Legacy warned with counts; the crate trims with no log/outcome, so context loss is undiagnosable.
Fix plan
- Pre-count multimodal content before the trim sees it: substitute image/file markers with their fixed token cost in the estimator input (adapter-side: wrap or pre-process messages fed to
MessageTrimMiddleware, or supply a custom TokenEstimator if the crate accepts one; if not, upstream that hook).
- Restore the proportional reserve for small windows: compute the trim target ourselves (legacy clamp formula) and configure the middleware with it, instead of the fixed
window − 16384.
- Guarantee system messages survive: assert/keep the system prefix out of the droppable set at the adapter layer; add a regression test with an oversized image + small window asserting the system message survives.
- Log a warn with (messages dropped, tokens before/after) whenever the trim removes anything; consider emitting a
DomainEvent for the UI.
Acceptance criteria
- One large image no longer evicts the system prompt or the rest of the transcript.
- On an 8k-window local model, input budget matches the legacy proportional formula (±rounding).
- Any trim produces a grep-able warn with counts.
Part of the v0.58.7 → HEAD TinyAgents-migration audit (see parent issue). Replaces deleted
harness/token_budget.rs(696 lines) semantics that regressed in the crate trim path (TAsummarization/mod.rs:43,MessageTrimMiddlewareinstalled atsrc/openhuman/tinyagents/mod.rs:1330).Regressions
IMAGE_MARKER_TOKEN_COST = 1200tokens. The crate estimates chars/4, so a large base64 image counts as ~2M tokens → the trim believes the context is massively over budget and can drop everything, including system messages. (The crate trim also reorders system messages to front and is allowed to drop them — both legacy regression guards lost.)clamp(window/10, ≥512, ≤max(8192, window/4))— proportional. New: fixedwindow − 16384floored at 1024. An 8k local model's input budget collapses from ~7373 to 1024 tokens, gutting small local-model contexts.Fix plan
MessageTrimMiddleware, or supply a customTokenEstimatorif the crate accepts one; if not, upstream that hook).window − 16384.DomainEventfor the UI.Acceptance criteria