feat: implement workflow — planner/builder agent stages over agentloop

AgentStage is the reusable bridge from workflow stages to agentloop
runs: role->model selection from config, budgets, repo gate conversion,
task-row NotesStore (survives worktree cleanup, never pollutes the PR),
and park-on-anything-but-passed. Every future LLM stage (tdd,
feasibility) is an AgentStage with a different mission/gate/handler.

The implement workflow: prepare -> baseline gate (park on red base,
prevents park-storms) -> plan (read-only agentloop, plan posted to the
issue as a veto window) -> build (full toolset behind the repo's
configured gate) -> commit/push -> diff cap -> PR with the builder's
finish summary as the body.

Proven live on samcharles93/todo#3: planner 3 steps, builder 4 steps
(~31k tokens total), producing gate-verified table-driven TodoStore
tests as PR #4.
This commit is contained in:
sam committed 2026-07-21 04:03:06 +10:00
1 parent 61612568d6
commit 708e55a95b
6 files changed
+257 -14

No files matched your search

+5
View File
@@ -14,9 +14,14 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.46.0 // indirect
modernc.org/libc v1.74.1 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
)
require github.com/samcharles93/ai-sdk v0.1.16
replace github.com/samcharles93/ai-sdk => /work/projects/ai-sdk
+3
View File
@@ -19,12 +19,15 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
+10 -7
View File
@@ -11,6 +11,7 @@ import (
"time"
"github.com/google/go-github/v78/github"
"github.com/samcharles93/ai-sdk/runtime"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/forge"
@@ -24,6 +25,7 @@ type Daemon struct {
Store *store.Store
Forge *forge.Client
Trees *worktree.Manager
Runtime *runtime.Runtime
Workflows workflow.Registry
Log *slog.Logger
}
@@ -140,13 +142,14 @@ func (d *Daemon) process(ctx context.Context, task *store.Task) {
wf := workflow.Route(task, d.Workflows)
d.Log.Info("processing task", "repo", repo.FullName(), "issue", task.IssueNumber, "workflow", wf.Name, "attempt", task.Attempt)
workflow.Run(ctx, wf, &workflow.TaskContext{
Task: task,
Repo: repo,
Cfg: d.Cfg,
Forge: d.Forge,
Store: d.Store,
Trees: d.Trees,
Log: d.Log,
Task: task,
Repo: repo,
Cfg: d.Cfg,
Forge: d.Forge,
Store: d.Store,
Trees: d.Trees,
Runtime: d.Runtime,
Log: d.Log,
})
}
+126
View File
@@ -0,0 +1,126 @@
package workflow
import (
"context"
"fmt"
"github.com/samcharles93/ai-sdk/agentloop"
"github.com/samcharles93/archie-core/internal/config"
)
// AgentStage is the reusable bridge from a workflow stage to an
// agentloop run. Every LLM-driven stage in any workflow (implement's
// planner/builder, tdd's test-writer/fixer, feasibility's analyst)
// is an AgentStage with a different mission, gate, and result handler —
// never a new engine.
type AgentStage struct {
Name string
// Role selects the model via cfg.Models[Role]; falls back to
// cfg.Models["builder"].
Role string
ReadOnly bool
// Mission produces the task statement from the current context.
Mission func(*TaskContext) string
// Gate returns the stage's quality gate; nil means ungated (e.g.
// read-only analysis stages).
Gate func(*TaskContext) agentloop.GateConfig
// ExtraRules is appended to the system prompt.
ExtraRules string
// MaxSteps overrides the configured step budget when > 0 (planner
// stages are cheaper than builder stages).
MaxSteps int
// OnResult consumes a successful (passed) result. Parked and idle
// results park the workflow before OnResult is called.
OnResult func(*TaskContext, agentloop.Result) error
}
// Stage adapts the AgentStage to the engine.
func (a AgentStage) Stage() Stage {
return Stage{Name: a.Name, Run: func(ctx context.Context, tc *TaskContext) error {
if tc.Runtime == nil {
return fmt.Errorf("no LLM runtime configured (missing [providers] in config?)")
}
modelRef := tc.Cfg.Models[a.Role]
if modelRef == "" {
modelRef = tc.Cfg.Models["builder"]
}
if modelRef == "" {
return fmt.Errorf("no model configured for role %q (set [models] in config)", a.Role)
}
budget := agentloop.Budget{
MaxSteps: tc.Cfg.Budgets.MaxSteps,
MaxTokens: tc.Cfg.Budgets.MaxTokens,
WallClock: tc.Cfg.Budgets.WallClock.Std(),
}
if a.MaxSteps > 0 {
budget.MaxSteps = a.MaxSteps
}
var gate agentloop.GateConfig
if a.Gate != nil {
gate = a.Gate(tc)
}
res, err := agentloop.Run(ctx, agentloop.Config{
Runtime: tc.Runtime,
ModelRef: modelRef,
WorkDir: tc.Dir,
Mission: a.Mission(tc),
ExtraRules: a.ExtraRules,
Notes: taskNotes{tc: tc},
Gate: gate,
Preflight: []agentloop.GateCommand{
{Name: "go-version", Argv: []string{"go", "version"}},
},
Budget: budget,
ReadOnly: a.ReadOnly,
Logger: tc.Log.With("stage", a.Name, "model", modelRef),
})
if err != nil {
return fmt.Errorf("agent run: %w", err)
}
tc.Task.TokensUsed += res.TokensUsed
tc.Task.Iterations += res.Iterations
if res.Status != agentloop.StatusPassed {
detail := res.Detail
if detail == "" {
detail = res.Summary
}
return fmt.Errorf("agent %s (%s): %s", res.Status, res.StopReason, clip(detail, 2000))
}
if a.OnResult != nil {
return a.OnResult(tc, res)
}
return nil
}}
}
// taskNotes stores agent notes on the task row — orchestrator-side, so
// they survive worktree cleanup and never pollute the PR diff.
type taskNotes struct{ tc *TaskContext }
func (n taskNotes) Load(context.Context) (string, error) {
return n.tc.Task.Notes, nil
}
func (n taskNotes) Append(ctx context.Context, entry string) error {
n.tc.Task.Notes += "- " + entry + "\n"
return n.tc.Store.Update(ctx, n.tc.Task)
}
// GateFromRepo converts the repo's configured command lists into an
// agentloop gate.
func GateFromRepo(repo config.Repo, budgets config.Budgets) agentloop.GateConfig {
cmds := make([]agentloop.GateCommand, 0, len(repo.Gate))
for _, argv := range repo.Gate {
if len(argv) == 0 {
continue
}
cmds = append(cmds, agentloop.GateCommand{Name: argv[0], Argv: argv})
}
return agentloop.GateConfig{Commands: cmds, MaxConsecutiveFailures: budgets.GateMaxFailures}
}
+101
View File
@@ -0,0 +1,101 @@
package workflow
import (
"context"
"fmt"
"os/exec"
"strings"
"github.com/samcharles93/ai-sdk/agentloop"
)
// StageBaselineGate verifies the repo's gate is green at the base commit
// before any planning starts — a red baseline would park-storm the
// builder with failures it didn't cause.
func StageBaselineGate() Stage {
return Stage{Name: "baseline", Run: func(ctx context.Context, tc *TaskContext) error {
for _, argv := range tc.Repo.Gate {
if len(argv) == 0 {
continue
}
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
cmd.Dir = tc.Dir
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("baseline red — %s fails at %s before archie touched anything:\n%s",
strings.Join(argv, " "), tc.Repo.BaseBranch(), clip(string(out), 2000))
}
}
return nil
}}
}
// Implement is the default workflow: plan read-only, post the plan to
// the issue for visibility, build behind the repo's gate, open the PR
// with the builder's own summary.
func Implement() Workflow {
return Workflow{
Name: "implement",
Stages: []Stage{
StagePrepareWorktree(),
StageBaselineGate(),
AgentStage{
Name: "plan",
Role: "planner",
ReadOnly: true,
MaxSteps: 15,
Mission: func(tc *TaskContext) string {
return fmt.Sprintf(
"Produce a concrete implementation plan for this GitHub issue on the repository %s.\n\n"+
"<issue number=%d>\n# %s\n\n%s\n</issue>\n\n"+
"Explore the codebase with your read-only tools, then call finish with status "+
"\"passed\" and the plan as the summary: files to touch, the approach, acceptance "+
"criteria, and what tests should prove it. Keep the plan tightly scoped to the issue — "+
"call finish with status \"blocked\" if the issue is too vague or too large for one PR.",
tc.Repo.FullName(), tc.Task.IssueNumber, tc.Task.Title, tc.Task.Body,
)
},
OnResult: func(tc *TaskContext, res agentloop.Result) error {
tc.Task.Plan = res.Summary
body := fmt.Sprintf("**archie's plan** (review now if you want to veto — building starts immediately):\n\n%s", res.Summary)
if err := tc.Forge.Comment(context.Background(), tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, body); err != nil {
tc.Log.Warn("failed to post plan comment", "err", err)
}
return nil
},
}.Stage(),
AgentStage{
Name: "build",
Role: "builder",
Gate: func(tc *TaskContext) agentloop.GateConfig {
return GateFromRepo(tc.Repo, tc.Cfg.Budgets)
},
Mission: func(tc *TaskContext) string {
return fmt.Sprintf(
"Implement this GitHub issue on the repository %s, following the plan below.\n\n"+
"<issue number=%d>\n# %s\n\n%s\n</issue>\n\n<plan>\n%s\n</plan>\n\n"+
"Make the smallest change that satisfies the issue and the plan's acceptance criteria. "+
"Do not run git — the orchestrator commits and pushes for you. When done, call finish "+
"with status \"passed\" and a summary written for the human who will review the pull "+
"request: what changed, why, and how it was verified.",
tc.Repo.FullName(), tc.Task.IssueNumber, tc.Task.Title, tc.Task.Body, tc.Task.Plan,
)
},
OnResult: func(tc *TaskContext, res agentloop.Result) error {
tc.BuildSummary = res.Summary
return nil
},
}.Stage(),
StageCommitPush(func(tc *TaskContext) string {
return fmt.Sprintf("%s (archie)\n\nImplements #%d", tc.Task.Title, tc.Task.IssueNumber)
}),
StageDiffCap(),
StageOpenPR(func(tc *TaskContext) string {
return fmt.Sprintf("%s\n\n---\n*workflow: implement · %d iterations · %d tokens*\n\nCloses #%d",
tc.BuildSummary, tc.Task.Iterations, tc.Task.TokensUsed, tc.Task.IssueNumber)
}),
},
}
}
+12 -7
View File
@@ -11,6 +11,8 @@ import (
"log/slog"
"strings"
"github.com/samcharles93/ai-sdk/runtime"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/forge"
"github.com/samcharles93/archie-core/internal/store"
@@ -21,17 +23,20 @@ import (
// forward by mutating Task (persisted after every stage) and the
// scratch fields below.
type TaskContext struct {
Task *store.Task
Repo config.Repo
Cfg config.Config
Forge *forge.Client
Store *store.Store
Trees *worktree.Manager
Log *slog.Logger
Task *store.Task
Repo config.Repo
Cfg config.Config
Forge *forge.Client
Store *store.Store
Trees *worktree.Manager
Runtime *runtime.Runtime
Log *slog.Logger
// Dir/Branch are set by the prepare step.
Dir string
Branch string
// BuildSummary is the builder agent's finish summary — the PR body.
BuildSummary string
// Outcome describes where the task ended up; the engine applies it.
Outcome Outcome
}