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
fix(host): tidy only the memory that is nearly full
leepokai019b48761917
diff --git a/host/Cargo.lock b/host/Cargo.lockindex 247d089a..3a850c70 100644--- a/host/Cargo.lock+++ b/host/Cargo.lock@@ -634,7 +634,7 @@ dependencies = [[[package]]name = "codync-mem"-version = "0.3.0"+version = "0.3.1"dependencies = ["rusqlite","serde",diff --git a/host/crates/memory/Cargo.toml b/host/crates/memory/Cargo.tomlindex a63e1db1..15c94a80 100644--- a/host/crates/memory/Cargo.toml+++ b/host/crates/memory/Cargo.toml@@ -1,6 +1,6 @@[package]name = "codync-mem"-version = "0.3.0"+version = "0.3.1"edition = "2024"rust-version = "1.88"description = "Memory for coding-agent bots: one store, the rules for who may write it, and what each session sees"diff --git a/host/crates/memory/src/dream/mod.rs b/host/crates/memory/src/dream/mod.rsindex 43f7db16..0c62b67a 100644--- a/host/crates/memory/src/dream/mod.rs+++ b/host/crates/memory/src/dream/mod.rs@@ -83,25 +83,31 @@ impl Snapshot {pub fn layer_bot(&self, shared: bool) -> Option<&str> {(!shared).then_some(self.bot.as_str())}++ /// Whether a layer's `MEMORY.md` is nearly full ([`full`]): only then do its lines move out to notes.+ fn layer_full(&self, shared: bool) -> bool {+ let content = self.layer(shared).and_then(|l| l.content.as_deref()).unwrap_or_default();+ full(content, self.layer_bot(shared))+ }}pub fn snapshot(conn: &Connection, bot: &str, shared: bool, today: &str) -> store::Result<Snapshot> {let mut layers = Vec::new();- let mut tidy = false;for (layer, is_shared) in std::iter::once((Some(bot), false)).chain(shared.then_some((None, true))) {let content = files::current(conn, layer, MAIN)?;- tidy |= full(content.as_deref().unwrap_or_default(), layer);layers.push(Layer { shared: is_shared, content, facts: files::facts_of(conn, layer)? });}let index = files::index(conn, bot, shared)?;- Ok(Snapshot {+ let mut snapshot = Snapshot {bot: bot.to_owned(),shared,layers,files: index.yours.unwrap_or_default().into_iter().chain(index.bot).collect(),- full: tidy,+ full: false,today: today.to_owned(),- })+ };+ snapshot.full = snapshot.layers.iter().any(|l| snapshot.layer_full(l.shared));+ Ok(snapshot)}/// Whether a layer's `MEMORY.md` (`layer`: `None` for the shared one) is nine-tenths full: a run then tidies it,@@ -110,12 +116,13 @@ pub fn full(content: &str, layer: Option<&str>) -> bool {crate::tokens::estimate(content) * 10 >= files::cap(layer) * 9}-/// What the prompt adds when MEMORY.md is nearly full: after the rule on the user's lines (without it the model reads-/// a move as a removal and leaves a full MEMORY.md of the user's lines as it is), the tidying rule, the output shape.+/// What the prompt adds when a MEMORY.md is nearly full (the material marks which): after the rule on the user's lines+/// (without it the model reads a move as a removal and leaves a full MEMORY.md of the user's lines as it is), the+/// tidying rule, the output shape.const TIDY: [&str; 3] = [" Moving one word for word into a note (\"moveOut\") keeps it, so their lines move too.",- "\n- MEMORY.md is nearly full, so tidy it the way a project's CLAUDE.md keeps one line per topic and points \-into its docs: \"moveOut\" moves a topic's detail lines word for word into the note for that topic, and \"summary\" \+ "\n- Tidy each MEMORY.md shown as nearly full, and no other, the way a project's CLAUDE.md keeps one line per \+topic and points into its docs: \"moveOut\" moves a topic's detail lines word for word into the note for that topic, and \"summary\" \is the one short line MEMORY.md keeps for the topic instead, saying what every reply still needs, in the user's \language. The host adds \" → \" and the note's path. A line already ending in \"→ path\" points to a note: don't \move it; give a new summary for its note when the moved lines change what it should say.",@@ -174,7 +181,12 @@ Output only one JSON object:.layers.iter().flat_map(|layer| {- let where_ = if layer.shared { "Shared (\"shared\": true)" } else { "This bot's (\"shared\": false)" };+ let nearly = if snapshot.layer_full(layer.shared) { ", nearly full" } else { "" };+ let where_ = if layer.shared {+ format!("Shared (\"shared\": true{nearly})")+ } else {+ format!("This bot's (\"shared\": false{nearly})")+ };[(true, "written by the user"), (false, "the rest")].into_iter().filter_map(move |(users, who)| {let lines: Vec<String> = layer.factsdiff --git a/host/crates/memory/src/dream/notes.rs b/host/crates/memory/src/dream/notes.rsindex 201a9ed4..fb12b17d 100644--- a/host/crates/memory/src/dream/notes.rs+++ b/host/crates/memory/src/dream/notes.rs@@ -160,7 +160,8 @@ pub(super) fn audit(moved.shared &= snapshot.shared;describe(moved.shared, &moved.file, &mut moved.description);let verdict = (|| {- if !snapshot.full {+ // Only the layer that is nearly full gets tidied: the other keeps its lines, the user's above all.+ if !snapshot.layer_full(moved.shared) {return Err("MEMORY.md isn't full");}// Even a line the user wrote moves: word for word, into a note of the same layer.diff --git a/host/crates/memory/src/dream/tests.rs b/host/crates/memory/src/dream/tests.rsindex 2c1d7cd5..46fdadc2 100644--- a/host/crates/memory/src/dream/tests.rs+++ b/host/crates/memory/src/dream/tests.rs@@ -27,13 +27,13 @@ fn remove(line: &str, quote: &str, message: &str) -> Remove {/// Writes bot `a`'s MEMORY.md as `author` wrote it.fn seed(conn: &Connection, author: Author, content: &str) {- write(conn, author, MAIN, content);+ write(conn, Some("a"), author, MAIN, content);}-/// Writes bot `a`'s file `name` as `author` wrote it.-fn write(conn: &Connection, author: Author, name: &str, content: &str) {+/// Writes the file `name` of `bot`'s layer (`None`: the shared one) as `author` wrote it.+fn write(conn: &Connection, bot: Option<&str>, author: Author, name: &str, content: &str) {let change = Change {- bot: Some("a"),+ bot,name,content: Some(content),author,diff --git a/host/crates/memory/src/dream/tests/notes.rs b/host/crates/memory/src/dream/tests/notes.rsindex adb6d020..2363e709 100644--- a/host/crates/memory/src/dream/tests/notes.rs+++ b/host/crates/memory/src/dream/tests/notes.rs@@ -85,7 +85,7 @@ fn a_full_memory_moves_detail_out_and_the_log_keeps_the_run() {// A note that can't take the line leaves it in MEMORY.md.let crowded = format!("---\ndescription: The user's week\n---\n{}", "字".repeat(FILE_CAP - 10));- write(&conn, Author::Bot, "week.md", &crowded);+ write(&conn, Some("a"), Author::Bot, "week.md", &crowded);let crowded_snapshot = super::snapshot(&conn, "a", false, "2026-10-17").unwrap();let (kept, _) = audit(&crowded_snapshot, &[], kept);let (stuck, rejected) = super::edits(&conn, &crowded_snapshot, &kept, &[]).unwrap();@@ -131,7 +131,7 @@ fn a_full_memory_tidies_into_topic_folders_even_the_users_lines() {seed(&conn, Author::User, &lines([filler(0)]));seed(&conn, Author::Bot, &lines((0..9).map(filler)));let snapshot = snapshot(&conn, "a", false, "2026-10-17").unwrap();- assert!(snapshot.full && full(snapshot.layers[0].content.as_deref().unwrap(), Some("a")));+ assert!(snapshot.full && snapshot.layer_full(false));assert!(prompt(&snapshot, &[]).0.contains("their lines move too"), "a move isn't a removal to the model");// The new note is described once, on the first line sent there.let into = |i, summary: &str| {@@ -212,7 +212,13 @@ fn a_topic_keeps_one_line_pointing_to_its_note() {fn without_a_summary_a_pointer_says_what_its_note_is_about() {let conn = db();seed(&conn, Author::Bot, &lines((0..9).map(filler)));- write(&conn, Author::Bot, "health/allergies.md", "---\ndescription: The user's allergies\n---\n- Peanuts\n");+ write(+ &conn,+ Some("a"),+ Author::Bot,+ "health/allergies.md",+ "---\ndescription: The user's allergies\n---\n- Peanuts\n",+ );let snapshot = snapshot(&conn, "a", false, "2026-10-17").unwrap();let pointer_to = |file: &str, description: &str| {let plan = Plan { move_out: vec![move_out(&filler(0), file, description, "")], ..Plan::default() };@@ -284,3 +290,22 @@ fn a_line_memory_md_keeps_doesnt_go_into_a_note_too() {assert!(edits.files.is_empty(), "{edits:?}");assert!(rejected.iter().any(|r| r.contains(line) && r.contains("wasn't changed")), "{rejected:?}");}++#[test]+fn only_the_layer_that_is_nearly_full_gets_tidied() {+ let conn = db();+ seed(&conn, Author::Bot, &lines((0..9).map(filler)));+ write(&conn, None, Author::User, MAIN, "- Lives in Taipei\n");+ let snapshot = snapshot(&conn, "a", true, "2026-10-17").unwrap();+ assert!(snapshot.full, "this bot's own MEMORY.md is nearly full");+ let (system, material) = prompt(&snapshot, &[]);+ assert!(system.contains("Tidy each MEMORY.md shown as nearly full"), "{system}");+ assert!(material.contains("This bot's (\"shared\": false, nearly full)"), "{material}");+ assert!(material.contains("Shared (\"shared\": true)"), "only the full layer is marked: {material}");+ let from_shared = MoveOut { shared: true, ..move_out("Lives in Taipei", "home.md", "Where the user lives", "") };+ let own = move_out(&filler(0), "week.md", "The user's week", "");+ let (kept, rejected) = audit(&snapshot, &[], Plan { move_out: vec![from_shared, own], ..Plan::default() });+ assert_eq!(kept.move_out.len(), 1, "{rejected:?}");+ assert!(!kept.move_out[0].shared, "the bot's own full layer is tidied");+ assert!(rejected[0].contains("Lives in Taipei") && rejected[0].contains("isn't full"), "{rejected:?}");+}