diff --git a/docs/architecture/resource-protection.md b/docs/architecture/resource-protection.md index 6b75b7137503..fb0f9ccc031e 100644 --- a/docs/architecture/resource-protection.md +++ b/docs/architecture/resource-protection.md @@ -75,7 +75,7 @@ Five profiles: | Profile | Used by | Effect | |---------|---------|--------| | `tool` (default) | Every ordinary agent-influenced spawn | The full rlimit ceiling plus `oom_score_adj=1000` | -| `extractor` | The PDF text-extraction child (`pdf_extract.py` -> `python -m kiro_crew.pdf_extract_child`), fed untrusted document bytes on stdin by file-grep and knowledge ingest | A FIXED ceiling independent of `resource_limits`: `RLIMIT_AS` 1 GiB, `RLIMIT_CPU` 60 s, `RLIMIT_NOFILE` 1024, plus the OOM bias. `pdfplumber` allocates a page's whole character list before any caller can measure it, so the memory bound has to be on by default and one process down; a pure-CPython child measures ~270 MB virtual on a one-page document, which is why a virtual cap is safe here where `tool` leaves it opt-in. On Windows (no rlimits) `pdf_extract.py` spawns the child `CREATE_SUSPENDED`, attaches a Job object with `JobMemoryLimit` at the same byte count via `platform_compat.apply_job_limits`, and FAILS CLOSED -- kills the unrun child and reports `unbounded` -- when the job cannot be attached. macOS accepts `RLIMIT_AS` without enforcing it, so the child additionally polices its own peak RSS (`getrusage` `ru_maxrss`, sampled every 20 ms) against the same 1 GiB and ends itself with the `memory` report: the ceiling there, a second layer on Linux | +| `extractor` | The PDF text-extraction child (`pdf_extract.py` -> `python -m kiro_crew.pdf_extract_child`), fed untrusted document bytes on stdin by file-grep and knowledge ingest | A FIXED ceiling independent of `resource_limits`: `RLIMIT_AS` 1 GiB, `RLIMIT_CPU` 60 s, `RLIMIT_NOFILE` 1024, plus the OOM bias. `pdfplumber` allocates a page's whole character list before any caller can measure it, so the memory bound has to be on by default and one process down; a pure-CPython child measures ~270 MB virtual on a one-page document, which is why a virtual cap is safe here where `tool` leaves it opt-in. On Windows (no rlimits) `pdf_extract.py` spawns the child `CREATE_SUSPENDED`, attaches a Job object with `JobMemoryLimit` at the same byte count via `platform_compat.apply_job_limits`, and FAILS CLOSED -- kills the unrun child and reports `unbounded` -- when the job cannot be attached. macOS accepts `RLIMIT_AS` without enforcing it, so the child additionally polices its own peak RSS (sampled every 20 ms) against the same 1 GiB and ends itself with the `memory` report: the ceiling there, a second layer on Linux. The sample is the CHILD's own high-water mark: `getrusage` `ru_maxrss` on macOS, but `/proc/self/status` `VmHWM` on Linux, because there `execve` folds the pre-exec image's peak into `ru_maxrss` and a child of a gateway already past 1 GiB would read its parent's peak on the first tick and lose every document unparsed | | `session_host` | The trusted ACP session-host spawns (`acp/client.py`, `acp/runtime.py`) | RAISES NOFILE to the inherited hard limit and does nothing else. A session host multiplexes many MCP pipe pairs, and the 1024 cap caused EMFILE crashes. No OOM bias: a trusted session host must not be the preferred kill target | | `build` | The dev-fleet build spawns (`apps/builtins/dev_fleet/runtime.py`) | Vite and npm need thousands of descriptors; keeps the OOM bias | | `none` | The user's own interactive terminal | No rlimits and no OOM bias, so the shim is skipped entirely unless the spawn also asks for a controlling terminal (`ctty_fd=`), which the terminal does | diff --git a/docs/system-specs/common/platform-compat.md b/docs/system-specs/common/platform-compat.md index 0937a990b2b9..b90284cced34 100644 --- a/docs/system-specs/common/platform-compat.md +++ b/docs/system-specs/common/platform-compat.md @@ -63,7 +63,7 @@ produces exactly those silent failures, which is why the helper is named per cal | Detect/remove a dir link | `is_link_or_junction(path)` / `unlink_link_or_junction(path)` | `path.is_symlink()` (misses a Windows junction) | | Compare a resolved path against an unresolved one | `strip_extended_length_prefix(path)` on BOTH sides before comparing | comparing the two spellings as `Path.resolve` returns them (on Windows `ntpath.realpath` keeps the extended-length prefix when its prefix-strip re-check races a concurrent swap of the same file, so a prefixed child against an unprefixed parent reads as a path escape; the fold is LEXICAL and must not re-resolve, which would bless the redirect the caller is testing for) | | Hold a directory in place while a child writes into it by path | `pin_directory(path)` (then `os.close`) | `os.open(dir, O_RDONLY)` (EACCES on Windows, and even where it opens it follows a link planted at the name) | -| Process RSS (live) / peak RSS / CPU | `proc_rss_bytes()` / `proc_peak_rss_bytes()` / `proc_cpu_seconds()` | `resource.getrusage` (`ru_maxrss` is a high-water mark, never a live reading, and its unit is KiB on Linux but bytes on macOS) | +| Process RSS (live) / peak RSS / CPU | `proc_rss_bytes()` / `proc_peak_rss_bytes()` / `proc_cpu_seconds()` | `resource.getrusage` (`ru_maxrss` is a high-water mark, never a live reading, and its unit is KiB on Linux but bytes on macOS). The peak on Linux is NOT `ru_maxrss`: `execve` seeds it with the pre-exec image's peak, so a gateway started from a large parent would report that parent's number for life; Linux reads its own `/proc/self/status` `VmHWM` instead, monotonic across reads (the kernel folds live RSS into `hiwater_rss` lazily, so raw consecutive readings can dip a few hundred KiB), and an unreadable `/proc` is the documented 0, never the inherited figure. `pdf_extract_child` carries the same `VmHWM` parser rather than importing this module (its imports stay minimal under a capped address space) | | Available host memory | `host_available_mib()` (0 = unknown, never 0 = no memory) | `/proc/meminfo` directly (Linux-only, so the bound built on it silently vanishes on macOS and Windows) | | FD soft limit | `raise_nofile_soft_limit(n)` | `resource.setrlimit` | | Port to PID | `find_listening_pids(port)` / `listening_pid_tool_available()`; `find_port_listeners(port)` when ownership must be scoped to the local address actually probed; `probe_port_listeners(port)` when completed-empty must be distinguished from timeout or execution failure; `process_owns_loopback_listener(pid, port)` for per-process ownership through Linux procfs, PID-scoped `lsof`, or Windows `GetExtendedTcpTable` owner-PID tables | `lsof` or `netstat` directly | diff --git a/docs/system-specs/common/testing-conventions.md b/docs/system-specs/common/testing-conventions.md index 0d6ef16d3d3d..de57087f1ac9 100644 --- a/docs/system-specs/common/testing-conventions.md +++ b/docs/system-specs/common/testing-conventions.md @@ -2825,13 +2825,217 @@ went red, or the fix lands in the wrong file and the leak stays. and `green_age.run()` is a CLI whose contract is "git in the invoking cwd", so its ~150 descriptors per round will stay flagged until the probe records the PROCESS cwd alongside the `cwd` kwarg -- with the fixture's `chdir` under `tmp_path`, none of them ran in the -checkout. `push_guard.py`'s two direct `git patch-id --stable` calls bypass that module's -`_GIT_CMD` injection seam (its docstring's "every command goes through `run()`" is false -for them) and want a separate fix. `history_search`'s lazily opened `SessionSearchIndex` +checkout. `push_guard.py`'s two direct `git patch-id --stable` calls bypassed that module's +`_GIT_CMD` injection seam (its docstring's "every command goes through `run()`" was false +for them); the twelfth pass routed them through `run()`. `history_search`'s lazily opened `SessionSearchIndex` has no close path, so `search_chat_history` leaks one handle per call until GC (+3, below threshold). The dashboard files' 731 `aiohttp` warnings are class 15 (`app[...]` writes after start, ~24 sites), not this pass. +### What a twelfth five-run pass found (Linux, 16 workers, 139,019 tests per run) + +Five rounds of backend, vitest and electron on a test-only branch off one commit, on the +same 32-core host as the tenth pass, with a LIVE gateway out of the same data home and a +FRESH venv -- run the same day as the eleventh pass (macOS), on the other platform, from +the same base: 139,019 / 39,695 / 2,204 tests per round, identical every round. The backend +was red in every round with an unstable red set -- 12 tests failed 5/5, ten in one file +flipped between pass and fail in every combination, one passed exactly once -- and vitest, +green four times, went red once in round 5. Five production defects came out of it, two of +them user-visible: a PDF extractor that refused every PDF under a large gateway, and a +terminal whose Ctrl-C reached nothing when the gateway had been started as a background +job. The largest class in the report was, once more, the instrument. + +The generalisable lesson this time: **inheritance across `exec` is a host property tests +never see at `-n0`.** A child's `ru_maxrss` starts at its parent's high-water mark; a +signal a launcher ignored stays ignored in every descendant; a process global one module +bound stays bound for every module after it on the worker. Each of the three surfaced as a +red in a test that was correct about its own subject, and each fix is at the seam that +inherits, not in the test that noticed. + +#### The instrument + +- **The conftest's own cache tree is not a host write.** Run 1 filed 210 tests in 77 files + under `host_write`; 209 were `.pyc` files landing in the `sys.pycache_prefix` mirror the + rootdir conftest sets (a fresh venv compiles everything once) and the redirected + hypothesis example database, both under `~/.cache/kirocrew` by that conftest's documented + choice. The probe now sanctions the interpreter's bytecode prefix by construction and + reads a per-sweep `allow-roots.txt` for roots a harness owns, and the analyser re-cuts + runs recorded before the root was learned (`--allow-root`). After the re-cut the class + held one test, and it was real. +- **A session-scoped fence lands on the worker's first test.** `GIT_CEILING_DIRECTORIES`, + set once per worker by the seventh pass's conftest floor, read as an `env_leak` on 44 + innocent tests across five runs -- the same shape as the `XDG_RUNTIME_DIR` pop the + analyser already excludes, and now excluded with it. The one real leak underneath was a + module-scoped `mock.patch.dict(os.environ, {...})` in `test_issue_radar_crew_runtime.py` + (159 of 160 tests, `KIROCREW_CREW_LOG` on and a foreign `KIROCREW_HOME` between tests); + a function-scoped `monkeypatch.setenv` on the flag, and the conftest's own per-test home + pin for the home, took it to 0 of 160 under a per-test env probe. +- **The driver ignored SIGINT for the whole suite.** `cmd &` in a non-interactive bash + starts `cmd` with SIGINT and SIGQUIT ignored; `SIG_IGN` survives `exec`; so every xdist + worker, every PTY shell a terminal test opened and every `sleep` under it ran with + Ctrl-C disabled, and the one test that asserts Ctrl-C delivery failed 4/5 with the + child's `SigIgn` bit set while the shell's was clean. The sweep's own `setsid` wrapper now + resets inherited `SIG_IGN` to `SIG_DFL` before it execs the run. It was also a production + finding (below): a gateway launched the same way ships the same terminals. +- **`under_measured` was volume, not findings.** Thirty-nine tests dropped events past the + 40-event cap; re-run alone with the cap raised, every one was the run-1 bytecode noise + above or the test's own `cwd`-carrying `git`/`openssl` spawns (1,384 per test in one + file). Zero host writes, zero checkout writes, zero connects in the complete streams. + +#### The reds + +- **A child's `ru_maxrss` is its parent's, until the child grows past it.** All seven + PDF reds (`test_pdf_extract.py`, `test_knowledge.py`, `test_file_grep.py`) were + `PDF extraction failed: memory` with detail `rss`: the extractor child's own peak-RSS + watchdog fired on its first 20 ms sample. On Linux `execve` folds the pre-exec image's + high-water RSS into the process's `ru_maxrss` (`fs/exec.c`, `exec_mmap` -> + `setmax_mm_hiwater_rss`), so a child forked from a 1.5-2.2 GiB xdist worker read more + than the 1 GiB `--max-rss` before parsing a byte -- measured on this host as a 1,312 MiB + child `ru_maxrss` against an 11 MB `VmHWM`. At `-n0` the runner is small and all 44 + tests pass; a live gateway over 1 GiB cannot extract any PDF. The fix is production: + `pdf_extract_child.peak_rss_bytes()` reads the child's OWN `VmHWM` from + `/proc/self/status` on Linux (unreadable -> `None`, watchdog off, never the inherited + number); `ru_maxrss` stays the macOS source, where it is per-process. Pinned by a fake + status text through the `_LINUX_STATUS_PATH` seam and by a test whose PARENT touches + 256 MiB, asserts its own `ru_maxrss` exceeds the 128 MiB ceiling it hands the real + child, and still gets page 1 back. The seven tests are unchanged. + `platform_compat.proc_peak_rss_bytes()` -- the dashboard's `proc_mem_peak_mb` and the + `process.memory.peak_rss_bytes` gauge -- read the same inherited number, so a gateway + started from a large parent published a peak that was wrong from its first sample and + never moved; it reads its own `VmHWM` now too, clamped monotonic (the kernel folds live + RSS into `hiwater_rss` lazily, and a raw read dips a few hundred KiB after an `munmap`), + pinned by a bloated-parent -> child spawn that asserts the inheritance is present in the + raw `ru_maxrss` and absent in the reader. +- **A test that plants a fake executable under `tmp_path` and expects a LATER guard to + fire is asserting that `tmp_path` lies outside every checkout.** The five + `test_metachar_arg_refused_for_cmd_launcher[...]` reds failed one check early, at + `preflight.run()`'s working-tree fence, because this sweep's `TMPDIR` sits under the + worktree. CI passed by accident: `which()` was patched to answer the `.cmd` for `git` + too, so the fence's `git rev-parse` exec'd a text file and the resolver swallowed the + `OSError`. The fix pins the fence root at the seam `run()` reads (`_WORKTREE_ROOT`) to a + `tmp_path` sibling that is not the fake's ancestor -- the seventh pass's class, one more + spelling -- and four neighbours that paid a real host `git rev-parse` for a root they do + not test got the same pin (6 -> 2 `git` spawns per run; the two left are the tests OF + the probe). +- **The same stale dispatcher, on Linux.** Ten tests in `test_trusted_apps_api.py` flipped + between runs, every failure a 409 `teardown_incomplete` whose body named `hooks disable + failed: object MagicMock can't be used in 'await' expression` -- the eleventh pass's + finding, measured here as a different subset of ten each round under sixteen workers. + Its rootdir restore floor is the fix; this pass adds the leaker's own contract on top: + the startup-coverage module's `_start_dashboard` pins + `hooks_integration._lifecycle_dispatcher` / `_route_registry` to `None` BEFORE it boots + the real server, so `monkeypatch` puts back what the test found. Attribute by the + failure body, not by the endpoint that answered. +- **A launcher's `SIG_IGN` reaches the user's terminal.** `test_ws_ctrl_c_delivers_sigint` + passed once and failed four times. The failing evidence showed the PTY delivering `^C` + to the right foreground group and `sleep` ignoring it (`SigIgn` bit 1) while bash was + clean: bash keeps a signal that was ignored on entry ignored in every command it runs, + and the shell inherited it from the worker, which inherited it from the driver's `&`. + The passing run was vacuous: the test sent `\x03` on the line-discipline ECHO of + `sleep 120`, before bash had read the line, so `VINTR` flushed the unread input and the + marker echo ran in 48 ms with no `sleep` ever born. The production fix is in the exec + shim: on the `--ctty-fd` path, after `login_tty` and before `execv`, every disposition + that is `SIG_IGN` is reset to `SIG_DFL` (what `login` and `sshd` hand a shell), and the + set is re-ignored if `exec` does not happen so the in-process shim tests leave the worker + untouched. The test now gates the first Ctrl-C on `tcgetpgrp` of the PTY's own descriptor leaving the + shell's group, asserts the shell gets the foreground back afterwards, and reaps the + session in a `finally` -- the `leaked_child`, `thread_leak` and `slow` rows on this + nodeid were all its 35 s failure path. 6/6 green under a launcher that ignores SIGINT. +- **The clock is a dependency, and `not.toContain` is a substring.** The one vitest red + (`CrewWebviewContainment.test.tsx`, round 5) rendered relative ages from the real + `Date.now()` against a fixture instant fixed in September; the "shown version" label + crossed from `22d ago` to `23d ago` during the sweep, and `not.toContain('3d ago')` is + satisfied by `23d ago`. It went red on `main` the same day and was fixed on its own + (a pinned clock and exact element text), so this pass carries no change for it. + +#### The classes that were quiet but real + +- **`spawn_no_cwd`: three binaries, three seams.** Real `kiro-cli --version` ran from + unit tests in eleven files, exactly once per worker per run (80 over five runs): every + agent-spec write ends in `installed_kiro_cli_version()`, cached process-wide, so the + HOST's install decided whether `permissions` was written and which test paid the spawn. + Each module now carries the house pin to `SPEC_PERMISSIONS_MIN_VERSION`. Real `ssh -V` + ran from nine sandbox files -- the `functools.lru_cache`d accept-new probe behind + `_build_launcher_script` the tenth pass pinned in six others; with the eleventh pass's + three and this pass's seven, the same one-line autouse fixture now sits in every + module that builds the launcher (a per-`Popen` recorder, not a + passing suite, is what showed 0). `push_guard.py`'s two `git patch-id --stable` calls + now go through `run()`/`_GIT_CMD`, with a test that fails before the change. A test in + `test_browser_cli_view.py` hand-rolled three of `_stub_port_owner`'s pins and dropped + `process_descendant_identities`: 242 real `ps` snapshots against a pid it did not own, + and 27 s -> 7 s for the file once it used the helper. The doctor's `warm_backend()` + re-probed userns on every `_doctor()` call past a warm cache (44 spawns -> 1); + `code_fingerprint`'s cache paid a real `git` in two more modules (pinned as the daemon + lifecycle module already was). `green_age.run()`'s 152 `cwd=None` spawns per round were + recorded WITH the process cwd this time: every one ran under the scratch root, so the + deliberate descriptor stays and the docstring now says why. +- **`fd_leak`: the tenth pass's seams, applied.** Thirteen tests at +5..+10 in eight + files, all unclosed SQLite handles or a dashboard boot's process handles: stores routed + through the rootdir `opened` fixture (a `KnowledgeStore` per-thread `close()` never + reaches the connection a `to_thread` worker opened; the test-only every-thread close + does), `close_skills_loaders` requested from module autouse fixtures where + `ContextBuilder`s or an `env` fixture build a `SkillsLoader`, `SubagentManager`s tracked + and closed, and a boot that RAISES before returning its state now has that state + recorded through the constructor so `_release_process_handles` still runs. Per-test fd + probe: every flagged test at +0; the only +3 rows left are pytest-asyncio's replacement + loop. +- **`heavy_rss`: a memoised read seam.** `run_scoped_tests._read_text` was an unbounded + `lru_cache`: the local gate's reference scan retained the text of 3,172 test files + (+189 MiB) for the life of the process, for a cache that saved a 0.4 s re-read. It + streams now, pinned by a shape test. `test_source_corpus.py` materialised the corpus + twice per test (`tuple(...)` plus `set(...)`); the tests consume the iterators (count, + path set, `zip(strict=True)`), +264 -> <20 MiB. On the way, `repo_files()` was letting + `git ls-files --others` WALK the run's own scratch under the checkout (126k files, 20 s + per first-asking worker) and filtering afterwards; the same scratch roots and pytest's + `pytest-of-*/` marker are now `--exclude` patterns git prunes, 27 s -> 0.5 s, and about + ten `slow`-class consumers of it clear without a change of their own. +- **`host_write`: one, and it was a stub.** The DeepSeek arm of the launch-golden capture + answered `agent_scratch.allocate_scratch` with a FIXED absolute path, + `/opt/scratch/dsh-session`, and that arm WRITES into the window it is handed + (`record_owner` unlinks `.owner`, the gate probe `rmtree`s it) from executor threads -- + unattributable by frame, host by path. The stub answers a real per-label directory under + `tmp_path` now, pinned by a test that wraps the writers and asserts every path + `is_relative_to(tmp_path)`. A "synthetic-looking" literal is still a path. +- **`thread_leak` was named pools, again.** `mc-mcpprobe_*`, `mc-subproc_*`, + `mc-embed_*`, `mc-recall_*`, `mc-pathres_*`, `mc-discovery_*`, + `skill-catalog-refresh`: bounded pools warming on first use, left alone. The unnamed + `Thread-N (_do_shutdown)` / `asyncio-waitpid` rows were the terminal test's failure path. +- **`kill`: 707 signals, zero non-zero signals at a foreign process.** An ownership + probe (registering every `Popen`/`fork`/`posix_spawn` pid, auditing `os.kill`/`killpg`, + classifying each target by registered pid, `/proc` lineage, own process group, own + thread, or an inherited marker variable) over the 68 flagged files, at `-n0`, twice: + 395 signal-0 liveness probes, 190 SIGKILL, 120 SIGTERM, 2 SIGINT. Every SIGTERM and + SIGKILL went to a registered child, a descendant by lineage or group, or a number the + body had already proven dead (two: the fabricated `2000000000` and production's + `_sync_kill_provider` at an already-reaped grandchild, ESRCH). The six "external" + targets left were all signal 0 through `platform_compat.pid_exists` / `pid_liveness` / + `pgroup_exists` at fabricated pids (`1`, `123`, `1001`) or at the test's own `setsid` + grandchild after init had adopted it -- class 9's sanctioned shape. Exactly two + signal-0 probes came from anywhere else, and they were the real defects: a naked + `os.kill(pid, 0)` poll in `test_cli_manifest_signature.py` (routed through + `pid_exists`), and the twin driver-reap tests in `test_codex_session_mcp.py` / + `test_opencode_session_mcp.py`, which polled a grandchild pid read from the driver's + stdout with `os.kill(pid, 0)` and SIGKILLed the RAW number on the failure path after the + driver's group had been killed -- class 27: the driver now prints + `get_process_start_id(pid)` at spawn, the poll treats an identity mismatch as gone, and + the failure-path kill is `kill_pid_pinned`. Post-fix census: signal 0 outside + `platform_compat`, 2 -> 0. + +#### What not to re-derive + +The `unattributable` class (49,815 tests, every second file) is pytest's `tmp_path` +retention sweep and the repo's pinned-descriptor writers, as the tenth pass established; +the analyser excludes it from the partition. `green_age.run()` and the auto-improvement +`git -C` helpers keep `cwd=None` (see the tenth pass), now with the process cwd recorded +per spawn to prove it harmless. The exec shim still leaves `SIGPIPE`/`SIGXFSZ` ignored for +NON-terminal spawns (a Python shim re-ignores them after the fork child's +`restore_signals`), so `yes | head` in a spawned `bash -c` gets `EPIPE` -- a latent defect +with its own test to write, deliberately not changed in this pass because it alters tool +spawn semantics. `session_pid._sync_kill_provider` SIGKILLs a snapshot pid through the unpinned `kill_pid` +(the census saw it hit an already-reaped grandchild, ESRCH) -- a production class-27 +candidate for its own change, with a test that plants a reissued number. The dashboard +files' `aiohttp` warnings remain class 15. + ## Running the suite: the defaults, and how to narrow safely The checkpoint run before a commit is the change-related set on both surfaces, diff --git a/scripts/run_scoped_tests.py b/scripts/run_scoped_tests.py index bebebdf7d46e..ff6bbe4f373e 100644 --- a/scripts/run_scoped_tests.py +++ b/scripts/run_scoped_tests.py @@ -254,8 +254,18 @@ def _tree_files(root: Path, rel_roots: tuple[str, ...], name_re: re.Pattern[str] return tuple(sorted(out)) -@functools.lru_cache(maxsize=None) def _read_text(root: Path, rel: str) -> str: + """One test file's text, read for the scan that asked and then dropped. + + Deliberately NOT memoised, unlike `_tree_files` above. `_referencing_tests` + reads each file exactly once per `related_targets` call, so a cache buys the + gate nothing; what it cost was the whole test tree resident for the life of + the process -- ~3,200 files, ~100 MB of `str` -- for a 0.4 s re-read. In the + gate process that is wasted memory; imported into a pytest worker (as + `test_local_gate.py` does) it was a +190 MiB high-water mark that outlived + the test. The self-test's dozen `related_targets` calls pay the re-read; the + regex scan they also repeat costs more than the read does. + """ try: return (root / rel).read_text(encoding="utf-8", errors="replace") except OSError: diff --git a/src/kiro_crew/_spawn_exec_shim.py b/src/kiro_crew/_spawn_exec_shim.py index 7c65fe101544..ba7c60c63ab9 100644 --- a/src/kiro_crew/_spawn_exec_shim.py +++ b/src/kiro_crew/_spawn_exec_shim.py @@ -223,6 +223,50 @@ def _acquire_controlling_tty(fd: int) -> bool: return True +def _default_ignored_signals() -> list[int]: + """Give the terminal session the default dispositions a login would. + + ``SIG_IGN`` survives ``exec`` where a handler does not, so every signal the + gateway process inherited as ignored -- a launcher that backgrounds it from + a script sets SIGINT and SIGQUIT to ignore, ``nohup`` sets SIGHUP, and this + interpreter itself ignores SIGPIPE on startup -- would otherwise reach the + shell as ignored. An interactive shell keeps a signal that was ignored on + entry ignored in every command it runs, so the user would get a terminal + where Ctrl+C never interrupts the foreground job and a closed terminal never + hangs it up, with nothing in the shell's own state to say why. ``login`` + and ``sshd`` hand the shell defaults; so does this. + + Only dispositions that are ``SIG_IGN`` are touched: handlers reset on + ``exec`` by themselves, and the two signals that cannot be changed are + skipped by the ``OSError`` the kernel raises for them. Returns the signals + it changed so a failed ``exec`` can put them back: the shim then reports + through the stderr it was given, and must not die of a SIGPIPE it had been + told to ignore while doing so. + """ + import signal + + changed: list[int] = [] + for signum in signal.valid_signals(): + try: + if signal.getsignal(signum) is signal.SIG_IGN: + signal.signal(signum, signal.SIG_DFL) + changed.append(signum) + except (OSError, ValueError, RuntimeError): + continue + return changed + + +def _reignore_signals(signums: list[int]) -> None: + """Undo :func:`_default_ignored_signals` on the path where ``exec`` did not happen.""" + import signal + + for signum in signums: + try: + signal.signal(signum, signal.SIG_IGN) + except (OSError, ValueError, RuntimeError): + continue + + def main(argv: list[str] | None = None) -> int: """Parse the shim's own options, then ``exec`` the command after ``--``. @@ -307,16 +351,24 @@ def main(argv: list[str] | None = None) -> int: # runtime spawn path owns. A terminal (--ctty-fd) command that is already # missing must fail fast instead of stalling the shell for the budget. attempts = 1 if ctty_fd is not None else _EXECV_RETRY_ATTEMPTS - for attempt in range(attempts): - try: - os.execv(encoded[0], encoded) - except OSError as exc: - if exc.errno in _EXECV_RETRYABLE_ERRNOS and attempt < attempts - 1: - time.sleep(_EXECV_RETRY_DELAY_S) - continue - sys.stderr.write(f"spawn shim: cannot execute {args[0]!r}: {exc.strerror}\n") - return _EXEC_FAILED - return _EXEC_FAILED # pragma: no cover - execv does not return on success + # A terminal session starts from default signal dispositions, as it would + # from login. Done last, so the shim's own failure reports above still run + # under the dispositions it was given, and undone if exec does not happen. + reset_signals = _default_ignored_signals() if ctty_fd is not None else [] + try: + for attempt in range(attempts): + try: + os.execv(encoded[0], encoded) + except OSError as exc: + if exc.errno in _EXECV_RETRYABLE_ERRNOS and attempt < attempts - 1: + time.sleep(_EXECV_RETRY_DELAY_S) + continue + _reignore_signals(reset_signals) + sys.stderr.write(f"spawn shim: cannot execute {args[0]!r}: {exc.strerror}\n") + return _EXEC_FAILED + return _EXEC_FAILED # pragma: no cover - execv does not return on success + finally: + _reignore_signals(reset_signals) return _EXEC_FAILED # pragma: no cover - the loop returns on every attempt diff --git a/src/kiro_crew/builtin_skills/kirocrew-dev/prepare-pr/scripts/push_guard.py b/src/kiro_crew/builtin_skills/kirocrew-dev/prepare-pr/scripts/push_guard.py index f960549332ef..ab96491916a5 100644 --- a/src/kiro_crew/builtin_skills/kirocrew-dev/prepare-pr/scripts/push_guard.py +++ b/src/kiro_crew/builtin_skills/kirocrew-dev/prepare-pr/scripts/push_guard.py @@ -52,9 +52,13 @@ _GIT_CMD: list[str] | None = None -def run(args): +def run(args, input=None): """Run a command; return (returncode, stdout, stderr) as stripped text. + Every git command this script issues goes through this ONE runner -- + including the ``patch-id`` calls, which hand their diff over ``input`` -- + so the injection point below covers all of them. + When the module-level _GIT_CMD is set (test monkeypatch), "git" is replaced with the specified command list — no PATH/shell wrappers or environment-variable indirection needed. Otherwise git is resolved via @@ -69,7 +73,14 @@ def run(args): resolved = shutil.which("git") if resolved: args = [resolved] + list(args[1:]) - p = subprocess.run(args, capture_output=True, text=True, encoding="utf-8", errors="replace") + p = subprocess.run( + args, + input=input, + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + ) return p.returncode, p.stdout.strip(), p.stderr.strip() except OSError as exc: return 127, "", "{}: {}".format(args[0], exc) @@ -319,33 +330,19 @@ def _check_pre_squash(base, max_ahead): if not diff_out: # diff-tree SUCCEEDED with empty output — empty commit, skip. continue - # Feed the diff to git patch-id --stable via stdin. - try: - pid_proc = subprocess.run( - ["git", "patch-id", "--stable"], - input=diff_out, - capture_output=True, - text=True, - encoding="utf-8", - errors="replace", - ) - if pid_proc.returncode != 0: - err( - "REFUSED: git patch-id --stable failed (exit {}) for" - " commit {}. Cannot verify replay safety —" - " failing closed.".format(pid_proc.returncode, commit_sha[:12]) - ) - return 40 - if pid_proc.stdout.strip(): - patch_id = pid_proc.stdout.strip().split()[0] - ahead_patch_ids[patch_id] = commit_sha - except OSError as exc: + # Feed the diff to git patch-id --stable via stdin. run() reports an + # OSError as exit 127, so one branch covers both failure shapes. + pid_rc, pid_out, _ = run(["git", "patch-id", "--stable"], input=diff_out) + if pid_rc != 0: err( - "REFUSED: git patch-id --stable raised OSError for commit" - " {}: {}. Cannot verify replay safety —" - " failing closed.".format(commit_sha[:12], exc) + "REFUSED: git patch-id --stable failed (exit {}) for" + " commit {}. Cannot verify replay safety —" + " failing closed.".format(pid_rc, commit_sha[:12]) ) return 40 + if pid_out: + patch_id = pid_out.split()[0] + ahead_patch_ids[patch_id] = commit_sha if not ahead_patch_ids: print("STATUS: SAFE TO PUSH") @@ -383,32 +380,17 @@ def _check_pre_squash(base, max_ahead): if not diff_out: # diff-tree SUCCEEDED with empty output — empty commit, skip. continue - try: - pid_proc = subprocess.run( - ["git", "patch-id", "--stable"], - input=diff_out, - capture_output=True, - text=True, - encoding="utf-8", - errors="replace", - ) - if pid_proc.returncode != 0: - err( - "REFUSED: git patch-id --stable failed (exit {}) for" - " base commit {}. Cannot verify replay safety —" - " failing closed.".format(pid_proc.returncode, commit_sha[:12]) - ) - return 40 - if pid_proc.stdout.strip(): - patch_id = pid_proc.stdout.strip().split()[0] - base_patch_ids[patch_id] = commit_sha - except OSError as exc: + pid_rc, pid_out, _ = run(["git", "patch-id", "--stable"], input=diff_out) + if pid_rc != 0: err( - "REFUSED: git patch-id --stable raised OSError for base" - " commit {}: {}. Cannot verify replay safety —" - " failing closed.".format(commit_sha[:12], exc) + "REFUSED: git patch-id --stable failed (exit {}) for" + " base commit {}. Cannot verify replay safety —" + " failing closed.".format(pid_rc, commit_sha[:12]) ) return 40 + if pid_out: + patch_id = pid_out.split()[0] + base_patch_ids[patch_id] = commit_sha # Step 3: find matches. replayed_pairs: list[tuple[str, str]] = [] # (ahead-sha, base-sha) diff --git a/src/kiro_crew/builtin_skills/kirocrew-dev/writing-tests/SKILL.md b/src/kiro_crew/builtin_skills/kirocrew-dev/writing-tests/SKILL.md index 14b521547ebc..a12e086b2d33 100644 --- a/src/kiro_crew/builtin_skills/kirocrew-dev/writing-tests/SKILL.md +++ b/src/kiro_crew/builtin_skills/kirocrew-dev/writing-tests/SKILL.md @@ -644,3 +644,34 @@ The consequence for how you write a test: (`pool.spawn_shutdown`), never an inline `await shutdown()` in a handler's `finally`: teardown cancels the handler after the backend has left the map, and the child then belongs to nobody +- [ ] A child that polices its OWN peak memory reads `/proc/self/status` `VmHWM` on Linux, + never `getrusage(RUSAGE_SELF).ru_maxrss`: `execve` seeds `ru_maxrss` with the parent's + high-water mark, so a child of a 2 GiB xdist worker (or gateway) reads over its ceiling + before it has parsed a byte -- green at `-n0`, red under the suite; the test that pins + it plants a BLOATED PARENT and asserts the child still finishes +- [ ] A test that plants a fake executable under `tmp_path` and expects a guard LATER than + the working-tree fence to fire pins the fence root (`_WORKTREE_ROOT`) to a `tmp_path` + sibling that is not the fake's ancestor -- under a repo-internal `TMPDIR` the fence + wins first, and a patched `which()` that answers the fake for `git` too can hide it on CI +- [ ] A module that boots the REAL `start_dashboard` pins + `hooks_integration._lifecycle_dispatcher` / `_route_registry` to `None` BEFORE the boot + (so `monkeypatch` restores them); a module whose assertions assume "never booted" says + so with an autouse pin. Attribute a flaky 409 by its BODY (`hooks disable failed: ... + MagicMock ... await`), not by the endpoint that answered +- [ ] A PTY test that asserts a child receives a signal gates the send on + `os.tcgetpgrp` on the PTY's own descriptor leaving the shell's group -- the line-discipline ECHO of the + command is not proof the shell has read it (`VINTR` flushes unread input and the test + passes with no child ever born) -- and reaps the session group in a `finally`; a spawn + that hands a shell a terminal resets inherited `SIG_IGN` to `SIG_DFL` first, because a + backgrounded launcher's ignored SIGINT survives `exec` into every descendant +- [ ] A module-scoped autouse fixture never wraps the module in + `mock.patch.dict(os.environ, {...})`: the keys are live between tests for every module + the worker runs meanwhile, while the function-scoped conftest floor already overrides the + home during each body -- set the feature flag per test with `monkeypatch.setenv` +- [ ] A memoised per-file read on a tree scan (`lru_cache` on `_read_text`, a `tuple(...)` + corpus helper) keeps every file resident for the process; the seam streams, and the test + consumes the iterator (count, path set, `zip(strict=True)`) instead of materialising it +- [ ] A test's own `kiro-cli --version` is never the HOST's: every agent-spec write reaches + `installed_kiro_cli_version()`, cached per process, so the module pins it to + `SPEC_PERMISSIONS_MIN_VERSION` (house fixture) or the host's install decides the + contract under test diff --git a/src/kiro_crew/pdf_extract_child.py b/src/kiro_crew/pdf_extract_child.py index 2c5fb28a06a1..e8826b5a2490 100644 --- a/src/kiro_crew/pdf_extract_child.py +++ b/src/kiro_crew/pdf_extract_child.py @@ -115,12 +115,48 @@ def _emit(payload: dict, *, flush: bool = False) -> None: sys.stdout.flush() +#: Where Linux reports THIS process's peak resident size. ``ru_maxrss`` is not +#: that on Linux: ``execve`` folds the pre-exec image's high-water mark into the +#: new one (``fs/exec.c`` ``exec_mmap`` -> ``setmax_mm_hiwater_rss``), so a child +#: forked from a large parent reads the PARENT's peak as its own for life. A +#: gateway over ``--max-rss`` would then lose every child on the watchdog's first +#: sample, before a byte of the document was parsed. ``VmHWM`` is the current +#: ``mm``'s own high-water mark, which a fresh image starts from zero. +_LINUX_STATUS_PATH = "/proc/self/status" +_LINUX_PEAK_FIELD = "VmHWM:" + + +def _peak_rss_from_status(status: str) -> int | None: + """Parse ``VmHWM`` out of a ``/proc//status`` text, in bytes. + + The kernel prints the field as ``VmHWM:\\t 11432 kB`` -- always kB, so + any other shape (a missing field, a unit that is not kB, a non-numeric + value) is unreadable rather than a guess. + """ + for line in status.splitlines(): + if not line.startswith(_LINUX_PEAK_FIELD): + continue + parts = line.split() + if len(parts) == 3 and parts[2] == "kB" and parts[1].isdigit(): + return int(parts[1]) * 1024 + return None + return None + + def peak_rss_bytes() -> int | None: """This process's peak resident size in bytes, or ``None`` where unreadable. - ``ru_maxrss`` is KiB on Linux and BYTES on macOS -- the one place the two + Linux reads its own ``VmHWM`` (see :data:`_LINUX_STATUS_PATH` for why + ``ru_maxrss`` is the parent's number there); everywhere else ``ru_maxrss``, + which is KiB on the BSDs and BYTES on macOS -- the one place those two differ, and the reason this is not a bare ``getrusage`` call at the use site. """ + if sys.platform.startswith("linux"): + try: + with open(_LINUX_STATUS_PATH, encoding="utf-8", errors="replace") as fh: + return _peak_rss_from_status(fh.read()) + except OSError: + return None if _resource is None: return None peak = _resource.getrusage(_resource.RUSAGE_SELF).ru_maxrss diff --git a/src/kiro_crew/platform_compat.py b/src/kiro_crew/platform_compat.py index ed9664eefd5b..575a380fc21e 100644 --- a/src/kiro_crew/platform_compat.py +++ b/src/kiro_crew/platform_compat.py @@ -8157,8 +8157,13 @@ def _scale_ru_maxrss(ru_maxrss: int) -> int: def _ru_maxrss_bytes() -> int | None: """Peak (high-water) RSS in bytes from ``getrusage``, or None on failure. - POSIX only. This is a **peak**, not a live reading: ``ru_maxrss`` never - decreases for the life of the process. + POSIX only, and NOT the Linux reader: there ``execve`` folds the pre-exec + image's high-water mark into the new process's ``ru_maxrss`` (``fs/exec.c`` + ``exec_mmap`` -> ``setmax_mm_hiwater_rss``), so a gateway started from a + large parent -- a launcher, a test runner, a bloated shell -- reports that + parent's peak as its own for life. :func:`_linux_peak_rss_bytes` is the + Linux source; :func:`_posix_peak_rss_bytes` picks. This is a **peak**, not + a live reading: ``ru_maxrss`` never decreases for the life of the process. """ try: return _scale_ru_maxrss(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss) @@ -8166,6 +8171,71 @@ def _ru_maxrss_bytes() -> int | None: return None +#: Where Linux reports THIS process's own peak resident size: ``VmHWM`` in +#: ``/proc/self/status`` is the current ``mm``'s high-water mark, which a fresh +#: image starts from zero, unlike ``ru_maxrss`` (see :func:`_ru_maxrss_bytes`). +_LINUX_STATUS_PATH = Path("/proc/self/status") +_LINUX_PEAK_RSS_FIELD = "VmHWM:" +#: The highest ``VmHWM`` this process has read. The kernel answers ``VmHWM`` +#: with ``max(hiwater_rss, live RSS)`` but folds the live figure into +#: ``hiwater_rss`` only at unmap/exit, from per-thread counters it syncs in +#: batches, so a reading taken while a mapping is live can sit a few hundred KiB +#: above what the next reading, after the unmap, reports (measured 136-376 KiB +#: on a 128 MiB mapping). A peak that never decreases is the contract, so the +#: reader keeps its own floor. +_LINUX_PEAK_RSS_FLOOR = 0 + + +def _peak_rss_from_status(status: str) -> int | None: + """Parse ``VmHWM`` out of a ``/proc//status`` text, in bytes. + + The kernel prints the field as ``VmHWM:\\t 11432 kB`` -- always kB, so any + other shape (a missing field, a unit that is not kB, a non-numeric value) is + unreadable rather than a guess. ``kiro_crew.pdf_extract_child`` carries the + same parser by design: that module keeps its imports minimal because it runs + under a capped address space, so it does not import this one. + """ + for line in status.splitlines(): + if not line.startswith(_LINUX_PEAK_RSS_FIELD): + continue + parts = line.split() + if len(parts) == 3 and parts[2] == "kB" and parts[1].isdigit(): + return int(parts[1]) * 1024 + return None + return None + + +def _linux_peak_rss_bytes() -> int | None: + """This process's OWN peak RSS in bytes from ``VmHWM``, or None if unreadable. + + Monotonic across calls (see :data:`_LINUX_PEAK_RSS_FLOOR`); an unreadable + status file is None even when a floor exists, so a failure reads as one. + """ + global _LINUX_PEAK_RSS_FLOOR + try: + peak = _peak_rss_from_status(_LINUX_STATUS_PATH.read_text(encoding="utf-8")) + except (OSError, ValueError): + return None + if peak is None: + return None + if peak > _LINUX_PEAK_RSS_FLOOR: + _LINUX_PEAK_RSS_FLOOR = peak + return _LINUX_PEAK_RSS_FLOOR + + +def _posix_peak_rss_bytes() -> int | None: + """Peak RSS in bytes for THIS process, or None where it cannot be read. + + Linux reads its own ``VmHWM``; an unreadable ``/proc`` is None, never the + inherited ``ru_maxrss`` -- a wrong number is worse than a missing one in a + figure an operator uses to size a host. Every other POSIX platform reads + ``ru_maxrss`` in its unit. + """ + if sys.platform.startswith("linux"): + return _linux_peak_rss_bytes() + return _ru_maxrss_bytes() + + def _linux_current_rss_bytes() -> int | None: """Current RSS in bytes from ``/proc/self/statm``, or None if unreadable. @@ -8264,11 +8334,12 @@ def proc_rss_bytes() -> int: - Linux: ``/proc/self/statm`` resident pages. - macOS: Mach ``task_info(MACH_TASK_BASIC_INFO).resident_size``. - Windows: ``GetProcessMemoryInfo().WorkingSetSize``. - - Last resort on POSIX only: ``getrusage(RUSAGE_SELF).ru_maxrss``, which is - a **peak** that never decreases. It is here so an unreadable ``/proc`` or - an unavailable ``libSystem`` still yields an order-of-magnitude number - rather than 0, and it over-reports by construction — see - :func:`proc_peak_rss_bytes` for the peak as a deliberate reading. + - Last resort on POSIX only: the process's own peak (``VmHWM`` on Linux, + ``getrusage(RUSAGE_SELF).ru_maxrss`` elsewhere), which never decreases. + It is here so an unavailable ``libSystem`` or a ``statm`` that will not + parse still yields an order-of-magnitude number rather than 0, and it + over-reports by construction — see :func:`proc_peak_rss_bytes` for the + peak as a deliberate reading. """ if IS_POSIX: current = ( @@ -8276,7 +8347,7 @@ def proc_rss_bytes() -> int: ) if current is not None: return current - return _ru_maxrss_bytes() or 0 + return _posix_peak_rss_bytes() or 0 counters = _windows_memory_counters() return 0 if counters is None else int(counters.WorkingSetSize) @@ -8391,12 +8462,14 @@ def proc_peak_rss_bytes() -> int: The high-water mark since the process started: it never decreases, which is what makes it useful for diagnosing a transient spike that a live reading - has already forgotten — and useless as the live reading itself. POSIX reads - ``getrusage(RUSAGE_SELF).ru_maxrss``; Windows reads + has already forgotten — and useless as the live reading itself. Linux reads + this process's own ``/proc/self/status`` ``VmHWM`` (``ru_maxrss`` there is + inherited across ``execve`` from the parent, see :func:`_ru_maxrss_bytes`); + other POSIX reads ``getrusage(RUSAGE_SELF).ru_maxrss``; Windows reads ``GetProcessMemoryInfo().PeakWorkingSetSize``. """ if IS_POSIX: - return _ru_maxrss_bytes() or 0 + return _posix_peak_rss_bytes() or 0 counters = _windows_memory_counters() return 0 if counters is None else int(counters.PeakWorkingSetSize) diff --git a/test/acp_launch_capture.py b/test/acp_launch_capture.py index 5554b1deb620..ca6bc21a29b3 100644 --- a/test/acp_launch_capture.py +++ b/test/acp_launch_capture.py @@ -75,9 +75,6 @@ # argv rather than the filesystem. _DSH_EXTENSION = "/opt/run/kiro_crew_tool_gate.mjs" _DSH_PATCH = "/opt/run/kiro_crew_dsh_gate.patch.yml" -#: The private scratch window the DeepSeek gate marker is written into. Fixed so -#: the capture names no host path; see the ``allocate_scratch`` stub below. -_DSH_SCRATCH = Path("/opt/scratch/dsh-session") _OPENCODE_BIN = "/opt/bin/opencode" _GOOSE_BIN = "/opt/bin/goose" _DEEPSEEK_BIN = "/opt/bin/dsh" @@ -221,6 +218,27 @@ async def _windows_cleanup_passthrough(factory: Any) -> Any: return await factory() +def _allocate_capture_scratch(tmp_path: Path, label: str) -> Path: + """A scratch window for *label*, as a REAL directory under *tmp_path*. + + Stands in for ``agent_scratch.allocate_scratch`` on the one arm that needs a + window (DeepSeek, see :func:`_stub_common`). It has to be a directory that + exists, and it has to be under the test's own temp dir, because the spawn path + does not stop at reading the path back: ``record_owner`` installs the child's + pid as ``.owner`` inside the session window from an executor thread, and the + gate probe's throwaway window is ``shutil.rmtree``'d in the arm's ``finally``. + A fixed synthetic path -- this file once answered ``/opt/scratch/dsh-session`` + -- turns both of those into writes at a real absolute path on the recording + host, outside every sandbox: an ``unlink`` of that path's ``.owner`` and an + ``rmtree`` of the path itself. Neither reaches the golden (the sandbox wrap, + the read-back and ``scratch_env`` are all stubbed), so nothing is pinned by + naming a host path here and nothing changes in the fixture by not doing so. + """ + path = tmp_path / "scratch" / label + path.mkdir(parents=True, exist_ok=True) + return path + + #: Collaborators the capture answers with one of the call's OWN arguments, and which #: argument each answers with. Every one of them reads the host otherwise -- the two #: env resolvers and the pod home remap read config and the real environment, the pod @@ -308,17 +326,26 @@ def _finish(_process, _pid, *, label: str) -> bool: # its gate's load marker is written by the child into this private # window, because the gate-artifact leaf is sealed read-only against the # child, and a session with nowhere to put the marker is refused rather - # than run ungated. + # than run ungated. It is answered with a real directory under + # ``tmp_path`` (see :func:`_allocate_capture_scratch`), one per label, + # because the arm WRITES into the window it is handed. patch.object( client_mod.agent_scratch, "allocate_scratch", - return_value=_DSH_SCRATCH if backend == ACP_BACKEND_DEEPSEEK else None, + side_effect=_stub_for( + client_mod.agent_scratch.allocate_scratch, + lambda call: ( + _allocate_capture_scratch(tmp_path, call["label"]) + if backend == ACP_BACKEND_DEEPSEEK + else None + ), + ), ), # The env that window contributes, pinned to placeholders rather than to # this host's spelling of it. Two things vary by platform and neither is - # the fact the golden exists to hold. ``str(Path("/opt/scratch/x"))`` - # renders backslash-separated on Windows, so a literal path would fail - # there on separators alone; and ``KIRO_CHAT_LOG_FILE`` is set only where + # the fact the golden exists to hold. ``str(Path(...))`` renders + # backslash-separated on Windows, so a literal path would fail there on + # separators alone; and ``KIRO_CHAT_LOG_FILE`` is set only where # the log cap can bound it, which is every platform except Windows, so its # KEY presence varies too. Stubbing the whole contribution keeps one # golden for both platforms while still pinning what matters -- that the diff --git a/test/source_corpus.py b/test/source_corpus.py index a049598da3cd..ed0891b929c1 100644 --- a/test/source_corpus.py +++ b/test/source_corpus.py @@ -270,7 +270,28 @@ def repo_files() -> tuple[Path, ...]: CALLING gate: this says what the checkout holds, never what a gate polices. """ root = repo_root() - argv = ["git", "-C", str(root), "ls-files", "-z", "--cached", "--others", "--exclude-standard"] + scratch = _run_scratch_roots(root) + # What ``_is_run_scratch`` drops below is also handed to git as exclusions, so + # ``--others`` does not WALK it first: a sweep's ``TMPDIR`` under the checkout + # held ~126k files under pytest's ``pytest-of-`` marker -- 20 s of git + # per worker (plus 138k ``Path`` objects built to be dropped), paid by every + # gate that first asked on that worker and again by every test that cleared the + # cache. Excluded, the same enumeration is under a second. The Python filter + # stays: it is the contract (``test_the_runs_own_temp_files_are_not_the_checkout`` + # pins it with real git), and the two must agree, so both read the same roots + # and the same marker. + argv = [ + "git", + "-C", + str(root), + "ls-files", + "-z", + "--cached", + "--others", + "--exclude-standard", + f"--exclude={_PYTEST_TEMP_ROOT_PREFIX}*/", + *(f"--exclude={_gitignore_literal(candidate.relative_to(root))}" for candidate in scratch), + ] try: out = subprocess.run(argv, capture_output=True, check=True, timeout=60, cwd=str(root)) except OSError: @@ -302,7 +323,6 @@ def repo_files() -> tuple[Path, ...]: # tree but not yet staged (the index entry survives, so git still names it), # and a submodule gitlink, which arrives as a DIRECTORY path -- and ``--others`` # lists a nested repository as its bare directory. ``is_file`` drops all three. - scratch = _run_scratch_roots(root) return tuple( sorted( path for path in paths if not _is_run_scratch(path, root, scratch) and path.is_file() @@ -310,6 +330,20 @@ def repo_files() -> tuple[Path, ...]: ) +def _gitignore_literal(rel: Path) -> str: + """*rel* as a ``--exclude`` pattern matching exactly that directory under the root. + + gitignore syntax: a leading ``/`` anchors the pattern at the top level and a + trailing ``/`` matches only a directory, so ``/hygiene/tmp/`` is that tree and + nothing that merely shares its name. The wildcard characters and the escape + itself are escaped so a temp root spelled with one is still a literal. + """ + text = rel.as_posix() + for char in ("\\", "*", "?", "[", "]"): + text = text.replace(char, "\\" + char) + return f"/{text}/" + + #: pytest's per-user temp root. ``_pytest/tmpdir.py`` creates every ``tmp_path`` #: under ``/pytest-of-/pytest-/``; the name is pytest's #: own marker, the same one the root conftest recognises in its residue scan. diff --git a/test/test_acp_client_more_coverage.py b/test/test_acp_client_more_coverage.py index 99b752d9e2e1..a5bb2ad4330f 100644 --- a/test/test_acp_client_more_coverage.py +++ b/test/test_acp_client_more_coverage.py @@ -67,12 +67,31 @@ JsonRpcMessage, ) from kiro_crew.hooks import HOOK_EVENT_POST_TOOL_USE +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION _POSIX_ONLY = pytest.mark.skipif( sys.platform == "win32", reason="POSIX-only APIs (os.kill, /proc, ps, AF_UNIX sockets)" ) +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A client start here materialises the agent spec (``ensure_agent_materialized`` + -> ``rebuild_agent_config`` -> ``_write_derived_permissions``), which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker starts a client first pays it against the HOST's + install with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + def _client(tmp_path: Path, **kwargs) -> AcpClient: """An AcpClient pinned to *tmp_path* so nothing is written outside it.""" return AcpClient(work_dir=tmp_path, **kwargs) diff --git a/test/test_acp_codex_harness.py b/test/test_acp_codex_harness.py index dec13b814634..070ac7eec97e 100644 --- a/test/test_acp_codex_harness.py +++ b/test/test_acp_codex_harness.py @@ -78,9 +78,29 @@ ACP_BACKENDS_SESSION_EVICTION, effort_config_option_id, ) +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION from kiro_crew.providers.acp import AcpProvider from kiro_crew.providers.mirrors.registry import has_mirror + +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A runtime start here materialises the agent spec (``ensure_agent_materialized`` + -> ``rebuild_agent_config`` -> ``_write_derived_permissions``), which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker starts first pays it against the HOST's install + with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + # --------------------------------------------------------------------------- # The fake peer # --------------------------------------------------------------------------- diff --git a/test/test_acp_deepseek_backend.py b/test/test_acp_deepseek_backend.py index cd6c233aad91..16567e323146 100644 --- a/test/test_acp_deepseek_backend.py +++ b/test/test_acp_deepseek_backend.py @@ -49,6 +49,26 @@ effort_config_option_id, routing_for, ) +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION + + +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A client start here materialises the agent spec (``ensure_agent_materialized`` + -> ``rebuild_agent_config`` -> ``_write_derived_permissions``), which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker starts first pays it against the HOST's install + with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + # ── Vocabulary ─────────────────────────────────────────────────────────────── diff --git a/test/test_acp_launch_goldens.py b/test/test_acp_launch_goldens.py index c9e969532c16..0bf092aa21d7 100644 --- a/test/test_acp_launch_goldens.py +++ b/test/test_acp_launch_goldens.py @@ -124,6 +124,54 @@ def test_a_capture_leaves_every_resolver_cache_as_it_found_it(tmp_path) -> None: ) +def test_the_deepseek_capture_writes_its_scratch_windows_only_under_tmp_path( + monkeypatch, tmp_path +) -> None: + """Every filesystem write the DeepSeek arm makes lands under the test's temp dir. + + The arm is the one harness whose spawn path WRITES into the scratch window it is + handed rather than only naming it: ``record_owner`` installs the child's pid as + ``.owner`` in the session window, and the gate probe's throwaway window is + ``rmtree``'d in the arm's ``finally`` -- both from an executor thread, so neither + is attributable to this test by frame. When the capture answered + ``allocate_scratch`` with a fixed synthetic ``/opt/scratch/...`` path, both became + writes at a real absolute path on the recording host, outside every sandbox. + + The two writers are wrapped at the names the arm looks up, so a stub that ever + again answers a path outside ``tmp_path`` fails here with the path named, instead + of leaving an ``unlink`` and an ``rmtree`` aimed at someone's disk. And the owner + write must be RECORDED, not merely attempted: an absent directory answers + ``"unwritable"``, which is exactly what the host path did. + """ + touched: list[Path] = [] + outcomes: list[str] = [] + real_record_owner = client_mod.agent_scratch.record_owner + real_rmtree = client_mod.shutil.rmtree + + def _record_owner(path, pid): + touched.append(Path(path)) + outcome = real_record_owner(path, pid) + outcomes.append(outcome) + return outcome + + def _rmtree(path, *args, **kwargs): + touched.append(Path(path)) + return real_rmtree(path, *args, **kwargs) + + monkeypatch.setattr(client_mod.agent_scratch, "record_owner", _record_owner) + monkeypatch.setattr(client_mod.shutil, "rmtree", _rmtree) + + capture_mod.capture(ACP_BACKEND_DEEPSEEK, tmp_path) + + assert len(touched) >= 2, ( + "the DeepSeek arm neither recorded a scratch owner nor removed its probe " + f"window, so this test measured nothing: {touched}" + ) + outside = [path for path in touched if not path.is_relative_to(tmp_path)] + assert not outside, f"the capture wrote outside its own tmp_path: {outside}" + assert outcomes == ["recorded"], outcomes + + def test_the_env_delta_does_not_depend_on_the_recording_environment(monkeypatch, tmp_path) -> None: """The fixture must record what _spawn CONTRIBUTES, not what this host lacked. diff --git a/test/test_acp_runtime.py b/test/test_acp_runtime.py index fc072f179b3b..e2a39078dc7e 100644 --- a/test/test_acp_runtime.py +++ b/test/test_acp_runtime.py @@ -68,6 +68,7 @@ METHOD_SET_MODE, JsonRpcMessage, ) +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION from kiro_crew.mcp_cleanup import KIROCREW_BIN_MCP_SERVERS from kiro_crew.mcp_gateway.claim import STUB_SESSION_TOKEN_ENV from kiro_crew.metrics.events import CHILD_PERMISSION_DENIED @@ -75,6 +76,26 @@ # ── Harness ── +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A runtime start materialises the agent spec (``ensure_agent_materialized`` -> + ``rebuild_agent_config``) and a worker install writes one + (``_install_worker_agent`` -> ``_write_worker_spec``); both end in + ``_write_derived_permissions``, which reads ``installed_kiro_cli_version`` + function-locally from ``kiro_crew.kiro_cli``: one real ``kiro-cli --version`` + spawn per binary identity, process-cached, so whichever test in the worker + writes a spec first pays it against the HOST's install with the checkout as + the child's cwd. Pinned to the floor release, as ``test_agent.py`` and the + generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + @pytest.fixture def kas_readiness_wire(monkeypatch, tmp_path): """Real demux and session startup; only the subprocess and clock are fake.""" diff --git a/test/test_acp_seed_provenance.py b/test/test_acp_seed_provenance.py index b43ee12d5845..78a708970876 100644 --- a/test/test_acp_seed_provenance.py +++ b/test/test_acp_seed_provenance.py @@ -43,6 +43,7 @@ from kiro_crew.acp import seed_provenance as sp from kiro_crew.acp.client import AcpClient from kiro_crew.acp.types import ACP_BACKEND_CLAUDE +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION _SERVED = [ "global.anthropic.claude-opus-5[1m]", @@ -54,6 +55,24 @@ _OWNER = "owner-under-test" +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A client start here materialises the agent spec (``ensure_agent_materialized`` + -> ``rebuild_agent_config`` -> ``_write_derived_permissions``), which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker starts first pays it against the HOST's install + with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + @pytest.fixture(autouse=True) def isolated_records(monkeypatch): """Per-test provenance state. diff --git a/test/test_acp_session_mcp.py b/test/test_acp_session_mcp.py index 95877197a52f..c773dfaea920 100644 --- a/test/test_acp_session_mcp.py +++ b/test/test_acp_session_mcp.py @@ -23,6 +23,7 @@ from kiro_crew.acp import session_mcp from kiro_crew.acp.client import AcpClient from kiro_crew.acp.types import ACP_BACKEND_CLAUDE +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION from kiro_crew.providers.mirrors import claude_code as claude_mirror from kiro_crew.providers.mirrors import registry as mirrors_registry from kiro_crew.providers.mirrors.claude_code import ClaudeCodeMirror @@ -31,6 +32,24 @@ _CRON = {"command": "/opt/kirocrew", "args": ["mcp-cron"]} +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A client start here materialises the agent spec (``ensure_agent_materialized`` + -> ``rebuild_agent_config`` -> ``_write_derived_permissions``), which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker starts first pays it against the HOST's install + with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + @pytest.fixture def agents_dir(tmp_path, monkeypatch): """Point the agent-spec resolver at a temp agents directory.""" diff --git a/test/test_acp_spawn_offload.py b/test/test_acp_spawn_offload.py index a64e13164e42..0350e3064d80 100644 --- a/test/test_acp_spawn_offload.py +++ b/test/test_acp_spawn_offload.py @@ -39,6 +39,25 @@ import kiro_crew.acp.runtime as runtime_mod from kiro_crew.acp.client import AcpClient, _resolve_spawn_env from kiro_crew.acp.runtime import AcpRuntime +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION + + +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + A client start here materialises the agent spec (``ensure_agent_materialized`` + -> ``rebuild_agent_config`` -> ``_write_derived_permissions``), which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker starts first pays it against the HOST's install + with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) @pytest.fixture(autouse=True) diff --git a/test/test_agent.py b/test/test_agent.py index 6341a6c24868..94848f251f73 100644 --- a/test/test_agent.py +++ b/test/test_agent.py @@ -29,6 +29,26 @@ from kiro_crew import agent_state from kiro_crew import atomic_write as aw from kiro_crew.agent import _MANAGED_MCP_ENTRY_KEYS, install_agent, migrate_agent_specs +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION + + +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + Every spec write here reaches ``_write_derived_permissions``, which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker writes a spec first pays it -- against the + HOST's install, with the checkout as the child's cwd -- and that host decides + whether the block is written at all (CI has no binary and reads "refuse"). + Pinned to the floor release, the same accepting arm ``test_agent_capabilities`` + and the generated-writer suites pin, so no test here reaches the binary. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) def _reject_json_constant(name: str): # pragma: no cover - raises by design diff --git a/test/test_browser_cli_view.py b/test/test_browser_cli_view.py index 83035885b69f..ca91ad636e40 100644 --- a/test/test_browser_cli_view.py +++ b/test/test_browser_cli_view.py @@ -2212,14 +2212,11 @@ def test_blind_lookup_squatter_is_not_adopted_without_child_binding_proof( lambda pid, port: None, raising=False, ) - monkeypatch.setattr(platform_compat, "listening_pid_tool_available", lambda: True) - monkeypatch.setattr(platform_compat, "find_port_listeners", lambda port: []) - monkeypatch.setattr( - platform_compat, - "probe_port_listeners", - lambda port: ([], True), - raising=False, - ) + # The shared stub, not a hand-rolled copy of its three listener pins: the copy + # dropped ``process_descendant_identities``, so every one of the 120 readiness + # rounds ran two real ``ps`` snapshots against pid 4242 -- a host process this + # test does not own -- and the fake-clock loop took 20 real seconds. + _stub_port_owner(monkeypatch, listener_pids=()) assert mod.ensure_running() is None assert mod._info is None @@ -2250,14 +2247,7 @@ def test_blind_lookup_adopts_real_child_after_recognized_binding_report( lambda pid, port: None, raising=False, ) - monkeypatch.setattr(platform_compat, "listening_pid_tool_available", lambda: True) - monkeypatch.setattr(platform_compat, "find_port_listeners", lambda port: []) - monkeypatch.setattr( - platform_compat, - "probe_port_listeners", - lambda port: ([], True), - raising=False, - ) + _stub_port_owner(monkeypatch, listener_pids=()) with caplog.at_level("WARNING"): info = mod.ensure_running() diff --git a/test/test_cli.py b/test/test_cli.py index 8b326e0fa014..534032937cbc 100644 --- a/test/test_cli.py +++ b/test/test_cli.py @@ -153,6 +153,25 @@ def _pristine() -> KiroCrewConfig: monkeypatch.setattr(KiroCrewConfig, "load_credentials", lambda self: {}) +@pytest.fixture(autouse=True) +def _doctor_reads_the_warm_probe_cache(monkeypatch): + """The doctor's blocking ``warm_backend()`` is a no-op here. + + ``_doctor()`` calls ``sandbox.warm_backend()`` before its MCP probes so a fresh + CLI process has a settled backend verdict before anything spawns on the loop. + In this suite the rootdir conftest already keeps ``sandbox._backend`` warm for + every test (``pytest_runtest_setup``), yet the warm thread re-runs the real + probe unconditionally -- a fork plus a ``sys.executable`` spawn per doctor + test, 43 of them in this file, all against a verdict the conftest already + holds. The doctor then reads the cache the conftest filled, which is the same + thing a warmed boot gives it. Nothing in this file tests the warm itself + (``test_sandbox_backend_cache.py`` does). + """ + import kiro_crew.cli_doctor as _doc + + monkeypatch.setattr(_doc, "warm_backend", lambda timeout=None: None) + + class TestDoctor: @pytest.fixture(autouse=True) def _hermetic_config(self, monkeypatch): @@ -6475,10 +6494,15 @@ class TestInstallPidfdChildWatcher: """Verify _install_child_watcher's platform behavior.""" @staticmethod - def _install_then_spawn_in_child(expected_watcher: str) -> None: + def _install_then_spawn_in_child(expected_watcher: str, cwd: Path) -> None: """Run "_install_child_watcher() then asyncio.run(subprocess)" in a CLEAN child Python process, and assert it exits 0. + *cwd* is the caller's ``tmp_path``: the child imports ``kiro_crew`` off the + propagated ``PYTHONPATH``, never off its working directory, so the one + thing the process cwd could still do is let a ``-c`` child run inside the + developer's checkout. + Why a subprocess instead of an in-process ``asyncio.run``: on CPython 3.10 the child watcher is bound to the loop inside asyncio's ``set_event_loop()``, which only calls ``attach_loop`` when @@ -6523,6 +6547,7 @@ async def _spawn_true(): text=True, timeout=60, env=env, + cwd=cwd, ) assert result.returncode == 0, ( f"install-before-run child failed (rc={result.returncode}):\n" @@ -6652,7 +6677,7 @@ def _ctor_must_not_run() -> object: ) @pytest.mark.skipif(sys.platform != "linux", reason="pidfd watcher is Linux-only") - def test_real_subprocess_works_after_install_on_linux(self) -> None: + def test_real_subprocess_works_after_install_on_linux(self, tmp_path: Path) -> None: """End-to-end: after installing the watcher the way the gateway does (before asyncio.run, on the main thread), asyncio subprocess support must still work. This is the property the mocked test above cannot prove — it @@ -6677,13 +6702,15 @@ def test_real_subprocess_works_after_install_on_linux(self) -> None: expected = "SafeChildWatcher" else: expected = "PidfdChildWatcher" - self._install_then_spawn_in_child(expected_watcher=expected) + self._install_then_spawn_in_child(expected_watcher=expected, cwd=tmp_path) @pytest.mark.skipif( sys.platform == "linux" or not hasattr(__import__("asyncio"), "SafeChildWatcher"), reason="exercises the real macOS SafeChildWatcher install (non-Linux Unix, 3.10-3.13)", ) - def test_real_subprocess_works_after_safe_watcher_install_on_macos(self) -> None: + def test_real_subprocess_works_after_safe_watcher_install_on_macos( + self, tmp_path: Path + ) -> None: """End-to-end on macOS: after the REAL _install_child_watcher() installs SafeChildWatcher the way the gateway does (before asyncio.run, on the main thread), asyncio subprocess support must still work. @@ -6699,7 +6726,7 @@ def test_real_subprocess_works_after_safe_watcher_install_on_macos(self) -> None is itself main-thread-only, so an in-process run under a non-main pytest-xdist worker thread would fail spuriously. """ - self._install_then_spawn_in_child(expected_watcher="SafeChildWatcher") + self._install_then_spawn_in_child(expected_watcher="SafeChildWatcher", cwd=tmp_path) class TestChildWatcherApiRemoved: diff --git a/test/test_cli_manifest_signature.py b/test/test_cli_manifest_signature.py index ca72dc4c2811..18c55e3cd331 100644 --- a/test/test_cli_manifest_signature.py +++ b/test/test_cli_manifest_signature.py @@ -21,6 +21,8 @@ from installer_test_helpers import run_bounded from skill_script_helpers import load_skill_script +from kiro_crew import platform_compat + ROOT = Path(__file__).resolve().parents[1] HELPER = ROOT / "packaging" / "signing" / "cli-manifest.py" INSTALLER = ROOT / "cli.sh" @@ -670,11 +672,12 @@ def test_a_wedged_grandchild_does_not_outlive_the_bounded_run(tmp_path: Path) -> with pytest.raises(subprocess.TimeoutExpired): run_bounded(["sh", str(script), str(pidfile)], os.environ.copy(), 5.0, cwd=str(tmp_path)) pid = int(pidfile.read_text(encoding="utf-8").strip()) + # Liveness through the repo's own probe (AGENTS.md "Cross-platform"): a raw + # ``os.kill(pid, 0)`` is a POSIX idiom that TERMINATES the target on Windows, + # and the sweep's caller filter recognises only the sanctioned helper. deadline = time.monotonic() + 5.0 while time.monotonic() < deadline: - try: - os.kill(pid, 0) - except ProcessLookupError: + if not platform_compat.pid_exists(pid): return time.sleep(0.05) pytest.fail(f"grandchild {pid} survived the bounded run") diff --git a/test/test_codex_session_mcp.py b/test/test_codex_session_mcp.py index 7ee3551e9b68..4523132f5c32 100644 --- a/test/test_codex_session_mcp.py +++ b/test/test_codex_session_mcp.py @@ -1848,13 +1848,27 @@ def test_the_driver_runner_reaps_descendants_on_the_timeout_path(): Asserted on the GRANDCHILD, because a parent-only kill is the bug -- the parent dies either way. + + The grandchild is known here only as a NUMBER the driver printed, and by the + time this process reads it the driver's group has been SIGKILLed: on the + passing path init has already collected the grandchild and the kernel is free + to hand its number to a stranger. So the driver reports the grandchild's + start-time identity alongside the pid, read through the repo's own helper at + the one moment the pid is provably ours, and "gone" below means "no process + with THAT identity" -- a reissued number is a stranger, neither a leak nor a + kill target. The failure-path kill is pinned to the same identity. """ - parent = r""" -import subprocess, sys, time -g = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(300)"]) -print(g.pid, flush=True) -time.sleep(300) -""" + from kiro_crew import platform_compat + + src_root = Path(platform_compat.__file__).resolve().parents[1] + parent = ( + "import subprocess, sys, time\n" + f"sys.path.insert(0, {str(src_root)!r})\n" + "from kiro_crew.platform_compat import get_process_start_id\n" + 'g = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(300)"])\n' + "print(g.pid, get_process_start_id(g.pid) or '', flush=True)\n" + "time.sleep(300)\n" + ) with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as w: script = Path(w) / "parent.py" script.write_text(parent, encoding="utf-8") @@ -1862,18 +1876,30 @@ def test_the_driver_runner_reaps_descendants_on_the_timeout_path(): result = _run_driver_reaping_group([sys.executable, str(script)], timeout=5, cwd=w) # The bound is the control here, and the reap must not add a long second wait. assert time.monotonic() - started < 90 - grandchild = int((result.stdout or "").strip().splitlines()[0]) - + reported = (result.stdout or "").strip().splitlines()[0].split() + grandchild = int(reported[0]) + assert len(reported) == 2, ( + f"the driver could not read the start-time identity of grandchild " + f"{grandchild}, so neither the liveness check nor the failure-path kill " + "below could be pinned to the process it spawned" + ) + grandchild_identity = reported[1] + + # Liveness through the repo's own identity helper (AGENTS.md "Cross-platform"): + # a raw ``os.kill(pid, 0)`` is a POSIX idiom that TERMINATES the target on + # Windows, the sweep's caller filter recognises only the sanctioned helpers, + # and a bare existence probe cannot tell our reaped grandchild's reissued + # number from the grandchild itself. deadline = time.monotonic() + 10 while time.monotonic() < deadline: - try: - os.kill(grandchild, 0) - except OSError: + if platform_compat.get_process_start_id(grandchild) != grandchild_identity: break time.sleep(0.2) else: # pragma: no cover - the failure this test exists to catch try: - os.kill(grandchild, 9) + platform_compat.kill_pid_pinned( + grandchild, grandchild_identity, platform_compat.SIGKILL + ) except OSError: pass pytest.fail( diff --git a/test/test_computer_use_registration.py b/test/test_computer_use_registration.py index e3eb7ed4b005..d07621678d69 100644 --- a/test/test_computer_use_registration.py +++ b/test/test_computer_use_registration.py @@ -38,8 +38,27 @@ from kiro_crew import agent, agent_state, mcp_cleanup, mcp_discovery, onboarding_import from kiro_crew.agent import install_agent +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION from kiro_crew.platform_compat import IS_POSIX + +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + Every ``install_agent`` here ends in ``_write_derived_permissions``, which + reads ``installed_kiro_cli_version`` function-locally from + ``kiro_crew.kiro_cli``: one real ``kiro-cli --version`` spawn per binary + identity, process-cached, so whichever test in the worker installs first pays + it against the HOST's install with the checkout as the child's cwd. Pinned to + the floor release, as ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + CU_SERVER = "kirocrew-computer" CU_REF = f"@{CU_SERVER}" CU_SUBCOMMAND = "mcp-computer" diff --git a/test/test_connections_tool_aliases.py b/test/test_connections_tool_aliases.py index f6e63dd281a3..f459d0813dc1 100644 --- a/test/test_connections_tool_aliases.py +++ b/test/test_connections_tool_aliases.py @@ -49,6 +49,26 @@ resolve_tool_aliases, statically_visible_tool_names, ) +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION + + +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + The real-rebuild tests below drive ``rebuild_agent_config``, which ends in + ``_write_derived_permissions`` and reads ``installed_kiro_cli_version`` + function-locally from ``kiro_crew.kiro_cli``: one real ``kiro-cli --version`` + spawn per binary identity, process-cached, so whichever test in the worker + rebuilds first pays it against the HOST's install with the checkout as the + child's cwd. Pinned to the floor release, as ``test_agent.py`` and the + generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + URLS = { "github": "https://api.githubcopilot.com/mcp/", diff --git a/test/test_dashboard_server_startup_coverage.py b/test/test_dashboard_server_startup_coverage.py index dc3e5384219e..f7844d766846 100644 --- a/test/test_dashboard_server_startup_coverage.py +++ b/test/test_dashboard_server_startup_coverage.py @@ -287,6 +287,16 @@ async def _start_dashboard(tmp_path: Path, monkeypatch, **kwargs: Any) -> Any: monkeypatch.setattr(srv, "data_home", lambda: tmp_path) monkeypatch.setattr(_st, "config_dir", lambda: tmp_path) monkeypatch.setattr(_loader, "config_dir", lambda: tmp_path) + # The boot calls ``init_hooks_system`` with this module's MagicMock cron + # service and binds the result to two process globals nothing unbinds; a + # later module on the same worker that revokes an app trust would then + # await that MagicMock (the eleventh sweep's ten flaky 409s). Pin them + # here so monkeypatch puts back what this test found -- the leaker's own + # contract, alongside the rootdir conftest's snapshot/restore floor. + from kiro_crew.apps import hooks_integration as _hooks + + monkeypatch.setattr(_hooks, "_lifecycle_dispatcher", None) + monkeypatch.setattr(_hooks, "_route_registry", None) # POSIX-only extra transport; irrelevant to the wiring under test and it # would bind a real socket in the data home. monkeypatch.setattr(srv, "_start_unix_site", AsyncMock(return_value=None)) @@ -435,6 +445,27 @@ async def _cancel_stray_tasks() -> None: await asyncio.gather(*stray, return_exceptions=True) +def _record_states(monkeypatch) -> list[Any]: + """Record every ``DashboardState`` the boot builds, for a boot that raises. + + A failing ``start_dashboard`` never returns its state, yet by the time it + fails it has already opened the knowledge store's connections (the route + registration reads the lazy property), and production releases those by + exiting. Handing the recorded state to :func:`_release_process_handles` + is what lets a failure-path test close them. + """ + built: list[Any] = [] + real_state = srv.DashboardState + + def _recording(*args: Any, **kwargs: Any) -> Any: + state = real_state(*args, **kwargs) + built.append(state) + return state + + monkeypatch.setattr(srv, "DashboardState", _recording) + return built + + class TestReserveDashboardPort: @staticmethod def _assert_kernel_confirms_listening(sock: socket.socket) -> None: @@ -716,7 +747,7 @@ async def test_backends_spawn_with_the_reserved_ports_evidence_exported( sessions.remove = AsyncMock() sessions.get_pid = MagicMock(return_value=None) sessions.any_active_turn = MagicMock(return_value=False) - runner, _state = await srv.start_dashboard( + runner, state = await srv.start_dashboard( sessions=sessions, crons=MagicMock( list_jobs=MagicMock(return_value=[]), @@ -735,6 +766,7 @@ async def test_backends_spawn_with_the_reserved_ports_evidence_exported( finally: await runner.cleanup() await _cancel_stray_tasks() + _release_process_handles(state) @pytest.mark.asyncio async def test_ipv6_loopback_bind_exports_v6_host_evidence(self, tmp_path, monkeypatch) -> None: @@ -782,7 +814,7 @@ async def test_ipv6_loopback_bind_exports_v6_host_evidence(self, tmp_path, monke sessions.remove = AsyncMock() sessions.get_pid = MagicMock(return_value=None) sessions.any_active_turn = MagicMock(return_value=False) - runner, _state = await srv.start_dashboard( + runner, state = await srv.start_dashboard( sessions=sessions, crons=MagicMock( list_jobs=MagicMock(return_value=[]), @@ -800,6 +832,7 @@ async def test_ipv6_loopback_bind_exports_v6_host_evidence(self, tmp_path, monke finally: await runner.cleanup() await _cancel_stray_tasks() + _release_process_handles(state) @pytest.mark.asyncio async def test_a_failed_port_reservation_spawns_no_backends( @@ -998,6 +1031,7 @@ async def test_a_failed_listen_stops_the_spawned_backends(self, tmp_path, monkey ) monkeypatch.setattr(srv.web, "SockSite", MagicMock(side_effect=RuntimeError("listen boom"))) spies = _neutralise_outside_process_work(monkeypatch) + states = _record_states(monkeypatch) for _leak_key in ( browser_cli_snapshots.OUTPUT_DIR_ENV, browser_cli_token.TOKEN_ENV, @@ -1031,6 +1065,8 @@ async def test_a_failed_listen_stops_the_spawned_backends(self, tmp_path, monkey spies["on_gateway_shutdown"].assert_awaited() finally: await _cancel_stray_tasks() + for state in states: + _release_process_handles(state) @pytest.mark.asyncio async def test_the_app_is_wired_and_reports_ready(self, tmp_path, monkeypatch) -> None: diff --git a/test/test_decisions_memory_recall_reachable.py b/test/test_decisions_memory_recall_reachable.py index 3efe79ee695a..66b67d3e9cc0 100644 --- a/test/test_decisions_memory_recall_reachable.py +++ b/test/test_decisions_memory_recall_reachable.py @@ -89,8 +89,13 @@ async def ask(self, state, questions): @pytest.fixture -def wired(tmp_path, monkeypatch): - """A consented keystone, a live config, a seeded store, and a surfaced session.""" +def wired(tmp_path, monkeypatch, opened): + """A consented keystone, a live config, a seeded store, and a surfaced session. + + The store goes through the rootdir conftest's ``opened`` register-and-close + fixture: a dropped ``VectorMemoryStore`` keeps its ``db``/``-wal``/``-shm`` + descriptors until the cyclic collector runs. + """ _Oracle.asked = [] keystone = tmp_path / "decisions_consent.json" @@ -119,7 +124,7 @@ def wired(tmp_path, monkeypatch): "kiro_crew.decisions.capability.is_decisions_denied", lambda *_a, **_kw: False ) - store = VectorMemoryStore(db_path=tmp_path / "mem.db") + store = opened(VectorMemoryStore(db_path=tmp_path / "mem.db")) store.init() _seed(store) # The embedder the real path would use, pinned so the query vector is the one the diff --git a/test/test_issue_radar_crew_runtime.py b/test/test_issue_radar_crew_runtime.py index 173160abd1e7..268a630af002 100644 --- a/test/test_issue_radar_crew_runtime.py +++ b/test/test_issue_radar_crew_runtime.py @@ -236,26 +236,31 @@ def _crew(root, name="Andromeda", **spec) -> dict[str, Any]: _UNITS: dict[str, str] = {} -@pytest.fixture(autouse=True, scope="module") -def _isolated_crew_log(): - """One crew log data home for the module, crew log on. - - Module-scoped because the seeding helper below is called from unittest classes - that manage their own store roots; crews are minted with unique ids, so their - units never collide, and the fold cache is keyed by crew. +@pytest.fixture(autouse=True) +def _isolated_crew_log(monkeypatch): + """Crew log on, in this test's own data home, and no warm state carried over. + + FUNCTION-scoped: the data home is the rootdir conftest's per-test + ``KIROCREW_HOME`` pin, so the crew log a test writes lands in a directory that + test alone owns, and ``KIROCREW_CREW_LOG`` is set through ``monkeypatch`` so it + is gone the moment the test is. A module-scoped ``patch.dict`` here set both + keys for the whole worker between tests: every other suite the worker ran + after this file's first test saw a crew log switched on that none of them + asked for, and a data home none of them pinned. The seeding helpers below are + called from unittest classes that manage their own store roots; a crew's id is + minted fresh per test, so ``_UNITS`` is cleared with the rest. """ - with tempfile.TemporaryDirectory() as home: - with mock.patch.dict(os.environ, {"KIROCREW_HOME": home, "KIROCREW_CREW_LOG": "1"}): - from kiro_crew.crew_log import emit as crew_log_emit - - crew_log_emit.reset_caches() - crew_log_projection.forget_slot_folds() - _UNITS.clear() - yield - crew_log_emit.drain_for_shutdown(timeout=2.0) - crew_log_emit.reset_caches() - crew_log_projection.forget_slot_folds() - _UNITS.clear() + from kiro_crew.crew_log import emit as crew_log_emit + + monkeypatch.setenv("KIROCREW_CREW_LOG", "1") + crew_log_emit.reset_caches() + crew_log_projection.forget_slot_folds() + _UNITS.clear() + yield + crew_log_emit.drain_for_shutdown(timeout=2.0) + crew_log_emit.reset_caches() + crew_log_projection.forget_slot_folds() + _UNITS.clear() def _unit_for(crew_id: str) -> str: diff --git a/test/test_knowledge_add_source.py b/test/test_knowledge_add_source.py index d9ad36c2d7a4..a36565e76f82 100644 --- a/test/test_knowledge_add_source.py +++ b/test/test_knowledge_add_source.py @@ -21,10 +21,14 @@ @pytest.fixture() -def store(tmp_path): - s = KnowledgeStore(str(tmp_path / "test.db")) - yield s - s.close() +def store(tmp_path, opened): + """A store closed on EVERY thread at teardown (rootdir conftest ``opened``). + + ``add_source`` claims and ingests off the loop, so the handler opens a second + per-thread connection on a worker; ``close()`` releases only the calling + thread's and left that one to the cyclic collector. + """ + return opened(KnowledgeStore(str(tmp_path / "test.db"))) def _make_app(store, pipeline=None): diff --git a/test/test_knowledge_sync_outcome_serialization.py b/test/test_knowledge_sync_outcome_serialization.py index 53d76930efb7..00f945fc62b8 100644 --- a/test/test_knowledge_sync_outcome_serialization.py +++ b/test/test_knowledge_sync_outcome_serialization.py @@ -47,11 +47,12 @@ async def off(fn, *args, **kwargs): @pytest.fixture() -def strict_store(monkeypatch, tmp_path): +def strict_store(monkeypatch, tmp_path, opened): + """Every take here runs on a worker thread, so the store holds one connection + per thread that touched it; the rootdir conftest's ``opened`` closes them + all, where ``close()`` would release only the loop thread's (usually none).""" monkeypatch.setenv(STORE_STRICT_ENV, "1") - store = KnowledgeStore(str(tmp_path / "knowledge.db")) - yield store - store.close() + return opened(KnowledgeStore(str(tmp_path / "knowledge.db"))) def _pipeline(store: KnowledgeStore, reader_meta: dict | None = None) -> IngestionPipeline: diff --git a/test/test_local_gate.py b/test/test_local_gate.py index c579b151bea7..f398ff0b4952 100644 --- a/test/test_local_gate.py +++ b/test/test_local_gate.py @@ -656,3 +656,21 @@ def test_dry_run_against_the_real_repo_never_plans_a_full_suite(gate, monkeypatc # Every rc==0 plan names CI as the full suite's owner, in one of two # spellings depending on whether there was anything related to select. assert "full suite deferred to CI" in err or "the full suite runs in CI" in err, err + + +def test_the_reference_scan_reads_each_test_file_once_and_drops_it(gate) -> None: + """The selector streams the test tree; it must not memoise what it read. + + ``related_targets`` reads every collectable test file once per call to match + it against the diff's tokens. An earlier revision wrapped that read in + ``functools.lru_cache(maxsize=None)``, so the whole tree -- ~3,200 files, + ~100 MB of ``str`` -- stayed resident for the life of whichever process had + imported the selector: the gate itself, or the pytest worker running the + end-to-end dry run above (+190 MiB high-water, outliving the test). The + re-read it saved is 0.4 s. Pinned by shape: the read seam is a plain function. + """ + selector = importlib.import_module("run_scoped_tests") + assert not hasattr(selector._read_text, "cache_info"), ( + "run_scoped_tests._read_text is memoised again; the reference scan must " + "read each file once and drop it, not retain the test tree's text" + ) diff --git a/test/test_mcp_gateway_pool_integ.py b/test/test_mcp_gateway_pool_integ.py index 4afb9d4fa279..d67f83edd452 100644 --- a/test/test_mcp_gateway_pool_integ.py +++ b/test/test_mcp_gateway_pool_integ.py @@ -39,11 +39,13 @@ import asyncio import json import sys +from collections.abc import Iterator from pathlib import Path import pytest from fake_pool_mcp_server import recorded +from kiro_crew import code_fingerprint as cf from kiro_crew import platform_compat as pc from kiro_crew.mcp_gateway import gatewayd as gw from kiro_crew.mcp_gateway import transport @@ -57,6 +59,27 @@ _REPLY_TIMEOUT = 60.0 +@pytest.fixture(autouse=True) +def _no_git_fingerprint(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: + """``run_gatewayd`` warms ``code_fingerprint()`` in-process without a git spawn. + + The daemon these tests drive calls ``warm_code_fingerprint()`` at start, and + the cached fingerprint is of ``_PACKAGE_ROOT`` -- THIS checkout -- so a cold + read runs ``git -C rev-parse HEAD`` and ``git diff HEAD`` on the + developer's own repository with the worker's cwd inherited; which test pays + depends on run order. Nothing here reads the value (the stubs' target is the + fake server, not a Kiro Crew subcommand, so no stub folds it into its pool + key), so the trusted-git resolver is pinned to "absent" -- the product's own + no-spawn arm, the mtime rule -- exactly as ``test_mcp_gateway_daemon_lifecycle.py`` + does. The cache is cleared on both sides so the pinned value neither reuses + a real one nor leaks to a module that must match a child daemon's. + """ + monkeypatch.setattr(cf, "trusted_git_bin", lambda: None) + cf.code_fingerprint.cache_clear() + yield + cf.code_fingerprint.cache_clear() + + def _endpoint_dir() -> str | None: """Where to put the test endpoint. diff --git a/test/test_mcp_gateway_transport.py b/test/test_mcp_gateway_transport.py index f7b6e8c7dbb8..6216253833fa 100644 --- a/test/test_mcp_gateway_transport.py +++ b/test/test_mcp_gateway_transport.py @@ -28,9 +28,33 @@ import pytest from tmpdir_helpers import SHORT_TMP_PREFIX, short_tmp_base +from kiro_crew import code_fingerprint as cf from kiro_crew import platform_compat as pc from kiro_crew.mcp_gateway import transport + +@pytest.fixture(autouse=True) +def _no_git_fingerprint(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]: + """``code_fingerprint()`` without a git spawn, per test, cache isolated. + + ``GatewayManager._start_locked`` and ``run_gatewayd`` both warm + ``code_fingerprint()`` before ``prepare_dir``, and the cached fingerprint is + of ``_PACKAGE_ROOT`` -- THIS checkout -- so a cold read runs + ``git -C rev-parse HEAD`` and ``git diff HEAD`` on the developer's + own repository from a pool thread with the worker's cwd inherited; which test + pays depends on run order (whoever reads first after a ``cache_clear``). + Nothing in this module reads the value, so the trusted-git resolver is pinned + to "absent" -- the product's own no-spawn arm, the mtime rule -- exactly as + ``test_mcp_gateway_daemon_lifecycle.py`` does. The cache is cleared on both + sides so the pinned value neither reuses a real one nor leaks to a module + that must match a child daemon's real fingerprint. + """ + monkeypatch.setattr(cf, "trusted_git_bin", lambda: None) + cf.code_fingerprint.cache_clear() + yield + cf.code_fingerprint.cache_clear() + + # --- Address resolution ------------------------------------------------------ diff --git a/test/test_mcp_rebuild_reconsumption.py b/test/test_mcp_rebuild_reconsumption.py index 164af18e0526..698f61e17847 100644 --- a/test/test_mcp_rebuild_reconsumption.py +++ b/test/test_mcp_rebuild_reconsumption.py @@ -24,9 +24,27 @@ from mcp_merge_helpers import bundled_defaults as _bundled_defaults from mcp_merge_helpers import run_install_mcp_merge as _run_install_mcp_merge +from kiro_crew.kiro_cli import SPEC_PERMISSIONS_MIN_VERSION from kiro_crew.mcp_provenance import DERIVED_KEY +@pytest.fixture(autouse=True) +def _pinned_kiro_cli_version(monkeypatch): + """Pin the kiro-cli release the spec ``permissions`` gate believes is installed. + + Every rebuild here ends in ``_write_derived_permissions``, which reads + ``installed_kiro_cli_version`` function-locally from ``kiro_crew.kiro_cli``: + one real ``kiro-cli --version`` spawn per binary identity, process-cached, so + whichever test in the worker rebuilds first pays it against the HOST's install + with the checkout as the child's cwd. Pinned to the floor release, as + ``test_agent.py`` and the generated-writer suites pin it. + """ + monkeypatch.setattr( + "kiro_crew.kiro_cli.installed_kiro_cli_version", + lambda: SPEC_PERMISSIONS_MIN_VERSION, + ) + + def _emitted(tmp_path: Path, cfg_dir: Path, kiro_servers: dict, **kw) -> dict: return _run_install_mcp_merge( tmp_path, cfg_dir, cc_servers={}, kiro_servers=kiro_servers, **kw diff --git a/test/test_member_essential_context.py b/test/test_member_essential_context.py index 1fb4b8cb88ce..76d77cf00e2c 100644 --- a/test/test_member_essential_context.py +++ b/test/test_member_essential_context.py @@ -26,6 +26,11 @@ from kiro_crew.skills import SkillsLoader +@pytest.fixture(autouse=True) +def _close_skills_loaders(close_skills_loaders): + """The ``env`` fixture builds a ``SkillsLoader`` it never closes: close it (rootdir conftest).""" + + @pytest.fixture def env(tmp_path, monkeypatch): home = tmp_path / "host-home" diff --git a/test/test_memory_on_demand.py b/test/test_memory_on_demand.py index 99e1adf06936..620b18f2d89b 100644 --- a/test/test_memory_on_demand.py +++ b/test/test_memory_on_demand.py @@ -14,6 +14,13 @@ env = _member_env +@pytest.fixture(autouse=True) +def _close_skills_loaders(close_skills_loaders): + """Every test here builds a ``ContextBuilder`` (``env`` included): close its + ``SkillsLoader`` (rootdir conftest) -- the skill search index is a SQLite + connection nothing else closes.""" + + def test_v2_prompt_lifecycles_leave_retrieval_to_the_tool(env, monkeypatch): memory = env.memory memory.write_preferences("Use a concise reply.") @@ -122,14 +129,14 @@ def test_withheld_memory_does_not_advertise_automatic_recall(tmp_path, options): assert "Facts and past experiences are not searched automatically" not in message -def test_v1_new_session_bounds_pref_rows_at_the_startup_cap(tmp_path): +def test_v1_new_session_bounds_pref_rows_at_the_startup_cap(tmp_path, opened): """The cap must reach the store through the real plumbing (context._ResolvedCaps -> memory.get_context -> get_preferences_context). Mocks cannot see a dropped keyword argument; a real store over the cap can.""" from kiro_crew.context import _PREFS_STARTUP_CAP from kiro_crew.vector_memory import VectorMemoryStore - vectors = VectorMemoryStore(db_path=tmp_path / "mem.db") + vectors = opened(VectorMemoryStore(db_path=tmp_path / "mem.db")) vectors.init() for i in range(60): vectors.set_semantic(f"pref.rule_{i:02d}", f"standing rule {i} " * 25, 0.9, "user_explicit") diff --git a/test/test_opencode_session_mcp.py b/test/test_opencode_session_mcp.py index 716da3f07aec..e9aa08eae0f9 100644 --- a/test/test_opencode_session_mcp.py +++ b/test/test_opencode_session_mcp.py @@ -1107,34 +1107,55 @@ def test_the_driver_runner_reaps_descendants_on_the_timeout_path(): Asserted on the GRANDCHILD, because a parent-only kill is the bug -- the parent dies either way. + + The grandchild is known here only as a NUMBER the driver printed, and by the + time this process reads it the driver's group has been SIGKILLed: on the + passing path init has already collected the grandchild and the kernel is free + to hand its number to a stranger. So the driver reports the grandchild's + start-time identity alongside the pid, read through the repo's own helper at + the one moment the pid is provably ours, and "gone" below means "no process + with THAT identity" -- a reissued number is a stranger, neither a leak nor a + kill target. The failure-path kill is pinned to the same identity. """ - parent = r""" -import subprocess, sys, time -g = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(300)"]) -print(g.pid, flush=True) -time.sleep(300) -""" + src_root = Path(platform_compat.__file__).resolve().parents[1] + parent = ( + "import subprocess, sys, time\n" + f"sys.path.insert(0, {str(src_root)!r})\n" + "from kiro_crew.platform_compat import get_process_start_id\n" + 'g = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(300)"])\n' + "print(g.pid, get_process_start_id(g.pid) or '', flush=True)\n" + "time.sleep(300)\n" + ) with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as w: script = Path(w) / "parent.py" script.write_text(parent, encoding="utf-8") started = time.monotonic() result = _run_driver_reaping_group([sys.executable, str(script)], timeout=5, cwd=w) assert time.monotonic() - started < 90 - grandchild = int((result.stdout or "").strip().splitlines()[0]) + reported = (result.stdout or "").strip().splitlines()[0].split() + grandchild = int(reported[0]) + assert len(reported) == 2, ( + f"the driver could not read the start-time identity of grandchild " + f"{grandchild}, so neither the liveness check nor the failure-path kill " + "below could be pinned to the process it spawned" + ) + grandchild_identity = reported[1] - # Liveness through the repo's own probe (AGENTS.md "Cross-platform"): a raw - # ``os.kill(pid, 0)`` is a POSIX idiom that TERMINATES the target on Windows, - # and the sweep's caller filter recognises only the sanctioned helper. - # ``PID_UNSIGNALABLE`` counts as gone, like a bare ``OSError``: the grandchild - # was ours, so a number this process cannot signal is not it. + # Liveness through the repo's own identity helper (AGENTS.md "Cross-platform"): + # a raw ``os.kill(pid, 0)`` is a POSIX idiom that TERMINATES the target on + # Windows, the sweep's caller filter recognises only the sanctioned helpers, + # and a bare existence probe cannot tell our reaped grandchild's reissued + # number from the grandchild itself. deadline = time.monotonic() + 10 while time.monotonic() < deadline: - if platform_compat.pid_liveness(grandchild) != platform_compat.PID_ALIVE: + if platform_compat.get_process_start_id(grandchild) != grandchild_identity: break time.sleep(0.2) else: # pragma: no cover - the failure this test exists to catch try: - os.kill(grandchild, 9) + platform_compat.kill_pid_pinned( + grandchild, grandchild_identity, platform_compat.SIGKILL + ) except OSError: pass pytest.fail( diff --git a/test/test_pdf_extract.py b/test/test_pdf_extract.py index 458e4aead752..7514841aa988 100644 --- a/test/test_pdf_extract.py +++ b/test/test_pdf_extract.py @@ -13,6 +13,7 @@ import json import subprocess import sys +import textwrap import time import pytest @@ -155,7 +156,8 @@ def test_non_positive_caps_are_a_programming_error(self): class TestRssWatchdog: - """The child's own peak-RSS ceiling: the bound where the kernel has none (macOS).""" + """The child's own peak-RSS ceiling: the bound where the kernel has none (macOS), + sampled from the child's OWN footprint -- on Linux ``ru_maxrss`` is the parent's.""" def test_watchdog_reports_memory_and_ends_the_process(self, capsys): samples = iter([100, 200, 2_000_000_001]) @@ -180,6 +182,116 @@ def test_peak_rss_is_bytes_on_every_platform(self): # This test process is at least a few MB resident; KiB misread as bytes would not be. assert peak > 4 * 1024 * 1024 + def test_linux_samples_its_own_high_water_mark_never_getrusage(self, tmp_path, monkeypatch): + """On Linux ``execve`` hands the child the PARENT's ``ru_maxrss``, so the + sample comes from the child's own ``VmHWM`` and ``getrusage`` is not + consulted at all -- a gateway past the ceiling would otherwise lose every + child on the watchdog's first tick.""" + status = tmp_path / "status" + status.write_text( + "Name:\tpython\nVmPeak:\t 1400000 kB\nVmHWM:\t 11432 kB\nVmRSS:\t 11432 kB\n", + encoding="utf-8", + ) + + class InheritedRusage: + @staticmethod + def getrusage(_who): + raise AssertionError("ru_maxrss consulted on linux: that is the parent's peak") + + RUSAGE_SELF = 0 + + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr(pdf_extract_child, "_resource", InheritedRusage) + monkeypatch.setattr(pdf_extract_child, "_LINUX_STATUS_PATH", str(status)) + assert pdf_extract_child.peak_rss_bytes() == 11432 * 1024 + # No /proc is unreadable, not a breach and not a fall-back to the inherited number. + monkeypatch.setattr(pdf_extract_child, "_LINUX_STATUS_PATH", str(tmp_path / "gone")) + assert pdf_extract_child.peak_rss_bytes() is None + + @pytest.mark.parametrize( + "status", + [ + "Name:\tpython\nVmRSS:\t 11432 kB\n", # no high-water field at all + "VmHWM:\t 11432 MB\n", # a unit the kernel never prints + "VmHWM:\t lots kB\n", + "VmHWM:\n", + "", + ], + ) + def test_an_unreadable_status_is_none_not_a_guess(self, status): + assert pdf_extract_child._peak_rss_from_status(status) is None + + @pytest.mark.parametrize( + ("platform", "ru_maxrss", "expected"), + [("darwin", 123_456_789, 123_456_789), ("freebsd13", 11432, 11432 * 1024)], + ) + def test_off_linux_getrusage_is_read_in_the_platforms_unit( + self, monkeypatch, platform, ru_maxrss, expected + ): + class Rusage: + RUSAGE_SELF = 0 + + @staticmethod + def getrusage(_who): + return type("Usage", (), {"ru_maxrss": ru_maxrss})() + + monkeypatch.setattr(sys, "platform", platform) + monkeypatch.setattr(pdf_extract_child, "_resource", Rusage) + monkeypatch.setattr(pdf_extract_child, "_LINUX_STATUS_PATH", "/nonexistent/status") + assert pdf_extract_child.peak_rss_bytes() == expected + + def test_a_child_of_a_parent_past_the_ceiling_still_extracts(self, tmp_path): + """The parent's peak is not the child's. + + A parent that has touched twice the child's ``--max-rss`` spawns the real + extractor on a one-page PDF. Sampled from ``ru_maxrss``, the child's first + tick on Linux reads THAT parent's high-water mark (``execve`` inherits it) + and the page comes back as ``error: memory`` / ``rss``; the child's own + footprint is a fraction of the limit, so it must extract. The parent's + measured peak is asserted so the planted condition is proven, not assumed. + """ + limit = 128 * 1024 * 1024 + argv = [*pdf_extract._child_argv(4000, 10)[:-1], f"--max-rss={limit}"] + parent = textwrap.dedent(f""" + import json, resource, subprocess, sys + blob = bytearray({2 * limit}) + for i in range(0, len(blob), 4096): + blob[i] = 1 + del blob + peak_kib = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss + # ``run`` with its own, SHORTER bound: on TimeoutExpired it kills the + # extractor and waits for it before raising, so the grandchild never + # outlives the outer bound this parent runs under. + proc = subprocess.run( + {argv!r}, cwd={str(tmp_path)!r}, input=sys.stdin.buffer.read(), + capture_output=True, timeout={_FAR}, + ) + json.dump( + {{"parent_peak": peak_kib * 1024, "rc": proc.returncode, + "out": proc.stdout.decode(), + "err": proc.stderr.decode(errors="replace")[-512:]}}, + sys.stdout, + ) + """) + # Outer bound strictly wider than the inner: a parent killed at the same + # instant its child hits the bound would leave that child running. + run = subprocess.run( + [sys.executable, "-c", parent], + input=text_pdf("from a fat parent"), + capture_output=True, + cwd=tmp_path, + timeout=_FAR * 2, + check=True, + ) + report = json.loads(run.stdout) + assert report["parent_peak"] > limit, "the parent never crossed the child's ceiling" + assert report["rc"] == 0, report + lines = [json.loads(line) for line in report["out"].splitlines() if line] + assert lines == [ + {"label": "page 1", "text": "from a fat parent"}, + {"end": True, "truncated": False, "pages": 1}, + ] + def test_the_child_polices_its_own_rss_where_the_kernel_has_no_ceiling(self, bomb): """No rlimit at all (``none`` profile) and a 400 MB RSS ceiling: the inflate of the bomb crosses it and the child ends itself with the ``memory`` report diff --git a/test/test_perf_boot_path.py b/test/test_perf_boot_path.py index 168ebb240228..e50fa1b406a7 100644 --- a/test/test_perf_boot_path.py +++ b/test/test_perf_boot_path.py @@ -37,35 +37,6 @@ _SRC = str(Path(kiro_crew.__file__).resolve().parents[1]) -@pytest.fixture(autouse=True) -def _close_knowledge_stores(monkeypatch): - """Close the SQLite connection each ``KnowledgeStore`` opened on this thread. - - ``KnowledgeStore`` opens a per-thread SQLite connection (three descriptors - in WAL) on first ``db`` access and never closes it without an explicit - call; the scan tests here would otherwise leave the test-thread connection - open until GC. Track every instance and release it at teardown. - """ - from kiro_crew.knowledge import store as _store_mod - - created = [] - orig_init = _store_mod.KnowledgeStore.__init__ - - def _tracking_init(self, *args, **kwargs): - orig_init(self, *args, **kwargs) - created.append(self) - - monkeypatch.setattr(_store_mod.KnowledgeStore, "__init__", _tracking_init) - try: - yield - finally: - for store in created: - try: - store.close() - except Exception: - pass - - def _probe(snippet: str) -> dict: """Run *snippet* in a clean interpreter, returning the JSON it prints. @@ -387,10 +358,15 @@ def __getattr__(self, name): class TestFolderWatcherScanQueryCount: """A scan re-read ``sources.properties`` and issued a ``last_seen`` UPDATE - once per discovered file — up to 10,000 on-loop sqlite ops per scan.""" + once per discovered file — up to 10,000 on-loop sqlite ops per scan. + + Each store goes through the rootdir conftest's ``opened`` fixture: the + batched ``last_seen`` flush runs on a worker connection, which a per-thread + ``close()`` from the test thread never reached. + """ @pytest.mark.asyncio - async def test_pause_check_and_last_seen_are_not_per_file(self, tmp_path: Path) -> None: + async def test_pause_check_and_last_seen_are_not_per_file(self, tmp_path: Path, opened) -> None: from kiro_crew.knowledge.folder_watcher import ( _PAUSE_RECHECK_FILES, FolderWatcher, @@ -405,7 +381,7 @@ async def test_pause_check_and_last_seen_are_not_per_file(self, tmp_path: Path) for i in range(n_files): (vault / f"note{i}.md").write_text(f"Note {i}", encoding="utf-8") - store = KnowledgeStore(tmp_path / "knowledge.db") + store = opened(KnowledgeStore(tmp_path / "knowledge.db")) pipeline = MagicMock() pipeline._dedup_enabled = False fw = FolderWatcher(store, pipeline) @@ -454,7 +430,7 @@ def _counting_flush(batch): ], "all unchanged-file last_seen touches must land in one worker batch" @pytest.mark.asyncio - async def test_last_seen_is_still_written(self, tmp_path: Path) -> None: + async def test_last_seen_is_still_written(self, tmp_path: Path, opened) -> None: """The batching must not drop the touches it defers.""" from kiro_crew.knowledge.folder_watcher import FolderWatcher from kiro_crew.knowledge.store import KnowledgeStore @@ -463,7 +439,7 @@ async def test_last_seen_is_still_written(self, tmp_path: Path) -> None: vault.mkdir() (vault / "note.md").write_text("Note", encoding="utf-8") - store = KnowledgeStore(tmp_path / "knowledge.db") + store = opened(KnowledgeStore(tmp_path / "knowledge.db")) pipeline = MagicMock() pipeline._dedup_enabled = False fw = FolderWatcher(store, pipeline) @@ -495,7 +471,7 @@ async def _ingest(file_path, source_id, namespace, props, old_ids, root: str = " assert row["last_seen"] != "1999-01-01", "batched last_seen was never flushed" @pytest.mark.asyncio - async def test_pause_still_stops_a_scan(self, tmp_path: Path) -> None: + async def test_pause_still_stops_a_scan(self, tmp_path: Path, opened) -> None: """Bounding the re-check must not remove the pause path.""" from kiro_crew.knowledge.folder_watcher import FolderWatcher from kiro_crew.knowledge.store import KnowledgeStore @@ -505,7 +481,7 @@ async def test_pause_still_stops_a_scan(self, tmp_path: Path) -> None: for i in range(5): (vault / f"note{i}.md").write_text(f"Note {i}", encoding="utf-8") - store = KnowledgeStore(tmp_path / "knowledge.db") + store = opened(KnowledgeStore(tmp_path / "knowledge.db")) fw = FolderWatcher(store, MagicMock()) source_id = store.add_source( "t", "local_folder", str(vault), properties={"scan_paused": True} diff --git a/test/test_platform_compat.py b/test/test_platform_compat.py index 0d1e305dc0c5..8afb69fcd8f9 100644 --- a/test/test_platform_compat.py +++ b/test/test_platform_compat.py @@ -24,6 +24,7 @@ import subprocess import sys import tempfile +import textwrap import threading import time import types @@ -1232,6 +1233,165 @@ def test_proc_rss_bytes_for_pid_self_positive(self): def test_proc_rss_bytes_for_pid_none_for_unused_pid(self): assert pc.proc_rss_bytes_for_pid(2_000_000_000) is None + +class TestPeakRssIsThisProcesss: + """``proc_peak_rss_bytes`` reports the PROCESS's own high-water mark. + + On Linux ``execve`` seeds the new image's ``ru_maxrss`` with the pre-exec + image's peak, so a gateway launched from a large parent would otherwise + publish that parent's peak on ``proc_mem_peak_mb`` and the + ``memory.peak_rss_bytes`` gauge for its whole life. + """ + + _STATUS = "Name:\tpython\nVmPeak:\t 1400000 kB\nVmHWM:\t 11432 kB\nVmRSS:\t 11432 kB\n" + + # These tests drive the POSIX reader (``_posix_peak_rss_bytes``) directly, with + # ``sys.platform`` and the status-file seam pinned, so they run on every host: + # ``proc_peak_rss_bytes`` itself dispatches on the import-time ``IS_POSIX`` and + # on Windows answers from the Win32 counters, which have their own tests. The + # ``resource`` module exists only on POSIX, so the ``getrusage`` stand-in is + # installed with ``raising=False`` -- on Windows it CREATES the attribute the + # reader would consult, which is exactly what must never happen. + + @staticmethod + def _never_getrusage(monkeypatch): + def inherited(*_a, **_k): + raise AssertionError("ru_maxrss consulted on linux: that is the parent's peak") + + monkeypatch.setattr( + pc, "resource", types.SimpleNamespace(RUSAGE_SELF=0, getrusage=inherited), raising=False + ) + + def test_linux_peak_is_its_own_vmhwm_never_getrusage(self, tmp_path, monkeypatch): + status = tmp_path / "status" + status.write_text(self._STATUS, encoding="utf-8") + self._never_getrusage(monkeypatch) + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", status) + monkeypatch.setattr(pc, "_LINUX_PEAK_RSS_FLOOR", 0) + assert pc._posix_peak_rss_bytes() == 11432 * 1024 + # An unreadable /proc is None (the callers' documented 0), not the + # inherited number and not the floor a readable earlier call left behind. + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", tmp_path / "gone") + assert pc._posix_peak_rss_bytes() is None + + @pytest.mark.skipif( + not pc.IS_POSIX, reason="proc_rss_bytes's POSIX last resort; Windows reads Win32 counters" + ) + def test_the_current_readings_last_resort_is_the_same_own_peak(self, tmp_path, monkeypatch): + status = tmp_path / "status" + status.write_text(self._STATUS, encoding="utf-8") + self._never_getrusage(monkeypatch) + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", status) + monkeypatch.setattr(pc, "_LINUX_PEAK_RSS_FLOOR", 0) + monkeypatch.setattr(pc, "_linux_current_rss_bytes", lambda: None) + assert pc.proc_rss_bytes() == 11432 * 1024 + + @pytest.mark.parametrize( + "status", + [ + "Name:\tpython\nVmRSS:\t 11432 kB\n", # no high-water field at all + "VmHWM:\t 11432 MB\n", # a unit the kernel never prints + "VmHWM:\t lots kB\n", + "VmHWM:\n", + "", + ], + ) + def test_an_unreadable_status_is_none_not_a_guess(self, status): + assert pc._peak_rss_from_status(status) is None + + def test_the_linux_reading_never_decreases(self, tmp_path, monkeypatch): + # The kernel folds the live RSS into hiwater_rss lazily from batched + # per-thread counters, so consecutive VmHWM readings around an unmap + # can dip by a few hundred KiB. The contract is a peak, so the reader + # holds its floor -- and drops it only for an unreadable file, which + # is None, not a stale number. + status = tmp_path / "status" + monkeypatch.setattr(sys, "platform", "linux") + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", status) + monkeypatch.setattr(pc, "_LINUX_PEAK_RSS_FLOOR", 0) + readings = [] + for kib in (186_335_232 // 1024, 185_970_688 // 1024, 200_000): + status.write_text(f"VmHWM:\t{kib} kB\n", encoding="utf-8") + readings.append(pc._linux_peak_rss_bytes()) + assert readings == [186_335_232, 186_335_232, 200_000 * 1024] + status.unlink() + assert pc._linux_peak_rss_bytes() is None + + @pytest.mark.parametrize( + ("platform", "ru_maxrss", "expected"), + [("darwin", 123_456_789, 123_456_789), ("freebsd13", 11432, 11432 * 1024)], + ) + def test_off_linux_getrusage_is_read_in_the_platforms_unit( + self, monkeypatch, platform, ru_maxrss, expected + ): + monkeypatch.setattr(sys, "platform", platform) + monkeypatch.setattr( + pc, + "resource", + types.SimpleNamespace( + RUSAGE_SELF=0, getrusage=lambda _who: types.SimpleNamespace(ru_maxrss=ru_maxrss) + ), + raising=False, + ) + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", Path("/nonexistent/status")) + assert pc._posix_peak_rss_bytes() == expected + + def test_the_two_status_parsers_agree(self): + # pdf_extract_child keeps its own copy (it must not import this module + # under a capped address space); the two must read the same text alike. + from kiro_crew import pdf_extract_child + + for text in (self._STATUS, "VmHWM:\t 1 kB\n", "VmHWM:\t 1 MB\n", ""): + assert pc._peak_rss_from_status(text) == pdf_extract_child._peak_rss_from_status(text) + + def test_a_child_of_a_bloated_parent_reports_its_own_small_peak(self, tmp_path): + """A parent that has touched twice the bar spawns a child that reports its + own peak under it. The planted condition is proven, not assumed: the + parent's measured peak must exceed the bar, and on Linux the child's raw + ``ru_maxrss`` must show the inheritance the reader exists to bypass.""" + bar = 256 * 1024 * 1024 + child = textwrap.dedent(""" + import json, sys + from kiro_crew import platform_compat as pc + json.dump( + {"own_peak": pc.proc_peak_rss_bytes(), + "inherited": pc._ru_maxrss_bytes() if pc.IS_POSIX else None}, + sys.stdout, + ) + """) + parent = textwrap.dedent(f""" + import json, subprocess, sys + from kiro_crew import platform_compat as pc + blob = bytearray({2 * bar}) + for i in range(0, len(blob), 4096): + blob[i] = 1 + del blob + report = {{"parent_peak": pc.proc_peak_rss_bytes()}} + run = subprocess.run( + [sys.executable, "-c", {child!r}], cwd={str(tmp_path)!r}, + capture_output=True, text=True, timeout=60, check=True, + ) + report.update(json.loads(run.stdout)) + json.dump(report, sys.stdout) + """) + run = subprocess.run( + [sys.executable, "-c", parent], + capture_output=True, + text=True, + encoding="utf-8", + cwd=tmp_path, + timeout=90, + check=True, + ) + report = json.loads(run.stdout) + assert report["parent_peak"] > bar, "the parent never crossed the bar" + if sys.platform.startswith("linux"): + # The kernel fact the Linux reader exists for: seen, or the pin is hollow. + assert report["inherited"] > bar, "ru_maxrss did not inherit the parent's peak" + assert 0 < report["own_peak"] < bar, report + def test_proc_rss_tree_mb_for_pid_windows_only(self): # Windows-only: the lineage-validated tree walk. On POSIX it returns None # (callers keep their /proc or ps route), and it must never raise. @@ -4014,7 +4174,8 @@ def test_proc_rss_bytes_returns_zero_when_every_source_fails(self, monkeypatch): # the labelled last-resort peak -- so reaching 0 needs BOTH the # current-RSS reader and the fallback to fail. Asserting only the # getrusage failure would pass on a platform whose primary reader was - # silently removed. + # silently removed. On Linux the fallback peak is VmHWM, not getrusage, + # so its status file has to be unreadable too. if not pc.IS_POSIX: pytest.skip("POSIX resource.getrusage branch") @@ -4022,20 +4183,24 @@ def boom(*args, **kwargs): raise OSError("getrusage failed") monkeypatch.setattr(pc.resource, "getrusage", boom) + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", Path("/nonexistent/status")) monkeypatch.setattr(pc, "_linux_current_rss_bytes", lambda: None) monkeypatch.setattr(pc, "_macos_current_rss_bytes", lambda: None) assert pc.proc_rss_bytes() == 0 - def test_proc_peak_rss_bytes_returns_zero_on_getrusage_failure(self, monkeypatch): - # The peak reading has getrusage as its ONLY POSIX source, so its - # failure branch is still a plain 0. + def test_proc_peak_rss_bytes_returns_zero_when_its_source_fails(self, monkeypatch): + # The peak reading has ONE source per POSIX platform: VmHWM on Linux, + # getrusage elsewhere. That source failing is a plain 0, and on Linux + # the failure must not fall through to getrusage, whose ru_maxrss is + # the parent's inherited peak (test_linux_peak_is_its_own_vmhwm). if not pc.IS_POSIX: - pytest.skip("POSIX resource.getrusage branch") + pytest.skip("POSIX peak-RSS branch") def boom(*args, **kwargs): raise OSError("getrusage failed") monkeypatch.setattr(pc.resource, "getrusage", boom) + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", Path("/nonexistent/status")) assert pc.proc_peak_rss_bytes() == 0 def test_proc_cpu_seconds_returns_zero_on_getrusage_failure(self, monkeypatch): diff --git a/test/test_platform_compat_coverage.py b/test/test_platform_compat_coverage.py index 55a75e19a357..b79f4e82245b 100644 --- a/test/test_platform_compat_coverage.py +++ b/test/test_platform_compat_coverage.py @@ -2029,15 +2029,20 @@ def test_linux_current_reader_agrees_with_proc_status(self): # Sampled a moment apart, so allow drift rather than demanding equality. assert abs(measured - vm_rss_kb * 1024) < 4 * 1024 * 1024 - def test_linux_fallback_scales_the_peak_from_kib_to_bytes(self, monkeypatch): - # Unit handling is the trap: ru_maxrss is KiB on Linux and bytes on - # macOS, with nothing in the value to tell them apart. + def test_linux_fallback_scales_the_peak_from_kib_to_bytes(self, monkeypatch, tmp_path): + # Unit handling is the trap: the kernel prints VmHWM in kB with nothing + # in the value to say so. The Linux peak is the process's OWN VmHWM, not + # ru_maxrss, which execve seeds with the parent's peak. monkeypatch.setattr(pc, "IS_POSIX", True) monkeypatch.setattr(pc.sys, "platform", "linux") _fake_resource( monkeypatch, - getrusage=lambda _who: types.SimpleNamespace(ru_maxrss=2048), + getrusage=lambda _who: types.SimpleNamespace(ru_maxrss=999_999_999), ) + status = tmp_path / "status" + status.write_text("VmHWM:\t 2048 kB\n", encoding="utf-8") + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", status) + monkeypatch.setattr(pc, "_LINUX_PEAK_RSS_FLOOR", 0) monkeypatch.setattr(pc, "_linux_current_rss_bytes", lambda: None) assert pc.proc_rss_bytes() == 2048 * 1024 @@ -2078,12 +2083,13 @@ def _no_libsystem(_path: Any, **_kw: Any) -> Any: monkeypatch.setattr(pc.ctypes, "CDLL", _no_libsystem) assert pc.proc_rss_bytes() == 555 - def test_a_total_posix_failure_is_zero(self, monkeypatch): + def test_a_total_posix_failure_is_zero(self, monkeypatch, tmp_path): def _boom(_who: Any) -> Any: raise OSError("no rusage") monkeypatch.setattr(pc, "IS_POSIX", True) _fake_resource(monkeypatch, getrusage=_boom) + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", tmp_path / "no-proc") monkeypatch.setattr(pc, "_linux_current_rss_bytes", lambda: None) monkeypatch.setattr(pc, "_macos_current_rss_bytes", lambda: None) assert pc.proc_rss_bytes() == 0 @@ -2139,13 +2145,19 @@ def test_the_flavor_selector_is_mach_task_basic_info(self): class TestProcPeakRss: """The peak is still reported, but as its own clearly-named reading.""" - def test_posix_scales_kib_to_bytes_on_linux(self, monkeypatch): + def test_posix_scales_kib_to_bytes_on_linux(self, monkeypatch, tmp_path): + # Linux reads its own VmHWM (kB); ru_maxrss there is the parent's + # inherited peak and must not be the source. monkeypatch.setattr(pc, "IS_POSIX", True) monkeypatch.setattr(pc.sys, "platform", "linux") _fake_resource( monkeypatch, - getrusage=lambda _who: types.SimpleNamespace(ru_maxrss=2048), + getrusage=lambda _who: types.SimpleNamespace(ru_maxrss=999_999_999), ) + status = tmp_path / "status" + status.write_text("VmHWM:\t 2048 kB\n", encoding="utf-8") + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", status) + monkeypatch.setattr(pc, "_LINUX_PEAK_RSS_FLOOR", 0) assert pc.proc_peak_rss_bytes() == 2048 * 1024 def test_posix_reports_bytes_directly_on_macos(self, monkeypatch): @@ -2157,12 +2169,15 @@ def test_posix_reports_bytes_directly_on_macos(self, monkeypatch): ) assert pc.proc_peak_rss_bytes() == 999 - def test_a_getrusage_failure_is_zero(self, monkeypatch): + def test_a_getrusage_failure_is_zero(self, monkeypatch, tmp_path): + # The single POSIX source failing (VmHWM on Linux, getrusage elsewhere) + # is 0: neither falls through to the other. def _boom(_who: Any) -> Any: raise OSError("no rusage") monkeypatch.setattr(pc, "IS_POSIX", True) _fake_resource(monkeypatch, getrusage=_boom) + monkeypatch.setattr(pc, "_LINUX_STATUS_PATH", tmp_path / "no-proc") assert pc.proc_peak_rss_bytes() == 0 def test_windows_reads_the_peak_working_set(self, monkeypatch): diff --git a/test/test_preflight_write_permission.py b/test/test_preflight_write_permission.py index 6fa6c11bc15b..4faed35019dd 100644 --- a/test/test_preflight_write_permission.py +++ b/test/test_preflight_write_permission.py @@ -722,6 +722,7 @@ def test_run_kills_hanging_subprocess_at_timeout(self, tmp_path, monkeypatch): rc 124 - the call returns promptly instead of blocking on the child.""" monkeypatch.chdir(tmp_path) mod = _preflight_module() + monkeypatch.setattr(mod, "_WORKTREE_ROOT", str(tmp_path)) start = time.monotonic() rc, out, err = mod.run([sys.executable, "-c", "import time; time.sleep(30)"], timeout=1) elapsed = time.monotonic() - start @@ -733,6 +734,7 @@ def test_run_without_timeout_expiry_passes_through(self, tmp_path, monkeypatch): """A subprocess that finishes inside the bound is unaffected.""" monkeypatch.chdir(tmp_path) mod = _preflight_module() + monkeypatch.setattr(mod, "_WORKTREE_ROOT", str(tmp_path)) rc, out, err = mod.run([sys.executable, "-c", "print('ok')"], timeout=30) assert rc == 0 assert out == "ok" @@ -848,8 +850,20 @@ def _preflight_module(self): @pytest.mark.parametrize("arg", ["a&b", "a|b", "a%PATH%b", 'x"y', "a\r\nb"]) def test_metachar_arg_refused_for_cmd_launcher(self, tmp_path, monkeypatch, arg): + """The launcher sits OUTSIDE the fenced working tree, so the batch + metacharacter guard - not the working-tree fence one check earlier - + is what refuses it. ``tmp_path`` may itself lie inside a git checkout + (a ``TMPDIR`` pinned under the repo), so the fence root is pinned at + the seam ``run()`` reads to a sibling that is not the fake's ancestor + rather than left to the host's ``git rev-parse``.""" + work = tmp_path / "clone" + work.mkdir() + monkeypatch.chdir(work) mod = self._preflight_module() - fake = tmp_path / "gh.cmd" + monkeypatch.setattr(mod, "_WORKTREE_ROOT", str(work)) + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + fake = bin_dir / "gh.cmd" fake.write_text("@echo off\r\n", encoding="utf-8") monkeypatch.setattr(mod.shutil, "which", lambda name: str(fake)) rc, out, err = mod.run(["gh", arg]) @@ -859,6 +873,7 @@ def test_metachar_arg_refused_for_cmd_launcher(self, tmp_path, monkeypatch, arg) def test_clean_args_pass_for_non_batch_target(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) mod = self._preflight_module() + monkeypatch.setattr(mod, "_WORKTREE_ROOT", str(tmp_path)) rc, out, err = mod.run([sys.executable, "-c", "print('ok')"]) assert rc == 0 assert out == "ok" @@ -923,6 +938,7 @@ def test_resolution_outside_worktree_allowed(self, tmp_path, monkeypatch): work = tmp_path / "clone" work.mkdir() monkeypatch.chdir(work) + monkeypatch.setattr(mod, "_WORKTREE_ROOT", str(work)) rc, out, err = mod.run([sys.executable, "-c", "print('ok')"]) assert rc == 0 assert out == "ok" diff --git a/test/test_prepare_pr_green_age.py b/test/test_prepare_pr_green_age.py index 993615abcb09..ab228ee1991f 100644 --- a/test/test_prepare_pr_green_age.py +++ b/test/test_prepare_pr_green_age.py @@ -115,6 +115,16 @@ def pair(tmp_path: Path) -> Pair: def _run_main(mod: ModuleType, monkeypatch, pair: Pair, *argv: str) -> int: + """Run the script's ``main`` from inside the work clone. + + ``green_age.run()`` is a CLI runner whose contract is "git in the invoking + cwd": it passes no ``cwd=`` of its own, so the PROCESS cwd is the repository + it measures. The ``chdir`` here is therefore the whole isolation: with it, + every ``git`` the script spawns runs under ``tmp_path``; without it, the same + spawns would run in the pytest worker's cwd -- this checkout -- and answer + about the wrong repository. The ``cwd=None`` descriptor itself is deliberate + and stays (test-hygiene class 7, "what not to re-derive"). + """ monkeypatch.chdir(pair.work) return mod.main(list(argv)) diff --git a/test/test_push_guard.py b/test/test_push_guard.py index d40b9dcb5134..a1c026a8dd97 100644 --- a/test/test_push_guard.py +++ b/test/test_push_guard.py @@ -1551,6 +1551,37 @@ def test_revlist_base_failure_refuses(self, repo_pair, tmp_path, monkeypatch): f"stdout: {stdout}\nstderr: {stderr}" ) + def test_patchid_failure_refuses(self, repo_pair, tmp_path, monkeypatch): + """patch-id --stable failure → exit 40, and it is the INJECTED git that fails. + + Both ``patch-id`` calls go through ``run()`` and so through ``_GIT_CMD``, + which is what lets this fake reach them: a bare ``git`` spawn of their + own would be untestable here and would cost every run of the class a + real host ``git``. Failing the fake on ``patch-id`` and getting the refusal proves + the calls now go through ``run()`` like every other command. + """ + clone_dir, _ = repo_pair + + _git(clone_dir, "checkout", "-b", "feature/patchid-fail-test") + Path(clone_dir, "change.py").write_text("# change\n") + _git(clone_dir, "add", "change.py") + _git(clone_dir, "commit", "-m", "feat: test commit") + + fake_cmd = self._make_fake_git_cmd(tmp_path, "'patch-id' in args") + + rc, stdout, stderr = self._run_push_guard_inprocess(monkeypatch, clone_dir, fake_cmd) + assert rc == 40, ( + f"Expected refused (40), got {rc}. A failing " + f"patch-id must not produce SAFE TO PUSH.\n" + f"stdout: {stdout}\nstderr: {stderr}" + ) + assert "SAFE TO PUSH" not in stdout + combined = (stdout + stderr).lower() + assert "refused" in combined and "patch-id" in combined, ( + "Expected a REFUSED diagnostic mentioning patch-id.\n" + f"stdout: {stdout}\nstderr: {stderr}" + ) + def test_empty_ahead_set_on_success_is_still_safe(self, repo_pair): """rev-list SUCCEEDS with empty output (0 ahead commits) → still SAFE.""" clone_dir, _ = repo_pair diff --git a/test/test_sandbox_launcher_libc_load.py b/test/test_sandbox_launcher_libc_load.py index aab58b11dc16..d21d4eca6772 100644 --- a/test/test_sandbox_launcher_libc_load.py +++ b/test/test_sandbox_launcher_libc_load.py @@ -39,6 +39,7 @@ import pytest +import kiro_crew.sandbox as sandbox_mod from kiro_crew.sandbox import _PROBE_SHIM_CODE, _build_launcher_script pytestmark = pytest.mark.skipif( @@ -47,6 +48,18 @@ "os.getuid() (absent on Windows) and binds unshare() (absent on macOS libc)", ) + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The AST these tests walk does not depend on that answer, and a real ssh + spawned from the test process is a host dependency this module is not about. + Pinned so no binary runs. + """ + monkeypatch.setattr(sandbox_mod, "_ssh_supports_accept_new", lambda: True) + + #: Labels for every pre-confinement script this module generates. Both kinds run #: with a caller-supplied environment before any namespace exists, so both are #: bound by the no-``find_library`` rule -- asserting over the pair is what stops diff --git a/test/test_sandbox_launcher_refusal.py b/test/test_sandbox_launcher_refusal.py index 7479588e1b87..62725452ffaa 100644 --- a/test/test_sandbox_launcher_refusal.py +++ b/test/test_sandbox_launcher_refusal.py @@ -21,6 +21,18 @@ from kiro_crew import sandbox as sb + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The refusal lines read out of the launcher do not depend on that answer, and a + real ssh spawned from the test process is a host dependency this module is not + about. Pinned so no binary runs. + """ + monkeypatch.setattr(sb, "_ssh_supports_accept_new", lambda: True) + + #: The exact line a container under its runtime's default AppArmor profile #: produces: both unshares succeed, the launcher's first mount is refused. _MOUNT_REFUSED = ( diff --git a/test/test_sandbox_mount_source_sweep.py b/test/test_sandbox_mount_source_sweep.py index 7dec0818bfbf..959fd8aee21e 100644 --- a/test/test_sandbox_mount_source_sweep.py +++ b/test/test_sandbox_mount_source_sweep.py @@ -46,6 +46,7 @@ import pytest +import kiro_crew.sandbox as sandbox_mod from kiro_crew.config.paths import config_dir from kiro_crew.sandbox import ( _MOUNT_SOURCE_MAX_AGE_SECONDS, @@ -57,6 +58,18 @@ cleanup_stale_sandbox_profiles, ) + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The staging sites read out of the launcher do not depend on that answer, and a + real ssh spawned from the test process is a host dependency this module is not + about. Pinned so no binary runs. + """ + monkeypatch.setattr(sandbox_mod, "_ssh_supports_accept_new", lambda: True) + + # ``_build_launcher_script`` calls POSIX-only ``os.getuid``/``os.getgid`` (the # namespace launcher never runs on Windows). requires_posix = pytest.mark.skipif( diff --git a/test/test_sandbox_private_window_in_caller_mask.py b/test/test_sandbox_private_window_in_caller_mask.py index 39408c9650d8..ff67876f7330 100644 --- a/test/test_sandbox_private_window_in_caller_mask.py +++ b/test/test_sandbox_private_window_in_caller_mask.py @@ -38,6 +38,18 @@ from kiro_crew import sandbox + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The private-window lists read out of the launcher do not depend on that answer, + and a real ssh spawned from the test process is a host dependency this module is + not about. Pinned so no binary runs. + """ + monkeypatch.setattr(sandbox, "_ssh_supports_accept_new", lambda: True) + + _HOME = os.path.expanduser("~") _APPS = os.path.join(_HOME, ".kiro", "crew", "apps") _BUNDLE = os.path.join(_APPS, "demo-app") diff --git a/test/test_sandbox_protected_name_holds.py b/test/test_sandbox_protected_name_holds.py index 5e951154000b..a155c514f24e 100644 --- a/test/test_sandbox_protected_name_holds.py +++ b/test/test_sandbox_protected_name_holds.py @@ -36,6 +36,18 @@ reason="_build_launcher_script uses POSIX-only os.getuid; Windows skips the wrap", ) + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The protected-name populations read out of the launcher do not depend on that + answer, and a real ssh spawned from the test process is a host dependency this + module is not about. Pinned so no binary runs. + """ + monkeypatch.setattr(sandbox, "_ssh_supports_accept_new", lambda: True) + + TIERS = ("standard", "cc", "strict") #: Protected names held by an ENCLOSING stand-in mask today. Measured, not aspired diff --git a/test/test_sandbox_seal_locked_flags.py b/test/test_sandbox_seal_locked_flags.py index b99e3b081e93..463e70859fb3 100644 --- a/test/test_sandbox_seal_locked_flags.py +++ b/test/test_sandbox_seal_locked_flags.py @@ -46,10 +46,23 @@ import pytest +import kiro_crew.sandbox as sandbox_mod from kiro_crew.sandbox import _build_launcher_script _LINUX_ONLY = pytest.mark.skipif(sys.platform != "linux", reason="Linux namespace launcher only") + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The flag helper lifted out of the launcher does not depend on that answer, and a + real ssh spawned from the test process is a host dependency this module is not + about. Pinned so no binary runs. + """ + monkeypatch.setattr(sandbox_mod, "_ssh_supports_accept_new", lambda: True) + + #: Flag values the launcher defines for itself; mirrored so extracted code can run. _MS_RDONLY = 1 _MS_NOSUID = 2 diff --git a/test/test_sandbox_ssh_auth_sock_forward.py b/test/test_sandbox_ssh_auth_sock_forward.py index a014a5903a2d..745b0494d6c9 100644 --- a/test/test_sandbox_ssh_auth_sock_forward.py +++ b/test/test_sandbox_ssh_auth_sock_forward.py @@ -29,6 +29,18 @@ import kiro_crew.sandbox as sb + +@pytest.fixture(autouse=True) +def _no_host_ssh_probe(monkeypatch): + """``_build_launcher_script`` asks the HOST's ``ssh -V`` for accept-new support. + + The env scrub lists read out of the launcher do not depend on that answer, and a + real ssh spawned from the test process is a host dependency this module is not + about (it tests the SOCKET forward, never the client). Pinned so no binary runs. + """ + monkeypatch.setattr(sb, "_ssh_supports_accept_new", lambda: True) + + # --- _agent_scrub_prefixes: the shared prefix filter (takes an explicit bool) --- diff --git a/test/test_source_corpus.py b/test/test_source_corpus.py index ee4e9078eece..f120bd54ece0 100644 --- a/test/test_source_corpus.py +++ b/test/test_source_corpus.py @@ -47,10 +47,11 @@ import test_session_map_locking as session_map from source_corpus import ( candidate_sources, + iter_candidate_sources, + iter_source_texts, repo_files, repo_files_named, repo_root, - source_texts, src_root, unreadable_files, ) @@ -340,19 +341,26 @@ def _unencodable_docstrings(sources: Sequence[tuple[Path, str]]) -> list[tuple[P class TestCorpusHealth: - """A stale or empty corpus is the one failure that makes every gate green.""" + """A stale or empty corpus is the one failure that makes every gate green. + + Streamed, not materialised. ``source_texts()`` holds ~130 MB of ``str`` for as + long as its tuple lives, and one comparison written as two of them at once + measured at +264 MiB of resident memory -- the very high-water mark the corpus + module exists to keep off every gate. So each pin below walks the iterator + the gates walk and keeps a count, a path set or two texts, never the tree. + """ def test_the_corpus_is_not_empty_or_stale(self): - texts = source_texts() - assert len(texts) >= _MIN_FILES, ( - f"source_corpus returned {len(texts)} files; every whole-tree ratchet " + count = sum(1 for _pair in iter_source_texts()) + assert count >= _MIN_FILES, ( + f"source_corpus returned {count} files; every whole-tree ratchet " "reads this, so a short corpus makes all of them pass while blind." ) def test_every_file_is_python_under_the_package(self): root = src_root() assert (root / "security" / "__init__.py").is_file(), f"{root} is not the kiro_crew package" - for path, _text in source_texts(): + for path, _text in iter_source_texts(): assert path.suffix == ".py" assert path.is_relative_to(root) @@ -385,18 +393,40 @@ def test_no_docstring_holds_a_character_utf_8_cannot_encode(self): def test_sources_are_the_real_file_contents(self): """Pins the read, not just the count: a corpus of empty strings is worse.""" - # One materialisation: ``source_texts()`` is not memoised, so two calls - # in one expression would hold the corpus twice. - texts = source_texts() - by_name = {path.name: text for path, text in texts} - by_rel = {path.relative_to(src_root()).as_posix(): text for path, text in texts} - assert "def redact" in by_rel["security/__init__.py"] - assert "def batched_save" in by_name["session_map.py"] + root = src_root() + wanted = {"security/__init__.py": "def redact", "session_map.py": "def batched_save"} + seen: dict[str, str] = {} + for path, text in iter_source_texts(): + for key in (path.relative_to(root).as_posix(), path.name): + if key in wanted: + seen[key] = text + assert set(seen) == set(wanted), f"corpus is missing {set(wanted) - set(seen)}" + for key, marker in wanted.items(): + assert marker in seen[key], f"{key} did not read as the real file" def test_a_filter_returns_a_subset_and_no_filter_returns_everything(self): - everything = source_texts() - assert set(candidate_sources(blocking._REQUIRE_ALL)) <= set(everything) - assert candidate_sources() == everything + """The filter is a subset of the corpus; an empty filter is the corpus. + + Both halves stream in lockstep with the corpus. A candidate's ``text`` is + checked against the file itself, so the subset claim covers the pair and + not only the path; ``strict`` makes a filter that drops or invents a file + fail on length, not just on content. + """ + corpus_paths = set() + for path, _text in iter_source_texts(): + corpus_paths.add(path) + kept = 0 + for path, text in iter_candidate_sources(blocking._REQUIRE_ALL): + assert path in corpus_paths, f"{path} is not in the corpus" + assert text == path.read_text(encoding="utf-8"), f"{path} was not read verbatim" + kept += 1 + assert 0 < kept < len(corpus_paths), "the filter admitted nothing, or everything" + + everything = 0 + for candidate, whole in zip(iter_candidate_sources(), iter_source_texts(), strict=True): + assert candidate == whole + everything += 1 + assert everything == len(corpus_paths) class TestFilterLiteralsStillMatchWhatTheGatesReject: @@ -479,11 +509,13 @@ class TestFiltersStillNarrowTheTree: ], ) def test_the_filter_narrows_the_tree(self, label, require_all, require_any, ceiling): - kept = candidate_sources(require_all, require_any) + # Counted off the stream: the broad filters keep up to half the tree, and + # a tuple of those texts is most of the corpus resident again. + kept = sum(1 for _pair in iter_candidate_sources(require_all, require_any)) assert kept, f"{label}: matched nothing, so that gate now scans an empty tree" - assert len(kept) < ceiling, ( - f"{label}: kept {len(kept)} of {len(source_texts())} files, so the filter " - "is no longer buying anything -- either the tree or the literal moved." + assert kept < ceiling, ( + f"{label}: kept {kept} of {sum(1 for _pair in iter_source_texts())} files, so " + "the filter is no longer buying anything -- either the tree or the literal moved." ) diff --git a/test/test_subagent_sizing.py b/test/test_subagent_sizing.py index 7197e5bf9530..bfb94bec156f 100644 --- a/test/test_subagent_sizing.py +++ b/test/test_subagent_sizing.py @@ -36,6 +36,31 @@ def _no_learned_cost(monkeypatch): monkeypatch.setattr(subagent, "read_learned_cost", lambda *a, **k: None) +@pytest.fixture(autouse=True) +def _close_subagent_managers(monkeypatch): + """Close every ``SubagentManager`` built in a test. + + Construction opens the durable task queue (a SQLite connection and its + writer thread); ``_mgr()`` builds one per test and nothing here closes it, + so each manager leaked those descriptors until the cyclic collector ran. + Track every instance and release it at teardown, the shape + ``test_spawn_reasoning_effort`` uses. + """ + created = [] + orig_init = subagent.SubagentManager.__init__ + + def _tracking_init(self, *args, **kwargs): + orig_init(self, *args, **kwargs) + created.append(self) + + monkeypatch.setattr(subagent.SubagentManager, "__init__", _tracking_init) + try: + yield + finally: + for mgr in created: + mgr.close() + + def _cfg( *, max_subagents: int = 0, diff --git a/test/test_subagent_spawn_approval_parked_6484.py b/test/test_subagent_spawn_approval_parked_6484.py index dfac5c36fe40..fd280d465332 100644 --- a/test/test_subagent_spawn_approval_parked_6484.py +++ b/test/test_subagent_spawn_approval_parked_6484.py @@ -119,10 +119,17 @@ async def _park( async def _drain(mgr: SubagentManager, approval: _ParkedApproval) -> None: + """Release the parked run, then the manager's process-lifetime task store. + + Cancelling the tasks frees the loop; ``close()`` frees the durable task + queue the constructor opened (a SQLite connection plus its writer thread), + which nothing else in these tests closes. + """ approval.gate.set() for t in list(mgr._tasks.values()): t.cancel() await asyncio.sleep(0) + mgr.close() @asynccontextmanager diff --git a/test/test_terminal_handler.py b/test/test_terminal_handler.py index 996c77a3fd9f..116cb29171cc 100644 --- a/test/test_terminal_handler.py +++ b/test/test_terminal_handler.py @@ -3209,6 +3209,61 @@ def _unwrapped(buf: bytes) -> bytes: return buf.replace(b"\r", b"").replace(b"\n", b"").replace(b" ", b"") +async def _drain_ws_until(ws, predicate, *, budget_secs: float) -> bytes: + """Read PTY frames into an accumulator until ``predicate(buf)`` is true or + the overall ``budget_secs`` runs out. Returns the accumulated bytes (the + caller decides whether the predicate held).""" + loop = asyncio.get_event_loop() + deadline = loop.time() + budget_secs + buf = bytearray() + while True: + remaining = deadline - loop.time() + if remaining <= 0: + return bytes(buf) + try: + msg = await ws.receive(timeout=remaining) + except asyncio.TimeoutError: + return bytes(buf) + if msg.type == web.WSMsgType.BINARY: + buf.extend(msg.data) + if predicate(bytes(buf)): + return bytes(buf) + elif msg.type in (web.WSMsgType.CLOSE, web.WSMsgType.ERROR): + return bytes(buf) + + +async def _drain_ws_while(ws, condition, *, budget_secs: float) -> bool: + """Keep consuming PTY frames until ``condition()`` holds or the budget runs out. + + The condition is about the PTY's PROCESS state (who owns the foreground), + not its output, so it is re-read on every short receive slice rather than + only when a frame happens to arrive; the frames are still consumed so the + shell is never blocked writing into a full controller buffer. Returns + whether the condition held. + """ + loop = asyncio.get_event_loop() + deadline = loop.time() + budget_secs + while not condition(): + remaining = deadline - loop.time() + if remaining <= 0: + return False + try: + msg = await ws.receive(timeout=min(0.02, remaining)) + except asyncio.TimeoutError: + continue + if msg.type in (web.WSMsgType.CLOSE, web.WSMsgType.ERROR): + return condition() + return True + + +def _pty_foreground_pgid(sess) -> int | None: + """The process group the PTY delivers Ctrl+C to, or None once the controller is gone.""" + try: + return os.tcgetpgrp(sess.master_fd) # wokeignore:rule=master + except OSError: + return None + + async def _recv_matching(ws, predicate, what: str, *, frames: int = 40, timeout: float = 3): """Return the first frame satisfying *predicate*, skipping the ones it does not. @@ -4088,6 +4143,37 @@ async def test_ws_ctrl_c_delivers_sigint(self, monkeypatch, tmp_path): output" helpers: deterministic on a fast host, falls back to a generous overall budget on a slow one. """ + await self._drive_ctrl_c_through_the_pty(monkeypatch, tmp_path) + + @pytest.mark.skipif( + terminal.platform_compat.IS_WINDOWS, + reason="POSIX SIGINT via PTY; on Windows Ctrl+C is handled inside ConPTY", + ) + @pytest.mark.asyncio + async def test_ws_ctrl_c_reaches_the_child_when_the_gateway_ignores_sigint( + self, monkeypatch, tmp_path + ): + """A gateway that inherited ``SIGINT`` as ignored still ships a live Ctrl+C. + + ``SIG_IGN`` survives ``exec``: a shell script that backgrounds the gateway + with ``&`` hands it SIGINT and SIGQUIT ignored, and an interactive shell + keeps a signal ignored on entry ignored in every command it runs, so + without a reset in the spawn path ``sleep`` here never dies and Ctrl+C is + dead in every terminal that gateway opens -- with bash's own disposition + looking normal. The post-exec shim restores the login defaults; this + pins it by running the whole Ctrl+C exchange from a process whose SIGINT + is ignored. Restored in ``finally`` so the disposition never leaks past + this test into the worker. + """ + import signal + + previous = signal.signal(signal.SIGINT, signal.SIG_IGN) + try: + await self._drive_ctrl_c_through_the_pty(monkeypatch, tmp_path) + finally: + signal.signal(signal.SIGINT, previous) + + async def _drive_ctrl_c_through_the_pty(self, monkeypatch, tmp_path): cfg_file = tmp_path / "config.json" cfg_file.write_text(json.dumps({"dashboard": {"terminal": {"enabled": True}}})) monkeypatch.setattr(terminal, "config_path", lambda: cfg_file) @@ -4098,154 +4184,158 @@ async def test_ws_ctrl_c_delivers_sigint(self, monkeypatch, tmp_path): from aiohttp.test_utils import TestClient, TestServer - async def _drain_until(ws, predicate, *, budget_secs: float): - """Read PTY frames into an accumulator until ``predicate(buf)`` is - true or the overall ``budget_secs`` runs out. Returns the - accumulated bytes (caller can decide whether the predicate held).""" - loop = asyncio.get_event_loop() - deadline = loop.time() + budget_secs - buf = bytearray() - while True: - remaining = deadline - loop.time() - if remaining <= 0: - return bytes(buf) - try: - msg = await ws.receive(timeout=remaining) - except asyncio.TimeoutError: - return bytes(buf) - if msg.type == web.WSMsgType.BINARY: - buf.extend(msg.data) - if predicate(bytes(buf)): - return bytes(buf) - elif msg.type in (web.WSMsgType.CLOSE, web.WSMsgType.ERROR): - return bytes(buf) - - async with TestClient(TestServer(app)) as client: - async with client.ws_connect("/api/ws/terminal/sigint-sess") as ws: - # The backend's Bash init stream emits readiness only after the - # login profile chain returns. Wait for that control frame - # before writing the first probe, exactly as Run in terminal - # does; prompt text and timing are deliberately irrelevant. - loop = asyncio.get_event_loop() - ready_deadline = loop.time() + 15 - ready_seen = False - while loop.time() < ready_deadline: - msg = await ws.receive(timeout=ready_deadline - loop.time()) - if msg.type == web.WSMsgType.TEXT: - if json.loads(msg.data).get("type") == "ready": - ready_seen = True - break - elif msg.type in (web.WSMsgType.CLOSE, web.WSMsgType.ERROR): - break - assert ready_seen, "shell never emitted the post-profile ready frame" - - # Drive the PTY off INPUT ECHO, not an unsolicited prompt. A - # login shell on a minimal build host (no MOTD, empty PS1) may - # render no recognizable prompt. The probe confirms the shell - # is interactive and consuming stdin without depending on one. - await ws.send_bytes(b"echo __PTY_READY__\n") - ready = await _drain_until( - ws, - lambda b: b"__PTY_READY__" in _unwrapped(b), - budget_secs=15, - ) - assert b"__PTY_READY__" in _unwrapped(ready), ( - "shell never echoed the readiness probe — PTY input/echo " - "path is not live" - ) + try: + async with TestClient(TestServer(app)) as client: + async with client.ws_connect("/api/ws/terminal/sigint-sess") as ws: + await self._exchange_ctrl_c(ws, registry) + finally: + # On the failure path as well: a ``sleep`` that ignored SIGINT, the + # shell waiting on it and the reader task must not outlive the test. + sess = registry.get("sigint-sess") + if sess is not None: + await terminal._kill_session(sess) - # Run a long-lived sleep in the foreground; drain until we see - # the command echoed back (so we know the shell is processing - # it, not buffering it pre-prompt). The duration is chosen to be - # far larger than the SIGINT-verification budget below: a - # genuinely dropped SIGINT must leave `sleep` running for the - # whole budget, so it can never end on its own and let the shell - # run the queued marker echo (which would be a false pass). Here - # this drain WANTS the line-discipline echo — it only proves the - # shell received the input, so matching the echoed command text - # is correct. - await ws.send_bytes(b"sleep 120\n") - echoed = await _drain_until( - ws, - lambda b: b"sleep120" in _unwrapped(b), - budget_secs=5, - ) - assert b"sleep120" in _unwrapped(echoed), ( - "shell did not echo `sleep 120` within 5s — " - f"input may not have reached an interactive shell: {echoed[-200:]!r}" - ) + @staticmethod + async def _exchange_ctrl_c(ws, registry): + # The backend's Bash init stream emits readiness only after the + # login profile chain returns. Wait for that control frame + # before writing the first probe, exactly as Run in terminal + # does; prompt text and timing are deliberately irrelevant. + loop = asyncio.get_event_loop() + ready_deadline = loop.time() + 15 + ready_seen = False + while loop.time() < ready_deadline: + msg = await ws.receive(timeout=ready_deadline - loop.time()) + if msg.type == web.WSMsgType.TEXT: + if json.loads(msg.data).get("type") == "ready": + ready_seen = True + break + elif msg.type in (web.WSMsgType.CLOSE, web.WSMsgType.ERROR): + break + assert ready_seen, "shell never emitted the post-profile ready frame" + + # Drive the PTY off INPUT ECHO, not an unsolicited prompt. A + # login shell on a minimal build host (no MOTD, empty PS1) may + # render no recognizable prompt. The probe confirms the shell + # is interactive and consuming stdin without depending on one. + await ws.send_bytes(b"echo __PTY_READY__\n") + ready = await _drain_ws_until( + ws, + lambda b: b"__PTY_READY__" in _unwrapped(b), + budget_secs=15, + ) + assert b"__PTY_READY__" in _unwrapped(ready), ( + "shell never echoed the readiness probe — PTY input/echo " "path is not live" + ) - # Deliver SIGINT and confirm the child actually received it. - # This step is inherently racy against shell scheduling on a - # loaded CI host, in two ways the old single-shot version did - # not survive: - # * The ``sleep 120`` echo drained above is emitted by the PTY - # line discipline the instant the bytes arrive — BEFORE the - # shell has necessarily read the line and forked ``sleep`` - # into the foreground process group. A ``\x03`` that lands in - # that window is delivered to the shell sitting at its prompt - # (which simply discards the pending line) rather than to - # ``sleep``, so the FIRST Ctrl+C can miss the child. - # * Under this class's xdist integration group the forked shell - # / reader thread can go unscheduled past a fixed budget, so - # a single marker drain can time out even when delivery would - # eventually succeed. - # Handle both by re-poking with Ctrl+C and re-probing the marker - # over a generous overall budget, rather than the old "\x03 once, - # wait for a prompt redraw, probe once" — a login shell on a - # minimal host may render no prompt at all (see the readiness-gate - # note above), which made the prompt drain a pure time sink and - # left the single probe to absorb the whole race. - # - # EXECUTION-only marker: the probe command is written so the PTY - # line-discipline echo of our own keystrokes never contains the - # search token. The typed bytes are ``echo SIG''INT_OK`` (an - # empty '' splits the literal), so the echoed input reads - # ``SIG''INT_OK`` — no ``SIGINT_OK`` substring — while only the - # shell's *execution* of the echo emits the concatenated - # ``SIGINT_OK`` on stdout. A match therefore proves the shell ran - # a command, i.e. SIGINT killed the foreground ``sleep`` and - # returned the shell to its prompt. Matching the bare echoed input - # (the previous version) let the test pass even when SIGINT was - # never delivered — a false pass that would hide a real - # terminal-signal regression. - sess = registry["sigint-sess"] - loop = asyncio.get_event_loop() - overall_deadline = loop.time() + 25 - found = False - while True: - remaining = overall_deadline - loop.time() - if remaining <= 0: - break - # Ctrl+C (ETX): kills the foreground `sleep` if it is - # running, or harmlessly aborts an empty prompt line if a - # previous iteration already recovered the shell. - await ws.send_bytes(b"\x03") - await ws.send_bytes(b"echo SIG''INT_OK\n") - # Clamp each drain to the remaining budget so the overall - # wait cannot overshoot ``overall_deadline`` by a full drain. - tail = await _drain_until( - ws, - lambda b: b"SIGINT_OK" in _unwrapped(b), - budget_secs=min(5.0, remaining), - ) - if b"SIGINT_OK" in _unwrapped(tail): - found = True - break - # Signal may instead have torn down the whole session — that - # is also a valid "SIGINT was delivered" outcome. - if sess.proc.returncode is not None: - break + # Run a long-lived sleep in the foreground; drain until we see + # the command echoed back (so we know the shell is processing + # it, not buffering it pre-prompt). The duration is chosen to be + # far larger than the SIGINT-verification budget below: a + # genuinely dropped SIGINT must leave `sleep` running for the + # whole budget, so it can never end on its own and let the shell + # run the queued marker echo (which would be a false pass). Here + # this drain WANTS the line-discipline echo — it only proves the + # shell received the input, so matching the echoed command text + # is correct. + await ws.send_bytes(b"sleep 120\n") + echoed = await _drain_ws_until( + ws, + lambda b: b"sleep120" in _unwrapped(b), + budget_secs=5, + ) + assert b"sleep120" in _unwrapped(echoed), ( + "shell did not echo `sleep 120` within 5s — " + f"input may not have reached an interactive shell: {echoed[-200:]!r}" + ) - # Success: the shell executed a command after Ctrl+C (SIGINT - # killed sleep, shell continued) OR the process exited (signal - # was delivered, just tore the whole session down). A dropped - # SIGINT leaves `sleep 120` running for the whole 25s budget, so - # neither branch can become true — the test correctly fails. - assert found or sess.proc.returncode is not None, _sigint_failure_evidence(sess) - await ws.close() + # The echo above is emitted by the line discipline the instant the + # bytes arrive -- BEFORE the shell has read the line and put ``sleep`` + # in the foreground. A ``\x03`` that lands in that window is a VINTR + # on a canonical-mode PTY: the kernel FLUSHES the unread ``sleep 120`` + # line and signals the shell at its prompt, which redraws it and then + # runs the marker echo -- a pass with no ``sleep`` ever born, and so + # no evidence about whether the child would have received SIGINT. The + # sweep saw exactly that: the run that passed took 48 ms while the + # four that failed showed ``sleep`` alive with SIGINT ignored. So wait + # until the terminal's foreground process group differs from the + # shell's own before the first Ctrl+C: bash runs a job in its own + # group and hands it the terminal, so that transition IS ``sleep`` + # being the thing Ctrl+C will reach. + sess = registry["sigint-sess"] + shell_pgid = sess.proc.pid + job_in_foreground = await _drain_ws_while( + ws, + lambda: _pty_foreground_pgid(sess) not in (shell_pgid, None), + budget_secs=10, + ) + assert job_in_foreground, ( + "the shell never handed the terminal to a foreground job: " + f"foreground pgid is still {_pty_foreground_pgid(sess)} " + f"(shell {shell_pgid})" + ) - await terminal._kill_session(registry["sigint-sess"]) + # Deliver SIGINT and confirm the child actually received it. + # This step is racy against shell scheduling on a loaded CI host: + # under this class's xdist integration group the forked shell / + # reader thread can go unscheduled past a fixed budget, so a single + # marker drain can time out even when delivery would eventually + # succeed. Handle it by re-poking with Ctrl+C and re-probing the + # marker over a generous overall budget, rather than the old "\x03 + # once, wait for a prompt redraw, probe once" — a login shell on a + # minimal host may render no prompt at all (see the readiness-gate + # note above), which made the prompt drain a pure time sink and + # left the single probe to absorb the whole race. + # + # EXECUTION-only marker: the probe command is written so the PTY + # line-discipline echo of our own keystrokes never contains the + # search token. The typed bytes are ``echo SIG''INT_OK`` (an + # empty '' splits the literal), so the echoed input reads + # ``SIG''INT_OK`` — no ``SIGINT_OK`` substring — while only the + # shell's *execution* of the echo emits the concatenated + # ``SIGINT_OK`` on stdout. A match therefore proves the shell ran + # a command, i.e. SIGINT killed the foreground ``sleep`` and + # returned the shell to its prompt. Matching the bare echoed input + # (the previous version) let the test pass even when SIGINT was + # never delivered — a false pass that would hide a real + # terminal-signal regression. + overall_deadline = loop.time() + 25 + found = False + while True: + remaining = overall_deadline - loop.time() + if remaining <= 0: + break + # Ctrl+C (ETX): kills the foreground `sleep` if it is + # running, or harmlessly aborts an empty prompt line if a + # previous iteration already recovered the shell. + await ws.send_bytes(b"\x03") + await ws.send_bytes(b"echo SIG''INT_OK\n") + # Clamp each drain to the remaining budget so the overall + # wait cannot overshoot ``overall_deadline`` by a full drain. + tail = await _drain_ws_until( + ws, + lambda b: b"SIGINT_OK" in _unwrapped(b), + budget_secs=min(5.0, remaining), + ) + if b"SIGINT_OK" in _unwrapped(tail): + found = True + break + # Signal may instead have torn down the whole session — that + # is also a valid "SIGINT was delivered" outcome. + if sess.proc.returncode is not None: + break + + # Success: the shell executed a command after Ctrl+C (SIGINT + # killed sleep, shell continued) OR the process exited (signal + # was delivered, just tore the whole session down). A dropped + # SIGINT leaves `sleep 120` running for the whole 25s budget, so + # neither branch can become true — the test correctly fails. + assert found or sess.proc.returncode is not None, _sigint_failure_evidence(sess) + if found: + # The shell only runs the marker once its foreground job is gone, + # so the terminal is back in the shell's own group. + assert _pty_foreground_pgid(sess) == shell_pgid, _sigint_failure_evidence(sess) + await ws.close() @pytest.mark.skipif( terminal.platform_compat.IS_WINDOWS or not shutil.which("bash"),