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
//! Top-level clap definitions for the `astrid` binary.
//!
//! Lives in its own module so [`crate::main`] stays under the 1000-line
//! CI threshold and the dispatch logic isn't tangled with structural
//! definitions. Subcommand variants here are wired to handler modules
//! in [`crate::commands`] by [`crate::dispatch`].
use clap::{Parser, Subcommand};
use crate::commands::{
agent::AgentCommand, audit::AuditArgs, budget::BudgetCommand, caps::CapsCommand,
capsule::config::ConfigArgs as CapsuleConfigArgs, capsule::show::ShowArgs as CapsuleShowArgs,
completions::CompletionsArgs, doctor::DoctorArgs, gc::GcArgs, group::GroupCommand,
invite::InviteCommand, keypair::KeypairCommand, logs::LogsArgs, ps::PsArgs,
quota::QuotaCommand, run::RunArgs, secret::SecretCommand, setup::SetupArgs, top::TopArgs,
trust::TrustCommand, version::VersionArgs, voucher::VoucherCommand, who::WhoArgs,
};
/// Astrid - Secure Agent Runtime
#[derive(Parser)]
#[command(name = "astrid")]
#[command(author, version, about, long_about = None)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct Cli {
/// Enable verbose output
#[arg(short, long, global = true)]
pub verbose: bool,
/// Output format: pretty (default), json, or stream-json
#[arg(long, global = true, default_value = "pretty")]
pub format: String,
/// Non-interactive prompt. Sends the prompt, prints the response, and exits.
/// Forces headless mode (no TUI). Stdin is appended to the prompt if piped.
#[arg(short, long)]
pub prompt: Option<String>,
/// Auto-approve all tool approval requests in headless mode (autonomous/yolo mode).
/// Without this flag, headless mode auto-denies approvals.
#[arg(short = 'y', long = "yes", alias = "yolo", alias = "autonomous")]
pub auto_approve: bool,
/// Resume an existing session by UUID, or create/resume a named
/// session by string. UUIDs (the form `--print-session` reports)
/// are used as-is so an operator can copy the printed id straight
/// into the next `-p` call; any other string is hashed into a
/// stable UUID v5 so the same name always maps to the same
/// session. Omit the flag for a fresh random session per call.
#[arg(long = "session", value_name = "ID_OR_NAME")]
pub session_name: Option<String>,
/// Print the session ID to stderr after the response, for use in scripts.
#[arg(long = "print-session")]
pub print_session: bool,
/// Render the TUI to stdout as text snapshots instead of an interactive terminal.
/// Each significant event (input, response, tool call, approval) produces a frame.
/// Requires --prompt. Useful for automated testing and CI.
#[arg(long = "snapshot-tui")]
pub snapshot_tui: bool,
/// Terminal width for --snapshot-tui rendering (default: 120).
#[arg(long = "tui-width", default_value = "120")]
pub tui_width: u16,
/// Terminal height for --snapshot-tui rendering (default: 40).
#[arg(long = "tui-height", default_value = "40")]
pub tui_height: u16,
/// Print the absolute path to the co-installed `astrid-emit`
/// companion binary and exit. Used by hook-bridge installers (sage)
/// to wire `settings.local.json` commands at the right path without
/// guessing the install layout. Handled before banner/config so it
/// works on a half-configured host.
#[arg(long = "emit-path")]
pub emit_path: bool,
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand)]
#[allow(
clippy::large_enum_variant,
reason = "clap subcommand enum, constructed once per process"
)]
pub(crate) enum Commands {
/// Start an interactive chat session
Chat {
/// Resume a specific session
#[arg(short, long)]
session: Option<String>,
},
/// One-shot non-interactive prompt execution.
Run(RunArgs),
/// Manage agent identities, group membership, and active context.
Agent {
#[command(subcommand)]
command: AgentCommand,
},
/// Manage capability groups (admin, agent, restricted, custom).
Group {
#[command(subcommand)]
command: GroupCommand,
},
/// View and manage capability grants and revokes.
Caps {
#[command(subcommand)]
command: CapsCommand,
},
/// View and adjust per-principal resource quotas.
Quota {
#[command(subcommand)]
command: QuotaCommand,
},
/// Mint invite tokens so new principals can self-enroll through the
/// HTTP gateway (or via `astrid invite redeem`).
Invite {
#[command(subcommand)]
command: InviteCommand,
},
/// Manage local ed25519 keypairs used for invite redemption.
Keypair {
#[command(subcommand)]
command: KeypairCommand,
},
/// Store and inspect capsule env configuration (API keys, base URLs).
Secret {
#[command(subcommand)]
command: SecretCommand,
},
/// Capability vouchers (deferred — see #656).
Voucher {
#[command(subcommand)]
command: VoucherCommand,
},
/// Cross-host trust relationships (deferred — see #656/#658).
Trust {
#[command(subcommand)]
command: TrustCommand,
},
/// Audit trail inspection (deferred — see #675).
Audit(AuditArgs),
/// Per-agent budget allocation and accounting (deferred — see #653/#656).
Budget {
#[command(subcommand)]
command: BudgetCommand,
},
/// Manage chat sessions
Session {
#[command(subcommand)]
command: SessionCommands,
},
/// Manage capsules
Capsule {
#[command(subcommand)]
command: CapsuleCommands,
},
/// Expose Astrid capsule tools over the Model Context Protocol.
Mcp {
#[command(subcommand)]
command: McpCommands,
},
/// Manage the system distro (curated capsule bundle).
Distro {
#[command(subcommand)]
command: DistroCommands,
},
/// Build and package a Capsule (legacy — prefer `astrid capsule build`).
#[command(hide = true)]
Build {
/// Optional path to the project directory (defaults to current directory)
path: Option<String>,
/// Output directory for the packaged `.capsule` archive
#[arg(short, long)]
output: Option<String>,
/// Explicitly define the project type (e.g., 'mcp' for legacy host servers)
#[arg(short, long, name = "type")]
project_type: Option<String>,
/// Import a legacy `mcp.json` to auto-convert
#[arg(long)]
from_mcp_json: Option<String>,
},
/// Initialize a workspace and install a distro
Init {
/// Distro to install (name, @org/repo, or path to Distro.toml)
#[arg(long, default_value = "astralis")]
distro: String,
},
/// View resolved configuration, edit it in `$EDITOR`, or print paths.
Config {
#[command(subcommand)]
command: ConfigCommands,
},
/// Manage the content-addressed WIT store (legacy — use `astrid gc`).
#[command(hide = true)]
Wit {
#[command(subcommand)]
command: WitCommands,
},
/// Garbage collect content-addressed stores (WIT, orphaned binaries).
Gc(GcArgs),
/// Start the Astrid daemon in persistent mode (detached, no TUI)
Start,
/// Show daemon status (PID, uptime, connected clients, loaded capsules)
Status,
/// Stop a running Astrid daemon
Stop,
/// Restart the Astrid daemon (graceful stop + start).
Restart,
/// Tail kernel or per-capsule logs.
Logs(LogsArgs),
/// Show the loaded capsules and their lifecycle state.
Ps(PsArgs),
/// Live resource monitor (one-shot snapshot until telemetry lands).
Top(TopArgs),
/// Show connected clients and their agent attribution.
Who(WhoArgs),
/// Run a system health check.
Doctor(DoctorArgs),
/// One-time host configuration (`AppArmor` profile for unprivileged
/// user namespaces on Ubuntu 23.10+, etc.).
Setup(SetupArgs),
/// Print version information.
Version(VersionArgs),
/// Generate shell completion scripts.
Completions(CompletionsArgs),
/// Update Astrid to the latest release.
Update,
/// Update Astrid to the latest release (legacy — use `astrid update`).
#[command(hide = true)]
SelfUpdate,
}
#[derive(Subcommand)]
pub(crate) enum CapsuleCommands {
/// Install a capsule from a local path or registry.
///
/// Capsules are deployed once and shared across every principal —
/// per-invocation isolation comes from the kernel's caller-context
/// scoping (KV namespace, home, secrets, log, quotas), not from
/// duplicating the WASM. There is intentionally no per-agent
/// install: an agent says "I use capsule X" and the kernel routes
/// their invocations into the already-loaded instance.
Install {
/// Capsule source (local path or package name)
source: String,
/// Install to workspace instead of user-level
#[arg(long)]
workspace: bool,
},
/// Update an installed capsule (or all capsules) from its original source
Update {
/// Capsule name to update (omit to update all)
target: Option<String>,
/// Update workspace capsules instead of user-level
#[arg(long)]
workspace: bool,
},
/// List all installed capsules with capability metadata
List {
/// Show full provides/requires details
#[arg(short, long)]
verbose: bool,
},
/// Remove an installed capsule
Remove {
/// Capsule name to remove
name: String,
/// Remove from workspace instead of user-level
#[arg(long)]
workspace: bool,
/// Force removal even if other capsules depend on it
#[arg(long)]
force: bool,
/// Also delete saved configuration (API keys, env vars)
#[arg(long)]
purge: bool,
},
/// Show the capsule imports/exports dependency tree
Tree,
/// Alias for `tree` (deprecated)
#[command(hide = true)]
Deps,
/// Build and package a Capsule.
Build {
/// Optional path to the project directory (defaults to current directory)
path: Option<String>,
/// Output directory for the packaged `.capsule` archive
#[arg(short, long)]
output: Option<String>,
/// Explicitly define the project type
#[arg(short, long, name = "type")]
project_type: Option<String>,
/// Import a legacy `mcp.json` to auto-convert
#[arg(long)]
from_mcp_json: Option<String>,
},
/// View or edit a capsule's env configuration without reinstalling.
Config(CapsuleConfigArgs),
/// Show manifest, interfaces, source for an installed capsule.
Show(CapsuleShowArgs),
}
/// Model Context Protocol surfaces — expose Astrid's capsule tools to an
/// external MCP client (e.g. `claude -p`, Codex).
#[derive(Subcommand)]
pub(crate) enum McpCommands {
/// Run a Model Context Protocol stdio server that bridges the
/// daemon's capsule tool surface to a generic MCP client.
///
/// Long-running: serves on stdin/stdout until the client closes the
/// stream (EOF) or the process is killed. Stdout carries the MCP
/// JSON-RPC protocol only — all diagnostics go to stderr.
Serve {
/// Principal to act as. Defaults to the active CLI agent (or
/// the `default` principal when no context is set). Stamped onto
/// every IPC message so the kernel scopes tool execution to this
/// identity.
#[arg(long)]
principal: Option<String>,
},
}
#[derive(Subcommand)]
pub(crate) enum WitCommands {
/// Garbage-collect unreferenced WIT blobs (legacy — use `astrid gc`).
Gc {
/// Delete unreferenced blobs. Without this flag, only reports them.
#[arg(long)]
force: bool,
},
}
#[derive(Subcommand)]
pub(crate) enum ConfigCommands {
/// Print the resolved configuration with source annotations.
Show {
/// Output format: `pretty` / `toml` (default) or `json`.
#[arg(long, default_value = "toml")]
format: String,
/// Restrict the output to a config section.
#[arg(long, value_name = "SECTION")]
section: Option<String>,
},
/// Open the runtime configuration file in `$EDITOR`.
Edit,
/// List all candidate config-file locations and which exist.
Path,
}
#[derive(Subcommand)]
pub(crate) enum SessionCommands {
/// List all sessions
List,
/// Delete a session
Delete {
/// The session ID to delete
id: String,
},
/// Show information about a session.
Show {
/// The session ID to query
id: String,
},
/// Show information about a session (deprecated alias for `show`).
#[command(hide = true)]
Info {
/// The session ID to query
id: String,
},
}
#[derive(Subcommand)]
pub(crate) enum DistroCommands {
/// Apply a distro to the active or specified agent.
Apply {
/// Distro identifier (name, `@org/repo`, or path).
name: Option<String>,
/// Target agent (defaults to active context).
#[arg(short, long)]
agent: Option<String>,
},
/// Show the currently-applied distro and its lockfile.
Show {
/// Target agent (defaults to active context).
#[arg(short, long)]
agent: Option<String>,
},
/// Update to the latest distro version.
Update {
/// Target agent (defaults to active context).
#[arg(short, long)]
agent: Option<String>,
},
}