signal-bot/signal_bot/changelog.py
Krzysztof kuhy Rudnicki a202317072 feat: answer the message that asked, not just the room
A "!cat" in a busy group produced a cat picture floating loose in the chat with
nothing tying it to whoever asked. Replies now quote the triggering message.

The container advertises this and always could: /v1/about reports
capabilities {"v2/send": ["quotes", "mentions"]}, and /v2/send takes
quote_timestamp, quote_author and quote_message. What was missing was on this
side -- parse_envelope discarded the envelope's timestamp, which is the id
Signal quotes a message by, so nothing downstream could have built a quote even
in principle. IncomingMessage now carries it, preferring the payload's own
value over the envelope's for the case where they ever disagree.

Quote is a value object rather than three more parameters threaded through
every send: the Transport protocol has five methods and two of them send.
CommandContext grows reply_image() so quoting lives in one place instead of
each handler reaching for ctx.transport itself, which is what animals.py did.

quote_of() returns None when the timestamp or author is missing. Answering the
room is a worse reply than a quoted one but a much better outcome than a
payload the container rejects.

The e2e gate learned to tell the two apart: Expectation.quotes demands a send
either does or does not point at a message, defaulting to None so the ten
existing scenarios stay indifferent. Without it the fake would have accepted an
unquoted reply as a pass.

Verified in the testbot group on a real Signal client before this bump, per
kuhy's rule that a bump is a publish rather than a checkpoint: he sent !cat and
confirmed the picture renders attached to his message as a reply.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d48t4qsQLBJnDwDeo2Nom
2026-08-29 23:01:01 +02:00

93 lines
3.2 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""The changelog the bot announces, read from DOCS-changelog.md.
That file is the single source: there is no second machine-readable copy to
drift out of sync with it. Only two shapes are recognised -- a `## <version> —
<pope>` heading and a `- ` bullet beneath one -- so prose around them is free.
"""
from __future__ import annotations
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Final
from signal_bot.versioning import ordinal_for
# Repo root, since the changelog is documentation rather than package data.
DEFAULT_PATH: Final = Path(__file__).resolve().parent.parent / "DOCS-changelog.md"
_HEADING: Final = re.compile(r"^##\s+(\d+\.\d+\.\d+)\s*[—-]\s*(.+?)\s*$")
_BULLET: Final = re.compile(r"^-\s+(.+?)\s*$")
# A wrapped bullet: indented, directly under one. Without this a bullet
# that runs past the line width is announced truncated mid-sentence.
_CONTINUATION: Final = re.compile(r"^\s+(\S.*?)\s*$")
@dataclass(frozen=True, slots=True)
class Release:
"""One released version and the lines describing it."""
version: str
pope: str
lines: tuple[str, ...]
@property
def ordinal(self) -> int:
"""The papal ordinal this release carries."""
return ordinal_for(self.version)
def parse(text: str) -> tuple[Release, ...]:
"""Return every release in the document, oldest first."""
releases: list[Release] = []
version = pope = ""
lines: list[str] = []
open_bullet = False
for raw in text.splitlines():
heading = _HEADING.match(raw)
if heading is not None:
if version:
releases.append(Release(version, pope, tuple(lines)))
version, pope, lines = heading.group(1), heading.group(2), []
open_bullet = False
continue
bullet = _BULLET.match(raw)
if bullet is not None and version:
lines.append(bullet.group(1))
open_bullet = True
continue
continuation = _CONTINUATION.match(raw)
if continuation is not None and open_bullet:
lines[-1] = f"{lines[-1]} {continuation.group(1)}"
continue
open_bullet = False
if version:
releases.append(Release(version, pope, tuple(lines)))
return tuple(sorted(releases, key=lambda release: release.ordinal))
def load(path: Path | None = None) -> tuple[Release, ...]:
"""Return every release, or nothing if the changelog is not on disk.
A missing changelog must not stop the bot: it is documentation that lives
outside the package, so an odd install layout should cost an announcement
rather than the whole process.
"""
source = DEFAULT_PATH if path is None else path
try:
text = source.read_text(encoding="utf-8")
except OSError:
return ()
return parse(text)
def since(releases: tuple[Release, ...], previous: str) -> tuple[Release, ...]:
"""Return the releases newer than ``previous``, oldest first."""
try:
floor = ordinal_for(previous)
except ValueError:
return ()
return tuple(release for release in releases if release.ordinal > floor)