Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(store): V26 admits 'zero' into the sessions.agent_kind CHECK

AkitaOnRailscf75dc5ab754
diff --git a/crates/ai-memory-core/src/ids.rs b/crates/ai-memory-core/src/ids.rs
index e6f98e62..12b1e547 100644
--- a/crates/ai-memory-core/src/ids.rs
+++ b/crates/ai-memory-core/src/ids.rs
@@ -205,6 +205,27 @@ pub enum AgentKind {
}
impl AgentKind {
+ /// Every variant, for tests that must cover the full agent surface —
+ /// e.g. the store-level check that the persisted `sessions.agent_kind`
+ /// CHECK constraint accepts every kind (the Zero integration shipped
+ /// with the enum variant but without the V26 migration and only a
+ /// live test caught it). Extend together with the enum.
+ pub const ALL: [Self; 13] = [
+ Self::ClaudeCode,
+ Self::Codex,
+ Self::OpenCode,
+ Self::Cursor,
+ Self::GeminiCli,
+ Self::ClaudeDesktop,
+ Self::OpenClaw,
+ Self::AntigravityCli,
+ Self::Omp,
+ Self::Pi,
+ Self::Grok,
+ Self::Zero,
+ Self::Other,
+ ];
+
/// Kebab-case wire string matching the serde representation.
#[must_use]
pub const fn as_str(self) -> &'static str {
diff --git a/crates/ai-memory-store/migrations/V26__sessions_zero_agent_kind.sql b/crates/ai-memory-store/migrations/V26__sessions_zero_agent_kind.sql
new file mode 100644
index 00000000..85e90c6a
--- /dev/null
+++ b/crates/ai-memory-store/migrations/V26__sessions_zero_agent_kind.sql
@@ -0,0 +1,61 @@
+-- Expand sessions.agent_kind CHECK for the Zero coding agent (`zero`).
+--
+-- V09/V11/V20/V25 intentionally enumerated every supported agent. Adding a
+-- concrete AgentKind must update the persisted CHECK too, or Zero hook
+-- events fail at begin_session on upgraded databases (the hook router
+-- WARNs and drops the session, silently breaking Zero capture
+-- server-side). Caught live during the #156 integration: the ops-level
+-- `begin_session_accepts_every_agent_kind` test now pins the whole class.
+
+PRAGMA foreign_keys = OFF;
+
+DROP TRIGGER IF EXISTS auto_improve_scheduler_claims_session_pairing_ai;
+
+CREATE TABLE sessions_new (
+ id BLOB PRIMARY KEY NOT NULL,
+ workspace_id BLOB NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
+ project_id BLOB NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
+ agent_kind TEXT NOT NULL CHECK (agent_kind IN ('claude-code','codex','open-code','cursor','gemini-cli','claude-desktop','openclaw','antigravity-cli','omp','pi','grok','zero','other')),
+ cwd TEXT,
+ started_at INTEGER NOT NULL,
+ ended_at INTEGER,
+ summary_page_id BLOB REFERENCES pages(id) ON DELETE SET NULL
+);
+
+INSERT INTO sessions_new SELECT * FROM sessions;
+
+DROP TABLE sessions;
+
+ALTER TABLE sessions_new RENAME TO sessions;
+
+CREATE INDEX idx_sessions_recent ON sessions(workspace_id, project_id, started_at DESC);
+CREATE INDEX idx_sessions_project ON sessions(project_id);
+CREATE INDEX idx_sessions_started_at ON sessions(started_at DESC);
+CREATE INDEX idx_sessions_scope_ended
+ ON sessions(workspace_id, project_id, ended_at ASC)
+ WHERE ended_at IS NOT NULL;
+
+-- Recreate the V18 pairing-enforcement trigger: dropping the old `sessions`
+-- table above also dropped its triggers. Every sessions-table rebuild after
+-- V18 must reinstate it or the (workspace_id, project_id) pairing invariant
+-- silently stops being enforced on inserts.
+CREATE TRIGGER sessions_ws_proj_pairing_ai
+BEFORE INSERT ON sessions
+FOR EACH ROW
+WHEN NEW.workspace_id IS NOT (SELECT workspace_id FROM projects WHERE id = NEW.project_id)
+BEGIN
+ SELECT RAISE(ABORT, 'sessions.workspace_id does not match the project''s workspace');
+END;
+
+-- Recreate the V22 claim/session pairing trigger, which references `sessions`
+-- and must be dropped before the table rebuild above.
+CREATE TRIGGER auto_improve_scheduler_claims_session_pairing_ai
+BEFORE INSERT ON auto_improve_scheduler_claims
+FOR EACH ROW
+WHEN NEW.workspace_id IS NOT (SELECT workspace_id FROM sessions WHERE id = NEW.session_id)
+ OR NEW.project_id IS NOT (SELECT project_id FROM sessions WHERE id = NEW.session_id)
+BEGIN
+ SELECT RAISE(ABORT, 'auto_improve_scheduler_claims scope does not match the session scope');
+END;
+
+PRAGMA foreign_keys = ON;
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index 1c3b4afa..c438219d 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -1434,6 +1434,33 @@ mod tests {
/// Open a fresh DB with migrations applied + a default workspace
/// and "scratch" project pre-created. Tuple-return keeps the
/// tempdir alive for the duration of the test.
+ // Issue #156 regression, class-wide: every AgentKind must survive
+ // begin_session — i.e. the persisted sessions.agent_kind CHECK
+ // constraint must enumerate every variant. Zero shipped with the enum
+ // variant but without the CHECK migration (V26) and only a live test
+ // caught the constraint failure; this pins the whole class so the next
+ // agent addition fails here, before any deploy.
+ #[test]
+ fn begin_session_accepts_every_agent_kind() {
+ let (_tmp, mut conn, ws, proj) = fresh_db();
+ for kind in ai_memory_core::AgentKind::ALL {
+ let session = NewSession {
+ id: ai_memory_core::SessionId::new(),
+ workspace_id: ws,
+ project_id: proj,
+ agent_kind: kind,
+ cwd: None,
+ };
+ begin_session(&mut conn, &session).unwrap_or_else(|e| {
+ panic!(
+ "sessions.agent_kind CHECK rejects '{}' — add it to the \
+ constraint with a new migration: {e}",
+ kind.as_str()
+ )
+ });
+ }
+ }
+
fn fresh_db() -> (
TempDir,
Connection,