Compact a waiting worker's session shortly before its prompt cache expires
CI / tests (pull_request) Failing after 3m16s

This commit is contained in:
2026-11-17 13:00:25 +01:00
parent 0aec288e75
commit e60e891251
9 changed files with 186 additions and 5 deletions
+17
View File
@@ -9,6 +9,7 @@ import signal
import subprocess
import sys
import termios
import threading
import time
import typing
@@ -51,6 +52,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.
@@ -86,6 +89,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,
)
@@ -112,6 +116,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 loop that exits while it waits doesn't wait for the action.
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]
+26 -1
View File
@@ -6,13 +6,14 @@ import pathlib
import signal
import sys
import tempfile
import threading
import time
import unittest
import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from loop_effects.effects import run_python, say, system_effects
from loop_effects.effects import run_python, say, start_timer, system_effects
NOW = 1790000000
# How a line said at NOW opens, with clocked's local_time.
@@ -134,3 +135,27 @@ class PythonEffect(unittest.TestCase):
done = run_python(["-c", "pass"], gone, 60)
self.assertEqual(done.returncode, 1)
self.assertIn(gone, done.stderr)
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_a_timer_stopped_before_its_delay_has_passed_never_runs_the_action(self):
ran = threading.Event()
start_timer(0.2, ran.set)()
self.assertFalse(ran.wait(0.5))
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())