Files
archie/internal/infrastructure/staterpc/attribution_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

72 lines
2.4 KiB
Go

package staterpc
import (
"testing"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/store"
)
// TestEventAttributionSurvivesTheStateStoreBoundary runs the same insert through
// the local adapter and through a bufconn-backed gRPC client, because that hop is
// where the attribution was being lost: the daemon records the event and the State
// Store holds it, so a field the contract does not carry is a field the record
// never has.
//
// The failure this pins is specific and was live: the actor was named inside the
// event's detail text while actor_id, actor_kind and principal_id were all empty,
// so an audit query reading the columns found nothing and "who approved this"
// meant parsing a human-readable string. An in-process test passed throughout,
// because it never crossed this boundary.
func TestEventAttributionSurvivesTheStateStoreBoundary(t *testing.T) {
const (
actorID = "70000000-0000-5000-8000-000000000001"
principalID = "70000000-0000-5000-8000-000000000002"
actorKind = "bot"
)
for _, mode := range []string{"local", "grpc"} {
t.Run(mode, func(t *testing.T) {
ctx := t.Context()
local := store.OpenTest(t)
var c contract = local
if mode == "grpc" {
c = remoteTaskStore(t, local, nil, nil)
}
task, err := c.EnqueueChatTask(ctx, "acme", "widget", "title", "body", "implement", "")
if err != nil || task == nil {
t.Fatalf("EnqueueChatTask = (%+v, %v)", task, err)
}
recorded := events.Event{
Kind: events.KindAgentApproved,
TaskID: task.ID,
ActorID: actorID,
ActorKind: actorKind,
PrincipalID: principalID,
Detail: "approved by an agent, authorised by a person",
}
if _, err := c.InsertEvent(ctx, recorded); err != nil {
t.Fatalf("InsertEvent: %v", err)
}
got, err := c.TaskEvents(ctx, task.ID)
if err != nil {
t.Fatalf("TaskEvents: %v", err)
}
if len(got) != 1 {
t.Fatalf("TaskEvents returned %d events, want 1", len(got))
}
event := got[0]
if event.ActorID != actorID || event.ActorKind != actorKind || event.PrincipalID != principalID {
t.Fatalf("attribution did not cross the boundary: actor=%q kind=%q principal=%q",
event.ActorID, event.ActorKind, event.PrincipalID)
}
if event.Kind != events.KindAgentApproved {
t.Fatalf("kind = %q, want %q", event.Kind, events.KindAgentApproved)
}
})
}
}