Show per-turn usage lines for earlier turns in the `clai2` web UI after a reload
Merged
MP
1mo ago+121+121+012345678901234567890123456789-36-36-01234567890123456789
Fixes #12194
After a reload, the web UI showed no per-turn usage line (↑ … · ↓ … · N requests) for finished turns. The page only builds that line from a live turn_ended message, and a snapshot's history_items carried no turn summaries.
The fix is server-side:
history_entries(new;history_itemsnow wraps it and drops the summaries) puts aTurnSummary(kind: 'turn',outcome: 'completed',seconds: null) after each saved turn. Its usage comes from the saved responses, grouped the way/usagegroups them. To keep those counts the same as/usage, the turn-boundary check is pulled out ofsession_usageinto a sharedstarts_turnhelper.- Only
SessionChannel.snapshotuseshistory_entries. Saved-session previews, task transcripts and the orchestrator chat still usehistory_items, so they are unchanged except that their opaqueh<N>item IDs can now have gaps where summaries were dropped. SessionView(web/client.py, which the orchestrator's workspace tools read) keeps only a snapshot'sItems, as before. It never tracked usage lines.- The page already renders
kind: 'turn'entries withTurnUsage, and it leaves out the duration whensecondsisnull. On the frontend,TurnSummarymoves toprotocol.tsas a wire shape, and the snapshot'sitemstype becomes(Item | TurnSummary)[]. These are type-only changes, so the committedstatic/bundle is byte-identical afternpm run build.
Outcomes aren't saved, so a reloaded failed or cancelled turn shows its usage line instead of the "Failed" / "Interrupted" note.
Tests
The web-ui package has no test runner, so the regression tests are on the Python server side:
test_a_turn_streams_to_the_browser: a browser connecting after a turn gets aturnentry whose usage matches the liveTurnEnded. This fails onmainwith['user', 'tool', 'assistant'] != ['user', 'tool', 'assistant', 'turn'].test_history_entries_follow_each_turn_with_its_usage_as_usage_reports_it: a 3-turn history (one turn with a tool round trip, plus a prompt cancelled before any response) gives one summary per turn, matchingsession_usage(...).turns.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.