mori-cli 0.1.0

mori — the git for cognition. Compile context with the Locus SDK and keep it in a per-repo SurrealKV file.
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
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use std::collections::HashSet;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use chrono::{DateTime, Utc};
use locus_core_rs::domain::contracts::{
    NodeStore, NodeStoreInitializer, SemanticIndexStore, SemanticIndexStoreInitializer,
};
use locus_core_rs::domain::models::AvecState;
use locus_core_rs::storage::surrealdb::QueryParams;
use locus_core_rs::{
    StoreContextService, SttpNodeParser, SurrealDbClient, SurrealDbNodeStore,
    SurrealDbRuntimeOptions, SurrealDbSemanticIndexStore, TreeSitterValidator,
};
use locus_sdk::prelude::{
    FallbackPolicy, MemoryFindRequest, MemoryFindService, MemoryPage, MemoryRecallRequest,
    MemoryRecallService, MemoryScope, MemoryScoring, StrictnessMode,
};
use locus_surreal_adapter::RuntimeSurrealDbClient;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use uuid::Uuid;

use crate::compile::compile_context;
use crate::repo::Repo;
use crate::stage::{ContextKind, StagedContext};

/// One embedded SurrealKV connection.
///
/// Construct it at the start of a command and drop it before the command
/// returns. There is no resident process.
pub struct Memory {
    client: Arc<RuntimeSurrealDbClient>,
    store: Arc<SurrealDbNodeStore>,
    index: Arc<SurrealDbSemanticIndexStore>,
    endpoint: String,
}

impl Memory {
    pub async fn connect(repo: &Repo) -> Result<Self> {
        let kv_dir = repo.kv_dir();
        std::fs::create_dir_all(&kv_dir)
            .with_context(|| format!("creating {}", kv_dir.display()))?;
        let endpoint = format!("surrealkv://{}", kv_dir.display());
        let runtime = SurrealDbRuntimeOptions {
            root_dir: repo.root.display().to_string(),
            use_remote: false,
            endpoint: endpoint.clone(),
            namespace: "mori".to_string(),
            database: "memory".to_string(),
        };

        let client = Arc::new(
            RuntimeSurrealDbClient::connect(&runtime, None, None)
                .await
                .with_context(|| format!("connecting to {endpoint}"))?,
        );
        let index = Arc::new(SurrealDbSemanticIndexStore::new(client.clone()));
        let store = Arc::new(SurrealDbNodeStore::new(client.clone()));

        let initializer: Arc<dyn NodeStoreInitializer> = store.clone();
        initializer.initialize_async().await?;
        let semantic_initializer: Arc<dyn SemanticIndexStoreInitializer> = index.clone();
        semantic_initializer.initialize_async().await?;

        client
            .raw_query(
                "DEFINE TABLE IF NOT EXISTS mori_commit SCHEMALESS;
                 DEFINE TABLE IF NOT EXISTS mori_ref SCHEMALESS;
                 DEFINE TABLE IF NOT EXISTS mori_branch SCHEMALESS;",
                QueryParams::new(),
            )
            .await
            .context("preparing mori commit tables")?;

        let memory = Self {
            client,
            store,
            index,
            endpoint,
        };
        memory.ensure_branch().await?;
        Ok(memory)
    }

    pub fn endpoint(&self) -> &str {
        &self.endpoint
    }

    /// Drop the embedded engine. Safe to call explicitly at the end of a command.
    pub fn disconnect(self) {}

