From 561863613f02b6fb1ae37831a07294f0c979e7aa Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 9 Nov 2026 09:38:01 +1100 Subject: [PATCH] feat(workflow): agent.run asks to continue when it reaches its step budget (#1832) --- internal/app/archied/outcome_post.go | 3 ++- internal/app/archiegateway/chat_tasks.go | 2 +- internal/daemon/daemon.go | 5 +++++ internal/domain/workflow/agent.go | 26 ++++++++++++++++++++++++ internal/domain/workflow/agent_run.go | 1 + internal/taskstate/taskstate.go | 17 +++++++++++++--- internal/webui/tasks_handlers.go | 2 +- internal/worktree/worktree.go | 13 +++++++++++- ui/src/lib/task-meta.ts | 1 + 9 files changed, 63 insertions(+), 7 deletions(-) diff --git a/internal/app/archied/outcome_post.go b/internal/app/archied/outcome_post.go index 5d135ea7a..097bcc3e9 100644 --- a/internal/app/archied/outcome_post.go +++ b/internal/app/archied/outcome_post.go @@ -104,7 +104,7 @@ func validOrigin(origin string) bool { func outcomeText(t *task.Task) string { title := notificationTitle(t.Title) - if kind := taskstate.ApprovalKind(t.Status, t.ReviewGate); kind != "" { + if kind := taskstate.ApprovalKind(t.Status, t.ReviewGate, t.RetryMode); kind != "" { return fmt.Sprintf("Task %d (%s) needs your approval of its %s. Approve or reject it on the task page.", t.ID, title, approvalNoun[kind]) } if t.Status == task.StatusPROpen { @@ -141,4 +141,5 @@ func notificationTitle(title string) string { var approvalNoun = map[string]string{ taskstate.ApprovalPlan: "plan", taskstate.ApprovalReview: "review findings", + taskstate.ApprovalBudget: "request to continue past its step budget", } diff --git a/internal/app/archiegateway/chat_tasks.go b/internal/app/archiegateway/chat_tasks.go index 45afe39cb..2097e9a33 100644 --- a/internal/app/archiegateway/chat_tasks.go +++ b/internal/app/archiegateway/chat_tasks.go @@ -107,7 +107,7 @@ func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity strin // started -- so the pinned definition answers there instead. A review wait and // every other status name the workflow the task is on or will run next. func (a chatTaskListerAdapter) chatWorkflow(ctx context.Context, task workflow.Task) string { - if a.task == nil || taskstate.ApprovalKind(task.Status, task.ReviewGate) != taskstate.ApprovalPlan { + if a.task == nil || taskstate.ApprovalKind(task.Status, task.ReviewGate, task.RetryMode) != taskstate.ApprovalPlan { return task.Workflow } full, err := a.task(ctx, task.ID) diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 83460e6c9..5bed71434 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1807,6 +1807,11 @@ func (d *Daemon) prepareWorkspace(ctx context.Context, task *workflow.Task, tree // The retry mode on the task decides whether to resume the pushed branch or // reset onto base. Prepare clones a missing worktree either way. target := worktree.Fresh + if taskstate.NormalizeRetryMode(task.RetryMode) == taskstate.RetryKeepWorktree { + target = worktree.Keep + // Consumed: a later plain requeue must not skip the reset. + task.RetryMode = "" + } if taskstate.NormalizeRetryMode(task.RetryMode) == taskstate.RetryContinuePushedWork { if task.Branch == "" { // The action refuses this combination, so an empty branch here is a diff --git a/internal/domain/workflow/agent.go b/internal/domain/workflow/agent.go index e9c89cd1e..150de32c7 100644 --- a/internal/domain/workflow/agent.go +++ b/internal/domain/workflow/agent.go @@ -11,6 +11,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/agentrun" "github.com/samcharles93/archie-core/internal/events" "github.com/samcharles93/archie-core/internal/skill" + "github.com/samcharles93/archie-core/internal/taskstate" ) // AgentStage runs one LLM-driven workflow stage on the agent runtime with its @@ -41,6 +42,9 @@ type AgentStage struct { // OnResult is called; any other parked or idle result parks the // workflow. OnResult func(*TaskContext, agentrun.Result) error + // ApproveBudget makes a stop on the step budget ask for approval to + // continue, in the same worktree, instead of parking the run. + ApproveBudget bool // ReviewResult gates agent output before OnResult forwards it to the // delivery channels. The daemon calls this hook to review stage output (issue // comments, PR bodies) before delivery. Return an error to block the @@ -256,6 +260,24 @@ func budgetStopOutcome(stage string, res agentrun.Result) Outcome { return Outcome{Status: StatusParked, Detail: clip(b.String(), 4000)} } +// awaitBudgetApproval waits for a decision on running the stage again. The +// worktree is kept as it stands and the stage's findings and changed files go +// to the next run as task notes, which stand in for the transcript: the run +// resumes from what is on disk, not from the model's message history. +func awaitBudgetApproval(tc *TaskContext, stage string, res agentrun.Result) { + note := fmt.Sprintf("Stage %s stopped on its step budget and was approved to continue. The worktree holds its work so far; carry on from it.", stage) + if summary := strings.TrimSpace(res.Summary); summary != "" { + note += " Where it stood: " + summary + } + if len(res.Changes) > 0 { + note += " Files changed so far: " + strings.Join(res.Changes, ", ") + "." + } + tc.Task.Notes += "- " + note + "\n" + tc.Task.RetryMode = string(taskstate.RetryKeepWorktree) + detail := budgetStopOutcome(stage, res).Detail + tc.Outcome = Outcome{Status: StatusWaitingApproval, Detail: clip(detail+"\n\nApprove to continue with another full step budget, or reject to stop.", 4000)} +} + // deliverResult checks the run passed, then runs ReviewResult and OnResult. func (a AgentStage) deliverResult(tc *TaskContext, res agentrun.Result) error { if res.Status != agentrun.StatusPassed { @@ -263,6 +285,10 @@ func (a AgentStage) deliverResult(tc *TaskContext, res agentrun.Result) error { // run with what it found, without demanding the gate passed. // ReviewResult and OnResult stay behind the passed check, because // a budget stop is not a success. + if a.ApproveBudget && res.StopReason == stopBudgetExhausted { + awaitBudgetApproval(tc, a.Name, res) + return nil + } if budgetStopped(res) { tc.Outcome = budgetStopOutcome(a.Name, res) return nil diff --git a/internal/domain/workflow/agent_run.go b/internal/domain/workflow/agent_run.go index 8ffe44c3d..59b455d97 100644 --- a/internal/domain/workflow/agent_run.go +++ b/internal/domain/workflow/agent_run.go @@ -83,6 +83,7 @@ func newAgentRunStage(settings yaml.Node) (Stage, error) { return json.Unmarshal(calls[0], &tc.stepResult.Result) }, } + agent.ApproveBudget = true applyRepositoryControls(&agent, s) if resultTool != nil { agent.CaptureTools = func(*TaskContext) []agentrun.CaptureTool { return []agentrun.CaptureTool{*resultTool} } diff --git a/internal/taskstate/taskstate.go b/internal/taskstate/taskstate.go index 575d61ff3..eb584ab3b 100644 --- a/internal/taskstate/taskstate.go +++ b/internal/taskstate/taskstate.go @@ -113,14 +113,19 @@ const ( ApprovalPlan = "plan" // ApprovalReview is a review gate's findings. ApprovalReview = "review" + // ApprovalBudget is a stage that reached its step budget and asks to + // continue with more steps. + ApprovalBudget = "budget" ) // ApprovalKind is what a task waits on the operator to approve, or "" when it // is not waiting for approval. -func ApprovalKind(status, reviewGate string) string { +func ApprovalKind(status, reviewGate, retryMode string) string { switch { case status != WaitingApproval: return "" + case NormalizeRetryMode(retryMode) == RetryKeepWorktree: + return ApprovalBudget case reviewGate != "": return ApprovalReview default: @@ -141,6 +146,11 @@ const ( // the retry continues the work an earlier attempt committed instead of // resetting over it. It requires a branch to resume. RetryContinuePushedWork RetryMode = "continue_pushed_work" + // RetryKeepWorktree resumes in the task's worktree as it stands, with its + // uncommitted changes. A stage that stopped on its step budget sets it + // while it waits for approval to continue; it is never an operator + // choice. + RetryKeepWorktree RetryMode = "keep_worktree" ) // NormalizeRetryMode maps a stored retry mode to the mode it means. Empty and @@ -148,8 +158,9 @@ const ( // choice resets onto base, which is the safe default for work that is not an // open pull request. func NormalizeRetryMode(mode string) RetryMode { - if RetryMode(mode) == RetryContinuePushedWork { - return RetryContinuePushedWork + switch RetryMode(mode) { + case RetryContinuePushedWork, RetryKeepWorktree: + return RetryMode(mode) } return RetryRefreshOntoBase } diff --git a/internal/webui/tasks_handlers.go b/internal/webui/tasks_handlers.go index 3e545e63d..b772df814 100644 --- a/internal/webui/tasks_handlers.go +++ b/internal/webui/tasks_handlers.go @@ -88,7 +88,7 @@ func (a *tasksAPI) handleTasks(w http.ResponseWriter, r *http.Request) { views := make([]taskView, len(tasks)) for i := range tasks { repoURL, issueURL, prURL := taskURLs(tasks[i], forge(tasks[i])) - views[i] = taskView{Task: tasks[i], Actions: taskstate.Actions(tasks[i].Status), Approval: taskstate.ApprovalKind(tasks[i].Status, tasks[i].ReviewGate), RepoURL: repoURL, IssueURL: issueURL, PRURL: prURL, Recovery: a.recovery(r, &tasks[i])} + views[i] = taskView{Task: tasks[i], Actions: taskstate.Actions(tasks[i].Status), Approval: taskstate.ApprovalKind(tasks[i].Status, tasks[i].ReviewGate, tasks[i].RetryMode), RepoURL: repoURL, IssueURL: issueURL, PRURL: prURL, Recovery: a.recovery(r, &tasks[i])} } writeJSON(w, views) } diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index 8fd169794..0491f5925 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -111,6 +111,11 @@ type Target string // Fresh is the fresh-run target: reset onto origin/. const Fresh Target = "" +// Keep leaves a prepared worktree as it stands, uncommitted changes included, +// so a run resumes where the last one stopped. A missing worktree is cloned +// fresh. +const Keep Target = "\x00keep" + // Prepare clones the task's worktree if missing and positions it on target, // returning its directory and branch. A resume fails if the branch is not on // the remote. @@ -126,7 +131,7 @@ func (m *Manager) Prepare( } dir = m.Dir(owner, repo, issue) branch = archieBranch(issue, title, labels) - if target != Fresh { + if target != Fresh && target != Keep { branch = string(target) } @@ -150,6 +155,12 @@ func (m *Manager) Prepare( } } + if target == Keep && prepared { + return dir, branch, nil + } + if target == Keep { + target = Fresh + } if err := m.sync(ctx, dir, base, branch, target); err != nil { return "", "", err } diff --git a/ui/src/lib/task-meta.ts b/ui/src/lib/task-meta.ts index d2fe6dc2b..c476ea2f9 100644 --- a/ui/src/lib/task-meta.ts +++ b/ui/src/lib/task-meta.ts @@ -151,4 +151,5 @@ export function configSchema(): string { export const approvalLabels: Record = { plan: "Plan", review: "Review findings", + budget: "Continue?", };