diff --git a/internal/app/agentworker/task_execution.go b/internal/app/agentworker/task_execution.go index 23f0d715f..373f21d65 100644 --- a/internal/app/agentworker/task_execution.go +++ b/internal/app/agentworker/task_execution.go @@ -138,6 +138,16 @@ func newTaskRunner(providers map[string]agentexec.Provider, log *slog.Logger) ag return agentexec.NewLoopRunner(agentexec.NewRuntime(providers), log) } +// routeTask applies the request-carried routing bindings and selects the +// workflow for a task. The daemon's package-level SetKindWorkflows state +// never crosses the process boundary, so runTask installs the resolved +// bindings here -- this worker process is where workflow.Route actually runs. +func routeTask(req taskrun.Request, registry workflow.Registry) workflow.Workflow { + workflow.SetKindWorkflows(req.KindWorkflows) + workflow.SetLabelWorkflows(req.LabelWorkflows) + return workflow.Route(req.Task, registry) +} + // runTask builds a workflow.Registry from the container's mounted worktree, // routes and runs the entire workflow, and reports its terminal outcome. // Store remains archied's authority; Response.Task is a logging snapshot. @@ -147,7 +157,7 @@ func runTask(ctx context.Context, req taskrun.Request, dependencies taskDependen return nil, fmt.Errorf("build registry: %w", err) } - wf := workflow.Route(req.Task, registry) + wf := routeTask(req, registry) trees := &hybridTrees{ push: dependencies.trees, diff --git a/internal/app/agentworker/task_execution_test.go b/internal/app/agentworker/task_execution_test.go index 8da17d3fc..b2c792c6b 100644 --- a/internal/app/agentworker/task_execution_test.go +++ b/internal/app/agentworker/task_execution_test.go @@ -22,6 +22,7 @@ import ( "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/domain/workintake" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/forge" "github.com/samcharles93/archie-core/internal/forgerpc" @@ -626,3 +627,54 @@ func TestExecuteTaskRequestForwardsWorkflowEventsOverNATS(t *testing.T) { t.Errorf("received %d events %+v, want at least one %q", len(received), received, events.KindOutcome) } } + +// TestRouteTaskUsesRequestKindOverride is the regression for +// workflow-engine-1: a kind -> workflow override loaded by the daemon is +// carried in the taskrun request and applied by the worker process, so +// workflow.Route honours it instead of silently falling back to the +// built-in default (bug -> tdd). +func TestRouteTaskUsesRequestKindOverride(t *testing.T) { + t.Cleanup(func() { + workflow.SetKindWorkflows(nil) + workflow.SetLabelWorkflows(nil) + }) + + registry := workflow.Registry{ + "tdd": workflow.Workflow{Name: "tdd"}, + "bootstrap": workflow.Workflow{Name: "bootstrap"}, + "implement": workflow.Workflow{Name: "implement"}, + } + req := taskrun.Request{ + Task: &workflow.Task{Labels: "bug"}, + KindWorkflows: workflow.KindWorkflows{workintake.KindBug: "bootstrap"}, + } + + wf := routeTask(req, registry) + if wf.Name != "bootstrap" { + t.Fatalf("routeTask() = %q, want %q (request kind override not applied)", wf.Name, "bootstrap") + } +} + +// TestRouteTaskUsesRequestLabelOverride is the label counterpart: an +// arbitrary-label binding carried in the request also reaches the worker's +// routing decision. +func TestRouteTaskUsesRequestLabelOverride(t *testing.T) { + t.Cleanup(func() { + workflow.SetKindWorkflows(nil) + workflow.SetLabelWorkflows(nil) + }) + + registry := workflow.Registry{ + "implement": workflow.Workflow{Name: "implement"}, + "security-review": workflow.Workflow{Name: "security-review"}, + } + req := taskrun.Request{ + Task: &workflow.Task{Labels: "security"}, + LabelWorkflows: workflow.LabelWorkflows{"security": "security-review"}, + } + + wf := routeTask(req, registry) + if wf.Name != "security-review" { + t.Fatalf("routeTask() = %q, want %q (request label override not applied)", wf.Name, "security-review") + } +} diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 8fb4143cd..567411620 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -196,6 +196,14 @@ type boot struct { providerRegistry *toolprovider.Registry d *daemon.Daemon + // kindWorkflows/labelWorkflows are the resolved kind/label -> workflow + // routing bindings loaded by loadWorkflowRouting. They are handed to the + // daemon so runViaAgent can carry them in taskrun.Request: workflow.Route + // runs in the archie-agent process, which never sees the daemon's + // package-level workflow state. + kindWorkflows workflow.KindWorkflows + labelWorkflows workflow.LabelWorkflows + // agentStatus records the most recent version/install-type an // archie-agent worker reported about itself. Allocated up front, before // any gateway goroutine (Telegram, webui) that might read it via @@ -782,6 +790,8 @@ func (b *boot) loadWorkflowRouting(cfg config.Config, log *slog.Logger) error { workflow.SetKindWorkflows(kindWorkflows) workflow.SetLabelWorkflows(labelWorkflows) + b.kindWorkflows = kindWorkflows + b.labelWorkflows = labelWorkflows return nil } @@ -1306,6 +1316,8 @@ func (b *boot) buildDaemon() { Identities: b.identityRunners, TaskLogs: b.taskLogs, AgentStatus: b.agentStatus, + KindWorkflows: b.kindWorkflows, + LabelWorkflows: b.labelWorkflows, } // Curator observability (archie-core-1786637489932-6): GET // /api/curators reads registered names, health and recent activity diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 650235543..588c95beb 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -180,6 +180,14 @@ type Daemon struct { // means guardrails are disabled (backward compatible). Guardrails *tools.GuardrailEngine + // KindWorkflows and LabelWorkflows are the resolved kind/label -> + // workflow-name routing bindings loaded at startup (WorkflowRoutingFile, + // WorkflowLabelsFile, PlaybookDirs). They travel in taskrun.Request so + // the archie-agent process that calls workflow.Route applies the same + // bindings the daemon loaded; nil means built-in defaults. + KindWorkflows workflow.KindWorkflows + LabelWorkflows workflow.LabelWorkflows + // ToolRegistry is the central tool registry, wired by the composition // root. MCP-discovered tools and built-in tools are registered here // and passed as CaptureTools in agent requests. Nil means no dynamic @@ -1324,11 +1332,13 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf } defer revoke() req := taskrun.Request{ - Task: task, - Repo: repo, - Cfg: d.configFor(task).ForTask(), - Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers), - WorktreeGrant: grant, + Task: task, + Repo: repo, + Cfg: d.configFor(task).ForTask(), + Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers), + WorktreeGrant: grant, + KindWorkflows: d.KindWorkflows, + LabelWorkflows: d.LabelWorkflows, } data, err := json.Marshal(req) if err != nil { diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 1d943918a..56bc8b6a3 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -734,6 +734,53 @@ func TestRunViaAgentSendsExpectedRequest(t *testing.T) { } } +// TestRunViaAgentSendsRoutingBindings is the daemon half of +// workflow-engine-1: the kind/label -> workflow bindings the daemon loaded +// must be carried in the taskrun request, because workflow.Route runs in the +// separate archie-agent process. A request missing them would silently route +// with the built-in defaults. +func TestRunViaAgentSendsRoutingBindings(t *testing.T) { + d, s, busClient := daemonWithNATS(t) + d.KindWorkflows = workflow.KindWorkflows{workintake.KindBug: "custom-bug"} + d.LabelWorkflows = workflow.LabelWorkflows{"security": "security-review"} + ctx := context.Background() + + if _, err := s.EnqueueIssue(ctx, "acme", "widget", 9, "t", "b", "", ""); err != nil { + t.Fatal(err) + } + task, err := s.ClaimNext(ctx) + if err != nil || task == nil { + t.Fatalf("claim: (%v, %v)", task, err) + } + + received := make(chan taskrun.Request, 1) + sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) { + var req taskrun.Request + _ = json.Unmarshal(msg.Data, &req) + received <- req + data, _ := json.Marshal(taskrun.Response{Status: workflow.StatusPROpen}) + _ = msg.Respond(data) + }) + if err != nil { + t.Fatalf("subscribe: %v", err) + } + t.Cleanup(func() { _ = sub.Unsubscribe() }) + + d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget", Base: "main"}) + + select { + case req := <-received: + if got := req.KindWorkflows[workintake.KindBug]; got != "custom-bug" { + t.Errorf("request KindWorkflows[bug] = %q, want %q", got, "custom-bug") + } + if got := req.LabelWorkflows["security"]; got != "security-review" { + t.Errorf("request LabelWorkflows[security] = %q, want %q", got, "security-review") + } + case <-time.After(time.Second): + t.Fatal("archied did not publish a taskrun request") + } +} + // TestRunViaAgentObservesReportedAgentVersion is the regression proof for // daemon.AgentStatus actually being fed: every archie-agent process is // task-scoped and ephemeral, so a completed taskrun.Response is the only diff --git a/internal/taskrun/taskrun.go b/internal/taskrun/taskrun.go index 86a9812fd..6d148ac9a 100644 --- a/internal/taskrun/taskrun.go +++ b/internal/taskrun/taskrun.go @@ -29,6 +29,14 @@ type Request struct { // transports, discover tools, and register them locally. Absent/empty // means no MCP servers (backward compatible). MCPServers []config.MCPServer `json:"mcp_servers,omitempty"` + // KindWorkflows and LabelWorkflows carry the resolved kind/label -> + // workflow-name bindings the daemon loaded from WorkflowRoutingFile, + // WorkflowLabelsFile and PlaybookDirs. workflow.Route runs in the + // archie-agent process, not the daemon, so these resolved values must + // cross the process boundary here; nil means use the built-in defaults + // (backward compatible). + KindWorkflows workflow.KindWorkflows `json:"kind_workflows,omitempty"` + LabelWorkflows workflow.LabelWorkflows `json:"label_workflows,omitempty"` // WorktreeGrant is an opaque, per-dispatch capability authorizing the // daemon to publish this task's already-prepared branch. Repository // coordinates never cross back from the sandbox as authority.