Skip to content

The agent that grows with you

test: trim the ended-flow re-mint coverage to two invariants

teknium11d0c580619af
diff --git a/tests/tools/test_mcp_dashboard_oauth.py b/tests/tools/test_mcp_dashboard_oauth.py
index 0b052f9fa8a..60ac433563b 100644
--- a/tests/tools/test_mcp_dashboard_oauth.py
+++ b/tests/tools/test_mcp_dashboard_oauth.py
@@ -226,125 +226,6 @@ def test_preregistered_pinned_redirect_port_keeps_loopback_listener_under_dashbo
assert (result.code, result.state) == ("code-5", "state-5")
-def test_ended_flow_is_reopened_for_the_retry_instead_of_raising():
- """An ended handle must be re-minted for the next authorization attempt, not raise.
-
- The MCP server task inherits this flow for its whole life (``_park`` re-establishes nothing:
- the ContextVar stays set), so a stale handle raised ``RuntimeError: OAuth flow already
- ended`` out of the SDK's auth flow on every retry and parked the server for good.
- """
- flow = _flow()
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=stale"))
- flow.deliver_callback(code="spent-code", state="stale", error=None)
- assert asyncio.run(flow.wait_for_callback()) == ("spent-code", "stale", None)
- flow.mark_approved()
-
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=fresh"))
-
- snapshot = flow.snapshot()
- assert snapshot["status"] == "authorization_required"
- assert snapshot["authorization_url"] == "https://idp.example/authorize?state=fresh"
- assert flow.expected_state == "fresh"
- # the ended attempt's spent authorization code must never be replayed to the new attempt
- with pytest.raises(TimeoutError):
- asyncio.run(flow.wait_for_callback(timeout=0.05))
- flow.deliver_callback(code="fresh-code", state="fresh", error=None)
- assert asyncio.run(flow.wait_for_callback()) == ("fresh-code", "fresh", None)
-
-
-def test_failed_attempt_reopens_the_same_handle():
- """An attempt that failed (not cancelled) is recoverable too — the desktop keeps polling the
- same flow id, so re-minting in place is what lets it show a working URL again."""
- flow = _flow("flow-failed")
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=one"))
- flow.mark_error("connection dropped mid-consent")
-
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=two"))
-
- assert flow.snapshot() == {
- "flow_id": "flow-failed",
- "server_name": "asana",
- "status": "authorization_required",
- "authorization_url": "https://idp.example/authorize?state=two",
- "error": None,
- }
-
-
-def test_first_publish_is_unchanged_and_a_repeat_does_not_reopen():
- """Protection: the ordinary first flow behaves exactly as before, and a repeat of the same URL
- (the SDK re-invoking the handler) neither reopens the flow nor pushes a second URL."""
- flow = _flow("flow-first")
- url = "https://idp.example/authorize?state=s1"
- expected = {
- "flow_id": "flow-first",
- "server_name": "asana",
- "status": "authorization_required",
- "authorization_url": url,
- "error": None,
- }
-
- asyncio.run(flow.publish_authorization_url(url))
- assert flow.snapshot() == expected
-
- asyncio.run(flow.publish_authorization_url(url))
- assert flow.snapshot() == expected
- assert flow.expected_state == "s1"
- # nothing was re-minted, so no callback slot was opened/wiped behind the waiting worker
- assert not flow._callback_ready.is_set()
- # and the flow is still the one the browser callback completes
- flow.deliver_callback(code="code-first", state="s1", error=None)
- assert asyncio.run(flow.wait_for_callback()) == ("code-first", "s1", None)
-
-
-def test_concurrent_reopens_leave_one_live_url_and_state_pair():
- """Re-minting is serialized on the flow lock: concurrent publishes leave ONE live (url, state)
- pair — never two active flows — and the callback slot still belongs to that pair."""
- flow = _flow("flow-race-reopen")
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=ended"))
- flow.mark_error("attempt 1 failed")
-
- start = threading.Barrier(3)
- errors: list = []
-
- def publish(state: str) -> None:
- start.wait()
- try:
- asyncio.run(
- flow.publish_authorization_url(f"https://idp.example/authorize?state={state}")
- )
- except Exception as exc: # noqa: BLE001 — reported, not swallowed
- errors.append(exc)
-
- workers = [threading.Thread(target=publish, args=(state,)) for state in ("a", "b")]
- for worker in workers:
- worker.start()
- start.wait()
- for worker in workers:
- worker.join()
-
- assert errors == []
- assert flow.expected_state in {"a", "b"}
- assert flow.snapshot()["status"] == "authorization_required"
- assert flow.snapshot()["authorization_url"] == (
- f"https://idp.example/authorize?state={flow.expected_state}"
- )
- flow.deliver_callback(code="code-race", state=flow.expected_state, error=None)
- assert asyncio.run(flow.wait_for_callback())[0] == "code-race"
-
-
-def test_cancelled_flow_stays_terminal():
- """A user cancellation is not "ended by accident": the retrying worker must not reopen it
- (the per-server slot has to free promptly, and the user said no)."""
- flow = _flow("flow-cancelled")
- flow.mark_error("Cancelled by user", cancelled=True)
-
- with pytest.raises(RuntimeError, match="cancelled"):
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=late"))
-
- assert flow.snapshot()["status"] == "error"
- assert flow.expected_state is None
-
-
@pytest.mark.no_isolate
@pytest.mark.parametrize("ended_by", ["approved", "error"])
def test_server_task_does_not_park_on_an_ended_dashboard_flow(
diff --git a/tests/tui_gateway/test_mcp_oauth_cancel.py b/tests/tui_gateway/test_mcp_oauth_cancel.py
index 3ffa61a7829..0fd7918f0e3 100644
--- a/tests/tui_gateway/test_mcp_oauth_cancel.py
+++ b/tests/tui_gateway/test_mcp_oauth_cancel.py
@@ -141,30 +141,3 @@ def test_cancel_does_not_revoke_an_approved_flow(tmp_path, monkeypatch):
"status": "approved",
}
assert sessions.cancel_flow("missing", "reports", home)["ok"] is False
-
-
-def test_cancelled_session_is_not_reopened_by_a_retrying_worker(tmp_path, monkeypatch):
- """A cancel is terminal-for-good: the worker's next authorization attempt must fail instead of
- silently re-minting the flow the user abandoned (and holding the per-server slot again)."""
- home = str(tmp_path)
- flow = DashboardOAuthFlow(
- "cancel-then-retry", "reports", None, home, "http://127.0.0.1:49152/callback"
- )
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=first"))
- monkeypatch.setattr(
- sessions,
- "_sessions",
- {
- "cancel-then-retry": {
- "flow": flow,
- "server_name": "reports",
- "hermes_home": home,
- "httpd": None,
- }
- },
- )
-
- assert sessions.cancel_flow("cancel-then-retry", "reports", home)["ok"] is True
- with pytest.raises(RuntimeError, match="cancelled"):
- asyncio.run(flow.publish_authorization_url("https://idp.example/authorize?state=second"))
- assert flow.snapshot()["status"] == "error"