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): tell agent failures apart in analytics

leepokai3dc8d56adb15
diff --git a/docs/reference/analytics-events.md b/docs/reference/analytics-events.md
index 76f86cdf..41513061 100644
--- a/docs/reference/analytics-events.md
+++ b/docs/reference/analytics-events.md
@@ -69,7 +69,7 @@ host answers and sends it once the host is up.
| `bot_deleted` | host | `deleteBot` | — |
| `message_sent` | host | `send` (a resend counts once) | `kind`, `agent`, `reply` (whether it quotes a message), `attachments` (count) |
| `turn_stopped` | host | `stop` | — |
-| `turn_finished` | host | An agent's turn ends | `agent`, `outcome` (`done`/`stopped`/`failed`/`incomplete`), `stop_reason`, `source` (`user`/`routine`/`group`/`bot`), `seconds`; when failed, `error` and, for `agent_error`, `rpc_code` and `agent_reason` |
+| `turn_finished` | host | An agent's turn ends | `agent`, `outcome` (`done`/`stopped`/`failed`/`incomplete`), `stop_reason`, `source` (`user`/`routine`/`group`/`bot`), `seconds`; when failed, `error` and, for `agent_error`, `rpc_code` and `agent_reason` (the adapter's own category, else one the host reads from the message, which is never sent: `session_ended`/`rate_limit`/`quota`/`overloaded`/`context`/`model`/`network`/`provider`/`unknown`) |
| `session_reset` | host | `newSession` | — |
| `permission_answered` | host | `respondPermission` | `choice` (`allow_once`/`allow_always`/`reject_once`/`reject_always`/`other`) |
| `reaction_added` | host | `react` | — |
diff --git a/host/src/analytics/turns.rs b/host/src/analytics/turns.rs
index 49aee08e..d2ea0fc5 100644
--- a/host/src/analytics/turns.rs
+++ b/host/src/analytics/turns.rs
@@ -62,9 +62,7 @@ pub fn turn_finished(
p["error"] = kind.into();
if let Some(rpc) = rpc {
p["rpc_code"] = rpc.code.into();
- if let Some(reason) = &rpc.kind {
- p["agent_reason"] = reason.as_str().into();
- }
+ p["agent_reason"] = rpc.kind.as_deref().unwrap_or_else(|| reason_of(&rpc.message)).into();
}
}
capture(hub, Event::TurnFinished, p.as_object().cloned().unwrap_or_default());
@@ -96,6 +94,41 @@ fn failure_kind(e: &anyhow::Error) -> (&'static str, Option<&crate::agent::acp::
("other", None)
}
+/// What an agent error without the adapter's own category is about, read from its message, which
+/// itself never leaves the computer. Checked in order: the first match wins. A status code counts
+/// only as a word of its own, not inside a token count or an id.
+fn reason_of(message: &str) -> &'static str {
+ const REASONS: &[(&str, &[&str])] = &[
+ ("session_ended", &["session has ended", "session not found", "no session"]),
+ ("rate_limit", &["rate limit", "rate_limit", "too many requests", "429"]),
+ ("quota", &["quota", "usage limit", "credit balance", "billing", "insufficient", "402"]),
+ ("overloaded", &["overloaded", "529", "503", "unavailable", "capacity"]),
+ ("context", &["context length", "context window", "maximum context", "too long", "token limit"]),
+ (
+ "network",
+ &[
+ "econn",
+ "enotfound",
+ "etimedout",
+ "fetch failed",
+ "network",
+ "socket",
+ "getaddrinfo",
+ "connection error",
+ "timed out",
+ "timeout",
+ ],
+ ),
+ ("provider", &["provider", "api key", "api_key", "base url"]),
+ // Last: many messages name a model in passing.
+ ("model", &["model"]),
+ ];
+ let message = message.to_lowercase();
+ let words: Vec<&str> = message.split(|c: char| !c.is_ascii_alphanumeric()).collect();
+ let says = |w: &str| if w.bytes().all(|b| b.is_ascii_digit()) { words.contains(&w) } else { message.contains(w) };
+ REASONS.iter().find(|(_, patterns)| patterns.iter().any(|w| says(w))).map_or("unknown", |(reason, _)| reason)
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -125,4 +158,22 @@ mod tests {
assert_eq!(failure_kind(&limited).1.and_then(|r| r.kind.as_deref()), Some("rate_limit"));
assert_eq!(code(&anyhow::anyhow!("something else")), ("other", None));
}
+
+ #[test]
+ fn an_uncategorized_agent_error_gets_a_reason_from_its_message() {
+ assert_eq!(reason_of("The Claude Agent session has ended"), "session_ended");
+ assert_eq!(reason_of("429 Too Many Requests: rate limit reached for model x"), "rate_limit");
+ assert_eq!(reason_of("Your credit balance is too low"), "quota");
+ assert_eq!(reason_of("Overloaded"), "overloaded");
+ assert_eq!(reason_of("prompt is too long: 210000 tokens"), "context");
+ assert_eq!(reason_of("prompt is too long: 205290 tokens > 200000 maximum"), "context", "529 inside a count");
+ assert_eq!(reason_of("Error 503 Service Unavailable"), "overloaded");
+ assert_eq!(reason_of("Model not found: openai/gpt-x"), "model");
+ assert_eq!(reason_of("fetch failed (ECONNRESET)"), "network");
+ assert_eq!(reason_of("API Error: Connection error."), "network");
+ assert_eq!(reason_of("Request timed out"), "network");
+ assert_eq!(reason_of("fetch failed while loading the model list"), "network");
+ assert_eq!(reason_of("No provider configured"), "provider");
+ assert_eq!(reason_of("Internal error: /Users/me/project"), "unknown");
+ }
}