Files
pipelines/app/notes/chat-turn-diffs.md
T
alan 3374bea2ef Show a chat turn's changed files as a chip that opens its diff
The answer footer shows '3 files changed +9 -2'; tapping opens the turn's
files in the sheet (wide) or a full-screen page (narrow), and a file opens
the run Changes diff viewer. Patches load through the chat's own gateway.
2026-11-09 16:08:35 +00:00

102 lines
5.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Chat per-turn diffs
User request: show per-turn diffs as a compact chip like the tool count, which
opens the actual diff, never raw diff text in the transcript.
## Behaviour
- Server (`internal/server/chat_turn_diff.go`): for every harness, the chat hub
snapshots the chat's recorded directory (`operator_sessions.directory`) at
turn start and compares it at turn end with `internal/filediff`
(`Capture`/`Compare`, the stage-visit capture). A turn the room prompts is
captured synchronously before the prompt is posted; a turn the stream
announces (non-intermediate `step started`, e.g. CLI-originated) is captured
from that frame. The end is the authoritative `step ended` frame. The next
turn's baseline waits for the previous turn's end snapshot.
- Git working trees use filediff's Git path (clean files kept as blob ids and
recovered from Git); other directories use filediff's workspace walk. An
unreadable directory or unavailable capture records nothing (no chip).
- A turn with no changed file records nothing. A changed turn is stored in
`chat_turn_diffs` (SQLite, newest 100 per session, pruned on insert) and
pushed to the chat's watchers as `chat_turn_diffs`.
- Protocol (`internal/protocol/chat_turn_diff.go`):
- `chat_turn_diffs` body `{session_id, turn_diffs: [summary]}`: pushed per
finished turn and attached to every `chat_messages` reply (survives reload
and server restart).
- summary: `turn_id, message_ids (≤16 assistant ids streamed in the turn),
started_at, ended_at (ms), files, additions, deletions, truncated, status,
diagnostic`.
- `chat_turn_diff {session_id, turn_id}` → reply `chat_turn_diff` with
`summary` and `changes` (the `file_changes` shape: per-file patches).
- App: `ChatThreadController.turnDiffs` folds history and pushes for its own
session; `attachChatTurnDiffs` puts a summary on the assistant row holding
one of its message ids (fallback: the first assistant row created inside
the turn's time span). The row footer shows `ChatTurnDiffChip`
("3 files changed +9 −2", accessible label "3 files changed, 9 lines added,
2 lines removed, partly shown, open changes"). Tap: wide window (≥600) opens
the turn sheet (Material bottom sheet / Cupertino popup, like the tool
chip); narrow opens a full-screen "Turn changes" route. The viewer lists
files with `ChangeFileRow`; a file opens the run Changes `FileDiffPage`
(rendered/raw patch, copy, previous/next file). Patches are fetched on open
through the chat's own gateway, so multi-machine chats go to their machine
(`ChatServerGateway` re-keys `session_id` both ways).
## Limits
- Bounds are filediff's: 1 MiB per text file, 32 MiB retained text per
snapshot, 5,000 lines per comparison, 64 KiB per patch, 128 KiB per turn,
10,000 files, 20 s per snapshot. Over-limit files are listed without a patch
with a reason; the summary is `truncated` and the viewer shows the capture's
diagnostic above the files.
- Protected paths (`.git`, `.env*`, keys, `node_modules`, databases …) and
symlinks are never read.
- The diff is observed change in the chat's directory during the turn, not
authorship: another process (a run, another chat) editing the same
directory at the same time shows up too.
- Turns the hub does not observe (no room: nobody watching and nothing
unattended) are not captured. A room released mid-turn drops its baseline.
- Codex `turn/diff/updated` is not used; the generic snapshot covers it.
## Verification (2026-11-09)
```
go test ./internal/server ./internal/pipeline ./internal/protocol ./internal/filediff -count=1
ok git.a-wan.net/alan/pipelines/internal/server 66.283s
ok git.a-wan.net/alan/pipelines/internal/pipeline 7.208s
ok git.a-wan.net/alan/pipelines/internal/protocol 0.203s
ok git.a-wan.net/alan/pipelines/internal/filediff 5.752s
go test ./internal/server -run TestChatTurnDiff -v
--- PASS: TestChatTurnDiffRecordsGitWorkspaceChanges
--- PASS: TestChatTurnDiffNonGitDirectoryUsesWorkspaceSnapshot
--- PASS: TestChatTurnDiffRecordsNothingWithoutChanges
--- PASS: TestChatTurnDiffMarksBoundedPatchesTruncated
--- PASS: TestChatTurnDiffCapturesStreamAnnouncedTurn
--- PASS: TestChatTurnDiffPatchLineCounts
--- PASS: TestChatTurnDiffProtocolRoundTrip
tool/memwatch/full.sh test/features/chat
memwatch: PASS: 1075 passed, 0 failed, 0 skipped; peak 2233 MB; 148s; exit 0 (test/features/chat)
tool/memwatch/full.sh test/core/servers
memwatch: PASS: 50 passed, 0 failed, 0 skipped; peak 1509 MB; 11s; exit 0 (test/core/servers)
tool/memwatch/full.sh test/features/changes
memwatch: PASS: 73 passed, 0 failed, 0 skipped; peak 1710 MB; 12s; exit 0 (test/features/changes)
tool/analyze.sh lib test tool
analyze: clean; 11s; exit 0
tool/analyze.sh
analyze: 0 errors, 8 warnings, 0 infos; 7s; exit 1
```
The 8 whole-app warnings are all `invalid_null_aware_operator` in
`integration_test/discovery_probe_test.dart`, a file this change does not touch.
Browser captures (fixture `tool/search_selection_probe_entry.dart` with
`--params 'chats=1&turndiffs=1'`, `open=/chat/perf-chat-0`, click chip, click
`lib/search/search_box.dart`), Material and Cupertino at 1100x900 and 560x1000:
`/tmp/turn-diff-shots/<platform>-<size>/{01-chip,02-viewer,03-file-diff}.png`.
Inspected: the chip sits in the answer footer beside Copy; the wide viewer is
the turn sheet with the truncation note and three file rows; the narrow viewer
is a full-screen "Turn changes" page; a file opens the rendered patch with
"1 of 3" paging. Not verified: a live harness turn against a real server, and
any device.