From 8e2607a4fe44b3801987340e1791a17a838a199c Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:55:34 -0600 Subject: [PATCH] =?UTF-8?q?fix(pm-stack):=20claimant=20session=20fetch=20m?= =?UTF-8?q?ust=20prove=20completeness=20=E2=80=94=20escalating=20limit,=20?= =?UTF-8?q?fail=20closed=20(#4757)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GET /api/session truncates at 50 by default; poll.py treated that page as the whole list, understating live roots ~150x (true count 7,583 vs the 50-row page) and making bound sessions outside the page read as gone. The endpoint honours limit but not offset (an offset page repeats the first page — verified live), so fetch_sessions now reads with an escalating limit (1000, 10000, 100000) and accepts the first read that returns below its limit as complete by construction; a read at its limit every time, or any failed/non-list read, fails closed to None (cap_room null). claimant.py supervision_step gets the same list-verb correction as poll.py in #6609 (the deployed binary rejects list --worker) and reads rich rows (progress.session_id) with a flattened-row fallback. Five fetch-layer tests stub _server_get: short read, escalation, exact-limit escalation, failed read fail-closed, at-every-limit fail-closed. The 36 pinned tests are untouched and pass; live: fetch_sessions returns all 9,601 sessions in 0.4s, summarize_cap reports 7,583 live / 15 active / room 21. --- pm-stack/opencode-claimant/claimant.py | 22 +++++++--- pm-stack/opencode-claimant/poll.py | 29 ++++++++++--- pm-stack/opencode-claimant/test_poll.py | 55 +++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/pm-stack/opencode-claimant/claimant.py b/pm-stack/opencode-claimant/claimant.py index b8adfc860d..4738bc60f0 100755 --- a/pm-stack/opencode-claimant/claimant.py +++ b/pm-stack/opencode-claimant/claimant.py @@ -170,17 +170,27 @@ def supervision_step(mode: str) -> List[Dict]: try: # Use repo-queue to list tasks result = subprocess.run( - [str(REPO_QUEUE_CMD), "list", "--worker", "opencode-claimant", "--queue", queue], + [str(REPO_QUEUE_CMD), "list", "--queue", queue, "--status", "claimed"], capture_output=True, text=True, timeout=10 ) - + if result.returncode == 0: - tasks = json.loads(result.stdout) - for task in tasks: - if task.get("lane") == "opencode" and task.get("session_id"): - session_id = task["session_id"] + data = json.loads(result.stdout) + if isinstance(data, dict): + data = data.get("rows") or [] + for task in data: + if not isinstance(task, dict): + continue + # Rich rows carry lane/session in progress; a + # flattened row carries them at top level (#4757: + # the list verb rejects --worker, so this call + # silently fetched nothing before). + prog = task.get("progress") or {} + lane = prog.get("lane") or task.get("lane") + session_id = prog.get("session_id") or task.get("session_id") + if lane == "opencode" and session_id: # Check if session exists and is progressing try: diff --git a/pm-stack/opencode-claimant/poll.py b/pm-stack/opencode-claimant/poll.py index 31b3d3fbbc..55d9ef43d1 100755 --- a/pm-stack/opencode-claimant/poll.py +++ b/pm-stack/opencode-claimant/poll.py @@ -247,13 +247,30 @@ def _server_get(path): return data +SESSION_FETCH_LIMITS = (1000, 10000, 100000) + + def fetch_sessions(): - """The server's session list (GET /api/session), or None when - unreadable.""" - data = _server_get("/api/session") - if not isinstance(data, list): - return None - return data + """The server's COMPLETE session list (GET /api/session), or None + when completeness cannot be proven. + + The endpoint truncates at 50 entries by default (#4757, verified + live 2026-11-07). It honours `limit` — ?limit=1000 returns 1000 + rows — but NOT `offset`: an offset page repeats the first page, + so offset pagination cannot prove completeness here. Read with + an escalating limit instead: the first read whose count is + BELOW the requested limit is the whole list by construction. + If every read comes back exactly at its limit, the list may + still be truncated, so the fetch fails closed (None) rather + than let a partial list undercount the cap or make a bound + session read as gone.""" + for limit in SESSION_FETCH_LIMITS: + data = _server_get(f"/api/session?limit={limit}") + if not isinstance(data, list): + return None + if len(data) < limit: + return data + return None def fetch_active_ids(): diff --git a/pm-stack/opencode-claimant/test_poll.py b/pm-stack/opencode-claimant/test_poll.py index 75af510cdc..e27cc33d97 100755 --- a/pm-stack/opencode-claimant/test_poll.py +++ b/pm-stack/opencode-claimant/test_poll.py @@ -293,5 +293,60 @@ def test_unbound_or_direct_lane_is_not_stuck(self): ) +class TestFetchSessions(unittest.TestCase): + """Fetch-layer completeness (#4757): /api/session truncates at 50 + by default and honours limit but not offset, so fetch_sessions + escalates the limit until a read comes back below it — and fails + closed (None) whenever completeness cannot be proven. + _server_get is stubbed; no server is touched.""" + + def stub_server(self, total, fail_on_call=None): + calls = [] + + def fake_get(path): + calls.append(path) + if fail_on_call is not None and len(calls) == fail_on_call: + return None + limit = int(path.rsplit("limit=", 1)[1]) + return [{"id": f"ses_{i}"} for i in range(min(limit, total))] + + return fake_get, calls + + def run_fetch(self, total, **kw): + fake_get, calls = self.stub_server(total, **kw) + original = poll._server_get + poll._server_get = fake_get + try: + return poll.fetch_sessions(), calls + finally: + poll._server_get = original + + def test_short_single_read_is_complete(self): + sessions, calls = self.run_fetch(3) + self.assertEqual(len(sessions), 3) + self.assertEqual(len(calls), 1) + + def test_escalating_limit_finds_the_whole_list(self): + sessions, calls = self.run_fetch(2500) + self.assertEqual(len(sessions), 2500) + self.assertEqual(len(calls), 2) + + def test_count_exactly_at_a_limit_escalates(self): + sessions, calls = self.run_fetch(1000) + self.assertEqual(len(sessions), 1000) + self.assertEqual(len(calls), 2) + + def test_failed_read_fails_closed(self): + sessions, _ = self.run_fetch(2500, fail_on_call=1) + self.assertIsNone(sessions) + sessions, _ = self.run_fetch(2500, fail_on_call=2) + self.assertIsNone(sessions) + + def test_count_at_every_limit_fails_closed(self): + sessions, calls = self.run_fetch(10**9) + self.assertIsNone(sessions) + self.assertEqual(len(calls), len(poll.SESSION_FETCH_LIMITS)) + + if __name__ == "__main__": unittest.main()