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
22 changes: 16 additions & 6 deletions pm-stack/opencode-claimant/claimant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 23 additions & 6 deletions pm-stack/opencode-claimant/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
55 changes: 55 additions & 0 deletions pm-stack/opencode-claimant/test_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading