scone-cli 0.2.0

Scone: a local-first temporal memory engine — CLI, MCP server, and HTTP API
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
//! `scone` — the CLI face of the Scone memory engine.
//!
//! Thin by design: every capability lives in `scone-core`; this binary
//! parses arguments, prints, and sets the exit code.

use std::path::PathBuf;

use clap::{Parser, Subcommand, ValueEnum};
use scone_core::embed::{EmbeddingProvider, HashEmbedder};
use scone_core::llm::{AnthropicProvider, ExtractedFact, FakeLlm, LlmProvider, OpenAiCompatible};
use scone_core::{Engine, IngestInput, IngestOutcome, RecallOpts, auth};

const HASH_EMBEDDER_DIM: usize = 256;

#[derive(Parser)]
#[command(
    name = "scone",
    about = "A local-first temporal memory engine",
    version
)]
struct Cli {
    /// Data directory (default: ~/.scone)
    #[arg(long, global = true)]
    data_dir: Option<PathBuf>,
    /// Space to operate in
    #[arg(long, global = true, default_value = "default")]
    space: String,
    /// Embedding provider: local ONNX model (default) or the model-free
    /// deterministic hash embedder
    #[arg(long, global = true, value_enum, default_value_t = EmbedderKind::Local)]
    embedder: EmbedderKind,
    /// LLM for the semantic lane: none (paused) or fake (testing hook
    /// reading SCONE_FAKE_FACTS as a JSON fact array)
    #[arg(long, global = true, value_enum, default_value_t = LlmKind::Config)]
    llm: LlmKind,
    /// Attach the local cross-encoder reranker for higher recall precision
    /// (downloads bge-reranker-base once into the data dir)
    #[arg(long, global = true)]
    reranker: bool,
    #[command(subcommand)]
    cmd: Cmd,
}

#[derive(Subcommand)]
enum Cmd {
    /// Ingest files, or a note via --note
    Add {
        /// Files to ingest
        paths: Vec<PathBuf>,
        /// Ingest this text directly as a note
        #[arg(long)]
        note: Option<String>,
        /// Tag the ingested content (repeatable); files also get their
        /// extension as a source tag
        #[arg(long = "tag")]
        tags: Vec<String>,
    },
    /// Hybrid search within the space
    Search {
        query: String,
        #[arg(long, default_value_t = 10)]
        limit: usize,
        /// Evaluate fact validity at this ISO-8601 instant (time travel)
        #[arg(long)]
        as_of: Option<String>,
        /// Focus on episodes carrying ALL of these tags (repeatable)
        #[arg(long = "tag")]
        tags: Vec<String>,
    },
    /// Register scone as a memory server, zero questions asked
    Setup {
        /// claude-code or claude-desktop
        client: String,
    },
    /// List tags in this space with usage counts
    Tags,
    /// Show the space's profile: identity facts and recent activity
    Profile {
        #[arg(long, default_value_t = 8)]
        limit: usize,
    },
    /// Show stores, counts, and index health
    Status,
    /// Scan a directory into memory, repeatedly or once
    Watch {
        /// Directory to scan
        dir: std::path::PathBuf,
        /// Scan once and exit (default: rescan every --interval-secs)
        #[arg(long)]
        once: bool,
        #[arg(long, default_value_t = 60)]
        interval_secs: u64,
        /// Tag everything ingested by this watch (repeatable)
        #[arg(long = "tag")]
        tags: Vec<String>,
    },
    /// Background loop: scan watch dirs and distill pending episodes
    Daemon {
        /// Directories to scan each cycle
        #[arg(long)]
        watch: Vec<std::path::PathBuf>,
        /// Run one cycle and exit
        #[arg(long)]
        once: bool,
        #[arg(long, default_value_t = 300)]
        interval_secs: u64,
    },
    /// Export the space as JSONL to stdout (episodes, aliases, facts)
    Export,
    /// Import a JSONL export into the space (idempotent)
    Import {
        /// Path to a JSONL file, or - for stdin
        path: std::path::PathBuf,
    },
    /// List spaces with their episode counts
    Spaces,
    /// Serve persistent agent memory over MCP (stdio)
    Mcp,
    /// Serve the multi-user HTTP API (keys from config.toml [[server.keys]])
    Serve {
        /// Listen address (overrides config [server].listen)
        #[arg(long)]
        listen: Option<String>,
    },
    /// Verify and repair the derived indexes
    Doctor {
        /// Rebuild all indexes from SQLite truth
        #[arg(long)]
        rebuild: bool,
    },
    /// Ask a question against your memory (recall + optional LLM answer)
    Ask {
        question: String,
        #[arg(long, default_value_t = 10)]
        limit: usize,
    },
    /// Distill pending episodes into temporal facts (needs --llm)
    Distill {
        #[arg(long, default_value_t = 100)]
        limit: usize,
    },
    /// Inspect the semantic store
    Facts {
        #[command(subcommand)]
        action: FactsCmd,
    },
}

