vtcode-core 0.136.3

Core library for VT Code - a Rust-based terminal coding agent
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
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
use clap::{ArgAction, ColorChoice, Parser, Subcommand, ValueHint};
use colorchoice_clap::Color as ColorSelection;
use std::env;
use std::path::{Path, PathBuf};

use crate::config::models::ModelId;

mod acp;
mod ask;
mod background;
mod bench;
mod check;
mod config;
mod dependencies;
mod exec;
mod models;
mod pods;
mod review;
mod schedule;
mod schema;
mod session_store;
mod skills;

pub use acp::AgentClientProtocolTarget;
pub use ask::{AskCommandOptions, AskOutputFormat};
pub use background::BackgroundSubagentArgs;
pub use bench::BenchAllocatorArgs;
pub use check::CheckSubcommand;
pub use config::{ConfigFile, ContextConfig, LoggingConfig, PerformanceConfig, SecurityConfig, ToolConfig};
pub use dependencies::{DependenciesSubcommand, ManagedDependency};
pub use exec::{ExecEvalArgs, ExecResumeArgs, ExecSubcommand};
pub use models::ModelCommands;
pub use pods::PodsCommands;
pub use review::ReviewArgs;
pub use schedule::{ScheduleCreateArgs, ScheduleSubcommand};
pub use schema::{SchemaCommands, SchemaMode, SchemaOutputFormat};
pub use session_store::SessionStoreCommand;
pub use skills::{SkillsRefSubcommand, SkillsSubcommand};

pub const PLANNING_WORKFLOW_READ_ONLY_HEADER: &str = "# PLANNING WORKFLOW (READ-ONLY)";

pub const PLANNING_WORKFLOW_READ_ONLY_NOTICE_LINE: &str = "Mutating file edits are blocked, including `apply_patch`. Use `exec_command.cmd` only for read-only repository inspection with the active shell profile's syntax; keep `task_tracker` current. Plan artifacts under `.vtcode/plans/` are allowed.";

pub const PLANNING_WORKFLOW_EXIT_INSTRUCTION_LINE: &str =
    "The user approved the plan. Stop planning and switch to implementation.";

pub const PLANNING_WORKFLOW_PLAN_QUALITY_LINE: &str = "Keep plans compact and spec-like. Emit ONE `<proposed_plan>` that fits ~1500 tokens: a 1-3 line Summary; a tight numbered step list where each step is `Action -> files/symbols -> verify:`; one Validation line (build/lint + test commands); Assumptions as short bullets. Prefer file:symbol references over prose, written as plain text or inline code (e.g. `src/main.rs:42`) — never as markdown links or editor/IDE URIs (no `[label](url)`, no `vscode-file://`/`file://` schemes). Ask only material blocking questions; unresolved: `Next open decision: ...`.";

pub const PLANNING_WORKFLOW_RESEARCH_SCOPE_LINE: &str = "Scale research to the request: for a narrow or simple ask, ~5-10 targeted reads/searches is usually enough before drafting `<proposed_plan>` — do not exhaustively enumerate the whole repository. For a broad or ambiguous ask, research proportionally more, but stop and draft as soon as scope/decomposition/verification decisions are closed.";

pub const PLANNING_WORKFLOW_INTERVIEW_POLICY_LINE: &str = "Use `request_user_input` for interview questions informed by repo context. Continue until scope/decomposition/verification decisions are closed before finalizing `<proposed_plan>`.";

pub const PLANNING_WORKFLOW_NO_REQUEST_USER_INPUT_POLICY_LINE: &str = "`request_user_input` unavailable here. Continue exploring read-only, finish unblocked planning, surface blockers in plain text.";

pub const PLANNING_WORKFLOW_NO_AUTO_EXIT_LINE: &str = "Do not auto-exit planning workflow. Present `<proposed_plan>` and wait for explicit user approval (\"implement\", \"looks good\", \"ship it\", etc.) before switching to implementation.";

pub const PLANNING_WORKFLOW_IMPLEMENTATION_PROMPT: &str = "Implement the approved plan.";

pub const PLANNING_WORKFLOW_HINT: &str = "Present the plan for approval; do not start implementing yet.";

pub const PLANNING_WORKFLOW_TASK_TRACKER_LINE: &str = "`task_tracker` remains available while planning.";

pub const PLANNING_WORKFLOW_IMPLEMENT_REMINDER: &str = "• Planning workflow is active with read-only permissions. Say “implement” to present the plan for user approval, or “stay in planning workflow” to revise. Calling `finish_planning` only presents the plan; mutating tools stay disabled until the user approves the plan. If a write tool is unavailable because planning workflow is active, do not emit the full artifact content in the chat. Instead, summarize the blocker briefly and ask the user to save the content, or call `finish_planning` to present the plan for approval.";

#[derive(Parser, Debug, Clone)]
pub struct Cli {
    /// Color output selection (auto, always, never)
    #[command(flatten)]
    pub color: ColorSelection,

