fix(core): recompute cached_prompt_tokens after system-prompt rebuild - #6337
Merged
Conversation
bug-ops
enabled auto-merge (squash)
July 16, 2026 19:53
assemble_final_system_prompt rewrote messages[0] with the real, per-turn-filtered system prompt but never recomputed the cached token counter afterward. On turn 0 this left cached_prompt_tokens pinned at the construction-time seed derived from the entire unfiltered skill registry, which could reach ~100K tokens regardless of the actual request size. This could trip the tool-loop context-budget early-stop check on a brand-new session and showed a misleading context estimate in the TUI/channel display before the first LLM call. Closes #6332
bug-ops
force-pushed
the
fix/6332-context-budget-turn0-cache
branch
from
July 16, 2026 19:54
be4644b to
6437ef1
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.
Summary
assemble_final_system_prompt(crates/zeph-core/src/agent/context/assembly.rs) rewritesmessages[0]with the real, per-turn-filtered system prompt every turn, but never recomputedcached_prompt_tokensafterward.Agent::new_with_registry_arc), which could reach ~100K tokens regardless of the actual assembled request size (~17-18K tokens in the reported case).tier_loop.rs) on a brand-new session, and showed a misleading context estimate in the TUI/channel display before the first LLM call.self.recompute_prompt_tokens()immediately aftermessages[0].content = system_prompt;inassemble_final_system_prompt, so the counter always reflects the actual outgoing prompt on every turn.Closes #6332
Test plan
rebuild_system_prompt_recomputes_cached_prompt_tokensassertingcached_prompt_tokensreflects the real rebuilt prompt (not the stale seed) afterrebuild_system_promptcargo +nightly fmt --check— cleancargo clippy --profile ci --workspace --all-targets --features "desktop,ide,server,chat,pdf,scheduler,testing" -- -D warnings— cleancargo nextest run --config-file .github/nextest.toml --workspace --features "desktop,ide,server,chat,pdf,scheduler" --lib --bins— 13853 passedRUSTFLAGS="-D warnings" RUSTDOCFLAGS="--deny rustdoc::broken_intra_doc_links" cargo doc --no-deps --workspace --features ...) — cleanqwen2.5:7b,testing-local-only.toml, cloud accounts still exhausted) reproducing the exact reported scenario (~19.7K message chars + ~18.2K tool-schema chars, closely matching the issue's reported sizes): well-formed request/response round-trip, no 400/422, tool call parsed correctly, and no spurious context-budget early-stop triggered