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:
@@ -11,7 +11,7 @@ from loop_claude.chunk import chunk_items, fresh_prompt
|
||||
from loop_claude.loop.chunk_windows import chunk_session, item_of, start_chunks
|
||||
from loop_claude.loop.chunk_start import chunk_start, plan_item, scored_plan
|
||||
from chunk_driver.chunk_run import closed_cycle
|
||||
from agent_cycle.run import Cycle, run_cycle
|
||||
from agent_cycle.run import Concluded, Cycle, run_cycle
|
||||
from loop_claude.loop.reload import decode_handed, exec_if_edited, fingerprint, LoopRun, RELOAD_FLAG
|
||||
from loop_effects.effects import Effects, say
|
||||
from failure import Failure
|
||||
@@ -20,6 +20,7 @@ from loop_claude.loop.usage import HELP
|
||||
from chunk_driver.this_loop import Loop
|
||||
from loop_state.paths import Paths
|
||||
from transcript_archive.project_key import project_key
|
||||
from model_routing.ladder import ProjectRung, Rung
|
||||
from chunk_driver.project.premises import read_premises
|
||||
from chunk_driver.project.records import read_records
|
||||
from model_routing.ladder_file import SHIPPED
|
||||
@@ -28,9 +29,31 @@ from agent_cycle.direct_message import tell
|
||||
from agent_cycle import tmux
|
||||
|
||||
|
||||
# Starts the chunks that may start now, as chunk_windows.start_chunks does.
|
||||
StartChunks = typing.Callable[[LoopRun, bool, Paths, typing.Mapping[str, str], pathlib.Path, Effects], list[str] | Failure]
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Steps:
|
||||
"""The steps a loop takes, each the function of its name, which main binds."""
|
||||
start_chunks: StartChunks
|
||||
fingerprint: typing.Callable[[pathlib.Path], str]
|
||||
exec_if_edited: typing.Callable[[Loop, str, LoopRun], Stop | None]
|
||||
next_cycle: typing.Callable[[LoopRun, Loop], Cycle | Stop]
|
||||
run_cycle: typing.Callable[[Cycle], Stop | Concluded]
|
||||
closed_cycle: typing.Callable[[LoopRun, ProjectRung | None, Concluded, pathlib.Path, str, Loop], LoopRun | Stop]
|
||||
check_routing: typing.Callable[[pathlib.Path, Rung, Loop], Stop | None]
|
||||
|
||||
|
||||
def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> int:
|
||||
steps = Steps(start_chunks, fingerprint, exec_if_edited, next_cycle, run_cycle, closed_cycle, check_routing)
|
||||
return exit_code(lambda: run_loop(argv, env, paths, launcher, cwd, effects, steps), cwd, effects)
|
||||
|
||||
|
||||
def exit_code(run: typing.Callable[[], Stop], cwd: pathlib.Path, effects: Effects) -> int:
|
||||
"""The exit code of the loop that run runs in cwd, once its stop is said and sent as a DM."""
|
||||
try:
|
||||
stop = run_loop(argv, env, paths, launcher, cwd, effects)
|
||||
stop = run()
|
||||
except KeyboardInterrupt:
|
||||
stop = Stop(130, "")
|
||||
except Exception as crash:
|
||||
@@ -44,7 +67,7 @@ def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher:
|
||||
return stop.code
|
||||
|
||||
|
||||
def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects) -> Stop:
|
||||
def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, cwd: pathlib.Path, effects: Effects, steps: Steps) -> Stop:
|
||||
loop_run = loop_run_from(argv, cwd, effects)
|
||||
if isinstance(loop_run, Stop):
|
||||
return loop_run
|
||||
@@ -52,38 +75,38 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc
|
||||
if reason := tmux.unusable(effects.tmux, fresh_prompt.DRIVER):
|
||||
return Stop(1, reason)
|
||||
if loop_run.chunk is None:
|
||||
return windows_opened(loop_run, env, paths, launcher, effects)
|
||||
return windows_opened(loop_run, env, paths, launcher, effects, steps.start_chunks)
|
||||
pane = env.get("TMUX_PANE")
|
||||
if not pane:
|
||||
return Stop(1, f"{CHUNK_FLAG} is how loop-claude starts itself in a tmux window; run loop-claude <task>")
|
||||
baseline = fingerprint(launcher)
|
||||
baseline = steps.fingerprint(launcher)
|
||||
key = project_key(loop_run.root, paths.home)
|
||||
window = tmux.Window(loop_session(loop_run.root, paths.home), loop_run.chunk, pane)
|
||||
loop = Loop(root=loop_run.root, session=chunk_session(key, loop_run.chunk, paths.claude_code_transcripts), window=window, paths=paths, env=env, launcher=launcher, effects=effects)
|
||||
while True:
|
||||
if stop := exec_if_edited(loop, baseline, loop_run):
|
||||
if stop := steps.exec_if_edited(loop, baseline, loop_run):
|
||||
return stop
|
||||
loop_run = dataclasses.replace(loop_run, cycle_number=loop_run.cycle_number + 1)
|
||||
cycle = next_cycle(loop_run, loop)
|
||||
cycle = steps.next_cycle(loop_run, loop)
|
||||
if isinstance(cycle, Stop):
|
||||
return cycle
|
||||
concluded = run_cycle(cycle)
|
||||
concluded = steps.run_cycle(cycle)
|
||||
if isinstance(concluded, Stop):
|
||||
return concluded
|
||||
closed = closed_cycle(loop_run, cycle.project, concluded, cycle.project_archive, fresh_prompt.DRIVER, loop)
|
||||
closed = steps.closed_cycle(loop_run, cycle.project, concluded, cycle.project_archive, fresh_prompt.DRIVER, loop)
|
||||
if isinstance(closed, Stop):
|
||||
return closed
|
||||
loop_run = closed
|
||||
# Off the ladder the worker runs on the project's model itself, which its route names.
|
||||
if stop := check_routing(archive_root(paths.home, env), cycle.project.rung if cycle.project else cycle.route.rung, loop):
|
||||
if stop := steps.check_routing(archive_root(paths.home, env), cycle.project.rung if cycle.project else cycle.route.rung, loop):
|
||||
return stop
|
||||
if isinstance(concluded.verdict, Finished):
|
||||
return handed_on(loop_run, loop)
|
||||
return handed_on(loop_run, loop, steps.start_chunks)
|
||||
|
||||
|
||||
def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects) -> Stop:
|
||||
"""What the loop-claude the human runs does: starts each chunk that may start now in a window of the project's tmux session, then puts that session on the terminal until the human detaches."""
|
||||
started = start_chunks(loop_run, False, paths, env, launcher, effects)
|
||||
def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects, start: StartChunks) -> Stop:
|
||||
"""What the loop-claude the human runs does: starts, through start, each chunk that may start now in a window of the project's tmux session, then puts that session on the terminal until the human detaches."""
|
||||
started = start(loop_run, False, paths, env, launcher, effects)
|
||||
if isinstance(started, Failure):
|
||||
return Stop(1, started.reason)
|
||||
session = loop_session(loop_run.root, paths.home)
|
||||
@@ -95,9 +118,9 @@ def windows_opened(loop_run: LoopRun, env: typing.Mapping[str, str], paths: Path
|
||||
return Stop(1, f"`tmux {' '.join(shown)}` failed; the chunks go on in the tmux session") if effects.tmux_on_terminal(shown) else Stop(0, "")
|
||||
|
||||
|
||||
def handed_on(loop_run: LoopRun, loop: Loop) -> Stop:
|
||||
"""The stop of a loop whose chunk finished, once it has started each chunk that may start now. Its window closes as it exits."""
|
||||
started = start_chunks(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)
|
||||
def handed_on(loop_run: LoopRun, loop: Loop, start: StartChunks) -> Stop:
|
||||
"""The stop of a loop whose chunk finished, once it has started, through start, each chunk that may start now. Its window closes as it exits."""
|
||||
started = start(loop_run, True, loop.paths, loop.env, loop.launcher, loop.effects)
|
||||
if isinstance(started, Failure):
|
||||
return Stop(1, f"the chunk finished, but the next could not start: {started.reason}")
|
||||
say(f"the chunk finished; started {', '.join(started) or 'no other, since the chunks running fill what may run now'}", loop.effects, stdout=True)
|
||||
|
||||
Reference in New Issue
Block a user