feat(telegram): /stop stops the chat agent, else the tasks it created, or one task; delete the dead approval actions

This commit is contained in:
sam committed 2026-10-06 01:12:28 +11:00
1 parent 399098329b
commit b0f1ad7cdc
52 files changed
+2385 -2781

No files matched your search

@@ -0,0 +1,12 @@
-- tasks.origin names the conversation that created a chat task, so a chat's
-- /stop can find the work its agent started. Empty for every other task.
-- +goose Up
ALTER TABLE tasks ADD COLUMN origin text NOT NULL DEFAULT '';
CREATE INDEX idx_tasks_origin ON tasks (origin) WHERE origin <> '';
-- +goose Down
DROP INDEX idx_tasks_origin;
ALTER TABLE tasks DROP COLUMN origin;
@@ -24,7 +24,7 @@ func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, e
const enqueueCallTask = `-- name: EnqueueCallTask :one
WITH caller AS (
SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews FROM tasks t WHERE t.id = $1 FOR UPDATE
SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews, t.origin FROM tasks t WHERE t.id = $1 FOR UPDATE
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth)
SELECT
@@ -41,7 +41,7 @@ SELECT
caller.id, caller.call_depth + 1
FROM caller
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $7::int
RETURNING 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
RETURNING 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
`
type EnqueueCallTaskParams struct {
@@ -111,6 +111,7 @@ func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
@@ -428,6 +428,7 @@ type Task struct {
ResumeFrom string
ResumeResults []byte
PendingReviews []byte
Origin string
}
type ToolCall struct {
@@ -12,6 +12,79 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const activeTasksByOrigin = `-- name: ActiveTasksByOrigin :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 FROM tasks
WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running')
ORDER BY id
`
// The queued and running tasks one conversation created, for its /stop.
func (q *Queries) ActiveTasksByOrigin(ctx context.Context, origin string) ([]Task, error) {
rows, err := q.db.Query(ctx, activeTasksByOrigin, origin)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Task
for rows.Next() {
var i Task
if err := rows.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Branch,
&i.Plan,
&i.Notes,
&i.PrNumber,
&i.TokensUsed,
&i.Iterations,
&i.Attempt,
&i.ParkReason,
&i.WatchCommentID,
&i.ParkClass,
&i.RemediationRounds,
&i.RetryCount,
&i.Source,
&i.Identity,
&i.BindingID,
&i.BindingVersion,
&i.ReviewPayload,
&i.WorkflowDefinitionVersion,
&i.WorkflowDefinitionDigest,
&i.WorkflowDefinitionYaml,
&i.CreatedAt,
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
&i.Outputs,
&i.ReviewGate,
&i.RereviewRounds,
&i.RetryMode,
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const appendPendingReview = `-- name: AppendPendingReview :execrows
UPDATE tasks SET pending_reviews = CASE
WHEN review_payload = $2::text THEN pending_reviews
@@ -80,7 +153,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation
const claimByIssue = `-- name: ClaimByIssue :one
UPDATE tasks SET status = 'running', attempt = attempt + 1, outputs = '', updated_at = now()
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
RETURNING 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
RETURNING 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
`
type ClaimByIssueParams struct {
@@ -139,6 +212,7 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
@@ -153,7 +227,7 @@ WHERE id = (
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING 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
RETURNING 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
`
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
@@ -210,6 +284,7 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
@@ -302,7 +377,7 @@ func (q *Queries) EnqueueIssue(ctx context.Context, arg EnqueueIssueParams) (int
}
const insertChatTask = `-- name: InsertChatTask :one
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin)
VALUES (
$1, $2,
COALESCE((
@@ -318,9 +393,10 @@ VALUES (
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
),
$8
$8,
$9
)
RETURNING 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
RETURNING 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
`
type InsertChatTaskParams struct {
@@ -332,6 +408,7 @@ type InsertChatTaskParams struct {
Workflow string
Identity string
Inputs string
Origin string
}
// The synthetic issue number keeps chat-sourced tasks off the forge's real
@@ -351,6 +428,7 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
arg.Workflow,
arg.Identity,
arg.Inputs,
arg.Origin,
)
var i Task
err := row.Scan(
@@ -398,6 +476,7 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
@@ -836,7 +915,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
}
const taskByID = `-- name: TaskByID :one
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 FROM tasks WHERE id = $1
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 FROM tasks WHERE id = $1
`
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
@@ -887,12 +966,13 @@ func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
const taskByIssue = `-- name: TaskByIssue :one
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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
`
type TaskByIssueParams struct {
@@ -949,12 +1029,13 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
const taskByPR = `-- name: TaskByPR :one
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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
`
type TaskByPRParams struct {
@@ -1020,6 +1101,7 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
)
return i, err
}
@@ -60,7 +60,7 @@ SELECT status, count(*)::int AS count FROM tasks GROUP BY status;
-- inputs is the chat task's own workflow inputs; a binding dispatch passes
-- its inputs here too, so this insert is the single writer of the column and
-- StampTaskBinding adds only the provenance.
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, inputs, origin)
VALUES (
sqlc.arg(owner), sqlc.arg(repo),
COALESCE((
@@ -76,10 +76,17 @@ VALUES (
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
'org-sys'
),
sqlc.arg(inputs)
sqlc.arg(inputs),
sqlc.arg(origin)
)
RETURNING *;
-- name: ActiveTasksByOrigin :many
-- The queued and running tasks one conversation created, for its /stop.
SELECT * FROM tasks
WHERE origin = sqlc.arg(origin) AND origin <> '' AND status IN ('queued', 'running')
ORDER BY id;
-- name: StampTaskBinding :exec
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1;
@@ -34,7 +34,7 @@ func TestRetryTaskResume(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx := t.Context()
db := pgstore.Open(t)
if _, err := db.EnqueueChatTask(ctx, "", "", "resume", "", "tdd", "", nil); err != nil {
if _, err := db.EnqueueChatTask(ctx, "", "", "resume", "", "tdd", "", "", nil); err != nil {
t.Fatal(err)
}
run, err := db.ClaimNext(ctx)
@@ -59,7 +59,7 @@ func TestSecondReviewDuringRemediationIsQueued(t *testing.T) {
func openTaskAtPROpen(t *testing.T, db *pgstore.TaskDB) int64 {
t.Helper()
ctx := t.Context()
created, err := db.EnqueueChatTask(ctx, "acme", "widget", "a task", "", "tdd", "", nil)
created, err := db.EnqueueChatTask(ctx, "acme", "widget", "a task", "", "tdd", "", "", nil)
if err != nil {
t.Fatal(err)
}
+18 -3
View File
@@ -153,8 +153,8 @@ func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int
}
// EnqueueChatTask inserts a queued chat task with a store-allocated synthetic
// issue number.
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity string, inputs map[string]any) (*workflow.Task, error) {
// issue number. origin names the conversation that created it, if any.
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin string, inputs map[string]any) (*workflow.Task, error) {
encoded, err := task.EncodeInputs(inputs)
if err != nil {
return nil, err
@@ -162,6 +162,7 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
t, err := s.queries().InsertChatTask(ctx, postgresdb.InsertChatTaskParams{
Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity,
Inputs: encoded,
Origin: origin,
FallbackIssueNumber: syntheticIssueNumberBase - 1,
})
if err != nil {
@@ -175,7 +176,7 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
// inputs travel with the insert, so a failed stamp leaves a task whose inputs
// are already right and whose provenance is absent, never the reverse.
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, inputs map[string]any) (*workflow.Task, error) {
t, err := s.EnqueueChatTask(ctx, owner, repo, title, body, wf, identity, inputs)
t, err := s.EnqueueChatTask(ctx, owner, repo, title, body, wf, identity, "", inputs)
if err != nil {
return nil, err
}
@@ -855,3 +856,17 @@ func (s *Store) CallStatus(ctx context.Context, callerTaskID, callTaskID int64)
}
return callee.Status, detail, outputs, nil
}
// ActiveTasksByOrigin returns the queued and running tasks the conversation
// origin created.
func (s *Store) ActiveTasksByOrigin(ctx context.Context, origin string) ([]*workflow.Task, error) {
rows, err := s.queries().ActiveTasksByOrigin(ctx, origin)
if err != nil {
return nil, fmt.Errorf("store: active tasks by origin: %w", err)
}
out := make([]*workflow.Task, 0, len(rows))
for _, row := range rows {
out = append(out, taskFromRow(row))
}
return out, nil
}
@@ -0,0 +1,54 @@
package postgres_test
import (
"slices"
"testing"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// A conversation's /stop finds the queued and running tasks it created, and
// only those: not another conversation's, not finished ones, not originless.
func TestActiveTasksByOrigin(t *testing.T) {
ctx := t.Context()
db := pgstore.Open(t)
enqueue := func(origin string) int64 {
created, err := db.EnqueueChatTask(ctx, "", "", "work", "", "tdd", "", origin, nil)
if err != nil {
t.Fatal(err)
}
return created.ID
}
running, queued, finished := enqueue("telegram:1"), enqueue("telegram:1"), enqueue("telegram:1")
enqueue("telegram:2")
enqueue("")
if err := db.Transition(ctx, running, taskstate.Queued, taskstate.Running, ""); err != nil {
t.Fatal(err)
}
if err := db.Transition(ctx, finished, taskstate.Queued, taskstate.Declined, ""); err != nil {
t.Fatal(err)
}
cases := []struct {
origin string
want []int64
}{
{"telegram:1", []int64{running, queued}},
{"telegram:2", []int64{finished + 1}},
{"", nil},
}
for _, tc := range cases {
tasks, err := db.ActiveTasksByOrigin(ctx, tc.origin)
if err != nil {
t.Fatal(err)
}
var got []int64
for _, task := range tasks {
got = append(got, task.ID)
}
if !slices.Equal(got, tc.want) {
t.Errorf("origin %q: tasks %v, want %v", tc.origin, got, tc.want)
}
}
}