remem-ai 0.6.93

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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::time::Instant;

use anyhow::{bail, Context, Result};
use rusqlite::Connection;
use serde::Serialize;

use super::golden::{self, CategoryEvaluation, GoldenDataset, MetricAverages};

pub const DEFAULT_DATASET_PATH: &str = "eval/golden.json";
pub const DEFAULT_REPORT_PATH: &str = "eval/graph-decision/report.json";
const BENEFIT_THRESHOLD: f64 = 0.05;
const LATENCY_BUDGET_P95_MS: f64 = 1000.0;
const EPSILON: f64 = 0.000_001;

#[derive(Debug, Clone)]
pub struct GraphDecisionEvalOptions {
    pub dataset_path: String,
    pub k: usize,
}

impl Default for GraphDecisionEvalOptions {
    fn default() -> Self {
        Self {
            dataset_path: DEFAULT_DATASET_PATH.to_string(),
            k: 5,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct GraphDecisionReport {
    pub version: String,
    pub dataset_path: String,
    pub evidence_fingerprint: evidence_fingerprint::GraphEvidenceFingerprint,
    pub embedding_profile: GraphDecisionEmbeddingProfile,
    pub k: usize,
    pub benefit_threshold: f64,
    pub latency_budget_p95_ms: f64,
    pub evaluated_channel: EvaluatedGraphChannel,
    pub graph_edges_evaluated: bool,
    pub graph_edges_retrieval_decision: GraphEdgesRetrievalDecision,
    pub decision: GraphDecision,
    pub decision_reason: String,
    pub standard: GraphDecisionArmReport,
    pub entity_bfs: GraphDecisionArmReport,
    pub literal_graph: GraphDecisionArmReport,
    pub deltas: GraphDecisionDeltas,
    pub checks: GraphDecisionChecks,
    pub notes: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct GraphDecisionEmbeddingProfile {
    pub configured_provider: String,
    pub active_provider: String,
    pub fallback_provider: Option<String>,
    pub model_id: String,
    pub dimensions: usize,
    pub degraded: bool,
    pub disabled: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GraphDecision {
    WireLiteralGraphTraversal,
    KeepGraphEdgesFrozenPendingLiteralEval,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EvaluatedGraphChannel {
    LiteralGraphEdges,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GraphEdgesRetrievalDecision {
    WireProductionChannel,
    RemainFrozenPendingLiteralEval,
}

#[derive(Debug, Clone, Serialize)]
pub struct GraphDecisionArmReport {
    pub mode: GraphDecisionMode,
    pub overall: CategoryEvaluation,
    pub associative_slice: CategoryEvaluation,
    pub non_associative_slices: CategoryEvaluation,
    pub non_associative_by_slice: BTreeMap<String, CategoryEvaluation>,
    pub associative_queries_with_two_or_more_hops: usize,
    pub scope_leak_count: usize,
    pub query_summaries: Vec<GraphDecisionQuerySummary>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum GraphDecisionMode {
    Standard,
    EntityBfs,
    LiteralGraph,
}

#[derive(Debug, Clone, Serialize)]
pub struct GraphDecisionQuerySummary {
    pub id: String,
    pub slice: String,
    pub status: String,
    pub result_count: usize,
    pub retrieved_ids: Vec<i64>,
    pub matched_refs: usize,
    pub expected_refs: usize,
    pub retrieval_latency_ms: f64,
    pub hops: Option<u8>,
    pub entities_discovered: Vec<String>,
    pub graph_result_count: usize,
}

#[derive(Debug, Clone, Serialize)]
pub struct GraphDecisionDeltas {
    pub associative_recall_at_k: f64,
    pub associative_evidence_recall_at_k: f64,
    pub associative_ndcg_at_10: f64,
    pub non_associative_recall_at_k: f64,
    pub non_associative_evidence_recall_at_k: f64,
    pub non_associative_ndcg_at_10: f64,
    pub p95_latency_ms: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct GraphDecisionChecks {
    pub associative_slice_present: bool,
    pub literal_two_hop_observed: bool,
    pub benefit_threshold_met: bool,
    pub non_associative_zero_regression: bool,
    pub zero_scope_leak: bool,
    pub p95_latency_within_budget: bool,
    pub safe_to_wire_literal_graph: bool,
    pub all_checks_passed: bool,
}

pub fn run_graph_decision_eval(options: GraphDecisionEvalOptions) -> Result<GraphDecisionReport> {
    let dataset = golden::load_dataset(&options.dataset_path)?;
    run_graph_decision_dataset(dataset, options.dataset_path, options.k)
}

fn run_graph_decision_dataset(
    dataset: GoldenDataset,
    dataset_path: String,
    requested_k: usize,
) -> Result<GraphDecisionReport> {
    let _env_guard = crate::runtime_config::ENV_LOCK
        .lock()
        .map_err(|_| anyhow::anyhow!("graph decision embedding environment lock poisoned"))?;
    let base_config = crate::retrieval::embedding::resolve_embedding_config()?;
    let forced_config = super::provider_comparison::forced_provider_config(
        &base_config,
        crate::retrieval::embedding::EmbeddingProvider::FeatureHash,
    )?;
    let _scoped_config = super::provider_comparison::ScopedEmbeddingConfig::activate(
        &forced_config,
        crate::retrieval::embedding::EmbeddingProvider::FeatureHash,
        false,
    )?;
    let embedding_profile = fixed_graph_embedding_profile()?;
    run_graph_decision_dataset_with_profile(dataset, dataset_path, requested_k, embedding_profile)
}

fn run_graph_decision_dataset_with_profile(
    dataset: GoldenDataset,
    dataset_path: String,
    requested_k: usize,
    embedding_profile: GraphDecisionEmbeddingProfile,
) -> Result<GraphDecisionReport> {
    if !dataset.has_fixture_corpus() {
        bail!("graph decision eval requires a fixture-backed golden dataset");
    }

    let k = requested_k.max(1);
    let standard = evaluate_arm(&dataset, k, GraphDecisionMode::Standard)?;
    let entity_bfs = evaluate_arm(&dataset, k, GraphDecisionMode::EntityBfs)?;
    let literal_graph = evaluate_arm(&dataset, k, GraphDecisionMode::LiteralGraph)?;
    ensure_required_slices(&standard, &literal_graph)?;
    let deltas = build_deltas(&standard, &literal_graph);
    let checks = build_checks(&standard, &literal_graph, &deltas);
    let decision = if checks.safe_to_wire_literal_graph {
        GraphDecision::WireLiteralGraphTraversal
    } else {
        GraphDecision::KeepGraphEdgesFrozenPendingLiteralEval
    };
    let decision_reason = match decision {
        GraphDecision::WireLiteralGraphTraversal => format!(
            "Literal graph_edges traversal is safe to wire: it improved associative evidence recall by at least {:.0}%, preserved non-associative quality, produced no scope leak, stayed within the p95 latency budget, and exercised a real two-edge path.",
            BENEFIT_THRESHOLD * 100.0
        ),
        GraphDecision::KeepGraphEdgesFrozenPendingLiteralEval => format!(
            "Literal graph_edges traversal did not satisfy all wire requirements: >= {:.0}% associative evidence-recall gain, non-associative zero regression, zero scope leak, p95 latency <= {:.0}ms, and an observed two-edge expansion. Keep graph_edges retrieval frozen.",
            BENEFIT_THRESHOLD * 100.0,
            LATENCY_BUDGET_P95_MS
        ),
    };

    Ok(GraphDecisionReport {
        version: "2026-07-29".to_string(),
        evidence_fingerprint: evidence_fingerprint::compute(&dataset_path)?,
        dataset_path,
        embedding_profile,
        k,
        benefit_threshold: BENEFIT_THRESHOLD,
        latency_budget_p95_ms: LATENCY_BUDGET_P95_MS,
        evaluated_channel: EvaluatedGraphChannel::LiteralGraphEdges,
        graph_edges_evaluated: true,
        graph_edges_retrieval_decision: if checks.safe_to_wire_literal_graph {
            GraphEdgesRetrievalDecision::WireProductionChannel
        } else {
            GraphEdgesRetrievalDecision::RemainFrozenPendingLiteralEval
        },
        decision,
        decision_reason,
        standard,
        entity_bfs,
        literal_graph,
        deltas,
        checks,
        notes: vec![
            "All graph-decision arms force the same feature-hash embedding profile, independent of ambient Auto/local installation state.".to_string(),
            "The standard and literal arms use the same golden dataset and search implementation; the standard arm sets graph weight to zero.".to_string(),
            "Associative hop_path metadata seeds trusted mentions/touches_file edges through the typed provenance contract before literal-arm queries run.".to_string(),
            "Entity BFS remains informational and does not decide whether literal graph_edges traversal is wired.".to_string(),
        ],
    })
}

fn fixed_graph_embedding_profile() -> Result<GraphDecisionEmbeddingProfile> {
    let status = crate::retrieval::embedding::embedding_provider_status_without_probe()?;
    let model_id = status
        .active_model_id
        .clone()
        .context("fixed graph-decision embedding profile has no model id")?;
    let dimensions = status
        .active_dimensions
        .context("fixed graph-decision embedding profile has no dimensions")?;
    let expected_model = crate::retrieval::embedding::FEATURE_HASH_EMBEDDING_MODEL;
    let expected_dimensions = crate::retrieval::embedding::FEATURE_HASH_EMBEDDING_DIMENSIONS;
    if status.configured_provider != "feature-hash"
        || status.active_provider != "feature-hash"
        || status.fallback_provider.is_some()
        || model_id != expected_model
        || dimensions != expected_dimensions
        || status.degraded
        || status.disabled
        || status.unavailable_reason.is_some()
    {
        bail!(
            "graph decision eval requires an exact feature-hash profile: configured={} active={} fallback={:?} model={} dimensions={} degraded={} disabled={} unavailable={:?}",
            status.configured_provider,
            status.active_provider,
            status.fallback_provider,
            model_id,
            dimensions,
            status.degraded,
            status.disabled,
            status.unavailable_reason
        );
    }
    Ok(GraphDecisionEmbeddingProfile {
        configured_provider: status.configured_provider,
        active_provider: status.active_provider,
        fallback_provider: status.fallback_provider,
        model_id,
        dimensions,
        degraded: status.degraded,
        disabled: status.disabled,
    })
}

pub fn ensure_graph_decision_gate(report: &GraphDecisionReport) -> Result<()> {
    if report.checks.all_checks_passed {
        return Ok(());
    }
    bail!(
        "graph decision eval failed: associative_slice_present={} non_associative_zero_regression={} zero_scope_leak={} p95_latency_within_budget={}",
        report.checks.associative_slice_present,
        report.checks.non_associative_zero_regression,
        report.checks.zero_scope_leak,
        report.checks.p95_latency_within_budget
    )
}

fn evaluate_arm(
    dataset: &GoldenDataset,
    k: usize,
    mode: GraphDecisionMode,
) -> Result<GraphDecisionArmReport> {
    let conn = Connection::open_in_memory().context("open in-memory graph decision eval DB")?;
    crate::migrate::run_migrations(&conn).context("migrate graph decision eval DB")?;
    golden::run::seed_fixture_corpus(&conn, &dataset.corpus)?;
    if mode == GraphDecisionMode::LiteralGraph {
        seed_fixture_graph_edges(&conn, dataset)?;
    }

    let mut overall = golden::run::CategoryAccumulator::default();
    let mut associative_slice = golden::run::CategoryAccumulator::default();
    let mut non_associative_slices = golden::run::CategoryAccumulator::default();
    let mut non_associative_by_slice = BTreeMap::<String, golden::run::CategoryAccumulator>::new();
    let mut query_summaries = Vec::with_capacity(dataset.queries.len());
    let mut scope_leak_count = 0;

    for query in &dataset.queries {
        let started = Instant::now();
        let (results, hops, entities_discovered, graph_result_count) = match mode {
            GraphDecisionMode::Standard => (
                crate::retrieval::search::search_with_branch_weights(
                    &conn,
                    Some(&query.query),
                    query.project.as_deref(),
                    query.memory_type.as_deref(),
                    k.max(10) as i64,
                    0,
                    false,
                    query.branch.as_deref(),
                    crate::retrieval::search::SearchWeights {
                        graph: 0.0,
                        ..crate::retrieval::search::SearchWeights::default()
                    },
                )?,
                None,
                Vec::new(),
                0,
            ),
            GraphDecisionMode::EntityBfs => {
                let multi_hop = crate::retrieval::search_multihop::search_multi_hop(
                    &conn,
                    &query.query,
                    query.project.as_deref(),
                    k.max(10) as i64,
                    0,
                    query.memory_type.as_deref(),
                    query.branch.as_deref(),
                    false,
                    false,
                )?;
                (
                    multi_hop.memories,
                    Some(multi_hop.hops),
                    multi_hop.entities_discovered,
                    0,
                )
            }
            GraphDecisionMode::LiteralGraph => {
                let (results, explain) = crate::retrieval::search::search_with_branch_explain(
                    &conn,
                    Some(&query.query),
                    query.project.as_deref(),
                    query.memory_type.as_deref(),
                    k.max(10) as i64,
                    0,
                    false,
                    query.branch.as_deref(),
                )?;
                let (hops, graph_result_count) = literal_path_summary(
                    &conn,
                    query,
                    &results,
                    explain
                        .as_ref()
                        .context("literal graph search missing explain")?,
                )?;
                (results, hops, Vec::new(), graph_result_count)
            }
        };
        let retrieval_latency_ms = started.elapsed().as_secs_f64() * 1000.0;
        let query_tokens = golden::run::estimate_query_tokens(&query.query);
        let evaluation =
            golden::run::evaluate_query(query, &results, k, query_tokens, retrieval_latency_ms);

        golden::run::record_bucket(&mut overall, query, &evaluation);
        if query.slice_label() == "associative" {
            golden::run::record_bucket(&mut associative_slice, query, &evaluation);
        } else {
            golden::run::record_bucket(&mut non_associative_slices, query, &evaluation);
            golden::run::record_bucket(
                non_associative_by_slice
                    .entry(query.slice_label().to_string())
                    .or_default(),
                query,
                &evaluation,
            );
        }
        scope_leak_count += results
            .iter()
            .filter(|memory| memory.scope != "global")
            .filter(|memory| {
                query.project.as_deref().is_some_and(|project| {
                    !crate::project_id::project_matches(Some(&memory.project), project)
                })
            })
            .count();
        query_summaries.push(GraphDecisionQuerySummary {
            id: evaluation.id.clone(),
            slice: evaluation.slice.clone(),
            status: evaluation.status.label().to_string(),
            result_count: evaluation.result_count,
            retrieved_ids: evaluation.retrieved_ids.clone(),
            matched_refs: evaluation.matched_refs,
            expected_refs: evaluation.expected_refs,
            retrieval_latency_ms,
            hops,
            entities_discovered,
            graph_result_count,
        });
    }

    let associative_queries_with_two_or_more_hops = query_summaries
        .iter()
        .filter(|summary| {
            summary.slice == "associative" && summary.hops.is_some_and(|hops| hops >= 2)
        })
        .count();

    Ok(GraphDecisionArmReport {
        mode,
        overall: golden::run::bucket_evaluation(overall),
        associative_slice: golden::run::bucket_evaluation(associative_slice),
        non_associative_slices: golden::run::bucket_evaluation(non_associative_slices),
        non_associative_by_slice: non_associative_by_slice
            .into_iter()
            .map(|(name, bucket)| (name, golden::run::bucket_evaluation(bucket)))
            .collect(),
        associative_queries_with_two_or_more_hops,
        scope_leak_count,
        query_summaries,
    })
}

fn literal_path_summary(
    conn: &Connection,
    query: &golden::GoldenQuery,
    results: &[crate::memory::Memory],
    explain: &crate::retrieval::search::SearchExplain,
) -> Result<(Option<u8>, usize)> {
    let seed_ids = explain
        .channels
        .iter()
        .filter(|channel| channel.name == "fts" || channel.name == "vector")
        .flat_map(|channel| channel.hits.iter().map(|hit| hit.memory_id))
        .take(32)
        .collect::<Vec<_>>();
    let outcome = crate::retrieval::graph::traverse_trusted_graph(
        conn,
        crate::retrieval::graph::GraphTraversalRequest {
            seed_memory_ids: &seed_ids,
            project: query.project.as_deref(),
            memory_type: query.memory_type.as_deref(),
            branch: query.branch.as_deref(),
            include_inactive: false,
            reference_time_epoch: chrono::Utc::now().timestamp(),
            limits: crate::retrieval::graph::GraphTraversalLimits::default(),
        },
    )?;
    let result_ids = results
        .iter()
        .map(|memory| memory.id)
        .collect::<BTreeSet<_>>();
    let graph_hits = outcome
        .hits
        .iter()
        .filter(|hit| result_ids.contains(&hit.memory_id))
        .collect::<Vec<_>>();
    Ok((
        graph_hits.iter().map(|hit| hit.hop_count).max(),
        graph_hits.len(),
    ))
}

fn ensure_required_slices(
    standard: &GraphDecisionArmReport,
    literal_graph: &GraphDecisionArmReport,
) -> Result<()> {
    if standard.associative_slice.scored_queries == 0
        || literal_graph.associative_slice.scored_queries == 0
    {
        bail!("graph decision eval requires scored associative queries in both arms");
    }
    Ok(())
}

fn build_deltas(
    standard: &GraphDecisionArmReport,
    literal_graph: &GraphDecisionArmReport,
) -> GraphDecisionDeltas {
    GraphDecisionDeltas {
        associative_recall_at_k: metric_delta(
            standard.associative_slice.metrics.as_ref(),
            literal_graph.associative_slice.metrics.as_ref(),
            |m| m.recall_at_k,
        ),
        associative_evidence_recall_at_k: metric_delta(
            standard.associative_slice.metrics.as_ref(),
            literal_graph.associative_slice.metrics.as_ref(),
            |m| m.evidence_recall_at_k,
        ),
        associative_ndcg_at_10: metric_delta(
            standard.associative_slice.metrics.as_ref(),
            literal_graph.associative_slice.metrics.as_ref(),
            |m| m.ndcg_at_10,
        ),
        non_associative_recall_at_k: metric_delta(
            standard.non_associative_slices.metrics.as_ref(),
            literal_graph.non_associative_slices.metrics.as_ref(),
            |m| m.recall_at_k,
        ),
        non_associative_evidence_recall_at_k: metric_delta(
            standard.non_associative_slices.metrics.as_ref(),
            literal_graph.non_associative_slices.metrics.as_ref(),
            |m| m.evidence_recall_at_k,
        ),
        non_associative_ndcg_at_10: metric_delta(
            standard.non_associative_slices.metrics.as_ref(),
            literal_graph.non_associative_slices.metrics.as_ref(),
            |m| m.ndcg_at_10,
        ),
        p95_latency_ms: literal_graph.overall.retrieval_latency_p95_ms
            - standard.overall.retrieval_latency_p95_ms,
    }
}

fn metric_delta(
    standard: Option<&MetricAverages>,
    candidate: Option<&MetricAverages>,
    value: impl Fn(&MetricAverages) -> f64,
) -> f64 {
    match (standard, candidate) {
        (Some(standard), Some(candidate)) => value(candidate) - value(standard),
        _ => 0.0,
    }
}

fn build_checks(
    standard: &GraphDecisionArmReport,
    literal_graph: &GraphDecisionArmReport,
    deltas: &GraphDecisionDeltas,
) -> GraphDecisionChecks {
    let associative_slice_present = standard.associative_slice.scored_queries > 0
        && literal_graph.associative_slice.scored_queries > 0;
    let literal_two_hop_observed = literal_graph.associative_queries_with_two_or_more_hops > 0;
    let benefit_threshold_met = deltas.associative_evidence_recall_at_k >= BENEFIT_THRESHOLD;
    let non_associative_zero_regression = non_associative_slices_not_lower(
        &standard.non_associative_by_slice,
        &literal_graph.non_associative_by_slice,
    );
    let zero_scope_leak = literal_graph.scope_leak_count == 0;
    let p95_latency_within_budget =
        literal_graph.overall.retrieval_latency_p95_ms <= LATENCY_BUDGET_P95_MS;
    let safe_to_wire_literal_graph = benefit_threshold_met
        && non_associative_zero_regression
        && zero_scope_leak
        && p95_latency_within_budget
        && literal_two_hop_observed;

    GraphDecisionChecks {
        associative_slice_present,
        literal_two_hop_observed,
        benefit_threshold_met,
        non_associative_zero_regression,
        zero_scope_leak,
        p95_latency_within_budget,
        safe_to_wire_literal_graph,
        all_checks_passed: associative_slice_present && safe_to_wire_literal_graph,
    }
}

fn metrics_not_lower(
    standard: Option<&MetricAverages>,
    candidate: Option<&MetricAverages>,
) -> bool {
    match (standard, candidate) {
        (Some(standard), Some(candidate)) => {
            candidate.hit_at_k + EPSILON >= standard.hit_at_k
                && candidate.mrr_at_10 + EPSILON >= standard.mrr_at_10
                && candidate.precision_at_k + EPSILON >= standard.precision_at_k
                && candidate.recall_at_k + EPSILON >= standard.recall_at_k
                && candidate.ndcg_at_10 + EPSILON >= standard.ndcg_at_10
                && candidate.evidence_recall_at_k + EPSILON >= standard.evidence_recall_at_k
        }
        (None, None) => true,
        _ => false,
    }
}

fn non_associative_slices_not_lower(
    standard: &BTreeMap<String, CategoryEvaluation>,
    candidate: &BTreeMap<String, CategoryEvaluation>,
) -> bool {
    standard.len() == candidate.len()
        && standard.iter().all(|(slice, standard)| {
            candidate.get(slice).is_some_and(|candidate| {
                metrics_not_lower(standard.metrics.as_ref(), candidate.metrics.as_ref())
                    && candidate.abstention_passed >= standard.abstention_passed
            })
        })
}

fn seed_fixture_graph_edges(conn: &Connection, dataset: &GoldenDataset) -> Result<()> {
    use crate::memory::graph_contract::{
        insert_graph_edge, GraphEdgeInput, GraphEdgeProvenance, GraphEdgeType, GraphNodeRef,
    };

    let (event_id, candidate_id, operation_id) = seed_graph_provenance(conn)?;
    let event_ids = [event_id];
    let provenance = GraphEdgeProvenance {
        source_event_ids: &event_ids,
        source_candidate_id: Some(candidate_id),
        source_operation_id: Some(operation_id),
        confidence: Some(1.0),
        reason: Some("pre-registered associative hop_path"),
    };
    let mut bridges = BTreeMap::<(String, String, String), GraphNodeRef>::new();
    let mut inserted = BTreeSet::<(String, i64, i64)>::new();
    for query in dataset
        .queries
        .iter()
        .filter(|query| query.slice_label() == "associative")
    {
        let hop = query
            .hop_path
            .as_ref()
            .with_context(|| format!("associative query {} missing hop_path", query.id))?;
        let project = query.project.as_deref().unwrap_or("");
        let key = (
            project.to_string(),
            hop.entity_type.clone(),
            hop.entity.clone(),
        );
        let bridge = if let Some(node) = bridges.get(&key) {
            *node
        } else {
            let node = create_graph_bridge(conn, project, &hop.entity_type, &hop.entity)?;
            bridges.insert(key, node);
            node
        };
        let edge_type = if hop.entity_type == "file_path" {
            GraphEdgeType::TouchesFile
        } else {
            GraphEdgeType::Mentions
        };
        for topic_key in [&hop.source, &hop.target] {
            let memory_id = conn
                .query_row(
                    "SELECT id FROM memories WHERE topic_key = ?1
                     AND (?2 IS NULL OR project = ?2)
                     AND (?3 IS NULL OR branch = ?3 OR branch IS NULL) LIMIT 1",
                    rusqlite::params![topic_key, query.project, query.branch],
                    |row| row.get(0),
                )
                .with_context(|| format!("resolve golden graph memory {topic_key}"))?;
            if inserted.insert((edge_type.as_str().to_string(), memory_id, bridge.id)) {
                insert_graph_edge(
                    conn,
                    &GraphEdgeInput {
                        edge_type,
                        from_node: GraphNodeRef::memory(memory_id)?,
                        to_node: bridge,
                        provenance,
                        valid_from_epoch: None,
                        valid_to_epoch: None,
                    },
                )?;
            }
        }
    }
    Ok(())
}

fn seed_graph_provenance(conn: &Connection) -> Result<(i64, i64, i64)> {
    let now = 1_700_000_000_i64;
    let host_id: i64 =
        conn.query_row("SELECT id FROM hosts WHERE name = 'codex-cli'", [], |row| {
            row.get(0)
        })?;
    conn.execute(
        "INSERT INTO workspaces(root_path, git_remote, git_branch, created_at_epoch, updated_at_epoch)
         VALUES ('/tmp/remem-gh853-eval', 'origin', 'main', ?1, ?1)",
        [now],
    )?;
    let workspace_id = conn.last_insert_rowid();
    conn.execute(
        "INSERT INTO projects(workspace_id, project_path, project_key, created_at_epoch, updated_at_epoch)
         VALUES (?1, '/tmp/remem-gh853-eval', 'gh853-eval', ?2, ?2)",
        rusqlite::params![workspace_id, now],
    )?;
    let project_id = conn.last_insert_rowid();
    conn.execute(
        "INSERT INTO sessions(host_id, workspace_id, project_id, session_id, started_at_epoch,
         last_seen_at_epoch, status) VALUES (?1, ?2, ?3, 'gh853-eval', ?4, ?4, 'active')",
        rusqlite::params![host_id, workspace_id, project_id, now],
    )?;
    let session_row_id = conn.last_insert_rowid();
    conn.execute(
        "INSERT INTO captured_events(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, 'gh853-eval', 'gh853-eval-event',
         'message', 'gh853-eval-hash', 'default', ?5, ?5)",
        rusqlite::params![host_id, workspace_id, project_id, session_row_id, now],
    )?;
    let event_id = conn.last_insert_rowid();
    conn.execute(
        "INSERT INTO memory_candidates(project_id, scope, memory_type, topic_key, text,
         evidence_event_ids, confidence, risk_class, review_status, created_at_epoch,
         updated_at_epoch) VALUES (?1, 'project', 'decision', 'gh853-eval',
         'pre-registered graph fixture', ?2, 1.0, 'low', 'accepted', ?3, ?3)",
        rusqlite::params![project_id, format!("[{event_id}]"), now],
    )?;
    let candidate_id = conn.last_insert_rowid();
    conn.execute(
        "INSERT INTO memory_operation_log(operation, planner_version, actor, source,
         owner_scope, owner_key, memory_type, state_key, source_candidate_id, superseded_ids,
         conflicting_ids, confidence, reason, created_at_epoch) VALUES ('add', 'gh853-eval',
         'eval', 'memory_candidate', 'project', 'gh853-eval', 'decision', 'gh853-eval',
         ?1, '[]', '[]', 1.0, 'pre-registered graph fixture', ?2)",
        rusqlite::params![candidate_id, now],
    )?;
    Ok((event_id, candidate_id, conn.last_insert_rowid()))
}

fn create_graph_bridge(
    conn: &Connection,
    project: &str,
    entity_type: &str,
    entity: &str,
) -> Result<crate::memory::graph_contract::GraphNodeRef> {
    use crate::memory::graph_contract::GraphNodeRef;
    let now = 1_700_000_000_i64;
    if entity_type == "file_path" {
        conn.execute(
            "INSERT INTO graph_file_nodes(project_id, source_project, path,
             created_at_epoch, updated_at_epoch) VALUES (NULL, ?1, ?2, ?3, ?3)",
            rusqlite::params![project, entity, now],
        )?;
        return GraphNodeRef::file(conn.last_insert_rowid());
    }
    conn.execute(
        "INSERT OR IGNORE INTO entities(canonical_name, entity_type, mention_count,
         created_at_epoch) VALUES (?1, ?2, 1, ?3)",
        rusqlite::params![entity, entity_type, now],
    )?;
    let id = conn.query_row(
        "SELECT id FROM entities WHERE canonical_name = ?1 COLLATE NOCASE LIMIT 1",
        [entity],
        |row| row.get(0),
    )?;
    GraphNodeRef::entity(id)
}

impl Display for GraphDecisionReport {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        writeln!(
            f,
            "remem graph decision eval — {:?}, k={}, threshold={:.2}",
            self.decision, self.k, self.benefit_threshold
        )?;
        writeln!(f, "reason: {}", self.decision_reason)?;
        writeln!(
            f,
            "associative evidence delta={:.3}, non-associative evidence delta={:.3}, literal-graph p95={:.2}ms",
            self.deltas.associative_evidence_recall_at_k,
            self.deltas.non_associative_evidence_recall_at_k,
            self.literal_graph.overall.retrieval_latency_p95_ms
        )?;
        writeln!(
            f,
            "checks: associative_slice_present={} literal_two_hop_observed={} benefit_threshold_met={} non_associative_zero_regression={} zero_scope_leak={} p95_latency_within_budget={} safe_to_wire_literal_graph={} all_checks_passed={}",
            self.checks.associative_slice_present,
            self.checks.literal_two_hop_observed,
            self.checks.benefit_threshold_met,
            self.checks.non_associative_zero_regression,
            self.checks.zero_scope_leak,
            self.checks.p95_latency_within_budget,
            self.checks.safe_to_wire_literal_graph,
            self.checks.all_checks_passed
        )
    }
}

pub mod evidence_fingerprint;

#[cfg(test)]
mod tests;