Skip to content

Automate browser based workflows with AI

feat(copilot): emit workflow_run_id on block-running tool-result SSE frames (#7018)

Trevor Cheungae61830ec564
diff --git a/skyvern/forge/sdk/copilot/streaming_adapter.py b/skyvern/forge/sdk/copilot/streaming_adapter.py
index 9ce594d64..4f3ceba9e 100644
--- a/skyvern/forge/sdk/copilot/streaming_adapter.py
+++ b/skyvern/forge/sdk/copilot/streaming_adapter.py
@@ -234,6 +234,7 @@ async def stream_to_sse(
iteration=iteration,
tool_call_id=call_id,
detail=detail,
+ workflow_run_id=_tool_result_workflow_run_id(tool_name, parsed),
)
)
@@ -326,6 +327,19 @@ async def _emit_code_repair_progress(
)
+_BLOCK_RUNNING_TOOL_NAMES = frozenset({"update_and_run_blocks", "run_blocks_and_collect_debug"})
+
+
+def _tool_result_workflow_run_id(tool_name: str, parsed: dict[str, Any]) -> str | None:
+ # Only block-running tools create a new run; read-only tools (e.g. get_run_results) echo a prior
+ # run id and would misattribute a stale run to the current turn.
+ if tool_name not in _BLOCK_RUNNING_TOOL_NAMES:
+ return None
+ data = parsed.get("data")
+ run_id = data.get("workflow_run_id") if isinstance(data, dict) else None
+ return run_id if isinstance(run_id, str) else None
+
+
async def flush_goal_satisfied_tool_result(stream: EventSourceStream, ctx: CopilotContext) -> None:
"""Emit the TOOL_RESULT frame for the goal-satisfying tool.
@@ -360,6 +374,7 @@ async def flush_goal_satisfied_tool_result(stream: EventSourceStream, ctx: Copil
iteration=pending.iteration,
tool_call_id=pending.call_id,
detail=summarize_tool_result_detail(parsed, tool_name=pending.tool_name, blocker_signal=blocker_signals),
+ workflow_run_id=_tool_result_workflow_run_id(pending.tool_name, parsed),
)
)
diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.py
index da7efafac..fac5cc05d 100644
--- a/skyvern/forge/sdk/schemas/workflow_copilot.py
+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py
@@ -288,6 +288,14 @@ class WorkflowCopilotToolResultUpdate(BaseModel):
"Distinct from `summary`, which is capped tighter for the visible bullet."
),
)
+ workflow_run_id: str | None = Field(
+ None,
+ description=(
+ "The workflow run a block-running tool created, when this result came from one "
+ "(update_and_run_blocks / run_blocks_and_collect_debug). Present whether the run "
+ "passed or failed; None for non-run tools."
+ ),
+ )
class WorkflowCopilotCondensingUpdate(BaseModel):
diff --git a/tests/unit/test_copilot_streaming_adapter.py b/tests/unit/test_copilot_streaming_adapter.py
index aa6ee3ba9..5ce350728 100644
--- a/tests/unit/test_copilot_streaming_adapter.py
+++ b/tests/unit/test_copilot_streaming_adapter.py
@@ -724,6 +724,22 @@ class TestEnforcementStateUpdates:
assert ctx.update_workflow_called is False
+def test_tool_result_workflow_run_id_only_for_block_running_tools() -> None:
+ from skyvern.forge.sdk.copilot.streaming_adapter import _tool_result_workflow_run_id
+
+ payload = {"ok": True, "data": {"workflow_run_id": "wr_42"}}
+ # Block-running tools created the run -> surface it (pass or fail).
+ assert _tool_result_workflow_run_id("update_and_run_blocks", payload) == "wr_42"
+ assert _tool_result_workflow_run_id("run_blocks_and_collect_debug", payload) == "wr_42"
+ # get_run_results echoes a prior run id; attributing it to this turn would grade a stale run.
+ assert _tool_result_workflow_run_id("get_run_results", payload) is None
+ assert _tool_result_workflow_run_id("update_workflow", payload) is None
+ # Malformed / missing data payloads yield None rather than raising.
+ assert _tool_result_workflow_run_id("update_and_run_blocks", {}) is None
+ assert _tool_result_workflow_run_id("update_and_run_blocks", {"data": "string"}) is None
+ assert _tool_result_workflow_run_id("update_and_run_blocks", {"data": {"workflow_run_id": 42}}) is None
+
+
class TestFlushGoalSatisfiedToolResult:
@staticmethod
def _ctx(**overrides: Any) -> SimpleNamespace: