rsleigh-cli 0.6.1

CLI decompiler — turn any binary into C-like pseudocode
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
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
//! Typed agent operation contracts. Dispatch validation, `--capabilities`,
//! generated usage text, and documentation drift tests all read this catalog,
//! so an accepted flag cannot be missing from discovery output.
use super::*;

/// Value accepted by a flag. Switches take no value.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Kind {
    Switch,
    /// Unsigned decimal integer.
    Count,
    /// `0x`-prefixed hexadecimal virtual address.
    Address,
    /// Function name or `0x` address in the current binary.
    Function,
    /// Local filesystem path.
    Path,
    /// Second input binary; evidence identifies it by content, not path.
    Binary,
    /// Free text or an identifier defined by the operation.
    Text,
}

impl Kind {
    fn id(self) -> &'static str {
        match self {
            Kind::Switch => "switch",
            Kind::Count => "count",
            Kind::Address => "address",
            Kind::Function => "function",
            Kind::Path => "path",
            Kind::Binary => "binary",
            Kind::Text => "text",
        }
    }
}

/// How a flag relates to evidence identity. Only semantic parameters enter
/// an evidence record's content ID; the rest are execution metadata.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Role {
    /// Selects or bounds the observation itself (target, selector, page, caps).
    Semantic,
    /// Affects cost or reuse only; any effect on output appears in diagnostics.
    Execution,
    /// Rendering choice; evidence records are always JSON.
    Output,
    /// Evidence capture, storage, and linking.
    Evidence,
}

impl Role {
    fn id(self) -> &'static str {
        match self {
            Role::Semantic => "semantic",
            Role::Execution => "execution",
            Role::Output => "output",
            Role::Evidence => "evidence",
        }
    }
}

#[derive(Clone, Copy)]
pub(super) struct Flag {
    pub name: &'static str,
    pub kind: Kind,
    pub placeholder: &'static str,
    pub help: &'static str,
    pub repeatable: bool,
    pub role: Role,
}

impl Flag {
    pub fn takes_value(&self) -> bool {
        self.kind != Kind::Switch
    }

    const fn role(self, role: Role) -> Flag {
        Flag { role, ..self }
    }
}

const fn switch(name: &'static str, help: &'static str) -> Flag {
    Flag {
        name,
        kind: Kind::Switch,
        placeholder: "",
        help,
        repeatable: false,
        role: Role::Semantic,
    }
}

const fn value(
    name: &'static str,
    kind: Kind,
    placeholder: &'static str,
    help: &'static str,
) -> Flag {
    Flag {
        name,
        kind,
        placeholder,
        help,
        repeatable: false,
        role: Role::Semantic,
    }
}

const fn repeated(
    name: &'static str,
    kind: Kind,
    placeholder: &'static str,
    help: &'static str,
) -> Flag {
    Flag {
        name,
        kind,
        placeholder,
        help,
        repeatable: true,
        role: Role::Semantic,
    }
}

/// Where the binary path appears relative to the mode flag.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Target {
    /// `rsleigh FILE --mode ...`
    Binary,
    /// `rsleigh --mode ...`; never reads a target binary.
    None,
    /// Either form; supplying FILE enables source-binding checks.
    OptionalBinary,
}

pub(super) struct Example {
    /// `FILE`, `FUNCTION`, and `DIR` are placeholders for caller values.
    pub argv: &'static [&'static str],
    pub exits: &'static [u8],
    pub description: &'static str,
}

pub(super) struct Operation {
    pub id: &'static str,
    pub mode: Flag,
    pub schema: &'static str,
    pub summary: &'static str,
    pub target: Target,
    pub positional: Option<&'static str>,
    pub flags: &'static [Flag],
    /// (effect, condition under which it occurs)
    pub effects: &'static [(&'static str, &'static str)],
    pub limits: &'static [(&'static str, u64)],
    pub examples: &'static [Example],
}

impl Operation {
    pub fn flag(&self, name: &str) -> Option<&Flag> {
        std::iter::once(&self.mode)
            .chain(self.flags)
            .find(|f| f.name == name)
    }

