Files
roboco/tests/unit/runtime/test_resolve_wait_repark.py
T
Renn F 3441e37120 [sweep] strip Fxxx audit-ID tokens + trim bloated comments/docstrings + add behavior-change docs
Post-audit sweep over the 135 audit-fix commits since 19a474d3:

1. Stripped every # Fxxx: audit-ID token from comments AND every Fxxx token
   from docstring openings across 211 blocks / ~626 lines. The CEO flagged
   these twice: audit-issue IDs in code confuse future devs/agents. The
   descriptive text is preserved; only the Fxxx token is removed (and bloated
   narrative blocks trimmed to 1-3 lines keeping the one non-obvious invariant).
2. Trimmed bloated comments/docstrings to the concise standard (1-3 lines).
3. Added missing behavior-change docs for the audit-fix batch: prompts/roles
   (documenter, pr_reviewer, qa), user-facing docs (api auth, websockets,
   agent-gateway, megatask, merge-model, task-lifecycle, grok, resilience,
   conventions, panel, security, troubleshooting), and the RAG corpus (cell-pm,
   main-pm, pr-reviewer, qa roles; conventions; messaging-tools; escalation;
   megatask; task-claiming workflows).

Comment/docstring/prose ONLY — zero code-line edits (verified: the diff
contains no def/class/return/if/for/await/assignment/call lines). Gates green:
ruff format + ruff check clean, mypy clean on roboco/. The only pytest failures
are the pre-existing sync_branch tracing-decision gap (B1, 250be5c2) — not
sweep-caused and tracked separately.
2026-06-29 01:25:40 +02:00

132 lines
5.0 KiB
Python

"""A re-park during probe-success resume must not orphan the agent. The waiting
record must stay until ``spawn_agent`` actually launches a container — deleting
it before the spawn lets a provider re-park (``spawn_agent`` bails OFFLINE on
the parked-provider short-circuit) leave the agent with no record for the
probe-resume loop to revive.
"""
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from unittest.mock import AsyncMock
from uuid import uuid4
import pytest
from roboco.models.runtime import AgentInstance, WaitingRecord
from roboco.runtime.orchestrator import (
AgentOrchestrator,
AgentReadinessError,
AgentState,
)
def _make_orchestrator() -> AgentOrchestrator:
orch = AgentOrchestrator.__new__(AgentOrchestrator)
orch._waiting_records = {}
orch._instances = {}
return orch
def _record(agent_id: str = "be-dev-1", provider: str = "anthropic") -> WaitingRecord:
return WaitingRecord(
agent_id=agent_id,
task_id=str(uuid4()),
waiting_for="rate_limit_lifted",
waiting_since=datetime.now(UTC),
context={"provider": provider},
)
def _instance(state: AgentState) -> AgentInstance:
cfg = type("C", (), {"provider_type": "anthropic", "model": "opus"})()
return AgentInstance(agent_id="be-dev-1", state=state, config=cfg)
class TestResolveWaitReparkKeepsRecord:
async def test_record_kept_when_spawn_bails_offline(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A re-park bails spawn with an OFFLINE instance — the record must stay
so the next probe-success re-attempts the resume (not orphan the agent)."""
orch = _make_orchestrator()
orch._waiting_records = {"be-dev-1": _record()}
offline = _instance(AgentState.OFFLINE)
monkeypatch.setattr(orch, "spawn_agent", AsyncMock(return_value=offline))
delete_mock = AsyncMock()
monkeypatch.setattr(orch, "_delete_waiting_record", delete_mock)
result = await orch.resolve_wait(
"be-dev-1", {"reason": "rate_limit_lifted", "provider": "anthropic"}
)
assert result is offline
# Record kept — the probe-resume loop can re-attempt on the next clear.
assert "be-dev-1" in orch._waiting_records
delete_mock.assert_not_awaited()
async def test_record_deleted_when_spawn_launches(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A successful launch (ACTIVE) tears down the record as before."""
orch = _make_orchestrator()
orch._waiting_records = {"be-dev-1": _record()}
active = _instance(AgentState.ACTIVE)
monkeypatch.setattr(orch, "spawn_agent", AsyncMock(return_value=active))
delete_mock = AsyncMock()
monkeypatch.setattr(orch, "_delete_waiting_record", delete_mock)
result = await orch.resolve_wait(
"be-dev-1", {"reason": "rate_limit_lifted", "provider": "anthropic"}
)
assert result is active
assert "be-dev-1" not in orch._waiting_records
delete_mock.assert_awaited_once_with("be-dev-1")
async def test_record_deleted_when_spawn_raises(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A spawn failure (e.g. readiness refused → task auto-blocked) tears
down the record so the probe loop doesn't keep re-resuming a task that
moved to a different state. Matches the pre-fix behavior (the record
was deleted before the spawn attempt)."""
orch = _make_orchestrator()
orch._waiting_records = {"be-dev-1": _record()}
async def _boom(*_a: Any, **_k: Any) -> AgentInstance:
raise AgentReadinessError("not ready")
monkeypatch.setattr(orch, "spawn_agent", _boom)
delete_mock = AsyncMock()
monkeypatch.setattr(orch, "_delete_waiting_record", delete_mock)
with pytest.raises(AgentReadinessError):
await orch.resolve_wait(
"be-dev-1", {"reason": "rate_limit_lifted", "provider": "anthropic"}
)
assert "be-dev-1" not in orch._waiting_records
delete_mock.assert_awaited_once_with("be-dev-1")
async def test_record_kept_across_repeated_offline_bails(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Two probe-success resumes that both bail (provider flaps) must both
find the record still present — the agent is never orphaned across the
flap, and the durable delete is never called."""
orch = _make_orchestrator()
orch._waiting_records = {"be-dev-1": _record()}
offline = _instance(AgentState.OFFLINE)
monkeypatch.setattr(orch, "spawn_agent", AsyncMock(return_value=offline))
delete_mock = AsyncMock()
monkeypatch.setattr(orch, "_delete_waiting_record", delete_mock)
for _ in range(3):
await orch.resolve_wait(
"be-dev-1", {"reason": "rate_limit_lifted", "provider": "anthropic"}
)
assert "be-dev-1" in orch._waiting_records
delete_mock.assert_not_awaited()