Files
pipelines/internal/server/chat_manager_lazy_test.go
alan 0c34704f06 Attach Codex and Claude chat backends lazily after startup
A harness missing at server start (e.g. codex mid-reinstall) is probed
again on chat_backends and on session create/adopt, throttled to once
per 10s while unavailable. Attached clients are never replaced.
2026-10-31 16:52:31 +00:00

159 lines
4.7 KiB
Go

package server
import (
"context"
"slices"
"sync"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/claude"
"git.a-wan.net/alan/pipelines/internal/codex"
)
// fakeHarness is an injectable availability probe and opener.
type fakeHarness struct {
mu sync.Mutex
available bool
probes int
opens int
client *fakeChat
}
func (h *fakeHarness) probe() *harnessProbe {
return &harnessProbe{
available: func() bool {
h.mu.Lock()
defer h.mu.Unlock()
h.probes++
return h.available
},
open: func(context.Context) (chatBroker, error) {
h.mu.Lock()
defer h.mu.Unlock()
h.opens++
return h.client, nil
},
}
}
func (h *fakeHarness) set(available bool) {
h.mu.Lock()
h.available = available
h.mu.Unlock()
}
func (h *fakeHarness) counts() (int, int) {
h.mu.Lock()
defer h.mu.Unlock()
return h.probes, h.opens
}
type fakeClock struct {
mu sync.Mutex
at time.Time
}
func (c *fakeClock) now() time.Time {
c.mu.Lock()
defer c.mu.Unlock()
return c.at
}
func (c *fakeClock) advance(d time.Duration) {
c.mu.Lock()
c.at = c.at.Add(d)
c.mu.Unlock()
}
func TestChatManagerAttachesHarnessThatAppearsAfterStartup(t *testing.T) {
ctx := context.Background()
codexHarness := &fakeHarness{client: scriptedChat()}
claudeHarness := &fakeHarness{available: true, client: scriptedChat()}
clock := &fakeClock{at: time.Unix(1_800_000_000, 0)}
m := newChatManager(ctx, scriptedChat(), nil, codexHarness.probe(), claudeHarness.probe(), clock.now)
if got := m.Backends(); slices.Contains(got, codex.Backend) || !slices.Contains(got, claude.Backend) {
t.Fatalf("backends at start = %v, want claude without codex", got)
}
if _, err := m.CreateBackend(ctx, "t", "/tmp", codex.Backend); err == nil {
t.Fatal("creating a session on an unavailable codex succeeded")
}
if probes, _ := codexHarness.counts(); probes != 1 {
t.Fatalf("codex probed %d times within the throttle, want only the startup probe", probes)
}
// Codex appears, but the throttle still hides it until the interval passes.
codexHarness.set(true)
for range 5 {
if slices.Contains(m.Backends(), codex.Backend) {
t.Fatal("codex attached before the probe interval elapsed")
}
}
if probes, _ := codexHarness.counts(); probes != 1 {
t.Fatalf("codex probed %d times within the throttle, want 1", probes)
}
clock.advance(harnessProbeInterval)
if got := m.Backends(); !slices.Contains(got, codex.Backend) {
t.Fatalf("backends after codex appeared = %v, want codex", got)
}
session, err := m.CreateBackend(ctx, "t", "/tmp", codex.Backend)
if err != nil || session.Backend != codex.Backend {
t.Fatalf("create on lazily attached codex = %+v, %v", session, err)
}
if calls, _, _ := codexHarness.client.createQuery(); calls != 1 {
t.Fatalf("codex client create calls = %d, want 1", calls)
}
// The attached client is reused: no more probes or opens, even when the
// probe would now report it missing.
codexHarness.set(false)
clock.advance(time.Hour)
for range 3 {
if !slices.Contains(m.Backends(), codex.Backend) {
t.Fatal("attached codex client was detached")
}
}
if m.adapter(codex.Backend) != chatBroker(codexHarness.client) {
t.Fatal("adapter did not reuse the attached codex client")
}
if probes, opens := codexHarness.counts(); probes != 2 || opens != 1 {
t.Fatalf("codex probes/opens = %d/%d, want 2/1", probes, opens)
}
if probes, opens := claudeHarness.counts(); probes != 1 || opens != 1 {
t.Fatalf("claude probes/opens = %d/%d, want the single startup attach", probes, opens)
}
}
func TestChatManagerAdoptAttachesHarnessLazily(t *testing.T) {
ctx := context.Background()
client := scriptedChat()
client.sessionFound = true
client.sessionRecord.ID = "thread"
codexHarness := &fakeHarness{client: client}
clock := &fakeClock{at: time.Unix(1_800_000_000, 0)}
m := newChatManager(ctx, nil, nil, codexHarness.probe(), nil, clock.now)
if _, _, _, err := m.AdoptSession(ctx, "thread", codex.Backend); err == nil {
t.Fatal("adopt on unavailable codex succeeded")
}
codexHarness.set(true)
clock.advance(harnessProbeInterval)
if _, backend, found, err := m.AdoptSession(ctx, "thread", codex.Backend); err != nil || !found || backend != codex.Backend {
t.Fatalf("adopt after codex appeared = %q %v %v", backend, found, err)
}
}
func TestChatManagerDoesNotAttachAfterClose(t *testing.T) {
codexHarness := &fakeHarness{client: scriptedChat()}
clock := &fakeClock{at: time.Unix(1_800_000_000, 0)}
m := newChatManager(context.Background(), nil, nil, codexHarness.probe(), nil, clock.now)
m.Close()
codexHarness.set(true)
clock.advance(harnessProbeInterval)
if slices.Contains(m.Backends(), codex.Backend) {
t.Fatal("codex attached after the manager was closed")
}
}