Skip to content

Agent Zero AI framework

Avoid rewriting unrelated chats on project changes

Alessandrodb4913ff765c
diff --git a/helpers/projects.py b/helpers/projects.py
index baa14f91c..11f407952 100644
--- a/helpers/projects.py
+++ b/helpers/projects.py
@@ -493,7 +493,6 @@ def reactivate_project_in_chats(name: str):
for context in AgentContext.all():
if context.get_data(CONTEXT_DATA_KEY_PROJECT) == name:
activate_project(context.id, name, mark_dirty=False)
- persist_chat.save_tmp_chat(context)
from helpers.state_monitor_integration import mark_dirty_all
mark_dirty_all(reason="projects.reactivate_project_in_chats")
@@ -505,7 +504,6 @@ def deactivate_project_in_chats(name: str):
for context in AgentContext.all():
if context.get_data(CONTEXT_DATA_KEY_PROJECT) == name:
deactivate_project(context.id, mark_dirty=False)
- persist_chat.save_tmp_chat(context)
from helpers.state_monitor_integration import mark_dirty_all
mark_dirty_all(reason="projects.deactivate_project_in_chats")
diff --git a/helpers/projects.py.dox.md b/helpers/projects.py.dox.md
index 8c7a779df..4de0b9338 100644
--- a/helpers/projects.py.dox.md
+++ b/helpers/projects.py.dox.md
@@ -91,6 +91,8 @@
scope; only chats whose active profile actually changes are persisted and
marked dirty. Context creation uses the same reconciliation after resolving
its scope, so a disabled configured profile cannot become invisibly active.
+- Project updates and deletion refresh only chats assigned to that project and
+ persist each affected chat once; unrelated chats are never rewritten.
- Observed side-effect areas: filesystem reads, filesystem writes, filesystem deletion, plugin state, settings/state persistence, secret handling.
- Imported dependency areas include: `helpers`, `helpers.print_style`, `os`,
`typing`.
diff --git a/tests/test_projects.py b/tests/test_projects.py
index 8b45eeb2b..ae334fa95 100644
--- a/tests/test_projects.py
+++ b/tests/test_projects.py
@@ -299,6 +299,40 @@ def test_bulk_profile_reconciliation_persists_only_changed_chats(
assert catalog_lookups == ([None, "demo"] if all_scopes else [None])
+def test_project_refresh_touches_only_matching_chats(monkeypatch) -> None:
+ contexts = [
+ SimpleNamespace(id="matching", get_data=lambda _key: "demo"),
+ SimpleNamespace(id="unrelated", get_data=lambda _key: "other"),
+ ]
+ calls: list[tuple] = []
+ monkeypatch.setattr(
+ AgentContext, "all", staticmethod(lambda: contexts)
+ )
+ monkeypatch.setattr(
+ projects,
+ "activate_project",
+ lambda context_id, name, *, mark_dirty: calls.append(
+ ("activate", context_id, name, mark_dirty)
+ ),
+ )
+ monkeypatch.setattr(
+ projects,
+ "deactivate_project",
+ lambda context_id, *, mark_dirty: calls.append(
+ ("deactivate", context_id, mark_dirty)
+ ),
+ )
+ monkeypatch.setattr(state_monitor_integration, "mark_dirty_all", lambda **_kwargs: None)
+
+ projects.reactivate_project_in_chats("demo")
+ projects.deactivate_project_in_chats("demo")
+
+ assert calls == [
+ ("activate", "matching", "demo", False),
+ ("deactivate", "matching", False),
+ ]
+
+
def test_project_include_agents_md_defaults_true_and_saves(monkeypatch, tmp_path):
_prepare_project_tree(monkeypatch, tmp_path)
meta = tmp_path / "usr" / "projects" / "demo" / ".a0proj"