feat(correction): implement cross-plan correction cascading with child plan state handling #547

Closed
opened 2026-03-04 01:02:08 +00:00 by freemo · 1 comment
Owner

Metadata

Field Value
Type Feature
Priority High
MoSCoW Should Have
Points 8
Milestone v3.5.0
Assignee freemo
Parent Epic #394 (Epic: Decision Framework)
Depends On #542 (influence DAG traversal)

Background

The specification (§ Correction > Cross-Plan Correction Cascading, lines 28393-28413) defines 4 child-plan-state-dependent behaviors when a correction's affected subtree includes child plans:

Child Plan State Action
Not yet started Cancel the child plan
In progress Cancel + rollback sandbox to pre-child-plan state
Completed but not applied Cancel + rollback sandbox
Already applied Reject the correction (cannot undo applied changes)

Current state: The CorrectionService handles corrections within a single plan but has NO cross-plan logic. No grep results for cross_plan_correction or correction_cascad in the codebase. When a correction's affected subtree touches a child plan, the behavior is undefined.

Acceptance Criteria

  1. When a correction's affected subtree (from #542) includes child plan nodes, the correction engine checks each child plan's state.
  2. Child plans not yet started are cancelled.
  3. Child plans in progress are cancelled and their sandboxes rolled back.
  4. Child plans completed but not applied are cancelled and rolled back.
  5. If any child plan is already applied, the correction is rejected with a clear explanation.
  6. Rejection returns an CorrectionRejection result with reason and affected applied child plan IDs.
  7. All cascading actions are atomic: either all succeed or the correction is fully rolled back.

Subtasks

1. Design

  • Define child plan state detection logic
  • Design atomic cascade-or-rollback transaction
  • Design CorrectionRejection result type

2. Implementation

  • Add child plan state inspection to CorrectionService
  • Implement cancel logic for not-started child plans
  • Implement cancel + sandbox rollback for in-progress and completed-unapplied child plans
  • Implement rejection logic for already-applied child plans
  • Ensure atomicity (transaction boundary around cascade)

3. Testing

  • Unit test: correction cascades to not-started child plan → cancel
  • Unit test: correction cascades to in-progress child plan → cancel + rollback
  • Unit test: correction cascades to applied child plan → rejection
  • Unit test: mixed child plan states → reject if any applied
  • Integration test: end-to-end cross-plan correction

4. Documentation

  • Update CorrectionService docstrings
  • Document rejection scenarios

5. Integration

  • Wire into existing correction workflow
  • Verify sandbox rollback compatibility

6. Observability

  • Log cascade decisions per child plan
  • Log rejection reasons

7. Security

  • Ensure cascade cannot bypass plan isolation boundaries

Definition of Done

  • All acceptance criteria met
  • All subtask checkboxes checked
  • Tests pass in CI
  • Code reviewed and approved
## Metadata | Field | Value | |-------|-------| | **Type** | Feature | | **Priority** | High | | **MoSCoW** | Should Have | | **Points** | 8 | | **Milestone** | v3.5.0 | | **Assignee** | freemo | | **Parent Epic** | #394 (Epic: Decision Framework) | | **Depends On** | #542 (influence DAG traversal) | ## Background The specification (§ Correction > Cross-Plan Correction Cascading, lines 28393-28413) defines 4 child-plan-state-dependent behaviors when a correction's affected subtree includes child plans: | Child Plan State | Action | |-----------------|--------| | Not yet started | Cancel the child plan | | In progress | Cancel + rollback sandbox to pre-child-plan state | | Completed but not applied | Cancel + rollback sandbox | | Already applied | **Reject the correction** (cannot undo applied changes) | **Current state:** The `CorrectionService` handles corrections within a single plan but has NO cross-plan logic. No grep results for `cross_plan_correction` or `correction_cascad` in the codebase. When a correction's affected subtree touches a child plan, the behavior is undefined. ## Acceptance Criteria 1. When a correction's affected subtree (from #542) includes child plan nodes, the correction engine checks each child plan's state. 2. Child plans not yet started are cancelled. 3. Child plans in progress are cancelled and their sandboxes rolled back. 4. Child plans completed but not applied are cancelled and rolled back. 5. If any child plan is already applied, the correction is rejected with a clear explanation. 6. Rejection returns an `CorrectionRejection` result with reason and affected applied child plan IDs. 7. All cascading actions are atomic: either all succeed or the correction is fully rolled back. ## Subtasks ### 1. Design - [ ] Define child plan state detection logic - [ ] Design atomic cascade-or-rollback transaction - [ ] Design CorrectionRejection result type ### 2. Implementation - [ ] Add child plan state inspection to CorrectionService - [ ] Implement cancel logic for not-started child plans - [ ] Implement cancel + sandbox rollback for in-progress and completed-unapplied child plans - [ ] Implement rejection logic for already-applied child plans - [ ] Ensure atomicity (transaction boundary around cascade) ### 3. Testing - [ ] Unit test: correction cascades to not-started child plan → cancel - [ ] Unit test: correction cascades to in-progress child plan → cancel + rollback - [ ] Unit test: correction cascades to applied child plan → rejection - [ ] Unit test: mixed child plan states → reject if any applied - [ ] Integration test: end-to-end cross-plan correction ### 4. Documentation - [ ] Update CorrectionService docstrings - [ ] Document rejection scenarios ### 5. Integration - [ ] Wire into existing correction workflow - [ ] Verify sandbox rollback compatibility ### 6. Observability - [ ] Log cascade decisions per child plan - [ ] Log rejection reasons ### 7. Security - [ ] Ensure cascade cannot bypass plan isolation boundaries ## Definition of Done - [ ] All acceptance criteria met - [ ] All subtask checkboxes checked - [ ] Tests pass in CI - [ ] Code reviewed and approved
freemo added this to the v3.5.0 milestone 2026-03-04 01:03:30 +00:00
freemo self-assigned this 2026-03-04 01:41:13 +00:00
Author
Owner

