remem-ai 0.6.81

Local-first coding agent memory for Claude Code and OpenAI Codex
Documentation
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use crate::memory::Memory;
use crate::workstream::{WorkStream, WorkStreamStatus};
use rusqlite::{params, Connection, OptionalExtension};

mod bundle_candidates;
mod codex_hook_stdout;
mod context_audit_persistence;
mod current_truth_activation;
mod cursor_hook;
mod diagnostics;
mod engine_convergence;
mod gate_pipeline;
mod load;
mod ownership;
mod project_alias;
mod render;
mod render_inline;
mod render_poisoning;
mod render_stability;
mod render_workstreams;
mod retrieval;
mod sessions;
mod staleness;
mod truncation;

pub(super) fn sample_memory(id: i64, memory_type: &str, title: &str) -> Memory {
    sample_memory_with_epoch(id, memory_type, title, 1_710_000_000)
}

pub(super) fn sample_memory_with_epoch(
    id: i64,
    memory_type: &str,
    title: &str,
    updated_at_epoch: i64,
) -> Memory {
    Memory {
        id,
        session_id: None,
        project: "demo/project".to_string(),
        topic_key: None,
        title: title.to_string(),
        text: "Body".to_string(),
        memory_type: memory_type.to_string(),
        files: None,
        created_at_epoch: updated_at_epoch,
        updated_at_epoch,
        status: "active".to_string(),
        branch: None,
        scope: "project".to_string(),
    }
}

pub(super) fn sample_workstream(id: i64, title: &str, next_action: Option<&str>) -> WorkStream {
    WorkStream {
        id,
        project: "demo/project".to_string(),
        title: title.to_string(),
        description: None,
        status: WorkStreamStatus::Active,
        progress: None,
        next_action: next_action.map(str::to_string),
        blockers: None,
        created_at_epoch: 0,
        updated_at_epoch: id,
        completed_at_epoch: None,
    }
}

pub(super) fn insert_memory(
    conn: &Connection,
    id: i64,
    project: &str,
    topic_key: Option<&str>,
    memory_type: &str,
    title: &str,
    content: &str,
    updated_at_epoch: i64,
) {
    insert_memory_with_branch(
        conn,
        id,
        project,
        topic_key,
        memory_type,
        title,
        content,
        updated_at_epoch,
        None,
    );
}

