Skip to main content
Glama
README.md
# Auto-Heal-MCP

**Trust-Gated Playwright Failure Arbiter & Deterministic MCP Server**

Auto-Heal-MCP is an automated, safety-critical test repair engine and Model Context Protocol (MCP) server for Playwright suites. Rather than blindly patching broken tests or modifying locators at runtime, Auto-Heal-MCP deterministically classifies failures, halts on genuine application bugs and assertion mismatches, preserves test semantics via TypeScript AST invariants, verifies candidates against dependent blast-radius specs, and outputs auditable unified `.patch` files.

---

## Architecture Pipeline

```text
                                  Playwright Test Failure
                                             │
                                             ▼
                             ┌───────────────────────────────┐
                             │    Forensic Custom Reporter   │
                             │ (ARIA Snapshot, Console, Net) │
                             └───────────────┬───────────────┘
                                             │
                                             ▼
                             ┌───────────────────────────────┐
                             │       Security Redactor       │
                             │ (Tokens, Cookies, Auth, PII)  │
                             └───────────────┬───────────────┘
                                             │
                                             ▼
                             ┌───────────────────────────────┐
                             │  Deterministic Classifier     │
                             │ (Net 500, Page Crash, Assert) │
                             └───────┬───────────────┬───────┘
                                     │               │
                        APP_BUG /    │               │ LOCATOR_DRIFT
                        ASSERTION    ▼               ▼
                       ┌────────────────┐ ┌───────────────────────────────────┐
                       │  HALT HEALING  │ │ Single-Shot Ollama / ARIA Healer  │
                       │ (0% False-Grn) │ │ (qwen2.5-coder:7b / Semantic ARIA)│
                       └────────────────┘ └─────────────────┬─────────────────┘
                                                            │ Candidate Locator
                                                            ▼
                                          ┌───────────────────────────────────┐
                                          │ TypeScript AST Multi-Gate Guard   │
                                          │ (Syntax, Assertion Multiset, No   │
                                          │ Skip, No XPath/Positional Locators│
                                          └─────────────────┬─────────────────┘
                                                            │ Verified AST
                                                            ▼
                                          ┌───────────────────────────────────┐
                                          │ Safe In-Memory Blast-Radius Check │
                                          │ (Dependent Specs, try/finally disk│
                                          │ restoration, silent stdio pipe)   │
                                          └─────────────────┬─────────────────┘
                                                            │ All Specs Green
                                                            ▼
                                          ┌───────────────────────────────────┐
                                          │ Emit Unified .patch Diff          │
                                          │ & Expose via Deterministic MCP    │
                                          └───────────────────────────────────┘
```

---

## Key Engineering Trade-offs

1. **Native Telemetry & ARIA Snapshot over `trace.zip`**:
   Extracting multi-megabyte Playwright trace zip archives requires unzipping and heavy binary parsing. Auto-Heal-MCP uses an official custom `Reporter` and test fixture that extracts real-time accessible trees (`page.locator('body').ariaSnapshot()`), console errors, and first-party HTTP responses with zero external dependencies and sub-millisecond overhead.
2. **Deterministic, Zero-LLM MCP Server**:
   The MCP server (`src/mcp/server.ts`) does **not** call any LLM internally. AI agent clients (such as OpenCode, Antigravity, or Claude Code) perform high-level orchestration, while the MCP server exposes pure, deterministic, sandboxed tools for forensics retrieval, policy classification, AST validation, and blast-radius verification.
3. **TypeScript AST Multiset Assertion Preservation**:
   Regex-based code checks are easily tricked. Auto-Heal-MCP parses TypeScript source into an Abstract Syntax Tree using the official TypeScript Compiler API, tracks the multiset counts of all outer `expect(...)` call chains, forbids assertion deletion, prevents weakening assertion expected values (e.g., `'$99.00'` to `'$199.00'`), blocks test skipping (`test.skip`), and rejects brittle positional/XPath locators.
4. **In-Memory `try/finally` Restoration over Git Worktrees**:
   Git worktrees on Windows often hit file locking, symlink permission issues, and index contention. Auto-Heal-MCP verifies candidates in-place inside a strict `try { write(patched) } finally { write(original) }` block, guaranteeing byte-for-byte original file restoration on disk even if test execution crashes or aborts.
5. **Auditable Unified Diffs over Runtime Monkey-Patching**:
   Runtime locator healing masks underlying regressions and prevents CI reviewers from auditing code changes. Auto-Heal-MCP emits standard, reviewable unified `.patch` files in `.veriheal/patches/` that can be inspected, committed, or rejected by human developers.

---

## 8-Scenario Chaos Benchmark Scorecard

Auto-Heal-MCP was evaluated against an 8-scenario controlled chaos checkout suite simulating real-world UI refactors and P0 application regressions.

The table below reflects real, verified terminal output from `npm run eval:offline` (with `process.env.VERIHEAL_OFFLINE = '1'`):

