From e40dc0e36e9fcd209683784c51d44acdcfb33072 Mon Sep 17 00:00:00 2001 From: Zejiang Guo Date: Mon, 19 Oct 2026 23:51:50 +0000 Subject: [PATCH] fix(dashboard): key the path-probe stall gate for callers that do not lead with the path --- docs/system-specs/modules/security.md | 8 + src/kiro_crew/dashboard/file_api/desktop.py | 2 +- src/kiro_crew/dashboard/file_api/editor.py | 8 +- src/kiro_crew/dashboard/file_api/grep.py | 4 +- .../dashboard/file_api/office_preview.py | 9 +- .../dashboard/file_api/path_probe.py | 13 +- src/kiro_crew/dashboard/file_api/pinned_io.py | 33 +++- src/kiro_crew/dashboard/file_api/search.py | 14 +- src/kiro_crew/dashboard/file_api/sheet.py | 2 +- src/kiro_crew/dashboard/file_api/transfer.py | 8 +- src/kiro_crew/dashboard/handlers/files.py | 2 + .../dashboard/handlers/office_slides.py | 6 +- src/kiro_crew/dashboard/handlers/terminal.py | 4 +- ...st_dashboard_files_composition_contract.py | 3 +- test/test_dashboard_files_onloop_fs.py | 153 +++++++++++++++++- test/test_terminal_handler.py | 45 ++++++ 16 files changed, 297 insertions(+), 17 deletions(-) diff --git a/docs/system-specs/modules/security.md b/docs/system-specs/modules/security.md index b8d56b733ad6..465df4bb1798 100644 --- a/docs/system-specs/modules/security.md +++ b/docs/system-specs/modules/security.md @@ -1167,6 +1167,14 @@ never one) this gate keys one component deeper, so `/home//mnt/nas/x` keys to `/home//mnt` and a dead FUSE or sshfs mount inside the home does not refuse `/home//workspace`; the home itself keeps its own key. The rule is lexical, and the resolver's cooldown keeps the plain `_stall_prefix` key. +A caller whose path is not the first argument names it with `stall_path=`, +keyed by the same rule (`""` means no key): the reveal-and-open closure passes +its path, the terminal passes the requested cwd, and the project-relative +resolve passes `_project_relative_stall_path` (the project directory for a +relative path, the path itself otherwise). Callers that touch several roots +(`/api/file-kinds`, the unscoped file-search fallback) use `_batch_stall_path`: +keyed only when every path shares one key, because a key on one of several +mounts would let a dead mount refuse a healthy one. While `_PATH_PROBE_STUCK_TO_REFUSE` (2) probe-pool calls under that prefix have each run longer than `_PATH_PROBE_STALL_SECS` (2 s), any new call under it, probe or transfer, gets the same `503 path_probe_busy` diff --git a/src/kiro_crew/dashboard/file_api/desktop.py b/src/kiro_crew/dashboard/file_api/desktop.py index e5000be33955..c31457e31761 100644 --- a/src/kiro_crew/dashboard/file_api/desktop.py +++ b/src/kiro_crew/dashboard/file_api/desktop.py @@ -102,7 +102,7 @@ def _stat_then_launch() -> tuple[bool, bool]: return (True, platform_compat.open_with_default_app(path)) try: - is_regular_file, launched = await _run_path_probe(_stat_then_launch) + is_regular_file, launched = await _run_path_probe(_stat_then_launch, stall_path=path) except _PathProbeBusy: return _probe_busy_response( resource=path, tool_name="reveal_path", session_key="api", source="api" diff --git a/src/kiro_crew/dashboard/file_api/editor.py b/src/kiro_crew/dashboard/file_api/editor.py index 63ca9408c0d1..ab62e6c34e12 100644 --- a/src/kiro_crew/dashboard/file_api/editor.py +++ b/src/kiro_crew/dashboard/file_api/editor.py @@ -32,6 +32,7 @@ _PathProbeBusy, _probe_busy_response, _probe_request_path, + _project_relative_stall_path, _read_request_path, _resolve_project_relative, _run_path_probe, @@ -81,7 +82,11 @@ async def api_file_read(request: web.Request) -> web.Response: # the resolved string, so it cannot be folded into the probe. if request.query.get("resolve") == "1": try: - raw_path, _resolve_err = await _run_path_probe(_resolve_project_relative, raw_path) + raw_path, _resolve_err = await _run_path_probe( + _resolve_project_relative, + raw_path, + stall_path=_project_relative_stall_path(raw_path), + ) except _PathProbeBusy: return _probe_busy_response(resource=raw_path, tool_name="file_read") if _resolve_err == "cannot_resolve": @@ -275,6 +280,7 @@ async def api_file_raw(request: web.Request) -> web.Response: max_bytes=_MAX_UPLOAD_BYTES, ), transfer=True, + stall_path=request.query.get("path", ""), ) except _PathProbeBusy: return _probe_busy_response(resource=request.query.get("path", ""), tool_name="file_raw") diff --git a/src/kiro_crew/dashboard/file_api/grep.py b/src/kiro_crew/dashboard/file_api/grep.py index d68fb677346d..a48715f409a3 100644 --- a/src/kiro_crew/dashboard/file_api/grep.py +++ b/src/kiro_crew/dashboard/file_api/grep.py @@ -802,7 +802,9 @@ def _search() -> tuple[list[dict], bool, str, int]: return results + doc_hits, truncated or doc_truncated, engine, skipped try: - results, truncated, engine, skipped_docs = await _run_path_probe(_search, transfer=True) + results, truncated, engine, skipped_docs = await _run_path_probe( + _search, transfer=True, stall_path=root + ) except _PathProbeBusy: return _probe_busy_response( resource=f"q={logged_query} root={root}", operation="file_grep", caller=caller diff --git a/src/kiro_crew/dashboard/file_api/office_preview.py b/src/kiro_crew/dashboard/file_api/office_preview.py index 7b858bfff024..245be898c6fe 100644 --- a/src/kiro_crew/dashboard/file_api/office_preview.py +++ b/src/kiro_crew/dashboard/file_api/office_preview.py @@ -20,6 +20,7 @@ _OpenDenied, _PathProbeBusy, _probe_busy_response, + _project_relative_stall_path, _resolve_project_relative, _run_path_probe, _sel, @@ -197,7 +198,11 @@ def _log(outcome: str, res: str, error: str = "") -> None: # Off-loop: the resolution is a pair of realpath calls. if request.query.get("resolve") == "1": try: - raw_path, _resolve_err = await _run_path_probe(_resolve_project_relative, raw_path) + raw_path, _resolve_err = await _run_path_probe( + _resolve_project_relative, + raw_path, + stall_path=_project_relative_stall_path(raw_path), + ) except _PathProbeBusy: return _probe_busy_response(resource=raw_path, tool_name="file_office_preview") if _resolve_err == "cannot_resolve": @@ -340,7 +345,7 @@ def _open_and_extract() -> dict[str, object] | _OpenDenied: return payload try: - result = await _run_path_probe(_open_and_extract, transfer=True) + result = await _run_path_probe(_open_and_extract, transfer=True, stall_path=raw_path) except asyncio.CancelledError: # Gateway shutdown / client disconnect while the worker thread is # parsing: the access attempt already happened, so record it before diff --git a/src/kiro_crew/dashboard/file_api/path_probe.py b/src/kiro_crew/dashboard/file_api/path_probe.py index 5a0badff574a..9b7e659c0123 100644 --- a/src/kiro_crew/dashboard/file_api/path_probe.py +++ b/src/kiro_crew/dashboard/file_api/path_probe.py @@ -172,7 +172,11 @@ def _tracked_path_probe(key: str, fn: Callable[..., _ProbeT], /, *args: object) async def _run_path_probe( - fn: Callable[..., _ProbeT], /, *args: object, transfer: bool = False + fn: Callable[..., _ProbeT], + /, + *args: object, + transfer: bool = False, + stall_path: str | None = None, ) -> _ProbeT: """Run a blocking request-path call on a dedicated bounded pool, or refuse. @@ -215,8 +219,13 @@ async def _run_path_probe( served. Probes that arrive together, before any of them is past the budget, are still bounded only by the pool. A transfer naming a stuck prefix is refused the same way, but a transfer never marks a prefix stuck: it legitimately runs long. + + ``stall_path`` names the path the call touches when it is not the first + argument: a closure, a config-first helper, a root the caller computed. It + is keyed by the same lexical rule, and ``""`` means no key. Without it the + first argument is the key, as before. """ - key = _path_probe_stall_key(args) + key = _path_probe_stall_key(args if stall_path is None else (stall_path,)) if key and _path_probe_prefix_wedged(key): raise _PathProbeBusy() pool = executors.path_transfer_executor() if transfer else executors.path_probe_executor() diff --git a/src/kiro_crew/dashboard/file_api/pinned_io.py b/src/kiro_crew/dashboard/file_api/pinned_io.py index 9e0b16320632..b5ec3c2e1f7d 100644 --- a/src/kiro_crew/dashboard/file_api/pinned_io.py +++ b/src/kiro_crew/dashboard/file_api/pinned_io.py @@ -17,6 +17,7 @@ _FILE_KINDS_MAX_PATHS, _FILE_READ_BINARY_EXTS, _FILE_READ_SNIFF_BYTES, + _path_probe_stall_key, _PathProbeBusy, _probe_busy_response, _probe_request_path, @@ -412,6 +413,34 @@ def _resolve_project_relative(raw: str) -> tuple[str, str | None]: return candidate, None +def _project_relative_stall_path(raw: str) -> str: + """The path :func:`_resolve_project_relative` touches for *raw*, for the probe's stall key. + + A path it passes through unchanged is its own key, as before. A relative + one is resolved inside the project directory, so it is charged to that + directory: two resolves stuck on a dead project mount then refuse the next + one. ``""`` (no key) when no project directory is set, since that answer + touches no filesystem. Lexical, so it runs on the event loop. + """ + if not raw or raw.startswith(("/", "~", "\\")) or ntpath.splitdrive(raw)[0]: + return raw + return os.environ.get("KIROCREW_PROJECT_DIR", "") + + +def _batch_stall_path(raws: list[str]) -> str: + """The stall path for :func:`_classify_request_paths` over *raws*, or ``""``. + + The batch is keyed only when every path in it falls under ONE stall key; + then that key is exactly the mount the hop can wedge on. A batch spanning + several mounts gets no key: keying it on any one path (the first, say) + would let two batches stuck on a dead mount listed third refuse a healthy + mount listed first, on every route. Such a batch stays bounded by the pool. + """ + paths = [_project_relative_stall_path(raw) for raw in raws] + keys = {_path_probe_stall_key((p,)) for p in paths} + return paths[0] if len(keys) == 1 else "" + + def _classify_request_paths(raws: list[str]) -> list[tuple[str, str, str]]: """Classify each request path exactly as ``HEAD /api/file-read?resolve=1`` would. @@ -489,7 +518,9 @@ async def api_file_kinds(request: web.Request) -> web.Response: if not unique: return web.json_response({"kinds": {}}) try: - verdicts = await _run_path_probe(_classify_request_paths, unique) + verdicts = await _run_path_probe( + _classify_request_paths, unique, stall_path=_batch_stall_path(unique) + ) except _PathProbeBusy: return _probe_busy_response(resource=f"{len(unique)} paths", tool_name="file_read") sel = _sel() diff --git a/src/kiro_crew/dashboard/file_api/search.py b/src/kiro_crew/dashboard/file_api/search.py index eac8d3efaf5c..f8f5b89d980f 100644 --- a/src/kiro_crew/dashboard/file_api/search.py +++ b/src/kiro_crew/dashboard/file_api/search.py @@ -19,6 +19,7 @@ _WALK_SKIP_DIRS, _WALK_TIME_BUDGET_SECS, DashboardState, + _batch_stall_path, _PathProbeBusy, _probe_busy_response, _run_path_probe, @@ -264,9 +265,14 @@ def _probe_fallback_roots() -> tuple[bool, bool]: return bool(proj) and os.path.isdir(proj), os.path.isdir(mc_workspace) # Off-loop: two isdir probes on operator-configured paths, either of - # which may sit on a network mount. + # which may sit on a network mount. Keyed only when both roots share + # one mount, the batch rule: a key on either root alone would let a + # dead workspace mount refuse a healthy project dir, or the reverse. try: - proj_is_dir, workspace_is_dir = await _run_path_probe(_probe_fallback_roots) + proj_is_dir, workspace_is_dir = await _run_path_probe( + _probe_fallback_roots, + stall_path=_batch_stall_path([r for r in (proj, mc_workspace) if r]), + ) except _PathProbeBusy: return _probe_busy_response(resource=proj, operation="file_search", caller=caller) if proj_is_dir: @@ -457,7 +463,9 @@ def _collect(kind: str, dirpath: str, names: list[str], root_dir: str) -> None: # probe slot too: a walk into a dead mount would otherwise pin a # default-executor worker exactly as an unbounded stat does. try: - results = await _run_path_probe(_walk_file_search, transfer=True) + results = await _run_path_probe( + _walk_file_search, transfer=True, stall_path=_batch_stall_path(safe_roots) + ) except _PathProbeBusy: return _probe_busy_response(resource=f"q={query}", operation="file_search", caller=caller) diff --git a/src/kiro_crew/dashboard/file_api/sheet.py b/src/kiro_crew/dashboard/file_api/sheet.py index c025b8f90d8b..c2ab89756a23 100644 --- a/src/kiro_crew/dashboard/file_api/sheet.py +++ b/src/kiro_crew/dashboard/file_api/sheet.py @@ -303,7 +303,7 @@ def _open_and_load() -> dict | _OpenDenied: return _load_sheet_payload(checked.file, max_bytes=_MAX_UPLOAD_BYTES) try: - result = await _run_path_probe(_open_and_load, transfer=True) + result = await _run_path_probe(_open_and_load, transfer=True, stall_path=raw_path) except asyncio.CancelledError: # Shutdown or client disconnect: the access attempt must not vanish # from the audit trail. No resource handling here -- the worker diff --git a/src/kiro_crew/dashboard/file_api/transfer.py b/src/kiro_crew/dashboard/file_api/transfer.py index 0ab90d53d9ac..e00c1e1eb66c 100644 --- a/src/kiro_crew/dashboard/file_api/transfer.py +++ b/src/kiro_crew/dashboard/file_api/transfer.py @@ -32,6 +32,7 @@ _PathProbeBusy, _probe_busy_response, _probe_request_path, + _project_relative_stall_path, _resolve_project_relative, _run_path_probe, _sel, @@ -208,7 +209,11 @@ async def api_file_download(request: web.Request) -> web.Response: # api_file_read). Off-loop: the resolution is a pair of realpath calls. if request.query.get("resolve") == "1": try: - raw_path, _resolve_err = await _run_path_probe(_resolve_project_relative, raw_path) + raw_path, _resolve_err = await _run_path_probe( + _resolve_project_relative, + raw_path, + stall_path=_project_relative_stall_path(raw_path), + ) except _PathProbeBusy: return _probe_busy_response(resource=raw_path, tool_name="file_download") if _resolve_err == "cannot_resolve": @@ -243,6 +248,7 @@ async def api_file_download(request: web.Request) -> web.Response: _open_checked, raw_path, tool_name="file_download", max_bytes=_MAX_UPLOAD_BYTES ), transfer=True, + stall_path=raw_path, ) except _PathProbeBusy: return _probe_busy_response(resource=raw_path, tool_name="file_download") diff --git a/src/kiro_crew/dashboard/handlers/files.py b/src/kiro_crew/dashboard/handlers/files.py index 4a0c2c63eca1..8c8bcf4967ae 100644 --- a/src/kiro_crew/dashboard/handlers/files.py +++ b/src/kiro_crew/dashboard/handlers/files.py @@ -202,6 +202,7 @@ _validate_dashboard_path, ) from kiro_crew.dashboard.file_api.pinned_io import ( # noqa: F401 + _batch_stall_path, _CheckedFile, _classify_request_paths, _file_write_blocking, @@ -211,6 +212,7 @@ _OpenDenied, _OpenedFile, _OpenRefusal, + _project_relative_stall_path, _read_request_path, _resolve_project_relative, _TextRead, diff --git a/src/kiro_crew/dashboard/handlers/office_slides.py b/src/kiro_crew/dashboard/handlers/office_slides.py index a0648b5c2478..58aa07f32a96 100644 --- a/src/kiro_crew/dashboard/handlers/office_slides.py +++ b/src/kiro_crew/dashboard/handlers/office_slides.py @@ -1301,7 +1301,11 @@ async def _resolve_path(request: web.Request, tool_name: str) -> tuple[str, web. raw_path = request.query.get("path", "") if request.query.get("resolve") == "1": try: - raw_path, err = await _files._run_path_probe(_files._resolve_project_relative, raw_path) + raw_path, err = await _files._run_path_probe( + _files._resolve_project_relative, + raw_path, + stall_path=_files._project_relative_stall_path(raw_path), + ) except _files._PathProbeBusy: return raw_path, _files._probe_busy_response(resource=raw_path, tool_name=tool_name) if err == "cannot_resolve": diff --git a/src/kiro_crew/dashboard/handlers/terminal.py b/src/kiro_crew/dashboard/handlers/terminal.py index aeecae61d257..16f84bc6a9d6 100644 --- a/src/kiro_crew/dashboard/handlers/terminal.py +++ b/src/kiro_crew/dashboard/handlers/terminal.py @@ -595,7 +595,9 @@ async def api_terminal_ws(request: web.Request) -> web.WebSocketResponse | web.R requested_cwd = request.query.get("cwd") if requested_cwd: cwd = await asyncio.wait_for( - _run_path_probe(_resolve_cwd, cfg, requested_cwd), + _run_path_probe( + _resolve_cwd, cfg, requested_cwd, stall_path=requested_cwd + ), timeout=_TERMINAL_CWD_TIMEOUT_S, ) else: diff --git a/test/test_dashboard_files_composition_contract.py b/test/test_dashboard_files_composition_contract.py index 23e749eb41df..c4bd49360766 100644 --- a/test/test_dashboard_files_composition_contract.py +++ b/test/test_dashboard_files_composition_contract.py @@ -255,7 +255,8 @@ #: project-tree walk replaced the ``project_tree`` helpers. #: Recaptured with the ``path_probe``, ``outbox``, ``channel_upload``, ``desktop`` and #: ``editor`` owners, from the base file, where each of their names had the same shape. -_BASE_SHAPE_DIGEST = "65c4f9e11ec674f56489bb76f0c456811a262047983e711248ab01ad828beeca" +#: Recaptured when ``_run_path_probe`` gained its keyword-only ``stall_path``. +_BASE_SHAPE_DIGEST = "00b01a4e5eb247df7c73163e04a56f9bf00495f64187cd93a253fc958c3a9d8e" #: Definitions that stay in the facade file: the seams the owners share -- the SEL #: accessor, the file-send audit, the sub-agent session mapping and the request-body #: error code -- the search walk's clock, the file viewer's owner-view verdict, whose diff --git a/test/test_dashboard_files_onloop_fs.py b/test/test_dashboard_files_onloop_fs.py index 9ddc3c9670c1..6454da44a810 100644 --- a/test/test_dashboard_files_onloop_fs.py +++ b/test/test_dashboard_files_onloop_fs.py @@ -30,6 +30,7 @@ import ast import asyncio +import contextlib import errno import inspect import json @@ -50,7 +51,7 @@ from kiro_crew import executors from kiro_crew.dashboard import file_api from kiro_crew.dashboard.handlers import files as f -from kiro_crew.testing.wait import async_wait_until +from kiro_crew.testing.wait import async_wait_until, await_until #: The shared open-and-check prefix resolves the validator through the #: ``handlers`` package at call time -- a documented monkey-patch seam kept for @@ -1272,6 +1273,156 @@ def stall_prefix(expanded: str) -> str: pool.shutdown(wait=True) +@contextlib.asynccontextmanager +async def _stuck_under(monkeypatch: pytest.MonkeyPatch, path: str): + """Park two probes under the stall key of *path*, on a three-worker pool. + + Every running probe counts as stuck (``_PATH_PROBE_STALL_SECS`` is zero), so + the stall is set by the test, not by a clock. The third worker stays free, so + a refusal that follows is the stall gate's and not the pool's capacity. An + ``async with`` helper, not a fixture, so the exit can await the parked + probes under a named bound before the pool is shut down. + """ + pool = ThreadPoolExecutor(max_workers=3, thread_name_prefix=_PROBE_THREAD_PREFIX) + monkeypatch.setattr(f, "_PATH_PROBE_ADMIT_TIMEOUT_SECS", 0.2) + monkeypatch.setattr(f, "_PATH_PROBE_STALL_SECS", 0.0) + monkeypatch.setattr(executors, "path_probe_executor", lambda: pool) + monkeypatch.setattr(executors, "path_transfer_executor", lambda: pool) + release = threading.Event() + wedged: list[asyncio.Future] = [] + try: + key = f._path_probe_stall_key((path,)) + assert key, path + for name in ("a", "b"): + entered = threading.Event() + wedged.append( + asyncio.ensure_future( + f._run_path_probe(_wedge_on, os.path.join(path, name), entered, release) + ) + ) + await _until_entered(entered) + assert f._path_probe_prefix_wedged(key) + yield key + finally: + release.set() + if wedged: + await await_until(asyncio.gather(*wedged), what="the parked probes settling") + pool.shutdown(wait=True) # every worker has returned: the join is immediate + + +async def _third_worker_is_free() -> None: + """The pool still serves a call under no stuck key, so the refusal was the gate.""" + assert await f._run_path_probe(lambda: "free") == "free" + + +class TestStallKeyForCallersThatDoNotLeadWithThePath: + """Callers that hand the probe a closure, a list or a config first name their + path with ``stall_path``, so two probes stuck under it refuse the third.""" + + @pytest.mark.asyncio + async def test_reveal_open_is_refused_under_its_stuck_path( + self, a_file: Path, monkeypatch: pytest.MonkeyPatch + ): + launch = mock.MagicMock(return_value=False) + monkeypatch.setattr(f.platform_compat, "open_with_default_app", launch) + monkeypatch.setattr(f, "is_direct_local_request", lambda request: True) + req = make_mocked_request("POST", "/api/reveal", app=_app()) + req["user"] = "test-user" + body = {"path": str(a_file), "action": "open"} + async with _stuck_under(monkeypatch, str(a_file.parent)): + with mock.patch.object( + f, "read_bounded_json", mock.AsyncMock(return_value=(body, None)) + ): + resp = await f.api_reveal_path(req) + + assert resp.status == 503, resp.text + assert _body(resp)["code"] == "path_probe_busy" + launch.assert_not_called() + await _third_worker_is_free() + + @pytest.mark.asyncio + async def test_fallback_search_is_refused_under_its_stuck_roots( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ): + monkeypatch.setenv("KIROCREW_PROJECT_DIR", str(tmp_path / "proj")) + monkeypatch.setattr(f, "data_home", lambda: tmp_path) + async with _stuck_under(monkeypatch, str(tmp_path)): + resp = await f.api_file_search(_req("/api/file-search", "q=note")) + + assert resp.status == 503, resp.text + assert _body(resp)["code"] == "path_probe_busy" + await _third_worker_is_free() + + @pytest.mark.asyncio + async def test_file_kinds_is_refused_when_its_batch_is_under_a_stuck_key( + self, a_file: Path, monkeypatch: pytest.MonkeyPatch + ): + monkeypatch.setenv("KIROCREW_PROJECT_DIR", str(a_file.parent)) + req = make_mocked_request("POST", "/api/file-kinds", app=_app()) + req["user"] = "local-app" + req["app"] = "" + body = {"paths": [str(a_file), "note.md"]} + async with _stuck_under(monkeypatch, str(a_file.parent)): + with mock.patch.object( + f, "read_bounded_json", mock.AsyncMock(return_value=(body, None)) + ): + resp = await f.api_file_kinds(req) + + assert resp.status == 503, resp.text + assert _body(resp)["code"] == "path_probe_busy" + await _third_worker_is_free() + + @pytest.mark.asyncio + async def test_a_relative_path_is_refused_under_its_stuck_project_dir( + self, a_file: Path, monkeypatch: pytest.MonkeyPatch + ): + """The resolve itself is refused, before it realpaths inside the dead project.""" + monkeypatch.setenv("KIROCREW_PROJECT_DIR", str(a_file.parent)) + resolve = mock.MagicMock(side_effect=f._resolve_project_relative) + monkeypatch.setattr(f, "_resolve_project_relative", resolve) + async with _stuck_under(monkeypatch, str(a_file.parent)): + resp = await f.api_file_read(_req("/api/file-read", "path=note.md&resolve=1")) + + assert resp.status == 503, resp.text + assert _body(resp)["code"] == "path_probe_busy" + resolve.assert_not_called() + await _third_worker_is_free() + + @pytest.mark.parametrize( + "route", + ["file_raw", "file_download", "file_office_preview", "file_sheet"], + ) + @pytest.mark.asyncio + async def test_a_transfer_that_opens_the_path_itself_is_refused_under_its_key( + self, a_file: Path, monkeypatch: pytest.MonkeyPatch, route: str + ): + """These routes validate and open inside ONE transfer closure, with no keyed + probe before it, so the transfer carries the path's key itself.""" + handler = getattr(f, f"api_{route}") + async with _stuck_under(monkeypatch, str(a_file.parent)): + resp = await handler(_req(f"/api/{route.replace('_', '-')}", f"path={a_file}")) + + assert resp.status == 503, (route, resp.status, resp.text) + assert _body(resp)["code"] == "path_probe_busy" + await _third_worker_is_free() + + def test_a_batch_is_keyed_only_when_every_path_shares_one_key( + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch + ): + """A batch across mounts gets no key: keyed on one path, two batches stuck on + a dead mount listed later would refuse the healthy mount listed first.""" + monkeypatch.setenv("KIROCREW_PROJECT_DIR", str(tmp_path)) + here = str(tmp_path / "x") + other = os.path.join(os.sep, "kc-other-mount", "share", "y") + assert f._path_probe_stall_key((here,)) != f._path_probe_stall_key((other,)) + assert f._batch_stall_path([here, "rel.md"]) == here + assert f._batch_stall_path([here, other]) == "" + assert f._batch_stall_path(["rel.md"]) == str(tmp_path) + monkeypatch.delenv("KIROCREW_PROJECT_DIR") + assert f._project_relative_stall_path("rel.md") == "" + assert f._project_relative_stall_path(here) == here + + class TestExecutorPools: def test_probe_and_transfer_pools_are_distinct_bounded_and_named(self): """Two pools, not one: a burst of large transfers must queue behind diff --git a/test/test_terminal_handler.py b/test/test_terminal_handler.py index df0e312da6f2..a0b1b9d3c077 100644 --- a/test/test_terminal_handler.py +++ b/test/test_terminal_handler.py @@ -463,6 +463,51 @@ async def test_probe_capacity_refusal_is_audited_and_releases_slot( terminal._pty.openpty.assert_not_called() terminal._pty_child_env.assert_not_called() + @pytest.mark.asyncio + async def test_a_cwd_under_two_stuck_probes_is_refused_by_its_key( + self, invalid_workspace, monkeypatch, tmp_path, + ): + """The cwd is the probe's SECOND argument, so it is keyed by ``stall_path``: + with two probes stuck under it the open is refused before ``_resolve_cwd`` + runs, while a third worker is still free.""" + from kiro_crew import executors + from kiro_crew.dashboard.handlers import files + + request, registry, ws = invalid_workspace + request.query = {"cwd": str(tmp_path)} + resolve = MagicMock(side_effect=AssertionError("_resolve_cwd ran on a stuck mount")) + monkeypatch.setattr(terminal, "_resolve_cwd", resolve) + monkeypatch.setattr(files, "_PATH_PROBE_ADMIT_TIMEOUT_SECS", 0.2) + monkeypatch.setattr(files, "_PATH_PROBE_STALL_SECS", 0.0) + release = threading.Event() + + def park(_path: str, entered: threading.Event) -> None: + entered.set() + release.wait() + + with ThreadPoolExecutor(max_workers=3) as pool: + monkeypatch.setattr(executors, "path_probe_executor", lambda: pool) + entered_a, entered_b = threading.Event(), threading.Event() + wedged = [ + asyncio.ensure_future(files._run_path_probe(park, str(tmp_path / n), e)) + for e, n in ((entered_a, "a"), (entered_b, "b")) + ] + try: + await async_wait_until( + lambda: entered_a.is_set() and entered_b.is_set(), + describe=lambda: "the two stuck probes starting", + ) + assert await terminal.api_terminal_ws(request) is ws + assert registry == {} + assert json.loads(ws.send_str.call_args.args[0])["code"] == ( + "terminal_invalid_cwd" + ) + resolve.assert_not_called() + assert await files._run_path_probe(lambda: "free") == "free" + finally: + release.set() + await await_until(asyncio.gather(*wedged), what="the parked probes settling") + @pytest.mark.parametrize("stage", ["send_str", "close"]) @pytest.mark.asyncio async def test_invalid_workspace_response_is_bounded(