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
use std::path::PathBuf;
use std::process::ExitCode;

use anyhow::{bail, Context, Result};
use chrono::Utc;
use clap::{Parser, Subcommand, ValueEnum};
use mori::{
    compile_context, detect_kind, find_repo, init_repo, read_source, ContextKind, Memory,
    MergeOutcome, RebaseOutcome, Repo, StageIndex, StagedContext, Stash,
};
use uuid::Uuid;

#[derive(Parser)]
#[command(
    name = "mori",
    version,
    about = "the git for cognition",
    arg_required_else_help = true
)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Clone, Copy, ValueEnum)]
enum KindArg {
    Auto,
    Sttp,
    Document,
    Chat,
    Note,
    Context,
}

impl From<KindArg> for ContextKind {
    fn from(value: KindArg) -> Self {
        match value {
            KindArg::Auto => ContextKind::Auto,
            KindArg::Sttp => ContextKind::Sttp,
            KindArg::Document => ContextKind::Document,
            KindArg::Chat => ContextKind::Chat,
            KindArg::Note => ContextKind::Note,
            KindArg::Context => ContextKind::Context,
        }
    }
}

#[derive(Subcommand)]
enum Command {
    /// Create a cognition repo in this directory.
    Init {
        /// Directory to initialize. Defaults to the current directory.
        path: Option<PathBuf>,
        /// Default Locus session label stored on new context.
        #[arg(long, default_value = "main")]
        session: String,
    },
    /// Stage a document, chat, note, or raw STTP file.
    Add {
        /// Files to stage. Use `-` to read stdin.
        sources: Vec<String>,
        #[arg(long, value_enum, default_value = "auto")]
        kind: KindArg,
        #[arg(long)]
        session: Option<String>,
    },
    /// Show the branch, HEAD, staged context, and stash.
    Status,
    /// List branches, or create one at the current tip.
    Branch {
        /// New branch name. Omit it to list branches.
        name: Option<String>,
    },
    /// Check out a branch. Refuses when context is staged.
    Checkout {
        name: String,
        /// Create the branch at the current tip, then check it out.
        #[arg(short = 'b')]
        branch: bool,
    },
    /// Bring another branch into the current one.
    Merge {
        branch: String,
        /// Message for a merge commit. Fast-forwards do not use it.
        #[arg(short, long)]
        message: Option<String>,
    },
    /// Replay the current branch's commits onto another branch.
    Rebase {
        /// Branch to replay onto. That branch is left where it is.
        onto: String,
    },
    /// Park staged context so you can check out another branch.
    Stash {
        /// Message recorded on the stash. Used when parking context.
        #[arg(short, long)]
        message: Option<String>,
        #[command(subcommand)]
        action: Option<StashCommand>,
    },
    /// Compile context into STTP and print it. Does not open the database.
    Compile {
        /// Files to compile. With no paths, compile whatever is staged.
        sources: Vec<String>,
        #[arg(long, value_enum, default_value = "auto")]
        kind: KindArg,
        #[arg(long)]
        session: Option<String>,
    },
    /// Compile staged context and store it in this repo's SurrealKV file.
    Commit {
        #[arg(short, long)]
        message: String,
    },
    /// List stored commits, newest first.
    Log {
        #[arg(short = 'n', long, default_value_t = 20)]
        limit: usize,
    },
    /// Show one commit. Pass `--raw` to print the stored STTP.
    Show {
        rev: String,
        #[arg(long)]
        raw: bool,
    },
    /// Rank stored context against a question.
    Recall {
        query: String,
        #[arg(long)]
        session: Option<String>,
        #[arg(long, default_value_t = 8)]
        limit: usize,
        #[arg(long)]
        raw: bool,
    },
    /// Filter stored context without ranking.
    Find {
        #[arg(long)]
        session: Option<String>,
        #[arg(long)]
        contains: Option<String>,
        #[arg(long, default_value_t = 20)]
        limit: usize,
    },
    /// Unstage context. With no paths, unstage everything.
    Reset { sources: Vec<String> },
}

