Skip to content

Agent Zero AI framework

feat(connector): page tail history snapshots

Alessandro1831c7b47e6d
diff --git a/plugins/_a0_connector/api/ws_connector.py b/plugins/_a0_connector/api/ws_connector.py
index 526da9c7d..148ef5ef1 100644
--- a/plugins/_a0_connector/api/ws_connector.py
+++ b/plugins/_a0_connector/api/ws_connector.py
@@ -9,7 +9,10 @@ from helpers.ws import WsHandler
from helpers.ws_manager import WsResult
from plugins._a0_connector.helpers.exec_config import build_exec_config
-from plugins._a0_connector.helpers.event_bridge import get_context_log_entries
+from plugins._a0_connector.helpers.event_bridge import (
+ get_context_log_entries,
+ get_context_log_entry_count,
+)
from plugins._a0_connector.helpers.version import agent_zero_version
from plugins._a0_connector.helpers.ws_runtime import (
clear_remote_tree_snapshot,
@@ -68,6 +71,7 @@ WS_FEATURES = [
]
_SNAPSHOT_REPLAY_PAGE_SIZE = 50
+_TAIL_HISTORY_PAGE_SIZE = 100
_LIVE_STREAM_PAGE_SIZE = 100
@@ -257,6 +261,8 @@ class WsConnector(WsHandler):
context_id = str(data.get("context_id", "")).strip()
from_sequence = int(data.get("from", 0) or 0)
+ history_mode = str(data.get("history", "")).strip().lower()
+ history_before = data.get("history_before")
if not context_id:
return WsResult.error(
@@ -274,6 +280,67 @@ class WsConnector(WsHandler):
)
subscribe_sid_to_context(sid, context_id)
+
+ if history_before is not None:
+ before = min(
+ max(int(history_before or 0), 0),
+ get_context_log_entry_count(context_id),
+ )
+ start = max(before - _TAIL_HISTORY_PAGE_SIZE, 0)
+ events, last_sequence = get_context_log_entries(
+ context_id,
+ after=start,
+ limit=before - start,
+ )
+ await self._emit_context_snapshot(
+ sid,
+ context_id=context_id,
+ events=events,
+ last_sequence=last_sequence,
+ context=context,
+ correlation_id=data.get("correlationId"),
+ history_before=start,
+ has_more_history=bool(start),
+ )
+ return {
+ "context_id": context_id,
+ "subscribed": True,
+ "last_sequence": last_sequence,
+ "history_before": start,
+ "has_more_history": bool(start),
+ }
+
+ if history_mode == "tail":
+ total = get_context_log_entry_count(context_id)
+ start = max(total - _TAIL_HISTORY_PAGE_SIZE, 0)
+ events, last_sequence = get_context_log_entries(
+ context_id,
+ after=start,
+ limit=_TAIL_HISTORY_PAGE_SIZE,
+ )
+ await self._emit_context_snapshot(
+ sid,
+ context_id=context_id,
+ events=events,
+ last_sequence=last_sequence,
+ context=context,
+ correlation_id=data.get("correlationId"),
+ history_before=start,
+ has_more_history=bool(start),
+ )
+ self._start_streaming(
+ sid,
+ context_id,
+ from_sequence=last_sequence,
+ )
+ return {
+ "context_id": context_id,
+ "subscribed": True,
+ "last_sequence": last_sequence,
+ "history_before": start,
+ "has_more_history": bool(start),
+ }
+
events, last_sequence = get_context_log_entries(
context_id,
after=from_sequence,
@@ -927,16 +994,23 @@ class WsConnector(WsHandler):
last_sequence: int,
context: AgentContext | None = None,
correlation_id: str | None = None,
+ history_before: int | None = None,
+ has_more_history: bool | None = None,
) -> None:
+ payload: dict[str, Any] = {
+ "context_id": context_id,
+ "events": events,
+ "last_sequence": last_sequence,
+ "message_queue": self._queue_items_for_context(context),
+ }
+ if history_before is not None:
+ payload["history_before"] = history_before
+ if has_more_history is not None:
+ payload["has_more_history"] = has_more_history
await self.emit_to(
sid,
"connector_context_snapshot",
- {
- "context_id": context_id,
- "events": events,
- "last_sequence": last_sequence,
- "message_queue": self._queue_items_for_context(context),
- },
+ payload,
correlation_id=correlation_id,
)
diff --git a/plugins/_a0_connector/helpers/event_bridge.py b/plugins/_a0_connector/helpers/event_bridge.py
index c453a6e78..e4413f282 100644
--- a/plugins/_a0_connector/helpers/event_bridge.py
+++ b/plugins/_a0_connector/helpers/event_bridge.py
@@ -116,6 +116,32 @@ def get_context_log_entries(
return [], max(int(after or 0), 0)
+def get_context_log_entry_count(context_id: str) -> int:
+ """Return the current log-output cursor for a context."""
+ try:
+ from agent import AgentContext
+
+ context = AgentContext.get(context_id)
+ if context is None:
+ return 0
+
+ log = context.log
+ log_lock = getattr(log, "_lock", None)
+ updates = getattr(log, "updates", None)
+ if isinstance(updates, list):
+ if log_lock is not None:
+ with log_lock:
+ return len(updates)
+ return len(updates)
+
+ return int(log.output().end)
+ except Exception as exc:
+ PrintStyle.error(
+ f"[a0-connector] event_bridge cursor error for context {context_id}: {exc}"
+ )
+ return 0
+
+
async def stream_context_events(
context_id: str,
from_sequence: int = 0,