1use vela_protocol::frontier_repo;
2use vela_protocol::sign;
3use vela_protocol::state;
4
5use std::path::{Path, PathBuf};
6
7use clap::Parser;
8use serde::Serialize;
9use serde_json::{Value, json};
10use vela_protocol::cli_style as style;
11
12#[derive(Parser)]
13#[command(name = "vela", version)]
14#[command(about = "Portable frontier state for science")]
15struct Cli {
16 #[arg(long, global = true)]
18 quiet: bool,
19 #[command(subcommand)]
20 command: Commands,
21}
22
23pub(crate) use crate::claim_view::*;
24pub(crate) use crate::cli_admin::*;
25pub(crate) use crate::cli_check::*;
26use crate::cli_commands::*;
27use crate::cli_engine::{cmd_reproduce, cmd_verify_evidence};
28pub(crate) use crate::cli_read::*;
29pub(crate) use crate::cli_write::*;
30
31mod authority;
32pub(crate) mod help_text;
33mod identity;
34mod lifecycle;
35mod output;
36pub(crate) mod progress;
37pub(crate) mod records;
38pub(crate) mod review_decision;
39pub(crate) mod safe_text;
40mod surface;
41pub(crate) use authority::*;
42pub(crate) use identity::*;
43pub(crate) use lifecycle::*;
44pub(crate) use output::*;
45pub(crate) use records::*;
46pub(crate) use surface::*;
47pub async fn run_command() {
48 if std::env::var_os("NO_COLOR").is_some() {
59 colored::control::set_override(false);
60 } else {
61 match crate::config::settings::resolve("ui.color", None)
62 .0
63 .as_str()
64 {
65 "never" => colored::control::set_override(false),
66 "always" => colored::control::set_override(true),
67 _ => {}
68 }
69 }
70
71 let cli = Cli::parse();
72 crate::ui::set_quiet(cli.quiet);
73 match cli.command {
74 Commands::Repository { action } => match action {
75 RepositoryAction::Verify { frontier, json } => {
76 crate::current_repository::cmd_repository_verify(&frontier, json)
77 }
78 },
79 Commands::Check {
80 source,
81 strict,
82 json,
83 } => cmd_check(source.as_deref(), strict, json),
84 Commands::Doctor {
85 frontier,
86 port,
87 all,
88 json,
89 } => cmd_doctor(frontier.as_deref(), port, all, json).await,
90 Commands::Status { frontier, json } => {
91 cmd_status_compact(&crate::ui::resolve_frontier(frontier), json)
92 }
93 Commands::Log {
94 frontier,
95 finding_id,
96 limit,
97 kind,
98 as_of,
99 json,
100 } => {
101 let (frontier, finding_id) =
102 crate::ui::resolve_frontier_with_id(frontier, finding_id, &["vf_"]);
103 if let Some(vf) = finding_id {
104 let payload = state::history_as_of(&frontier, &vf, as_of.as_deref())
105 .unwrap_or_else(|e| fail_return(&e));
106 if json {
107 print_json(&payload);
108 } else {
109 print_history(&payload);
110 }
111 } else {
112 cmd_log(&frontier, limit, kind.as_deref(), json);
113 }
114 }
115 Commands::Verification { action } => cmd_verify_evidence(action),
116 Commands::Agents { action } => crate::cli_agents::cmd_agents(action),
117 Commands::Completions { shell } => {
118 use clap::CommandFactory;
119 let mut cmd = Cli::command();
120 let name = cmd.get_name().to_string();
121 let shell_kind: clap_complete::Shell = match shell.as_str() {
122 "bash" => clap_complete::Shell::Bash,
123 "zsh" => clap_complete::Shell::Zsh,
124 "fish" => clap_complete::Shell::Fish,
125 other => fail_return(&format!(
126 "unsupported shell '{other}'. Valid: bash, zsh, fish"
127 )),
128 };
129 clap_complete::generate(shell_kind, &mut cmd, name, &mut std::io::stdout());
130 }
131
132 Commands::Reproduce {
133 path,
134 proposal,
135 json,
136 } => cmd_reproduce(&path, proposal.as_deref(), json),
137 Commands::Id { action } => cmd_id(action),
138 Commands::Authority { action } => match action {
139 AuthorityAction::Init {
140 frontier,
141 key,
142 reason,
143 json,
144 } => cmd_authority_init(&frontier, key.as_deref(), &reason, json),
145 AuthorityAction::Trust { action } => match action {
146 AuthorityTrustAction::Pin {
147 frontier,
148 record_root,
149 json,
150 } => cmd_authority_trust_pin(&frontier, &record_root, json),
151 },
152 },
153 Commands::Init {
154 path,
155 name,
156 scope,
157 json,
158 } => cmd_init(&path, name.as_deref(), scope.as_deref(), json),
159 Commands::Review { action } => cmd_review(action),
160 Commands::TargetIndex { action } => crate::target_index::cmd_target_index(action),
161 Commands::Claim {
162 command:
163 ClaimCommands::Show {
164 frontier,
165 claim_id,
166 view,
167 json,
168 },
169 } => cmd_claim_show(&frontier, &claim_id, &view, json),
170 Commands::Show {
171 frontier,
172 object_id,
173 json,
174 } => crate::cli_object::cmd_show(&frontier, &object_id, json),
175 Commands::Why {
176 frontier,
177 claim_id,
178 json,
179 } => crate::cli_object::cmd_why(&frontier, &claim_id, json),
180 Commands::Next {
181 frontier,
182 limit,
183 json,
184 } => {
185 crate::ui::set_mode("next", json);
186 let dir = crate::ui::resolve_frontier(frontier);
187 crate::current_repository::cmd_current_next(&dir, limit, json);
188 }
189 Commands::Start {
190 target,
191 frontier,
192 ttl,
193 drop: drop_it,
194 reason,
195 r#as,
196 json,
197 } => {
198 crate::ui::set_mode("start", json);
199 let dir = crate::ui::resolve_frontier(frontier);
200 let actor = crate::cli_identity::resolve_actor(r#as.as_deref());
201 crate::current_work::cmd_start(
202 &dir,
203 target.as_deref(),
204 ttl,
205 drop_it,
206 reason.as_deref(),
207 &actor,
208 json,
209 );
210 }
211 Commands::Submit {
212 submission,
213 frontier,
214 claim,
215 claim_type,
216 condition,
217 replayability,
218 artifact,
219 caveat,
220 producer_check,
221 verification_requirement,
222 packet_root,
223 profile_root,
224 verifier_capsule_root,
225 result_contract_root,
226 attempt,
227 r#as,
228 push,
229 json,
230 } => {
231 crate::ui::set_mode("submit", json);
232 let dir = crate::ui::resolve_frontier(frontier);
233 let actor = crate::cli_identity::resolve_actor(r#as.as_deref());
234 let preflight_identity =
235 vela_protocol::canonical::to_canonical_bytes(&serde_json::json!({
236 "schema": "vela.submit-preflight.internal.v1",
237 "frontier": dir.display().to_string(),
238 "actor": actor,
239 "submission": submission.as_ref().map(|path| path.display().to_string()),
240 "claim": claim,
241 "claim_type": claim_type,
242 "conditions": condition,
243 "replayability": replayability,
244 "artifacts": artifact,
245 "caveats": caveat,
246 "producer_checks": producer_check,
247 "verification_requirements": verification_requirement,
248 "packet_root": packet_root,
249 "profile_root": profile_root,
250 "verifier_capsule_root": verifier_capsule_root,
251 "result_contract_root": result_contract_root,
252 "attempt": attempt,
253 "push": push,
254 }))
255 .unwrap_or_default();
256 let preflight_id =
257 crate::operation_journal::operation_id("submit-preflight", &preflight_identity);
258 let fail_preflight = |kind, message: String| -> ! {
259 crate::ui::fail_unchanged(kind, &message, &preflight_id, "vela submit --help")
260 };
261 let (submission, bundle_root) = if let Some(path) = submission {
262 let raw =
263 crate::bounded_file::read_bounded_file(&path, 8 * 1024 * 1024, "Submission v1")
264 .unwrap_or_else(|error| {
265 let kind = if error.code == "missing" {
266 crate::ui::ErrorKind::NotFound
267 } else {
268 crate::ui::ErrorKind::Domain
269 };
270 fail_preflight(kind, error.to_string())
271 });
272 let parsed = vela_protocol::submission_v1::SubmissionV1::parse(&raw)
273 .unwrap_or_else(|error| {
274 fail_preflight(crate::ui::ErrorKind::Domain, error.to_string())
275 });
276 let root = path.parent().map(std::path::Path::to_path_buf);
277 (parsed, root)
278 } else {
279 let Some(claim) = claim else {
280 crate::ui::fail_unchanged(
281 crate::ui::ErrorKind::Usage,
282 "submit needs a Submission file or --claim",
283 &preflight_id,
284 "vela submit --help",
285 );
286 };
287 let claim_type = claim_type.unwrap_or_else(|| {
288 fail_preflight(
289 crate::ui::ErrorKind::Usage,
290 "flag authoring needs --type (computational, theoretical, empirical, negative, or contradiction)".to_string(),
291 )
292 });
293 let replayability = replayability.unwrap_or_else(|| {
294 fail_preflight(
295 crate::ui::ErrorKind::Usage,
296 "flag authoring needs --replayability (exact, bounded, approximate, unavailable, or unknown)".to_string(),
297 )
298 });
299 let execution_binding = match (
300 packet_root,
301 profile_root,
302 verifier_capsule_root,
303 result_contract_root,
304 ) {
305 (
306 Some(packet_root),
307 Some(profile_root),
308 Some(verifier_capsule_root),
309 Some(result_contract_root),
310 ) => {
311 let binding = vela_protocol::receipt_v1::ExecutionBindingV1 {
312 schema: vela_protocol::receipt_v1::EXECUTION_BINDING_SCHEMA.to_string(),
313 packet_root,
314 profile_root,
315 verifier_capsule_root,
316 result_contract_root,
317 };
318 binding.validate().unwrap_or_else(|error| {
319 fail_preflight(
320 crate::ui::ErrorKind::Usage,
321 format!("invalid exact execution binding: {error}"),
322 )
323 });
324 Some(binding)
325 }
326 (None, None, None, None) => None,
327 _ => fail_preflight(
328 crate::ui::ErrorKind::Usage,
329 "exact execution binding requires all four full roots".to_string(),
330 ),
331 };
332 let authored = crate::workflow::author_submission(
333 &dir,
334 &actor,
335 attempt.as_deref(),
336 claim,
337 claim_type,
338 condition,
339 replayability,
340 &artifact,
341 caveat,
342 producer_check,
343 verification_requirement,
344 execution_binding,
345 )
346 .unwrap_or_else(|error| {
347 fail_preflight(
348 crate::ui::ErrorKind::Domain,
349 format!("Submission build: {error}"),
350 )
351 });
352 (authored, None)
353 };
354 match crate::workflow::submit(
355 &dir,
356 &submission,
357 &actor,
358 attempt.as_deref(),
359 bundle_root.as_deref(),
360 push,
361 ) {
362 Ok(outcome) => {
363 if json {
364 let mut payload = serde_json::to_value(&outcome)
365 .expect("SubmitOutcome contains only serializable values");
366 let fields = payload
367 .as_object_mut()
368 .expect("SubmitOutcome serializes as an object");
369 fields.insert("ok".to_string(), serde_json::json!(true));
370 fields.insert("command".to_string(), serde_json::json!("submit"));
371 fields.insert(
372 "request_id".to_string(),
373 serde_json::json!(outcome.operation_id),
374 );
375 print_json(&payload);
376 } else {
377 crate::ui::header(
378 "SUBMISSION",
379 &outcome.submission_id,
380 Some(outcome.route),
381 );
382 println!(
383 " {:<18} {}",
384 vela_protocol::cli_style::dim("proposal"),
385 safe_text::inline(&outcome.proposal_id)
386 );
387 println!(
388 " {:<18} {}",
389 vela_protocol::cli_style::dim("registration"),
390 safe_text::inline(&outcome.registration_record_id)
391 );
392 println!(
393 " {:<18} {}",
394 vela_protocol::cli_style::dim("accepted delta"),
395 outcome.accepted_event_delta
396 );
397 }
398 }
399 Err(e) => fail(&e),
400 }
401 }
402 Commands::Config { action } => match action {
403 ConfigAction::Get {
404 key,
405 frontier,
406 json,
407 } => {
408 crate::ui::set_mode("config", json);
409 crate::config::settings::cmd_config_get(&key, frontier.as_deref(), json)
410 }
411 ConfigAction::Set {
412 key,
413 value,
414 frontier,
415 json,
416 } => {
417 crate::ui::set_mode("config", json);
418 crate::config::settings::cmd_config_set(&key, &value, frontier.as_deref(), json)
419 }
420 ConfigAction::Unset {
421 key,
422 frontier,
423 json,
424 } => {
425 crate::ui::set_mode("config", json);
426 crate::config::settings::cmd_config_unset(&key, frontier.as_deref(), json)
427 }
428 ConfigAction::List { frontier, json } => {
429 crate::ui::set_mode("config", json);
430 crate::config::settings::cmd_config_list(frontier.as_deref(), json)
431 }
432 },
433 }
434}
435
436pub fn run_from_args() {
437 style::init();
438 let args = std::env::args().collect::<Vec<_>>();
439 match args.get(1).map(String::as_str) {
440 None => {
441 print_product_help();
442 return;
443 }
444 Some("-h" | "--help" | "help") => {
445 if args.get(2).map(String::as_str) == Some("advanced") {
448 print_strict_help();
449 } else {
450 print_product_help();
451 }
452 return;
453 }
454 Some("-V" | "--version" | "version") => {
455 println!("vela {}", env!("CARGO_PKG_VERSION"));
456 return;
457 }
458 Some(_) => {}
459 }
460 let runtime = tokio::runtime::Runtime::new().expect("failed to create tokio runtime");
461 runtime.block_on(run_command());
462}