vetto 0.2.9

Daemon-less sandbox + security layer for AI coding agents (Landlock/Seatbelt, TUI statusline, post-session audit reports)
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
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
pub mod enable;
pub mod git_hook;
pub mod hook;
pub mod plugin;
pub mod shell_env;
pub mod status;
pub mod why_slow;

pub use enable::{DisableArgs, EnableArgs};
pub use hook::{HookCommand, HookScope, ShellType};

use clap::{CommandFactory, Parser, Subcommand};
use clap_complete::Shell;
use std::path::PathBuf;

const HELP_ABOUT: &str = "\
1. vetto enable <agent>
2. Run your agent as usual — it runs in the sandbox under the hood.

Daemon-less sandbox + security layer for AI coding agents.";

const EXAMPLES: &str = "\
Examples:
  vetto enable claude
  vetto enable codex
  claude
  vetto doctor
  vetto tour
  vetto status
  vetto allow ./target
  vetto deny ~/.aws/credentials
  vetto -- python agent.py";

/// vetto - daemon-less sandbox + security layer for AI coding agents.
#[derive(Parser, Debug)]
#[command(
    name = "vetto",
    version,
    about = HELP_ABOUT,
    after_help = EXAMPLES
)]
pub struct Cli {
    /// Built-in policy profile
    #[arg(long, default_value = "default", value_name = "NAME")]
    pub profile: String,

    /// Base security preset: paranoid | balanced | yolo
    #[arg(long, value_name = "PRESET")]
    pub preset: Option<String>,

    /// Explicit policy TOML layer applied after the profile and project policy
    #[arg(long, value_name = "PATH")]
    pub policy: Option<String>,

    /// Network mode: off | allowlist:<domain,domain,...> |
    /// strict:<domain:port,domain:port,...>
    #[arg(long, value_name = "MODE")]
    pub net: Option<String>,

    /// UI mode: statusline | full | none
    #[arg(long, value_name = "MODE", default_value = "statusline")]
    pub tui: String,

    /// Explicit sandbox backend: auto | process | win-sandbox
    #[arg(long, value_name = "BACKEND")]
    pub backend: Option<String>,

    /// Emit events to macOS unified log (os_log / logger)
    #[arg(long)]
    pub oslog: bool,

    /// Run Windows AppContainer in Less Privileged AppContainer (LPAC) mode
    #[arg(long)]
    pub lpac: bool,

    /// Attach a best-effort blocked-attempt observation tap (Linux).
    /// Observation ONLY — Landlock remains the sole enforcer.
    #[arg(long)]
    pub observe_seccomp: bool,

    /// Shadow mode: policy layer logs "would deny" instead of blocking in verification/preflight.
    /// Note: Kernel sandbox (Landlock/seccomp) cannot be shadowed; shadow mode applies to policy-layer verification.
    #[arg(long)]
    pub shadow: bool,

    /// Append every session event as JSON lines to PATH
    #[arg(long, value_name = "PATH")]
    pub jsonl: Option<String>,

    /// Post-session report formats, comma separated: html,md,json,sarif
    #[arg(long, value_name = "FMTS")]
    pub report: Option<String>,

    /// Directory in which reports are stored (defaults to $PROJECT/.vetto/reports).
    #[arg(long, value_name = "PATH")]
    pub report_dir: Option<String>,

    /// Compatibility spelling for report retention cleanup.
    #[arg(long, alias = "report-cleanup")]
    pub report_auto_cleanup: bool,

