Keep a worker's shells that run a busy tool, name the shell's real command and fix the review's prose findings
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
"""The shells a running claude has started, read from /proc (Linux), and how to stop one."""
|
||||
"""The shells a running claude has started, read from /proc (Linux), and how to stop one.
|
||||
|
||||
Claude Code starts each Bash call as `bash -c "source <snapshot> && … && eval '<the command>'"` and writes its output to a file under /tmp/claude-<uid>/.
|
||||
"""
|
||||
import dataclasses
|
||||
import os
|
||||
import signal
|
||||
@@ -6,8 +9,9 @@ import stat
|
||||
|
||||
# The command names of a shell claude starts for a Bash call.
|
||||
SHELL_NAMES = frozenset({"bash", "dash", "sh", "zsh"})
|
||||
# How much of a shell's command line a Shell keeps.
|
||||
COMMAND_CHARS = 200
|
||||
COMMAND_CHARS = 300
|
||||
# Tools whose process below a shell shows work that may go quiet for long: run-heavy holds a slot of the machine's heavy work or waits for one, and chunk-pr waits for a merge's approval and checks.
|
||||
BUSY_TOOLS = frozenset({"chunk-pr", "run-heavy"})
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -15,11 +19,12 @@ class Shell:
|
||||
pid: int
|
||||
# Epoch seconds at which the shell started.
|
||||
started: float
|
||||
# What the shell runs, without the snapshot Claude Code sources before it.
|
||||
command: str
|
||||
# Epoch seconds of the last write to the file the shell's output goes to; its start where the output goes to no file.
|
||||
# Epoch seconds of the last write to the file the shell's output goes to; the shell's start where the output goes to no file, which the check cannot read.
|
||||
written: float
|
||||
# Whether the shell or one of its descendants is run-heavy, which holds a slot of the machine's heavy work or waits for one.
|
||||
holds_slot: bool
|
||||
# Whether a process below the shell runs one of BUSY_TOOLS.
|
||||
runs_busy_tool: bool
|
||||
|
||||
|
||||
def parent_and_start(pid: int) -> tuple[int, int, str] | None:
|
||||
@@ -39,16 +44,16 @@ def children_of(pid: int) -> list[int]:
|
||||
return [int(entry) for entry in os.listdir("/proc") if entry.isdigit() and (found := parent_and_start(int(entry))) and found[0] == pid]
|
||||
|
||||
|
||||
def command_of(pid: int) -> str:
|
||||
def argv_of(pid: int) -> list[str]:
|
||||
try:
|
||||
with open(f"/proc/{pid}/cmdline", "rb") as handle:
|
||||
return handle.read().replace(b"\0", b" ").decode(errors="replace").strip()
|
||||
return handle.read().decode(errors="replace").rstrip("\0").split("\0")
|
||||
except OSError:
|
||||
return ""
|
||||
return []
|
||||
|
||||
|
||||
def descendants(pid: int) -> list[int]:
|
||||
"""Every process below pid, nearest first."""
|
||||
"""Every process below pid, each after its parent."""
|
||||
found = children_of(pid)
|
||||
for child in list(found):
|
||||
found += descendants(child)
|
||||
@@ -61,7 +66,7 @@ def boot_time() -> float:
|
||||
|
||||
|
||||
def output_written(pid: int, started: float) -> float:
|
||||
"""When the file that pid's standard output goes to was last written; started where that is a pipe or a terminal, whose writes leave no time."""
|
||||
"""When the file that pid's standard output goes to was last written; started where it goes to a pipe or a terminal."""
|
||||
try:
|
||||
info = os.stat(f"/proc/{pid}/fd/1")
|
||||
except OSError:
|
||||
@@ -69,6 +74,18 @@ def output_written(pid: int, started: float) -> float:
|
||||
return info.st_mtime if stat.S_ISREG(info.st_mode) else started
|
||||
|
||||
|
||||
def command_of(shell_argv: list[str]) -> str:
|
||||
"""What a shell runs: the text of its command line between Claude Code's `eval '` and the `' < /dev/null` that ends it, or the whole line where it holds none."""
|
||||
line = " ".join(shell_argv)
|
||||
_, found, command = line.partition(" && eval ")
|
||||
return (command.split(" < /dev/null", 1)[0].removeprefix("'").removesuffix("'") if found else line)[:COMMAND_CHARS]
|
||||
|
||||
|
||||
def runs_busy_tool(pid: int) -> bool:
|
||||
"""Whether a process below pid has one of BUSY_TOOLS as its program or its script, as `python3 /x/chunk-pr`. A command line that merely names one, such as a `pgrep -f chunk-pr`, doesn't count."""
|
||||
return any(os.path.basename(name) in BUSY_TOOLS for each in descendants(pid) for name in argv_of(each)[:2])
|
||||
|
||||
|
||||
def shells_of(claude_pid: int) -> list[Shell]:
|
||||
"""The shells that are children of claude_pid."""
|
||||
booted = boot_time()
|
||||
@@ -79,13 +96,12 @@ def shells_of(claude_pid: int) -> list[Shell]:
|
||||
if found is None or found[2] not in SHELL_NAMES:
|
||||
continue
|
||||
started = booted + found[1] / ticks
|
||||
below = [pid, *descendants(pid)]
|
||||
shells.append(Shell(pid, started, command_of(pid)[:COMMAND_CHARS], output_written(pid, started), any("run-heavy" in command_of(each) for each in below)))
|
||||
shells.append(Shell(pid, started, command_of(argv_of(pid)), output_written(pid, started), runs_busy_tool(pid)))
|
||||
return shells
|
||||
|
||||
|
||||
def stop(shell: Shell) -> None:
|
||||
"""Ends the shell and everything below it, so the loop it ran doesn't outlive it."""
|
||||
"""Ends the shell and every process below it, so that a loop the shell runs doesn't outlive it."""
|
||||
for pid in [*reversed(descendants(shell.pid)), shell.pid]:
|
||||
try:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
|
||||
Reference in New Issue
Block a user