refactor(state-store): migrate workflow/intake/task lifecycle consumers to State Store (archie-core-8cda.4.5) (#773)

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.
This commit is contained in:
sam authored and GitHub committed 2026-09-07 04:28:10 +10:00
1 parent 5fe5ff6626
commit 9ef16151f4
8 files changed
+199 -53

No files matched your search

+6 -6
View File
@@ -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:
+41 -29
View File
@@ -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,
@@ -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{},
}
+9 -9
View File
@@ -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,
}
+7 -5
View File
@@ -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
}
@@ -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)
}
}
@@ -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}}}
}
+1 -1
View File
@@ -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,