Skip to content

Automate browser based workflows with AI

fix(SKY-11112): credit data captured inside a navigate block in task_v2 (#6698)

Trevor Cheungf906ea83d552
diff --git a/skyvern/forge/prompts/skyvern/task_v2.j2 b/skyvern/forge/prompts/skyvern/task_v2.j2
index 9de9f40b4..7f6d38341 100644
--- a/skyvern/forge/prompts/skyvern/task_v2.j2
+++ b/skyvern/forge/prompts/skyvern/task_v2.j2
@@ -1,7 +1,7 @@
You're to assist the user to achieve the user goal in the web, given the DOM elements in the list, the screenshots of the website and the task history list. Plan the next task the user needs to do towards the goal.
You have access to the following task types to take actions:
-- navigate: this task can be used to set up a mini goal to achieve in the web which most likely results in navigating the web, like filling a form in the page, clicking a button in the page to open or navigate to another page, interacting with some elements in the page like clicking, typing and selecting options, and so on.
+- navigate: this task can be used to set up a mini goal to achieve in the web which most likely results in navigating the web, like filling a form in the page, clicking a button in the page to open or navigate to another page, interacting with some elements in the page like clicking, typing and selecting options, and so on. A navigate task's job is to GET TO the right page or perform an on-page action. Do NOT phrase a navigate task as "...and extract/collect/read/report/find the data" — capturing data is the extract task's job. To gather information from a page, plan a navigate task to reach the page, then a SEPARATE extract task to capture the data.
- extract: extract information users would like to output from the page. When planning such a task, you should not expect anything related to navigation mentioned in the "navigate" task. The website will remain still and the only action here is to extract information from it.
- loop: this task can be used to generate a list of planning sessions like this. When to use a loop task? Use loop when there are multiple parallel tasks you can do with the same goal. Each task in the loop has the same goal but with different objects/values/targets/variables. Use loop task when it's in a "breadth first search" situation where you can go through a list of values and execute the same task for each value. PREFER a loop task whenever the goal repeats the same operation across multiple items, pages, links, rows, or entities — visiting each of N results, collecting the same fields from each of N products, checking each of N pages. One loop handles all N items in a single planned task. Examples:
- When the goal is "Open up to 10 links from an ecomm search result page, and extract information like the price of each product.", loop task should be used to iterate through a list of links or URLs. In each iteration of the loop, the task will go to the linked page and trigger another planning session with the goal of extracting price information of the product
@@ -40,7 +40,7 @@ When in doubt, DO NOT terminate. Try an alternative approach instead.
CRITICAL - Do not declare success on a partially-complete multi-part goal:
- Many goals have multiple required parts/legs (e.g. "find X AND find Y AND find Z"). The goal is achieved ONLY when ALL required parts are done.
- If your own analysis (task_history_information, thoughts, or required_subgoals) identifies ANY required part as missing, unmet, or not yet found, you MUST set user_goal_achieved to false and set plan to the next mini goal that makes progress on the missing part.
-- A required part that needs information is satisfied ONLY when an extract task has actually captured that information. A completed navigate/goto step that merely reached the relevant page does NOT satisfy it; plan an extract task instead.
+- A required part that needs information is satisfied once that information has actually been captured in the task history — normally by an extract task, but also when it is already present as extracted_data on a completed navigate/loop record. A navigate/goto step that merely reached the relevant page WITHOUT capturing the data does NOT satisfy it; when the data is still missing, plan a separate extract task to capture it rather than re-running the navigate.
- Setting user_goal_achieved to false does NOT fail the task — it lets the agent keep working on the missing part. Only set should_terminate to true if a required part is genuinely IMPOSSIBLE per the termination rules above.
CRITICAL - The agent's own final answer is composed at completion, not by a task:
@@ -56,12 +56,12 @@ Reply in JSON format with the following keys:
"extraction_thought": str, // Think step by step. Should any information be extracted given the user goal? If yes, has all the information been extracted? If the user is searching for something, looking for information or specifically trying to extract information along side the goal, consider it an intention to extract information. Phrases like "find something", "show me something", "search something" and so on indicate the intention to extract information.
"require_extraction": bool, // True if the user goal requires information extraction. False otherwise.
"task_history_information": str, // Think step by step. In task history, what information has been collected that's helpful and relevant to the user goal, and what information is missing if any.
- "information_extracted": optional[bool], // True if the needed information has been extracted. False if the needed information has not been extracted. If task history has no "extract" type, that means no data extraction has happened, return false. Null if the user goal does not require information extraction.
+ "information_extracted": optional[bool], // True if the needed information has been captured in the task history — by an "extract" type task, OR present as extracted_data on a completed navigate/loop record. False if the needed information is not present anywhere in the task history. Null if the user goal does not require information extraction.
"thoughts": str, // Think step by step. What has been done so far and what is the next reasonable mini goal a human can do foreseeably move towards the overall goal. If the goal appears impossible to achieve, explain why.
"required_subgoals": array, // Decompose the user goal into its distinct required parts/legs. For a multi-part goal (e.g. "do A, and B, and C"), list each required part separately. For each part, decide whether the task history and current page actually satisfy it.
[{
"subgoal": str, // one required part of the user goal
- "satisfied": bool, // true ONLY if this part is fully met based on task history / extracted information. A part that requires information is satisfied ONLY when an extract task has actually captured that information; a completed navigate/visit to the relevant page alone does NOT satisfy it.
+ "satisfied": bool, // true ONLY if this part is fully met based on task history / extracted information. A part that requires information is satisfied once that information has actually been captured in task history — by an extract task, or already present as extracted_data on a completed navigate/loop record. A navigate/visit that merely reached the relevant page WITHOUT capturing the data does NOT satisfy it.
"evidence": str // brief evidence it is satisfied, or a description of what is still missing
}],
"user_goal_achieved": bool, // True ONLY if EVERY entry in required_subgoals is satisfied. If task_history_information OR any entry in required_subgoals indicates a required part is missing, unmet, or not yet found, this MUST be false. Never set this true while simultaneously acknowledging in thoughts or task_history_information that a required part is missing. If the user wants to extract information and it has not been done, the user goal is not achieved.
diff --git a/skyvern/forge/prompts/skyvern/task_v2_check_completion.j2 b/skyvern/forge/prompts/skyvern/task_v2_check_completion.j2
index e402904ee..581d5c0e5 100644
--- a/skyvern/forge/prompts/skyvern/task_v2_check_completion.j2
+++ b/skyvern/forge/prompts/skyvern/task_v2_check_completion.j2
@@ -6,12 +6,12 @@ Reply in JSON format with the following keys:
"extraction_thought": str, // Think step by step. Should any information be extracted given the user goal? If yes, has all the information been extracted? If the user is searching for something, looking for information or specifically trying to extract information along side the goal, consider it an intention to extract information. Phrases like "find something", "show me something", "search something" and so on indicate the intention to extract information.
"require_extraction": bool, // True if the user goal requires information extraction. False otherwise.
"task_history_information": str, // Think step by step. In task history, what information has been collected that's helpful and relevant to the user goal, and what information is missing if any.
- "information_extracted": optional[bool], // True if the needed information has been extracted. False if the needed information has not been extracted (no extract task in history). Null if the user goal does not require information extraction.
+ "information_extracted": optional[bool], // True if the needed information has been captured in the task history — by an extract task, OR present as extracted_data on a completed navigate/loop record. False if it has not been captured anywhere in history. Null if the user goal does not require information extraction.
"thoughts": str, // Think step by step. Would completing the tasks in the task history be good enough to achieve the user goal? If more tasks need to be completed to achieve the goal, what would be the next task? If the goal appears impossible, explain why.
"required_subgoals": array, // Decompose the user goal into its distinct required parts/legs. For a multi-part goal (e.g. "do A, and B, and C"), list each required part separately. For each part, decide whether the task history and current page actually satisfy it.
[{
"subgoal": str, // one required part of the user goal
- "satisfied": bool, // true ONLY if this part is fully met based on task history / extracted information
+ "satisfied": bool, // true ONLY if this part is fully met based on task history / extracted information. A part that requires information is satisfied once that information has actually been captured in task history — by an extract task, or already present as extracted_data on a completed navigate/loop record. A navigate that merely reached the page WITHOUT capturing the data does NOT satisfy it.
"evidence": str // brief evidence it is satisfied, or a description of what is still missing
}],
"user_goal_achieved": bool, // True ONLY if EVERY required subgoal is satisfied. If task_history_information OR any entry in required_subgoals indicates a required part is missing, unmet, or not yet found, this MUST be false. Never set this true while simultaneously acknowledging that a required part is missing. If the user wants to extract information and it has not been done, the goal is not achieved. If info extraction is not required, use the task history, assisted by the screenshot to decide if every required part has been achieved.
diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.py
index 181a85785..6405f705b 100644
--- a/skyvern/services/task_v2_service.py
+++ b/skyvern/services/task_v2_service.py
@@ -68,6 +68,7 @@ from skyvern.schemas.workflows import (
from skyvern.services.webhook_delivery import deliver_webhook_with_retries
from skyvern.utils.prompt_engine import load_prompt_with_elements
from skyvern.utils.strings import generate_random_string
+from skyvern.webeye.actions.actions import ActionType
from skyvern.webeye.browser_state import BrowserState
from skyvern.webeye.scraper.scraped_page import ScrapedPage
from skyvern.webeye.utils.page import SkyvernFrame
@@ -79,6 +80,13 @@ RANDOM_STRING_POOL = string.ascii_letters + string.digits
# This limits how many times the LLM can plan and execute actions
DEFAULT_MAX_ITERATIONS = 50
+# Cap a navigate block's recovered terminal output: task_history is re-fed to the
+# planner every iteration, so an unbounded reasoning blob would grow context.
+NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS = 2000
+# Structured (dict/list) output can't be sliced without corrupting it; one that exceeds
+# this is dropped entirely rather than written uncapped to task_history.
+NAVIGATE_STRUCTURED_OUTPUT_MAX_CHARS = 10 * NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS
+
def _generate_data_extraction_schema_for_loop(loop_values_key: str) -> dict:
return {
@@ -1049,6 +1057,30 @@ async def run_task_v2_helper(
task_v2_id=task_v2_id,
workflow_run_id=workflow_run_id,
)
+ # Fallback for navigate: recover from the COMPLETE action only when extraction was
+ # expected but came back null (extracted_information present and None). A navigate
+ # block's output is always a TaskOutput dump, so the key is always present here.
+ block_output = block_result.output_parameter_value
+ if (
+ extracted_data is None
+ and task_type == "navigate"
+ and block_result.success
+ and isinstance(block_output, dict)
+ and "extracted_information" in block_output
+ and block_output["extracted_information"] is None
+ ):
+ extracted_data = await _get_navigate_complete_output(
+ block_result,
+ organization_id,
+ task_v2_id=task_v2_id,
+ workflow_run_id=workflow_run_id,
+ )
+ if extracted_data is not None:
+ LOG.info(
+ "Recovered navigate terminal output from COMPLETE action",
+ task_v2_id=task_v2_id,
+ workflow_run_id=workflow_run_id,
+ )
if extracted_data is not None:
task_history_record["extracted_data"] = extracted_data
task_history.append(task_history_record)
@@ -1923,14 +1955,14 @@ def _get_extracted_data_from_block_result(
Args:
block_result: The result from block execution
- task_type: Type of task ("extract" or "loop")
+ task_type: Type of task ("extract", "navigate", or "loop")
task_v2_id: Optional ID for logging
workflow_run_id: Optional ID for logging
Returns:
Extracted data if available, None otherwise
"""
- if task_type == "extract":
+ if task_type in ("extract", "navigate"):
if (
isinstance(block_result.output_parameter_value, dict)
and "extracted_information" in block_result.output_parameter_value
@@ -1982,6 +2014,70 @@ def _get_extracted_data_from_block_result(
return None
+async def _get_navigate_complete_output(
+ block_result: BlockResult,
+ organization_id: str | None,
+ task_v2_id: str | None = None,
+ workflow_run_id: str | None = None,
+) -> Any:
+ """Recover a navigate block's terminal answer from its verifying COMPLETE action.
+
+ Falls back through response, output, then reasoning, capping str candidates.
+ """
+ if not isinstance(block_result.output_parameter_value, dict):
+ return None
+ task_id = block_result.output_parameter_value.get("task_id")
+ if not task_id:
+ return None
+ try:
+ actions = await app.DATABASE.tasks.get_task_actions(task_id=task_id, organization_id=organization_id)
+ except Exception:
+ LOG.warning(
+ "Failed to load navigate task actions; skipping terminal-output recovery",
+ task_id=task_id,
+ task_v2_id=task_v2_id,
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+ return None
+ for action in reversed(actions):
+ if action.action_type != ActionType.COMPLETE:
+ continue
+ for candidate in (action.response, action.output, action.reasoning):
+ if not candidate:
+ continue
+ if isinstance(candidate, str):
+ if len(candidate) > NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS:
+ LOG.warning(
+ "Truncating navigate terminal output before writing to task_history",
+ task_id=task_id,
+ original_length=len(candidate),
+ cap=NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS,
+ task_v2_id=task_v2_id,
+ workflow_run_id=workflow_run_id,
+ )
+ return candidate[:NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS]
+ return candidate
+ # Structured output (dict/list from CUA engines) can't be sliced without
+ # corrupting it, so drop an oversized one entirely (forcing a separate extract
+ # task) rather than let it bloat the task_history fed to the planner.
+ structured_len = len(str(candidate))
+ if structured_len > NAVIGATE_STRUCTURED_OUTPUT_MAX_CHARS:
+ LOG.warning(
+ "Dropping oversized structured navigate terminal output; expecting a separate extract task",
+ task_id=task_id,
+ approx_length=structured_len,
+ task_v2_id=task_v2_id,
+ workflow_run_id=workflow_run_id,
+ )
+ return None
+ return candidate
+ # Only the terminal COMPLETE is authoritative: if it carries no data,
+ # don't fall back to a stale earlier COMPLETE.
+ return None
+ return None
+
+
async def _summarize_task_v2(
task_v2: TaskV2,
task_history: list[dict],
diff --git a/tests/unit/test_task_v2_navigate_extracted_data.py b/tests/unit/test_task_v2_navigate_extracted_data.py
new file mode 100644
index 000000000..480902d2b
--- /dev/null
+++ b/tests/unit/test_task_v2_navigate_extracted_data.py
@@ -0,0 +1,202 @@
+"""Data captured inside a navigate block must reach task_history.
+
+The _get_navigate_complete_output tests exercise the helper in isolation, so some
+pass block_result shapes the run_task_v2_helper guard would screen out upstream.
+"""
+
+from __future__ import annotations
+
+from datetime import UTC, datetime
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType
+from skyvern.schemas.workflows import BlockResult
+from skyvern.services import task_v2_service
+from skyvern.services.task_v2_service import (
+ NAVIGATE_STRUCTURED_OUTPUT_MAX_CHARS,
+ NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS,
+ _get_extracted_data_from_block_result,
+ _get_navigate_complete_output,
+)
+from skyvern.webeye.actions.actions import CompleteAction, WaitAction
+
+
+def _output_parameter() -> OutputParameter:
+ now = datetime.now(UTC)
+ return OutputParameter(
+ parameter_type=ParameterType.OUTPUT,
+ key="nav_output",
+ output_parameter_id="op_nav",
+ workflow_id="wf_test",
+ created_at=now,
+ modified_at=now,
+ )
+
+
+def _block_result(output_parameter_value: object, success: bool = True) -> BlockResult:
+ return BlockResult(
+ success=success,
+ output_parameter=_output_parameter(),
+ output_parameter_value=output_parameter_value,
+ )
+
+
+def test_navigate_block_extracted_information_is_credited() -> None:
+ block_result = _block_result({"task_id": "tsk_1", "extracted_information": {"price": "$12,952"}})
+ assert _get_extracted_data_from_block_result(block_result, "navigate") == {"price": "$12,952"}
+
+
+def test_navigate_block_without_extracted_information_returns_none() -> None:
+ block_result = _block_result({"task_id": "tsk_1", "extracted_information": None})
+ assert _get_extracted_data_from_block_result(block_result, "navigate") is None
+
+
+async def test_navigate_complete_output_prefers_response_then_output_then_reasoning(monkeypatch) -> None:
+ actions = [
+ WaitAction(task_id="tsk_1"),
+ CompleteAction(task_id="tsk_1", reasoning="reached the page", response="RAV4 2015 XLE AWD $12,952"),
+ ]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1", "extracted_information": None})
+ result = await _get_navigate_complete_output(block_result, organization_id="o_1")
+ assert result == "RAV4 2015 XLE AWD $12,952"
+
+
+async def test_navigate_complete_output_uses_output_when_response_empty(monkeypatch) -> None:
+ actions = [
+ CompleteAction(task_id="tsk_1", response=None, output={"price": "$5,522"}, reasoning="on the page"),
+ ]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ result = await _get_navigate_complete_output(block_result, organization_id="o_1")
+ assert result == {"price": "$5,522"}
+
+
+async def test_navigate_complete_output_falls_back_to_reasoning(monkeypatch) -> None:
+ actions = [
+ CompleteAction(task_id="tsk_1", reasoning="Found cheapest RAV4: $12,952 at a dealer"),
+ ]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ result = await _get_navigate_complete_output(block_result, organization_id="o_1")
+ assert result == "Found cheapest RAV4: $12,952 at a dealer"
+
+
+async def test_navigate_complete_output_uses_terminal_complete_action(monkeypatch) -> None:
+ actions = [
+ CompleteAction(task_id="tsk_1", reasoning="first attempt, wrong page"),
+ WaitAction(task_id="tsk_1"),
+ CompleteAction(task_id="tsk_1", reasoning="final answer: $5,522 Prius"),
+ ]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ result = await _get_navigate_complete_output(block_result, organization_id="o_1")
+ assert result == "final answer: $5,522 Prius"
+
+
+async def test_navigate_complete_output_does_not_fall_back_past_dataless_terminal(monkeypatch) -> None:
+ # Only the terminal COMPLETE is authoritative: a data-less terminal action
+ # returns None even if an earlier COMPLETE carried data.
+ actions = [
+ CompleteAction(task_id="tsk_1", reasoning="earlier answer: $5,522 Prius"),
+ CompleteAction(task_id="tsk_1", response=None, output=None, reasoning=None),
+ ]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ assert await _get_navigate_complete_output(block_result, organization_id="o_1") is None
+
+
+async def test_navigate_complete_output_caps_long_reasoning(monkeypatch) -> None:
+ long_reasoning = "x" * (NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS + 500)
+ actions = [CompleteAction(task_id="tsk_1", reasoning=long_reasoning)]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ result = await _get_navigate_complete_output(block_result, organization_id="o_1")
+ assert result == "x" * NAVIGATE_TERMINAL_OUTPUT_MAX_CHARS
+
+
+async def test_navigate_complete_output_drops_oversized_structured_output(monkeypatch) -> None:
+ big_list = ["row"] * NAVIGATE_STRUCTURED_OUTPUT_MAX_CHARS # str() far exceeds the cap
+ actions = [CompleteAction(task_id="tsk_1", output=big_list)]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ assert await _get_navigate_complete_output(block_result, organization_id="o_1") is None
+
+
+async def test_navigate_complete_output_keeps_small_structured_output(monkeypatch) -> None:
+ actions = [CompleteAction(task_id="tsk_1", output={"price": "$5,522"})]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ assert await _get_navigate_complete_output(block_result, organization_id="o_1") == {"price": "$5,522"}
+
+
+async def test_navigate_complete_output_none_without_complete_action(monkeypatch) -> None:
+ actions = [WaitAction(task_id="tsk_1")]
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(return_value=actions)))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ assert await _get_navigate_complete_output(block_result, organization_id="o_1") is None
+
+
+async def test_navigate_complete_output_none_on_db_error(monkeypatch) -> None:
+ fake_app = SimpleNamespace(
+ DATABASE=SimpleNamespace(tasks=SimpleNamespace(get_task_actions=AsyncMock(side_effect=RuntimeError("db down"))))
+ )
+ monkeypatch.setattr(task_v2_service, "app", fake_app)
+
+ block_result = _block_result({"task_id": "tsk_1"})
+ assert await _get_navigate_complete_output(block_result, organization_id="o_1") is None
+
+
+async def test_navigate_complete_output_none_when_no_task_id() -> None:
+ assert await _get_navigate_complete_output(_block_result(None), organization_id="o_1") is None
+ assert await _get_navigate_complete_output(_block_result({}), organization_id="o_1") is None