#[derive(Subcommand)]
enum FactsCmd {
    /// List facts (active only; --all includes closed with reasons)
    List {
        #[arg(long)]
        all: bool,
    },
    /// Show which episodes taught us a fact
    Why { id: i64 },
    /// Close a fact by hand with a reason (never deletes)
    Close {
        id: i64,
        #[arg(long)]
        reason: String,
    },
}

#[derive(Clone, Copy, ValueEnum)]
enum LlmKind {
    /// Read [llm] from <data-dir>/config.toml (absent file = none)
    Config,
    None,
    Fake,
}

fn llm_from_config(dir: &std::path::Path) -> Result<Option<Box<dyn LlmProvider>>, String> {
    let path = dir.join("config.toml");
    let Ok(raw) = std::fs::read_to_string(&path) else {
        return Ok(None);
    };
    let table: toml::Table = raw
        .parse()
        .map_err(|e| format!("{}: {e}", path.display()))?;
    let Some(llm) = table.get("llm") else {
        return Ok(None);
    };
    let get = |key: &str| llm.get(key).and_then(|v| v.as_str()).map(str::to_owned);
    let provider = get("provider").unwrap_or_else(|| "none".into());
    let api_key = match get("api_key_env") {
        Some(env_name) => Some(std::env::var(&env_name).map_err(|_| {
            format!("config names api_key_env = {env_name}, but that variable is not set")
        })?),
        None => None,
    };
    let model = get("model");
    let need_model = || {
        model
            .clone()
            .ok_or_else(|| "config [llm] needs model".to_owned())
    };
    match provider.as_str() {
        "none" => Ok(None),
        "ollama" => Ok(Some(Box::new(OpenAiCompatible::new(
            &get("base_url").unwrap_or_else(|| "http://localhost:11434/v1".into()),
            &need_model()?,
            api_key,
        )))),
        "openai" => Ok(Some(Box::new(OpenAiCompatible::new(
            &get("base_url").unwrap_or_else(|| "https://api.openai.com/v1".into()),
            &need_model()?,
            api_key,
        )))),
        "anthropic" => Ok(Some(Box::new(AnthropicProvider::new(
            &get("base_url").unwrap_or_else(|| "https://api.anthropic.com".into()),
            &need_model()?,
            &api_key.ok_or("anthropic needs api_key_env in config")?,
        )))),
        other => Err(format!("unknown llm provider {other:?} in config")),
    }
}

fn make_llm(kind: LlmKind, dir: &std::path::Path) -> Result<Option<Box<dyn LlmProvider>>, String> {
    match kind {
        LlmKind::Config => llm_from_config(dir),
        LlmKind::None => Ok(None),
        LlmKind::Fake => {
            let raw = std::env::var("SCONE_FAKE_FACTS").unwrap_or_else(|_| "[]".into());
            let parsed: serde_json::Value =
                serde_json::from_str(&raw).map_err(|e| format!("SCONE_FAKE_FACTS: {e}"))?;
            let facts = parsed
                .as_array()
                .ok_or("SCONE_FAKE_FACTS must be a JSON array")?
                .iter()
                .map(|f| {
                    Ok(ExtractedFact {
                        subject: f["subject"]
                            .as_str()
                            .ok_or("fact needs subject")?
                            .to_owned(),
                        predicate: f["predicate"]
                            .as_str()
                            .ok_or("fact needs predicate")?
                            .to_owned(),
                        object: f["object"].as_str().ok_or("fact needs object")?.to_owned(),
                        confidence: f["confidence"].as_f64().unwrap_or(0.8) as f32,
                    })
                })
                .collect::<Result<Vec<_>, String>>()?;
            Ok(Some(Box::new(FakeLlm::new(facts))))
        }
    }
}

