diff --git a/internal/contracts/state/v1/state.pb.go b/internal/contracts/state/v1/state.pb.go index 5aecdc632..f8231fd85 100644 --- a/internal/contracts/state/v1/state.pb.go +++ b/internal/contracts/state/v1/state.pb.go @@ -1563,9 +1563,19 @@ type Event struct { // first attempt: rows written before the field existed, and the deliberately // task-agnostic producers, all carry zero. A reader must report those as // unattributable rather than presenting them as one run. - Attempt int64 `protobuf:"varint,11,opt,name=attempt,proto3" json:"attempt,omitempty"` - Detail string `protobuf:"bytes,9,opt,name=detail,proto3" json:"detail,omitempty"` - DataJson string `protobuf:"bytes,10,opt,name=data_json,json=dataJson,proto3" json:"data_json,omitempty"` + Attempt int64 `protobuf:"varint,11,opt,name=attempt,proto3" json:"attempt,omitempty"` + Detail string `protobuf:"bytes,9,opt,name=detail,proto3" json:"detail,omitempty"` + DataJson string `protobuf:"bytes,10,opt,name=data_json,json=dataJson,proto3" json:"data_json,omitempty"` + // Attribution. actor_id is the identity that performed the action and + // actor_kind its kind, so a reader can tell an agent's action from a person's + // without resolving the identity. principal_id is the identity whose authority + // the action used, which is a different fact when an agent acts under a + // person's standing approval; empty means unattributed. All three must cross + // this boundary: a record whose attribution survives only in `detail` is not + // queryable, which is the attribution this design exists to replace. + ActorId string `protobuf:"bytes,12,opt,name=actor_id,json=actorId,proto3" json:"actor_id,omitempty"` + ActorKind string `protobuf:"bytes,13,opt,name=actor_kind,json=actorKind,proto3" json:"actor_kind,omitempty"` + PrincipalId string `protobuf:"bytes,14,opt,name=principal_id,json=principalId,proto3" json:"principal_id,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -1677,6 +1687,27 @@ func (x *Event) GetDataJson() string { return "" } +func (x *Event) GetActorId() string { + if x != nil { + return x.ActorId + } + return "" +} + +func (x *Event) GetActorKind() string { + if x != nil { + return x.ActorKind + } + return "" +} + +func (x *Event) GetPrincipalId() string { + if x != nil { + return x.PrincipalId + } + return "" +} + // CapturedEvent mirrors internal/store.CapturedEvent. type CapturedEvent struct { state protoimpl.MessageState `protogen:"open.v1"` @@ -7712,7 +7743,7 @@ const file_state_v1_state_proto_rawDesc = "" + "\x0ereview_payload\x18\x1b \x01(\tR\rreviewPayload\x12>\n" + "\x1bworkflow_definition_version\x18\x1c \x01(\x03R\x19workflowDefinitionVersion\x12<\n" + "\x1aworkflow_definition_digest\x18\x1d \x01(\tR\x18workflowDefinitionDigest\x128\n" + - "\x18workflow_definition_yaml\x18\x1e \x01(\tR\x16workflowDefinitionYaml\"\x9b\x02\n" + + "\x18workflow_definition_yaml\x18\x1e \x01(\tR\x16workflowDefinitionYaml\"\xf8\x02\n" + "\x05Event\x12\x0e\n" + "\x02id\x18\x01 \x01(\x03R\x02id\x12*\n" + "\x02at\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\x02at\x12\x12\n" + @@ -7725,7 +7756,11 @@ const file_state_v1_state_proto_rawDesc = "" + "\aattempt\x18\v \x01(\x03R\aattempt\x12\x16\n" + "\x06detail\x18\t \x01(\tR\x06detail\x12\x1b\n" + "\tdata_json\x18\n" + - " \x01(\tR\bdataJson\"\x8c\x02\n" + + " \x01(\tR\bdataJson\x12\x19\n" + + "\bactor_id\x18\f \x01(\tR\aactorId\x12\x1d\n" + + "\n" + + "actor_kind\x18\r \x01(\tR\tactorKind\x12!\n" + + "\fprincipal_id\x18\x0e \x01(\tR\vprincipalId\"\x8c\x02\n" + "\rCapturedEvent\x12\x0e\n" + "\x02id\x18\x01 \x01(\x03R\x02id\x12;\n" + "\vreceived_at\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\n" + diff --git a/internal/infrastructure/staterpc/attribution_test.go b/internal/infrastructure/staterpc/attribution_test.go new file mode 100644 index 000000000..68a5ba91b --- /dev/null +++ b/internal/infrastructure/staterpc/attribution_test.go @@ -0,0 +1,71 @@ +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) + } + }) + } +} diff --git a/internal/infrastructure/staterpc/values.go b/internal/infrastructure/staterpc/values.go index 7a28c2325..a4040a7c9 100644 --- a/internal/infrastructure/staterpc/values.go +++ b/internal/infrastructure/staterpc/values.go @@ -123,6 +123,7 @@ func eventProto(e events.Event) *pb.Event { Id: e.ID, At: timestamp(e.At), Kind: e.Kind, TaskId: e.TaskID, Repo: e.Repo, Issue: int64(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Attempt: int64(e.Attempt), Detail: e.Detail, DataJson: eventDataJSON(e.Data), + ActorId: e.ActorID, ActorKind: e.ActorKind, PrincipalId: e.PrincipalID, } } @@ -134,6 +135,7 @@ func eventValue(e *pb.Event) events.Event { ID: e.Id, At: timeValue(e.At), Kind: e.Kind, TaskID: e.TaskId, Repo: e.Repo, Issue: int(e.Issue), Workflow: e.Workflow, Stage: e.Stage, Attempt: int(e.Attempt), Detail: e.Detail, Data: eventDataValue(e.DataJson), + ActorID: e.ActorId, ActorKind: e.ActorKind, PrincipalID: e.PrincipalId, } } diff --git a/proto/state/v1/state.proto b/proto/state/v1/state.proto index 26dc602da..891e27728 100644 --- a/proto/state/v1/state.proto +++ b/proto/state/v1/state.proto @@ -266,6 +266,16 @@ message Event { int64 attempt = 11; string detail = 9; string data_json = 10; + // Attribution. actor_id is the identity that performed the action and + // actor_kind its kind, so a reader can tell an agent's action from a person's + // without resolving the identity. principal_id is the identity whose authority + // the action used, which is a different fact when an agent acts under a + // person's standing approval; empty means unattributed. All three must cross + // this boundary: a record whose attribution survives only in `detail` is not + // queryable, which is the attribution this design exists to replace. + string actor_id = 12; + string actor_kind = 13; + string principal_id = 14; } // CapturedEvent mirrors internal/store.CapturedEvent.