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

This commit is contained in:
2026-11-15 00:33:09 +01:00
parent eb692daee2
commit dd1a8fc203
6 changed files with 59 additions and 37 deletions
+19 -16
View File
@@ -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)