    pub async fn commit(
        &self,
        message: &str,
        session: &str,
        staged: &[StagedContext],
    ) -> Result<CommitRecord> {
        let message = message.trim();
        if message.is_empty() {
            bail!("commit message cannot be empty");
        }
        if staged.is_empty() {
            bail!("nothing staged");
        }

        let store_context = StoreContextService::new(
            self.store.clone() as Arc<dyn NodeStore>,
            Arc::new(TreeSitterValidator::new()),
            SttpNodeParser::new(),
        )
        .with_semantic_index(self.index.clone() as Arc<dyn SemanticIndexStore>);

        let mut entries = Vec::with_capacity(staged.len());
        for item in staged {
            let compiled = compile_context(item)?;
            let stored = store_context
                .store_async(&compiled.raw_sttp, &compiled.session)
                .await;
            if !stored.valid {
                bail!(
                    "could not store {}: {}",
                    compiled.source,
                    stored
                        .validation_error
                        .unwrap_or_else(|| "store rejected the context".to_string())
                );
            }
            entries.push(CommitEntry {
                node_id: stored.node_id,
                kind: compiled.kind,
                session: compiled.session,
                source: compiled.source,
                summary: compiled.summary,
            });
        }

        let branch = self.current_branch().await?;
        let parent = self.branch_tip(&branch).await?;
        let record = CommitRecord {
            id: Uuid::new_v4().simple().to_string(),
            parent,
            merge_parents: Vec::new(),
            message: message.to_string(),
            session: session.to_string(),
            created_at: Utc::now(),
            entries,
        };
        self.store_commit(&record).await?;
        self.write_branch(&branch, Some(&record.id)).await?;
        Ok(record)
    }

    /// Bring `branch` into the current branch.
    ///
    /// A fast-forward moves the current pointer. A real merge writes one commit
    /// with both parents and no new context. Stored nodes stay where they are.
    pub async fn merge(
        &self,
        branch: &str,
        message: Option<&str>,
        session: &str,
    ) -> Result<MergeOutcome> {
        let branch = validate_branch_name(branch)?;
        let current = self.current_branch().await?;
        if branch == current {
            bail!("cannot merge a branch into itself");
        }
        let ours = self.branch_tip(&current).await?;
        let theirs = self.branch_tip(branch).await?;
        if self.is_ancestor(theirs.as_deref(), ours.as_deref()).await? {
            return Ok(MergeOutcome::UpToDate);
        }
        if self.is_ancestor(ours.as_deref(), theirs.as_deref()).await? {
            let commit_id = theirs
                .clone()
                .ok_or_else(|| anyhow!("branch '{branch}' has no commits"))?;
            self.write_branch(&current, Some(&commit_id)).await?;
            return Ok(MergeOutcome::FastForward { commit_id });
        }

        let message = message
            .map(str::trim)
            .filter(|text| !text.is_empty())
            .map(str::to_string)
            .unwrap_or_else(|| format!("merge {branch} into {current}"));
        let record = CommitRecord {
            id: Uuid::new_v4().simple().to_string(),
            parent: ours,
            merge_parents: vec![theirs.ok_or_else(|| anyhow!("branch '{branch}' has no commits"))?],
            message,
            session: session.to_string(),
            created_at: Utc::now(),
            entries: Vec::new(),
        };
        self.store_commit(&record).await?;
        self.write_branch(&current, Some(&record.id)).await?;
        Ok(MergeOutcome::Merged { commit: record })
    }

    /// Replay this branch's own commits onto `onto`.
    ///
    /// The target branch stays where it is. Merge commits are refused before
    /// anything is written, because replaying them would drop a parent.
    pub async fn rebase(&self, onto: &str) -> Result<RebaseOutcome> {
        let onto = validate_branch_name(onto)?;
        let current = self.current_branch().await?;
        if onto == current {
            bail!("cannot rebase a branch onto itself");
        }
        let onto_tip = self.branch_tip(onto).await?;
        let current_tip = self.branch_tip(&current).await?;
        let onto_history = self.reachable(onto_tip.as_deref()).await?;
        let pending = self
            .first_parent_commits(current_tip.as_deref(), &onto_history)
            .await?;
        if pending
            .iter()
            .any(|record| !record.merge_parents.is_empty())
        {
            bail!("rebase does not replay merge commits");
        }
        if pending.is_empty() {
            if current_tip == onto_tip {
                return Ok(RebaseOutcome::UpToDate);
            }
            let commit_id = onto_tip
                .clone()
                .ok_or_else(|| anyhow!("branch '{onto}' has no commits"))?;
            self.write_branch(&current, Some(&commit_id)).await?;
            return Ok(RebaseOutcome::FastForward { commit_id });
        }

        let mut parent = onto_tip;
        let mut commits = Vec::with_capacity(pending.len());
        for record in pending.into_iter().rev() {
            let replay = CommitRecord {
                id: Uuid::new_v4().simple().to_string(),
                parent: parent.clone(),
                merge_parents: Vec::new(),
                message: record.message,
                session: record.session,
                created_at: record.created_at,
                entries: record.entries,
            };
            self.store_commit(&replay).await?;
            parent = Some(replay.id.clone());
            commits.push(replay);
        }
        let tip = parent.ok_or_else(|| anyhow!("rebase produced no commits"))?;
        self.write_branch(&current, Some(&tip)).await?;
        Ok(RebaseOutcome::Rebased { commits, tip })
    }

