From 16d331a16ead57aa2e15801ba7b74788b43c4185 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 12:57:54 +1000 Subject: [PATCH 1/2] refactor(daemon): remove unreachable SQLite poll/drain path DELETE: NATS startup is mandatory (config.NATSConfig documents "there is no broker-off execution mode"), so Daemon.Tasks is never nil in production and pollSQLite/drainSQLite are unreachable. Remove them and their d.Tasks==nil call sites, and correct the Tasks field comment. --- internal/daemon/daemon.go | 53 ++++----------------------------------- 1 file changed, 5 insertions(+), 48 deletions(-) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index f814669f3..bca762693 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -124,8 +124,11 @@ type Daemon struct { Trees *worktree.Manager Bus *events.Bus Log *slog.Logger - // Tasks is the optional message bus for task distribution. Nil means no - // bus is configured; the existing SQLite ClaimNext flow is used. + // Tasks is the NATS task distribution bus: the poller publishes + // discovered work over it and runViaAgent requests execution through it. + // NATS startup is mandatory (there is no broker-off execution mode), so + // production composition always sets it. Nil appears only in tests and + // is handled fail-closed at the publish/run sites. Tasks TaskBus WorktreeGrants WorktreeGrantIssuer // StateStoreGrants issues per-task State Store credentials for container @@ -381,8 +384,6 @@ func (d *Daemon) pollForIdentity(ctx context.Context, id *IdentityRunner) { labels := strings.Join(is.Labels, ",") if d.Tasks != nil { d.pollNATS(ctx, id.Forge, cfg, repo, is, labels, id.Name) - } else { - d.pollSQLite(ctx, id.Forge, cfg, repo, is, labels, id.Name) } } } @@ -398,8 +399,6 @@ func (d *Daemon) maintainAndDrain(ctx context.Context) { d.dispatchBindings(ctx) if d.Tasks != nil { d.drainNATS(ctx) - } else { - d.drainSQLite(ctx) } } @@ -474,8 +473,6 @@ func (d *Daemon) Cycle(ctx context.Context) { d.dispatchBindings(ctx) if d.Tasks != nil { d.drainNATS(ctx) - } else { - d.drainSQLite(ctx) } } @@ -495,23 +492,6 @@ func (d *Daemon) cleanupExpiredStorage(ctx context.Context) { } } -// drainSQLite claims queued tasks from SQLite and dispatches them concurrently. -func (d *Daemon) drainSQLite(ctx context.Context) { - dispatcher := newTaskDispatcher(d.Cfg.Get().Containers.MaxConcurrency, d.allowConcurrentForTask) - for ctx.Err() == nil { - task, err := d.Store.ClaimNext(ctx) - if err != nil { - d.Log.Error("claim failed", "err", err) - break - } - if task == nil { - break - } - dispatcher.Submit(ctx, task, d.process) - } - dispatcher.Wait() -} - // 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 @@ -835,34 +815,11 @@ func (d *Daemon) poll(ctx context.Context) { labels := strings.Join(is.Labels, ",") if d.Tasks != nil { d.pollNATS(ctx, d.Forge, d.Cfg.Get(), repo, is, labels, "") - } else { - d.pollSQLite(ctx, d.Forge, d.Cfg.Get(), repo, is, labels, "") } } } } -// pollSQLite enqueues discovered issues directly into SQLite (existing flow). -// fg is the forge client that discovered is -- d.Forge for the -// single-identity path, or an identity's own client from pollForIdentity. -// cfg is the dispatch config that owns this poll (root or identity-scoped). -// identity records which identity owns the resulting task; empty for -// single-identity deployments. -func (d *Daemon) pollSQLite(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, is forge.Issue, labels, identity string) { - inserted, err := d.Store.EnqueueIssue(ctx, - repo.Owner, repo.Name, is.Number, is.Title, is.Body, labels, identity) - if err != nil { - d.Log.Error("enqueue failed", "repo", repo.FullName(), "issue", is.Number, "err", err) - return - } - if inserted { - d.acknowledge(ctx, fg, cfg, repo, is) - return - } - // Existing tasks are left untouched. Retry and approval are explicit - // operator actions in messaging or the Web UI, not forge-side label edits. -} - // pollNATS publishes discovered issues to NATS (new flow). func (d *Daemon) pollNATS(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, is forge.Issue, labels, identity string) { // Read-only existence check prevents rediscovering an existing task. From 2b84ec29896ef7d24dc83cc18c787c815f250004 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 13:01:53 +1000 Subject: [PATCH 2/2] fix(daemon): forward configured MCP servers to task agents WIRE: taskrun.Request.MCPServers is documented as the channel for MCP server definitions the agent should construct, and the archie-agent worker consumes it (task_execution.go startMCPProviders). The daemon's request constructor dropped the field in a refactor, so configured MCP servers never reached a task container. Restore it from d.configFor(task).Tools.MCPServers. --- internal/daemon/daemon.go | 1 + internal/daemon/daemon_test.go | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index bca762693..9e43277cf 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1295,6 +1295,7 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf Repo: repo, Cfg: d.configFor(task).ForTask(), Providers: agentexec.ProvidersFromConfig(d.configFor(task).Providers), + MCPServers: d.configFor(task).Tools.MCPServers, WorktreeGrant: grant, KindWorkflows: d.KindWorkflows, LabelWorkflows: d.LabelWorkflows, diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index b9cc29dbc..7de7ae1bc 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -734,6 +734,53 @@ func TestRunViaAgentSendsExpectedRequest(t *testing.T) { } } +// TestRunViaAgentSendsMCPServers is the daemon half of persistence-2: the +// configured daemon-wide MCP servers must be carried in the taskrun request +// so the archie-agent worker can construct transports and register their +// tools (task_execution.go startMCPProviders). A request missing them leaves +// the whole agent-side MCP tool path dead. +func TestRunViaAgentSendsMCPServers(t *testing.T) { + d, s, busClient := daemonWithNATS(t) + cfg := d.Cfg.Get() + cfg.Tools.MCPServers = []config.MCPServer{ + {Name: "mcp-npm", Transport: "stdio", Command: "npx", Args: []string{"-y", "@modelcontextprotocol/server-filesystem"}}, + } + d.Cfg.Set(cfg) + ctx := context.Background() + + if _, err := s.EnqueueIssue(ctx, "acme", "widget", 10, "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 len(req.MCPServers) != 1 || req.MCPServers[0].Name != "mcp-npm" { + t.Fatalf("request MCPServers = %+v, want the configured mcp-npm server", req.MCPServers) + } + case <-time.After(time.Second): + t.Fatal("archied did not publish a taskrun request") + } +} + // 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