    /// One usage line generated from the typed contract.
    pub fn usage(&self) -> String {
        let mut line = String::from("rsleigh");
        match self.target {
            Target::Binary => line.push_str(" FILE"),
            Target::OptionalBinary => line.push_str(" [FILE]"),
            Target::None => {}
        }
        if let Some(positional) = self.positional {
            line.push(' ');
            line.push_str(positional);
        }
        line.push(' ');
        line.push_str(&flag_usage(&self.mode));
        for flag in self.flags {
            line.push_str(" [");
            line.push_str(&flag_usage(flag));
            line.push(']');
            if flag.repeatable {
                line.push_str("...");
            }
        }
        line
    }
}

fn flag_usage(flag: &Flag) -> String {
    if flag.takes_value() {
        format!("{} {}", flag.name, flag.placeholder)
    } else {
        flag.name.to_string()
    }
}

const JSON: Flag = switch(
    "--json",
    "Accepted for consistency; this operation always emits JSON",
)
.role(Role::Output);
const CACHE: Flag = value(
    "--analysis-cache",
    Kind::Path,
    "DIR",
    "Reuse verified analysis snapshots; publishes complete snapshots only",
)
.role(Role::Execution);
const MAX_DECODE: Flag = value(
    "--max-decode-instructions",
    Kind::Count,
    "N",
    "Stop decoding after N instructions (partial evidence)",
)
.role(Role::Execution);
const MAX_SSA: Flag = value(
    "--max-ssa-work",
    Kind::Count,
    "N",
    "Stop SSA construction/folding after N work units",
)
.role(Role::Execution);
const DEADLINE: Flag = value(
    "--deadline-ms",
    Kind::Count,
    "N",
    "Cooperative deadline checked between analysis stages",
)
.role(Role::Execution);
const EVIDENCE: Flag = switch(
    "--evidence",
    "Emit an rsleigh.evidence/v1 record wrapping the unchanged payload",
)
.role(Role::Evidence);
const EVIDENCE_STORE: Flag = value(
    "--evidence-store",
    Kind::Path,
    "DIR",
    "Emit the evidence record and persist it as DIR/records/<id>.json",
)
.role(Role::Evidence);
const DERIVED_FROM: Flag = repeated(
    "--derived-from",
    Kind::Text,
    "ID",
    "Link the record to stored evidence it builds on (requires --evidence-store)",
)
.role(Role::Evidence);
const HANDOFF: Flag =
    switch("--handoff", "Render a Markdown handoff instead of JSON").role(Role::Output);
const SHELL: Flag = value(
    "--shell",
    Kind::Text,
    "posix|powershell",
    "Shell for rendered next commands (default: platform)",
)
.role(Role::Output);
const REQUIRE: Flag = repeated(
    "--require",
    Kind::Text,
    "OPERATION",
    "Evidence operation an answer must cite (agent-brief, card, ssa-slice, compare)",
);
const EXIT_COMPLETE: &[u8] = &[0];
const EXIT_EVIDENCE: &[u8] = &[0, 2];

