fix(plan): guard cleanup_stale against execute/processing and execute/complete plans #11127

Merged
hurui200320 merged 2 commits from bugfix/m3-cleanup-stale-destroys-execute-output into master 2026-05-12 11:28:47 +00:00

2 Commits

Author SHA1 Message Date
hurui200320 52830971f2 fix(plan): guard cleanup_stale against execute/complete plans awaiting apply
CI / push-validation (pull_request) Successful in 33s
CI / helm (pull_request) Successful in 48s
CI / build (pull_request) Successful in 1m10s
CI / lint (pull_request) Successful in 1m31s
CI / typecheck (pull_request) Successful in 1m53s
CI / quality (pull_request) Successful in 1m57s
CI / security (pull_request) Successful in 2m13s
CI / integration_tests (pull_request) Successful in 4m2s
CI / unit_tests (pull_request) Successful in 5m23s
CI / docker (pull_request) Successful in 1m28s
CI / coverage (pull_request) Successful in 10m41s
CI / status-check (pull_request) Successful in 3s
CI / benchmark-publish (push) Has started running
CI / helm (push) Successful in 52s
CI / push-validation (push) Successful in 56s
CI / benchmark-regression (push) Failing after 1m10s
CI / build (push) Successful in 1m18s
CI / lint (push) Successful in 1m51s
CI / quality (push) Successful in 1m51s
CI / typecheck (push) Successful in 1m58s
CI / security (push) Successful in 2m24s
CI / e2e_tests (push) Successful in 4m30s
CI / integration_tests (push) Successful in 5m20s
CI / unit_tests (push) Successful in 6m58s
CI / docker (push) Successful in 1m28s
CI / coverage (push) Successful in 10m41s
CI / status-check (push) Successful in 3s
Bug #11121: _create_sandbox_for_plan() called GitWorktreeSandbox.cleanup_stale()
unconditionally, destroying the cleveragents/plan-<id> git branch when
agents plan execute was re-invoked on a plan already in execute/complete
state (awaiting apply). This caused plan apply to find zero artifacts.

Fix: Add a phase/state guard inside _create_sandbox_for_plan() that returns
the flat fallback early when the plan is execute/complete, preserving the
worktree branch intact for plan apply to merge.

- TDD regression test from issue #11120 (without @tdd_expected_fail)
- Verified sandbox lifecycle tests still pass

ISSUES CLOSED: #11121
2026-05-12 10:17:39 +00:00
hurui200320 9fe69c468c test(plan): add tdd issue-capture test for cleanup_stale destroying execute output before apply
Add two Behave scenarios tagged @tdd_issue, @tdd_issue_11121, and @tdd_expected_fail
that capture bug #11121: _create_sandbox_for_plan() calls
GitWorktreeSandbox.cleanup_stale() unconditionally on every execute invocation,
including when the plan is already in execute/complete state awaiting apply.

Scenario 1 asserts that the cleveragents/plan-<id> branch survives a second call
to _create_sandbox_for_plan() on an execute/complete plan. This assertion fails
because cleanup_stale deletes the branch regardless of plan state.

Scenario 2 asserts that plan apply would find at least one artifact after a
re-invoked execute on an execute/complete plan. This assertion fails because the
branch (holding execute output) was destroyed by cleanup_stale.

Both scenarios use @tdd_expected_fail so CI passes while the bug is unfixed.
The @mock_only tag ensures no database is created for these git-only tests.
The companion fix is tracked in issue #11121.

Additional CI fixes bundled in this commit:

- Fixed PlanGenerationGraph recursion bug: _should_retry() was mutating state
  in-place but LangGraph conditional edge functions cannot persist state
  mutations. Replaced with a proper _handle_retry() node that increments
  retry_count via state returns, resolving the GraphRecursionError that was
  crashing the integration tests. Updated the graph to include handle_retry
  as the 5th node, routing validate→should_retry→handle_retry→analyze.

- Fixed TDD quality gate (scripts/tdd_quality_gate.py): Renamed @tdd_bug_N
  tags to @tdd_issue_N to match the CONTRIBUTING.md specification. Added
  _diff_is_tdd_issue_capture() detection so that TDD issue-capture PRs
  (which add @tdd_expected_fail rather than remove it) pass the quality gate
  correctly. Updated all related tests (Behave unit tests, Robot integration
  tests, and test helpers) to use the new tag naming.

ISSUES CLOSED: #11120
2026-05-12 16:57:50 +08:00