session_vault_prompt.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """The browser vault's prompts (unlock, save login, one-time code) for a local viewer's turn.
  2. Main's TUI sidecar registered the three vault callbacks per session (``tui_gateway/agent_callbacks.py``):
  3. ``browser_vault`` asked the renderer through ``vault.unlock_prompt``, ``vault.save_login`` and ``vault.code``
  4. server→client requests, and the Desktop and Ink cards answered with ``{value}``. On the session authority no
  5. callback was registered, so every Desktop chat reported ``prompt_unavailable`` ("this session cannot ask the
  6. user") and a site's emailed or texted code could not be entered at all. Here the owner sends the same request
  7. frames (``tui_gateway/contracts/server_requests.py``) to the viewers attached to the session's event stream and
  8. blocks the agent thread until one answers, the turn is interrupted, or it times out. Passwords and codes are
  9. never published, journaled or logged: they travel only in the viewer's response frame. The request registry
  10. itself is ``gateway/session_server_requests.py``.
  11. """
  12. from __future__ import annotations
  13. import contextlib
  14. import json
  15. import logging
  16. logger = logging.getLogger(__name__)
  17. # Main's sidecar deadlines (``tui_gateway/agent_callbacks.py``).
  18. UNLOCK_TIMEOUT_S = 120
  19. SAVE_LOGIN_TIMEOUT_S = 180
  20. CODE_TIMEOUT_S = 180
  21. def release_unlocks(session_id) -> None:
  22. """The session closed: lock the password managers it unlocked, as the sidecar's session end does
  23. (``tui_gateway/session_lifecycle.py`` ``_lock_vault_managers``). Other sessions keep theirs."""
  24. try:
  25. from agent.vault_backends import unlock
  26. unlock.release_session(session_id)
  27. except Exception: # health: allow BLE001 -- a close must finish even if the lock fails
  28. logger.debug("vault manager lock on session close failed", exc_info=True)
  29. @contextlib.contextmanager
  30. def vault_prompt_scope(turn, agent):
  31. """Install the vault callbacks on this (agent) thread for a local viewer's turn; restore after.
  32. Tool workers inherit them through ``tools.thread_context.propagate_context_to_thread``."""
  33. from gateway.run_turn_progress import local_viewer_turn
  34. if not local_viewer_turn(turn):
  35. yield
  36. return
  37. from agent.vault_backends import unlock
  38. authority, session_id, _generation = turn._approval_owner
  39. def ask(method, params, timeout):
  40. from gateway.session_server_requests import ask_viewer
  41. live = authority.sessions.get(session_id)
  42. if live is None:
  43. return ""
  44. return ask_viewer(live.event_stream, session_id, method, params, timeout=timeout,
  45. cancelled=lambda: bool(getattr(agent, "_interrupt_requested", False)))
  46. def unlock_prompt(backend, display_name):
  47. return ask("vault.unlock_prompt", {"backend": backend, "display_name": display_name}, UNLOCK_TIMEOUT_S)
  48. def save_login(origin, site):
  49. # The card sends identifier + password as one JSON string; it goes straight to the vault store.
  50. raw = ask("vault.save_login", {"origin": origin, "site": site}, SAVE_LOGIN_TIMEOUT_S)
  51. try:
  52. data = json.loads(raw) if raw else None
  53. except ValueError:
  54. return None
  55. return data if isinstance(data, dict) and data.get("password") else None
  56. def code_prompt(site, hint):
  57. return ask("vault.code", {"site": site, "hint": hint}, CODE_TIMEOUT_S)
  58. previous = (unlock.get_unlock_prompt_callback(), unlock.get_save_login_prompt_callback(),
  59. unlock.get_code_prompt_callback(), unlock.get_current_session_id())
  60. unlock.set_unlock_prompt_callback(unlock_prompt)
  61. unlock.set_save_login_prompt_callback(save_login)
  62. unlock.set_code_prompt_callback(code_prompt)
  63. unlock.set_current_session_id(session_id) # an unlock made on this turn is released when this session closes
  64. try:
  65. yield
  66. finally:
  67. unlock.set_unlock_prompt_callback(previous[0])
  68. unlock.set_save_login_prompt_callback(previous[1])
  69. unlock.set_code_prompt_callback(previous[2])
  70. unlock.set_current_session_id(previous[3])