Files
archie/internal/webui/api_tasks.go
T
sam 1f2e7a42eb refactor: one definition of what approving or declining a task means
The dashboard and chat both let an operator approve or refuse work, and
they had drifted. Declining from the dashboard left the task
closed_wont_do; cancelling the same task from chat left it rejected. Two
operators looking at one queue saw different states for the same
decision, and "rejected" -- which reconcilePRs also uses for a pull
request closed without merging -- stopped meaning one thing.

internal/gateway deliberately does not import internal/store, so it kept
a hand-synced copy of the status strings with a comment asking future
editors to keep them aligned. That is the same defect one level down,
and it is how the drift happened.

internal/taskstate is a leaf package with no dependencies holding the
lifecycle vocabulary and the rules for operator actions. gateway, store
and the dashboard all import it, so gateway stays decoupled from store
without a copy, and CheckApprove/CheckRetry/CheckDecline have one
implementation. Declined is named for what it is -- a human said no --
and stays distinct from Rejected, which is something archie observed.

Chat cancel now records Declined. That is the behaviour change: same
decision, same state, whichever surface the operator used.

Rules were preserved rather than tidied while unifying. A parked task
still cannot be declined, which both surfaces already refused, even
though "not terminal" would have allowed it -- widening a rule under
cover of a refactor is how you ship a change nobody reviewed. The
comment says where to change it if that is ever wanted.

TestDashboardAndChatAgreeOnTerminalStates drives both real surfaces
against one store, so this cannot drift apart again without a red test.

Also removes a second rule for the dash-top-standalone class: renderSetup
already reconciles it from the data at the top of every call, and a later
branch cleared it again. Harmless, but it reads as two sources of truth
for one bit of layout state.
2026-08-05 04:15:39 +10:00

269 lines
8.5 KiB
Go

