cli_plan_context_commands_steps.py defines after_scenario hook outside environment.py, causing resource management conflict #8420

Open
opened 2026-04-13 18:44:58 +00:00 by HAL9000 · 1 comment
Owner

Metadata

Commit: Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.
Branch: main

Background and Context

In Behave, lifecycle hooks (before_all, after_all, before_scenario, after_scenario, etc.) are intended to be defined exclusively in features/environment.py. Defining these hooks inside step files is non-standard and creates a resource management conflict: Behave may execute both the step file's hook and environment.py's hook, or only one of them, depending on load order.

At SHA 5a9aaa79edaefb1a257114f054ea87facb8efe69, features/steps/cli_plan_context_commands_steps.py defines its own after_scenario hook at the bottom of the file:

# Cleanup
def after_scenario(context, scenario):
    """Clean up after each scenario."""
    if hasattr(context, "test_dir"):
        # Change back to original directory
        if hasattr(context, "original_cwd"):
            os.chdir(context.original_cwd)
        # Remove test directory
        shutil.rmtree(context.test_dir, ignore_errors=True)

    # Clean up environment
    if "CLEVERAGENTS_TESTING_USE_MOCK_AI" in os.environ:
        del os.environ["CLEVERAGENTS_TESTING_USE_MOCK_AI"]

    # Reset container
    reset_container()

Meanwhile, features/environment.py also defines after_scenario which handles:

  • Returning to original working directory
  • Running cleanup handlers
  • Removing test directories
  • Cleaning up environment variables
  • Resetting Settings singleton
  • Resetting container singleton
  • Resetting provider registry
  • Cleaning up in-memory database engine cache
  • Removing per-scenario temp database files

The step file's after_scenario duplicates a subset of this logic. In Behave, when a step file defines a hook function with the same name as one in environment.py, the behavior is undefined and version-dependent. This can result in:

  1. The step file's cleanup running but environment.py's not running (or vice versa)
  2. Incomplete cleanup of context.test_dir if the wrong hook runs
  3. Leaked environment variables (CLEVERAGENTS_TESTING_USE_MOCK_AI) between scenarios

Current Behavior

features/steps/cli_plan_context_commands_steps.py defines after_scenario at module level. This conflicts with features/environment.py's after_scenario. The step file's cleanup is incomplete compared to environment.py's — it does not clean up temp database files, reset the Settings singleton, or flush the engine cache.

Expected Behavior

Per Behave best practices and the project's architecture, lifecycle hooks must only be defined in features/environment.py. Step files must not define before_* or after_* hooks. Any scenario-specific cleanup needed by cli_plan_context_commands_steps.py must be registered via context._cleanup_handlers (the mechanism already established in environment.py).

Acceptance Criteria

  • features/steps/cli_plan_context_commands_steps.py does not define any after_scenario, before_scenario, before_all, or after_all functions
  • All cleanup logic from the removed after_scenario is either already handled by environment.py or registered via context._cleanup_handlers.append(...) in the relevant step functions
  • Scenarios that use context.test_dir are correctly cleaned up after each run
  • CLEVERAGENTS_TESTING_USE_MOCK_AI is not leaked between scenarios
  • Full BDD suite passes with no resource leaks

Subtasks

  • Remove after_scenario function from features/steps/cli_plan_context_commands_steps.py
  • Verify environment.py's after_scenario already handles context.test_dir cleanup (it does — via context.test_dir check)
  • Verify environment.py's after_scenario already handles CLEVERAGENTS_TESTING_USE_MOCK_AI cleanup (it does — via LANGSMITH_ENV_VARS and env var cleanup)
  • Verify environment.py's after_scenario already calls reset_container() (it does)
  • Run full BDD suite to confirm no regressions

Definition of Done

This issue is closed when features/steps/cli_plan_context_commands_steps.py contains no lifecycle hook definitions and the full BDD suite passes without resource leaks between scenarios.


Automated by CleverAgents Bot
Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor

