| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | pub mod admin; |
| 6 | | pub mod agent; |
| 7 | | pub mod format; |
| 8 | | pub mod identity; |
| 9 | | pub mod json; |
| 10 | | pub mod marks; |
| 11 | | pub mod mcp; |
| 12 | | pub mod project; |
| 13 | | pub mod serve; |
| 14 | | pub mod ui; |
| 15 | |
|
| 16 | | use anyhow::{bail, Result}; |
| 17 | | use clap::{Parser, Subcommand}; |
| 18 | | use std::path::PathBuf; |
| 19 | |
|
| 20 | | #[derive(Parser)] |
| 21 | | #[command(name = "spectra", version = env!("SPECTRA_VERSION_LONG"), about = "A control plane for AI coding agents")] |
| 22 | | pub struct Cli { |
| 23 | | |
| 24 | | #[arg(long, global = true)] |
| 25 | | pub plain: bool, |
| 26 | | |
| 27 | | #[arg(long, global = true, env = "SPECTRA_DATA")] |
| 28 | | pub data: Option<PathBuf>, |
| 29 | | #[command(subcommand)] |
| 30 | | pub command: Command, |
| 31 | | } |
| 32 | |
|
| 33 | | #[derive(Subcommand)] |
| 34 | | pub enum Command { |
| 35 | | |
| 36 | | Version { |
| 37 | | |
| 38 | | #[arg(long)] |
| 39 | | json: bool, |
| 40 | | }, |
| 41 | | |
| 42 | | |
| 43 | | Init { |
| 44 | | |
| 45 | | #[arg(long)] |
| 46 | | owner_home: Option<PathBuf>, |
| 47 | | |
| 48 | | #[arg(long)] |
| 49 | | name: Option<String>, |
| 50 | | }, |
| 51 | | |
| 52 | | Identity { |
| 53 | | #[command(subcommand)] |
| 54 | | command: IdentityCmd, |
| 55 | | }, |
| 56 | | |
| 57 | | Serve { |
| 58 | | |
| 59 | | #[arg(long, default_value = "127.0.0.1:7350")] |
| 60 | | bind: String, |
| 61 | | |
| 62 | | #[arg(long)] |
| 63 | | insecure: bool, |
| 64 | | }, |
| 65 | | |
| 66 | | Mcp { |
| 67 | | |
| 68 | | #[arg(long)] |
| 69 | | agent: String, |
| 70 | | |
| 71 | | #[arg(long)] |
| 72 | | home: Option<PathBuf>, |
| 73 | | }, |
| 74 | | |
| 75 | | Project { |
| 76 | | #[command(subcommand)] |
| 77 | | command: ProjectCmd, |
| 78 | | }, |
| 79 | | |
| 80 | | Agent { |
| 81 | | #[command(subcommand)] |
| 82 | | command: AgentCmd, |
| 83 | | }, |
| 84 | | |
| 85 | | Admin { |
| 86 | | #[command(subcommand)] |
| 87 | | command: Admin, |
| 88 | | }, |
| 89 | | } |
| 90 | |
|
| 91 | | #[derive(Subcommand)] |
| 92 | | pub enum IdentityCmd { |
| 93 | | |
| 94 | | Show { |
| 95 | | #[arg(long)] |
| 96 | | json: bool, |
| 97 | | }, |
| 98 | | |
| 99 | | Refresh { |
| 100 | | #[arg(long)] |
| 101 | | owner_home: Option<PathBuf>, |
| 102 | | }, |
| 103 | | } |
| 104 | |
|
| 105 | | #[derive(Subcommand)] |
| 106 | | pub enum ProjectCmd { |
| 107 | | |
| 108 | | Add { |
| 109 | | path: PathBuf, |
| 110 | | |
| 111 | | #[arg(long)] |
| 112 | | name: Option<String>, |
| 113 | | |
| 114 | | #[arg(long)] |
| 115 | | remote: Option<String>, |
| 116 | | #[arg(long)] |
| 117 | | owner_home: Option<PathBuf>, |
| 118 | | }, |
| 119 | | |
| 120 | | List { |
| 121 | | #[arg(long)] |
| 122 | | json: bool, |
| 123 | | }, |
| 124 | | } |
| 125 | |
|
| 126 | | #[derive(Subcommand)] |
| 127 | | pub enum AgentCmd { |
| 128 | | |
| 129 | | Add { |
| 130 | | |
| 131 | | name: String, |
| 132 | | |
| 133 | | #[arg(long)] |
| 134 | | kind: String, |
| 135 | | |
| 136 | | #[arg(long)] |
| 137 | | project: String, |
| 138 | | |
| 139 | | #[arg(long)] |
| 140 | | home: PathBuf, |
| 141 | | |
| 142 | | #[arg(long)] |
| 143 | | uid: Option<u32>, |
| 144 | | |
| 145 | | #[arg(long, conflicts_with = "uid")] |
| 146 | | learn_uid: bool, |
| 147 | | |
| 148 | | #[arg(long, default_value_t = 600)] |
| 149 | | wait: u64, |
| 150 | | |
| 151 | | #[arg(long)] |
| 152 | | owner_home: Option<PathBuf>, |
| 153 | | }, |
| 154 | | |
| 155 | | List { |
| 156 | | #[arg(long)] |
| 157 | | json: bool, |
| 158 | | }, |
| 159 | | |
| 160 | | Disable { |
| 161 | | name: String, |
| 162 | | #[arg(long)] |
| 163 | | owner_home: Option<PathBuf>, |
| 164 | | }, |
| 165 | | } |
| 166 | |
|
| 167 | | #[derive(Subcommand)] |
| 168 | | pub enum Admin { |
| 169 | | |
| 170 | | Verify { |
| 171 | | |
| 172 | | #[arg(long)] |
| 173 | | json: bool, |
| 174 | | }, |
| 175 | | |
| 176 | | Check, |
| 177 | | |
| 178 | | Rebuild, |
| 179 | | } |
| 180 | |
|
| 181 | |
|
| 182 | | pub struct Ctx { |
| 183 | | pub data: PathBuf, |
| 184 | | } |
| 185 | |
|
| 186 | | impl Ctx { |
| 187 | | |
| 188 | 99× | pub fn db(&self) -> Result<PathBuf> { |
| 189 | 99× | let db = self.data.join("spectra.db"); |
| 190 | 99× | if !db.exists() { |
| 191 | 1× | bail!("no Spectra in {} (run `spectra init`, or pass --data)", self.data.display()); |
| 192 | 98× | } |
| 193 | 98× | Ok(db) |
| 194 | 99× | } |
| 195 | |
|
| 196 | | |
| 197 | | |
| 198 | 34× | pub async fn writer(&self) -> Result<spectra_core::ledger::Ledger> { |
| 199 | 34× | let db = self.db()?; |
| 200 | 34× | let instance = spectra_core::keys::KeyPair::instance(&self.data, "spectra")?; |
| 201 | 34× | spectra_core::ledger::Ledger::open(&db, Some(instance)).await |
| 202 | 34× | } |
| 203 | |
|
| 204 | | |
| 205 | 40× | pub fn socket(&self) -> PathBuf { |
| 206 | 40× | self.data.join("spectra.sock") |
| 207 | 40× | } |
| 208 | |
|
| 209 | | |
| 210 | | |
| 211 | 31× | pub async fn owner_writer(&self, owner_home: &std::path::Path) -> Result<Writer> { |
| 212 | 31× | let db = self.db()?; |
| 213 | 31× | if spectra_mcp::client::Client::alive(&self.socket()).await { |
| 214 | 2× | let ledger = spectra_core::ledger::Ledger::open_read_only(&db).await?; |
| 215 | 2× | let transport = identity::recorded_owner(&ledger, owner_home).await?; |
| 216 | 2× | return Ok(Writer::Daemon { ledger, client: Box::new(spectra_mcp::client::Client::new(&self.socket(), Some(transport))) }); |
| 217 | 29× | } |
| 218 | 29× | Ok(Writer::Direct(self.writer().await?)) |
| 219 | 31× | } |
| 220 | |
|
| 221 | | |
| 222 | 46× | pub fn config_owner_home(&self) -> Option<PathBuf> { |
| 223 | 46× | let text = std::fs::read_to_string(self.data.join("spectra.toml")).ok()?; |
| 224 | 32× | let t: toml::Table = text.parse().ok()?; |
| 225 | 32× | t.get("owner")?.get("home")?.as_str().map(PathBuf::from) |
| 226 | 46× | } |
| 227 | | } |
| 228 | |
|
| 229 | |
|
| 230 | | pub enum Writer { |
| 231 | | Direct(spectra_core::ledger::Ledger), |
| 232 | | Daemon { ledger: spectra_core::ledger::Ledger, client: Box<spectra_mcp::client::Client> }, |
| 233 | | } |
| 234 | |
|
| 235 | | impl Writer { |
| 236 | | |
| 237 | 31× | pub fn ledger(&self) -> &spectra_core::ledger::Ledger { |
| 238 | 31× | match self { |
| 239 | 31× | Writer::Direct(l) | Writer::Daemon { ledger: l, .. } => l, |
| 240 | | } |
| 241 | 31× | } |
| 242 | |
|
| 243 | | |
| 244 | 29× | pub async fn append(&self, events: Vec<spectra_core::event::NewEvent>) -> Result<()> { |
| 245 | 29× | match self { |
| 246 | 27× | Writer::Direct(l) => { |
| 247 | 27× | l.append(events).await?; |
| 248 | 27× | l.checkpoint().await?; |
| 249 | | } |
| 250 | 2× | Writer::Daemon { client, .. } => { |
| 251 | 2× | let wire: Vec<_> = events.iter().map(spectra_core::daemon::WireEvent::from_new).collect(); |
| 252 | 2× | let r = client.send("POST", "/api/v1/ledger/events", &[], serde_json::to_vec(&wire)?).await?; |
| 253 | 2× | if r.status != 200 { |
| 254 | 0× | let why = r |
| 255 | 0× | .json() |
| 256 | 0× | .ok() |
| 257 | 0× | .and_then(|v| v.get("title").and_then(|t| t.as_str()).map(String::from)) |
| 258 | 0× | .unwrap_or_else(|| String::from_utf8_lossy(&r.body).into()); |
| 259 | 0× | bail!("the daemon refused: {why}"); |
| 260 | 2× | } |
| 261 | | } |
| 262 | | } |
| 263 | 29× | Ok(()) |
| 264 | 29× | } |
| 265 | | } |
| 266 | |
|
| 267 | 7× | fn init_logging() { |
| 268 | 7× | let filter = tracing_subscriber::EnvFilter::try_from_env("SPECTRA_LOG").unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")); |
| 269 | 7× | tracing_subscriber::fmt().with_env_filter(filter).with_writer(std::io::stderr).init(); |
| 270 | 7× | } |
| 271 | |
|
| 272 | 0× | fn default_data() -> PathBuf { |
| 273 | 0× | if let Ok(x) = std::env::var("XDG_DATA_HOME") { |
| 274 | 0× | return PathBuf::from(x).join("spectra"); |
| 275 | 0× | } |
| 276 | 0× | let home = std::env::var("HOME").unwrap_or_else(|_| ".".into()); |
| 277 | 0× | PathBuf::from(home).join(".local").join("share").join("spectra") |
| 278 | 0× | } |
| 279 | |
|
| 280 | 80× | fn runtime() -> Result<tokio::runtime::Runtime> { |
| 281 | 80× | Ok(tokio::runtime::Builder::new_multi_thread().enable_all().build()?) |
| 282 | 80× | } |
| 283 | |
|
| 284 | 80× | pub fn run() -> Result<i32> { |
| 285 | 80× | let cli = Cli::parse(); |
| 286 | 80× | ui::init(cli.plain); |
| 287 | 80× | let ctx = Ctx { data: cli.data.unwrap_or_else(default_data) }; |
| 288 | 80× | match cli.command { |
| 289 | 0× | Command::Version { json } => version(json), |
| 290 | 15× | Command::Init { owner_home, name } => runtime()?.block_on(identity::init(&ctx, owner_home, name)), |
| 291 | 0× | Command::Identity { command } => runtime()?.block_on(async { |
| 292 | 0× | match command { |
| 293 | 0× | IdentityCmd::Show { json } => identity::show(&ctx, json).await, |
| 294 | 0× | IdentityCmd::Refresh { owner_home } => identity::refresh(&ctx, owner_home).await, |
| 295 | | } |
| 296 | 0× | }), |
| 297 | 7× | Command::Serve { bind, insecure } => { |
| 298 | 7× | init_logging(); |
| 299 | 7× | runtime()?.block_on(serve::serve(&ctx, &bind, insecure)) |
| 300 | | } |
| 301 | 2× | Command::Mcp { agent, home } => runtime()?.block_on(mcp::run(&ctx, &agent, home)), |
| 302 | 18× | Command::Project { command } => runtime()?.block_on(async { |
| 303 | 18× | match command { |
| 304 | 16× | ProjectCmd::Add { path, name, remote, owner_home } => project::add(&ctx, &path, name, remote, owner_home).await, |
| 305 | 2× | ProjectCmd::List { json } => project::list(&ctx, json).await, |
| 306 | | } |
| 307 | 18× | }), |
| 308 | 21× | Command::Agent { command } => runtime()?.block_on(async { |
| 309 | 21× | match command { |
| 310 | 15× | AgentCmd::Add { name, kind, project, home, uid, learn_uid, wait, owner_home } => { |
| 311 | 15× | agent::add(&ctx, agent::Add { name, kind, project, home, uid, learn_uid, wait_secs: wait, owner_home }).await |
| 312 | | } |
| 313 | 4× | AgentCmd::List { json } => agent::list(&ctx, json).await, |
| 314 | 2× | AgentCmd::Disable { name, owner_home } => agent::disable(&ctx, &name, owner_home).await, |
| 315 | | } |
| 316 | 21× | }), |
| 317 | 17× | Command::Admin { command } => runtime()?.block_on(async { |
| 318 | 17× | match command { |
| 319 | 12× | Admin::Verify { json } => admin::verify(&ctx, json).await, |
| 320 | 4× | Admin::Check => admin::check(&ctx).await, |
| 321 | 1× | Admin::Rebuild => admin::rebuild(&ctx).await, |
| 322 | | } |
| 323 | 17× | }), |
| 324 | | } |
| 325 | 80× | } |
| 326 | |
|
| 327 | 0× | fn version(as_json: bool) -> Result<i32> { |
| 328 | 0× | if as_json { |
| 329 | 0× | println!( |
| 330 | 0× | "{}", |
| 331 | 0× | serde_json::json!({ |
| 332 | 0× | "version": env!("CARGO_PKG_VERSION"), |
| 333 | 0× | "build": env!("SPECTRA_BUILD_ID"), |
| 334 | 0× | "mcp_protocol_versions": spectra_mcp::PROTOCOL_VERSIONS, |
| 335 | 0× | "ledger_format": spectra_core::LEDGER_FORMAT, |
| 336 | 0× | }) |
| 337 | 0× | ); |
| 338 | 0× | } else { |
| 339 | 0× | println!("spectra {}", env!("SPECTRA_VERSION_LONG")); |
| 340 | 0× | } |
| 341 | 0× | Ok(0) |
| 342 | 0× | } |