Files
archie/internal/infrastructure/staterpc/attribution_test.go
T
sam 03847dadfb fix(state): carry event attribution across the state-store boundary
The live path recorded actor_id, actor_kind and principal_id as empty while the
actor was named inside the event's detail text. The daemon records the event and the
State Store holds it, and the contract between them did not carry the three fields,
so the columns were empty by the time anything could query them. An audit query
reading actor_id found nothing and "who approved this" meant parsing a
human-readable string -- the transient, unqueryable attribution this work exists to
replace.

The in-process test passed throughout because it used the local store and never
crossed that hop. The new test runs the same insert through the local adapter AND a
bufconn-backed gRPC client and asserts the columns on both, so the two paths cannot
disagree again.

Verified: task check exit 0 on this tree; the new test passes in both modes. The
grpc mode is reasoned to have failed before this change -- the field did not exist on
the contract, so it could not be set or read -- but that is inference from the
mechanism, not a run I performed against the old code.
2026-09-22 15:13:55 +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)
}
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)
}
})
}
}