mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
- hermes_cli/web_routers/sessions.py: 14 routes across 3 routers (list_router, search_router, manage_router) mounted at the three original registration points so global route order is preserved exactly. - hermes_cli/web_routers/mcp.py: 11 routes; OAuth flow registry (_mcp_oauth_flows/lock/cap) stays in web_server, reached via new web_deps.LateState live proxies so tests mutating web_server._mcp_oauth_flows keep working. - hermes_cli/web_routers/skills.py: 12 routes across hub_router + router (two original registration points straddle the profiles router include). - hermes_cli/web_routers/tools.py: 12 routes; toolset/terminal catalogs stay in web_server (some are defined after the mount point), reached via LateState. - web_deps.py: add LateState — operation-time proxy for web_server-owned module state (getattr/item/iter/len/contains/context-manager/comparisons). - Handler bodies byte-identical; legacy re-exports keep web_server.<handler> importable for tests. - Verified: ordered route table (method, path) identical to pre-refactor app (291 routes); import smoke; ruff; windows-footguns clean. - test_web_server_sessiondb_eventloop.py: structural AST scan now reads both web_server.py and web_routers/sessions.py (handlers moved; helpers stayed).
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
import ast
|
|
import asyncio
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
from hermes_cli import web_server
|
|
from hermes_cli.web_routers import sessions as web_sessions
|
|
|
|
|
|
TARGET_HANDLERS = {
|
|
"bulk_delete_sessions_endpoint",
|
|
"count_empty_sessions_endpoint",
|
|
"delete_empty_sessions_endpoint",
|
|
"get_session_latest_descendant",
|
|
"get_session_messages",
|
|
"delete_session_endpoint",
|
|
"export_session_endpoint",
|
|
"prune_sessions_endpoint",
|
|
"get_usage_analytics",
|
|
"get_models_analytics",
|
|
}
|
|
|
|
|
|
def _call_name(call: ast.Call) -> str | None:
|
|
if isinstance(call.func, ast.Name):
|
|
return call.func.id
|
|
if isinstance(call.func, ast.Attribute):
|
|
return call.func.attr
|
|
return None
|
|
|
|
|
|
def test_sessiondb_handlers_open_connections_inside_executor_helpers():
|
|
# The session route handlers were extracted to web_routers/sessions.py
|
|
# (wave 2); the analytics handlers and the executor helpers still live in
|
|
# web_server.py — scan both modules' top-level bodies.
|
|
handlers: dict[str, ast.AsyncFunctionDef] = {}
|
|
top_level_helpers: dict[str, ast.FunctionDef] = {}
|
|
for mod in (web_server, web_sessions):
|
|
tree = ast.parse(Path(mod.__file__).read_text(encoding="utf-8"))
|
|
for node in tree.body:
|
|
if isinstance(node, ast.AsyncFunctionDef) and node.name in TARGET_HANDLERS:
|
|
handlers[node.name] = node
|
|
elif isinstance(node, ast.FunctionDef):
|
|
top_level_helpers[node.name] = node
|
|
assert handlers.keys() == TARGET_HANDLERS
|
|
|
|
for name, handler in handlers.items():
|
|
helpers = {
|
|
**top_level_helpers,
|
|
**{
|
|
node.name: node
|
|
for node in handler.body
|
|
if isinstance(node, ast.FunctionDef)
|
|
},
|
|
}
|
|
offloaded = {
|
|
arg.id
|
|
for node in ast.walk(handler)
|
|
if isinstance(node, ast.Call)
|
|
and _call_name(node) == "to_thread"
|
|
for arg in node.args[:1]
|
|
if isinstance(arg, ast.Name)
|
|
}
|
|
db_open_owners = {
|
|
helper_name
|
|
for helper_name, helper in helpers.items()
|
|
if helper_name in offloaded
|
|
and any(
|
|
isinstance(node, ast.Call)
|
|
and _call_name(node) == "_open_session_db_for_profile"
|
|
for node in ast.walk(helper)
|
|
)
|
|
}
|
|
assert db_open_owners, f"{name} does not offload SessionDB open + work"
|
|
|
|
|
|
def test_bulk_delete_sessiondb_work_runs_off_event_loop(monkeypatch):
|
|
loop_thread = threading.get_ident()
|
|
db_threads: list[int] = []
|
|
|
|
class _DB:
|
|
def delete_sessions(self, ids):
|
|
db_threads.append(threading.get_ident())
|
|
assert ids == ["one", "two"]
|
|
return 2
|
|
|
|
def close(self):
|
|
db_threads.append(threading.get_ident())
|
|
|
|
monkeypatch.setattr(web_server, "_open_session_db_for_profile", lambda profile=None: _DB())
|
|
|
|
result = asyncio.run(
|
|
web_server.bulk_delete_sessions_endpoint(
|
|
web_server.BulkDeleteSessions(ids=["one", "two"])
|
|
)
|
|
)
|
|
|
|
assert result == {"ok": True, "deleted": 2}
|
|
assert db_threads
|
|
assert all(thread_id != loop_thread for thread_id in db_threads)
|