doiget-cli 0.3.0

doiget CLI binary
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
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
//! `doiget capabilities` — single-shot inventory JSON for LLM cold-boot
//! (#214).
//!
//! Emits a single JSON value describing the **full surface** of this
//! `doiget` binary: subcommands (walked from the live `clap::Command`
//! tree so the inventory cannot drift from the parser), positional args
//! and named flags per subcommand, global flags, the four
//! [`super::output::OutputMode`] values, hand-maintained env-var + example tables, the
//! `doiget_*` MCP tool list, compile-time features, and a `docs` map
//! pointing at the canonical spec files.
//!
//! Design rationale: the existing `--help` output lists subcommand
//! names but the rest of doiget's surface (env vars, MCP tools, JSON
//! schemas, ADR refs) is scattered across `docs/`. An LLM cold-booted
//! into doiget — no repo access, no follow-up doc reads — cannot
//! discover those via `--help` alone. This subcommand closes that gap
//! with one round-trip.
//!
//! # Output mode
//!
//! `doiget capabilities` is a **product-output** command per the
//! ADR-0017 convention (`--mode` is informational; the JSON inventory
//! is the artefact). `--mode quiet` is the one mode that suppresses
//! stdout (#203 / CONFIG.md §5); every other mode emits the same JSON.
//!
//! # Wire-format stability (whole module)
//!
//! Every `pub` struct / enum below carries `#[non_exhaustive]`. Adding
//! a field is non-breaking; renaming or removing one is a
//! compile-time break for downstream Rust consumers and a
//! `[BREAKING]`-class change for JSON consumers (CHANGELOG must call
//! it out). The per-item `#[non_exhaustive]` attributes intentionally
//! carry no inline comment; this module-doc says it once.

use anyhow::{Context, Result};
use serde::Serialize;

/// Top-level capability inventory. Serialised to stdout as one JSON
/// value. Field names are part of the public wire format: renaming
/// any field is a semver minor with a CHANGELOG `\[BREAKING\]` callout
/// (same discipline as `EntryInfo` / `MigrationReport` in #213).
#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct Capabilities {
    /// `CARGO_PKG_VERSION` for this build.
    pub version: &'static str,
    /// Cargo features compiled into this binary. Contains `"oa-only"`
    /// in stock release builds (the default feature). Empty only when
    /// the crate was built with `--no-default-features` and **no
    /// other features enabled**; a build like
    /// `cargo build --no-default-features --features citation`
    /// yields `["citation"]`, not `[]`.
    pub features: Vec<&'static str>,
    /// All four [`super::output::OutputMode`] values; the parser accepts these for
    /// `--mode`. Mirrors `CONFIG.md` §5 (CLI flags).
    pub modes: &'static [&'static str],
    /// Global flags that apply to every subcommand.
    pub global_flags: Vec<FlagSpec>,
    /// One entry per CLI subcommand (clap-walked).
    pub subcommands: Vec<SubcommandSpec>,
    /// `DOIGET_*` env vars from CONFIG.md §4.
    pub env_vars: &'static [EnvVar],
    /// MCP tools exposed by `doiget serve` (hand-coded; the source of
    /// truth is `docs/MCP_TOOLS.md` §1).
    pub mcp_tools: &'static [McpTool],
    /// Canonical doc paths an LLM can pull for deeper context.
    pub docs: Docs,
}

/// What kind of value (if any) a [`FlagSpec`] carries.
///
/// Typed (not `&'static str`) so a typo can't slip into the wire
/// format and the `Enum`-implies-`values`-present invariant is
/// expressible at the type layer (see #215 for the design pass). Serialises
/// as the lowercased variant name: `"bool"`, `"enum"`, `"string"`.
#[non_exhaustive]
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum FlagKind {
    /// Boolean switch (no value).
    Bool,
    /// Value-bounded flag — `values` carries the accepted set.
    Enum,
    /// Any non-`Bool`, non-`Enum` flag. Today every such flag emits
    /// `"string"`; richer typing (`Path` / `Int` etc.) is intentionally
    /// out of scope until a real consumer needs it — `#[non_exhaustive]`
    /// reserves space without commitment.
    String,
}

