Skip to content

Automate browser based workflows with AI

SKY-12918 Copilot: remember the site from the first message when a follow-up doesn't repeat it (#7701)

Andrew Neilson8ed9fccffa90
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 6ac1a09d0..05610e39f 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -50,7 +50,7 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
terminal_evidence_has_recorded_state,
)
from skyvern.forge.sdk.copilot.blocker_signal import to_trace_data as blocker_signal_to_trace_data
-from skyvern.forge.sdk.copilot.build_phase import BuildPhase, initial_build_phase
+from skyvern.forge.sdk.copilot.build_phase import BuildPhase, anchor_recovers_entrypoint, initial_build_phase
from skyvern.forge.sdk.copilot.build_test_outcome import (
_VALUE_EXCERPT_MAX,
RecordedBuildTestOutcome,
@@ -600,6 +600,21 @@ def _turn_context_trace_fields(packet: TurnContextPacket | None) -> dict[str, st
return {key: str(value) for key, value in _turn_context_log_fields(packet).items()}
+def _transcript_anchor_for_turn(packet: TurnContextPacket | None, chat_history_len: int) -> str:
+ """The earliest-user-turn anchor, or "" when it cannot be trusted this turn.
+
+ Blanked when the retained window is at capacity: a full window may have
+ dropped older turns, so earliest_user_turn would be a middle-history turn
+ rather than the original request.
+ """
+ # Deferred: routes.workflow_copilot imports this module (circular at import time).
+ from skyvern.forge.sdk.routes.workflow_copilot import CHAT_HISTORY_CONTEXT_MESSAGES
+
+ if not isinstance(packet, TurnContextPacket) or chat_history_len >= CHAT_HISTORY_CONTEXT_MESSAGES:
+ return ""
+ return packet.transcript_context.earliest_user_turn
+
+
def _store_turn_context_packet_on_context(
ctx: CopilotContext,
*,
@@ -5510,12 +5525,23 @@ async def _run_copilot_turn_impl(
ctx.prior_page_inspection_calls_made = prior_structured_context.page_inspection_calls_made
ctx.prior_observed_acted_pages = [page.model_dump() for page in prior_structured_context.observed_acted_pages]
ctx.prior_fill_carry = [carry.model_dump() for carry in prior_structured_context.fill_carry]
+ transcript_anchor = _transcript_anchor_for_turn(ctx.turn_context_packet, len(chat_history))
ctx.build_phase = initial_build_phase(
ctx.turn_intent,
chat_request.message or "",
agent_user_message or "",
chat_request.workflow_yaml or "",
+ transcript_anchor,
+ )
+ anchor_entrypoint = anchor_recovers_entrypoint(
+ ctx.turn_intent,
+ chat_request.message or "",
+ agent_user_message or "",
+ chat_request.workflow_yaml or "",
+ transcript_anchor,
)
+ if anchor_entrypoint is not None and ctx.resolved_discovery_entrypoint_url is None:
+ ctx.resolved_discovery_entrypoint_url = anchor_entrypoint
LOG.info(
"copilot.build_phase_initial",
build_phase=ctx.build_phase.value,
diff --git a/skyvern/forge/sdk/copilot/blocker_signal.py b/skyvern/forge/sdk/copilot/blocker_signal.py
index 9de32ac46..67212d0eb 100644
--- a/skyvern/forge/sdk/copilot/blocker_signal.py
+++ b/skyvern/forge/sdk/copilot/blocker_signal.py
@@ -449,6 +449,7 @@ RECORDED_OUTCOME_GROUNDING_REASON_CODE = "recorded_outcome_grounding_required"
DEFINITION_CONTRACT_UNSATISFIED_REASON_CODE = "definition_contract_unsatisfied"
SCHEMA_INCOMPATIBILITY_REASON_CODE = "schema_incompatibility"
OUTPUT_CONTRACT_REJECT_BUDGET_EXHAUSTED_REASON_CODE = "output_contract_reject_budget_exhausted"
+DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE = "loop_detected_discovery_exhausted_no_entry_url"
_OUTPUT_CONTRACT_TERMINAL_REASON_CODES = frozenset(
{
OUTPUT_SOURCE_UNOBSERVABLE_REASON_CODE,
@@ -475,6 +476,7 @@ GENUINELY_TERMINAL_BLOCKER_REASON_CODES: frozenset[str] = frozenset(
DEFINITION_CONTRACT_UNSATISFIED_REASON_CODE,
"repair_ceiling_reached",
METADATA_REJECT_SAME_KEY_TERMINAL_REASON_CODE,
+ DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
}
)
diff --git a/skyvern/forge/sdk/copilot/build_phase.py b/skyvern/forge/sdk/copilot/build_phase.py
index b519c49ed..51b8745ab 100644
--- a/skyvern/forge/sdk/copilot/build_phase.py
+++ b/skyvern/forge/sdk/copilot/build_phase.py
@@ -24,6 +24,7 @@ import re
import time
from enum import StrEnum
from typing import TYPE_CHECKING, Any
+from urllib.parse import urlparse
import structlog
import yaml
@@ -81,6 +82,44 @@ _COMPOSITION_CONTEXT_TOOLS: frozenset[str] = frozenset({"inspect_page_for_compos
_URL_IN_TEXT_RE = re.compile(r"https?://[^\s<>\"\)]+", re.IGNORECASE)
+# Two discovery failures with no resolvable entrypoint end the turn with one ask.
+DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD = 2
+
+_ANCHOR_URL_TRAILING_PUNCTUATION = ",.;:!?)]}'\"`"
+_ANCHOR_URL_MARKDOWN_WRAPPERS = frozenset("*_~")
+_ANCHOR_TRUNCATION_SENTINEL = "…"
+
+
+def extract_anchor_entry_url(text: str | None) -> str | None:
+ """Extract the first well-formed http(s) URL from a transcript anchor.
+
+ Consulted last and read-side only, so a stale URL from an unrelated turn
+ cannot leak. Matches abutting the `_safe_slot` middle-truncation marker are
+ rejected because the URL may be mangled across the splice.
+ """
+ if not text:
+ return None
+ for match in _URL_IN_TEXT_RE.finditer(text):
+ start, end = match.span()
+ if text[end : end + 1] == "<" or text[start - 1 : start] == ">":
+ continue
+ if _ANCHOR_TRUNCATION_SENTINEL in text[max(0, start - 2) : end + 2]:
+ continue
+ candidate = match.group(0).rstrip(_ANCHOR_URL_TRAILING_PUNCTUATION)
+ preceding = text[start - 1 : start]
+ if preceding in _ANCHOR_URL_MARKDOWN_WRAPPERS:
+ candidate = candidate.rstrip(preceding)
+ try:
+ parsed = urlparse(candidate)
+ port = parsed.port
+ except ValueError:
+ continue
+ if parsed.scheme not in {"http", "https"} or not parsed.hostname:
+ continue
+ if "." in parsed.hostname or port is not None or parsed.hostname == "localhost":
+ return candidate
+ return None
+
def _parse_workflow_blocks(workflow_yaml: str | None) -> list[dict[str, Any]]:
if not workflow_yaml:
@@ -115,6 +154,7 @@ def initial_build_phase(
user_message: str,
agent_user_message: str,
workflow_yaml: str | None,
+ transcript_earliest_user_turn: str = "",
) -> BuildPhase:
"""Decide the initial build phase for this turn.
@@ -133,11 +173,14 @@ def initial_build_phase(
alternative is to let mutation through on an ambiguous turn, which is
worse than asking discovery to resolve or falling through to ASK_QUESTION.
- Signals considered (this-turn only — never prior visited URLs or chat
- history, which can leak stale URLs across unrelated turns):
+ Signals considered, in order:
- The raw latest user message.
- The rewritten agent input (request-policy may carry the prior request).
- The current workflow YAML's first goto_url/navigation block.
+ - The earliest-user-turn transcript anchor, consulted last and read-side
+ only. The anchor IS the original request, so it recovers the URL an
+ abnormally-ended turn dropped without reviving stale URLs from prior
+ visited pages or middle history turns.
"""
mode_value = getattr(getattr(turn_intent, "mode", None), "value", None)
if mode_value in _PHASE_NON_BUILD_MODE_VALUES:
@@ -146,12 +189,38 @@ def initial_build_phase(
_URL_IN_TEXT_RE.search(user_message or "")
or _URL_IN_TEXT_RE.search(agent_user_message or "")
or _yaml_has_target_url(workflow_yaml)
+ or extract_anchor_entry_url(transcript_earliest_user_turn)
)
if not has_url_signal:
return BuildPhase.INITIAL
return BuildPhase.COMPOSING
+def anchor_recovers_entrypoint(
+ turn_intent: TurnIntent | None,
+ user_message: str,
+ agent_user_message: str,
+ workflow_yaml: str | None,
+ transcript_earliest_user_turn: str = "",
+) -> str | None:
+ """The transcript-anchor URL to record as the resolved entrypoint, or None.
+
+ Returns a URL only when the anchor is the sole entrypoint signal this turn:
+ an in-turn URL (message, rewritten input, or YAML) is authoritative and
+ must not be overridden by the older anchor, so any of those returns None.
+ """
+ mode_value = getattr(getattr(turn_intent, "mode", None), "value", None)
+ if mode_value in _PHASE_NON_BUILD_MODE_VALUES:
+ return None
+ if (
+ _URL_IN_TEXT_RE.search(user_message or "")
+ or _URL_IN_TEXT_RE.search(agent_user_message or "")
+ or _yaml_has_target_url(workflow_yaml)
+ ):
+ return None
+ return extract_anchor_entry_url(transcript_earliest_user_turn)
+
+
def _log_transition(ctx: CopilotContext, *, prev: BuildPhase, new: BuildPhase, reason: str) -> None:
LOG.info(
"copilot.build_phase_transition",
diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.py
index d595dffbd..6e611def9 100644
--- a/skyvern/forge/sdk/copilot/context.py
+++ b/skyvern/forge/sdk/copilot/context.py
@@ -937,6 +937,7 @@ class CopilotContext(AgentContext):
resolved_discovery_entrypoint_inspection_baseline: int = 0
discovery_entrypoint_url_question_nudge_count: int = 0
pre_discovery_url_question_nudge_count: int = 0
+ discovery_failure_streak_this_turn: int = 0
# Set in `_run_attempt` after SkyvernOverlayMCPServer is constructed.
# The discovery tool reaches the connected FastMCP client through this.
discovery_mcp_server: Any | None = None
diff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.py
index 3e940c088..cf4080088 100644
--- a/skyvern/forge/sdk/copilot/enforcement.py
+++ b/skyvern/forge/sdk/copilot/enforcement.py
@@ -28,7 +28,7 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
loop_blocker_evidence_from_ctx,
stash_blocker_signal,
)
-from skyvern.forge.sdk.copilot.build_phase import DISCOVERY_PERMITTED_PHASES
+from skyvern.forge.sdk.copilot.build_phase import DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD, DISCOVERY_PERMITTED_PHASES
from skyvern.forge.sdk.copilot.build_test_outcome import (
PostRunPagePathFailure,
RecordedBuildTestOutcome,
@@ -1262,6 +1262,11 @@ def _pre_discovery_url_question_nudge(
return None
if _get_int(ctx, "discovery_calls_this_turn") != 0:
return None
+ if (
+ getattr(ctx, "turn_halt", None) is not None
+ or _get_int(ctx, "discovery_failure_streak_this_turn") >= DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD
+ ):
+ return None
request_policy = getattr(ctx, "request_policy", None)
clarification_reason = getattr(request_policy, "clarification_reason", "none")
if clarification_reason not in (None, "none"):
diff --git a/skyvern/forge/sdk/copilot/tools/discovery.py b/skyvern/forge/sdk/copilot/tools/discovery.py
index 8e85ba520..45298979d 100644
--- a/skyvern/forge/sdk/copilot/tools/discovery.py
+++ b/skyvern/forge/sdk/copilot/tools/discovery.py
@@ -18,7 +18,12 @@ except ImportError: # pragma: no cover — bs4 is a transitive dep but discover
from skyvern.forge import app
from skyvern.forge.agent_functions import CopilotCandidateNetworkHop, CopilotEntrypointCandidate
+from skyvern.forge.sdk.copilot.blocker_signal import (
+ DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
+ CopilotToolBlockerSignal,
+)
from skyvern.forge.sdk.copilot.build_phase import (
+ DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD,
BuildPhase,
advance_to_composing,
advance_to_discovering,
@@ -26,6 +31,8 @@ from skyvern.forge.sdk.copilot.build_phase import (
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.copilot.loop_detection import record_tool_step_result_for_ctx
from skyvern.forge.sdk.copilot.tracing_setup import copilot_span
+from skyvern.forge.sdk.copilot.turn_halt import stash_turn_halt_from_blocker_signal
+from skyvern.forge.sdk.copilot.turn_ownership import TurnClaimant, claim_and_stash_blocker_signal
from ._shared import (
_DISCOVERY_ANTI_BOT_PATTERNS,
@@ -769,6 +776,51 @@ async def _discovery_walk(
)
+# Only pre-navigation resolution failures advance the streak; scorer-miss reasons
+# consume the per-turn budget so a retry short-circuits and never reaches the threshold.
+_DISCOVERY_ENTRY_RESOLUTION_FAILURE_REASONS = frozenset({"could_not_resolve_site_name"})
+
+
+def _build_discovery_exhausted_escape_signal() -> CopilotToolBlockerSignal:
+ return CopilotToolBlockerSignal(
+ blocker_kind="loop_detected",
+ agent_steering_text=(
+ "STOP: entrypoint resolution failed repeatedly this turn and no web address is available to open. "
+ "This is not repairable by retrying. Ask the user for the target URL."
+ ),
+ user_facing_reason=(
+ "I looked through your messages and the workflow draft but couldn't find a web address to open. "
+ "Tell me the URL of the site you want me to work on and I'll get started."
+ ),
+ recovery_hint="ask_user_clarifying",
+ cleared_by_tools=frozenset(),
+ renders_final_reply=True,
+ internal_reason_code=DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
+ blocked_tool="discover_workflow_entrypoint",
+ )
+
+
+def _emit_discovery_exhausted_escape(copilot_ctx: CopilotContext) -> None:
+ if copilot_ctx.turn_halt is not None:
+ return
+ signal = _build_discovery_exhausted_escape_signal()
+ claim_and_stash_blocker_signal(copilot_ctx, TurnClaimant.GENUINELY_TERMINAL, signal)
+ stash_turn_halt_from_blocker_signal(copilot_ctx, signal, source="discovery")
+
+
+def _maybe_advance_discovery_failure_streak(copilot_ctx: CopilotContext, result: Mapping[str, Any]) -> None:
+ data_payload = result.get("data")
+ data: Mapping[str, Any] = data_payload if isinstance(data_payload, Mapping) else {}
+ if (
+ data.get("failure_reason") not in _DISCOVERY_ENTRY_RESOLUTION_FAILURE_REASONS
+ or copilot_ctx.resolved_discovery_entrypoint_url
+ ):
+ return
+ copilot_ctx.discovery_failure_streak_this_turn += 1
+ if copilot_ctx.discovery_failure_streak_this_turn >= DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD:
+ _emit_discovery_exhausted_escape(copilot_ctx)
+
+
async def _discover_workflow_entrypoint_impl(
copilot_ctx: Any,
site_or_url: str,
@@ -784,6 +836,7 @@ async def _discover_workflow_entrypoint_impl(
def finish(result: dict[str, Any], *, site_or_url_kind: str | None = None) -> dict[str, Any]:
_record_discovery_resolution_on_ctx(copilot_ctx, result)
record_tool_step_result_for_ctx(copilot_ctx, "discover_workflow_entrypoint", arguments, result)
+ _maybe_advance_discovery_failure_streak(copilot_ctx, result)
data_payload = result.get("data")
data = data_payload if isinstance(data_payload, Mapping) else {}
if isinstance(data_payload, dict) and input_kind == "bare_word":
@@ -826,6 +879,19 @@ async def _discover_workflow_entrypoint_impl(
)
return result
+ if (
+ copilot_ctx.turn_halt is not None
+ or copilot_ctx.discovery_failure_streak_this_turn >= DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD
+ ):
+ result = _discovery_build_result(
+ candidate_url=None,
+ candidate_form_fields=[],
+ evidence_trail=[],
+ confidence=0.0,
+ failure_reason=DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
+ )
+ return finish(result)
+
authority_error = _authority_tool_error(copilot_ctx, "discover_workflow_entrypoint")
if authority_error:
result = {"ok": False, "error": authority_error}
diff --git a/skyvern/forge/sdk/copilot/turn_halt.py b/skyvern/forge/sdk/copilot/turn_halt.py
index c7c28faed..ef0037c88 100644
--- a/skyvern/forge/sdk/copilot/turn_halt.py
+++ b/skyvern/forge/sdk/copilot/turn_halt.py
@@ -10,6 +10,7 @@ import structlog
from skyvern.forge.sdk.copilot.blocker_signal import (
DEFINITION_CONTRACT_UNSATISFIED_REASON_CODE,
+ DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
)
from skyvern.forge.sdk.copilot.blocker_signal import (
GENUINELY_TERMINAL_BLOCKER_REASON_CODES as GENUINELY_TERMINAL_BLOCKER_REASON_CODES,
@@ -80,6 +81,7 @@ _LOOP_TERMINAL_REASON_CODES = frozenset(
"code_authoring_guardrail_churn",
"credential_priority_authoring_churn",
"loop_detected_no_forward_progress_interaction",
+ DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,
}
)
_ACTIVE_TERMINAL_CHALLENGE_REASON_CODES = frozenset(
diff --git a/tests/unit/test_copilot_agent_blocker_renderer.py b/tests/unit/test_copilot_agent_blocker_renderer.py
index 2ddfa9163..62b03ec93 100644
--- a/tests/unit/test_copilot_agent_blocker_renderer.py
+++ b/tests/unit/test_copilot_agent_blocker_renderer.py
@@ -31,6 +31,7 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
from skyvern.forge.sdk.copilot.completion_verification import CompletionVerificationResult, CriterionVerdict
from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy
from skyvern.forge.sdk.copilot.context import AgentResult, CopilotContext, DeliveredUnverifiedPublicOutputs
+from skyvern.forge.sdk.copilot.enforcement import SCOUTED_SPINE_TURN_HALT_USER_REASON
from skyvern.forge.sdk.copilot.output_contracts import OutputContractAdvisoryState
from skyvern.forge.sdk.copilot.output_policy import (
ACTUATION_OBLIGATION_STEER_REASON_CODE,
@@ -40,6 +41,7 @@ from skyvern.forge.sdk.copilot.output_policy import (
)
from skyvern.forge.sdk.copilot.request_policy import CompletionCriterion, RequestPolicy
from skyvern.forge.sdk.copilot.run_outcome import TERMINAL_CHALLENGE_BLOCKER_REASON_CODE, RecordedRunOutcome
+from skyvern.forge.sdk.copilot.tools.discovery import _build_discovery_exhausted_escape_signal
from skyvern.forge.sdk.copilot.turn_halt import TurnHalt, TurnHaltKind
from skyvern.forge.sdk.copilot.turn_ownership import TurnClaimant, claim_and_stash_blocker_signal
from skyvern.forge.sdk.schemas.workflow_copilot import WorkflowCopilotChatRequest
@@ -601,6 +603,18 @@ def test_turn_halt_exit_renders_no_forward_progress_interaction_reason() -> None
assert "click" not in result.user_response
+def test_turn_halt_exit_keeps_discovery_exhaustion_ask_when_no_scouted_spine() -> None:
+ ctx = _ctx()
+ signal = _build_discovery_exhausted_escape_signal()
+ ctx.blocker_signal = signal
+ halt = TurnHalt(kind=TurnHaltKind.LOOP_DETECTED, blocker_signal=signal)
+
+ result = _build_turn_halt_exit_result(ctx, global_llm_context=None, halt=halt)
+
+ assert result.user_response == signal.user_facing_reason
+ assert result.user_response != SCOUTED_SPINE_TURN_HALT_USER_REASON
+
+
def test_turn_halt_exit_keeps_credential_scout_reply_through_refresh_with_draft() -> None:
ctx = _ctx()
ctx.last_workflow_yaml = "title: Draft\nworkflow_definition:\n blocks: []\n"
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index c17940316..d2cafb12a 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -122,6 +122,7 @@ from skyvern.forge.sdk.copilot.turn_intent import (
)
from skyvern.forge.sdk.copilot.turn_origin import TurnOrigin
from skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence
+from skyvern.forge.sdk.routes.workflow_copilot import CHAT_HISTORY_CONTEXT_MESSAGES
from skyvern.forge.sdk.schemas.copilot_turn_outcome import ResponseKind, TurnOutcome
from skyvern.forge.sdk.schemas.workflow_copilot import (
WorkflowCopilotChatHistoryMessage,
@@ -7360,3 +7361,27 @@ class TestDeclaredEqualsGradedCompletionCriteria:
assert "__copilot_fallback_floor__run" not in verification.criterion_ids
assert verification.criterion_ids == ["__copilot_authored_output_contract_missing"]
assert not verification.is_fully_satisfied()
+
+
+def _transcript_packet(earliest_user_turn: str) -> TurnContextPacket:
+ return TurnContextPacket(
+ turn_intent_summary={},
+ transcript_context=TranscriptContext(
+ earliest_user_turn=earliest_user_turn,
+ latest_prior_user_turn="",
+ latest_assistant_turn="",
+ retained_history="",
+ omitted_any=False,
+ ),
+ omissions=[],
+ )
+
+
+def test_transcript_anchor_blanked_when_retained_window_at_capacity() -> None:
+ packet = _transcript_packet("go to https://example.com/login")
+ assert agent_module._transcript_anchor_for_turn(packet, CHAT_HISTORY_CONTEXT_MESSAGES - 1) == (
+ "go to https://example.com/login"
+ )
+ assert agent_module._transcript_anchor_for_turn(packet, CHAT_HISTORY_CONTEXT_MESSAGES) == ""
+ assert agent_module._transcript_anchor_for_turn(packet, CHAT_HISTORY_CONTEXT_MESSAGES + 5) == ""
+ assert agent_module._transcript_anchor_for_turn(None, 0) == ""
diff --git a/tests/unit/test_copilot_build_phase.py b/tests/unit/test_copilot_build_phase.py
index 3cafbc6e3..bc489a35b 100644
--- a/tests/unit/test_copilot_build_phase.py
+++ b/tests/unit/test_copilot_build_phase.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+from datetime import datetime, timezone
+
import pytest
from skyvern.forge.sdk.copilot.build_phase import (
@@ -13,9 +15,16 @@ from skyvern.forge.sdk.copilot.build_phase import (
advance_to_composing,
advance_to_discovering,
advance_to_testing,
+ anchor_recovers_entrypoint,
+ extract_anchor_entry_url,
initial_build_phase,
)
+from skyvern.forge.sdk.copilot.request_policy import build_transcript_context
from skyvern.forge.sdk.copilot.turn_intent import TurnIntent, TurnIntentMode
+from skyvern.forge.sdk.schemas.workflow_copilot import (
+ WorkflowCopilotChatHistoryMessage,
+ WorkflowCopilotChatSender,
+)
class _Ctx:
@@ -161,6 +170,166 @@ def test_initial_build_phase_returns_expected(
assert initial_build_phase(_ti(mode), user_message, agent_message, workflow_yaml) == expected
+def test_initial_build_phase_resolves_composing_from_transcript_anchor() -> None:
+ # No this-turn URL signal, but the earliest-user-turn anchor carries the URL
+ # a prior abnormally-ended turn dropped -> COMPOSING.
+ assert (
+ initial_build_phase(
+ _ti(TurnIntentMode.BUILD),
+ "yes go ahead",
+ "yes go ahead",
+ "",
+ "go to https://example.com/login and fill it",
+ )
+ == BuildPhase.COMPOSING
+ )
+ # Sentinel anchor ("(none)") carries no URL -> INITIAL.
+ assert (
+ initial_build_phase(_ti(TurnIntentMode.BUILD), "yes go ahead", "yes go ahead", "", "(none)")
+ == BuildPhase.INITIAL
+ )
+
+
+def test_initial_build_phase_default_anchor_arg_is_back_compatible() -> None:
+ # Existing four-arg callers keep INITIAL when no this-turn URL exists.
+ assert initial_build_phase(_ti(TurnIntentMode.BUILD), "go to example", "go to example", "") == BuildPhase.INITIAL
+
+
+ "text,expected",
+ [
+ ("go to https://example.com/login", "https://example.com/login"),
+ ("visit https://example.com/path.", "https://example.com/path"),
+ ("(https://example.com/x)", "https://example.com/x"),
+ (
+ "open http://localhost:8942/analytics_console/houndline/?captcha=block&mfa=1",
+ "http://localhost:8942/analytics_console/houndline/?captcha=block&mfa=1",
+ ),
+ ("no url here at all", None),
+ ("(none)", None),
+ ("", None),
+ (None, None),
+ # Bare host with no dot and no port is rejected as mangled.
+ ("go to http://internalhost/path", None),
+ # A match abutting the middle-truncation marker is rejected.
+ ("prefix https://example.com/very-long-pa<…120 chars truncated…>th", None),
+ ("<…120 chars truncated…>https://example.com/tail", None),
+ ("go to https://example.com… please", None),
+ ("https://example.com/aaa…bbb", None),
+ # Markdown delimiters wrapping a URL are stripped, not adopted into the path.
+ (
+ "use `http://localhost:8945/analytics_console/houndline/`",
+ "http://localhost:8945/analytics_console/houndline/",
+ ),
+ ("bold *http://localhost:8945/x* here", "http://localhost:8945/x"),
+ ("under _http://localhost:8945/y_ end", "http://localhost:8945/y"),
+ ("tilde ~http://localhost:8945/z~ end", "http://localhost:8945/z"),
+ # A bare trailing wrapper char that is a legal URL character is preserved when the URL is not wrapped.
+ ("data at http://localhost:8945/path_bar_ end", "http://localhost:8945/path_bar_"),
+ # A markdown-wrapped URL is unwrapped while its internal underscore stays intact.
+ ("emphasis _http://localhost:8945/foo_bar_ done", "http://localhost:8945/foo_bar"),
+ ],
+)
+def test_extract_anchor_entry_url(text: str | None, expected: str | None) -> None:
+ assert extract_anchor_entry_url(text) == expected
+
+
+ "text",
+ [
+ "http://localhost:abc",
+ "go to http://example.com:999999/x",
+ "visit http://localhost:70000",
+ ],
+)
+def test_extract_anchor_entry_url_malformed_port_returns_none(text: str) -> None:
+ assert extract_anchor_entry_url(text) is None
+
+
+_FIXTURE_ENTRYPOINT_URL = "http://localhost:8942/analytics_console/houndline/?captcha=block&mfa=1"
+
+
+def _history_message(sender: WorkflowCopilotChatSender, content: str) -> WorkflowCopilotChatHistoryMessage:
+ return WorkflowCopilotChatHistoryMessage(
+ sender=sender, content=content, created_at=datetime(2026, 7, 23, tzinfo=timezone.utc)
+ )
+
+
+def test_two_turn_bare_answer_recovers_entrypoint_from_earliest_user_turn() -> None:
+ messages = [
+ _history_message(WorkflowCopilotChatSender.USER, f"Build me a workflow at {_FIXTURE_ENTRYPOINT_URL}"),
+ _history_message(WorkflowCopilotChatSender.AI, "Which page should I start on?"),
+ ]
+ transcript = build_transcript_context(messages, "yes go ahead")
+ anchor = transcript.earliest_user_turn
+
+ assert (
+ initial_build_phase(_ti(TurnIntentMode.BUILD), "yes go ahead", "yes go ahead", "", anchor)
+ == BuildPhase.COMPOSING
+ )
+ assert (
+ anchor_recovers_entrypoint(_ti(TurnIntentMode.BUILD), "yes go ahead", "yes go ahead", "", anchor)
+ == _FIXTURE_ENTRYPOINT_URL
+ )
+
+
+def test_anchor_recovery_defers_to_in_turn_url_signal() -> None:
+ assert (
+ anchor_recovers_entrypoint(
+ _ti(TurnIntentMode.BUILD),
+ "actually go to https://fresh.example/now",
+ "",
+ "",
+ "earlier: https://stale.example/old",
+ )
+ is None
+ )
+
+
+def test_anchor_recovery_defers_to_in_turn_yaml_url() -> None:
+ yaml_with_url = """
+title: T
+workflow_definition:
+ parameters: []
+ blocks:
+ - block_type: goto_url
+ label: open
+ url: https://fresh.example/page
+"""
+ assert (
+ anchor_recovers_entrypoint(
+ _ti(TurnIntentMode.BUILD), "run it", "run it", yaml_with_url, "earlier: https://stale.example/old"
+ )
+ is None
+ )
+
+
+def test_anchor_recovery_skips_non_build_modes() -> None:
+ assert (
+ anchor_recovers_entrypoint(_ti(TurnIntentMode.EDIT), "tweak it", "tweak it", "", _FIXTURE_ENTRYPOINT_URL)
+ is None
+ )
+
+
+def test_anchor_recovery_none_without_anchor_url() -> None:
+ assert anchor_recovers_entrypoint(_ti(TurnIntentMode.BUILD), "yes", "yes", "", "(none)") is None
+
+
+def test_anchor_recovery_adopts_earliest_url_on_url_less_corrective_pivot() -> None:
+ # Ceiling: a URL-less corrective pivot carries no in-turn signal, so the
+ # earliest-turn anchor still resolves — the pivot cannot retarget without a URL.
+ assert (
+ anchor_recovers_entrypoint(
+ _ti(TurnIntentMode.BUILD),
+ "no, do the reporting flow instead",
+ "no, do the reporting flow instead",
+ "",
+ f"Build me a workflow at {_FIXTURE_ENTRYPOINT_URL}",
+ )
+ == _FIXTURE_ENTRYPOINT_URL
+ )
+
+
def test_initial_build_phase_none_turn_intent_acts_like_unknown_mode() -> None:
# turn_intent=None is treated as if mode is UNKNOWN — eligible for INITIAL
# when no URL signal exists. The phase gate then blocks mutation until
diff --git a/tests/unit/test_copilot_pre_discovery_url_nudge.py b/tests/unit/test_copilot_pre_discovery_url_nudge.py
index d7ca83bcf..e0a5250fc 100644
--- a/tests/unit/test_copilot_pre_discovery_url_nudge.py
+++ b/tests/unit/test_copilot_pre_discovery_url_nudge.py
@@ -16,7 +16,7 @@ from types import SimpleNamespace
import pytest
-from skyvern.forge.sdk.copilot.build_phase import BuildPhase
+from skyvern.forge.sdk.copilot.build_phase import DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD, BuildPhase
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.copilot.enforcement import (
MAX_PRE_DISCOVERY_URL_QUESTION_NUDGES,
@@ -141,6 +141,18 @@ def test_no_nudge_for_non_ask_question() -> None:
assert _pre_discovery_url_question_nudge(ctx, reply) is None
+def test_no_nudge_after_discovery_failure_escape() -> None:
+ ctx = _Ctx()
+ ctx.discovery_failure_streak_this_turn = DISCOVERY_FAILURE_STREAK_ESCAPE_THRESHOLD
+ assert _pre_discovery_url_question_nudge(ctx, _URL_ASK) is None
+
+
+def test_no_nudge_when_turn_halt_stashed() -> None:
+ ctx = _Ctx()
+ ctx.turn_halt = object()
+ assert _pre_discovery_url_question_nudge(ctx, _URL_ASK) is None
+
+
# ---------------------------------------------------------------------------
# No over-suppression — legitimate INITIAL asks are let through.
# ---------------------------------------------------------------------------