Raven-1/tests/_windows_kill.py
Dizhan Xue c9f7df0d9e
fix(*): stop cli sub-agents on windows and hand new ones a rotated web key (#927)
## Summary

Two gaps in how Raven stops its cli sub-agents and what environment it
starts them with.

**Stopping a cli sub-agent on Windows.** A cli sub-agent's timeout,
cancel and stream-failure paths, and the `kimi` commands Raven runs to
explain a failed Kimi Code connect, ended the process group with
`os.killpg`, which Windows does not have. The `AttributeError` replaced
the timeout error or the cancellation, and the CLI was left running.
Where `killpg` is missing, the tree is now ended from the launcher's pid
with `taskkill /T /F` while the launcher is still alive, and the
launcher is then killed itself, the way `raven/ops/transport.py` already
ends a command on Windows. Once the launcher has exited its pid may name
another process, so nothing is killed by number. POSIX keeps the group
kill.

**A web key rotated through `settings.set`.** `settings.set` rewrites
`~/.raven/env` when a web credential changes, but a POSIX cli or acp
sub-agent reads that file only through the login-shell capture, which a
running Raven takes once and keeps. Every later spawn started with the
key the write had replaced, until a Connect, a Test, "Check again" or a
restart took the capture again. A mirrored key's write now takes the
capture again before the reply. Captures also run one at a time: a
refresh that read the shell before the write, a Connect's for instance,
could otherwise land after the write's own and put the replaced key
back, and readers that arrive while the first capture runs now wait for
it rather than each running the shell.

Windows is unchanged for keys. Nothing sources `~/.raven/env` there, and
a Windows sub-agent's environment is Raven's own with the registry's
PATH, so a key held only in config does not reach it; #912 states this
in the code. On POSIX the keys reach a sub-agent only through a `source`
line in the user's shell startup files, which the onboard wizard adds
only with the user's agreement, and a Windows delivery path would need a
consent step of its own, so this PR does not add one.

Not changed here: an acp sub-agent that is already connected keeps the
environment it was launched with until its next launch; a key written by
`raven onboard`, which runs as its own process, reaches a running
Raven's sub-agents at the next Connect, Test, "Check again" or restart;
and on Windows the wizard still prints the POSIX `source` line when
`$SHELL` is not bash or zsh.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Linux, Python 3.12, rebased onto main 182dacac:

- `uv run --frozen --python 3.12 --all-extras pytest -q` with the proxy
variables unset: 3 failed, 29667 passed, 158 skipped. The same 3 fail on
main on this machine (an npm on PATH, a root-only permission check, a
LibreOffice conversion); 0 introduced.
- `uv run --frozen --python 3.12 --all-extras pytest
tests/test_subagent_third_party.py tests/test_subagent_kimi_code.py
tests/test_subagent_acp.py tests/test_subagent_host_env.py
tests/test_rpc_console.py tests/test_rpc_settings.py
tests/test_rpc_subagents.py -q`: 1080 passed.
- Windows stop: `tests/_windows_kill.py` leaves a Windows host as it is,
where the real `taskkill` runs, and elsewhere removes `os.killpg` and
stands in for `taskkill` by ending the process group it is given, the
launcher's tree here; either way it records each call. The launchers,
their workers and the Kimi stand-in are Python started on
`sys._base_executable`, because a Windows venv's `python.exe` is a
redirector that runs the base interpreter as its child and returns a pid
the script never sees. The tests have not run on a Windows host, and no
CI job runs these test files there.
- Windows stop, measured here: the six tests that use the helper pass.
With `os.killpg` removed for the whole session and a `taskkill` on PATH
that ends the group it is given, they pass again through that `taskkill`
(3 calls), and the POSIX-only Kimi test beside them is skipped. With
`sys.executable` replaced by a wrapper that runs Python as its child, as
a venv's `python.exe` does on Windows, both modes stay green; started on
that wrapper instead, the two cancel tests fail on the pid they compare.
- Windows stop, against the fix: the six fail with `AttributeError` on
main. Removing the alive check in the cli backend, the `taskkill` call
or the kill after it in either module, or the cli backend's handling of
a `taskkill` that times out, turns at least one of them red. Removing
the POSIX group kill from the Kimi Code commands turns only the new test
whose stand-in `kimi` starts a process of its own red (1 failed, 347
passed over the two test files).
- Rotated key:
`test_a_rotated_key_reaches_the_capture_the_next_spawn_reads` saves a
key twice through `settings.set` and reads the capture after each save,
through a real bash that sources the mirror; on main the second read
still returns the first key. Removing the refresh turns 12 tests red;
refreshing before the mirror is rewritten turns that test red. Two
existing tests that write a web key now stub the refresh, so they do not
run the host's login shell.
- Concurrency: three tests force the interleavings with events: a
refresh that read the shell first and lands after a later one, a second
reader that arrives while the first capture runs, and readers waiting on
a first capture that fails. Each failed in 3 of 3 runs on main and
passes with the lock; removing the lock from either capture path, or
either re-check inside the first one, turns one of them red.
- With `--cov=raven --cov=raven_everos --cov-branch` added to the full
run, `python scripts/coverage_gate.py diff --base-ref origin/main
--threshold 90`: 93.22% of the changed lines (55 of 59). The four left
are except clauses for a process group or a process that is already
gone.
- `ruff check`, `ruff format --check`, `lint-imports`, `ty check` and
`deptry` over the Makefile targets, `commitlint`, the three repository
gate scripts and `pre-commit run --from-ref origin/main --to-ref HEAD`:
passed.
- `git merge-file` of `raven/rpc/methods/console.py` with each open PR
that touches it (#883, #900, #911, #912, #923, #925), and of
`raven/agent/subagent/backends/env.py` with #912 in both orders: no
conflicts.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed

No user-facing docs describe either behavior.

## Risk

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

Saving a web key now waits for the user's login shell to start before
the reply, as Connect and Test already do; the capture's 15 s timeout
bounds it, and it can also wait for a capture already in flight. A
capture that fails keeps the previous one and does not fail the save. No
credential reaches a sub-agent that did not before; it is the same shell
reading the same file, read again. On Windows a stop now waits up to 5 s
for `taskkill`, and a worker that a launcher started and left behind
after exiting outlives the stop there, since Windows has no group to
address and an exited launcher's pid is not trusted. If `taskkill` is
missing, fails or does not answer within those 5 s, only the launcher is
killed, and a cli stop then waits for any worker still holding the
launcher's output pipes to exit. Revert this PR to roll back.

## Related Issues

#908, #912
2026-10-14 11:06:08 +08:00

70 lines
2.5 KiB
Python

"""The kill path a Windows host takes, on Windows or reproduced on this host.
Windows has no ``os.killpg`` and starts no process group, so code that ends a
child's whole tree there asks ``taskkill /T`` instead. On Windows nothing is
replaced and the real ``taskkill`` runs. Elsewhere ``killpg`` is removed to send
the code down that branch, and ``taskkill`` is stood in for by ending the
process group it was asked about: the code under test starts its launcher in a
session of its own, so on this host that group is the launcher's tree. Either
way every call to ``taskkill`` is recorded, so a test asserts the same calls
and the same deaths on both.
"""
from __future__ import annotations
import os
import shutil
import signal
import subprocess
import sys
from contextlib import suppress
import pytest
_STAND_IN = "taskkill"
#: The interpreter the processes these tests stop run on. A Windows venv's
#: ``python.exe`` is a redirector that starts this one as its child, so a spawn
#: of it hands back a pid the script inside never sees as its own.
PYTHON = sys._base_executable
def windows_kill_path(monkeypatch: pytest.MonkeyPatch, *, fails: BaseException | None = None) -> list[list[str]]:
"""Send a stop down the ``taskkill`` branch and return the arguments of each call to it.
Every other ``subprocess.run`` still runs: the login-shell capture a spawn
reads its environment from goes through it too. With ``fails``, each call
to ``taskkill`` is recorded and then raises it, ending nothing.
"""
real_run = subprocess.run
calls: list[list[str]] = []
killpg = getattr(os, "killpg", None)
if killpg is None:
taskkill = shutil.which("taskkill")
if taskkill is None:
pytest.skip("taskkill is not on PATH")
else:
monkeypatch.delattr(os, "killpg")
real_which = shutil.which
taskkill = _STAND_IN
def which(name, *args, **kwargs):
return _STAND_IN if name == "taskkill" else real_which(name, *args, **kwargs)
monkeypatch.setattr(shutil, "which", which)
def run(argv, *args, **kwargs):
if not argv or argv[0] != taskkill:
return real_run(argv, *args, **kwargs)
calls.append(list(argv[1:]))
if fails is not None:
raise fails
if killpg is None:
return real_run(argv, *args, **kwargs)
with suppress(ProcessLookupError):
killpg(int(argv[argv.index("/PID") + 1]), signal.SIGKILL)
return subprocess.CompletedProcess(argv, 0)
monkeypatch.setattr(subprocess, "run", run)
return calls