leankg 0.19.10

Lightweight Knowledge Graph for AI-Assisted Development
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
use clap::Subcommand;

pub mod shell_runner;

#[derive(Subcommand, Debug)]
pub enum CLICommand {
    /// Show LeanKG version
    Version,
    /// Initialize a new LeanKG project
    Init {
        #[arg(long, default_value = ".leankg")]
        path: String,
        /// FR-LSP-B / REL-039: write prefab `lsp:` servers + typed_resolve=go,ts
        #[arg(long, default_value_t = false)]
        with_lsp: bool,
    },
    /// Index the codebase
    Index {
        /// Path to index
        path: Option<String>,
        #[arg(long, short)]
        incremental: bool,
        /// Filter by language (e.g., go,ts,py)
        #[arg(long, short)]
        lang: Option<String>,
        /// Exclude patterns (comma-separated)
        #[arg(long)]
        exclude: Option<String>,
        /// Verbose output
        #[arg(long, short)]
        verbose: bool,
        /// Target environment (local, staging, production)
        #[arg(long, default_value = "local")]
        env: String,
        /// Service name for this index
        #[arg(long)]
        service_name: Option<String>,
        /// Version tag for this index (semver or git sha)
        #[arg(long)]
        version: Option<String>,
        /// Source URI for remote indexing (gs://bucket, git+https://..., etc.)
        /// When provided, content is synced to a local staging directory before indexing.
        /// Without this flag, indexes the local filesystem path as before.
        #[arg(long)]
        source: Option<String>,
        /// Git branch/tag/commit to check out (used with --source git+...)
        #[arg(long)]
        ref_name: Option<String>,
        /// Auth credential for the source (access token, key path, or service account JSON)
        /// Prefer env vars (GCS_ACCESS_TOKEN, GIT_TOKEN, etc.) when possible.
        #[arg(long)]
        auth: Option<String>,
        /// Run live A/B benchmark before and after indexing.
        #[arg(long)]
        benchmark: bool,
    },
    /// Query the knowledge graph
    Query {
        /// Query string
        query: String,
        /// Query type: name, type, rel, pattern, content, or subgraph
        /// (content does case-insensitive substring match across name, qualified_name, and file_path;
        /// subgraph runs US-GF-03 NL scoped subgraph query — same as `graph-query`)
        #[arg(long, default_value = "name")]
        kind: String,
        /// Find elements in a specific file path (substring match)
        #[arg(long)]
        file: Option<String>,
        /// Find functions by name (substring match)
        #[arg(long)]
        function: Option<String>,
        /// US-GF-03: token budget when `--kind subgraph` (default 2000)
        #[arg(long)]
        token_budget: Option<usize>,
        /// US-GF-03: BFS depth when `--kind subgraph` (default 2)
        #[arg(long)]
        max_depth: Option<usize>,
    },
    /// US-GF-03: Natural-language scoped subgraph query (seed → expand → budget trim)
    GraphQuery {
        /// Natural-language connection question
        question: String,
        /// Approximate token budget for the response (default 2000)
        #[arg(long, default_value = "2000")]
        token_budget: usize,
        /// BFS expansion depth from seeds (default 2)
        #[arg(long, default_value = "2")]
        max_depth: usize,
    },
    /// Generate documentation
    Generate {
        #[arg(long, short)]
        template: Option<String>,
    },
    /// Start web UI server (deprecated - use 'web' command instead)
    Serve {
        /// Port to listen on (default: from PORT env var or 8080)
        #[arg(long)]
        port: Option<u16>,
        /// Project root to open (default: cwd / find_project_root). Use container
        /// mounts like `/workspace` in Docker — do not inherit MCP's multi-repo cwd.
        #[arg(long)]
        project: Option<String>,
    },
    /// Start the embedded web UI server
    Web {
        /// Port to listen on (default: from PORT env var or 8080)
        #[arg(long)]
        port: Option<u16>,
        /// Project root to open (default: cwd / find_project_root)
        #[arg(long)]
        project: Option<String>,
    },
    /// Start MCP server with stdio transport (for opencode integration)
    McpStdio {
        /// Enable auto-indexing with file watcher
        #[arg(long)]
        watch: bool,
    },
    /// Start MCP server with HTTP transport (for remote clients)
    McpHttp {
        /// Port to listen on (default: 9699)
        #[arg(long)]
        port: Option<u16>,
        /// Bearer token for authentication (optional)
        #[arg(long)]
        auth: Option<String>,
        /// Enable auto-indexing with file watcher
        #[arg(long)]
        watch: bool,
        /// Reuse existing server if already running (don't wait/start new)
        #[arg(long)]
        reuse: bool,
        /// Project root directory (default: auto-detect from cwd)
        #[arg(long)]
        project: Option<String>,
    },
    /// Calculate impact radius
    Impact {
        /// File to analyze
        file: String,
        /// Depth of analysis
        #[arg(long, default_value = "3")]
        depth: u32,
        /// Maximum affected elements to return (default 10000).
        /// Bounded to keep memory + output size predictable on big monorepos.
        #[arg(long, default_value = "10000")]
        max_affected: usize,
    },
    /// US-GF-01: Find shortest path between two symbols in the graph
    Path {
        /// Source symbol (qualified_name, name, or fuzzy suffix)
        source: String,
        /// Target symbol (qualified_name, name, or fuzzy suffix)
        target: String,
        /// Maximum number of hops (1-10)
        #[arg(long, default_value = "6")]
        max_hops: usize,
    },
    /// US-GF-02: Explain a node (definition, cluster, degree, neighbors)
    Explain {
        /// Symbol qualified_name, exact name, or fuzzy suffix
        name: String,
    },
    /// US-GF-05: List god nodes (most-connected symbols)
    Gods {
        /// Limit number of results
        #[arg(long, default_value = "20")]
        limit: usize,
        /// Exclude top-N% super-hubs (0-100)
        #[arg(long)]
        exclude_hubs_percentile: Option<u8>,
    },
    /// US-GF-06: Generate GRAPH_REPORT.md (god nodes, confidence, suggested questions)
    Report {
        /// Project display name (default: directory name)
        #[arg(long)]
        project_name: Option<String>,
        /// Output file path (default: .leankg/GRAPH_REPORT.md)
        #[arg(long)]
        out: Option<String>,
    },
    /// US-MP-05: Check graph for broken / stale links
    CheckConsistency {
        /// Filter by severity: BROKEN | STALE | CURRENT
        #[arg(long)]
        severity: Option<String>,
        /// Limit findings shown (default 50). Use 0 for unlimited,
        /// but be ready for big output on a large graph.
        #[arg(long, default_value = "50")]
        limit: usize,
    },
    /// US-CBM-B1: Resolve a symbol via the configured LSP server
    LspResolve {
        /// Source language (go, typescript, python, ...)
        /// If omitted, the bridge auto-detects from the file extension.
        #[arg(long)]
        language: Option<String>,
        /// File containing the symbol
        file_path: String,
        /// 0-indexed line
        #[arg(long, default_value = "0")]
        line: u32,
        /// 0-indexed character (column)
        #[arg(long, default_value = "0")]
        character: u32,
        /// LSP request kind
        #[arg(long, default_value = "definition", value_parser = ["definition", "references", "hover"])]
        request: String,
        /// Project root (where leankg.yaml lives)
        #[arg(long, default_value = ".")]
        project: String,
    },
    /// US-CBM-B1: Install the LSP server for a language (or "all").
    /// Runs the best install method we know for the host OS.
    LspInstall {
        /// Language id or "all" to install every known server.
        language: String,
        /// Project root (where leankg.yaml lives)
        #[arg(long, default_value = ".")]
        project: String,
        /// Print commands instead of running them.
        #[arg(long)]
        dry_run: bool,
    },
    /// US-CBM-B1: List every language the LSP registry knows about.
    LspList,
    /// US-MP-06: List cross-domain tunnels (cross-cluster relationships)
    Tunnels {
        /// Limit
        #[arg(long, default_value = "50")]
        limit: usize,
    },
    /// US-GF-09: Record a query outcome lesson (useful | dead_end | corrected)
    Reflect {
        /// Original question
        question: String,
        /// Outcome classification
        outcome: String,
        /// Optional comma-separated qualified_names that were returned
        #[arg(long)]
        nodes: Option<String>,
        /// Optional free-form note
        #[arg(long)]
        note: Option<String>,
    },
    /// US-GF-08: PR impact dashboard (severity + touched clusters)
    Prs {
        /// Environment scope (default: local)
        #[arg(long, default_value = "local")]
        env: String,
        /// Comma-separated changed file paths (overrides git diff auto-detect)
        #[arg(long)]
        files: Option<String>,
    },
    /// Auto-install MCP config
    Install,
    /// Diagnose stale leankg processes, mmap'd DB files, and current
    /// RSS. Prints `leankg daemon kill` to clean them up. Safe to run
    /// at any time.
    Doctor {
        /// Also kill stale leankg processes (default: report only).
        /// Refuses to kill the current process and the caller's parent.
        #[arg(long)]
        kill: bool,
    },
    /// Show index status
    Status,
    /// Start file watcher for incremental re-indexing
    Watch {
        /// Path to watch (default: project root)
        #[arg(long)]
        path: Option<String>,
    },
    /// Find oversized functions
    Quality {
        /// Minimum line count (default: 50)
        #[arg(long, default_value = "50")]
        min_lines: u32,
        /// Filter by language
        #[arg(long)]
        lang: Option<String>,
    },
    /// Build or refresh the embedding index (requires --features embeddings).
    /// Default mode is incremental: only re-embed nodes touched since the
    /// last `embed` run, plus newly-added nodes. Orphans (state rows whose
    /// qualified_name no longer exists) are reaped from usearch + state.
    ///
    /// By default the command spawns a detached background process and
    /// returns in <1s, leaving a PID + progress file under
    /// `<project>/.leankg/embed_status.json`. Pass `--wait` to run in the
    /// foreground (the legacy behavior). Use `--status` / `--cancel` to
    /// inspect or stop a running background embed.
    #[cfg(feature = "embeddings")]
    Embed {
        /// Download the embedding + reranker models to the cache and exit.
        /// No index is built. Recommended first step on a fresh install.
        #[arg(long)]
        init: bool,
        /// Ignore embedding_state freshness and re-embed every node from
        /// scratch. Use after a model swap or index corruption.
        #[arg(long)]
        full: bool,
        /// Override the embedding batch size (default 32). Auto-capped by
        /// `LEANKG_EMBED_MAX_MB` (default 2048 on macOS). Lower further on
        /// memory-constrained hosts.
        #[arg(long, default_value = "32")]
        batch_size: usize,
        /// Project root (defaults to current working directory).
        #[arg(long, default_value = ".")]
        project: String,
        /// Wait for the embed to complete in the foreground (legacy
        /// behavior). Default: spawn a detached background process and
        /// return immediately.
        #[arg(long)]
        wait: bool,
        /// Print progress for an in-flight background embed and exit.
        #[arg(long)]
        status: bool,
        /// Cancel an in-flight background embed (SIGTERM) and exit.
        #[arg(long)]
        cancel: bool,
        /// Internal: set by the parent process when re-spawning itself as
        /// a background worker. End-users should not pass this.
        #[arg(long, hide = true)]
        background: bool,
        /// Number of parallel ONNX inference workers (default 2). Each
        /// worker holds its own ONNX session (~300–400 MB). Auto-capped by
        /// `LEANKG_EMBED_MAX_MB`. Set to 1 on low-RAM hosts.
        #[arg(long, default_value = "2")]
        workers: usize,
        /// Comma-separated list of element types to embed (e.g. `function,method`).
        /// Defaults to "all" for small graphs (<50k elements) and
        /// `function,method` for mega-graphs to keep cold embed under 5 min.
        /// Pass `--types all` to embed every embeddable type regardless of size.
        #[arg(long, default_value = "")]
        types: String,
        /// Run live A/B benchmark measuring semantic search quality before and after embedding.
        #[arg(long)]
        benchmark: bool,
    },
    /// One-shot embedding retrieval for CLI testing (requires
    /// --features embeddings). Useful for validating the retrieve→rerank→
    /// traverse pipeline without standing up the MCP server.
    #[cfg(feature = "embeddings")]
    SemanticContext {
        /// Natural language query.
        query: String,
        /// Environment filter.
        #[arg(long, default_value = "local")]
        env: String,
        /// ANN retrieve depth. Defaults to adaptive based on index size
        /// (50 for ≤10k vectors, scaling up to 300 for >1M).
        #[arg(long)]
        top_k: Option<usize>,
        /// Final seed count after rerank.
        #[arg(long, default_value = "10")]
        rerank_top_n: usize,
        /// Disable Stage 4 graph enrichment.
        #[arg(long)]
        no_traverse: bool,
        /// Include paths under .worktrees/ / .claude/worktrees/ /
        /// .opencode/worktrees/ (filtered by default).
        #[arg(long)]
        include_worktrees: bool,
        /// Include workflow_step / playbook_step / decision_point /
        /// failure_mode candidates even when the query doesn't mention
        /// them (filtered by default).
        #[arg(long)]
        include_ontology_steps: bool,
        /// Print diagnostics: candidate counts, latency, reranker status.
        #[arg(long)]
        debug: bool,
        /// Project root (defaults to current working directory).
        #[arg(long, default_value = ".")]
        project: String,
    },
    /// Run canonical semantic-context queries with structural assertions.
    /// Catches regressions in the retrieve→rerank→traverse pipeline.
    #[cfg(feature = "embeddings")]
    SmokeTest {
        /// Project root (defaults to current working directory).
        #[arg(long, default_value = ".")]
        project: String,
    },
    /// Export knowledge graph
    Export {
        /// Output file path
        #[arg(long, default_value = "graph.json")]
        output: String,
        /// Export format: json, dot, or mermaid
        #[arg(long, default_value = "json")]
        format: String,
        /// Scope export to a specific file's subgraph
        #[arg(long)]
        file: Option<String>,
        /// Max depth for subgraph traversal (used with --file)
        #[arg(long, default_value = "3")]
        depth: u32,
    },
    /// Annotate code element with business logic description
    Annotate {
        /// Element qualified name (e.g., src/main.rs::main)
        element: String,
        /// Business logic description
        #[arg(long, short)]
        description: String,
        /// User story ID (optional)
        #[arg(long)]
        user_story: Option<String>,
        /// Feature ID (optional)
        #[arg(long)]
        feature: Option<String>,
    },
    /// Link code element to user story or feature
    Link {
        /// Element qualified name
        element: String,
        /// User story or feature ID
        id: String,
        /// Link type: story or feature
        #[arg(long, default_value = "story")]
        kind: String,
    },
    /// Search business logic annotations
    SearchAnnotations {
        /// Search query
        query: String,
    },
    /// Show annotations for an element
    ShowAnnotations {
        /// Element qualified name
        element: String,
    },
    /// Show feature-to-code traceability
    Trace {
        /// Feature ID to trace
        #[arg(long)]
        feature: Option<String>,
        /// User story ID to trace
        #[arg(long)]
        user_story: Option<String>,
        /// Show all traceabilities
        #[arg(long, short)]
        all: bool,
    },
    /// Find code elements by business domain
    FindByDomain {
        /// Business domain (e.g., authentication, validation)
        domain: String,
    },
    /// Run benchmark comparison
    Benchmark {
        /// Specific category to run (optional)
        #[arg(long)]
        category: Option<String>,
        /// CLI tool to use: opencode, gemini, kilo, or claude (default: kilo)
        #[arg(long, default_value = "kilo")]
        cli: String,
    },
    /// Run direct tool performance benchmarks (ontology/search/find)
    ToolBench {
        /// Project path (default: auto-detect from cwd)
        #[arg(long)]
        project: Option<String>,
    },
    /// Run A/B test: LeanKG tools vs manual grep/find equivalents
    AbTest {
        /// Project path (default: auto-detect from cwd)
        #[arg(long)]
        project: Option<String>,
    },
    /// Run unified A/B benchmark (all tools, simple->complex, auto-export markdown)
    BenchmarkUnified {
        /// Project path (default: auto-detect from cwd)
        #[arg(long)]
        project: Option<String>,
    },
    /// Register current directory in global registry
    Register {
        /// Name for the repository
        name: String,
    },
    /// Unregister a repository from global registry
    Unregister {
        /// Name of the repository to unregister
        name: String,
    },
    /// List all registered repositories
    List,
    /// Show status for a registered repository
    StatusRepo {
        /// Name of the repository
        name: String,
    },
    /// Global setup: configure MCP for all registered repos at once, install Claude hooks and register plugin
    Setup {},
    /// Run a shell command with optional RTK-style compression
    Run {
        /// Command to run (e.g., "git status", "cargo test")
        command: Vec<String>,
        /// Enable compression (RTK-style)
        #[arg(long)]
        compress: bool,
    },
    /// Run community detection to identify code clusters
    DetectClusters {
        /// Path to the project (default: current directory)
        #[arg(long)]
        path: Option<String>,
        /// Minimum edges for a node to be considered a hub
        #[arg(long, default_value = "5")]
        min_hub_edges: usize,
    },
    /// Start the REST API server
    ApiServe {
        /// Port to listen on (default: 8081)
        #[arg(long, default_value = "8081")]
        port: u16,
        /// Require API key authentication
        #[arg(long)]
        auth: bool,
    },
    /// Manage API keys for REST API access
    ApiKey {
        #[command(subcommand)]
        command: ApiKeyCommand,
    },
    /// Obsidian vault sync commands
    Obsidian {
        #[command(subcommand)]
        command: ObsidianCommand,
    },
    /// Show context metrics (token savings, usage stats)
    Metrics {
        /// Show metrics from the last N days (e.g., 7d, 30d)
        #[arg(long)]
        since: Option<String>,
        /// Filter by tool name (e.g., search_code, get_context)
        #[arg(long)]
        tool: Option<String>,
        /// Output in JSON format
        #[arg(long, short)]
        json: bool,
        /// Show metrics for current session only
        #[arg(long)]
        session: bool,
        /// Reset all metrics
        #[arg(long)]
        reset: bool,
        /// Set retention period in days (for cleanup)
        #[arg(long)]
        retention: Option<i32>,
        /// Run cleanup to remove old metrics
        #[arg(long)]
        cleanup: bool,
        /// Seed test metrics data
        #[arg(long)]
        seed: bool,
    },
    /// Update LeanKG to the latest version from GitHub releases
    Update,
    /// Manage LeanKG and Vite processes
    Proc {
        #[command(subcommand)]
        command: ProcCommand,
    },
    /// Manage incidents in the knowledge graph
    Incident {
        #[command(subcommand)]
        command: IncidentCommand,
    },
    /// Add a team note to a service or element
    Note {
        /// Target service or element qualified name
        #[arg(long)]
        target: String,
        /// Note content
        #[arg(long)]
        content: String,
        /// Environment
        #[arg(long, default_value = "local")]
        env: String,
    },
    /// Add a known risky pattern annotation
    Pattern {
        /// Pattern title
        #[arg(long)]
        title: String,
        /// Pattern context (code/config pattern description)
        #[arg(long)]
        context: String,
        /// Solution or prevention
        #[arg(long)]
        solution: String,
        /// Environment
        #[arg(long, default_value = "local")]
        env: String,
    },
    /// Show environment conflicts for a service
    EnvConflicts {
        /// Service name
        #[arg(long)]
        service: String,
    },
    /// Push local graph deltas to a shared LeanKG server
    Push {
        /// Remote server URL (e.g., https://leankg.internal)
        #[arg(long)]
        remote: String,
        /// Team token
        #[arg(long)]
        token: String,
        /// Environment
        #[arg(long, default_value = "local")]
        env: String,
    },
    /// Pull latest graph state from a shared LeanKG server
    Pull {
        /// Remote server URL
        #[arg(long)]
        remote: String,
        /// Team token
        #[arg(long)]
        token: String,
        /// Environment to pull
        #[arg(long, default_value = "production")]
        env: String,
    },
    /// Team management commands
    Team {
        #[command(subcommand)]
        command: TeamCommand,
    },
    /// Ontology management commands (semantic search layer)
    Ontology {
        #[command(subcommand)]
        command: OntologyCommand,
    },
}

