[AUTO-GUARD-1] Refactor: Long functions and deep nesting in src/cleveragents/cli/commands/plan.py #9028

Open
opened 2026-04-14 05:52:25 +00:00 by HAL9000 · 1 comment
Owner

Metadata

  • Commit Message: refactor(cli): extract helper functions to reduce length and nesting in plan.py
  • Branch: refactor/auto-guard-1-plan-py-long-functions-deep-nesting

Background and Context

The file src/cleveragents/cli/commands/plan.py contains several functions that are longer than 50 lines and have a nesting level greater than 4. This increases the cognitive complexity of the code and makes it harder to maintain and test.

Long Functions:

  • _plan_spec_dict (77 lines)
  • _execute_output_dict (167 lines)
  • _tell_streaming (106 lines)
  • _lifecycle_apply_with_id (81 lines)
  • _create_sandbox_for_plan (53 lines)
  • _apply_sandbox_changes (80 lines)

Deep Nesting:

  • _create_sandbox_for_plan (nesting level of 4)
  • _apply_sandbox_changes (nesting level of 4)
  • _tell_streaming (nesting level of 4)

Per CONTRIBUTING.md, files should be kept under 500 lines and functions should be short and focused. High cognitive complexity makes code harder to review, test, and maintain.

Expected Behavior

After this refactoring:

  • All functions in src/cleveragents/cli/commands/plan.py are ≤ 50 lines long.
  • Maximum nesting depth in any function does not exceed 3 levels.
  • All existing tests continue to pass.
  • No behavioral changes are introduced — this is a pure structural refactoring.

Acceptance Criteria

  • _plan_spec_dict is refactored to ≤ 50 lines by extracting focused helper functions.
  • _execute_output_dict is refactored to ≤ 50 lines by extracting focused helper functions.
  • _tell_streaming is refactored to ≤ 50 lines and nesting depth ≤ 3.
  • _lifecycle_apply_with_id is refactored to ≤ 50 lines by extracting focused helper functions.
  • _create_sandbox_for_plan is refactored to ≤ 50 lines and nesting depth ≤ 3.
  • _apply_sandbox_changes is refactored to ≤ 50 lines and nesting depth ≤ 3.
  • All existing BDD/Behave and Robot Framework tests pass without modification.
  • No new # type: ignore annotations are introduced.
  • Coverage remains ≥ 97%.
  • nox (all default sessions) passes without errors.

Subtasks

  • Analyze _plan_spec_dict and extract sub-functions for each logical section.
  • Analyze _execute_output_dict and extract sub-functions for each output section.
  • Refactor _tell_streaming to reduce nesting (early returns, extracted helpers).
  • Analyze _lifecycle_apply_with_id and extract sub-functions.
  • Refactor _create_sandbox_for_plan to reduce nesting depth to ≤ 3.
  • Refactor _apply_sandbox_changes to reduce nesting depth to ≤ 3.
  • Tests (Behave): Verify all existing plan command scenarios still pass.
  • Tests (Robot): Verify all existing plan integration tests still pass.
  • Verify coverage ≥ 97% via nox -s coverage_report.
  • Run nox (all default sessions), fix any errors.

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.

Automated by CleverAgents Bot
Agent: new-issue-creator

## Metadata - **Commit Message**: `refactor(cli): extract helper functions to reduce length and nesting in plan.py` - **Branch**: `refactor/auto-guard-1-plan-py-long-functions-deep-nesting` ## Background and Context The file `src/cleveragents/cli/commands/plan.py` contains several functions that are longer than 50 lines and have a nesting level greater than 4. This increases the cognitive complexity of the code and makes it harder to maintain and test. **Long Functions:** - `_plan_spec_dict` (77 lines) - `_execute_output_dict` (167 lines) - `_tell_streaming` (106 lines) - `_lifecycle_apply_with_id` (81 lines) - `_create_sandbox_for_plan` (53 lines) - `_apply_sandbox_changes` (80 lines) **Deep Nesting:** - `_create_sandbox_for_plan` (nesting level of 4) - `_apply_sandbox_changes` (nesting level of 4) - `_tell_streaming` (nesting level of 4) Per CONTRIBUTING.md, files should be kept under 500 lines and functions should be short and focused. High cognitive complexity makes code harder to review, test, and maintain. ## Expected Behavior After this refactoring: - All functions in `src/cleveragents/cli/commands/plan.py` are ≤ 50 lines long. - Maximum nesting depth in any function does not exceed 3 levels. - All existing tests continue to pass. - No behavioral changes are introduced — this is a pure structural refactoring. ## Acceptance Criteria - [ ] `_plan_spec_dict` is refactored to ≤ 50 lines by extracting focused helper functions. - [ ] `_execute_output_dict` is refactored to ≤ 50 lines by extracting focused helper functions. - [ ] `_tell_streaming` is refactored to ≤ 50 lines and nesting depth ≤ 3. - [ ] `_lifecycle_apply_with_id` is refactored to ≤ 50 lines by extracting focused helper functions. - [ ] `_create_sandbox_for_plan` is refactored to ≤ 50 lines and nesting depth ≤ 3. - [ ] `_apply_sandbox_changes` is refactored to ≤ 50 lines and nesting depth ≤ 3. - [ ] All existing BDD/Behave and Robot Framework tests pass without modification. - [ ] No new `# type: ignore` annotations are introduced. - [ ] Coverage remains ≥ 97%. - [ ] `nox` (all default sessions) passes without errors. ## Subtasks - [ ] Analyze `_plan_spec_dict` and extract sub-functions for each logical section. - [ ] Analyze `_execute_output_dict` and extract sub-functions for each output section. - [ ] Refactor `_tell_streaming` to reduce nesting (early returns, extracted helpers). - [ ] Analyze `_lifecycle_apply_with_id` and extract sub-functions. - [ ] Refactor `_create_sandbox_for_plan` to reduce nesting depth to ≤ 3. - [ ] Refactor `_apply_sandbox_changes` to reduce nesting depth to ≤ 3. - [ ] Tests (Behave): Verify all existing plan command scenarios still pass. - [ ] Tests (Robot): Verify all existing plan integration tests still pass. - [ ] Verify coverage ≥ 97% via `nox -s coverage_report`. - [ ] Run `nox` (all default sessions), fix any errors. ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. --- **Automated by CleverAgents Bot** Agent: new-issue-creator
HAL9000 added this to the v3.2.0 milestone 2026-04-14 06:13:07 +00:00
Author
Owner

Verified — Refactor: long functions and deep nesting in plan.py CLI. MoSCoW: Should-have. Priority: Medium — code maintainability.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Verified** — Refactor: long functions and deep nesting in plan.py CLI. MoSCoW: Should-have. Priority: Medium — code maintainability. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
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#9028
No description provided.