Skip to content

Self-improving AI Employees for everyone

fix(approvals): Ask first holds for agents outside AgentWorkflow too

trohitgf51cb6f2cdec
diff --git a/server/services/approvals/tool_calls.py b/server/services/approvals/tool_calls.py
index 76680626..4c1006ac 100644
--- a/server/services/approvals/tool_calls.py
+++ b/server/services/approvals/tool_calls.py
@@ -17,6 +17,12 @@ A call that comes back to run because the owner pressed Send carries
``approval_execution`` (services/approvals/execution.py): it runs only when
its row is the one being sent, and is never held again.
+**Agents outside AgentWorkflow** (rlm, claude_code, vertex, or every agent
+with AgentWorkflow off) reach plugins through ``services/handlers/tools.py``,
+which calls :func:`check_in_process`. Such a call cannot wait as a draft, so
+while the workflow asks first a call that sends is refused (a restricted
+tool runs restricted); no row is written.
+
**At most once.** Temporal runs a tool activity again only when an attempt
broke off (the worker stopped, a timeout): a failure the node reports comes
back as a result and is not retried. The attempt that broke off may have
@@ -142,6 +148,16 @@ async def _execution_allowed(database: Any, context: Mapping[str, Any]) -> bool:
return bool(row is not None and row.status == "sending" and row.claim_token and row.claim_token == execution.get("claim_token"))
+async def _asks_first(database: Any, workflow_id: Any) -> Optional[bool]:
+ """The workflow's Ask first rule (None: no rule). True when it cannot be
+ read: hold or refuse the call rather than send it unasked."""
+ try:
+ return await rules.ask_first(database, workflow_id)
+ except Exception:
+ logger.warning("Could not read the Ask first rule; acting as if it is on", workflow_id=workflow_id, exc_info=True)
+ return True
+
+
async def check(context: Mapping[str, Any], node_cls: Any) -> Checked:
"""What to do with a tool call before it runs (see the module docstring)."""
from services.plugin.approval import approval_spec
@@ -166,12 +182,7 @@ async def check(context: Mapping[str, Any], node_cls: Any) -> Checked:
if not spec.sends(node_data):
return RUN
database = _database()
- try:
- asking = await rules.ask_first(database, context.get("workflow_id"))
- except Exception:
- # The rule could not be read: hold it rather than send unasked.
- logger.warning("Could not read the Ask first rule; holding the call", workflow_id=context.get("workflow_id"), exc_info=True)
- asking = True
+ asking = await _asks_first(database, context.get("workflow_id"))
if asking is None:
return RUN
if not asking:
@@ -196,6 +207,31 @@ async def check(context: Mapping[str, Any], node_cls: Any) -> Checked:
return Checked(result=_held(row, node_cls, context))
+async def check_in_process(
+ context: Mapping[str, Any],
+ node_cls: Any,
+ params: Mapping[str, Any],
+ tool_args: Mapping[str, Any],
+) -> Checked:
+ """:func:`check` for an agent's tool call that reaches the plugin in
+ process (see the module docstring). ``result`` is the flat answer the
+ model reads, as ``execute_as_tool`` gives one."""
+ from services.plugin.approval import approval_spec
+
+ spec = approval_spec(node_cls)
+ if spec is None or not context.get("parent_node_id"):
+ return RUN
+ if not spec.sends({**dict(params or {}), **dict(tool_args or {})}):
+ return RUN
+ if not await _asks_first(_database(), context.get("workflow_id")):
+ return RUN
+ if spec.restrict_while_asking:
+ restricted = {**dict(params or {}), **dict(spec.restrict_while_asking)}
+ args = {k: v for k, v in dict(tool_args or {}).items() if k not in spec.restrict_while_asking}
+ return Checked(node_data=restricted, tool_args=args)
+ return Checked(result={"status": "not_run", "message": REFUSED_MESSAGE.format(channel=spec.channel)})
+
+
def _row_fields(context: Mapping[str, Any], node_cls: Any, spec: Any, node_data: Mapping[str, Any]) -> Dict[str, Any]:
args = _clean_args(node_cls, context.get("tool_args") or {})
data = _clean_node_data(node_cls, node_data, context.get("tool_args") or {})
@@ -335,6 +371,7 @@ __all__ = [
"RUN",
"call_key",
"check",
+ "check_in_process",
"claim_token",
"hold",
"is_agent_tool_call",
diff --git a/server/services/employees/builder.py b/server/services/employees/builder.py
index 5729bf78..2fbf17f4 100644
--- a/server/services/employees/builder.py
+++ b/server/services/employees/builder.py
@@ -12,11 +12,13 @@ One employee is one workflow:
trigger's output;
- tools on the agent: web search, a checklist (writeTodos), a clock and a
canvas (what the agent puts there shows in Home's Workspace), always; the
- apps' tools, minus anything that sends or spends while "ask me first" is
- on (a tool with ``ask_first_params``, the browser, stays in its read-only
- form instead); Memory when the owner keeps memory across chats. Which
- tools and skills a hire may have is policy.py's rule, the same one that
- governs what is added to an employee later;
+ apps' tools: one whose plugin declares an approval spec is attached as it
+ is (while "ask me first" is on, each call that sends waits as a draft and
+ the browser runs read-only); one without a spec that sends or spends is
+ left out while "ask me first" is on, unless the app declares
+ ``ask_first_params`` that make it safe; Memory when the owner keeps memory
+ across chats. Which tools and skills a hire may have is policy.py's rule,
+ the same one that governs what is added to an employee later;
- the owner's skill library (Settings > Skills): every skill that is on, on
one Skills node (masterSkill), with its text copied in, so a later edit to
the library never changes an employee already hired;
@@ -645,11 +647,12 @@ def _plan_app_tools(inputs: BuildInputs, trigger: _TriggerPlan, warnings: List[s
"""The apps' tools the agent gets, decided before its instructions are
written so they can mention them.
- policy.check_tool decides: under ``ask first`` a tool that can send or
- spend is left out, unless the app declares ``ask_first_params`` that
- make it safe: then it is attached with those applied (the browser can
- read, and hands any page change to the owner). An app the owner has
- not connected yet keeps its tools; the card asks them to connect it."""
+ policy.check_tool decides: a tool whose plugin declares an approval
+ spec is attached as it is (each call that sends is decided when it is
+ made). Under ``ask first`` a tool without a spec that can send or spend
+ is left out, unless the app declares ``ask_first_params`` that make it
+ safe: then it is attached with those applied. An app the owner has not
+ connected yet keeps its tools; the card asks them to connect it."""
plans: List[_ToolPlan] = []
for app in inputs.apps:
for tool in app.tools:
diff --git a/server/services/handlers/tools.py b/server/services/handlers/tools.py
index 1aee1b1e..cbddb498 100644
--- a/server/services/handlers/tools.py
+++ b/server/services/handlers/tools.py
@@ -207,8 +207,20 @@ async def _dispatch_tool(tool_name: str, tool_args: Dict[str, Any], config: Dict
plugin_cls = None if is_registered_agent(node_type) else get_node_class(node_type)
if plugin_cls is not None:
+ from services.approvals.tool_calls import check_in_process
from services.plugin import NodeContext
+ # Ask first: a call that sends is refused while the workflow asks
+ # first (it cannot wait as a draft here); a restricted tool runs
+ # restricted. Temporal tool calls are checked in BaseNode.as_activity.
+ checked = await check_in_process(context, plugin_cls, node_params, tool_args)
+ if checked.result is not None:
+ return checked.result
+ if checked.node_data is not None:
+ node_params = checked.node_data
+ if checked.tool_args is not None:
+ tool_args = checked.tool_args
+
instance = plugin_cls()
ctx = NodeContext.from_legacy(
node_id=config.get("node_id", f"tool_{node_type}"),
diff --git a/server/tests/services/approvals/test_tool_calls.py b/server/tests/services/approvals/test_tool_calls.py
index 9f8002ad..791230fb 100644
--- a/server/tests/services/approvals/test_tool_calls.py
+++ b/server/tests/services/approvals/test_tool_calls.py
@@ -124,6 +124,78 @@ async def test_tools_that_cannot_wait_are_refused_or_restricted(harness, nodes_l
assert await store.list_approvals(harness.database) == []
+class Ran:
+ """Stands in for a plugin's execute_as_tool: records what ran."""
+
+ def __init__(self) -> None:
+ self.calls = []
+
+ async def __call__(self, tool_args, node_params, ctx):
+ self.calls.append((dict(tool_args), dict(node_params)))
+ return {"sent": True}
+
+
+async def dispatch(node_type, tool_args, params=None, **config):
+ from services.handlers.tools import _dispatch_tool
+
+ return await _dispatch_tool(
+ node_type,
+ tool_args,
+ {
+ "node_type": node_type,
+ "node_id": f"wf:{node_type}:1",
+ "workflow_id": "wf",
+ "parent_node_id": "wf:claudeCodeAgent:1",
+ "parameters": dict(params or {}),
+ **config,
+ },
+ )
+
+
+async def test_an_agent_outside_agentworkflow_sends_only_when_it_need_not_ask(harness, nodes_loaded, monkeypatch):
+ whatsapp, gmail = get_node_class("whatsappSend"), get_node_class("googleGmail")
+ ran = Ran()
+ monkeypatch.setattr(whatsapp, "execute_as_tool", ran)
+ monkeypatch.setattr(gmail, "execute_as_tool", ran)
+ args = {"recipient_type": "phone", "phone": "447700900123", "message": "On my way."}
+
+ # No rule (a workflow built in the editor), then Ask first off: it runs.
+ assert await dispatch("whatsappSend", args, {"message_type": "text"}) == {"sent": True}
+ await rules.set_ask_first(harness.database, "wf", False)
+ assert await dispatch("whatsappSend", args, {"message_type": "text"}) == {"sent": True}
+ # Ask first on: refused, nothing kept, since nothing could send it later.
+ await rules.set_ask_first(harness.database, "wf", True)
+ refused = await dispatch("whatsappSend", args, {"message_type": "text"})
+ assert refused["status"] == "not_run" and "WhatsApp" in refused["message"]
+ assert len(ran.calls) == 2 and await store.list_approvals(harness.database) == []
+ # A call that doesn't send, and a call no agent made, run as before.
+ assert await dispatch("googleGmail", {"operation": "search", "query": "invoice"}) == {"sent": True}
+ assert await dispatch("whatsappSend", args, {"message_type": "text"}, parent_node_id=None) == {"sent": True}
+
+
+async def test_an_agent_outside_agentworkflow_browses_read_only_while_asking(harness, nodes_loaded, monkeypatch):
+ browser = get_node_class("browser")
+ ran = Ran()
+ monkeypatch.setattr(browser, "execute_as_tool", ran)
+ await rules.set_ask_first(harness.database, "wf", True)
+ await dispatch("browser", {"operation": "click", "ref": "e3", "interaction": "full"}, {"interaction": "full"})
+ [(args, params)] = ran.calls
+ assert params["interaction"] == "read_only" and "interaction" not in args
+
+
+async def test_an_unreadable_rule_refuses_rather_than_sends(harness, nodes_loaded, monkeypatch):
+ whatsapp = get_node_class("whatsappSend")
+ ran = Ran()
+ monkeypatch.setattr(whatsapp, "execute_as_tool", ran)
+
+ async def broken(*_args, **_kwargs):
+ raise RuntimeError("database is locked")
+
+ monkeypatch.setattr(rules, "ask_first", broken)
+ refused = await dispatch("whatsappSend", {"recipient_type": "phone", "phone": "447700900123", "message": "Hi"}, {"message_type": "text"})
+ assert refused["status"] == "not_run" and ran.calls == []
+
+
async def test_send_starts_the_send_and_puts_it_back_when_nothing_can(harness, nodes_loaded, monkeypatch):
await rules.set_ask_first(harness.database, "wf", True)
await tool_calls.check(call(), get_node_class("whatsappSend"))