feat(controlplane): runtime administration over the State Store's gRPC server

The Web UI and messaging channels now read and write Archie's runtime
settings through one versioned, audited control-plane API hosted on the
State Store's existing gRPC server: catalog, query, command, watch. Each
feature owns its resource, validation and commands; storage only writes
versions and audit records.

Workflows move with it. Definitions become portable YAML compiled through
a closed, typed step registry and stored as a control-plane resource, so
the yaegi interpretation paths (internal/gate, wfeval, wfextract,
gateeval, gateextract, skillbuild) are deleted rather than carried.

The UI process reads definitions as data: the JSON projection lives in
internal/domain/workflow/task so a dashboard decodes the stored
collection without linking the workflow engine, which cmd/archie-ui's
deletion gate forbids. Shipped definitions ride the catalog descriptor
rather than a bespoke REST route, keeping the API to its four operations.

archie-messaging gains -state-store-target/-state-store-token and reads
channel settings from the database, failing closed when the State Store
is unreachable: the database is authoritative for those settings.
This commit is contained in:
sam committed 2026-09-21 08:17:42 +10:00
1 parent 2e172c679c
commit 924b02a1b0
131 files changed
+8510 -3371

No files matched your search

+47 -23
View File
@@ -13,9 +13,7 @@ import (
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workflow/skillbuild"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/domain/workflow/wfeval"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/installtype"
"github.com/samcharles93/archie-core/internal/storage"
@@ -202,27 +200,23 @@ func applyToolLimits(agent agentexec.Runner, policy config.ToolPolicy) {
}
}
// routeTask applies the request-carried routing bindings and selects the
// workflow for a task. The daemon's package-level SetKindWorkflows state
// never crosses the process boundary, so runTask installs the resolved
// bindings here -- this worker process is where workflow.Route actually runs.
// routeTask remains available for routing-only callers. Production execution
// compiles the request's pinned YAML directly.
func routeTask(req taskrun.Request, registry workflow.Registry) workflow.Workflow {
workflow.SetKindWorkflows(req.KindWorkflows)
workflow.SetLabelWorkflows(req.LabelWorkflows)
return workflow.Route(req.Task, registry)
}
// runTask builds a workflow.Registry from the container's mounted worktree,
// routes and runs the entire workflow, and reports its terminal outcome.
// runTask compiles the immutable database definition carried by the request,
// runs it, and reports its terminal outcome.
// Store remains archied's authority; Response.Task is a logging snapshot.
func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependencies, newRunner runnerFactory, workDir string, log *slog.Logger) (*taskrun.Response, error) {
registry, err := skillbuild.BuildRegistry(workDir)
wf, err := resolvePinnedWorkflow(&req)
if err != nil {
return nil, fmt.Errorf("build registry: %w", err)
return nil, err
}
wf := routeTask(req, registry)
trees := &hybridTrees{
push: dependencies.trees,
local: &worktree.Manager{
@@ -286,17 +280,16 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
}
tc := &workflow.TaskContext{
Task: req.Task,
Repo: req.Repo,
Cfg: req.Cfg.ToConfig(),
Forge: dependencies.forge,
Store: dependencies.store,
Trees: trees,
Agent: agent,
Reviewer: newReviewerFor(req),
Bus: bus,
Log: log,
CustomStages: wfeval.Discover,
Task: req.Task,
Repo: req.Repo,
Cfg: req.Cfg.ToConfig(),
Forge: dependencies.forge,
Store: dependencies.store,
Trees: trees,
Agent: agent,
Reviewer: newReviewerFor(req),
Bus: bus,
Log: log,
}
if req.Repo.PersistentStorage {
memory, readErr := readProjectMemory(storage.MemoryPath)
@@ -317,3 +310,34 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen
AgentInstallType: installtype.Type(),
}, nil
}
func resolvePinnedWorkflow(req *taskrun.Request) (workflow.Workflow, error) {
if req.WorkflowDefinition == "" {
shipped := workflow.ShippedDefinitions()
registry := make(workflow.Registry, len(shipped.Definitions))
for _, definition := range shipped.Definitions {
compiled, compileErr := workflow.ParseAndCompile(definition.YAML, workflow.BuiltinStepRegistry())
if compileErr != nil {
return workflow.Workflow{}, fmt.Errorf("compile shipped workflow definition: %w", compileErr)
}
registry[definition.ID] = compiled
}
selected := routeTask(*req, registry)
req.Task.Workflow = selected.Name
entry, ok := shipped.DefinitionByID(selected.Name)
if !ok {
return workflow.Workflow{}, fmt.Errorf("task has no pinned workflow definition")
}
req.WorkflowDefinition = entry.YAML
req.Task.WorkflowDefinitionYAML = entry.YAML
req.Task.WorkflowDefinitionDigest = workflow.DigestDefinition(entry.YAML)
}
wf, err := workflow.ParseAndCompile(req.WorkflowDefinition, workflow.BuiltinStepRegistry())
if err != nil {
return workflow.Workflow{}, fmt.Errorf("compile pinned workflow definition: %w", err)
}
if wf.Name != req.Task.Workflow || workflow.DigestDefinition(req.WorkflowDefinition) != req.Task.WorkflowDefinitionDigest {
return workflow.Workflow{}, fmt.Errorf("pinned workflow definition does not match task identity")
}
return wf, nil
}
+17 -17
View File
@@ -24,6 +24,7 @@ import (
natsio "github.com/nats-io/nats.go"
"github.com/samcharles93/ai-sdk/runtime"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/container"
"github.com/samcharles93/archie-core/internal/daemon"
@@ -32,11 +33,11 @@ import (
"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"
"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"
@@ -116,6 +117,7 @@ type boot struct {
// standalone-only compose leaves no other case).
stateStoreGrants *staterpc.GrantIssuer
stateStoreToken string
controlPlane *controlplane.Client
chatSessionStore gateway.SessionStore
catalog modelcatalog.Snapshot
@@ -282,6 +284,7 @@ func (b *boot) loadConfig(ctx context.Context, cfgPath, overlayPath string, noCo
}
}
b.cfg = b.doc.Config
b.cfgHolder = config.NewHolder(b.cfg)
b.currentProvenance.Store(&b.doc.Provenance)
return b.setupLogging()
}
@@ -363,6 +366,7 @@ func (b *boot) openStateStoreAdapter() error {
return err
}
b.stateStore = client
b.controlPlane = controlplane.NewRPCClient(client.ControlPlane())
b.stateStoreGrants = &staterpc.GrantIssuer{Client: client}
b.stateStoreToken = b.cfg.Services.ResolvedToken(config.ServiceNameState, b.secrets.Getenv)
b.addCleanup(cleanup)
@@ -419,7 +423,6 @@ func (b *boot) setupObservability(ctx context.Context) {
bus := events.NewBus()
b.bus = bus
b.addCleanup(func() { bus.Close() })
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).
@@ -584,13 +587,15 @@ func (b *boot) setupEmbeddings(cfg config.Config, log *slog.Logger) {
// setupLLMAndChat wires the runtime, tool registry, model management,
// personas and the dashboard's chat service.
func (b *boot) setupLLMAndChat() error {
func (b *boot) setupLLMAndChat(ctx context.Context) 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)
if err := b.setupChatRuntime(ctx, cfg); err != nil {
return err
}
b.setupEmbeddings(cfg, log)
contract, cleanup, err := composeChatContract(cfg.Services, b.secrets)
@@ -635,9 +640,8 @@ func (b *boot) startRateLimiter(ctx context.Context, cfg config.RateLimitConfig)
}()
}
// loadWorkflows builds the workflow registry from the skill catalog.
// Plugin-defined workflows override built-ins of the same name;
// built-ins fill gaps.
// loadWorkflows loads routing inputs. Executable definitions are supplied by
// the State Store control plane and pinned before each dispatch.
func (b *boot) loadWorkflows(ctx context.Context) error {
cfg, log := b.cfg, b.log
if err := b.loadWorkflowRouting(cfg, log); err != nil {
@@ -650,16 +654,7 @@ func (b *boot) loadWorkflows(ctx context.Context) error {
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))
log.Info("workflow step registry built", "shipped_workflows", len(workflow.ShippedDefinitions().Definitions))
// 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.
@@ -1250,10 +1245,15 @@ func (b *boot) buildDaemon() {
Guardrails: b.guardrails,
ToolRegistry: b.toolReg,
Identities: b.identityRunners,
RootIdentityID: identity.StableID(configuredIdentityNames(b.cfg)[0]),
TaskLogs: b.taskLogs,
AgentStatus: b.agentStatus,
KindWorkflows: b.kindWorkflows,
LabelWorkflows: b.labelWorkflows,
WorkflowDefinitions: b.controlPlane,
}
if identities, ok := b.stateStore.(identity.Repository); ok {
b.d.IdentityRepository = identities
}
// Consumer mapping/binding surfaces resolve from b.stateStore (the State
// Store contract adapter): local by default, remote *staterpc.Client when
@@ -20,7 +20,7 @@ func TestRemoteChatCompositionPreservesChannelTurnLedger(t *testing.T) {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
b := &boot{cfg: cfg, log: slog.Default(), cfgHolder: config.NewHolder(cfg), stateStore: st}
b := &boot{cfg: cfg, log: slog.Default(), cfgHolder: config.NewHolder(cfg), stateStore: st, personas: gateway.NewPersonaRegistry(gateway.DefaultPersonas())}
t.Cleanup(b.cleanup)
if err := b.openStores(t.Context()); err != nil {
t.Fatal(err)
@@ -29,7 +29,7 @@ func TestRemoteChatCompositionPreservesChannelTurnLedger(t *testing.T) {
t.Fatal("startup did not open the channel conversation store")
}
sessions := b.chatSessionStore
if err := b.setupLLMAndChat(); err != nil {
if err := b.setupLLMAndChat(t.Context()); err != nil {
t.Fatal(err)
}
if b.chatSessionStore != sessions {
+61
View File
@@ -0,0 +1,61 @@
package archied
import (
"context"
"fmt"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
func (b *boot) loadRuntimeConfig(ctx context.Context) error {
cfg, err := b.controlPlane.RuntimeConfig(ctx, b.cfg)
if err != nil {
return err
}
if err := configuration.Validate(&cfg); err != nil {
return fmt.Errorf("validate database settings: %w", err)
}
b.cfg = cfg
b.cfgHolder.Set(cfg)
return nil
}
func (b *boot) startWorkflowExecutionSettings(ctx context.Context) error {
settings, version, err := b.controlPlane.WorkflowExecutionSettings(ctx)
if err != nil {
return err
}
b.applyWorkflowExecutionSettings(settings, version)
updates, err := b.controlPlane.WatchWorkflowExecutionSettings(ctx, version)
if err != nil {
return err
}
go func() {
for update := range updates {
if update.Err != nil {
b.log.Error("workflow execution settings watch failed", "err", update.Err)
return
}
b.applyWorkflowExecutionSettings(update.Settings, update.Version)
}
}()
return nil
}
func (b *boot) applyWorkflowExecutionSettings(settings workflow.ExecutionSettings, version int64) {
cfg := b.cfgHolder.Get().Clone()
cfg.Budgets = executionBudgets(settings)
for i := range cfg.Identities {
cfg.Identities[i].Budgets = cfg.Budgets
}
b.cfgHolder.Set(cfg)
b.log.Info("workflow execution settings applied", "version", version)
}
func executionBudgets(settings workflow.ExecutionSettings) config.Budgets {
return config.Budgets{
MaxSteps: settings.MaxModelToolSteps, WallClock: config.Duration(settings.MaxRuntime), GateMaxFailures: settings.MaxConsecutiveGateFailures,
}
}
+15 -2
View File
@@ -67,7 +67,7 @@ func RunGateway(ctx context.Context, options GatewayOptions) error {
// same remote State Store contract the daemon does via
// [services.state].target (docs/prds/state-store-contract.md §12 step 7).
// Its own session SQLite is untouched and stays Gateway-owned.
if err := b.openStateStoreAdapter(); err != nil {
if err := b.openGatewayState(ctx); err != nil {
return err
}
b.loadCatalog(ctx, options.Config)
@@ -125,6 +125,16 @@ func RunGateway(ctx context.Context, options GatewayOptions) error {
return serveGateway(ctx, listener, contract, b.chatSessionStore, opts)
}
func (b *boot) openGatewayState(ctx context.Context) error {
if err := b.openStateStoreAdapter(); err != nil {
return err
}
if err := b.loadRuntimeConfig(ctx); err != nil {
return fmt.Errorf("load runtime settings: %w", err)
}
return nil
}
func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
b.bus = events.NewBus()
b.addCleanup(b.bus.Close)
@@ -136,7 +146,10 @@ func (b *boot) startGatewayRuntime(ctx context.Context, actor gateway.ChatTaskAc
if err := b.setupMemoryEngine(); err != nil { //nolint:contextcheck // setupMemoryEngine owns its own lifecycle context, matching the daemon's setupMemoryEngine
return nil, err
}
contract := b.setupGatewayChat(ctx, actor)
contract, err := b.setupGatewayChat(ctx, actor)
if err != nil {
return nil, err
}
if err := b.registerTools(ctx); err != nil {
return nil, err
}
+38 -5
View File
@@ -6,6 +6,7 @@ import (
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/agent"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/tools"
)
@@ -14,7 +15,7 @@ import (
// chat task creator/controller, default chat identity and the release-update
// service. The daemon and the standalone Gateway build through this one
// constructor so the two processes cannot diverge.
func (b *boot) setupChatRuntime(cfg config.Config) {
func (b *boot) setupChatRuntime(ctx context.Context, cfg config.Config) error {
// ── LLM runtime ──────────────────────────────────────────────────
// Created before the router so the LLMResponder can be wired in for
// non-command message processing.
@@ -26,7 +27,27 @@ func (b *boot) setupChatRuntime(cfg config.Config) {
b.chatModels = chatModels
// ── Persona registry ─────────────────────────────────────────────
b.personas = gateway.NewPersonaRegistry(gateway.DefaultPersonas())
if b.personas == nil {
personas, version, err := b.controlPlane.Personas(ctx)
if err != nil {
return fmt.Errorf("load personas: %w", err)
}
b.personas = gateway.NewPersonaRegistry(nil)
b.applyPersonas(personas, version)
updates, err := b.controlPlane.WatchPersonas(ctx, version)
if err != nil {
return fmt.Errorf("watch personas: %w", err)
}
go func() {
for update := range updates {
if update.Err != nil {
b.log.Error("persona watch failed", "err", update.Err)
return
}
b.applyPersonas(update.Collection, update.Version)
}
}()
}
// ── Operator health surface ──────────────────────────────────────
// Built before the gateways because every turn runner and router built
@@ -52,12 +73,24 @@ func (b *boot) setupChatRuntime(cfg config.Config) {
transition: b.stateStore.Transition,
})
b.updateService = makeUpdateService(chatSetup{Cfg: config.NewHolder(cfg)})
return nil
}
func (b *boot) applyPersonas(collection agent.PersonaCollection, version int64) {
personas := make([]gateway.Persona, 0, len(collection.Personas))
for _, persona := range collection.Personas {
personas = append(personas, gateway.Persona{Name: persona.Name, Prompt: persona.Prompt})
}
b.personas.Replace(personas, collection.Default)
b.log.Info("personas applied", "version", version)
}
// setupGatewayChat is the sole production constructor of the local contract.
// Frontends use its gRPC representation; only this service owns the router.
func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor) gateway.ChatContract {
b.setupChatRuntime(b.cfg)
func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor) (gateway.ChatContract, error) {
if err := b.setupChatRuntime(ctx, b.cfg); err != nil {
return nil, err
}
cfg := b.cfg
router := gateway.NewRouter(b.stateStore, nil, "web")
router.Limiter = b.rateLimiter
@@ -88,5 +121,5 @@ func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor
return &gateway.LocalChatAdapter{
Router: router, Sessions: b.chatSessionStore, Turns: gateway.NewTurns(b.log),
Models: b.chatModels, Personas: b.personas, TaskActor: actor,
}
}, nil
}
+10 -2
View File
@@ -347,7 +347,7 @@ func parseArgs() (runArgs, bool) {
return args, false
}
func Run() int { //nolint:cyclop // the composition root's setup sequence is deliberately flat and sequential
func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence is deliberately flat and sequential
args, exit := parseArgs()
if exit {
return 0
@@ -390,6 +390,14 @@ func Run() int { //nolint:cyclop // the composition root's setup sequence is del
if err := b.openStateStoreAdapter(); err != nil {
return 1
}
if err := b.loadRuntimeConfig(ctx); err != nil {
b.log.Error("runtime settings unavailable", "err", err)
return 1
}
if err := b.startWorkflowExecutionSettings(ctx); err != nil {
b.log.Error("workflow execution settings unavailable", "err", err)
return 1
}
if exit, err := b.handleRequeue(ctx, args.requeue, args.once); err != nil {
return 1
} else if exit {
@@ -402,7 +410,7 @@ func Run() int { //nolint:cyclop // the composition root's setup sequence is del
return 1
}
if err := b.setupLLMAndChat(); err != nil {
if err := b.setupLLMAndChat(ctx); err != nil {
return 1
}
if err := b.buildTreesAndIdentities(ctx); err != nil {
+19 -3
View File
@@ -29,6 +29,10 @@ type prReviewer struct {
models map[string]string
providers map[string]agentexec.Provider
maxSteps int
// currentMaxSteps lets the composition root apply live control-plane
// versions to reviews that have not started yet. Tests and direct
// constructions retain maxSteps as the fallback.
currentMaxSteps func() int
// allowed maps a chat identity to the "owner/name" repos it may review.
// A nil identity is an authenticated dashboard operator, who may review
// any configured repository.
@@ -100,11 +104,15 @@ func (r *prReviewer) ReviewPR(ctx context.Context, identity *string, repo string
return gateway.PRReviewResult{}, fmt.Errorf("snapshot PR head: %w", err)
}
maxSteps := r.maxSteps
if r.currentMaxSteps != nil {
maxSteps = r.currentMaxSteps()
}
report := reviewer.Review(ctx, workflow.ReviewRequest{
SnapshotDir: snapshotDir,
Diff: diff,
IssueText: prIssueText(pr),
MaxSteps: r.maxSteps,
MaxSteps: maxSteps,
})
r.logReview(key, model, report)
return mapReviewResult(repo, number, model, report), nil
@@ -246,14 +254,22 @@ func (b *boot) prReviewer() gateway.ChatPRReviewer {
if forgeReader == nil {
return nil
}
b.chatPRReviewer = &prReviewer{
maxSteps := b.cfg.Budgets.MaxSteps
if b.cfgHolder != nil {
maxSteps = b.cfgHolder.Get().Budgets.MaxSteps
}
review := &prReviewer{
forge: forgeReader,
trees: b.trees,
models: b.cfg.Models,
providers: executionProviders(b.cfg),
maxSteps: b.cfg.Budgets.MaxSteps,
maxSteps: maxSteps,
allowed: buildReviewAllowlist(b.cfg),
log: b.log,
}
if b.cfgHolder != nil {
review.currentMaxSteps = func() int { return b.cfgHolder.Get().Budgets.MaxSteps }
}
b.chatPRReviewer = review
return b.chatPRReviewer
}
+73 -11
View File
@@ -2,11 +2,12 @@ package archied
import (
"context"
"errors"
"fmt"
"log/slog"
"path/filepath"
"time"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/gateway"
@@ -68,16 +69,7 @@ func (b *boot) setupScheduling() error {
return nil
}
storePath := filepath.Join(filepath.Dir(b.cfg.DBPath), "cron", "jobs.json")
store, err := cronstore.Open(storePath)
if err != nil {
return fmt.Errorf("scheduling: open job store: %w", err)
}
b.addCleanup(func() {
if err := store.Close(); err != nil {
b.log.Error("scheduling store close", "err", err)
}
})
store := scheduleResourceStore{client: b.controlPlane}
workflowRunner, err := crondelivery.NewWorkflowTask(store, spawnTaskSubmitter{creator: b.chatTasks, identity: b.defaultChatIdentity})
if err != nil {
@@ -97,6 +89,76 @@ func (b *boot) setupScheduling() error {
return nil
}
type scheduleResourceStore struct {
client *controlplane.Client
}
func (s scheduleResourceStore) Get(ctx context.Context, id string) (cronstore.JobSpec, bool, error) {
jobs, _, err := s.client.Schedules(ctx)
if err != nil {
return cronstore.JobSpec{}, false, err
}
for _, job := range jobs {
if job.ID == id {
return job, true, nil
}
}
return cronstore.JobSpec{}, false, nil
}
func (s scheduleResourceStore) Due(ctx context.Context, now time.Time) ([]scheduling.Job, error) {
jobs, _, err := s.client.Schedules(ctx)
if err != nil {
return nil, err
}
due := make([]scheduling.Job, 0, len(jobs))
for _, job := range jobs {
if !job.NextRun.After(now) {
due = append(due, scheduling.Job{ID: job.ID, Pool: scheduling.Pool(job.Pool), Detail: job.Detail})
}
}
return due, nil
}
func (s scheduleResourceStore) MarkRun(ctx context.Context, id string, runAt time.Time) error {
for range 4 {
jobs, version, err := s.client.Schedules(ctx)
if err != nil {
return err
}
found := false
for index := range jobs {
if jobs[index].ID != id {
continue
}
found = true
if jobs[index].Schedule.Resolved().Kind == cronstore.ScheduleOnce {
jobs = append(jobs[:index], jobs[index+1:]...)
break
}
next, nextErr := jobs[index].Schedule.NextRun(runAt)
if nextErr != nil {
return nextErr
}
at := runAt.UTC()
jobs[index].LastRun = &at
jobs[index].NextRun = next.UTC()
break
}
if !found {
return fmt.Errorf("%w: id %q", cronstore.ErrJobNotFound, id)
}
_, err = s.client.ReplaceSchedules(ctx, jobs, version, "system:scheduler", "scheduler", fmt.Sprintf("schedule-run:%s:%d", id, runAt.UnixNano()))
if err == nil {
return nil
}
if !errors.Is(err, controlplane.ErrVersionConflict) {
return err
}
}
return controlplane.ErrVersionConflict
}
// shutdownSchedulingEngine returns a cleanup that stops the ticker engine.
// The stop path derives its own timeout budget from Background rather than
// the boot context, which is already cancelled by the time shutdown runs --
+85 -1
View File
@@ -15,18 +15,39 @@ import (
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"time"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/config"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
"github.com/samcharles93/archie-core/internal/infrastructure/readiness"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/store"
)
func configuredIdentityNames(cfg config.Config) []string {
if len(cfg.Identities) == 0 {
name := cfg.BotUser
if name == "" {
name = "archie"
}
return []string{name}
}
names := make([]string, 0, len(cfg.Identities))
for _, value := range cfg.Identities {
names = append(names, value.Name)
}
return names
}
// StateStoreOptions contains process inputs for the standalone State Store.
type StateStoreOptions struct {
Config string
@@ -65,6 +86,31 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error {
if err := b.openStateStore(ctx); err != nil {
return err
}
identityStore, ok := b.st.(interface {
BootstrapIdentities(context.Context, []string) error
})
if !ok {
return fmt.Errorf("state store does not support identity bootstrap")
}
legacyNames := configuredIdentityNames(b.cfg)
if err := identityStore.BootstrapIdentities(ctx, legacyNames); err != nil {
return fmt.Errorf("bootstrap identities: %w", err)
}
resources, ok := b.st.(interface {
Resource(context.Context, string) (store.Resource, error)
PutResource(context.Context, store.ResourceWrite) (store.Resource, error)
})
if !ok {
return fmt.Errorf("state store does not support control-plane resources")
}
control := controlplane.NewServer(resources)
versions, err := control.ImportConfig(ctx, b.cfg)
if err != nil {
return fmt.Errorf("import control-plane resources: %w", err)
}
if err := migrateLegacySchedules(ctx, control, b.cfg.DBPath, versions[controlplane.SchedulesKind]); err != nil {
return fmt.Errorf("migrate schedules: %w", err)
}
token := options.Token
if token == "" {
@@ -89,7 +135,42 @@ func RunStateStore(ctx context.Context, options StateStoreOptions) error {
}
}
return serveStateStore(ctx, listener, b.stateStoreDeps(grants), opts)
deps := b.stateStoreDeps(grants)
deps.ControlPlane = control
return serveStateStore(ctx, listener, deps, opts)
}
func migrateLegacySchedules(ctx context.Context, control *controlplane.Server, dbPath string, version int64) error {
if version != 1 {
return nil
}
path := filepath.Join(filepath.Dir(dbPath), "cron", "jobs.json")
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
legacy, err := cronstore.Open(path)
if err != nil {
return err
}
jobs, listErr := legacy.List(ctx)
closeErr := legacy.Close()
if listErr != nil {
return listErr
}
if closeErr != nil {
return closeErr
}
value, err := json.Marshal(jobs)
if err != nil {
return err
}
if _, err := control.Command(ctx, &controlpb.CommandRequest{Kind: controlplane.SchedulesKind, Command: "replace", ValueJson: value, ExpectedVersion: 1, Actor: "system:migration", Source: "legacy-cronstore", RequestId: "import:legacy-schedules"}); err != nil {
return err
}
return os.Rename(path, path+".migrated")
}
// openStateStore opens the single task-store SQLite file exactly once for
@@ -130,6 +211,9 @@ func (b *boot) openStateStore(ctx context.Context) error {
// degrades that group rather than aborting boot.
func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps {
deps := staterpc.Deps{Tasks: b.st, Log: b.log, Grants: grants}
if identities, ok := b.st.(identity.Repository); ok {
deps.Identities = identities
}
// Task logs live in the state directory, which this process owns, and the
// dashboard process owns no such directory -- so this is where a task-log
// read is served from (docs/prds/ui-service-boundary.md). The reader is
@@ -50,3 +50,16 @@ func TestLoadConfigDoesNotWarnOnAKnownConfig(t *testing.T) {
t.Errorf("unexpected warning for a config with no unknown keys: %s", out)
}
}
func TestLoadConfigMakesRuntimeSettingsAvailableBeforeSubsystemSetup(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
writeConfig(t, path, minimalConfigTOML("widget"))
b := &boot{log: slog.Default()}
if err := b.loadConfig(t.Context(), path, "", true); err != nil {
t.Fatalf("loadConfig: %v", err)
}
if b.cfgHolder == nil {
t.Fatal("runtime config holder is nil before control-plane settings load")
}
}
+6 -4
View File
@@ -19,10 +19,11 @@ import (
)
type deps struct {
Config ResolvedConfig
Log *slog.Logger
Chat messaging.ChatContract
Health *health.Registry
Config ResolvedConfig
Log *slog.Logger
Chat messaging.ChatContract
Health *health.Registry
Settings *messaging.SettingsCommand
}
type channelInstance struct {
@@ -63,6 +64,7 @@ func compose(ctx context.Context, d deps) (*Service, error) {
"Add your Telegram user id to chat.telegram.allowed_user_ids to enable the bot.")
}
tg := telegram.New(d.Config.TelegramToken, d.Config.Telegram.AllowedUserIDs, d.Log)
tg.Settings = d.Settings
configureTelegram(ctx, tg, d.Config, d.Chat, d.Log)
if err := srv.add("telegram", tg, telegramValidateConfigMap(d.Config.Telegram)); err != nil {
return nil, err
+11
View File
@@ -34,6 +34,7 @@ type ResolvedConfig struct {
type projection struct {
gateway ServiceTarget
stateStore ServiceTarget
telegram config.TelegramConfig
email config.EmailConfig
webhook config.WebhookRoute
@@ -111,6 +112,7 @@ func project(cfg config.Config) projection {
Target: cfg.Services.Get(config.ServiceNameGateway).Target,
Token: cfg.Services.Get(config.ServiceNameGateway).TargetToken,
},
stateStore: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target, Token: cfg.Services.Get(config.ServiceNameState).TargetToken},
telegram: cfg.Chat.Telegram,
email: cfg.Chat.Email,
webhook: cfg.Chat.Webhook,
@@ -126,6 +128,9 @@ func withEnvTokens(o Options) Options {
if o.Gateway.Token == "" {
o.Gateway.Token = os.Getenv("GATEWAY_TOKEN")
}
if o.StateStore.Token == "" {
o.StateStore.Token = os.Getenv("STATE_STORE_TOKEN")
}
return o
}
@@ -136,6 +141,12 @@ func merge(o Options, p projection) Options {
if o.Gateway.Token == "" {
o.Gateway.Token = p.gateway.Token
}
if o.StateStore.Target == "" {
o.StateStore.Target = p.stateStore.Target
}
if o.StateStore.Token == "" {
o.StateStore.Token = p.stateStore.Token
}
return o
}
+6
View File
@@ -35,6 +35,7 @@ type Options struct {
Overlay string
Gateway ServiceTarget
StateStore ServiceTarget
DependencyTimeout time.Duration
ShutdownTimeout time.Duration
}
@@ -59,6 +60,11 @@ func (o Options) validate() error {
if err := validateServiceTarget("gateway", o.Gateway); err != nil {
return err
}
if o.StateStore.Target != "" {
if err := validateServiceTarget("state store", o.StateStore); err != nil {
return err
}
}
return nil
}
+37 -4
View File
@@ -5,7 +5,12 @@ import (
"fmt"
"log/slog"
"github.com/samcharles93/archie-core/internal/app/controlplane"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/secret"
)
// Run runs the standalone Messaging Service process until ctx is cancelled.
@@ -24,12 +29,40 @@ func Run(ctx context.Context, o Options) error {
defer closeGateway()
health := newReadinessRegistry(cfg.Options, chat)
var settings *messaging.SettingsCommand
var closeStateStore func()
if cfg.Options.StateStore.Target != "" {
stateStore, closeClient, dialErr := staterpc.Dial(cfg.Options.StateStore.Target, cfg.Options.StateStore.Token)
if dialErr != nil {
return fmt.Errorf("dial state store control plane (%s): %w", cfg.Options.StateStore.Target, dialErr)
}
closeStateStore = closeClient
defer closeStateStore()
chatSettings, settingsErr := controlplane.NewRPCClient(stateStore.ControlPlane()).RuntimeChatConfig(ctx, config.ChatConfig{
Telegram: cfg.Telegram, Email: cfg.Email, Webhook: cfg.Webhook, WebhookAddr: cfg.WebhookAddr, ShowToolCalls: cfg.ShowToolCalls,
})
if settingsErr != nil {
return fmt.Errorf("load channel settings: %w", settingsErr)
}
secrets := secret.NewRegistry()
cfg.Telegram, cfg.Email, cfg.Webhook, cfg.WebhookAddr, cfg.ShowToolCalls = chatSettings.Telegram, chatSettings.Email, chatSettings.Webhook, chatSettings.WebhookAddr, chatSettings.ShowToolCalls
cfg.TelegramToken, settingsErr = resolveTelegramToken(chatSettings.Telegram, secrets)
if settingsErr != nil {
return fmt.Errorf("resolve database telegram token: %w", settingsErr)
}
cfg.WebhookSecret, settingsErr = resolveWebhookSecret(chatSettings.Webhook, secrets)
if settingsErr != nil {
return fmt.Errorf("resolve database webhook secret: %w", settingsErr)
}
settings = messaging.NewSettingsCommand(messagingControlPlane{client: stateStore.ControlPlane()}).WithIdentities(stateStore)
}
srv, err := compose(ctx, deps{
Config: cfg,
Log: log,
Chat: chat,
Health: health,
Config: cfg,
Log: log,
Chat: chat,
Health: health,
Settings: settings,
})
if err != nil {
return err
+100
View File
@@ -0,0 +1,100 @@
package archiemessaging
import (
"context"
"encoding/json"
"errors"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
type messagingControlPlane struct{ client pb.ControlPlaneServiceClient }
func (c messagingControlPlane) Catalog(ctx context.Context) ([]messaging.SettingDescriptor, error) {
response, err := c.client.Catalog(ctx, &pb.CatalogRequest{})
if err != nil {
return nil, settingsRPCError(err)
}
descriptors := make([]messaging.SettingDescriptor, 0, len(response.Resources))
for _, resource := range response.Resources {
var schema struct {
Properties map[string]struct {
Type string `json:"type"`
Format string `json:"format"`
WriteOnly bool `json:"writeOnly"`
} `json:"properties"`
}
if err := json.Unmarshal([]byte(resource.SchemaJson), &schema); err != nil {
return nil, errors.Join(messaging.ErrSettingsValidation, err)
}
fields := make(map[string]messaging.SettingField, len(schema.Properties))
for name, property := range schema.Properties {
fieldType := property.Type
if property.Format == "duration" || strings.HasSuffix(name, "_seconds") {
fieldType = "duration"
}
fields[name] = messaging.SettingField{Type: fieldType, Secret: property.WriteOnly || secretFieldName(name)}
}
descriptors = append(descriptors, messaging.SettingDescriptor{Kind: resource.Kind, Title: resource.Title, Fields: fields})
}
return descriptors, nil
}
func (c messagingControlPlane) Query(ctx context.Context, kind string) (messaging.SettingResource, error) {
response, err := c.client.Query(ctx, &pb.QueryRequest{Kind: kind})
if err != nil {
return messaging.SettingResource{}, settingsRPCError(err)
}
return settingResource(response.Resource)
}
func (c messagingControlPlane) Command(ctx context.Context, command messaging.SettingCommand) (messaging.SettingResource, error) {
input := any(command.Value)
if command.RawValue != nil {
input = command.RawValue
}
value, err := json.Marshal(input)
if err != nil {
return messaging.SettingResource{}, errors.Join(messaging.ErrSettingsValidation, err)
}
response, err := c.client.Command(ctx, &pb.CommandRequest{Kind: command.Kind, Command: "replace", ValueJson: value, ExpectedVersion: command.ExpectedVersion, RequestId: command.RequestID, Actor: command.Actor, Source: command.Source})
if err != nil {
return messaging.SettingResource{}, settingsRPCError(err)
}
return settingResource(response.Resource)
}
func settingResource(resource *pb.Resource) (messaging.SettingResource, error) {
if resource == nil {
return messaging.SettingResource{}, messaging.ErrSettingsUnavailable
}
var raw any
if err := json.Unmarshal(resource.ValueJson, &raw); err != nil {
return messaging.SettingResource{}, errors.Join(messaging.ErrSettingsValidation, err)
}
value, _ := raw.(map[string]any)
return messaging.SettingResource{Kind: resource.Kind, Version: resource.Version, Value: value, Raw: raw}, nil
}
func settingsRPCError(err error) error {
switch status.Code(err) {
case codes.InvalidArgument:
return errors.Join(messaging.ErrSettingsValidation, err)
case codes.NotFound:
return errors.Join(messaging.ErrSettingsNotFound, err)
case codes.Aborted:
return errors.Join(messaging.ErrSettingsConflict, err)
default:
return errors.Join(messaging.ErrSettingsUnavailable, err)
}
}
func secretFieldName(name string) bool {
name = strings.ToLower(name)
return strings.Contains(name, "secret") || strings.Contains(name, "password") || strings.Contains(name, "token") || strings.Contains(name, "credential") || strings.Contains(name, "key")
}
+11 -5
View File
@@ -5,7 +5,9 @@ import (
"log/slog"
"time"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/events"
@@ -18,11 +20,13 @@ import (
// contracts, the process's own logger and options, and the readiness registry
// assembled over those same contracts.
type deps struct {
Options Options
Log *slog.Logger
Store storecontract.TaskStore
Chat messaging.ChatContract
Health *health.Registry
Options Options
Log *slog.Logger
Store storecontract.TaskStore
Chat messaging.ChatContract
Health *health.Registry
ControlPlane controlpb.ControlPlaneServiceClient
Identities identity.Repository
}
// compose builds the dashboard server for the UI process. It sets exactly the
@@ -65,6 +69,8 @@ func compose(d deps) *webui.Server {
Token: d.Options.Token,
TrustForwardedHeaders: d.Options.trustForwardedHeaders(),
Health: d.Health,
ControlPlane: d.ControlPlane,
Identities: d.Identities,
}
if snapshots, ok := d.Store.(storecontract.ConfigSnapshotStore); ok {
srv.ConfigSource = webui.RemoteConfigView(snapshots)
-3
View File
@@ -94,9 +94,6 @@ func TestComposeUIServerHoldsNoDaemonState(t *testing.T) {
t.Errorf("%s is wired; it is a daemon-owned callback or runtime handle with no contract behind it yet", name)
}
}
if srv.Workflows != nil {
t.Error("Workflows is wired; the definition catalog is a filesystem scan on the daemon host")
}
// Mappings and bindings are the other way round: ratified State Store
// contracts, carried by the client this process already holds, so
// leaving them nil would degrade two pages that have an owner.
+7 -5
View File
@@ -45,11 +45,13 @@ func Run(ctx context.Context, options Options) error {
cleanups = append(cleanups, closeGateway)
srv := compose(deps{
Options: opts,
Log: log,
Store: tasks,
Chat: chat,
Health: newReadinessRegistry(opts, tasks, chat),
Options: opts,
Log: log,
Store: tasks,
Chat: chat,
Health: newReadinessRegistry(opts, tasks, chat),
ControlPlane: tasks.ControlPlane(),
Identities: tasks,
})
// Live activity has no in-process bus in this process: the pump reads
@@ -0,0 +1,57 @@
package controlplane
import (
"fmt"
"github.com/samcharles93/archie-core/internal/config"
)
type channelSettings struct {
Operator string `json:"operator"`
ShowToolCalls bool `json:"show_tool_calls"`
MaxSteps int `json:"max_steps"`
Models []string `json:"models"`
Email config.EmailConfig `json:"email"`
WebhookAddr string `json:"webhook_addr"`
Webhook webhookChannelSettings `json:"webhook"`
Telegram telegramSettings `json:"telegram"`
RateLimit config.RateLimitConfig `json:"rate_limit"`
UnrestrictedFilesystem bool `json:"unrestricted_filesystem"`
Workspace string `json:"workspace"`
}
type telegramSettings struct {
AllowedUserIDs []int64 `json:"allowed_user_ids"`
Token config.SecretRef `json:"token_ref"`
TokenEnv string `json:"token_env,omitempty"`
CredentialConfigured bool `json:"credential_configured"`
}
type webhookChannelSettings struct {
Path string `json:"path"`
Secret config.SecretRef `json:"secret_ref"`
CredentialConfigured bool `json:"credential_configured"`
Template string `json:"template"`
DeliverTo string `json:"deliver_to"`
}
func seedChannels(cfg config.Config) any {
chat := cfg.Chat
return channelSettings{
Operator: chat.Operator, Workspace: chat.Workspace, UnrestrictedFilesystem: chat.UnrestrictedFilesystem, ShowToolCalls: chat.ShowToolCalls, MaxSteps: chat.MaxSteps, Models: chat.Models, Email: chat.Email, WebhookAddr: chat.WebhookAddr,
Telegram: telegramSettings{AllowedUserIDs: chat.Telegram.AllowedUserIDs, Token: chat.Telegram.Token, TokenEnv: chat.Telegram.TokenEnv, CredentialConfigured: chat.Telegram.TokenEnv != "" || chat.Telegram.Token != (config.SecretRef{})},
Webhook: webhookChannelSettings{Path: chat.Webhook.Path, Secret: chat.Webhook.Secret, CredentialConfigured: chat.Webhook.Secret != (config.SecretRef{}), Template: chat.Webhook.Template, DeliverTo: chat.Webhook.DeliverTo}, RateLimit: chat.RateLimit,
}
}
func validateChannels(input []byte) error {
return validateAs(input, func(settings channelSettings) error {
if settings.MaxSteps < 0 || settings.RateLimit.MaxRequests < 0 || settings.RateLimit.Window < 0 {
return fmt.Errorf("channel limits must not be negative")
}
if settings.Webhook.DeliverTo != "" && settings.Webhook.DeliverTo != "origin" {
return fmt.Errorf("webhook deliver_to must be empty or origin")
}
return nil
})
}
+188
View File
@@ -0,0 +1,188 @@
package controlplane
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/agent"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
)
type Client struct{ rpc pb.ControlPlaneServiceClient }
func NewClient(conn grpc.ClientConnInterface) *Client {
return &Client{rpc: pb.NewControlPlaneServiceClient(conn)}
}
func NewRPCClient(client pb.ControlPlaneServiceClient) *Client { return &Client{rpc: client} }
func (c *Client) Catalog(ctx context.Context) ([]*pb.ResourceDescriptor, error) {
response, err := c.rpc.Catalog(ctx, &pb.CatalogRequest{})
if err != nil {
return nil, clientError(err)
}
return response.Resources, nil
}
func (c *Client) WorkflowDefinitions(ctx context.Context) (workflow.WorkflowDefinitionCollection, int64, error) {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: WorkflowDefinitionsKind})
if err != nil {
return workflow.WorkflowDefinitionCollection{}, 0, clientError(err)
}
definitions, err := workflow.DecodeDefinitionCollection(response.Resource.ValueJson, workflow.BuiltinStepRegistry())
return definitions, response.Resource.Version, err
}
// ReplaceWorkflowDefinitions replaces the collection. Passing
// workflow.ShippedDefinitions() restores all shipped definitions while keeping
// the previous override in resource history.
func (c *Client) ReplaceWorkflowDefinitions(ctx context.Context, definitions workflow.WorkflowDefinitionCollection, expectedVersion int64, actor, source, requestID string) (int64, error) {
value, err := encodeWorkflowDefinitions(definitions)
if err != nil {
return 0, err
}
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: WorkflowDefinitionsKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
if err != nil {
return 0, clientError(err)
}
return response.Resource.Version, nil
}
func (c *Client) WorkflowExecutionSettings(ctx context.Context) (workflow.ExecutionSettings, int64, error) {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: WorkflowExecutionSettingsKind})
if err != nil {
return workflow.ExecutionSettings{}, 0, clientError(err)
}
settings, err := decodeSettings(response.Resource.ValueJson)
return settings, response.Resource.Version, err
}
func (c *Client) ReplaceWorkflowExecutionSettings(ctx context.Context, settings workflow.ExecutionSettings, expectedVersion int64, actor, source, requestID string) (int64, error) {
if err := settings.Validate(); err != nil {
return 0, fmt.Errorf("%w: %w", ErrValidation, err)
}
value, err := encodeSettings(settings)
if err != nil {
return 0, err
}
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: WorkflowExecutionSettingsKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
if err != nil {
return 0, clientError(err)
}
return response.Resource.Version, nil
}
func (c *Client) WatchWorkflowExecutionSettings(ctx context.Context, afterVersion int64) (<-chan AppliedSettings, error) {
stream, err := c.rpc.Watch(ctx, &pb.WatchRequest{Kind: WorkflowExecutionSettingsKind, AfterVersion: afterVersion})
if err != nil {
return nil, clientError(err)
}
out := make(chan AppliedSettings)
go func() {
defer close(out)
for {
response, err := stream.Recv()
if err != nil {
if !errors.Is(err, io.EOF) && ctx.Err() == nil {
out <- AppliedSettings{Err: clientError(err)}
}
return
}
settings, err := decodeSettings(response.Resource.ValueJson)
out <- AppliedSettings{Settings: settings, Version: response.Resource.Version, Err: err}
}
}()
return out, nil
}
type AppliedSettings struct {
Settings workflow.ExecutionSettings
Version int64
Err error
}
func (c *Client) Personas(ctx context.Context) (agent.PersonaCollection, int64, error) {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: PersonasKind})
if err != nil {
return agent.PersonaCollection{}, 0, clientError(err)
}
collection, err := decodePersonas(response.Resource.ValueJson)
return collection, response.Resource.Version, err
}
type AppliedPersonas struct {
Collection agent.PersonaCollection
Version int64
Err error
}
func (c *Client) WatchPersonas(ctx context.Context, afterVersion int64) (<-chan AppliedPersonas, error) {
stream, err := c.rpc.Watch(ctx, &pb.WatchRequest{Kind: PersonasKind, AfterVersion: afterVersion})
if err != nil {
return nil, clientError(err)
}
out := make(chan AppliedPersonas)
go func() {
defer close(out)
for {
response, err := stream.Recv()
if err != nil {
if !errors.Is(err, io.EOF) && ctx.Err() == nil {
out <- AppliedPersonas{Err: clientError(err)}
}
return
}
collection, err := decodePersonas(response.Resource.ValueJson)
out <- AppliedPersonas{Collection: collection, Version: response.Resource.Version, Err: err}
}
}()
return out, nil
}
func (c *Client) Schedules(ctx context.Context) ([]cronstore.JobSpec, int64, error) {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: SchedulesKind})
if err != nil {
return nil, 0, clientError(err)
}
var jobs []cronstore.JobSpec
if err := json.Unmarshal(response.Resource.ValueJson, &jobs); err != nil {
return nil, 0, err
}
return jobs, response.Resource.Version, nil
}
func (c *Client) ReplaceSchedules(ctx context.Context, jobs []cronstore.JobSpec, expectedVersion int64, actor, source, requestID string) (int64, error) {
value, err := json.Marshal(jobs)
if err != nil {
return 0, err
}
response, err := c.rpc.Command(ctx, &pb.CommandRequest{Kind: SchedulesKind, Command: "replace", ValueJson: value, ExpectedVersion: expectedVersion, Actor: actor, Source: source, RequestId: requestID})
if err != nil {
return 0, clientError(err)
}
return response.Resource.Version, nil
}
func clientError(err error) error {
switch status.Code(err) {
case codes.InvalidArgument:
return fmt.Errorf("%w: %w", ErrValidation, err)
case codes.NotFound:
return fmt.Errorf("%w: %w", ErrNotFound, err)
case codes.Aborted:
return fmt.Errorf("%w: %w", ErrVersionConflict, err)
case codes.Unavailable:
return fmt.Errorf("%w: %w", ErrUnavailable, err)
default:
return err
}
}
+131
View File
@@ -0,0 +1,131 @@
// Package controlplane owns control-plane resource registration, dispatch, and validation.
package controlplane
import (
"context"
"errors"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/store"
)
var (
ErrValidation = errors.New("control-plane validation")
ErrNotFound = errors.New("control-plane resource not found")
ErrVersionConflict = errors.New("control-plane version conflict")
ErrUnavailable = errors.New("control-plane unavailable")
)
type resourceStore interface {
Resource(context.Context, string) (store.Resource, error)
PutResource(context.Context, store.ResourceWrite) (store.Resource, error)
}
type Server struct {
pb.UnimplementedControlPlaneServiceServer
store resourceStore
definitions map[string]Definition
ordered []Definition
}
func NewServer(resources resourceStore) *Server {
definitions := builtinDefinitions()
byKind := make(map[string]Definition, len(definitions))
for _, definition := range definitions {
byKind[definition.Kind] = definition
}
return &Server{store: resources, definitions: byKind, ordered: definitions}
}
func (s *Server) Catalog(context.Context, *pb.CatalogRequest) (*pb.CatalogResponse, error) {
descriptors := make([]*pb.ResourceDescriptor, 0, len(s.ordered)+len(domainManagedDescriptors()))
for _, definition := range s.ordered {
descriptors = append(descriptors, definition.Descriptor())
}
descriptors = append(descriptors, domainManagedDescriptors()...)
return &pb.CatalogResponse{Resources: descriptors}, nil
}
func (s *Server) Query(ctx context.Context, request *pb.QueryRequest) (*pb.QueryResponse, error) {
if _, ok := s.definitions[request.GetKind()]; !ok {
return nil, status.Error(codes.NotFound, "resource not found")
}
resource, err := s.store.Resource(ctx, request.Kind)
if err != nil {
return nil, mapError(err)
}
return &pb.QueryResponse{Resource: resourceProto(resource)}, nil
}
func (s *Server) Command(ctx context.Context, request *pb.CommandRequest) (*pb.CommandResponse, error) {
definition, ok := s.definitions[request.GetKind()]
if !ok || request.GetCommand() != "replace" {
return nil, status.Error(codes.NotFound, "resource or command not found")
}
if request.GetActor() == "" || request.GetSource() == "" || request.GetRequestId() == "" {
return nil, status.Error(codes.InvalidArgument, "actor, source, and request ID are required")
}
value, err := definition.Decode(request.ValueJson)
if err != nil {
return nil, mapError(err)
}
resource, err := s.store.PutResource(ctx, store.ResourceWrite{Kind: request.Kind, Value: value, Actor: request.Actor, Source: request.Source, RequestID: request.RequestId, ExpectedVersion: request.ExpectedVersion, At: time.Now().UTC()})
if err != nil {
return nil, mapError(err)
}
return &pb.CommandResponse{Resource: resourceProto(resource)}, nil
}
func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_WatchServer) error {
if _, ok := s.definitions[request.GetKind()]; !ok {
return status.Error(codes.NotFound, "resource not found")
}
version := request.AfterVersion
ticker := time.NewTicker(250 * time.Millisecond)
defer ticker.Stop()
for {
resource, err := s.store.Resource(stream.Context(), request.Kind)
if err == nil && resource.Version > version {
if err := stream.Send(&pb.WatchResponse{Resource: resourceProto(resource)}); err != nil {
return err
}
version = resource.Version
} else if err != nil && !errors.Is(err, store.ErrResourceNotFound) {
return mapError(err)
}
select {
case <-stream.Context().Done():
return stream.Context().Err()
case <-ticker.C:
}
}
}
func resourceProto(resource store.Resource) *pb.Resource {
return &pb.Resource{Kind: resource.Kind, Version: resource.Version, ValueJson: resource.Value, UpdatedAt: timestamp(resource.At)}
}
func timestamp(value time.Time) *timestamppb.Timestamp {
if value.IsZero() {
return nil
}
return timestamppb.New(value)
}
func mapError(err error) error {
switch {
case errors.Is(err, ErrValidation):
return status.Error(codes.InvalidArgument, err.Error())
case errors.Is(err, store.ErrResourceNotFound):
return status.Error(codes.NotFound, err.Error())
case errors.Is(err, store.ErrResourceVersionConflict):
return status.Error(codes.Aborted, err.Error())
default:
return status.Error(codes.Unavailable, err.Error())
}
}
@@ -0,0 +1,202 @@
package controlplane
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/agent"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/store"
)
func TestCatalogCarriesShippedWorkflowDefinitionsForRestore(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
catalog, err := NewServer(resources).Catalog(t.Context(), &pb.CatalogRequest{})
if err != nil {
t.Fatal(err)
}
var defaults string
for _, resource := range catalog.Resources {
if resource.Kind == WorkflowDefinitionsKind {
defaults = resource.DefaultsJson
}
}
if defaults == "" {
t.Fatal("workflow-definitions descriptor carries no defaults")
}
collection, err := workflow.DecodeDefinitionCollection([]byte(defaults), workflow.BuiltinStepRegistry())
if err != nil {
t.Fatal(err)
}
if _, ok := collection.DefinitionByID("remediate"); !ok {
t.Fatalf("shipped defaults = %#v", collection.Definitions)
}
}
func TestImportConfigSeedsWorkflowDefinitionsWithoutOverwritingOverride(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := NewServer(resources)
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), WorkflowDefinitionsKind)
if err != nil {
t.Fatal(err)
}
definitions, err := workflow.DecodeDefinitionCollection(resource.Value, workflow.BuiltinStepRegistry())
if err != nil {
t.Fatal(err)
}
if _, ok := definitions.DefinitionByID("remediate"); !ok {
t.Fatal("seeded definitions omit remediate")
}
custom := workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "custom", YAML: "id: custom\nsteps:\n - type: bootstrap.apply\n"}}}
value, err := encodeWorkflowDefinitions(custom)
if err != nil {
t.Fatal(err)
}
if _, err := resources.PutResource(t.Context(), store.ResourceWrite{Kind: WorkflowDefinitionsKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "override"}); err != nil {
t.Fatal(err)
}
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
got, err := resources.Resource(t.Context(), WorkflowDefinitionsKind)
if err != nil {
t.Fatal(err)
}
if got.Version != 2 {
t.Fatalf("version after reimport = %d, want 2", got.Version)
}
}
func TestImportConfigSeedsPersonasWithoutOverwritingEdits(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := NewServer(resources)
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), PersonasKind)
if err != nil {
t.Fatal(err)
}
collection, err := decodePersonas(resource.Value)
if err != nil || len(collection.Personas) != len(agent.ShippedPersonas().Personas) {
t.Fatalf("seeded personas = %+v, %v", collection, err)
}
collection.Personas[0].Prompt = "edited"
value, err := json.Marshal(collection)
if err != nil {
t.Fatal(err)
}
if _, err := resources.PutResource(t.Context(), store.ResourceWrite{Kind: PersonasKind, Value: value, ExpectedVersion: resource.Version, Actor: "test", Source: "test", RequestID: "persona-edit"}); err != nil {
t.Fatal(err)
}
if _, err := server.ImportConfig(t.Context(), config.Config{}); err != nil {
t.Fatal(err)
}
got, err := resources.Resource(t.Context(), PersonasKind)
if err != nil {
t.Fatal(err)
}
updated, err := decodePersonas(got.Value)
if err != nil || updated.Personas[0].Prompt != "edited" || got.Version != 2 {
t.Fatalf("personas after reimport = %+v (version %d), %v", updated, got.Version, err)
}
}
func TestImportWorkflowExecutionSettingsDoesNotOverwriteExistingValue(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := NewServer(resources)
first := workflow.ExecutionSettings{MaxModelToolSteps: 10, MaxRuntime: time.Minute, MaxConsecutiveGateFailures: 2}
if _, err := server.ImportWorkflowExecutionSettings(t.Context(), first); err != nil {
t.Fatal(err)
}
if _, err := server.ImportWorkflowExecutionSettings(t.Context(), workflow.ExecutionSettings{MaxModelToolSteps: 99, MaxRuntime: time.Hour, MaxConsecutiveGateFailures: 9}); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), WorkflowExecutionSettingsKind)
if err != nil {
t.Fatal(err)
}
got, err := decodeSettings(resource.Value)
if err != nil {
t.Fatal(err)
}
if got != first || resource.Version != 1 {
t.Fatalf("imported settings = (%+v, version %d), want (%+v, version 1)", got, resource.Version, first)
}
}
func TestRegistryRoutesAndValidatesDefinitions(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := NewServer(resources)
if _, err := server.ImportConfig(t.Context(), config.Config{Containers: config.ContainerConfig{PullPolicy: "missing"}}); err != nil {
t.Fatal(err)
}
catalog, err := server.Catalog(t.Context(), &pb.CatalogRequest{})
if err != nil {
t.Fatal(err)
}
found := false
for _, descriptor := range catalog.Resources {
if descriptor.Kind == ContainerRuntimePoliciesKind {
found = descriptor.ApplyMode == "restart-required"
}
}
if !found {
t.Fatal("container runtime policies missing restart-required descriptor")
}
_, err = server.Command(t.Context(), &pb.CommandRequest{Kind: ContainerRuntimePoliciesKind, Command: "replace", ValueJson: []byte(`{"pull_policy":"sometimes"}`), Actor: "test", Source: "test", RequestId: "bad", ExpectedVersion: 1})
if err == nil {
t.Fatal("invalid pull policy accepted")
}
}
func TestProviderSeedKeepsReferencesAndNeverResolvedSecrets(t *testing.T) {
t.Parallel()
resources := store.OpenTest(t)
defer resources.Close()
server := NewServer(resources)
cfg := config.Config{Providers: map[string]config.Provider{"openai": {Class: "openai", APIKeyEnv: "OPENAI_API_KEY", APIKey: config.SecretRef{Engine: "env", Key: "OPENAI_API_KEY"}}}}
if _, err := server.ImportConfig(context.Background(), cfg); err != nil {
t.Fatal(err)
}
resource, err := resources.Resource(t.Context(), ProviderSettingsKind)
if err != nil {
t.Fatal(err)
}
var value map[string]any
if err := json.Unmarshal(resource.Value, &value); err != nil {
t.Fatal(err)
}
encoded := string(resource.Value)
if encoded == "" || !json.Valid(resource.Value) {
t.Fatalf("provider resource = %q", encoded)
}
if _, ok := value["openai"]; !ok {
t.Fatalf("provider resource = %s", encoded)
}
}
+25
View File
@@ -0,0 +1,25 @@
package controlplane
import pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
const (
objectSchema = `{"type":"object"}`
arraySchema = `{"type":"array"}`
)
func builtinDefinitions() []Definition {
definitions := []Definition{workflowDefinition(), workflowDefinitionsDefinition(), personaDefinition(), scheduleDefinition()}
definitions = append(definitions, modelDefinitions()...)
definitions = append(definitions, operationalDefinitions()...)
return definitions
}
func domainManagedDescriptors() []*pb.ResourceDescriptor {
const stateService = "state.v1.StateStoreService"
return []*pb.ResourceDescriptor{
{Kind: "identities", Title: "Identities", ApplyMode: "domain-managed", QueryService: stateService + "/ListIdentities,GetIdentity", CommandService: stateService + "/CreateIdentity,RenameIdentity,SuspendIdentity,ReactivateIdentity,RetireIdentity"},
{Kind: "capture-events", Title: "Captured events", ApplyMode: "domain-managed", QueryService: stateService + "/StreamCaptures", CommandService: stateService + "/InsertCapture"},
{Kind: "capture-mappings", Title: "Capture mappings", ApplyMode: "domain-managed", QueryService: stateService + "/ListMappings", CommandService: stateService + "/InsertMapping,UpdateMapping,DeleteMapping"},
{Kind: "capture-bindings", Title: "Capture bindings", ApplyMode: "domain-managed", QueryService: stateService + "/ListBindings", CommandService: stateService + "/InsertBinding,UpdateBinding,DeleteBinding"},
}
}
@@ -0,0 +1,65 @@
package controlplane
import (
"fmt"
"net/url"
"strings"
"github.com/samcharles93/archie-core/internal/config"
)
const (
ProviderSettingsKind = "provider-settings"
ModelRoleAssignmentsKind = "model-role-assignments"
)
type providerDocument struct {
Class string `json:"class"`
APIKeyEnv string `json:"api_key_env,omitempty"`
APIKey config.SecretRef `json:"api_key_ref"`
BaseURL string `json:"base_url,omitempty"`
HasCredential bool `json:"has_credential"`
}
func modelDefinitions() []Definition {
return []Definition{
{Kind: ProviderSettingsKind, Title: "Providers", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedProviders, Validate: validateProviders},
{Kind: ModelRoleAssignmentsKind, Title: "Model role assignments", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any { return cfg.Models }, Validate: validateModelRoles},
}
}
func seedProviders(cfg config.Config) any {
out := make(map[string]providerDocument, len(cfg.Providers))
for name, provider := range cfg.Providers {
out[name] = providerDocument{Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, APIKey: provider.APIKey, BaseURL: provider.BaseURL, HasCredential: provider.APIKeyEnv != "" || provider.APIKey != (config.SecretRef{})}
}
return out
}
func validateProviders(input []byte) error {
return validateAs(input, func(providers map[string]providerDocument) error {
for name, provider := range providers {
if strings.TrimSpace(name) == "" || strings.TrimSpace(provider.Class) == "" {
return fmt.Errorf("provider name and class are required")
}
if provider.BaseURL != "" {
parsed, err := url.Parse(provider.BaseURL)
if err != nil || parsed.Scheme == "" || parsed.Host == "" || parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" {
return fmt.Errorf("provider %q has invalid base_url", name)
}
}
}
return nil
})
}
func validateModelRoles(input []byte) error {
return validateAs(input, func(models map[string]string) error {
for role, model := range models {
if strings.TrimSpace(role) == "" || !strings.Contains(model, "/") {
return fmt.Errorf("model role %q must reference provider/model", role)
}
}
return nil
})
}
@@ -0,0 +1,89 @@
package controlplane
import (
"fmt"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
const (
RepositoryPoliciesKind = "repository-policies"
ChannelSettingsKind = "channel-settings"
SchedulingPolicyKind = "scheduling-policy"
ToolSettingsKind = "tool-settings"
PluginSettingsKind = "plugin-settings"
ContainerRuntimePoliciesKind = "container-runtime-policies"
)
type schedulingPolicy struct {
PollInterval string `json:"poll_interval"`
MaxRetries int `json:"max_retries"`
Dispatch config.Dispatch `json:"dispatch"`
}
type pluginSettings struct {
PluginDir string `json:"plugin_dir"`
ModuleDir string `json:"module_dir"`
SecretEngineDir string `json:"secret_engine_dir"`
SkillsDir string `json:"skills_dir"`
}
func operationalDefinitions() []Definition {
return []Definition{
{Kind: RepositoryPoliciesKind, Title: "Repository policies", ApplyMode: "restart-required", Schema: arraySchema, Seed: func(cfg config.Config) any { return cfg.Repos }, Validate: validateRepositories},
{Kind: ChannelSettingsKind, Title: "Channel settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedChannels, Validate: validateChannels},
{Kind: SchedulingPolicyKind, Title: "Scheduling policy", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any {
return schedulingPolicy{cfg.PollInterval.Std().String(), cfg.MaxRetries, cfg.Dispatch}
}, Validate: validateScheduling},
{Kind: ToolSettingsKind, Title: "Tool and MCP settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: seedTools, Validate: validateTools},
{Kind: PluginSettingsKind, Title: "Plugin settings", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any {
return pluginSettings{cfg.PluginDir, cfg.ModuleDir, cfg.SecretEngineDir, cfg.SkillsDir}
}, Validate: func(input []byte) error { return validateAs(input, func(pluginSettings) error { return nil }) }},
{Kind: ContainerRuntimePoliciesKind, Title: "Container runtime policies", ApplyMode: "restart-required", Schema: objectSchema, Seed: func(cfg config.Config) any { return cfg.Containers }, Validate: validateContainers},
}
}
func validateRepositories(input []byte) error {
return validateAs(input, func(repos []config.Repo) error {
seen := make(map[string]struct{}, len(repos))
for _, repo := range repos {
if strings.TrimSpace(repo.Owner) == "" || strings.TrimSpace(repo.Name) == "" {
return fmt.Errorf("repository owner and name are required")
}
if _, exists := seen[repo.FullName()]; exists {
return fmt.Errorf("duplicate repository %q", repo.FullName())
}
seen[repo.FullName()] = struct{}{}
}
return nil
})
}
func validateScheduling(input []byte) error {
return validateAs(input, func(policy schedulingPolicy) error {
if policy.MaxRetries < 0 {
return fmt.Errorf("max_retries must not be negative")
}
if policy.PollInterval != "0s" {
interval, err := time.ParseDuration(policy.PollInterval)
if err != nil || interval < 0 {
return fmt.Errorf("poll_interval must be a non-negative duration")
}
}
return nil
})
}
func validateContainers(input []byte) error {
return validateAs(input, func(settings config.ContainerConfig) error {
if settings.MaxConcurrency < 0 || time.Duration(settings.MaxUptime) < 0 || time.Duration(settings.VolumeTTL) < 0 {
return fmt.Errorf("container limits must not be negative")
}
if settings.PullPolicy != "" && settings.PullPolicy != "missing" && settings.PullPolicy != "always" {
return fmt.Errorf("pull_policy must be missing or always")
}
return nil
})
}
+29
View File
@@ -0,0 +1,29 @@
package controlplane
import (
"encoding/json"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/agent"
)
const PersonasKind = "personas"
func personaDefinition() Definition {
return Definition{
Kind: PersonasKind,
Title: "Personas",
Schema: objectSchema,
ApplyMode: "live",
Seed: func(config.Config) any { return agent.ShippedPersonas() },
Validate: func(input []byte) error {
return validateAs(input, func(collection agent.PersonaCollection) error { return collection.Validate() })
},
}
}
func decodePersonas(input []byte) (agent.PersonaCollection, error) {
var collection agent.PersonaCollection
err := json.Unmarshal(input, &collection)
return collection, err
}
+100
View File
@@ -0,0 +1,100 @@
package controlplane
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/store"
)
type Definition struct {
Kind string
Title string
Schema string
ApplyMode string
Seed func(config.Config) any
Validate func([]byte) error
Normalize func([]byte) ([]byte, error)
// Defaults is the factory value clients offer as "restore shipped". Only
// resources that ship with content set it.
Defaults func() any
}
func (d Definition) Descriptor() *pb.ResourceDescriptor {
descriptor := &pb.ResourceDescriptor{Kind: d.Kind, Title: d.Title, SchemaJson: d.Schema, Commands: []string{"replace"}, ApplyMode: d.ApplyMode}
if d.Defaults != nil {
if defaults, err := json.Marshal(d.Defaults()); err == nil {
descriptor.DefaultsJson = string(defaults)
}
}
return descriptor
}
func (d Definition) Decode(input []byte) ([]byte, error) {
if !json.Valid(input) {
return nil, fmt.Errorf("%w: invalid JSON", ErrValidation)
}
if err := d.Validate(input); err != nil {
return nil, fmt.Errorf("%w: %w", ErrValidation, err)
}
if d.Normalize != nil {
normalized, err := d.Normalize(input)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrValidation, err)
}
input = normalized
}
var value any
if err := json.Unmarshal(input, &value); err != nil {
return nil, fmt.Errorf("%w: %w", ErrValidation, err)
}
encoded, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("encode %s: %w", d.Kind, err)
}
return encoded, nil
}
func (s *Server) ImportConfig(ctx context.Context, cfg config.Config) (map[string]int64, error) {
versions := make(map[string]int64, len(s.ordered))
for _, definition := range s.ordered {
resource, err := s.store.Resource(ctx, definition.Kind)
if err == nil {
versions[definition.Kind] = resource.Version
continue
}
if !errors.Is(err, store.ErrResourceNotFound) {
return nil, err
}
value, err := json.Marshal(definition.Seed(cfg))
if err != nil {
return nil, fmt.Errorf("seed %s: %w", definition.Kind, err)
}
value, err = definition.Decode(value)
if err != nil {
return nil, fmt.Errorf("seed %s: %w", definition.Kind, err)
}
resource, err = s.store.PutResource(ctx, store.ResourceWrite{Kind: definition.Kind, Value: value, Actor: "system:migration", Source: "legacy-config", RequestID: "import:" + definition.Kind, ExpectedVersion: 0, At: time.Now().UTC()})
if err != nil {
return nil, fmt.Errorf("seed %s: %w", definition.Kind, err)
}
versions[definition.Kind] = resource.Version
}
return versions, nil
}
func validateAs[T any](input []byte, validate func(T) error) error {
var value T
decoder := json.NewDecoder(bytes.NewReader(input))
decoder.DisallowUnknownFields()
if err := decoder.Decode(&value); err != nil {
return err
}
return validate(value)
}
+131
View File
@@ -0,0 +1,131 @@
package controlplane
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
)
// RuntimeConfig applies restart-scoped database resources over bootstrap
// configuration. Values intentionally absent from a control-plane projection,
// such as secret-bearing MCP headers and channel update commands, remain
// bootstrap-owned.
func (c *Client) RuntimeConfig(ctx context.Context, base config.Config) (config.Config, error) {
out := base.Clone()
if err := c.query(ctx, ProviderSettingsKind, func(value []byte) error {
var providers map[string]providerDocument
if err := json.Unmarshal(value, &providers); err != nil {
return err
}
out.Providers = make(map[string]config.Provider, len(providers))
for name, provider := range providers {
out.Providers[name] = config.Provider{Class: provider.Class, APIKeyEnv: provider.APIKeyEnv, APIKey: provider.APIKey, BaseURL: provider.BaseURL}
}
return nil
}); err != nil {
return config.Config{}, err
}
if err := c.queryJSON(ctx, ModelRoleAssignmentsKind, &out.Models); err != nil {
return config.Config{}, err
}
if err := c.queryJSON(ctx, RepositoryPoliciesKind, &out.Repos); err != nil {
return config.Config{}, err
}
chat, err := c.RuntimeChatConfig(ctx, out.Chat)
if err != nil {
return config.Config{}, err
}
out.Chat = chat
if err := c.query(ctx, SchedulingPolicyKind, func(value []byte) error {
var policy schedulingPolicy
if err := json.Unmarshal(value, &policy); err != nil {
return err
}
interval, err := time.ParseDuration(policy.PollInterval)
if err != nil {
return err
}
out.PollInterval, out.MaxRetries, out.Dispatch = config.Duration(interval), policy.MaxRetries, policy.Dispatch
return nil
}); err != nil {
return config.Config{}, err
}
return c.runtimeToolConfig(ctx, out)
}
func (c *Client) runtimeToolConfig(ctx context.Context, out config.Config) (config.Config, error) {
if err := c.query(ctx, ToolSettingsKind, func(value []byte) error {
var settings toolSettings
if err := json.Unmarshal(value, &settings); err != nil {
return err
}
headers := make(map[string]map[string]string, len(out.Tools.MCPServers))
for _, server := range out.Tools.MCPServers {
headers[server.Name] = server.Headers
}
servers := make([]config.MCPServer, 0, len(settings.MCPServers))
for _, server := range settings.MCPServers {
servers = append(servers, config.MCPServer{Name: server.Name, Transport: server.Transport, Command: server.Command, Args: server.Args, WorkDir: server.WorkDir, URL: server.URL, Headers: headers[server.Name], SSEEndpoint: server.SSEEndpoint, MessageEndpoint: server.MessageEndpoint})
}
out.Tools = config.ToolsConfig{MCPServers: servers, Policy: settings.Policy, WebFetch: settings.WebFetch, Minimax: config.MinimaxConfig{Enabled: settings.Minimax.Enabled, APIKey: settings.Minimax.APIKey, BaseURL: settings.Minimax.BaseURL}}
return nil
}); err != nil {
return config.Config{}, err
}
if err := c.query(ctx, PluginSettingsKind, func(value []byte) error {
var settings pluginSettings
if err := json.Unmarshal(value, &settings); err != nil {
return err
}
out.PluginDir, out.ModuleDir, out.SecretEngineDir, out.SkillsDir = settings.PluginDir, settings.ModuleDir, settings.SecretEngineDir, settings.SkillsDir
return nil
}); err != nil {
return config.Config{}, err
}
if err := c.queryJSON(ctx, ContainerRuntimePoliciesKind, &out.Containers); err != nil {
return config.Config{}, err
}
return out, nil
}
func (c *Client) RuntimeChatConfig(ctx context.Context, base config.ChatConfig) (config.ChatConfig, error) {
out := base
err := c.query(ctx, ChannelSettingsKind, func(value []byte) error {
var settings channelSettings
if err := json.Unmarshal(value, &settings); err != nil {
return err
}
check, install := base.Telegram.UpdateCheckCommand, base.Telegram.UpdateInstallCommand
out = config.ChatConfig{
Operator: settings.Operator, ShowToolCalls: settings.ShowToolCalls, MaxSteps: settings.MaxSteps,
Models: settings.Models, Email: settings.Email, WebhookAddr: settings.WebhookAddr,
Webhook: config.WebhookRoute{Path: settings.Webhook.Path, Secret: settings.Webhook.Secret, Template: settings.Webhook.Template, DeliverTo: settings.Webhook.DeliverTo},
Telegram: config.TelegramConfig{AllowedUserIDs: settings.Telegram.AllowedUserIDs, Token: settings.Telegram.Token, TokenEnv: settings.Telegram.TokenEnv, UpdateCheckCommand: check, UpdateInstallCommand: install},
RateLimit: settings.RateLimit, UnrestrictedFilesystem: settings.UnrestrictedFilesystem, Workspace: settings.Workspace,
}
return nil
})
return out, err
}
func (c *Client) queryJSON(ctx context.Context, kind string, target any) error {
return c.query(ctx, kind, func(value []byte) error { return json.Unmarshal(value, target) })
}
func (c *Client) query(ctx context.Context, kind string, decode func([]byte) error) error {
response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: kind})
if err != nil {
return clientError(err)
}
if response.Resource == nil {
return fmt.Errorf("%s resource missing", kind)
}
if err := decode(response.Resource.ValueJson); err != nil {
return fmt.Errorf("decode %s: %w", kind, err)
}
return nil
}
@@ -0,0 +1,67 @@
package controlplane
import (
"context"
"encoding/json"
"testing"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
)
type runtimeConfigClient struct{ values map[string]any }
func (*runtimeConfigClient) Catalog(context.Context, *pb.CatalogRequest, ...grpc.CallOption) (*pb.CatalogResponse, error) {
return &pb.CatalogResponse{}, nil
}
func (c *runtimeConfigClient) Query(_ context.Context, request *pb.QueryRequest, _ ...grpc.CallOption) (*pb.QueryResponse, error) {
value, err := json.Marshal(c.values[request.Kind])
return &pb.QueryResponse{Resource: &pb.Resource{Kind: request.Kind, Version: 2, ValueJson: value}}, err
}
func (*runtimeConfigClient) Command(context.Context, *pb.CommandRequest, ...grpc.CallOption) (*pb.CommandResponse, error) {
panic("unexpected Command")
}
func (*runtimeConfigClient) Watch(context.Context, *pb.WatchRequest, ...grpc.CallOption) (grpc.ServerStreamingClient[pb.WatchResponse], error) {
panic("unexpected Watch")
}
func TestRuntimeConfigUsesDatabaseResourcesAndPreservesBootstrapOnlySecrets(t *testing.T) {
base := config.Config{
Providers: map[string]config.Provider{"old": {Class: "openai"}},
Models: map[string]string{"builder": "old/model"},
Repos: []config.Repo{{Owner: "old", Name: "repo"}},
Chat: config.ChatConfig{Telegram: config.TelegramConfig{
UpdateCheckCommand: []string{"check"}, UpdateInstallCommand: []string{"install"},
}},
Tools: config.ToolsConfig{MCPServers: []config.MCPServer{{Name: "docs", Headers: map[string]string{"Authorization": "secret"}}}},
}
client := NewRPCClient(&runtimeConfigClient{values: map[string]any{
ProviderSettingsKind: map[string]any{"main": map[string]any{"class": "openai", "base_url": "https://api.example.com", "api_key_ref": map[string]any{"engine": "env", "key": "API_KEY"}, "has_credential": true}},
ModelRoleAssignmentsKind: map[string]string{"builder": "main/model"},
RepositoryPoliciesKind: []config.Repo{{Owner: "acme", Name: "widget"}},
ChannelSettingsKind: map[string]any{"operator": "Sam", "show_tool_calls": true, "max_steps": 20, "models": []string{"main/model"}, "email": map[string]any{}, "webhook_addr": "", "webhook": map[string]any{}, "telegram": map[string]any{"allowed_user_ids": []int64{42}, "credential_configured": false}, "rate_limit": map[string]any{}, "unrestricted_filesystem": false, "workspace": "/workspace"},
SchedulingPolicyKind: map[string]any{"poll_interval": "2m", "max_retries": 7, "dispatch": map[string]any{"trigger": "assignee"}},
ToolSettingsKind: map[string]any{"mcp_servers": []map[string]any{{"name": "docs", "transport": "http", "url": "https://mcp.example.com", "headers_configured": true}}, "policy": map[string]any{}, "web_fetch": map[string]any{}, "minimax": map[string]any{"enabled": false, "credential_configured": false}},
PluginSettingsKind: map[string]any{"plugin_dir": "/plugins", "module_dir": "/modules", "secret_engine_dir": "/secrets", "skills_dir": "/skills"},
ContainerRuntimePoliciesKind: config.ContainerConfig{LegacyEnabled: true, Image: "archie:next", PullPolicy: "missing"},
}})
got, err := client.RuntimeConfig(t.Context(), base)
if err != nil {
t.Fatalf("RuntimeConfig: %v", err)
}
if got.Models["builder"] != "main/model" || got.Repos[0].FullName() != "acme/widget" || got.MaxRetries != 7 || got.PluginDir != "/plugins" || got.Containers.Image != "archie:next" {
t.Fatalf("database settings not applied: %+v", got)
}
if got.Chat.Operator != "Sam" || got.Chat.Telegram.AllowedUserIDs[0] != 42 {
t.Fatalf("channel settings not applied: %+v", got.Chat)
}
if got.Chat.Telegram.UpdateCheckCommand[0] != "check" || got.Tools.MCPServers[0].Headers["Authorization"] != "secret" {
t.Fatalf("bootstrap-only secret-backed settings were dropped: chat=%+v tools=%+v", got.Chat, got.Tools)
}
}
+74
View File
@@ -0,0 +1,74 @@
package controlplane
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/scheduling"
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
)
const SchedulesKind = "schedules"
func scheduleDefinition() Definition {
return Definition{
Kind: SchedulesKind,
Title: "Schedules",
Schema: arraySchema,
ApplyMode: "live",
Seed: func(config.Config) any { return []cronstore.JobSpec{} },
Validate: func(input []byte) error {
return validateAs(input, validateSchedules)
},
Normalize: normalizeSchedules,
}
}
func validateSchedules(jobs []cronstore.JobSpec) error {
seen := make(map[string]struct{}, len(jobs))
for _, job := range jobs {
if strings.TrimSpace(job.ID) == "" {
return fmt.Errorf("schedule ID is required")
}
if _, exists := seen[job.ID]; exists {
return fmt.Errorf("duplicate schedule %q", job.ID)
}
seen[job.ID] = struct{}{}
if job.Kind != cronstore.KindWorkflow {
return fmt.Errorf("schedule %q has unsupported kind %q", job.ID, job.Kind)
}
if err := job.Schedule.Validate(); err != nil {
return err
}
if err := scheduling.Pool(job.Pool).Validate(); err != nil {
return err
}
}
return nil
}
func normalizeSchedules(input []byte) ([]byte, error) {
var jobs []cronstore.JobSpec
if err := json.Unmarshal(input, &jobs); err != nil {
return nil, err
}
now := time.Now().UTC()
for i := range jobs {
jobs[i].Schedule = jobs[i].Schedule.Resolved()
if jobs[i].NextRun.IsZero() {
next, err := jobs[i].Schedule.FirstRun(now)
if err != nil {
return nil, err
}
jobs[i].NextRun = next.UTC()
}
if jobs[i].Created.IsZero() {
jobs[i].Created = now
}
jobs[i].Updated = now
}
return json.Marshal(jobs)
}
@@ -0,0 +1,38 @@
package controlplane
import (
"encoding/json"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/infrastructure/cronstore"
)
func TestScheduleDefinitionComputesFirstRunAndRejectsDuplicateIDs(t *testing.T) {
t.Parallel()
definition := scheduleDefinition()
jobs := []cronstore.JobSpec{{ID: "daily", Kind: cronstore.KindWorkflow, Schedule: cronstore.Schedule{Kind: cronstore.ScheduleInterval, Interval: time.Hour}}}
input, err := json.Marshal(jobs)
if err != nil {
t.Fatal(err)
}
got, err := definition.Decode(input)
if err != nil {
t.Fatal(err)
}
var normalized []cronstore.JobSpec
if err := json.Unmarshal(got, &normalized); err != nil {
t.Fatal(err)
}
if len(normalized) != 1 || normalized[0].NextRun.IsZero() || normalized[0].Created.IsZero() || normalized[0].Updated.IsZero() {
t.Fatalf("normalized schedules = %+v", normalized)
}
duplicate, err := json.Marshal(append(jobs, jobs[0]))
if err != nil {
t.Fatal(err)
}
if _, err := definition.Decode(duplicate); err == nil {
t.Fatal("duplicate schedule IDs accepted")
}
}
@@ -0,0 +1,70 @@
package controlplane
import (
"fmt"
"strings"
"github.com/samcharles93/archie-core/internal/config"
)
type toolSettings struct {
MCPServers []mcpServerSettings `json:"mcp_servers"`
Policy config.ToolPolicy `json:"policy"`
WebFetch config.WebFetchConfig `json:"web_fetch"`
Minimax minimaxSettings `json:"minimax"`
}
type mcpServerSettings struct {
Name string `json:"name"`
Transport string `json:"transport"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
WorkDir string `json:"work_dir,omitempty"`
URL string `json:"url,omitempty"`
SSEEndpoint string `json:"sse_endpoint,omitempty"`
MessageEndpoint string `json:"message_endpoint,omitempty"`
HeadersConfigured bool `json:"headers_configured"`
}
type minimaxSettings struct {
Enabled bool `json:"enabled"`
APIKey config.SecretRef `json:"api_key_ref"`
CredentialConfigured bool `json:"credential_configured"`
BaseURL string `json:"base_url,omitempty"`
}
func seedTools(cfg config.Config) any {
servers := make([]mcpServerSettings, 0, len(cfg.Tools.MCPServers))
for _, server := range cfg.Tools.MCPServers {
servers = append(servers, mcpServerSettings{Name: server.Name, Transport: server.Transport, Command: server.Command, Args: server.Args, WorkDir: server.WorkDir, URL: server.URL, SSEEndpoint: server.SSEEndpoint, MessageEndpoint: server.MessageEndpoint, HeadersConfigured: len(server.Headers) > 0})
}
return toolSettings{MCPServers: servers, Policy: cfg.Tools.Policy, WebFetch: cfg.Tools.WebFetch, Minimax: minimaxSettings{Enabled: cfg.Tools.Minimax.Enabled, APIKey: cfg.Tools.Minimax.APIKey, CredentialConfigured: cfg.Tools.Minimax.APIKey != (config.SecretRef{}), BaseURL: cfg.Tools.Minimax.BaseURL}}
}
func validateTools(input []byte) error {
return validateAs(input, func(settings toolSettings) error {
for _, server := range settings.MCPServers {
transport := strings.ToLower(strings.TrimSpace(server.Transport))
if server.Name == "" {
return fmt.Errorf("MCP server name is required")
}
switch transport {
case "", "stdio":
if server.Command == "" {
return fmt.Errorf("MCP stdio server %q requires command", server.Name)
}
case "http", "streamablehttp":
if server.URL == "" {
return fmt.Errorf("MCP http server %q requires url", server.Name)
}
case "sse":
if server.SSEEndpoint == "" {
return fmt.Errorf("MCP sse server %q requires sse_endpoint", server.Name)
}
default:
return fmt.Errorf("unknown MCP transport %q", server.Transport)
}
}
return nil
})
}
@@ -0,0 +1,37 @@
package controlplane
import (
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
const WorkflowDefinitionsKind = "workflow-definitions"
func workflowDefinitionsDefinition() Definition {
return Definition{
Kind: WorkflowDefinitionsKind,
Title: "Workflow definitions",
Schema: objectSchema,
ApplyMode: "live",
Seed: func(config.Config) any {
return workflow.ShippedDefinitions()
},
Defaults: func() any {
return workflow.ShippedDefinitions()
},
Validate: func(input []byte) error {
_, err := workflow.DecodeDefinitionCollection(input, workflow.BuiltinStepRegistry())
return err
},
}
}
func encodeWorkflowDefinitions(collection workflow.WorkflowDefinitionCollection) ([]byte, error) {
if err := workflow.ValidateDefinitionCollection(collection, workflow.BuiltinStepRegistry()); err != nil {
return nil, fmt.Errorf("%w: %w", ErrValidation, err)
}
return json.Marshal(collection)
}
@@ -0,0 +1,69 @@
package controlplane
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/store"
)
const WorkflowExecutionSettingsKind = "workflow-execution-settings"
type executionSettingsDocument struct {
MaxModelToolSteps int `json:"max_model_tool_steps"`
MaxRuntimeSeconds int64 `json:"max_runtime_seconds"`
MaxConsecutiveGateFailures int `json:"max_consecutive_gate_failures"`
}
// ImportWorkflowExecutionSettings preserves the focused bootstrap API for callers
// that do not own the complete legacy configuration document.
func (s *Server) ImportWorkflowExecutionSettings(ctx context.Context, settings workflow.ExecutionSettings) (int64, error) {
resource, err := s.store.Resource(ctx, WorkflowExecutionSettingsKind)
if err == nil {
return resource.Version, nil
}
if !errors.Is(err, store.ErrResourceNotFound) {
return 0, err
}
value, err := encodeSettings(settings)
if err != nil {
return 0, err
}
value, err = workflowDefinition().Decode(value)
if err != nil {
return 0, err
}
resource, err = s.store.PutResource(ctx, store.ResourceWrite{Kind: WorkflowExecutionSettingsKind, Value: value, Actor: "system:migration", Source: "legacy-config", RequestID: "import:" + WorkflowExecutionSettingsKind, ExpectedVersion: 0, At: time.Now().UTC()})
return resource.Version, err
}
func workflowDefinition() Definition {
return Definition{
Kind: WorkflowExecutionSettingsKind, Title: "Workflow execution settings", ApplyMode: "live", Schema: objectSchema,
Seed: func(cfg config.Config) any {
return executionSettingsDocument{cfg.Budgets.MaxSteps, int64(time.Duration(cfg.Budgets.WallClock) / time.Second), cfg.Budgets.GateMaxFailures}
},
Validate: func(input []byte) error { _, err := decodeSettings(input); return err },
}
}
func encodeSettings(s workflow.ExecutionSettings) ([]byte, error) {
return json.Marshal(executionSettingsDocument{s.MaxModelToolSteps, int64(s.MaxRuntime / time.Second), s.MaxConsecutiveGateFailures})
}
func decodeSettings(value []byte) (workflow.ExecutionSettings, error) {
var document executionSettingsDocument
if err := json.Unmarshal(value, &document); err != nil {
return workflow.ExecutionSettings{}, fmt.Errorf("%w: invalid JSON: %w", ErrValidation, err)
}
settings := workflow.ExecutionSettings{MaxModelToolSteps: document.MaxModelToolSteps, MaxRuntime: time.Duration(document.MaxRuntimeSeconds) * time.Second, MaxConsecutiveGateFailures: document.MaxConsecutiveGateFailures}
if err := settings.Validate(); err != nil {
return workflow.ExecutionSettings{}, fmt.Errorf("%w: %w", ErrValidation, err)
}
return settings, nil
}