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
feat(host): give every harness the bot's instructions and keep them after compaction
leepokaid2a696e5cb71
diff --git a/docs/features/context-and-memory.md b/docs/features/context-and-memory.mdindex 284f73a0..b962c6a4 100644--- a/docs/features/context-and-memory.md+++ b/docs/features/context-and-memory.md@@ -22,12 +22,17 @@ its own context and compacts it itself.lives with the tool (MCP server instructions, tool descriptions), not here. They're stored as a snapshot in `kv` under `context.<bot>`, keyed by **session + compactionepoch**, and don't change until one of those changes. This keeps the prompt byte-identical so the promptcache stays warm.-- **Claude** (`agentCapabilities._meta.claudeCode` present) receives the snapshot as a real system prompt:- `_meta.systemPrompt.append` on `session/new` and `session/load`. It survives compaction.-- **Other harnesses** receive it in the first message of each session (`<bot-profile>…</bot-profile>`).+- How the snapshot reaches the agent (`Instructions` in `agent/bot`):+ - **Claude** (`agentCapabilities._meta.claudeCode` present) receives it as a real system prompt:+ `_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>`),+ 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>`. The next turn- re-renders the snapshot, and Claude's session is loaded again with the new system prompt. The adapter+ `compaction_update` (deduplicated by `compactionId`) increments `context.epoch.<bot>`; for an agent that has+ never sent one, so does its `usage_update.used` falling below half of a previous value of at least 20k. The+ next turn re-renders the snapshot, and Claude's session is loaded again with the new system prompt. The adapterrebuilds its query and resumes the same conversation.- **Profile edits mid-session** (name, description, skills) are sent once as an `<agent_profile_update>` blockappended to the next message. After that turn reaches the agent, the change is recorded in@@ -88,8 +93,8 @@ the `codync-mem` crate; `chat/memory` runs them in the host.## Limits-- Harnesses other than Claude have no system prompt channel and don't report compaction. For them, the first- message carries the instructions and the snapshot only refreshes when a new session starts.+- An agent without ACP `systemPrompt` gets the instructions as a user message, which models follow less firmly+ than system text. Memory a bot or dreaming wrote reaches it after its next compaction or in a new session.- Compaction is the harness's own. Codync can't choose what survives it, only re-apply its instructionsafterwards.- A session a harness has deleted (for example, one cleaned up after a month) can't be resumed. The botdiff --git a/host/src/agent/bot/mod.rs b/host/src/agent/bot/mod.rsindex 62fd230e..26c57334 100644--- a/host/src/agent/bot/mod.rs+++ b/host/src/agent/bot/mod.rs@@ -23,7 +23,7 @@ mod session;mod turn;mod updates;-pub(crate) use session::{launch_commands, select_model, start_agent};+pub(crate) use session::{launch_commands, select_model, start_agent, system_prompt};use crate::agent::acp::{Acp, Incoming};use crate::agent::config::FastModeSupport;@@ -35,7 +35,7 @@ use crate::store::{BotConfig, Entry, EntryKind, Lane, now_ms};use anyhow::{Result, anyhow};use serde::Serialize;use serde_json::{Value, json};-use std::collections::{HashMap, VecDeque};+use std::collections::{HashMap, HashSet, VecDeque};use std::sync::Arc;use std::time::{Duration, Instant};use tokio::sync::mpsc;@@ -133,6 +133,7 @@ pub fn spawn(hub: Arc<Hub>, cfg: BotConfig) -> BotHandle {routine_timed_out: false,routine_completion: None,session_fresh: false,+ profile_resend: HashSet::new(),applied_system: HashMap::new(),dreamer,turn_text: None,@@ -166,6 +167,8 @@ fn activity(title: &str) -> &str {}/// What the agent can do with sessions besides creating them.+// Each flag is one capability the agent advertised at `initialize`, independent of the others.+#[allow(clippy::struct_excessive_bools)]#[derive(Clone, Copy)]struct SessionCaps {/// `session/load`: resume a session after a restart.@@ -174,6 +177,19 @@ struct SessionCaps {fork: bool,/// `session/close`: an idle session can be released while the agent keeps running.close: bool,+ /// ACP `systemPrompt`: instructions on `session/new`, `load` and `fork` that the agent keeps at system level.+ system_prompt: bool,+}++/// How the bot's instructions (identity and memory) reach the agent.+#[derive(Clone, Copy, PartialEq, Eq, Debug)]+pub(crate) enum Instructions {+ /// Claude's adapter: `_meta.systemPrompt`, swapped in by loading the session again.+ ClaudeMeta,+ /// ACP `systemPrompt` (append), which the agent keeps across compaction.+ SystemPrompt,+ /// A `<bot-profile>` block before the first message, sent again after a compaction.+ FirstMessage,}pub(crate) struct Conn {@@ -187,6 +203,23 @@ pub(crate) struct Conn {started: Instant,/// 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.+ reports_compaction: bool,+ /// Each session's context use at its last `usage_update`.+ used: HashMap<String, u64>,+}++impl Conn {+ pub(crate) fn instructions(&self) -> Instructions {+ // Claude's adapter rebuilds the standard field without the `_meta` object's options, so Claude stays on it.+ if self.claude {+ Instructions::ClaudeMeta+ } else if self.sessions.system_prompt {+ Instructions::SystemPrompt+ } else {+ Instructions::FirstMessage+ }+ }}#[derive(Clone, Copy, PartialEq)]@@ -223,6 +256,9 @@ struct Actor {routine_completion: Option<RoutineCompletion>,/// True until the first prompt of a new ACP session has been sent.session_fresh: bool,+ /// Sessions that compacted away the `<bot-profile>` their first message carried; their next prompt carries+ /// a fresh one.+ profile_resend: HashSet<String>,/// Session -> the instructions this process last handed Claude as its system prompt.applied_system: HashMap<String, String>,dreamer: mpsc::UnboundedSender<memory::DreamEvent>,diff --git a/host/src/agent/bot/session.rs b/host/src/agent/bot/session.rsindex bd8a2ce4..1b2948c6 100644--- a/host/src/agent/bot/session.rs+++ b/host/src/agent/bot/session.rs@@ -1,7 +1,7 @@//! The agent process and its ACP sessions: launch, handshake, new/load/fork, thread sessions.use super::fast_mode::Listing;-use super::{Actor, Conn, NoticeStyle, SessionCaps};+use super::{Actor, Conn, Instructions, NoticeStyle, SessionCaps};use crate::agent::acp::{self, Acp, Incoming};use crate::chat::context;use crate::store::{BotConfig, Entry, EntryKind, Lane, lane_key};@@ -181,7 +181,7 @@ impl Actor {}}let servers = self.mcp_servers()?;- let claude = self.conn.as_ref().is_some_and(|c| c.claude);+ let instructions = self.conn.as_ref().map_or(Instructions::FirstMessage, Conn::instructions);let mut forked = false;if let Some(root) = slot&& self.session_for(slot).is_none()@@ -189,16 +189,16 @@ impl Actor {&& self.conn.as_ref().is_some_and(|c| c.sessions.fork){self.hub.set_runtime(&self.cfg.id, |r| r.activity = "Opening the thread…".into());- let res = self- .session_request("session/fork", {- let mut params = json!({"sessionId": main, "cwd": self.cfg.cwd, "mcpServers": servers});- if claude {- params["_meta"] = json!({"claudeCode": claude_options(&self.cfg)});- }- params- })- .await- .ok();+ let mut params = json!({"sessionId": main, "cwd": self.cfg.cwd, "mcpServers": servers});+ match instructions {+ Instructions::ClaudeMeta => params["_meta"] = json!({"claudeCode": claude_options(&self.cfg)}),+ // The fork keeps the instructions its main session runs with.+ Instructions::SystemPrompt => {+ params["systemPrompt"] = system_prompt(&self.snapshot(&main).await?.system);+ }+ Instructions::FirstMessage => {}+ }+ let res = self.session_request("session/fork", params).await.ok();if let Some(res) = res&& let Some(sid) = res["sessionId"].as_str().map(str::to_owned)&& let Some(conn) = self.conn.as_mut()@@ -218,8 +218,8 @@ impl Actor {// re-rendered them, loading the session again swaps the prompt in (the adapter// rebuilds its query and resumes the same conversation).let current = self.session_for(slot);- let snapshot = match (¤t, claude) {- (Some(sid), true) => Some(self.snapshot(sid).await?),+ let snapshot = match (¤t, instructions) {+ (Some(sid), Instructions::ClaudeMeta) => Some(self.snapshot(sid).await?),_ => None,};let Some(conn) = self.conn.as_ref() else { bail!("agent not running") };@@ -243,6 +243,9 @@ impl Actor {if let Some(s) = &snapshot {params["_meta"] = claude_meta(&self.cfg, &s.system);}+ if instructions == Instructions::SystemPrompt {+ params["systemPrompt"] = system_prompt(&self.snapshot(&sid).await?.system);+ }let res = self.session_request("session/load", params).await;let Some(conn) = self.conn.as_mut() else { return res.map(drop) };// The agent replays history as session/update before answering; drop it.@@ -282,8 +285,10 @@ impl Actor {tokio::task::spawn_blocking(move || context::render(&hub.store, &cfg)).await?};let mut params = json!({"cwd": self.cfg.cwd, "mcpServers": servers});- if claude {- params["_meta"] = claude_meta(&self.cfg, &system);+ match instructions {+ Instructions::ClaudeMeta => params["_meta"] = claude_meta(&self.cfg, &system),+ Instructions::SystemPrompt => params["systemPrompt"] = system_prompt(&system),+ Instructions::FirstMessage => {}}let res = self.session_request("session/new", params).await?;let Some(conn) = self.conn.as_mut() else { bail!("agent not running") };@@ -298,7 +303,7 @@ impl Actor {self.thread_intro = Some(self.thread_intro(root, false));}context::adopt(&self.hub.store, &self.cfg, &sid, system.clone())?;- if claude {+ if instructions == Instructions::ClaudeMeta {self.applied_system.insert(sid, system);}Ok(())@@ -327,6 +332,11 @@ impl Actor {/// How long `session/new`, `session/load` and `session/fork` may take.const SESSION_TIMEOUT: Duration = Duration::from_secs(180);+/// ACP's `systemPrompt`: the bot's instructions added to the agent's own.+pub(crate) fn system_prompt(system: &str) -> Value {+ json!({"mode": "append", "content": [{"type": "text", "text": system}]})+}+/// Claude's `_meta`: the bot's instructions appended to Claude Code's own system prompt, and its options.fn claude_meta(cfg: &BotConfig, system: &str) -> Value {json!({"systemPrompt": {"append": system}, "claudeCode": claude_options(cfg)})@@ -439,6 +449,7 @@ pub(crate) async fn start_agent(command: &str, cwd: &str, env: &[(String, Stringload: init["agentCapabilities"]["loadSession"].as_bool().unwrap_or(false),fork: init["agentCapabilities"]["sessionCapabilities"]["fork"].is_object(),close: init["agentCapabilities"]["sessionCapabilities"]["close"].is_object(),+ system_prompt: init["agentCapabilities"]["sessionCapabilities"]["systemPrompt"].is_object(),};let claude = init["agentCapabilities"]["_meta"]["claudeCode"].is_object();Ok(Conn {@@ -449,6 +460,8 @@ pub(crate) async fn start_agent(command: &str, cwd: &str, env: &[(String, Stringloaded: HashMap::new(),started: Instant::now(),fast_options: HashMap::new(),+ reports_compaction: false,+ used: HashMap::new(),})}diff --git a/host/src/agent/bot/turn.rs b/host/src/agent/bot/turn.rsindex 8a17cd14..7c2056a8 100644--- a/host/src/agent/bot/turn.rs+++ b/host/src/agent/bot/turn.rs@@ -1,6 +1,6 @@//! Starting a turn and wrapping it up: final reply, notices, replies to waiters, alerts and memory.-use super::{Actor, Done, NoticeStyle, Queued, Seg};+use super::{Actor, Conn, Done, Instructions, NoticeStyle, Queued, Seg};use crate::agent::acp::{self, Incoming};use crate::analytics::{self, TurnOutcome, TurnSource};use crate::chat::context::{self, Snapshot};@@ -13,6 +13,9 @@ use serde_json::json;use std::time::{Duration, Instant};use tokio::sync::mpsc;+/// Leads a `<bot-profile>` sent again after a compaction, so the agent drops an older copy it may still hold.+const PROFILE_AGAIN: &str = "Your context was summarized. This is your full, current profile and memory; it replaces any earlier bot profile in this conversation.";+impl Actor {/// This session's frozen instructions (reads memory files when re-rendering).pub(super) async fn snapshot(&self, session: &str) -> Result<Snapshot> {@@ -120,13 +123,15 @@ impl Actor {}}}- let claude = self.conn.as_ref().is_some_and(|c| c.claude);+ let instructions = self.conn.as_ref().map_or(Instructions::FirstMessage, Conn::instructions);let sid = self.turn_session.clone().ok_or_else(|| anyhow!("no session"))?;- let snapshot = self.snapshot(&sid).await?;+ let mut snapshot = self.snapshot(&sid).await?;let remind_files = !snapshot.system.contains("send_file");let mut prompt = text.to_owned();- if self.session_fresh {- // A new session's instructions already hold memory as it is now.+ // 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 {+ // The instructions about to reach the agent hold memory as it is now.let revision = memory::current_revision(&self.hub.store, &self.cfg.id);memory::mark_announced(&self.hub.store, &self.cfg.id, &sid, &revision)?;}@@ -137,12 +142,12 @@ impl Actor {if let Some(intro) = self.thread_intro.take() {prompt = format!("{intro}\n\n{prompt}");}- if self.session_fresh {- self.session_fresh = false;- // Claude already has the instructions as its system prompt.- if !claude {- prompt = format!("<bot-profile>\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);- }+ if std::mem::take(&mut self.session_fresh) && 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())?;+ snapshot.announced = snapshot.system_identity.clone();+ prompt = format!("<bot-profile>\n{PROFILE_AGAIN}\n\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);}if let Some((update, identity)) = context::profile_update(&self.hub.store, &snapshot, &self.cfg) {prompt = format!("{prompt}\n\n{update}");diff --git a/host/src/agent/bot/updates.rs b/host/src/agent/bot/updates.rsindex 36b7c562..ccb06863 100644--- a/host/src/agent/bot/updates.rs+++ b/host/src/agent/bot/updates.rs@@ -1,7 +1,7 @@//! Agent → client: `session/update`s, permission requests and streamed text mapped onto entries.use super::fast_mode::Listing;-use super::{Actor, NoticeStyle, Seg, SegKind};+use super::{Actor, Instructions, NoticeStyle, Seg, SegKind};use crate::agent::acp::{self, Incoming};use crate::chat::context;use crate::hub::BotStatus;@@ -48,6 +48,35 @@ impl Actor {}}+ /// The agent summarized its context: the next turn gets freshly rendered instructions and memory (see+ /// `context`), and a harness that got them in its first message gets them again.+ fn compacted(&mut self, id: &str) {+ let Some(sid) = self.turn_session.clone() else { return };+ match context::bump_epoch(&self.hub.store, &self.cfg.id, &sid, id) {+ Ok(true) => {+ if self.conn.as_ref().is_some_and(|c| c.instructions() == Instructions::FirstMessage) {+ self.profile_resend.insert(sid);+ }+ let _ = self.dreamer.send(crate::chat::memory::DreamEvent::Flush);+ self.notice("Earlier context was summarized to make room.", NoticeStyle::Info);+ }+ Ok(false) => {}+ Err(error) => {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the compaction");+ }+ }+ }++ /// An agent that doesn't report compactions still shows them in its usage.+ fn track_usage(&mut self, used: u64) {+ let Some(sid) = self.turn_session.clone() else { return };+ let Some(conn) = self.conn.as_mut() else { return };+ let before = conn.used.insert(sid, used);+ if !conn.reports_compaction && compacted_by_usage(before, used) {+ self.compacted(&format!("usage-{}", crate::store::now_ms()));+ }+ }+pub(super) fn on_update(&mut self, u: &Value) {let turn = self.turn.unwrap_or(0);match u["sessionUpdate"].as_str().unwrap_or_default() {@@ -100,20 +129,12 @@ impl Actor {self.set_activity(super::activity(&title));}}- // The agent summarized its context: the next turn gets freshly rendered- // instructions and memory (see `context`).- "compaction_update" if u["status"] == "completed" => {- let Some(sid) = &self.turn_session else { return };- let id = u["compactionId"].as_str().unwrap_or_default();- match context::bump_epoch(&self.hub.store, &self.cfg.id, sid, id) {- Ok(true) => {- let _ = self.dreamer.send(crate::chat::memory::DreamEvent::Flush);- self.notice("Earlier context was summarized to make room.", NoticeStyle::Info);- }- Ok(false) => {}- Err(error) => {- tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the compaction");- }+ "compaction_update" => {+ if let Some(conn) = self.conn.as_mut() {+ conn.reports_compaction = true;+ }+ if u["status"] == "completed" {+ self.compacted(u["compactionId"].as_str().unwrap_or_default());}}"usage_update" => {@@ -121,6 +142,9 @@ impl Actor {if info.is_object() {crate::usage::ingest_claude_rate_limit(&self.hub, info);}+ if let Some(used) = u["used"].as_u64() {+ self.track_usage(used);+ }}"plan" => {let data = json!({"entries": u["entries"].clone(), "author": self.cfg.id});@@ -354,10 +378,26 @@ pub fn diff_summary(path: &str, old: &str, new: &str) -> Value {})}+/// Context use below which a fall isn't read as a compaction: a short session has little to summarize.+const COMPACTED_FROM: u64 = 20_000;++/// Whether a session's context use fell the way it does when the agent summarizes it: by more than half.+fn compacted_by_usage(before: Option<u64>, used: u64) -> bool {+ before.is_some_and(|before| before >= COMPACTED_FROM && used < before / 2)+}+#[cfg(test)]mod tests {use super::*;+ #[test]+ fn a_halved_context_reads_as_a_compaction() {+ assert!(compacted_by_usage(Some(90_000), 12_000));+ assert!(!compacted_by_usage(Some(90_000), 60_000), "ordinary churn");+ assert!(!compacted_by_usage(Some(8_000), 1_000), "too little to summarize");+ assert!(!compacted_by_usage(None, 1_000), "the session's first report");+ }+#[test]fn diff_counts_changed_middle() {let d = diff_summary("f", "a\nb\nc\n", "a\nB\nB2\nc\n");diff --git a/host/src/chat/oneshot.rs b/host/src/chat/oneshot.rsindex f91d2215..9dd32254 100644--- a/host/src/chat/oneshot.rs+++ b/host/src/chat/oneshot.rs@@ -1,6 +1,7 @@//! Background jobs on the bot's own harness (dreaming, automatic naming): one prompt to a throwaway agent.use crate::agent::acp::{self, Acp, Incoming};+use crate::agent::bot::Instructions;use crate::hub::Hub;use crate::store::BotConfig;use anyhow::{Result, anyhow, bail};@@ -36,14 +37,19 @@ pub async fn one_shot(hub: &Arc<Hub>, cfg: &BotConfig, system: &str, user: &str)let acp = conn.acp.clone();let result = async {let mut params = json!({"cwd": cwd, "mcpServers": []});- let text = if conn.claude {- params["_meta"] = json!({- "systemPrompt": system,- "claudeCode": {"options": {"tools": [], "persistSession": false, "settingSources": [], "model": "haiku"}},- });- user.to_owned()- } else {- format!("{system}\n\n---\n\n{user}")+ let text = match conn.instructions() {+ Instructions::ClaudeMeta => {+ params["_meta"] = json!({+ "systemPrompt": system,+ "claudeCode": {"options": {"tools": [], "persistSession": false, "settingSources": [], "model": "haiku"}},+ });+ user.to_owned()+ }+ Instructions::SystemPrompt => {+ params["systemPrompt"] = crate::agent::bot::system_prompt(system);+ user.to_owned()+ }+ Instructions::FirstMessage => format!("{system}\n\n---\n\n{user}"),};let res = acp.request("session/new", params).await?;let sid = res["sessionId"].as_str().ok_or_else(|| anyhow!("agent returned no sessionId"))?.to_owned();diff --git a/host/tests/fixtures/instructions_agent.py b/host/tests/fixtures/instructions_agent.pynew file mode 100644index 00000000..e353e954--- /dev/null+++ b/host/tests/fixtures/instructions_agent.py@@ -0,0 +1,51 @@+"""ACP fixture recording how a bot's instructions reach it.++`standard` advertises ACP `systemPrompt`; `reporting` reports compactions with `compaction_update`; `silent` only+shows them in `usage_update`. A prompt containing COMPACT_NOW compacts, one containing `USAGE <n>` reports that+much context in use. Every request is appended to `requests.jsonl` in the working directory.+"""+import json+import re+import sys++mode = sys.argv[1] if len(sys.argv) > 1 else "reporting"+sessions = 0+++def send(value):+ print(json.dumps({"jsonrpc": "2.0", **value}), flush=True)+++def update(session, value):+ send({"method": "session/update", "params": {"sessionId": session, "update": value}})+++for line in sys.stdin:+ request = json.loads(line)+ method = request.get("method")+ params = request.get("params", {})+ with open("requests.jsonl", "a", encoding="utf-8") as log:+ log.write(json.dumps({"method": method, "params": params}) + "\n")+ if method == "initialize":+ capabilities = {"loadSession": False, "sessionCapabilities": {}}+ if mode == "standard":+ capabilities["sessionCapabilities"]["systemPrompt"] = {}+ result = {"protocolVersion": 1, "agentCapabilities": capabilities}+ elif method == "session/new":+ sessions += 1+ result = {"sessionId": f"s{sessions}"}+ elif method == "session/prompt":+ session = params["sessionId"]+ text = params["prompt"][0]["text"]+ if "COMPACT_NOW" in text and mode != "silent":+ update(session, {"sessionUpdate": "compaction_update", "compactionId": "c1", "status": "in_progress"})+ update(session, {"sessionUpdate": "compaction_update", "compactionId": "c1", "status": "completed"})+ usage = re.search(r"USAGE (\d+)", text)+ if usage:+ update(session, {"sessionUpdate": "usage_update", "used": int(usage.group(1)), "size": 200000})+ update(session, {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "ok"}})+ result = {"stopReason": "end_turn"}+ else:+ result = {}+ if "id" in request:+ send({"id": request["id"], "result": result})diff --git a/host/tests/memory_e2e.rs b/host/tests/memory_e2e.rsindex b1c5229e..9c24e155 100644--- a/host/tests/memory_e2e.rs+++ b/host/tests/memory_e2e.rs@@ -1,5 +1,5 @@//! Bot memory on a real host with disposable data: moving the old memory, the user's edits, a bot's writes by-//! its permission, and dreaming.+//! its permission, dreaming, and how the instructions holding memory reach each kind of harness.#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]@@ -88,6 +88,52 @@ impl Host {self.refused("memoryCall", json!({"botId": bot, "name": name, "arguments": arguments})).await}+ /// A bot running `tests/fixtures/<fixture>` with `args`.+ async fn fixture_bot(&self, name: &str, fixture: &str, args: &str) -> String {+ let script = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(fixture);+ let command = format!("{} {args}", common::python_agent(&script));+ let bot = json!({"name": name, "backend": "custom", "command": command, "connectors": [], "notify": false});+ self.call("createBot", bot).await["bot"]["id"].as_str().unwrap().to_owned()+ }++ /// Sends `text` and waits for the bot's reply.+ async fn reply(&self, bot: &str, text: &str) {+ let finals = |history: &Value| {+ history["entries"]+ .as_array()+ .unwrap()+ .iter()+ .filter(|e| e["kind"] == "agent" && e["data"]["final"] == true)+ .count()+ };+ let before = finals(&self.call("history", json!({"botId": bot})).await);+ let nonce = uuid::Uuid::new_v4().to_string();+ self.call("send", json!({"botId": bot, "text": text, "clientNonce": nonce})).await;+ let deadline = Instant::now() + Duration::from_secs(30);+ loop {+ let history = self.call("history", json!({"botId": bot})).await;+ if finals(&history) > before {+ return;+ }+ assert!(Instant::now() < deadline, "{bot} didn't answer {text:?}: {history}");+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ }++ /// The requests `instructions_agent.py` logged in a bot's workspace.+ fn requests(&self, bot: &str, method: &str) -> Vec<Value> {+ let log = std::fs::read_to_string(self.home.join("bots").join(bot).join("workspace/requests.jsonl")).unwrap();+ log.lines().map(|l| serde_json::from_str::<Value>(l).unwrap()).filter(|r| r["method"] == method).collect()+ }++ /// The text of every prompt a bot's agent got, oldest first.+ fn prompts(&self, bot: &str) -> Vec<String> {+ self.requests(bot, "session/prompt")+ .iter()+ .map(|r| r["params"]["prompt"][0]["text"].as_str().unwrap().to_owned())+ .collect()+ }+/// The newest memory line in a bot's chat.async fn card(&self, bot: &str) -> Value {let history = self.call("history", json!({"botId": bot})).await;@@ -275,3 +321,43 @@ async fn dreaming_keeps_only_what_the_user_typed_and_can_be_undone() {assert_eq!(view["bot"]["entries"], json!([]));assert_eq!(view["dreams"][0]["undone"], true);}++#[tokio::test]+async fn instructions_reach_every_harness_and_survive_compaction() {+ let host = Host::start().await;+ let profile_again = "replaces any earlier bot profile";++ // An agent with ACP's systemPrompt gets the instructions there, not in a message.+ let standard = host.fixture_bot("Standard", "instructions_agent.py", "standard").await;+ host.call("saveMemory", json!({"botId": standard, "text": "Allergic to peanuts"})).await;+ host.reply(&standard, "hello").await;+ let new = &host.requests(&standard, "session/new")[0]["params"];+ assert_eq!(new["systemPrompt"]["mode"], "append");+ let system = new["systemPrompt"]["content"][0]["text"].as_str().unwrap();+ assert!(system.contains("Standard") && system.contains("Allergic to peanuts"), "{system}");+ assert!(!host.prompts(&standard)[0].contains("<bot-profile>"));++ // Others get it in the first message, and again once after the agent reports a compaction.+ let reporting = host.fixture_bot("Reporting", "instructions_agent.py", "reporting").await;+ host.reply(&reporting, "hello").await;+ host.reply(&reporting, "COMPACT_NOW").await;+ host.reply(&reporting, "after").await;+ host.reply(&reporting, "later").await;+ assert!(host.requests(&reporting, "session/new")[0]["params"].get("systemPrompt").is_none());+ let prompts = host.prompts(&reporting);+ assert!(prompts[0].contains("<bot-profile>") && !prompts[0].contains(profile_again), "{}", prompts[0]);+ assert!(!prompts[1].contains("<bot-profile>"));+ assert!(prompts[2].contains("<bot-profile>") && prompts[2].contains(profile_again), "{}", prompts[2]);+ assert!(!prompts[3].contains("<bot-profile>"), "only once");++ // An agent that doesn't report compactions shows them as its context use halving.+ let silent = host.fixture_bot("Silent", "instructions_agent.py", "silent").await;+ host.reply(&silent, "USAGE 90000").await;+ host.reply(&silent, "USAGE 70000").await;+ host.reply(&silent, "steady").await;+ host.reply(&silent, "USAGE 9000").await;+ host.reply(&silent, "after").await;+ let prompts = host.prompts(&silent);+ assert!(!prompts[2].contains("<bot-profile>"), "a small fall isn't a compaction");+ assert!(prompts[4].contains(profile_again), "{}", prompts[4]);+}