remem-ai 0.6.78

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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
use anyhow::Result;
use rusqlite::{Connection, OptionalExtension};

use super::types::{Check, Status};
use crate::db;

mod admin_required;

use admin_required::{
    admin_required_archived_recovery_detail, ADMIN_REQUIRED_ARCHIVED_CANDIDATE_LIMIT,
};

const STALE_CAPTURE_HEARTBEAT_SECS: i64 = 7 * 24 * 60 * 60;
const SUMMARY_HEARTBEAT_GRACE_SECS: i64 = 60;

pub(super) fn check_capture_liveness(conn: Option<&Connection>, setup_checks: &[Check]) -> Check {
    let setup_findings = capture_setup_findings(setup_checks);
    let mut failures: Vec<String> = setup_findings
        .iter()
        .filter(|finding| matches!(finding.status, Status::Fail))
        .map(|finding| finding.detail.clone())
        .collect();
    let warnings: Vec<String> = setup_findings
        .iter()
        .filter(|finding| matches!(finding.status, Status::Warn))
        .map(|finding| finding.detail.clone())
        .collect();

    let Some(conn) = conn else {
        if failures.is_empty() {
            return Check::new(
                "Capture liveness",
                Status::Warn,
                join_detail(&warnings, "cannot open database"),
            );
        }
        return Check::new("Capture liveness", Status::Fail, failures.join("; "));
    };

    let stats = match db::query_system_stats(conn) {
        Ok(stats) => stats,
        Err(err) => {
            if failures.is_empty() {
                return Check::new(
                    "Capture liveness",
                    Status::Warn,
                    format!("cannot load capture stats: {}", err),
                );
            }
            failures.push(format!("cannot load capture stats: {err}"));
            return Check::new("Capture liveness", Status::Fail, failures.join("; "));
        }
    };
    let archived_transient_pending =
        match db::pending::admin::query_archived_transient_legacy_pending(conn) {
            Ok(stats) => stats,
            Err(err) => {
                if failures.is_empty() {
                    return Check::new(
                        "Capture liveness",
                        Status::Warn,
                        format!("cannot classify archived transient pending observations: {err}"),
                    );
                }
                failures.push(format!(
                    "cannot classify archived transient pending observations: {err}"
                ));
                return Check::new("Capture liveness", Status::Fail, failures.join("; "));
            }
        };
    let recoverable_archived_pending = archived_transient_pending.due;
    let deferred_archived_pending = archived_transient_pending.deferred;
    let admin_required_archived_pending =
        match db::pending::admin::count_admin_required_archived_legacy_pending(conn) {
            Ok(count) => count,
            Err(err) => {
                if failures.is_empty() {
                    return Check::new(
                        "Capture liveness",
                        Status::Warn,
                        format!("cannot count admin-required archived pending observations: {err}"),
                    );
                }
                failures.push(format!(
                    "cannot count admin-required archived pending observations: {err}"
                ));
                return Check::new("Capture liveness", Status::Fail, failures.join("; "));
            }
        };
    let admin_required_archived_candidates = if admin_required_archived_pending > 0 {
        match db::pending::admin::list_admin_required_archived_legacy_pending(
            conn,
            ADMIN_REQUIRED_ARCHIVED_CANDIDATE_LIMIT,
        ) {
            Ok(candidates) => candidates,
            Err(err) => {
                failures.push(format!(
                    "{admin_required_archived_pending} admin-required archived pending observations, but cannot list recovery candidates: {err}"
                ));
                return Check::new("Capture liveness", Status::Fail, failures.join("; "));
            }
        }
    } else {
        Vec::new()
    };

    if stats.failed_pending_observations > 0
        || recoverable_archived_pending > 0
        || deferred_archived_pending > 0
        || admin_required_archived_pending > 0
        || stats.failed_extraction_tasks > 0
    {
        let oldest_age = oldest_actionable_failure_age(&stats.failure_lifecycle)
            .map(|age| format!("; oldest actionable failure age={}s", age))
            .unwrap_or_default();
        let mut recovery: Vec<String> = Vec::new();
        if stats.failed_pending_observations > 0 || recoverable_archived_pending > 0 {
            recovery.push("a running `remem worker` auto-migrates eligible transient rows into the capture pipeline; run `remem worker --once` to drain one known-host batch now, including archived transient rows; for non-archived or unknown-host repair, run `remem pending list-failed --limit 20`, then preview/apply `remem pending retry-failed --dry-run` and `remem pending retry-failed`, followed by `remem pending migrate-legacy --dry-run --host claude-code` and `remem pending migrate-legacy --host claude-code` (or the corresponding `remem pending migrate-legacy --dry-run --host codex-cli` and `remem pending migrate-legacy --host codex-cli` commands)".to_string());
        }
        if deferred_archived_pending > 0 {
            let Some(next_retry_epoch) = archived_transient_pending.earliest_deferred_retry_epoch
            else {
                failures.push(
                    "deferred archived transient pending observations have no retry epoch"
                        .to_string(),
                );
                return Check::new("Capture liveness", Status::Fail, failures.join("; "));
            };
            recovery.push(format!(
                "automatic recovery remains deferred; earliest next_retry_epoch={next_retry_epoch}"
            ));
        }
        if admin_required_archived_pending > 0 {
            recovery.push(admin_required_archived_recovery_detail(
                &admin_required_archived_candidates,
                admin_required_archived_pending,
            ));
        }
        if stats.failed_extraction_tasks > 0 {
            recovery.push("run `remem worker --once` for failed extraction tasks".to_string());
        }
        failures.push(format!(
            "failed-observation backlog: {} actionable failed pending observations, {} recoverable archived transient pending observations, {} deferred archived transient pending observations, {} admin-required archived pending observations, {} actionable failed extraction tasks{}; {}",
            stats.failed_pending_observations,
            recoverable_archived_pending,
            deferred_archived_pending,
            admin_required_archived_pending,
            stats.failed_extraction_tasks,
            oldest_age,
            recovery.join("; ")
        ));
    }
    if stats.actionable_capture_drops > 0 {
        failures.push(format!(
            "{} actionable capture drop(s); latest reason={}",
            stats.actionable_capture_drops,
            stats
                .latest_capture_drop_reason
                .as_deref()
                .unwrap_or("unknown")
        ));
    }

    let liveness = match query_liveness_rows(conn) {
        Ok(liveness) => liveness,
        Err(err) => {
            if failures.is_empty() {
                return Check::new(
                    "Capture liveness",
                    Status::Warn,
                    format!("cannot load capture liveness rows: {}", err),
                );
            }
            failures.push(format!("cannot load capture liveness rows: {err}"));
            return Check::new("Capture liveness", Status::Fail, failures.join("; "));
        }
    };
    if let Some(gap) = liveness.hosted_summary_without_capture {
        failures.push(format!(
            "{} hosted session summary row(s) have no captured_events heartbeat; latest session_row_id={} host={}",
            gap.count,
            gap.latest_session_row_id.unwrap_or_default(),
            gap.latest_host.unwrap_or_else(|| "unknown".to_string())
        ));
    }
    if stats.session_summaries > 0 && stats.latest_capture_activity_epoch.is_none() {
        failures.push(format!(
            "{} completed session summary row(s), but no captured_events/raw_messages/capture_drop_events heartbeat; hooks are not recording capture activity",
            stats.session_summaries
        ));
    }
    if let (Some(summary_epoch), Some(heartbeat_epoch)) = (
        liveness.latest_session_summary_epoch,
        stats.latest_capture_activity_epoch,
    ) {
        if summary_epoch.saturating_sub(heartbeat_epoch) > SUMMARY_HEARTBEAT_GRACE_SECS {
            failures.push(format!(
                "completed session summary is newer than latest capture heartbeat by {}s; hooks may be missing or stale",
                summary_epoch.saturating_sub(heartbeat_epoch)
            ));
        }
    }

    if !failures.is_empty() {
        return Check::new("Capture liveness", Status::Fail, failures.join("; "));
    }
    if stats.latest_capture_activity_epoch.is_none() {
        return Check::new(
            "Capture liveness",
            Status::Warn,
            join_detail(
                &warnings,
                "no capture heartbeat yet; run one host session and re-run doctor",
            ),
        );
    }

    let heartbeat_age_secs = chrono::Utc::now()
        .timestamp()
        .saturating_sub(stats.latest_capture_activity_epoch.unwrap_or_default());
    let detail = format!(
        "latest capture heartbeat {}s ago; captured_events={}, raw_messages={}, expected drops={}",
        heartbeat_age_secs, stats.captured_events, stats.raw_messages, stats.capture_drop_events
    );
    if heartbeat_age_secs > STALE_CAPTURE_HEARTBEAT_SECS {
        return Check::new(
            "Capture liveness",
            Status::Warn,
            join_detail(
                &warnings,
                &format!(
                    "{detail}; stale capture heartbeat exceeds {}s, run one host session and re-run doctor",
                    STALE_CAPTURE_HEARTBEAT_SECS
                ),
            ),
        );
    }
    if !warnings.is_empty() {
        return Check::new(
            "Capture liveness",
            Status::Warn,
            join_detail(&warnings, &detail),
        );
    }

    Check::new("Capture liveness", Status::Ok, detail)
}

