memd 1.9.0

Local memory CLI for AI agents with hybrid retrieval
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
use super::*;

pub(super) const CONSOLIDATION_RUN_COLUMNS: &str =
    "run_id, tenant_id, project_id, input_hash, state, consolidator, \
     prompt_hash, response_hash, validation_result, error, sparse_cleanup_done, \
     consolidator_command, consolidator_model, consolidator_version, audit_artifact_path, \
     promotion_requested, created_at_ms, updated_at_ms";

pub(super) fn row_to_consolidation_run(
    row: &rusqlite::Row<'_>,
) -> rusqlite::Result<ConsolidationRun> {
    let run_id = ConsolidationRunId::parse(&row.get::<_, String>(0)?)
        .map_err(|error| sql_decode_error(0, error))?;
    let tenant_id =
        TenantId::new(row.get::<_, String>(1)?).map_err(|error| sql_decode_error(1, error))?;
    let state = row
        .get::<_, String>(4)?
        .parse::<ConsolidationState>()
        .map_err(|error| sql_decode_error(4, error))?;
    Ok(ConsolidationRun {
        run_id,
        tenant_id,
        project_id: row.get(2)?,
        input_hash: row.get(3)?,
        state,
        consolidator: row.get(5)?,
        prompt_hash: row.get(6)?,
        response_hash: row.get(7)?,
        validation_result: row.get(8)?,
        error: row.get(9)?,
        sparse_cleanup_done: row.get::<_, i64>(10)? != 0,
        consolidator_command: row.get(11)?,
        consolidator_model: row.get(12)?,
        consolidator_version: row.get(13)?,
        audit_artifact_path: row.get(14)?,
        promotion_requested: row.get::<_, i64>(15)? != 0,
        created_at_ms: row.get(16)?,
        updated_at_ms: row.get(17)?,
    })
}

pub(super) fn row_to_consolidation_entry(
    row: &rusqlite::Row<'_>,
) -> rusqlite::Result<ConsolidationEntryRecord> {
    let run_id = ConsolidationRunId::parse(&row.get::<_, String>(0)?)
        .map_err(|error| sql_decode_error(0, error))?;
    let candidate = row
        .get::<_, Option<String>>(2)?
        .map(|value| ChunkId::parse(&value).map_err(|error| sql_decode_error(2, error)))
        .transpose()?;
    let entry_index = usize::try_from(row.get::<_, i64>(1)?).map_err(|error| {
        rusqlite::Error::FromSqlConversionFailure(
            1,
            rusqlite::types::Type::Integer,
            Box::new(error),
        )
    })?;
    let state = row
        .get::<_, String>(4)?
        .parse::<ConsolidationState>()
        .map_err(|error| sql_decode_error(4, error))?;
    Ok(ConsolidationEntryRecord {
        run_id,
        entry_index,
        candidate_chunk_id: candidate,
        source_set_hash: row.get(3)?,
        state,
        validation_error: row.get(5)?,
        created_at_ms: row.get(6)?,
        updated_at_ms: row.get(7)?,
    })
}

pub(super) fn row_to_memory_lineage(row: &rusqlite::Row<'_>) -> rusqlite::Result<MemoryLineage> {
    let run_id = ConsolidationRunId::parse(&row.get::<_, String>(0)?)
        .map_err(|error| sql_decode_error(0, error))?;
    let tenant_id =
        TenantId::new(row.get::<_, String>(1)?).map_err(|error| sql_decode_error(1, error))?;
    let source_chunk_id =
        ChunkId::parse(&row.get::<_, String>(3)?).map_err(|error| sql_decode_error(3, error))?;
    let result_chunk_id =
        ChunkId::parse(&row.get::<_, String>(4)?).map_err(|error| sql_decode_error(4, error))?;
    let relation = row
        .get::<_, String>(5)?
        .parse::<LineageRelation>()
        .map_err(|error| sql_decode_error(5, error))?;
    Ok(MemoryLineage {
        run_id,
        tenant_id,
        project_id: row.get(2)?,
        source_chunk_id,
        result_chunk_id,
        relation,
        created_at_ms: row.get(6)?,
    })
}

