use std::collections::{BTreeMap, BTreeSet};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use async_trait::async_trait;
use incurs_codemode::{
ArtifactRef, ArtifactStore, ExecutionState, ExecutionStatus as CodeExecutionStatus,
RuntimeStore, Snippet,
};
use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use sha2::{Digest, Sha256};
use crate::debugger::{
DebuggerAfter, DebuggerArtifact, DebuggerBreakpoint, DebuggerCapture, DebuggerEvent,
DebuggerEvidenceTrace, DebuggerEvidenceTraceStatus, DebuggerFlow, DebuggerFlowPhase,
DebuggerFlowRun, DebuggerFlowStep, DebuggerMark, DebuggerMutationRecord, DebuggerMutationState,
DebuggerRecording, DebuggerRecordingPhase, DebuggerSelection, DebuggerSessionRecord,
DebuggerTap, DebuggerTapHit, DebuggerTraceItem, DebuggerWatch, DebuggerWatchHit, RrTrace,
RrTraceStatus,
};
use crate::environment::ScopedEnvironment;
use crate::hook::{
Hook, HookDelivery, HookDeliveryStatus, HookPhase, delivery_id, hook_storage_key,
matching_program,
};
use crate::model::{
AgentSession, ArtifactMetadata, CausalEvent, Execution, ExecutionStatus, HistoryPatternOutput,
HistoryQueryOutput, InvocationPage, InvocationPattern, InvocationPatternQuery, InvocationQuery,
InvocationRecord, InvocationScopeKind, InvocationStatus, InvocationSummary, InvocationSurface,
InvocationTimelinePage, Lease, LeaseOwnerKind, ManagedRun, OperationReceipt, PollIteration,
ProcessContext, ProcessController, ProcessDossier, ProcessEvent, ProcessEventQuery,
ProcessInfo, RecipeScopeKind, RecipeSuggestion, Recording, RecordingStatus, Reservation,
TelemetryCapture, TelemetrySample, now_ms,
};
use crate::pattern::invocation_pattern_signature;
use crate::profile::Profile;
use crate::recipe::{recipe_expired, recipe_generation, recipe_storage_key};
const ACTIVE_EXECUTION_COUNT_SQL: &str = "
SELECT (
SELECT COUNT(*) FROM execution_records e
WHERE json_extract(e.value, '$.status') IN ('queued', 'running', 'stalled', 'interrupted')
AND NOT EXISTS (SELECT 1 FROM runs r WHERE r.id = e.id)
) + (
SELECT COUNT(*) FROM runs r
WHERE (json_extract(r.value, '$.status') IN ('starting', 'running')
OR json_extract(r.value, '$.status') GLOB 'context_record_failed:*')
AND EXISTS (SELECT 1 FROM execution_records e WHERE e.id = r.id)
)";
pub struct Store {
connection: Mutex<Connection>,
path: PathBuf,
}
pub enum ExecutionClaim {
Claimed,
Replayed(Box<Execution>),
}
pub enum DebuggerMutationClaim {
Claimed,
Replayed(Box<DebuggerMutationRecord>),
}
mod gc;
mod observe;
pub use gc::{ExpiredLifetime, KEEP_LABEL, RetentionDeleted, SpaceReclaim};
const DATABASE_VERSION: u32 = 23;
impl Store {
pub fn put_hook(&self, hook: &Hook) -> Result<(), String> {
let key = hook_storage_key(hook.scope_kind, hook.scope_key.as_deref(), &hook.name)?;
self.put_named("hooks", "name", &key, hook)
}
pub fn list_hooks(&self) -> Result<Vec<Hook>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM hooks ORDER BY updated_at DESC, name")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn get_hook(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
name: &str,
) -> Result<Option<Hook>, String> {
let key = hook_storage_key(scope_kind, scope_key, name)?;
self.get_named("hooks", "name", &key)
}
pub fn delete_hook(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
name: &str,
) -> Result<bool, String> {
let key = hook_storage_key(scope_kind, scope_key, name)?;
self.delete_named("hooks", "name", &key)
}
pub fn put_decider(&self, decider: &crate::intent::Decider) -> Result<(), String> {
self.put_named("deciders", "name", &decider.name, decider)
}
pub fn list_deciders(&self) -> Result<Vec<crate::intent::Decider>, String> {
self.list_values("deciders", "updated_at DESC, name")
}
pub fn delete_decider(&self, name: &str) -> Result<bool, String> {
self.delete_named("deciders", "name", name)
}
pub fn put_intent_record(&self, record: &crate::intent::IntentRecord) -> Result<(), String> {
let value = serde_json::to_string(record).map_err(|error| error.to_string())?;
let state = serde_json::to_value(record.state)
.ok()
.and_then(|state| state.as_str().map(ToString::to_string))
.unwrap_or_default();
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO intents (id, at, verb, proceeded, state, execution_id, value)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
record.intent.id,
record.intent.at as i64,
record.intent.verb,
record.proceeded as i64,
state,
record.intent.lineage.execution_id,
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn list_intent_records(
&self,
limit: usize,
) -> Result<Vec<crate::intent::IntentRecord>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM intents ORDER BY at DESC, id DESC LIMIT ?1")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![limit as i64], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn enqueue_hook_deliveries(
&self,
invocation: &InvocationRecord,
phase: HookPhase,
hook_depth: u32,
) -> Result<Vec<HookDelivery>, String> {
let hooks = self.list_hooks()?;
let mut visible = BTreeMap::<String, Hook>::new();
for hook in hooks
.iter()
.filter(|hook| hook.scope_kind == RecipeScopeKind::Global)
{
if matching_program(hook, invocation, phase, hook_depth).is_some() {
visible.insert(hook.name.clone(), hook.clone());
}
}
for hook in hooks
.iter()
.filter(|hook| hook.scope_kind != RecipeScopeKind::Global)
{
if matching_program(hook, invocation, phase, hook_depth).is_some() {
visible.insert(hook.name.clone(), hook.clone());
}
}
let now = now_ms();
let mut deliveries = Vec::with_capacity(visible.len());
for hook in visible.into_values() {
let program = matching_program(&hook, invocation, phase, hook_depth)
.expect("visible hooks have matching programs")
.to_string();
let delivery = HookDelivery {
id: delivery_id(&hook, invocation, phase),
hook_name: hook.name.clone(),
hook_generation: hook.generation.clone(),
invocation_id: invocation.id.clone(),
phase,
program,
invocation: invocation.clone(),
status: HookDeliveryStatus::Pending,
attempts: 0,
max_attempts: hook.max_attempts.max(1),
timeout_ms: hook.timeout_ms.max(1),
next_attempt_at: now,
execution_id: None,
error: None,
created_at: now,
updated_at: now,
};
let value = serde_json::to_string(&delivery).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR IGNORE INTO hook_deliveries(
id, hook_name, invocation_id, phase, status, next_attempt_at, updated_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
delivery.id,
delivery.hook_name,
delivery.invocation_id,
enum_value(&delivery.phase)?,
enum_value(&delivery.status)?,
as_i64(delivery.next_attempt_at),
as_i64(delivery.updated_at),
value,
],
)
.map_err(|error| error.to_string())?;
deliveries.push(
self.get_hook_delivery(&delivery.id)?
.ok_or_else(|| format!("HOOK_DELIVERY_MISSING: {}", delivery.id))?,
);
}
Ok(deliveries)
}
pub fn get_hook_delivery(&self, id: &str) -> Result<Option<HookDelivery>, String> {
self.get_json_row("SELECT value FROM hook_deliveries WHERE id = ?1", id)
}
pub fn list_hook_deliveries(
&self,
hook: Option<&str>,
status: Option<HookDeliveryStatus>,
) -> Result<Vec<HookDelivery>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM hook_deliveries ORDER BY updated_at DESC, id")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
let mut deliveries = deserialize_rows::<HookDelivery>(rows)?;
if let Some(hook) = hook {
deliveries.retain(|delivery| delivery.hook_name == hook);
}
if let Some(status) = status {
deliveries.retain(|delivery| delivery.status == status);
}
Ok(deliveries)
}
pub fn running_hook_delivery_count(&self) -> Result<usize, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT COUNT(*) FROM hook_deliveries WHERE status = ?1",
[enum_value(&HookDeliveryStatus::Running)?],
|row| row.get::<_, i64>(0),
)
.map(|count| count.max(0) as usize)
.map_err(|error| error.to_string())
}
pub fn claim_hook_deliveries(
&self,
limit: usize,
at: u64,
) -> Result<Vec<HookDelivery>, String> {
if limit == 0 {
return Ok(Vec::new());
}
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let values = {
let mut statement = transaction
.prepare(
"SELECT value FROM hook_deliveries
WHERE status = ?1 AND next_attempt_at <= ?2
ORDER BY next_attempt_at, updated_at, id LIMIT ?3",
)
.map_err(|error| error.to_string())?;
statement
.query_map(
params![
enum_value(&HookDeliveryStatus::Pending)?,
as_i64(at),
limit.min(i64::MAX as usize) as i64,
],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
let mut claimed = Vec::with_capacity(values.len());
for value in values {
let mut delivery =
serde_json::from_str::<HookDelivery>(&value).map_err(|error| error.to_string())?;
delivery.status = HookDeliveryStatus::Running;
delivery.attempts = delivery.attempts.saturating_add(1);
delivery.updated_at = at;
delivery.error = None;
let value = serde_json::to_string(&delivery).map_err(|error| error.to_string())?;
let updated = transaction
.execute(
"UPDATE hook_deliveries
SET status = ?1, updated_at = ?2, value = ?3
WHERE id = ?4 AND status = ?5",
params![
enum_value(&HookDeliveryStatus::Running)?,
as_i64(at),
value,
delivery.id,
enum_value(&HookDeliveryStatus::Pending)?,
],
)
.map_err(|error| error.to_string())?;
if updated == 1 {
claimed.push(delivery);
}
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(claimed)
}
pub fn set_hook_delivery_execution(
&self,
id: &str,
execution_id: &str,
) -> Result<HookDelivery, String> {
let mut delivery = self
.get_hook_delivery(id)?
.ok_or_else(|| format!("HOOK_DELIVERY_NOT_FOUND: {id}"))?;
delivery.execution_id = Some(execution_id.to_string());
delivery.updated_at = now_ms();
self.put_hook_delivery(&delivery)?;
Ok(delivery)
}
pub fn finish_hook_delivery(
&self,
id: &str,
succeeded: bool,
error: Option<String>,
) -> Result<HookDelivery, String> {
self.finish_hook_delivery_with_execution(id, succeeded, error, None)
}
pub fn finish_hook_delivery_with_execution(
&self,
id: &str,
succeeded: bool,
error: Option<String>,
execution_id: Option<&str>,
) -> Result<HookDelivery, String> {
let mut delivery = self
.get_hook_delivery(id)?
.ok_or_else(|| format!("HOOK_DELIVERY_NOT_FOUND: {id}"))?;
if let Some(execution_id) = execution_id {
delivery.execution_id = Some(execution_id.to_string());
}
let now = now_ms();
if succeeded {
delivery.status = HookDeliveryStatus::Succeeded;
delivery.error = None;
} else if delivery.attempts < delivery.max_attempts {
let exponent = delivery.attempts.saturating_sub(1).min(16);
let backoff = 500_u64.saturating_mul(1_u64 << exponent).min(30_000);
delivery.status = HookDeliveryStatus::Pending;
delivery.next_attempt_at = now.saturating_add(backoff);
delivery.error = error;
} else {
delivery.status = HookDeliveryStatus::Failed;
delivery.error = error;
}
delivery.updated_at = now;
self.put_hook_delivery(&delivery)?;
Ok(delivery)
}
pub fn recover_hook_deliveries(&self) -> Result<Vec<HookDelivery>, String> {
let running = self.list_hook_deliveries(None, Some(HookDeliveryStatus::Running))?;
let mut recovered = Vec::with_capacity(running.len());
for mut delivery in running {
delivery.status = HookDeliveryStatus::Pending;
delivery.next_attempt_at = now_ms();
delivery.updated_at = delivery.next_attempt_at;
delivery.error = Some("HOOK_DAEMON_INTERRUPTED".to_string());
self.put_hook_delivery(&delivery)?;
recovered.push(delivery);
}
Ok(recovered)
}
fn put_hook_delivery(&self, delivery: &HookDelivery) -> Result<(), String> {
let value = serde_json::to_string(delivery).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"UPDATE hook_deliveries SET
hook_name = ?1, invocation_id = ?2, phase = ?3, status = ?4,
next_attempt_at = ?5, updated_at = ?6, value = ?7
WHERE id = ?8",
params![
delivery.hook_name,
delivery.invocation_id,
enum_value(&delivery.phase)?,
enum_value(&delivery.status)?,
as_i64(delivery.next_attempt_at),
as_i64(delivery.updated_at),
value,
delivery.id,
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
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 mut connection = Connection::open(&path).map_err(|error| error.to_string())?;
let prior_version = effective_database_version(&connection)?;
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())?;
let fresh = !connection
.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table')",
[],
|row| row.get::<_, bool>(0),
)
.map_err(|error| error.to_string())?;
if fresh {
connection
.pragma_update(None, "auto_vacuum", "INCREMENTAL")
.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 INDEX IF NOT EXISTS runs_active ON runs(id)
WHERE json_extract(value, '$.status') IN ('starting', 'running')
OR json_extract(value, '$.status') GLOB 'context_record_failed:*';
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 INDEX IF NOT EXISTS executions_running ON executions(id)
WHERE json_extract(value, '$.status') = 'running';
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 hooks (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS deciders (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS intents (
id TEXT PRIMARY KEY,
at INTEGER NOT NULL,
verb TEXT NOT NULL,
proceeded INTEGER NOT NULL,
state TEXT NOT NULL,
execution_id TEXT,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS intents_by_time ON intents(at DESC);
CREATE TABLE IF NOT EXISTS hook_deliveries (
id TEXT PRIMARY KEY,
hook_name TEXT NOT NULL,
invocation_id TEXT NOT NULL,
phase TEXT NOT NULL,
status TEXT NOT NULL,
next_attempt_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS hook_deliveries_pending
ON hook_deliveries(status, next_attempt_at, updated_at);
CREATE INDEX IF NOT EXISTS hook_deliveries_hook
ON hook_deliveries(hook_name, updated_at DESC);
CREATE INDEX IF NOT EXISTS hook_deliveries_invocation
ON hook_deliveries(invocation_id, phase);
CREATE TABLE IF NOT EXISTS environments (
scope TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS profiles (
name TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS profile_generations (
profile_name TEXT NOT NULL,
generation TEXT NOT NULL,
value TEXT NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY(profile_name, generation)
);
CREATE TABLE IF NOT EXISTS profile_roots (
root TEXT PRIMARY KEY,
profile_name TEXT NOT NULL,
created_at INTEGER NOT NULL,
FOREIGN KEY(profile_name) REFERENCES profiles(name) ON DELETE RESTRICT
);
CREATE INDEX IF NOT EXISTS profile_roots_profile
ON profile_roots(profile_name, root);
CREATE TABLE IF NOT EXISTS profile_settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS profile_controlled_keys (
name TEXT PRIMARY KEY,
created_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 recordings (
id TEXT PRIMARY KEY,
scope_digest TEXT NOT NULL,
name TEXT NOT NULL,
status TEXT NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS recordings_active_scope
ON recordings(scope_digest) WHERE status = 'active';
CREATE INDEX IF NOT EXISTS recordings_scope
ON recordings(scope_digest, updated_at DESC, id DESC);
CREATE UNIQUE INDEX IF NOT EXISTS recordings_scope_name
ON recordings(scope_digest, name);
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,
parent_id TEXT,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS execution_records_active ON execution_records(id)
WHERE json_extract(value, '$.status') IN ('queued', 'running', 'stalled', 'interrupted');
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 telemetry_captures (
id TEXT PRIMARY KEY,
execution_id TEXT NOT NULL UNIQUE,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS telemetry_samples (
capture_id TEXT NOT NULL,
sequence INTEGER NOT NULL,
value TEXT NOT NULL,
bytes INTEGER NOT NULL,
sampled_at INTEGER NOT NULL,
PRIMARY KEY(capture_id, sequence),
FOREIGN KEY(capture_id) REFERENCES telemetry_captures(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS telemetry_samples_time
ON telemetry_samples(capture_id, sampled_at, sequence);
CREATE TABLE IF NOT EXISTS telemetry_receipts (
idempotency_key TEXT PRIMARY KEY,
operation TEXT NOT NULL,
execution_id TEXT NOT NULL,
value TEXT NOT NULL,
created_at INTEGER NOT NULL
);
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 INDEX IF NOT EXISTS leases_expiry
ON leases(released, expires_at);
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 capability_snapshots (
fingerprint TEXT PRIMARY KEY,
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 INDEX IF NOT EXISTS causal_payloads_retained_size
ON causal_payloads(pruned, bytes);
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 INDEX IF NOT EXISTS causal_events_time
ON causal_events(at, sequence);
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);
CREATE TABLE IF NOT EXISTS debugger_sessions (
id TEXT PRIMARY KEY,
desired_state TEXT NOT NULL,
adapter TEXT NOT NULL,
execution_id TEXT,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_sessions_desired
ON debugger_sessions(desired_state, updated_at DESC);
CREATE INDEX IF NOT EXISTS debugger_sessions_execution
ON debugger_sessions(execution_id, updated_at DESC);
CREATE TABLE IF NOT EXISTS debugger_selections (
actor TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY(session_id) REFERENCES debugger_sessions(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS debugger_breakpoints (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL,
FOREIGN KEY(session_id) REFERENCES debugger_sessions(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS debugger_breakpoints_session
ON debugger_breakpoints(session_id, updated_at, id);
CREATE TABLE IF NOT EXISTS debugger_mutations (
id TEXT PRIMARY KEY,
idempotency_key TEXT NOT NULL UNIQUE,
operation TEXT NOT NULL,
fingerprint TEXT NOT NULL,
session_id TEXT NOT NULL,
state TEXT NOT NULL,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_mutations_session
ON debugger_mutations(session_id, updated_at DESC);
CREATE TABLE IF NOT EXISTS debugger_events (
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
id TEXT NOT NULL UNIQUE,
session_id TEXT NOT NULL,
execution_id TEXT,
at INTEGER NOT NULL,
kind TEXT NOT NULL,
details TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_events_session
ON debugger_events(session_id, sequence DESC);
CREATE INDEX IF NOT EXISTS debugger_events_execution
ON debugger_events(execution_id, sequence DESC);
CREATE TABLE IF NOT EXISTS debugger_artifacts (
id TEXT NOT NULL,
session_id TEXT NOT NULL,
execution_id TEXT,
kind TEXT NOT NULL,
media_type TEXT NOT NULL,
path TEXT NOT NULL,
bytes INTEGER NOT NULL,
created_at INTEGER NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY(id, session_id)
);
CREATE INDEX IF NOT EXISTS debugger_artifacts_session
ON debugger_artifacts(session_id, created_at DESC);
CREATE INDEX IF NOT EXISTS debugger_artifacts_execution
ON debugger_artifacts(execution_id, created_at DESC);
CREATE TABLE IF NOT EXISTS debugger_recordings (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
execution_id TEXT,
status TEXT NOT NULL,
started_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS debugger_recordings_active_session
ON debugger_recordings(session_id) WHERE status = 'running';
CREATE INDEX IF NOT EXISTS debugger_recordings_session
ON debugger_recordings(session_id, started_at DESC);
CREATE TABLE IF NOT EXISTS debugger_captures (
id TEXT PRIMARY KEY,
recording_id TEXT NOT NULL,
session_id TEXT NOT NULL,
sequence INTEGER NOT NULL,
at INTEGER NOT NULL,
artifact_id TEXT NOT NULL,
bytes INTEGER NOT NULL,
value TEXT NOT NULL,
UNIQUE(recording_id, sequence)
);
CREATE INDEX IF NOT EXISTS debugger_captures_recording
ON debugger_captures(recording_id, sequence);
CREATE INDEX IF NOT EXISTS debugger_captures_session
ON debugger_captures(session_id, at DESC);
CREATE TABLE IF NOT EXISTS debugger_marks (
id TEXT PRIMARY KEY,
recording_id TEXT NOT NULL,
session_id TEXT NOT NULL,
name TEXT NOT NULL,
capture_id TEXT NOT NULL,
at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_marks_recording
ON debugger_marks(recording_id, at);
CREATE TABLE IF NOT EXISTS debugger_afters (
id TEXT PRIMARY KEY,
recording_id TEXT NOT NULL,
session_id TEXT NOT NULL,
before_capture_id TEXT NOT NULL,
after_capture_id TEXT NOT NULL,
report_id TEXT NOT NULL,
at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_afters_recording
ON debugger_afters(recording_id, at DESC);
CREATE TABLE IF NOT EXISTS debugger_flows (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
recording_id TEXT NOT NULL,
name TEXT NOT NULL,
phase TEXT NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS debugger_flows_active_session
ON debugger_flows(session_id) WHERE phase = 'recording';
CREATE INDEX IF NOT EXISTS debugger_flows_name
ON debugger_flows(session_id, name, updated_at DESC);
CREATE TABLE IF NOT EXISTS debugger_flow_steps (
id TEXT PRIMARY KEY,
flow_id TEXT NOT NULL,
sequence INTEGER NOT NULL,
at INTEGER NOT NULL,
value TEXT NOT NULL,
UNIQUE(flow_id, sequence)
);
CREATE INDEX IF NOT EXISTS debugger_flow_steps_flow
ON debugger_flow_steps(flow_id, sequence);
CREATE TABLE IF NOT EXISTS debugger_flow_runs (
id TEXT PRIMARY KEY,
flow_id TEXT NOT NULL,
session_id TEXT NOT NULL,
status TEXT NOT NULL,
started_at INTEGER NOT NULL,
completed_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_flow_runs_flow
ON debugger_flow_runs(flow_id, started_at);
CREATE TABLE IF NOT EXISTS debugger_taps (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
backend_id TEXT,
enabled INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_taps_session
ON debugger_taps(session_id, enabled, updated_at, id);
CREATE TABLE IF NOT EXISTS debugger_tap_hits (
id TEXT PRIMARY KEY,
tap_id TEXT NOT NULL,
session_id TEXT NOT NULL,
at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_tap_hits_tap
ON debugger_tap_hits(tap_id, at DESC, id DESC);
CREATE TABLE IF NOT EXISTS debugger_watches (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
enabled INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_watches_session
ON debugger_watches(session_id, enabled, updated_at, id);
CREATE TABLE IF NOT EXISTS debugger_watch_hits (
id TEXT PRIMARY KEY,
watch_id TEXT NOT NULL,
session_id TEXT NOT NULL,
at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_watch_hits_watch
ON debugger_watch_hits(watch_id, at DESC, id DESC);
CREATE TABLE IF NOT EXISTS debugger_evidence_traces (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
status TEXT NOT NULL,
started_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS debugger_evidence_traces_active_session
ON debugger_evidence_traces(session_id) WHERE status = 'recording';
CREATE INDEX IF NOT EXISTS debugger_evidence_traces_recent
ON debugger_evidence_traces(updated_at DESC, id DESC);
CREATE TABLE IF NOT EXISTS debugger_evidence_trace_selections (
actor TEXT PRIMARY KEY,
trace_id TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS debugger_trace_import_items (
trace_id TEXT NOT NULL,
sequence INTEGER NOT NULL,
at INTEGER NOT NULL,
kind TEXT NOT NULL,
item_id TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY(trace_id, sequence)
);
CREATE INDEX IF NOT EXISTS debugger_trace_import_items_time
ON debugger_trace_import_items(trace_id, at, sequence);
CREATE TABLE IF NOT EXISTS debugger_rr_traces (
id TEXT PRIMARY KEY,
status TEXT NOT NULL,
started_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
value TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS debugger_rr_traces_status
ON debugger_rr_traces(status, updated_at DESC, id 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")?;
ensure_column(&connection, "invocations", "recording_id", "TEXT")?;
ensure_column(&connection, "invocations", "recording_take", "INTEGER")?;
let added_execution_parent =
ensure_column(&connection, "execution_records", "parent_id", "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)?;
}
if added_execution_parent {
connection
.execute(
"UPDATE execution_records
SET parent_id = json_extract(value, '$.parent_id')
WHERE parent_id IS NULL",
[],
)
.map_err(|error| error.to_string())?;
}
connection
.execute(
"CREATE INDEX IF NOT EXISTS invocations_timeline
ON invocations(started_at, accepted_sequence, id)",
[],
)
.map_err(|error| error.to_string())?;
connection
.execute(
"CREATE INDEX IF NOT EXISTS invocations_recording
ON invocations(recording_id, recording_take, started_at, accepted_sequence, id)",
[],
)
.map_err(|error| error.to_string())?;
connection
.execute(
"CREATE INDEX IF NOT EXISTS execution_records_parent
ON execution_records(parent_id, updated_at DESC, id DESC)",
[],
)
.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())?;
migrate_scoped_environments(&mut connection, prior_version)?;
observe::migrate(&mut connection)?;
migrate_execution_listing(&mut connection, prior_version)?;
gc::migrate(&connection)?;
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(crate) fn put_run(&self, run: &ManagedRun) -> Result<(), String> {
self.put_named("runs", "id", &run.id, run)
}
pub(crate) fn get_run(&self, id: &str) -> Result<Option<ManagedRun>, String> {
self.get_named("runs", "id", id)
}
#[cfg(test)]
pub(crate) fn list_runs(&self) -> Result<Vec<ManagedRun>, String> {
self.list_values("runs", "updated_at DESC")
}
pub(crate) fn recoverable_runs(&self) -> Result<Vec<ManagedRun>, String> {
self.filtered_values(
"SELECT value FROM runs
WHERE json_extract(value, '$.status') IN ('starting', 'running')
OR json_extract(value, '$.status') GLOB 'context_record_failed:*'",
[],
)
}
pub(crate) 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 claim_receipt<T>(&self, id: &str, kind: &str, value: &T) -> Result<Option<T>, String>
where
T: Serialize + DeserializeOwned,
{
let encoded = serde_json::to_string(value).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let existing = transaction
.query_row(
"SELECT kind, value FROM receipts WHERE id = ?1",
[id],
|row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)),
)
.optional()
.map_err(|error| error.to_string())?;
if let Some((existing_kind, existing_value)) = existing {
if existing_kind != kind {
return Err(format!(
"RECEIPT_KIND_CONFLICT: key \"{id}\" belongs to {existing_kind}, not {kind}"
));
}
let existing =
serde_json::from_str(&existing_value).map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
return Ok(Some(existing));
}
transaction
.execute(
"INSERT INTO receipts(id, kind, value, created_at) VALUES (?1, ?2, ?3, ?4)",
params![id, kind, encoded, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(None)
}
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_profile(&self, profile: &Profile) -> Result<(), String> {
let value = serde_json::to_string(profile).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO profiles(name, value, updated_at) VALUES (?1, ?2, ?3)
ON CONFLICT(name) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at",
params![profile.name, value, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT OR IGNORE INTO profile_generations(profile_name, generation, value, created_at)
VALUES (?1, ?2, ?3, ?4)",
params![profile.name, profile.generation, value, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
for name in profile.literals.keys().chain(profile.secrets.keys()) {
transaction
.execute(
"INSERT OR IGNORE INTO profile_controlled_keys(name, created_at) VALUES (?1, ?2)",
params![name, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(())
}
pub(crate) fn get_profile(&self, name: &str) -> Result<Option<Profile>, String> {
self.get_named("profiles", "name", name)
}
pub(crate) fn get_profile_generation(
&self,
name: &str,
generation: &str,
) -> Result<Option<Profile>, String> {
let value = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM profile_generations WHERE profile_name = ?1 AND generation = ?2",
params![name, generation],
|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(crate) fn list_profiles(&self) -> Result<Vec<Profile>, String> {
self.list_values("profiles", "name ASC")
}
pub(crate) fn delete_profile(&self, name: &str) -> Result<bool, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let roots = transaction
.query_row(
"SELECT COUNT(*) FROM profile_roots WHERE profile_name = ?1",
[name],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?;
let is_default = transaction
.query_row(
"SELECT EXISTS(SELECT 1 FROM profile_settings WHERE key = 'default' AND value = ?1)",
[name],
|row| row.get::<_, bool>(0),
)
.map_err(|error| error.to_string())?;
if roots > 0 || is_default {
return Err(format!(
"PROFILE_IN_USE: profile \"{name}\" still owns roots or the user default"
));
}
let removed = transaction
.execute("DELETE FROM profiles WHERE name = ?1", [name])
.map_err(|error| error.to_string())?
> 0;
transaction.commit().map_err(|error| error.to_string())?;
Ok(removed)
}
pub(crate) fn put_profile_root(&self, profile: &str, root: &str) -> Result<(), String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let existing = connection
.query_row(
"SELECT profile_name FROM profile_roots WHERE root = ?1",
[root],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
if let Some(existing) = existing
&& existing != profile
{
return Err(format!(
"PROFILE_ROOT_CONFLICT: root \"{root}\" already belongs to profile \"{existing}\""
));
}
connection
.execute(
"INSERT OR IGNORE INTO profile_roots(root, profile_name, created_at) VALUES (?1, ?2, ?3)",
params![root, profile, as_i64(now_ms())],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub(crate) fn delete_profile_root(&self, profile: &str, root: &str) -> Result<bool, String> {
Ok(self
.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"DELETE FROM profile_roots WHERE root = ?1 AND profile_name = ?2",
params![root, profile],
)
.map_err(|error| error.to_string())?
> 0)
}
pub(crate) fn list_profile_roots(&self) -> Result<Vec<(String, String)>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT root, profile_name FROM profile_roots ORDER BY root ASC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| {
Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
})
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
pub(crate) fn roots_for_profile(&self, profile: &str) -> Result<Vec<String>, String> {
Ok(self
.list_profile_roots()?
.into_iter()
.filter_map(|(root, owner)| (owner == profile).then_some(root))
.collect())
}
pub(crate) fn default_profile(&self) -> Result<Option<String>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM profile_settings WHERE key = 'default'",
[],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())
}
pub(crate) fn set_default_profile(&self, profile: Option<&str>) -> Result<(), String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
match profile {
Some(profile) => {
let exists = transaction
.query_row(
"SELECT EXISTS(SELECT 1 FROM profiles WHERE name = ?1)",
[profile],
|row| row.get::<_, bool>(0),
)
.map_err(|error| error.to_string())?;
if !exists {
return Err(format!(
"PROFILE_NOT_FOUND: profile \"{profile}\" does not exist"
));
}
transaction
.execute(
"INSERT INTO profile_settings(key, value, updated_at) VALUES ('default', ?1, ?2)
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at",
params![profile, as_i64(now_ms())],
)
.map_err(|error| error.to_string())?;
}
None => {
transaction
.execute("DELETE FROM profile_settings WHERE key = 'default'", [])
.map_err(|error| error.to_string())?;
}
}
transaction.commit().map_err(|error| error.to_string())
}
pub(crate) fn controlled_profile_keys(&self) -> Result<Vec<String>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT name FROM profile_controlled_keys ORDER BY name ASC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
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_recording(&self, recording: &Recording) -> Result<(), String> {
let digest = recording_scope_digest(recording.scope_kind, recording.scope_key.as_deref());
let value = serde_json::to_string(recording).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO recordings(id, scope_digest, name, status, value, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT(id) DO UPDATE SET
scope_digest = excluded.scope_digest,
name = excluded.name,
status = excluded.status,
value = excluded.value,
updated_at = excluded.updated_at",
params![
recording.id,
digest,
recording.name,
enum_value(&recording.status)?,
value,
as_i64(recording.updated_at)
],
)
.map_err(|error| {
let error = error.to_string();
if recording.status == RecordingStatus::Active
&& (error.contains("recordings_active_scope")
|| error.contains("UNIQUE constraint failed: recordings.scope_digest"))
{
"RECORDING_ACTIVE: One recording is already active in this scope".to_string()
} else {
error
}
})?;
Ok(())
}
pub fn publish_recording_stop(
&self,
recording: &Recording,
recipe: Option<&crate::model::Recipe>,
suggestion: Option<&RecipeSuggestion>,
receipt_id: &str,
receipt_kind: &str,
receipt: &impl Serialize,
) -> Result<(), String> {
if recipe.is_some() && suggestion.is_some() {
return Err("A recording stop cannot publish both a recipe and suggestion".to_string());
}
let recording_digest =
recording_scope_digest(recording.scope_kind, recording.scope_key.as_deref());
let recording_value =
serde_json::to_string(recording).map_err(|error| error.to_string())?;
let recording_status = enum_value(&recording.status)?;
let recipe = recipe
.map(|recipe| {
Ok::<_, String>((
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),
))
})
.transpose()?;
let suggestion = suggestion
.map(|suggestion| {
Ok::<_, String>((
suggestion.id.clone(),
serde_json::to_string(suggestion).map_err(|error| error.to_string())?,
as_i64(suggestion.created_at),
))
})
.transpose()?;
let receipt_value = serde_json::to_string(receipt).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
if let Some((key, value, updated_at)) = recipe {
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())?;
}
if let Some((id, value, created_at)) = suggestion {
transaction
.execute(
"INSERT INTO recipe_suggestions(id, value, created_at) VALUES (?1, ?2, ?3)",
params![id, value, created_at],
)
.map_err(|error| error.to_string())?;
}
transaction
.execute(
"INSERT OR REPLACE INTO recordings(id, scope_digest, name, status, value, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
recording.id,
recording_digest,
recording.name,
recording_status,
recording_value,
as_i64(recording.updated_at)
],
)
.map_err(|error| {
let error = error.to_string();
if error.contains("recordings_active_scope") {
"RECORDING_ACTIVE: One recording is already active in this scope".to_string()
} else {
error
}
})?;
transaction
.execute(
"INSERT OR REPLACE INTO receipts(id, kind, value, created_at)
VALUES (?1, ?2, ?3, ?4)",
params![receipt_id, receipt_kind, receipt_value, as_i64(now_ms())],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())
}
pub fn get_recording(&self, id: &str) -> Result<Option<Recording>, String> {
self.get_json_row("SELECT value FROM recordings WHERE id = ?1", id)
}
pub fn active_recording(
&self,
scope_kind: crate::model::InvocationScopeKind,
scope_key: Option<&str>,
) -> Result<Option<Recording>, String> {
let digest = invocation_scope_digest(scope_kind, scope_key);
if let Some(recording) = self.active_recording_for_digest(&digest)? {
return Ok(Some(recording));
}
self.active_recording_for_digest("global")
}
pub fn active_recording_in_recipe_scope(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
) -> Result<Option<Recording>, String> {
let digest = recording_scope_digest(scope_kind, scope_key);
self.active_recording_for_digest(&digest)
}
fn active_recording_for_digest(&self, digest: &str) -> Result<Option<Recording>, String> {
self.get_json_row(
"SELECT value FROM recordings WHERE scope_digest = ?1 AND status = 'active'",
digest,
)
}
pub fn get_recording_by_name(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
name: &str,
) -> Result<Option<Recording>, String> {
let digest = recording_scope_digest(scope_kind, scope_key);
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM recordings WHERE scope_digest = ?1 AND name = ?2",
params![digest, name],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn list_recordings(
&self,
scope_kind: RecipeScopeKind,
scope_key: Option<&str>,
) -> Result<Vec<Recording>, String> {
let digest = recording_scope_digest(scope_kind, scope_key);
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM recordings WHERE scope_digest = ?1
ORDER BY updated_at DESC, id DESC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([digest], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows::<Recording>(rows)
}
pub fn delete_recording(&self, id: &str) -> Result<bool, String> {
Ok(self
.connection
.lock()
.map_err(|error| error.to_string())?
.execute("DELETE FROM recordings WHERE id = ?1", [id])
.map_err(|error| error.to_string())?
> 0)
}
pub fn recording_invocations(
&self,
recording_id: &str,
take: u32,
) -> Result<Vec<InvocationRecord>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM invocations
WHERE recording_id = ?1 AND recording_take = ?2
ORDER BY started_at, accepted_sequence, id",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![recording_id, take], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows::<InvocationRecord>(rows)
}
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_execution_record(&self, execution: &Execution) -> Result<(), String> {
let value = serde_json::to_string(execution).map_err(|error| error.to_string())?;
let verdict = crate::model::ExecutionVerdict::of_execution(execution);
let failing = matches!(
verdict,
crate::model::ExecutionVerdict::Failed | crate::model::ExecutionVerdict::Error
);
let changed = {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let changed = if failing {
let previous = 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())?;
previous
.and_then(|value| serde_json::from_str::<Execution>(&value).ok())
.is_none_or(|previous| {
crate::model::ExecutionVerdict::of_execution(&previous) != verdict
|| previous.recorded_error_code() != execution.recorded_error_code()
})
} else {
false
};
transaction
.execute(
"INSERT INTO execution_records(id, parent_id, value, updated_at)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(id) DO UPDATE SET
parent_id = excluded.parent_id,
value = excluded.value,
updated_at = excluded.updated_at",
params![
execution.id,
execution.parent_id,
value,
execution.updated_at as i64
],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
changed
};
if changed {
let code = execution
.recorded_error_code()
.unwrap_or("EXECUTION_FAILED");
crate::error_telemetry::record_at(
self.path.parent().unwrap_or(Path::new(".")),
"execution",
"execution",
code,
None,
Some(&execution.id),
code,
);
}
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_with_behavior(TransactionBehavior::Immediate)
.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)));
}
validate_execution_owner(&transaction, execution)?;
transaction
.execute(
"INSERT INTO execution_records(id, parent_id, value, updated_at)
VALUES (?1, ?2, ?3, ?4)",
params![
execution.id,
execution.parent_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 accept_execution(&self, execution: &Execution) -> Result<(), 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_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
validate_execution_owner(&transaction, execution)?;
transaction
.execute(
"INSERT INTO execution_records(id, parent_id, value, updated_at)
VALUES (?1, ?2, ?3, ?4)",
params![
execution.id,
execution.parent_id,
value,
execution.updated_at as i64
],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())
}
pub fn get_execution_record(&self, id: &str) -> Result<Option<Execution>, String> {
self.get_named("execution_records", "id", id)
}
pub fn put_telemetry_capture(&self, capture: &TelemetryCapture) -> Result<(), String> {
let value = serde_json::to_string(capture).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO telemetry_captures(id, execution_id, value, updated_at)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(id) DO UPDATE SET
execution_id = excluded.execution_id,
value = excluded.value,
updated_at = excluded.updated_at",
params![capture.id, capture.execution_id, value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn get_telemetry_capture(&self, id: &str) -> Result<Option<TelemetryCapture>, String> {
self.get_json_row(
"SELECT value FROM telemetry_captures WHERE id = ?1 OR execution_id = ?1",
id,
)
}
pub fn append_telemetry_sample(
&self,
capture_id: &str,
sample: TelemetrySample,
) -> Result<TelemetrySample, String> {
self.append_telemetry_samples(capture_id, vec![sample])?
.pop()
.ok_or_else(|| "Telemetry sample batch was empty".to_string())
}
pub fn append_telemetry_samples(
&self,
capture_id: &str,
mut samples: Vec<TelemetrySample>,
) -> Result<Vec<TelemetrySample>, String> {
if samples.is_empty() {
return Ok(samples);
}
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let encoded = transaction
.query_row(
"SELECT value FROM telemetry_captures WHERE id = ?1",
[capture_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.ok_or_else(|| format!("Telemetry capture \"{capture_id}\" not found"))?;
let mut capture: TelemetryCapture =
serde_json::from_str(&encoded).map_err(|error| error.to_string())?;
for sample in &mut samples {
sample.sequence = capture.next_sequence;
let value = serde_json::to_string(&sample).map_err(|error| error.to_string())?;
let bytes = value.len() as u64 + 1;
transaction
.execute(
"INSERT INTO telemetry_samples(capture_id, sequence, value, bytes, sampled_at)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![
capture_id,
sample.sequence.min(i64::MAX as u64) as i64,
value,
bytes.min(i64::MAX as u64) as i64,
sample.sampled_at.min(i64::MAX as u64) as i64
],
)
.map_err(|error| error.to_string())?;
capture.next_sequence = capture.next_sequence.saturating_add(1);
capture.sample_count = capture.sample_count.saturating_add(1);
capture.retained_bytes = capture.retained_bytes.saturating_add(bytes);
}
while capture.retained_bytes > capture.max_bytes {
let oldest = transaction
.query_row(
"SELECT sequence, bytes FROM telemetry_samples
WHERE capture_id = ?1 ORDER BY sequence ASC LIMIT 1",
[capture_id],
|row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)?)),
)
.optional()
.map_err(|error| error.to_string())?;
let Some((sequence, removed_bytes)) = oldest else {
break;
};
transaction
.execute(
"DELETE FROM telemetry_samples WHERE capture_id = ?1 AND sequence = ?2",
params![capture_id, sequence],
)
.map_err(|error| error.to_string())?;
capture.retained_bytes = capture
.retained_bytes
.saturating_sub(removed_bytes.max(0) as u64);
capture.sample_count = capture.sample_count.saturating_sub(1);
capture.dropped_samples = capture.dropped_samples.saturating_add(1);
}
let capture_value = serde_json::to_string(&capture).map_err(|error| error.to_string())?;
transaction
.execute(
"UPDATE telemetry_captures SET value = ?2, updated_at = ?3 WHERE id = ?1",
params![capture_id, capture_value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(samples)
}
pub fn telemetry_samples(&self, capture_id: &str) -> Result<Vec<TelemetrySample>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM telemetry_samples
WHERE capture_id = ?1 ORDER BY sequence ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([capture_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn claim_telemetry_receipt(
&self,
key: &str,
operation: &str,
execution_id: &str,
capture: &TelemetryCapture,
) -> Result<TelemetryCapture, String> {
if key.is_empty() || key.len() > 256 {
return Err(
"Telemetry idempotency keys must contain between 1 and 256 bytes".to_string(),
);
}
let value = serde_json::to_string(capture).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let existing = transaction
.query_row(
"SELECT operation, execution_id, value FROM telemetry_receipts
WHERE idempotency_key = ?1",
[key],
|row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
))
},
)
.optional()
.map_err(|error| error.to_string())?;
if let Some((existing_operation, existing_execution, existing_value)) = existing {
if existing_operation != operation || existing_execution != execution_id {
return Err(format!(
"TELEMETRY_IDEMPOTENCY_CONFLICT: key \"{key}\" identifies {existing_operation} for execution \"{existing_execution}\""
));
}
let existing =
serde_json::from_str(&existing_value).map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
return Ok(existing);
}
transaction
.execute(
"INSERT INTO telemetry_receipts(
idempotency_key, operation, execution_id, value, created_at
) VALUES (?1, ?2, ?3, ?4, ?5)",
params![key, operation, execution_id, value, now_ms() as i64],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(capture.clone())
}
pub fn telemetry_receipt(
&self,
key: &str,
) -> Result<Option<(String, String, TelemetryCapture)>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let row = connection
.query_row(
"SELECT operation, execution_id, value FROM telemetry_receipts
WHERE idempotency_key = ?1",
[key],
|row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
))
},
)
.optional()
.map_err(|error| error.to_string())?;
row.map(|(operation, execution_id, value)| {
serde_json::from_str(&value)
.map(|capture| (operation, execution_id, capture))
.map_err(|error| error.to_string())
})
.transpose()
}
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 recoverable_execution_records(&self) -> Result<Vec<Execution>, String> {
self.filtered_values(
"SELECT value FROM execution_records
WHERE json_extract(value, '$.status') IN ('queued', 'running', 'stalled', 'interrupted')",
[],
)
}
pub fn execution_children(&self, parent_id: &str) -> Result<Vec<Execution>, String> {
self.filtered_values(
"SELECT value FROM execution_records WHERE parent_id = ?1 ORDER BY updated_at DESC, id DESC",
[parent_id],
)
}
pub fn execution_family_ids(&self, root_id: &str) -> Result<Vec<String>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection.prepare(
"WITH RECURSIVE family(id) AS (
SELECT ?1
UNION
SELECT child.id FROM execution_records child JOIN family ON child.parent_id = family.id
) SELECT id FROM family",
).map_err(|error| error.to_string())?;
statement
.query_map([root_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
pub fn execution_family(&self, root_id: &str) -> Result<Vec<Execution>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"WITH RECURSIVE family(id, depth) AS (
SELECT id, 0 FROM execution_records WHERE id = ?1
UNION ALL
SELECT child.id, family.depth + 1
FROM execution_records child
JOIN family ON child.parent_id = family.id
)
SELECT execution_records.value
FROM family
JOIN execution_records ON execution_records.id = family.id
ORDER BY family.depth ASC, execution_records.updated_at ASC, execution_records.id ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([root_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn list_telemetry_captures(&self) -> Result<Vec<TelemetryCapture>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM telemetry_captures ORDER BY updated_at DESC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
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,
recording_id, recording_take, 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, ?29, ?30
)
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,
recording_id = excluded.recording_id,
recording_take = excluded.recording_take,
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,
invocation.recording_id,
invocation.recording_take,
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.error_code {
sql.push_str(" AND error_code = ?");
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.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.error_code {
sql.push_str(" AND error_code = ?");
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.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 active_execution_count(&self) -> Result<usize, String> {
let count = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(ACTIVE_EXECUTION_COUNT_SQL, [], |row| row.get::<_, i64>(0))
.map_err(|error| error.to_string())?;
usize::try_from(count).map_err(|error| error.to_string())
}
pub fn interrupt_code_execution(
&self,
id: &str,
error: &str,
) -> Result<Option<ExecutionState>, String> {
let Some(mut execution) = self.get_named::<ExecutionState>("executions", "id", id)? else {
return Ok(None);
};
if matches!(
execution.status,
CodeExecutionStatus::Running | CodeExecutionStatus::Paused
) {
execution.status = CodeExecutionStatus::Error;
execution.error = Some(error.to_owned());
execution.updated_at = now_ms();
self.put_named("executions", "id", id, &execution)?;
}
Ok(Some(execution))
}
pub fn interrupt_orphaned_code_executions(&self) -> Result<usize, String> {
let executions: Vec<ExecutionState> = self.filtered_values(
"SELECT value FROM executions WHERE json_extract(value, '$.status') = 'running'",
[],
)?;
let now = now_ms();
let mut closed = 0;
for mut execution in executions {
if execution.status != CodeExecutionStatus::Running {
continue;
}
execution.status = CodeExecutionStatus::Error;
execution.error = Some(
"INTERRUPTED: the daemon running this Code Mode program exited before it finished"
.to_owned(),
);
execution.updated_at = now;
self.put_named("executions", "id", &execution.id.clone(), &execution)?;
closed += 1;
}
Ok(closed)
}
pub fn list_execution_records_page(
&self,
before: Option<(u64, &str)>,
status: Option<&str>,
parent_id: Option<&str>,
error_code: Option<&str>,
limit: usize,
) -> Result<Vec<Execution>, String> {
let mut query = String::from(
"SELECT e.value FROM execution_listing l
JOIN execution_records e ON e.id = l.id WHERE 1",
);
let mut values = Vec::<rusqlite::types::Value>::new();
for (column, value) in [
("status", status),
("parent_id", parent_id),
("error_code", error_code),
] {
if let Some(value) = value {
query.push_str(&format!(" AND l.{column} = ?"));
values.push(value.to_string().into());
}
}
if let Some((at, id)) = before {
query.push_str(" AND (l.updated_at, l.id) < (?, ?)");
values.push((at as i64).into());
values.push(id.to_string().into());
}
query.push_str(" ORDER BY l.updated_at DESC, l.id DESC LIMIT ?");
values.push((limit as i64).into());
let rows = {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(&query)
.map_err(|error| error.to_string())?;
statement
.query_map(rusqlite::params_from_iter(values), |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
rows.into_iter()
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.collect()
}
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())?;
transaction
.execute(
"DELETE FROM telemetry_samples
WHERE capture_id IN (
SELECT id FROM telemetry_captures WHERE execution_id = ?1
)",
[id],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM telemetry_captures WHERE execution_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 || session.closing,
value,
now_ms() as i64
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn begin_session_close(&self, id: &str) -> Result<(AgentSession, bool), String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let value = transaction
.query_row(
"SELECT value FROM agent_sessions WHERE id = ?1",
[id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.ok_or_else(|| format!("Session \"{id}\" not found"))?;
let mut session: AgentSession =
serde_json::from_str(&value).map_err(|error| error.to_string())?;
if session.closed || session.closing {
transaction.commit().map_err(|error| error.to_string())?;
return Ok((session, false));
}
session.closing = true;
let value = serde_json::to_string(&session).map_err(|error| error.to_string())?;
transaction
.execute(
"UPDATE agent_sessions
SET closed = 1, value = ?2, updated_at = ?3
WHERE id = ?1",
params![id, value, now_ms().min(i64::MAX as u64) as i64],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok((session, true))
}
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_expired_leases(&self, cutoff: u64) -> Result<Vec<Lease>, String> {
let values = {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM leases WHERE released = 0 AND expires_at <= ?1
ORDER BY updated_at DESC",
)
.map_err(|error| error.to_string())?;
statement
.query_map([cutoff.min(i64::MAX as u64) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
values
.into_iter()
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.collect()
}
pub fn list_leases(&self, active_only: bool) -> Result<Vec<Lease>, String> {
let values = {
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())?;
statement
.query_map([now_ms().min(i64::MAX as u64) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
values
.into_iter()
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.collect()
}
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 mut 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
));
}
current.released = true;
transaction
.execute(
"UPDATE reservations SET released = 1, value = ?2, updated_at = ?3 WHERE id = ?1",
params![
current.id,
serde_json::to_string(¤t).map_err(|error| error.to_string())?,
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 put_debugger_session(&self, session: &DebuggerSessionRecord) -> Result<(), String> {
let value = if session.observed_state.scripts.is_empty() {
serde_json::to_string(session).map_err(|error| error.to_string())?
} else {
let mut stored = session.clone();
stored.observed_state.scripts = Vec::new();
serde_json::to_string(&stored).map_err(|error| error.to_string())?
};
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_sessions(
id, desired_state, adapter, execution_id, value, updated_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)
ON CONFLICT(id) DO UPDATE SET
desired_state = excluded.desired_state,
adapter = excluded.adapter,
execution_id = excluded.execution_id,
value = excluded.value,
updated_at = excluded.updated_at",
params![
session.id,
enum_value(&session.desired_state)?,
enum_value(&session.adapter)?,
session.execution_id,
value,
as_i64(session.updated_at)
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_session(&self, id: &str) -> Result<Option<DebuggerSessionRecord>, String> {
self.get_json_row("SELECT value FROM debugger_sessions WHERE id = ?1", id)
}
pub fn debugger_sessions(&self) -> Result<Vec<DebuggerSessionRecord>, String> {
self.list_values("debugger_sessions", "updated_at DESC, id")
}
pub fn put_debugger_selection(&self, selection: &DebuggerSelection) -> Result<(), String> {
let value = serde_json::to_string(selection).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_selections(actor, session_id, value, updated_at)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(actor) DO UPDATE SET
session_id = excluded.session_id,
value = excluded.value,
updated_at = excluded.updated_at",
params![
selection.actor,
selection.session_id,
value,
as_i64(selection.updated_at)
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_selection(&self, actor: &str) -> Result<Option<DebuggerSelection>, String> {
self.get_json_row(
"SELECT value FROM debugger_selections WHERE actor = ?1",
actor,
)
}
pub fn put_debugger_breakpoint(&self, breakpoint: &DebuggerBreakpoint) -> Result<(), String> {
let value = serde_json::to_string(breakpoint).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_breakpoints(id, session_id, value, updated_at)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(id) DO UPDATE SET
session_id = excluded.session_id,
value = excluded.value,
updated_at = excluded.updated_at",
params![
breakpoint.id,
breakpoint.session_id,
value,
as_i64(breakpoint.updated_at)
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_breakpoint(&self, id: &str) -> Result<Option<DebuggerBreakpoint>, String> {
self.get_json_row("SELECT value FROM debugger_breakpoints WHERE id = ?1", id)
}
pub fn debugger_breakpoints(
&self,
session_id: &str,
) -> Result<Vec<DebuggerBreakpoint>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_breakpoints
WHERE session_id = ?1 ORDER BY updated_at, id",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([session_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn delete_debugger_breakpoint(&self, id: &str) -> Result<bool, String> {
self.delete_named("debugger_breakpoints", "id", id)
}
pub fn claim_debugger_mutation(
&self,
record: &DebuggerMutationRecord,
) -> Result<DebuggerMutationClaim, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let existing = transaction
.query_row(
"SELECT value FROM debugger_mutations WHERE idempotency_key = ?1",
[&record.idempotency_key],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
if let Some(existing) = existing {
let existing = serde_json::from_str(&existing).map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
return Ok(DebuggerMutationClaim::Replayed(Box::new(existing)));
}
let value = serde_json::to_string(record).map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO debugger_mutations(
id, idempotency_key, operation, fingerprint, session_id, state, value, updated_at
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
record.id,
record.idempotency_key,
record.operation,
record.fingerprint,
record.session_id,
enum_value(&record.state)?,
value,
as_i64(record.started_at)
],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(DebuggerMutationClaim::Claimed)
}
pub fn put_debugger_mutation(&self, record: &DebuggerMutationRecord) -> Result<(), String> {
let value = serde_json::to_string(record).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"UPDATE debugger_mutations SET state = ?2, value = ?3, updated_at = ?4
WHERE id = ?1",
params![
record.id,
enum_value(&record.state)?,
value,
as_i64(record.completed_at.unwrap_or(record.started_at))
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_mutation(&self, key: &str) -> Result<Option<DebuggerMutationRecord>, String> {
self.get_json_row(
"SELECT value FROM debugger_mutations WHERE id = ?1 OR idempotency_key = ?1",
key,
)
}
pub fn recover_debugger_mutations(&self) -> Result<Vec<DebuggerMutationRecord>, String> {
let pending = {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM debugger_mutations WHERE state = 'pending'")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows::<DebuggerMutationRecord>(rows)?
};
let now = now_ms();
for mut record in pending.iter().cloned() {
record.state = DebuggerMutationState::Unknown;
record.error = Some(
"Debugger daemon restarted after mutation acceptance; the external result is unknown"
.to_string(),
);
record.completed_at = Some(now);
self.put_debugger_mutation(&record)?;
}
Ok(pending)
}
pub fn append_debugger_event(&self, mut event: DebuggerEvent) -> Result<DebuggerEvent, 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 debugger_events(
id, session_id, execution_id, at, kind, details
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
event.id,
event.session_id,
event.execution_id,
as_i64(event.at),
event.kind,
details
],
)
.map_err(|error| error.to_string())?;
event.sequence = connection.last_insert_rowid() as u64;
Ok(event)
}
pub fn debugger_events(
&self,
session_id: &str,
after: u64,
limit: usize,
) -> Result<Vec<DebuggerEvent>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT sequence, id, session_id, execution_id, at, kind, details
FROM debugger_events
WHERE session_id = ?1 AND sequence > ?2
ORDER BY sequence ASC LIMIT ?3",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![session_id, as_i64(after), limit.min(1000) as i64],
|row| {
let details = row.get::<_, String>(6)?;
Ok(DebuggerEvent {
sequence: row.get::<_, i64>(0)?.max(0) as u64,
id: row.get(1)?,
session_id: row.get(2)?,
execution_id: row.get(3)?,
at: row.get::<_, i64>(4)?.max(0) as u64,
kind: row.get(5)?,
details: serde_json::from_str(&details).map_err(|error| {
rusqlite::Error::FromSqlConversionFailure(
details.len(),
rusqlite::types::Type::Text,
Box::new(error),
)
})?,
})
},
)
.map_err(|error| error.to_string())?;
rows.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())
}
pub fn put_debugger_recording(&self, recording: &DebuggerRecording) -> Result<(), String> {
let value = serde_json::to_string(recording).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_recordings(
id, session_id, execution_id, status, started_at, updated_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
ON CONFLICT(id) DO UPDATE SET
execution_id = excluded.execution_id,
status = excluded.status,
updated_at = excluded.updated_at,
value = excluded.value",
params![
recording.id,
recording.session_id,
recording.execution_id,
debugger_recording_phase(recording.phase),
as_i64(recording.started_at),
as_i64(recording.updated_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_recording(&self, id: &str) -> Result<Option<DebuggerRecording>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM debugger_recordings WHERE id = ?1",
[id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn active_debugger_recording(
&self,
session_id: &str,
) -> Result<Option<DebuggerRecording>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM debugger_recordings
WHERE session_id = ?1 AND status = 'running'
ORDER BY started_at DESC LIMIT 1",
[session_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn latest_debugger_recording(
&self,
session_id: &str,
) -> Result<Option<DebuggerRecording>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM debugger_recordings
WHERE session_id = ?1 ORDER BY started_at DESC LIMIT 1",
[session_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn debugger_recordings(&self) -> Result<Vec<DebuggerRecording>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM debugger_recordings ORDER BY started_at DESC")
.map_err(|error| error.to_string())?;
statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
pub fn put_debugger_capture(
&self,
mut capture: DebuggerCapture,
) -> Result<DebuggerCapture, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
capture.sequence = transaction
.query_row(
"SELECT COALESCE(MAX(sequence), 0) + 1
FROM debugger_captures WHERE recording_id = ?1",
[&capture.recording_id],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())?
.try_into()
.map_err(|_| "Debugger capture sequence exceeded u64".to_string())?;
let value = serde_json::to_string(&capture).map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO debugger_captures(
id, recording_id, session_id, sequence, at, artifact_id, bytes, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
capture.id,
capture.recording_id,
capture.session_id,
as_i64(capture.sequence),
as_i64(capture.at),
capture.screenshot.artifact.id,
as_i64(capture.screenshot.bytes),
value
],
)
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(capture)
}
pub fn debugger_capture(&self, id: &str) -> Result<Option<DebuggerCapture>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM debugger_captures WHERE id = ?1",
[id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn debugger_captures(&self, recording_id: &str) -> Result<Vec<DebuggerCapture>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_captures
WHERE recording_id = ?1 ORDER BY sequence",
)
.map_err(|error| error.to_string())?;
statement
.query_map([recording_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
pub fn put_debugger_mark(&self, mark: &DebuggerMark) -> Result<(), String> {
let value = serde_json::to_string(mark).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_marks(
id, recording_id, session_id, name, capture_id, at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
mark.id,
mark.recording_id,
mark.session_id,
mark.name,
mark.capture_id,
as_i64(mark.at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_marks(&self, recording_id: &str) -> Result<Vec<DebuggerMark>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_marks
WHERE recording_id = ?1 ORDER BY at, id",
)
.map_err(|error| error.to_string())?;
statement
.query_map([recording_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
pub fn put_debugger_after(&self, after: &DebuggerAfter) -> Result<(), String> {
let value = serde_json::to_string(after).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_afters(
id, recording_id, session_id, before_capture_id, after_capture_id,
report_id, at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
after.id,
after.recording_id,
after.session_id,
after.before_capture_id,
after.after_capture_id,
after.report.id,
as_i64(after.at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_after(&self, id: &str) -> Result<Option<DebuggerAfter>, String> {
self.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM debugger_afters WHERE id = ?1",
[id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn debugger_afters(&self, recording_id: &str) -> Result<Vec<DebuggerAfter>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_afters
WHERE recording_id = ?1 ORDER BY at DESC, id DESC",
)
.map_err(|error| error.to_string())?;
statement
.query_map([recording_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.map(|row| {
let value = row.map_err(|error| error.to_string())?;
serde_json::from_str(&value).map_err(|error| error.to_string())
})
.collect()
}
pub fn trim_debugger_captures(
&self,
recording_id: &str,
max_frames: usize,
max_bytes: u64,
) -> Result<Vec<DebuggerCapture>, String> {
let captures = self.debugger_captures(recording_id)?;
if captures.len() <= max_frames
&& captures
.iter()
.map(|capture| capture.screenshot.bytes)
.sum::<u64>()
<= max_bytes
{
return Ok(captures);
}
let protected = self
.debugger_marks(recording_id)?
.into_iter()
.map(|mark| mark.capture_id)
.chain(
self.debugger_afters(recording_id)?
.into_iter()
.flat_map(|after| [after.before_capture_id, after.after_capture_id]),
)
.chain(captures.first().map(|capture| capture.id.clone()))
.chain(captures.last().map(|capture| capture.id.clone()))
.collect::<BTreeSet<_>>();
let mut retained = captures;
let mut bytes = retained
.iter()
.map(|capture| capture.screenshot.bytes)
.sum::<u64>();
let mut removed = Vec::new();
while retained.len() > max_frames || bytes > max_bytes {
let Some(index) = retained
.iter()
.position(|capture| !protected.contains(&capture.id))
else {
break;
};
let capture = retained.remove(index);
bytes = bytes.saturating_sub(capture.screenshot.bytes);
removed.push(capture);
}
if !removed.is_empty() {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
for mut capture in removed {
capture.screenshot.png_base64 = None;
capture.screenshot.bytes = 0;
capture.screenshot.artifact.bytes = 0;
let value = serde_json::to_string(&capture).map_err(|error| error.to_string())?;
transaction
.execute(
"UPDATE debugger_captures SET bytes = 0, value = ?2 WHERE id = ?1",
params![capture.id, value],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
}
Ok(retained)
}
pub fn put_debugger_artifact(
&self,
session_id: &str,
execution_id: Option<&str>,
kind: &str,
media_type: &str,
bytes: &[u8],
) -> Result<DebuggerArtifact, String> {
let digest = format!("{:x}", Sha256::digest(bytes));
let id = format!("sha256:{digest}");
let directory = self
.path
.parent()
.unwrap_or_else(|| Path::new("."))
.join("artifacts")
.join("sha256");
std::fs::create_dir_all(&directory).map_err(|error| error.to_string())?;
let path = directory.join(&digest);
if !path.exists() {
let temporary = directory.join(format!(".{digest}.{}.tmp", uuid::Uuid::now_v7()));
std::fs::write(&temporary, bytes).map_err(|error| error.to_string())?;
if let Err(error) = std::fs::rename(&temporary, &path) {
let _ = std::fs::remove_file(&temporary);
if !path.exists() {
return Err(error.to_string());
}
}
}
let created_at = now_ms();
let artifact = DebuggerArtifact {
id: id.clone(),
kind: kind.to_string(),
media_type: media_type.to_string(),
bytes: bytes.len() as u64,
path: path.to_string_lossy().to_string(),
created_at,
};
self.register_debugger_artifact(session_id, execution_id, &artifact)?;
Ok(artifact)
}
pub fn put_debugger_artifact_file(
&self,
session_id: &str,
execution_id: Option<&str>,
kind: &str,
media_type: &str,
source: &Path,
) -> Result<DebuggerArtifact, String> {
let mut file = std::fs::File::open(source).map_err(|error| error.to_string())?;
let mut hasher = Sha256::new();
let mut buffer = [0_u8; 64 * 1024];
let mut bytes = 0_u64;
loop {
let read = file.read(&mut buffer).map_err(|error| error.to_string())?;
if read == 0 {
break;
}
hasher.update(&buffer[..read]);
bytes = bytes.saturating_add(read as u64);
}
let digest = format!("{:x}", hasher.finalize());
let directory = self
.path
.parent()
.unwrap_or_else(|| Path::new("."))
.join("artifacts")
.join("sha256");
std::fs::create_dir_all(&directory).map_err(|error| error.to_string())?;
let path = directory.join(&digest);
if !path.exists() {
let temporary = directory.join(format!(".{digest}.{}.tmp", uuid::Uuid::now_v7()));
std::fs::copy(source, &temporary).map_err(|error| error.to_string())?;
if let Err(error) = std::fs::rename(&temporary, &path) {
let _ = std::fs::remove_file(&temporary);
if !path.exists() {
return Err(error.to_string());
}
}
}
let artifact = DebuggerArtifact {
id: format!("sha256:{digest}"),
kind: kind.to_string(),
media_type: media_type.to_string(),
bytes,
path: path.to_string_lossy().into_owned(),
created_at: now_ms(),
};
self.register_debugger_artifact(session_id, execution_id, &artifact)?;
Ok(artifact)
}
fn register_debugger_artifact(
&self,
session_id: &str,
execution_id: Option<&str>,
artifact: &DebuggerArtifact,
) -> Result<(), String> {
let value = serde_json::to_string(&artifact).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO debugger_artifacts(
id, session_id, execution_id, kind, media_type, path, bytes, created_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
ON CONFLICT(id, session_id) DO UPDATE SET value = excluded.value",
params![
artifact.id,
session_id,
execution_id,
artifact.kind,
artifact.media_type,
artifact.path,
as_i64(artifact.bytes),
as_i64(artifact.created_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
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 put_debugger_flow(&self, flow: &DebuggerFlow) -> Result<(), String> {
let value = serde_json::to_string(flow).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_flows(
id, session_id, recording_id, name, phase, updated_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
flow.id,
flow.session_id,
flow.recording_id,
flow.name,
enum_value(&flow.phase)?,
as_i64(flow.updated_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_flow(&self, id: &str) -> Result<Option<DebuggerFlow>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.query_row(
"SELECT value FROM debugger_flows WHERE id = ?1",
params![id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn active_debugger_flow(&self, session_id: &str) -> Result<Option<DebuggerFlow>, String> {
let phase = enum_value(&DebuggerFlowPhase::Recording)?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.query_row(
"SELECT value FROM debugger_flows
WHERE session_id = ?1 AND phase = ?2 ORDER BY updated_at DESC LIMIT 1",
params![session_id, phase],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn debugger_flow_by_name(
&self,
session_id: &str,
name: &str,
) -> Result<Option<DebuggerFlow>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.query_row(
"SELECT value FROM debugger_flows
WHERE session_id = ?1 AND name = ?2 ORDER BY updated_at DESC LIMIT 1",
params![session_id, name],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn debugger_flows(&self) -> Result<Vec<DebuggerFlow>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM debugger_flows ORDER BY updated_at DESC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_debugger_flow_step(&self, step: &DebuggerFlowStep) -> Result<(), String> {
let value = serde_json::to_string(step).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_flow_steps(id, flow_id, sequence, at, value)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![
step.id,
step.flow_id,
as_i64(step.sequence),
as_i64(step.at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_flow_steps(&self, flow_id: &str) -> Result<Vec<DebuggerFlowStep>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_flow_steps
WHERE flow_id = ?1 ORDER BY sequence ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![flow_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_debugger_flow_run(&self, run: &DebuggerFlowRun) -> Result<(), String> {
let value = serde_json::to_string(run).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_flow_runs(
id, flow_id, session_id, status, started_at, completed_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
run.id,
run.flow_id,
run.session_id,
run.status,
as_i64(run.started_at),
as_i64(run.completed_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_flow_runs(&self, flow_id: &str) -> Result<Vec<DebuggerFlowRun>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_flow_runs
WHERE flow_id = ?1 ORDER BY started_at ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![flow_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_debugger_tap(&self, tap: &DebuggerTap) -> Result<(), String> {
let value = serde_json::to_string(tap).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_taps(
id, session_id, backend_id, enabled, updated_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
tap.id,
tap.session_id,
tap.backend_id,
tap.enabled,
as_i64(tap.updated_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_tap(&self, id: &str) -> Result<Option<DebuggerTap>, String> {
self.get_json_row("SELECT value FROM debugger_taps WHERE id = ?1", id)
}
pub fn debugger_taps(&self, session_id: &str) -> Result<Vec<DebuggerTap>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_taps
WHERE session_id = ?1 ORDER BY updated_at ASC, id ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![session_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn record_debugger_tap_hit(&self, hit: &DebuggerTapHit) -> Result<bool, String> {
let value = serde_json::to_string(hit).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let inserted = transaction
.execute(
"INSERT OR IGNORE INTO debugger_tap_hits(id, tap_id, session_id, at, value)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![hit.id, hit.tap_id, hit.session_id, as_i64(hit.at), value],
)
.map_err(|error| error.to_string())?
== 1;
if inserted {
let encoded = transaction
.query_row(
"SELECT value FROM debugger_taps WHERE id = ?1",
params![hit.tap_id],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
let mut tap: DebuggerTap =
serde_json::from_str(&encoded).map_err(|error| error.to_string())?;
tap.hits = tap.hits.saturating_add(1);
tap.updated_at = hit.at;
transaction
.execute(
"UPDATE debugger_taps SET updated_at = ?2, value = ?3 WHERE id = ?1",
params![
tap.id,
as_i64(tap.updated_at),
serde_json::to_string(&tap).map_err(|error| error.to_string())?
],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM debugger_tap_hits WHERE tap_id = ?1 AND id NOT IN (
SELECT id FROM debugger_tap_hits WHERE tap_id = ?1
ORDER BY at DESC, id DESC LIMIT 1000
)",
params![hit.tap_id],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(inserted)
}
pub fn debugger_tap_hits(
&self,
tap_id: &str,
limit: usize,
) -> Result<Vec<DebuggerTapHit>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM (
SELECT value, at, id FROM debugger_tap_hits WHERE tap_id = ?1
ORDER BY at DESC, id DESC LIMIT ?2
) ORDER BY at ASC, id ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![tap_id, limit.clamp(1, 1000) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_debugger_watch(&self, watch: &DebuggerWatch) -> Result<(), String> {
let value = serde_json::to_string(watch).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_watches(id, session_id, enabled, updated_at, value)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![
watch.id,
watch.session_id,
watch.enabled,
as_i64(watch.updated_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_watch(&self, id: &str) -> Result<Option<DebuggerWatch>, String> {
self.get_json_row("SELECT value FROM debugger_watches WHERE id = ?1", id)
}
pub fn debugger_watches(&self, session_id: &str) -> Result<Vec<DebuggerWatch>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_watches
WHERE session_id = ?1 ORDER BY updated_at ASC, id ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![session_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn record_debugger_watch_hit(&self, hit: &DebuggerWatchHit) -> Result<bool, String> {
let value = serde_json::to_string(hit).map_err(|error| error.to_string())?;
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let inserted = transaction
.execute(
"INSERT OR IGNORE INTO debugger_watch_hits(id, watch_id, session_id, at, value)
VALUES (?1, ?2, ?3, ?4, ?5)",
params![hit.id, hit.watch_id, hit.session_id, as_i64(hit.at), value],
)
.map_err(|error| error.to_string())?
== 1;
if inserted {
let encoded = transaction
.query_row(
"SELECT value FROM debugger_watches WHERE id = ?1",
params![hit.watch_id],
|row| row.get::<_, String>(0),
)
.map_err(|error| error.to_string())?;
let mut watch: DebuggerWatch =
serde_json::from_str(&encoded).map_err(|error| error.to_string())?;
watch.hits = watch.hits.saturating_add(1);
watch.last_value = hit.new_value.clone();
watch.last_error = hit.error.clone();
watch.updated_at = hit.at;
transaction
.execute(
"UPDATE debugger_watches SET updated_at = ?2, value = ?3 WHERE id = ?1",
params![
watch.id,
as_i64(watch.updated_at),
serde_json::to_string(&watch).map_err(|error| error.to_string())?
],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM debugger_watch_hits WHERE watch_id = ?1 AND id NOT IN (
SELECT id FROM debugger_watch_hits WHERE watch_id = ?1
ORDER BY at DESC, id DESC LIMIT 1000
)",
params![hit.watch_id],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())?;
Ok(inserted)
}
pub fn debugger_watch_hits(
&self,
watch_id: &str,
limit: usize,
) -> Result<Vec<DebuggerWatchHit>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM (
SELECT value, at, id FROM debugger_watch_hits WHERE watch_id = ?1
ORDER BY at DESC, id DESC LIMIT ?2
) ORDER BY at ASC, id ASC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![watch_id, limit.clamp(1, 1000) as i64], |row| {
row.get::<_, String>(0)
})
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_debugger_evidence_trace(&self, trace: &DebuggerEvidenceTrace) -> Result<(), String> {
let value = serde_json::to_string(trace).map_err(|error| error.to_string())?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_evidence_traces(
id, session_id, status, started_at, updated_at, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
trace.id,
trace.session_id,
enum_value(&trace.status)?,
as_i64(trace.started_at),
as_i64(trace.updated_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_evidence_trace(
&self,
id: &str,
) -> Result<Option<DebuggerEvidenceTrace>, String> {
self.get_json_row(
"SELECT value FROM debugger_evidence_traces WHERE id = ?1",
id,
)
}
pub fn active_debugger_evidence_trace(
&self,
session_id: &str,
) -> Result<Option<DebuggerEvidenceTrace>, String> {
let status = enum_value(&DebuggerEvidenceTraceStatus::Recording)?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.query_row(
"SELECT value FROM debugger_evidence_traces
WHERE session_id = ?1 AND status = ?2 ORDER BY updated_at DESC LIMIT 1",
params![session_id, status],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.transpose()
}
pub fn debugger_evidence_traces(&self) -> Result<Vec<DebuggerEvidenceTrace>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM debugger_evidence_traces ORDER BY updated_at DESC, id DESC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn put_debugger_evidence_trace_selection(
&self,
actor: &str,
trace_id: &str,
) -> Result<(), String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.execute(
"INSERT OR REPLACE INTO debugger_evidence_trace_selections(
actor, trace_id, updated_at
) VALUES (?1, ?2, ?3)",
params![actor, trace_id, as_i64(now_ms())],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn debugger_evidence_trace_selection(&self, actor: &str) -> Result<Option<String>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
connection
.query_row(
"SELECT trace_id FROM debugger_evidence_trace_selections WHERE actor = ?1",
params![actor],
|row| row.get(0),
)
.optional()
.map_err(|error| error.to_string())
}
pub fn put_debugger_trace_import_items(
&self,
trace_id: &str,
items: &[DebuggerTraceItem],
) -> Result<(), String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
for (sequence, item) in items.iter().take(20_000).enumerate() {
transaction
.execute(
"INSERT INTO debugger_trace_import_items(
trace_id, sequence, at, kind, item_id, value
) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
trace_id,
sequence as i64,
as_i64(item.at),
item.kind,
item.id,
serde_json::to_string(item).map_err(|error| error.to_string())?
],
)
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())
}
pub fn debugger_trace_import_items(
&self,
trace_id: &str,
) -> Result<Vec<DebuggerTraceItem>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_trace_import_items
WHERE trace_id = ?1 ORDER BY sequence ASC LIMIT 20000",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![trace_id], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn delete_debugger_evidence_trace(&self, trace_id: &str) -> Result<bool, String> {
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM debugger_evidence_trace_selections WHERE trace_id = ?1",
params![trace_id],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"DELETE FROM debugger_trace_import_items WHERE trace_id = ?1",
params![trace_id],
)
.map_err(|error| error.to_string())?;
let deleted = transaction
.execute(
"DELETE FROM debugger_evidence_traces WHERE id = ?1",
params![trace_id],
)
.map_err(|error| error.to_string())?
== 1;
transaction.commit().map_err(|error| error.to_string())?;
Ok(deleted)
}
pub fn put_rr_trace(&self, trace: &RrTrace) -> Result<(), String> {
let value = serde_json::to_string(trace).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT OR REPLACE INTO debugger_rr_traces(
id, status, started_at, updated_at, value
) VALUES (?1, ?2, ?3, ?4, ?5)",
params![
trace.id,
enum_value(&trace.status)?,
as_i64(trace.started_at),
as_i64(trace.updated_at),
value
],
)
.map_err(|error| error.to_string())?;
Ok(())
}
pub fn rr_trace(&self, id: &str) -> Result<Option<RrTrace>, String> {
self.get_json_row("SELECT value FROM debugger_rr_traces WHERE id = ?1", id)
}
pub fn rr_traces(&self) -> Result<Vec<RrTrace>, String> {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare("SELECT value FROM debugger_rr_traces ORDER BY updated_at DESC, id DESC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn rr_traces_by_status(&self, status: RrTraceStatus) -> Result<Vec<RrTrace>, String> {
let status = enum_value(&status)?;
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection
.prepare(
"SELECT value FROM debugger_rr_traces
WHERE status = ?1 ORDER BY updated_at DESC, id DESC",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(params![status], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows(rows)
}
pub fn causal_events_between(
&self,
since: u64,
until: 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 at >= ?1 AND at <= ?2
ORDER BY at ASC, sequence ASC LIMIT ?3",
)
.map_err(|error| error.to_string())?;
let rows = statement
.query_map(
params![as_i64(since), as_i64(until), limit.min(1000) 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_with_behavior(TransactionBehavior::Immediate)
.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 filtered_values<T: DeserializeOwned>(
&self,
sql: &str,
params: impl rusqlite::Params,
) -> Result<Vec<T>, String> {
let values = {
let connection = self.connection.lock().map_err(|error| error.to_string())?;
let mut statement = connection.prepare(sql).map_err(|error| error.to_string())?;
statement
.query_map(params, |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
values
.into_iter()
.map(|value| serde_json::from_str(&value).map_err(|error| error.to_string()))
.collect()
}
fn list_values<T: DeserializeOwned>(&self, table: &str, order: &str) -> Result<Vec<T>, String> {
require_list(table, order)?;
let values = {
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())?;
statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?
.collect::<Result<Vec<_>, _>>()
.map_err(|error| error.to_string())?
};
values
.into_iter()
.map(|value| 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 migrate_scoped_environments(
connection: &mut Connection,
prior_version: u32,
) -> Result<(), String> {
if prior_version >= 11 {
return Ok(());
}
let environments = {
let mut statement = connection
.prepare("SELECT value FROM environments ORDER BY scope ASC")
.map_err(|error| error.to_string())?;
let rows = statement
.query_map([], |row| row.get::<_, String>(0))
.map_err(|error| error.to_string())?;
deserialize_rows::<ScopedEnvironment>(rows)?
};
if environments.is_empty() {
return Ok(());
}
let global = environments
.iter()
.find(|environment| environment.scope_kind == RecipeScopeKind::Global)
.cloned();
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let mut inserted = 0_i64;
if let Some(global) = &global {
let mut profile = Profile::empty("legacy-default");
profile.literals = global.literals.clone();
profile.secrets = global.secrets.clone();
profile.refresh_generation();
insert_migrated_profile(&transaction, &profile)?;
transaction
.execute(
"INSERT INTO profile_settings(key, value, updated_at) VALUES ('default', ?1, ?2)
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at",
params![profile.name, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
inserted += 1;
}
for environment in environments
.iter()
.filter(|environment| environment.scope_kind != RecipeScopeKind::Global)
{
let scope = serde_json::to_vec(&serde_json::json!({
"kind": environment.scope_kind,
"key": environment.scope_key,
}))
.map_err(|error| error.to_string())?;
let digest = format!("{:x}", Sha256::digest(scope));
let mut profile = Profile::empty(format!("legacy-{}", &digest[..12]));
if let Some(global) = &global {
profile.literals = global.literals.clone();
profile.secrets = global.secrets.clone();
}
merge_profile_values(
&mut profile.literals,
&mut profile.secrets,
&environment.literals,
&environment.secrets,
);
profile.refresh_generation();
insert_migrated_profile(&transaction, &profile)?;
if let Some(root) = environment.scope_key.as_deref() {
let canonical = std::fs::canonicalize(root)
.unwrap_or_else(|_| PathBuf::from(root))
.to_string_lossy()
.into_owned();
transaction
.execute(
"INSERT INTO profile_roots(root, profile_name, created_at) VALUES (?1, ?2, ?3)",
params![canonical, profile.name, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
}
inserted += 1;
}
let migrated = transaction
.query_row("SELECT COUNT(*) FROM profiles", [], |row| {
row.get::<_, i64>(0)
})
.map_err(|error| error.to_string())?;
if migrated < inserted {
return Err(format!(
"PROFILE_MIGRATION_FAILED: expected {inserted} profiles but found {migrated}"
));
}
transaction
.execute("DELETE FROM environments", [])
.map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
Ok(())
}
fn insert_migrated_profile(
transaction: &rusqlite::Transaction<'_>,
profile: &Profile,
) -> Result<(), String> {
let value = serde_json::to_string(profile).map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO profiles(name, value, updated_at) VALUES (?1, ?2, ?3)",
params![profile.name, value, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
transaction
.execute(
"INSERT INTO profile_generations(profile_name, generation, value, created_at)
VALUES (?1, ?2, ?3, ?4)",
params![
profile.name,
profile.generation,
value,
as_i64(profile.updated_at)
],
)
.map_err(|error| error.to_string())?;
for name in profile.literals.keys().chain(profile.secrets.keys()) {
transaction
.execute(
"INSERT OR IGNORE INTO profile_controlled_keys(name, created_at) VALUES (?1, ?2)",
params![name, as_i64(profile.updated_at)],
)
.map_err(|error| error.to_string())?;
}
Ok(())
}
fn merge_profile_values(
literals: &mut std::collections::BTreeMap<String, String>,
secrets: &mut std::collections::BTreeMap<String, crate::model::SecretRef>,
next_literals: &std::collections::BTreeMap<String, String>,
next_secrets: &std::collections::BTreeMap<String, crate::model::SecretRef>,
) {
for (name, value) in next_literals {
secrets.remove(name);
literals.insert(name.clone(), value.clone());
}
for (name, value) in next_secrets {
literals.remove(name);
secrets.insert(name.clone(), value.clone());
}
}
fn validate_execution_owner(
transaction: &rusqlite::Transaction<'_>,
execution: &Execution,
) -> Result<(), String> {
match execution.spec.owner_kind {
LeaseOwnerKind::Persistent => {}
LeaseOwnerKind::Execution => {
let owner_id = execution
.spec
.owner_id
.as_deref()
.or(execution.parent_id.as_deref())
.ok_or_else(|| "Execution-owned requests require an owner execution".to_string())?;
let owner = transaction
.query_row(
"SELECT value FROM execution_records WHERE id = ?1",
[owner_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.ok_or_else(|| format!("Execution owner \"{owner_id}\" not found"))?;
let owner: Execution =
serde_json::from_str(&owner).map_err(|error| error.to_string())?;
if matches!(
owner.status,
ExecutionStatus::Completed | ExecutionStatus::Failed | ExecutionStatus::Canceled
) {
return Err(format!("Execution owner \"{owner_id}\" is terminal"));
}
if owner
.result
.as_ref()
.and_then(|result| result.get("cancel_requested"))
.and_then(serde_json::Value::as_bool)
== Some(true)
{
return Err(format!("Execution owner \"{owner_id}\" is canceling"));
}
}
LeaseOwnerKind::Session => {
let session_id = execution
.session_id
.as_deref()
.ok_or_else(|| "Session-owned requests require a session".to_string())?;
if execution.spec.owner_id.as_deref() != Some(session_id) {
return Err("Session owner does not match execution session".to_string());
}
let session = transaction
.query_row(
"SELECT value FROM agent_sessions WHERE id = ?1",
[session_id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?
.ok_or_else(|| format!("Session \"{session_id}\" not found"))?;
let session: AgentSession =
serde_json::from_str(&session).map_err(|error| error.to_string())?;
if session.closed || session.closing || session.expires_at <= now_ms() {
return Err(format!("Session \"{session_id}\" is closed or expired"));
}
}
}
Ok(())
}
fn migrate_execution_listing(
connection: &mut Connection,
prior_version: u32,
) -> Result<(), String> {
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
transaction
.execute_batch(
"CREATE TABLE IF NOT EXISTS execution_listing (
id TEXT PRIMARY KEY,
parent_id TEXT,
status TEXT,
error_code TEXT,
updated_at INTEGER NOT NULL
);",
)
.map_err(|error| error.to_string())?;
for (name, columns) in [
("timeline", ""),
("status", "status, "),
("parent", "parent_id, "),
("error", "error_code, "),
("parent_status", "parent_id, status, "),
("status_error", "status, error_code, "),
("parent_error", "parent_id, error_code, "),
("parent_status_error", "parent_id, status, error_code, "),
] {
transaction
.execute_batch(&format!(
"CREATE INDEX IF NOT EXISTS execution_listing_{name}
ON execution_listing({columns}updated_at DESC, id DESC);"
))
.map_err(|error| error.to_string())?;
}
let select = "SELECT e.id, e.parent_id,
CASE
WHEN json_extract(e.value, '$.status') = 'stalled'
AND json_extract(r.value, '$.status') = 'running' THEN 'stalled'
WHEN json_extract(r.value, '$.status') = 'starting' THEN 'queued'
WHEN json_extract(r.value, '$.status') = 'running' THEN 'running'
WHEN json_extract(r.value, '$.status') = 'completed' THEN 'completed'
WHEN json_extract(r.value, '$.status') IN ('stopped', 'timed_out') THEN 'canceled'
WHEN json_extract(r.value, '$.status') LIKE 'context_record_failed:%'
THEN 'interrupted'
WHEN r.id IS NOT NULL THEN 'failed'
ELSE json_extract(e.value, '$.status')
END,
CASE WHEN json_extract(r.value, '$.status') = 'timed_out'
THEN 'EXECUTION_TIMEOUT'
ELSE COALESCE(
json_extract(e.value, '$.attempts[#-1].error_code'),
json_extract(e.value, '$.passes[#-1].error_code'),
json_extract(e.value, '$.result.error_code')
)
END,
MAX(e.updated_at, COALESCE(r.updated_at, e.updated_at))
FROM execution_records e LEFT JOIN runs r ON r.id = e.id";
let upsert = "ON CONFLICT(id) DO UPDATE SET
parent_id = excluded.parent_id, status = excluded.status,
error_code = excluded.error_code, updated_at = excluded.updated_at";
for (table, event, row) in [
("execution_records", "INSERT", "NEW"),
("execution_records", "UPDATE", "NEW"),
("runs", "INSERT", "NEW"),
("runs", "UPDATE", "NEW"),
("runs", "DELETE", "OLD"),
] {
transaction
.execute_batch(&format!(
"CREATE TRIGGER IF NOT EXISTS execution_listing_{table}_{event}
AFTER {event} ON {table} BEGIN
INSERT INTO execution_listing(id, parent_id, status, error_code, updated_at)
{select} WHERE e.id = {row}.id {upsert};
END;"
))
.map_err(|error| error.to_string())?;
}
transaction
.execute_batch(
"CREATE TRIGGER IF NOT EXISTS execution_listing_execution_records_DELETE
AFTER DELETE ON execution_records BEGIN
DELETE FROM execution_listing WHERE id = OLD.id;
END;",
)
.map_err(|error| error.to_string())?;
if prior_version < 22 {
transaction
.execute_batch(&format!(
"INSERT INTO execution_listing(id, parent_id, status, error_code, updated_at)
{select} WHERE 1 {upsert};"
))
.map_err(|error| error.to_string())?;
}
transaction.commit().map_err(|error| error.to_string())
}
fn effective_database_version(connection: &Connection) -> Result<u32, String> {
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.map_err(|error| error.to_string())?;
let listing_exists = connection.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'execution_listing')",
[], |row| row.get::<_, bool>(0),
).map_err(|error| error.to_string())?;
let lifetimes_exist = connection.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'execution_lifetimes')",
[], |row| row.get::<_, bool>(0),
).map_err(|error| error.to_string())?;
let version = if version == 21 && listing_exists {
22
} else {
version
};
Ok(if version == 22 && lifetimes_exist {
23
} else {
version
})
}
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 = effective_database_version(&connection)?;
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
}
fn debugger_recording_phase(phase: DebuggerRecordingPhase) -> &'static str {
match phase {
DebuggerRecordingPhase::Running => "running",
DebuggerRecordingPhase::Stopped => "stopped",
DebuggerRecordingPhase::Failed => "failed",
}
}
impl Store {
fn intern_capabilities(&self, execution: &ExecutionState) -> Result<ExecutionState, String> {
let Some(capabilities) = execution.capabilities.as_ref() else {
return Ok(execution.clone());
};
if capabilities.fingerprint.is_empty() || capabilities.connectors.is_empty() {
return Ok(execution.clone());
}
let value = serde_json::to_string(capabilities).map_err(|error| error.to_string())?;
self.connection
.lock()
.map_err(|error| error.to_string())?
.execute(
"INSERT INTO capability_snapshots(fingerprint, value, updated_at)
VALUES (?1, ?2, ?3)
ON CONFLICT(fingerprint) DO UPDATE SET updated_at = excluded.updated_at",
params![capabilities.fingerprint, value, as_i64(now_ms())],
)
.map_err(|error| error.to_string())?;
let mut slim = execution.clone();
if let Some(stored) = slim.capabilities.as_mut() {
stored.connectors = Vec::new();
}
Ok(slim)
}
fn rehydrate_capabilities(&self, execution: &mut ExecutionState) -> Result<(), String> {
let Some(capabilities) = execution.capabilities.as_ref() else {
return Ok(());
};
if !capabilities.connectors.is_empty() || capabilities.fingerprint.is_empty() {
return Ok(());
}
let stored: Option<String> = self
.connection
.lock()
.map_err(|error| error.to_string())?
.query_row(
"SELECT value FROM capability_snapshots WHERE fingerprint = ?1",
params![capabilities.fingerprint],
|row| row.get(0),
)
.optional()
.map_err(|error| error.to_string())?;
if let Some(stored) = stored
&& let Ok(snapshot) = serde_json::from_str(&stored)
{
execution.capabilities = Some(snapshot);
}
Ok(())
}
}
#[async_trait]
impl RuntimeStore for Store {
async fn get_execution(&self, id: &str) -> Result<Option<ExecutionState>, String> {
let mut execution: Option<ExecutionState> = self.get_named("executions", "id", id)?;
if let Some(execution) = execution.as_mut() {
self.rehydrate_capabilities(execution)?;
}
Ok(execution)
}
async fn put_execution(&self, execution: &ExecutionState) -> Result<(), String> {
let slim = self.intern_capabilities(execution)?;
if execution.status != CodeExecutionStatus::Error {
return self.put_named("executions", "id", &slim.id, &slim);
}
let value = serde_json::to_string(&slim).map_err(|error| error.to_string())?;
let changed =
{
let mut connection = self.connection.lock().map_err(|error| error.to_string())?;
let transaction = connection
.transaction_with_behavior(TransactionBehavior::Immediate)
.map_err(|error| error.to_string())?;
let previous = transaction
.query_row(
"SELECT json_extract(value, '$.status') FROM executions WHERE id = ?1",
[&slim.id],
|row| row.get::<_, String>(0),
)
.optional()
.map_err(|error| error.to_string())?;
transaction.execute(
"INSERT OR REPLACE INTO executions(id, value, updated_at) VALUES (?1, ?2, ?3)",
params![slim.id, value, now_ms() as i64],
).map_err(|error| error.to_string())?;
transaction.commit().map_err(|error| error.to_string())?;
previous.as_deref() != Some("error")
};
if changed {
crate::error_telemetry::record_at(
self.path.parent().unwrap_or(Path::new(".")),
"code",
"code",
"CODE_EXECUTION_ERROR",
None,
Some(&execution.id),
execution.error.as_deref().unwrap_or("CODE_EXECUTION_ERROR"),
);
}
Ok(())
}
async fn list_executions(&self) -> Result<Vec<ExecutionState>, String> {
let mut executions: Vec<ExecutionState> =
self.list_values("executions", "updated_at DESC")?;
for execution in &mut executions {
self.rehydrate_capabilities(execution)?;
}
Ok(executions)
}
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 recording_scope_digest(kind: RecipeScopeKind, scope_key: Option<&str>) -> String {
match kind {
RecipeScopeKind::Global => "global".to_string(),
RecipeScopeKind::GitWorktree | RecipeScopeKind::Cwd => {
let digest = Sha256::digest(scope_key.unwrap_or_default().as_bytes());
format!("{kind:?}:{digest:x}").to_ascii_lowercase()
}
}
}
fn invocation_scope_digest(
kind: crate::model::InvocationScopeKind,
scope_key: Option<&str>,
) -> String {
let kind = match kind {
crate::model::InvocationScopeKind::GitWorktree => RecipeScopeKind::GitWorktree,
crate::model::InvocationScopeKind::Cwd | crate::model::InvocationScopeKind::Unknown => {
RecipeScopeKind::Cwd
}
};
recording_scope_digest(kind, scope_key)
}
fn require_table(table: &str, key: &str) -> Result<(), String> {
if matches!(
(table, key),
("runs", "id")
| ("executions", "id")
| ("execution_records", "id")
| ("snippets", "name")
| ("recipes", "name")
| ("hooks", "name")
| ("deciders", "name")
| ("environments", "scope")
| ("profiles", "name")
| ("recipe_suggestions", "id")
| ("connectors", "name")
| ("providers", "name")
| ("services", "name")
| ("debugger_breakpoints", "id")
) {
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")
| ("profiles", "name ASC")
| ("recipe_suggestions", "created_at DESC, id DESC")
| ("connectors", "name ASC")
| ("providers", "name ASC")
| ("services", "name ASC")
| ("debugger_sessions", "updated_at DESC, id")
| ("deciders", "updated_at DESC, name")
) {
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 std::sync::Arc;
use tempfile::tempdir;
use super::*;
use crate::model::{
ExecutionBudget, ExecutionProgram, ExecutionSpec, ExecutionStatus, InvocationPatternQuery,
InvocationQuery, InvocationRecord, InvocationScopeKind, InvocationSource, InvocationStatus,
InvocationSurface, IoMode, LeaseOwnerKind, PollIterationStatus, Reservation,
ReservationKind, RestartPolicy, RetryPolicy, RunLifetime, RunSpec, SCHEMA_VERSION,
TelemetryCapture, TelemetryCaptureStatus, TelemetrySample, WorkspaceContext,
};
#[test]
fn list_values_releases_database_lock_before_deserializing() {
thread_local! {
static STORE: std::cell::RefCell<Option<Arc<Store>>> = const { std::cell::RefCell::new(None) };
}
struct Probe;
impl<'de> serde::Deserialize<'de> for Probe {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let _ = <serde_json::Value as serde::Deserialize>::deserialize(deserializer)?;
let unlocked = STORE.with(|store| {
store
.borrow()
.as_ref()
.unwrap()
.connection
.try_lock()
.is_ok()
});
if !unlocked {
return Err(serde::de::Error::custom(
"JSON decoding holds the shared database lock",
));
}
Ok(Self)
}
}
let directory = tempdir().unwrap();
let store = Arc::new(Store::open(directory.path().join("state.sqlite3")).unwrap());
store
.connection
.lock()
.unwrap()
.execute(
"INSERT INTO snippets(name, value, updated_at) VALUES ('probe', '{}', 1)",
[],
)
.unwrap();
STORE.with(|slot| *slot.borrow_mut() = Some(store.clone()));
let result = store.list_values::<Probe>("snippets", "name ASC");
STORE.with(|slot| *slot.borrow_mut() = None);
assert!(result.is_ok(), "{}", result.err().unwrap());
}
#[test]
fn interrupting_orphans_closes_running_and_spares_everything_else() {
use incurs_codemode::ExecutionStatus as Code;
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let state = |id: &str, status: Code| ExecutionState {
id: id.to_owned(),
code: "return 1".to_owned(),
status,
log: Vec::new(),
result: None,
error: None,
logs: Vec::new(),
connectors: Vec::new(),
capabilities: None,
events: Vec::new(),
created_at: 1,
updated_at: 1,
};
for (id, status) in [
("orphan-a", Code::Running),
("orphan-b", Code::Running),
("waiting", Code::Paused),
("done", Code::Completed),
("broke", Code::Error),
] {
store
.put_named("executions", "id", id, &state(id, status))
.unwrap();
}
let closed = store.interrupt_orphaned_code_executions().unwrap();
assert_eq!(closed, 2, "both running orphans should be closed");
let by_id = |id: &str| -> ExecutionState {
store.get_named("executions", "id", id).unwrap().unwrap()
};
for id in ["orphan-a", "orphan-b"] {
let found = by_id(id);
assert_eq!(
found.status,
Code::Error,
"{id} should no longer be running"
);
assert!(
found
.error
.is_some_and(|reason| reason.contains("INTERRUPTED")),
"{id} should say why it was closed"
);
}
assert_eq!(by_id("waiting").status, Code::Paused);
assert_eq!(by_id("done").status, Code::Completed);
assert_eq!(by_id("broke").status, Code::Error);
assert_eq!(store.interrupt_orphaned_code_executions().unwrap(), 0);
}
#[test]
fn active_execution_count_is_indexed_read_only_and_uses_run_status() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
{
let connection = store.connection.lock().unwrap();
for (id, execution_status, run_status) in [
("live", "running", None),
("queued", "queued", None),
("stalled", "stalled", None),
("interrupted", "interrupted", None),
("done", "completed", None),
("failed", "failed", None),
("canceled", "canceled", None),
("stale-live", "running", Some("completed")),
("stale-done", "completed", Some("running")),
("starting", "completed", Some("starting")),
("stopped", "running", Some("stopped")),
("timed-out", "running", Some("timed_out")),
("launch-error", "running", Some("launch_failed")),
(
"context-error",
"running",
Some("context_record_failed:fixture"),
),
] {
connection
.execute(
"INSERT INTO execution_records(id,value,updated_at) VALUES (?1,?2,1)",
params![
id,
serde_json::json!({"status":execution_status}).to_string()
],
)
.unwrap();
if let Some(status) = run_status {
connection
.execute(
"INSERT INTO runs(id,value,updated_at) VALUES (?1,?2,1)",
params![id, serde_json::json!({"status":status}).to_string()],
)
.unwrap();
}
}
connection.execute("INSERT INTO runs(id,value,updated_at) VALUES ('orphan','{\"status\":\"running\"}',1)", []).unwrap();
}
let before = store.connection.lock().unwrap().total_changes();
assert_eq!(store.active_execution_count().unwrap(), 7);
assert_eq!(store.connection.lock().unwrap().total_changes(), before);
let connection = store.connection.lock().unwrap();
let mut statement = connection
.prepare(&format!("EXPLAIN QUERY PLAN {ACTIVE_EXECUTION_COUNT_SQL}"))
.unwrap();
let plan = statement
.query_map([], |row| row.get::<_, String>(3))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
.join("\n");
assert!(plan.contains("execution_records_active"), "{plan}");
assert!(plan.contains("runs_active"), "{plan}");
}
#[test]
fn recovery_reads_only_active_execution_payloads() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
store
.put_execution_record(&fixture_execution("active", None, 1))
.unwrap();
store.connection.lock().unwrap().execute(
"INSERT INTO execution_records(id,value,updated_at) VALUES ('terminal','{\"status\":\"completed\"}',2)", []
).unwrap();
assert_eq!(
store
.recoverable_execution_records()
.unwrap()
.iter()
.map(|e| e.id.as_str())
.collect::<Vec<_>>(),
["active"]
);
let connection = store.connection.lock().unwrap();
let plan = connection.prepare("EXPLAIN QUERY PLAN SELECT value FROM execution_records WHERE json_extract(value, '$.status') IN ('queued', 'running', 'stalled', 'interrupted')")
.unwrap().query_map([], |row| row.get::<_, String>(3)).unwrap()
.collect::<Result<Vec<_>, _>>().unwrap().join("\n");
assert!(plan.contains("execution_records_active"), "{plan}");
}
#[test]
fn execution_family_id_query_bounds_cycles_and_uses_parent_index() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
for (id, parent) in [
("root", Some("leaf")),
("child", Some("root")),
("leaf", Some("child")),
("other", None),
] {
store
.put_execution_record(&fixture_execution(id, parent, 1))
.unwrap();
}
let mut ids = store.execution_family_ids("root").unwrap();
ids.sort();
assert_eq!(ids, ["child", "leaf", "root"]);
assert_eq!(store.execution_family_ids("missing").unwrap(), ["missing"]);
assert_eq!(
store
.execution_children("root")
.unwrap()
.iter()
.map(|e| e.id.as_str())
.collect::<Vec<_>>(),
["child"]
);
let connection = store.connection.lock().unwrap();
let plan = connection.prepare("EXPLAIN QUERY PLAN WITH RECURSIVE family(id) AS (SELECT 'root' UNION SELECT child.id FROM execution_records child JOIN family ON child.parent_id = family.id) SELECT id FROM family")
.unwrap().query_map([], |row| row.get::<_, String>(3)).unwrap()
.collect::<Result<Vec<_>, _>>().unwrap().join("\n");
assert!(plan.contains("execution_records_parent"), "{plan}");
}
#[test]
fn recovery_ignores_terminal_run_and_code_payloads() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
{
let connection = store.connection.lock().unwrap();
connection.execute("INSERT INTO runs(id,value,updated_at) VALUES ('terminal','{\"status\":\"completed\"}',1)", []).unwrap();
connection.execute("INSERT INTO executions(id,value,updated_at) VALUES ('terminal','{\"status\":\"completed\"}',1)", []).unwrap();
}
assert!(store.recoverable_runs().unwrap().is_empty());
assert_eq!(store.interrupt_orphaned_code_executions().unwrap(), 0);
let connection = store.connection.lock().unwrap();
for (sql, index) in [
(
"SELECT value FROM runs WHERE json_extract(value, '$.status') IN ('starting', 'running') OR json_extract(value, '$.status') GLOB 'context_record_failed:*'",
"runs_active",
),
(
"SELECT value FROM executions WHERE json_extract(value, '$.status') = 'running'",
"executions_running",
),
] {
let plan = connection
.prepare(&format!("EXPLAIN QUERY PLAN {sql}"))
.unwrap()
.query_map([], |row| row.get::<_, String>(3))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap()
.join("\n");
assert!(plan.contains(index), "{plan}");
}
}
#[test]
fn telemetry_retention_keeps_newest_stable_sequence() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let sample = |sequence| TelemetrySample {
sequence,
sampled_at: 100 + sequence,
wall_time_ms: sequence,
process_count: 0,
cpu_percent: 0.0,
cpu_time_ms: 0,
memory_bytes: 0,
virtual_memory_bytes: 0,
disk_read_bytes: 0,
disk_written_bytes: 0,
processes: Vec::new(),
};
let one_sample_bytes = serde_json::to_vec(&sample(0)).unwrap().len() as u64 + 1;
store
.put_telemetry_capture(&TelemetryCapture {
schema_version: SCHEMA_VERSION,
id: "capture".to_string(),
execution_id: "execution".to_string(),
interval_ms: 250,
max_bytes: one_sample_bytes + 8,
status: TelemetryCaptureStatus::Active,
started_at: 100,
stopped_at: None,
next_sequence: 0,
sample_count: 0,
retained_bytes: 0,
dropped_samples: 0,
error: None,
})
.unwrap();
store
.append_telemetry_sample("capture", sample(999))
.unwrap();
store
.append_telemetry_sample("capture", sample(999))
.unwrap();
let capture = store.get_telemetry_capture("capture").unwrap().unwrap();
assert_eq!(capture.next_sequence, 2);
assert_eq!(capture.sample_count, 1);
assert_eq!(capture.dropped_samples, 1);
let samples = store.telemetry_samples("capture").unwrap();
assert_eq!(samples.len(), 1);
assert_eq!(samples[0].sequence, 1);
}
#[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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.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, DATABASE_VERSION);
}
#[test]
fn migrates_v9_into_durable_debugger_tables_after_backup() {
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);
PRAGMA user_version = 9;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let tables = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN (
'debugger_sessions', 'debugger_selections', 'debugger_breakpoints',
'debugger_mutations', 'debugger_events'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(tables, 5);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v10_into_debugger_artifacts_after_backup() {
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);
PRAGMA user_version = 10;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let table = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'table' AND name = 'debugger_artifacts'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(table, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v11_into_debugger_recording_tables_after_backup() {
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);
PRAGMA user_version = 11;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let tables = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN (
'debugger_recordings', 'debugger_captures', 'debugger_marks'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(tables, 3);
let active_index = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'debugger_recordings_active_session'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(active_index, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v12_into_debugger_after_table_after_backup() {
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);
PRAGMA user_version = 12;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let table = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'table' AND name = 'debugger_afters'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(table, 1);
let index = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'debugger_afters_recording'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(index, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v13_into_causal_time_index_after_backup() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let connection = Connection::open(&path).unwrap();
connection
.execute_batch(
"CREATE TABLE 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
);
PRAGMA user_version = 13;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let index = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'causal_events_time'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(index, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v14_into_debugger_flow_tables_after_backup() {
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);
PRAGMA user_version = 14;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let tables = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN (
'debugger_flows', 'debugger_flow_steps', 'debugger_flow_runs'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(tables, 3);
let active_index = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'debugger_flows_active_session'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(active_index, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v15_into_debugger_tap_and_watch_tables_after_backup() {
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);
PRAGMA user_version = 15;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let tables = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN (
'debugger_taps', 'debugger_tap_hits',
'debugger_watches', 'debugger_watch_hits'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(tables, 4);
let indexes = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name IN (
'debugger_tap_hits_tap', 'debugger_watch_hits_watch'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(indexes, 2);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v16_into_debugger_evidence_trace_tables_after_backup() {
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);
PRAGMA user_version = 16;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let tables = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN (
'debugger_evidence_traces', 'debugger_evidence_trace_selections',
'debugger_trace_import_items'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(tables, 3);
let indexes = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name IN (
'debugger_evidence_traces_active_session',
'debugger_evidence_traces_recent',
'debugger_trace_import_items_time'
)",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(indexes, 3);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn migrates_v17_into_debugger_rr_trace_state_after_backup() {
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);
PRAGMA user_version = 17;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let table = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'table' AND name = 'debugger_rr_traces'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(table, 1);
let index = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'debugger_rr_traces_status'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(index, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn a_stored_debugger_session_carries_no_script_inventory() {
use crate::debugger::{
DebuggerAdapterKind, DebuggerDesiredState, DebuggerScript, DebuggerSessionRecord,
DebuggerSnapshot,
};
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let observed = DebuggerSnapshot {
scripts: (0..5_000)
.map(|index| DebuggerScript {
id: format!("script-{index}"),
url: format!("https://example.test/bundle-{index}.js"),
file: format!("bundle-{index}.js"),
start_line: 0,
end_line: 400,
source_map_url: Some(format!("bundle-{index}.js.map")),
module: true,
})
.collect(),
..DebuggerSnapshot::default()
};
let record = DebuggerSessionRecord {
id: "session-1".to_string(),
adapter: DebuggerAdapterKind::BrowserCdp,
target: "http://localhost/".to_string(),
target_id: None,
host: None,
port: None,
execution_id: None,
desired_state: DebuggerDesiredState::Open,
observed_state: observed,
created_at: 1,
updated_at: 2,
};
store.put_debugger_session(&record).unwrap();
let stored: Vec<DebuggerSessionRecord> = store.debugger_sessions().unwrap();
assert_eq!(stored.len(), 1);
assert!(
stored[0].observed_state.scripts.is_empty(),
"scripts must not be persisted"
);
let bytes: usize = store
.connection
.lock()
.unwrap()
.query_row(
"SELECT length(value) FROM debugger_sessions WHERE id = ?1",
params!["session-1"],
|row| row.get::<_, i64>(0),
)
.unwrap() as usize;
assert!(
bytes < 8 * 1024,
"a session row must stay small regardless of how many scripts a page parsed, got {bytes} bytes"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn capability_snapshots_are_stored_once_and_rehydrated() {
use incurs_codemode::{CapabilitySnapshot, ExecutionState, RuntimeStore};
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let connectors = vec![incurs_codemode::ConnectorDescription {
name: "fixture".to_string(),
instructions: Some("x".repeat(64 * 1024)),
tools: Vec::new(),
}];
let snapshot = CapabilitySnapshot::new(connectors).unwrap();
for id in ["exec-a", "exec-b"] {
let execution = ExecutionState {
id: id.to_string(),
code: "return 1;".to_string(),
status: incurs_codemode::ExecutionStatus::Completed,
log: Vec::new(),
result: None,
error: None,
logs: Vec::new(),
connectors: vec!["fixture".to_string()],
capabilities: Some(snapshot.clone()),
events: Vec::new(),
created_at: 1,
updated_at: 2,
};
store.put_execution(&execution).await.unwrap();
}
let interned: i64 = store
.connection
.lock()
.unwrap()
.query_row("SELECT count(*) FROM capability_snapshots", [], |row| {
row.get(0)
})
.unwrap();
assert_eq!(interned, 1, "one connector set must be stored once");
let row_bytes: i64 = store
.connection
.lock()
.unwrap()
.query_row("SELECT max(length(value)) FROM executions", [], |row| {
row.get(0)
})
.unwrap();
assert!(
row_bytes < 4 * 1024,
"an execution row must not carry the connector schemas, got {row_bytes} bytes"
);
let read = store.get_execution("exec-a").await.unwrap().unwrap();
let capabilities = read
.capabilities
.expect("capabilities survive the round trip");
assert_eq!(capabilities.connectors.len(), 1);
assert_eq!(capabilities.fingerprint, snapshot.fingerprint);
assert_eq!(
capabilities.connectors[0]
.instructions
.as_deref()
.map(str::len),
Some(64 * 1024)
);
}
#[test]
fn migrates_v18_into_hook_tables_after_backup() {
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);
PRAGMA user_version = 18;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.collect::<Vec<_>>();
assert_eq!(backups.len(), 1);
let connection = Connection::open(path).unwrap();
let tables = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'table' AND name IN ('hooks', 'hook_deliveries')",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(tables, 2);
let pending_index = connection
.query_row(
"SELECT COUNT(*) FROM sqlite_master
WHERE type = 'index' AND name = 'hook_deliveries_pending'",
[],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(pending_index, 1);
let version = connection
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
.unwrap();
assert_eq!(version, DATABASE_VERSION);
}
#[test]
fn stores_debugger_artifacts_by_content_digest_and_session() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let bytes = b"durable screenshot bytes";
let digest = format!("{:x}", Sha256::digest(bytes));
let artifact = store
.put_debugger_artifact(
"browser-1",
Some("execution-1"),
"browser_screenshot",
"image/png",
bytes,
)
.unwrap();
assert_eq!(artifact.id, format!("sha256:{digest}"));
assert_eq!(artifact.bytes, bytes.len() as u64);
assert_eq!(std::fs::read(&artifact.path).unwrap(), bytes);
assert_eq!(
artifact.path,
directory
.path()
.join("artifacts/sha256")
.join(digest)
.to_string_lossy()
);
let connection = store.connection.lock().unwrap();
let linked = connection
.query_row(
"SELECT COUNT(*) FROM debugger_artifacts
WHERE id = ?1 AND session_id = 'browser-1'
AND execution_id = 'execution-1' AND media_type = 'image/png'",
[artifact.id],
|row| row.get::<_, u32>(0),
)
.unwrap();
assert_eq!(linked, 1);
}
#[test]
fn migrates_v8_execution_parent_ids_into_indexed_column() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let connection = Connection::open(&path).unwrap();
connection
.execute_batch(
"CREATE TABLE execution_records (
id TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
);
INSERT INTO execution_records(id, value, updated_at) VALUES
('outer', '{\"id\":\"outer\",\"parent_id\":null}', 1),
('child', '{\"id\":\"child\",\"parent_id\":\"outer\"}', 2);
PRAGMA user_version = 8;",
)
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.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, DATABASE_VERSION);
let parent_id = connection
.query_row(
"SELECT parent_id FROM execution_records WHERE id = 'child'",
[],
|row| row.get::<_, String>(0),
)
.unwrap();
assert_eq!(parent_id, "outer");
let indexed_columns = connection
.prepare("PRAGMA index_info(execution_records_parent)")
.unwrap()
.query_map([], |row| row.get::<_, String>(2))
.unwrap()
.collect::<Result<Vec<_>, _>>()
.unwrap();
assert_eq!(indexed_columns, ["parent_id", "updated_at", "id"]);
}
#[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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.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;
DROP INDEX invocations_recording;
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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.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(),
suppressed_env: Vec::new(),
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);
let mut restarted = process.clone();
restarted.identity.started_at = restarted.identity.started_at.saturating_add(1);
restarted.identity.started_at_ns = restarted.identity.started_at_ns.saturating_add(1);
store
.record_processes(
std::slice::from_ref(&restarted),
"process.get",
"observed",
None,
&controller,
"observed",
None,
&serde_json::json!({"lifetime": "second"}),
)
.unwrap();
let dossiers = store
.process_dossiers_for_pid(process.identity.pid, 10)
.unwrap();
assert_eq!(dossiers.len(), 2);
assert_eq!(
dossiers[0].context.latest.identity.started_at,
restarted.identity.started_at
);
assert_eq!(dossiers[0].events.len(), 1);
assert_eq!(dossiers[1].events.len(), 2);
assert!(
store
.process_dossiers_for_pid(u32::MAX, 10)
.unwrap()
.is_empty()
);
}
#[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"
);
let expired = Reservation {
id: "reservation-expired".to_string(),
key: "tcp/5174".to_string(),
lease_id: "lease-expired".to_string(),
expires_at: Some(0),
..first.clone()
};
let replacement = Reservation {
id: "reservation-replacement".to_string(),
lease_id: "lease-replacement".to_string(),
expires_at: None,
..expired.clone()
};
store.acquire_reservation(&expired).unwrap();
store.acquire_reservation(&replacement).unwrap();
assert!(
store
.get_reservation(&expired.id)
.unwrap()
.unwrap()
.released
);
assert_eq!(
store
.get_reservation(&replacement.id)
.unwrap()
.unwrap()
.lease_id,
"lease-replacement"
);
}
#[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 payload_size_checks_do_not_scan_pruned_history() {
use std::sync::atomic::{AtomicUsize, Ordering};
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
{
let mut connection = store.connection.lock().unwrap();
let transaction = connection.transaction().unwrap();
for index in 0..10_000 {
transaction
.execute(
"INSERT INTO causal_payloads
(id, kind, execution_id, value, bytes, created_at, accessed_at, pinned, pruned)
VALUES (?1, 'fixture', NULL, NULL, 50, 1, 1, 0, 1)",
[format!("pruned-{index}")],
)
.unwrap();
}
transaction.commit().unwrap();
}
let payload = store
.put_causal_payload("fixture", None, &serde_json::json!({"kept": true}))
.unwrap();
let steps = Arc::new(AtomicUsize::new(0));
let measured = steps.clone();
store
.connection
.lock()
.unwrap()
.progress_handler(
1000,
Some(move || measured.fetch_add(1000, Ordering::Relaxed) >= 10000),
)
.unwrap();
let result = store.prune_causal_payloads_to_size(u64::MAX);
store
.connection
.lock()
.unwrap()
.progress_handler(0, None::<fn() -> bool>)
.unwrap();
assert!(
result.is_ok(),
"payload size check scanned pruned history: {result:?}"
);
assert_eq!(result.unwrap(), 0);
assert!(steps.load(Ordering::Relaxed) <= 10000);
assert!(store.causal_payload(&payload.id).unwrap().is_some());
}
#[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());
}
#[test]
fn exercises_public_named_execution_poll_and_ownership_persistence() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let store = Store::open(&path).unwrap();
assert_eq!(store.path(), path);
let recipe = serde_json::json!({"name": "fixture", "code": "return 1;"});
store.put_recipe("fixture", &recipe).unwrap();
assert_eq!(store.get_recipe("fixture").unwrap(), Some(recipe.clone()));
assert_eq!(store.list_recipes().unwrap(), [recipe]);
assert!(store.delete_recipe("fixture").unwrap());
assert!(!store.delete_recipe("fixture").unwrap());
let receipt = serde_json::json!({"state": "applied"});
store.put_receipt("receipt", "fixture", &receipt).unwrap();
assert_eq!(
store.get_receipt::<Value>("receipt").unwrap(),
Some(receipt)
);
assert!(store.get_receipt::<Value>("missing").unwrap().is_none());
let provider = serde_json::json!({"name": "provider"});
store.put_connector("provider", &provider).unwrap();
assert_eq!(store.get_connector("provider").unwrap(), Some(provider));
assert_eq!(store.list_connectors().unwrap().len(), 1);
assert!(store.delete_connector("provider").unwrap());
assert!(!store.delete_provider("provider").unwrap());
let legacy = serde_json::json!({"name": "legacy"});
store
.put_named("connectors", "name", "legacy", &legacy)
.unwrap();
assert_eq!(store.get_connector("legacy").unwrap(), Some(legacy));
let spec = fixture_run_spec();
let run = ManagedRun {
id: "run".to_string(),
spec: spec.clone(),
status: "running".to_string(),
processes: Vec::new(),
process_history: Vec::new(),
created_at: 1,
updated_at: 2,
exit_code: None,
stdout_path: None,
stderr_path: None,
};
store.put_run(&run).unwrap();
assert!(store.delete_run("run").unwrap());
assert!(store.get_run("run").unwrap().is_none());
let execution = fixture_execution("execution", None, 10);
store.put_execution_record(&execution).unwrap();
assert_eq!(
store.get_execution_record("execution").unwrap().unwrap().id,
"execution"
);
assert_eq!(store.list_execution_records().unwrap().len(), 1);
assert_eq!(
store
.list_execution_records_page(None, Some("queued"), None, None, 10)
.unwrap()
.len(),
1
);
assert!(matches!(
store
.claim_execution(
&fixture_execution("claimed", None, 20),
"key",
"fingerprint"
)
.unwrap(),
ExecutionClaim::Claimed
));
assert!(matches!(
store
.claim_execution(&fixture_execution("other", None, 30), "key", "fingerprint")
.unwrap(),
ExecutionClaim::Replayed(existing) if existing.id == "claimed"
));
assert!(
store
.claim_execution(&fixture_execution("conflict", None, 40), "key", "different")
.is_err()
);
store
.connection
.lock()
.unwrap()
.execute_batch(
"PRAGMA foreign_keys = OFF;
DELETE FROM execution_records WHERE id = 'claimed';
PRAGMA foreign_keys = ON;",
)
.unwrap();
let corrupt = store.claim_execution(
&fixture_execution("corrupt", None, 50),
"key",
"fingerprint",
);
assert!(matches!(corrupt, Err(error) if error.contains("CORRUPT")));
let mut iteration = PollIteration {
id: "iteration".to_string(),
poll_id: "execution".to_string(),
number: 1,
status: PollIterationStatus::Running,
started_at: 10,
completed_at: None,
execution_id: None,
payload_id: None,
observation: None,
matched: false,
evaluated: false,
error_code: None,
error: None,
};
store.put_poll_iteration(&iteration).unwrap();
assert!(
store
.get_poll_iteration("other", "iteration")
.unwrap()
.is_none()
);
assert_eq!(
store
.get_poll_iteration("execution", "iteration")
.unwrap()
.unwrap()
.number,
1
);
assert_eq!(
store
.get_poll_iteration_number("execution", 1)
.unwrap()
.unwrap()
.id,
"iteration"
);
iteration.status = PollIterationStatus::Observed;
iteration.completed_at = Some(20);
iteration.observation = Some(serde_json::json!({"ok": true}));
store.put_poll_iteration(&iteration).unwrap();
assert_eq!(
store
.list_poll_iterations("execution", None, 10)
.unwrap()
.len(),
1
);
assert!(
store
.list_poll_iterations("execution", Some(1), 10)
.unwrap()
.is_empty()
);
assert!(
store
.finish_invocation("missing", InvocationStatus::Failed, None, None, None, None)
.unwrap()
.is_none()
);
assert!(
store
.update_invocation_input("missing", None, Value::Null, "missing".to_string())
.unwrap()
.is_none()
);
assert!(
store
.link_invocation_execution("missing", "execution")
.unwrap()
.is_none()
);
let now = now_ms();
let mut session = AgentSession {
id: "session".to_string(),
actor: "agent".to_string(),
labels: BTreeMap::new(),
created_at: now,
heartbeat_at: now,
expires_at: now + 60_000,
closing: false,
closed: false,
};
store.put_session(&session).unwrap();
assert_eq!(
store.get_session("session").unwrap().unwrap().actor,
"agent"
);
assert_eq!(store.list_sessions(true).unwrap().len(), 1);
session.closed = true;
store.put_session(&session).unwrap();
assert!(store.list_sessions(true).unwrap().is_empty());
assert_eq!(store.list_sessions(false).unwrap().len(), 1);
let lease = Lease {
id: "lease".to_string(),
owner_kind: LeaseOwnerKind::Session,
owner_id: "session".to_string(),
resources: vec!["custom/fixture".to_string()],
created_at: now,
renewed_at: now,
expires_at: Some(now + 60_000),
released: false,
};
store.put_lease(&lease).unwrap();
assert_eq!(
store.get_lease("lease").unwrap().unwrap().owner_id,
"session"
);
assert_eq!(store.list_leases(true).unwrap().len(), 1);
let reservation = Reservation {
id: "owned".to_string(),
kind: ReservationKind::Custom,
key: "fixture".to_string(),
lease_id: "lease".to_string(),
created_at: now,
expires_at: None,
external_conflict: false,
released: false,
};
store.acquire_reservation(&reservation).unwrap();
assert!(store.release_lease("missing").unwrap().is_none());
assert!(store.release_lease("lease").unwrap().unwrap().released);
assert!(store.get_reservation("owned").unwrap().unwrap().released);
assert!(store.list_leases(true).unwrap().is_empty());
assert_eq!(store.list_leases(false).unwrap().len(), 1);
assert!(store.release_reservation("missing").unwrap().is_none());
assert!(store.delete_execution_record("execution").unwrap());
assert!(!store.delete_execution_record("execution").unwrap());
}
#[tokio::test]
async fn exercises_runtime_and_artifact_store_contracts() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let execution = ExecutionState {
id: "code".to_string(),
code: "return 1;".to_string(),
status: incurs_codemode::ExecutionStatus::Completed,
log: Vec::new(),
result: Some(serde_json::json!(1)),
error: None,
logs: vec!["complete".to_string()],
connectors: Vec::new(),
capabilities: None,
events: Vec::new(),
created_at: 1,
updated_at: 2,
};
RuntimeStore::put_execution(&store, &execution)
.await
.unwrap();
assert_eq!(
RuntimeStore::get_execution(&store, "code")
.await
.unwrap()
.unwrap()
.code,
"return 1;"
);
assert_eq!(
RuntimeStore::list_executions(&store).await.unwrap().len(),
1
);
RuntimeStore::delete_execution(&store, "code")
.await
.unwrap();
assert!(
RuntimeStore::get_execution(&store, "code")
.await
.unwrap()
.is_none()
);
let snippet = Snippet {
name: "snippet".to_string(),
description: "Fixture".to_string(),
code: "return input;".to_string(),
saved_at: 1,
input_schema: None,
connectors: Vec::new(),
};
RuntimeStore::put_snippet(&store, &snippet).await.unwrap();
assert_eq!(
RuntimeStore::get_snippet(&store, "snippet")
.await
.unwrap()
.unwrap(),
snippet
);
assert_eq!(RuntimeStore::list_snippets(&store).await.unwrap().len(), 1);
assert!(
RuntimeStore::delete_snippet(&store, "snippet")
.await
.unwrap()
);
assert!(
!RuntimeStore::delete_snippet(&store, "snippet")
.await
.unwrap()
);
let large = serde_json::json!({"value": "x".repeat(600)});
let artifact = ArtifactStore::put(&store, "code", &large).await.unwrap();
assert_eq!(artifact.execution_id, "code");
assert_eq!(artifact.preview.chars().count(), 515);
assert!(artifact.preview.ends_with("..."));
assert_eq!(
ArtifactStore::get(&store, "code", &artifact.id)
.await
.unwrap(),
Some(large)
);
assert!(
ArtifactStore::get(&store, "other", &artifact.id)
.await
.unwrap()
.is_none()
);
ArtifactStore::delete_execution(&store, "code")
.await
.unwrap();
assert!(
ArtifactStore::get(&store, "code", &artifact.id)
.await
.unwrap()
.is_none()
);
}
fn fixture_run_spec() -> RunSpec {
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(),
suppressed_env: Vec::new(),
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,
}
}
#[test]
fn execution_listing_tracks_run_changes_before_public_record_sync() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let mut run = ManagedRun {
id: "listed".to_string(),
spec: fixture_run_spec(),
status: "completed".to_string(),
processes: Vec::new(),
process_history: Vec::new(),
created_at: 1,
updated_at: 30,
exit_code: Some(0),
stdout_path: None,
stderr_path: None,
};
store.put_run(&run).unwrap();
let mut execution = fixture_execution("listed", Some("owner"), 10);
store.put_execution_record(&execution).unwrap();
store
.put_execution_record(&fixture_execution("older", None, 20))
.unwrap();
let listed = |status, error| {
store
.list_execution_records_page(None, Some(status), Some("owner"), error, 2)
.unwrap()
};
assert_eq!(listed("completed", None)[0].id, "listed");
assert!(listed("queued", None).is_empty());
assert_eq!(
store
.get_execution_record("listed")
.unwrap()
.unwrap()
.status,
ExecutionStatus::Queued,
"listing must not synchronize or rewrite the public record",
);
run.status = "timed_out".to_string();
run.updated_at = 40;
store.put_run(&run).unwrap();
assert_eq!(
listed("canceled", Some("EXECUTION_TIMEOUT"))[0].id,
"listed"
);
let page = store
.list_execution_records_page(None, None, None, None, 1)
.unwrap();
assert_eq!(page[0].id, "listed");
let next = store
.list_execution_records_page(Some((40, "listed")), None, None, None, 1)
.unwrap();
assert_eq!(next[0].id, "older");
run.status = "running".to_string();
run.updated_at = 50;
store.put_run(&run).unwrap();
execution.status = ExecutionStatus::Stalled;
execution.updated_at = 60;
store.put_execution_record(&execution).unwrap();
assert_eq!(listed("stalled", None)[0].id, "listed");
assert!(listed("running", None).is_empty());
store.delete_run("listed").unwrap();
assert_eq!(listed("stalled", None)[0].id, "listed");
execution.status = ExecutionStatus::Completed;
execution.updated_at = 70;
store.put_execution_record(&execution).unwrap();
assert_eq!(listed("completed", None)[0].id, "listed");
store.delete_execution_record("listed").unwrap();
assert!(listed("completed", None).is_empty());
}
#[test]
fn expired_lease_selection_does_not_scan_released_history() {
use std::sync::atomic::{AtomicUsize, Ordering};
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let mut old = Lease {
id: "old".to_string(),
owner_kind: LeaseOwnerKind::Session,
owner_id: "fixture".to_string(),
resources: vec![],
created_at: 1,
renewed_at: 1,
expires_at: Some(10),
released: true,
};
{
let mut connection = store.connection.lock().unwrap();
let transaction = connection.transaction().unwrap();
for index in 0..10_000 {
old.id = format!("old-{index}");
transaction.execute(
"INSERT INTO leases (id, owner_kind, owner_id, expires_at, released, value, updated_at)
VALUES (?1, 'session', 'fixture', 10, 1, ?2, 1)",
params![old.id, serde_json::to_string(&old).unwrap()],
).unwrap();
}
transaction.commit().unwrap();
}
for (id, expiry, released) in [
("expired", Some(49), false),
("boundary", Some(50), false),
("future", Some(51), false),
("persistent", None, false),
("released", Some(49), true),
] {
old.id = id.to_string();
old.expires_at = expiry;
old.released = released;
store.put_lease(&old).unwrap();
}
let steps = Arc::new(AtomicUsize::new(0));
let measured = steps.clone();
store
.connection
.lock()
.unwrap()
.progress_handler(
1000,
Some(move || measured.fetch_add(1000, Ordering::Relaxed) >= 10000),
)
.unwrap();
let result = store.list_expired_leases(50);
store
.connection
.lock()
.unwrap()
.progress_handler(0, None::<fn() -> bool>)
.unwrap();
assert!(
result.is_ok(),
"expired lease selection scanned released history: {result:?}"
);
let mut ids = result
.unwrap()
.into_iter()
.map(|lease| lease.id)
.collect::<Vec<_>>();
ids.sort();
assert_eq!(ids, ["boundary", "expired"]);
assert!(steps.load(Ordering::Relaxed) <= 10000);
}
#[test]
fn older_client_schema_marker_does_not_repeat_a_completed_listing_migration() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let store = Store::open(&path).unwrap();
store
.put_execution_record(&fixture_execution("existing", None, 10))
.unwrap();
drop(store);
let connection = Connection::open(&path).unwrap();
connection
.execute_batch(
"PRAGMA user_version = 21;
CREATE TRIGGER forbid_listing_rebuild BEFORE INSERT ON execution_listing
BEGIN SELECT RAISE(ABORT, 'already migrated listing was rebuilt'); END;",
)
.unwrap();
drop(connection);
let reopened = Store::open(&path).unwrap();
assert_eq!(
reopened
.list_execution_records_page(None, None, None, None, 1)
.unwrap()[0]
.id,
"existing"
);
assert!(!std::fs::read_dir(directory.path()).unwrap().any(|entry| {
entry
.unwrap()
.file_name()
.to_string_lossy()
.contains(".pre-v")
}));
}
#[test]
fn migrates_v21_effective_execution_listing_without_rewriting_history() {
let directory = tempdir().unwrap();
let path = directory.path().join("state.sqlite3");
let connection = Connection::open(&path).unwrap();
connection.execute_batch(
"CREATE TABLE execution_records (
id TEXT PRIMARY KEY, parent_id TEXT, value TEXT NOT NULL, updated_at INTEGER NOT NULL
);
CREATE TABLE runs (
id TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at INTEGER NOT NULL
);
PRAGMA user_version = 21;",
).unwrap();
let old = fixture_execution("legacy", Some("owner"), 10);
let value = serde_json::to_string(&old).unwrap();
connection
.execute(
"INSERT INTO execution_records VALUES ('legacy', 'owner', ?1, 10)",
[&value],
)
.unwrap();
connection
.execute(
"INSERT INTO runs VALUES ('legacy', '{\"status\":\"timed_out\"}', 20)",
[],
)
.unwrap();
drop(connection);
let store = Store::open(&path).unwrap();
let page = store
.list_execution_records_page(
None,
Some("canceled"),
Some("owner"),
Some("EXECUTION_TIMEOUT"),
1,
)
.unwrap();
assert_eq!(page[0].id, "legacy");
assert_eq!(serde_json::to_string(&page[0]).unwrap(), value);
assert_eq!(
store
.connection
.lock()
.unwrap()
.query_row(
"SELECT updated_at FROM execution_listing WHERE id = 'legacy'",
[],
|row| row.get::<_, i64>(0),
)
.unwrap(),
20,
);
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(&format!("state.sqlite3.pre-v{DATABASE_VERSION}-")))
.count();
assert_eq!(backups, 1);
drop(store);
let reopened = Store::open(&path).unwrap();
assert_eq!(
reopened
.list_execution_records_page(
None,
Some("canceled"),
None,
Some("EXECUTION_TIMEOUT"),
1,
)
.unwrap()[0]
.id,
"legacy"
);
}
#[test]
fn execution_pages_bound_database_work_with_large_terminal_history() {
use std::sync::atomic::{AtomicUsize, Ordering};
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let mut old = fixture_execution("old", None, 1);
old.status = ExecutionStatus::Completed;
let value = serde_json::to_string(&old).unwrap();
store
.connection
.lock()
.unwrap()
.execute(
"WITH RECURSIVE history(n) AS (
SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 10000
)
INSERT INTO execution_records(id, parent_id, value, updated_at)
SELECT printf('old-%05d', n), NULL,
json_set(?1, '$.id', printf('old-%05d', n)), n FROM history",
[value],
)
.unwrap();
let live = fixture_execution("live", Some("owner"), 20000);
store.put_execution_record(&live).unwrap();
for (status, parent, error) in [
(None, None, None),
(Some("queued"), None, None),
(None, Some("owner"), None),
(Some("queued"), Some("owner"), None),
(None, None, Some("EXECUTION_TIMEOUT")),
(Some("queued"), None, Some("EXECUTION_TIMEOUT")),
(None, Some("owner"), Some("EXECUTION_TIMEOUT")),
(Some("canceled"), Some("owner"), Some("EXECUTION_TIMEOUT")),
] {
let steps = Arc::new(AtomicUsize::new(0));
let measured = steps.clone();
store
.connection
.lock()
.unwrap()
.progress_handler(
1000,
Some(move || measured.fetch_add(1000, Ordering::Relaxed) >= 10000),
)
.unwrap();
let result = store.list_execution_records_page(None, status, parent, error, 2);
store
.connection
.lock()
.unwrap()
.progress_handler(0, None::<fn() -> bool>)
.unwrap();
assert!(
result.is_ok(),
"small execution page scanned terminal history: {result:?}"
);
assert!(steps.load(Ordering::Relaxed) <= 10000);
if error.is_none() {
assert_eq!(result.unwrap()[0].id, "live");
} else {
assert!(result.unwrap().is_empty());
}
}
}
fn fixture_execution(id: &str, parent_id: Option<&str>, at: u64) -> Execution {
Execution {
id: id.to_string(),
parent_id: parent_id.map(str::to_string),
session_id: None,
spec: ExecutionSpec {
schema_version: SCHEMA_VERSION,
idempotency_key: None,
name: id.to_string(),
purpose: Some("Fixture execution".to_string()),
labels: BTreeMap::new(),
program: ExecutionProgram::Command {
argv: vec!["/usr/bin/true".to_string()],
shell: false,
},
backend: crate::model::ExecutionBackendSpec::default(),
workspace: WorkspaceContext::default(),
env: BTreeMap::new(),
secrets: BTreeMap::new(),
profile_selection: None,
io: IoMode::Pipes,
owner_kind: LeaseOwnerKind::Persistent,
owner_id: None,
expires_at: None,
retry: RetryPolicy::default(),
budget: ExecutionBudget::default(),
telemetry: crate::model::TelemetryPolicy::default(),
expect: None,
},
status: ExecutionStatus::Queued,
passes: Vec::new(),
attempts: Vec::new(),
created_at: at,
updated_at: at,
last_progress_at: at,
result: None,
}
}
#[test]
fn validates_session_ownership_during_execution_acceptance() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let now = now_ms();
let mut session = AgentSession {
id: "session-owner".to_string(),
actor: "agent".to_string(),
labels: BTreeMap::new(),
created_at: now,
heartbeat_at: now,
expires_at: now + 60_000,
closing: false,
closed: false,
};
store.put_session(&session).unwrap();
let session_id = session.id.clone();
let owned = |id: &str| {
let mut execution = fixture_execution(id, None, now);
execution.session_id = Some(session_id.clone());
execution.spec.owner_kind = LeaseOwnerKind::Session;
execution.spec.owner_id = Some(session_id.clone());
execution
};
store.accept_execution(&owned("active")).unwrap();
let mut missing_session = owned("missing-session");
missing_session.session_id = None;
assert!(
store
.accept_execution(&missing_session)
.unwrap_err()
.contains("require a session")
);
let mut mismatched = owned("mismatched");
mismatched.spec.owner_id = Some("other-session".to_string());
assert!(
store
.accept_execution(&mismatched)
.unwrap_err()
.contains("does not match")
);
let mut unknown = owned("unknown");
unknown.session_id = Some("unknown-session".to_string());
unknown.spec.owner_id = unknown.session_id.clone();
assert!(
store
.accept_execution(&unknown)
.unwrap_err()
.contains("not found")
);
session.closed = true;
store.put_session(&session).unwrap();
assert!(
store
.accept_execution(&owned("closed"))
.unwrap_err()
.contains("closed or expired")
);
session.closed = false;
session.expires_at = now;
store.put_session(&session).unwrap();
let expired = store.claim_execution(&owned("expired"), "expired-owner", "fingerprint");
assert!(matches!(
expired,
Err(error) if error.contains("closed or expired")
));
}
#[test]
fn rejects_execution_owned_work_after_cancellation_begins() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let now = now_ms();
let mut owner = fixture_execution("canceling-owner", None, now);
owner.status = ExecutionStatus::Running;
owner.result = Some(serde_json::json!({ "cancel_requested": true }));
store.put_execution_record(&owner).unwrap();
let mut child = fixture_execution("late-child", Some(&owner.id), now + 1);
child.spec.owner_kind = LeaseOwnerKind::Execution;
child.spec.owner_id = Some(owner.id.clone());
let error = store.accept_execution(&child).unwrap_err();
assert!(error.contains("is canceling"), "{error}");
}
#[test]
fn trims_recording_captures_without_dangling_after_anchors() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let recording_id = "recording-1";
store
.put_debugger_recording(&DebuggerRecording {
id: recording_id.to_string(),
session_id: "session-1".to_string(),
execution_id: None,
phase: DebuggerRecordingPhase::Running,
max_frames: 2,
max_bytes: 2,
retained_frames: 0,
retained_bytes: 0,
retention_overflow: false,
last_capture_id: None,
error: None,
started_at: 1,
stopped_at: None,
updated_at: 1,
})
.unwrap();
for id in ["capture-1", "capture-2", "capture-3", "capture-4"] {
store
.put_debugger_capture(debugger_capture(recording_id, id))
.unwrap();
}
store
.put_debugger_after(&DebuggerAfter {
id: "after-1".to_string(),
recording_id: recording_id.to_string(),
session_id: "session-1".to_string(),
before_capture_id: "capture-2".to_string(),
after_capture_id: "capture-3".to_string(),
diff_pixels: 1,
diff_percent: 1.0,
region: None,
console: Vec::new(),
exceptions: Vec::new(),
network_failures: Vec::new(),
storage: Vec::new(),
styles: Vec::new(),
accessibility: Vec::new(),
performance: Vec::new(),
report: debugger_artifact("after-report"),
at: 5,
})
.unwrap();
let retained = store.trim_debugger_captures(recording_id, 2, 2).unwrap();
let retained_ids = retained
.iter()
.map(|capture| capture.id.as_str())
.collect::<BTreeSet<_>>();
for id in ["capture-2", "capture-3"] {
assert!(
retained_ids.contains(id),
"trim pruned capture {id} even though an after record references it"
);
assert!(store.debugger_capture(id).unwrap().is_some());
}
}
#[test]
fn trims_recording_payloads_without_deleting_capture_metadata() {
let directory = tempdir().unwrap();
let store = Store::open(directory.path().join("state.sqlite3")).unwrap();
let recording_id = "recording-1";
store
.put_debugger_recording(&DebuggerRecording {
id: recording_id.to_string(),
session_id: "session-1".to_string(),
execution_id: None,
phase: DebuggerRecordingPhase::Running,
max_frames: 4,
max_bytes: 4,
retained_frames: 0,
retained_bytes: 0,
retention_overflow: false,
last_capture_id: None,
error: None,
started_at: 1,
stopped_at: None,
updated_at: 1,
})
.unwrap();
for id in [
"capture-1",
"capture-2",
"capture-3",
"capture-4",
"capture-5",
] {
store
.put_debugger_capture(debugger_capture(recording_id, id))
.unwrap();
}
store
.put_debugger_after(&DebuggerAfter {
id: "after-1".to_string(),
recording_id: recording_id.to_string(),
session_id: "session-1".to_string(),
before_capture_id: "capture-2".to_string(),
after_capture_id: "capture-3".to_string(),
diff_pixels: 1,
diff_percent: 1.0,
region: None,
console: Vec::new(),
exceptions: Vec::new(),
network_failures: Vec::new(),
storage: Vec::new(),
styles: Vec::new(),
accessibility: Vec::new(),
performance: Vec::new(),
report: debugger_artifact("after-report"),
at: 5,
})
.unwrap();
let retained = store.trim_debugger_captures(recording_id, 4, 4).unwrap();
let retained_ids = retained
.iter()
.map(|capture| capture.id.as_str())
.collect::<BTreeSet<_>>();
assert!(!retained_ids.contains("capture-4"));
assert_eq!(
store
.debugger_capture("capture-4")
.unwrap()
.unwrap()
.screenshot
.bytes,
0,
"trim should decay heavy screenshot payload accounting without deleting capture metadata"
);
assert_eq!(store.debugger_captures(recording_id).unwrap().len(), 5);
for id in ["capture-1", "capture-2", "capture-3", "capture-5"] {
assert_eq!(
store
.debugger_capture(id)
.unwrap()
.unwrap()
.screenshot
.bytes,
1,
"trim decayed protected capture {id}"
);
}
}
fn debugger_capture(recording_id: &str, id: &str) -> DebuggerCapture {
DebuggerCapture {
id: id.to_string(),
recording_id: recording_id.to_string(),
session_id: "session-1".to_string(),
sequence: 0,
at: 1,
reason: "test".to_string(),
url: "http://example.test".to_string(),
scroll_y: 0.0,
device_pixel_ratio: 1.0,
screenshot: crate::debugger::BrowserScreenshot {
png_base64: None,
artifact: debugger_artifact(id),
bytes: 1,
width: 1,
height: 1,
selector: None,
state: None,
full_page: false,
},
state: crate::debugger::DebuggerCaptureState::default(),
}
}
fn debugger_artifact(id: &str) -> DebuggerArtifact {
DebuggerArtifact {
id: id.to_string(),
kind: "test".to_string(),
media_type: "image/png".to_string(),
bytes: 1,
path: format!("/tmp/{id}.png"),
created_at: 1,
}
}
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()),
recording_id: None,
recording_take: None,
started_at: at,
completed_at: Some(at + 10),
updated_at: at,
status: InvocationStatus::Succeeded,
exit_code: Some(0),
error_code: None,
error: None,
}
}
}