From 0c34704f068d8c1d5b43810d55f156fb3b395d6b Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 31 Oct 2026 16:52:31 +0000 Subject: [PATCH] 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. --- internal/server/chat_manager.go | 184 ++++++++++++++++++---- internal/server/chat_manager_lazy_test.go | 158 +++++++++++++++++++ 2 files changed, 315 insertions(+), 27 deletions(-) create mode 100644 internal/server/chat_manager_lazy_test.go diff --git a/internal/server/chat_manager.go b/internal/server/chat_manager.go index 6ce59729..ce3f00ea 100644 --- a/internal/server/chat_manager.go +++ b/internal/server/chat_manager.go @@ -3,8 +3,10 @@ package server import ( "context" "errors" + "log" "sort" "sync" + "time" "git.a-wan.net/alan/pipelines/internal/claude" "git.a-wan.net/alan/pipelines/internal/codex" @@ -13,41 +15,171 @@ import ( ) // chatManager routes one session to the harness that owns it. OpenCode remains -// the default; Codex is attached when its app-server control socket exists. +// the default; Codex is attached when its app-server control socket exists and +// Claude Code when its executable exists. Both are probed at startup and, while +// still missing, again on demand (see harness), so a harness installed or +// restarted after the server started becomes available without a restart. // The operator registry is the durable record of the backend, so a restart does // not have to guess from a session id. type chatManager struct { opencode chatBroker - codex chatBroker - claude chatBroker registry operatorRegistry - mu sync.Mutex - cache map[string]string // session id -> backend + // baseCtx owns lazily attached clients; a request context would end with + // the request and take the client with it. + baseCtx context.Context + now func() time.Time + codexProbe *harnessProbe + claudeProbe *harnessProbe + started bool + // attachMu serializes availability probes so a slow Codex handshake never + // holds mu (which guards the session cache) while it runs. + attachMu sync.Mutex + + mu sync.Mutex + codex chatBroker // guarded by mu once the manager is shared + claude chatBroker // guarded by mu once the manager is shared + closed bool + cache map[string]string // session id -> backend +} + +// harnessProbeInterval bounds how often a missing harness is probed again, so +// chat_backends stays cheap while a harness remains unavailable. +const harnessProbeInterval = 10 * time.Second + +// harnessProbe re-checks one optional harness and opens its client. +type harnessProbe struct { + available func() bool + open func(context.Context) (chatBroker, error) + next time.Time // earliest next probe; guarded by chatManager.attachMu +} + +func codexHarnessProbe() *harnessProbe { + return &harnessProbe{available: codex.Available, open: func(ctx context.Context) (chatBroker, error) { + client, err := codex.Open(ctx) + if err != nil { + return nil, err + } + return client, nil + }} +} + +func claudeHarnessProbe() *harnessProbe { + return &harnessProbe{available: claude.Available, open: func(ctx context.Context) (chatBroker, error) { + return claude.New(ctx), nil + }} } func NewChatManager(ctx context.Context, open chatBroker, registry operatorRegistry) *chatManager { - m := &chatManager{opencode: open, registry: registry, cache: map[string]string{}} - if codex.Available() { - client, err := codex.Open(ctx) - if err == nil { - m.codex = client - } + return newChatManager(ctx, open, registry, codexHarnessProbe(), claudeHarnessProbe(), time.Now) +} + +func newChatManager(ctx context.Context, open chatBroker, registry operatorRegistry, codexProbe, claudeProbe *harnessProbe, now func() time.Time) *chatManager { + if ctx == nil { + ctx = context.Background() } - if claude.Available() { - m.claude = claude.New(ctx) + m := &chatManager{ + opencode: open, registry: registry, cache: map[string]string{}, + baseCtx: ctx, now: now, codexProbe: codexProbe, claudeProbe: claudeProbe, } + m.harness(codex.Backend) + m.harness(claude.Backend) + m.attachMu.Lock() + m.started = true + m.attachMu.Unlock() return m } -// Close releases the owned Codex client, if any. +// attached returns the client already attached for an optional harness. +func (m *chatManager) attached(backend string) chatBroker { + m.mu.Lock() + defer m.mu.Unlock() + switch backend { + case codex.Backend: + return m.codex + case claude.Backend: + return m.claude + } + return nil +} + +// harness returns the attached client for Codex or Claude Code, probing and +// attaching it when it is missing and the probe interval has elapsed. A working +// client is never replaced or detached. +func (m *chatManager) harness(backend string) chatBroker { + if client := m.attached(backend); client != nil { + return client + } + probe := m.claudeProbe + if backend == codex.Backend { + probe = m.codexProbe + } + if probe == nil { + return nil + } + m.attachMu.Lock() + defer m.attachMu.Unlock() + if client := m.attached(backend); client != nil { + return client + } + m.mu.Lock() + closed := m.closed + m.mu.Unlock() + if closed { + return nil + } + now := time.Now + if m.now != nil { + now = m.now + } + at := now() + if at.Before(probe.next) { + return nil + } + probe.next = at.Add(harnessProbeInterval) + if !probe.available() { + return nil + } + client, err := probe.open(m.baseCtx) + if err != nil || client == nil { + if m.started { + log.Printf("chat backend %s is installed but could not be opened: %v", backend, err) + } + return nil + } + m.mu.Lock() + if m.closed { + m.mu.Unlock() + closeHarness(client) + return nil + } + if backend == codex.Backend { + m.codex = client + } else { + m.claude = client + } + m.mu.Unlock() + if m.started { + log.Printf("chat backend %s became available after startup", backend) + } + return client +} + +func closeHarness(client chatBroker) { + if closer, ok := client.(interface{ Close() }); ok { + closer.Close() + } +} + +// Close releases the owned Codex and Claude Code clients, if any, and stops +// later lazy attachment. func (m *chatManager) Close() { - if closer, ok := m.codex.(interface{ Close() }); ok { - closer.Close() - } - if closer, ok := m.claude.(interface{ Close() }); ok { - closer.Close() - } + m.mu.Lock() + m.closed = true + codexClient, claudeClient := m.codex, m.claude + m.mu.Unlock() + closeHarness(codexClient) + closeHarness(claudeClient) } // Backends lists the harnesses available for a new session. @@ -56,10 +188,10 @@ func (m *chatManager) Backends() []string { if m.opencode != nil { backends = append(backends, "opencode") } - if m.codex != nil { + if m.harness(codex.Backend) != nil { backends = append(backends, codex.Backend) } - if m.claude != nil { + if m.harness(claude.Backend) != nil { backends = append(backends, claude.Backend) } sort.Strings(backends) @@ -109,10 +241,8 @@ func (m *chatManager) adapter(backend string) chatBroker { switch backend { case "", "opencode": return m.opencode - case codex.Backend: - return m.codex - case claude.Backend: - return m.claude + case codex.Backend, claude.Backend: + return m.harness(backend) default: return nil } @@ -397,7 +527,7 @@ func (m *chatManager) Messages(ctx context.Context, sessionID string, limit int, func (m *chatManager) Sessions(ctx context.Context) ([]protocol.ChatSession, error) { sessions := []protocol.ChatSession{} seen := map[string]bool{} - for _, adapter := range []chatBroker{m.opencode, m.codex, m.claude} { + for _, adapter := range []chatBroker{m.opencode, m.attached(codex.Backend), m.attached(claude.Backend)} { if adapter == nil { continue } diff --git a/internal/server/chat_manager_lazy_test.go b/internal/server/chat_manager_lazy_test.go new file mode 100644 index 00000000..4b2e62c3 --- /dev/null +++ b/internal/server/chat_manager_lazy_test.go @@ -0,0 +1,158 @@ +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") + } +}