hallouminate 0.2.3

A markdown corpus indexer for LLMs to build and query their own per-repo wikis.
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
use std::path::PathBuf;

use clap::{Args, Parser, Subcommand, ValueEnum};

mod config;
mod ground;
mod hook;
mod index;
mod init_repo;

pub use config::{
    ConfigDownloadArgs, ConfigInitArgs, ConfigShowArgs, ConfigValidateArgs, cmd_config_download,
    cmd_config_init, cmd_config_show, cmd_config_validate,
};
pub use ground::{GroundArgs, cmd_ground, run_ground};
pub use hook::{HookArgs, cmd_hook_install, cmd_hook_uninstall};
pub use index::{
    AD_HOC_CORPUS_NAME, CorpusReport, IndexArgs, IndexReport, cmd_index, run_index, select_corpora,
};
pub use init_repo::{InitRepoArgs, cmd_init_repo};

/// CLI surface for output format selection. Mirrors `domain::ground::Format`
/// but kept in the app layer to keep `ValueEnum` (a clap dep) out of the
/// domain module.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, ValueEnum)]
pub enum FormatArg {
    #[default]
    Outline,
    Json,
    JsonPretty,
}

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    Index(IndexCli),
    Ground(GroundCli),
    Hook {
        #[command(subcommand)]
        action: HookAction,
    },
    /// Seed a repository as a hallouminate tenant: writes
    /// `.hallouminate/config.toml` declaring the `[[repository]]` plus a
    /// `.hallouminate/wiki/` skeleton. Identical on every harness.
    InitRepo(InitRepoCli),
    Config {
        #[command(subcommand)]
        action: ConfigAction,
    },
    /// Boot the MCP server on stdio. Exposes `ground`, `index`,
    /// `list_corpora`, `list_files`, `list_tree`, `add_markdown`,
    /// `read_markdown`, `delete_markdown`, and `get_footnote` tools to
    /// MCP-aware clients (Claude Desktop, Claude Code, etc.). The process
    /// runs until stdin closes.
    Serve,
    /// Manage the local daemon: single owner of the LanceDB ground directory,
    /// repository registry, and per-corpus mutation locks. CLI and MCP
    /// clients talk to it over a Unix domain socket. Bare `daemon` runs it in
    /// the foreground until killed; `stop`/`restart`/`status` are lifecycle
    /// controls. Only one instance per socket can run.
    Daemon(DaemonCli),
}

#[derive(Debug, Args)]
pub struct InitRepoCli {
    /// `[[repository]]` name; the wiki becomes the `repo:<name>:wiki` corpus.
    pub name: String,
    /// Repo root to seed (created if absent; defaults to the current directory).
    #[arg(long, value_name = "PATH")]
    pub path: Option<PathBuf>,
    /// Overwrite an existing `.hallouminate/config.toml`. Never clobbers an
    /// existing wiki.
    #[arg(long)]
    pub force: bool,
}

impl From<InitRepoCli> for InitRepoArgs {
    fn from(cli: InitRepoCli) -> Self {
        Self {
            name: cli.name,
            path: cli.path,
            force: cli.force,
        }
    }
}

#[derive(Debug, Args)]
pub struct DaemonCli {
    #[command(subcommand)]
    pub action: Option<DaemonAction>,
    /// Config path override for the foreground run. Only consulted by the
    /// bare `daemon` / `daemon run` form; the lifecycle subcommands talk to
    /// an already-running daemon over the control socket.
    #[arg(long, value_name = "PATH")]
    pub config: Option<PathBuf>,
}

/// Daemon lifecycle action. Bare `daemon` (no subcommand) is `Run`, so
/// `ensure_daemon_running`'s `exe daemon` spawn and existing scripts keep
/// working unchanged.
#[derive(Debug, Subcommand, Clone, PartialEq, Eq)]
pub enum DaemonAction {
    /// Run the daemon in the foreground (the default).
    Run,
    /// Ask a running daemon to shut down gracefully.
    Stop,
    /// Stop a running daemon (if any) and start a fresh one.
    Restart,
    /// Report whether a daemon is reachable.
    Status,
}

