100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
package claude
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/claudehost"
|
|
)
|
|
|
|
func TestLiveTaskEventAfterTrimmedReplayBoundaryWinsOverSnapshot(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
tasks []claudehost.Task
|
|
line string
|
|
want int
|
|
}{
|
|
{
|
|
name: "new task remains running after older idle snapshot",
|
|
tasks: []claudehost.Task{},
|
|
line: `{"type":"assistant","message":{"id":"fresh","content":[{"type":"tool_use","id":"fresh-tool","name":"Bash","input":{"command":"new job","run_in_background":true}}]}}`,
|
|
want: 1,
|
|
},
|
|
{
|
|
name: "completion is not resurrected by older running snapshot",
|
|
tasks: []claudehost.Task{{ToolUseID: "current-tool", TaskID: "native-task", Name: "Bash", Command: "existing job"}},
|
|
line: `{"type":"system","subtype":"task_notification","task_id":"native-task","status":"completed","summary":"Finished"}`,
|
|
want: 0,
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
base, err := os.MkdirTemp("/tmp", "pcb")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(base)
|
|
socket := filepath.Join(base, "host.sock")
|
|
listener, err := net.Listen("unix", socket)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
stop := make(chan struct{})
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
var attach claudehost.Frame
|
|
if json.NewDecoder(conn).Decode(&attach) != nil {
|
|
return
|
|
}
|
|
out := json.NewEncoder(conn)
|
|
_ = out.Encode(claudehost.Frame{Op: claudehost.OpStatus, Status: &claudehost.Status{SessionID: "boundary-session", Base: 100, End: 200, Acked: 100, Init: json.RawMessage(`{"models":[{"value":"fixture"}]}`), Tasks: &tc.tasks}})
|
|
// The old replay region was compacted before delivery. Its
|
|
// first available record is newer than the snapshot boundary.
|
|
_ = out.Encode(claudehost.Frame{Op: claudehost.OpOut, Offset: 300, End: 400, Data: json.RawMessage(tc.line)})
|
|
<-stop
|
|
}()
|
|
chat := newChat(context.Background())
|
|
defer chat.Close()
|
|
p, conn, plan := chat.connectHost(claudehost.Record{SessionID: "boundary-session", Socket: socket})
|
|
if p == nil {
|
|
close(stop)
|
|
t.Fatal("fixture did not attach")
|
|
}
|
|
s := p.session
|
|
s.mu.Lock()
|
|
s.proc = p
|
|
s.mu.Unlock()
|
|
go p.readHost(conn, plan)
|
|
defer func() { p.detach(); close(stop); <-done }()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
_, _ = chat.Shells(ctx, "boundary-session")
|
|
p.link.mu.Lock()
|
|
committed := p.link.committed
|
|
p.link.mu.Unlock()
|
|
// Shells can unblock just before the first live line completes.
|
|
eventually(t, "the first live record to be committed", func() bool {
|
|
p.link.mu.Lock()
|
|
committed = p.link.committed
|
|
p.link.mu.Unlock()
|
|
return committed == 400
|
|
})
|
|
shells, err := chat.Shells(ctx, "boundary-session")
|
|
if err != nil || len(shells) != tc.want {
|
|
t.Fatalf("live record lost to stale snapshot: %+v %v", shells, err)
|
|
}
|
|
})
|
|
}
|
|
}
|