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.

fix(host): leave no unknown agent session open when a routine runs out of time during setup

leepokai81fec06285d0
diff --git a/docs/features/routines.md b/docs/features/routines.md
index 4b151e01..55b68626 100644
--- a/docs/features/routines.md
+++ b/docs/features/routines.md
@@ -134,7 +134,10 @@ host checks the run's turn for a tool call whose ACP kind isn't `read`, `search`
(busy); a paused routine is refused until it's resumed.
Each run has a timeout (default one hour, configurable from 1 to 86400 seconds).
-A hung agent is terminated so subsequent work can proceed. A run's outcome is
+A hung agent is terminated so subsequent work can proceed. When the time limit cuts
+session setup short, an agent that can load sessions is stopped too, so a session it was
+still opening can't stay open unknown to the host; one that can't keeps running with the
+sessions it has. A run's outcome is
saved before it is reported, and a failure notice has a stable transcript ID, so a
restart never reports it twice. Failed reporting retries without rerunning the agent.
The latest 1000 terminal runs are retained globally, plus active, unpublished and retryable runs;
diff --git a/host/src/agent/bot/launch.rs b/host/src/agent/bot/launch.rs
index 4ed4f1ec..f90ddb74 100644
--- a/host/src/agent/bot/launch.rs
+++ b/host/src/agent/bot/launch.rs
@@ -151,6 +151,7 @@ pub(crate) async fn start_agent(command: &str, cwd: &str, env: &[(String, String
adapter: adapter(&init),
loaded: HashMap::new(),
started: Instant::now(),
+ opening: false,
fast_options: HashMap::new(),
reports_compaction: false,
usage: HashMap::new(),
diff --git a/host/src/agent/bot/mod.rs b/host/src/agent/bot/mod.rs
index 8ea00e7f..6667b6fe 100644
--- a/host/src/agent/bot/mod.rs
+++ b/host/src/agent/bot/mod.rs
@@ -177,6 +177,9 @@ pub(crate) struct Conn {
/// Sessions already created or loaded in this process, and when each was last used.
loaded: HashMap<String, Instant>,
started: Instant,
+ /// A `session/new` or `session/load` is waiting for its answer. Still set when a routine's time limit cut it short:
+ /// the agent may then open that session, tools and all, under an id the host never learns (`drop_unanswered`).
+ opening: bool,
/// Each live session's Fast mode option, as the agent last listed it.
fast_options: HashMap<String, Value>,
/// The agent reports compactions (`compaction_update`); until it does, a fall in usage stands in for them.
diff --git a/host/src/agent/bot/session.rs b/host/src/agent/bot/session.rs
index f3dfa887..050b6112 100644
--- a/host/src/agent/bot/session.rs
+++ b/host/src/agent/bot/session.rs
@@ -8,7 +8,7 @@ use crate::agent::own_memory;
use crate::agent::registry::Candidate;
use crate::chat::{context, memory};
use crate::store::run_session_key;
-use anyhow::{Result, anyhow, bail};
+use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};
use std::time::{Duration, Instant};
@@ -124,9 +124,20 @@ impl Actor {
})
}
+ /// `ensure_session` for the turn: a routine run's setup must end by its time limit, and setup cut short
+ /// mustn't leave the agent opening a session the host never learns of.
+ pub(super) async fn ensure_turn_session(&mut self, slot: Option<&str>) -> Result<()> {
+ let Some(deadline) = self.routine_deadline else { return self.ensure_session(slot).await };
+ let setup = tokio::time::timeout_at(deadline.into(), self.ensure_session(slot)).await;
+ if setup.is_err() {
+ self.drop_unanswered().await;
+ }
+ setup.context("routine session setup timed out")?
+ }
+
/// Makes sure the agent runs and `slot`'s session (main, or a routine run's) is live;
/// sets `turn_session`.
- pub(super) async fn ensure_session(&mut self, slot: Option<&str>) -> Result<()> {
+ async fn ensure_session(&mut self, slot: Option<&str>) -> Result<()> {
if !self.cfg.connectors.is_empty()
|| self.hub.store.kv_read(&format!("agent-env:{}", self.cfg.backend))?.is_some()
{
@@ -267,8 +278,11 @@ impl Actor {
/// A session request the agent doesn't answer in time stops the agent: a hung one
/// would otherwise hold the bot, and its Stop, forever.
pub(super) async fn session_request(&mut self, method: &str, params: Value) -> Result<Value> {
- let acp = self.conn.as_ref().ok_or_else(|| anyhow!("agent not running"))?.acp.clone();
- if let Ok(result) = tokio::time::timeout(SESSION_TIMEOUT, acp.request(method, params)).await {
+ let conn = self.conn.as_mut().ok_or_else(|| anyhow!("agent not running"))?;
+ conn.opening = matches!(method, "session/new" | "session/load");
+ let answered = tokio::time::timeout(SESSION_TIMEOUT, conn.acp.request(method, params)).await;
+ conn.opening = false;
+ if let Ok(result) = answered {
return result;
}
if let Some(c) = self.conn.take() {
@@ -278,6 +292,24 @@ impl Actor {
Err(acp::Failure::error(acp::FailureKind::Timeout, message))
}
+ /// A routine's time limit cut a session request short. An agent that can resume stops, so a session it might
+ /// still open goes with its process; one that can't keeps running rather than lose the sessions it has.
+ async fn drop_unanswered(&mut self) {
+ let Some(conn) = self.conn.as_mut() else { return };
+ if !std::mem::take(&mut conn.opening) {
+ return;
+ }
+ if !conn.sessions.load {
+ // ponytail: that session lasts until the agent next stops; keep the cut-short request's Answer past the
+ // time limit and session/close the id it returns, if that ever matters.
+ tracing::warn!(bot = %self.cfg.id, "a session request was cut short; the agent can't resume, so it keeps running");
+ return;
+ }
+ if let Some(c) = self.conn.take() {
+ c.acp.kill().await;
+ }
+ }
+
/// Drops the main ACP session so the next turn starts a fresh one.
pub(super) fn forget_session(&mut self) {
self.set_session(None, None);
diff --git a/host/src/agent/bot/turn.rs b/host/src/agent/bot/turn.rs
index 30a17922..134897ba 100644
--- a/host/src/agent/bot/turn.rs
+++ b/host/src/agent/bot/turn.rs
@@ -8,7 +8,7 @@ use crate::chat::memory;
use crate::hub::BotStatus;
use crate::remote::push::{self, AlertKind};
use crate::store::{EntryKind, Lane, now_ms};
-use anyhow::{Context, Result, anyhow};
+use anyhow::{Result, anyhow};
use serde_json::json;
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
@@ -167,13 +167,7 @@ impl Actor {
let (hub, cfg) = (self.hub.clone(), self.cfg.clone());
let _ = tokio::task::spawn_blocking(move || memory::files::scan(&hub, &cfg)).await;
let slot = self.slot();
- if let Some(deadline) = self.routine_deadline {
- tokio::time::timeout_at(tokio::time::Instant::from_std(deadline), self.ensure_session(slot.as_deref()))
- .await
- .context("routine session setup timed out")??;
- } else {
- self.ensure_session(slot.as_deref()).await?;
- }
+ self.ensure_turn_session(slot.as_deref()).await?;
if !hidden {
self.carry_in();
}
diff --git a/host/tests/fixtures/routine_agent.py b/host/tests/fixtures/routine_agent.py
index 8db94482..f0991a81 100644
--- a/host/tests/fixtures/routine_agent.py
+++ b/host/tests/fixtures/routine_agent.py
@@ -27,6 +27,11 @@ def log(event, sid, text):
output.write(json.dumps({"event": event, "session": sid, "text": text}) + "\n")
+def answer_late(answer):
+ log("late", answer["result"]["sessionId"], "")
+ send(answer)
+
+
def prompt(request, stop):
sid = request["params"]["sessionId"]
text = request["params"]["prompt"][0]["text"]
@@ -135,12 +140,21 @@ for line in sys.stdin:
method = request.get("method")
if method == "initialize":
log("initialize", "", str(os.getpid()))
- capabilities = {"loadSession": not pathlib.Path("no-load").exists(), "sessionCapabilities": {"close": {}}}
+ resumable = not pathlib.Path("no-load").exists() and "--no-load" not in sys.argv
+ capabilities = {"loadSession": resumable, "sessionCapabilities": {"close": {}}}
send({"id": request["id"], "result": {"agentCapabilities": capabilities}})
elif method == "session/new":
servers = request["params"]["mcpServers"]
- if "--hang-new" not in sys.argv: # an agent stuck starting never answers
- send({"id": request["id"], "result": {"sessionId": str(uuid.uuid4())}})
+ answer = {"id": request["id"], "result": {"sessionId": str(uuid.uuid4())}}
+ if "--slow-new" in sys.argv and not pathlib.Path("slowed").exists():
+ # The first session ever opens 4 s late (its tools starting), and is logged as it does, unless the agent
+ # is stopped first.
+ pathlib.Path("slowed").touch()
+ late = threading.Timer(4, answer_late, args=(answer,))
+ late.daemon = True
+ late.start()
+ elif "--hang-new" not in sys.argv: # an agent stuck starting never answers
+ send(answer)
elif method == "session/load":
sid = request["params"]["sessionId"]
if sid in sessions:
diff --git a/host/tests/routine_retry_e2e.rs b/host/tests/routine_retry_e2e.rs
index 53518060..78ceae47 100644
--- a/host/tests/routine_retry_e2e.rs
+++ b/host/tests/routine_retry_e2e.rs
@@ -127,3 +127,38 @@ async fn a_run_whose_agent_did_not_start_within_its_time_limit_waits_for_the_use
let listing = host.call("routines", json!({"botId": bot})).await;
assert!(!listing["runs"].as_array().unwrap().iter().any(|r| r["status"] == "pending"), "no automatic retry");
}
+
+/// How many events of `kind` `routine_agent.py` logged.
+fn agent_events(host: &Host, bot: &str, kind: &str) -> usize {
+ host.log(bot, "events.jsonl").iter().filter(|e| e["event"] == kind).count()
+}
+
+/// A time limit that cuts `session/new` short must not leave the agent to open that session (tools and all) under
+/// an id the host never learns: an agent that can resume stops, and the next run starts it again.
+#[tokio::test]
+async fn a_session_cut_short_by_the_time_limit_is_not_left_open() {
+ let host = Host::start().await;
+ let (bot, id) = routine(&host, "slow", "--slow-new", "slow start", 2).await;
+ deliver(&host, &bot, &id, "payload").await;
+ let failed = run(&host, &bot, &id, "failed").await;
+ assert!(failed["detail"].as_str().unwrap().contains("setup timed out"), "{failed}");
+ // Past the moment the agent would have opened it.
+ tokio::time::sleep(Duration::from_secs(4)).await;
+ assert_eq!(agent_events(&host, &bot, "late"), 0, "the agent opened a session the host never learned of");
+ let retried = host.call("retryRoutineRun", json!({"botId": bot, "runId": failed["id"]})).await;
+ assert_eq!(run(&host, &bot, &id, "succeeded").await["id"], retried["run"]["id"]);
+}
+
+/// An agent that can't resume keeps running, and keeps the sessions it has, rather than stop over a session cut
+/// short; the next run still works.
+#[tokio::test]
+async fn an_agent_that_cannot_resume_keeps_running_after_a_session_cut_short() {
+ let host = Host::start().await;
+ let (bot, id) = routine(&host, "slow-kept", "--slow-new --no-load", "slow start", 2).await;
+ deliver(&host, &bot, &id, "payload").await;
+ let failed = run(&host, &bot, &id, "failed").await;
+ assert!(failed["detail"].as_str().unwrap().contains("setup timed out"), "{failed}");
+ let retried = host.call("retryRoutineRun", json!({"botId": bot, "runId": failed["id"]})).await;
+ assert_eq!(run(&host, &bot, &id, "succeeded").await["id"], retried["run"]["id"]);
+ assert_eq!(agent_events(&host, &bot, "initialize"), 1, "the agent was restarted");
+}