0696ab849a
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.
295 lines
9.9 KiB
Go
295 lines
9.9 KiB
Go
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,
|
|
}})
|
|
}
|