    /// Keep reports without automatic retention cleanup.
    #[arg(
        long = "no-report-auto-cleanup",
        conflicts_with = "report_auto_cleanup"
    )]
    pub no_report_auto_cleanup: bool,

    /// Compatibility spelling for report cleanup (kept hidden from help).
    #[arg(long = "auto-cleanup", hide = true)]
    pub auto_cleanup: bool,

    /// Maximum number of reports to retain (default: 50).
    #[arg(long, value_name = "COUNT")]
    pub report_retention: Option<usize>,

    /// Remove reports older than this many seconds when cleanup is enabled.
    #[arg(long, value_name = "SECONDS")]
    pub report_max_age_secs: Option<u64>,

    /// Exit non-zero when at least THRESHOLD blocked attempts are observed.
    /// With no value, THRESHOLD defaults to 1.
    #[arg(
        long,
        value_name = "THRESHOLD",
        num_args = 0..=1,
        default_missing_value = "1"
    )]
    pub fail_on_block: Option<u64>,

    /// Route git/SSH connections through vetto's in-process relay helper.
    #[arg(long)]
    pub git_ssh: bool,

    /// Desktop notifications on security violations (blocked path access, network escape).
    #[arg(long)]
    pub notify: bool,

    /// OpenTelemetry OTLP endpoint for session span export.
    #[arg(long, value_name = "URL")]
    pub otel_endpoint: Option<String>,

    /// Kill the sandboxed session after DURATION without the agent finishing
    /// (e.g. 90s, 30m, 2h). Enforced with --tui=none (CI mode); other TUI
    /// modes warn and ignore it.
    #[arg(long, value_name = "DURATION")]
    pub timeout: Option<String>,

    /// Resource ceilings for the agent process, comma separated:
    /// cpu=SECONDS, as=BYTES, procs=N, nofile=N, fsize=BYTES. Merged
    /// strictest-wins with any limits from the policy layers.
    #[arg(long, value_name = "SPEC")]
    pub limits: Option<String>,

    /// Run the boundary verification battery against the resolved policy
    /// before spawning the agent. Any leak aborts the session (fail-closed).
    #[arg(long)]
    pub verify: bool,

    /// Print resolved policy + tier plan and exit (nothing enforced)
    #[arg(long)]
    pub dry_run: bool,

    /// Non-interactive mode for CI: implies --tui=none and a JSON summary on stdout
    #[arg(long)]
    pub ci: bool,

    /// Suppress diagnostic and non-essential progress messages on stderr
    #[arg(short = 'q', long, global = true, conflicts_with = "verbose")]
    pub quiet: bool,

    /// Verbose diagnostics on stderr
    #[arg(short = 'v', long, global = true)]
    pub verbose: bool,

    /// Forward session events to system journal (journald, EventLog, syslog)
    #[arg(long)]
    pub system_log: bool,

    /// Select an agent preset, or provide NAME=PROGRAM entries with --multi.
    #[arg(long = "agent", value_name = "NAME", action = clap::ArgAction::Append)]
    pub agents: Vec<String>,

    /// Run the compatibility multi-agent frontend without a `multi` subcommand.
    #[arg(long)]
    pub multi: bool,

    /// Multi-agent TOML manifest for the compatibility frontend.
    #[arg(long = "manifest", value_name = "PATH")]
    pub multi_manifest: Option<PathBuf>,

    /// Additional glob patterns to resolve and deny (e.g. "**/*.pem").
    #[arg(long = "deny-glob", value_name = "PATTERN", action = clap::ArgAction::Append)]
    pub deny_glob: Vec<String>,

    /// Enforce Git branch protection (refuse write on main/master) and block destructive git pushes.
    #[arg(long = "git-guard")]
    pub git_guard: bool,

    /// Take a project snapshot before session starts and enable rollback.
    #[arg(long = "snapshot")]
    pub snapshot: bool,

    /// Automatically scan project for secrets at session start and deny them.
    #[arg(long = "auto-deny-secrets")]
    pub auto_deny_secrets: bool,

    /// Target remote daemon API endpoint URL (e.g. http://127.0.0.1:54321)
    #[arg(long, value_name = "URL")]
    pub remote: Option<String>,

    #[command(subcommand)]
    pub command: Option<Command>,

    /// Agent command to supervise; everything after `--`
    #[arg(last = true, value_name = "COMMAND [ARGS...]")]
    pub agent: Vec<String>,
}

#[derive(Subcommand, Debug)]
pub enum Command {
    /// Enable transparent sandbox wrapper for an AI coding agent (e.g. `vetto enable claude`)
    Enable(EnableArgs),

    /// Disable transparent sandbox wrapper for an AI coding agent (e.g. `vetto disable claude`)
    Disable(DisableArgs),

