Files
archie/internal/app/archied/state_store_consumers_test.go
T
sam ed1557c12e feat(edastore)!: move event capture and bindings onto PocketBase collections
The event-capture tables (captures, mappings, bindings, binding_dispatches,
playbook_dispatches) now live in PocketBase-owned collections and their ids
are opaque record ids rather than autoincrement integers. The task lifecycle
stays on SQLite: tasks/transitions/events carry FIFO claim ordering, id-cursor
pagination and the task-grant map, none of which gain anything from being
hand-editable. binding_dispatches.task_id is the single seam and stays an
integer addressing that store.

BREAKING: CapturedEvent.Id, Mapping.Id, Binding.Id and Binding.MappingId are
strings on the wire, as are the ids in the capture/mapping/binding requests.
RecordDispatchRequest.task_id is deliberately unchanged.

What the collections buy, beyond storage: a payload nobody has modelled yet is
inspectable, a mapping is editable at runtime, and a binding's relation to its
mapping is enforced. The old schema declared no FOREIGN KEY on that link, so a
dangling mapping_id was previously possible and is now refused.

Behaviour preserved deliberately, each recovered from the SQLite version
rather than reimplemented from memory:
  - draft -> pending_approval -> armed, with approval only from
    pending_approval and an edit always returning a binding for re-approval
  - one binding per source, guarded on insert, update and approve
  - an empty secret on update keeps the stored one, so an edit form that does
    not echo it back cannot silently disarm a binding
  - ErrMappingNotFound / ErrBindingNotFound on a missing delete target
  - sentinels are re-exported from storecontract, never redefined: staterpc
    matches on exact canonical message to rehydrate them, so a second
    definition would break errors.Is across the wire invisibly

Two implementation notes worth keeping:
  - at-most-once dispatch is now a unique index rather than INSERT OR IGNORE.
    Only the unique-violation maps to ErrAlreadyDispatched; any other save
    error is returned, because treating them all as "already recorded" would
    turn the ledger into a silent at-least-once dispatcher.
  - captures store received_at themselves at nanosecond precision in a
    fixed-width layout. PocketBase autodates are millisecond and record ids
    are random, so same-millisecond captures would otherwise order
    arbitrarily; the old store got insertion order free from autoincrement.
  - ListUndispatchedCaptures is raw SQL: the anti-join is a subquery and
    PocketBase's filter DSL is not SQL. A read fires no hooks either way.

Adds tool_calls, which had no persistence at all: tool calls only ever reached
the on-disk task log, readable as text but never queryable or watchable.
Nothing writes it yet.

The binding secret cipher moves with the bindings it protects.
2026-09-23 02:19:28 +10:00

108 lines
4.0 KiB
Go

package archied
import (
"log/slog"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
taskactionstore "github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
"github.com/samcharles93/archie-core/internal/store"
)
// 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.
// stateStoreAdapter stands in for the real adapter (*staterpc.Client), which
// fronts both halves of the contract over one connection: the task store and
// the event-capture store. A plain *store.Store no longer satisfies the
// event-capture surfaces, so a test that asserts "everything routes through
// the adapter" needs something that implements them, exactly as the client
// does in production.
// edaSide aliases the event-capture store so it can be embedded beside
// *store.Store: both are named Store, and Go allows only one embedded field
// per name.
type edaSide = edastore.Store
type stateStoreAdapter struct {
*store.Store
*edaSide
}
// Close disambiguates the two embedded stores, which both have one.
func (a *stateStoreAdapter) Close() error { return a.Store.Close() }
func openSecondStore(t *testing.T) *stateStoreAdapter {
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 &stateStoreAdapter{Store: st, edaSide: edastore.OpenTest(t)}
}
// TestBuildDaemonRoutesTaskStoreThroughStateStore proves the daemon's task
// lifecycle consumer resolves its store surfaces from b.stateStore (the State
// Store contract adapter -- always the remote *staterpc.Client after .4.6)
// 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,
}
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)
}
}
// 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.
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)
}
}