From 41f44c43a1f4c1e115caef367974df99220fd2ed Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Fri, 13 Nov 2026 20:28:36 +1100 Subject: [PATCH] feat(workflow): issue routing is a control-plane setting edited in the dashboard (#1959) 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. --- WORKFLOWS.md | 8 +- internal/daemon/binding_dispatch.go | 10 +- internal/daemon/daemon.go | 9 +- internal/daemon/intake_disabled_test.go | 4 + internal/daemon/issue_poll.go | 5 +- internal/daemon/pinning.go | 17 +-- internal/daemon/task_bus.go | 3 - internal/daemon/wiring_test.go | 4 + internal/daemon/workflow_org_test.go | 4 + internal/domain/applystatus/applystatus.go | 2 +- internal/domain/eda/playbook/playbook.go | 52 ++------ internal/domain/workflow/routing.go | 107 +++++++++------- internal/domain/workflow/routing_test.go | 52 ++++++++ .../workflow/triage_repo_optional_test.go | 2 +- internal/domain/workflow/workflow.go | 9 +- internal/domain/workintake/envelope.go | 9 +- internal/domain/workintake/subjects.go | 97 ++------------ internal/gateway/integration_tools.go | 2 +- .../controlplanestore/controlplane.go | 2 +- .../controlplanestore/definitions.go | 2 +- .../controlplanestore/intake_routes.go | 70 +++++++++++ .../controlplanestore/org_models.go | 2 + .../controlplanestore/workflow_definitions.go | 2 +- ui/src/lib/nav.ts | 2 +- ui/src/router/index.ts | 6 + ui/src/settings/IssueRoutingPage.vue | 119 ++++++++++++++++++ ui/src/stores/workflows.ts | 5 - ui/src/workflows/workflow-triggers.ts | 23 ++-- 28 files changed, 395 insertions(+), 234 deletions(-) create mode 100644 internal/domain/workflow/routing_test.go create mode 100644 internal/infrastructure/controlplanestore/intake_routes.go create mode 100644 ui/src/settings/IssueRoutingPage.vue diff --git a/WORKFLOWS.md b/WORKFLOWS.md index 4649ef94c..e397bdaf6 100644 --- a/WORKFLOWS.md +++ b/WORKFLOWS.md @@ -314,8 +314,10 @@ expressions that limit which files the rule applies to. ## What starts a workflow -- **Issues**, by label: `bug` runs `tdd`, `feature` runs `feasibility`, anything - else runs `triage`, which classifies it and hands off. +- **Issues**, by label: the Issue routing setting lists label to workflow rules + (first match wins) and a default for the rest. It starts as `bug` runs + `tdd`, `feature` runs `feasibility`, anything else runs `triage`, which + classifies it and hands off. - **Event bindings**: a webhook source mapped onto a workflow and its inputs (Events page). - **Schedules**: each firing is captured on the reserved `schedule` source as @@ -323,7 +325,7 @@ expressions that limit which files the rule applies to. `fired_at`). A schedule trigger is a binding on that source using the `schedule-fired` mapping, filtered to one `schedule_id`. A firing starts at most one task per binding. -- **Playbooks**: a trigger (issue kind and labels) and actions that dispatch +- **Playbooks**: a trigger (issue labels) and actions that dispatch workflows. - **Other workflows**: `workflow.call` (a child run), `workflow.handoff` (the same task, requeued), `workflow.approve` (once approved). diff --git a/internal/daemon/binding_dispatch.go b/internal/daemon/binding_dispatch.go index 732249db8..a669f96f5 100644 --- a/internal/daemon/binding_dispatch.go +++ b/internal/daemon/binding_dispatch.go @@ -20,9 +20,11 @@ import ( "github.com/samcharles93/archie-core/internal/events" ) -// workflowDefinitionSource supplies the active workflow definitions. +// 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. @@ -367,7 +369,11 @@ func (bd *BindingDispatch) disabledRoutedWorkflow(ctx context.Context, labels, i for _, definition := range definitions.Definitions { available[definition.ID] = struct{}{} } - routed, resolveErr := workflow.ResolveWorkflowID(&workflow.Task{Labels: labels}, available) + 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 { diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index c8a764ef7..375406759 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -137,11 +137,10 @@ type Daemon struct { // PlaybookLedger is the at-most-once gate action playbooks run through. PlaybookLedger storecontract.PlaybookDispatcher - // WorkflowDefinitions supplies the active database definitions. A task is - // pinned once before dispatch; retries reuse the task's stored YAML. - WorkflowDefinitions interface { - WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error) - } + // WorkflowDefinitions supplies the active database definitions and the + // label routes that pick one. A task is pinned once before dispatch; + // retries reuse the task's stored YAML. + WorkflowDefinitions workflowDefinitionSource // TaskLogs persists each task's log output and mirrors it to the dashboard. // Nil disables task logging. diff --git a/internal/daemon/intake_disabled_test.go b/internal/daemon/intake_disabled_test.go index 4247f6607..e96b8464a 100644 --- a/internal/daemon/intake_disabled_test.go +++ b/internal/daemon/intake_disabled_test.go @@ -38,6 +38,10 @@ func (s staticDefinitions) WorkflowDefinitions(context.Context) (workflow.Workfl return s.collection, 1, s.err } +func (staticDefinitions) IntakeRoutes(context.Context) (workflow.IntakeRoutes, error) { + return workflow.ShippedIntakeRoutes(), nil +} + // enablementSource serves one enablement value, or an error. type enablementSource struct { value workflowtask.WorkflowEnablement diff --git a/internal/daemon/issue_poll.go b/internal/daemon/issue_poll.go index 3c1a56425..1da7de826 100644 --- a/internal/daemon/issue_poll.go +++ b/internal/daemon/issue_poll.go @@ -238,8 +238,8 @@ func (p *IssuePoller) pollNATS(ctx context.Context, fg forge.Forge, cfg config.C p.log.InfoContext(ctx, "issue skipped: its routed workflow is disabled for this org", "repo", repo.FullName(), "issue", is.Number, "workflow", routed) return false } - // Classify and encode here: the envelope, its routing kind and its - // subject belong to the work-intake domain, not to the transport. + // Encode here: the envelope and its subject belong to the work-intake + // domain, not to the transport. parsed := workintake.SplitLabels(labels) task := workintake.TaskEnvelope{ Owner: repo.Owner, @@ -249,7 +249,6 @@ func (p *IssuePoller) pollNATS(ctx context.Context, fg forge.Forge, cfg config.C Body: is.Body, Labels: parsed, Identity: identity, - Kind: workintake.KindForLabels(parsed), } if err := p.publishTask(ctx, task); err != nil { p.log.ErrorContext(ctx, "task publish failed", "repo", repo.FullName(), "issue", is.Number, "err", err) diff --git a/internal/daemon/pinning.go b/internal/daemon/pinning.go index 9d6b29e53..65213d91e 100644 --- a/internal/daemon/pinning.go +++ b/internal/daemon/pinning.go @@ -93,7 +93,11 @@ func (d *Daemon) pinWorkflowFromCollection(ctx context.Context, task *workflow.T for _, definition := range collection.Definitions { available[definition.ID] = struct{}{} } - id, err := d.resolveWorkflowID(ctx, task, available) + 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} } @@ -112,14 +116,14 @@ func (d *Daemon) pinWorkflowFromCollection(ctx context.Context, task *workflow.T } // resolveWorkflowID picks the definition to pin: a matching playbook, then -// the kind defaults. A task already naming a workflow keeps it. -func (d *Daemon) resolveWorkflowID(ctx context.Context, task *workflow.Task, available map[string]struct{}) (string, error) { +// 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, available) + return workflow.ResolveWorkflowID(task, routes, available) } decision, matched := d.Playbooks.Dispatch(ctx, playbookInput(task)) if !matched { - return workflow.ResolveWorkflowID(task, available) + return workflow.ResolveWorkflowID(task, routes, available) } if _, defined := available[decision.Workflow]; !defined { // Reported, not silently downgraded to the binding's choice: the @@ -151,13 +155,10 @@ func (d *Daemon) runActionPlaybooks(ctx context.Context, task *workflow.Task) { // padded with values the daemon would have to invent. func playbookInput(task *workflow.Task) playbook.DispatchInput { labels := workintake.SplitLabels(task.Labels) - kind := string(workintake.KindForLabels(labels)) return playbook.DispatchInput{ Labels: labels, - Kind: kind, TaskID: workintake.TaskEnvelope{Org: task.Org, Identity: task.Identity, Owner: task.Owner, Repo: task.Repo, Number: task.IssueNumber}.IdempotencyKey(), Event: map[string]any{ - "kind": kind, "labels": labels, "owner": task.Owner, "repo": task.Repo, diff --git a/internal/daemon/task_bus.go b/internal/daemon/task_bus.go index 357555313..c497b2e48 100644 --- a/internal/daemon/task_bus.go +++ b/internal/daemon/task_bus.go @@ -281,9 +281,6 @@ func (b *TaskBus) emitTaskQueued(ctx context.Context, task *workflow.Task) { // PublishTask validates a task envelope, stamps the producing identity's // org and publishes it. The idempotency key dedups rediscovered issues. func (b *TaskBus) PublishTask(ctx context.Context, task workintake.TaskEnvelope) error { - if err := task.Kind.Validate(); err != nil { - return fmt.Errorf("publish task %s: %w", task.Ref(), err) - } if task.Identity == "" { return fmt.Errorf("publish task %s: no identity", task.Ref()) } diff --git a/internal/daemon/wiring_test.go b/internal/daemon/wiring_test.go index e40c52163..b5adf5288 100644 --- a/internal/daemon/wiring_test.go +++ b/internal/daemon/wiring_test.go @@ -228,6 +228,10 @@ func (neutralDefinitions) WorkflowDefinitions(context.Context) (workflow.Workflo return workflow.ShippedDefinitions(), 0, nil } +func (neutralDefinitions) IntakeRoutes(context.Context) (workflow.IntakeRoutes, error) { + return workflow.ShippedIntakeRoutes(), nil +} + // neutralEnablement disables no workflow. type neutralEnablement struct{} diff --git a/internal/daemon/workflow_org_test.go b/internal/daemon/workflow_org_test.go index 761cf880e..11013d2f4 100644 --- a/internal/daemon/workflow_org_test.go +++ b/internal/daemon/workflow_org_test.go @@ -129,6 +129,10 @@ func (d orgDefinitions) WorkflowDefinitions(ctx context.Context) (workflow.Workf return workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "deploy", YAML: d[owner]}}}, 3, nil } +func (orgDefinitions) IntakeRoutes(context.Context) (workflow.IntakeRoutes, error) { + return workflow.ShippedIntakeRoutes(), nil +} + func TestPinWorkflowInTaskOrg(t *testing.T) { const shared = "id: deploy\nsteps:\n - type: workflow.finish\n" const custom = "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n" diff --git a/internal/domain/applystatus/applystatus.go b/internal/domain/applystatus/applystatus.go index 253e0d3ba..9f1b6fcea 100644 --- a/internal/domain/applystatus/applystatus.go +++ b/internal/domain/applystatus/applystatus.go @@ -32,7 +32,7 @@ func Processes() []string { return []string{Daemon, Gateway, Messaging} } // Applicability names the consumers of each resource, including boot-only settings. func Applicability() map[string][]string { kinds := map[string][]string{} - for _, kind := range []string{"task-policy", "provider-settings", "model-aliases", "repository-policies", "scheduling-policy", "tool-settings", "plugin-settings", "channel-settings", "container-runtime-policies", "credential-bindings", "identity-grants", "agent-profiles", "workflow-execution-settings", "extension-settings", "workflow-definitions", "workflow-enablement", "eda-playbooks", "forge-connections"} { + for _, kind := range []string{"task-policy", "provider-settings", "model-aliases", "repository-policies", "scheduling-policy", "tool-settings", "plugin-settings", "channel-settings", "container-runtime-policies", "credential-bindings", "identity-grants", "agent-profiles", "workflow-execution-settings", "extension-settings", "workflow-definitions", "workflow-enablement", "intake-routes", "eda-playbooks", "forge-connections"} { kinds[kind] = []string{Daemon} } for _, kind := range []string{"provider-settings", "model-aliases", "tool-settings", "extension-settings", "soul", "skills", "curators"} { diff --git a/internal/domain/eda/playbook/playbook.go b/internal/domain/eda/playbook/playbook.go index bd1c41735..cea8f919b 100644 --- a/internal/domain/eda/playbook/playbook.go +++ b/internal/domain/eda/playbook/playbook.go @@ -15,7 +15,6 @@ import ( "github.com/samcharles93/archie-core/internal/domain/eda/expr" "github.com/samcharles93/archie-core/internal/domain/stableid" - "github.com/samcharles93/archie-core/internal/domain/workintake" ) // KindSchemas is the narrow kind-to-schema source the loader consults to @@ -57,15 +56,9 @@ type Playbook struct { Actions []Action } -// Trigger decides which incoming events this playbook matches. It reuses the -// existing workintake label/kind vocabulary -- no second label-matching -// mechanism is invented here. +// Trigger decides which incoming events this playbook matches. type Trigger struct { - // Kind is the routing kind (bug/feature); empty matches the - // default (unlabelled) flow. - Kind workintake.Kind - // Labels is the closed label set this trigger matches; an empty set - // matches any labels. + // Labels is the label set a task must carry in full to match. Labels []string } @@ -100,7 +93,6 @@ type rawPlaybook struct { } type rawTrigger struct { - Kind string `yaml:"kind"` Labels []string `yaml:"labels"` } @@ -229,17 +221,11 @@ func compileOne(doc Document, schemas KindSchemas) (*Playbook, error) { pb := &Playbook{ ID: path, Version: fmt.Sprintf("%x", sha256.Sum256(doc.YAML)), - Trigger: Trigger{ - Kind: workintake.Kind(strings.TrimSpace(raw.Trigger.Kind)), - Labels: raw.Trigger.Labels, - }, + Trigger: Trigger{Labels: raw.Trigger.Labels}, } - if pb.Trigger.Kind == "" && len(raw.Trigger.Labels) == 0 { - return nil, fmt.Errorf("playbook %s: trigger must declare a kind or labels", path) - } - if err := pb.Trigger.Kind.Validate(); err != nil { - return nil, fmt.Errorf("playbook %s: %w", path, err) + if len(raw.Trigger.Labels) == 0 { + return nil, fmt.Errorf("playbook %s: trigger must declare labels", path) } actions, err := compileActions(path, raw.Actions, schemas) @@ -513,12 +499,10 @@ func compileExpr(path, field, src string, env *expr.Env) (*expr.Program, error) type DispatchInput struct { // Labels are the incoming task's labels (comma-split already). Labels []string - // Kind is the routing kind the labels produced (workintake.KindForLabels). - Kind string // TaskID is the originating task's idempotency key, used as the event id. TaskID string // Event is the event payload exposed as `event` in CEL expressions. For - // a workflow-kind dispatch this carries the label/kind fields cheaply + // a workflow-kind dispatch this carries the label fields cheaply // available at this point. Event map[string]any } @@ -531,25 +515,13 @@ func (pb *Playbook) IsActionPlaybook() bool { // Match reports whether the playbook's trigger matches the input's labels. func (pb *Playbook) Match(input DispatchInput) bool { - labels := input.Labels - if len(labels) == 0 { - labels = []string{} + inputSet := make(map[string]bool, len(input.Labels)) + for _, l := range input.Labels { + inputSet[strings.TrimSpace(l)] = true } - kind := workintake.Kind(input.Kind) - - if pb.Trigger.Kind != "" && pb.Trigger.Kind != kind { - return false - } - if len(pb.Trigger.Labels) > 0 { - // Trigger labels must all be present in the input's labels. - inputSet := make(map[string]bool, len(labels)) - for _, l := range labels { - inputSet[strings.TrimSpace(l)] = true - } - for _, want := range pb.Trigger.Labels { - if !inputSet[strings.TrimSpace(want)] { - return false - } + for _, want := range pb.Trigger.Labels { + if !inputSet[strings.TrimSpace(want)] { + return false } } return true diff --git a/internal/domain/workflow/routing.go b/internal/domain/workflow/routing.go index b45cfec8a..8ca5f9030 100644 --- a/internal/domain/workflow/routing.go +++ b/internal/domain/workflow/routing.go @@ -2,64 +2,83 @@ package workflow import ( "fmt" + "strings" "github.com/samcharles93/archie-core/internal/domain/workintake" ) -// defaultKindWorkflows maps each intake kind to its default workflow. -var defaultKindWorkflows = map[workintake.Kind]string{ - workintake.KindBug: "tdd", - workintake.KindFeature: "feasibility", +// IntakeRule sends an issue carrying Label to Workflow. +type IntakeRule struct { + Label string `json:"label" title:"Label" doc:"Matched without regard to case."` + Workflow string `json:"workflow" title:"Workflow"` } -// IntakeRoutes is where an issue goes by its label, as a dashboard shows it: -// each kind's default workflow, and "default" for an issue with no -// recognised label, which goes to triage to be classified. -func IntakeRoutes() map[string]string { - routes := map[string]string{string(workintake.KindDefault): "triage"} - for kind, workflow := range defaultKindWorkflows { - routes[string(kind)] = workflow - } - return routes +// IntakeRoutes is where an issue goes by its labels: the first rule, in list +// order, that matches one of them, else Default. +type IntakeRoutes struct { + Rules []IntakeRule `json:"rules" title:"Rules"` + Default string `json:"default" title:"Default workflow" doc:"Runs an issue no rule matches."` } -// workflowForLabels returns the registered workflow name for a task's -// labels, trying each recognised kind in label order so a "bug,feature" task -// still reaches feasibility when no tdd workflow is registered. -func workflowForLabels(reg Registry, labels string) (Workflow, bool) { - for _, kind := range workintake.KindsForLabels(workintake.SplitLabels(labels)) { - name, ok := defaultKindWorkflows[kind] - if !ok { - continue - } - if wf, ok := reg[name]; ok { - return wf, true +// ShippedIntakeRoutes is the routing a fresh install starts with. An +// unlabelled issue goes to triage to be classified. +func ShippedIntakeRoutes() IntakeRoutes { + return IntakeRoutes{ + Rules: []IntakeRule{{Label: "bug", Workflow: "tdd"}, {Label: "feature", Workflow: "feasibility"}}, + Default: "triage", + } +} + +// WorkflowFor returns the workflow an issue with these labels runs. +func (r IntakeRoutes) WorkflowFor(labels []string) string { + for _, rule := range r.Rules { + for _, label := range labels { + if strings.EqualFold(workintake.LabelValue(label), strings.TrimSpace(rule.Label)) { + return rule.Workflow + } } } - return Workflow{}, false + return r.Default +} + +// Validate rejects an empty label or workflow name and a missing default. +func (r IntakeRoutes) Validate() error { + for i, rule := range r.Rules { + if strings.TrimSpace(rule.Label) == "" { + return fmt.Errorf("intake routes: rule %d has no label", i+1) + } + if strings.TrimSpace(rule.Workflow) == "" { + return fmt.Errorf("intake routes: rule %d has no workflow", i+1) + } + } + if strings.TrimSpace(r.Default) == "" { + return fmt.Errorf("intake routes: a default workflow is required") + } + return nil +} + +// CheckDefined rejects a route naming a workflow that is not defined. +func (r IntakeRoutes) CheckDefined(available map[string]struct{}) error { + for _, rule := range r.Rules { + if _, ok := available[rule.Workflow]; !ok { + return fmt.Errorf("intake routes: label %q names workflow %q, which is not defined", rule.Label, rule.Workflow) + } + } + if _, ok := available[r.Default]; !ok { + return fmt.Errorf("intake routes: the default names workflow %q, which is not defined", r.Default) + } + return nil } // ResolveWorkflowID deterministically selects a definition without compiling // or executing it. It is used before worker dispatch to pin the exact YAML. -func ResolveWorkflowID(t *Task, available map[string]struct{}) (string, error) { - if t.Workflow != "" { - if _, ok := available[t.Workflow]; ok { - return t.Workflow, nil - } - return "", fmt.Errorf("workflow %q is not defined", t.Workflow) +func ResolveWorkflowID(t *Task, routes IntakeRoutes, available map[string]struct{}) (string, error) { + id := t.Workflow + if id == "" { + id = routes.WorkflowFor(workintake.SplitLabels(t.Labels)) } - for _, kind := range workintake.KindsForLabels(workintake.SplitLabels(t.Labels)) { - if id := defaultKindWorkflows[kind]; id != "" { - if _, ok := available[id]; ok { - return id, nil - } - } + if _, ok := available[id]; !ok { + return "", fmt.Errorf("workflow %q is not defined", id) } - if _, ok := available["triage"]; ok { - return "triage", nil - } - if _, ok := available["implement"]; ok { - return "implement", nil - } - return "", fmt.Errorf("no routable workflow definition") + return id, nil } diff --git a/internal/domain/workflow/routing_test.go b/internal/domain/workflow/routing_test.go new file mode 100644 index 000000000..797cd02da --- /dev/null +++ b/internal/domain/workflow/routing_test.go @@ -0,0 +1,52 @@ +package workflow + +import "testing" + +// Guards the label to workflow contract: a label matches without regard to +// case, the first rule in list order wins when an issue carries several +// labels, and an issue no rule matches runs the default. +func TestResolveWorkflowIDByLabel(t *testing.T) { + t.Parallel() + + routes := IntakeRoutes{ + Rules: []IntakeRule{ + {Label: "Security", Workflow: "audit"}, + {Label: "bug", Workflow: "tdd"}, + {Label: "feature", Workflow: "feasibility"}, + }, + Default: "triage", + } + available := map[string]struct{}{"audit": {}, "tdd": {}, "feasibility": {}, "triage": {}} + + tests := []struct { + name string + labels string + want string + wantErr bool + }{ + {name: "label matches without regard to case", labels: "BUG", want: "tdd"}, + {name: "rule label case does not matter either", labels: "security", want: "audit"}, + {name: "scoped label matches its value", labels: "kind::feature", want: "feasibility"}, + {name: "first rule in list order wins over label order", labels: "feature,bug", want: "tdd"}, + {name: "a rule earlier in the list beats a later one", labels: "bug,security", want: "audit"}, + {name: "no match runs the default", labels: "priority::high", want: "triage"}, + {name: "no labels runs the default", labels: "", want: "triage"}, + {name: "a route to an undefined workflow is an error", labels: "bug", want: "", wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + avail := available + if tt.wantErr { + avail = map[string]struct{}{"triage": {}} + } + got, err := ResolveWorkflowID(&Task{Labels: tt.labels}, routes, avail) + if (err != nil) != tt.wantErr { + t.Fatalf("err = %v, wantErr %v", err, tt.wantErr) + } + if got != tt.want { + t.Errorf("routed to %q, want %q", got, tt.want) + } + }) + } +} diff --git a/internal/domain/workflow/triage_repo_optional_test.go b/internal/domain/workflow/triage_repo_optional_test.go index 154b22999..54f5974aa 100644 --- a/internal/domain/workflow/triage_repo_optional_test.go +++ b/internal/domain/workflow/triage_repo_optional_test.go @@ -84,7 +84,7 @@ func TestRepoLessTaskRunsTriageFromAPrompt(t *testing.T) { if promptTask.HasRepository() { t.Fatal("the prompt-only task must have no repository") } - id, err := ResolveWorkflowID(promptTask, available) + id, err := ResolveWorkflowID(promptTask, ShippedIntakeRoutes(), available) if err != nil { t.Fatalf("route: %v", err) } diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 38d437aa6..130fad5a3 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -344,13 +344,8 @@ func Route(t *Task, reg Registry) Workflow { }, }}} } - if wf, ok := workflowForLabels(reg, t.Labels); ok { - return wf - } - // No explicit workflow, no label match: a labelled task already has a free, - // reliable signal and never reaches here. Everything else -- overwhelmingly - // chat-spawned tasks, which rarely carry labels -- gets classified by - // triage instead of defaulting straight to the heaviest workflow. + // No explicit workflow: classify with triage instead of defaulting + // straight to the heaviest workflow. if wf, ok := reg["triage"]; ok { return wf } diff --git a/internal/domain/workintake/envelope.go b/internal/domain/workintake/envelope.go index 2f0bc1231..56256d8c4 100644 --- a/internal/domain/workintake/envelope.go +++ b/internal/domain/workintake/envelope.go @@ -43,11 +43,6 @@ type TaskEnvelope struct { // publishing. // Empty means the default org of a single-operator install. Org org.OrgID `json:"org,omitempty"` - - // Kind is the routing category, chosen by the publisher. Empty means - // KindDefault, which is also what messages queued before this field - // existed decode to. - Kind Kind `json:"kind,omitempty"` } // envelopeWire avoids infinite recursion in the JSON methods below. @@ -113,8 +108,8 @@ func (t TaskEnvelope) org() org.OrgID { return t.Org } -// Subject returns the address this envelope routes to. -func (t TaskEnvelope) Subject() string { return SubjectForKind(t.Kind) } +// Subject returns the address this envelope is published to. +func (t TaskEnvelope) Subject() string { return SubjectTask } // SplitLabels parses the comma-separated wire form, dropping empties so // "bug,,feature" and a trailing comma do not produce blank labels. diff --git a/internal/domain/workintake/subjects.go b/internal/domain/workintake/subjects.go index d007be561..9ce420533 100644 --- a/internal/domain/workintake/subjects.go +++ b/internal/domain/workintake/subjects.go @@ -1,98 +1,21 @@ package workintake -import ( - "errors" - "fmt" - "strings" -) +import "strings" -// Task subjects. Each encodes the routing kind so a multi-daemon deployment -// can filter by the work it wants. -const ( - SubjectTaskBug = "archie.task.bug" - SubjectTaskFeature = "archie.task.feature" - SubjectTaskDefault = "archie.task.default" +// SubjectTask is where every issue-intake task is published. Routing to a +// workflow happens at pin time from the intake-routes setting, so the subject +// carries nothing about the issue. +const SubjectTask = "archie.task.issue" - // SubjectTaskWildcard matches every task subject. - SubjectTaskWildcard = "archie.task.>" -) +// SubjectTaskWildcard matches every task subject. +const SubjectTaskWildcard = "archie.task.>" // SubjectReactionWildcard matches every reaction subject. const SubjectReactionWildcard = "archie.reaction.>" -// ErrUnknownKind reports a kind with no subject. Routing it to the default -// queue instead would silently misdeliver work. -var ErrUnknownKind = errors.New("workintake: unknown task kind") - -// Kind is the routing category an issue's labels select. -type Kind string - -const ( - KindBug Kind = "bug" - KindFeature Kind = "feature" - - // KindDefault routes issues carrying no recognised label. - KindDefault Kind = "default" -) - -// labelKinds maps a forge issue label to its routing kind. This is the one -// place the label vocabulary is defined. -var labelKinds = map[string]Kind{ - "bug": KindBug, - "feature": KindFeature, -} - -// kindSubjects is the closed set of routable kinds. -var kindSubjects = map[Kind]string{ - KindBug: SubjectTaskBug, - KindFeature: SubjectTaskFeature, - KindDefault: SubjectTaskDefault, -} - -// Validate reports whether the kind is routable. The zero kind is accepted -// and means KindDefault, so a caller that does not classify still publishes. -func (k Kind) Validate() error { - if k == "" { - return nil - } - if _, ok := kindSubjects[k]; !ok { - return fmt.Errorf("%w: %q", ErrUnknownKind, string(k)) - } - return nil -} - -// SubjectForKind renders a kind as its task subject. The zero and any -// unrecognised kind render as SubjectTaskDefault so a message is never -// addressed to an unroutable subject; publishers validate first. -func SubjectForKind(kind Kind) string { - if subject, ok := kindSubjects[kind]; ok { - return subject - } - return SubjectTaskDefault -} - -// KindForLabels returns the routing kind for a set of issue labels. The first -// recognised label wins; an empty or unrecognised set is KindDefault. -func KindForLabels(labels []string) Kind { - for _, kind := range KindsForLabels(labels) { - return kind - } - return KindDefault -} - -// KindsForLabels returns every recognised kind, in label order. -func KindsForLabels(labels []string) []Kind { - var kinds []Kind - for _, label := range labels { - if kind, ok := labelKinds[labelValue(label)]; ok { - kinds = append(kinds, kind) - } - } - return kinds -} - -// labelValue returns the part of a label after its last "::". -func labelValue(label string) string { +// LabelValue returns the part of a label after its last "::", so a scoped +// label such as "kind::bug" matches the rule for "bug". +func LabelValue(label string) string { trimmed := strings.TrimSpace(label) if i := strings.LastIndex(trimmed, "::"); i >= 0 { return strings.TrimSpace(trimmed[i+len("::"):]) diff --git a/internal/gateway/integration_tools.go b/internal/gateway/integration_tools.go index 0c42ca25c..9925277aa 100644 --- a/internal/gateway/integration_tools.go +++ b/internal/gateway/integration_tools.go @@ -27,7 +27,7 @@ type IntegrationSource interface { // soul is the instructions text, task-policy and org-model-policy carry limits // and settings whose secrets are references, never values. var readableKinds = []string{ - "workflow-execution-settings", "workflow-enablement", "agent-profiles", "skills", + "workflow-execution-settings", "workflow-enablement", "intake-routes", "agent-profiles", "skills", "eda-playbooks", "credential-bindings", "identity-grants", "soul", "task-policy", "org-model-policy", } diff --git a/internal/infrastructure/controlplanestore/controlplane.go b/internal/infrastructure/controlplanestore/controlplane.go index 7c71f7697..e21ad0b78 100644 --- a/internal/infrastructure/controlplanestore/controlplane.go +++ b/internal/infrastructure/controlplanestore/controlplane.go @@ -169,7 +169,7 @@ func (s *Server) Command(ctx context.Context, request *pb.CommandRequest) (*pb.C // org that has configured nothing has nothing, rather than the system org's // configuration or a not-found the dashboard cannot tell from a failure. var orgSeededKinds = map[string]bool{ - WorkflowDefinitionsKind: true, WorkflowEnablementKind: true, + WorkflowDefinitionsKind: true, WorkflowEnablementKind: true, IntakeRoutesKind: true, ForgeConnectionsKind: true, RepositoryPoliciesKind: true, } diff --git a/internal/infrastructure/controlplanestore/definitions.go b/internal/infrastructure/controlplanestore/definitions.go index f4b9150b0..33e2f6ce3 100644 --- a/internal/infrastructure/controlplanestore/definitions.go +++ b/internal/infrastructure/controlplanestore/definitions.go @@ -8,7 +8,7 @@ import ( // Schemas are not declared here: each Definition names its document type, and // Definition.Descriptor derives the JSON Schema from it (internal/infrastructure/controlplanestore/schema.go). func builtinDefinitions(steps workflow.StepRegistry, types map[string]workflow.StepType) []Definition { - definitions := []Definition{taskPolicyDefinition(), workflowDefinition(), workflowDefinitionsDefinition(steps, types), workflowDraftsDefinition(), workflowEnablementDefinition(), curatorDefinition(), soulDefinition(), agentProfileDefinition(), playbooksDefinition(), skillsDefinition(), credentialBindingsDefinition(), identityGrantsDefinition(), orgModelPolicyDefinition(), forgeConnectionsDefinition()} + definitions := []Definition{taskPolicyDefinition(), workflowDefinition(), workflowDefinitionsDefinition(steps, types), workflowDraftsDefinition(), workflowEnablementDefinition(), intakeRoutesDefinition(), curatorDefinition(), soulDefinition(), agentProfileDefinition(), playbooksDefinition(), skillsDefinition(), credentialBindingsDefinition(), identityGrantsDefinition(), orgModelPolicyDefinition(), forgeConnectionsDefinition()} definitions = append(definitions, modelDefinitions()...) definitions = append(definitions, operationalDefinitions()...) return definitions diff --git a/internal/infrastructure/controlplanestore/intake_routes.go b/internal/infrastructure/controlplanestore/intake_routes.go new file mode 100644 index 000000000..54f8b713a --- /dev/null +++ b/internal/infrastructure/controlplanestore/intake_routes.go @@ -0,0 +1,70 @@ +package controlplanestore + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/samcharles93/archie-core/internal/config" + pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc" +) + +// IntakeRoutesKind is the resource holding which workflow an issue runs by +// its labels. +const IntakeRoutesKind = "intake-routes" + +func intakeRoutesDefinition() Definition { + return Definition{ + Kind: IntakeRoutesKind, + Title: "Issue routing", + Document: workflow.IntakeRoutes{}, + ApplyMode: "live", + Seed: func(config.Config) any { return workflow.ShippedIntakeRoutes() }, + Validate: func(input []byte) error { + return validateAs(input, func(routes workflow.IntakeRoutes) error { return routes.Validate() }) + }, + } +} + +// checkIntakeRoutes refuses routes naming a workflow the writing org has not +// defined. +func (s *Server) checkIntakeRoutes(ctx context.Context, orgID string, value []byte) error { + var routes workflow.IntakeRoutes + if err := json.Unmarshal(value, &routes); err != nil { + return err + } + stored, err := s.resource(ctx, orgID, WorkflowDefinitionsKind) + if err != nil { + return err + } + collection, err := workflow.DecodeDefinitionCollection(stored.Value, s.steps) + if err != nil { + return err + } + available := make(map[string]struct{}, len(collection.Definitions)) + for _, definition := range collection.Definitions { + available[definition.ID] = struct{}{} + } + if err := routes.CheckDefined(available); err != nil { + return fmt.Errorf("%w: %w", ErrValidation, err) + } + return nil +} + +// IntakeRoutes returns where the caller's org sends an issue by its labels. +func (c *WorkflowDefinitionsClient) IntakeRoutes(ctx context.Context) (workflow.IntakeRoutes, error) { + response, err := c.rpc.Query(ctx, &pb.QueryRequest{Kind: IntakeRoutesKind}) + if err != nil { + c.ApplyStatus.Report(ctx, IntakeRoutesKind, 0, err) + return workflow.IntakeRoutes{}, controlplanerpc.ClientError(err) + } + var routes workflow.IntakeRoutes + err = json.Unmarshal(response.Resource.ValueJson, &routes) + if err == nil { + err = routes.Validate() + } + c.ApplyStatus.Report(ctx, IntakeRoutesKind, response.Resource.Version, err) + return routes, err +} diff --git a/internal/infrastructure/controlplanestore/org_models.go b/internal/infrastructure/controlplanestore/org_models.go index 0276fdb4e..c2c627c81 100644 --- a/internal/infrastructure/controlplanestore/org_models.go +++ b/internal/infrastructure/controlplanestore/org_models.go @@ -216,6 +216,8 @@ func (s *Server) checkOrgWrite(ctx context.Context, orgID, kind string, value [] return s.checkAliasWrite(ctx, orgID, value) case WorkflowDefinitionsKind: return s.checkModelAliases(ctx, orgID, value) + case IntakeRoutesKind: + return s.checkIntakeRoutes(ctx, orgID, value) } return nil } diff --git a/internal/infrastructure/controlplanestore/workflow_definitions.go b/internal/infrastructure/controlplanestore/workflow_definitions.go index 5236ef7a5..813968fd9 100644 --- a/internal/infrastructure/controlplanestore/workflow_definitions.go +++ b/internal/infrastructure/controlplanestore/workflow_definitions.go @@ -45,7 +45,7 @@ func workflowDefinitionsDefinition(steps workflow.StepRegistry, types map[string value, err := json.Marshal(collection) return value, removed, err }, - SchemaExtensions: map[string]any{"x-step-types": stepTypeVocabulary(steps, types), "x-intake-routes": workflow.IntakeRoutes()}, + SchemaExtensions: map[string]any{"x-step-types": stepTypeVocabulary(steps, types)}, } } diff --git a/ui/src/lib/nav.ts b/ui/src/lib/nav.ts index 77d34c63b..f22e3032d 100644 --- a/ui/src/lib/nav.ts +++ b/ui/src/lib/nav.ts @@ -41,7 +41,7 @@ const orgSpecs: NavSpec[] = [ }, { label: "Automation", - paths: ["/org/schedules", "/org/scheduling-policy", "/org/channels", "/org/playbooks", "/org/alerts"], + paths: ["/org/schedules", "/org/scheduling-policy", "/org/issue-routing", "/org/channels", "/org/playbooks", "/org/alerts"], }, { label: "Usage", diff --git a/ui/src/router/index.ts b/ui/src/router/index.ts index e1acbadc0..e24acc603 100644 --- a/ui/src/router/index.ts +++ b/ui/src/router/index.ts @@ -301,6 +301,12 @@ const routes = [ component: SchedulingPolicyPage, meta: { section: "org_config", label: "Scheduling policy", navPath: "/org", description: "Work discovery and forge labels." }, }, + { + path: "issue-routing", + name: "org-issue-routing", + component: () => import("@/settings/IssueRoutingPage.vue"), + meta: { section: "org_config", label: "Issue routing", navPath: "/org", description: "Which workflow an issue runs by its labels." }, + }, { path: "alerts", name: "org-alerts", diff --git a/ui/src/settings/IssueRoutingPage.vue b/ui/src/settings/IssueRoutingPage.vue new file mode 100644 index 000000000..46bf95d91 --- /dev/null +++ b/ui/src/settings/IssueRoutingPage.vue @@ -0,0 +1,119 @@ + + + diff --git a/ui/src/stores/workflows.ts b/ui/src/stores/workflows.ts index 73fb575ff..72df4c245 100644 --- a/ui/src/stores/workflows.ts +++ b/ui/src/stores/workflows.ts @@ -76,10 +76,6 @@ export const useWorkflowsStore = defineStore("workflows", () => { const stepTypes = computed(() => parse(descriptor.value?.schema_json, (value) => value["x-step-types"], []), ); - /** Where issues are routed by label, served beside the step types. */ - const intakeRoutes = computed(() => - parse>(descriptor.value?.schema_json, (value) => value["x-intake-routes"], {}), - ); /** Unpublished edits by workflow id; only a test run runs a draft. */ const drafts = computed( @@ -102,7 +98,6 @@ export const useWorkflowsStore = defineStore("workflows", () => { collection, shipped, stepTypes, - intakeRoutes, load: settings.load, save: (next: WorkflowDefinitionCollection) => settings.replace(KIND, next), }; diff --git a/ui/src/workflows/workflow-triggers.ts b/ui/src/workflows/workflow-triggers.ts index 09f2a002a..df07e92cc 100644 --- a/ui/src/workflows/workflow-triggers.ts +++ b/ui/src/workflows/workflow-triggers.ts @@ -4,7 +4,6 @@ import { parse } from "yaml"; import { api, describeError } from "@/lib/api"; import { errors } from "@/copy/errors"; import { useSettingsStore } from "@/stores/settings"; -import { useWorkflowsStore } from "@/stores/workflows"; import type { Binding } from "@/bindings/binding-draft"; /** One way a workflow gets started. */ @@ -17,8 +16,8 @@ export interface WorkflowTrigger { } export interface TriggerSources { - /** Issue label kind to workflow, with "default" for an unlabelled issue. */ - routes: Record; + /** The issue routing setting. */ + routes: { rules: { label: string; workflow: string }[]; default: string }; bindings: Binding[]; playbooks: { id: string; yaml: string }[]; /** The workflows that start this one, as the server works them out. */ @@ -66,14 +65,10 @@ function parseQuietly(source: string): unknown { /** Everything that starts the workflow named id. */ export function triggersFor(id: string, sources: TriggerSources): WorkflowTrigger[] { const triggers: WorkflowTrigger[] = []; - for (const [kind, workflow] of Object.entries(sources.routes)) { - if (workflow !== id) continue; - triggers.push( - kind === "default" - ? { kind: "issue", label: "Unlabelled issues" } - : { kind: "issue", label: `Issues labelled ${kind}` }, - ); + for (const rule of sources.routes.rules) { + if (rule.workflow === id) triggers.push({ kind: "issue", label: `Issues labelled ${rule.label}` }); } + if (sources.routes.default === id) triggers.push({ kind: "issue", label: "Other issues" }); for (const binding of sources.bindings) { if (binding.workflow !== id) continue; if (binding.matcher?.source === SCHEDULE_SOURCE) { @@ -90,7 +85,7 @@ export function triggersFor(id: string, sources: TriggerSources): WorkflowTrigge if (!value.actions.some((action) => isMapping(action) && action.workflow === id)) continue; const trigger = isMapping(value.trigger) ? value.trigger : {}; const labels = Array.isArray(trigger.labels) ? trigger.labels.join(", ") : ""; - triggers.push({ kind: "playbook", label: playbook.id, detail: [trigger.kind, labels].filter(Boolean).join(" ยท ") }); + triggers.push({ kind: "playbook", label: playbook.id, detail: labels }); } for (const starter of sources.startedBy) { triggers.push({ kind: "workflow", label: starter.workflow, detail: STARTED_VIA[starter.via] ?? starter.via }); @@ -99,13 +94,14 @@ export function triggersFor(id: string, sources: TriggerSources): WorkflowTrigge } // The words for the server's start kinds. +const ROUTES = "intake-routes"; + const STARTED_VIA: Record = { call: "calls it", handoff: "hands off to it", approve: "runs it once approved" }; /** The triggers of the workflow named id, from the routes and definitions the * control plane serves, the stored playbooks, and the event bindings. */ export function useWorkflowTriggers(id: Ref, startedBy: Readonly>) { const store = useSettingsStore(); - const workflows = useWorkflowsStore(); const bindings = ref([]); const schedules = ref([]); // A failed read is an error, not an empty trigger list: "Manual runs only" @@ -119,6 +115,7 @@ export function useWorkflowTriggers(id: Ref, startedBy: Readonly null), + store.stateFor(ROUTES).resource ? null : store.loadResource(ROUTES).catch(() => null), ]); // A failed read keeps what was shown, beside the error. if (response) bindings.value = response.bindings ?? []; @@ -128,7 +125,7 @@ export function useWorkflowTriggers(id: Ref, startedBy: Readonly triggersFor(id.value, { - routes: workflows.intakeRoutes, + routes: (store.stateFor(ROUTES).resource?.value as TriggerSources["routes"] | undefined) ?? { rules: [], default: "" }, bindings: bindings.value, playbooks: (store.stateFor("eda-playbooks").resource?.value as { playbooks?: TriggerSources["playbooks"] } | undefined)?.playbooks ?? [], startedBy: startedBy.value,