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): resend a bot's instructions until a prompt carrying them succeeds

leepokaia6377e635ba3
diff --git a/docs/design/bot-memory.md b/docs/design/bot-memory.md
index 60952a51..b7b673df 100644
--- a/docs/design/bot-memory.md
+++ b/docs/design/bot-memory.md
@@ -354,7 +354,7 @@ it offers (`Instructions` in `agent/bot`):
unstable): when an agent advertises `sessionCapabilities.systemPrompt`, the opening goes as
`{"mode": "append", "content": [text]}` on `session/new` and `session/load` (the session's current snapshot). The
agent keeps it at system level, across compaction.
-3. **Every other agent: the first message.** A `<bot-profile>` block precedes the first prompt of a new session.
+3. **Every other agent: the first message.** A `<bot-profile>` block precedes the first prompt of a new session, and each retry until a prompt carrying it succeeds (the snapshot's `unprompted`, kept across a host restart).
After a compaction the next prompt carries a fresh one that says it replaces any earlier profile, so the agent
never holds two versions that disagree. The compaction is the agent's `compaction_update`, or, for an agent
that never sent one, its `usage_update.used` falling below half of a previous value of at least 20k.
diff --git a/docs/features/context-and-memory.md b/docs/features/context-and-memory.md
index ed1b3ecc..5f3b6a9e 100644
--- a/docs/features/context-and-memory.md
+++ b/docs/features/context-and-memory.md
@@ -28,7 +28,7 @@ its own context and compacts it itself.
`_meta.systemPrompt.append` on `session/new` and `session/load`. It survives compaction.
- **An agent advertising ACP `sessionCapabilities.systemPrompt`** receives it as `systemPrompt`
(`append`) on `session/new`, `session/load` and `session/fork`, and keeps it across compaction.
- - **Every other harness** receives it in the first message of each session (`<bot-profile>…</bot-profile>`),
+ - **Every other harness** receives it in the first message of each session (`<bot-profile>…</bot-profile>`; repeated with each message until one carrying it succeeds, the snapshot's `unprompted`, kept across a host restart),
and once more, marked as replacing the earlier one, on the first message after a compaction.
- **Compaction epoch:** Codync advertises `clientCapabilities.session.compaction`. Each completed
`compaction_update` (deduplicated by `compactionId`) increments `context.epoch.<bot>`; for an agent that has
diff --git a/host/src/agent/bot/mod.rs b/host/src/agent/bot/mod.rs
index 202f225d..8ea00e7f 100644
--- a/host/src/agent/bot/mod.rs
+++ b/host/src/agent/bot/mod.rs
@@ -105,6 +105,7 @@ pub fn spawn(hub: Arc<Hub>, base: BotConfig) -> BotHandle {
turn_call: None,
turn_memory_revision: String::new(),
announce: None,
+ first_prompt: None,
turn: None,
queue: VecDeque::new(),
active_request: None,
@@ -267,6 +268,8 @@ struct Actor {
turn_memory_revision: String,
/// A profile update this turn carried, recorded once the agent got it.
announce: Option<(Snapshot, Identity)>,
+ /// The snapshot of a new session's first prompt, marked delivered once the agent answered it.
+ first_prompt: Option<Snapshot>,
turn: Option<i64>,
queue: VecDeque<Queued>,
active_request: Option<crate::chat::team::BotRequest>,
diff --git a/host/src/agent/bot/turn.rs b/host/src/agent/bot/turn.rs
index 23206238..30a17922 100644
--- a/host/src/agent/bot/turn.rs
+++ b/host/src/agent/bot/turn.rs
@@ -38,7 +38,7 @@ impl Actor {
}
// A compaction may have dropped the profile the session's first message carried.
let resend = instructions == Instructions::FirstMessage && self.profile_resend.remove(sid);
- if self.session_fresh || resend {
+ if snapshot.unprompted || resend {
// The instructions reaching the agent now hold memory as it was when they were rendered.
memory::opened(&self.hub.store, &self.cfg.id, sid, &snapshot.memory)?;
}
@@ -46,7 +46,12 @@ impl Actor {
prompt = format!("{notice}\n\n{prompt}");
self.turn_memory_revision = revision;
}
- if std::mem::take(&mut self.session_fresh) && instructions == Instructions::FirstMessage {
+ // A prompt that failed may never have reached the agent: the instructions stay owed until one succeeds.
+ self.session_fresh = false;
+ if snapshot.unprompted {
+ self.first_prompt = Some(snapshot.clone());
+ }
+ if snapshot.unprompted && instructions == Instructions::FirstMessage {
prompt = format!("<bot-profile>\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);
} else if resend {
context::mark_announced(&self.hub.store, &self.cfg.id, &snapshot, snapshot.system_identity.clone())?;
@@ -110,6 +115,7 @@ impl Actor {
self.turn_call = crate::chat::calls::of_turn(&self.hub.store, answered).or(opens);
self.turn_memory_revision.clear();
self.announce = None;
+ self.first_prompt = None;
self.stop_requested = false;
self.cancel_deadline = None;
let team_turn = if self.active_request.is_some() {
@@ -359,6 +365,11 @@ impl Actor {
{
tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the profile update");
}
+ if let (Ok(_), Some(sent)) = (&done, self.first_prompt.take())
+ && let Err(error) = context::mark_prompted(&self.hub.store, &self.cfg.id, &sent)
+ {
+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the delivered instructions");
+ }
if let (Some(user), Some(agent), "end_turn") = (self.turn_text.take(), &final_text, stop_reason.as_str()) {
let turn = crate::chat::naming::Turn { user, agent: agent.clone() };
let _ = self.dreamer.send(memory::DreamEvent::Turn(turn));
diff --git a/host/src/chat/context.rs b/host/src/chat/context.rs
index 60bc89ab..416a11dc 100644
--- a/host/src/chat/context.rs
+++ b/host/src/chat/context.rs
@@ -48,6 +48,9 @@ pub struct Snapshot {
/// The memory revision `system` holds (`memory::current_revision` before it was rendered).
#[serde(default)]
pub memory: String,
+ /// True from the session's creation until a prompt that carried its instructions has reached the agent.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub unprompted: bool,
}
// A bot talks in several sessions (its chat and groups, routine runs), each with its own: snapshots
@@ -107,11 +110,13 @@ pub fn resolve(store: &Store, cfg: &BotConfig, session: &str) -> Result<Snapshot
}
let identity = Identity::of(store, cfg);
let memory = crate::chat::memory::current_revision(store, &cfg.id);
+ let unprompted = stored.as_ref().is_some_and(|old| old.unprompted);
let s = Snapshot {
session: session.to_owned(),
epoch,
system: render(store, cfg),
system_identity: identity.clone(),
+ unprompted,
// A re-render reaches the agent only as Claude's system prompt or a re-sent profile (see `mark_announced`):
// until then it knows the identity it last heard, so an update it missed is still announced.
announced: stored.map_or(identity, |old| old.announced),
@@ -158,11 +163,18 @@ pub fn adopt(store: &Store, cfg: &BotConfig, session: &str, system: String, memo
system_identity: identity.clone(),
announced: identity,
memory,
+ unprompted: true,
};
save(store, &cfg.id, &s)?;
Ok(s)
}
+/// After the first prompt of a session reached the agent: its instructions are no longer owed, unless the
+/// snapshot moved on meanwhile.
+pub fn mark_prompted(store: &Store, bot_id: &str, sent: &Snapshot) -> Result<()> {
+ update_if_current(store, bot_id, sent, |current| std::mem::take(&mut current.unprompted))
+}
+
/// The bot's instructions, rendered live.
pub fn render(store: &Store, cfg: &BotConfig) -> String {
let name = cfg.name.trim();
@@ -242,15 +254,25 @@ pub fn profile_update(store: &Store, snapshot: &Snapshot, cfg: &BotConfig) -> Op
/// After the turn that carried an update reached the agent: remember it heard
/// `identity`, unless the snapshot moved on meanwhile (a compaction re-rendered it).
pub fn mark_announced(store: &Store, bot_id: &str, turn_snapshot: &Snapshot, identity: Identity) -> Result<()> {
- let Some(mut current) = load(store, bot_id, &turn_snapshot.session) else { return Ok(()) };
- if current.session != turn_snapshot.session
- || current.epoch != turn_snapshot.epoch
- || current.system != turn_snapshot.system
- || current.announced == identity
- {
+ update_if_current(store, bot_id, turn_snapshot, |current| {
+ let changed = current.announced != identity;
+ current.announced = identity;
+ changed
+ })
+}
+
+/// Applies `change` to the stored snapshot of `sent`'s session unless it moved on meanwhile (a re-render);
+/// saves only when `change` returns true.
+fn update_if_current(
+ store: &Store,
+ bot_id: &str,
+ sent: &Snapshot,
+ change: impl FnOnce(&mut Snapshot) -> bool,
+) -> Result<()> {
+ let Some(mut current) = load(store, bot_id, &sent.session) else { return Ok(()) };
+ if current.epoch != sent.epoch || current.system != sent.system || !change(&mut current) {
return Ok(());
}
- current.announced = identity;
save(store, bot_id, &current)
}
@@ -342,4 +364,14 @@ mod tests {
assert!(snap.system.contains("Description: Fix bugs and write tests"), "re-rendered");
assert!(profile_update(&store, &snap, &b).is_some(), "the agent never heard the new description");
}
+
+ #[test]
+ fn a_new_sessions_instructions_stay_owed_until_a_prompt_carried_them() {
+ let store = store();
+ let a = cfg("Fixer", "Fix bugs");
+ let first = adopt(&store, &a, "s1", "SYSTEM v1".into(), String::new()).expect("adopt");
+ assert!(resolve(&store, &a, "s1").expect("resolve").unprompted, "survives a restart");
+ mark_prompted(&store, &a.id, &first).expect("mark");
+ assert!(!resolve(&store, &a, "s1").expect("resolve").unprompted);
+ }
}
diff --git a/host/tests/fixtures/instructions_agent.py b/host/tests/fixtures/instructions_agent.py
index 20814e1f..3895cf75 100644
--- a/host/tests/fixtures/instructions_agent.py
+++ b/host/tests/fixtures/instructions_agent.py
@@ -1,12 +1,13 @@
"""ACP fixture recording how a bot's instructions and compactions reach it.
`standard` advertises ACP `systemPrompt`; `reporting` reports compactions with `compaction_update`; `silent` only
-shows them in `usage_update`; `codex` and `claude` answer `initialize` the way those adapters do, so the host asks
+shows them in `usage_update`; `flaky` resumes sessions and fails its first prompt ever (across restarts), as a rate limit does; `codex` and `claude` answer `initialize` the way those adapters do, so the host asks
them to compact. A prompt containing COMPACT_NOW, or one that is a `/compact` command, compacts; one containing
`USAGE <n>` (optionally `SIZE <m>`) reports that much context in use. Every request is appended to
`requests.jsonl` in the working directory.
"""
import json
+import os
import re
import sys
@@ -42,7 +43,7 @@ for line in sys.stdin:
with open("requests.jsonl", "a", encoding="utf-8") as log:
log.write(json.dumps({"method": method, "params": params}) + "\n")
if method == "initialize":
- capabilities = {"loadSession": mode == "claude", "sessionCapabilities": {}}
+ capabilities = {"loadSession": mode in ("claude", "flaky"), "sessionCapabilities": {}}
if mode == "standard":
capabilities["sessionCapabilities"]["systemPrompt"] = {}
if mode == "claude":
@@ -58,6 +59,10 @@ for line in sys.stdin:
elif method == "session/prompt":
session = params["sessionId"]
text = params["prompt"][0]["text"]
+ if mode == "flaky" and not os.path.exists("failed"):
+ open("failed", "w").close()
+ send({"id": request["id"], "error": {"code": -32000, "message": "rate limited"}})
+ continue
if text.startswith("/compact") or ("COMPACT_NOW" in text and mode != "silent"):
compact(session)
if text.startswith("/compact"):
diff --git a/host/tests/memory_e2e.rs b/host/tests/memory_e2e.rs
index 8b6cb15c..1d947646 100644
--- a/host/tests/memory_e2e.rs
+++ b/host/tests/memory_e2e.rs
@@ -351,3 +351,33 @@ async fn instructions_reach_every_harness_and_survive_compaction() {
assert!(!prompts[2].contains("<bot-profile>"), "a small fall isn't a compaction");
assert!(prompts[4].contains(profile_again), "{}", prompts[4]);
}
+
+/// A first prompt that failed may never have reached the agent: its retry (even after a host restart) still
+/// carries the instructions.
+async fn retry_after_failed_first_prompt(restart: bool) {
+ let mut host = Host::start().await;
+ let bot = host.fixture_bot("Flaky", "instructions_agent.py", "flaky").await;
+ host.call("send", json!({"botId": bot, "text": "first"})).await;
+ let deadline = Instant::now() + Duration::from_secs(30);
+ while !host.call("history", json!({"botId": bot})).await.to_string().contains("The agent failed") {
+ assert!(Instant::now() < deadline, "the first prompt never failed");
+ tokio::time::sleep(Duration::from_millis(50)).await;
+ }
+ if restart {
+ host.restart(|_| {}).await;
+ }
+ host.reply(&bot, "second").await;
+ let prompts = host.prompts(&bot);
+ assert_eq!(prompts.len(), 2, "{prompts:?}");
+ assert!(prompts[0].contains("<bot-profile>") && prompts[1].contains("<bot-profile>"), "{prompts:?}");
+}
+
+#[tokio::test]
+async fn a_retry_after_a_failed_first_prompt_still_carries_the_instructions() {
+ retry_after_failed_first_prompt(false).await;
+}
+
+#[tokio::test]
+async fn a_retry_after_a_restart_still_carries_the_instructions() {
+ retry_after_failed_first_prompt(true).await;
+}