a627b38ef3
OpenCode and Codex chats implement Detach (called by chatManager.Close) instead of a parallel DetachSessions interface. Docs follow the Claude Code host and the systemd KillMode=process unit. The opt-in live restart check copies only the stage provider's login row into its scratch OpenCode database, builds the CLI for skill install and removes its scratch tree.
233 lines
10 KiB
Go
233 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
|
"git.a-wan.net/alan/pipelines/internal/claude"
|
|
"git.a-wan.net/alan/pipelines/internal/discovery"
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/paths"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/server"
|
|
"git.a-wan.net/alan/pipelines/internal/summarise"
|
|
)
|
|
|
|
func main() {
|
|
addr := flag.String("addr", "127.0.0.1:8080", "HTTP/WebSocket listen address")
|
|
allowAuthoring := flag.Bool("allow-authoring", false, "Explicitly enable pipeline definition publication (disabled on execution servers by default)")
|
|
// -opencode-endpoint opts in to brokering chat from an operator-supplied
|
|
// OpenCode service instead of the spawned default. Its default is the
|
|
// environment variable, so both are the same opt-in; the recorded choice (set
|
|
// by a previous run) is used when neither is present.
|
|
endpoint := flag.String("opencode-endpoint", os.Getenv(opencode.EndpointEnvKey), "Broker chat from this OpenCode endpoint instead of spawning one")
|
|
defaultDB, err := paths.Database()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
db := flag.String("db", defaultDB, "SQLite database path (honors XDG_DATA_HOME)")
|
|
mdnsOn := flag.Bool("mdns", envEnabled(os.Getenv("PIPELINES_MDNS")), "Advertise the server on the LAN with mDNS/DNS-SD ("+discovery.ServiceType+"); PIPELINES_MDNS=off disables")
|
|
mdnsPort := flag.Int("mdns-port", envPort(os.Getenv("PIPELINES_MDNS_PORT")), "Port LAN clients dial: the authenticated web proxy fronting this server (default: the -addr port when it is not loopback)")
|
|
flag.Parse()
|
|
if err := os.MkdirAll(filepath.Dir(*db), 0700); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
store, err := pipeline.Open(*db)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
if err := store.AcquireServerLock(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
signals, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
// The server's own context ends after a signal, once the chats left live on
|
|
// their harness have been recorded (see the shutdown goroutine below).
|
|
ctx, cancelServer := context.WithCancel(context.Background())
|
|
defer cancelServer()
|
|
// A restart or stop leaves harness sessions running and the next start
|
|
// reattaches them; PIPELINES_SHUTDOWN_SESSIONS=stop restores stopping them.
|
|
detach, err := shutdownDetaches(os.Getenv(shutdownSessionsEnvKey))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
// A brand-new database starts with the bundled examples; an existing or
|
|
// already-seeded one is never changed, even if its pipelines were deleted.
|
|
if seeded, err := server.SeedExamples(ctx, store); err != nil {
|
|
log.Printf("seed example pipelines: %v", err)
|
|
} else if seeded > 0 {
|
|
log.Printf("added %d example pipelines to the new database", seeded)
|
|
}
|
|
artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts")
|
|
// Each new run gets a bounded one-line title from the configured default
|
|
// harness; the summariser falls back to the prompt's first line, so title
|
|
// generation never blocks or fails run creation.
|
|
options := server.Options{AllowAuthoring: *allowAuthoring, ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv), DetachSessions: detach}
|
|
recorded := ""
|
|
if value, err := store.OpenCodeEndpoint(ctx); err != nil {
|
|
log.Printf("OpenCode endpoint setting unreadable: %v", err)
|
|
} else {
|
|
recorded = value
|
|
}
|
|
reference := opencode.ResolveEndpointRef(*endpoint, os.Getenv(opencode.EndpointEnvKey), recorded)
|
|
// Chat brokers OpenCode over one endpoint. When the operator configured an
|
|
// endpoint (flag, environment or a previously recorded choice) the broker
|
|
// attaches to it: it uses the password-file credential if one was supplied,
|
|
// otherwise it pairs once with the operator's local OpenCode service
|
|
// credential, stores the paired token owner-only beside the pipeline
|
|
// database and reuses it across restarts. An endpoint that cannot be
|
|
// attached is a log line and a status note, and chat then falls back to the
|
|
// server's own OpenCode instance rather than staying refused. Only a
|
|
// URL-bearing, SSE-capable connection enables chat; otherwise chat stays
|
|
// disabled and every chat_* request is answered with a protocol error naming
|
|
// the missing configuration rather than hanging.
|
|
openCodeStatus := protocol.ServiceStatus{Health: "unavailable"}
|
|
if reference != "" {
|
|
explicit, explicitErr := opencode.ReadEndpointCredentialFile(os.Getenv(opencode.EndpointPasswordFileEnvKey))
|
|
if explicitErr != nil {
|
|
log.Printf("OpenCode endpoint password file unreadable: %v", explicitErr)
|
|
}
|
|
// The pair bootstrap (the operator's local service credential) is only
|
|
// needed when no explicit password was supplied; a password-file setup
|
|
// therefore logs nothing here.
|
|
bootstrap := ""
|
|
if explicit == "" {
|
|
setting, _ := opencode.ServiceCredentialPath()
|
|
value, bootstrapErr := opencode.ReadServiceCredential(setting)
|
|
if bootstrapErr != nil {
|
|
log.Printf("OpenCode local service credential unreadable: %v", bootstrapErr)
|
|
}
|
|
bootstrap = value
|
|
}
|
|
credentialPath := filepath.Join(filepath.Dir(*db), "opencode-endpoint.json")
|
|
attach, attachErr := opencode.OpenEndpointAttached(ctx, reference, explicit, bootstrap, credentialPath)
|
|
if attachErr != nil {
|
|
log.Printf("OpenCode endpoint %s could not be attached (%v); falling back to the server's own OpenCode instance", reference, attachErr)
|
|
openCodeStatus = protocol.ServiceStatus{Health: "fallback", URL: reference}
|
|
} else {
|
|
options.Chat = opencode.NewChat(attach.Connection)
|
|
defer attach.Connection.Close()
|
|
openCodeStatus = protocol.ServiceStatus{Health: "endpoint", URL: reference}
|
|
if attach.Note != "" {
|
|
log.Printf("OpenCode endpoint %s: %s", reference, attach.Note)
|
|
}
|
|
if err := store.RecordOpenCodeEndpoint(ctx, reference); err != nil {
|
|
log.Printf("record OpenCode endpoint: %v", err)
|
|
}
|
|
}
|
|
}
|
|
if options.Chat == nil {
|
|
if conn, err := openChat(ctx, detach); err != nil {
|
|
log.Printf("OpenCode chat unavailable: %v", err)
|
|
} else if conn.URL == "" {
|
|
conn.Close()
|
|
log.Printf("OpenCode chat unavailable: the connection exposes no streamable event endpoint")
|
|
} else {
|
|
options.Chat = opencode.NewChat(conn)
|
|
defer conn.Close()
|
|
if openCodeStatus.Health == "unavailable" {
|
|
openCodeStatus = protocol.ServiceStatus{Health: "spawned", URL: conn.URL}
|
|
}
|
|
}
|
|
}
|
|
// If neither the endpoint nor the spawned instance enabled chat, report that
|
|
// honestly rather than claiming the fallback is serving chat.
|
|
if options.Chat == nil && openCodeStatus.Health != "endpoint" {
|
|
openCodeStatus.Health = "unavailable"
|
|
}
|
|
options.OpenCodeStatus = openCodeStatus
|
|
// One manager routes each chat to its owning harness. OpenCode remains the
|
|
// default; Codex is attached when its app-server control socket is present.
|
|
// Claude Code chats run in detachable hosts scoped to this database, so a
|
|
// restart does not end them (internal/claudehost).
|
|
claude.ConfigureHosts(claude.DefaultHostDir(*db))
|
|
chats := server.NewChatManager(ctx, options.Chat, store)
|
|
if len(chats.Backends()) > 0 {
|
|
options.Chat = chats
|
|
// Close detaches a backend whose sessions survive a restart
|
|
// (chatShutdownDetacher) instead of stopping it.
|
|
defer chats.Close()
|
|
}
|
|
srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, options, store), ReadHeaderTimeout: 5 * time.Second}
|
|
// Deferred before WaitRuns so it runs after the runs settle: the owned stage
|
|
// service is stopped only when no stage session is still active on it.
|
|
defer opencode.StopIdleStageService()
|
|
defer server.WaitRuns(srv.Handler)
|
|
go func() {
|
|
select {
|
|
case <-signals.Done():
|
|
case <-ctx.Done():
|
|
}
|
|
if detach {
|
|
// Record the live chats while their rooms still exist.
|
|
server.DetachSessions(srv.Handler)
|
|
log.Printf("shutdown: harness sessions are left running; the next start reattaches them")
|
|
}
|
|
cancelServer()
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
|
log.Printf("shutdown: %v", err)
|
|
}
|
|
}()
|
|
var chatNames []string
|
|
if len(chats.Backends()) > 0 {
|
|
chatNames = chats.Backends()
|
|
}
|
|
if *mdnsOn {
|
|
if stopMDNS := advertise(*addr, *mdnsPort); stopMDNS != nil {
|
|
defer stopMDNS()
|
|
}
|
|
}
|
|
printBanner(startup{Build: buildinfo.Current(), Addr: *addr, Database: *db, Artifacts: artifactsRoot,
|
|
Authoring: *allowAuthoring, OpenCode: openCodeStatus, Chat: chatNames})
|
|
log.Printf("listening on %s (WebSocket endpoint: /connect)", *addr)
|
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// shutdownSessionsEnvKey selects what a stopping server does with harness
|
|
// sessions: "keep" (the default) leaves them running for the next start to
|
|
// reattach; "stop" interrupts runs and closes chat clients as before.
|
|
const shutdownSessionsEnvKey = "PIPELINES_SHUTDOWN_SESSIONS"
|
|
|
|
func shutdownDetaches(value string) (bool, error) {
|
|
switch value {
|
|
case "", "keep":
|
|
return true, nil
|
|
case "stop":
|
|
return false, nil
|
|
}
|
|
return false, errors.New(shutdownSessionsEnvKey + " must be keep or stop")
|
|
}
|
|
|
|
// openChat opens the OpenCode service chat brokers from when no endpoint is
|
|
// attached. When sessions are kept across restarts, a V2 installation uses the
|
|
// detached Pipelines-owned chat service, which outlives this process; otherwise
|
|
// (and for V1) it is a serve child that ends with the server.
|
|
func openChat(ctx context.Context, detach bool) (*opencode.Connection, error) {
|
|
executable := opencode.Executable()
|
|
if detach {
|
|
if backend, err := opencode.Detect(ctx, executable); err == nil && backend == "v2" {
|
|
directory, err := os.Getwd()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return opencode.EnsureOwnedChatService(ctx, executable, directory)
|
|
}
|
|
}
|
|
return opencode.OpenChat(ctx, executable, "")
|
|
}
|