use std::path::{Path, PathBuf};
use std::sync::Mutex;
use async_trait::async_trait;
use incurs_codemode::{ArtifactRef, ArtifactStore, ExecutionState, RuntimeStore, Snippet};
use rusqlite::{Connection, OptionalExtension, params};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use sha2::{Digest, Sha256};
use crate::environment::ScopedEnvironment;
use crate::model::{
AgentSession, ArtifactMetadata, CausalEvent, Execution, HistoryPatternOutput,
HistoryQueryOutput, InvocationPage, InvocationPattern, InvocationPatternQuery, InvocationQuery,
InvocationRecord, InvocationScopeKind, InvocationStatus, InvocationSummary, InvocationSurface,
InvocationTimelinePage, Lease, ManagedRun, OperationReceipt, PollIteration, ProcessContext,
ProcessController, ProcessDossier, ProcessEvent, ProcessEventQuery, ProcessInfo,
RecipeScopeKind, RecipeSuggestion, Reservation, now_ms,
};
use crate::pattern::invocation_pattern_signature;
use crate::recipe::{recipe_expired, recipe_generation, recipe_storage_key};
pub struct Store {
connection: Mutex<Connection>,
path: PathBuf,
}
pub enum ExecutionClaim {
Claimed,
Replayed(Box<Execution>),
}
const DATABASE_VERSION: u32 = 8;
impl Store {
pub fn open_default() -> Result<Self, String> {
Self::open(state_dir()?.join("apoc.sqlite3"))
}
pub fn open(path: impl AsRef<Path>) -> Result<Self, String> {
let path = path.as_ref().to_path_buf();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|error| error.to_string())?;
}
backup_before_migration(&path)?;
let connection = Connection::open(&path).map_err(|error| error.to_string())?;
let had_invocation_search = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE name IN (
'invocation_search',
'invocations_search_insert',
'invocations_search_delete',
'invocations_search_update'
)",
[],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?
== 4;
connection
.busy_timeout(std::time::Duration::from_secs(5))
.map_err(|error| error.to_string())?;
connection
.pragma_update(None, "journal_mode", "WAL")
.map_err(|error| error.to_string())?;
connection
.pragma_update(None, "foreign_keys", "ON")
.map_err(|error| error.to_string())?;
connection
.execute_batch(
"
CREATE TABLE IF NOT EXISTS runs (
id TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS receipts (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
value TEXT NOT NULL,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS executions (
id TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS snippets (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS artifacts (
id TEXT PRIMARY KEY,
execution_id TEXT NOT NULL,
value TEXT NOT NULL,
bytes INTEGER NOT NULL,
created_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS artifacts_execution
ON artifacts(execution_id);
CREATE TABLE IF NOT EXISTS recipes (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS environments (
scope TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS recipe_suggestions (
id TEXT PRIMARY KEY,
value TEXT NOT NULL,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS connectors (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS providers (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS services (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS execution_records (
id TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS execution_idempotency (
key TEXT PRIMARY KEY,
fingerprint TEXT NOT NULL,
execution_id TEXT NOT NULL UNIQUE,
created_at INTEGER NOT NULL,
FOREIGN KEY(execution_id) REFERENCES execution_records(id)
);
CREATE TABLE IF NOT EXISTS poll_iterations (
id TEXT PRIMARY KEY,
poll_id TEXT NOT NULL,
number INTEGER NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL,
UNIQUE(poll_id, number),
FOREIGN KEY(poll_id) REFERENCES execution_records(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS poll_iterations_page
ON poll_iterations(poll_id, number DESC);
CREATE TABLE IF NOT EXISTS invocations (
id TEXT PRIMARY KEY,
parent_id TEXT,
execution_id TEXT,
source TEXT NOT NULL,
source_id TEXT,
source_order INTEGER,
source_path TEXT,
accepted_sequence INTEGER NOT NULL DEFAULT 0,
surface TEXT NOT NULL,
actor TEXT,
agent INTEGER NOT NULL,
command_path TEXT NOT NULL,
purpose TEXT,
normalized_input TEXT NOT NULL,
search_text TEXT NOT NULL,
pattern_signature TEXT NOT NULL DEFAULT '',
variable_paths TEXT NOT NULL DEFAULT '[]',
cwd TEXT,
scope_kind TEXT NOT NULL,
scope_key TEXT,
started_at INTEGER NOT NULL,
completed_at INTEGER,
updated_at INTEGER NOT NULL,
status TEXT NOT NULL,
exit_code INTEGER,
error_code TEXT,
error TEXT,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS invocations_recent
ON invocations(updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_scope
ON invocations(scope_kind, scope_key, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_source
ON invocations(source, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_surface
ON invocations(surface, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_command
ON invocations(command_path, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_status
ON invocations(status, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_actor
ON invocations(actor, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_execution
ON invocations(execution_id, updated_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS invocations_started
ON invocations(started_at DESC, id DESC);
CREATE VIRTUAL TABLE IF NOT EXISTS invocation_search USING fts5(
search_text,
content = 'invocations',
content_rowid = 'rowid',
tokenize = 'trigram'
);
CREATE TRIGGER IF NOT EXISTS invocations_search_insert AFTER INSERT ON invocations
BEGIN
INSERT INTO invocation_search(rowid, search_text)
VALUES (new.rowid, new.search_text);
END;
CREATE TRIGGER IF NOT EXISTS invocations_search_delete AFTER DELETE ON invocations
BEGIN
INSERT INTO invocation_search(invocation_search, rowid, search_text)
VALUES ('delete', old.rowid, old.search_text);
END;
CREATE TRIGGER IF NOT EXISTS invocations_search_update AFTER UPDATE ON invocations
BEGIN
INSERT INTO invocation_search(invocation_search, rowid, search_text)
VALUES ('delete', old.rowid, old.search_text);
INSERT INTO invocation_search(rowid, search_text)
VALUES (new.rowid, new.search_text);
END;
CREATE TABLE IF NOT EXISTS agent_sessions (
id TEXT PRIMARY KEY,
actor TEXT NOT NULL,
expires_at INTEGER NOT NULL,
closed INTEGER NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS agent_sessions_actor
ON agent_sessions(actor, updated_at DESC);
CREATE TABLE IF NOT EXISTS leases (
id TEXT PRIMARY KEY,
owner_kind TEXT NOT NULL,
owner_id TEXT NOT NULL,
expires_at INTEGER,
released INTEGER NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS leases_owner
ON leases(owner_kind, owner_id, released);
CREATE TABLE IF NOT EXISTS reservations (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
key TEXT NOT NULL,
lease_id TEXT NOT NULL,
expires_at INTEGER,
released INTEGER NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS reservations_active_key
ON reservations(kind, key) WHERE released = 0;
CREATE INDEX IF NOT EXISTS reservations_lease
ON reservations(lease_id, released);
CREATE TABLE IF NOT EXISTS operation_receipts (
id TEXT PRIMARY KEY,
idempotency_key TEXT NOT NULL UNIQUE,
operation TEXT NOT NULL,
state TEXT NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS causal_payloads (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
execution_id TEXT,
value TEXT,
bytes INTEGER NOT NULL,
created_at INTEGER NOT NULL,
accessed_at INTEGER NOT NULL,
pinned INTEGER NOT NULL,
pruned INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS causal_payloads_retention
ON causal_payloads(pinned, pruned, accessed_at);
CREATE TABLE IF NOT EXISTS causal_events (
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
at INTEGER NOT NULL,
kind TEXT NOT NULL,
subject TEXT NOT NULL,
actor TEXT NOT NULL,
correlation_id TEXT NOT NULL,
causation_id TEXT,
execution_id TEXT,
session_id TEXT,
payload_id TEXT,
details TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS causal_events_subject
ON causal_events(subject, sequence DESC);
CREATE INDEX IF NOT EXISTS causal_events_execution
ON causal_events(execution_id, sequence DESC);
CREATE INDEX IF NOT EXISTS causal_events_session
ON causal_events(session_id, sequence DESC);
CREATE TABLE IF NOT EXISTS process_contexts (
key TEXT PRIMARY KEY,
pid INTEGER NOT NULL,
started_at INTEGER NOT NULL,
first_observed_at INTEGER NOT NULL,
last_observed_at INTEGER NOT NULL,
observations INTEGER NOT NULL,
origin TEXT NOT NULL,
run_id TEXT,
snapshot_id TEXT,
latest TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS process_contexts_pid
ON process_contexts(pid, started_at DESC);
CREATE INDEX IF NOT EXISTS process_contexts_recent
ON process_contexts(last_observed_at DESC);
CREATE TABLE IF NOT EXISTS process_events (
id TEXT PRIMARY KEY,
process_key TEXT NOT NULL,
at INTEGER NOT NULL,
operation TEXT NOT NULL,
phase TEXT NOT NULL,
actor TEXT NOT NULL,
execution_id TEXT,
run_id TEXT,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS process_events_process
ON process_events(process_key, at DESC);
CREATE INDEX IF NOT EXISTS process_events_recent
ON process_events(at DESC);
",
)
.map_err(|error| error.to_string())?;
if !had_invocation_search {
connection
.execute(
"INSERT INTO invocation_search(invocation_search) VALUES ('rebuild')",
[],
)
.map_err(|error| error.to_string())?;
}
connection
.execute(
"INSERT OR IGNORE INTO providers(name, value, updated_at)
SELECT name, value, updated_at FROM connectors",
[],
)
.map_err(|error| error.to_string())?;
migrate_legacy_recipes(&connection)?;
let added_execution = ensure_column(&connection, "process_events", "execution_id", "TEXT")?;
let added_run = ensure_column(&connection, "process_events", "run_id", "TEXT")?;
ensure_column(&connection, "process_contexts", "snapshot_id", "TEXT")?;
let added_pattern = ensure_column(
&connection,
"invocations",
"pattern_signature",
"TEXT NOT NULL DEFAULT ''",
)?;
let added_variable_paths = ensure_column(
&connection,
"invocations",
"variable_paths",
"TEXT NOT NULL DEFAULT '[]'",
)?;
let added_accepted_sequence = ensure_column(
&connection,
"invocations",
"accepted_sequence",
"INTEGER NOT NULL DEFAULT 0",
)?;
let added_purpose = ensure_column(&connection, "invocations", "purpose", "TEXT")?;
connection
.execute(
"CREATE INDEX IF NOT EXISTS invocations_pattern
ON invocations(pattern_signature, command_path, started_at DESC)",
[],
)
.map_err(|error| error.to_string())?;
if added_pattern || added_variable_paths {
backfill_invocation_patterns(&connection)?;
}
if added_accepted_sequence {
backfill_invocation_sequences(&connection)?;
}
if added_purpose {
backfill_invocation_purposes(&connection)?;
}
connection
.execute(
"CREATE INDEX IF NOT EXISTS invocations_timeline
ON invocations(started_at, accepted_sequence, id)",
[],
)
.map_err(|error| error.to_string())?;
if added_execution || added_run {
connection
.execute(
"UPDATE process_events SET
execution_id = json_extract(value, '$.controller.execution_id'),
run_id = json_extract(value, '$.run_id')
WHERE execution_id IS NULL OR run_id IS NULL",
[],
)
.map_err(|error| error.to_string())?;
}
connection
.execute_batch(
"
CREATE INDEX IF NOT EXISTS process_events_operation
ON process_events(operation, at DESC);
CREATE INDEX IF NOT EXISTS process_events_actor
ON process_events(actor, at DESC);
CREATE INDEX IF NOT EXISTS process_events_execution
ON process_events(execution_id, at DESC);
CREATE INDEX IF NOT EXISTS process_events_run
ON process_events(run_id, at DESC);
",
)
.map_err(|error| error.to_string())?;
connection
.pragma_update(None, "user_version", DATABASE_VERSION)
.map_err(|error| error.to_string())?;
Ok(Self {
connection: Mutex::new(connection),
path,
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn put_run(&self, run: &ManagedRun) -> Result<(), String> {
self.put_named("runs", "id", &run.id, run)
}
pub fn get_run(&self, id: &str) -> Result<Option<ManagedRun>, String> {
self.get_named("runs", "id", id)
}
pub fn list_runs(&self) -> Result<Vec<ManagedRun>, String> {
self.list_values("runs", "updated_at DESC")
}
pub fn delete_run(&self, id: &str) -> Result<bool, String> {
self.delete_named("runs", "id", id)
}
pub fn put_receipt(&self, id: &str, kind: &str, value: &impl Serialize) -> Result<(), String> {
let value = serde_json::to_string(value).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR REPLACE INTO receipts(id, kind, value, created_at)
VALUES (?1, ?2, ?3, ?4)",
params![id, kind, value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn get_receipt<T: DeserializeOwned>(&self, id: &str) -> Result<Option<T>, String> {
self.get_json_row("SELECT value FROM receipts WHERE id = ?1", id)
}
pub fn put_recipe(&self, name: &str, value: &Value) -> Result<(), String> {
self.put_named("recipes", "name", name, value)
}
pub fn get_recipe(&self, name: &str) -> Result<Option<Value>, String> {
self.get_named("recipes", "name", name)
}
pub fn list_recipes(&self) -> Result<Vec<Value>, String> {
self.list_values("recipes", "name ASC")
}
pub fn delete_recipe(&self, name: &str) -> Result<bool, String> {
self.delete_named("recipes", "name", name)
}
pub(crate) fn put_environment(&self, value: &ScopedEnvironment) -> Result<(), String> {
let key = recipe_storage_key(value.scope_kind, value.scope_key.as_deref(), "__env__")?;
self.put_named("environments", "scope", &key, value)
}
pub(crate) fn get_environment(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
) -> Result<Option<ScopedEnvironment>, String> {
let key = recipe_storage_key(scope_kind, scope_key, "__env__")?;
self.get_named("environments", "scope", &key)
}
pub(crate) fn delete_environment(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
) -> Result<bool, String> {
let key = recipe_storage_key(scope_kind, scope_key, "__env__")?;
self.delete_named("environments", "scope", &key)
}
pub fn put_recipe_suggestion(&self, suggestion: &RecipeSuggestion) -> Result<(), String> {
let value = serde_json::to_string(suggestion).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO recipe_suggestions(id, value, created_at) VALUES (?1, ?2, ?3)",
params![suggestion.id, value, as_i64(suggestion.created_at)],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn get_recipe_suggestion(&self, id: &str) -> Result<Option<RecipeSuggestion>, String> {
self.get_json_row("SELECT value FROM recipe_suggestions WHERE id = ?1", id)
}
pub fn list_recipe_suggestions(&self) -> Result<Vec<RecipeSuggestion>, String> {
self.list_values("recipe_suggestions", "created_at DESC, id DESC")
}
pub fn adopt_recipe_suggestion(
&self,
recipe: &crate::model::Recipe,
suggestion: &RecipeSuggestion,
) -> Result<(), String> {
let recipe_key =
recipe_storage_key(recipe.scope_kind, recipe.scope_key.as_deref(), &recipe.name)?;
let recipe_value = serde_json::to_string(recipe).map_err(|error| error.to_string())?;
let suggestion_value =
serde_json::to_string(suggestion).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT OR REPLACE INTO recipes(name, value, updated_at) VALUES (?1, ?2, ?3)",
params![recipe_key, recipe_value, as_i64(recipe.updated_at)],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"UPDATE recipe_suggestions SET value = ?2 WHERE id = ?1",
params![suggestion.id, suggestion_value],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())
}
pub fn put_recipe_definition(&self, recipe: &crate::model::Recipe) -> Result<(), String> {
let key = recipe_storage_key(recipe.scope_kind, recipe.scope_key.as_deref(), &recipe.name)?;
self.put_named("recipes", "name", &key, recipe)
}
pub fn put_recipe_definitions(&self, recipes: &[crate::model::Recipe]) -> Result<(), String> {
let values = recipes
.iter()
.map(|recipe| {
Ok((
recipe_storage_key(
recipe.scope_kind,
recipe.scope_key.as_deref(),
&recipe.name,
)?,
serde_json::to_string(recipe).map_err(|error| error.to_string())?,
as_i64(recipe.updated_at),
))
})
.collect::<Result<Vec<_>, String>>()?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
for (key, value, updated_at) in values {
transaction
.execute(
"INSERT OR REPLACE INTO recipes(name, value, updated_at) VALUES (?1, ?2, ?3)",
params![key, value, updated_at],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())
}
pub fn get_recipe_definition(
&self,
scope_kind: crate::model::RecipeScopeKind,
scope_key: Option<&str>,
name: &str,
) -> Result<Option<crate::model::Recipe>, String> {
let key = recipe_storage_key(scope_kind, scope_key, name)?;
self.get_named("recipes", "name", &key)
}
pub fn list_recipe_definitions(
&self,
scope_kind: crate::model::RecipeScopeKind,
scope_key: Option<&str>,
include_expired: bool,
prefix: Option<&str>,
) -> Result<Vec<crate::model::Recipe>, String> {
let now = now_ms();
let prefix = recipe_storage_key(scope_kind, scope_key, prefix.unwrap_or_default())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM recipes
WHERE name LIKE (?1 || '%') ORDER BY updated_at DESC, name",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([prefix], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
let mut recipes = deserialize_rows::<crate::model::Recipe>(rows)?;
if !include_expired {
recipes.retain(|recipe| !recipe_expired(recipe, now));
}
Ok(recipes)
}
pub fn delete_recipe_definition(
&self,
scope_kind: crate::model::RecipeScopeKind,
scope_key: Option<&str>,
name: &str,
) -> Result<bool, String> {
let key = recipe_storage_key(scope_kind, scope_key, name)?;
self.delete_named("recipes", "name", &key)
}
pub fn prune_recipe_definitions(
&self,
scope_kind: crate::model::RecipeScopeKind,
scope_key: Option<&str>,
) -> Result<Vec<String>, String> {
let expired = self
.list_recipe_definitions(scope_kind, scope_key, true, None)?
.into_iter()
.filter(|recipe| recipe_expired(recipe, now_ms()))
.collect::<Vec<_>>();
let mut removed = Vec::with_capacity(expired.len());
for recipe in expired {
if self.delete_recipe_definition(scope_kind, scope_key, &recipe.name)? {
removed.push(recipe.name);
}
}
Ok(removed)
}
pub fn put_connector(&self, name: &str, value: &Value) -> Result<(), String> {
self.put_provider(name, value)
}
pub fn get_connector(&self, name: &str) -> Result<Option<Value>, String> {
self.get_provider(name)?.map_or_else(
|| self.get_named("connectors", "name", name),
|value| Ok(Some(value)),
)
}
pub fn list_connectors(&self) -> Result<Vec<Value>, String> {
self.list_providers()
}
pub fn delete_connector(&self, name: &str) -> Result<bool, String> {
self.delete_provider(name)
}
pub fn put_provider(&self, name: &str, value: &Value) -> Result<(), String> {
self.put_named("providers", "name", name, value)
}
pub fn get_provider(&self, name: &str) -> Result<Option<Value>, String> {
self.get_named("providers", "name", name)
}
pub fn list_providers(&self) -> Result<Vec<Value>, String> {
self.list_values("providers", "name ASC")
}
pub fn delete_provider(&self, name: &str) -> Result<bool, String> {
let removed = self.delete_named("providers", "name", name)?;
let _ = self.delete_named("connectors", "name", name)?;
Ok(removed)
}
pub fn put_service(&self, name: &str, value: &crate::model::Service) -> Result<(), String> {
self.put_named("services", "name", name, value)
}
pub fn get_service(&self, name: &str) -> Result<Option<crate::model::Service>, String> {
self.get_named("services", "name", name)
}
pub fn list_services(&self) -> Result<Vec<crate::model::Service>, String> {
self.list_values("services", "name ASC")
}
pub fn delete_service(&self, name: &str) -> Result<bool, String> {
self.delete_named("services", "name", name)
}
pub fn put_execution_record(&self, execution: &Execution) -> Result<(), String> {
let value = serde_json::to_string(execution).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO execution_records(id, value, updated_at) VALUES (?1, ?2, ?3)
ON CONFLICT(id) DO UPDATE SET
value = excluded.value,
updated_at = excluded.updated_at",
params![execution.id, value, execution.updated_at as i64],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn claim_execution(
&self,
execution: &Execution,
key: &str,
fingerprint: &str,
) -> Result<ExecutionClaim, String> {
let value = serde_json::to_string(execution).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
let existing = transaction
.query_row(
"SELECT fingerprint, execution_id FROM execution_idempotency WHERE key = ?1",
[key],
|row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
)
.optional()
.map_err(|error| error.to_string())?;
if let Some((existing_fingerprint, execution_id)) = existing {
if existing_fingerprint != fingerprint {
return Err(format!(
"EXECUTION_IDEMPOTENCY_CONFLICT: key \"{key}\" already identifies a different execution request"
));
}
let existing = transaction
.query_row(
"SELECT value FROM execution_records WHERE id = ?1",
[&execution_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.ok_or_else(|| {
format!(
"EXECUTION_IDEMPOTENCY_CORRUPT: key \"{key}\" references missing execution \"{execution_id}\""
)
})?;
let existing = serde_json::from_str(&existing).map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
return Ok(ExecutionClaim::Replayed(Box::new(existing)));
}
transaction
.execute(
"INSERT INTO execution_records(id, value, updated_at) VALUES (?1, ?2, ?3)",
params![execution.id, value, execution.updated_at as i64],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO execution_idempotency(key, fingerprint, execution_id, created_at)
VALUES (?1, ?2, ?3, ?4)",
params![key, fingerprint, execution.id, execution.created_at as i64],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(ExecutionClaim::Claimed)
}
pub fn get_execution_record(&self, id: &str) -> Result<Option<Execution>, String> {
self.get_named("execution_records", "id", id)
}
pub fn put_poll_iteration(&self, iteration: &PollIteration) -> Result<(), String> {
let value = serde_json::to_string(iteration).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO poll_iterations(id, poll_id, number, value, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5)
ON CONFLICT(id) DO UPDATE SET
poll_id = excluded.poll_id,
number = excluded.number,
value = excluded.value,
updated_at = excluded.updated_at",
params![
iteration.id,
iteration.poll_id,
iteration.number.min(i64::MAX as u64) as i64,
value,
iteration
.completed_at
.unwrap_or(iteration.started_at)
.min(i64::MAX as u64) as i64
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn get_poll_iteration(
&self,
poll_id: &str,
id: &str,
) -> Result<Option<PollIteration>, String> {
let value = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM poll_iterations WHERE poll_id = ?1 AND id = ?2",
params![poll_id, id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
value
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn get_poll_iteration_number(
&self,
poll_id: &str,
number: u64,
) -> Result<Option<PollIteration>, String> {
let value = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM poll_iterations WHERE poll_id = ?1 AND number = ?2",
params![poll_id, number.min(i64::MAX as u64) as i64],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
value
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn list_poll_iterations(
&self,
poll_id: &str,
before: Option<u64>,
limit: usize,
) -> Result<Vec<PollIteration>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM poll_iterations
WHERE poll_id = ?1 AND (?2 IS NULL OR number < ?2)
ORDER BY number DESC LIMIT ?3",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![
poll_id,
before.map(|value| value.min(i64::MAX as u64) as i64),
limit.min(i64::MAX as usize) as i64
],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn list_execution_records(&self) -> Result<Vec<Execution>, String> {
self.list_values("execution_records", "updated_at DESC")
}
pub fn insert_invocation(&self, invocation: &InvocationRecord) -> Result<(), String> {
let mut invocation = invocation.clone();
let connection = self.connection.lock().map_err(|error| error.to_string())?;
if invocation.accepted_sequence == 0 {
invocation.accepted_sequence = connection
.query_row(
"SELECT accepted_sequence FROM invocations WHERE id = ?1",
[&invocation.id],
|row| row.get::<_, i64>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| value.max(0) as u64)
.unwrap_or(
connection
.query_row(
"SELECT COALESCE(MAX(accepted_sequence), 0) + 1 FROM invocations",
[],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?
.max(1) as u64,
);
}
let pattern =
invocation_pattern_signature(&invocation.command_path, &invocation.normalized_input);
invocation.pattern_signature = pattern.signature;
invocation.variable_paths = pattern.variable_paths;
let value = serde_json::to_string(&invocation).map_err(|error| error.to_string())?;
let normalized_input = serde_json::to_string(&invocation.normalized_input)
.map_err(|error| error.to_string())?;
let variable_paths =
serde_json::to_string(&invocation.variable_paths).map_err(|error| error.to_string())?;
connection.execute(
"INSERT INTO invocations(
id, parent_id, execution_id, source, source_id, source_order, source_path,
accepted_sequence, surface, actor, agent, command_path, purpose, normalized_input, search_text,
pattern_signature, variable_paths, cwd, scope_kind, scope_key, started_at,
completed_at, updated_at, status, exit_code, error_code, error, value
) VALUES (
?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14,
?15, ?16, ?17, ?18, ?19, ?20, ?21, ?22, ?23, ?24, ?25, ?26, ?27, ?28
)
ON CONFLICT(id) DO UPDATE SET
parent_id = excluded.parent_id,
execution_id = excluded.execution_id,
source = excluded.source,
source_id = excluded.source_id,
source_order = excluded.source_order,
source_path = excluded.source_path,
accepted_sequence = excluded.accepted_sequence,
surface = excluded.surface,
actor = excluded.actor,
agent = excluded.agent,
command_path = excluded.command_path,
purpose = excluded.purpose,
normalized_input = excluded.normalized_input,
search_text = excluded.search_text,
pattern_signature = excluded.pattern_signature,
variable_paths = excluded.variable_paths,
cwd = excluded.cwd,
scope_kind = excluded.scope_kind,
scope_key = excluded.scope_key,
started_at = excluded.started_at,
completed_at = excluded.completed_at,
updated_at = excluded.updated_at,
status = excluded.status,
exit_code = excluded.exit_code,
error_code = excluded.error_code,
error = excluded.error,
value = excluded.value",
params![
invocation.id,
invocation.parent_id,
invocation.execution_id,
enum_value(&invocation.source)?,
invocation.source_id,
invocation.source_order,
invocation.source_path,
as_i64(invocation.accepted_sequence),
enum_value(&invocation.surface)?,
invocation.actor,
invocation.agent,
invocation.command_path,
invocation.purpose,
normalized_input,
invocation.search_text,
invocation.pattern_signature,
variable_paths,
invocation.cwd,
enum_value(&invocation.scope_kind)?,
invocation.scope_key,
as_i64(invocation.started_at),
invocation.completed_at.map(as_i64),
as_i64(invocation.updated_at),
enum_value(&invocation.status)?,
invocation.exit_code,
invocation.error_code,
invocation.error,
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn start_invocation(&self, invocation: &InvocationRecord) -> Result<(), String> {
let mut invocation = invocation.clone();
invocation.status = InvocationStatus::Running;
invocation.completed_at = None;
self.insert_invocation(&invocation)
}
pub fn finish_invocation(
&self,
id: &str,
status: InvocationStatus,
completed_at: Option<u64>,
exit_code: Option<i32>,
error_code: Option<String>,
error: Option<String>,
) -> Result<Option<InvocationRecord>, String> {
let Some(mut invocation) = self.get_invocation(id)? else {
return Ok(None);
};
let completed_at = completed_at.unwrap_or_else(now_ms);
invocation.status = status;
invocation.completed_at = Some(completed_at);
invocation.updated_at = completed_at;
invocation.exit_code = exit_code;
invocation.error_code = error_code;
invocation.error = error;
self.insert_invocation(&invocation)?;
Ok(Some(invocation))
}
pub fn update_invocation_input(
&self,
id: &str,
purpose: Option<String>,
normalized_input: serde_json::Value,
search_text: String,
) -> Result<Option<InvocationRecord>, String> {
let Some(mut invocation) = self.get_invocation(id)? else {
return Ok(None);
};
invocation.purpose = purpose;
invocation.normalized_input = normalized_input;
invocation.search_text = search_text;
invocation.updated_at = now_ms();
self.insert_invocation(&invocation)?;
Ok(Some(invocation))
}
pub fn link_invocation_execution(
&self,
id: &str,
execution_id: &str,
) -> Result<Option<InvocationRecord>, String> {
let Some(mut invocation) = self.get_invocation(id)? else {
return Ok(None);
};
invocation.execution_id = Some(execution_id.to_string());
self.insert_invocation(&invocation)?;
let children = {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM invocations
WHERE execution_id = ?1 AND surface = ?2 AND parent_id IS NULL AND id != ?3",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![execution_id, enum_value(&InvocationSurface::Code)?, id],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
deserialize_rows::<InvocationRecord>(rows)?
};
for mut child in children {
child.parent_id = Some(id.to_string());
self.insert_invocation(&child)?;
}
Ok(Some(invocation))
}
pub fn get_invocation(&self, id: &str) -> Result<Option<InvocationRecord>, String> {
self.get_json_row("SELECT value FROM invocations WHERE id = ?1", id)
}
pub fn invocation_evidence_range(
&self,
start_id: &str,
end_id: &str,
scope_kind: crate::model::InvocationScopeKind,
scope_key: Option<&str>,
) -> Result<Vec<String>, String> {
let start = self
.get_invocation(start_id)?
.ok_or_else(|| format!("Evidence invocation \"{start_id}\" not found"))?;
let end = self
.get_invocation(end_id)?
.ok_or_else(|| format!("Evidence invocation \"{end_id}\" not found"))?;
if start.scope_kind != scope_kind
|| end.scope_kind != scope_kind
|| start.scope_key.as_deref() != scope_key
|| end.scope_key.as_deref() != scope_key
{
return Err("Recipe evidence must remain within the selected scope".to_string());
}
let start_key = (start.started_at, start.accepted_sequence, start.id.as_str());
let end_key = (end.started_at, end.accepted_sequence, end.id.as_str());
if start_key > end_key {
return Err("Recipe evidence range must be oldest-first".to_string());
}
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT id FROM invocations
WHERE scope_kind = ?1 AND scope_key IS ?2
AND (started_at > ?3 OR (started_at = ?3 AND accepted_sequence > ?4)
OR (started_at = ?3 AND accepted_sequence = ?4 AND id >= ?5))
AND (started_at < ?6 OR (started_at = ?6 AND accepted_sequence < ?7)
OR (started_at = ?6 AND accepted_sequence = ?7 AND id <= ?8))
ORDER BY started_at, accepted_sequence, id",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![
enum_value(&scope_kind)?,
scope_key,
as_i64(start.started_at),
as_i64(start.accepted_sequence),
start.id,
as_i64(end.started_at),
as_i64(end.accepted_sequence),
end.id,
],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
pub fn query_invocations(&self, query: &InvocationQuery) -> Result<InvocationPage, String> {
let limit = if query.limit == 0 {
50
} else {
query.limit.min(100)
};
let cursor = query
.cursor
.as_deref()
.map(parse_invocation_cursor)
.transpose()?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let indexed_search = query
.search
.as_deref()
.filter(|value| value.chars().count() >= 3);
let mut sql = if indexed_search.is_some() {
"SELECT invocations.value FROM invocations
JOIN invocation_search ON invocation_search.rowid = invocations.rowid
WHERE invocation_search MATCH ?"
.to_string()
} else {
"SELECT value FROM invocations WHERE 1 = 1".to_string()
};
let mut values = Vec::<Box<dyn rusqlite::ToSql>>::new();
if let Some(value) = indexed_search {
values.push(Box::new(fts_literal(value)));
}
if let Some(value) = &query.scope_kind {
sql.push_str(" AND scope_kind = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.scope_key {
sql.push_str(" AND scope_key = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.source {
sql.push_str(" AND source = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.surface {
sql.push_str(" AND surface = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.command_path {
sql.push_str(" AND command_path = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.status {
sql.push_str(" AND status = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.actor {
sql.push_str(" AND actor = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.execution_id {
sql.push_str(" AND execution_id = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = query.since_ms {
sql.push_str(" AND started_at >= ?");
values.push(Box::new(as_i64(value)));
}
if let Some(value) = query.until_ms {
sql.push_str(" AND started_at <= ?");
values.push(Box::new(as_i64(value)));
}
if let Some(value) = &query.exclude_id {
sql.push_str(" AND id != ?");
values.push(Box::new(value.clone()));
}
if indexed_search.is_none()
&& let Some(value) = &query.search
{
sql.push_str(" AND instr(lower(search_text), lower(?)) > 0");
values.push(Box::new(value.clone()));
}
if let Some((updated_at, id)) = cursor {
sql.push_str(" AND (updated_at < ? OR (updated_at = ? AND id < ?))");
values.push(Box::new(as_i64(updated_at)));
values.push(Box::new(as_i64(updated_at)));
values.push(Box::new(id));
}
sql.push_str(" ORDER BY updated_at DESC, id DESC LIMIT ?");
values.push(Box::new((limit + 1).min(i64::MAX as usize) as i64));
let mut statement = connection
.prepare(&sql)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
rusqlite::params_from_iter(values.iter().map(|value| value.as_ref())),
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
let records = deserialize_rows::<InvocationRecord>(rows)?;
let has_more = records.len() > limit;
let invocations = records
.into_iter()
.take(limit)
.map(|invocation| InvocationSummary {
id: invocation.id,
parent_id: invocation.parent_id,
execution_id: invocation.execution_id,
source: invocation.source,
surface: invocation.surface,
actor: invocation.actor,
agent: invocation.agent,
command_path: invocation.command_path,
purpose: invocation.purpose,
cwd: invocation.cwd,
scope_kind: invocation.scope_kind,
scope_key: invocation.scope_key,
started_at: invocation.started_at,
completed_at: invocation.completed_at,
updated_at: invocation.updated_at,
status: invocation.status,
exit_code: invocation.exit_code,
error_code: invocation.error_code,
})
.collect::<Vec<_>>();
let next_cursor = has_more.then(|| {
let invocation = invocations
.last()
.expect("bounded page contains a cursor item");
format!("{}:{}", invocation.updated_at, invocation.id)
});
Ok(InvocationPage {
invocations,
next_cursor,
})
}
pub fn query_invocation_purposes_sql(
&self,
sql: &str,
limit: usize,
scope_kind: Option<InvocationScopeKind>,
scope_key: Option<String>,
exclude_id: Option<String>,
) -> Result<HistoryQueryOutput, String> {
let limit = if limit == 0 { 20 } else { limit.min(100) };
let history = {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut query = "SELECT id, purpose, started_at, updated_at, status, command_path,
source, surface, actor, execution_id, error_code
FROM invocations
WHERE purpose IS NOT NULL AND trim(purpose) != ''"
.to_string();
let mut values = Vec::<Box<dyn rusqlite::ToSql>>::new();
if let Some(scope_kind) = scope_kind {
query.push_str(" AND scope_kind = ?");
values.push(Box::new(enum_value(&scope_kind)?));
if let Some(scope_key) = scope_key {
query.push_str(" AND scope_key = ?");
values.push(Box::new(scope_key));
} else {
query.push_str(" AND scope_key IS NULL");
}
}
if let Some(exclude_id) = exclude_id {
query.push_str(" AND id != ?");
values.push(Box::new(exclude_id));
}
let mut statement = connection
.prepare(&query)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
rusqlite::params_from_iter(values.iter().map(|value| value.as_ref())),
|row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, i64>(2)?,
row.get::<_, i64>(3)?,
row.get::<_, String>(4)?,
row.get::<_, String>(5)?,
row.get::<_, String>(6)?,
row.get::<_, String>(7)?,
row.get::<_, Option<String>>(8)?,
row.get::<_, Option<String>>(9)?,
row.get::<_, Option<String>>(10)?,
))
},
)
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
let mut index = Connection::open_in_memory().map_err(|error| error.to_string())?;
index
.execute_batch(
"CREATE TABLE history (
id TEXT NOT NULL,
purpose TEXT NOT NULL,
started_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
status TEXT NOT NULL,
command_path TEXT NOT NULL,
source TEXT NOT NULL,
surface TEXT NOT NULL,
actor TEXT,
execution_id TEXT,
error_code TEXT
);",
)
.map_err(|error| error.to_string())?;
let transaction = index.transaction().map_err(|error| error.to_string())?;
{
let mut insert = transaction
.prepare(
"INSERT INTO history(
id, purpose, started_at, updated_at, status, command_path,
source, surface, actor, execution_id, error_code
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
)
.map_err(|error| error.to_string())?;
for row in history {
insert
.execute(params![
row.0, row.1, row.2, row.3, row.4, row.5, row.6, row.7, row.8, row.9,
row.10
])
.map_err(|error| error.to_string())?;
}
}
transaction.commit().map_err(|error| error.to_string())?;
let first_token = sql
.trim_start()
.split(|character: char| character.is_whitespace() || character == '(')
.find(|token| !token.is_empty())
.unwrap_or_default();
if !first_token.eq_ignore_ascii_case("select") && !first_token.eq_ignore_ascii_case("with")
{
return Err("History SQL must be one read-only SELECT statement".to_string());
}
let mut statement = index.prepare(sql).map_err(|error| error.to_string())?;
if !statement.readonly() {
return Err("History SQL must be one read-only SELECT statement".to_string());
}
let columns = statement
.column_names()
.into_iter()
.map(str::to_string)
.collect::<Vec<_>>();
if columns.is_empty()
|| columns.len() > 2
|| columns
.iter()
.any(|column| column != "id" && column != "purpose")
{
return Err("SQL output columns must be id or purpose".to_string());
}
let column_count = columns.len();
let mut query = statement.query([]).map_err(|error| error.to_string())?;
let mut rows = Vec::new();
while rows.len() <= limit {
let Some(row) = query.next().map_err(|error| error.to_string())? else {
break;
};
let mut values = Vec::with_capacity(column_count);
for column in 0..column_count {
let value = match row.get_ref(column).map_err(|error| error.to_string())? {
rusqlite::types::ValueRef::Null => serde_json::Value::Null,
rusqlite::types::ValueRef::Integer(value) => value.into(),
rusqlite::types::ValueRef::Real(value) => serde_json::Number::from_f64(value)
.map(serde_json::Value::Number)
.ok_or_else(|| "SQL returned a non-finite number".to_string())?,
rusqlite::types::ValueRef::Text(value) => {
String::from_utf8_lossy(value).into_owned().into()
}
rusqlite::types::ValueRef::Blob(_) => {
return Err("History SQL cannot return binary values".to_string());
}
};
values.push(value);
}
rows.push(values);
}
let truncated = rows.len() > limit;
rows.truncate(limit);
Ok(HistoryQueryOutput {
columns,
rows,
truncated,
})
}
pub fn timeline_invocations(
&self,
query: &InvocationQuery,
) -> Result<InvocationTimelinePage, String> {
let limit = if query.limit == 0 {
100
} else {
query.limit.min(500)
};
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let cursor = query
.cursor
.as_deref()
.map(parse_timeline_cursor)
.transpose()?;
let snapshot = match cursor.as_ref() {
Some((snapshot, _, _, _)) => *snapshot,
None => connection
.query_row(
"SELECT COALESCE(MAX(accepted_sequence), 0) FROM invocations",
[],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?
.max(0) as u64,
};
let mut sql = "SELECT value FROM invocations WHERE accepted_sequence <= ?".to_string();
let mut values: Vec<Box<dyn rusqlite::ToSql>> = vec![Box::new(as_i64(snapshot))];
if let Some(value) = &query.scope_kind {
sql.push_str(" AND scope_kind = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.scope_key {
sql.push_str(" AND scope_key = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.source {
sql.push_str(" AND source = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.surface {
sql.push_str(" AND surface = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.command_path {
sql.push_str(" AND command_path = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.status {
sql.push_str(" AND status = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.actor {
sql.push_str(" AND actor = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.execution_id {
sql.push_str(" AND execution_id = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = query.since_ms {
sql.push_str(" AND started_at >= ?");
values.push(Box::new(as_i64(value)));
}
if let Some(value) = query.until_ms {
sql.push_str(" AND started_at <= ?");
values.push(Box::new(as_i64(value)));
}
if let Some(value) = &query.exclude_id {
sql.push_str(" AND id != ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.search {
sql.push_str(" AND instr(lower(search_text), lower(?)) > 0");
values.push(Box::new(value.clone()));
}
if let Some((_, started_at, sequence, id)) = cursor {
sql.push_str(
" AND (started_at > ? OR (started_at = ? AND accepted_sequence > ?)
OR (started_at = ? AND accepted_sequence = ? AND id > ?))",
);
values.push(Box::new(as_i64(started_at)));
values.push(Box::new(as_i64(started_at)));
values.push(Box::new(as_i64(sequence)));
values.push(Box::new(as_i64(started_at)));
values.push(Box::new(as_i64(sequence)));
values.push(Box::new(id));
}
sql.push_str(" ORDER BY started_at, accepted_sequence, id LIMIT ?");
values.push(Box::new((limit + 1) as i64));
let mut statement = connection
.prepare(&sql)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
rusqlite::params_from_iter(values.iter().map(|value| value.as_ref())),
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
let mut invocations = deserialize_rows::<InvocationRecord>(rows)?;
let has_more = invocations.len() > limit;
invocations.truncate(limit);
let next_cursor = has_more.then(|| {
let item = invocations.last().expect("timeline cursor item");
format!(
"{snapshot}:{}:{}:{}",
item.started_at, item.accepted_sequence, item.id
)
});
Ok(InvocationTimelinePage {
invocations,
next_cursor,
})
}
pub fn query_invocation_patterns(
&self,
query: &InvocationPatternQuery,
) -> Result<HistoryPatternOutput, String> {
let min_count = query.min_count.max(2);
let limit = if query.limit == 0 {
50
} else {
query.limit.min(100)
};
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let (filters, values) = invocation_pattern_filters(query)?;
let scanned = connection
.query_row(
&format!("SELECT COUNT(*) FROM invocations WHERE pattern_signature != ''{filters}"),
rusqlite::params_from_iter(values.iter().map(|value| value.as_ref())),
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())? as usize;
let (filters, mut values) = invocation_pattern_filters(query)?;
values.push(Box::new(min_count.min(i64::MAX as usize) as i64));
values.push(Box::new((limit + 1).min(i64::MAX as usize) as i64));
let sql = format!(
"SELECT pattern_signature, command_path, COUNT(*),
SUM(status = 'succeeded'), SUM(status = 'failed'),
SUM(status IN ('running', 'incomplete')),
MIN(started_at), MAX(started_at), MAX(variable_paths)
FROM invocations
WHERE pattern_signature != ''{filters}
GROUP BY pattern_signature, command_path
HAVING COUNT(*) >= ?
ORDER BY COUNT(*) DESC, MAX(started_at) DESC, pattern_signature
LIMIT ?"
);
let mut statement = connection
.prepare(&sql)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
rusqlite::params_from_iter(values.iter().map(|value| value.as_ref())),
|row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, i64>(2)?,
row.get::<_, i64>(3)?,
row.get::<_, i64>(4)?,
row.get::<_, i64>(5)?,
row.get::<_, i64>(6)?,
row.get::<_, i64>(7)?,
row.get::<_, String>(8)?,
))
},
)
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?;
drop(statement);
let truncated = rows.len() > limit;
let mut patterns = Vec::with_capacity(rows.len().min(limit));
for (
signature,
command_path,
occurrences,
succeeded,
failed,
incomplete,
first_seen,
last_seen,
variable_paths,
) in rows.into_iter().take(limit)
{
let (sample_filters, sample_values) = invocation_pattern_filters(query)?;
let mut sample_params: Vec<Box<dyn rusqlite::ToSql>> =
vec![Box::new(signature.clone()), Box::new(command_path.clone())];
sample_params.extend(sample_values);
let mut sample_statement = connection
.prepare(&format!(
"SELECT id FROM invocations
WHERE pattern_signature = ? AND command_path = ?{sample_filters}
ORDER BY started_at DESC, id DESC LIMIT 3"
))
.map_err(|error| error.to_string())?;
let sample_invocation_ids = sample_statement
.query_map(
rusqlite::params_from_iter(sample_params.iter().map(|value| value.as_ref())),
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?;
patterns.push(InvocationPattern {
signature,
command_path,
occurrences: occurrences as usize,
succeeded: succeeded as usize,
failed: failed as usize,
incomplete: incomplete as usize,
first_seen: first_seen as u64,
last_seen: last_seen as u64,
variable_paths: serde_json::from_str(&variable_paths)
.map_err(|error| error.to_string())?,
sample_invocation_ids,
});
}
Ok(HistoryPatternOutput {
patterns,
scanned,
truncated,
})
}
pub fn list_execution_records_page(
&self,
before: Option<(u64, &str)>,
status: Option<&str>,
limit: usize,
) -> Result<Vec<Execution>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM execution_records
WHERE (?1 IS NULL OR json_extract(value, '$.status') = ?1)
AND (?2 IS NULL OR updated_at < ?2 OR (updated_at = ?2 AND id < ?3))
ORDER BY updated_at DESC, id DESC LIMIT ?4",
)
.map_err(|error| error.to_string())?;
let (before_at, before_id) = before
.map(|(at, id)| (Some(at as i64), Some(id)))
.unwrap_or((None, None));
let rows = statement
.query_map(params![status, before_at, before_id, limit as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn delete_execution_record(&self, id: &str) -> Result<bool, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM execution_idempotency WHERE execution_id = ?1",
[id],
)
.map_err(|error| error.to_string())?;
transaction
.execute("DELETE FROM poll_iterations WHERE poll_id = ?1", [id])
.map_err(|error| error.to_string())?;
let removed = transaction
.execute("DELETE FROM execution_records WHERE id = ?1", [id])
.map_err(|error| error.to_string())?
> 0;
transaction.commit().map_err(|error| error.to_string())?;
Ok(removed)
}
pub fn put_session(&self, session: &AgentSession) -> Result<(), String> {
let value = serde_json::to_string(session).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR REPLACE INTO agent_sessions(
id, actor, expires_at, closed, value, updated_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
session.id,
session.actor,
session.expires_at.min(i64::MAX as u64) as i64,
session.closed,
value,
now_ms() as i64
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn get_session(&self, id: &str) -> Result<Option<AgentSession>, String> {
self.get_json_row("SELECT value FROM agent_sessions WHERE id = ?1", id)
}
pub fn list_sessions(&self, active_only: bool) -> Result<Vec<AgentSession>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let sql = if active_only {
"SELECT value FROM agent_sessions
WHERE closed = 0 AND expires_at > ?1 ORDER BY updated_at DESC"
} else {
"SELECT value FROM agent_sessions
WHERE ?1 >= 0 ORDER BY updated_at DESC"
};
let mut statement = connection.prepare(sql).map_err(|error| error.to_string())?;
let rows = statement
.query_map([now_ms().min(i64::MAX as u64) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_lease(&self, lease: &Lease) -> Result<(), String> {
let value = serde_json::to_string(lease).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR REPLACE INTO leases(
id, owner_kind, owner_id, expires_at, released, value, updated_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
lease.id,
enum_value(&lease.owner_kind)?,
lease.owner_id,
lease.expires_at.map(as_i64),
lease.released,
value,
now_ms() as i64
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn get_lease(&self, id: &str) -> Result<Option<Lease>, String> {
self.get_json_row("SELECT value FROM leases WHERE id = ?1", id)
}
pub fn list_leases(&self, active_only: bool) -> Result<Vec<Lease>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let sql = if active_only {
"SELECT value FROM leases
WHERE released = 0 AND (expires_at IS NULL OR expires_at > ?1)
ORDER BY updated_at DESC"
} else {
"SELECT value FROM leases WHERE ?1 >= 0 ORDER BY updated_at DESC"
};
let mut statement = connection.prepare(sql).map_err(|error| error.to_string())?;
let rows = statement
.query_map([now_ms().min(i64::MAX as u64) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn release_lease(&self, id: &str) -> Result<Option<Lease>, String> {
let Some(mut lease) = self.get_lease(id)? else {
return Ok(None);
};
lease.released = true;
lease.renewed_at = now_ms();
let value = serde_json::to_string(&lease).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
transaction
.execute(
"UPDATE leases SET released = 1, value = ?2, updated_at = ?3 WHERE id = ?1",
params![id, value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
let mut statement = transaction
.prepare("SELECT id, value FROM reservations WHERE lease_id = ?1 AND released = 0")
.map_err(|error| error.to_string())?;
let reservations = statement
.query_map([id], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?;
drop(statement);
for (reservation_id, value) in reservations {
let mut reservation =
serde_json::from_str::<Reservation>(&value).map_err(|error| error.to_string())?;
reservation.released = true;
transaction
.execute(
"UPDATE reservations SET released = 1, value = ?2, updated_at = ?3
WHERE id = ?1",
params![
reservation_id,
serde_json::to_string(&reservation).map_err(|error| error.to_string())?,
now_ms() as i64
],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(Some(lease))
}
pub fn acquire_reservation(&self, reservation: &Reservation) -> Result<Reservation, String> {
let kind = enum_value(&reservation.kind)?;
let now = now_ms();
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
let current = transaction
.query_row(
"SELECT value FROM reservations
WHERE kind = ?1 AND key = ?2 AND released = 0",
params![kind, reservation.key],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
if let Some(value) = current {
let current =
serde_json::from_str::<Reservation>(&value).map_err(|error| error.to_string())?;
if current.expires_at.is_none_or(|expires_at| expires_at > now) {
return Err(format!(
"RESERVATION_CONFLICT: {} is owned by lease {}",
current.key, current.lease_id
));
}
transaction
.execute(
"UPDATE reservations SET released = 1, updated_at = ?2 WHERE id = ?1",
params![current.id, now as i64],
)
.map_err(|error| error.to_string())?;
}
let value = serde_json::to_string(reservation).map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO reservations(
id, kind, key, lease_id, expires_at, released, value, updated_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
reservation.id,
kind,
reservation.key,
reservation.lease_id,
reservation.expires_at.map(as_i64),
reservation.released,
value,
now as i64
],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(reservation.clone())
}
pub fn get_reservation(&self, id: &str) -> Result<Option<Reservation>, String> {
self.get_json_row("SELECT value FROM reservations WHERE id = ?1", id)
}
pub fn list_reservations(&self, active_only: bool) -> Result<Vec<Reservation>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let sql = if active_only {
"SELECT value FROM reservations
WHERE released = 0 AND (expires_at IS NULL OR expires_at > ?1)
ORDER BY updated_at DESC"
} else {
"SELECT value FROM reservations WHERE ?1 >= 0 ORDER BY updated_at DESC"
};
let mut statement = connection.prepare(sql).map_err(|error| error.to_string())?;
let rows = statement
.query_map([now_ms().min(i64::MAX as u64) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn release_reservation(&self, id: &str) -> Result<Option<Reservation>, String> {
let Some(mut reservation) = self.get_reservation(id)? else {
return Ok(None);
};
reservation.released = true;
let value = serde_json::to_string(&reservation).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"UPDATE reservations SET released = 1, value = ?2, updated_at = ?3
WHERE id = ?1",
params![id, value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
Ok(Some(reservation))
}
pub fn put_operation_receipt(&self, receipt: &OperationReceipt) -> Result<(), String> {
let value = serde_json::to_string(receipt).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO operation_receipts(
id, idempotency_key, operation, state, value, updated_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT(idempotency_key) DO UPDATE SET
state = excluded.state,
value = excluded.value,
updated_at = excluded.updated_at",
params![
receipt.id,
receipt.idempotency_key,
receipt.operation,
enum_value(&receipt.state)?,
value,
now_ms() as i64
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn operation_receipt(&self, key: &str) -> Result<Option<OperationReceipt>, String> {
self.get_json_row(
"SELECT value FROM operation_receipts WHERE idempotency_key = ?1 OR id = ?1",
key,
)
}
pub fn operation_receipts(&self, limit: usize) -> Result<Vec<OperationReceipt>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM operation_receipts ORDER BY updated_at DESC LIMIT ?1")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([limit.min(i64::MAX as usize) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn append_causal_event(&self, mut event: CausalEvent) -> Result<CausalEvent, String> {
let details = serde_json::to_string(&event.details).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT INTO causal_events(
id, at, kind, subject, actor, correlation_id, causation_id,
execution_id, session_id, payload_id, details
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
params![
event.id,
event.at as i64,
event.kind,
event.subject,
event.actor,
event.correlation_id,
event.causation_id,
event.execution_id,
event.session_id,
event.payload_id,
details
],
)
.map_err(|error| error.to_string())?;
event.sequence = connection.last_insert_rowid() as u64;
Ok(event)
}
pub fn causal_events(&self, after: u64, limit: usize) -> Result<Vec<CausalEvent>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT sequence, id, at, kind, subject, actor, correlation_id,
causation_id, execution_id, session_id, payload_id, details
FROM causal_events WHERE sequence > ?1 ORDER BY sequence ASC LIMIT ?2",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![as_i64(after), limit.min(i64::MAX as usize) as i64],
causal_event,
)
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
pub fn causal_events_for_subject(
&self,
subject: &str,
limit: usize,
) -> Result<Vec<CausalEvent>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT sequence, id, at, kind, subject, actor, correlation_id,
causation_id, execution_id, session_id, payload_id, details
FROM causal_events WHERE subject = ?1
ORDER BY sequence DESC LIMIT ?2",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![subject, limit.min(i64::MAX as usize) as i64],
causal_event,
)
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
pub fn put_causal_payload(
&self,
kind: &str,
execution_id: Option<&str>,
value: &Value,
) -> Result<ArtifactMetadata, String> {
let value = serde_json::to_string(value).map_err(|error| error.to_string())?;
let id = format!(
"{:x}",
Sha256::digest([kind.as_bytes(), value.as_bytes()].concat())
);
let now = now_ms();
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO causal_payloads(
id, kind, execution_id, value, bytes, created_at, accessed_at, pinned, pruned
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6, 0, 0)
ON CONFLICT(id) DO UPDATE SET accessed_at = excluded.accessed_at",
params![
id,
kind,
execution_id,
value,
value.len() as i64,
now as i64
],
)
.map_err(|error| error.to_string())?;
Ok(ArtifactMetadata {
id,
kind: kind.to_string(),
execution_id: execution_id.map(str::to_string),
bytes: value.len() as u64,
created_at: now,
accessed_at: now,
pinned: false,
pruned: false,
})
}
pub fn causal_payload(&self, id: &str) -> Result<Option<Value>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let value = connection
.query_row(
"SELECT value FROM causal_payloads WHERE id = ?1 AND pruned = 0",
[id],
|row| row.get::<_, Option<String>>(0),
)
.optional()
.map_err(|error| error.to_string())?
.flatten();
if value.is_some() {
connection
.execute(
"UPDATE causal_payloads SET accessed_at = ?2 WHERE id = ?1",
params![id, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
}
value
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn pin_causal_payload(&self, id: &str, pinned: bool) -> Result<bool, String> {
Ok(self
.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"UPDATE causal_payloads SET pinned = ?2, accessed_at = ?3 WHERE id = ?1",
params![id, pinned, now_ms() as i64],
)
.map_err(|error| error.to_string())?
> 0)
}
pub fn prune_causal_payloads(&self, before: u64) -> Result<usize, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"UPDATE causal_payloads SET value = NULL, pruned = 1
WHERE pinned = 0 AND pruned = 0 AND accessed_at < ?1",
[as_i64(before)],
)
.map_err(|error| error.to_string())
}
pub fn prune_causal_payloads_to_size(&self, max_bytes: u64) -> Result<usize, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
let mut retained = transaction
.query_row(
"SELECT COALESCE(SUM(bytes), 0) FROM causal_payloads WHERE pruned = 0",
[],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?
.max(0) as u64;
if retained <= max_bytes {
return Ok(0);
}
let payloads = {
let mut statement = transaction
.prepare(
"SELECT id, bytes FROM causal_payloads
WHERE pinned = 0 AND pruned = 0
ORDER BY accessed_at ASC, created_at ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, i64>(1)?.max(0) as u64,
))
})
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
let mut pruned = 0;
for (id, bytes) in payloads {
if retained <= max_bytes {
break;
}
transaction
.execute(
"UPDATE causal_payloads SET value = NULL, pruned = 1 WHERE id = ?1",
[id],
)
.map_err(|error| error.to_string())?;
retained = retained.saturating_sub(bytes);
pruned += 1;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(pruned)
}
#[allow(clippy::too_many_arguments)]
pub fn record_processes(
&self,
processes: &[ProcessInfo],
operation: &str,
phase: &str,
selector: Option<&str>,
controller: &ProcessController,
origin: &str,
managed_execution_id: Option<&str>,
details: &Value,
) -> Result<Vec<ProcessEvent>, String> {
self.record_processes_with_details(
processes,
operation,
phase,
selector,
controller,
origin,
managed_execution_id,
&vec![details.clone(); processes.len()],
)
}
#[allow(clippy::too_many_arguments)]
pub fn record_processes_with_details(
&self,
processes: &[ProcessInfo],
operation: &str,
phase: &str,
selector: Option<&str>,
controller: &ProcessController,
origin: &str,
managed_execution_id: Option<&str>,
details: &[Value],
) -> Result<Vec<ProcessEvent>, String> {
if processes.len() != details.len() {
return Err("Process snapshots and event details must have equal lengths".to_string());
}
let at = now_ms();
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction()
.map_err(|error| error.to_string())?;
let mut events = Vec::with_capacity(processes.len());
for (process, details) in processes.iter().zip(details) {
let key = process_key(&process.identity);
let latest = serde_json::to_string(process).map_err(|error| error.to_string())?;
let snapshot_id = format!("{:x}", Sha256::digest(latest.as_bytes()));
transaction
.execute(
"INSERT OR IGNORE INTO causal_payloads(
id, kind, execution_id, value, bytes, created_at, accessed_at, pinned, pruned
) VALUES (?1, 'process_snapshot', ?2, ?3, ?4, ?5, ?5, 0, 0)",
params![
snapshot_id,
controller.execution_id,
latest,
latest.len() as i64,
at as i64
],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO process_contexts(
key, pid, started_at, first_observed_at, last_observed_at,
observations, origin, run_id, snapshot_id, latest
) VALUES (?1, ?2, ?3, ?4, ?4, 1, ?5, ?6, ?7, ?8)
ON CONFLICT(key) DO UPDATE SET
last_observed_at = excluded.last_observed_at,
observations = process_contexts.observations + 1,
origin = CASE
WHEN excluded.origin = 'observed' THEN process_contexts.origin
ELSE excluded.origin
END,
run_id = COALESCE(excluded.run_id, process_contexts.run_id),
snapshot_id = excluded.snapshot_id,
latest = CASE
WHEN process_contexts.snapshot_id = excluded.snapshot_id
THEN process_contexts.latest
ELSE excluded.latest
END",
params![
key,
process.identity.pid as i64,
process.identity.started_at as i64,
at as i64,
origin,
managed_execution_id,
snapshot_id,
latest
],
)
.map_err(|error| error.to_string())?;
let event = ProcessEvent {
id: uuid::Uuid::now_v7().to_string(),
process_key: key,
at,
operation: operation.to_string(),
phase: phase.to_string(),
selector: selector.map(str::to_string),
controller: controller.clone(),
execution_id: managed_execution_id.map(str::to_string),
details: details.clone(),
};
transaction
.execute(
"INSERT INTO process_events(
id, process_key, at, operation, phase, actor,
execution_id, run_id, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
params![
event.id,
event.process_key,
event.at as i64,
event.operation,
event.phase,
event.controller.actor,
event.controller.execution_id,
event.execution_id,
serde_json::to_string(&event).map_err(|error| error.to_string())?
],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO causal_events(
id, at, kind, subject, actor, correlation_id, causation_id,
execution_id, session_id, payload_id, details
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, NULL, ?7, NULL, ?8, ?9)",
params![
event.id,
event.at as i64,
format!("{}.{}", event.operation, event.phase),
event.process_key,
event.controller.actor,
event.controller.invocation_id,
event.controller.execution_id,
snapshot_id,
serde_json::to_string(&serde_json::json!({
"selector": &event.selector,
"execution_id": &event.execution_id,
"details": &event.details
}))
.map_err(|error| error.to_string())?
],
)
.map_err(|error| error.to_string())?;
events.push(event);
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(events)
}
pub fn process_dossier(&self, key: &str, event_limit: usize) -> Result<ProcessDossier, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let context = connection
.query_row(
"SELECT key, first_observed_at, last_observed_at, observations,
origin, run_id, snapshot_id, latest
FROM process_contexts WHERE key = ?1",
[key],
process_context,
)
.optional()
.map_err(|error| error.to_string())?
.ok_or_else(|| format!("No recorded process context for \"{key}\""))?;
let events = process_events(&connection, Some(key), event_limit)?;
Ok(ProcessDossier { context, events })
}
pub fn process_dossiers_for_pid(
&self,
pid: u32,
event_limit: usize,
) -> Result<Vec<ProcessDossier>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT key, first_observed_at, last_observed_at, observations,
origin, run_id, snapshot_id, latest
FROM process_contexts WHERE pid = ?1 ORDER BY started_at DESC",
)
.map_err(|error| error.to_string())?;
let contexts = statement
.query_map([pid as i64], process_context)
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?;
contexts
.into_iter()
.map(|context| {
let events = process_events(&connection, Some(&context.key), event_limit)?;
Ok(ProcessDossier { context, events })
})
.collect()
}
pub fn list_process_events(&self, limit: usize) -> Result<Vec<ProcessEvent>, String> {
self.query_process_events(&ProcessEventQuery {
limit,
..ProcessEventQuery::default()
})
}
pub fn query_process_events(
&self,
query: &ProcessEventQuery,
) -> Result<Vec<ProcessEvent>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut sql = "SELECT value FROM process_events WHERE 1 = 1".to_string();
let mut values = Vec::<Box<dyn rusqlite::ToSql>>::new();
if let Some(value) = &query.process_key {
sql.push_str(" AND process_key = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = query.pid {
sql.push_str(" AND process_key LIKE ?");
values.push(Box::new(format!("%:{value}@%")));
}
if let Some(value) = &query.operation {
sql.push_str(" AND operation = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.phase {
sql.push_str(" AND phase = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.actor {
sql.push_str(" AND actor = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.controller_execution_id {
sql.push_str(" AND execution_id = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.execution_id {
sql.push_str(" AND run_id = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = query.since_ms {
sql.push_str(" AND at >= ?");
values.push(Box::new(value.min(i64::MAX as u64) as i64));
}
if let Some(value) = query.until_ms {
sql.push_str(" AND at <= ?");
values.push(Box::new(value.min(i64::MAX as u64) as i64));
}
sql.push_str(" ORDER BY at DESC LIMIT ?");
values.push(Box::new(query.limit.min(i64::MAX as usize) as i64));
let mut statement = connection
.prepare(&sql)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
rusqlite::params_from_iter(values.iter().map(|value| value.as_ref())),
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
rows.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
fn put_named(
&self,
table: &str,
key: &str,
name: &str,
value: &impl Serialize,
) -> Result<(), String> {
require_table(table, key)?;
let value = serde_json::to_string(value).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
&format!(
"INSERT OR REPLACE INTO {table}({key}, value, updated_at)
VALUES (?1, ?2, ?3)"
),
params![name, value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
Ok(())
}
fn get_json_row<T: DeserializeOwned>(&self, sql: &str, key: &str) -> Result<Option<T>, String> {
let value = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(sql, [key], |row| row.get::<_, String>(0))
.optional()
.map_err(|error| error.to_string())?;
value
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
fn get_named<T: DeserializeOwned>(
&self,
table: &str,
key: &str,
name: &str,
) -> Result<Option<T>, String> {
require_table(table, key)?;
let value = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
&format!("SELECT value FROM {table} WHERE {key} = ?1"),
[name],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
value
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
fn list_values<T: DeserializeOwned>(&self, table: &str, order: &str) -> Result<Vec<T>, String> {
require_list(table, order)?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(&format!("SELECT value FROM {table} ORDER BY {order}"))
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
rows.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
fn delete_named(&self, table: &str, key: &str, name: &str) -> Result<bool, String> {
require_table(table, key)?;
Ok(self
.connection
.lock()
.map_err(|error| error.to_string())?
.execute(&format!("DELETE FROM {table} WHERE {key} = ?1"), [name])
.map_err(|error| error.to_string())?
> 0)
}
}
fn backup_before_migration(path: &Path) -> Result<(), String> {
if !path.exists() || path.metadata().map_err(|error| error.to_string())?.len() == 0 {
return Ok(());
}
let connection = Connection::open(path).map_err(|error| error.to_string())?;
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.map_err(|error| error.to_string())?;
let tables = connection
.query_row(
"SELECT EXISTS(
SELECT 1 FROM sqlite_master
WHERE type = 'table' AND name NOT LIKE 'sqlite_%'
)",
[],
|row| row.get::<_, bool>(0),
)
.map_err(|error| error.to_string())?;
if version >= DATABASE_VERSION || !tables {
return Ok(());
}
connection
.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);")
.map_err(|error| error.to_string())?;
drop(connection);
let destination = DATABASE_VERSION;
let backup = PathBuf::from(format!(
"{}.pre-v{destination}-{}.sqlite3",
path.display(),
now_ms()
));
std::fs::copy(path, &backup).map_err(|error| {
format!(
"Could not back up database before v{destination} migration {} to {}: {error}",
path.display(),
backup.display()
)
})?;
Ok(())
}
fn invocation_pattern_filters(
query: &InvocationPatternQuery,
) -> Result<(String, Vec<Box<dyn rusqlite::ToSql>>), String> {
let mut sql = String::new();
let mut values = Vec::<Box<dyn rusqlite::ToSql>>::new();
if let Some(value) = &query.scope_kind {
sql.push_str(" AND scope_kind = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.scope_key {
sql.push_str(" AND scope_key = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = &query.source {
sql.push_str(" AND source = ?");
values.push(Box::new(enum_value(value)?));
}
if let Some(value) = &query.command_path {
sql.push_str(" AND command_path = ?");
values.push(Box::new(value.clone()));
}
if let Some(value) = query.since_ms {
sql.push_str(" AND started_at >= ?");
values.push(Box::new(as_i64(value)));
}
if let Some(value) = query.until_ms {
sql.push_str(" AND started_at <= ?");
values.push(Box::new(as_i64(value)));
}
if let Some(value) = &query.exclude_id {
sql.push_str(" AND id != ?");
values.push(Box::new(value.clone()));
}
Ok((sql, values))
}
fn backfill_invocation_patterns(connection: &Connection) -> Result<(), String> {
let rows = {
let mut statement = connection
.prepare(
"SELECT id, value FROM invocations
WHERE pattern_signature = '' OR variable_paths = '[]'",
)
.map_err(|error| error.to_string())?;
statement
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
for (id, value) in rows {
let mut invocation =
serde_json::from_str::<InvocationRecord>(&value).map_err(|error| error.to_string())?;
let pattern =
invocation_pattern_signature(&invocation.command_path, &invocation.normalized_input);
invocation.pattern_signature = pattern.signature;
invocation.variable_paths = pattern.variable_paths;
connection
.execute(
"UPDATE invocations
SET pattern_signature = ?1, variable_paths = ?2, value = ?3
WHERE id = ?4",
params![
invocation.pattern_signature,
serde_json::to_string(&invocation.variable_paths)
.map_err(|error| error.to_string())?,
serde_json::to_string(&invocation).map_err(|error| error.to_string())?,
id,
],
)
.map_err(|error| error.to_string())?;
}
Ok(())
}
fn backfill_invocation_sequences(connection: &Connection) -> Result<(), String> {
let rows = {
let mut statement = connection
.prepare("SELECT id, value FROM invocations ORDER BY started_at, rowid")
.map_err(|error| error.to_string())?;
statement
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
for (index, (id, value)) in rows.into_iter().enumerate() {
let mut invocation =
serde_json::from_str::<InvocationRecord>(&value).map_err(|error| error.to_string())?;
invocation.accepted_sequence = (index + 1) as u64;
connection
.execute(
"UPDATE invocations SET accepted_sequence = ?1, value = ?2 WHERE id = ?3",
params![
as_i64(invocation.accepted_sequence),
serde_json::to_string(&invocation).map_err(|error| error.to_string())?,
id,
],
)
.map_err(|error| error.to_string())?;
}
Ok(())
}
fn backfill_invocation_purposes(connection: &Connection) -> Result<(), String> {
let rows = {
let mut statement = connection
.prepare("SELECT id, value FROM invocations WHERE purpose IS NULL")
.map_err(|error| error.to_string())?;
statement
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
for (id, value) in rows {
let mut invocation =
serde_json::from_str::<InvocationRecord>(&value).map_err(|error| error.to_string())?;
let purpose = invocation
.normalized_input
.get("purpose")
.or_else(|| invocation.normalized_input.pointer("/options/purpose"))
.and_then(Value::as_str)
.map(str::trim)
.filter(|purpose| !purpose.is_empty())
.map(ToString::to_string);
let Some(purpose) = purpose else {
continue;
};
invocation.purpose = Some(purpose.clone());
if !invocation
.search_text
.to_lowercase()
.contains(&purpose.to_lowercase())
{
invocation.search_text = format!("{} {purpose}", invocation.search_text);
}
connection
.execute(
"UPDATE invocations SET purpose = ?1, search_text = ?2, value = ?3 WHERE id = ?4",
params![
purpose,
invocation.search_text,
serde_json::to_string(&invocation).map_err(|error| error.to_string())?,
id,
],
)
.map_err(|error| error.to_string())?;
}
Ok(())
}
fn migrate_legacy_recipes(connection: &Connection) -> Result<(), String> {
let rows = {
let mut statement = connection
.prepare("SELECT name, value FROM recipes")
.map_err(|error| error.to_string())?;
statement
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
for (stored_name, value) in rows {
let mut recipe = serde_json::from_str::<crate::model::Recipe>(&value)
.map_err(|error| error.to_string())?;
let legacy = !stored_name.contains("::");
if legacy {
recipe.scope_kind = crate::model::RecipeScopeKind::Global;
recipe.scope_key = None;
}
if legacy || recipe.generation.is_empty() {
recipe.generation = recipe_generation(&recipe)?;
let key =
recipe_storage_key(recipe.scope_kind, recipe.scope_key.as_deref(), &recipe.name)?;
connection
.execute(
"INSERT OR REPLACE INTO recipes(name, value, updated_at) VALUES (?1, ?2, ?3)",
params![
key,
serde_json::to_string(&recipe).map_err(|error| error.to_string())?,
as_i64(recipe.updated_at),
],
)
.map_err(|error| error.to_string())?;
if stored_name != key {
connection
.execute("DELETE FROM recipes WHERE name = ?1", [stored_name])
.map_err(|error| error.to_string())?;
}
}
}
Ok(())
}
fn process_key(identity: &crate::model::ProcessIdentity) -> String {
crate::process::process_context_key(identity)
}
fn ensure_column(
connection: &Connection,
table: &str,
column: &str,
kind: &str,
) -> Result<bool, String> {
let mut statement = connection
.prepare(&format!("PRAGMA table_info({table})"))
.map_err(|error| error.to_string())?;
let columns = statement
.query_map([], |row| row.get::<_, String>(1))
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?;
if !columns.iter().any(|value| value == column) {
connection
.execute(
&format!("ALTER TABLE {table} ADD COLUMN {column} {kind}"),
[],
)
.map_err(|error| error.to_string())?;
return Ok(true);
}
Ok(false)
}
fn process_context(row: &rusqlite::Row<'_>) -> rusqlite::Result<ProcessContext> {
let latest = row.get::<_, String>(7)?;
let latest = serde_json::from_str::<ProcessInfo>(&latest).map_err(|error| {
rusqlite::Error::FromSqlConversionFailure(7, rusqlite::types::Type::Text, Box::new(error))
})?;
Ok(ProcessContext {
key: row.get(0)?,
first_observed_at: row.get::<_, i64>(1)? as u64,
last_observed_at: row.get::<_, i64>(2)? as u64,
observations: row.get::<_, i64>(3)? as u64,
snapshot_id: row.get(6)?,
origin: row.get(4)?,
execution_id: row.get(5)?,
latest,
})
}
fn process_events(
connection: &Connection,
key: Option<&str>,
limit: usize,
) -> Result<Vec<ProcessEvent>, String> {
let (sql, parameter): (&str, Box<dyn rusqlite::ToSql>) = if let Some(key) = key {
(
"SELECT value FROM process_events
WHERE process_key = ?1 ORDER BY at DESC LIMIT ?2",
Box::new(key.to_string()),
)
} else {
(
"SELECT value FROM process_events
WHERE ?1 IS NULL ORDER BY at DESC LIMIT ?2",
Box::new(rusqlite::types::Null),
)
};
let mut statement = connection.prepare(sql).map_err(|error| error.to_string())?;
let rows = statement
.query_map(
rusqlite::params_from_iter([
parameter.as_ref(),
&(limit.min(i64::MAX as usize) as i64) as &dyn rusqlite::ToSql,
]),
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
rows.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
fn causal_event(row: &rusqlite::Row<'_>) -> rusqlite::Result<CausalEvent> {
let details = row.get::<_, String>(11)?;
let details = serde_json::from_str(&details).map_err(|error| {
rusqlite::Error::FromSqlConversionFailure(11, rusqlite::types::Type::Text, Box::new(error))
})?;
Ok(CausalEvent {
sequence: row.get::<_, i64>(0)? as u64,
id: row.get(1)?,
at: row.get::<_, i64>(2)? as u64,
kind: row.get(3)?,
subject: row.get(4)?,
actor: row.get(5)?,
correlation_id: row.get(6)?,
causation_id: row.get(7)?,
execution_id: row.get(8)?,
session_id: row.get(9)?,
payload_id: row.get(10)?,
details,
})
}
fn deserialize_rows<T: DeserializeOwned>(
rows: impl Iterator<Item = rusqlite::Result<String>>,
) -> Result<Vec<T>, String> {
rows.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
fn parse_invocation_cursor(value: &str) -> Result<(u64, String), String> {
let (updated_at, id) = value
.split_once(':')
.ok_or_else(|| "Invalid invocation cursor".to_string())?;
let updated_at = updated_at
.parse::<u64>()
.map_err(|_| "Invalid invocation cursor".to_string())?;
if id.is_empty() {
return Err("Invalid invocation cursor".to_string());
}
Ok((updated_at, id.to_string()))
}
fn parse_timeline_cursor(value: &str) -> Result<(u64, u64, u64, String), String> {
let mut parts = value.splitn(4, ':');
let snapshot = parts
.next()
.and_then(|value| value.parse::<u64>().ok())
.ok_or_else(|| "Invalid history timeline cursor".to_string())?;
let started_at = parts
.next()
.and_then(|value| value.parse::<u64>().ok())
.ok_or_else(|| "Invalid history timeline cursor".to_string())?;
let sequence = parts
.next()
.and_then(|value| value.parse::<u64>().ok())
.ok_or_else(|| "Invalid history timeline cursor".to_string())?;
let id = parts
.next()
.filter(|value| !value.is_empty())
.ok_or_else(|| "Invalid history timeline cursor".to_string())?;
Ok((snapshot, started_at, sequence, id.to_string()))
}
fn fts_literal(value: &str) -> String {
format!("\"{}\"", value.replace('"', "\"\""))
}
fn enum_value(value: &impl Serialize) -> Result<String, String> {
serde_json::to_value(value)
.map_err(|error| error.to_string())?
.as_str()
.map(str::to_string)
.ok_or_else(|| "Expected a string-backed enum".to_string())
}
fn as_i64(value: u64) -> i64 {
value.min(i64::MAX as u64) as i64
}
#[async_trait]
impl RuntimeStore for Store {
async fn get_execution(&self, id: &str) -> Result<Option<ExecutionState>, String> {
self.get_named("executions", "id", id)
}
async fn put_execution(&self, execution: &ExecutionState) -> Result<(), String> {
self.put_named("executions", "id", &execution.id, execution)
}
async fn list_executions(&self) -> Result<Vec<ExecutionState>, String> {
self.list_values("executions", "updated_at DESC")
}
async fn delete_execution(&self, id: &str) -> Result<(), String> {
self.delete_named("executions", "id", id)?;
Ok(())
}
async fn get_snippet(&self, name: &str) -> Result<Option<Snippet>, String> {
self.get_named("snippets", "name", name)
}
async fn put_snippet(&self, snippet: &Snippet) -> Result<(), String> {
self.put_named("snippets", "name", &snippet.name, snippet)
}
async fn list_snippets(&self) -> Result<Vec<Snippet>, String> {
self.list_values("snippets", "name ASC")
}
async fn delete_snippet(&self, name: &str) -> Result<bool, String> {
self.delete_named("snippets", "name", name)
}
}
#[async_trait]
impl ArtifactStore for Store {
async fn put(&self, execution_id: &str, value: &Value) -> Result<ArtifactRef, String> {
let value = serde_json::to_string(value).map_err(|error| error.to_string())?;
let bytes = value.len();
let id = format!(
"{:x}",
Sha256::digest([execution_id.as_bytes(), value.as_bytes()].concat())
);
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR REPLACE INTO artifacts(id, execution_id, value, bytes, created_at)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![id, execution_id, value, bytes as i64, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
Ok(ArtifactRef {
id,
execution_id: execution_id.to_string(),
bytes,
preview: truncate(&value, 512),
})
}
async fn get(&self, execution_id: &str, artifact_id: &str) -> Result<Option<Value>, String> {
let value = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM artifacts WHERE id = ?1 AND execution_id = ?2",
params![artifact_id, execution_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
value
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
async fn delete_execution(&self, execution_id: &str) -> Result<(), String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"DELETE FROM artifacts WHERE execution_id = ?1",
[execution_id],
)
.map_err(|error| error.to_string())?;
Ok(())
}
}
pub fn state_dir() -> Result<PathBuf, String> {
if let Some(path) = std::env::var_os("APOC_HOME") {
return Ok(PathBuf::from(path));
}
dirs::data_dir()
.map(|path| path.join("apoc"))
.ok_or_else(|| "Cannot determine the per-user application data directory".to_string())
}
pub fn runtime_dir() -> PathBuf {
if let Some(path) = std::env::var_os("APOC_RUNTIME_DIR") {
return PathBuf::from(path);
}
std::env::temp_dir().join(format!("apoc-{}", unsafe { libc::geteuid() }))
}
fn require_table(table: &str, key: &str) -> Result<(), String> {
if matches!(
(table, key),
("runs", "id")
| ("executions", "id")
| ("execution_records", "id")
| ("snippets", "name")
| ("recipes", "name")
| ("environments", "scope")
| ("recipe_suggestions", "id")
| ("connectors", "name")
| ("providers", "name")
| ("services", "name")
) {
Ok(())
} else {
Err("Unsupported state table".to_string())
}
}
fn require_list(table: &str, order: &str) -> Result<(), String> {
if matches!(
(table, order),
("runs", "updated_at DESC")
| ("executions", "updated_at DESC")
| ("execution_records", "updated_at DESC")
| ("snippets", "name ASC")
| ("recipes", "name ASC")
| ("recipe_suggestions", "created_at DESC, id DESC")
| ("connectors", "name ASC")
| ("providers", "name ASC")
| ("services", "name ASC")
) {
Ok(())
} else {
Err("Unsupported state list".to_string())
}
}
fn truncate(value: &str, max: usize) -> String {
if value.len() <= max {
return value.to_string();
}
let mut end = max;
while !value.is_char_boundary(end) {
end -= 1;
}
format!("{}...", &value[..end])
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use tempfile::tempdir;
use super::*;
use crate::model::{
InvocationPatternQuery, InvocationQuery, InvocationRecord, InvocationScopeKind,
InvocationSource, InvocationStatus, InvocationSurface, IoMode, Reservation,
ReservationKind, RestartPolicy, RunLifetime, RunSpec,
};
#[test]
fn backs_up_an_unversioned_database_before_migration() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let connection = Connection::open(&path).unwrap();
connection
.execute_batch("CREATE TABLE legacy_state (id TEXT PRIMARY KEY);")
.unwrap();
drop(connection);
drop(Store::open(&path).unwrap());
let backups = std::fs::read_dir(directory.path())
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().to_string())
.filter(|name| name.starts_with("state.sqlite3.pre-v8-"))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, 8);
}
#[test]
fn migrates_v1_connectors_into_providers() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let connection = Connection::open(&path).unwrap();
connection
.execute_batch(
"CREATE TABLE connectors (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
INSERT INTO connectors(name, value, updated_at) VALUES (
'fixture',
'{\"name\":\"fixture\",\"kind\":\"mcp_http\",\"url\":\"http://localhost/mcp\",\"base_url\":null,\"headers\":{},\"instructions\":null,\"created_at\":1,\"updated_at\":1}',
1
);
PRAGMA user_version = 1;",
)
.unwrap();
drop(connection);
let store = Store::open(&path).unwrap();
let backups = std::fs::read_dir(directory.path())
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().to_string())
.filter(|name| name.starts_with("state.sqlite3.pre-v8-"))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let providers = store.list_providers().unwrap();
assert_eq!(providers.len(), 1);
let provider =
serde_json::from_value::<crate::model::ProviderConfig>(providers[0].clone()).unwrap();
assert_eq!(provider.name, "fixture");
assert_eq!(provider.kind, crate::model::ProviderKind::McpHttp);
assert!(store.get_connector("fixture").unwrap().is_some());
}
#[test]
fn migrates_v4_recipes_to_global_scoped_definitions() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let connection = Connection::open(&path).unwrap();
connection
.execute_batch(
"CREATE TABLE recipes (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
INSERT INTO recipes(name, value, updated_at) VALUES (
'release.logs',
'{\"name\":\"release.logs\",\"description\":\"legacy\",\"code\":\"return 1;\",\"input_schema\":null,\"created_at\":10,\"updated_at\":20}',
20
);
PRAGMA user_version = 4;",
)
.unwrap();
drop(connection);
let store = Store::open(&path).unwrap();
let recipe = store
.get_recipe_definition(crate::model::RecipeScopeKind::Global, None, "release.logs")
.unwrap()
.unwrap();
assert_eq!(recipe.scope_kind, crate::model::RecipeScopeKind::Global);
assert!(recipe.scope_key.is_none());
assert!(recipe.generation.starts_with("sha256:"));
assert!(recipe.expires_at.is_none());
let backups = std::fs::read_dir(directory.path())
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().to_string())
.filter(|name| name.starts_with("state.sqlite3.pre-v8-"))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
}
#[test]
fn migrates_v5_invocations_to_stable_acceptance_sequences() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let store = Store::open(&path).unwrap();
store
.start_invocation(&invocation("legacy", "version", 10))
.unwrap();
drop(store);
let connection = Connection::open(&path).unwrap();
let encoded = connection
.query_row(
"SELECT value FROM invocations WHERE id = 'legacy'",
[],
|row| row.get::<_, String>(0),
)
.unwrap();
let mut value = serde_json::from_str::<Value>(&encoded).unwrap();
value.as_object_mut().unwrap().remove("accepted_sequence");
connection
.execute_batch(
"DROP INDEX invocations_timeline;
ALTER TABLE invocations DROP COLUMN accepted_sequence;
PRAGMA user_version = 5;",
)
.unwrap();
connection
.execute(
"UPDATE invocations SET value = ?1 WHERE id = 'legacy'",
[serde_json::to_string(&value).unwrap()],
)
.unwrap();
drop(connection);
let store = Store::open(&path).unwrap();
assert_eq!(
store
.get_invocation("legacy")
.unwrap()
.unwrap()
.accepted_sequence,
1
);
let backups = std::fs::read_dir(directory.path())
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().to_string())
.filter(|name| name.starts_with("state.sqlite3.pre-v8-"))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
}
#[test]
fn adds_invocation_pattern_columns_before_creating_their_index() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let store = Store::open(&path).unwrap();
store
.start_invocation(&invocation("legacy-pattern", "version", 10))
.unwrap();
drop(store);
let connection = Connection::open(&path).unwrap();
connection
.execute_batch(
"DROP INDEX invocations_pattern;
ALTER TABLE invocations DROP COLUMN pattern_signature;
ALTER TABLE invocations DROP COLUMN variable_paths;
PRAGMA user_version = 4;",
)
.unwrap();
drop(connection);
let store = Store::open(&path).unwrap();
let migrated = store.get_invocation("legacy-pattern").unwrap().unwrap();
assert!(!migrated.pattern_signature.is_empty());
let connection = Connection::open(path).unwrap();
let index_count = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'invocations_pattern'",
[],
|row| row.get::<_, i64>(0),
)
.unwrap();
assert_eq!(index_count, 1);
}
#[test]
fn migrates_v6_invocation_purposes_into_searchable_history() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let store = Store::open(&path).unwrap();
let mut legacy = invocation("legacy-purpose", "execution command", 10);
legacy.normalized_input = serde_json::json!({
"args": { "executable": "cargo" },
"options": { "purpose": "Running the legacy verification suite." }
});
legacy.search_text = "execution command cargo".to_string();
store.insert_invocation(&legacy).unwrap();
drop(store);
let connection = Connection::open(&path).unwrap();
let encoded = connection
.query_row(
"SELECT value FROM invocations WHERE id = 'legacy-purpose'",
[],
|row| row.get::<_, String>(0),
)
.unwrap();
let mut value = serde_json::from_str::<Value>(&encoded).unwrap();
value.as_object_mut().unwrap().remove("purpose");
connection
.execute_batch(
"ALTER TABLE invocations DROP COLUMN purpose;
PRAGMA user_version = 6;",
)
.unwrap();
connection
.execute(
"UPDATE invocations SET value = ?1 WHERE id = 'legacy-purpose'",
[serde_json::to_string(&value).unwrap()],
)
.unwrap();
drop(connection);
let store = Store::open(&path).unwrap();
let migrated = store.get_invocation("legacy-purpose").unwrap().unwrap();
assert_eq!(
migrated.purpose.as_deref(),
Some("Running the legacy verification suite.")
);
assert_eq!(
store
.query_invocations(&InvocationQuery {
limit: 10,
search: Some("legacy verification".to_string()),
..InvocationQuery::default()
})
.unwrap()
.invocations[0]
.id,
"legacy-purpose"
);
let backups = std::fs::read_dir(directory.path())
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().to_string())
.filter(|name| name.starts_with("state.sqlite3.pre-v8-"))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
}
#[test]
fn persists_managed_runs() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let run = ManagedRun {
id: "run-1".to_string(),
spec: RunSpec {
name: "fixture".to_string(),
purpose: None,
labels: BTreeMap::new(),
requested_by: None,
argv: vec!["true".to_string()],
cwd: None,
env: BTreeMap::new(),
resolved_env: Default::default(),
shell: false,
io: IoMode::Pipes,
lifetime: RunLifetime::Persistent,
restart: RestartPolicy::Never,
timeout_ms: None,
replicas: 1,
max_attempts: 1,
retry_backoff_ms: 500,
retry_max_backoff_ms: 30_000,
},
status: "running".to_string(),
processes: Vec::new(),
process_history: Vec::new(),
created_at: 1,
updated_at: 1,
exit_code: None,
stdout_path: None,
stderr_path: None,
};
store.put_run(&run).unwrap();
assert_eq!(
store.get_run("run-1").unwrap().unwrap().spec.name,
"fixture"
);
assert_eq!(store.list_runs().unwrap().len(), 1);
}
#[test]
fn stores_and_finishes_native_invocations() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let mut record = invocation("invoke-1", "process.list", 100);
record.status = InvocationStatus::Running;
record.completed_at = None;
store.start_invocation(&record).unwrap();
let started = store.get_invocation("invoke-1").unwrap().unwrap();
assert_eq!(started.status, InvocationStatus::Running);
assert!(started.completed_at.is_none());
store
.finish_invocation(
"invoke-1",
InvocationStatus::Succeeded,
Some(150),
Some(0),
None,
None,
)
.unwrap();
let finished = store.get_invocation("invoke-1").unwrap().unwrap();
assert_eq!(finished.status, InvocationStatus::Succeeded);
assert_eq!(finished.exit_code, Some(0));
assert_eq!(finished.completed_at, Some(150));
assert_eq!(finished.updated_at, 150);
store
.update_invocation_input(
"invoke-1",
Some("Inspecting one process.".to_string()),
serde_json::json!({ "selector": "pid:42" }),
"process.list Inspecting one process. selector pid:42".to_string(),
)
.unwrap();
let updated = store.get_invocation("invoke-1").unwrap().unwrap();
assert_eq!(
updated.normalized_input,
serde_json::json!({ "selector": "pid:42" })
);
assert_eq!(updated.purpose.as_deref(), Some("Inspecting one process."));
assert_eq!(
updated.search_text,
"process.list Inspecting one process. selector pid:42"
);
assert!(
store
.query_invocations(&InvocationQuery {
limit: 10,
search: Some("fixture input".to_string()),
..InvocationQuery::default()
})
.unwrap()
.invocations
.is_empty()
);
assert_eq!(
store
.query_invocations(&InvocationQuery {
limit: 10,
search: Some("pid:42".to_string()),
..InvocationQuery::default()
})
.unwrap()
.invocations
.len(),
1
);
}
#[test]
fn queries_invocations_with_literal_search_filters_and_cursor_pages() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
for record in [
invocation("global", "execution.command", 300),
InvocationRecord {
id: "local".to_string(),
actor: Some("agent-a".to_string()),
command_path: "process.get".to_string(),
search_text: "PID 42 exact lookup".to_string(),
scope_kind: InvocationScopeKind::Cwd,
scope_key: Some("/tmp/project".to_string()),
started_at: 200,
updated_at: 200,
..invocation("local", "process.get", 200)
},
InvocationRecord {
id: "devsql".to_string(),
source: InvocationSource::Devsql,
surface: InvocationSurface::Code,
command_path: "history.recall".to_string(),
search_text: "Recent PROCESS management calls".to_string(),
status: InvocationStatus::Incomplete,
started_at: 100,
updated_at: 100,
..invocation("devsql", "history.recall", 100)
},
] {
store.insert_invocation(&record).unwrap();
}
let local = store
.query_invocations(&InvocationQuery {
limit: 10,
scope_kind: Some(InvocationScopeKind::Cwd),
scope_key: Some("/tmp/project".to_string()),
search: Some("pid 42".to_string()),
..InvocationQuery::default()
})
.unwrap();
assert_eq!(local.invocations.len(), 1);
assert_eq!(local.invocations[0].id, "local");
let searched = store
.query_invocations(&InvocationQuery {
limit: 10,
search: Some("PROCESS".to_string()),
exclude_id: Some("local".to_string()),
..InvocationQuery::default()
})
.unwrap();
assert_eq!(searched.invocations.len(), 1);
assert_eq!(searched.invocations[0].id, "devsql");
let first = store
.query_invocations(&InvocationQuery {
limit: 1,
..InvocationQuery::default()
})
.unwrap();
let second = store
.query_invocations(&InvocationQuery {
limit: 10,
cursor: first.next_cursor.clone(),
..InvocationQuery::default()
})
.unwrap();
assert_eq!(first.invocations[0].id, "global");
assert_eq!(
second
.invocations
.iter()
.map(|invocation| invocation.id.as_str())
.collect::<Vec<_>>(),
vec!["local", "devsql"]
);
assert!(second.next_cursor.is_none());
}
#[test]
fn invocation_search_keeps_literal_and_short_query_semantics() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
store
.insert_invocation(&InvocationRecord {
search_text: "PID 42 with a quoted \"exact value\"".to_string(),
..invocation("literal", "process get", 100)
})
.unwrap();
for query in ["pid", "42", "\"exact value\""] {
let page = store
.query_invocations(&InvocationQuery {
limit: 10,
search: Some(query.to_string()),
..InvocationQuery::default()
})
.unwrap();
assert_eq!(page.invocations.len(), 1, "query: {query}");
assert_eq!(page.invocations[0].id, "literal");
}
}
#[test]
fn groups_repeated_invocation_patterns_with_status_and_scope_counts() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
for (id, executable, status, at, scope) in [
(
"cargo-a",
"cargo",
InvocationStatus::Succeeded,
100,
"/tmp/a",
),
("cargo-b", "cargo", InvocationStatus::Failed, 200, "/tmp/a"),
(
"cargo-c",
"cargo",
InvocationStatus::Incomplete,
300,
"/tmp/a",
),
("git-a", "git", InvocationStatus::Succeeded, 400, "/tmp/a"),
(
"cargo-other",
"cargo",
InvocationStatus::Succeeded,
500,
"/tmp/b",
),
] {
store
.insert_invocation(&InvocationRecord {
normalized_input: serde_json::json!({
"args": { "executable": executable },
"options": { "arg": [id], "idempotency_key": id }
}),
status,
scope_key: Some(scope.to_string()),
started_at: at,
updated_at: at,
..invocation(id, "execution command", at)
})
.unwrap();
}
let output = store
.query_invocation_patterns(&InvocationPatternQuery {
min_count: 2,
limit: 10,
scope_kind: Some(InvocationScopeKind::GitWorktree),
scope_key: Some("/tmp/a".to_string()),
..InvocationPatternQuery::default()
})
.unwrap();
assert_eq!(output.scanned, 4);
assert!(!output.truncated);
assert_eq!(output.patterns.len(), 1);
let pattern = &output.patterns[0];
assert_eq!(pattern.command_path, "execution command");
assert_eq!(pattern.occurrences, 3);
assert_eq!(pattern.succeeded, 1);
assert_eq!(pattern.failed, 1);
assert_eq!(pattern.incomplete, 1);
assert_eq!(pattern.first_seen, 100);
assert_eq!(pattern.last_seen, 300);
assert_eq!(
pattern.sample_invocation_ids,
["cargo-c", "cargo-b", "cargo-a"]
);
assert!(
pattern
.variable_paths
.contains(&"$.options.arg[]".to_string())
);
}
#[test]
fn searches_one_hundred_thousand_invocations_within_interactive_budget() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let value = serde_json::to_string(&invocation("fixture", "process list", 1)).unwrap();
store
.connection
.lock()
.unwrap()
.execute(
"WITH RECURSIVE sequence(value) AS (
VALUES(0)
UNION ALL
SELECT value + 1 FROM sequence WHERE value < 99999
)
INSERT INTO invocations(
id, source, surface, agent, command_path, normalized_input, search_text,
scope_kind, started_at, updated_at, status, value
)
SELECT
printf('benchmark-%06d', value), 'native', 'cli', 1, 'process list', '{}',
CASE WHEN value = 99999 THEN 'unique-history-needle' ELSE 'routine' END,
'cwd', value, value, 'succeeded', ?1
FROM sequence",
[value],
)
.unwrap();
let started = std::time::Instant::now();
let page = store
.query_invocations(&InvocationQuery {
limit: 50,
search: Some("unique-history-needle".to_string()),
..InvocationQuery::default()
})
.unwrap();
let elapsed = started.elapsed();
assert_eq!(page.invocations.len(), 1);
assert!(
elapsed < std::time::Duration::from_millis(100),
"100k-row history search took {elapsed:?}"
);
}
#[test]
fn filters_process_history_by_execution_and_operation() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let process = crate::process::process_info_for_pid(std::process::id()).unwrap();
let controller = ProcessController::capture(true, Some("execution-42".to_string()));
store
.record_processes(
std::slice::from_ref(&process),
"process.get",
"observed",
Some(&format!("pid:{}", process.identity.pid)),
&controller,
"observed",
Some("managed-7"),
&serde_json::json!({}),
)
.unwrap();
let events = store
.query_process_events(&ProcessEventQuery {
limit: 10,
operation: Some("process.get".to_string()),
controller_execution_id: Some("execution-42".to_string()),
execution_id: Some("managed-7".to_string()),
..ProcessEventQuery::default()
})
.unwrap();
assert_eq!(events.len(), 1);
assert_eq!(events[0].process_key, process_key(&process.identity));
assert_eq!(events[0].execution_id.as_deref(), Some("managed-7"));
assert!(
store
.query_process_events(&ProcessEventQuery {
limit: 10,
pid: Some(u32::MAX),
..ProcessEventQuery::default()
})
.unwrap()
.is_empty()
);
}
#[test]
fn preserves_managed_lineage_across_later_observations() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let process = crate::process::process_info_for_pid(std::process::id()).unwrap();
let controller = ProcessController::capture(true, Some("execution-42".to_string()));
store
.record_processes(
std::slice::from_ref(&process),
"execution.start",
"result",
None,
&controller,
"managed",
Some("run-42"),
&serde_json::json!({}),
)
.unwrap();
store
.record_processes(
std::slice::from_ref(&process),
"process.get",
"observed",
Some(&format!("pid:{}", process.identity.pid)),
&controller,
"observed",
None,
&serde_json::json!({}),
)
.unwrap();
let dossier = store
.process_dossier(&process_key(&process.identity), 10)
.unwrap();
assert_eq!(dossier.context.origin, "managed");
assert_eq!(dossier.context.execution_id.as_deref(), Some("run-42"));
assert_eq!(dossier.context.observations, 2);
}
#[test]
fn deduplicates_unchanged_process_snapshots_in_the_causal_journal() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let process = crate::process::process_info_for_pid(std::process::id()).unwrap();
let controller = ProcessController::capture(true, Some("execution-42".to_string()));
for _ in 0..10 {
store
.record_processes(
std::slice::from_ref(&process),
"process.get",
"observed",
None,
&controller,
"observed",
None,
&serde_json::json!({}),
)
.unwrap();
}
let connection = store.connection.lock().unwrap();
let payloads = connection
.query_row(
"SELECT COUNT(*) FROM causal_payloads WHERE kind = 'process_snapshot'",
[],
|row| row.get::<_, i64>(0),
)
.unwrap();
let events = connection
.query_row("SELECT COUNT(*) FROM causal_events", [], |row| {
row.get::<_, i64>(0)
})
.unwrap();
assert_eq!(payloads, 1);
assert_eq!(events, 10);
}
#[test]
fn gives_one_active_owner_each_reservation_key() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let first = Reservation {
id: "reservation-1".to_string(),
kind: ReservationKind::Port,
key: "tcp/5173".to_string(),
lease_id: "lease-1".to_string(),
created_at: 1,
expires_at: None,
external_conflict: false,
released: false,
};
let second = Reservation {
id: "reservation-2".to_string(),
lease_id: "lease-2".to_string(),
..first.clone()
};
store.acquire_reservation(&first).unwrap();
let error = store.acquire_reservation(&second).unwrap_err();
assert!(error.starts_with("RESERVATION_CONFLICT:"));
store.release_reservation(&first.id).unwrap();
store.acquire_reservation(&second).unwrap();
assert_eq!(
store.list_reservations(true).unwrap()[0].lease_id,
"lease-2"
);
}
#[test]
fn keeps_causal_event_cursors_monotonic() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
for subject in ["process:1", "process:2"] {
store
.append_causal_event(CausalEvent {
sequence: 0,
id: uuid::Uuid::now_v7().to_string(),
at: now_ms(),
kind: "process.observed".to_string(),
subject: subject.to_string(),
actor: "test".to_string(),
correlation_id: "test-correlation".to_string(),
causation_id: None,
execution_id: None,
session_id: None,
payload_id: None,
details: serde_json::json!({}),
})
.unwrap();
}
let first = store.causal_events(0, 1).unwrap();
let second = store.causal_events(first[0].sequence, 10).unwrap();
assert_eq!(first.len(), 1);
assert_eq!(second.len(), 1);
assert!(second[0].sequence > first[0].sequence);
}
#[test]
fn prunes_lru_payloads_to_a_size_cap_but_preserves_pins() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let pinned = store
.put_causal_payload("fixture", None, &serde_json::json!({ "value": "pinned" }))
.unwrap();
store.pin_causal_payload(&pinned.id, true).unwrap();
let disposable = store
.put_causal_payload(
"fixture",
None,
&serde_json::json!({ "value": "disposable" }),
)
.unwrap();
assert_eq!(store.prune_causal_payloads_to_size(0).unwrap(), 1);
assert!(store.causal_payload(&pinned.id).unwrap().is_some());
assert!(store.causal_payload(&disposable.id).unwrap().is_none());
}
fn invocation(id: &str, command_path: &str, at: u64) -> InvocationRecord {
InvocationRecord {
id: id.to_string(),
parent_id: None,
execution_id: Some(format!("execution-{id}")),
source: InvocationSource::Native,
source_id: None,
source_order: None,
source_path: None,
accepted_sequence: 0,
surface: InvocationSurface::Cli,
actor: Some("agent".to_string()),
agent: true,
command_path: command_path.to_string(),
purpose: None,
normalized_input: serde_json::json!({ "command": command_path }),
search_text: format!("{command_path} fixture input"),
pattern_signature: String::new(),
variable_paths: Vec::new(),
cwd: Some("/tmp/project".to_string()),
scope_kind: InvocationScopeKind::GitWorktree,
scope_key: Some("/tmp/project".to_string()),
started_at: at,
completed_at: Some(at + 10),
updated_at: at,
status: InvocationStatus::Succeeded,
exit_code: Some(0),
error_code: None,
error: None,
}
}
}