    pub async fn current_branch(&self) -> Result<String> {
        let row = self
            .read_head()
            .await?
            .ok_or_else(|| anyhow!("HEAD is missing"))?;
        row.get("branch")
            .and_then(Value::as_str)
            .filter(|name| !name.is_empty())
            .map(str::to_string)
            .ok_or_else(|| anyhow!("HEAD does not name a branch"))
    }

    pub async fn branches(&self) -> Result<Vec<BranchInfo>> {
        let current = self.current_branch().await?;
        let rows = self
            .client
            .raw_query("SELECT * FROM mori_branch;", QueryParams::new())
            .await
            .context("listing branches")?;
        let mut branches = rows
            .iter()
            .filter_map(|row| branch_from_row(row))
            .map(|row| BranchInfo {
                current: row.name == current,
                name: row.name,
                commit_id: row.commit_id,
            })
            .collect::<Vec<_>>();
        branches.sort_by(|left, right| left.name.cmp(&right.name));
        Ok(branches)
    }

    /// Point a new branch at the current tip. The current branch stays checked out.
    pub async fn create_branch(&self, name: &str) -> Result<String> {
        let name = validate_branch_name(name)?;
        if self.read_branch(name).await?.is_some() {
            bail!("branch '{name}' already exists");
        }
        let tip = self.branch_tip(&self.current_branch().await?).await?;
        self.write_branch(name, tip.as_deref()).await?;
        Ok(name.to_string())
    }

    pub async fn checkout_branch(&self, name: &str) -> Result<String> {
        let name = validate_branch_name(name)?;
        if self.read_branch(name).await?.is_none() {
            bail!("branch '{name}' does not exist");
        }
        self.set_head_branch(name).await?;
        Ok(name.to_string())
    }

    pub async fn branch_tip(&self, name: &str) -> Result<Option<String>> {
        let name = validate_branch_name(name)?;
        let Some(row) = self.read_branch(name).await? else {
            bail!("branch '{name}' does not exist");
        };
        Ok(row.commit_id)
    }

    pub async fn head(&self) -> Result<Option<CommitRecord>> {
        let Some(id) = self.branch_tip(&self.current_branch().await?).await? else {
            return Ok(None);
        };
        self.commit_by_id(&id).await
    }

    pub async fn log(&self, limit: usize) -> Result<Vec<CommitRecord>> {
        let head = self.branch_tip(&self.current_branch().await?).await?;
        self.walk_history(head.as_deref(), Some(limit.max(1))).await
    }

    pub async fn find_commit(&self, prefix: &str) -> Result<CommitRecord> {
        let prefix = prefix.trim();
        if prefix.is_empty() {
            bail!("commit id is empty");
        }
        let rows = self
            .client
            .raw_query("SELECT * FROM mori_commit;", QueryParams::new())
            .await
            .context("listing commits")?;
        let matches = rows
            .into_iter()
            .filter_map(|row| commit_from_row(&row).ok())
            .filter(|record| record.id.starts_with(prefix))
            .collect::<Vec<_>>();
        match matches.len() {
            0 => bail!("no commit matches '{prefix}'"),
            1 => Ok(matches.into_iter().next().unwrap()),
            _ => bail!("commit prefix '{prefix}' is ambiguous"),
        }
    }