    /// Optional positional path to run vtcode against a different workspace
    #[arg(
        value_name = "WORKSPACE",
        value_hint = ValueHint::DirPath,
        value_parser = parse_workspace_directory,
        global = true
    )]
    pub workspace_path: Option<PathBuf>,

    /// LLM Model ID (e.g., gpt-5, claude-sonnet-4-6, gemini-3-flash-preview)
    #[arg(long, global = true)]
    pub model: Option<String>,

    /// LLM Provider (gemini, openai, anthropic, deepseek, openrouter, codex, zai, moonshot, minimax, ollama, lmstudio)
    #[arg(long, global = true)]
    pub provider: Option<String>,

    /// API key environment variable (auto-detects GEMINI_API_KEY, OPENAI_API_KEY, etc.)
    #[arg(long, global = true, default_value = crate::config::constants::defaults::DEFAULT_API_KEY_ENV)]
    pub api_key_env: String,

    /// Workspace root directory (default: current directory)
    #[arg(
        long,
        global = true,
        alias = "workspace-dir",
        value_name = "PATH",
        value_hint = ValueHint::DirPath,
        value_parser = parse_workspace_directory
    )]
    pub workspace: Option<PathBuf>,

    /// Enable research-preview features
    #[arg(long, global = true)]
    pub research_preview: bool,

    /// Security level for tool execution (strict, moderate, permissive)
    #[arg(long, global = true, default_value = "moderate")]
    pub security_level: String,

    /// Show diffs for file changes in chat interface
    #[arg(long, global = true)]
    pub show_file_diffs: bool,

    /// Maximum concurrent async operations
    #[arg(long, global = true, default_value_t = 5)]
    pub max_concurrent_ops: usize,

    /// Maximum API requests per minute
    #[arg(long, global = true, default_value_t = 30)]
    pub api_rate_limit: usize,

    /// Maximum tool calls per session
    #[arg(long, global = true, default_value_t = 10)]
    pub max_tool_calls: usize,

    /// Enable debug output for troubleshooting
    #[arg(long, global = true)]
    pub debug: bool,

    /// Enable verbose logging
    #[arg(long, global = true)]
    pub verbose: bool,

    /// Suppress all non-essential output (for scripting, CI/CD)
    #[arg(short, long, global = true)]
    pub quiet: bool,

    /// Configuration overrides or file path (KEY=VALUE or PATH)
    #[arg(
        short = 'c',
        long = "config",
        value_name = "KEY=VALUE|PATH",
        action = ArgAction::Append,
        global = true
    )]
    pub config: Vec<String>,

    /// Log level (error, warn, info, debug, trace)
    #[arg(long, global = true, default_value = "info")]
    pub log_level: String,

    /// Disable color output (equivalent to `--color never`)
    #[arg(long, global = true)]
    pub no_color: bool,

    /// Select UI theme (e.g., ciapre-dark, ciapre-blue)
    #[arg(long, global = true, value_name = "THEME")]
    pub theme: Option<String>,

    /// App tick rate in milliseconds (default: 250)
    #[arg(short = 't', long, default_value_t = 250)]
    pub tick_rate: u64,

    /// Frame rate in FPS (default: 60)
    #[arg(short = 'f', long, default_value_t = 60)]
    pub frame_rate: u64,

    /// Enable skills system
    #[arg(long, global = true)]
    pub enable_skills: bool,

    /// Enable Chrome browser integration for web automation
    #[arg(long, global = true)]
    pub chrome: bool,

    /// Disable Chrome browser integration
    #[arg(long = "no-chrome", global = true, conflicts_with = "chrome")]
    pub no_chrome: bool,

    /// Skip safety confirmations (use with caution)
    #[arg(long, global = true)]
    pub skip_confirmations: bool,

    /// Enable experimental Codex app-server features for this run
    #[arg(long = "codex-experimental", global = true, conflicts_with = "no_codex_experimental")]
    pub codex_experimental: bool,

    /// Disable experimental Codex app-server features for this run
    #[arg(long = "no-codex-experimental", global = true, conflicts_with = "codex_experimental")]
    pub no_codex_experimental: bool,

    /// Print response without launching the interactive TUI
    #[arg(
        short = 'p',
        long = "print",
        value_name = "PROMPT",
        value_hint = ValueHint::Other,
        num_args = 0..=1,
        default_missing_value = "",
        global = true,
        conflicts_with_all = ["full_auto"]
    )]
    pub print: Option<String>,

    /// Run non-interactively with full-auto permission review
    #[arg(
        long = "full-auto",
        global = true,
        help = "Run non-interactively with full-auto permission review",
        long_help = r#"Run non-interactively on top of the active primary agent.

If no primary agent is explicitly selected or configured, VT Code selects the effective `auto` primary agent. Explicit choices, including `duck`, are honoured. Full-auto is an execution and permission layer, not a primary agent.

