End a worker that lingers after its final result by stopping the quiet background shells it left
CI / tests (pull_request) Failing after 4m10s
CI / tests (pull_request) Failing after 4m10s
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
"""Ends a worker whose claude -p printed its final result and then waits on a background shell that never ends.
|
||||
|
||||
Claude Code's own time limit for a background command cannot do this: `BASH_DEFAULT_TIMEOUT_MS` and `BASH_MAX_TIMEOUT_MS` only raise the limit, and a call's `timeout` of up to 2 hours replaces the 10 minutes of a `claude -p` run (docs: code.claude.com/docs/en/tools-reference.md → "Time limit for background commands"). On 2026-11-18 a worker waited 95 minutes in an `until` loop whose `pgrep -f` matched its own command line.
|
||||
"""
|
||||
import pathlib
|
||||
|
||||
from loop_effects.child_shells import Shell
|
||||
from loop_effects.effects import Effects, Streaming, say
|
||||
|
||||
# Seconds between the checks, counted from the last line the worker printed.
|
||||
CHECK_EVERY = 300
|
||||
# Seconds a final result must have stood, and a shell's output must have been quiet, before a shell is stopped.
|
||||
SETTLED = 180
|
||||
RECORD = "lingering-workers.md"
|
||||
|
||||
|
||||
def stopped_shells(proc: Streaming, settled_at: float | None, record: pathlib.Path, effects: Effects) -> None:
|
||||
"""Stops the shells of a claude that printed its final result at settled_at and is still alive, and says so on the terminal and in record. Stops nothing where the claude is still working, or a shell writes output or holds a run-heavy slot."""
|
||||
now = effects.now()
|
||||
if settled_at is None or now - settled_at <= SETTLED:
|
||||
return
|
||||
shells = proc.shells()
|
||||
if any(now - shell.written <= SETTLED or shell.holds_slot for shell in shells):
|
||||
return
|
||||
for shell in shells:
|
||||
proc.stop(shell)
|
||||
line = stop_line(shell, now)
|
||||
say(line, effects, stdout=True)
|
||||
record.parent.mkdir(parents=True, exist_ok=True)
|
||||
with record.open("a") as handle:
|
||||
handle.write(f"- {effects.local_time(now)} {line}\n")
|
||||
|
||||
|
||||
def stop_line(shell: Shell, now: float) -> str:
|
||||
return f"stopped pid {shell.pid} ({int(now - shell.started)} s old, quiet {int(now - shell.written)} s) that held a worker after its final result: {shell.command}"
|
||||
@@ -122,7 +122,7 @@ def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None, stepped: boo
|
||||
if isinstance(answered, QuotedAnswer):
|
||||
say(f"\n=== {prompts.QUOTE_SOURCES} answered: {answered.question} ===\n{answered.answer}", cycle.effects, stdout=True)
|
||||
# worker_start runs inside the lambda: a failed attempt may have opened the session.
|
||||
if stop := retried("worker", lambda: run_worker(cycle.root, worker_start(cycle, answered, stepped), worker_environment(cycle), cycle.effects), cycle.effects):
|
||||
if stop := retried("worker", lambda: run_worker(cycle.root, worker_start(cycle, answered, stepped), worker_environment(cycle), cycle.project_archive, cycle.effects), cycle.effects):
|
||||
return stop
|
||||
path = transcript_file(cycle.session)
|
||||
return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}")
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import pathlib
|
||||
|
||||
from claude_code.entries import AssistantTurn, ends_stream, entry_from, hit_limit, json_object, limit_said, rejected_until
|
||||
from loop_claude.cycle.lingering_workers import CHECK_EVERY, RECORD, stopped_shells
|
||||
from loop_effects.effects import Effects, say
|
||||
from failure import Failure
|
||||
from claude_code.reply import Limited
|
||||
@@ -15,16 +18,22 @@ AUTO_COMPACT_WINDOW = "CLAUDE_CODE_AUTO_COMPACT_WINDOW"
|
||||
COMPACT_WINDOW = 200_000
|
||||
|
||||
|
||||
def run_worker(root: str, args: list[str], env: dict[str, str], effects: Effects) -> Failure | Limited | None:
|
||||
def run_worker(root: str, args: list[str], env: dict[str, str], project_archive: pathlib.Path, effects: Effects) -> Failure | Limited | None:
|
||||
wait_for_memory(effects)
|
||||
try:
|
||||
proc = effects.stream_claude(args, root, env)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
result: dict | None = None
|
||||
# When the last line the worker printed, if it is a result: a worker that prints on is working.
|
||||
settled_at: float | None = None
|
||||
resets: list[int] = []
|
||||
for raw in proc.lines():
|
||||
for raw in proc.lines(CHECK_EVERY):
|
||||
if raw is None:
|
||||
stopped_shells(proc, settled_at, project_archive / RECORD, effects)
|
||||
continue
|
||||
event = json_object(raw) or {}
|
||||
settled_at = effects.now() if ends_stream(event) else None
|
||||
if ends_stream(event):
|
||||
result = event
|
||||
if (resets_at := rejected_until(event)) is not None:
|
||||
|
||||
Reference in New Issue
Block a user