pub(super) const OPERATIONS: &[Operation] = &[
    Operation {
        id: "agent-brief",
        mode: switch("--agent-brief", "Select the agent brief operation"),
        schema: "rsleigh.agent-brief/v1",
        summary: "Bounded ranked function map, findings, file identity, and next commands",
        target: Target::Binary,
        positional: None,
        flags: &[
            value(
                "--limit",
                Kind::Count,
                "N",
                "Functions to return (1-100, default 25)",
            ),
            JSON,
            EVIDENCE,
            EVIDENCE_STORE,
            DERIVED_FROM,
        ],
        effects: &[("reads_binary", "always")],
        limits: &[
            ("functions_default", AGENT_BRIEF_DEFAULT_FUNCTIONS as u64),
            ("functions_cap", AGENT_BRIEF_MAX_FUNCTIONS as u64),
            ("findings_cap", AGENT_BRIEF_MAX_FINDINGS as u64),
            ("strings_per_function", 5),
            ("pseudocode_bytes", 0),
        ],
        examples: &[Example {
            argv: &["FILE", "--agent-brief", "--limit", "5"],
            exits: EXIT_EVIDENCE,
            description: "Rank where to start",
        }],
    },
    Operation {
        id: "card",
        mode: switch("--card", "Select the function card operation"),
        schema: "rsleigh.card/v2",
        summary: "Paginated instructions, raw P-code, and optional capped pseudocode for one function",
        target: Target::Binary,
        positional: Some("FUNCTION"),
        flags: &[
            switch("--json", "Emit rsleigh.card/v2 JSON instead of text").role(Role::Output),
            switch("--pcode", "Include raw P-code operations"),
            switch("--decompile", "Include capped pseudocode (hypothesis)"),
            value(
                "--instruction-cursor",
                Kind::Count,
                "N",
                "First instruction index of the page",
            ),
            value(
                "--operation-cursor",
                Kind::Count,
                "N",
                "First P-code operation index of the page (requires --pcode)",
            ),
            CACHE,
            MAX_DECODE,
            MAX_SSA,
            DEADLINE,
            EVIDENCE,
            EVIDENCE_STORE,
            DERIVED_FROM,
        ],
        effects: &[
            ("reads_binary", "always"),
            ("reads_debug_companions", "FILE.pdb or FILE.dSYM when present"),
            ("reads_cache", "with --analysis-cache"),
            ("writes_cache", "with --analysis-cache and complete analysis"),
        ],
        limits: &[
            ("instructions_per_page", AGENT_CARD_MAX_INSTRUCTIONS as u64),
            ("operations_per_page", AGENT_CARD_MAX_PCODE_OPS as u64),
            ("pseudocode_bytes", AGENT_CARD_MAX_PSEUDOCODE_BYTES as u64),
        ],
        examples: &[
            Example {
                argv: &["FILE", "FUNCTION", "--card", "--json", "--pcode"],
                exits: EXIT_EVIDENCE,
                description: "First page of instructions and raw P-code",
            },
            Example {
                argv: &[
                    "FILE",
                    "FUNCTION",
                    "--card",
                    "--json",
                    "--pcode",
                    "--decompile",
                    "--operation-cursor",
                    "0",
                ],
                exits: EXIT_EVIDENCE,
                description: "Add capped pseudocode as a hypothesis",
            },
            Example {
                argv: &["FILE", "FUNCTION", "--card", "--pcode", "--evidence-store", "STORE"],
                exits: EXIT_EVIDENCE,
                description: "Record the card as content-addressed evidence",
            },
        ],
    },
    Operation {
        id: "xrefs",
        mode: value(
            "--xrefs",
            Kind::Function,
            "FUNCTION",
            "Function whose callers, callees, and strings to report",
        ),
        schema: "rsleigh.xrefs/v1",
        summary: "Direct callers of one function, plus the callees and strings in its pseudocode",
        target: Target::Binary,
        positional: None,
        flags: &[
            switch("--json", "Emit rsleigh.xrefs/v1 JSON instead of text").role(Role::Output),
        ],
        effects: &[("reads_binary", "always")],
        limits: &[("scan_bytes_per_function", XREF_SCAN_BYTES as u64)],
        examples: &[Example {
            argv: &["FILE", "--xrefs", "FUNCTION", "--json"],
            exits: EXIT_EVIDENCE,
            description: "Who calls this function, and what it calls",
        }],
    },
    Operation {
        id: "search",
        mode: switch("--search", "Select the function search operation"),
        schema: "rsleigh.search/v1",
        summary: "Functions that use a string, call an API, contain a constant, or carry a behavior tag",
        target: Target::Binary,
        positional: Some("QUERY"),
        flags: &[
            switch("--api", "Match functions that call the API named by QUERY"),
            switch("--const", "Match functions whose bytes contain the constant QUERY"),
            switch("--tag", "Match functions tagged with any of the comma-separated QUERY tags"),
            switch("--decompile", "Include each match's pseudocode (hypothesis)"),
            switch("--json", "Emit rsleigh.search/v1 JSON instead of text").role(Role::Output),
        ],
        effects: &[("reads_binary", "always")],
        limits: &[("scan_bytes_per_function", XREF_SCAN_BYTES as u64)],
        examples: &[
            Example {
                argv: &["FILE", "--search", "password", "--json"],
                exits: EXIT_EVIDENCE,
                description: "Functions that reference a string",
            },
            Example {
                argv: &["FILE", "--search", "--api", "recv", "--json"],
                exits: EXIT_EVIDENCE,
                description: "Functions that call an API",
            },
        ],
    },
    Operation {
        id: "index",
        mode: value(
            "--index",
            Kind::Path,
            "DIR",
            "Publish a new index generation into DIR",
        ),
        schema: "rsleigh.index/v2",
        summary: "Reusable functions, direct-call xrefs, imports, and findings artifacts",
        target: Target::Binary,
        positional: None,
        flags: &[
            value(
                "--limit",
                Kind::Count,
                "N",
                "Functions to retain (1-10000, default 10000)",
            ),
            JSON,
        ],
        effects: &[
            ("reads_binary", "always"),
            ("writes_directory", "new immutable generation plus atomic index.json"),
        ],
        limits: &[
            ("functions_cap", AGENT_INDEX_MAX_FUNCTIONS as u64),
            ("findings_cap", AGENT_INDEX_MAX_FINDINGS as u64),
        ],
        examples: &[Example {
            argv: &["FILE", "--index", "DIR", "--limit", "100"],
            exits: EXIT_EVIDENCE,
            description: "Write a reusable map",
        }],
    },
    Operation {
        id: "verify-index",
        mode: value(
            "--verify-index",
            Kind::Path,
            "DIR",
            "Index directory to verify against FILE",
        ),
        schema: "rsleigh.index-verification/v1",
        summary: "Check index binary identity, tool version, generation, and artifact checksums",
        target: Target::Binary,
        positional: None,
        flags: &[JSON],
        effects: &[("reads_binary", "always"), ("reads_directory", "always")],
        limits: &[],
        examples: &[Example {
            argv: &["FILE", "--verify-index", "DIR"],
            exits: EXIT_COMPLETE,
            description: "Verify the index written above",
        }],
    },
    Operation {
        id: "ssa-slice",
        mode: value(
            "--ssa-slice",
            Kind::Function,
            "FUNCTION",
            "Function whose post-fold SSA is queried",
        ),
        schema: "rsleigh.ssa-slice/v3",
        summary: "Bounded backward data dependencies with raw P-code origins and explicit boundaries",
        target: Target::Binary,
        positional: None,
        flags: &[
            value("--var", Kind::Count, "ID", "Root: SSA variable ID"),
            value(
                "--call-site",
                Kind::Address,
                "0xADDRESS",
                "Root: call instruction (with --arg)",
            ),
            value(
                "--arg",
                Kind::Count,
                "INDEX",
                "Zero-based integer argument (with --call-site)",
            ),
            switch("--return", "Root: returned value"),
            value(
                "--at",
                Kind::Address,
                "0xADDRESS",
                "Return instruction (with --return)",
            ),
            value(
                "--condition",
                Kind::Address,
                "0xADDRESS",
                "Root: conditional branch",
            ),
            CACHE,
            MAX_DECODE,
            MAX_SSA,
            DEADLINE,
            value(
                "--max-nodes",
                Kind::Count,
                "N",
                "Dependency node limit (default 64, cap 256)",
            ),
            value(
                "--max-depth",
                Kind::Count,
                "N",
                "Expression depth limit (default 16, cap 32)",
            ),
            value(
                "--max-call-depth",
                Kind::Count,
                "N",
                "Helper call depth (default 2, cap 8)",
            ),
            value(
                "--max-functions",
                Kind::Count,
                "N",
                "Functions admitted to traversal (default 16, cap 32)",
            ),
            value(
                "--max-traversal-work",
                Kind::Count,
                "N",
                "Traversal work units (default 100000, cap 1000000)",
            ),
            JSON,
            EVIDENCE,
            EVIDENCE_STORE,
            DERIVED_FROM,
        ],
        effects: &[
            ("reads_binary", "always"),
            ("reads_cache", "with --analysis-cache"),
            ("writes_cache", "with --analysis-cache and complete snapshots"),
        ],
        limits: &[
            ("max_nodes_default", 64),
            ("max_nodes_cap", rsleigh_decompile::slice::MAX_NODES as u64),
            ("max_depth_default", 16),
            ("max_depth_cap", rsleigh_decompile::slice::MAX_DEPTH as u64),
            ("max_call_depth_default", 2),
            ("max_call_depth_cap", 8),
            ("max_functions_default", 16),
            ("max_functions_cap", 32),
            ("max_traversal_work_default", 100_000),
            ("max_traversal_work_cap", 1_000_000),
            (
                "origins_per_node",
                rsleigh_decompile::provenance::MAX_ORIGINS as u64,
            ),
        ],
        examples: &[
            Example {
                argv: &["FILE", "--ssa-slice", "FUNCTION", "--return"],
                exits: EXIT_EVIDENCE,
                description: "Trace the value returned by a single-return function",
            },
            Example {
                argv: &["FILE", "--ssa-slice", "FUNCTION", "--return", "--evidence-store", "STORE"],
                exits: EXIT_EVIDENCE,
                description: "Record the slice, including unresolved boundaries, as evidence",
            },
        ],
    },
    Operation {
        id: "compare",
        mode: value(
            "--compare",
            Kind::Binary,
            "NEW",
            "Binary to compare against FILE (the old side)",
        ),
        schema: "rsleigh.function-comparison/v1",
        summary: "Compare explicitly paired or uniquely named functions by bytes, instructions, calls, strings, CFG, and pseudocode",
        target: Target::Binary,
        positional: None,
        flags: &[
            repeated(
                "--pair",
                Kind::Text,
                "OLD[:NEW]",
                "Function pair by name or 0x address; default: unique shared symbol names",
            ),
            value(
                "--limit",
                Kind::Count,
                "N",
                "Pairs to compare (default 100, cap 10000)",
            ),
            switch("--json", "Emit rsleigh.function-comparison/v1 JSON instead of text").role(Role::Output),
            EVIDENCE,
            EVIDENCE_STORE,
            DERIVED_FROM,
        ],
        effects: &[
            ("reads_binary", "FILE and NEW"),
            ("reads_debug_companions", "PDB or dSYM next to either input"),
        ],
        limits: &[
            ("pairs_default", 100),
            ("pairs_cap", 10_000),
            ("diff_lines_per_pair", 400),
            ("lcs_cells_per_pair", 2_000_000),
        ],
        examples: &[
            Example {
                argv: &["FILE", "--compare", "FILE", "--json"],
                exits: EXIT_EVIDENCE,
                description: "Compare every uniquely named function",
            },
            Example {
                argv: &["FILE", "--compare", "FILE", "--pair", "FUNCTION", "--json"],
                exits: EXIT_EVIDENCE,
                description: "Compare one explicit pair",
            },
        ],
    },
    Operation {
        id: "export-bundle",
        mode: value(
            "--export-bundle",
            Kind::Path,
            "BUNDLE",
            "Publish a new bundle generation into BUNDLE",
        ),
        schema: "rsleigh.evidence-bundle/v1",
        summary: "Export selected evidence records and their referenced dependencies for one binary",
        target: Target::Binary,
        positional: None,
        flags: &[
            value(
                "--evidence-store",
                Kind::Path,
                "STORE",
                "Store holding the records (required)",
            ),
            repeated(
                "--record",
                Kind::Text,
                "ID",
                "Record to export; default is every record for FILE",
            ),
            value(
                "--investigation",
                Kind::Path,
                "STORE",
                "Also export this investigation, its revisions, and cited evidence",
            ),
            JSON,
        ],
        effects: &[
            ("reads_binary", "always (subject hash only)"),
            ("reads_directory", "the evidence store"),
            ("writes_directory", "new immutable generation plus atomic index.json"),
        ],
        limits: &[],
        examples: &[Example {
            argv: &["FILE", "--export-bundle", "BUNDLE", "--evidence-store", "STORE"],
            exits: EXIT_COMPLETE,
            description: "Package every stored record for FILE",
        }],
    },
    Operation {
        id: "verify-bundle",
        mode: value(
            "--verify-bundle",
            Kind::Path,
            "BUNDLE",
            "Bundle directory to verify offline",
        ),
        schema: "rsleigh.bundle-verification/v1",
        summary: "Verify bundle checksums, content IDs, references, and coverage; FILE adds source binding",
        target: Target::OptionalBinary,
        positional: None,
        flags: &[JSON],
        effects: &[
            ("reads_directory", "the bundle"),
            ("reads_binary", "only when FILE is supplied (hash only)"),
        ],
        limits: &[],
        examples: &[
            Example {
                argv: &["--verify-bundle", "BUNDLE"],
                exits: &[2],
                description: "Integrity only: exits 2 because source binding was not checked",
            },
            Example {
                argv: &["FILE", "--verify-bundle", "BUNDLE"],
                exits: EXIT_COMPLETE,
                description: "Integrity plus source binary identity",
            },
        ],
    },
    Operation {
        id: "import-bundle",
        mode: value(
            "--import-bundle",
            Kind::Path,
            "BUNDLE",
            "Verified bundle to copy into a local store",
        ),
        schema: "rsleigh.bundle-import/v1",
        summary: "Copy verified records into a local evidence store without executing saved commands",
        target: Target::OptionalBinary,
        positional: None,
        flags: &[
            value(
                "--evidence-store",
                Kind::Path,
                "STORE",
                "Destination store for imported records",
            ),
            value(
                "--investigation",
                Kind::Path,
                "STORE",
                "Empty directory to receive the bundled investigation and its records",
            ),
            JSON,
        ],
        effects: &[
            ("reads_directory", "the bundle"),
            ("writes_directory", "STORE/records; existing records are kept"),
        ],
        limits: &[],
        examples: &[Example {
            argv: &["--import-bundle", "BUNDLE", "--evidence-store", "STORE"],
            exits: EXIT_COMPLETE,
            description: "Import into a store; identical records are kept as existing",
        }],
    },
    Operation {
        id: "investigate",
        mode: value(
            "--investigate",
            Kind::Path,
            "STORE",
            "New investigation directory (also its evidence store)",
        ),
        schema: "rsleigh.investigation-view/v1",
        summary: "Start a durable investigation bound to FILE's identity",
        target: Target::Binary,
        positional: None,
        flags: &[
            value("--question", Kind::Text, "TEXT", "The question to answer (required)"),
            REQUIRE,
            HANDOFF,
            SHELL,
            JSON,
        ],
        effects: &[
            ("reads_binary", "always (identity only)"),
            ("writes_directory", "STORE/revisions/000001.json and STORE/investigation.json"),
        ],
        limits: &[],
        examples: &[Example {
            argv: &["FILE", "--investigate", "STORE", "--question", "What does FUNCTION return?"],
            exits: EXIT_COMPLETE,
            description: "Open an investigation whose evidence will live in STORE/records",
        }],
    },
    Operation {
        id: "investigation",
        mode: value(
            "--investigation",
            Kind::Path,
            "STORE",
            "Investigation directory to show or update",
        ),
        schema: "rsleigh.investigation-view/v1",
        summary: "Show an investigation with next queries, render a handoff, or apply one revision-checked update",
        target: Target::OptionalBinary,
        positional: None,
        flags: &[
            HANDOFF,
            SHELL,
            value(
                "--expect-revision",
                Kind::Count,
                "N",
                "Required for updates; rejects a stale revision",
            ),
            value("--add-question", Kind::Text, "TEXT", "Action: add an analyst question"),
            REQUIRE,
            value(
                "--add-unknowns",
                Kind::Text,
                "ID",
                "Action: track unresolved boundaries from a stored slice record",
            ),
            repeated(
                "--node",
                Kind::Count,
                "INDEX",
                "Slice node to track (default: every boundary node)",
            ),
            value("--observe", Kind::Text, "TEXT", "Action: record an observation (needs --cite)"),
            value("--infer", Kind::Text, "TEXT", "Action: record a provisional analyst inference"),
            value("--support", Kind::Text, "ISSUE", "Action: attach supporting evidence"),
            value("--contradict", Kind::Text, "ISSUE", "Action: attach contradicting evidence"),
            value("--resolve", Kind::Text, "ISSUE", "Action: close an issue with --disposition"),
            value(
                "--disposition",
                Kind::Text,
                "answered|withdrawn|out-of-scope",
                "Closing state; only answered checks evidence",
            ),
            value("--note", Kind::Text, "TEXT", "Resolution note"),
            repeated("--cite", Kind::Text, "ID", "Evidence record in STORE/records"),
            JSON,
        ],
        effects: &[
            ("reads_directory", "the investigation and its records"),
            ("reads_binary", "only when FILE is supplied (identity check)"),
            ("writes_directory", "one new immutable revision per update"),
        ],
        limits: &[],
        examples: &[
            Example {
                argv: &["--investigation", "STORE"],
                exits: EXIT_COMPLETE,
                description: "Reopen: pending questions and next queries without rediscovery",
            },
            Example {
                argv: &[
                    "--investigation",
                    "STORE",
                    "--expect-revision",
                    "1",
                    "--add-question",
                    "Which callers supply its input?",
                ],
                exits: EXIT_COMPLETE,
                description: "Revision-checked update",
            },
        ],
    },
];