fn oldest_actionable_failure_age(stats: &db::FailureLifecycleStats) -> Option<i64> {
    let now = chrono::Utc::now().timestamp();
    [
        stats.pending_observation.oldest_actionable_epoch,
        stats.extraction_task.oldest_actionable_epoch,
        stats.extraction_replay_range.oldest_actionable_epoch,
        stats.job.oldest_actionable_epoch,
    ]
    .into_iter()
    .flatten()
    .min()
    .map(|epoch| now.saturating_sub(epoch))
}

#[derive(Clone, PartialEq, Eq)]
struct SetupFinding {
    status: Status,
    detail: String,
}

fn capture_setup_findings(checks: &[Check]) -> Vec<SetupFinding> {
    checks
        .iter()
        .filter_map(|check| {
            if check.name.starts_with("Hooks") {
                return hook_setup_finding(check);
            }
            if check.name == "Install paths" {
                return install_path_setup_finding(check);
            }
            None
        })
        .collect()
}

fn hook_setup_finding(check: &Check) -> Option<SetupFinding> {
    match check.status {
        Status::Fail => Some(SetupFinding {
            status: Status::Fail,
            detail: format!("{} failed: {}", check.name, check.detail),
        }),
        Status::Warn if hook_warning_blocks_capture(&check.detail) => Some(SetupFinding {
            status: Status::Fail,
            detail: format!("{} stale or incomplete: {}", check.name, check.detail),
        }),
        _ => None,
    }
}

