Skip to content

Fix #1921: event-driven wait after BRC consensus timeout - #1929

Merged
jwbron merged 3 commits into
mainfrom
egg/fix-1921-consensus-timeout-event-driven-wait
Apr 23, 2026
Merged

jwbron merged 3 commits into
mainfrom
egg/fix-1921-consensus-timeout-event-driven-wait

Conversation

@jwbron

@jwbron jwbron commented Apr 23, 2026

Copy link
Copy Markdown
Owner

Summary

Closes #1921.

Replace the blind 1-hour 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, and mirror the non-failure branch's NACK summary so operators see blocking reviewer edges regardless of exit code.

Before

orchestrator/routes/pipelines.py step 6 called:

with ThreadPoolExecutor(max_workers=len(remaining)) as pool:
    def _wait_remaining(exec_info):
        docker_client.wait_for_container(exec_info.container_id, timeout=3600)
        ...
    futures = {pool.submit(_wait_remaining, e): e for e in remaining}
    for future in as_completed(futures):
        ...

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:

  1. Checks executor.check_consensus() at the top of each iteration. Returns (0, logs) with CONSENSUS_REACHED emitted if complete and no unresolved NACKs — before touching containers.
  2. Uses docker_client.get_container_info() non-blockingly to record containers that exited naturally during the wait.
  3. Sleeps post_timeout_poll_interval (30s) and re-iterates.
  4. Force-kills only after the 3600s budget is exhausted.

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-unused ThreadPoolExecutor/as_completed import.

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_sixty hangs for the full pytest-timeout budget). That issue is unrelated to this change and blocks running make test locally.

Test plan

  • CI runs full orchestrator test suite
  • Observe first real implement-phase run where prior timeout triggered; expect late-arriving CONSENSUS_PROPOSE/ACK to be recognized and phase to complete successfully
  • Verify pending_decisions surfacing is unchanged (separate concern tracked if still a gap)

🤖 Generated with Claude Code

jwbron and others added 2 commits April 23, 2026 13:06
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>
@james-in-a-box

This comment has been minimized.

@egg-reviewer egg-reviewer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_nacks guard 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_exit is called for both naturally-exited and force-killed containers, so has_failures and all_logs are correctly populated.
  • The exited_containers dict 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_logs reassignment at line 8530 is outside the if 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

@james-in-a-box

This comment has been minimized.

@james-in-a-box

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.

@james-in-a-box james-in-a-box Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Addressed feedback items 1-3:

  1. Dead wait_for_container mock in test_timeout_emits_event — removed (commit 41e73ec).
  2. Stale ThreadPoolExecutor references in test_consensus_timeout_recheck.py — updated module docstring and test docstring to reference event-driven polling. Removed dead wait_for_container mock setups at all four locations and the now-unused ContainerOperationError import.
  3. Missing test for NACK-summary-in-has_failures path — added test_timeout_incomplete_consensus_with_unresolved_nacks_appends_summary covering the is_complete=False + has_unresolved_nacks=True code path at pipelines.py:8617-8630.
  4. 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

@egg-reviewer egg-reviewer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@james-in-a-box

Copy link
Copy Markdown
Contributor

egg review completed. View run logs

3 previous review(s) hidden.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement phase fails after 90 min even when BRC consensus is actively converging (consensus_timeout_minutes=30 + blind 1h container-wait window)

1 participant