#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct FlagSpec {
    /// e.g. `--mode`, `--json`, `-q`.
    pub name: String,
    /// Boolean / enum / free-string discriminator. See [`FlagKind`].
    pub kind: FlagKind,
    /// `clap` `help` text.
    pub help: Option<String>,
    /// For `kind == FlagKind::Enum`: the accepted values, harvested
    /// from clap's `PossibleValuesParser`. Owned (not `&'static`) so
    /// the helper works for any future enum flag, not just `--mode`
    /// (see #215).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub values: Option<Vec<String>>,
}

#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct SubcommandSpec {
    pub name: String,
    pub summary: Option<String>,
    pub args: Vec<ArgSpec>,
    pub flags: Vec<FlagSpec>,
    /// Hand-maintained canonical invocations.
    pub examples: &'static [&'static str],
    /// How this command interacts with `--mode json`. See [`JsonMode`].
    pub json_mode: JsonMode,
    /// Cargo feature this subcommand is gated behind, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feature_gated: Option<&'static str>,
}

/// What kind of positional argument an [`ArgSpec`] describes.
///
/// Currently every entry is `Positional`; the typed enum reserves
/// space for future variants (e.g. `Stdin` markers) without breaking
/// existing JSON consumers. Serialises as `"positional"`.
#[non_exhaustive]
#[derive(Debug, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ArgKind {
    /// A required-or-optional positional argument on the subcommand.
    Positional,
}

#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct ArgSpec {
    pub name: String,
    /// Always [`ArgKind::Positional`] today. Kept as a discriminator
    /// so the JSON shape can grow new arg kinds later without
    /// renaming fields (see #215 for the design pass).
    pub kind: ArgKind,
    pub help: Option<String>,
    /// `true` when the arg has no default and no `Option<T>` wrapper.
    pub required: bool,
}

/// How a subcommand interacts with `--mode json`.
///
/// Wire shape: every variant serialises to an object with a `status`
/// discriminant, so a consumer sees uniform `{"status":"…", …}`
/// records (`#[serde(tag = "status")]`). Before #215 the previous
/// mixed string/object representation forced consumers to handle two
/// JSON shapes for sibling variants.
///
/// **Tuple variants not permitted.** `#[serde(tag = "status")]`
/// requires the tag to live in the same flat object as variant
/// fields; tuple variants are incompatible with internally-tagged
/// representation. Future variants MUST use named fields.
#[non_exhaustive] // Adding a future variant is non-breaking for JSON consumers.
#[derive(Debug, Serialize)]
#[serde(tag = "status", rename_all = "lowercase")]
pub enum JsonMode {
    /// The command's primary output IS the requested artifact, not
    /// informational chatter. `--mode` is informational here; the
    /// exact stdout shape (e.g. JSON for `csl` / `graph` /
    /// `capabilities` and the JSON-RPC stream from `serve`; BibTeX
    /// for `bib`; PDF-on-disk + stderr summary for `fetch`; a
    /// `--dry-run` JSON plan in the dry-run variants) is fixed by
    /// the subcommand and may vary across flags. **Consult
    /// `examples` for the per-flag stdout form** rather than
    /// assuming JSON.
    Artifact,
    /// Under `--mode json` the command emits a structured JSON body
    /// on stdout; otherwise the human form (e.g. `info`,
    /// `list-recent`, `audit-log`, `provenance migrate`, `batch`).
    Supported,
    // NOTE: a `Deferred { tracking: &'static str }` variant was
    // sketched during #214's design phase but never instantiated by
    // any subcommand. Removed in the #215 self-review pass to avoid
    // shipping an unused wire shape; `#[non_exhaustive]` keeps the
    // door open to add it back non-breakingly when a real consumer
    // emerges.
}

#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct EnvVar {
    pub name: &'static str,
    /// `(none)` when no built-in default.
    pub default: &'static str,
    pub help: &'static str,
}

#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct McpTool {
    pub name: &'static str,
    /// Anchor-style reference into `docs/MCP_TOOLS.md`.
    pub schema_ref: &'static str,
}

#[allow(missing_docs)] // Field names ARE the schema; documented externally in #214.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct Docs {
    pub config: &'static str,
    pub errors: &'static str,
    pub scope: &'static str,
    pub mcp: &'static str,
    pub sources: &'static str,
    pub redirect_allowlist: &'static str,
    pub provenance_log: &'static str,
}

