Files
archie/cmd/archie-agent/taskrun.go
T
sam 1eee983681 feat: archie-agent runs a full task's workflow itself (archie-core-5zh.7)
Add internal/taskrun (Request/Response wire types) and
cmd/archie-agent/taskrun.go: runTask builds a workflow.Registry from the
container's own bind-mounted worktree, routes and runs the task's entire
workflow via workflow.Route/workflow.Run, using storerpc.Client/
forgerpc.Client for Store/Forge and a new hybridTrees that splits
Prepare/Push (proxied over worktreerpc — network ops needing the daemon's
credential) from CommitAll/Diff/ChangedFiles/ChangedLines (local git
against the mount).

Add config.TaskConfig.ToConfig() to rebuild a usable Config from the
curated payload, and storage.WorktreeMountDir (/data/worktree) — critical
fix: hybridTrees.Prepare discards the RPC response's host-side directory
and returns the container's fixed local mount path instead, since archied
and archie-agent see the same files at different paths.

Wired a queue-grouped core-NATS subscription on archie.taskrun.> in
cmd/archie-agent/main.go alongside the existing per-stage consumer, so
this is reachable in the running binary. End-to-end test drives the real
"bootstrap" workflow (no LLM) through real storerpc/forgerpc/worktreerpc
servers and a real local git remote — registry build, route, prepare,
commit, push, PR creation, and confirms the authoritative task state
lands in archied's store via RPC, not just in-memory.

archie-core-5zh.8 (daemon.go handoff) is unblocked.
2026-07-24 09:08:19 +10:00

160 lines
5.3 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"time"
natsio "github.com/nats-io/nats.go"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/forgerpc"
"github.com/samcharles93/archie-core/internal/storage"
"github.com/samcharles93/archie-core/internal/storerpc"
"github.com/samcharles93/archie-core/internal/taskrun"
"github.com/samcharles93/archie-core/internal/workflow"
"github.com/samcharles93/archie-core/internal/workflow/skillbuild"
"github.com/samcharles93/archie-core/internal/workflow/wfeval"
"github.com/samcharles93/archie-core/internal/worktree"
"github.com/samcharles93/archie-core/internal/worktreerpc"
)
const rpcTimeout = 60 * time.Second
// hybridTrees implements workflow.Trees by splitting operations: Prepare
// and Push (network ops needing the daemon's forge credential) proxy over
// worktreerpc; CommitAll/Diff/ChangedFiles/ChangedLines are local git
// operations run directly against the container's bind-mounted worktree.
//
// Prepare deliberately discards the RPC response's directory (the
// daemon's host-side path) and returns localDir instead — archied and
// archie-agent see the same files at different paths.
type hybridTrees struct {
push *worktreerpc.Client
local *worktree.Manager
owner, repo string
issue int
localDir string
}
func (h *hybridTrees) Prepare(ctx context.Context, owner, repo, base string, issue int, title, body, labels string) (dir, branch string, err error) {
_, branch, err = h.push.Prepare(ctx, owner, repo, base, issue, title, body, labels)
if err != nil {
return "", "", err
}
return h.localDir, branch, nil
}
func (h *hybridTrees) CommitAll(ctx context.Context, dir, message string) (bool, error) {
return h.local.CommitAll(ctx, dir, message)
}
func (h *hybridTrees) Push(ctx context.Context, dir, branch string) error {
return h.push.Push(ctx, h.owner, h.repo, h.issue, branch)
}
func (h *hybridTrees) Diff(ctx context.Context, dir, base string) (string, error) {
return h.local.Diff(ctx, dir, base)
}
func (h *hybridTrees) ChangedFiles(ctx context.Context, dir, base string) ([]string, error) {
return h.local.ChangedFiles(ctx, dir, base)
}
func (h *hybridTrees) ChangedLines(ctx context.Context, dir, base string) (int, error) {
return h.local.ChangedLines(ctx, dir, base)
}
var _ workflow.Trees = (*hybridTrees)(nil)
// runTask builds a workflow.Registry from the container's own mounted
// worktree, routes and runs req.Task's entire workflow, and reports the
// terminal outcome. Store/Forge/worktree-push calls proxy back to archied
// over nc; Store is archied's sole authority for task state — the
// returned Response.Task is a best-effort snapshot for logging only.
func runTask(ctx context.Context, req taskrun.Request, nc *natsio.Conn, newRunner agentexec.RunnerFactory, workDir string, log *slog.Logger) (*taskrun.Response, error) {
registry, err := skillbuild.BuildRegistry(workDir)
if err != nil {
return nil, fmt.Errorf("build registry: %w", err)
}
wf := workflow.Route(req.Task, registry)
trees := &hybridTrees{
push: &worktreerpc.Client{Conn: nc, Timeout: rpcTimeout},
local: &worktree.Manager{WorkDir: workDir},
owner: req.Task.Owner,
repo: req.Task.Repo,
issue: req.Task.IssueNumber,
localDir: workDir,
}
agent := newRunner(req.Providers, log)
if agent == nil {
return nil, fmt.Errorf("no agent runner configured for task %d", req.Task.ID)
}
tc := &workflow.TaskContext{
Task: req.Task,
Repo: req.Repo,
Cfg: req.Cfg.ToConfig(),
Forge: &forgerpc.Client{Conn: nc, Timeout: rpcTimeout},
Store: &storerpc.Client{Conn: nc, Timeout: rpcTimeout},
Trees: trees,
Agent: agent,
Log: log,
CustomStages: wfeval.Discover,
}
workflow.Run(ctx, wf, tc)
return &taskrun.Response{
Task: tc.Task,
Status: tc.Outcome.Status,
}, nil
}
// handleTaskRun decodes a taskrun.Request from msg, runs it via runTask
// against the container's fixed worktree mount, and replies with the
// outcome. Errors are reported in the reply, not returned — there's no
// caller to return them to; this is a NATS subscription callback.
func handleTaskRun(ctx context.Context, msg *natsio.Msg, nc *natsio.Conn, log *slog.Logger) {
var req taskrun.Request
if err := json.Unmarshal(msg.Data, &req); err != nil {
log.Error("taskrun decode failed", "err", err)
respondTaskRun(msg, nil, fmt.Errorf("decode taskrun request: %w", err), log)
return
}
log.Info("running task", "task", req.Task.ID, "repo", req.Repo.FullName(), "issue", req.Task.IssueNumber)
resp, err := runTask(ctx, req, nc, agentexec.DefaultRunnerFactory, storage.WorktreeMountDir, log)
if err != nil {
log.Error("task run failed", "task", req.Task.ID, "err", err)
respondTaskRun(msg, nil, err, log)
return
}
log.Info("task run complete", "task", req.Task.ID, "status", resp.Status)
respondTaskRun(msg, resp, nil, log)
}
func respondTaskRun(msg *natsio.Msg, resp *taskrun.Response, runErr error, log *slog.Logger) {
if resp == nil {
resp = &taskrun.Response{}
}
if runErr != nil {
resp.Error = runErr.Error()
}
data, err := json.Marshal(resp)
if err != nil {
log.Error("taskrun encode response failed", "err", err)
return
}
if err := msg.Respond(data); err != nil {
log.Error("taskrun respond failed", "err", err)
}
}