    pub async fn node_raw(&self, node_id: &str) -> Result<String> {
        let id = sanitize_node_id(node_id)?;
        let rows = self
            .client
            .raw_query(
                &format!("SELECT raw FROM temporal_node:`{id}`;"),
                QueryParams::new(),
            )
            .await
            .context("reading stored node")?;
        rows.first()
            .and_then(|row| row.get("raw"))
            .and_then(Value::as_str)
            .map(str::to_string)
            .ok_or_else(|| anyhow!("node {id} has no stored text"))
    }

    pub async fn recall(
        &self,
        query: &str,
        session: Option<&str>,
        limit: usize,
    ) -> Result<Vec<Recalled>> {
        let visible = self.visible_nodes().await?;
        if visible.is_empty() {
            return Ok(Vec::new());
        }
        let limit = limit.max(1);
        let recall = MemoryRecallService::new(self.store.clone() as Arc<dyn NodeStore>)
            .with_semantic_index(self.index.clone() as Arc<dyn SemanticIndexStore>);
        let result = recall
            .execute(&MemoryRecallRequest {
                scope: scope_for(session),
                page: MemoryPage {
                    limit: scan_limit(limit),
                    cursor: None,
                },
                scoring: MemoryScoring {
                    fallback_policy: FallbackPolicy::OnEmpty,
                    strictness: StrictnessMode::Balanced,
                    lexical_weight: 0.4,
                    ..MemoryScoring::default()
                },
                current_avec: Some(AvecState::analytical()),
                query_text: Some(query.to_string()),
                ..MemoryRecallRequest::default()
            })
            .await?;

        let path = format!("{:?}", result.retrieval_path).to_ascii_lowercase();
        Ok(result
            .nodes
            .into_iter()
            .filter(|node| visible.matches(node.sync_key.as_str(), node.raw.as_str()))
            .take(limit)
            .map(|node| Recalled {
                session: node.session_id,
                summary: node
                    .context_summary
                    .filter(|value| !value.trim().is_empty())
                    .unwrap_or_else(|| "(stored context)".to_string()),
                raw_sttp: node.raw,
                path: path.clone(),
            })
            .collect())
    }

    pub async fn find(
        &self,
        session: Option<&str>,
        contains: Option<&str>,
        limit: usize,
    ) -> Result<Vec<Recalled>> {
        let visible = self.visible_nodes().await?;
        if visible.is_empty() {
            return Ok(Vec::new());
        }
        let limit = limit.max(1);
        let find = MemoryFindService::new(self.store.clone() as Arc<dyn NodeStore>)
            .with_semantic_index(self.index.clone() as Arc<dyn SemanticIndexStore>);
        let mut request = MemoryFindRequest {
            scope: scope_for(session),
            page: MemoryPage {
                limit: scan_limit(limit),
                cursor: None,
            },
            ..MemoryFindRequest::default()
        };
        if let Some(text) = contains.map(str::trim).filter(|text| !text.is_empty()) {
            request.filter.text_contains = Some(text.to_string());
        }
        let result = find.execute(&request).await?;
        Ok(result
            .nodes
            .into_iter()
            .filter(|node| visible.matches(node.sync_key.as_str(), node.raw.as_str()))
            .take(limit)
            .map(|node| Recalled {
                session: node.session_id,
                summary: node
                    .context_summary
                    .filter(|value| !value.trim().is_empty())
                    .unwrap_or_else(|| "(stored context)".to_string()),
                raw_sttp: node.raw,
                path: "find".to_string(),
            })
            .collect())
    }

    async fn ensure_branch(&self) -> Result<()> {
        let Some(row) = self.read_head().await? else {
            self.write_branch("main", None).await?;
            self.set_head_branch("main").await?;
            return Ok(());
        };

        if let Some(name) = row
            .get("branch")
            .and_then(Value::as_str)
            .filter(|name| !name.is_empty())
        {
            if self.read_branch(name).await?.is_none() {
                let tip = optional_string(row.get("commit_id"));
                self.write_branch(name, tip.as_deref()).await?;
            }
            return Ok(());
        }

        let tip = optional_string(row.get("commit_id"));
        self.write_branch("main", tip.as_deref()).await?;
        self.set_head_branch("main").await?;
        Ok(())
    }