#[derive(Clone, Copy, ValueEnum)]
enum EmbedderKind {
    Local,
    Hash,
}

fn make_embedder(
    kind: EmbedderKind,
    dir: &std::path::Path,
) -> Result<Box<dyn EmbeddingProvider>, String> {
    match kind {
        EmbedderKind::Hash => Ok(Box::new(HashEmbedder::new(HASH_EMBEDDER_DIM))),
        #[cfg(feature = "local-embed")]
        EmbedderKind::Local => Ok(Box::new(
            scone_core::embed::OnnxEmbedder::new(&dir.join("models")).map_err(|e| e.to_string())?,
        )),
        #[cfg(not(feature = "local-embed"))]
        EmbedderKind::Local => {
            let _ = dir;
            Err("this build lacks the local-embed feature; use --embedder hash".into())
        }
    }
}

fn main() {
    if let Err(e) = run() {
        eprintln!("error: {e}");
        std::process::exit(1);
    }
}

fn data_dir(cli: &Cli) -> Result<PathBuf, String> {
    if let Some(d) = &cli.data_dir {
        return Ok(d.clone());
    }
    std::env::var_os("HOME")
        .map(|h| PathBuf::from(h).join(".scone"))
        .ok_or_else(|| "cannot resolve home directory; pass --data-dir".to_owned())
}

