refactor(daemon): binding dispatch is its own type (#1930)

This commit is contained in:
sam committed 2026-11-13 04:15:14 +11:00
1 parent 0a770ed171
commit 0cd9cefd76
13 files changed
+234 -131

No files matched your search

+17 -8
View File
@@ -598,11 +598,6 @@ func (b *boot) buildDaemon(ctx context.Context) {
ConnectedNATS: daemon.NATSEndpoint{URL: b.natsURL, Token: b.natsToken, Scoped: b.cfg.NATS.Mode != config.NATSModeExternal},
ConnectedStateStore: daemon.StateStoreEndpoint{URL: strings.TrimSpace(b.cfg.Services.Get(config.ServiceNameState).Target), CA: b.stateStoreCA(ctx)},
Store: b.stateStore,
Mappings: b.stateClient,
Bindings: b.stateClient,
BindingDispatcher: b.stateClient,
MappingMatches: b.stateClient,
BindingTaskCreator: b.stateClient,
IdentityRepository: b.stateClient,
AgentAssignments: b.stateClient,
Principals: b.stateClient,
@@ -620,17 +615,31 @@ func (b *boot) buildDaemon(ctx context.Context) {
ModelEgress: b.modelEgress,
TaskModels: b.taskModels,
TaskInstructions: b.taskInstructions,
OrgPrincipals: b.orgPrincipals,
TaskLogs: b.taskLogs,
AgentStatus: b.agentStatus,
WorkflowDefinitions: b.workflowDefinitions,
WorkflowEnablement: b.controlPlane,
Playbooks: b.livePlaybooks(),
}
b.d.IssuePoller = daemon.NewIssuePoller(b.cfgHolder, b.stateStore, log, b.issueSources, b.taskActions().Withdraw, b.d.PublishTask, b.d)
b.d.Access = b.accessChain
b.d.ReviewScanner = daemon.NewReviewScanner(b.stateStore, b.reactionClient, b.natsClient.PublishUnique, b.bus, b.d, b.d, b.d, log)
b.d.BindingDispatch = daemon.NewBindingDispatch(daemon.BindingDispatchDeps{
Store: b.stateStore,
Mappings: b.stateClient,
Bindings: b.stateClient,
Dispatcher: b.stateClient,
Matches: b.stateClient,
Creator: b.stateClient,
Enablement: b.controlPlane,
Definitions: b.workflowDefinitions,
Access: b.accessChain,
Denials: b.stateClient,
Cfg: b.cfgHolder,
Orgs: b.d,
OrgPrincipals: b.orgPrincipals,
Log: log,
})
b.d.IssuePoller = daemon.NewIssuePoller(b.cfgHolder, b.stateStore, log, b.issueSources, b.taskActions().Withdraw, b.d.PublishTask, b.d, b.d.BindingDispatch)
}
func (b *boot) publishConfig(ctx context.Context, cfg config.Config) {
+136 -60
View File
@@ -4,9 +4,11 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"maps"
"strings"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/identity"
@@ -18,32 +20,106 @@ import (
"github.com/samcharles93/archie-core/internal/events"
)
// workflowDefinitionSource supplies the active workflow definitions.
type workflowDefinitionSource interface {
WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error)
}
// workflowEnablementSource supplies which workflows each org has disabled.
type workflowEnablementSource interface {
WorkflowEnablement(context.Context) (workflowtask.WorkflowEnablement, error)
}
// BindingDispatchDeps is everything a BindingDispatch reads or writes. Every
// field is required.
type BindingDispatchDeps struct {
Store interface {
InsertEvent(context.Context, events.Event) (int64, error)
}
Mappings storecontract.MappingStore
Bindings storecontract.BindingStore
Dispatcher storecontract.BindingDispatcher
Matches storecontract.MappingMatchRecorder
Creator storecontract.BindingTaskCreator
Enablement workflowEnablementSource
Definitions workflowDefinitionSource
Access access.Authorizer
Denials access.DenialRecorder
Cfg *config.Holder
// Orgs resolves an identity's org for the routed-workflow check at intake.
Orgs orgResolver
// OrgPrincipals is one acting principal per organisation, system org first.
OrgPrincipals func(context.Context) []access.Principal
Log *slog.Logger
}
// BindingDispatch offers each identified capture to the armed bindings that
// match it and starts the task each one names.
type BindingDispatch struct {
store interface {
InsertEvent(context.Context, events.Event) (int64, error)
}
mappings storecontract.MappingStore
bindings storecontract.BindingStore
dispatcher storecontract.BindingDispatcher
matches storecontract.MappingMatchRecorder
creator storecontract.BindingTaskCreator
enablement workflowEnablementSource
definitions workflowDefinitionSource
authorizer access.Authorizer
denials access.DenialRecorder
cfg *config.Holder
orgs orgResolver
orgPrincipals func(context.Context) []access.Principal
log *slog.Logger
}
// NewBindingDispatch returns the dispatch loop over deps.
func NewBindingDispatch(deps BindingDispatchDeps) *BindingDispatch {
return &BindingDispatch{
store: deps.Store,
mappings: deps.Mappings,
bindings: deps.Bindings,
dispatcher: deps.Dispatcher,
matches: deps.Matches,
creator: deps.Creator,
enablement: deps.Enablement,
definitions: deps.Definitions,
authorizer: deps.Access,
denials: deps.Denials,
cfg: deps.Cfg,
orgs: deps.Orgs,
orgPrincipals: deps.OrgPrincipals,
log: deps.Log,
}
}
// dispatchBindings offers each identified capture to every armed binding on
// its source.
func (d *Daemon) dispatchBindings(ctx context.Context) {
principals := d.OrgPrincipals(ctx)
func (bd *BindingDispatch) dispatchBindings(ctx context.Context) {
principals := bd.orgPrincipals(ctx)
seen := make(map[org.OrgID]bool)
for _, principal := range principals {
if seen[principal.Org] {
continue
}
seen[principal.Org] = true
d.dispatchOrgBindings(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org))
bd.dispatchOrgBindings(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org))
}
}
func (d *Daemon) dispatchOrgBindings(ctx context.Context) {
func (bd *BindingDispatch) dispatchOrgBindings(ctx context.Context) {
// Gather the armed bindings whose workflow is enabled first, so the
// oldest-first captures query is not held by captures that only a binding
// on a disabled workflow would take.
bindings, err := d.Bindings.ListBindings(ctx)
bindings, err := bd.bindings.ListBindings(ctx)
if err != nil {
d.Log.WarnContext(ctx, "list bindings failed", "error", err)
bd.log.WarnContext(ctx, "list bindings failed", "error", err)
return
}
enablement, err := d.workflowEnablement(ctx)
enablement, err := bd.workflowEnablement(ctx)
if err != nil {
d.Log.WarnContext(ctx, "binding dispatch: workflow enablement unavailable", "error", err)
bd.log.WarnContext(ctx, "binding dispatch: workflow enablement unavailable", "error", err)
return
}
enabled := make([]string, 0, len(bindings))
@@ -58,42 +134,42 @@ func (d *Daemon) dispatchOrgBindings(ctx context.Context) {
return
}
captures, err := d.BindingDispatcher.ListUndispatchedCaptures(ctx, enabled, bindingDispatchBatchLimit)
captures, err := bd.dispatcher.ListUndispatchedCaptures(ctx, enabled, bindingDispatchBatchLimit)
if err != nil {
d.Log.WarnContext(ctx, "list undispatched captures failed", "error", err)
bd.log.WarnContext(ctx, "list undispatched captures failed", "error", err)
return
}
workflows, err := d.activeWorkflows(ctx)
workflows, err := bd.activeWorkflows(ctx)
if err != nil {
d.Log.WarnContext(ctx, "binding dispatch: workflow definitions unavailable", "error", err)
bd.log.WarnContext(ctx, "binding dispatch: workflow definitions unavailable", "error", err)
return
}
for _, c := range captures {
d.dispatchCapture(ctx, c, armed, workflows)
bd.dispatchCapture(ctx, c, armed, workflows)
}
}
// dispatchCapture offers one capture to every armed binding that matches it.
// armed holds only the org's armed bindings on enabled workflows, so a
// disabled workflow's capture waits rather than recording a failure every pass.
func (d *Daemon) dispatchCapture(ctx context.Context, c storecontract.CapturedEvent, armed []binding.Binding, workflows workflow.WorkflowDefinitionCollection) {
func (bd *BindingDispatch) dispatchCapture(ctx context.Context, c storecontract.CapturedEvent, armed []binding.Binding, workflows workflow.WorkflowDefinitionCollection) {
for _, b := range armed {
if b.Matcher.Matches(c.Source, c.Dispatchable()) {
d.dispatchOneBinding(ctx, b, c, workflows)
bd.dispatchOneBinding(ctx, b, c, workflows)
}
}
}
// bindingMapping returns b's mapping, or nil after logging why it is
// unavailable. A mapping that no longer exists is a terminal outcome.
func (d *Daemon) bindingMapping(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent) *mapping.Mapping {
m, err := d.Mappings.GetMapping(ctx, b.MappingID)
func (bd *BindingDispatch) bindingMapping(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent) *mapping.Mapping {
m, err := bd.mappings.GetMapping(ctx, b.MappingID)
if err != nil {
d.Log.WarnContext(ctx, "binding dispatch: get mapping", "binding", b.ID, "mapping", b.MappingID, "error", err)
bd.log.WarnContext(ctx, "binding dispatch: get mapping", "binding", b.ID, "mapping", b.MappingID, "error", err)
return nil
}
if m == nil {
d.recordDispatchFailure(ctx, b, c, fmt.Sprintf("mapping %q does not exist", b.MappingID), nil)
bd.recordDispatchFailure(ctx, b, c, fmt.Sprintf("mapping %q does not exist", b.MappingID), nil)
}
return m
}
@@ -103,48 +179,48 @@ func (d *Daemon) bindingMapping(ctx context.Context, b binding.Binding, c storec
// must admit the values, and the workflow must accept the inputs. The
// ledger claim and the task commit together, so each binding fires exactly
// once per delivery.
func (d *Daemon) dispatchOneBinding(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, workflows workflow.WorkflowDefinitionCollection) {
m := d.bindingMapping(ctx, b, c)
func (bd *BindingDispatch) dispatchOneBinding(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, workflows workflow.WorkflowDefinitionCollection) {
m := bd.bindingMapping(ctx, b, c)
if m == nil || m.EventTypeID != c.EventType {
return
}
values, failures := mapping.Resolve(m.Fields, []byte(c.Body))
if hasBlockingFailure(m.Fields, failures) {
d.recordDispatchFailure(ctx, b, c, "required field failed", map[string]any{"failures": failures})
bd.recordDispatchFailure(ctx, b, c, "required field failed", map[string]any{"failures": failures})
return
}
if err := d.MappingMatches.RecordMappingMatch(ctx, m.ID, c.ID); err != nil {
d.Log.WarnContext(ctx, "binding dispatch: record mapping match", "mapping", m.ID, "capture", c.ID, "error", err)
if err := bd.matches.RecordMappingMatch(ctx, m.ID, c.ID); err != nil {
bd.log.WarnContext(ctx, "binding dispatch: record mapping match", "mapping", m.ID, "capture", c.ID, "error", err)
}
filter, err := binding.CompileFilter(b.Filter, m.Fields)
if err != nil {
d.recordDispatchFailure(ctx, b, c, "filter does not compile", map[string]any{"error": err.Error()})
bd.recordDispatchFailure(ctx, b, c, "filter does not compile", map[string]any{"error": err.Error()})
return
}
if admitted, _ := filter.Admits(values); !admitted {
d.recordEvaluated(ctx, b, c, "filtered")
bd.recordEvaluated(ctx, b, c, "filtered")
return
}
target, reason, ok := d.resolveBindingTarget(b, values, workflows)
target, reason, ok := bd.resolveBindingTarget(b, values, workflows)
if !ok {
d.recordDispatchFailure(ctx, b, c, reason, nil)
bd.recordDispatchFailure(ctx, b, c, reason, nil)
return
}
// The chain decides dispatch: the binding's principal may `run` this
// workflow in its workspace, and the event's signature result and address
// travel with the request. A denial is terminal; an unavailable principal
// or policy chain leaves the capture listed for the next cycle.
if !d.authorizeDispatch(ctx, b, c, target) {
if !bd.authorizeDispatch(ctx, b, c, target) {
return
}
d.claimAndEnqueue(ctx, b, c, target, values)
bd.claimAndEnqueue(ctx, b, c, target, values)
}
// claimAndEnqueue asks the State Store to claim the dispatch and enqueue its
// task as one unit. A failure leaves the delivery undispatched, so the next
// cycle offers it again; a delivery already dispatched is refused.
func (d *Daemon) claimAndEnqueue(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget, values map[string]any) {
func (bd *BindingDispatch) claimAndEnqueue(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget, values map[string]any) {
title := fmt.Sprintf("binding %s/%d from %s", b.Name, b.Version, c.Source)
body := renderBindingBody(values, c)
if target.declared {
@@ -154,31 +230,31 @@ func (d *Daemon) claimAndEnqueue(ctx context.Context, b binding.Binding, c store
if principal, ok := access.PrincipalFromContext(ctx); ok {
taskIdentity = string(principal.IdentityID)
}
task, err := d.BindingTaskCreator.EnqueueBindingTask(ctx, target.owner, target.repo, title, body, b.Workflow, taskIdentity, b.ID, b.Version, c.ID, target.inputs)
task, err := bd.creator.EnqueueBindingTask(ctx, target.owner, target.repo, title, body, b.Workflow, taskIdentity, b.ID, b.Version, c.ID, target.inputs)
if err != nil {
if !errors.Is(err, storecontract.ErrAlreadyDispatched) {
d.Log.WarnContext(ctx, "binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err)
bd.log.WarnContext(ctx, "binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err)
}
return
}
d.markBindingStart(ctx, task.ID, b, c)
bd.markBindingStart(ctx, task.ID, b, c)
}
// authorizeDispatch checks the binding's principal, its org's dispatching
// identity, may start the workflow. A denial is recorded and reported as a
// binding_dispatch_failure. The run itself acts as the workflow's declared
// identity, which adoptWorkflowIdentity checks when the task is processed.
func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget) bool {
func (bd *BindingDispatch) authorizeDispatch(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget) bool {
principal, ok := access.PrincipalFromContext(ctx)
if !ok {
d.Log.WarnContext(ctx, "binding dispatch: no principal; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID)
bd.log.WarnContext(ctx, "binding dispatch: no principal; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID)
return false
}
resource := access.Resource{
Kind: access.KindWorkflow, ID: b.Workflow, Org: principal.Org, Workspace: b.WorkspaceID,
}
contextValue := access.Context{Signature: signatureOf(c), Addr: c.RemoteAddr}
decision := d.Access.Authorize(ctx, principal, access.ActionRun, resource, contextValue)
decision := bd.authorizer.Authorize(ctx, principal, access.ActionRun, resource, contextValue)
if decision.Allowed {
return true
}
@@ -186,7 +262,7 @@ func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c sto
// Nothing was evaluated. Recording it would mark the pair evaluated for
// good, so a State Store blip would drop the capture instead of leaving
// it for the next cycle.
d.Log.WarnContext(ctx, "binding dispatch: access chain unavailable; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID, "err", decision.Err)
bd.log.WarnContext(ctx, "binding dispatch: access chain unavailable; dispatch left for the next cycle", "binding", b.ID, "capture", c.ID, "err", decision.Err)
return false
}
denial := access.Denial{
@@ -194,10 +270,10 @@ func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c sto
Kind: resource.Kind, ResourceID: resource.ID, Level: decision.Level,
Policies: decision.Policies,
}
if err := d.Denials.RecordDenial(ctx, denial); err != nil {
d.Log.WarnContext(ctx, "binding dispatch: record denial", "binding", b.ID, "error", err)
if err := bd.denials.RecordDenial(ctx, denial); err != nil {
bd.log.WarnContext(ctx, "binding dispatch: record denial", "binding", b.ID, "error", err)
}
d.recordDispatchFailure(ctx, b, c, "access denied", map[string]any{
bd.recordDispatchFailure(ctx, b, c, "access denied", map[string]any{
"level": string(decision.Level), "policies": decision.Policies,
})
return false
@@ -205,8 +281,8 @@ func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c sto
// markBindingStart records on a task's timeline the binding and captured
// event that started it.
func (d *Daemon) markBindingStart(ctx context.Context, taskID int64, b binding.Binding, c storecontract.CapturedEvent) {
if _, err := d.Store.InsertEvent(ctx, events.Event{
func (bd *BindingDispatch) markBindingStart(ctx context.Context, taskID int64, b binding.Binding, c storecontract.CapturedEvent) {
if _, err := bd.store.InsertEvent(ctx, events.Event{
TaskID: taskID,
Kind: events.KindBindingStarted,
Detail: fmt.Sprintf("started by binding %q from source %q", b.Name, c.Source),
@@ -215,21 +291,21 @@ func (d *Daemon) markBindingStart(ctx context.Context, taskID int64, b binding.B
"capture_id": c.ID, "source": c.Source, "unsigned": c.Unsigned,
},
}); err != nil {
d.Log.WarnContext(ctx, "binding dispatch: start marker", "task", taskID, "error", err)
bd.log.WarnContext(ctx, "binding dispatch: start marker", "task", taskID, "error", err)
}
}
// 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)
func (bd *BindingDispatch) recordDispatchFailure(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, reason string, extra map[string]any) {
bd.recordEvaluated(ctx, b, c, reason)
data := map[string]any{
"binding_id": b.ID,
"binding_version": b.Version,
"capture_id": c.ID,
}
maps.Copy(data, extra)
_, _ = d.Store.InsertEvent(ctx, events.Event{
_, _ = bd.store.InsertEvent(ctx, events.Event{
Kind: "binding_dispatch_failure",
Detail: fmt.Sprintf("binding %q (capture %q): %s", b.ID, c.ID, reason),
Data: data,
@@ -239,20 +315,20 @@ 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)
func (bd *BindingDispatch) recordEvaluated(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, reason string) {
err := bd.dispatcher.RecordDispatch(ctx, b.ID, int64(b.Version), c.ID, 0, reason)
if err != nil && !errors.Is(err, storecontract.ErrAlreadyDispatched) {
d.Log.WarnContext(ctx, "binding dispatch: record evaluation", "binding", b.ID, "capture", c.ID, "error", err)
bd.log.WarnContext(ctx, "binding dispatch: record evaluation", "binding", b.ID, "capture", c.ID, "error", err)
}
}
// resolveBindingRepo returns the binding's pinned owner/repo, or the only
// configured repo. With zero or several repos and no pin it refuses.
func (d *Daemon) resolveBindingRepo(b binding.Binding) (owner, repo, reason string) {
func (bd *BindingDispatch) resolveBindingRepo(b binding.Binding) (owner, repo, reason string) {
if b.Owner != "" && b.Repo != "" {
return b.Owner, b.Repo, ""
}
repos := d.Cfg.Get().Repos
repos := bd.cfg.Get().Repos
if len(repos) == 1 {
return repos[0].Owner, repos[0].Name, ""
}
@@ -261,15 +337,15 @@ func (d *Daemon) resolveBindingRepo(b binding.Binding) (owner, repo, reason stri
// activeWorkflows returns the definitions a dispatch cycle checks bindings
// against, the same set a task is later pinned from.
func (d *Daemon) activeWorkflows(ctx context.Context) (workflow.WorkflowDefinitionCollection, error) {
collection, _, err := d.WorkflowDefinitions.WorkflowDefinitions(ctx)
func (bd *BindingDispatch) activeWorkflows(ctx context.Context) (workflow.WorkflowDefinitionCollection, error) {
collection, _, err := bd.definitions.WorkflowDefinitions(ctx)
return collection, err
}
// workflowEnablement returns which workflows each org has disabled; with no
// source wired every workflow is enabled.
func (d *Daemon) workflowEnablement(ctx context.Context) (workflowtask.WorkflowEnablement, error) {
return d.WorkflowEnablement.WorkflowEnablement(ctx)
func (bd *BindingDispatch) workflowEnablement(ctx context.Context) (workflowtask.WorkflowEnablement, error) {
return bd.enablement.WorkflowEnablement(ctx)
}
// disabledRoutedWorkflow reports the workflow an issue's labels route to and
@@ -278,12 +354,12 @@ func (d *Daemon) workflowEnablement(ctx context.Context) (workflowtask.WorkflowE
// definitions, enablement) is an error so the caller falls through to
// publishing rather than dropping the issue; no routable definition is not an
// error, it is reported as not disabled and the pin path parks the task.
func (d *Daemon) disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error) {
orgID, err := d.identityOrg(ctx, identity.IdentityID(identityID))
func (bd *BindingDispatch) disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error) {
orgID, err := bd.orgs.identityOrg(ctx, identity.IdentityID(identityID))
if err != nil {
return "", false, err
}
definitions, err := d.activeWorkflows(ctx)
definitions, err := bd.activeWorkflows(ctx)
if err != nil {
return "", false, err
}
@@ -293,7 +369,7 @@ func (d *Daemon) disabledRoutedWorkflow(ctx context.Context, labels, identityID
}
routed, resolveErr := workflow.ResolveWorkflowID(&workflow.Task{Labels: labels}, available)
if resolveErr == nil {
enablement, err := d.workflowEnablement(ctx)
enablement, err := bd.workflowEnablement(ctx)
if err != nil {
return "", false, err
}
+3 -2
View File
@@ -108,15 +108,16 @@ func TestBindingDispatchCreatesOneTask(t *testing.T) {
store := &ledgerStore{claimed: map[string]bool{}, failures: tc.failures}
d := wired(&Daemon{
Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler),
Bindings: store, BindingDispatcher: store, BindingTaskCreator: store, Mappings: store,
WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n"},
})
d.BindingDispatch.bindings, d.BindingDispatch.dispatcher = store, store
d.BindingDispatch.creator, d.BindingDispatch.mappings = store, store
for range tc.cycles {
var listed, done sync.WaitGroup
listed.Add(tc.concurrent)
store.listed = &listed
for range tc.concurrent {
done.Go(func() { d.dispatchBindings(t.Context()) })
done.Go(func() { d.BindingDispatch.dispatchBindings(t.Context()) })
}
done.Wait()
}
+5 -5
View File
@@ -21,7 +21,7 @@ type bindingTarget struct {
// resolveBindingTarget checks a matched binding against the workflow it
// targets and resolves the task's repository and inputs. A non-empty reason
// means the binding does not dispatch and the reason is recorded on it.
func (d *Daemon) resolveBindingTarget(b binding.Binding, values map[string]any, workflows workflow.WorkflowDefinitionCollection) (target bindingTarget, reason string, ok bool) {
func (bd *BindingDispatch) resolveBindingTarget(b binding.Binding, values map[string]any, workflows workflow.WorkflowDefinitionCollection) (target bindingTarget, reason string, ok bool) {
entry, found := workflows.DefinitionByID(b.Workflow)
if !found {
return bindingTarget{}, fmt.Sprintf("workflow %q is not defined", b.Workflow), false
@@ -43,7 +43,7 @@ func (d *Daemon) resolveBindingTarget(b binding.Binding, values map[string]any,
if err != nil {
return bindingTarget{}, err.Error(), false
}
if !d.repoConfigured(owner, repo) {
if !bd.repoConfigured(owner, repo) {
return bindingTarget{}, fmt.Sprintf("repository %s/%s from the event is not a configured repository", owner, repo), false
}
target.owner, target.repo = owner, repo
@@ -51,7 +51,7 @@ func (d *Daemon) resolveBindingTarget(b binding.Binding, values map[string]any,
case mode == workflowtask.RepositoryOptional && b.Owner == "":
return target, "", true
}
owner, repo, reason := d.resolveBindingRepo(b)
owner, repo, reason := bd.resolveBindingRepo(b)
if reason != "" {
return bindingTarget{}, reason, false
}
@@ -62,8 +62,8 @@ func (d *Daemon) resolveBindingTarget(b binding.Binding, values map[string]any,
// repoConfigured reports whether owner/repo is one of the configured repos. A
// repository named by an event must be one, so a payload cannot point an
// agent at an arbitrary repository.
func (d *Daemon) repoConfigured(owner, repo string) bool {
for _, r := range d.Cfg.Get().Repos {
func (bd *BindingDispatch) repoConfigured(owner, repo string) bool {
for _, r := range bd.cfg.Get().Repos {
if r.Owner == owner && r.Name == repo {
return true
}
+3 -23
View File
@@ -20,7 +20,6 @@ import (
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/eventbus"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/forge"
@@ -85,20 +84,9 @@ type Daemon struct {
// startup, passed to containers.
ConnectedStateStore StateStoreEndpoint
Store storecontract.TaskStore
// Mappings persists payload field mappings, which the binding dispatch
// loop uses to resolve capture bodies against the mapping a binding names.
Mappings storecontract.MappingStore
// Bindings persists playbook bindings.
Bindings storecontract.BindingStore
// BindingDispatcher lists undispatched captures, finds armed bindings and
// records dispatches.
BindingDispatcher storecontract.BindingDispatcher
// MappingMatches counts each event a mapping resolved at dispatch.
MappingMatches storecontract.MappingMatchRecorder
// BindingTaskCreator enqueues a task triggered by a binding. Split
// off TaskLifecycle so the lifecycle surface stays narrow and the
// dispatch loop does not acquire the full task-creation contract.
BindingTaskCreator storecontract.BindingTaskCreator
// BindingDispatch offers captured events to armed bindings and starts their
// tasks. Built by the composition root from its own stores.
BindingDispatch *BindingDispatch
// Access evaluates the policy chain at dispatch, the second of the two
// Authorizer call sites: the workflow's identity may `run` the workflow in
// its workspace.
@@ -148,9 +136,6 @@ type Daemon struct {
Storage storage.Backend
// IssuePoller discovers forge issues and publishes them as tasks.
IssuePoller *IssuePoller
// OrgPrincipals is one acting principal per organisation, system org
// first.
OrgPrincipals func(context.Context) []access.Principal
// IdentityRepository is the durable lifecycle authority. Polling checks it
// every cycle so suspend/reactivate commands do not require a restart.
IdentityRepository identity.Repository
@@ -173,11 +158,6 @@ type Daemon struct {
WorkflowDefinitions interface {
WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error)
}
// WorkflowEnablement supplies which workflows each org has disabled. A
// binding targeting a workflow its org disabled does not dispatch.
WorkflowEnablement interface {
WorkflowEnablement(context.Context) (workflowtask.WorkflowEnablement, error)
}
// TaskLogs persists each task's log output and mirrors it to the dashboard.
// Nil disables task logging.
+3 -2
View File
@@ -92,9 +92,10 @@ func TestPollNATSSkipsDisabledRoutedWorkflow(t *testing.T) {
var logs bytes.Buffer
d := wired(&Daemon{
Log: slog.New(slog.NewTextHandler(&logs, nil)), Store: taskLookup{}, Tasks: bus,
WorkflowDefinitions: tt.definitions, WorkflowEnablement: tt.enablement,
Principals: tt.principals,
WorkflowDefinitions: tt.definitions,
Principals: tt.principals,
})
d.BindingDispatch.enablement = tt.enablement
is := forge.Issue{Number: 42, Title: "crash on startup"}
if created := d.IssuePoller.pollNATS(t.Context(), fg, cfg, repo, is, tt.labels, "acme-bot"); created != tt.wantCreated {
t.Errorf("pollNATS created %v, want %v", created, tt.wantCreated)
+17 -9
View File
@@ -14,10 +14,15 @@ import (
"github.com/samcharles93/archie-core/internal/taskstate"
)
// intakeGate is the identity and binding checks a poll reads through. The
// Daemon satisfies it; the poller keeps no state of its own for either.
type intakeGate interface {
// activeIdentities answers whether an identity may still act; a poll skips
// sources whose identity is suspended.
type activeIdentities interface {
identityActive(ctx context.Context, id identity.IdentityID) bool
}
// routedWorkflows answers whether an issue's labels route it to a workflow
// its org disabled.
type routedWorkflows interface {
disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error)
}
@@ -30,7 +35,8 @@ type IssuePoller struct {
sources func(context.Context) []IssueSource
withdrawTask func(ctx context.Context, taskID int64, reason string) error
publishTask func(context.Context, workintake.TaskEnvelope) error
gate intakeGate
identities activeIdentities
routing routedWorkflows
// lastPolled remembers each repo's eligible issues from its previous
// complete poll, under the dispatch rule that produced them, so only an
@@ -42,7 +48,7 @@ type IssuePoller struct {
// NewIssuePoller builds the poller over the running configuration, the task
// store and the task publisher. sources resolves each poll cycle's connections,
// withdrawTask declines a task whose issue a complete poll no longer returns,
// and gate answers the identity and routed-workflow checks.
// identities and routing answer the identity and routed-workflow checks.
func NewIssuePoller(
cfg *config.Holder,
store storecontract.TaskStore,
@@ -50,7 +56,8 @@ func NewIssuePoller(
sources func(context.Context) []IssueSource,
withdrawTask func(ctx context.Context, taskID int64, reason string) error,
publishTask func(context.Context, workintake.TaskEnvelope) error,
gate intakeGate,
identities activeIdentities,
routing routedWorkflows,
) *IssuePoller {
return &IssuePoller{
cfg: cfg,
@@ -59,7 +66,8 @@ func NewIssuePoller(
sources: sources,
withdrawTask: withdrawTask,
publishTask: publishTask,
gate: gate,
identities: identities,
routing: routing,
lastPolled: map[string]polledIssues{},
}
}
@@ -126,7 +134,7 @@ func (p *IssuePoller) poll(ctx context.Context) error {
var failed []string
room := p.cfg.Get().IntakePerPass
for _, source := range p.issueSources(ctx) {
if !p.gate.identityActive(ctx, source.Identity) {
if !p.identities.identityActive(ctx, source.Identity) {
continue
}
cfg := p.cfg.Get()
@@ -224,7 +232,7 @@ func (p *IssuePoller) pollNATS(ctx context.Context, fg forge.Forge, cfg config.C
// An issue whose label-routed workflow the identity's org disabled must
// not queue a task at all. Skip without acknowledging, so a later poll
// picks the issue up again if the workflow is re-enabled.
if routed, disabled, err := p.gate.disabledRoutedWorkflow(ctx, labels, identity); err != nil {
if routed, disabled, err := p.routing.disabledRoutedWorkflow(ctx, labels, identity); err != nil {
p.log.WarnContext(ctx, "routed workflow check failed; publishing anyway", "repo", repo.FullName(), "issue", is.Number, "err", err)
} else if disabled {
p.log.InfoContext(ctx, "issue skipped: its routed workflow is disabled for this org", "repo", repo.FullName(), "issue", is.Number, "workflow", routed)
+1 -1
View File
@@ -120,7 +120,7 @@ func (d *Daemon) RunOnce(ctx context.Context) {
d.Log.WarnContext(ctx, "built-in job failed", "job", job.ID, "err", err)
}
}
d.dispatchBindings(ctx)
d.BindingDispatch.dispatchBindings(ctx)
d.Cycle(ctx)
d.drainNATS(ctx)
ticker := time.NewTicker(time.Second)
+1 -1
View File
@@ -57,7 +57,7 @@ func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task)
if err := d.pinDefinition(ctx, task); err != nil {
return err
}
enablement, err := d.workflowEnablement(ctx)
enablement, err := d.BindingDispatch.workflowEnablement(ctx)
if err != nil {
return pinFailure{taskstate.ParkTransient, fmt.Errorf("workflow enablement: %w", err)}
}
+1 -1
View File
@@ -27,7 +27,7 @@ func (d *Daemon) drainLoop(ctx context.Context) {
defer ticker.Stop()
for {
d.markPass("drain")
d.dispatchBindings(ctx)
d.BindingDispatch.dispatchBindings(ctx)
d.drainNATS(ctx)
select {
case <-ctx.Done():
+37 -12
View File
@@ -7,6 +7,7 @@ import (
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
@@ -65,31 +66,55 @@ func wired(d *Daemon) *Daemon {
if d.WorkflowDefinitions == nil {
d.WorkflowDefinitions = neutralDefinitions{}
}
if d.WorkflowEnablement == nil {
d.WorkflowEnablement = neutralEnablement{}
}
if d.OrgPrincipals == nil {
d.OrgPrincipals = func(context.Context) []access.Principal {
return []access.Principal{{Org: org.DefaultOrgID}}
}
}
if d.MappingMatches == nil {
d.MappingMatches = neutralMatches{}
}
if d.AgentStatus == nil {
d.AgentStatus = &AgentStatus{}
}
if d.Log == nil {
d.Log = slog.New(slog.DiscardHandler)
}
if d.BindingDispatch == nil {
d.BindingDispatch = NewBindingDispatch(BindingDispatchDeps{
Store: d.Store, Mappings: neutralMappings{}, Bindings: neutralBindings{},
Dispatcher: neutralDispatcher{}, Matches: neutralMatches{}, Creator: neutralCreator{},
Enablement: neutralEnablement{}, Definitions: d.WorkflowDefinitions,
Access: d.Access, Denials: d.Denials, Cfg: d.Cfg, Orgs: d,
OrgPrincipals: func(context.Context) []access.Principal {
return []access.Principal{{Org: org.DefaultOrgID}}
},
Log: d.Log,
})
}
d.IssuePoller = NewIssuePoller(d.Cfg, d.Store, d.Log,
func(context.Context) []IssueSource { return nil },
func(context.Context, int64, string) error { return nil },
d.PublishTask, d)
d.PublishTask, d, d.BindingDispatch)
d.ReviewScanner = NewReviewScanner(d.Store, neutralReactions{}, d.Tasks.PublishUnique, d.Bus, d, d, d, d.Log)
return d
}
// neutralMappings resolves no mapping, so a binding never reaches a capture.
type neutralMappings struct{ storecontract.MappingStore }
// neutralBindings lists no armed binding, so no capture is offered.
type neutralBindings struct{ storecontract.BindingStore }
func (neutralBindings) ListBindings(context.Context) ([]binding.Binding, error) { return nil, nil }
// neutralDispatcher lists no capture. Any other dispatch call panics, so a test
// that reaches one sets its own dispatcher.
type neutralDispatcher struct {
storecontract.BindingDispatcher
}
func (neutralDispatcher) ListUndispatchedCaptures(context.Context, []string, int) ([]storecontract.CapturedEvent, error) {
return nil, nil
}
// neutralCreator enqueues nothing. A test that reaches it sets its own creator.
type neutralCreator struct {
storecontract.BindingTaskCreator
}
// neutralTaskBus takes no work and accepts every publish.
type neutralTaskBus struct{}
+1 -1
View File
@@ -135,7 +135,7 @@ func TestWorkflowIdentity(t *testing.T) {
IdentityRepository: agentDirectory{}, AgentAssignments: agentDirectory{},
})
if tt.disabled {
d.WorkflowEnablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("", "deploy", false))
d.BindingDispatch.enablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("", "deploy", false))
}
task := &workflow.Task{
ID: 1, Identity: tt.starter, Workflow: "deploy",
+9 -6
View File
@@ -88,16 +88,19 @@ func TestDispatchOrgWorkflows(t *testing.T) {
{ID: "root", MappingID: "root", Matcher: binding.Matcher{Source: "root"}, OrgID: org.DefaultOrgID, Workflow: "deploy", Status: binding.StatusArmed, Owner: "owner", Repo: "repo"},
{ID: "acme", MappingID: "acme", Matcher: binding.Matcher{Source: "acme"}, OrgID: "acme", Workflow: "deploy", Status: binding.StatusArmed},
}}
d := wired(&Daemon{Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler), Bindings: store, BindingDispatcher: store, BindingTaskCreator: store, Mappings: store, OrgPrincipals: func(context.Context) []access.Principal {
return []access.Principal{{IdentityID: identity.SystemID, Org: org.DefaultOrgID}, {IdentityID: "acme-bot", Org: "acme"}}
}, Principals: workflowPrincipals{sys: org.DefaultOrgID, "acme-bot": "acme"}, WorkflowDefinitions: orgDefinitions{
d := wired(&Daemon{Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler), Principals: workflowPrincipals{sys: org.DefaultOrgID, "acme-bot": "acme"}, WorkflowDefinitions: orgDefinitions{
org.DefaultOrgID: "id: deploy\nrepository: required\nsteps:\n - type: workflow.finish\n",
"acme": "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n",
}})
if disabled {
d.WorkflowEnablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("acme", "deploy", false))
d.BindingDispatch.bindings, d.BindingDispatch.dispatcher = store, store
d.BindingDispatch.creator, d.BindingDispatch.mappings = store, store
d.BindingDispatch.orgPrincipals = func(context.Context) []access.Principal {
return []access.Principal{{IdentityID: identity.SystemID, Org: org.DefaultOrgID}, {IdentityID: "acme-bot", Org: "acme"}}
}
d.dispatchBindings(t.Context())
if disabled {
d.BindingDispatch.enablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("acme", "deploy", false))
}
d.BindingDispatch.dispatchBindings(t.Context())
root := store.started[org.DefaultOrgID]
if root == nil || root.Repo != "repo" || root.Identity != string(sys) {
t.Fatalf("root dispatch %#v", root)