From c5f7b616e9073e6db8e21a68b1c8a9b1a0917b82 Mon Sep 17 00:00:00 2001 From: Sertug17 <104278804+Sertug17@users.noreply.github.com> Date: Sun, 30 Aug 2026 21:07:15 +0300 Subject: [PATCH 1/4] fix(store): clamp since past head to the real head in read_messages Fixes #565. A ?since= cursor past the room's head was echoed back as last_seq, causing the text lane's next: line to print a dead cursor. Agents following that line poll forever. Now clamped to min(since, head_seq). Verified-against: 45921c3 --- src/store.py | 4 +++- tests/http/test_limits.py | 2 +- tests/unit/test_store.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/store.py b/src/store.py index fd499dc0e..280315ab1 100644 --- a/src/store.py +++ b/src/store.py @@ -889,12 +889,14 @@ def read_messages( # advancing past records nobody can read any more, or an expired room would reuse seqs. cutoff = _cutoff(room) out: list[dict] = [] + head_seq = 0 if path.exists(): with 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): @@ -907,7 +909,7 @@ def read_messages( "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 35fda56d7..c352e6433 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/unit/test_store.py b/tests/unit/test_store.py index 2c212b70f..a102e47b1 100644 --- a/tests/unit/test_store.py +++ b/tests/unit/test_store.py @@ -1258,6 +1258,7 @@ def test_a_room_holding_undecodable_bytes_is_counted_not_crashed_on(tmp_path): assert store._reapable(p, os.stat(p).st_mtime, stillborn_rule=True) is None + def test_compaction_retains_the_whole_byte_budget_at_every_record_size(tmp_path): """Retention is the byte budget, at every record size. @@ -1303,6 +1304,7 @@ def test_compaction_retains_the_whole_byte_budget_at_every_record_size(tmp_path) assert seqs[-1] == written, "the newest record must survive compaction" + def test_the_append_path_can_size_the_file_it_just_wrote(tmp_path): """The compaction check adds `size + len(line)` rather than stat()ing a file it has just written while holding the room lock. That is exact only because `size` is read *before* @@ -1337,3 +1339,16 @@ def test_the_append_path_can_size_the_file_it_just_wrote(tmp_path): texts = [m["text"] for m in store.read_messages(tmp_path, "torncalc")["messages"]] assert texts == ["first", "second"], "the healed record and the new one both survive" + + +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 From e0f7f6049551852811b88a6f917370a22a1e181f Mon Sep 17 00:00:00 2001 From: Sertug17 <104278804+Sertug17@users.noreply.github.com> Date: Mon, 7 Sep 2026 16:48:02 +0300 Subject: [PATCH 2/4] style: fix ruff formatting (PEP 8 blank lines) --- tests/unit/test_store.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/unit/test_store.py b/tests/unit/test_store.py index a102e47b1..92f805fb3 100644 --- a/tests/unit/test_store.py +++ b/tests/unit/test_store.py @@ -1258,7 +1258,6 @@ def test_a_room_holding_undecodable_bytes_is_counted_not_crashed_on(tmp_path): assert store._reapable(p, os.stat(p).st_mtime, stillborn_rule=True) is None - def test_compaction_retains_the_whole_byte_budget_at_every_record_size(tmp_path): """Retention is the byte budget, at every record size. @@ -1304,7 +1303,6 @@ def test_compaction_retains_the_whole_byte_budget_at_every_record_size(tmp_path) assert seqs[-1] == written, "the newest record must survive compaction" - def test_the_append_path_can_size_the_file_it_just_wrote(tmp_path): """The compaction check adds `size + len(line)` rather than stat()ing a file it has just written while holding the room lock. That is exact only because `size` is read *before* From d773a2e2c20810d6c72bc01a39b79f1daad09da6 Mon Sep 17 00:00:00 2001 From: Sertug17 <104278804+Sertug17@users.noreply.github.com> Date: Mon, 7 Sep 2026 16:54:19 +0300 Subject: [PATCH 3/4] test: update stateful test to match clamped last_seq contract --- tests/test_store_stateful.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_store_stateful.py b/tests/test_store_stateful.py index 77bae3a29..35a015e03 100644 --- a/tests/test_store_stateful.py +++ b/tests/test_store_stateful.py @@ -265,7 +265,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"]] From e9979d3780add72303effb25b5bf4a174d7427e1 Mon Sep 17 00:00:00 2001 From: Sergey Vidyuk Date: Thu, 24 Sep 2026 11:28:09 +0800 Subject: [PATCH 4/4] fix(store): clamp a cursor on a reaped room to its floor, not to 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The clamp took the room's head from the newest line on disk, so a room the reaper had removed had a head of 0: a caller whose cursor was exactly caught up got `last_seq: 0` back, a rewind (#287's symptom), and the text lane printed `next: ?since=0`. A reaped room's head is the floor the reaper left, the seq its next write continues from (#139). Read it only when nothing is on disk and a cursor was given, so an ordinary read pays nothing. Also documents the clamp where docs/design.md §3.5 says advisory parameters must: the `since` schema description, and §3.5's own paragraph on `since`. store.py: 1025 -> 1029 code lines with this branch (cap 1030). --- docs/design.md | 4 +++- src/manifest.py | 6 +++++- src/store.py | 6 ++++++ tests/unit/test_store.py | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) 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 5b5b5ed9b..41bc0cf85 100644 --- a/src/store.py +++ b/src/store.py @@ -889,6 +889,10 @@ 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): @@ -904,6 +908,8 @@ 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), diff --git a/tests/unit/test_store.py b/tests/unit/test_store.py index 3f9c4ac55..d22222eee 100644 --- a/tests/unit/test_store.py +++ b/tests/unit/test_store.py @@ -1456,3 +1456,22 @@ def test_a_cursor_past_the_room_head_clamps_so_text_polling_can_progress(tmp_pat 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"