Run every claude the loop starts in a Claude home generated outside the clone
CI / tests (pull_request) Successful in 4m45s
CI / tests (pull_request) Successful in 4m45s
This commit is contained in:
@@ -16,6 +16,7 @@ import typing
|
||||
|
||||
from notifier.notify import Backend, BACKENDS, notify
|
||||
from loop_effects import child_shells
|
||||
from loop_state.paths import claude_home, in_claude_home, RELEASE_ROOT, with_release_tools
|
||||
from failure import Failure
|
||||
from git_cli import git_repo
|
||||
from machine_load.memory import Memory, read_memory
|
||||
@@ -23,7 +24,7 @@ from project_config.settings import Notifier
|
||||
from text_files.file_writes import replace_file
|
||||
from forge.commands import Run, runner
|
||||
from machine_load.slots import slot_folder
|
||||
from loop_state.paths import with_release_tools
|
||||
from claude_home.home import unready
|
||||
|
||||
# Seconds before a Python script build-release runs is stopped, so a hung one, such as chunk-preflight, cannot hold the loop.
|
||||
PYTHON_TIMEOUT = 600
|
||||
@@ -52,6 +53,8 @@ class Streaming(typing.Protocol):
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Effects:
|
||||
claude: typing.Callable[[list[str], str, str], subprocess.CompletedProcess[str]]
|
||||
# Builds or refreshes the loop's Claude home, as claude_home.home.unready does: why a claude cannot run there yet, else None.
|
||||
claude_home: typing.Callable[[], str | None]
|
||||
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]
|
||||
@@ -84,7 +87,7 @@ class Effects:
|
||||
|
||||
class StreamingClaude:
|
||||
def __init__(self, args: list[str], cwd: str, env: dict[str, str]) -> None:
|
||||
self.proc = subprocess.Popen(["claude", *args], cwd=cwd, env=with_release_tools({**os.environ, **env}), stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, bufsize=0)
|
||||
self.proc = subprocess.Popen(["claude", *args], cwd=cwd, env=claude_environment({**os.environ, **env}, pathlib.Path.home()), stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, bufsize=0)
|
||||
|
||||
def lines(self, idle: float) -> typing.Iterable[str | None]:
|
||||
# Reads the pipe's file descriptor itself. select cannot see what a buffered reader holds.
|
||||
@@ -116,12 +119,13 @@ class StreamingClaude:
|
||||
|
||||
|
||||
def system_effects(home: pathlib.Path, env: typing.Mapping[str, str], backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS, forge_timeout: float = FORGE_TIMEOUT) -> Effects:
|
||||
"""The effects on this machine: the configs of its DM are under home, and tmux and the one-shot headless claude run in env, while a streaming claude and one on a terminal run in this process's environment plus what their caller adds. Every claude starts with the release's tools first on its `PATH`.
|
||||
"""The effects on this machine: the configs of its DM are under home, and tmux and the one-shot headless claude run in env, while a streaming claude and one on a terminal run in this process's environment plus what their caller adds. Every claude starts in the loop's Claude home, with the release's tools first on its `PATH`.
|
||||
|
||||
A test hands in `backends` that don't send anything, in place of the notifiers build-release's DM goes through. It hands in a short `forge_timeout`, so a command that outlasts it ends quickly.
|
||||
"""
|
||||
return Effects(
|
||||
claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, env=with_release_tools(env), input=stdin, text=True),
|
||||
claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, env=claude_environment(env, home), input=stdin, text=True),
|
||||
claude_home=lambda: unready(claude_home(env, home), RELEASE_ROOT, lambda args: subprocess.run(["claude", *args], capture_output=True, env=claude_environment(env, home), stdin=subprocess.DEVNULL, text=True), git_repo.git),
|
||||
claude_on_terminal=claude_on_terminal,
|
||||
discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH),
|
||||
dm=lambda text, directory: notify(text, directory, home, env, backends),
|
||||
@@ -143,12 +147,17 @@ def system_effects(home: pathlib.Path, env: typing.Mapping[str, str], backends:
|
||||
)
|
||||
|
||||
|
||||
def claude_environment(env: typing.Mapping[str, str], home: pathlib.Path) -> dict[str, str]:
|
||||
"""env as every claude the loop starts gets it: the loop's Claude home under home, or where env's XDG_DATA_HOME says, and the release's tools first on `PATH`."""
|
||||
return in_claude_home(with_release_tools(env), home)
|
||||
|
||||
|
||||
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=with_release_tools({**os.environ, **env})).returncode
|
||||
return subprocess.run(["claude", *args], cwd=cwd, env=claude_environment({**os.environ, **env}, pathlib.Path.home())).returncode
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, previous)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user