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): keep bots working through memory, Stop and group hiccups

leepokai104f99a93922
diff --git a/docs/features/context-and-memory.md b/docs/features/context-and-memory.md
index fda738c2..413a2e7f 100644
--- a/docs/features/context-and-memory.md
+++ b/docs/features/context-and-memory.md
@@ -37,11 +37,14 @@ its own context and compacts it itself.
- Engram stores each bot's durable observations in an independent SQLite database. The old Markdown memory
imports automatically, with original dates and source references preserved.
- The instruction snapshot includes up to 100 profile facts and 30 recent facts, within a 4,000-character
- recent-memory budget. A keeper consolidates crowded profiles to at most 60 facts; demoted facts remain
- searchable. Manual corrections invalidate snapshots and send a notice on the next turn.
+ recent-memory budget (each recent fact or summary clipped to 500 characters). A keeper consolidates crowded
+ profiles to at most 60 facts; demoted facts remain searchable. Manual corrections invalidate snapshots and
+ send a notice on the next turn; pins and imports re-render them. A re-render keeps what the agent last heard
+ of its profile, so an update it missed is still announced.
- Completed memorable exchanges queue durably. After five idle minutes or eight exchanges, the bot's own
harness extracts durable facts and saves a native Engram session summary. A new session or reported
- compaction requests an early flush. Failed batches remain queued for retry.
+ compaction requests an early flush. Failed batches remain queued and retry with backoff.
+- Memory is best-effort: a bot whose Engram can't start still answers, and says so once.
- Claude's keeper uses Haiku without tools, settings or persisted helper sessions; other harnesses use the
bot's selected model with inline instructions. Automatic naming still uses `chat/naming.rs`.
- The built-in memory MCP server forwards native Engram tools for recall, saving, review and conflict
@@ -54,6 +57,10 @@ its own context and compacts it itself.
- Messages sent while the agent works are folded into one next turn, joined by blank lines.
- Bot-to-bot requests occupy separate turns in the same queue; user messages are only folded together up to the next request. Requests never feed the user-fact memory keeper. See [bot collaboration](bot-collaboration.md).
- Delegated turns do not set the restart marker: their waiter disappears on restart. Pending delegation notices become interrupted errors instead of automatically replaying work.
+- Stop sends `session/cancel`; an agent still running 15 seconds later is stopped (it resumes its session on
+ the next turn). `session/new`, `session/load` and `session/fork` get three minutes before the agent is
+ stopped. Connector or computer changes restart an agent that can resume sessions before its next turn; one
+ that can't keeps its session, and its next new session gets them.
- `turn.inflight.<bot>` records the running turn start and its thread lane. If the host stops mid-turn (update, crash, restart), the next
start resumes that session with a hidden "you were interrupted, don't repeat finished steps" prompt. If the
session can't be resumed, or the turn started over an hour ago, it does nothing.
diff --git a/docs/features/engram-memory.md b/docs/features/engram-memory.md
index c37b5cc6..b0ad3acf 100644
--- a/docs/features/engram-memory.md
+++ b/docs/features/engram-memory.md
@@ -10,8 +10,10 @@ native tools plus Codync's `search_history`. ACP and MCP have different responsi
Each bot has an independent `bots/<id>/engram/engram.db` under `CODYNC_HOME` (normally `~/.codync`).
The host downloads the MIT-licensed release on first use, validates its SHA-256 against
`packaging/engram/release.json`, and installs it with its license in `engram/3.2.1/`.
-First use requires network access; subsequent use works offline. Supported binaries are macOS, Linux and
-Windows, each on arm64 and x86_64. No global Engram install or HTTP listener is needed.
+First use requires network access; subsequent use works offline. A failed download is retried after five
+minutes. Memory is best-effort: while Engram can't start, bots answer without it and say so once.
+Supported binaries are macOS, Linux and Windows, each on arm64 and x86_64. No global Engram install or HTTP
+listener is needed.
The host lazily starts one native MCP stdio process per active bot. Requests serialize within that process;
an idle process stops after five minutes and restarts on demand. A failed request is reported, never silently
@@ -31,7 +33,8 @@ On a bot's first memory access, `memory/profile.md` and `memory/log/*.md` are co
immutable `engram/markdown-backup.json` import journal. Each dated bullet receives a deterministic sync ID;
its original date, content, profile/log scope and source path/line are imported through the native CLI.
The host verifies every imported ID before writing `.codync-markdown-imported`. Interrupted imports retry
-without duplicating records. Malformed dated rows fail visibly instead of being silently discarded.
+without duplicating records. A bullet whose date can't be read is imported whole, dated by the import, rather
+than discarded or blocking the import (the journal is immutable, so editing the Markdown can't fix it).
The original Markdown remains untouched as a backup and is no longer authoritative.
The completion marker is retained by **Forget everything** so old Markdown cannot reappear. Removing the
@@ -54,10 +57,12 @@ consolidating a crowded profile, and saving native `session_summary` observation
minutes or eight queued exchanges; new-session and compaction events request an early flush. Summaries are
incremental batches of a session. The harness's compacted context is a separate artifact.
-Exchanges remain in the durable Codync queue until processing succeeds. Failures are logged and retried after
-five minutes. Explicit memory corrections invalidate prompt snapshots and pending old extraction work;
-in-progress extraction checks a revision before writing. Editing memory sends a correction notice on the
-next agent turn. It cannot remove text already held in a harness's context; start a new session when needed.
+Only the user's own messages feed it (and Engram's captured prompts): not bot requests, group rooms or
+routine runs. Exchanges remain in the durable Codync queue until processing succeeds; one run takes up to
+eight. A failure is logged and retried after ten minutes, doubling up to about two and a half hours; while it
+keeps failing, the newest 32 exchanges wait. Explicit memory corrections (edit, forget, clear) invalidate prompt
+snapshots and pending old extraction work; in-progress extraction checks a revision before writing. Editing
+memory sends a correction notice on the next agent turn. Pinning and importing only re-render the snapshots. It cannot remove text already held in a harness's context; start a new session when needed.
The transcript is retained separately and is still searchable with `search_history`.
**Forget** soft-deletes an observation through Engram. **Forget everything** invokes native hard project
diff --git a/docs/features/groups-and-threads.md b/docs/features/groups-and-threads.md
index a2d9bd11..bf6067c9 100644
--- a/docs/features/groups-and-threads.md
+++ b/docs/features/groups-and-threads.md
@@ -53,7 +53,8 @@ replies itself.
told who's in the room and what was said since it last spoke there, framed as
`[Group chat: "<name>" - with …]`. Its final reply is the one message the room sees; replying
`(pass)` says nothing. A round where nobody speaks ends the room turn.
-- A new message in the same lane, or Stop, ends a running room turn before its next speaker.
+- A new message in the same lane, or Stop, ends a running room turn before its next speaker;
+ a member still busy elsewhere skips the old room turn's queued request.
- Tool calls, thoughts and permission cards of a member's group turn are written to the
group (with `author`), not to the member's own chat. One "done" push per room turn.
- Files sent to a group are kept in the group's data folder (`~/.codync/bots/<group>/uploads`);
diff --git a/host/src/agent/bot/mod.rs b/host/src/agent/bot/mod.rs
index bf36cd3e..cb788d73 100644
--- a/host/src/agent/bot/mod.rs
+++ b/host/src/agent/bot/mod.rs
@@ -127,10 +127,11 @@ pub fn spawn(hub: Arc<Hub>, cfg: BotConfig) -> BotHandle {
routine_timed_out: false,
routine_completion: None,
session_fresh: false,
- applied_system: None,
+ applied_system: HashMap::new(),
keeper,
turn_text: None,
memory_session: None,
+ memory_warned: false,
turn_memory_revision: String::new(),
announce: None,
turn: None,
@@ -144,6 +145,7 @@ pub fn spawn(hub: Arc<Hub>, cfg: BotConfig) -> BotHandle {
files: files::FileShares::default(),
perms: HashMap::new(),
stop_requested: false,
+ cancel_deadline: None,
exit_tail: None,
tools_changed: false,
};
@@ -209,12 +211,14 @@ struct Actor {
routine_completion: Option<RoutineCompletion>,
/// True until the first prompt of a new ACP session has been sent.
session_fresh: bool,
- /// (session, instructions) last handed to Claude as its system prompt by this process.
- applied_system: Option<(String, String)>,
+ /// Session -> the instructions this process last handed Claude as its system prompt.
+ applied_system: HashMap<String, String>,
keeper: mpsc::UnboundedSender<memory::KeeperEvent>,
- /// The user's words for this turn (None for a hidden turn); feeds memory.
+ /// The user's words for this turn (None for a hidden turn or one not from the user); feeds memory.
turn_text: Option<String>,
memory_session: Option<String>,
+ /// The user was told memory is unavailable.
+ memory_warned: bool,
turn_memory_revision: String,
/// A profile update this turn carried, recorded once the agent got it.
announce: Option<(Snapshot, Identity)>,
@@ -231,6 +235,8 @@ struct Actor {
/// permission entry id -> JSON-RPC request id
perms: HashMap<String, Value>,
stop_requested: bool,
+ /// A cancelled turn still running past this stops its agent process.
+ cancel_deadline: Option<Instant>,
/// Last stderr lines of an agent process that just exited.
exit_tail: Option<String>,
/// Connectors changed: restart the agent before the next turn so the
@@ -295,6 +301,11 @@ impl Actor {
self.stop_requested = true;
if let Some(c) = self.conn.take() { c.acp.kill().await; }
}
+ // The agent ignored the cancel: stop its process so the bot is free again.
+ if self.turn.is_some() && self.cancel_deadline.is_some_and(|deadline| Instant::now() >= deadline) {
+ self.cancel_deadline = None;
+ if let Some(c) = self.conn.take() { c.acp.kill().await; }
+ }
},
}
}
@@ -343,6 +354,9 @@ impl Actor {
/// Told to a bot whose turn the previous host process cut off (Grok Bot's upgrade resume).
const RESUME_PROMPT: &str = "[Codync restarted on this computer and interrupted you mid-task. You've been resumed with your full conversation intact. Continue exactly where you left off and finish what you were doing. If your previous step already completed an action, do NOT repeat it — just carry on from there.]";
+/// How long a cancelled turn may take to wind down before its agent process is stopped.
+const CANCEL_GRACE: Duration = Duration::from_secs(15);
+
/// An interrupted turn older than this isn't resumed.
const STALE_RESUME_MS: i64 = 60 * 60 * 1000;
diff --git a/host/src/agent/bot/queue.rs b/host/src/agent/bot/queue.rs
index d6414f1c..51264ee2 100644
--- a/host/src/agent/bot/queue.rs
+++ b/host/src/agent/bot/queue.rs
@@ -1,6 +1,6 @@
//! Commands from clients and the queue of turns waiting for the bot.
-use super::{Actor, Cmd, Done, NoticeStyle, Queued, RESUME_PROMPT, RoutineCompletion, inflight_key};
+use super::{Actor, CANCEL_GRACE, Cmd, Done, NoticeStyle, Queued, RESUME_PROMPT, RoutineCompletion, inflight_key};
use crate::chat::memory;
use crate::hub::BotStatus;
use crate::store::{Lane, now_ms};
@@ -131,6 +131,7 @@ impl Actor {
return;
}
self.stop_requested = true;
+ self.cancel_deadline.get_or_insert_with(|| Instant::now() + CANCEL_GRACE);
self.files.cancel();
let ids: Vec<String> = self.perms.keys().cloned().collect();
for id in ids {
@@ -153,7 +154,6 @@ impl Actor {
Ok(Some(prepared)) => {
self.routine_deadline = Some(Instant::now() + prepared.timeout);
let result = self.start_turn(prepared.lane, &[], &prepared.prompt, false, done_tx).await;
- self.turn_text = None;
if let Err(error) = result {
self.start_failed(&error);
}
@@ -184,8 +184,6 @@ impl Actor {
ask.mark_started();
self.active_request = Some(ask);
let result = self.start_turn(main.clone(), &ids, &prompt, false, done_tx).await;
- // Another bot's words are not facts learned from the user.
- self.turn_text = None;
if let Err(e) = result {
self.start_failed(&e);
}
@@ -195,14 +193,13 @@ impl Actor {
let Some(Queued::Group(turn)) = self.queue.pop_front() else {
unreachable!("front is a group turn")
};
- if turn.reply.is_closed() {
+ // A newer message in the room (or Stop) ended the room turn that queued this.
+ if turn.reply.is_closed() || !self.hub.groups.current(&turn.lane, turn.epoch) {
continue;
}
let (lane, prompt) = (turn.lane.clone(), turn.prompt.clone());
self.active_group = Some(turn);
let result = self.start_turn(lane, &[], &prompt, false, done_tx).await;
- // The room is not the user's private chat: nothing here feeds memory.
- self.turn_text = None;
if let Err(e) = result {
self.start_failed(&e);
}
diff --git a/host/src/agent/bot/session.rs b/host/src/agent/bot/session.rs
index cc132679..87720fb8 100644
--- a/host/src/agent/bot/session.rs
+++ b/host/src/agent/bot/session.rs
@@ -133,17 +133,21 @@ impl Actor {
/// Makes sure the agent runs and `slot`'s session (main, or a thread's) is live;
/// sets `turn_session`. A thread without one forks the main session when it can.
pub(super) async fn ensure_session(&mut self, slot: Option<&str>) -> Result<()> {
- crate::chat::memory::prepare(&self.cfg.id).await?;
+ // Memory is best-effort: a bot without it still answers.
+ if let Err(error) = crate::chat::memory::prepare(&self.cfg.id).await {
+ self.memory_unavailable(&error);
+ }
if !self.cfg.connectors.is_empty()
|| self.hub.store.kv_read(&format!("agent-env:{}", self.cfg.backend))?.is_some()
{
crate::market::vault::unlock(self.hub.clone()).await?;
}
- if self.tools_changed && self.turn.is_none() {
- self.tools_changed = false;
- if let Some(c) = self.conn.take() {
- c.acp.kill().await;
- }
+ // An agent that can't resume keeps its session; its next new session gets the new tools.
+ if std::mem::take(&mut self.tools_changed)
+ && self.conn.as_ref().is_some_and(|c| c.sessions.load)
+ && let Some(c) = self.conn.take()
+ {
+ c.acp.kill().await;
}
if self.conn.is_none() {
let (hub, id) = (self.hub.clone(), self.cfg.id.clone());
@@ -184,12 +188,13 @@ 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 conn = self.conn.as_ref().ok_or_else(|| anyhow!("agent not running"))?;
- let res = conn
- .acp
- .request("session/fork", json!({"sessionId": main, "cwd": self.cfg.cwd, "mcpServers": servers}))
+ let res = self
+ .session_request("session/fork", json!({"sessionId": main, "cwd": self.cfg.cwd, "mcpServers": servers}))
.await;
- if let Some(sid) = res.ok().and_then(|r| r["sessionId"].as_str().map(str::to_owned)) {
+ if let Some(sid) = res.ok().and_then(|r| r["sessionId"].as_str().map(str::to_owned))
+ && let Some(conn) = self.conn.as_mut()
+ {
+ conn.loaded.insert(sid.clone());
self.set_session(slot, Some(&sid));
forked = true;
} else {
@@ -207,11 +212,10 @@ impl Actor {
(Some(sid), true) => Some(self.snapshot(sid).await?),
_ => None,
};
- let Some(conn) = self.conn.as_mut() else { bail!("agent not running") };
+ let Some(conn) = self.conn.as_ref() else { bail!("agent not running") };
if let Some(sid) = current {
- let prompt_current = snapshot
- .as_ref()
- .is_none_or(|s| self.applied_system.as_ref().is_some_and(|(a, t)| *a == sid && *t == s.system));
+ let prompt_current =
+ snapshot.as_ref().is_none_or(|s| self.applied_system.get(&sid).is_some_and(|t| *t == s.system));
if conn.loaded.contains(&sid) && prompt_current {
self.turn_session = Some(sid);
return Ok(());
@@ -229,7 +233,8 @@ impl Actor {
if let Some(s) = &snapshot {
params["_meta"] = system_meta(&s.system);
}
- let res = conn.acp.request("session/load", params).await;
+ 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.
while let Ok(inc) = conn.rx.try_recv() {
if let Incoming::Request { id, .. } = inc {
@@ -240,7 +245,9 @@ impl Actor {
Ok(_) => {
conn.loaded.insert(sid.clone());
if let Some(s) = snapshot {
- self.applied_system = Some((sid.clone(), s.system));
+ // Its system prompt now names the bot as rendered.
+ context::mark_announced(&self.hub.store, &self.cfg.id, &s, s.system_identity.clone())?;
+ self.applied_system.insert(sid.clone(), s.system);
}
self.turn_session = Some(sid);
return Ok(());
@@ -256,12 +263,12 @@ impl Actor {
let (hub, cfg) = (self.hub.clone(), self.cfg.clone());
tokio::task::spawn_blocking(move || context::render(&hub.store, &cfg)).await?
};
- let Some(conn) = self.conn.as_mut() else { bail!("agent not running") };
let mut params = json!({"cwd": self.cfg.cwd, "mcpServers": servers});
if claude {
params["_meta"] = system_meta(&system);
}
- let res = conn.acp.request("session/new", params).await?;
+ let res = self.session_request("session/new", params).await?;
+ let Some(conn) = self.conn.as_mut() else { bail!("agent not running") };
let sid = res["sessionId"].as_str().ok_or_else(|| anyhow!("agent returned no sessionId"))?.to_owned();
conn.loaded.insert(sid.clone());
select_model(&conn.acp, &res, &sid, &self.cfg).await?;
@@ -273,17 +280,44 @@ impl Actor {
}
context::adopt(&self.hub.store, &self.cfg, &sid, system.clone())?;
if claude {
- self.applied_system = Some((sid, system));
+ self.applied_system.insert(sid, system);
}
Ok(())
}
+ /// 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.
+ 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 {
+ return result;
+ }
+ if let Some(c) = self.conn.take() {
+ c.acp.kill().await;
+ }
+ bail!("the agent didn't answer {method} within {}s", SESSION_TIMEOUT.as_secs())
+ }
+
+ /// Memory failed to start: said once per actor, then the bot works without it.
+ pub(super) fn memory_unavailable(&mut self, error: &anyhow::Error) {
+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "memory unavailable");
+ if !std::mem::replace(&mut self.memory_warned, true) {
+ self.notice(
+ &format!("Memory is unavailable; the bot works without it for now. {error:#}"),
+ NoticeStyle::Info,
+ );
+ }
+ }
+
/// 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);
}
}
+/// How long `session/new`, `session/load` and `session/fork` may take.
+const SESSION_TIMEOUT: Duration = Duration::from_secs(180);
+
/// Claude's `_meta`: the bot's instructions appended to Claude Code's own system prompt.
fn system_meta(system: &str) -> Value {
json!({"systemPrompt": {"append": system}})
diff --git a/host/src/agent/bot/turn.rs b/host/src/agent/bot/turn.rs
index f316ab9b..0ad7086c 100644
--- a/host/src/agent/bot/turn.rs
+++ b/host/src/agent/bot/turn.rs
@@ -44,10 +44,14 @@ impl Actor {
}
self.lane = lane.clone();
self.turn = Some(turn);
- self.turn_text = (!hidden).then(|| text.to_owned());
+ // Only the user's own words feed memory: not another bot's, a room's or a routine's.
+ let from_user =
+ !hidden && self.active_request.is_none() && self.active_group.is_none() && self.active_routine.is_none();
+ self.turn_text = from_user.then(|| text.to_owned());
self.turn_memory_revision = memory::maintenance::revision(&self.hub.store, &self.cfg.id);
self.announce = None;
self.stop_requested = false;
+ self.cancel_deadline = None;
self.hub.team.start_turn(&self.cfg.id, self.active_request.as_ref());
self.exit_tail = None;
self.seg = Seg::None;
@@ -111,9 +115,17 @@ impl Actor {
}
let claude = self.conn.as_ref().is_some_and(|c| c.claude);
let sid = self.turn_session.clone().ok_or_else(|| anyhow!("no session"))?;
- let memory_session = memory::lifecycle::begin(&self.hub, &self.cfg.id, &sid).await?;
- self.memory_session = Some(memory_session.clone());
- if !hidden && let Err(error) = memory::lifecycle::capture_prompt(&self.cfg.id, &memory_session, text).await {
+ self.memory_session = match memory::lifecycle::begin(&self.hub, &self.cfg.id, &sid).await {
+ Ok(session) => Some(session),
+ Err(error) => {
+ self.memory_unavailable(&error);
+ None
+ }
+ };
+ if from_user
+ && let Some(session) = &self.memory_session
+ && let Err(error) = memory::lifecycle::capture_prompt(&self.cfg.id, session, text).await
+ {
tracing::warn!(bot = %self.cfg.id, %error, "memory prompt capture failed");
}
let snapshot = self.snapshot(&sid).await?;
diff --git a/host/src/chat/context.rs b/host/src/chat/context.rs
index ee69066f..829303e9 100644
--- a/host/src/chat/context.rs
+++ b/host/src/chat/context.rs
@@ -95,10 +95,36 @@ fn save(store: &Store, bot_id: &str, s: &Snapshot) -> Result<()> {
/// the stored one belongs to another session or an earlier epoch. Reads memory files.
pub fn resolve(store: &Store, cfg: &BotConfig, session: &str) -> Result<Snapshot> {
let epoch = epoch(store, &cfg.id, session);
- if let Some(s) = load(store, &cfg.id, session).filter(|s| s.epoch == epoch) {
- return Ok(s);
+ let stored = load(store, &cfg.id, session);
+ if let Some(s) = stored.as_ref().filter(|s| s.epoch == epoch && !s.system.is_empty()) {
+ return Ok(s.clone());
}
- adopt(store, cfg, session, render(store, cfg))
+ let identity = Identity::of(cfg);
+ 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.
+ announced: stored.map_or(identity, |old| old.announced),
+ };
+ save(store, &cfg.id, &s)?;
+ Ok(s)
+}
+
+/// Memory the instructions show changed: every session of the bot re-renders them on its next turn.
+pub fn invalidate(store: &Store, bot_id: &str) -> Result<()> {
+ for k in store.kv_prefix(&format!("context.{bot_id}.")) {
+ match store.kv_get(&k).and_then(|v| serde_json::from_str::<Snapshot>(&v).ok()) {
+ Some(mut s) => {
+ s.system.clear();
+ save(store, bot_id, &s)?;
+ }
+ None => store.kv_set(&k, "")?,
+ }
+ }
+ Ok(())
}
/// Records `system` (already handed to a new session) as that session's snapshot.
@@ -248,4 +274,16 @@ mod tests {
// A new session starts over at epoch 0.
assert_eq!(epoch(&store, &a.id, "s2"), 0);
}
+
+ #[test]
+ 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");
+ let b = cfg("Fixer", "Fix bugs and write tests");
+ invalidate(&store, &a.id).expect("invalidate");
+ let snap = resolve(&store, &b, "s1").expect("resolve");
+ 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");
+ }
}
diff --git a/host/src/chat/group.rs b/host/src/chat/group.rs
index 203b4532..4d3ad098 100644
--- a/host/src/chat/group.rs
+++ b/host/src/chat/group.rs
@@ -35,6 +35,8 @@ const PASS: &str = "(pass)";
pub struct GroupTurn {
pub lane: Lane,
pub prompt: String,
+ /// The room turn that asked; a member skips it once a newer one started (see [`Rooms::current`]).
+ pub epoch: u64,
/// The reply the room sees, or `None` for a pass.
pub reply: oneshot::Sender<Result<Option<String>>>,
}
@@ -51,7 +53,7 @@ impl Rooms {
*epoch
}
- fn current(&self, lane: &Lane, epoch: u64) -> bool {
+ pub fn current(&self, lane: &Lane, epoch: u64) -> bool {
self.0.locked().get(&lane.key()).copied() == Some(epoch)
}
@@ -119,7 +121,7 @@ pub fn start(hub: &Arc<Hub>, group: &BotConfig, lane: Lane) {
async fn run(hub: &Arc<Hub>, group_id: &str, lane: &Lane, epoch: u64) -> Result<()> {
let mut replies = 0;
let mut last_reply: Option<(String, String, String)> = None;
- for round in 0..MAX_ROUNDS {
+ 'rounds: for round in 0..MAX_ROUNDS {
let group = hub.store.bot(group_id)?.filter(|r| !r.deleted).ok_or_else(|| anyhow!("group deleted"))?.config;
let members = members(hub, &group);
let since_user = since_last_user_message(hub, lane)?;
@@ -131,12 +133,17 @@ async fn run(hub: &Arc<Hub>, group_id: &str, lane: &Lane, epoch: u64) -> Result<
}
let mut spoke = 0;
for member in &speakers {
- if !hub.groups.current(lane, epoch) || replies >= MAX_REPLIES {
+ if !hub.groups.current(lane, epoch) {
return Ok(());
}
+ // A full room turn still reports its last reply.
+ if replies >= MAX_REPLIES {
+ break 'rounds;
+ }
let prompt = member_prompt(hub, &group, &members, member, lane)?;
let (reply, answer) = oneshot::channel();
- if hub.send_cmd(&member.id, Cmd::Group(GroupTurn { lane: lane.clone(), prompt, reply })).is_err() {
+ let turn = GroupTurn { lane: lane.clone(), prompt, epoch, reply };
+ if hub.send_cmd(&member.id, Cmd::Group(turn)).is_err() {
continue;
}
// A failed, cancelled or overdue member turn counts as a pass; its error shows in the room.
diff --git a/host/src/chat/memory/engram/install.rs b/host/src/chat/memory/engram/install.rs
index 9d45765f..22941a31 100644
--- a/host/src/chat/memory/engram/install.rs
+++ b/host/src/chat/memory/engram/install.rs
@@ -1,13 +1,22 @@
//! Install one checksum-pinned binary, shared by every bot on this host.
use super::VERSION;
+use crate::LockExt;
use anyhow::{Context, Result, bail, ensure};
use sha2::{Digest, Sha256};
-use std::{fmt::Write as _, path::PathBuf, time::Duration};
+use std::{
+ fmt::Write as _,
+ path::PathBuf,
+ sync::Mutex,
+ time::{Duration, Instant},
+};
const RELEASES: &str = "https://github.com/Gentleman-Programming/engram/releases/download";
const MAX_ARCHIVE_BYTES: usize = 128 * 1024 * 1024;
static INSTALL: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+/// When the last download failed, and how long until another is tried.
+static FAILED_AT: Mutex<Option<Instant>> = Mutex::new(None);
+const RETRY_AFTER: Duration = Duration::from_secs(5 * 60);
pub(super) async fn binary() -> Result<PathBuf> {
#[cfg(test)]
@@ -21,6 +30,18 @@ pub(super) async fn binary() -> Result<PathBuf> {
if tokio::fs::try_exists(root.join(".installed")).await? && tokio::fs::try_exists(&binary).await? {
return Ok(binary);
}
+ // Every turn asks for memory: after a failed download (offline), don't make each one wait on another.
+ if FAILED_AT.locked().is_some_and(|at| at.elapsed() < RETRY_AFTER) {
+ bail!("Engram couldn't be downloaded; trying again in a few minutes");
+ }
+ let installed = install(root, name).await;
+ if installed.is_err() {
+ *FAILED_AT.locked() = Some(Instant::now());
+ }
+ installed.map(|()| binary)
+}
+
+async fn install(root: PathBuf, name: &str) -> Result<()> {
let (platform, checksum) = distribution(std::env::consts::OS, std::env::consts::ARCH)?;
let extension = if cfg!(windows) { "zip" } else { "tar.gz" };
let archive = format!("engram_{VERSION}_{platform}.{extension}");
@@ -37,7 +58,7 @@ pub(super) async fn binary() -> Result<PathBuf> {
}
verify(&bytes, &checksum)?;
let staging = root.with_extension(format!("staging-{}", uuid::Uuid::new_v4()));
- let (target, stage, executable) = (root.clone(), staging.clone(), name.to_owned());
+ let (target, stage, executable) = (root, staging.clone(), name.to_owned());
let result = tokio::task::spawn_blocking(move || -> Result<()> {
std::fs::create_dir_all(&stage)?;
let file = stage.join(&archive);
@@ -64,8 +85,7 @@ pub(super) async fn binary() -> Result<PathBuf> {
{
tracing::warn!(%error, "could not remove incomplete Engram installation");
}
- result.context("installing Engram")?;
- Ok(binary)
+ result.context("installing Engram")
}
fn verify(bytes: &[u8], expected: &str) -> Result<()> {
diff --git a/host/src/chat/memory/engram/migrate.rs b/host/src/chat/memory/engram/migrate.rs
index cdb493d9..001abffc 100644
--- a/host/src/chat/memory/engram/migrate.rs
+++ b/host/src/chat/memory/engram/migrate.rs
@@ -24,7 +24,7 @@ pub(super) async fn run(binary: &Path, dir: &Path, project: &str) -> Result<()>
tokio::fs::rename(&staging, &backup).await?;
}
let files: BTreeMap<String, String> = serde_json::from_slice(&tokio::fs::read(&backup).await?)?;
- let payload = bundle(project, &files)?;
+ let payload = bundle(project, &files);
let count = payload["observations"].as_array().map_or(0, Vec::len);
if count > 0 {
let input = dir.join("markdown-import.json");
@@ -95,7 +95,7 @@ fn read_legacy(dir: &Path) -> Result<BTreeMap<String, String>> {
Ok(files)
}
-fn bundle(project: &str, files: &BTreeMap<String, String>) -> Result<Value> {
+fn bundle(project: &str, files: &BTreeMap<String, String>) -> Value {
let session = format!("{project}:markdown-import");
let mut observations = Vec::new();
for (path, text) in files {
@@ -103,9 +103,15 @@ fn bundle(project: &str, files: &BTreeMap<String, String>) -> Result<Value> {
let Some(rest) = raw.trim().strip_prefix("- (") else {
continue;
};
- let (date, content) = rest.split_once(") ").context("malformed dated memory; fix it before importing")?;
- let date = chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d").context("invalid date in memory")?;
- ensure!(!content.trim().is_empty(), "empty memory in {path}:{}", line + 1);
+ // The backup is frozen, so a line that can't be read must not block the import forever:
+ // a malformed one is kept whole, dated by the import.
+ let dated = rest.split_once(')').and_then(|(date, content)| {
+ Some((chrono::NaiveDate::parse_from_str(date, "%Y-%m-%d").ok()?, content.trim()))
+ });
+ let (date, content) = dated.unwrap_or_else(|| (chrono::Utc::now().date_naive(), &raw.trim()[2..]));
+ if content.is_empty() {
+ continue;
+ }
let timestamp = format!("{date} 00:00:00");
let profile = path == "profile.md";
let source = format!("{project}\n{path}\n{}\n{raw}", line + 1);
@@ -125,12 +131,12 @@ fn bundle(project: &str, files: &BTreeMap<String, String>) -> Result<Value> {
}
}
let start = observations.first().and_then(|o| o["created_at"].as_str()).unwrap_or("1970-01-01 00:00:00");
- Ok(json!({
+ json!({
"version": "0.2.0", "exported_at": chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
"sessions": [{"id": session, "project": project, "directory": "", "started_at": start,
"ended_at": start, "summary": "Imported Codync Markdown memory; original files preserved in markdown-backup.json"}],
"observations": observations, "prompts": [],
- }))
+ })
}
#[cfg(test)]
@@ -143,19 +149,23 @@ mod tests {
("profile.md".into(), "# Profile\n- (2026-01-01) 喜歡繁體中文\n".into()),
("log/2026-02.md".into(), "- (2026-02-02) 發布 Codync\n".into()),
]);
- let a = bundle("bot-a", &files).unwrap();
- let b = bundle("bot-a", &files).unwrap();
+ let a = bundle("bot-a", &files);
+ let b = bundle("bot-a", &files);
assert_eq!(a["observations"], b["observations"]);
let profile = &a["observations"][1];
assert_eq!(profile["created_at"], "2026-01-01 00:00:00");
assert_eq!(profile["content"], "喜歡繁體中文");
assert_eq!(profile["tool_name"], "codync-import:profile.md:2");
- assert_ne!(profile["sync_id"], bundle("bot-b", &files).unwrap()["observations"][1]["sync_id"]);
+ assert_ne!(profile["sync_id"], bundle("bot-b", &files)["observations"][1]["sync_id"]);
}
#[test]
- fn malformed_dated_fact_stops_import_instead_of_disappearing() {
- let files = BTreeMap::from([("profile.md".into(), "- (2026-02-31) fact".into())]);
- assert!(bundle("bot", &files).is_err());
+ fn malformed_dated_fact_is_imported_whole_instead_of_blocking() {
+ let files =
+ BTreeMap::from([("profile.md".into(), "- (2026-02-31) fact\n- (no date\n- (2026-01-01) \n".into())]);
+ let payload = bundle("bot", &files);
+ let contents: Vec<&str> =
+ payload["observations"].as_array().unwrap().iter().filter_map(|o| o["content"].as_str()).collect();
+ assert_eq!(contents, ["(2026-02-31) fact", "(no date"]);
}
}
diff --git a/host/src/chat/memory/keeper.rs b/host/src/chat/memory/keeper.rs
index c398e77d..3e503097 100644
--- a/host/src/chat/memory/keeper.rs
+++ b/host/src/chat/memory/keeper.rs
@@ -7,7 +7,9 @@ use super::extract::{
pending_episode, set_pending_episode,
};
use super::facts::{Kind, Memory, dedupe_key};
-use super::{EXCHANGE_CHARS, KEEPER_BATCH, KEEPER_IDLE, KEEPER_TIMEOUT, NONE, PROFILE_PROMPT_LIMIT};
+use super::{
+ EXCHANGE_CHARS, KEEPER_BATCH, KEEPER_IDLE, KEEPER_QUEUE_LIMIT, KEEPER_TIMEOUT, NONE, PROFILE_PROMPT_LIMIT,
+};
use crate::agent::acp::{self, Acp, Incoming};
use crate::hub::Hub;
use crate::store::{BotConfig, Store};
@@ -52,12 +54,19 @@ pub fn spawn_keeper(hub: Arc<Hub>, bot_id: String) -> mpsc::UnboundedSender<Keep
let mut queued = unprocessed(&hub.store, &bot_id);
let idle = tokio::time::sleep(KEEPER_IDLE);
tokio::pin!(idle);
+ // After a failed run the next one waits, longer each time, instead of retrying on every exchange.
+ let mut failures = 0u32;
+ let mut retry_at: Option<tokio::time::Instant> = None;
+ let quiet_until = |retry_at: Option<tokio::time::Instant>| {
+ let after = tokio::time::Instant::now() + KEEPER_IDLE;
+ retry_at.map_or(after, |at| at.max(after))
+ };
loop {
let flush = tokio::select! {
x = rx.recv() => {
let Some(event) = x else { break };
let KeeperEvent::Exchange(x) = event else {
- idle.as_mut().reset(tokio::time::Instant::now());
+ idle.as_mut().reset(retry_at.unwrap_or_else(tokio::time::Instant::now));
continue;
};
if x.revision != super::maintenance::revision(&hub.store, &bot_id) { continue; }
@@ -73,6 +82,10 @@ pub fn spawn_keeper(hub: Arc<Hub>, bot_id: String) -> mpsc::UnboundedSender<Keep
// Re-read: another keeper of this bot (before a restart) may have flushed.
queued = unprocessed(&hub.store, &bot_id);
queued.push(turn.clone());
+ // ponytail: while the keeper can't run, only the newest exchanges wait for it (the
+ // transcript keeps them all); keep a longer backlog if older ones must still be learned.
+ let over = queued.len().saturating_sub(KEEPER_QUEUE_LIMIT);
+ queued.drain(..over);
if let Err(e) = hub.store.kv_set(&unprocessed_key(&bot_id), &serde_json::to_string(&queued).unwrap_or_default()) {
tracing::warn!(bot = %bot_id, error = format!("{e:#}"), "couldn't queue an exchange for memory");
}
@@ -80,8 +93,8 @@ pub fn spawn_keeper(hub: Arc<Hub>, bot_id: String) -> mpsc::UnboundedSender<Keep
if let Err(e) = crate::chat::naming::observe(&hub, &bot_id, turn.clone()).await {
tracing::warn!(bot = %bot_id, error = format!("{e:#}"), "naming the bot failed");
}
- idle.as_mut().reset(tokio::time::Instant::now() + KEEPER_IDLE);
- queued.len() >= KEEPER_BATCH
+ idle.as_mut().reset(quiet_until(retry_at));
+ queued.len() >= KEEPER_BATCH && retry_at.is_none()
}
() = &mut idle, if !queued.is_empty() => true,
};
@@ -90,13 +103,16 @@ pub fn spawn_keeper(hub: Arc<Hub>, bot_id: String) -> mpsc::UnboundedSender<Keep
}
let processing = super::maintenance::keeper_lock(&bot_id);
let _processing = processing.lock().await;
- queued = unprocessed(&hub.store, &bot_id);
- match remember(&hub, &bot_id, queued.clone()).await {
+ // One batch per run keeps the keeper's prompt bounded; the rest follows right after.
+ let batch: Vec<EpisodeTurn> = unprocessed(&hub.store, &bot_id).into_iter().take(KEEPER_BATCH).collect();
+ match remember(&hub, &bot_id, batch.clone()).await {
Ok(()) => {
+ failures = 0;
+ retry_at = None;
let mutation = super::maintenance::lock(&bot_id);
let _guard = mutation.lock().await;
let mut current = unprocessed(&hub.store, &bot_id);
- acknowledge(&mut current, &queued);
+ acknowledge(&mut current, &batch);
if let Err(e) = hub
.store
.kv_set(&unprocessed_key(&bot_id), &serde_json::to_string(&current).unwrap_or_default())
@@ -107,10 +123,17 @@ pub fn spawn_keeper(hub: Arc<Hub>, bot_id: String) -> mpsc::UnboundedSender<Keep
}
}
Err(e) => {
- tracing::warn!(bot = %bot_id, error = format!("{e:#}"), "memory keeper failed; batch retained for retry");
+ failures += 1;
+ retry_at = Some(tokio::time::Instant::now() + KEEPER_IDLE * 2u32.pow(failures.min(5)));
+ tracing::warn!(bot = %bot_id, failures, error = format!("{e:#}"), "memory keeper failed; batch retained for retry");
}
}
- idle.as_mut().reset(tokio::time::Instant::now() + KEEPER_IDLE);
+ let next = if retry_at.is_none() && queued.len() >= KEEPER_BATCH {
+ tokio::time::Instant::now()
+ } else {
+ quiet_until(retry_at)
+ };
+ idle.as_mut().reset(next);
}
});
tx
diff --git a/host/src/chat/memory/maintenance.rs b/host/src/chat/memory/maintenance.rs
index a0e58188..706ffdab 100644
--- a/host/src/chat/memory/maintenance.rs
+++ b/host/src/chat/memory/maintenance.rs
@@ -24,12 +24,11 @@ pub fn revision(store: &Store, bot: &str) -> String {
store.kv_get(&format!("memory.revision.{bot}")).unwrap_or_default()
}
+/// A correction (edit, forget, clear): it supersedes what the keeper is extracting from older
+/// context, sessions are told memory changed, and their instructions re-render.
pub fn changed(store: &Store, bot: &str) -> Result<()> {
store.kv_set(&format!("memory.revision.{bot}"), &uuid::Uuid::new_v4().to_string())?;
- for key in store.kv_prefix(&format!("context.{bot}.")) {
- store.kv_set(&key, "")?;
- }
- Ok(())
+ crate::chat::context::invalidate(store, bot)
}
pub fn discard_pending(store: &Store, bot: &str) -> Result<()> {
diff --git a/host/src/chat/memory/manage.rs b/host/src/chat/memory/manage.rs
index ddcda71d..657d0bc4 100644
--- a/host/src/chat/memory/manage.rs
+++ b/host/src/chat/memory/manage.rs
@@ -9,9 +9,10 @@ use std::sync::Arc;
pub async fn call(hub: &Arc<Hub>, method: &str, args: &Value) -> Result<Value> {
let bot = args["botId"].as_str().context("botId is required")?.to_owned();
let row = hub.store.bot(&bot)?.filter(|b| !b.deleted && !b.config.is_group()).context("unknown bot")?;
- let mutates = !matches!(method, "memory" | "memoryDetail" | "exportMemory");
let clears = method == "clearMemory";
let corrects = matches!(method, "saveMemory" | "forgetMemory");
+ // Not corrections: the keeper's work stands, only what the instructions show changes.
+ let reorders = matches!(method, "pinMemory" | "importMemory");
let mutation = maintenance::lock(&bot);
let _guard = mutation.lock().await;
if clears {
@@ -50,7 +51,7 @@ pub async fn call(hub: &Arc<Hub>, method: &str, args: &Value) -> Result<Value> {
}
};
let result = super::engram::import(&bot, &data).await?;
- maintenance::changed(&hub.store, &bot)?;
+ crate::chat::context::invalidate(&hub.store, &bot)?;
return Ok(result);
}
let (method, args, id) = (method.to_owned(), args.clone(), bot.clone());
@@ -59,11 +60,11 @@ pub async fn call(hub: &Arc<Hub>, method: &str, args: &Value) -> Result<Value> {
dispatch(&memory, &method, &args)
})
.await??;
- if mutates {
- maintenance::changed(&hub.store, &bot)?;
- }
if clears || corrects {
+ maintenance::changed(&hub.store, &bot)?;
maintenance::discard_pending(&hub.store, &bot)?;
+ } else if reorders {
+ crate::chat::context::invalidate(&hub.store, &bot)?;
}
Ok(result)
}
diff --git a/host/src/chat/memory/mod.rs b/host/src/chat/memory/mod.rs
index 8cba2d85..2ed45d77 100644
--- a/host/src/chat/memory/mod.rs
+++ b/host/src/chat/memory/mod.rs
@@ -37,8 +37,10 @@ const EXCHANGE_CHARS: usize = 8_000;
const KEEPER_TIMEOUT: Duration = Duration::from_secs(180);
/// Quiet time after the last exchange before the keeper runs over the queued ones.
const KEEPER_IDLE: Duration = Duration::from_secs(5 * 60);
-/// Queued exchanges that make the keeper run without waiting.
+/// Queued exchanges that make the keeper run without waiting, and the most one run takes.
const KEEPER_BATCH: usize = 8;
+/// Exchanges kept waiting while the keeper fails; older ones are dropped.
+const KEEPER_QUEUE_LIMIT: usize = 4 * KEEPER_BATCH;
const NOTE_PREFIX: &str = "[note] ";
const NONE: &str = "NONE";
@@ -68,11 +70,16 @@ pub fn render(recall: &Recall, location: &Path) -> (String, bool) {
let mut budget = RECENT_CHAR_BUDGET;
let mut shown = 0;
for f in &recall.recent {
- let line = fact_line(f);
- if shown > 0 && line.len() > budget {
+ // Characters, not bytes, so CJK facts get the same room; a long one (a session summary) is clipped.
+ let mut line = fact_line(f);
+ if line.chars().count() > MAX_FACT_CHARS {
+ line = line.chars().take(MAX_FACT_CHARS).chain(['…']).collect();
+ }
+ let size = line.chars().count();
+ if size > budget {
break;
}
- budget = budget.saturating_sub(line.len());
+ budget -= size;
lines.push(line);
shown += 1;
}
@@ -92,3 +99,37 @@ pub fn render(recall: &Recall, location: &Path) -> (String, bool) {
pub async fn prepare(bot_id: &str) -> Result<()> {
engram::prepare(bot_id).await
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn fact(content: &str) -> Fact {
+ Fact {
+ id: "1".into(),
+ content: content.into(),
+ created_at: 0,
+ kind: facts::Kind::Log,
+ title: String::new(),
+ memory_type: "discovery".into(),
+ scope: "project".into(),
+ project: String::new(),
+ topic_key: None,
+ session_id: String::new(),
+ pinned: false,
+ review_after: None,
+ revision_count: 1,
+ source: None,
+ }
+ }
+
+ #[test]
+ fn recent_memories_share_a_character_budget() {
+ let mut recent = vec![fact(&"摘".repeat(9_000))];
+ recent.extend((0..20).map(|i| fact(&format!("使用者偏好繁體中文 {i}"))));
+ let (text, _) = render(&Recall { profile: vec![], recent }, Path::new("/tmp"));
+ let facts: Vec<&str> = text.lines().filter(|l| l.starts_with("- (learned")).collect();
+ assert!(facts.iter().all(|l| l.chars().count() <= MAX_FACT_CHARS + 1), "a long memory is clipped");
+ assert_eq!(facts.len(), 21, "short CJK facts still fit after a long summary");
+ }
+}
diff --git a/host/src/hub.rs b/host/src/hub.rs
index 2db7d7fa..e64c3cb1 100644
--- a/host/src/hub.rs
+++ b/host/src/hub.rs
@@ -308,11 +308,14 @@ impl Hub {
ReadScope::Chat => self.store.mark_read(id)?,
ReadScope::Thread(root) => self.store.mark_thread_read(id, root)?,
};
+ // Before the summaries' later revs: a client resuming after them must not miss the row.
+ if let Some(v) = self.bot_value(id) {
+ let _ = self.events.send(json!({"type": "bot", "rev": v["rev"], "bot": v}));
+ }
}
for root in roots {
self.refresh_thread_summary(id, &root);
}
- self.emit_bot_row(id);
Ok(())
}
@@ -335,13 +338,6 @@ impl Hub {
}
}
- fn emit_bot_row(&self, id: &str) {
- let _g = self.emit_lock.locked();
- if let Some(v) = self.bot_value(id) {
- let _ = self.events.send(json!({"type": "bot", "rev": v["rev"], "bot": v}));
- }
- }
-
pub fn set_runtime(&self, id: &str, f: impl FnOnce(&mut Runtime)) {
let (before, after) = {
let mut map = self.runtime.locked();
diff --git a/host/src/mcp.rs b/host/src/mcp.rs
index 59567dca..ae12899f 100644
--- a/host/src/mcp.rs
+++ b/host/src/mcp.rs
@@ -4,8 +4,9 @@
use anyhow::Result;
use serde_json::{Value, json};
-use std::time::Duration;
+use std::{sync::Arc, time::Duration};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
+use tokio::sync::mpsc;
pub const PROTOCOL_VERSION: &str = "2025-06-18";
@@ -29,9 +30,21 @@ Don't approve payments: ask the user to do that from their phone.";
/// Serves MCP on stdio until the agent closes it.
pub async fn serve(bot: String, port: u16, server: Server) -> Result<()> {
let token = std::fs::read_to_string(crate::service::data_dir().join("token")).unwrap_or_default();
- let token = token.trim().to_owned();
+ let token: Arc<str> = token.trim().into();
+ let bot: Arc<str> = bot.into();
let mut lines = BufReader::new(tokio::io::stdin()).lines();
- let mut out = tokio::io::stdout();
+ // Requests run side by side (an ask_bot can take minutes); their replies share stdout.
+ let (replies, mut outbox) = mpsc::unbounded_channel::<Value>();
+ tokio::spawn(async move {
+ let mut out = tokio::io::stdout();
+ while let Some(msg) = outbox.recv().await {
+ let mut line = serde_json::to_vec(&msg)?;
+ line.push(b'\n');
+ out.write_all(&line).await?;
+ out.flush().await?;
+ }
+ anyhow::Ok(())
+ });
let (name, instructions, available_tools) = match server {
Server::Connectors => {
("codync-connectors", crate::market::requests::INSTRUCTIONS, crate::market::requests::tools())
@@ -51,32 +64,37 @@ pub async fn serve(bot: String, port: u16, server: Server) -> Result<()> {
let Some(id) = msg.get("id").filter(|id| !id.is_null()).cloned() else {
continue; // notifications (`notifications/initialized`, cancellations)
};
- let params = &msg["params"];
- let reply = match msg["method"].as_str().unwrap_or_default() {
- "initialize" => Ok(json!({
- "protocolVersion": params["protocolVersion"].as_str().unwrap_or(PROTOCOL_VERSION),
- "capabilities": {"tools": {}},
- "serverInfo": {"name": name, "version": env!("CARGO_PKG_VERSION")},
- "instructions": instructions,
- })),
- "ping" => Ok(json!({})),
- "tools/list" => Ok(match server {
- Server::Computer => computer_tools(port, &token).await,
- Server::Memory => memory_tools(port, &token, &bot).await?,
- _ => json!({"tools": available_tools}),
- }),
- "tools/call" => Ok(call(port, &token, &bot, params, server).await),
- method => Err(json!({"code": -32601, "message": format!("unknown method {method}")})),
- };
- let msg = match reply {
- Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
- Err(error) => json!({"jsonrpc": "2.0", "id": id, "error": error}),
- };
- let mut line = serde_json::to_vec(&msg)?;
- line.push(b'\n');
- out.write_all(&line).await?;
- out.flush().await?;
+ let (replies, token, bot, available_tools) =
+ (replies.clone(), token.clone(), bot.clone(), available_tools.clone());
+ tokio::spawn(async move {
+ let params = &msg["params"];
+ let reply = match msg["method"].as_str().unwrap_or_default() {
+ "initialize" => Ok(json!({
+ "protocolVersion": params["protocolVersion"].as_str().unwrap_or(PROTOCOL_VERSION),
+ "capabilities": {"tools": {}},
+ "serverInfo": {"name": name, "version": env!("CARGO_PKG_VERSION")},
+ "instructions": instructions,
+ })),
+ "ping" => Ok(json!({})),
+ "tools/list" => Ok(match server {
+ Server::Computer => computer_tools(port, &token).await,
+ // Without Engram, history search still works.
+ Server::Memory => memory_tools(port, &token, &bot).await.unwrap_or_else(|error| {
+ tracing::warn!(error = format!("{error:#}"), "memory tools unavailable");
+ json!({"tools": available_tools})
+ }),
+ _ => json!({"tools": available_tools}),
+ }),
+ "tools/call" => Ok(call(port, &token, &bot, params, server).await),
+ method => Err(json!({"code": -32601, "message": format!("unknown method {method}")})),
+ };
+ let _ = replies.send(match reply {
+ Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
+ Err(error) => json!({"jsonrpc": "2.0", "id": id, "error": error}),
+ });
+ });
}
+ // The agent is gone: replies still pending have no one to read them.
Ok(())
}
@@ -163,7 +181,7 @@ pub async fn serve_remote(connector: String, port: u16) -> Result<()> {
let token = std::fs::read_to_string(crate::service::data_dir().join("token")).unwrap_or_default();
let remote = Remote { port, token: token.trim().to_owned(), connector };
// Both transports answer through one writer: the SSE one answers from its own stream.
- let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<Value>();
+ let (tx, mut rx) = mpsc::unbounded_channel::<Value>();
let writer = tokio::spawn(async move {
let mut out = tokio::io::stdout();
while let Some(reply) = rx.recv().await {
@@ -306,10 +324,7 @@ impl Remote {
/// Opens the older transport's event stream, which carries every answer from now on
/// (sent to `tx`); returns the URL messages are posted to and the task reading the stream.
- async fn open_legacy(
- &self,
- tx: tokio::sync::mpsc::UnboundedSender<Value>,
- ) -> Result<(String, tokio::task::JoinHandle<()>)> {
+ async fn open_legacy(&self, tx: mpsc::UnboundedSender<Value>) -> Result<(String, tokio::task::JoinHandle<()>)> {
for stale in [false, true] {
let (url, headers) = self.target(stale).await?;
let res = crate::http()
diff --git a/host/src/store/bots.rs b/host/src/store/bots.rs
index 10753cab..cf9f9343 100644
--- a/host/src/store/bots.rs
+++ b/host/src/store/bots.rs
@@ -46,7 +46,7 @@ impl Store {
let rev = next_rev(&c)?;
c.execute(
"INSERT INTO bots(id, rev, config) VALUES(?1, ?2, ?3)
- ON CONFLICT(id) DO UPDATE SET rev = ?2, config = ?3, deleted = 0",
+ ON CONFLICT(id) DO UPDATE SET rev = ?2, config = ?3 WHERE deleted = 0",
params![cfg.id, rev, serde_json::to_string(cfg)?],
)?;
Ok(rev)
@@ -81,10 +81,17 @@ impl Store {
Ok(())
}
- /// Main chat read: bumps the bot's `read_rev`.
+ /// Main chat read: bumps the bot's `read_rev`. A thread never opened counts from `read_rev`
+ /// (see `unread`), so it keeps the old one: reading the chat doesn't read its threads.
pub fn mark_read(&self, id: &str) -> Result<i64> {
let c = self.db.locked();
let rev = next_rev(&c)?;
+ c.execute(
+ "INSERT OR IGNORE INTO kv(k, v)
+ SELECT DISTINCT 'read.' || thread_id, (SELECT read_rev FROM bots WHERE id = ?1)
+ FROM entries WHERE bot_id = ?1 AND thread_id IS NOT NULL",
+ [id],
+ )?;
c.execute("UPDATE bots SET read_rev = ?2, rev = ?2 WHERE id = ?1", params![id, rev])?;
Ok(rev)
}
@@ -233,5 +240,18 @@ mod tests {
assert_eq!(s.history("b1", a.seq, 10).unwrap().len(), 1);
assert_eq!(s.bot("b1").unwrap().unwrap().config.permission, Permission::Ask);
assert!(s.bot("missing").unwrap().is_none());
+
+ // Reading the chat leaves a thread nobody opened unread.
+ let reply = serde_json::json!({"text": "done", "final": true});
+ s.insert_entry(&Lane::in_thread("b1", &u.id), EntryKind::Agent, 2, &reply).unwrap();
+ let r = s.mark_read("b1").unwrap();
+ assert_eq!(s.unread("b1", r, ReadScope::Thread(&u.id)), 1);
+ assert_eq!(s.unread("b1", r, ReadScope::All), 1);
+
+ // A deleted bot stays deleted, and its winding-down actor leaves no lines behind.
+ s.delete_bot("b1").unwrap();
+ assert!(s.insert_entry(&Lane::main("b1"), EntryKind::Notice, 3, &reply).is_err());
+ s.save_bot(&cfg).unwrap();
+ assert!(s.bot("b1").unwrap().unwrap().deleted);
}
}
diff --git a/host/src/store/entries.rs b/host/src/store/entries.rs
index cbd8b42b..ca606575 100644
--- a/host/src/store/entries.rs
+++ b/host/src/store/entries.rs
@@ -3,7 +3,7 @@
use super::model::{Entry, EntryKind, Lane, ReadScope};
use super::{Store, logged, next_rev, now_ms};
use crate::LockExt;
-use anyhow::Result;
+use anyhow::{Result, ensure};
use rusqlite::{OptionalExtension, params};
use serde_json::Value;
use std::fmt::Write as _;
@@ -25,6 +25,8 @@ fn row_entry(r: &rusqlite::Row) -> rusqlite::Result<Entry> {
}
const ENTRY_COLS: &str = "seq, id, bot_id, rev, kind, turn, data, created_at, updated_at, thread_id";
+/// A bot still winding down after its deletion can't leave lines behind (`?2` is the chat).
+const LIVE_CHAT: &str = "WHERE NOT EXISTS (SELECT 1 FROM bots WHERE id = ?2 AND deleted = 1)";
impl Store {
pub fn insert_entry(&self, lane: &Lane, kind: EntryKind, turn: i64, data: &Value) -> Result<Entry> {
@@ -32,11 +34,14 @@ impl Store {
let rev = next_rev(&c)?;
let now = now_ms();
let id = uuid::Uuid::new_v4().to_string();
- c.execute(
- "INSERT INTO entries(id, bot_id, thread_id, rev, kind, turn, data, created_at, updated_at)
- VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?8)",
+ let added = c.execute(
+ &format!(
+ "INSERT INTO entries(id, bot_id, thread_id, rev, kind, turn, data, created_at, updated_at)
+ SELECT ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?8 {LIVE_CHAT}"
+ ),
params![id, lane.chat, lane.thread, rev, kind.as_str(), turn, data.to_string(), now],
)?;
+ ensure!(added == 1, "the chat was deleted");
Ok(Entry {
id,
seq: c.last_insert_rowid(),
@@ -62,8 +67,9 @@ impl Store {
}
let rev = next_rev(&tx)?;
let now = now_ms();
- tx.execute("INSERT INTO entries(id, bot_id, thread_id, rev, kind, turn, data, created_at, updated_at) VALUES(?1, ?2, ?3, ?4, ?5, 0, ?6, ?7, ?7)",
+ let added = tx.execute(&format!("INSERT INTO entries(id, bot_id, thread_id, rev, kind, turn, data, created_at, updated_at) SELECT ?1, ?2, ?3, ?4, ?5, 0, ?6, ?7, ?7 {LIVE_CHAT}"),
params![id, lane.chat, lane.thread, rev, kind.as_str(), data.to_string(), now])?;
+ ensure!(added == 1, "the chat was deleted");
let entry = tx.query_row(&format!("SELECT {ENTRY_COLS} FROM entries WHERE id = ?"), [id], row_entry)?;
tx.commit()?;
Ok((entry, true))
diff --git a/host/src/tui/app/events.rs b/host/src/tui/app/events.rs
index 1999ad73..1a673230 100644
--- a/host/src/tui/app/events.rs
+++ b/host/src/tui/app/events.rs
@@ -119,7 +119,8 @@ impl App {
let Some(new) = Bot::parse(v) else { return };
// Marked read once per change: an open thread leaves the main chat's replies unread.
if self.bots.get(&new.id).is_none_or(|old| old.unread != new.unread) {
- self.reading.remove(&new.id);
+ let thread = format!("{}/", new.id);
+ self.reading.retain(|scope| *scope != new.id && !scope.starts_with(&thread));
}
let old = self.bots.insert(new.id.clone(), new.clone());
let Some(old) = old else { return };
@@ -319,7 +320,10 @@ impl App {
self.files.remove(key);
}
self.sends.remove(key);
- self.on_event(&json!({"type": "entry", "entry": value["entry"]}));
+ // The stream may already hold a newer copy (its delivery status).
+ if let Some((bot, e)) = Entry::parse(&value["entry"]) {
+ self.entries.entry(bot).or_default().entry(e.seq).or_insert(e);
+ }
self.flash("Sent");
}
Err(error) => self.flash(&format!("{error} Draft kept; Enter retries, Ctrl+C clears text.")),
diff --git a/host/src/tui/app/mod.rs b/host/src/tui/app/mod.rs
index 397cd0d7..c16884f9 100644
--- a/host/src/tui/app/mod.rs
+++ b/host/src/tui/app/mod.rs
@@ -90,6 +90,7 @@ pub struct App {
pub models: HashMap<String, Option<Vec<(String, String)>>>,
/// A setup terminal holding the screen.
pub shell: Option<Shell>,
+ /// Chats and threads (`bot` / `bot/root`) marked read since their bot's unread count last changed.
reading: HashSet<String>,
history_busy: HashSet<String>,
/// Permission cards whose answer is on its way, with the chosen option.
@@ -353,11 +354,12 @@ impl App {
&& self.term_focused
&& let Some(b) = self.bot()
&& b.unread > 0
- && !self.reading.contains(&b.id)
{
let id = b.id.clone();
- self.reading.insert(id.clone());
- self.call("markRead", json!({"botId": id, "threadId": self.thread}), After::Nothing);
+ let scope = self.thread.as_ref().map_or_else(|| id.clone(), |root| format!("{id}/{root}"));
+ if self.reading.insert(scope) {
+ self.call("markRead", json!({"botId": id, "threadId": self.thread}), After::Nothing);
+ }
}
if self.chat_top && self.chat_visible() && self.thread.is_none() {
self.load_history();
@@ -485,4 +487,19 @@ mod tests {
assert_eq!(ids(&app), ["e2"]);
assert_eq!(app.lane_turns("g"), [2]);
}
+
+ #[tokio::test]
+ async fn a_thread_opened_after_its_chat_is_marked_read_too() {
+ let mut app = test_app();
+ let bot = Bot::parse(&json!({"id": "b", "name": "Bot", "unread": 2})).expect("a bot with an id parses");
+ app.bots.insert("b".into(), bot);
+ app.selected = Some("b".into());
+ (app.width, app.term_focused) = (Width::Wide, true);
+ app.tick();
+ assert!(app.reading.contains("b"));
+ // Reading the chat left the thread's replies unread: opening it reads them.
+ app.thread = Some("r".into());
+ app.tick();
+ assert!(app.reading.contains("b/r"));
+ }
}
diff --git a/host/tests/fixtures/stubborn_agent.py b/host/tests/fixtures/stubborn_agent.py
new file mode 100644
index 00000000..b0ba1092
--- /dev/null
+++ b/host/tests/fixtures/stubborn_agent.py
@@ -0,0 +1,22 @@
+"""ACP agent stuck mid-turn: it never answers a prompt and ignores session/cancel."""
+
+import json
+import sys
+
+
+def send(value):
+ sys.stdout.write(json.dumps({"jsonrpc": "2.0", **value}) + "\n")
+ sys.stdout.flush()
+
+
+for line in sys.stdin:
+ message = json.loads(line)
+ method = message.get("method")
+ if method == "initialize":
+ send({"id": message["id"], "result": {"protocolVersion": 1, "agentCapabilities": {"loadSession": False}}})
+ elif method == "session/new":
+ send({"id": message["id"], "result": {"sessionId": "stuck"}})
+ elif method == "session/prompt":
+ text = {"type": "text", "text": "Working on it"}
+ update = {"sessionUpdate": "agent_message_chunk", "content": text}
+ send({"method": "session/update", "params": {"sessionId": "stuck", "update": update}})
diff --git a/host/tests/turns_e2e.rs b/host/tests/turns_e2e.rs
new file mode 100644
index 00000000..ec2d648d
--- /dev/null
+++ b/host/tests/turns_e2e.rs
@@ -0,0 +1,155 @@
+//! A real host driving scripted ACP agents: Stop on an agent that ignores it, and room turns
+//! that a newer group message replaced.
+
+#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
+
+mod common;
+
+use serde_json::{Value, json};
+use std::path::PathBuf;
+use std::process::{Child, Command, Stdio};
+use std::time::{Duration, Instant};
+
+struct Host {
+ child: Child,
+ base: String,
+ token: String,
+ home: PathBuf,
+}
+
+impl Drop for Host {
+ fn drop(&mut self) {
+ common::stop(&mut self.child);
+ let _ = std::fs::remove_dir_all(&self.home);
+ }
+}
+
+impl Host {
+ async fn start() -> Self {
+ let home = std::env::temp_dir().join(format!("codync-turns-e2e-{}", uuid::Uuid::new_v4()));
+ std::fs::create_dir_all(&home).unwrap();
+ common::seed_memory_runtime(&home);
+ let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
+ let child = Command::new(env!("CARGO_BIN_EXE_codync-host"))
+ .args(["serve", "--bind", "127.0.0.1", "--port", &port.to_string()])
+ .env("CODYNC_HOME", &home)
+ .env("CODYNC_CLOUD", "off")
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .spawn()
+ .unwrap();
+ let base = format!("http://127.0.0.1:{port}");
+ let deadline = Instant::now() + Duration::from_secs(30);
+ while reqwest::get(format!("{base}/health")).await.is_err() {
+ assert!(Instant::now() < deadline, "host didn't start");
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ }
+ let token = std::fs::read_to_string(home.join("token")).unwrap().trim().to_owned();
+ Self { child, base, token, home }
+ }
+
+ async fn call(&self, method: &str, body: Value) -> Value {
+ let res = reqwest::Client::new()
+ .post(format!("{}/api/{method}", self.base))
+ .bearer_auth(&self.token)
+ .json(&body)
+ .send()
+ .await
+ .unwrap();
+ let status = res.status();
+ let v: Value = res.json().await.unwrap();
+ assert!(status.is_success(), "{method} failed: {v}");
+ v
+ }
+
+ /// A bot running `fixture` in its own folder (`home/<name>`).
+ async fn bot(&self, name: &str, fixture: &str) -> String {
+ let cwd = self.home.join(name);
+ std::fs::create_dir_all(&cwd).unwrap();
+ let agent = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(fixture);
+ let bot = json!({
+ "name": name, "backend": "custom", "command": common::python_agent(&agent),
+ "cwd": cwd, "permission": "ask", "notify": false,
+ });
+ self.call("createBot", bot).await["bot"]["id"].as_str().unwrap().to_owned()
+ }
+
+ async fn entries(&self, chat: &str) -> Vec<Value> {
+ self.call("history", json!({"botId": chat})).await["entries"].as_array().unwrap().clone()
+ }
+
+ /// Polls `chat`'s history until `pred` matches an entry.
+ async fn wait_for(&self, chat: &str, within: Duration, pred: impl Fn(&Value) -> bool) -> Value {
+ let deadline = Instant::now() + within;
+ loop {
+ if let Some(e) = self.entries(chat).await.into_iter().rev().find(|e| pred(e)) {
+ return e;
+ }
+ assert!(Instant::now() < deadline, "timed out; history: {:?}", self.entries(chat).await);
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ }
+ }
+}
+
+fn python_missing() -> bool {
+ let missing = Command::new(common::python()).arg("--version").output().is_err();
+ if missing {
+ eprintln!("skipping: Python not available");
+ }
+ missing
+}
+
+#[tokio::test]
+async fn stop_ends_a_turn_whose_agent_ignores_the_cancel() {
+ if python_missing() {
+ return;
+ }
+ let host = Host::start().await;
+ let bot = host.bot("Stuck", "tests/fixtures/stubborn_agent.py").await;
+ host.call("send", json!({"botId": bot, "text": "go"})).await;
+ host.wait_for(&bot, Duration::from_secs(60), |e| e["data"]["text"] == "Working on it").await;
+ host.call("stop", json!({"botId": bot})).await;
+ // The agent never answers: after the grace period its process is stopped and the turn ends.
+ host.wait_for(&bot, Duration::from_secs(40), |e| e["kind"] == "notice" && e["data"]["text"] == "Stopped.").await;
+ let sync = host.call("sync", json!({"since": 0})).await;
+ assert_eq!(sync["bots"][0]["status"], "idle");
+}
+
+#[tokio::test]
+async fn a_member_busy_elsewhere_answers_only_the_latest_room_turn() {
+ if python_missing() {
+ return;
+ }
+ let host = Host::start().await;
+ let member = host.bot("Member", "tests/fixtures/team_agent.py").await;
+ let folder = host.home.join("Member");
+ let group =
+ host.call("createBot", json!({"name": "Room", "kind": "group", "members": [member]})).await["bot"]["id"]
+ .as_str()
+ .unwrap()
+ .to_owned();
+ // Busy in its own chat until `release` exists.
+ host.call("send", json!({"botId": member, "text": "BLOCK"})).await;
+ let started = Instant::now();
+ while !std::fs::read_to_string(folder.join("prompts.jsonl")).is_ok_and(|p| p.contains("BLOCK")) {
+ assert!(started.elapsed() < Duration::from_secs(60), "the member never started");
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ }
+ host.call("send", json!({"botId": group, "text": "first"})).await;
+ tokio::time::sleep(Duration::from_millis(500)).await;
+ host.call("send", json!({"botId": group, "text": "second"})).await;
+ tokio::time::sleep(Duration::from_millis(500)).await;
+ std::fs::write(folder.join("release"), "").unwrap();
+
+ let reply =
+ host.wait_for(&group, Duration::from_secs(30), |e| e["kind"] == "agent" && e["data"]["final"] == true).await;
+ assert_eq!(reply["data"]["text"], "reply: group");
+ tokio::time::sleep(Duration::from_secs(2)).await;
+ // The room turn "second" replaced was skipped: no room prompt saw "first" without "second".
+ let prompts = std::fs::read_to_string(folder.join("prompts.jsonl")).unwrap();
+ let rooms: Vec<&str> = prompts.lines().filter(|p| p.contains("[Group chat")).collect();
+ assert!(rooms.iter().any(|p| p.contains("User: second")), "{rooms:?}");
+ assert!(!rooms.iter().any(|p| p.contains("User: first") && !p.contains("User: second")), "{rooms:?}");
+ let finals = host.entries(&group).await.into_iter().filter(|e| e["kind"] == "agent" && e["data"]["final"] == true);
+ assert_eq!(finals.count(), 1);
+}