From 40e999803985204d9302d37133579acffb51a176 Mon Sep 17 00:00:00 2001 From: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> Date: Fri, 20 Nov 2026 05:16:09 +0800 Subject: [PATCH] perf(native_chat,lark): cache unchanged quota backlog health and interrupt observations Signed-off-by: LoopX Agent <337587101+loopx-agent@users.noreply.github.com> --- .../native_chat/external_conversations.py | 47 ++-- .../native_chat/file_discovery.py | 253 +++++++++++++++++- .../extensions/lark/private_conversations.py | 2 +- .../test_lark_steward_directory_scan_perf.py | 197 ++++++++++++++ 4 files changed, 474 insertions(+), 25 deletions(-) diff --git a/loopx/capabilities/native_chat/external_conversations.py b/loopx/capabilities/native_chat/external_conversations.py index f75b5ca83c..45b3230642 100644 --- a/loopx/capabilities/native_chat/external_conversations.py +++ b/loopx/capabilities/native_chat/external_conversations.py @@ -39,9 +39,11 @@ from .codex_context import checked_session_home from .file_discovery import ( FileChatDiscovery, + QuotaBacklogObservationCache, external_source_route_path, file_change_fingerprint, later_equivalent_source_input, + observe_quota_backlog_batch, recovery_journal_fields, source_control_fields, ) @@ -137,6 +139,7 @@ def __init__(self, controller: Any) -> None: self._recovery_discovery = FileChatDiscovery() self._user_messages_lock = threading.Lock() self._session_user_messages_cache: dict[str, tuple[tuple[int, ...], tuple[dict[str, Any], ...]]] = {} + self._quota_backlog_cache = QuotaBacklogObservationCache() self.actions: Any | None = None def _retained_result_review(self, row: dict[str, Any]) -> dict[str, Any] | None: @@ -406,36 +409,34 @@ def _source_stop_observed(self, row: dict[str, Any], turn: dict[str, Any], for item in (source_controls or self.source_controls)()))) def quota_backlog_observer( - self, *, source_controls: Callable[[], list[dict[str, Any]]] | None = None, + self, *, source_controls: Callable[[], list[dict[str, Any]]] | None = None, use_cache: bool = False, ) -> Callable[[Sequence[dict[str, Any]]], list[bool]]: """Read Session state after each batch's Turns and share source controls across batches.""" read_controls = cache(source_controls or self.source_controls) + @cache + def stop_controls() -> tuple[tuple[Any, Any, datetime], ...]: + return tuple( + (item.get("binding_id"), item.get("source"), datetime.fromisoformat(item["created_at"])) + for item in read_controls() + if item.get("command") in {"stop", "new"} + ) + def observe(rows: Sequence[dict[str, Any]]) -> list[bool]: if len(rows) > QUOTA_BACKLOG_BATCH_SIZE: raise ValueError("select at most 64 pending requests per observation") - params = [self._quota_pending_inputs(row) for row in rows] - load_session = self.controller.store.load_session - sessions = {} - for session_id in dict.fromkeys(row["session_id"] for row in rows): - # A transient read failure must not mask a closed Session. - session = load_session(session_id) or load_session(session_id) - sessions[session_id] = ({key: session[key] for key in ("session_id", "status") if key in session} - if session is not None else None) - for row, values in zip(rows, params, strict=True): - values["current_session"] = sessions[row["session_id"]] - flags = self._quota_pending_flags(params) - stopped = [] - for index, (row, values, pending) in enumerate(zip(rows, params, flags, strict=True)): - if pending: - events = self.controller.store.events_after(row["session_id"], row["turn_id"], None) - if self._source_stop_observed(row, values["pending_observation"]["turn"], events, - source_controls=read_controls): - values["pending_observation"]["stop_observed"] = True - stopped.append(index) - for index, pending in zip(stopped, self._quota_pending_flags([params[i] for i in stopped]), strict=True): - flags[index] = pending - return flags + cache_store = None + if use_cache: + cache_store = getattr(self, "_quota_backlog_cache", None) + if cache_store is None: + cache_store = QuotaBacklogObservationCache() + self._quota_backlog_cache = cache_store + return observe_quota_backlog_batch( + root=self.root, store=self.controller.store, rows=rows, + build_inputs=self._quota_pending_inputs, eval_flags=self._quota_pending_flags, + stop_controls=stop_controls, evaluator_id=id(effect_runtime.effect_runtime_result), + cache_store=cache_store, + ) return observe diff --git a/loopx/capabilities/native_chat/file_discovery.py b/loopx/capabilities/native_chat/file_discovery.py index fe0958ad43..e4ef53610a 100644 --- a/loopx/capabilities/native_chat/file_discovery.py +++ b/loopx/capabilities/native_chat/file_discovery.py @@ -1,7 +1,7 @@ """File-backed Chat journal discovery and bounded reply image artifacts.""" from __future__ import annotations -from collections.abc import Callable, Iterable, Iterator +from collections.abc import Callable, Iterable, Iterator, Sequence from copy import deepcopy from datetime import datetime import base64 @@ -108,6 +108,9 @@ def later_equivalent_source_input( return False +_MISSING_FILE_FP: tuple[int, ...] = (-1,) + + def file_change_fingerprint(path: Path) -> tuple[int, ...] | None: """A local mutation token, or no reuse when the platform lacks one.""" # Windows stat ctime can be creation time, not a mutation stamp. Keep @@ -118,6 +121,254 @@ def file_change_fingerprint(path: Path) -> tuple[int, ...] | None: return stat.st_dev, stat.st_ino, stat.st_mtime_ns, stat.st_ctime_ns, stat.st_size +def optional_file_fingerprint(path: Path) -> tuple[int, ...] | None: + """Return a file fingerprint or a sentinel when absent; None when unavailable.""" + try: + return file_change_fingerprint(path) + except FileNotFoundError: + return None if sys.platform == "win32" else _MISSING_FILE_FP + except OSError: + return None + + +def _set_bounded_cache(cache_map: dict[Any, Any], key: Any, value: Any, max_entries: int) -> None: + if key in cache_map: + del cache_map[key] + elif len(cache_map) >= max_entries: + del cache_map[next(iter(cache_map))] + cache_map[key] = value + + +class QuotaBacklogObservationCache: + """Revision-bound cache for read-only quota backlog health classification. + + Stores only pure boolean verdicts and Turn status labels keyed by file + fingerprints, Session status, and evaluator identity. Never retains Turn + bodies or request attachments. + """ + + def __init__(self, *, max_entries: int = 8192) -> None: + self._lock = Lock() + self._max_entries = max(1, max_entries) + self._base: dict[str, tuple[tuple[Any, ...], tuple[bool, str | None]]] = {} + self._stopped: dict[str, tuple[tuple[Any, ...], bool]] = {} + self._interrupt_events: dict[tuple[str, str], tuple[tuple[int, ...], bool]] = {} + + def get_base(self, request_ref: str, key: tuple[Any, ...]) -> tuple[bool, str | None] | None: + with self._lock: + entry = self._base.get(request_ref) + return entry[1] if entry is not None and entry[0] == key else None + + def has_base_prefix(self, request_ref: str, key_prefix: tuple[Any, ...]) -> bool: + with self._lock: + entry = self._base.get(request_ref) + return entry is not None and entry[0][:-1] == key_prefix + + def put_base(self, request_ref: str, key: tuple[Any, ...], pending: bool, turn_status: str | None) -> None: + with self._lock: + _set_bounded_cache(self._base, request_ref, (key, (pending, turn_status)), self._max_entries) + stopped = self._stopped.get(request_ref) + if stopped is not None and stopped[0] != key: + self._stopped.pop(request_ref, None) + + def get_stopped(self, request_ref: str, key: tuple[Any, ...]) -> bool | None: + with self._lock: + entry = self._stopped.get(request_ref) + return entry[1] if entry is not None and entry[0] == key else None + + def put_stopped(self, request_ref: str, key: tuple[Any, ...], pending: bool) -> None: + with self._lock: + _set_bounded_cache(self._stopped, request_ref, (key, pending), self._max_entries) + + def drop_request(self, request_ref: str) -> None: + with self._lock: + self._base.pop(request_ref, None) + self._stopped.pop(request_ref, None) + + @staticmethod + def build_key_prefix( + root: Path, store: Any, row: dict[str, Any], evaluator_id: int, + ) -> tuple[Any, Path | None, Path | None, tuple[int, ...] | None, tuple[int, ...] | None]: + try: + req_path = root / f"{row['request_ref']}.json" + turn_path = store._turn_path(row["session_id"], row["turn_id"]) + req_fp = optional_file_fingerprint(req_path) + turn_fp = optional_file_fingerprint(turn_path) + except (OSError, ValueError): + return None, None, None, None, None + if req_fp is None or turn_fp is None: + return None, req_path, turn_path, req_fp, turn_fp + prefix = (req_fp, turn_fp, row["session_id"], row["turn_id"], + row.get("status"), row.get("command"), evaluator_id) + return prefix, req_path, turn_path, req_fp, turn_fp + + def commit(self, request_ref: str, meta_entry: tuple[Any, ...], pending: bool, *, + turn_status: str | None = None, stopped: bool = False) -> None: + key, req_path, turn_path, req_fp, turn_fp = meta_entry + if key is None or req_path is None or turn_path is None: + return + if (optional_file_fingerprint(req_path), optional_file_fingerprint(turn_path)) != (req_fp, turn_fp): + self.drop_request(request_ref) + return + if stopped: + self.put_stopped(request_ref, key, pending) + else: + self.put_base(request_ref, key, pending, turn_status) + + def get_interrupt(self, session_id: str, turn_id: str, events_fp: tuple[int, ...]) -> bool | None: + with self._lock: + entry = self._interrupt_events.get((session_id, turn_id)) + return entry[1] if entry is not None and entry[0] == events_fp else None + + def put_interrupt(self, session_id: str, turn_id: str, events_fp: tuple[int, ...], observed: bool) -> None: + with self._lock: + _set_bounded_cache(self._interrupt_events, (session_id, turn_id), (events_fp, observed), self._max_entries) + + def drop_interrupt(self, session_id: str, turn_id: str) -> None: + with self._lock: + self._interrupt_events.pop((session_id, turn_id), None) + + +def observed_turn_interrupt( + store: Any, + session_id: str, + turn_id: str, + cache_store: QuotaBacklogObservationCache | None, + *, + real_events: bool, +) -> bool: + """Check Turn interrupt events with fingerprint/pending-buffer guard on real stores.""" + interrupt_kinds = {"turn.interrupt_requested", "turn.interrupted"} + if not real_events or cache_store is None: + return any(e.get("kind") in interrupt_kinds for e in store.events_after(session_id, turn_id, None)) + key = (session_id, turn_id) + try: + event_path = store._event_path(session_id, turn_id) + except ValueError: + return any(e.get("kind") in interrupt_kinds for e in store.events_after(session_id, turn_id, None)) + flush_lock = store._event_flush_lock(key) + fp_before: tuple[int, ...] | None = None + if flush_lock.acquire(blocking=False): + try: + with store._event_lock: + has_pending = bool(store._event_pending.get(key)) + if not has_pending: + fp_before = optional_file_fingerprint(event_path) + if fp_before is not None: + cached = cache_store.get_interrupt(session_id, turn_id, fp_before) + if cached is not None: + return cached + finally: + flush_lock.release() + observed = any(e.get("kind") in interrupt_kinds for e in store.events_after(session_id, turn_id, None)) + if fp_before is not None and flush_lock.acquire(blocking=False): + try: + with store._event_lock: + has_pending_after = bool(store._event_pending.get(key)) + fp_after = None if has_pending_after else optional_file_fingerprint(event_path) + if fp_after == fp_before: + cache_store.put_interrupt(session_id, turn_id, fp_after, observed) + else: + cache_store.drop_interrupt(session_id, turn_id) + finally: + flush_lock.release() + return observed + + +def _same_store_method(store: Any, name: str, expected_cls: type[Any]) -> bool: + return isinstance(store, expected_cls) and getattr(getattr(store, name, None), "__func__", None) is getattr(expected_cls, name) + + +def observe_quota_backlog_batch( + *, + root: Path, + store: Any, + rows: Sequence[dict[str, Any]], + build_inputs: Callable[[dict[str, Any]], dict[str, Any]], + eval_flags: Callable[[Sequence[dict[str, Any]]], list[bool]], + stop_controls: Callable[[], tuple[tuple[Any, Any, datetime], ...]], + evaluator_id: int, + cache_store: QuotaBacklogObservationCache | None, +) -> list[bool]: + """Evaluate backlog pending flags with optional revision-bound caching.""" + from ...chat_store import ChatSessionStore + + real_load_turn = cache_store is not None and _same_store_method(store, "load_turn", ChatSessionStore) + real_events = cache_store is not None and _same_store_method(store, "events_after", ChatSessionStore) + flags: list[bool] = [False] * len(rows) + turn_statuses: list[str | None] = [None] * len(rows) + params: list[dict[str, Any] | None] = [None] * len(rows) + meta: list[tuple[Any, ...]] = [(None, None, None, None, None)] * len(rows) + for index, row in enumerate(rows): + entry = (cache_store.build_key_prefix(root, store, row, evaluator_id) + if real_load_turn and cache_store is not None else (None, None, None, None, None)) + meta[index] = entry + if entry[0] is not None and cache_store is not None and cache_store.has_base_prefix(row["request_ref"], entry[0]): + continue + params[index] = build_inputs(row) + load_session = store.load_session + sessions: dict[str, dict[str, Any] | None] = {} + for session_id in dict.fromkeys(row["session_id"] for row in rows): + # A transient read failure must not mask a closed Session. + session = load_session(session_id) or load_session(session_id) + sessions[session_id] = ( + {key: session[key] for key in ("session_id", "status") if key in session} + if session is not None else None + ) + misses: list[tuple[int, dict[str, Any]]] = [] + for index, row in enumerate(rows): + session_row = sessions[row["session_id"]] + prefix, req_path, turn_path, req_fp, turn_fp = meta[index] + if prefix is not None and cache_store is not None: + session_status = session_row.get("status") if session_row is not None else None + full_key = (*prefix, session_status) + meta[index] = (full_key, req_path, turn_path, req_fp, turn_fp) + cached = cache_store.get_base(row["request_ref"], full_key) + if cached is not None: + flags[index], turn_statuses[index] = cached + continue + values = params[index] if params[index] is not None else build_inputs(row) + values["current_session"] = session_row + params[index] = values + misses.append((index, values)) + if misses: + for (index, values), pending in zip(misses, eval_flags([v for _, v in misses]), strict=True): + turn_dict = values["pending_observation"]["turn"] + turn_status = turn_dict.get("status") if isinstance(turn_dict, dict) else None + flags[index], turn_statuses[index] = pending, turn_status + if cache_store is not None: + cache_store.commit(rows[index]["request_ref"], meta[index], pending, turn_status=turn_status) + stopped_misses: list[tuple[int, dict[str, Any]]] = [] + for index, (row, pending) in enumerate(zip(rows, flags, strict=True)): + if not pending: + continue + if not observed_turn_interrupt(store, row["session_id"], row["turn_id"], cache_store, real_events=real_events): + if turn_statuses[index] not in {"failed", "completed"}: + continue + row_created = datetime.fromisoformat(row["created_at"]) + if not any(b == row["binding_id"] and s == row["source"] and c >= row_created + for b, s, c in stop_controls()): + continue + cache_key = meta[index][0] + if cache_store is not None and cache_key is not None: + cached_stop = cache_store.get_stopped(row["request_ref"], cache_key) + if cached_stop is not None: + flags[index] = cached_stop + continue + values = params[index] + if values is None: + values = build_inputs(row) + values["current_session"] = sessions[row["session_id"]] + values["pending_observation"]["stop_observed"] = True + stopped_misses.append((index, values)) + if stopped_misses: + for (index, _), pending in zip(stopped_misses, eval_flags([v for _, v in stopped_misses]), strict=True): + flags[index] = pending + if cache_store is not None: + cache_store.commit(rows[index]["request_ref"], meta[index], pending, stopped=True) + return flags + + class FileChatDiscovery: """Keep only caller-selected metadata while the complete file is unchanged. diff --git a/loopx/extensions/lark/private_conversations.py b/loopx/extensions/lark/private_conversations.py index ce22ffbaad..7df64a6e18 100644 --- a/loopx/extensions/lark/private_conversations.py +++ b/loopx/extensions/lark/private_conversations.py @@ -190,7 +190,7 @@ def _health_delivery_discovery(self) -> FileChatDiscovery: @effect_runtime_request_scope() def health(self) -> dict[str, dict[str, int]]: health: dict[str, dict[str, int]] = {} - quota_pending = self.core.quota_backlog_observer() + quota_pending = self.core.quota_backlog_observer(use_cache=True) completed: list[tuple[dict[str, Any], dict[str, Any]]] = [] def count(row: dict[str, Any]) -> None: diff --git a/tests/test_lark_steward_directory_scan_perf.py b/tests/test_lark_steward_directory_scan_perf.py index a7a4d645d1..65dc8579f7 100644 --- a/tests/test_lark_steward_directory_scan_perf.py +++ b/tests/test_lark_steward_directory_scan_perf.py @@ -576,3 +576,200 @@ def counted_read(path: Path): second = {path: row for path, row, _ in discovery.rows()} assert second[retired_path]["triage"]["retired"] == "card_delivery_expired" assert reads == [active_path], "retired triage capture must stay cached while active capture stays fresh" + + +def _seed_backlog_health_store(tmp_path: Path, *, failed_count: int = 20, completed_count: int = 20): + from loopx.chat_store import CHAT_TURN_SCHEMA_VERSION, ChatSessionStore + from loopx.extensions.lark.private_conversations import LarkPrivateConversations + + store = ChatSessionStore(tmp_path) + session = store.create_session( + goal_id="goal-health-cache", + agent_id="codex", + adapter_kind="codex_app_server", + upstream_thread_id="thread-health-cache", + session_id="a" * 32, + ) + sid = session["session_id"] + controller = SimpleNamespace( + store=store, + project_contexts=SimpleNamespace(conversation_bindings=SimpleNamespace()), + ) + core = ChatExternalConversations(controller) + transport = LarkPrivateConversations.__new__(LarkPrivateConversations) + transport.core = core + transport.root = store.root / "lark-private-deliveries" + core.root.mkdir(parents=True, exist_ok=True) + transport.root.mkdir(parents=True, exist_ok=True) + + failed_rows: list[dict[str, object]] = [] + for idx in range(failed_count + completed_count): + is_failed = idx < failed_count + ref = _hex_ref(1000 + idx) + tid = f"{1000 + idx:032x}" + row = { + "request_ref": ref, + "binding_id": "b" * 24, + "source": {"source_ref": "c" * 24}, + "session_id": sid, + "turn_id": tid, + "command": None, + "status": "accepted", + "delivery_verified": True, + "message": f"req {idx}", + "created_at": "2026-01-01T00:00:00+00:00", + } + turn = { + "schema_version": CHAT_TURN_SCHEMA_VERSION, + "session_id": sid, + "turn_id": tid, + "client_turn_id": f"external-{ref}", + "status": "failed" if is_failed else "completed", + **({"error_code": "usage_limit_exceeded", "response": None} if is_failed else { + "error_code": None, + "response": {"message": "done", "proposals": []}, + }), + } + _atomic_write_json(store._turn_path(sid, tid), turn) + _atomic_write_json(core.root / f"{ref}.json", row) + _atomic_write_json(transport.root / f"{ref}.json", {**row, "status": "delivered", "deliveries": {}}) + if is_failed: + failed_rows.append(row) + return store, core, transport, sid, failed_rows + + +def test_backlog_health_caches_unchanged_turns_and_avoids_ipc_and_event_lock_churn(tmp_path, monkeypatch): + from loopx.chat_store import ChatSessionStore + from loopx.control_plane import effect_runtime + + store, core, transport, _sid, failed_rows = _seed_backlog_health_store(tmp_path) + original_rpc = effect_runtime.effect_runtime_result + original_load_turn = ChatSessionStore.load_turn + original_events_after = ChatSessionStore.events_after + counts = {"rpc": 0, "load_turn": 0, "events_after": 0} + + def counted_rpc(method, params, **kwargs): + if method == "collaboration.conversation.request": + counts["rpc"] += 1 + return original_rpc(method, params, **kwargs) + + def counted_load_turn(self, session_id: str, turn_id: str): + counts["load_turn"] += 1 + return original_load_turn(self, session_id, turn_id) + + def counted_events_after(self, session_id: str, turn_id: str, event_id: str | None): + counts["events_after"] += 1 + return original_events_after(self, session_id, turn_id, event_id) + + monkeypatch.setattr(effect_runtime, "effect_runtime_result", counted_rpc) + monkeypatch.setattr(ChatSessionStore, "load_turn", counted_load_turn) + monkeypatch.setattr(ChatSessionStore, "events_after", counted_events_after) + + assert transport.health()["b" * 24] == {"pending_count": 20, "recovery_count": 0} + assert counts["rpc"] == 1 + assert counts["load_turn"] == 40 + assert counts["events_after"] == 20 + + counts = {"rpc": 0, "load_turn": 0, "events_after": 0} + assert transport.health()["b" * 24] == {"pending_count": 20, "recovery_count": 0} + assert counts == {"rpc": 0, "load_turn": 0, "events_after": 0} + + # Uncached direct observer (default use_cache=False) still evaluates in-memory mutations fresh. + mutated = [{**failed_rows[0], "command": "stop"}] + assert core.quota_backlog_observer()(mutated) == [False] + + +def test_backlog_health_cache_invalidates_on_turn_event_recovery_session_and_stop_mutations(tmp_path, monkeypatch): + from loopx.chat_store import CHAT_TURN_SCHEMA_VERSION + from loopx.control_plane import effect_runtime + + store, core, transport, sid, failed_rows = _seed_backlog_health_store(tmp_path, failed_count=6, completed_count=4) + original_rpc = effect_runtime.effect_runtime_result + rpc_calls = 0 + + def counted_rpc(method, params, **kwargs): + nonlocal rpc_calls + if method == "collaboration.conversation.request": + rpc_calls += 1 + return original_rpc(method, params, **kwargs) + + monkeypatch.setattr(effect_runtime, "effect_runtime_result", counted_rpc) + assert transport.health()["b" * 24]["pending_count"] == 6 + assert rpc_calls == 1 + + # 1. Completing one failed Turn invalidates only that row immediately. + r0 = failed_rows[0] + _atomic_write_json(store._turn_path(sid, str(r0["turn_id"])), { + "schema_version": CHAT_TURN_SCHEMA_VERSION, + "session_id": sid, + "turn_id": r0["turn_id"], + "client_turn_id": f"external-{r0['request_ref']}", + "status": "completed", + "error_code": None, + "response": {"message": "ok", "proposals": []}, + }) + assert transport.health()["b" * 24]["pending_count"] == 5 + + # 2. Appending a flushed turn.interrupted event invalidates that Turn immediately. + r1 = failed_rows[1] + store.append_event(sid, str(r1["turn_id"]), kind="turn.interrupted", payload={}, buffered=False) + assert transport.health()["b" * 24]["pending_count"] == 4 + + # 3. Buffered (unflushed) turn.interrupt_requested in _event_pending must never serve stale cache. + r2 = failed_rows[2] + store.append_event(sid, str(r2["turn_id"]), kind="turn.interrupt_requested", payload={}, buffered=True) + assert transport.health()["b" * 24]["pending_count"] == 3 + + # 4. Recording a verified native_result_recovery on disk invalidates that request immediately. + r3 = failed_rows[3] + _atomic_write_json(core.root / f"{r3['request_ref']}.json", { + **r3, + "native_result_recovery": { + "turn_id": r3["turn_id"], + "message_id": f"native.{r3['turn_id']}.recovered", + "message_text": "recovered ok", + "delivery_verified": True, + "native_observation": {"response": {"message": "recovered ok", "proposals": []}}, + }, + }) + assert transport.health()["b" * 24]["pending_count"] == 2 + + # 5. Writing a later /stop command clears remaining pending items using only stopped RPC, then hits _stopped cache. + rpc_calls = 0 + _atomic_write_json(core.root / f"{_hex_ref(9999)}.json", { + "request_ref": _hex_ref(9999), + "binding_id": "b" * 24, + "source": {"source_ref": "c" * 24}, + "session_id": sid, + "turn_id": None, + "command": "stop", + "status": "command_completed", + "created_at": "2026-01-02T00:00:00+00:00", + }) + assert transport.health()["b" * 24]["pending_count"] == 0 + assert rpc_calls == 1 + + rpc_calls = 0 + assert transport.health()["b" * 24]["pending_count"] == 0 + assert rpc_calls == 0 + + # Replacing effect_runtime_result invalidates cached verdicts by evaluator identity. + def wrapper_rpc(method, params, **kwargs): + nonlocal rpc_calls + if method == "collaboration.conversation.request": + rpc_calls += 1 + return original_rpc(method, params, **kwargs) + + monkeypatch.setattr(effect_runtime, "effect_runtime_result", wrapper_rpc) + assert transport.health()["b" * 24]["pending_count"] == 0 + assert rpc_calls > 0 + rpc_calls = 0 + assert transport.health()["b" * 24]["pending_count"] == 0 + assert rpc_calls == 0 + + # 6. Closing a Session also drops remaining cached pending counts. + _store2, _core2, transport2, sid2, _ = _seed_backlog_health_store(tmp_path / "s2", failed_count=3, completed_count=0) + assert transport2.health()["b" * 24]["pending_count"] == 3 + sess_path = _store2._session_path(sid2) + _atomic_write_json(sess_path, {**store_read_json(sess_path), "status": "closed"}) + assert transport2.health()["b" * 24]["pending_count"] == 0