Skip to content

Automate browser based workflows with AI

SKY-10642 Simplify Workflow Copilot LLM lanes (#6355)

Andrew Neilson72445267e432
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 576a87183..e2acf7d44 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -177,10 +177,8 @@ def _copilot_turn_span(
yield span
-def _resolve_request_policy_handler(fallback_handler: Any) -> Any:
- with contextlib.suppress(RuntimeError, AttributeError):
- return app.WORKFLOW_COPILOT_FAST_LLM_API_HANDLER or fallback_handler
- return fallback_handler
+def _resolve_request_policy_handler(llm_api_handler: Any) -> Any:
+ return llm_api_handler
@dataclass(frozen=True)
diff --git a/skyvern/forge/sdk/copilot/llm_config.py b/skyvern/forge/sdk/copilot/llm_config.py
new file mode 100644
index 000000000..c845c3378
--- /dev/null
+++ b/skyvern/forge/sdk/copilot/llm_config.py
@@ -0,0 +1,67 @@
+from __future__ import annotations
+
+from typing import Any
+
+import structlog
+
+from skyvern.forge import app
+from skyvern.forge.sdk.experimentation.llm_prompt_config import get_llm_handler_for_prompt_type
+
+LOG = structlog.get_logger()
+
+WORKFLOW_COPILOT_PROMPT_TYPE = "workflow-copilot"
+WORKFLOW_COPILOT_FAST_PROMPT_TYPE = "workflow-copilot-fast"
+
+
+def get_main_copilot_handler() -> Any | None:
+ try:
+ handler = app.WORKFLOW_COPILOT_AGENT_LLM_API_HANDLER
+ except (RuntimeError, AttributeError):
+ handler = None
+ if handler is not None:
+ return handler
+ try:
+ return app.LLM_API_HANDLER
+ except (RuntimeError, AttributeError):
+ return None
+
+
+def get_fast_copilot_handler() -> Any | None:
+ try:
+ handler = app.WORKFLOW_COPILOT_FAST_LLM_API_HANDLER
+ except (RuntimeError, AttributeError):
+ handler = None
+ if handler is not None:
+ return handler
+ try:
+ return app.SECONDARY_LLM_API_HANDLER
+ except (RuntimeError, AttributeError):
+ return None
+
+
+async def resolve_main_copilot_handler(workflow_permanent_id: str | None, organization_id: str | None) -> Any | None:
+ if workflow_permanent_id and organization_id:
+ try:
+ posthog_handler = await get_llm_handler_for_prompt_type(
+ WORKFLOW_COPILOT_PROMPT_TYPE, workflow_permanent_id, organization_id
+ )
+ except Exception as exc:
+ LOG.warning("main copilot PostHog lookup failed, falling back", error=str(exc))
+ posthog_handler = None
+ if posthog_handler is not None:
+ return posthog_handler
+ return get_main_copilot_handler()
+
+
+async def resolve_fast_copilot_handler(workflow_permanent_id: str | None, organization_id: str | None) -> Any | None:
+ if workflow_permanent_id and organization_id:
+ try:
+ posthog_handler = await get_llm_handler_for_prompt_type(
+ WORKFLOW_COPILOT_FAST_PROMPT_TYPE, workflow_permanent_id, organization_id
+ )
+ except Exception as exc:
+ LOG.warning("fast copilot PostHog lookup failed, falling back", error=str(exc))
+ posthog_handler = None
+ if posthog_handler is not None:
+ return posthog_handler
+ return get_fast_copilot_handler()
diff --git a/skyvern/forge/sdk/copilot/narration.py b/skyvern/forge/sdk/copilot/narration.py
index 15b4e7a59..a241a40ed 100644
--- a/skyvern/forge/sdk/copilot/narration.py
+++ b/skyvern/forge/sdk/copilot/narration.py
@@ -27,8 +27,7 @@ from urllib.parse import urlparse
import structlog
-from skyvern.forge import app
-from skyvern.forge.sdk.experimentation.llm_prompt_config import get_llm_handler_for_prompt_type
+from skyvern.forge.sdk.copilot.llm_config import get_fast_copilot_handler, resolve_fast_copilot_handler
from skyvern.forge.sdk.schemas.workflow_copilot import (
WorkflowCopilotBlockProgressUpdate,
WorkflowCopilotNarrationUpdate,
@@ -426,34 +425,11 @@ async def _call_narrator_llm(prompt_ctx: _NarratorPromptContext, handler: Any) -
def _get_narrator_handler() -> Any:
- # SECONDARY also serves the scraper and other non-copilot paths; prefer
- # the copilot-scoped fast handler so narration tunes independently.
- try:
- handler = app.WORKFLOW_COPILOT_FAST_LLM_API_HANDLER
- except (RuntimeError, AttributeError):
- handler = None
- if handler is not None:
- return handler
- try:
- return app.SECONDARY_LLM_API_HANDLER
- except (RuntimeError, AttributeError):
- return None
+ return get_fast_copilot_handler()
async def resolve_narrator_handler(workflow_permanent_id: str | None, organization_id: str | None) -> Any:
- # Resolution order: PostHog LLM_CONFIG_BY_PROMPT_TYPE["workflow-copilot-narration"]
- # → env-driven WORKFLOW_COPILOT_FAST_LLM_API_HANDLER → SECONDARY_LLM_API_HANDLER.
- if workflow_permanent_id and organization_id:
- try:
- posthog_handler = await get_llm_handler_for_prompt_type(
- "workflow-copilot-narration", workflow_permanent_id, organization_id
- )
- except Exception as exc:
- LOG.warning("narrator PostHog lookup failed, falling back", error=str(exc))
- posthog_handler = None
- if posthog_handler is not None:
- return posthog_handler
- return _get_narrator_handler()
+ return await resolve_fast_copilot_handler(workflow_permanent_id, organization_id)
def handler_available() -> bool:
diff --git a/skyvern/forge/sdk/copilot/tools.py b/skyvern/forge/sdk/copilot/tools.py
index 70204ffbb..0ef835816 100644
--- a/skyvern/forge/sdk/copilot/tools.py
+++ b/skyvern/forge/sdk/copilot/tools.py
@@ -4,7 +4,6 @@ from __future__ import annotations
import asyncio
import base64
-import contextlib
import json
import re
import time
@@ -82,6 +81,7 @@ from skyvern.forge.sdk.copilot.failure_tracking import (
compute_action_sequence_fingerprint,
update_repeated_failure_state,
)
+from skyvern.forge.sdk.copilot.llm_config import resolve_main_copilot_handler
from skyvern.forge.sdk.copilot.loop_detection import (
clear_failed_step_tracker_for_tools_in_ctx,
detect_failed_tool_step_loop_for_ctx,
@@ -5340,10 +5340,11 @@ def _update_verification_evidence_from_run_result(copilot_ctx: AgentContext, res
_COMPLETION_VERIFICATION_BUDGET_MARGIN_SECONDS = 5.0
-def _completion_verification_handler() -> Any:
- with contextlib.suppress(RuntimeError, AttributeError):
- return app.WORKFLOW_COPILOT_FAST_LLM_API_HANDLER
- return None
+async def _completion_verification_handler(copilot_ctx: Any) -> Any | None:
+ return await resolve_main_copilot_handler(
+ getattr(copilot_ctx, "workflow_permanent_id", None),
+ getattr(copilot_ctx, "organization_id", None),
+ )
def _is_outcome_evidence_candidate(copilot_ctx: Any, result: dict[str, Any]) -> bool:
@@ -5430,7 +5431,7 @@ async def _maybe_run_completion_verification(
return None
# A missing judge handler is an infra/config state, not a transient failure:
# fall back to the prior gate rather than fail closed on every run.
- handler = _completion_verification_handler()
+ handler = await _completion_verification_handler(copilot_ctx)
if handler is None:
return None
# Too little budget to verify a candidate run: fail closed (unavailable) rather
@@ -6670,11 +6671,11 @@ def _composition_visual_prompt(evidence: dict[str, Any]) -> str:
)
-def _composition_visual_handler() -> Any | None:
- try:
- return app.WORKFLOW_COPILOT_FAST_LLM_API_HANDLER
- except (RuntimeError, AttributeError):
- return None
+async def _composition_visual_handler(ctx: CopilotContext) -> Any | None:
+ return await resolve_main_copilot_handler(
+ getattr(ctx, "workflow_permanent_id", None),
+ getattr(ctx, "organization_id", None),
+ )
def _normalize_visual_summary(value: Any) -> dict[str, Any] | None:
@@ -6710,9 +6711,9 @@ async def _composition_summarize_screenshot(
evidence: dict[str, Any],
screenshot_b64: str,
) -> tuple[dict[str, Any] | None, str | None]:
- handler = _composition_visual_handler()
+ handler = await _composition_visual_handler(ctx)
if handler is None:
- return None, "workflow copilot fast LLM handler is not configured"
+ return None, "workflow copilot LLM handler is not configured"
try:
screenshot_bytes = base64.b64decode(screenshot_b64, validate=True)
except Exception:
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index e2a37f8f4..aa64e0b13 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -28,6 +28,7 @@ from skyvern.forge.sdk.copilot.attribution import is_copilot_born_initial_write,
from skyvern.forge.sdk.copilot.block_type_aliases import normalize_copilot_block_type_alias
from skyvern.forge.sdk.copilot.config import CopilotConfig
from skyvern.forge.sdk.copilot.context import AgentResult, ProposalDisposition, TurnNarrativePayload
+from skyvern.forge.sdk.copilot.llm_config import resolve_main_copilot_handler
from skyvern.forge.sdk.copilot.output_utils import truncate_output
from skyvern.forge.sdk.copilot.recoverable_failure import (
RecoverableFailure,
@@ -36,7 +37,6 @@ from skyvern.forge.sdk.copilot.recoverable_failure import (
merge_failure_into_context,
)
from skyvern.forge.sdk.core import skyvern_context
-from skyvern.forge.sdk.experimentation.llm_prompt_config import get_llm_handler_for_prompt_type
from skyvern.forge.sdk.routes.event_source_stream import EventSourceStream, FastAPIEventSourceStream
from skyvern.forge.sdk.routes.routers import base_router
from skyvern.forge.sdk.schemas.copilot_turn_outcome import TurnOutcome
@@ -143,22 +143,8 @@ async def _resolve_copilot_agent_handler(
workflow_permanent_id: str,
organization_id: str,
) -> LLMAPIHandler:
- try:
- posthog_handler = await get_llm_handler_for_prompt_type(
- "workflow-copilot", workflow_permanent_id, organization_id
- )
- except Exception as exc:
- LOG.warning("copilot agent PostHog lookup failed, falling back", error=str(exc))
- posthog_handler = None
- if posthog_handler is not None:
- return posthog_handler
- # AppHolder.__getattr__ raises bare RuntimeError (not AttributeError)
- # pre-startup; getattr(...,default) would not catch it.
- try:
- dedicated = app.WORKFLOW_COPILOT_AGENT_LLM_API_HANDLER
- except (RuntimeError, AttributeError):
- dedicated = None
- return dedicated or app.LLM_API_HANDLER
+ handler = await resolve_main_copilot_handler(workflow_permanent_id, organization_id)
+ return handler or app.LLM_API_HANDLER
@contextmanager
@@ -844,10 +830,7 @@ async def copilot_call_llm(
system_prompt_len=len(system_prompt),
user_prompt_len=len(user_prompt),
)
- llm_api_handler = (
- await get_llm_handler_for_prompt_type("workflow-copilot", chat_request.workflow_permanent_id, organization_id)
- or app.LLM_API_HANDLER
- )
+ llm_api_handler = await _resolve_copilot_agent_handler(chat_request.workflow_permanent_id, organization_id)
llm_start_time = time.monotonic()
llm_response = await llm_api_handler(
prompt=user_prompt,
diff --git a/tests/unit/test_copilot_completion_verification.py b/tests/unit/test_copilot_completion_verification.py
index c119d6034..1dfde1f07 100644
--- a/tests/unit/test_copilot_completion_verification.py
+++ b/tests/unit/test_copilot_completion_verification.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import time
+from collections.abc import Awaitable, Callable
from types import SimpleNamespace
import pytest
@@ -57,6 +58,13 @@ def _evaluated(*satisfied_by_id: tuple[str, bool]) -> CompletionVerificationResu
return CompletionVerificationResult(status="evaluated", criterion_ids=ids, verdicts=verdicts)
+def _completion_handler_lookup(handler: object) -> Callable[[object], Awaitable[object]]:
+ async def _lookup(_ctx: object) -> object:
+ return handler
+
+ return _lookup
+
+
def test_is_fully_satisfied_requires_every_criterion() -> None:
assert _evaluated(("c0", True), ("c1", True)).is_fully_satisfied() is True
assert _evaluated(("c0", True), ("c1", False)).is_fully_satisfied() is False
@@ -407,7 +415,10 @@ async def test_maybe_run_completion_verification_runs_on_canceled_run(monkeypatc
async def handler(**_: object) -> dict:
return {"verdicts": [{"criterion_id": "c0", "satisfied": True, "reason_code": "evidence_confirms"}]}
- monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._completion_verification_handler", lambda: handler)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools._completion_verification_handler",
+ _completion_handler_lookup(handler),
+ )
ctx = _run_ctx()
result = await _maybe_run_completion_verification(ctx, _canceled_budget_result(), time.monotonic())
assert result is not None
@@ -513,7 +524,10 @@ async def test_maybe_run_completion_verification_runs_on_unverified_prefix(monke
async def handler(**_: object) -> dict:
return {"verdicts": [{"criterion_id": "c0", "satisfied": True, "reason_code": "evidence_confirms"}]}
- monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._completion_verification_handler", lambda: handler)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools._completion_verification_handler",
+ _completion_handler_lookup(handler),
+ )
ctx = _ctx_unverified_prefix()
result = await _maybe_run_completion_verification(ctx, _clean_success_result(), time.monotonic())
assert result is not None
@@ -540,7 +554,10 @@ async def test_method_mandated_criteria_excluded_from_verification(monkeypatch:
async def handler(**_: object) -> dict:
return {"verdicts": [{"criterion_id": "c0", "satisfied": True, "reason_code": "evidence_confirms"}]}
- monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._completion_verification_handler", lambda: handler)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools._completion_verification_handler",
+ _completion_handler_lookup(handler),
+ )
ctx = _run_ctx()
ctx.request_policy = RequestPolicy(
completion_criteria=[
@@ -596,7 +613,10 @@ async def test_maybe_run_completion_verification_unavailable_on_low_budget(monke
async def handler(**_: object) -> dict:
return {"verdicts": [{"criterion_id": "c0", "satisfied": True, "reason_code": "evidence_confirms"}]}
- monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._completion_verification_handler", lambda: handler)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools._completion_verification_handler",
+ _completion_handler_lookup(handler),
+ )
ctx = _run_ctx()
starved = time.monotonic() - 100_000 # no budget left to verify this candidate run
result = await _maybe_run_completion_verification(ctx, _clean_success_result(), starved)
@@ -607,7 +627,10 @@ async def test_maybe_run_completion_verification_unavailable_on_low_budget(monke
assert result.is_fully_satisfied() is False
# A missing judge handler stays a soft fallback (None), not a hard block.
- monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._completion_verification_handler", lambda: None)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools._completion_verification_handler",
+ _completion_handler_lookup(None),
+ )
assert await _maybe_run_completion_verification(ctx, _clean_success_result(), time.monotonic()) is None
diff --git a/tests/unit/test_copilot_streaming_adapter.py b/tests/unit/test_copilot_streaming_adapter.py
index 25fd07f4e..68c6b117b 100644
--- a/tests/unit/test_copilot_streaming_adapter.py
+++ b/tests/unit/test_copilot_streaming_adapter.py
@@ -205,14 +205,16 @@ async def test_stream_to_sse_emits_narration_on_workflow_updated_transition(
from agents.items import RunItem
from agents.stream_events import RunItemStreamEvent
- from skyvern.forge.sdk.copilot import narration
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.schemas.workflow_copilot import WorkflowCopilotStreamMessageType
async def _handler(prompt: str, prompt_name: str, **kwargs: object) -> str:
return "Revising the workflow draft."
- monkeypatch.setattr(narration, "_get_narrator_handler", lambda: _handler)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.streaming_adapter.resolve_narrator_handler",
+ AsyncMock(return_value=_handler),
+ )
# Tool input / output raw_items matching the shapes streaming_adapter reads.
call_raw = {"call_id": "c-upd", "name": "update_workflow", "arguments": "{}"}
diff --git a/tests/unit/test_workflow_copilot_cancel.py b/tests/unit/test_workflow_copilot_cancel.py
index 4a4c9d245..43a166c6e 100644
--- a/tests/unit/test_workflow_copilot_cancel.py
+++ b/tests/unit/test_workflow_copilot_cancel.py
@@ -207,7 +207,7 @@ def _setup_route_mocks(
return None
monkeypatch.setattr(
- "skyvern.forge.sdk.routes.workflow_copilot.get_llm_handler_for_prompt_type",
+ "skyvern.forge.sdk.routes.workflow_copilot.resolve_main_copilot_handler",
fake_llm_handler,
)
@@ -824,7 +824,7 @@ async def test_operational_cancel_does_not_persist_cancelled_message(
return None
monkeypatch.setattr(
- "skyvern.forge.sdk.routes.workflow_copilot.get_llm_handler_for_prompt_type",
+ "skyvern.forge.sdk.routes.workflow_copilot.resolve_main_copilot_handler",
fake_llm_handler,
)
monkeypatch.setattr(
diff --git a/tests/unit/test_workflow_copilot_llm_config.py b/tests/unit/test_workflow_copilot_llm_config.py
index 0bfeb3fc1..4bcc7765e 100644
--- a/tests/unit/test_workflow_copilot_llm_config.py
+++ b/tests/unit/test_workflow_copilot_llm_config.py
@@ -1,10 +1,10 @@
-"""Tests for the workflow-copilot v2 LLM key wiring (SKY-9398).
+"""Tests for the workflow-copilot v2 LLM key wiring (SKY-10642).
-Two optional settings give operators independent control over (a) the
-Agents-SDK reasoning loop and (b) narration: ``WORKFLOW_COPILOT_AGENT_LLM_KEY``
-and ``WORKFLOW_COPILOT_FAST_LLM_KEY``. These tests cover the public
-contract: defaults, narration handler fallback chain, and the route helper's
-PostHog → dedicated → primary resolution order.
+Two optional settings give operators independent control over (a) the main
+Copilot reasoning/guardrail/evidence lane and (b) the narration-only fast lane:
+``WORKFLOW_COPILOT_AGENT_LLM_KEY`` and ``WORKFLOW_COPILOT_FAST_LLM_KEY``.
+These tests cover the public contract: defaults, fallback chains, and
+PostHog → env-specific → default resolution order.
"""
from __future__ import annotations
@@ -15,7 +15,10 @@ from typing import Any
import pytest
from skyvern.config import Settings
+from skyvern.forge.sdk.copilot import agent as copilot_agent
+from skyvern.forge.sdk.copilot import llm_config as copilot_llm_config
from skyvern.forge.sdk.copilot import narration
+from skyvern.forge.sdk.copilot import tools as copilot_tools
from skyvern.forge.sdk.routes import workflow_copilot as workflow_copilot_route
# ---------------------------------------------------------------------------
@@ -52,7 +55,7 @@ def test_narrator_handler_prefers_dedicated_when_set(monkeypatch: pytest.MonkeyP
dedicated = object()
secondary = object()
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(
WORKFLOW_COPILOT_FAST_LLM_API_HANDLER=dedicated,
@@ -67,7 +70,7 @@ def test_narrator_handler_falls_back_to_secondary_on_attribute_error(monkeypatch
the fallback must catch that branch as well as the holder's ``RuntimeError``."""
secondary = object()
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(SECONDARY_LLM_API_HANDLER=secondary),
)
@@ -79,7 +82,7 @@ def test_narrator_handler_falls_back_to_secondary_on_runtime_error(monkeypatch:
not ``AttributeError`` — the fallback must catch both."""
secondary = object()
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
_AppHolderStub(SECONDARY_LLM_API_HANDLER=secondary),
)
@@ -91,7 +94,7 @@ def test_narrator_handler_falls_back_to_secondary_when_dedicated_is_none(monkeyp
to ``None`` must not silently disable narration when SECONDARY is wired."""
secondary = object()
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(
WORKFLOW_COPILOT_FAST_LLM_API_HANDLER=None,
@@ -102,27 +105,45 @@ def test_narrator_handler_falls_back_to_secondary_when_dedicated_is_none(monkeyp
def test_narrator_handler_returns_none_when_both_unreachable(monkeypatch: pytest.MonkeyPatch) -> None:
- monkeypatch.setattr(narration, "app", _AppHolderStub())
+ monkeypatch.setattr(copilot_llm_config, "app", _AppHolderStub())
assert narration._get_narrator_handler() is None
# ---------------------------------------------------------------------------
-# _resolve_copilot_agent_handler fallback chain
+# main Copilot handler fallback chain
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
-async def test_resolve_agent_handler_posthog_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
+async def test_route_resolve_copilot_agent_handler_delegates_to_main_lane(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ main_handler = object()
+
+ async def _main_lookup(workflow_permanent_id: str | None, organization_id: str | None) -> object:
+ assert workflow_permanent_id == "wpid_1"
+ assert organization_id == "org_1"
+ return main_handler
+
+ monkeypatch.setattr(workflow_copilot_route, "resolve_main_copilot_handler", _main_lookup)
+
+ handler = await workflow_copilot_route._resolve_copilot_agent_handler("wpid_1", "org_1")
+ assert handler is main_handler
+
+
+async def test_resolve_main_copilot_handler_posthog_override_wins(monkeypatch: pytest.MonkeyPatch) -> None:
posthog_handler = object()
dedicated = object()
primary = object()
- async def _posthog_lookup(*_args: object, **_kwargs: object) -> object:
+ async def _posthog_lookup(prompt_type: str, *_args: object, **_kwargs: object) -> object:
+ assert prompt_type == "workflow-copilot"
return posthog_handler
- monkeypatch.setattr(workflow_copilot_route, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- workflow_copilot_route,
+ copilot_llm_config,
"app",
SimpleNamespace(
WORKFLOW_COPILOT_AGENT_LLM_API_HANDLER=dedicated,
@@ -130,21 +151,21 @@ async def test_resolve_agent_handler_posthog_override_wins(monkeypatch: pytest.M
),
)
- handler = await workflow_copilot_route._resolve_copilot_agent_handler("wpid_1", "org_1")
+ handler = await copilot_llm_config.resolve_main_copilot_handler("wpid_1", "org_1")
assert handler is posthog_handler
@pytest.mark.asyncio
-async def test_resolve_agent_handler_falls_back_to_dedicated(monkeypatch: pytest.MonkeyPatch) -> None:
+async def test_resolve_main_copilot_handler_falls_back_to_dedicated(monkeypatch: pytest.MonkeyPatch) -> None:
dedicated = object()
primary = object()
async def _posthog_lookup(*_args: object, **_kwargs: object) -> None:
return None
- monkeypatch.setattr(workflow_copilot_route, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- workflow_copilot_route,
+ copilot_llm_config,
"app",
SimpleNamespace(
WORKFLOW_COPILOT_AGENT_LLM_API_HANDLER=dedicated,
@@ -152,12 +173,12 @@ async def test_resolve_agent_handler_falls_back_to_dedicated(monkeypatch: pytest
),
)
- handler = await workflow_copilot_route._resolve_copilot_agent_handler("wpid_1", "org_1")
+ handler = await copilot_llm_config.resolve_main_copilot_handler("wpid_1", "org_1")
assert handler is dedicated
@pytest.mark.asyncio
-async def test_resolve_agent_handler_falls_back_to_primary_on_attribute_error(
+async def test_resolve_main_copilot_handler_falls_back_to_primary_on_attribute_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A plain object lacking the dedicated attribute raises ``AttributeError``."""
@@ -166,19 +187,19 @@ async def test_resolve_agent_handler_falls_back_to_primary_on_attribute_error(
async def _posthog_lookup(*_args: object, **_kwargs: object) -> None:
return None
- monkeypatch.setattr(workflow_copilot_route, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- workflow_copilot_route,
+ copilot_llm_config,
"app",
SimpleNamespace(LLM_API_HANDLER=primary),
)
- handler = await workflow_copilot_route._resolve_copilot_agent_handler("wpid_1", "org_1")
+ handler = await copilot_llm_config.resolve_main_copilot_handler("wpid_1", "org_1")
assert handler is primary
@pytest.mark.asyncio
-async def test_resolve_agent_handler_falls_back_to_primary_on_runtime_error(
+async def test_resolve_main_copilot_handler_falls_back_to_primary_on_runtime_error(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""``AppHolder.__getattr__`` raises bare ``RuntimeError`` pre-startup, not
@@ -188,15 +209,15 @@ async def test_resolve_agent_handler_falls_back_to_primary_on_runtime_error(
async def _posthog_lookup(*_args: object, **_kwargs: object) -> None:
return None
- monkeypatch.setattr(workflow_copilot_route, "get_llm_handler_for_prompt_type", _posthog_lookup)
- monkeypatch.setattr(workflow_copilot_route, "app", _AppHolderStub(LLM_API_HANDLER=primary))
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "app", _AppHolderStub(LLM_API_HANDLER=primary))
- handler = await workflow_copilot_route._resolve_copilot_agent_handler("wpid_1", "org_1")
+ handler = await copilot_llm_config.resolve_main_copilot_handler("wpid_1", "org_1")
assert handler is primary
@pytest.mark.asyncio
-async def test_resolve_agent_handler_falls_back_when_dedicated_is_none(
+async def test_resolve_main_copilot_handler_falls_back_when_dedicated_is_none(
monkeypatch: pytest.MonkeyPatch,
) -> None:
primary = object()
@@ -204,9 +225,9 @@ async def test_resolve_agent_handler_falls_back_when_dedicated_is_none(
async def _posthog_lookup(*_args: object, **_kwargs: object) -> None:
return None
- monkeypatch.setattr(workflow_copilot_route, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- workflow_copilot_route,
+ copilot_llm_config,
"app",
SimpleNamespace(
WORKFLOW_COPILOT_AGENT_LLM_API_HANDLER=None,
@@ -214,7 +235,7 @@ async def test_resolve_agent_handler_falls_back_when_dedicated_is_none(
),
)
- handler = await workflow_copilot_route._resolve_copilot_agent_handler("wpid_1", "org_1")
+ handler = await copilot_llm_config.resolve_main_copilot_handler("wpid_1", "org_1")
assert handler is primary
@@ -229,12 +250,12 @@ async def test_resolve_narrator_handler_posthog_override_wins(monkeypatch: pytes
fast = object()
async def _posthog_lookup(prompt_type: str, *_args: object, **_kwargs: object) -> object:
- assert prompt_type == "workflow-copilot-narration"
+ assert prompt_type == "workflow-copilot-fast"
return posthog_handler
- monkeypatch.setattr(narration, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(WORKFLOW_COPILOT_FAST_LLM_API_HANDLER=fast, SECONDARY_LLM_API_HANDLER=object()),
)
@@ -252,9 +273,9 @@ async def test_resolve_narrator_handler_falls_back_to_fast_when_posthog_returns_
async def _posthog_lookup(*_args: object, **_kwargs: object) -> None:
return None
- monkeypatch.setattr(narration, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(WORKFLOW_COPILOT_FAST_LLM_API_HANDLER=fast, SECONDARY_LLM_API_HANDLER=object()),
)
@@ -272,9 +293,9 @@ async def test_resolve_narrator_handler_falls_back_when_posthog_raises(monkeypat
async def _raising_lookup(*_args: object, **_kwargs: object) -> object:
raise RuntimeError("posthog down")
- monkeypatch.setattr(narration, "get_llm_handler_for_prompt_type", _raising_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _raising_lookup)
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(WORKFLOW_COPILOT_FAST_LLM_API_HANDLER=fast, SECONDARY_LLM_API_HANDLER=object()),
)
@@ -295,9 +316,9 @@ async def test_resolve_narrator_handler_skips_posthog_when_ids_missing(monkeypat
posthog_called = True
return object()
- monkeypatch.setattr(narration, "get_llm_handler_for_prompt_type", _posthog_lookup)
+ monkeypatch.setattr(copilot_llm_config, "get_llm_handler_for_prompt_type", _posthog_lookup)
monkeypatch.setattr(
- narration,
+ copilot_llm_config,
"app",
SimpleNamespace(WORKFLOW_COPILOT_FAST_LLM_API_HANDLER=fast, SECONDARY_LLM_API_HANDLER=object()),
)
@@ -309,3 +330,49 @@ async def test_resolve_narrator_handler_skips_posthog_when_ids_missing(monkeypat
handler = await narration.resolve_narrator_handler("wpid_1", None)
assert handler is fast
assert posthog_called is False
+
+
+# ---------------------------------------------------------------------------
+# non-narration Copilot helpers use the main lane
+# ---------------------------------------------------------------------------
+
+
+def test_resolve_request_policy_handler_uses_main_copilot_handler() -> None:
+ main_handler = object()
+ assert copilot_agent._resolve_request_policy_handler(main_handler) is main_handler
+
+
+async def test_completion_verification_handler_uses_main_copilot_lane(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ main_handler = object()
+
+ async def _main_lookup(workflow_permanent_id: str | None, organization_id: str | None) -> object:
+ assert workflow_permanent_id == "wpid_1"
+ assert organization_id == "org_1"
+ return main_handler
+
+ monkeypatch.setattr(copilot_tools, "resolve_main_copilot_handler", _main_lookup)
+ ctx: Any = SimpleNamespace(workflow_permanent_id="wpid_1", organization_id="org_1")
+
+ handler = await copilot_tools._completion_verification_handler(ctx)
+ assert handler is main_handler
+
+
+async def test_composition_visual_handler_uses_main_copilot_lane(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ main_handler = object()
+
+ async def _main_lookup(workflow_permanent_id: str | None, organization_id: str | None) -> object:
+ assert workflow_permanent_id == "wpid_1"
+ assert organization_id == "org_1"
+ return main_handler
+
+ monkeypatch.setattr(copilot_tools, "resolve_main_copilot_handler", _main_lookup)
+ ctx: Any = SimpleNamespace(workflow_permanent_id="wpid_1", organization_id="org_1")
+
+ handler = await copilot_tools._composition_visual_handler(ctx)
+ assert handler is main_handler
diff --git a/tests/unit/test_workflow_copilot_prompt_injection.py b/tests/unit/test_workflow_copilot_prompt_injection.py
index f76bc1bae..87ee7c9e2 100644
--- a/tests/unit/test_workflow_copilot_prompt_injection.py
+++ b/tests/unit/test_workflow_copilot_prompt_injection.py
@@ -223,7 +223,7 @@ class TestCopilotCallLLMWiring:
with (
patch(
- "skyvern.forge.sdk.routes.workflow_copilot.get_llm_handler_for_prompt_type",
+ "skyvern.forge.sdk.routes.workflow_copilot.resolve_main_copilot_handler",
return_value=mock_handler,
),
patch("skyvern.forge.sdk.routes.workflow_copilot.app") as mock_app,
diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.py
index 17368d840..494cdf342 100644
--- a/tests/unit/test_workflow_copilot_route.py
+++ b/tests/unit/test_workflow_copilot_route.py
@@ -158,7 +158,7 @@ def _setup_new_copilot_mocks(
return None
monkeypatch.setattr(
- "skyvern.forge.sdk.routes.workflow_copilot.get_llm_handler_for_prompt_type",
+ "skyvern.forge.sdk.routes.workflow_copilot.resolve_main_copilot_handler",
fake_llm_handler,
)
@@ -301,7 +301,7 @@ async def test_flag_on_pre_agent_failure_persists_recoverable_reply(monkeypatch:
return None
monkeypatch.setattr(
- "skyvern.forge.sdk.routes.workflow_copilot.get_llm_handler_for_prompt_type",
+ "skyvern.forge.sdk.routes.workflow_copilot.resolve_main_copilot_handler",
fake_llm_handler,
)
monkeypatch.setattr(