Files
archie/internal/workflow/feasibility.go
T
sam 170fe0250b fix: close the duplicate-work loop and the dead paths around it
Applies the four-reviewer pass over the forge/dashboard-actions work.
The through-line is that several capabilities were wired but inert, and
one of them could cost real work.

DUPLICATE WORK. "Closes #N" was dropped from PR bodies in favour of
LinkBranch, which is sidebar linkage on Gitea and a no-op stub on
GitHub -- neither closes anything, and nothing else did either. So a
merged task left its issue open, labelled and assigned; the next poll
re-enqueued it, and once the dashboard's Clear removed the task row that
became a second implementation and a second PR for work already merged.
reconcilePRs now closes the issue on merge, and a rejected task closes
its issue too. A PR closed without merging deliberately leaves the issue
open: that usually means "try again".

MAX RETRIES. Configured, defaulted and reported by /api/config, and
enforced nowhere -- markDead was unreachable and EffectiveMaxRetries had
no callers, so the dashboard's retry incremented retry_count forever.
The retry action now retires a task to dead at the cap.

DEAD ON ARRIVAL. Tasks() never selected plan or source, so the
"Decision required" panel could not render and chat tasks -- whose issue
number is synthetic -- linked to forge issues that do not exist.

COSMETIC FAILURE, FATAL RESULT. OpenPR failed the whole stage when
LinkBranch errored, so the pull request was never opened and the task
parked; a retry re-links the same branch, which Gitea answers with a
conflict, so every retry parked again. It is best-effort now.

INVISIBLE ACTIONS. Approve/retry/reject wrote store rows and nothing
else, so the timeline and activity stream never showed that a human had
intervened. They emit events now.

Also: store write failures answer 500 rather than 409; the dashboard
surfaces the server's actual reason instead of "409 Conflict"; action
buttons stop propagation (acting on a task no longer also expands it),
disable in flight, keep per-task errors instead of one shared string,
and confirm before rejecting.

Removed: the maybeRetryParked shim and markDead with their now-vacuous
tests, WaitingTasks (no consumers; its tests were rewritten to assert
the same routing through TaskByID), write-only Daemon.Runtime, and
dir/dirs response bloat from /api/skills. skillSource checked workDir
before sharedDir, mislabelling every shared skill when the two are equal.
DefaultRoots now dedupes roots differing only by a trailing separator.

The air build rule claiming to keep ui/dist current never ran -- it
required one file to satisfy include_dir and include_file together, and
ui/ is excluded from the walk. Deleted with the comment that promised it.
task test:ui joins task check and CI; it ran nowhere before, so the
dashboard suite could rot silently.

Docs corrected where they described behaviour that no longer exists:
parks no longer comment on the issue, state labels no longer drive
retries, and forgerpc's doc claimed four proxied methods while listing
three of five.
2026-08-05 03:54:25 +10:00

167 lines
6.0 KiB
Go

