|
| 1 | +# `flaky-unit-test-analysis` mode |
| 2 | + |
| 3 | +Custom [`MetaMask/ai-analyzer`](https://github.com/MetaMask/ai-analyzer) mode that reviews **only the modified Jest unit test files** in a PR for known flaky-test patterns (J1–J10 from the [`flaky-test-detection`](https://github.com/MetaMask/skills/blob/main/domains/coding/skills/flaky-test-detection/skill.md) skill) and emits structured findings with educational fix suggestions. |
| 4 | + |
| 5 | +Consumed by [`.github/workflows/flaky-unit-test-detection.yml`](../../../.github/workflows/flaky-unit-test-detection.yml). Not a shipped built-in — this is a Tier 3 fully-custom mode defined entirely in this repo. |
| 6 | + |
| 7 | +## Files |
| 8 | + |
| 9 | +| File | Purpose | |
| 10 | +| ---------------------- | -------------------------------------------------------------------------------- | |
| 11 | +| `mode.yaml` | Mode identity — `id`, `finalizeToolName`, `outputFile` | |
| 12 | +| `system-prompt.md` | AI role, tool-use guidance, and pattern list (uses `{{template_vars}}`) | |
| 13 | +| `task-prompt.md` | Per-run instructions, receives `{{changed_files}}` | |
| 14 | +| `finalize-schema.json` | JSON Schema the AI must satisfy when calling `finalize_flaky_unit_test_analysis` | |
| 15 | +| `fallback.json` | Deterministic `conservative` / `empty` results when the AI can't complete | |
| 16 | + |
| 17 | +The J1–J10 pattern reference is **not** duplicated here. It is loaded on demand via the analyzer's `load_skill` tool, from a copy of `mms-flaky-test-detection` synced by `yarn skills` (CI does this with the cloud-agent bootstrap; see workflow). This keeps a single source of truth in `MetaMask/skills` and prevents content drift. |
| 18 | + |
| 19 | +## Output artifact |
| 20 | + |
| 21 | +Written to `.ai-pr-analyzer/flaky-ai-analysis.json` (relative to the workspace, per `outputFile` in `mode.yaml`). Consumed by `.github/scripts/flaky-sticky-comment.ts`. |
| 22 | + |
| 23 | +Shape (matches `finalize-schema.json`): |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "analyzedFiles": ["app/components/Views/Foo/Foo.test.tsx"], |
| 28 | + "findings": [ |
| 29 | + { |
| 30 | + "file": "app/components/Views/Foo/Foo.test.tsx", |
| 31 | + "line": 42, |
| 32 | + "patternId": "J1", |
| 33 | + "patternName": "Missing act() on async state update", |
| 34 | + "severity": "critical", |
| 35 | + "snippet": "refreshControl.props.onRefresh();", |
| 36 | + "explanation": "Async prop callback triggers a state update outside act(), causing an intermittent race.", |
| 37 | + "suggestedFix": "await act(async () => { await refreshControl.props.onRefresh(); });", |
| 38 | + "historicalHintUsed": true |
| 39 | + } |
| 40 | + ], |
| 41 | + "confidence": 78, |
| 42 | + "reasoning": "1 of 1 analyzed file contains a J1 pattern matching its historical failure signature." |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +`findings: []` with `analyzedFiles` populated means "reviewed, nothing found" — Stage 3 (sticky comment) uses that distinction for its four-state logic (create / update / no-op / all-clear). |
| 47 | + |
| 48 | +## Editing the mode |
| 49 | + |
| 50 | +- **Prompt tweaks**: edit `system-prompt.md` / `task-prompt.md`. The analyzer supports `{{prompt_context}}`, `{{changed_files}}`, `{{tools_section}}`, `{{skills_section}}`, `{{max_iterations}}`, `{{finalize_tool_name}}` and more — see [MetaMask/ai-analyzer docs/adding-a-new-mode.md](https://github.com/MetaMask/ai-analyzer/blob/v1/docs/adding-a-new-mode.md). |
| 51 | +- **Schema changes**: keep `finalize-schema.json` and `fallback.json` in sync (both must satisfy the same shape). Any downstream consumer (the sticky-comment script) must be updated too. |
| 52 | +- **Pattern reference changes**: do **not** edit `.ai-pr-analyzer/skills/mms-flaky-test-detection.md` — it is generated. Edit the source at [`MetaMask/skills/domains/coding/skills/flaky-test-detection/skill.md`](https://github.com/MetaMask/skills/blob/main/domains/coding/skills/flaky-test-detection/skill.md) and re-run `yarn skills`. |
0 commit comments