Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion loopx/canary/module_metric_baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"lines": 2760
},
"loopx/extensions/lark/private_conversations.py": {
"lines": 2191
"lines": 2213
},
"loopx/heartbeat_prompt.py": {
"any_count": 17,
Expand Down
5 changes: 5 additions & 0 deletions loopx/chat_ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ def read(path):
and str(payload.get("turn_id") or "") != active_turn_id
):
rows.append(payload)
# The ordinary read has no selectable Turn when storage has no queued
# candidates. Explicit capacity and strict reads still use the owner.
if not rows and not strict and not include_reserved and capacity_at is None:
return []

from .control_plane.chat_turn_acceptance import queued_chat_turn_ids
selected = set(queued_chat_turn_ids(rows, active_turn_id, include_reserved=include_reserved, capacity_at=capacity_at))
return sorted(
Expand Down
27 changes: 25 additions & 2 deletions loopx/extensions/lark/private_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from collections.abc import Callable, Iterator, Mapping, Sequence
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from functools import cached_property
from itertools import tee
from pathlib import Path
from threading import Lock
Expand Down Expand Up @@ -289,6 +290,10 @@ def configured_profile_apps(self, *, cli_bin: str | None = None) -> dict[str, st
apps.setdefault(app_id, app_id)
return {profile: app_id for profile, app_id in apps.items() if APP_ID_PATTERN.fullmatch(app_id)}

@cached_property
def _health_delivery_discovery(self) -> FileChatDiscovery:
return FileChatDiscovery()

@effect_runtime_request_scope()
def health(self) -> dict[str, dict[str, int]]:
health: dict[str, dict[str, int]] = {}
Expand All @@ -308,9 +313,27 @@ def observe_completed() -> None:
count(row)
completed.clear()

def health_fields(_path: Path, row: dict[str, Any]) -> dict[str, Any] | None:
if row["status"] != "delivered":
return None
deliveries = row.get("deliveries")
if not isinstance(deliveries, dict) or not all(
isinstance(phase, dict) for phase in deliveries.values()
):
return None
# Settings uses only correlation and retry metadata. Never retain
# delivered bodies or use these fields for admission or sending.
fields = ("binding_id", "status", "request_ref", "source", "session_id", "turn_id")
return {key: row[key] for key in fields if key in row} | {
"deliveries": {key: {"started": bool(phase.get("started")), "verified": bool(phase.get("verified"))}
for key, phase in deliveries.items()},
}

def delivered_requests() -> Iterator[dict[str, Any]]:
for path in [*self.root.glob("*.json"), *self.root.glob("web-projections/*.json")]:
row = _read_json(path)
paths = [*self.root.glob("*.json"), *self.root.glob("web-projections/*.json")]
for path, row in self._health_delivery_discovery.rows(
self.root, paths, read=_read_json, project=health_fields,
):
health.setdefault(row["binding_id"], {"pending_count": 0, "recovery_count": 0})
if row["status"] != "delivered" and triage.pending(row):
count(row)
Expand Down
23 changes: 22 additions & 1 deletion tests/test_lark_private_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,7 @@ def counted_revision():
def test_settings_health_reuses_core_correlation_but_observes_current_work(tmp_path, monkeypatch):
from loopx.capabilities.native_chat import external_conversations
from loopx.chat_store import _atomic_write_json
from loopx.extensions.lark import private_conversations
from test_private_chat_backlog_projection import fixture_history

store, core, transport, rows = fixture_history(tmp_path, count=2)
Expand All @@ -2381,19 +2382,29 @@ def test_settings_health_reuses_core_correlation_but_observes_current_work(tmp_p
_atomic_write_json(partial_path, {**initial_partial, "deliveries": {
"terminal": {"started": True, "verified": False}}})
read_request = external_conversations._read_json
read_delivery = private_conversations._read_json
request_reads = []
delivery_reads = []

def observe(path):
request_reads.append(path)
return read_request(path)

def observe_delivery(path):
delivery_reads.append(path)
return read_delivery(path)

monkeypatch.setattr(external_conversations, "_read_json", observe)
monkeypatch.setattr(private_conversations, "_read_json", observe_delivery)
assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 1}
assert set(request_reads) == {partial_request, completed_request}
assert set(delivery_reads) == {partial_path, completed_path}
request_reads.clear()
delivery_reads.clear()
assert transport.health()[binding] == {"pending_count": 1, "recovery_count": 1}
# Windows has no reliable ctime mutation stamp; its reader stays fresh.
assert set(request_reads) == ({partial_request, completed_request} if sys.platform == "win32" else set())
assert set(delivery_reads) == ({partial_path, completed_path} if sys.platform == "win32" else set())
request_reads.clear()

# Independent native updates must not wait for the Lark receipt to change.
Expand Down Expand Up @@ -4608,7 +4619,7 @@ def request(path, body=None):

def test_http_stop_recovery_reuses_source_revision_without_reusing_turn_state(ordinary, monkeypatch): # noqa: F811
from loopx.chat_server import ChatHTTPServer, ChatRequestHandler
from loopx.control_plane import effect_runtime
from loopx.control_plane import chat_turn_acceptance, effect_runtime

store, runtime, provider, transport = connect(ordinary)
binding = transport._binding("notes-app")
Expand All @@ -4626,9 +4637,11 @@ def queue(name, binding_id):
original_turns = [store._turn_path(sid, turn["turn_id"]).read_bytes() for turn in [owed, authorized]]
native_before = ordinary[4].read_bytes() if ordinary[4].exists() else None
revision_reads = []
queue_selections = []
observed = threading.local()
fingerprint = effect_runtime._runtime_fingerprint
stop_recoveries = ChatConversationBindings.stop_recoveries
select_queue = chat_turn_acceptance.queued_chat_turn_ids

def counted_fingerprint():
if getattr(observed, "stop_recovery", False):
Expand All @@ -4642,8 +4655,14 @@ def observed_stop_recoveries(self, current):
finally:
observed.stop_recovery = False

def read_queue(turns, *args, **kwargs):
if getattr(observed, "stop_recovery", False):
queue_selections.append(len(turns))
return select_queue(turns, *args, **kwargs)

monkeypatch.setattr(effect_runtime, "_runtime_fingerprint", counted_fingerprint)
monkeypatch.setattr(ChatConversationBindings, "stop_recoveries", observed_stop_recoveries)
monkeypatch.setattr(chat_turn_acceptance, "queued_chat_turn_ids", read_queue)
server = ChatHTTPServer(("127.0.0.1", 0), ChatRequestHandler)
server.chat_store, server.runtime_controller, server.verbose = store, runtime, False
server.lark_private_conversations = transport
Expand All @@ -4653,13 +4672,15 @@ def observed_stop_recoveries(self, current):

def snapshot():
revision_reads.clear()
queue_selections.clear()
connection = http.client.HTTPConnection(*server.server_address, timeout=15)
try:
connection.request("GET", "/api/chat/lark/private-conversations")
response = connection.getresponse()
assert response.status == 200
result = json.loads(response.read())
assert len(revision_reads) <= 1 # At most one source inventory, rather than one per Session.
assert len(queue_selections) == 1 and queue_selections[0] > 0
return result
finally:
connection.close()
Expand Down
Loading