fn hook_warning_blocks_capture(detail: &str) -> bool {
    detail.contains(" registered (run `remem install --target")
}

fn install_path_setup_finding(check: &Check) -> Option<SetupFinding> {
    match check.status {
        Status::Fail => Some(SetupFinding {
            status: Status::Fail,
            detail: format!("Install paths failed: {}", check.detail),
        }),
        Status::Warn => Some(SetupFinding {
            status: Status::Warn,
            detail: format!("Install paths warning: {}", check.detail),
        }),
        Status::Ok => None,
    }
}

fn join_detail(prefixes: &[String], detail: &str) -> String {
    if prefixes.is_empty() {
        detail.to_string()
    } else {
        format!("{}; {detail}", prefixes.join("; "))
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct CaptureLivenessRows {
    latest_session_summary_epoch: Option<i64>,
    hosted_summary_without_capture: Option<HostedSummaryGap>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct HostedSummaryGap {
    count: i64,
    latest_session_row_id: Option<i64>,
    latest_host: Option<String>,
}

fn query_liveness_rows(conn: &Connection) -> Result<CaptureLivenessRows> {
    let latest_session_summary_epoch = conn.query_row(
        "SELECT MAX(created_at_epoch)
         FROM session_summaries
         WHERE created_at_epoch IS NOT NULL",
        [],
        |row| row.get::<_, Option<i64>>(0),
    )?;
    let hosted_summary_without_capture = query_hosted_summary_without_capture(conn)?;
    Ok(CaptureLivenessRows {
        latest_session_summary_epoch,
        hosted_summary_without_capture,
    })
}

fn query_hosted_summary_without_capture(conn: &Connection) -> Result<Option<HostedSummaryGap>> {
    let count = conn.query_row(
        "SELECT COUNT(*)
         FROM session_summaries ss
         WHERE ss.session_row_id IS NOT NULL
           AND NOT EXISTS (
               SELECT 1 FROM captured_events ce
               WHERE ce.session_row_id = ss.session_row_id
           )",
        [],
        |row| row.get::<_, i64>(0),
    )?;
    if count == 0 {
        return Ok(None);
    }
    let latest = conn
        .query_row(
            "SELECT ss.session_row_id, h.name
             FROM session_summaries ss
             LEFT JOIN hosts h ON h.id = ss.host_id
             WHERE ss.session_row_id IS NOT NULL
               AND NOT EXISTS (
                   SELECT 1 FROM captured_events ce
                   WHERE ce.session_row_id = ss.session_row_id
               )
             ORDER BY ss.created_at_epoch DESC, ss.id DESC
             LIMIT 1",
            [],
            |row| {
                Ok((
                    row.get::<_, Option<i64>>(0)?,
                    row.get::<_, Option<String>>(1)?,
                ))
            },
        )
        .optional()?;
    Ok(Some(HostedSummaryGap {
        count,
        latest_session_row_id: latest.as_ref().and_then(|row| row.0),
        latest_host: latest.and_then(|row| row.1),
    }))
}

#[cfg(test)]
mod tests {
    use rusqlite::params;

    use super::*;

    fn setup_liveness_conn() -> anyhow::Result<Connection> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        Ok(conn)
    }

    fn record_liveness_capture(conn: &Connection) -> anyhow::Result<()> {
        crate::db::record_captured_event(
            conn,
            &crate::db::CaptureEventInput {
                host: "codex-cli",
                session_id: "sess-doctor",
                project: "/tmp/remem-doctor",
                cwd: Some("/tmp/remem-doctor"),
                event_type: "message",
                role: Some("user"),
                tool_name: None,
                content: "captured event",
                task_kind: None,
            },
        )?;
        Ok(())
    }

    #[test]
    fn capture_liveness_fails_on_failed_observation_backlog() -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        let id = crate::db::test_support::insert_legacy_pending_fixture(
            &conn,
            "codex-cli",
            "sess-failed",
            "/tmp/remem",
            "Bash",
            Some("{}"),
            Some("{}"),
            Some("/tmp/remem"),
        )?;
        conn.execute(
            "UPDATE pending_observations SET status = 'failed' WHERE id = ?1",
            [id],
        )?;

        let check = check_capture_liveness(Some(&conn), &[]);

        assert!(matches!(check.status, Status::Fail));
        assert!(check.detail.contains("failed-observation backlog"));
        assert!(check.detail.contains("failed pending observations"));
        assert!(check.detail.contains("`remem worker --once`"));
        assert!(check.detail.contains("`remem pending retry-failed`"));
        assert!(check
            .detail
            .contains("`remem pending migrate-legacy --host claude-code`"));
        assert!(check
            .detail
            .contains("`remem pending migrate-legacy --host codex-cli`"));
        Ok(())
    }

    #[test]
    fn capture_liveness_fails_on_recoverable_archived_observation_backlog() -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        let id = crate::db::test_support::insert_legacy_pending_fixture(
            &conn,
            "codex-cli",
            "sess-archived-transient",
            "/tmp/remem",
            "Bash",
            Some("{}"),
            Some("{}"),
            Some("/tmp/remem"),
        )?;
        let now = chrono::Utc::now().timestamp();
        conn.execute(
            "UPDATE pending_observations
             SET status = 'failed',
                 failure_class = 'transient',
                 failed_at_epoch = ?1,
                 archived_at_epoch = ?2
             WHERE id = ?3",
            params![now - 20 * 86_400, now - 86_400, id],
        )?;

        let check = check_capture_liveness(Some(&conn), &[]);

        assert!(matches!(check.status, Status::Fail));
        assert!(check.detail.contains("failed-observation backlog"));
        assert!(check
            .detail
            .contains("0 actionable failed pending observations"));
        assert!(check
            .detail
            .contains("1 recoverable archived transient pending observations"));
        assert!(check
            .detail
            .contains("a running `remem worker` auto-migrates"));
        assert!(check.detail.contains("run `remem worker --once`"));
        Ok(())
    }

    #[test]
    fn capture_liveness_keeps_deferred_archived_retry_visible_without_immediate_recovery(
    ) -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        let id = crate::db::test_support::insert_legacy_pending_fixture(
            &conn,
            "codex-cli",
            "sess-archived-backoff",
            "/tmp/remem",
            "Bash",
            Some("{}"),
            Some("{}"),
            Some("/tmp/remem"),
        )?;
        let now = chrono::Utc::now().timestamp();
        conn.execute(
            "UPDATE pending_observations
             SET status = 'failed',
                 failure_class = 'transient',
                 failed_at_epoch = ?1,
                 archived_at_epoch = ?2,
                 next_retry_epoch = ?3
             WHERE id = ?4",
            params![now - 20 * 86_400, now - 86_400, now + 900, id],
        )?;

        let check = check_capture_liveness(Some(&conn), &[]);

        assert!(matches!(check.status, Status::Fail));
        assert!(check.detail.contains("failed-observation backlog"));
        assert!(check
            .detail
            .contains("1 deferred archived transient pending observations"));
        assert!(check
            .detail
            .contains(&format!("earliest next_retry_epoch={}", now + 900)));
        assert!(!check.detail.contains("`remem worker --once`"));
        Ok(())
    }

    #[test]
    fn capture_liveness_fails_on_admin_required_archived_observation() -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        let unsafe_host = format!("unknown;\n{}", "h".repeat(120));
        let unsafe_failure_class = format!("permanent;\r{}", "f".repeat(120));
        let id = crate::db::test_support::insert_legacy_pending_fixture(
            &conn,
            &unsafe_host,
            "sess-archived-admin",
            "/tmp/remem",
            "Bash",
            Some("{}"),
            Some("{}"),
            Some("/tmp/remem"),
        )?;
        let now = chrono::Utc::now().timestamp();
        conn.execute(
            "UPDATE pending_observations
             SET status = 'failed',
                 failure_class = ?1,
                 failed_at_epoch = ?2,
                 archived_at_epoch = ?3,
                 updated_at_epoch = ?4
             WHERE id = ?5",
            params![
                unsafe_failure_class,
                now - 20 * 86_400,
                now - 86_400,
                now - 21 * 86_400,
                id
            ],
        )?;
        for index in 0..20 {
            let newer_id = crate::db::test_support::insert_legacy_pending_fixture(
                &conn,
                crate::runtime_config::CODEX_HOST,
                &format!("sess-newer-failed-{index}"),
                "/tmp/remem",
                "Bash",
                Some("{}"),
                Some("{}"),
                Some("/tmp/remem"),
            )?;
            conn.execute(
                "UPDATE pending_observations
                 SET status = 'failed',
                     failure_class = 'transient',
                     failed_at_epoch = ?1,
                     updated_at_epoch = ?1
                 WHERE id = ?2",
                params![now - index, newer_id],
            )?;
        }
        let mut additional_admin_ids = Vec::new();
        for index in 0..5 {
            let host = if index == 0 {
                crate::runtime_config::CODEX_HOST
            } else {
                "unknown"
            };
            let candidate_id = crate::db::test_support::insert_legacy_pending_fixture(
                &conn,
                host,
                &format!("sess-newer-admin-{index}"),
                "/tmp/remem",
                "Bash",
                Some("{}"),
                Some("{}"),
                Some("/tmp/remem"),
            )?;
            conn.execute(
                "UPDATE pending_observations
                 SET status = 'failed',
                     failure_class = 'permanent',
                     archived_at_epoch = ?1,
                     updated_at_epoch = ?1
                 WHERE id = ?2",
                params![now - 43_200 + index, candidate_id],
            )?;
            additional_admin_ids.push(candidate_id);
        }

        let global_recent = crate::db::pending::admin::list_failed(&conn, None, 20)?;
        assert_eq!(global_recent.len(), 20);
        assert!(
            global_recent.iter().all(|row| row.id != id),
            "the legacy global list must reproduce the hidden-candidate regression"
        );

        let check = check_capture_liveness(Some(&conn), &[]);

        assert!(matches!(check.status, Status::Fail));
        assert!(check
            .detail
            .contains("20 actionable failed pending observations"));
        assert!(check
            .detail
            .contains("6 admin-required archived pending observations"));
        assert!(check
            .detail
            .contains("admin-required archived candidates (showing 5 of 6, oldest first)"));
        assert!(check
            .detail
            .contains(&format!("candidate id={id} host=\"unknown;\\n")));
        assert!(check.detail.contains("failure_class=\"permanent;\\r"));
        assert!(check
            .detail
            .contains(&format!("archived_at_epoch={}", now - 86_400)));
        assert!(!check.detail.contains('\n'));
        assert!(!check.detail.contains(&unsafe_host));
        assert!(check
            .detail
            .contains("unknown host requires explicit `--host`"));
        assert!(check.detail.contains(&format!(
            "`remem pending recover-archived --id {id} --host claude-code --dry-run`"
        )));
        assert!(check.detail.contains(&format!(
            "`remem pending recover-archived --id {id} --host claude-code`"
        )));
        assert!(check.detail.contains(&format!(
            "`remem pending recover-archived --id {id} --host codex-cli --dry-run`"
        )));
        assert!(check.detail.contains(&format!(
            "`remem pending recover-archived --id {id} --host codex-cli`"
        )));
        let known_host_id = additional_admin_ids[0];
        assert!(check.detail.contains(&format!(
            "candidate id={known_host_id} host=\"codex-cli\" failure_class=\"permanent\""
        )));
        assert!(check.detail.contains(&format!(
            "`remem pending recover-archived --id {known_host_id} --dry-run`"
        )));
        assert!(check.detail.contains(&format!(
            "`remem pending recover-archived --id {known_host_id}`"
        )));
        assert!(!check
            .detail
            .contains(&format!("candidate id={}", additional_admin_ids[4])));
        Ok(())
    }

    #[test]
    fn capture_liveness_fails_when_summary_is_newer_than_heartbeat() -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        record_liveness_capture(&conn)?;
        conn.execute(
            "INSERT INTO session_summaries
             (memory_session_id, project, request, completed, created_at_epoch)
             VALUES ('legacy-newer-summary', '/tmp/remem-doctor', 'done', 'done', strftime('%s', 'now') + 120)",
            [],
        )?;

        let check = check_capture_liveness(Some(&conn), &[]);

        assert!(matches!(check.status, Status::Fail));
        assert!(check
            .detail
            .contains("summary is newer than latest capture heartbeat"));
        Ok(())
    }

    #[test]
    fn capture_liveness_fails_when_hosted_summary_has_no_capture() -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        conn.execute(
            "INSERT OR IGNORE INTO hosts(name, enabled, created_at_epoch)
             VALUES ('codex-cli', 1, strftime('%s', 'now'))",
            [],
        )?;
        let host_id: i64 =
            conn.query_row("SELECT id FROM hosts WHERE name = 'codex-cli'", [], |row| {
                row.get(0)
            })?;
        conn.execute(
            "INSERT INTO session_summaries
             (memory_session_id, project, request, completed, created_at_epoch, host_id, session_row_id)
             VALUES ('hosted-without-capture', '/tmp/remem-doctor', 'done', 'done',
                     strftime('%s', 'now'), ?1, 9876)",
            params![host_id],
        )?;

        let check = check_capture_liveness(Some(&conn), &[]);

        assert!(matches!(check.status, Status::Fail));
        assert!(check.detail.contains("hosted session summary"));
        assert!(check.detail.contains("session_row_id=9876"));
        Ok(())
    }

    #[test]
    fn capture_liveness_fails_on_missing_hook_setup() {
        let setup = vec![Check::new(
            "Hooks (codex)",
            Status::Fail,
            "no remem hooks (run `remem install --target codex`)",
        )];

        let check = check_capture_liveness(None, &setup);

        assert!(matches!(check.status, Status::Fail));
        assert!(check.detail.contains("Hooks (codex) failed"));
    }

    #[test]
    fn capture_liveness_fails_on_partial_hook_setup() {
        let setup = vec![Check::new(
            "Hooks (codex)",
            Status::Warn,
            "1/2 registered (run `remem install --target codex` to fix)",
        )];

        let check = check_capture_liveness(None, &setup);

        assert!(matches!(check.status, Status::Fail));
        assert!(check.detail.contains("stale or incomplete"));
    }

    #[test]
    fn capture_liveness_warns_on_stale_install_path_setup() -> anyhow::Result<()> {
        let conn = setup_liveness_conn()?;
        let setup = vec![Check::new(
            "Install paths",
            Status::Warn,
            "2 remem executable(s) found; configured /opt/remem; candidates: /usr/local/bin/remem (0.5.1); fix: remove or upgrade stale installs",
        )];

        let check = check_capture_liveness(Some(&conn), &setup);

        assert!(matches!(check.status, Status::Warn));
        assert!(check.detail.contains("Install paths warning"));
        assert!(check.detail.contains("no capture heartbeat yet"));
        Ok(())
    }
}