Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(scheduler): resolve a Run's runtime generation on the controller loop, not the door (#2664) (#2754)

Call White61f6714d2875
diff --git a/core/scheduled_tasks.py b/core/scheduled_tasks.py
index 021dee122..d1461bd67 100644
--- a/core/scheduled_tasks.py
+++ b/core/scheduled_tasks.py
@@ -6068,7 +6068,10 @@ class ScheduledTaskService:
reason=SKIP_REASON_SESSION_BUSY,
)
return False
- request = await self._run_runtime_sync(self._claim_pending_request, pending)
+ claimable, identity = self._claim_activation(pending)
+ if not claimable:
+ return True
+ request = await self._run_runtime_sync(self._commit_claim, pending, identity)
if request is None:
return True
if registration is not None and (
@@ -8320,25 +8323,48 @@ class ScheduledTaskService:
registry = getattr(self.controller, "runtime_activation", None)
return registry
- def _claim_pending_request(
+ def _claim_activation(
self,
pending: TaskExecutionRequest,
- ) -> TaskExecutionRequest | None:
+ ) -> tuple[bool, RuntimeActivationIdentity | None]:
+ """Whether ``pending`` can be claimed now, and the runtime generation it runs on.
+
+ The generation is read from the adapters' runtime state, which the controller
+ loop owns and mutates, so this runs on the loop (#2664). Only the claim's
+ write goes to the runtime-work door; ``commit_then_confirm`` there still
+ rejects a generation retired in between.
+ """
+
backend_resolution = self._claimed_request_backend_resolution(pending)
if not backend_resolution.authoritative:
- return None
+ return False, None
backend = backend_resolution.backend
service = getattr(self.controller, "agent_service", None)
is_backend_ready = getattr(service, "is_backend_ready", None)
if backend and callable(is_backend_ready) and not is_backend_ready(backend):
- return None
+ return False, None
resolution = self._activation_resolution_for_request(
pending,
backend=backend,
)
if resolution is not None and not resolution.authoritative:
+ return False, None
+ return True, resolution.identity if resolution is not None else None
+
+ def _claim_pending_request(
+ self,
+ pending: TaskExecutionRequest,
+ ) -> TaskExecutionRequest | None:
+ claimable, identity = self._claim_activation(pending)
+ if not claimable:
return None
- identity = resolution.identity if resolution is not None else None
+ return self._commit_claim(pending, identity)
+
+ def _commit_claim(
+ self,
+ pending: TaskExecutionRequest,
+ identity: RuntimeActivationIdentity | None,
+ ) -> TaskExecutionRequest | None:
registry = self._runtime_activation_registry()
if registry is None or identity is None:
return self.request_store.claim(pending.id)
@@ -8359,13 +8385,35 @@ class ScheduledTaskService:
self,
request: TaskExecutionRequest,
) -> tuple[TaskExecutionRequest, bool]:
+ # A claim that recorded no runtime generation resolves one again here, on the
+ # loop that owns the adapters' state (#2664); the step itself only writes.
+ resolution: RuntimeActivationResolution | None = None
+ resolved_backend = ""
+ if request.observed_activation_identity is None:
+ backend_resolution = self._claimed_request_backend_resolution(request)
+ resolved_backend = backend_resolution.backend
+ resolution = (
+ self._activation_resolution_for_request(
+ request,
+ backend=resolved_backend,
+ )
+ if backend_resolution.authoritative
+ else RuntimeActivationResolution(authoritative=False)
+ )
# One store step, on the door the claim itself runs through: a cancellation that
# arrives meanwhile is answered once the start (or the requeue) has landed.
- return await self._store_step(self._start_claimed_execution, request)
+ return await self._store_step(
+ self._start_claimed_execution,
+ request,
+ resolution,
+ resolved_backend,
+ )
def _start_claimed_execution(
self,
request: TaskExecutionRequest,
+ resolution: RuntimeActivationResolution | None = None,
+ resolved_backend: str = "",
) -> tuple[TaskExecutionRequest, bool]:
observed_identity = request.observed_activation_identity
request = self.request_store.refresh_claimed_request(request)
@@ -8379,11 +8427,12 @@ class ScheduledTaskService:
service = getattr(self.controller, "agent_service", None)
activation_identity = request.observed_activation_identity
if activation_identity is None:
- resolution = self._activation_resolution_for_request(
- request,
- backend=backend,
- )
- if resolution is not None and not resolution.authoritative:
+ if resolution is not None and (
+ not resolution.authoritative or resolved_backend != backend
+ ):
+ # Inconclusive, or resolved for a backend the refreshed Agent no longer
+ # names (a rename or archive since the claim): the next pass resolves
+ # again on the loop, with the refreshed row.
self.request_store.requeue(request.id)
return request, False
activation_identity = (
diff --git a/tests/test_scheduled_tasks.py b/tests/test_scheduled_tasks.py
index a55647812..f8ccf1a9b 100644
--- a/tests/test_scheduled_tasks.py
+++ b/tests/test_scheduled_tasks.py
@@ -4177,6 +4177,103 @@ def test_execute_claimed_request_skips_retired_runtime_generation(
assert row.get("pid") is None
+def test_claim_and_start_read_the_runtime_generation_on_the_loop(tmp_path: Path) -> None:
+ """#2664: an adapter's runtime state (its sessions and generation maps) is owned
+ and mutated by the controller loop. The claim and the execution start resolve
+ the Run's runtime generation from it, so they must read it there, not on the
+ runtime-work door, where a concurrent mutation raised "dictionary changed size
+ during iteration" and logged a traceback."""
+
+ request_store = TaskExecutionStore(tmp_path / "task_requests")
+ reads: list[int] = []
+
+ def identity_for_request(_backend, _request):
+ reads.append(threading.get_ident())
+ return None
+
+ controller = SimpleNamespace(
+ agent_service=SimpleNamespace(
+ is_backend_ready=Mock(return_value=True),
+ runtime_activation_identity_for_request=identity_for_request,
+ activation_registry=None,
+ )
+ )
+ service = ScheduledTaskService(
+ controller=controller,
+ store=ScheduledTaskStore(tmp_path / "scheduled_tasks.json"),
+ request_store=request_store,
+ )
+ pending = request_store.enqueue_agent_run(
+ message="run",
+ agent_backend="codex",
+ session_id="ses-runtime",
+ )
+ service._transport_ready_for_request = lambda _pending: True # type: ignore[method-assign]
+ service._spawn_execution = Mock() # type: ignore[method-assign]
+ service._execute_agent_run = AsyncMock( # type: ignore[assignment]
+ return_value=scheduled_tasks.AgentRunExecutionResult(error=None, complete_on_return=False)
+ )
+
+ async def run() -> int:
+ assert await service._process_pending_request(pending) is True
+ (claimed, _lock_key), _ = service._spawn_execution.call_args
+ await service._execute_claimed_request(claimed)
+ return threading.get_ident()
+
+ loop_thread = asyncio.run(run())
+
+ assert reads == [loop_thread, loop_thread]
+ service._execute_agent_run.assert_awaited_once()
+
+
+def test_start_requeues_when_the_refreshed_agent_names_another_backend(tmp_path: Path) -> None:
+ """The loop resolves the start's runtime generation from the claimed request
+ before the store step refreshes its Agent reference (#2664). If a rename since
+ the claim makes the refreshed Agent resolve to a backend the loop did not
+ resolve for, the step must not start without the generation fence: it requeues,
+ and the next pass resolves again with the refreshed row."""
+
+ request_store = TaskExecutionStore(tmp_path / "task_requests")
+ registry = RuntimeActivationRegistry()
+ identity = registry.attach("claude", "/repo")
+ agents = {"pm-renamed": SimpleNamespace(backend="claude")}
+ controller = SimpleNamespace(
+ vibe_agent_store=SimpleNamespace(get=agents.get, get_by_id=lambda _agent_id: None),
+ agent_service=SimpleNamespace(
+ agents={},
+ is_backend_ready=Mock(return_value=True),
+ runtime_activation_identity_for_request=Mock(return_value=identity),
+ activation_registry=registry,
+ ),
+ )
+ service = ScheduledTaskService(
+ controller=controller,
+ store=ScheduledTaskStore(tmp_path / "scheduled_tasks.json"),
+ request_store=request_store,
+ )
+ queued = request_store.enqueue_hook_send(
+ session_key="slack::channel::C123",
+ prompt="continue",
+ agent_name="pm",
+ )
+ claimed = request_store.claim(queued.id)
+ assert claimed is not None
+
+ def refresh_after_rename(item):
+ item.agent_name = "pm-renamed"
+ return item
+
+ request_store.refresh_claimed_request = refresh_after_rename # type: ignore[method-assign]
+ service._execute_request = AsyncMock() # type: ignore[method-assign]
+
+ asyncio.run(service._execute_claimed_request(claimed))
+
+ service._execute_request.assert_not_awaited()
+ row = request_store.get_run(claimed.id)
+ assert row["status"] == "queued"
+ assert row.get("pid") is None
+
+
def test_run_task_uses_tracked_execution_for_lease_loss(tmp_path: Path, monkeypatch) -> None:
request_store = TaskExecutionStore(tmp_path / "task_requests")
store = ScheduledTaskStore(tmp_path / "scheduled_tasks.json")
@@ -4286,7 +4383,7 @@ def test_request_capacity_is_reserved_before_claim(tmp_path: Path) -> None:
spawned: list[str] = []
async def _run_sync(operation, /, *args, **kwargs): # noqa: ANN001, ANN202
- if operation == service._claim_pending_request:
+ if operation == service._commit_claim:
claims.append(args[0].id)
claim_started.set()
await release_claim.wait()
@@ -4332,7 +4429,7 @@ def test_claimed_request_is_requeued_when_its_lane_generation_stops(
spawned: list[str] = []
async def _run_sync(operation, /, *args, **kwargs): # noqa: ANN001, ANN202
- if operation == service._claim_pending_request:
+ if operation == service._commit_claim:
claim_started.set()
await release_claim.wait()
return operation(*args, **kwargs)