Files
archie/internal/infrastructure/postgres/postgresdb/tasks.sql.go
T
sam d1ed835030 feat(postgres): State Store schema and sqlc queries, with the claim path proven on PG 18
goose migration 0001 lands the twelve State Store tables on Postgres, and sqlc generates the task queries from it. Two behaviours that genuinely differ from SQLite are pinned by tests against a real PG 18: claiming takes the oldest queued row with FOR UPDATE SKIP LOCKED (without the clause a second claimer blocks, which the test shows by timing out), and every column behind a Go int is bigint -- SQLite's INTEGER is 64-bit, and the chat issue-number allocator seeds at 10^15, which does not fit an int4.

task db:generate writes the committed postgresdb package; task db:check regenerates it in the gate, as proto:check and ui:check do.

The remaining store methods (events, resources, identities, the status tables) still run on SQLite: porting them is the rest of archie-core-7r0x.2.
2026-09-24 01:18:19 +10:00

568 lines
14 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: tasks.sql
package postgresdb
import (
"context"
"time"
)
const claimNextTask = `-- name: ClaimNextTask :one
UPDATE tasks
SET status = 'running', attempt = attempt + 1, updated_at = now()
WHERE id = (
SELECT id FROM tasks
WHERE status = 'queued'
ORDER BY id
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
`
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
// claimers running at once take different rows instead of blocking, so the
// daemon no longer depends on holding the only connection to the file.
func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
row := q.db.QueryRow(ctx, claimNextTask)
var i Task
err := row.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Stage,
&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,
)
return i, err
}
const clearTerminalTasks = `-- name: ClearTerminalTasks :execrows
DELETE FROM tasks
WHERE status IN ('merged', 'rejected', 'dead', 'closed_wont_do')
`
func (q *Queries) ClearTerminalTasks(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, clearTerminalTasks)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const countTasksByStatus = `-- name: CountTasksByStatus :many
SELECT status, count(*)::int AS count FROM tasks GROUP BY status
`
type CountTasksByStatusRow struct {
Status string
Count int32
}
func (q *Queries) CountTasksByStatus(ctx context.Context) ([]CountTasksByStatusRow, error) {
rows, err := q.db.Query(ctx, countTasksByStatus)
if err != nil {
return nil, err
}
defer rows.Close()
var items []CountTasksByStatusRow
for rows.Next() {
var i CountTasksByStatusRow
if err := rows.Scan(&i.Status, &i.Count); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertChatTask = `-- name: InsertChatTask :one
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity)
VALUES (
$1, $2,
COALESCE((
SELECT MAX(existing.issue_number) FROM tasks existing
WHERE existing.owner = $1
AND existing.repo = $2
AND existing.source = 'chat'
), $3) + 1,
$4, $5, 'chat', $6, 'chat', $7
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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
`
type InsertChatTaskParams struct {
Owner string
Repo string
FallbackIssueNumber int64
Title string
Body string
Workflow string
Identity string
}
// The synthetic issue number keeps chat-sourced tasks off the forge's real
// issue numbers; this allocator is the single source of truth for it.
// fallback_issue_number is only the seed for a repo's first chat task -- the
// passed value is not the issue number that lands.
func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams) (Task, error) {
row := q.db.QueryRow(ctx, insertChatTask,
arg.Owner,
arg.Repo,
arg.FallbackIssueNumber,
arg.Title,
arg.Body,
arg.Workflow,
arg.Identity,
)
var i Task
err := row.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Stage,
&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,
)
return i, err
}
const insertTransition = `-- name: InsertTransition :exec
INSERT INTO transitions (task_id, from_status, to_status, detail)
VALUES ($1, $2, $3, $4)
`
type InsertTransitionParams struct {
TaskID int64
FromStatus string
ToStatus string
Detail string
}
func (q *Queries) InsertTransition(ctx context.Context, arg InsertTransitionParams) error {
_, err := q.db.Exec(ctx, insertTransition,
arg.TaskID,
arg.FromStatus,
arg.ToStatus,
arg.Detail,
)
return err
}
const listTaskSummaries = `-- name: ListTaskSummaries :many
SELECT id, owner, repo, issue_number, title, status, workflow, stage,
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
created_at, updated_at, plan, source, identity, binding_id, binding_version
FROM tasks ORDER BY updated_at DESC LIMIT $1
`
type ListTaskSummariesRow struct {
ID int64
Owner string
Repo string
IssueNumber int64
Title string
Status string
Workflow string
Stage string
PrNumber int64
TokensUsed int64
Iterations int64
Attempt int64
ParkReason string
RetryCount int64
CreatedAt time.Time
UpdatedAt time.Time
Plan string
Source string
Identity string
BindingID string
BindingVersion int64
}
// The dashboard's list. This projection is deliberately narrow: Plan gates
// the "Decision required" panel and Source labels the row, and widening it
// back would send rows without them.
func (q *Queries) ListTaskSummaries(ctx context.Context, limit int32) ([]ListTaskSummariesRow, error) {
rows, err := q.db.Query(ctx, listTaskSummaries, limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListTaskSummariesRow
for rows.Next() {
var i ListTaskSummariesRow
if err := rows.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Status,
&i.Workflow,
&i.Stage,
&i.PrNumber,
&i.TokensUsed,
&i.Iterations,
&i.Attempt,
&i.ParkReason,
&i.RetryCount,
&i.CreatedAt,
&i.UpdatedAt,
&i.Plan,
&i.Source,
&i.Identity,
&i.BindingID,
&i.BindingVersion,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const parkTask = `-- name: ParkTask :execrows
UPDATE tasks
SET status = 'parked', park_reason = $2, park_class = $3, updated_at = now()
WHERE id = $1 AND status = $4
`
type ParkTaskParams struct {
ID int64
ParkReason string
ParkClass string
Status string
}
func (q *Queries) ParkTask(ctx context.Context, arg ParkTaskParams) (int64, error) {
result, err := q.db.Exec(ctx, parkTask,
arg.ID,
arg.ParkReason,
arg.ParkClass,
arg.Status,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const recoverStaleTasks = `-- name: RecoverStaleTasks :execrows
UPDATE tasks SET status = 'queued', updated_at = now() WHERE status = 'running'
`
func (q *Queries) RecoverStaleTasks(ctx context.Context) (int64, error) {
result, err := q.db.Exec(ctx, recoverStaleTasks)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const stampTaskBinding = `-- name: StampTaskBinding :exec
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1
`
type StampTaskBindingParams struct {
ID int64
BindingID string
BindingVersion int64
}
func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingParams) error {
_, err := q.db.Exec(ctx, stampTaskBinding, arg.ID, arg.BindingID, arg.BindingVersion)
return err
}
const taskByID = `-- name: TaskByID :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE id = $1
`
func (q *Queries) TaskByID(ctx context.Context, id int64) (Task, error) {
row := q.db.QueryRow(ctx, taskByID, id)
var i Task
err := row.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Stage,
&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,
)
return i, err
}
const taskByIssue = `-- name: TaskByIssue :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
`
type TaskByIssueParams struct {
Owner string
Repo string
IssueNumber int64
}
func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task, error) {
row := q.db.QueryRow(ctx, taskByIssue, arg.Owner, arg.Repo, arg.IssueNumber)
var i Task
err := row.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Stage,
&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,
)
return i, err
}
const taskByPR = `-- name: TaskByPR :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, stage, 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 FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3
`
type TaskByPRParams struct {
Owner string
Repo string
PrNumber int64
}
func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error) {
row := q.db.QueryRow(ctx, taskByPR, arg.Owner, arg.Repo, arg.PrNumber)
var i Task
err := row.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Stage,
&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,
)
return i, err
}
const transitionTask = `-- name: TransitionTask :execrows
UPDATE tasks
SET status = $2,
park_reason = CASE WHEN $2 = 'parked' THEN $3 ELSE park_reason END,
park_class = CASE WHEN $2 = 'parked' THEN $4 ELSE park_class END,
updated_at = now()
WHERE id = $1 AND status = $5
`
type TransitionTaskParams struct {
ID int64
Status string
ParkReason string
ParkClass string
Status_2 string
}
// 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.
func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams) (int64, error) {
result, err := q.db.Exec(ctx, transitionTask,
arg.ID,
arg.Status,
arg.ParkReason,
arg.ParkClass,
arg.Status_2,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const updateTask = `-- name: UpdateTask :exec
UPDATE tasks SET workflow = $2, stage = $3, branch = $4, plan = $5, notes = $6,
pr_number = $7, tokens_used = $8, iterations = $9, park_reason = $10,
watch_comment_id = $11, retry_count = $12, remediation_rounds = $13,
review_payload = $14, workflow_definition_version = $15,
workflow_definition_digest = $16, workflow_definition_yaml = $17,
updated_at = now()
WHERE id = $1
`
type UpdateTaskParams struct {
ID int64
Workflow string
Stage string
Branch string
Plan string
Notes string
PrNumber int64
TokensUsed int64
Iterations int64
ParkReason string
WatchCommentID int64
RetryCount int64
RemediationRounds int64
ReviewPayload string
WorkflowDefinitionVersion int64
WorkflowDefinitionDigest string
WorkflowDefinitionYaml string
}
func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) error {
_, err := q.db.Exec(ctx, updateTask,
arg.ID,
arg.Workflow,
arg.Stage,
arg.Branch,
arg.Plan,
arg.Notes,
arg.PrNumber,
arg.TokensUsed,
arg.Iterations,
arg.ParkReason,
arg.WatchCommentID,
arg.RetryCount,
arg.RemediationRounds,
arg.ReviewPayload,
arg.WorkflowDefinitionVersion,
arg.WorkflowDefinitionDigest,
arg.WorkflowDefinitionYaml,
)
return err
}