* feat(memory): four-scope contract with CRUD and revisions
Seam for memory unification slice 2 (docs/prds/memory-engine-unification.md
§1, §2, §7): internal/domain/memory is now a scope-addressed CRUD contract.
- Scope{Kind,Agent,User} with the four ratified kinds, Validate rejecting a
component present where it is not required, and a length-prefixed Key so
opaque channel-native ids cannot collide.
- Subject{AgentID,UserID} with Scopes()/WritableScopes(): global is never
writable, and an absent user id removes every scope requiring one.
- Store becomes Create/Get/Query/List/Update/Forget/Revisions over
NewRecord/RecordUpdate/Record/Revision, with ErrNotFound/ErrStaleRevision.
Observation, Identity and Metadata are deleted.
- The content scanner moves into the family (internal/domain/memory) so every
engine applies it in Create and Update; internal/memory re-exports the types
until slice 5 deletes it.
- The markdown store moves to internal/infrastructure/memory/builtin, its
correct home per organisation.md.
WIP: internal/infrastructure/memory and internal/infrastructure/sessioncurator
are converted in the lanes that follow this commit.
* feat(memory): builtin engine CRUD, retained revisions and the curator's agent-user write
Slice 2 implementation (docs/prds/memory-engine-unification.md §1, §2, §6,
§7), on top of the contract seam in the previous commit.
- internal/infrastructure/memory: per-scope markdown heads with a JSON
provenance marker, per-scope HISTORY.md retaining superseded and deleted
states, Update superseding before replacing the live block, ErrStaleRevision
via RecordUpdate.Expected, idempotent Forget, and the family scanner applied
in Create and Update. Scope directories are hex(sha256(Scope.Key())).
- internal/infrastructure/sessioncurator: the participant is derived from the
session's user-role senders; exactly one writes agent-user scope, zero or
several write nothing and return ActionSkipped naming the count.
- docs/architecture: the settled decision recorded in migration-decisions §5
(superseding the 2026-09-02 marker entry) and agent-system.md pointing at it.
Draft: to be squashed and reviewed before publication.
* test(curator): conform the registry's fake memory engine to the CRUD contract
The fake still implemented the pre-CRUD Store surface (Write taking an
Observation, List/Forget keyed by a bare string), so the package's test
binary no longer compiles: fakeMemoryEngine is missing Create, Get, Update
and Revisions, and domainmemory.Observation no longer exists. That took
`go fix ./...`, `go vet ./...` and `go test ./...` down with it, which is
what `task check` runs first.
Missed by the same commit that updated internal/infrastructure/sessioncurator's
own fake to the new interface. Repaired on the fake, not by loosening the
interface: the fake is the only thing out of date.
* fix(sessioncurator): refuse to attribute a session with unidentified user messages
sessionParticipant skipped user-role messages with an empty SenderID and
counted only the identified ones, so a session holding one identified sender
plus any number of unidentified user messages yielded that sender as the
single participant. Those messages are still part of the excerpt the
extraction model reads, so facts derived from content of unknown provenance
were written into the identified sender's agent-user scope: user A's memory
can end up crediting content that may belong to someone else. It is exactly
the case the function's own doc comment claims to handle, which it only
caught when no message at all was identified.
An unidentified user message now makes the session unattributable outright,
beside any number of identified senders, and the skip action's reason names
the count instead of reporting a sender count that no longer explains the
skip. Assistant-role messages are unaffected: they carry the bot, so they
still cannot manufacture a participant.
Fixed in sessionParticipant rather than at the write, because it is the
single producer of the attribution decision: it now returns the participant
or the reason there is none, so the caller cannot print a reason that
disagrees with what it decided. The fail-closed path (nothing written, one
ActionSkipped) and the extraction flow are unchanged.
* fix(memory): carry record content through the block format intact
builtin.Store's format ends a block at a blank line and starts a section at a
"## " line, but storeContent only trimmed the content and rejected an empty
one. Content holding either delimiter was accepted, written, and answered back
to the caller in full -- then read back truncated at the delimiter by Get,
List, or a process restart, with the remainder unaddressable and no error
anywhere along the path.
Fixed at the producer, in the render/parse pair that owns the block format:
renderBlock escapes the two delimiter shapes (an empty line becomes a single
backslash, a line already starting with a backslash or "## " gains one) and
parseBlocks undoes it on the way back, so a record's content survives the
write whole. Rejecting the content instead would have made an ordinary
multi-paragraph note or a quoted heading unrepresentable; escaping every line
would have changed the on-disk shape of every record. Only the lines that need
it are escaped, so a one-paragraph record is byte-identical to what the
unfixed engine wrote and the document stays hand-editable.
Reachable from both producers: the chat tool's note and the curator's
model-extracted fact are free text, and neither is constrained to one
paragraph.
Tests: TestBuiltinEngineRenderedBlockRoundTripsContent asserts
parseBlocks(renderBlock(c)) == c for eleven content shapes, and
TestBuiltinEngineContentSurvivesTheWriteAndAReopen carries the same shapes
through Create, Get, and a second engine reading the file a restart would.
* fix(memory): compare Update's expected revision against the file, not the cache
Update read the record to compare against out of findBlock(stores.live, ...)
-- the engine's cached copy of the scope document -- and checked Expected
against that copy. This engine caches one document per scope and rewrites it
whole, so a second engine over the same scope file (archied and the gateway,
or a restart) holds a copy taken when it last read the file: its view of the
record's revision is superseded, Expected matches the revision it remembers,
ErrStaleRevision never fires, and the rewrite that follows writes that stale
copy back over the file -- discarding the other writer's update and every
record written since the copy was loaded. The field existed and could not
fire.
Reload the scope document before anything is decided from it, so the check
answers about the revision actually on disk and the rewrite starts from the
current document. That is the smaller of the two fixes the PRD leaves open
("Not determined": a reload-before-mutate or an advisory lock); no locking
scheme, no retry loop, and no change to the read paths.
Still unsolved, and deliberately: the whole-document rewrite is
last-writer-wins between the reload and the write, so two engines writing one
scope concurrently can still lose one of the two writes, and HISTORY.md is
rewritten from the cached copy the same way.
Tests: TestBuiltinEngineSecondEngineCannotReuseAStaleRevision asserts the
second engine gets ErrStaleRevision and the first writer's state survives;
TestBuiltinEngineUpdateDoesNotEraseRecordsItHasNotLoaded asserts the record
written after the second engine loaded is still there after its update.
* fix(memory): emit the scanner's warning instead of dropping it
scanContent returned nil for every result that was not ThreatBlock, so a
ThreatWarn result -- documented in the scanner as "allow the write but emit a
warning", and what every sensitive-data match produces (API keys, passwords,
tokens, private keys) -- was stored with no audit signal anywhere. The
comment justified the silence by pointing at Bind, but Bind's argument is
about Registrar.Events: internal/events declares no memory kind and the
composition binds no memory event sink, so an invented kind would look wired
and not be. A scanner warning is a diagnostic rather than an event, and there
was nothing else to carry it: the registrar had no logger.
Bind now takes Registrar.Log, optional and mirroring the curator family's
Registrar for the same reason, scanContent emits the warn-level result
through it before the write is allowed, and bootstrap passes the daemon's
logger so the line reaches archied's configured sinks instead of only the
process default. The write is still allowed: only the silence changed.
Tests: TestBuiltinEngineScanWarnsWithoutRefusingTheWrite runs Create and
Update over warn, clean and block content, asserting the write lands either
way, that a warn-level scan result logs exactly one warning carrying the
scanner's own message, that clean content logs none, and that a refused
block-level match logs none.
* test(memory): carry every content shape through Update and Revisions
Update replaces the block it read out of the document by that text, and
retains the superseded state in HISTORY.md, so an escaped block has to be
found, superseded and retained exactly like an unescaped one. Covered here
rather than left to the Create path: without the escaping, every shape in
this table came back truncated after an update, including the plain
one-paragraph record whose replacement holds a blank line.
The test asserts values only: the updated record as a second engine reads it
off disk, and the superseded content as Revisions returns it.
* style(memory): apply the linter's gofumpt form to the Update revision error
golangci-lint's bundled gofumpt wants the closing paren of that wrapped
fmt.Errorf on its own line; the standalone gofumpt binary the fmt task uses
(0.7.0, a different version) leaves it as it was. The text is unchanged from
before the engine fixes in this branch, so this is pre-existing drift inside a
function these commits already touch -- taken so the file is clean under both
formatters rather than only one.
1791 lines
73 KiB
Go
1791 lines
73 KiB
Go
// bootstrap.go owns the composition of the daemon: run() is the entry
|
|
// point and driver, and each phase of wiring lives in a method on boot.
|
|
// The struct exists because the composition root assembles ~30 pieces of
|
|
// state that every phase touches; passing them as parameters would make
|
|
// each phase function longer than the code it contains.
|
|
package archied
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/samcharles93/ai-sdk/runtime"
|
|
|
|
"github.com/samcharles93/archie-core/internal/channels/email"
|
|
"github.com/samcharles93/archie-core/internal/channels/status"
|
|
"github.com/samcharles93/archie-core/internal/channels/webhook"
|
|
"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/curator"
|
|
"github.com/samcharles93/archie-core/internal/domain/eda/module"
|
|
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
|
|
domainembedding "github.com/samcharles93/archie-core/internal/domain/embedding"
|
|
"github.com/samcharles93/archie-core/internal/domain/health"
|
|
domainmemory "github.com/samcharles93/archie-core/internal/domain/memory"
|
|
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild"
|
|
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/forge"
|
|
forgewebhook "github.com/samcharles93/archie-core/internal/forge/webhook"
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
"github.com/samcharles93/archie-core/internal/indexing"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay"
|
|
infraembedding "github.com/samcharles93/archie-core/internal/infrastructure/embedding"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
|
infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/sessioncurator"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/skillcurator"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
|
"github.com/samcharles93/archie-core/internal/logging"
|
|
"github.com/samcharles93/archie-core/internal/memory"
|
|
"github.com/samcharles93/archie-core/internal/plugin"
|
|
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
|
|
"github.com/samcharles93/archie-core/internal/ratelimit"
|
|
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
|
"github.com/samcharles93/archie-core/internal/secret"
|
|
"github.com/samcharles93/archie-core/internal/skill"
|
|
"github.com/samcharles93/archie-core/internal/storage"
|
|
"github.com/samcharles93/archie-core/internal/tools"
|
|
toolsbuiltin "github.com/samcharles93/archie-core/internal/tools/builtin"
|
|
"github.com/samcharles93/archie-core/internal/tools/minimax"
|
|
toolprovider "github.com/samcharles93/archie-core/internal/tools/provider"
|
|
builtintoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/builtin"
|
|
memorytoolprovider "github.com/samcharles93/archie-core/internal/tools/provider/memory"
|
|
"github.com/samcharles93/archie-core/internal/tools/sendfile"
|
|
"github.com/samcharles93/archie-core/internal/tools/webfetch"
|
|
"github.com/samcharles93/archie-core/internal/webui"
|
|
"github.com/samcharles93/archie-core/internal/worktree"
|
|
"github.com/samcharles93/archie-core/internal/worktreerpc"
|
|
)
|
|
|
|
// boot is the mutable wiring state assembled by the run() composition
|
|
// phases. Cleanup funcs are registered in creation order and run in
|
|
// reverse at shutdown, matching the LIFO ordering of the deferred calls
|
|
// they replace.
|
|
type boot struct {
|
|
cfg config.Config
|
|
log *slog.Logger
|
|
|
|
loader *configuration.Loader
|
|
doc *configuration.Document
|
|
overlayStore *overlay.Store
|
|
bootOverlayErr atomic.Pointer[string]
|
|
currentProvenance atomic.Pointer[configuration.Provenance]
|
|
|
|
health *healthSurface
|
|
logFeed *logging.Feed
|
|
taskLogs *logging.TaskRegistry
|
|
|
|
secrets *secret.Registry
|
|
forgeClient forge.Forge
|
|
token string
|
|
|
|
modules *module.ModuleRegistry
|
|
|
|
playbooks *playbook.Store
|
|
|
|
st storecontract.TaskStore
|
|
// stateStore is the State Store contract adapter every daemon and gateway
|
|
// store consumer depends on. It is ALWAYS the remote *staterpc.Client
|
|
// dialed to the standalone archie-state-store gRPC service at
|
|
// [services.state].target (the daemon and gateway no longer own archie.db
|
|
// in-process, per docs/prds/state-store-contract.md §12 step 7). It is set
|
|
// by openStateStoreAdapter, which requires [services.state].target to be
|
|
// set. The b.st field remains solely for the standalone archie-state-store
|
|
// binary, which owns the single SQLite file.
|
|
stateStore storecontract.TaskStore
|
|
// stateStoreGrants issues per-task, scoped State Store credentials for
|
|
// agent containers (daemon.StateStoreGrantIssuer), wrapping the same
|
|
// *staterpc.Client as stateStore. Nil when the State Store adapter isn't
|
|
// the remote gRPC client (never true in production; state_store.go's
|
|
// standalone-only compose leaves no other case).
|
|
stateStoreGrants *staterpc.GrantIssuer
|
|
stateStoreToken string
|
|
chatSessionStore gateway.SessionStore
|
|
|
|
catalog modelcatalog.Snapshot
|
|
catalogModels []string
|
|
|
|
bus *events.Bus
|
|
channelManager *status.Manager
|
|
// rateLimiter is the shared per-(channel, sender) inbound budget every
|
|
// chat Router is given. Nil when [chat.rate_limit] is not configured,
|
|
// which leaves rate limiting off.
|
|
rateLimiter *ratelimit.Limiter
|
|
// cfgHolder is the daemon's one configuration Holder. Boot owns it and
|
|
// the daemon reads through it, so a reload swaps one snapshot and every
|
|
// reader sees it: there is no second holder to keep in step.
|
|
cfgHolder *config.Holder
|
|
// chat is the dashboard-shaped chat surface. It survives the UI
|
|
// extraction because the readiness gateway probe and the Telegram task
|
|
// actor both read the same contract.
|
|
chat *webui.ChatService
|
|
// healthRegistry backs /health/detailed on the daemon's own listener.
|
|
healthRegistry *health.Registry
|
|
// lastReload reports the most recent reload outcome for the published
|
|
// configuration projection. Nil until reload wiring installs it.
|
|
lastReload func() config.ReloadStatus
|
|
|
|
natsClient *nats.Client
|
|
// natsURL is the endpoint the daemon's own client connected with at
|
|
// startup. For external mode it is cfg.NATS.URL; for embedded mode it is
|
|
// the embedded server's ClientURL(). Recorded so Daemon.ConnectedNATS
|
|
// carries the real endpoint rather than an empty config URL.
|
|
natsURL string
|
|
// natsToken is the credential the daemon's client connected with at
|
|
// startup. Embedded mode generates it; external mode resolves it once.
|
|
natsToken string
|
|
|
|
containerPool *container.Pool
|
|
storeBackend storage.Backend
|
|
|
|
llm *runtime.Runtime
|
|
|
|
// embeddings is nil unless models["embedding"] and a working provider
|
|
// credential are both configured -- see setupLLMAndChat's "Embeddings
|
|
// capability" section. Consumers must treat a nil embeddings the same
|
|
// as domainembedding.ErrUnavailable: skip, never fail startup or an
|
|
// unrelated call.
|
|
embeddings domainembedding.Client
|
|
|
|
toolReg *tools.Registry
|
|
chatModels gateway.ModelManager
|
|
personas *gateway.PersonaRegistry
|
|
chatTasks gateway.TaskCreator
|
|
chatController *gateway.StoreTaskController
|
|
// chatPRReviewer is the operator-triggered PR review capability, built
|
|
// once and shared by every channel so a review in flight on one channel
|
|
// is deduplicated against the same request on another (single-flight).
|
|
chatPRReviewer gateway.ChatPRReviewer
|
|
defaultChatIdentity string
|
|
updateService *releaseupdate.Service
|
|
|
|
startGateways []func()
|
|
capabilityHost *plugin.Host
|
|
trees *worktree.Manager
|
|
worktreeGrants *worktreerpc.Grants
|
|
identityRunners []*daemon.IdentityRunner
|
|
memManager *memory.Manager
|
|
memEngines *domainmemory.Registry
|
|
curatorRegistry *curator.Registry
|
|
curatorRuntime *curator.Runtime
|
|
guardrails *tools.GuardrailEngine
|
|
providerRegistry *toolprovider.Registry
|
|
d *daemon.Daemon
|
|
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
|
|
// Nil when no chat task creator is configured, in which case startServices
|
|
// leaves it unstarted rather than running with no reachable job kind.
|
|
schedulingEngine *scheduling.Engine
|
|
|
|
// kindWorkflows/labelWorkflows are the resolved kind/label -> workflow
|
|
// routing bindings loaded by loadWorkflowRouting. They are handed to the
|
|
// daemon so runViaAgent can carry them in taskrun.Request: workflow.Route
|
|
// runs in the archie-agent process, which never sees the daemon's
|
|
// package-level workflow state.
|
|
kindWorkflows workflow.KindWorkflows
|
|
labelWorkflows workflow.LabelWorkflows
|
|
|
|
// agentStatus records the most recent version/install-type an
|
|
// archie-agent worker reported about itself. Allocated up front, before
|
|
// any gateway goroutine (Telegram, webui) that might read it via
|
|
// RunningVersions starts, so those closures never see a nil pointer or
|
|
// race its assignment -- only its own internal mutex guards concurrent
|
|
// Observe/Snapshot calls once tasks start completing.
|
|
agentStatus *daemon.AgentStatus
|
|
|
|
// shutdown cancels the daemon's root context, the exact path a SIGTERM
|
|
// takes to graceful shutdown. The drain monitor invokes it when it honours
|
|
// an external drain request, so a marker-driven drain reuses the operator
|
|
// signal path rather than inventing a new one.
|
|
shutdown context.CancelFunc
|
|
|
|
cleanups []func()
|
|
}
|
|
|
|
func newBootstrap() *boot {
|
|
return &boot{log: slog.New(slog.NewJSONHandler(os.Stderr, nil)), agentStatus: &daemon.AgentStatus{}}
|
|
}
|
|
|
|
func (b *boot) addCleanup(fn func()) {
|
|
b.cleanups = append(b.cleanups, fn)
|
|
}
|
|
|
|
func (b *boot) cleanup() {
|
|
for _, v := range slices.Backward(b.cleanups) {
|
|
v()
|
|
}
|
|
}
|
|
|
|
// loadConfig resolves the file config and layers the runtime overlay
|
|
// over it. A Resolve failure is reported on stderr because the file log
|
|
// destination is itself configuration that has not been read yet.
|
|
func (b *boot) loadConfig(ctx context.Context, cfgPath, overlayPath string, noConfigOverlay bool) error {
|
|
loader := configuration.New(b.log)
|
|
b.loader = loader
|
|
skipOverlay := noConfigOverlay || configuration.SkipOverlay()
|
|
if skipOverlay {
|
|
b.log.Info("runtime config overlay disabled (recovery hatch); booting on file config alone")
|
|
}
|
|
doc, err := loader.Resolve(cfgPath, overlayPath)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return err
|
|
}
|
|
b.doc = doc
|
|
// A stray/misspelled key parses and validates cleanly (unknown TOML
|
|
// keys are otherwise silently discarded), so it must be visible
|
|
// somewhere rather than just quietly doing nothing -- see
|
|
// docs/architecture/configuration.md's startup policy: an invalid
|
|
// config is fatal, but a typo like this is not that, only a warning.
|
|
if len(doc.UnknownKeys) > 0 {
|
|
b.log.Warn("config file has unrecognised keys; check for typos", "keys", doc.UnknownKeys)
|
|
}
|
|
// Runtime config overlay: dashboard-edited overrides layered over the
|
|
// file config from their own SQLite file (own user_version and
|
|
// migrator, no contention with the task store). Skipped under
|
|
// --no-config-overlay / ARCHIE_SKIP_CONFIG_OVERLAY=1, or when the
|
|
// store cannot be opened or its values fail validation -- a broken
|
|
// overlay must not brick the daemon. bootOverlayErr carries the
|
|
// degrade reason into /api/config's reload status so it is visible
|
|
// where the operator is looking, not only in logs.
|
|
if !skipOverlay {
|
|
b.overlayStore, b.doc = bootConfigOverlay(ctx, loader, doc, &b.bootOverlayErr, b.log)
|
|
if b.overlayStore != nil {
|
|
store := b.overlayStore
|
|
b.addCleanup(func() { _ = store.Close() })
|
|
}
|
|
}
|
|
b.cfg = b.doc.Config
|
|
b.currentProvenance.Store(&b.doc.Provenance)
|
|
return b.setupLogging()
|
|
}
|
|
|
|
// setupLogging re-creates the logger now the config is known. Everything
|
|
// before this point logs to stderr only, which is unavoidable: the log
|
|
// destination is itself configuration. A file that cannot be opened is
|
|
// reported and the daemon continues on stderr -- losing the durable copy
|
|
// must not take the daemon down with it.
|
|
func (b *boot) setupLogging() error {
|
|
cfg := b.cfg
|
|
logFeed := logging.NewFeed(1000)
|
|
b.logFeed = logFeed
|
|
// Task logs live alongside the store rather than under cfg.Log.File's
|
|
// directory: cfg.Log.File is optional (file logging can be off), while
|
|
// DBPath is required for the daemon to run at all, so it is the more
|
|
// reliable anchor for "where archie keeps its state" on this host.
|
|
taskLogs := logging.NewTaskRegistry(filepath.Join(filepath.Dir(cfg.DBPath), "logs", "tasks"), logFeed, logging.TaskSinkOptions{})
|
|
b.taskLogs = taskLogs
|
|
fileLog, logCloser, logErr := logging.New(logging.Options{
|
|
File: cfg.Log.File,
|
|
MaxSizeMB: cfg.Log.MaxSizeMB,
|
|
Keep: cfg.Log.Keep,
|
|
Level: cfg.Log.Level,
|
|
Stderr: !cfg.Log.Quiet,
|
|
Feed: logFeed,
|
|
})
|
|
// All daemon diagnostics carry an explicit component so the dashboard's
|
|
// component filter does not have to infer ownership from message text.
|
|
b.log = fileLog.With("component", "daemon")
|
|
b.addCleanup(func() { _ = logCloser.Close() })
|
|
if logErr != nil {
|
|
b.log.Error("file logging disabled", "err", logErr)
|
|
} else if cfg.Log.File != "" {
|
|
b.log.Info("logging to file", "path", cfg.Log.File)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// openStores wires the secret registry, forge client and both stores.
|
|
func (b *boot) openStores(ctx context.Context) error {
|
|
cfg, log := b.cfg, b.log
|
|
secrets, err := configuredSecretRegistry(&cfg, log)
|
|
if err != nil {
|
|
log.Error("configure secrets", "err", err)
|
|
return err
|
|
}
|
|
b.secrets = secrets
|
|
b.forgeClient, b.token = resolveForge(cfg.Forge, secrets, log)
|
|
|
|
// The daemon and gateway no longer open archie.db directly: the single
|
|
// SQLite file is owned by the standalone archie-state-store process, and
|
|
// both consumers dial its gRPC State Store contract. openStateStoreAdapter
|
|
// (called by Run and RunGateway after openStores) resolves b.stateStore as
|
|
// the remote *staterpc.Client. There is no local store to open here
|
|
// (docs/prds/state-store-contract.md §12 step 7, no dual-store ownership).
|
|
return b.openChatSessions(ctx)
|
|
}
|
|
|
|
// openStateStoreAdapter resolves the State Store contract adapter as the
|
|
// remote *staterpc.Client dialed to [services.state].target, so every daemon
|
|
// and gateway store consumer uses one endpoint -- the standalone
|
|
// archie-state-store service. It requires [services.state].target to be set:
|
|
// after .4.6 the daemon and gateway no longer own archie.db in-process, so the
|
|
// local default of earlier phases is removed (no dual-store ownership;
|
|
// docs/prds/state-store-contract.md §12 step 7). The token the daemon also
|
|
// injects into agent containers (STATE_STORE_TOKEN) is the same one this
|
|
// client authenticates with, resolved from [services.state].target_token or
|
|
// the STATE_STORE_TOKEN secret (§10). It runs after openStores has resolved
|
|
// b.secrets, and before setupObservability / buildDaemon wire consumers.
|
|
func (b *boot) openStateStoreAdapter() error {
|
|
target := strings.TrimSpace(b.cfg.Services.Get(config.ServiceNameState).Target)
|
|
if target == "" {
|
|
return fmt.Errorf("services.state.target is required: archied/archie-gateway no longer own archie.db; the standalone archie-state-store process owns it (docs/prds/state-store-contract.md §12 step 7)")
|
|
}
|
|
client, cleanup, err := composeStateStoreClient(b.cfg.Services, b.secrets)
|
|
if err != nil {
|
|
b.log.Error("state store adapter", "err", err)
|
|
return err
|
|
}
|
|
b.stateStore = client
|
|
b.stateStoreGrants = &staterpc.GrantIssuer{Client: client}
|
|
b.stateStoreToken = b.cfg.Services.ResolvedToken(config.ServiceNameState, b.secrets.Getenv)
|
|
b.addCleanup(cleanup)
|
|
return nil
|
|
}
|
|
|
|
func (b *boot) openChatSessions(_ context.Context) error { //nolint:unparam // context keeps the composition phase contract aligned with other openers
|
|
chatSessionStore, err := makeTelegramSessionStore(b.cfg) //nolint:contextcheck // SQLite schema initialization is synchronous and has no context-aware API
|
|
if err != nil {
|
|
return fmt.Errorf("open conversation store: %w", err)
|
|
}
|
|
b.chatSessionStore = chatSessionStore
|
|
b.addCleanup(func() {
|
|
if err := chatSessionStore.Close(); err != nil {
|
|
b.log.Error("close conversation store", "err", err)
|
|
}
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// handleRequeue replays a parked/waiting task by id, then signals an
|
|
// early success exit unless -once keeps the daemon running.
|
|
func (b *boot) handleRequeue(ctx context.Context, requeue int64, once bool) (bool, error) {
|
|
if requeue <= 0 {
|
|
return false, nil
|
|
}
|
|
if err := manualRequeueTask(ctx, b.stateStore, requeue); err != nil {
|
|
b.log.Error("requeue failed", "task", requeue, "err", err)
|
|
return false, err
|
|
}
|
|
b.log.Info("task requeued", "task", requeue)
|
|
return !once, nil
|
|
}
|
|
|
|
func (b *boot) loadCatalog(ctx context.Context, cfgPath string) {
|
|
catalog, err := modelcatalog.Load(ctx, modelcatalog.Options{
|
|
CachePath: filepath.Join(filepath.Dir(cfgPath), "models.json"),
|
|
Getenv: b.secrets.Getenv,
|
|
Configured: b.cfg.Providers,
|
|
})
|
|
b.catalog = catalog
|
|
if err != nil {
|
|
b.log.Warn("model catalog unavailable; using configured providers and models", "err", err)
|
|
return
|
|
}
|
|
b.catalogModels = applyModelCatalog(&b.cfg, catalog)
|
|
b.log.Info("model catalog loaded", "providers", len(catalog.Providers), "models", len(b.catalogModels))
|
|
}
|
|
|
|
// channelDescriptors projects the configured chat front-ends into the
|
|
// dashboard's channel-status descriptors.
|
|
//
|
|
// Configured comes from config.ChatConfig.FrontEnds so this list and the
|
|
// configuration projection published for the extracted UI process cannot
|
|
// disagree about whether a front-end is set up (GitHub #821). ReloadSupported
|
|
// and Detail stay here because they are channel-lifecycle facts about the bot
|
|
// the daemon runs, not configuration: only Telegram can be reloaded in place
|
|
// today, and its allowlist caveat is about who the running bot answers.
|
|
func channelDescriptors(chat config.ChatConfig) []status.Descriptor {
|
|
frontEnds := chat.FrontEnds()
|
|
descriptors := make([]status.Descriptor, 0, len(frontEnds))
|
|
for _, frontEnd := range frontEnds {
|
|
descriptor := status.Descriptor{
|
|
ID: frontEnd.ID,
|
|
Name: frontEnd.Name,
|
|
Configured: frontEnd.Configured,
|
|
}
|
|
if frontEnd.ID == "telegram" {
|
|
descriptor.ReloadSupported = frontEnd.Configured
|
|
if frontEnd.Configured && len(chat.Telegram.AllowedUserIDs) == 0 {
|
|
descriptor.Detail = "Token set, but the allowlist is empty -- the bot answers nobody."
|
|
}
|
|
}
|
|
descriptors = append(descriptors, descriptor)
|
|
}
|
|
return descriptors
|
|
}
|
|
|
|
// setupObservability builds the event bus, channel manager and dashboard
|
|
// server. Every event is logged to SQLite (stamped with its row id) and
|
|
// then fanned out to live dashboard connections.
|
|
func (b *boot) setupObservability(ctx context.Context) {
|
|
cfg, log := b.cfg, b.log
|
|
bus := events.NewBus()
|
|
b.bus = bus
|
|
b.addCleanup(func() { bus.Close() })
|
|
b.channelManager = status.NewManager(channelDescriptors(cfg.Chat))
|
|
b.cfgHolder = config.NewHolder(cfg)
|
|
// The watchdog leaves its verdict in a file on this host, so the daemon
|
|
// reads it and publishes the outcome as an event; the dashboard renders
|
|
// what it receives, wherever it runs (archie-core-8cda.5.4).
|
|
b.startUpdateRelay(ctx, updateReportPath(cfg.WorkDir, "webui"))
|
|
sink := bus.Subscribe(256)
|
|
go persistEvents(ctx, sink, b.stateStore, log)
|
|
}
|
|
|
|
// reactionStreamMaxAge bounds how long a reaction survives in the fan-out
|
|
// stream before JetStream discards it. Reactions are producer-only wake
|
|
// events (docs/prds/event-sources-and-reactions.md): a dropped reaction only
|
|
// delays work until the next poll, never loses it, because the authoritative
|
|
// state is re-read at pass time. One day therefore tolerates a consumer that
|
|
// is down or lagging for a full maintenance window without letting
|
|
// acknowledged reactions accumulate without bound.
|
|
const reactionStreamMaxAge = 24 * time.Hour
|
|
|
|
// connectNATS opens the NATS client. External mode dials cfg.NATS.URL;
|
|
// embedded mode starts an in-process nats-server and dials it, so single-
|
|
// process deployments get task distribution and reaction delivery without a
|
|
// separate server. Broker deployment never changes the worker executor.
|
|
func (b *boot) connectNATS(ctx context.Context) error { //nolint:nestif // embedded broker discovery and startup require explicit fallback branches
|
|
cfg, log := b.cfg, b.log
|
|
url := cfg.NATS.URL
|
|
var natsToken string
|
|
if cfg.NATS.Mode == config.NATSModeExternal { //nolint:nestif // embedded broker discovery and startup require explicit fallback branches
|
|
if url == "" {
|
|
return fmt.Errorf("nats.url is required when nats.mode is external")
|
|
}
|
|
var err error
|
|
natsToken, err = configuredNATSToken(cfg.NATS, os.Getenv)
|
|
if err != nil {
|
|
log.Error("nats credentials", "err", err)
|
|
return err
|
|
}
|
|
} else {
|
|
endpoint, readErr := readEmbeddedNATSEndpoint(cfg.DBPath)
|
|
if readErr == nil {
|
|
url, natsToken = endpoint.URL, endpoint.Token
|
|
if probe, err := nats.Connect(ctx, nats.Config{URL: url, Token: natsToken, Subjects: []string{workintake.SubjectTaskWildcard}, FilterSubject: workintake.SubjectTaskWildcard}, log); err == nil {
|
|
probe.Close()
|
|
// The endpoint is live; the connection is recreated below with the
|
|
// same subject configuration and becomes the daemon's owner.
|
|
url, natsToken = endpoint.URL, endpoint.Token
|
|
} else {
|
|
url, natsToken, err = b.startEmbeddedNATS(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
var err error
|
|
url, natsToken, err = b.startEmbeddedNATS(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// Composition owns the subject list: the bus must not know which
|
|
// subjects belong to which domain.
|
|
natsClient, err := nats.Connect(ctx, nats.Config{
|
|
URL: url,
|
|
Token: natsToken,
|
|
Subjects: []string{workintake.SubjectTaskWildcard},
|
|
FilterSubject: workintake.SubjectTaskWildcard,
|
|
}, log)
|
|
if err != nil {
|
|
log.Error("nats connect failed", "err", err)
|
|
return err
|
|
}
|
|
|
|
// Reactions are producer-only fan-out events: every interested consumer
|
|
// must see each one, so they get their own stream under LimitsPolicy
|
|
// rather than the work-queue policy ARCHIE_TASKS uses (which lets a
|
|
// second consumer on an overlapping filter silently receive nothing).
|
|
// LimitsPolicy retains every message until a limit is reached, so without
|
|
// a finite limit acknowledged reactions would accumulate forever; the
|
|
// MaxAge cap bounds that by time.
|
|
reactionMaxAge := reactionStreamMaxAge
|
|
reactionClient, err := nats.Connect(ctx, nats.Config{
|
|
URL: url,
|
|
Token: natsToken,
|
|
StreamName: nats.DefaultReactionStreamName,
|
|
Subjects: []string{workintake.SubjectReactionWildcard},
|
|
FilterSubject: workintake.SubjectReactionWildcard,
|
|
Retention: nats.FanOutRetention(),
|
|
MaxAge: &reactionMaxAge,
|
|
}, log)
|
|
if err != nil {
|
|
log.Error("nats reaction stream connect failed", "err", err)
|
|
natsClient.Close()
|
|
return err
|
|
}
|
|
|
|
b.natsClient = natsClient
|
|
b.natsURL = url
|
|
b.natsToken = natsToken
|
|
b.addCleanup(func() { natsClient.Close() })
|
|
// The reaction client is kept only so its connection stays open for the
|
|
// daemon's lifetime and is closed at shutdown; the stream it provisions
|
|
// is the deliverable. A future producer (bead archie-core-8li9.3) will
|
|
// need its own publisher surface, wired when that step lands.
|
|
b.addCleanup(func() { reactionClient.Close() })
|
|
log.Info("nats connected", "url", url, "task_stream", nats.DefaultStreamName, "reaction_stream", nats.DefaultReactionStreamName)
|
|
return nil
|
|
}
|
|
|
|
// startEmbeddedNATS starts the in-process nats-server and returns the client
|
|
// URL and token to dial it with. Its shutdown is registered BEFORE the caller
|
|
// registers the client close, so the client closes first (cleanups run LIFO).
|
|
func (b *boot) startEmbeddedNATS(ctx context.Context) (string, string, error) {
|
|
cfg, log := b.cfg, b.log
|
|
host := ""
|
|
if b.containerPool != nil {
|
|
host = b.containerPool.HostGateway()
|
|
}
|
|
srv, err := nats.StartEmbedded(ctx, nats.EmbeddedOptions{
|
|
Host: host,
|
|
StoreDir: filepath.Join(filepath.Dir(cfg.DBPath), "nats"),
|
|
}, log)
|
|
if err != nil {
|
|
log.Error("embedded nats start failed", "err", err)
|
|
return "", "", err
|
|
}
|
|
b.addCleanup(func() { srv.Shutdown() })
|
|
log.Info("embedded nats started", "url", srv.ClientURL())
|
|
if err := writeEmbeddedNATSEndpoint(cfg.DBPath, srv.ClientURL(), srv.Token()); err != nil {
|
|
srv.Shutdown()
|
|
return "", "", err
|
|
}
|
|
return srv.ClientURL(), srv.Token(), nil
|
|
}
|
|
|
|
// setupContainers builds the managed autonomous-worker pool. Failure degrades
|
|
// rather than aborting because the native interactive agent and dashboard do
|
|
// not require Docker; repository tasks park instead of falling back to a host
|
|
// model loop.
|
|
func (b *boot) setupContainers(ctx context.Context) func() {
|
|
containerPool, storeBackend, closeDocker := startContainers(ctx, b.cfg, b.log)
|
|
b.containerPool = containerPool
|
|
b.storeBackend = storeBackend
|
|
return closeDocker
|
|
}
|
|
|
|
// setupEmbeddings builds the optional embedding capability. A client is
|
|
// wired only when models["embedding"] names a provider/model and that
|
|
// provider's credential resolves -- the same credential-missing-degrades-
|
|
// not-fatal rule registerMinimaxTool follows for generate_video. No
|
|
// warning when the role was simply never configured; a warning when it was
|
|
// configured but couldn't be made to work, so a broken setup doesn't go
|
|
// unnoticed the way AGENTS.md already warns an unnoticed optional-provider
|
|
// degradation can.
|
|
func (b *boot) setupEmbeddings(cfg config.Config, log *slog.Logger) {
|
|
if client, ok := infraembedding.New(cfg, infraembedding.Options{}); ok {
|
|
b.embeddings = client
|
|
log.Info("embedding capability enabled", "role", infraembedding.Role)
|
|
} else if cfg.Models[infraembedding.Role] != "" {
|
|
log.Warn("embedding capability configured but unavailable; capability disabled", "role", infraembedding.Role)
|
|
}
|
|
}
|
|
|
|
// setupLLMAndChat wires the runtime, tool registry, model management,
|
|
// personas and the dashboard's chat service.
|
|
func (b *boot) setupLLMAndChat() error {
|
|
cfg, log := b.cfg, b.log
|
|
|
|
// Telegram/email keep their own in-process routers rather than going
|
|
// through ChatContract: their streaming responder is a callback, and
|
|
// ChatContract's wire-safe interface can't carry one.
|
|
b.setupChatRuntime(cfg)
|
|
|
|
b.setupEmbeddings(cfg, log)
|
|
contract, cleanup, err := composeChatContract(cfg.Services, b.secrets)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b.addCleanup(cleanup)
|
|
// Channel routers execute turns locally and need the SQLite TurnLedger.
|
|
// The remote contract serves web chat; it must not replace their store.
|
|
b.chat = &webui.ChatService{Contract: contract, Updates: b.updateService}
|
|
b.setupReadinessProbes()
|
|
return nil
|
|
}
|
|
|
|
// rateLimiterEvictInterval is how often an active Limiter sweeps entries
|
|
// whose hits have all aged out of the window, per internal/ratelimit's own
|
|
// documented ticker contract.
|
|
const rateLimiterEvictInterval = time.Minute
|
|
|
|
// startRateLimiter constructs b.rateLimiter from cfg when configured, and
|
|
// drives its documented EvictStale ticker for the life of ctx. Leaves
|
|
// b.rateLimiter nil (rate limiting off) when cfg is not enabled.
|
|
func (b *boot) startRateLimiter(ctx context.Context, cfg config.RateLimitConfig) {
|
|
if !cfg.Enabled() {
|
|
return
|
|
}
|
|
b.rateLimiter = ratelimit.New(cfg.Window, cfg.MaxRequests)
|
|
limiter := b.rateLimiter
|
|
go func() {
|
|
ticker := time.NewTicker(rateLimiterEvictInterval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
limiter.EvictStale()
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// setupGateways assembles the Telegram, email and webhook gateways. It
|
|
// returns false when the Telegram gateway could not start, which the
|
|
// caller treats as a fatal boot error.
|
|
//
|
|
// Multi-agent collaboration PRD phase C (docs/prds/multi-agent-collaboration.md).
|
|
func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) bool {
|
|
cfg, log := b.cfg, b.log
|
|
b.startRateLimiter(ctx, cfg.Chat.RateLimit)
|
|
start, ok := setupTelegramGateway(ctx, telegramSetup{
|
|
Cfg: config.NewHolder(cfg), CfgPath: cfgPath, OverlayPath: overlayPath,
|
|
St: b.stateStore, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg,
|
|
Personas: b.personas, ChatTasks: b.chatTasks, ChatController: b.chatController,
|
|
ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks},
|
|
ChatTaskLogs: chatTaskLogReaderAdapter{
|
|
tasks: b.stateStore.TaskByID,
|
|
taskLogs: b.taskLogs,
|
|
},
|
|
ChatTaskActor: chatTaskActorAdapter{
|
|
contract: func() gateway.ChatContract {
|
|
if b.chat != nil {
|
|
return b.chat.Contract
|
|
}
|
|
return nil
|
|
}(),
|
|
},
|
|
ChatPRReviewer: b.prReviewer(),
|
|
DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, Updates: b.updateService,
|
|
Secrets: b.secrets,
|
|
Bus: b.bus,
|
|
Log: log,
|
|
ChannelManager: b.channelManager, AgentStatus: b.agentStatus,
|
|
RateLimiter: b.rateLimiter,
|
|
})
|
|
if !ok {
|
|
return false
|
|
}
|
|
if start != nil {
|
|
b.startGateways = append(b.startGateways, start)
|
|
}
|
|
|
|
if !b.setupEmailGateway(ctx, cfg, log) {
|
|
return false
|
|
}
|
|
if !b.setupWebhookGateway(ctx, cfg, log) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// setupEmailGateway registers the optional inbound email gateway when
|
|
// chat.email.listen_addr is configured. It returns false when the
|
|
// configured gateway fails its own ConfigSchema validation, which the
|
|
// caller treats as a fatal boot error -- the same convention
|
|
// setupTelegramGateway already uses.
|
|
func (b *boot) setupEmailGateway(ctx context.Context, cfg config.Config, log *slog.Logger) bool {
|
|
if cfg.Chat.Email.ListenAddr == "" {
|
|
return true
|
|
}
|
|
em := email.New(cfg.Chat.Email.ListenAddr, cfg.Chat.Email.RelayAddr, log)
|
|
if err := em.ValidateConfig(map[string]any{
|
|
"listen_addr": cfg.Chat.Email.ListenAddr,
|
|
"relay_addr": cfg.Chat.Email.RelayAddr,
|
|
}); err != nil {
|
|
log.Error("chat.email config invalid", "err", err)
|
|
return false
|
|
}
|
|
emRouter := gateway.NewRouter(b.stateStore, nil, "email")
|
|
emRouter.Limiter = b.rateLimiter
|
|
configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
|
b.startGateways = append(b.startGateways, func() {
|
|
go func() {
|
|
lifecycle := gateway.Lifecycle{
|
|
Starting: func() { b.channelManager.MarkStarting("email") },
|
|
Running: func() {
|
|
b.channelManager.MarkRunning("email")
|
|
log.Info("email gateway started", "addr", cfg.Chat.Email.ListenAddr)
|
|
},
|
|
}
|
|
if err := em.Start(ctx, emRouter, lifecycle); err != nil && ctx.Err() == nil {
|
|
b.channelManager.MarkFailed("email", err.Error())
|
|
log.Error("email gateway stopped", "err", err)
|
|
}
|
|
}()
|
|
})
|
|
return true
|
|
}
|
|
|
|
// setupWebhookGateway registers the optional inbound webhook gateway when
|
|
// chat.webhook_addr is configured. It returns false when the configured
|
|
// gateway fails its own ConfigSchema validation, which the caller treats
|
|
// as a fatal boot error -- the same convention setupTelegramGateway
|
|
// already uses.
|
|
func (b *boot) setupWebhookGateway(ctx context.Context, cfg config.Config, log *slog.Logger) bool {
|
|
if cfg.Chat.WebhookAddr == "" {
|
|
return true
|
|
}
|
|
host, port := parseListenAddr(cfg.Chat.WebhookAddr, "0.0.0.0", 8644)
|
|
secretValue, err := b.secrets.Resolve(cfg.Chat.Webhook.Secret)
|
|
if err != nil {
|
|
log.Error("webhook gateway secret unresolvable; starting with signature validation disabled",
|
|
"engine", cfg.Chat.Webhook.Secret.Engine, "key", cfg.Chat.Webhook.Secret.Key, "err", err)
|
|
}
|
|
wh := webhook.New(
|
|
host, port,
|
|
webhookRoutes(cfg.Chat.Webhook, secretValue),
|
|
log,
|
|
)
|
|
if err := wh.ValidateConfig(map[string]any{
|
|
"host": host,
|
|
"port": port,
|
|
}); err != nil {
|
|
log.Error("chat.webhook config invalid", "err", err)
|
|
return false
|
|
}
|
|
whRouter := gateway.NewRouter(b.stateStore, nil, "webhook")
|
|
whRouter.Limiter = b.rateLimiter
|
|
configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity)
|
|
b.startGateways = append(b.startGateways, func() {
|
|
go func() {
|
|
lifecycle := gateway.Lifecycle{
|
|
Starting: func() { b.channelManager.MarkStarting("webhook") },
|
|
Running: func() {
|
|
b.channelManager.MarkRunning("webhook")
|
|
log.Info("webhook gateway started", "addr", fmt.Sprintf("%s:%d", host, port))
|
|
},
|
|
}
|
|
if err := wh.Start(ctx, whRouter, lifecycle); err != nil && ctx.Err() == nil {
|
|
b.channelManager.MarkFailed("webhook", err.Error())
|
|
log.Error("webhook gateway stopped", "err", err)
|
|
}
|
|
}()
|
|
})
|
|
return true
|
|
}
|
|
|
|
// webhookRoutes translates the configured [chat.webhook] route into the
|
|
// gateway's RouteConfig. It is the composition site config.WebhookRoute
|
|
// exists for: the HMAC/template/deliver-to fields have been implemented and
|
|
// tested in internal/channels/webhook since it was written, reachable only
|
|
// through this translation.
|
|
func webhookRoutes(route config.WebhookRoute, secretValue string) []webhook.RouteConfig {
|
|
path := route.Path
|
|
if path == "" {
|
|
path = "/webhook"
|
|
}
|
|
return []webhook.RouteConfig{{
|
|
Path: path,
|
|
Secret: secretValue,
|
|
Template: route.Template,
|
|
DeliverTo: route.DeliverTo,
|
|
}}
|
|
}
|
|
|
|
// loadWorkflows builds the workflow registry from the skill catalog.
|
|
// Plugin-defined workflows override built-ins of the same name;
|
|
// built-ins fill gaps.
|
|
func (b *boot) loadWorkflows(ctx context.Context) error {
|
|
cfg, log := b.cfg, b.log
|
|
if err := b.loadWorkflowRouting(cfg, log); err != nil {
|
|
return err
|
|
}
|
|
if err := b.loadModules(cfg, log); err != nil {
|
|
return err
|
|
}
|
|
if err := b.loadEDAPlaybooks(cfg, log); err != nil {
|
|
return err
|
|
}
|
|
|
|
skillsBase := cfg.SkillsDir
|
|
if skillsBase == "" {
|
|
skillsBase = cfg.WorkDir
|
|
}
|
|
workflowCatalog, err := skillbuild.BuildCatalog(skillsBase)
|
|
if err != nil {
|
|
log.Error("skill registry build failed", "err", err)
|
|
return err
|
|
}
|
|
log.Info("workflow registry built", "workflows", len(workflowCatalog.Registry))
|
|
// The dashboard is served by the archie-ui process from the cutover
|
|
// change (archie-core-8cda.5.4, PRD gate 7): the daemon runs no webui
|
|
// listener, and [web].listen is the archie-ui process's bind address.
|
|
// The daemon still renders the configuration snapshot via the
|
|
// standalone webui.BuildConfigView, not through a Server.
|
|
return nil
|
|
}
|
|
|
|
// loadWorkflowRouting loads the kind/label -> workflow-name bindings from
|
|
// the single-file fields and the playbook directories, merges them, and
|
|
// installs the result via workflow.SetKindWorkflows/SetLabelWorkflows.
|
|
// Split out of loadWorkflows (t2db.16) to keep the top-level function's
|
|
// complexity within the lint gate -- pure extraction, same log messages
|
|
// and error-return order as before.
|
|
func (b *boot) loadWorkflowRouting(cfg config.Config, log *slog.Logger) error {
|
|
kindWorkflows, err := workflow.LoadKindWorkflowsYAML(cfg.WorkflowRoutingFile)
|
|
if err != nil {
|
|
log.Error("workflow routing file load failed", "path", cfg.WorkflowRoutingFile, "err", err)
|
|
return err
|
|
}
|
|
|
|
labelWorkflows, err := workflow.LoadLabelWorkflowsYAML(cfg.WorkflowLabelsFile)
|
|
if err != nil {
|
|
log.Error("workflow labels file load failed", "path", cfg.WorkflowLabelsFile, "err", err)
|
|
return err
|
|
}
|
|
|
|
dirKindWorkflows, dirLabelWorkflows, err := workflow.LoadPlaybookDirs(cfg.PlaybookDirs)
|
|
if err != nil {
|
|
log.Error("playbook dirs load failed", "dirs", cfg.PlaybookDirs, "err", err)
|
|
return err
|
|
}
|
|
// The directory is an additional input to the single-file fields, not a
|
|
// replacement (t2db.11). A key bound by both a single-file field and the
|
|
// directory is the same collision the loader enforces inside a directory:
|
|
// reported, never silently arbitrated by source precedence.
|
|
kindWorkflows, err = workflow.MergeKindWorkflows(kindWorkflows, dirKindWorkflows)
|
|
if err != nil {
|
|
log.Error("workflow binding collision between routing file and playbook dir", "err", err)
|
|
return err
|
|
}
|
|
labelWorkflows, err = workflow.MergeLabelWorkflows(labelWorkflows, dirLabelWorkflows)
|
|
if err != nil {
|
|
log.Error("workflow binding collision between labels file and playbook dir", "err", err)
|
|
return err
|
|
}
|
|
|
|
workflow.SetKindWorkflows(kindWorkflows)
|
|
workflow.SetLabelWorkflows(labelWorkflows)
|
|
b.kindWorkflows = kindWorkflows
|
|
b.labelWorkflows = labelWorkflows
|
|
return nil
|
|
}
|
|
|
|
// loadModules builds the daemon's Module registry (EDA playbook Module
|
|
// position, t2db.13): operator-trusted, in-process, Yaegi-interpreted. A
|
|
// broken module is a startup failure -- the daemon does not start with a
|
|
// partial module set, matching the routing-file load pattern (not the
|
|
// degrade-and-skip plugin pattern). Kinds whose file is not present in the
|
|
// directory are simply not loaded. Split out of loadWorkflows (t2db.16);
|
|
// pure extraction, same log messages and error-return order as before.
|
|
func (b *boot) loadModules(cfg config.Config, log *slog.Logger) error {
|
|
b.modules = module.New()
|
|
if cfg.ModuleDir == "" {
|
|
return nil
|
|
}
|
|
for _, kind := range module.Kinds() {
|
|
path := filepath.Join(cfg.ModuleDir, kind+".go")
|
|
if _, err := os.Stat(path); err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
continue // kind not installed in this module dir
|
|
}
|
|
log.Error("module stat failed", "kind", kind, "path", path, "err", err)
|
|
return err
|
|
}
|
|
if err := b.modules.Register(kind, cfg.ModuleDir); err != nil {
|
|
log.Error("module load failed", "kind", kind, "dir", cfg.ModuleDir, "err", err)
|
|
return err
|
|
}
|
|
}
|
|
log.Info("module registry built", "dir", cfg.ModuleDir, "kinds", b.modules.Len())
|
|
return nil
|
|
}
|
|
|
|
// loadEDAPlaybooks loads the EDA playbook documents (t2db.15): trigger +
|
|
// single workflow-kind action with CEL when conditions. Loaded at startup
|
|
// with the same reject-at-load rule -- any malformed playbook,
|
|
// multi-action playbook, non-workflow position, or when compile failure
|
|
// aborts startup, matching the routing-file load pattern (not
|
|
// degrade-and-skip). A nonexistent dir is an empty store. Split out of
|
|
// loadWorkflows (t2db.16); pure extraction, same log messages as before.
|
|
func (b *boot) loadEDAPlaybooks(cfg config.Config, log *slog.Logger) error {
|
|
var err error
|
|
b.playbooks, err = playbook.Load(cfg.EDAPlaybookDir)
|
|
if err != nil {
|
|
log.Error("eda playbook load failed", "dir", cfg.EDAPlaybookDir, "err", err)
|
|
return err
|
|
}
|
|
log.Info("eda playbooks loaded", "dir", cfg.EDAPlaybookDir, "playbooks", len(b.playbooks.Playbooks))
|
|
return nil
|
|
}
|
|
|
|
// loadPlugins loads daemon plugins from the configured plugin directory
|
|
// (Layer 2). Failed plugins are skipped -- the daemon starts with the
|
|
// remaining set.
|
|
func (b *boot) loadPlugins() error {
|
|
cfg, log := b.cfg, b.log
|
|
b.capabilityHost = plugin.NewHost()
|
|
if cfg.PluginDir == "" {
|
|
return nil
|
|
}
|
|
plugins, err := plugin.LoadDir(cfg.PluginDir, pluginextract.Symbols)
|
|
if err != nil {
|
|
log.Error("plugin load failed", "dir", cfg.PluginDir, "err", err)
|
|
return err
|
|
}
|
|
for _, p := range plugins {
|
|
name, version := safePluginInfo(p)
|
|
module, err := plugin.AdaptLegacy(p)
|
|
if err != nil {
|
|
log.Warn("daemon plugin capability registration skipped", "name", name, "version", version, "err", err)
|
|
continue
|
|
}
|
|
if err := b.capabilityHost.Register(module); err != nil {
|
|
log.Warn("daemon plugin capability registration skipped", "name", name, "version", version, "err", err)
|
|
continue
|
|
}
|
|
log.Info("daemon plugin loaded", "name", name, "version", version)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// buildTreesAndIdentities composes the worktree manager and the
|
|
// multi-identity runners. Each configured identity gets its own forge
|
|
// client (its own token, possibly its own forge type/host) and its own
|
|
// worktree manager (a distinct WorkDir so concurrent identities never
|
|
// collide on the same clone). When cfg.Identities is empty,
|
|
// Daemon.Identities stays nil and Run() takes the single-identity path
|
|
// unchanged.
|
|
func (b *boot) buildTreesAndIdentities(ctx context.Context) error {
|
|
cfg, log := b.cfg, b.log
|
|
b.trees = &worktree.Manager{
|
|
WorkDir: cfg.WorkDir,
|
|
Token: b.token,
|
|
BotUser: cfg.BotUser,
|
|
BotEmail: cfg.BotEmail,
|
|
BaseURL: cfg.Forge.Host,
|
|
}
|
|
|
|
for _, idCfg := range cfg.Identities {
|
|
// Same reasoning as the primary forge: one identity whose credential is
|
|
// missing must not deny every other identity, and every other
|
|
// subsystem, the ability to run.
|
|
idForge, idToken := resolveForge(idCfg.Forge, b.secrets, log.With("identity", idCfg.Name))
|
|
idTrees := &worktree.Manager{
|
|
WorkDir: filepath.Join(cfg.WorkDir, "identity-"+idCfg.Name),
|
|
Token: idToken,
|
|
BotUser: idCfg.BotUser,
|
|
BotEmail: idCfg.BotEmail,
|
|
BaseURL: idCfg.Forge.Host,
|
|
}
|
|
runner, err := daemon.NewIdentityRunner(ctx, idCfg, idForge, idTrees, log)
|
|
if err != nil {
|
|
log.Error("identity construction failed", "identity", idCfg.Name, "err", err)
|
|
return err
|
|
}
|
|
b.identityRunners = append(b.identityRunners, runner)
|
|
log.Info("identity configured", "identity", idCfg.Name, "bot_user", idCfg.BotUser, "repos", len(idCfg.Repos))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// registerNATSRPC lets archie-agent containers (which hold no DB
|
|
// connection, forge token, or push credential) proxy Store/Forge/
|
|
// worktree operations back to archied over NATS.
|
|
func (b *boot) registerNATSRPC() error {
|
|
log := b.log
|
|
if b.natsClient == nil {
|
|
return nil
|
|
}
|
|
coreConn, err := b.natsClient.CoreConn()
|
|
if err != nil {
|
|
log.Error("nats connection unavailable for task RPC", "err", err)
|
|
return err
|
|
}
|
|
b.worktreeGrants = worktreerpc.NewGrants()
|
|
unsubscribe, err := registerTaskRPCServers(coreConn, b.forgeClient, b.trees, b.identityRunners, b.worktreeGrants, log)
|
|
if err != nil {
|
|
log.Error("task RPC server registration failed", "err", err)
|
|
return err
|
|
}
|
|
b.addCleanup(unsubscribe)
|
|
|
|
unsubscribeSystemLogs, err := subscribeSystemLogs(coreConn, b.taskLogs, log)
|
|
if err != nil {
|
|
log.Error("system log subscribe failed", "err", err)
|
|
return err
|
|
}
|
|
b.addCleanup(unsubscribeSystemLogs)
|
|
|
|
unsubscribeAgentEvents, err := subscribeAgentEvents(coreConn, b.bus, log)
|
|
if err != nil {
|
|
log.Error("agent event subscribe failed", "err", err)
|
|
return err
|
|
}
|
|
b.addCleanup(unsubscribeAgentEvents)
|
|
|
|
// The standalone Gateway forwards operator task actions to the daemon
|
|
// over NATS. The daemon owns execution cancellation, retry policy,
|
|
// forge closure and event persistence; the responder applies the same
|
|
// taskactions.Service the dashboard uses.
|
|
unsubscribeTaskActions, err := taskactions.Register(coreConn, b.taskActions(), log)
|
|
if err != nil {
|
|
log.Error("gateway task action register failed", "err", err)
|
|
return err
|
|
}
|
|
b.addCleanup(unsubscribeTaskActions)
|
|
return nil
|
|
}
|
|
|
|
// setupMemory starts the memory manager. The built-in file-backed
|
|
// provider (MEMORY.md + USER.md) lives under the daemon work directory.
|
|
// memory.Manager.RegisterExternal exists for an external provider, but
|
|
// nothing calls it here: no external MemoryProvider is implemented
|
|
// anywhere in the repo to construct and register, and
|
|
// configuration.validateMemory now rejects cfg.Memory.Provider being set
|
|
// at all rather than silently accepting a value with no effect
|
|
// (archie-core-1786637499161-356-e424e40d.1).
|
|
func (b *boot) setupMemory() error {
|
|
cfg, log := b.cfg, b.log
|
|
memProvider, memDir := memoryProvider(cfg.WorkDir, log)
|
|
if memProvider == nil {
|
|
log.Error("memory provider init failed", "dir", memDir)
|
|
return fmt.Errorf("memory provider init failed in %s", memDir)
|
|
}
|
|
memManager, err := memory.NewManager(memProvider, nil)
|
|
if err != nil {
|
|
log.Error("memory manager init failed", "err", err)
|
|
return err
|
|
}
|
|
// Install the scanner the HandleToolCall gate reads. Without this the gate
|
|
// returns ThreatNone on every write (manager.ScanContent short-circuits when
|
|
// scanner is nil), so configuration.md's "Memory safety scanner" row would
|
|
// describe a control that never runs. The consequences stay at the
|
|
// documented defaults: scanReject is false, so a prompt-injection match is
|
|
// downgraded to a warn and logged rather than refusing the operator's write.
|
|
memManager.SetScanner(&memory.DefaultScanner{})
|
|
b.memManager = memManager
|
|
|
|
if err := memManager.Initialize("daemon"); err != nil {
|
|
log.Warn("memory manager initialize", "err", err)
|
|
}
|
|
mm := memManager
|
|
b.addCleanup(func() {
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := mm.ShutdownContext(shutdownCtx); err != nil {
|
|
log.Error("memory manager shutdown", "err", err)
|
|
}
|
|
})
|
|
log.Info("memory manager started", "dir", memDir)
|
|
return nil
|
|
}
|
|
|
|
// setupMemoryEngine wires the domain/memory engine family
|
|
// (archie-core-1786637499161-356-e424e40d), separate from and not yet
|
|
// consulted by the legacy memory manager set up in setupMemory above.
|
|
// cfg.Memory.Engine is validated at config load
|
|
// (configuration.validateMemory) against the same set this switch covers,
|
|
// so an unrecognised value cannot reach here -- this only guards against
|
|
// the two lists drifting apart.
|
|
//
|
|
// Rooted at workDir/memory-engine, a directory separate from the legacy
|
|
// provider's workDir/memory, so the two paths can never collide on the
|
|
// same files while both exist.
|
|
func (b *boot) setupMemoryEngine() error {
|
|
cfg, log := b.cfg, b.log
|
|
// The logger is what carries an engine's scanner warning: warn allows
|
|
// the write, so the log line is the whole audit trail for it.
|
|
registry := domainmemory.NewRegistry(domainmemory.Registrar{Log: log})
|
|
|
|
switch cfg.Memory.Engine {
|
|
case infraMemory.EngineName, "":
|
|
root := filepath.Join(cfg.WorkDir, "memory-engine")
|
|
if err := registry.Register(infraMemory.NewBuiltinEngine(root, 0)); err != nil {
|
|
return fmt.Errorf("register memory engine %q: %w", infraMemory.EngineName, err)
|
|
}
|
|
default:
|
|
return fmt.Errorf("memory.engine %q has no registered implementation", cfg.Memory.Engine)
|
|
}
|
|
|
|
// Not b's own ctx: matches setupMemory's identical choice just above,
|
|
// and Start here is a synchronous startup step, not a subscription
|
|
// that needs to live and later be cancelled with the daemon's context
|
|
// the way setupCurators' WakeOnPrimaryInput does.
|
|
if err := registry.Start(context.Background()); err != nil {
|
|
return fmt.Errorf("start memory engine registry: %w", err)
|
|
}
|
|
b.memEngines = registry
|
|
b.addCleanup(func() {
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := registry.Stop(shutdownCtx); err != nil {
|
|
log.Error("memory engine registry shutdown", "err", err)
|
|
}
|
|
})
|
|
log.Info("memory engine started", "engine", cfg.Memory.Engine)
|
|
return nil
|
|
}
|
|
|
|
// setupMemoryAll runs both memory setup phases -- the legacy manager and
|
|
// the new engine family -- as one step. Kept as a single call from Run()
|
|
// rather than two: they are always run together and Run() is already at
|
|
// its cyclomatic complexity budget.
|
|
func (b *boot) setupMemoryAll() error {
|
|
if err := b.setupMemory(); err != nil {
|
|
return err
|
|
}
|
|
return b.setupMemoryEngine()
|
|
}
|
|
|
|
// setupCurators wires the curator engine family. The registry owns
|
|
// curator registration, lifecycle and shutdown ordering. The runtime
|
|
// loop (archie-core-89x) and the reference curators (archie-core-i7i,
|
|
// gs8) are wired by their issues; until then the registry is empty and
|
|
// Stop is a no-op. The event sink rides the in-process bus, whose
|
|
// bounded dropping per-subscriber buffers guarantee curator activity can
|
|
// never backpressure the daemon or a chat turn.
|
|
func (b *boot) setupCurators(ctx context.Context) {
|
|
log := b.log.With("component", "curator")
|
|
// Skills root matches loadWorkflows' own resolution of skillsBase --
|
|
// the skill curator maintains the same local skills this daemon
|
|
// already treats as authoritative, not the full multi-root catalog a
|
|
// chat turn reads. See docs/prds/skill-curator.md.
|
|
skillsRoot := b.cfg.SkillsDir
|
|
if skillsRoot == "" {
|
|
skillsRoot = b.cfg.WorkDir
|
|
}
|
|
b.curatorRegistry = curator.NewRegistry(curator.Registrar{
|
|
Log: log.With("component", "curator"),
|
|
Events: curatorEventSink{b.bus},
|
|
// b.memEngines (*domainmemory.Registry) satisfies
|
|
// curator.MemoryEngineSource's Get(name) signature directly, no
|
|
// adapter needed. Set by setupMemoryAll, which Run() calls before
|
|
// setupCurators.
|
|
MemoryEngines: b.memEngines,
|
|
// Skills is a shared host service like Events/MemoryEngines --
|
|
// registry.filter narrows it out of the view for any curator that
|
|
// doesn't declare Manifest.Skills, per curator not per instance.
|
|
Skills: skillcurator.NewStore(skillsRoot),
|
|
// Conversations backs the session-memory curator; b.chatSessionStore
|
|
// is opened by openChatSessions before setupCurators.
|
|
Conversations: sessioncurator.NewAdapter(b.chatSessionStore),
|
|
LLM: curatorLLMRunner{rt: b.llm},
|
|
// b.chatModels.ActiveModel() is the same source sendChatTurn uses
|
|
// for a real chat turn (telegram_setup.go) -- not
|
|
// b.defaultChatIdentity, which names a task-routing identity, not
|
|
// a model reference.
|
|
Model: b.chatModels.ActiveModel(),
|
|
})
|
|
if err := b.curatorRegistry.Register(skillcurator.New(skillcurator.DefaultInterval)); err != nil {
|
|
log.Error("skill curator registration failed", "err", err)
|
|
}
|
|
if err := b.curatorRegistry.Register(sessioncurator.New(sessioncurator.DefaultInterval, infraMemory.EngineName)); err != nil {
|
|
log.Error("session-memory curator registration failed", "err", err)
|
|
}
|
|
// The runtime owns the per-curator loops (archie-core-89x): one
|
|
// goroutine per curator, wake nudges, per-pass budgets, panic
|
|
// recovery, bounded shutdown. Stop order at shutdown: runtime first
|
|
// (cancels in-flight passes, then stops curator lifecycle), then the
|
|
// registry's own Stop below, which is a no-op by then.
|
|
b.curatorRuntime = curator.NewRuntime(b.curatorRegistry, curator.RuntimeConfig{})
|
|
// Primary chat turns wake input-driven curators (archie-core-035):
|
|
// the forwarder consumes only primary-input kinds, and curator
|
|
// output never produces them, so derived work cannot feed its own
|
|
// trigger. The subscriber buffer is bounded and dropping — a slow
|
|
// curator can never backpressure the chat publisher.
|
|
curator.WakeOnPrimaryInput(ctx, b.bus, b.curatorRuntime, events.KindTurnCompleted)
|
|
rt := b.curatorRuntime
|
|
b.addCleanup(shutdownCuratorRuntime(rt, log))
|
|
reg := b.curatorRegistry
|
|
b.addCleanup(shutdownCuratorRegistry(reg, log))
|
|
}
|
|
|
|
func (b *boot) setupGuardrails() {
|
|
log := b.log
|
|
gc := tools.DefaultGuardrailConfig()
|
|
b.guardrails = tools.NewGuardrailEngine(gc)
|
|
log.Info(
|
|
"guardrail engine enabled",
|
|
"exact_failure_warn", gc.ExactFailureWarnAfter,
|
|
"same_tool_failure_warn", gc.SameToolFailureWarnAfter,
|
|
"no_progress_warn", gc.NoProgressWarnAfter,
|
|
)
|
|
}
|
|
|
|
// workspaceIndex builds the optional codesearch index that narrows grep's
|
|
// candidate files. It returns a slice so a failure is simply "no index"
|
|
// rather than a typed-nil reaching the provider.
|
|
//
|
|
// Every failure degrades instead of propagating: grep falls back to its
|
|
// direct walk, which is what it did before this was wired at all. An
|
|
// accelerator that can refuse to build must never stop the daemon starting.
|
|
//
|
|
// The context is detached from cancellation but keeps its values, because
|
|
// NewManager starts an asynchronous build whose lifetime is the process
|
|
// while the gateway path's boot context is cancelled once startup returns.
|
|
// Inheriting cancellation would kill the build mid-flight and leave grep
|
|
// permanently unindexed with nothing to show why; using Background instead
|
|
// would silently drop any values the caller's context carries.
|
|
func (b *boot) workspaceIndex(ctx context.Context, workspace string) []toolsbuiltin.GrepIndex {
|
|
cfg, log := b.cfg, b.log
|
|
idxCfg := cfg.Indexing.WithDefaults(cfg.WorkDir)
|
|
manager, err := indexing.NewManager(
|
|
context.WithoutCancel(ctx),
|
|
indexing.Config{IndexDir: idxCfg.IndexDir, DBPath: idxCfg.DBPath, Log: log},
|
|
workspace,
|
|
)
|
|
if err != nil {
|
|
log.Warn("workspace index unavailable; grep will search unindexed",
|
|
"workspace", workspace, "index_dir", idxCfg.IndexDir, "error", err)
|
|
return nil
|
|
}
|
|
log.Info("workspace index enabled", "workspace", workspace, "index_dir", idxCfg.IndexDir)
|
|
return []toolsbuiltin.GrepIndex{manager}
|
|
}
|
|
|
|
// registerTools registers the tool providers with a lifecycle: memory,
|
|
// workspace file/shell tools and optional MCP servers.
|
|
func (b *boot) registerTools(ctx context.Context) error {
|
|
cfg, log := b.cfg, b.log
|
|
b.providerRegistry = toolprovider.NewRegistry(b.toolReg)
|
|
if err := b.providerRegistry.Register(memorytoolprovider.New(b.memManager)); err != nil {
|
|
log.Error("memory tool provider registration failed", "err", err)
|
|
return err
|
|
}
|
|
// Workspace file and shell tools. Registered only when a workspace is
|
|
// configured: these read, write and execute, so the directory is a
|
|
// deliberate choice rather than a default.
|
|
if workspace := cfg.Chat.Workspace; workspace != "" {
|
|
unrestricted := cfg.Chat.UnrestrictedFilesystem
|
|
index := b.workspaceIndex(ctx, workspace)
|
|
if err := b.providerRegistry.Register(builtintoolprovider.New(workspace, unrestricted, index...)); err != nil {
|
|
log.Error("workspace tool provider registration failed", "err", err)
|
|
return err
|
|
}
|
|
// Logged at the same level either way: which of the two postures is
|
|
// running is the first thing worth knowing when a file tool refuses a
|
|
// path, or reaches one it should not have.
|
|
log.Info("workspace tools enabled",
|
|
"workspace", workspace, "unrestricted_filesystem", unrestricted)
|
|
} else {
|
|
log.Info("workspace tools disabled (chat.workspace is unset)")
|
|
}
|
|
for _, srv := range cfg.Tools.MCPServers {
|
|
provider, err := configuredMCPProvider(srv, cfg.WorkDir)
|
|
if err != nil {
|
|
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
|
continue
|
|
}
|
|
// Optional: an MCP server is a third-party process pulled in at
|
|
// runtime, and it is exactly the category that is allowed to be
|
|
// absent. Registering it as required meant one failing npm package
|
|
// unregistered every builtin tool and exited the daemon, which under
|
|
// Restart=on-failure is a crash loop that takes chat and the gateway
|
|
// with it.
|
|
if err := b.providerRegistry.RegisterOptional(provider); err != nil {
|
|
log.Warn("mcp tool provider skipped", "name", srv.Name, "err", err)
|
|
continue
|
|
}
|
|
}
|
|
if err := b.capabilityHost.Register(b.providerRegistry); err != nil {
|
|
log.Error("tool-provider capability registration failed", "err", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// registerStandaloneTools registers the tools with no provider
|
|
// lifecycle: the skill catalog activator, the spill directory check and
|
|
// web_fetch.
|
|
func (b *boot) registerStandaloneTools() {
|
|
cfg, log := b.cfg, b.log
|
|
// Skill catalog → skill_activate tool (progressive disclosure: the
|
|
// catalog's name+description is always in the tool schema; the full
|
|
// SKILL.md body loads only when the model activates one).
|
|
if catalog, err := skill.CatalogRoots(skill.DefaultRoots(cfg.WorkDir, cfg.SkillsDir)...); err != nil {
|
|
log.Warn("skill catalog load failed", "err", err)
|
|
} else if entry := skill.ActivateTool(cfg.WorkDir, catalog); entry != nil {
|
|
if err := b.toolReg.Register(*entry); err != nil {
|
|
log.Warn("skill_activate registration failed", "err", err)
|
|
} else {
|
|
log.Info("skill catalog registered", "skills", len(catalog))
|
|
}
|
|
}
|
|
|
|
// Tool results too large to inline are written here. Created once, at
|
|
// startup: the write failure inside a turn is silent, so without this
|
|
// every oversized result would quietly fall back to truncation and
|
|
// spilling would look configured while doing nothing.
|
|
if spillDir := cfg.Tools.Policy.SpillDir; spillDir != "" {
|
|
if err := toolLimits(cfg).EnsureSpillDir(); err != nil {
|
|
log.Warn("tool spill directory unavailable; large results will be truncated instead", "err", err)
|
|
} else if ws := cfg.Chat.Workspace; ws != "" && !cfg.Chat.UnrestrictedFilesystem && !isWithin(ws, spillDir) {
|
|
// A spill hands the model a path to read back, and a confined read
|
|
// tool refuses anything outside the workspace. Outside it, the
|
|
// spill reference is a dead end and the result is lost rather than
|
|
// displaced -- worse than truncating in the first place.
|
|
//
|
|
// Not a concern when the filesystem is unrestricted: the read tool
|
|
// can reach the spill wherever it lives.
|
|
log.Warn("tool spill directory is outside chat.workspace; the model cannot read back what is spilled there",
|
|
"spill_dir", spillDir, "workspace", ws)
|
|
}
|
|
}
|
|
|
|
// web_fetch. Registered directly rather than as a tool provider: it has
|
|
// no process to start or stop, so the provider lifecycle would buy
|
|
// nothing. Disabled by configuration returns nil and advertises nothing.
|
|
if entry := webfetch.Tool(webfetch.Config{
|
|
Enabled: cfg.Tools.WebFetch.IsEnabled(),
|
|
Timeout: cfg.Tools.WebFetch.Timeout.Std(),
|
|
MaxBytes: cfg.Tools.WebFetch.MaxBytes,
|
|
AllowPrivateNetworks: cfg.Tools.WebFetch.AllowPrivateNetworks,
|
|
}); entry != nil {
|
|
if err := b.toolReg.Register(*entry); err != nil {
|
|
log.Warn("web_fetch registration failed", "err", err)
|
|
} else {
|
|
log.Info("web fetch enabled",
|
|
"allow_private_networks", cfg.Tools.WebFetch.AllowPrivateNetworks)
|
|
}
|
|
} else {
|
|
log.Info("web fetch disabled")
|
|
}
|
|
|
|
// send_file. Rooted at the same workspace as the file tools and gated
|
|
// by the same confinement, because it hands a host file to an outbound
|
|
// message: a send that could reach paths the read tool refuses would
|
|
// be a way around whatever confinement is configured.
|
|
if entry := sendfile.Tool(cfg.Chat.Workspace); entry != nil {
|
|
if err := b.toolReg.Register(*entry); err != nil {
|
|
log.Warn("send_file registration failed", "err", err)
|
|
} else {
|
|
log.Info("file sending enabled", "workspace", cfg.Chat.Workspace)
|
|
}
|
|
} else {
|
|
log.Info("file sending disabled (chat.workspace is unset)")
|
|
}
|
|
|
|
b.registerMinimaxTool(cfg, log)
|
|
}
|
|
|
|
// registerMinimaxTool registers generate_video. Off by default -- see
|
|
// MinimaxConfig's doc comment, it spends real API credits per call -- and,
|
|
// unlike web_fetch, needs a resolved credential before it can be
|
|
// registered at all: a tool advertised with no working key would fail
|
|
// every call, the same "don't advertise a broken capability" reasoning as
|
|
// disabled-by-config. Split out of registerStandaloneTools to keep that
|
|
// function's branching flat rather than nesting this tool's three failure
|
|
// modes (resolve error, empty key, registration error) inside it.
|
|
func (b *boot) registerMinimaxTool(cfg config.Config, log *slog.Logger) {
|
|
if !cfg.Tools.Minimax.IsEnabled() {
|
|
log.Info("minimax video generation disabled")
|
|
return
|
|
}
|
|
|
|
apiKey, err := b.secrets.Resolve(cfg.Tools.Minimax.APIKey)
|
|
if err != nil {
|
|
log.Warn("minimax video generation enabled but the API key failed to resolve; tool not registered", "err", err)
|
|
return
|
|
}
|
|
if apiKey == "" {
|
|
log.Warn("minimax video generation enabled but no API key is configured; tool not registered")
|
|
return
|
|
}
|
|
|
|
entry := minimax.Tool(minimax.Config{Enabled: true, APIKey: apiKey, BaseURL: cfg.Tools.Minimax.BaseURL})
|
|
if entry == nil {
|
|
return
|
|
}
|
|
if err := b.toolReg.Register(*entry); err != nil {
|
|
log.Warn("generate_video registration failed", "err", err)
|
|
return
|
|
}
|
|
log.Info("minimax video generation enabled")
|
|
}
|
|
|
|
// buildDaemon constructs the daemon and hands the dashboard the handles
|
|
// it shares with it.
|
|
func (b *boot) buildDaemon() {
|
|
log := b.log
|
|
b.d = &daemon.Daemon{
|
|
Cfg: b.cfgHolder,
|
|
ConnectedNATS: daemon.NATSEndpoint{URL: b.natsURL, Token: b.natsToken},
|
|
ConnectedStateStore: daemon.StateStoreEndpoint{URL: strings.TrimSpace(b.cfg.Services.Get(config.ServiceNameState).Target), Token: b.stateStoreToken},
|
|
Store: b.stateStore,
|
|
Bus: b.bus,
|
|
Forge: b.forgeClient,
|
|
Trees: b.trees,
|
|
CapabilityHost: b.capabilityHost,
|
|
Storage: b.storeBackend,
|
|
Log: log,
|
|
Tasks: b.natsClient,
|
|
WorktreeGrants: b.worktreeGrants,
|
|
StateStoreGrants: b.stateStoreGrants,
|
|
ContainerPool: b.containerPool,
|
|
Guardrails: b.guardrails,
|
|
ToolRegistry: b.toolReg,
|
|
Identities: b.identityRunners,
|
|
TaskLogs: b.taskLogs,
|
|
AgentStatus: b.agentStatus,
|
|
KindWorkflows: b.kindWorkflows,
|
|
LabelWorkflows: b.labelWorkflows,
|
|
}
|
|
// Consumer mapping/binding surfaces resolve from b.stateStore (the State
|
|
// Store contract adapter): local by default, remote *staterpc.Client when
|
|
// [services.state].target is set. See docs/prds/state-store-contract.md §10.
|
|
if ms, ok := b.stateStore.(storecontract.MappingStore); ok {
|
|
b.d.Mappings = ms
|
|
}
|
|
if bs, ok := b.stateStore.(storecontract.BindingStore); ok {
|
|
b.d.Bindings = bs
|
|
}
|
|
if bd, ok := b.stateStore.(storecontract.BindingDispatcher); ok {
|
|
b.d.BindingDispatcher = bd
|
|
}
|
|
if btc, ok := b.stateStore.(storecontract.BindingTaskCreator); ok {
|
|
b.d.BindingTaskCreator = btc
|
|
}
|
|
b.setupForgeWebhook()
|
|
}
|
|
|
|
// setupForgeWebhook starts the forge webhook receiver when intake is "webhook"
|
|
// or "both". It decodes GitHub issue events into task envelopes and publishes
|
|
// them through the same path the poller uses, so a labelled or assigned issue
|
|
// becomes work immediately instead of on the next poll. Called from
|
|
// buildDaemon, since it needs the freshly built (*daemon.Daemon).PublishTask.
|
|
//
|
|
// A secret that fails to resolve degrades rather than aborts boot, same
|
|
// reasoning as resolveForge: under intake="both" the poll is the deployment's
|
|
// explicit backstop for exactly this kind of misconfiguration (CLAUDE.md's
|
|
// "polling has to remain a first-class option, not a legacy fallback"), so a
|
|
// typo'd env var name must not take the whole daemon down and silently stop
|
|
// polling too. Config validation already requires webhook_secret to be a
|
|
// configured reference before intake="webhook"/"both" is accepted; this
|
|
// handles the reference resolving to nothing at runtime.
|
|
//
|
|
// Single-identity only: the receiver has one dispatch config (root
|
|
// Dispatch.Trigger/Label/BotUser), so a multi-identity deployment's
|
|
// per-identity trigger/label/bot user cannot be matched correctly. Rather
|
|
// than run with the wrong identity's rules and silently misclassify or drop
|
|
// events for identity-scoped repos, webhook intake refuses to start when
|
|
// [[identities]] is configured; those repos keep working via their own
|
|
// per-identity poll loop, unaffected. Extending this to multi-identity is a
|
|
// separate feature, not a bug in this one.
|
|
func (b *boot) setupForgeWebhook() {
|
|
cfg, log := b.cfg, b.log
|
|
if cfg.Forge.Intake != config.ForgeIntakeWebhook && cfg.Forge.Intake != config.ForgeIntakeBoth {
|
|
return
|
|
}
|
|
if len(cfg.Identities) > 0 {
|
|
log.Error("forge webhook disabled: multi-identity deployments are not supported yet (each identity's own poll loop is unaffected)")
|
|
return
|
|
}
|
|
secretValue, err := b.secrets.Resolve(cfg.Forge.WebhookSecret)
|
|
if err != nil || secretValue == "" {
|
|
log.Error("forge webhook disabled: secret unavailable",
|
|
"engine", cfg.Forge.WebhookSecret.Engine, "key", cfg.Forge.WebhookSecret.Key, "err", err)
|
|
return
|
|
}
|
|
receiver := forgewebhook.New(secretValue, cfg.Dispatch.Trigger, cfg.Label, cfg.BotUser, b.d.PublishTask, log)
|
|
host, port := parseListenAddr(cfg.Forge.WebhookAddr, "0.0.0.0", 8645)
|
|
addr := fmt.Sprintf("%s:%d", host, port)
|
|
srv := &http.Server{Addr: addr, Handler: receiver}
|
|
|
|
b.startGateways = append(b.startGateways, func() {
|
|
go func() {
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Error("forge webhook server stopped", "err", err)
|
|
}
|
|
}()
|
|
log.Info("forge webhook listening", "addr", addr)
|
|
})
|
|
b.addCleanup(func() {
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_ = srv.Shutdown(shutdownCtx)
|
|
})
|
|
}
|
|
|
|
// publishConfig makes a new configuration the running one and republishes
|
|
// the dashboard's projection from it. Every path that changes configuration
|
|
// goes through here, so the running config and the published page can never
|
|
// diverge. The provenance chain it renders is b.currentProvenance, which the
|
|
// caller stores before publishing.
|
|
func (b *boot) publishConfig(ctx context.Context, cfg config.Config) {
|
|
b.cfgHolder.Set(cfg)
|
|
b.publishConfigSnapshot(ctx)
|
|
}
|
|
|
|
// configOrigins projects the current provenance chain into the dashboard's
|
|
// view type.
|
|
func (b *boot) configOrigins() []webui.ConfigOrigin {
|
|
provenance := b.currentProvenance.Load()
|
|
if provenance == nil {
|
|
return nil
|
|
}
|
|
origins := make([]webui.ConfigOrigin, 0, len(provenance.Origins))
|
|
for _, origin := range provenance.Origins {
|
|
origins = append(origins, webui.ConfigOrigin{
|
|
Path: origin.Path, Role: string(origin.Role), Layer: string(origin.Layer), Feature: string(origin.Feature),
|
|
})
|
|
}
|
|
return origins
|
|
}
|
|
|
|
// configOverrides lists the dotted keys the runtime overlay currently sets,
|
|
// so the dashboard can mark those rows: their file value is shadowed until
|
|
// reset. A disabled overlay store reports no overrides.
|
|
func (b *boot) configOverrides(ctx context.Context) []string {
|
|
if b.overlayStore == nil {
|
|
return nil
|
|
}
|
|
keys, err := b.overlayStore.Keys(ctx)
|
|
if err != nil {
|
|
// A failed read omits the list rather than failing the whole
|
|
// view; the reload status carries the overlay degrade reason.
|
|
return nil
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// configViewInput assembles the dashboard's configuration projection from
|
|
// the daemon's own configuration state. The daemon is the configuration
|
|
// owner, so it is the process that renders this view (archie-core-ml30).
|
|
func (b *boot) configViewInput(ctx context.Context) webui.ConfigViewInput {
|
|
in := webui.ConfigViewInput{
|
|
Config: b.cfgHolder.Get(),
|
|
Provenance: b.configOrigins(),
|
|
Overridden: b.configOverrides(ctx),
|
|
}
|
|
if b.lastReload != nil {
|
|
status := b.lastReload()
|
|
in.Reload = &status
|
|
}
|
|
return in
|
|
}
|
|
|
|
// publishConfigSnapshot sends the dashboard's projection to the State Store,
|
|
// where a UI process reads it. This is the only crossing: the configuration
|
|
// owner publishes what it already renders, and no other process holds the
|
|
// daemon's config.Holder (archie-core-ymut).
|
|
//
|
|
// A failed publish degrades to a stale configuration page and is logged, not
|
|
// propagated: it must never fail the reload or dashboard write that produced
|
|
// the new configuration, both of which have already taken effect.
|
|
func (b *boot) publishConfigSnapshot(ctx context.Context) {
|
|
snapshots, ok := b.stateStore.(storecontract.ConfigSnapshotStore)
|
|
if !ok || b.cfgHolder == nil {
|
|
return
|
|
}
|
|
// Detached from the caller: a dashboard edit's request context is
|
|
// cancelled the moment the browser has its answer, and the write it
|
|
// describes has already taken effect either way.
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
|
defer cancel()
|
|
|
|
document, err := json.Marshal(webui.BuildConfigView(b.configViewInput(ctx)))
|
|
if err != nil {
|
|
b.log.Warn("config snapshot not published", "err", err)
|
|
return
|
|
}
|
|
err = snapshots.PutConfigSnapshot(ctx, storecontract.ConfigSnapshot{
|
|
Schema: webui.ConfigViewSchema,
|
|
Document: document,
|
|
PublishedAt: time.Now().UTC(),
|
|
})
|
|
if err != nil {
|
|
b.log.Warn("config snapshot not published", "err", err)
|
|
}
|
|
}
|
|
|
|
// wireConfigPublishing installs SIGHUP reload and the reload status
|
|
// surface. The reload log warns about changed fields that require a
|
|
// restart, so an operator never has to read the source to find out
|
|
// whether their edit took effect. web.LastReload exposes the outcome to
|
|
// /api/config.
|
|
func (b *boot) wireConfigPublishing(ctx context.Context, cfgPath, overlayPath string) {
|
|
log := b.log
|
|
reloadController := newReloadController(b.loader, cfgPath, overlayPath, func(doc *configuration.Document) {
|
|
// Boot merges catalog-discovered providers into cfg before the
|
|
// Holders are seeded; publishing the raw reloaded document would
|
|
// drop them from the running config even though the file is
|
|
// unchanged. Re-apply the same merge (idempotent: a catalog that
|
|
// failed to load merges to identity).
|
|
applyModelCatalog(&doc.Config, b.catalog)
|
|
old := b.d.Cfg.Get()
|
|
b.currentProvenance.Store(&doc.Provenance)
|
|
b.publishConfig(ctx, doc.Config)
|
|
if fields := changedNonReloadableFields(old, doc.Config); len(fields) > 0 {
|
|
log.Warn("config reloaded; some changes require a restart",
|
|
"fields", fields, "paths", doc.Provenance.Paths())
|
|
} else {
|
|
log.Info("config reloaded", "paths", doc.Provenance.Paths())
|
|
}
|
|
})
|
|
if b.overlayStore != nil {
|
|
reloadController.WithOverlay(func() (map[string]any, error) { return b.overlayStore.Snapshot(ctx) })
|
|
}
|
|
// lastReload merges the reload controller's outcome with the boot-time
|
|
// overlay degrade, so /api/config carries both the last reload result
|
|
// and whether the runtime overlay is in effect at all.
|
|
b.lastReload = func() config.ReloadStatus {
|
|
st := reloadController.Status()
|
|
if p := b.bootOverlayErr.Load(); p != nil {
|
|
st.OverlayUnavailable = *p
|
|
}
|
|
return st
|
|
}
|
|
|
|
reloadCh := make(chan os.Signal, 1)
|
|
signal.Notify(reloadCh, syscall.SIGHUP)
|
|
b.addCleanup(func() { signal.Stop(reloadCh) })
|
|
go reloadLoop(ctx, reloadCh, reloadController, log)
|
|
|
|
// Give /cancel and /stop a handle on work already in flight. The
|
|
// controller is built before the daemon exists, so the runtime is
|
|
// attached here; gateways start further down, after d.Startup, so no
|
|
// command can arrive before this is wired.
|
|
b.chatController.WithRuntime(b.d)
|
|
}
|
|
|
|
// shutdownCapabilityHost returns a cleanup that stops the capability
|
|
// host; same Background reasoning as shutdownCuratorRuntime. Defined
|
|
// before startServices so the Stop-before-Start ordering stays visible
|
|
// in the source layout the contract test reads.
|
|
//
|
|
//nolint:contextcheck
|
|
func shutdownCapabilityHost(capabilityHost *plugin.Host, log *slog.Logger) func() {
|
|
return func() {
|
|
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := capabilityHost.Stop(stopCtx); err != nil {
|
|
log.Error("capability host shutdown", "err", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// startServices starts the capability host, daemon and curator runtime,
|
|
// then launches the gateways. Optional providers that could not start
|
|
// are excluded rather than fatal, so the only way an operator learns
|
|
// about one is this loop's log line.
|
|
func (b *boot) startServices(ctx context.Context) error {
|
|
log := b.log
|
|
capabilityHost := b.capabilityHost
|
|
b.addCleanup(shutdownCapabilityHost(capabilityHost, log))
|
|
if err := b.capabilityHost.Start(ctx); err != nil {
|
|
log.Error("capability host startup", "err", err)
|
|
return err
|
|
}
|
|
for _, skipped := range b.providerRegistry.Skipped() {
|
|
log.Error("tool provider unavailable; archie is running without its tools",
|
|
"provider", skipped.ID, "err", skipped.Err)
|
|
}
|
|
|
|
if err := b.d.Startup(ctx); err != nil {
|
|
log.Error("startup", "err", err)
|
|
return err
|
|
}
|
|
if err := b.curatorRuntime.Start(ctx); err != nil {
|
|
log.Error("curator runtime startup", "err", err)
|
|
}
|
|
if b.schedulingEngine != nil {
|
|
if err := b.schedulingEngine.Start(ctx); err != nil {
|
|
log.Error("scheduling engine startup", "err", err)
|
|
}
|
|
}
|
|
for _, startGateway := range b.startGateways {
|
|
startGateway()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// setupBackends wires the autonomous-workflow backends. Container setup may
|
|
// degrade, but NATS startup must succeed because there is no other task handoff.
|
|
func (b *boot) setupBackends(ctx context.Context) error {
|
|
closeContainers := b.setupContainers(ctx)
|
|
err := b.connectNATS(ctx)
|
|
// Container discovery must precede embedded NATS so it can bind the
|
|
// worker bridge gateway, but cleanup registration comes afterwards.
|
|
// cleanups run LIFO: workers stop first, then the daemon client closes,
|
|
// then the embedded server shuts down.
|
|
b.addCleanup(closeContainers)
|
|
return err
|
|
}
|
|
|
|
// shutdownCuratorRuntime returns a cleanup that stops the curator
|
|
// runtime. The stop path derives its own timeout budget from Background
|
|
// rather than the boot context, which is already cancelled by the time
|
|
// shutdown runs.
|
|
//
|
|
//nolint:contextcheck
|
|
func shutdownCuratorRuntime(rt *curator.Runtime, log *slog.Logger) func() {
|
|
return func() {
|
|
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := rt.Stop(stopCtx); err != nil {
|
|
log.Error("curator runtime shutdown", "err", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// shutdownCuratorRegistry returns a cleanup that stops the curator
|
|
// registry; same Background reasoning as shutdownCuratorRuntime.
|
|
//
|
|
//nolint:contextcheck
|
|
func shutdownCuratorRegistry(reg *curator.Registry, log *slog.Logger) func() {
|
|
return func() {
|
|
stopCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := reg.Stop(stopCtx); err != nil {
|
|
log.Error("curator registry shutdown", "err", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// exitCode maps a bootstrap error to the process exit code.
|
|
func exitCode(err error) int {
|
|
if err != nil {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (b *boot) runLoop(ctx context.Context, once bool) error {
|
|
// Boot is done: the liveness surface stops answering 503, which is what
|
|
// the update watchdog is waiting to see after a restart.
|
|
b.health.markServing()
|
|
if once {
|
|
b.d.Cycle(ctx)
|
|
return nil
|
|
}
|
|
b.log.Info("archied running", "repos", len(b.cfg.Repos), "poll", b.cfg.PollInterval.Std().String(), "label", b.cfg.Label)
|
|
// Drain monitoring only matters while the daemon stays up to be drained;
|
|
// a --once cycle exits immediately, so a drain that arrives mid-cycle is
|
|
// out of scope and the monitor is not started.
|
|
b.startDrainMonitor(ctx)
|
|
if err := b.d.Run(ctx); err != nil && ctx.Err() == nil {
|
|
b.log.Error("daemon exited", "err", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|