1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Command-line parsing, projections, and exit codes.
mod cache;
#[cfg(test)]
mod defaults_tests;
mod dispatch;
#[cfg(test)]
mod identity_tests;
mod interactive;
mod maintenance;
#[cfg(test)]
mod monitor_tests;
mod remote;
mod render;
mod run;
mod workflow;
use dispatch::Context;
use render::{outcome_code, print_json};
mod request;
mod server;
mod session;
use request::RequestCommand;
use server::ServerCommand;
use session::SessionCommand;
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{
Args, Parser, Subcommand,
error::{ContextKind, ErrorKind},
};
use serde_json::json;
use crate::{
config::ProfileKind,
project::selection::{ProfileSelection, Selection, read_selection_file},
types::{RequestId, ReuseKey, RunId},
};
/// Keep default Run history output readable; --limit/--offset expose older records.
const DEFAULT_RUN_LIST_LIMIT: u32 = 50;
/// Shells conventionally report an interrupt as 130; use the same status on every platform.
const INTERRUPTED_EXIT: u8 = 130;
#[derive(Debug, Parser)]
#[command(
name = "artifactize",
// Help names the command, not the file run; on Windows that is artifactize.exe.
bin_name = "artifactize",
version,
about = "The AI-native collaboration layer for one-of-a-kind teammates and their agents."
)]
pub struct Cli {
/// Repository input path.
#[arg(long, global = true, value_name = "PATH")]
pub repo: Option<PathBuf>,
/// State database and Run output directory.
#[arg(long, global = true, value_name = "PATH")]
pub state_dir: Option<PathBuf>,
/// Use JSON for command results.
#[arg(long, global = true)]
pub json: bool,
#[command(subcommand)]
pub command: Option<Command>,
}
#[derive(Debug, Subcommand)]
pub enum Command {
/// Serve bounded verification and inspection tools over MCP stdio.
Mcp,
#[command(name = "__verify", hide = true)]
VerifyWorker { run_id: RunId },
#[command(name = "__execute", hide = true)]
VerifyExecution { run_id: RunId },
/// Run scoped tools or check declared review tools.
#[command(disable_help_subcommand = true, version)]
Tools {
#[command(subcommand)]
command: ToolsCommand,
},
/// Sign in with Codex (ChatGPT) in the system browser and save artifactize's tokens.
Login {
#[command(subcommand)]
provider: AuthProvider,
},
/// Revoke and remove artifactize's own Codex tokens.
Logout {
#[command(subcommand)]
provider: AuthProvider,
},
/// Sign in to, sign out of, or check a shared remote review store.
Remote {
#[command(subcommand)]
command: remote::RemoteCommand,
},
/// Check local readiness without provider calls or creating a Run.
Doctor,
/// Remove finished, unowned Run output; preserve database audit and repositories.
Prune {
/// Minimum age since Run completion (for example 7d, 24h, 30m).
#[arg(long, value_name = "DURATION", value_parser = crate::store::prune::parse_duration)]
older_than: Option<std::time::Duration>,
/// Report eligible paths without deleting anything.
#[arg(long)]
dry_run: bool,
},
/// List the models a backend offers to your key or account.
Models {
#[command(subcommand)]
provider: ModelProvider,
},
/// Submit selected evals; use --wait to observe the validation outcome.
#[command(group(clap::ArgGroup::new("required_selection")
.args(["artifact", "eval", "evals", "artifacts", "evals_file", "artifacts_file", "all"])
.required(true)))]
Verify {
#[command(flatten)]
selection: SelectionArgs,
#[command(flatten)]
policy: PolicyArgs,
/// Opt-in maximum concurrent evals, including fingerprint waiters; otherwise unlimited.
#[arg(
long,
value_name = "N",
value_parser = clap::value_parser!(u32).range(1..)
)]
jobs: Option<u32>,
/// Limit executor starts in this Run; cache hits and waiters are free.
#[arg(long, value_name = "N")]
max_executions: Option<u64>,
/// Observe until the Run ends; interrupting stops watching, not execution.
#[arg(long)]
wait: bool,
/// Retry-safe caller attempt key (1–128 ASCII letters/digits/._:-).
#[arg(long, value_name = "KEY")]
request_key: Option<crate::types::RequestKey>,
/// Only reuse results for these kinds (comma-separated); an eval with nothing to reuse is
/// not executed.
#[arg(long, value_name = "KINDS", value_enum, value_delimiter = ',')]
reuse_only: Vec<ProfileKind>,
/// Start reviews before unresolved dependency gates; cancel blocked work.
#[arg(long)]
speculate: bool,
/// Run past pending sign-offs and finish with SIGNOFFS_PENDING (exit 5).
#[arg(long)]
defer_human: bool,
},
/// Inspect current validation, saved Run audit, and what verify would do.
Status {
#[command(flatten)]
selection: SelectionArgs,
#[command(flatten)]
policy: PolicyArgs,
},
/// Moved to "artifactize config graph".
#[command(hide = true, disable_help_flag = true)]
Graph {
// Any former graph arguments, including --help, get the same hint.
#[arg(trailing_var_arg = true, allow_hyphen_values = true, hide = true)]
args: Vec<String>,
},
/// Read recorded Runs without discovering or executing project code.
Run {
#[command(subcommand)]
command: RunCommand,
},
/// Read saved requests or claim, inspect, and submit Human reviews.
Request {
#[command(subcommand)]
command: RequestCommand,
},
/// Read a saved Agent review conversation or continue it with a follow-up.
Session {
#[command(subcommand)]
command: SessionCommand,
},
/// Inspect or maintain reusable results by reuse key without a repository.
Cache {
#[command(subcommand)]
command: CacheCommand,
},
/// Inspect static folder declarations without executing hooks or reviews.
Config {
#[command(subcommand)]
command: ConfigCommand,
},
/// Browse Runs; starts on ALL regardless of cwd. Paths select only the initial repository.
Monitor {
/// Initial repository/worktree; all scopes remain accessible in the UI.
#[arg(value_name = "PATH", conflicts_with = "repo")]
path: Option<PathBuf>,
/// Initially select ALL repositories (the default).
#[arg(long, conflicts_with_all = ["path", "repo"])]
all: bool,
},
/// Search saved requests and claim, run tools for and submit Human reviews in a terminal
/// UI; the search starts on the waiting Human reviews of every repository.
Review {
/// Open this request instead of the list.
#[arg(value_name = "REQUEST_ID")]
request: Option<RequestId>,
/// Search every repository (the default); with --repo the search starts on that
/// repository's term instead.
#[arg(long)]
all: bool,
/// Reviewer name (defaults to USER).
#[arg(long, value_name = "NAME")]
reviewer: Option<crate::types::ReviewerId>,
},
/// Serve or administer a shared remote review store (review-store.sqlite).
Server {
#[command(subcommand)]
command: ServerCommand,
},
}
#[derive(Debug, Args)]
#[group(multiple = false)]
pub struct SelectionArgs {
/// Select one Artifact.
artifact: Option<Selector>,
/// Select one qualified Eval ID.
#[arg(long, value_name = "ID")]
eval: Option<crate::types::EvalId>,
/// Select comma-separated qualified Eval IDs.
#[arg(long, value_name = "CSV", value_delimiter = ',', num_args = 1)]
evals: Option<Vec<crate::types::EvalId>>,
/// Select comma-separated Artifact names.
#[arg(long, value_name = "CSV", value_delimiter = ',', num_args = 1)]
artifacts: Option<Vec<crate::types::ArtifactName>>,
/// Read Eval IDs from a JSON array or one ID per line.
#[arg(long, value_name = "PATH")]
evals_file: Option<PathBuf>,
/// Read Artifact names from a JSON array or one ID per line.
#[arg(long, value_name = "PATH")]
artifacts_file: Option<PathBuf>,
/// Select every Artifact.
#[arg(long)]
all: bool,
}
impl SelectionArgs {
fn resolve(self) -> Result<Selection, String> {
if let Some(selector) = self.artifact {
Ok(match selector {
Selector::Artifact(id) => Selection::Artifact { artifact_id: id },
Selector::Eval(id) => Selection::Eval { eval_id: id },
})
} else if let Some(eval_id) = self.eval {
Ok(Selection::Eval { eval_id })
} else if let Some(ids) = self.evals {
Ok(Selection::Evals { eval_ids: ids })
} else if let Some(ids) = self.artifacts {
Ok(Selection::Artifacts { artifact_ids: ids })
} else if let Some(path) = self.evals_file {
Ok(Selection::Evals {
eval_ids: selection_file::<crate::types::EvalId>(&path)?,
})
} else if let Some(path) = self.artifacts_file {
Ok(Selection::Artifacts {
artifact_ids: selection_file::<crate::types::ArtifactName>(&path)?,
})
} else {
Ok(Selection::All)
}
}
}
#[derive(Debug, Args)]
pub struct PolicyArgs {
/// Use a declared profile variant for every included Eval.
#[arg(long, value_name = "NAME")]
profile: Option<crate::config::ProfileVariantName>,
/// Include all Evals in the required dependency scope, including cycle peers.
#[arg(long)]
recursive: bool,
/// Force explicitly selected Evals only; dependency gates still apply.
#[arg(long)]
force: bool,
/// Fingerprints to compute at once (default: the available CPUs).
#[arg(long, value_name = "N", value_parser = clap::value_parser!(u32).range(1..))]
fingerprint_jobs: Option<u32>,
}
impl PolicyArgs {
fn options(self) -> crate::project::VerifyOptions {
crate::project::VerifyOptions {
profile: self.profile.map(ProfileSelection::Named),
recursive: self.recursive,
force: self.force,
fingerprint_jobs: self.fingerprint_jobs.map(|jobs| jobs as usize),
..Default::default()
}
}
}
#[derive(Debug, Subcommand)]
pub enum AuthProvider {
/// The ChatGPT/Codex account that the codex backend uses.
Codex,
}
#[derive(Debug, Subcommand)]
pub enum ModelProvider {
/// OpenAI API models (OPENAI_API_KEY).
Openai,
/// Anthropic API models (ANTHROPIC_API_KEY).
Anthropic,
/// Codex models your ChatGPT account can use (artifactize login codex).
Codex,
}
impl ModelProvider {
fn backend(self) -> crate::config::Backend {
match self {
Self::Openai => crate::config::Backend::Openai,
Self::Anthropic => crate::config::Backend::Anthropic,
Self::Codex => crate::config::Backend::Codex,
}
}
}
#[derive(Debug, Subcommand)]
pub enum ConfigCommand {
/// Validate declarations and unique Artifact/Eval ids.
Check,
/// Inspect full static definitions, relations, and cycles.
Graph {
/// Select one Artifact and its required closure; defaults to all.
artifact: Option<crate::types::ArtifactName>,
},
}
#[derive(Debug, Subcommand)]
pub enum RunCommand {
/// Request cancellation; committed results are kept and cleanup settles asynchronously.
Cancel { run_id: RunId },
/// List saved Runs, newest first; defaults to the current repository.
List {
/// Explicitly restrict results to --repo (the default).
#[arg(long, conflicts_with = "all")]
repo_only: bool,
/// List Runs from every repository in the shared state.
#[arg(long)]
all: bool,
/// Maximum number of Runs to return.
#[arg(long, value_name = "N", default_value_t = DEFAULT_RUN_LIST_LIMIT)]
limit: u32,
/// Skip this many Runs before returning results.
#[arg(long, value_name = "N", default_value = "0")]
offset: u32,
},
/// Read the full saved audit as JSON, even without --json.
Show {
run_id: RunId,
/// Follow a RUNNING Run until it finishes and exit with its outcome code.
#[arg(long)]
wait: bool,
/// Wait timeout; never cancels the Run (default 600000).
#[arg(
long,
value_name = "MS",
requires = "wait",
value_parser = timeout_duration
)]
timeout_ms: Option<std::time::Duration>,
},
}
#[derive(Debug, Subcommand)]
pub enum CacheCommand {
/// List the latest record of each reuse key, the one verify reuses.
List {
/// List every record of every key, latest first per key.
#[arg(long)]
history: bool,
},
/// Read a key's latest record (result, profile, options, provenance) as JSON.
Show {
key: ReuseKey,
/// Print every record of the key as a JSON array, latest first.
#[arg(long)]
history: bool,
},
/// Remove every record of an unused key, preserving saved Runs and executions.
Rm { key: ReuseKey },
}
#[derive(Debug, Subcommand)]
pub enum ToolsCommand {
/// Static preflight by default; --execute opts in to one tool call.
Check(crate::diagnostics::ToolCheckOptions),
#[command(flatten)]
Builtin(artifactize_tools::cli::Tool),
}
fn cancellation_listener() -> Result<
(
tokio_util::sync::CancellationToken,
tokio::task::JoinHandle<()>,
),
String,
> {
let cancellation = tokio_util::sync::CancellationToken::new();
let stop = crate::platform::stop_requested().map_err(|e| e.to_string())?;
let token = cancellation.clone();
let listener = tokio::spawn(async move {
stop.await;
token.cancel();
});
Ok((cancellation, listener))
}
fn failure(message: &str, json: bool) -> ExitCode {
if json {
let _ = writeln!(io::stdout().lock(), "{}", json!({ "error": message }));
} else {
let _ = writeln!(io::stderr().lock(), "{message}");
}
ExitCode::from(2)
}
/// Global options may precede or follow the subcommand, but only once in total.
fn repeated_global(options: &[&str]) -> Option<&'static str> {
["--repo", "--state-dir", "--json"]
.into_iter()
.find(|name| {
options
.iter()
.filter(|arg| {
arg.strip_prefix(name)
.is_some_and(|rest| rest.is_empty() || rest.starts_with('='))
})
.count()
> 1
})
}
/// What a screen runs until the reviewer quits; screens need no `Send`, the command runs on a
/// single thread.
pub type Screen = std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), String>>>>;
/// The terminal screens `artifactize monitor` and `artifactize review` open. They live in
/// `artifactize-tui`, which the binary hands to [`run`], so this library draws nothing.
#[derive(Clone, Copy)]
pub struct Screens {
/// The Monitor over the state directory, on one repository or on every one.
pub monitor: fn(PathBuf, Option<PathBuf>, tokio_util::sync::CancellationToken) -> Screen,
/// The Human review list, or one request when given.
pub review: fn(
PathBuf,
Option<PathBuf>,
crate::types::ReviewerId,
Option<crate::types::RequestId>,
tokio_util::sync::CancellationToken,
) -> Screen,
}
pub fn run(screens: Screens) -> ExitCode {
let args: Vec<_> = std::env::args_os().collect();
let has_args = args.len() > 1;
let parsed = Cli::try_parse_from(&args);
// The help tool's trailing arguments belong to the target program, even if they
// happen to share names with artifactize's global flags.
let tool_args = match &parsed {
Ok(Cli {
command:
Some(Command::Tools {
command: ToolsCommand::Builtin(artifactize_tools::cli::Tool::Help { args }),
}),
..
}) => args.len(),
_ => 0,
};
let options: Vec<_> = args
.iter()
.take(args.len() - tool_args)
.skip(1)
.take_while(|arg| *arg != "--")
.filter_map(|arg| arg.to_str())
.collect();
let mcp = matches!(
&parsed,
Ok(Cli {
command: Some(Command::Mcp),
..
})
) || parsed
.as_ref()
.err()
.and_then(|error| error.get(ContextKind::Usage))
.is_some_and(|usage| usage.to_string().starts_with("Usage: artifactize mcp"));
let json = options.contains(&"--json") && !mcp;
if let Some(name) = repeated_global(&options) {
return failure(&format!("{name} cannot be used multiple times."), json);
}
let cli = match parsed {
Ok(cli) => cli,
Err(error) if json && error.use_stderr() => {
let message = error.to_string();
let message = if error.kind() == ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand {
"A subcommand is required; see --help."
} else {
let usage = message.split("\n\n").next().unwrap_or_default();
usage.trim_start_matches("error: ")
};
return failure(message, true);
}
Err(error) => {
return if error.print().is_ok() && !error.use_stderr() {
ExitCode::SUCCESS
} else {
ExitCode::from(2)
};
}
};
// Run on a thread whose stack is the same size everywhere: Windows gives the main thread
// only 1 MiB, which deep review futures outgrow in debug builds.
let worker = std::thread::Builder::new()
.name("artifactize".into())
.stack_size(STACK_BYTES)
.spawn(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.thread_stack_size(STACK_BYTES)
.build()
.map_err(|error| error.to_string())?;
runtime.block_on(Box::pin(async {
let result = dispatch::execute(cli, screens).await;
crate::changes::drain().await;
result
}))
});
let result = match worker {
Ok(worker) => worker
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic)),
Err(error) => Err(error.to_string()),
};
match result {
Ok(code) => match crate::platform::pause_after_help(has_args) {
Ok(()) => ExitCode::from(code),
Err(error) => failure(&error.to_string(), json),
},
Err(error) => failure(&error, json),
}
}
/// The stack of the thread commands run on, and of blocking helper threads: 8 MiB, the
/// usual Linux and macOS main-thread size, so every system runs the same futures.
const STACK_BYTES: usize = 8 * 1024 * 1024;
/// Parse protocol milliseconds exactly once, preserving the public signed 32-bit bound.
fn timeout_duration(text: &str) -> Result<std::time::Duration, String> {
let millis: u64 = text
.parse()
.map_err(|_| "timeout must be milliseconds".to_owned())?;
if !(1..=crate::config::validation::MAX_TIMEOUT_MS).contains(&millis) {
return Err("timeoutMs must be between 1 and 2147483647".into());
}
Ok(std::time::Duration::from_millis(millis))
}
/// The positional selection intentionally accepts either an Artifact or qualified Eval.
#[derive(Debug, Clone)]
enum Selector {
Artifact(crate::types::ArtifactName),
Eval(crate::types::EvalId),
}
impl std::str::FromStr for Selector {
type Err = String;
fn from_str(text: &str) -> Result<Self, Self::Err> {
if text.contains('/') {
text.parse().map(Self::Eval)
} else {
text.parse().map(Self::Artifact)
}
}
}
fn selection_file<T>(path: &std::path::Path) -> Result<Vec<T>, String>
where
T: std::str::FromStr<Err = String>,
{
read_selection_file(path)?
.into_iter()
.map(|text| text.parse::<T>())
.collect()
}