/// Stable machine-readable failure classes. Messages may change; codes may not.
pub(super) const ERROR_CODES: &[(&str, &str)] = &[
    ("multiple_modes", "More than one operation was selected"),
    (
        "unknown_option",
        "A target-free invocation named no known operation",
    ),
    (
        "unsupported_option",
        "The flag is not part of the selected operation's contract",
    ),
    (
        "unexpected_argument",
        "A positional argument is not accepted here",
    ),
    ("missing_value", "A flag that requires a value has none"),
    (
        "duplicate_option",
        "A non-repeatable flag appears more than once",
    ),
    ("invalid_value", "A flag value has the wrong type or range"),
    (
        "invalid_selector",
        "Slice root flags do not select exactly one supported root",
    ),
    (
        "invalid_combination",
        "Individually valid flags cannot be combined",
    ),
    (
        "invalid_target",
        "The function target is missing, repeated, or malformed",
    ),
    (
        "target_not_found",
        "No discovered function matches the target",
    ),
    (
        "invalid_cursor",
        "A pagination cursor is beyond the available evidence",
    ),
    (
        "unsupported_input",
        "The input frontend does not implement agent operations",
    ),
    (
        "input_unreadable",
        "The binary or a referenced file cannot be read",
    ),
    (
        "unsupported_format",
        "The container or architecture is not supported",
    ),
    (
        "verification_failed",
        "Stored evidence failed an integrity or identity check",
    ),
    (
        "missing_evidence",
        "Referenced evidence is unavailable in the store or bundle",
    ),
    (
        "stale_revision",
        "The investigation changed since the expected revision",
    ),
    (
        "requirements_unmet",
        "Cited evidence does not satisfy the question's requirements",
    ),
    (
        "write_failed",
        "Publishing output files failed; previous output is unchanged",
    ),
    (
        "execution_limit",
        "An execution limit stopped analysis before any evidence was produced",
    ),
    ("analysis_failed", "Analysis could not produce any evidence"),
    (
        "command_failed",
        "Unclassified failure; inspect the error message",
    ),
];

