Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
@@ -20,6 +20,8 @@ from failure import Failure
|
||||
from git_cli import git_repo
|
||||
from machine_load.memory import Memory, read_memory
|
||||
from project_config.settings import Notifier
|
||||
from text_files.file_writes import replace_file
|
||||
from machine_load.slots import slot_folder
|
||||
|
||||
# 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
|
||||
@@ -62,6 +64,8 @@ class Effects:
|
||||
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]]
|
||||
# Writes a file whole, as text_files.file_writes.replace_file does.
|
||||
replace_file: typing.Callable[[pathlib.Path, bytes], Failure | None]
|
||||
sleep: typing.Callable[[float], None]
|
||||
stream_claude: typing.Callable[[list[str], str, dict[str, str]], Streaming]
|
||||
# Starts a timer, as start_timer does.
|
||||
@@ -96,7 +100,7 @@ class StreamingClaude:
|
||||
yield pending.decode("utf-8", errors="replace")
|
||||
|
||||
def shells(self) -> list[child_shells.Shell]:
|
||||
return child_shells.shells_of(self.proc.pid)
|
||||
return child_shells.shells_of(self.proc.pid, slot_folder(os.environ, pathlib.Path.home()), child_shells.children_of)
|
||||
|
||||
def stop(self, shell: child_shells.Shell) -> None:
|
||||
child_shells.stop(shell)
|
||||
@@ -105,13 +109,16 @@ class StreamingClaude:
|
||||
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."""
|
||||
def system_effects(home: pathlib.Path, env: typing.Mapping[str, str], backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS) -> Effects:
|
||||
"""The effects on this machine: the configs of its DM are under home, and tmux runs in env.
|
||||
|
||||
A test hands in `backends`, the notifiers loop-claude's DM can go through, that send 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),
|
||||
dm=lambda text, directory: notify(text, directory, home, backends),
|
||||
execv=os.execv,
|
||||
git=git_repo.git,
|
||||
input=input,
|
||||
@@ -120,11 +127,12 @@ def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BAC
|
||||
now=time.time,
|
||||
on_terminal=lambda: sys.stdin.isatty() and sys.stdout.isatty(),
|
||||
python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT),
|
||||
replace_file=replace_file,
|
||||
sleep=time.sleep,
|
||||
stream_claude=StreamingClaude,
|
||||
timer=start_timer,
|
||||
tmux=run_tmux,
|
||||
tmux_on_terminal=lambda args: subprocess.run(["tmux", *args]).returncode,
|
||||
tmux=lambda args: run_tmux(args, env),
|
||||
tmux_on_terminal=lambda args: subprocess.run(["tmux", *args], env=env).returncode,
|
||||
)
|
||||
|
||||
|
||||
@@ -162,11 +170,11 @@ def start_timer(delay: float, action: typing.Callable[[], None]) -> typing.Calla
|
||||
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."""
|
||||
def run_tmux(args: list[str], env: typing.Mapping[str, str] | None) -> subprocess.CompletedProcess[str]:
|
||||
"""Runs tmux in env, else in this process's environment, 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)
|
||||
return subprocess.run(command, capture_output=True, encoding="utf-8", env=env, errors="replace", stdin=subprocess.DEVNULL)
|
||||
except OSError as failure:
|
||||
return subprocess.CompletedProcess(command, 127, "", f"could not start: {failure}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user