Answer architecture round 1: 1 🔴, 6 🟡 (0 misread); open claude through one hand_over, route loop-inbox's /talk through the Talking state, give Ask one state
This commit is contained in:
+28
-56
@@ -24,25 +24,20 @@ bin/loop_claude/waiting.py describes.
|
||||
Exit 2 when stdin or stdout is not a terminal, or on an unknown argument.
|
||||
"""
|
||||
import curses
|
||||
import os
|
||||
import pathlib
|
||||
import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from loop_inbox.inbox import Answering, answering_lines, BACKSPACE, ENTER, ESCAPE, fitted, Listing, listing_lines, Open, pressed, Quit, Screen, Send
|
||||
from loop_claude.waiting import Ask, ask_file, read_ask, send, Talked, Talking, Typed, waiting
|
||||
from loop_claude.waiting import Ask, ask_file, Asking, claim_talk, ClaudeEnded, MAKE_WAY_SECONDS, read_ask, send, Typed, waiting
|
||||
from loop_claude.effects import Effects, system_effects
|
||||
from failure import Failure
|
||||
from loop_claude.cycle.interrupt import hand_over
|
||||
from loop_claude.paths import Paths, paths_from
|
||||
from loop_claude.effects import system_effects
|
||||
from claude_code.trust import trust
|
||||
from loop_claude.project.checkout import worktrees
|
||||
|
||||
USAGE = "usage: loop-inbox"
|
||||
# Milliseconds between refreshes of the list while no key is pressed.
|
||||
REFRESH_MS = 1000
|
||||
# Seconds loop-inbox waits for a loop to stop reading its terminal before it opens claude for /talk; the loop looks for an answer every half second.
|
||||
MAKE_WAY_SECONDS = 5
|
||||
KEYS = {"\n": ENTER, "\r": ENTER, curses.KEY_ENTER: ENTER, "\x7f": BACKSPACE, "\b": BACKSPACE, curses.KEY_BACKSPACE: BACKSPACE, "\x1b": ESCAPE}
|
||||
|
||||
|
||||
@@ -57,19 +52,19 @@ def main(argv: list[str], home: pathlib.Path) -> int:
|
||||
print("loop-inbox: stdin or stdout is not a terminal; run it on one", file=sys.stderr)
|
||||
return 2
|
||||
try:
|
||||
curses.wrapper(lambda window: run(window, paths_from(home)))
|
||||
curses.wrapper(lambda window: run(window, paths_from(home), system_effects()))
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
return 0
|
||||
|
||||
|
||||
def run(window: curses.window, paths: Paths) -> None:
|
||||
def run(window: curses.window, paths: Paths, effects: Effects) -> None:
|
||||
folder = paths.waiting
|
||||
window.timeout(REFRESH_MS)
|
||||
screen: Screen = Listing()
|
||||
while True:
|
||||
screen = refreshed(screen, folder)
|
||||
draw(window, screen)
|
||||
screen = refreshed(screen, folder, effects.alive)
|
||||
draw(window, screen, effects)
|
||||
try:
|
||||
key = window.get_wch()
|
||||
except curses.error:
|
||||
@@ -80,7 +75,7 @@ def run(window: curses.window, paths: Paths) -> None:
|
||||
case Send(ask, text):
|
||||
screen = Listing(note=sent(folder, ask, text))
|
||||
case Open(ask):
|
||||
screen = Listing(note=talked(window, paths, ask))
|
||||
screen = Listing(note=opened(window, paths, ask, effects))
|
||||
case Listing() | Answering() as next_screen:
|
||||
screen = next_screen
|
||||
|
||||
@@ -90,20 +85,20 @@ def key_name(key: str | int) -> str:
|
||||
return KEYS.get(key, key if isinstance(key, str) else "")
|
||||
|
||||
|
||||
def refreshed(screen: Screen, folder: pathlib.Path) -> Screen:
|
||||
def refreshed(screen: Screen, folder: pathlib.Path, alive: typing.Callable[[int], bool]) -> Screen:
|
||||
"""screen with the loops as they wait now, unless the human is typing a number; a question that no longer waits leaves the answer screen."""
|
||||
match screen:
|
||||
case Listing(_, "", note):
|
||||
return Listing(tuple(waiting(folder)), "", note)
|
||||
return Listing(tuple(waiting(folder, alive)), "", note)
|
||||
case Answering(ask) if not still_waits(folder, ask):
|
||||
return Listing(note=no_longer_waits(ask))
|
||||
return screen
|
||||
|
||||
|
||||
def draw(window: curses.window, screen: Screen) -> None:
|
||||
def draw(window: curses.window, screen: Screen, effects: Effects) -> None:
|
||||
height, width = window.getmaxyx()
|
||||
now = time.time()
|
||||
lines = listing_lines(screen, now, local_time, width) if isinstance(screen, Listing) else answering_lines(screen, now, local_time, width)
|
||||
now = effects.now()
|
||||
lines = listing_lines(screen, now, effects.local_time, width) if isinstance(screen, Listing) else answering_lines(screen, now, effects.local_time, width)
|
||||
window.erase()
|
||||
for row, line in enumerate(fitted(lines, height)):
|
||||
# The last cell of the screen cannot be written without an error, so each line stops one short of it.
|
||||
@@ -111,14 +106,10 @@ def draw(window: curses.window, screen: Screen) -> None:
|
||||
window.refresh()
|
||||
|
||||
|
||||
def local_time(seconds: float) -> str:
|
||||
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(seconds))
|
||||
|
||||
|
||||
def still_waits(folder: pathlib.Path, ask: Ask) -> bool:
|
||||
"""Whether ask still waits for an answer from loop-inbox."""
|
||||
now = read_ask(ask_file(folder, ask.session))
|
||||
return now is not None and now.id == ask.id and now.talk is not None
|
||||
return now is not None and now.id == ask.id and isinstance(now.state, Asking)
|
||||
|
||||
|
||||
def no_longer_waits(ask: Ask) -> str:
|
||||
@@ -134,43 +125,24 @@ def sent(folder: pathlib.Path, ask: Ask, text: str) -> str:
|
||||
return f"Sent to {ask.root}."
|
||||
|
||||
|
||||
def talked(window: curses.window, paths: Paths, ask: Ask) -> str:
|
||||
"""Opens the worker's claude session on this terminal once the loop has stopped reading its own, and says what came of it."""
|
||||
def opened(window: curses.window, paths: Paths, ask: Ask, effects: Effects) -> str:
|
||||
"""Opens the worker's claude session on this terminal once the loop has made way for it, and says what came of it."""
|
||||
folder = paths.waiting
|
||||
if not still_waits(folder, ask) or ask.talk is None:
|
||||
if not (isinstance(ask.state, Asking) and still_waits(folder, ask)):
|
||||
return no_longer_waits(ask)
|
||||
if unsent := send(folder, ask, Talking(os.getpid())):
|
||||
return f"Could not open claude for {ask.root}: {unsent.reason}"
|
||||
if not made_way(folder, ask):
|
||||
return f"{ask.root} did not make way for claude within {MAKE_WAY_SECONDS} s; it was answered on its terminal, or stopped."
|
||||
claimed = claim_talk(folder, ask, effects.pid(), effects.now, effects.sleep)
|
||||
if isinstance(claimed, Failure):
|
||||
return f"Could not open claude for {ask.root}: {claimed.reason}"
|
||||
if not claimed:
|
||||
return f"{ask.root} did not make way for claude within {MAKE_WAY_SECONDS:g} s; it was answered on its terminal, or stopped."
|
||||
curses.def_prog_mode()
|
||||
curses.endwin()
|
||||
# As loop-claude's hand_over does before it opens claude on its own terminal.
|
||||
trust(paths.claude_json, worktrees(ask.talk.cwd, system_effects()), lambda text: print(f"loop-inbox: {text}", file=sys.stderr))
|
||||
# Ctrl-C belongs to claude while it runs, not to loop-inbox.
|
||||
interrupt = signal.signal(signal.SIGINT, signal.SIG_IGN)
|
||||
try:
|
||||
subprocess.run(["claude", *ask.talk.args], cwd=ask.talk.cwd, env={**os.environ, **ask.talk.env})
|
||||
said = f"claude for {ask.root} has ended; the loop goes on."
|
||||
except OSError as failure:
|
||||
said = f"Could not start claude for {ask.root}: {failure.strerror}"
|
||||
unstarted = hand_over(ask.state.talk, paths, effects)
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, interrupt)
|
||||
unsent = send(folder, ask, Talked())
|
||||
unsent = send(folder, ask, ClaudeEnded())
|
||||
curses.reset_prog_mode()
|
||||
window.refresh()
|
||||
# Without Talked, the loop goes on only once loop-inbox quits.
|
||||
said = f"Could not start claude for {ask.root}: {unstarted.reason}" if unstarted else f"claude for {ask.root} has ended; the loop goes on."
|
||||
# Without ClaudeEnded, the loop goes on only once loop-inbox quits.
|
||||
return f"{said} Could not tell the loop: {unsent.reason}; quit loop-inbox to let it go on." if unsent else said
|
||||
|
||||
|
||||
def made_way(folder: pathlib.Path, ask: Ask) -> bool:
|
||||
"""Whether the loop shows, within MAKE_WAY_SECONDS, that it has stopped reading its terminal for this loop-inbox."""
|
||||
deadline = time.monotonic() + MAKE_WAY_SECONDS
|
||||
while time.monotonic() < deadline:
|
||||
now = read_ask(ask_file(folder, ask.session))
|
||||
if now is None or now.id != ask.id:
|
||||
return False
|
||||
if now.talking == os.getpid():
|
||||
return True
|
||||
time.sleep(0.1)
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user