feat(webui): visualize context usage by logical round (#5649)

* feat(webui): visualize per-request context reuse

* refactor(webui): report usage by logical round

* fix(webui): align context chart with logical rounds

* refactor(webui): derive turn usage from logical rounds

* fix(webui): polish context usage details
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-04 15:13:17 +08:00
1 parent 8fb7bc34ec
commit 6b82d642fc
35 files changed
+994 -274

No files matched your search

+21
View File
@@ -629,6 +629,7 @@ async def test_runner_uses_no_tools_finalization_after_max_iterations():
arguments={"path": "."},
)
],
usage=LLMUsage.reported(input_tokens=1, output_tokens=1),
)
return LLMResponse(
content="Read the directory twice. More investigation remains.",
@@ -660,6 +661,14 @@ async def test_runner_uses_no_tools_finalization_after_max_iterations():
assert calls[-1]["tools"] is None
assert "tool-call budget" in calls[-1]["messages"][-1]["content"]
assert tools.execute.await_count == 2
assert result.usage is not None
assert result.usage.input_tokens == 12
assert result.usage.output_tokens == 9
assert [(item.input_tokens, item.output_tokens) for item in result.round_usages] == [
(1, 1),
(1, 1),
(10, 7),
]
@pytest.mark.asyncio
@@ -914,6 +923,11 @@ async def test_runner_retries_empty_final_response_with_summary_prompt():
assert result.usage is not None
assert result.usage.input_tokens == 13
assert result.usage.output_tokens == 9
assert [(item.input_tokens, item.output_tokens) for item in result.round_usages] == [
(5, 1),
(5, 1),
(3, 7),
]
@pytest.mark.asyncio
@@ -1258,6 +1272,13 @@ async def test_runner_accumulates_usage_and_preserves_cache_reads():
assert result.usage.cache_read_tokens == 230 # 80 + 150
assert result.usage.context_tokens == 200
assert result.usage.request_count == 2
assert [
(item.input_tokens, item.cache_read_tokens)
for item in result.round_usages
] == [
(100, 80),
(200, 150),
]
@pytest.mark.asyncio
+6
View File
@@ -742,6 +742,12 @@ async def test_runner_fits_each_malformed_retry_with_its_actual_tools(monkeypatc
assert None in estimated_tools
assert [len(call["messages"]) for call in calls] == [1, 1, 1]
assert result.final_content == "recovered"
assert result.usage is not None
assert result.usage.input_tokens == 300
assert result.usage.output_tokens == 30
assert result.usage.request_count == 3
# Recovery dispatches contribute to the total without becoming extra chart bars.
assert result.round_usages == [result.usage]
assert result.messages == [
{"role": "user", "content": "use a tool"},
{"role": "assistant", "content": "recovered"},
+6 -2
View File
@@ -100,7 +100,10 @@ async def test_runtime_event_publisher_consumes_turn_metadata_on_complete() -> N
bus.subscribe(seen.append)
publisher.record_turn_runtime("cli:direct", "runtime")
publisher.record_turn_latency("cli:direct", 123)
publisher.record_turn_usage("cli:direct", LLMUsage.reported(input_tokens=40, output_tokens=2))
first_round = LLMUsage.reported(input_tokens=40, output_tokens=2)
second_round = LLMUsage.reported(input_tokens=60, output_tokens=3)
publisher.record_turn_usage("cli:direct", [first_round])
publisher.record_turn_usage("cli:direct", [second_round])
await publisher.turn_completed(
channel="cli",
@@ -121,7 +124,8 @@ async def test_runtime_event_publisher_consumes_turn_metadata_on_complete() -> N
assert first.context.metadata == {"source": "test"}
assert first.latency_ms == 123
assert first.runtime == "runtime"
assert first.usage == LLMUsage.reported(input_tokens=40, output_tokens=2)
assert first.usage == first_round + second_round
assert first.round_usages == (first_round, second_round)
assert isinstance(second, TurnCompleted)
assert second.latency_ms is None
assert second.runtime is None
+25
View File
@@ -548,6 +548,26 @@ def test_replay_turn_end_preserves_usage_semantics(tmp_path, monkeypatch) -> Non
"context_tokens": 8_200,
"request_count": 3,
},
"round_usages": [
{
"prompt_tokens": 4_000,
"completion_tokens": 200,
"cached_tokens": 2_000,
"request_count": 1,
},
{
"prompt_tokens": 4_200,
"completion_tokens": 300,
"cached_tokens": 3_672,
"request_count": 1,
},
{
"prompt_tokens": 4_200,
"completion_tokens": 323,
"cached_tokens": 4_000,
"request_count": 1,
},
],
"context_window_tokens": 128_000,
},
):
@@ -562,6 +582,11 @@ def test_replay_turn_end_preserves_usage_semantics(tmp_path, monkeypatch) -> Non
"context_tokens": 8_200,
"request_count": 3,
}
assert [item["prompt_tokens"] for item in messages[-1]["roundUsages"]] == [
4_000,
4_200,
4_200,
]
assert messages[-1]["contextWindowTokens"] == 128_000
assert messages[-1]["latencyMs"] == 18_200