Answer stability round 1: 0 🔴, 4 🟡 (0 misread); type each backend's config, pass the notifier table in place of an http override, type the notifier setting, list notifier/ among the shared packages
CI / tests (pull_request) Successful in 2m36s
CI / tests (pull_request) Successful in 2m36s
This commit is contained in:
@@ -182,7 +182,7 @@ Install puts only `loop-claude` on `PATH`; call the others by their path, such a
|
||||
- `todo-item` reads and changes a project's plan, whatever holds it: `list` prints the items in the plan's order and `show <ID>` one item's row and detail; `add …` files an item under the next free ID, `edit <ID> …` sets an item's cells or detail, and `move <ID> --release <version>` sets an item's release; `finish <ID>` removes a finished item and adds the bullet passed with `--changelog` to `CHANGELOG.md`. Each change ranks a markdown plan. `todo-item --help` states the flags.
|
||||
- `wait-for <file> [<pattern>] [--timeout <seconds>]` waits until a file a background job writes holds a line matching the pattern, and prints it.
|
||||
|
||||
`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `branch_chunks/`, `claude_code/`, `command_lines/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `mattermost_dm/`, `model_routing/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool.
|
||||
`bin/` is the import root: each launcher puts it on `sys.path`, so a module imports another by its path under `bin/`, such as `from claude_code.entries import parse_entry`. Each tool's package holds its tests. Beside the packages, `bin/`'s root holds only the launchers, the tools of one file with their tests, `test_env.py`, `failure.py` and `setup.cfg`. `failure.py`'s `Failure`, which modules across `bin/` return for a failed step, belongs to no single package. `branch_chunks/`, `claude_code/`, `command_lines/`, `git_cli/`, `loop_effects/`, `loop_state/`, `machine_load/`, `model_routing/`, `notifier/`, `plan_store/`, `record_store/`, `release_plan/`, `review_rules/`, `rung_replay/`, `text_files/` and `transcript_archive/` serve more than one tool.
|
||||
|
||||
- `adf_codec/` converts markdown to ADF, the rich text of Jira Cloud and Confluence, and back, in a throwaway container running a pinned `@larvit/adf-codec`. It builds the container's image inside a heavy slot.
|
||||
- `branch_chunks/` cuts a default branch into chunks and finds, for each, the items it shipped and the later chunks that fixed its code, for `score_outcomes` and `routing_figures`. It imports `reading_cost`'s record path.
|
||||
|
||||
@@ -12,10 +12,11 @@ import termios
|
||||
import time
|
||||
import typing
|
||||
|
||||
from notifier.notify import Backend, BACKENDS, notify
|
||||
from failure import Failure
|
||||
from git_cli import git_repo
|
||||
from machine_load.memory import Memory, read_memory
|
||||
from notifier.notify import notify
|
||||
from project_config.settings import Notifier
|
||||
|
||||
# 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
|
||||
@@ -64,12 +65,13 @@ class StreamingClaude:
|
||||
return self.proc.wait()
|
||||
|
||||
|
||||
def system_effects() -> Effects:
|
||||
def system_effects(backends: typing.Mapping[Notifier, Backend[typing.Any]] = BACKENDS) -> Effects:
|
||||
"""The effects on this machine; `backends` are the notifiers loop-claude's DM can go through, where a test swaps in one that sends nowhere."""
|
||||
return Effects(
|
||||
claude=lambda args, cwd, stdin: subprocess.run(["claude", *args], capture_output=True, cwd=cwd, input=stdin, text=True),
|
||||
claude_on_terminal=claude_on_terminal,
|
||||
discard_typed=lambda: termios.tcflush(sys.stdin, termios.TCIFLUSH),
|
||||
dm=lambda text: notify(text, None, pathlib.Path.home()),
|
||||
dm=lambda text: notify(text, None, pathlib.Path.home(), backends),
|
||||
execv=os.execv,
|
||||
git=git_repo.git,
|
||||
input=input,
|
||||
|
||||
+19
-16
@@ -7,7 +7,7 @@ from failure import Failure
|
||||
from rest_client.json_request import Http
|
||||
from project_config.main import located
|
||||
import mattermost_dm.dm
|
||||
from project_config.settings import NOTIFIER, NOTIFIERS, ProjectConfig
|
||||
from project_config.settings import Notifier, NOTIFIER, NOTIFIERS, ProjectConfig
|
||||
import slack_dm.dm
|
||||
|
||||
# Sends one message, with the credentials already read.
|
||||
@@ -15,44 +15,47 @@ Sender = typing.Callable[[str], Failure | None]
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Backend:
|
||||
"""What one notifier needs: `read_config` reads its credentials from an env file, and returns whatever `send` takes as its config; `send` posts a message with them through `http`; `keys` are the env file's keys."""
|
||||
read_config: typing.Callable[[pathlib.Path], typing.Any]
|
||||
send: typing.Callable[[typing.Any, str, Http], Failure | None]
|
||||
class Backend[C]:
|
||||
"""What one notifier needs: `read_config` reads its credentials from an env file, and returns what `send` takes as its config; `send` posts a message with them through `http`; `keys` are the env file's keys."""
|
||||
read_config: typing.Callable[[pathlib.Path], C | Failure]
|
||||
send: typing.Callable[[C, str, Http], Failure | None]
|
||||
http: Http
|
||||
keys: tuple[str, ...]
|
||||
|
||||
|
||||
BACKENDS = {
|
||||
# The one place each notifier is wired to the network: a caller that tests a send passes a table with its own `http`.
|
||||
BACKENDS: dict[Notifier, Backend[typing.Any]] = {
|
||||
"mattermost": Backend(mattermost_dm.dm.read_config, mattermost_dm.dm.send, mattermost_dm.dm.http_json, mattermost_dm.dm.KEYS),
|
||||
"slack": Backend(slack_dm.dm.read_config, slack_dm.dm.send, slack_dm.dm.http_json, slack_dm.dm.KEYS),
|
||||
}
|
||||
assert tuple(BACKENDS) == NOTIFIERS
|
||||
|
||||
|
||||
def sender_of(project: ProjectConfig, http: Http | None = None) -> Sender | Failure:
|
||||
"""A sender through the project's notifier, with the credentials in its env file read; http replaces the backend's own. A Failure where the project names no notifier or its env file cannot be used."""
|
||||
if project.notifier is None or project.env_file is None:
|
||||
return Failure(f"{project.folder / 'config.toml'} sets no `{NOTIFIER}`; set `notifier` to {' or '.join(NOTIFIERS)}, and `env-file` to the file that holds that service's keys ({'; '.join(f'{name}: {', '.join(backend.keys)}' for name, backend in BACKENDS.items())})")
|
||||
backend = BACKENDS[project.notifier]
|
||||
def sender_of(project: ProjectConfig, backends: typing.Mapping[Notifier, Backend[typing.Any]]) -> Sender | Failure:
|
||||
"""A sender through the project's notifier in backends, with the credentials in its env file read. A Failure where the project names no notifier or its env file cannot be used."""
|
||||
if project.notifier is None:
|
||||
return Failure(f"{project.folder / 'config.toml'} sets no `{NOTIFIER}`; set `notifier` to {' or '.join(NOTIFIERS)}, and `env-file` to the file that holds that service's keys ({'; '.join(f'{name}: {', '.join(backend.keys)}' for name, backend in backends.items())})")
|
||||
if project.env_file is None:
|
||||
return Failure(f"{project.folder / 'config.toml'} sets `{NOTIFIER}` but no `env-file`, the file that holds its credentials")
|
||||
backend = backends[project.notifier]
|
||||
config = backend.read_config(project.env_file)
|
||||
if isinstance(config, Failure):
|
||||
return config
|
||||
return lambda message: backend.send(config, message, http or backend.http)
|
||||
return lambda message: backend.send(config, message, backend.http)
|
||||
|
||||
|
||||
def notify(text: str, directory: str | None, home: pathlib.Path, http: Http | None = None) -> Failure | None:
|
||||
def notify(text: str, directory: str | None, home: pathlib.Path, backends: typing.Mapping[Notifier, Backend[typing.Any]]) -> Failure | None:
|
||||
"""Sends text through the notifier of the project whose checkout holds directory.
|
||||
|
||||
The DM is skipped where the config names no notifier, since the project hasn't asked for DMs. It is skipped too where no config can be read: loop-claude has stopped with that reason, and without a config there is no notifier to ask."""
|
||||
project = located(directory, home)
|
||||
if isinstance(project, Failure) or project.notifier is None:
|
||||
return None
|
||||
sender = sender_of(project, http)
|
||||
sender = sender_of(project, backends)
|
||||
return sender if isinstance(sender, Failure) else sender(text)
|
||||
|
||||
|
||||
def sender_in(directory: str | None, home: pathlib.Path) -> Sender | Failure:
|
||||
def sender_in(directory: str | None, home: pathlib.Path, backends: typing.Mapping[Notifier, Backend[typing.Any]]) -> Sender | Failure:
|
||||
"""sender_of the project whose checkout holds directory."""
|
||||
project = located(directory, home)
|
||||
return project if isinstance(project, Failure) else sender_of(project)
|
||||
return project if isinstance(project, Failure) else sender_of(project, backends)
|
||||
|
||||
@@ -5,16 +5,19 @@ import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import typing
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from project_config.test_project_config import configured
|
||||
from failure import Failure
|
||||
from rest_client.json_request import Http
|
||||
from test_env import isolated_env
|
||||
from project_config.main import located
|
||||
from notifier import main, notify
|
||||
from mattermost_dm.test_mattermost_dm import mattermost
|
||||
from project_config.settings import Notifier
|
||||
from slack_dm.test_slack_dm import Slack
|
||||
from loop_effects.effects import system_effects
|
||||
|
||||
@@ -26,6 +29,11 @@ SLACK_POST = ("POST", "https://slack.com/api/chat.postMessage", "xoxb-secret", {
|
||||
MISSING_MATTERMOST = "does not exist; it needs MATTERMOST_URL, MATTERMOST_TOKEN and MATTERMOST_RECIPIENT"
|
||||
|
||||
|
||||
def through(name: Notifier, http: Http) -> dict[Notifier, notify.Backend[typing.Any]]:
|
||||
"""The notifier table with the named backend sending through http."""
|
||||
return {**notify.BACKENDS, name: dataclasses.replace(notify.BACKENDS[name], http=http)}
|
||||
|
||||
|
||||
class Project(unittest.TestCase):
|
||||
"""A home with a git checkout that a config names; `folder` is the config's folder."""
|
||||
|
||||
@@ -52,26 +60,26 @@ class Notify(Project):
|
||||
def test_sends_through_mattermost_where_the_config_names_it(self):
|
||||
self.name_notifier("mattermost", MATTERMOST_KEYS)
|
||||
server = mattermost()
|
||||
self.assertIsNone(notify.notify("Which one?", str(self.checkout), self.home, server))
|
||||
self.assertIsNone(notify.notify("Which one?", str(self.checkout), self.home, through("mattermost", server)))
|
||||
self.assertEqual(server.requests[-1], MATTERMOST_POST)
|
||||
|
||||
def test_sends_through_slack_where_the_config_names_it(self):
|
||||
self.name_notifier("slack", SLACK_KEYS)
|
||||
slack = Slack({"ok": True})
|
||||
self.assertIsNone(notify.notify("Which one?", str(self.checkout), self.home, slack))
|
||||
self.assertIsNone(notify.notify("Which one?", str(self.checkout), self.home, through("slack", slack)))
|
||||
self.assertEqual(slack.requests, [SLACK_POST])
|
||||
|
||||
def test_loop_claudes_dm_goes_through_the_notifier_for_the_project_holding_the_current_directory(self):
|
||||
self.name_notifier("slack", SLACK_KEYS)
|
||||
slack = Slack({"ok": True})
|
||||
with unittest.mock.patch.dict(notify.BACKENDS, {"slack": dataclasses.replace(notify.BACKENDS["slack"], http=slack)}), contextlib.chdir(self.checkout):
|
||||
self.assertIsNone(system_effects().dm("Which one?"))
|
||||
with contextlib.chdir(self.checkout):
|
||||
self.assertIsNone(system_effects(through("slack", slack)).dm("Which one?"))
|
||||
self.assertEqual(slack.requests, [SLACK_POST])
|
||||
|
||||
def test_skips_the_notice_for_a_project_that_names_no_notifier_even_where_its_env_file_holds_the_keys(self):
|
||||
self.name_notifier(None, MATTERMOST_KEYS)
|
||||
server = mattermost()
|
||||
self.assertIsNone(notify.notify("Which one?", str(self.checkout), self.home, server))
|
||||
self.assertIsNone(notify.notify("Which one?", str(self.checkout), self.home, through("mattermost", server)))
|
||||
self.assertEqual(server.requests, [])
|
||||
|
||||
def test_skips_the_notice_where_no_config_names_the_checkout_since_loop_claude_stops_on_that_itself(self):
|
||||
@@ -79,37 +87,44 @@ class Notify(Project):
|
||||
other.mkdir()
|
||||
subprocess.run(["git", "init", "--quiet", str(other)], check=True, env=isolated_env())
|
||||
server = mattermost()
|
||||
self.assertIsNone(notify.notify("Which one?", str(other), self.home, server))
|
||||
self.assertIsNone(notify.notify("Which one?", str(other), self.home, through("mattermost", server)))
|
||||
self.assertEqual(server.requests, [])
|
||||
|
||||
def test_fails_naming_the_file_or_the_key_where_the_env_file_is_missing_or_wrong(self):
|
||||
path = self.name_notifier("slack")
|
||||
self.assertEqual(notify.notify("hi", str(self.checkout), self.home, Slack({"ok": True})), Failure(f"{path} does not exist; it needs SLACK_TOKEN and SLACK_RECIPIENT"))
|
||||
self.assertEqual(notify.notify("hi", str(self.checkout), self.home, through("slack", Slack({"ok": True}))), Failure(f"{path} does not exist; it needs SLACK_TOKEN and SLACK_RECIPIENT"))
|
||||
path.write_text("SLACK_TOKEN=xoxb-secret\n")
|
||||
path.chmod(0o600)
|
||||
self.assertEqual(notify.notify("hi", str(self.checkout), self.home, Slack({"ok": True})), Failure(f"{path} lacks SLACK_RECIPIENT"))
|
||||
self.assertEqual(notify.notify("hi", str(self.checkout), self.home, through("slack", Slack({"ok": True}))), Failure(f"{path} lacks SLACK_RECIPIENT"))
|
||||
|
||||
def test_the_failure_for_an_unreachable_server_names_the_server_and_not_the_token(self):
|
||||
self.name_notifier("mattermost", MATTERMOST_KEYS.replace("https://chat.example", "https://127.0.0.1:1"))
|
||||
failed = notify.notify("Which one?", str(self.checkout), self.home)
|
||||
failed = notify.notify("Which one?", str(self.checkout), self.home, notify.BACKENDS)
|
||||
assert isinstance(failed, Failure), failed
|
||||
self.assertIn("cannot reach https://127.0.0.1:1", failed.reason)
|
||||
self.assertNotIn("secret-token", failed.reason)
|
||||
|
||||
def test_sender_in_fails_for_an_env_file_that_no_notifier_reads(self):
|
||||
self.name_notifier(None, MATTERMOST_KEYS)
|
||||
failed = notify.sender_in(str(self.checkout), self.home)
|
||||
failed = notify.sender_in(str(self.checkout), self.home, notify.BACKENDS)
|
||||
assert isinstance(failed, Failure), failed
|
||||
self.assertIn("sets no `notifier`", failed.reason)
|
||||
|
||||
def test_sender_of_names_the_missing_env_file_for_a_config_built_by_hand_with_a_notifier_and_none(self):
|
||||
self.name_notifier("slack", SLACK_KEYS)
|
||||
project = located(str(self.checkout), self.home)
|
||||
assert not isinstance(project, Failure), project
|
||||
failed = notify.sender_of(dataclasses.replace(project, env_file=None), notify.BACKENDS)
|
||||
self.assertEqual(failed, Failure(f"{self.folder / 'config.toml'} sets `notifier` but no `env-file`, the file that holds its credentials"))
|
||||
|
||||
def test_sender_in_fails_with_the_configs_reason_where_no_config_names_the_checkout(self):
|
||||
other = self.home / "code" / "other"
|
||||
other.mkdir()
|
||||
subprocess.run(["git", "init", "--quiet", str(other)], check=True, env=isolated_env())
|
||||
self.assertIsInstance(notify.sender_in(str(other), self.home), Failure)
|
||||
self.assertIsInstance(notify.sender_in(str(other), self.home, notify.BACKENDS), Failure)
|
||||
|
||||
def test_sender_in_names_the_settings_to_add_where_the_config_names_no_notifier(self):
|
||||
failed = notify.sender_in(str(self.checkout), self.home)
|
||||
failed = notify.sender_in(str(self.checkout), self.home, notify.BACKENDS)
|
||||
assert isinstance(failed, Failure), failed
|
||||
self.assertEqual(failed.reason, f"{self.folder / 'config.toml'} sets no `notifier`; set `notifier` to mattermost or slack, and `env-file` to the file that holds that service's keys (mattermost: MATTERMOST_URL, MATTERMOST_TOKEN, MATTERMOST_RECIPIENT; slack: SLACK_TOKEN, SLACK_RECIPIENT)")
|
||||
|
||||
|
||||
+1
-1
@@ -6,4 +6,4 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
|
||||
from notifier import main, notify
|
||||
|
||||
sys.exit(main.main(sys.argv[1:], sys.stdin, lambda: notify.sender_in(None, pathlib.Path.home()), sys.stderr))
|
||||
sys.exit(main.main(sys.argv[1:], sys.stdin, lambda: notify.sender_in(None, pathlib.Path.home(), notify.BACKENDS), sys.stderr))
|
||||
|
||||
@@ -7,6 +7,7 @@ import math
|
||||
import pathlib
|
||||
import re
|
||||
import tomllib
|
||||
import typing
|
||||
|
||||
from failure import Failure
|
||||
from project_config import templates
|
||||
@@ -21,7 +22,8 @@ SPELLING = '"checkout:<path>" for a path in the checkout, "folder:<path>" for on
|
||||
ENV_FILE = "env-file"
|
||||
# An optional setting: the service that sends the loop's DMs to the human, one of NOTIFIERS. A project that names none gets no DMs, so a client's env file of Jira credentials is never read for a chat token.
|
||||
NOTIFIER = "notifier"
|
||||
NOTIFIERS = ("mattermost", "slack")
|
||||
Notifier = typing.Literal["mattermost", "slack"]
|
||||
NOTIFIERS: tuple[Notifier, ...] = typing.get_args(Notifier)
|
||||
# An optional boolean setting: whether `chunk-pr merge` waits for a human's approval of the pull request. False where left out.
|
||||
WAIT_FOR_APPROVAL = "wait-for-approval"
|
||||
# An optional boolean setting: whether the loop may run the project's language runtime, package manager, tests and task runner on the host, for a project with no container setup. False where left out.
|
||||
@@ -83,7 +85,7 @@ class ProjectConfig:
|
||||
# The epic whose description holds the goals and audience, where the config's `goals-and-audience` names one.
|
||||
goals_epic: Epic | None = None
|
||||
# The config's `notifier`, or None where it names none.
|
||||
notifier: str | None = None
|
||||
notifier: Notifier | None = None
|
||||
|
||||
@property
|
||||
def plan_file(self) -> pathlib.Path | None:
|
||||
@@ -140,7 +142,7 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo
|
||||
epics = {name: epic_of(values.get(name)) for name in EPIC_SPELLINGS}
|
||||
env_file = anywhere(values.get(ENV_FILE), home)
|
||||
misplaced = ENV_FILE in values and (env_file is None or lies_in(env_file, top, main, file.parent))
|
||||
notifier = str(values[NOTIFIER]) if NOTIFIER in values else None
|
||||
notifier = next((name for name in NOTIFIERS if name == values.get(NOTIFIER)), None)
|
||||
waits = values.get(WAIT_FOR_APPROVAL, False)
|
||||
on_host = values.get(TOOLING_ON_HOST, False)
|
||||
floor = floor_of(values[READABILITY_FLOOR]) if READABILITY_FLOOR in values else DEFAULT_FLOOR
|
||||
|
||||
Reference in New Issue
Block a user