Answer product-owner round 1: 0 🔴, 3 🟡 (0 misread); check a shell that holds a heavy slot, name the process's command, and judge each shell once

This commit is contained in:
2026-11-19 09:56:43 +01:00
parent ed241f3ab5
commit f3af8fda13
5 changed files with 75 additions and 42 deletions
+15 -8
View File
@@ -27,14 +27,16 @@ class Shell:
command: str
# 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 a process below the shell runs one of BUSY_TOOLS or holds a heavy slot, a file of slot_folder that run-heavy leaves open in the command it runs.
# Whether a process below the shell runs one of BUSY_TOOLS.
runs_busy_tool: bool
# CPU seconds the shell and every process below it have used.
cpu: float = 0.0
# Each process below the shell, as `<pid> <state> <CPU seconds> s <command line>`, with the state letter /proc gives, such as S for sleeping.
processes: tuple[str, ...] = ()
# The last OUTPUT_BYTES of the file the shell's output goes to; empty where it goes to no file.
# The last OUTPUT_BYTES of the file the shell's output goes to; empty where it goes to a pipe or a terminal.
output: str = ""
# Whether a process below the shell holds a heavy slot: a file of slot_folder, which run-heavy leaves open in the command it runs.
holds_slot: bool = False
def stat_of(pid: int) -> tuple[str, list[str]] | None:
@@ -55,7 +57,7 @@ def parent_and_start(pid: int) -> tuple[int, int, str] | None:
def cpu_and_state(pid: int) -> tuple[float, str]:
"""The CPU seconds a process has used, in user and kernel mode, and its state letter; none and `gone` where it is gone."""
"""The CPU seconds a process has used, in user and kernel mode, and its state letter; 0.0 and `gone` where it is gone."""
found = stat_of(pid)
if found is None:
return 0.0, "gone"
@@ -129,7 +131,7 @@ def command_of(shell_argv: list[str]) -> str:
return (command.rsplit(" < /dev/null", 1)[0].removeprefix("'").removesuffix("'") if found else line)[:COMMAND_CHARS]
def holds_slot(pid: int, slots: pathlib.Path) -> bool:
def has_slot(pid: int, slots: pathlib.Path) -> bool:
"""Whether pid has a file of the heavy slots' folder open. run-heavy replaces itself with its command once it holds a slot, and the command keeps the slot's file open."""
try:
return any(os.readlink(f"/proc/{pid}/fd/{fd}").startswith(f"{slots}/") for fd in os.listdir(f"/proc/{pid}/fd"))
@@ -137,9 +139,14 @@ def holds_slot(pid: int, slots: pathlib.Path) -> bool:
return False
def runs_busy_tool(pid: int, slots: pathlib.Path) -> bool:
"""Whether a process below pid holds a heavy slot or has one of BUSY_TOOLS as its program or its script, as `python3 /x/chunk-pr`. A command line that merely names a tool, such as a `pgrep -f chunk-pr`, doesn't count."""
return any(holds_slot(each, slots) or any(os.path.basename(name) in BUSY_TOOLS for name in argv_of(each)[:2]) for each in descendants(pid))
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 a tool, 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 holds_slot(pid: int, slots: pathlib.Path) -> bool:
"""Whether a process below pid holds a heavy slot."""
return any(has_slot(each, slots) for each in descendants(pid))
def shells_of(claude_pid: int) -> list[Shell]:
@@ -156,7 +163,7 @@ def shells_of(claude_pid: int) -> list[Shell]:
continue
started = booted + found[1] / ticks
cpu, processes = tree_of(pid)
shells.append(Shell(pid, started, command_of(argv_of(pid)), output_written(pid, started), runs_busy_tool(pid, slots), cpu, processes, output_tail(pid)))
shells.append(Shell(pid, started, command_of(argv_of(pid)), output_written(pid, started), runs_busy_tool(pid), cpu, processes, output_tail(pid), holds_slot(pid, slots)))
return shells