From 0696ab849ae3f5b6c68d67ec8bfaaadde55bf5ee Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 9 Nov 2026 16:08:35 +0000 Subject: [PATCH] Record each chat turn's workspace changes for every harness Snapshot the chat directory with internal/filediff when a turn starts and compare it when the turn ends; store changed turns (newest 100 per chat), push chat_turn_diffs summaries, attach them to chat_messages replies and serve one turn's patches through chat_turn_diff. --- internal/pipeline/chat_turn_diffs.go | 97 +++++++ internal/pipeline/chat_turn_diffs_test.go | 50 ++++ internal/pipeline/store.go | 4 + internal/protocol/chat_turn_diff.go | 56 +++++ internal/protocol/message.go | 9 +- internal/server/chat.go | 3 + internal/server/chat_commands.go | 3 + internal/server/chat_hub.go | 9 + internal/server/chat_room.go | 6 + internal/server/chat_turn_diff.go | 294 ++++++++++++++++++++++ internal/server/chat_turn_diff_test.go | 268 ++++++++++++++++++++ internal/server/websocket.go | 1 + 12 files changed, 798 insertions(+), 2 deletions(-) create mode 100644 internal/pipeline/chat_turn_diffs.go create mode 100644 internal/pipeline/chat_turn_diffs_test.go create mode 100644 internal/protocol/chat_turn_diff.go create mode 100644 internal/server/chat_turn_diff.go create mode 100644 internal/server/chat_turn_diff_test.go diff --git a/internal/pipeline/chat_turn_diffs.go b/internal/pipeline/chat_turn_diffs.go new file mode 100644 index 00000000..6545eb87 --- /dev/null +++ b/internal/pipeline/chat_turn_diffs.go @@ -0,0 +1,97 @@ +package pipeline + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// initChatTurnDiffs creates the additive per-turn change table. One row is a +// chat turn that changed files: its chip summary and the bounded filediff +// capture (at most filediff's retained patch budget). A session keeps its +// newest protocol.ChatTurnDiffLimit rows; older ones are pruned on insert. +func (s *Store) initChatTurnDiffs() error { + _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_turn_diffs ( +id INTEGER PRIMARY KEY AUTOINCREMENT, +session_id TEXT NOT NULL, +turn_id TEXT NOT NULL, +summary_json TEXT NOT NULL, +changes_json TEXT NOT NULL, +UNIQUE(session_id, turn_id)); +CREATE INDEX IF NOT EXISTS chat_turn_diffs_session ON chat_turn_diffs(session_id, id);`) + return err +} + +// RecordChatTurnDiff stores one turn's summary and changes and prunes the +// session to its newest protocol.ChatTurnDiffLimit turns. +func (s *Store) RecordChatTurnDiff(ctx context.Context, sessionID string, summary protocol.ChatTurnDiffSummary, changes protocol.StageFileChanges) error { + summaryJSON, err := json.Marshal(summary) + if err != nil { + return err + } + changesJSON, err := json.Marshal(changes) + if err != nil { + return err + } + tx, err := s.db.BeginTx(ctx, nil) + if err != nil { + return err + } + defer tx.Rollback() + if _, err := tx.ExecContext(ctx, `INSERT OR REPLACE INTO chat_turn_diffs(session_id,turn_id,summary_json,changes_json) VALUES(?,?,?,?)`, + sessionID, summary.TurnID, string(summaryJSON), string(changesJSON)); err != nil { + return err + } + if _, err := tx.ExecContext(ctx, `DELETE FROM chat_turn_diffs WHERE session_id = ? AND id NOT IN ( +SELECT id FROM chat_turn_diffs WHERE session_id = ? ORDER BY id DESC LIMIT ?)`, sessionID, sessionID, protocol.ChatTurnDiffLimit); err != nil { + return err + } + return tx.Commit() +} + +// ChatTurnDiffs returns the session's recorded summaries, oldest first. +func (s *Store) ChatTurnDiffs(ctx context.Context, sessionID string) ([]protocol.ChatTurnDiffSummary, error) { + rows, err := s.db.QueryContext(ctx, `SELECT summary_json FROM chat_turn_diffs WHERE session_id = ? ORDER BY id`, sessionID) + if err != nil { + return nil, err + } + defer rows.Close() + summaries := []protocol.ChatTurnDiffSummary{} + for rows.Next() { + var raw string + if err := rows.Scan(&raw); err != nil { + return nil, err + } + var summary protocol.ChatTurnDiffSummary + if err := json.Unmarshal([]byte(raw), &summary); err != nil { + continue // an unreadable row is not reported as a turn + } + summaries = append(summaries, summary) + } + return summaries, rows.Err() +} + +// ChatTurnDiff returns one turn's summary and changes; found is false when the +// session holds no such turn (never recorded, or pruned). +func (s *Store) ChatTurnDiff(ctx context.Context, sessionID, turnID string) (protocol.ChatTurnDiffSummary, protocol.StageFileChanges, bool, error) { + var summaryJSON, changesJSON string + var summary protocol.ChatTurnDiffSummary + var changes protocol.StageFileChanges + err := s.db.QueryRowContext(ctx, `SELECT summary_json, changes_json FROM chat_turn_diffs WHERE session_id = ? AND turn_id = ?`, sessionID, turnID).Scan(&summaryJSON, &changesJSON) + if errors.Is(err, sql.ErrNoRows) { + return summary, changes, false, nil + } + if err != nil { + return summary, changes, false, err + } + if err := json.Unmarshal([]byte(summaryJSON), &summary); err != nil { + return summary, changes, false, err + } + if err := json.Unmarshal([]byte(changesJSON), &changes); err != nil { + return summary, changes, false, err + } + return summary, changes, true, nil +} diff --git a/internal/pipeline/chat_turn_diffs_test.go b/internal/pipeline/chat_turn_diffs_test.go new file mode 100644 index 00000000..237de9ce --- /dev/null +++ b/internal/pipeline/chat_turn_diffs_test.go @@ -0,0 +1,50 @@ +package pipeline + +import ( + "context" + "fmt" + "path/filepath" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func TestChatTurnDiffsSurviveReopenAndPrunePerSession(t *testing.T) { + path := filepath.Join(t.TempDir(), "diffs.db") + s, err := Open(path) + if err != nil { + t.Fatal(err) + } + ctx := context.Background() + changes := protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a.go", Kind: "modified", Patch: "@@ -1 +1 @@\n-a\n+b\n"}}} + for i := 0; i < protocol.ChatTurnDiffLimit+5; i++ { + summary := protocol.ChatTurnDiffSummary{TurnID: fmt.Sprintf("turn_%03d", i), Files: 1, Additions: 1, Deletions: 1, Status: "complete"} + if err := s.RecordChatTurnDiff(ctx, "ses_a", summary, changes); err != nil { + t.Fatal(err) + } + } + if err := s.RecordChatTurnDiff(ctx, "ses_b", protocol.ChatTurnDiffSummary{TurnID: "turn_b", Files: 1}, changes); err != nil { + t.Fatal(err) + } + s.Close() + + s, err = Open(path) + if err != nil { + t.Fatal(err) + } + defer s.Close() + got, err := s.ChatTurnDiffs(ctx, "ses_a") + if err != nil || len(got) != protocol.ChatTurnDiffLimit || got[0].TurnID != "turn_005" || got[len(got)-1].TurnID != fmt.Sprintf("turn_%03d", protocol.ChatTurnDiffLimit+4) { + t.Fatalf("ses_a = %d rows (first %+v) err %v", len(got), got[0], err) + } + if other, _ := s.ChatTurnDiffs(ctx, "ses_b"); len(other) != 1 { + t.Fatalf("pruning one session touched another: %+v", other) + } + summary, stored, found, err := s.ChatTurnDiff(ctx, "ses_a", "turn_050") + if err != nil || !found || summary.Additions != 1 || len(stored.Files) != 1 || stored.Files[0].Patch != changes.Files[0].Patch { + t.Fatalf("turn_050 = %+v %+v found=%v err=%v", summary, stored, found, err) + } + if _, _, found, _ := s.ChatTurnDiff(ctx, "ses_a", "turn_000"); found { + t.Fatal("a pruned turn is still readable") + } +} diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index 9a78b245..8c1e3154 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -101,6 +101,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU db.Close() return nil, err } + if err := s.initChatTurnDiffs(); err != nil { + db.Close() + return nil, err + } if err := s.initAppNotifications(); err != nil { db.Close() return nil, err diff --git a/internal/protocol/chat_turn_diff.go b/internal/protocol/chat_turn_diff.go new file mode 100644 index 00000000..c4ac914e --- /dev/null +++ b/internal/protocol/chat_turn_diff.go @@ -0,0 +1,56 @@ +package protocol + +// Per-turn workspace changes of a chat. At each chat turn the server captures +// the session directory before the turn and compares it afterwards (the same +// bounded internal/filediff capture a pipeline stage visit records). A turn +// that changed files gets one ChatTurnDiffSummary: the transcript shows it as a +// compact chip, and chat_turn_diff fetches that turn's per-file patches. The +// evidence is observed workspace change during the turn, not a claim that the +// agent authored it. +const ( + // ChatTurnDiffs is both the push (one new summary for a watched session) + // and the chat_messages companion field: a session's recent summaries. + ChatTurnDiffs = "chat_turn_diffs" + // ChatTurnDiffRequest asks for one turn's recorded file changes. + ChatTurnDiffRequest = "chat_turn_diff" +) + +// ChatTurnDiffLimit bounds the summaries kept (and listed) per session. +const ChatTurnDiffLimit = 100 + +// ChatTurnDiffMessageIDs bounds the assistant message ids one summary names. +const ChatTurnDiffMessageIDs = 16 + +// ChatTurnDiffSummary is the chip's data for one turn that changed files. +// MessageIDs are the assistant message ids the turn streamed (bounded), so a +// client attaches the chip to the transcript row holding any of them; +// StartedAt/EndedAt (epoch ms) bound the turn for a client that cannot match +// an id. Truncated says the capture was partial or a patch was cut or omitted. +type ChatTurnDiffSummary struct { + TurnID string `json:"turn_id"` + MessageIDs []string `json:"message_ids,omitempty"` + StartedAt int64 `json:"started_at"` + EndedAt int64 `json:"ended_at"` + Files int `json:"files"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + Truncated bool `json:"truncated,omitempty"` + Status string `json:"status"` // complete | partial + Diagnostic string `json:"diagnostic,omitempty"` +} + +// ChatTurnDiffList carries a session's turn summaries, oldest first. It names +// the session so a multi-machine client can re-key it like other chat bodies. +type ChatTurnDiffList struct { + SessionID string `json:"session_id"` + TurnDiffs []ChatTurnDiffSummary `json:"turn_diffs"` +} + +// ChatTurnDiff is the chat_turn_diff request (SessionID and TurnID) and its +// reply, which adds the summary and the recorded per-file changes. +type ChatTurnDiff struct { + SessionID string `json:"session_id"` + TurnID string `json:"turn_id"` + Summary *ChatTurnDiffSummary `json:"summary,omitempty"` + Changes *StageFileChanges `json:"changes,omitempty"` +} diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 3b4d0e37..8c4125ad 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -252,8 +252,13 @@ type Message struct { // ChatQueue is the session's server-held prompt queue: pushed as a // chat_queue frame when it changes and carried by the chat_subscribe and // chat_unqueue replies. - ChatQueue *ChatQueueFrame `json:"chat_queue,omitempty"` - ChatUnqueue *ChatUnqueueRequest `json:"chat_unqueue,omitempty"` + ChatQueue *ChatQueueFrame `json:"chat_queue,omitempty"` + // ChatTurnDiffList is the chat_turn_diffs push and the session's recent + // turn summaries on a chat_messages reply; ChatTurnDiff is the + // chat_turn_diff request and reply (see chat_turn_diff.go). + ChatTurnDiffList *ChatTurnDiffList `json:"chat_turn_diffs,omitempty"` + ChatTurnDiff *ChatTurnDiff `json:"chat_turn_diff,omitempty"` + ChatUnqueue *ChatUnqueueRequest `json:"chat_unqueue,omitempty"` // ChatDirectories carries the operator's directory choices on a // chat_directories response (home first, then recent operator-session // directories, deduplicated and bounded). Additive/omitempty so older peers diff --git a/internal/server/chat.go b/internal/server/chat.go index dc3e0eb9..1aae69b0 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -261,6 +261,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker case protocol.ChatAutomations: handleChatAutomations(ctx, request, broker, registry, write) return true + case protocol.ChatTurnDiffRequest: + handleChatTurnDiff(ctx, request, hub, write) + return true } return false } diff --git a/internal/server/chat_commands.go b/internal/server/chat_commands.go index 18e43f7a..b42572c6 100644 --- a/internal/server/chat_commands.go +++ b/internal/server/chat_commands.go @@ -28,6 +28,9 @@ func handleChatMessages(ctx context.Context, request protocol.Message, broker ch page.Messages = hub.reconcileHistory(query.SessionID, page.Messages) response.ChatMessages = page.Messages // legacy peers keep working. response.ChatHistory = &page + // The session's recorded per-turn changes ride with its history, so the + // transcript's change chips survive a reload and a server restart. + response.ChatTurnDiffList = hub.turnDiffList(ctx, query.SessionID) _ = write(response) } diff --git a/internal/server/chat_hub.go b/internal/server/chat_hub.go index 71686418..9e5af1aa 100644 --- a/internal/server/chat_hub.go +++ b/internal/server/chat_hub.go @@ -74,6 +74,15 @@ type chatHub struct { // and its sequence source (chat_activity.go). activity map[string]protocol.ChatActivity activitySeq int64 + // turnDiffStore records per-turn workspace changes (chat_turn_diff.go); + // nil captures nothing. turnCaptures holds each session's pending + // baseline and finishingTurns the turn whose end snapshot is still being + // taken. turnDiffDirectory overrides the registry's directory in tests. + turnDiffStore chatTurnDiffStore + turnCaptures map[string]*chatTurnCapture + finishingTurns map[string]*chatTurnCapture + turnDiffWG sync.WaitGroup + turnDiffDirectory func(context.Context, string) string } // chatPart is the retained form of one part. Text and reasoning deltas append to diff --git a/internal/server/chat_room.go b/internal/server/chat_room.go index dbe7be7a..1d3a46ed 100644 --- a/internal/server/chat_room.go +++ b/internal/server/chat_room.go @@ -346,6 +346,8 @@ func (h *chatHub) releaseRoomLocked(room *chatRoom) { h.rememberLiveLocked(room.sessionID) h.clearCompactionLocked(room) delete(h.rooms, room.sessionID) + // A turn whose end this room can no longer see keeps no stale baseline. + h.dropTurnDiffLocked(room.sessionID) room.cancel() // An unobserved session reports no turn and no background tasks. h.noteActivityLocked(room.sessionID) @@ -458,6 +460,7 @@ func (h *chatHub) emitRoom(source *chatRoom, frame protocol.ChatEventFrame) { priorStep = h.turnLocked(frame.SessionID).step } h.applyLocked(frame) + h.noteTurnDiffLocked(room, frame) h.noteFormAttentionLocked(frame) h.noteReplyAttentionLocked(priorStep, frame) buffered := false @@ -834,6 +837,8 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{ h.endRoomTurn(room) return } + // The turn's baseline is read before its prompt can change anything. + h.startRoomTurnDiff(room.sessionID) if first { if err := h.ensureObserving(room, broker); err != nil { h.failPrompt(room, p, err) @@ -885,6 +890,7 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{ // comes. func (h *chatHub) endRoomTurn(room *chatRoom) { h.mu.Lock() + h.dropTurnDiffLocked(room.sessionID) h.finishTurnLocked(room) h.mu.Unlock() } diff --git a/internal/server/chat_turn_diff.go b/internal/server/chat_turn_diff.go new file mode 100644 index 00000000..dcc8b96e --- /dev/null +++ b/internal/server/chat_turn_diff.go @@ -0,0 +1,294 @@ +package server + +import ( + "context" + "crypto/rand" + "encoding/hex" + "log" + "strconv" + "strings" + "time" + + "git.a-wan.net/alan/pipelines/internal/filediff" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// Per-turn chat diffs (protocol/chat_turn_diff.go). The hub snapshots the +// chat's directory when a turn starts and compares it when the turn ends, with +// the same bounded internal/filediff capture a pipeline stage visit records, +// so it works for every harness: it only reads the room's step frames. A turn +// the room starts (runTurn) is captured before its prompt is posted; a turn +// the stream announces (a CLI-originated one) is captured when its started +// frame arrives. A turn that changed files is stored and pushed to the +// session's watchers as chat_turn_diffs. + +// chatTurnDiffCaptureTimeout bounds one snapshot; a slower capture is partial. +const chatTurnDiffCaptureTimeout = 20 * time.Second + +type chatTurnDiffStore interface { + RecordChatTurnDiff(ctx context.Context, sessionID string, summary protocol.ChatTurnDiffSummary, changes protocol.StageFileChanges) error + ChatTurnDiffs(ctx context.Context, sessionID string) ([]protocol.ChatTurnDiffSummary, error) + ChatTurnDiff(ctx context.Context, sessionID, turnID string) (protocol.ChatTurnDiffSummary, protocol.StageFileChanges, bool, error) +} + +// chatTurnCapture is one turn's pending baseline. ready is closed once +// baseline is set (nil when the directory could not be captured); done is +// closed once the turn's end snapshot was taken, so the next turn's baseline +// never reads files the next prompt may already be changing. +type chatTurnCapture struct { + id string + started time.Time + baseline *filediff.Snapshot + dir string + ready chan struct{} + done chan struct{} + messages []string +} + +// turnDiffDirectoryFor returns the chat's recorded directory, or "" when the +// session is not an operator chat (or turn diffs are not configured). +func (h *chatHub) turnDiffDirectoryFor(ctx context.Context, sessionID string) string { + if h.turnDiffDirectory != nil { + return h.turnDiffDirectory(ctx, sessionID) + } + if h.registry == nil { + return "" + } + session, found, err := h.registry.OperatorSession(ctx, sessionID) + if err != nil || !found { + return "" + } + return session.Directory +} + +// beginTurnDiff starts a turn's capture and returns it; the caller runs +// capture (synchronously before a prompt, or on its own goroutine). Callers +// hold h.mu. It returns nil when turn diffs are off. +func (h *chatHub) beginTurnDiffLocked(sessionID string) *chatTurnCapture { + if h.turnDiffStore == nil || sessionID == "" { + return nil + } + if h.turnCaptures == nil { + h.turnCaptures = map[string]*chatTurnCapture{} + } + var b [6]byte + _, _ = rand.Read(b[:]) + capture := &chatTurnCapture{ + id: "turn_" + strconv.FormatInt(h.clock().UnixMilli(), 36) + hex.EncodeToString(b[:]), + started: h.clock(), + ready: make(chan struct{}), + done: make(chan struct{}), + } + h.turnCaptures[sessionID] = capture + return capture +} + +// captureBaseline waits for the previous turn's end snapshot, then snapshots +// the chat's directory and closes ready. +func (h *chatHub) captureBaseline(sessionID string, capture *chatTurnCapture, previous *chatTurnCapture) { + defer close(capture.ready) + ctx, cancel := context.WithTimeout(h.baseCtx, chatTurnDiffCaptureTimeout) + defer cancel() + if previous != nil { + select { + case <-previous.done: + case <-ctx.Done(): + return + } + } + dir := h.turnDiffDirectoryFor(ctx, sessionID) + if strings.TrimSpace(dir) == "" { + return + } + capture.dir = dir + capture.baseline = filediff.Capture(ctx, dir) +} + +// startRoomTurnDiff captures the baseline of a turn the room is about to +// prompt. It blocks until the snapshot is taken, so the prompt cannot change +// a file before it is read. +func (h *chatHub) startRoomTurnDiff(sessionID string) { + h.mu.Lock() + previous := h.finishingTurns[sessionID] + capture := h.beginTurnDiffLocked(sessionID) + h.mu.Unlock() + if capture != nil { + h.captureBaseline(sessionID, capture, previous) + } +} + +// dropTurnDiffLocked forgets a pending capture whose turn will not be seen to +// end (a refused prompt, a released room). Callers hold h.mu. +func (h *chatHub) dropTurnDiffLocked(sessionID string) { + delete(h.turnCaptures, sessionID) +} + +// noteTurnDiffLocked follows the turn lifecycle in the room's frames. Callers +// hold h.mu; it only updates maps and starts goroutines. +func (h *chatHub) noteTurnDiffLocked(room *chatRoom, frame protocol.ChatEventFrame) { + if h.turnDiffStore == nil || frame.SessionID == "" { + return + } + capture := h.turnCaptures[frame.SessionID] + switch frame.Kind { + case protocol.ChatEventText, protocol.ChatEventReasoning, protocol.ChatEventTool: + if capture != nil && frame.MessageID != "" && len(capture.messages) < protocol.ChatTurnDiffMessageIDs { + for _, id := range capture.messages { + if id == frame.MessageID { + return + } + } + capture.messages = append(capture.messages, frame.MessageID) + } + case protocol.ChatEventStep: + step := frame.Step + if step == nil || step.Intermediate { + return + } + switch step.Status { + case protocol.ChatStepStarted: + if capture != nil || room == nil { + return + } + previous := h.finishingTurns[frame.SessionID] + if next := h.beginTurnDiffLocked(frame.SessionID); next != nil { + go h.captureBaseline(frame.SessionID, next, previous) + } + case protocol.ChatStepEnded: + if capture == nil { + return + } + delete(h.turnCaptures, frame.SessionID) + if h.finishingTurns == nil { + h.finishingTurns = map[string]*chatTurnCapture{} + } + h.finishingTurns[frame.SessionID] = capture + h.turnDiffWG.Add(1) + go h.finishTurnDiff(frame.SessionID, capture, h.clock()) + } + } +} + +// finishTurnDiff takes the turn's end snapshot, and when files changed stores +// the turn and pushes its summary to the session's watchers. +func (h *chatHub) finishTurnDiff(sessionID string, capture *chatTurnCapture, ended time.Time) { + defer h.turnDiffWG.Done() + ctx, cancel := context.WithTimeout(h.baseCtx, chatTurnDiffCaptureTimeout) + defer cancel() + var after *filediff.Snapshot + select { + case <-capture.ready: + if capture.baseline != nil { + after = filediff.Capture(ctx, capture.dir, capture.baseline) + } + case <-ctx.Done(): + } + close(capture.done) + h.mu.Lock() + if h.finishingTurns[sessionID] == capture { + delete(h.finishingTurns, sessionID) + } + h.mu.Unlock() + if after == nil { + return + } + changes := filediff.Compare(capture.baseline, after) + summary, ok := chatTurnDiffSummary(capture, ended, changes) + if !ok { + return + } + if err := h.turnDiffStore.RecordChatTurnDiff(ctx, sessionID, summary, *changes); err != nil { + log.Printf("record chat turn diff %s: %v", sessionID, err) + return + } + h.fanout(sessionID, protocol.Message{ + Type: protocol.ChatTurnDiffs, + ChatTurnDiffList: &protocol.ChatTurnDiffList{SessionID: sessionID, TurnDiffs: []protocol.ChatTurnDiffSummary{summary}}, + }, "") +} + +// chatTurnDiffSummary derives the chip's counts from a comparison; ok is +// false when no file changed (an unavailable capture has no files either). +func chatTurnDiffSummary(capture *chatTurnCapture, ended time.Time, changes *protocol.StageFileChanges) (protocol.ChatTurnDiffSummary, bool) { + if changes == nil || len(changes.Files) == 0 && changes.Omitted == 0 { + return protocol.ChatTurnDiffSummary{}, false + } + summary := protocol.ChatTurnDiffSummary{ + TurnID: capture.id, + MessageIDs: append([]string(nil), capture.messages...), + StartedAt: capture.started.UnixMilli(), + EndedAt: ended.UnixMilli(), + Files: len(changes.Files) + changes.Omitted, + Status: changes.Status, + Diagnostic: changes.Diagnostic, + Truncated: changes.Status != "complete" || changes.Omitted > 0, + } + for _, file := range changes.Files { + if file.Truncated { + summary.Truncated = true + } + added, removed := patchLineCounts(file.Patch) + summary.Additions += added + summary.Deletions += removed + } + return summary, true +} + +// patchLineCounts counts a unified patch's added and removed lines; the file +// header lines before the first hunk are not counted. +func patchLineCounts(patch string) (added, removed int) { + hunk := false + for _, line := range strings.Split(patch, "\n") { + switch { + case strings.HasPrefix(line, "@@"): + hunk = true + case !hunk: + case strings.HasPrefix(line, "+"): + added++ + case strings.HasPrefix(line, "-"): + removed++ + } + } + return added, removed +} + +// turnDiffList returns the session's recorded summaries for a chat_messages +// reply, or nil when there are none (or turn diffs are off). +func (h *chatHub) turnDiffList(ctx context.Context, sessionID string) *protocol.ChatTurnDiffList { + if h == nil || h.turnDiffStore == nil { + return nil + } + summaries, err := h.turnDiffStore.ChatTurnDiffs(ctx, sessionID) + if err != nil || len(summaries) == 0 { + return nil + } + return &protocol.ChatTurnDiffList{SessionID: sessionID, TurnDiffs: summaries} +} + +// handleChatTurnDiff answers chat_turn_diff with one turn's recorded changes. +func handleChatTurnDiff(ctx context.Context, request protocol.Message, hub *chatHub, write func(protocol.Message) error) { + query := request.ChatTurnDiff + if query == nil || !validChatSessionID(query.SessionID) || strings.TrimSpace(query.TurnID) == "" { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id and turn id are required"}) + return + } + if hub == nil || hub.turnDiffStore == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "turn changes are not recorded on this server"}) + return + } + summary, changes, found, err := hub.turnDiffStore.ChatTurnDiff(ctx, query.SessionID, query.TurnID) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + if !found { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "no recorded changes for this turn"}) + return + } + _ = write(protocol.Message{Type: protocol.ChatTurnDiffRequest, ID: request.ID, ChatTurnDiff: &protocol.ChatTurnDiff{ + SessionID: query.SessionID, + TurnID: query.TurnID, + Summary: &summary, + Changes: &changes, + }}) +} diff --git a/internal/server/chat_turn_diff_test.go b/internal/server/chat_turn_diff_test.go new file mode 100644 index 00000000..ae4b7b3c --- /dev/null +++ b/internal/server/chat_turn_diff_test.go @@ -0,0 +1,268 @@ +package server + +import ( + "context" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func turnDiffHub(t *testing.T, dir string) *chatHub { + t.Helper() + h := newChatHub() + h.turnDiffStore = newOperatorStore(t) + h.turnDiffDirectory = func(context.Context, string) string { return dir } + return h +} + +func gitRepo(t *testing.T, files map[string]string) string { + t.Helper() + dir := t.TempDir() + for name, body := range files { + writeTurnFile(t, dir, name, body) + } + for _, args := range [][]string{ + {"init", "-q"}, + {"add", "-A"}, + {"-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "base"}, + } { + cmd := exec.Command("git", args...) + cmd.Dir = dir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v %s", args, err, out) + } + } + return dir +} + +func writeTurnFile(t *testing.T, dir, name, body string) { + t.Helper() + if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil { + t.Fatal(err) + } +} + +func endDiffTurn(h *chatHub, sid string) { + h.emit(protocol.ChatEventFrame{SessionID: sid, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}}) + h.turnDiffWG.Wait() +} + +func recordedTurns(t *testing.T, h *chatHub, sid string) []protocol.ChatTurnDiffSummary { + t.Helper() + got, err := h.turnDiffStore.ChatTurnDiffs(context.Background(), sid) + if err != nil { + t.Fatal(err) + } + return got +} + +func TestChatTurnDiffRecordsGitWorkspaceChanges(t *testing.T) { + dir := gitRepo(t, map[string]string{"a.txt": "one\ntwo\nthree\n"}) + h := turnDiffHub(t, dir) + h.startRoomTurnDiff("ses_1") + writeTurnFile(t, dir, "a.txt", "one\n2\nthree\nfour\n") + writeTurnFile(t, dir, "new.txt", "fresh\n") + h.emit(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_a", Kind: protocol.ChatEventText, Delta: "done"}) + h.emit(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_a", Kind: protocol.ChatEventText, Delta: "!"}) + endDiffTurn(h, "ses_1") + + turns := recordedTurns(t, h, "ses_1") + if len(turns) != 1 { + t.Fatalf("turns = %+v, want one", turns) + } + got := turns[0] + if got.Files != 2 || got.Additions != 3 || got.Deletions != 1 || got.Truncated || got.Status != "complete" { + t.Fatalf("summary = %+v, want 2 files +3 -1 complete", got) + } + if len(got.MessageIDs) != 1 || got.MessageIDs[0] != "msg_a" || got.TurnID == "" || got.EndedAt < got.StartedAt { + t.Fatalf("summary identity = %+v", got) + } + _, changes, found, err := h.turnDiffStore.ChatTurnDiff(context.Background(), "ses_1", got.TurnID) + if err != nil || !found || len(changes.Files) != 2 || !strings.Contains(changes.Files[0].Patch, "+2") { + t.Fatalf("changes = %+v found=%v err=%v", changes, found, err) + } +} + +func TestChatTurnDiffNonGitDirectoryUsesWorkspaceSnapshot(t *testing.T) { + dir := t.TempDir() + writeTurnFile(t, dir, "notes.md", "a\n") + h := turnDiffHub(t, dir) + h.startRoomTurnDiff("ses_1") + writeTurnFile(t, dir, "notes.md", "a\nb\n") + endDiffTurn(h, "ses_1") + turns := recordedTurns(t, h, "ses_1") + if len(turns) != 1 || turns[0].Files != 1 || turns[0].Additions != 1 || turns[0].Deletions != 0 { + t.Fatalf("turns = %+v", turns) + } +} + +func TestChatTurnDiffRecordsNothingWithoutChanges(t *testing.T) { + dir := gitRepo(t, map[string]string{"a.txt": "same\n"}) + h := turnDiffHub(t, dir) + h.startRoomTurnDiff("ses_1") + endDiffTurn(h, "ses_1") + if turns := recordedTurns(t, h, "ses_1"); len(turns) != 0 { + t.Fatalf("an unchanged turn recorded %+v", turns) + } + // A directory that cannot be read yields no diff, never an empty claim. + h = turnDiffHub(t, filepath.Join(dir, "missing")) + h.startRoomTurnDiff("ses_1") + endDiffTurn(h, "ses_1") + if turns := recordedTurns(t, h, "ses_1"); len(turns) != 0 { + t.Fatalf("an unavailable capture recorded %+v", turns) + } +} + +func TestChatTurnDiffMarksBoundedPatchesTruncated(t *testing.T) { + dir := gitRepo(t, map[string]string{"a.txt": "x\n"}) + h := turnDiffHub(t, dir) + h.startRoomTurnDiff("ses_1") + writeTurnFile(t, dir, "big.txt", strings.Repeat("line\n", 6000)) + endDiffTurn(h, "ses_1") + turns := recordedTurns(t, h, "ses_1") + if len(turns) != 1 || !turns[0].Truncated || turns[0].Files != 1 || turns[0].Status != "partial" { + t.Fatalf("turns = %+v, want one truncated partial turn", turns) + } + _, changes, _, _ := h.turnDiffStore.ChatTurnDiff(context.Background(), "ses_1", turns[0].TurnID) + if file := changes.Files[0]; file.Patch != "" || !file.Truncated || file.NoPatchReason != "lines" { + t.Fatalf("file = %+v, want no patch (lines)", file) + } +} + +// A turn the stream announces (no room prompt) is captured from its started +// frame, for every harness that reports one. +func TestChatTurnDiffCapturesStreamAnnouncedTurn(t *testing.T) { + dir := gitRepo(t, map[string]string{"a.txt": "a\n"}) + h := turnDiffHub(t, dir) + h.mu.Lock() + h.rooms["ses_1"] = newChatRoom(context.Background(), "ses_1") + h.mu.Unlock() + h.emit(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepStarted, TurnID: "native"}}) + h.mu.Lock() + capture := h.turnCaptures["ses_1"] + h.mu.Unlock() + if capture == nil { + t.Fatal("started frame did not begin a capture") + } + <-capture.ready + writeTurnFile(t, dir, "a.txt", "b\n") + h.emit(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "native"}}) + h.turnDiffWG.Wait() + if turns := recordedTurns(t, h, "ses_1"); len(turns) != 1 || turns[0].Additions != 1 || turns[0].Deletions != 1 { + t.Fatalf("turns = %+v", turns) + } +} + +func TestChatTurnDiffPatchLineCounts(t *testing.T) { + patch := "--- \"a/x\"\n+++ \"b/x\"\n@@ -1,2 +1,2 @@\n--- removed dashes\n+++ added pluses\n same\n\\ No newline at end of file\n" + if added, removed := patchLineCounts(patch); added != 1 || removed != 1 { + t.Fatalf("counts = +%d -%d, want +1 -1", added, removed) + } +} + +// editingChat changes the workspace when the prompt arrives and only then +// streams the scripted turn, so the turn's diff is deterministic. +type editingChat struct { + *fakeChat + edit func() + prompted chan struct{} +} + +func (e *editingChat) Prompt(ctx context.Context, sessionID, text string) error { + e.edit() + close(e.prompted) + return e.fakeChat.Prompt(ctx, sessionID, text) +} + +func (e *editingChat) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error { + ready() + select { + case <-e.prompted: + case <-ctx.Done(): + return ctx.Err() + } + return e.fakeChat.Stream(ctx, sessionID, nil, emit) +} + +func TestChatTurnDiffProtocolRoundTrip(t *testing.T) { + dir := gitRepo(t, map[string]string{"main.go": "package main\n"}) + store := newOperatorStore(t) + if err := store.RecordOperatorSession(context.Background(), "ses_1", dir, "chat", protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + broker := &editingChat{fakeChat: scriptedChat(), prompted: make(chan struct{}), edit: func() { + writeTurnFile(t, dir, "main.go", "package main\n\nfunc main() {}\n") + }} + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: broker, OperatorRegistry: store}, store)) + t.Cleanup(func() { srv.Close(); cancel() }) + conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { conn.CloseNow() }) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "p1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "edit"}}); err != nil { + t.Fatal(err) + } + var push protocol.Message + for push.Type != protocol.ChatTurnDiffs { + push = protocol.Message{} + if err := readTestFrame(ctx, conn, &push); err != nil { + t.Fatal(err) + } + } + list := push.ChatTurnDiffList + if list == nil || list.SessionID != "ses_1" || len(list.TurnDiffs) != 1 { + t.Fatalf("push = %+v", push) + } + summary := list.TurnDiffs[0] + if summary.Files != 1 || summary.Additions != 2 || summary.Deletions != 0 || len(summary.MessageIDs) != 1 || summary.MessageIDs[0] != "msg_1" { + t.Fatalf("summary = %+v", summary) + } + + request := protocol.Message{Type: protocol.ChatTurnDiffRequest, ID: "d1", ChatTurnDiff: &protocol.ChatTurnDiff{SessionID: "ses_1", TurnID: summary.TurnID}} + reply := turnDiffReply(t, ctx, conn, request) + if reply.Type != protocol.ChatTurnDiffRequest || reply.ChatTurnDiff == nil || reply.ChatTurnDiff.Changes == nil { + t.Fatalf("reply = %+v", reply) + } + files := reply.ChatTurnDiff.Changes.Files + if len(files) != 1 || files[0].Path != "main.go" || !strings.Contains(files[0].Patch, "+func main() {}") { + t.Fatalf("files = %+v", files) + } + + history := turnDiffReply(t, ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "h1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}) + if history.ChatTurnDiffList == nil || len(history.ChatTurnDiffList.TurnDiffs) != 1 || history.ChatTurnDiffList.TurnDiffs[0].TurnID != summary.TurnID { + t.Fatalf("history turn diffs = %+v", history.ChatTurnDiffList) + } + + missing := turnDiffReply(t, ctx, conn, protocol.Message{Type: protocol.ChatTurnDiffRequest, ID: "d2", ChatTurnDiff: &protocol.ChatTurnDiff{SessionID: "ses_1", TurnID: "turn_gone"}}) + if missing.Type != protocol.Error || !strings.Contains(missing.Error, "no recorded changes") { + t.Fatalf("missing turn reply = %+v", missing) + } +} + +// turnDiffReply writes request and returns the response carrying its id. +func turnDiffReply(t *testing.T, ctx context.Context, conn *websocket.Conn, request protocol.Message) protocol.Message { + t.Helper() + if err := wsjson.Write(ctx, conn, request); err != nil { + t.Fatal(err) + } + for { + var message protocol.Message + if err := readTestFrame(ctx, conn, &message); err != nil { + t.Fatal(err) + } + if message.ID == request.ID { + return message + } + } +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index ce791257..9379b6e9 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -134,6 +134,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin // session; resume delivering any a previous process left queued. hub.queueStore = stores[0] hub.liveStore = stores[0] + hub.turnDiffStore = stores[0] } acks := newAttentionAckHub() var ackStore attentionAckStore