diff --git a/internal/contracts/state/v1/state.pb.go b/internal/contracts/state/v1/state.pb.go index b250bab9a..8da394f71 100644 --- a/internal/contracts/state/v1/state.pb.go +++ b/internal/contracts/state/v1/state.pb.go @@ -10645,7 +10645,10 @@ type RecordDispatchRequest struct { BindingVersion int64 `protobuf:"varint,2,opt,name=binding_version,json=bindingVersion,proto3" json:"binding_version,omitempty"` CaptureId string `protobuf:"bytes,3,opt,name=capture_id,json=captureId,proto3" json:"capture_id,omitempty"` // task_id addresses the tasks table. - TaskId int64 `protobuf:"varint,4,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` + TaskId int64 `protobuf:"varint,4,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"` + // reason is empty for a dispatch and names why an evaluated capture + // started no task. + Reason string `protobuf:"bytes,5,opt,name=reason,proto3" json:"reason,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -10708,6 +10711,13 @@ func (x *RecordDispatchRequest) GetTaskId() int64 { return 0 } +func (x *RecordDispatchRequest) GetReason() string { + if x != nil { + return x.Reason + } + return "" +} + type RecordDispatchResponse struct { state protoimpl.MessageState `protogen:"open.v1"` unknownFields protoimpl.UnknownFields @@ -13662,14 +13672,15 @@ const file_state_v1_state_proto_rawDesc = "" + "\x1dArmedBindingsForSourceRequest\x12\x16\n" + "\x06source\x18\x01 \x01(\tR\x06source\"O\n" + "\x1eArmedBindingsForSourceResponse\x12-\n" + - "\bbindings\x18\x01 \x03(\v2\x11.state.v1.BindingR\bbindings\"\x97\x01\n" + + "\bbindings\x18\x01 \x03(\v2\x11.state.v1.BindingR\bbindings\"\xaf\x01\n" + "\x15RecordDispatchRequest\x12\x1d\n" + "\n" + "binding_id\x18\x01 \x01(\tR\tbindingId\x12'\n" + "\x0fbinding_version\x18\x02 \x01(\x03R\x0ebindingVersion\x12\x1d\n" + "\n" + "capture_id\x18\x03 \x01(\tR\tcaptureId\x12\x17\n" + - "\atask_id\x18\x04 \x01(\x03R\x06taskId\"\x18\n" + + "\atask_id\x18\x04 \x01(\x03R\x06taskId\x12\x16\n" + + "\x06reason\x18\x05 \x01(\tR\x06reason\"\x18\n" + "\x16RecordDispatchResponse\"\xa3\x01\n" + "\x1dRecordPlaybookDispatchRequest\x12\x1f\n" + "\vplaybook_id\x18\x01 \x01(\tR\n" + diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index d3f19b5f4..52a870002 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -660,6 +660,7 @@ func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c st return } if admitted, _ := filter.Admits(values); !admitted { + d.recordEvaluated(ctx, b, c, "filtered") return } @@ -672,8 +673,8 @@ func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c st } // The chain decides dispatch: the workflow's identity may `run` this // workflow in its workspace, and the event's signature result and address - // travel with the request. A denial is recorded and the dispatch is not - // claimed: the capture stays listed for the next cycle. + // travel with the request. A denial is terminal; an unavailable principal + // leaves the capture listed for the next cycle. if !d.authorizeDispatch(ctx, b, c, target) { return } @@ -684,7 +685,7 @@ func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c st // the task. A failed enqueue after the claim loses that dispatch, which is // the at-most-once side of the trade. func (d *Daemon) claimAndEnqueue(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget, values map[string]any) { - if err := d.BindingDispatcher.RecordDispatch(ctx, b.ID, int64(b.Version), c.ID, 0); err != nil { + if err := d.BindingDispatcher.RecordDispatch(ctx, b.ID, int64(b.Version), c.ID, 0, ""); err != nil { if !errors.Is(err, storecontract.ErrAlreadyDispatched) { d.Log.Warn("binding dispatch: record", "binding", b.ID, "capture", c.ID, "error", err) } @@ -767,8 +768,10 @@ func (d *Daemon) markUnsignedStart(ctx context.Context, taskID int64, b binding. } } -// recordDispatchFailure records why a binding did not dispatch a capture. +// recordDispatchFailure records why a binding did not dispatch a capture and +// marks the pair evaluated, so the capture is not offered to it again. func (d *Daemon) recordDispatchFailure(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, reason string, extra map[string]any) { + d.recordEvaluated(ctx, b, c, reason) data := map[string]any{ "binding_id": b.ID, "binding_version": b.Version, @@ -782,6 +785,16 @@ func (d *Daemon) recordDispatchFailure(ctx context.Context, b binding.Binding, c }) } +// recordEvaluated writes the ledger row for an evaluation that started no +// task. Only terminal outcomes call it; transient ones leave the capture +// listed for the next cycle. +func (d *Daemon) recordEvaluated(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, reason string) { + err := d.BindingDispatcher.RecordDispatch(ctx, b.ID, int64(b.Version), c.ID, 0, reason) + if err != nil && !errors.Is(err, storecontract.ErrAlreadyDispatched) { + d.Log.Warn("binding dispatch: record evaluation", "binding", b.ID, "capture", c.ID, "error", err) + } +} + // hasBlockingFailure reports whether any failure is on a required field. func hasBlockingFailure(fields []mapping.Field, failures []mapping.Failure) bool { if len(failures) == 0 { diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index c85ef1bb9..b375080f1 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -257,6 +257,9 @@ type BindingDispatcher interface { // migrating: it stays an integer on purpose. The binding dispatch // loop claims before it enqueues, so it records 0. taskID int64, + // reason is empty for a dispatch and names the terminal outcome of + // an evaluation that started no task. + reason string, ) error ListUndispatchedCaptures(ctx context.Context, sources []string, limit int) ([]CapturedEvent, error) } diff --git a/internal/infrastructure/postgres/binding_dispatch_test.go b/internal/infrastructure/postgres/binding_dispatch_test.go new file mode 100644 index 000000000..4648aa9b0 --- /dev/null +++ b/internal/infrastructure/postgres/binding_dispatch_test.go @@ -0,0 +1,65 @@ +package postgres_test + +import ( + "errors" + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/binding" + "github.com/samcharles93/archie-core/internal/domain/eventtype" + "github.com/samcharles93/archie-core/internal/domain/mapping" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" +) + +// A capture a binding evaluated without dispatching is recorded once and not +// offered to that binding again; a later dispatch of the pair is refused. +func TestEvaluatedCaptureIsNotReoffered(t *testing.T) { + ctx := t.Context() + eda := pgstore.EDA(t, nil) + + eventTypeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "forge", Name: "push"}) + if err != nil { + t.Fatal(err) + } + mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: "push", EventTypeID: eventTypeID}) + if err != nil { + t.Fatal(err) + } + bindingID, err := eda.InsertBinding(ctx, binding.Binding{ + Name: "on-push", Matcher: binding.Matcher{Source: "forge"}, MappingID: mappingID, Workflow: "tdd", + }) + if err != nil { + t.Fatal(err) + } + if err := eda.ApproveBinding(ctx, bindingID); err != nil { + t.Fatal(err) + } + captureID, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{ + Source: "forge", Body: "{}", Authenticated: true, ReceivedAt: time.Now(), + }, time.Hour, 100) + if err != nil { + t.Fatal(err) + } + + assertUndispatched(t, eda, 1) + if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, "filtered"); err != nil { + t.Fatalf("record filtered evaluation: %v", err) + } + assertUndispatched(t, eda, 0) + if err := eda.RecordDispatch(ctx, bindingID, 1, captureID, 0, ""); !errors.Is(err, storecontract.ErrAlreadyDispatched) { + t.Fatalf("dispatch after a filtered evaluation = %v, want ErrAlreadyDispatched", err) + } +} + +func assertUndispatched(t *testing.T, eda *postgres.EDA, want int) { + t.Helper() + got, err := eda.ListUndispatchedCaptures(t.Context(), []string{"forge"}, 10) + if err != nil { + t.Fatal(err) + } + if len(got) != want { + t.Fatalf("undispatched captures = %d, want %d", len(got), want) + } +} diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index ae7413690..cc0095890 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -485,13 +485,15 @@ func (s *EDA) ApproveBinding(ctx context.Context, id string) error { // RecordDispatch writes one at-most-once binding dispatch. binding_version is // stored but is not part of the unique key, so a version bump does not permit -// re-dispatching the same (binding, capture). -func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64) error { +// re-dispatching the same (binding, capture). A non-empty reason records an +// evaluation that ended without a task. +func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64, reason string) error { err := s.q.InsertBindingDispatch(ctx, postgresdb.InsertBindingDispatchParams{ Binding: bindingID, BindingVersion: bindingVersion, Capture: captureID, TaskID: taskID, + Reason: reason, }) return ledgerWrite(err, "edastore: record dispatch") } diff --git a/internal/infrastructure/postgres/migrations/0032_binding_dispatch_reason.sql b/internal/infrastructure/postgres/migrations/0032_binding_dispatch_reason.sql new file mode 100644 index 000000000..64a49727a --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0032_binding_dispatch_reason.sql @@ -0,0 +1,11 @@ +-- binding_dispatches.reason says why an evaluated capture did not start a +-- task; empty means it dispatched. Recording terminal non-dispatch outcomes +-- keeps the capture from being re-evaluated every cycle. + +-- +goose Up + +ALTER TABLE binding_dispatches ADD COLUMN reason text NOT NULL DEFAULT ''; + +-- +goose Down + +ALTER TABLE binding_dispatches DROP COLUMN reason; diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index 2c97ad90a..c5b54c44b 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -395,8 +395,8 @@ func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) er } const insertBindingDispatch = `-- name: InsertBindingDispatch :exec -INSERT INTO binding_dispatches (binding, binding_version, capture, task_id) -VALUES ($1, $2, $3, $4) +INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason) +VALUES ($1, $2, $3, $4, $5) ` type InsertBindingDispatchParams struct { @@ -404,6 +404,7 @@ type InsertBindingDispatchParams struct { BindingVersion int64 Capture string TaskID int64 + Reason string } func (q *Queries) InsertBindingDispatch(ctx context.Context, arg InsertBindingDispatchParams) error { @@ -412,6 +413,7 @@ func (q *Queries) InsertBindingDispatch(ctx context.Context, arg InsertBindingDi arg.BindingVersion, arg.Capture, arg.TaskID, + arg.Reason, ) return err } diff --git a/internal/infrastructure/postgres/postgresdb/models.go b/internal/infrastructure/postgres/postgresdb/models.go index fc6ca7b71..f88d1983c 100644 --- a/internal/infrastructure/postgres/postgresdb/models.go +++ b/internal/infrastructure/postgres/postgresdb/models.go @@ -78,6 +78,7 @@ type BindingDispatch struct { Capture string TaskID int64 DispatchedAt time.Time + Reason string } type Capture struct { diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index 7912926b4..298d11c2e 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -88,8 +88,8 @@ UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1; DELETE FROM bindings WHERE id = $1; -- name: InsertBindingDispatch :exec -INSERT INTO binding_dispatches (binding, binding_version, capture, task_id) -VALUES ($1, $2, $3, $4); +INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason) +VALUES ($1, $2, $3, $4, $5); -- name: InsertPlaybookDispatch :exec INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id) diff --git a/internal/infrastructure/staterpc/client.go b/internal/infrastructure/staterpc/client.go index 1fc2d9de3..5953d2c73 100644 --- a/internal/infrastructure/staterpc/client.go +++ b/internal/infrastructure/staterpc/client.go @@ -635,8 +635,8 @@ func (c *Client) ArmedBindingsForSource(ctx context.Context, source string) ([]b return mapValues(r.Bindings, bindingValue), nil } -func (c *Client) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64) error { - _, err := c.client.RecordDispatch(ctx, &pb.RecordDispatchRequest{BindingId: bindingID, BindingVersion: bindingVersion, CaptureId: captureID, TaskId: taskID}) +func (c *Client) RecordDispatch(ctx context.Context, bindingID string, bindingVersion int64, captureID string, taskID int64, reason string) error { + _, err := c.client.RecordDispatch(ctx, &pb.RecordDispatchRequest{BindingId: bindingID, BindingVersion: bindingVersion, CaptureId: captureID, TaskId: taskID, Reason: reason}) return unmapError(err) } diff --git a/internal/infrastructure/staterpc/server.go b/internal/infrastructure/staterpc/server.go index e1c0f5223..c8ef6e8dd 100644 --- a/internal/infrastructure/staterpc/server.go +++ b/internal/infrastructure/staterpc/server.go @@ -942,7 +942,7 @@ func (s *server) RecordDispatch(ctx context.Context, r *pb.RecordDispatchRequest if s.deps.BindingDispatcher == nil { return nil, errBindingDispatchUnavailable } - if err := s.deps.BindingDispatcher.RecordDispatch(ctx, r.BindingId, r.BindingVersion, r.CaptureId, r.TaskId); err != nil { + if err := s.deps.BindingDispatcher.RecordDispatch(ctx, r.BindingId, r.BindingVersion, r.CaptureId, r.TaskId, r.Reason); err != nil { return nil, s.logErr("RecordDispatch", err) } return &pb.RecordDispatchResponse{}, nil diff --git a/proto/state/v1/state.proto b/proto/state/v1/state.proto index cc0da7126..3427ab40c 100644 --- a/proto/state/v1/state.proto +++ b/proto/state/v1/state.proto @@ -1114,6 +1114,9 @@ message RecordDispatchRequest { string capture_id = 3; // task_id addresses the tasks table. int64 task_id = 4; + // reason is empty for a dispatch and names why an evaluated capture + // started no task. + string reason = 5; } message RecordDispatchResponse {}