Skip to main content

vitrify_cli/
cli.rs

1//! The complete command tree, via clap derive.
2//! Non-interactive always; stdout data, stderr diagnostics.
3//! Every argument carries help text; each command's `--help` adds its block
4//! from [`help::verbs`], and the root help is [`help::root`].
5//! `tests/cli_help.rs` holds all three.
6
7use clap::{Args, Parser, Subcommand, ValueEnum};
8
9use crate::help;
10
11#[derive(Parser)]
12#[command(
13    name = "vitrify",
14    version,
15    about = help::root::ABOUT,
16    help_template = help::root::TEMPLATE,
17    before_help = help::root::BEFORE_SHORT,
18    before_long_help = help::root::BEFORE_LONG,
19    after_help = help::root::AFTER_SHORT,
20    after_long_help = help::root::AFTER_LONG
21)]
22pub struct Cli {
23    #[command(subcommand)]
24    pub cmd: Cmd,
25}
26
27#[derive(Subcommand)]
28pub enum Cmd {
29    /// Find SDD instances; list units and claimability
30    #[command(after_long_help = help::verbs::LOCATE)]
31    Locate {
32        /// List units of one SDD with the computed claimable flag
33        #[arg(long)]
34        units: bool,
35        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
36        /// (default: every SDD; with --units, the one SDD in the repository)
37        #[arg(long, value_name = "ROOT")]
38        sdd: Option<String>,
39        /// Print JSON on stdout instead of text
40        #[arg(long)]
41        json: bool,
42    },
43    /// Print the ordered minimal reading set for one unit
44    #[command(after_long_help = help::verbs::READSET)]
45    Readset {
46        /// Unit ID, e.g. U-012
47        #[arg(long)]
48        unit: String,
49        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
50        /// (default: the one SDD in the repository)
51        #[arg(long, value_name = "ROOT")]
52        sdd: Option<String>,
53        /// Print JSON on stdout instead of text
54        #[arg(long)]
55        json: bool,
56        /// Total bytes and fail over budget (default: lint.readset_budget_bytes)
57        #[arg(long, num_args = 0..=1, value_name = "BYTES")]
58        budget: Option<Option<i64>>,
59    },
60    /// Claim a unit lease (atomic mainline transaction)
61    #[command(after_long_help = help::verbs::CLAIM)]
62    Claim(ClaimArgs),
63    /// Renew a held lease
64    #[command(after_long_help = help::verbs::RENEW)]
65    Renew(ClaimArgs),
66    /// Release a held lease
67    #[command(after_long_help = help::verbs::RELEASE)]
68    Release(ClaimArgs),
69    /// Record or verify a unit's input-trace hashes
70    #[command(after_long_help = help::verbs::TRACE)]
71    Trace {
72        #[command(subcommand)]
73        mode: TraceMode,
74    },
75    /// Regenerate the derived ledger or print the progress line
76    #[command(after_long_help = help::verbs::LEDGER)]
77    Ledger {
78        #[command(subcommand)]
79        mode: LedgerMode,
80    },
81    /// Append journal events; replay and verify the journal
82    #[command(after_long_help = help::verbs::JOURNAL)]
83    Journal {
84        #[command(subcommand)]
85        mode: JournalMode,
86    },
87    /// The mechanical drift audit (seven checks)
88    #[command(after_long_help = help::verbs::AUDIT)]
89    Audit {
90        /// Print JSON on stdout instead of text
91        #[arg(long)]
92        json: bool,
93        /// Return lapsed Claimed units to Ready (release event, actor audit)
94        #[arg(long)]
95        fix_leases: bool,
96        /// Write Stale marks for check 4–5 findings
97        #[arg(long)]
98        apply_stale: bool,
99        /// Skip re-running Verified units' Verification tables
100        #[arg(long)]
101        skip_rotten: bool,
102        /// Append one `audit` journal event
103        #[arg(long)]
104        journal: bool,
105        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
106        /// (default: the one SDD in the repository)
107        #[arg(long, value_name = "ROOT")]
108        sdd: Option<String>,
109        /// Audit every SDD of the repository
110        #[arg(long, conflicts_with = "sdd")]
111        all: bool,
112    },
113    /// Print the procedure index, or the operative procedure for a task
114    #[command(after_long_help = help::verbs::BRIEF)]
115    Brief {
116        /// The procedure to print (none: the index)
117        #[arg(value_enum, value_name = "TASK")]
118        phase: Option<BriefPhase>,
119        /// Unit ID (build-unit worker-prompt generation)
120        #[arg(long)]
121        unit: Option<String>,
122        /// Holder identity (build-unit worker-prompt generation)
123        #[arg(long)]
124        holder: Option<String>,
125        /// SDD root for the worker prompt: a bundle directory, its sdd.md,
126        /// or a single *.sdd.md (default: the one SDD in the repository)
127        #[arg(long, value_name = "ROOT")]
128        sdd: Option<String>,
129    },
130    /// Set this machine up: ~/.vitrify/config.toml with every default, then a first probe
131    #[command(after_long_help = help::verbs::INIT)]
132    Init {
133        /// Print JSON on stdout instead of text
134        #[arg(long)]
135        json: bool,
136    },
137    /// Probe git and the harness CLIs again; update ~/.vitrify/config.toml after asking
138    #[command(after_long_help = help::verbs::PROBE)]
139    Probe {
140        /// Print JSON on stdout instead of text
141        #[arg(long)]
142        json: bool,
143        /// Apply the changes without asking
144        #[arg(short = 'y', long)]
145        assume_yes: bool,
146    },
147    /// The deterministic orchestrator: run a complete generation pass
148    #[command(
149        args_conflicts_with_subcommands = true,
150        after_long_help = help::verbs::RUN
151    )]
152    Run {
153        /// Print preflight results and the plan, then stop
154        #[arg(long)]
155        dry_run: bool,
156        /// Override run.worker_cap: workers at once
157        #[arg(long, value_name = "N")]
158        cap: Option<i64>,
159        /// Override run.gate_jobs: Verification rows run at once (default
160        /// 0 = one per CPU core; 1 = one at a time)
161        #[arg(long, value_name = "N")]
162        gate_jobs: Option<i64>,
163        /// When only blocked units remain, keep running and resume when
164        /// the spec changes, instead of ending Blocked
165        #[arg(long)]
166        wait: bool,
167        /// SDD root to build: a bundle directory, its sdd.md, or a single
168        /// *.sdd.md (default: the one SDD in the repository)
169        #[arg(long, value_name = "ROOT")]
170        sdd: Option<String>,
171        /// Mainline branch the run publishes to (default: config mainline,
172        /// else the primary checkout's branch, else origin/HEAD)
173        #[arg(long, value_name = "BRANCH")]
174        mainline: Option<String>,
175        #[command(subcommand)]
176        sub: Option<RunSub>,
177    },
178    /// Mechanical authoring checks (L1–L11)
179    #[command(after_long_help = help::verbs::LINT)]
180    Lint {
181        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
182        /// (default: the one SDD in the repository)
183        #[arg(long, value_name = "ROOT")]
184        sdd: Option<String>,
185        /// Lint every SDD of the repository
186        #[arg(long, conflicts_with = "sdd")]
187        all: bool,
188        /// The SDD is being built, or has been: leave out L7 (state that
189        /// must not exist before the build) — what `run` preflights with
190        #[arg(long)]
191        built: bool,
192        /// Print JSON on stdout instead of text
193        #[arg(long)]
194        json: bool,
195    },
196    /// Every consistency check vitrify has, over one SDD or all of them
197    #[command(after_long_help = help::verbs::CHECK)]
198    Check {
199        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
200        /// (default: the one SDD in the repository)
201        #[arg(long, value_name = "ROOT")]
202        sdd: Option<String>,
203        /// Check every SDD of the repository
204        #[arg(long, conflicts_with = "sdd")]
205        all: bool,
206        /// Skip re-running Verified units' Verification tables (audit check 4)
207        #[arg(long)]
208        skip_rotten: bool,
209        /// Print JSON on stdout instead of text
210        #[arg(long)]
211        json: bool,
212    },
213    /// Record a change entry's bookkeeping: journal events, Stale marks or
214    /// re-recorded traces, the ledger
215    #[command(after_long_help = help::verbs::CHANGE)]
216    Change {
217        #[command(subcommand)]
218        mode: ChangeMode,
219    },
220    /// External contracts: one SDD's pins on another's contracts, and its
221    /// transcriptions
222    #[command(after_long_help = help::verbs::PIN)]
223    Pin {
224        #[command(subcommand)]
225        mode: PinMode,
226    },
227    /// List the open questions (Q-###) and the units they block
228    #[command(after_long_help = help::verbs::QUESTIONS)]
229    Questions {
230        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
231        /// (default: the one SDD in the repository)
232        #[arg(long, value_name = "ROOT")]
233        sdd: Option<String>,
234        /// Every SDD of the repository
235        #[arg(long, conflicts_with = "sdd")]
236        all: bool,
237        /// Only the questions not marked resolved
238        #[arg(long)]
239        open: bool,
240        /// Print JSON on stdout instead of text
241        #[arg(long)]
242        json: bool,
243    },
244    /// Materialize instance templates into the current directory
245    #[command(after_long_help = help::verbs::NEW)]
246    New {
247        /// The document to materialize
248        #[arg(value_enum)]
249        kind: NewKind,
250        /// Bundle shape (SDD only; default)
251        #[arg(long, conflicts_with = "single")]
252        bundle: bool,
253        /// Single-file shape (SDD only)
254        #[arg(long)]
255        single: bool,
256        /// Instance name (slug)
257        name: Option<String>,
258    },
259    /// Print the embedded normative schema documents
260    #[command(after_long_help = help::verbs::SCHEMA)]
261    Schema {
262        #[command(subcommand)]
263        mode: SchemaMode,
264    },
265}
266
267/// What `vitrify run` does besides running: looking at a run from outside.
268#[derive(Subcommand)]
269pub enum RunSub {
270    /// Show what a run is doing, unit by unit (read-only)
271    #[command(after_long_help = help::verbs::RUN_STATUS)]
272    Status(StatusArgs),
273}
274
275/// `vitrify run status`: read-only, so it can be pointed at a live run from
276/// a second terminal without disturbing it.
277#[derive(Args, Clone)]
278pub struct StatusArgs {
279    /// Keep watching: one line per change, until the run ends
280    #[arg(long)]
281    pub follow: bool,
282    /// JSON: one object; with --follow, one event per line
283    #[arg(long)]
284    pub json: bool,
285    /// With --follow: first replay what the run recorded since it started
286    #[arg(long, requires = "follow")]
287    pub since_start: bool,
288    /// The run to show, by job ID (default: the latest)
289    #[arg(long, value_name = "JOB")]
290    pub job: Option<String>,
291    /// Seconds between looks while following
292    #[arg(long, default_value_t = 2, value_parser = clap::value_parser!(u64).range(1..))]
293    pub interval: u64,
294    /// Seconds without worker output before a worker is flagged quiet
295    #[arg(long, default_value_t = 300, value_parser = clap::value_parser!(u64).range(1..))]
296    pub quiet_after: u64,
297}
298
299#[derive(Args, Clone)]
300pub struct ClaimArgs {
301    /// Unit ID, e.g. U-012
302    #[arg(long)]
303    pub unit: String,
304    /// Lease holder identity
305    #[arg(long, value_name = "ID")]
306    pub holder: String,
307    /// Lease length in minutes
308    #[arg(long, default_value_t = 60, value_name = "N")]
309    pub lease_minutes: i64,
310    /// SDD root, as a path from where the verb runs (the primary checkout
311    /// or any worktree of it); default: the one SDD in the repository
312    #[arg(long, value_name = "ROOT")]
313    pub sdd: Option<String>,
314    /// Mainline branch the lease commit is published to (default: config
315    /// mainline, else the primary checkout's branch, else origin/HEAD)
316    #[arg(long, value_name = "BRANCH")]
317    pub mainline: Option<String>,
318}
319
320#[derive(Subcommand)]
321pub enum TraceMode {
322    /// Compute and write the trace map into unit front matter, and stamp
323    /// the unit Built or Verified
324    #[command(after_long_help = help::verbs::TRACE_RECORD)]
325    Record {
326        /// Unit ID, e.g. U-012
327        #[arg(long)]
328        unit: String,
329        /// The status to set, with its stamp
330        #[arg(long, value_enum)]
331        stamp: StampKind,
332        /// Short SHA for the stamp (default: HEAD)
333        #[arg(long, value_name = "SHA")]
334        commit: Option<String>,
335        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
336        /// (default: the one SDD in the repository)
337        #[arg(long, value_name = "ROOT")]
338        sdd: Option<String>,
339    },
340    /// Compare recorded hashes against current content
341    #[command(after_long_help = help::verbs::TRACE_VERIFY)]
342    Verify {
343        /// Unit ID, e.g. U-012
344        #[arg(long)]
345        unit: String,
346        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
347        /// (default: the one SDD in the repository)
348        #[arg(long, value_name = "ROOT")]
349        sdd: Option<String>,
350        /// Print JSON on stdout instead of text
351        #[arg(long)]
352        json: bool,
353    },
354}
355
356/// The status `trace record --stamp` sets.
357#[derive(Clone, Copy, ValueEnum)]
358pub enum StampKind {
359    /// Status Built; writes the `built` stamp (the worker's last step)
360    Built,
361    /// Status Verified; writes the `verified` stamp (the integrator, after the gate)
362    Verified,
363}
364
365#[derive(Subcommand)]
366pub enum LedgerMode {
367    /// Rewrite the derived ledger (never hand-edit it)
368    #[command(after_long_help = help::verbs::LEDGER_REGEN)]
369    Regen {
370        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
371        /// (default: the one SDD in the repository)
372        #[arg(long, value_name = "ROOT")]
373        sdd: Option<String>,
374        /// Every SDD of the repository
375        #[arg(long, conflicts_with = "sdd")]
376        all: bool,
377        /// Write nothing: compare the ledger on disk with a fresh
378        /// derivation; exit 3 when they differ
379        #[arg(long)]
380        check: bool,
381    },
382    /// Print the one-line progress summary
383    #[command(after_long_help = help::verbs::LEDGER_PROGRESS)]
384    Progress {
385        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
386        /// (default: the one SDD in the repository)
387        #[arg(long, value_name = "ROOT")]
388        sdd: Option<String>,
389        /// Every SDD of the repository
390        #[arg(long, conflicts_with = "sdd")]
391        all: bool,
392    },
393}
394
395#[derive(Subcommand)]
396pub enum ChangeMode {
397    /// Apply the last change entry of each Active SDD: the `change` event,
398    /// then Stale marks (MINOR, MAJOR) or re-recorded traces (PATCH), then
399    /// the ledger (working tree only)
400    #[command(after_long_help = help::verbs::CHANGE_APPLY)]
401    Apply {
402        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
403        /// (default: the one SDD in the repository)
404        #[arg(long, value_name = "ROOT")]
405        sdd: Option<String>,
406        /// Every SDD of the repository
407        #[arg(long, conflicts_with = "sdd")]
408        all: bool,
409        /// Print what would be done; write nothing
410        #[arg(long)]
411        dry_run: bool,
412        /// Code commit to record on the events
413        #[arg(long, value_name = "SHA")]
414        commit: Option<String>,
415        /// Print JSON on stdout instead of text
416        #[arg(long)]
417        json: bool,
418    },
419}
420
421#[derive(Subcommand)]
422pub enum PinMode {
423    /// List every external contract with its authority and state
424    #[command(after_long_help = help::verbs::PIN_LIST)]
425    List {
426        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
427        /// (default: the one SDD in the repository)
428        #[arg(long, value_name = "ROOT")]
429        sdd: Option<String>,
430        /// Every SDD of the repository
431        #[arg(long, conflicts_with = "sdd")]
432        all: bool,
433        /// Print JSON on stdout instead of text
434        #[arg(long)]
435        json: bool,
436    },
437    /// Rewrite stale pins from their authorities (working tree only);
438    /// print the units whose trace that moved
439    #[command(after_long_help = help::verbs::PIN_SYNC)]
440    Sync {
441        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
442        /// (default: the one SDD in the repository)
443        #[arg(long, value_name = "ROOT")]
444        sdd: Option<String>,
445        /// Every SDD of the repository
446        #[arg(long, conflicts_with = "sdd")]
447        all: bool,
448        /// Print what would be synced, and the Staleness applied lines; write nothing
449        #[arg(long)]
450        dry_run: bool,
451        /// Print JSON on stdout instead of text
452        #[arg(long)]
453        json: bool,
454    },
455}
456
457#[derive(Subcommand)]
458pub enum JournalMode {
459    /// Append one event (inside mainline-bound commits only)
460    #[command(after_long_help = help::verbs::JOURNAL_APPEND)]
461    Append {
462        /// The event kind
463        #[arg(long, value_name = "KIND")]
464        event: EventKind,
465        /// Unit ID (every kind except change and audit)
466        #[arg(long)]
467        unit: Option<String>,
468        /// Who acted: a lease holder's identity, or the role that acted
469        #[arg(long)]
470        actor: Option<String>,
471        /// The claim number the event belongs to (claim, renew, release)
472        #[arg(long, value_name = "N")]
473        claim_seq: Option<i64>,
474        /// Change entry id (CH-###)
475        #[arg(long)]
476        entry: Option<String>,
477        /// Comma-separated unit ids (change event)
478        #[arg(long)]
479        stale: Option<String>,
480        /// Cause text (stale event)
481        #[arg(long)]
482        cause: Option<String>,
483        /// Finding count (audit event)
484        #[arg(long, value_name = "N")]
485        findings: Option<i64>,
486        /// Relevant code commit
487        #[arg(long, value_name = "SHA")]
488        commit: Option<String>,
489        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
490        /// (default: the one SDD in the repository)
491        #[arg(long, value_name = "ROOT")]
492        sdd: Option<String>,
493    },
494    /// Replay from empty and compare the projection with front matter
495    #[command(after_long_help = help::verbs::JOURNAL_REPLAY)]
496    Replay {
497        /// Print JSON on stdout instead of text
498        #[arg(long)]
499        json: bool,
500        /// SDD root: a bundle directory, its sdd.md, or a single *.sdd.md
501        /// (default: the one SDD in the repository)
502        #[arg(long, value_name = "ROOT")]
503        sdd: Option<String>,
504        /// Every SDD of the repository
505        #[arg(long, conflicts_with = "sdd")]
506        all: bool,
507    },
508}
509
510/// The journal event kinds `journal append --event` takes.
511#[derive(Clone, Copy, ValueEnum, Debug)]
512pub enum EventKind {
513    /// A lease taken
514    Claim,
515    /// A lease extended
516    Renew,
517    /// A lease given back
518    Release,
519    /// A unit stamped Built
520    Built,
521    /// A unit stamped Verified
522    Verified,
523    /// A unit marked Stale (with --cause)
524    Stale,
525    /// A unit retired
526    Retired,
527    /// A change entry recorded (with --entry, and --stale for MINOR or MAJOR)
528    Change,
529    /// An audit run recorded (with --findings)
530    Audit,
531}
532
533impl EventKind {
534    pub fn as_str(self) -> &'static str {
535        match self {
536            EventKind::Claim => "claim",
537            EventKind::Renew => "renew",
538            EventKind::Release => "release",
539            EventKind::Built => "built",
540            EventKind::Verified => "verified",
541            EventKind::Stale => "stale",
542            EventKind::Retired => "retired",
543            EventKind::Change => "change",
544            EventKind::Audit => "audit",
545        }
546    }
547}
548
549/// The procedures `vitrify brief` prints, in index order.
550#[derive(Clone, Copy, ValueEnum, Debug, PartialEq, Eq)]
551pub enum BriefPhase {
552    /// Take a repository from nothing to ready-to-build, or name the gaps
553    Onboard,
554    /// A PRD to an Approved TDD and an Active, lint-clean SDD
555    Authoring,
556    /// Change TDDs and SDDs, one or many, and record what each change stales
557    Change,
558    /// Start, watch and read a `vitrify run` build
559    Run,
560    /// Drive the build loop from a session instead of `vitrify run`
561    Kickoff,
562    /// Build one unit (--unit --holder prints a worker's whole prompt)
563    #[value(name = "build-unit")]
564    BuildUnit,
565    /// Take one built unit through the gate to Verified
566    Integrate,
567}
568
569impl BriefPhase {
570    /// Every procedure, in index order.
571    pub const ALL: [BriefPhase; 7] = [
572        BriefPhase::Onboard,
573        BriefPhase::Authoring,
574        BriefPhase::Change,
575        BriefPhase::Run,
576        BriefPhase::Kickoff,
577        BriefPhase::BuildUnit,
578        BriefPhase::Integrate,
579    ];
580
581    pub fn as_str(self) -> &'static str {
582        match self {
583            BriefPhase::Onboard => "onboard",
584            BriefPhase::Authoring => "authoring",
585            BriefPhase::Change => "change",
586            BriefPhase::Run => "run",
587            BriefPhase::Kickoff => "kickoff",
588            BriefPhase::BuildUnit => "build-unit",
589            BriefPhase::Integrate => "integrate",
590        }
591    }
592}
593
594/// The documents `vitrify new` materializes.
595#[derive(Clone, Copy, ValueEnum)]
596pub enum NewKind {
597    /// A TDD: <name>.md (default tdd.md) from the TDD template
598    Tdd,
599    /// An SDD: a bundle directory <name>/ (default scope/), or
600    /// <name>.sdd.md with --single
601    Sdd,
602}
603
604#[derive(Subcommand)]
605pub enum SchemaMode {
606    /// Print the embedded normative schema (SDD by default)
607    #[command(after_long_help = help::verbs::SCHEMA_SHOW)]
608    Show {
609        /// Print the TDD schema instead
610        #[arg(long)]
611        tdd: bool,
612    },
613}