diff --git a/internal/app/archied/outcome_post.go b/internal/app/archied/outcome_post.go index 82f0b3382..0df58a40d 100644 --- a/internal/app/archied/outcome_post.go +++ b/internal/app/archied/outcome_post.go @@ -13,6 +13,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/messaging" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/taskstate" ) const outcomePostInterval = 15 * time.Second @@ -102,6 +103,9 @@ func validOrigin(origin string) bool { } func outcomeText(t *task.Task) string { + if kind := taskstate.ApprovalKind(t.Status, t.ReviewGate); kind != "" { + return fmt.Sprintf("Task %d (%s) needs your approval of its %s. Approve or reject it on the task page.", t.ID, t.Title, approvalNoun[kind]) + } text := fmt.Sprintf("Task %d (%s) finished: %s", t.ID, t.Title, t.Status) if t.PRNumber > 0 { text += fmt.Sprintf(", PR #%d", t.PRNumber) @@ -111,3 +115,9 @@ func outcomeText(t *task.Task) string { } return text } + +// approvalNoun names what an approval kind asks the operator to decide. +var approvalNoun = map[string]string{ + taskstate.ApprovalPlan: "plan", + taskstate.ApprovalReview: "review findings", +} diff --git a/internal/infrastructure/postgres/postgresdb/tasks.sql.go b/internal/infrastructure/postgres/postgresdb/tasks.sql.go index a9298c567..940a49445 100644 --- a/internal/infrastructure/postgres/postgresdb/tasks.sql.go +++ b/internal/infrastructure/postgres/postgresdb/tasks.sql.go @@ -1216,6 +1216,7 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error const transitionTask = `-- name: TransitionTask :execrows UPDATE tasks SET status = $2, + outcome_posted = CASE WHEN status = 'waiting_approval' THEN false ELSE outcome_posted END, park_reason = CASE WHEN $2 = 'parked' THEN $3 ELSE park_reason END, park_class = CASE WHEN $2 = 'parked' THEN $4 ELSE park_class END, park_blocker = CASE WHEN $2 = 'parked' THEN '' ELSE park_blocker END, @@ -1233,6 +1234,8 @@ type TransitionTaskParams struct { // Arriving at 'parked' also records why. The class is normalized by the // caller, so an unknown value persists as needs_human rather than as itself. +// Leaving 'waiting_approval' clears outcome_posted: the approval request was +// posted, and the task's final outcome still has to be. func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams) (int64, error) { result, err := q.db.Exec(ctx, transitionTask, arg.ID, @@ -1250,13 +1253,14 @@ func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams) const unpostedOutcomes = `-- name: UnpostedOutcomes :many SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted, binding_signature, binding_addr, park_blocker FROM tasks WHERE origin <> '' AND NOT outcome_posted - AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed') + AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed', 'waiting_approval') AND ($1::text IS NULL OR org_id = $1) ORDER BY id LIMIT 50 ` -// Finished chat tasks whose outcome has not reached their conversation yet. +// Chat tasks that finished or wait for approval, whose state has not reached +// their conversation yet. func (q *Queries) UnpostedOutcomes(ctx context.Context, scopeOrg pgtype.Text) ([]Task, error) { rows, err := q.db.Query(ctx, unpostedOutcomes, scopeOrg) if err != nil { diff --git a/internal/infrastructure/postgres/queries/tasks.sql b/internal/infrastructure/postgres/queries/tasks.sql index 14259284b..e41a074bc 100644 --- a/internal/infrastructure/postgres/queries/tasks.sql +++ b/internal/infrastructure/postgres/queries/tasks.sql @@ -102,10 +102,11 @@ WHERE origin = sqlc.arg(origin) AND origin <> '' AND status IN ('queued', 'runni ORDER BY id; -- name: UnpostedOutcomes :many --- Finished chat tasks whose outcome has not reached their conversation yet. +-- Chat tasks that finished or wait for approval, whose state has not reached +-- their conversation yet. SELECT * FROM tasks WHERE origin <> '' AND NOT outcome_posted - AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed') + AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed', 'waiting_approval') AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY id LIMIT 50; @@ -151,8 +152,11 @@ SELECT status FROM tasks WHERE id = $1 FOR UPDATE; -- name: TransitionTask :execrows -- Arriving at 'parked' also records why. The class is normalized by the -- caller, so an unknown value persists as needs_human rather than as itself. +-- Leaving 'waiting_approval' clears outcome_posted: the approval request was +-- posted, and the task's final outcome still has to be. UPDATE tasks SET status = $2, + outcome_posted = CASE WHEN status = 'waiting_approval' THEN false ELSE outcome_posted END, park_reason = CASE WHEN $2 = 'parked' THEN $3 ELSE park_reason END, park_class = CASE WHEN $2 = 'parked' THEN $4 ELSE park_class END, park_blocker = CASE WHEN $2 = 'parked' THEN '' ELSE park_blocker END,