Files
ai-agent/backend/test_gen_progress.py
gabrielvidalandClaude Opus 5.5 1f176f60f9 feat(gen-progress): relay the readings' trail, every second while a local run is live
- GenProgress.trail (GenProgressPoint[]) passes through /api/gen-progress and
  the gen-progress SSE event; a trail that only re-stamped its clocks is no move.
- The exit watcher polls the runners every GEN_RELAY_S (1 s) while a local run
  is live, 2 s otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 11:26:32 +01:00

109 lines
5.0 KiB
Python

"""Local-model runs' live generation, relayed (runs/genprogress.py): the map
the viewer reads, the ``gen-progress`` event only when it moved, a guest's
share of it, and the one stall push per idle stretch — against fakes.
``python3 -m unittest test_gen_progress`` from ``backend/`` (needs fastapi)."""
import pathlib
import tempfile
import types
import unittest
from unittest import mock
from core import routes as core_routes
from runs import genprogress as gp
from users.caller import Caller
SID, OTHER = "11111111-1111-1111-1111-111111111111", "22222222-2222-2222-2222-222222222222"
class FakeHub:
def __init__(self):
self.events: list[dict] = []
def publish(self, ev):
self.events.append(ev)
def _row(sid, **p):
return {"sessionId": sid, "progress": {"phase": "decode", "decoded": 10, "at": 1.0, **p}}
class GenProgressTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
for name, val in (("STATE_PATH", pathlib.Path(self.tmp.name) / "gp.json"),
("_by_runner", {}), ("_last_sent", None)):
p = mock.patch.object(gp, name, val)
p.start()
self.addCleanup(p.stop)
self.deps = types.SimpleNamespace(hub=FakeHub())
def test_the_map_merges_runners_and_drops_bookkeeping(self):
gp.update(self.deps, None, [_row(SID, lastActive=5.0, task=3), {"sessionId": OTHER}])
self.assertEqual(gp.snapshot(), {SID: {"phase": "decode", "decoded": 10, "at": 1.0}})
gp.update(self.deps, "mac", [_row(OTHER)])
self.assertEqual(set(gp.snapshot()), {SID, OTHER})
gp.update(self.deps, None, [])
self.assertEqual(set(gp.snapshot()), {OTHER})
def test_an_event_goes_out_only_when_something_but_the_clock_moved(self):
gp.update(self.deps, None, [_row(SID)])
gp.update(self.deps, None, [_row(SID, at=4.0)]) # same reading, later
gp.update(self.deps, None, [_row(SID, decoded=60, at=7.0)])
gp.update(self.deps, None, []) # the run ended
sent = [e["progress"] for e in self.deps.hub.events]
self.assertEqual([p.get(SID, {}).get("decoded") for p in sent], [10, 60, None])
self.assertEqual(self.deps.hub.events[0]["type"], "gen-progress")
def test_a_trail_that_only_re_stamped_is_no_move(self):
waiting = lambda at, trail: _row(SID, phase="idle", toolRunning=True, at=at, # noqa: E731
trail=[{"phase": "idle", "toolRunning": True, "at": t}
for t in trail])
gp.update(self.deps, None, [waiting(3.0, [1.0, 2.0])])
gp.update(self.deps, None, [waiting(5.0, [3.0, 4.0])]) # still waiting: silent
# a short step came and went between two relays: the trail caught it
caught = waiting(7.0, [5.0, 6.0])
caught["progress"]["trail"][1] = {"phase": "prefill", "promptDone": 80, "at": 6.0}
gp.update(self.deps, None, [caught])
self.assertEqual(len(self.deps.hub.events), 2)
self.assertEqual(gp.snapshot()[SID]["trail"][1]["phase"], "prefill")
def test_a_stall_pushes_once_per_idle_stretch(self):
now = 10_000.0
idle = lambda since: _row(SID, phase="idle", idleSince=since, model="m") # noqa: E731
with mock.patch.object(gp, "_push") as push, mock.patch.object(gp.time, "time", return_value=now):
gp.update(self.deps, None, [idle(now - 600)]) # 10 min: not yet
push.assert_not_called()
gp.update(self.deps, None, [idle(now - 1300)])
gp.update(self.deps, None, [idle(now - 1300)]) # same stretch: no repeat
self.assertEqual(push.call_count, 1)
gp.update(self.deps, None, [idle(now - 1250)]) # moved, then idle again
self.assertEqual(push.call_count, 2)
self.assertEqual(push.call_args.args[1], SID)
def test_a_busy_run_never_pushes(self):
with mock.patch.object(gp, "_push") as push:
gp.update(self.deps, None, [_row(SID, idleSince=None)])
push.assert_not_called()
def test_the_stall_message_names_what_is_wrong(self):
p = {"phase": "unreachable", "idleSince": 0.0, "model": "qwen3.8-flash-next-coder"}
msg = gp.stall_message(p, 21 * 60)
self.assertIn("21 min", msg)
self.assertIn("llm-server doesn't answer", msg)
def test_a_guest_sees_only_the_conversations_they_can_see(self):
guest = Caller(id="g1", role="guest", name="G")
meta = {SID: {"owner": "g1"}, OTHER: {"owner": "owner"}}
deps = types.SimpleNamespace(meta_store=types.SimpleNamespace(peek=lambda s: meta.get(s, {})))
prog = {SID: {"phase": "decode"}, OTHER: {"phase": "prefill"}}
self.assertEqual(set(core_routes._visible_progress(deps, guest, prog)), {SID})
ev = core_routes._guest_filter(deps, guest)({"type": "gen-progress", "progress": prog})
self.assertEqual(set(ev["progress"]), {SID})
if __name__ == "__main__":
unittest.main()