#[derive(Subcommand)]
enum StashCommand {
    /// Show parked context, newest first.
    List,
    /// Restore the newest stash onto an empty index.
    Pop,
    /// Drop the newest stash.
    Drop,
}

#[tokio::main]
async fn main() -> ExitCode {
    match run().await {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => {
            eprintln!("mori: {err:#}");
            ExitCode::FAILURE
        }
    }
}

async fn run() -> Result<()> {
    let cli = Cli::parse();
    match cli.command {
        Command::Init { path, session } => cmd_init(path, &session).await,
        Command::Add {
            sources,
            kind,
            session,
        } => cmd_add(sources, kind.into(), session),
        Command::Status => cmd_status().await,
        Command::Branch { name } => cmd_branch(name).await,
        Command::Checkout { name, branch } => cmd_checkout(&name, branch).await,
        Command::Merge { branch, message } => cmd_merge(&branch, message.as_deref()).await,
        Command::Rebase { onto } => cmd_rebase(&onto).await,
        Command::Stash { message, action } => cmd_stash(message, action).await,
        Command::Compile {
            sources,
            kind,
            session,
        } => cmd_compile(sources, kind.into(), session),
        Command::Commit { message } => cmd_commit(&message).await,
        Command::Log { limit } => cmd_log(limit).await,
        Command::Show { rev, raw } => cmd_show(&rev, raw).await,
        Command::Recall {
            query,
            session,
            limit,
            raw,
        } => cmd_recall(&query, session.as_deref(), limit, raw).await,
        Command::Find {
            session,
            contains,
            limit,
        } => cmd_find(session.as_deref(), contains.as_deref(), limit).await,
        Command::Reset { sources } => cmd_reset(sources),
    }
}

async fn cmd_init(path: Option<PathBuf>, session: &str) -> Result<()> {
    let path = path.unwrap_or(std::env::current_dir()?);
    let repo = init_repo(&path, session)?;
    let memory = Memory::connect(&repo).await?;
    let endpoint = memory.endpoint().to_string();
    memory.disconnect();
    println!(
        "initialized empty mori repository in {}",
        repo.mori_dir().display()
    );
    println!("branch main");
    println!("session {session}");
    println!("store {endpoint}");
    Ok(())
}

fn cmd_add(sources: Vec<String>, kind: ContextKind, session: Option<String>) -> Result<()> {
    if sources.is_empty() {
        bail!("nothing specified, nothing added");
    }
    let repo = repo_from_cwd()?;
    let config = repo.load_config()?;
    let session = session.unwrap_or(config.default_session);
    let mut index = StageIndex::load(&repo)?;
    for source in sources {
        let text = read_source(&source)?;
        let resolved = match kind {
            ContextKind::Auto => detect_kind(&source, &text),
            other => other,
        };
        index.upsert(StagedContext {
            id: Uuid::new_v4().to_string(),
            kind: resolved,
            session: session.clone(),
            source,
            text,
            added_at: Utc::now(),
        });
    }
    let staged = index.entries().to_vec();
    index.save(&repo)?;
    for entry in staged {
        println!("staged {} {}", entry.kind.as_str(), entry.source);
    }
    Ok(())
}

async fn cmd_status() -> Result<()> {
    let repo = repo_from_cwd()?;
    let config = repo.load_config()?;
    let index = StageIndex::load(&repo)?;
    println!("session {}", config.default_session);

    if repo.kv_dir().exists() {
        let memory = Memory::connect(&repo).await?;
        let branch = memory.current_branch().await?;
        println!("On branch {branch}");
        match memory.head().await? {
            Some(head) => println!("HEAD {} {}", short(&head.id), head.message),
            None => println!("HEAD (no commits yet)"),
        }
        memory.disconnect();
    } else {
        println!("On branch main");
        println!("HEAD (no commits yet)");
    }

    let stash = Stash::load(&repo)?;
    if !stash.is_empty() {
        println!(
            "stash: {} {}",
            stash.entries().len(),
            if stash.entries().len() == 1 {
                "entry"
            } else {
                "entries"
            }
        );
    }

    if index.is_empty() {
        println!("nothing staged");
    } else {
        println!("staged context:");
        for entry in index.entries() {
            println!(
                "  {:<10} {}  ({})",
                entry.kind.as_str(),
                entry.source,
                entry.session
            );
        }
    }
    Ok(())
}