Full-auto does not override explicit denies or grant tools outside `[automation.full_auto].allowed_tools`. Tools outside that allow-list are denied. Promptable actions inside the allow-list are routed through automatic permission review after deny and policy checks instead of asking. The run fails fast if it needs the defaulted `auto` primary agent and no effective `auto` exists."#,
        value_name = "PROMPT",
        num_args = 0..=1,
        default_missing_value = "",
        value_hint = ValueHint::Other
    )]
    pub full_auto: Option<String>,

    /// Resume a previous conversation (use without ID for interactive picker)
    #[arg(
        short = 'r',
        long = "resume",
        global = true,
        value_name = "SESSION_ID",
        num_args = 0..=1,
        default_missing_value = "__interactive__",
        conflicts_with_all = ["continue_latest", "full_auto"]
    )]
    pub resume_session: Option<String>,

    /// Continue the most recent conversation automatically
    #[arg(
        long = "continue",
        visible_alias = "continue-session",
        global = true,
        conflicts_with_all = ["resume_session", "full_auto"]
    )]
    pub continue_latest: bool,

    /// Fork an existing session with a new session ID
    #[arg(
        long = "fork-session",
        global = true,
        value_name = "SESSION_ID",
        conflicts_with_all = ["resume_session", "continue_latest", "full_auto"]
    )]
    pub fork_session: Option<String>,

    /// Show archived sessions from every workspace when resuming or forking
    #[arg(long, global = true)]
    pub all: bool,

    /// Custom suffix for session identifier (alphanumeric, dash, underscore only, max 64 chars)
    #[arg(long = "session-id", global = true, value_name = "CUSTOM_SUFFIX")]
    pub session_id: Option<String>,

    /// Use summarized history when forking a session
    #[arg(long, global = true)]
    pub summarize: bool,

    /// Override the default agent model for this session
    #[arg(long, global = true, value_name = "AGENT")]
    pub agent: Option<String>,

    /// Tools that execute without prompting (comma-separated, supports patterns like "Bash(git:*)")
    #[arg(long = "allowed-tools", global = true, value_name = "TOOLS", action = ArgAction::Append)]
    pub allowed_tools: Vec<String>,

    /// Tools that cannot be used by the agent
    #[arg(long = "disallowed-tools", global = true, value_name = "TOOLS", action = ArgAction::Append)]
    pub disallowed_tools: Vec<String>,

    /// Auto-approve promptable actions while respecting denies and policy blocks
    #[arg(
        long = "dangerously-skip-permissions",
        global = true,
        help = "Auto-approve promptable actions while respecting denies and policy blocks",
        long_help = "Auto-approve promptable actions while still respecting explicit denies and policy blocks."
    )]
    pub dangerously_skip_permissions: bool,

    /// Explicitly connect to IDE on startup (auto-detects available IDEs)
    #[arg(long, global = true)]
    pub ide: bool,

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

#[derive(Subcommand, Debug, Clone)]
pub enum Commands {
    /// Start Agent Client Protocol bridge for IDE integrations
    #[command(name = "acp")]
    AgentClientProtocol {
        /// Client to connect over ACP
        #[arg(value_enum, default_value_t = AgentClientProtocolTarget::Zed)]
        target: AgentClientProtocolTarget,
    },

    /// Unified per-session state store (single source of truth for state,
    /// context, and history). Consolidates the legacy `checkpoints/`, `logs/`,
    /// and `history/` stores.
    #[command(name = "session-store")]
    SessionStore {
        #[command(subcommand)]
        command: SessionStoreCommand,
    },

    /// Interactive AI coding assistant
    Chat,

    /// Resume the most recent conversation automatically
    ///
    /// Equivalent to `vtcode --continue`. Loads the latest archived session in
    /// the current workspace (or across all workspaces with `--all`) and resumes
    /// it without showing the interactive picker.
    ///
    /// Examples:
    ///   vtcode continue
    ///   vtcode continue --all
    ///   vtcode continue --session-id my-fork   # fork latest into a new session
    Continue,

