gateway_replay_gap.py 1.5 KB

1234567891011121314151617181920212223242526272829303132
  1. """After ``session.replay_gap``: re-attach, settle once, never splice the reply across the hole.
  2. On overflow the owner retires a viewer's subscription and sends one ``session.replay_gap``; the turns run
  3. on. The viewer re-attaches with ``session.resume``. Its snapshot is taken under the event-stream lock, so
  4. an admission it still lists publishes its completion on the new subscription, while one it no longer
  5. lists ended in the hole and only its receipt holds the reply. Live text stops at the gap.
  6. """
  7. from __future__ import annotations
  8. RESUMED = "[output resumed]"
  9. def pending_status(pending, admission):
  10. """``queued``/``started`` (its completion follows live), or None (it ended in the hole)."""
  11. return next((row.get("status") for row in pending or () if row.get("admission_id") == admission), None)
  12. def settled(receipt):
  13. """True: terminal with its reply (cancelled/failed need none); False: still running; None: unreadable."""
  14. if receipt.get("status") != "terminal":
  15. return False
  16. return True if receipt.get("outcome") in {"cancelled", "interrupted", "failed"} or "result" in receipt else None
  17. def owed_reply(shown, final):
  18. """What is owed of ``final`` after ``shown`` of its segment was painted: the rest of an exact prefix
  19. (ignoring the owner's leading ``"\\n\\n"`` and trailing whitespace), else a marker and the whole."""
  20. head = shown.removeprefix("\n\n")
  21. for prefix in (head, head.rstrip()):
  22. if final.startswith(prefix):
  23. return final[len(prefix):]
  24. return f"\n{RESUMED}\n{final}"