remem-ai 0.6.4

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
use anyhow::Result;
use tokio::time::Duration;

use crate::db;
use crate::memory_candidate::MemoryCandidateResult;

tokio::task_local! {
    static EXACT_REPLAY_TASK: ();
}

const DEPENDENCY_WAIT_RETRY_SECS: i64 = 300;

#[derive(Debug, PartialEq, Eq)]
enum ExtractionTaskOutcome {
    Deferred(String),
    // to_event_id is the highest event id actually covered by processing;
    // None means the full claim-time watermark range was covered.
    Done { to_event_id: Option<i64> },
    Waiting(String),
}

pub(crate) async fn run_next(
    lease_owner: &str,
    lease_secs: i64,
    timeout_secs: u64,
) -> Result<bool> {
    let mut conn = db::open_db()?;
    let Some(task) = db::claim_next_extraction_task(&mut conn, lease_owner, lease_secs)? else {
        return Ok(false);
    };

    crate::log::info(
        "worker",
        &format!(
            "claimed extraction id={} kind={} project={} attempt={}/{}",
            task.id,
            task.task_kind.as_str(),
            task.project,
            task.attempts + 1,
            db::EXTRACTION_TASK_MAX_ATTEMPTS
        ),
    );

    let timed = tokio::time::timeout(
        Duration::from_secs(timeout_secs),
        process_extraction_task(&task),
    )
    .await;
    let conn = db::open_db()?;
    match timed {
        Ok(Ok(ExtractionTaskOutcome::Done { to_event_id })) => {
            db::mark_extraction_task_done(
                &conn,
                task.id,
                lease_owner,
                to_event_id.or(task.high_watermark_event_id),
            )?;
            crate::log::info("worker", &format!("done extraction id={}", task.id));
        }
        Ok(Ok(ExtractionTaskOutcome::Deferred(msg))) => {
            let backoff = retry_backoff_secs(task.attempts);
            db::defer_claimed_extraction_task(&conn, &task, lease_owner, &msg, backoff)?;
            crate::log::warn(
                "worker",
                &format!(
                    "extraction id={} deferred: {} (retry in {}s)",
                    task.id,
                    crate::db::truncate_str(&msg, 300),
                    backoff
                ),
            );
        }
        Ok(Ok(ExtractionTaskOutcome::Waiting(msg))) => {
            db::wait_extraction_task(
                &conn,
                task.id,
                lease_owner,
                &msg,
                DEPENDENCY_WAIT_RETRY_SECS,
            )?;
            crate::log::warn(
                "worker",
                &format!(
                    "extraction id={} waiting: {} (recheck in {}s)",
                    task.id,
                    crate::db::truncate_str(&msg, 300),
                    DEPENDENCY_WAIT_RETRY_SECS
                ),
            );
        }
        Ok(Err(e)) => {
            let msg = e.to_string();
            let backoff = retry_backoff_secs(task.attempts);
            db::mark_claimed_extraction_task_failed_or_retry(
                &conn,
                &task,
                lease_owner,
                &msg,
                backoff,
            )?;
            crate::log::warn(
                "worker",
                &format!(
                    "extraction id={} failed: {} (retry in {}s)",
                    task.id,
                    crate::db::truncate_str(&msg, 300),
                    backoff
                ),
            );
        }
        Err(_) => {
            let msg = format!("extraction task timed out after {}s", timeout_secs);
            let backoff = retry_backoff_secs(task.attempts);
            db::mark_claimed_extraction_task_failed_or_retry(
                &conn,
                &task,
                lease_owner,
                &msg,
                backoff,
            )?;
            crate::log::warn(
                "worker",
                &format!("extraction id={} timeout (retry in {}s)", task.id, backoff),
            );
        }
    }

    Ok(true)
}