    /// Single prompt mode - prints model reply without tools
    ///
    /// Send a single prompt to the model and print the response. No tools are
    /// invoked, no session is created, and the process exits after replying.
    ///
    /// Examples:
    ///   vtcode ask "what is a monad?"
    ///   echo "summarize this" | vtcode ask
    ///   vtcode ask --output-format json "explain ownership in Rust"
    Ask {
        /// Prompt to ask. Use `-` to force reading from stdin.
        #[arg(
            value_name = "PROMPT",
            long_help = "The prompt to send to the model.\n\nOmit to read from stdin (piped input).\nUse '-' to explicitly force reading from stdin."
        )]
        prompt: Option<String>,
        /// Format the response using a structured representation.
        #[arg(
            long = "output-format",
            value_enum,
            value_name = "FORMAT",
            long_help = "Output format for the response.\n\nCurrently supports:\n  json - Emit the response as a structured JSON document."
        )]
        output_format: Option<AskOutputFormat>,
    },
    /// Headless execution mode
    ///
    /// Run the agent in non-interactive mode. The agent executes the prompt,
    /// runs tools, and exits when done. Ideal for CI/CD, scripting, and
    /// agent-to-agent workflows.
    ///
    /// Examples:
    ///   vtcode exec "explain this codebase"
    ///   vtcode exec --json "fix the failing test"
    ///   vtcode exec --dry-run "refactor auth module"
    ///   cat file.rs | vtcode exec "review this code"
    ///   vtcode exec resume --last
    Exec {
        /// Emit structured JSON events to stdout (one per line)
        #[arg(
            long,
            long_help = "Stream newline-delimited JSON events to stdout.\nEach line is a JSON object representing an agent event (tool call, message, etc.).\nUseful for programmatic consumption and CI integration."
        )]
        json: bool,
        /// Run a read-only dry-run execution (blocks mutating tool calls)
        #[arg(
            long,
            long_help = "Simulate execution without making changes.\nThe agent plans tool calls but does not execute mutating operations (file writes, shell commands).\nUseful for previewing what the agent would do."
        )]
        dry_run: bool,
        /// Optional path to write the JSONL transcript
        #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath, long_help = "Write the full JSONL event transcript to this file.\nIncludes all agent events: tool calls, messages, errors, and metadata.")]
        events: Option<PathBuf>,
        /// Write the last agent message to this file
        #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath, long_help = "Write only the final agent message to this file.\nUseful for piping the agent's response into other tools.")]
        last_message_file: Option<PathBuf>,
        /// Optional exec subcommand
        #[command(subcommand)]
        command: Option<ExecSubcommand>,
        /// Prompt to execute. Use `-` to force reading from stdin.
        #[arg(
            value_name = "PROMPT",
            long_help = "The prompt to execute.\n\nOmit to read from stdin (piped input).\nUse '-' to explicitly force reading from stdin.\nQuote multi-word prompts: vtcode exec \"fix the bug in auth.rs\""
        )]
        prompt: Option<String>,
    },
    /// Manage durable scheduled tasks
    ///
    /// Create, list, and delete scheduled tasks that run on a recurring or
    /// one-shot basis. Tasks are stored persistently and survive restarts
    /// when paired with `vtcode schedule install-service`.
    ///
    /// Examples:
    ///   vtcode schedule create --name "daily-review" --cron "0 9 * * 1-5" --prompt "review recent changes"
    ///   vtcode schedule create --name "reminder" --reminder "standup in 10 minutes" --at "09:50"
    ///   vtcode schedule list
    ///   vtcode schedule delete `<task-id>`
    Schedule {
        #[command(subcommand)]
        command: ScheduleSubcommand,
    },

    /// Internal VT Code background subagent runner
    #[command(name = "background-subagent", hide = true)]
    BackgroundSubagent(BackgroundSubagentArgs),

    /// Headless code review for the current diff, selected files, or a custom git target
    #[command(
        long_about = "Run a non-interactive code review.\n\nExamples:\n  vtcode review\n  vtcode review --last-diff\n  vtcode review --target HEAD~1..HEAD\n  vtcode review --file src/main.rs --file crates/codegen/vtcode-core/src/lib.rs\n  vtcode review --style security"
    )]
    Review(ReviewArgs),

    /// Runtime schema introspection for built-in tools
    Schema {
        #[command(subcommand)]
        command: SchemaCommands,
    },

    /// Verbose interactive chat with debug output
    ChatVerbose,

    /// Analyze workspace (structure, security, performance)
    Analyze {
        /// Type of analysis to perform
        #[arg(value_name = "TYPE", default_value = "full")]
        analysis_type: String,
    },

    /// Pretty-print trajectory logs
    #[command(name = "trajectory")]
    Trajectory {
        /// Optional path to trajectory JSONL file
        #[arg(long)]
        file: Option<PathBuf>,
        /// Number of top entries to show
        #[arg(long, default_value_t = 10)]
        top: usize,
    },

    /// Send a VT Code notification using the built-in notification system
    Notify {
        /// Optional notification title
        #[arg(long, value_name = "TITLE")]
        title: Option<String>,
        /// Notification message
        #[arg(value_name = "MESSAGE")]
        message: String,
    },

    /// Benchmark against SWE-bench evaluation framework
    Benchmark {
        /// Path to a JSON benchmark specification
        #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
        task_file: Option<PathBuf>,
        /// Inline JSON specification for quick experiments
        #[arg(long, value_name = "JSON")]
        task: Option<String>,
        /// Optional path to write the structured benchmark report
        #[arg(long, value_name = "PATH", value_hint = ValueHint::FilePath)]
        output: Option<PathBuf>,
        /// Limit the number of tasks executed
        #[arg(long, value_name = "COUNT")]
        max_tasks: Option<usize>,
    },

    /// Measure allocator RSS behavior under a bursty/sparse Tokio workload
    ///
    /// Reproduces the mimalloc-vs-jemalloc analysis pattern: many short-lived
    /// tasks allocated across Tokio worker threads, with idle gaps between
    /// bursts. Reports the RSS trajectory so you can see whether the global
    /// allocator returns memory to the OS (jemalloc) or pins it (mimalloc/glibc).
    /// Build with `--features allocator-jemalloc` to compare allocators.
    #[command(name = "bench-allocator")]
    BenchAllocator(BenchAllocatorArgs),

    /// Create complete Rust project
    CreateProject {
        name: String,
        #[arg(long = "feature", value_name = "FEATURE", action = ArgAction::Append)]
        features: Vec<String>,
    },

    /// Revert agent to a previous snapshot
    Revert {
        /// Turn number to revert to
        #[arg(short, long)]
        turn: usize,
        /// Scope of revert operation: conversation, code, full
        #[arg(long)]
        partial: Option<String>,
    },

    /// List all available snapshots
    Snapshots,

    /// Clean up old snapshots
    ///
    /// Features:
    ///   • Remove snapshots beyond limit
    ///   • Configurable retention policy
    ///   • Safe deletion with confirmation
    ///
    /// Examples:
    ///   vtcode cleanup-snapshots
    ///   vtcode cleanup-snapshots --max 20
    #[command(name = "cleanup-snapshots")]
    CleanupSnapshots {
        /// Maximum number of snapshots to keep
        ///
        /// Default: 50
        /// Example: --max 20
        #[arg(short, long, default_value_t = 50)]
        max: usize,
    },

    /// Initialize project guidance and workspace scaffolding
    ///
    /// Bootstrap a workspace for use with VT Code. Creates vtcode.toml,
    /// AGENTS.md, and other scaffolding. Run this once per project.
    ///
    /// Examples:
    ///   vtcode init
    ///   vtcode init --force
    Init {
        /// Overwrite an existing AGENTS.md without prompting
        #[arg(
            long,
            short = 'f',
            long_help = "Overwrite AGENTS.md without confirmation.\nUse this in CI/CD or scripts where interactive prompts are not possible."
        )]
        force: bool,
    },

    /// Initialize project in ~/.vtcode/projects/
    ///
    /// Create a new project entry in the VT Code projects directory.
    /// This is separate from `vtcode init` which bootstraps a workspace.
    ///
    /// Examples:
    ///   vtcode init-project
    ///   vtcode init-project --name my-project
    ///   vtcode init-project --force --migrate
    #[command(name = "init-project")]
    InitProject {
        /// Project name - defaults to current directory name
        #[arg(
            long,
            long_help = "Name for the project.\nDefaults to the current directory name if not specified."
        )]
        name: Option<String>,
        /// Force initialization - overwrite existing project structure
        #[arg(long, long_help = "Overwrite existing project structure without confirmation.")]
        force: bool,
        /// Migrate existing files - move existing config/cache files to new structure
        #[arg(
            long,
            long_help = "Move existing config and cache files into the new project structure."
        )]
        migrate: bool,
    },

    /// Generate configuration file
    ///
    /// Create a vtcode.toml configuration file with default settings.
    /// Use --global to create in ~/.vtcode/ or specify an output path.
    ///
    /// Examples:
    ///   vtcode config
    ///   vtcode config --global
    ///   vtcode config --output ./my-vtcode.toml
    Config {
        /// Output file path
        #[arg(
            long,
            long_help = "Write the configuration to this path.\nDefaults to ./vtcode.toml in the current directory."
        )]
        output: Option<PathBuf>,
        /// Create in user home directory (~/.vtcode/vtcode.toml)
        #[arg(
            long,
            long_help = "Write the configuration to ~/.vtcode/vtcode.toml.\nThis sets global defaults for all workspaces."
        )]
        global: bool,
    },

    /// Authenticate with a supported provider
    ///
    /// Start an OAuth or API-key login flow for the given provider.
    /// Credentials are stored securely in the OS keychain.
    ///
    /// Examples:
    ///   vtcode login openai
    ///   vtcode login openrouter
    ///   vtcode login codex
    ///   vtcode login codex --device-code
    Login {
        /// Provider name (`openai`, `openrouter`, `copilot`, or `codex`)
        #[arg(long_help = "The provider to authenticate with.\nSupported: openai, openrouter, copilot, codex")]
        provider: String,
        /// Use device-code login when the provider supports it (currently `codex` only)
        #[arg(
            long,
            default_value_t = false,
            long_help = "Use the device-code OAuth flow.\nCurrently supported only for the `codex` provider.\nOpens a browser URL and asks you to enter a code."
        )]
        device_code: bool,
    },

    /// Clear stored authentication credentials for a provider
    ///
    /// Remove stored OAuth tokens or API keys for the given provider.
    ///
    /// Examples:
    ///   vtcode logout openai
    ///   vtcode logout openrouter
    Logout {
        /// Provider name (`openai`, `openrouter`, `copilot`, or `codex`)
        #[arg(long_help = "The provider to deauthenticate.\nSupported: openai, openrouter, copilot, codex")]
        provider: String,
    },

    /// Show authentication status for one provider or all supported providers
    ///
    /// Display whether each provider is authenticated, which credential type
    /// is in use, and token/session metadata when available.
    ///
    /// Examples:
    ///   vtcode auth
    ///   vtcode auth openai
    ///   vtcode auth openrouter
    Auth {
        /// Optional provider name (`openai`, `openrouter`, `copilot`, or `codex`)
        #[arg(long_help = "Show status for a single provider.\nOmit to show status for all supported providers.")]
        provider: Option<String>,
    },

    /// Manage tool execution policies
    #[command(name = "tool-policy")]
    ToolPolicy {
        #[command(subcommand)]
        command: crate::cli::tool_policy_commands::ToolPolicyCommands,
    },

    /// Manage Model Context Protocol providers
    #[command(name = "mcp")]
    Mcp {
        #[command(subcommand)]
        command: crate::mcp::cli::McpCommands,
    },

    /// Agent2Agent (A2A) Protocol
    #[command(name = "a2a")]
    A2a {
        #[command(subcommand)]
        command: super::super::a2a::cli::A2aCommands,
    },

    /// Proxy to the official Codex app-server
    #[command(name = "app-server")]
    AppServer {
        /// Transport listen target passed through to `codex app-server`
        #[arg(long, default_value = "stdio://")]
        listen: String,
    },

    /// Manage models and providers
    Models {
        #[command(subcommand)]
        command: ModelCommands,
    },

    /// Manage GPU pod deployments
    #[command(name = "pods")]
    Pods {
        #[command(subcommand)]
        command: PodsCommands,
    },

    /// Generate or display man pages
    Man {
        /// Command name to generate man page for (optional)
        command: Option<String>,
        /// Output file path to save man page
        #[arg(short, long)]
        output: Option<PathBuf>,
    },

    /// Manage Agent Skills
    ///
    /// Skills are reusable instruction sets that extend the agent's capabilities.
    /// Each skill is a directory containing a SKILL.md manifest and optional scripts.
    ///
    /// Examples:
    ///   vtcode skills list
    ///   vtcode skills create my-skill
    ///   vtcode skills load my-skill
    ///   vtcode skills info my-skill
    ///   vtcode skills validate ./path/to/skill
    #[command(subcommand)]
    Skills(SkillsSubcommand),

    /// List available skills (alias for `vtcode skills list`)
    #[command(name = "list-skills", hide = true)]
    ListSkills {},

    /// Manage optional VT Code dependencies
    ///
    /// Install, update, or check the status of optional tools that VT Code
    /// can use (ripgrep, ast-grep, search-tools bundle).
    ///
    /// Examples:
    ///   vtcode dependencies status
    ///   vtcode dependencies install search-tools
    ///   vtcode deps install ripgrep
    #[command(name = "dependencies", visible_alias = "deps", subcommand)]
    Dependencies(DependenciesSubcommand),

    /// Run built-in repository checks
    ///
    /// Execute repository-level checks such as ast-grep rule tests and scans.
    ///
    /// Examples:
    ///   vtcode check ast-grep
    Check {
        #[command(subcommand)]
        command: CheckSubcommand,
    },

    /// Check for and install binary updates from GitHub Releases
    ///
    /// Manage VT Code binary updates. By default checks for a new version
    /// and offers to install it. Use flags to customize behavior.
    ///
    /// Examples:
    ///   vtcode update
    ///   vtcode update --check
    ///   vtcode update --force
    ///   vtcode update --list
    ///   vtcode update --pin 0.120.0
    ///   vtcode update --unpin
    #[command(name = "update")]
    Update {
        /// Check for updates without installing
        #[arg(
            long,
            long_help = "Check whether a newer version is available without installing it."
        )]
        check: bool,
        /// Force update even if on latest version
        #[arg(
            long,
            long_help = "Reinstall or downgrade even if the current version is already the latest."
        )]
        force: bool,
        /// List available versions
        #[arg(long, long_help = "Print available release versions from GitHub and exit.")]
        list: bool,
        /// Number of versions to list (default: 10)
        #[arg(long, long_help = "Maximum number of versions to display with --list.")]
        limit: usize,
        /// Pin to a specific version
        #[arg(
            long,
            value_name = "VERSION",
            long_help = "Pin the binary to a specific version.\nAuto-updates are disabled until --unpin is used."
        )]
        pin: Option<String>,
        /// Unpin version
        #[arg(long, long_help = "Remove a previously set version pin and resume auto-updates.")]
        unpin: bool,
        /// Set release channel (stable, beta, nightly)
        #[arg(
            long,
            value_name = "CHANNEL",
            long_help = "Switch the release channel.\nAccepted values: stable, beta, nightly."
        )]
        channel: Option<String>,
        /// Show current update configuration
        #[arg(
            long,
            long_help = "Display the current update configuration (channel, pin, intervals) and exit."
        )]
        show_config: bool,
    },

    /// Start Anthropic API compatibility server
    #[command(name = "anthropic-api")]
    AnthropicApi {
        /// Port to run the server on
        #[arg(long, default_value = "11434")]
        port: u16,
        /// Host address to bind to
        #[arg(long, default_value = "127.0.0.1")]
        host: String,
    },
}

