// 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" _ "modernc.org/sqlite" ) // Task lifecycle statuses. Workflows move tasks between them; the // daemon owns queued→running claims and crash recovery. const ( StatusQueued = "queued" StatusRunning = "running" StatusWaitingHuman = "waiting_human" StatusPROpen = "pr_open" StatusMerged = "merged" StatusParked = "parked" StatusDead = "dead" StatusRejected = "rejected" StatusClosedWontDo = "closed_wont_do" ) type Task struct { ID int64 `json:"id"` Owner string `json:"owner"` Repo string `json:"repo"` IssueNumber int `json:"issue_number"` Title string `json:"title"` Body string `json:"body"` Labels string `json:"labels"` // comma-separated, as seen at enqueue time Status string `json:"status"` Workflow string `json:"workflow"` Stage string `json:"stage"` Branch string `json:"branch"` Plan string `json:"plan"` Notes string `json:"notes"` PRNumber int `json:"pr_number"` TokensUsed int `json:"tokens_used"` Iterations int `json:"iterations"` Attempt int `json:"attempt"` ParkReason string `json:"park_reason"` // RetryCount tracks how many times a parked task has been retried // (parking-to-queued transitions). When it reaches the configured // max_retries the daemon moves the task to StatusDead. RetryCount int `json:"retry_count"` // WatchCommentID: replies to the issue after this comment are the // human input a waiting_human task is blocked on. WatchCommentID int64 `json:"watch_comment_id"` } type Store struct{ db *sql.DB } // Open opens (creating if needed) the SQLite database and its schema. func Open(path string) (*Store, error) { if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return nil, err } db, err := sql.Open("sqlite", path+"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)") if err != nil { return nil, err } if _, err := db.Exec(schema + eventsSchema); err != nil { return nil, errors.Join(fmt.Errorf("store: init schema: %w", err), db.Close()) } // Additive migrations; "duplicate column" means already applied. if _, err := db.Exec(`ALTER TABLE tasks ADD COLUMN watch_comment_id INTEGER NOT NULL DEFAULT 0`); err != nil && !strings.Contains(err.Error(), "duplicate column") { return nil, errors.Join(fmt.Errorf("store: migrate: %w", err), db.Close()) } if _, err := db.Exec(`ALTER TABLE tasks ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0`); err != nil && !strings.Contains(err.Error(), "duplicate column") { return nil, errors.Join(fmt.Errorf("store: migrate: %w", err), db.Close()) } return &Store{db: db}, nil } 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 'queued', 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 '', 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() } // OpenTest opens an in-memory SQLite store suitable for tests. func OpenTest(t interface{ TempDir() string }) *Store { s, err := Open(filepath.Join(t.TempDir(), "test.db")) if err != nil { panic(err) } return s } // IncrementRetryCount atomically bumps retry_count by 1. Called after // a parked task is requeued by maybeRetryParked. func (s *Store) IncrementRetryCount(ctx context.Context, taskID int64) error { _, err := s.db.ExecContext(ctx, `UPDATE tasks SET retry_count = retry_count + 1, updated_at = datetime('now') WHERE id = ?`, taskID) return err } // EnqueueIssue inserts a new queued task for the issue; returns false if // the issue is already tracked (the idempotency key is owner/repo/number). func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int, title, body, labels string) (bool, error) { res, err := s.db.ExecContext(ctx, ` INSERT INTO tasks (owner, repo, issue_number, title, body, labels) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT(owner, repo, issue_number) DO NOTHING`, owner, repo, number, title, body, labels) if err != nil { return false, err } n, _ := res.RowsAffected() return n > 0, 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) (*Task, error) { row := s.db.QueryRowContext(ctx, ` UPDATE tasks SET status='running', attempt=attempt+1, updated_at=datetime('now') WHERE id = (SELECT id FROM tasks WHERE status='queued' 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`) t, err := scanTask(row) if errors.Is(err, sql.ErrNoRows) { return nil, nil } return t, err } func scanTask(row *sql.Row) (*Task, error) { var t 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) 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) (*Task, error) { row := s.db.QueryRowContext(ctx, ` UPDATE tasks SET status='running', attempt=attempt+1, updated_at=datetime('now') WHERE owner=? AND repo=? AND issue_number=? AND status='queued' 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`, owner, repo, number) t, err := scanTask(row) if errors.Is(err, sql.ErrNoRows) { return nil, nil } return t, err } // Transition moves a task to a new status, recording the change. func (s *Store) Transition(ctx context.Context, taskID int64, from, to, detail string) error { if _, err := s.db.ExecContext(ctx, `UPDATE tasks SET status=?, updated_at=datetime('now') WHERE id=?`, to, taskID); err != nil { return err } _, err := s.db.ExecContext(ctx, `INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`, taskID, from, to, clip(detail, 4000)) return err } // Update persists mutable task fields written by workflows. func (s *Store) Update(ctx context.Context, t *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) (*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 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 } // WaitingTasks returns tasks blocked on human input. func (s *Store) WaitingTasks(ctx context.Context) (tasks []Task, retErr error) { rows, err := s.db.QueryContext(ctx, ` SELECT id, owner, repo, issue_number, title, body, plan, workflow, watch_comment_id, status FROM tasks WHERE status='waiting_human'`) if err != nil { return nil, err } defer func() { retErr = errors.Join(retErr, rows.Close()) }() for rows.Next() { var t Task if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title, &t.Body, &t.Plan, &t.Workflow, &t.WatchCommentID, &t.Status); err != nil { return nil, err } tasks = append(tasks, t) } return tasks, rows.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). func (s *Store) Requeue(ctx context.Context, taskID int64, fromStatus, workflow string) error { if _, err := s.db.ExecContext(ctx, ` UPDATE tasks SET status='queued', workflow=CASE WHEN ?='' THEN workflow ELSE ? END, stage='', park_reason='', updated_at=datetime('now') WHERE id=?`, workflow, workflow, taskID); err != nil { return err } _, err := s.db.ExecContext(ctx, `INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, 'queued', ?)`, taskID, fromStatus, "requeued "+workflow) return err } // 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='queued', updated_at=datetime('now') WHERE status='running'`) if err != nil { return 0, err } return res.RowsAffected() } // OpenPRs returns tasks whose PR state should be reconciled with GitHub. func (s *Store) OpenPRs(ctx context.Context) (tasks []Task, retErr error) { rows, err := s.db.QueryContext(ctx, ` SELECT id, owner, repo, issue_number, pr_number, status FROM tasks WHERE status='pr_open'`) if err != nil { return nil, err } defer func() { retErr = errors.Join(retErr, rows.Close()) }() for rows.Next() { var t Task if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.PRNumber, &t.Status); err != nil { return nil, err } tasks = append(tasks, t) } return tasks, rows.Err() } // ClearTerminalTasks deletes tasks whose status is terminal (merged, parked, // rejected, closed_wont_do). Returns the number of rows removed. func (s *Store) ClearTerminalTasks(ctx context.Context) (int64, error) { res, err := s.db.ExecContext(ctx, `DELETE FROM tasks WHERE status IN ('merged','parked','rejected','closed_wont_do')`) if err != nil { return 0, err } return res.RowsAffected() } func clip(s string, n int) string { if len(s) <= n { return s } return s[:n] }