Show the same diff and counts on a reloaded `clai2` web UI edit card as on the live one

Merged
MP
1mo ago
+134-4

Fixes #12193

Why we make these changes

While a turn runs, an edit_file card shows the harness's real diff (FileEditedEvent). After a reload, history_items sent the card without diffs. The browser then rebuilt the diff from the arguments, with every line of old_text as - and every line of new_text as +. So the issue's edit went from +4 −0 to +5 −1.

history_items now gives each successful EditFileCallPart (matched by tool kind and outcome == 'success', as web/changes.py does) a line diff of each replacement. Each diff is one unnumbered hunk with full context, because a saved call doesn't say where in the file it was.

Edits that failed, edits too large to diff (over the harness's 65,536 line-pair bound), and edits that only add or drop a final line feed get no diff, as before. The browser keeps showing their arguments.

Alternative not taken: persisting the live diff in ToolReturnPart.metadata. That would change what the harness edit_file tool returns for every user, and it wouldn't fix sessions already saved. Known limitation: a replacement that starts or ends mid-line can count slightly differently from the whole-file diff. The issue's case (whole-line anchors) matches exactly.

New public surface

none

User-visible behavior

Reloading a session whose edit added sub under return a + b:

  • before: +5 −1, showing - return a + b and + return a + b
  • after: +4 −0, the same lines as the live card

Verification

  • test_a_reloaded_edit_shows_the_lines_the_live_one_did (tests/clai2/test_web_parts.py) runs a real Agent with FileSystem for three cases: an addition after a prefix anchor, a deletion, and a replacements batch. It translates the live events with EventTranslator and checks that history_items of the saved messages changes the same lines. All 3 cases fail on main.
  • test_a_reloaded_edit_without_a_diff covers the fallbacks: failed, malformed, incomplete, too large, and line-feed-only edits.
  • The web UI has no frontend test runner, so the regression test is on the server side, which is where the fix is.
  • Ran tests/clai2/test_web_parts.py, test_web.py, test_web_changes.py and test_tasks.py (168 passed), plus ruff and pyright on the changed files.

What changes for existing users

A reloaded edit card shows the same changed lines and counts as the live one. Nothing else changes.

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.

Summary by cubic

Makes a reloaded edit_file card in the clai2 web UI show the same changed lines and counts as the live card, instead of rebuilding the diff from tool arguments (which counted every replaced line as a + or -).

  • history_items now computes a line diff for each successful edit's replacements (old_text/new_text or replacements), as one full-context, unnumbered hunk since a saved call doesn't record its file position.
  • Failed edits, edits over the 65,536 line-pair bound, and edits that only add or drop a final line feed get no diff, so the browser keeps showing their arguments.
  • Known limitation: a replacement that starts or ends mid-line can count slightly differently from the whole-file diff; the issue's whole-line case matches exactly.