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.
This commit is contained in:
2026-11-09 16:08:35 +00:00
parent 57679d9591
commit 0696ab849a
12 changed files with 798 additions and 2 deletions
+97
View File
@@ -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
}
+50
View File
@@ -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")
}
}
+4
View File
@@ -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