pub(super) fn find(mode: &str) -> Option<&'static Operation> {
    OPERATIONS.iter().find(|op| op.mode.name == mode)
}

/// A failed agent command. `code` is stable; `message` is for humans.
#[derive(Debug)]
pub(super) struct AgentError {
    pub code: &'static str,
    pub message: String,
}

impl AgentError {
    pub fn new(code: &'static str, message: impl Into<String>) -> Self {
        debug_assert!(ERROR_CODES.iter().any(|(c, _)| *c == code), "{code}");
        Self {
            code,
            message: message.into(),
        }
    }
}

impl std::fmt::Display for AgentError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl From<String> for AgentError {
    fn from(message: String) -> Self {
        Self::new("command_failed", message)
    }
}

impl From<&str> for AgentError {
    fn from(message: &str) -> Self {
        Self::new("command_failed", message)
    }
}

impl From<AgentError> for String {
    fn from(error: AgentError) -> Self {
        error.message
    }
}

/// Validate argv after the binary (or target-free mode) against `op`.
/// Returns positional arguments. Value checks match the typed flag kinds.
pub(super) fn validate<'a>(
    op: &Operation,
    args: &'a [String],
    start: usize,
) -> Result<Vec<&'a str>, AgentError> {
    let mut positionals = Vec::new();
    let mut i = start;
    while i < args.len() {
        let arg = args[i].as_str();
        if arg.starts_with("--") {
            let flag = op.flag(arg).ok_or_else(|| {
                AgentError::new(
                    "unsupported_option",
                    format!("unsupported {} option: {arg}", op.mode.name),
                )
            })?;
            if flag.takes_value() {
                if flag.repeatable {
                    args.get(i + 1)
                        .filter(|a| !a.starts_with("--"))
                        .ok_or_else(|| {
                            AgentError::new("missing_value", format!("{arg} requires a value"))
                        })?;
                } else {
                    value_arg(args, arg)?;
                }
                i += 1;
            }
        } else if op.positional.is_some() {
            positionals.push(arg);
        } else {
            return Err(AgentError::new(
                "unexpected_argument",
                format!("unexpected argument: {arg}"),
            ));
        }
        i += 1;
    }
    for flag in std::iter::once(&op.mode).chain(op.flags) {
        if flag.kind == Kind::Count && !flag.repeatable && value_arg(args, flag.name)?.is_some() {
            number_arg(args, flag.name, 0)?;
        }
    }
    if op.flag("--limit").is_some()
        && value_arg(args, "--limit")?.is_some()
        && number_arg(args, "--limit", 1)? == 0
    {
        return Err(AgentError::new("invalid_value", "--limit must be positive"));
    }
    Ok(positionals)
}

