Files
roboco/tests/unit/gateway/test_choreographer_delegate_guards.py
T
e9ca7d4036 Delegation detail-fidelity + PM-loop hardening (#541)
* feat(gateway): delegation detail-fidelity — details survive hand-off, both directions

Details thinned out at every delegation hop: a PM child task mapped to no
parent criterion was legal (coverage only surfaced at submit_up, after the
whole wave ran — a 12-subtask docs tree grew through 8 review rounds that
way, one child titled 'docs page and route wrapper' shipping only the
page), and QA could pass work on a gestalt read (a 4-scene video brief
shipped 3 scenes past every gate because the features existed only in
prose). Three chokepoint gates:

- delegate (down): every child must declare covers_parent_criteria
  resolving against the parent's real acceptance criteria — no mapping or
  an unresolvable ref rejects naming every offending child and the valid
  criteria; the success envelope carries parent_ac_coverage
  {covered, uncovered} so a wave-planning PM sees remaining gaps in the
  same turn. Full coverage stays enforced at submit_up (waves stay legal).
- pass_review (up): mandatory criteria_verified — one {criterion,
  evidence} entry per task AC, matched by the findings ledger's
  id-or-exact-text matcher, evidence soup-checked and capped; rejects
  naming the unverified criteria; entries render deterministically into
  qa_notes as '[AC] <criterion> — verified: <evidence>' lines. The old
  count-only ac_verdicts gate is superseded (arg kept for back-compat).
- video briefs (structured detail at origination): an enumerable feature
  list (release highlights, or input_props.highlights carried onto a
  reject re-author) becomes its own scene acceptance criterion, bounded to
  the AC caps; a re-author without highlights carries the
  feedback-addressed criterion instead.

Extracted findings.py's criterion matcher into shared unmatched_criteria /
uncovered_acceptance_criteria instead of duplicating it; criteria_verified
joins the WAF free-text exclusion set like findings/issues.

* fix(gateway): break the block/unblock wedge — four hardening fixes from the live PM loop

A cell task looped fe-pm/main-pm block/unblock for hours (10 cycles, 43
spawns): a transient GitHub API error resolving CI became an unwaivable
blocker finding whose own fix text said no code change was required, the
submit freshness guard then demanded a commit no finding called for,
escalate_up auto-blocked, and main-pm's correct recovery plan 422'd on
the approach length cap, degrading it to a bare unblock. Four fixes:

- pr_pass CI-unresolvable refusal is now explicitly transient-worded:
  retry pr_pass shortly, do NOT pr_fail over a CI-status lookup error —
  a platform blip is not a code finding
- submit freshness guard grants ONE unchanged-head resubmission per
  head sha when the findings ledger has zero open rows (all addressed
  without code changes) — stamped via the resubmit_unchanged_head
  marker so the same head can never loop a second time
- unblock carries a flip breaker: block_flip_count marker, and at the
  third flip a one-shot CEO notification flags the task as structurally
  wedged (unblock itself still succeeds — the breaker signals, it does
  not wedge recovery)
- i_will_plan's approach cap truncates at 800 chars instead of
  rejecting — an over-detailed plan must never cost the PM its turn

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-17 01:52:33 +02:00

398 lines
14 KiB
Python

"""Gate Set B: delegation-time guards in Choreographer.delegate.
Pre-gateway behavior: a PM could only call ``task_create`` while spawned
in-context, which only happened after the orchestrator saw them claim
and start a parent task. That implicit gate is restored explicitly here:
- PARENT_NOT_CLAIMED: parent must be in_progress AND assigned_to PM.
- SUBTASK_CAP: 8 children = soft warn (allowed), >12 = hard block.
"""
from __future__ import annotations
from datetime import UTC, datetime
from typing import Any
from unittest.mock import AsyncMock, MagicMock
from uuid import uuid4
import pytest
from roboco.services.base import ValidationError
from roboco.services.gateway.choreographer import (
Choreographer,
ChoreographerDeps,
DelegateInputs,
)
def _make_deps(**overrides: Any) -> ChoreographerDeps:
base: dict[str, Any] = {
"task": AsyncMock(),
"work_session": AsyncMock(),
"git": AsyncMock(),
"a2a": AsyncMock(),
"journal": AsyncMock(),
"audit": AsyncMock(),
"evidence_repo": AsyncMock(),
}
base.update(overrides)
repo = base["evidence_repo"]
for method in (
"list_unread_a2a",
"list_unread_mentions",
"list_pending_notifications",
"task_metadata_gaps",
"recent_team_activity",
"blockers_in_lane",
"journal_highlights_for_task",
):
getattr(repo, method).return_value = []
# C8: default-fresh journal:decision so PM-decision gate passes.
# Tests that exercise the gate boundary stub their own value.
# The check matches MagicMock and AsyncMock (the two default sentinel
# types pytest's unittest.mock leaves on un-stubbed return_values).
_ldef = base["journal"].latest_decision_at.return_value
if type(_ldef).__name__ in ("MagicMock", "AsyncMock"):
base["journal"].latest_decision_at.return_value = datetime.now(UTC)
return ChoreographerDeps(**base)
def _delegate_inputs() -> DelegateInputs:
return DelegateInputs(
title="Implement endpoint",
description="Add /v1/foo endpoint with tests",
assigned_to="be-dev-1",
team="backend",
task_type="code",
nature="technical",
acceptance_criteria=["GET /v1/foo returns 200 with body"],
intends_to_touch=["backend/api/routers/foo.py"],
)
@pytest.mark.asyncio
async def test_delegate_blocks_when_parent_not_in_progress() -> None:
"""Parent in 'pending' status (PM never called i_will_plan) blocks delegate."""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="pending",
assigned_to=pm_id,
)
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = []
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
# The spec's create_subtask gate requires the parent in_progress.
# Per Section 6 of the design doc, rejection messages now come from
# the spec — the spec rejects with 'invalid_state' citing the
# source-status set, not the gateway-specific i_will_plan hint.
assert body["error"] == "invalid_state"
assert "in_progress" in body["message"]
task_svc.create_subtask.assert_not_awaited()
@pytest.mark.asyncio
async def test_delegate_blocks_when_parent_assigned_to_other_agent() -> None:
"""Parent claimed by a different PM cannot be delegated against by us."""
pm_id = uuid4()
other_pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=other_pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
)
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = []
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] == "not_authorized"
assert "i_will_plan" in body["remediate"]
task_svc.create_subtask.assert_not_awaited()
@pytest.mark.asyncio
async def test_delegate_allows_when_parent_in_progress_and_owned() -> None:
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
acceptance_criteria=[],
)
new_task = MagicMock(id=uuid4())
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = []
task_svc.create_subtask.return_value = new_task
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] is None
assert body["status"] == "created"
task_svc.create_subtask.assert_awaited_once()
@pytest.mark.asyncio
async def test_delegate_blocks_when_subtask_cap_exceeded() -> None:
"""13+ subtasks = hard block (cap is 12)."""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
)
too_many = [MagicMock(id=uuid4()) for _ in range(13)]
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = too_many
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] == "invalid_state"
assert "13" in body["message"] or "consolidate" in body["remediate"].lower()
task_svc.create_subtask.assert_not_awaited()
@pytest.mark.asyncio
async def test_delegate_allows_when_subtask_cap_within_soft_zone() -> None:
"""8-12 subtasks: warn-but-allow."""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
acceptance_criteria=[],
)
many = [MagicMock(id=uuid4()) for _ in range(10)]
new_task = MagicMock(id=uuid4())
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = many
task_svc.create_subtask.return_value = new_task
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] is None
task_svc.create_subtask.assert_awaited_once()
@pytest.mark.asyncio
async def test_delegate_allows_at_zero_subtasks() -> None:
"""Empty subtask list is the common case."""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
acceptance_criteria=[],
)
new_task = MagicMock(id=uuid4())
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = []
task_svc.create_subtask.return_value = new_task
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] is None
@pytest.mark.asyncio
async def test_delegate_blocks_at_exact_cap_plus_one() -> None:
"""Cap is 12; 13th attempt blocks."""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
)
# Already 12 children — adding the 13th must be blocked.
twelve = [MagicMock(id=uuid4()) for _ in range(12)]
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = twelve
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] == "invalid_state"
task_svc.create_subtask.assert_not_awaited()
@pytest.mark.asyncio
async def test_delegate_blocks_when_parent_quick_context_empty() -> None:
"""delegate obligates the PM's quick_context resumption section on the
parent; an empty quick_context (PM never called note(scope='handoff'))
fails the tracing gate before any subtask is created."""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="",
)
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = []
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] == "tracing_gap"
assert "quick_context>=min" in body["missing"]
task_svc.create_subtask.assert_not_awaited()
@pytest.mark.asyncio
async def test_delegate_past_max_depth_returns_invalid_state_not_500() -> None:
"""Bug B: delegating past MAX_TASK_DEPTH raised a bare ValueError that
escaped uncaught as a 500 ExceptionGroup. ``_validate_parent_depth`` now
raises ``ValidationError`` (a ServiceError), and the choreographer
translates it to an ``invalid_state`` envelope whose remediate carries the
"create as a sibling" instruction — so the PM gets a clean, actionable
rejection instead of a crash, and the agent loop doesn't respawn-blind.
"""
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=uuid4(),
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Decomposition planned; cells implement their slice next.",
acceptance_criteria=[],
)
depth_msg = (
"Task hierarchy would exceed MAX_TASK_DEPTH=4. Create this work as a "
"sibling of the deepest task instead of a further nested subtask."
)
task_svc = AsyncMock()
task_svc.get.return_value = parent
task_svc.agent_for.return_value = MagicMock(role="cell_pm", team="backend")
task_svc.get_subtasks.return_value = []
# create_subtask -> create -> _validate_parent_depth raises ValidationError.
task_svc.create_subtask.side_effect = ValidationError(
depth_msg, field="parent_task_id"
)
deps = _make_deps(task=task_svc)
c = Choreographer(deps)
env = await c.delegate(pm_id, parent_id, _delegate_inputs())
body = env.as_dict()
assert body["error"] == "invalid_state"
assert "MAX_TASK_DEPTH" in body["message"]
# The remediation must reach the agent — the whole point of the fix.
assert "sibling" in body["remediate"]
task_svc.create_subtask.assert_awaited_once()
@pytest.mark.asyncio
async def test_delegate_static_guards_allows_cell_map_root() -> None:
# A cross-cell MegaTask root-subtask carries a cell_projects map with
# project_id=None, product_id=None — a valid branchless-coordination
# shape (batch.is_branchless_coordination). The first project/product
# guard must NOT reject it.
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=None,
product_id=None,
cell_projects=[{"team": "backend", "project_id": uuid4()}],
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="Cross-cell fan-out planned.",
)
deps = _make_deps()
c = Choreographer(deps)
result = await c._delegate_static_guards(
pm_id, parent_id, parent, _delegate_inputs()
)
# The project/product guard must not fire for a cell-map root.
assert result is None or "neither a project_id nor a product_id" not in (
getattr(result, "message", "") or ""
)
@pytest.mark.asyncio
async def test_delegate_static_guards_rejects_bare_no_project_no_product() -> None:
# No project_id, no product_id, no cell_projects — still rejected.
pm_id = uuid4()
parent_id = uuid4()
parent = MagicMock(
id=parent_id,
project_id=None,
product_id=None,
cell_projects=None,
status="in_progress",
assigned_to=pm_id,
team="backend",
quick_context="x",
)
deps = _make_deps()
c = Choreographer(deps)
result = await c._delegate_static_guards(
pm_id, parent_id, parent, _delegate_inputs()
)
assert result is not None
assert "neither a project_id nor a product_id" in (result.message or "")