|
| 1 | +# Module Lifecycle Single-Owner Implementation Plan |
| 2 | + |
| 3 | +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| 4 | +
|
| 5 | +**Goal:** Ensure a successful Core module state transition invokes each AFTER lifecycle hook exactly once. |
| 6 | + |
| 7 | +**Architecture:** `PbxExtensionState` remains the sole lifecycle owner. `ReloadModuleStateAction` continues reacting to persisted module state and planning configuration actions, but no longer dispatches lifecycle callbacks. |
| 8 | + |
| 9 | +**Tech Stack:** PHP 8.4, PHPUnit 9, Phalcon-based MikoPBX Core. |
| 10 | + |
| 11 | +## Global Constraints |
| 12 | + |
| 13 | +- Do not change the module installation pipeline, reload coalescing, firewall behavior, worker handling, or external module code. |
| 14 | +- Preserve all existing configuration-action planning in `ReloadModuleStateAction`. |
| 15 | +- Use a failing regression test before changing production code. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +### Task 1: Prove model-event processing repeats AFTER hooks |
| 20 | + |
| 21 | +**Files:** |
| 22 | +- Create: `tests/Unit/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateActionTest.php` |
| 23 | +- Modify: none |
| 24 | + |
| 25 | +**Interfaces:** |
| 26 | +- Consumes: `ReloadModuleStateAction::handleModuleConfigChanges(ConfigClass $configClassObj, array $moduleRecord): void` |
| 27 | +- Produces: regression coverage for enabled and disabled records. |
| 28 | + |
| 29 | +- [ ] **Step 1: Write the failing regression test** |
| 30 | + |
| 31 | +Create a small `ConfigClass` test double with counters in `onAfterModuleEnable()` and `onAfterModuleDisable()`. Instantiate it without its environment-dependent constructor and call `handleModuleConfigChanges()` once with `disabled=0` and once with `disabled=1`. Assert both counters remain zero because model-event processing is not a lifecycle transition owner. |
| 32 | + |
| 33 | +- [ ] **Step 2: Run the focused test and verify RED** |
| 34 | + |
| 35 | +Run: |
| 36 | + |
| 37 | +```bash |
| 38 | +vendor/bin/phpunit -c tests/Unit/phpunit.xml tests/Unit/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateActionTest.php |
| 39 | +``` |
| 40 | + |
| 41 | +Expected: two assertion failures showing that the current implementation invoked the corresponding AFTER hooks once. |
| 42 | + |
| 43 | +### Task 2: Remove duplicate lifecycle dispatch |
| 44 | + |
| 45 | +**Files:** |
| 46 | +- Modify: `src/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateAction.php` |
| 47 | +- Test: `tests/Unit/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateActionTest.php` |
| 48 | + |
| 49 | +**Interfaces:** |
| 50 | +- Consumes: persisted `PbxExtensionModules` model-event records. |
| 51 | +- Produces: configuration action planning without lifecycle hook execution. |
| 52 | + |
| 53 | +- [ ] **Step 1: Implement the minimal fix** |
| 54 | + |
| 55 | +Delete only the final `onAfterModuleEnable()` / `onAfterModuleDisable()` dispatch block from `handleModuleConfigChanges()`. Do not modify fail2ban, nginx, crond, or manager action planning. |
| 56 | + |
| 57 | +- [ ] **Step 2: Run the focused test and verify GREEN** |
| 58 | + |
| 59 | +Run the focused PHPUnit command from Task 1. |
| 60 | + |
| 61 | +Expected: two tests, zero failures. |
| 62 | + |
| 63 | +- [ ] **Step 3: Run related regression tests** |
| 64 | + |
| 65 | +Run: |
| 66 | + |
| 67 | +```bash |
| 68 | +vendor/bin/phpunit -c tests/Unit/phpunit.xml tests/Unit/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateActionTest.php tests/Modules/PbxExtensionStateTest.php |
| 69 | +``` |
| 70 | + |
| 71 | +Expected: zero failures. |
| 72 | + |
| 73 | +- [ ] **Step 4: Run syntax and diff validation** |
| 74 | + |
| 75 | +Run: |
| 76 | + |
| 77 | +```bash |
| 78 | +php -l src/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateAction.php |
| 79 | +php -l tests/Unit/Core/Workers/Libs/WorkerModelsEvents/Actions/ReloadModuleStateActionTest.php |
| 80 | +git diff --check |
| 81 | +``` |
| 82 | + |
| 83 | +Expected: both files report no syntax errors and `git diff --check` exits successfully. |
| 84 | + |
| 85 | +- [ ] **Step 5: Run the broad available unit suite** |
| 86 | + |
| 87 | +Run the repository PHPUnit suite supported by the local environment. Record any environmental or pre-existing failures separately from failures caused by this change. |
| 88 | + |
| 89 | +- [ ] **Step 6: Commit the implementation** |
| 90 | + |
| 91 | +Stage only the Core source, regression test, and this plan, then commit with message: |
| 92 | + |
| 93 | +```text |
| 94 | +fix(modules): invoke lifecycle hooks once |
| 95 | +``` |
0 commit comments