The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): bound terminal pre-exec guards
KoNit-Kc1e749d67905
diff --git a/tests/tools/test_terminal_pre_guard_deadline.py b/tests/tools/test_terminal_pre_guard_deadline.pynew file mode 100644index 00000000000..7dece6eb41f--- /dev/null+++ b/tests/tools/test_terminal_pre_guard_deadline.py@@ -0,0 +1,61 @@+"""Terminal pre-execution guards must not outlive the tool deadline."""++from __future__ import annotations++import time+from types import SimpleNamespace++import tools.terminal_tool as terminal_module+++def _plan(timeout: float = 0.05) -> SimpleNamespace:+ return SimpleNamespace(+ config={},+ env_type="local",+ effective_task_id="pre-guard-deadline-test",+ cwd="/tmp",+ effective_timeout=timeout,+ promoted_from_foreground_timeout=None,+ )+++def test_terminal_tool_bounds_a_wedged_pre_execution_guard(monkeypatch):+ """A stalled supervised-gateway identity probe cannot wedge terminal_tool."""+ monkeypatch.setattr(terminal_module, "_plan_execution", lambda *_a, **_k: _plan())+ monkeypatch.setattr(terminal_module, "_acquire_env", lambda *_a, **_k: object())+ monkeypatch.setattr(terminal_module, "_run_approval_guards", lambda *_a, **_k: terminal_module._ApprovalVerdict())+ monkeypatch.setattr(terminal_module, "_run_foreground", lambda *_a, **_k: "foreground-ran")++ def _wedged_supervised_gateway_probe(*_a, **_k):+ time.sleep(1)++ monkeypatch.setattr(terminal_module, "_pre_exec_block", _wedged_supervised_gateway_probe)++ start = time.monotonic()+ result = terminal_module.terminal_tool("echo ok")+ elapsed = time.monotonic() - start++ assert elapsed < 0.5, f"pre-execution guard wedged terminal_tool for {elapsed:.2f}s"+ assert result == "foreground-ran"+++def test_terminal_tool_runs_normal_pre_execution_guard(monkeypatch):+ """A normal guard result still reaches foreground execution unchanged."""+ monkeypatch.setattr(terminal_module, "_plan_execution", lambda *_a, **_k: _plan())+ monkeypatch.setattr(terminal_module, "_acquire_env", lambda *_a, **_k: object())+ monkeypatch.setattr(terminal_module, "_run_approval_guards", lambda *_a, **_k: terminal_module._ApprovalVerdict())+ monkeypatch.setattr(terminal_module, "_pre_exec_block", lambda *_a, **_k: None)+ monkeypatch.setattr(terminal_module, "_run_foreground", lambda *_a, **_k: "foreground-ran")++ assert terminal_module.terminal_tool("echo ok") == "foreground-ran"+++def test_terminal_tool_preserves_pre_execution_rejection(monkeypatch):+ """A completed guard rejection still returns its original tool result."""+ monkeypatch.setattr(terminal_module, "_plan_execution", lambda *_a, **_k: _plan())+ monkeypatch.setattr(terminal_module, "_acquire_env", lambda *_a, **_k: object())+ monkeypatch.setattr(terminal_module, "_pre_exec_block", lambda *_a, **_k: (_ for _ in ()).throw(+ terminal_module._Rejected('{"status":"blocked"}')+ ))++ assert terminal_module.terminal_tool("echo ok") == '{"status":"blocked"}'diff --git a/tools/terminal_tool.py b/tools/terminal_tool.pyindex 4e9b7fc75ac..be675457ecc 100644--- a/tools/terminal_tool.py+++ b/tools/terminal_tool.py@@ -1222,7 +1222,25 @@ def terminal_tool(session_key = get_current_session_key(default="") or (task_id or "")- _pre_exec_block(command, env=env, env_type=env_type, cwd=cwd, workdir=workdir, session_key=session_key)+ # A supervised-gateway identity check can enter a kernel-level psutil+ # query. Put the whole pre-execution chain behind the command's wall+ # clock deadline so that a wedged probe cannot hold a cron run forever.+ # A timed-out guard fails open: its worker is abandoned, while ordinary+ # guard rejections and exceptions keep their original behavior.+ from agent.deadline import run_bounded_sync++ bounded_guard = run_bounded_sync(+ lambda: _pre_exec_block(+ command, env=env, env_type=env_type, cwd=cwd, workdir=workdir, session_key=session_key,+ ),+ plan.effective_timeout,+ label="terminal.pre-exec-guard",+ )+ if bounded_guard.timed_out:+ logger.warning(+ "Terminal pre-execution guard timed out after %ss; continuing fail-open",+ plan.effective_timeout,+ )# Pre-exec security checks (tirith + dangerous command detection);# force=True means the user already confirmed.verdict = _run_approval_guards(command, env_type, plan.config, force=force)