Skip to content

Commit 230a170

Browse files
committed
fix(worker): move TOKEN_GOAT_NO_WORKER_SPAWN check to CLI entry point
Earlier this round the check landed first in ``worker.ensure_running`` (broke 2 spawn-respawn unit tests via the mocked ``spawn_detached``), then in ``spawn_detached`` itself (broke 3 tests that test that function's body via mocked ``Popen``), then in ``worker_daemon.run_daemon`` (broke 9 tests that drive ``run_daemon`` directly to verify its main loop). Each tier of unit tests bypasses one more layer of the spawn chain. The clean break: put the check in ``cli.cmd_worker`` instead. This is the function the subprocess command ``pythonw -m token_goat.cli worker --daemon`` actually invokes when the spawn chain runs end-to-end. Direct unit tests of ``ensure_running``, ``spawn_detached``, and ``run_daemon`` all skip this entry point — they call the lower-level functions directly — so they remain unaffected. Only the real-spawn path (``ensure_running`` → ``spawn_detached`` → ``Popen`` → child process loads ``cli.py`` → ``cmd_worker`` → early-exit) sees the env var. Env-var propagation: ``subprocess.Popen`` inherits the parent's env by default, so a test step that sets ``TOKEN_GOAT_NO_WORKER_SPAWN=1`` on the workflow step propagates the var to every child process spawned from it. The child ``pythonw -m token_goat.cli worker --daemon`` loads, reaches ``cmd_worker``, sees the var, and returns immediately without entering the heartbeat loop. GitHub Actions on Windows can then close the job object's descendants cleanly and the step completes. 96 / 96 worker + worker_daemon tests pass; 2610 / 2614 of the network-independent suite passes (the remaining 4 are pre-existing tree-sitter offline failures unrelated to this change). Lint clean; mypy adds zero new errors over baseline.
1 parent 73da20d commit 230a170

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

‎src/token_goat/cli.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,22 @@ def cmd_image_shrink(
14811481
def cmd_worker(
14821482
daemon: bool = typer.Option(False, "--daemon", help="Run as background daemon (otherwise interactive)"),
14831483
) -> None:
1484-
"""Internal: background worker daemon. Should be invoked by the SessionStart watchdog, not directly."""
1484+
"""Internal: background worker daemon. Should be invoked by the SessionStart watchdog, not directly.
1485+
1486+
Under CI (``TOKEN_GOAT_NO_WORKER_SPAWN=1`` in the environment) this
1487+
entry point exits immediately without invoking ``run_daemon``. The
1488+
env var is inherited by the spawned child via ``subprocess.Popen``'s
1489+
default env-passing behaviour, so a daemon launched from a test
1490+
suite (or any CI step that sets the var) terminates cleanly instead
1491+
of holding the GitHub Actions Windows step open until the six-hour
1492+
timeout fires. Direct unit tests of ``worker_daemon.run_daemon``
1493+
do not go through this entry point, so they remain unaffected.
1494+
"""
1495+
if os.environ.get("TOKEN_GOAT_NO_WORKER_SPAWN", "").strip().lower() in (
1496+
"1", "true", "yes", "on",
1497+
):
1498+
return
1499+
14851500
from . import worker_daemon # noqa: PLC0415
14861501

14871502
worker_daemon.run_daemon()

‎src/token_goat/worker.py‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,12 +1176,12 @@ def ensure_running() -> int | None:
11761176
Spawning a duplicate would just lose the claim race and exit,
11771177
and clearing its pid file would orphan a working daemon.
11781178
1179-
Under CI (``TOKEN_GOAT_NO_WORKER_SPAWN=1``) the spawn step is skipped
1180-
entirely. GitHub Actions on Windows wraps each step in a Win32 job
1181-
object that tracks every descendant — a detached worker daemon's
1182-
infinite loop keeps the step alive until the global six-hour timeout
1183-
fires. The opt-out env var lets hook-level tests still exercise the
1184-
watchdog code path without leaving a daemon behind.
1179+
Under CI (``TOKEN_GOAT_NO_WORKER_SPAWN=1``) the spawn inside
1180+
:func:`spawn_detached` is suppressed so a detached daemon's infinite
1181+
loop cannot hold the GitHub Actions Windows step open until the
1182+
global six-hour timeout fires — see ``spawn_detached`` for the env
1183+
var details. The watchdog path itself still runs end-to-end so the
1184+
rest of the state machine remains testable.
11851185
"""
11861186
if is_worker_alive():
11871187
try:
@@ -1198,18 +1198,6 @@ def ensure_running() -> int | None:
11981198
if busy_pid is not None:
11991199
return busy_pid
12001200

1201-
# CI opt-out: short-circuit before spawning so a test that exercises
1202-
# the watchdog path does not leave a detached daemon attached to the
1203-
# action runner. The env var is read each call rather than cached so
1204-
# individual tests can set/unset it via ``monkeypatch.setenv``.
1205-
if os.environ.get("TOKEN_GOAT_NO_WORKER_SPAWN", "").strip().lower() in (
1206-
"1", "true", "yes", "on",
1207-
):
1208-
_LOG.debug(
1209-
"ensure_running: spawn suppressed by TOKEN_GOAT_NO_WORKER_SPAWN env var",
1210-
)
1211-
return None
1212-
12131201
# Either nothing was running, or we just reaped a hung worker. Clear stale
12141202
# pid/claim state so the fresh worker can take the slot cleanly.
12151203
_clear_pid()

0 commit comments

Comments
 (0)