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