feat(workflow): dynamic triage instead of defaulting every task to implement

Route() had no content-aware fallback: any task with no explicit
workflow and no label match ran the full "implement" pipeline
regardless of content. Confirmed in production (task 6): a
chat-spawned task whose entire content was "this is just a test,
close it when you receive it" burned 669,421 tokens finding that out
the expensive way -- baseline, plan, and build all ran before the
plan stage's own conclusion (nothing needed to change) arrived too
late to save anything.

Adds a "triage" workflow: one cheap, read-only classification turn
(mirrors feasibility.go's decide-tool pattern) that either closes a
task needing no code change immediately, or sets Task.Workflow and
requeues under whichever workflow fits. Route()'s fallback now
prefers a registered "triage" over "implement", unchanged when
triage isn't registered. Labeled tasks are untouched -- a label is
already a free, reliable signal, so triage never fires there.

See docs/prds/dynamic-workflow-triage.md for the full design.
This commit is contained in:
sam committed 2026-09-02 21:40:06 +10:00
1 parent 322e081365
commit 5bec08b6ce
8 files changed
+477 -4

No files matched your search

+1
View File
@@ -91,6 +91,7 @@
{"_type":"issue","id":"archie-core-g7tm","title":"telegram: finalize the reply through a live context, matching the error branch","description":"The success path calls live.finalize(turnCtx, reply) with the raw cancellable turn context (telegram.go:582), while the error branch two lines above deliberately uses context.WithoutCancel(turnCtx). If /stop lands during finalize, the rich edit, the plain-text retry, and the split-send fallback all abort with context.Canceled (go-telegram/bot builds requests with NewRequestWithContext), so the authoritative reply is never delivered and the live message keeps the ▌ cursor forever. The reply is persisted to the ledger, so the loss is delivery-level — but the user sees a frozen half-answer.\n\nFailure: a long tool-using turn is /stop-ped as it completes; turnCtx is cancelled mid-finalize; every send fails on the dead context; the chat shows the last live frame with a blinking cursor and no final answer.\n\nFound by code-review of PR #476 (feat/467, merged 98e8dda).","design":"live.finalize(context.WithoutCancel(turnCtx), reply) — the one-liner the error branch already uses. Consider making finalize take a live context internally rather than trusting each caller to remember.","acceptance_criteria":"A /stop landing mid-finalize still delivers the authoritative reply and drops the cursor; a test covers the cancellable-context finalize path.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-14T14:43:08Z","created_by":"Sam Catlow","updated_at":"2026-08-14T15:07:36Z","started_at":"2026-08-14T14:53:28Z","closed_at":"2026-08-14T15:03:28Z","close_reason":"Fixed in internal/channels/telegram/live.go and telegram.go: finalize/abandon strip context cancellation before sending (mirroring each other), ToolCall escapes Markdown metacharacters via bot.EscapeMarkdown, splitLongMessage splits on rune boundaries. All new/changed tests verified red-then-green; task check clean; adversarial review pass found no blocking issues.","external_ref":"https://github.com/samcharles93/archie-core/issues/484","labels":["code-review","streaming","telegram"],"dependency_count":0,"dependent_count":0,"comment_count":0}
{"_type":"issue","id":"archie-core-tb3a","title":"Fix streaming review blockers for inline tool calls","description":"Resolve the four evidence-gated findings in merged inline tool-call streaming: preserve the authoritative Telegram reply when final edits fail; keep Telegram network I/O off the model FullStream consumer and cancellable; correlate tool inputs with results and render a bounded redacted parameter summary; snapshot show_tool_calls per live reply to avoid reload races. Add regression tests for every path and run task check plus lint/race validation.","acceptance_criteria":"Regression tests fail on the merged implementation and pass after the fix. Final edit failure falls back to normal split delivery. Delta and ToolCall callbacks do not wait on Telegram HTTP and stop cancels in-flight rendering. Completed tool events include bounded redacted parameters correlated by tool-call ID. Reload cannot race an in-flight reply's show_tool_calls setting. task check passes, golangci-lint reports no new findings, and focused race tests pass.","notes":"Implemented in worktree /tmp/archie-core-tb3a on branch fix/archie-core-tb3a. Added cancellable async Telegram rendering, final edit failure fallback through normal split delivery, call-ID-correlated bounded conservative parameter summaries, atomic per-reply show_tool_calls snapshots, SSE/UI parameter rendering, rebuilt ui/dist, and regression tests. Validation: GOFLAGS=-buildvcs=false task check passed; golangci-lint --new-from-rev=HEAD ./... reported 0 issues; focused race suite passed; fresh adversarial reviewer reported PASS with zero findings. Plain task check is blocked in linked worktrees by Go VCS stamping exit 128, hence buildvcs override.\n2026-08-24 reconciliation: the close reason's 'worktree left uncommitted' is stale. The work did land on main via ba33527 (Merge pull request #477 from samcharles93/fix/archie-core-tb3a); verified on main: gateway/stream.go sensitiveParameterKey+redactToolParameters, telegram/live.go per-reply showToolCalls snapshot, webui/api_chat.go gating. The /tmp/archie-core-tb3a worktree and its branch are gone, as expected.\n","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T18:05:35Z","created_by":"Sam Catlow","updated_at":"2026-08-24T07:29:09Z","started_at":"2026-08-13T18:05:39Z","closed_at":"2026-08-13T18:35:26Z","close_reason":"Completed and independently reviewed; worktree left uncommitted per conservative repository policy.","external_ref":"https://github.com/samcharles93/archie-core/issues/485","dependency_count":0,"dependent_count":0,"comment_count":0}
{"_type":"issue","id":"archie-core-dzx","title":"Remove duplicate archie-agent safe.directory setup","description":"## Problem\\ncmd/archie-agent/main.go configures git safe.directory inline and then calls markWorktreeSafe, so startup writes the same global git configuration twice.\\n\\n## Scope\\nKeep markWorktreeSafe as the single tested production path and remove the older inline exec.Command block/import. Add regression coverage proving startup wiring has exactly one authoritative safe-directory call.\\n\\n## Acceptance\\n- archie-agent startup invokes markWorktreeSafe exactly once\\n- no second inline safe.directory command remains in main.go\\n- cmd/archie-agent tests and repository quality gate pass","acceptance_criteria":"Single tested safe.directory setup path remains; duplicate inline command is removed; tests and task check pass","notes":"Red proof: TestMainConfiguresWorktreeSafetyOnce failed with 2 setup paths before implementation. Green proof: go test/vet/build ./cmd/archie-agent/... passed; golangci-lint --new-from-rev=HEAD reported 0 issues. Fresh adversarial review found no blockers. Full task check was not run because the working tree contains unrelated in-progress changes and task check rewrites files; scoped package gate used per repository protocol.","status":"closed","priority":1,"issue_type":"bug","assignee":"Sam Catlow","owner":"sam@catlow.tech","created_at":"2026-08-13T16:10:09Z","created_by":"Sam Catlow","updated_at":"2026-08-13T16:13:48Z","started_at":"2026-08-13T16:10:24Z","closed_at":"2026-08-13T16:13:48Z","close_reason":"Removed duplicate inline safe.directory setup; markWorktreeSafe is now the single tested production path","external_ref":"https://github.com/samcharles93/archie-core/issues/470","dependency_count":0,"dependent_count":0,"comment_count":0}
{"_type":"issue","id":"archie-core-enfj","title":"[EPIC] Dynamic workflow routing: route task intent to the cheapest sufficient workflow","description":"workflow.Route() (internal/domain/workflow/workflow.go:237) only checks an explicit t.Workflow, then label match, then falls back unconditionally to 'implement' -- the heaviest workflow. There is no content/intent-based routing at all. task_spawn's own tool schema actively overpromises: 'workflow: ... Omit to let the daemon route it' (internal/gateway/task_tools.go:240), implying real routing exists when it means only 'check labels, then give up and run the heaviest workflow anyway'.\n\nConfirmed via production task 6 (2026-09-02): a chat-spawned task whose entire content was 'this is just a test, close it when you receive it' ran the full implement pipeline (prepare, baseline, plan, build, commit-push) end to end, burning 669,421 tokens and ~7 minutes, when nothing about the request needed a worktree, a plan, or a build at all.\n\nNeeds a design doc before any code lands per CLAUDE.md's scope-discipline rule (no settled design exists).","status":"closed","priority":2,"issue_type":"epic","owner":"sam@catlow.tech","created_at":"2026-09-02T11:29:07Z","created_by":"Sam Catlow","updated_at":"2026-09-02T11:40:00Z","closed_at":"2026-09-02T11:40:00Z","close_reason":"Implemented internal/domain/workflow/triage.go: a cheap classify-only-once AgentStage (still runs StagePrepareWorktree -- AgentStage requires tc.Dir to mount into the container, no cheaper primitive exists) that either closes a task needing no code change (reusing the CloseIssue/StatusMerged pattern feasibility.go's OnResult already established for the same ctx-less constraint) or sets tc.Task.Workflow and requeues under the chosen workflow (implement/tdd/feasibility, defaulting to implement for a missing/unrecognized choice). Route()'s fallback (workflow.go) now prefers reg[\"triage\"] over reg[\"implement\"] when registered, falling back unchanged when it isn't -- fully backward compatible. Registered in skillbuild.builtins(). 9 new tests (triage_test.go, routing_test.go) plus the design doc at docs/prds/dynamic-workflow-triage.md. task check clean.","dependency_count":0,"dependent_count":0,"comment_count":0}
{"_type":"issue","id":"archie-core-rwzo","title":"Token usage totals mislead: raw prompt sum not netted against cache hits","description":"Confirmed on production task 6 (2026-09-02, carina): dashboard/PR summary showed '14 iterations · 405,004 tokens · 400,021 prompt · 4,983 completion · 360,018 cached'. Cross-checked against the per-iteration daemon log: nothing is double-counted or hardcoded -- the totals are the correct sum of 14 real, separately-billed API calls with growing context. But 360,018 of the 400,021 'prompt tokens' shown were cache hits (billed at a steep discount), not full price -- real fresh spend was ~40,003 tokens. Showing only the raw prompt-token sum makes every agent run's cost read roughly 10x worse than what was actually billed, everywhere this aggregate surfaces (PR body, dashboard timeline).\n\nSeparately (real, not just cosmetic cost): StageBaselineGate's retry-mission prompt re-injected the ENTIRE go test ./... output -- hundreds of lines covering every passing package -- into every retry iteration's prompt, not just the failing ones. Direct mechanical contributor to the growing per-iteration prompt sizes.","design":"TaskContext.RunUsage (in-memory, not persisted -- Task.TokensUsed remains authoritative) accumulates agentexec.Usage across every agent run in one workflow execution. formatTokenUsage(total, usage) renders 'N tokens (F fresh + C cached)' with a plain fallback. extractFailingGateOutput(s) strips go test 'ok' package lines, only when it recognizes --- FAIL:/FAIL\\t markers, else returns input unchanged. Known gap: a task resumed after a daemon restart mid-workflow loses RunUsage and falls back to the plain total -- an honest degrade, not a bug; persisting the breakdown would need a store migration for a purely presentational value, not worth it yet.","acceptance_criteria":"PR bodies and the dashboard timeline show a fresh-vs-cached breakdown (or an equivalent net figure) instead of a raw prompt-token sum, falling back gracefully when no usage breakdown is available. Gate-retry mission prompts drop passing-package output, keeping only failure-relevant content, with graceful degradation for non-Go gates or unrecognized output shapes. task check passes.","status":"closed","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-09-02T11:28:39Z","created_by":"Sam Catlow","updated_at":"2026-09-02T11:28:49Z","closed_at":"2026-09-02T11:28:49Z","close_reason":"Added TaskContext.RunUsage + accumulateUsage (agent.go, implement.go's baseline-fix branch) and formatTokenUsage (workflow.go), wired into implement.go's and tdd.go's PR-body footers plus ui/src/tasks/timeline-event.js's agent_finish rendering (raw prompt replaced with computed fresh = prompt - cached). Added extractFailingGateOutput (workflow.go) trimming go test 'ok' package lines from StageBaselineGate's retry mission, degrading to unchanged output when no --- FAIL:/FAIL\\t marker survives the filter. 5 new Go tests, 1 updated JS test. task check clean, 100/100 JS tests.","dependency_count":0,"dependent_count":0,"comment_count":0}
{"_type":"issue","id":"archie-core-n2pv","title":"Dashboard Logs page never surfaces per-task attempt-log detail","description":"internal/webui/api_tasks_logs.go's handleTaskLogs (GET /api/tasks/{id}/logs) was fully working -- internal/logging/reader.go's decode() already captures every JSON field generically into Entry.Fields (stage, tokens, iterations, model, stop_reason, everything). But no frontend code called it: ui/src/logs/logs.js (the dashboard's only Logs page) exclusively called api.logs(), the daemon-wide feed, which only carries the daemon's own component-scoped Info/Warn/Error lines -- never the rich per-stage/per-agent-run detail that lives in a task's own per-attempt log file. An operator could never see why a specific task took as long as it did, what each workflow stage did, or its token spend breakdown from the dashboard, even though the daemon already recorded and could already serve all of it. Discovered while investigating production task 6 (2026-09-02, archie-core-95dj).","acceptance_criteria":"A task's persisted attempt log (stage timings, iteration counts, token/prompt/completion/cached breakdowns, model) is visible from the dashboard task view. Reuses the daemon-wide log page's row-rendering logic rather than duplicating it. Handles disabled/empty/error states without crashing. task check (including task test:ui) passes.","status":"closed","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-09-02T11:26:44Z","created_by":"Sam Catlow","updated_at":"2026-09-02T11:26:52Z","closed_at":"2026-09-02T11:26:52Z","close_reason":"Added ui/src/base/log-row.js (row rendering extracted out of logs.js, shared -- second consumer per CLAUDE.md), ui/src/tasks/task-logs.js (taskLogPanel handling loading/error/disabled/empty/populated states), api.taskLogs() in ui/src/base/api.js, and a collapsible 'Attempt log' section wired into each task's existing timeline drill-down in ui/src/tasks/tasks.js -- lazily fetched on first expand. New tests: ui/test/log-row.test.js, ui/test/task-logs.test.js. task check clean, 100/100 JS tests.","dependency_count":0,"dependent_count":0,"comment_count":0}
{"_type":"issue","id":"archie-core-1786637499161-356-e424e40d.1","title":"setupMemory never calls RegisterExternal; cfg.Memory.Provider is silently inert","description":"internal/app/archied/bootstrap.go:setupMemory's own doc comment says\n\"External providers from config are added via RegisterExternal when\ncfg.Memory.Provider is set\" -- no such branch exists in the function body.\nToday, an operator who sets [memory] provider = \"...\" in config.toml gets\nno error and no effect: the builtin file-backed provider is the only one\nthat ever runs. config.example.toml already documents this as a known gap\n(\"[memory] exists to register an EXTERNAL provider alongside it, but as of\nthis writing that wiring ... is not called\").\n\nSeparately, internal/config.MemoryConfig (config.go:287) has no `toml`\nstruct tag on any field (yaml/json only), so even fixing the wiring call\nmay not bind values from config.toml, the format operators actually use --\nverify this before or as part of the fix.\n\nThis sits ahead of the typed engine-family work (archie-core-...-11-e245c170)\nin the sense that it's a bug in the CURRENT ad-hoc MemoryProvider wiring,\nnot a gap in the future engine contract -- decide whether it's worth fixing\nin the legacy path or superseded once the conforming memory engine family\nlands, rather than fixing both.","status":"closed","priority":2,"issue_type":"bug","owner":"sam@catlow.tech","created_at":"2026-09-01T15:50:52Z","created_by":"Sam Catlow","updated_at":"2026-09-01T16:51:00Z","closed_at":"2026-09-01T16:51:00Z","close_reason":"Fixed, but not as originally framed. Investigated wiring RegisterExternal\nfor real: there is no external MemoryProvider implementation anywhere in\nthe repo to construct and pass to it (only internal/memory/builtin.Provider\nexists) -- calling RegisterExternal would be dead plumbing to nothing, not\na fix.\n\nThe actual fix: configuration.validateMemory now rejects memory.provider\nbeing set at all, with a clear reason, instead of accepting it and doing\nnothing. Turns the silent no-op into a loud one. setupMemory's doc comment\nand config.example.toml updated to match; the now-pointless\nprovider_config example section removed.\n\ntoml struct tags (the other half of this bug) were already fixed in\narchie-core-1786637490043's commit.\n\nDrop the rejection once a real external provider (or the domain/memory\nengine family's Honcho proof, archie-core-1786637492031) gives the field\nan effect again.\n\nTestValidateMemoryRejectsProviderSetToAnything covers it. task check is\ngreen.","external_ref":"https://github.com/samcharles93/archie-core/issues/649","labels":["epic"],"dependencies":[{"issue_id":"archie-core-1786637499161-356-e424e40d.1","depends_on_id":"archie-core-1786637499161-356-e424e40d","type":"parent-child","created_at":"2026-09-01T15:50:52Z","created_by":"Sam Catlow","metadata":"{}"}],"dependency_count":0,"dependent_count":0,"comment_count":0}
+112
View File
@@ -0,0 +1,112 @@
# Dynamic workflow triage
Epic: archie-core-enfj. Scope: give `workflow.Route()` a real, content-aware
fallback instead of unconditionally defaulting every unlabeled task to the
heaviest workflow.
## Problem
`Route()` (`internal/domain/workflow/workflow.go:254`) has exactly two real
signals today: an explicit `t.Workflow`, and label matches via
`workflowForLabels` (`routing.go`, `bug`→tdd, `feature`→feasibility,
bootstrap→bootstrap). Anything else — which is every chat-spawned task,
since `task_spawn` rarely sets labels — falls straight through to
`reg["implement"]`, the full `prepare → baseline → plan → build →
commit-push → review → open-pr` pipeline, regardless of what the task
actually asks for.
`task_spawn`'s own tool schema overpromises: *"workflow: … Omit to let the
daemon route it"* (`internal/gateway/task_tools.go:240`) implies real
routing exists. It doesn't — "route" currently means "check labels, then
give up and run the heaviest workflow anyway."
Confirmed in production (task 6, 2026-09-02): a chat-spawned task whose
entire content was "this is just a test, close it when you receive it" ran
the full `implement` pipeline end to end — 669,421 tokens, ~7 minutes —
when nothing about the request needed a worktree-driven build at all. The
`plan` stage's own LLM output *correctly* concluded no code change was
needed, but that conclusion arrives two expensive stages (`baseline`,
`plan`) too late to save anything.
## Decision
Add a **`triage` workflow**: cheap, classifies once, then either closes the
task immediately or hands off to the workflow the task actually needs.
`Route()`'s final fallback (currently `reg["implement"]`) becomes
`reg["triage"]` when a `triage` entry is registered, else the existing
`implement` fallback — a two-line change, fully backward compatible when
`triage` isn't wired up.
Triage does **not** replace label-based routing. A labeled task
(`bug`/`feature`/bootstrap) already has a free, reliable signal and goes
straight to its known workflow exactly as today — spending a classification
call there would be pure waste. Triage only fires in the gap that currently
defaults to `implement` blindly: no explicit workflow, no label match.
### Triage workflow shape
```
StagePrepareWorktree() -- still needed: AgentStage mounts tc.Dir into the
agent container: internal/domain/workflow/agent.go:74
passes tc.Dir straight through. No cheaper
primitive exists in this codebase for a
directory-less classification call.
AgentStage "classify" -- ReadOnly, ONE cheap agent turn, no exploration
tools required beyond what ReadOnly already
grants. Mirrors feasibility.go's decideCaptureTools
pattern: a structured capture tool, called
exactly once, MaxCalls: 1.
```
Mission: read the task's title/body/labels (already in `taskPromptBlock`)
and decide, via a captured `decide` tool call:
```json
{"needs_code_change": bool, "workflow": "implement" | "tdd" | "feasibility", "reasons": "..."}
```
`needs_code_change: false` → close path: reuse `closeNoChangesIssue`
(`steps.go:67`) directly — issue closed if forge-backed, task marked
`StatusMerged`/`"completed -- no changes required"` otherwise. No baseline,
no plan, no build, no commit, no PR. This is the actual fix for the
production incident: a no-op request now costs one classification call
(low thousands of tokens) instead of 669,421.
`needs_code_change: true` → sets `tc.Task.Workflow` to the classifier's
chosen workflow (`implement`/`tdd`/`feasibility`, defaulting to
`implement` if the field is missing or unrecognized) and an Outcome that
requeues the task under it, the same requeue mechanism
`chatTaskControllerAdapter.ApproveChatTask` already uses
(`main.go:529`, `requeue(ctx, taskID, fromStatus, workflow)`). The task then
re-enters `Route()` on its next claim with `t.Workflow` now set, and takes
the normal path for that workflow from there — triage never runs its
classification twice for the same task.
### Non-goals (v1)
- Full bug/feature intent classification quality — the classifier only
needs to be *directionally* right; a wrong `tdd` vs `implement` call
still produces a working PR, just via a slightly less-tailored pipeline.
Tightening this is a follow-up, not a blocker.
- Any change to label-based routing, `Route()`'s explicit-workflow branch,
or `Bootstrap()`/`Feasibility()`/`TDD()` themselves.
- Triage for forge-sourced (real GitHub issue) tasks with no labels —
in scope structurally (same fallback branch), but the dominant case this
fixes is chat-spawned tasks, which essentially never carry labels today.
## Testing
- `workflow_test.go`: `Route()` picks `reg["triage"]` over `reg["implement"]`
for an unlabeled task when both are registered; falls back to
`reg["implement"]` unchanged when `triage` isn't registered (regression
guard for every existing `Route()` test).
- New `triage_test.go`: classify → `needs_code_change: false` closes
without ever calling `tc.Trees.Prepare` a second time or touching
`tc.Agent.Run` again; classify → `needs_code_change: true` sets
`tc.Task.Workflow` and produces a requeue Outcome, using a fake
`agentRunnerFunc` returning a captured decision, mirroring
`feasibility_test.go`'s existing `decideCaptureTools` test style.
- Integration-shaped test reproducing the production scenario: a
chat-spawned task with no labels, title "this is just a test, close it
when you receive it" — asserts the task closes without ever reaching a
`baseline`/`plan`/`build` stage.
+8 -4
View File
@@ -439,15 +439,19 @@ func (f *fakeTrees) CommitAll(context.Context, string, string) (bool, error) {
return f.commitAllChanged, nil
}
func (f *fakeTrees) Push(_ context.Context, _ string, branch string) error {
func (f *fakeTrees) Push(_ context.Context, _, branch string) error {
f.pushed = true
f.pushBranch = branch
return nil
}
func (f *fakeTrees) Diff(context.Context, string, string) (string, error) { return "", nil }
func (f *fakeTrees) Diff(context.Context, string, string) (string, error) { return "", nil }
func (f *fakeTrees) ChangedFiles(context.Context, string, string) ([]string, error) { return nil, nil }
func (f *fakeTrees) ChangedLines(context.Context, string, string) (int, error) { return 0, nil }
func (f *fakeTrees) Snapshot(context.Context, string, string) error { return nil }
func (f *fakeTrees) ChangedLines(context.Context, string, string) (int, error) { return 0, nil }
func (f *fakeTrees) Snapshot(context.Context, string, string) error { return nil }
// TestStageCommitPushPushesBaselineFixEvenWithNothingNewToCommit is the
// regression case for archie-core-95dj: StageBaselineGate already
+34
View File
@@ -13,3 +13,37 @@ func TestRouteDoesNotSilentlyReplaceAnAdmittedWorkflow(t *testing.T) {
t.Fatalf("Route() = %#v, want explicit failure workflow", wf)
}
}
// TestRoutePrefersTriageOverImplementWhenNoLabelMatches is the regression
// case for archie-core-enfj: an unlabeled task (the common shape for a
// chat-spawned task) with no explicit workflow must reach triage, not the
// heaviest workflow, when triage is registered.
func TestRoutePrefersTriageOverImplementWhenNoLabelMatches(t *testing.T) {
task := &store.Task{}
wf := Route(task, Registry{"implement": Implement(), "triage": Triage()})
if wf.Name != "triage" {
t.Fatalf("Route() = %q, want %q", wf.Name, "triage")
}
}
// TestRouteFallsBackToImplementWhenTriageIsNotRegistered pins backward
// compatibility: a registry without a "triage" entry must behave exactly
// as it did before triage existed.
func TestRouteFallsBackToImplementWhenTriageIsNotRegistered(t *testing.T) {
task := &store.Task{}
wf := Route(task, Registry{"implement": Implement()})
if wf.Name != "implement" {
t.Fatalf("Route() = %q, want %q", wf.Name, "implement")
}
}
// TestRouteLabelMatchStillWinsOverTriage confirms labels remain the
// preferred, free signal: a labeled task never pays for a triage
// classification call even when triage is registered.
func TestRouteLabelMatchStillWinsOverTriage(t *testing.T) {
task := &store.Task{Labels: "bug"}
wf := Route(task, Registry{"tdd": TDD(), "triage": Triage(), "implement": Implement()})
if wf.Name != "tdd" {
t.Fatalf("Route() = %q, want %q", wf.Name, "tdd")
}
}
@@ -28,6 +28,7 @@ func builtins() workflow.Registry {
"implement": workflow.Implement(),
"tdd": workflow.TDD(),
"feasibility": workflow.Feasibility(),
"triage": workflow.Triage(),
"default": workflow.Implement(),
}
}
+122
View File
@@ -0,0 +1,122 @@
package workflow
import (
"context"
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/store"
)
// 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: func(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. Then call the decide tool EXACTLY ONCE and afterwards call finish "+
"with status \"passed\".",
taskKind(tc.Task), tc.Repo.FullName(), taskPromptBlock(tc.Task),
)
},
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: store.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: store.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. 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,
}}
}
+191
View File
@@ -0,0 +1,191 @@
package workflow
import (
"context"
"encoding/json"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/store"
)
func triageDecision(t *testing.T, needsCodeChange bool, workflow, reasons string) []json.RawMessage {
t.Helper()
payload := map[string]any{"needs_code_change": needsCodeChange, "reasons": reasons}
if workflow != "" {
payload["workflow"] = workflow
}
raw, err := json.Marshal(payload)
if err != nil {
t.Fatal(err)
}
return []json.RawMessage{raw}
}
func classifyStage() Stage {
wf := Triage()
for _, s := range wf.Stages {
if s.Name == "classify" {
return s
}
}
panic("triage workflow has no classify stage")
}
// TestTriageClosesWithoutCodeChange is the regression case for
// archie-core-enfj: a task the classifier judges needs no code change
// closes immediately, without ever reaching baseline/plan/build.
func TestTriageClosesWithoutCodeChange(t *testing.T) {
f := &fakeForge{}
runner := agentRunnerFunc(func(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
return agentexec.Result{
Version: agentexec.ProtocolVersion,
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Status: agentexec.StatusPassed,
Captures: map[string][]json.RawMessage{"decide": triageDecision(t, false, "", "purely conversational, nothing to build")},
}, nil
})
tc := &TaskContext{
Task: &store.Task{ID: 1, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1},
Repo: config.Repo{Owner: "o", Name: "r"},
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
Agent: runner,
Forge: f,
Log: slog.New(slog.DiscardHandler),
}
if err := classifyStage().Run(context.Background(), tc); err != nil {
t.Fatalf("classify stage = %v, want nil", err)
}
if f.closed != 1 {
t.Fatalf("CloseIssue calls = %d, want 1", f.closed)
}
if tc.Outcome.Status != store.StatusMerged {
t.Fatalf("Outcome.Status = %q, want %q", tc.Outcome.Status, store.StatusMerged)
}
if tc.Task.Workflow != "" {
t.Fatalf("Task.Workflow = %q, want unset (no requeue on the close path)", tc.Task.Workflow)
}
}
// TestTriageClosesChatTaskWithoutForgeCall confirms a chat-spawned task
// (synthetic issue number, no real forge issue behind it) closes via the
// Outcome alone -- never calling CloseIssue against a number that does
// not exist on the forge.
func TestTriageClosesChatTaskWithoutForgeCall(t *testing.T) {
f := &fakeForge{}
runner := agentRunnerFunc(func(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
return agentexec.Result{
Version: agentexec.ProtocolVersion,
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Status: agentexec.StatusPassed,
Captures: map[string][]json.RawMessage{"decide": triageDecision(t, false, "", "just a test")},
}, nil
})
tc := &TaskContext{
Task: &store.Task{ID: 1, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1_000_000_000_000_001, Source: store.SourceChat},
Repo: config.Repo{Owner: "o", Name: "r"},
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
Agent: runner,
Forge: f,
Log: slog.New(slog.DiscardHandler),
}
if err := classifyStage().Run(context.Background(), tc); err != nil {
t.Fatalf("classify stage = %v, want nil", err)
}
if f.closed != 0 {
t.Fatalf("CloseIssue calls = %d, want 0 for a chat-spawned (non-forge-backed) task", f.closed)
}
if tc.Outcome.Status != store.StatusMerged {
t.Fatalf("Outcome.Status = %q, want %q", tc.Outcome.Status, store.StatusMerged)
}
}
// TestTriageRequeuesUnderChosenWorkflow confirms a task the classifier
// judges needs a code change gets tc.Task.Workflow set to its choice and
// an Outcome that requeues it -- never running build itself.
func TestTriageRequeuesUnderChosenWorkflow(t *testing.T) {
runner := agentRunnerFunc(func(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
return agentexec.Result{
Version: agentexec.ProtocolVersion,
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Status: agentexec.StatusPassed,
Captures: map[string][]json.RawMessage{"decide": triageDecision(t, true, "tdd", "reported bug, needs a repro test first")},
}, nil
})
tc := &TaskContext{
Task: &store.Task{ID: 1, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1},
Repo: config.Repo{Owner: "o", Name: "r"},
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
Agent: runner,
Log: slog.New(slog.DiscardHandler),
}
if err := classifyStage().Run(context.Background(), tc); err != nil {
t.Fatalf("classify stage = %v, want nil", err)
}
if tc.Task.Workflow != "tdd" {
t.Fatalf("Task.Workflow = %q, want %q", tc.Task.Workflow, "tdd")
}
if tc.Outcome.Status != store.StatusQueued {
t.Fatalf("Outcome.Status = %q, want %q", tc.Outcome.Status, store.StatusQueued)
}
}
// TestTriageDefaultsToImplementForUnrecognizedWorkflow confirms a missing
// or unrecognized workflow choice degrades to "implement" rather than
// producing an unroutable task.
func TestTriageDefaultsToImplementForUnrecognizedWorkflow(t *testing.T) {
runner := agentRunnerFunc(func(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
return agentexec.Result{
Version: agentexec.ProtocolVersion,
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Status: agentexec.StatusPassed,
Captures: map[string][]json.RawMessage{"decide": triageDecision(t, true, "not-a-real-workflow", "unsure, defaulting")},
}, nil
})
tc := &TaskContext{
Task: &store.Task{ID: 1, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1},
Repo: config.Repo{Owner: "o", Name: "r"},
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
Agent: runner,
Log: slog.New(slog.DiscardHandler),
}
if err := classifyStage().Run(context.Background(), tc); err != nil {
t.Fatalf("classify stage = %v, want nil", err)
}
if tc.Task.Workflow != "implement" {
t.Fatalf("Task.Workflow = %q, want %q", tc.Task.Workflow, "implement")
}
}
// TestTriageRejectsMissingDecideCall pins that classify must not silently
// pass through without a verdict -- mirrors feasibility's assess stage.
func TestTriageRejectsMissingDecideCall(t *testing.T) {
runner := agentRunnerFunc(func(_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter) (agentexec.Result, error) {
return agentexec.Result{Version: agentexec.ProtocolVersion, TaskID: req.TaskID, Attempt: req.Attempt, Stage: req.Stage, Status: agentexec.StatusPassed}, nil
})
tc := &TaskContext{
Task: &store.Task{ID: 1, Attempt: 1, Owner: "o", Repo: "r", IssueNumber: 1},
Repo: config.Repo{Owner: "o", Name: "r"},
Cfg: config.Config{Models: map[string]string{"planner": "provider/model"}},
Agent: runner,
Log: slog.New(slog.DiscardHandler),
}
if err := classifyStage().Run(context.Background(), tc); err == nil {
t.Fatal("classify stage = nil, want an error when decide was never called")
}
}
+8
View File
@@ -266,6 +266,14 @@ func Route(t *store.Task, reg Registry) Workflow {
if wf, ok := workflowForLabels(reg, t.Labels); ok {
return wf
}
// No explicit workflow, no label match: a labelled task already has a
// free, reliable signal and never reaches here. Everything else --
// overwhelmingly chat-spawned tasks, which rarely carry labels -- gets
// classified by triage instead of defaulting straight to the heaviest
// workflow. See docs/prds/dynamic-workflow-triage.md.
if wf, ok := reg["triage"]; ok {
return wf
}
if wf, ok := reg["implement"]; ok {
return wf
}