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