From 9ef16151f404a9cc9ff02a2de827748b79098437 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 7 Sep 2026 04:28:10 +1000 Subject: [PATCH] refactor(state-store): migrate workflow/intake/task lifecycle consumers to State Store (archie-core-8cda.4.5) (#773) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Route the daemon's task-lifecycle / work-intake / workflow consumers (the TaskStore composite) through b.stateStore instead of reaching into b.st directly, the same additive composition pattern .4.4 used for the capture/mapping/binding contracts (docs/prds/state-store-contract.md §10/§12 step 6). b.stateStore is the in-process *store.Store by default and a remote *staterpc.Client dialed to the standalone archie-state-store gRPC service when [services.state].target is set, so this is behaviour-neutral until an operator flips the seam. Consumers migrated: daemon.Daemon.Store, webui.Server.Store, the message gateway routers, the chat task lister/log-reader adapters, the operator task action service, event persistence (persistAndBroadcastEvents), the manual requeue path, and the chat task creator/controller. openStores now seeds b.stateStore = b.st so it is never nil in either the daemon (Run) or the gateway (RunGateway) composition; the gateway never calls openStateStoreAdapter, so it stays on the local adapter and never dials a remote store (preserving the .4.4 fail-closed intent). The in-process State Store gRPC server keeps fronting b.st (the agent's gRPC and NATS storerpc handoff paths both stay on the daemon's local store) and the in-process serving path is untouched (deletion is .4.6). Adds focused composition-regression tests asserting the daemon, webui and task-actions consumers resolve their store from b.stateStore, plus the existing bufconn conformance suite (grpc adapter) already proves contract fidelity for the TaskStore surface. --- config.example.toml | 12 +- internal/app/archied/bootstrap.go | 70 ++++++---- .../app/archied/channel_lifecycle_test.go | 1 + internal/app/archied/gateway_runtime.go | 18 +-- internal/app/archied/main.go | 12 +- .../app/archied/state_store_consumers_test.go | 130 ++++++++++++++++++ .../app/archied/task_action_adapter_test.go | 7 +- internal/app/archied/task_actions.go | 2 +- 8 files changed, 199 insertions(+), 53 deletions(-) create mode 100644 internal/app/archied/state_store_consumers_test.go diff --git a/config.example.toml b/config.example.toml index be0758bc5..eda28799a 100644 --- a/config.example.toml +++ b/config.example.toml @@ -274,12 +274,12 @@ gate_max_failures = 5 # # without a token fails closed (docs/prds/state-store-contract.md §9). # # The standalone archie-state-store binary (.4.3) consumes # # this surface; the daemon reads [services.state].target to -# # route its own capture/mapping/binding consumers to the -# # remote store (docs/prds/state-store-contract.md §10/§12 step 6). -# # Task lifecycle stays in-process and the daemon still serves -# # the State Store gRPC service in-process until that path is -# # deleted (.4.6); flipping this during .4.4 routes only the -# # capture/mapping/binding contracts remotely. +# # route its own capture/mapping/binding AND task-lifecycle / +# # workflow / intake consumers to the remote store +# # (docs/prds/state-store-contract.md §10/§12 step 6). The +# # daemon still serves the State Store gRPC service in-process +# # until that path is deleted (.4.6); staying empty (the +# # default) keeps every consumer on the in-process local store. # NATS is the sole autonomous workflow handoff. mode changes only where the # broker runs; every workflow still runs in a managed task container: diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 7d27e8b02..f1e866eda 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -103,13 +103,18 @@ type boot struct { st store.TaskStore // stateStore is the State Store contract adapter the daemon's own - // capture/mapping/binding consumers use. It is the in-process - // *store.Store (the same one b.st holds) unless [services.state].target - // is set, in which case it is a remote *staterpc.Client dialed to the - // standalone archie-state-store gRPC service. Task lifecycle (b.st) - // stays in-process until .4.5/.4.6; only capture/mapping/binding - // resolve through this field, so a consumer mixes in-process and remote - // contracts per contract-code while the daemon's own store stays local + // store consumers depend on. It is the in-process *store.Store (the + // same one b.st holds) unless [services.state].target is set, in which + // case it is a remote *staterpc.Client dialed to the standalone + // archie-state-store gRPC service. openStores seeds it from b.st so it + // is never nil in any composition (daemon or gateway); the daemon's + // openStateStoreAdapter upgrades it to the remote client when the seam + // is set (the gateway never calls openStateStoreAdapter, so it stays on + // the local adapter). During .4.5 the workflow/intake/task-lifecycle + // consumers (the TaskStore composite) resolve through this field, so a + // consumer mixes in-process and remote contracts per contract-code while + // the daemon's own local store stays in-process; the in-process State + // Store gRPC server keeps fronting b.st for the agent until .4.6 // (docs/prds/state-store-contract.md §12 step 6). stateStore store.TaskStore chatSessionStore gateway.SessionStore @@ -290,6 +295,13 @@ func (b *boot) openStores(ctx context.Context) error { return err } b.st = st + // Seed the State Store contract adapter from the just-opened in-process + // store so it is never nil in any composition (Run and RunGateway both + // open b.st here). The daemon's openStateStoreAdapter upgrades it to a + // remote *staterpc.Client when [services.state].target is set; the + // gateway never calls openStateStoreAdapter, so it stays on this local + // adapter and never dials remote (docs/prds/state-store-contract.md §10). + b.stateStore = st b.addCleanup(func() { if err := st.Close(); err != nil { log.Error("close store", "err", err) @@ -298,17 +310,17 @@ func (b *boot) openStores(ctx context.Context) error { return b.openChatSessions(ctx) } -// openStateStoreAdapter selects the State Store contract adapter the daemon's -// own capture/mapping/binding consumers will use. The base path is the local -// *store.Store opened by openStores; when [services.state].target is set the -// daemon instead dials the standalone archie-state-store gRPC service and -// uses *staterpc.Client. Empty target keeps the in-process adapter (the -// default -- the State Store service is not yet extracted into its own -// process), so this is additive and behaviour-neutral until an operator flips -// the seam (docs/prds/state-store-contract.md §10). Task lifecycle (b.st) is -// untouched here -- only the capture/mapping/binding contracts route through -// the adapter during .4.4. Called only from the daemon's composition (the -// gateway does not consume these surfaces), so it runs after openStores has +// openStateStoreAdapter upgrades the State Store contract adapter to the +// remote *staterpc.Client when [services.state].target is set; otherwise it +// leaves the in-process adapter seeded by openStores (the default -- the +// State Store service is not yet extracted into its own process), so this is +// additive and behaviour-neutral until an operator flips the seam +// (docs/prds/state-store-contract.md §10). openStores already seeds +// b.stateStore = b.st, so the empty-target branch is an idempotent no-op that +// keeps the default explicit. Called only from the daemon's composition (the +// gateway does not consume these surfaces via a remote adapter), so a gateway +// config that happens to carry [services.state].target can never dial (or fail +// closed against) a state store it does not use. It runs after openStores has // opened b.st and resolved b.secrets. func (b *boot) openStateStoreAdapter() error { if strings.TrimSpace(b.cfg.Services.State.Target) == "" { @@ -345,7 +357,7 @@ func (b *boot) handleRequeue(ctx context.Context, requeue int64, once bool) (boo if requeue <= 0 { return false, nil } - if err := manualRequeueTask(ctx, b.st, requeue); err != nil { + if err := manualRequeueTask(ctx, b.stateStore, requeue); err != nil { b.log.Error("requeue failed", "task", requeue, "err", err) return false, err } @@ -391,7 +403,7 @@ func (b *boot) setupObservability() { Path: origin.Path, Role: string(origin.Role), Layer: string(origin.Layer), Feature: string(origin.Feature), }) } - b.web = &webui.Server{Store: b.st, Log: log.With("component", "webui"), LogFeed: b.logFeed, TaskLogs: b.taskLogs, Cfg: config.NewHolder(cfg), Channels: b.channelManager, Events: bus} + b.web = &webui.Server{Store: b.stateStore, Log: log.With("component", "webui"), LogFeed: b.logFeed, TaskLogs: b.taskLogs, Cfg: config.NewHolder(cfg), Channels: b.channelManager, Events: bus} b.web.UpdateReportPath = updateReportPath(cfg.WorkDir, "webui") if (cfg.Chat.Telegram.Token != (secret.SecretRef{}) || cfg.Chat.Telegram.TokenEnv != "") && len(cfg.Chat.Telegram.AllowedUserIDs) > 0 { b.web.TelegramUpdateReportPath = updateReportPath(cfg.WorkDir, cfg.BotUser) @@ -412,7 +424,7 @@ func (b *boot) setupObservability() { b.web.Issues = b.forgeClient b.wireWebStoreSurfaces() sink := bus.Subscribe(256) - go persistAndBroadcastEvents(sink, b.st, b.web, log) + go persistAndBroadcastEvents(sink, b.stateStore, b.web, log) } // wireWebStoreSurfaces attaches the dashboard's optional storage surfaces. @@ -608,11 +620,11 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b cfg, log := b.cfg, b.log start, ok := setupTelegramGateway(ctx, telegramSetup{ Cfg: config.NewHolder(cfg), CfgPath: cfgPath, OverlayPath: overlayPath, - St: b.st, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, + St: b.stateStore, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, Personas: b.personas, ChatTasks: b.chatTasks, ChatController: b.chatController, - ChatTaskLister: chatTaskListerAdapter{tasks: b.st.Tasks}, + ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks}, ChatTaskLogs: chatTaskLogReaderAdapter{ - tasks: b.st.TaskByID, + tasks: b.stateStore.TaskByID, taskLogs: b.taskLogs, }, ChatTaskActor: chatTaskActorAdapter{ @@ -639,8 +651,8 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b // ── Email gateway (optional) ─────────────────────────────────── if cfg.Chat.Email.ListenAddr != "" { em := email.New(cfg.Chat.Email.ListenAddr, cfg.Chat.Email.RelayAddr, log) - emRouter := gateway.NewRouter(b.st, nil, "email") - configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.st.Tasks}, b.defaultChatIdentity) + emRouter := gateway.NewRouter(b.stateStore, nil, "email") + configureTaskCommands(emRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) b.startGateways = append(b.startGateways, func() { go func() { lifecycle := gateway.Lifecycle{ @@ -667,8 +679,8 @@ func (b *boot) setupGateways(ctx context.Context, cfgPath, overlayPath string) b []webhook.RouteConfig{{Path: "/webhook"}}, log, ) - whRouter := gateway.NewRouter(b.st, nil, "webhook") - configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.st.Tasks}, b.defaultChatIdentity) + whRouter := gateway.NewRouter(b.stateStore, nil, "webhook") + configureTaskCommands(whRouter, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) b.startGateways = append(b.startGateways, func() { go func() { lifecycle := gateway.Lifecycle{ @@ -1273,7 +1285,7 @@ func (b *boot) buildDaemon() { b.d = &daemon.Daemon{ Cfg: config.NewHolder(cfg), ConnectedNATS: daemon.NATSEndpoint{URL: b.natsURL, Token: b.natsToken}, - Store: b.st, + Store: b.stateStore, Bus: b.bus, Forge: b.forgeClient, Trees: b.trees, diff --git a/internal/app/archied/channel_lifecycle_test.go b/internal/app/archied/channel_lifecycle_test.go index 12448daff..e093da5a3 100644 --- a/internal/app/archied/channel_lifecycle_test.go +++ b/internal/app/archied/channel_lifecycle_test.go @@ -49,6 +49,7 @@ func TestConfiguredNetworkGatewaysBecomeRunning(t *testing.T) { cfg: test.cfg, log: slog.New(slog.DiscardHandler), st: st, + stateStore: st, channelManager: manager, web: &webui.Server{}, } diff --git a/internal/app/archied/gateway_runtime.go b/internal/app/archied/gateway_runtime.go index bc591687c..31ad7df62 100644 --- a/internal/app/archied/gateway_runtime.go +++ b/internal/app/archied/gateway_runtime.go @@ -33,16 +33,16 @@ func (b *boot) setupChatRuntime(cfg config.Config) { var chatTasks gateway.TaskCreator if len(profiles) > 0 { chatTasks = gateway.NewStoreTaskCreatorForProfiles( - chatTaskWriterAdapter{enqueue: b.st.EnqueueChatTask}, + chatTaskWriterAdapter{enqueue: b.stateStore.EnqueueChatTask}, profiles, ) } b.chatTasks = chatTasks b.defaultChatIdentity = defaultChatIdentity b.chatController = gateway.NewStoreTaskController(chatTaskControllerAdapter{ - taskByID: b.st.TaskByID, - requeue: b.st.Requeue, - transition: b.st.Transition, + taskByID: b.stateStore.TaskByID, + requeue: b.stateStore.Requeue, + transition: b.stateStore.Transition, }) b.updateService = makeUpdateService(telegramSetup{Cfg: config.NewHolder(cfg)}) } @@ -52,18 +52,18 @@ func (b *boot) setupChatRuntime(cfg config.Config) { func (b *boot) setupGatewayChat(ctx context.Context, actor gateway.ChatTaskActor) gateway.ChatContract { b.setupChatRuntime(b.cfg) cfg := b.cfg - router := gateway.NewRouter(b.st, nil, "web") + router := gateway.NewRouter(b.stateStore, nil, "web") router.Version = fmt.Sprintf("Archie\nGateway: %s\nRuntime: %s", gatewayVersion, runtimeVersion) router.Models = b.chatModels router.Personas = b.personas router.Updates = b.updateService router.InitSessions(b.chatSessionStore) - configureTaskCommands(router, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.st.Tasks}, b.defaultChatIdentity) + configureTaskCommands(router, b.chatTasks, b.chatController, chatTaskListerAdapter{tasks: b.stateStore.Tasks}, b.defaultChatIdentity) setup := telegramSetup{ - Cfg: config.NewHolder(cfg), St: b.st, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, + Cfg: config.NewHolder(cfg), St: b.stateStore, LLM: b.llm, ChatModels: b.chatModels, ToolReg: b.toolReg, Personas: b.personas, ChatTasks: b.chatTasks, ChatController: b.chatController, - ChatTaskLister: chatTaskListerAdapter{tasks: b.st.Tasks}, - ChatTaskLogs: chatTaskLogReaderAdapter{tasks: b.st.TaskByID, taskLogs: b.taskLogs}, + ChatTaskLister: chatTaskListerAdapter{tasks: b.stateStore.Tasks}, + ChatTaskLogs: chatTaskLogReaderAdapter{tasks: b.stateStore.TaskByID, taskLogs: b.taskLogs}, ChatTaskActor: actor, DefaultChatIdentity: b.defaultChatIdentity, SessionStore: b.chatSessionStore, Bus: b.bus, Log: b.log, Secrets: b.secrets, } diff --git a/internal/app/archied/main.go b/internal/app/archied/main.go index 19aefec9c..311a6fc10 100644 --- a/internal/app/archied/main.go +++ b/internal/app/archied/main.go @@ -378,11 +378,13 @@ func Run() int { //nolint:cyclop // the composition root's setup sequence is del if err := b.openStores(ctx); err != nil { return 1 } - // Resolve the State Store contract adapter the daemon's own - // capture/mapping/binding consumers use. This is daemon-only (the gateway - // does not consume those surfaces), so it runs after openStores has opened - // b.st and resolved b.secrets and before setupObservability wires the - // dashboard's storage surfaces (docs/prds/state-store-contract.md §10). + // Upgrade the State Store contract adapter to a remote *staterpc.Client + // when [services.state].target is set; openStores already seeded it from + // b.st (the local default), so an empty target is a no-op. This is + // daemon-only (the gateway never dials a remote store), so it runs after + // openStores has opened b.st and resolved b.secrets and before + // setupObservability wires the dashboard's storage surfaces + // (docs/prds/state-store-contract.md §10). if err := b.openStateStoreAdapter(); err != nil { return 1 } diff --git a/internal/app/archied/state_store_consumers_test.go b/internal/app/archied/state_store_consumers_test.go new file mode 100644 index 000000000..2b67a121e --- /dev/null +++ b/internal/app/archied/state_store_consumers_test.go @@ -0,0 +1,130 @@ +package archied + +import ( + "log/slog" + "path/filepath" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/daemon" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" + taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions" + "github.com/samcharles93/archie-core/internal/logging" + "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/webui" +) + +// openSecondStore opens a second, independent *store.Store on a fresh temp +// path so a boot consumer test can prove the composition routes through +// b.stateStore rather than b.st: the two differ by identity, so an assertion +// that a consumer field holds storeB (not storeA) can only pass if the wiring +// reads b.stateStore. +func openSecondStore(t *testing.T) *store.Store { + t.Helper() + st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "state-store.db")) + if err != nil { + t.Fatalf("open second store: %v", err) + } + t.Cleanup(func() { _ = st.Close() }) + return st +} + +// TestBuildDaemonRoutesTaskStoreThroughStateStore proves the daemon's task +// lifecycle consumer resolves its store surfaces from b.stateStore (the State +// Store contract adapter) -- local by default, remote *staterpc.Client when +// [services.state].target is set -- and not from b.st directly. This is the +// .4.5 TaskStore-composite swap (docs/prds/state-store-contract.md §12 step +// 6): the daemon's Store field and its mapping/binding dispatch surfaces all +// come from the adapter. +func TestBuildDaemonRoutesTaskStoreThroughStateStore(t *testing.T) { + storeA, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "store-a.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = storeA.Close() }) + storeB := openSecondStore(t) + + b := &boot{ + cfg: config.Config{}, + log: slog.New(slog.DiscardHandler), + st: storeA, + stateStore: storeB, + web: &webui.Server{}, + } + b.buildDaemon() + + if b.d.Store != storeB { + t.Fatalf("daemon Store = %p, want b.stateStore (%p); task lifecycle did not route through the State Store adapter", b.d.Store, storeB) + } + if b.d.Mappings != storeB { + t.Fatalf("daemon Mappings = %p, want b.stateStore (%p)", b.d.Mappings, storeB) + } + if b.d.Bindings != storeB { + t.Fatalf("daemon Bindings = %p, want b.stateStore (%p)", b.d.Bindings, storeB) + } + if b.d.BindingDispatcher != storeB { + t.Fatalf("daemon BindingDispatcher = %p, want b.stateStore (%p)", b.d.BindingDispatcher, storeB) + } + if b.d.BindingTaskCreator != storeB { + t.Fatalf("daemon BindingTaskCreator = %p, want b.stateStore (%p)", b.d.BindingTaskCreator, storeB) + } +} + +// TestSetupObservabilityRoutesTaskStoreThroughStateStore proves the dashboard's +// task store (web.Store) is wired from b.stateStore, not b.st, so the webui +// reaches the same contract adapter the daemon uses. +func TestSetupObservabilityRoutesTaskStoreThroughStateStore(t *testing.T) { + storeA, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "store-a.db")) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = storeA.Close() }) + storeB := openSecondStore(t) + + logFeed := logging.NewFeed(16) + b := &boot{ + cfg: config.Config{}, + log: slog.New(slog.DiscardHandler), + st: storeA, + stateStore: storeB, + doc: &configuration.Document{}, + agentStatus: &daemon.AgentStatus{}, + logFeed: logFeed, + taskLogs: logging.NewTaskRegistry(filepath.Join(t.TempDir(), "logs"), logFeed, logging.TaskSinkOptions{}), + } + t.Cleanup(b.cleanup) + b.setupObservability() + + if b.web.Store != storeB { + t.Fatalf("webui Store = %p, want b.stateStore (%p); dashboard task store did not route through the State Store adapter", b.web.Store, storeB) + } + // Capture/mapping/binding surfaces resolve from the same adapter. + if b.web.Captures != storeB { + t.Fatalf("webui Captures = %p, want b.stateStore (%p)", b.web.Captures, storeB) + } + if b.web.Mappings != storeB { + t.Fatalf("webui Mappings = %p, want b.stateStore (%p)", b.web.Mappings, storeB) + } +} + +// TestTaskActionsRoutesThroughStateStore proves the daemon's operator task +// action service wraps b.stateStore (the contract adapter), so an operator +// approve/cancel/archive action mutates the same store the daemon lifecycle +// uses -- local by default, remote when [services.state].target is set. +func TestTaskActionsRoutesThroughStateStore(t *testing.T) { + storeB := openSecondStore(t) + + b := &boot{ + cfg: config.Config{}, + log: slog.New(slog.DiscardHandler), + stateStore: storeB, + } + svc := b.taskActions() + shadow, ok := svc.Store.(taskactionstore.Store) + if !ok { + t.Fatalf("taskactions Service.Store = %T, want taskactionstore.Store", svc.Store) + } + if shadow.TaskStore != storeB { + t.Fatalf("taskactions TaskStore = %p, want b.stateStore (%p)", shadow.TaskStore, storeB) + } +} diff --git a/internal/app/archied/task_action_adapter_test.go b/internal/app/archied/task_action_adapter_test.go index 07e81774d..6fbed3554 100644 --- a/internal/app/archied/task_action_adapter_test.go +++ b/internal/app/archied/task_action_adapter_test.go @@ -37,9 +37,10 @@ func (a testTaskActor) ApplyChatTaskAction( func newChatTaskActorForTest(t *testing.T, st store.TaskStore, cfg config.Config) chatTaskActorAdapter { t.Helper() b := &boot{ - st: st, - cfg: cfg, - log: slog.Default(), + st: st, + stateStore: st, + cfg: cfg, + log: slog.Default(), } return chatTaskActorAdapter{contract: &gateway.LocalChatAdapter{TaskActor: testTaskActor{b}}} } diff --git a/internal/app/archied/task_actions.go b/internal/app/archied/task_actions.go index 32646d11f..af8e2264d 100644 --- a/internal/app/archied/task_actions.go +++ b/internal/app/archied/task_actions.go @@ -16,7 +16,7 @@ import ( // through the same constructor so the surfaces cannot diverge. func (b *boot) taskActions() taskactions.Service { return taskactionstore.NewService( - taskactionstore.Store{TaskStore: b.st}, + taskactionstore.Store{TaskStore: b.stateStore}, taskactionstore.MaxRetries(config.NewHolder(b.cfg)), b.cancelTask, b.closeIssue,