OXYGENHUB
# 2d2dcb7a2778 spectra / src / cli / mod.rs
25.3 KiB · Rust · last changed Remove Radar: Spectra serves only signed requests (spec Draft 0.7) 1 day ago source blame history raw
1
//! The `spectra` command line (spec §19). Output is modern: colour and
2
//! glyphs on a UTF-8 terminal, plain when piped, with `--plain` or with
3
//! `NO_COLOR`; `--json` prints one document on stdout and nothing else.
4
5
pub mod admin;
6
pub mod agent;
7
pub mod auto;
8
pub mod brief;
9
pub mod connect;
10
pub mod format;
11
pub mod human;
12
pub mod identity;
13
pub mod marks;
14
pub mod mcp;
15
pub mod project;
16
pub mod serve;
17
pub mod setup;
18
pub mod ui;
19
20
use anyhow::{bail, Context, Result};
21
use clap::{Parser, Subcommand};
22
use std::path::PathBuf;
23
24
#[derive(Parser)]
25
#[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")]
26
pub struct Cli {
27
    /// Plain output: no colour or glyphs (also NO_COLOR, or when not a terminal)
28
    #[arg(long, global = true)]
29
    pub plain: bool,
30
    /// The data directory (default: $XDG_DATA_HOME/spectra, else ~/.local/share/spectra)
31
    #[arg(long, global = true, env = "SPECTRA_DATA")]
32
    pub data: Option<PathBuf>,
33
    #[command(subcommand)]
34
    pub command: Command,
35
}
36
37
#[derive(Subcommand)]
38
pub enum Command {
39
    /// The version and build of this binary
40
    Version {
41
        /// Machine-readable output
42
        #[arg(long)]
43
        json: bool,
44
    },
45
    /// Make this machine's Spectra: record the owner's identity (read from
46
    /// their OXY_HOME and proven by signing) and make the instance key
47
    Init {
48
        /// The owner's identity home (default: $SPECTRA_HOME, else ~/.config/spectra)
49
        #[arg(long)]
50
        owner_home: Option<PathBuf>,
51
        /// This instance's name (default: the host name)
52
        #[arg(long)]
53
        name: Option<String>,
54
        /// Set up a server without the owner's secret key: name the owner's
55
        /// KeyId, and they claim it from their machine with `spectra
56
        /// connect` (spec §8.6)
57
        #[arg(long, value_name = "KEYID", conflicts_with = "owner_home")]
58
        owner: Option<String>,
59
    },
60
    /// Use a Spectra on another machine: trust it through your delegation to
61
    /// its instance key, pin its certificate, and claim it if it is new
62
    Connect {
63
        /// The server, https://HOST[:PORT]
64
        url: String,
65
        /// Trust the server if its instance key starts with this, without asking
66
        #[arg(long, value_name = "KEYID-PREFIX")]
67
        expect: Option<String>,
68
        /// Trust it without asking (for scripts)
69
        #[arg(long, short = 'y')]
70
        yes: bool,
71
        /// The owner's identity home (default: $SPECTRA_HOME, else ~/.config/spectra)
72
        #[arg(long)]
73
        owner_home: Option<PathBuf>,
74
    },
75
    /// What is set up here: your identity, and the Spectra you use
76
    Status {
77
        #[arg(long)]
78
        json: bool,
79
    },
80
    /// What a server is (its name and instance key), without trusting it
81
    Probe {
82
        /// The server, https://HOST[:PORT]
83
        url: String,
84
        #[arg(long)]
85
        json: bool,
86
    },
87
    /// Sign as you for a program that cannot (the VS Code extension): a
88
    /// loopback proxy that ends when its input closes
89
    Bridge {
90
        #[arg(long, default_value_t = 0)]
91
        port: u16,
92
        #[arg(long)]
93
        owner_home: Option<PathBuf>,
94
    },
95
    /// What the agents are told: the overall brief and each project's
96
    Brief {
97
        #[command(subcommand)]
98
        command: BriefCmd,
99
    },
100
    /// The owner's and the instance's identities: show, refresh
101
    Identity {
102
        #[command(subcommand)]
103
        command: IdentityCmd,
104
    },
105
    /// Run the daemon: the ledger, Tuner on the Unix socket and a loopback port
106
    Serve {
107
        /// The address and port for browsers and HTTP clients (default:
108
        /// `[server] bind`, else 127.0.0.1:7350); beyond loopback, other
109
        /// machines connect over TLS (spec §8.6)
110
        #[arg(long)]
111
        bind: Option<String>,
112
        /// TLS on the TCP port: auto (beyond loopback only), on, or off
113
        /// (default: `[server] tls`, else auto)
114
        #[arg(long)]
115
        tls: Option<String>,
116
        /// Allow a non-loopback address in plain HTTP
117
        #[arg(long)]
118
        insecure: bool,
119
    },
120
    /// The stdio MCP server for one agent: signs with its key and forwards to the daemon
121
    Mcp {
122
        /// The agent it speaks for
123
        #[arg(long, required_unless_present = "auto")]
124
        agent: Option<String>,
125
        /// Speak for the agent of this session's folder, so one MCP entry
126
        /// serves every folder (spec §15.1)
127
        #[arg(long, conflicts_with = "agent")]
128
        auto: bool,
129
        /// With --auto: the client's kind (claude-code, antigravity, kimi-code, codex, other)
130
        #[arg(long, default_value = "claude-code")]
131
        kind: String,
132
        /// The agent's home (default: the one recorded when it was added)
133
        #[arg(long)]
134
        home: Option<PathBuf>,
135
    },
136
    /// Post a message as yourself
137
    Say {
138
        /// What to say (Markdown; @name mentions)
139
        body: String,
140
        /// The channel (default: #general)
141
        #[arg(short, long)]
142
        channel: Option<String>,
143
        /// Reply in the thread of this message
144
        #[arg(long)]
145
        reply_to: Option<String>,
146
        #[arg(long)]
147
        json: bool,
148
        #[arg(long)]
149
        owner_home: Option<PathBuf>,
150
    },
151
    /// Your unread mentions and direct messages; --follow waits for more
152
    Inbox {
153
        #[arg(long)]
154
        follow: bool,
155
        #[arg(long)]
156
        json: bool,
157
        #[arg(long)]
158
        owner_home: Option<PathBuf>,
159
    },
160
    /// Search what was said: words, "phrases", prefix*, AND/OR/NOT
161
    Search {
162
        query: String,
163
        #[arg(long)]
164
        channel: Option<String>,
165
        #[arg(long)]
166
        author: Option<String>,
167
        /// 7d, 3h, 30m
168
        #[arg(long)]
169
        since: Option<String>,
170
        #[arg(long, default_value_t = 20)]
171
        limit: u32,
172
        #[arg(long)]
173
        json: bool,
174
    },
175
    /// Any API call, signed as the owner: JSON out
176
    Api {
177
        method: String,
178
        path: String,
179
        /// The request body (JSON)
180
        #[arg(short = 'd', long = "body")]
181
        body: Option<String>,
182
        #[arg(long)]
183
        owner_home: Option<PathBuf>,
184
    },
185
    /// The checkouts agents work in: add, list
186
    Project {
187
        #[command(subcommand)]
188
        command: ProjectCmd,
189
    },
190
    /// Agents, each its owner's delegate: add, list, disable
191
    Agent {
192
        #[command(subcommand)]
193
        command: AgentCmd,
194
    },
195
    /// The ledger's own checks: verify, check, rebuild
196
    Admin {
197
        #[command(subcommand)]
198
        command: Admin,
199
    },
200
}
201
202
#[derive(Subcommand)]
203
pub enum IdentityCmd {
204
    /// Make your own identity: a key pair that signs as you (spec §8.0)
205
    Create {
206
        /// Your name, as agents and Radar show it
207
        #[arg(long)]
208
        name: String,
209
        /// An email or URL, if you want one shown
210
        #[arg(long, default_value = "")]
211
        contact: String,
212
        /// Where to keep it (default: $SPECTRA_HOME, else ~/.config/spectra)
213
        #[arg(long)]
214
        home: Option<PathBuf>,
215
        #[arg(long)]
216
        json: bool,
217
    },
218
    /// The owner and the instance as the ledger knows them
219
    Show {
220
        #[arg(long)]
221
        json: bool,
222
    },
223
    /// Record the owner's current identity if the ledger has missed a successor
224
    Refresh {
225
        #[arg(long)]
226
        owner_home: Option<PathBuf>,
227
    },
228
}
229
230
#[derive(Subcommand)]
231
pub enum BriefCmd {
232
    /// Show the overall brief, or a project's
233
    Show {
234
        #[arg(long)]
235
        project: Option<String>,
236
        #[arg(long)]
237
        json: bool,
238
    },
239
    /// Set the overall brief, or a project's (an empty text clears it)
240
    Set {
241
        #[arg(long)]
242
        project: Option<String>,
243
        /// The text
244
        text: Option<String>,
245
        /// Read the text from a file (- for standard input)
246
        #[arg(long, short = 'f')]
247
        file: Option<PathBuf>,
248
        #[arg(long)]
249
        owner_home: Option<PathBuf>,
250
    },
251
    /// Edit it in $VISUAL or $EDITOR
252
    Edit {
253
        #[arg(long)]
254
        project: Option<String>,
255
        #[arg(long)]
256
        owner_home: Option<PathBuf>,
257
    },
258
}
259
260
#[derive(Subcommand)]
261
pub enum ProjectCmd {
262
    /// Add a project: any kind of work, with a folder if it has one
263
    Add {
264
        /// A folder (its name becomes the project's), or just a name
265
        target: String,
266
        /// Its name, when TARGET is a folder
267
        #[arg(long)]
268
        name: Option<String>,
269
        /// The work's folder, when TARGET is a name; Spectra never reads it
270
        #[arg(long)]
271
        path: Option<PathBuf>,
272
        /// What the work is, for agents and Radar
273
        #[arg(long, short = 'd', default_value = "")]
274
        description: String,
275
        /// A link to it, if any
276
        #[arg(long)]
277
        url: Option<String>,
278
        #[arg(long)]
279
        owner_home: Option<PathBuf>,
280
    },
281
    /// Say that one project depends on another (`delegate to: downstream` follows links)
282
    Link {
283
        project: String,
284
        #[arg(long)]
285
        depends_on: String,
286
        /// Remove the link instead
287
        #[arg(long)]
288
        remove: bool,
289
        #[arg(long)]
290
        owner_home: Option<PathBuf>,
291
    },
292
    /// Every project with its links and agents
293
    List {
294
        #[arg(long)]
295
        json: bool,
296
    },
297
}
298
299
#[derive(Subcommand)]
300
pub enum AgentCmd {
301
    /// Make an agent's key in its own home, delegate to it, and record it
302
    Add {
303
        /// Lower-case letters, digits and dashes
304
        name: String,
305
        /// claude-code, antigravity, kimi-code, codex or other
306
        #[arg(long)]
307
        kind: String,
308
        /// The project it works in
309
        #[arg(long)]
310
        project: String,
311
        /// The agent's own identity home, where its key is made and kept
312
        #[arg(long)]
313
        home: PathBuf,
314
        /// Bind the key to this Unix user: a request from another is refused
315
        #[arg(long)]
316
        uid: Option<u32>,
317
        /// Bind the key to the Unix user of its first signed connection
318
        #[arg(long, conflicts_with = "uid")]
319
        learn_uid: bool,
320
        /// How long the agent may act for you: 30m, 8h, 7d (default: until disabled)
321
        #[arg(long)]
322
        expires: Option<String>,
323
        /// Delegate without asking (for scripts)
324
        #[arg(long, short = 'y')]
325
        yes: bool,
326
        /// The owner's identity home
327
        #[arg(long)]
328
        owner_home: Option<PathBuf>,
329
    },
330
    /// Let an agent post in another project's channel (or take it back)
331
    Allow {
332
        name: String,
333
        /// The channel, without the #
334
        #[arg(long)]
335
        channel: String,
336
        /// Take the permission back
337
        #[arg(long)]
338
        remove: bool,
339
        #[arg(long)]
340
        owner_home: Option<PathBuf>,
341
    },
342
    /// Give an agent more time: delegate to its key again with a new expiry
343
    Renew {
344
        name: String,
345
        /// 30m, 8h, 7d, or `never`
346
        #[arg(long)]
347
        expires: String,
348
        #[arg(long)]
349
        owner_home: Option<PathBuf>,
350
    },
351
    /// Every agent with its owner, key, delegation and state
352
    List {
353
        #[arg(long)]
354
        json: bool,
355
    },
356
    /// Refuse an agent's key from now on
357
    Disable {
358
        name: String,
359
        #[arg(long)]
360
        owner_home: Option<PathBuf>,
361
    },
362
}
363
364
#[derive(Subcommand)]
365
pub enum Admin {
366
    /// Recompute the hash chain and check every checkpoint and proof (exit 1 on any finding)
367
    Verify {
368
        /// Machine-readable output
369
        #[arg(long)]
370
        json: bool,
371
    },
372
    /// Whether the projections equal a rebuild from the ledger
373
    Check,
374
    /// Replay the ledger into every projection
375
    Rebuild,
376
}
377
378
/// What every command needs: where the data lives.
379
pub struct Ctx {
380
    pub data: PathBuf,
381
}
382
383
impl Ctx {
384
    /// The ledger's database, which must exist.
385
    pub fn db(&self) -> Result<PathBuf> {
386
        let db = self.data.join("spectra.db");
387
        if !db.exists() {
388
            bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display());
389
        }
390
        Ok(db)
391
    }