pub(super) fn query_consolidation_entries(
    conn: &Connection,
    run_id: &ConsolidationRunId,
) -> Result<Vec<ConsolidationEntryRecord>> {
    let mut statement = conn.prepare(
        "SELECT run_id, entry_index, candidate_chunk_id, source_set_hash,
                state, validation_error, created_at_ms, updated_at_ms
         FROM consolidation_entries WHERE run_id = ?1 ORDER BY entry_index ASC",
    )?;
    let rows = statement.query_map([run_id.to_string()], row_to_consolidation_entry)?;
    let mut entries = Vec::new();
    for row in rows {
        entries.push(row?);
    }
    Ok(entries)
}

pub(super) fn query_memory_lineage(
    conn: &Connection,
    run_id: &ConsolidationRunId,
) -> Result<Vec<MemoryLineage>> {
    let mut statement = conn.prepare(
        "SELECT run_id, tenant_id, project_id, source_chunk_id,
                result_chunk_id, relation, created_at_ms
         FROM memory_lineage WHERE run_id = ?1
         ORDER BY result_chunk_id ASC, source_chunk_id ASC",
    )?;
    let rows = statement.query_map([run_id.to_string()], row_to_memory_lineage)?;
    let mut lineage = Vec::new();
    for row in rows {
        lineage.push(row?);
    }
    Ok(lineage)
}

pub(super) fn validate_consolidation_plan(
    run: &ConsolidationRun,
    entries: &[ConsolidationEntryRecord],
    lineage: &[MemoryLineage],
) -> Result<()> {
    if run.state != ConsolidationState::Planned || run.sparse_cleanup_done {
        return Err(MemdError::ValidationError(
            "new consolidation runs must start in planned state".to_string(),
        ));
    }
    if run.input_hash.trim().is_empty() || entries.is_empty() || lineage.is_empty() {
        return Err(MemdError::ValidationError(
            "a consolidation plan requires an input hash, entries, and lineage".to_string(),
        ));
    }
    let mut expected_relation = None;
    let mut indexes = std::collections::HashSet::new();
    let mut candidates = std::collections::HashSet::new();
    for entry in entries {
        let candidate = entry.candidate_chunk_id.as_ref().ok_or_else(|| {
            MemdError::ValidationError("planned entries require candidate chunk ids".to_string())
        })?;
        if entry.run_id != run.run_id
            || entry.state != ConsolidationState::Planned
            || entry.source_set_hash.trim().is_empty()
            || !indexes.insert(entry.entry_index)
            || !candidates.insert(candidate.clone())
        {
            return Err(MemdError::ValidationError(
                "consolidation entries do not form a unique planned run".to_string(),
            ));
        }
    }
    let mut sources = std::collections::HashSet::new();
    for edge in lineage {
        let run_relation = *expected_relation.get_or_insert(edge.relation);
        if edge.run_id != run.run_id
            || edge.tenant_id != run.tenant_id
            || edge.project_id != run.project_id
            || edge.relation != run_relation
            || !candidates.contains(&edge.result_chunk_id)
            || edge.source_chunk_id == edge.result_chunk_id
            || !sources.insert(edge.source_chunk_id.clone())
        {
            return Err(MemdError::ValidationError(
                "consolidation lineage does not match its run scope".to_string(),
            ));
        }
    }
    if candidates.iter().any(|candidate| {
        !lineage
            .iter()
            .any(|edge| edge.result_chunk_id == *candidate)
    }) {
        return Err(MemdError::ValidationError(
            "every consolidation candidate requires lineage".to_string(),
        ));
    }
    Ok(())
}

impl SqliteMetadataStore {
    /// Create a consolidation journal and all of its planned entries and
    /// lineage edges in one transaction. Repeating an exact tenant/scope/input
    /// returns the first run without duplicating entries.
    pub fn begin_consolidation_run(
        &self,
        run: &ConsolidationRun,
        entries: &[ConsolidationEntryRecord],
        lineage: &[MemoryLineage],
    ) -> Result<ConsolidationRun> {
        validate_consolidation_plan(run, entries, lineage)?;

        let mut conn = self.pool.get();
        let tx = conn.transaction()?;
        let inserted = tx.execute(
            "INSERT OR IGNORE INTO consolidation_runs (
                 run_id, tenant_id, project_id, input_hash, state, consolidator,
                 prompt_hash, response_hash, validation_result, error,
                 sparse_cleanup_done, consolidator_command, consolidator_model,
                 consolidator_version, audit_artifact_path, promotion_requested,
                 created_at_ms, updated_at_ms
             ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12,
                       ?13, ?14, ?15, ?16, ?17, ?18)",
            rusqlite::params![
                run.run_id.to_string(),
                run.tenant_id.as_str(),
                run.project_id.as_deref(),
                run.input_hash,
                run.state.as_str(),
                run.consolidator,
                run.prompt_hash.as_deref(),
                run.response_hash.as_deref(),
                run.validation_result.as_deref(),
                run.error.as_deref(),
                i64::from(run.sparse_cleanup_done),
                run.consolidator_command.as_deref(),
                run.consolidator_model.as_deref(),
                run.consolidator_version.as_deref(),
                run.audit_artifact_path.as_deref(),
                i64::from(run.promotion_requested),
                run.created_at_ms,
                run.updated_at_ms,
            ],
        )?;

