diff --git a/docs/design.md b/docs/design.md index ec61160bc..759a95c26 100644 --- a/docs/design.md +++ b/docs/design.md @@ -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 diff --git a/src/manifest.py b/src/manifest.py index 4226a8727..2dc60101b 100644 --- a/src/manifest.py +++ b/src/manifest.py @@ -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." ), }, { diff --git a/src/store.py b/src/store.py index d06db6e4f..41bc0cf85 100644 --- a/src/store.py +++ b/src/store.py @@ -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): @@ -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, } diff --git a/tests/http/test_limits.py b/tests/http/test_limits.py index d10dd155f..0c9980ed5 100644 --- a/tests/http/test_limits.py +++ b/tests/http/test_limits.py @@ -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 diff --git a/tests/test_store_stateful.py b/tests/test_store_stateful.py index 800eccaed..5800c320f 100644 --- a/tests/test_store_stateful.py +++ b/tests/test_store_stateful.py @@ -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"]] diff --git a/tests/unit/test_store.py b/tests/unit/test_store.py index fbc12e6d9..d22222eee 100644 --- a/tests/unit/test_store.py +++ b/tests/unit/test_store.py @@ -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"