## Metadata **Commit:** `Build: Reinforced label enforcement, and ensure implementation workers dont continue work on a mergable PR.` **Branch:** main ## Background and Context In Behave, lifecycle hooks (`before_all`, `after_all`, `before_scenario`, `after_scenario`, etc.) are intended to be defined exclusively in `features/environment.py`. Defining these hooks inside step files is non-standard and creates a resource management conflict: Behave may execute both the step file's hook and `environment.py`'s hook, or only one of them, depending on load order. At SHA `5a9aaa79edaefb1a257114f054ea87facb8efe69`, `features/steps/cli_plan_context_commands_steps.py` defines its own `after_scenario` hook at the bottom of the file: ```python # Cleanup def after_scenario(context, scenario): """Clean up after each scenario.""" if hasattr(context, "test_dir"): # Change back to original directory if hasattr(context, "original_cwd"): os.chdir(context.original_cwd) # Remove test directory shutil.rmtree(context.test_dir, ignore_errors=True) # Clean up environment if "CLEVERAGENTS_TESTING_USE_MOCK_AI" in os.environ: del os.environ["CLEVERAGENTS_TESTING_USE_MOCK_AI"] # Reset container reset_container() ``` Meanwhile, `features/environment.py` also defines `after_scenario` which handles: - Returning to original working directory - Running cleanup handlers - Removing test directories - Cleaning up environment variables - Resetting Settings singleton - Resetting container singleton - Resetting provider registry - Cleaning up in-memory database engine cache - Removing per-scenario temp database files The step file's `after_scenario` duplicates a subset of this logic. In Behave, when a step file defines a hook function with the same name as one in `environment.py`, the behavior is undefined and version-dependent. This can result in: 1. The step file's cleanup running but `environment.py`'s not running (or vice versa) 2. Incomplete cleanup of `context.test_dir` if the wrong hook runs 3. Leaked environment variables (`CLEVERAGENTS_TESTING_USE_MOCK_AI`) between scenarios ## Current Behavior `features/steps/cli_plan_context_commands_steps.py` defines `after_scenario` at module level. This conflicts with `features/environment.py`'s `after_scenario`. The step file's cleanup is incomplete compared to `environment.py`'s — it does not clean up temp database files, reset the Settings singleton, or flush the engine cache. ## Expected Behavior Per Behave best practices and the project's architecture, lifecycle hooks must only be defined in `features/environment.py`. Step files must not define `before_*` or `after_*` hooks. Any scenario-specific cleanup needed by `cli_plan_context_commands_steps.py` must be registered via `context._cleanup_handlers` (the mechanism already established in `environment.py`). ## Acceptance Criteria - [ ] `features/steps/cli_plan_context_commands_steps.py` does not define any `after_scenario`, `before_scenario`, `before_all`, or `after_all` functions - [ ] All cleanup logic from the removed `after_scenario` is either already handled by `environment.py` or registered via `context._cleanup_handlers.append(...)` in the relevant step functions - [ ] Scenarios that use `context.test_dir` are correctly cleaned up after each run - [ ] `CLEVERAGENTS_TESTING_USE_MOCK_AI` is not leaked between scenarios - [ ] Full BDD suite passes with no resource leaks ## Subtasks - [ ] Remove `after_scenario` function from `features/steps/cli_plan_context_commands_steps.py` - [ ] Verify `environment.py`'s `after_scenario` already handles `context.test_dir` cleanup (it does — via `context.test_dir` check) - [ ] Verify `environment.py`'s `after_scenario` already handles `CLEVERAGENTS_TESTING_USE_MOCK_AI` cleanup (it does — via `LANGSMITH_ENV_VARS` and env var cleanup) - [ ] Verify `environment.py`'s `after_scenario` already calls `reset_container()` (it does) - [ ] Run full BDD suite to confirm no regressions ## Definition of Done This issue is closed when `features/steps/cli_plan_context_commands_steps.py` contains no lifecycle hook definitions and the full BDD suite passes without resource leaks between scenarios. --- **Automated by CleverAgents Bot** Supervisor: Bug Hunt Pool | Agent: bug-hunt-pool-supervisor
HAL9000 added this to the v3.3.0 milestone 2026-04-13 18:57:51 +00:00
Author
Owner

Verified — Defining Behave lifecycle hooks outside environment.py is a real architecture violation that can cause non-deterministic test cleanup failures and resource leaks between scenarios. MoSCoW: Must Have for v3.3.0 — test reliability is non-negotiable for a 97% coverage requirement. [AUTO-OWNR-1]


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

✅ **Verified** — Defining Behave lifecycle hooks outside environment.py is a real architecture violation that can cause non-deterministic test cleanup failures and resource leaks between scenarios. **MoSCoW: Must Have** for v3.3.0 — test reliability is non-negotiable for a 97% coverage requirement. [AUTO-OWNR-1] --- **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#8420
No description provided.