    /// Grant the agent access to a path or network domain (writes policy)
    Allow {
        /// Filesystem path, or network domain with --net
        #[arg(value_name = "PATH|DOMAIN")]
        target: String,
        /// Filesystem only: read-only grant (default is read + write)
        #[arg(long)]
        read_only: bool,
        /// Treat TARGET as a network domain instead of a path
        #[arg(long)]
        net: bool,
        /// Edit ~/.vetto/config.toml instead of the project policy
        #[arg(long)]
        global: bool,
    },
    /// Explicitly deny reads of a path (secret masking) in the policy
    Deny {
        /// Filesystem path to mask, e.g. ~/.aws/credentials
        #[arg(value_name = "PATH")]
        target: String,
        /// Edit ~/.vetto/config.toml instead of the project policy
        #[arg(long)]
        global: bool,
    },
    /// Diagnose platform support: tiers, landlock ABI, userns, seccomp, audit feed
    Doctor {
        /// Additionally verify that every display_only_deny path is truly
        /// unreachable from inside a throwaway sandbox.
        #[arg(long)]
        probe: bool,
        /// Probe a known agent executable with a bounded --version command.
        #[arg(long = "check-agent", value_name = "NAME")]
        check_agent: Option<String>,
        /// Show concrete remediation commands and steps for missing sandbox primitives.
        #[arg(long)]
        fix: bool,
    },
    /// Interactive 5-step onboarding walkthrough
    Tour {
        /// Run all tour steps non-interactively without waiting for keypresses
        #[arg(long)]
        non_interactive: bool,
    },
    /// List active sandboxed sessions and cleanup stale metadata.
    Status {
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Verify the sandbox boundary WITHOUT running any agent: secret paths,
    /// network reachability, and write-outside checks execute inside a
    /// throwaway sandbox built from the resolved policy.
    Verify {
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Run an agent command under the Vetto sandbox supervisor
    #[command(hide = true)]
    Run {
        /// Target agent binary or command
        #[arg(value_name = "COMMAND")]
        command: Option<String>,

        /// Arguments passed to the agent
        #[arg(last = true, value_name = "ARGS")]
        args: Vec<String>,
    },
    /// Analyze project ecosystem and generate a tailored policy.toml policy
    #[command(hide = true)]
    Init {
        /// Overwrite existing policy if present
        #[arg(long, short = 'f')]
        force: bool,
        /// Interactive first-run setup wizard
        #[arg(long)]
        wizard: bool,
    },
    /// List built-in policy profiles
    #[command(hide = true)]
    Profiles,
    /// Manage transparent developer shims, shell hooks, and Git hook wrappers
    #[command(hide = true)]
    Hook {
        #[command(subcommand)]
        command: HookCommand,
    },
    /// Manage agent integration plugins (Claude Code, OpenCode)
    #[command(hide = true)]
    Plugin {
        #[command(subcommand)]
        command: plugin::PluginCommand,
    },
    /// Run as a Model Context Protocol (MCP) JSON-RPC stdio server
    #[command(hide = true)]
    Mcp,
    /// Manage background session multiplexer daemon and session registry
    #[command(hide = true)]
    Daemon {
        #[command(subcommand)]
        command: crate::daemon::DaemonCommand,
    },
    /// Run multiplexer daemon in foreground with SSH remote instructions
    #[command(hide = true)]
    Serve {
        /// Loopback HTTP port for REST API (default: 54321)
        #[arg(long, default_value_t = crate::daemon::DEFAULT_HTTP_PORT)]
        port: u16,
    },
    /// Fast native shim dispatcher for intercepted toolchain binaries
    #[command(hide = true)]
    Shim {
        /// Target binary name (if not inferred from argv[0])
        #[arg(value_name = "BINARY")]
        binary: Option<String>,

        /// Arguments passed to the target binary
        #[arg(last = true, value_name = "ARGS")]
        args: Vec<String>,
    },
    /// Run named agents concurrently, each in an independent sandbox.
    #[command(hide = true)]
    Multi {
        /// TOML manifest containing one or more [[agents]] argv definitions.
        #[arg(long, value_name = "PATH", conflicts_with = "agents")]
        manifest: Option<PathBuf>,
        /// Explicit repeated executable form: NAME=PROGRAM. Arguments and
        /// per-agent policies belong in the manifest; no shell is involved.
        #[arg(long = "agent", value_name = "NAME=PROGRAM", action = clap::ArgAction::Append)]
        agents: Vec<String>,
        /// Compatibility form for exactly one argv command. A literal `--`
        /// inside this vector is rejected as ambiguous by the manifest parser.
        #[arg(last = true, value_name = "COMMAND [ARGS...]")]
        command: Vec<String>,
    },
    /// Inspect and copy persisted agent sessions without modifying originals.
    #[command(hide = true)]
    Rescue {
        /// Recovery adapter: codex, claude or cursor.
        #[arg(long, default_value = "codex", value_name = "ID")]
        adapter: String,
        /// Explicit agent state root. When omitted each adapter resolves its
        /// own default: CODEX_HOME or $HOME/.codex, CLAUDE_HOME or
        /// $HOME/.claude, and the platform Cursor user directory.
        #[arg(long, value_name = "PATH")]
        root: Option<PathBuf>,
        /// Emit sanitized machine-readable JSON.
        #[arg(long)]
        json: bool,
        #[command(subcommand)]
        command: RescueCommand,
    },
    /// Compare two JSON session reports.
    #[command(hide = true)]
    Report {
        #[command(subcommand)]
        command: ReportCommand,
    },
    /// Run red-team sandbox containment and kernel isolation attack battery.
    #[command(hide = true)]
    Redteam {
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Explain the effective policy or lint it for dangerous configurations.
    #[command(hide = true)]
    Policy {
        #[command(subcommand)]
        command: PolicyCommand,
    },
    /// Print shell completion script for the requested shell.
    #[command(hide = true)]
    Completions {
        #[arg(value_enum)]
        shell: Shell,
    },
    /// Generate man page to stdout.
    #[command(hide = true)]
    Man,
    /// Print environment variable export lines for shell integration and PS1.
    #[command(name = "shell-env", hide = true)]
    ShellEnv {
        /// Session ID to export.
        #[arg(long)]
        session_id: Option<String>,
        /// Sandbox tier to export.
        #[arg(long)]
        tier: Option<String>,
        /// Profile name to export.
        #[arg(long)]
        profile: Option<String>,
    },
    /// Manage persistent workspace profiles (cwd, agent, policy).
    #[command(hide = true)]
    Profile {
        #[command(subcommand)]
        command: ProfileCommand,
    },
    /// Diagnostic latency breakdown and optimization hints for a session.
    #[command(name = "why-slow", hide = true)]
    WhySlow {
        /// Session identifier or report path.
        session: String,
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Self-upgrade vetto via npm or cargo based on installation method
    #[command(hide = true)]
    Upgrade {
        /// Channel to upgrade from (stable or alpha)
        #[arg(long, value_name = "CHANNEL")]
        channel: Option<String>,
        /// Check for updates without applying
        #[arg(long)]
        check: bool,
        /// Simulate upgrade command without running
        #[arg(long)]
        dry_run: bool,
    },
    /// Scan project directory for exposed secrets and credentials
    #[command(hide = true)]
    ScanSecrets {
        /// Target directory or file to scan (defaults to current directory)
        #[arg(value_name = "PATH")]
        path: Option<PathBuf>,
        /// Emit machine-readable JSON output
        #[arg(long)]
        json: bool,
        /// Maximum file size to scan in bytes (default: 1MB)
        #[arg(long, value_name = "BYTES")]
        max_size: Option<u64>,
        /// Maximum number of files to scan (default: 5000)
        #[arg(long, value_name = "COUNT")]
        max_files: Option<usize>,
    },
    /// Live-tail session events from JSONL log with optional path filtering
    #[command(hide = true)]
    Watch {
        /// Session PID or path to JSONL log file
        #[arg(value_name = "SESSION_OR_LOG")]
        target: String,
        /// Optional path filter
        #[arg(long, value_name = "PATTERN")]
        path: Option<String>,
        /// Emit raw JSON lines instead of formatted output
        #[arg(long)]
        json: bool,
    },
    /// Restore project files from a previously created session snapshot
    #[command(hide = true)]
    Rollback {
        /// Session ID or path to snapshot archive
        #[arg(value_name = "SESSION")]
        session: String,
        /// Optional restore target directory override
        #[arg(long, value_name = "TARGET")]
        target: Option<PathBuf>,
    },
    /// Tail and filter JSONL session event logs.
    #[command(hide = true)]
    Events {
        /// Path to session JSONL log file or session identifier
        #[arg(value_name = "SESSION")]
        session: PathBuf,
        /// Filter events by category (deny, net, files, exec, notice) or substring
        #[arg(long, value_name = "FILTER")]
        filter: Option<String>,
        /// Continuously follow the log for new events (streaming tail)
        #[arg(short = 'f', long)]
        follow: bool,
        /// Emit machine-readable JSON lines
        #[arg(long)]
        json: bool,
        /// Format output as a column table
        #[arg(long)]
        table: bool,
    },
    /// Query and inspect session audit history (~/.vetto/history.jsonl).
    #[command(hide = true)]
    Audit {
        /// Filter sessions since duration (e.g. 24h, 7d, 30m, YYYY-MM-DD)
        #[arg(long, value_name = "DURATION")]
        since: Option<String>,
        /// Filter by agent preset or name
        #[arg(long, value_name = "NAME")]
        agent: Option<String>,
        /// Limit the maximum number of history entries displayed
        #[arg(long, value_name = "COUNT")]
        limit: Option<usize>,
        /// Optional substring search in policy path, profile, agent, or session ID
        #[arg(value_name = "QUERY")]
        query: Option<String>,
        /// Emit machine-readable JSON lines
        #[arg(long)]
        json: bool,
    },
    /// Generate an aggregated daily audit digest from session history.
    #[command(hide = true)]
    Digest {
        /// Window duration to aggregate (e.g. 24h, 7d, 30m; default 24h)
        #[arg(long, value_name = "DURATION", default_value = "24h")]
        since: String,
        /// Emit machine-readable JSON summary
        #[arg(long)]
        json: bool,
    },
    /// Compare two session JSON audit reports (metric deltas and violation diffs).
    #[command(name = "diff-sessions", hide = true)]
    DiffSessions {
        /// Base session JSON report or identifier
        #[arg(value_name = "SESSION1")]
        session1: PathBuf,
        /// Target session JSON report or identifier
        #[arg(value_name = "SESSION2")]
        session2: PathBuf,
        /// Emit machine-readable JSON diff
        #[arg(long)]
        json: bool,
    },
    /// Chronologically replay sandbox observation and security events from a session log.
    #[command(hide = true)]
    Replay {
        /// Path to session JSONL log file or session identifier
        #[arg(value_name = "SESSION")]
        session: PathBuf,
        /// Playback speed multiplier (e.g. 1.0 for real-time, 2.0 for 2x; default instant)
        #[arg(long, value_name = "FACTOR")]
        speed: Option<f64>,
        /// Emit machine-readable JSON lines
        #[arg(long)]
        json: bool,
    },
    /// Internal SSH ProxyCommand helper; not intended for direct use.
    #[command(name = "ssh-proxy", visible_alias = "__ssh-proxy", hide = true)]
    SshProxy {
        /// Host token supplied by OpenSSH (%h).
        host: String,
        /// Port token supplied by OpenSSH (%p).
        port: u16,
    },
    /// Stored workspace profile invocation by name.
    #[command(external_subcommand)]
    External(Vec<String>),
}

#[derive(Subcommand, Debug)]
pub enum PolicyCommand {
    /// Print the effective policy after all layers merge: tier, network,
    /// roots, masked secrets, limits, environment.
    Explain {
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
        /// Explain why a specific path is allowed, denied, or writable.
        #[arg(long = "why", value_name = "PATH")]
        why: Option<PathBuf>,
    },
    /// Show the resolved effective policy.
    Show {
        /// Print effective resolved policy.
        #[arg(long)]
        effective: bool,
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Check the resolved policy for dangerous configurations. Exits
    /// non-zero with --strict when any finding is reported.
    Lint {
        /// Exit non-zero when any finding is reported.
        #[arg(long)]
        strict: bool,
    },
    /// Import permissions from external agent configurations (e.g. claude, codex)
    Import {
        /// Source agent configuration format: claude | codex
        #[arg(long, value_name = "AGENT")]
        from: String,
        /// Path to source configuration file (defaults to ~/.claude/settings.json or ~/.codex/config.toml)
        #[arg(long, value_name = "PATH")]
        path: Option<PathBuf>,
        /// Output path for generated policy (default: ./policy.toml)
        #[arg(long, short = 'o', value_name = "PATH", default_value = "policy.toml")]
        output: PathBuf,
    },
    /// Cryptographically sign a policy file using Ed25519
    Sign {
        /// Policy file to sign
        file: PathBuf,
        /// Custom private signing key path (default: ~/.vetto/signing.key)
        #[arg(long)]
        key: Option<PathBuf>,
        /// Custom signature output path (default: <file>.sig)
        #[arg(long)]
        out: Option<PathBuf>,
    },
    /// Verify the cryptographic Ed25519 signature of a policy file
    Verify {
        /// Policy file to verify
        file: PathBuf,
        /// Signature file path (default: <file>.sig)
        #[arg(long)]
        sig: Option<PathBuf>,
        /// Public key file path (default: ~/.vetto/signing.pub)
        #[arg(long)]
        key: Option<PathBuf>,
    },
    /// Adopt a community policy into the current project
    Use {
        /// Community policy name (e.g. python-dev, node-dev, rust-dev)
        name: String,
        /// Overwrite existing vetto.toml
        #[arg(short, long)]
        force: bool,
    },
    /// List available community policies
    List,
}

#[derive(Subcommand, Debug)]
pub enum ProfileCommand {
    /// Save current working directory and settings as a named workspace profile.
    Save {
        /// Name of the profile.
        name: String,
        /// Agent command or preset.
        #[arg(long)]
        agent: Option<String>,
        /// Explicit policy path.
        #[arg(long)]
        policy: Option<PathBuf>,
        /// Network mode.
        #[arg(long)]
        net: Option<String>,
        /// Built-in profile layer name.
        #[arg(long)]
        profile: Option<String>,
    },
    /// List all saved workspace profiles.
    List {
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Remove a saved workspace profile.
    Rm {
        /// Name of the profile to remove.
        name: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum ReportCommand {
    /// Print a machine-readable delta for two session JSON reports.
    Compare {
        #[arg(value_name = "SESSION1")]
        session1: PathBuf,
        #[arg(value_name = "SESSION2")]
        session2: PathBuf,
    },
}

#[derive(Subcommand, Debug)]
pub enum RescueCommand {
    /// Discover sessions. Codex defaults to verified index-first (limit 50);
    /// other adapters use their bounded filesystem discovery.
    Scan {
        /// For Codex, use a verified provider index and return at most COUNT
        /// sessions. This never falls back to a filesystem walk.
        #[arg(long, value_name = "COUNT", conflicts_with = "all")]
        limit: Option<usize>,
        /// Explicitly use the bounded recursive filesystem walk. For Codex,
        /// this opts out of the default index-first scan.
        #[arg(long, conflicts_with = "limit")]
        all: bool,
    },
    /// Diagnose one exact session key without changing agent state.
    Diagnose {
        #[arg(value_name = "SESSION")]
        session: String,
    },
    /// Create a verified, exclusive new copy outside the agent state root.
    Snapshot {
        #[arg(value_name = "SESSION")]
        session: String,
        #[arg(long, value_name = "PATH")]
        output: PathBuf,
    },
    /// Create a recovery fork as a verified new copy outside agent state.
    Fork {
        #[arg(value_name = "SESSION")]
        session: String,
        #[arg(long, value_name = "PATH")]
        output: PathBuf,
    },
    /// Perform transactional state repair on a session with backup receipt.
    Repair {
        #[arg(value_name = "SESSION")]
        session: String,
        /// Directory in which pre-repair backups are stored (defaults to ~/.vetto/rescue_backups).
        #[arg(long, value_name = "PATH")]
        backup_dir: Option<PathBuf>,
    },
    /// Rollback a previous state repair using a repair receipt.
    Rollback {
        /// Path to the repair receipt JSON file.
        #[arg(long, value_name = "RECEIPT_PATH")]
        receipt: PathBuf,
        /// Explicit target path override (if target was moved or renamed).
        #[arg(long, value_name = "TARGET_PATH")]
        target: Option<PathBuf>,
    },
}

/// Render completions to stdout without starting a sandbox session.
pub fn print_completions(shell: Shell) -> anyhow::Result<()> {
    let mut command = Cli::command();
    clap_complete::generate(shell, &mut command, "vetto", &mut std::io::stdout());
    Ok(())
}

/// Render man page to stdout without starting a sandbox session.
pub fn print_man() -> anyhow::Result<()> {
    let command = Cli::command();
    let man = clap_mangen::Man::new(command);
    man.render(&mut std::io::stdout())?;
    Ok(())
}

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

    #[test]
    fn completion_generators_cover_supported_shells() {
        for shell in [
            Shell::Bash,
            Shell::Zsh,
            Shell::Fish,
            Shell::PowerShell,
            Shell::Elvish,
        ] {
            let mut command = Cli::command();
            let mut output = Vec::new();
            clap_complete::generate(shell, &mut command, "vetto", &mut output);
            assert!(!output.is_empty(), "empty completion for {shell:?}");
        }
    }

    #[test]
    fn hook_subcommand_parses_install_and_status() {
        let install_cli =
            Cli::try_parse_from(["vetto", "hook", "install", "--scope", "local", "--git"])
                .expect("hook install parsing");
        assert!(matches!(
            install_cli.command,
            Some(Command::Hook {
                command: HookCommand::Install {
                    scope: HookScope::Local,
                    git: true,
                    ..
                }
            })
        ));

        let status_cli = Cli::try_parse_from(["vetto", "hook", "status", "--json"])
            .expect("hook status parsing");
        assert!(matches!(
            status_cli.command,
            Some(Command::Hook {
                command: HookCommand::Status {
                    scope: HookScope::Global,
                    json: true,
                }
            })
        ));
    }

    #[test]
    fn shim_subcommand_parses_binary_and_args() {
        let cli =
            Cli::try_parse_from(["vetto", "shim", "node", "--", "index.js", "--port", "3000"])
                .expect("shim parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Shim {
                ref binary,
                ref args,
            }) if binary.as_deref() == Some("node") && args == &vec!["index.js", "--port", "3000"]
        ));
    }

    #[test]
    fn top_level_multi_keeps_named_agents_separate_from_literal_command() {
        let cli = Cli::try_parse_from([
            "vetto",
            "--multi",
            "--agent",
            "lint=/bin/true",
            "--",
            "/bin/true",
            "--version",
        ])
        .expect("top-level multi syntax");
        assert!(cli.multi);
        assert_eq!(cli.agents, vec!["lint=/bin/true"]);
        assert_eq!(cli.agent, vec!["/bin/true", "--version"]);
    }

    #[test]
    fn doctor_check_agent_is_an_explicit_subcommand_option() {
        let cli = Cli::try_parse_from(["vetto", "doctor", "--check-agent", "codex"])
            .expect("doctor agent check");
        assert!(matches!(
            cli.command,
            Some(Command::Doctor {
                check_agent: Some(ref agent),
                ..
            }) if agent == "codex"
        ));
    }

    #[test]
    fn rescue_parser_keeps_adapter_options_outside_the_session_selector() {
        let cli = Cli::try_parse_from([
            "vetto",
            "rescue",
            "--adapter",
            "codex",
            "--root",
            "/tmp/codex-home",
            "--json",
            "diagnose",
            "sessions/example.jsonl",
        ])
        .expect("rescue syntax");
        assert!(matches!(
            cli.command,
            Some(Command::Rescue {
                ref adapter,
                json: true,
                command: RescueCommand::Diagnose { ref session },
                ..
            }) if adapter == "codex" && session == "sessions/example.jsonl"
        ));
    }

    #[test]
    fn rescue_scan_exposes_explicit_index_limit_and_full_walk_modes() {
        let default =
            Cli::try_parse_from(["vetto", "rescue", "scan"]).expect("default rescue scan syntax");
        assert!(matches!(
            default.command,
            Some(Command::Rescue {
                command: RescueCommand::Scan {
                    limit: None,
                    all: false,
                },
                ..
            })
        ));

        let limited = Cli::try_parse_from(["vetto", "rescue", "scan", "--limit", "25"])
            .expect("limited rescue scan syntax");
        assert!(matches!(
            limited.command,
            Some(Command::Rescue {
                command: RescueCommand::Scan {
                    limit: Some(25),
                    all: false,
                },
                ..
            })
        ));

        let full = Cli::try_parse_from(["vetto", "rescue", "scan", "--all"])
            .expect("full rescue scan syntax");
        assert!(matches!(
            full.command,
            Some(Command::Rescue {
                command: RescueCommand::Scan {
                    limit: None,
                    all: true,
                },
                ..
            })
        ));
    }

    #[test]
    fn man_generator_renders_valid_troff_manpage() {
        let command = Cli::command();
        let man = clap_mangen::Man::new(command);
        let mut buffer = Vec::new();
        man.render(&mut buffer).expect("render man page");
        let rendered = String::from_utf8_lossy(&buffer);
        assert!(rendered.contains(".TH vetto"));
        assert!(rendered.contains("NAME"));
        assert!(rendered.contains("SYNOPSIS"));
    }

    #[test]
    fn parses_preset_and_shadow_flags() {
        let cli = Cli::try_parse_from(["vetto", "--preset", "paranoid", "--shadow", "--", "node"])
            .expect("preset and shadow flags");
        assert_eq!(cli.preset.as_deref(), Some("paranoid"));
        assert!(cli.shadow);
    }

    #[test]
    fn doctor_fix_subcommand_parses() {
        let cli = Cli::try_parse_from(["vetto", "doctor", "--fix"]).expect("doctor fix parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Doctor { fix: true, .. })
        ));
    }

    #[test]
    fn upgrade_subcommand_parses_channel_and_flags() {
        let cli = Cli::try_parse_from(["vetto", "upgrade", "--channel", "alpha", "--check"])
            .expect("upgrade parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Upgrade {
                channel: Some(ref ch),
                check: true,
                dry_run: false,
            }) if ch == "alpha"
        ));
    }

