Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
Record Vertex agent tool outputs so nodes show results like normal runs
trohitgcefdae455fbf
diff --git a/server/nodes/agent/vertex_managed_agent/__init__.py b/server/nodes/agent/vertex_managed_agent/__init__.pyindex dbe28aeb..0e1d654e 100644--- a/server/nodes/agent/vertex_managed_agent/__init__.py+++ b/server/nodes/agent/vertex_managed_agent/__init__.py@@ -222,7 +222,7 @@ class VertexManagedAgentNode(ActionNode):# ---- live visibility state (shared with the post-turn sweep)live_nodes: Dict[str, str] = {} # cloud_tool_key -> canvas node id- open_calls: Dict[str, str] = {} # step id -> canvas node id+ open_calls: Dict[str, Dict[str, Any]] = {} # step id -> {node_id, label, arguments}on_event = Noneif params.visualize_cloud_tools:on_event = self._make_live_handler(@@ -346,8 +346,9 @@ class VertexManagedAgentNode(ActionNode):for swept_id in resolved.values():await pulse_node(swept_id, "executing", workflow_id=workflow_id)await pulse_node(swept_id, "success", workflow_id=workflow_id)+ await self._record_swept_outputs(interaction, resolved, workflow_id)# Calls whose result step never streamed: don't leave them glowing.- for dangling_id in set(open_calls.values()):+ for dangling_id in {c["node_id"] for c in open_calls.values()}:await pulse_node(dangling_id, "success", workflow_id=workflow_id)open_calls.clear()except Exception: # noqa: BLE001 — visualization is best-effort@@ -526,18 +527,40 @@ class VertexManagedAgentNode(ActionNode):if ctx.raw.get(key) is not None:config[key] = ctx.raw[key]+ from ._ops import record_tool_output++ async def record(payload: Dict[str, Any], is_error: bool) -> None:+ # Same Output-panel visibility as a normally-executed node.+ tool_node_id = config.get("node_id")+ if not tool_node_id:+ return+ await record_tool_output(+ tool_node_id,+ {+ "tool": call["name"],+ "arguments": dict(call["arguments"]),+ "result": payload,+ "is_error": is_error,+ "timestamp": datetime.now().isoformat(),+ },+ workflow_id=ctx.workflow_id,+ )+try:result = await execute_tool(call["name"], dict(call["arguments"]), config)+ jsonable = to_jsonable_python(result)+ await record(jsonable, is_error=False)return {"type": "function_result","name": call["name"],"call_id": call["call_id"],- "result": to_jsonable_python(result),+ "result": jsonable,}except Exception as exc: # noqa: BLE001 — surfaced to the cloud agentlogger.warning("[Vertex Agent] bridged tool %s failed: %s", call["name"], exc)+ await record({"error": str(exc)}, is_error=True)return {"type": "function_result","name": call["name"],@@ -564,6 +587,71 @@ class VertexManagedAgentNode(ActionNode):used[f"fn:{name}"] = namereturn used+ @staticmethod+ def _jsonable(value: Any) -> Any:+ """Best-effort JSON-safe conversion for step payloads."""+ if value is None:+ return None+ try:+ from pydantic_core import to_jsonable_python++ if hasattr(value, "model_dump"):+ return value.model_dump(mode="json", exclude_none=True)+ return to_jsonable_python(value)+ except Exception: # noqa: BLE001 — display data only+ return str(value)++ async def _record_swept_outputs(+ self,+ interaction: Any,+ resolved: Dict[str, str],+ workflow_id: Optional[str],+ ) -> None:+ """Record invocation outputs for cloud tools the stream missed.++ Pairs each swept key's call step with its ``call_id``-matched+ result step from the FINAL resource, mirroring what the live+ handler records mid-stream.+ """+ from ._ops import record_tool_output++ steps = list(getattr(interaction, "steps", None) or [])+ calls_by_id: Dict[str, Tuple[str, Any]] = {}+ for step in steps:+ step_type = getattr(step, "type", "") or ""+ key = None+ if step_type in _CLOUD_STEP_LABELS:+ key = f"type:{step_type}"+ elif step_type == "function_call":+ name = getattr(step, "name", "") or ""+ if name and name not in _CLOUD_NOISE_NAMES:+ key = f"fn:{name}"+ if key and key in resolved:+ step_id = getattr(step, "id", None)+ if step_id:+ calls_by_id[step_id] = (key, step)++ for step in steps:+ step_type = getattr(step, "type", "") or ""+ if not (step_type == "function_result" or step_type.endswith("_result")):+ continue+ entry = calls_by_id.get(getattr(step, "call_id", None))+ if entry is None:+ continue+ key, call_step = entry+ await record_tool_output(+ resolved[key],+ {+ "tool": self._label_for_key(key),+ "arguments": self._jsonable(getattr(call_step, "arguments", None)),+ "result": self._jsonable(getattr(step, "result", None))+ or self._jsonable(step),+ "is_error": bool(getattr(step, "is_error", False)),+ "timestamp": datetime.now().isoformat(),+ },+ workflow_id=workflow_id,+ )+@staticmethoddef _label_for_key(key: str) -> str:"""Recover the display label from a cloud_tool_key."""@@ -593,10 +681,14 @@ class VertexManagedAgentNode(ActionNode):their real execution still happens at requires_action, where``execute_tool`` owns their canvas animation.+ Result steps also RECORD the invocation output on the display+ node (``record_tool_output``) so clicking it shows what the+ cloud tool did — same visibility as a normally-executed node.+Exceptions are swallowed by ``stream_interaction`` — visibilitymust never kill the turn."""- from ._ops import ensure_cloud_tool_nodes, pulse_node+ from ._ops import ensure_cloud_tool_nodes, pulse_node, record_tool_outputasync def phase_tool(label: str) -> None:await broadcaster.update_node_status(@@ -628,12 +720,25 @@ class VertexManagedAgentNode(ActionNode):returnstep_type = getattr(step, "type", "") or ""- # Result steps close the matching call's glow.+ # Result steps close the matching call's glow and record the+ # invocation output onto the display node.if step_type == "function_result" or step_type.endswith("_result"):call_id = getattr(step, "call_id", None)- node_id = open_calls.pop(call_id, None)- if node_id and workflow_id:- await pulse_node(node_id, "success", workflow_id=workflow_id)+ call = open_calls.pop(call_id, None)+ if call and workflow_id:+ await pulse_node(call["node_id"], "success", workflow_id=workflow_id)+ await record_tool_output(+ call["node_id"],+ {+ "tool": call["label"],+ "arguments": call["arguments"],+ "result": self._jsonable(getattr(step, "result", None))+ or self._jsonable(step),+ "is_error": bool(getattr(step, "is_error", False)),+ "timestamp": datetime.now().isoformat(),+ },+ workflow_id=workflow_id,+ )returnname = getattr(step, "name", "") or ""@@ -667,7 +772,11 @@ class VertexManagedAgentNode(ActionNode):await pulse_node(node_id, "executing", workflow_id=workflow_id)step_id = getattr(step, "id", None)if step_id:- open_calls[step_id] = node_id+ open_calls[step_id] = {+ "node_id": node_id,+ "label": label,+ "arguments": self._jsonable(getattr(step, "arguments", None)),+ }return on_eventdiff --git a/server/nodes/agent/vertex_managed_agent/_ops.py b/server/nodes/agent/vertex_managed_agent/_ops.pyindex 54f02bca..6065574c 100644--- a/server/nodes/agent/vertex_managed_agent/_ops.py+++ b/server/nodes/agent/vertex_managed_agent/_ops.py@@ -48,6 +48,30 @@ async def pulse_node()+async def record_tool_output(+ node_id: str,+ payload: Dict[str, object],+ *,+ workflow_id: str | None,+ session_id: str = "default",+) -> None:+ """Persist + broadcast a tool invocation's output for the node panel.++ Same visibility contract as a normally-executed node: the frontend+ Output section fetches ``get_node_output`` under the "default"+ session (memory miss falls back to the DB row written here), and the+ ``node_output`` broadcast refreshes an open panel live.+ """+ try:+ await get_database().save_node_output(node_id, session_id, "output_0", dict(payload))+ except Exception: # noqa: BLE001 — output visibility is best-effort+ logger.exception("[Vertex Agent] tool output persist failed for %s", node_id)+ return+ await get_status_broadcaster().update_node_output(+ node_id, dict(payload), workflow_id=workflow_id+ )++async def ensure_cloud_tool_nodes(*,workflow_id: str,diff --git a/server/tests/nodes/test_vertex_agents.py b/server/tests/nodes/test_vertex_agents.pyindex 068c1153..6643bd76 100644--- a/server/tests/nodes/test_vertex_agents.py+++ b/server/tests/nodes/test_vertex_agents.py@@ -255,6 +255,7 @@ class TestVertexManagedAgent:_interaction(status="completed", interaction_id="ix-2"),]client_patch, create_patch = _patched_interactions(turns)+ record_mock = AsyncMock()with (patched_container(auth_api_keys={}),patched_broadcaster() as bc,@@ -262,6 +263,7 @@ class TestVertexManagedAgent:create_patch as create_mock,patch("services.plugin.deps.get_ai_service", return_value=ai_service),patch("services.handlers.tools.execute_tool", execute_tool_mock),+ patch(f"{_NODE_MODULE}._ops.record_tool_output", record_mock),):_wire_async_broadcasts(bc)result = await harness.execute(@@ -309,6 +311,14 @@ class TestVertexManagedAgent:}]+ # Local tool node got its invocation output recorded (Output panel).+ record_mock.assert_awaited_once()+ rec = record_mock.await_args+ assert rec.args[0] == tool_id+ assert rec.args[1]["tool"] == "fake_tool"+ assert rec.args[1]["result"] == {"answer": 42}+ assert rec.args[1]["is_error"] is False+async def test_stale_chain_wipes_and_retries_fresh(self, harness):agent_id = "vx-1"mem_id = "mem-1"@@ -443,7 +453,12 @@ class TestVertexManagedAgent:SimpleNamespace(event_type="step.start",index=1,- step=SimpleNamespace(type="code_execution_result", call_id="c1"),+ step=SimpleNamespace(+ type="code_execution_result",+ call_id="c1",+ result={"stdout": "42"},+ is_error=False,+ ),))await on_event(@@ -459,6 +474,7 @@ class TestVertexManagedAgent:mint_mock = AsyncMock(return_value={"type:code_execution_call": "vct-1"})pulse_mock = AsyncMock()+ record_mock = AsyncMock()with (patched_container(auth_api_keys={}),patched_broadcaster() as bc,@@ -469,6 +485,7 @@ class TestVertexManagedAgent:),patch(f"{_NODE_MODULE}._ops.ensure_cloud_tool_nodes", mint_mock),patch(f"{_NODE_MODULE}._ops.pulse_node", pulse_mock),+ patch(f"{_NODE_MODULE}._ops.record_tool_output", record_mock),):_wire_async_broadcasts(bc)result = await harness.execute(@@ -493,6 +510,14 @@ class TestVertexManagedAgent:assert ("vct-1", "executing") in pulsesassert ("vct-1", "success") in pulsesassert result["result"]["cloud_tools_used"] == ["Code Execution"]+ # Invocation output recorded on the display node (Output panel).+ record_mock.assert_awaited_once()+ rec_args = record_mock.await_args+ assert rec_args.args[0] == "vct-1"+ payload = rec_args.args[1]+ assert payload["tool"] == "Code Execution"+ assert payload["result"] == {"stdout": "42"}+ assert payload["is_error"] is False# ============================================================================@@ -641,6 +666,46 @@ class TestCloudToolMinting:assert args.args[1] == "executing"assert args.kwargs["workflow_id"] == "wf-1"+ async def test_record_tool_output_persists_then_broadcasts(self):+ from nodes.agent.vertex_managed_agent import _ops++ database = MagicMock()+ database.save_node_output = AsyncMock(return_value=True)+ broadcaster = MagicMock()+ broadcaster.update_node_output = AsyncMock()+ payload = {"tool": "run_command", "result": {"stdout": "ok"}}++ with (+ patch.object(_ops, "get_database", return_value=database),+ patch.object(_ops, "get_status_broadcaster", return_value=broadcaster),+ ):+ await _ops.record_tool_output("vct-1", payload, workflow_id="wf-1")++ # Stored under the "default" session (what the Output panel fetches).+ database.save_node_output.assert_awaited_once_with(+ "vct-1", "default", "output_0", payload+ )+ out_args = broadcaster.update_node_output.await_args+ assert out_args.args[0] == "vct-1"+ assert out_args.args[1] == payload+ assert out_args.kwargs["workflow_id"] == "wf-1"++ async def test_record_tool_output_skips_broadcast_on_persist_failure(self):+ from nodes.agent.vertex_managed_agent import _ops++ database = MagicMock()+ database.save_node_output = AsyncMock(side_effect=RuntimeError("db locked"))+ broadcaster = MagicMock()+ broadcaster.update_node_output = AsyncMock()++ with (+ patch.object(_ops, "get_database", return_value=database),+ patch.object(_ops, "get_status_broadcaster", return_value=broadcaster),+ ):+ await _ops.record_tool_output("vct-1", {"tool": "x"}, workflow_id="wf-1")++ broadcaster.update_node_output.assert_not_awaited()+async def test_dedupes_existing_node_by_label(self):from nodes.agent.vertex_managed_agent import _ops