synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-coord / src / bin / synapse-coord.rs
| 1 | //! Thin command-line front end over `synapse_coord`, mirroring `crumb claim|update|whisper| |
| 2 | //! close|sniff` output lines and exit codes (0 ok, 1 error, 2 blocked/refused). |
| 3 | //! |
| 4 | //! usage: synapse-coord --store DIR --top WORKSPACE [--expect-store-id ID] [--branch B --head H --base X] |
| 5 | //! [--synapse LANE REVISION BASE] [--session S] [--no-tree] <command> ... |
| 6 | //! claim <agent> <dir> <target> <intent> [ttl] |
| 7 | //! update <agent> <dir> <focus> [ttl] |
| 8 | //! whisper <from> <dir> <to|-> <message> [priority] [target_file] |
| 9 | //! close <agent> <dir> <target> [action] [message] |
| 10 | //! sniff <agent> <dir> |
| 11 | //! id print the store id, creating the marker if absent (--top optional) |
| 12 | //! |
| 13 | //! --expect-store-id ID refuses (exit 1) unless the store carries that STORE_ID marker. |
| 14 | //! |
| 15 | //! synapse-coord canon <file> parse a record and print it re-serialized (format parity check) |
| 16 | //! |
| 17 | //! synapse-coord board ... the mission message board (see `synapse_coord::board::cli`) |
| 18 | |
| 19 | use std::process::ExitCode; |
| 20 | use synapse_coord::{ |
| 21 | ClaimOutcome, CloseOutcome, CoordStore, FsOracle, Holder, NoOracle, PathOracle, StoreId, |
| 22 | Workspace, |
| 23 | }; |
| 24 | |
| 25 | fn die(msg: impl std::fmt::Display) -> ExitCode { |
| 26 | eprintln!("synapse-coord: {msg}"); |
| 27 | ExitCode::from(1) |
| 28 | } |
| 29 | |
| 30 | fn ttl(s: Option<&String>) -> Result<Option<i64>, String> { |
| 31 | match s { |
| 32 | None => Ok(None), |
| 33 | Some(s) => s |
| 34 | .parse::<i64>() |
| 35 | .ok() |
| 36 | .filter(|v| *v >= 0) |
| 37 | .map(Some) |
| 38 | .ok_or_else(|| format!("bad ttl: {s}")), |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn main() -> ExitCode { |
| 43 | let mut args: Vec<String> = std::env::args().skip(1).collect(); |
| 44 | if args.first().is_some_and(|a| a == "board") { |
| 45 | return ExitCode::from(synapse_coord::board::cli::run(&args[1..]) as u8); |
| 46 | } |
| 47 | if args.len() == 2 && args[0] == "canon" { |
| 48 | return match synapse_coord::read_record_file(std::path::Path::new(&args[1])) |
| 49 | .ok() |
| 50 | .flatten() |
| 51 | .and_then(|b| synapse_coord::json::parse(&b)) |
| 52 | { |
| 53 | Some(j) => { |
| 54 | use std::io::Write; |
| 55 | let _ = std::io::stdout().write_all(&j.dump()); |
| 56 | ExitCode::SUCCESS |
| 57 | } |
| 58 | None => die(format!("{} is not valid JSON", args[1])), |
| 59 | }; |
| 60 | } |
| 61 | let (mut store, mut top, mut expect) = (None, None, None); |
| 62 | let mut holder = Holder::default(); |
| 63 | let mut no_tree = false; |
| 64 | while args.first().is_some_and(|a| a.starts_with("--")) { |
| 65 | let flag = args.remove(0); |
| 66 | let mut take = || { |
| 67 | if args.is_empty() { |
| 68 | None |
| 69 | } else { |
| 70 | Some(args.remove(0)) |
| 71 | } |
| 72 | }; |
| 73 | let ok = match flag.as_str() { |
| 74 | "--store" => take().map(|v| store = Some(v)).is_some(), |
| 75 | "--top" => take().map(|v| top = Some(v)).is_some(), |
| 76 | "--expect-store-id" => match take().map(|v| StoreId::parse_hex(&v)) { |
| 77 | Some(Some(id)) => { |
| 78 | expect = Some(id); |
| 79 | true |
| 80 | } |
| 81 | _ => false, |
| 82 | }, |
| 83 | "--branch" => take().map(|v| holder.branch = v).is_some(), |
| 84 | "--head" => take().map(|v| holder.head = v).is_some(), |
| 85 | "--base" => take().map(|v| holder.base = v).is_some(), |
| 86 | "--session" => take().map(|v| holder.session = Some(v)).is_some(), |
| 87 | "--synapse" => match (take(), take(), take()) { |
| 88 | (Some(l), Some(r), Some(b)) => { |
| 89 | let session = holder.session.take(); |
| 90 | holder = Holder::synapse(&l, &r, &b); |
| 91 | holder.session = session; |
| 92 | true |
| 93 | } |
| 94 | _ => false, |
| 95 | }, |
| 96 | "--no-tree" => { |
| 97 | no_tree = true; |
| 98 | true |
| 99 | } |
| 100 | _ => false, |
| 101 | }; |
| 102 | if !ok { |
| 103 | return die(format!("bad or incomplete flag {flag}")); |
| 104 | } |
| 105 | } |
| 106 | let Some(store) = store else { |
| 107 | return die("--store is required"); |
| 108 | }; |
| 109 | let cs = match expect { |
| 110 | Some(id) => match CoordStore::open_expecting(store, id) { |
| 111 | Ok(cs) => cs, |
| 112 | Err(e) => return die(e), |
| 113 | }, |
| 114 | None => CoordStore::open(store), |
| 115 | }; |
| 116 | if args.len() == 1 && args[0] == "id" { |
| 117 | return match cs.ensure_store_id() { |
| 118 | Ok(id) => { |
| 119 | println!("{id}"); |
| 120 | ExitCode::SUCCESS |
| 121 | } |
| 122 | Err(e) => die(e), |
| 123 | }; |
| 124 | } |
| 125 | let Some(top) = top else { |
| 126 | return die("--top is required"); |
| 127 | }; |
| 128 | let oracle: Box<dyn PathOracle> = if no_tree { |
| 129 | Box::new(NoOracle) |
| 130 | } else { |
| 131 | Box::new(FsOracle(top.clone().into())) |
| 132 | }; |
| 133 | let ws = Workspace::with_oracle(top.clone(), holder, oracle); |
| 134 | let a = &args; |
| 135 | let n = a.len(); |
| 136 | let cmd = a.first().map(String::as_str).unwrap_or(""); |
| 137 | let res: Result<u8, String> = (|| { |
| 138 | match cmd { |
| 139 | "claim" if n >= 5 => { |
| 140 | let t = ttl(a.get(5))?; |
| 141 | match cs |
| 142 | .claim(&ws, &a[1], &a[2], &a[3], &a[4], t) |
| 143 | .map_err(|e| e.to_string())? |
| 144 | { |
| 145 | ClaimOutcome::Claimed { key } => { |
| 146 | println!( |
| 147 | "CLAIMED {key} in {} as {} on {} (ttl {}s)", |
| 148 | ws.top, |
| 149 | a[1], |
| 150 | ws.holder.branch, |
| 151 | t.unwrap_or(synapse_coord::LOCK_TTL) |
| 152 | ); |
| 153 | Ok(0) |
| 154 | } |
| 155 | ClaimOutcome::Blocked { key, holder: h } => { |
| 156 | println!( |
| 157 | "BLOCKED {key} held by {} on branch {} (worktree {}) since {} ({})", |
| 158 | h.holder, h.branch, h.worktree, h.acquired_at, h.intent |
| 159 | ); |
| 160 | Ok(2) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | "update" if n >= 4 => { |
| 165 | let t = ttl(a.get(4))?; |
| 166 | cs.update(&ws, &a[1], &a[2], &a[3], t) |
| 167 | .map_err(|e| e.to_string())?; |
| 168 | println!( |
| 169 | "SCENT {} in {}: {} (ttl {}s)", |
| 170 | a[1], |
| 171 | ws.top, |
| 172 | a[3], |
| 173 | t.unwrap_or(synapse_coord::SCENT_TTL) |
| 174 | ); |
| 175 | Ok(0) |
| 176 | } |
| 177 | "whisper" if n >= 5 => { |
| 178 | cs.whisper( |
| 179 | &ws, |
| 180 | &a[1], |
| 181 | &a[2], |
| 182 | Some(a[3].as_str()), |
| 183 | &a[4], |
| 184 | a.get(5).map(String::as_str), |
| 185 | a.get(6).map(String::as_str), |
| 186 | ) |
| 187 | .map_err(|e| e.to_string())?; |
| 188 | let to = if a[3] == "-" { "(broadcast)" } else { a[3].as_str() }; |
| 189 | println!("WHISPER {} -> {to} in {}", a[1], ws.top); |
| 190 | Ok(0) |
| 191 | } |
| 192 | "close" if n >= 4 => { |
| 193 | let action = a.get(4).map(String::as_str).unwrap_or("modify"); |
| 194 | match cs |
| 195 | .close(&ws, &a[1], &a[2], &a[3], action, a.get(5).map(String::as_str)) |
| 196 | .map_err(|e| e.to_string())? |
| 197 | { |
| 198 | CloseOutcome::Closed { key, lock_was_held } => { |
| 199 | println!( |
| 200 | "CLOSED {key} in {} by {} ({action}){}", |
| 201 | ws.top, |
| 202 | a[1], |
| 203 | if lock_was_held { "" } else { " [no lock was held]" } |
| 204 | ); |
| 205 | Ok(0) |
| 206 | } |
| 207 | CloseOutcome::Refused { key, holder } => { |
| 208 | println!("REFUSED: {key} is held by {holder}, not {}", a[1]); |
| 209 | Ok(2) |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | "sniff" if n >= 3 => { |
| 214 | let s = cs.sniff(&ws, &a[1], &a[2]).map_err(|e| e.to_string())?; |
| 215 | print!("{}", s.render(&ws, &a[1])); |
| 216 | Ok(0) |
| 217 | } |
| 218 | _ => Err("usage: synapse-coord --store DIR --top WORKSPACE [holder flags] claim|update|whisper|close|sniff ...".into()), |
| 219 | } |
| 220 | })(); |
| 221 | match res { |
| 222 | Ok(code) => ExitCode::from(code), |
| 223 | Err(e) => die(e), |
| 224 | } |
| 225 | } |