impl Default for Cli {
    fn default() -> Self {
        Self {
            color: ColorSelection { color: ColorChoice::Auto },
            workspace_path: None,
            model: Some(ModelId::default().to_string()),
            provider: Some("gemini".to_owned()),
            api_key_env: "GEMINI_API_KEY".to_owned(),
            workspace: None,
            research_preview: false,
            security_level: "moderate".to_owned(),
            show_file_diffs: false,
            max_concurrent_ops: 5,
            api_rate_limit: 30,
            max_tool_calls: 10,
            verbose: false,
            quiet: false,
            config: Vec::new(),
            log_level: "info".to_owned(),
            no_color: false,
            theme: None,
            skip_confirmations: false,
            codex_experimental: false,
            no_codex_experimental: false,
            print: None,
            full_auto: None,
            resume_session: None,
            continue_latest: false,
            fork_session: None,
            all: false,
            session_id: None,
            summarize: false,
            debug: false,
            enable_skills: false,
            tick_rate: 250,
            frame_rate: 60,
            agent: None,
            allowed_tools: Vec::new(),
            disallowed_tools: Vec::new(),
            dangerously_skip_permissions: false,
            ide: false,
            chrome: false,
            no_chrome: false,
            command: Some(Commands::Chat),
        }
    }
}

