Files
pipelines/internal/opencode/chat_shell_reconcile_test.go
T
alan e9d902213e Preserve background shell visibility across readback races
Reconcile authoritative shell snapshots with bounded lifecycle journals so concurrent exits and creates cannot be overwritten, coalesce observation setup, and retain verified ownership across reconnects. Add deterministic reconciliation and cancellation regressions plus stable limited-visibility handling.\n\nChecked packaged templates and authoring skill references: none need updating. The task-owned docs note records the stale shared chat documentation for docs-fold; shared documentation was not edited.
2026-10-18 12:54:36 +01:00

117 lines
4.1 KiB
Go

package opencode
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func TestChatShellReadbackBarrier(t *testing.T) {
for _, known := range []bool{false, true} {
t.Run(fmt.Sprint(known), func(t *testing.T) {
read, apply := make(chan struct{}), make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
close(read)
select {
case <-apply:
fmt.Fprint(w, `{"data":[{"id":"sh_old","status":"running","metadata":{"sessionID":"ses_1"}},{"id":"sh_foreign","status":"running","metadata":{"sessionID":"ses_other"}}]}`)
case <-r.Context().Done():
}
}))
defer srv.Close()
c := NewChat(&Connection{Backend: "v2", URL: srv.URL})
state := c.streamState("ses_1")
if known {
state.markShell("sh_old")
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
result := make(chan []protocol.ChatShell, 1)
errs := make(chan error, 1)
go func() { shells, err := c.Shells(ctx, "ses_1"); result <- shells; errs <- err }()
select {
case <-read:
case <-ctx.Done():
t.Fatal("read did not reach barrier")
}
// Both a previously verified exit and an exit first learned while
// reading must suppress the stale running snapshot.
frame, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_old","status":"exited","exit":0}}`), "ses_1", state)
if err != nil || ok != known || (ok && frame.Shell.Status != protocol.ChatShellExited) {
t.Fatalf("exit = %+v %v %v", frame, ok, err)
}
frame, ok, err = NormalizeChatEvent([]byte(`{"type":"shell.created","data":{"info":{"id":"sh_new","status":"running","command":"sleep 10","metadata":{"sessionID":"ses_1"}}}}`), "ses_1", state)
if err != nil || !ok || frame.Shell.ID != "sh_new" {
t.Fatalf("create = %+v %v %v", frame, ok, err)
}
close(apply)
got := <-result
if err := <-errs; err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].ID != "sh_new" || got[0].Command != "sleep 10" {
t.Fatalf("readback lost events/leaked ownership: %+v", got)
}
if state.knowsShell("sh_old") || state.knowsShell("sh_foreign") || !state.knowsShell("sh_new") {
t.Fatal("adapter identity state regressed")
}
// User-visible subsequent exit remains attributable after reconciliation.
frame, ok, err = NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_new","status":"exited","exit":0}}`), "ses_1", state)
if err != nil || !ok || frame.Shell.ID != "sh_new" {
t.Fatalf("new shell exit lost: %+v %v %v", frame, ok, err)
}
})
}
}
func TestChatShellReadbackCancelWaiter(t *testing.T) {
read := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { close(read); <-r.Context().Done() }))
defer srv.Close()
c := NewChat(&Connection{Backend: "v2", URL: srv.URL})
ctx, cancel := context.WithCancel(context.Background())
result := make(chan error, 2)
go func() { _, err := c.Shells(ctx, "ses_1"); result <- err }()
select {
case <-read:
case <-time.After(time.Second):
t.Fatal("read stuck")
}
go func() { _, err := c.Shells(ctx, "ses_1"); result <- err }()
cancel()
for range 2 {
select {
case err := <-result:
if err == nil {
t.Fatal("canceled read succeeded")
}
case <-time.After(time.Second):
t.Fatal("read or waiter stuck")
}
}
old := c.streamState("ses_1")
c.ReleaseShells("ses_1")
if c.streamState("ses_1") == old {
t.Fatal("reconnect reused released state")
}
}
func TestShellSnapshotBounded(t *testing.T) {
var snapshot ShellSnapshot
for i := 0; i <= chatShellIdentityLimit; i++ {
snapshot.Record(&protocol.ChatShell{ID: fmt.Sprintf("sh_%d", i), Status: protocol.ChatShellExited})
}
if len(snapshot.changes) != chatShellIdentityLimit {
t.Fatalf("unbounded journal: %d", len(snapshot.changes))
}
if _, err := snapshot.Merge([]protocol.ChatShell{{ID: "sh_0", Status: protocol.ChatShellRunning}}); err == nil {
t.Fatal("overflow accepted stale snapshot")
}
}