End a worker that lingers after its final result by stopping the quiet background shells it left
CI / tests (pull_request) Failing after 4m10s

This commit is contained in:
2026-11-18 16:53:30 +01:00
parent aa34fb8912
commit 72ffb9f748
9 changed files with 272 additions and 15 deletions
+36 -4
View File
@@ -5,6 +5,7 @@ A change here, such as to how input reads a line, changes every tool that runs o
import dataclasses
import os
import pathlib
import select
import signal
import subprocess
import sys
@@ -13,6 +14,7 @@ import time
import typing
from notifier.notify import Backend, BACKENDS, notify
from loop_effects import child_shells
from failure import Failure
from git_cli import git_repo
from machine_load.memory import Memory, read_memory
@@ -25,7 +27,17 @@ PYTHON_TIMEOUT = 600
class Streaming(typing.Protocol):
"""A started claude whose stream-json lines arrive as it works."""
def lines(self) -> typing.Iterable[str]: ...
def lines(self, idle: float) -> typing.Iterable[str | None]:
"""Each line as it arrives, and None whenever idle seconds pass without one."""
...
def shells(self) -> list[child_shells.Shell]:
"""The shells that are children of the claude."""
...
def stop(self, shell: child_shells.Shell) -> None:
"""Ends the shell and everything below it."""
...
def wait(self) -> int: ...
@@ -59,11 +71,31 @@ class Effects:
class StreamingClaude:
def __init__(self, args: list[str], cwd: str, env: dict[str, str]) -> None:
self.proc = subprocess.Popen(["claude", *args], cwd=cwd, env={**os.environ, **env}, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, stdout=subprocess.PIPE)
self.proc = subprocess.Popen(["claude", *args], cwd=cwd, env={**os.environ, **env}, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, bufsize=0)
def lines(self) -> typing.Iterable[str]:
def lines(self, idle: float) -> typing.Iterable[str | None]:
# Reads the pipe's file descriptor itself, since select cannot see what a buffered reader holds, and a thread blocked in a read keeps the pipe from closing when a Ctrl-C ends the loop.
pending = b""
with self.proc:
yield from self.proc.stdout or ()
fd = self.proc.stdout.fileno() if self.proc.stdout else -1
while True:
if not select.select([fd], [], [], idle)[0]:
yield None
continue
chunk = os.read(fd, 65536)
if not chunk:
break
*complete, pending = (pending + chunk).split(b"\n")
for line in complete:
yield line.decode("utf-8", errors="replace") + "\n"
if pending:
yield pending.decode("utf-8", errors="replace")
def shells(self) -> list[child_shells.Shell]:
return child_shells.shells_of(self.proc.pid)
def stop(self, shell: child_shells.Shell) -> None:
child_shells.stop(shell)
def wait(self) -> int:
return self.proc.wait()