Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.
Rust acpagent-client-protocolai-agentsai-assistantclaudeclaude-codecodexcoding-agentscursorgeminigemini-cligrok-bot-alternativeiosiphonelinuxmacosmcpopen-sourcerustself-hosted
feat(host): compact the bot's chat while the cache is warm, and roll over with a handoff
leepokai3412d18a8d18
diff --git a/docs/architecture/file-structure.md b/docs/architecture/file-structure.mdindex 7618a1ff..a2a5bda1 100644--- a/docs/architecture/file-structure.md+++ b/docs/architecture/file-structure.md@@ -83,6 +83,7 @@ Codync/| 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/` (`launch.rs`, `queue.rs`, `session.rs`, `turn.rs`, `updates.rs`), `acp.rs` |+| Compaction timing and rollover | `host/src/agent/bot/compaction.rs`, the handoff card in `host/src/chat/handoff.rs`; the rules in `codync-mem`'s `compaction.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.mdindex 27e9c040..7b1580a9 100644--- a/docs/design/bot-memory.md+++ b/docs/design/bot-memory.md@@ -19,6 +19,12 @@ Built differently from the plan below, for now:`search_memory` returns each entry's source messages, and `search_history` reads back from a message id.- A memory notice carries the whole memory (at most 1,600 tokens) rather than a delta: a forgotten entry leavesno row to diff against, and a whole copy also replaces whatever an earlier notice said.+- The cache's lifetime is learned from turns that come after a pause (most of the input read from cache, or+ not); until a bot has one, Claude on a subscription counts as an hour and everything else as five minutes.+- The host compacts only Claude and Codex ahead of time; other harnesses keep their own timing. Codex's+ compaction line in the chat has no summary, since Codex reports none; its handoff is built from the chat.+- The handoff card appears as soon as a rollover is due, not when the user returns, so it can be edited before+ the message that sends it.In one line: **memory records the person, the transcript stays in the host, the opening stays frozen untilcompaction, and the host decides when to compact.**diff --git a/docs/features/context-and-memory.md b/docs/features/context-and-memory.mdindex 2b65e9f1..5417dc51 100644--- a/docs/features/context-and-memory.md+++ b/docs/features/context-and-memory.md@@ -82,6 +82,29 @@ the `codync-mem` crate; `chat/memory` runs them in the host.profile and pinned facts first, then the newest), the rest archived; then the old files (deleted bots' too) andthe Engram download go. Dreaming starts from that point in the chat.+## Compaction and rollover (`host/src/agent/bot/compaction.rs`, rules in `codync_mem::compaction`)++- **What the host records**, per session in `kv` (`compaction.<bot>.<session>`): context use and window size+ from `usage_update`, compactions, and when turns end. How long the provider keeps prompts cached is learned+ from turns that came after a pause (`cache.life.<bot>`); until then it is an hour for Claude on a subscription+ and five minutes otherwise. Each turn's cached read and write tokens are logged (`turn usage`).+- **Compacting ahead of time**, in the bot's own chat only (never threads, rooms, routines or bot requests):+ - at the end of a turn past three quarters of the window or 200k tokens, whichever is less;+ - while idle, with a one-hour cache and over 100k in use, five minutes before the cache expires.++ It runs as a hidden turn: dreaming first, then the opening rendered again (a Claude session reloads only when+ it changed), then `/compact <keep-list>` for Claude, or `/compact` for Codex, whose keep-list is its+ `CODEX_CONFIG` `compact_prompt`. Other harnesses compact on their own. The keep-list is also in the opening.+- **Loop guard:** after three host compactions in a day, or one that left 60% of the trigger in use, or one+ fewer than five turns ago, the host rolls over instead of compacting again.+- **Rollover:** also due when a conversation over 100k went cold (its cache expired) or after eight hours away+ with three compactions behind it. A handoff card appears in the chat (`chat/handoff.rs`): the last summary,+ the user's last four messages, the last reply, the open plan, and a bookmark for `search_history`. The user can+ edit it (`editHandoff`) or keep the conversation (`keepConversation`); otherwise their next message there starts+ a new session that begins with it.+- **After every compaction** the chat shows "Earlier context was summarized to make room." Its `compaction`+ field carries Claude's summary, which the apps show when expanded, and which the next handoff starts from.+## Turns- Messages sent while the agent works are folded into one next turn, joined by blank lines.@@ -98,9 +121,9 @@ the `codync-mem` crate; `chat/memory` runs them in the host.## Limits- An agent without ACP `systemPrompt` gets the instructions as a user message, which models follow less firmly- than system text. Memory a bot or dreaming wrote reaches it after its next compaction or in a new session.-- Compaction is the harness's own. Codync can't choose what survives it, only re-apply its instructions- afterwards.+ than system text.+- The harness writes every summary. Codync chooses when the bot's own chat compacts and what it asks the summary+ to keep, and re-applies the instructions afterwards; it can't make a harness keep anything.- A session a harness has deleted (for example, one cleaned up after a month) can't be resumed. The botstarts fresh, but its memory remains.- Memory search is a substring scan, not semantic vectors. Voice transcripts aren't marked yet, so dreamingdiff --git a/host/crates/memory/src/compaction.rs b/host/crates/memory/src/compaction.rsnew file mode 100644index 00000000..8685b2b6--- /dev/null+++ b/host/crates/memory/src/compaction.rs@@ -0,0 +1,328 @@+//! When to compact a conversation, or roll it over into a new one that starts from a handoff.+//!+//! The harness writes the summary, because it knows the work; the caller decides when, because it knows how full the+//! context is, whether the user is around and whether the prompt cache is still warm. The caller reports what it+//! sees into a [`Conversation`] and asks [`at_turn_end`], [`while_idle`] and [`rollover_due`].++use serde::{Deserialize, Serialize};++const MINUTE_MS: i64 = 60_000;+const HOUR_MS: i64 = 60 * MINUTE_MS;+const DAY_MS: i64 = 24 * HOUR_MS;++/// At the end of a turn, compact past three quarters of the window or this many tokens, whichever is less: the+/// summary needs room, and a hot cache makes reading the history cheap.+const TURN_END_CAP: u64 = 200_000;+/// Idle compaction and rollover only pay for themselves past this much context.+const LARGE: u64 = 100_000;+/// How long before a one-hour cache expires an idle conversation is compacted.+const BEFORE_EXPIRY_MS: i64 = 5 * MINUTE_MS;+/// Away this long with this many compactions behind it, a conversation is summaries of summaries.+const AWAY_MS: i64 = 8 * HOUR_MS;+const AWAY_COMPACTIONS: u32 = 3;+/// The loop guard: at most this many host compactions a day...+const PER_DAY: usize = 3;+/// ...none that leaves the context above 60% of the trigger...+const THRASH_PERCENT: u64 = 60;+/// ...and none within this many turns of the last compaction.+const THRASH_TURNS: u32 = 5;++/// How long the provider keeps a prompt cached.+#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase")]+pub enum CacheLife {+ FiveMinutes,+ OneHour,+}++impl CacheLife {+ pub fn ms(self) -> i64 {+ match self {+ Self::FiveMinutes => 5 * MINUTE_MS,+ Self::OneHour => HOUR_MS,+ }+ }++ /// What a turn that came `gap_ms` after the previous one shows about the cache: whether most of its input was+ /// read from cache. Only a gap past five minutes and short of an hour tells the two apart.+ pub fn observed(gap_ms: i64, cached_read: u64, input: u64) -> Option<Self> {+ if gap_ms <= 6 * MINUTE_MS || gap_ms >= 55 * MINUTE_MS || input == 0 {+ return None;+ }+ Some(if cached_read * 2 >= input { Self::OneHour } else { Self::FiveMinutes })+ }+}++/// One conversation as the caller saw it.+#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]+#[serde(rename_all = "camelCase", default)]+pub struct Conversation {+ /// Context in use and the window's size, from the agent's last usage report.+ pub used: u64,+ pub size: u64,+ /// When its last turn ended (ms).+ pub last_turn: i64,+ /// Turns since the last compaction.+ pub turns_since: u32,+ /// Compactions of any kind.+ pub compactions: u32,+ /// When the caller compacted it, within the last day (ms).+ pub host_compactions: Vec<i64>,+ /// Context in use just after the last compaction, once reported.+ pub after: Option<u64>,+ /// The caller compacted it while idle since its last turn.+ pub idle_compacted: bool,+ /// The user chose to carry on over a rollover since its last turn.+ pub rollover_declined: bool,+}++/// What to do with a conversation now.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum Decision {+ Keep,+ Compact,+ /// Start a new conversation from a handoff.+ Rollover,+}++impl Conversation {+ /// The agent reported its context use.+ pub fn usage(&mut self, used: u64, size: u64) {+ if self.compactions > 0 && self.after.is_none() && self.turns_since == 0 {+ self.after = Some(used);+ }+ self.used = used;+ if size > 0 {+ self.size = size;+ }+ }++ pub fn turn_ended(&mut self, now: i64) {+ self.last_turn = now;+ self.turns_since += 1;+ self.idle_compacted = false;+ self.rollover_declined = false;+ }++ /// The agent summarized its context; `by_host` when the caller asked it to.+ pub fn compacted(&mut self, by_host: bool, idle: bool, now: i64) {+ self.compactions += 1;+ self.turns_since = 0;+ self.after = None;+ if by_host {+ self.host_compactions.retain(|at| now - at < DAY_MS);+ self.host_compactions.push(now);+ }+ self.idle_compacted |= idle;+ }++ fn trigger(&self) -> u64 {+ (self.size / 4 * 3).min(TURN_END_CAP)+ }++ /// Compacting again wouldn't help: too many today, too little gained last time, or too soon after the last.+ fn thrashing(&self, now: i64) -> bool {+ let today = self.host_compactions.iter().filter(|at| now - **at < DAY_MS).count();+ today >= PER_DAY+ || self.after.is_some_and(|after| after * 100 >= self.trigger() * THRASH_PERCENT)+ || (self.compactions > 0 && self.turns_since < THRASH_TURNS)+ }+}++/// At the end of a turn in the conversation the user returns to.+pub fn at_turn_end(c: &Conversation, now: i64) -> Decision {+ if c.size == 0 || c.used < c.trigger() {+ Decision::Keep+ } else if c.thrashing(now) {+ Decision::Rollover+ } else {+ Decision::Compact+ }+}++/// While nothing runs: compact a large conversation just before a one-hour cache expires, which is cheaper than+/// reading it cold when the user returns. A five-minute cache expires before this pays off.+pub fn while_idle(c: &Conversation, life: CacheLife, now: i64) -> Decision {+ let idle = now - c.last_turn;+ let due = life == CacheLife::OneHour+ && c.used >= LARGE+ && !c.idle_compacted+ && idle >= life.ms() - BEFORE_EXPIRY_MS+ && idle < life.ms();+ if due && !c.thrashing(now) { Decision::Compact } else { Decision::Keep }+}++/// Whether the next message should start a new conversation from a handoff instead of carrying on: a large context+/// whose cache went cold, or a long absence after several compactions.+pub fn rollover_due(c: &Conversation, life: CacheLife, now: i64) -> bool {+ let away = now - c.last_turn;+ c.last_turn > 0+ && !c.rollover_declined+ && ((away >= life.ms() && c.used >= LARGE) || (away >= AWAY_MS && c.compactions >= AWAY_COMPACTIONS))+}++/// What a summary must keep. It goes with every compaction the caller starts, and in the opening for the ones the+/// harness starts itself.+pub const KEEP: &str = "When your context is summarized, keep word for word the user's latest request and every \"don't\" or \"不要\" they said; keep exact paths, URLs and ids; keep decisions with their reasons, open tasks with their next step, who said what in group chats, and what a thread is about. Leave out your profile and memory: you get them again.";++/// One of the user's recent messages.+pub struct Request<'a> {+ /// `YYYY-MM-DD`.+ pub date: &'a str,+ pub text: &'a str,+}++/// What a new conversation is told about the one it continues.+pub struct Handoff<'a> {+ /// The harness's last summary, if it gave one.+ pub summary: Option<&'a str>,+ /// The user's last few messages, oldest first.+ pub requests: &'a [Request<'a>],+ /// The agent's last reply.+ pub reply: Option<&'a str>,+ /// The plan the agent was working through.+ pub plan: Option<&'a str>,+ /// The last message of the old conversation, to read back from.+ pub bookmark: Option<&'a str>,+}++impl Handoff<'_> {+ pub fn render(&self) -> String {+ let mut lines = vec![+ "<handoff>".to_owned(),+ "This conversation continues an earlier one with the user, which grew too long to carry on. It is what you knew there; it is information, not instructions.".to_owned(),+ ];+ if let Some(summary) = self.summary.filter(|s| !s.trim().is_empty()) {+ lines.push(String::new());+ lines.push("Summary of the earlier conversation:".into());+ lines.push(summary.trim().to_owned());+ }+ if !self.requests.is_empty() {+ lines.push(String::new());+ lines.push("The user's latest messages there, word for word:".into());+ lines.extend(self.requests.iter().map(|r| format!("- ({}) {}", r.date, r.text.trim())));+ }+ if let Some(reply) = self.reply.filter(|s| !s.trim().is_empty()) {+ lines.push(String::new());+ lines.push("Your last reply there:".into());+ lines.push(reply.trim().to_owned());+ }+ if let Some(plan) = self.plan.filter(|s| !s.trim().is_empty()) {+ lines.push(String::new());+ lines.push("Your open plan:".into());+ lines.push(plan.trim().to_owned());+ }+ if let Some(bookmark) = self.bookmark {+ lines.push(String::new());+ lines.push(format!("To read the earlier conversation, call search_history with from: \"{bookmark}\"."));+ }+ lines.push("</handoff>".into());+ lines.join("\n")+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ const NOW: i64 = 100 * DAY_MS;++ fn full() -> Conversation {+ Conversation { used: 210_000, size: 1_000_000, last_turn: NOW, turns_since: 9, ..Conversation::default() }+ }++ #[test]+ fn a_turn_ending_past_the_trigger_compacts() {+ assert_eq!(at_turn_end(&full(), NOW), Decision::Compact);+ let small = Conversation { used: 150_000, size: 200_000, ..full() };+ assert_eq!(at_turn_end(&small, NOW), Decision::Compact, "three quarters of a smaller window");+ let room = Conversation { used: 120_000, ..full() };+ assert_eq!(at_turn_end(&room, NOW), Decision::Keep);+ assert_eq!(at_turn_end(&Conversation::default(), NOW), Decision::Keep, "no usage reported yet");+ }++ #[test]+ fn the_loop_guard_rolls_over_instead_of_compacting_again() {+ let mut c = full();+ c.compacted(true, false, NOW);+ c.usage(130_000, 1_000_000);+ for _ in 0..THRASH_TURNS {+ c.turn_ended(NOW);+ }+ c.used = 210_000;+ assert_eq!(at_turn_end(&c, NOW), Decision::Rollover, "the last one left 65% of the trigger");++ let mut c = full();+ c.compacted(true, false, NOW);+ c.usage(20_000, 1_000_000);+ c.turn_ended(NOW);+ c.used = 210_000;+ assert_eq!(at_turn_end(&c, NOW), Decision::Rollover, "two compactions within five turns");++ let mut c = full();+ for hour in 0..3 {+ c.compacted(true, false, NOW - hour * HOUR_MS);+ }+ c.usage(20_000, 1_000_000);+ c.turns_since = 9;+ c.used = 210_000;+ assert_eq!(at_turn_end(&c, NOW), Decision::Rollover, "three today");+ assert_eq!(at_turn_end(&c, NOW + DAY_MS), Decision::Compact, "a day later");+ }++ #[test]+ fn idle_compaction_runs_just_before_a_one_hour_cache_expires() {+ let c = Conversation { used: 120_000, ..full() };+ let at = |minutes: i64| while_idle(&c, CacheLife::OneHour, NOW + minutes * MINUTE_MS);+ assert_eq!(at(30), Decision::Keep);+ assert_eq!(at(56), Decision::Compact);+ assert_eq!(at(61), Decision::Keep, "already cold");+ assert_eq!(while_idle(&c, CacheLife::FiveMinutes, NOW + 4 * MINUTE_MS), Decision::Keep);+ let small = Conversation { used: 60_000, ..c.clone() };+ assert_eq!(while_idle(&small, CacheLife::OneHour, NOW + 56 * MINUTE_MS), Decision::Keep);+ let mut done = c;+ done.compacted(true, true, NOW + 56 * MINUTE_MS);+ assert_eq!(while_idle(&done, CacheLife::OneHour, NOW + 57 * MINUTE_MS), Decision::Keep, "once");+ }++ #[test]+ fn a_cold_large_or_long_away_conversation_rolls_over() {+ let c = Conversation { used: 120_000, ..full() };+ assert!(!rollover_due(&c, CacheLife::OneHour, NOW + 30 * MINUTE_MS));+ assert!(rollover_due(&c, CacheLife::OneHour, NOW + 61 * MINUTE_MS));+ assert!(rollover_due(&c, CacheLife::FiveMinutes, NOW + 6 * MINUTE_MS));+ let small = Conversation { used: 30_000, ..full() };+ assert!(!rollover_due(&small, CacheLife::FiveMinutes, NOW + 6 * HOUR_MS));+ let summarized = Conversation { compactions: 3, ..small };+ assert!(rollover_due(&summarized, CacheLife::FiveMinutes, NOW + 9 * HOUR_MS));+ assert!(!rollover_due(&Conversation::default(), CacheLife::OneHour, NOW), "never used");+ let mut kept = Conversation { rollover_declined: true, ..c.clone() };+ assert!(!rollover_due(&kept, CacheLife::OneHour, NOW + 61 * MINUTE_MS), "the user carried on");+ kept.turn_ended(NOW + 62 * MINUTE_MS);+ assert!(rollover_due(&kept, CacheLife::OneHour, NOW + 130 * MINUTE_MS), "until the next quiet spell");+ }++ #[test]+ fn the_cache_life_shows_in_a_turn_after_a_pause() {+ assert_eq!(CacheLife::observed(20 * MINUTE_MS, 40_000, 41_000), Some(CacheLife::OneHour));+ assert_eq!(CacheLife::observed(20 * MINUTE_MS, 0, 41_000), Some(CacheLife::FiveMinutes));+ assert_eq!(CacheLife::observed(2 * MINUTE_MS, 0, 41_000), None, "both would still be warm");+ assert_eq!(CacheLife::observed(90 * MINUTE_MS, 0, 41_000), None, "both would be cold");+ }++ #[test]+ fn a_handoff_carries_what_the_new_conversation_needs() {+ let requests = [Request { date: "2026-10-18", text: "Book the train to Tainan" }];+ let text = Handoff {+ summary: Some("Planned a trip."),+ requests: &requests,+ reply: Some("Booked."),+ plan: None,+ bookmark: Some("m42"),+ }+ .render();+ assert!(text.starts_with("<handoff>") && text.ends_with("</handoff>"));+ assert!(text.contains("Planned a trip.") && text.contains("- (2026-10-18) Book the train to Tainan"));+ assert!(text.contains("from: \"m42\"") && !text.contains("open plan"));+ }+}diff --git a/host/crates/memory/src/lib.rs b/host/crates/memory/src/lib.rsindex b61be6eb..9f0410d7 100644--- a/host/crates/memory/src/lib.rs+++ b/host/crates/memory/src/lib.rs@@ -5,6 +5,7 @@//! caller's transaction, so a memory change and whatever the caller records with it commit together.pub mod check;+pub mod compaction;pub mod dream;pub mod evidence;pub mod opening;diff --git a/host/src/agent/bot/compaction.rs b/host/src/agent/bot/compaction.rsnew file mode 100644index 00000000..5f741825--- /dev/null+++ b/host/src/agent/bot/compaction.rs@@ -0,0 +1,307 @@+//! The bot's conversations as they fill up ([design](../../../../docs/design/bot-memory.md#compaction)): what the+//! agent reports about its context and its own compactions, and the compactions and rollovers the host starts. The+//! rules are `codync_mem::compaction`; this records what it needs and acts on what it decides.++use super::{Actor, Adapter, Done, Instructions, Queued};+use crate::chat::{context, handoff, memory};+use crate::store::{EntryKind, Lane, now_ms};+use codync_mem::compaction::{self as rules, CacheLife, Conversation, Decision};+use serde_json::{Value, json};++/// Context use below which a fall isn't read as a compaction: a short session has little to summarize.+const COMPACTED_FROM: u64 = 20_000;++impl Actor {+ fn conversation_key(&self, session: &str) -> String {+ format!("compaction.{}.{session}", self.cfg.id)+ }++ fn conversation(&self, session: &str) -> Conversation {+ self.hub+ .store+ .kv_get(&self.conversation_key(session))+ .and_then(|v| serde_json::from_str(&v).ok())+ .unwrap_or_default()+ }++ fn save_conversation(&self, session: &str, c: &Conversation) {+ let saved = serde_json::to_string(c)+ .map_err(anyhow::Error::from)+ .and_then(|v| self.hub.store.kv_set(&self.conversation_key(session), &v));+ if let Err(error) = saved {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the conversation");+ }+ }++ fn cache_life_key(&self) -> String {+ format!("cache.life.{}", self.cfg.id)+ }++ /// How long the provider keeps this bot's prompts cached: learned from its turns, or guessed from its sign-in+ /// (Claude keeps an hour on a subscription, five minutes on an API key).+ fn cache_life(&self) -> CacheLife {+ if let Some(life) = self.hub.store.kv_get(&self.cache_life_key()).and_then(|v| serde_json::from_str(&v).ok()) {+ return life;+ }+ let api_key = crate::agent::auth::env(&self.hub.store, &self.cfg.backend)+ .unwrap_or_default()+ .iter()+ .any(|(k, _)| k == "ANTHROPIC_API_KEY")+ || std::env::var_os("ANTHROPIC_API_KEY").is_some();+ let claude = self.conn.as_ref().is_some_and(|c| c.adapter == Adapter::Claude) || self.cfg.backend == "claude";+ if claude && !api_key { CacheLife::OneHour } else { CacheLife::FiveMinutes }+ }++ /// The bot's own chat, not a thread, a room, a routine or another bot's request: the conversation the user comes+ /// back to, and the only one compacted ahead of time.+ fn in_main_chat(&self) -> bool {+ self.lane.thread.is_none()+ && self.lane.chat == self.cfg.id+ && self.active_group.is_none()+ && self.active_routine.is_none()+ && self.active_request.is_none()+ && self.turn_session.is_some()+ && self.turn_session == self.session_id+ }++ /// The command that makes the agent compact, with the keep-list where it takes one. Before the agent runs, its+ /// harness stands in for the adapter.+ fn compact_command(&self) -> Option<String> {+ let adapter = self.conn.as_ref().map_or(+ match self.cfg.backend.as_str() {+ "claude" => Adapter::Claude,+ "codex" => Adapter::Codex,+ _ => Adapter::Other,+ },+ |c| c.adapter,+ );+ match adapter {+ Adapter::Claude => Some(format!("/compact {}", rules::KEEP)),+ Adapter::Codex => Some("/compact".into()),+ Adapter::Other => None,+ }+ }++ /// The agent reported its context use.+ pub(super) fn track_usage(&mut self, used: u64, size: u64) {+ let Some(sid) = self.turn_session.clone() else { return };+ let Some(conn) = self.conn.as_mut() else { return };+ let before = conn.usage.insert(sid, (used, size)).map(|(used, _)| used);+ if !conn.reports_compaction && compacted_by_usage(before, used) {+ self.compacted(&format!("usage-{}", now_ms()), None);+ }+ }++ /// The agent summarized its context: the next turn gets freshly rendered instructions and memory (see+ /// `context`), a harness that got them in its first message gets them again, and the chat shows the summary.+ pub(super) fn compacted(&mut self, id: &str, summary: Option<String>) {+ let Some(sid) = self.turn_session.clone() else { return };+ match context::bump_epoch(&self.hub.store, &self.cfg.id, &sid, id) {+ Ok(true) => {}+ // The same compaction reported again; Claude's adapter carries the summary on one of its reports.+ Ok(false) => {+ if let Some(summary) = summary {+ self.keep_summary(&sid, &summary);+ }+ return;+ }+ Err(error) => {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the compaction");+ return;+ }+ }+ if let Err(error) = 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.clone());+ }+ if let Some(root) = self.lane.thread.clone() {+ self.thread_intro = Some(self.thread_intro(&root, false));+ }+ let mut c = self.conversation(&sid);+ c.compacted(self.compacting.is_some(), self.compacting == Some(true), now_ms());+ self.save_conversation(&sid, &c);+ let _ = self.dreamer.send(memory::DreamEvent::Flush);+ let data = json!({+ "text": "Earlier context was summarized to make room.",+ "style": "info",+ "compaction": {"summary": Value::Null, "byHost": self.compacting.is_some()},+ });+ self.compaction_notice = self.add(EntryKind::Notice, self.turn.unwrap_or(0), data).map(|e| e.id);+ if let Some(summary) = summary {+ self.keep_summary(&sid, &summary);+ }+ }++ /// The harness's summary: shown on the compaction's line in the chat, and kept for a later handoff.+ fn keep_summary(&mut self, session: &str, summary: &str) {+ if let Err(error) = self.hub.store.kv_set(&handoff::summary_key(&self.cfg.id, session), summary) {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't keep the summary");+ }+ if let Some(id) = &self.compaction_notice+ && let Some(mut entry) = self.hub.store.entry(id)+ {+ entry.data["compaction"]["summary"] = summary.into();+ self.hub.set_entry(id, &entry.data);+ }+ }++ /// After a turn in the main chat: record it, learn the cache's life from it, and compact or prepare a rollover+ /// when that's due.+ pub(super) fn after_turn(&mut self, done: &Done) {+ let compacting = self.compacting.take();+ if let Ok(answer) = done+ && answer["usage"].is_object()+ {+ // How well the prompt cache held: the first thing to look at when a turn costs more than it should.+ let usage = &answer["usage"];+ tracing::info!(+ bot = %self.cfg.id,+ session = self.turn_session.as_deref().unwrap_or_default(),+ cached_read = usage["cachedReadTokens"].as_u64().unwrap_or(0),+ cached_write = usage["cachedWriteTokens"].as_u64().unwrap_or(0),+ input = usage["inputTokens"].as_u64().unwrap_or(0),+ "turn usage"+ );+ }+ if !self.in_main_chat() {+ return;+ }+ let Some(sid) = self.turn_session.clone() else { return };+ let now = now_ms();+ let mut c = self.conversation(&sid);+ if let Some(&(used, size)) = self.conn.as_ref().and_then(|conn| conn.usage.get(&sid)) {+ c.usage(used, size);+ }+ // The compaction turn itself is no turn of the conversation: it neither counts nor resets the idle clock.+ if compacting.is_some() {+ self.save_conversation(&sid, &c);+ return;+ }+ if let Ok(answer) = done+ && let Some(life) = learned_life(answer, now - c.last_turn)+ && let Err(error) = self.hub.store.kv_set(&self.cache_life_key(), &json!(life).to_string())+ {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the cache's life");+ }+ c.turn_ended(now);+ self.save_conversation(&sid, &c);+ match rules::at_turn_end(&c, now) {+ Decision::Compact if self.compact_command().is_some() => {+ self.queue.push_back(Queued::Compact { idle: false });+ }+ Decision::Rollover => self.draft_handoff(&sid, handoff::Reason::Full),+ _ => {}+ }+ }++ /// While the bot is at rest: compact before an hour-long cache expires, and once the next message should start+ /// a new conversation, put the handoff in the chat for the user to review.+ pub(super) fn tend_idle(&mut self) {+ let Some(sid) = self.session_id.clone() else { return };+ let c = self.settle_kept(&sid);+ if c.last_turn == 0 {+ return;+ }+ let now = now_ms();+ let life = self.cache_life();+ if rules::while_idle(&c, life, now) == Decision::Compact && self.compact_command().is_some() {+ self.queue.push_back(Queued::Compact { idle: true });+ } else if rules::rollover_due(&c, life, now) && handoff::pending(&self.hub.store, &self.cfg.id).is_none() {+ let reason = if now - c.last_turn >= life.ms() { handoff::Reason::Cold } else { handoff::Reason::Away };+ self.draft_handoff(&sid, reason);+ }+ }++ /// The conversation, with the user's choice to carry on over its last handoff card recorded.+ fn settle_kept(&self, session: &str) -> Conversation {+ let mut c = self.conversation(session);+ if handoff::kept(&self.hub.store, &self.cfg.id) {+ c.rollover_declined = true;+ self.save_conversation(session, &c);+ }+ c+ }++ fn draft_handoff(&mut self, session: &str, reason: handoff::Reason) {+ if handoff::pending(&self.hub.store, &self.cfg.id).is_some() {+ return;+ }+ if let Err(error) = handoff::draft(&self.hub, &self.cfg, session, reason) {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't prepare the handoff");+ }+ }++ /// Runs a compaction the host decided on, as a hidden turn in the main chat. Dreaming goes first, and Claude's+ /// opening is rendered again, so the compaction keeps current memory (render, load, compact).+ pub(super) async fn start_compaction(&mut self, idle: bool, done_tx: &tokio::sync::mpsc::UnboundedSender<Done>) {+ let (Some(sid), Some(command)) = (self.session_id.clone(), self.compact_command()) else { return };+ let _ = self.dreamer.send(memory::DreamEvent::Flush);+ if let Err(error) = context::refresh(&self.hub.store, &self.cfg.id, &sid) {+ tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't refresh the opening");+ }+ self.compacting = Some(idle);+ self.raw_prompt = true;+ if let Err(e) = self.start_turn(Lane::main(&self.cfg.id), &[], &command, true, done_tx).await {+ self.compacting = None;+ self.start_failed(&e);+ }+ }++ /// Before the user's message in the main chat: a handoff waiting for it starts a new conversation. Returns the+ /// handoff to put before the message.+ pub(super) fn take_handoff(&mut self) -> Option<String> {+ // A rollover that came due since the last tick gets its card now, without the chance to edit it.+ if let Some(sid) = self.session_id.clone()+ && handoff::pending(&self.hub.store, &self.cfg.id).is_none()+ {+ let c = self.settle_kept(&sid);+ let life = self.cache_life();+ if rules::rollover_due(&c, life, now_ms()) {+ let reason =+ if now_ms() - c.last_turn >= life.ms() { handoff::Reason::Cold } else { handoff::Reason::Away };+ self.draft_handoff(&sid, reason);+ }+ }+ let (id, text) = handoff::pending(&self.hub.store, &self.cfg.id)?;+ handoff::sent(&self.hub, &self.cfg.id, &id);+ self.forget_session();+ Some(text)+ }+}++/// What a turn's prompt usage says about the cache's life, `gap_ms` after the previous turn.+fn learned_life(answer: &Value, gap_ms: i64) -> Option<CacheLife> {+ let usage = &answer["usage"];+ let read = usage["cachedReadTokens"].as_u64()?;+ let input = read + usage["cachedWriteTokens"].as_u64().unwrap_or(0) + usage["inputTokens"].as_u64().unwrap_or(0);+ CacheLife::observed(gap_ms, read, input)+}++/// Whether a session's context use fell the way it does when the agent summarizes it: by more than half.+fn compacted_by_usage(before: Option<u64>, used: u64) -> bool {+ before.is_some_and(|before| before >= COMPACTED_FROM && used < before / 2)+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn a_halved_context_reads_as_a_compaction() {+ assert!(compacted_by_usage(Some(90_000), 12_000));+ assert!(!compacted_by_usage(Some(90_000), 60_000), "ordinary churn");+ assert!(!compacted_by_usage(Some(8_000), 1_000), "too little to summarize");+ assert!(!compacted_by_usage(None, 1_000), "the session's first report");+ }++ #[test]+ fn a_turn_after_a_pause_tells_the_cache_life() {+ let warm = json!({"usage": {"inputTokens": 2, "cachedReadTokens": 40_000, "cachedWriteTokens": 300}});+ assert_eq!(learned_life(&warm, 20 * 60_000), Some(CacheLife::OneHour));+ let cold = json!({"usage": {"inputTokens": 2, "cachedReadTokens": 0, "cachedWriteTokens": 40_000}});+ assert_eq!(learned_life(&cold, 20 * 60_000), Some(CacheLife::FiveMinutes));+ assert_eq!(learned_life(&json!({"stopReason": "end_turn"}), 20 * 60_000), None, "no usage reported");+ }+}diff --git a/host/src/agent/bot/launch.rs b/host/src/agent/bot/launch.rsindex 104b7807..4c8f14e7 100644--- a/host/src/agent/bot/launch.rs+++ b/host/src/agent/bot/launch.rs@@ -1,7 +1,7 @@//! 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 super::{Adapter, Conn, SessionCaps};use crate::agent::acp::{self, Acp};use crate::store::BotConfig;use anyhow::{Result, anyhow};@@ -130,20 +130,29 @@ pub(crate) async fn start_agent(command: &str, cwd: &str, env: &[(String, Stringclose: 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,+ adapter: adapter(&init),loaded: HashMap::new(),started: Instant::now(),fast_options: HashMap::new(),reports_compaction: false,- used: HashMap::new(),+ usage: HashMap::new(),})}+fn adapter(init: &Value) -> Adapter {+ if init["agentCapabilities"]["_meta"]["claudeCode"].is_object() {+ Adapter::Claude+ } else if init["agentInfo"]["name"].as_str().is_some_and(|name| name.ends_with("codex-acp")) {+ Adapter::Codex+ } else {+ Adapter::Other+ }+}+#[cfg(test)]mod tests {use super::*;diff --git a/host/src/agent/bot/lifecycle.rs b/host/src/agent/bot/lifecycle.rsindex 0dfc7c5e..b41e6a3d 100644--- a/host/src/agent/bot/lifecycle.rs+++ b/host/src/agent/bot/lifecycle.rs@@ -40,12 +40,16 @@ fn plan(used: &HashMap<String, Instant>, started: Instant, can_close: bool, now:impl Actor {/// Runs on the actor's tick: at most every [`CHECK_EVERY`], and only while the bot has nothing- /// running, queued or waiting on the user.+ /// running, queued or waiting on the user. A compaction due while idle goes first.pub(super) async fn release_idle(&mut self) {if self.idle_checked.elapsed() < CHECK_EVERY || !self.at_rest() {return;}self.idle_checked = Instant::now();+ self.tend_idle();+ if !self.queue.is_empty() {+ return;+ }let Some(conn) = &self.conn else { return };if !conn.sessions.load {return;diff --git a/host/src/agent/bot/mod.rs b/host/src/agent/bot/mod.rsindex 2fff5165..767895b9 100644--- a/host/src/agent/bot/mod.rs+++ b/host/src/agent/bot/mod.rs@@ -15,6 +15,7 @@//! forked from the main one when the agent can fork) and in group chats (turns in//! its main session, written to the group's transcript; see `group`).+mod compaction;mod fast_mode;mod files;mod launch;@@ -92,8 +93,16 @@ pub enum Cmd {enum Queued {Routine(String),- User { lane: Lane, entry_id: String, text: String },+ User {+ lane: Lane,+ entry_id: String,+ text: String,+ },BotRequest(crate::chat::team::BotRequest),+ /// A compaction the host decided on (see `compaction`); `idle` while the user is away.+ Compact {+ idle: bool,+ },Group(GroupTurn),}@@ -135,6 +144,10 @@ pub fn spawn(hub: Arc<Hub>, cfg: BotConfig) -> BotHandle {routine_completion: None,session_fresh: false,profile_resend: HashSet::new(),+ compacting: None,+ raw_prompt: false,+ handoff: None,+ compaction_notice: None,applied_system: HashMap::new(),dreamer,turn_text: None,@@ -193,12 +206,21 @@ pub(crate) enum Instructions {FirstMessage,}+/// Which adapter answered `initialize`, where Codync treats it apart from the others.+#[derive(Clone, Copy, PartialEq, Eq, Debug)]+pub(crate) enum Adapter {+ /// claude-agent-acp: takes `_meta.systemPrompt` and `_meta.claudeCode.options`, and `/compact <instructions>`.+ Claude,+ /// codex-acp: `/compact` (its keep-list is in `CODEX_CONFIG`).+ Codex,+ Other,+}+pub(crate) struct Conn {pub(crate) acp: Arc<Acp>,pub(crate) rx: mpsc::UnboundedReceiver<Incoming>,sessions: SessionCaps,- /// Claude's adapter: takes `_meta.systemPrompt` and `_meta.claudeCode.options`.- pub(crate) claude: bool,+ pub(crate) adapter: Adapter,/// Sessions already created or loaded in this process, and when each was last used.loaded: HashMap<String, Instant>,started: Instant,@@ -206,14 +228,14 @@ pub(crate) struct Conn {fast_options: HashMap<String, Value>,/// The agent reports compactions (`compaction_update`); until it does, a fall in usage stands in for them.reports_compaction: bool,- /// Each session's context use at its last `usage_update`.- used: HashMap<String, u64>,+ /// Each session's context use and window size at its last `usage_update`.+ usage: HashMap<String, (u64, u64)>,}impl Conn {pub(crate) fn instructions(&self) -> Instructions {// Claude's adapter rebuilds the standard field without the `_meta` object's options, so Claude stays on it.- if self.claude {+ if self.adapter == Adapter::Claude {Instructions::ClaudeMeta} else if self.sessions.system_prompt {Instructions::SystemPrompt@@ -260,6 +282,14 @@ struct Actor {/// Sessions that compacted away the `<bot-profile>` their first message carried; their next prompt carries/// a fresh one.profile_resend: HashSet<String>,+ /// The running turn is a compaction the host started (`Some(true)` while the user is away).+ compacting: Option<bool>,+ /// The next prompt goes as it is: a command such as `/compact`, with nothing put before or after it.+ raw_prompt: bool,+ /// What the next prompt carries from the conversation it starts over from.+ handoff: Option<String>,+ /// The chat line of the last compaction, which a late summary fills in.+ compaction_notice: Option<String>,/// Session -> the instructions this process last handed Claude as its system prompt.applied_system: HashMap<String, String>,dreamer: mpsc::UnboundedSender<memory::DreamEvent>,@@ -360,6 +390,10 @@ impl Actor {if let Some(c) = self.conn.take() { c.acp.kill().await; }}self.release_idle().await;+ // The idle check may have queued a compaction.+ if self.turn.is_none() && !self.queue.is_empty() {+ self.next_in_queue(&done_tx).await;+ }},}}diff --git a/host/src/agent/bot/queue.rs b/host/src/agent/bot/queue.rsindex 8bf7856c..ca2b7581 100644--- a/host/src/agent/bot/queue.rs+++ b/host/src/agent/bot/queue.rs@@ -212,6 +212,13 @@ impl Actor {}continue;}+ Some(Queued::Compact { .. }) => {+ let Some(Queued::Compact { idle }) = self.queue.pop_front() else {+ unreachable!("front is a compaction")+ };+ self.start_compaction(idle, done_tx).await;+ continue;+ }_ => {}}// Messages sent while the agent worked go out together as its next turn@@ -228,6 +235,10 @@ impl Actor {}let ids: Vec<String> = batch.iter().map(|(id, _)| id.clone()).collect();let text = batch.iter().map(|(_, t)| t.as_str()).collect::<Vec<_>>().join("\n\n");+ // A handoff waiting in the chat starts a new conversation with this message.+ if lane == main {+ self.handoff = self.take_handoff();+ }if let Err(e) = self.start_turn(lane, &ids, &text, false, done_tx).await {self.start_failed(&e);}diff --git a/host/src/agent/bot/session.rs b/host/src/agent/bot/session.rsindex 1bd3f8fb..e0c1120d 100644--- a/host/src/agent/bot/session.rs+++ b/host/src/agent/bot/session.rs@@ -161,6 +161,11 @@ impl Actor {// Codync's memory is the only one a bot in its own workspace has.own_memory::turn_off(&self.cfg.backend, &self.cfg.cwd, &mut env, &mut candidates).await;}+ if self.cfg.backend == "codex" && !env.iter().any(|(k, _)| k == "CODEX_CONFIG") {+ // Codex's `/compact` takes no instructions: the keep-list is its config's `compact_prompt`.+ let config = json!({"compact_prompt": codync_mem::compaction::KEEP});+ env.push(("CODEX_CONFIG".into(), config.to_string()));+ }let mut last_err = None;for (i, command) in candidates.iter().enumerate() {let fallback_left = i + 1 < candidates.len();diff --git a/host/src/agent/bot/turn.rs b/host/src/agent/bot/turn.rsindex 10a10623..a94c53e3 100644--- a/host/src/agent/bot/turn.rs+++ b/host/src/agent/bot/turn.rs@@ -23,6 +23,47 @@ impl Actor {tokio::task::spawn_blocking(move || context::resolve(&hub.store, &cfg, &session)).await?}+ /// The prompt for `text` in session `sid`: what the agent should hear first (its profile, a handoff, a thread's+ /// start, a memory change) and after (a profile update, how to share files).+ async fn compose(&mut self, sid: &str, text: &str) -> Result<String> {+ let instructions = self.conn.as_ref().map_or(Instructions::FirstMessage, Conn::instructions);+ let mut snapshot = self.snapshot(sid).await?;+ let remind_files = !snapshot.system.contains("send_file");+ let mut prompt = text.to_owned();+ if let Some(handoff) = self.handoff.take() {+ prompt = format!("{handoff}\n\n{prompt}");+ }+ // 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 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}");+ self.turn_memory_revision = revision;+ }+ if let Some(intro) = self.thread_intro.take() {+ prompt = format!("{intro}\n\n{prompt}");+ }+ if std::mem::take(&mut self.session_fresh) && instructions == Instructions::FirstMessage {+ prompt = format!("<bot-profile>\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);+ } else if resend {+ context::mark_announced(&self.hub.store, &self.cfg.id, &snapshot, snapshot.system_identity.clone())?;+ snapshot.announced = snapshot.system_identity.clone();+ prompt = format!("<bot-profile>\n{PROFILE_AGAIN}\n\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);+ }+ if let Some((update, identity)) = context::profile_update(&self.hub.store, &snapshot, &self.cfg) {+ prompt = format!("{prompt}\n\n{update}");+ self.announce = Some((snapshot, identity));+ }+ if remind_files && self.active_group.is_none() && self.active_request.is_none() && self.active_routine.is_none()+ {+ prompt.push_str("\n\n[Codync: send_file(path, name?) shares any regular file up to 100 MiB as a downloadable card. Use it instead of sending a local path.]");+ }+ Ok(prompt)+ }+/// Starts a turn in `lane` for the given user entries (none for a hidden turn).pub(super) async fn start_turn(&mut self,@@ -73,7 +114,12 @@ impl Actor {);self.hub.set_runtime(&self.id(), |r| {r.status = BotStatus::Working;- r.activity = if hidden { "Picking up where it left off…" } else { "Starting…" }.into();+ r.activity = match (self.compacting.is_some(), hidden) {+ (true, _) => "Summarizing the earlier conversation…",+ (false, true) => "Picking up where it left off…",+ (false, false) => "Starting…",+ }+ .into();r.started_at = Some(now_ms());r.lane = Some(lane.clone());r.message = if from_user { entry_ids.last().cloned() } else { None };@@ -93,7 +139,9 @@ impl Actor {self.sync_fast_mode(&sid).await;}if hidden && self.session_fresh {- // A new session must never silently repeat an interrupted routine.+ // A new session must never silently repeat an interrupted routine, and has nothing to compact.+ self.compacting = None;+ self.raw_prompt = false;self.finish_routine(crate::routines::Status::Interrupted,Some("The agent could not reload this run's saved session; inspect before retrying".into()),@@ -126,39 +174,9 @@ impl Actor {}}}- let instructions = self.conn.as_ref().map_or(Instructions::FirstMessage, Conn::instructions);let sid = self.turn_session.clone().ok_or_else(|| anyhow!("no session"))?;- let mut snapshot = self.snapshot(&sid).await?;- let remind_files = !snapshot.system.contains("send_file");- let mut prompt = text.to_owned();- // 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 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}");- self.turn_memory_revision = revision;- }- if let Some(intro) = self.thread_intro.take() {- prompt = format!("{intro}\n\n{prompt}");- }- if std::mem::take(&mut self.session_fresh) && instructions == Instructions::FirstMessage {- prompt = format!("<bot-profile>\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);- } else if resend {- context::mark_announced(&self.hub.store, &self.cfg.id, &snapshot, snapshot.system_identity.clone())?;- snapshot.announced = snapshot.system_identity.clone();- prompt = format!("<bot-profile>\n{PROFILE_AGAIN}\n\n{}\n</bot-profile>\n\n{prompt}", snapshot.system);- }- if let Some((update, identity)) = context::profile_update(&self.hub.store, &snapshot, &self.cfg) {- prompt = format!("{prompt}\n\n{update}");- self.announce = Some((snapshot, identity));- }- if remind_files && self.active_group.is_none() && self.active_request.is_none() && self.active_routine.is_none()- {- prompt.push_str("\n\n[Codync: send_file(path, name?) shares any regular file up to 100 MiB as a downloadable card. Use it instead of sending a local path.]");- }+ let prompt =+ if std::mem::take(&mut self.raw_prompt) { text.to_owned() } else { self.compose(&sid, text).await? };self.hub.set_runtime(&self.id(), |r| r.activity = "Thinking…".into());let acp = self.conn.as_ref().ok_or_else(|| anyhow!("agent not running"))?.acp.clone();let params = json!({@@ -187,6 +205,7 @@ impl Actor {if self.turn.is_none() {return;}+ self.after_turn(done);if let (Some(conn), Some(sid)) = (self.conn.as_mut(), &self.turn_session)&& let Some(used) = conn.loaded.get_mut(sid){diff --git a/host/src/agent/bot/updates.rs b/host/src/agent/bot/updates.rsindex f33f3a8e..f480d7c7 100644--- a/host/src/agent/bot/updates.rs+++ b/host/src/agent/bot/updates.rs@@ -1,9 +1,8 @@//! Agent → client: `session/update`s, permission requests and streamed text mapped onto entries.use super::fast_mode::Listing;-use super::{Actor, Instructions, NoticeStyle, Seg, SegKind};+use super::{Actor, Seg, SegKind};use crate::agent::acp::{self, Incoming};-use crate::chat::context;use crate::hub::BotStatus;use crate::remote::push::{self, AlertKind};use crate::store::{EntryKind, Permission};@@ -48,38 +47,6 @@ impl Actor {}}- /// The agent summarized its context: the next turn gets freshly rendered instructions and memory (see- /// `context`), and a harness that got them in its first message gets them again.- fn compacted(&mut self, id: &str) {- let Some(sid) = self.turn_session.clone() else { return };- match context::bump_epoch(&self.hub.store, &self.cfg.id, &sid, id) {- Ok(true) => {- if 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);- }- let _ = self.dreamer.send(crate::chat::memory::DreamEvent::Flush);- self.notice("Earlier context was summarized to make room.", NoticeStyle::Info);- }- Ok(false) => {}- Err(error) => {- tracing::warn!(bot = %self.cfg.id, error = format!("{error:#}"), "couldn't record the compaction");- }- }- }-- /// An agent that doesn't report compactions still shows them in its usage.- fn track_usage(&mut self, used: u64) {- let Some(sid) = self.turn_session.clone() else { return };- let Some(conn) = self.conn.as_mut() else { return };- let before = conn.used.insert(sid, used);- if !conn.reports_compaction && compacted_by_usage(before, used) {- self.compacted(&format!("usage-{}", crate::store::now_ms()));- }- }-pub(super) fn on_update(&mut self, u: &Value) {let turn = self.turn.unwrap_or(0);match u["sessionUpdate"].as_str().unwrap_or_default() {@@ -136,8 +103,13 @@ impl Actor {if let Some(conn) = self.conn.as_mut() {conn.reports_compaction = true;}+ // Claude's adapter carries the summary on one of its completed updates.if u["status"] == "completed" {- self.compacted(u["compactionId"].as_str().unwrap_or_default());+ let summary = acp::content_text(&u["summary"]);+ self.compacted(+ u["compactionId"].as_str().unwrap_or_default(),+ (!summary.trim().is_empty()).then_some(summary),+ );}}"usage_update" => {@@ -146,7 +118,7 @@ impl Actor {crate::usage::ingest_claude_rate_limit(&self.hub, info);}if let Some(used) = u["used"].as_u64() {- self.track_usage(used);+ self.track_usage(used, u["size"].as_u64().unwrap_or(0));}}"plan" => {@@ -381,26 +353,10 @@ pub fn diff_summary(path: &str, old: &str, new: &str) -> Value {})}-/// Context use below which a fall isn't read as a compaction: a short session has little to summarize.-const COMPACTED_FROM: u64 = 20_000;--/// Whether a session's context use fell the way it does when the agent summarizes it: by more than half.-fn compacted_by_usage(before: Option<u64>, used: u64) -> bool {- before.is_some_and(|before| before >= COMPACTED_FROM && used < before / 2)-}-#[cfg(test)]mod tests {use super::*;- #[test]- fn a_halved_context_reads_as_a_compaction() {- assert!(compacted_by_usage(Some(90_000), 12_000));- assert!(!compacted_by_usage(Some(90_000), 60_000), "ordinary churn");- assert!(!compacted_by_usage(Some(8_000), 1_000), "too little to summarize");- assert!(!compacted_by_usage(None, 1_000), "the session's first report");- }-#[test]fn diff_counts_changed_middle() {let d = diff_summary("f", "a\nb\nc\n", "a\nB\nB2\nc\n");diff --git a/host/src/api/bots.rs b/host/src/api/bots.rsindex 669ad478..83bd744c 100644--- a/host/src/api/bots.rs+++ b/host/src/api/bots.rs@@ -226,6 +226,7 @@ pub(super) async fn call(hub: &Arc<Hub>, method: &str, b: Value) -> Result<Contr}"memory" | "saveMemory" | "pinMemory" | "archiveMemory" | "forgetMemory" | "clearMemory" | "undoDream"| "answerMemory" | "undoMemory" => crate::chat::memory::call(hub, method, &b).await?,+ "editHandoff" | "keepConversation" => crate::chat::handoff::call(hub, method, &b)?,"react" => {let e = hub.react(str_arg(&b, "entryId")?, str_arg(&b, "emoji")?)?;json!({"entry": e})diff --git a/host/src/chat/context.rs b/host/src/chat/context.rsindex ec7e213d..3c859b5f 100644--- a/host/src/chat/context.rs+++ b/host/src/chat/context.rs@@ -117,7 +117,19 @@ pub fn resolve(store: &Store, cfg: &BotConfig, session: &str) -> Result<SnapshotOk(s)}-/// Memory the instructions show changed: every session of the bot re-renders them on its next turn.+/// One session's instructions are rendered again on its next turn: before a compaction the host starts, so the+/// compaction keeps them as they are now.+pub fn refresh(store: &Store, bot_id: &str, session: &str) -> Result<()> {+ match load(store, bot_id, session) {+ Some(mut s) => {+ s.system.clear();+ save(store, bot_id, &s)+ }+ None => Ok(()),+ }+}++/// Every session of the bot renders its instructions again on its next turn (after the move from the old memory).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()) {@@ -178,6 +190,8 @@ pub fn render(store: &Store, cfg: &BotConfig) -> String {lines.push(String::new());lines.push(skills);}+ lines.push(String::new());+ lines.push(codync_mem::compaction::KEEP.to_owned());let mut out = lines.join("\n");let memory = crate::chat::memory::opening(store, cfg);if !memory.is_empty() {diff --git a/host/src/chat/handoff.rs b/host/src/chat/handoff.rsnew file mode 100644index 00000000..2659b3ab--- /dev/null+++ b/host/src/chat/handoff.rs@@ -0,0 +1,176 @@+//! A rollover's handoff. When the bot's main conversation is due to start over (see `agent::bot::compaction`), a+//! card in its chat shows what the new conversation will carry; the user can edit it, or keep the old conversation,+//! until their next message starts the new one with it. The card is a notice whose `handoff` field holds the draft,+//! so an app that doesn't know it still shows the text.++use crate::hub::Hub;+use crate::store::{BotConfig, Entry, EntryKind, Lane, Store};+use anyhow::{Result, anyhow, bail};+use codync_mem::compaction::{Handoff, Request};+use serde::{Deserialize, Serialize};+use serde_json::{Value, json};++/// The longest handoff a user can save.+const MAX_CHARS: usize = 20_000;+/// The user's messages a handoff repeats word for word.+const REQUESTS: usize = 4;+/// Main-chat entries read to build one.+const READ: i64 = 120;++#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]+#[serde(rename_all = "camelCase")]+pub enum Status {+ /// The next message starts a new conversation with it.+ Draft,+ Sent,+ /// The user kept the old conversation.+ Kept,+}++/// Why the conversation starts over.+#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]+#[serde(rename_all = "camelCase")]+pub enum Reason {+ /// A large conversation whose prompt cache expired: going on would read it all again at full price.+ Cold,+ /// A long absence after several summaries.+ Away,+ /// Summaries stopped freeing room.+ Full,+}++impl Reason {+ fn line(self) -> &'static str {+ match self {+ Self::Cold => {+ "This conversation is long and has gone quiet, so your next message starts a fresh one carrying this summary."+ }+ Self::Away => {+ "You've been away a while and this conversation has been summarized several times, so your next message starts a fresh one carrying this summary."+ }+ Self::Full => {+ "Summarizing this conversation no longer frees enough room, so your next message starts a fresh one carrying this summary."+ }+ }+ }+}++fn key(bot: &str) -> String {+ format!("handoff.{bot}")+}++/// Where the harness's last summary of a session is kept.+pub fn summary_key(bot: &str, session: &str) -> String {+ format!("compaction.summary.{bot}.{session}")+}++/// Posts the card for `session`, the bot's main conversation, built from its chat.+pub fn draft(hub: &Hub, cfg: &BotConfig, session: &str, reason: Reason) -> Result<Entry> {+ let text = build(&hub.store, &cfg.id, session)?;+ let lane = Lane::main(&cfg.id);+ let data = json!({+ "text": reason.line(),+ "style": "info",+ "author": cfg.id,+ "handoff": {"status": Status::Draft, "reason": reason, "text": text},+ });+ let entry = hub+ .add_entry(&lane, EntryKind::Notice, hub.store.max_turn(&cfg.id), &data)+ .ok_or_else(|| anyhow!("couldn't post the handoff"))?;+ hub.store.kv_set(&key(&cfg.id), &entry.id)?;+ Ok(entry)+}++fn build(store: &Store, bot: &str, session: &str) -> Result<String> {+ let entries = store.history(bot, i64::MAX, READ)?;+ let user = EntryKind::User.as_str();+ let requests: Vec<(String, String)> = entries+ .iter()+ .filter(|e| e.kind == user)+ .rev()+ .take(REQUESTS)+ .map(|e| (crate::chat::memory::ymd(e.created_at), e.data["text"].as_str().unwrap_or_default().to_owned()))+ .collect::<Vec<_>>()+ .into_iter()+ .rev()+ .collect();+ let requests: Vec<Request> = requests.iter().map(|(date, text)| Request { date, text }).collect();+ let reply = entries.iter().rev().find(|e| e.kind == "agent" && e.data["final"] == true);+ let plan = entries.iter().rev().find(|e| e.kind == "plan").map(|e| plan_text(&e.data));+ let summary = store.kv_get(&summary_key(bot, session));+ let bookmark = entries.iter().rev().find(|e| e.kind == user || (e.kind == "agent" && e.data["final"] == true));+ Ok(Handoff {+ summary: summary.as_deref(),+ requests: &requests,+ reply: reply.and_then(|e| e.data["text"].as_str()),+ plan: plan.as_deref(),+ bookmark: bookmark.map(|e| e.id.as_str()),+ }+ .render())+}++fn plan_text(data: &Value) -> String {+ let steps = data["entries"].as_array().into_iter().flatten();+ steps+ .map(|s| {+ format!("- [{}] {}", s["status"].as_str().unwrap_or("pending"), s["content"].as_str().unwrap_or_default())+ })+ .collect::<Vec<_>>()+ .join("\n")+}++/// The draft the next main-chat message starts a new conversation with: its card and text.+pub fn pending(store: &Store, bot: &str) -> Option<(String, String)> {+ let id = store.kv_get(&key(bot)).filter(|id| !id.is_empty())?;+ let entry = store.entry(&id)?;+ let card = &entry.data["handoff"];+ (card["status"] == json!(Status::Draft)).then(|| (id, card["text"].as_str().unwrap_or_default().to_owned()))+}++/// Whether the user kept the old conversation over the current card; forgets the card either way once it's settled.+pub fn kept(store: &Store, bot: &str) -> bool {+ let Some(id) = store.kv_get(&key(bot)).filter(|id| !id.is_empty()) else { return false };+ let status = store.entry(&id).map(|e| e.data["handoff"]["status"].clone());+ let kept = status.as_ref() == Some(&json!(Status::Kept));+ if status.as_ref() != Some(&json!(Status::Draft)) {+ let _ = store.kv_delete(&key(bot));+ }+ kept+}++/// The new conversation started with the card's text.+pub fn sent(hub: &Hub, bot: &str, id: &str) {+ set_status(hub, id, Status::Sent, "Started a fresh conversation with this summary.");+ let _ = hub.store.kv_delete(&key(bot));+}++fn set_status(hub: &Hub, id: &str, status: Status, line: &str) -> Option<Entry> {+ let mut data = hub.store.entry(id)?.data;+ data["handoff"]["status"] = json!(status);+ data["text"] = line.into();+ hub.set_entry(id, &data)+}++/// `editHandoff` and `keepConversation`.+pub fn call(hub: &Hub, method: &str, b: &Value) -> Result<Value> {+ let id = b["entryId"].as_str().ok_or_else(|| anyhow!("entryId required"))?;+ let entry = hub.store.entry(id).filter(|e| e.data["handoff"].is_object()).ok_or_else(|| anyhow!("no such card"))?;+ if entry.data["handoff"]["status"] != json!(Status::Draft) {+ bail!("This conversation already moved on.");+ }+ let entry = match method {+ "editHandoff" => {+ let text =+ b["text"].as_str().map(str::trim).filter(|t| !t.is_empty()).ok_or_else(|| anyhow!("text required"))?;+ if text.chars().count() > MAX_CHARS {+ bail!("A handoff can be at most {MAX_CHARS} characters.");+ }+ let mut data = entry.data;+ data["handoff"]["text"] = text.into();+ hub.set_entry(id, &data)+ }+ "keepConversation" => set_status(hub, id, Status::Kept, "Kept this conversation."),+ _ => bail!("unknown method {method}"),+ };+ Ok(json!({"entry": entry}))+}diff --git a/host/src/chat/memory/mod.rs b/host/src/chat/memory/mod.rsindex fca1483a..3bd1d762 100644--- a/host/src/chat/memory/mod.rs+++ b/host/src/chat/memory/mod.rs@@ -11,6 +11,7 @@ mod notices;mod tools;pub use api::call;+pub use dates::ymd;pub use dream::{DreamEvent, spawn_dreamer};pub use import::import_old_memory;pub use tools::{INSTRUCTIONS, call as tool_call, tools};diff --git a/host/src/chat/mod.rs b/host/src/chat/mod.rsindex 41836a7b..60179e06 100644--- a/host/src/chat/mod.rs+++ b/host/src/chat/mod.rs@@ -4,6 +4,7 @@pub mod context;pub mod files;pub mod group;+pub mod handoff;pub mod memory;pub mod naming;pub mod oneshot;diff --git a/host/src/chat/oneshot.rs b/host/src/chat/oneshot.rsindex 75ca9617..719cc9cc 100644--- a/host/src/chat/oneshot.rs+++ b/host/src/chat/oneshot.rs@@ -1,7 +1,7 @@//! Background jobs on the bot's own harness (dreaming, automatic naming): one prompt to a throwaway agent.use crate::agent::acp::{self, Acp, Incoming};-use crate::agent::bot::Instructions;+use crate::agent::bot::{Adapter, Instructions};use crate::hub::Hub;use crate::store::BotConfig;use anyhow::{Result, anyhow, bail};@@ -57,7 +57,7 @@ pub async fn one_shot(hub: &Arc<Hub>, cfg: &BotConfig, system: &str, user: &str)let sid = res["sessionId"].as_str().ok_or_else(|| anyhow!("agent returned no sessionId"))?.to_owned();// Same model as the bot: the harness default may be another (local) model that// would load next to the bot's, or a cloud one the user kept these chats away from.- if !conn.claude {+ if conn.adapter != Adapter::Claude {crate::agent::bot::select_model(&acp, &res, &sid, cfg).await?;}let prompt = acp.request("session/prompt", json!({"sessionId": sid, "prompt": [{"type": "text", "text": text}]}));diff --git a/host/tests/common/host.rs b/host/tests/common/host.rsnew file mode 100644index 00000000..f1bbdd1b--- /dev/null+++ b/host/tests/common/host.rs@@ -0,0 +1,140 @@+//! A real host on disposable data, and what the end-to-end tests ask of it.++// Each test binary compiles `common` and uses only part of this.+#![allow(dead_code)]++use serde_json::{Value, json};+use std::{+ path::PathBuf,+ process::Child,+ time::{Duration, Instant},+};++pub struct Host {+ child: Child,+ /// The host's `CODYNC_HOME`.+ pub home: PathBuf,+ base: String,+ token: String,+}++impl Drop for Host {+ fn drop(&mut self) {+ super::stop(&mut self.child);+ let _ = std::fs::remove_dir_all(&self.home);+ }+}++impl Host {+ pub async fn start() -> Self {+ let home = std::env::temp_dir().join(format!("codync-memory-e2e-{}", uuid::Uuid::new_v4()));+ let (child, base) = super::serve(&home).await;+ let token = std::fs::read_to_string(home.join("token")).unwrap().trim().to_owned();+ Self { child, home, base, token }+ }++ /// Stops the host, lets `between` change its files, and starts it again on the same data.+ pub async fn restart(&mut self, between: impl FnOnce(&std::path::Path)) {+ super::stop(&mut self.child);+ between(&self.home);+ let (child, base) = super::serve(&self.home).await;+ self.child = child;+ self.base = base;+ }++ pub async fn post(&self, method: &str, body: &Value) -> (bool, Value) {+ let response = reqwest::Client::new()+ .post(format!("{}/api/{method}", self.base))+ .bearer_auth(&self.token)+ .json(body)+ .timeout(Duration::from_secs(120))+ .send()+ .await+ .unwrap();+ let ok = response.status().is_success();+ (ok, response.json().await.unwrap())+ }++ pub async fn call(&self, method: &str, body: Value) -> Value {+ let (ok, body) = self.post(method, &body).await;+ assert!(ok, "{method}: {body}");+ body+ }++ /// A call that must be refused: its error text.+ pub async fn refused(&self, method: &str, body: Value) -> String {+ let (ok, body) = self.post(method, &body).await;+ assert!(!ok, "{method} should have been refused: {body}");+ body["error"].as_str().unwrap_or_default().to_owned()+ }++ pub async fn bot(&self, name: &str, permission: &str) -> String {+ let bot =+ json!({"name": name, "backend": "custom", "command": "unused", "connectors": [], "permission": permission});+ self.call("createBot", bot).await["bot"]["id"].as_str().unwrap().to_owned()+ }++ pub async fn memory(&self, bot: &str) -> Value {+ self.call("memory", json!({"botId": bot})).await+ }++ pub async fn tool(&self, bot: &str, name: &str, arguments: Value) -> Value {+ self.call("memoryCall", json!({"botId": bot, "name": name, "arguments": arguments})).await+ }++ pub async fn tool_refused(&self, bot: &str, name: &str, arguments: Value) -> String {+ self.refused("memoryCall", json!({"botId": bot, "name": name, "arguments": arguments})).await+ }++ /// A bot running `tests/fixtures/<fixture>` with `args`.+ pub async fn fixture_bot(&self, name: &str, fixture: &str, args: &str) -> String {+ let script = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(fixture);+ let command = format!("{} {args}", super::python_agent(&script));+ let bot = json!({"name": name, "backend": "custom", "command": command, "connectors": [], "notify": false});+ self.call("createBot", bot).await["bot"]["id"].as_str().unwrap().to_owned()+ }++ /// Sends `text` and waits for the bot's reply.+ pub async fn reply(&self, bot: &str, text: &str) {+ let finals = |history: &Value| {+ history["entries"]+ .as_array()+ .unwrap()+ .iter()+ .filter(|e| e["kind"] == "agent" && e["data"]["final"] == true)+ .count()+ };+ let before = finals(&self.call("history", json!({"botId": bot})).await);+ let nonce = uuid::Uuid::new_v4().to_string();+ self.call("send", json!({"botId": bot, "text": text, "clientNonce": nonce})).await;+ let deadline = Instant::now() + Duration::from_secs(30);+ loop {+ let history = self.call("history", json!({"botId": bot})).await;+ if finals(&history) > before {+ return;+ }+ assert!(Instant::now() < deadline, "{bot} didn't answer {text:?}: {history}");+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ }++ /// The requests `instructions_agent.py` logged in a bot's workspace.+ pub fn requests(&self, bot: &str, method: &str) -> Vec<Value> {+ let log = std::fs::read_to_string(self.home.join("bots").join(bot).join("workspace/requests.jsonl")).unwrap();+ log.lines().map(|l| serde_json::from_str::<Value>(l).unwrap()).filter(|r| r["method"] == method).collect()+ }++ /// The text of every prompt a bot's agent got, oldest first.+ pub fn prompts(&self, bot: &str) -> Vec<String> {+ self.requests(bot, "session/prompt")+ .iter()+ .map(|r| r["params"]["prompt"][0]["text"].as_str().unwrap().to_owned())+ .collect()+ }++ /// The newest memory line in a bot's chat.+ pub async fn card(&self, bot: &str) -> Value {+ let history = self.call("history", json!({"botId": bot})).await;+ history["entries"].as_array().unwrap().iter().rev().find(|e| e["data"]["memory"].is_object()).unwrap().clone()+ }+}diff --git a/host/tests/common/mod.rs b/host/tests/common/mod.rsindex 07bac032..861889aa 100644--- a/host/tests/common/mod.rs+++ b/host/tests/common/mod.rs@@ -1,5 +1,7 @@//! Shared by the end-to-end tests.+pub mod host;+use std::path::Path;use std::process::{Child, Command, Stdio};use std::time::{Duration, Instant};diff --git a/host/tests/compaction_e2e.rs b/host/tests/compaction_e2e.rsnew file mode 100644index 00000000..3f209452--- /dev/null+++ b/host/tests/compaction_e2e.rs@@ -0,0 +1,96 @@+//! Compaction and rollover on a real host with disposable data: the host compacting a full conversation at the+//! end of a turn, its loop guard turning to a rollover, and the handoff card the user edits or turns down.++#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]++#[allow(dead_code)]+mod common;++use common::host::Host;+use serde_json::{Value, json};+use std::time::{Duration, Instant};++/// Waits until the bot's chat has a notice with this field, and returns the newest one.+async fn notice(host: &Host, bot: &str, field: &str) -> Value {+ let deadline = Instant::now() + Duration::from_secs(30);+ loop {+ let history = host.call("history", json!({"botId": bot})).await;+ if let Some(e) = history["entries"].as_array().unwrap().iter().rev().find(|e| e["data"][field].is_object()) {+ return e.clone();+ }+ assert!(Instant::now() < deadline, "no {field} notice: {history}");+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+}++#[tokio::test]+async fn a_full_conversation_is_compacted_with_its_keep_list() {+ let host = Host::start().await;+ let claude = host.fixture_bot("Claude-like", "instructions_agent.py", "claude").await;+ host.reply(&claude, "hello").await;+ host.call("saveMemory", json!({"botId": claude, "text": "Allergic to peanuts"})).await;+ host.reply(&claude, "USAGE 210000 SIZE 1000000").await;+ let line = notice(&host, &claude, "compaction").await;+ assert_eq!(line["data"]["compaction"]["byHost"], true);+ assert_eq!(line["data"]["compaction"]["summary"], "Summary 1: the user is planning a trip.");+ let prompts = host.prompts(&claude);+ let compact = prompts.iter().find(|p| p.starts_with("/compact")).unwrap();+ assert!(compact.starts_with("/compact When your context is summarized"), "Claude gets the keep-list: {compact}");+ // Memory changed since the session opened, so the compaction keeps an opening rendered just before it.+ let loads = host.requests(&claude, "session/load");+ let opening = loads.last().unwrap()["params"]["_meta"]["systemPrompt"]["append"].as_str().unwrap();+ assert!(opening.contains("Allergic to peanuts"), "{opening}");++ let codex = host.fixture_bot("Codex-like", "instructions_agent.py", "codex").await;+ host.reply(&codex, "USAGE 150000 SIZE 200000").await;+ notice(&host, &codex, "compaction").await;+ assert!(host.prompts(&codex).iter().any(|p| p == "/compact"), "Codex's keep-list is in its config");+}++#[tokio::test]+async fn compacting_too_often_turns_into_a_rollover_the_user_can_edit_or_turn_down() {+ let host = Host::start().await;+ let bot = host.fixture_bot("Busy", "instructions_agent.py", "codex").await;+ host.reply(&bot, "USAGE 210000 SIZE 1000000").await;+ notice(&host, &bot, "compaction").await;+ // Full again right after a compaction: compacting again wouldn't help.+ host.reply(&bot, "USAGE 210000 SIZE 1000000").await;+ let card = notice(&host, &bot, "handoff").await;+ assert_eq!(card["data"]["handoff"]["status"], "draft");+ let text = card["data"]["handoff"]["text"].as_str().unwrap();+ assert!(text.starts_with("<handoff>") && text.contains("USAGE 210000"), "{text}");++ host.call("editHandoff", json!({"entryId": card["id"], "text": "<handoff>We were booking a train.</handoff>"}))+ .await;+ host.reply(&bot, "Which seat did we pick?").await;+ let news = host.requests(&bot, "session/new");+ assert_eq!(news.len(), 2, "a new conversation");+ let last = host.prompts(&bot).last().unwrap().clone();+ assert!(last.contains("We were booking a train.") && last.contains("Which seat did we pick?"), "{last}");+ let card = host.call("history", json!({"botId": bot})).await["entries"]+ .as_array()+ .unwrap()+ .iter()+ .find(|e| e["id"] == card["id"])+ .unwrap()+ .clone();+ assert_eq!(card["data"]["handoff"]["status"], "sent");+ assert!(host.refused("editHandoff", json!({"entryId": card["id"], "text": "late"})).await.contains("moved on"));++ // Turned down: the conversation carries on.+ host.reply(&bot, "USAGE 210000 SIZE 1000000").await;+ notice(&host, &bot, "compaction").await;+ host.reply(&bot, "USAGE 210000 SIZE 1000000").await;+ let card = host.call("history", json!({"botId": bot})).await["entries"]+ .as_array()+ .unwrap()+ .iter()+ .rev()+ .find(|e| e["data"]["handoff"]["status"] == "draft")+ .unwrap()+ .clone();+ host.call("keepConversation", json!({"entryId": card["id"]})).await;+ host.reply(&bot, "carry on").await;+ assert_eq!(host.requests(&bot, "session/new").len(), 2, "same conversation");+ assert!(!host.prompts(&bot).last().unwrap().contains("<handoff>"));+}diff --git a/host/tests/fixtures/instructions_agent.py b/host/tests/fixtures/instructions_agent.pyindex e353e954..20814e1f 100644--- a/host/tests/fixtures/instructions_agent.py+++ b/host/tests/fixtures/instructions_agent.py@@ -1,8 +1,10 @@-"""ACP fixture recording how a bot's instructions reach it.+"""ACP fixture recording how a bot's instructions and compactions reach it.`standard` advertises ACP `systemPrompt`; `reporting` reports compactions with `compaction_update`; `silent` only-shows them in `usage_update`. A prompt containing COMPACT_NOW compacts, one containing `USAGE <n>` reports that-much context in use. Every request is appended to `requests.jsonl` in the working directory.+shows them in `usage_update`; `codex` and `claude` answer `initialize` the way those adapters do, so the host asks+them to compact. A prompt containing COMPACT_NOW, or one that is a `/compact` command, compacts; one containing+`USAGE <n>` (optionally `SIZE <m>`) reports that much context in use. Every request is appended to+`requests.jsonl` in the working directory."""import jsonimport re@@ -10,6 +12,7 @@ import sysmode = sys.argv[1] if len(sys.argv) > 1 else "reporting"sessions = 0+compactions = 0def send(value):@@ -20,6 +23,18 @@ def update(session, value):send({"method": "session/update", "params": {"sessionId": session, "update": value}})+def compact(session):+ global compactions+ compactions += 1+ cid = f"c{compactions}"+ update(session, {"sessionUpdate": "compaction_update", "compactionId": cid, "status": "in_progress"})+ completed = {"sessionUpdate": "compaction_update", "compactionId": cid, "status": "completed"}+ if mode == "claude":+ completed["summary"] = [{"type": "text", "text": f"Summary {compactions}: the user is planning a trip."}]+ update(session, completed)+ update(session, {"sessionUpdate": "usage_update", "used": 20000, "size": 1000000})++for line in sys.stdin:request = json.loads(line)method = request.get("method")@@ -27,22 +42,32 @@ for line in sys.stdin:with open("requests.jsonl", "a", encoding="utf-8") as log:log.write(json.dumps({"method": method, "params": params}) + "\n")if method == "initialize":- capabilities = {"loadSession": False, "sessionCapabilities": {}}+ capabilities = {"loadSession": mode == "claude", "sessionCapabilities": {}}if mode == "standard":capabilities["sessionCapabilities"]["systemPrompt"] = {}+ if mode == "claude":+ capabilities["_meta"] = {"claudeCode": {}}result = {"protocolVersion": 1, "agentCapabilities": capabilities}+ if mode == "codex":+ result["agentInfo"] = {"name": "@agentclientprotocol/codex-acp", "version": "0"}elif method == "session/new":sessions += 1result = {"sessionId": f"s{sessions}"}+ elif method == "session/load":+ result = {}elif method == "session/prompt":session = params["sessionId"]text = params["prompt"][0]["text"]- if "COMPACT_NOW" in text and mode != "silent":- update(session, {"sessionUpdate": "compaction_update", "compactionId": "c1", "status": "in_progress"})- update(session, {"sessionUpdate": "compaction_update", "compactionId": "c1", "status": "completed"})+ if text.startswith("/compact") or ("COMPACT_NOW" in text and mode != "silent"):+ compact(session)+ if text.startswith("/compact"):+ send({"id": request["id"], "result": {"stopReason": "end_turn"}})+ continueusage = re.search(r"USAGE (\d+)", text)if usage:- update(session, {"sessionUpdate": "usage_update", "used": int(usage.group(1)), "size": 200000})+ size = re.search(r"SIZE (\d+)", text)+ update(session, {"sessionUpdate": "usage_update", "used": int(usage.group(1)),+ "size": int(size.group(1)) if size else 200000})update(session, {"sessionUpdate": "agent_message_chunk", "content": {"type": "text", "text": "ok"}})result = {"stopReason": "end_turn"}else:diff --git a/host/tests/memory_e2e.rs b/host/tests/memory_e2e.rsindex 88827225..2d551f4e 100644--- a/host/tests/memory_e2e.rs+++ b/host/tests/memory_e2e.rs@@ -6,141 +6,13 @@#[allow(dead_code)]mod common;+use common::host::Host;use serde_json::{Value, json};use std::{path::PathBuf,- process::Child,time::{Duration, Instant},};-struct Host {- child: Child,- home: PathBuf,- base: String,- token: String,-}--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-memory-e2e-{}", uuid::Uuid::new_v4()));- let (child, base) = common::serve(&home).await;- let token = std::fs::read_to_string(home.join("token")).unwrap().trim().to_owned();- Self { child, home, base, token }- }-- /// Stops the host, lets `between` change its files, and starts it again on the same data.- async fn restart(&mut self, between: impl FnOnce(&std::path::Path)) {- common::stop(&mut self.child);- between(&self.home);- let (child, base) = common::serve(&self.home).await;- self.child = child;- self.base = base;- }-- async fn post(&self, method: &str, body: &Value) -> (bool, Value) {- let response = reqwest::Client::new()- .post(format!("{}/api/{method}", self.base))- .bearer_auth(&self.token)- .json(body)- .timeout(Duration::from_secs(120))- .send()- .await- .unwrap();- let ok = response.status().is_success();- (ok, response.json().await.unwrap())- }-- async fn call(&self, method: &str, body: Value) -> Value {- let (ok, body) = self.post(method, &body).await;- assert!(ok, "{method}: {body}");- body- }-- /// A call that must be refused: its error text.- async fn refused(&self, method: &str, body: Value) -> String {- let (ok, body) = self.post(method, &body).await;- assert!(!ok, "{method} should have been refused: {body}");- body["error"].as_str().unwrap_or_default().to_owned()- }-- async fn bot(&self, name: &str, permission: &str) -> String {- let bot =- json!({"name": name, "backend": "custom", "command": "unused", "connectors": [], "permission": permission});- self.call("createBot", bot).await["bot"]["id"].as_str().unwrap().to_owned()- }-- async fn memory(&self, bot: &str) -> Value {- self.call("memory", json!({"botId": bot})).await- }-- async fn tool(&self, bot: &str, name: &str, arguments: Value) -> Value {- self.call("memoryCall", json!({"botId": bot, "name": name, "arguments": arguments})).await- }-- async fn tool_refused(&self, bot: &str, name: &str, arguments: Value) -> String {- self.refused("memoryCall", json!({"botId": bot, "name": name, "arguments": arguments})).await- }-- /// A bot running `tests/fixtures/<fixture>` with `args`.- async fn fixture_bot(&self, name: &str, fixture: &str, args: &str) -> String {- let script = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures").join(fixture);- let command = format!("{} {args}", common::python_agent(&script));- let bot = json!({"name": name, "backend": "custom", "command": command, "connectors": [], "notify": false});- self.call("createBot", bot).await["bot"]["id"].as_str().unwrap().to_owned()- }-- /// Sends `text` and waits for the bot's reply.- async fn reply(&self, bot: &str, text: &str) {- let finals = |history: &Value| {- history["entries"]- .as_array()- .unwrap()- .iter()- .filter(|e| e["kind"] == "agent" && e["data"]["final"] == true)- .count()- };- let before = finals(&self.call("history", json!({"botId": bot})).await);- let nonce = uuid::Uuid::new_v4().to_string();- self.call("send", json!({"botId": bot, "text": text, "clientNonce": nonce})).await;- let deadline = Instant::now() + Duration::from_secs(30);- loop {- let history = self.call("history", json!({"botId": bot})).await;- if finals(&history) > before {- return;- }- assert!(Instant::now() < deadline, "{bot} didn't answer {text:?}: {history}");- tokio::time::sleep(Duration::from_millis(50)).await;- }- }-- /// The requests `instructions_agent.py` logged in a bot's workspace.- fn requests(&self, bot: &str, method: &str) -> Vec<Value> {- let log = std::fs::read_to_string(self.home.join("bots").join(bot).join("workspace/requests.jsonl")).unwrap();- log.lines().map(|l| serde_json::from_str::<Value>(l).unwrap()).filter(|r| r["method"] == method).collect()- }-- /// The text of every prompt a bot's agent got, oldest first.- fn prompts(&self, bot: &str) -> Vec<String> {- self.requests(bot, "session/prompt")- .iter()- .map(|r| r["params"]["prompt"][0]["text"].as_str().unwrap().to_owned())- .collect()- }-- /// The newest memory line in a bot's chat.- async fn card(&self, bot: &str) -> Value {- let history = self.call("history", json!({"botId": bot})).await;- history["entries"].as_array().unwrap().iter().rev().find(|e| e["data"]["memory"].is_object()).unwrap().clone()- }-}-fn texts(layer: &Value, state: &str) -> Vec<String> {layer["entries"].as_array()