// Command archied is the archie orchestrator daemon: it watches GitHub // for issues labelled for archie, works each one in an isolated // worktree through its routed workflow, and opens pull requests for // human review. package archied import ( "context" "crypto/sha256" "encoding/json" "flag" "fmt" "log/slog" "net" "os" "os/signal" "path/filepath" "strconv" "strings" "syscall" "github.com/moby/moby/client" natsio "github.com/nats-io/nats.go" "github.com/samcharles93/ai-sdk/chat" "github.com/samcharles93/ai-sdk/core" "github.com/samcharles93/ai-sdk/runtime" "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/container" "github.com/samcharles93/archie-core/internal/daemon" "github.com/samcharles93/archie-core/internal/domain/applystatus" "github.com/samcharles93/archie-core/internal/domain/curator" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/forge" "github.com/samcharles93/archie-core/internal/forgerpc" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/logging" "github.com/samcharles93/archie-core/internal/plugin" "github.com/samcharles93/archie-core/internal/secret" "github.com/samcharles93/archie-core/internal/storage" "github.com/samcharles93/archie-core/internal/store" "github.com/samcharles93/archie-core/internal/taskstate" "github.com/samcharles93/archie-core/internal/tools" "github.com/samcharles93/archie-core/internal/tools/mcp" toolprovider "github.com/samcharles93/archie-core/internal/tools/provider" mcptoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/mcp" "github.com/samcharles93/archie-core/internal/worktree" "github.com/samcharles93/archie-core/internal/worktreerpc" ) const ( // defaultChatMaxSteps bounds the model/tool round-trips in one chat // turn when [config.ChatConfig.MaxSteps] is unset. defaultChatMaxSteps = 100 ) // Component versions are injected from their independent release tags. // Components without a release tag remain "dev" and never generate upgrade // notifications. var ( gatewayVersion = "dev" runtimeVersion = "dev" ) // chatGenerateOptions builds one chat turn's request. func chatGenerateOptions( ctx context.Context, messages []chat.Message, registry *tools.Registry, maxSteps int, limits agentexec.ToolLimits, extra []tools.ToolEntry, contextWindow int, ) (core.GenerateOptions, error) { toolOpts := limits.Options() if approval := gateway.ApprovalFromContext(ctx); approval != nil { toolOpts.Approval = approval } // Progressive tool disclosure: compose the base registry with the per-turn // extras into a turn-local registry (extras are identity-bound and cannot // live in the process-wide registry), then let the ContextPressureGate // decide whether to serve the full arsenal directly or fall back to the // bridge tools. The bridge tools are never registered process-wide, so they // stay inside this composed, per-turn registry. composed, err := tools.ComposeForTurn(registry, extra) if err != nil { return core.GenerateOptions{}, err } gate := tools.NewContextPressureGate(contextWindow) gate.Evaluate(composed.All()) toolSet, err := agentexec.BuildToolSetFrom(gate.FilterTools(composed), toolOpts) if err != nil { return core.GenerateOptions{}, err } if maxSteps <= 0 { maxSteps = defaultChatMaxSteps } return core.GenerateOptions{ Messages: messages, Tools: toolSet, MaxSteps: maxSteps, }, nil } // isWithin reports whether target sits inside base. func isWithin(base, target string) bool { absBase, err := filepath.Abs(base) if err != nil { return false } absTarget, err := filepath.Abs(target) if err != nil { return false } rel, err := filepath.Rel(absBase, absTarget) if err != nil { return false } return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) } // toolLimits reads the configured per-turn result limits. func toolLimits(cfg config.Config) agentexec.ToolLimits { return agentexec.ToolLimits{ MaxResultChars: cfg.Tools.Policy.MaxResultChars, SpillDir: cfg.Tools.Policy.SpillDir, } } // taskListOverRead multiplies the requested limit when reading rows that are // filtered by identity afterwards. const taskListOverRead = 5 // chatTaskListerAdapter gives the gateway a read view of one identity's tasks. // gateway deliberately does not import internal/store, so the projection // happens here, as it does for the writer and controller adapters below. type chatTaskListerAdapter struct { tasks func(context.Context, int) ([]workflow.Task, error) } func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) { // Over-read before filtering: Tasks applies its limit across the whole // table, so asking for exactly `limit` would return fewer than that for // this identity whenever another identity's work is more recent. rows, err := a.tasks(ctx, limit*taskListOverRead) if err != nil { return nil, err } out := make([]gateway.ChatTaskSummary, 0, limit) for _, task := range rows { if task.Identity != identity { continue } if len(out) >= limit { break } out = append(out, gateway.ChatTaskSummary{ ID: task.ID, Repo: task.Owner + "/" + task.Repo, Title: task.Title, Status: task.Status, Workflow: task.Workflow, Stage: task.Stage, PRNumber: task.PRNumber, Attempt: task.Attempt, ParkReason: task.ParkReason, UpdatedAt: task.UpdatedAt, }) } return out, nil } // npmCacheServerEnv returns environment variables that make an // npx-launched MCP server reuse a persistent package cache across daemon // restarts, instead of npm re-resolving and re-downloading the package // from the registry every time the daemon starts. Rooted under the // daemon's own work dir so it persists regardless of whether the daemon // itself runs in an ephemeral container. See mcp.NpmCacheEnv for the // command-matching and env var details shared with archie-agent's // per-task equivalent (internal/app/agentworker/mcp_providers.go), which // points at a mounted cache volume instead of a work-dir subdirectory. // // workDir is unconditionally defaulted before registerTools runs // (internal/infrastructure/configuration's applyGeneralDefaults), but // filepath.Join silently accepts an empty string and would then put the // cache in the daemon's current working directory instead of its // persistent data directory -- guard it explicitly rather than trust that // invariant here too. func npmCacheServerEnv(command, workDir string) []string { if workDir == "" { return nil } return mcp.NpmCacheEnv(command, filepath.Join(workDir, "mcp-npm-cache")) } func configuredMCPProvider(server config.MCPServer, workDir string) (toolprovider.Engine, error) { name := strings.TrimSpace(server.Name) if name == "" { return nil, fmt.Errorf("MCP server name is required") } transportType := strings.ToLower(strings.TrimSpace(server.Transport)) if transportType == "" { transportType = "stdio" } switch transportType { case "stdio": command := strings.TrimSpace(server.Command) if command == "" { return nil, fmt.Errorf("MCP stdio server %q requires a command", name) } transport := mcp.NewStdioTransport(mcp.StdioTransportConfig{ Command: command, Args: append([]string(nil), server.Args...), Dir: server.WorkDir, Env: npmCacheServerEnv(command, workDir), }) return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil case "http", "streamablehttp": url := strings.TrimSpace(server.URL) if url == "" { return nil, fmt.Errorf("MCP http server %q requires a url", name) } transport := mcp.NewHTTPTransport(mcp.HTTPTransportConfig{ Endpoint: url, Headers: server.Headers, }) return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil case "sse": sseEndpoint := strings.TrimSpace(server.SSEEndpoint) if sseEndpoint == "" { return nil, fmt.Errorf("MCP sse server %q requires an sse_endpoint", name) } transport := mcp.NewSSETransport(mcp.SSETransportConfig{ SSEEndpoint: sseEndpoint, MessageEndpoint: strings.TrimSpace(server.MessageEndpoint), Headers: server.Headers, }) return mcptoolprovider.New(name, transport, server.ParallelToolCalls), nil default: return nil, fmt.Errorf("MCP transport %q is not supported", transportType) } } // resolveForge builds the forge client for one forge configuration, returning // the resolved token alongside it for the worktree manager. // // A missing or unusable credential disables the forge; it does not stop the // daemon. The forge is one feature among many, and killing the process denies // the operator chat, the gateway and every other subsystem over a capability // they may not use at all -- under a systemd unit with Restart=on-failure that // becomes a crash loop where the error scrolls past unread. A malformed // configuration is a different matter and still fails fast in validation, well // before this point. func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger) (forge.Forge, string) { if configuration.ForgeDisabled(cfg.Type) { return forge.NewNoop(log), "" } token, err := secrets.Resolve(cfg.Token) if err != nil || token == "" { log.Warn("forge disabled: token unavailable", "forge_type", cfg.Type, "engine", cfg.Token.Engine, "key", cfg.Token.Key, "err", err) return forge.NewNoop(log), "" } client, err := forge.New(cfg.Type, token, cfg.Host, log) if err != nil { log.Warn("forge disabled: client construction failed", "forge_type", cfg.Type, "err", err) return forge.NewNoop(log), "" } return client, token } func openProductionTaskStore(ctx context.Context, path string, opts ...store.OpenOption) (storecontract.TaskStore, error) { return store.Open(ctx, path, opts...) } func taskDBPath(configuredPath string) string { return configuredPath + "-tasks.sqlite" } func manualRequeueTask(ctx context.Context, st storecontract.TaskStore, taskID int64) error { task, err := st.TaskByID(ctx, taskID) if err != nil { return err } if task == nil { return fmt.Errorf("task %d not found", taskID) } switch task.Status { case workflow.StatusParked, workflow.StatusWaitingHuman: return st.Requeue(ctx, taskID, task.Status, "") default: return fmt.Errorf("task %d has status %q; only parked or waiting_human tasks can be requeued", taskID, task.Status) } } type runArgs struct { cfgPath string overlayPath string once bool requeue int64 } func parseArgs() (runArgs, bool) { var args runArgs flag.StringVar(&args.cfgPath, "config", configuration.DefaultConfigPath(), "path to a TOML/YAML config file or configuration directory") flag.StringVar(&args.overlayPath, "config-overlay", "", "path to a TOML/YAML overlay file or configuration directory applied on top of -config") flag.BoolVar(&args.once, "once", false, "run a single poll+process cycle and exit (systemd timer / testing)") flag.Int64Var(&args.requeue, "requeue", 0, "requeue a parked/waiting task by id (keeps its workflow), then exit unless -once is also set") showVersion := flag.Bool("version", false, "print the gateway and runtime versions and exit") flag.Parse() // The versions were only reachable through the chat /version command, // which needs a running daemon and a configured channel. An update-check // adapter has to answer "what is installed?" from a shell, so expose the // same two values here. Machine-readable: one "name version" per line. if *showVersion { fmt.Printf("archied %s\narchie-agent %s\n", gatewayVersion, runtimeVersion) return args, true } return args, false } func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence is deliberately flat and sequential args, exit := parseArgs() if exit { return 0 } root, cancelRoot := context.WithCancel(context.Background()) ctx, stop := signal.NotifyContext(root, os.Interrupt, syscall.SIGTERM) defer stop() b := newBootstrap() b.processName = applystatus.Daemon // Arm the shutdown watchdog before any cleanup is registered so its // disarm runs last in the LIFO cleanup chain: it force-exits the process // as a backstop if graceful shutdown hangs past the drain-plus-grace // leash, and stands down once every subsystem has shut down cleanly. b.startShutdownWatchdog(ctx) // The drain monitor cancels this root to trigger a graceful shutdown, which // propagates through ctx exactly as an operator SIGTERM does. b.shutdown = cancelRoot if err := b.loadConfig(ctx, args.cfgPath, args.overlayPath); err != nil { return 1 } defer b.cleanup() // Before any subsystem: the port answers 503 for the whole of boot, so a // watchdog polling a restarted daemon sees "not yet" rather than a // refused connection it cannot tell from a crash. if err := b.startHealth(ctx); err != nil { return 1 } if err := b.openStores(ctx); err != nil { return 1 } // Resolve the State Store surfaces before setupObservability wires the // dashboard's storage surfaces (§10): the shared contract adapter (the // remote *staterpc.Client dialed to [services.state].target; the daemon no // longer owns archie.db in-process, so an empty target is a composition // error, docs/prds/state-store-contract.md §12 step 7) and then, daemon // root only, the workflow-definitions client on this process's step // vocabulary. The daemon resolves step types through that client, so it is // built here -- after the adapter, whose transport it wraps -- and before // buildDaemon captures it. The gateway root shares only the adapter and // resolves no step type, so openDaemonStateSurfaces owns the order and // step_vocabulary_test.go pins the cut. if err := b.openDaemonStateSurfaces(); err != nil { return 1 } if err := b.loadRuntimeConfig(ctx); err != nil { b.log.Error("runtime settings unavailable", "err", err) return 1 } if err := b.startWorkflowExecutionSettings(ctx); err != nil { b.log.Error("workflow execution settings unavailable", "err", err) return 1 } go b.applyStatus.Run(ctx) if exit, err := b.handleRequeue(ctx, args.requeue, args.once); err != nil { return 1 } else if exit { return 0 } b.loadCatalog(ctx, args.cfgPath) b.setupObservability(ctx) if err := b.setupBackends(ctx); err != nil { return 1 } if err := b.setupLLMAndChat(ctx); err != nil { return 1 } if err := b.buildTreesAndIdentities(ctx); err != nil { return 1 } // setupMemoryEngine must run before setupCurators: curator.Registrar // captures b.memEngines at construction time, so built the other way // round every curator would hold a nil engine source. if err := b.setupMemoryEngine(); err != nil { return 1 } if err := b.loadWorkflows(ctx); err != nil { return 1 } if err := b.loadPlugins(); err != nil { return 1 } if err := b.registerNATSRPC(); err != nil { return 1 } b.setupCurators(ctx) if err := b.setupScheduling(); err != nil { return 1 } b.setupGuardrails() if err := b.registerTools(ctx); err != nil { return 1 } b.registerStandaloneTools() b.buildDaemon() b.wireConfigSurfaces(ctx, args.cfgPath, args.overlayPath) if err := b.startServices(ctx); err != nil { return 1 } return exitCode(b.runLoop(ctx, args.once)) } // wireConfigSurfaces attaches the dashboard's configuration read path and // the reload plumbing behind it. The write handlers (PATCH /api/config, the // per-repository field update, per-row reset) are descoped for the UI // process (archie-core-ymut): the daemon still owns the validate-persist- // publish policy, but no HTTP route reaches it -- editing is config.toml // plus reload -- so only the snapshot publication and the override list the // renderer needs stay wired. func (b *boot) wireConfigSurfaces(ctx context.Context, cfgPath, overlayPath string) { b.wireConfigPublishing(ctx, cfgPath, overlayPath) // Publish once at boot. Every later change goes through publishConfig, // which republishes; without this the UI process would render nothing // until the first reload or dashboard edit. b.publishConfigSnapshot(ctx) } // persistEvents drains the bus until it closes, giving each event an ID // from the store. The store is the only fan-out point (docs/architecture/ // migration-decisions.md, "Dashboard live event delivery"): the UI process // reads the same table through its event pump, so the daemon's job is // persistence, not delivery. // // The insert deliberately outlives ctx: this loop ends when the bus closes, // which is part of shutdown, and the last events of a run are exactly the // ones an operator wants to read afterwards. func persistEvents(ctx context.Context, sink *events.Sub, st storecontract.TaskStore, log *slog.Logger) { ctx = context.WithoutCancel(ctx) for e := range sink.C { if e.ID != 0 { continue } if _, err := st.InsertEvent(ctx, e); err != nil { log.Error("event sink insert failed", "err", err) } } } // curatorEventSink adapts the in-process event bus to the curator family's // narrow emission contract. Bus publish is non-blocking with bounded, // dropping per-subscriber buffers, so curator activity can never // backpressure the daemon or a chat turn. type curatorEventSink struct { b *events.Bus } func (s curatorEventSink) Emit(kind, detail string, data map[string]any) { s.b.Publish(events.Event{Kind: kind, Detail: detail, Data: data}) } // curatorLLMRunner adapts the shared ai-sdk runtime to the curator // family's narrow LLMRunner contract: one model reference, plain // messages, no tools, no streaming -- curators that need more than a // single completion are a bigger decision than this adapter makes. type curatorLLMRunner struct { rt *runtime.Runtime // outcomes records this call for /status alongside sendChatTurn's. A // curator call is a model call this process made, and it does not pass // through sendChatTurn, so without this a daemon whose only recent model // traffic was curator work reports "no calls attempted yet" while the // provider is demonstrably reachable -- or worse, keeps showing a much // older chat outcome as current. outcomes *providerOutcomeRecorder } func (r curatorLLMRunner) Chat(ctx context.Context, req curator.ChatRequest) (curator.ChatResult, error) { // agentexec.NewRuntime returns nil when no provider is configured, and a // nil *runtime.Runtime panics on the first method call. A curator asking // for a completion on a daemon with no providers is a misconfiguration, // not a reason to take the process down. if r.rt == nil { return curator.ChatResult{}, fmt.Errorf("curator chat: no model runtime is configured") } msgs := make([]chat.Message, 0, len(req.Messages)) for _, m := range req.Messages { msgs = append(msgs, chat.Message{Role: chat.Role(m.Role), Content: m.Content}) } res, err := r.rt.Chat(ctx, req.Model, core.GenerateOptions{Messages: msgs, MaxSteps: max(req.MaxSteps, 1)}) r.outcomes.record(req.Model, err) if err != nil { return curator.ChatResult{}, err } return curator.ChatResult{Text: res.Text}, nil } type chatTaskWriterAdapter struct { enqueue func( ctx context.Context, owner, repo, title, body, workflow, identity string, ) (*workflow.Task, error) } func (a chatTaskWriterAdapter) EnqueueChatTask( ctx context.Context, owner, repo, title, body, workflow, identity string, ) (int64, error) { task, err := a.enqueue(ctx, owner, repo, title, body, workflow, identity) if err != nil { return 0, err } if task == nil { return 0, fmt.Errorf("enqueue chat task returned no task") } return task.ID, nil } type chatTaskControllerAdapter struct { taskByID func(context.Context, int64) (*workflow.Task, error) requeue func(context.Context, int64, string, string) error transition func(context.Context, int64, string, string, string) error } func (a chatTaskControllerAdapter) ChatTaskStatus(ctx context.Context, taskID int64) (gateway.ChatTaskStatus, bool, error) { task, err := a.taskByID(ctx, taskID) if err != nil { return gateway.ChatTaskStatus{}, false, err } if task == nil { return gateway.ChatTaskStatus{}, false, nil } if !task.IsForgeBacked() { return gateway.ChatTaskStatus{Status: task.Status, Identity: task.Identity}, true, nil } return gateway.ChatTaskStatus{}, false, fmt.Errorf("task %d is not chat-originated", taskID) } func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID int64) error { return a.requeue(ctx, taskID, workflow.StatusWaitingHuman, "implement") } func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error { task, err := a.taskByID(ctx, taskID) if err != nil { return err } if task == nil { return fmt.Errorf("task %d not found", taskID) } if task.IsForgeBacked() { return fmt.Errorf("task %d is not chat-originated", taskID) } // An operator declining work lands in the same state whichever surface // they used. This used to record StatusRejected while the dashboard // recorded StatusClosedWontDo, so the same decision showed up as two // different states -- and StatusRejected, which the PR reconciler uses // for "the pull request was closed without merging", stopped meaning one // thing. return a.transition(ctx, taskID, task.Status, workflow.StatusClosedWontDo, reason) } // chatTaskLogReaderAdapter gives the gateway a read view of a task's // persisted log history without importing internal/logging or internal/store // into the gateway package. Each identity's reader is scoped to its own // tasks: the identity bound at construction is used for authorization, so a // model cannot read another identity's task logs by passing a different // identity through the tool input. type chatTaskLogReaderAdapter struct { tasks func(context.Context, int64) (*workflow.Task, error) taskLogs *logging.TaskRegistry } func (a chatTaskLogReaderAdapter) ReadChatTaskLogs( ctx context.Context, identity string, taskID int64, attempt int, q gateway.ChatTaskLogQuery, ) (gateway.ChatTaskLogResult, error) { task, err := a.tasks(ctx, taskID) if err != nil { return gateway.ChatTaskLogResult{}, err } if task == nil { return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID) } // Match the filter chatTaskListerAdapter already applies: a model // bound to one identity must not read logs for another identity's // tasks, and tasks with no identity (forge-sourced) are not // readable through a chat tool at all — they belong to the daemon, // not a particular identity. "The empty string MUST NOT retain // special meaning" (docs/architecture/identity.md). if task.Identity != identity { return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to %q, not %q", taskID, task.Identity, identity) } if attempt <= 0 { attempt = task.Attempt } path := a.taskLogs.Path(taskID, attempt) if path == "" { return gateway.ChatTaskLogResult{Attempt: attempt, Entries: []gateway.ChatTaskLogEntry{}}, nil } page, err := logging.Page(path, logging.Query{ Component: q.Component, Contains: q.Contains, Levels: q.Levels, Since: q.Since, Until: q.Until, Limit: q.Limit, }, q.AfterID) if err != nil { return gateway.ChatTaskLogResult{}, err } entries := make([]gateway.ChatTaskLogEntry, len(page.Entries)) for i, e := range page.Entries { entries[i] = gateway.ChatTaskLogEntry{ Time: e.Time, Level: e.Level, Message: e.Message, Fields: e.Fields, } } return gateway.ChatTaskLogResult{ Entries: entries, Attempt: attempt, Truncated: page.Truncated, Cursor: page.Cursor, MoreAvailable: page.MoreAvailable, }, nil } // chatTaskActorAdapter sends messaging actions through the Gateway boundary. type chatTaskActorAdapter struct { contract gateway.ChatContract } func (a chatTaskActorAdapter) ApplyChatTaskAction( ctx context.Context, identity *string, taskID int64, action taskstate.Action, ) (gateway.TaskActionResult, error) { if a.contract == nil { return gateway.TaskActionResult{}, gateway.ErrChatCapabilityUnavailable } if identity == nil { return a.contract.ApplyOperatorTaskAction(ctx, taskID, action) } return a.contract.ApplyTaskAction(ctx, *identity, taskID, action) } func chatTaskProfiles(cfg config.Config) ([]gateway.TaskProfile, string) { if len(cfg.Identities) > 0 { profiles := make([]gateway.TaskProfile, 0, len(cfg.Identities)) for _, identity := range cfg.Identities { if len(identity.Repos) == 0 { continue } profiles = append(profiles, newChatTaskProfile(identity.Name, identity.Repos)) } if len(profiles) == 0 { return nil, "" } return profiles, profiles[0].Identity } if len(cfg.Repos) == 0 { return nil, "" } return []gateway.TaskProfile{newChatTaskProfile("", cfg.Repos)}, "" } func newChatTaskProfile(identity string, repos []config.Repo) gateway.TaskProfile { allowed := make([]string, 0, len(repos)) for _, repo := range repos { allowed = append(allowed, repo.Owner+"/"+repo.Name) } return gateway.TaskProfile{ Identity: identity, DefaultOwner: repos[0].Owner, DefaultRepo: repos[0].Name, Repos: allowed, } } func configureTaskCommands( router *gateway.Router, tasks gateway.TaskCreator, controller gateway.TaskController, lister gateway.ChatTaskLister, identity string, ) { router.Tasks = tasks router.Controller = controller router.TaskLister = lister router.Identity = identity } // parseListenAddr splits "host:port" into components, using defaults // when the input is empty or missing a part. func parseListenAddr(addr, defaultHost string, defaultPort int) (string, int) { if addr == "" { return defaultHost, defaultPort } host, portStr, err := net.SplitHostPort(addr) if err != nil { return defaultHost, defaultPort } port, err := strconv.Atoi(portStr) if err != nil || port < 0 { return host, defaultPort } return host, port } // sessionKey builds a deterministic session identifier from a gateway // message's routing fields. Platform + channel + thread uniquely identify // a conversation for session persistence and history retrieval. func executionProviders(cfg config.Config) map[string]agentexec.Provider { return agentexec.ProvidersFromConfig(cfg.Providers) } // subscribeSystemLogs subscribes to every task's system log subject at once. // A sandboxed container's own stderr disappears at AutoRemove; archie-agent // ships it here instead (agentexec.SubjectForSystem), and this is where it // lands: one wildcard subscription demuxed by taskID and routed through // taskLogs, which is opened for a task's duration in Daemon.process, so a // message for a task not currently open there -- late, duplicate, or from // an attempt this instance never dispatched -- is expected and silently // dropped rather than treated as an error. func subscribeSystemLogs(nc *natsio.Conn, taskLogs *logging.TaskRegistry, log *slog.Logger) (unsubscribe func(), err error) { sub, err := nc.Subscribe(agentexec.SubjectSystemWildcard, func(msg *natsio.Msg) { taskID, ok := agentexec.TaskIDFromSystemSubject(msg.Subject) if !ok { log.Warn("system log message on unparseable subject", "subject", msg.Subject) return } var entry logging.Entry if err := json.Unmarshal(msg.Data, &entry); err != nil { log.Warn("system log message undecodable", "task", taskID, "err", err) return } // The NATS callback has no context of its own; the write is an append to // an already-open sink, so there is nothing to cancel. taskLogs.Write(context.Background(), taskID, entry) }) if err != nil { return nil, err } return func() { if err := sub.Unsubscribe(); err != nil { log.Warn("system log unsubscribe failed", "err", err) } }, nil } // subscribeAgentEvents subscribes to every task's events subject at once and // republishes each decoded event on bus. An archie-agent worker's // agentexec.ForwardTaskEvents ships a task's workflow events (stage // progress, outcome, parking) here because that worker's own *events.Bus is // in-process and invisible to the daemon; this is the other half of the // bridge, landing them on bus so persistAndBroadcastEvents -- the single // choke point that inserts into the SQLite events table and fans out over // SSE -- treats them exactly like every other daemon-observed event. Mirrors // subscribeSystemLogs's demux-by-taskID shape. func subscribeAgentEvents(nc *natsio.Conn, bus *events.Bus, log *slog.Logger) (unsubscribe func(), err error) { sub, err := nc.Subscribe(agentexec.SubjectEventsWildcard, func(msg *natsio.Msg) { taskID, ok := agentexec.TaskIDFromEventsSubject(msg.Subject) if !ok { log.Warn("task event message on unparseable subject", "subject", msg.Subject) return } var e events.Event if err := json.Unmarshal(msg.Data, &e); err != nil { log.Warn("task event message undecodable", "task", taskID, "err", err) return } bus.Publish(e) }) if err != nil { return nil, err } if err := nc.Flush(); err != nil { if cleanupErr := sub.Unsubscribe(); cleanupErr != nil { log.Warn("task event subscription cleanup failed after flush failure", "err", cleanupErr) } return nil, fmt.Errorf("flush task event subscription: %w", err) } return func() { if err := sub.Unsubscribe(); err != nil { log.Warn("task event unsubscribe failed", "err", err) } }, nil } // registerTaskRPCServers subscribes the forgerpc/worktreerpc handlers on nc // so an archie-agent container (which holds no forge token, or push // credential) can proxy those operations back to archied. The returned func // unsubscribes all of them. The agent's store calls are NOT proxied here: they // go over gRPC to the State Store (docs/prds/state-store-contract.md §12 step // 4), so the legacy NATS storerpc registration is deleted. // // Forge and worktree are identity-scoped: the root servers answer the root // (identity-less) subjects for single-identity deployments and root-owned // tasks, and one server pair per identity answers that identity's scoped // subjects so a container-mode task owned by a non-root identity has its RPC // calls served by its own forge client and worktree manager. func registerTaskRPCServers(nc *natsio.Conn, forgeClient forge.Forge, trees *worktree.Manager, identities []*daemon.IdentityRunner, grants *worktreerpc.Grants, log *slog.Logger) (unsubscribe func(), err error) { unsubs := make([]func(), 0, 2+2*len(identities)) unsubAll := func() { for _, u := range unsubs { u() } } registerForge := func(fg forge.Forge, identity string) error { srv := &forgerpc.Server{Forge: fg, Log: log.With("rpc_identity", identity)} u, err := srv.RegisterFor(nc, identity) if err != nil { return fmt.Errorf("register forgerpc%s: %w", identitySuffix(identity), err) } unsubs = append(unsubs, u) return nil } registerTrees := func(mgr *worktree.Manager, identity string) error { srv := &worktreerpc.Server{Trees: mgr, Grants: grants, Log: log.With("rpc_identity", identity)} u, err := srv.RegisterFor(nc, identity) if err != nil { return fmt.Errorf("register worktreerpc%s: %w", identitySuffix(identity), err) } unsubs = append(unsubs, u) return nil } // Root (identity-less) forge and worktree servers. if err := registerForge(forgeClient, ""); err != nil { unsubAll() return nil, err } if err := registerTrees(trees, ""); err != nil { unsubAll() return nil, err } // One forge/worktree server pair per identity, on identity-scoped // subjects. for _, id := range identities { if err := registerForge(id.Forge, id.Name); err != nil { unsubAll() return nil, err } if err := registerTrees(id.Trees, id.Name); err != nil { unsubAll() return nil, err } } return unsubAll, nil } // identitySuffix renders a log suffix for an identity-scoped registration, // empty for the root set. func identitySuffix(identity string) string { if identity == "" { return "" } return " (" + identity + ")" } func configuredNATSToken(cfg config.NATSConfig, getenv func(string) string) (string, error) { if cfg.TokenEnv == "" { return "", nil } token := getenv(cfg.TokenEnv) if token == "" { return "", fmt.Errorf("%s is required when nats.token_env is configured", cfg.TokenEnv) } return token, nil } // updateReportPath is where the update watchdog leaves the phase-2 outcome // of an update for this identity to relay on its next launch. The identity is // hashed so multiple identities sharing one daemon (see // docs/architecture/identity.md) never collide. func updateReportPath(workDir, identity string) string { identityHash := sha256.Sum256([]byte(identity)) return filepath.Join( workDir, fmt.Sprintf("update-report-%x.json", identityHash[:8]), ) } // safePluginInfo calls Name() and Version() on a plugin, recovering from // panics. A panicking plugin must not crash the daemon at startup. func safePluginInfo(p plugin.Plugin) (name, version string) { defer func() { if r := recover(); r != nil { name = "(panic)" version = "(panic)" } }() return p.Name(), p.Version() } // startContainers brings up the mandatory autonomous-worker pool and storage // backend, returning a degraded nil pool when Docker is unavailable. // // Failure here degrades rather than aborting: the native interactive agent and // dashboard remain useful without Docker, while autonomous tasks park because // there is deliberately no host execution fallback. func startContainers( ctx context.Context, cfg config.Config, log *slog.Logger, ) (*container.Pool, storage.Backend, func()) { noop := func() {} // Single Docker client shared between pool and storage backend. dockerCli, err := client.New(client.FromEnv) if err != nil { log.Error("autonomous workflows unavailable: docker client unavailable", "err", err) return nil, nil, noop } closeDocker := func() { if err := dockerCli.Close(); err != nil { log.Warn("docker client close failed", "err", err) } } pool, err := container.NewPool(ctx, container.Config{ Image: cfg.Containers.Image, MaxConcurrency: cfg.Containers.MaxConcurrency, MaxUptime: cfg.Containers.MaxUptime.Std(), PullPolicy: cfg.Containers.PullPolicy, Network: cfg.Containers.Network, DockerClient: dockerCli, // Only the embedded broker binds a discovered host gateway. The // standalone State Store uses its configured, container-reachable target. RequireHostGateway: cfg.NATS.Mode == config.NATSModeEmbedded, }, log) if err != nil { // A missing image is recoverable by hand. The daemon sends no registry // credentials on pull (internal/container/pool.go), so a private // registry always needs the operator's CLI to fetch it first. log.Error("autonomous workflows unavailable: container pool unavailable", "err", err, "hint", "run `docker compose pull agent` (or `build agent`) so the image is present locally") return nil, storage.NewDockerBackend(dockerCli), closeDocker } // contextcheck: Pool.Close takes no context by design -- it is a shutdown // path that must run to completion after ctx is already cancelled. //nolint:contextcheck return pool, storage.NewDockerBackend(dockerCli), func() { if err := pool.Close(); err != nil { log.Warn("container pool close failed", "err", err) } closeDocker() } }