impl Cli {
    /// Get the model to use, with fallback to default
    pub fn get_model(&self) -> String {
        self.model.clone().unwrap_or_else(|| ModelId::default().to_string())
    }

    /// Load configuration from a simple TOML-like file without external deps
    ///
    /// Supported keys (top-level): model, api_key_env, verbose, log_level, workspace
    /// Example:
    ///   model = "gemini-3-flash-preview"
    ///   api_key_env = "GEMINI_API_KEY"
    ///   verbose = true
    ///   log_level = "info"
    ///   workspace = "/path/to/workspace"
    pub async fn load_config(&self) -> anyhow::Result<ConfigFile> {
        use tokio::fs;

        // Resolve candidate path
        let explicit_path = self.config.iter().find_map(|entry| {
            let trimmed = entry.trim();
            if trimmed.contains('=') || trimmed.is_empty() {
                None
            } else {
                Some(PathBuf::from(trimmed))
            }
        });

        let path = if let Some(p) = explicit_path {
            p
        } else {
            let cwd = env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
            let primary = cwd.join("vtcode.toml");
            let secondary = cwd.join(".vtcode.toml");
            if fs::try_exists(&primary).await.unwrap_or(false) {
                primary
            } else if fs::try_exists(&secondary).await.unwrap_or(false) {
                secondary
            } else {
                // No config file; return empty config
                return Ok(ConfigFile {
                    model: None,
                    provider: None,
                    api_key_env: None,
                    verbose: None,
                    log_level: None,
                    workspace: None,
                    tools: None,
                    context: None,
                    logging: None,
                    performance: None,
                    security: None,
                });
            }
        };

        let text = fs::read_to_string(&path).await?;

        // Very small parser: key = value, supports quoted strings, booleans, and plain paths
        let mut cfg = ConfigFile {
            model: None,
            provider: None,
            api_key_env: None,
            verbose: None,
            log_level: None,
            workspace: None,
            tools: None,
            context: None,
            logging: None,
            performance: None,
            security: None,
        };

        for raw_line in text.lines() {
            let line = raw_line.trim();
            if line.is_empty() || line.starts_with('#') || line.starts_with("//") {
                continue;
            }
            // Strip inline comments after '#'
            let line = match line.find('#') {
                Some(idx) => &line[..idx],
                None => line,
            }
            .trim();

            // Expect key = value
            let mut parts = line.splitn(2, '=');
            let key = parts.next().map(|s| s.trim()).unwrap_or("");
            let val = parts.next().map(|s| s.trim()).unwrap_or("");
            if key.is_empty() || val.is_empty() {
                continue;
            }

            // Remove surrounding quotes if present
            let unquote = |s: &str| -> String {
                let s = s.trim();
                if (s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')) {
                    s[1..s.len() - 1].to_owned()
                } else {
                    s.to_owned()
                }
            };

            match key {
                "model" => cfg.model = Some(unquote(val)),
                "api_key_env" => cfg.api_key_env = Some(unquote(val)),
                "verbose" => {
                    let v = unquote(val).to_lowercase();
                    cfg.verbose = Some(matches!(v.as_str(), "true" | "1" | "yes"));
                }
                "log_level" => cfg.log_level = Some(unquote(val)),
                "workspace" => {
                    let v = unquote(val);
                    let p = if Path::new(&v).is_absolute() {
                        PathBuf::from(v)
                    } else {
                        // Resolve relative to config file directory
                        let base = path.parent().unwrap_or(Path::new("."));
                        base.join(v)
                    };
                    cfg.workspace = Some(p);
                }
                _ => {
                    // Ignore unknown keys in this minimal parser
                }
            }
        }

        Ok(cfg)
    }