392
393
    /// The ledger, for writing: the instance key signs its checkpoints, and
394
    /// the lock keeps any other writer out.
395
    pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> {
396
        let db = self.db()?;
397
        let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?;
398
        spectra_core::ledger::Ledger::open(&db, Some(instance)).await
399
    }
400
401
    /// The daemon's Unix socket.
402
    pub fn socket(&self) -> PathBuf {
403
        self.data.join("spectra.sock")
404
    }
405
406
    /// The Spectra on another machine this one uses, if any (spec §8.6).
407
    pub fn remote(&self) -> Result<Option<connect::ClientCfg>> {
408
        connect::load()
409
    }
410
411
    /// Where the daemon is: the configured server, else the local socket.
412
    pub fn endpoint(&self) -> Result<spectra_mcp::client::Endpoint> {
413
        Ok(match self.remote()? {
414
            Some(c) => c.endpoint()?,
415
            None => spectra_mcp::client::Endpoint::Unix(self.socket()),
416
        })
417
    }
418
419
    /// A client signing as the owner: to the configured server (spec §8.6),
420
    /// or to the local daemon when it is serving; none when neither. It
421
    /// signs with the owner identity the ledger records, since the daemon
422
    /// may not know a newer one yet.
423
    pub async fn owner_client(&self, owner_home: &std::path::Path) -> Result<Option<spectra_mcp::client::Client>> {
424
        use spectra_mcp::client::Client;
425
        if let Some(cfg) = self.remote()? {
426
            let ep = cfg.endpoint()?;
427
            let pair = remote_owner(&ep, owner_home).await?;
428
            return Ok(Some(Client::at(ep, Some(pair))));
429
        }
430
        if !self.data.join("spectra.db").exists() || !Client::alive(&self.socket()).await {
431
            return Ok(None);
432
        }
433
        let ledger = spectra_core::ledger::Ledger::open_read_only(&self.db()?).await?;
434
        Ok(Some(Client::new(&self.socket(), Some(identity::recorded_owner(&ledger, owner_home).await?))))
435
    }
