Skip to content

Automate browser based workflows with AI

Gate durable Copilot authoring on scout goal-reach; arm the freehand reject by selector provenance (SKY-12547) (#7556)

Andrew Neilson6d49ba12427f
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.py
index 69d4dbada..b8e01f685 100644
--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py
+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py
@@ -1929,6 +1929,15 @@ SCOUTED_SPINE_UNRECORDED_INDEX_REASON_CODE = "scouted_spine_unrecorded_index"
SCOUTED_SPINE_TRUNCATED_REASON_CODE = "scouted_spine_truncated"
+_LEADING_TAG_ID_SELECTOR_RE = re.compile(r"^[A-Za-z][A-Za-z0-9-]*#")
+
+
+def normalized_scout_selector(selector: str) -> str:
+ # Capture and persist-seam comparison share one normal form: a leading `tag#id` qualifier reduces
+ # to `#id` (ids are document-unique), so both sides name the same control.
+ return _LEADING_TAG_ID_SELECTOR_RE.sub("#", selector)
+
+
def normalized_locator_expr(text: str) -> str:
try:
return ast.unparse(ast.parse(text, mode="eval"))
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index fb4b958e0..d0fbfe0be 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -51,6 +51,7 @@ if TYPE_CHECKING:
RecordedOutcomeBindingConstraint,
RecordedOutcomeGroundingRequirement,
)
+ from skyvern.forge.sdk.copilot.code_block_synthesis import SynthesizedCodeBlock
from skyvern.forge.sdk.copilot.completion_criteria_store import CompletionCriteriaTurnState
from skyvern.forge.sdk.copilot.completion_verification import CompletionVerificationResult
from skyvern.forge.sdk.copilot.context import CodeAuthoringRepairContext
@@ -494,9 +495,12 @@ class AgentContext:
# 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.
+ # Imposition answered this persist attempt on a goal-complete trajectory; owned-carrier metadata
+ # scaffolding keys off it (workflow_update._scaffold_metadata_from_owned_carrier_produced_output).
spine_imposition_owned_attempt: bool = False
+ # Synthesized block computed by this persist attempt's imposition pass; the pre-persist spine gate
+ # reuses it so both seams grade one synthesis. Reset at each imposition entry.
+ imposition_synthesized_block: SynthesizedCodeBlock | None = None
# Label of the code block the imposition attempt owns this call (carrier), including on no-op early
# returns. The freehand persist-seam surface leg exempts exactly this label and gates its siblings.
spine_imposition_carrier_label: str | None = None
diff --git a/skyvern/forge/sdk/copilot/tools/scouting.py b/skyvern/forge/sdk/copilot/tools/scouting.py
index f9113d293..00bbf9256 100644
--- a/skyvern/forge/sdk/copilot/tools/scouting.py
+++ b/skyvern/forge/sdk/copilot/tools/scouting.py
@@ -17,6 +17,7 @@ from skyvern.forge.sdk.copilot.build_test_outcome import (
recorded_outcome_from_loaded_result_evidence,
recorded_outcome_from_scout_act_observe_hollow,
)
+from skyvern.forge.sdk.copilot.code_block_synthesis import normalized_scout_selector
from skyvern.forge.sdk.copilot.composition_browser_expressions import (
role_name_match_count_expression as _role_name_match_count_expression,
)
@@ -527,8 +528,8 @@ def _page_evidence_with_inputs_as_fields(page_evidence: dict[str, Any]) -> dict[
continue
field = dict(item)
selector = field.get("selector")
- if isinstance(selector, str) and selector.startswith("input#"):
- field["selector"] = f"#{selector.split('#', 1)[1]}"
+ if isinstance(selector, str):
+ field["selector"] = normalized_scout_selector(selector)
fields.append(field)
if not fields:
return page_evidence
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index e5dd7cffa..40e41eae7 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -78,6 +78,7 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
SynthesisDiagnostics,
SynthesizedCodeBlock,
_bare_drop_superseded_on_screen,
+ _bare_locator_call_selector,
_credential_field_accesses,
_get_by_role_expr_strict,
_is_ignorable_entry_opener_drop,
@@ -94,6 +95,7 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
locator_selector_literals,
missing_rung_text,
normalized_locator_expr,
+ normalized_scout_selector,
obligation_finding_reason_code,
obligation_finding_selector,
produce_covered_static_return_envelope,
@@ -7563,7 +7565,7 @@ class _BrowserMutationSignature(NamedTuple):
_BrowserSurfaceProvenanceKind = Literal["never_captured", "shape_diverged", "ambiguous", "suffix_disallowed"]
_BrowserSurfaceDivergenceSource = Literal["synthesized", "trajectory_dropped"]
-_BrowserSurfaceProvenanceSite = Literal["whole_trajectory", "extraction_suffix"]
+_BrowserSurfaceProvenanceSite = Literal["whole_trajectory", "extraction_suffix", "fragment_scout"]
_BROWSER_SURFACE_PROVENANCE_EVENT = "copilot_browser_surface_rejection_provenance"
@@ -8270,6 +8272,7 @@ def _scouted_spine_under_build_result(
draft_codes: list[str],
block_label: str,
site: str = "imposition",
+ draft_repairable_only: bool = False,
) -> _SynthesizedCodeImpositionResult | None:
diagnostics = synthesized.diagnostics
# Lane-flagged emissions (optional dismissals, readonly verifies, entry recovery) are conditional
@@ -8325,6 +8328,10 @@ def _scouted_spine_under_build_result(
selector=str(first_uncovered.get("selector") or "") or None,
),
)
+ # Non-uncovered partition findings are synthesizer-side: no draft edit closes them, so a
+ # repair-convergence-only site routes them to the turn-end obligation halt instead of a churn loop.
+ if draft_repairable_only:
+ return None
partition_findings = [
finding
for finding in spine_partition_findings(diagnostics, draft_calls, ctx.scout_trajectory or [])
@@ -8371,17 +8378,11 @@ def _scouted_spine_partition_under_build_result(
)
-def _persist_seam_spine_under_build_result(
- workflow_yaml: str, ctx: AgentContext
-) -> _SynthesizedCodeImpositionResult | None:
+def _pre_persist_scouted_spine_result(workflow_yaml: str, ctx: AgentContext) -> _SynthesizedCodeImpositionResult | None:
+ """Last author-time gate before a durable persist: whatever path produced the final yaml, a draft
+ whose browser calls leave the scouted spine partition open is rejected instead of persisted."""
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:
- return None
if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
return None
scout_trajectory = ctx.scout_trajectory
@@ -8392,14 +8393,18 @@ def _persist_seam_spine_under_build_result(
parsed = parse_workflow_yaml(workflow_yaml)
if not isinstance(parsed, dict):
return None
- code_blocks = _workflow_code_blocks(parsed)
- synthesized = synthesize_code_block(
- scout_trajectory,
- strict_selectors=True,
- reached_download_target=ctx.reached_download_target,
- )
+ synthesized = ctx.imposition_synthesized_block
+ if synthesized is None:
+ # Deliberate fallback: when no imposition pass synthesized this attempt, grade a fresh strict
+ # synthesis of the raw trajectory, without imposition's grounded-outcome reconciliation.
+ synthesized = synthesize_code_block(
+ scout_trajectory,
+ strict_selectors=True,
+ reached_download_target=ctx.reached_download_target,
+ )
if synthesized is None:
return None
+ code_blocks = _workflow_code_blocks(parsed)
# A submission with zero code blocks still holds the open spine obligation: empty draft calls
# leave every required rung uncovered rather than slipping the seam.
return _scouted_spine_under_build_result(
@@ -8408,7 +8413,8 @@ def _persist_seam_spine_under_build_result(
synthesized=synthesized,
draft_codes=[str(block.get("code") or "") for block in code_blocks],
block_label=", ".join(_code_block_label(block) for block in code_blocks) or _SYNTHESIZED_BLOCK_LABEL,
- site="persist_seam",
+ site="pre_persist",
+ draft_repairable_only=True,
)
@@ -8416,6 +8422,114 @@ _FREEHAND_UNRESOLVABLE_SELECTOR_REASON_CODE = "freehand_unresolvable_selector"
_FREEHAND_UNGUARDED_CREDENTIAL_REASON_CODE = "freehand_unguarded_credential_fill"
+def _scouted_selector_forms(scout_trajectory: list[ScoutedInteraction]) -> set[str]:
+ return {
+ normalized_scout_selector(selector)
+ for interaction in scout_trajectory
+ if (selector := str(interaction.get("selector") or "").strip())
+ }
+
+
+def _receiver_is_self_validating_get_by_role(receiver: str) -> bool:
+ try:
+ node = ast.parse(receiver, mode="eval").body
+ except SyntaxError:
+ return False
+ if not (
+ isinstance(node, ast.Call)
+ and isinstance(node.func, ast.Attribute)
+ and node.func.attr == "get_by_role"
+ and isinstance(node.func.value, ast.Name)
+ and node.func.value.id == "page"
+ ):
+ return False
+ if len(node.args) != 1:
+ return False
+ role = node.args[0]
+ if not (isinstance(role, ast.Constant) and isinstance(role.value, str) and role.value):
+ return False
+ if any(keyword.arg is None or not isinstance(keyword.value, ast.Constant) for keyword in node.keywords):
+ return False
+ return any(
+ keyword.arg == "name"
+ and isinstance(keyword.value, ast.Constant)
+ and isinstance(keyword.value.value, str)
+ and keyword.value.value
+ for keyword in node.keywords
+ )
+
+
+def _fragment_scout_freehand_validation(
+ *,
+ code_blocks: list[dict[str, Any]],
+ exempt_block: dict[str, Any] | None,
+ prior_yaml: str | None,
+ scout_trajectory: list[ScoutedInteraction],
+ synthesized_code: str,
+ synthesized_diagnostics: SynthesisDiagnostics | None,
+) -> _BrowserSurfaceValidation:
+ """A fragment scout is no authoritative full-spine reference, but every durable mutation must still
+ resolve to a scout-captured form (captured selector or synthesized receiver) or be rejected; only
+ selectorless page navigation and self-validating literal role+name receivers are admitted unscouted."""
+ scouted_forms = _scouted_selector_forms(scout_trajectory)
+ scouted_mutations, _, _ = _browser_surface_for_code(synthesized_code)
+ admitted_receivers = {
+ (mutation.method, normalized_locator_expr(mutation.receiver)) for mutation in scouted_mutations
+ }
+ violations: list[str] = []
+ provenance: list[_BrowserSurfaceRejectionProvenance] = []
+ for block in code_blocks:
+ if block is exempt_block:
+ continue
+ if prior_yaml is not None and not _submitted_code_block_changed(block, prior_yaml):
+ continue
+ label = _code_block_label(block)
+ block_mutations, _, block_ambiguous = _browser_surface_for_code(str(block.get("code") or ""))
+ freehand_mutations: list[_BrowserMutationSignature] = []
+ for mutation in block_mutations:
+ if mutation.receiver == "page":
+ continue
+ if (mutation.method, normalized_locator_expr(mutation.receiver)) in admitted_receivers:
+ continue
+ if _receiver_is_self_validating_get_by_role(mutation.receiver):
+ continue
+ selector = _bare_locator_call_selector(mutation.receiver)
+ if selector is not None and normalized_scout_selector(selector) in scouted_forms:
+ continue
+ freehand_mutations.append(mutation)
+ if freehand_mutations:
+ action_text = ", ".join(f"{mutation.receiver}.{mutation.method}" for mutation in sorted(freehand_mutations))
+ block_provenance = [
+ _classify_unscouted_mutation(
+ mutation,
+ scouted_mutations=scouted_mutations,
+ diagnostics=synthesized_diagnostics,
+ site="fragment_scout",
+ block_label=label,
+ )
+ for mutation in sorted(freehand_mutations)
+ ]
+ provenance.extend(block_provenance)
+ violations.append(
+ f"Unable to impose synthesized code block: `{label}` contains unscouted browser action(s): "
+ f"{action_text}.{_provenance_suffix_text(block_provenance)}"
+ )
+ if block_ambiguous:
+ ambiguous_provenance = [
+ _ambiguous_browser_action_provenance(action, site="fragment_scout", block_label=label)
+ for action in block_ambiguous
+ ]
+ provenance.extend(ambiguous_provenance)
+ violations.append(
+ f"Unable to impose synthesized code block: `{label}` contains ambiguous browser action(s): "
+ + ", ".join(block_ambiguous)
+ + "."
+ + _provenance_suffix_text(ambiguous_provenance)
+ )
+ _log_browser_surface_rejection_provenance(provenance)
+ return _BrowserSurfaceValidation(violations, provenance)
+
+
def _workflow_credential_parameter_keys(parsed: Mapping[str, Any]) -> set[str]:
workflow_definition = parsed.get("workflow_definition")
parameters = workflow_definition.get("parameters") if isinstance(workflow_definition, dict) else None
@@ -8588,6 +8702,18 @@ def _persist_seam_freehand_surface_result(
# The full synthesized spine is an authoritative admissibility reference only when the scout captured a
# durable entry through a commit; a fragment scout would false-reject legitimate multi-block authoring.
if synthesized is None or not synthesized_trajectory_reaches_goal(ctx):
+ # scouted_interactions joins the admitted set because the trajectory is cap-evicted; both
+ # collections carry only scout-captured selectors.
+ fragment_validation = _fragment_scout_freehand_validation(
+ code_blocks=code_blocks,
+ exempt_block=exempt_block,
+ prior_yaml=prior_yaml,
+ scout_trajectory=[*scout_trajectory, *ctx.scouted_interactions],
+ synthesized_code=synthesized_code,
+ synthesized_diagnostics=synthesized_diagnostics,
+ )
+ if fragment_validation.violations:
+ return _freehand_surface_reject(workflow_yaml, code_blocks, fragment_validation)
return None
validation = _whole_trajectory_browser_surface_violations(
code_blocks=code_blocks,
@@ -9863,6 +9989,7 @@ def _maybe_impose_synthesized_code_block(
ctx.pending_requested_output_extraction_candidate = None
ctx.spine_imposition_owned_attempt = False
ctx.spine_imposition_carrier_label = None
+ ctx.imposition_synthesized_block = None
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:
@@ -9957,6 +10084,7 @@ def _maybe_impose_synthesized_code_block(
workflow_yaml=workflow_yaml,
violations=["Unable to impose synthesized code block: scout trajectory produced no runnable code."],
)
+ ctx.imposition_synthesized_block = synthesized
if synthesized.diagnostics.grounded_submit_binding_fingerprints:
LOG.info(
"copilot recorded outcome binding consumed by synthesizer",
@@ -11517,17 +11645,6 @@ async def _update_workflow(
)
workflow_yaml = imposition.workflow_yaml
if imposition.substitutions is None:
- seam_under_build = _persist_seam_spine_under_build_result(workflow_yaml, ctx)
- if seam_under_build is not None:
- _set_code_authoring_repair_context(ctx, seam_under_build.repair_context)
- _record_code_authoring_guardrail_reject(ctx)
- return reject(
- error="\n".join(seam_under_build.violations),
- user_facing_summary=_compiled_authoring_user_summary(),
- data=_code_repair_progress_data(seam_under_build.repair_context),
- repair_context=seam_under_build.repair_context,
- record_repair_context_outcome=False,
- )
freehand_surface = _persist_seam_freehand_surface_result(workflow_yaml, ctx)
if freehand_surface is not None:
_set_code_authoring_repair_context(ctx, freehand_surface.repair_context)
@@ -12263,6 +12380,18 @@ async def _update_workflow(
"data": _code_repair_progress_data(select_option_mismatch_context),
}
+ pre_persist_spine = _pre_persist_scouted_spine_result(workflow_yaml, ctx)
+ if pre_persist_spine is not None:
+ _set_code_authoring_repair_context(ctx, pre_persist_spine.repair_context)
+ _record_code_authoring_guardrail_reject(ctx)
+ return reject(
+ error="\n".join(pre_persist_spine.violations),
+ user_facing_summary=_compiled_authoring_user_summary(),
+ data=_code_repair_progress_data(pre_persist_spine.repair_context),
+ repair_context=pre_persist_spine.repair_context,
+ record_repair_context_outcome=False,
+ )
+
try:
# A code block renders code-first (goal + plain step timeline) only when it
# carries a `prompt`; the model authors the goal as artifact `declared_goal`
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index 50e06217d..60241ca5c 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -9159,6 +9159,18 @@ class TestCompiledAuthoringImposition:
]
return ctx
+ @staticmethod
+ def _cafe_search_capture() -> dict[str, object]:
+ return {
+ "tool_name": "type_text",
+ "selector": "#café-search",
+ "source_url": "https://example.com/catalog",
+ "typed_length": 15,
+ "role": "textbox",
+ "accessible_name": "Catalog search",
+ "trajectory_index": 0,
+ }
+
@pytest.mark.asyncio
async def test_imposes_strict_scout_selector_and_lifts_singleton_literal(
self, monkeypatch: pytest.MonkeyPatch
@@ -9842,6 +9854,25 @@ class TestCompiledAuthoringImposition:
async def test_unchanged_prior_code_does_not_impose(self, monkeypatch: pytest.MonkeyPatch) -> None:
_stub_successful_update(monkeypatch)
ctx = self._provider_search_ctx()
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "input[placeholder='Search']",
+ "source_url": "https://example.com/find-care",
+ "typed_length": 13,
+ "role": "textbox",
+ "accessible_name": "Search",
+ "trajectory_index": 0,
+ },
+ {
+ "tool_name": "click",
+ "selector": "button.lookup",
+ "source_url": "https://example.com/find-care",
+ "role": "button",
+ "accessible_name": "Lookup",
+ "trajectory_index": 1,
+ },
+ ]
ctx.workflow_yaml = _SUBMITTED_LITERAL_YAML
result = await _update_workflow({"workflow_yaml": _SUBMITTED_LITERAL_YAML}, ctx)
@@ -10742,6 +10773,7 @@ class TestCompiledAuthoringImposition:
) -> None:
_stub_successful_update(monkeypatch)
ctx = self._typed_default_ctx()
+ ctx.scout_trajectory = [self._cafe_search_capture(), {**ctx.scout_trajectory[0], "trajectory_index": 1}]
result = await _update_workflow({"workflow_yaml": _SUBMITTED_TYPED_LITERAL_REWRITE_YAML}, ctx)
@@ -10777,6 +10809,7 @@ class TestCompiledAuthoringImposition:
) -> None:
_stub_successful_update(monkeypatch)
ctx = self._typed_default_ctx()
+ ctx.scout_trajectory = [self._cafe_search_capture(), {**ctx.scout_trajectory[0], "trajectory_index": 1}]
submitted_yaml = _SUBMITTED_TYPED_LITERAL_REWRITE_YAML.replace("example_sku_123", "other_sku_456")
result = await _update_workflow({"workflow_yaml": submitted_yaml}, ctx)
@@ -15978,7 +16011,7 @@ class TestScoutedSpinePersistSeamCoverage:
assert 'await page.locator("#stage-c").click()' in rejected["error"]
events = [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
assert len(events) == 1
- assert events[0]["site"] == "persist_seam"
+ assert events[0]["site"] == "pre_persist"
with capture_logs() as logs:
nudge = enforcement_module._scouted_spine_turn_end_nudge(ctx)
@@ -16009,7 +16042,7 @@ class TestScoutedSpinePersistSeamCoverage:
assert [log for log in logs if log["event"] == "copilot_imposition_skipped_after_update"]
events = [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
assert len(events) == 1
- assert events[0]["site"] == "persist_seam"
+ assert events[0]["site"] == "pre_persist"
@pytest.mark.asyncio
async def test_second_persist_dropping_all_code_blocks_rejected(self, monkeypatch: pytest.MonkeyPatch) -> None:
@@ -16039,7 +16072,7 @@ class TestScoutedSpinePersistSeamCoverage:
assert ctx.code_authoring_guardrail_reject_count == 1
events = [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
assert len(events) == 1
- assert events[0]["site"] == "persist_seam"
+ assert events[0]["site"] == "pre_persist"
def test_separated_split_branch_under_coverage_rejected(self, monkeypatch: pytest.MonkeyPatch) -> None:
diagnostics = _spine_emission_diagnostics()
@@ -16119,7 +16152,7 @@ class TestScoutedSpinePersistSeamCoverage:
assert "Missing rung source to reuse verbatim" in rejected["error"]
events = [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
assert len(events) == 1
- assert events[0]["site"] == "persist_seam"
+ assert events[0]["site"] == "pre_persist"
assert events[0]["credential_scout_precondition_pending"] is True
ctx.scout_trajectory.append(_submit_interaction(source_url="https://example.com/records"))
@@ -16167,6 +16200,19 @@ class TestScoutedSpinePersistSeamCoverage:
assert "Missing rung source to reuse verbatim" in result["error"]
assert ".password" in result["error"]
+ @pytest.mark.asyncio
+ async def test_render_gate_preempts_pre_persist_spine_gate(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ ctx = _checkpoint_eligible_ctx()
+ draft = _records_block_yaml('await page.locator("#stage-a").click()\nnote = "{{ frobnicator }}"')
+
+ with capture_logs() as logs:
+ result = await _update_workflow({"workflow_yaml": draft}, ctx)
+
+ assert result["ok"] is False
+ assert result["data"]["reason_code"] == "code_block_unrenderable"
+ assert not [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
+
class TestScoutedSpineTurnEndCheckpoint:
def test_checkpoint_is_single_shot_and_emits_unresolved_when_spent(self) -> None:
@@ -17485,7 +17531,7 @@ class TestAdmittedImpositionOwnsSpineCoverage:
remaining = [block.get("label") for block in parsed["workflow_definition"]["blocks"]]
assert "orphan_rung" not in remaining
- def test_admitted_imposition_is_not_answered_by_the_persist_seam_guard(self) -> None:
+ def test_admitted_imposition_passes_the_pre_persist_gate(self) -> None:
ctx = _quote_ctx()
ctx.update_workflow_called = True
ctx.synthesized_goal_complete_landed = True
@@ -17504,10 +17550,10 @@ class TestAdmittedImpositionOwnsSpineCoverage:
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 workflow_update_module._pre_persist_scouted_spine_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:
+ def test_pre_persist_gate_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()')
@@ -17518,12 +17564,12 @@ class TestAdmittedImpositionOwnsSpineCoverage:
assert imposition.substitutions is None
with capture_logs() as logs:
- guarded = workflow_update_module._persist_seam_spine_under_build_result(imposition.workflow_yaml, ctx)
+ guarded = workflow_update_module._pre_persist_scouted_spine_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"
+ assert events and events[0]["site"] == "pre_persist"
def test_ungrounded_sibling_mixing_ambiguous_and_concrete_calls_is_replaced_by_the_spine(self) -> None:
ctx = _quote_ctx()
@@ -17705,7 +17751,7 @@ class TestAdmittedImpositionOwnsSpineCoverage:
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
+ assert workflow_update_module._pre_persist_scouted_spine_result(imposition.workflow_yaml, ctx) is None
def _reaching_extraction_ctx() -> CopilotContext:
@@ -18087,7 +18133,7 @@ class TestSequencedDownloadTerminalCoverage:
later.update_workflow_called = True
with capture_logs() as logs:
- guarded = workflow_update_module._persist_seam_spine_under_build_result(imposed.workflow_yaml, later)
+ guarded = workflow_update_module._pre_persist_scouted_spine_result(imposed.workflow_yaml, later)
assert guarded is None
assert not [log for log in logs if log["event"] == "copilot_scouted_spine_under_build"]
@@ -18110,7 +18156,7 @@ class TestSequencedDownloadTerminalCoverage:
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)
+ guarded = workflow_update_module._pre_persist_scouted_spine_result(imposition.workflow_yaml, ctx)
assert guarded is not None
assert any("scouted_spine_under_build" in violation for violation in guarded.violations)
@@ -19339,6 +19385,145 @@ class TestFreehandSurfacePersistSeam:
result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
assert result is None
+ def test_fragment_scout_rejects_nameless_get_by_role(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.get_by_role("button").click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+
+ def test_fragment_scout_rejects_dynamic_role_get_by_role(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = "await page.get_by_role(role_var).click()"
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+
+ def test_fragment_scout_rejects_dynamic_name_get_by_role(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.get_by_role("button", name=btn_name).click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+
+ def test_fragment_scout_rejects_non_literal_kwarg_get_by_role(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.get_by_role("button", name="Continue", exact=flag).click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+
+ def test_fragment_scout_reject_names_get_by_role_site(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.get_by_role("button").click()'
+ with capture_logs() as logs:
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ events = [log for log in logs if log["event"] == "copilot_browser_surface_rejection_provenance"]
+ assert events and events[0]["site"] == "fragment_scout"
+
+ def test_goal_reaching_rejects_unscouted_get_by_role(self) -> None:
+ code = 'await page.get_by_role("button", name="Unscouted", exact=True).click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code), _goal_reaching_freehand_ctx()
+ )
+ assert result is not None
+ assert "unscouted browser action(s)" in result.violations[0]
+
+ def test_fragment_scout_rejects_freehand_bare_locator_guess(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("#search").fill("x")\nawait page.locator("tr").click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+ assert result.repair_context.block_label == "find_address"
+ assert "page.locator('tr')" in result.violations[0]
+ assert "never_captured" in result.violations[0]
+
+ def test_fragment_scout_rejects_freehand_attribute_selector_guess(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = "await page.locator('button[data-action=\"orderDocuments\"]').click()"
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+ assert "orderDocuments" in result.violations[0]
+
+ def test_fragment_scout_rejects_aliased_locator_mutation(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'target = page.locator("#guessed")\nawait target.click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+ assert "ambiguous browser action(s)" in result.violations[0]
+ assert "target.click()" in result.violations[0]
+
+ def test_fragment_scout_rejects_nth_wrapped_locator_guess(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("tr").nth(2).click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert "ambiguous browser action(s)" in result.violations[0]
+
+ def test_fragment_scout_rejects_has_text_refined_locator_guess(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("tr", has_text="Order").click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert "unscouted browser action(s)" in result.violations[0]
+ assert "never_captured" in result.violations[0]
+
+ def test_fragment_scout_rejects_first_wrapped_bare_locator_guess(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("tr").first.click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert "page.locator('tr')" in result.violations[0]
+
+ def test_fragment_scout_admits_first_wrapped_scouted_selector(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("#search").first.fill("x")'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is None
+
+ def test_fragment_scout_reject_carries_fragment_scout_site(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("tr").click()'
+ with capture_logs() as logs:
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ events = [log for log in logs if log["event"] == "copilot_browser_surface_rejection_provenance"]
+ assert events and events[0]["site"] == "fragment_scout"
+
+ def test_fragment_scout_admits_input_prefixed_capture_replay(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ ctx.scout_trajectory[0]["selector"] = "input#search"
+ code = 'await page.locator("#search").fill("x")'
+ result = workflow_update_module._persist_seam_freehand_surface_result(_freehand_block_yaml(code), ctx)
+ assert result is None
+
+ def test_fragment_scout_skips_unchanged_block(self) -> None:
+ ctx = self._fragment_scout_ctx()
+ code = 'await page.locator("tr").click()'
+ submitted = _freehand_block_yaml(code)
+ ctx.workflow_yaml = submitted
+ result = workflow_update_module._persist_seam_freehand_surface_result(submitted, ctx)
+ assert result is None
+
+ def test_goal_reaching_freehand_bare_locator_reject_unchanged(self) -> None:
+ code = 'await page.locator("#search").fill("x")\nawait page.locator("tr").click()'
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code), _goal_reaching_freehand_ctx()
+ )
+ assert result is not None
+ assert "unscouted browser action(s)" in result.violations[0]
+
def test_unguarded_credential_fill_routes_to_guarded_entry_rung(self) -> None:
parameters = " parameters:\n - {parameter_type: credential, key: portal}\n"
code = 'await page.locator("#password").fill(portal.password)'
@@ -19563,3 +19748,495 @@ class TestCodeBlockRenderPreflightSeam:
and "parameters.business_name" in str(log.get("failing_expression"))
for log in logs
)
+
+
+def _under_scouted_order_status_ctx() -> CopilotContext:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#confirmation",
+ "source_url": "https://portal.example.com/order-status",
+ "role": "textbox",
+ "accessible_name": "Confirmation number",
+ "typed_length": 8,
+ "trajectory_index": 0,
+ }
+ ]
+ assert enforcement_module.synthesized_trajectory_reaches_goal(ctx) is False
+ return ctx
+
+
+class TestUnderScoutedOrderStatusReplay:
+ _LOOKUP_BLOCK_CODE = (
+ 'await page.goto("https://portal.example.com/order-status")\n'
+ 'await page.locator("#confirmation").fill("ABC123")\n'
+ "await page.locator('button[data-action=\"orderLookup\"]').click()\n"
+ "await page.locator('button[data-action=\"orderDocuments\"]').click()\n"
+ 'rows = page.locator("tr")\n'
+ "row_count = await rows.count()\n"
+ )
+
+ def test_freehand_button_guesses_rejected_instead_of_admitted(self) -> None:
+ ctx = _under_scouted_order_status_ctx()
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(self._LOOKUP_BLOCK_CODE, label="lookup_order"), ctx
+ )
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+ assert result.repair_context.block_label == "lookup_order"
+ assert "orderLookup" in result.violations[0]
+ assert "orderDocuments" in result.violations[0]
+
+ def test_scouted_confirmation_replay_admitted(self) -> None:
+ ctx = _under_scouted_order_status_ctx()
+ code = (
+ 'await page.goto("https://portal.example.com/order-status")\n'
+ 'await page.locator("#confirmation").fill("ABC123")\n'
+ )
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code, label="lookup_order"), ctx
+ )
+ assert result is None
+
+
+def _pre_goal_wizard_ctx() -> CopilotContext:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "fill_credential_field",
+ "selector": "#username",
+ "source_url": "https://utility.example.com/login",
+ "credential_id": "cred_1",
+ "credential_name": "portal",
+ "credential_field": "username",
+ "trajectory_index": 0,
+ },
+ {
+ "tool_name": "fill_credential_field",
+ "selector": "#password",
+ "source_url": "https://utility.example.com/login",
+ "credential_id": "cred_1",
+ "credential_name": "portal",
+ "credential_field": "password",
+ "trajectory_index": 1,
+ },
+ {
+ "tool_name": "click",
+ "selector": "#sign-in",
+ "source_url": "https://utility.example.com/login",
+ "role": "button",
+ "accessible_name": "Sign In",
+ "trajectory_index": 2,
+ },
+ {
+ "tool_name": "click",
+ "selector": "#business-toggle",
+ "source_url": "https://utility.example.com/start-service",
+ "role": "button",
+ "accessible_name": "Business",
+ "trajectory_index": 3,
+ },
+ {
+ "tool_name": "type_text",
+ "selector": "#service-address",
+ "source_url": "https://utility.example.com/start-service",
+ "role": "textbox",
+ "accessible_name": "Service address",
+ "typed_length": 20,
+ "trajectory_index": 4,
+ },
+ ]
+ assert enforcement_module.synthesized_trajectory_reaches_goal(ctx) is False
+ return ctx
+
+
+def _wizard_block_yaml(*, include_business_block: bool) -> str:
+ sign_in_code = (
+ ' _scout_entry_target = page.locator("#username")\n'
+ " if await _scout_entry_target.count() == 1:\n"
+ ' await page.locator("#username").fill(portal.username)\n'
+ ' await page.locator("#password").fill(portal.password)\n'
+ ' await page.locator("#sign-in").click()\n'
+ )
+ business_code = (
+ ' await page.locator("#business-toggle").click()\n'
+ ' await page.locator("#service-address").fill(str(service_address))\n'
+ )
+ yaml_text = (
+ "title: t\n"
+ "workflow_definition:\n"
+ " parameters:\n"
+ " - {parameter_type: credential, key: portal, credential_id: cred_1}\n"
+ " - {parameter_type: workflow, workflow_parameter_type: string, key: service_address, default_value: addr}\n"
+ " blocks:\n"
+ " - block_type: code\n"
+ " label: sign_in_to_portal\n"
+ " code: |\n"
+ f"{sign_in_code}"
+ )
+ if include_business_block:
+ yaml_text += " - block_type: code\n label: open_business_start_service\n code: |\n" + business_code
+ return yaml_text
+
+
+class TestPreGoalWizardSpineReplay:
+ def test_sign_in_only_collapse_rejected_naming_dropped_rungs(self) -> None:
+ ctx = _pre_goal_wizard_ctx()
+ result = workflow_update_module._pre_persist_scouted_spine_result(
+ _wizard_block_yaml(include_business_block=False), ctx
+ )
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "scouted_spine_under_build"
+ assert "#business-toggle" in result.violations[0]
+ assert "#service-address" in result.violations[0]
+
+ def test_multi_block_draft_covering_all_scouted_rungs_admitted(self) -> None:
+ ctx = _pre_goal_wizard_ctx()
+ result = workflow_update_module._pre_persist_scouted_spine_result(
+ _wizard_block_yaml(include_business_block=True), ctx
+ )
+ assert result is None
+
+ @pytest.mark.asyncio
+ async def test_multi_block_covering_draft_persists_with_multiple_blocks(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+
+ async def _fake_process_from_yaml(**kwargs: object) -> SimpleNamespace:
+ parsed = yaml.safe_load(str(kwargs["workflow_yaml"]))
+ blocks = [SimpleNamespace(label=block.get("label")) for block in parsed["workflow_definition"]["blocks"]]
+ return SimpleNamespace(workflow_definition=SimpleNamespace(blocks=blocks), proxy_location=None)
+
+ monkeypatch.setattr(workflow_update_module, "_process_workflow_yaml", _fake_process_from_yaml)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "click",
+ "selector": "#business-toggle",
+ "source_url": "https://utility.example.com/start-service",
+ "role": "button",
+ "accessible_name": "Business",
+ "trajectory_index": 0,
+ },
+ {
+ "tool_name": "type_text",
+ "selector": "#service-address",
+ "source_url": "https://utility.example.com/start-service",
+ "role": "textbox",
+ "accessible_name": "Service address",
+ "typed_length": 20,
+ "trajectory_index": 1,
+ },
+ ]
+ submitted = (
+ "title: t\n"
+ "workflow_definition:\n"
+ " parameters:\n"
+ " - {parameter_type: workflow, workflow_parameter_type: string, key: service_address, default_value: a}\n"
+ " blocks:\n"
+ " - block_type: code\n"
+ " label: open_business_services\n"
+ " code: |\n"
+ ' await page.locator("#business-toggle").click()\n'
+ " - block_type: code\n"
+ " label: fill_service_address\n"
+ " code: |\n"
+ ' await page.locator("#service-address").fill(str(service_address))\n'
+ )
+ ctx.workflow_yaml = submitted
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ assert result["data"]["block_count"] == 2
+
+
+class TestPrePersistScoutedSpineGate:
+ def test_covering_draft_returns_none(self) -> None:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ code = 'await page.locator("#search-submit").click()'
+ assert workflow_update_module._pre_persist_scouted_spine_result(_freehand_block_yaml(code), ctx) is None
+
+ def test_draft_dropping_scouted_rung_rejected_with_rung_provenance(self) -> None:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ code = 'print(await page.locator("body").inner_text())'
+ result = workflow_update_module._pre_persist_scouted_spine_result(_freehand_block_yaml(code), ctx)
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "scouted_spine_under_build"
+ assert "#search-submit" in result.violations[0]
+
+ def test_gate_skipped_without_imposition(self) -> None:
+ ctx = _code_only_ctx()
+ code = 'print(await page.locator("body").inner_text())'
+ assert workflow_update_module._pre_persist_scouted_spine_result(_freehand_block_yaml(code), ctx) is None
+
+ def test_gate_skipped_without_scout_trajectory(self) -> None:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = []
+ code = 'print(await page.locator("body").inner_text())'
+ assert workflow_update_module._pre_persist_scouted_spine_result(_freehand_block_yaml(code), ctx) is None
+
+ def test_gate_reuses_imposition_synthesized_result(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.imposition_synthesized_block = code_block_synthesis_module.synthesize_code_block(
+ ctx.scout_trajectory, strict_selectors=True, reached_download_target=None
+ )
+ assert ctx.imposition_synthesized_block is not None
+
+ def _fail_resynthesis(*_args: object, **_kwargs: object) -> None:
+ raise AssertionError("pre-persist gate re-synthesized instead of reusing the imposition result")
+
+ monkeypatch.setattr(workflow_update_module, "synthesize_code_block", _fail_resynthesis)
+ code = 'await page.locator("#search-submit").click()'
+ assert workflow_update_module._pre_persist_scouted_spine_result(_freehand_block_yaml(code), ctx) is None
+
+ def test_gate_declines_synthesizer_side_partition_findings(self) -> None:
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "click",
+ "selector": "#stage-a",
+ "source_url": "https://example.com/records",
+ "trajectory_index": 0,
+ },
+ {"tool_name": "press_key", "key": "", "trajectory_index": 1},
+ {"tool_name": "click", "selector": "#stage-b", "trajectory_index": 2},
+ ]
+ synthesized = code_block_synthesis_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)
+ assert synthesized is not None
+ covering_code = synthesized.interaction_code or synthesized.code
+ draft_calls = [
+ (mutation.method, mutation.receiver)
+ for mutation in workflow_update_module._browser_surface_for_code(covering_code)[0]
+ ]
+ findings = code_block_synthesis_module.spine_partition_findings(
+ synthesized.diagnostics, draft_calls, ctx.scout_trajectory
+ )
+ assert any(finding.kind == code_block_synthesis_module.UNFORGIVEN_DROP_FINDING for finding in findings)
+
+ result = workflow_update_module._pre_persist_scouted_spine_result(_freehand_block_yaml(covering_code), ctx)
+
+ assert result is None
+ assert ctx.code_authoring_guardrail_reject_count == 0
+
+ @pytest.mark.asyncio
+ async def test_pre_persist_reject_leaves_canonical_definition_unwritten(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ _stub_successful_update(monkeypatch)
+ monkeypatch.setattr(workflow_update_module, "_workflow_requires_canonical_persist", lambda *_a, **_k: True)
+ canonical_writes: list[dict[str, object]] = []
+
+ async def _spy_update_workflow_definition(**kwargs: object) -> None:
+ canonical_writes.append(kwargs)
+
+ monkeypatch.setattr(
+ workflow_update_module.app,
+ "WORKFLOW_SERVICE",
+ SimpleNamespace(update_workflow_definition=_spy_update_workflow_definition),
+ )
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ submitted = _freehand_block_yaml('print(await page.locator("body").inner_text())', label="report_only")
+ ctx.workflow_yaml = submitted
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is False
+ assert "under-builds the scouted spine" in result["error"]
+ assert canonical_writes == []
+ assert ctx.has_staged_proposal is False
+ assert ctx.persisted_draft_browser_calls is None
+
+ @pytest.mark.asyncio
+ async def test_covering_draft_persists_through_canonical_path(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ _stub_successful_update(monkeypatch)
+ monkeypatch.setattr(workflow_update_module, "_workflow_requires_canonical_persist", lambda *_a, **_k: True)
+ canonical_writes: list[dict[str, object]] = []
+
+ async def _spy_update_workflow_definition(**kwargs: object) -> None:
+ canonical_writes.append(kwargs)
+
+ async def _fake_created_by_stamp(*_args: object, **_kwargs: object) -> str | None:
+ return "copilot"
+
+ async def _fake_canonical_workflow(**_kwargs: object) -> SimpleNamespace:
+ return SimpleNamespace(
+ workflow_definition=SimpleNamespace(blocks=[SimpleNamespace(label="submit_search")]),
+ title="t",
+ description=None,
+ proxy_location=None,
+ webhook_callback_url=None,
+ totp_verification_url=None,
+ totp_identifier=None,
+ persist_browser_session=False,
+ pin_saved_session_ip=False,
+ browser_profile_id=None,
+ browser_profile_key=None,
+ model=None,
+ max_screenshot_scrolls=None,
+ extra_http_headers=None,
+ cdp_connect_headers=None,
+ run_with=None,
+ ai_fallback=True,
+ cache_key=None,
+ adaptive_caching=None,
+ enable_self_healing=False,
+ code_version=None,
+ run_sequentially=False,
+ sequential_key=None,
+ )
+
+ monkeypatch.setattr(
+ workflow_update_module.app,
+ "WORKFLOW_SERVICE",
+ SimpleNamespace(update_workflow_definition=_spy_update_workflow_definition),
+ )
+ monkeypatch.setattr(workflow_update_module, "resolve_copilot_created_by_stamp", _fake_created_by_stamp)
+ monkeypatch.setattr(workflow_update_module, "_process_workflow_yaml", _fake_canonical_workflow)
+ ctx = _code_only_ctx()
+ _enable_imposition(ctx)
+ submitted = _freehand_block_yaml('await page.locator("#search-submit").click()', label="submit_search")
+ ctx.workflow_yaml = submitted
+
+ result = await _update_workflow({"workflow_yaml": submitted}, ctx)
+
+ assert result["ok"] is True
+ assert len(canonical_writes) == 1
+ assert ctx.persisted_draft_browser_calls == [("click", "page.locator('#search-submit')")]
+
+
+_P6_UNDER_SCOUT_RECORDED_RUN_IDS = (
+ "wr_551756368310299524",
+ "wr_551757699750161408",
+ "wr_551759572355902654",
+)
+_P10_SPINE_COLLAPSE_RECORDED_RUN_ID = "wr_551770266824468500"
+
+# Selector shapes are transcribed verbatim from each recorded run's proposed workflow; only the mock
+# host is placeholder-normalized per this file's OSS-synced target convention.
+_P6_RECORDED_FREEHAND_BLOCKS: dict[str, tuple[str, str, tuple[str, ...]]] = {
+ "wr_551756368310299524": (
+ "lookup_order",
+ 'await page.goto("https://example.com/order-status")\n'
+ 'await page.locator("#confirmation").fill(str(enter_confirmation))\n'
+ "await page.locator('button[data-action=\"orderLookup\"]').click()\n"
+ 'await page.locator(\'button[data-action="orderDocuments"]\').wait_for(state="visible", timeout=10000)\n'
+ "await page.locator('button[data-action=\"orderDocuments\"]').click()\n",
+ ("orderLookup", "orderDocuments"),
+ ),
+ "wr_551757699750161408": (
+ "lookup_order",
+ 'await page.goto("https://example.com/order-status")\n'
+ 'await page.locator("#confirmation").fill(str(enter_confirmation))\n'
+ "await page.locator('button[data-action=\"orderLookup\"]').click()\n",
+ ("orderLookup",),
+ ),
+ "wr_551759572355902654": (
+ "retrieve_resale_document",
+ "documents_button = page.locator('button[data-action=\"orderDocuments\"]')\n"
+ "await documents_button.click()\n"
+ 'rows = page.locator("tr")\n',
+ ("documents_button",),
+ ),
+}
+
+
+class TestRecordedGauntletPacketReplay:
+ @pytest.mark.parametrize("run_id", _P6_UNDER_SCOUT_RECORDED_RUN_IDS)
+ def test_p6_recorded_freehand_block_rejected(self, run_id: str) -> None:
+ label, code, expected_tokens = _P6_RECORDED_FREEHAND_BLOCKS[run_id]
+ ctx = _under_scouted_order_status_ctx()
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code, label=label), ctx
+ )
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "freehand_unresolvable_selector"
+ assert result.repair_context.block_label == label
+ for token in expected_tokens:
+ assert token in result.violations[0]
+
+ def test_p6_recorded_freehand_reject_carries_fragment_scout_site(self) -> None:
+ label, code, _ = _P6_RECORDED_FREEHAND_BLOCKS[_P6_UNDER_SCOUT_RECORDED_RUN_IDS[0]]
+ ctx = _under_scouted_order_status_ctx()
+ with capture_logs() as logs:
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code, label=label), ctx
+ )
+ assert result is not None
+ events = [log for log in logs if log["event"] == "copilot_browser_surface_rejection_provenance"]
+ assert events and events[0]["site"] == "fragment_scout"
+
+ def test_p6_recorded_scouted_confirmation_replay_admitted(self) -> None:
+ ctx = _under_scouted_order_status_ctx()
+ code = (
+ 'await page.goto("https://example.com/order-status")\nawait page.locator("#confirmation").fill("ABC123")\n'
+ )
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code, label="lookup_order"), ctx
+ )
+ assert result is None
+
+ def test_p6_cap_evicted_scouted_selector_still_admitted(self) -> None:
+ ctx = _under_scouted_order_status_ctx()
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "click",
+ "selector": "#search-submit",
+ "source_url": "https://example.com/order-status",
+ "role": "button",
+ "accessible_name": "Search",
+ "trajectory_index": 0,
+ }
+ ]
+ ctx.scouted_interactions = [
+ {
+ "tool_name": "type_text",
+ "selector": "#confirmation",
+ "source_url": "https://example.com/order-status",
+ "role": "textbox",
+ "accessible_name": "Confirmation number",
+ "typed_length": 8,
+ "trajectory_index": 0,
+ }
+ ]
+ assert enforcement_module.synthesized_trajectory_reaches_goal(ctx) is False
+ code = 'await page.locator("#confirmation").fill("ABC123")'
+ result = workflow_update_module._persist_seam_freehand_surface_result(
+ _freehand_block_yaml(code, label="lookup_order"), ctx
+ )
+ assert result is None
+
+ @pytest.mark.parametrize("run_id", [_P10_SPINE_COLLAPSE_RECORDED_RUN_ID])
+ def test_p10_recorded_sign_in_only_collapse_rejected_naming_dropped_rungs(self, run_id: str) -> None:
+ ctx = _pre_goal_wizard_ctx()
+ result = workflow_update_module._pre_persist_scouted_spine_result(
+ _wizard_block_yaml(include_business_block=False), ctx
+ )
+ assert result is not None
+ assert result.repair_context is not None
+ assert result.repair_context.reason_code == "scouted_spine_under_build"
+ assert "#business-toggle" in result.violations[0]
+ assert "#service-address" in result.violations[0]
+
+ @pytest.mark.parametrize("run_id", [_P10_SPINE_COLLAPSE_RECORDED_RUN_ID])
+ def test_p10_recorded_covering_multi_block_spine_admitted(self, run_id: str) -> None:
+ ctx = _pre_goal_wizard_ctx()
+ result = workflow_update_module._pre_persist_scouted_spine_result(
+ _wizard_block_yaml(include_business_block=True), ctx
+ )
+ assert result is None
diff --git a/tests/unit/test_copilot_enforcement_hardening.py b/tests/unit/test_copilot_enforcement_hardening.py
index 3fbee5520..a6199b2d1 100644
--- a/tests/unit/test_copilot_enforcement_hardening.py
+++ b/tests/unit/test_copilot_enforcement_hardening.py
@@ -59,6 +59,7 @@ from skyvern.forge.sdk.copilot.enforcement import (
_strip_input_images,
register_no_progress_interaction_click,
reset_no_progress_interaction_count,
+ synthesized_trajectory_reaches_goal,
)
from skyvern.forge.sdk.copilot.output_contracts import OutputContractAdvisoryState
from skyvern.forge.sdk.copilot.run_outcome import TERMINAL_CHALLENGE_BLOCKER_REASON_CODE
@@ -829,3 +830,20 @@ def test_grant_plus_ceiling_reject_in_one_call_reaches_stalled_terminal_not_chur
signal = ctx.blocker_signal
assert isinstance(signal, CopilotToolBlockerSignal)
assert signal.internal_reason_code == ADVISORY_DISPATCH_STALLED_REASON_CODE
+
+
+def test_single_captured_interaction_trajectory_never_reaches_goal() -> None:
+ ctx = _fresh_context()
+ ctx.scout_trajectory = [
+ {
+ "tool_name": "type_text",
+ "selector": "#confirmation",
+ "source_url": "https://portal.example.com/order-status",
+ "role": "textbox",
+ "accessible_name": "Confirmation number",
+ "typed_length": 8,
+ "trajectory_index": 0,
+ }
+ ]
+
+ assert synthesized_trajectory_reaches_goal(ctx) is False