fn cmd_compile(sources: Vec<String>, kind: ContextKind, session: Option<String>) -> Result<()> {
    let staged = if sources.is_empty() {
        let repo = repo_from_cwd()?;
        StageIndex::load(&repo)?.entries().to_vec()
    } else {
        let session = match session {
            Some(session) => session,
            None => match find_repo(&std::env::current_dir()?) {
                Ok(repo) => repo.load_config()?.default_session,
                Err(_) => "main".to_string(),
            },
        };
        sources
            .into_iter()
            .map(|source| {
                let text = read_source(&source)?;
                Ok(StagedContext {
                    id: Uuid::new_v4().to_string(),
                    kind,
                    session: session.clone(),
                    source,
                    text,
                    added_at: Utc::now(),
                })
            })
            .collect::<Result<Vec<_>>>()?
    };

    if staged.is_empty() {
        bail!("nothing to compile");
    }

    for entry in &staged {
        let compiled = compile_context(entry)?;
        eprintln!(
            "compiled {} {} (session {})",
            compiled.kind.as_str(),
            compiled.source,
            compiled.session
        );
        println!("{}", compiled.raw_sttp);
    }
    Ok(())
}

async fn cmd_commit(message: &str) -> Result<()> {
    let repo = repo_from_cwd()?;
    let config = repo.load_config()?;
    let index = StageIndex::load(&repo)?;
    if index.is_empty() {
        bail!("nothing staged");
    }
    let memory = Memory::connect(&repo).await?;
    let branch = memory.current_branch().await?;
    let record = memory
        .commit(message, &config.default_session, index.entries())
        .await?;
    memory.disconnect();

    let mut cleared = index;
    cleared.clear();
    cleared.save(&repo)?;

    println!("[{} {}] {}", branch, short(&record.id), record.message);
    println!(" {} context stored", record.entries.len());
    for entry in &record.entries {
        println!("  {} {}", entry.kind.as_str(), entry.source);
    }
    Ok(())
}

async fn cmd_log(limit: usize) -> Result<()> {
    let repo = repo_from_cwd()?;
    let memory = Memory::connect(&repo).await?;
    let branch = memory.current_branch().await?;
    let records = memory.log(limit).await?;
    memory.disconnect();
    println!("branch {branch}");
    if records.is_empty() {
        println!("no commits yet");
        return Ok(());
    }
    for record in records {
        println!("commit {}", record.id);
        if !record.merge_parents.is_empty() {
            let mut parents = Vec::new();
            if let Some(parent) = &record.parent {
                parents.push(short(parent).to_string());
            }
            for parent in &record.merge_parents {
                parents.push(short(parent).to_string());
            }
            println!("Merge: {}", parents.join(" "));
        }
        println!("session {}", record.session);
        println!("Date: {}", record.created_at.to_rfc3339());
        println!();
        println!("    {}", record.message);
        println!();
        for entry in &record.entries {
            println!("    {} {}", entry.kind.as_str(), entry.source);
            println!("    {}", entry.summary);
        }
        println!();
    }
    Ok(())
}

