Some checks failed
CI / Lint & TypeCheck (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
CI / Build Frontend (push) Has been cancelled
CI / Rust Check (push) Has been cancelled
CI / Security Scan (push) Has been cancelled
CI / E2E Tests (push) Has been cancelled
- Schema: migrations now execute ALTER TABLE ADD COLUMN for state/message_count - MemoryStore: add update_agent_runtime() and list_agents_with_runtime() - Registry: add register_with_runtime() to accept persisted state/message_count - Kernel boot: restore agents with their persisted state (not always Running) - Kernel shutdown: persist all agent states/message_counts before terminating Agents that were suspended stay suspended after restart. Message counts survive restarts instead of resetting to 0.
97 lines
2.9 KiB
Rust
97 lines
2.9 KiB
Rust
//! Database schema definitions
|
|
|
|
/// Current schema version
|
|
pub const SCHEMA_VERSION: i32 = 2;
|
|
|
|
/// Schema creation SQL
|
|
pub const CREATE_SCHEMA: &str = r#"
|
|
-- Agents table
|
|
CREATE TABLE IF NOT EXISTS agents (
|
|
id TEXT PRIMARY KEY,
|
|
name TEXT NOT NULL,
|
|
config TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
|
|
-- Sessions table
|
|
CREATE TABLE IF NOT EXISTS sessions (
|
|
id TEXT PRIMARY KEY,
|
|
agent_id TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL,
|
|
FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Messages table
|
|
CREATE TABLE IF NOT EXISTS messages (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
session_id TEXT NOT NULL,
|
|
seq INTEGER NOT NULL,
|
|
content TEXT NOT NULL,
|
|
created_at TEXT NOT NULL,
|
|
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE CASCADE,
|
|
UNIQUE(session_id, seq)
|
|
);
|
|
|
|
-- KV Store table
|
|
CREATE TABLE IF NOT EXISTS kv_store (
|
|
agent_id TEXT NOT NULL,
|
|
key TEXT NOT NULL,
|
|
value TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL,
|
|
PRIMARY KEY (agent_id, key),
|
|
FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Schema version table
|
|
CREATE TABLE IF NOT EXISTS schema_version (
|
|
version INTEGER PRIMARY KEY
|
|
);
|
|
|
|
-- Hand execution runs table
|
|
CREATE TABLE IF NOT EXISTS hand_runs (
|
|
id TEXT PRIMARY KEY,
|
|
hand_name TEXT NOT NULL,
|
|
trigger_source TEXT NOT NULL,
|
|
params TEXT NOT NULL,
|
|
status TEXT NOT NULL DEFAULT 'pending',
|
|
result TEXT,
|
|
error TEXT,
|
|
duration_ms INTEGER,
|
|
created_at TEXT NOT NULL,
|
|
started_at TEXT,
|
|
completed_at TEXT
|
|
);
|
|
|
|
-- Indexes
|
|
CREATE INDEX IF NOT EXISTS idx_sessions_agent ON sessions(agent_id);
|
|
CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_kv_agent ON kv_store(agent_id);
|
|
CREATE INDEX IF NOT EXISTS idx_hand_runs_hand ON hand_runs(hand_name);
|
|
CREATE INDEX IF NOT EXISTS idx_hand_runs_status ON hand_runs(status);
|
|
CREATE INDEX IF NOT EXISTS idx_hand_runs_created ON hand_runs(created_at);
|
|
|
|
-- Structured facts table (extracted from conversations)
|
|
CREATE TABLE IF NOT EXISTS facts (
|
|
id TEXT PRIMARY KEY,
|
|
agent_id TEXT NOT NULL,
|
|
content TEXT NOT NULL,
|
|
category TEXT NOT NULL,
|
|
confidence REAL NOT NULL,
|
|
source_session TEXT,
|
|
created_at INTEGER NOT NULL,
|
|
FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_facts_agent ON facts(agent_id);
|
|
CREATE INDEX IF NOT EXISTS idx_facts_category ON facts(agent_id, category);
|
|
CREATE INDEX IF NOT EXISTS idx_facts_confidence ON facts(agent_id, confidence DESC);
|
|
"#;
|
|
|
|
/// Incremental migrations (safe to run repeatedly via ALTER … ADD COLUMN + IF NOT EXISTS pattern)
|
|
pub const MIGRATIONS: &[&str] = &[
|
|
// v1→v2: persist runtime state and message count
|
|
"ALTER TABLE agents ADD COLUMN state TEXT NOT NULL DEFAULT 'running'",
|
|
"ALTER TABLE agents ADD COLUMN message_count INTEGER NOT NULL DEFAULT 0",
|
|
];
|