# Implementation Plan - Phase 7 Review Feedback Hardening

Address the PR review comments for the Phase 7 test hardening and nightly dashboard.

## Proposed Changes

### 1. Script Correction — `tools/aggregate_regression.py`

#### [MODIFY] [aggregate_regression.py](file:///home/Messier82/projects/service-book/tools/aggregate_regression.py)
- **Class Parsing & Regex**: Update the class detection regex to make the annotations optional:
  `r"((?:@[A-Za-z0-9_.\(\)\s=,\"]*?\s*)*)class\s+([A-Za-z0-9_]+)"`
  This correctly handles non-annotated classes and multiple classes per file. Scoping methods inside each class body using the sliced string remains.
- **Strict `is_test` Heuristic**: Remove the `startswith("r")` or `startswith("M")` check, relying solely on `@Test` and `@Benchmark` annotations to identify tests.
- **Limit Glob to Output Dir**: Restrict the flake marker search to `ADDITIONAL_OUTPUT_DIR` to avoid picking up stale workspace/build files:
  `glob.glob(f"{ADDITIONAL_OUTPUT_DIR}/**/flake-{sanitized_key}.txt", recursive=True)`
- **Streaks Realignment**:
  - `consecutive_green` (for de-quarantine): increments on `pass` and `flake`; resets on `fail`; neutral on `skip` and `missing`.
  - `consecutive_passes` (for promotion): increments on `pass`; resets on `fail` and `flake`; neutral on `skip` and `missing`.
- **Active Tests Table Layout**: Change the active tests header from `Consecutive Green` to `Consecutive Passes` and render `consecutive_passes`.
- **Tier Indexing**: Resolve active test tiers directly via the `SCENARIO_ID_TO_TIER` map rather than a linear scan of `SCENARIOS_MAP`. Print warnings showing all offending methods for any duplicate scenario ID mappings.
- **Dynamic Issue URLs**: Render issues URLs dynamically using `os.environ.get("GITHUB_REPOSITORY", "yorvana/android")`:
  `https://github.com/{repo}/issues/{issue}`
- **Delete Mock Arm**: Remove `get_mock_results()` and the `--mock` CLI flag.
- **Infra Failure Banner**: If `infra_failure = true` on the last run, prepend a warning banner under the title in the generated `docs/regression-dashboard.md`:
  ```markdown
  > [!CAUTION]
  > **Infrastructure Failure Detected**: The most recent scheduled run failed due to system/infrastructure issues and did not produce test results.
  ```

---

### 2. Flake Marker Cleanup & Instrumented Testing — `RetryRule.kt`

#### [MODIFY] [RetryRule.kt](file:///home/Messier82/projects/service-book/app/src/androidTest/java/com/yorvana/testsupport/RetryRule.kt)
- **Complete Directory Wipe**: Add a static flag/lock pattern to clear the entire `/sdcard/test-outputs/flakes` directory on the device once per test suite execution before the first test runs.
- **Combine Shell Commands**: Keep the optimized shell command syntax `mkdir -p ... && echo 'flaked' > ...`.

#### [NEW] [RetryRuleTest.kt](file:///home/Messier82/projects/service-book/app/src/androidTest/java/com/yorvana/testsupport/RetryRuleTest.kt)
Create an instrumented test class that programmatically applies the `RetryRule` to a custom statement (that throws on run 1 and passes on run 2) and verifies:
1. The rule successfully retries the test.
2. The flake marker file is written correctly in `/sdcard/test-outputs/flakes/`.
3. Subsequent test executions properly clean up existing markers.

---

### 3. Annotation Round-Tripping

#### [MODIFY] [LifecycleRobustnessRegressionTest.kt](file:///home/Messier82/projects/service-book/app/src/androidTest/java/com/yorvana/regression/lifecycle/LifecycleRobustnessRegressionTest.kt)
Import `com.yorvana.testsupport.tiers.ScenarioId` and annotate `rL04_backPressFromRecordDetail_returnsThroughStack` with `@ScenarioId("R-L04")`.

#### [MODIFY] [BillingSandboxRegressionTest.kt](file:///home/Messier82/projects/service-book/app/src/androidTest/java/com/yorvana/regression/billing/BillingSandboxRegressionTest.kt)
Import `com.yorvana.testsupport.tiers.ScenarioId` and annotate `rP04_restorePurchaseFromFreshAppData_flipsPremiumAndRerendersSettings` with `@ScenarioId("R-P04")`.

---

### 4. Git Conflicts & Database Resets

#### [MODIFY] [regression.yml](file:///home/Messier82/projects/service-book/.github/workflows/regression.yml)
Update the `Commit and Push Dashboard Update` step in the `aggregate-results` job to execute `git pull --rebase origin master` before pushing to handle any concurrent commits cleanly.

#### [MODIFY] [regression-history.json](file:///home/Messier82/projects/service-book/docs/regression-history.json)
Reset the JSON content to `{"runs": []}` to bootstrap nightly statistics cleanly without synthetic mock runs.

---

### 5. Python Code Quality Coverage

#### [NEW] [test_aggregate_regression.py](file:///home/Messier82/projects/service-book/tools/test_aggregate_regression.py)
Create a Python unit test using standard `unittest`:
- Test parsing of multiple classes per file (annotated & unannotated).
- Test strict `is_test` annotation checks.
- Test `consecutive_green` and `consecutive_passes` streak calculations.
- Test report parsing, tier lookup with duplicates warning, and markdown generation logic.

---

## Verification Plan

### Automated Tests
- Run Python aggregator tests: `python3 -m unittest tools/test_aggregate_regression.py`
- Run local Kotlin scanner unit tests: `./gradlew testDebugUnitTest`
- Run the new RetryRule instrumented test on GMD: `./gradlew pixel2api33DebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.yorvana.testsupport.RetryRuleTest`

### Manual Verification
- Execute `python3 tools/aggregate_regression.py` to confirm it runs cleanly and generates `docs/regression-dashboard.md` with an empty history format.
