feat: bot reset with hard confirm and event-driven purge

This commit is contained in:
Joseph Doherty
2026-04-26 14:07:56 -04:00
parent 46062973c2
commit 82be8b3f51
5 changed files with 279 additions and 1 deletions
+40
View File
@@ -31,6 +31,46 @@ def _apply_you_authored(conn: Connection, e: Event) -> None:
)
@on("bot_reset")
def _apply_bot_reset(conn: Connection, e: Event) -> None:
"""Purge per-bot runtime state while preserving the bot's identity row.
Wipes chats hosted by this bot (with cascading chat-scoped tables),
memories owned by this bot, edges involving this bot, and the bot's own
activity row. The ``bots`` row itself is preserved so identity,
initial-relationship, and kickoff prose remain authored.
"""
bot_id = e.payload["bot_id"]
chat_ids = [
row[0]
for row in conn.execute(
"SELECT id FROM chats WHERE host_bot_id = ?", (bot_id,)
).fetchall()
]
for chat_id in chat_ids:
conn.execute("DELETE FROM scenes WHERE chat_id = ?", (chat_id,))
conn.execute("DELETE FROM containers WHERE chat_id = ?", (chat_id,))
conn.execute("DELETE FROM chat_state WHERE chat_id = ?", (chat_id,))
conn.execute("DELETE FROM chats WHERE id = ?", (chat_id,))
# Activity for this bot's entity row (independent of chat_id since the
# ``activity`` table is keyed on entity_id).
conn.execute("DELETE FROM activity WHERE entity_id = ?", (bot_id,))
# Memories authored by this bot.
conn.execute("DELETE FROM memories WHERE owner_id = ?", (bot_id,))
# Edges in either direction involving this bot.
conn.execute(
"DELETE FROM edges WHERE source_id = ? OR target_id = ?",
(bot_id, bot_id),
)
# NOTE: bots row itself is preserved (identity, kickoff_prose intact).
# NOTE: "you" activity (entity_id="you") may linger from a deleted chat;
# acceptable for v1 — Phase 1.5 cleanup if needed.
def get_bot(conn: Connection, bot_id: str) -> dict | None:
row = conn.execute("SELECT * FROM bots WHERE id = ?", (bot_id,)).fetchone()
if not row: