package daemon 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" "github.com/samcharles93/archie-core/internal/domain/mapping" "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/events" ) // workflowDefinitionSource supplies the active workflow definitions and the // routes that send an issue to one by its labels. type workflowDefinitionSource interface { WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error) IntakeRoutes(context.Context) (workflow.IntakeRoutes, 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 (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 bd.dispatchOrgBindings(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org)) } } 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 := bd.bindings.ListBindings(ctx) if err != nil { bd.log.WarnContext(ctx, "list bindings failed", "error", err) return } enablement, err := bd.workflowEnablement(ctx) if err != nil { bd.log.WarnContext(ctx, "binding dispatch: workflow enablement unavailable", "error", err) return } enabled := make([]string, 0, len(bindings)) armed := make([]binding.Binding, 0, len(bindings)) for _, b := range bindings { if b.OrgID == org.OrgFromContext(ctx) && b.Status == binding.StatusArmed && b.Matcher.Source != "" && enablement.Enabled(b.OrgID, b.Workflow) { enabled = append(enabled, b.ID) armed = append(armed, b) } } if len(enabled) == 0 { return } captures, err := bd.dispatcher.ListUndispatchedCaptures(ctx, enabled, bindingDispatchBatchLimit) if err != nil { bd.log.WarnContext(ctx, "list undispatched captures failed", "error", err) return } workflows, err := bd.activeWorkflows(ctx) if err != nil { bd.log.WarnContext(ctx, "binding dispatch: workflow definitions unavailable", "error", err) return } for _, c := range captures { 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 (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()) { 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 (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 { bd.log.WarnContext(ctx, "binding dispatch: get mapping", "binding", b.ID, "mapping", b.MappingID, "error", err) return nil } if m == nil { bd.recordDispatchFailure(ctx, b, c, fmt.Sprintf("mapping %q does not exist", b.MappingID), nil) } return m } // dispatchOneBinding offers one capture to one binding: the mapping must // match the capture's event type and resolve required fields, the filter // 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 (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) { bd.recordDispatchFailure(ctx, b, c, "required field failed", map[string]any{"failures": failures}) return } 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 { bd.recordDispatchFailure(ctx, b, c, "filter does not compile", map[string]any{"error": err.Error()}) return } if admitted, _ := filter.Admits(values); !admitted { bd.recordEvaluated(ctx, b, c, "filtered") return } target, reason, ok := bd.resolveBindingTarget(b, values, workflows) if !ok { 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 !bd.authorizeDispatch(ctx, b, c, target) { return } 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 (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 { body = fmt.Sprintf("Started by binding %q from capture %q; the workflow's inputs travel with the task.", b.Name, c.ID) } taskIdentity := string(identity.SystemID) if principal, ok := access.PrincipalFromContext(ctx); ok { taskIdentity = string(principal.IdentityID) } 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) { bd.log.WarnContext(ctx, "binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err) } return } 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 (bd *BindingDispatch) authorizeDispatch(ctx context.Context, b binding.Binding, c storecontract.CapturedEvent, target bindingTarget) bool { principal, ok := access.PrincipalFromContext(ctx) if !ok { 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 := bd.authorizer.Authorize(ctx, principal, access.ActionRun, resource, contextValue) if decision.Allowed { return true } if errors.Is(decision.Err, access.ErrChainUnavailable) { // 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. 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{ Principal: principal.IdentityID, Org: principal.Org, Action: access.ActionRun, Kind: resource.Kind, ResourceID: resource.ID, Level: decision.Level, Policies: decision.Policies, } if err := bd.denials.RecordDenial(ctx, denial); err != nil { bd.log.WarnContext(ctx, "binding dispatch: record denial", "binding", b.ID, "error", err) } bd.recordDispatchFailure(ctx, b, c, "access denied", map[string]any{ "level": string(decision.Level), "policies": decision.Policies, }) return false } // markBindingStart records on a task's timeline the binding and captured // event that started it. 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), Data: map[string]any{ "binding_id": b.ID, "binding_name": b.Name, "binding_version": b.Version, "capture_id": c.ID, "source": c.Source, "unsigned": c.Unsigned, }, }); err != nil { 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 (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) _, _ = 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, }) } // 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 (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) { 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 (bd *BindingDispatch) resolveBindingRepo(b binding.Binding) (owner, repo, reason string) { if b.Owner != "" && b.Repo != "" { return b.Owner, b.Repo, "" } repos := bd.cfg.Get().Repos if len(repos) == 1 { return repos[0].Owner, repos[0].Name, "" } return "", "", fmt.Sprintf("cannot pick a repository: %d are configured; pin the binding to one", len(repos)) } // activeWorkflows returns the definitions a dispatch cycle checks bindings // against, the same set a task is later pinned from. 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 (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 // whether the identity's org has that workflow disabled, matching the // enablement filter binding dispatch applies. A read failure (org, // 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 (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 := bd.activeWorkflows(ctx) if err != nil { return "", false, err } available := make(map[string]struct{}, len(definitions.Definitions)) for _, definition := range definitions.Definitions { available[definition.ID] = struct{}{} } routes, err := bd.definitions.IntakeRoutes(ctx) if err != nil { return "", false, err } routed, resolveErr := workflow.ResolveWorkflowID(&workflow.Task{Labels: labels}, routes, available) if resolveErr == nil { enablement, err := bd.workflowEnablement(ctx) if err != nil { return "", false, err } return routed, !enablement.Enabled(orgID, routed), nil } // No routable definition is a normal outcome here, not a read failure: // keep the publish behaviour and let the pin path park the task. return "", false, nil } // bindingDispatchBatchLimit caps how many undispatched captures a single // dispatchBindings pass will fetch from the store. The cap is defensive // (the captures table is also pruned by retention/maxEvents) and bounds a // single cycle's worst-case latency on a backlog. const bindingDispatchBatchLimit = 100 // signatureOf names the event's signature result as the dispatch context // carries it: a signed event and an approved unsigned event are the two // dispatchable shapes (storecontract.CapturedEvent.Dispatchable). func signatureOf(c storecontract.CapturedEvent) string { switch { case c.Authenticated: return "valid" case c.Unsigned: return "unsigned" } return "unverified" } // hasBlockingFailure reports whether any failure is on a required field. func hasBlockingFailure(fields []mapping.Field, failures []mapping.Failure) bool { if len(failures) == 0 { return false } required := make(map[string]bool, len(fields)) for _, f := range fields { if f.Required { required[f.Name] = true } } for _, fail := range failures { if required[fail.FieldName] { return true } } return false } // renderBindingBody builds the task body from the values a mapping // resolved, one "key=value" pair per line. Empty maps still produce // a body that records the source capture id so the workflow has // provenance to hand back to the operator. func renderBindingBody(values map[string]any, c storecontract.CapturedEvent) string { if len(values) == 0 { return fmt.Sprintf("(no fields resolved from capture %q)", c.ID) } parts := make([]string, 0, len(values)) for k, v := range values { parts = append(parts, fmt.Sprintf("%s=%v", k, v)) } return strings.Join(parts, "\n") }