fix(agent): clamp context_percent to 100 when the window is a guess#214
Open
AmirF194 wants to merge 1 commit into
Open
fix(agent): clamp context_percent to 100 when the window is a guess#214AmirF194 wants to merge 1 commit into
AmirF194 wants to merge 1 commit into
Conversation
resolve_context_window() returns None when a model is not in litellm's or OpenRouter's context-window table, and the caller falls back to context_window_tokens (65536 by default). context_percent was then computed against that guessed denominator even when real usage legitimately exceeded it while staying well under the model's true, larger window, so the TUI context gauge could read past 100% while requests kept succeeding. Clamp the percent to 100 so an unresolved window never renders as over budget. context_max itself is unchanged (still surfaced as-is for the used/max label); only the derived percent is bounded. Adds a regression test that drives the real per-turn usage path with a guessed window smaller than actual usage and asserts context_percent stays at 100 instead of the raw (unclamped) ratio. Fixes EverMind-AI#124 Co-authored-by: Claude (claude-sonnet-5) <noreply@anthropic.com>
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.
Root cause:
resolve_context_window()returnsNonewhenever a model isn't in litellm's static map or OpenRouter's live/modelstable, and the per-turn usage handler inraven/agent/loop/main.pyfalls back toself.context_window_tokens(65536 by default) as the denominator forcontext_percent. That fallback is a guess, not the model's real window. Real usage can legitimately pass 65536 tokens while staying well under a model's true window (128k/200k/1M), so the computed percent renders past 100% even though the request keeps succeeding.Fix: clamp
context_percentto 100.context_maxandcontext_usedare unchanged (still the real numbers behind the used/max label); only the derived percent is bounded, so the gauge never implies an over-budget state that isn't real.raven/tui_rpc/methods/session.py's_baseline_usageuses the same fallback-window pattern but always pairs it withcontext_used=0(the docstring: "Usage starts at zero for a fresh session by design... counters refresh on the next turn"), so it can't itself produce a percent over 100 and needed no change.Verified in a clean
python:3.12-slimcontainer (uv-managed venv,uv sync --frozen --extra dev --dev):test_usage_sink_context_percent_clamps_when_window_is_a_guess, which drives the real_process_messageusage-sink path with astubmodel (never resolves viaresolve_context_window) and a configured window smaller than actual usage.main(800 == 100, i.e. the raw unclamped ratio) and passes with the fix.uv run --extra dev pytest tests/test_agent_loop_usage_sink.py tests/test_tui_rpc_session_init_bundle.py -q-> 17 passed.ruff check/ruff format --checkon both changed files, andpre-commit run --files raven/agent/loop/main.py tests/test_agent_loop_usage_sink.py(ruff, trailing-whitespace, end-of-file-fixer, large-files, merge-conflict, private-key) all pass.Not changed: the reporter's other suggestion (mark the window as estimated/unknown to the UI instead of clamping) would need a new wire field in
raven/tui_rpc/models.pyplus TUI/bridge changes to consume it, which is a separate, larger concern; happy to follow up if that's wanted.Fixes #124
Co-authored-by: Claude (claude-sonnet-5) noreply@anthropic.com