Skip to content

Self-improving AI Employees for everyone

feat(cli_agent): /compact native event forwarded to CompactionService (Step 8)

trohitg8c9927021771
diff --git a/server/services/cli_agent/session.py b/server/services/cli_agent/session.py
index a0f9437b..1c666173 100644
--- a/server/services/cli_agent/session.py
+++ b/server/services/cli_agent/session.py
@@ -501,17 +501,71 @@ class AICliSession(BaseProcessSupervisor):
)
async def _on_jsonl_event(self, event: Dict[str, Any]) -> None:
- """JsonlWatcher callback: append + dispatch + result-signal.
+ """JsonlWatcher callback: append + dispatch + result-signal +
+ compaction-record.
Wraps the existing :meth:`_on_event` so the events list is
- updated centrally and the result-event Asyncio primitive fires
- when claude writes a `result` line — that's the signal that the
- turn is done.
+ updated centrally, the result-event Asyncio primitive fires
+ when claude writes a `result` line, and native compactions
+ (``system/compact_boundary``) flow into the shared
+ :class:`CompactionService` so the local-threshold path doesn't
+ also fire.
"""
self._events.append(event)
await self._on_event(event)
if self._provider.is_final_event(event):
self._result_event.set()
+ if (
+ event.get("type") == "system"
+ and event.get("subtype") == "compact_boundary"
+ ):
+ await self._record_native_compaction(event)
+
+ async def _record_native_compaction(self, event: Dict[str, Any]) -> None:
+ """Forward a native ``/compact`` event into ``CompactionService``.
+
+ Avoids double-compaction: without this, the local-threshold
+ path in ``services/ai.py:_track_token_usage`` and claude's own
+ auto-compaction would both trigger and the UI's token gauge
+ would race against claude's internal state.
+ """
+ try:
+ from services.compaction import get_compaction_service
+ except Exception: # pragma: no cover — defensive
+ return
+ svc = get_compaction_service()
+ if svc is None:
+ return
+
+ metadata = event.get("compact_metadata") or {}
+ pre_tokens = int(metadata.get("pre_tokens") or 0)
+ # Session-id for the compaction store: prefer the memory bridge
+ # session_id (stable across `--resume`), fall back to the
+ # current task's resume_session_id or the node id.
+ session_id = (
+ getattr(self._task, "resume_session_id", None)
+ or getattr(self._task, "session_id", None)
+ or self._node_id
+ )
+ model = getattr(self._task, "model", "") or ""
+ try:
+ await svc.record(
+ session_id=session_id,
+ node_id=self._node_id,
+ provider=self._provider.name,
+ model=model,
+ tokens_before=pre_tokens,
+ tokens_after=0,
+ summary=None,
+ )
+ self._logger.info(
+ "[%s] native compaction recorded pre_tokens=%d trigger=%s",
+ self.label, pre_tokens, metadata.get("trigger", "unknown"),
+ )
+ except Exception as exc: # pragma: no cover — best-effort
+ self._logger.debug(
+ "[%s] compaction.record failed: %s", self.label, exc,
+ )
# ------------------------------------------------------------------
# Event dispatch (UI broadcasts)
diff --git a/server/services/cli_agent/session_pool.py b/server/services/cli_agent/session_pool.py
index ecab617a..35847a4d 100644
--- a/server/services/cli_agent/session_pool.py
+++ b/server/services/cli_agent/session_pool.py
@@ -466,22 +466,103 @@ class ClaudeSessionPool:
session.new_jsonl_path = path
session.new_jsonl_event.set()
- async def _on_event(event: Dict[str, Any]) -> None:
- session.events_this_turn.append(event)
- if self._provider.is_final_event(event):
- session.result_event.set()
+ def _make_event_handler(s: PooledClaudeSession):
+ async def _on_event(event: Dict[str, Any]) -> None:
+ await self._handle_jsonl_event(s, event)
+ return _on_event
session.dir_watcher = JsonlDirWatcher(
project_dir, on_new_file=_on_new_jsonl,
)
await session.dir_watcher.start()
session.jsonl_watcher = JsonlWatcher(
- jsonl_path, on_event=_on_event, start_from_end=False,
+ jsonl_path,
+ on_event=_make_event_handler(session),
+ start_from_end=False,
)
await session.jsonl_watcher.start()
return session
+ async def _handle_jsonl_event(
+ self, session: PooledClaudeSession, event: Dict[str, Any],
+ ) -> None:
+ """Shared JSONL event dispatcher for both spawn-time and
+ post-``/clear`` watchers.
+
+ Three concerns:
+ 1. Append to ``session.events_this_turn`` so :meth:`send_turn`
+ can build a :class:`SessionResult` from the turn's events.
+ 2. Set ``session.result_event`` when the provider's final
+ event arrives (``type == "result"``).
+ 3. Detect native compaction (``type=="system"``,
+ ``subtype=="compact_boundary"``) and forward to
+ :class:`CompactionService` so the existing token-tracking
+ pipeline accounts for it — avoids double-compaction
+ (client-side summarisation + native auto-compaction
+ running independently).
+ """
+ session.events_this_turn.append(event)
+ if self._provider.is_final_event(event):
+ session.result_event.set()
+
+ if (
+ event.get("type") == "system"
+ and event.get("subtype") == "compact_boundary"
+ ):
+ await self._record_native_compaction(session, event)
+
+ @staticmethod
+ async def _record_native_compaction(
+ session: PooledClaudeSession, event: Dict[str, Any],
+ ) -> None:
+ """Forward a ``system/compact_boundary`` event to the existing
+ :class:`CompactionService` so its token-state machine reflects
+ the native compaction that just happened.
+
+ Without this, the local-threshold path in
+ ``services/ai.py:_track_token_usage`` and claude's own
+ auto-compaction would both trigger summarisations and the UI's
+ token gauge would race against claude's internal state.
+ """
+ try:
+ from services.compaction import get_compaction_service
+ except Exception: # pragma: no cover — defensive
+ return
+
+ svc = get_compaction_service()
+ if svc is None: # not initialised in test envs
+ return
+
+ metadata = event.get("compact_metadata") or {}
+ pre_tokens = int(metadata.get("pre_tokens") or 0)
+ # `tokens_after` after native compaction isn't directly visible
+ # in the JSONL — claude doesn't emit a "post-summary token
+ # count" event. The next `result` event's `usage` block will
+ # carry the true post-compaction cumulative; recording 0 here
+ # is a conservative reset that lets the next turn's `track()`
+ # call set the accurate cumulative.
+ try:
+ await svc.record(
+ session_id=session.memory_node_id,
+ node_id=session.memory_node_id,
+ provider="claude",
+ model="", # claude doesn't emit the model on this event
+ tokens_before=pre_tokens,
+ tokens_after=0,
+ summary=None,
+ )
+ logger.info(
+ "[ClaudeSessionPool] native compaction recorded "
+ "memory_node=%s pre_tokens=%d trigger=%s",
+ session.memory_node_id, pre_tokens,
+ metadata.get("trigger", "unknown"),
+ )
+ except Exception as exc: # pragma: no cover — best-effort
+ logger.debug(
+ "[ClaudeSessionPool] compaction.record failed: %s", exc,
+ )
+
async def _clear_and_swap(self, session: PooledClaudeSession) -> None:
"""Send ``/clear``, await the new JSONL filename via the dir
watcher, swap the file watcher to the new path. Holds the
@@ -539,10 +620,10 @@ class ClaudeSessionPool:
# Reattach the file watcher to whatever path we believe is
# current. If detection failed we re-watch the previous
# file — the next turn's `result` event will still fire.
+ # Reuses :meth:`_handle_jsonl_event` so the compaction +
+ # result-event detection are identical to the spawn path.
async def _on_event(event: Dict[str, Any]) -> None:
- session.events_this_turn.append(event)
- if self._provider.is_final_event(event):
- session.result_event.set()
+ await self._handle_jsonl_event(session, event)
session.jsonl_watcher = JsonlWatcher(
session.current_jsonl_path,