Files
pipelines/internal/claude/hosted_readback_test.go

136 lines
4.6 KiB
Go

package claude
import (
"context"
"encoding/json"
"errors"
"os"
"reflect"
"syscall"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/claudehost"
)
func TestMetadataReadbackRetriesUnavailableStartupHostWithoutPrompt(t *testing.T) {
fixture := hostedSetup(t)
first := hostedChat(t)
session := newCreatedChat(t, first)
frames := subscribe(t, first, session.ID)
if err := first.Prompt(context.Background(), session.ID, "background please"); err != nil {
t.Fatal(err)
}
waitStepEnded(t, frames)
before, err := first.Shells(context.Background(), session.ID)
if err != nil || len(before) != 1 {
t.Fatalf("fixture did not establish background work: %+v %v", before, err)
}
record := onlyRecord(t, fixture)
first.Detach()
if err := syscall.Kill(record.HostPID, syscall.SIGSTOP); err != nil {
t.Fatal(err)
}
defer syscall.Kill(record.HostPID, syscall.SIGCONT)
// New's normal startup attach cannot reach the paused host. Read-only
// recovery must not mistake that missing in-memory session for idle.
after := hostedChat(t)
if s := after.session(session.ID); s != nil {
t.Fatal("paused host unexpectedly attached during startup")
}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
busy, probeErr := after.ProbeTurn(ctx, session.ID)
cancel()
if probeErr == nil {
t.Fatalf("unavailable retained host reported authoritative turn state: %v", busy)
}
ctx, cancel = context.WithTimeout(context.Background(), 50*time.Millisecond)
shells, shellErr := after.Shells(ctx, session.ID)
cancel()
if shellErr == nil {
t.Fatalf("unavailable retained host reported authoritative task state: %+v", shells)
}
if !alive(record.HostPID) || !alive(record.ClaudePID) {
t.Fatal("metadata readback stopped live work")
}
if err := syscall.Kill(record.HostPID, syscall.SIGCONT); err != nil {
t.Fatal(err)
}
eventually(t, "ProbeTurn alone to reattach the retained host", func() bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
busy, err := after.ProbeTurn(ctx, session.ID)
if err != nil || busy {
return false
}
s := after.session(session.ID)
if s == nil {
return false
}
s.mu.Lock()
attached := s.proc != nil
s.mu.Unlock()
return attached
})
shells, err = after.Shells(context.Background(), session.ID)
if err != nil || !reflect.DeepEqual(shells, before) {
t.Fatalf("metadata-only reattachment changed native tasks: before=%+v after=%+v err=%v", before, shells, err)
}
authoritative := onlyRecord(t, fixture)
if record.HostPID != authoritative.HostPID || record.ClaudePID != authoritative.ClaudePID {
t.Fatal("metadata-only recovery replaced the live processes")
}
if starts := readArgs(t, fixture.logPath); len(starts) != 1 {
t.Fatalf("readback spawned/resumed a CLI: %d", len(starts))
}
}
func TestMetadataReadbackReadinessDeadlineIsUnknown(t *testing.T) {
chat := newChat(context.Background())
defer chat.Close()
s := chat.sessionFor("pending-replay", t.TempDir())
p := newProcess(s, "")
s.proc = p
defer func() { s.proc = nil; p.finishDetached() }()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
_, err := chat.ProbeTurn(ctx, s.id)
cancel()
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("replay timeout became authoritative turn state: %v", err)
}
ctx, cancel = context.WithTimeout(context.Background(), 20*time.Millisecond)
_, err = chat.Shells(ctx, s.id)
cancel()
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("replay timeout became authoritative empty task set: %v", err)
}
p.markReady()
if busy, err := chat.ProbeTurn(context.Background(), s.id); err != nil || busy {
t.Fatalf("completed empty replay must be authoritative idle: %v %v", busy, err)
}
}
func TestMetadataReadbackMissingOrDeadHostIsIdle(t *testing.T) {
hostedSetup(t)
chat := newChat(context.Background())
defer chat.Close()
for _, id := range []string{"missing-host", "dead-host"} {
if id == "dead-host" {
paths := claudehost.PathsFor(chat.hostDir, id)
if err := claudehost.EnsureDir(chat.hostDir); err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(claudehost.Record{SessionID: id, Socket: paths.Socket, HostPID: 0, ClaudePID: 0})
if err := os.WriteFile(paths.Record, raw, 0o600); err != nil {
t.Fatal(err)
}
}
if busy, err := chat.ProbeTurn(context.Background(), id); err != nil || busy {
t.Fatalf("%s should be idle without a live owner: %v %v", id, busy, err)
}
if shells, err := chat.Shells(context.Background(), id); err != nil || len(shells) != 0 {
t.Fatalf("%s should have no tasks without a live owner: %+v %v", id, shells, err)
}
}
}