fix(lifecycle): stop PMs re-claiming tasks out of the closure queue — kills the i_will_plan review loop

A cell/main PM's i_will_plan could legally re-claim its own task from
awaiting_pm_review (a CLAIM_RULES edge added for post-respawn recovery),
resetting the task to in_progress and re-running submit_up -> pr_pass ->
awaiting_pm_review forever: one Sentinel conventions child looped eleven
full laps in four hours (14 reviews on one PR, 37 agent spawns) while
its root's closure check fired eighteen times and the Main PM could
never close anything.

The claim edge is gone from every table that carried it — CLAIM_RULES,
the claim ActionSpec's source statuses, the StatusTransition row, the
service-layer _ROLE_CLAIM_STATUSES twin, and the legacy enforcement
shim's operational-edge/role-gate entries (left divergent, it would be
the same silent two-table drift that produced this bug). The respawn
case the edge existed for is now served properly: _handle_pm_reentry
gained a third contract — a PM calling i_will_plan on its own
awaiting_pm_review task gets a steering envelope (no claim, no state
change) pointing at complete/request_changes, and give_me_work's next
hint for that status says the same instead of steering back into
i_will_plan. The no-transition review-claim path and the pm-review
dispatch prompt were already correct and are untouched, so closure
still converges through them.

A new bidirectional test asserts CLAIM_RULES and _ROLE_CLAIM_STATUSES
stay identical per PM role (the old comment claimed a sync test existed;
it checked one direction only). Lifecycle artifacts regenerated; the
parity suite's three unshaped session mocks fixed, zero AsyncMock
warnings remain.
This commit is contained in:
Renn F
2026-07-30 23:37:27 +02:00
parent 27e58f469f
commit d87e2d9b4e
12 changed files with 373 additions and 71 deletions
@@ -869,6 +869,26 @@ async def test_complete_matches_spec(role: str, status: str) -> None:
id=task_id, status="awaiting_ceo_approval", assigned_to=None, team="backend"
)
task_svc.all_subtasks_terminal.return_value = True
# complete's merge path runs _stamp_pm_findings_verified_or_rejection,
# which opens a session.begin_nested() savepoint and reads the findings
# ledger via ReviewFindingsRepository.list_for_task (session.execute ->
# .scalars().all()). Unconfigured, both calls resolve to auto-generated
# AsyncMock children: `async with <AsyncMock>():` fails the async
# context-manager protocol and `<AsyncMock-result>.scalars()` returns an
# unawaited coroutine — caught by the verb's own except Exception, but
# the never-awaited coroutine leaks a RuntimeWarning at GC time.
task_svc.session = MagicMock()
task_svc.session.execute = AsyncMock(
return_value=MagicMock(
scalars=MagicMock(return_value=MagicMock(all=MagicMock(return_value=[])))
)
)
task_svc.session.begin_nested = MagicMock(
return_value=MagicMock(
__aenter__=AsyncMock(return_value=None),
__aexit__=AsyncMock(return_value=False),
)
)
git_svc = AsyncMock()
git_svc.pr_merge.return_value = {"merged": True, "merge_commit_sha": "x"}
git_svc.create_pr.return_value = {"pr_number": 99, "pr_url": "x"}
@@ -1268,6 +1288,18 @@ async def test_claim_review_matches_spec(role: str, status: str) -> None:
task_svc.qa_claim.return_value = after
task_svc.list_in_progress_for_agent.return_value = []
task_svc.list_paused_for_agent.return_value = []
# claim_review's full=True briefing reads the findings ledger
# (findings.open_findings_for_task -> session.execute -> .scalars().all()).
# Unconfigured, session.execute auto-generates as an AsyncMock child whose
# call returns another AsyncMock; .scalars() on that is itself an
# unawaited coroutine — caught by open_findings_for_task's own fail-open
# except, but the dangling coroutine leaks a RuntimeWarning at GC time.
task_svc.session = MagicMock()
task_svc.session.execute = AsyncMock(
return_value=MagicMock(
scalars=MagicMock(return_value=MagicMock(all=MagicMock(return_value=[])))
)
)
deps = _make_deps(task_svc=task_svc)
c = Choreographer(deps)
@@ -1546,6 +1578,14 @@ async def test_claim_doc_task_matches_spec(role: str, status: str) -> None:
task_svc.doc_claim.return_value = after
task_svc.list_in_progress_for_agent.return_value = []
task_svc.list_paused_for_agent.return_value = []
# claim_doc_task's full=True briefing reads the findings ledger the same
# way claim_review's does (see that test's comment) — same fix.
task_svc.session = MagicMock()
task_svc.session.execute = AsyncMock(
return_value=MagicMock(
scalars=MagicMock(return_value=MagicMock(all=MagicMock(return_value=[])))
)
)
deps = _make_deps(task_svc=task_svc)
c = Choreographer(deps)
+20 -10
View File
@@ -491,6 +491,11 @@ def test_claim_rules_match_pre_gateway_table() -> None:
re-delegating fixes (scoped by give_me_work routing, which offers only the
caller's own assigned tasks). BACKLOG → PENDING is a separate `activate`
action (strict transitions; no implicit activate-on-claim).
AWAITING_PM_REVIEW is deliberately absent from both PM roles — a claim
edge there let a respawned PM's i_will_plan legally re-claim its own
review-queue task and loop the submit_up -> pr_pass -> awaiting_pm_review
cycle forever. See test_awaiting_pm_review_not_claimable_by_any_role.
"""
assert spec.CLAIM_RULES[spec.Role.DEVELOPER] == frozenset(
{spec.Status.PENDING, spec.Status.NEEDS_REVISION}
@@ -500,21 +505,26 @@ def test_claim_rules_match_pre_gateway_table() -> None:
{spec.Status.PENDING, spec.Status.AWAITING_DOCUMENTATION}
)
assert spec.CLAIM_RULES[spec.Role.CELL_PM] == frozenset(
{
spec.Status.PENDING,
spec.Status.NEEDS_REVISION,
spec.Status.AWAITING_PM_REVIEW,
}
{spec.Status.PENDING, spec.Status.NEEDS_REVISION}
)
assert spec.CLAIM_RULES[spec.Role.MAIN_PM] == frozenset(
{
spec.Status.PENDING,
spec.Status.NEEDS_REVISION,
spec.Status.AWAITING_PM_REVIEW,
}
{spec.Status.PENDING, spec.Status.NEEDS_REVISION}
)
def test_awaiting_pm_review_not_claimable_by_any_role() -> None:
"""The claim edge that let a respawned PM's i_will_plan reset an
awaiting_pm_review task (looping submit_up -> pr_pass -> awaiting_pm_review
forever) is permanently closed: no role may claim from this status. A PM
re-entering its own review task is steered by the choreographer directly
to complete/request_changes, never through claim.
"""
for role, statuses in spec.CLAIM_RULES.items():
assert spec.Status.AWAITING_PM_REVIEW not in statuses, role
# PR_REVIEWER's own review-gate claim (a different status) is untouched.
assert spec.Status.AWAITING_PR_REVIEW in spec.CLAIM_RULES[spec.Role.PR_REVIEWER]
def test_team_rules_pin_team_for_seeded_agents() -> None:
assert spec.ROLE_TEAM_RULES["be-dev-1"] == "backend"
assert spec.ROLE_TEAM_RULES["be-pm"] == "backend"