session_clarify_lock.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. """``clarify.lock`` on the session owner (#140400).
  2. The owner sends a local viewer's clarify batch as one ``clarify`` server request
  3. (``gateway/session_clarify_batch.py``). Ink's batch panel and Desktop's batch card answer it one
  4. question at a time with ``clarify.lock {request_id, question_id, answer}`` (``ClarifyLockParams`` /
  5. ``ClarifyLockResult``), as they did against main's sidecar; the owner had no such verb, so every
  6. answer was refused as an unknown method and the turn waited out ``clarify_timeout``. Only a viewer
  7. attached to the request's session with ``session:respond`` may lock its answers.
  8. """
  9. import json
  10. from hermes_state_runtime import RuntimeStoreError
  11. async def clarify_lock(connection, ref, params):
  12. from gateway.session_server_requests import lock_answer, may_settle
  13. # The dispatcher already refused unknown keys and mistyped fields against the declared
  14. # ``ClarifyLockParams`` (``tui_gateway/contracts/canonical.py``); a missing or empty id is ours to refuse.
  15. # ``profile`` naming another served profile was routed to its owner by ``dispatch``.
  16. request_id, question_id = params.get('request_id'), params.get('question_id')
  17. if not isinstance(request_id, str) or not request_id or not isinstance(question_id, str) or not question_id:
  18. raise RuntimeStoreError('invalid_params')
  19. answer = params.get('answer', '')
  20. if answer is not None and not isinstance(answer, str):
  21. answer = json.dumps(answer, ensure_ascii=False)
  22. authority, actor = connection.authority, connection.actor
  23. def entitled(session_id, events):
  24. # The same rule the closing response frame for this request id goes through.
  25. return may_settle(authority, actor, session_id, events)
  26. try:
  27. remaining = lock_answer(request_id, question_id, answer, session_ok=entitled)
  28. except ValueError:
  29. raise RuntimeStoreError('invalid_params') from None
  30. if remaining is None:
  31. # The wait already ended (timeout / cancel) while the card was still visible: not an error.
  32. return {'status': 'expired'}
  33. return {'status': 'ok', 'remaining': remaining}