// ---------------------------------------------------------------------------
// Static tables
// ---------------------------------------------------------------------------

const MODES: &[&str] = &["human", "json", "quiet", "mcp"];

const ENV_VARS: &[EnvVar] = &[
    EnvVar {
        name: "DOIGET_STORE_ROOT",
        default: "$HOME/papers",
        help: "Root of the on-disk paper store. CONFIG.md §4.",
    },
    EnvVar {
        name: "DOIGET_CACHE_ROOT",
        default: "$HOME/.cache/doiget",
        help: "Root of the on-disk HTTP / metadata cache. CONFIG.md §4.",
    },
    EnvVar {
        name: "DOIGET_LOG_PATH",
        default: "<config_dir>/doiget/access.jsonl",
        help: "JSON-Lines provenance log file path (PROVENANCE_LOG.md §3).",
    },
    EnvVar {
        name: "DOIGET_LOG_RETENTION_DAYS",
        default: "90",
        help: "Rotated-segment retention window (0 disables pruning). #140 / PROVENANCE_LOG.md §6.",
    },
    EnvVar {
        name: "DOIGET_MODE",
        default: "(none)",
        help: "Output mode (`human`/`json`/`quiet`/`mcp`). ADR-0017 ladder rung 3.",
    },
    EnvVar {
        name: "DOIGET_CONTACT_EMAIL",
        default: "(none)",
        help: "Contact email for polite User-Agent header (CONFIG.md §4).",
    },
    EnvVar {
        name: "DOIGET_UNPAYWALL_EMAIL",
        default: "(falls back to DOIGET_CONTACT_EMAIL)",
        help: "Unpaywall-specific contact email.",
    },
    EnvVar {
        name: "DOIGET_USER_AGENT",
        default: "(default polite UA)",
        help: "Override the User-Agent header for all outbound requests.",
    },
    EnvVar {
        name: "DOIGET_ENABLE_OPENALEX",
        default: "(off)",
        help: "Enable the OpenAlex citation graph source (graph subcommand prerequisite).",
    },
    EnvVar {
        name: "DOIGET_ARXIV_BASE",
        default: "https://export.arxiv.org/",
        help: "arXiv API base URL — primarily for testing/wiremock override.",
    },
    EnvVar {
        name: "DOIGET_CROSSREF_BASE",
        default: "https://api.crossref.org/",
        help: "Crossref API base URL.",
    },
    EnvVar {
        name: "DOIGET_UNPAYWALL_BASE",
        default: "https://api.unpaywall.org/",
        help: "Unpaywall API base URL.",
    },
];

const MCP_TOOLS: &[McpTool] = &[
    McpTool {
        name: "doiget_resolve_paper",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_fetch_paper",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_metadata_only",
        schema_ref: "docs/MCP_TOOLS.md#11-doiget_metadata_only-normative",
    },
    McpTool {
        name: "doiget_batch_fetch",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_info",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_search_local",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_list_recent",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_paper_pdf_path",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_capability_profile",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_health",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_expand_citation_graph",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_bibtex_export",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
    McpTool {
        name: "doiget_csl_export",
        schema_ref: "docs/MCP_TOOLS.md#1-tool-list",
    },
];

const DOCS: Docs = Docs {
    config: "docs/CONFIG.md",
    errors: "docs/ERRORS.md",
    scope: "docs/SCOPE.md",
    mcp: "docs/MCP_TOOLS.md",
    sources: "docs/SOURCES.md",
    redirect_allowlist: "docs/REDIRECT_ALLOWLIST.md",
    provenance_log: "docs/PROVENANCE_LOG.md",
};

/// Per-subcommand hand-maintained metadata. The clap walk provides
/// name + summary + args + flags; this table adds examples,
/// `json_mode` semantics, and feature-gating that clap doesn't
/// expose. A regression unit test asserts every clap-visible
/// subcommand has an entry here (otherwise the test fails loudly).
///
/// **Maintenance:** `feature_gated` MUST be kept in sync with the
/// corresponding `#[cfg(feature = …)]` annotation in `main.rs`. There
/// is no compile-time check; the `every_test_cli_subcommand_has_metadata`
/// regression test does not cover feature-gating directly — it only
/// asserts metadata exists. Add a CI matrix entry (`--features
/// citation`) when introducing new gated subcommands so the e2e
/// assertion list catches drift (see #215). Alternatively, add a
/// unit test that asserts `metadata_for("graph").unwrap().feature_gated
/// == Some("citation")` to lock the gate at the lib-test layer.
struct SubcommandMeta {
    examples: &'static [&'static str],
    json_mode: JsonMode,
    feature_gated: Option<&'static str>,
}

