Skip to main content

orch/store/
mod.rs

1//! Unified task store — SQLite as single source of truth.
2//!
3//! `TaskStore` replaces the legacy `internal_tasks` table and GitHub labels as the
4//! authoritative task state. External backends (GitHub, Linear) become sync adapters
5//! that mirror state changes outward.
6//!
7//! Uses sqlx for async SQLite access with file-based migrations.
8//!
9//! All task state, metrics, KV, and rate limits flow through this module.
10//!
11//! # Safety Warning: Never Use `SELECT *`
12//!
13//! Using `SELECT *` with `sqlx-sqlite`'s prepared statement cache can cause **OOB
14//! (out-of-bounds) panics** when the schema changes (e.g., new migration adds a column).
15//!
16//! The issue occurs because:
17//! 1. SQLite caches prepared statements with column metadata at prepare time
18//! 2. When a migration adds a new column, `sqlite3_column_count()` returns the updated count
19//! 3. The cached statement's column metadata vector was built with the old count
20//! 4. Accessing column index N when only N columns exist (0..N-1) causes a panic
21//!
22//! **Solution:** Always use explicit column lists via constants like `TASK_COLS`,
23//! `CONTROL_MESSAGE_COLS`, `JOB_STATE_COLS`, etc. See `tasks.rs` and `control.rs`
24//! for examples. CI will fail if `SELECT *` is detected in store files.
25
26use anyhow::Context;
27use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions};
28use std::path::Path;
29
30mod control;
31mod dependencies;
32mod helpers;
33mod jobs;
34mod kv;
35mod metrics;
36mod pricing;
37mod tasks;
38
39#[allow(unused_imports)]
40pub use control::{parse_since_duration, ChatCostSummary, ControlMessage, MemoryEntry};
41#[allow(unused_imports)]
42pub use dependencies::{
43    blocker_failure_kind, blocker_is_satisfied, blocker_snapshot_key, dep_label,
44    worktree_refresh_key, BlockerSnapshot, DepSource, NewDependency, ObservedPullRequest,
45    TaskDependency, WaitingOnDependency,
46};
47#[allow(unused_imports)]
48pub use helpers::{
49    get_cost_estimate, get_cost_estimate_result, get_recent_memory, get_recent_memory_result,
50    get_task_field, get_task_field_direct, get_token_usage, get_token_usage_result,
51    opt_store_get_task, opt_store_get_task_by_id, resolve_store_id, review_session_expected,
52    set_review_session_expected, store_increment, store_increment_by_id, store_log_activity,
53    store_reset_counters, store_reset_failure_counters, store_set, store_set_by_id,
54    store_set_result, store_set_result_by_id, store_touch_updated_at, StoreResult,
55};
56#[allow(unused_imports)]
57pub use jobs::JobState;
58#[allow(unused_imports)]
59pub use metrics::{
60    AgentStat, CostByGroup, CostPeriod, CostSummary, ErrorStat, HighReviewCycleTask,
61    InsertTaskMetric, MetricsSummary, SlowTaskInfo,
62};
63#[allow(unused_imports)]
64pub use pricing::{pricing_for_model, CostEstimate, ModelPricing, TokenUsage};
65pub use tasks::{
66    parse_cli_task_spec, CliTaskRef, CliTaskSpec, CompleteRun, NewTask, RunTokenUsage, StartRun,
67    StoreRoute, Task, TaskActivity, TaskRun, TaskStatus, UpsertExternal,
68};
69
70/// Default database path: `~/.orch/orch.db`
71pub async fn default_db_path() -> anyhow::Result<std::path::PathBuf> {
72    crate::home::db_path().await
73}
74
75/// The unified task store backed by SQLite (via sqlx).
76#[derive(Clone)]
77pub struct TaskStore {
78    pool: SqlitePool,
79}
80
81impl TaskStore {
82    /// Open (or create) the store at the given database path.
83    ///
84    /// Runs file-based migrations from the `migrations/` directory.
85    pub async fn open(db_path: &Path) -> anyhow::Result<Self> {
86        let options = SqliteConnectOptions::new()
87            .filename(db_path)
88            .journal_mode(SqliteJournalMode::Wal)
89            .create_if_missing(true)
90            .busy_timeout(std::time::Duration::from_secs(5))
91            .pragma("foreign_keys", "ON");
92
93        let pool = SqlitePoolOptions::new()
94            .max_connections(5)
95            .connect_with(options)
96            .await
97            .with_context(|| format!("opening task store: {}", db_path.display()))?;
98
99        let store = Self { pool };
100        store.migrate().await?;
101        Ok(store)
102    }
103
104    /// Open a store with a single connection.
105    #[allow(dead_code)]
106    ///
107    /// Uses `max_connections(1)` to avoid WAL visibility issues where a write
108    /// on one pooled connection isn't visible to a read on another. Used by
109    /// integration tests that need read-your-own-writes consistency.
110    pub async fn open_single(db_path: &Path) -> anyhow::Result<Self> {
111        let options = SqliteConnectOptions::new()
112            .filename(db_path)
113            .journal_mode(SqliteJournalMode::Wal)
114            .create_if_missing(true)
115            .busy_timeout(std::time::Duration::from_secs(5))
116            .pragma("foreign_keys", "ON");
117
118        let pool = SqlitePoolOptions::new()
119            .max_connections(1)
120            .connect_with(options)
121            .await
122            .with_context(|| format!("opening task store (single): {}", db_path.display()))?;
123
124        let store = Self { pool };
125        store.migrate().await?;
126        Ok(store)
127    }
128
129    /// Open an in-memory store (for testing).
130    #[cfg(test)]
131    pub async fn open_memory() -> anyhow::Result<Self> {
132        let options = SqliteConnectOptions::new()
133            .filename(":memory:")
134            .pragma("foreign_keys", "ON");
135        let pool = SqlitePoolOptions::new()
136            .max_connections(1)
137            .connect_with(options)
138            .await?;
139
140        let store = Self { pool };
141        store.migrate().await?;
142        Ok(store)
143    }
144
145    /// Run migrations.
146    async fn migrate(&self) -> anyhow::Result<()> {
147        sqlx::migrate!("./migrations")
148            .run(&self.pool)
149            .await
150            .context("running task store migrations")?;
151        Ok(())
152    }
153
154    /// Get a reference to the underlying pool (for advanced queries).
155    #[allow(dead_code)]
156    pub fn pool(&self) -> &SqlitePool {
157        &self.pool
158    }
159}
160
161#[cfg(test)]
162mod tests;