Skip to content

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.

feat(host): report failed network steps to analytics

leepokaic3f2b36c056c
diff --git a/docs/reference/analytics-events.md b/docs/reference/analytics-events.md
index ceb8d082..178d519f 100644
--- a/docs/reference/analytics-events.md
+++ b/docs/reference/analytics-events.md
@@ -101,6 +101,7 @@ host answers and sends it once the host is up.
| `host_update_installed` | host | `installHostUpdate` | — |
| `network_joined` | host | `networkCreate` / `networkJoin`: the computer is in a network of the user's computers | `via` (`create`/`invite`), never a name, invite or connector address |
| `network_changed` | host | `networkInvite` / `networkPromote` / `networkRemove` / `networkRecover` / `setNetworkRules` / `networkLeave` | `action` (`invite`/`promote`/`remove`/`recover`/`rules`/`leave`), never a computer's name, the invite, the recovery code or a rule |
+| `network_failed` | host | A network step failed: a saved network didn't start with the host, a bot's letter to another computer didn't go (refused, or no such computer; one queued for a computer that's away isn't a failure), a request's outcome didn't go back, a letter from another computer couldn't be taken in, or a `network*` call failed | `stage` (`start`/`send`/`reply`/`receive`/`action`); for `action`, the call's own `via` or `action` as on `network_joined` / `network_changed`; never the error's text, a name or an address |
## Screens
diff --git a/host/src/analytics.rs b/host/src/analytics.rs
index 9735b450..4f0278d1 100644
--- a/host/src/analytics.rs
+++ b/host/src/analytics.rs
@@ -22,7 +22,7 @@ mod session;
mod turns;
pub use lifecycle::{KV_STARTED, Via, device_paired, host_started, phone_connected};
-pub use observe::{answered, observe_call};
+pub use observe::{answered, failed, observe_call};
pub use reported::track;
pub use turns::{TurnOutcome, TurnSource, agent_fell_back, turn_finished};
@@ -100,6 +100,8 @@ pub enum Event {
/// `networkCreate` or `networkJoin`: this computer is in a network of the user's computers.
NetworkJoined,
NetworkChanged,
+ /// A network step failed (`NetworkStage`): starting a node, a letter, or a `network*` call.
+ NetworkFailed,
/// Error tracking (`errors`): a panic or an uncaught error.
Exception,
}
@@ -162,11 +164,37 @@ impl Event {
Self::HostUpdateInstalled => "host_update_installed",
Self::NetworkJoined => "network_joined",
Self::NetworkChanged => "network_changed",
+ Self::NetworkFailed => "network_failed",
Self::Exception => "$exception",
}
}
}
+/// Where a network of the user's computers failed (`network_failed`'s `stage`); a failed
+/// `network*` call is `action` (`observe::failed`).
+#[derive(Clone, Copy, Debug)]
+pub enum NetworkStage {
+ /// A saved network's node didn't start with the host.
+ Start,
+ /// A bot's letter to another computer didn't go (refused, or no such computer).
+ Send,
+ /// The outcome of another computer's request didn't go back.
+ Reply,
+ /// A letter from another computer couldn't be taken in.
+ Receive,
+}
+
+/// Reports a network step that failed; never the error's text, a name or an address.
+pub fn network_failed(hub: &Hub, stage: NetworkStage) {
+ let stage = match stage {
+ NetworkStage::Start => "start",
+ NetworkStage::Send => "send",
+ NetworkStage::Reply => "reply",
+ NetworkStage::Receive => "receive",
+ };
+ capture(hub, Event::NetworkFailed, Map::from_iter([("stage".into(), stage.into())]));
+}
+
pub struct Analytics {
enabled: Mutex<bool>,
tx: mpsc::UnboundedSender<Msg>,
diff --git a/host/src/analytics/observe.rs b/host/src/analytics/observe.rs
index e7d3f110..346ea0a5 100644
--- a/host/src/analytics/observe.rs
+++ b/host/src/analytics/observe.rs
@@ -224,6 +224,15 @@ pub fn answered(event: Event, mut p: Map<String, Value>, answer: &Value) -> Opti
Some(p)
}
+/// What a failed call reports: a `network*` call's failure keeps what it was (`via` or
+/// `action`) as `network_failed` with `stage` `action`; other failures report nothing.
+pub fn failed(event: Event, mut p: Map<String, Value>) -> Option<(Event, Map<String, Value>)> {
+ matches!(event, Event::NetworkJoined | Event::NetworkChanged).then(|| {
+ p.insert("stage".into(), "action".into());
+ (Event::NetworkFailed, p)
+ })
+}
+
/// The bot settings a patch changes, by name (never their values), sorted.
fn changed_fields(stored: &BotConfig, patch: &Value) -> Vec<String> {
let Ok(Value::Object(before)) = serde_json::to_value(stored) else { return vec![] };
diff --git a/host/src/analytics/observe/tests.rs b/host/src/analytics/observe/tests.rs
index 0d0e714e..aaa6b7f3 100644
--- a/host/src/analytics/observe/tests.rs
+++ b/host/src/analytics/observe/tests.rs
@@ -254,3 +254,20 @@ async fn store_lookups_add_fields_choice_and_platform() {
assert_eq!(p["platform"], "other");
std::fs::remove_dir_all(&dir).unwrap();
}
+
+#[test]
+fn a_failed_network_call_reports_what_it_was_and_others_nothing() {
+ let failed_call = |method: &str, b: &Value| {
+ let (event, p) = observe(&Caller::Local, method, b)?;
+ failed(event, p).map(|(e, p)| (e, Value::Object(p)))
+ };
+ assert_eq!(
+ failed_call("networkJoin", &json!({"invite": "codync-network:…", "name": "laptop"})),
+ Some((Event::NetworkFailed, json!({"via": "invite", "client": "local", "stage": "action"})))
+ );
+ assert_eq!(
+ failed_call("networkRemove", &json!({"computer": "desk"})),
+ Some((Event::NetworkFailed, json!({"action": "remove", "client": "local", "stage": "action"})))
+ );
+ assert_eq!(failed_call("setRoutineEnabled", &json!({"botId": "b", "id": "r", "enabled": false})), None);
+}
diff --git a/host/src/api/mod.rs b/host/src/api/mod.rs
index b552aa57..a98a911d 100644
--- a/host/src/api/mod.rs
+++ b/host/src/api/mod.rs
@@ -59,9 +59,11 @@ pub async fn dispatch(hub: &Arc<Hub>, caller: &Caller, method: &str, b: Value) -
devices::permit(caller, method)?;
let observed = crate::analytics::observe_call(hub, caller, method, &b);
let result = route(hub, caller, method, b).await;
- if let (Ok(answer), Some((event, properties))) = (&result, observed)
- && let Some(properties) = crate::analytics::answered(event, properties, answer)
- {
+ let report = observed.and_then(|(event, properties)| match &result {
+ Ok(answer) => crate::analytics::answered(event, properties, answer).map(|p| (event, p)),
+ Err(_) => crate::analytics::failed(event, properties),
+ });
+ if let Some((event, properties)) = report {
crate::analytics::capture(hub, event, properties);
}
result
diff --git a/host/src/chat/team/remote.rs b/host/src/chat/team/remote.rs
index fc7198eb..eb8a2c98 100644
--- a/host/src/chat/team/remote.rs
+++ b/host/src/chat/team/remote.rs
@@ -10,6 +10,7 @@ use super::{
use crate::LockExt;
use crate::agent::acp::truncate;
use crate::agent::bot::Cmd;
+use crate::analytics::NetworkStage;
use crate::hub::Hub;
use crate::network::Target;
use crate::store::{BotConfig, EntryKind, Lane};
@@ -66,6 +67,7 @@ pub async fn listen(hub: Arc<Hub>, network: String, mut inbox: mpsc::Receiver<Me
while let Some(m) = inbox.recv().await {
if let Err(error) = take(&hub, &network, &m) {
tracing::warn!(error = format!("{error:#}"), "couldn't take a letter from another computer");
+ crate::analytics::network_failed(&hub, NetworkStage::Receive);
}
}
}
@@ -152,6 +154,7 @@ async fn send(hub: &Arc<Hub>, node: &Node, source: &BotConfig, to: &Target, lett
}
Err(error) => {
hub.waiting.take(&id);
+ crate::analytics::network_failed(hub, NetworkStage::Send);
finish_entry(hub, &entry, RequestStatus::Failed, &format!("{error:#}"), None);
return Err(error);
}
@@ -187,6 +190,7 @@ pub(super) fn ask_for_replies(
Ok(Sent::Queued) => {}
Err(error) => {
hub.waiting.take(&request);
+ crate::analytics::network_failed(&hub, NetworkStage::Send);
hub.assistant.answered(&request, Err(format!("{error:#}")));
}
}
@@ -236,20 +240,20 @@ fn note(hub: &Hub, network: &str, target: &BotConfig, m: &Message) {
}
/// The outcome of a request from another computer goes back to the agent that sent it.
-pub(super) fn answer(hub: &Hub, to: &Return, outcome: &std::result::Result<String, String>, hops: u8) {
+pub(super) fn answer(hub: &Arc<Hub>, to: &Return, outcome: &std::result::Result<String, String>, hops: u8) {
match outcome {
Ok(reply) => send_back(hub, to, reply.clone(), RequestStatus::Completed, hops),
Err(detail) => send_back(hub, to, format!("Couldn't finish: {detail}"), RequestStatus::Failed, hops),
}
}
-pub(super) fn cancelled(hub: &Hub, to: &Return, hops: u8) {
+pub(super) fn cancelled(hub: &Arc<Hub>, to: &Return, hops: u8) {
send_back(hub, to, STOPPED.to_owned(), RequestStatus::Cancelled, hops);
}
-fn send_back(hub: &Hub, to: &Return, mut body: String, status: RequestStatus, hops: u8) {
+fn send_back(hub: &Arc<Hub>, to: &Return, mut body: String, status: RequestStatus, hops: u8) {
let Ok(slot) = hub.network.slot(Some(&to.network)) else { return };
- let node = slot.node;
+ let (node, hub) = (slot.node, hub.clone());
if body.trim().is_empty() {
body = "(no reply)".into();
}
@@ -260,6 +264,7 @@ fn send_back(hub: &Hub, to: &Return, mut body: String, status: RequestStatus, ho
tokio::spawn(async move {
if let Err(error) = node.send(letter).await {
tracing::warn!(error = format!("{error:#}"), "couldn't send a reply to another computer");
+ crate::analytics::network_failed(&hub, NetworkStage::Reply);
}
});
}
diff --git a/host/src/network/mod.rs b/host/src/network/mod.rs
index 03215c2f..fbc35391 100644
--- a/host/src/network/mod.rs
+++ b/host/src/network/mod.rs
@@ -268,6 +268,7 @@ pub async fn start(hub: &Arc<Hub>) {
let id = saved.id.clone();
if let Err(error) = boot(hub, &saved).await {
tracing::warn!(network = id, error = format!("{error:#}"), "couldn't start a network");
+ crate::analytics::network_failed(hub, crate::analytics::NetworkStage::Start);
}
}
}