Implementation Complete — PR #562

What was implemented

Cross-plan correction cascading with the four child-plan-state-dependent behaviours:

Child Plan State Action
Not yet started Cancel the child plan
In progress (Strategize/Execute) Cancel + rollback sandbox
Completed but not applied Cancel + rollback sandbox
Already applied RejectCorrectionRejection returned

Key design decisions

  1. New CrossPlanCorrectionService rather than extending CorrectionService — follows single responsibility principle and avoids coupling the cross-plan cascade logic with existing single-plan correction logic.

  2. Protocol-based dependency injectionChildPlanLookup, ChildPlanCanceller, and SandboxRollbacker protocols allow the cascade service to be tested with mocks and wired to real implementations via the DI container.

  3. Atomic cascade-or-rollback — If any action fails mid-cascade, all previously completed actions are rolled back. If any child plan is already applied, the entire correction is rejected before any mutations occur.

  4. CorrectionRejection result type — Distinct from CorrectionResult so callers can discriminate rejection from failure or success at the type level.

  5. CorrectionStatus.REJECTED added to the lifecycle enum.

Files changed

  • src/cleveragents/domain/models/core/correction.py — New models
  • src/cleveragents/application/services/cross_plan_correction_service.py — New service
  • src/cleveragents/application/services/__init__.py — Export
  • features/cross_plan_correction.feature — 24 BDD scenarios
  • features/steps/cross_plan_correction_steps.py — Step definitions
  • robot/cross_plan_correction.robot — 8 e2e tests
  • robot/helper_cross_plan_correction.py — Robot helper
  • benchmarks/cross_plan_correction_bench.py — ASV benchmarks

Quality gates

  • nox -s lint
  • nox -s typecheck (0 errors)
  • nox -s unit_tests (8158 scenarios, 0 failures)
  • nox -s integration_tests (1145 tests, 0 failures)
  • nox -s coverage_report (97% ≥ threshold)
## Implementation Complete — PR #562 ### What was implemented Cross-plan correction cascading with the four child-plan-state-dependent behaviours: | Child Plan State | Action | |---|---| | Not yet started | Cancel the child plan | | In progress (Strategize/Execute) | Cancel + rollback sandbox | | Completed but not applied | Cancel + rollback sandbox | | Already applied | **Reject** — `CorrectionRejection` returned | ### Key design decisions 1. **New `CrossPlanCorrectionService`** rather than extending `CorrectionService` — follows single responsibility principle and avoids coupling the cross-plan cascade logic with existing single-plan correction logic. 2. **Protocol-based dependency injection** — `ChildPlanLookup`, `ChildPlanCanceller`, and `SandboxRollbacker` protocols allow the cascade service to be tested with mocks and wired to real implementations via the DI container. 3. **Atomic cascade-or-rollback** — If any action fails mid-cascade, all previously completed actions are rolled back. If any child plan is already applied, the entire correction is rejected before any mutations occur. 4. **`CorrectionRejection` result type** — Distinct from `CorrectionResult` so callers can discriminate rejection from failure or success at the type level. 5. **`CorrectionStatus.REJECTED`** added to the lifecycle enum. ### Files changed - `src/cleveragents/domain/models/core/correction.py` — New models - `src/cleveragents/application/services/cross_plan_correction_service.py` — New service - `src/cleveragents/application/services/__init__.py` — Export - `features/cross_plan_correction.feature` — 24 BDD scenarios - `features/steps/cross_plan_correction_steps.py` — Step definitions - `robot/cross_plan_correction.robot` — 8 e2e tests - `robot/helper_cross_plan_correction.py` — Robot helper - `benchmarks/cross_plan_correction_bench.py` — ASV benchmarks ### Quality gates - `nox -s lint` ✅ - `nox -s typecheck` ✅ (0 errors) - `nox -s unit_tests` ✅ (8158 scenarios, 0 failures) - `nox -s integration_tests` ✅ (1145 tests, 0 failures) - `nox -s coverage_report` ✅ (97% ≥ threshold)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#547
No description provided.