pub(crate) async fn run_claimed_exact(
    mut task: db::ExtractionTask,
    profile: &crate::runtime_config::ResolvedMemoryAiProfile,
    lease_owner: &str,
    timeout_secs: u64,
) -> Result<()> {
    task.ai_profile = Some(profile.profile_name.clone());
    crate::log::info(
        "worker",
        &format!(
            "claimed exact extraction id={} range_id={} kind={} project={} profile={}",
            task.id,
            task.replay_range_id
                .map(|id| id.to_string())
                .unwrap_or_else(|| "<none>".to_string()),
            task.task_kind.as_str(),
            task.project,
            profile.profile_name
        ),
    );

    let process = crate::ai::with_resolved_profile(
        profile.clone(),
        EXACT_REPLAY_TASK.scope((), process_extraction_task(&task)),
    );
    let timed = tokio::time::timeout(Duration::from_secs(timeout_secs), process).await;
    match timed {
        Ok(Ok(ExtractionTaskOutcome::Done { to_event_id })) => {
            let completed = to_event_id.or(task.high_watermark_event_id);
            if task
                .high_watermark_event_id
                .is_some_and(|high_watermark| completed != Some(high_watermark))
            {
                return archive_exact_outcome(
                    &task,
                    lease_owner,
                    "exact replay processed only part of the bounded event range",
                );
            }
            let conn = db::open_db()?;
            db::mark_extraction_task_done(&conn, task.id, lease_owner, completed)?;
            crate::log::info(
                "worker",
                &format!(
                    "done exact extraction id={} profile={}",
                    task.id, profile.profile_name
                ),
            );
            Ok(())
        }
        Ok(Ok(ExtractionTaskOutcome::Deferred(reason))) => archive_exact_outcome(
            &task,
            lease_owner,
            &format!("exact replay deferred: {reason}"),
        ),
        Ok(Ok(ExtractionTaskOutcome::Waiting(reason))) => archive_exact_outcome(
            &task,
            lease_owner,
            &format!("exact replay waiting: {reason}"),
        ),
        Ok(Err(error)) => {
            archive_exact_outcome(&task, lease_owner, &format!("exact replay failed: {error}"))
        }
        Err(_) => archive_exact_outcome(
            &task,
            lease_owner,
            &format!("exact replay timed out after {timeout_secs}s"),
        ),
    }
}

pub(crate) fn exact_replay_task_active() -> bool {
    EXACT_REPLAY_TASK.try_with(|()| ()).is_ok()
}

fn archive_exact_outcome(task: &db::ExtractionTask, lease_owner: &str, error: &str) -> Result<()> {
    let conn = db::open_db()?;
    db::archive_claimed_exact_replay_task(&conn, task.id, lease_owner, error)?;
    crate::log::error(
        "worker",
        &format!(
            "exact extraction archived id={} range_id={} error={}",
            task.id,
            task.replay_range_id
                .map(|id| id.to_string())
                .unwrap_or_else(|| "<none>".to_string()),
            crate::db::truncate_str(error, 300)
        ),
    );
    anyhow::bail!("{error}")
}

async fn process_extraction_task(task: &db::ExtractionTask) -> Result<ExtractionTaskOutcome> {
    match task.task_kind {
        db::ExtractionTaskKind::CapturedGitLink => {
            let mut conn = db::open_db()?;
            crate::captured_git::link_task_range(&mut conn, task)?;
            Ok(ExtractionTaskOutcome::Done { to_event_id: None })
        }
        db::ExtractionTaskKind::SessionRollup => {
            crate::session_rollup::process(task).await?;
            Ok(ExtractionTaskOutcome::Done { to_event_id: None })
        }
        db::ExtractionTaskKind::ObservationExtract => {
            crate::observation_extract::process(task).await?;
            Ok(ExtractionTaskOutcome::Done { to_event_id: None })
        }
        db::ExtractionTaskKind::MemoryCandidate => {
            let result = crate::memory_candidate::process(task).await?;
            Ok(memory_candidate_task_outcome(result))
        }
        db::ExtractionTaskKind::UserContextCandidate => {
            let result = crate::user_context::extraction::process(task).await?;
            Ok(user_context_candidate_task_outcome(result))
        }
        db::ExtractionTaskKind::GraphCandidate => {
            let result = crate::graph_candidate::process_graph_candidate_task(task).await?;
            Ok(graph_candidate_task_outcome(result))
        }
        _ => Ok(ExtractionTaskOutcome::Deferred(format!(
            "extraction task kind '{}' is not implemented",
            task.task_kind.as_str()
        ))),
    }
}

