Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(mcp): share a running test selection instead of answering busy (#3727)
Swati Ahuja383f645c873a
diff --git a/packages/server/src/repowise/server/mcp_server/_test_selection.py b/packages/server/src/repowise/server/mcp_server/_test_selection.pyindex cf76f11ee..20e568a4a 100644--- a/packages/server/src/repowise/server/mcp_server/_test_selection.py+++ b/packages/server/src/repowise/server/mcp_server/_test_selection.py@@ -13,6 +13,7 @@ from __future__ import annotationsimport asyncioimport threading+from dataclasses import dataclassfrom pathlib import Pathfrom typing import Any@@ -32,10 +33,21 @@ UNAVAILABLE_REASON = ("The test selection is unavailable, so run every test (or `repowise impacted-tests`).")-#: Repository path -> the cancel flag of a selection still running there. One-#: at a time per repository: a selection past its budget keeps its threads-#: until they notice the flag, and a second one would only add to them.-_IN_FLIGHT: dict[str, threading.Event] = {}++@dataclass+class _Flight:+ """A selection still running in a repository, and how many calls await it."""++ change: Any+ task: asyncio.Future+ cancel: threading.Event+ waiters: int = 0+++#: Repository path -> the selection still running there. One at a time per+#: repository: a selection past its budget keeps its threads until they notice+#: the cancel flag, and a second one would only add to them.+_IN_FLIGHT: dict[str, _Flight] = {}class SelectionUnavailableError(Exception):@@ -53,7 +65,9 @@ async def select_change_tests(:class:`SelectionUnavailableError`.The checkout read (``git ls-files`` plus pytest's conftests and configs)- and every git call run off the event loop.+ and every git call run off the event loop. A call for the change already+ running shares that selection; a call for another change waits for it to+ end, within the same budget, then runs its own."""from repowise.core.analysis.test_selection import TestSelectionConfigfrom repowise.core.repo_config import RepoConfigError, load_repo_config@@ -63,36 +77,67 @@ async def select_change_tests(except (RepoConfigError, ValueError) as exc:raise SelectionUnavailableError("config_invalid", f"tests config is invalid: {exc}") from exckey = str(Path(repo_path).resolve())- if key in _IN_FLIGHT:- raise SelectionUnavailableError(- "busy",- "A test selection for this repository is still running; run "- "`repowise impacted-tests` for this change.",- )- cancel = _IN_FLIGHT[key] = threading.Event()- task = asyncio.ensure_future(_select(repo_path, session_factory, change, config, cancel))- # Released when the selection ends, not when the caller stops waiting.- task.add_done_callback(lambda done: _release(key, done))+ loop = asyncio.get_running_loop()+ deadline = loop.time() + SELECTION_TIMEOUT_SECONDS+ flight = await _flight_for(+ key,+ change,+ deadline,+ lambda cancel: _select(repo_path, session_factory, change, config, cancel),+ )+ # asyncio.wait never cancels the task: one caller leaving keeps it for the rest.+ flight.waiters += 1try:- done, _ = await asyncio.wait({task}, timeout=SELECTION_TIMEOUT_SECONDS)- except asyncio.CancelledError:- cancel.set()- raise+ done, _ = await asyncio.wait({flight.task}, timeout=max(deadline - loop.time(), 0))+ finally:+ flight.waiters -= 1+ if not flight.waiters and not flight.task.done():+ flight.cancel.set()if not done:- cancel.set()raise SelectionUnavailableError("timeout",f"Selecting tests took over {SELECTION_TIMEOUT_SECONDS:.0f} s; run ""`repowise impacted-tests` for this change.",)- return task.result()---def _release(key: str, task: asyncio.Future) -> None:- _IN_FLIGHT.pop(key, None)+ return flight.task.result()+++async def _flight_for(key: str, change: Any, deadline: float, start: Any) -> _Flight:+ """The selection running for *change*, or one *start* begins once any other ends."""+ loop = asyncio.get_running_loop()+ flight = _running(key)+ # Another change, or one every caller has left, is waited out, not joined.+ while flight is not None and (flight.change != change or flight.cancel.is_set()):+ done, _ = await asyncio.wait({flight.task}, timeout=deadline - loop.time())+ if not done:+ which = "another change" if flight.change != change else "this change, still stopping,"+ raise SelectionUnavailableError(+ "busy",+ f"A test selection for {which} in this repository ran past "+ f"this call's {SELECTION_TIMEOUT_SECONDS:.0f} s budget; run "+ "`repowise impacted-tests` for this change.",+ )+ flight = _running(key)+ if flight is None:+ cancel = threading.Event()+ task = asyncio.ensure_future(start(cancel))+ flight = _IN_FLIGHT[key] = _Flight(change, task, cancel)+ # Released when the selection ends, not when the callers stop waiting.+ task.add_done_callback(lambda _done, ended=flight: _release(key, ended))+ return flight+++def _running(key: str) -> _Flight | None:+ flight = _IN_FLIGHT.get(key)+ return None if flight is None or flight.task.done() else flight+++def _release(key: str, flight: _Flight) -> None:+ if _IN_FLIGHT.get(key) is flight:+ del _IN_FLIGHT[key]# An abandoned selection ends in SelectionCancelledError; nobody reads it.- if not task.cancelled():- task.exception()+ if not flight.task.cancelled():+ flight.task.exception()async def _select(diff --git a/tests/unit/server/mcp/test_test_selection_in_flight.py b/tests/unit/server/mcp/test_test_selection_in_flight.pynew file mode 100644index 000000000..40a1c23d2--- /dev/null+++ b/tests/unit/server/mcp/test_test_selection_in_flight.py@@ -0,0 +1,135 @@+"""Concurrent test selections in one repository share or queue, never fail as busy.++A second call for the change already running awaits that selection; a call for+another change waits for it to end and then runs its own. The running selection+is told to stop only when the last caller waiting on it leaves.+"""++from __future__ import annotations++import asyncio++import pytest++from repowise.core.analysis.changed_lines import ChangeSet, FileDiff+from repowise.server.mcp_server import _test_selection+++def _change(path: str) -> ChangeSet:+ return ChangeSet(+ files={path: FileDiff(path=path)}, deleted=set(), label=path, base="b", head="h"+ )+++def gated(monkeypatch):+ """``_select`` that records each run and returns only once ``gate`` opens."""+ gate = asyncio.Event()+ runs: list = []++ async def _select(_repo, _factory, change, _config, cancel):+ runs.append((change, cancel))+ await gate.wait()+ return {"for": change.label}, None, None++ monkeypatch.setattr(_test_selection, "_select", _select)+ monkeypatch.setattr(_test_selection, "_IN_FLIGHT", {})+ return gate, runs+++async def _settle() -> None:+ for _ in range(5):+ await asyncio.sleep(0)+++def _call(tmp_path, change):+ return asyncio.ensure_future(_test_selection.select_change_tests(tmp_path, None, change))+++async def test_two_calls_for_one_change_share_the_running_selection(tmp_path, gated) -> None:+ gate, runs = gated+ first, second = _call(tmp_path, _change("a.py")), _call(tmp_path, _change("a.py"))+ await _settle()+ gate.set()++ assert (await first)[0] == (await second)[0] == {"for": "a.py"}+ assert len(runs) == 1+++async def test_a_call_for_another_change_runs_after_the_running_one(tmp_path, gated) -> None:+ gate, runs = gated+ first = _call(tmp_path, _change("a.py"))+ await _settle()+ second = _call(tmp_path, _change("b.py"))+ await _settle()+ assert [change.label for change, _ in runs] == ["a.py"]+ gate.set()++ assert (await first)[0] == {"for": "a.py"}+ assert (await second)[0] == {"for": "b.py"}+ assert [change.label for change, _ in runs] == ["a.py", "b.py"]+++async def test_another_change_past_its_budget_is_busy_and_says_why(+ tmp_path, gated, monkeypatch+) -> None:+ gate, _ = gated+ first = _call(tmp_path, _change("a.py"))+ await _settle()+ monkeypatch.setattr(_test_selection, "SELECTION_TIMEOUT_SECONDS", 0.01)++ with pytest.raises(_test_selection.SelectionUnavailableError) as raised:+ await _test_selection.select_change_tests(tmp_path, None, _change("b.py"))+ assert raised.value.status == "busy"+ assert "another change" in str(raised.value)+ gate.set()+ assert (await first)[0] == {"for": "a.py"}+++async def test_one_caller_timing_out_leaves_the_selection_to_the_other(+ tmp_path, gated, monkeypatch+) -> None:+ gate, runs = gated+ monkeypatch.setattr(_test_selection, "SELECTION_TIMEOUT_SECONDS", 0.01)+ first = _call(tmp_path, _change("a.py"))+ await _settle()+ monkeypatch.setattr(_test_selection, "SELECTION_TIMEOUT_SECONDS", 30.0)+ second = _call(tmp_path, _change("a.py"))++ with pytest.raises(_test_selection.SelectionUnavailableError) as raised:+ await first+ assert raised.value.status == "timeout"+ [(_, cancel)] = runs+ assert not cancel.is_set()+ gate.set()+ assert (await second)[0] == {"for": "a.py"}+++async def test_the_selection_is_cancelled_when_its_last_caller_leaves(+ tmp_path, gated, monkeypatch+) -> None:+ gate, runs = gated+ first, second = _call(tmp_path, _change("a.py")), _call(tmp_path, _change("a.py"))+ await _settle()+ [(_, cancel)] = runs++ first.cancel()+ await _settle()+ assert not cancel.is_set()+ second.cancel()+ await _settle()+ assert cancel.is_set()+ # The same change, still stopping, is waited out and named as such.+ monkeypatch.setattr(_test_selection, "SELECTION_TIMEOUT_SECONDS", 0.01)+ with pytest.raises(_test_selection.SelectionUnavailableError) as raised:+ await _test_selection.select_change_tests(tmp_path, None, _change("a.py"))+ assert raised.value.status == "busy"+ assert "this change, still stopping" in str(raised.value)+ gate.set()+ await _settle()+ assert _test_selection._IN_FLIGHT == {}