async fn cmd_show(rev: &str, raw: bool) -> Result<()> {
    let repo = repo_from_cwd()?;
    let memory = Memory::connect(&repo).await?;
    let record = memory.find_commit(rev).await?;
    println!("commit {}", record.id);
    println!("session {}", record.session);
    println!("Date: {}", record.created_at.to_rfc3339());
    println!();
    println!("    {}", record.message);
    println!();
    for entry in &record.entries {
        println!("    {} {}", entry.kind.as_str(), entry.source);
        println!("    {}", entry.summary);
        if raw {
            let text = memory.node_raw(&entry.node_id).await?;
            println!();
            println!("{text}");
            println!();
        }
    }
    memory.disconnect();
    Ok(())
}

async fn cmd_recall(query: &str, session: Option<&str>, limit: usize, raw: bool) -> Result<()> {
    let repo = repo_from_cwd()?;
    let memory = Memory::connect(&repo).await?;
    let hits = memory.recall(query, session, limit).await?;
    memory.disconnect();
    if hits.is_empty() {
        println!("nothing recalled");
        return Ok(());
    }
    let path = hits
        .first()
        .map(|hit| hit.path.as_str())
        .unwrap_or("recall");
    println!("retrieved {} via {path}", hits.len());
    for hit in hits {
        println!("{}", hit.summary);
        if raw {
            println!("{}", hit.raw_sttp);
        }
    }
    Ok(())
}

async fn cmd_find(session: Option<&str>, contains: Option<&str>, limit: usize) -> Result<()> {
    let repo = repo_from_cwd()?;
    let memory = Memory::connect(&repo).await?;
    let hits = memory.find(session, contains, limit).await?;
    memory.disconnect();
    if hits.is_empty() {
        println!("nothing found");
        return Ok(());
    }
    println!("found {}", hits.len());
    for hit in hits {
        println!("{}", hit.summary);
    }
    Ok(())
}

async fn cmd_branch(name: Option<String>) -> Result<()> {
    let repo = repo_from_cwd()?;
    let memory = Memory::connect(&repo).await?;
    if let Some(name) = name {
        let created = memory.create_branch(&name).await?;
        memory.disconnect();
        println!("created branch {created}");
        return Ok(());
    }

    let branches = memory.branches().await?;
    memory.disconnect();
    if branches.is_empty() {
        println!("no branches yet");
        return Ok(());
    }
    for branch in branches {
        let marker = if branch.current { "*" } else { " " };
        match branch.commit_id.as_deref() {
            Some(id) => println!("{marker} {} {}", branch.name, short(id)),
            None => println!("{marker} {}", branch.name),
        }
    }
    Ok(())
}

async fn cmd_checkout(name: &str, create: bool) -> Result<()> {
    let repo = repo_from_cwd()?;
    let index = StageIndex::load(&repo)?;
    let memory = Memory::connect(&repo).await?;
    let current = memory.current_branch().await?;
    if current != name && !index.is_empty() {
        bail!(
            "staged context is in the way; stash or commit it before checking out another branch"
        );
    }
    if create {
        memory.create_branch(name).await?;
    }
    memory.checkout_branch(name).await?;
    memory.disconnect();
    if create {
        println!("checked out new branch {name}");
    } else {
        println!("checked out branch {name}");
    }
    Ok(())
}

async fn cmd_merge(branch: &str, message: Option<&str>) -> Result<()> {
    let repo = repo_from_cwd()?;
    refuse_dirty(&repo, "merging")?;
    let config = repo.load_config()?;
    let memory = Memory::connect(&repo).await?;
    let current = memory.current_branch().await?;
    let outcome = memory
        .merge(branch, message, &config.default_session)
        .await?;
    memory.disconnect();
    match outcome {
        MergeOutcome::UpToDate => println!("already up to date"),
        MergeOutcome::FastForward { commit_id } => {
            println!("fast-forward to {}", short(&commit_id));
        }
        MergeOutcome::Merged { commit } => {
            println!("[{current} {}] {}", short(&commit.id), commit.message);
        }
    }
    Ok(())
}

