From cd7fcd53c8b51e207eacc83d7e77dcf5997e000c Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 15:12:32 -0600 Subject: [PATCH] dispatch_batch: normalize the count==1 single-claim response (#8217) Live acceptance found it: --count 1 IS the single claim, and the client answers with the bare row (or {task: null} when nothing is claimable), not the batch {tasks: [...]} body. claim_batch now normalizes both shapes. Regression test covers the bare-row response. --- pm-stack/opencode-claimant/dispatch_batch.py | 14 +++++++--- .../opencode-claimant/test_dispatch_batch.py | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/pm-stack/opencode-claimant/dispatch_batch.py b/pm-stack/opencode-claimant/dispatch_batch.py index d1b4206644..2ce53e2de2 100644 --- a/pm-stack/opencode-claimant/dispatch_batch.py +++ b/pm-stack/opencode-claimant/dispatch_batch.py @@ -191,9 +191,17 @@ def claim_batch(queue_name, worker, lane, count, family, if data is None: return [], f"batch claim unparseable (rc={rc}): {err.strip()[:200]}" tasks = data.get("tasks") - if not isinstance(tasks, list): - return [], f"batch claim returned no tasks array: {str(data)[:200]}" - return tasks, None + if isinstance(tasks, list): + return tasks, None + # count == 1 IS the single claim (#8217: count absent or 1 is + # the ordinary claim), and the client answers a single claim + # with the bare row — or {"task": null} when nothing is + # claimable. Normalize both to the batch shape. + if isinstance(data.get("id"), int): + return [data], None + if data.get("task") is None and "task" in data: + return [], None + return [], f"batch claim returned no tasks array: {str(data)[:200]}" def release_task(task_id, worker, reason): diff --git a/pm-stack/opencode-claimant/test_dispatch_batch.py b/pm-stack/opencode-claimant/test_dispatch_batch.py index f8867c380e..d785cce338 100644 --- a/pm-stack/opencode-claimant/test_dispatch_batch.py +++ b/pm-stack/opencode-claimant/test_dispatch_batch.py @@ -78,6 +78,17 @@ def verbs(self, verb: str) -> list[tuple]: return [c for c in self.calls if c[0] == verb] +class BareRowQueue(FakeQueue): + """The client's count==1 shape: a claim answers with the bare + row (the single claim), not the batch {"tasks": [...]} body.""" + + def __call__(self, *args, timeout=120): + self.calls.append(args) + if args[0] == "claim": + return 0, json.dumps(self.claim_rows[0]), "" + return 0, "{}", "" + + class DispatchBatchTests(unittest.TestCase): def setUp(self): self.worktree_calls: list[tuple] = [] @@ -275,6 +286,22 @@ def test_claude_item_without_bind_is_prepared_unbound(self): self.assertIn("bind_command", item) self.assertEqual(fake_q.verbs("bind"), []) + def test_single_item_batch_accepts_bare_row_response(self): + # Regression (live acceptance, #8217): --count 1 is the + # single claim, whose response is the bare row; dispatch + # must normalize it instead of failing the claim phase. + fake_q = BareRowQueue([row(101, 555)]) + db.queue = fake_q + out = io.StringIO() + with contextlib.redirect_stdout(out): + rc = db.main(["dispatch_batch.py", + *self.base_argv("--to", "opencode", + "--session-id", "ses_lane", + "--count", "1", "--dry-run")]) + self.assertEqual(rc, 0) + summary = json.loads(out.getvalue().strip().splitlines()[-1]) + self.assertEqual(summary["tasks"], [101]) + # -- validation --------------------------------------------------- def test_count_over_cap_refused_before_claiming(self):