436
437
    /// Somewhere to append the owner's signed events: the configured server,
438
    /// the local daemon when it is serving, else straight to the ledger
439
    /// (spec §8.5, §8.6).
440
    pub async fn owner_writer(&self, owner_home: &std::path::Path) -> Result<Writer> {
441
        if let Some(cfg) = self.remote()? {
442
            let ep = cfg.endpoint()?;
443
            let pair = remote_owner(&ep, owner_home).await?;
444
            let recorded = pair.identity.clone();
445
            return Ok(Writer::Remote { client: Box::new(spectra_mcp::client::Client::at(ep, Some(pair))), recorded });
446
        }
447
        let db = self.db()?;
448
        if spectra_mcp::client::Client::alive(&self.socket()).await {
449
            let ledger = spectra_core::ledger::Ledger::open_read_only(&db).await?;
450
            let transport = identity::recorded_owner(&ledger, owner_home).await?;
451
            return Ok(Writer::Daemon { ledger, client: Box::new(spectra_mcp::client::Client::new(&self.socket(), Some(transport))) });
452
        }
453
        Ok(Writer::Direct(self.writer().await?))
454
    }
455
456
    /// Refuse a command that needs the ledger file on this machine.
457
    pub fn local_only(&self, what: &str) -> Result<()> {
458
        if self.remote()?.is_some() {
459
            bail!("{what} runs on the server, which holds the ledger (this machine uses {})", connect::path().display());
460
        }
461
        Ok(())
462
    }