fn effects_json(op: &Operation) -> Value {
    json!(op
        .effects
        .iter()
        .map(|(effect, condition)| json!({"effect":effect,"condition":condition}))
        .collect::<Vec<_>>())
}

fn flag_json(flag: &Flag) -> Value {
    json!({"name":flag.name,"kind":flag.kind.id(),"role":flag.role.id(),
        "value":flag.takes_value().then_some(flag.placeholder),
        "repeatable":flag.repeatable,"help":flag.help})
}

pub(super) fn operation_json(op: &Operation) -> Value {
    let limits: serde_json::Map<String, Value> = op
        .limits
        .iter()
        .map(|(name, value)| (name.to_string(), json!(value)))
        .collect();
    json!({
        "id":op.id, "mode":flag_json(&op.mode), "schema":op.schema, "summary":op.summary,
        "target":match op.target { Target::Binary=>"binary", Target::None=>"none", Target::OptionalBinary=>"optional_binary" },
        "positional":op.positional, "usage":op.usage(),
        "flags":op.flags.iter().map(flag_json).collect::<Vec<_>>(),
        "effects":effects_json(op), "limits":limits,
        "examples":op.examples.iter().map(|e| json!({"argv":e.argv,"exits":e.exits,"description":e.description})).collect::<Vec<_>>(),
    })
}

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

    #[test]
    fn catalog_is_internally_consistent() {
        let mut modes = std::collections::HashSet::new();
        let mut ids = std::collections::HashSet::new();
        for op in OPERATIONS {
            assert!(
                modes.insert(op.mode.name),
                "duplicate mode {}",
                op.mode.name
            );
            assert!(ids.insert(op.id), "duplicate id {}", op.id);
            assert!(op.schema.starts_with("rsleigh.") && op.schema.contains("/v"));
            let mut flags = std::collections::HashSet::from([op.mode.name]);
            for flag in op.flags {
                assert!(
                    flags.insert(flag.name),
                    "{}: duplicate {}",
                    op.id,
                    flag.name
                );
                assert!(flag.name.starts_with("--"));
                assert_eq!(flag.takes_value(), !flag.placeholder.is_empty());
            }
            assert!(!op.examples.is_empty(), "{} needs an example", op.id);
            for example in op.examples {
                let start = usize::from(example.argv[0] == "FILE");
                match op.target {
                    Target::Binary => assert_eq!(start, 1),
                    Target::None => assert_eq!(start, 0),
                    Target::OptionalBinary => {}
                }
                assert!(example.argv.contains(&op.mode.name), "{}", op.id);
                let argv: Vec<String> = std::iter::once("rsleigh")
                    .chain(example.argv.iter().copied())
                    .map(str::to_string)
                    .collect();
                validate(op, &argv, start + 1)
                    .unwrap_or_else(|e| panic!("{} example rejected: {e}", op.id));
            }
        }
    }

    #[test]
    fn validation_reports_stable_codes() {
        let card = find("--card").unwrap();
        let brief = find("--agent-brief").unwrap();
        let argv = |v: &[&str]| -> Vec<String> { v.iter().map(|s| s.to_string()).collect() };
        for (op, args, code) in [
            (
                card,
                argv(&["rsleigh", "f", "x", "--card", "--bogus"]),
                "unsupported_option",
            ),
            (
                brief,
                argv(&["rsleigh", "f", "--agent-brief", "x"]),
                "unexpected_argument",
            ),
            (
                brief,
                argv(&["rsleigh", "f", "--agent-brief", "--limit"]),
                "missing_value",
            ),
            (
                brief,
                argv(&[
                    "rsleigh",
                    "f",
                    "--agent-brief",
                    "--limit",
                    "1",
                    "--limit",
                    "2",
                ]),
                "duplicate_option",
            ),
            (
                brief,
                argv(&["rsleigh", "f", "--agent-brief", "--limit", "oops"]),
                "invalid_value",
            ),
            (
                brief,
                argv(&["rsleigh", "f", "--agent-brief", "--limit", "0"]),
                "invalid_value",
            ),
        ] {
            assert_eq!(validate(op, &args, 2).unwrap_err().code, code, "{args:?}");
        }
    }
}