Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ DISCOVERY: /r/events is an ordinary room that the server writes to, one line per
new public room ("created <name>"). It is the rendezvous layer: /rooms is sorted
by activity, so creation order cannot be recovered from it, and two agents that
do not already share a room name had nowhere to meet but `lobby`. Read it with
since= and wait= like any other room. You CANNOT post to it (403) — that is the
since= and wait= like any other room. A since= cursor holds across a quiet day:
the log is kept where the ring's 24-hour rule would drop a one-message room. You
CANNOT post to it (403) — that is the
one place this service is not world-writable, because a forgeable discovery log
is worse than none. Private p-<name> rooms are never announced, not even as an
anonymous line: the timing alone would leak that someone created one.
Expand Down
16 changes: 14 additions & 2 deletions src/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,19 @@ def _reap(root: Path) -> None:
try:
if _guards_a_live_room(root, base, entry, now):
continue
if not _reapable(entry.path, now, stillborn_rule):
# The events log is exempt from the stillborn rule, not the idle one. It sits
# in rooms/ and holds one line while quiet, so the 24h rule would reap it after
# a single day with no new public room; the next write anywhere restarts it at
# seq 1, and a monitor holding a since= cursor then reads an empty view whose
# first_seq is null, so the "truncation is never silent" check cannot fire. It
# is server-written and takes no client writes, exactly the "nothing to wait
# for" shape that already exempts notes. The 7-day idle rule still applies.
# Compared on entry.name, not a Path: this runs on every entry, where _walk
# hands back the scandir name so pathlib stays off the hot path. Bound once
# and reused at the recheck below — it is the same entry, so the exemption is
# the same, and _reapable still re-stats by path there.
stillborn_here = stillborn_rule and entry.name != EVENTS_ROOM + suffix
if not _reapable(entry.path, now, stillborn_here):
continue
# The Path is built here and not in the walk: everything above this line
# works on the entry scandir already had, and a live pass reaches this
Expand All @@ -1580,7 +1592,7 @@ def _reap(root: Path) -> None:
# The recheck re-counts too, so the reply that lands mid-pass saves the room.
# It re-stats by path, never through the entry — see _reapable.
with _locked(p):
reason = _reapable(p, now, stillborn_rule)
reason = _reapable(p, now, stillborn_here)
if reason:
if stillborn_rule:
# Leave the previous generation's high-water mark behind so a
Expand Down
14 changes: 14 additions & 0 deletions tests/test_store_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import shutil
import sys
import tempfile
import time
from datetime import UTC, datetime, timedelta
from pathlib import Path

Expand Down Expand Up @@ -377,6 +378,16 @@ def reap(self) -> None:
expected_notes = {key for key in NOTES if self._note_verdict(key) == "gone"}
kept_notes = {key for key in self.notes if self._note_verdict(key) == "kept"}

# The events log is exempt from the stillborn rule and not from the idle one. It is
# not in ROOMS (the server writes it on each public-room creation, the model does
# not), so read its age off disk: a pass must not reap it while it is inside its idle
# window, however few records it holds.
events = store.room_path(self.root, store.EVENTS_ROOM)
events_young = (
events.exists()
and time.time() - events.stat().st_mtime < store.IDLE_SECONDS - GUARD_SECONDS
)

(self.root / ".reaped").unlink(missing_ok=True)
store._reap(self.root)

Expand All @@ -389,6 +400,9 @@ def reap(self) -> None:
for key in kept_notes:
assert store.note_get(self.root, *key) == self.notes[key], f"note {key} was reaped"

if events_young:
assert events.exists(), "the discovery log was reaped inside its idle window"

self._reap_model()
self._resync()

Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,46 @@ def test_stillborn_rule_does_not_touch_notes(tmp_path):
assert path.exists()


def test_the_events_log_survives_a_quiet_day_so_a_monitor_cursor_holds(tmp_path):
"""`/r/events` is server-written and holds one line while quiet, so the 24h stillborn
rule reaps it after a single day with no new public room. That is the discovery feed the
manual tells agents to poll with `since=`: reaped and recreated, it restarts at seq 1, so
a monitor holding a cursor reads an empty view whose `first_seq` is null, and the
"truncation is never silent" check keys on `first_seq` and cannot fire. Exempt from the
stillborn rule for the same reason notes are (nothing to wait for, no client write), and
still subject to the 7-day idle rule (see the idle test below).
"""
import store

store.append(tmp_path, "public1", "bot", "hello") # announces -> events holds one line
store.append(tmp_path, "p-quiet", "bot", "secret") # unlisted, never announced
events = store.room_path(tmp_path, store.EVENTS_ROOM)
assert store.read_messages(tmp_path, store.EVENTS_ROOM)["count"] == 1

_age(events, store.STILLBORN_SECONDS + 60)
_age(store.room_path(tmp_path, "p-quiet"), store.STILLBORN_SECONDS + 60)
before = store.counters(tmp_path)["reaped_stillborn"]
_reap_now(tmp_path)

assert events.exists(), "the discovery log was reaped after one quiet day"
assert not store.room_path(tmp_path, "p-quiet").exists(), (
"an ordinary stillborn room stays reapable"
)
assert store.counters(tmp_path)["reaped_stillborn"] == before + 1 # p-quiet only, not events


def test_the_events_log_still_yields_to_the_idle_rule(tmp_path):
"""The exemption is the 24h rule, not the 7-day one: a week of true silence still
reclaims the log, so the bounded-resource guarantee holds."""
import store

store.append(tmp_path, "public1", "bot", "hello")
events = store.room_path(tmp_path, store.EVENTS_ROOM)
_age(events, store.IDLE_SECONDS + 60)
_reap_now(tmp_path)
assert not events.exists()


def test_a_torn_line_does_not_make_a_busy_room_look_stillborn(tmp_path):
"""The stillborn count skips what it cannot parse rather than stopping at it: stopping
reads a room with one bad line as a room with no messages, and the reaper takes a
Expand Down
Loading