pub mod cluster;
pub(crate) mod control;
pub(crate) mod dependencies;
mod helpers;
pub(crate) mod jobs;
pub(crate) mod kv;
mod metrics;
pub(crate) mod replica;
pub(crate) mod tasks;
#[cfg(test)]
mod tests;
mod usage;
use anyhow::Context;
use serde::{Deserialize, Serialize};
use sqlx::SqliteConnection;
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions};
use std::collections::BTreeMap;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::path::Path;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
use tasks::{StoreRouteCmd, UpsertExternalCmd, apply};
pub use control::{ChatCostSummary, ControlMessage, MemoryEntry, parse_since_duration};
pub use dependencies::{
BlockerSnapshot, DepSource, NewDependency, ObservedPullRequest, TaskDependency,
WaitingOnDependency, blocker_failure_kind, blocker_is_satisfied, blocker_snapshot_key,
dep_label, worktree_refresh_key,
};
pub use helpers::{
StoreResult, get_recent_memory, get_recent_memory_result, get_reported_cost,
get_reported_cost_result, get_task_field, get_task_field_direct, get_token_usage,
get_token_usage_result, opt_store_get_task, opt_store_get_task_by_id, resolve_store_id,
store_increment, store_increment_by_id, store_log_activity, store_reset_counters,
store_reset_failure_counters, store_set, store_set_by_id, store_set_result,
store_set_result_by_id, store_touch_updated_at,
};
pub use jobs::JobState;
pub use metrics::{
AgentStat, CostByGroup, CostPeriod, CostSummary, ErrorStat, HighReviewCycleTask,
InsertTaskMetric, MetricsSummary, SlowTaskInfo,
};
use sqlx::error::DatabaseError;
pub use tasks::{
CliTaskRef, CliTaskSpec, CompleteRun, NewTask, RunTokenUsage, StartRun, StatsEvents,
StatusChange, StoreRoute, Task, TaskActivity, TaskRun, TaskStatus, UpsertExternal,
parse_cli_task_spec,
};
use tokio::time;
pub use usage::TokenUsage;
use crate::home;
use cluster::Node;
use cluster::local;
use cluster::local::Endpoint;
pub async fn default_db_path() -> anyhow::Result<PathBuf> {
home::db_path().await
}
#[derive(Clone)]
pub struct TaskStore {
pool: SqlitePool,
raft: Arc<OnceLock<Node>>,
daemon: Arc<OnceLock<Endpoint>>,
}
impl Debug for TaskStore {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("TaskStore").finish_non_exhaustive()
}
}
impl TaskStore {
pub async fn open(db_path: &Path) -> anyhow::Result<Self> {
let options = SqliteConnectOptions::new()
.filename(db_path)
.journal_mode(SqliteJournalMode::Wal)
.create_if_missing(true)
.busy_timeout(Duration::from_secs(5))
.pragma("foreign_keys", "ON");
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect_with(options)
.await
.with_context(|| format!("opening task store: {}", db_path.display()))?;
let store = Self {
pool,
raft: Arc::default(),
daemon: Arc::default(),
};
store.migrate().await?;
Ok(store)
}
pub async fn open_single(db_path: &Path) -> anyhow::Result<Self> {
let options = SqliteConnectOptions::new()
.filename(db_path)
.journal_mode(SqliteJournalMode::Wal)
.create_if_missing(true)
.busy_timeout(Duration::from_secs(5))
.pragma("foreign_keys", "ON");
let pool = SqlitePoolOptions::new()
.max_connections(1)
.connect_with(options)
.await
.with_context(|| format!("opening task store (single): {}", db_path.display()))?;
let store = Self {
pool,
raft: Arc::default(),
daemon: Arc::default(),
};
store.migrate().await?;
Ok(store)
}
#[cfg(test)]
pub async fn open_memory() -> anyhow::Result<Self> {
let options = SqliteConnectOptions::new()
.filename(":memory:")
.pragma("foreign_keys", "ON");
let pool = SqlitePoolOptions::new()
.max_connections(1)
.connect_with(options)
.await?;
let store = Self {
pool,
raft: Arc::default(),
daemon: Arc::default(),
};
store.migrate().await?;
Ok(store)
}
async fn migrate(&self) -> anyhow::Result<()> {
sqlx::migrate!("./migrations")
.run(&self.pool)
.await
.context("running task store migrations")?;
Ok(())
}
pub fn attach_node(&self, node: Node) {
if self.raft.set(node).is_err() {
tracing::warn!("raft already attached to the task store");
}
}
pub fn is_leader(&self) -> bool {
self.raft.get().is_none_or(cluster::Node::is_leader)
}
pub(crate) fn replicates(&self) -> bool {
self.raft.get().is_some() || self.daemon.get().is_some()
}
pub fn node(&self) -> Option<&Node> {
self.raft.get()
}
pub fn attach_daemon(&self, endpoint: Endpoint) {
let _ = self.daemon.set(endpoint);
}
pub(crate) async fn write(&self, cmd: Command) -> anyhow::Result<Reply> {
let Some(node) = self.raft.get() else {
if let Some(endpoint) = self.daemon.get() {
match local::send(endpoint, &cmd).await {
Ok(reply) => return reply_result(reply),
Err(e) if e.is_connect() => {
tracing::debug!(err = %e, "daemon not listening, writing directly");
}
Err(e) => return Err(e.into()),
}
}
return self.apply_direct(cmd).await;
};
let req = Request {
now: now_stamp(),
cmd,
};
reply_result(node.write(req).await?)
}
pub(crate) async fn write_int(&self, cmd: Command) -> anyhow::Result<i64> {
match self.write(cmd).await? {
Reply::Int(n) => Ok(n),
other => anyhow::bail!("expected an integer reply, got {other:?}"),
}
}
pub(crate) async fn write_bool(&self, cmd: Command) -> anyhow::Result<bool> {
match self.write(cmd).await? {
Reply::Bool(b) => Ok(b),
other => anyhow::bail!("expected a boolean reply, got {other:?}"),
}
}
pub(crate) async fn apply_direct(&self, cmd: Command) -> anyhow::Result<Reply> {
let mut attempt = 0;
loop {
match self.apply_once(cmd.clone()).await {
Err(e) if attempt < WRITE_LOCK_RETRIES && is_write_lock_error(&e) => {
attempt += 1;
tracing::warn!(attempt, err = %e, "database is locked, retrying the write");
time::sleep(Duration::from_millis(100 << (attempt - 1))).await;
}
result => return result,
}
}
}
async fn apply_once(&self, cmd: Command) -> anyhow::Result<Reply> {
let mut tx = self.begin_write().await?;
let reply = cmd.apply(&mut tx, &now_stamp(), false).await?;
tx.commit().await?;
Ok(reply)
}
pub(crate) async fn begin_write(
&self,
) -> sqlx::Result<sqlx::Transaction<'static, sqlx::Sqlite>> {
self.pool.begin_with("BEGIN IMMEDIATE").await
}
pub fn pool(&self) -> &SqlitePool {
&self.pool
}
}
const WRITE_LOCK_RETRIES: u32 = 4;
fn is_write_lock_error(err: &anyhow::Error) -> bool {
err.chain().any(|cause| {
cause
.downcast_ref::<sqlx::Error>()
.and_then(|e| e.as_database_error())
.and_then(DatabaseError::code)
.is_some_and(|code| code == "5" || code == "517")
})
}
fn reply_result(reply: Reply) -> anyhow::Result<Reply> {
match reply {
Reply::Err(e) => Err(anyhow::anyhow!(e)),
other => Ok(other),
}
}
impl TaskStore {
pub(crate) async fn snapshot_rows(
&self,
) -> anyhow::Result<(
BTreeMap<String, Vec<serde_json::Value>>,
Vec<serde_json::Value>,
Vec<kv::Row>,
)> {
let mut tables = BTreeMap::new();
for table in replica::TABLES {
tables.insert(
table.to_string(),
replica::table_rows(&self.pool, table).await?,
);
}
let tasks = tasks::replicated_rows(&self.pool).await?;
let kv = kv::replicated_rows(&self.pool).await?;
Ok((tables, tasks, kv))
}
}
type Updates = Vec<(String, serde_json::Value)>;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Request {
pub now: String,
pub cmd: Command,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum Command {
KvSet {
key: String,
value: String,
},
KvIncrement {
key: String,
},
KvInsertIfAbsent {
key: String,
value: String,
},
KvDelete {
key: String,
},
TaskCreate {
new: NewTask,
internal: bool,
},
TaskUpsertExternal(UpsertExternalCmd),
TaskUpdateStatusAndFields {
id: i64,
status: TaskStatus,
updates: Updates,
},
TaskUpdateStatus {
id: i64,
status: TaskStatus,
expected: Option<TaskStatus>,
},
TaskUpdateExternalId {
id: i64,
external_id: String,
},
TaskTouch {
id: i64,
},
TaskResetToNew {
id: i64,
#[serde(default)]
keep_branch: bool,
},
TaskSetFields {
id: i64,
updates: Updates,
silent: bool,
},
TaskIncrement {
id: i64,
field: String,
},
TaskResetCounters {
id: i64,
failure_only: bool,
},
TaskStoreRoute(StoreRouteCmd),
TaskSetEstimateIfZero {
id: i64,
estimate: u8,
},
TaskAppendMemory {
id: i64,
entry: MemoryEntry,
},
TaskMarkCleaned {
id: i64,
},
MoveRepo {
old: String,
new: String,
},
DepReplace {
task_id: i64,
rows: Vec<NewDependency>,
},
DepLinkKnown {
task_id: i64,
repo: String,
number: i64,
},
DepBlockClaim {
task_id: i64,
},
JobStateUpsert(JobState),
ChannelSubscribe {
channel: String,
thread_id: String,
repo: String,
topic_id: String,
},
ChannelUnsubscribe {
channel: String,
thread_id: String,
repo: String,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum Reply {
Done,
Bool(bool),
Int(i64),
Count(u64),
Value(String),
Unmet(Option<Vec<String>>),
Err(String),
}
impl Command {
pub(crate) fn check_replicable(&self) -> anyhow::Result<()> {
let key = match self {
Command::KvSet { key, .. }
| Command::KvIncrement { key }
| Command::KvInsertIfAbsent { key, .. }
| Command::KvDelete { key } => Some(key),
_ => None,
};
if let Some(key) = key {
anyhow::ensure!(
!kv::is_local(key),
"{key} is local to each node, never replicated"
);
}
let updates = match self {
Command::TaskSetFields { updates, .. }
| Command::TaskUpdateStatusAndFields { updates, .. } => &updates[..],
_ => &[],
};
if let Some((col, _)) = updates.iter().find(|(col, _)| tasks::is_local_column(col)) {
anyhow::bail!("{col} is local to each node, never replicated");
}
Ok(())
}
pub(crate) async fn apply(
self,
conn: &mut SqliteConnection,
now: &str,
replicated: bool,
) -> anyhow::Result<Reply> {
if replicated {
self.check_replicable()?;
}
Ok(match self {
Command::KvSet { key, value } => {
kv::set(conn, now, &key, &value).await?;
Reply::Done
}
Command::KvIncrement { key } => Reply::Count(kv::increment(conn, now, &key).await?),
Command::KvInsertIfAbsent { key, value } => {
Reply::Value(kv::insert_if_absent(conn, now, &key, &value).await?)
}
Command::KvDelete { key } => {
kv::delete(conn, &key).await?;
Reply::Done
}
Command::TaskCreate { new, internal } => {
Reply::Int(apply::create(conn, now, &new, internal, replicated).await?)
}
Command::TaskUpsertExternal(ext) => {
Reply::Int(apply::upsert_external(conn, now, &ext, replicated).await?)
}
Command::TaskUpdateStatusAndFields {
id,
status,
updates,
} => {
apply::update_status_and_fields(conn, now, id, status, &updates).await?;
Reply::Done
}
Command::TaskUpdateStatus {
id,
status,
expected,
} => Reply::Bool(apply::update_status(conn, now, id, status, expected).await?),
Command::TaskUpdateExternalId { id, external_id } => {
apply::update_external_id(conn, now, id, &external_id).await?;
Reply::Done
}
Command::TaskTouch { id } => {
apply::touch(conn, now, id).await?;
Reply::Done
}
Command::TaskResetToNew { id, keep_branch } => {
apply::reset_to_new(conn, now, id, keep_branch).await?;
Reply::Done
}
Command::TaskSetFields {
id,
updates,
silent,
} => {
apply::set_fields(conn, now, id, &updates, silent).await?;
Reply::Done
}
Command::TaskIncrement { id, field } => {
Reply::Int(apply::increment(conn, now, id, &field).await?)
}
Command::TaskResetCounters { id, failure_only } => {
apply::reset_counters(conn, now, id, failure_only).await?;
Reply::Done
}
Command::TaskStoreRoute(route) => {
apply::store_route(conn, now, &route).await?;
Reply::Done
}
Command::TaskSetEstimateIfZero { id, estimate } => {
Reply::Bool(apply::set_estimate_if_zero(conn, now, id, estimate).await?)
}
Command::TaskAppendMemory { id, entry } => {
apply::append_memory(conn, now, id, &entry).await?;
Reply::Done
}
Command::TaskMarkCleaned { id } => {
apply::mark_cleaned(conn, now, id).await?;
Reply::Done
}
Command::MoveRepo { old, new } => {
let moved = dependencies::apply::move_repo(conn, &old, &new).await?;
Reply::Int(i64::try_from(moved)?)
}
Command::DepReplace { task_id, rows } => {
dependencies::apply::replace_dependencies(conn, task_id, &rows).await?;
Reply::Done
}
Command::DepLinkKnown {
task_id,
repo,
number,
} => {
dependencies::apply::link_known_task(conn, task_id, &repo, number).await?;
Reply::Done
}
Command::DepBlockClaim { task_id } => {
Reply::Unmet(dependencies::apply::block_claim(conn, now, task_id).await?)
}
Command::JobStateUpsert(state) => {
jobs::apply::upsert_job_state(conn, &state).await?;
Reply::Done
}
Command::ChannelSubscribe {
channel,
thread_id,
repo,
topic_id,
} => {
jobs::apply::subscribe(conn, &channel, &thread_id, &repo, &topic_id).await?;
Reply::Done
}
Command::ChannelUnsubscribe {
channel,
thread_id,
repo,
} => {
jobs::apply::unsubscribe(conn, &channel, &thread_id, &repo).await?;
Reply::Done
}
})
}
}
pub fn now_stamp() -> String {
chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string()
}
pub type KvRow = kv::Row;
pub(crate) async fn put_kv(
conn: &mut SqliteConnection,
now: &str,
key: &str,
value: &str,
) -> anyhow::Result<()> {
kv::set(conn, now, key, value).await
}
pub(crate) async fn replace_snapshot_rows(
conn: &mut SqliteConnection,
tables: &BTreeMap<String, Vec<serde_json::Value>>,
task_rows: &[serde_json::Value],
kv_rows: &[KvRow],
) -> anyhow::Result<()> {
tasks::replace_replicated(conn, task_rows).await?;
for table in replica::TABLES {
let rows = tables.get(table).map(Vec::as_slice).unwrap_or_default();
replica::replace_table(conn, table, rows).await?;
}
kv::replace_replicated(conn, kv_rows).await
}