Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions acp_adapter/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def make_approval_callback(
request_permission_fn: Callable,
loop: asyncio.AbstractEventLoop,
session_id: str,
timeout: float = 60.0,
timeout: float | None = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extend the configured timeout to the ACP edit approval requester as well. Current main constructs that independent bridge in acp_adapter/server.py:1425; it still defaults to 60 seconds in acp_adapter/edit_approval.py:290, so edits would retain the reported bug.

) -> Callable[[str, str], str]:
"""
Return a hermes-compatible ``approval_callback(command, description) -> str``
Expand All @@ -38,8 +38,17 @@ def make_approval_callback(
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.
If None, use Hermes approvals.timeout config. If <= 0, wait indefinitely.
"""

if timeout is None:
try:
from tools.approval import _get_approval_timeout

timeout = float(_get_approval_timeout())
except Exception:
timeout = 60.0

def _callback(command: str, description: str) -> str:
options = [
PermissionOption(option_id="allow_once", kind="allow_once", name="Allow once"),
Expand All @@ -58,7 +67,10 @@ def _callback(command: str, description: str) -> str:

try:
future = asyncio.run_coroutine_threadsafe(coro, loop)
response = future.result(timeout=timeout)
if timeout is not None and timeout <= 0:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An indefinite wait for timeout <= 0 conflicts with the current CLI, which immediately expires a non-positive deadline (cli.py:11705, :11733), and with the documented fail-closed timeout behavior. Define one shared policy before making ACP special-case this value.

response = future.result()
else:
response = future.result(timeout=timeout)
except (FutureTimeout, Exception) as exc:
logger.warning("Permission request timed out or failed: %s", exc)
return "deny"
Expand Down
2 changes: 1 addition & 1 deletion cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9071,7 +9071,7 @@ def _approval_callback(self, command: str, description: str,
import time as _time

with self._approval_lock:
timeout = 60
timeout = int((CLI_CONFIG.get("approvals", {}) or {}).get("timeout", 60) or 60)
response_queue = queue.Queue()

self._approval_state = {
Expand Down
29 changes: 29 additions & 0 deletions tests/acp/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,32 @@ def test_approval_none_response_returns_deny(self):
result = cb("echo hi", "demo")

assert result == "deny"

def test_approval_uses_hermes_config_timeout_when_not_provided(self):
"""ACP approvals should inherit Hermes approvals.timeout when timeout=None."""
loop = MagicMock(spec=asyncio.AbstractEventLoop)
mock_rp = MagicMock(name="request_permission")
future = MagicMock(spec=Future)
future.result.return_value = _make_response(AllowedOutcome(option_id="allow_once", outcome="selected"))

with patch("acp_adapter.permissions.asyncio.run_coroutine_threadsafe", return_value=future), \
patch("tools.approval._get_approval_timeout", return_value=86400):
cb = make_approval_callback(mock_rp, loop, session_id="s1")
result = cb("rm -rf /", "dangerous")

future.result.assert_called_once_with(timeout=86400.0)
assert result == "once"

def test_non_positive_timeout_waits_indefinitely(self):
"""A non-positive timeout should call Future.result() with no timeout."""
loop = MagicMock(spec=asyncio.AbstractEventLoop)
mock_rp = MagicMock(name="request_permission")
future = MagicMock(spec=Future)
future.result.return_value = _make_response(AllowedOutcome(option_id="allow_once", outcome="selected"))

with patch("acp_adapter.permissions.asyncio.run_coroutine_threadsafe", return_value=future):
cb = make_approval_callback(mock_rp, loop, session_id="s1", timeout=0)
result = cb("rm -rf /", "dangerous")

future.result.assert_called_once_with()
assert result == "once"
22 changes: 22 additions & 0 deletions tests/cli/test_cli_approval_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ def _run_callback():
thread.join(timeout=2)
assert result["value"] == "deny"


def test_approval_callback_uses_configured_timeout(self):
cli = _make_cli_stub()
command = "rm -f /tmp/test-file"
captured = {}

def _fake_get(self, timeout=None):
captured["poll_timeout"] = timeout
captured["remaining_before_expire"] = cli._approval_deadline - time.monotonic()
cli._approval_deadline = time.monotonic() - 1
raise queue.Empty

with patch.object(cli_module, "CLI_CONFIG", {"approvals": {"timeout": 86400}}), \
patch.object(queue.Queue, "get", _fake_get), \
patch.object(cli_module, "_cprint"):
result = cli._approval_callback(command, "delete temp file")

assert result == "deny"
assert captured["poll_timeout"] == 1
assert captured["remaining_before_expire"] > 86000
assert cli._approval_deadline == 0

def test_handle_approval_selection_view_expands_in_place(self):
cli = _make_cli_stub()
cli._approval_state = {
Expand Down