Answer principles round 1: 0 🔴, 3 🟡 (0 misread); return withdraw's failure as a warning, check every field read_ask reads, test loop-inbox's /talk end, move TALK to waiting.py
This commit is contained in:
@@ -24,6 +24,8 @@ from file_writes import make_private, replace_json
|
||||
POLL_SECONDS = 0.5
|
||||
# Seconds loop-inbox waits for a loop to make way for its claude: ten of the loop's looks, so a loop slow for a moment still makes way.
|
||||
MAKE_WAY_SECONDS = 10 * POLL_SECONDS
|
||||
# What the human types, on the loop's terminal or in loop-inbox, to open claude for the ask; interrupt.py says who picked it.
|
||||
TALK = "/talk"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -105,10 +107,14 @@ def publish(folder: pathlib.Path, ask: Ask) -> Failure | None:
|
||||
return replace_json(ask_file(folder, ask.session), dataclasses.asdict(ask) | {"state": tagged(ask.state)})
|
||||
|
||||
|
||||
def withdraw(folder: pathlib.Path, session: str) -> None:
|
||||
def withdraw(folder: pathlib.Path, session: str) -> Failure | None:
|
||||
"""Removes the session's ask and any answer to it."""
|
||||
for path in (ask_file(folder, session), answer_file(folder, session)):
|
||||
path.unlink(missing_ok=True)
|
||||
try:
|
||||
path.unlink(missing_ok=True)
|
||||
except OSError as failure:
|
||||
return Failure(f"cannot remove {path}: {failure.strerror}")
|
||||
return None
|
||||
|
||||
|
||||
def waiting(folder: pathlib.Path, alive: typing.Callable[[int], bool]) -> list[Ask]:
|
||||
@@ -122,18 +128,20 @@ def waiting(folder: pathlib.Path, alive: typing.Callable[[int], bool]) -> list[A
|
||||
|
||||
|
||||
def read_ask(path: pathlib.Path) -> Ask | None:
|
||||
"""The ask in path; None where a field has the wrong type, or names another session than path does."""
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
ask = Ask(**{**data, "state": ask_state(data["state"])})
|
||||
except (OSError, ValueError, TypeError, KeyError):
|
||||
return None
|
||||
texts = all(isinstance(text, str) for text in (ask.id, ask.session, ask.root, ask.question))
|
||||
# A pid of 0 or below would name a process group to alive.
|
||||
return ask if isinstance(ask.asked, int | float) and isinstance(ask.pid, int) and ask.pid > 0 else None
|
||||
return ask if texts and ask.session == path.stem and isinstance(ask.asked, int | float) and isinstance(ask.pid, int) and ask.pid > 0 else None
|
||||
|
||||
|
||||
def ask_state(data: object) -> AskState:
|
||||
match data:
|
||||
case {"kind": "Asking", "talk": {"args": list(args), "cwd": str(cwd), "env": dict(env)}}:
|
||||
case {"kind": "Asking", "talk": {"args": list(args), "cwd": str(cwd), "env": dict(env)}} if all(isinstance(text, str) for text in [*args, *env.values()]):
|
||||
return Asking(ClaudeCommand(args, cwd, env))
|
||||
case {"kind": "OpenOnTerminal"}:
|
||||
return OpenOnTerminal()
|
||||
|
||||
Reference in New Issue
Block a user