impl From<DaemonCli> for crate::app::daemon::DaemonArgs {
    fn from(cli: DaemonCli) -> Self {
        Self { config: cli.config }
    }
}

#[derive(Debug, Args)]
pub struct IndexCli {
    #[arg(long)]
    pub corpus: Option<String>,
    /// Unsupported in the daemon-backed v1 — always errors with
    /// "paths_from is not supported via the daemon yet". Kept hidden so old
    /// scripts surface the message instead of a clap-level "unknown flag".
    #[arg(long, value_name = "FILE", hide = true)]
    pub paths_from: Option<PathBuf>,
    /// Accepted for backward compatibility with pre-layered-config scripts;
    /// no longer consulted locally. The daemon owns config resolution
    /// (XDG baseline at startup + repo-layer discovery per request), so to
    /// change what corpora are visible, restart the daemon
    /// (`hallouminate daemon --config ...`) or edit `.hallouminate/config.toml`
    /// in the working directory's repo.
    #[arg(long, value_name = "PATH")]
    pub config: Option<PathBuf>,
    /// Override the daemon socket path. Mirrors the `HALLOUMINATE_SOCKET`
    /// env var the daemon itself reads; lets test fixtures pin per-test
    /// sockets without env mutation.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
    /// Abort the run if any selected corpus root is missing, instead of the
    /// default skip-with-warning (the missing corpus is skipped and the rest
    /// still index).
    #[arg(long)]
    pub strict: bool,
}

impl From<IndexCli> for IndexArgs {
    fn from(cli: IndexCli) -> Self {
        Self {
            corpus: cli.corpus,
            paths_from: cli.paths_from,
            config: cli.config,
            socket: cli.socket,
            strict: cli.strict,
        }
    }
}

#[derive(Debug, Args)]
pub struct GroundCli {
    pub query: String,
    #[arg(long)]
    pub corpus: Option<String>,
    /// Output format. Default `outline` is the token-efficient ripgrep-style
    /// view. `json` and `json-pretty` emit the full structured response.
    /// Conflicts with `--full`.
    #[arg(long, value_enum, default_value_t = FormatArg::Outline, conflicts_with = "full")]
    pub format: FormatArg,
    /// Shorthand for `--format json-pretty`. The human-readable full view.
    #[arg(long, conflicts_with = "format")]
    pub full: bool,
    /// Trim each chunk's snippet to N chars (ending with `…` if truncated).
    /// Applies to every format — orthogonal to `--format` / `--full`.
    #[arg(long, value_name = "N")]
    pub snippet_chars: Option<usize>,
    #[arg(long, value_name = "N")]
    pub top_files: Option<usize>,
    #[arg(long, value_name = "N")]
    pub chunks_per_file: Option<usize>,
    #[arg(long, value_name = "N")]
    pub limit: Option<usize>,
    /// Accepted for backward compatibility; same caveat as `hallouminate
    /// index --config`. The daemon owns config resolution (XDG baseline
    /// at startup + repo-layer discovery per request), so this flag is
    /// only consulted locally for the cosmetic outline path-prefix-strip
    /// and not for the actual search.
    #[arg(long, value_name = "PATH")]
    pub config: Option<PathBuf>,
    /// Override the daemon socket path. Mirrors `HALLOUMINATE_SOCKET`; lets
    /// test fixtures pin per-test sockets without env mutation.
    #[arg(long, value_name = "PATH")]
    pub socket: Option<PathBuf>,
}

impl From<GroundCli> for GroundArgs {
    fn from(cli: GroundCli) -> Self {
        use crate::domain::ground::Format;
        let format = if cli.full {
            Format::JsonPretty
        } else {
            match cli.format {
                FormatArg::Outline => Format::Outline,
                FormatArg::Json => Format::Json,
                FormatArg::JsonPretty => Format::JsonPretty,
            }
        };
        Self {
            query: cli.query,
            corpus: cli.corpus,
            format,
            snippet_chars: cli.snippet_chars,
            top_files: cli.top_files,
            chunks_per_file: cli.chunks_per_file,
            limit: cli.limit,
            config: cli.config,
            socket: cli.socket,
        }
    }
}

