55 KiB
State Store — contract boundary & transport (ratification)
Status: Approved Revision: 2e Date: 2026-09-06
This is the pre-implementation design document that Phase 2 subtasks .4.2 (generalise
storerpc as the State Store gRPC contract) and .4.3 (stand up archie-state-store)
implement against.
Beads milestone: archie-core-8cda.4.1
Parent: docs/prds/service-decomposition.md (open-question Q4 RESOLVED)
This document fixes the contract boundary and transport for the State Store service.
It does not redesign Archie's domain model, does not invent a second store, and does not
split archie.db. It records method ownership, DTO/wire representation, error semantics,
limits, mode/config shape, local-adapter compatibility, and cutover/deletion rules so that
.4.2 and .4.3 have exactly one authoritative surface to build against.
Storage superseded. The store behind this contract is PostgreSQL:
internal/infrastructure/postgresimplements the producer-owned interfaces ininternal/domain/storecontractandworkflow.Store, over the databasedatabase_urlnames.internal/store, the SQLite file and the local adapter are gone; where this document saysarchie.db,*store.Storeorinternal/store, read the State Store's PostgreSQL database andinternal/infrastructure/postgres. The contract, ownership split, error semantics and cutover rules stand.
Current state — read before
.4.2starts. The §12 step 2 relocation (archie-core-8cda. 4.8) has landed:Task/Status*/Source*and the 3-methodworkflow.Storenow live ininternal/domain/workflow, the six production workflow files import nointernal/storesymbol, andstorerpc/the NATS transport assert againstworkflow.Storeinstead ofstore.WorkflowStore. The broader Phase 2 acceptance criterion (State Store as its own service behind gRPC) is still open — that is what.4.2/.4.3build next.
Relationship to migration-decisions §4 — not replaced.
docs/architecture/ migration-decisions.md§4 (store.Task → WorkflowExecution,mutable Stage → StepExecution history,RetryCount/Attempt → Attempt records, versioned Workflow defs) remains the authoritative, open document for the long-term Workflow migration. This PRD does not replace it. It only pulls the minimalTask/Status/Sourcetype + interface relocation forward as a Phase 2 prerequisite (because the acceptance criterion requires it); the rest of §4 stays its own migration and stays open.
1. Decision (the invariant everything follows)
Archie is composed such that a single archie-state-store service owns the one existing
SQLite file (archie.db) and exposes it over gRPC behind narrow typed contracts. There is
exactly one store, one file, one service. No database-per-service, no distributed
transactions, no second store abstraction.
Two adapter families satisfy the contract interfaces:
- Local adapter —
*store.Store(the in-process SQLite implementation). - Remote adapter —
*staterpc.Client(wraps the generated gRPC client).
Each adapter carries an explicit compile-time assertion against the contract interfaces it
serves. Composition picks local vs. remote by the presence of [services.state].target
(empty = local; set = dial the gRPC service). Default is local (the State Store is not yet
extracted), which is the PRD §3 posture; it differs from the gateway's remote-default only
because the gateway is already a separate deployment and the State Store is not.
Contract-ownership fork, decided (rev. 2c — resolves the domain contradiction). Ownership splits by the consumer's layer, per the dependency contract's rule #2 ("a domain defines the smallest interfaces required to perform its work") and rule #7 ("database rows … MUST NOT leak into domain models"):
- The workflow domain (
internal/domain/workflow) is the only domain-layer consumer of store state. It defines its own consumer-owned narrowStoreinterface (Update/Transition/InsertEvent) and its own domain-ownedTasktype plusStatus/Sourcevocabulary. It imports zerointernal/storesymbols — satisfying the Phase 2 acceptance criterion. This is the minimal bound of migration-decisions §4 (store.Task → WorkflowExecution), pulled forward because the acceptance criterion requires it.- The daemon / webui / intake store surfaces (
TaskStore,CaptureStore,MappingStore,BindingStore,BindingDispatcher,BindingTaskCreator) stay producer-owned ininternal/store, because those consumers are application and infrastructure layers, not domains — rule #2 does not apply to them.
So the State Store contract is not uniformly producer-owned: the domain-facing workflow
contract is consumer-owned (in the domain), while the daemon/webui store surfaces remain in
internal/store. internal/store imports internal/domain/workflow for the Task/Status/
Source types it persists (persistence → domain, the correct direction), and its Store
implementation satisfies workflow.Store. This is the single decision that makes the whole
Phase 2 acceptance criterion and the architecture's rules simultaneously satisfiable.
internal/storeis a TRANSITIONAL compatibility location (rev. 2c). The repository's target architecture (docs/architecture/dependencies-and-contracts.md,organisation.md) places persistence implementations underinternal/infrastructure/<capability>.internal/storeholding the task/capture/mapping/binding implementation and ALSO being the home of the producer-owned daemon/webui interfaces is acceptable only as a transitional location. The long-term home for the store implementation isinternal/infrastructure/store; the producer-owned interfaces may move to the same infrastructure package (or an approved contract location) when the store implementation relocates. Agents must not treatinternal/storeas the final infrastructure boundary.REV. 2d (archie-core-8cda.5.6): the producer-owned interfaces HAVE moved to an approved contract location —
internal/domain/storecontract(packagestorecontract), ahead of the store implementation's own relocation. The UI Service deletion gate required the archie-ui process to hold the contracts without linkinginternal/store's SQL implementation, andinternal/contracts/itself is ruled out: that tree is thebuf generateoutput andtools/proto.sh checkdeletes hand-written files there.internal/storekeeps type aliases (type TaskStore = storecontract.TaskStore, the sentinelvars, the value types) so daemon-side callers are unaffected; the sentinel message strings are unchanged and remain the wire contract (§4).
2. Contract boundary
Ownership split (see the fork in §1): the workflow-domain consumer contract is
workflow.Store (consumer-owned, in internal/domain/workflow); the daemon/webui/intake
store surfaces are the producer-owned interfaces in internal/store/interface.go.
store.WorkflowStore is superseded — it is replaced by the domain-owned workflow.Store,
and internal/store's Store implementation satisfies workflow.Store directly. TaskStore
remains the only composite; every other interface is flat. Narrow slices are split along
consumer-capability lines so no consumer acquires a surface it does not need
(interfacebloat cap = 8).
| Interface (owner) | Embeds | Methods | Local impl | Remote impl |
|---|---|---|---|---|
TaskStore (store) |
TaskLifecycle, TaskEvents, TaskQueries, TaskArchiver, TaskRetryer |
24 (union) | *Store |
*staterpc.Client |
TaskLifecycle (store) |
— | 8 | *Store |
*staterpc.Client |
TaskEvents (store) |
— | 7 | *Store |
*staterpc.Client |
TaskQueries (store) |
— | 7 | *Store |
*staterpc.Client |
TaskArchiver (store) |
— | 1 | *Store |
*staterpc.Client |
TaskRetryer (store) |
— | 1 | *Store |
*staterpc.Client |
workflow.Store (workflow domain) |
— | 3 | *Store |
*staterpc.Client |
CaptureStore (store) |
— | 2 | *Store |
*staterpc.Client |
MappingStore (store) |
— | 5 | *Store |
*staterpc.Client |
BindingStore (store) |
— | 6 | *Store |
*staterpc.Client |
BindingDispatcher (store) |
— | 3 | *Store |
*staterpc.Client |
BindingTaskCreator (store) |
— | 1 | *Store |
*staterpc.Client |
PlaybookDispatcher (store) |
— | 2 | *Store |
*staterpc.Client |
Key property of the remote client: *staterpc.Client is a single adapter that wraps one
StateStoreClient and is asserted against whichever narrow Go interfaces its caller needs
(exactly as gatewayrpc.Client asserts both gateway.ChatContract and
gateway.SessionStore). The Go contracts stay narrow (≤8, the interfacebloat cap); the wire
is one service. workflow.Store introduces no new RPCs — it is a narrow consumer face over
the same Update/Transition/InsertEvent RPCs.
3. Method ownership
*Store remains the single implementation of the daemon/webui store surfaces and of
workflow.Store. Ownership below is about which process is the primary consumer of each
method and therefore which methods are genuinely cross-process on the wire vs.
daemon-internal.
Process legend: DAEMON = archied (incl. in-process webui + gateway runtime +
storerpc.Server); AGENT = archie-agent worker (no DB connection); GATEWAY = store
access through daemon-side narrow adapter interfaces.
| Contract | Primary consumers (process) | Remote on the wire? |
|---|---|---|
TaskLifecycle (8) |
DAEMON (dispatch, claim, action) + AGENT (Transition, Update via storerpc) |
Transition/Update = YES (agent); rest DAEMON |
TaskArchiver (1) |
DAEMON (taskactions archive) |
not yet |
TaskRetryer (1) |
DAEMON (taskactions retry) |
not yet |
TaskQueries (7) |
DAEMON + GATEWAY (status/list adapters) | not yet (OpenPRs/Tasks/StatusCounts cross to gateway adapters in-process) |
TaskEvents (7) |
DAEMON + AGENT (InsertEvent via storerpc) |
InsertEvent = YES (agent); rest DAEMON |
workflow.Store (3) |
AGENT only | YES (the sole genuinely remote consumer) |
CaptureStore (2) |
DAEMON (webhook intake + mapping editor) | not yet |
MappingStore (5) |
DAEMON (dashboard editor + dispatch loop) | not yet |
BindingStore (6) |
DAEMON (dashboard editor + dispatch loop) | not yet |
BindingDispatcher (3) |
DAEMON (dispatch loop) | not yet |
BindingTaskCreator (1) |
DAEMON (dispatch loop) | not yet |
PlaybookDispatcher (2) |
DAEMON (EDA playbook dispatch loop) | YES (side-effecting-action idempotency ledger over gRPC) |
Consequence for migration order: the agent's workflow.Store is already cross-process
already (via the NATS storerpc surrogate), so it is the first contract to move to gRPC. The
daemon-internal surfaces (Capture/Mapping/Binding) stay in-process until the daemon
itself points at archie-state-store. TaskStore (the large composite) moves last.
Two orphan methods — TaskQueries.ClearTerminalTasks and
TaskQueries.IncrementRetryCount have no production consumer (only *Store tests). They
remain part of the Go TaskQueries/TaskStore surface, and therefore part of the wire
contract, but are flagged for deletion: they must not gain dedicated consumers or wire
methods in this work. Do not add the rest of the codebase to them.
4. Domain-facing types & consumer-owned contract — resolves the contradiction
The Phase 2 acceptance criterion ("internal/domain/workflow … no longer import
internal/store directly — they call the State Store contract") cannot be met while
store.Task, store.Status*, store.Source*, and store.WorkflowStore live in
internal/store. This section fixes where they move.
The relocation (a prerequisite that must land before or with .4.2):
Current symbol (internal/store) |
Moves to | Notes |
|---|---|---|
Task type |
internal/domain/workflow.Task |
The task-execution record the workflow domain operates on. internal/store persists this type (persistence → domain). |
Status + Status* constants |
internal/domain/workflow |
e.g. StatusClosedWontDo, StatusMerged, StatusPROpen, StatusWaitingHuman, StatusRunning — task-lifecycle vocabulary. |
Source + Source* constants |
internal/domain/workflow |
e.g. SourceChat — provenance vocabulary, follows Task. |
WorkflowStore interface (3 methods) |
internal/domain/workflow.Store |
Consumer-owned (dependency rule #2). Supersedes store.WorkflowStore. |
Resulting dependency direction (correct per the architecture):
internal/domain/workflowimports nointernal/storesymbols → the acceptance criterion is satisfied. It consumesstore.Task's replacement asworkflow.Task, usesworkflow.Status*/workflow.Source*, and depends on its ownworkflow.Storeinterface.internal/storeimportsinternal/domain/workflowfor theTask/Status/Sourcetypes it reads and writes (infrastructure → domain). NOTE:internal/storeis a transitional compatibility location only — its long-term home isinternal/infrastructure/store(see the callout in §1). Do not treatinternal/storeas the final infrastructure boundary.- Every store test that constructs
store.Task{…}is updated to the relocated type as part of this prerequisite. This is a mechanical rename across ~6 workflow production files (workflow.go,steps.go,triage.go,review.go,feasibility.go,implement.go) and their tests plus the store tests.TestRecordDispatchViaExplicitTxis removed rather than updated — its non-nil*sql.Txpath has no production consumer (see §5'sRecordDispatchblocker).
What this does NOT do: it does not implement the full §4 Workflow migration
(mutable Stage → StepExecution history, RetryCount/Attempt → Attempt records,
workflow.Registry → versioned Workflow definitions, etc.). It lands only the minimal
task-type + Status/Source + consumer-owned-interface relocation that the Phase 2
domain-decoupling criterion requires. The residue of §4 remains its own migration.
Forward-compatibility: because the wire DTOs are derived from the domain Task/Status
(§5), when the full §4 migration later renames workflow.Task (or refines it into
WorkflowExecution), only values.go and the proto field mapping change — the RPC surface and
consumer interfaces are unaffected. The wire contract is deliberately type-agnostic at this
level.
5. DTO / wire representation
Proto layout
- Service:
proto/state/v1/state.proto,package state.v1, oneStateStoreservice (mirrors the gateway's singleChatService; proto bypasses the Gointerfacebloatcap, asChatContractdoes via//nolint:interfacebloat). - Codegen: the existing buf v2 pipeline.
proto/buf.yaml(module, lint STANDARD, breaking FILE againstmain),proto/buf.gen.yamlmanaged mode withgo_package_prefix → github.com/samcharles93/archie-core/internal/contracts, pluginsprotoc-gen-go+protoc-gen-go-grpc,out: ../internal/contracts,opt: paths=source_relative. Nogo_packagein source — managed mode owns it. Generated Go lands ininternal/contracts/state/v1/(packagestatev1), committed (proto:checkfails on uncommitted/unmodified generated files). - Adapter package:
internal/infrastructure/staterpc/withserver.go(registers the generated server over the store),client.go(wraps the generated client intostore.*andworkflow.Storefacades),values.go(proto ⇄ domain mappers), andconformance_test.go(bufconn local-vs-grpc dual-adapter suite), mirroringinternal/infrastructure/gatewayrpc/. - Binary:
cmd/archie-state-store/main.go, mirroringcmd/archie-gateway/main.goand the serve pattern ininternal/app/archied/gateway.go(serveGateway).
The StateStore RPC set (union of the contracts, grouped)
Methods are named after the store methods so the mapping is unambiguous. workflow.Store is a
view, not new RPCs — Update/Transition/InsertEvent are shared with
TaskLifecycle/TaskEvents and appear once.
| Group | RPCs |
|---|---|
| Lifecycle | EnqueueIssue, EnqueueChatTask, ClaimNext, ClaimByIssue, Transition, Update, Requeue, RecoverStale |
| Archive / Retry | ArchiveTask, RetryTask |
| Queries | TaskByIssue, OpenTaskByPR, TaskByID, OpenPRs, ClearTerminalTasks, Tasks, StatusCounts, IncrementRetryCount |
| Events | InsertEvent, EventsSince, TaskEvents, WorkflowStats, StageStats, TokensByDay |
| Capture | InsertCapture, ListCaptures |
| Mapping | InsertMapping, GetMapping, ListMappings, UpdateMapping, DeleteMapping |
| Binding | InsertBinding, GetBinding, ListBindings, UpdateBinding, DeleteBinding, ApproveBinding |
| Dispatch | ArmedBindingsForSource, RecordDispatch, ListUndispatchedCaptures |
| Playbook | RecordPlaybookDispatch, DeletePlaybookDispatches |
| BindingTaskCreator | EnqueueBindingTask |
| Config snapshot | PutConfigSnapshot, GetConfigSnapshot |
| Apply status | PutApplyStatus, ListApplyStatus |
| Channel status | PutChannelStatus, ListChannelStatus |
| Task log | ReadTaskLog, StreamTaskLogContent |
That is 51 unique contract RPCs across one service (the TaskEvents.Close method is dropped).
Close() is excluded from the wire (it is server lifecycle, not a client call) — see §11.
The channel-status pair follows the config snapshot's split for the same reason: the writer is the process that HOSTS the channels -- the Messaging Service, which alone observes whether a channel is starting, running or failed -- and the reader is the UI process. Both RPCs are administrative, so a task-scoped grant reaches neither. A channel's state is runtime state rather than a settings document, which is why it is not a ControlPlaneService resource kind: a failed channel is not a document anybody edits. Asking for a reload travels on its own surface rather than through this one; see docs/architecture/migration-decisions.md, "Channel state to the dashboard".
The config-snapshot pair was added by archie-core-ymut (see
docs/architecture/migration-decisions.md, "Dashboard configuration page"). It is the one
surface whose writer is the daemon and whose reader is the UI process, and both RPCs are
administrative: authorizesTaskScopedCall is deny-by-default, so a task-scoped grant reaches
neither. ConfigSnapshot.document is opaque to this contract — the store neither parses nor
validates it, and schema names the projection so a reader can refuse a shape it does not
understand.
The apply-status pair was added by archie-core-pskb (see
docs/prds/control-plane-apply-status.md). It is administrative for the same reason as the
config-snapshot pair, and deny-by-default in authorizesTaskScopedCall covers it without a
new rule. Unlike that pair, the read is a list: ListApplyStatus returns every process's
record and an empty slice means nothing has reported yet, so the found=false convention for
absent single rows does not apply. The rows are keyed on (process, kind) and replaced in
place, because each process re-stamps its record on an interval and no reader wants a history
of re-stamps.
The task-log pair was added by archie-core-iaqx (see docs/prds/ui-service-boundary.md's
route inventory). It is the one surface whose payload is not a row: the store service reads
them out of the state directory it owns with internal/logging's own reader, so the wire
carries logging.Entry/logging.Query values and neither side restates the JSONL format.
ReadTaskLog returns a decoded page and StreamTaskLogContent returns the raw bytes
server-streamed, because a log file rotates at 20 MB — past gRPC's 4 MiB unary cap — and the
same reasoning that made StreamCaptures supersede ListCaptures applies. found=false with
no error means the attempt has no log; a service with no reader answers Unavailable instead,
and the two answers are load-bearing for the dashboard: only the second is a deployment
condition, and the panel that collapsed them told operators task logging was switched off when
it was not. The Go facade is storecontract.TaskLogStore (2 methods), carried by
*staterpc.Client alongside the rest.
The playbook pair was added by archie-core-t2db.17's implementation (see
docs/prds/eda-playbook-engine.md, execution-time gap 2). It is administrative
for the same reason as the config-snapshot and apply-status pairs, and
deny-by-default in authorizesTaskScopedCall covers it without a new rule. The
row is the structural (playbook_id, playbook_version, event_id, action_id)
tuple, written before the action's side effect fires (the reverse of
binding_dispatches), so a duplicate returns store.ErrAlreadyDispatched and
the coordinator skips the invoke rather than repeating a non-revocable side
effect. DeletePlaybookDispatches is the explicit reclamation primitive; nothing
calls it automatically, because a playbook file's absence at boot is not a
deliberate removal (eda-playbook-engine.md, "Lifetime"). The Go facade is
storecontract.PlaybookDispatcher (2 methods), carried by *staterpc.Client
alongside the rest.
Domain types and values.go mapping
values.go maps proto ⇄ these types. The task model for the agent path is the domain-owned
workflow.Task (§4); the daemon/webui store surfaces return the same workflow.Task (the store
persists the domain type).
| Domain type | Package | Notes for values.go |
|---|---|---|
workflow.Task |
internal/domain/workflow |
time fields stored as layout strings; convert to/from google.protobuf.Timestamp |
events.Event |
internal/events |
Timestamp → google.protobuf.Timestamp |
store.CapturedEvent |
internal/store |
headers/body/remote_addr; body capped 256 KiB |
mapping.Mapping |
internal/domain/mapping |
JSON-path field bindings |
binding.Binding |
internal/domain/binding |
secret is already decrypted before returning; keep decrypted form out of any durable/logged representation |
workflow.Status / workflow.Source |
internal/domain/workflow |
enum-like; map to proto enum or string, keeping the names stable for compatibility |
store.WorkflowStat |
internal/store |
aggregate row |
store.StageStat |
internal/store |
aggregate row |
store.DayTokens |
internal/store |
aggregate row |
Wire-contract blocker fixed here: RecordDispatch
store.BindingDispatcher.RecordDispatch(ctx, tx *sql.Tx, bindingID, bindingVersion int64, captureID int64, taskID int64) error takes a *sql.Tx that cannot cross a gRPC boundary.
Evidence that the *sql.Tx is removable: the dispatch loop calls RecordDispatch(ctx, nil, …) (internal/daemon/daemon.go). The implementation falls back to the store's own
*sql.DB when tx == nil ("best-effort, not atomic"; the comment and the
TestRecordDispatchViaExplicitTx test are the only consumers of the non-nil tx path).
Production never opens the explicit transaction.
Decision: the contract drops *sql.Tx from RecordDispatch (the method becomes
RecordDispatch(ctx, bindingID, bindingVersion, captureID, taskID) error). The store service
owns any transaction boundary internally when atomicity is wanted; the at-most-once
guarantee is the INSERT OR IGNORE dedup row, which is already transactionless in production.
TestRecordDispatchViaExplicitTx is superseded by the store service's own internal-transaction
test. This is a required interface migration that must land before or with .4.2 (the
current interface.go signature and test still use the transaction-bearing form).
6. Agent State Store client handoff
Moving the agent from NATS storerpc to gRPC workflow.Store requires the agent process to
receive a State Store endpoint. The agent already receives its NATS connection settings via
boot-time env/flags (see cmd/archie-agent/main.go: -nats-url / NATS_URL, NATS_TOKEN
env), and the daemon injects them into the task container. The State Store handoff follows the
same seam.
Boot handoff contract (additive, not replacing the NATS settings):
| Field | Where | Source | Notes |
|---|---|---|---|
StateStoreTarget |
agentworker.Settings |
env STATE_STORE_URL (or -state-store-url flag), set by the daemon in the container env |
host:port of the State Store gRPC server. Loopback/Docker-bridge address — the agent reaches it the same way it reaches the daemon's NATS. |
StateStoreToken |
agentworker.Settings |
env STATE_STORE_TOKEN (or -state-store-token flag) |
Per-task bearer token carried in gRPC metadata by a client interceptor (see §9). |
StoreTimeout |
agentworker.Settings |
a bounded per-call timeout | Mirrors natsrpc.Client.Timeout / the existing rpcTimeout; bounds each State Store call when the caller ctx has no deadline. |
Wiring change in the transport façade:
internal/infrastructure/agenttransport/nats.Transport—Store(timeout) store.WorkflowStorereturns&storerpc.Client{Conn: t.conn, Timeout: timeout}. It becomesStore(timeout) workflow.Storeand returns a single long-livedstaterpc.Clientdialled tosettings.StateStoreTarget(with the token interceptor and per-call timeout).internal/app/agentworker/worker.goSettingsgains the three fields above;taskServiceTransportandtaskDependencieschange theirstoretype fromstore.WorkflowStoretoworkflow.Store(viadomain/workflow.TaskContext{Store: …}).- Reconnect/deadline:
grpc.NewClient(or the existing dial path) provides transparent reconnect; each RPC is bounded by the per-call context deadline (StoreTimeout), matching the request/reply timeout the NATS path used. No streaming is involved, so no server-sent reconnect semantics apply.
Deletion rule for the NATS path: the NATS storerpc (Server, Client, subjects
archie.store.update|transition|insert_event) is deleted only after the agent has fully
moved to gRPC and the container env carries STATE_STORE_URL. Until then both paths may coexist
(agent flips when the daemon starts injecting STATE_STORE_URL); there is no dual-live window
across a single agent process, which is either NATS-backed or gRPC-backed, never both.
Operational specifics (in-process listener → address → per-task injection)
- Who binds the in-process State Store listener: the daemon (
archied), at bootstrap, when the State Store is served in-process/multiplexed (mirroringserveGatewayininternal/app/archied/gateway.go): agrpc.NewServer()+RegisterServer+ graceful stop onctx.Done(). - Address selection (Docker-bridge reachable): reuse the host-gateway resolution already used
for embedded NATS (
internal/container/network.goRequireHostGateway/resolveHostGateway). Per §9's single listener-topology rule, the listener binds the host-gateway bridge address when agent containers consume it (the Phase 2 default), or loopback-only when the daemon is the sole consumer. The chosen address is stored on the daemon asd.ConnectedStateStore.URL(analogous tod.ConnectedNATS.URL). - Per-task injection:
containerEnv(task)(internal/daemon/daemon.go) appendsSTATE_STORE_URL=<d.ConnectedStateStore.URL>andSTATE_STORE_TOKEN=<token>alongside the existingNATS_URL/NATS_TOKEN. The agent reads them via env (-state-store-url/STATE_STORE_URL,-state-store-token/STATE_STORE_TOKEN), the same seam asNATS_URL. A reloaded[services.state]must not point new containers at a store the daemon is not serving — the same immutability rule as NATS.
7. Error semantics
Current state (both boundaries lose error identity)
- NATS
natsrpc.Envelope(internal/natsrpc/natsrpc.go) serialises any error to a singleerr.Error()string and rehydrates a fresherrors.New(string). The original type,%wchain, and sentinel identity are lost →errors.Is(err, store.ErrStaleTransition)is false across the boundary. - gRPC
gatewayrpcreturns rawerrfrom handlers; gRPC-Go converts tocodes.Unknownwith only the message. The only explicit code in the repo iscodes.Unavailablefor a missing session store (server.go). No status-code mapping for store errors exists.
Decision — structured mapping so errors.Is survives
The StateStore service must map the store's sentinel errors to canonical gRPC codes and
rehydrate them to the same sentinels on the client, so consumer errors.Is checks keep
working — an improvement over the flattening it replaces, not a regression.
| Store sentinel / outcome | gRPC code | Client rehydrates to |
|---|---|---|
store.ErrStaleTransition |
FailedPrecondition |
store.ErrStaleTransition |
store.ErrBindingNotFound |
NotFound |
store.ErrBindingNotFound |
store.ErrMappingNotFound |
NotFound |
store.ErrMappingNotFound |
store.ErrBindingOverlap |
FailedPrecondition |
store.ErrBindingOverlap |
store.ErrBindingTransition |
FailedPrecondition |
store.ErrBindingTransition |
store.ErrAlreadyDispatched |
AlreadyExists |
store.ErrAlreadyDispatched |
not-found-as-(nil,nil) (TaskByID, GetBinding, GetMapping, ClaimNext, ClaimByIssue, …) |
NotFound with a found=false field (not an error) |
(nil, nil) |
| infra / wrapped error | Internal (sanitised message, see below) |
wrapped fmt.Errorf(...%w) |
| capability absent | Unavailable |
context/typed error |
The found=false-not-error convention matters: the repo deliberately returns (nil, nil) for
not-found in reads and a sentinel for not-found in writes. The wire must preserve both
conventions (a GoogleRPCStatus with NotFound is an error, which would wrongly surface to
callers that expect (nil, nil)). Implement found as an explicit boolean field on read
responses, and map (nil,nil) ⇄ found=false + OK.
Error-detail sanitisation (rev. 2c)
Full internal error messages must not be placed in gRPC details for remote callers. SQL
paths, provider/forge details, secrets, and stack traces are sensitive internals. The rule:
- Sentinel errors — the client rehydrates the sentinel from the code; the message string in
status.Erroris a short, public, stable phrase (e.g."stale transition"), not the raw wrapped chain. - Infra errors — the wire carries a sanitised public message + the
Internalcode. The full error (with%wchain, SQL, provider detail) is logged server-side only by the store service. The client rehydrates a generic wrapped error and must not surface the raw text to users.
8. Limits
Interface bloat (interfacebloat cap = 8) — .golangci.yml interfacebloat: max: 8,
which overrides the upstream default of 10. The Go consumer facades must stay ≤8 methods.
TaskLifecycle is exactly 8 (at the cap); TaskStore is the 24-method composite that the cap
is why it is decomposed; proto services bypass the cap (like ChatContract).
Payload caps:
- Capture body:
defaultCaptureMaxBodyBytes = 256 KiB(may reduce further; the wire message must not exceed gRPC's default 4 MiB receive limit for a single capture). A single capture fits, but a batch of them did not: the caller-side cap of up to 100 captures at 256 KiB each could exceed 4 MiB in one unaryListCaptures/ListUndispatchedCapturesresponse..4.7addsStreamCaptures/StreamUndispatchedCaptures(server-streaming, one capture per message) as the path bothstaterpc.Clientmethods actually call; the original unary RPCs stay defined, unmodified and deprecated, only sobuf breakingprotects an in-flight rolling deploy. - Task
detail/park_reason/eventDetail: 4000-char hard cap (clip(s, 4000)).
Retention (stays server-side in the store; the wire does not carry pruning decisions):
eventstable: no prune, unbounded growth — a known risk to revisit, not changed here.captured_events: prune-on-write, default 7 days / 5000 events.
Pagination/arity: store read methods take a raw int limit (no store-side cap); the
caller-side caps (task list 100, log 2000, captures 100, capture scan window 5000, binding
dispatch batch 100, session list 20) remain caller-side. The wire passes limit int and returns
[]* slices, mirroring the existing Go signatures — no cursor/paging protocol is introduced
in this work (the current EventsSince(sinceID, limit) offset cursor is the only one, and it
stays as-is).
9. Transport security boundary
The gateway precedent is loopback-only, unauthenticated gRPC. The State Store holds the
crown-jewel archie.db (task/event/capture/mapping/binding data), so its boundary must be
stated explicitly rather than left implicit.
The State Store's equivalent boundary (rev. 2c):
- Listener topology — single rule (rev. 2c). One in-process listener; its bind address is
chosen by the consumer set, never both at once:
- Agent containers consume it (the Phase 2 driver during multiplexed serving) → bind to the
host-gateway bridge address so containers can reach it, and token auth is mandatory
(a bridge host is reachable by any process on that bridge). A container cannot reach the
host's
127.0.0.1, so loopback-only is NOT usable for the agent-consumed path. - Only the daemon consumes it (no agent) → bind loopback-only (
--listen 127.0.0.1),insecureis fine because it is not network-reachable; no token required. - There is no dual-listener design. The practical default during
.4.2(the reason in-process serving exists is to give the agent a gRPC target) is the bridge address + token.127.0.0.1is reserved for the daemon-local-only case.
- Agent containers consume it (the Phase 2 driver during multiplexed serving) → bind to the
host-gateway bridge address so containers can reach it, and token auth is mandatory
(a bridge host is reachable by any process on that bridge). A container cannot reach the
host's
- Agent access → the container bridge. The agent runs in a task-scoped container on the
host and reaches the State Store over the Docker bridge gateway (the bind address from the
topology rule above), exactly as it reaches the daemon's NATS. To close the "any host
process could reach the store" gap, the agent authenticates with a per-task bearer token
carried in gRPC metadata by a client interceptor (
StateStoreToken, §6). Network reachability + the token is the trust boundary for the default bridge topology. - The daemon uses the local adapter (
*store.Store) in-process by default, so no network boundary exists until[services.state].targetis set; when it is set, the daemon client authenticates with a token supplied alongside the target. - Non-loopback / k8s: TLS/mTLS is an operator decision, not a default. Any non-loopback
exposure (e.g.
--listen 0.0.0.0, a CDN, a cross-host service) requires an explicit decision and mTLS or token auth — mirroring the gateway's--listen/--enable-cors-headerconfirmation caveat. The NATS-KV discovery fallback supplies endpoints but does not add transport security; that stays the operator's responsibility.
Token lifecycle (rev. 2c; scoping added in .4.7)
- Generation: the daemon generates a per-task bearer token when it acquires the container
(or per incumbence) and injects it via
containerEnvasSTATE_STORE_TOKEN. A single token may cover a task's container lifetime. - Registration (
.4.7, post-standalone-cutover): the State Store is now its own process (.4.3/.4.6), so the daemon can no longer just hold an in-process issued-token set for the server to check — it registers the token at the remote authority via theRegisterTaskGrant/RevokeTaskGrantRPCs (staterpc.GrantIssuer,daemon.StateStoreGrantIssuer), admin-token-authenticated calls a task-scoped token itself can never make. - Lifetime: tied to the task + container grace period; rotated on a new container acquisition
for the same task; a token for a released container is invalidated (
RevokeTaskGrant, deferred alongsideContainerPool.Release). An unrevoked grant still expires server-side (bounded byRegisterTaskGrantRequest.lifetime_seconds, capped at 7 days) so a daemon crash before revoke cannot leave a grant valid indefinitely. - Validation and scope (
.4.7): a gRPC interceptor (staterpc.TaskGrants.UnaryInterceptor) validates the token against the State Store's own issued-grant set; missing/unknown/expired →codes.Unauthenticated. Unlike the daemon's own administrative token (full access to all ~40 RPCs), a task-scoped grant additionally authorises onlyUpdate/Transition/InsertEventon its own task ID — every other RPC, including the streaming capture surface andRegisterTaskGrant/RevokeTaskGrantthemselves, iscodes.PermissionDeniedfor a task-scoped caller. The token is carried in gRPC metadata, never in a URL.
Fail-closed on non-loopback (rev. 2c)
If [services.state].target points to a non-loopback address and neither TLS nor a token
is configured, the State Store client (daemon or agent) fails closed — it refuses to
start/dial rather than fall back to a plaintext, unauthenticated remote connection. This mirrors
the gateway's --listen/--enable-cors-header confinement caveat and prevents a silent
unauthenticated remote store. The loopback + token default is the only path that starts without
an explicit operator decision.
10. Mode / config shape
Follow the gateway's presence-based target seam, not the NATS mode enum.
- Services are keyed by name, not named as struct fields. A
State ServiceConnectionfield per service is the duplicationdocs/prds/service-registry.mdremoves, and this document's summary describes the seam as[services.<name>]:registered once viatype Services map[string]ServiceConnectionRegisterService(context, name, target, listen, tokenEnv). The TOML shape below is unchanged, so nothing else in this section is affected; readcfg.Services.Get("state")wherever it sayscfg.Services.State. - Empty
Target→ startup error. The local*store.Storeadapter default was withdrawn when the State Store was extracted (§12 step 7): the standalonearchie-state-storeprocess exclusively ownsarchie.db, so botharchiedandarchie-gatewaynow reject an empty target (internal/app/archied/bootstrap.goandstate_store_client.go:services.state.target is required). SetTarget→ dial gRPC with*staterpc.Client; no client opens the database itself. This differs from the gateway, whereTargetis defaulted to127.0.0.1:8585becausearchie-gatewayis already a separate process whose client and server share one default. [services.state]is present and uncommented inconfig.example.toml, withtargetmarked REQUIRED, because the value is load-bearing for every client. A commented-outtargetcannot boot, so the section ships uncommented.- Composition root:
internal/app/archied/bootstrap.go(the daemon) andinternal/app/archieui/config.go(the UI process) both readcfg.Services.Get("state")and dial the remote State Store. The task store is built only in the standalonearchie-state-storeprocess (internal/app/archied/state_store.go), which is the only path that opens the task tables.
Mode fork, decided. The gateway uses a single
targetaddress (no enum). The State Store boundary uses the same seam for symmetry, and both now require a non-emptytarget: the State Store's local-store default was withdrawn when the process was extracted (§12 step 7). Nomodefield is added. A companionSTATE_STORE_TOKENsecret (or a[services.state] target_tokenkey) carries authentication.
11. Local-adapter compatibility + conformance
*store.Store(local) and*staterpc.Client(remote) satisfy the same contract interfaces. Use explicitvar _assertions on both adapters (the gateway's local adapters use only structural/return-type assertions; the store precedent's explicitvar _style is stronger and should be adopted on both sides).var _ workflow.Store = (*staterpc.Client)(nil) // first contract to move (agent) var _ store.TaskStore = (*staterpc.Client)(nil) // daemon path, added progressively var _ store.CaptureStore = (*staterpc.Client)(nil) var _ store.MappingStore = (*staterpc.Client)(nil) var _ store.BindingStore = (*staterpc.Client)(nil) var _ store.BindingDispatcher = (*staterpc.Client)(nil) var _ store.BindingTaskCreator = (*staterpc.Client)(nil) var _ store.PlaybookDispatcher = (*staterpc.Client)(nil) // local side (in store), already present: var _ workflow.Store = (*Store)(nil)- Conformance suite: a
staterpc/conformance_test.gobufconn suite runs the same contract through both"local"and"grpc"adapters (mirroringgatewayrpc/conformance_test.go), asserting error-sentinel fidelity and the not-found-as-(nil,nil)convention both ways. Close(): stays on the Go store interfaces (soTaskStore/TaskEventsremain fully satisfiable) but is not aStateStoreRPC. The remotestaterpc.ClientimplementsClose()as a no-op; the store service owns its own DB lifecycle. When the daemon hands the store toarchie-state-store,b.cleanup()no longer callsstore.Close()on a remote client (it is the daemon's own lifecycle only when it owns the store in-process).
12. Cutover / deletion rules
- Ratify the contract (this document). No code.
- Domain type + interface relocation (prerequisite, coupled with §4). Move
Task/Status/Sourceand theWorkflowStoreinterface intointernal/domain/workflow(asworkflow.Task/workflow.Status/workflow.Source/workflow.Store), sointernal/domain/workflowimports nointernal/store. Update the store tests and the 6 workflow production files. This must land before or with.4.2, because.4.2cannot move the agent's store contract offstore.WorkflowStorewhile the domain still imports it. .4.2— generalisestorerpcto the gRPC contract. Move the agent'sworkflow.Store(Update/Transition/InsertEvent) from NATS JSONstorerpctostaterpc.Clientover gRPC, using the §6 handoff. The daemon serves the gRPCStateStoreserver in-process (multiplexed) first so the agent has a gRPC target before the standalone binary exists (PRD §3 "multiplexed in-process is the migration default"). Bind the in-process listener and injectSTATE_STORE_URL/STATE_STORE_TOKENper task (see §6 "Operational specifics"). Also land theRecordDispatch*sql.Txdrop (see §5) in the same span. Flip the agent'sTransport.Storeto a gRPC client onceSTATE_STORE_URLis injected.- Delete NATS
storerpconly after the agent is fully on gRPC — deleteinternal/storerpc(Server, Client, subjectsarchie.store.update|transition|insert_event), thenatsrpc.RegisterAllregistration, and the now-unusedstore.WorkflowStoreNATS path ininternal/infrastructure/agenttransport/nats, in the same commit. [services.state].targetstays empty (inproc) through.4.2; thearchie-state-storecmd/binary is.4.3and hosts the sameStateStoregRPC service.- Swap daemon-internal contracts one at a time (only once the daemon points at
archie-state-store):Capture → Mapping → Binding → TaskStore. Each swap is independent — add the remote-client composition path, run the per-contract conformance suite, flip[services.state].target, delete the local use. A consumer mixes in-process and remote contracts for a contract-code (no dual-live window; each contract is either local or remote at a time). - Final flip / deletion of the in-process serving path: when the daemon no longer owns the
store (i.e.
TaskStorehas moved), delete the in-process task-store /wireWebStoreSurfacespath in the daemon in a same-commit deletion — no dual-store ownership. The single SQLite file remains owned byarchie-state-store. - Never split
archie.dbor add a second store. The gateway keeps its own already-separate session SQLite; that is unchanged and out of state-store scope.
13. Decisions recorded (for the review trail)
internal/storeis a transitional compatibility location (targetinternal/infrastructure/store), not the final infrastructure boundary. The producer-owned daemon/webui store surfaces live there now but may relocate with the implementation.- In-process listener + token lifecycle: the daemon binds the in-process State Store
listener to the host-gateway bridge address when agent containers consume it (the Phase 2
default) or loopback-only when the daemon is the sole consumer — one listener, one address
rule, never both (rev. 2c). It selects the bridge address by reusing the embedded-NATS
host-gateway resolution and injects
STATE_STORE_URL/STATE_STORE_TOKENper task viacontainerEnv. Token is per-incumbence (task + container grace period) and validated by a gRPC interceptor →codes.Unauthenticatedon missing/unknown/expired. Non-loopback targets fail closed without TLS or a token. - §12 step 2 (domain type + interface relocation) is done —
internal/domain/workflowimports nointernal/storesymbol andstore.WorkflowStoreis gone fromTaskContext. The broader Phase 2 acceptance criterion (State Store as its own gRPC service) is still open;.4.2picks up from here. - Ownership split: domain-facing workflow contract is consumer-owned in the domain
(
workflow.Store+workflow.Task/Status/Source, per dependency rules #2 and #7); daemon/webui store surfaces stay producer-owned ininternal/store.store.WorkflowStoreis superseded byworkflow.Store. - One
StateStoregRPC service (49 contract RPCs, grouped by contract) + narrow Go consumer facades (≤8) onstaterpc.Client— mirrors the single-ChatServiceprecedent. - Domain type relocation (
Task/Status/Source→internal/domain/workflow) is a Phase 2 prerequisite, pulled forward from migration-decisions §4 (minimal bound,Task → WorkflowExecution); the rest of §4 stays a separate migration. RecordDispatchdrops*sql.Tx(wire-blocker; production passesnil) — a required interface migration landing before/with.4.2.Close()not on the wire; remote clientClose()is a no-op.- Structured gRPC error codes with client rehydration to store sentinels (preserves
errors.Is), afound=false+OKconvention for read not-found, and sanitised public error details (full detail logged server-side only). - Agent handoff:
agentworker.SettingsgainsStateStoreTarget/StateStoreToken/StoreTimeout, injected asSTATE_STORE_URL/STATE_STORE_TOKENenv (mirrors the NATS handoff);Transport.Storereturns a long-lived gRPCworkflow.Storeclient. - Transport security + listener topology (rev. 2c): one in-process listener, bound to the
host-gateway bridge address when agent containers consume it (the Phase 2 default) or
loopback-only for a daemon-local-only consumer.
insecureonly on loopback; mandatory per-task bearer-token auth on the bridge; TLS/mTLS is an operator choice for any non-loopback/k8s topology. - Mode = presence-based
[services.state].target, default local (not yet extracted); add[services.state]toconfig.example.toml. - Explicit
var _assertions on both adapters; per-contract bufconn conformance. - Migration order: domain type/interface relocation →
workflow.Store(agent) → stand uparchie-state-store→Capture→Mapping→Binding→TaskStore; NATSstorerpcdeleted once the agent is on gRPC; in-process store path deleted in a same-commit flip. - Orphans
ClearTerminalTasks/IncrementRetryCountretained but not built upon.