Skip to content

Automate browser based workflows with AI

fix(SKY-12332): reconcile the persisted copilot block against the full captured trajectory (#7375)

Andrew Neilson2dbb46404ca9
diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.py
index 6ae49393f..a75268419 100644
--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py
+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py
@@ -280,6 +280,9 @@ class SynthesisDiagnostics:
forgiven_interactions: list[dict[str, Any]] = field(default_factory=list)
download_terminal_anchor: int | None = None
download_terminal_dropped_trailing: int = 0
+ # Post-download-cut trajectory indices recorded before the emission loop, so the partition obligation
+ # can detect a truncation-break index that lands in no record lane instead of silently losing it.
+ retained_trajectory_indices: list[int] = field(default_factory=list)
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 == "").
@@ -678,6 +681,25 @@ def is_optional_dismissal_only_trajectory(trajectory: Sequence[Mapping[str, Any]
)
+def _is_anonymous_structural_dismissal_click(interaction: Mapping[str, Any]) -> bool:
+ return _is_structural_dismissal_click(interaction) and not _is_optional_dismissal_click(interaction)
+
+
+def _last_action_interaction_index(trajectory: Sequence[Mapping[str, Any]]) -> int:
+ last = -1
+ for index, interaction in enumerate(trajectory):
+ tool_name = str(interaction.get("tool_name") or "")
+ if tool_name not in _ENTRY_TARGET_TOOLS:
+ continue
+ # An empty-key press_key is dropped as missing_key and emits nothing, so it must not claim the
+ # terminal index — otherwise a trailing empty keypress steals it from a real terminal dismissal
+ # click and defeats the reclassify-to-required guard.
+ if tool_name == "press_key" and not str(interaction.get("key") or "").strip():
+ continue
+ last = index
+ return last
+
+
def _optional_dismissal_locator_expr(interaction: Mapping[str, Any], fallback_locator: str) -> str:
selector = str(interaction.get("selector") or "").strip()
if _NOT_DECLINE_BUTTON_SELECTOR_PATTERN.match(selector) or _is_cookie_accept_xpath_selector(selector):
@@ -818,6 +840,7 @@ def synthesize_code_block(
anchor=reached_download_target.trajectory_anchor,
dropped_trailing_count=dropped_trailing,
)
+ diagnostics.retained_trajectory_indices = list(range(len(trajectory)))
def append_step(description: str, action_type: str, line_start: int) -> None:
steps.append(
@@ -999,6 +1022,7 @@ def synthesize_code_block(
append_step(f"Open {entry_url}", "goto_url", line_start)
emitted = 0
+ terminal_action_index = _last_action_interaction_index(trajectory)
deferred_readonly_assertions: list[tuple[int, str, str, str]] = []
def action_indent_for(trajectory_index: int) -> str:
@@ -1085,7 +1109,12 @@ def synthesize_code_block(
line_start = len(lines) + 1
if tool_name == "click":
- if _is_optional_or_structural_dismissal_click(interaction):
+ reclassify_terminal_required = (
+ trajectory_index == terminal_action_index
+ and _is_anonymous_structural_dismissal_click(interaction)
+ and any(not str(record.get("lane") or "") for record in diagnostics.emitted_interactions)
+ )
+ if _is_optional_or_structural_dismissal_click(interaction) and not reclassify_terminal_required:
optional_locator = _optional_dismissal_locator_expr(interaction, locator)
lines.append(f"{action_indent}{_OPTIONAL_DISMISSAL_VAR} = {optional_locator}")
lines.append(f"{action_indent}if await {_OPTIONAL_DISMISSAL_VAR}.count() > 0:")
@@ -1309,6 +1338,9 @@ def synthesize_code_block(
SCOUTED_SPINE_UNDER_BUILD_REASON_CODE = "scouted_spine_under_build"
+SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE = "scouted_spine_dropped_unforgiven"
+SCOUTED_SPINE_UNRECORDED_INDEX_REASON_CODE = "scouted_spine_unrecorded_index"
+SCOUTED_SPINE_TRUNCATED_REASON_CODE = "scouted_spine_truncated"
def normalized_locator_expr(text: str) -> str:
@@ -1383,6 +1415,267 @@ def uncovered_required_emitted_interactions(
return uncovered
+_IDENTITY_QUALIFIER_BOUNDARY = ("[", "#", ".")
+_FILTERING_PSEUDO_CLASSES = (
+ ":visible",
+ ":enabled",
+ ":disabled",
+ ":checked",
+ ":not(",
+ ":has(",
+ ":has-text(",
+ ":text(",
+ ":is(",
+)
+_EXACT_TEXT_XPATH_TAG_RE = re.compile(
+ r"""^(?:xpath=)?//(?P<tag>[a-zA-Z][a-zA-Z0-9-]*)\s*\[\s*normalize-space\(\s*(?:\.|text\(\))?\s*\)\s*=\s*(?P<quote>['"])[^'"]+(?P=quote)\s*\]\s*$"""
+)
+
+
+def _qualifier_narrows_to_identity(qualifier: str) -> bool:
+ if not qualifier or qualifier[0] not in _IDENTITY_QUALIFIER_BOUNDARY:
+ return False
+ if any(pseudo in qualifier for pseudo in _FILTERING_PSEUDO_CLASSES):
+ return False
+ bracket_depth = 0
+ quote: str | None = None
+ for char in qualifier:
+ if quote is not None:
+ if char == quote:
+ quote = None
+ elif char in ("'", '"'):
+ quote = char
+ elif char == "[":
+ bracket_depth += 1
+ elif char == "]":
+ bracket_depth = max(0, bracket_depth - 1)
+ elif bracket_depth == 0 and (char.isspace() or char in ">+~"):
+ return False
+ return True
+
+
+def _selector_refines(bare: str, candidate: str) -> bool:
+ bare = bare.strip()
+ candidate = candidate.strip()
+ if not bare or not candidate or bare == candidate:
+ return False
+
+ bare_role = _parse_role_name(bare)
+ candidate_role = _parse_role_name(candidate)
+ if bare_role is not None or candidate_role is not None:
+ if bare_role is None or candidate_role is None:
+ return False
+ bare_role_name, bare_name, bare_suffix = bare_role
+ candidate_role_name, candidate_name, candidate_suffix = candidate_role
+ return (
+ bare_role_name == candidate_role_name
+ and not bare_name
+ and not bare_suffix
+ and bool(candidate_name)
+ and not candidate_suffix
+ )
+ if not _BARE_TAG_RE.match(bare):
+ return False
+ if not candidate.startswith(bare) or _is_positional_selector(candidate):
+ return False
+ return _qualifier_narrows_to_identity(candidate[len(bare) :])
+
+
+def _stable_same_kind_bare_click_refiner(bare: str, candidate: str) -> bool:
+ bare = bare.strip()
+ candidate = candidate.strip()
+ if not bare or not candidate or bare == candidate or _is_positional_selector(candidate):
+ return False
+ if _selector_refines(bare, candidate):
+ return True
+ if bare != "button":
+ return False
+
+ candidate_role = _parse_role_name(candidate)
+ if candidate_role is not None:
+ role_name, accessible_name, suffix = candidate_role
+ return role_name == "button" and bool(accessible_name) and not suffix
+
+ xpath_match = _EXACT_TEXT_XPATH_TAG_RE.match(candidate)
+ return xpath_match is not None and xpath_match.group("tag").casefold() == "button"
+
+
+def _is_ignorable_entry_opener_drop(dropped: Mapping[str, Any], diagnostics: SynthesisDiagnostics) -> bool:
+ return (
+ dropped.get("reason_code") == "ambiguous_bare_selector"
+ and dropped.get("tool_name") == "click"
+ and dropped.get("trajectory_index") == 0
+ and str(dropped.get("selector") or "").strip() in {"button", "role=button"}
+ and bool(diagnostics.locator_provenance)
+ )
+
+
+def _bare_drop_superseded_on_screen(
+ dropped: Mapping[str, Any],
+ scout_trajectory: Sequence[Mapping[str, Any]],
+ *,
+ claimed_refiner_indices: set[int],
+) -> tuple[bool, dict[str, Any] | None]:
+ if dropped.get("reason_code") != "ambiguous_bare_selector" or dropped.get("tool_name") != "click":
+ return False, None
+ dropped_selector = str(dropped.get("selector") or "").strip()
+ if not dropped_selector:
+ return False, None
+
+ dropped_index = dropped.get("trajectory_index")
+ if not isinstance(dropped_index, int) or dropped_index < 0 or dropped_index >= len(scout_trajectory):
+ return False, None
+ source_url = str(scout_trajectory[dropped_index].get("source_url") or "").strip()
+ if not source_url:
+ return False, None
+
+ for refiner_index in range(dropped_index + 1, len(scout_trajectory)):
+ if refiner_index in claimed_refiner_indices:
+ continue
+ later = scout_trajectory[refiner_index]
+ if later.get("tool_name") != "click":
+ continue
+ if str(later.get("source_url") or "").strip() != source_url:
+ continue
+ later_selector = str(later.get("selector") or "").strip()
+ if not _stable_same_kind_bare_click_refiner(dropped_selector, later_selector):
+ continue
+ claimed_refiner_indices.add(refiner_index)
+ return True, {
+ "dropped_index": dropped_index,
+ "dropped_selector": dropped_selector,
+ "refiner_index": refiner_index,
+ "refiner_selector": later_selector,
+ "source_url": source_url,
+ }
+ return False, None
+
+
+UNCOVERED_RUNG_FINDING = "uncovered_rung"
+UNFORGIVEN_DROP_FINDING = "unforgiven_drop"
+UNRECORDED_INDEX_FINDING = "unrecorded_index"
+TRUNCATED_FINDING = "truncated"
+
+
+@dataclass(frozen=True, slots=True)
+class ObligationFinding:
+ kind: str
+ record: Mapping[str, Any] | None = None
+ trajectory_index: int | None = None
+
+
+def forgiven_dropped_indices(
+ diagnostics: SynthesisDiagnostics, scout_trajectory: Sequence[Mapping[str, Any]]
+) -> set[int]:
+ """Trajectory indices whose drop the closed forgiveness allowlist absolves, re-derived from the
+ synthesized diagnostics and trajectory so no forgiveness record needs to be transported."""
+ forgiven: set[int] = set()
+ claimed_refiner_indices: set[int] = set()
+ for dropped in diagnostics.dropped_interactions:
+ index = dropped.get("trajectory_index")
+ if _is_ignorable_entry_opener_drop(dropped, diagnostics):
+ if isinstance(index, int):
+ forgiven.add(index)
+ continue
+ superseded, _ = _bare_drop_superseded_on_screen(
+ dropped, scout_trajectory, claimed_refiner_indices=claimed_refiner_indices
+ )
+ if superseded and isinstance(index, int):
+ forgiven.add(index)
+ return forgiven
+
+
+def _recorded_partition_indices(diagnostics: SynthesisDiagnostics) -> set[int]:
+ recorded: set[int] = set()
+ for group in (
+ diagnostics.emitted_interactions,
+ diagnostics.dropped_interactions,
+ diagnostics.forgiven_interactions,
+ ):
+ for record in group:
+ index = record.get("trajectory_index")
+ if isinstance(index, int):
+ recorded.add(index)
+ return recorded
+
+
+def spine_partition_findings(
+ diagnostics: SynthesisDiagnostics,
+ draft_calls: Sequence[tuple[str, str]],
+ scout_trajectory: Sequence[Mapping[str, Any]],
+) -> list[ObligationFinding]:
+ """Partition-exhaustiveness obligation over the full retained-index manifest: an uncovered required
+ rung, a dropped interaction the allowlist does not forgive, a retained index in no record lane, or a
+ truncation are each a typed under-build finding. Forgiveness names the reason; it never absolves."""
+ findings: list[ObligationFinding] = []
+ for record in uncovered_required_emitted_interactions(diagnostics.emitted_interactions, draft_calls):
+ index = record.get("trajectory_index")
+ findings.append(
+ ObligationFinding(
+ kind=UNCOVERED_RUNG_FINDING,
+ record=record,
+ trajectory_index=index if isinstance(index, int) else None,
+ )
+ )
+ forgiven = forgiven_dropped_indices(diagnostics, scout_trajectory)
+ for dropped in diagnostics.dropped_interactions:
+ index = dropped.get("trajectory_index")
+ if isinstance(index, int) and index in forgiven:
+ continue
+ findings.append(
+ ObligationFinding(
+ kind=UNFORGIVEN_DROP_FINDING,
+ record=dropped,
+ trajectory_index=index if isinstance(index, int) else None,
+ )
+ )
+ recorded = _recorded_partition_indices(diagnostics)
+ for index in diagnostics.retained_trajectory_indices:
+ if index not in recorded:
+ findings.append(ObligationFinding(kind=UNRECORDED_INDEX_FINDING, trajectory_index=index))
+ if diagnostics.truncated:
+ findings.append(ObligationFinding(kind=TRUNCATED_FINDING))
+ return findings
+
+
+def uncovered_rung_records(findings: Sequence[ObligationFinding]) -> list[Mapping[str, Any]]:
+ return [finding.record for finding in findings if finding.kind == UNCOVERED_RUNG_FINDING and finding.record]
+
+
+def obligation_finding_reason_code(finding: ObligationFinding) -> str:
+ if finding.kind == UNCOVERED_RUNG_FINDING:
+ return SCOUTED_SPINE_UNDER_BUILD_REASON_CODE
+ if finding.kind == UNFORGIVEN_DROP_FINDING:
+ return SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE
+ if finding.kind == UNRECORDED_INDEX_FINDING:
+ return SCOUTED_SPINE_UNRECORDED_INDEX_REASON_CODE
+ return SCOUTED_SPINE_TRUNCATED_REASON_CODE
+
+
+def obligation_finding_selector(finding: ObligationFinding) -> str | None:
+ if finding.record is None:
+ return None
+ return str(finding.record.get("selector") or "") or None
+
+
+def obligation_finding_text(finding: ObligationFinding) -> str:
+ if finding.kind == UNCOVERED_RUNG_FINDING:
+ return missing_rung_text([finding.record]) if finding.record else "an uncovered scouted rung"
+ if finding.kind == UNFORGIVEN_DROP_FINDING:
+ record = finding.record or {}
+ tool_name = str(record.get("tool_name") or "unknown")
+ reason = str(record.get("reason_code") or "unknown")
+ index = record.get("trajectory_index", "?")
+ return f"dropped scout interaction {index} from `{tool_name}` ({reason})"
+ if finding.kind == UNRECORDED_INDEX_FINDING:
+ return f"scout interaction {finding.trajectory_index} was retained but landed in no persisted or forgiven lane"
+ return "the scout trajectory was truncated before every captured interaction was compiled"
+
+
+def render_obligation_findings(findings: Sequence[ObligationFinding]) -> str:
+ return "; ".join(obligation_finding_text(finding) for finding in findings)
+
+
def missing_rung_text(uncovered: Sequence[Mapping[str, Any]]) -> str:
return ", ".join(
f"`{str(record.get('method') or '')}` on {str(record.get('selector') or record.get('locator') or '')!r}"
diff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.py
index 9ba76f801..0135fe753 100644
--- a/skyvern/forge/sdk/copilot/enforcement.py
+++ b/skyvern/forge/sdk/copilot/enforcement.py
@@ -40,7 +40,7 @@ from skyvern.forge.sdk.copilot.challenge_evidence import composition_challenge_c
from skyvern.forge.sdk.copilot.code_block_synthesis import (
CREDENTIAL_FILL_TOOL_NAME,
LIVE_SCOUT_CREDENTIAL_FIELDS,
- SCOUTED_SPINE_UNDER_BUILD_REASON_CODE,
+ ObligationFinding,
credential_scout_gap,
first_matched_post_fill_submit_index,
freeze_requested_output_extraction_candidate,
@@ -48,12 +48,16 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
is_generic_entry_opener_click,
is_optional_dismissal_only_trajectory,
missing_rung_text,
+ obligation_finding_reason_code,
+ obligation_finding_selector,
render_missing_rung_call_sources,
+ render_obligation_findings,
render_synthesized_offer_text,
+ spine_partition_findings,
synthesize_code_block,
synthesize_code_block_with_extraction,
trajectory_has_browser_fill_interaction,
- uncovered_required_emitted_interactions,
+ uncovered_rung_records,
)
from skyvern.forge.sdk.copilot.completion_criteria_store import requested_output_paths
from skyvern.forge.sdk.copilot.completion_verification import only_structural_requested_output_abstentions
@@ -474,9 +478,10 @@ _CHURN_REASON_CODES = frozenset({"code_authoring_guardrail_churn", "credential_p
_SCOUTED_SPINE_CHECKPOINT_BLOCK_LABEL = "persisted_draft"
-def _scouted_spine_open_obligation(ctx: AgentContext) -> list[Mapping[str, Any]]:
- """Required scouted rungs the latest persisted draft leaves uncovered; empty when no in-turn
- persist exists or coverage holds."""
+def _scouted_spine_open_obligation(ctx: AgentContext) -> list[ObligationFinding]:
+ """Partition-exhaustiveness findings the latest persisted draft leaves open — uncovered required
+ rungs, dropped interactions the allowlist does not forgive, retained indices in no lane, and
+ truncation; empty when no in-turn persist exists or the full manifest is accounted for."""
persisted_calls = ctx.persisted_draft_browser_calls
if persisted_calls is None:
return []
@@ -496,15 +501,20 @@ def _scouted_spine_open_obligation(ctx: AgentContext) -> list[Mapping[str, Any]]
)
if synthesized is None:
return []
- return uncovered_required_emitted_interactions(synthesized.diagnostics.emitted_interactions, persisted_calls)
+ return spine_partition_findings(synthesized.diagnostics, persisted_calls, trajectory)
-def _log_scouted_spine_unresolved(uncovered: list[Mapping[str, Any]], *, site: str) -> None:
+def _scouted_spine_missing_text(findings: list[ObligationFinding]) -> str:
+ uncovered = uncovered_rung_records(findings)
+ return missing_rung_text(uncovered) if uncovered else render_obligation_findings(findings)
+
+
+def _log_scouted_spine_unresolved(findings: list[ObligationFinding], *, site: str) -> None:
LOG.info(
"copilot_scouted_spine_under_build_unresolved",
site=site,
- missing_rung_count=len(uncovered),
- missing_rungs=missing_rung_text(uncovered),
+ missing_rung_count=len(uncovered_rung_records(findings)),
+ missing_rungs=_scouted_spine_missing_text(findings),
)
@@ -518,46 +528,50 @@ SCOUTED_SPINE_TURN_HALT_USER_REASON = (
def log_scouted_spine_unresolved_at_turn_halt(ctx: AgentContext) -> bool:
"""Log-only and never raises: a failed obligation read must not block rendering the halt reply."""
try:
- uncovered = _scouted_spine_open_obligation(ctx)
+ findings = _scouted_spine_open_obligation(ctx)
except Exception:
LOG.warning("copilot_scouted_spine_turn_halt_check_failed", exc_info=True)
return False
- if not uncovered:
+ if not findings:
return False
- _log_scouted_spine_unresolved(uncovered, site="turn_halt")
+ _log_scouted_spine_unresolved(findings, site="turn_halt")
return True
def _scouted_spine_turn_end_nudge(ctx: AgentContext) -> str | None:
try:
- uncovered = _scouted_spine_open_obligation(ctx)
+ findings = _scouted_spine_open_obligation(ctx)
except Exception:
LOG.warning("copilot_scouted_spine_turn_end_check_failed", exc_info=True)
return None
- if not uncovered:
+ if not findings:
return None
if ctx.scouted_spine_checkpoint_fired:
- _log_scouted_spine_unresolved(uncovered, site="turn_end")
+ _log_scouted_spine_unresolved(findings, site="turn_end")
return None
ctx.scouted_spine_checkpoint_fired = True
+ first = findings[0]
+ reason_code = obligation_finding_reason_code(first)
repair_context = CodeAuthoringRepairContext(
block_label=_SCOUTED_SPINE_CHECKPOINT_BLOCK_LABEL,
- reason_code=SCOUTED_SPINE_UNDER_BUILD_REASON_CODE,
- selector=str(uncovered[0].get("selector") or "") or None,
+ reason_code=reason_code,
+ selector=obligation_finding_selector(first),
)
ctx.last_code_authoring_repair_context = repair_context
record_build_test_outcome(ctx, recorded_outcome_from_authoring_repair_context(repair_context))
_record_code_authoring_guardrail_reject(ctx)
- missing_text = missing_rung_text(uncovered)
+ uncovered = uncovered_rung_records(findings)
+ missing_text = _scouted_spine_missing_text(findings)
LOG.info(
"copilot_scouted_spine_under_build",
block_label=_SCOUTED_SPINE_CHECKPOINT_BLOCK_LABEL,
site="turn_end",
+ reason_code=reason_code,
missing_rung_count=len(uncovered),
missing_rungs=missing_text,
)
nudge = (
- f"The persisted draft under-builds the scouted spine ({SCOUTED_SPINE_UNDER_BUILD_REASON_CODE}): "
+ f"The persisted draft under-builds the scouted spine ({reason_code}): "
f"missing rung(s): {missing_text}. Resubmit the code block through update_workflow so every scouted "
"rung is replayed — reuse the synthesized code block verbatim."
)
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index 6c7bb46a6..5a406a16d 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -53,18 +53,24 @@ from skyvern.forge.sdk.copilot.code_block_preflight import (
from skyvern.forge.sdk.copilot.code_block_security import CodeBlockSecurityError, author_time_code_security_errors
from skyvern.forge.sdk.copilot.code_block_steps import apply_derived_code_block_steps, fill_code_block_prompts_in_yaml
from skyvern.forge.sdk.copilot.code_block_synthesis import (
- _BARE_TAG_RE,
_CODE_SUBMIT_ACTION_RE,
_INTERNAL_SCOUT_VARS,
_SYNTHESIZED_BLOCK_LABEL,
CREDENTIAL_FILL_TOOL_NAME,
+ SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE,
+ SCOUTED_SPINE_TRUNCATED_REASON_CODE,
SCOUTED_SPINE_UNDER_BUILD_REASON_CODE,
+ SCOUTED_SPINE_UNRECORDED_INDEX_REASON_CODE,
+ UNCOVERED_RUNG_FINDING,
+ ObligationFinding,
SynthesisDiagnostics,
SynthesizedCodeBlock,
+ _bare_drop_superseded_on_screen,
_credential_field_accesses,
_get_by_role_expr_strict,
+ _is_ignorable_entry_opener_drop,
_is_positional_selector,
- _parse_role_name,
+ _selector_refines,
artifact_dependency_id,
artifact_observation_ref_id,
credential_scout_gap,
@@ -72,11 +78,16 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
locator_selector_literals,
missing_rung_text,
normalized_locator_expr,
+ obligation_finding_reason_code,
+ obligation_finding_selector,
produce_covered_static_return_envelope,
render_missing_rung_call_sources,
+ render_obligation_findings,
+ spine_partition_findings,
synthesize_code_block,
synthesize_code_block_with_extraction,
uncovered_required_emitted_interactions,
+ uncovered_rung_records,
)
from skyvern.forge.sdk.copilot.composition_evidence import (
SCOUT_INTERACTION_EVIDENCE_TOOL,
@@ -5434,6 +5445,7 @@ class _SynthesizedCodeImpositionResult:
repair_context: CodeAuthoringRepairContext | None = None
scrubbed_selected_metadata_label: str | None = None
selected_extraction_metadata_disposition: SelectedExtractionMetadataDisposition = "none"
+ minted_parameter_keys: list[str] = dataclass_field(default_factory=list)
_SUBMITTED_LITERAL_METHODS = frozenset({"fill", "type"})
@@ -5849,15 +5861,18 @@ def _stale_spine_rung_provenance(
return provenance
-def _draft_leaves_scouted_rungs_uncovered(
- code_blocks: list[dict[str, Any]], *, synthesized: SynthesizedCodeBlock
+def _draft_leaves_scouted_partition_open(
+ code_blocks: list[dict[str, Any]],
+ *,
+ synthesized: SynthesizedCodeBlock,
+ scout_trajectory: list[ScoutedInteraction],
) -> 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))
+ return bool(spine_partition_findings(synthesized.diagnostics, draft_calls, scout_trajectory))
def _code_block_browser_actions(block: Mapping[str, Any]) -> list[str]:
@@ -5906,7 +5921,24 @@ def _drop_stale_spine_rung_blocks(
carrier_label: str,
provenance: list[_BrowserSurfaceRejectionProvenance],
) -> _StaleSpineRungReplacement:
- undroppable = [block for block in stale_rungs if _code_block_output_is_referenced(parsed, block)]
+ definition = parsed.get("workflow_definition")
+ all_blocks = definition.get("blocks") if isinstance(definition, dict) else None
+ surviving_blocks = [
+ block
+ for block in (all_blocks if isinstance(all_blocks, list) else [])
+ if all(block is not stale for stale in stale_rungs)
+ ]
+ # A stale rung that a surviving block still points at via next_block_label/branch cannot be dropped:
+ # removing it would manufacture the dangling reference that _dangling_next_block_label_violation rejects.
+ # References from the stale set itself do not count (those pointers leave with the dropped blocks).
+ _, surviving_references = _workflow_block_graph({"workflow_definition": {"blocks": surviving_blocks}})
+ graph_referenced_labels = set(surviving_references)
+ undroppable = [
+ block
+ for block in stale_rungs
+ if _code_block_output_is_referenced(parsed, block)
+ or str(block.get("label") or "").strip() in graph_referenced_labels
+ ]
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(
@@ -5927,16 +5959,6 @@ def _drop_stale_spine_rung_blocks(
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"
- and dropped.get("tool_name") == "click"
- and dropped.get("trajectory_index") == 0
- and str(dropped.get("selector") or "").strip() in {"button", "role=button"}
- and bool(diagnostics.locator_provenance)
- )
-
-
def _locator_provenance_is_self_validating(provenance: Mapping[str, Any]) -> bool:
source = provenance.get("source")
if source == "selector":
@@ -6642,6 +6664,14 @@ def _browser_surface_contains_full_action_spine(submitted_code: str, synthesized
_SCOUTED_SPINE_UNDER_BUILD_REASON_CODE = SCOUTED_SPINE_UNDER_BUILD_REASON_CODE
+_SCOUTED_SPINE_REASON_CODES = frozenset(
+ {
+ SCOUTED_SPINE_UNDER_BUILD_REASON_CODE,
+ SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE,
+ SCOUTED_SPINE_UNRECORDED_INDEX_REASON_CODE,
+ SCOUTED_SPINE_TRUNCATED_REASON_CODE,
+ }
+)
def _synthesized_resubmission_credential_scout_requirements(
@@ -6686,55 +6716,98 @@ def _scouted_spine_under_build_result(
# Lane-flagged emissions (optional dismissals, readonly verifies, entry recovery) are conditional
# or read-only replays, not load-bearing rungs.
required = [record for record in diagnostics.emitted_interactions if not str(record.get("lane") or "")]
- if not required:
- return None
draft_calls = [
(mutation.method, mutation.receiver) for code in draft_codes for mutation in _browser_surface_for_code(code)[0]
]
- uncovered = uncovered_required_emitted_interactions(diagnostics.emitted_interactions, draft_calls)
- if not uncovered:
+ uncovered = (
+ uncovered_required_emitted_interactions(diagnostics.emitted_interactions, draft_calls) if required else []
+ )
+ if uncovered:
+ credential_scout_requirements = _synthesized_resubmission_credential_scout_requirements(
+ ctx, synthesized, block_label=block_label
+ )
+ missing_text = missing_rung_text(uncovered)
+ LOG.info(
+ "copilot_scouted_spine_under_build",
+ block_label=block_label,
+ site=site,
+ required_rung_count=len(required),
+ covered_rung_count=len(required) - len(uncovered),
+ missing_rungs=missing_text,
+ credential_scout_precondition_pending=bool(credential_scout_requirements),
+ )
+ first_uncovered = uncovered[0]
+ pass_route = render_missing_rung_call_sources(uncovered)
+ violation = (
+ f"Unable to impose synthesized code block: `{block_label}` under-builds the scouted spine "
+ f"({_SCOUTED_SPINE_UNDER_BUILD_REASON_CODE}): the draft covers "
+ f"{len(required) - len(uncovered)} of {len(required)} scouted rung(s); missing: {missing_text}. "
+ )
+ if credential_scout_requirements:
+ violation += (
+ "Two steps are required, in order. Step 1 — satisfy the credential-scout precondition: "
+ + " ".join(credential_scout_requirements)
+ + " Step 2 — resubmit the code block, reusing the synthesized rung source verbatim so every "
+ "scouted rung is replayed."
+ )
+ else:
+ violation += (
+ "Author the remaining synthesized rungs — reuse the synthesized code block verbatim so every "
+ "scouted rung is replayed."
+ )
+ if pass_route:
+ violation += "\n" + pass_route
+ return _SynthesizedCodeImpositionResult(
+ workflow_yaml=workflow_yaml,
+ violations=[violation],
+ repair_context=CodeAuthoringRepairContext(
+ block_label=block_label,
+ reason_code=_SCOUTED_SPINE_UNDER_BUILD_REASON_CODE,
+ selector=str(first_uncovered.get("selector") or "") or None,
+ ),
+ )
+ partition_findings = [
+ finding
+ for finding in spine_partition_findings(diagnostics, draft_calls, ctx.scout_trajectory or [])
+ if finding.kind != UNCOVERED_RUNG_FINDING
+ ]
+ if not partition_findings:
return None
- credential_scout_requirements = _synthesized_resubmission_credential_scout_requirements(
- ctx, synthesized, block_label=block_label
+ return _scouted_spine_partition_under_build_result(
+ workflow_yaml, findings=partition_findings, block_label=block_label, site=site
)
- missing_text = missing_rung_text(uncovered)
+
+
+def _scouted_spine_partition_under_build_result(
+ workflow_yaml: str,
+ *,
+ findings: list[ObligationFinding],
+ block_label: str,
+ site: str,
+) -> _SynthesizedCodeImpositionResult:
+ first = findings[0]
+ reason_code = obligation_finding_reason_code(first)
+ summary = render_obligation_findings(findings)
LOG.info(
"copilot_scouted_spine_under_build",
block_label=block_label,
site=site,
- required_rung_count=len(required),
- covered_rung_count=len(required) - len(uncovered),
- missing_rungs=missing_text,
- credential_scout_precondition_pending=bool(credential_scout_requirements),
+ reason_code=reason_code,
+ finding_count=len(findings),
+ finding_summary=summary,
)
- first_uncovered = uncovered[0]
- pass_route = render_missing_rung_call_sources(uncovered)
violation = (
f"Unable to impose synthesized code block: `{block_label}` under-builds the scouted spine "
- f"({_SCOUTED_SPINE_UNDER_BUILD_REASON_CODE}): the draft covers "
- f"{len(required) - len(uncovered)} of {len(required)} scouted rung(s); missing: {missing_text}. "
+ f"({reason_code}): {summary}. Resubmit the code block reusing the synthesized rung source verbatim so "
+ "every captured interaction is replayed or carries a forgiven-drop reason."
)
- if credential_scout_requirements:
- violation += (
- "Two steps are required, in order. Step 1 — satisfy the credential-scout precondition: "
- + " ".join(credential_scout_requirements)
- + " Step 2 — resubmit the code block, reusing the synthesized rung source verbatim so every "
- "scouted rung is replayed."
- )
- else:
- violation += (
- "Author the remaining synthesized rungs — reuse the synthesized code block verbatim so every "
- "scouted rung is replayed."
- )
- if pass_route:
- violation += "\n" + pass_route
return _SynthesizedCodeImpositionResult(
workflow_yaml=workflow_yaml,
violations=[violation],
repair_context=CodeAuthoringRepairContext(
block_label=block_label,
- reason_code=_SCOUTED_SPINE_UNDER_BUILD_REASON_CODE,
- selector=str(first_uncovered.get("selector") or "") or None,
+ reason_code=reason_code,
+ selector=obligation_finding_selector(first),
),
)
@@ -6791,132 +6864,6 @@ def _workflow_yaml_browser_call_pairs(workflow_yaml: str) -> list[tuple[str, str
]
-_IDENTITY_QUALIFIER_BOUNDARY = ("[", "#", ".")
-_FILTERING_PSEUDO_CLASSES = (
- ":visible",
- ":enabled",
- ":disabled",
- ":checked",
- ":not(",
- ":has(",
- ":has-text(",
- ":text(",
- ":is(",
-)
-_EXACT_TEXT_XPATH_TAG_RE = re.compile(
- r"""^(?:xpath=)?//(?P<tag>[a-zA-Z][a-zA-Z0-9-]*)\s*\[\s*normalize-space\(\s*(?:\.|text\(\))?\s*\)\s*=\s*(?P<quote>['"])[^'"]+(?P=quote)\s*\]\s*$"""
-)
-
-
-def _qualifier_narrows_to_identity(qualifier: str) -> bool:
- if not qualifier or qualifier[0] not in _IDENTITY_QUALIFIER_BOUNDARY:
- return False
- if any(pseudo in qualifier for pseudo in _FILTERING_PSEUDO_CLASSES):
- return False
- bracket_depth = 0
- quote: str | None = None
- for char in qualifier:
- if quote is not None:
- if char == quote:
- quote = None
- elif char in ("'", '"'):
- quote = char
- elif char == "[":
- bracket_depth += 1
- elif char == "]":
- bracket_depth = max(0, bracket_depth - 1)
- elif bracket_depth == 0 and (char.isspace() or char in ">+~"):
- return False
- return True
-
-
-def _selector_refines(bare: str, candidate: str) -> bool:
- bare = bare.strip()
- candidate = candidate.strip()
- if not bare or not candidate or bare == candidate:
- return False
-
- bare_role = _parse_role_name(bare)
- candidate_role = _parse_role_name(candidate)
- if bare_role is not None or candidate_role is not None:
- if bare_role is None or candidate_role is None:
- return False
- bare_role_name, bare_name, bare_suffix = bare_role
- candidate_role_name, candidate_name, candidate_suffix = candidate_role
- return (
- bare_role_name == candidate_role_name
- and not bare_name
- and not bare_suffix
- and bool(candidate_name)
- and not candidate_suffix
- )
- if not _BARE_TAG_RE.match(bare):
- return False
- if not candidate.startswith(bare) or _is_positional_selector(candidate):
- return False
- return _qualifier_narrows_to_identity(candidate[len(bare) :])
-
-
-def _stable_same_kind_bare_click_refiner(bare: str, candidate: str) -> bool:
- bare = bare.strip()
- candidate = candidate.strip()
- if not bare or not candidate or bare == candidate or _is_positional_selector(candidate):
- return False
- if _selector_refines(bare, candidate):
- return True
- if bare != "button":
- return False
-
- candidate_role = _parse_role_name(candidate)
- if candidate_role is not None:
- role_name, accessible_name, suffix = candidate_role
- return role_name == "button" and bool(accessible_name) and not suffix
-
- xpath_match = _EXACT_TEXT_XPATH_TAG_RE.match(candidate)
- return xpath_match is not None and xpath_match.group("tag").casefold() == "button"
-
-
-def _bare_drop_superseded_on_screen(
- dropped: Mapping[str, Any],
- scout_trajectory: list[ScoutedInteraction],
- *,
- claimed_refiner_indices: set[int],
-) -> tuple[bool, dict[str, Any] | None]:
- if dropped.get("reason_code") != "ambiguous_bare_selector" or dropped.get("tool_name") != "click":
- return False, None
- dropped_selector = str(dropped.get("selector") or "").strip()
- if not dropped_selector:
- return False, None
-
- dropped_index = dropped.get("trajectory_index")
- if not isinstance(dropped_index, int) or dropped_index < 0 or dropped_index >= len(scout_trajectory):
- return False, None
- source_url = str(scout_trajectory[dropped_index].get("source_url") or "").strip()
- if not source_url:
- return False, None
-
- for refiner_index in range(dropped_index + 1, len(scout_trajectory)):
- if refiner_index in claimed_refiner_indices:
- continue
- later = scout_trajectory[refiner_index]
- if later.get("tool_name") != "click":
- continue
- if str(later.get("source_url") or "").strip() != source_url:
- continue
- later_selector = str(later.get("selector") or "").strip()
- if not _stable_same_kind_bare_click_refiner(dropped_selector, later_selector):
- continue
- claimed_refiner_indices.add(refiner_index)
- return True, {
- "dropped_index": dropped_index,
- "dropped_selector": dropped_selector,
- "refiner_index": refiner_index,
- "refiner_selector": later_selector,
- "source_url": source_url,
- }
- return False, None
-
-
def _submitted_suffix_after_synthesized_code(submitted_code: str, synthesized_code: str) -> str:
# Preserve a pure suffix appended after the synthesized steps. Returns empty for
# prepended extraction scaffolding; that shape is handled by preserve_submitted_extraction.
@@ -7356,20 +7303,166 @@ def _strip_redundant_sandbox_imports_in_yaml(workflow_yaml: str) -> tuple[str, l
return yaml.safe_dump(parsed, sort_keys=False), stripped_modules
-def _apply_scouted_typed_default_promotions(workflow_yaml: str, ctx: AgentContext) -> tuple[str, list[str]]:
+def _code_block_has_load_bearing_mutation(code: str) -> bool:
+ mutations, _reads, _ambiguous = _browser_surface_for_code(code)
+ return any(mutation.method in _LOCATOR_MUTATION_METHODS for mutation in mutations)
+
+
+def _iter_workflow_blocks(blocks: object) -> Iterator[dict[str, Any]]:
+ if not isinstance(blocks, list):
+ return
+ for block in blocks:
+ if not isinstance(block, dict):
+ continue
+ yield block
+ yield from _iter_workflow_blocks(block.get("loop_blocks"))
+
+
+def _workflow_block_graph(parsed: Mapping[str, Any]) -> tuple[set[str], list[str]]:
+ definition = parsed.get("workflow_definition")
+ blocks = definition.get("blocks") if isinstance(definition, dict) else None
+ labels: set[str] = set()
+ references: list[str] = []
+ for block in _iter_workflow_blocks(blocks):
+ label = str(block.get("label") or "").strip()
+ if label:
+ labels.add(label)
+ for block in _iter_workflow_blocks(blocks):
+ next_label = str(block.get("next_block_label") or "").strip()
+ if next_label:
+ references.append(next_label)
+ branch_conditions = block.get("branch_conditions")
+ if isinstance(branch_conditions, list):
+ for branch in branch_conditions:
+ if not isinstance(branch, dict):
+ continue
+ branch_target = str(branch.get("next_block_label") or "").strip()
+ if branch_target:
+ references.append(branch_target)
+ return labels, references
+
+
+def _dangling_next_block_label_violation(parsed: Mapping[str, Any]) -> str | None:
+ labels, references = _workflow_block_graph(parsed)
+ dangling = sorted({reference for reference in references if reference not in labels})
+ if not dangling:
+ return None
+ return (
+ "Unable to persist workflow: next_block_label reference(s) point to no existing block: "
+ + ", ".join(f"`{label}`" for label in dangling)
+ + "."
+ )
+
+
+def _scaffolding_only_body_violation(ctx: AgentContext, parsed: Mapping[str, Any]) -> str | None:
+ if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
+ return None
+ scout_trajectory = ctx.scout_trajectory
+ if not isinstance(scout_trajectory, list) or not scout_trajectory:
+ return None
+ synthesized = synthesize_code_block(
+ scout_trajectory, reached_download_target=getattr(ctx, "reached_download_target", None)
+ )
+ if synthesized is None:
+ return None
+ required = [record for record in synthesized.diagnostics.emitted_interactions if not str(record.get("lane") or "")]
+ if not required:
+ return None
+ code_blocks = _workflow_code_blocks(parsed) if isinstance(parsed, dict) else []
+ if not code_blocks:
+ return None
+ if any(_code_block_has_load_bearing_mutation(str(block.get("code") or "")) for block in code_blocks):
+ return None
+ return (
+ "Unable to persist workflow: the persisted code block(s) commit no load-bearing browser interaction "
+ f"({SCOUTED_SPINE_UNDER_BUILD_REASON_CODE}), though the scout demonstrated {len(required)} rung(s). "
+ "Reuse the synthesized code block verbatim so the captured interactions are replayed."
+ )
+
+
+def _normalize_prompt_text(text: str) -> str:
+ return " ".join(text.split()).casefold()
+
+
+def _stub_with_raw_prompt_violation(ctx: AgentContext, parsed: Mapping[str, Any]) -> str | None:
+ packet = getattr(ctx, "turn_context_packet", None)
+ if packet is None:
+ return None
+ anchor = _normalize_prompt_text(str(packet.transcript_context.earliest_user_turn or ""))
+ if not anchor:
+ return None
+ if _scaffolding_only_body_violation(ctx, parsed) is None:
+ return None
+ for block in _iter_workflow_blocks(parsed.get("workflow_definition", {}).get("blocks")):
+ for field_name in ("prompt", "description", "title"):
+ if _normalize_prompt_text(str(block.get(field_name) or "")) == anchor:
+ return (
+ "Unable to persist workflow: a block reproduces the raw user request as its "
+ f"{field_name} while committing no load-bearing browser interaction "
+ f"({SCOUTED_SPINE_UNDER_BUILD_REASON_CODE})."
+ )
+ return None
+
+
+def _code_references_parameter(code: str, key: str) -> bool:
+ return re.search(rf"\b{re.escape(key)}\b", code) is not None
+
+
+def _orphan_minted_parameter_violation(parsed: Mapping[str, Any], minted_keys: list[str]) -> str | None:
+ if not minted_keys or not isinstance(parsed, dict):
+ return None
+ codes = [str(block.get("code") or "") for block in _workflow_code_blocks(parsed)]
+ orphans = sorted(
+ {key for key in minted_keys if key and not any(_code_references_parameter(code, key) for code in codes)}
+ )
+ if not orphans:
+ return None
+ return (
+ "Unable to persist workflow: minted workflow parameter(s) are not referenced by any generated code: "
+ + ", ".join(f"`{key}`" for key in orphans)
+ + "."
+ )
+
+
+def _final_yaml_structural_violations(
+ ctx: AgentContext,
+ workflow_yaml: str,
+ *,
+ minted_parameter_keys: list[str],
+ promoted_parameter_keys: list[str],
+ carried_by_imposition: bool,
+) -> list[str]:
+ if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
+ return []
+ parsed = parse_workflow_yaml(workflow_yaml)
+ if not isinstance(parsed, dict):
+ return []
+ candidates: list[str | None] = [
+ _dangling_next_block_label_violation(parsed),
+ _orphan_minted_parameter_violation(parsed, [*minted_parameter_keys, *promoted_parameter_keys]),
+ ]
+ # The spine-coverage seam runs only when imposition made no substitution; a successful imposition
+ # skips it, so the body-shape predicates re-validate exactly that bypassed window.
+ if carried_by_imposition:
+ candidates.insert(0, _scaffolding_only_body_violation(ctx, parsed))
+ candidates.insert(1, _stub_with_raw_prompt_violation(ctx, parsed))
+ return [violation for violation in candidates if violation is not None]
+
+
+def _apply_scouted_typed_default_promotions(workflow_yaml: str, ctx: AgentContext) -> tuple[str, list[str], list[str]]:
if not getattr(ctx, "impose_synthesized_code_block", False):
- return workflow_yaml, []
+ return workflow_yaml, [], []
if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
- return workflow_yaml, []
+ return workflow_yaml, [], []
scout_trajectory = ctx.scout_trajectory
if not isinstance(scout_trajectory, list) or not scout_trajectory:
- return workflow_yaml, []
+ return workflow_yaml, [], []
synthesized = synthesize_code_block(
scout_trajectory,
reached_download_target=getattr(ctx, "reached_download_target", None),
)
if synthesized is None:
- return workflow_yaml, []
+ return workflow_yaml, [], []
defaults_by_value: dict[str, list[str]] = {}
for parameter in synthesized.parameters:
@@ -7388,20 +7481,24 @@ def _apply_scouted_typed_default_promotions(workflow_yaml: str, ctx: AgentContex
)
value_to_key = {value: keys[0] for value, keys in defaults_by_value.items() if len(set(keys)) == 1}
if not value_to_key:
- return workflow_yaml, []
+ return workflow_yaml, [], []
parsed = parse_workflow_yaml(workflow_yaml)
if not isinstance(parsed, dict):
- return workflow_yaml, []
+ return workflow_yaml, [], []
workflow_definition = parsed.get("workflow_definition")
if not isinstance(workflow_definition, dict):
- return workflow_yaml, []
+ return workflow_yaml, [], []
parameters = workflow_definition.get("parameters")
if parameters is None:
parameters = []
workflow_definition["parameters"] = parameters
if not isinstance(parameters, list):
- return workflow_yaml, ["Unable to bind typed workflow inputs: workflow_definition.parameters must be a list."]
+ return (
+ workflow_yaml,
+ ["Unable to bind typed workflow inputs: workflow_definition.parameters must be a list."],
+ [],
+ )
reserved_keys = set(RESERVED_PARAMETER_KEYS) | _workflow_output_parameter_keys(parsed)
allocated_value_to_key: dict[str, str] = {}
@@ -7439,7 +7536,7 @@ def _apply_scouted_typed_default_promotions(workflow_yaml: str, ctx: AgentContex
any_rewrite = True
if not any_rewrite:
- return workflow_yaml, []
+ return workflow_yaml, [], []
existing_by_key = {
str(param.get("key")): param for param in parameters if isinstance(param, dict) and param.get("key")
@@ -7452,7 +7549,7 @@ def _apply_scouted_typed_default_promotions(workflow_yaml: str, ctx: AgentContex
existing = existing_by_key.get(key)
if isinstance(existing, dict):
_normalize_plain_parameter_row(existing, default_value)
- return yaml.safe_dump(parsed, sort_keys=False), []
+ return yaml.safe_dump(parsed, sort_keys=False), [], sorted(used_promoted_keys)
def _is_credential_parameter(parameter: Mapping[str, Any]) -> bool:
@@ -8044,7 +8141,9 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
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):
+ if not carrier_changed and not _draft_leaves_scouted_partition_open(
+ code_blocks, synthesized=synthesized, scout_trajectory=scout_trajectory
+ ):
return _SynthesizedCodeImpositionResult(workflow_yaml=workflow_yaml)
replaced_rung_labels: list[str] = []
if owns_spine:
@@ -8073,9 +8172,19 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
violations.append("Unable to impose synthesized code block: scout trajectory was truncated.")
claimed_refiner_indices: set[int] = set()
forgiven_superseded_bare_drops: list[dict[str, Any]] = []
+ forgiven_entry_opener_drops: list[dict[str, Any]] = []
repair_context: CodeAuthoringRepairContext | None = None
for dropped in diagnostics.dropped_interactions:
if _is_ignorable_entry_opener_drop(dropped, diagnostics):
+ entry_opener_record = {
+ "trajectory_index": dropped.get("trajectory_index"),
+ "tool_name": str(dropped.get("tool_name") or ""),
+ "reason_code": str(dropped.get("reason_code") or ""),
+ "selector": str(dropped.get("selector") or "").strip(),
+ "forgiveness": "entry_opener_superseded_by_locator_provenance",
+ }
+ forgiven_entry_opener_drops.append(entry_opener_record)
+ LOG.info("copilot_imposition_forgave_entry_opener_drop", **entry_opener_record)
continue
forgiven, refiner_record = _bare_drop_superseded_on_screen(
dropped, scout_trajectory, claimed_refiner_indices=claimed_refiner_indices
@@ -8361,6 +8470,8 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
substitutions["scrubbed_stale_selected_goal_value_paths"] = True
if forgiven_superseded_bare_drops:
substitutions["forgiven_superseded_bare_drops"] = forgiven_superseded_bare_drops
+ if forgiven_entry_opener_drops:
+ substitutions["forgiven_entry_opener_drops"] = forgiven_entry_opener_drops
selected_extraction_metadata_disposition: SelectedExtractionMetadataDisposition = "none"
if scrubbed_selected_metadata_label:
selected_extraction_metadata_disposition = "browser_spine_replaced_metadata_stale"
@@ -8385,6 +8496,7 @@ def _maybe_impose_synthesized_code_block(workflow_yaml: str, ctx: AgentContext)
substitutions=substitutions,
scrubbed_selected_metadata_label=scrubbed_selected_metadata_label,
selected_extraction_metadata_disposition=selected_extraction_metadata_disposition,
+ minted_parameter_keys=list(parameter_reconciliation.parameter_keys),
)
@@ -9357,11 +9469,11 @@ def _missing_scouted_rung_violation_text(artifact: str) -> str:
def _open_scouted_spine_obligation_artifact(ctx: AgentContext) -> str:
try:
- uncovered = _scouted_spine_open_obligation(ctx)
+ findings = _scouted_spine_open_obligation(ctx)
except Exception:
LOG.warning("copilot_scouted_spine_obligation_read_failed", exc_info=True)
return ""
- artifact = render_missing_rung_call_sources(uncovered)
+ artifact = render_missing_rung_call_sources(uncovered_rung_records(findings))
if not artifact:
return ""
return _missing_scouted_rung_violation_text(artifact)
@@ -9468,7 +9580,7 @@ async def _update_workflow(
)
if (
imposition.repair_context is not None
- and imposition.repair_context.reason_code == _SCOUTED_SPINE_UNDER_BUILD_REASON_CODE
+ and imposition.repair_context.reason_code in _SCOUTED_SPINE_REASON_CODES
):
_set_code_authoring_repair_context(ctx, imposition.repair_context)
_record_code_authoring_guardrail_reject(ctx)
@@ -9501,7 +9613,9 @@ async def _update_workflow(
stripped_sandbox_imports: list[str] = []
if _copilot_block_authoring_policy(ctx) == BlockAuthoringPolicy.CODE_ONLY_BROWSER:
workflow_yaml, stripped_sandbox_imports = _strip_redundant_sandbox_imports_in_yaml(workflow_yaml)
- workflow_yaml, typed_default_violations = _apply_scouted_typed_default_promotions(workflow_yaml, ctx)
+ workflow_yaml, typed_default_violations, promoted_parameter_keys = _apply_scouted_typed_default_promotions(
+ workflow_yaml, ctx
+ )
if typed_default_violations:
return reject(
error="\n".join(typed_default_violations),
@@ -9510,6 +9624,20 @@ async def _update_workflow(
)
if _copilot_block_authoring_policy(ctx) == BlockAuthoringPolicy.CODE_ONLY_BROWSER:
workflow_yaml = _adopt_exact_declared_parameter_keys_for_unresolved_names(workflow_yaml)
+ final_structural_violations = _final_yaml_structural_violations(
+ ctx,
+ workflow_yaml,
+ minted_parameter_keys=imposition.minted_parameter_keys,
+ promoted_parameter_keys=promoted_parameter_keys,
+ carried_by_imposition=imposition.substitutions is not None,
+ )
+ if final_structural_violations:
+ _record_code_authoring_guardrail_reject(ctx)
+ return reject(
+ error="\n".join(final_structural_violations),
+ user_facing_summary=_compiled_authoring_user_summary(),
+ data=_code_repair_progress_data(),
+ )
params["workflow_yaml"] = workflow_yaml
metadata_scrubbed_by_imposition = False
if (
diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.py
index e1944ae31..153dfcd3a 100644
--- a/tests/unit/test_copilot_code_block_persist_seam.py
+++ b/tests/unit/test_copilot_code_block_persist_seam.py
@@ -16,6 +16,7 @@ import yaml
from structlog.testing import capture_logs
from skyvern.forge.sdk.copilot import agent as agent_module
+from skyvern.forge.sdk.copilot import code_block_synthesis as code_block_synthesis_module
from skyvern.forge.sdk.copilot import enforcement as enforcement_module
from skyvern.forge.sdk.copilot import request_policy as request_policy_module
from skyvern.forge.sdk.copilot.blocker_signal import (
@@ -42,12 +43,14 @@ from skyvern.forge.sdk.copilot.code_block_preflight import (
)
from skyvern.forge.sdk.copilot.code_block_security import CodeBlockSecurityError
from skyvern.forge.sdk.copilot.code_block_synthesis import (
+ _MAX_STEPS,
ProducedStaticReturnEnvelope,
ScoutGap,
SynthesisDiagnostics,
SynthesizedCodeBlock,
_get_by_role_expr,
_get_by_role_expr_strict,
+ _stable_same_kind_bare_click_refiner,
credential_scout_gap,
produce_covered_static_return_envelope,
)
@@ -11248,6 +11251,37 @@ class TestWholeTrajectoryImposition:
assert code.index('page.locator("#zip")') < code.index('page.locator("#continue")')
assert code.index('page.locator("#continue")') < code.index('page.locator("#coverage-next")')
+ def test_entry_opener_drop_is_instrumented_not_silently_forgiven(self) -> None:
+ ctx = _quote_ctx()
+ ctx.scout_trajectory.insert(
+ 0,
+ {"tool_name": "click", "selector": "button", "source_url": _QUOTE_URL, "trajectory_index": 0},
+ )
+ 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("#electricDate").fill("2026-07-01")
+ await page.locator("#coverage-next").click()
+ """
+ )
+
+ 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
+ forgiven = result.substitutions["forgiven_entry_opener_drops"]
+ assert [record["reason_code"] for record in forgiven] == ["ambiguous_bare_selector"]
+ assert forgiven[0]["forgiveness"] == "entry_opener_superseded_by_locator_provenance"
+ assert any(entry.get("event") == "copilot_imposition_forgave_entry_opener_drop" for entry in logs)
+
def test_imposes_over_unscouted_selected_block_extra_click(self) -> None:
ctx = _quote_ctx()
submitted = _yaml(
@@ -12629,19 +12663,17 @@ class TestBareDropSupersession:
def test_stable_bare_click_refiner_accepts_only_same_kind_text_and_role_anchors(self) -> None:
assert (
- workflow_update_module._stable_same_kind_bare_click_refiner(
- "button", "xpath=//button[normalize-space()='Check Order Status']"
- )
+ _stable_same_kind_bare_click_refiner("button", "xpath=//button[normalize-space()='Check Order Status']")
is True
)
- assert workflow_update_module._stable_same_kind_bare_click_refiner("button", 'role=button[name="Next"]') is True
+ assert _stable_same_kind_bare_click_refiner("button", 'role=button[name="Next"]') is True
for candidate in (
"xpath=//a[normalize-space()='Check Order Status']",
'role=link[name="Next"]',
"xpath=(//button[normalize-space()='Check Order Status'])[2]",
"xpath=//button[contains(normalize-space(), 'Check')]",
):
- assert workflow_update_module._stable_same_kind_bare_click_refiner("button", candidate) is False
+ assert _stable_same_kind_bare_click_refiner("button", candidate) is False
def test_supersession_true_returns_pairing_record(self) -> None:
dropped = {
@@ -14018,6 +14050,225 @@ class TestScoutedSpineTurnEndCheckpoint:
standard.block_authoring_policy = BlockAuthoringPolicy.STANDARD
assert enforcement_module._scouted_spine_turn_end_nudge(standard) is None
+ def test_dropped_unforgiven_interaction_nudges_despite_full_rung_coverage(self) -> None:
+ ctx = _checkpoint_eligible_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},
+ ]
+ ctx.persisted_draft_browser_calls = [
+ ("click", 'page.locator("#stage-a")'),
+ ("click", 'page.locator("#stage-b")'),
+ ]
+
+ findings = enforcement_module._scouted_spine_open_obligation(ctx)
+ assert any(finding.kind == code_block_synthesis_module.UNFORGIVEN_DROP_FINDING for finding in findings)
+
+ nudge = enforcement_module._scouted_spine_turn_end_nudge(ctx)
+ assert nudge is not None
+ assert code_block_synthesis_module.SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE in nudge
+ assert ctx.last_code_authoring_repair_context is not None
+ assert (
+ ctx.last_code_authoring_repair_context.reason_code
+ == code_block_synthesis_module.SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE
+ )
+
+
+class TestPartitionAwareImpositionFastPath:
+ def _synthesize(self, trajectory: list[dict[str, object]]) -> SynthesizedCodeBlock:
+ result = workflow_update_module.synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ return result
+
+ def _covering_blocks(self, synthesized: SynthesizedCodeBlock) -> list[dict[str, object]]:
+ return [{"code": synthesized.interaction_code or synthesized.code}]
+
+ def test_clean_covered_draft_allows_early_return(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": "#stage-a", "source_url": "https://example.com/records"},
+ {"tool_name": "click", "selector": "#stage-b"},
+ ]
+ synthesized = self._synthesize(trajectory)
+ assert (
+ workflow_update_module._draft_leaves_scouted_partition_open(
+ self._covering_blocks(synthesized), synthesized=synthesized, scout_trajectory=trajectory
+ )
+ is False
+ )
+
+ def test_covered_but_dropped_unforgiven_blocks_early_return(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": "#stage-a", "source_url": "https://example.com/records"},
+ {"tool_name": "press_key", "key": ""},
+ {"tool_name": "click", "selector": "#stage-b"},
+ ]
+ synthesized = self._synthesize(trajectory)
+ assert (
+ workflow_update_module._draft_leaves_scouted_partition_open(
+ self._covering_blocks(synthesized), synthesized=synthesized, scout_trajectory=trajectory
+ )
+ is True
+ )
+
+ def test_covered_but_truncated_blocks_early_return(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": f"#stage-{index}", "source_url": "https://example.com/records"}
+ for index in range(_MAX_STEPS + 2)
+ ]
+ synthesized = self._synthesize(trajectory)
+ assert synthesized.diagnostics.truncated is True
+ assert (
+ workflow_update_module._draft_leaves_scouted_partition_open(
+ self._covering_blocks(synthesized), synthesized=synthesized, scout_trajectory=trajectory
+ )
+ is True
+ )
+
+
+class TestFinalYamlStructuralGate:
+ def test_dangling_next_block_label_is_flagged(self) -> None:
+ parsed = {
+ "workflow_definition": {
+ "blocks": [
+ {"block_type": "code", "label": "a", "next_block_label": "missing"},
+ {"block_type": "code", "label": "b"},
+ ]
+ }
+ }
+ violation = workflow_update_module._dangling_next_block_label_violation(parsed)
+ assert violation is not None
+ assert "missing" in violation
+
+ def test_final_yaml_structural_gate_is_scoped_to_code_authoring(self) -> None:
+ ctx = _records_spine_ctx()
+ ctx.block_authoring_policy = BlockAuthoringPolicy.STANDARD
+ dangling_yaml = _yaml(
+ """
+ title: Records
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: a
+ next_block_label: missing
+ - block_type: code
+ label: b
+ """
+ )
+ assert (
+ workflow_update_module._final_yaml_structural_violations(
+ ctx,
+ dangling_yaml,
+ minted_parameter_keys=[],
+ promoted_parameter_keys=[],
+ carried_by_imposition=False,
+ )
+ == []
+ )
+ assert (
+ workflow_update_module._dangling_next_block_label_violation(parse_workflow_yaml(dangling_yaml)) is not None
+ )
+
+ def test_resolved_next_block_label_passes(self) -> None:
+ parsed = {
+ "workflow_definition": {
+ "blocks": [
+ {"block_type": "code", "label": "a", "next_block_label": "b"},
+ {"block_type": "code", "label": "b"},
+ ]
+ }
+ }
+ assert workflow_update_module._dangling_next_block_label_violation(parsed) is None
+
+ def test_orphan_minted_parameter_is_flagged_and_referenced_is_not(self) -> None:
+ parsed = {
+ "workflow_definition": {
+ "blocks": [
+ {"block_type": "code", "label": "a", "code": "await page.locator('#x').fill(str(used_key))"},
+ ]
+ }
+ }
+ violation = workflow_update_module._orphan_minted_parameter_violation(parsed, ["used_key", "orphan_key"])
+ assert violation is not None
+ assert "orphan_key" in violation
+ assert "used_key" not in violation
+
+ def test_preexisting_authored_param_outside_manifest_is_untouched(self) -> None:
+ parsed = {
+ "workflow_definition": {
+ "blocks": [
+ {"block_type": "code", "label": "a", "code": "await page.locator('#x').click()"},
+ ],
+ "parameters": [{"key": "unused_authored_input", "parameter_type": "workflow"}],
+ }
+ }
+ assert workflow_update_module._orphan_minted_parameter_violation(parsed, []) is None
+
+ def test_scaffolding_only_body_flagged_only_when_carried_by_imposition(self) -> None:
+ ctx = _records_spine_ctx()
+ scaffolding_yaml = _records_block_yaml("await page.goto('https://example.com/records')")
+ carried = workflow_update_module._final_yaml_structural_violations(
+ ctx,
+ scaffolding_yaml,
+ minted_parameter_keys=[],
+ promoted_parameter_keys=[],
+ carried_by_imposition=True,
+ )
+ assert any("no load-bearing" in violation for violation in carried)
+ not_carried = workflow_update_module._final_yaml_structural_violations(
+ ctx,
+ scaffolding_yaml,
+ minted_parameter_keys=[],
+ promoted_parameter_keys=[],
+ carried_by_imposition=False,
+ )
+ assert not any("no load-bearing" in violation for violation in not_carried)
+
+ def test_load_bearing_body_passes_the_gate(self) -> None:
+ ctx = _records_spine_ctx()
+ real_yaml = _records_block_yaml("await page.locator('#stage-a').click()")
+ assert (
+ workflow_update_module._final_yaml_structural_violations(
+ ctx,
+ real_yaml,
+ minted_parameter_keys=[],
+ promoted_parameter_keys=[],
+ carried_by_imposition=True,
+ )
+ == []
+ )
+
+ def test_stub_reproducing_raw_prompt_is_flagged(self) -> None:
+ ctx = _records_spine_ctx()
+ ctx.turn_context_packet = SimpleNamespace(
+ transcript_context=SimpleNamespace(earliest_user_turn="Log me in and stop")
+ )
+ stub_yaml = _yaml(
+ """
+ title: Records
+ workflow_definition:
+ blocks:
+ - block_type: code
+ label: search_registry
+ description: Log me in and stop
+ code: |
+ await page.goto("https://example.com/records")
+ """
+ )
+ violations = workflow_update_module._final_yaml_structural_violations(
+ ctx,
+ stub_yaml,
+ minted_parameter_keys=[],
+ promoted_parameter_keys=[],
+ carried_by_imposition=True,
+ )
+ assert any("raw user request" in violation for violation in violations)
+
def _full_coverage_calls() -> list[tuple[str, str]]:
return [
@@ -14719,6 +14970,62 @@ class TestAdmittedImpositionOwnsSpineCoverage:
assert any("`coverage_step`" in violation for violation in result.violations)
assert result.workflow_yaml == submitted
+ def test_graph_referenced_stale_rung_steers_instead_of_being_dropped(self) -> None:
+ # A surviving block's next_block_label points at the stale rung; dropping it would manufacture the
+ # dangling reference the AC4 gate rejects, so it must refuse-with-provenance, not silently delete.
+ stale = {
+ "block_type": "code",
+ "label": "extract_priority_resale_document",
+ "code": "await page.locator('#doc').click()",
+ }
+ parsed: dict = {
+ "workflow_definition": {
+ "blocks": [
+ {
+ "block_type": "code",
+ "label": "order_lookup",
+ "next_block_label": "extract_priority_resale_document",
+ "code": "await page.goto('https://example.com')",
+ },
+ stale,
+ ]
+ }
+ }
+
+ result = workflow_update_module._drop_stale_spine_rung_blocks(
+ parsed, [stale], carrier_label="carrier", provenance=[]
+ )
+
+ assert result.violation is not None
+ assert "`extract_priority_resale_document`" in result.violation
+ assert result.replaced_labels == []
+ remaining = [block.get("label") for block in parsed["workflow_definition"]["blocks"]]
+ assert "extract_priority_resale_document" in remaining
+
+ def test_unreferenced_stale_rung_is_still_dropped(self) -> None:
+ stale = {
+ "block_type": "code",
+ "label": "orphan_rung",
+ "code": "await page.locator('#doc').click()",
+ }
+ parsed: dict = {
+ "workflow_definition": {
+ "blocks": [
+ {"block_type": "code", "label": "carrier", "code": "await page.goto('https://example.com')"},
+ stale,
+ ]
+ }
+ }
+
+ result = workflow_update_module._drop_stale_spine_rung_blocks(
+ parsed, [stale], carrier_label="carrier", provenance=[]
+ )
+
+ assert result.violation is None
+ assert result.replaced_labels == ["orphan_rung"]
+ 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:
ctx = _quote_ctx()
ctx.update_workflow_called = True
diff --git a/tests/unit/test_copilot_code_block_synthesis.py b/tests/unit/test_copilot_code_block_synthesis.py
index 020c92f28..8a3c2d156 100644
--- a/tests/unit/test_copilot_code_block_synthesis.py
+++ b/tests/unit/test_copilot_code_block_synthesis.py
@@ -26,14 +26,22 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (
_READONLY_DEFERRED_VAR,
_SYNTHESIZED_BLOCK_LABEL,
CREDENTIAL_FILL_TOOL_NAME,
+ SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE,
+ SCOUTED_SPINE_TRUNCATED_REASON_CODE,
+ TRUNCATED_FINDING,
+ UNFORGIVEN_DROP_FINDING,
+ UNRECORDED_INDEX_FINDING,
ProducedStaticReturnEnvelope,
+ SynthesisDiagnostics,
_get_by_role_expr,
_get_by_role_expr_strict,
build_synthesized_artifact_metadata,
code_contains_credential_fill,
is_optional_dismissal_only_trajectory,
+ obligation_finding_reason_code,
produce_covered_static_return_envelope,
render_synthesized_offer_text,
+ spine_partition_findings,
synthesize_code_block,
synthesize_code_block_with_extraction,
synthesize_extraction_suffix,
@@ -973,6 +981,123 @@ class TestActionSynthesis:
assert forbidden_snippet not in result.code
ast.parse("async def _block(page):\n" + result.code)
+ def test_terminal_anonymous_structural_click_after_required_is_emitted_as_required(self) -> None:
+ terminal_selector = (
+ 'xpath=/*[name()="html"][1]/*[name()="body"][1]/*[name()="div"][1]/*[name()="div"][2]/*[name()="button"][2]'
+ )
+ trajectory = [
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ _interaction(
+ "click",
+ selector="button.primary",
+ source_url="https://example.com/find",
+ role="button",
+ accessible_name="Search",
+ ),
+ _interaction(
+ "click",
+ selector=terminal_selector,
+ source_url="https://example.com/results",
+ role="button",
+ ),
+ ]
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ assert "button:has-text('Accept')" not in result.code
+ assert "_scout_optional_dismissal" not in result.code
+ terminal_records = [
+ record for record in result.diagnostics.emitted_interactions if record.get("trajectory_index") == 2
+ ]
+ assert len(terminal_records) == 1
+ assert not terminal_records[0].get("lane")
+ assert terminal_records[0]["method"] == "click"
+ assert terminal_records[0]["selector"] == terminal_selector
+
+ def test_terminal_dismissal_click_before_empty_key_press_is_still_reclassified_required(self) -> None:
+ # A trailing empty-key press_key is dropped (missing_key) and emits nothing, so it must not steal the
+ # terminal index from the anonymous-structural dismissal click before it and defeat the reclassify.
+ terminal_selector = (
+ 'xpath=/*[name()="html"][1]/*[name()="body"][1]/*[name()="div"][1]/*[name()="div"][2]/*[name()="button"][2]'
+ )
+ trajectory = [
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ _interaction(
+ "click",
+ selector=terminal_selector,
+ source_url="https://example.com/results",
+ role="button",
+ ),
+ _interaction("press_key", key="", source_url="https://example.com/results"),
+ ]
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ assert "_scout_optional_dismissal" not in result.code
+ terminal_records = [
+ record for record in result.diagnostics.emitted_interactions if record.get("trajectory_index") == 1
+ ]
+ assert len(terminal_records) == 1
+ assert not terminal_records[0].get("lane")
+ assert terminal_records[0]["method"] == "click"
+ assert terminal_records[0]["selector"] == terminal_selector
+
+ def test_terminal_structural_click_without_prior_required_stays_in_lane(self) -> None:
+ trajectory = [
+ _interaction(
+ "click",
+ selector=".btns button:nth-of-type(2)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ ]
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ assert '_scout_optional_dismissal = page.locator(".btns button:nth-of-type(2)")' in result.code
+ assert 'await page.locator(".btns button:nth-of-type(2)").click()' not in result.code
+
+ def test_terminal_text_matched_dismissal_after_required_stays_in_lane(self) -> None:
+ trajectory = [
+ _interaction(
+ "type_text",
+ selector="#locInput",
+ source_url="https://example.com/find",
+ role="textbox",
+ accessible_name="City, county, or ZIP code",
+ typed_value="Example City",
+ ),
+ _interaction(
+ "click",
+ selector="button:not(.decline)",
+ source_url="https://example.com/find",
+ role="button",
+ ),
+ ]
+
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+
+ assert result is not None
+ assert "_scout_optional_dismissal = page.locator(\"button:has-text('Accept')\")" in result.code
+ assert 'await page.locator("button:not(.decline)").click()' not in result.code
+
def test_optional_dismissal_with_durable_target_is_offerable(self) -> None:
trajectory = [
_interaction(
@@ -2964,6 +3089,132 @@ class TestEmittedInteractionPartition:
]
+def _covering_draft_calls(diagnostics: SynthesisDiagnostics) -> list[tuple[str, str]]:
+ return [
+ (str(record.get("method") or ""), str(record.get("locator") or ""))
+ for record in diagnostics.emitted_interactions
+ if not str(record.get("lane") or "")
+ ]
+
+
+class TestSpinePartitionFindings:
+ def _spine_trajectory(self) -> list[dict[str, Any]]:
+ return [
+ {"tool_name": "click", "selector": "#stage-a", "source_url": "https://example.com/records"},
+ {"tool_name": "click", "selector": "#stage-b"},
+ ]
+
+ def test_retained_manifest_covers_every_partition_index(self) -> None:
+ result = synthesize_code_block(self._spine_trajectory(), strict_selectors=True)
+ assert result is not None
+ diagnostics = result.diagnostics
+ assert diagnostics.retained_trajectory_indices == list(range(2))
+ recorded = (
+ {record["trajectory_index"] for record in diagnostics.emitted_interactions}
+ | {record["trajectory_index"] for record in diagnostics.dropped_interactions}
+ | {record["trajectory_index"] for record in diagnostics.forgiven_interactions}
+ )
+ assert set(diagnostics.retained_trajectory_indices) == recorded
+
+ def test_covered_draft_has_no_findings(self) -> None:
+ result = synthesize_code_block(self._spine_trajectory(), strict_selectors=True)
+ assert result is not None
+ draft_calls = _covering_draft_calls(result.diagnostics)
+ assert spine_partition_findings(result.diagnostics, draft_calls, self._spine_trajectory()) == []
+
+ def test_unforgiven_drop_is_a_typed_finding(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": "#stage-a", "source_url": "https://example.com/records"},
+ {"tool_name": "press_key", "key": ""},
+ ]
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ draft_calls = _covering_draft_calls(result.diagnostics)
+ findings = spine_partition_findings(result.diagnostics, draft_calls, trajectory)
+ kinds = {finding.kind for finding in findings}
+ assert UNFORGIVEN_DROP_FINDING in kinds
+ drop_finding = next(finding for finding in findings if finding.kind == UNFORGIVEN_DROP_FINDING)
+ assert obligation_finding_reason_code(drop_finding) == SCOUTED_SPINE_DROPPED_UNFORGIVEN_REASON_CODE
+
+ def test_entry_opener_drop_is_forgiven_not_flagged(self) -> None:
+ trajectory = [
+ {
+ "tool_name": "click",
+ "selector": "button",
+ "role": "button",
+ "accessible_name": "Open menu",
+ "source_url": "https://example.com/records",
+ },
+ {"tool_name": "click", "selector": "#stage-b"},
+ ]
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ draft_calls = _covering_draft_calls(result.diagnostics)
+ findings = spine_partition_findings(result.diagnostics, draft_calls, trajectory)
+ assert all(finding.kind != UNFORGIVEN_DROP_FINDING for finding in findings)
+
+ def test_truncation_leaves_post_break_indices_in_manifest_only(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": f"#stage-{index}", "source_url": "https://example.com/records"}
+ for index in range(_MAX_STEPS + 3)
+ ]
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ diagnostics = result.diagnostics
+ assert diagnostics.truncated is True
+ assert diagnostics.retained_trajectory_indices == list(range(len(trajectory)))
+ recorded = {record["trajectory_index"] for record in diagnostics.emitted_interactions}
+ post_break = set(diagnostics.retained_trajectory_indices) - recorded
+ assert post_break
+ findings = spine_partition_findings(diagnostics, _covering_draft_calls(diagnostics), trajectory)
+ kinds = {finding.kind for finding in findings}
+ assert TRUNCATED_FINDING in kinds
+ assert UNRECORDED_INDEX_FINDING in kinds
+ truncated_finding = next(finding for finding in findings if finding.kind == TRUNCATED_FINDING)
+ assert obligation_finding_reason_code(truncated_finding) == SCOUTED_SPINE_TRUNCATED_REASON_CODE
+
+
+_STRUCTURAL_BUTTON_XPATH = 'xpath=/*[name()="body"][1]/*[name()="button"][3]'
+
+
+class TestTerminalDismissalReclassification:
+ def test_anonymous_structural_terminal_click_after_required_work_is_required(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": "#stage-a", "source_url": "https://example.com/records"},
+ {"tool_name": "click", "selector": _STRUCTURAL_BUTTON_XPATH},
+ ]
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ assert "_scout_optional_dismissal" not in result.code
+ assert "button:has-text('Accept')" not in result.code
+ by_index = {record["trajectory_index"]: record for record in result.diagnostics.emitted_interactions}
+ assert not str(by_index[1].get("lane") or "")
+
+ def test_named_terminal_dismissal_keeps_the_optional_lane(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": "#stage-a", "source_url": "https://example.com/records"},
+ {
+ "tool_name": "click",
+ "selector": _STRUCTURAL_BUTTON_XPATH,
+ "role": "button",
+ "accessible_name": "Accept all cookies",
+ },
+ ]
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ assert "_scout_optional_dismissal" in result.code
+ by_index = {record["trajectory_index"]: record for record in result.diagnostics.emitted_interactions}
+ assert by_index[1].get("lane") == "optional_dismissal"
+
+ def test_dismissal_only_single_step_keeps_the_optional_lane(self) -> None:
+ trajectory = [
+ {"tool_name": "click", "selector": _STRUCTURAL_BUTTON_XPATH, "source_url": "https://example.com/records"},
+ ]
+ result = synthesize_code_block(trajectory, strict_selectors=True)
+ assert result is not None
+ assert "_scout_optional_dismissal" in result.code
+
+
class TestUncoveredRequiredEmittedInteractions:
def _emitted(self) -> list[dict[str, Any]]:
return [