Skip to content

Self-improving AI Employees for everyone

fix(temporal): make parallel tool identities deterministic

trohitga0a48285e586
diff --git a/server/routers/nodejs_compat.py b/server/routers/nodejs_compat.py
index f62dabf8..6a20a0f1 100644
--- a/server/routers/nodejs_compat.py
+++ b/server/routers/nodejs_compat.py
@@ -113,6 +113,7 @@ async def execute_node(request: NodeExecuteRequest, workflow_service: WorkflowSe
nodes=request.nodes or [],
edges=request.edges or [],
session_id="default",
+ execution_id=execution_id,
)
# Transform result to match Node.js format
@@ -198,6 +199,7 @@ async def legacy_execute_node(
nodes=request.get("nodes", []),
edges=request.get("edges", []),
session_id="default",
+ execution_id=execution_id,
)
# Return in Node.js format
diff --git a/server/routers/websocket.py b/server/routers/websocket.py
index 20b73d9b..7255dfbc 100644
--- a/server/routers/websocket.py
+++ b/server/routers/websocket.py
@@ -411,7 +411,18 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dic
broadcaster = get_status_broadcaster()
node_id, node_type = data["node_id"], data["node_type"]
workflow_id = data.get("workflow_id") # Per-workflow isolation
- execution_id = uuid.uuid4().hex
+ execution_id = str(data.get("execution_id") or uuid.uuid4().hex)
+ invocation_extras = {
+ key: data[key]
+ for key in (
+ "auto_rebind_tools",
+ "invoking_agent_node_id",
+ "agent_iteration",
+ "tool_call_index",
+ "tool_call_id",
+ )
+ if key in data
+ }
await broadcaster.update_node_status(
node_id,
@@ -431,12 +442,22 @@ async def handle_execute_node(data: Dict[str, Any], websocket: WebSocket) -> Dic
nodes=data.get("nodes", []),
edges=data.get("edges", []),
session_id=data.get("session_id", "default"),
+ execution_id=execution_id,
workflow_id=workflow_id,
outputs=data.get("outputs", {}), # Upstream node outputs for data flow
+ extras=invocation_extras or None,
)
if result.get("success"):
- success_payload = {**(result.get("result") or {}), "execution_id": execution_id}
+ raw_result = result.get("result")
+ success_payload = (
+ {**raw_result, "execution_id": execution_id}
+ if isinstance(raw_result, dict)
+ else {
+ **({"result": raw_result} if raw_result is not None else {}),
+ "execution_id": execution_id,
+ }
+ )
await broadcaster.update_node_status(node_id, "success", success_payload, workflow_id=workflow_id)
await broadcaster.update_node_output(node_id, success_payload, workflow_id=workflow_id)
elif result.get("error") == "Cancelled by user":
@@ -905,6 +926,7 @@ async def handle_execute_workflow(data: Dict[str, Any], websocket: WebSocket) ->
"error": result.get("error"),
"total_nodes": result.get("total_nodes", 0),
"completed_nodes": result.get("completed_nodes", 0),
+ "execution_id": result.get("execution_id"),
"execution_time": result.get("execution_time", 0),
"timestamp": time.time(),
}
@@ -930,8 +952,9 @@ async def handle_execute_ai_node(data: Dict[str, Any], websocket: WebSocket) ->
broadcaster = get_status_broadcaster()
node_id, node_type = data["node_id"], data["node_type"]
workflow_id = data.get("workflow_id") # Per-workflow isolation for tool node glowing
+ execution_id = str(data.get("execution_id") or uuid.uuid4().hex)
- await broadcaster.update_node_status(node_id, "executing", workflow_id=workflow_id)
+ await broadcaster.update_node_status(node_id, "executing", {"execution_id": execution_id}, workflow_id=workflow_id)
await broadcaster.workflow_run_started(workflow_id)
result: Dict[str, Any]
try:
@@ -942,16 +965,36 @@ async def handle_execute_ai_node(data: Dict[str, Any], websocket: WebSocket) ->
nodes=data.get("nodes", []),
edges=data.get("edges", []),
session_id=data.get("session_id", "default"),
+ execution_id=execution_id,
workflow_id=workflow_id,
)
if result.get("success"):
- await broadcaster.update_node_status(node_id, "success", result.get("result"), workflow_id=workflow_id)
- await broadcaster.update_node_output(node_id, result.get("result"), workflow_id=workflow_id)
+ raw_result = result.get("result")
+ success_payload = {
+ **(
+ raw_result
+ if isinstance(raw_result, dict)
+ else ({"result": raw_result} if raw_result is not None else {})
+ ),
+ "execution_id": execution_id,
+ }
+ await broadcaster.update_node_status(node_id, "success", success_payload, workflow_id=workflow_id)
+ await broadcaster.update_node_output(node_id, success_payload, workflow_id=workflow_id)
else:
- await broadcaster.update_node_status(node_id, "error", {"error": result.get("error")}, workflow_id=workflow_id)
+ await broadcaster.update_node_status(
+ node_id,
+ "error",
+ {"error": result.get("error"), "execution_id": execution_id},
+ workflow_id=workflow_id,
+ )
except Exception:
- await broadcaster.update_node_status(node_id, "error", {"error": "execution crashed"}, workflow_id=workflow_id)
+ await broadcaster.update_node_status(
+ node_id,
+ "error",
+ {"error": "execution crashed", "execution_id": execution_id},
+ workflow_id=workflow_id,
+ )
raise
finally:
await broadcaster.workflow_run_ended(workflow_id)
@@ -959,6 +1002,7 @@ async def handle_execute_ai_node(data: Dict[str, Any], websocket: WebSocket) ->
return {
"success": result.get("success", False),
"node_id": node_id,
+ "execution_id": execution_id,
"result": result.get("result"),
"error": result.get("error"),
"execution_time": result.get("execution_time"),
diff --git a/server/routers/workflow.py b/server/routers/workflow.py
index ee405519..77b7c55c 100644
--- a/server/routers/workflow.py
+++ b/server/routers/workflow.py
@@ -2,7 +2,8 @@
from fastapi import APIRouter, Depends
from pydantic import BaseModel
-from typing import Dict, Any, List
+from typing import Dict, Any, List, Optional
+from uuid import uuid4
from core.container import container
from services.workflow import WorkflowService
@@ -20,6 +21,8 @@ class WorkflowExecutionRequest(BaseModel):
nodes: List[Dict[str, Any]] = []
edges: List[Dict[str, Any]] = []
session_id: str = "default"
+ workflow_id: Optional[str] = None
+ execution_id: Optional[str] = None
@router.post("/execute-node")
@@ -28,10 +31,16 @@ async def execute_workflow_node(
):
"""Execute a single node in a workflow with parameter resolution."""
logger.debug(f"[DEBUG ROUTER] Received execution request: node_id={request.node_id}, node_type={request.node_type}")
+ execution_id = request.execution_id or uuid4().hex
# Get broadcaster and send "executing" status
broadcaster = get_status_broadcaster()
- await broadcaster.update_node_status(node_id=request.node_id, status="executing", data={"node_type": request.node_type})
+ await broadcaster.update_node_status(
+ node_id=request.node_id,
+ status="executing",
+ data={"node_type": request.node_type, "execution_id": execution_id},
+ workflow_id=request.workflow_id,
+ )
try:
result = await workflow_service.execute_node(
@@ -41,21 +50,48 @@ async def execute_workflow_node(
nodes=request.nodes,
edges=request.edges,
session_id=request.session_id,
+ execution_id=execution_id,
+ workflow_id=request.workflow_id,
)
+ result.setdefault("execution_id", execution_id)
# Broadcast completion status based on result
if result.get("success"):
+ success_data = {
+ "node_type": request.node_type,
+ "execution_id": execution_id,
+ "execution_time": result.get("execution_time"),
+ "result": result.get("result"),
+ }
await broadcaster.update_node_status(
node_id=request.node_id,
status="success",
- data={"node_type": request.node_type, "execution_time": result.get("execution_time"), "result": result.get("result")},
+ data=success_data,
+ workflow_id=request.workflow_id,
)
# Also broadcast the output
if result.get("result"):
- await broadcaster.update_node_output(node_id=request.node_id, output=result.get("result"))
+ raw_result = result.get("result")
+ correlated_output = (
+ {**raw_result, "execution_id": execution_id}
+ if isinstance(raw_result, dict)
+ else {"result": raw_result, "execution_id": execution_id}
+ )
+ await broadcaster.update_node_output(
+ node_id=request.node_id,
+ output=correlated_output,
+ workflow_id=request.workflow_id,
+ )
else:
await broadcaster.update_node_status(
- node_id=request.node_id, status="error", data={"node_type": request.node_type, "error": result.get("error")}
+ node_id=request.node_id,
+ status="error",
+ data={
+ "node_type": request.node_type,
+ "execution_id": execution_id,
+ "error": result.get("error"),
+ },
+ workflow_id=request.workflow_id,
)
return result
@@ -63,7 +99,14 @@ async def execute_workflow_node(
except Exception as e:
# Broadcast error status
await broadcaster.update_node_status(
- node_id=request.node_id, status="error", data={"node_type": request.node_type, "error": str(e)}
+ node_id=request.node_id,
+ status="error",
+ data={
+ "node_type": request.node_type,
+ "execution_id": execution_id,
+ "error": str(e),
+ },
+ workflow_id=request.workflow_id,
)
raise
@@ -149,6 +192,17 @@ async def execute_node_for_temporal(
context = request.context
try:
+ invocation_extras = {
+ key: context[key]
+ for key in (
+ "auto_rebind_tools",
+ "invoking_agent_node_id",
+ "agent_iteration",
+ "tool_call_index",
+ "tool_call_id",
+ )
+ if key in context
+ }
result = await workflow_service.execute_node(
node_id=request.node_id,
node_type=request.node_type,
@@ -156,7 +210,9 @@ async def execute_node_for_temporal(
nodes=context.get("nodes", []),
edges=context.get("edges", []),
session_id=context.get("session_id", "temporal"),
+ execution_id=context.get("execution_id"),
workflow_id=context.get("workflow_id"),
+ extras=invocation_extras or None,
)
logger.info(
"Temporal endpoint node result",
diff --git a/server/services/plugin/base.py b/server/services/plugin/base.py
index 2a47ea83..3ac50fda 100644
--- a/server/services/plugin/base.py
+++ b/server/services/plugin/base.py
@@ -681,6 +681,7 @@ class BaseNode:
node_id = context["node_id"]
workflow_id = context.get("workflow_id")
+ execution_id = context.get("execution_id")
broadcaster = get_status_broadcaster()
# Pre-executed trigger nodes — return cached output without dispatching.
@@ -692,6 +693,7 @@ class BaseNode:
"node_type": cls.type,
"result": context.get("trigger_output", {}),
"pre_executed": True,
+ "execution_id": execution_id,
"timestamp": datetime.now().isoformat(),
}
await broadcaster.update_node_status(
@@ -712,12 +714,13 @@ class BaseNode:
"node_type": cls.type,
"skipped": True,
"reason": "disabled",
+ "execution_id": execution_id,
"timestamp": datetime.now().isoformat(),
}
await broadcaster.update_node_status(
node_id,
"skipped",
- {"disabled": True},
+ {"disabled": True, "execution_id": execution_id},
workflow_id=workflow_id,
)
return result
@@ -726,7 +729,7 @@ class BaseNode:
await broadcaster.update_node_status(
node_id,
"executing",
- {"node_type": cls.type},
+ {"node_type": cls.type, "execution_id": execution_id},
workflow_id=workflow_id,
)
@@ -762,8 +765,15 @@ class BaseNode:
# ``auto_rebind_tools`` for agentBuilder) through the
# ``extras`` channel so they land in NodeContext.raw.
extras: Dict[str, Any] = {}
- if "auto_rebind_tools" in context:
- extras["auto_rebind_tools"] = context["auto_rebind_tools"]
+ for key in (
+ "auto_rebind_tools",
+ "invoking_agent_node_id",
+ "agent_iteration",
+ "tool_call_index",
+ "tool_call_id",
+ ):
+ if key in context:
+ extras[key] = context[key]
result = await workflow_service.execute_node(
node_id=node_id,
node_type=cls.type,
@@ -779,6 +789,7 @@ class BaseNode:
result["node_id"] = node_id
result["node_type"] = cls.type
+ result.setdefault("execution_id", execution_id)
result["timestamp"] = datetime.now().isoformat()
# Polymorphic dispatch — each node base class owns its
@@ -788,16 +799,21 @@ class BaseNode:
# payload, error_message).
success, payload, error = cls.interpret_result(result)
if success:
+ correlated_payload = (
+ {**payload, "execution_id": execution_id}
+ if isinstance(payload, dict)
+ else {"result": payload, "execution_id": execution_id}
+ )
activity.logger.info(f"Node {node_id} succeeded")
await broadcaster.update_node_status(
node_id,
"success",
- payload,
+ correlated_payload,
workflow_id=workflow_id,
)
await broadcaster.update_node_output(
node_id,
- payload,
+ correlated_payload,
workflow_id=workflow_id,
)
else:
@@ -805,7 +821,7 @@ class BaseNode:
await broadcaster.update_node_status(
node_id,
"error",
- {"error": error},
+ {"error": error, "execution_id": execution_id},
workflow_id=workflow_id,
)
@@ -818,7 +834,7 @@ class BaseNode:
await broadcaster.update_node_status(
node_id,
"error",
- {"error": error_msg},
+ {"error": error_msg, "execution_id": execution_id},
workflow_id=workflow_id,
)
raise
diff --git a/server/services/temporal/activities.py b/server/services/temporal/activities.py
index 406986ae..417f5cbe 100644
--- a/server/services/temporal/activities.py
+++ b/server/services/temporal/activities.py
@@ -93,6 +93,7 @@ class NodeExecutionActivities:
node_type = context["node_type"]
node_data = context.get("node_data", {})
workflow_id = context.get("workflow_id")
+ execution_id = context.get("execution_id")
tenant_id = context.get("tenant_id")
activity.logger.info(
@@ -112,6 +113,7 @@ class NodeExecutionActivities:
"node_type": node_type,
"result": context.get("trigger_output", {}),
"pre_executed": True,
+ "execution_id": execution_id,
"timestamp": datetime.now().isoformat(),
}
await self._broadcast_status(node_id, "success", result, workflow_id)
@@ -126,16 +128,22 @@ class NodeExecutionActivities:
"node_type": node_type,
"skipped": True,
"reason": "disabled",
+ "execution_id": execution_id,
"timestamp": datetime.now().isoformat(),
}
- await self._broadcast_status(node_id, "skipped", {"disabled": True}, workflow_id)
+ await self._broadcast_status(
+ node_id,
+ "skipped",
+ {"disabled": True, "execution_id": execution_id},
+ workflow_id,
+ )
return result
# Broadcast "executing" status for UI updates
await self._broadcast_status(
node_id=node_id,
status="executing",
- data={"node_type": node_type},
+ data={"node_type": node_type, "execution_id": execution_id},
workflow_id=workflow_id,
)
@@ -172,7 +180,7 @@ class NodeExecutionActivities:
await self._broadcast_status(
node_id=node_id,
status="error",
- data={"error": error_msg},
+ data={"error": error_msg, "execution_id": execution_id},
workflow_id=workflow_id,
)
@@ -210,10 +218,20 @@ class NodeExecutionActivities:
"edges": context.get("edges", []),
"session_id": context.get("session_id", "default"),
"workflow_id": context.get("workflow_id"),
+ "execution_id": context.get("execution_id"),
# CRITICAL: Pass upstream node outputs for downstream nodes to access
# This enables taskTrigger -> chatAgent data flow via input-task handle
"outputs": context.get("inputs", {}),
}
+ for key in (
+ "auto_rebind_tools",
+ "invoking_agent_node_id",
+ "agent_iteration",
+ "tool_call_index",
+ "tool_call_id",
+ ):
+ if key in context:
+ message[key] = context[key]
activity.logger.debug(f"WebSocket execute for {node_id}")
diff --git a/server/services/temporal/agent_workflow.py b/server/services/temporal/agent_workflow.py
index ba670de1..e3ca2c34 100644
--- a/server/services/temporal/agent_workflow.py
+++ b/server/services/temporal/agent_workflow.py
@@ -100,6 +100,96 @@ def _default_max_iterations() -> int:
# failures inside the LLM step fail fast instead of burning 3 retries.
AGENT_ACTIVITY_RETRY: RetryPolicy = DEFAULT_ACTIVITY_RETRY
+# Temporal patch marker for per-call command identity and duplicate-name
+# validation. Existing histories must retain their recorded activity/child
+# ids and last-wins tool index; new histories take the isolated path.
+TOOL_CALL_IDENTITY_V2_PATCH = "agent-tool-call-identity-v2"
+DUPLICATE_TOOL_NAME_ERROR_TYPE = "DuplicateToolNameError"
+
+
+def _tool_activity_id_v2(tool_node_id: str, iteration: int, call_index: int) -> str:
+ """Return a stable, unique id for one tool call in one agent turn."""
+ return f"tool-{tool_node_id}-{iteration + 1}-{call_index + 1}"
+
+
+def _delegation_child_id_v2(
+ agent_workflow_id: str,
+ tool_node_id: str,
+ iteration: int,
+ call_index: int,
+) -> str:
+ """Return a stable child id for one delegation call."""
+ return f"{agent_workflow_id}-delegate-{tool_node_id}-{iteration + 1}-{call_index + 1}"
+
+
+def _refresh_tools_activity_id_v2(tool_node_id: str, iteration: int, call_index: int) -> str:
+ """Return a stable id for the hot-refresh owned by one tool call."""
+ return f"refresh-tools-{tool_node_id}-{iteration + 1}-{call_index + 1}"
+
+
+def _tool_call_metadata(
+ *,
+ agent_node_id: str,
+ iteration: int,
+ call_index: int,
+ call: Dict[str, Any],
+) -> Dict[str, Any]:
+ """Serializable correlation fields shared by one call's commands."""
+ return {
+ "invoking_agent_node_id": agent_node_id,
+ "agent_iteration": iteration + 1,
+ "tool_call_index": call_index + 1,
+ "tool_call_id": str(call.get("id", "") or ""),
+ }
+
+
+def _duplicate_visible_tool_name_conflicts(
+ tools: List[Dict[str, Any]],
+) -> Dict[str, List[Dict[str, str]]]:
+ """Return deterministic provider-name conflicts for one agent."""
+ grouped: Dict[str, List[Dict[str, str]]] = {}
+ for tool in tools:
+ name = str(tool.get("name", "") or "")
+ tool_info = tool.get("tool_info") or {}
+ grouped.setdefault(name, []).append(
+ {
+ "node_id": str(tool.get("tool_node_id") or "<missing-node-id>"),
+ "label": str(tool_info.get("label") or tool.get("node_type") or "tool"),
+ }
+ )
+ return {
+ name: sorted(entries, key=lambda entry: (entry["node_id"], entry["label"]))
+ for name, entries in sorted(grouped.items())
+ if len(entries) > 1
+ }
+
+
+def _duplicate_visible_tool_name_error(tools: List[Dict[str, Any]]) -> Optional[str]:
+ """Describe duplicate provider-visible names, or return ``None``.
+
+ Tool dispatch is name based and providers also require a unique function
+ surface. Reporting every conflicting node is safer than silently selecting
+ the last entry or inventing aliases that change the LLM contract.
+ """
+ conflicts = _duplicate_visible_tool_name_conflicts(tools)
+ if not conflicts:
+ return None
+
+ details: List[str] = []
+ for name in sorted(conflicts):
+ identities: List[str] = []
+ for identity in conflicts[name]:
+ identities.append(
+ f"{identity['label']} ({identity['node_id']})"
+ )
+ details.append(f"{name!r}: {', '.join(identities)}")
+
+ return (
+ "Duplicate LLM-visible tool names are not allowed: "
+ + "; ".join(details)
+ + ". Assign a unique Tool Name to each connected tool."
+ )
+
@workflow.defn(sandboxed=False, name="AgentWorkflow")
class AgentWorkflow:
@@ -180,6 +270,12 @@ class AgentWorkflow:
``services/ai.py:execute_agent`` returns today so downstream
code (OutputPanel, edge inputs, etc.) doesn't change.
"""
+ # Activity inputs and command ids are recorded in Temporal Event
+ # History. Old histories take the exact legacy branches below; new
+ # runs record this marker and receive per-call command identity plus
+ # deterministic duplicate-name validation.
+ use_tool_call_identity_v2 = workflow.patched(TOOL_CALL_IDENTITY_V2_PATCH)
+
# ---- Step 0: Resolve payload via the prep activity --------------
# DB lookups + edge walking + tool schema build happen here, NOT
# in the workflow body (workflows must be deterministic).
@@ -197,6 +293,51 @@ class AgentWorkflow:
# the ``child_context`` spread below. ``workflow.info().run_id``
# is deterministic — safe inside workflow code.
execution_id = str(context.get("execution_id") or "") or workflow.info().run_id[:8]
+ max_iterations = int(payload.get("max_iterations") or _default_max_iterations())
+ agent_node_id = payload["node_id"]
+ agent_workflow_id = payload.get("workflow_id")
+ self._parent_node_id: Optional[str] = context.get("parent_node_id")
+
+ # The provider-visible name is the dispatch key. The legacy dict
+ # comprehension silently selected the last connected node when two
+ # tools shared a name. New histories fail before the first billed LLM
+ # call and identify every conflicting canvas node.
+ tools = payload.get("tools") or []
+ duplicate_tool_error = (
+ _duplicate_visible_tool_name_error(tools)
+ if use_tool_call_identity_v2
+ else None
+ )
+ duplicate_tool_conflicts = (
+ _duplicate_visible_tool_name_conflicts(tools)
+ if duplicate_tool_error
+ else {}
+ )
+ if duplicate_tool_error:
+ await self._emit_phase(
+ agent_node_id,
+ agent_workflow_id,
+ 0,
+ max_iterations,
+ phase="failed",
+ status="error",
+ extra={
+ "error_type": DUPLICATE_TOOL_NAME_ERROR_TYPE,
+ "error": duplicate_tool_error,
+ "conflicts": duplicate_tool_conflicts,
+ },
+ )
+ return {
+ "success": False,
+ "error_type": DUPLICATE_TOOL_NAME_ERROR_TYPE,
+ "error": duplicate_tool_error,
+ "conflicts": duplicate_tool_conflicts,
+ "node_id": agent_node_id,
+ "node_type": payload.get("node_type"),
+ "execution_id": execution_id,
+ "result": {"iterations": 0, "usage": {}},
+ }
+
# ---- Build initial message list ---------------------------------
# Workflow state is JSON dicts in LangChain's canonical shape
# ({"type": "<role>", "data": {"content": "...", ...}}) so the
@@ -228,20 +369,14 @@ class AgentWorkflow:
# Map LLM tool name -> {node_type, version, task_queue, node_id,
# parameters} so the workflow can schedule the right activity
# when the LLM emits a tool_call.
- tools = payload.get("tools") or []
tool_index: Dict[str, Dict[str, Any]] = {t["name"]: t for t in tools}
- max_iterations = int(payload.get("max_iterations") or _default_max_iterations())
token_total = 0
compaction_threshold = payload.get("compaction_threshold")
thinking_accumulated = ""
final_content: Optional[str] = None
usage_total: Dict[str, int] = {}
- agent_node_id = payload["node_id"]
- agent_workflow_id = payload.get("workflow_id")
- self._parent_node_id: Optional[str] = context.get("parent_node_id")
-
# Emit "executing" + phase="starting" via the existing
# broadcast_agent_progress activity (CloudEvents
# com.opencompany.agent.progress + raw-dict node_status for
@@ -353,7 +488,7 @@ class AgentWorkflow:
calls = step_result.get("calls") or []
workflow.logger.info(f"AgentWorkflow scheduling {len(calls)} tool call(s)")
- for call in calls:
+ for call_index, call in enumerate(calls):
tool_info = tool_index.get(call.get("name", ""))
if tool_info is None:
workflow.logger.warning(f"AgentWorkflow: LLM called unknown tool {call.get('name')!r}; " "returning error to model")
@@ -447,6 +582,16 @@ class AgentWorkflow:
child_nodes = []
child_edges = []
+ call_metadata = (
+ _tool_call_metadata(
+ agent_node_id=agent_node_id,
+ iteration=iteration,
+ call_index=call_index,
+ call=call,
+ )
+ if use_tool_call_identity_v2
+ else {}
+ )
tool_payload = {
"node_id": tool_info["tool_node_id"],
"node_type": tool_info["node_type"],
@@ -461,6 +606,7 @@ class AgentWorkflow:
# so canvas-mutating tools (agentBuilder) render their
# summary text to match the user's current preference.
"auto_rebind_tools": bool(payload.get("auto_rebind_tools", True)),
+ **call_metadata,
}
tool_activity_name = f"node.{tool_info['node_type']}.v{tool_info['version']}"
@@ -484,18 +630,37 @@ class AgentWorkflow:
"context": task_context,
},
}
+ delegation_child_id = (
+ _delegation_child_id_v2(
+ workflow.info().workflow_id,
+ tool_info["tool_node_id"],
+ iteration,
+ call_index,
+ )
+ if use_tool_call_identity_v2
+ else f"{workflow.info().workflow_id}-delegate-{tool_info['tool_node_id']}-{iteration}"
+ )
tool_result = await workflow.execute_child_workflow(
"AgentWorkflow",
args=[child_context],
- id=f"{workflow.info().workflow_id}-delegate-{tool_info['tool_node_id']}-{iteration}",
+ id=delegation_child_id,
execution_timeout=timedelta(hours=1),
run_timeout=timedelta(hours=1),
)
else:
+ tool_activity_id = (
+ _tool_activity_id_v2(
+ tool_info["tool_node_id"],
+ iteration,
+ call_index,
+ )
+ if use_tool_call_identity_v2
+ else f"tool-{tool_info['tool_node_id']}-{iteration + 1}"
+ )
tool_result = await workflow.execute_activity(
tool_activity_name,
args=[tool_payload],
- activity_id=f"tool-{tool_info['tool_node_id']}-{iteration + 1}",
+ activity_id=tool_activity_id,
start_to_close_timeout=TOOL_STEP_TIMEOUT,
heartbeat_timeout=TOOL_HEARTBEAT_TIMEOUT,
)
@@ -525,14 +690,63 @@ class AgentWorkflow:
# step's one-shot LLM_STEP_RETRY): rebuilding
# the tool surface from canvas state is fully
# idempotent, so retries are free.
+ refresh_activity_id = (
+ _refresh_tools_activity_id_v2(
+ tool_info["tool_node_id"],
+ iteration,
+ call_index,
+ )
+ if use_tool_call_identity_v2
+ else f"refresh-tools-{tool_info['tool_node_id']}-{iteration + 1}"
+ )
+ refresh_payload = {
+ "operations": ops_from_tool,
+ **call_metadata,
+ }
refresh_result = await workflow.execute_activity(
"agent.refresh_tools.v1",
- args=[{"operations": ops_from_tool}],
- activity_id=f"refresh-tools-{tool_info['tool_node_id']}-{iteration + 1}",
+ args=[refresh_payload],
+ activity_id=refresh_activity_id,
start_to_close_timeout=timedelta(seconds=30),
retry_policy=AGENT_ACTIVITY_RETRY,
)
added_tools = refresh_result.get("tools") or []
+ refresh_duplicate_error = (
+ _duplicate_visible_tool_name_error([*tools, *added_tools])
+ if use_tool_call_identity_v2
+ else None
+ )
+ refresh_duplicate_conflicts = (
+ _duplicate_visible_tool_name_conflicts([*tools, *added_tools])
+ if refresh_duplicate_error
+ else {}
+ )
+ if refresh_duplicate_error:
+ workflow.logger.warning(
+ "AgentWorkflow rejected hot-rebound tools: %s",
+ refresh_duplicate_error,
+ )
+ tool_content = _serialise_tool_result(
+ {
+ "error_type": DUPLICATE_TOOL_NAME_ERROR_TYPE,
+ "error": refresh_duplicate_error,
+ "conflicts": refresh_duplicate_conflicts,
+ }
+ )
+ await self._emit_phase(
+ agent_node_id,
+ agent_workflow_id,
+ iteration,
+ max_iterations,
+ phase="tool_error",
+ extra={
+ "error_type": DUPLICATE_TOOL_NAME_ERROR_TYPE,
+ "error": refresh_duplicate_error,
+ "conflicts": refresh_duplicate_conflicts,
+ **call_metadata,
+ },
+ )
+ added_tools = []
for new_tool in added_tools:
tools.append(new_tool)
tool_index[new_tool["name"]] = new_tool
diff --git a/server/services/temporal/workflow.py b/server/services/temporal/workflow.py
index 7a3f455f..96472964 100644
--- a/server/services/temporal/workflow.py
+++ b/server/services/temporal/workflow.py
@@ -72,6 +72,17 @@ AGENT_WORKFLOW_TYPES = frozenset(
]
)
+# Temporal patch marker for the root -> agent child-workflow identity change.
+# Keep the legacy branch until every pre-patch history is outside the
+# namespace's replay/reset window, then deprecate the marker in a later
+# release before removing that branch.
+AGENT_CHILD_ID_V2_PATCH = "machina-agent-child-id-v2"
+
+
+def _agent_child_workflow_id_v2(root_workflow_id: str, agent_node_id: str) -> str:
+ """Return a deterministic child id unique to one canvas agent and run."""
+ return f"{root_workflow_id}-agent-{agent_node_id}"
+
# Durable protocol identifier: existing Temporal histories and child-workflow
# start calls replay against this exact type string. It intentionally retains
@@ -208,6 +219,11 @@ class MachinaWorkflow:
# session-keyed nodes (browser) share one instance per run instead
# of minting a fresh uuid per call (node_executor.py fallback).
execution_id = workflow_data.get("execution_id") or workflow.info().workflow_id
+ # Command attributes are part of Temporal Event History. Existing
+ # histories must keep the label-derived child ids they recorded;
+ # new histories use the root Temporal id + exact canvas node id so
+ # two same-label agents can start in the same graph execution.
+ use_agent_child_id_v2 = workflow.patched(AGENT_CHILD_ID_V2_PATCH)
workflow.logger.info(f"Starting workflow orchestration: {len(nodes)} nodes, {len(edges)} edges")
@@ -348,15 +364,22 @@ class MachinaWorkflow:
# blocking on child completion). The handle is a
# Task-like with ``.done()`` so it slots into the same
# FIRST_COMPLETED loop as activity handles.
- # Child id format: ``<slug>-<node_label>`` — same
- # ``<workflow_slug>-<node_label>`` convention as the
- # trigger ids. ``node_label`` is the user-renamed
- # F2 label or the node type (``aiAgent`` / ``orchestrator_agent``
- # / ...) — already conveys "agent" so no middle tag.
+ # Patch-gated for replay safety. The legacy label-derived
+ # id is preserved for histories recorded before
+ # ``machina-agent-child-id-v2``. New runs use the actual
+ # root Temporal workflow id plus the exact canvas node id;
+ # labels are mutable and need not be unique.
+ if use_agent_child_id_v2:
+ child_workflow_id = _agent_child_workflow_id_v2(
+ workflow.info().workflow_id,
+ node_id,
+ )
+ else:
+ child_workflow_id = f"{workflow_slug}-{node_label_slug(node)}"
handle = await workflow.start_child_workflow(
dispatch["name"],
args=[context],
- id=f"{workflow_slug}-{node_label_slug(node)}",
+ id=child_workflow_id,
# Child workflow execution timeout: agent loops can run
# 10+ minutes with multiple LLM turns. Set generously.
execution_timeout=timedelta(hours=1),
diff --git a/server/services/temporal/ws_client.py b/server/services/temporal/ws_client.py
index 6cb2c460..ba0847ab 100644
--- a/server/services/temporal/ws_client.py
+++ b/server/services/temporal/ws_client.py
@@ -117,7 +117,17 @@ class WSConnectionPool:
"edges": context.get("edges", []),
"session_id": context.get("session_id", "default"),
"workflow_id": context.get("workflow_id"),
+ "execution_id": context.get("execution_id"),
}
+ for key in (
+ "auto_rebind_tools",
+ "invoking_agent_node_id",
+ "agent_iteration",
+ "tool_call_index",
+ "tool_call_id",
+ ):
+ if key in context:
+ message[key] = context[key]
try:
async with self.connection() as ws:
diff --git a/server/services/tool_identity.py b/server/services/tool_identity.py
new file mode 100644
index 00000000..b9e7d974
--- /dev/null
+++ b/server/services/tool_identity.py
@@ -0,0 +1,74 @@
+"""Deterministic validation for name-dispatched agent tool surfaces."""
+
+from __future__ import annotations
+
+from collections import defaultdict
+from typing import Any, Dict, Iterable, List, Mapping, Optional
+
+
+class DuplicateToolNameError(ValueError):
+ """Raised before model/tool exposure when names are ambiguous."""
+
+ error_type = "DuplicateToolNameError"
+
+ def __init__(self, conflicts: Mapping[str, List[Dict[str, str]]]):
+ normalized = {
+ name: sorted(
+ [dict(identity) for identity in identities],
+ key=lambda identity: (identity["node_id"], identity["label"]),
+ )
+ for name, identities in sorted(conflicts.items())
+ }
+ self.conflicts = normalized
+ details = []
+ for name, identities in normalized.items():
+ nodes = ", ".join(
+ f"{identity['label']} ({identity['node_id']})"
+ for identity in identities
+ )
+ details.append(f"{name!r}: {nodes}")
+ super().__init__(
+ "Duplicate LLM-visible tool names are not allowed: "
+ + "; ".join(details)
+ + ". Assign a unique Tool Name to each connected tool."
+ )
+
+ def as_dict(self) -> Dict[str, Any]:
+ return {
+ "error_type": self.error_type,
+ "error": str(self),
+ "conflicts": self.conflicts,
+ }
+
+
+def duplicate_tool_name_error(
+ identities: Iterable[Mapping[str, Any]],
+) -> Optional[DuplicateToolNameError]:
+ """Return a structured duplicate-name error, if one exists.
+
+ Each identity uses the canonical ``name``, ``node_id``, and ``label``
+ fields. Validation is intentionally scoped to one agent surface; the same
+ visible name remains valid on different agents.
+ """
+ grouped: Dict[str, List[Dict[str, str]]] = defaultdict(list)
+ for identity in identities:
+ name = str(identity.get("name") or "")
+ grouped[name].append(
+ {
+ "node_id": str(identity.get("node_id") or "<missing-node-id>"),
+ "label": str(identity.get("label") or identity.get("node_type") or "tool"),
+ }
+ )
+ conflicts = {
+ name: entries
+ for name, entries in grouped.items()
+ if len(entries) > 1
+ }
+ return DuplicateToolNameError(conflicts) if conflicts else None
+
+
+def ensure_unique_tool_names(identities: Iterable[Mapping[str, Any]]) -> None:
+ """Raise :class:`DuplicateToolNameError` for an ambiguous surface."""
+ error = duplicate_tool_name_error(identities)
+ if error is not None:
+ raise error
diff --git a/server/services/workflow.py b/server/services/workflow.py
index 0f2053e3..9580b4c6 100644
--- a/server/services/workflow.py
+++ b/server/services/workflow.py
@@ -14,6 +14,7 @@ import time
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, List, Optional, TYPE_CHECKING
+from uuid import uuid4
from core.logging import get_logger
from constants import WORKFLOW_TRIGGER_TYPES
@@ -409,6 +410,7 @@ class WorkflowService:
async def _execute_sequential(self, nodes, edges, session_id, status_callback, start_time, workflow_id: Optional[str] = None) -> Dict:
"""Execute nodes sequentially (fallback mode)."""
+ execution_id = uuid4().hex
start_node = self._find_start_node(nodes)
execution_order = self._build_execution_order(start_node, nodes, edges)
@@ -450,6 +452,7 @@ class WorkflowService:
nodes=nodes,
edges=edges,
session_id=session_id,
+ execution_id=execution_id,
workflow_id=workflow_id,
)
@@ -469,6 +472,7 @@ class WorkflowService:
return {
"success": all(r.get("success", False) for r in results.values()),
+ "execution_id": execution_id,
"nodes_executed": executed,
"node_results": results,
"execution_time": time.time() - start_time,