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.
127 lines
3.7 KiB
Go
127 lines
3.7 KiB
Go
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}
|
|
}
|