Files
archie/internal/domain/workflow/triage.go
T
sam 4e3e48ca76 feat(workflow): give triage criteria for choosing between workflows
The classify stage picked between implement, tdd and feasibility with no
stated criteria: the mission spent its guidance on needs_code_change and the
schema said only 'Which workflow fits best', with implement as the free
default for an omitted or unrecognised answer. Issue 813 was classified
implement on that basis.

Both surfaces now state what each workflow is for and say not to pick
implement merely because nothing else obviously fits. The mission moves to a
named triageMission so a test can assert the criteria reach the model.

Closes archie-core-s5ys
2026-09-19 00:17:44 +10:00

137 lines
6.2 KiB
Go

package workflow
import (
"context"
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/agentexec"
)
// triageWorkflowNames is the set of workflows triage may hand a task to.
// Unrecognized or missing classifier output falls back to "implement" --
// see triageDecideCaptureTools' schema description and Triage's OnResult.
var triageWorkflowNames = map[string]bool{
"implement": true,
"tdd": true,
"feasibility": true,
}
// Triage is the cheap-classification workflow: one read-only agent turn
// decides whether a task needs a code change at all and, if so, which
// workflow suits it best. See docs/prds/dynamic-workflow-triage.md.
//
// It exists because Route (workflow.go) previously had no content-aware
// fallback: every task with no explicit workflow and no label match ran
// the full "implement" pipeline regardless of what it actually asked for.
// A chat-spawned administrative request ("this is just a test, close it")
// cost 669,421 tokens finding that out the expensive way (baseline, plan,
// and build all ran before concluding nothing needed to change).
//
// StagePrepareWorktree still runs first: AgentStage mounts tc.Dir into the
// agent container (agent.go's handleResult passes it straight through),
// and no cheaper directory-less classification primitive exists in this
// codebase. The savings are in what triage skips, not in avoiding a
// worktree checkout.
func Triage() Workflow {
return Workflow{
Name: "triage",
Stages: []Stage{
StagePrepareWorktree(),
AgentStage{
Name: "classify",
Role: "planner",
ReadOnly: true,
CaptureTools: triageDecideCaptureTools,
Mission: triageMission,
OnResult: func(tc *TaskContext, res agentexec.Result) error {
calls := res.Captures["decide"]
if len(calls) != 1 {
return fmt.Errorf("triage classify stage called the decide tool %d times (want exactly once)", len(calls))
}
var captured struct {
NeedsCodeChange *bool `json:"needs_code_change"`
Workflow string `json:"workflow"`
Reasons string `json:"reasons"`
}
if err := json.Unmarshal(calls[0], &captured); err != nil {
return fmt.Errorf("decode triage decision: %w", err)
}
if captured.NeedsCodeChange == nil {
return fmt.Errorf("triage classify stage decision has no boolean needs_code_change value")
}
if !*captured.NeedsCodeChange {
// OnResult has no ctx of its own (matches
// feasibility.go's identical constraint); Forge
// calls build their own timeout from what they're
// given, same as feasibility.go's CloseIssue call.
if tc.Task.IsForgeBacked() {
if err := tc.Forge.CloseIssue(context.Background(), tc.Task.Owner, tc.Task.Repo, tc.Task.IssueNumber, ""); err != nil {
return err
}
}
tc.Outcome = Outcome{Status: StatusMerged, Detail: "triaged: no code change needed -- " + captured.Reasons}
return nil
}
target := captured.Workflow
if !triageWorkflowNames[target] {
target = "implement"
}
tc.Task.Workflow = target
tc.Outcome = Outcome{
Status: StatusQueued,
Detail: fmt.Sprintf("triaged to %s: %s", target, captured.Reasons),
}
return nil
},
}.Stage(),
},
}
}
// triageDecideCaptureTools gives the classify agent a structured verdict
// tool, mirroring feasibility.go's decideCaptureTools.
func triageDecideCaptureTools(*TaskContext) []agentexec.CaptureTool {
params := json.RawMessage(`{
"type": "object",
"properties": {
"needs_code_change": {"type": "boolean", "description": "false: nothing to build -- close/no-op. true: route to a workflow that builds something."},
"workflow": {"type": "string", "enum": ["implement", "tdd", "feasibility"], "description": "Which workflow fits best, when needs_code_change is true. feasibility: a new capability whose design is not settled, or a request too broad to scope -- it produces a design document for a human to approve before any code is written. tdd: a defect with an observable wrong behaviour that a test can reproduce first. implement: the change is well understood and its shape is already clear. Choose feasibility when no approved design exists and the request is not a defect; do not choose implement merely because nothing else obviously fits. Defaults to implement if omitted or unrecognized."},
"reasons": {"type": "string", "description": "The rationale, written for the human who filed the request."}
},
"required": ["needs_code_change", "reasons"]
}`)
return []agentexec.CaptureTool{{
Name: "decide", Description: "Record the triage verdict. Call exactly once, before finish.",
Parameters: params, RequiredFields: []string{"needs_code_change", "reasons"},
NonEmptyStrings: []string{"reasons"}, BooleanFields: []string{"needs_code_change"}, MaxCalls: 1,
}}
}
// triageMission is the classify stage's prompt. It is a named function so a
// test can assert the selection criteria are actually stated to the model,
// which is the whole of this stage's behaviour.
func triageMission(tc *TaskContext) string {
return fmt.Sprintf(
"Triage this %s on the repository %s: decide whether it needs a code "+
"change at all, and if so which workflow suits it best.\n\n"+
"%s\n\n"+
"Read only as much as you need to judge this -- a title/body that is "+
"purely conversational, administrative, or already resolved needs no code "+
"change.\n\n"+
"If it does need a change, the workflow is a real decision, not a "+
"formality. Check whether the repository already has a settled design "+
"for what is being asked (a design document, an approved plan, an "+
"existing implementation to extend). If it does not, and the request "+
"is not a defect, choose feasibility: it writes a design document for "+
"a human to approve, which is cheaper than an implementation built on "+
"a guess. Choose tdd when there is an observable wrong behaviour a "+
"test can reproduce first. Choose implement only when the change is "+
"well understood and its shape is already clear.\n\n"+
"Then call the decide tool EXACTLY ONCE and afterwards call finish "+
"with status \"passed\".",
taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task),
)
}