#[derive(Debug, Subcommand)]
pub enum HookAction {
    Install {
        #[arg(long, value_name = "PATH")]
        repo: Option<PathBuf>,
    },
    Uninstall {
        #[arg(long, value_name = "PATH")]
        repo: Option<PathBuf>,
    },
}

#[derive(Debug, Subcommand)]
pub enum ConfigAction {
    Init {
        #[arg(long)]
        force: bool,
        #[arg(long, value_name = "PATH")]
        path: Option<PathBuf>,
    },
    Show {
        #[arg(long, value_name = "PATH")]
        config: Option<PathBuf>,
        /// Working directory for repo-config discovery (defaults to current dir).
        #[arg(long, value_name = "PATH")]
        cwd: Option<PathBuf>,
    },
    Download {
        #[arg(long, value_name = "PATH")]
        config: Option<PathBuf>,
    },
    /// Parse the config, print a summary, and flag unknown top-level keys
    /// (e.g. `[[corpora]]` typo for `[[corpus]]`).
    Validate {
        #[arg(long, value_name = "PATH")]
        config: Option<PathBuf>,
        /// Working directory for repo-config discovery (defaults to current dir).
        #[arg(long, value_name = "PATH")]
        cwd: Option<PathBuf>,
    },
}

pub async fn dispatch(cli: Cli) -> anyhow::Result<()> {
    match cli.command {
        Command::Index(args) => cmd_index(args.into()).await,
        Command::Ground(args) => cmd_ground(args.into()).await,
        Command::Hook { action } => match action {
            HookAction::Install { repo } => cmd_hook_install(HookArgs { repo }),
            HookAction::Uninstall { repo } => cmd_hook_uninstall(HookArgs { repo }),
        },
        Command::InitRepo(args) => cmd_init_repo(args.into()),
        Command::Config { action } => match action {
            ConfigAction::Init { force, path } => cmd_config_init(ConfigInitArgs { force, path }),
            ConfigAction::Show { config, cwd } => cmd_config_show(ConfigShowArgs { config, cwd }),
            ConfigAction::Download { config } => cmd_config_download(ConfigDownloadArgs { config }),
            ConfigAction::Validate { config, cwd } => {
                cmd_config_validate(ConfigValidateArgs { config, cwd })
            }
        },
        Command::Serve => {
            crate::app::daemon::ensure_daemon_running().await?;
            crate::adapters::mcp::serve_stdio().await
        }
        Command::Daemon(args) => {
            let action = args.action.clone().unwrap_or(DaemonAction::Run);
            match action {
                DaemonAction::Run => crate::app::daemon::run_daemon(args.into()).await,
                DaemonAction::Stop => {
                    crate::app::daemon::stop().await?;
                    println!("daemon stopped");
                    Ok(())
                }
                DaemonAction::Restart => {
                    crate::app::daemon::restart().await?;
                    println!("daemon restarted");
                    Ok(())
                }
                DaemonAction::Status => {
                    match crate::app::daemon::status().await? {
                        crate::app::daemon::DaemonStatus::Running => println!("running"),
                        crate::app::daemon::DaemonStatus::NotRunning => println!("not running"),
                    }
                    Ok(())
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_index_subcommand() {
        let cli = Cli::try_parse_from(["hallouminate", "index"]).expect("parse index");
        assert!(matches!(cli.command, Command::Index(_)));
    }

    #[test]
    fn parses_index_with_corpus_and_paths_from() {
        let cli = Cli::try_parse_from([
            "hallouminate",
            "index",
            "--corpus",
            "docs",
            "--paths-from",
            "/tmp/p.txt",
        ])
        .expect("parse");
        match cli.command {
            Command::Index(args) => {
                assert_eq!(args.corpus.as_deref(), Some("docs"));
                assert_eq!(args.paths_from, Some(PathBuf::from("/tmp/p.txt")));
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_ground_subcommand_with_query_and_outline_default() {
        let cli =
            Cli::try_parse_from(["hallouminate", "ground", "spice melange"]).expect("parse ground");
        match cli.command {
            Command::Ground(args) => {
                assert_eq!(args.query, "spice melange");
                assert!(!args.full);
                assert_eq!(args.format, FormatArg::Outline);
                assert_eq!(args.corpus, None);
                assert_eq!(args.snippet_chars, None);
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_ground_with_format_json_flag() {
        let cli = Cli::try_parse_from(["hallouminate", "ground", "q", "--format", "json"])
            .expect("parse");
        match cli.command {
            Command::Ground(args) => {
                assert_eq!(args.format, FormatArg::Json);
                assert!(!args.full);
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_ground_with_full_flag() {
        let cli = Cli::try_parse_from(["hallouminate", "ground", "q", "--full"]).expect("parse");
        match cli.command {
            Command::Ground(args) => {
                assert!(args.full);
                // GroundArgs conversion maps --full → Format::JsonPretty.
                let ga: GroundArgs = args.into();
                assert_eq!(ga.format, crate::domain::ground::Format::JsonPretty);
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_ground_with_snippet_chars() {
        let cli = Cli::try_parse_from(["hallouminate", "ground", "q", "--snippet-chars", "80"])
            .expect("parse");
        match cli.command {
            Command::Ground(args) => assert_eq!(args.snippet_chars, Some(80)),
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_ground_with_overrides() {
        let cli = Cli::try_parse_from([
            "hallouminate",
            "ground",
            "tokio",
            "--corpus",
            "docs",
            "--format",
            "json-pretty",
            "--top-files",
            "5",
            "--chunks-per-file",
            "2",
            "--limit",
            "20",
        ])
        .expect("parse ground with flags");
        match cli.command {
            Command::Ground(args) => {
                assert_eq!(args.query, "tokio");
                assert_eq!(args.corpus.as_deref(), Some("docs"));
                assert_eq!(args.format, FormatArg::JsonPretty);
                assert_eq!(args.top_files, Some(5));
                assert_eq!(args.chunks_per_file, Some(2));
                assert_eq!(args.limit, Some(20));
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn rejects_ground_without_query() {
        let err = Cli::try_parse_from(["hallouminate", "ground"]).expect_err("query required");
        assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
    }

    #[test]
    fn rejects_full_and_format_together() {
        // The two flags are mutually exclusive — clap should error before
        // dispatch instead of letting the user wonder which one won.
        let err =
            Cli::try_parse_from(["hallouminate", "ground", "q", "--full", "--format", "json"])
                .expect_err("conflicting flags must be rejected");
        assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
    }

    #[test]
    fn rejects_removed_pretty_flag() {
        // Pre-1.0 break: --pretty was the old name for what's now --full.
        // clap must surface a clean unknown-arg error instead of silently
        // ignoring the flag and emitting compact JSON.
        let err = Cli::try_parse_from(["hallouminate", "ground", "q", "--pretty"])
            .expect_err("--pretty must be rejected as unknown");
        assert_eq!(err.kind(), clap::error::ErrorKind::UnknownArgument);
    }

    #[test]
    fn parses_daemon_subcommand_without_args() {
        let cli = Cli::try_parse_from(["hallouminate", "daemon"]).expect("parse daemon");
        match cli.command {
            Command::Daemon(args) => {
                assert!(args.config.is_none(), "--config defaults to None");
                let inner: crate::app::daemon::DaemonArgs = args.into();
                assert!(inner.config.is_none());
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_daemon_subcommand_with_config_flag() {
        let cli = Cli::try_parse_from(["hallouminate", "daemon", "--config", "/tmp/cfg.toml"])
            .expect("parse daemon --config");
        match cli.command {
            Command::Daemon(args) => {
                assert_eq!(args.config, Some(PathBuf::from("/tmp/cfg.toml")));
            }
            _ => panic!("wrong variant"),
        }
    }

    #[test]
    fn parses_hook_install_and_uninstall() {
        let install =
            Cli::try_parse_from(["hallouminate", "hook", "install"]).expect("parse hook install");
        match install.command {
            Command::Hook {
                action: HookAction::Install { repo },
            } => assert_eq!(repo, None),
            other => panic!("wrong variant: {other:?}"),
        }
        let uninstall = Cli::try_parse_from(["hallouminate", "hook", "uninstall"])
            .expect("parse hook uninstall");
        match uninstall.command {
            Command::Hook {
                action: HookAction::Uninstall { repo },
            } => assert_eq!(repo, None),
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn parses_hook_install_with_repo_path() {
        let cli = Cli::try_parse_from(["hallouminate", "hook", "install", "--repo", "/tmp/r"])
            .expect("parse hook install --repo");
        match cli.command {
            Command::Hook {
                action: HookAction::Install { repo },
            } => assert_eq!(repo, Some(PathBuf::from("/tmp/r"))),
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn parses_config_init_and_show() {
        let init =
            Cli::try_parse_from(["hallouminate", "config", "init"]).expect("parse config init");
        match init.command {
            Command::Config {
                action: ConfigAction::Init { force, path },
            } => {
                assert!(!force);
                assert_eq!(path, None);
            }
            other => panic!("wrong variant: {other:?}"),
        }
        let show =
            Cli::try_parse_from(["hallouminate", "config", "show"]).expect("parse config show");
        match show.command {
            Command::Config {
                action: ConfigAction::Show { config, cwd: _ },
            } => assert_eq!(config, None),
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn parses_config_init_with_force_and_path() {
        let cli = Cli::try_parse_from([
            "hallouminate",
            "config",
            "init",
            "--force",
            "--path",
            "/tmp/cfg.toml",
        ])
        .expect("parse");
        match cli.command {
            Command::Config {
                action: ConfigAction::Init { force, path },
            } => {
                assert!(force);
                assert_eq!(path, Some(PathBuf::from("/tmp/cfg.toml")));
            }
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn parses_config_show_with_explicit_config_path() {
        let cli =
            Cli::try_parse_from(["hallouminate", "config", "show", "--config", "/tmp/c.toml"])
                .expect("parse");
        match cli.command {
            Command::Config {
                action: ConfigAction::Show { config, cwd: _ },
            } => assert_eq!(config, Some(PathBuf::from("/tmp/c.toml"))),
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn parses_config_download_with_explicit_config_path() {
        let cli = Cli::try_parse_from([
            "hallouminate",
            "config",
            "download",
            "--config",
            "/tmp/c.toml",
        ])
        .expect("parse");
        match cli.command {
            Command::Config {
                action: ConfigAction::Download { config },
            } => assert_eq!(config, Some(PathBuf::from("/tmp/c.toml"))),
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn rejects_missing_subcommand() {
        let err = Cli::try_parse_from(["hallouminate"]).expect_err("requires subcommand");
        assert_eq!(
            err.kind(),
            clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
        );
    }

    #[test]
    fn rejects_unknown_hook_action() {
        let err = Cli::try_parse_from(["hallouminate", "hook", "frobnicate"])
            .expect_err("unknown hook action");
        assert_eq!(err.kind(), clap::error::ErrorKind::InvalidSubcommand);
    }

    #[test]
    fn parses_init_repo_with_name_only() {
        let cli = Cli::try_parse_from(["hallouminate", "init-repo", "demo"]).expect("parse");
        match cli.command {
            Command::InitRepo(args) => {
                assert_eq!(args.name, "demo");
                assert_eq!(args.path, None);
                assert!(!args.force);
            }
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn parses_init_repo_with_path_and_force() {
        let cli = Cli::try_parse_from([
            "hallouminate",
            "init-repo",
            "demo",
            "--path",
            "/tmp/repo",
            "--force",
        ])
        .expect("parse");
        match cli.command {
            Command::InitRepo(args) => {
                assert_eq!(args.name, "demo");
                assert_eq!(args.path, Some(PathBuf::from("/tmp/repo")));
                assert!(args.force);
            }
            other => panic!("wrong variant: {other:?}"),
        }
    }

    #[test]
    fn rejects_init_repo_without_name() {
        let err = Cli::try_parse_from(["hallouminate", "init-repo"]).expect_err("name required");
        assert_eq!(err.kind(), clap::error::ErrorKind::MissingRequiredArgument);
    }
}