Skip to main content

blyte_cli/
cli.rs

1use clap::Parser;
2use serde::{Deserialize, Serialize};
3use std::path::PathBuf;
4
5#[derive(clap::Subcommand, Debug, PartialEq, Clone)]
6#[allow(clippy::large_enum_variant)]
7pub enum Command {
8    /// Detect this repository's checks and write blyte.toml
9    Init(InitArgs),
10    /// Serve a local dashboard of running and recent jobs
11    Dashboard(DashboardArgs),
12    /// Serve a local replay of recent runs
13    #[command(hide = true)]
14    Mock(MockArgs),
15    /// Combine a request's passed runs on a work branch and run the repository's check
16    Combine(CombineArgs),
17    /// Run one task in its own git worktree (.wt/<id>, branch wip/<id>) and print a JSON summary
18    Dispatch(DispatchArgs),
19    /// Start a direct edit in its own git worktree (.wt/<id>, branch wip/<id>)
20    #[command(hide = true)]
21    EditStart(EditStartArgs),
22    /// Verify and record a direct edit started with `blyte edit-start`
23    #[command(hide = true)]
24    EditFinish(EditFinishArgs),
25    /// Show what a dispatched run did: commits, files, tree state, report, and optionally the diff
26    Review(ReviewArgs),
27    /// Remove finished worktrees and merged branches
28    Clean(CleanArgs),
29    /// Show what a dispatch is doing
30    Status {
31        id: String,
32        #[arg(long)]
33        json: bool,
34    },
35    /// Wait until dispatched runs finish (all of them, or the first with --any) and print one line per finished run
36    Wait {
37        /// Run ids, as given to `blyte dispatch`
38        #[arg(required = true, num_args = 1..)]
39        ids: Vec<String>,
40        /// Return as soon as at least one run has finished
41        #[arg(long)]
42        any: bool,
43        /// Give up after this many seconds (exit 2); stays under the 300 s shell-command limit
44        #[arg(long, default_value_t = 280)]
45        timeout: u64,
46    },
47    /// Install or print the Claude Code skill for the plan, delegate, review workflow
48    Skill(SkillArgs),
49    /// Replace this binary with the latest release (the old one is kept as blyte.prev)
50    Update {
51        /// Only show the installed and latest versions
52        #[arg(long, conflicts_with = "rollback")]
53        check: bool,
54        /// Swap back to the previous build (blyte.prev)
55        #[arg(long)]
56        rollback: bool,
57    },
58    /// Show conversation model and implementer costs
59    Cost(CostArgs),
60    /// Print Claude Code's status line with blyte costs
61    Statusline(StatuslineArgs),
62    /// List a plan's tasks with their state and the command to dispatch each ready one
63    Plan(PlanArgs),
64    /// Fail when the working tree adds comments since a base commit
65    #[command(hide = true)]
66    Comments(CommentsArgs),
67    /// Print the instructions the conversation model is given
68    #[command(hide = true)]
69    Instructions(InstructionsArgs),
70}
71
72#[derive(clap::Args, Debug, PartialEq, Clone)]
73pub struct InitArgs {
74    /// Overwrite an existing blyte.toml
75    #[arg(long)]
76    pub force: bool,
77
78    /// Print the file instead of writing it
79    #[arg(long)]
80    pub print: bool,
81
82    /// Skip gate verification on HEAD
83    #[arg(long)]
84    pub no_verify: bool,
85
86    /// Sandbox local command execution: 'on' or 'off' (default 'on')
87    #[arg(
88        long,
89        default_value = "on",
90        value_parser = ["on", "off"]
91    )]
92    pub sandbox: String,
93
94    /// Timeout in seconds for each verification step (default 600)
95    #[arg(long, default_value = "600", value_name = "SECONDS")]
96    pub step_timeout: u64,
97}
98
99#[derive(clap::Args, Debug, PartialEq, Clone)]
100pub struct DashboardArgs {
101    /// Port to listen on (0 for a free port)
102    #[arg(long, default_value = "7171", value_name = "N")]
103    pub port: u16,
104
105    /// Directory holding repositories or worktrees (repeatable)
106    #[arg(long = "root", value_name = "DIR")]
107    pub roots: Vec<PathBuf>,
108
109    /// Directory holding run logs and reports (repeatable)
110    #[arg(long = "runs-dir", value_name = "DIR")]
111    pub runs_dirs: Vec<PathBuf>,
112
113    /// How far back finished jobs are listed, in hours
114    #[arg(long, default_value = "24", value_name = "N")]
115    pub hours: u64,
116
117    /// Open the dashboard in the default browser
118    #[arg(long)]
119    pub open: bool,
120}
121
122#[derive(clap::Args, Debug, PartialEq, Clone)]
123pub struct SurfaceArgs {
124    #[arg(
125        long,
126        default_value_t = 0,
127        value_name = "N",
128        help = "Port to listen on (default: a free port)"
129    )]
130    pub port: u16,
131
132    #[arg(long, help = "Do not open the live surface in the browser")]
133    pub no_open: bool,
134
135    #[arg(
136        long,
137        default_value_t = 14,
138        value_name = "N",
139        value_parser = clap::value_parser!(u64).range(1..=31),
140        help = "How many days back to include finished runs (1 to 31)"
141    )]
142    pub days: u64,
143
144    #[arg(
145        long,
146        value_name = "NAME",
147        help = "Name for this surface (default: the repository directory name)"
148    )]
149    pub name: Option<String>,
150
151    #[arg(
152        long,
153        value_name = "NAME",
154        conflicts_with = "name",
155        help = "Restart a stopped surface, by name or id, with its transcript"
156    )]
157    pub resume: Option<String>,
158}
159
160impl Default for SurfaceArgs {
161    fn default() -> Self {
162        Self {
163            port: 0,
164            no_open: false,
165            days: 14,
166            name: None,
167            resume: None,
168        }
169    }
170}
171
172#[derive(clap::Args, Debug, PartialEq, Clone)]
173pub struct MockArgs {
174    /// Port to listen on (0 for a free port)
175    #[arg(long, default_value = "7272", value_name = "N")]
176    pub port: u16,
177
178    /// How many days back to include finished runs (1 to 31)
179    #[arg(
180        long,
181        default_value = "7",
182        value_name = "N",
183        value_parser = clap::value_parser!(u64).range(1..=31)
184    )]
185    pub days: u64,
186
187    /// Directory holding repositories or worktrees (repeatable)
188    #[arg(long = "root", value_name = "DIR")]
189    pub roots: Vec<PathBuf>,
190}
191
192#[derive(clap::Args, Debug, PartialEq, Clone)]
193pub struct CombineArgs {
194    /// Request identifier whose passed runs should be combined
195    pub request: String,
196}
197
198#[derive(clap::Args, Debug, PartialEq, Clone)]
199pub struct EditStartArgs {
200    /// Identifier for this direct edit (used for worktree and branch wip/<id>)
201    pub id: String,
202
203    /// Short description of the requested change
204    #[arg(long, value_name = "TEXT")]
205    pub task: String,
206
207    /// The commit the new worktree starts from
208    #[arg(long, default_value = "HEAD")]
209    pub base: String,
210}
211
212#[derive(clap::Args, Debug, PartialEq, Clone)]
213pub struct EditFinishArgs {
214    /// Identifier given to `blyte edit-start`
215    pub id: String,
216
217    /// Request identifier this edit belongs to
218    #[arg(long, value_name = "ID")]
219    pub request: Option<String>,
220
221    /// Surface identifier that started this edit
222    #[arg(long, value_name = "ID")]
223    pub surface: Option<String>,
224
225    /// Commit message used to commit uncommitted or untracked changes in the worktree
226    #[arg(long, value_name = "TEXT")]
227    pub message: Option<String>,
228}
229
230#[derive(clap::Args, Debug, PartialEq, Clone)]
231pub struct DispatchArgs {
232    /// Identifier for this dispatch (used for worktree and branch wip/<id>)
233    pub id: String,
234
235    /// Path to the task description markdown file; optional with --plan and --plan-task, which take the task from the plan
236    pub task_file: Option<PathBuf>,
237
238    /// The check command; several lines are several steps, all of which must pass
239    #[arg(short, long)]
240    pub gate: Option<String>,
241
242    /// Tag for this dispatch (repeatable)
243    #[arg(long = "tag", value_name = "TEXT")]
244    pub tags: Vec<String>,
245
246    /// Path to the plan file this dispatch belongs to
247    #[arg(long, value_name = "PATH")]
248    pub plan: Option<String>,
249
250    /// Plan task this dispatch implements
251    #[arg(long, value_name = "TEXT")]
252    pub plan_task: Option<String>,
253
254    /// Request identifier this dispatch belongs to
255    #[arg(long, value_name = "ID")]
256    pub request: Option<String>,
257
258    /// Surface identifier that started this dispatch
259    #[arg(long, value_name = "ID")]
260    pub surface: Option<String>,
261
262    /// The commit a new worktree starts from
263    #[arg(long, default_value = "HEAD")]
264    pub base: String,
265
266    /// Run read-only research: scratch repository, report gate, findings in report.md
267    #[arg(long, conflicts_with = "gate", conflicts_with = "base")]
268    pub research: bool,
269
270    /// Copy the finished research report to this path (only with --research)
271    #[arg(long = "report-to", requires = "research", value_name = "PATH")]
272    pub report_to: Option<PathBuf>,
273
274    /// Model identifier or provider selection
275    #[arg(short, long)]
276    pub model: Option<String>,
277
278    /// Wall-clock budget in seconds (default 3600)
279    #[arg(long)]
280    pub max_seconds: Option<u64>,
281
282    /// Maximum turn budget (default 300)
283    #[arg(long)]
284    pub max_turns: Option<u32>,
285
286    /// Maximum spend in USD (default 25)
287    #[arg(long = "max-cost")]
288    pub max_cost_usd: Option<f64>,
289
290    /// Approximate token budget for the verbatim tail before folding
291    #[arg(long)]
292    pub fold_budget: Option<usize>,
293
294    /// Sandbox local command execution: 'on' or 'off' (default 'on')
295    #[arg(
296        long,
297        default_value = "on",
298        value_parser = ["on", "off"]
299    )]
300    pub sandbox: String,
301
302    /// Outbound network mode: 'on' or 'off' (default 'on')
303    #[arg(
304        long,
305        value_parser = ["on", "off"]
306    )]
307    pub network: Option<String>,
308
309    /// Let sandboxed commands read this path despite credential deny-list (repeatable)
310    #[arg(long = "allow-read")]
311    pub allow_read: Vec<PathBuf>,
312
313    /// Start even if the task names paths outside the workspace and --allow-read
314    #[arg(long = "no-path-check")]
315    pub no_path_check: bool,
316
317    /// Research runs: no warning for missing brief sections, and allow a question that names an unverified report
318    #[arg(long = "no-brief-check")]
319    pub no_brief_check: bool,
320
321    /// Skip the model claim check on report.md for research runs
322    #[arg(long = "no-claim-check")]
323    pub no_claim_check: bool,
324
325    /// Skip the pre-run gate check on the untouched worktree
326    #[arg(long = "no-base-check")]
327    pub no_base_check: bool,
328
329    /// Pass this environment variable to commands (repeatable)
330    #[arg(long = "pass-env")]
331    pub pass_env: Vec<String>,
332}
333
334#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
335#[value(rename_all = "lowercase")]
336#[serde(rename_all = "lowercase")]
337pub enum ReviewVerdict {
338    Accepted,
339    Fixed,
340    Redone,
341}
342
343impl ReviewVerdict {
344    pub fn as_str(&self) -> &'static str {
345        match self {
346            Self::Accepted => "accepted",
347            Self::Fixed => "fixed",
348            Self::Redone => "redone",
349        }
350    }
351}
352
353impl std::fmt::Display for ReviewVerdict {
354    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
355        write!(f, "{}", self.as_str())
356    }
357}
358
359#[derive(clap::Args, Debug, PartialEq, Clone)]
360pub struct ReviewArgs {
361    pub id: String,
362
363    #[arg(long)]
364    pub diff: bool,
365
366    #[arg(long, value_enum)]
367    pub verdict: Option<ReviewVerdict>,
368
369    #[arg(long, value_name = "TEXT", requires = "verdict")]
370    pub notes: Option<String>,
371
372    /// Ask the implementer model for a first review of the diff against the task (trial)
373    #[arg(long, conflicts_with = "verdict")]
374    pub model_review: bool,
375}
376
377#[derive(clap::Args, Debug, PartialEq, Clone)]
378pub struct CleanArgs {
379    /// Show what would be removed without deleting anything
380    #[arg(long)]
381    pub dry_run: bool,
382
383    /// Worktrees older than this many days are removable without a verdict (default 14)
384    #[arg(long, default_value = "14", value_name = "DAYS")]
385    pub older_than: u64,
386
387    /// Clean every repository in the registry
388    #[arg(long)]
389    pub all: bool,
390
391    #[arg(long, hide = true, conflicts_with_all = ["all", "dry_run"])]
392    pub quiet: bool,
393
394    #[arg(long, hide = true, value_name = "ID", conflicts_with_all = ["all", "dry_run"])]
395    pub skip: Option<String>,
396}
397
398#[derive(clap::Args, Debug, PartialEq, Clone)]
399pub struct CostArgs {
400    /// Session identifier to filter by (an unknown session prints zero totals and exits 0)
401    #[arg(long, value_name = "ID")]
402    pub session: Option<String>,
403
404    /// Break down totals by session id
405    #[arg(long)]
406    pub by_session: bool,
407
408    /// Filter runs at or after this Unix timestamp in seconds
409    #[arg(long, value_name = "UNIX_SECS")]
410    pub since: Option<u64>,
411
412    /// Repository path to filter by
413    #[arg(long, value_name = "PATH")]
414    pub repo: Option<PathBuf>,
415
416    /// Output totals as JSON
417    #[arg(long)]
418    pub json: bool,
419}
420
421#[derive(clap::Args, Debug, PartialEq, Clone)]
422pub struct StatuslineArgs {
423    /// Command to generate the base line before appending blyte costs
424    #[arg(long, value_name = "COMMAND")]
425    pub after: Option<String>,
426}
427
428#[derive(clap::Args, Debug, PartialEq, Clone)]
429pub struct PlanArgs {
430    /// Path to the plan markdown file
431    pub plan: PathBuf,
432
433    /// Output task statuses as JSON
434    #[arg(long)]
435    pub json: bool,
436}
437
438#[derive(clap::Args, Debug, PartialEq, Clone)]
439pub struct CommentsArgs {
440    /// Base commit or revision to compare the working tree against
441    #[arg(long, value_name = "REV")]
442    pub base: String,
443}
444
445#[derive(clap::Args, Debug, PartialEq, Clone)]
446pub struct InstructionsArgs {
447    /// Print the built-in default process and exit
448    #[arg(long, conflicts_with_all = ["one_shot", "dispatch_tool"])]
449    pub default_process: bool,
450
451    /// Show the instructions for a one-shot run
452    #[arg(long)]
453    pub one_shot: bool,
454
455    /// Show the instructions for `--dispatch-cmd` mode
456    #[arg(long)]
457    pub dispatch_tool: bool,
458
459    /// Show the instructions for sandbox 'on' or 'off'
460    #[arg(long, value_parser = ["on", "off"])]
461    pub sandbox: Option<String>,
462}
463
464#[derive(clap::Subcommand, Debug, PartialEq, Clone)]
465pub enum SkillAction {
466    /// Install the Claude Code skill
467    Install {
468        /// Install to repository instead of personal ~/.claude
469        #[arg(long)]
470        project: bool,
471
472        /// Overwrite even if existing skill was modified
473        #[arg(long)]
474        force: bool,
475
476        /// Also tell Claude Code to delegate implementation to blyte by default, in CLAUDE.md
477        #[arg(long, hide = true)]
478        default: bool,
479
480        /// Install the skill without making blyte the default in CLAUDE.md
481        #[arg(long)]
482        no_default: bool,
483
484        /// Do not set Claude Code's status line
485        #[arg(long)]
486        no_statusline: bool,
487
488        /// Do not set Claude Code's auto-compact window
489        #[arg(long)]
490        no_autocompact: bool,
491
492        /// Which conversation model to set up: claude (Claude Code), codex (Codex CLI) or all
493        #[arg(
494            long,
495            default_value = "claude",
496            value_parser = ["claude", "codex", "all"]
497        )]
498        harness: String,
499
500        /// Default implementer model to write to the env file
501        #[arg(long, value_name = "ID")]
502        model: Option<String>,
503    },
504
505    /// Remove the skill and the CLAUDE.md block
506    Uninstall {
507        /// Remove from repository instead of personal ~/.claude
508        #[arg(long)]
509        project: bool,
510
511        /// Which conversation model to set up: claude (Claude Code), codex (Codex CLI) or all
512        #[arg(
513            long,
514            default_value = "claude",
515            value_parser = ["claude", "codex", "all"]
516        )]
517        harness: String,
518    },
519
520    /// Print the skill markdown to stdout
521    Print,
522}
523
524#[derive(clap::Args, Debug, PartialEq, Clone)]
525pub struct SkillArgs {
526    #[command(subcommand)]
527    pub action: SkillAction,
528}
529
530#[derive(Parser, Debug, PartialEq)]
531#[command(
532    name = "blyte",
533    version = concat!(env!("CARGO_PKG_VERSION"), " (", env!("BLYTE_BUILD"), ")"),
534    about = "A lean coding-agent harness with a sealed verification gate\nBare `blyte` starts the live surface for the current repository.",
535    subcommand_precedence_over_arg = true
536)]
537pub struct Cli {
538    #[command(subcommand)]
539    pub command: Option<Command>,
540
541    /// Direct task prompt to run in single-shot / headless mode
542    #[arg(index = 1)]
543    pub prompt: Option<String>,
544
545    /// Verification gate command (e.g. 'pytest', 'cargo test'). Several steps may be given one per line: every step runs and all failures are reported together.
546    #[arg(short, long)]
547    pub gate: Option<String>,
548
549    /// Run blyte as the conversation model: plan, dispatch implementers with `blyte dispatch`, review and merge
550    #[arg(long, conflicts_with = "gate")]
551    pub converse: bool,
552
553    /// Command to run on the host when the conversation model delegates work via the `dispatch` tool
554    #[arg(long, requires = "converse")]
555    pub dispatch_cmd: Option<String>,
556
557    /// Context strategy: 'conversational' (append-all chat) or 'folded' (append-only chat, oldest half folded into digests past a budget)
558    #[arg(
559        long,
560        default_value = "folded",
561        value_parser = ["conversational", "folded"]
562    )]
563    pub context: String,
564
565    /// Model identifier or provider selection
566    #[arg(short, long)]
567    pub model: Option<String>,
568
569    /// Run inside an isolated container workspace instead of local host
570    #[arg(long)]
571    pub container: Option<String>,
572
573    /// Maximum turn budget
574    #[arg(long, default_value = "25")]
575    pub max_turns: usize,
576
577    /// Maximum verification gate attempts before failing
578    #[arg(long, default_value = "5")]
579    pub max_gates: usize,
580
581    /// Enable automatic rollback to baseline git commit if gate fails (disable with --rollback=false)
582    #[arg(
583        long,
584        default_value_t = true,
585        action = clap::ArgAction::Set,
586        num_args = 0..=1,
587        require_equals = true,
588        default_missing_value = "true",
589        value_parser = clap::builder::BoolishValueParser::new()
590    )]
591    pub rollback: bool,
592
593    /// Working directory inside the container when --container is set
594    #[arg(long, default_value = "/workspace")]
595    pub container_workdir: String,
596
597    /// Wall-clock budget in seconds; the run stops itself and reports when it is spent
598    #[arg(long)]
599    pub max_seconds: Option<u64>,
600
601    /// Maximum spend in USD; the run stops before a model call once reached
602    #[arg(long = "max-cost")]
603    pub max_cost_usd: Option<f64>,
604
605    /// Write a JSON run report to this path when the headless run exits
606    #[arg(long)]
607    pub report: Option<PathBuf>,
608
609    /// Approximate token budget for the verbatim tail before folding (folded context; default 48000)
610    #[arg(long)]
611    pub fold_budget: Option<usize>,
612
613    /// Sandbox the commands blyte runs on this machine (macOS Seatbelt, Linux Landlock): 'on' or 'off'
614    #[arg(
615        long,
616        default_value = "on",
617        value_parser = ["on", "off"]
618    )]
619    pub sandbox: String,
620
621    /// Let commands blyte runs connect out: 'on' or 'off' (default 'on', or 'off' under --converse). 'off' needs the sandbox
622    #[arg(
623        long,
624        value_parser = ["on", "off"]
625    )]
626    pub network: Option<String>,
627
628    /// Let sandboxed commands read this path despite the credential deny-list (repeatable)
629    #[arg(long = "allow-read")]
630    pub allow_read: Vec<PathBuf>,
631
632    /// Pass this environment variable to commands even though it looks like a credential (repeatable)
633    #[arg(long = "pass-env")]
634    pub pass_env: Vec<String>,
635
636    /// Directory allowed for reading with the read and grep tools (repeatable)
637    #[arg(long = "read-root", value_name = "DIR")]
638    pub read_roots: Vec<PathBuf>,
639
640    /// Gates in this run do not take a machine-wide slot
641    #[arg(long)]
642    pub no_gate_slot: bool,
643
644    /// Whether this is a research run: periodically remind the model to keep report.md updated
645    #[arg(long)]
646    pub research: bool,
647
648    /// Research run: check report.md's claims against their cited lines
649    #[arg(long = "claim-check")]
650    pub claim_check: bool,
651
652    /// Run the verification gate once before the first model call and stop as blocked if it fails
653    #[arg(long = "base-check")]
654    pub base_check: bool,
655
656    /// Directory containing an untouched checkout of the base commit for running the base check beside the model's first turns
657    #[arg(long = "base-check-dir", value_name = "DIR")]
658    pub base_check_dir: Option<PathBuf>,
659
660    #[arg(
661        long,
662        help = "Run the interactive terminal session instead of starting the live surface"
663    )]
664    pub terminal: bool,
665
666    #[arg(
667        long,
668        value_name = "N",
669        help = "Port to listen on (default: a free port)"
670    )]
671    pub port: Option<u16>,
672
673    #[arg(long = "no-open", help = "Do not open the live surface in the browser")]
674    pub no_open: bool,
675
676    #[arg(
677        long,
678        value_name = "N",
679        value_parser = clap::value_parser!(u64).range(1..=31),
680        help = "How many days back to include finished runs in the live surface (1 to 31, default 14)"
681    )]
682    pub days: Option<u64>,
683
684    #[arg(
685        long,
686        value_name = "NAME",
687        help = "Name for this surface (default: the repository directory name)"
688    )]
689    pub name: Option<String>,
690
691    #[arg(
692        long,
693        value_name = "NAME",
694        conflicts_with = "name",
695        help = "Restart a stopped surface, by name or id, with its transcript"
696    )]
697    pub resume: Option<String>,
698}
699
700/// Turn and context defaults for `--converse`. A flag the user gave explicitly wins.
701pub fn apply_converse_defaults(cli: &mut Cli, matches: &clap::ArgMatches) {
702    if !cli.converse {
703        return;
704    }
705    if matches.value_source("max_turns") == Some(clap::parser::ValueSource::DefaultValue) {
706        cli.max_turns = 1000;
707    }
708    if cli.fold_budget.is_none() {
709        cli.fold_budget = Some(100_000);
710    }
711}