Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.
Rust acpagent-client-protocolai-agentsai-assistantclaudeclaude-codecodexcoding-agentscursorgeminigemini-cligrok-bot-alternativeiosiphonelinuxmacosmcpopen-sourcerustself-hosted
fix(host): save a lead change all at once and in order
leepokaie1b431573417
diff --git a/host/src/hub/lead.rs b/host/src/hub/lead.rsindex 6407cb42..fe7fccf3 100644--- a/host/src/hub/lead.rs+++ b/host/src/hub/lead.rs@@ -7,27 +7,19 @@ use crate::store::BotConfig;use anyhow::{Result, anyhow, bail};impl Hub {- /// Makes `id` the computer's one lead in place of the one before (`chat::lead::set`): checked and saved under one- /// `emit_lock`, so two picks never leave two leads and a bot deleted or hidden meanwhile never leads. Each bot- /// whose role changed is then reconfigured, so its next message tells it (`context::profile_update`).+ /// Makes `id` the computer's one lead in place of the one before (`chat::lead::set`): checked and saved in one+ /// transaction under one `emit_lock`, so two picks never leave two leads (or none), and a bot deleted or hidden+ /// meanwhile never leads. Each bot whose role changed is reconfigured under the same lock, so its next message+ /// tells it (`context::profile_update`) and a later pick's settings always reach it after this one's.pub(crate) fn make_lead(&self, id: &str) -> Result<()> {- let mut changed = vec![];- let saved = self.save_lead(id, &mut changed);- // Emitted even when a save failed: a saved row has its rev already, and a client past it must not miss it.- for cfg in &changed {- self.emit_bot(&cfg.id);+ for bot in self.save_lead(id)? {+ self.emit_bot(&bot);}- for cfg in changed {- let bot = cfg.id.clone();- if let Err(error) = self.send_cmd(&bot, Cmd::Reconfigure(Box::new(cfg))) {- tracing::warn!(bot, error = format!("{error:#}"), "couldn't tell a bot its lead role changed");- }- }- saved+ Ok(())}- /// Sets `id`'s lead flag and clears every other live bot's, noting each bot saved in `changed`.- fn save_lead(&self, id: &str, changed: &mut Vec<BotConfig>) -> Result<()> {+ /// Sets `id`'s lead flag and clears every other live bot's, then reconfigures each changed bot; returns their ids.+ fn save_lead(&self, id: &str) -> Result<Vec<String>> {let _g = self.emit_lock.locked();let bots: Vec<BotConfig> = self.store.bots()?.into_iter().filter(|r| !r.deleted).map(|r| r.config).collect();let mut target = bots.iter().find(|b| b.id == id).cloned().ok_or_else(|| anyhow!("unknown bot"))?;@@ -37,14 +29,24 @@ impl Hub {target.lead = true;// Refuses a hidden bot.validate(&target)?;- for mut cfg in bots {- let lead = cfg.id == id;- if cfg.lead != lead {- cfg.lead = lead;- self.store.save_bot(&cfg)?;- changed.push(cfg);- }- }- Ok(())+ let changed: Vec<BotConfig> = bots+ .into_iter()+ .filter(|cfg| cfg.lead != (cfg.id == id))+ .map(|mut cfg| {+ cfg.lead = cfg.id == id;+ cfg+ })+ .collect();+ self.store.save_bots(&changed)?;+ Ok(changed+ .into_iter()+ .map(|cfg| {+ let bot = cfg.id.clone();+ if let Err(error) = self.send_cmd(&bot, Cmd::Reconfigure(Box::new(cfg))) {+ tracing::warn!(bot, error = format!("{error:#}"), "couldn't tell a bot its lead role changed");+ }+ bot+ })+ .collect())}}diff --git a/host/src/store/bots.rs b/host/src/store/bots.rsindex 9fd9bab8..ca4a0df0 100644--- a/host/src/store/bots.rs+++ b/host/src/store/bots.rs@@ -26,6 +26,8 @@ fn parse_bot((id, cfg, rev, deleted, session_id, read_rev): RawBot) -> Option<Bo}const BOT_COLS: &str = "id, config, rev, deleted, session_id, read_rev";+const SAVE_BOT: &str = "INSERT INTO bots(id, rev, config) VALUES(?1, ?2, ?3)+ ON CONFLICT(id) DO UPDATE SET rev = ?2, config = ?3 WHERE deleted = 0";impl Store {pub fn bots(&self) -> Result<Vec<BotRow>> {@@ -44,14 +46,22 @@ impl Store {pub fn save_bot(&self, cfg: &BotConfig) -> Result<i64> {let c = self.db.locked();let rev = next_rev(&c)?;- c.execute(- "INSERT INTO bots(id, rev, config) VALUES(?1, ?2, ?3)- ON CONFLICT(id) DO UPDATE SET rev = ?2, config = ?3 WHERE deleted = 0",- params![cfg.id, rev, serde_json::to_string(cfg)?],- )?;+ c.execute(SAVE_BOT, params![cfg.id, rev, serde_json::to_string(cfg)?])?;Ok(rev)}+ /// Saves several bots in one transaction, each with its own rev: all of them or, on an error, none.+ pub fn save_bots(&self, cfgs: &[BotConfig]) -> Result<()> {+ let mut c = self.db.locked();+ let tx = c.transaction()?;+ for cfg in cfgs {+ let rev = next_rev(&tx)?;+ tx.execute(SAVE_BOT, params![cfg.id, rev, serde_json::to_string(cfg)?])?;+ }+ tx.commit()?;+ Ok(())+ }+pub fn touch_bot(&self, id: &str) -> Result<i64> {let c = self.db.locked();let rev = next_rev(&c)?;@@ -186,6 +196,12 @@ mod tests {auto_name: false,created_at: 0,};+ // Several at once: each with its own rev, in order.+ let other = BotConfig { id: "b0".into(), ..cfg.clone() };+ let before = s.current_rev();+ s.save_bots(&[other.clone(), other]).unwrap();+ assert_eq!(s.current_rev(), before + 2);+ assert_eq!(s.bot("b0").unwrap().map(|r| r.rev), Some(before + 2));s.save_bot(&cfg).unwrap();let u = s.insert_entry(&Lane::main("b1"), EntryKind::User, 1, &serde_json::json!({"text": "hi"})).unwrap();let a = s