pub(super) fn insert_memory_with_branch(
    conn: &Connection,
    id: i64,
    project: &str,
    topic_key: Option<&str>,
    memory_type: &str,
    title: &str,
    content: &str,
    updated_at_epoch: i64,
    branch: Option<&str>,
) {
    let proof_event_id = 8_200_000 + id;
    let candidate_id = 7_200_000 + id;
    let proposed_state_key_id = 6_200_000 + id;
    let candidate_topic = topic_key.map_or_else(|| format!("context-fixture-{id}"), str::to_string);
    let has_capture_attribution: bool = conn
        .query_row(
            "SELECT EXISTS(
                 SELECT 1 FROM pragma_table_info('captured_events')
                 WHERE name = 'host_id'
             )",
            [],
            |row| row.get(0),
        )
        .unwrap();
    if has_capture_attribution {
        conn.execute_batch(
            "INSERT OR IGNORE INTO hosts (id, name, created_at_epoch)
             VALUES (9200001, 'context-fixture-host', 0);
             INSERT OR IGNORE INTO workspaces
             (id, root_path, created_at_epoch, updated_at_epoch)
             VALUES (9200001, '/context-fixture', 0, 0);
             INSERT OR IGNORE INTO projects
             (id, workspace_id, project_path, project_key, created_at_epoch, updated_at_epoch)
             VALUES (9200001, 9200001, '/context-fixture', 'context-fixture', 0, 0);
             INSERT OR IGNORE INTO sessions
             (id, host_id, workspace_id, project_id, session_id, last_seen_at_epoch, status)
             VALUES (9200001, 9200001, 9200001, 9200001,
                     'context-fixture-session', 0, 'active');",
        )
        .unwrap();
        let parents: Option<(i64, i64, i64, i64)> = conn
            .query_row(
                "SELECT host.id, workspace.id, project.id, session.id
                 FROM hosts host
                 JOIN workspaces workspace ON workspace.id = 9200001
                 JOIN projects project ON project.workspace_id = workspace.id
                 JOIN sessions session ON session.project_id = project.id
                 WHERE host.id = session.host_id
                 LIMIT 1",
                [],
                |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
            )
            .optional()
            .unwrap();
        let (host_id, workspace_id, project_id, session_row_id) =
            parents.expect("captured-event fixture parents");
        conn.execute(
            "INSERT INTO captured_events
             (id, host_id, workspace_id, project_id, session_row_id, session_id, event_id,
              event_type, content_hash, retention_class, created_at_epoch, inserted_at_epoch)
             VALUES (?1, ?2, ?3, ?4, ?5, 'context-fixture', ?6,
                     'message', ?7, 'normal', ?8, ?8)",
            params![
                proof_event_id,
                host_id,
                workspace_id,
                project_id,
                session_row_id,
                format!("context-proof-{id}"),
                format!("context-proof-hash-{id}"),
                updated_at_epoch
            ],
        )
        .unwrap();
    } else {
        conn.execute(
            "INSERT INTO captured_events (id) VALUES (?1)",
            [proof_event_id],
        )
        .unwrap();
    }
    conn.execute(
        "INSERT INTO memory_candidates
         (id, project_id, scope, memory_type, topic_key, text, evidence_event_ids,
          confidence, risk_class, review_status, created_at_epoch, updated_at_epoch)
         VALUES (?1, NULL, 'project', ?2, ?3, ?4, ?5,
                 0.9, 'low', 'accepted', 0, 0)",
        params![
            candidate_id,
            memory_type,
            candidate_topic,
            content,
            format!("[{proof_event_id}]")
        ],
    )
    .unwrap();
    conn.execute(
        "INSERT OR IGNORE INTO memory_state_keys
         (id, owner_scope, owner_key, memory_type, state_key,
          created_at_epoch, updated_at_epoch)
         VALUES (?1, 'repo', ?2, ?3, ?4, ?5, ?5)",
        params![
            proposed_state_key_id,
            project,
            memory_type,
            candidate_topic,
            updated_at_epoch
        ],
    )
    .unwrap();
    let state_key_id: i64 = conn
        .query_row(
            "SELECT id FROM memory_state_keys
             WHERE owner_scope = 'repo' AND owner_key = ?1
               AND memory_type = ?2 AND state_key = ?3",
            params![project, memory_type, candidate_topic],
            |row| row.get(0),
        )
        .unwrap();
    conn.execute(
        "INSERT INTO memories
         (id, session_id, project, topic_key, title, content, memory_type, files,
          created_at_epoch, updated_at_epoch, status, branch, scope,
          source_trust_class, source_candidate_id, state_key_id)
         VALUES (?1, NULL, ?2, ?3, ?4, ?5, ?6, NULL, ?7, ?7, 'active', ?8,
                 'project', 'local_tool_output', ?9, ?10)",
        params![
            id,
            project,
            topic_key,
            title,
            content,
            memory_type,
            updated_at_epoch,
            branch,
            candidate_id,
            state_key_id
        ],
    )
    .unwrap();
}

#[allow(clippy::too_many_arguments)]
pub(super) fn insert_owned_memory(
    conn: &Connection,
    id: i64,
    project: &str,
    topic_key: Option<&str>,
    memory_type: &str,
    title: &str,
    content: &str,
    updated_at_epoch: i64,
    owner_scope: &str,
    owner_key: &str,
    target_project: Option<&str>,
    topic_domain: Option<&str>,
) {
    insert_memory(
        conn,
        id,
        project,
        topic_key,
        memory_type,
        title,
        content,
        updated_at_epoch,
    );
    conn.execute(
        "UPDATE memories
         SET source_project = ?1, target_project = ?2, owner_scope = ?3,
             owner_key = ?4, topic_domain = ?5, context_class = 'startup_core'
         WHERE id = ?6",
        params![
            project,
            target_project,
            owner_scope,
            owner_key,
            topic_domain,
            id
        ],
    )
    .unwrap();
}

