Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ it. There is no third answer and no per-parameter judgement call.

`since` is advisory by the same rule as `limit`: it shapes the window a read returns, so a
value that is not a non-negative integer is read as no cursor rather than refused, and the
schema says so in prose instead of publishing a `minimum` nothing enforces.
schema says so in prose instead of publishing a `minimum` nothing enforces. A cursor past the
room's head is clamped the same way: `last_seq` comes back as the real head (for a reaped
room, the floor it resumes from) rather than as an echo of whatever the caller sent.

**Why the line falls there.** Clamping an advisory parameter changes *how much comes back*;
the caller can see the answer it got and read `count` instead of assuming one. Clamping a
Expand Down
6 changes: 5 additions & 1 deletion src/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,11 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"Return only messages with a greater seq. Advisory: "
"anything that is not a non-negative integer — a negative "
"number, a decimal, a word — is read as no cursor at all, "
"and the reply is the newest messages."
"and the reply is the newest messages. A cursor past the "
"room's newest seq is clamped to it: an empty reply's "
"`last_seq` is the room's real head, so the next poll "
"resumes there instead of waiting on a seq that will not "
"come."
),
},
{
Expand Down
10 changes: 9 additions & 1 deletion src/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,17 @@ def read_messages(
# advancing past records nobody can read any more, or an expired room would reuse seqs.
cutoff = _cutoff(room)
out: list[dict] = []
# The room's head, where a cursor past it is clamped (#565): echoing it back printed a
# `next:` that polls a dead cursor forever, and let a caller put a number of any width
# into every JSON reply. The newest record on disk, expired or not, for the same reason
# `last_seq` does not filter.
head_seq = 0
with suppress(FileNotFoundError), path.open("rb") as f:
for raw in reverse_lines(f):
rec = _parse(raw)
if rec is None:
continue
head_seq = head_seq or rec["seq"]
if since is not None and rec["seq"] <= since:
break
if cutoff is not None and _expired(rec, cutoff):
Expand All @@ -902,11 +908,13 @@ def read_messages(
if len(out) >= limit:
break
out.reverse()
if not head_seq and since: # no record on disk: a reaped room resumes from its floor (#139)
head_seq = _seq_field(root, room, "floor")
return {
"room": room,
"count": len(out),
"first_seq": out[0]["seq"] if out else None,
"last_seq": out[-1]["seq"] if out else (since or 0),
"last_seq": out[-1]["seq"] if out else min(since or 0, head_seq),
"generation": room_generation(root, room),
"messages": out,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/http/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def test_limit_is_clamped_to_the_response_budget(client):
def test_cursor_past_the_end_returns_an_empty_but_usable_view(client):
client.get("/r/lobby/say/bot/hi")
view = client.get("/r/lobby?since=999&format=json").json()
assert view["count"] == 0 and view["last_seq"] == 999 # cursor preserved, not reset to 0
assert view["count"] == 0 and view["last_seq"] == 1 # clamped to real head
assert "(no new messages)" in client.get("/r/lobby?since=999").text


Expand Down
5 changes: 4 additions & 1 deletion tests/test_store_stateful.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ def read(self, room: str, limit: int, since: int | None) -> None:
if since is not None:
assert all(s > since for s in seqs), "`since` returned something already seen"
assert view["first_seq"] == (seqs[0] if seqs else None)
assert view["last_seq"] == (seqs[-1] if seqs else (since or 0))
if seqs:
assert view["last_seq"] == seqs[-1]
else:
assert view["last_seq"] <= (since or 0), "last_seq must clamp to head, not echo since"
for message in view["messages"]:
assert (message["from"], message["text"]) == self.said[room][message["seq"]]

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1442,3 +1442,36 @@ def test_service_stats_measures_room_bytes_until_a_reap_settles_them(tmp_path):
store.append(tmp_path, "openroom", "nick", "hi")
assert store.room_bytes_used(tmp_path) == 0 # nothing reaped yet
assert store.service_stats(tmp_path)["bytes"]["rooms"] == store._count_rooms(tmp_path)[1] > 0


def test_a_cursor_past_the_room_head_clamps_so_text_polling_can_progress(tmp_path):
"""A future cursor must settle on the room's actual head, or the text lane keeps
printing a dead next: URL forever (#565)."""
import store

store.append(tmp_path, "cursor", "bot", "one")

view = store.read_messages(tmp_path, "cursor", since=999)

assert view["count"] == 0
assert view["last_seq"] == 1


def test_a_cursor_on_a_reaped_room_clamps_to_its_floor_not_to_zero(tmp_path):
"""A reaped room has no file, but its head is not 0: the next write continues from the
floor the reaper left (#139). Clamping to the newest line on disk would rewind a caller
who is exactly caught up to `last_seq: 0` — the rewind #287 is about — and hand the text
lane a `next:` that no longer matches the cursor it was given."""
import store

for i in range(6):
store.append(tmp_path, "d-talk", "alice", f"msg {i}")
p = store.room_path(tmp_path, "d-talk")
_age(p, store.IDLE_SECONDS + 60)
(tmp_path / ".reaped").unlink(missing_ok=True)
store._reap(tmp_path)
assert not p.exists(), "premise: the room was reaped"

assert store.read_messages(tmp_path, "d-talk", since=6)["last_seq"] == 6, "caught up: kept"
assert store.read_messages(tmp_path, "d-talk", since=999)["last_seq"] == 6, "past it: clamped"
assert store.read_messages(tmp_path, "d-talk")["last_seq"] == 0, "no cursor: as before"
Loading