package workflow
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/store"
)
// Feasibility is the feature-request workflow: assess the request
// against the project's direction, close it with reasons when it
// doesn't fit, otherwise produce a PRD, deliver it to Sam (issue
// comment + notify webhook), and hand the task to waiting_human. The
// daemon watches for Sam's reply and -- LLM-judged, not keyword-matched --
// requeues approved features under the implement workflow or closes
// rejected ones. Routed via the "feature" label.
func Feasibility() Workflow {
return Workflow{
Name: "feasibility",
Stages: []Stage{
StagePrepareWorktree(), // read-only stages still need the checkout
AgentStage{
Name: "assess",
Role: "planner",
ReadOnly: true,
CaptureTools: decideCaptureTools,
Mission: func(tc *TaskContext) string {
return fmt.Sprintf(
"Assess whether this feature request fits the project %s.\n\n"+
"%s\n\n"+
"Read the repository's AGENT.md, ROADMAP.md, and README (whichever exist) plus "+
"enough code to judge scope and architectural fit. Then call the decide tool "+
"EXACTLY ONCE with fit=true or fit=false and your reasons, and afterwards call "+
"finish with status \"passed\" summarising the assessment.",
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("assess stage called the decide tool %d times (want exactly once)", len(calls))
}
var captured struct {
Fit *bool `json:"fit"`
Reasons string `json:"reasons"`
}
if err := json.Unmarshal(calls[0], &captured); err != nil {
return fmt.Errorf("decode feasibility decision: %w", err)
}
if captured.Fit == nil {
return fmt.Errorf("assess stage decision has no boolean fit value")
}
if captured.Reasons == "" {
return fmt.Errorf("assess stage decision has no reasons")
}
tc.decision = &decision{Fit: *captured.Fit, Reasons: captured.Reasons}
if !tc.decision.Fit {
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.StatusClosedWontDo, Detail: tc.decision.Reasons}
}
return nil
},
}.Stage(),
AgentStage{
Name: "prd",
Role: "planner",
ReadOnly: true,
Mission: func(tc *TaskContext) string {
return fmt.Sprintf(
"Write a PRD for this accepted feature request on %s.\n\n"+
"%s\n\n<assessment>\n%s\n</assessment>\n\n"+
"Explore the affected code surface, then call finish with status \"passed\" and the "+
"PRD as the summary: problem, proposed solution, files/components affected, "+
"acceptance criteria, explicit non-goals, and estimated diff size. It will be read "+
"by a human deciding whether to green-light implementation.",
tc.Repo.FullName(), taskPromptBlock(tc.Task), tc.decision.Reasons,
)
},
OnResult: func(tc *TaskContext, res agentexec.Result) error {
tc.Task.Plan = res.Summary
return nil
},
}.Stage(),
// Persist the PRD and block on Sam. Messaging and the Web UI are
// the decision surfaces; the forge issue is not a chat log.
{Name: "deliver", Run: func(ctx context.Context, tc *TaskContext) error {
notify(ctx, tc, "feasibility_prd")
tc.Outcome = Outcome{Status: store.StatusWaitingHuman, Detail: "PRD delivered, awaiting go/no-go"}
return nil
}},
},
}
}
// decision is the assess stage's structured verdict.
type decision struct {
Fit bool `json:"fit"`
Reasons string `json:"reasons"`
}
// decideCaptureTools gives the assess agent a structured verdict tool. Its
// arguments cross the execution boundary as data and are applied by OnResult.
func decideCaptureTools(*TaskContext) []agentexec.CaptureTool {
params := json.RawMessage(`{
"type": "object",
"properties": {
"fit": {"type": "boolean", "description": "true: fits the project and is worth a PRD. false: close as won't-do."},
"reasons": {"type": "string", "description": "The rationale, written for the human who filed the request."}
},
"required": ["fit", "reasons"]
}`)
return []agentexec.CaptureTool{{
Name: "decide", Description: "Record the feasibility verdict. Call exactly once, before finish.",
Parameters: params, RequiredFields: []string{"fit", "reasons"},
NonEmptyStrings: []string{"reasons"}, BooleanFields: []string{"fit"}, MaxCalls: 1,
}}
}
// notify POSTs a JSON payload to the configured webhook (n8n turns it
// into an email). Best-effort: failures log, the issue comment is the
// authoritative channel.
func notify(ctx context.Context, tc *TaskContext, kind string) {
url := tc.Cfg.Notify.Webhook
if url == "" {
return
}
fields := map[string]any{
"type": kind, "repo": tc.Repo.FullName(), "task_id": tc.Task.ID,
"source": tc.Task.Source, "identity": tc.Task.Identity,
"title": tc.Task.Title, "prd": tc.Task.Plan,
}
if tc.Task.IsForgeBacked() {
fields["issue"] = tc.Task.IssueNumber
fields["issue_url"] = fmt.Sprintf("%s/%s/issues/%d",
tc.Cfg.Forge.Host, tc.Repo.FullName(), tc.Task.IssueNumber)
}
payload, _ := json.Marshal(fields)
reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, url, bytes.NewReader(payload))
if err != nil {
tc.Log.Warn("notify webhook request build failed", "err", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
tc.Log.Warn("notify webhook failed", "err", err)
return
}
if err := resp.Body.Close(); err != nil {
tc.Log.Warn("close notify webhook response", "err", err)
}
}