        if inserted == 0 {
            let existing = tx.query_row(
                &format!(
                    "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs
                     WHERE tenant_id = ?1 AND project_id IS ?2 AND input_hash = ?3
                       AND state NOT IN ('rejected', 'rolled_back')"
                ),
                rusqlite::params![
                    run.tenant_id.as_str(),
                    run.project_id.as_deref(),
                    run.input_hash
                ],
                row_to_consolidation_run,
            )?;
            tx.commit()?;
            return Ok(existing);
        }

        for entry in entries {
            tx.execute(
                "INSERT INTO consolidation_entries (
                     run_id, entry_index, candidate_chunk_id, source_set_hash,
                     state, validation_error, created_at_ms, updated_at_ms
                 ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
                rusqlite::params![
                    entry.run_id.to_string(),
                    entry.entry_index as i64,
                    entry.candidate_chunk_id.as_ref().map(ToString::to_string),
                    entry.source_set_hash,
                    entry.state.as_str(),
                    entry.validation_error.as_deref(),
                    entry.created_at_ms,
                    entry.updated_at_ms,
                ],
            )?;
        }

        for edge in lineage {
            tx.execute(
                "INSERT INTO memory_lineage (
                     run_id, tenant_id, project_id, source_chunk_id,
                     result_chunk_id, relation, created_at_ms
                 ) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
                rusqlite::params![
                    edge.run_id.to_string(),
                    edge.tenant_id.as_str(),
                    edge.project_id.as_deref(),
                    edge.source_chunk_id.to_string(),
                    edge.result_chunk_id.to_string(),
                    edge.relation.as_str(),
                    edge.created_at_ms,
                ],
            )?;
        }

        tx.commit()?;
        Ok(run.clone())
    }

    pub fn get_consolidation_run(
        &self,
        run_id: &ConsolidationRunId,
    ) -> Result<Option<ConsolidationRun>> {
        let conn = self.pool.get();
        let mut statement = conn.prepare(&format!(
            "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs WHERE run_id = ?1"
        ))?;
        let mut rows = statement.query([run_id.to_string()])?;
        Ok(rows.next()?.map(row_to_consolidation_run).transpose()?)
    }

    pub fn find_consolidation_run_by_input(
        &self,
        tenant_id: &TenantId,
        project_id: Option<&str>,
        input_hash: &str,
    ) -> Result<Option<ConsolidationRun>> {
        let conn = self.pool.get();
        let mut statement = conn.prepare(&format!(
            "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs
             WHERE tenant_id = ?1 AND project_id IS ?2 AND input_hash = ?3
               AND state NOT IN ('rejected', 'rolled_back')"
        ))?;
        let mut rows = statement.query(rusqlite::params![
            tenant_id.as_str(),
            project_id,
            input_hash
        ])?;
        Ok(rows.next()?.map(row_to_consolidation_run).transpose()?)
    }

    pub fn list_recoverable_consolidation_runs(
        &self,
        limit: usize,
    ) -> Result<Vec<ConsolidationRun>> {
        self.list_recoverable_consolidation_runs_before(limit, i64::MAX)
    }

    /// List validated candidates awaiting an explicit promotion/rejection
    /// decision. These runs are intentionally absent from automatic recovery.
    pub fn list_staged_consolidation_runs(
        &self,
        tenant_id: Option<&TenantId>,
        project_id: Option<&str>,
        limit: usize,
    ) -> Result<Vec<ConsolidationRun>> {
        if limit == 0 {
            return Ok(Vec::new());
        }
        let conn = self.pool.get();
        let mut statement = conn.prepare(&format!(
            "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs
             WHERE state = 'validated' AND promotion_requested = 0
               AND (?1 IS NULL OR tenant_id = ?1)
               AND (?2 IS NULL OR project_id = ?2)
             ORDER BY updated_at_ms ASC, run_id ASC LIMIT ?3"
        ))?;
        let rows = statement.query_map(
            rusqlite::params![tenant_id.map(TenantId::as_str), project_id, limit as i64],
            row_to_consolidation_run,
        )?;
        let mut runs = Vec::new();
        for row in rows {
            runs.push(row?);
        }
        Ok(runs)
    }

    /// List nonterminal runs whose last journal update is old enough for
    /// recovery. The cutoff is what prevents a startup/context request in
    /// the same process from rolling back a consolidation that is still
    /// writing its candidate payloads.
    pub fn list_recoverable_consolidation_runs_before(
        &self,
        limit: usize,
        updated_before_ms: i64,
    ) -> Result<Vec<ConsolidationRun>> {
        if limit == 0 {
            return Ok(Vec::new());
        }
        let conn = self.pool.get();
        let mut statement = conn.prepare(&format!(
            "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs
             WHERE (state IN ('planned', 'candidate_written', 'failed_recoverable')
                    OR (state = 'validated' AND promotion_requested = 1))
               AND updated_at_ms <= ?1
             ORDER BY updated_at_ms ASC, run_id ASC LIMIT ?2"
        ))?;
        let rows = statement.query_map(
            rusqlite::params![updated_before_ms, limit as i64],
            row_to_consolidation_run,
        )?;
        let mut runs = Vec::new();
        for row in rows {
            runs.push(row?);
        }
        Ok(runs)
    }

    pub fn list_consolidation_runs_pending_sparse_cleanup(
        &self,
        limit: usize,
    ) -> Result<Vec<ConsolidationRun>> {
        if limit == 0 {
            return Ok(Vec::new());
        }
        let conn = self.pool.get();
        let mut statement = conn.prepare(&format!(
            "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs
             WHERE state = 'committed' AND sparse_cleanup_done = 0
             ORDER BY updated_at_ms ASC, run_id ASC LIMIT ?1"
        ))?;
        let rows = statement.query_map([limit as i64], row_to_consolidation_run)?;
        let mut runs = Vec::new();
        for row in rows {
            runs.push(row?);
        }
        Ok(runs)
    }

    pub fn mark_consolidation_sparse_cleanup_done(
        &self,
        run_id: &ConsolidationRunId,
        now_ms: i64,
    ) -> Result<bool> {
        let conn = self.pool.get();
        let rows = conn.execute(
            "UPDATE consolidation_runs
                SET sparse_cleanup_done = 1, updated_at_ms = ?1
              WHERE run_id = ?2 AND state = 'committed' AND sparse_cleanup_done = 0",
            rusqlite::params![now_ms, run_id.to_string()],
        )?;
        Ok(rows == 1)
    }

    /// Record a per-run recovery error without changing the protocol state.
    /// Updating the clock rotates a poisoned run behind other recoverable
    /// work instead of letting it remain the oldest row forever.
    pub fn record_consolidation_recovery_error(
        &self,
        run_id: &ConsolidationRunId,
        now_ms: i64,
        error: &str,
    ) -> Result<bool> {
        let conn = self.pool.get();
        let rows = conn.execute(
            "UPDATE consolidation_runs
                SET error = ?1, updated_at_ms = ?2
              WHERE run_id = ?3
                AND state NOT IN ('committed', 'rejected', 'rolled_back')",
            rusqlite::params![error, now_ms, run_id.to_string()],
        )?;
        Ok(rows == 1)
    }

    /// Persist explicit promotion intent before any validated candidate can
    /// become visible or supersede a source. Recovery consults this same bit.
    pub fn request_consolidation_promotion(
        &self,
        run_id: &ConsolidationRunId,
        now_ms: i64,
    ) -> Result<bool> {
        let conn = self.pool.get();
        let rows = conn.execute(
            "UPDATE consolidation_runs
                SET promotion_requested = 1, updated_at_ms = ?1
              WHERE run_id = ?2
                AND state IN ('planned', 'candidate_written', 'validated', 'failed_recoverable')",
            rusqlite::params![now_ms, run_id.to_string()],
        )?;
        Ok(rows == 1)
    }

    /// Hide any candidate payloads still attached to a terminal run. This
    /// reconciles the narrow race where a writer persists a candidate after
    /// another task has already rolled its Planned run back.
    pub fn hide_terminal_consolidation_candidates(&self, now_ms: i64) -> Result<usize> {
        let conn = self.pool.get();
        Ok(conn.execute(
            "UPDATE chunks
                SET status = 'error', lifecycle_updated_at_ms = ?1
              WHERE status = 'candidate' AND chunk_id IN (
                    SELECT entry.candidate_chunk_id
                      FROM consolidation_entries AS entry
                      JOIN consolidation_runs AS run ON run.run_id = entry.run_id
                     WHERE run.state IN ('committed', 'rejected', 'rolled_back')
              )",
            [now_ms],
        )?)
    }

    /// Hide every still-staged payload for one run without changing its
    /// journal state. Used when a guarded transition discovers that another
    /// recovery task already made the run terminal.
    pub fn hide_consolidation_candidates(
        &self,
        run_id: &ConsolidationRunId,
        now_ms: i64,
    ) -> Result<usize> {
        let conn = self.pool.get();
        Ok(conn.execute(
            "UPDATE chunks
                SET status = 'error', lifecycle_updated_at_ms = ?1
              WHERE status = 'candidate' AND chunk_id IN (
                    SELECT candidate_chunk_id
                      FROM consolidation_entries
                     WHERE run_id = ?2
              )",
            rusqlite::params![now_ms, run_id.to_string()],
        )?)
    }

    pub fn get_consolidation_entries(
        &self,
        run_id: &ConsolidationRunId,
    ) -> Result<Vec<ConsolidationEntryRecord>> {
        let conn = self.pool.get();
        query_consolidation_entries(&conn, run_id)
    }

    pub fn get_memory_lineage(&self, run_id: &ConsolidationRunId) -> Result<Vec<MemoryLineage>> {
        let conn = self.pool.get();
        query_memory_lineage(&conn, run_id)
    }

    /// Move a run and all of its entries through one guarded state transition.
    pub fn transition_consolidation_run(
        &self,
        run_id: &ConsolidationRunId,
        expected: ConsolidationState,
        next: ConsolidationState,
        now_ms: i64,
        validation_result: Option<&str>,
        error: Option<&str>,
    ) -> Result<bool> {
        let mut conn = self.pool.get();
        let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
        let run_rows = tx.execute(
            "UPDATE consolidation_runs
                SET state = ?1, validation_result = COALESCE(?2, validation_result),
                    error = ?3, updated_at_ms = ?4
              WHERE run_id = ?5 AND state = ?6",
            rusqlite::params![
                next.as_str(),
                validation_result,
                error,
                now_ms,
                run_id.to_string(),
                expected.as_str(),
            ],
        )?;
        if run_rows == 0 {
            return Ok(false);
        }
        let entry_rows = tx.execute(
            "UPDATE consolidation_entries
                SET state = ?1, validation_error = ?2, updated_at_ms = ?3
              WHERE run_id = ?4 AND state = ?5",
            rusqlite::params![
                next.as_str(),
                error,
                now_ms,
                run_id.to_string(),
                expected.as_str(),
            ],
        )?;
        let total_entries: i64 = tx.query_row(
            "SELECT COUNT(*) FROM consolidation_entries WHERE run_id = ?1",
            [run_id.to_string()],
            |row| row.get(0),
        )?;
        if entry_rows != total_entries as usize {
            return Err(MemdError::StorageError(format!(
                "consolidation run {run_id} has entries outside expected state {expected}"
            )));
        }
        tx.commit()?;
        Ok(true)
    }

    /// Reject or roll back a run while keeping any already-written candidate
    /// payloads durably hidden. Candidate metadata is moved to `error` in the
    /// same transaction as the terminal journal state.
    pub fn terminate_consolidation_run(
        &self,
        run_id: &ConsolidationRunId,
        expected: ConsolidationState,
        terminal: ConsolidationState,
        now_ms: i64,
        error: &str,
    ) -> Result<bool> {
        if !matches!(
            terminal,
            ConsolidationState::Rejected | ConsolidationState::RolledBack
        ) {
            return Err(MemdError::ValidationError(
                "consolidation termination requires rejected or rolled_back state".to_string(),
            ));
        }
        let mut conn = self.pool.get();
        let tx = conn.transaction()?;
        let run_rows = tx.execute(
            "UPDATE consolidation_runs
                SET state = ?1, error = ?2, updated_at_ms = ?3
              WHERE run_id = ?4 AND state = ?5",
            rusqlite::params![
                terminal.as_str(),
                error,
                now_ms,
                run_id.to_string(),
                expected.as_str(),
            ],
        )?;
        if run_rows == 0 {
            return Ok(false);
        }
        let entry_rows = tx.execute(
            "UPDATE consolidation_entries
                SET state = ?1, validation_error = ?2, updated_at_ms = ?3
              WHERE run_id = ?4 AND state = ?5",
            rusqlite::params![
                terminal.as_str(),
                error,
                now_ms,
                run_id.to_string(),
                expected.as_str(),
            ],
        )?;
        let total_entries: i64 = tx.query_row(
            "SELECT COUNT(*) FROM consolidation_entries WHERE run_id = ?1",
            [run_id.to_string()],
            |row| row.get(0),
        )?;
        if entry_rows != total_entries as usize {
            return Err(MemdError::StorageError(format!(
                "consolidation run {run_id} has entries outside expected state {expected}"
            )));
        }
        tx.execute(
            "UPDATE chunks
                SET status = 'error', lifecycle_updated_at_ms = ?1
              WHERE status = 'candidate' AND chunk_id IN (
                    SELECT candidate_chunk_id FROM consolidation_entries WHERE run_id = ?2
              )",
            rusqlite::params![now_ms, run_id.to_string()],
        )?;
        tx.commit()?;
        Ok(true)
    }

    /// Promote all validated candidates and, for project-scoped runs, their
    /// sources in one SQLite transaction. Tenant-wide derivations never
    /// tombstone or supersede project-owned sources.
    pub fn atomic_promote_consolidation_run(
        &self,
        run_id: &ConsolidationRunId,
        now_ms: i64,
    ) -> Result<PromotionOutcome> {
        let mut conn = self.pool.get();
        let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
        let run = tx.query_row(
            &format!(
                "SELECT {CONSOLIDATION_RUN_COLUMNS} FROM consolidation_runs WHERE run_id = ?1"
            ),
            [run_id.to_string()],
            row_to_consolidation_run,
        )?;
        if run.state == ConsolidationState::Committed {
            return Ok(PromotionOutcome::AlreadyCommitted);
        }
        if run.state != ConsolidationState::Validated {
            return Err(MemdError::StorageError(format!(
                "consolidation run {run_id} cannot promote from state {}",
                run.state
            )));
        }
        if !run.promotion_requested {
            return Err(MemdError::ValidationError(format!(
                "consolidation run {run_id} has no durable promotion request"
            )));
        }

        let entries = query_consolidation_entries(&tx, run_id)?;
        let lineage = query_memory_lineage(&tx, run_id)?;
        if entries.is_empty() || lineage.is_empty() {
            return Err(MemdError::StorageError(format!(
                "consolidation run {run_id} has no entries or lineage"
            )));
        }

        let candidate_ids = entries
            .iter()
            .map(|entry| {
                if entry.state != ConsolidationState::Validated {
                    return Err(MemdError::StorageError(format!(
                        "consolidation entry {} is not validated",
                        entry.entry_index
                    )));
                }
                entry.candidate_chunk_id.clone().ok_or_else(|| {
                    MemdError::StorageError(format!(
                        "consolidation entry {} has no candidate chunk",
                        entry.entry_index
                    ))
                })
            })
            .collect::<Result<std::collections::HashSet<_>>>()?;

        for candidate_id in &candidate_ids {
            let (status, project_id): (String, Option<String>) = tx.query_row(
                "SELECT status, project_id FROM chunks
                 WHERE tenant_id = ?1 AND chunk_id = ?2",
                rusqlite::params![run.tenant_id.as_str(), candidate_id.to_string()],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )?;
            if status != ChunkStatus::Candidate.to_string() || project_id != run.project_id {
                return Err(MemdError::StorageError(format!(
                    "candidate {candidate_id} drifted outside run {run_id}"
                )));
            }
            if !lineage
                .iter()
                .any(|edge| edge.result_chunk_id == *candidate_id)
            {
                return Err(MemdError::StorageError(format!(
                    "candidate {candidate_id} has no lineage in run {run_id}"
                )));
            }
        }

        for edge in &lineage {
            if edge.tenant_id != run.tenant_id
                || edge.project_id != run.project_id
                || !candidate_ids.contains(&edge.result_chunk_id)
            {
                return Err(MemdError::StorageError(format!(
                    "lineage edge escaped consolidation run {run_id} scope"
                )));
            }
            let expected_relation = lineage[0].relation;
            if edge.relation != expected_relation {
                return Err(MemdError::StorageError(format!(
                    "lineage relation {} is invalid for run {run_id} scope",
                    edge.relation
                )));
            }

            let (source_status, source_project, superseded_by, tier, expires_at_ms): (
                String,
                Option<String>,
                Option<String>,
                String,
                Option<i64>,
            ) = tx.query_row(
                "SELECT status, project_id, superseded_by, tier, expires_at_ms FROM chunks
                 WHERE tenant_id = ?1 AND chunk_id = ?2",
                rusqlite::params![run.tenant_id.as_str(), edge.source_chunk_id.to_string()],
                |row| {
                    Ok((
                        row.get(0)?,
                        row.get(1)?,
                        row.get(2)?,
                        row.get(3)?,
                        row.get(4)?,
                    ))
                },
            )?;
            if source_status != ChunkStatus::Final.to_string()
                || superseded_by.is_some()
                || tier == MemoryTier::History.to_string()
                || expires_at_ms.is_some_and(|expiry| expiry <= now_ms)
            {
                return Err(MemdError::StorageError(format!(
                    "source {} is no longer a visible final head for run {run_id}",
                    edge.source_chunk_id
                )));
            }
            if expected_relation == LineageRelation::Supersedes && source_project != run.project_id
            {
                return Err(MemdError::StorageError(format!(
                    "source {} escaped project scope for run {run_id}",
                    edge.source_chunk_id
                )));
            }
        }

        for candidate_id in &candidate_ids {
            let rows = tx.execute(
                "UPDATE chunks
                    SET status = 'final', lifecycle_updated_at_ms = ?1
                  WHERE tenant_id = ?2 AND chunk_id = ?3 AND status = 'candidate'",
                rusqlite::params![now_ms, run.tenant_id.as_str(), candidate_id.to_string()],
            )?;
            if rows != 1 {
                return Err(MemdError::StorageError(format!(
                    "candidate {candidate_id} could not be promoted for run {run_id}"
                )));
            }
        }

        if lineage[0].relation == LineageRelation::Supersedes {
            for edge in &lineage {
                let rows = tx.execute(
                    "UPDATE chunks
                        SET status = 'superseded', superseded_by = ?1,
                            lifecycle_updated_at_ms = ?2
                      WHERE tenant_id = ?3 AND chunk_id = ?4 AND status = 'final'
                        AND project_id IS ?5 AND superseded_by IS NULL
                        AND tier != 'history'
                        AND (expires_at_ms IS NULL OR expires_at_ms > ?6)",
                    rusqlite::params![
                        edge.result_chunk_id.to_string(),
                        now_ms,
                        run.tenant_id.as_str(),
                        edge.source_chunk_id.to_string(),
                        run.project_id.as_deref(),
                        now_ms,
                    ],
                )?;
                if rows != 1 {
                    return Err(MemdError::StorageError(format!(
                        "source {} could not be superseded for run {run_id}",
                        edge.source_chunk_id
                    )));
                }
            }
        }

        let entry_rows = tx.execute(
            "UPDATE consolidation_entries
                SET state = 'committed', updated_at_ms = ?1
              WHERE run_id = ?2 AND state = 'validated'",
            rusqlite::params![now_ms, run_id.to_string()],
        )?;
        if entry_rows != entries.len() {
            return Err(MemdError::StorageError(format!(
                "not all entries committed for run {run_id}"
            )));
        }
        let run_rows = tx.execute(
            "UPDATE consolidation_runs
                SET state = 'committed', error = NULL, updated_at_ms = ?1
              WHERE run_id = ?2 AND state = 'validated'",
            rusqlite::params![now_ms, run_id.to_string()],
        )?;
        if run_rows != 1 {
            return Err(MemdError::StorageError(format!(
                "run {run_id} lost its validated promotion guard"
            )));
        }
        tx.commit()?;
        Ok(PromotionOutcome::Committed)
    }
}