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): make taking back a memory stick

leepokai3329a031f256
diff --git a/docs/features/context-and-memory.md b/docs/features/context-and-memory.md
index 75b36668..5aa1c519 100644
--- a/docs/features/context-and-memory.md
+++ b/docs/features/context-and-memory.md
@@ -56,8 +56,10 @@ the `codync-mem` crate; `chat/memory` runs them in the host.
- **The bot's tools** (built-in `memory` MCP server): `remember`, `revise_memory`, `archive_memory`,
`search_memory`, `search_history`. A bot on automatic approval writes at once, in any turn and to either layer;
the chat shows a "Remembered" line the user can undo. A bot that asks first shows a card the user approves or
- declines, unless the user's own message in that turn asked it to remember. A declined or undone memory is
- never written again by a bot.
+ declines, unless the user's own message in that turn asked it to remember. A declined, undone or forgotten
+ memory is never written again by a bot or dreaming, reworded or not: its text stays closed to that layer, and
+ the message it came from to both layers the bot uses. Entry ids are never reused, so an old undo line can't
+ reach a newer entry.
- **Checks** on every write but the user's own (`codync_mem::check`): no hidden characters, prompt-carrying
links, planted instructions, passwords, keys, ID or card numbers, nothing the user forgot, no duplicates, and
the cap. Entries the user wrote can't be changed by a bot or dreaming.
diff --git a/host/crates/memory/src/schema.rs b/host/crates/memory/src/schema.rs
index e7609f82..9b52efbe 100644
--- a/host/crates/memory/src/schema.rs
+++ b/host/crates/memory/src/schema.rs
@@ -69,6 +69,31 @@ const MIGRATIONS: &[&str] = &[
version INTEGER NOT NULL
);
CREATE INDEX memory_dream_files_dream ON memory_dream_files (dream);",
+ // 3: entry ids are never reused, so an old undo line, a dream's undo or a bot's stale id can't reach a newer
+ // entry. Ids the dream log still names count as used.
+ "CREATE TABLE memory_entries_new (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ bot TEXT,
+ state TEXT NOT NULL,
+ text TEXT NOT NULL,
+ author TEXT NOT NULL,
+ author_bot TEXT,
+ pinned INTEGER NOT NULL DEFAULT 0,
+ superseded_by INTEGER,
+ room TEXT,
+ evidence TEXT NOT NULL DEFAULT '',
+ created_at INTEGER NOT NULL,
+ updated_at INTEGER NOT NULL,
+ rev INTEGER NOT NULL
+ );
+ INSERT INTO memory_entries_new SELECT * FROM memory_entries;
+ DROP TABLE memory_entries;
+ ALTER TABLE memory_entries_new RENAME TO memory_entries;
+ CREATE INDEX memory_entries_bot ON memory_entries (bot, state);
+ DELETE FROM sqlite_sequence WHERE name = 'memory_entries';
+ INSERT INTO sqlite_sequence (name, seq) SELECT 'memory_entries', max(
+ coalesce((SELECT max(id) FROM memory_entries), 0),
+ coalesce((SELECT max(entry) FROM memory_dream_changes), 0));",
];
/// Brings the crate's tables up to date. Safe to call on every start.
@@ -104,4 +129,38 @@ mod tests {
conn.query_row("SELECT value FROM memory_meta WHERE key = 'schema'", [], |row| row.get(0)).unwrap();
assert_eq!(version, i64::try_from(MIGRATIONS.len()).unwrap());
}
+
+ fn insert(conn: &Connection) -> i64 {
+ conn.execute(
+ "INSERT INTO memory_entries (state, text, author, created_at, updated_at, rev)
+ VALUES ('active', 'x', 'user', 0, 0, 0)",
+ [],
+ )
+ .unwrap();
+ conn.last_insert_rowid()
+ }
+
+ #[test]
+ fn entry_ids_are_not_reused() {
+ let conn = Connection::open_in_memory().unwrap();
+ migrate(&conn).unwrap();
+ let first = insert(&conn);
+ conn.execute("DELETE FROM memory_entries WHERE id = ?1", [first]).unwrap();
+ assert!(insert(&conn) > first);
+ }
+
+ #[test]
+ fn upgrading_keeps_entries_and_skips_ids_the_dream_log_names() {
+ let conn = Connection::open_in_memory().unwrap();
+ conn.execute_batch("CREATE TABLE memory_meta (key TEXT PRIMARY KEY, value INTEGER NOT NULL)").unwrap();
+ conn.execute_batch(&MIGRATIONS[..2].concat()).unwrap();
+ conn.execute("INSERT INTO memory_meta (key, value) VALUES ('schema', 2)", []).unwrap();
+ let kept = insert(&conn);
+ conn.execute("INSERT INTO memory_dream_changes (dream, entry, change) VALUES (1, 7, 'added')", []).unwrap();
+ migrate(&conn).unwrap();
+ let text: String =
+ conn.query_row("SELECT text FROM memory_entries WHERE id = ?1", [kept], |row| row.get(0)).unwrap();
+ assert_eq!(text, "x");
+ assert_eq!(insert(&conn), 8);
+ }
}
diff --git a/host/crates/memory/src/store.rs b/host/crates/memory/src/store.rs
index bf6774b8..25ee3aee 100644
--- a/host/crates/memory/src/store.rs
+++ b/host/crates/memory/src/store.rs
@@ -142,6 +142,11 @@ pub fn cap(bot: Option<&str>) -> usize {
pub fn add(conn: &Connection, writer: Writer<'_>, new: New<'_>, now: i64, rev: i64) -> Result<Entry> {
admissible(conn, writer, new.bot, new.text)?;
+ if let Writer::Bot(bot) | Writer::Dream(bot) = writer
+ && spent(conn, bot, new.evidence)?
+ {
+ return Err(Refusal::Forgotten.into());
+ }
let text = new.text.trim();
conn.execute(
"INSERT INTO memory_entries (bot, state, text, author, author_bot, room, evidence, created_at, updated_at, rev)
@@ -244,14 +249,10 @@ pub fn forget(conn: &Connection, id: i64) -> Result<Entry> {
Ok(entry)
}
-/// Keeps `text` out of a layer (`None`: "yours") for bots and dreaming, as if it had been forgotten: the user
-/// declined it.
-pub fn forbid(conn: &Connection, bot: Option<&str>, text: &str) -> Result<()> {
- conn.execute(
- "INSERT OR IGNORE INTO memory_tombstones (scope, hash) VALUES (?1, ?2)",
- params![scope(bot), fingerprint(text)],
- )?;
- Ok(())
+/// Keeps `text`, and memory drawn from the `message` it came from, out of a layer (`None`: "yours") for bots and
+/// dreaming, as if it had been forgotten: the user declined it.
+pub fn forbid(conn: &Connection, bot: Option<&str>, text: &str, message: Option<&str>) -> Result<()> {
+ tombstone(conn, bot, std::iter::once(fingerprint(text)).chain(message.map(cited)))
}
/// Forgets every entry in one layer (`None`: "yours"). Returns what was deleted.
@@ -362,14 +363,46 @@ fn forgotten(conn: &Connection, bot: Option<&str>, text: &str) -> Result<bool> {
.is_some())
}
+/// Tombstones the entry's text and the messages it was learned from, so a reworded copy of it isn't learned again
+/// from the same words.
fn bury(conn: &Connection, entry: &Entry) -> Result<()> {
- conn.execute(
- "INSERT OR IGNORE INTO memory_tombstones (scope, hash) VALUES (?1, ?2)",
- params![scope(entry.bot.as_deref()), fingerprint(&entry.text)],
- )?;
+ let marks = std::iter::once(fingerprint(&entry.text)).chain(entry.evidence.iter().map(|m| cited(m)));
+ tombstone(conn, entry.bot.as_deref(), marks)
+}
+
+fn tombstone(conn: &Connection, bot: Option<&str>, marks: impl Iterator<Item = String>) -> Result<()> {
+ for mark in marks {
+ conn.execute(
+ "INSERT OR IGNORE INTO memory_tombstones (scope, hash) VALUES (?1, ?2)",
+ params![scope(bot), mark],
+ )?;
+ }
Ok(())
}
+/// A tombstone for a message rather than a text: no bot or dream may draw memory for the layer from it again.
+fn cited(message: &str) -> String {
+ format!("message:{message}")
+}
+
+/// Whether the user turned down memory from one of these messages in a layer `bot` uses: the message is spent for
+/// both, so a fact undone in one layer doesn't come back in the other.
+fn spent(conn: &Connection, bot: &str, evidence: &[String]) -> Result<bool> {
+ for message in evidence {
+ let found = conn
+ .query_row(
+ "SELECT 1 FROM memory_tombstones WHERE scope IN (?1, ?2) AND hash = ?3",
+ params![scope(None), scope(Some(bot)), cited(message)],
+ |_| Ok(()),
+ )
+ .optional()?;
+ if found.is_some() {
+ return Ok(true);
+ }
+ }
+ Ok(false)
+}
+
fn scope(bot: Option<&str>) -> String {
bot.map_or_else(|| "yours".to_owned(), |bot| format!("bot:{bot}"))
}
@@ -439,86 +472,4 @@ fn parse_author(name: &str) -> Author {
}
#[cfg(test)]
-mod tests {
- use super::*;
-
- fn db() -> Connection {
- let conn = Connection::open_in_memory().unwrap();
- crate::schema::migrate(&conn).unwrap();
- conn
- }
-
- fn new(bot: Option<&str>, text: &str) -> New<'static> {
- New { bot: bot.map(|b| &*b.to_owned().leak()), text: text.to_owned().leak(), room: None, evidence: &[] }
- }
-
- #[test]
- fn bots_cannot_change_what_the_user_wrote() {
- let conn = db();
- let mine = add(&conn, Writer::User, new(Some("a"), "Likes tea"), 1, 1).unwrap();
- assert!(matches!(revise(&conn, Writer::Bot("a"), mine.id, "Likes coffee", 2, 2), Err(Error::UsersOwn)));
- assert!(matches!(archive(&conn, Writer::Dream("a"), mine.id, None, 2, 2), Err(Error::UsersOwn)));
- let theirs = add(&conn, Writer::Bot("a"), new(Some("a"), "Works late"), 3, 3).unwrap();
- assert_eq!(theirs.author, Author::Bot);
- assert_eq!(theirs.author_bot.as_deref(), Some("a"));
- assert_eq!(revise(&conn, Writer::Bot("a"), theirs.id, "Works late on Fridays", 4, 4).unwrap().rev, 4);
- }
-
- #[test]
- fn forgotten_entries_never_come_back_from_a_bot() {
- let conn = db();
- let entry = add(&conn, Writer::Bot("a"), new(None, "Lives in Taipei"), 1, 1).unwrap();
- forget(&conn, entry.id).unwrap();
- let again = add(&conn, Writer::Bot("b"), new(None, "lives in taipei."), 2, 2);
- assert!(matches!(again, Err(Error::Refused(Refusal::Forgotten))));
- // The tombstone is per layer, and the user may always write it again.
- add(&conn, Writer::Bot("a"), new(Some("a"), "Lives in Taipei"), 3, 3).unwrap();
- add(&conn, Writer::User, new(None, "Lives in Taipei"), 4, 4).unwrap();
- }
-
- #[test]
- fn bot_writes_stop_at_the_cap_but_the_users_do_not() {
- let conn = db();
- let fact = "字".repeat(100);
- for i in 0..3 {
- add(&conn, Writer::Bot("a"), new(None, &format!("{fact}{i}")), i, i).unwrap();
- }
- assert_eq!(used(&conn, None).unwrap(), 303);
- let over = add(&conn, Writer::Bot("a"), new(None, &format!("{fact}x")), 9, 9);
- assert!(matches!(over, Err(Error::Refused(Refusal::Full))));
- add(&conn, Writer::User, new(None, &format!("{fact}y")), 10, 10).unwrap();
- }
-
- #[test]
- fn searches_both_layers_case_insensitively() {
- let conn = db();
- add(&conn, Writer::User, new(None, "Allergic to Peanuts"), 1, 1).unwrap();
- let old = add(&conn, Writer::User, new(Some("a"), "Uses peanut butter in recipes"), 2, 2).unwrap();
- archive(&conn, Writer::User, old.id, None, 3, 3).unwrap();
- add(&conn, Writer::User, new(Some("b"), "Peanut farm visit"), 4, 4).unwrap();
- let found: Vec<String> = search(&conn, "a", "PEANUT", 10).unwrap().into_iter().map(|e| e.text).collect();
- assert_eq!(found, ["Allergic to Peanuts", "Uses peanut butter in recipes"]);
- assert_eq!(search(&conn, "a", "100%", 10).unwrap(), [] as [Entry; 0]);
- }
-
- #[test]
- fn a_bot_cant_write_what_the_layer_already_holds() {
- let conn = db();
- add(&conn, Writer::User, new(Some("a"), "Likes tea"), 1, 1).unwrap();
- let again = add(&conn, Writer::Bot("a"), new(Some("a"), "likes TEA."), 2, 2);
- assert!(matches!(again, Err(Error::Refused(Refusal::Duplicate))));
- assert!(admissible(&conn, Writer::Bot("a"), Some("a"), "Likes coffee").is_ok());
- assert!(admissible(&conn, Writer::Bot("a"), Some("a"), "my password is 1234").is_err());
- }
-
- #[test]
- fn forgetting_a_layer_leaves_the_others() {
- let conn = db();
- add(&conn, Writer::User, new(None, "Shared fact"), 1, 1).unwrap();
- add(&conn, Writer::User, new(Some("a"), "Bot fact"), 2, 2).unwrap();
- assert_eq!(forget_layer(&conn, Some("a")).unwrap().len(), 1);
- assert_eq!(layer(&conn, None).unwrap().len(), 1);
- assert_eq!(layer(&conn, Some("a")).unwrap(), [] as [Entry; 0]);
- assert_eq!(changed_since(&conn, 0).unwrap().len(), 1);
- }
-}
+mod tests;
diff --git a/host/crates/memory/src/store/tests.rs b/host/crates/memory/src/store/tests.rs
new file mode 100644
index 00000000..a44ae273
--- /dev/null
+++ b/host/crates/memory/src/store/tests.rs
@@ -0,0 +1,97 @@
+use super::*;
+
+fn db() -> Connection {
+ let conn = Connection::open_in_memory().unwrap();
+ crate::schema::migrate(&conn).unwrap();
+ conn
+}
+
+fn new(bot: Option<&str>, text: &str) -> New<'static> {
+ New { bot: bot.map(|b| &*b.to_owned().leak()), text: text.to_owned().leak(), room: None, evidence: &[] }
+}
+
+#[test]
+fn bots_cannot_change_what_the_user_wrote() {
+ let conn = db();
+ let mine = add(&conn, Writer::User, new(Some("a"), "Likes tea"), 1, 1).unwrap();
+ assert!(matches!(revise(&conn, Writer::Bot("a"), mine.id, "Likes coffee", 2, 2), Err(Error::UsersOwn)));
+ assert!(matches!(archive(&conn, Writer::Dream("a"), mine.id, None, 2, 2), Err(Error::UsersOwn)));
+ let theirs = add(&conn, Writer::Bot("a"), new(Some("a"), "Works late"), 3, 3).unwrap();
+ assert_eq!(theirs.author, Author::Bot);
+ assert_eq!(theirs.author_bot.as_deref(), Some("a"));
+ assert_eq!(revise(&conn, Writer::Bot("a"), theirs.id, "Works late on Fridays", 4, 4).unwrap().rev, 4);
+}
+
+#[test]
+fn forgotten_entries_never_come_back_from_a_bot() {
+ let conn = db();
+ let entry = add(&conn, Writer::Bot("a"), new(None, "Lives in Taipei"), 1, 1).unwrap();
+ forget(&conn, entry.id).unwrap();
+ let again = add(&conn, Writer::Bot("b"), new(None, "lives in taipei."), 2, 2);
+ assert!(matches!(again, Err(Error::Refused(Refusal::Forgotten))));
+ // The tombstone is per layer, and the user may always write it again.
+ add(&conn, Writer::Bot("a"), new(Some("a"), "Lives in Taipei"), 3, 3).unwrap();
+ add(&conn, Writer::User, new(None, "Lives in Taipei"), 4, 4).unwrap();
+}
+
+#[test]
+fn bot_writes_stop_at_the_cap_but_the_users_do_not() {
+ let conn = db();
+ let fact = "字".repeat(100);
+ for i in 0..3 {
+ add(&conn, Writer::Bot("a"), new(None, &format!("{fact}{i}")), i, i).unwrap();
+ }
+ assert_eq!(used(&conn, None).unwrap(), 303);
+ let over = add(&conn, Writer::Bot("a"), new(None, &format!("{fact}x")), 9, 9);
+ assert!(matches!(over, Err(Error::Refused(Refusal::Full))));
+ add(&conn, Writer::User, new(None, &format!("{fact}y")), 10, 10).unwrap();
+}
+
+#[test]
+fn searches_both_layers_case_insensitively() {
+ let conn = db();
+ add(&conn, Writer::User, new(None, "Allergic to Peanuts"), 1, 1).unwrap();
+ let old = add(&conn, Writer::User, new(Some("a"), "Uses peanut butter in recipes"), 2, 2).unwrap();
+ archive(&conn, Writer::User, old.id, None, 3, 3).unwrap();
+ add(&conn, Writer::User, new(Some("b"), "Peanut farm visit"), 4, 4).unwrap();
+ let found: Vec<String> = search(&conn, "a", "PEANUT", 10).unwrap().into_iter().map(|e| e.text).collect();
+ assert_eq!(found, ["Allergic to Peanuts", "Uses peanut butter in recipes"]);
+ assert_eq!(search(&conn, "a", "100%", 10).unwrap(), [] as [Entry; 0]);
+}
+
+#[test]
+fn a_bot_cant_write_what_the_layer_already_holds() {
+ let conn = db();
+ add(&conn, Writer::User, new(Some("a"), "Likes tea"), 1, 1).unwrap();
+ let again = add(&conn, Writer::Bot("a"), new(Some("a"), "likes TEA."), 2, 2);
+ assert!(matches!(again, Err(Error::Refused(Refusal::Duplicate))));
+ assert!(admissible(&conn, Writer::Bot("a"), Some("a"), "Likes coffee").is_ok());
+ assert!(admissible(&conn, Writer::Bot("a"), Some("a"), "my password is 1234").is_err());
+}
+
+#[test]
+fn forgetting_a_layer_leaves_the_others() {
+ let conn = db();
+ add(&conn, Writer::User, new(None, "Shared fact"), 1, 1).unwrap();
+ add(&conn, Writer::User, new(Some("a"), "Bot fact"), 2, 2).unwrap();
+ assert_eq!(forget_layer(&conn, Some("a")).unwrap().len(), 1);
+ assert_eq!(layer(&conn, None).unwrap().len(), 1);
+ assert_eq!(layer(&conn, Some("a")).unwrap(), [] as [Entry; 0]);
+ assert_eq!(changed_since(&conn, 0).unwrap().len(), 1);
+}
+
+#[test]
+fn a_forgotten_memory_isnt_learned_again_from_the_same_message() {
+ let conn = db();
+ let from = |text: &'static str, message: &str| New { evidence: vec![message.to_owned()].leak(), ..new(None, text) };
+ let mine = |text: &'static str, message: &str| New { bot: Some("a"), ..from(text, message) };
+ let entry = add(&conn, Writer::Bot("a"), mine("我養了一隻柴犬叫麻糬", "m1"), 1, 1).unwrap();
+ forget(&conn, entry.id).unwrap();
+ let reworded = add(&conn, Writer::Dream("a"), from("養了一隻柴犬叫麻糬", "m1"), 2, 2);
+ assert!(matches!(reworded, Err(Error::Refused(Refusal::Forgotten))), "nor in the other layer");
+ // A later message may still say it, and the user may always write it.
+ add(&conn, Writer::Dream("a"), from("養了一隻柴犬叫麻糬", "m2"), 3, 3).unwrap();
+ forbid(&conn, None, "Works late", Some("m3")).unwrap();
+ assert!(matches!(add(&conn, Writer::Bot("a"), from("Likes tea", "m3"), 4, 4), Err(Error::Refused(_))));
+ add(&conn, Writer::User, from("Likes tea", "m3"), 5, 5).unwrap();
+}
diff --git a/host/src/chat/memory/notices.rs b/host/src/chat/memory/notices.rs
index c6a3b49d..91dd052f 100644
--- a/host/src/chat/memory/notices.rs
+++ b/host/src/chat/memory/notices.rs
@@ -123,7 +123,7 @@ pub fn answer(hub: &Hub, notice_id: &str, accept: bool) -> Result<Value> {
card.status = Status::Declined;
if let Action::Remember { text, shared } = &action {
let layer = (!(*shared && super::shared(&cfg))).then_some(cfg.id.as_str());
- hub.store.memory_write(|c, _, _| mem::forbid(c, layer, text))?;
+ hub.store.memory_write(|c, _, _| mem::forbid(c, layer, text, card.message.as_deref()))?;
}
}
Ok(update(hub, &notice, &cfg, &action, &card))