Answer stability round 1: 1 🔴, 4 🟡 (1 misread); keep a shell whose command holds a heavy slot, and say a failed record write on stderr

This commit is contained in:
2026-11-18 20:12:40 +01:00
parent abae3a0559
commit a7439c2e00
6 changed files with 108 additions and 66 deletions
+22 -8
View File
@@ -4,13 +4,16 @@ Claude Code starts each Bash call as `bash -c "source <snapshot> && … && eval
"""
import dataclasses
import os
import pathlib
import signal
import stat
from machine_load.slots import slot_folder
# The command names of a shell claude starts for a Bash call.
SHELL_NAMES = frozenset({"bash", "dash", "sh", "zsh"})
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.
# Scripts whose process below a shell shows work that may go quiet for long: run-heavy while it waits for a slot, and chunk-pr, which waits for a merge's approval and checks.
BUSY_TOOLS = frozenset({"chunk-pr", "run-heavy"})
@@ -23,7 +26,7 @@ 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.
# 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.
runs_busy_tool: bool
@@ -75,19 +78,30 @@ def output_written(pid: int, started: float) -> float:
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."""
"""What a shell runs: the text of its command line after Claude Code's `eval '`, up to the first ` < /dev/null`, which a call that began in the foreground lacks; the whole line where it holds no `eval`."""
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 holds_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"))
except OSError:
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 shells_of(claude_pid: int) -> list[Shell]:
"""The shells that are children of claude_pid."""
"""The shells that are children of claude_pid; none where /proc doesn't exist, as off Linux."""
if not os.path.isdir("/proc"):
return []
slots = slot_folder(os.environ, pathlib.Path.home())
booted = boot_time()
ticks = os.sysconf("SC_CLK_TCK")
shells = []
@@ -96,7 +110,7 @@ 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
shells.append(Shell(pid, started, command_of(argv_of(pid)), output_written(pid, started), runs_busy_tool(pid)))
shells.append(Shell(pid, started, command_of(argv_of(pid)), output_written(pid, started), runs_busy_tool(pid, slots)))
return shells