package webui
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/taskstate"
)
func (s *Server) handleSummary(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
statuses, err := s.Store.StatusCounts(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
workflows, err := s.Store.WorkflowStats(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
stages, err := s.Store.StageStats(ctx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
days, err := s.Store.TokensByDay(ctx, 14)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, map[string]any{
"statuses": statuses, "workflows": workflows,
"stages": stages, "tokens_by_day": days,
})
}
func (s *Server) handleTasks(w http.ResponseWriter, r *http.Request) {
tasks, err := s.Store.Tasks(r.Context(), 100)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, tasks)
}
func (s *Server) handleClearTasks(w http.ResponseWriter, r *http.Request) {
deleted, err := s.Store.ClearTerminalTasks(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, map[string]any{"deleted": deleted})
}
func (s *Server) handleTask(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.Error(w, "bad id", http.StatusBadRequest)
return
}
evs, err := s.Store.TaskEvents(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
writeJSON(w, evs)
}
type taskActionRequest struct {
Action string `json:"action"`
}
func (s *Server) handleTaskAction(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.Error(w, "bad id", http.StatusBadRequest)
return
}
var req taskActionRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "invalid action", http.StatusBadRequest)
return
}
task, err := s.Store.TaskByID(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if task == nil {
http.Error(w, "task not found", http.StatusNotFound)
return
}
// Each action reports the event to record, or writes its own error
// response and returns nothing. Keeping the dispatch flat here and the
// rules in one function each is what stops this growing back into a
// single branch nobody can read.
ctx := r.Context()
var outcome *actionOutcome
switch req.Action {
case "approve":
outcome = s.approveTask(ctx, w, task)
case "retry":
outcome = s.retryTask(ctx, w, task)
case "reject":
outcome = s.rejectTask(ctx, w, task)
default:
http.Error(w, "unknown action", http.StatusBadRequest)
return
}
if outcome == nil {
return
}
s.emit(events.Event{
Kind: outcome.kind, TaskID: task.ID,
Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber,
Detail: outcome.detail,
})
writeJSON(w, map[string]any{"ok": true, "action": req.Action, "task_id": id})
}
// actionOutcome is what a completed operator action should record.
type actionOutcome struct {
kind string
detail string
}
// storeFailed writes the response for a failed store write and reports
// whether it did. A store write that fails is not the caller's fault;
// answering 409 made the UI say "conflict" for a broken database.
func storeFailed(w http.ResponseWriter, err error) bool {
if err == nil {
return false
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return true
}
func (s *Server) approveTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome {
if err := taskstate.CheckApprove(task.Status); err != nil {
http.Error(w, err.Error(), http.StatusConflict)
return nil
}
if storeFailed(w, s.Store.Requeue(ctx, task.ID, store.StatusWaitingHuman, "implement")) {
return nil
}
return &actionOutcome{events.KindHumanApproved, "approved via the dashboard"}
}
// retryTask requeues a parked task, or retires it once max_retries is spent.
//
// max_retries was configured, defaulted and reported by /api/config while
// nothing enforced it: retry_count climbed forever, so a task that fails
// every time could be retried without limit. At the cap the task belongs in
// dead, which is what the setting exists to say.
func (s *Server) retryTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome {
if err := taskstate.CheckRetry(task.Status); err != nil {
http.Error(w, err.Error(), http.StatusConflict)
return nil
}
if limit := s.maxRetriesFor(task); limit > 0 && task.RetryCount >= limit {
s.retireTask(ctx, task, fmt.Sprintf("max retries reached (%d/%d)", task.RetryCount, limit))
http.Error(w, fmt.Sprintf("max retries reached (%d/%d)", task.RetryCount, limit), http.StatusConflict)
return nil
}
if storeFailed(w, s.Store.Requeue(ctx, task.ID, store.StatusParked, "")) {
return nil
}
if storeFailed(w, s.Store.IncrementRetryCount(ctx, task.ID)) {
return nil
}
return &actionOutcome{events.KindTaskRetried, "retried via the dashboard"}
}
// retireTask moves a spent task to dead. Failure is logged, not returned:
// the caller is already answering with the reason it refused the retry.
func (s *Server) retireTask(ctx context.Context, task *store.Task, reason string) {
if err := s.Store.Transition(ctx, task.ID, store.StatusParked, store.StatusDead, reason); err != nil {
s.logf("transition to dead failed", "task", task.ID, "err", err)
return
}
s.emit(events.Event{
Kind: events.KindTaskDead, TaskID: task.ID,
Repo: task.Owner + "/" + task.Repo, Issue: task.IssueNumber, Detail: reason,
})
}
func (s *Server) rejectTask(ctx context.Context, w http.ResponseWriter, task *store.Task) *actionOutcome {
// The dashboard only offers Reject on a waiting_human task, but the rule
// is the shared one so the two surfaces cannot drift apart again.
if err := taskstate.CheckApprove(task.Status); err != nil {
http.Error(w, err.Error(), http.StatusConflict)
return nil
}
err := s.Store.Transition(ctx, task.ID, store.StatusWaitingHuman, store.StatusClosedWontDo, "declined from the dashboard")
if storeFailed(w, err) {
return nil
}
s.closeRejectedIssue(ctx, task)
return &actionOutcome{events.KindHumanRejected, "rejected via the dashboard"}
}
// maxRetriesFor resolves the retry cap for a task, honouring a per-repo
// override. Zero means unlimited.
func (s *Server) maxRetriesFor(task *store.Task) int {
if s.Cfg == nil {
return 0
}
for _, repo := range s.Cfg.Repos {
if repo.Owner == task.Owner && repo.Name == task.Repo {
return repo.EffectiveMaxRetries(s.Cfg.MaxRetries)
}
}
return s.Cfg.MaxRetries
}
// closeRejectedIssue closes the forge issue behind a task the operator has
// declined.
//
// Without this the issue stays open, labelled and assigned, so the next poll
// re-enqueues it -- and once the operator uses Clear, which deletes the task
// row, that is a second implementation and a second PR for work already
// refused. Approve and retry deliberately leave the issue open: the task is
// going back to work.
//
// Failure is logged, not returned. The operator's decision is already
// recorded, and reporting an error would invite them to click again.
func (s *Server) closeRejectedIssue(ctx context.Context, task *store.Task) {
if s.Issues == nil {
s.logf("task rejected but no forge is wired; the issue stays open and will be re-polled",
"task", task.ID, "issue", task.IssueNumber)
return
}
// A chat task's issue number is synthetic and matches no forge issue.
if !task.IsForgeBacked() {
return
}
const comment = "Closing: this was rejected from the archie dashboard. " +
"Reopen the issue to have archie pick it up again."
if err := s.Issues.CloseIssue(ctx, task.Owner, task.Repo, task.IssueNumber, comment); err != nil {
s.logf("closing rejected issue failed; it stays open and will be re-polled",
"task", task.ID, "issue", task.IssueNumber, "err", err)
}
}
// emit publishes an operator action so it reaches the task timeline and the
// live activity stream. A nil publisher is not an error: the action is still
// recorded in the store.
func (s *Server) emit(e events.Event) {
if s.Events == nil || e.Kind == "" {
return
}
s.Events.Publish(e)
}
func (s *Server) logf(msg string, args ...any) {
if s.Log == nil {
return
}
s.Log.Warn(msg, args...)
}