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 every session about memory changes without reloading it

leepokaida52873725a6
diff --git a/docs/architecture/file-structure.md b/docs/architecture/file-structure.md
index ffd47515..7618a1ff 100644
--- a/docs/architecture/file-structure.md
+++ b/docs/architecture/file-structure.md
@@ -82,7 +82,7 @@ Codync/
| CLI and background service | `host/src/main.rs`, `service/` |
| API, caller permissions, event ordering | `host/src/api/`, `hub.rs` |
| SQLite transcript, bots, lanes and sessions | `host/src/store/` (`entries.rs`, `bots.rs`, `model.rs`, `devices.rs`, `schema.rs`) |
-| Agent process, ACP, queue and session lifecycle | `host/src/agent/bot/` (`queue.rs`, `session.rs`, `turn.rs`, `updates.rs`), `acp.rs` |
+| Agent process, ACP, queue and session lifecycle | `host/src/agent/bot/` (`launch.rs`, `queue.rs`, `session.rs`, `turn.rs`, `updates.rs`), `acp.rs` |
| Group room turns / bot-to-bot requests | `host/src/chat/group.rs` / `team.rs` |
| Prompt snapshots and memory | `host/src/chat/context.rs`, `memory/` (`api.rs`, `tools.rs`, `notices.rs`, `dream.rs`, `import.rs`); the rules in `host/crates/memory` (`codync-mem`); harnesses' own memory turned off in `host/src/agent/own_memory.rs` |
| Identity, encryption and direct channel | `host/src/remote/identity.rs`, `crypto.rs`, `channel.rs` |
diff --git a/docs/design/bot-memory.md b/docs/design/bot-memory.md
index b8ee2595..1c1222e3 100644
--- a/docs/design/bot-memory.md
+++ b/docs/design/bot-memory.md
@@ -17,8 +17,8 @@ Built differently from the plan below, for now:
don't feed dreaming.
- Entries show who wrote them, not a link to the message they were learned from. The bot can follow it:
`search_memory` returns each entry's source messages, and `search_history` reads back from a message id.
-- Until M2, a change the user makes still re-renders the opening (and reloads a Claude session), and the
- notice carries the whole memory rather than a delta.
+- A memory notice carries the whole memory (at most 1,600 tokens) rather than a delta: a forgotten entry leaves
+ no row to diff against, and a whole copy also replaces whatever an earlier notice said.
In one line: **memory records the person, the transcript stays in the host, the opening stays frozen until
compaction, and the host decides when to compact.**
diff --git a/docs/features/context-and-memory.md b/docs/features/context-and-memory.md
index 86db61fd..2b65e9f1 100644
--- a/docs/features/context-and-memory.md
+++ b/docs/features/context-and-memory.md
@@ -48,9 +48,11 @@ the `codync-mem` crate; `chat/memory` runs them in the host.
its own workspace uses "yours" by default and one in a project folder doesn't; the user switches it per bot
(`sharedMemory`).
- **The opening** holds every active entry the bot sees, pinned first, within the caps, with ids so the bot can
- revise or archive one. Archived entries stay searchable (`search_memory`). A change the user makes reaches
- each session on its next message as a `<memory_update>` with the whole memory; what a bot or dreaming wrote
- reaches other sessions at their next compaction or new session.
+ revise or archive one. Archived entries stay searchable (`search_memory`). The opening doesn't change for a
+ memory change, so the prompt cache holds: every change (the user's, a bot's, dreaming's) reaches each session of
+ the bots that see it on its next message as a `<memory_update>` with the whole memory. The session whose own
+ tool call made the change isn't told. A ledger in `kv` keeps, per session, the revision its opening holds and
+ the one it has heard; after a compaction the second falls back to the first, so whatever is newer is told again.
- **The bot's tools** (built-in `memory` MCP server): `remember`, `revise_memory`, `archive_memory`,
`search_memory`, `search_history`. A bot on automatic approval writes at once, in any turn and to either layer;
the chat shows a "Remembered" line the user can undo. A bot that asks first shows a card the user approves or
diff --git a/host/src/agent/bot/launch.rs b/host/src/agent/bot/launch.rs
new file mode 100644
index 00000000..104b7807
--- /dev/null
+++ b/host/src/agent/bot/launch.rs
@@ -0,0 +1,171 @@
+//! The agent process: which commands start it, the ACP handshake, the bot's model, and how its instructions are
+//! attached to a session request.
+
+use super::{Conn, SessionCaps};
+use crate::agent::acp::{self, Acp};
+use crate::store::BotConfig;
+use anyhow::{Result, anyhow};
+use serde_json::{Value, json};
+use std::collections::HashMap;
+use std::time::{Duration, Instant};
+
+/// 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. The
+/// per-session parts of Claude Code's prompt (working directory, git status) move to the first message, so a
+/// bot's chat, threads and routines share one cached system prompt.
+pub(super) fn claude_meta(cfg: &BotConfig, system: &str) -> Value {
+ json!({"systemPrompt": {"append": system, "excludeDynamicSections": true}, "claudeCode": claude_options(cfg)})
+}
+
+/// Claude Code's own memory stays off where Codync's is the bot's only one (a bot in its own workspace); in a
+/// project folder it is the project's working memory and stays on. Its auto-dream rewrites memory with no log, so
+/// it is off everywhere ([design](../../../../docs/design/bot-memory.md#claude-codes-own-memory)).
+fn claude_options(cfg: &BotConfig) -> Value {
+ let mut settings = json!({"autoDreamEnabled": false});
+ if crate::agent::workspace::is_managed(cfg) {
+ settings["autoMemoryEnabled"] = false.into();
+ }
+ json!({"options": {"settings": settings}})
+}
+
+/// Shell commands that may start the bot's agent over ACP, best first.
+/// `progress` receives short status lines ("Downloading Cursor…").
+pub(crate) async fn launch_commands(cfg: &BotConfig, progress: impl Fn(&str)) -> Result<Vec<String>> {
+ Ok(match cfg.command.as_deref().map(str::trim) {
+ Some(c) if !c.is_empty() => vec![c.to_owned()],
+ _ => crate::agent::backends::launch_candidates(&cfg.backend, progress)
+ .await?
+ .iter()
+ .map(crate::agent::registry::Cmd::acp)
+ .collect(),
+ })
+}
+
+/// Switches a session (`session/new` or `session/load` answered `res`) to the bot's
+/// chosen model, then sets that model's options (effort, fast…). Returns the answer holding
+/// the session's config options now: a model can change them.
+pub(crate) async fn select_model(acp: &Acp, res: &Value, sid: &str, cfg: &BotConfig) -> Result<Value> {
+ let Some(model) = cfg.model.as_deref().filter(|m| !m.is_empty()) else { return Ok(res.clone()) };
+ let Some(config_id) = crate::agent::auth::model_config(res).and_then(|c| c["id"].as_str()) else {
+ let r = acp.request("session/set_model", json!({"sessionId": sid, "modelId": model})).await;
+ return r.map(|_| res.clone()).map_err(|e| model_error(model, &e));
+ };
+ let (model, mut options) = split_variant(res, model);
+ options.extend(cfg.model_options.iter().map(|(k, v)| (k.as_str(), v.as_str())));
+ let set = |id: &str, value: &str| {
+ acp.request("session/set_config_option", json!({"sessionId": sid, "configId": id, "value": value}))
+ };
+ let mut current = set(config_id, model).await.map_err(|e| model_error(model, &e))?;
+ for (id, value) in options {
+ let answer = set(id, value).await.map_err(|e| model_error(&format!("{model} with {id}={value}"), &e))?;
+ if answer["configOptions"].is_array() {
+ current = answer;
+ }
+ }
+ Ok(if current["configOptions"].is_array() { current } else { res.clone() })
+}
+
+fn model_error(model: &str, e: &anyhow::Error) -> anyhow::Error {
+ tracing::warn!(model, error = format!("{e:#}"), "couldn't set model");
+ anyhow!("Couldn't select model {model}: {e:#}")
+}
+
+/// Cursor names a model variant `grok-4.7[reasoning_effort=high,fast=true]`. When the agent
+/// lists plain models instead, the bracket becomes options of the plain model.
+fn split_variant<'a>(res: &Value, model: &'a str) -> (&'a str, Vec<(&'a str, &'a str)>) {
+ let listed = |id: &str| crate::agent::auth::model_ids(res).iter().any(|m| m == id);
+ if let Some((name, params)) = model.strip_suffix(']').and_then(|m| m.split_once('['))
+ && let Some(params) =
+ params.split(',').filter(|p| !p.is_empty()).map(|p| p.split_once('=')).collect::<Option<Vec<_>>>()
+ && !listed(model)
+ && listed(name)
+ {
+ return (name, params);
+ }
+ (model, Vec::new())
+}
+
+/// Spawns an ACP agent and completes the `initialize` handshake.
+pub(crate) async fn start_agent(command: &str, cwd: &str, env: &[(String, String)], budget: Duration) -> Result<Conn> {
+ let (acp, rx) = Acp::spawn(command, cwd, env)?;
+ let init = tokio::time::timeout(
+ budget,
+ acp.request(
+ "initialize",
+ json!({
+ "protocolVersion": 1,
+ // `session.compaction`: tell us when the agent summarizes its context.
+ // `parameterizedModelPicker`: Cursor lists plain models with their options
+ // (effort, fast…) instead of one fixed variant each; model discovery asks the same.
+ "clientCapabilities": {
+ "fs": {"readTextFile": false, "writeTextFile": false},
+ "terminal": false,
+ "session": {"compaction": {}},
+ "_meta": {"parameterizedModelPicker": true},
+ },
+ "clientInfo": {"name": "codync", "title": "Codync", "version": env!("CARGO_PKG_VERSION")},
+ }),
+ ),
+ )
+ .await;
+ let init = match init {
+ Ok(Ok(v)) => v,
+ Ok(Err(e)) => {
+ acp.kill().await;
+ return Err(e);
+ }
+ Err(_) => {
+ acp.kill().await;
+ let message = format!("the agent didn't answer the ACP handshake within {}s", budget.as_secs());
+ return Err(acp::Failure::error(acp::FailureKind::Timeout, message));
+ }
+ };
+ let sessions = SessionCaps {
+ load: 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 {
+ acp,
+ rx,
+ sessions,
+ claude,
+ loaded: HashMap::new(),
+ started: Instant::now(),
+ fast_options: HashMap::new(),
+ reports_compaction: false,
+ used: HashMap::new(),
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn variant_ids_become_options_only_when_the_agent_lists_plain_models() {
+ let plain = json!({"configOptions": [{"id": "model", "category": "model", "type": "select",
+ "options": [{"value": "grok-4.7"}]}]});
+ let variant = "grok-4.7[context=256k,reasoning_effort=medium,fast=false]";
+ assert_eq!(
+ split_variant(&plain, variant),
+ ("grok-4.7", vec![("context", "256k"), ("reasoning_effort", "medium"), ("fast", "false")])
+ );
+ assert_eq!(split_variant(&plain, "grok-4.7"), ("grok-4.7", vec![]));
+ let variants = json!({"configOptions": [{"id": "model", "category": "model", "type": "select",
+ "options": [{"value": variant}]}]});
+ assert_eq!(split_variant(&variants, variant), (variant, vec![]));
+ assert_eq!(split_variant(&plain, "other[x=1]"), ("other[x=1]", vec![]));
+ // Claude's `opus[1m]` is a model id of its own, not a variant.
+ assert_eq!(split_variant(&json!({"models": {"availableModels": [{"modelId": "opus"}]}}), "opus[1m]").1, vec![]);
+ // Cursor's Auto: `default[]` with variants, `default` without.
+ let auto = json!({"models": {"availableModels": [{"modelId": "default"}]}});
+ assert_eq!(split_variant(&auto, "default[]"), ("default", vec![]));
+ }
+}
diff --git a/host/src/agent/bot/mod.rs b/host/src/agent/bot/mod.rs
index 26c57334..2fff5165 100644
--- a/host/src/agent/bot/mod.rs
+++ b/host/src/agent/bot/mod.rs
@@ -17,13 +17,14 @@
mod fast_mode;
mod files;
+mod launch;
mod lifecycle;
mod queue;
mod session;
mod turn;
mod updates;
-pub(crate) use session::{launch_commands, select_model, start_agent, system_prompt};
+pub(crate) use launch::{launch_commands, select_model, start_agent, system_prompt};
use crate::agent::acp::{Acp, Incoming};
use crate::agent::config::FastModeSupport;
diff --git a/host/src/agent/bot/queue.rs b/host/src/agent/bot/queue.rs
index 675e6307..8bf7856c 100644
--- a/host/src/agent/bot/queue.rs
+++ b/host/src/agent/bot/queue.rs
@@ -255,6 +255,7 @@ impl Actor {
r.started_at = None;
r.lane = None;
r.message = None;
+ r.session = None;
});
self.hub.team.cancel_from(&self.cfg.id);
}
diff --git a/host/src/agent/bot/session.rs b/host/src/agent/bot/session.rs
index 842fb237..1bd3f8fb 100644
--- a/host/src/agent/bot/session.rs
+++ b/host/src/agent/bot/session.rs
@@ -1,14 +1,14 @@
-//! The agent process and its ACP sessions: launch, handshake, new/load/fork, thread sessions.
+//! The bot's ACP sessions: starting its agent, new/load/fork, thread sessions.
use super::fast_mode::Listing;
-use super::{Actor, Conn, Instructions, NoticeStyle, SessionCaps};
-use crate::agent::acp::{self, Acp, Incoming};
+use super::launch::{claude_meta, launch_commands, select_model, start_agent, system_prompt};
+use super::{Actor, Conn, Instructions, NoticeStyle};
+use crate::agent::acp::{self, Incoming};
use crate::agent::own_memory;
-use crate::chat::context;
-use crate::store::{BotConfig, Entry, EntryKind, Lane, lane_key};
+use crate::chat::{context, memory};
+use crate::store::{Entry, EntryKind, Lane, lane_key};
use anyhow::{Result, anyhow, bail};
use serde_json::{Value, json};
-use std::collections::HashMap;
use std::time::{Duration, Instant};
impl Actor {
@@ -191,12 +191,11 @@ impl Actor {
{
self.hub.set_runtime(&self.cfg.id, |r| r.activity = "Opening the thread…".into());
let mut params = json!({"sessionId": main, "cwd": self.cfg.cwd, "mcpServers": servers});
+ // The fork keeps the instructions its main session runs with, so it shares its cached prompt.
+ let parent = self.snapshot(&main).await?;
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::ClaudeMeta => params["_meta"] = claude_meta(&self.cfg, &parent.system),
+ Instructions::SystemPrompt => params["systemPrompt"] = system_prompt(&parent.system),
Instructions::FirstMessage => {}
}
let res = self.session_request("session/fork", params).await.ok();
@@ -207,6 +206,11 @@ impl Actor {
conn.loaded.insert(sid.clone(), Instant::now());
self.set_session(slot, Some(&sid));
self.note_options(&sid, &res, Listing::Session);
+ context::adopt(&self.hub.store, &self.cfg, &sid, parent.system.clone(), parent.memory.clone())?;
+ memory::opened(&self.hub.store, &self.cfg.id, &sid, &parent.memory)?;
+ if instructions == Instructions::ClaudeMeta {
+ self.applied_system.insert(sid, parent.system);
+ }
forked = true;
} else {
tracing::warn!(bot = %self.cfg.id, "session/fork failed; the thread starts fresh");
@@ -267,8 +271,9 @@ impl Actor {
conn.loaded.insert(sid.clone(), Instant::now());
self.note_options(&sid, &res, Listing::Session);
if let Some(s) = snapshot {
- // Its system prompt now names the bot as rendered.
+ // Its system prompt now names the bot and holds memory as rendered.
context::mark_announced(&self.hub.store, &self.cfg.id, &s, s.system_identity.clone())?;
+ memory::opened(&self.hub.store, &self.cfg.id, &sid, &s.memory)?;
self.applied_system.insert(sid.clone(), s.system);
}
self.turn_session = Some(sid);
@@ -281,9 +286,12 @@ impl Actor {
}
}
}
- let system = {
+ let (revision, system) = {
let (hub, cfg) = (self.hub.clone(), self.cfg.clone());
- tokio::task::spawn_blocking(move || context::render(&hub.store, &cfg)).await?
+ tokio::task::spawn_blocking(move || {
+ (memory::current_revision(&hub.store, &cfg.id), context::render(&hub.store, &cfg))
+ })
+ .await?
};
let mut params = json!({"cwd": self.cfg.cwd, "mcpServers": servers});
match instructions {
@@ -303,7 +311,7 @@ impl Actor {
if let Some(root) = slot {
self.thread_intro = Some(self.thread_intro(root, false));
}
- context::adopt(&self.hub.store, &self.cfg, &sid, system.clone())?;
+ context::adopt(&self.hub.store, &self.cfg, &sid, system.clone(), revision)?;
if instructions == Instructions::ClaudeMeta {
self.applied_system.insert(sid, system);
}
@@ -332,162 +340,3 @@ 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)})
-}
-
-/// Claude Code's own memory stays off where Codync's is the bot's only one (a bot in its own workspace); in a
-/// project folder it is the project's working memory and stays on. Its auto-dream rewrites memory with no log, so
-/// it is off everywhere ([design](../../../../docs/design/bot-memory.md#claude-codes-own-memory)).
-fn claude_options(cfg: &BotConfig) -> Value {
- let mut settings = json!({"autoDreamEnabled": false});
- if crate::agent::workspace::is_managed(cfg) {
- settings["autoMemoryEnabled"] = false.into();
- }
- json!({"options": {"settings": settings}})
-}
-
-/// Shell commands that may start the bot's agent over ACP, best first.
-/// `progress` receives short status lines ("Downloading Cursor…").
-pub(crate) async fn launch_commands(cfg: &BotConfig, progress: impl Fn(&str)) -> Result<Vec<String>> {
- Ok(match cfg.command.as_deref().map(str::trim) {
- Some(c) if !c.is_empty() => vec![c.to_owned()],
- _ => crate::agent::backends::launch_candidates(&cfg.backend, progress)
- .await?
- .iter()
- .map(crate::agent::registry::Cmd::acp)
- .collect(),
- })
-}
-
-/// Switches a session (`session/new` or `session/load` answered `res`) to the bot's
-/// chosen model, then sets that model's options (effort, fast…). Returns the answer holding
-/// the session's config options now: a model can change them.
-pub(crate) async fn select_model(acp: &Acp, res: &Value, sid: &str, cfg: &BotConfig) -> Result<Value> {
- let Some(model) = cfg.model.as_deref().filter(|m| !m.is_empty()) else { return Ok(res.clone()) };
- let Some(config_id) = crate::agent::auth::model_config(res).and_then(|c| c["id"].as_str()) else {
- let r = acp.request("session/set_model", json!({"sessionId": sid, "modelId": model})).await;
- return r.map(|_| res.clone()).map_err(|e| model_error(model, &e));
- };
- let (model, mut options) = split_variant(res, model);
- options.extend(cfg.model_options.iter().map(|(k, v)| (k.as_str(), v.as_str())));
- let set = |id: &str, value: &str| {
- acp.request("session/set_config_option", json!({"sessionId": sid, "configId": id, "value": value}))
- };
- let mut current = set(config_id, model).await.map_err(|e| model_error(model, &e))?;
- for (id, value) in options {
- let answer = set(id, value).await.map_err(|e| model_error(&format!("{model} with {id}={value}"), &e))?;
- if answer["configOptions"].is_array() {
- current = answer;
- }
- }
- Ok(if current["configOptions"].is_array() { current } else { res.clone() })
-}
-
-fn model_error(model: &str, e: &anyhow::Error) -> anyhow::Error {
- tracing::warn!(model, error = format!("{e:#}"), "couldn't set model");
- anyhow!("Couldn't select model {model}: {e:#}")
-}
-
-/// Cursor names a model variant `grok-4.7[reasoning_effort=high,fast=true]`. When the agent
-/// lists plain models instead, the bracket becomes options of the plain model.
-fn split_variant<'a>(res: &Value, model: &'a str) -> (&'a str, Vec<(&'a str, &'a str)>) {
- let listed = |id: &str| crate::agent::auth::model_ids(res).iter().any(|m| m == id);
- if let Some((name, params)) = model.strip_suffix(']').and_then(|m| m.split_once('['))
- && let Some(params) =
- params.split(',').filter(|p| !p.is_empty()).map(|p| p.split_once('=')).collect::<Option<Vec<_>>>()
- && !listed(model)
- && listed(name)
- {
- return (name, params);
- }
- (model, Vec::new())
-}
-
-/// Spawns an ACP agent and completes the `initialize` handshake.
-pub(crate) async fn start_agent(command: &str, cwd: &str, env: &[(String, String)], budget: Duration) -> Result<Conn> {
- let (acp, rx) = Acp::spawn(command, cwd, env)?;
- let init = tokio::time::timeout(
- budget,
- acp.request(
- "initialize",
- json!({
- "protocolVersion": 1,
- // `session.compaction`: tell us when the agent summarizes its context.
- // `parameterizedModelPicker`: Cursor lists plain models with their options
- // (effort, fast…) instead of one fixed variant each; model discovery asks the same.
- "clientCapabilities": {
- "fs": {"readTextFile": false, "writeTextFile": false},
- "terminal": false,
- "session": {"compaction": {}},
- "_meta": {"parameterizedModelPicker": true},
- },
- "clientInfo": {"name": "codync", "title": "Codync", "version": env!("CARGO_PKG_VERSION")},
- }),
- ),
- )
- .await;
- let init = match init {
- Ok(Ok(v)) => v,
- Ok(Err(e)) => {
- acp.kill().await;
- return Err(e);
- }
- Err(_) => {
- acp.kill().await;
- let message = format!("the agent didn't answer the ACP handshake within {}s", budget.as_secs());
- return Err(acp::Failure::error(acp::FailureKind::Timeout, message));
- }
- };
- let sessions = SessionCaps {
- load: 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 {
- acp,
- rx,
- sessions,
- claude,
- loaded: HashMap::new(),
- started: Instant::now(),
- fast_options: HashMap::new(),
- reports_compaction: false,
- used: HashMap::new(),
- })
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn variant_ids_become_options_only_when_the_agent_lists_plain_models() {
- let plain = json!({"configOptions": [{"id": "model", "category": "model", "type": "select",
- "options": [{"value": "grok-4.7"}]}]});
- let variant = "grok-4.7[context=256k,reasoning_effort=medium,fast=false]";
- assert_eq!(
- split_variant(&plain, variant),
- ("grok-4.7", vec![("context", "256k"), ("reasoning_effort", "medium"), ("fast", "false")])
- );
- assert_eq!(split_variant(&plain, "grok-4.7"), ("grok-4.7", vec![]));
- let variants = json!({"configOptions": [{"id": "model", "category": "model", "type": "select",
- "options": [{"value": variant}]}]});
- assert_eq!(split_variant(&variants, variant), (variant, vec![]));
- assert_eq!(split_variant(&plain, "other[x=1]"), ("other[x=1]", vec![]));
- // Claude's `opus[1m]` is a model id of its own, not a variant.
- assert_eq!(split_variant(&json!({"models": {"availableModels": [{"modelId": "opus"}]}}), "opus[1m]").1, vec![]);
- // Cursor's Auto: `default[]` with variants, `default` without.
- let auto = json!({"models": {"availableModels": [{"modelId": "default"}]}});
- assert_eq!(split_variant(&auto, "default[]"), ("default", vec![]));
- }
-}
diff --git a/host/src/agent/bot/turn.rs b/host/src/agent/bot/turn.rs
index 7c2056a8..10a10623 100644
--- a/host/src/agent/bot/turn.rs
+++ b/host/src/agent/bot/turn.rs
@@ -87,7 +87,9 @@ impl Actor {
} else {
self.ensure_session(slot.as_deref()).await?;
}
- if let Some(sid) = self.turn_session.clone() {
+ let session = self.turn_session.clone();
+ self.hub.set_runtime(&self.id(), |r| r.session.clone_from(&session));
+ if let Some(sid) = session {
self.sync_fast_mode(&sid).await;
}
if hidden && self.session_fresh {
@@ -108,6 +110,7 @@ impl Actor {
r.started_at = None;
r.lane = None;
r.message = None;
+ r.session = None;
});
return Ok(());
}
@@ -131,9 +134,8 @@ 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 {
- // 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)?;
+ // 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)?;
}
if let Some((notice, revision)) = memory::change_notice(&self.hub.store, &self.cfg, &sid) {
prompt = format!("{notice}\n\n{prompt}");
@@ -317,6 +319,7 @@ impl Actor {
r.started_at = None;
r.lane = None;
r.message = None;
+ r.session = None;
});
self.hub.team.cancel_from(&self.cfg.id);
// A room turn's news comes from the group once the room is done (see `group`).
diff --git a/host/src/agent/bot/updates.rs b/host/src/agent/bot/updates.rs
index ccb06863..f33f3a8e 100644
--- a/host/src/agent/bot/updates.rs
+++ b/host/src/agent/bot/updates.rs
@@ -54,6 +54,9 @@ impl Actor {
let Some(sid) = self.turn_session.clone() else { return };
match context::bump_epoch(&self.hub.store, &self.cfg.id, &sid, id) {
Ok(true) => {
+ if let Err(error) = crate::chat::memory::compacted(&self.hub.store, &self.cfg.id, &sid) {
+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the compaction");
+ }
if self.conn.as_ref().is_some_and(|c| c.instructions() == Instructions::FirstMessage) {
self.profile_resend.insert(sid);
}
diff --git a/host/src/chat/context.rs b/host/src/chat/context.rs
index 7c03c8b8..ec7e213d 100644
--- a/host/src/chat/context.rs
+++ b/host/src/chat/context.rs
@@ -3,14 +3,13 @@
//!
//! The bot's instructions (name, description, skills, memory) are rendered once
//! and kept as a [`Snapshot`] keyed by the ACP session and its *compaction
-//! epoch*. Harnesses that take a system prompt (Claude, via `_meta.systemPrompt`)
-//! get it as one; others get it in the first message of the session. The
-//! snapshot stays byte-identical, so the prompt cache stays warm, until the
+//! epoch*. How it reaches the agent depends on the harness (`agent::bot::Instructions`).
+//! The snapshot stays byte-identical, so the prompt cache stays warm, until the
//! agent compacts its context (a `compaction_update`) or a new session starts.
//! Profile edits in between are announced once as an `<agent_profile_update>`
//! block on the next message and folded into the snapshot after the next
-//! compaction. Memory learned in between reaches the prompt the same way; a change
-//! the user makes is also told on the next message (see `memory::change_notice`).
+//! compaction. Memory changes in between are told on the next message the same way
+//! (see `memory::change_notice`).
use crate::store::{BotConfig, Store};
use anyhow::Result;
@@ -42,6 +41,9 @@ pub struct Snapshot {
pub system_identity: Identity,
/// The identity the agent last heard of (`system` or a later update block).
pub announced: Identity,
+ /// The memory revision `system` holds (`memory::current_revision` before it was rendered).
+ #[serde(default)]
+ pub memory: String,
}
// A bot talks in several lanes (its chat, threads, groups), each its own session: snapshots
@@ -100,14 +102,16 @@ pub fn resolve(store: &Store, cfg: &BotConfig, session: &str) -> Result<Snapshot
return Ok(s.clone());
}
let identity = Identity::of(cfg);
+ let memory = crate::chat::memory::current_revision(store, &cfg.id);
let s = Snapshot {
session: session.to_owned(),
epoch,
system: render(store, cfg),
system_identity: identity.clone(),
- // A re-render reaches only Claude, as its system prompt (see `mark_announced`): the agent
- // still knows the identity it last heard, so an update it missed is still announced.
+ // 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),
+ memory,
};
save(store, &cfg.id, &s)?;
Ok(s)
@@ -127,8 +131,9 @@ pub fn invalidate(store: &Store, bot_id: &str) -> Result<()> {
Ok(())
}
-/// Records `system` (already handed to a new session) as that session's snapshot.
-pub fn adopt(store: &Store, cfg: &BotConfig, session: &str, system: String) -> Result<Snapshot> {
+/// Records `system` (already handed to a new session), rendered at memory revision `memory`, as that session's
+/// snapshot.
+pub fn adopt(store: &Store, cfg: &BotConfig, session: &str, system: String, memory: String) -> Result<Snapshot> {
let identity = Identity::of(cfg);
let s = Snapshot {
session: session.to_owned(),
@@ -136,6 +141,7 @@ pub fn adopt(store: &Store, cfg: &BotConfig, session: &str, system: String) -> R
system,
system_identity: identity.clone(),
announced: identity,
+ memory,
};
save(store, &cfg.id, &s)?;
Ok(s)
@@ -249,7 +255,7 @@ mod tests {
fn snapshot_is_frozen_until_compaction_and_updates_are_announced_once() {
let store = store();
let a = cfg("Fixer", "Fix bugs");
- let first = adopt(&store, &a, "s1", "SYSTEM v1".into()).expect("adopt");
+ let first = adopt(&store, &a, "s1", "SYSTEM v1".into(), String::new()).expect("adopt");
// Frozen: resolving again returns the same text, even though a live render differs.
assert_eq!(resolve(&store, &a, "s1").expect("resolve").system, "SYSTEM v1");
assert!(profile_update(&store, &first, &a).is_none());
@@ -276,7 +282,7 @@ mod tests {
fn a_memory_change_re_renders_but_keeps_what_the_agent_heard() {
let store = store();
let a = cfg("Fixer", "Fix bugs");
- adopt(&store, &a, "s1", "SYSTEM v1".into()).expect("adopt");
+ adopt(&store, &a, "s1", "SYSTEM v1".into(), String::new()).expect("adopt");
let b = cfg("Fixer", "Fix bugs and write tests");
invalidate(&store, &a.id).expect("invalidate");
let snap = resolve(&store, &b, "s1").expect("resolve");
diff --git a/host/src/chat/memory/api.rs b/host/src/chat/memory/api.rs
index ec7e7cf6..a2f590e6 100644
--- a/host/src/chat/memory/api.rs
+++ b/host/src/chat/memory/api.rs
@@ -1,6 +1,6 @@
//! The apps' memory methods: the two layers, the user's own edits, the dream log, and the chat cards.
-use super::{Change, notices};
+use super::notices;
use crate::hub::Hub;
use crate::store::BotConfig;
use anyhow::{Result, anyhow, bail};
@@ -38,7 +38,7 @@ fn dispatch(hub: &Hub, method: &str, b: &Value) -> Result<Value> {
let layer = (b["shared"] != true).then_some(cfg.id.as_str());
let new = New { bot: layer, text, room: None, evidence: &[] };
let entry = hub.store.memory_write(|c, now, rev| mem::add(c, Writer::User, new, now, rev))?;
- super::changed(hub, entry.bot.as_deref(), Change::User);
+ super::changed(hub, entry.bot.as_deref());
entry
};
Ok(json!({"entry": entry}))
@@ -65,7 +65,7 @@ fn dispatch(hub: &Hub, method: &str, b: &Value) -> Result<Value> {
"clearMemory" => {
let layer = (b["shared"] != true).then_some(cfg.id.as_str());
hub.store.memory_write(|c, _, _| mem::forget_layer(c, layer))?;
- super::changed(hub, layer, Change::User);
+ super::changed(hub, layer);
Ok(json!({}))
}
"undoDream" => {
@@ -76,7 +76,7 @@ fn dispatch(hub: &Hub, method: &str, b: &Value) -> Result<Value> {
}
hub.store.memory_write(|c, now, rev| codync_mem::dream::undo(c, id, now, rev))?;
// A run may have written to either layer.
- super::changed(hub, None, Change::User);
+ super::changed(hub, None);
Ok(json!({}))
}
_ => bail!("unknown method: {method}"),
@@ -98,7 +98,7 @@ fn edit(
}
change(c, now, rev)
})?;
- super::changed(hub, entry.bot.as_deref(), Change::User);
+ super::changed(hub, entry.bot.as_deref());
Ok(entry)
}
diff --git a/host/src/chat/memory/dream.rs b/host/src/chat/memory/dream.rs
index b404ec92..1699f637 100644
--- a/host/src/chat/memory/dream.rs
+++ b/host/src/chat/memory/dream.rs
@@ -1,7 +1,6 @@
//! Dreaming in the host: when a bot's run starts, what it reads, and running its model on the bot's own harness.
//! What a run may change is the crate's decision (`codync_mem::dream`).
-use super::Change;
use crate::LockExt;
use crate::chat::{naming, oneshot};
use crate::hub::Hub;
@@ -188,7 +187,7 @@ async fn run(hub: &Arc<Hub>, bot_id: &str) -> Result<Ran> {
Outcome::Changed(dream) => {
tracing::info!(bot = %bot_id, added = dream.added.len(), archived = dream.archived.len(), "memory dreamed");
let layer = if kept.add.iter().any(|a| a.shared) { None } else { Some(bot_id) };
- super::changed(hub, layer, Change::Background);
+ super::changed(hub, layer);
}
Outcome::Unchanged => {}
}
diff --git a/host/src/chat/memory/mod.rs b/host/src/chat/memory/mod.rs
index 5b213e5a..fca1483a 100644
--- a/host/src/chat/memory/mod.rs
+++ b/host/src/chat/memory/mod.rs
@@ -35,68 +35,131 @@ pub fn opening(store: &Store, cfg: &BotConfig) -> String {
}
}
-#[derive(Clone, Copy, Debug, PartialEq, Eq)]
-pub enum Change {
- /// The user changed memory: every session of the bot hears it on its next turn.
- User,
- /// A bot or dreaming wrote it. The session that wrote it knows; others pick it up at their next compaction or
- /// new session.
- Background,
-}
-
-/// After memory changed in `bot`'s own layer, or in the shared one (`None`): clients fetch it again, and a change
-/// the user made reaches the bots' sessions.
-pub fn changed(hub: &Hub, bot: Option<&str>, change: Change) {
- let bots: Vec<String> = match bot {
- Some(bot) => vec![bot.to_owned()],
+/// After memory changed in `bot`'s own layer, or in the shared one (`None`): the apps fetch it again, and every
+/// session of a bot that sees the layer hears it on its next message (see [`change_notice`]). The opening itself
+/// stays as it is, so the prompt cache does too.
+pub fn changed(hub: &Hub, bot: Option<&str>) {
+ let bots: Vec<(String, bool)> = match bot {
+ Some(bot) => vec![(bot.to_owned(), true)],
None => hub
.store
.bots()
- .map(|rows| rows.into_iter().filter(|r| !r.deleted && !r.config.is_group()).map(|r| r.config.id).collect())
+ .map(|rows| {
+ rows.into_iter()
+ .filter(|r| !r.deleted && !r.config.is_group())
+ .map(|r| {
+ let sees = shared(&r.config);
+ (r.config.id, sees)
+ })
+ .collect()
+ })
.unwrap_or_default(),
};
let revision = hub.store.memory_rev().to_string();
- for id in &bots {
- if change == Change::User
- && let Err(error) = hub
- .store
- .kv_set(&revision_key(id), &revision)
- .and_then(|()| crate::chat::context::invalidate(&hub.store, id))
- {
+ for (id, sees) in &bots {
+ if *sees && let Err(error) = hub.store.kv_set(&revision_key(id), &revision) {
tracing::warn!(bot = %id, error = format!("{error:#}"), "couldn't flag the memory change");
}
hub.emit_bot(id);
}
}
+/// The memory revision a bot's layers are at.
fn revision_key(bot: &str) -> String {
format!("memory.revision.{bot}")
}
+/// The revision a session has heard: its opening, then each notice since.
fn announced_key(bot: &str, session: &str) -> String {
format!("memory.announced.{bot}.{session}")
}
-/// When the user changed memory since this session last heard: the whole memory as it is now, to put before the
-/// next message, and the revision to acknowledge once it was delivered.
+/// The revision a session's opening holds.
+fn opening_key(bot: &str, session: &str) -> String {
+ format!("memory.opening.{bot}.{session}")
+}
+
+/// When memory changed since this session last heard: the whole memory as it is now, to put before the next
+/// message, and the revision to acknowledge once it was delivered.
pub fn change_notice(store: &Store, cfg: &BotConfig, session: &str) -> Option<(String, String)> {
- let revision = store.kv_get(&revision_key(&cfg.id)).unwrap_or_default();
+ let revision = current_revision(store, &cfg.id);
if revision.is_empty() || store.kv_get(&announced_key(&cfg.id, session)).unwrap_or_default() == revision {
return None;
}
let notice = format!(
- "<memory_update>\nThe user changed your memory. This is all of it now and replaces any earlier memory in this conversation.\n{}\n</memory_update>",
+ "<memory_update>\nYour memory changed. This is all of it now and replaces any earlier memory in this conversation.\n{}\n</memory_update>",
opening(store, cfg)
);
Some((notice, revision))
}
-/// The session heard memory up to `revision`: from a notice, or from instructions rendered just now.
+/// The session heard memory up to `revision`, from a notice.
pub fn mark_announced(store: &Store, bot: &str, session: &str, revision: &str) -> anyhow::Result<()> {
store.kv_set(&announced_key(bot, session), revision)
}
-/// The revision a fresh session's instructions already include.
+/// The session's opening now holds memory at `revision` (a new session, or instructions it got again).
+pub fn opened(store: &Store, bot: &str, session: &str, revision: &str) -> anyhow::Result<()> {
+ store.kv_set(&opening_key(bot, session), revision)?;
+ mark_announced(store, bot, session, revision)
+}
+
+/// The agent summarized its context, so the notices it got since its opening may be gone: it is left knowing what
+/// its opening holds, and hears anything newer again.
+pub fn compacted(store: &Store, bot: &str, session: &str) -> anyhow::Result<()> {
+ match store.kv_get(&opening_key(bot, session)) {
+ Some(opening) => mark_announced(store, bot, session, &opening),
+ None => Ok(()),
+ }
+}
+
+/// A bot wrote memory itself in `session`, which therefore knows the change: when it had heard everything up to
+/// `before`, it has heard this too.
+pub fn heard_own_write(store: &Store, bot: &str, session: &str, before: &str) -> anyhow::Result<()> {
+ if store.kv_get(&announced_key(bot, session)).unwrap_or_default() == before {
+ mark_announced(store, bot, session, &current_revision(store, bot))
+ } else {
+ Ok(())
+ }
+}
+
+/// The memory revision a bot's layers are at; an opening rendered now holds it.
pub fn current_revision(store: &Store, bot: &str) -> String {
store.kv_get(&revision_key(bot)).unwrap_or_default()
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn a_session_hears_each_change_once_and_again_after_a_compaction() {
+ let store =
+ Store::open(&std::env::temp_dir().join(format!("codync-memory-{}.db", uuid::Uuid::new_v4()))).unwrap();
+ let cfg: BotConfig = serde_json::from_value(serde_json::json!({
+ "id": "5f0c7a52-0000-4000-8000-000000000002", "name": "Fixer", "backend": "claude", "cwd": "/tmp",
+ }))
+ .unwrap();
+ let changes = |revision: &str| store.kv_set(&revision_key(&cfg.id), revision).unwrap();
+ opened(&store, &cfg.id, "s1", "").unwrap();
+ assert!(change_notice(&store, &cfg, "s1").is_none());
+
+ changes("7");
+ let (notice, revision) = change_notice(&store, &cfg, "s1").expect("a change");
+ assert!(notice.starts_with("<memory_update>"), "{notice}");
+ mark_announced(&store, &cfg.id, "s1", &revision).unwrap();
+ assert!(change_notice(&store, &cfg, "s1").is_none(), "told once");
+ compacted(&store, &cfg.id, "s1").unwrap();
+ assert!(change_notice(&store, &cfg, "s1").is_some(), "the summary may have dropped the notice");
+
+ // What the session wrote itself, with nothing else new, needs no notice; unless it missed something before.
+ opened(&store, &cfg.id, "s1", "7").unwrap();
+ changes("8");
+ heard_own_write(&store, &cfg.id, "s1", "7").unwrap();
+ assert!(change_notice(&store, &cfg, "s1").is_none());
+ changes("9");
+ changes("10");
+ heard_own_write(&store, &cfg.id, "s1", "9").unwrap();
+ assert!(change_notice(&store, &cfg, "s1").is_some());
+ }
+}
diff --git a/host/src/chat/memory/notices.rs b/host/src/chat/memory/notices.rs
index 7efaead3..c6a3b49d 100644
--- a/host/src/chat/memory/notices.rs
+++ b/host/src/chat/memory/notices.rs
@@ -2,7 +2,6 @@
//! acting, a card the user approves or declines. The line is an ordinary notice whose `memory` field says what
//! happened, so an app that doesn't know it still shows the text.
-use super::Change;
use crate::hub::Hub;
use crate::store::{BotConfig, EntryKind, Lane};
use anyhow::{Result, anyhow, bail};
@@ -60,7 +59,7 @@ pub fn apply(
let (entry, previous) = write(hub, cfg, &lane, message.clone(), action)?;
let card = Card { status: Status::Done, entry: Some(entry.id), previous, message, error: None };
post(hub, cfg, &lane, action, &card);
- super::changed(hub, entry.bot.as_deref(), Change::Background);
+ super::changed(hub, entry.bot.as_deref());
Ok(entry)
}
@@ -104,7 +103,7 @@ pub fn answer(hub: &Hub, notice_id: &str, accept: bool) -> Result<Value> {
card.status = Status::Accepted;
card.entry = Some(entry.id);
card.previous = previous.or(card.previous);
- super::changed(hub, entry.bot.as_deref(), Change::User);
+ super::changed(hub, entry.bot.as_deref());
}
// Dreaming may have learned it meanwhile: it's remembered either way.
Err(error)
@@ -152,7 +151,7 @@ pub fn undo(hub: &Hub, notice_id: &str) -> Result<Value> {
Ok(entry.bot)
})?;
card.status = Status::Undone;
- super::changed(hub, layer.as_deref(), Change::User);
+ super::changed(hub, layer.as_deref());
Ok(update(hub, &notice, &cfg, &action, &card))
}
diff --git a/host/src/chat/memory/tools.rs b/host/src/chat/memory/tools.rs
index f9ca2277..64d9caf3 100644
--- a/host/src/chat/memory/tools.rs
+++ b/host/src/chat/memory/tools.rs
@@ -107,7 +107,14 @@ async fn write(hub: &Arc<Hub>, cfg: &BotConfig, action: Action) -> Result<Value>
.and_then(|id| hub.store.entry(id))
.is_some_and(|e| codync_mem::evidence::asks_to_remember(&notices::typed(&e.data).join("\n")));
if cfg.permission == Permission::Auto || asked {
+ let before = super::current_revision(&hub.store, &cfg.id);
let entry = notices::apply(&hub, &cfg, turn.lane, turn.message, &action)?;
+ // The session that wrote it needs no notice about its own write.
+ if let Some(session) = &turn.session
+ && let Err(error) = super::heard_own_write(&hub.store, &cfg.id, session, &before)
+ {
+ tracing::warn!(bot = %cfg.id, error = format!("{error:#}"), "couldn't record the bot's own write");
+ }
Ok(json!({"status": "saved", "id": entry.id}))
} else {
notices::propose(&hub, &cfg, turn.lane, turn.message, &action)?;
diff --git a/host/src/hub.rs b/host/src/hub.rs
index 4dbf445d..6985ee4c 100644
--- a/host/src/hub.rs
+++ b/host/src/hub.rs
@@ -42,6 +42,8 @@ pub struct Runtime {
/// The user's message that started the running turn; `None` for a turn a routine, a room or another bot
/// started. Memory cites it as where a fact was learned.
pub message: Option<String>,
+ /// The ACP session the running turn prompts. A memory write made there is one that session already knows.
+ pub session: Option<String>,
pub fast_mode: crate::agent::config::FastModeSupport,
}
@@ -252,7 +254,7 @@ impl Hub {
}
if !cfg.is_group() {
if crate::chat::memory::shared(&cfg) != crate::chat::memory::shared(&row.config) {
- crate::chat::memory::changed(self, Some(&id), crate::chat::memory::Change::User);
+ crate::chat::memory::changed(self, Some(&id));
}
self.send_cmd(&id, Cmd::Reconfigure(Box::new(cfg)))?;
}
diff --git a/host/tests/memory_e2e.rs b/host/tests/memory_e2e.rs
index 9c24e155..88827225 100644
--- a/host/tests/memory_e2e.rs
+++ b/host/tests/memory_e2e.rs
@@ -336,6 +336,14 @@ async fn instructions_reach_every_harness_and_survive_compaction() {
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>"));
+ // A change reaches the session as a note on its next message; the opening stays as it was.
+ host.call("saveMemory", json!({"botId": standard, "text": "Plays the piano"})).await;
+ host.reply(&standard, "again").await;
+ host.reply(&standard, "and again").await;
+ let prompts = host.prompts(&standard);
+ assert!(prompts[1].starts_with("<memory_update>") && prompts[1].contains("Plays the piano"), "{}", prompts[1]);
+ assert!(!prompts[2].contains("<memory_update>"), "told once");
+ assert_eq!(host.requests(&standard, "session/new").len(), 1);
// 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;