Agent Zero AI framework
Fix notifications skipped during state snapshots
Alessandro5c1d50f92888
diff --git a/helpers/notification.py b/helpers/notification.pyindex e76de42e5..d1f4445b7 100644--- a/helpers/notification.py+++ b/helpers/notification.py@@ -156,21 +156,24 @@ class NotificationManager:return [n for n in self.notifications if n.timestamp >= cutoff]def output(self, start: int | None = None, end: int | None = None) -> list[dict]:+ return self.output_with_state(start, end)[0]++ def output_with_state(+ self, start: int | None = None, end: int | None = None+ ) -> tuple[list[dict], str, int]:with self._lock:if start is None:start = 0if end is None:end = len(self.updates)updates = self.updates[start:end]- notifications = list(self.notifications)-- out = []- seen = set()- for update in updates:- if update not in seen and update < len(notifications):- out.append(notifications[update].output())- seen.add(update)- return out+ out = []+ seen = set()+ for update in updates:+ if update not in seen and update < len(self.notifications):+ out.append(self.notifications[update].output())+ seen.add(update)+ return out, self.guid, len(self.updates)def output_all(self) -> list[dict]:with self._lock:diff --git a/helpers/notification.py.dox.md b/helpers/notification.py.dox.mdindex d8f198f4a..e72aadbfb 100644--- a/helpers/notification.py.dox.md+++ b/helpers/notification.py.dox.md@@ -21,6 +21,7 @@- `add_notification(self, type: NotificationType, priority: NotificationPriority, message: str, title: str=..., detail: str=..., display_time: int=..., group: str=..., id: str=...) -> NotificationItem`- `get_recent_notifications(self, seconds: int=...) -> list[NotificationItem]`- `output(self, start: int | None=..., end: int | None=...) -> list[dict]`+ - `output_with_state(self, start: int | None=..., end: int | None=...) -> tuple[list[dict], str, int]`- `output_all(self) -> list[dict]`- `mark_read_by_ids(self, notification_ids: list[str]) -> int`- `update_item(self, no: int, **kwargs) -> None`@@ -30,6 +31,7 @@- Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.- Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.+- Notification payloads, GUIDs, and update cursors are captured under one lock so WebUI snapshots cannot skip notifications created during snapshot assembly.- Observed side-effect areas: filesystem deletion, settings/state persistence.- Imported dependency areas include: `dataclasses`, `datetime`, `enum`, `helpers.localization`, `threading`, `uuid`.diff --git a/helpers/state_snapshot.py b/helpers/state_snapshot.pyindex 9a1db075d..b26c010a1 100644--- a/helpers/state_snapshot.py+++ b/helpers/state_snapshot.py@@ -282,7 +282,9 @@ async def build_snapshot_from_request(*, request: StateRequestV1) -> SnapshotV1:log_end = 0notification_manager = AgentContext.get_notification_manager()- notifications = notification_manager.output(start=notifications_from_no)+ notifications, notifications_guid, notifications_version = (+ notification_manager.output_with_state(start=notifications_from_no)+ )scheduler = TaskScheduler.get()@@ -352,8 +354,8 @@ async def build_snapshot_from_request(*, request: StateRequestV1) -> SnapshotV1:"log_progress_active": bool(active_context.log.progress_active) if active_context else False,"paused": active_context.paused if active_context else False,"notifications": notifications,- "notifications_guid": notification_manager.guid,- "notifications_version": len(notification_manager.updates),+ "notifications_guid": notifications_guid,+ "notifications_version": notifications_version,}validate_snapshot_schema_v1(snapshot)diff --git a/helpers/state_snapshot.py.dox.md b/helpers/state_snapshot.py.dox.mdindex 69fccccf2..ed72f803c 100644--- a/helpers/state_snapshot.py.dox.md+++ b/helpers/state_snapshot.py.dox.md@@ -41,6 +41,7 @@- Important called helpers/classes observed in the source: `dataclass`, `_build_schema_from_typeddict`, `get_origin`, `timezone.strip`, `StateRequestV1`, `localization.get_timezone`, `localization.set_timezone`, `ctxid.strip`, `_coerce_non_negative_int`, `AgentContext.get_notification_manager`, `notification_manager.output`, `_get_agent_profile_labels`, `ctxs.sort`, `tasks.sort`, `validate_snapshot_schema_v1`, `_coerce_state_request_inputs`, `super.__init__`, `get_args`, `_annotation_to_isinstance_types`, `TypeError`.- Snapshot building prunes non-running in-memory contexts that were previously saved but no longer have a `chat.json`, preventing stale sidebar rows after chat files are deleted outside `/chat_remove`.+- Notification payloads use the manager's matching GUID and cursor from the same atomic read, preventing a concurrent notification from being skipped by the WebUI.- Keep request/response, tool, or helper semantics documented here at the same time as source changes.## Work Guidancediff --git a/tests/test_snapshot_schema_v1.py b/tests/test_snapshot_schema_v1.pyindex f520019d6..a72496f7b 100644--- a/tests/test_snapshot_schema_v1.py+++ b/tests/test_snapshot_schema_v1.py@@ -108,3 +108,17 @@ def test_snapshot_schema_rejects_unexpected_top_level_keys():with pytest.raises(ValueError):snapshot.validate_snapshot_schema_v1(payload)+++def test_notification_payload_and_cursor_are_captured_together():+ from helpers.notification import NotificationManager, NotificationPriority, NotificationType++ manager = NotificationManager()+ manager.add_notification(NotificationType.INFO, NotificationPriority.HIGH, "first")++ notifications, _, version = manager.output_with_state()+ manager.add_notification(NotificationType.INFO, NotificationPriority.HIGH, "second")++ assert [item["message"] for item in notifications] == ["first"]+ assert version == 1+ assert [item["message"] for item in manager.output(start=version)] == ["second"]