    /// Get the effective workspace path
    pub fn get_workspace(&self) -> PathBuf {
        self.workspace
            .clone()
            .unwrap_or_else(|| env::current_dir().unwrap_or_else(|_| PathBuf::from(".")))
    }

    /// Get the effective API key environment variable
    ///
    /// Automatically infers the API key environment variable based on the provider
    /// when the current value matches the default or is not explicitly set.
    pub fn get_api_key_env(&self) -> String {
        crate::config::api_keys::resolve_api_key_env(
            self.provider
                .as_deref()
                .unwrap_or(crate::config::constants::defaults::DEFAULT_PROVIDER),
            &self.api_key_env,
        )
    }

    /// Check if verbose mode is enabled
    pub fn is_verbose(&self) -> bool {
        self.verbose
    }

    /// Check if performance monitoring is enabled
    /// Check if research-preview features are enabled
    pub fn is_research_preview_enabled(&self) -> bool {
        self.research_preview
    }

    /// Get the security level
    pub fn get_security_level(&self) -> &str {
        &self.security_level
    }

    /// Check if debug mode is enabled (includes verbose)
    pub fn is_debug_mode(&self) -> bool {
        self.debug || self.verbose
    }

    pub fn codex_experimental_override(&self) -> Option<bool> {
        if self.codex_experimental {
            Some(true)
        } else if self.no_codex_experimental {
            Some(false)
        } else {
            None
        }
    }
}