| Scenario | Category | Classifier Verdict | Outcome Status | Mode | Blast Specs Verified | Duration | Result |
| :--- | :--- | :--- | :--- | :--- | :--- | :---: | :---: |
| `rename-testid` | `UI_REFACTOR` | `LOCATOR_DRIFT` | `HEALED` | `deterministic-fallback` | 2 specs | 13,891 ms | **PASS** |
| `rename-button` | `UI_REFACTOR` | `LOCATOR_DRIFT` | `HEALED` | `deterministic-fallback` | 2 specs | 11,800 ms | **PASS** |
| `change-input-id` | `UI_REFACTOR` | `LOCATOR_DRIFT` | `HEALED` | `deterministic-fallback` | 2 specs | 11,561 ms | **PASS** |
| `wrap-dom` | `UI_REFACTOR` | `LOCATOR_DRIFT` | `HEALED` | `deterministic-fallback` | 2 specs | 11,814 ms | **PASS** |
| `api-500` | `APP_BUG` | `APP_BUG` | `BLOCKED_BY_CLASSIFIER` | `none` | none | 7,879 ms | **PASS** |
| `delete-pay-btn` | `APP_BUG` | `LOCATOR_DRIFT` | `NO_CANDIDATE` | `deterministic-fallback` | none | 7,294 ms | **PASS** |
| `js-crash` | `APP_BUG` | `APP_BUG` | `BLOCKED_BY_CLASSIFIER` | `none` | none | 7,239 ms | **PASS** |
| `wrong-total` | `APP_BUG` | `ASSERTION_MISMATCH` | `BLOCKED_BY_CLASSIFIER` | `none` | none | 7,441 ms | **PASS** |

### Summary Metrics
- **Refactors Healed**: **4 / 4 (100.0%)** (all valid UI refactors repaired and verified against dependent specs)
- **P0 Bugs Blocked**: **4 / 4 (100.0%)** (zero attempts to heal broken payment gateways, crashes, or mismatched totals)
- **False-Green Rate**: **0.0% (0 / 4)** (strict gate met: not a single regression was falsely masked)
- **Overall Accuracy**: **100.0%**
- **Disk File Integrity**: **PASSED (Zero disk drift)**

*(Note: When local Ollama is active with `qwen2.5-coder:7b`, UI refactors heal via `ollama-live` mode; under CI or when Ollama is offline, the deterministic semantic ARIA matcher resolves 100% of candidate locators without network).*

---

## MCP Server Tools Reference

The MCP Server communicates over `stdio` without external API keys or nested LLM calls.

| Tool Name | Input Schema | Description |
| :--- | :--- | :--- |
| `get_latest_forensics` | `{}` | Retrieves and parses `.veriheal/latest-forensics.json` (redacted errors, network failures, console logs, and ARIA snapshots). |
| `classify_failure` | `{ bundle: ForensicBundle }` | Pure deterministic classifier assigning failure categories (`APP_BUG`, `ASSERTION_MISMATCH`, `LOCATOR_DRIFT`, `UNKNOWN`). |
| `validate_ast_patch` | `{ originalSource, patchedSource, fileName? }` | Runs TypeScript Compiler API AST invariant validation (syntax, multiset assertion preservation, skip prevention). |
| `verify_patch_blast_radius` | `{ targetFile, oldSnippet, newSnippet, chaosMode?, emitPatch? }` | Safely validates patch against path traversal, tests across all dependent specs in-place, and restores original files. |

### Connecting via OpenCode (`opencode.json`)
```json
{
  "$schema": "https://opencode.ai/config.json",
  "mcp": {
    "auto-heal-mcp": {
      "type": "local",
      "command": ["npx", "tsx", "src/mcp/server.ts"],
      "enabled": true
    }
  }
}
```

---

## Quickstart & Verification Commands

### Prerequisites
- Node.js 22+
- npm 10+
- (Optional) [Ollama](https://ollama.com/) running `qwen2.5-coder:7b` locally for live LLM healing.

### Installation
```bash
# Clone the repository and install dependencies
npm ci

# Install Playwright browser binaries
npx playwright install chromium
```

### Running the Test Suite
```bash
# 1. Verify TypeScript types
npm run typecheck

# 2. Run core unit tests (redactor & classifier)
npm run test:unit

# 3. Run AST guardrail & unified diff tests
npm run test:guardrail

# 4. Run baseline Playwright specs (100% passing baseline)
npm run test:baseline

# 5. Run end-to-end MCP stdio verification (all 4 tools tested over stdio)
npm run test:mcp

# 6. Run the full 8-scenario Chaos Benchmark (Offline Mode)
npm run eval:offline

# 7. Run the full 8-scenario Chaos Benchmark (Online Ollama Mode)
npm run eval
```

---

## Scope & v2 Roadmap

- **v1 Focus**: Page Object Model (POM) locator healing, web-first ARIA matching, AST assertion invariants, single-file blast-radius verification.
- **v2 Roadmap**:
  - Multi-file atomic patch verification (refactors touching both POM and test specs simultaneously).
  - Visual regression gate using pixelmatch / Playwright visual snapshot diffs.
  - Bidirectional git PR integration and automated PR comments.

---

## License

This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.