utils/leetcode_coach/leetcode_coach/relay/phone.py
Krzysztof kuhy Rudnicki 2531f9eeb3 Coach relay: phone writes go through an ordered outbox; 280 s request cap
The RTDB mirror wrote synchronously on the lane's one worker, so a hanging
Firebase (15 s connect timeouts) would have stalled every desktop request.
PhoneLink now only enqueues; relay.outbox writes on its own thread in the
contract's order (chat reset/lines, state, heartbeat, then the result),
keeps only the latest state and heartbeat, never drops a result or a chat
line, and retries a failed write first, with backoff.

The whole-request 280 s cap is enforced again: Coach.handle sets a
deadline and a coach turn gets what is left of it (at most 240 s), so an
ambiguous message's code check plus the turn cannot outlive the phone's
300 s "No reply" wait. The contract's caps line is back to that wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PjvF5DZG9JduSBYKxqyysi
2026-10-23 21:10:04 +02:00

206 lines
8.2 KiB
Python

"""The phone's path: RTDB requests in, results/state/chat/heartbeat out.
Under ``leetcode-guard/coach/`` (DOCS-coach-relay.md 1). Requests arrive over
an SSE stream of ``requests`` and, as a backstop, a rescan every
`RESCAN_S` and at start-up; both go through `consider`, which answers a rid
the done-map knows from the map (re-PUT, never re-run) and queues the rest
on the shared lane. Results, ``state`` (a JSON string leaf) and the chat
lines are written as they change; the heartbeat every `HEARTBEAT_S` and on
every ``rev`` or busy change. ``requests/{rid}`` goes 24 h after its result.
Writes only ever *enqueue* (`outbox`): the lane's worker never waits on
Firebase, so a hanging or down database never slows the desktop. A failed
write is logged and retried in order; the relay never exits over it.
"""
from __future__ import annotations
import functools
import json
import logging
import threading
from typing import TYPE_CHECKING, Any, Final
from leetcode_coach.relay import protocol, sse
from leetcode_coach.relay.done import DAY_MS
from leetcode_coach.relay.outbox import Outbox
from leetcode_coach.relay.rtdb import SERVER_TIME, RtdbError
if TYPE_CHECKING:
from collections.abc import Callable
from leetcode_coach.relay.coach import Coach
from leetcode_coach.relay.lane import Lane
from leetcode_coach.relay.rtdb import Rtdb
from leetcode_coach.relay.session import Session
_logger: Final = logging.getLogger(__name__)
_REQ_DEPTH: Final = 2
"""``{rid}/req``: a path two segments deep."""
HEARTBEAT_S: Final = 30.0
RESCAN_S: Final = 60.0
def _pairs(event: sse.Event) -> list[tuple[list[str], object]]:
"""``(path segments, value)`` for every node an event set."""
base = [s for s in event.path.split("/") if s]
if event.kind == "patch" and isinstance(event.data, dict):
return [
(base + [s for s in str(k).split("/") if s], v)
for k, v in event.data.items()
]
return [(base, event.data)]
class PhoneLink:
"""Mirrors the relay into RTDB and feeds RTDB requests to the lane."""
def __init__( # pylint: disable=too-many-arguments
self,
rtdb: Rtdb,
lane: Lane,
coach: Coach,
heartbeat: Callable[[], dict[str, Any]],
*,
clock_ms: Callable[[], int],
heartbeat_s: float = HEARTBEAT_S,
rescan_s: float = RESCAN_S,
) -> None:
"""Bind the database and the relay; call `attach` to start mirroring."""
self.rtdb = rtdb
self.lane = lane
self.coach = coach
self._heartbeat = heartbeat
self._clock = clock_ms
self._beat_every = heartbeat_s
self._rescan_every = rescan_s
self.outbox = Outbox()
self._mirror_lock = threading.Lock()
self._chat_sid: str | None = None
self._sent: set[str] = set()
def attach(self) -> None:
"""Listen to the lane and the backend; then mirror everything once."""
self.lane.on_result.append(self.on_result)
self.lane.on_busy.append(lambda _busy: self.beat())
self.coach.listeners.append(self.on_state)
self._chat_sid = None
self.on_state(self.coach.state(), self.coach.session)
def beat(self) -> None:
"""PUT the heartbeat (``beat`` as a server timestamp)."""
beat = {**self._heartbeat(), "beat": SERVER_TIME}
put = functools.partial(self.rtdb.put, "heartbeat", beat)
self.outbox.put("heartbeat", put, latest="heartbeat")
def put_result(self, rid: str, result: dict[str, Any]) -> None:
"""``requests/{rid}/result`` as a JSON string leaf."""
text = json.dumps(result, ensure_ascii=False)
put = functools.partial(self.rtdb.put, f"requests/{rid}/result", text)
self.outbox.put("result", put)
def on_result(self, req: protocol.Request, result: dict[str, Any]) -> None:
"""A finished request: answer it if it came from the phone."""
if req.transport == "phone":
self.put_result(req.rid, result)
def on_state(self, state: dict[str, Any], session: Session | None) -> None:
"""Queue the chat lines, then ``state``, then the heartbeat."""
text = json.dumps(state, ensure_ascii=False)
with self._mirror_lock:
sid = None if session is None else session.sid
if sid != self._chat_sid:
self.outbox.put(
"chat reset", functools.partial(self.rtdb.delete, "chat")
)
self._chat_sid, self._sent = sid, set()
lines = {} if session is None else dict(session.chat)
new: dict[str, object] = {
k: v for k, v in lines.items() if k not in self._sent
}
if new:
self.outbox.put("chat", functools.partial(self.rtdb.patch, "chat", new))
self._sent |= set(new)
put = functools.partial(self.rtdb.put, "state", text)
self.outbox.put("state", put, latest="state")
self.beat()
def consider(self, rid: str, node: object) -> None:
"""One ``requests/{rid}`` node: answer, queue, or leave it."""
if not isinstance(node, dict) or "req" not in node or "result" in node:
return
if not protocol.RID_RE.fullmatch(rid):
_logger.warning("phone request with a bad rid ignored")
return
if (done := self.lane.done.get(rid)) is not None:
self.put_result(rid, done.result)
return
raw = node["req"]
try:
req = protocol.parse(
{**raw, "rid": rid} if isinstance(raw, dict) else raw, "phone"
)
except protocol.BadRidError as exc:
_logger.warning("phone request %s refused: %s", rid, exc)
return
except protocol.BadRequestError as exc:
_logger.warning("bad phone request %s: %s", rid, exc)
result = protocol.failure("bad_request", str(exc), at=self._clock())
self.lane.finish(rid, "phone", result)
self.put_result(rid, result)
return
if (immediate := self.lane.submit(req)) is not None:
self.put_result(rid, immediate)
def on_event(self, event: sse.Event) -> None:
"""Route one SSE event under ``requests``."""
for segs, value in _pairs(event):
if not segs and isinstance(value, dict):
for rid in sorted(value):
self.consider(str(rid), value[rid])
elif len(segs) == 1:
self.consider(segs[0], value)
elif len(segs) == _REQ_DEPTH and segs[1] == "req":
self.consider(segs[0], {"req": value})
def rescan(self) -> None:
"""Pick up anything the stream missed; drop answered nodes after 24 h."""
try:
nodes = self.rtdb.get("requests")
except RtdbError as exc:
_logger.warning("RTDB rescan failed: %s", exc)
return
if not isinstance(nodes, dict):
return
now = self._clock()
for rid in sorted(nodes):
node = nodes[rid]
self.consider(str(rid), node)
if not isinstance(node, dict) or "result" not in node:
continue
done = self.lane.done.get(str(rid))
if done is None or now - done.at > DAY_MS:
drop = functools.partial(self.rtdb.delete, f"requests/{rid}")
self.outbox.put("cleanup", drop)
def listen(self, stop: threading.Event) -> None:
"""The SSE loop; a cancelled stream leaves the rescans to carry on."""
try:
for event in sse.listen(self.rtdb, "requests", stop):
self.on_event(event)
except sse.StreamCancelledError:
_logger.exception("the requests stream was cancelled; rescans only now")
def tend(self, stop: threading.Event, wait: Callable[[float], object]) -> None:
"""Heartbeat every 30 s, rescan every 60 s, until `stop`."""
since_scan = self._rescan_every
while not stop.is_set():
if since_scan >= self._rescan_every:
self.rescan()
since_scan = 0.0
self.beat()
wait(self._beat_every)
since_scan += self._beat_every