From 339bd42e2b0ca45acb2d0231d070683f38087cc8 Mon Sep 17 00:00:00 2001 From: Correctover Date: Wed, 25 Nov 2026 08:56:35 +0800 Subject: [PATCH] fix(hitl): warn loudly when hitl_approval is disabled instead of silently skipping the middleware (#8440) * fix(hitl): warn loudly when hitl_approval is disabled instead of silently skipping the middleware Raised in #8431: _insert_hitl_approval_middleware returns silently when hitl_approval is absent or enabled != true, so agents run tools without runtime human approval and nothing in the logs marks it. This keeps the default (no behavior change) but makes the fail-open posture visible: - emit a warning at middleware insertion time, with the exact config keys to flip and a doc pointer - state the disabled-by-default posture explicitly in docs/guide Signed-off-by: DSHCorrectover * test(hitl): unit tests for the disabled-visibility warning (three mount states) - absence of hitl_approval -> warning + no middleware - enabled=false -> warning + no middleware - enabled=true -> middleware mounted, no warning Runs the same assertions as our internal replay (AST guard-uniqueness, mount-point coverage, behavioral stub replay) against the real package; maintainer CI is the authoritative run. Signed-off-by: DSHCorrectover * fix(hitl): report disabled approvals at Gateway startup --------- Signed-off-by: DSHCorrectover Co-authored-by: Willem Jiang --- README.md | 2 ++ backend/app/gateway/app.py | 3 +++ .../tests/test_gateway_lifespan_shutdown.py | 27 +++++++++++++++++++ backend/tests/test_tool_approval.py | 15 +++++++++++ docs/guide/skills-and-tools.md | 4 ++- 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ffcf3520af..237d96f72d 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ DeerFlow is model-agnostic — it works with any LLM that implements the OpenAI- DeerFlow can execute commands and read and write files. It is designed to run in a local trusted environment, and a Gateway admin account is equivalent to code execution on the host. Before you deploy it on a server or expose it to a network, read the full [Security Notice](docs/guide/security.md) and [SECURITY.md](SECURITY.md). +Runtime tool approval is disabled by default. The Gateway warns at startup when it is disabled. See [Skills & Tools](docs/guide/skills-and-tools.md) for configuration. + ## Contributing We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, workflow, and guidelines. diff --git a/backend/app/gateway/app.py b/backend/app/gateway/app.py index 811dbea6f8..347175426b 100644 --- a/backend/app/gateway/app.py +++ b/backend/app/gateway/app.py @@ -672,6 +672,9 @@ async def _gateway_lifespan(app: FastAPI) -> AsyncGenerator[None, None]: ensure_browser_runtime_available(startup_config) logger.info("Configuration loaded successfully") warn_if_auth_disabled_enabled() + hitl_approval = getattr(startup_config, "hitl_approval", None) + if getattr(hitl_approval, "enabled", False) is not True: + logger.warning("hitl_approval is not enabled. Tool calls can execute without runtime human review. Set hitl_approval.enabled to true in config.yaml to enable tool approvals. See docs/guide/skills-and-tools.md.") except Exception as e: error_msg = f"Failed to load configuration during gateway startup: {e}" logger.exception(error_msg) diff --git a/backend/tests/test_gateway_lifespan_shutdown.py b/backend/tests/test_gateway_lifespan_shutdown.py index ed5f1647fc..dbbbedebbf 100644 --- a/backend/tests/test_gateway_lifespan_shutdown.py +++ b/backend/tests/test_gateway_lifespan_shutdown.py @@ -33,6 +33,33 @@ async def _langgraph_runtime_with_scheduler_repositories(app, _startup_config): yield +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("approval_config", "expected_warnings"), + [ + pytest.param({}, 1, id="omitted"), + pytest.param({"hitl_approval": {"enabled": False}}, 1, id="disabled"), + pytest.param({"hitl_approval": {"enabled": True}}, 0, id="enabled"), + ], +) +async def test_lifespan_warns_once_when_tool_approval_is_disabled(approval_config, expected_warnings, caplog): + from app.gateway.app import lifespan + from deerflow.config.app_config import AppConfig + + startup_config = AppConfig( + sandbox={"use": "deerflow.sandbox.local:LocalSandboxProvider"}, + memory={"enabled": False}, + **approval_config, + ) + with ExitStack() as stack: + for patcher in _gateway_lifespan_patches(startup_config): + stack.enter_context(patcher) + stack.enter_context(caplog.at_level(logging.WARNING, logger="app.gateway.app")) + async with lifespan(FastAPI()): + warnings = [record for record in caplog.records if record.name == "app.gateway.app" and record.levelno == logging.WARNING and "hitl_approval is not enabled" in record.getMessage()] + assert len(warnings) == expected_warnings + + def test_enabled_scheduler_start_failure_aborts_gateway_lifespan(): """An enabled scheduler must fail lifespan before channel or request admission.""" from app.gateway.app import lifespan diff --git a/backend/tests/test_tool_approval.py b/backend/tests/test_tool_approval.py index 3c6dfeb013..061f39923a 100644 --- a/backend/tests/test_tool_approval.py +++ b/backend/tests/test_tool_approval.py @@ -36,6 +36,7 @@ from test_hitl_approval_api import ( # noqa: F401 isolated_deer_flow_home, ) +from deerflow.agents.lead_agent.agent import _insert_hitl_approval_middleware from deerflow.approvals.middleware import ToolApprovalMiddleware from deerflow.approvals.policy import action_fingerprint, effective_outcome, resolve_mode, tool_descriptor from deerflow.approvals.reviewer import review_operation @@ -50,6 +51,20 @@ from deerflow.runtime.runs.manager import PersistenceRetryPolicy from deerflow.runtime.runs.store.memory import MemoryRunStore from deerflow.tools.sync import make_sync_tool_wrapper + +@pytest.mark.parametrize("enabled", [False, True]) +def test_agent_assembly_mounts_tool_approval_only_when_enabled(enabled, caplog): + app_config = AppConfig( + sandbox={"use": "deerflow.sandbox.local:LocalSandboxProvider"}, + hitl_approval=HitlApprovalConfig(enabled=enabled), + ) + for _ in range(2): + middlewares = [] + _insert_hitl_approval_middleware(middlewares, [], app_config, supports_resume=True) + assert [type(middleware) for middleware in middlewares] == ([ToolApprovalMiddleware] if enabled else []) + assert "hitl_approval is not enabled" not in caplog.text + + # --- test_tool_approval_call_ids --- diff --git a/docs/guide/skills-and-tools.md b/docs/guide/skills-and-tools.md index d7285bd458..5af58d81a3 100644 --- a/docs/guide/skills-and-tools.md +++ b/docs/guide/skills-and-tools.md @@ -1,6 +1,8 @@ # Skills & Tools -**Runtime tool approvals**: Enable `hitl_approval.enabled` to offer Ask and Auto +**Runtime tool approvals.** HITL approval is **disabled by default**. When it is +disabled, tool calls can execute without runtime human review, and the Gateway +logs a warning at startup. Enable `hitl_approval.enabled` to offer Ask and Auto independently of reasoning mode. Operators can also enable Full access in `hitl_approval.allowed_modes`. Ask presents the complete operation before execution. Auto makes a separate, tool-free review call using