mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 18:07:29 +01:00
One row per command attempt, not two aggregates written only on success. command_uses holds last-use only and daily_uses counts successes; both are rate-limiter state and both are written by store.record_use, which the dispatcher calls only when a handler returns cleanly. A gate refusal and a crash returned before it, so a command that failed every time looked exactly like one nobody ran, and nothing knew which command a counted message was. command_events now takes every attempt with its outcome, its duration and the item it served; upstream_calls takes every outbound request made while serving one. The two writes stay separate on purpose -- recording a refusal as a use is what slid the cooldown window forward every time somebody was told to wait -- and test_dispatcher_outcomes asserts last_used does not move. Backfilled from journald joined against message_events, with daily_uses filling the residual so the two sources cannot double-count. Two things were wrong on the first attempt and both were found by measuring rather than by reasoning: the window has to be symmetric, because sent_at_ms comes from the sender's phone and the journal stamp from this host (194 of 299 lines landed *before* their own message, by up to 3.2 s); and matching has to be a global closest-pair assignment, because per-row greedy cannot separate two commands seconds apart. Together those took the dropped rows from 137 to zero, and recovered 418 invocations back to 29 August. Also: !analityka in the group, twelve Polish lines, naming at most three people and only ones whose display name has been seen; an Analytics tab with Nivo charts; and continuous latency measurement, which answers the open question in DOCS-latency.md -- a text send is 235 ms and an image send about 2.2 s, so the upload really is where the time goes. EPHEMERAL_TABLES was doing two unrelated jobs and is now two tuples: what a schema bump rebuilds, and what leaving a group clears. daily_uses left the first (it is a backfill source now) and stayed in the second, and _drop_reshaped took over the repair a bump used to do for it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4QTp3h5Q3MvfrwGV8xqPf
91 lines
3.5 KiB
Python
91 lines
3.5 KiB
Python
# 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<at>\S+)\s.*?running command (?P<command>\S+) for (?P<who>.+)$")
|
|
# 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<at>\S+)\s.*?cooldown refused (?P<command>\S+) for (?P<who>.+?): 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[<pid>]`` 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
|