#[derive(Subcommand, Debug)]
pub enum ApiKeyCommand {
    /// Create a new API key
    Create {
        /// Name for the API key
        #[arg(long)]
        name: String,
    },
    /// List all API keys
    List,
    /// Revoke an API key
    Revoke {
        /// ID of the API key to revoke
        #[arg(long)]
        id: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum ObsidianCommand {
    /// Initialize Obsidian vault structure
    Init {
        /// Custom vault path (default: .leankg/obsidian/vault)
        #[arg(long)]
        vault: Option<String>,
    },
    /// Push LeanKG data to Obsidian notes
    Push {
        /// Custom vault path (default: .leankg/obsidian/vault)
        #[arg(long)]
        vault: Option<String>,
    },
    /// Pull annotation edits from Obsidian to LeanKG
    Pull {
        /// Custom vault path (default: .leankg/obsidian/vault)
        #[arg(long)]
        vault: Option<String>,
    },
    /// Watch Obsidian vault for changes and auto-pull
    Watch {
        /// Custom vault path (default: .leankg/obsidian/vault)
        #[arg(long)]
        vault: Option<String>,
        /// Debounce delay in milliseconds (default: 1000)
        #[arg(long, default_value = "1000")]
        debounce_ms: u64,
    },
    /// Show vault status
    Status {
        /// Custom vault path (default: .leankg/obsidian/vault)
        #[arg(long)]
        vault: Option<String>,
    },
}

#[derive(Subcommand, Debug)]
pub enum ProcCommand {
    /// Show running LeanKG and Vite processes
    Status,
    /// Kill all LeanKG and Vite processes
    Kill,
}

#[derive(Subcommand, Debug)]
pub enum IncidentCommand {
    /// Add a new incident
    Add {
        /// Incident title
        #[arg(long)]
        title: String,
        /// Severity: P0, P1, P2, P3
        #[arg(long)]
        severity: String,
        /// Affected service(s), comma-separated
        #[arg(long)]
        affected: String,
        /// Root cause description
        #[arg(long)]
        root_cause: String,
        /// Resolution description
        #[arg(long)]
        resolution: String,
        /// Prevention advice
        #[arg(long)]
        prevention: Option<String>,
        /// Environment
        #[arg(long, default_value = "production")]
        env: String,
        /// Linked ticket ID
        #[arg(long)]
        ticket: Option<String>,
    },
    /// List incidents for a service
    List {
        /// Service name
        #[arg(long)]
        service: String,
        /// Environment
        #[arg(long, default_value = "production")]
        env: String,
        /// Search pattern
        #[arg(long)]
        pattern: Option<String>,
        /// Limit results
        #[arg(long, default_value = "10")]
        limit: usize,
    },
    /// Show a single incident
    Show {
        /// Incident ID
        id: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum TeamCommand {
    /// Create a new team
    Create {
        /// Team name
        #[arg(long)]
        name: String,
        /// Team description
        #[arg(long)]
        description: String,
        /// Owner user ID
        #[arg(long)]
        owner: String,
    },
    /// List all teams
    List,
    /// Show team details
    Show {
        /// Team ID
        id: String,
    },
    /// Update team information
    Update {
        /// Team ID
        #[arg(long)]
        id: String,
        /// New name (optional)
        #[arg(long)]
        name: Option<String>,
        /// New description (optional)
        #[arg(long)]
        description: Option<String>,
    },
    /// Delete a team
    Delete {
        /// Team ID
        #[arg(long)]
        id: String,
    },
    /// Add member to team
    AddMember {
        /// Team ID
        #[arg(long)]
        team: String,
        /// User ID to add
        #[arg(long)]
        user: String,
        /// Role: admin, contributor, viewer
        #[arg(long, default_value = "viewer")]
        role: String,
    },
    /// Remove member from team
    RemoveMember {
        /// Team ID
        #[arg(long)]
        team: String,
        /// User ID to remove
        #[arg(long)]
        user: String,
    },
    /// Generate invite link for team
    Invite {
        /// Team ID
        #[arg(long)]
        team: String,
        /// Role for invitee
        #[arg(long, default_value = "viewer")]
        role: String,
        /// Email for invitee (optional)
        #[arg(long)]
        email: Option<String>,
        /// Invite expiration in hours (default: 48)
        #[arg(long, default_value = "48")]
        expires_hours: u64,
    },
    /// Accept team invite
    Accept {
        /// Invite token
        #[arg(long)]
        token: String,
        /// User ID accepting invite
        #[arg(long)]
        user: String,
    },
    /// List pending invites for team
    Invites {
        /// Team ID
        #[arg(long)]
        team: String,
    },
    /// Revoke team invite
    RevokeInvite {
        /// Invite token
        #[arg(long)]
        token: String,
    },
    /// Set graph read permissions for team
    SetReadUsers {
        /// Team ID
        #[arg(long)]
        team: String,
        /// Comma-separated list of user IDs
        #[arg(long)]
        users: String,
    },
    /// Set graph write permissions for team
    SetWriteUsers {
        /// Team ID
        #[arg(long)]
        team: String,
        /// Comma-separated list of user IDs
        #[arg(long)]
        users: String,
    },
    /// Check if user has permission
    CheckPermission {
        /// Team ID
        #[arg(long)]
        team: String,
        /// User ID to check
        #[arg(long)]
        user: String,
        /// Require write permission
        #[arg(long)]
        write: bool,
    },
}

#[derive(Subcommand, Debug)]
pub enum OntologyCommand {
    /// Validate ontology YAML files
    Validate,
    /// Sync ontology from YAML files into the graph
    Sync {
        /// Path to ontology directory (default: ./ontology)
        #[arg(long)]
        path: Option<String>,
    },
    /// Show ontology status and coverage
    Status,
    /// Get ontology context for a semantic query
    Context {
        /// Query string
        query: String,
        /// Environment
        #[arg(long, default_value = "local")]
        env: String,
        /// Expansion depth
        #[arg(long, default_value = "2")]
        depth: u32,
    },
    /// Get concept map for a domain or service
    ConceptMap {
        /// Concept or service name
        query: String,
        /// Environment
        #[arg(long, default_value = "local")]
        env: String,
    },
    /// Trace a workflow's ordered steps
    TraceWorkflow {
        /// Workflow name or ID
        workflow_id_or_query: String,
        /// Environment
        #[arg(long, default_value = "local")]
        env: String,
    },
    /// Concept-gated search: extract keywords -> scan concept ontology ->
    /// load concept -> query the LeanKG DB for the actual code.
    ConceptSearch {
        /// Raw natural-language or concept query (e.g. "feature flag", "gorm store")
        query: String,
        /// Environment scope for the ontology scan
        #[arg(long, default_value = "local")]
        env: String,
        /// Maximum number of concepts / code results
        #[arg(long, default_value_t = 20)]
        limit: usize,
    },
}