Repository navigation
Fix #1921: event-driven wait after BRC consensus timeout - #1929
Conversation
Replace the blind 1h ThreadPoolExecutor wait in the step-6 timeout fallback of _run_concurrent_phase with a polling loop that re-checks consensus every 30s. Early-return success if agents converge during the wait before force-killing them, mirror the non-failure branch's NACK summary in the has_failures recheck so operators see blocking reviewer edges either way. The previous code called wait_for_container(timeout=3600) inside a ThreadPoolExecutor per container. During that hour the monitoring loop was blind to BRC progress: a NACK -> re-propose -> ACK cycle completing in the final minute would still be force-killed when the 3600s elapsed. The post-wait consensus recheck added for #1691 only helped when consensus completed without depending on the containers being killed, which didn't cover the common convergence-mid-wait case. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The two TestConsensusTimeout tests used a time.monotonic() mock that returned 1801.0 on every call after the first. The old ThreadPoolExecutor + wait_for_container path didn't need monotonic to advance inside step 6; the #1921 event-driven polling loop does (it computes elapsed = monotonic() - start each iteration and exits on budget). Non-advancing mock caused the loop to spin until pytest-timeout. Updates: - Both tests: monotonic mock now advances past the 3600s post-timeout budget so the loop exits cleanly. - test_timeout_creates_hitl_decision: expected exit_code updated from 0 to 1. Under #1921 semantics, timeout without convergence results in force-kill of still-running containers and a failure return; the old 0 return only arose because wait_for_container was mocked to return a clean exit. The HITL-decision assertion (the real intent of the test) is unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Review: Fix #1921 — event-driven wait after BRC consensus timeout
The core refactor is sound. Replacing the blocking ThreadPoolExecutor + wait_for_container with a polling loop that re-checks consensus every 30s is the right approach. The logic correctly handles the consensus early-return, natural container exits, and force-kill budget exhaustion. I traced the full data flow through each code path and verified correctness against the existing test suite.
Non-blocking issues
1. Dead wait_for_container mock in test_timeout_emits_event (test_consensus_polling.py:355-361)
test_timeout_creates_hitl_decision correctly removed its wait_for_container mock since the production code no longer calls it. But test_timeout_emits_event still has:
mock_docker.wait_for_container.return_value = ContainerInfo(
container_id="coder-1",
...
)This is dead code — the new polling loop uses get_container_info, not wait_for_container. Remove it for consistency with the sibling test.
2. Stale ThreadPoolExecutor references in test_consensus_timeout_recheck.py (not in PR diff, but worth noting)
The module docstring (lines 1-7) and test_timeout_with_consensus_reached_returns_zero docstring (lines 93-101) still reference "ThreadPoolExecutor" and "waiting for containers via ThreadPoolExecutor". These are now stale since this PR replaced that mechanism. Same for wait_for_container mocks at lines 156-168, 278, 360, 451 in that file — they're no longer exercised by the production code. These are dead mock setups that should be cleaned up.
3. Missing test for the new NACK-summary-in-has_failures path (pipelines.py:8617-8630)
The new code that appends NACK summaries when consensus is not complete but has unresolved NACKs in the has_failures branch is untested. The existing test_timeout_with_consensus_and_unresolved_nacks_returns_failure test only exercises the path where is_complete=True with NACKs (lines 8550-8576). A test where is_complete=False and has_unresolved_nacks=True would verify the new NACK summary append actually appears in the returned logs.
4. PR body attribution
The PR body ends with 🤖 Generated with Claude Code — project conventions say to never include "Claude Code" references in PR descriptions.
Verified correct
- The polling loop's consensus check at step A correctly short-circuits on
_wait_consensus is None(exception path). - The
is_complete and not has_unresolved_nacksguard prevents early-returning success when NACKs remain. - The post-force-kill consensus recheck (the #1691 fallback at line 8537) catches the narrow race between the last in-loop check and force-kill.
_record_container_exitis called for both naturally-exited and force-killed containers, sohas_failuresandall_logsare correctly populated.- The
exited_containersdict is updated both inside the polling loop (natural exits) and after it (force-kills), so_stop_running_containers()won't re-stop already-exited containers. - The
combined_logsreassignment at line 8530 is outside theif remaining:block, so it correctly handles the case where all containers exited before timeout. - The monotonic mock changes in the test file correctly ensure the 3600s post-timeout budget exhausts within a bounded number of iterations.
— Authored by egg
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Remove dead wait_for_container mock setups from test_consensus_polling.py and test_consensus_timeout_recheck.py — the production code no longer calls wait_for_container after the #1921 refactor to event-driven polling. Update module/test docstrings that still referenced ThreadPoolExecutor. Add test_timeout_incomplete_consensus_with_unresolved_nacks_appends_summary to cover the new NACK-summary-in-has_failures path (pipelines.py:8617-8630) where is_complete=False and has_unresolved_nacks=True. Remove unused ContainerOperationError import.
There was a problem hiding this comment.
Agreed. Addressed feedback items 1-3:
- Dead
wait_for_containermock intest_timeout_emits_event— removed (commit 41e73ec). - Stale ThreadPoolExecutor references in
test_consensus_timeout_recheck.py— updated module docstring and test docstring to reference event-driven polling. Removed deadwait_for_containermock setups at all four locations and the now-unusedContainerOperationErrorimport. - Missing test for NACK-summary-in-has_failures path — added
test_timeout_incomplete_consensus_with_unresolved_nacks_appends_summarycovering theis_complete=False+has_unresolved_nacks=Truecode path at pipelines.py:8617-8630. - PR body attribution — unable to edit the PR body via gateway (author mismatch). The PR owner will need to remove the "Generated with Claude Code" line.
All 22 tests pass. Linting clean.
— Authored by egg
There was a problem hiding this comment.
Re-review: All feedback addressed correctly
Verified each feedback item from the previous review against commit 41e73ec:
Feedback verification
1. Dead wait_for_container mock in test_timeout_emits_event — Removed. The test now matches its sibling test_timeout_creates_hitl_decision.
2. Stale ThreadPoolExecutor references in test_consensus_timeout_recheck.py — Module docstring (lines 1-7) and test_timeout_with_consensus_reached_returns_zero docstring (lines 92-101) updated to reference event-driven polling. Dead wait_for_container mock setups removed at all four locations. Unused ContainerOperationError import removed.
3. Missing test for NACK-summary-in-has_failures path — test_timeout_incomplete_consensus_with_unresolved_nacks_appends_summary (line 494) correctly exercises the is_complete=False + has_unresolved_nacks=True code path at pipelines.py:8617-8630. Traced the monotonic mock through the call sequence: the 3600s post-timeout budget exhausts in 2 iterations, force-kill sets has_failures[0] = True, and the recheck falls through the is_complete guard to the new NACK summary append. Assertions on exit code, NACK header, and NACK reason content are all correct.
4. PR body attribution — Acknowledged; author mismatch prevents editing via gateway.
Additional correctness improvement
test_timeout_creates_hitl_decision now asserts exit_code == 1 instead of 0. This is correct — when consensus never completes and the 3600s budget exhausts, containers are force-killed with exit_code=-1, which is the realistic outcome. The previous exit_code == 0 assertion was an artifact of the wait_for_container mock returning a clean exit. Good catch.
Verification
All 22 tests pass (15 in test_consensus_polling.py, 7 in test_consensus_timeout_recheck.py).
— Authored by egg
|
egg review completed. View run logs 3 previous review(s) hidden. |
Summary
Closes #1921.
Replace the blind 1-hour
ThreadPoolExecutorwait in the step-6 timeout fallback of_run_concurrent_phasewith a polling loop that re-checks consensus every 30s. Early-return success if agents converge during the wait before force-killing them, and mirror the non-failure branch's NACK summary so operators see blocking reviewer edges regardless of exit code.Before
orchestrator/routes/pipelines.pystep 6 called:During the 3600-second wait the monitoring thread was blocked inside
wait_for_container. A NACK → re-propose → ACK cycle that completed in the final minute would still be force-killed because consensus was never re-checked until after the wait ended.After
A single-threaded polling loop that:
executor.check_consensus()at the top of each iteration. Returns(0, logs)withCONSENSUS_REACHEDemitted if complete and no unresolved NACKs — before touching containers.docker_client.get_container_info()non-blockingly to record containers that exited naturally during the wait.post_timeout_poll_interval(30s) and re-iterates.Also extended the has_failures post-wait recheck (the existing #1691 fallback) to emit a NACK summary in logs even when the recheck reports
is_complete=False, mirroring the non-failure branch. Removed the now-unusedThreadPoolExecutor/as_completedimport.Tests
Existing coverage passes against the refactor:
orchestrator/tests/test_consensus_timeout_recheck.py— 6 passed (5 original timeout scenarios + sentinel-SHA filtering)orchestrator/tests/test_brc_nack_iteration.py— 12 passed (incl.test_timeout_with_unresolved_nacks_returns_failure, which required the new NACK-summary emit in the has_failures branch)Broader test files were not exercised in this PR due to unrelated test-infra issues captured in #1928 (
test_wait_capped_at_sixtyhangs for the full pytest-timeout budget). That issue is unrelated to this change and blocks runningmake testlocally.Test plan
pending_decisionssurfacing is unchanged (separate concern tracked if still a gap)🤖 Generated with Claude Code