async fn cmd_rebase(onto: &str) -> Result<()> {
    let repo = repo_from_cwd()?;
    refuse_dirty(&repo, "rebasing")?;
    let memory = Memory::connect(&repo).await?;
    let outcome = memory.rebase(onto).await?;
    memory.disconnect();
    match outcome {
        RebaseOutcome::UpToDate => println!("already up to date"),
        RebaseOutcome::FastForward { commit_id } => {
            println!("fast-forward to {}", short(&commit_id));
        }
        RebaseOutcome::Rebased { commits, tip } => {
            println!("rebased {} onto {onto}", commits.len());
            for commit in &commits {
                println!("  {} {}", short(&commit.id), commit.message);
            }
            println!("HEAD {}", short(&tip));
        }
    }
    Ok(())
}

fn refuse_dirty(repo: &Repo, action: &str) -> Result<()> {
    if StageIndex::load(repo)?.is_empty() {
        Ok(())
    } else {
        bail!("staged context is in the way; stash or commit it before {action}")
    }
}

async fn cmd_stash(message: Option<String>, action: Option<StashCommand>) -> Result<()> {
    match action {
        Some(StashCommand::List) => cmd_stash_list(),
        Some(StashCommand::Pop) => cmd_stash_pop(),
        Some(StashCommand::Drop) => cmd_stash_drop(),
        None => cmd_stash_push(message.as_deref()).await,
    }
}

async fn cmd_stash_push(message: Option<&str>) -> Result<()> {
    let repo = repo_from_cwd()?;
    let mut index = StageIndex::load(&repo)?;
    if index.is_empty() {
        bail!("nothing staged");
    }
    let memory = Memory::connect(&repo).await?;
    let branch = memory.current_branch().await?;
    memory.disconnect();

    let mut stash = Stash::load(&repo)?;
    let entry = stash
        .push(&branch, message.unwrap_or(""), index.entries().to_vec())
        .clone();
    stash.save(&repo)?;
    index.clear();
    index.save(&repo)?;
    println!("stashed {}: {}", short(&entry.id), entry.message);
    Ok(())
}

fn cmd_stash_list() -> Result<()> {
    let repo = repo_from_cwd()?;
    let stash = Stash::load(&repo)?;
    if stash.is_empty() {
        println!("nothing stashed");
        return Ok(());
    }
    for entry in stash.entries() {
        println!(
            "stash {}: {} (branch {})",
            short(&entry.id),
            entry.message,
            entry.branch
        );
    }
    Ok(())
}

fn cmd_stash_pop() -> Result<()> {
    let repo = repo_from_cwd()?;
    let mut index = StageIndex::load(&repo)?;
    if !index.is_empty() {
        bail!("staged context is in the way; commit, reset, or stash it before popping");
    }
    let mut stash = Stash::load(&repo)?;
    let entry = stash.pop()?;
    index.replace(entry.entries);
    index.save(&repo)?;
    stash.save(&repo)?;
    println!(
        "restored stash {}: {} (branch {})",
        short(&entry.id),
        entry.message,
        entry.branch
    );
    Ok(())
}

fn cmd_stash_drop() -> Result<()> {
    let repo = repo_from_cwd()?;
    let mut stash = Stash::load(&repo)?;
    let entry = stash.pop()?;
    stash.save(&repo)?;
    println!("dropped stash {}: {}", short(&entry.id), entry.message);
    Ok(())
}

fn cmd_reset(sources: Vec<String>) -> Result<()> {
    let repo = repo_from_cwd()?;
    let mut index = StageIndex::load(&repo)?;
    if sources.is_empty() {
        index.clear();
        index.save(&repo)?;
        println!("unstaged all context");
        return Ok(());
    }
    for source in &sources {
        if !index.unstage(source) {
            bail!("{source} is not staged");
        }
        println!("unstaged {source}");
    }
    index.save(&repo)?;
    Ok(())
}

fn repo_from_cwd() -> Result<Repo> {
    find_repo(&std::env::current_dir().context("current directory")?)
}

fn short(id: &str) -> &str {
    let end = id.chars().take(12).map(|ch| ch.len_utf8()).sum();
    &id[..end]
}