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