fn run() -> Result<(), String> {
    let cli = Cli::parse();
    let dir = data_dir(&cli)?;
    let embedder = make_embedder(cli.embedder, &dir)?;
    let repair = matches!(cli.cmd, Cmd::Doctor { rebuild: true });
    let mut engine = if repair {
        Engine::open_for_repair(&dir, embedder).map_err(|e| e.to_string())?
    } else {
        Engine::open(&dir, embedder).map_err(|e| e.to_string())?
    };
    engine.set_llm(make_llm(cli.llm, &dir)?);
    #[cfg(feature = "local-embed")]
    if cli.reranker {
        engine.set_reranker(Some(Box::new(
            scone_core::rerank::OnnxReranker::new(&dir.join("models"))
                .map_err(|e| e.to_string())?,
        )));
    }

    match &cli.cmd {
        Cmd::Add { paths, note, tags } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let mut inputs = Vec::new();
            if let Some(text) = note {
                inputs.push(IngestInput::Note { text: text.clone() });
            }
            for path in paths {
                inputs.push(IngestInput::File { path: path.clone() });
            }
            if inputs.is_empty() {
                return Err("nothing to add: pass file paths or --note".into());
            }
            for input in inputs {
                let label = match &input {
                    IngestInput::Note { .. } => "note".to_owned(),
                    IngestInput::File { path } => path.display().to_string(),
                };
                let is_file = matches!(&input, IngestInput::File { .. });
                let outcome = engine.ingest(&space, input).map_err(|e| e.to_string())?;
                let episode_id = match outcome {
                    IngestOutcome::Ingested { episode_id, chunks } => {
                        println!("ingested {label} as episode {episode_id} ({chunks} chunks)");
                        episode_id
                    }
                    IngestOutcome::Deduplicated { episode_id } => {
                        println!("deduplicated {label}: already stored as episode {episode_id}");
                        episode_id
                    }
                };
                let mut all: Vec<String> = tags.clone();
                if is_file
                    && let Some(ext) = std::path::Path::new(&label)
                        .extension()
                        .map(|e| e.to_string_lossy().to_lowercase())
                {
                    all.push(ext);
                }
                if !all.is_empty() {
                    let refs: Vec<&str> = all.iter().map(String::as_str).collect();
                    engine
                        .tag_episode(&space, episode_id, &refs)
                        .map_err(|e| e.to_string())?;
                }
            }
        }
        Cmd::Search {
            query,
            limit,
            as_of,
            tags,
        } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let opts = RecallOpts {
                limit: *limit,
                budget_bytes: None,
                as_of: as_of.clone(),
                tags: tags.clone(),
                ..Default::default()
            };
            let pack = engine
                .recall(&space, query, &opts)
                .map_err(|e| e.to_string())?;
            for warning in &pack.degraded {
                eprintln!("degraded: {warning}");
            }
            for f in &pack.facts {
                println!(
                    "fact  {} {} {}  (conf {:.2}, {})",
                    f.subject, f.predicate, f.object, f.confidence, f.status
                );
            }
            if pack.items.is_empty() && pack.facts.is_empty() {
                println!("no results");
            } else {
                println!(
                    "context: {} bytes of {} stored ({:.1}% saved)",
                    pack.returned_bytes,
                    pack.space_bytes,
                    pack.context_reduction() * 100.0
                );
            }
            for item in &pack.items {
                let text: String = item.text.chars().take(120).collect();
                let text = text.replace('\n', " ");
                let source = item.source.as_deref().unwrap_or("note");
                println!(
                    "{:.3}  [{}] {}  {}",
                    item.score, item.episode_id, source, text
                );
            }
        }
        Cmd::Setup { client } => {
            let message = match client.as_str() {
                "claude-code" => scone::setup::setup_claude_code(&cli.space)?,
                "claude-desktop" => scone::setup::setup_claude_desktop(&cli.space)?,
                other => {
                    return Err(format!(
                        "unknown client {other:?}: use claude-code or claude-desktop"
                    ));
                }
            };
            println!("{message}");
        }
        Cmd::Tags => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let tags = engine.tags_list(&space).map_err(|e| e.to_string())?;
            if tags.is_empty() {
                println!("no tags yet: add with `scone add --tag <name>`");
            }
            for (name, count) in tags {
                println!("{name}  ({count})");
            }
        }
        Cmd::Profile { limit } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let profile = engine.profile(&space, *limit).map_err(|e| e.to_string())?;
            if !profile.static_facts.is_empty() {
                println!("profile:");
                for f in &profile.static_facts {
                    println!(
                        "  {} {} {}  (conf {:.2})",
                        f.subject, f.predicate, f.object, f.confidence
                    );
                }
            }
            if !profile.dynamic.is_empty() {
                println!("recent:");
                for d in &profile.dynamic {
                    println!("  {}", d.replace('\n', " "));
                }
            }
            if profile.static_facts.is_empty() && profile.dynamic.is_empty() {
                println!("empty profile: nothing stored in this space yet");
            }
        }
        Cmd::Status => {
            let report = engine.status().map_err(|e| e.to_string())?;
            if report.read_only {
                println!("READ-ONLY: another scone process holds the write lock");
            }
            for s in &report.spaces {
                println!(
                    "space {}: episodes: {} chunks: {} revision: {}",
                    s.name, s.episodes, s.chunks, s.revision
                );
            }
            println!(
                "embedder: {} ({} dims)",
                report.embedder_id, report.embedder_dim
            );
            println!(
                "indexes: {}",
                if report.index_dirty {
                    "DIRTY — run `scone doctor --rebuild`"
                } else {
                    "clean"
                }
            );
            match &report.llm_id {
                Some(id) => println!(
                    "semantic lane: llm {id}{} pending, {} failed",
                    report.pending_distill, report.failed_distill
                ),
                None => println!(
                    "semantic lane: paused — no LLM configured ({} pending, {} failed)",
                    report.pending_distill, report.failed_distill
                ),
            }
        }
        Cmd::Ask { question, limit } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let opts = RecallOpts {
                limit: *limit,
                expand_neighbors: true,
                ..Default::default()
            };
            let pack = engine
                .recall(&space, question, &opts)
                .map_err(|e| e.to_string())?;
            let mut context = String::new();
            for f in &pack.facts {
                context.push_str(&format!(
                    "- fact: {} {} {}\n",
                    f.subject, f.predicate, f.object
                ));
            }
            for item in &pack.items {
                context.push_str(&format!("- [episode {}] {}\n", item.episode_id, item.text));
            }
            if engine.has_llm() {
                let answer = engine
                    .llm_answer(question, &context)
                    .map_err(|e| e.to_string())?;
                println!("{answer}");
                let ids: Vec<String> = pack
                    .items
                    .iter()
                    .map(|i| i.episode_id.to_string())
                    .collect();
                if !ids.is_empty() {
                    println!("\nsources: episodes {}", ids.join(", "));
                }
            } else {
                if context.is_empty() {
                    println!("no matching memory");
                } else {
                    print!("{context}");
                }
                println!("(semantic lane paused — no LLM configured; showing raw recall)");
            }
        }
        Cmd::Distill { limit } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let r = engine.distill(&space, *limit).map_err(|e| e.to_string())?;
            println!(
                "distilled {} episode{}: +{} facts, {} closed, {} failed",
                r.processed,
                if r.processed == 1 { "" } else { "s" },
                r.facts_added,
                r.facts_closed,
                r.failed
            );
        }
        Cmd::Facts { action } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            match action {
                FactsCmd::List { all } => {
                    let facts = engine.facts_list(&space, *all).map_err(|e| e.to_string())?;
                    if facts.is_empty() {
                        println!("no facts");
                    }
                    for f in facts {
                        println!(
                            "[{}] {} {} {}  (conf {:.2}, {}, from {})",
                            f.fact_id,
                            f.subject,
                            f.predicate,
                            f.object,
                            f.confidence,
                            f.status,
                            f.valid_from
                        );
                    }
                }
                FactsCmd::Why { id } => {
                    for p in engine.facts_why(&space, *id).map_err(|e| e.to_string())? {
                        println!(
                            "episode {} ({}) {} at {}",
                            p.episode_id,
                            p.kind,
                            p.source.as_deref().unwrap_or("note"),
                            p.created_at
                        );
                    }
                }
                FactsCmd::Close { id, reason } => {
                    engine
                        .facts_close(&space, *id, reason)
                        .map_err(|e| e.to_string())?;
                    println!("closed fact {id}: {reason}");
                }
            }
        }
        Cmd::Watch {
            dir: watch_dir,
            once,
            interval_secs,
            tags,
        } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            loop {
                let tag_refs: Vec<&str> = tags.iter().map(String::as_str).collect();
                let r = engine
                    .ingest_directory_tagged(&space, watch_dir, 1_000_000, &tag_refs)
                    .map_err(|e| e.to_string())?;
                println!(
                    "ingested {} ({} unchanged, {} skipped) from {}",
                    r.ingested,
                    r.deduplicated,
                    r.skipped,
                    watch_dir.display()
                );
                if *once {
                    break;
                }
                std::thread::sleep(std::time::Duration::from_secs((*interval_secs).max(1)));
            }
        }
        Cmd::Daemon {
            watch,
            once,
            interval_secs,
        } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            loop {
                for watch_dir in watch {
                    let r = engine
                        .ingest_directory(&space, watch_dir, 1_000_000)
                        .map_err(|e| e.to_string())?;
                    println!(
                        "scanned {}: {} new, {} unchanged, {} skipped",
                        watch_dir.display(),
                        r.ingested,
                        r.deduplicated,
                        r.skipped
                    );
                }
                if engine.has_llm() {
                    let r = engine.distill(&space, 100).map_err(|e| e.to_string())?;
                    println!(
                        "distilled {}: +{} facts, {} closed, {} failed",
                        r.processed, r.facts_added, r.facts_closed, r.failed
                    );
                } else {
                    println!("distilled 0: semantic lane paused (no LLM configured)");
                }
                let expired = engine.decay_facts(&space, 90).map_err(|e| e.to_string())?;
                if expired > 0 {
                    println!("decayed {expired} stale facts (reasons recorded)");
                }
                if *once {
                    break;
                }
                std::thread::sleep(std::time::Duration::from_secs((*interval_secs).max(1)));
            }
        }
        Cmd::Export => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let jsonl = engine.export_jsonl(&space).map_err(|e| e.to_string())?;
            print!("{jsonl}");
        }
        Cmd::Import { path } => {
            let space = auth::resolve(&mut engine, &cli.space, true).map_err(|e| e.to_string())?;
            let data = if path.as_os_str() == "-" {
                use std::io::Read;
                let mut buf = String::new();
                std::io::stdin()
                    .read_to_string(&mut buf)
                    .map_err(|e| e.to_string())?;
                buf
            } else {
                std::fs::read_to_string(path).map_err(|e| e.to_string())?
            };
            let report = engine
                .import_jsonl(&space, &data)
                .map_err(|e| e.to_string())?;
            println!(
                "imported {} episode{} ({} deduplicated), {} fact{}, {} alias{}",
                report.episodes,
                if report.episodes == 1 { "" } else { "s" },
                report.deduplicated,
                report.facts,
                if report.facts == 1 { "" } else { "s" },
                report.aliases,
                if report.aliases == 1 { "" } else { "es" },
            );
        }
        Cmd::Spaces => {
            let report = engine.status().map_err(|e| e.to_string())?;
            if report.spaces.is_empty() {
                println!("no spaces yet");
            }
            for s in &report.spaces {
                println!(
                    "{}  ({} episodes, revision {})",
                    s.name, s.episodes, s.revision
                );
            }
        }
        Cmd::Mcp => {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .map_err(|e| e.to_string())?;
            let server = scone::mcp::SconeMcp::new(engine, &cli.space);
            rt.block_on(async move {
                use rmcp::ServiceExt;
                let running = server
                    .serve(rmcp::transport::stdio())
                    .await
                    .map_err(|e| e.to_string())?;
                running.waiting().await.map_err(|e| e.to_string())?;
                Ok::<(), String>(())
            })?;
            return Ok(());
        }
        Cmd::Serve { listen } => {
            let raw = std::fs::read_to_string(dir.join("config.toml"))
                .map_err(|_| "scone serve needs config.toml with [[server.keys]]".to_owned())?;
            let table: toml::Table = raw.parse().map_err(|e| format!("config.toml: {e}"))?;
            let server = table
                .get("server")
                .ok_or("config.toml needs a [server] section")?;
            let keys: Vec<scone::serve::SpaceKey> = server
                .get("keys")
                .and_then(|k| k.as_array())
                .map(|rows| {
                    rows.iter()
                        .filter_map(|row| {
                            Some(scone::serve::SpaceKey {
                                key: row.get("key")?.as_str()?.to_owned(),
                                space: row.get("space")?.as_str()?.to_owned(),
                            })
                        })
                        .collect()
                })
                .unwrap_or_default();
            if keys.is_empty() {
                return Err(
                    "refusing to serve with zero API keys — add [[server.keys]]                      entries (key, space) to config.toml"
                        .into(),
                );
            }
            let addr = listen
                .clone()
                .or_else(|| {
                    server
                        .get("listen")
                        .and_then(|l| l.as_str())
                        .map(str::to_owned)
                })
                .unwrap_or_else(|| "127.0.0.1:7437".to_owned());
            let n_keys = keys.len();
            let app = scone::serve::router(engine, scone::serve::ServeConfig { keys });
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .map_err(|e| e.to_string())?;
            rt.block_on(async move {
                let listener = tokio::net::TcpListener::bind(&addr)
                    .await
                    .map_err(|e| format!("bind {addr}: {e}"))?;
                println!("scone serving on http://{addr} ({n_keys} keys)");
                axum::serve(listener, app).await.map_err(|e| e.to_string())
            })?;
            return Ok(());
        }
        Cmd::Doctor { rebuild } => {
            if !rebuild {
                let report = engine.status().map_err(|e| e.to_string())?;
                println!(
                    "indexes: {}",
                    if report.index_dirty { "DIRTY" } else { "clean" }
                );
                println!("run `scone doctor --rebuild` to rebuild from truth");
            } else {
                let report = engine.doctor_rebuild().map_err(|e| e.to_string())?;
                println!(
                    "rebuilt: {} episodes, {} chunks ({} re-embedded)",
                    report.episodes, report.chunks, report.reembedded
                );
            }
        }
    }
    Ok(())
}