Skip to content

fix(core): recompute cached_prompt_tokens after system-prompt rebuild - #6337

Merged
bug-ops merged 1 commit into
mainfrom
fix/6332-context-budget-turn0-cache
Jul 16, 2026
Merged

fix(core): recompute cached_prompt_tokens after system-prompt rebuild#6337
bug-ops merged 1 commit into
mainfrom
fix/6332-context-budget-turn0-cache

Conversation

@bug-ops

@bug-ops bug-ops commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Summary

  • assemble_final_system_prompt (crates/zeph-core/src/agent/context/assembly.rs) rewrites messages[0] with the real, per-turn-filtered system prompt every turn, but never recomputed cached_prompt_tokens afterward.
  • On turn 0 this left the counter pinned at the construction-time seed derived from the entire unfiltered skill registry (Agent::new_with_registry_arc), which could reach ~100K tokens regardless of the actual assembled request size (~17-18K tokens in the reported case).
  • This could trigger a premature "context window nearly full" stop in the tool-loop early-stop check (tier_loop.rs) on a brand-new session, and showed a misleading context estimate in the TUI/channel display before the first LLM call.
  • Fix: call self.recompute_prompt_tokens() immediately after messages[0].content = system_prompt; in assemble_final_system_prompt, so the counter always reflects the actual outgoing prompt on every turn.

Closes #6332

Test plan

  • Added regression test rebuild_system_prompt_recomputes_cached_prompt_tokens asserting cached_prompt_tokens reflects the real rebuilt prompt (not the stale seed) after rebuild_system_prompt
  • cargo +nightly fmt --check — clean
  • cargo clippy --profile ci --workspace --all-targets --features "desktop,ide,server,chat,pdf,scheduler,testing" -- -D warnings — clean
  • cargo nextest run --config-file .github/nextest.toml --workspace --features "desktop,ide,server,chat,pdf,scheduler" --lib --bins — 13853 passed
  • rustdoc gate (RUSTFLAGS="-D warnings" RUSTDOCFLAGS="--deny rustdoc::broken_intra_doc_links" cargo doc --no-deps --workspace --features ...) — clean
  • LLM Serialization Gate — live session against local Ollama (qwen2.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

@github-actions github-actions Bot added documentation Improvements or additions to documentation rust Rust code changes core zeph-core crate bug Something isn't working size/M Medium PR (51-200 lines) labels Jul 16, 2026
@bug-ops
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
bug-ops force-pushed the fix/6332-context-budget-turn0-cache branch from be4644b to 6437ef1 Compare July 16, 2026 19:54
@bug-ops
bug-ops merged commit fc98c3b into main Jul 16, 2026
43 checks passed
@bug-ops
bug-ops deleted the fix/6332-context-budget-turn0-cache branch July 16, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working core zeph-core crate documentation Improvements or additions to documentation rust Rust code changes size/M Medium PR (51-200 lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

context-budget early-stop check reports fixed cached_prompt_tokens on turn-0 regardless of actual request size

1 participant