Label to workflow rules and a default workflow are the intake-routes resource: ordered, case-insensitive, first rule wins, seeded with bug to tdd, feature to feasibility, default triage. A write naming an undefined workflow or an empty label is refused. The daemon reads it through the workflow definitions client when pinning a task and at intake. The kind indirection only carried this routing, so it is removed: the per-kind NATS subjects (every consumer already bound archie.task.>) are one subject, the envelope and playbook triggers lose their kind, and x-intake-routes is dropped. The dashboard gets an Issue routing page under Automation; the trigger card reads the same setting.
172 lines
7.2 KiB
Go
172 lines
7.2 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/eda/playbook"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
// pinTaskProfile pins the task's workflow definition and resolves its agent
|
|
// profile, parking the task on failure.
|
|
func (d *Daemon) pinTaskProfile(ctx context.Context, task *workflow.Task) (config.AgentProfile, bool) {
|
|
if err := d.pinWorkflowDefinition(ctx, task); err != nil {
|
|
d.Log.ErrorContext(ctx, "pin workflow definition failed", "task", task.ID, "err", err)
|
|
d.parkBlockedTask(ctx, task, "pin workflow definition: "+err.Error(), pinParkClass(err), parkBlocker(err))
|
|
return config.AgentProfile{}, false
|
|
}
|
|
iface, err := workflowtask.ParseWorkflowInterface(task.WorkflowDefinitionYAML)
|
|
if err == nil {
|
|
// Every producer meets the declared interface here, including the
|
|
// ones that cannot read definitions when they enqueue (chat and
|
|
// dashboard spawns go through the Gateway, which never does).
|
|
err = iface.CheckInputs(task.Inputs)
|
|
}
|
|
if err == nil && iface.RepositoryMode() == workflowtask.RepositoryRequired && !task.HasRepository() {
|
|
// An optional workflow may hand off to one that requires a
|
|
// repository, because its task may carry one; a task that does not
|
|
// is parked here rather than run the target's steps in a scratch
|
|
// workspace.
|
|
err = fmt.Errorf("requires a repository and this task has none")
|
|
}
|
|
if err == nil {
|
|
var profile config.AgentProfile
|
|
if profile, err = d.configFor(task).Containers.Profile(iface.Profile); err == nil {
|
|
return profile, true
|
|
}
|
|
}
|
|
d.Log.ErrorContext(ctx, "agent profile unavailable", "task", task.ID, "err", err)
|
|
d.parkRunningTask(ctx, task, "workflow "+task.Workflow+": "+err.Error(), taskstate.ParkNeedsDecision)
|
|
return config.AgentProfile{}, false
|
|
}
|
|
|
|
// pinWorkflowDefinition ensures the task carries a valid definition pin for
|
|
// the workflow it names and that its org has the workflow enabled. Every run
|
|
// passes through here, whatever started it, so this is the one enablement
|
|
// check for issue labels, handoffs, approvals, calls and playbooks alike.
|
|
func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task) error {
|
|
ctx, err := d.Identity.workflowContext(ctx, task)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := d.pinDefinition(ctx, task); err != nil {
|
|
return err
|
|
}
|
|
enablement, err := d.BindingDispatch.workflowEnablement(ctx)
|
|
if err != nil {
|
|
return pinFailure{taskstate.ParkTransient, fmt.Errorf("workflow enablement: %w", err)}
|
|
}
|
|
if !enablement.Enabled(task.Org, task.Workflow) {
|
|
return blockedPark{taskstate.BlockerWorkflowDisabled, pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("workflow %s is disabled", task.Workflow)}}
|
|
}
|
|
if err := d.checkPackageTrigger(ctx, task); err != nil {
|
|
return pinFailure{taskstate.ParkNeedsDecision, err}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// pinDefinition keeps a valid pin and resolves a missing or mismatched one
|
|
// again from the active definitions.
|
|
func (d *Daemon) pinDefinition(ctx context.Context, task *workflow.Task) error {
|
|
pinned, ok, err := workflow.PinnedDefinitionID(task)
|
|
if err != nil {
|
|
return pinFailure{taskstate.ParkNeedsDecision, err}
|
|
}
|
|
if ok && pinned == task.Workflow {
|
|
return nil
|
|
}
|
|
d.runActionPlaybooks(ctx, task)
|
|
collection, version, err := d.WorkflowDefinitions.WorkflowDefinitions(ctx)
|
|
if err != nil {
|
|
return pinFailure{taskstate.ParkTransient, err}
|
|
}
|
|
return d.pinWorkflowFromCollection(ctx, task, collection, version)
|
|
}
|
|
|
|
func (d *Daemon) pinWorkflowFromCollection(ctx context.Context, task *workflow.Task, collection workflow.WorkflowDefinitionCollection, version int64) error {
|
|
available := make(map[string]struct{}, len(collection.Definitions))
|
|
for _, definition := range collection.Definitions {
|
|
available[definition.ID] = struct{}{}
|
|
}
|
|
routes, err := d.WorkflowDefinitions.IntakeRoutes(ctx)
|
|
if err != nil {
|
|
return pinFailure{taskstate.ParkTransient, fmt.Errorf("intake routes: %w", err)}
|
|
}
|
|
id, err := d.resolveWorkflowID(ctx, task, routes, available)
|
|
if err != nil {
|
|
return pinFailure{taskstate.ParkNeedsDecision, err}
|
|
}
|
|
definition, ok := collection.DefinitionByID(id)
|
|
if !ok {
|
|
return pinFailure{taskstate.ParkNeedsDecision, fmt.Errorf("workflow definition %q disappeared", id)}
|
|
}
|
|
task.Workflow = id
|
|
task.WorkflowDefinitionVersion = version
|
|
task.WorkflowDefinitionYAML = definition.YAML
|
|
task.WorkflowDefinitionDigest = workflow.DigestDefinition(definition.YAML)
|
|
if err := d.Store.Update(ctx, task); err != nil {
|
|
return pinFailure{taskstate.ParkTransient, fmt.Errorf("persist workflow definition pin: %w", err)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// resolveWorkflowID picks the definition to pin: a matching playbook, then
|
|
// the label routes. A task already naming a workflow keeps it.
|
|
func (d *Daemon) resolveWorkflowID(ctx context.Context, task *workflow.Task, routes workflow.IntakeRoutes, available map[string]struct{}) (string, error) {
|
|
if task.Workflow != "" {
|
|
return workflow.ResolveWorkflowID(task, routes, available)
|
|
}
|
|
decision, matched := d.Playbooks.Dispatch(ctx, playbookInput(task))
|
|
if !matched {
|
|
return workflow.ResolveWorkflowID(task, routes, available)
|
|
}
|
|
if _, defined := available[decision.Workflow]; !defined {
|
|
// Reported, not silently downgraded to the binding's choice: the
|
|
// operator bound this trigger to a named workflow, and running a
|
|
// different one under their rule is worse than parking the task.
|
|
return "", fmt.Errorf("playbook %q binds this trigger to workflow %q, which is not defined", decision.PlaybookID, decision.Workflow)
|
|
}
|
|
d.Log.InfoContext(ctx, "playbook selected workflow",
|
|
"task", task.ID, "playbook", decision.PlaybookID,
|
|
"playbook_version", decision.Version, "workflow", decision.Workflow)
|
|
return decision.Workflow, nil
|
|
}
|
|
|
|
// runActionPlaybooks runs the action playbooks matching a task before it is
|
|
// pinned. Failures never fail the task; tasks already naming a workflow are
|
|
// skipped.
|
|
func (d *Daemon) runActionPlaybooks(ctx context.Context, task *workflow.Task) {
|
|
if task.Workflow != "" {
|
|
return
|
|
}
|
|
if err := d.Playbooks.Run(ctx, d.PlaybookLedger, d.Log, playbookInput(task)); err != nil {
|
|
d.Log.WarnContext(ctx, "action playbook run failed", "task", task.ID, "err", err)
|
|
}
|
|
}
|
|
|
|
// playbookInput builds the coordinator's dispatch input from a task row. The
|
|
// event surface is what the intake point actually knows about the originating
|
|
// issue -- the fields a `when` condition can read; it is deliberately not
|
|
// padded with values the daemon would have to invent.
|
|
func playbookInput(task *workflow.Task) playbook.DispatchInput {
|
|
labels := workintake.SplitLabels(task.Labels)
|
|
return playbook.DispatchInput{
|
|
Labels: labels,
|
|
TaskID: workintake.TaskEnvelope{Org: task.Org, Identity: task.Identity, Owner: task.Owner, Repo: task.Repo, Number: task.IssueNumber}.IdempotencyKey(),
|
|
Event: map[string]any{
|
|
"labels": labels,
|
|
"owner": task.Owner,
|
|
"repo": task.Repo,
|
|
"number": task.IssueNumber,
|
|
"title": task.Title,
|
|
"body": task.Body,
|
|
"source": task.Source,
|
|
},
|
|
}
|
|
}
|