The events table had no attempt column, so two attempts of the same task were indistinguishable: a retried task's stage events interleaved into one timeline and nothing could say which run produced a given result. - events gains an `attempt` column through the existing presence-gated migrateTasks loop rather than a version bump, so user_version stays 3 and an existing archie.db is migrated in place. A legacy-database test builds the pre-change schema and proves both that existing rows survive unattributed (attempt = 0) and that a new value round-trips. - Event.attempt = 11 and ReadTaskLogRequest.stage = 9 are field additions to existing messages: no new RPC, no new table, and no change to any canonical error message that crosses the state-store wire. - Every task event the workflow engine emits now carries the attempt. Producers that have no attempt yet (task_queued, binding_dispatch_failure) stay unattributed rather than guessing one. - Changed files are captured as a durable changes_captured event at commit and push time, reusing the existing three-dot diff primitive: paths from FilePatches(), counts from Stats(), base_sha recorded as the merge base actually diffed rather than the base tip, a 200-file cap with totals kept complete. Consumption goes through an unexported optional interface so workflow.Trees -- the yaegi capability surface for interpreted stage plugins -- is not widened. A capture is also taken once the pull request exists, so the capture can link it. - The per-attempt configuration is captured as a durable config_captured event carrying the non-secret config.TaskConfig, instead of a new table plus two new state-store RPCs: events are already this system's per-attempt provenance stream, so a table would have given one feature two provenance mechanisms. Redaction is proven against canary secrets. Measured payload: 539 bytes typical, 2333 bytes at 20 models. - Captures carry a schema marker and are refused unless their key set matches the declared payload shape, so drift surfaces as "recorded but could not be read" rather than as a capture of zeroes. - Three read endpoints serve the attempt rail, the changed-file captures and a raw debug view, mapping to UI-owned response types rather than dumping store types. - The task-log read gains a stage filter, bound to the stage logger for that stage's own lines; agent and tool output records no stage. A task-scoped grant may already insert events on its own task, so this provenance is exactly as trustworthy as the rest of that task's event stream. That limit is documented rather than papered over.
654 lines
23 KiB
Go
654 lines
23 KiB
Go
// Package store is archied's SQLite state: one row per task (a GitHub
|
|
// issue picked up for work), with every lifecycle transition recorded.
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
)
|
|
|
|
type Store struct {
|
|
db *sql.DB
|
|
bindingsCipher BindingCipher
|
|
}
|
|
|
|
const taskSchemaVersion = 3
|
|
|
|
// OpenOption configures the store at open time.
|
|
type OpenOption func(*openOptions)
|
|
|
|
type openOptions struct {
|
|
bindingsCipher BindingCipher
|
|
}
|
|
|
|
// WithBindingCipher installs an at-rest encryption cipher for binding secrets
|
|
// (docs/prds/binding-secret-encryption.md). When omitted, binding secrets are
|
|
// persisted as plaintext -- legacy behaviour, unchanged.
|
|
func WithBindingCipher(c BindingCipher) OpenOption {
|
|
return func(o *openOptions) { o.bindingsCipher = c }
|
|
}
|
|
|
|
// Open opens (creating if needed) the SQLite database and its schema.
|
|
func Open(ctx context.Context, path string, opts ...OpenOption) (*Store, error) {
|
|
var o openOptions
|
|
for _, opt := range opts {
|
|
opt(&o)
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
db, err := sql.Open("sqlite", sqliteDSN(path))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
db.SetMaxOpenConns(1)
|
|
if _, err := db.ExecContext(ctx, schema+eventsSchema+capturesSchema+mappingsSchema+bindingsSchema+configSnapshotSchema); err != nil {
|
|
return nil, errors.Join(fmt.Errorf("store: init schema: %w", err), db.Close())
|
|
}
|
|
if err := migrateTasks(ctx, db); err != nil {
|
|
return nil, errors.Join(fmt.Errorf("store: migrate: %w", err), db.Close())
|
|
}
|
|
return &Store{db: db, bindingsCipher: o.bindingsCipher}, nil
|
|
}
|
|
|
|
func sqliteDSN(path string) string {
|
|
separator := "?"
|
|
if strings.HasSuffix(path, "?") || strings.HasSuffix(path, "&") {
|
|
separator = ""
|
|
} else if strings.Contains(path, "?") {
|
|
separator = "&"
|
|
}
|
|
return path + separator + "_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
|
|
}
|
|
|
|
func migrateTasks(ctx context.Context, db *sql.DB) error {
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
var version int
|
|
if err := tx.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&version); err != nil {
|
|
return err
|
|
}
|
|
if version > taskSchemaVersion {
|
|
return fmt.Errorf("database schema version %d is newer than supported version %d", version, taskSchemaVersion)
|
|
}
|
|
columns, err := taskColumns(ctx, tx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
bindingColumns, err := tableColumns(ctx, tx, "bindings")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// events is created by `CREATE TABLE IF NOT EXISTS` (eventsSchema), which
|
|
// is a no-op against a database that already has the table. On an existing
|
|
// archie.db this read plus the `events` arm below is therefore the ONLY
|
|
// thing that adds events.attempt; without it the first insert fails with
|
|
// `table events has no column named attempt`.
|
|
eventsColumns, err := tableColumns(ctx, tx, "events")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
migrations := []struct {
|
|
table string
|
|
column string
|
|
sql string
|
|
}{
|
|
{"tasks", "watch_comment_id", `ALTER TABLE tasks ADD COLUMN watch_comment_id INTEGER NOT NULL DEFAULT 0`},
|
|
{"tasks", "retry_count", `ALTER TABLE tasks ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0`},
|
|
{"tasks", "source", `ALTER TABLE tasks ADD COLUMN source TEXT NOT NULL DEFAULT 'forge'`},
|
|
{"tasks", "identity", `ALTER TABLE tasks ADD COLUMN identity TEXT NOT NULL DEFAULT ''`},
|
|
{"tasks", "binding_id", `ALTER TABLE tasks ADD COLUMN binding_id INTEGER NOT NULL DEFAULT 0`},
|
|
{"tasks", "binding_version", `ALTER TABLE tasks ADD COLUMN binding_version INTEGER NOT NULL DEFAULT 0`},
|
|
{"bindings", "owner", `ALTER TABLE bindings ADD COLUMN owner TEXT NOT NULL DEFAULT ''`},
|
|
{"bindings", "repo", `ALTER TABLE bindings ADD COLUMN repo TEXT NOT NULL DEFAULT ''`},
|
|
{"events", "attempt", `ALTER TABLE events ADD COLUMN attempt INTEGER NOT NULL DEFAULT 0`},
|
|
}
|
|
for _, migration := range migrations {
|
|
present := columns
|
|
switch migration.table {
|
|
case "bindings":
|
|
present = bindingColumns
|
|
case "events":
|
|
present = eventsColumns
|
|
}
|
|
if present[migration.column] {
|
|
continue
|
|
}
|
|
if _, err := tx.ExecContext(ctx, migration.sql); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if _, err := tx.ExecContext(ctx, `PRAGMA user_version = 3`); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
func taskColumns(ctx context.Context, tx *sql.Tx) (_ map[string]bool, retErr error) {
|
|
return tableColumns(ctx, tx, "tasks")
|
|
}
|
|
|
|
func tableColumns(ctx context.Context, tx *sql.Tx, table string) (_ map[string]bool, retErr error) {
|
|
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`PRAGMA table_info(%s)`, table))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { retErr = errors.Join(retErr, rows.Close()) }()
|
|
columns := make(map[string]bool)
|
|
for rows.Next() {
|
|
var cid, notNull, primaryKey int
|
|
var name, columnType string
|
|
var defaultValue any
|
|
if err := rows.Scan(&cid, &name, &columnType, ¬Null, &defaultValue, &primaryKey); err != nil {
|
|
return nil, err
|
|
}
|
|
columns[name] = true
|
|
}
|
|
return columns, rows.Err()
|
|
}
|
|
|
|
const schema = `
|
|
CREATE TABLE IF NOT EXISTS tasks (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
owner TEXT NOT NULL,
|
|
repo TEXT NOT NULL,
|
|
issue_number INTEGER NOT NULL,
|
|
title TEXT NOT NULL DEFAULT '',
|
|
body TEXT NOT NULL DEFAULT '',
|
|
labels TEXT NOT NULL DEFAULT '',
|
|
status TEXT NOT NULL DEFAULT '` + workflow.StatusQueued + `',
|
|
workflow TEXT NOT NULL DEFAULT '',
|
|
stage TEXT NOT NULL DEFAULT '',
|
|
branch TEXT NOT NULL DEFAULT '',
|
|
plan TEXT NOT NULL DEFAULT '',
|
|
notes TEXT NOT NULL DEFAULT '',
|
|
pr_number INTEGER NOT NULL DEFAULT 0,
|
|
tokens_used INTEGER NOT NULL DEFAULT 0,
|
|
iterations INTEGER NOT NULL DEFAULT 0,
|
|
attempt INTEGER NOT NULL DEFAULT 0,
|
|
park_reason TEXT NOT NULL DEFAULT '',
|
|
watch_comment_id INTEGER NOT NULL DEFAULT 0,
|
|
retry_count INTEGER NOT NULL DEFAULT 0,
|
|
source TEXT NOT NULL DEFAULT 'forge',
|
|
identity TEXT NOT NULL DEFAULT '',
|
|
binding_id INTEGER NOT NULL DEFAULT 0,
|
|
binding_version INTEGER NOT NULL DEFAULT 0,
|
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
updated_at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
UNIQUE(owner, repo, issue_number)
|
|
);
|
|
CREATE TABLE IF NOT EXISTS transitions (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
task_id INTEGER NOT NULL,
|
|
at TEXT NOT NULL DEFAULT (datetime('now')),
|
|
from_status TEXT NOT NULL,
|
|
to_status TEXT NOT NULL,
|
|
detail TEXT NOT NULL DEFAULT ''
|
|
);
|
|
`
|
|
|
|
func (s *Store) Close() error { return s.db.Close() }
|
|
|
|
// DB exposes the underlying *sql.DB so tests outside the store
|
|
// package can drive fixtures that the public surface does not (yet)
|
|
// cover -- e.g. advancing a binding draft -> pending_approval without
|
|
// going through the operator edit flow. Production callers must NOT
|
|
// use this; the structured methods above are the contract.
|
|
func (s *Store) DB() *sql.DB { return s.db }
|
|
|
|
// OpenTest opens an in-memory SQLite store suitable for tests.
|
|
func OpenTest(t interface {
|
|
TempDir() string
|
|
Context() context.Context
|
|
},
|
|
) *Store {
|
|
s, err := Open(t.Context(), filepath.Join(t.TempDir(), "test.db"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// EnqueueIssue inserts a new queued task for the issue; returns false if
|
|
// the issue is already tracked (the idempotency key is owner/repo/number).
|
|
// The task's Source defaults to "forge" (the SQLite column default).
|
|
// identity is the archie identity whose forge poll discovered this issue
|
|
// (empty for single-identity deployments); it scopes which identity's
|
|
// forge client, worktree manager, and config downstream processing uses.
|
|
func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels, identity string) (bool, error) {
|
|
res, err := s.db.ExecContext(ctx, `
|
|
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, identity)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(owner, repo, issue_number) DO NOTHING`,
|
|
owner, repo, number, title, body, labels, identity)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
n, _ := res.RowsAffected()
|
|
return n > 0, nil
|
|
}
|
|
|
|
// EnqueueChatTask inserts a new queued task with Source "chat" and no
|
|
// backing forge issue, returning the full created row (with its real
|
|
// database ID). The store allocates the synthetic issue number durably so
|
|
// multiple processes sharing the database cannot generate the same value.
|
|
// Workflow stages and daemon reconciliation must check workflow.Task.IsForgeBacked()
|
|
// before treating it as a real forge issue number.
|
|
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity string) (*workflow.Task, error) {
|
|
const syntheticIssueNumberBase = 1_000_000_000_000_000
|
|
row := s.db.QueryRowContext(ctx, `
|
|
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity)
|
|
VALUES (?, ?, COALESCE((
|
|
SELECT MAX(issue_number) FROM tasks
|
|
WHERE owner = ? AND repo = ? AND source = 'chat'
|
|
), ?) + 1, ?, ?, 'chat', ?, 'chat', ?)
|
|
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, retry_count,
|
|
source, identity, binding_id, binding_version, created_at, updated_at`,
|
|
owner, repo, owner, repo, syntheticIssueNumberBase-1, title, body, wf, identity)
|
|
return scanTask(row)
|
|
}
|
|
|
|
// EnqueueBindingTask enqueues a task triggered by a playbook binding
|
|
// dispatch (t2db.4 Phase B). It builds the row through EnqueueChatTask
|
|
// (so the synthetic issue-number allocator stays the single source of
|
|
// truth for chat-sourced tasks) and then stamps binding_id and
|
|
// binding_version on the new row in a second statement.
|
|
//
|
|
// A crash between the two writes leaves the task without provenance
|
|
// (binding_id == 0); the binding_dispatches ledger row written by the
|
|
// dispatch loop still records the (binding, capture, task) triple, so
|
|
// a future repair pass could backfill. This is the same
|
|
// best-effort-provenance pattern the rest of the task lifecycle uses
|
|
// for fields added after the row's primary insert.
|
|
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity string, bindingID int64, bindingVersion int) (*workflow.Task, error) {
|
|
t, err := s.EnqueueChatTask(ctx, owner, repo, title, body, wf, identity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err := s.db.ExecContext(ctx,
|
|
`UPDATE tasks SET binding_id=?, binding_version=? WHERE id=?`,
|
|
bindingID, bindingVersion, t.ID); err != nil {
|
|
return nil, fmt.Errorf("store: stamp binding provenance: %w", err)
|
|
}
|
|
t.BindingID = bindingID
|
|
t.BindingVersion = bindingVersion
|
|
return t, nil
|
|
}
|
|
|
|
// ClaimNext atomically moves the oldest queued task to running and
|
|
// returns it; nil when the queue is empty.
|
|
func (s *Store) ClaimNext(ctx context.Context) (*workflow.Task, error) {
|
|
row := s.db.QueryRowContext(ctx, `
|
|
UPDATE tasks SET status=?, attempt=attempt+1, updated_at=datetime('now')
|
|
WHERE id = (SELECT id FROM tasks WHERE status=? ORDER BY id 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, retry_count,
|
|
source, identity, binding_id, binding_version, created_at, updated_at`,
|
|
workflow.StatusRunning, workflow.StatusQueued)
|
|
t, err := scanTask(row)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return t, err
|
|
}
|
|
|
|
func scanTask(row *sql.Row) (*workflow.Task, error) {
|
|
var t workflow.Task
|
|
err := row.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title, &t.Body,
|
|
&t.Labels, &t.Status, &t.Workflow, &t.Stage, &t.Branch, &t.Plan, &t.Notes,
|
|
&t.PRNumber, &t.TokensUsed, &t.Iterations, &t.Attempt, &t.ParkReason,
|
|
&t.WatchCommentID, &t.RetryCount, &t.Source, &t.Identity,
|
|
&t.BindingID, &t.BindingVersion,
|
|
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &t, nil
|
|
}
|
|
|
|
// ClaimByIssue atomically claims a queued task by owner/repo/issue_number.
|
|
// Returns nil if the task is not in queued state (already claimed, parked,
|
|
// or terminal). Used by the NATS consumer path where the task was just
|
|
// inserted via EnqueueIssue and needs an immediate targeted claim.
|
|
func (s *Store) ClaimByIssue(ctx context.Context, owner, repo string, number int) (*workflow.Task, error) {
|
|
row := s.db.QueryRowContext(ctx, `
|
|
UPDATE tasks SET status=?, attempt=attempt+1, updated_at=datetime('now')
|
|
WHERE owner=? AND repo=? AND issue_number=? AND status=?
|
|
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, retry_count,
|
|
source, identity, binding_id, binding_version, created_at, updated_at`,
|
|
workflow.StatusRunning, owner, repo, number, workflow.StatusQueued)
|
|
t, err := scanTask(row)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return t, err
|
|
}
|
|
|
|
// ErrStaleTransition is returned when a guarded transition fails because
|
|
// the task's current status does not match the expected from status.
|
|
|
|
// Transition moves a task to a new status and records the audit detail. The
|
|
// from status guards the update; a mismatch returns ErrStaleTransition without
|
|
// writing an audit row. Transitioning to parked also stores detail as
|
|
// ParkReason in the same transaction. Detail used to exist only in the
|
|
// timeline, which left daemon-side parks looking reasonless in task APIs and
|
|
// the dashboard.
|
|
func (s *Store) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
res, err := tx.ExecContext(ctx, `
|
|
UPDATE tasks
|
|
SET status=?,
|
|
park_reason=CASE WHEN ?=? THEN ? ELSE park_reason END,
|
|
updated_at=datetime('now')
|
|
WHERE id=? AND status=?`,
|
|
to, to, workflow.StatusParked, clip(detail, 4000), taskID, from)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n == 0 {
|
|
return ErrStaleTransition
|
|
}
|
|
|
|
_, err = tx.ExecContext(ctx,
|
|
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
|
taskID, from, to, clip(detail, 4000))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
// Update persists mutable task fields written by workflows.
|
|
func (s *Store) Update(ctx context.Context, t *workflow.Task) error {
|
|
_, err := s.db.ExecContext(ctx, `
|
|
UPDATE tasks SET workflow=?, stage=?, branch=?, plan=?, notes=?,
|
|
pr_number=?, tokens_used=?, iterations=?, park_reason=?,
|
|
watch_comment_id=?, updated_at=datetime('now')
|
|
WHERE id=?`,
|
|
t.Workflow, t.Stage, t.Branch, t.Plan, t.Notes,
|
|
t.PRNumber, t.TokensUsed, t.Iterations, clip(t.ParkReason, 4000),
|
|
t.WatchCommentID, t.ID)
|
|
return err
|
|
}
|
|
|
|
// TaskByIssue returns the task tracking an issue, or nil.
|
|
func (s *Store) TaskByIssue(ctx context.Context, owner, repo string, number int) (*workflow.Task, error) {
|
|
row := s.db.QueryRowContext(ctx, `
|
|
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, retry_count,
|
|
source, identity, binding_id, binding_version, created_at, updated_at
|
|
FROM tasks WHERE owner=? AND repo=? AND issue_number=?`, owner, repo, number)
|
|
t, err := scanTask(row)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return t, err
|
|
}
|
|
|
|
// TaskByID returns the task with the given database ID, or nil. Used by
|
|
// chat controls (/approve, /cancel) that reference a task by its real
|
|
// ID rather than a forge issue number.
|
|
func (s *Store) TaskByID(ctx context.Context, taskID int64) (*workflow.Task, error) {
|
|
row := s.db.QueryRowContext(ctx, `
|
|
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, retry_count,
|
|
source, identity, binding_id, binding_version, created_at, updated_at
|
|
FROM tasks WHERE id=?`, taskID)
|
|
t, err := scanTask(row)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
return t, err
|
|
}
|
|
|
|
// Requeue puts a task back on the queue. A non-empty workflow forces it
|
|
// (the waiting_human → approved → implement handoff); empty keeps the
|
|
// task's current workflow (retrying a parked task).
|
|
// The fromStatus acts as a guard: the UPDATE only affects rows whose
|
|
// current status matches fromStatus. If no row matches, ErrStaleTransition
|
|
// is returned and no audit row is written. Both statements execute in a
|
|
// single transaction.
|
|
func (s *Store) Requeue(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
res, err := tx.ExecContext(ctx, `
|
|
UPDATE tasks SET status=?,
|
|
workflow=CASE WHEN ?='' THEN workflow ELSE ? END,
|
|
stage='', park_reason='', updated_at=datetime('now')
|
|
WHERE id=? AND status=?`, workflow.StatusQueued, wf, wf, taskID, fromStatus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n == 0 {
|
|
return ErrStaleTransition
|
|
}
|
|
|
|
_, err = tx.ExecContext(ctx,
|
|
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
|
taskID, fromStatus, workflow.StatusQueued, "requeued "+wf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
// RetryTask requeues a task and increments retry_count in the same guarded
|
|
// transaction. A queued task with an uncounted retry would make max_retries a
|
|
// suggestion rather than a cap.
|
|
func (s *Store) RetryTask(ctx context.Context, taskID int64, fromStatus, wf string) error {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
res, err := tx.ExecContext(ctx, `
|
|
UPDATE tasks SET status=?, retry_count=retry_count+1,
|
|
workflow=CASE WHEN ?='' THEN workflow ELSE ? END,
|
|
stage='', park_reason='', updated_at=datetime('now')
|
|
WHERE id=? AND status=?`, workflow.StatusQueued, wf, wf, taskID, fromStatus)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n == 0 {
|
|
return ErrStaleTransition
|
|
}
|
|
if _, err := tx.ExecContext(ctx,
|
|
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
|
|
taskID, fromStatus, workflow.StatusQueued, "retried "+wf); err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|
|
|
|
// ArchiveTask removes one terminal task from the active task board. The
|
|
// expected status is part of the delete predicate so a stale browser cannot
|
|
// remove a task that has since resumed or otherwise changed state. Historical
|
|
// transition and activity rows remain as the operator audit trail.
|
|
func (s *Store) ArchiveTask(
|
|
ctx context.Context,
|
|
taskID int64,
|
|
fromStatus string,
|
|
audit events.Event,
|
|
) (int64, error) {
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
eventID, err := insertEvent(ctx, tx, audit)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
res, err := tx.ExecContext(ctx, `DELETE FROM tasks WHERE id=? AND status=?`, taskID, fromStatus)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if n == 0 {
|
|
return 0, ErrStaleTransition
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return 0, err
|
|
}
|
|
return eventID, nil
|
|
}
|
|
|
|
// RecoverStale re-queues tasks left running by a crashed daemon.
|
|
func (s *Store) RecoverStale(ctx context.Context) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx,
|
|
`UPDATE tasks SET status=?, updated_at=datetime('now') WHERE status=?`,
|
|
workflow.StatusQueued, workflow.StatusRunning)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.RowsAffected()
|
|
}
|
|
|
|
// OpenPRs returns tasks whose PR state should be reconciled with GitHub.
|
|
//
|
|
// It carries attempt deliberately: the reconcile loop attributes pr_merged and
|
|
// pr_rejected to the attempt that opened the PR, and it reaches the state store
|
|
// over the wire, so the row must carry the value for taskProto to publish it.
|
|
// Narrowing this projection back silently sends those events out unattributed.
|
|
func (s *Store) OpenPRs(ctx context.Context) (tasks []workflow.Task, retErr error) {
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt
|
|
FROM tasks WHERE status=?`, workflow.StatusPROpen)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
for rows.Next() {
|
|
var t workflow.Task
|
|
if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.PRNumber,
|
|
&t.Status, &t.Source, &t.Identity, &t.Attempt); err != nil {
|
|
return nil, err
|
|
}
|
|
tasks = append(tasks, t)
|
|
}
|
|
return tasks, rows.Err()
|
|
}
|
|
|
|
// ClearTerminalTasks deletes tasks whose status is terminal. Parked work is
|
|
// deliberately excluded because it is recoverable.
|
|
func (s *Store) ClearTerminalTasks(ctx context.Context) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx,
|
|
`DELETE FROM tasks WHERE status IN (?, ?, ?, ?)`,
|
|
workflow.StatusMerged, workflow.StatusRejected, workflow.StatusDead, workflow.StatusClosedWontDo)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.RowsAffected()
|
|
}
|
|
|
|
func clip(s string, n int) string {
|
|
if len(s) <= n {
|
|
return s
|
|
}
|
|
// Walk forward by runes; stop when the next rune would exceed n bytes.
|
|
pos := 0
|
|
for pos < len(s) {
|
|
_, size := utf8.DecodeRuneInString(s[pos:])
|
|
if pos+size > n {
|
|
break
|
|
}
|
|
pos += size
|
|
}
|
|
return s[:pos]
|
|
}
|
|
|
|
// sqliteTime scans SQLite's TEXT datetime columns into a time.Time.
|
|
//
|
|
// The tasks table stores timestamps as TEXT via datetime('now') rather than a
|
|
// typed column, so the driver hands back a string and the standard time.Time
|
|
// scan fails. Parsing here keeps the column definition untouched -- changing
|
|
// it would need a migration over existing rows for no behavioural gain.
|
|
type sqliteTime struct{ t *time.Time }
|
|
|
|
// sqliteTimeLayout is what datetime('now') produces: UTC, no zone suffix.
|
|
const sqliteTimeLayout = "2006-01-02 15:04:05"
|
|
|
|
func (s sqliteTime) Scan(v any) error {
|
|
switch value := v.(type) {
|
|
case nil:
|
|
return nil
|
|
case time.Time:
|
|
*s.t = value.UTC()
|
|
return nil
|
|
case []byte:
|
|
return s.parse(string(value))
|
|
case string:
|
|
return s.parse(value)
|
|
default:
|
|
return fmt.Errorf("store: cannot scan %T into time", v)
|
|
}
|
|
}
|
|
|
|
func (s sqliteTime) parse(v string) error {
|
|
if v == "" {
|
|
return nil
|
|
}
|
|
// RFC3339 first: rows written by other paths may carry a zone.
|
|
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, sqliteTimeLayout} {
|
|
if parsed, err := time.Parse(layout, v); err == nil {
|
|
*s.t = parsed.UTC()
|
|
return nil
|
|
}
|
|
}
|
|
return fmt.Errorf("store: unrecognised time %q", v)
|
|
}
|