clarify_batch.py 1.9 KB

12345678910111213141516171819202122232425262728293031
  1. """The terminal result of a batch clarify, shared by every registry that holds one.
  2. A batch (``clarify`` server request with question ids) is answered per question with ``clarify.lock``
  3. and ends one of four ways: the last lock, a closing response frame, a timeout, or an interrupt/cancel.
  4. Main's TUI sidecar (``tui_gateway/server_requests.py``) and the session owner
  5. (``gateway/session_server_requests.py``) both settle batches; each used to compute ``{answers, outcome}``
  6. itself and they drifted. Both call :func:`settle_batch`, so the model is told the same thing on every
  7. surface.
  8. """
  9. from __future__ import annotations
  10. OUTCOMES = ("submitted", "timed_out", "cancelled")
  11. def settle_batch(locked: dict, frame_result=None, *, ended: str | None = None) -> dict:
  12. """``{answers, outcome}`` for a batch whose locks so far are *locked*.
  13. - ``ended`` (``"timed_out"`` / ``"cancelled"``): the wait ended with no frame (timeout, interrupt,
  14. a ``cancel()``), or ``"submitted"`` for the last lock. The locked answers stand.
  15. - Otherwise *frame_result* is the closing frame's ``result``. A dict with an ``answers`` dict lays
  16. those answers over the locks (the frame wins per question) and keeps the frame's own outcome when it
  17. names a known one (main's classic CLI reports ``timed_out``), else ``submitted``. Anything else (Ink
  18. Esc / Desktop Skip send ``{}``; an error frame gives ``None``) is a cancel-all: locks kept, ``cancelled``.
  19. """
  20. if ended is not None:
  21. return {"answers": dict(locked), "outcome": ended if ended in OUTCOMES else "cancelled"}
  22. if isinstance(frame_result, dict) and isinstance(frame_result.get("answers"), dict):
  23. outcome = frame_result.get("outcome")
  24. return {**frame_result, "answers": {**locked, **frame_result["answers"]},
  25. "outcome": outcome if outcome in OUTCOMES else "submitted"}
  26. return {"answers": dict(locked), "outcome": "cancelled"}