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:
2026-10-28 23:57:33 +01:00
parent a7fe878631
commit 3409ab3d07
17 changed files with 374 additions and 258 deletions
+36 -6
View File
@@ -1,17 +1,18 @@
import dataclasses
import os
import select
import signal
import subprocess
import sys
import termios
import time
import typing
import uuid
from loop_claude.waiting import Answer, POLL_SECONDS
import git_repo
from machine_load.memory import Memory, read_memory
# Seconds between calls input_or makes while nothing is typed.
POLL_SECONDS = 0.5
# 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
@@ -26,6 +27,8 @@ class Streaming(typing.Protocol):
@dataclasses.dataclass(frozen=True)
class Effects:
# Whether the process with this pid still runs.
alive: typing.Callable[[int], bool]
claude: typing.Callable[[list[str], str, str], subprocess.CompletedProcess[str]]
claude_on_terminal: typing.Callable[[list[str], str, dict[str, str]], int]
# Drops what was typed on the terminal and not yet read.
@@ -33,14 +36,18 @@ class Effects:
execv: typing.Callable[[str, list[str]], None]
git: git_repo.Git
input: typing.Callable[[str], str]
# Prints a prompt, then returns the line typed at the terminal, or the first value other than None that the callable returns, called every POLL_SECONDS. Raises EOFError where the input ends.
input_or: typing.Callable[[str, typing.Callable[[], typing.Any]], typing.Any]
# Prints a prompt, then returns the line typed at the terminal, or the first answer from loop-inbox that the callable returns, called every POLL_SECONDS. Raises EOFError where the input ends.
input_or: typing.Callable[[str, typing.Callable[[], Answer | None]], str | Answer]
# A date and time in the machine's zone, as `2026-10-28 11:46:01`. lilleman chose this spelling and local time over UTC on 2026-10-28, since the human reads these on their own clock.
local_time: typing.Callable[[float], str]
# The machine's memory; None where it cannot be read.
memory: typing.Callable[[], Memory | None]
# A new random id.
new_id: typing.Callable[[], str]
now: typing.Callable[[], float]
on_terminal: typing.Callable[[], bool]
# This process's pid.
pid: typing.Callable[[], int]
# Runs a Python script in a directory, as run_python does.
python: typing.Callable[[list[str], str], subprocess.CompletedProcess[str]]
sleep: typing.Callable[[float], None]
@@ -61,8 +68,9 @@ class StreamingClaude:
def system_effects() -> Effects:
return Effects(
alive=alive,
claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=stdin, text=True),
claude_on_terminal=lambda args, cwd, env: subprocess.run(["claude", *args], cwd=cwd, env={**os.environ, **env}).returncode,
claude_on_terminal=claude_on_terminal,
discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH),
execv=os.execv,
git=git_repo.git,
@@ -70,14 +78,36 @@ def system_effects() -> Effects:
input_or=input_or,
local_time=lambda seconds: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(seconds)),
memory=read_memory,
new_id=lambda: str(uuid.uuid4()),
now=time.time,
on_terminal=lambda: sys.stdin.isatty() and sys.stdout.isatty(),
pid=os.getpid,
python=lambda args, cwd: run_python(args, cwd, PYTHON_TIMEOUT),
sleep=time.sleep,
stream_claude=StreamingClaude,
)
def claude_on_terminal(args: list[str], cwd: str, env: dict[str, str]) -> int:
"""Runs claude on the terminal in cwd, with env added to its environment, and returns its exit code. While claude runs, Ctrl-C belongs to it: this process shares its terminal, and ignores the key."""
# A Python handler, unlike SIG_IGN, is reset to the default as claude starts, so Ctrl-C still reaches claude.
previous = signal.signal(signal.SIGINT, lambda number, frame: None)
try:
return subprocess.run(["claude", *args], cwd=cwd, env={**os.environ, **env}).returncode
finally:
signal.signal(signal.SIGINT, previous)
def alive(pid: int) -> bool:
try:
os.kill(pid, 0)
except ProcessLookupError:
return False
except PermissionError:
return True
return True
def run_python(args: list[str], cwd: str, timeout: float) -> subprocess.CompletedProcess[str]:
"""Runs a Python script in cwd. One that cannot start, or still runs after timeout seconds and is stopped, reads as exit 1, saying why on stderr."""
command = [sys.executable, *args]
@@ -89,7 +119,7 @@ def run_python(args: list[str], cwd: str, timeout: float) -> subprocess.Complete
return subprocess.CompletedProcess(command, 1, "", f"stopped after {timeout} s")
def input_or[T](prompt: str, other: typing.Callable[[], T | None]) -> str | T:
def input_or(prompt: str, other: typing.Callable[[], Answer | None]) -> str | Answer:
# On stderr, where input prints its prompt on a terminal.
print(prompt, end="", file=sys.stderr, flush=True)
while True: