Skip to content

The agent that grows with you

fix(gateway): let browser_vault ask for codes and logins in Desktop chats

emozilla4c3315a6fe38
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.py
index 7bcbcd93be7..360667706ca 100644
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@ -1284,7 +1284,8 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
from gateway.session_cli_turn import run_classic_cli_turn
from gateway.session_secret_prompt import secret_prompt_scope
from gateway.session_sudo_prompt import sudo_prompt_scope
- with sudo_prompt_scope(self, agent), secret_prompt_scope(self, agent):
+ from gateway.session_vault_prompt import vault_prompt_scope
+ with sudo_prompt_scope(self, agent), secret_prompt_scope(self, agent), vault_prompt_scope(self, agent):
result = run_classic_cli_turn(self, lambda: self._run_conversation_with_approval(
agent, agent_history, observed_group_context, persist_msg, persist_ts))
finally:
diff --git a/gateway/session_vault_prompt.py b/gateway/session_vault_prompt.py
new file mode 100644
index 00000000000..90301ed6917
--- /dev/null
+++ b/gateway/session_vault_prompt.py
@@ -0,0 +1,83 @@
+"""The browser vault's prompts (unlock, save login, one-time code) for a local viewer's turn.
+
+Main's TUI sidecar registered the three vault callbacks per session (``tui_gateway/agent_callbacks.py``):
+``browser_vault`` asked the renderer through ``vault.unlock_prompt``, ``vault.save_login`` and ``vault.code``
+server→client requests, and the Desktop and Ink cards answered with ``{value}``. On the session authority no
+callback was registered, so every Desktop chat reported ``prompt_unavailable`` ("this session cannot ask the
+user") and a site's emailed or texted code could not be entered at all. Here the owner sends the same request
+frames (``tui_gateway/contracts/server_requests.py``) to the viewers attached to the session's event stream and
+blocks the agent thread until one answers, the turn is interrupted, or it times out. Passwords and codes are
+never published, journaled or logged: they travel only in the viewer's response frame. The request registry
+itself is ``gateway/session_server_requests.py``.
+"""
+from __future__ import annotations
+
+import contextlib
+import json
+import logging
+
+logger = logging.getLogger(__name__)
+
+# Main's sidecar deadlines (``tui_gateway/agent_callbacks.py``).
+UNLOCK_TIMEOUT_S = 120
+SAVE_LOGIN_TIMEOUT_S = 180
+CODE_TIMEOUT_S = 180
+
+
+def release_unlocks(session_id) -> None:
+ """The session closed: lock the password managers it unlocked, as the sidecar's session end does
+ (``tui_gateway/session_lifecycle.py`` ``_lock_vault_managers``). Other sessions keep theirs."""
+ try:
+ from agent.vault_backends import unlock
+ unlock.release_session(session_id)
+ except Exception: # health: allow BLE001 -- a close must finish even if the lock fails
+ logger.debug("vault manager lock on session close failed", exc_info=True)
+
+
+def vault_prompt_scope(turn, agent):
+ """Install the vault callbacks on this (agent) thread for a local viewer's turn; restore after.
+ Tool workers inherit them through ``tools.thread_context.propagate_context_to_thread``."""
+ from gateway.run_turn_progress import local_viewer_turn
+ if not local_viewer_turn(turn):
+ yield
+ return
+ from agent.vault_backends import unlock
+ authority, session_id, _generation = turn._approval_owner
+
+ def ask(method, params, timeout):
+ from gateway.session_server_requests import ask_viewer
+ live = authority.sessions.get(session_id)
+ if live is None:
+ return ""
+ return ask_viewer(live.event_stream, session_id, method, params, timeout=timeout,
+ cancelled=lambda: bool(getattr(agent, "_interrupt_requested", False)))
+
+ def unlock_prompt(backend, display_name):
+ return ask("vault.unlock_prompt", {"backend": backend, "display_name": display_name}, UNLOCK_TIMEOUT_S)
+
+ def save_login(origin, site):
+ # The card sends identifier + password as one JSON string; it goes straight to the vault store.
+ raw = ask("vault.save_login", {"origin": origin, "site": site}, SAVE_LOGIN_TIMEOUT_S)
+ try:
+ data = json.loads(raw) if raw else None
+ except ValueError:
+ return None
+ return data if isinstance(data, dict) and data.get("password") else None
+
+ def code_prompt(site, hint):
+ return ask("vault.code", {"site": site, "hint": hint}, CODE_TIMEOUT_S)
+
+ previous = (unlock.get_unlock_prompt_callback(), unlock.get_save_login_prompt_callback(),
+ unlock.get_code_prompt_callback(), getattr(unlock._current_session_tls, "sid", None))
+ unlock.set_unlock_prompt_callback(unlock_prompt)
+ unlock.set_save_login_prompt_callback(save_login)
+ unlock.set_code_prompt_callback(code_prompt)
+ unlock.set_current_session_id(session_id) # an unlock made on this turn is released when this session closes
+ try:
+ yield
+ finally:
+ unlock.set_unlock_prompt_callback(previous[0])
+ unlock.set_save_login_prompt_callback(previous[1])
+ unlock.set_code_prompt_callback(previous[2])
+ unlock.set_current_session_id(previous[3])
diff --git a/gateway/session_viewer_close.py b/gateway/session_viewer_close.py
index ac52095970b..7e9e30962b3 100644
--- a/gateway/session_viewer_close.py
+++ b/gateway/session_viewer_close.py
@@ -185,6 +185,8 @@ async def settle_viewer_close(authority, session_id):
if live.subscribers:
return # a viewer reattached during the stamp; reopen_if_reattached reopened the row
platform = (row or {}).get('source') or (live.source.platform.value if live.source else '')
+ from gateway.session_vault_prompt import release_unlocks
+ release_unlocks(session_id)
await _run_close_lifecycle(authority, live, target, platform, close.reason)
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index 61aa0457283..6b706e8320a 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -1136,7 +1136,7 @@ class GatewayAgent:
owner={"type": "session", "session_id": self.session_id})
def _answer_request(self, frame):
- """A gateway server→client request (``sudo``, ``secret``, ``clarify``): main's own modal
+ """A gateway server→client request (``sudo``, ``secret``, the ``vault.*`` prompts, ``clarify``): main's own modal
answers it on this (agent) thread, through the callback HermesCLI registered for the turn; the
answer travels only in the JSON-RPC response frame."""
method = frame["method"]
@@ -1210,6 +1210,37 @@ class GatewayAgent:
callbacks.save_env_value_secure = saver
return captured[0] if captured else ""
+ def _ask_vault_unlock(self, params):
+ """Main's masked password-manager unlock panel, through the callback HermesCLI registered."""
+ from agent.vault_backends.unlock import get_unlock_prompt_callback
+ callback = get_unlock_prompt_callback()
+ if callback is None:
+ return ""
+ return callback(str(params.get("backend") or ""), str(params.get("display_name") or "")) or ""
+
+ def _ask_vault_save_login(self, params):
+ """Main's two-step "save this login" panel. The gateway stores the login in the vault, so the
+ identifier and password go back as one JSON string in the response frame, as the cards send it."""
+ from agent.vault_backends.unlock import get_save_login_prompt_callback
+ callback = get_save_login_prompt_callback()
+ if callback is None:
+ return ""
+ answer = callback(str(params.get("origin") or ""), str(params.get("site") or ""))
+ if not isinstance(answer, dict) or not answer.get("password"):
+ return ""
+ try:
+ return json.dumps({"identifier": str(answer.get("identifier") or ""), "password": str(answer["password"])})
+ finally:
+ answer.clear()
+
+ def _ask_vault_code(self, params):
+ """Main's one-time-code panel, through the callback HermesCLI registered."""
+ from agent.vault_backends.unlock import get_code_prompt_callback
+ callback = get_code_prompt_callback()
+ if callback is None:
+ return ""
+ return callback(str(params.get("site") or ""), str(params.get("hint") or "")) or ""
+
# -- result --------------------------------------------------------------------------------
def _user_row(self, user_message):
staged = self._pending_cli_user_message
@@ -1417,6 +1448,9 @@ _SECRET_LOCK = threading.Lock()
_SERVER_REQUESTS = {
"sudo": GatewayAgent._ask_sudo,
"secret": GatewayAgent._ask_secret,
+ "vault.unlock_prompt": GatewayAgent._ask_vault_unlock,
+ "vault.save_login": GatewayAgent._ask_vault_save_login,
+ "vault.code": GatewayAgent._ask_vault_code,
"clarify": GatewayAgent._ask_clarify,
}
diff --git a/tests/gateway/test_local_viewer_vault_prompt.py b/tests/gateway/test_local_viewer_vault_prompt.py
new file mode 100644
index 00000000000..b436f52543e
--- /dev/null
+++ b/tests/gateway/test_local_viewer_vault_prompt.py
@@ -0,0 +1,237 @@
+"""A local viewer's turn asks for browser-vault input (a site's one-time code, a login to save, a password
+manager's master password) through the Desktop/Ink cards, as main's sidecar did
+(``tui_gateway/agent_callbacks.py``). The canonical owner registered no vault callback, so ``browser_vault``
+reported ``prompt_unavailable`` in every Desktop chat and an emailed sign-in code could not be entered."""
+import json
+import threading
+import time
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.config import Platform
+
+
+class _Peer:
+ def __init__(self):
+ self.frames = []
+
+ def write(self, frame):
+ self.frames.append(frame)
+ return True
+
+ def close(self):
+ pass
+
+
+def _events():
+ from gateway.session_events import SessionEvents
+ events, peer = SessionEvents(), _Peer()
+ events.fanout.attach(peer)
+ return events, peer
+
+
+def _wait_for(predicate, timeout=5.0):
+ deadline = time.monotonic() + timeout
+ while not predicate():
+ assert time.monotonic() < deadline
+ time.sleep(0.01)
+
+
+def _turn(events, platform=Platform.LOCAL, session_id='s1'):
+ authority = SimpleNamespace(sessions={session_id: SimpleNamespace(event_stream=events)})
+ return SimpleNamespace(_approval_owner=(authority, session_id, 1),
+ _ctx=SimpleNamespace(source=SimpleNamespace(platform=platform)))
+
+
+def _answer(peer, method, value):
+ from gateway.session_server_requests import is_response_frame, resolve
+ _wait_for(lambda: any(f.get('method') == method for f in peer.frames))
+ request = next(f for f in peer.frames if f.get('method') == method)
+ from tui_gateway.contracts.registry import SERVER_REQUESTS, validate_params
+ assert validate_params(SERVER_REQUESTS[method], request['params'])[1] is None
+ frame = {'jsonrpc': '2.0', 'id': request['id'], 'result': {'value': value}}
+ assert is_response_frame(frame) and resolve(frame)
+ return request
+
+
[email protected](('platform', 'installed'), [(Platform.LOCAL, True), (Platform.TELEGRAM, False)])
+def test_scope_installs_the_callbacks_only_for_local_viewer_turns_and_restores_them(platform, installed):
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import vault_prompt_scope
+ events, _ = _events()
+ before = (unlock.get_unlock_prompt_callback(), unlock.get_save_login_prompt_callback(),
+ unlock.get_code_prompt_callback(), getattr(unlock._current_session_tls, 'sid', None))
+ with vault_prompt_scope(_turn(events, platform), SimpleNamespace()):
+ got = (unlock.get_unlock_prompt_callback(), unlock.get_save_login_prompt_callback(),
+ unlock.get_code_prompt_callback())
+ assert all(cb is not None for cb in got) is installed
+ if installed:
+ assert unlock.can_prompt_here()
+ assert unlock._current_session_tls.sid == 's1'
+ assert (unlock.get_unlock_prompt_callback(), unlock.get_save_login_prompt_callback(),
+ unlock.get_code_prompt_callback(), getattr(unlock._current_session_tls, 'sid', None)) == before
+
+
+def test_code_request_reaches_the_viewer_and_the_code_never_rides_the_event_stream():
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import vault_prompt_scope
+ events, peer = _events()
+ out = {}
+
+ def run():
+ with vault_prompt_scope(_turn(events), SimpleNamespace()):
+ out['code'] = unlock.get_code_prompt_callback()('www.epicgames.com', '')
+ worker = threading.Thread(target=run)
+ worker.start()
+ request = _answer(peer, 'vault.code', '954145')
+ worker.join(5)
+ assert request['params'] == {'session_id': 's1', 'site': 'www.epicgames.com', 'hint': ''}
+ assert out['code'] == '954145'
+ assert not any('954145' in repr(f) for f in peer.frames)
+
+
+def test_tool_worker_threads_inherit_the_code_prompt():
+ """browser_vault runs on a tool worker when calls run in parallel; the callbacks must reach it."""
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import vault_prompt_scope
+ from tools.thread_context import propagate_context_to_thread
+ events, _ = _events()
+ seen = {}
+ with vault_prompt_scope(_turn(events), SimpleNamespace()):
+ worker = threading.Thread(target=propagate_context_to_thread(
+ lambda: seen.update(cb=unlock.get_code_prompt_callback(), can=unlock.can_prompt_here())))
+ worker.start()
+ worker.join(5)
+ assert seen['cb'] is not None and seen['can'] is True
+
+
[email protected](('value', 'expected'), [
+ (json.dumps({'identifier': '[email protected]', 'password': 'pw'}), {'identifier': '[email protected]', 'password': 'pw'}),
+ ('not json', None),
+ (json.dumps({'identifier': '[email protected]'}), None),
+ ('', None),
+])
+def test_save_login_parses_the_cards_json_answer(value, expected):
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import vault_prompt_scope
+ events, peer = _events()
+ out = {}
+
+ def run():
+ with vault_prompt_scope(_turn(events), SimpleNamespace()):
+ out['answer'] = unlock.get_save_login_prompt_callback()('https://example.com', 'example.com')
+ worker = threading.Thread(target=run)
+ worker.start()
+ _answer(peer, 'vault.save_login', value)
+ worker.join(5)
+ assert out['answer'] == expected
+
+
+def test_unlock_prompt_returns_the_master_password():
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import vault_prompt_scope
+ events, peer = _events()
+ out = {}
+
+ def run():
+ with vault_prompt_scope(_turn(events), SimpleNamespace()):
+ out['master'] = unlock.get_unlock_prompt_callback()('bitwarden', 'Bitwarden')
+ worker = threading.Thread(target=run)
+ worker.start()
+ request = _answer(peer, 'vault.unlock_prompt', 'correct horse')
+ worker.join(5)
+ assert request['params']['display_name'] == 'Bitwarden'
+ assert out['master'] == 'correct horse'
+
+
+def test_interrupt_withdraws_the_code_card():
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import vault_prompt_scope
+ events, peer = _events()
+ agent = SimpleNamespace(_interrupt_requested=False)
+ out = {}
+
+ def run():
+ with vault_prompt_scope(_turn(events), agent):
+ out['code'] = unlock.get_code_prompt_callback()('example.com', '')
+ worker = threading.Thread(target=run)
+ worker.start()
+ _wait_for(lambda: any(f.get('method') == 'vault.code' for f in peer.frames))
+ agent._interrupt_requested = True
+ worker.join(5)
+ assert out['code'] == ''
+ _wait_for(lambda: any(f.get('params', {}).get('type') == 'request.cancel' for f in peer.frames))
+
+
+def test_no_attached_viewer_returns_at_once():
+ from agent.vault_backends import unlock
+ from gateway.session_events import SessionEvents
+ from gateway.session_vault_prompt import vault_prompt_scope
+ started = time.monotonic()
+ with vault_prompt_scope(_turn(SessionEvents()), SimpleNamespace()):
+ assert unlock.get_code_prompt_callback()('example.com', '') == ''
+ assert time.monotonic() - started < 2
+
+
+def _enter_code_with_a_fake_page():
+ """browser_vault_enter_code against a page with one code field; returns (tool result, filled JS)."""
+ from unittest.mock import patch
+ from tools import browser_vault_tool
+ controls = [{'index': 0, 'type': 'text', 'name': 'otp', 'label': 'Security code', 'autocomplete': 'one-time-code'}]
+ seen = {}
+
+ def fake_eval(task_id, expr):
+ return {'success': True, 'result': json.dumps(controls) if 'querySelectorAll' in expr
+ else 'https://www.epicgames.com/id/login/mfa'}
+
+ def fake_secret(task_id, expr):
+ seen['expr'] = expr
+ return {'success': True, 'result': json.dumps({'filled': 1})}
+
+ with patch.object(browser_vault_tool, '_focus_bound_origin', lambda *a, **k: None), \
+ patch.object(browser_vault_tool, '_eval_js', side_effect=fake_eval), \
+ patch.object(browser_vault_tool, '_eval_js_secret', side_effect=fake_secret):
+ raw = browser_vault_tool.browser_vault_enter_code(task_id='t')
+ return json.loads(raw), raw, seen.get('expr', '')
+
+
+def test_enter_code_in_a_desktop_chat_asks_the_viewer_and_fills_the_page():
+ """The reported bug: in a Desktop chat browser_vault said "this session cannot ask the user" and an
+ emailed sign-in code could not be entered. Without the scope that is still the answer; with it the
+ code card opens, and the typed code reaches the page but not the tool result or the event stream."""
+ from gateway.session_vault_prompt import vault_prompt_scope
+ out, _, _ = _enter_code_with_a_fake_page()
+ assert out['error_type'] == 'prompt_unavailable'
+
+ events, peer = _events()
+ result = {}
+
+ def run():
+ with vault_prompt_scope(_turn(events), SimpleNamespace()):
+ result['out'], result['raw'], result['expr'] = _enter_code_with_a_fake_page()
+ worker = threading.Thread(target=run)
+ worker.start()
+ _answer(peer, 'vault.code', '954145')
+ worker.join(5)
+ assert result['out']['success'] and result['out']['source'] == 'user'
+ assert '954145' in result['expr']
+ assert '954145' not in result['raw']
+ assert not any('954145' in repr(f) for f in peer.frames)
+
+
+def test_closing_the_session_locks_only_the_managers_it_unlocked():
+ from agent.vault_backends import unlock
+ from gateway.session_vault_prompt import release_unlocks, vault_prompt_scope
+ events, _ = _events()
+ try:
+ with vault_prompt_scope(_turn(events, session_id='s1'), SimpleNamespace()):
+ assert unlock.store_session_token('test-backend-a', 'token-a')
+ with vault_prompt_scope(_turn(events, session_id='s2'), SimpleNamespace()):
+ assert unlock.store_session_token('test-backend-b', 'token-b')
+ release_unlocks('s1')
+ assert not unlock.is_unlocked('test-backend-a')
+ assert unlock.is_unlocked('test-backend-b')
+ finally:
+ unlock.lock('test-backend-a')
+ unlock.lock('test-backend-b')
diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.py
index 856689702d6..84062b0b03a 100644
--- a/tests/hermes_cli/test_gateway_agent.py
+++ b/tests/hermes_cli/test_gateway_agent.py
@@ -1,5 +1,6 @@
"""GatewayAgent drives main's HermesCLI callbacks from a scripted gateway (no live gateway)."""
import asyncio
+import json
import threading
from contextlib import asynccontextmanager
@@ -487,6 +488,34 @@ def test_gateway_sudo_request_opens_mains_sudo_panel_and_replies_with_the_value(
assert replies == [("srq-abc", {"value": "pw-from-panel"})]
[email protected](("method", "params", "setter", "callback", "expected"), [
+ ("vault.code", {"site": "example.com", "hint": ""}, "set_code_prompt_callback",
+ lambda site, hint: "123456", "123456"),
+ ("vault.unlock_prompt", {"backend": "bitwarden", "display_name": "Bitwarden"}, "set_unlock_prompt_callback",
+ lambda backend, name: "master", "master"),
+ ("vault.save_login", {"origin": "https://example.com", "site": "example.com"}, "set_save_login_prompt_callback",
+ lambda origin, site: {"identifier": "[email protected]", "password": "pw"},
+ json.dumps({"identifier": "[email protected]", "password": "pw"})),
+ ("vault.save_login", {"origin": "https://example.com", "site": "example.com"}, "set_save_login_prompt_callback",
+ lambda origin, site: None, ""),
+])
+def test_gateway_vault_requests_open_mains_panels_and_reply_with_the_value(make_agent, method, params, setter,
+ callback, expected):
+ """The gateway's ``vault.*`` server→client requests are answered by main's vault panels (the turn's
+ registered callbacks); a classic CLI viewer no longer leaves them to time out."""
+ from agent.vault_backends import unlock
+ request = {"jsonrpc": "2.0", "id": "srq-vault", "method": method, "params": {"session_id": SID, **params}}
+ agent, _ = make_agent([request, frame("message.complete", {"text": "ok", "outcome": "completed"})])
+ replies = []
+ agent.connection.reply = lambda rid, result: replies.append((rid, result)) or True
+ getattr(unlock, setter)(callback)
+ try:
+ agent.run_conversation("sign in")
+ finally:
+ getattr(unlock, setter)(None)
+ assert replies == [("srq-vault", {"value": expected})]
+
+
def test_connection_reply_sends_a_jsonrpc_response_frame():
sent = []