    async fn read_head(&self) -> Result<Option<Value>> {
        let rows = self
            .client
            .raw_query("SELECT * FROM mori_ref:HEAD;", QueryParams::new())
            .await
            .context("reading HEAD")?;
        Ok(rows.into_iter().next())
    }

    async fn read_branch(&self, name: &str) -> Result<Option<BranchRow>> {
        let name = validate_branch_name(name)?;
        let rows = self
            .client
            .raw_query(
                &format!("SELECT * FROM mori_branch:`{name}`;"),
                QueryParams::new(),
            )
            .await
            .context("reading branch")?;
        Ok(rows.first().and_then(branch_from_row))
    }

    async fn write_branch(&self, name: &str, commit_id: Option<&str>) -> Result<()> {
        let name = validate_branch_name(name)?;
        let mut params = QueryParams::new();
        params.insert("mori_name".to_string(), json!(name));
        params.insert("mori_commit_id".to_string(), json!(commit_id));
        self.client
            .raw_query(
                &format!(
                    "UPSERT mori_branch:`{name}` SET name = $mori_name, commit_id = $mori_commit_id;"
                ),
                params,
            )
            .await
            .context("writing branch")?;
        Ok(())
    }

    async fn set_head_branch(&self, name: &str) -> Result<()> {
        let name = validate_branch_name(name)?;
        let mut params = QueryParams::new();
        params.insert("mori_branch".to_string(), json!(name));
        self.client
            .raw_query("UPSERT mori_ref:HEAD SET branch = $mori_branch;", params)
            .await
            .context("updating HEAD")?;
        Ok(())
    }

    async fn visible_nodes(&self) -> Result<VisibleNodes> {
        let mut visible = VisibleNodes::default();
        let head = self.branch_tip(&self.current_branch().await?).await?;
        for record in self.walk_history(head.as_deref(), None).await? {
            for entry in &record.entries {
                if let Some(identity) = self.node_identity(&entry.node_id).await? {
                    if !identity.sync_key.is_empty() {
                        visible.sync_keys.insert(identity.sync_key);
                    }
                    if !identity.raw.is_empty() {
                        visible.raws.insert(identity.raw);
                    }
                }
            }
        }
        Ok(visible)
    }

    async fn store_commit(&self, record: &CommitRecord) -> Result<()> {
        let mut params = QueryParams::new();
        params.insert("mori_commit_id".to_string(), json!(record.id));
        params.insert("mori_parent".to_string(), json!(record.parent));
        params.insert(
            "mori_merge_parents".to_string(),
            json!(record.merge_parents),
        );
        params.insert("mori_message".to_string(), json!(record.message));
        params.insert("mori_scope".to_string(), json!(record.session));
        params.insert(
            "mori_created_at".to_string(),
            json!(record.created_at.to_rfc3339()),
        );
        params.insert(
            "mori_entries".to_string(),
            serde_json::to_value(&record.entries)?,
        );

        self.client
            .raw_query(
                &format!(
                    "CREATE mori_commit:`{}` SET commit_id = $mori_commit_id, parent = $mori_parent, merge_parents = $mori_merge_parents, message = $mori_message, session = $mori_scope, created_at = $mori_created_at, entries = $mori_entries;",
                    record.id
                ),
                params,
            )
            .await
            .context("writing commit")?;
        Ok(())
    }

    /// `ancestor` is an ancestor of `descendant` when walking every parent of
    /// `descendant` reaches it. A missing commit is an ancestor of every history.
    async fn is_ancestor(&self, ancestor: Option<&str>, descendant: Option<&str>) -> Result<bool> {
        let Some(ancestor) = ancestor else {
            return Ok(true);
        };
        Ok(self.reachable(descendant).await?.contains(ancestor))
    }

    async fn reachable(&self, start: Option<&str>) -> Result<HashSet<String>> {
        Ok(self
            .walk_history(start, None)
            .await?
            .into_iter()
            .map(|record| record.id)
            .collect())
    }

