e55b8826fb
# Conflicts: # bin/agent_cycle/interrupt.py # bin/agent_cycle/run.py # bin/agent_cycle/states.py # docs/reading-cost.md # todo.md
146 lines
6.7 KiB
Python
146 lines
6.7 KiB
Python
"""The effects a tool reaches the world through, such as its terminal, clock, processes, git and the machine's memory.
|
|
|
|
A change here, such as to how input reads a line, changes every tool that runs on them.
|
|
"""
|
|
import dataclasses
|
|
import os
|
|
import pathlib
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import termios
|
|
import threading
|
|
import time
|
|
import typing
|
|
|
|
from notifier.notify import Backend, BACKENDS, notify
|
|
from failure import Failure
|
|
from git_cli import git_repo
|
|
from machine_load.memory import Memory, read_memory
|
|
from project_config.settings import Notifier
|
|
|
|
# Seconds before a Python script loop-claude runs is stopped, so a hung one, such as chunk-preflight, cannot hold the loop.
|
|
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 wait(self) -> int: ...
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Effects:
|
|
claude: typing.Callable[[list[str], str, str], subprocess.CompletedProcess[str]]
|
|
claude_on_terminal: typing.Callable[[list[str], str, dict[str, str]], int]
|
|
# Drops what was typed on the terminal and not yet read.
|
|
discard_typed: typing.Callable[[], None]
|
|
# Sends the human a direct message through the notifier the project's config names, as `notify` does, for the project whose checkout holds the given directory, else the current directory; a Failure where it could not.
|
|
dm: typing.Callable[[str, str | None], Failure | None]
|
|
execv: typing.Callable[[str, list[str]], None]
|
|
git: git_repo.Git
|
|
input: typing.Callable[[str], str]
|
|
# A date and time in the machine's zone, as `2026-10-28 11:46:01`. lilleman chose this spelling and local time over UTC on 2026-10-28, since the human reads these on their own clock.
|
|
local_time: typing.Callable[[float], str]
|
|
# The machine's memory; None where it cannot be read.
|
|
memory: typing.Callable[[], Memory | None]
|
|
now: typing.Callable[[], float]
|
|
on_terminal: typing.Callable[[], bool]
|
|
# Runs a Python script in a directory, as run_python does.
|
|
python: typing.Callable[[list[str], str], subprocess.CompletedProcess[str]]
|
|
sleep: typing.Callable[[float], None]
|
|
stream_claude: typing.Callable[[list[str], str, dict[str, str]], Streaming]
|
|
# Starts a timer, as start_timer does.
|
|
timer: typing.Callable[[float, typing.Callable[[], None]], typing.Callable[[], None]]
|
|
# Runs tmux with these arguments, as run_tmux does.
|
|
tmux: typing.Callable[[list[str]], subprocess.CompletedProcess[str]]
|
|
# Runs tmux on the terminal, as to attach a session, and returns its exit code once the human detaches.
|
|
tmux_on_terminal: typing.Callable[[list[str]], int]
|
|
|
|
|
|
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)
|
|
|
|
def lines(self) -> typing.Iterable[str]:
|
|
with self.proc:
|
|
yield from self.proc.stdout or ()
|
|
|
|
def wait(self) -> int:
|
|
return self.proc.wait()
|
|
|
|
|
|
def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS) -> Effects:
|
|
"""The effects on this machine; `backends` are the notifiers loop-claude's DM can go through, where a test swaps in one that sends nowhere."""
|
|
return Effects(
|
|
claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=stdin, text=True),
|
|
claude_on_terminal=claude_on_terminal,
|
|
discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH),
|
|
dm=lambda text, directory: notify(text, directory, pathlib.Path.home(), backends),
|
|
execv=os.execv,
|
|
git=git_repo.git,
|
|
input=input,
|
|
local_time=lambda seconds: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(seconds)),
|
|
memory=read_memory,
|
|
now=time.time,
|
|
on_terminal=lambda: sys.stdin.isatty() and sys.stdout.isatty(),
|
|
python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT),
|
|
sleep=time.sleep,
|
|
stream_claude=StreamingClaude,
|
|
timer=start_timer,
|
|
tmux=run_tmux,
|
|
tmux_on_terminal=lambda args: subprocess.run(["tmux", *args]).returncode,
|
|
)
|
|
|
|
|
|
def claude_on_terminal(args: list[str], cwd: str, env: dict[str, str]) -> int:
|
|
"""Runs claude on the terminal in cwd, with env added to its environment, and returns its exit code. While claude runs, Ctrl-C belongs to it: this process shares its terminal, and ignores the key."""
|
|
# A Python handler, unlike SIG_IGN, is reset to the default as claude starts, so Ctrl-C still reaches claude.
|
|
previous = signal.signal(signal.SIGINT, lambda number, frame: None)
|
|
try:
|
|
return subprocess.run(["claude", *args], cwd=cwd, env={**os.environ, **env}).returncode
|
|
finally:
|
|
signal.signal(signal.SIGINT, previous)
|
|
|
|
|
|
def run_python(args: list[str], cwd: str, timeout: float) -> subprocess.CompletedProcess[str]:
|
|
"""Runs a Python script in cwd. One that cannot start, or still runs after timeout seconds and is stopped, reads as exit 1, saying why on stderr."""
|
|
command = [sys.executable, *args]
|
|
try:
|
|
return subprocess.run(command, capture_output=True, cwd=cwd, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, timeout=timeout)
|
|
except OSError as failure:
|
|
return subprocess.CompletedProcess(command, 1, "", f"could not start: {failure}")
|
|
except subprocess.TimeoutExpired:
|
|
return subprocess.CompletedProcess(command, 1, "", f"stopped after {timeout} s")
|
|
|
|
|
|
def start_timer(delay: float, action: typing.Callable[[], None]) -> typing.Callable[[], None]:
|
|
"""Runs action on a thread of its own once delay seconds have passed, and returns what stops the timer: it cancels an action not yet started, and waits for one that has started to end."""
|
|
timer = threading.Timer(delay, action)
|
|
# A timer still running doesn't keep the process alive once the loop exits.
|
|
timer.daemon = True
|
|
timer.start()
|
|
|
|
def stop() -> None:
|
|
timer.cancel()
|
|
timer.join()
|
|
return stop
|
|
|
|
|
|
def run_tmux(args: list[str]) -> subprocess.CompletedProcess[str]:
|
|
"""Runs tmux and returns what it printed. A tmux that cannot start, as where none is installed, reads as exit 127, saying why on stderr."""
|
|
command = ["tmux", *args]
|
|
try:
|
|
return subprocess.run(command, capture_output=True, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL)
|
|
except OSError as failure:
|
|
return subprocess.CompletedProcess(command, 127, "", f"could not start: {failure}")
|
|
|
|
|
|
def say(text: str, effects: Effects, *, stdout: bool = False) -> None:
|
|
"""Prints text on stderr, or on stdout where stdout, opening the first line of words with the machine's local time. Blank lines before those words stay blank, and later lines get no time."""
|
|
body = text.lstrip("\n")
|
|
blanks = text[: len(text) - len(body)]
|
|
print(f"{blanks}{effects.local_time(effects.now())} {body}", file=sys.stdout if stdout else sys.stderr, flush=True)
|