fn metadata_for(subcommand: &str) -> Option<SubcommandMeta> {
    let m = match subcommand {
        "fetch" => SubcommandMeta {
            examples: &[
                "doiget fetch 10.1234/foo",
                "doiget fetch arxiv:2401.12345",
                "doiget fetch 10.1234/foo --dry-run",
            ],
            // The success summary is on stderr (ADR-0001); the
            // dry-run plan is JSON product output (ADR-0022).
            json_mode: JsonMode::Artifact,
            feature_gated: None,
        },
        "batch" => SubcommandMeta {
            examples: &[
                "doiget batch refs.txt",
                "doiget batch refs.txt --dry-run",
                "doiget batch refs.txt --json",
            ],
            // `--json` emits the ERRORS.md §3 JSONL per-ref shape (#205).
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "info" => SubcommandMeta {
            examples: &[
                "doiget info 10.1234/foo",
                "doiget info arxiv:2401.12345 --json",
            ],
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "list-recent" => SubcommandMeta {
            examples: &[
                "doiget list-recent",
                "doiget list-recent 20",
                "doiget list-recent --json",
            ],
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "search" => SubcommandMeta {
            examples: &[
                "doiget search 'quantum entanglement'",
                "doiget search renormalization --json",
            ],
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "bib" => SubcommandMeta {
            examples: &["doiget bib 10.1234/foo", "doiget bib arxiv:2401.12345"],
            // BibTeX output is the product; `--mode` is informational.
            json_mode: JsonMode::Artifact,
            feature_gated: None,
        },
        "csl" => SubcommandMeta {
            examples: &["doiget csl 10.1234/foo"],
            json_mode: JsonMode::Artifact,
            feature_gated: None,
        },
        "audit-log" => SubcommandMeta {
            examples: &[
                "doiget audit-log --verify",
                "doiget audit-log --verify --json",
                "doiget --quiet audit-log --verify   # exit code only",
            ],
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "provenance" => SubcommandMeta {
            examples: &[
                "doiget provenance migrate --dry-run",
                "doiget provenance migrate",
                "doiget provenance migrate --dry-run --json",
            ],
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "config" => SubcommandMeta {
            examples: &[
                "doiget config show",
                "doiget config show --json",
                "doiget config path",
                "doiget config doctor",
            ],
            json_mode: JsonMode::Supported,
            feature_gated: None,
        },
        "serve" => SubcommandMeta {
            examples: &["doiget serve   # stdio MCP server (ADR-0001)"],
            // serve always runs in mcp mode; the protocol output is
            // JSON-RPC, which is product.
            json_mode: JsonMode::Artifact,
            feature_gated: None,
        },
        "graph" => SubcommandMeta {
            examples: &[
                "DOIGET_ENABLE_OPENALEX=1 doiget graph 10.1234/foo",
                "DOIGET_ENABLE_OPENALEX=1 doiget graph 10.1234/foo --depth 2 --total 50",
            ],
            json_mode: JsonMode::Artifact,
            feature_gated: Some("citation"),
        },
        "capabilities" => SubcommandMeta {
            examples: &["doiget capabilities | jq ."],
            // The whole point of capabilities IS JSON output.
            json_mode: JsonMode::Artifact,
            feature_gated: None,
        },
        // clap auto-adds `help`; we silently ignore it (it's not a
        // domain subcommand).
        "help" => return None,
        _ => return None,
    };
    Some(m)
}

// ---------------------------------------------------------------------------
// Build
// ---------------------------------------------------------------------------

/// Build the [`Capabilities`] inventory from `cli` (the clap parser
/// for this binary, supplied by the caller because the `Cli` struct
/// lives in `main.rs` and is not exposed in the library crate). The
/// caller is `commands::main::run_dispatch` via `Cli::command()`.
pub fn build_capabilities(cli: &clap::Command) -> Capabilities {
    let global_flags = collect_global_flags(cli);
    let subcommands = cli
        .get_subcommands()
        .filter_map(|sub| build_subcommand(sub, cli))
        .collect::<Vec<_>>();
    Capabilities {
        version: env!("CARGO_PKG_VERSION"),
        features: compile_time_features(),
        modes: MODES,
        global_flags,
        subcommands,
        env_vars: ENV_VARS,
        mcp_tools: MCP_TOOLS,
        docs: DOCS,
    }
}

fn compile_time_features() -> Vec<&'static str> {
    let mut feats: Vec<&'static str> = Vec::new();
    if cfg!(feature = "oa-only") {
        feats.push("oa-only");
    }
    if cfg!(feature = "metadata") {
        feats.push("metadata");
    }
    if cfg!(feature = "citation") {
        feats.push("citation");
    }
    if cfg!(feature = "tdm-elsevier") {
        feats.push("tdm-elsevier");
    }
    if cfg!(feature = "tdm-aps") {
        feats.push("tdm-aps");
    }
    if cfg!(feature = "tdm-springer") {
        feats.push("tdm-springer");
    }
    feats
}

fn collect_global_flags(cmd: &clap::Command) -> Vec<FlagSpec> {
    cmd.get_arguments()
        .filter(|a| a.is_global_set())
        .map(arg_to_flag_spec)
        .collect()
}

fn build_subcommand(sub: &clap::Command, root: &clap::Command) -> Option<SubcommandSpec> {
    let name = sub.get_name();
    let meta = metadata_for(name)?;
    let (args, flags) = split_args_and_flags(sub, root);
    Some(SubcommandSpec {
        name: name.to_string(),
        summary: sub.get_about().map(|s| s.to_string()),
        args,
        flags,
        examples: meta.examples,
        json_mode: meta.json_mode,
        feature_gated: meta.feature_gated,
    })
}

fn split_args_and_flags(
    sub: &clap::Command,
    root: &clap::Command,
) -> (Vec<ArgSpec>, Vec<FlagSpec>) {
    // The root's global args appear in every subcommand's iterator;
    // suppress them from per-subcommand `flags` (they're already in
    // `global_flags`).
    let global_names: std::collections::HashSet<&str> = root
        .get_arguments()
        .filter(|a| a.is_global_set())
        .map(|a| a.get_id().as_str())
        .collect();
    let mut args = Vec::new();
    let mut flags = Vec::new();
    for a in sub.get_arguments() {
        if global_names.contains(a.get_id().as_str()) {
            continue;
        }
        // Clap auto-adds `--help` (and `--version` on the root) to
        // every subcommand. They're not positional and not
        // `is_global_set()`, so they would otherwise leak into every
        // subcommand's `flags[]` as `kind: "string"`. Filter on the
        // action against the known built-in variants.
        //
        // **Maintenance:** `clap::ArgAction` is itself
        // `#[non_exhaustive]` upstream. A future clap release that
        // adds a new built-in action (e.g. a hypothetical
        // `HelpMarkdown`) would fall through this `matches!` and
        // reappear in `flags[]`. Re-audit this filter on every clap
        // minor-version bump.
        if matches!(
            a.get_action(),
            clap::ArgAction::Help
                | clap::ArgAction::HelpShort
                | clap::ArgAction::HelpLong
                | clap::ArgAction::Version
        ) {
            continue;
        }
        if a.is_positional() {
            args.push(ArgSpec {
                name: a.get_id().to_string(),
                kind: ArgKind::Positional,
                help: a.get_help().map(|s| s.to_string()),
                required: a.is_required_set(),
            });
        } else {
            flags.push(arg_to_flag_spec(a));
        }
    }
    (args, flags)
}

fn arg_to_flag_spec(a: &clap::Arg) -> FlagSpec {
    let name = a
        .get_long()
        .map(|s| format!("--{s}"))
        .or_else(|| a.get_short().map(|c| format!("-{c}")))
        .unwrap_or_else(|| a.get_id().to_string());
    // Boolean switches → `Bool`; value-enum flags → `Enum` with the
    // accepted values harvested from clap directly; everything else
    // → `String`. The `possible_values()` harvest covers any future
    // enum flag without code change (see #215).
    let possible: Option<Vec<String>> = a
        .get_value_parser()
        .possible_values()
        .map(|it| it.map(|pv| pv.get_name().to_owned()).collect());
    let (kind, values) = if matches!(
        a.get_action(),
        clap::ArgAction::SetTrue | clap::ArgAction::SetFalse
    ) {
        (FlagKind::Bool, None)
    } else if let Some(vs) = possible {
        (FlagKind::Enum, Some(vs))
    } else {
        (FlagKind::String, None)
    };
    FlagSpec {
        name,
        kind,
        help: a.get_help().map(|s| s.to_string()),
        values,
    }
}

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

/// Run the `doiget capabilities` subcommand. Honors [`super::output::OutputMode`]:
/// `Quiet` suppresses stdout (#203); every other mode emits the same
/// pretty-printed JSON inventory. The caller passes the live
/// `clap::Command` so the clap walk operates on the binary's actual
/// `Cli` tree (which the lib half of this crate can't reach
/// directly — the `Cli` struct lives in `main.rs`).
pub fn run(cli: &clap::Command, mode: super::output::OutputMode) -> Result<()> {
    // `Quiet` is the one mode that suppresses (per ADR-0017 / #203).
    // Every other mode emits the same pretty JSON: `capabilities` is a
    // product-output command.
    if mode == super::output::OutputMode::Quiet {
        return Ok(());
    }
    let caps = build_capabilities(cli);
    let s = serde_json::to_string_pretty(&caps).context("serialise capabilities inventory")?;
    // `print_stdout` workspace-deny; localised allow at the
    // sanctioned product-output sink. See `commands/csl.rs`'s pattern.
    #[allow(clippy::print_stdout)]
    {
        println!("{s}");
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;

    /// Mirrors the `Cli` struct in `main.rs` for lib-test reach.
    /// `commands::capabilities` is library-level; the binary-only
    /// `Cli` struct can't be reached from here, so we re-derive a
    /// shadow whose subcommand list is identical. The
    /// `cli_shadow_matches_main_cli` integration test in
    /// `tests/capabilities_e2e.rs` runs the real binary and asserts
    /// the wire output matches.
    fn test_cli() -> clap::Command {
        use clap::{Arg, ArgAction, Command};
        let mode_values = ["human", "json", "quiet", "mcp"];
        let cmd = Command::new("doiget")
            .arg(
                Arg::new("mode")
                    .long("mode")
                    .global(true)
                    .value_parser(clap::builder::PossibleValuesParser::new(mode_values))
                    .help("Output mode (human|json|quiet|mcp)."),
            )
            .arg(
                Arg::new("json")
                    .long("json")
                    .global(true)
                    .action(ArgAction::SetTrue)
                    .help("Short for `--mode json`."),
            )
            .arg(
                Arg::new("quiet")
                    .long("quiet")
                    .short('q')
                    .global(true)
                    .action(ArgAction::SetTrue)
                    .help("Short for `--mode quiet`."),
            )
            .subcommand(
                Command::new("fetch")
                    .about("Fetch a single paper PDF")
                    .arg(Arg::new("ref").required(true))
                    .arg(
                        Arg::new("dry-run")
                            .long("dry-run")
                            .action(ArgAction::SetTrue),
                    ),
            )
            .subcommand(
                Command::new("batch")
                    .about("Fetch many refs")
                    .arg(Arg::new("path").required(true))
                    .arg(
                        Arg::new("dry-run")
                            .long("dry-run")
                            .action(ArgAction::SetTrue),
                    ),
            )
            .subcommand(
                Command::new("info")
                    .about("Show metadata")
                    .arg(Arg::new("ref").required(true)),
            )
            .subcommand(Command::new("list-recent").about("List recent"))
            .subcommand(
                Command::new("search")
                    .about("Search local")
                    .arg(Arg::new("query").required(true)),
            )
            .subcommand(
                Command::new("bib")
                    .about("BibTeX export")
                    .arg(Arg::new("ref").required(true)),
            )
            .subcommand(
                Command::new("csl")
                    .about("CSL export")
                    .arg(Arg::new("ref").required(true)),
            )
            .subcommand(
                Command::new("audit-log")
                    .about("Audit log")
                    .arg(Arg::new("verify").long("verify").action(ArgAction::SetTrue)),
            )
            .subcommand(Command::new("provenance").about("Provenance ops"))
            .subcommand(
                Command::new("config")
                    .about("Config")
                    .arg(Arg::new("action").required(true)),
            )
            .subcommand(Command::new("serve").about("MCP server"));
        // `graph` is `#[cfg(feature = "citation")]` in main.rs; mirror
        // the gate so the shadow CLI matches the production surface
        // (see #215).
        #[cfg(feature = "citation")]
        let cmd = cmd.subcommand(
            Command::new("graph")
                .about("Citation graph")
                .arg(Arg::new("ref").required(true)),
        );
        cmd.subcommand(Command::new("capabilities").about("Capabilities"))
    }

    fn caps() -> Capabilities {
        build_capabilities(&test_cli())
    }

    #[test]
    fn capabilities_serialises_to_valid_json() {
        let s = serde_json::to_string_pretty(&caps()).expect("serialise");
        let v: serde_json::Value = serde_json::from_str(&s).expect("parse round-trip");
        for key in [
            "version",
            "features",
            "modes",
            "global_flags",
            "subcommands",
            "env_vars",
            "mcp_tools",
            "docs",
        ] {
            assert!(
                v.get(key).is_some(),
                "top-level key `{key}` missing from capabilities JSON: {v}"
            );
        }
    }

    #[test]
    fn modes_field_matches_output_mode_enum() {
        // Tied to `OutputMode { Human, Json, Quiet, Mcp }`.
        assert_eq!(caps().modes, &["human", "json", "quiet", "mcp"]);
    }

    #[test]
    fn env_vars_all_use_doiget_prefix() {
        for ev in ENV_VARS {
            assert!(
                ev.name.starts_with("DOIGET_"),
                "env var name MUST use DOIGET_ prefix, got `{}`",
                ev.name
            );
        }
    }

    #[test]
    fn mcp_tools_all_use_doiget_prefix() {
        for t in MCP_TOOLS {
            assert!(
                t.name.starts_with("doiget_"),
                "MCP tool name MUST use doiget_ prefix, got `{}`",
                t.name
            );
        }
    }

    #[test]
    fn subcommand_examples_reference_the_subcommand_name() {
        for sub in &caps().subcommands {
            for ex in sub.examples {
                // `graph` examples carry a `DOIGET_ENABLE_OPENALEX=1`
                // env prefix before `doiget …`. Allow either form.
                assert!(
                    ex.starts_with("doiget ") || ex.contains(" doiget "),
                    "example `{ex}` for `{}` must invoke `doiget` somewhere",
                    sub.name
                );
                assert!(
                    ex.contains(&sub.name),
                    "example `{ex}` does not mention subcommand `{}`",
                    sub.name
                );
            }
        }
    }

    // Exact-set parity guard against drift between the static
    // `ENV_VARS` table and the documented surface (#215). The expected set is the SOURCE OF TRUTH at test time;
    // adding a new DOIGET_* env var requires updating both ENV_VARS
    // and this list in lockstep. CHANGELOG records cross-PR changes.
    #[test]
    fn env_vars_exact_set_matches_expected() {
        let actual: std::collections::BTreeSet<&str> = ENV_VARS.iter().map(|ev| ev.name).collect();
        let expected: std::collections::BTreeSet<&str> = [
            // CONFIG.md §4 documented:
            "DOIGET_STORE_ROOT",
            "DOIGET_CACHE_ROOT",
            "DOIGET_LOG_PATH",
            "DOIGET_LOG_RETENTION_DAYS",
            "DOIGET_USER_AGENT",
            "DOIGET_UNPAYWALL_EMAIL",
            "DOIGET_MODE",
            // Code-reachable but documented in code-level docs or
            // CAPABILITY.md (not CONFIG.md §4):
            "DOIGET_CONTACT_EMAIL",
            "DOIGET_ENABLE_OPENALEX",
            // Test/wiremock-override base URLs:
            "DOIGET_ARXIV_BASE",
            "DOIGET_CROSSREF_BASE",
            "DOIGET_UNPAYWALL_BASE",
        ]
        .into_iter()
        .collect();
        assert_eq!(
            actual, expected,
            "ENV_VARS table drifted from the expected canonical set; \
             update both `ENV_VARS` and this test together (and CONFIG.md §4 \
             if the new var is user-documented)."
        );
    }

    // Exact-set parity guard against drift between the static
    // `MCP_TOOLS` table and `docs/MCP_TOOLS.md` §1 (#215).
    #[test]
    fn mcp_tools_exact_set_matches_expected() {
        let actual: std::collections::BTreeSet<&str> = MCP_TOOLS.iter().map(|t| t.name).collect();
        let expected: std::collections::BTreeSet<&str> = [
            "doiget_resolve_paper",
            "doiget_fetch_paper",
            "doiget_metadata_only",
            "doiget_batch_fetch",
            "doiget_info",
            "doiget_search_local",
            "doiget_list_recent",
            "doiget_paper_pdf_path",
            "doiget_capability_profile",
            "doiget_health",
            "doiget_expand_citation_graph",
            "doiget_bibtex_export",
            "doiget_csl_export",
        ]
        .into_iter()
        .collect();
        assert_eq!(
            actual, expected,
            "MCP_TOOLS table drifted from the expected set; update both \
             `MCP_TOOLS` and this test together (and docs/MCP_TOOLS.md §1)."
        );
    }

    // Pin the `#[serde(tag = "status")]` wire shape: every variant
    // serialises to a `{"status":"…", …}` object. Accidentally
    // removing the `tag` attribute (or renaming the discriminant)
    // would silently degrade the wire format; this test catches it
    // (#215 N1).
    #[test]
    fn json_mode_serialises_with_status_discriminant() {
        let s = serde_json::to_string(&JsonMode::Artifact).expect("serialise");
        assert_eq!(
            s, r#"{"status":"artifact"}"#,
            "Artifact must emit a status-tagged object"
        );
        let s = serde_json::to_string(&JsonMode::Supported).expect("serialise");
        assert_eq!(s, r#"{"status":"supported"}"#);
    }

    // `arg_to_flag_spec` was generalised in #215 to harvest the
    // accepted values from clap's `PossibleValuesParser` instead of
    // hard-coding `--mode`. Pin the contract: the `--mode` entry in
    // `global_flags` MUST report `kind: Enum` with all four mode
    // strings. A future regression that silently degrades `--mode`
    // to `kind: String, values: None` would otherwise pass every
    // existing test (#215 N3).
    #[test]
    fn mode_flag_carries_enum_kind_and_all_four_values() {
        let global = &caps().global_flags;
        let mode = global
            .iter()
            .find(|f| f.name == "--mode")
            .expect("--mode flag is in global_flags");
        assert!(
            matches!(mode.kind, FlagKind::Enum),
            "--mode kind MUST be Enum, got {:?}",
            mode.kind
        );
        let vs = mode.values.as_ref().expect("--mode carries values");
        let mut sorted = vs.clone();
        sorted.sort();
        assert_eq!(sorted, vec!["human", "json", "mcp", "quiet"]);
    }

    // `compile_time_features()` pushes string literals that must
    // exactly match the Cargo feature names in `Cargo.toml`. A
    // typo in the literal (`"oa_only"` vs `"oa-only"`) would
    // silently invert the inventory's `features` field for every
    // consumer. The default build has `oa-only` active; assert
    // the literal round-trips (#215 A9).
    #[test]
    fn compile_time_features_contains_oa_only_under_default() {
        // `cfg!(feature = "oa-only")` is true in the default test
        // build; if a future maintainer disables the default feature
        // for the test target, this test becomes meaningless but
        // does not cause a false failure.
        if cfg!(feature = "oa-only") {
            let f = compile_time_features();
            assert!(
                f.contains(&"oa-only"),
                "oa-only feature was enabled at compile time but \
                 `compile_time_features()` did not list it: {f:?}"
            );
        }
    }

    #[test]
    fn version_is_cargo_pkg_version() {
        assert_eq!(caps().version, env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn every_test_cli_subcommand_has_metadata() {
        // Regression at the lib layer: anything we add to the shadow
        // `test_cli` must also be in `metadata_for`. The real
        // `Cli::command()` is exercised by the e2e test in
        // `tests/capabilities_e2e.rs`.
        for sub in test_cli().get_subcommands() {
            let name = sub.get_name();
            if name == "help" {
                continue;
            }
            assert!(
                metadata_for(name).is_some(),
                "subcommand `{name}` lacks metadata in `metadata_for`"
            );
        }
    }
}