Skip to content

Agent Zero AI framework

Guard chat deletion against empty context IDs

Alessandro1b4505d8e59f
diff --git a/helpers/persist_chat.py b/helpers/persist_chat.py
index 47479bd35..0f4be317e 100644
--- a/helpers/persist_chat.py
+++ b/helpers/persist_chat.py
@@ -161,6 +161,8 @@ def export_json_chat(context: AgentContext):
def remove_chat(ctxid):
"""Remove a chat or task context"""
+ if not isinstance(ctxid, str) or not ctxid.strip():
+ raise ValueError("remove_chat: context id must not be empty")
_delete_provider_responses_for_chat(ctxid)
path = get_chat_folder_path(ctxid)
files.delete_dir(path)
@@ -168,6 +170,8 @@ def remove_chat(ctxid):
def remove_msg_files(ctxid):
"""Remove all message files for a chat or task context"""
+ if not isinstance(ctxid, str) or not ctxid.strip():
+ raise ValueError("remove_msg_files: context id must not be empty")
path = get_chat_msg_files_folder(ctxid)
files.delete_dir(path)
diff --git a/helpers/persist_chat.py.dox.md b/helpers/persist_chat.py.dox.md
index 09205e7d0..54be55d1a 100644
--- a/helpers/persist_chat.py.dox.md
+++ b/helpers/persist_chat.py.dox.md
@@ -47,6 +47,7 @@
- Chat loading skips directories that do not contain `chat.json`; malformed existing chat files still report load errors.
- Chat saves write and fsync a same-directory temporary file, atomically replace `chat.json`, and fsync the directory so an interrupted save cannot truncate the previous chat.
- Contexts are marked with private `SAVED_CHAT_CONTEXT_DATA_KEY` only after a successful save or load from disk so snapshot code can detect deleted chat files without hiding fresh unsaved chats.
+- Chat and message-file removal reject empty or whitespace-only context IDs before provider or filesystem deletion begins.
## Key Concepts
diff --git a/tests/test_persist_chat_deletion_guards.py b/tests/test_persist_chat_deletion_guards.py
new file mode 100644
index 000000000..ce456d50c
--- /dev/null
+++ b/tests/test_persist_chat_deletion_guards.py
@@ -0,0 +1,20 @@
+from __future__ import annotations
+
+import sys
+from pathlib import Path
+
+import pytest
+
+PROJECT_ROOT = Path(__file__).resolve().parents[1]
+if str(PROJECT_ROOT) not in sys.path:
+ sys.path.insert(0, str(PROJECT_ROOT))
+
+from helpers import persist_chat
+
+
[email protected]("ctxid", [None, "", " "])
+def test_chat_deletion_rejects_empty_context_ids(ctxid) -> None:
+ with pytest.raises(ValueError, match="context id must not be empty"):
+ persist_chat.remove_chat(ctxid)
+ with pytest.raises(ValueError, match="context id must not be empty"):
+ persist_chat.remove_msg_files(ctxid)