    #[test]
    fn init_wizard_subcommand_parses() {
        let cli = Cli::try_parse_from(["vetto", "init", "--wizard"]).expect("init wizard parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Init { wizard: true, .. })
        ));
    }

    #[test]
    fn policy_explain_why_parses() {
        let cli = Cli::try_parse_from(["vetto", "policy", "explain", "--why", "src/main.rs"])
            .expect("policy explain why parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Policy {
                command: PolicyCommand::Explain { why: Some(ref path), .. }
            }) if path == &PathBuf::from("src/main.rs")
        ));
    }

    #[test]
    fn policy_import_parses() {
        let cli = Cli::try_parse_from([
            "vetto",
            "policy",
            "import",
            "--from",
            "claude",
            "-o",
            "my-policy.toml",
        ])
        .expect("policy import parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Policy {
                command: PolicyCommand::Import { ref from, ref output, .. }
            }) if from == "claude" && output == &PathBuf::from("my-policy.toml")
        ));
    }

    #[test]
    fn tour_subcommand_parses_non_interactive_flag() {
        let cli =
            Cli::try_parse_from(["vetto", "tour", "--non-interactive"]).expect("tour parsing");
        assert!(matches!(
            cli.command,
            Some(Command::Tour {
                non_interactive: true,
            })
        ));
    }

    #[test]
    fn parses_observability_subcommands() {
        let events_cli = Cli::try_parse_from([
            "vetto",
            "events",
            "session.jsonl",
            "--filter",
            "deny",
            "--follow",
        ])
        .expect("events parsing");
        assert!(matches!(
            events_cli.command,
            Some(Command::Events {
                ref session,
                ref filter,
                follow: true,
                ..
            }) if session == &PathBuf::from("session.jsonl") && filter.as_deref() == Some("deny")
        ));

        let audit_cli = Cli::try_parse_from([
            "vetto",
            "audit",
            "--since",
            "24h",
            "--agent",
            "codex",
            "--limit",
            "10",
            "search_term",
        ])
        .expect("audit parsing");
        assert!(matches!(
            audit_cli.command,
            Some(Command::Audit {
                ref since,
                ref agent,
                limit: Some(10),
                ref query,
                ..
            }) if since.as_deref() == Some("24h") && agent.as_deref() == Some("codex") && query.as_deref() == Some("search_term")
        ));

        let digest_cli = Cli::try_parse_from(["vetto", "digest", "--since", "7d", "--json"])
            .expect("digest parsing");
        assert!(matches!(
            digest_cli.command,
            Some(Command::Digest {
                ref since,
                json: true,
            }) if since == "7d"
        ));

        let diff_cli =
            Cli::try_parse_from(["vetto", "diff-sessions", "s1.json", "s2.json", "--json"])
                .expect("diff-sessions parsing");
        assert!(matches!(
            diff_cli.command,
            Some(Command::DiffSessions {
                ref session1,
                ref session2,
                json: true,
            }) if session1 == &PathBuf::from("s1.json") && session2 == &PathBuf::from("s2.json")
        ));

        let replay_cli =
            Cli::try_parse_from(["vetto", "replay", "session.jsonl", "--speed", "1.5"])
                .expect("replay parsing");
        assert!(matches!(
            replay_cli.command,
            Some(Command::Replay {
                ref session,
                speed: Some(1.5),
                ..
            }) if session == &PathBuf::from("session.jsonl")
        ));
    }

    #[test]
    fn tier7_subcommands_parse_correctly() {
        let mcp = Cli::try_parse_from(["vetto", "mcp"]).expect("mcp syntax");
        assert!(matches!(mcp.command, Some(Command::Mcp)));

        let plugin_install =
            Cli::try_parse_from(["vetto", "plugin", "install", "claude-code", "--force"])
                .expect("plugin install syntax");
        assert!(matches!(
            plugin_install.command,
            Some(Command::Plugin {
                command: plugin::PluginCommand::Install {
                    ref target,
                    force: true
                }
            }) if target == "claude-code"
        ));

        let daemon_start = Cli::try_parse_from([
            "vetto",
            "daemon",
            "start",
            "--port",
            "54321",
            "--foreground",
        ])
        .expect("daemon start syntax");
        assert!(matches!(
            daemon_start.command,
            Some(Command::Daemon {
                command: crate::daemon::DaemonCommand::Start {
                    port: 54321,
                    foreground: true,
                    ..
                }
            })
        ));

        let serve =
            Cli::try_parse_from(["vetto", "serve", "--port", "8080"]).expect("serve syntax");
        assert!(matches!(serve.command, Some(Command::Serve { port: 8080 })));

        let policy_sign = Cli::try_parse_from(["vetto", "policy", "sign", "vetto.toml"])
            .expect("policy sign syntax");
        assert!(matches!(
            policy_sign.command,
            Some(Command::Policy {
                command: PolicyCommand::Sign { ref file, .. }
            }) if file == &PathBuf::from("vetto.toml")
        ));

        let policy_use = Cli::try_parse_from(["vetto", "policy", "use", "python-dev"])
            .expect("policy use syntax");
        assert!(matches!(
            policy_use.command,
            Some(Command::Policy {
                command: PolicyCommand::Use { ref name, force: false }
            }) if name == "python-dev"
        ));
    }
}