The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(groups): fence serve retry on the selected generation
unsupportedpastels09b1348ab8f2
diff --git a/tests/tui_gateway/test_groups_retry_coordinates.py b/tests/tui_gateway/test_groups_retry_coordinates.pyindex ab7284e86bf..4d6f763ff98 100644--- a/tests/tui_gateway/test_groups_retry_coordinates.py+++ b/tests/tui_gateway/test_groups_retry_coordinates.py@@ -2,10 +2,14 @@from __future__ import annotations+import sqlite3import threadingimport time+from contextlib import closingfrom types import SimpleNamespace+import pytest+import tui_gateway.server as srvfrom gateway import hosted_room_driver as driverfrom tui_gateway.hosted_room_service import HostedRoomService@@ -54,3 +58,27 @@ def test_retry_row_round_trips_through_groups_retry(tmp_path, monkeypatch):retried = srv.handle_request({"id": 2, "method": "groups.retry", "params": {"room_id": "room", **coordinates}})assert retried["result"]["task"]["status"] == "queued"+++def test_stale_retry_cannot_requeue_a_newer_attempt(tmp_path, monkeypatch):+ service, task_id = _deferred_room(tmp_path, monkeypatch)+ identity = driver.list_tasks(service.db_path, room_id="room", status="deferred")[0]["identity"]+ select = service._list_tasks++ def select_then_advance(room_id, statuses):+ candidates = list(select(room_id, statuses))+ # Another Retry runs generation 2, which also ends deferred, before this request resumes.+ lease = service.runtime._ensure_lease(service.bindings()[0])+ driver.requeue_deferred_task(service.db_path, identity, lease, expected_execution_generation=1,+ expected_cancel_generation=0, clock=service.runtime.clock)+ driver.start_task(service.db_path, identity, lease, expected_cancel_generation=0,+ clock=service.runtime.clock)+ with closing(sqlite3.connect(service.db_path)) as conn, conn:+ conn.execute("UPDATE hosted_room_driver_tasks SET status='deferred' WHERE task_id=?", (task_id,))+ return candidates++ monkeypatch.setattr(service, "_list_tasks", select_then_advance)+ with pytest.raises(driver.StaleTaskError):+ service.retry_room_task("room", task_id=task_id, member_id="worker", execution_generation=1)+ task = driver.get_task(service.db_path, identity)+ assert (task["status"], task["execution_generation"]) == ("deferred", 2)diff --git a/tui_gateway/hosted_room_driver.py b/tui_gateway/hosted_room_driver.pyindex 2d14741a5d8..1a6aa7806a2 100644--- a/tui_gateway/hosted_room_driver.py+++ b/tui_gateway/hosted_room_driver.py@@ -251,9 +251,14 @@ class HostedRoomRuntime:"cancel kept losing races with task transitions "f"(last observed state '{final['status']}')")- def retry_indeterminate(self, identity: state.TaskIdentity) -> dict[str, Any]:+ def retry_indeterminate(+ self, identity: state.TaskIdentity, *, execution_generation: int | None = None) -> dict[str, Any]:"""Explicitly retry one uncertain attempt under the current room lease."""task = state.get_task(self.db_path, identity)+ # The write below fences on this re-read generation, so pinning it to the+ # caller's selected attempt keeps a stale Retry off a newer attempt.+ if execution_generation not in (None, task["execution_generation"]):+ raise state.StaleTaskError("task attempt changed before retry")if task["status"] not in {"indeterminate", "deferred"}:raise state.InvalidTaskTransitionError(f"cannot retry task in state '{task['status']}'")binding = self._binding_for_room(identity.room_id)diff --git a/tui_gateway/hosted_room_service.py b/tui_gateway/hosted_room_service.pyindex ce853df33d6..ba3e5710d14 100644--- a/tui_gateway/hosted_room_service.py+++ b/tui_gateway/hosted_room_service.py@@ -544,7 +544,8 @@ class HostedRoomService:and execution_generation in (None, c["execution_generation"])), None)if task is None:raise driver.InvalidTaskTransitionError("no retryable room task matches task_id")- return self.runtime.retry_indeterminate(task["identity"])+ return self.runtime.retry_indeterminate(+ task["identity"], execution_generation=task["execution_generation"])def approve_room_task(self, room_id: str, *, member_id: str, task_id: str, execution_generation: int,