open-swe/tests/agent/test_dispatch.py

125 lines
4 KiB
Python
Raw Normal View History

feat: port durable dispatch hardening and startup latency improvements (#160) * feat: port plan-review & workflow-approval UX (#135) Port six upstream commits onto dev: - c03a6be7 (already ported): keep plan guidance high-level - 546042a4: add workflow approval UI with diff preview, approval URLs, web review links, and polling for approval status during active runs - 216cf181: remove workflow token elevation; approved pushes pass through directly without proxy token rewriting - 3dbc0282: preserve plan redirects after login by accepting relative same-origin redirect_to values and rejecting blocked paths - bb104d93: submit plan comments with cmd+enter - 90cb6caa: terse Slack replies, shared content via save_plan outside plan mode (PLAN_STATUS_SHARED), reject shared-content mutations Refs: #135 * feat: port durable dispatch hardening and startup latency improvements Port five upstream PRs onto dev: - #1621 / #1658: durable dispatch with loopback webhook defense, create_durable_run helper, _config_with_prepare_run_id, degradation to None for relative/loopback completion webhook URLs - #1696: run-level completion webhook deduplication (replace claim-then-post with post-then-flag per run_id), DeferredErrorModel for graph-factory resilience, ToolRetryMiddleware for task subagents, TimeoutWrapupMiddleware for all three graphs - #1697: lazy-load __init__.py for agent.middleware, agent.tools, agent.dashboard (PEP 562); defer heavy imports (exa_py in web_search, agent.webapp in request_pr_review, deepagents in sandbox.py); add ttl_cache.py with stale-while-revalidate for tool loaders Refs: #137 * fix: restore login page render and clear CI lint/format The plan-review port removed the authRedirectUrl import from login.tsx but left its call site, crashing the login page at runtime (blank page, no 'Sign in to open-swe'). Pass the relative path straight to loginUrl, matching the plan route and the backend relative-redirect handling. Also drop an unused os import in the guard test and reformat workflow_push_guard.py to satisfy ruff. * fix: restore RepairOrphaned middleware export and repoint model fake to deferred_model boundary * fix: restore RepairOrphanedToolCallsMiddleware, fix E2E model-fake patch, drop dead ttl_cache - Re-add RepairOrphanedToolCallsMiddleware to the lazy middleware __init__ (_MIDDLEWARE_MODULES, __all__, TYPE_CHECKING) so agent.reviewer can import it. - Reroute E2E model patching to deferred_model.make_model so make_model_or_defer (used by all three graph factories) returns the scripted fake instead of building a real model with fake credentials. - Drop unused agent/utils/ttl_cache.py — no agent module imports it. - Fix import ordering in agent/reviewer.py and agent/analyzer.py (ruff I001). - Format tests/test_dispatch.py. * fix: claim-then-post run-level failure dedup; stop permanent suppression --------- Co-authored-by: amoussa1229 <166072409+amoussa1229@users.noreply.github.com> Co-authored-by: Adam Moussa <adam@seahavenind.com>
2026-07-09 17:11:25 -04:00
from __future__ import annotations
import importlib
from typing import Any
import pytest
dispatch = importlib.import_module("agent.dispatch")
_ABSOLUTE = "https://open-swe-v3-abc.us.langgraph.app/webhooks/run-complete"
def test_is_loopback_webhook_relative() -> None:
assert dispatch._is_loopback_webhook("/webhooks/run-complete") is True
def test_is_loopback_webhook_localhost() -> None:
assert dispatch._is_loopback_webhook("http://localhost:2024/webhooks/run-complete") is True
assert dispatch._is_loopback_webhook("http://127.0.0.1:8000/webhooks/run-complete") is True
def test_is_loopback_webhook_absolute() -> None:
assert dispatch._is_loopback_webhook(_ABSOLUTE) is False
def test_resolve_no_secret_attaches_nothing() -> None:
assert dispatch._resolve_completion_webhook_url(_ABSOLUTE, None) is None
assert dispatch._resolve_completion_webhook_url(_ABSOLUTE, "") is None
def test_resolve_relative_url_degrades_to_none() -> None:
# Secret set but a loopback URL would 422 every run — attach nothing instead.
assert dispatch._resolve_completion_webhook_url("/webhooks/run-complete", "s3cret") is None
def test_resolve_localhost_url_degrades_to_none() -> None:
assert dispatch._resolve_completion_webhook_url("http://localhost/x", "s3cret") is None
def test_resolve_absolute_url_appends_token() -> None:
assert (
dispatch._resolve_completion_webhook_url(_ABSOLUTE, "s3cret") == f"{_ABSOLUTE}?token=s3cret"
)
def test_resolve_absolute_url_with_existing_query_left_as_is() -> None:
url = f"{_ABSOLUTE}?token=preset"
assert dispatch._resolve_completion_webhook_url(url, "s3cret") == url
class _FakeRuns:
def __init__(self) -> None:
self.created: list[dict[str, Any]] = []
async def create(self, thread_id: str, assistant_id: str, **kwargs: Any) -> dict[str, str]:
self.created.append({"thread_id": thread_id, "assistant_id": assistant_id, **kwargs})
return {"run_id": "run-1"}
class _FakeClient:
def __init__(self) -> None:
self.runs = _FakeRuns()
@pytest.mark.asyncio
async def test_create_durable_run_applies_defaults(monkeypatch: pytest.MonkeyPatch) -> None:
client = _FakeClient()
monkeypatch.setattr(dispatch, "COMPLETION_WEBHOOK_URL", "https://app/webhooks/run-complete")
run = await dispatch.create_durable_run(
"thread-1",
"agent",
input={"messages": [{"role": "user", "content": "hi"}]},
source="test",
config={"configurable": {"thread_id": "thread-1"}, "metadata": {"kind": "test"}},
client=client,
)
assert run == {"run_id": "run-1"}
created = client.runs.created[0]
assert created["durability"] == "sync"
assert created["multitask_strategy"] == "interrupt"
assert created["if_not_exists"] == "create"
assert created["webhook"] == "https://app/webhooks/run-complete"
assert created["config"]["metadata"] == {"kind": "test"}
assert created["config"]["configurable"]["thread_id"] == "thread-1"
assert isinstance(created["config"]["configurable"]["prepare_run_id"], str)
@pytest.mark.asyncio
async def test_create_durable_run_no_webhook_when_none(monkeypatch: pytest.MonkeyPatch) -> None:
client = _FakeClient()
monkeypatch.setattr(dispatch, "COMPLETION_WEBHOOK_URL", None)
await dispatch.create_durable_run(
"thread-1",
"agent",
input={"messages": []},
source="test",
client=client,
)
created = client.runs.created[0]
assert "webhook" not in created
@pytest.mark.asyncio
async def test_dispatch_agent_run_preserves_multitask_strategy(
monkeypatch: pytest.MonkeyPatch,
) -> None:
client = _FakeClient()
monkeypatch.setattr(dispatch, "COMPLETION_WEBHOOK_URL", None)
await dispatch.dispatch_agent_run(
"thread-1",
"hi",
{"key": "val"},
source="test",
client=client,
multitask_strategy="reject",
)
created = client.runs.created[0]
assert created["multitask_strategy"] == "reject"