463
464
    /// `[owner] home` from spectra.toml.
465
    pub fn config_owner_home(&self) -> Option<PathBuf> {
466
        let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?;
467
        let t: toml::Table = text.parse().ok()?;
468
        t.get("owner")?.get("home")?.as_str().map(PathBuf::from)
469
    }
470
}
471
472
/// Where the owner's events go.
473
pub enum Writer {
474
    Direct(spectra_core::ledger::Ledger),
475
    Daemon {
476
        ledger: spectra_core::ledger::Ledger,
477
        client: Box<spectra_mcp::client::Client>,
478
    },
479
    /// A Spectra on another machine; `recorded` is the owner identity it knows.
480
    Remote {
481
        client: Box<spectra_mcp::client::Client>,
482
        recorded: spectra_core::identity::Identity,
483
    },
484
}
485
486
impl Writer {
487
    /// The ledger on this machine, for reading, if there is one.
488
    pub fn ledger(&self) -> Option<&spectra_core::ledger::Ledger> {
489
        match self {
490
            Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => Some(l),
491
            Writer::Remote { .. } => None,
492
        }
493
    }
494
495
    /// A client of the daemon, when one is serving.
496
    pub fn client(&self) -> Option<&spectra_mcp::client::Client> {
497
        match self {
498
            Writer::Daemon { client, .. } | Writer::Remote { client, .. } => Some(client),
499
            Writer::Direct(_) => None,
500
        }
501
    }
502
503
    /// The rotation that brings the recorded owner to the current identity
504
    /// in `home`, if it has moved.
505
    pub async fn refresh_event(&self, home: &std::path::Path) -> Result<Option<spectra_core::event::NewEvent>> {
506
        match self {
507
            Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => identity::refresh_event(l, home).await,
508
            Writer::Remote { recorded, .. } => identity::rotation(recorded, home),
509
        }
510
    }
511
512
    /// Append events signed over their payload, then checkpoint.
513
    pub async fn append(&self, events: Vec<spectra_core::event::NewEvent>) -> Result<()> {
514
        match self {
515
            Writer::Direct(l) => {
516
                l.append(events).await?;
517
                l.checkpoint().await?;
518
            }
519
            Writer::Daemon { client, .. } | Writer::Remote { client, .. } => {
520
                let wire: Vec<_> = events.iter().map(spectra_core::daemon::WireEvent::from_new).collect();
521
                let r = client.send("POST", "/api/v1/ledger/events", &[], serde_json::to_vec(&wire)?).await?;
522
                if r.status != 200 {
523
                    let why = r
524
                        .json()
525
                        .ok()
526
                        .and_then(|v| v.get("title").and_then(|t| t.as_str()).map(String::from))
527
                        .unwrap_or_else(|| String::from_utf8_lossy(&r.body).into());
528
                    bail!("the daemon refused: {why}");
529
                }
530
            }
531
        }
532
        Ok(())
533
    }
534
}
535
536
/// The owner identity a server records, as a key pair from the owner's
537
/// home: what requests to it are signed with.
538
async fn remote_owner(ep: &spectra_mcp::client::Endpoint, home: &std::path::Path) -> Result<spectra_core::keys::KeyPair> {
539
    let r = spectra_mcp::client::Client::at(ep.clone(), None).send("GET", "/api/v1/instance", &[], vec![]).await?;
540
    let info = r.json()?;
541
    let key = info.get("owner").and_then(|o| o.as_str()).context("the server has no owner yet: spectra connect claims it")?;
542
    let keys = home.join("keys");
543
    spectra_core::keys::KeyPair::load(&keys.join(format!("{key}.secret")), &keys.join(format!("{key}.identity")))
544
        .with_context(|| format!("the server's owner {} is not an identity in {}", &key[..12.min(key.len())], home.display()))
545
}
546
547
/// An API call; a refusal becomes an error that says why.
548
pub async fn call(c: &spectra_mcp::client::Client, method: &str, path: &str, body: Option<serde_json::Value>) -> Result<serde_json::Value> {
549
    let r = c.send(method, path, &[], body.map(|b| serde_json::to_vec(&b)).transpose()?.unwrap_or_default()).await?;
550
    let v: serde_json::Value = r.json().unwrap_or(serde_json::Value::Null);
551
    if r.status >= 400 {
552
        let why = v
553
            .get("detail")
554
            .or(v.get("title"))
555
            .and_then(serde_json::Value::as_str)
556
            .map(String::from)
557
            .unwrap_or_else(|| String::from_utf8_lossy(&r.body).into());
558
        bail!("{why}");
559
    }
560
    Ok(v)
561
}
562
563
/// A query string value, percent-encoded.
564
pub fn encode(s: &str) -> String {
565
    s.bytes()
566
        .map(|b| match b {
567
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => (b as char).to_string(),
568
            _ => format!("%{b:02X}"),
569
        })
570
        .collect()
571
}
572
573
fn init_logging() {
574
    let filter = tracing_subscriber::EnvFilter::try_from_env("SPECTRA_LOG").unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn"));
575
    tracing_subscriber::fmt().with_env_filter(filter).with_writer(std::io::stderr).init();
576
}
577
578
fn default_data() -> PathBuf {
579
    if let Ok(x) = std::env::var("XDG_DATA_HOME") {
580
        return PathBuf::from(x).join("spectra");
581
    }
582
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".into());
583
    PathBuf::from(home).join(".local").join("share").join("spectra")
584
}
585
586
fn runtime() -> Result<tokio::runtime::Runtime> {
587
    Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?)
