Merge remote-tracking branch 'origin/main' into item-420
# Conflicts: # bin/loop_claude/test_loop_claude.py # bin/loop_effects/test_effects.py
This commit is contained in:
@@ -10,6 +10,7 @@ import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import termios
|
||||
import threading
|
||||
import time
|
||||
import typing
|
||||
|
||||
@@ -48,8 +49,8 @@ class Effects:
|
||||
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 current directory; a Failure where it could not.
|
||||
dm: typing.Callable[[str], Failure | 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]
|
||||
@@ -63,6 +64,8 @@ class Effects:
|
||||
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.
|
||||
@@ -108,7 +111,7 @@ def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BAC
|
||||
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: notify(text, None, pathlib.Path.home(), backends),
|
||||
dm=lambda text, directory: notify(text, directory, pathlib.Path.home(), backends),
|
||||
execv=os.execv,
|
||||
git=git_repo.git,
|
||||
input=input,
|
||||
@@ -119,6 +122,7 @@ def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BAC
|
||||
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,
|
||||
)
|
||||
@@ -145,6 +149,19 @@ def run_python(args: list[str], cwd: str, timeout: float) -> subprocess.Complete
|
||||
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]
|
||||
|
||||
@@ -7,6 +7,7 @@ import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
import unittest.mock
|
||||
@@ -14,7 +15,7 @@ import unittest.mock
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from loop_effects import child_shells
|
||||
from loop_effects.effects import run_python, say, StreamingClaude, system_effects
|
||||
from loop_effects.effects import run_python, say, start_timer, StreamingClaude, system_effects
|
||||
|
||||
NOW = 1790000000
|
||||
# How a line said at NOW opens, with clocked's local_time.
|
||||
@@ -283,3 +284,35 @@ class StreamingClaudeLines(unittest.TestCase):
|
||||
return
|
||||
time.sleep(0.05)
|
||||
self.fail("the shell did not start")
|
||||
|
||||
|
||||
class Timer(unittest.TestCase):
|
||||
def test_runs_the_action_beside_the_caller_once_the_delay_has_passed(self):
|
||||
ran = threading.Event()
|
||||
start_timer(0.05, ran.set)
|
||||
self.assertTrue(ran.wait(10))
|
||||
|
||||
def test_the_systems_timer_runs_on_a_daemon_thread_so_a_loop_that_exits_while_it_waits_does_not_wait_for_it(self):
|
||||
self.assertIs(system_effects().timer, start_timer)
|
||||
before = set(threading.enumerate())
|
||||
stop = start_timer(60, lambda: None)
|
||||
self.addCleanup(stop)
|
||||
self.assertEqual([thread.daemon for thread in set(threading.enumerate()) - before], [True])
|
||||
|
||||
def test_a_timer_stopped_before_its_delay_has_passed_never_runs_the_action(self):
|
||||
ran = threading.Event()
|
||||
# stop returns once the timer's thread has ended, so the action cannot run later.
|
||||
start_timer(0.2, ran.set)()
|
||||
self.assertFalse(ran.is_set())
|
||||
|
||||
def test_stopping_a_timer_whose_action_runs_waits_for_the_action_to_end(self):
|
||||
started, ended = threading.Event(), threading.Event()
|
||||
|
||||
def action():
|
||||
started.set()
|
||||
time.sleep(0.2)
|
||||
ended.set()
|
||||
stop = start_timer(0, action)
|
||||
self.assertTrue(started.wait(10))
|
||||
stop()
|
||||
self.assertTrue(ended.is_set())
|
||||
|
||||
Reference in New Issue
Block a user