# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """Reading the bot's own journal back into the command history. Pure parsing, no I/O: the lines arrive as an iterable so that the joining logic and its tests never shell out, and so that a captured log file works exactly as well as a live ``journalctl``. The journal is the only record of what ran before ``command_events`` existed, and it is a poor one. It carries no group id and no uuid -- only a *display name*, because ``dispatcher.py``'s log line was written to be read by a human. That is why the join in ``backfill`` runs the other way round, over ``message_events``, which knows everything except which command it was. A display name read here is a lookup key and nothing else. It never reaches a row, a log line or a file: ``group_people`` turns it into a uuid inside the joiner and the string is dropped. ``DOCS-privacy.md`` names the journal as the one place display names live, and ``--vacuum-time=`` as how that is emptied -- so this parser must never be the thing that copies them out. """ from __future__ import annotations import datetime as dt import re from dataclasses import dataclass from typing import TYPE_CHECKING, Final if TYPE_CHECKING: from collections.abc import Iterable, Iterator MS_PER_SECOND: Final = 1000 # `journalctl -o short-iso` prefixes each line with an offset-aware timestamp # -- 2026-08-31T21:28:26+02:00 -- which is the only spelling in the file that # survives a DST boundary. The bot's own asctime, further along the same # line, is naive and would be an hour wrong for half the year. _RUNNING: Final = re.compile(r"^(?P\S+)\s.*?running command (?P\S+) for (?P.+)$") # The one gate that already logged its refusals. daily_limit and vote_gated # logged nothing at all, so those simply cannot be recovered. _REFUSED: Final = re.compile( r"^(?P\S+)\s.*?cooldown refused (?P\S+) for (?P.+?): told them" ) @dataclass(frozen=True, slots=True) class JournalLine: """One thing the bot said it did, with the name it said it for.""" at_ms: int command: str display_name: str refused: bool def _at_ms(stamp: str) -> int | None: """Parse the journal's own timestamp, or None if this is not one.""" try: moment = dt.datetime.fromisoformat(stamp) except ValueError: return None # An offset-naive stamp would silently be read as UTC and land the whole # import two hours out, which is exactly the class of error the hour # columns exist to avoid. Refuse it instead. if moment.tzinfo is None: return None return int(moment.timestamp() * MS_PER_SECOND) def parse(lines: Iterable[str]) -> Iterator[JournalLine]: """Yield one record per line the bot wrote about running a command. Everything else -- startup banners, tracebacks, envelope shapes, other units -- simply does not match and is skipped. The parser is deliberately permissive about what sits between the timestamp and the phrase, because the journal identifier is ``run.sh[]`` and the pid changes. """ for line in lines: for pattern, refused in ((_RUNNING, False), (_REFUSED, True)): found = pattern.match(line) if found is None: continue at_ms = _at_ms(found["at"]) if at_ms is not None: yield JournalLine( at_ms=at_ms, command=found["command"], display_name=found["who"].strip(), refused=refused, ) break