2026-04-08 15:20:24 +02:00
|
|
|
"""ACP permission bridging for Hermes dangerous-command approvals."""
|
2026-03-14 00:09:05 -07:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import logging
|
|
|
|
|
from concurrent.futures import TimeoutError as FutureTimeout
|
2026-04-08 15:20:24 +02:00
|
|
|
from itertools import count
|
chore: remove ~100 unused imports across 55 files (#3016)
Automated cleanup via pyflakes + autoflake with manual review.
Changes:
- Removed unused stdlib imports (os, sys, json, pathlib.Path, etc.)
- Removed unused typing imports (List, Dict, Any, Optional, Tuple, Set, etc.)
- Removed unused internal imports (hermes_cli.auth, hermes_cli.config, etc.)
- Fixed cli.py: removed 8 shadowed banner imports (imported from hermes_cli.banner
then immediately redefined locally — only build_welcome_banner is actually used)
- Added noqa comments to imports that appear unused but serve a purpose:
- Re-exports (gateway/session.py SessionResetPolicy, tools/terminal_tool.py
is_interrupted/_interrupt_event)
- SDK presence checks in try/except (daytona, fal_client, discord)
- Test mock targets (auxiliary_client.py Path, mcp_config.py get_hermes_home)
Zero behavioral changes. Full test suite passes (6162/6162, 2 pre-existing
streaming test failures unrelated to this change).
2026-03-25 15:02:03 -07:00
|
|
|
from typing import Callable
|
2026-03-14 00:09:05 -07:00
|
|
|
|
|
|
|
|
from acp.schema import (
|
|
|
|
|
AllowedOutcome,
|
|
|
|
|
PermissionOption,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-04-08 15:20:24 +02:00
|
|
|
# Maps ACP permission option ids to Hermes approval result strings.
|
|
|
|
|
# Option ids are stable across both the ``allow_permanent=True`` and
|
|
|
|
|
# ``allow_permanent=False`` paths even though the option list differs.
|
|
|
|
|
_OPTION_ID_TO_HERMES = {
|
2026-03-14 00:09:05 -07:00
|
|
|
"allow_once": "once",
|
2026-04-08 15:20:24 +02:00
|
|
|
"allow_session": "session",
|
2026-03-14 00:09:05 -07:00
|
|
|
"allow_always": "always",
|
2026-04-08 15:20:24 +02:00
|
|
|
"deny": "deny",
|
2026-05-15 23:19:20 +01:00
|
|
|
"deny_always": "deny",
|
2026-03-14 00:09:05 -07:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 15:20:24 +02:00
|
|
|
_PERMISSION_REQUEST_IDS = count(1)
|
|
|
|
|
|
|
|
|
|
|
2026-05-15 23:19:20 +01:00
|
|
|
def _permission_option_supports_kind(kind: str) -> bool:
|
|
|
|
|
"""Return whether the installed ACP SDK accepts a permission option kind."""
|
|
|
|
|
try:
|
|
|
|
|
PermissionOption(option_id="__probe__", kind=kind, name="probe")
|
|
|
|
|
except Exception:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2026-04-08 15:20:24 +02:00
|
|
|
def _build_permission_options(*, allow_permanent: bool) -> list[PermissionOption]:
|
|
|
|
|
"""Return ACP options that match Hermes approval semantics."""
|
|
|
|
|
options = [
|
|
|
|
|
PermissionOption(option_id="allow_once", kind="allow_once", name="Allow once"),
|
|
|
|
|
PermissionOption(
|
|
|
|
|
option_id="allow_session",
|
|
|
|
|
# ACP has no session-scoped kind, so use the closest persistent
|
|
|
|
|
# hint while keeping Hermes semantics in the option id.
|
|
|
|
|
kind="allow_always",
|
|
|
|
|
name="Allow for session",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
if allow_permanent:
|
|
|
|
|
options.append(
|
|
|
|
|
PermissionOption(
|
|
|
|
|
option_id="allow_always",
|
|
|
|
|
kind="allow_always",
|
|
|
|
|
name="Allow always",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
options.append(PermissionOption(option_id="deny", kind="reject_once", name="Deny"))
|
2026-05-15 23:19:20 +01:00
|
|
|
if _permission_option_supports_kind("reject_always"):
|
|
|
|
|
options.append(
|
|
|
|
|
PermissionOption(
|
|
|
|
|
option_id="deny_always",
|
|
|
|
|
kind="reject_always",
|
|
|
|
|
name="Deny always",
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-04-08 15:20:24 +02:00
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_permission_tool_call(command: str, description: str):
|
|
|
|
|
"""Return the ACP tool-call update attached to a permission request.
|
|
|
|
|
|
|
|
|
|
``request_permission`` expects a ``ToolCallUpdate`` payload — produced
|
|
|
|
|
by ``_acp.update_tool_call`` — not a ``ToolCallStart``. Each request
|
|
|
|
|
gets a unique ``perm-check-N`` id so concurrent requests don't collide.
|
|
|
|
|
"""
|
|
|
|
|
import acp as _acp
|
|
|
|
|
|
|
|
|
|
tool_call_id = f"perm-check-{next(_PERMISSION_REQUEST_IDS)}"
|
2026-05-15 23:19:20 +01:00
|
|
|
title = f"{description}: {command}" if description else command
|
|
|
|
|
content_text = f"{description}\n$ {command}" if description else f"$ {command}"
|
2026-04-08 15:20:24 +02:00
|
|
|
return _acp.update_tool_call(
|
|
|
|
|
tool_call_id,
|
2026-05-15 23:19:20 +01:00
|
|
|
title=title,
|
2026-04-08 15:20:24 +02:00
|
|
|
kind="execute",
|
|
|
|
|
status="pending",
|
2026-05-15 23:19:20 +01:00
|
|
|
content=[_acp.tool_content(_acp.text_block(content_text))],
|
2026-04-08 15:20:24 +02:00
|
|
|
raw_input={"command": command, "description": description},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _map_outcome_to_hermes(outcome: object, *, allowed_option_ids: set[str]) -> str:
|
|
|
|
|
"""Map an ACP permission outcome into Hermes approval strings."""
|
|
|
|
|
if not isinstance(outcome, AllowedOutcome):
|
|
|
|
|
return "deny"
|
|
|
|
|
|
|
|
|
|
option_id = outcome.option_id
|
|
|
|
|
if option_id not in allowed_option_ids:
|
|
|
|
|
logger.warning("Permission request returned unknown option_id: %s", option_id)
|
|
|
|
|
return "deny"
|
|
|
|
|
return _OPTION_ID_TO_HERMES.get(option_id, "deny")
|
|
|
|
|
|
2026-03-14 00:09:05 -07:00
|
|
|
|
|
|
|
|
def make_approval_callback(
|
|
|
|
|
request_permission_fn: Callable,
|
|
|
|
|
loop: asyncio.AbstractEventLoop,
|
|
|
|
|
session_id: str,
|
|
|
|
|
timeout: float = 60.0,
|
2026-04-08 15:20:24 +02:00
|
|
|
) -> Callable[..., str]:
|
2026-03-14 00:09:05 -07:00
|
|
|
"""
|
2026-04-08 15:20:24 +02:00
|
|
|
Return a Hermes-compatible approval callback that bridges to ACP.
|
|
|
|
|
|
|
|
|
|
The callback accepts ``command`` and ``description`` plus optional
|
|
|
|
|
keyword arguments such as ``allow_permanent`` used by
|
|
|
|
|
``tools.approval.prompt_dangerous_approval()``.
|
2026-03-14 00:09:05 -07:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
request_permission_fn: The ACP connection's ``request_permission`` coroutine.
|
|
|
|
|
loop: The event loop on which the ACP connection lives.
|
|
|
|
|
session_id: Current ACP session id.
|
|
|
|
|
timeout: Seconds to wait for a response before auto-denying.
|
|
|
|
|
"""
|
|
|
|
|
|
2026-04-08 15:20:24 +02:00
|
|
|
def _callback(
|
|
|
|
|
command: str,
|
|
|
|
|
description: str,
|
|
|
|
|
*,
|
|
|
|
|
allow_permanent: bool = True,
|
|
|
|
|
**_: object,
|
|
|
|
|
) -> str:
|
fix(async): close unscheduled coroutines in all threadsafe bridges (#26584)
Wraps every sync->async coroutine-scheduling site in the codebase with a
new agent.async_utils.safe_schedule_threadsafe() helper that closes the
coroutine on scheduling failure (closed loop, shutdown race, etc.)
instead of leaking it as 'coroutine was never awaited' RuntimeWarnings
plus reference leaks.
22 production call sites migrated across the codebase:
- acp_adapter/events.py, acp_adapter/permissions.py
- agent/lsp/manager.py
- cron/scheduler.py (media + text delivery paths)
- gateway/platforms/feishu.py (5 sites, via existing _submit_on_loop helper
which now delegates to safe_schedule_threadsafe)
- gateway/run.py (10 sites: telegram rename, agent:step hook, status
callback, interim+bg-review, clarify send, exec-approval button+text,
temp-bubble cleanup, channel-directory refresh)
- plugins/memory/hindsight, plugins/platforms/google_chat
- tools/browser_supervisor.py (3), browser_cdp_tool.py,
computer_use/cua_backend.py, slash_confirm.py
- tools/environments/modal.py (_AsyncWorker)
- tools/mcp_tool.py (2 + 8 _run_on_mcp_loop callers converted to
factory-style so the coroutine is never constructed on a dead loop)
- tui_gateway/ws.py
Tests: new tests/agent/test_async_utils.py covers helper behavior under
live loop, dead loop, None loop, and scheduling exceptions. Regression
tests added at three PR-original sites (acp events, acp permissions,
mcp loop runner) mirroring contributor's intent.
Live-tested end-to-end:
- Helper stress test: 1500 schedules across live/dead/race scenarios,
zero leaked coroutines
- Race exercised: 5000 schedules with loop killed mid-flight, 100 ok /
4900 None returns, zero leaks
- hermes chat -q with terminal tool call (exercises step_callback bridge)
- MCP probe against failing subprocess servers + factory path
- Real gateway daemon boot + SIGINT shutdown across multiple platform
adapter inits
- WSTransport 100 live + 50 dead-loop writes
- Cron delivery path live + dead loop
Salvages PR #2657 — adopts contributor's intent over a much wider site
list and a single centralized helper instead of inline try/except at
each site. 3 of the original PR's 6 sites no longer exist on main
(environments/patches.py deleted, DingTalk refactored to native async);
the equivalent fix lives in tools/environments/modal.py instead.
Co-authored-by: JithendraNara <jithendranaidunara@gmail.com>
2026-05-15 14:00:01 -07:00
|
|
|
from agent.async_utils import safe_schedule_threadsafe
|
|
|
|
|
|
2026-04-08 15:20:24 +02:00
|
|
|
options = _build_permission_options(allow_permanent=allow_permanent)
|
2026-03-14 00:09:05 -07:00
|
|
|
|
fix(async): close unscheduled coroutines in all threadsafe bridges (#26584)
Wraps every sync->async coroutine-scheduling site in the codebase with a
new agent.async_utils.safe_schedule_threadsafe() helper that closes the
coroutine on scheduling failure (closed loop, shutdown race, etc.)
instead of leaking it as 'coroutine was never awaited' RuntimeWarnings
plus reference leaks.
22 production call sites migrated across the codebase:
- acp_adapter/events.py, acp_adapter/permissions.py
- agent/lsp/manager.py
- cron/scheduler.py (media + text delivery paths)
- gateway/platforms/feishu.py (5 sites, via existing _submit_on_loop helper
which now delegates to safe_schedule_threadsafe)
- gateway/run.py (10 sites: telegram rename, agent:step hook, status
callback, interim+bg-review, clarify send, exec-approval button+text,
temp-bubble cleanup, channel-directory refresh)
- plugins/memory/hindsight, plugins/platforms/google_chat
- tools/browser_supervisor.py (3), browser_cdp_tool.py,
computer_use/cua_backend.py, slash_confirm.py
- tools/environments/modal.py (_AsyncWorker)
- tools/mcp_tool.py (2 + 8 _run_on_mcp_loop callers converted to
factory-style so the coroutine is never constructed on a dead loop)
- tui_gateway/ws.py
Tests: new tests/agent/test_async_utils.py covers helper behavior under
live loop, dead loop, None loop, and scheduling exceptions. Regression
tests added at three PR-original sites (acp events, acp permissions,
mcp loop runner) mirroring contributor's intent.
Live-tested end-to-end:
- Helper stress test: 1500 schedules across live/dead/race scenarios,
zero leaked coroutines
- Race exercised: 5000 schedules with loop killed mid-flight, 100 ok /
4900 None returns, zero leaks
- hermes chat -q with terminal tool call (exercises step_callback bridge)
- MCP probe against failing subprocess servers + factory path
- Real gateway daemon boot + SIGINT shutdown across multiple platform
adapter inits
- WSTransport 100 live + 50 dead-loop writes
- Cron delivery path live + dead loop
Salvages PR #2657 — adopts contributor's intent over a much wider site
list and a single centralized helper instead of inline try/except at
each site. 3 of the original PR's 6 sites no longer exist on main
(environments/patches.py deleted, DingTalk refactored to native async);
the equivalent fix lives in tools/environments/modal.py instead.
Co-authored-by: JithendraNara <jithendranaidunara@gmail.com>
2026-05-15 14:00:01 -07:00
|
|
|
tool_call = _build_permission_tool_call(command, description)
|
|
|
|
|
coro = request_permission_fn(
|
|
|
|
|
session_id=session_id,
|
|
|
|
|
tool_call=tool_call,
|
|
|
|
|
options=options,
|
|
|
|
|
)
|
|
|
|
|
future = safe_schedule_threadsafe(
|
|
|
|
|
coro, loop,
|
|
|
|
|
logger=logger,
|
|
|
|
|
log_message="Permission request: failed to schedule on loop",
|
|
|
|
|
)
|
|
|
|
|
if future is None:
|
|
|
|
|
return "deny"
|
|
|
|
|
|
2026-03-14 00:09:05 -07:00
|
|
|
try:
|
|
|
|
|
response = future.result(timeout=timeout)
|
|
|
|
|
except (FutureTimeout, Exception) as exc:
|
fix(async): close unscheduled coroutines in all threadsafe bridges (#26584)
Wraps every sync->async coroutine-scheduling site in the codebase with a
new agent.async_utils.safe_schedule_threadsafe() helper that closes the
coroutine on scheduling failure (closed loop, shutdown race, etc.)
instead of leaking it as 'coroutine was never awaited' RuntimeWarnings
plus reference leaks.
22 production call sites migrated across the codebase:
- acp_adapter/events.py, acp_adapter/permissions.py
- agent/lsp/manager.py
- cron/scheduler.py (media + text delivery paths)
- gateway/platforms/feishu.py (5 sites, via existing _submit_on_loop helper
which now delegates to safe_schedule_threadsafe)
- gateway/run.py (10 sites: telegram rename, agent:step hook, status
callback, interim+bg-review, clarify send, exec-approval button+text,
temp-bubble cleanup, channel-directory refresh)
- plugins/memory/hindsight, plugins/platforms/google_chat
- tools/browser_supervisor.py (3), browser_cdp_tool.py,
computer_use/cua_backend.py, slash_confirm.py
- tools/environments/modal.py (_AsyncWorker)
- tools/mcp_tool.py (2 + 8 _run_on_mcp_loop callers converted to
factory-style so the coroutine is never constructed on a dead loop)
- tui_gateway/ws.py
Tests: new tests/agent/test_async_utils.py covers helper behavior under
live loop, dead loop, None loop, and scheduling exceptions. Regression
tests added at three PR-original sites (acp events, acp permissions,
mcp loop runner) mirroring contributor's intent.
Live-tested end-to-end:
- Helper stress test: 1500 schedules across live/dead/race scenarios,
zero leaked coroutines
- Race exercised: 5000 schedules with loop killed mid-flight, 100 ok /
4900 None returns, zero leaks
- hermes chat -q with terminal tool call (exercises step_callback bridge)
- MCP probe against failing subprocess servers + factory path
- Real gateway daemon boot + SIGINT shutdown across multiple platform
adapter inits
- WSTransport 100 live + 50 dead-loop writes
- Cron delivery path live + dead loop
Salvages PR #2657 — adopts contributor's intent over a much wider site
list and a single centralized helper instead of inline try/except at
each site. 3 of the original PR's 6 sites no longer exist on main
(environments/patches.py deleted, DingTalk refactored to native async);
the equivalent fix lives in tools/environments/modal.py instead.
Co-authored-by: JithendraNara <jithendranaidunara@gmail.com>
2026-05-15 14:00:01 -07:00
|
|
|
future.cancel()
|
2026-03-14 00:09:05 -07:00
|
|
|
logger.warning("Permission request timed out or failed: %s", exc)
|
|
|
|
|
return "deny"
|
|
|
|
|
|
2026-04-21 15:22:58 +05:30
|
|
|
if response is None:
|
|
|
|
|
return "deny"
|
|
|
|
|
|
2026-04-08 15:20:24 +02:00
|
|
|
allowed_option_ids = {option.option_id for option in options}
|
|
|
|
|
return _map_outcome_to_hermes(
|
|
|
|
|
response.outcome,
|
|
|
|
|
allowed_option_ids=allowed_option_ids,
|
|
|
|
|
)
|
2026-03-14 00:09:05 -07:00
|
|
|
|
|
|
|
|
return _callback
|