    /// Newest first. Stops when a commit is already in `stop`, which is the
    /// history being replayed onto.
    async fn first_parent_commits(
        &self,
        start: Option<&str>,
        stop: &HashSet<String>,
    ) -> Result<Vec<CommitRecord>> {
        let mut records = Vec::new();
        let mut seen = HashSet::new();
        let mut cursor = start.map(str::to_string);
        while let Some(id) = cursor {
            if stop.contains(&id) || !seen.insert(id.clone()) {
                break;
            }
            let Some(record) = self.commit_by_id(&id).await? else {
                break;
            };
            cursor = record.parent.clone();
            records.push(record);
        }
        Ok(records)
    }

    /// Newest first. Merge parents are pushed before the first parent so the
    /// first parent is visited next, then the merged-in history.
    async fn walk_history(
        &self,
        start: Option<&str>,
        limit: Option<usize>,
    ) -> Result<Vec<CommitRecord>> {
        let Some(start) = start else {
            return Ok(Vec::new());
        };
        let mut records = Vec::new();
        let mut seen = HashSet::new();
        let mut stack = vec![start.to_string()];
        while let Some(id) = stack.pop() {
            if limit.is_some_and(|limit| records.len() >= limit) {
                break;
            }
            if !seen.insert(id.clone()) {
                continue;
            }
            let Some(record) = self.commit_by_id(&id).await? else {
                continue;
            };
            for parent in record.merge_parents.iter().rev() {
                stack.push(parent.clone());
            }
            if let Some(parent) = &record.parent {
                stack.push(parent.clone());
            }
            records.push(record);
        }
        Ok(records)
    }

    async fn node_identity(&self, node_id: &str) -> Result<Option<NodeIdentity>> {
        let id = sanitize_node_id(node_id)?;
        let rows = self
            .client
            .raw_query(
                &format!("SELECT raw, sync_key FROM temporal_node:`{id}`;"),
                QueryParams::new(),
            )
            .await
            .context("reading stored node identity")?;
        let Some(row) = rows.first() else {
            return Ok(None);
        };
        Ok(Some(NodeIdentity {
            sync_key: optional_string(row.get("sync_key")).unwrap_or_default(),
            raw: optional_string(row.get("raw")).unwrap_or_default(),
        }))
    }

    async fn commit_by_id(&self, id: &str) -> Result<Option<CommitRecord>> {
        let rows = self
            .client
            .raw_query(
                &format!("SELECT * FROM mori_commit:`{id}`;"),
                QueryParams::new(),
            )
            .await
            .context("reading commit")?;
        match rows.first() {
            Some(row) => Ok(Some(commit_from_row(row)?)),
            None => Ok(None),
        }
    }
}

#[derive(Debug, Clone)]
pub struct BranchInfo {
    pub name: String,
    pub commit_id: Option<String>,
    pub current: bool,
}

struct BranchRow {
    name: String,
    commit_id: Option<String>,
}

#[derive(Default)]
struct VisibleNodes {
    sync_keys: HashSet<String>,
    raws: HashSet<String>,
}

impl VisibleNodes {
    fn is_empty(&self) -> bool {
        self.sync_keys.is_empty() && self.raws.is_empty()
    }

    fn matches(&self, sync_key: &str, raw: &str) -> bool {
        (!sync_key.is_empty() && self.sync_keys.contains(sync_key))
            || (!raw.is_empty() && self.raws.contains(raw))
    }
}

struct NodeIdentity {
    sync_key: String,
    raw: String,
}

fn scan_limit(limit: usize) -> usize {
    limit.saturating_mul(25).clamp(64, 2000)
}

fn validate_branch_name(name: &str) -> Result<&str> {
    let name = name.trim();
    if name.eq_ignore_ascii_case("head") {
        bail!("HEAD is reserved");
    }
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        bail!("branch name cannot be empty");
    };
    let rest_ok = chars.all(|ch| ch.is_ascii_alphanumeric() || ch == '.' || ch == '_' || ch == '-');
    if name.len() > 64 || !first.is_ascii_alphanumeric() || !rest_ok {
        bail!("branch names use letters, digits, '.', '_' and '-'");
    }
    Ok(name)
}