fn memory_candidate_task_outcome(result: MemoryCandidateResult) -> ExtractionTaskOutcome {
    match result {
        MemoryCandidateResult::Deferred { reason } => {
            crate::log::warn(
                "worker",
                &format!(
                    "memory candidate extraction deferred by model: {}",
                    crate::db::truncate_str(&reason, 300)
                ),
            );
            ExtractionTaskOutcome::Deferred(reason)
        }
        MemoryCandidateResult::Written { to_event_id, .. } => ExtractionTaskOutcome::Done {
            to_event_id: Some(to_event_id),
        },
        _ => ExtractionTaskOutcome::Done { to_event_id: None },
    }
}

fn graph_candidate_task_outcome(
    result: crate::graph_candidate::GraphCandidateResult,
) -> ExtractionTaskOutcome {
    match result {
        crate::graph_candidate::GraphCandidateResult::Deferred { reason } => {
            crate::log::warn(
                "worker",
                &format!(
                    "graph candidate extraction deferred by model: {}",
                    crate::db::truncate_str(&reason, 300)
                ),
            );
            ExtractionTaskOutcome::Deferred(reason)
        }
        crate::graph_candidate::GraphCandidateResult::Waiting { reason } => {
            crate::log::warn(
                "worker",
                &format!(
                    "graph candidate extraction waiting for dependency: {}",
                    crate::db::truncate_str(&reason, 300)
                ),
            );
            ExtractionTaskOutcome::Waiting(reason)
        }
        _ => ExtractionTaskOutcome::Done { to_event_id: None },
    }
}

fn user_context_candidate_task_outcome(
    result: crate::user_context::extraction::UserContextCandidateExtractResult,
) -> ExtractionTaskOutcome {
    match result {
        crate::user_context::extraction::UserContextCandidateExtractResult::EmptyRange => {
            ExtractionTaskOutcome::Done { to_event_id: None }
        }
        crate::user_context::extraction::UserContextCandidateExtractResult::NoCandidates {
            to_event_id,
        } => ExtractionTaskOutcome::Done {
            to_event_id: Some(to_event_id),
        },
        crate::user_context::extraction::UserContextCandidateExtractResult::Written {
            to_event_id,
            ..
        } => ExtractionTaskOutcome::Done {
            to_event_id: Some(to_event_id),
        },
    }
}

fn retry_backoff_secs(attempt: i64) -> i64 {
    match attempt {
        0 => 5,
        1 => 15,
        2 => 45,
        3 => 120,
        4 => 300,
        _ => 900,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn done_outcome_carries_actual_covered_event_id_for_cursor_advance() {
        let outcome = memory_candidate_task_outcome(MemoryCandidateResult::Written {
            candidates: 1,
            promoted: 0,
            pending_review: 0,
            to_event_id: 3,
        });

        assert_eq!(
            outcome,
            ExtractionTaskOutcome::Done {
                to_event_id: Some(3)
            },
            "done must report the event id actually covered by processing, not the claim-time watermark snapshot"
        );
    }

    #[test]
    fn memory_candidate_defer_preserves_range_for_reprocessing() {
        let outcome = memory_candidate_task_outcome(MemoryCandidateResult::Deferred {
            reason: "ambiguous conflict".to_string(),
        });

        assert_eq!(
            outcome,
            ExtractionTaskOutcome::Deferred("ambiguous conflict".to_string())
        );
    }

    #[test]
    fn graph_candidate_defer_preserves_range_for_reprocessing() {
        let outcome =
            graph_candidate_task_outcome(crate::graph_candidate::GraphCandidateResult::Deferred {
                reason: "ambiguous graph conflict".to_string(),
            });

        assert_eq!(
            outcome,
            ExtractionTaskOutcome::Deferred("ambiguous graph conflict".to_string())
        );
    }

    #[test]
    fn graph_candidate_waiting_preserves_dependency_reason() {
        let outcome =
            graph_candidate_task_outcome(crate::graph_candidate::GraphCandidateResult::Waiting {
                reason: "memory review pending".to_string(),
            });

        assert_eq!(
            outcome,
            ExtractionTaskOutcome::Waiting("memory review pending".to_string())
        );
    }

    #[test]
    fn user_context_candidate_done_carries_covered_event_id() {
        let outcome = user_context_candidate_task_outcome(
            crate::user_context::extraction::UserContextCandidateExtractResult::Written {
                candidates: 1,
                promoted: 1,
                pending_review: 0,
                to_event_id: 42,
            },
        );

        assert_eq!(
            outcome,
            ExtractionTaskOutcome::Done {
                to_event_id: Some(42)
            }
        );
    }
}