fn parse_workspace_directory(raw: &str) -> Result<PathBuf, String> {
    let candidate = PathBuf::from(raw);
    if !candidate.exists() {
        return Err(format!(
            "'{}' is not a valid workspace path or subcommand.\n\
             Run `vtcode --help` to see available commands and options.",
            raw
        ));
    }

    Ok(candidate)
}

pub fn long_version() -> String {
    use crate::config::defaults::{get_config_dir, get_data_dir};

    let git_info = option_env!("VT_CODE_GIT_INFO").unwrap_or(env!("CARGO_PKG_VERSION"));

    let config_dir = get_config_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| "~/.vtcode/".to_string());

    let data_dir = get_data_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| "~/.vtcode/cache/".to_string());

    format!(
        "{}\n\nAuthors: {}\nConfig directory: {}\nData directory: {}\n\nEnvironment variables:\n  VTCODE_CONFIG - Override config directory\n  VTCODE_DATA - Override data directory",
        git_info,
        env!("CARGO_PKG_AUTHORS"),
        config_dir,
        data_dir
    )
}

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

    #[test]
    fn long_version_includes_expected_sections() {
        let text = long_version();
        assert!(text.contains("Authors:"));
        assert!(text.contains("Config directory:"));
        assert!(text.contains("Data directory:"));
        assert!(text.contains("VTCODE_CONFIG"));
        assert!(text.contains("VTCODE_DATA"));
    }

    #[test]
    fn long_version_starts_with_build_git_info() {
        let text = long_version();
        let expected = option_env!("VT_CODE_GIT_INFO").unwrap_or(env!("CARGO_PKG_VERSION"));
        assert!(text.starts_with(expected));
    }

    #[test]
    fn config_file_api_key_env_uses_provider_default() {
        let cli = Cli::parse_from(["vtcode", "--provider", "minimax"]);

        assert_eq!(cli.get_api_key_env(), "MINIMAX_API_KEY");
    }

    #[test]
    fn config_file_api_key_env_preserves_explicit_override() {
        let cli = Cli::parse_from(["vtcode", "--provider", "openai", "--api-key-env", "CUSTOM_OPENAI_KEY"]);

        assert_eq!(cli.get_api_key_env(), "CUSTOM_OPENAI_KEY");
    }

    #[test]
    fn parses_app_server_command_with_stdio_listen_target() {
        let cli = Cli::parse_from(["vtcode", "app-server", "--listen", "stdio://"]);

        assert!(matches!(
            cli.command,
            Some(Commands::AppServer { ref listen }) if listen == "stdio://"
        ));
    }

    #[test]
    fn parses_init_force_flag() {
        let cli = Cli::parse_from(["vtcode", "init", "--force"]);

        assert!(matches!(cli.command, Some(Commands::Init { force: true })));
    }

    #[test]
    fn parses_codex_login_device_code_flag() {
        let cli = Cli::parse_from(["vtcode", "login", "codex", "--device-code"]);

        assert!(matches!(
            cli.command,
            Some(Commands::Login {
                ref provider,
                device_code: true
            }) if provider == "codex"
        ));
    }

    #[test]
    fn parses_codex_experimental_flags() {
        let enabled = Cli::parse_from(["vtcode", "--codex-experimental"]);
        assert_eq!(enabled.codex_experimental_override(), Some(true));

        let disabled = Cli::parse_from(["vtcode", "--no-codex-experimental"]);
        assert_eq!(disabled.codex_experimental_override(), Some(false));
    }

    #[test]
    fn codex_experimental_flags_conflict() {
        let result = Cli::try_parse_from(["vtcode", "--codex-experimental", "--no-codex-experimental"]);

        result.unwrap_err();
    }

    #[test]
    fn parses_create_project_feature_flags() {
        let cli = Cli::parse_from([
            "vtcode",
            "create-project",
            "demo",
            "--feature",
            "web",
            "--feature",
            "db",
        ]);

        assert!(matches!(
            cli.command,
            Some(Commands::CreateProject { ref name, ref features })
                if name == "demo" && features == &vec!["web".to_string(), "db".to_string()]
        ));
    }

    #[test]
    fn parses_revert_partial_long_flag() {
        let cli = Cli::parse_from(["vtcode", "revert", "--turn", "3", "--partial", "code"]);

        assert!(matches!(
            cli.command,
            Some(Commands::Revert {
                turn: 3,
                partial: Some(ref scope)
            }) if scope == "code"
        ));
    }
}