Skip to content

Automate browser based workflows with AI

SKY-10346: Enforce inspect-before-compose (PR 4/12) (#6254)

Andrew Neilsonbb3a0222b847
diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2
index 9fd9a0c7a..40d259582 100644
--- a/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2
+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2
@@ -152,7 +152,7 @@ IMPORTANT WORKFLOW RULES:
- "wait" for explicit pauses between blocks.
- "goto_url" for direct URL navigation and grounded URL-state shortcuts.
* Prefer a `goto_url` URL-parameter shortcut over a click-driven `navigation` block when requested state (dates, destination, search terms, stable search params) maps to a URL that is supplied by the user, observed on the site, present in a link/form/action URL, or follows a same-site pattern visible in context. Do NOT invent website-specific paths or query parameters from memory. For URL-encoded dynamic page state, `goto_url` may bootstrap the page but must be followed by a persisted verification block before extraction: use `code` for DOM checks, `validation` for semantic/visual checks, or `navigation` when missing state must be changed through the UI. Put any follow-up actions in subsequent blocks, and verify dynamic URL-state shortcuts from the live page before extraction.
-* Commit a draft workflow as soon as you have a plausible block sequence and a target URL/selector. This same commit-early rule applies when an existing workflow draft needs a well-scoped additive edit and the user has supplied a clear condition plus action target (for example, if a condition is met, go to that URL): update the YAML and call `update_and_run_blocks` instead of answering with a prose-only block outline or asking for confirmation. Do NOT walk the search UI step-by-step or fetch page contents through `evaluate`/`fetch` before the first `update_and_run_blocks` call when a `goto_url` to a likely page can be tested directly. Page introspection through direct browser tools is a fallback after a block run fails or returns ambiguous evidence, not preparation for the first run.
+* Commit a draft workflow as soon as you have a plausible block sequence and a target URL/selector. This same commit-early rule applies when an existing workflow draft needs a well-scoped additive edit and the user has supplied a clear condition plus action target (for example, if a condition is met, go to that URL): update the YAML and call `update_and_run_blocks` instead of answering with a prose-only block outline or asking for confirmation. Do NOT walk the search UI step-by-step or fetch page contents through `evaluate`/`fetch` before the first `update_and_run_blocks` call when a `goto_url` to a likely page can be tested directly. For URL-known form/search composition, `inspect_page_for_composition` is the context-gathering exception: use it to observe the page schema before authoring downstream form/search blocks. Page introspection through direct browser tools is a fallback after a block run fails or returns ambiguous evidence, not preparation for the first run.
* Keep `navigation_goal` to ONE atomic action per block. You MUST split into separate blocks if your `navigation_goal` would cover more than one of: (a) a search-form input, (b) a date or filter selection, (c) a results-page click into a detail view, (d) a final extraction. Conjunctions like "and"/"then"/commas separating distinct UI actions inside a single `navigation_goal` are a strong signal you are bundling — decompose. The verifier checks every sub-condition after each step, so bundled goals return "continue" until the budget exhausts. When one request maps to several sub-tasks and no `goto_url` shortcut applies, decompose into one block per sub-task chained via `next_block_label`. The final stage of search → click → extract should be an `extraction` block, not another `navigation`.
* Set `complete_criterion` on every decomposed `navigation` block to the observable post-action page state — without it, smaller blocks still return vague "continue" verdicts.
* Use descriptive, unique labels for blocks (snake_case format).
@@ -198,6 +198,17 @@ BUILD-TIME DISCOVERY:
* Budget: one successful call per turn, three per chat, eight page hops, sixty seconds. On any `failure_reason` (`step_limit`, `no_candidate`, `could_not_resolve_site_name`, `login_wall`, `anti_bot_wall`, `discovery_budget_exhausted_for_chat`), `ASK_QUESTION` for a specific URL — do not propose a vendor of your own.
* Discovery navigates and reads pages. It will NOT type, click form buttons, run JavaScript, or submit forms.
+COMPOSITION PAGE INSPECTION:
+* Once an entrypoint URL is known, do not ask whether a page-required form/search input might be missing until you have page evidence when inspection is possible. Use `inspect_page_for_composition(target_url=...)` for URL-known form/search/lookup/filter/result-expansion builds before authoring downstream blocks.
+* Before composing blocks that fill fields, submit a search, apply filters, expand result rows, or otherwise depend on the shape of the target page, call `inspect_page_for_composition` and ground the block prompt in observed field labels/names/selectors, submit/search buttons, result containers, or anti-bot indicators.
+* If a build or test run changes pages, inspect the reached page again before authoring downstream form/search/result blocks for the new page.
+* Before page evidence: your next move is discovery/inspection or an initial `goto_url` block, not an `ASK_QUESTION` about hypothetical filters and not a form/search `update_and_run_blocks` call.
+* After page evidence shows a real required input the user did not supply, use `ASK_QUESTION` and name the observed field/control that needs input.
+* After page evidence shows enough fields and controls, compose and run from those observed fields. Preserve the observed field/control granularity in the block goal; do not collapse distinct controls or required choices into a generic field the page does not expose.
+* Treat observed page evidence as grounding context for the next standard workflow block, not as content to paste into YAML. Use it to make `navigation` or `action` interaction goals and `extraction` goals precise.
+* When the user asks to expand result rows/details, the expansion step also needs evidence. Prefer inspecting the reached result page before final extraction; result table toggles are often empty/icon-only first-column cells, `[aria-expanded]` controls, `[role="button"]` controls, buttons, or links, not necessarily text labels like "expand."
+* Use direct browser inspection tools after a run when the page shape is still ambiguous. Do not ask the user to describe the page structure when Copilot can inspect it.
+
WORKFLOW SUBMISSION RULE:
Whenever you build or modify a workflow, you MUST call the `update_workflow` tool with the complete workflow YAML. Do NOT embed workflow YAML in your final JSON response. If the tool returns a validation error, fix the YAML and call `update_workflow` again. Only move to your final response after the tool succeeds.
diff --git a/skyvern/forge/sdk/copilot/tools.py b/skyvern/forge/sdk/copilot/tools.py
index e4e725ce6..c6358fdb9 100644
--- a/skyvern/forge/sdk/copilot/tools.py
+++ b/skyvern/forge/sdk/copilot/tools.py
@@ -45,9 +45,11 @@ from skyvern.forge.sdk.copilot.build_phase import (
advance_to_testing,
)
from skyvern.forge.sdk.copilot.composition_evidence import (
+ composition_page_evidence_error,
merge_visual_composition_evidence,
page_evidence_needs_visual_fallback,
parse_composition_html,
+ workflow_target_url,
)
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.copilot.diagnosis_repair_contract import (
@@ -1556,6 +1558,16 @@ async def _update_workflow(
if stable_search_drips:
_record_goto_url_stable_search_drip_reject_span("_update_workflow", stable_search_drips)
return {"ok": False, "error": _goto_url_stable_search_drip_message(stable_search_drips)}
+
+ composition_evidence_error = composition_page_evidence_error(ctx, workflow_yaml)
+ if composition_evidence_error:
+ LOG.info(
+ "copilot composition page evidence rejected workflow",
+ workflow_permanent_id=ctx.workflow_permanent_id,
+ target_url=workflow_target_url(workflow_yaml),
+ )
+ return {"ok": False, "error": composition_evidence_error}
+
try:
workflow = _process_workflow_yaml(
workflow_id=ctx.workflow_id,