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" "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/protocol" ) // chatManager routes one session to the harness that owns it. OpenCode remains // 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 registry operatorRegistry // 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 hinted map[string]bool // sessions whose directory was given to a chatDirectoryHinter } // 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 { 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() } 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 } // 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 } // chatShutdownDetacher is implemented by a backend whose live sessions can // outlive this server: on shutdown it lets them go instead of stopping them, // and the next server reattaches (Claude Code's detachable hosts). type chatShutdownDetacher interface { Detach() } // chatSessionReleaser is implemented by a backend that keeps a session's agent // running until it is explicitly released; chat_close releases it. type chatSessionReleaser interface { ReleaseSession(ctx context.Context, sessionID string) error } func closeHarness(client chatBroker) { if detacher, ok := client.(chatShutdownDetacher); ok { detacher.Detach() return } if closer, ok := client.(interface{ Close() }); ok { closer.Close() } } // Close lets go of the Codex and Claude Code clients, if any, and stops later // lazy attachment. It runs at server shutdown: a backend whose sessions can // survive it (chatShutdownDetacher) is detached, not stopped. func (m *chatManager) Close() { m.mu.Lock() m.closed = true codexClient, claudeClient := m.codex, m.claude m.mu.Unlock() closeHarness(codexClient) closeHarness(claudeClient) } // ReleaseSession releases a session's agent on its own harness after an // explicit close. A harness without that notion is left as is. func (m *chatManager) ReleaseSession(ctx context.Context, sessionID string) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return nil } if releaser, ok := adapter.(chatSessionReleaser); ok { return releaser.ReleaseSession(ctx, sessionID) } return nil } // Backends lists the harnesses available for a new session. func (m *chatManager) Backends() []string { backends := []string{} if m.opencode != nil { backends = append(backends, "opencode") } if m.harness(codex.Backend) != nil { backends = append(backends, codex.Backend) } if m.harness(claude.Backend) != nil { backends = append(backends, claude.Backend) } sort.Strings(backends) return backends } // AdoptSession finds an existing session on [backend], or on the first // harness that knows it when [backend] is blank (OpenCode, Claude Code, Codex), // and remembers which harness owns it. func (m *chatManager) AdoptSession(ctx context.Context, sessionID, backend string) (protocol.ChatSession, string, bool, error) { candidates := []string{backend} if backend == "" { candidates = []string{"opencode", claude.Backend, codex.Backend} } var firstErr error for _, name := range candidates { adapter := m.adapter(name) if adapter == nil { if backend != "" { return protocol.ChatSession{}, "", false, errors.New("chat backend " + backend + " is not available") } continue } session, found, err := adapter.Session(ctx, sessionID) if err != nil { if firstErr == nil { firstErr = err } continue } if !found { continue } if name == "" { name = "opencode" } session.Backend = name m.mu.Lock() m.cache[sessionID] = name m.mu.Unlock() return session, name, true, nil } return protocol.ChatSession{}, "", false, firstErr } func (m *chatManager) adapter(backend string) chatBroker { switch backend { case "", "opencode": return m.opencode case codex.Backend, claude.Backend: return m.harness(backend) default: return nil } } func (m *chatManager) backendFor(ctx context.Context, sessionID string) string { m.mu.Lock() backend := m.cache[sessionID] m.mu.Unlock() if backend != "" { return backend } if m.registry != nil { if row, found, err := m.registry.OperatorSession(ctx, sessionID); err == nil && found && row.Backend != "" { m.mu.Lock() m.cache[sessionID] = row.Backend m.mu.Unlock() return row.Backend } } return "opencode" } func (m *chatManager) brokerFor(ctx context.Context, sessionID string) (chatBroker, error) { backend := m.backendFor(ctx, sessionID) adapter := m.adapter(backend) if adapter == nil { return nil, errors.New("chat backend " + backend + " is not available") } hinter, hintsDirectory := adapter.(chatDirectoryHinter) titler, takesTitle := adapter.(chatTitleSetter) if (hintsDirectory || takesTitle) && m.registry != nil { m.mu.Lock() done := m.hinted[sessionID] m.mu.Unlock() if !done { if row, found, err := m.registry.OperatorSession(ctx, sessionID); err == nil && found { if hintsDirectory { hinter.HintDirectory(ctx, sessionID, row.Directory) } // The registry holds the operator's title, renames included, so // a session rebuilt after a restart keeps its name. if takesTitle && row.Title != "" { titler.SetTitle(ctx, sessionID, row.Title) } m.mu.Lock() if m.hinted == nil { m.hinted = map[string]bool{} } m.hinted[sessionID] = true m.mu.Unlock() } } } return adapter, nil } // Automations routes the optional readback capability without adding it to the // base broker contract. The session's registry-selected provider remains the // ownership boundary; no provider-wide or cwd-wide schedule list is consulted. func (m *chatManager) Automations(ctx context.Context, sessionID string) (protocol.ChatAutomationInfo, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return protocol.ChatAutomationInfo{SessionID: sessionID, Status: protocol.ChatAutomationUnavailable, Reason: err.Error()}, nil } reader, ok := adapter.(chatAutomationReader) if !ok { return protocol.ChatAutomationInfo{SessionID: sessionID, Provider: m.backendFor(ctx, sessionID), Status: protocol.ChatAutomationUnavailable, Reason: "this chat provider has no authoritative scheduled-work read API"}, nil } return reader.Automations(ctx, sessionID) } // chatTitleSetter is a harness that names its own sessions from the operator's // chat title (Claude Code passes it as --name on the next spawn). SetTitle // records the title in memory only and does no I/O. type chatTitleSetter interface { SetTitle(ctx context.Context, sessionID, title string) } // SetTitle hands a renamed chat's title to its harness when the harness keeps // one. The registry stays the source of truth; this only refreshes the name the // harness shows outside the app. func (m *chatManager) SetTitle(ctx context.Context, sessionID, title string) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return } if titler, ok := adapter.(chatTitleSetter); ok { titler.SetTitle(ctx, sessionID, title) } } // chatDirectoryHinter is a harness that needs a session's recorded directory // to find it after a restart (Claude Code keeps a transcript in the // directory's own CLAUDE_CONFIG_DIR). HintDirectory is cheap after the first // call for a session. type chatDirectoryHinter interface { HintDirectory(ctx context.Context, sessionID, directory string) } // CreateBackend creates a session on the requested harness. An empty backend // keeps the OpenCode default. func (m *chatManager) CreateBackend(ctx context.Context, title, directory, backend string) (protocol.ChatSession, error) { if backend == "" { backend = "opencode" } adapter := m.adapter(backend) if adapter == nil { return protocol.ChatSession{}, errors.New("chat backend " + backend + " is not available") } session, err := adapter.Create(ctx, title, directory) if err != nil { return protocol.ChatSession{}, err } session.Backend = backend m.mu.Lock() m.cache[session.ID] = backend m.mu.Unlock() // The created row names its mode like a listed one, so the opened chat // shows the mode control at once rather than after the list re-reads. return m.withEffort(ctx, m.withMode(ctx, session)), nil } func (m *chatManager) Create(ctx context.Context, title, directory string) (protocol.ChatSession, error) { return m.CreateBackend(ctx, title, directory, "opencode") } func (m *chatManager) Prompt(ctx context.Context, sessionID, text string) error { return m.PromptWithMessageID(ctx, sessionID, text, "") } func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text, messageID string) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } if err := m.applyStoredMode(ctx, adapter, sessionID); err != nil { return err } if err := m.applyStoredEffort(ctx, adapter, sessionID); err != nil { return err } if isModelBackend(m.backendFor(ctx, sessionID)) { model, err := m.promptModel(ctx, adapter, sessionID) if err != nil { return err } if model != "" { withModel, ok := adapter.(interface { PromptWithMessageIDModel(context.Context, string, string, string, string) error }) if !ok { return errors.New("this chat adapter cannot apply the saved next-turn model") } return withModel.PromptWithMessageIDModel(ctx, sessionID, text, messageID, model) } } if withID, ok := adapter.(chatPromptWithMessageID); ok { return withID.PromptWithMessageID(ctx, sessionID, text, messageID) } return adapter.Prompt(ctx, sessionID, text) } // isModelBackend reports a harness whose model choice is stored by Pipelines // and applied at the next turn (Codex and Claude Code). func isModelBackend(backend string) bool { return backend == codex.Backend || backend == claude.Backend } func (m *chatManager) promptModel(ctx context.Context, adapter chatBroker, sessionID string) (string, error) { provider, model, err := m.chatModelSelection(ctx, sessionID) if err != nil || model == "" { return model, err } session, found, err := adapter.Session(ctx, sessionID) if err != nil { return "", err } if !found || session.ModelProvider != provider { return "", errors.New("saved model provider no longer matches the chat harness; select a model again") } return model, nil } // PromptWithMessageIDFiles forwards a prompt with the private local files the // server wrote for browser attachments. Never silently discard files. func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) error { if len(files) == 0 { return m.PromptWithMessageID(ctx, sessionID, text, messageID) } adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } if err := m.applyStoredMode(ctx, adapter, sessionID); err != nil { return err } if err := m.applyStoredEffort(ctx, adapter, sessionID); err != nil { return err } if isModelBackend(m.backendFor(ctx, sessionID)) { model, err := m.promptModel(ctx, adapter, sessionID) if err != nil { return err } if model != "" { withModelFiles, ok := adapter.(interface { PromptWithMessageIDModelFiles(context.Context, string, string, string, string, []opencode.PromptFile) error }) if !ok { return errors.New("this chat adapter cannot apply the saved next-turn model with attachments") } return withModelFiles.PromptWithMessageIDModelFiles(ctx, sessionID, text, messageID, model, files) } } if withFiles, ok := adapter.(chatPromptWithFiles); ok { return withFiles.PromptWithMessageIDFiles(ctx, sessionID, text, messageID, files) } return errChatAttachmentsUnsupported } // SteerPrompt forwards a prompt into the routed harness's running turn when // that harness accepts mid-turn input (Claude Code, Codex). A harness without // it reports not delivered, so the hub queues the prompt durably instead. func (m *chatManager) SteerPrompt(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) (bool, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return false, err } steerer, ok := adapter.(chatMidTurn) if !ok { return false, nil } if len(files) > 0 { if _, ok := adapter.(chatPromptWithFiles); !ok { return false, errChatAttachmentsUnsupported } } return steerer.SteerPrompt(ctx, sessionID, text, messageID, files) } var errChatAttachmentsUnsupported = errors.New("this chat harness does not support attachments") var errChatFormReadbackUnsupported = errors.New("this chat harness does not support pending question readback") func (m *chatManager) Forms(ctx context.Context, sessionID string) ([]protocol.ChatForm, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return nil, err } if reader, ok := adapter.(chatFormReader); ok { return reader.Forms(ctx, sessionID) } return nil, errChatFormReadbackUnsupported } var errChatInstructionsUnsupported = errors.New("this chat harness does not support session instructions") func (m *chatManager) CheckAttachments(ctx context.Context, sessionID string) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } if _, ok := adapter.(chatPromptWithFiles); !ok { return errChatAttachmentsUnsupported } return nil } // SetInstruction forwards one preloaded behavior instruction to the session's // harness when it supports durable instruction entries. func (m *chatManager) SetInstruction(ctx context.Context, sessionID, key string, value any) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } instr, ok := adapter.(chatInstructioner) if !ok { return errChatInstructionsUnsupported } return instr.SetInstruction(ctx, sessionID, key, value) } // Models lists the models the session's harness offers for switching. func (m *chatManager) Models(ctx context.Context, sessionID string) ([]protocol.ChatModel, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return nil, err } modeler, ok := adapter.(chatModeler) if !ok { return nil, errors.New("this chat harness does not support changing models") } return modeler.Models(ctx, sessionID) } // SwitchModel changes the session's model for subsequent turns. func (m *chatManager) SwitchModel(ctx context.Context, sessionID, provider, model string) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } modeler, ok := adapter.(chatModeler) if !ok { return errors.New("this chat harness does not support changing models") } if isModelBackend(m.backendFor(ctx, sessionID)) { registry, ok := m.registry.(chatModelSelectionRegistry) if !ok { return errors.New("durable chat model selection is not configured") } if err := modeler.SwitchModel(ctx, sessionID, provider, model); err != nil { return err } if err := registry.SetChatModelSelection(ctx, sessionID, provider, model); err != nil { return err } return m.reconcileEffort(ctx, sessionID) } return modeler.SwitchModel(ctx, sessionID, provider, model) } type chatModelSelectionRegistry interface { SetChatModelSelection(context.Context, string, string, string) error ChatModelSelection(context.Context, string) (string, string, error) } func (m *chatManager) ModelSelectionMode(ctx context.Context, id string) string { if isModelBackend(m.backendFor(ctx, id)) { return "next_turn" } return "" } func (m *chatManager) chatModelSelection(ctx context.Context, id string) (string, string, error) { if registry, ok := m.registry.(chatModelSelectionRegistry); ok { return registry.ChatModelSelection(ctx, id) } return "", "", nil } func (m *chatManager) withModelSelection(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) { session = m.withMode(ctx, session) if !isModelBackend(session.Backend) { return m.withEffort(ctx, session), nil } provider, model, err := m.chatModelSelection(ctx, session.ID) if err != nil { return session, err } if model != "" { session.ModelProvider, session.Model = provider, model session.ModelSelection = "next_turn" } return m.withEffort(ctx, session), nil } func (m *chatManager) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } return adapter.Stream(ctx, sessionID, ready, emit) } func (m *chatManager) Interrupt(ctx context.Context, sessionID string) error { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } return adapter.Interrupt(ctx, sessionID) } func (m *chatManager) Messages(ctx context.Context, sessionID string, limit int, cursor string) (protocol.ChatHistoryPage, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return protocol.ChatHistoryPage{}, err } return adapter.Messages(ctx, sessionID, limit, cursor) } func (m *chatManager) Sessions(ctx context.Context) ([]protocol.ChatSession, error) { sessions := []protocol.ChatSession{} seen := map[string]bool{} for _, adapter := range []chatBroker{m.opencode, m.attached(codex.Backend), m.attached(claude.Backend)} { if adapter == nil { continue } list, err := adapter.Sessions(ctx) if err != nil { continue } for _, session := range list { if session.ID == "" || seen[session.ID] { continue } seen[session.ID] = true sessions = append(sessions, session) } } // A backend that cannot enumerate its own store still has registry rows. // Resolve those individually so an older/restarted backend never silently // drops a chat from the list. if m.registry != nil { if rows, err := m.registry.OperatorSessions(ctx); err == nil { for _, row := range rows { if seen[row.ID] || row.Backend == "" || row.Backend == "opencode" { continue } adapter := m.adapter(row.Backend) if adapter == nil { continue } if hinter, ok := adapter.(chatDirectoryHinter); ok { hinter.HintDirectory(ctx, row.ID, row.Directory) } if session, found, err := adapter.Session(ctx, row.ID); err == nil && found { seen[row.ID] = true sessions = append(sessions, session) } } } } for i := range sessions { var err error sessions[i], err = m.withModelSelection(ctx, sessions[i]) if err != nil { return nil, err } } return sessions, nil } func (m *chatManager) Session(ctx context.Context, sessionID string) (protocol.ChatSession, bool, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return protocol.ChatSession{}, false, err } session, found, err := adapter.Session(ctx, sessionID) if err != nil || !found { return session, found, err } session, err = m.withModelSelection(ctx, session) return session, found, err } func (m *chatManager) Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return nil, err } if permissions, ok := adapter.(interface { Permissions(context.Context, string) ([]protocol.ChatPermissionFrame, error) }); ok { return permissions.Permissions(ctx, sessionID) } return nil, nil } func (m *chatManager) Shells(ctx context.Context, sessionID string) ([]protocol.ChatShell, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return nil, err } if shells, ok := adapter.(interface { Shells(context.Context, string) ([]protocol.ChatShell, error) }); ok { return shells.Shells(ctx, sessionID) } return nil, nil } func (m *chatManager) Reply(ctx context.Context, request protocol.ChatReplyRequest) error { adapter, err := m.brokerFor(ctx, request.SessionID) if err != nil { return err } return adapter.Reply(ctx, request) } func (m *chatManager) ActivateSkill(ctx context.Context, sessionID, skill string) error { // Skill bootstrap is an OpenCode chat concept. Other harnesses return from // create without a manager skill and must not fail the create. if m.backendFor(ctx, sessionID) != "opencode" { return nil } adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return err } return adapter.ActivateSkill(ctx, sessionID, skill) } func (m *chatManager) Answer(ctx context.Context, request protocol.ChatAnswerRequest) error { adapter, err := m.brokerFor(ctx, request.SessionID) if err != nil { return err } answerer, ok := adapter.(chatAnswerer) if !ok { return errors.New("this chat backend cannot answer questions") } return answerer.Answer(ctx, request) } func (m *chatManager) ShellOutput(ctx context.Context, query protocol.ChatShellOutputQuery) (protocol.ChatShellOutputPage, error) { adapter, err := m.brokerFor(ctx, query.SessionID) if err != nil { return protocol.ChatShellOutputPage{}, err } reader, ok := adapter.(chatShellOutputReader) if !ok { return protocol.ChatShellOutputPage{}, errors.New("this chat backend has no shell output") } return reader.ShellOutput(ctx, query) } func (m *chatManager) ReleaseShells(sessionID string) { m.mu.Lock() backend := m.cache[sessionID] m.mu.Unlock() adapter := m.adapter(backend) if releaser, ok := adapter.(chatShellReleaser); ok { releaser.ReleaseShells(sessionID) } } // TurnRunning reports the routed harness's own in-flight state. It reads only // the backend cache (filled once the session is observed) and an already // attached client: no registry read and no harness probe. func (m *chatManager) TurnRunning(sessionID string) bool { m.mu.Lock() backend := m.cache[sessionID] m.mu.Unlock() adapter := m.opencode if backend != "" && backend != "opencode" { adapter = m.attached(backend) } reporter, ok := adapter.(chatTurnReporter) return ok && reporter.TurnRunning(sessionID) } // ProbeTurn asks the session's own harness whether a turn is in flight. It is // the server-start probe (chat_unattended.go): a harness that can be asked // (OpenCode, Codex) is asked; otherwise its in-memory state is used, which for // Claude Code (a child of this server) is empty after a restart. func (m *chatManager) ProbeTurn(ctx context.Context, sessionID string) (bool, error) { adapter, err := m.brokerFor(ctx, sessionID) if err != nil { return false, err } if prober, ok := adapter.(chatTurnProber); ok { return prober.ProbeTurn(ctx, sessionID) } reporter, ok := adapter.(chatTurnReporter) return ok && reporter.TurnRunning(sessionID), nil }