Skip to content

Automate browser based workflows with AI

feat(SKY-12303): decouple spine imposition from first authoring call; gate landing on download-reached (#7316)

Andrew Neilsond2edf46b2375
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.py
index 5808d5fdd..eb731f629 100644
--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py
+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py
@@ -22,6 +22,8 @@ from dataclasses import dataclass, field
from typing import Any, NamedTuple
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
+import structlog
+
from skyvern.forge.sdk.copilot.composition_evidence import SCOUT_INTERACTION_EVIDENCE_TOOL
from skyvern.forge.sdk.copilot.output_extraction_plan import (
FrozenRequestedOutputExtractionCandidate,
@@ -33,6 +35,8 @@ from skyvern.forge.sdk.copilot.output_extraction_plan import (
from skyvern.forge.sdk.copilot.reached_download_target import ReachedDownloadTarget
from skyvern.utils.strings import escape_code_fences
+LOG = structlog.get_logger()
+
_MAX_STEPS = 60
_INDENT = " "
_DOMCONTENTLOADED = "domcontentloaded"
@@ -274,6 +278,8 @@ class SynthesisDiagnostics:
# retained trajectory indices. Diagnostics-only, never serialized.
emitted_interactions: list[dict[str, Any]] = field(default_factory=list)
forgiven_interactions: list[dict[str, Any]] = field(default_factory=list)
+ download_terminal_anchor: int | None = None
+ download_terminal_dropped_trailing: int = 0
locator_provenance: list[dict[str, Any]] = field(default_factory=list)
# (trajectory enumerate index -> minted type_text parameter key); diagnostics-only, never serialized.
# Recovers the key for a typed field whose value was withheld from default_value (typed_value == "").
@@ -759,6 +765,23 @@ def _post_auth_resume_locator(trajectory: Sequence[Mapping[str, Any]], *, strict
return "", -1
+def _trajectory_prefix_at_anchor(
+ trajectory: Sequence[Mapping[str, Any]], anchor: int | None
+) -> tuple[Sequence[Mapping[str, Any]], int]:
+ """Cut the trajectory at the position where the download affordance was observed; interactions captured
+ after it navigate away, so replaying them would leave the terminal click on a page without the target."""
+ if anchor is None:
+ return trajectory, 0
+ prefix = [
+ interaction
+ for interaction in trajectory
+ if not isinstance(interaction.get("trajectory_index"), int) or int(interaction["trajectory_index"]) <= anchor
+ ]
+ if not prefix or len(prefix) == len(trajectory):
+ return trajectory, 0
+ return prefix, len(trajectory) - len(prefix)
+
+
def synthesize_code_block(
trajectory: Sequence[Mapping[str, Any]],
*,
@@ -783,6 +806,18 @@ def synthesize_code_block(
and not reached_download_target.already_registered
and bool(reached_download_target.selector)
)
+ if compile_download_target and reached_download_target is not None:
+ trajectory, dropped_trailing = _trajectory_prefix_at_anchor(
+ trajectory, reached_download_target.trajectory_anchor
+ )
+ if dropped_trailing:
+ diagnostics.download_terminal_anchor = reached_download_target.trajectory_anchor
+ diagnostics.download_terminal_dropped_trailing = dropped_trailing
+ LOG.info(
+ "copilot_spine_download_terminal_sequenced",
+ anchor=reached_download_target.trajectory_anchor,
+ dropped_trailing_count=dropped_trailing,
+ )
def append_step(description: str, action_type: str, line_start: int) -> None:
steps.append(
diff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.py
index cfd020726..e84fc4184 100644
--- a/skyvern/forge/sdk/copilot/enforcement.py
+++ b/skyvern/forge/sdk/copilot/enforcement.py
@@ -106,7 +106,6 @@ from skyvern.forge.sdk.copilot.output_utils import (
looks_like_workflow_delivery_claim,
parse_final_response,
)
-from skyvern.forge.sdk.copilot.reached_download_target import ReachedDownloadTarget
from skyvern.forge.sdk.copilot.request_policy import (
REGISTERED_DOWNLOAD_REQUESTED_OUTPUT_PATHS,
CompletionCriterion,
@@ -2345,6 +2344,8 @@ def synthesized_persistence_reopened(ctx: AgentContext) -> bool:
return True
if ctx.synthesized_block_reopened_for_credential_scout:
return True
+ if synthesized_goal_completion_landing_pending(ctx):
+ return True
return synthesized_persistence_reopened_after_failed_run(ctx)
@@ -2553,23 +2554,14 @@ def _credential_flow_scout_gap_incomplete(ctx: Any, trajectory: list[Any]) -> bo
return bool(gap.missing_fields) or gap.missing_submit
-def synthesized_trajectory_is_goal_complete(ctx: Any) -> bool:
- """Complete once the scout trajectory covers a durable entry followed by a commit (or a reached download target)
- with no requested-output path left uncovered and no engaged credential flow left unscouted; empty requested-output
- and credential-fill sets fall through to the shape heuristic byte-identically."""
- if uncovered_requested_output_paths(ctx):
- return False
- requested_paths = _requested_output_paths_for_ctx(ctx)
- plan = requested_output_extraction_plan(ctx)
- if requested_paths and (plan is None or not requested_paths.issubset(set(plan.requested_output_paths))):
- return False
- trajectory = getattr(ctx, "scout_trajectory", None)
- if not isinstance(trajectory, list) or not trajectory:
- return False
- if _credential_flow_scout_gap_incomplete(ctx, trajectory):
+def synthesized_trajectory_reaches_goal(ctx: AgentContext) -> bool:
+ """The scout trajectory covers a durable entry followed by a commit, or a reached download target with a selector.
+ Monotone in what the scout captured, so it cannot flip as the requested-output extraction plan materializes."""
+ trajectory = ctx.scout_trajectory
+ if not trajectory:
return False
- download = getattr(ctx, "reached_download_target", None)
- if isinstance(download, ReachedDownloadTarget) and download.selector:
+ download = ctx.reached_download_target
+ if download is not None and download.selector:
return True
last_entry_index: int | None = None
for index, item in enumerate(trajectory):
@@ -2585,6 +2577,44 @@ def synthesized_trajectory_is_goal_complete(ctx: Any) -> bool:
)
+def _request_expects_unreached_download(ctx: AgentContext) -> bool:
+ # A registered-download deliverable is confirmable only post-run, so it is absent from the pre-run
+ # requested-output gate — a goal-reaching prefix (e.g. sign-in) would otherwise read goal-complete
+ # before the scout reaches the download and land the latch on a partial spine.
+ download = ctx.reached_download_target
+ if download is not None and download.selector:
+ return False
+ return any(criterion.deliverable_kind == "registered_download" for criterion in _active_completion_criteria(ctx))
+
+
+def synthesized_trajectory_is_goal_complete(ctx: AgentContext) -> bool:
+ """A goal-reaching trajectory with no requested-output path left uncovered; an empty requested-output set falls
+ through to the reach shape byte-identically, so an entry ``synthesize_code_block`` would drop never counts."""
+ if uncovered_requested_output_paths(ctx):
+ return False
+ if _request_expects_unreached_download(ctx):
+ return False
+ requested_paths = _requested_output_paths_for_ctx(ctx)
+ plan = requested_output_extraction_plan(ctx)
+ if requested_paths and (plan is None or not requested_paths.issubset(set(plan.requested_output_paths))):
+ return False
+ if _credential_flow_scout_gap_incomplete(ctx, ctx.scout_trajectory):
+ return False
+ return synthesized_trajectory_reaches_goal(ctx)
+
+
+def synthesized_goal_completion_landing_pending(ctx: AgentContext) -> bool:
+ """A goal-complete scout trajectory whose spine has not yet landed in a persisted draft. Only the imposition
+ seam lands a spine and only an authoring call can leave one unlanded, so both are preconditions."""
+ if not ctx.impose_synthesized_code_block:
+ return False
+ if not ctx.update_workflow_called:
+ return False
+ if ctx.synthesized_goal_complete_landed:
+ return False
+ return synthesized_trajectory_is_goal_complete(ctx)
+
+
def _has_unconsumed_output_contract_advisory_grant(ctx: Any) -> bool:
states = getattr(ctx, "output_contract_actuation_by_signature", None)
if not isinstance(states, dict):
diff --git a/skyvern/forge/sdk/copilot/reached_download_target.py b/skyvern/forge/sdk/copilot/reached_download_target.py
index 234523c74..a66fb0927 100644
--- a/skyvern/forge/sdk/copilot/reached_download_target.py
+++ b/skyvern/forge/sdk/copilot/reached_download_target.py
@@ -74,6 +74,9 @@ class ReachedDownloadTarget:
download_kind: DownloadKind
source_step: str
already_registered: bool
+ # Stored ``trajectory_index`` of the last scouted interaction at the moment the affordance was
+ # observed; the synthesizer sequences the download terminal here instead of after the whole trajectory.
+ trajectory_anchor: int | None = None
def to_dict(self) -> dict[str, Any]:
return {
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index c7979b0da..0d89e4583 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -430,6 +430,16 @@ class AgentContext:
synthesized_block_offered_trajectory_len: int = 0
synthesized_block_offered_goal_complete: bool = False
requested_output_extraction_candidate: FrozenRequestedOutputExtractionCandidate | None = None
+ # Candidate frozen by an imposition that has not been persisted yet; promoted to the committed
+ # candidate only once the update it rode in on succeeds.
+ pending_requested_output_extraction_candidate: FrozenRequestedOutputExtractionCandidate | None = None
+ # Set by the imposition seam when a goal-complete spine is on its way into a draft; the successful update
+ # promotes it to the landed latch only when the persisted draft covers the freshly scouted spine.
+ pending_goal_complete_landing: bool = False
+ synthesized_goal_complete_landed: bool = False
+ # Imposition answered this persist attempt on a goal-complete trajectory, so the persist-seam under-build
+ # guard (the fallback for a non-rewriting imposition) must not also answer for it.
+ spine_imposition_owned_attempt: bool = False
synthesized_block_reopened_after_failed_run: bool = False
synthesized_block_reopened_for_output_coverage: bool = False
synthesized_block_reopened_for_credential_scout: bool = False
diff --git a/skyvern/forge/sdk/copilot/tools/scouting.py b/skyvern/forge/sdk/copilot/tools/scouting.py
index a5cdefa0c..0d5581a24 100644
--- a/skyvern/forge/sdk/copilot/tools/scouting.py
+++ b/skyvern/forge/sdk/copilot/tools/scouting.py
@@ -5,6 +5,7 @@ import hashlib
import json
import re
import time
+from dataclasses import replace
from typing import Any, Literal, cast
from urllib.parse import urlparse
@@ -1369,6 +1370,18 @@ def _register_reached_download_scout_interaction(ctx: AgentContext, target: Reac
)
+def _with_trajectory_anchor(ctx: AgentContext, target: ReachedDownloadTarget) -> ReachedDownloadTarget:
+ """Pin the target to the trajectory position where the affordance was observed, using the stored
+ ``trajectory_index`` rather than the list position so the anchor survives trajectory eviction."""
+ trajectory = list(ctx.scout_trajectory)
+ if not trajectory:
+ return target
+ anchor = trajectory[-1].get("trajectory_index")
+ if not isinstance(anchor, int):
+ return target
+ return replace(target, trajectory_anchor=anchor)
+
+
async def _maybe_attach_reached_download_target(
ctx: AgentContext,
result: dict[str, Any],
@@ -1400,7 +1413,7 @@ async def _maybe_attach_reached_download_target(
data["reached_download_guidance"] = _reached_download_guidance_for(target)
if not target.already_registered:
# The pure synthesizer compiles the terminal expect_download step from this typed object.
- ctx.reached_download_target = target
+ ctx.reached_download_target = _with_trajectory_anchor(ctx, target)
if ctx.synthesized_block_offered and not ctx.update_workflow_called:
# The prompt-side offer latched before this download target resolved, so it rendered the
# non-download idiom. Reopen the latch once so the post-turn fallback re-fires carrying it.
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index 6d1da5552..803a5fcac 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -97,8 +97,11 @@ from skyvern.forge.sdk.copilot.enforcement import (
arm_credential_scout_reopen,
requested_output_extraction_plan,
requested_output_extraction_plan_changed,
+ synthesized_goal_completion_landing_pending,
synthesized_persistence_reopened,
synthesized_persistence_reopened_after_failed_run,
+ synthesized_trajectory_is_goal_complete,
+ synthesized_trajectory_reaches_goal,
)
from skyvern.forge.sdk.copilot.loop_detection import clear_failed_step_tracker_for_tools_in_ctx
from skyvern.forge.sdk.copilot.narration import CODE_REPAIR_PROGRESS_SURFACE_KIND, CODE_REPAIR_PROGRESS_TEXT
@@ -113,6 +116,7 @@ from skyvern.forge.sdk.copilot.output_contracts import (
declared_string_workflow_parameter_keys,
resolve_output_contract_actuation,
)
+from skyvern.forge.sdk.copilot.output_extraction_plan import FrozenRequestedOutputExtractionCandidate
from skyvern.forge.sdk.copilot.output_policy import (
OutputPolicyReason,
OutputPolicyVerdict,
@@ -5469,21 +5473,45 @@ def _author_time_reject_reopens_synthesized_imposition(ctx: AgentContext) -> boo
)
+def _reopen_download_target_registers(ctx: AgentContext) -> bool:
+ target = ctx.reached_download_target
+ return isinstance(target, ReachedDownloadTarget) and not target.already_registered and bool(target.selector.strip())
+
+
def _log_imposition_skipped_after_update(ctx: AgentContext) -> None:
scout_trajectory = ctx.scout_trajectory
if not scout_trajectory:
return
- target = ctx.reached_download_target
LOG.info(
"copilot_imposition_skipped_after_update",
trajectory_length=len(scout_trajectory),
- reopen_download_target=(
- isinstance(target, ReachedDownloadTarget)
- and not target.already_registered
- and bool(target.selector.strip())
- ),
+ reopen_download_target=_reopen_download_target_registers(ctx),
reopen_persistence_after_failed_run=synthesized_persistence_reopened_after_failed_run(ctx),
reopen_author_time_reject=_author_time_reject_reopens_synthesized_imposition(ctx),
+ reaches_goal=synthesized_trajectory_reaches_goal(ctx),
+ goal_complete=synthesized_trajectory_is_goal_complete(ctx),
+ synthesized_goal_complete_landed=ctx.synthesized_goal_complete_landed,
+ )
+
+
+def _imposition_admission_key_after_update(ctx: AgentContext) -> str:
+ if synthesized_goal_completion_landing_pending(ctx):
+ return "goal_completion_landing_pending"
+ if synthesized_trajectory_reaches_goal(ctx) and not ctx.synthesized_goal_complete_landed:
+ return "goal_reaching_spine_unlanded"
+ if _reopen_download_target_registers(ctx):
+ return "reopen_download_target"
+ if synthesized_persistence_reopened_after_failed_run(ctx):
+ return "reopen_failed_run"
+ return "reopen_author_time_reject"
+
+
+def _log_imposition_admitted_after_update(ctx: AgentContext) -> None:
+ LOG.info(
+ "copilot_imposition_admitted_after_update",
+ admission_key=_imposition_admission_key_after_update(ctx),
+ trajectory_length=len(ctx.scout_trajectory),
+ goal_complete=synthesized_trajectory_is_goal_complete(ctx),
)
@@ -5536,6 +5564,162 @@ def _select_synthesized_imposition_code_block(
return None
+_ADMISSIBLE_BROWSER_COVERAGE_ROUTE = (
+ "The scouted spine is the browser draft: extend browser coverage by scouting the step so the spine carries it, "
+ "never by authoring browser calls freehand."
+)
+
+
+class _StaleSpineRungReplacement(NamedTuple):
+ violation: str | None
+ replaced_labels: list[str]
+
+
+def _code_block_drives_browser(block: Mapping[str, Any]) -> bool:
+ mutations, _, ambiguous = _browser_surface_for_code(str(block.get("code") or ""))
+ return bool(mutations or ambiguous)
+
+
+def _spine_carrier_code_block(
+ code_blocks: list[dict[str, Any]],
+ *,
+ prior_yaml: str | None,
+) -> dict[str, Any] | None:
+ if not code_blocks:
+ return None
+ changed_blocks = [block for block in code_blocks if _submitted_code_block_changed(block, prior_yaml)]
+ for block in changed_blocks:
+ if _code_block_drives_browser(block):
+ return block
+ return changed_blocks[0] if changed_blocks else code_blocks[0]
+
+
+def _stale_spine_rung_blocks(
+ code_blocks: list[dict[str, Any]],
+ *,
+ carrier: Mapping[str, Any],
+ prior_yaml: str | None,
+) -> list[dict[str, Any]]:
+ """Changed non-carrier browser-driving blocks, each on an owned attempt either a duplicate spine rung or an
+ ungrounded hand-authored one that the spine replaces. Blocks that only parse or extract carry no browser surface
+ and are never touched."""
+ return [
+ block
+ for block in code_blocks
+ if block is not carrier
+ and _submitted_code_block_changed(block, prior_yaml)
+ and _code_block_drives_browser(block)
+ ]
+
+
+def _stale_spine_rung_provenance(
+ stale_rungs: list[dict[str, Any]],
+ *,
+ scouted_mutations: list[_BrowserMutationSignature],
+ diagnostics: SynthesisDiagnostics,
+) -> list[_BrowserSurfaceRejectionProvenance]:
+ """Provenance for the ungrounded actions among the replaced rungs; a rung that only duplicates scouted
+ signatures yields none, which is what distinguishes a duplicate from a hand-authored rung in the record."""
+ scouted_signatures = set(scouted_mutations)
+ provenance: list[_BrowserSurfaceRejectionProvenance] = []
+ for block in stale_rungs:
+ label = _code_block_label(block)
+ mutations, _, ambiguous = _browser_surface_for_code(str(block.get("code") or ""))
+ provenance.extend(
+ _classify_unscouted_mutation(
+ mutation,
+ scouted_mutations=scouted_mutations,
+ diagnostics=diagnostics,
+ site="whole_trajectory",
+ block_label=label,
+ )
+ for mutation in sorted(mutation for mutation in mutations if mutation not in scouted_signatures)
+ )
+ provenance.extend(
+ _ambiguous_browser_action_provenance(action, site="whole_trajectory", block_label=label)
+ for action in sorted(ambiguous)
+ )
+ return provenance
+
+
+def _draft_leaves_scouted_rungs_uncovered(
+ code_blocks: list[dict[str, Any]], *, synthesized: SynthesizedCodeBlock
+) -> bool:
+ draft_calls = [
+ (mutation.method, mutation.receiver)
+ for block in code_blocks
+ for mutation in _browser_surface_for_code(str(block.get("code") or ""))[0]
+ ]
+ return bool(uncovered_required_emitted_interactions(synthesized.diagnostics.emitted_interactions, draft_calls))
+
+
+def _code_block_browser_actions(block: Mapping[str, Any]) -> list[str]:
+ mutations, _, ambiguous = _browser_surface_for_code(str(block.get("code") or ""))
+ return sorted({f"{mutation.receiver}.{mutation.method}" for mutation in mutations} | set(ambiguous))
+
+
+def _workflow_string_scalars(node: Any, *, excluded: Mapping[str, Any]) -> Iterator[str]:
+ if node is excluded:
+ return
+ if isinstance(node, str):
+ yield node
+ elif isinstance(node, Mapping):
+ for value in node.values():
+ yield from _workflow_string_scalars(value, excluded=excluded)
+ elif isinstance(node, list):
+ for item in node:
+ yield from _workflow_string_scalars(item, excluded=excluded)
+
+
+def _code_block_output_is_referenced(parsed: Mapping[str, Any], block: Mapping[str, Any]) -> bool:
+ label = str(block.get("label") or "").strip()
+ if not label:
+ return False
+ output_key = f"{label}_output"
+ return any(output_key in text for text in _workflow_string_scalars(parsed, excluded=block))
+
+
+def _remove_top_level_code_blocks(parsed: dict[str, Any], removed: list[dict[str, Any]]) -> bool:
+ definition = parsed.get("workflow_definition")
+ if not isinstance(definition, dict):
+ return False
+ blocks = definition.get("blocks")
+ if not isinstance(blocks, list):
+ return False
+ if any(not any(existing is block for existing in blocks) for block in removed):
+ return False
+ definition["blocks"] = [block for block in blocks if all(block is not stale for stale in removed)]
+ return True
+
+
+def _drop_stale_spine_rung_blocks(
+ parsed: dict[str, Any],
+ stale_rungs: list[dict[str, Any]],
+ *,
+ carrier_label: str,
+ provenance: list[_BrowserSurfaceRejectionProvenance],
+) -> _StaleSpineRungReplacement:
+ undroppable = [block for block in stale_rungs if _code_block_output_is_referenced(parsed, block)]
+ if not undroppable and _remove_top_level_code_blocks(parsed, stale_rungs):
+ replaced_labels = [_code_block_label(block) for block in stale_rungs]
+ LOG.info(
+ "copilot_spine_stale_rung_dropped",
+ carrier_label=carrier_label,
+ dropped_labels=replaced_labels,
+ dropped_actions=[action for block in stale_rungs for action in _code_block_browser_actions(block)],
+ dropped_provenance=[record._asdict() for record in provenance],
+ )
+ return _StaleSpineRungReplacement(violation=None, replaced_labels=replaced_labels)
+ blocked = ", ".join(f"`{_code_block_label(block)}`" for block in (undroppable or stale_rungs))
+ violation = (
+ f"Unable to impose synthesized code block: the scouted spine belongs on `{carrier_label}`, but {blocked} "
+ f"still drive(s) the browser and cannot be removed automatically. Delete those block(s) and resubmit with "
+ f"the browser spine on a single code block. {_ADMISSIBLE_BROWSER_COVERAGE_ROUTE}"
+ f"{_provenance_suffix_text(provenance)}"
+ )
+ return _StaleSpineRungReplacement(violation=violation, replaced_labels=[])
+
+
def _is_ignorable_entry_opener_drop(dropped: Mapping[str, Any], diagnostics: SynthesisDiagnostics) -> bool:
return (
dropped.get("reason_code") == "ambiguous_bare_selector"
@@ -6353,6 +6537,8 @@ def _persist_seam_spine_under_build_result(
) -> _SynthesizedCodeImpositionResult | None:
if not ctx.impose_synthesized_code_block:
return None
+ if ctx.spine_imposition_owned_attempt:
+ return None
# First-persist drafts stay imposition's concern; this seam guards later persists in a turn that
# already committed one, and the turn-end checkpoint owns coverage for everything else.
if not ctx.update_workflow_called and ctx.persisted_draft_browser_calls is None:
@@ -7552,13 +7738,22 @@ def _split_selected_output_owner_into_browser_stages(
def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext) -> _SynthesizedCodeImpositionResult:
+ ctx.pending_goal_complete_landing = False
+ ctx.pending_requested_output_extraction_candidate = None
+ ctx.spine_imposition_owned_attempt = False
if not getattr(ctx, "impose_synthesized_code_block", False):
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
- if ctx.update_workflow_called and not _should_impose_after_update_attempt(ctx):
+ reaches_goal = synthesized_trajectory_reaches_goal(ctx)
+ # Goal-completeness gates on the requested-output plan, which materializes mid-turn, so keying availability on
+ # it would make ownership race the plan; reach is monotone in the scout's capture. Landing still closes the lane.
+ spine_landing_available = reaches_goal and not ctx.synthesized_goal_complete_landed
+ if ctx.update_workflow_called and not spine_landing_available and not _should_impose_after_update_attempt(ctx):
_log_imposition_skipped_after_update(ctx)
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
+ if ctx.update_workflow_called:
+ _log_imposition_admitted_after_update(ctx)
scout_trajectory = getattr(ctx, "scout_trajectory", None)
if not isinstance(scout_trajectory, list) or not scout_trajectory:
@@ -7577,12 +7772,26 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
prior_yaml=prior_yaml,
preferred_labels=_recorded_outcome_imposition_block_labels(ctx),
)
+ if code_block is None and reaches_goal:
+ code_block = _spine_carrier_code_block(code_blocks, prior_yaml=prior_yaml)
if code_block is None:
+ if reaches_goal:
+ LOG.info("copilot_spine_imposition_no_carrier", code_block_count=len(code_blocks))
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
submitted_code = str(code_block.get("code") or "")
- if not _submitted_code_block_changed(code_block, prior_yaml):
+ owns_spine = reaches_goal
+ ctx.spine_imposition_owned_attempt = owns_spine
+ carrier_changed = _submitted_code_block_changed(code_block, prior_yaml)
+ if not owns_spine and not carrier_changed:
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
+ if owns_spine:
+ LOG.info(
+ "copilot_spine_imposition_owned_attempt",
+ carrier_label=_code_block_label(code_block),
+ code_block_count=len(code_blocks),
+ carrier_changed=carrier_changed,
+ )
extraction_plan = requested_output_extraction_plan(ctx)
extraction_candidate_refresh_allowed = synthesized_persistence_reopened(
@@ -7607,6 +7816,7 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
workflow_yaml=workflow_yaml,
violations=["Unable to impose synthesized code block: scout trajectory produced no runnable code."],
)
+ imposed_candidate: FrozenRequestedOutputExtractionCandidate | None = None
if extraction_plan is not None:
candidate = freeze_requested_output_extraction_candidate(synthesized, extraction_plan, source="generated")
if candidate is None:
@@ -7624,9 +7834,29 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
workflow_yaml=workflow_yaml,
violations=["Unable to impose synthesized code block: extraction candidate identity changed."],
)
- ctx.requested_output_extraction_candidate = candidate
+ imposed_candidate = candidate
synthesized_spine_code = synthesized.interaction_code or synthesized.code
+ if not carrier_changed and not _draft_leaves_scouted_rungs_uncovered(code_blocks, synthesized=synthesized):
+ return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
+ replaced_rung_labels: list[str] = []
+ if owns_spine:
+ stale_rungs = _stale_spine_rung_blocks(code_blocks, carrier=code_block, prior_yaml=prior_yaml)
+ if stale_rungs:
+ replacement = _drop_stale_spine_rung_blocks(
+ parsed,
+ stale_rungs,
+ carrier_label=_code_block_label(code_block),
+ provenance=_stale_spine_rung_provenance(
+ stale_rungs,
+ scouted_mutations=_browser_surface_for_code(synthesized_spine_code)[0],
+ diagnostics=synthesized.diagnostics,
+ ),
+ )
+ if replacement.violation is not None:
+ return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml, violations=[replacement.violation])
+ replaced_rung_labels = replacement.replaced_labels
+ code_blocks = [block for block in code_blocks if all(block is not stale for stale in stale_rungs)]
if _separated_spine_already_imposed(code_blocks, code_block, synthesized_spine_code):
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
@@ -7911,6 +8141,9 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
if under_build is not None:
return under_build
code_block["code"] = imposed_code
+ if replaced_rung_labels:
+ substitutions["replaced_hand_authored_browser_rungs"] = replaced_rung_labels
+ substitutions["browser_coverage_route"] = _ADMISSIBLE_BROWSER_COVERAGE_ROUTE
if parameter_reconciliation.aliases:
substitutions["parameter_aliases"] = parameter_reconciliation.aliases
if extraction_suffix:
@@ -7937,6 +8170,9 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
candidate_source=extraction_candidate_source,
candidate_fingerprint=synthesized.extraction_fingerprint,
)
+ ctx.pending_requested_output_extraction_candidate = imposed_candidate
+ if synthesized_trajectory_is_goal_complete(ctx):
+ ctx.pending_goal_complete_landing = True
return _SynthesizedCodeImpositionResult(
workflow_yaml=yaml.safe_dump(parsed, sort_keys=False),
substitutions=substitutions,
@@ -9809,6 +10045,14 @@ def _record_code_only_raw_secret_reject_span(ctx: AgentContext, verdict: OutputP
pass
+def _persisted_landing_leaves_spine_uncovered(ctx: AgentContext) -> bool:
+ try:
+ return bool(_scouted_spine_open_obligation(ctx))
+ except Exception:
+ LOG.warning("copilot_landing_spine_coverage_read_failed", exc_info=True)
+ return True
+
+
def _record_workflow_update_result(
copilot_ctx: Any, result: dict[str, Any], prior_definition: object | None = None
) -> None:
@@ -9826,6 +10070,13 @@ def _record_workflow_update_result(
if isinstance(block_count, int):
copilot_ctx.last_update_block_count = block_count
copilot_ctx.update_workflow_called = True
+ if copilot_ctx.pending_requested_output_extraction_candidate is not None:
+ copilot_ctx.requested_output_extraction_candidate = copilot_ctx.pending_requested_output_extraction_candidate
+ copilot_ctx.pending_requested_output_extraction_candidate = None
+ if copilot_ctx.pending_goal_complete_landing:
+ if not _persisted_landing_leaves_spine_uncovered(copilot_ctx):
+ copilot_ctx.synthesized_goal_complete_landed = True
+ copilot_ctx.pending_goal_complete_landing = False
copilot_ctx.synthesized_block_reopened_after_failed_run = False
copilot_ctx.synthesized_block_reopened_for_output_coverage = False
copilot_ctx.uncovered_output_rescout_steer_key = None
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index 8c086ca0d..1b91f6b43 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -62,7 +62,12 @@ from skyvern.forge.sdk.copilot.output_contracts import (
)
from skyvern.forge.sdk.copilot.output_utils import sanitize_tool_result_for_llm
from skyvern.forge.sdk.copilot.reached_download_target import ReachedDownloadTarget
-from skyvern.forge.sdk.copilot.request_policy import CompletionCriterion, JudgmentTruthCondition, RequestPolicy
+from skyvern.forge.sdk.copilot.request_policy import (
+ CompletionCriterion,
+ JudgmentTruthCondition,
+ RequestedOutputEvidenceSource,
+ RequestPolicy,
+)
from skyvern.forge.sdk.copilot.run_outcome import TERMINAL_CHALLENGE_BLOCKER_REASON_CODE, RecordedRunOutcome
from skyvern.forge.sdk.copilot.runtime import AgentContext
from skyvern.forge.sdk.copilot.tools import (
@@ -464,7 +469,7 @@ def test_browser_surface_rejects_alias_dynamic_unbounded_and_mutation_forms(code
assert mutations or unscouted or ambiguous
-def test_plan_backed_imposition_executes_generated_and_submitted_live_read_recipe() -> None:
+def _live_read_extraction_ctx() -> CopilotContext:
ctx = _code_only_ctx()
_enable_imposition(ctx)
ctx.request_policy = RequestPolicy(
@@ -508,7 +513,11 @@ def test_plan_backed_imposition_executes_generated_and_submitted_live_read_recip
},
}
]
- submitted = _yaml(
+ return ctx
+
+
+def _live_read_submitted_yaml() -> str:
+ return _yaml(
"""
title: Record lookup
workflow_definition:
@@ -520,6 +529,11 @@ def test_plan_backed_imposition_executes_generated_and_submitted_live_read_recip
"""
)
+
+def test_plan_backed_imposition_executes_generated_and_submitted_live_read_recipe() -> None:
+ ctx = _live_read_extraction_ctx()
+ submitted = _live_read_submitted_yaml()
+
result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
assert result.violations == []
@@ -11298,18 +11312,18 @@ class TestWholeTrajectoryImposition:
assert "getattr" not in code
def test_rejects_extra_changed_block_with_unscouted_browser_mutation(self) -> None:
- ctx = _quote_ctx()
+ ctx = _records_spine_ctx()
submitted = _yaml(
f"""
- title: Quote
+ title: Records
workflow_definition:
blocks:
- block_type: code
label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}
code: |
- await page.locator("#zip").fill(str(zip_code))
- await page.locator("#continue").click()
- await page.locator("#coverage-next").click()
+ await page.locator("#stage-a").click()
+ await page.locator("#stage-b").click()
+ await page.locator("#stage-c").click()
- block_type: code
label: invented_browser_step
code: |
@@ -11317,10 +11331,15 @@ class TestWholeTrajectoryImposition:
"""
)
- result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+ assert ctx.spine_imposition_owned_attempt is False
assert any("unscouted browser action" in violation for violation in result.violations)
+ assert any("never_captured" in violation for violation in result.violations)
assert result.substitutions is None
+ events = [log for log in logs if log["event"] == "copilot_browser_surface_rejection_provenance"]
+ assert events and events[0]["kind"] == "never_captured"
@pytest.mark.parametrize(
"sibling_code",
@@ -11421,12 +11440,43 @@ class TestWholeTrajectoryImposition:
),
],
)
- def test_rejects_ambiguous_browser_mutation(self, sibling_code: str) -> None:
+ def test_owned_attempt_drops_ambiguous_only_sibling(self, sibling_code: str) -> None:
ctx = _quote_ctx()
submitted = _submitted_with_sibling_code(sibling_code)
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.violations == []
+ assert result.substitutions is not None
+ blocks = _code_blocks(parse_workflow_yaml(result.workflow_yaml))
+ assert "preserved_code" not in blocks
+ dropped = [log for log in logs if log["event"] == "copilot_spine_stale_rung_dropped"]
+ assert dropped and dropped[0]["dropped_labels"] == ["preserved_code"]
+ assert dropped[0]["dropped_actions"]
+
+ def test_unowned_attempt_still_rejects_ambiguous_browser_mutation(self) -> None:
+ ctx = _records_spine_ctx()
+ submitted = _yaml(
+ f"""
+ title: Records
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}
+ code: |
+ await page.locator("#stage-a").click()
+ - block_type: code
+ label: helper_stage
+ code: |
+ target = page.locator("#stage-z")
+ await getattr(target, "fill")("2026-07-01")
+ """
+ )
+
result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+ assert ctx.spine_imposition_owned_attempt is False
assert any("ambiguous browser action" in violation for violation in result.violations)
assert result.substitutions is None
@@ -11593,7 +11643,7 @@ class TestWholeTrajectoryImposition:
)
assert result.substitutions is None
- def test_rejects_ambiguous_helper_browser_mutation(self) -> None:
+ def test_owned_attempt_drops_ambiguous_helper_browser_mutation(self) -> None:
ctx = _quote_ctx()
submitted = _yaml(
f"""
@@ -11617,8 +11667,8 @@ class TestWholeTrajectoryImposition:
result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
- assert any("ambiguous browser action" in violation for violation in result.violations)
- assert result.substitutions is None
+ assert result.violations == []
+ assert "helper_step" not in _code_blocks(parse_workflow_yaml(result.workflow_yaml))
def test_multi_screen_trajectory_persists_in_order_with_proven_locators(self) -> None:
ctx = _quote_ctx()
@@ -12036,6 +12086,7 @@ class TestWholeTrajectoryImposition:
def test_author_schema_incompatibility_does_not_reopen_collapsed_code_block(self) -> None:
ctx = _quote_ctx()
ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
ctx.latest_recorded_build_test_outcome = _author_time_reject_outcome("schema_incompatibility")
ctx.workflow_yaml = _yaml(
f"""
@@ -13171,6 +13222,20 @@ class TestImpositionSkippedAfterUpdateRecord:
assert events[0]["reopen_download_target"] is False
assert events[0]["reopen_persistence_after_failed_run"] is False
assert events[0]["reopen_author_time_reject"] is False
+ assert events[0]["reaches_goal"] is False
+
+ def test_reach_admitted_attempt_names_its_own_lane(self) -> None:
+ ctx = _reaching_extraction_ctx()
+ ctx.flow_evidence = []
+ ctx.update_workflow_called = True
+
+ with capture_logs() as logs:
+ workflow_update_module._maybe_impose_synthesized_code_block(_live_read_submitted_yaml(), ctx)
+
+ events = [log for log in logs if log["event"] == "copilot_imposition_admitted_after_update"]
+ assert len(events) == 1
+ assert events[0]["admission_key"] == "goal_reaching_spine_unlanded"
+ assert events[0]["goal_complete"] is False
class TestScoutCaptureParityAccounting:
@@ -13747,6 +13812,7 @@ class TestScoutedSpinePersistSeamCoverage:
async def test_satisfied_credential_precondition_keeps_single_step_route(self) -> None:
ctx = _credential_spine_ctx()
ctx.scout_trajectory.append(_submit_interaction(source_url="https://example.com/records"))
+ ctx.synthesized_goal_complete_landed = True
with capture_logs() as logs:
rejected = await _update_workflow(
@@ -14235,3 +14301,708 @@ class TestCredentialScoutReopenSeam:
assert workflow_update_module._should_impose_after_update_attempt(ctx) is False
ctx.synthesized_block_reopened_for_credential_scout = True
assert workflow_update_module._should_impose_after_update_attempt(ctx) is True
+
+
+def _persisted_workflow_result() -> dict[str, object]:
+ return {
+ "ok": True,
+ "_workflow": SimpleNamespace(
+ workflow_definition=SimpleNamespace(blocks=[SimpleNamespace(label="quote_flow")]),
+ proxy_location=None,
+ ),
+ }
+
+
+def _quote_submitted_yaml() -> str:
+ return _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: quote_flow
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ await page.locator("#continue").click()
+ """
+ )
+
+
+class TestGoalCompletionLandingImposition:
+ def test_goal_complete_trajectory_imposes_after_mid_scout_first_authoring_call(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ assert result.violations == []
+ assert result.substitutions is not None
+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])
+ assert 'page.locator("#coverage-next")' in code
+ assert "expect_download" not in code
+ assert ctx.pending_goal_complete_landing is True
+ assert ctx.synthesized_goal_complete_landed is False
+
+ def test_unregistered_download_target_still_admits_imposition_after_landing(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ assert result.violations == []
+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])
+ assert "async with page.expect_download()" in code
+
+ def test_non_goal_complete_trajectory_still_skips_imposition_after_update(self) -> None:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "click",
+ "selector": "button",
+ "source_url": "https://example.com/search",
+ "trajectory_index": 0,
+ }
+ ]
+ ctx.update_workflow_called = True
+ submitted = _yaml(
+ """
+ title: Search
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: search
+ code: |
+ await page.locator("#other").click()
+ """
+ )
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.workflow_yaml == submitted
+ skipped = [entry for entry in logs if entry["event"] == "copilot_imposition_skipped_after_update"]
+ assert len(skipped) == 1
+ assert skipped[0]["goal_complete"] is False
+ assert skipped[0]["synthesized_goal_complete_landed"] is False
+ assert not [entry for entry in logs if entry["event"] == "copilot_imposition_admitted_after_update"]
+
+ def test_admission_record_names_the_landing_pending_key(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+
+ with capture_logs() as logs:
+ workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ admitted = [entry for entry in logs if entry["event"] == "copilot_imposition_admitted_after_update"]
+ assert len(admitted) == 1
+ assert admitted[0]["admission_key"] == "goal_completion_landing_pending"
+
+ def test_landed_spine_is_not_reimposed_on_resubmission(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ assert result.workflow_yaml == _quote_submitted_yaml()
+ assert result.substitutions is None
+ skipped = [entry for entry in logs if entry["event"] == "copilot_imposition_skipped_after_update"]
+ assert len(skipped) == 1
+ assert skipped[0]["goal_complete"] is True
+ assert skipped[0]["synthesized_goal_complete_landed"] is True
+
+ def test_successful_update_promotes_pending_goal_completion_landing(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+ ctx.persisted_draft_browser_calls = workflow_update_module._workflow_yaml_browser_call_pairs(
+ result.workflow_yaml
+ )
+ workflow_update_module._record_workflow_update_result(ctx, _persisted_workflow_result())
+
+ assert ctx.synthesized_goal_complete_landed is True
+ assert ctx.pending_goal_complete_landing is False
+
+ def test_premature_landing_that_under_builds_does_not_retire_and_reimposes_full_spine(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+
+ workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+ assert ctx.pending_goal_complete_landing is True
+ ctx.persisted_draft_browser_calls = workflow_update_module._workflow_yaml_browser_call_pairs(
+ _quote_submitted_yaml()
+ )
+ workflow_update_module._record_workflow_update_result(ctx, _persisted_workflow_result())
+
+ assert ctx.synthesized_goal_complete_landed is False
+ assert ctx.pending_goal_complete_landing is False
+
+ reimposed = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+ code = str(_single_code_block(parse_workflow_yaml(reimposed.workflow_yaml))["code"])
+ assert 'page.locator("#coverage-next")' in code
+ assert ctx.pending_goal_complete_landing is True
+
+ def test_armed_landing_reads_current_spine_not_arm_snapshot_and_does_not_retire(self) -> None:
+ ctx = _quote_ctx()
+ ctx.pending_goal_complete_landing = True
+ ctx.persisted_draft_browser_calls = workflow_update_module._workflow_yaml_browser_call_pairs(
+ _quote_submitted_yaml()
+ )
+
+ workflow_update_module._record_workflow_update_result(ctx, _persisted_workflow_result())
+
+ assert ctx.synthesized_goal_complete_landed is False
+ assert ctx.pending_goal_complete_landing is False
+
+ reimposed = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+ code = str(_single_code_block(parse_workflow_yaml(reimposed.workflow_yaml))["code"])
+ assert 'page.locator("#coverage-next")' in code
+
+ def test_full_spine_plus_suffix_draft_stays_retired_without_reimposition(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+ submitted = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: quote_flow
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ await page.locator("#continue").click()
+ await page.locator("#coverage-next").click()
+ records = [{"quote": "captured"}]
+ """
+ )
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.workflow_yaml == submitted
+ assert result.substitutions is None
+ assert result.violations == []
+ skipped = [entry for entry in logs if entry["event"] == "copilot_imposition_skipped_after_update"]
+ assert len(skipped) == 1
+ assert skipped[0]["synthesized_goal_complete_landed"] is True
+ assert ctx.synthesized_goal_complete_landed is True
+ assert ctx.pending_goal_complete_landing is False
+
+ def test_successful_update_promotes_pending_extraction_candidate(self) -> None:
+ ctx = _live_read_extraction_ctx()
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_live_read_submitted_yaml(), ctx)
+
+ assert result.violations == []
+ assert ctx.requested_output_extraction_candidate is None
+ pending_candidate = ctx.pending_requested_output_extraction_candidate
+ assert pending_candidate is not None
+
+ workflow_update_module._record_workflow_update_result(ctx, _persisted_workflow_result())
+
+ assert ctx.requested_output_extraction_candidate == pending_candidate
+ assert ctx.pending_requested_output_extraction_candidate is None
+
+ def test_failed_update_leaves_committed_candidate_and_landing_latch_untouched(self) -> None:
+ ctx = _live_read_extraction_ctx()
+
+ workflow_update_module._maybe_impose_synthesized_code_block(_live_read_submitted_yaml(), ctx)
+ workflow_update_module._record_workflow_update_result(ctx, {"ok": False})
+
+ assert ctx.requested_output_extraction_candidate is None
+ assert ctx.synthesized_goal_complete_landed is False
+
+ def test_rejected_imposition_does_not_mutate_committed_candidate(self) -> None:
+ ctx = _live_read_extraction_ctx()
+ workflow_update_module._maybe_impose_synthesized_code_block(_live_read_submitted_yaml(), ctx)
+ workflow_update_module._record_workflow_update_result(ctx, _persisted_workflow_result())
+ committed = ctx.requested_output_extraction_candidate
+ assert committed is not None
+ ctx.synthesized_block_reopened_after_failed_run = True
+
+ unscouted_sibling = _yaml(
+ f"""
+ title: Record lookup
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: {workflow_update_module._SYNTHESIZED_BLOCK_LABEL}
+ code: |
+ await page.locator("#search-submit").click()
+ - block_type: code
+ label: helper_stage
+ code: |
+ await page.locator("#surprise").click()
+ """
+ )
+ result = workflow_update_module._maybe_impose_synthesized_code_block(unscouted_sibling, ctx)
+
+ assert result.violations
+ assert ctx.requested_output_extraction_candidate == committed
+ assert ctx.pending_requested_output_extraction_candidate is None
+
+
+def _hand_authored_rung_yaml() -> str:
+ return _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: enter_zip
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ - block_type: code
+ label: continue_step
+ code: |
+ await page.locator("#continue").click()
+ - block_type: code
+ label: coverage_step
+ code: |
+ await page.locator("#coverage-next").click()
+ """
+ )
+
+
+class TestAdmittedImpositionOwnsSpineCoverage:
+ def test_multi_block_hand_authored_draft_lands_spine_on_carrier_without_stale_rungs(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_hand_authored_rung_yaml(), ctx)
+
+ assert result.violations == []
+ assert result.substitutions is not None
+ parsed = parse_workflow_yaml(result.workflow_yaml)
+ blocks = _code_blocks(parsed)
+ assert list(blocks) == ["enter_zip"]
+ code = str(blocks["enter_zip"]["code"])
+ assert 'page.locator("#continue")' in code
+ assert code.count('page.locator("#coverage-next")') == 1
+ assert ctx.spine_imposition_owned_attempt is True
+
+ def test_referenced_stale_rung_label_steers_instead_of_being_dropped(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ submitted = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: enter_zip
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ - block_type: code
+ label: coverage_step
+ code: |
+ await page.locator("#coverage-next").click()
+ - block_type: code
+ label: summarize
+ code: |
+ return {"output": coverage_step_output}
+ """
+ )
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.substitutions is None
+ assert any("`coverage_step`" in violation for violation in result.violations)
+ assert result.workflow_yaml == submitted
+
+ def test_admitted_imposition_is_not_answered_by_the_persist_seam_guard(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+ ctx.persisted_draft_browser_calls = [("click", 'page.locator("#zip")')]
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_hand_authored_rung_yaml(), ctx)
+
+ assert result.violations == []
+ assert result.substitutions is not None
+ assert ctx.spine_imposition_owned_attempt is True
+ assert workflow_update_module._persist_seam_spine_under_build_result(result.workflow_yaml, ctx) is None
+ assert not [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
+
+ def test_persist_seam_guard_still_fires_when_imposition_is_not_admitted(self) -> None:
+ ctx = _records_spine_ctx()
+ ctx.update_workflow_called = True
+ under_built = _records_block_yaml('await page.locator("#stage-a").click()')
+
+ imposition = workflow_update_module._maybe_impose_synthesized_code_block(under_built, ctx)
+
+ assert ctx.spine_imposition_owned_attempt is False
+ assert imposition.substitutions is None
+
+ with capture_logs() as logs:
+ guarded = workflow_update_module._persist_seam_spine_under_build_result(imposition.workflow_yaml, ctx)
+
+ assert guarded is not None
+ assert any("scouted_spine_under_build" in violation for violation in guarded.violations)
+ events = [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
+ assert events and events[0]["site"] == "persist_seam"
+
+ def test_ungrounded_sibling_mixing_ambiguous_and_concrete_calls_is_replaced_by_the_spine(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ submitted = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: quote_flow
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ await page.locator("#continue").click()
+ - block_type: code
+ label: download_matching_invoice
+ code: |
+ async with page.expect_download(timeout=20000) as download_info:
+ await page.get_by_role("link", name="View Printable Statement").click()
+ download = await download_info.value
+ - block_type: code
+ label: summarize
+ code: |
+ records = [{"invoice": "parsed"}]
+ """
+ )
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.violations == []
+ assert ctx.spine_imposition_owned_attempt is True
+ blocks = _code_blocks(parse_workflow_yaml(result.workflow_yaml))
+ assert list(blocks) == ["quote_flow", "summarize"]
+ carrier_code = str(blocks["quote_flow"]["code"])
+ assert "get_by_role" not in carrier_code
+ assert carrier_code.count('page.locator("#coverage-next").click()') == 1
+ dropped = [log for log in logs if log["event"] == "copilot_spine_stale_rung_dropped"]
+ assert dropped and dropped[0]["dropped_labels"] == ["download_matching_invoice"]
+ assert "never_captured" in {record["kind"] for record in dropped[0]["dropped_provenance"]}
+ assert result.substitutions is not None
+ assert result.substitutions["replaced_hand_authored_browser_rungs"] == ["download_matching_invoice"]
+
+ def test_referenced_ungrounded_sibling_steers_instead_of_being_replaced(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ submitted = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: quote_flow
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ await page.locator("#continue").click()
+ - block_type: code
+ label: download_matching_invoice
+ code: |
+ async with page.expect_download(timeout=20000) as download_info:
+ await page.get_by_role("link", name="View Printable Statement").click()
+ download = await download_info.value
+ - block_type: code
+ label: summarize
+ code: |
+ return {"output": download_matching_invoice_output}
+ """
+ )
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.substitutions is None
+ assert result.workflow_yaml == submitted
+ assert any("`download_matching_invoice`" in violation for violation in result.violations)
+ assert any("never_captured" in violation for violation in result.violations)
+ assert any("never by authoring browser calls freehand" in violation for violation in result.violations)
+
+ def test_hand_authored_download_rung_is_replaced_by_the_grounded_spine_terminal(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+ submitted = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: enter_zip
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ - block_type: code
+ label: download_statement
+ code: |
+ async with page.expect_download() as dl:
+ await page.locator("a[href=\\"/files/report.pdf\\"]").click()
+ """
+ )
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.violations == []
+ blocks = _code_blocks(parse_workflow_yaml(result.workflow_yaml))
+ assert list(blocks) == ["enter_zip"]
+ code = str(blocks["enter_zip"]["code"])
+ assert code.count("async with page.expect_download()") == 1
+ assert 'page.locator("#coverage-next")' in code
+
+ def test_aliased_download_handle_rung_is_dropped_for_the_grounded_spine_terminal(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+ submitted = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: enter_zip
+ code: |
+ await page.locator("#zip").fill(str(zip_code))
+ - block_type: code
+ label: download_statement
+ code: |
+ rows = page.locator("div.statement-row")
+ await rows.count()
+ printable = rows.nth(0).locator("a.printable")
+ await printable.wait_for(state="visible", timeout=10000)
+ async with page.expect_download(timeout=20000) as download_info:
+ await printable.click()
+ download = await download_info.value
+ """
+ )
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(submitted, ctx)
+
+ assert result.violations == []
+ blocks = _code_blocks(parse_workflow_yaml(result.workflow_yaml))
+ assert list(blocks) == ["enter_zip"]
+ code = str(blocks["enter_zip"]["code"])
+ assert code.count("async with page.expect_download()") == 1
+ assert "printable" not in code
+ dropped = [log for log in logs if log["event"] == "copilot_spine_stale_rung_dropped"]
+ assert dropped and dropped[0]["dropped_labels"] == ["download_statement"]
+ assert "page.expect_download(timeout=20000)" in dropped[0]["dropped_actions"]
+
+ def test_unchanged_under_built_carrier_still_lands_the_spine_on_an_owned_attempt(self) -> None:
+ ctx = _records_spine_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+ under_built = _records_block_yaml('await page.locator("#stage-a").click()')
+ ctx.workflow_yaml = under_built
+
+ imposition = workflow_update_module._maybe_impose_synthesized_code_block(under_built, ctx)
+
+ assert imposition.violations == []
+ assert imposition.substitutions is not None
+ assert ctx.spine_imposition_owned_attempt is True
+ code = str(_single_code_block(parse_workflow_yaml(imposition.workflow_yaml))["code"])
+ assert 'page.locator("#stage-b").click()' in code
+ assert workflow_update_module._persist_seam_spine_under_build_result(imposition.workflow_yaml, ctx) is None
+
+
+def _reaching_extraction_ctx() -> CopilotContext:
+ ctx = _live_read_extraction_ctx()
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ )
+ return ctx
+
+
+class TestReachingTrajectoryOwnershipDeterminism:
+ def test_owned_attempt_fires_before_the_extraction_plan_materializes(self) -> None:
+ ctx = _reaching_extraction_ctx()
+ ctx.flow_evidence = []
+ ctx.update_workflow_called = True
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_live_read_submitted_yaml(), ctx)
+
+ assert enforcement_module.synthesized_trajectory_reaches_goal(ctx) is True
+ assert enforcement_module.synthesized_trajectory_is_goal_complete(ctx) is False
+ assert result.violations == []
+ assert ctx.spine_imposition_owned_attempt is True
+ assert len([log for log in logs if log["event"] == "copilot_spine_imposition_owned_attempt"]) == 1
+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])
+ assert "_extraction_value_0" not in code
+ assert ctx.pending_requested_output_extraction_candidate is None
+
+ def test_materialized_plan_reimposes_idempotently_and_holds_candidate_identity(self) -> None:
+ ctx = _reaching_extraction_ctx()
+ ctx.flow_evidence = []
+ ctx.update_workflow_called = True
+ first = workflow_update_module._maybe_impose_synthesized_code_block(_live_read_submitted_yaml(), ctx)
+ assert first.violations == []
+
+ ctx.flow_evidence = _live_read_extraction_ctx().flow_evidence
+ second = workflow_update_module._maybe_impose_synthesized_code_block(first.workflow_yaml, ctx)
+
+ assert second.violations == []
+ assert ctx.spine_imposition_owned_attempt is True
+ code = str(_single_code_block(parse_workflow_yaml(second.workflow_yaml))["code"])
+ assert code.count('return {"output": {"record_id": _extraction_value_0}}') == 1
+ candidate = ctx.pending_requested_output_extraction_candidate
+ assert candidate is not None
+
+ third = workflow_update_module._maybe_impose_synthesized_code_block(second.workflow_yaml, ctx)
+
+ assert third.violations == []
+ assert ctx.pending_requested_output_extraction_candidate == candidate
+ assert str(_single_code_block(parse_workflow_yaml(third.workflow_yaml))["code"]) == code
+
+ def test_reaching_attempt_without_a_carrier_records_the_absence(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ blockless = _yaml(
+ """
+ title: Quote
+ workflow_definition:
+ blocks: []
+ """
+ )
+
+ with capture_logs() as logs:
+ result = workflow_update_module._maybe_impose_synthesized_code_block(blockless, ctx)
+
+ assert result.violations == []
+ assert result.substitutions is None
+ assert ctx.spine_imposition_owned_attempt is False
+ assert [log for log in logs if log["event"] == "copilot_spine_imposition_no_carrier"]
+
+
+def _download_ctx() -> CopilotContext:
+ ctx = _quote_ctx()
+ ctx.scout_trajectory = list(ctx.scout_trajectory) + [
+ {
+ "tool_name": "click",
+ "selector": "#view-printable",
+ "source_url": "https://example.com/quote/statement",
+ "trajectory_index": 3,
+ }
+ ]
+ ctx.reached_download_target = ReachedDownloadTarget(
+ selector='a[href="/files/report.pdf"]',
+ affordance_text="Download PDF",
+ download_kind="extension",
+ source_step="trajectory_recency",
+ already_registered=False,
+ trajectory_anchor=2,
+ )
+ return ctx
+
+
+def _download_ctx_with_criterion(evidence_source: RequestedOutputEvidenceSource) -> CopilotContext:
+ ctx = _download_ctx()
+ ctx.turn_id = "t-download"
+ ctx.update_workflow_called = True
+ ctx.request_policy = RequestPolicy(
+ completion_criteria=[
+ CompletionCriterion(
+ id="downloaded_statement",
+ outcome="the statement file is downloaded",
+ output_path="output.downloads",
+ requested_output_evidence_source=evidence_source,
+ )
+ ]
+ )
+ return ctx
+
+
+class TestSequencedDownloadTerminalCoverage:
+ def test_imposed_spine_stops_at_the_capture_anchor_and_ends_on_the_terminal(self) -> None:
+ ctx = _download_ctx()
+ ctx.update_workflow_called = True
+
+ result = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ assert result.violations == []
+ assert result.substitutions is not None
+ code = str(_single_code_block(parse_workflow_yaml(result.workflow_yaml))["code"])
+ assert '"#view-printable"' not in code
+ assert code.index("async with page.expect_download()") > code.index('"#coverage-next"')
+
+ def test_landed_sequenced_spine_satisfies_the_under_build_guard(self) -> None:
+ ctx = _download_ctx()
+ ctx.update_workflow_called = True
+
+ imposed = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ later = _download_ctx()
+ later.update_workflow_called = True
+
+ with capture_logs() as logs:
+ guarded = workflow_update_module._persist_seam_spine_under_build_result(imposed.workflow_yaml, later)
+
+ assert guarded is None
+ assert not [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
+
+ def test_sequenced_spine_with_runtime_output_criterion_stays_fail_closed(self) -> None:
+ ctx = _download_ctx_with_criterion("runtime_output")
+
+ imposed = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ assert imposed.violations == []
+ assert workflow_update_module._output_contract_required_paths_source(ctx).union == {"output.downloads"}
+
+ def test_retired_imposition_still_coverage_checks_a_later_under_built_persist(self) -> None:
+ ctx = _quote_ctx()
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+
+ imposition = workflow_update_module._maybe_impose_synthesized_code_block(_quote_submitted_yaml(), ctx)
+
+ assert imposition.substitutions is None
+ assert ctx.spine_imposition_owned_attempt is False
+
+ guarded = workflow_update_module._persist_seam_spine_under_build_result(imposition.workflow_yaml, ctx)
+
+ assert guarded is not None
+ assert any("scouted_spine_under_build" in violation for violation in guarded.violations)
diff --git a/tests/unit/test_copilot_code_block_synthesis.py b/tests/unit/test_copilot_code_block_synthesis.py
index 7821ed636..0901900fe 100644
--- a/tests/unit/test_copilot_code_block_synthesis.py
+++ b/tests/unit/test_copilot_code_block_synthesis.py
@@ -50,7 +50,7 @@ from skyvern.forge.sdk.copilot.output_extraction_plan import (
)
from skyvern.forge.sdk.copilot.reached_download_target import ReachedDownloadTarget
from skyvern.forge.sdk.copilot.tools import _normalize_code_artifact_metadata
-from skyvern.forge.sdk.copilot.tools.scouting import _fill_carry_to_interaction
+from skyvern.forge.sdk.copilot.tools.scouting import _fill_carry_to_interaction, _with_trajectory_anchor
from skyvern.forge.sdk.copilot.tools.workflow_update import _code_block_safety_errors
from skyvern.forge.sdk.workflow.models.block import CodeBlock, CodeBlockStep
@@ -2992,3 +2992,111 @@ class TestUncoveredRequiredEmittedInteractions:
]
assert uncovered_required_emitted_interactions(emitted, draft_calls) == []
+
+
+_BILLS_URL = "https://example.com/bills"
+_STATEMENT_URL = "https://example.com/bills/statement"
+
+
+def _anchored_trajectory() -> list[dict[str, Any]]:
+ return [
+ _interaction("type_text", selector="#account", source_url=_BILLS_URL, typed_value="A-1", trajectory_index=0),
+ _interaction("click", selector="#statement-row", source_url=_BILLS_URL, trajectory_index=1),
+ _interaction("click", selector="#view-printable", source_url=_STATEMENT_URL, trajectory_index=2),
+ ]
+
+
+class TestDownloadTerminalSequencing:
+ def test_post_capture_navigation_is_dropped_and_terminal_is_last(self) -> None:
+ result = synthesize_code_block(
+ _anchored_trajectory(),
+ reached_download_target=_download_target(trajectory_anchor=1),
+ )
+
+ assert result is not None
+ assert '"#view-printable"' not in result.code
+ assert '"#statement-row"' in result.code
+ assert result.code.index("async with page.expect_download()") > result.code.index('"#statement-row"')
+ assert '"downloaded_file_name"' in result.code
+ assert result.diagnostics.download_terminal_anchor == 1
+ assert result.diagnostics.download_terminal_dropped_trailing == 1
+ assert [record["selector"] for record in result.diagnostics.emitted_interactions] == [
+ "#account",
+ "#statement-row",
+ ]
+
+ def test_unanchored_target_keeps_the_whole_trajectory(self) -> None:
+ result = synthesize_code_block(
+ _anchored_trajectory(),
+ reached_download_target=_download_target(),
+ )
+
+ assert result is not None
+ assert '"#view-printable"' in result.code
+ assert result.diagnostics.download_terminal_dropped_trailing == 0
+
+ def test_anchor_at_the_last_interaction_is_byte_identical_to_unanchored(self) -> None:
+ anchored = synthesize_code_block(
+ _anchored_trajectory(),
+ reached_download_target=_download_target(trajectory_anchor=2),
+ )
+ unanchored = synthesize_code_block(
+ _anchored_trajectory(),
+ reached_download_target=_download_target(),
+ )
+
+ assert anchored is not None and unanchored is not None
+ assert anchored.code == unanchored.code
+ assert anchored.diagnostics.download_terminal_dropped_trailing == 0
+
+ def test_anchor_survives_trajectory_eviction(self) -> None:
+ evicted = [
+ _interaction("click", selector="#statement-row", source_url=_BILLS_URL, trajectory_index=5),
+ _interaction("click", selector="#view-printable", source_url=_STATEMENT_URL, trajectory_index=6),
+ ]
+
+ result = synthesize_code_block(evicted, reached_download_target=_download_target(trajectory_anchor=5))
+
+ assert result is not None
+ assert '"#view-printable"' not in result.code
+ assert result.diagnostics.download_terminal_dropped_trailing == 1
+
+ def test_registered_target_is_never_sequenced(self) -> None:
+ result = synthesize_code_block(
+ _anchored_trajectory(),
+ reached_download_target=_download_target(
+ already_registered=True, download_kind="registered", selector="", trajectory_anchor=1
+ ),
+ )
+
+ assert result is not None
+ assert '"#view-printable"' in result.code
+ assert "expect_download" not in result.code
+
+ def test_extraction_suffix_composes_after_the_sequenced_terminal(self) -> None:
+ trajectory = [
+ _interaction("click", selector="#show-details", source_url=_BILLS_URL, trajectory_index=0),
+ _interaction("click", selector="#view-printable", source_url=_STATEMENT_URL, trajectory_index=1),
+ ]
+
+ result = synthesize_code_block_with_extraction(
+ trajectory,
+ _extraction_plan(),
+ reached_download_target=_download_target(trajectory_anchor=0),
+ )
+
+ assert result is not None
+ assert '"#view-printable"' not in result.interaction_code
+ assert "async with page.expect_download()" in result.interaction_code
+ assert result.extraction_code
+ assert result.code.startswith(result.interaction_code)
+
+ def test_capture_stamps_the_latest_trajectory_index(self) -> None:
+ ctx = SimpleNamespace(scout_trajectory=_anchored_trajectory()[:2])
+
+ stamped = _with_trajectory_anchor(ctx, _download_target()) # type: ignore[arg-type]
+
+ assert stamped.trajectory_anchor == 1
+ assert (
+ _with_trajectory_anchor(SimpleNamespace(scout_trajectory=[]), _download_target()).trajectory_anchor is None
+ ) # type: ignore[arg-type]
diff --git a/tests/unit/test_copilot_enforcement_pruning.py b/tests/unit/test_copilot_enforcement_pruning.py
index f0686f420..3511e7137 100644
--- a/tests/unit/test_copilot_enforcement_pruning.py
+++ b/tests/unit/test_copilot_enforcement_pruning.py
@@ -54,9 +54,11 @@ from skyvern.forge.sdk.copilot.enforcement import (
record_scouted_output_coverage,
run_with_enforcement,
synthesized_block_persistence_signal,
+ synthesized_goal_completion_landing_pending,
synthesized_persistence_reopened,
synthesized_persistence_reopened_after_failed_run,
synthesized_trajectory_is_goal_complete,
+ synthesized_trajectory_reaches_goal,
uncovered_output_reject_scout_steer_signal,
uncovered_requested_output_paths,
)
@@ -134,6 +136,8 @@ class _Ctx:
self.synthesized_block_reopened_for_output_coverage = False
self.synthesized_block_reopened_for_credential_scout = False
self.credential_scout_rescout_context_key = None
+ self.synthesized_goal_complete_landed = False
+ self.impose_synthesized_code_block = False
self.scouted_output_covered_paths: set[str] = set()
self.flow_evidence: list[dict[str, object]] = []
self.copilot_config: CopilotConfig | None = None
@@ -782,6 +786,35 @@ class TestSynthesizedOfferPersistenceGate:
assert _should_force_synthesized_block_persistence(ctx) is True
assert synthesized_block_persistence_signal(ctx, "evaluate") is not None
+ def test_unlanded_goal_completion_forces_persistence_after_first_authoring_call(self) -> None:
+ trajectory = [
+ {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},
+ {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},
+ ]
+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)
+ ctx.impose_synthesized_code_block = True
+ ctx.update_workflow_called = True
+
+ assert synthesized_goal_completion_landing_pending(ctx) is True
+ assert synthesized_persistence_reopened(ctx) is True
+ assert _should_force_synthesized_block_persistence(ctx) is True
+ assert synthesized_block_persistence_signal(ctx, "evaluate") is not None
+
+ def test_landed_goal_completion_stops_forcing_on_identical_resubmission(self) -> None:
+ trajectory = [
+ {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},
+ {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},
+ ]
+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)
+ ctx.impose_synthesized_code_block = True
+ ctx.update_workflow_called = True
+ ctx.synthesized_goal_complete_landed = True
+
+ assert synthesized_goal_completion_landing_pending(ctx) is False
+ assert synthesized_persistence_reopened(ctx) is False
+ assert _should_force_synthesized_block_persistence(ctx) is False
+ assert synthesized_block_persistence_signal(ctx, "evaluate") is None
+
def test_failed_verified_run_with_new_commit_reopens_synthesized_persistence_gate(self) -> None:
previous_trajectory = [
{
@@ -2207,6 +2240,16 @@ def _criterion(output_path: str, outcome: str) -> CompletionCriterion:
return CompletionCriterion(id=output_path, outcome=outcome, output_path=output_path)
+def _registered_download_criterion() -> CompletionCriterion:
+ return CompletionCriterion(
+ id="output.statement_pdf",
+ outcome="the statement PDF is downloaded",
+ output_path="output.statement_pdf",
+ deliverable_kind="registered_download",
+ requested_output_evidence_source="registered_artifact_content",
+ )
+
+
def _turn_state(*criteria: CompletionCriterion) -> SimpleNamespace:
return SimpleNamespace(decision=SimpleNamespace(criteria=tuple(criteria)))
@@ -2348,6 +2391,13 @@ class TestScoutOutputCoverageGate:
assert _should_block_mutating_tool_after_synthesized_offer(ctx, "click") is False
assert synthesized_block_persistence_signal(ctx, "click") is None
+ def test_uncovered_output_leaves_the_trajectory_goal_reaching(self) -> None:
+ ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))
+ assert uncovered_requested_output_paths(ctx) == {"output.document_name"}
+ assert synthesized_trajectory_reaches_goal(ctx) is True
+ assert synthesized_trajectory_is_goal_complete(ctx) is False
+ assert _should_force_synthesized_block_persistence(ctx) is False
+
def test_value_bearing_container_coverage_without_plan_does_not_force(self) -> None:
ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))
page_evidence = {
@@ -2389,6 +2439,17 @@ class TestScoutOutputCoverageGate:
ctx.reached_download_target = _download_target()
assert uncovered_requested_output_paths(ctx) == {"output.document_name"}
+ def test_registered_download_request_not_goal_complete_until_download_reached(self) -> None:
+ # Post-run registered-download evidence is absent from the pre-run requested-output gate, so a
+ # durable-entry+commit prefix (sign-in) would read goal-complete and land the mechanism-F latch
+ # mid-scout — locking out imposition of the real download spine once the scout reaches it.
+ ctx = self._authoring_ctx(_registered_download_criterion())
+ assert uncovered_requested_output_paths(ctx) == set()
+ assert ctx.reached_download_target is None
+ assert synthesized_trajectory_is_goal_complete(ctx) is False
+ ctx.reached_download_target = _download_target()
+ assert synthesized_trajectory_is_goal_complete(ctx) is True
+
def test_unreachable_output_never_completes_on_long_trajectory(self) -> None:
ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))
ctx.scout_trajectory = _entry_commit_trajectory() * 12