fn sanitize_node_id(node_id: &str) -> Result<String> {
    let id = node_id
        .trim()
        .trim_start_matches("temporal_node:")
        .replace('`', "");
    if id.is_empty() || id.contains(';') || id.contains(' ') {
        bail!("invalid node id");
    }
    Ok(id)
}

fn optional_string(value: Option<&Value>) -> Option<String> {
    value.and_then(Value::as_str).and_then(|text| {
        let trimmed = text.trim();
        if trimmed.is_empty() || trimmed == "null" {
            None
        } else {
            Some(trimmed.to_string())
        }
    })
}

fn branch_from_row(row: &Value) -> Option<BranchRow> {
    let name = optional_string(row.get("name")).or_else(|| {
        optional_string(row.get("id")).and_then(|id| {
            id.rsplit([':', '`'])
                .find(|part| !part.is_empty() && *part != "mori_branch")
                .map(str::to_string)
        })
    })?;
    Some(BranchRow {
        name,
        commit_id: optional_string(row.get("commit_id")),
    })
}

fn scope_for(session: Option<&str>) -> MemoryScope {
    MemoryScope {
        session_ids: session
            .map(str::trim)
            .filter(|value| !value.is_empty())
            .map(|value| vec![value.to_string()]),
        ..MemoryScope::default()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitEntry {
    pub node_id: String,
    pub kind: ContextKind,
    pub session: String,
    pub source: String,
    pub summary: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitRecord {
    pub id: String,
    /// First parent. Ordinary commits have only this one.
    pub parent: Option<String>,
    /// Extra parents written by merge. Empty on commits that are not merges.
    #[serde(default)]
    pub merge_parents: Vec<String>,
    pub message: String,
    pub session: String,
    pub created_at: DateTime<Utc>,
    pub entries: Vec<CommitEntry>,
}

#[derive(Debug, Clone)]
pub enum MergeOutcome {
    UpToDate,
    FastForward { commit_id: String },
    Merged { commit: CommitRecord },
}

#[derive(Debug, Clone)]
pub enum RebaseOutcome {
    UpToDate,
    FastForward {
        commit_id: String,
    },
    Rebased {
        commits: Vec<CommitRecord>,
        tip: String,
    },
}

#[derive(Debug, Clone)]
pub struct Recalled {
    pub session: String,
    pub summary: String,
    pub raw_sttp: String,
    pub path: String,
}

fn commit_from_row(row: &Value) -> Result<CommitRecord> {
    let id = row
        .get("commit_id")
        .and_then(Value::as_str)
        .map(str::to_string)
        .ok_or_else(|| anyhow!("commit row is missing commit_id"))?;
    let created_at = row
        .get("created_at")
        .and_then(Value::as_str)
        .and_then(|value| DateTime::parse_from_rfc3339(value).ok())
        .map(|value| value.with_timezone(&Utc))
        .unwrap_or_else(Utc::now);
    let entries = match row.get("entries") {
        Some(value) => serde_json::from_value(value.clone()).unwrap_or_default(),
        None => Vec::new(),
    };
    let parent = row.get("parent").and_then(|value| match value {
        Value::String(text) if !text.is_empty() && text != "null" => Some(text.clone()),
        _ => None,
    });
    let merge_parents = row
        .get("merge_parents")
        .and_then(Value::as_array)
        .map(|items| {
            items
                .iter()
                .filter_map(|value| match value {
                    Value::String(text) if !text.is_empty() && text != "null" => Some(text.clone()),
                    _ => None,
                })
                .collect()
        })
        .unwrap_or_default();
    Ok(CommitRecord {
        id,
        parent,
        merge_parents,
        message: row
            .get("message")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string(),
        session: row
            .get("session")
            .and_then(Value::as_str)
            .unwrap_or("main")
            .to_string(),
        created_at,
        entries,
    })
}