Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions orchestrator/approval_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,56 @@ def is_context_change_nack(
return False
return not bool(prev_refs & new_refs)

def seed_auto_ack_for_empty_pure_producers(self, producers_with_tasks: set[str]) -> list[str]:
"""Pre-seed proposal + critical-reviewer ACKs for pure producers
whose role has no tasks in this slice.

Prevents a BRC deadlock (#2581) where a producer-only slice (e.g.
tester-only or documenter-only) leaves CODER with no work but
still spawned: CODER's critical reviewers (REVIEWER_CODE et al.)
have nothing to review and may NACK indefinitely, since the
protocol requires every critical reviewer to ACK at the latest
version.

Behavior, per producer ``P`` in the graph not present in
``producers_with_tasks``:

* Skip ``P`` if ``graph.is_dual_role(P)`` — a dual-role producer
(e.g. TESTER also reviews CODER) must always run so it can
discharge its reviewer responsibilities for the *other*
producers; auto-ACKing it as a producer is fine in principle,
but right now no role besides TESTER is dual-role, and skipping
here keeps the rule trivially aligned with the "tester always
runs" intent.
* Otherwise record an empty proposal at version 1, then record
an ACK at version 1 from **every** critical reviewer of ``P``.
The ACK from a dual-role reviewer (e.g. TESTER reviewing
CODER) is a starting state, not a final say: if the
dual-role reviewer's own producer work later uncovers a need
for ``P`` to produce something, it can NACK at version 1, which
overrides the seeded ACK and forces ``P`` to re-propose at
version 2 via the normal flow.

The producer container is still spawned by the caller — this only
pre-seeds the matrix. If the agent later proposes for real, the
version bumps and the seeded ACKs are superseded by the normal
flow.

Returns the list of producer roles that were auto-ACKed (mostly
useful for logging / tests).
"""
auto_acked: list[str] = []
for producer in sorted(self._graph._producer_roles):
if producer in producers_with_tasks:
continue
if self._graph.is_dual_role(producer):
continue
version = self.record_proposal(producer)
for reviewer in self._graph.critical_reviewers_for(producer):
self.record_ack(reviewer, producer, version=version)
auto_acked.append(producer)
return auto_acked

def is_fully_acked(self, producer: str) -> bool:
"""Check if all critical reviewers have ACKed the producer's latest proposal.

Expand Down
35 changes: 35 additions & 0 deletions orchestrator/concurrent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def __init__(
review_graph: ReviewGraph | None = None,
roles: list[AgentRole] | None = None,
slice_id: str | None = None,
producer_roles_with_tasks: set[str] | None = None,
) -> None:
"""Initialise the executor.

Expand All @@ -167,13 +168,25 @@ def __init__(
``egg/issue-N/{slice_id}/{role}/work`` so commits across
slices stay isolated. ``None`` preserves the pre-slicing
pipeline-scoped semantics.
producer_roles_with_tasks: Set of producer role names that
actually have at least one task in the slice's plan
(#2581). When provided, ``spawn_all`` calls
``tracker.seed_auto_ack_for_empty_pure_producers`` after
registering agents so pure producers (CODER, DOCUMENTER)
with no tasks don't deadlock BRC consensus waiting for
reviewers to ACK an empty proposal. ``None`` (the
default) preserves the prior unconditional-roster
behavior; callers that don't know the slice's task list
yet (CUSTOM-mode, BABYSIT, prompt-mode pipelines) leave
it unset.
"""
self.pipeline = pipeline
self.spawn_fn = spawn_fn
self.max_concurrent = max_concurrent
self._review_graph = review_graph
self._roles_override = roles
self._slice_id = slice_id
self._producer_roles_with_tasks = producer_roles_with_tasks
self._failure_times: list[datetime] = []
self._lock = threading.Lock()

Expand Down Expand Up @@ -397,6 +410,28 @@ def spawn_all(
for role in roles:
tracker.register_agent(role.value)

# Pre-seed BRC consensus for pure producers that have no tasks in
# this slice (#2581). Without this, e.g. a tester-only or
# documenter-only slice still spawns CODER, which proposes an
# empty artifact list — and CODER's pure reviewers can NACK
# forever ("no code to review"), spiraling until
# ``max_revision_rounds``. The seeder records an empty proposal +
# synthetic ACKs from pure reviewers; dual-role reviewers (TESTER
# reviewing CODER) are intentionally left to run and decide.
# Skipped when callers don't supply the task-role set (CUSTOM-mode,
# BABYSIT, prompt-mode), preserving the prior behavior.
if self._producer_roles_with_tasks is not None:
auto_acked = tracker.seed_auto_ack_for_empty_pure_producers(
self._producer_roles_with_tasks
)
if auto_acked:
logger.info(
"Pre-seeded BRC auto-ACK for empty pure producers",
pipeline_id=self.pipeline.id,
slice_id=self._slice_id,
auto_acked=auto_acked,
)

return self._spawn_roles(roles, agent_prompts or {})

def spawn_specific_roles(
Expand Down
10 changes: 10 additions & 0 deletions orchestrator/peer_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ def register_agent(self, role: str) -> None:
if self.graph.is_reviewer(role):
self._reviewer_phases[role] = ConsensusPhase.WORKING

def seed_auto_ack_for_empty_pure_producers(self, producers_with_tasks: set[str]) -> list[str]:
"""Pre-seed the matrix for pure producers absent from the slice's task list.

Thin lock-holding wrapper around
``ApprovalMatrix.seed_auto_ack_for_empty_pure_producers`` (#2581).
See that method's docstring for the dual-role / pure-reviewer rules.
"""
with self._lock:
return self.matrix.seed_auto_ack_for_empty_pure_producers(producers_with_tasks)

def release_nudge(self, role: str, version: int) -> None:
"""Roll back a nudge memo entry recorded by ``_collect_newly_ready_producers``.

Expand Down
29 changes: 29 additions & 0 deletions orchestrator/routes/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -14946,6 +14946,34 @@ def _run_concurrent_phase(
]
filtered_graph = ReviewGraph(filtered_edges)

# Determine which producer roles the slice's plan actually assigns
# tasks to (#2581). Used to pre-seed auto-ACKs for pure producers
# (e.g. CODER, DOCUMENTER) that the planner didn't include —
# otherwise their empty proposal can deadlock BRC consensus when
# reviewers NACK "nothing to review". Only meaningful for
# per-slice runs against a contracted pipeline; CUSTOM-mode,
# BABYSIT, and prompt-mode pipelines fall through to ``None``
# which preserves the pre-#2581 unconditional-roster behavior.
producer_roles_with_tasks: set[str] | None = None
if slice_id is not None and getattr(pipeline, "has_contract", True):
try:
from egg_contracts.loader import load_contract as _load_contract_for_seed

_contract = _load_contract_for_seed(pipeline.id, worktree_repo_path)
_slice_obj = next((s for s in _contract.slices if s.id == slice_id), None)
if _slice_obj is not None:
# ``Task.role`` is ``str | None``; ``None`` is the
# execution-time coder default per the contract schema.
producer_roles_with_tasks = {(t.role or "coder") for t in _slice_obj.tasks}
except Exception:
logger.debug(
"Could not derive producer_roles_with_tasks for auto-ACK seeding",
pipeline_id=pipeline.id,
slice_id=slice_id,
exc_info=True,
)
producer_roles_with_tasks = None

# Resolve base branch for diff commands in agent prompts.
_resolved_base_branch = pipeline.base_branch
if not _resolved_base_branch:
Expand Down Expand Up @@ -14999,6 +15027,7 @@ def _run_concurrent_phase(
review_graph=filtered_graph,
roles=roles,
slice_id=slice_id,
producer_roles_with_tasks=producer_roles_with_tasks,
)

# Spawn all agents with their prompts.
Expand Down
163 changes: 163 additions & 0 deletions orchestrator/tests/test_auto_ack_pure_producers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
"""Tests for the pure-producer auto-ACK seed (#2581).

Covers ``ApprovalMatrix.seed_auto_ack_for_empty_pure_producers`` and the
delegating ``PeerConsensusTracker.seed_auto_ack_for_empty_pure_producers``
wrapper. The seed exists to prevent BRC consensus deadlock for slices
whose plan omits a producer role (e.g. a tester-only or documenter-only
slice): without it, CODER spawns with no work, its critical reviewers
have nothing to review, and consensus stalls.
"""

from __future__ import annotations

import sys
from pathlib import Path

import pytest

_orchestrator_path = Path(__file__).parent.parent
if str(_orchestrator_path) not in sys.path:
sys.path.insert(0, str(_orchestrator_path))

from approval_matrix import ApprovalMatrix, ApprovalState
from peer_consensus import PeerConsensusTracker
from review_graph import get_default_implement_graph


@pytest.fixture
def implement_graph():
return get_default_implement_graph()


@pytest.fixture
def matrix(implement_graph):
return ApprovalMatrix(implement_graph)


class TestSeedAutoAckEmptyPureProducers:
"""Direct tests against ``ApprovalMatrix``."""

def test_documenter_only_slice_auto_acks_coder(self, matrix):
"""A documenter-only slice should auto-ACK CODER so its reviewers
don't deadlock on an empty proposal."""
auto_acked = matrix.seed_auto_ack_for_empty_pure_producers({"documenter"})
assert auto_acked == ["coder"]
# CODER's consensus is fully satisfied after seeding — every
# critical reviewer (including TESTER) has a seeded ACK at v1.
assert matrix.is_fully_acked("coder") is True

def test_tester_only_slice_auto_acks_coder_and_documenter(self, matrix):
"""A tester-only slice has neither a coder nor a documenter task —
both pure producers should auto-ACK."""
auto_acked = matrix.seed_auto_ack_for_empty_pure_producers({"tester"})
assert auto_acked == ["coder", "documenter"]
assert matrix.is_fully_acked("coder") is True
# DOCUMENTER has no critical reviewers in the default implement
# graph, so ``is_fully_acked`` returns True once any proposal is
# recorded.
assert matrix.is_fully_acked("documenter") is True

def test_coder_only_slice_auto_acks_only_documenter(self, matrix):
"""A coder-only slice: DOCUMENTER (pure producer) auto-ACKs;
TESTER is dual-role and is intentionally skipped so its reviewer
responsibility for CODER stays active."""
auto_acked = matrix.seed_auto_ack_for_empty_pure_producers({"coder"})
assert auto_acked == ["documenter"]
assert matrix.is_fully_acked("documenter") is True
# TESTER not auto-ACKed — its producer-side proposal version is
# still 0, so it isn't fully-ACKed.
assert matrix.get_proposal_version("tester") == 0
assert matrix.is_fully_acked("tester") is False

def test_all_producers_present_is_noop(self, matrix):
"""When every producer has at least one task, seeding is a no-op."""
auto_acked = matrix.seed_auto_ack_for_empty_pure_producers(
{"coder", "tester", "documenter"}
)
assert auto_acked == []
assert matrix.get_proposal_version("coder") == 0
assert matrix.get_proposal_version("documenter") == 0

def test_seeded_acks_carry_critical_reviewers(self, matrix):
"""The seeded proposal must satisfy every critical reviewer of
the auto-ACKed producer, including dual-role reviewers like
TESTER — that's what makes the empty proposal actually reach
consensus instead of just being recorded."""
matrix.seed_auto_ack_for_empty_pure_producers({"documenter"})
graph = matrix._graph
for reviewer in graph.critical_reviewers_for("coder"):
entry = matrix.get_entry(reviewer, "coder")
assert entry is not None
assert entry.state == ApprovalState.ACKED
assert entry.version == 1

def test_dual_role_producer_is_never_auto_acked(self, matrix):
"""Even when TESTER has no tasks (documenter-only slice), it must
not be auto-ACKed as a producer — its dual role means it should
always run so it can ACK/NACK CODER for real."""
matrix.seed_auto_ack_for_empty_pure_producers({"documenter"})
# TESTER's producer-side state: untouched.
assert matrix.get_proposal_version("tester") == 0

def test_dual_role_reviewer_can_nack_to_override_seeded_ack(self, matrix):
"""The 'tester may need coder to do some work' recovery path:
TESTER's seeded ACK of CODER is a starting state, not a verdict.
If TESTER's producer work later uncovers a need for code, it can
NACK at the seeded version and force CODER to re-propose."""
matrix.seed_auto_ack_for_empty_pure_producers({"tester"})
assert matrix.is_fully_acked("coder") is True

matrix.record_nack("tester", "coder", version=1, reason="need helper module")

assert matrix.is_fully_acked("coder") is False
entry = matrix.get_entry("tester", "coder")
assert entry is not None
assert entry.state == ApprovalState.NACKED

def test_real_proposal_supersedes_seeded_acks(self, matrix):
"""If the producer's container later proposes for real (version
bump), the seeded version-1 ACKs are no longer at the latest
version — the normal flow re-acquires fresh ACKs at v2."""
matrix.seed_auto_ack_for_empty_pure_producers({"documenter"})
assert matrix.get_proposal_version("coder") == 1

new_version = matrix.record_proposal("coder")
assert new_version == 2
# is_fully_acked falls back to False because seeded ACKs are at v1.
assert matrix.is_fully_acked("coder") is False

def test_seed_called_twice_is_idempotent_in_effect(self, matrix):
"""Calling the seeder twice with the same task set bumps the
proposal version but leaves consensus reachable — the second
call records ACKs at the new (v2) version. Idempotent in the
sense that the post-state is still fully-ACKed."""
first = matrix.seed_auto_ack_for_empty_pure_producers({"documenter"})
second = matrix.seed_auto_ack_for_empty_pure_producers({"documenter"})
assert first == ["coder"]
assert second == ["coder"]
assert matrix.is_fully_acked("coder") is True


class TestTrackerDelegation:
"""The ``PeerConsensusTracker`` wrapper holds the lock and
delegates to the matrix. These tests are thin — the heavy lifting
is covered above."""

def test_tracker_delegates_to_matrix(self, implement_graph):
tracker = PeerConsensusTracker(
pipeline_id="test-pipeline",
graph=implement_graph,
)
auto_acked = tracker.seed_auto_ack_for_empty_pure_producers({"documenter"})
assert auto_acked == ["coder"]
assert tracker.matrix.is_fully_acked("coder") is True

def test_tracker_noop_when_all_producers_present(self, implement_graph):
tracker = PeerConsensusTracker(
pipeline_id="test-pipeline",
graph=implement_graph,
)
auto_acked = tracker.seed_auto_ack_for_empty_pure_producers(
{"coder", "tester", "documenter"}
)
assert auto_acked == []
Loading