Show per-turn usage lines for earlier turns in the `clai2` web UI after a reload

Merged
MP
1mo ago
+121-36

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_items now wraps it and drops the summaries) puts a TurnSummary (kind: 'turn', outcome: 'completed', seconds: null) after each saved turn. Its usage comes from the saved responses, grouped the way /usage groups them. To keep those counts the same as /usage, the turn-boundary check is pulled out of session_usage into a shared starts_turn helper.
  • Only SessionChannel.snapshot uses history_entries. Saved-session previews, task transcripts and the orchestrator chat still use history_items, so they are unchanged except that their opaque h<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's Items, as before. It never tracked usage lines.
  • The page already renders kind: 'turn' entries with TurnUsage, and it leaves out the duration when seconds is null. On the frontend, TurnSummary moves to protocol.ts as a wire shape, and the snapshot's items type becomes (Item | TurnSummary)[]. These are type-only changes, so the committed static/ bundle is byte-identical after npm 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 a turn entry whose usage matches the live TurnEnded. This fails on main with ['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, matching session_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.