From 6edb1cc9a343f675b147d14827a85476e1ce8d1a Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 4 Oct 2026 00:48:28 +1000 Subject: [PATCH] refactor(archied): gateway owns model work; daemon drops chat runtime, tools, memory and curators The gateway now applies provider, model-role and tool settings live and runs the curators, which wake on its turns and read its conversations. --- internal/app/archied/bootstrap.go | 48 +++++---------------- internal/app/archied/control_plane.go | 14 ++++-- internal/app/archied/gateway.go | 9 ++++ internal/app/archied/gateway_runtime.go | 24 ++++++----- internal/app/archied/main.go | 21 ++------- internal/app/archied/readiness.go | 40 ----------------- internal/infrastructure/readiness/probes.go | 41 ------------------ 7 files changed, 47 insertions(+), 150 deletions(-) diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index d528ab1ec..c453b4442 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -39,7 +39,6 @@ import ( "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" "github.com/samcharles93/archie-core/internal/domain/identity" domainmemory "github.com/samcharles93/archie-core/internal/domain/memory" @@ -54,7 +53,6 @@ import ( "github.com/samcharles93/archie-core/internal/gateway" infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" - 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" @@ -240,13 +238,6 @@ type boot struct { // start, which read it as their resume points. runtimeVersions map[string]int64 - // 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 - // transcriber is the optional voice-transcription capability. It is built // beside the chat runtime from this process's own [models]/[providers], // which is the model-owning side of the Messaging boundary: a channel @@ -699,15 +690,6 @@ func (b *boot) setupContainers(ctx context.Context) func() { return closeDocker } -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) - } -} - func (b *boot) setupTranscriber(cfg config.Config, log *slog.Logger) { client, ok := transcription.New(cfg.Models, cfg.Providers, transcription.Options{ ResolveSecret: b.secrets.Resolve, @@ -720,20 +702,16 @@ func (b *boot) setupTranscriber(cfg config.Config, log *slog.Logger) { } } -func (b *boot) setupLLMAndChat(ctx context.Context) error { - cfg, log := b.cfg, b.log - - if err := b.setupChatRuntime(ctx, cfg); err != nil { - return err - } - - b.setupEmbeddings(cfg, log) - contract, cleanup, err := composeChatContract(cfg.Services, b.secrets) +// setupGatewayClient dials the Gateway, which owns every model call. The daemon +// only enqueues the chat tasks scheduled workflows create. +func (b *boot) setupGatewayClient() error { + b.setupChatTasks(b.cfg) + contract, cleanup, err := composeChatContract(b.cfg.Services, b.secrets) if err != nil { return err } b.addCleanup(cleanup) - b.chat = &webui.ChatService{Contract: contract, Updates: b.updateService} + b.chat = &webui.ChatService{Contract: contract} b.setupReadinessProbes() return nil } @@ -1319,7 +1297,10 @@ func (b *boot) setupForgeWebhook() { func (b *boot) publishConfig(ctx context.Context, cfg config.Config) { b.cfgHolder.Set(cfg) - b.publishConfigSnapshot(ctx) + // The daemon owns the published configuration view. + if b.processName != applystatus.Gateway { + b.publishConfigSnapshot(ctx) + } } func (b *boot) configOrigins() []webui.ConfigOrigin { @@ -1386,8 +1367,6 @@ func (b *boot) wireConfigPublishing(ctx context.Context, cfgPath, overlayPath st signal.Notify(reloadCh, syscall.SIGHUP) b.addCleanup(func() { signal.Stop(reloadCh) }) go reloadLoop(ctx, reloadCh, reloadController, log) - - b.chatController.WithRuntime(b.d) } //nolint:contextcheck // shutdown runs after the parent context is cancelled @@ -1409,18 +1388,11 @@ func (b *boot) startServices(ctx context.Context) error { 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) diff --git a/internal/app/archied/control_plane.go b/internal/app/archied/control_plane.go index f5f2fde14..f0216640f 100644 --- a/internal/app/archied/control_plane.go +++ b/internal/app/archied/control_plane.go @@ -251,7 +251,7 @@ func (b *boot) startLiveSettings(ctx context.Context) error { if err := b.startWorkflowExecutionSettings(ctx); err != nil { return fmt.Errorf("workflow execution settings: %w", err) } - if err := b.startRuntimeResourceWatches(ctx, b.runtimeVersions); err != nil { + if err := b.startRuntimeResourceWatches(ctx, b.runtimeVersions, runtimeResourceKinds); err != nil { return fmt.Errorf("runtime resource watches: %w", err) } return nil @@ -292,14 +292,22 @@ var runtimeResourceKinds = []string{ controlplane.ContainerRuntimePoliciesKind, } +// gatewayRuntimeKinds are the kinds the Gateway applies live: the settings its +// chat runtime and tool providers are built from. +var gatewayRuntimeKinds = []string{ + controlplane.ProviderSettingsKind, + controlplane.ModelRoleAssignmentsKind, + controlplane.ToolSettingsKind, +} + // startRuntimeResourceWatches keeps a watch per live kind established for the // life of the process. versions carries the resume point each kind starts // from, the versions boot's layering recorded. The first stream per kind is // opened synchronously, so a control plane that cannot be watched at all // fails the boot that asked for it; after that the watch does not return, it // reconnects (see keepWatch, whose backoff rules these watches ride). -func (b *boot) startRuntimeResourceWatches(ctx context.Context, versions map[string]int64) error { - for _, kind := range runtimeResourceKinds { +func (b *boot) startRuntimeResourceWatches(ctx context.Context, versions map[string]int64, kinds []string) error { + for _, kind := range kinds { updates, err := b.controlPlane.WatchResource(ctx, kind, versions[kind]) if err != nil { return fmt.Errorf("watch %s: %w", kind, err) diff --git a/internal/app/archied/gateway.go b/internal/app/archied/gateway.go index 5aafde9cb..774894a02 100644 --- a/internal/app/archied/gateway.go +++ b/internal/app/archied/gateway.go @@ -187,6 +187,15 @@ func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskAc for _, skipped := range b.providerRegistry.Skipped() { b.log.Warn("gateway tool provider unavailable", "provider", skipped.ID, "err", skipped.Err) } + if err := b.startRuntimeResourceWatches(ctx, b.runtimeVersions, gatewayRuntimeKinds); err != nil { + return nil, fmt.Errorf("runtime resource watches: %w", err) + } + b.startModelCatalogRefresh(ctx) + // Curators wake on this process's turns and read its conversations. + b.setupCurators(ctx) + if err := b.curatorRuntime.Start(ctx); err != nil { + b.log.Error("curator runtime startup", "err", err) + } return contract, nil } diff --git a/internal/app/archied/gateway_runtime.go b/internal/app/archied/gateway_runtime.go index bd2e8aa11..dc6698cd8 100644 --- a/internal/app/archied/gateway_runtime.go +++ b/internal/app/archied/gateway_runtime.go @@ -56,16 +56,7 @@ func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error { b.providerOutcomes = newProviderOutcomeRecorder() b.statusHealth = newStatusHealth(b) - profiles, defaultChatIdentity := chatTaskProfiles(cfg) - var chatTasks gateway.TaskCreator - if len(profiles) > 0 { - chatTasks = gateway.NewStoreTaskCreatorForProfiles( - chatTaskWriterAdapter{enqueue: b.stateStore.EnqueueChatTask}, - profiles, - ) - } - b.chatTasks = chatTasks - b.defaultChatIdentity = defaultChatIdentity + b.setupChatTasks(cfg) var canceller storecontract.ExecutionCanceller if ec, ok := b.stateStore.(storecontract.ExecutionCanceller); ok { canceller = ec @@ -89,6 +80,19 @@ func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error { // cannot be watched at all fails the boot that asked for it rather than // leaving the process running personas it can no longer update; after that the // watch reconnects instead of ending (see keepWatch). +// setupChatTasks wires the task creator chat commands and scheduled workflows +// enqueue through. +func (b *boot) setupChatTasks(cfg config.Config) { + profiles, defaultChatIdentity := chatTaskProfiles(cfg) + if len(profiles) > 0 { + b.chatTasks = gateway.NewStoreTaskCreatorForProfiles( + chatTaskWriterAdapter{enqueue: b.stateStore.EnqueueChatTask}, + profiles, + ) + } + b.defaultChatIdentity = defaultChatIdentity +} + func (b *boot) watchPersonas(ctx context.Context, version int64) error { updates, err := b.controlPlane.WatchPersonas(ctx, version) if err != nil { diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 51c76f8f4..311f1bec0 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -384,24 +384,15 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence return 1 } - if err := b.setupLLMAndChat(ctx); err != nil { + if err := b.setupGatewayClient(); err != nil { return 1 } - // The catalog's consumers exist from here on, and the daemon is the - // process that owns the published configuration, so this is where the - // live catalog refresh belongs. The standalone Gateway reads the catalog - // once at boot and runs no control-plane watches (RunGateway). + // The daemon publishes the configuration view, which carries the catalog's + // model limits. b.startModelCatalogRefresh(ctx) 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(); err != nil { return 1 } @@ -415,16 +406,10 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence if err := b.registerNATSRPC(); err != nil { return 1 } - b.setupCurators(ctx) - if err := b.setupScheduling(); err != nil { return 1 } - if err := b.registerTools(); err != nil { - return 1 - } - b.registerStandaloneTools() b.buildDaemon() b.wireConfigSurfaces(ctx, args.cfgPath, args.overlayPath) diff --git a/internal/app/archied/readiness.go b/internal/app/archied/readiness.go index 45ae597e1..0c710fe85 100644 --- a/internal/app/archied/readiness.go +++ b/internal/app/archied/readiness.go @@ -7,10 +7,7 @@ package archied import ( "context" "errors" - "fmt" - "net/http" "os" - "strings" "time" "github.com/samcharles93/archie-core/internal/config" @@ -34,7 +31,6 @@ func (b *boot) setupReadinessProbes() { readiness.NewStoreProbe(b.stateStore), readiness.NewConfigProbe(b.cfgHolder.Get, configuration.Validate), readiness.NewDiskProbeTargets(diskProbeTargets(cfg)), - readiness.NewModelProbe(b.chatModels.ActiveModel, b.chatModels.Models, modelReachProbe(cfg, b.chatModels.ActiveModel)), // The daemon no longer runs a chat channel, so it cannot observe one's // lifecycle; channel health is the Messaging Service's own probe. What // this process depends on is the Gateway answering, so that is what it @@ -86,39 +82,3 @@ func diskProbePath(cfg config.Config) string { return "." } } - -// modelReachProbe returns a live reachability check for the active model. It -// resolves the active model's provider and, when the provider has a custom -// base URL, performs a short-timeout HTTP request to it: any response -// (including 401/404) proves the endpoint is reachable, while a network error -// or an unconfigured provider reports unreachable. A provider using its SDK -// default endpoint is assumed reachable, because no custom endpoint exists to -// probe. -func modelReachProbe(cfg config.Config, active func() string) func(context.Context) error { - return func(ctx context.Context) error { - model := active() - provider, _, _ := strings.Cut(model, "/") - if provider == "" { - return fmt.Errorf("active model %q has no provider", model) - } - p, ok := cfg.Providers[provider] - if !ok { - return fmt.Errorf("provider %q not configured", provider) - } - if p.BaseURL == "" { - return nil - } - reqCtx, cancel := context.WithTimeout(ctx, 3*time.Second) - defer cancel() - req, err := http.NewRequestWithContext(reqCtx, http.MethodGet, p.BaseURL, nil) - if err != nil { - return err - } - resp, err := http.DefaultClient.Do(req) - if err != nil { - return err - } - _ = resp.Body.Close() - return nil - } -} diff --git a/internal/infrastructure/readiness/probes.go b/internal/infrastructure/readiness/probes.go index 3eb1614c3..c3c39933e 100644 --- a/internal/infrastructure/readiness/probes.go +++ b/internal/infrastructure/readiness/probes.go @@ -275,47 +275,6 @@ func (p *DiskProbe) Check(ctx context.Context) health.Result { // --- model --- -// ModelProbe reports whether a model is configured (an active model is -// selected) and reachable (a live network check succeeds). Reachability is a -// narrow function so the probe is deterministic in tests and the composition -// root supplies the real provider probe. -type ModelProbe struct { - ActiveModel func() string - Models func() []string - Reach func(context.Context) error -} - -// NewModelProbe returns a model probe. The reachability probe is optional: -// nil means the probe only verifies configuration, not the network. -func NewModelProbe(active func() string, models func() []string, reach func(context.Context) error) *ModelProbe { - return &ModelProbe{ActiveModel: active, Models: models, Reach: reach} -} - -func (p *ModelProbe) Name() string { return "model" } - -func (p *ModelProbe) Check(ctx context.Context) health.Result { - active := "" - if p.ActiveModel != nil { - active = p.ActiveModel() - } - models := 0 - if p.Models != nil { - models = len(p.Models()) - } - if active == "" && models == 0 { - return health.Result{Status: health.StatusDegraded, Detail: "no model configured"} - } - if p.Reach == nil { - return health.Result{Status: health.StatusOK, Detail: "model " + active + " configured"} - } - if err := p.Reach(ctx); err != nil { - return health.Result{Status: health.StatusDegraded, Detail: "model unreachable: " + err.Error()} - } - return health.Result{Status: health.StatusOK, Detail: "model " + active + " reachable"} -} - -// --- gateway --- - // ChannelState is a per-channel lifecycle fact as the gateway probe sees it. // It is a projection of the channels.Manager's Status, kept in this package // so the probe does not depend on the channels package's concrete type.