9f7f2f28c0
The gateway asked the user questions (approval, clarify, sudo, secret,
vault, MCP setup, the desktop read/act bridges) by emitting a
`<x>.request` EVENT carrying a hand-minted request_id, blocking the
agent thread on a module dict keyed by that id, and exposing a paired
`<x>.respond` METHOD per kind — thirteen pairs, four registries
(`_pending`, `_answers`, `_batch_clarify`, `_EXPIRING_REQUESTS`) and a
per-kind reconnect snapshot (`pending_clarify` / `pending_approval`)
that only two of the thirteen kinds ever got. JSON-RPC already has the
primitive: the server sends a request frame with an id and the client
answers with a response frame bearing the same id.
`tui_gateway/server_requests.py` owns the one mechanism:
send() block the agent thread until the response frame
(`srq-<n>` ids; ints belong to the client)
send_async() fire-and-callback variant (bot relay)
cancel*() withdraw with ONE `request.cancel {id, method, reason}`
event (timeout / interrupt / process exit /
answered elsewhere) instead of per-kind *.expire
open_requests() the still-open frames, replayed by session.resume,
session.activate and session.events.since so a
reconnecting client re-renders every kind, not two
clarify.lock stays a real client→server RPC (locks one batch
answer early); locked answers merge into the final
set even when the closing response carries only the
tail the user answered last
A client that does not implement a method answers -32601 and the agent
fails fast (the old fixed-timeout "unavailable" probes for tour/preview
still work — a wire error IS an answer). Approval: the queue entry's
settle hook withdraws the request when `/approve` from another surface,
a timeout or an interrupt resolves it first, so no window keeps a dead
card. Compute-host children own their waits; the parent mirrors their
open frames for replay and relays `clarify.lock` + response frames.
Clients: `JsonRpcRequestChannel` gains `onRequest` (unhandled → -32601,
dedup by id) and `JsonRpcGatewayClient` re-delivers `open_requests`
from the replay result. Desktop gets `gateway-event/server-requests.ts`
(one handler per method, replacing the request branches of
`input-requests.ts` / `desktop-bridge.ts`) and a `store/server-requests`
registry so every answer site calls `respondToServerRequest(id, result)`
synchronously; the TUI gets `createServerRequestHandler.ts` +
`serverRequestStore.ts`. `gateway-events.json` now pins both halves
(events + server request methods); the two contract tests check both.
Live (real stdio gateway, real `clarify_callback` on the agent thread):
before, `clarify.request` event + `clarify.respond` RPC, batch final
answers lost ('' returned); after, `{"id":"srq-…","method":"clarify"}`
frame, `session.events.since.open_requests` replays it, response frame
`{"answer":"yes"}` reaches the agent, batch lock + final response
merge to `{"q0":"1","q1":"free text"}`.
210 lines
6.5 KiB
Python
210 lines
6.5 KiB
Python
"""Tests for /loop handling in tui_gateway.
|
|
|
|
The TUI routes ``/loop`` through ``command.dispatch`` (same rationale as
|
|
``/goal`` — the CLI handler drives ``_pending_input``, which the slash
|
|
worker has no reader for). State mutations go through the shared
|
|
``dispatch_loop_command``; the per-session notification poller fires due
|
|
wakeups via ``_maybe_fire_tui_loop_tick``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture()
|
|
def hermes_home(tmp_path, monkeypatch):
|
|
home = tmp_path / ".hermes"
|
|
home.mkdir()
|
|
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
|
monkeypatch.setenv("HERMES_HOME", str(home))
|
|
|
|
from hermes_cli import goals
|
|
|
|
goals._DB_CACHE.clear()
|
|
yield home
|
|
goals._DB_CACHE.clear()
|
|
|
|
|
|
@pytest.fixture()
|
|
def server(hermes_home):
|
|
with patch.dict(
|
|
"sys.modules",
|
|
{
|
|
"hermes_cli.env_loader": MagicMock(),
|
|
"hermes_cli.banner": MagicMock(),
|
|
},
|
|
):
|
|
mod = importlib.import_module("tui_gateway.server")
|
|
yield mod
|
|
mod._sessions.clear()
|
|
__import__("tui_gateway.server_requests", fromlist=["x"]).reset_for_tests()
|
|
|
|
|
|
@pytest.fixture()
|
|
def session(server):
|
|
sid = "sid-loop-test"
|
|
session_key = "tui-loop-session-1"
|
|
s = {
|
|
"session_key": session_key,
|
|
"history": [],
|
|
"history_lock": threading.Lock(),
|
|
"history_version": 0,
|
|
"running": False,
|
|
"attached_images": [],
|
|
"cols": 120,
|
|
}
|
|
server._sessions[sid] = s
|
|
return sid, session_key, s
|
|
|
|
|
|
def _call(server, method, **params):
|
|
handler = server._methods[method]
|
|
return handler(1, params)
|
|
|
|
|
|
# ── command.dispatch /loop ────────────────────────────────────────────
|
|
|
|
|
|
def test_loop_bare_shows_status_when_none_set(server, session):
|
|
sid, _, _ = session
|
|
r = _call(server, "command.dispatch", name="loop", arg="", session_id=sid)
|
|
assert r["result"]["type"] == "exec"
|
|
assert "No loop set" in r["result"]["output"]
|
|
|
|
|
|
def test_loop_set_persists(server, session):
|
|
sid, session_key, _ = session
|
|
r = _call(server, "command.dispatch", name="loop", arg="5m check the deploy", session_id=sid)
|
|
result = r["result"]
|
|
assert result["type"] == "exec"
|
|
assert "Loop set" in result["output"]
|
|
|
|
from hermes_cli.loops import LoopManager
|
|
|
|
mgr = LoopManager(session_key)
|
|
assert mgr.state is not None
|
|
assert mgr.state.prompt == "check the deploy"
|
|
assert mgr.state.status == "active"
|
|
assert mgr.state.interval_seconds == 300.0
|
|
|
|
|
|
def test_loop_proactive_alias_resolves(server, session):
|
|
sid, _, _ = session
|
|
r = _call(server, "command.dispatch", name="proactive", arg="5m ping", session_id=sid)
|
|
assert "Loop set" in r["result"]["output"]
|
|
|
|
|
|
def test_loop_pause_resume_stop(server, session):
|
|
sid, session_key, _ = session
|
|
_call(server, "command.dispatch", name="loop", arg="5m poll CI", session_id=sid)
|
|
|
|
r = _call(server, "command.dispatch", name="loop", arg="pause", session_id=sid)
|
|
assert "paused" in r["result"]["output"].lower()
|
|
|
|
r = _call(server, "command.dispatch", name="loop", arg="resume", session_id=sid)
|
|
assert "resumed" in r["result"]["output"].lower()
|
|
|
|
r = _call(server, "command.dispatch", name="loop", arg="stop", session_id=sid)
|
|
assert "stopped" in r["result"]["output"].lower()
|
|
|
|
from hermes_cli.loops import LoopManager
|
|
|
|
assert not LoopManager(session_key).has_loop()
|
|
|
|
|
|
def test_loop_requires_session(server):
|
|
r = _call(server, "command.dispatch", name="loop", arg="5m x", session_id="unknown")
|
|
assert "error" in r
|
|
assert r["error"]["code"] == 4001
|
|
|
|
|
|
# ── idle wakeup driver ────────────────────────────────────────────────
|
|
|
|
|
|
def test_tui_tick_fires_when_idle_and_due(server, session):
|
|
sid, session_key, s = session
|
|
from hermes_cli.loops import LoopManager, save_loop
|
|
|
|
mgr = LoopManager(session_key)
|
|
mgr.set("poll the build", interval_seconds=60)
|
|
mgr.state.next_due_at = time.time() - 1
|
|
save_loop(session_key, mgr.state)
|
|
|
|
fired = {}
|
|
|
|
def fake_submit(rid, sid_, session_, text, **kwargs):
|
|
fired["text"] = text
|
|
|
|
with patch.object(server, "_run_prompt_submit", fake_submit), \
|
|
patch.object(server, "_emit"):
|
|
server._maybe_fire_tui_loop_tick(sid, s)
|
|
|
|
assert "poll the build" in fired.get("text", "")
|
|
assert "[/loop wakeup #1" in fired["text"]
|
|
# Session claimed for the wakeup turn.
|
|
assert s["running"] is True
|
|
|
|
|
|
def test_tui_tick_defers_when_running(server, session):
|
|
sid, session_key, s = session
|
|
from hermes_cli.loops import LoopManager, save_loop
|
|
|
|
mgr = LoopManager(session_key)
|
|
mgr.set("poll", interval_seconds=60)
|
|
mgr.state.next_due_at = time.time() - 1
|
|
save_loop(session_key, mgr.state)
|
|
s["running"] = True
|
|
|
|
with patch.object(server, "_run_prompt_submit") as submit, \
|
|
patch.object(server, "_emit"):
|
|
server._maybe_fire_tui_loop_tick(sid, s)
|
|
|
|
submit.assert_not_called()
|
|
# Tick not consumed — still due for the next poll.
|
|
assert LoopManager(session_key).state.ticks_fired == 0
|
|
|
|
|
|
def test_tui_tick_defers_to_active_goal(server, session):
|
|
sid, session_key, s = session
|
|
from hermes_cli.goals import GoalManager
|
|
from hermes_cli.loops import LoopManager, save_loop
|
|
|
|
GoalManager(session_id=session_key).set("finish the feature")
|
|
mgr = LoopManager(session_key)
|
|
mgr.set("poll", interval_seconds=60)
|
|
mgr.state.next_due_at = time.time() - 1
|
|
save_loop(session_key, mgr.state)
|
|
|
|
with patch.object(server, "_run_prompt_submit") as submit, \
|
|
patch.object(server, "_emit"):
|
|
server._maybe_fire_tui_loop_tick(sid, s)
|
|
|
|
submit.assert_not_called()
|
|
assert s["running"] is False
|
|
|
|
|
|
def test_tui_tick_noop_when_not_due(server, session):
|
|
sid, session_key, s = session
|
|
from hermes_cli.loops import LoopManager
|
|
|
|
mgr = LoopManager(session_key)
|
|
mgr.set("poll", interval_seconds=300)
|
|
# New loops are due immediately; push the wakeup out to model "not due".
|
|
from hermes_cli.loops import save_loop
|
|
mgr.state.next_due_at = time.time() + 300
|
|
save_loop(session_key, mgr.state)
|
|
|
|
with patch.object(server, "_run_prompt_submit") as submit, \
|
|
patch.object(server, "_emit"):
|
|
server._maybe_fire_tui_loop_tick(sid, s)
|
|
|
|
submit.assert_not_called()
|
|
assert s["running"] is False
|