588
}
589
590
pub fn run() -> Result<i32> {
591
    let cli = Cli::parse();
592
    ui::init(cli.plain);
593
    let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) };
594
    match cli.command {
595
        Command::Version { json } => version(json),
596
        Command::Status { json } => runtime()?.block_on(setup::status(&ctx, json)),
597
        Command::Probe { url, json } => runtime()?.block_on(setup::probe(&url, json)),
598
        Command::Bridge { port, owner_home } => runtime()?.block_on(setup::bridge(&ctx, port, owner_home)),
599
        Command::Brief { command } => runtime()?.block_on(async {
600
            match command {
601
                BriefCmd::Show { project, json } => brief::show(&ctx, project, json).await,
602
                BriefCmd::Set { project, text, file, owner_home } => {
603
                    brief::set(&ctx, project, brief::text_of(text, file)?, owner_home).await
604
                }
605
                BriefCmd::Edit { project, owner_home } => brief::edit(&ctx, project, owner_home).await,
606
            }
607
        }),
608
        Command::Connect { url, owner_home, expect, yes } => runtime()?.block_on(connect::connect(&ctx, &url, owner_home, expect, yes)),
609
        Command::Init { owner_home, name, owner: Some(key) } => {
610
            let _ = owner_home;
611
            runtime()?.block_on(identity::init_for(&ctx, &key, name))
612
        }
613
        Command::Init { owner_home, name, owner: None } => runtime()?.block_on(identity::init(&ctx, owner_home, name)),
614
        Command::Identity { command } => runtime()?.block_on(async {
615
            match command {
616
                IdentityCmd::Create { name, contact, home, json } => identity::create(&name, &contact, home, json),
617
                IdentityCmd::Show { json } => identity::show(&ctx, json).await,
618
                IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await,
619
            }
620
        }),
621
        Command::Serve { bind, tls, insecure } => {
622
            init_logging();
623
            runtime()?.block_on(serve::serve(&ctx, bind, tls, insecure))
624
        }
625
        Command::Mcp { agent, auto, kind, home } => runtime()?.block_on(async {
626
            match (agent, auto) {
627
                (Some(a), _) => mcp::run(&ctx, &a, home).await,
628
                (None, _) => match auto::resolve(&ctx, &kind).await? {
629
                    Some(l) => mcp::run(&ctx, &l.name, Some(l.home)).await,
630
                    None => auto::unconnected(&std::env::current_dir()?, &kind).await,
631
                },
632
            }
633
        }),
634
        Command::Say { body, channel, reply_to, json, owner_home } => {
635
            runtime()?.block_on(human::say(&ctx, channel, reply_to, body, owner_home, json))
636
        }
637
        Command::Inbox { follow, json, owner_home } => runtime()?.block_on(human::inbox(&ctx, follow, owner_home, json)),
638
        Command::Search { query, channel, author, since, limit, json } => {
639
            runtime()?.block_on(human::search(&ctx, &query, channel, author, since, limit, json))
640
        }
641
        Command::Api { method, path, body, owner_home } => runtime()?.block_on(human::raw(&ctx, &method, &path, body, owner_home)),
642
        Command::Project { command } => runtime()?.block_on(async {
643
            match command {
644
                ProjectCmd::Add { target, name, path, description, url, owner_home } => {
645
                    project::add(&ctx, project::Add { target, name, path, description, url }, owner_home).await
646
                }
647
                ProjectCmd::Link { project, depends_on, remove, owner_home } => {
648
                    project::link(&ctx, &project, &depends_on, remove, owner_home).await
649
                }
650
                ProjectCmd::List { json } => project::list(&ctx, json).await,
651
            }
652
        }),
653
        Command::Agent { command } => runtime()?.block_on(async {
654
            match command {
655
                AgentCmd::Add { name, kind, project, home, uid, learn_uid, expires, yes, owner_home } => {
656
                    agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, expires, yes, owner_home }).await
657
                }
658
                AgentCmd::Allow { name, channel, remove, owner_home } => agent::allow(&ctx, &name, &channel, remove, owner_home).await,
659
                AgentCmd::Renew { name, expires, owner_home } => agent::renew(&ctx, &name, &expires, owner_home).await,
660
                AgentCmd::List { json } => agent::list(&ctx, json).await,
661
                AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await,
662
            }
663
        }),
664
        Command::Admin { command } => runtime()?.block_on(async {
665
            match command {
666
                Admin::Verify { json } => admin::verify(&ctx, json).await,
667
                Admin::Check => admin::check(&ctx).await,
668
                Admin::Rebuild => admin::rebuild(&ctx).await,
669
            }
670
        }),
671
    }
672
}
673
674
fn version(as_json: bool) -> Result<i32> {
675
    if as_json {
676
        println!(
677
            "{}",
678
            serde_json::json!({
679
                "version": env!("CARGO_PKG_VERSION"),
680
                "build": env!("SPECTRA_BUILD_ID"),
681
                "mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS,
682
                "ledger_format": spectra_core::LEDGER_FORMAT,
683
            })
684
        );
685
    } else {
686
        println!("spectra {}", env!("SPECTRA_VERSION_LONG"));
687
    }
688
    Ok(0)
689
}