pub(super) fn insert_global_memory(
    conn: &Connection,
    id: i64,
    project: &str,
    topic_key: Option<&str>,
    memory_type: &str,
    title: &str,
    content: &str,
    updated_at_epoch: i64,
) {
    conn.execute(
        "INSERT INTO memories
         (id, session_id, project, topic_key, title, content, memory_type, files,
          created_at_epoch, updated_at_epoch, status, branch, scope)
         VALUES (?1, NULL, ?2, ?3, ?4, ?5, ?6, NULL, ?7, ?7, 'active', NULL, 'global')",
        params![
            id,
            project,
            topic_key,
            title,
            content,
            memory_type,
            updated_at_epoch
        ],
    )
    .unwrap();
}

pub(super) fn insert_session_summary(
    conn: &Connection,
    project: &str,
    request: &str,
    completed: Option<&str>,
    created_at_epoch: i64,
) {
    conn.execute(
        "INSERT INTO session_summaries
         (memory_session_id, project, request, completed, created_at_epoch)
         VALUES (?1, ?2, ?3, ?4, ?5)",
        params![
            format!("session-{created_at_epoch}"),
            project,
            request,
            completed,
            created_at_epoch
        ],
    )
    .unwrap();
}

pub(super) fn create_session_summary_schema(conn: &Connection) {
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS session_summaries (
            id INTEGER PRIMARY KEY,
            memory_session_id TEXT,
            project TEXT,
            request TEXT,
            completed TEXT,
            decisions TEXT,
            learned TEXT,
            next_steps TEXT,
            preferences TEXT,
            created_at_epoch INTEGER,
            source_project TEXT,
            target_project TEXT,
            owner_scope TEXT,
            owner_key TEXT,
            topic_domain TEXT,
            routing_confidence REAL,
            routing_reason TEXT,
            context_class TEXT,
            expires_at_epoch INTEGER,
            valid_from_epoch INTEGER,
            valid_to_epoch INTEGER,
            session_row_id INTEGER,
            covered_from_event_id INTEGER,
            covered_to_event_id INTEGER,
            poisoning_status TEXT NOT NULL DEFAULT 'safe',
            quarantine_stage TEXT,
            quarantine_field TEXT,
            quarantine_event_id INTEGER,
            quarantine_pattern_id TEXT,
            quarantine_pattern_version INTEGER,
            acknowledged_pattern_id TEXT,
            acknowledged_pattern_version INTEGER,
            acknowledged_at_epoch INTEGER,
            poisoning_block_count INTEGER NOT NULL DEFAULT 0,
            poisoning_last_blocked_at_epoch INTEGER
        );
        CREATE TABLE IF NOT EXISTS sessions (
            id INTEGER PRIMARY KEY,
            session_id TEXT
        );",
    )
    .unwrap();
}

pub(super) fn create_workstream_schema(conn: &Connection) {
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS workstreams (
            id INTEGER PRIMARY KEY,
            project TEXT NOT NULL,
            title TEXT NOT NULL,
            description TEXT,
            status TEXT NOT NULL,
            progress TEXT,
            next_action TEXT,
            blockers TEXT,
            created_at_epoch INTEGER NOT NULL,
            updated_at_epoch INTEGER NOT NULL,
            completed_at_epoch INTEGER,
            owner_scope TEXT,
            owner_key TEXT,
            target_project TEXT,
            identity_key TEXT,
            merged_into_workstream_id INTEGER
        );",
    )
    .unwrap();
}

pub(super) fn setup_context_schema(conn: &Connection) {
    crate::memory::types::tests_helper::setup_memory_schema(conn);
    create_session_summary_schema(conn);
    create_workstream_schema(conn);
    conn.execute_batch(
        "CREATE TABLE IF NOT EXISTS graph_edges (
            id INTEGER PRIMARY KEY,
            edge_type TEXT NOT NULL,
            edge_trust TEXT,
            from_node_kind TEXT,
            from_node_id INTEGER,
            to_node_kind TEXT,
            to_node_id INTEGER,
            created_at_epoch INTEGER NOT NULL,
            valid_from_epoch INTEGER,
            valid_to_epoch INTEGER
        );",
    )
    .unwrap();
}