An autonomous AI agent that automatically detects, analyzes, and fixes failing Playwright tests in GitHub Actions workflows. This agent uses OpenAI's GPT models to understand test failures, generate targeted fixes, verify them through CI, and create draft pull requestsβall without human intervention.
# Install dependencies
pip install -r requirements.txt
# Set up environment variables
export GITHUB_TOKEN="ghp_your_github_token"
export OPENAI_API_KEY="sk_your_openai_key"
# Run the agent
python main.py https://github.com/owner/repo/actions/runs/12345678- π Automatic Failure Detection: Monitors GitHub Actions workflows and identifies all test failures
- π§ AI-Powered Analysis: Uses OpenAI to analyze failure logs and understand root causes
- π οΈ Surgical Code Fixes: Generates precise, targeted fixes for failing tests
- π Self-Healing Loop: Automatically retries fixes up to 3 times, learning from previous failures
- β CI Verification: Creates branches and validates fixes through GitHub Actions
- π Draft PR Creation: Automatically creates draft PRs with verified fixes
- π’ Monorepo Support: Handles complex monorepo structures with path normalization
- π― Multi-Issue Resolution: Processes multiple distinct failures independently
βββββββββββββββββββ
β GitHub Actions β
β (Failed Run) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β GitHubClient βββββ Fetches logs, metadata, diffs
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β LogProcessor βββββ Extracts relevant failure info
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β AIAgent βββββ Analyzes & generates fixes
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Self-Healing β
β Loop β
βββββββββββββββββββ€
β 1. Generate Fix β
β 2. Create Branchβ
β 3. Push Changes β
β 4. Run CI β
β 5. Verify β
β 6. Retry if β β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Draft PR β
β β
Success β
βββββββββββββββββββ
- Python 3.8+
- GitHub Personal Access Token with appropriate permissions:
repo(full control)workflow(to trigger workflows)
- OpenAI API Key
-
Clone the repository:
git clone <repository-url> cd playwright-ai-agent
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment variables: Create a
.envfile in the root directory:GITHUB_TOKEN=ghp_your_github_token_here OPENAI_API_KEY=sk-your_openai_api_key_here
-
Run the agent with a failed workflow URL:
python main.py https://github.com/owner/repo/actions/runs/12345678
-
Monitor the process: The agent will:
- π Identify all distinct failures
- π οΈ Process each issue independently
- π Retry failed fixes up to 3 times
- β Create draft PRs for successful fixes
playwright-ai-agent/
βββ main.py # Entry point and orchestration logic
βββ requirements.txt # Python dependencies
βββ .env # Environment variables (create this)
βββ agent/
βββ __init__.py
βββ ai_agent.py # OpenAI integration for analysis & fix generation
βββ github_client.py # GitHub API wrapper
βββ log_processor.py # Log parsing and cleanup
βββ models.py # Pydantic models for structured data
Adjust the maximum retry attempts in main.py:
MAX_RETRIES = 3 # Number of fix attempts per issueConfigure the OpenAI model in agent/ai_agent.py:
self.model = "gpt-5-mini" # or "gpt-4", "gpt-4-turbo", etc.Modify the CI verification timeout in main.py:
timeout = 900 # 15 minutes (in seconds)The agent parses GitHub Actions logs and identifies distinct test failures with:
- File path and line number
- Error category (timeout, assertion, etc.)
- Stack traces and error messages
Using OpenAI, the agent:
- Analyzes the failure context
- Reviews the original code
- Generates a surgical fix following best practices
- Avoids generic solutions (e.g., blanket
waitForTimeoutcalls)
For each fix:
Generate Fix β Create Branch β Push Code β Trigger CI β Wait
β
βΌ
Success?
β β
Yes No
β β
Draft PR Retry
(with error context)
The agent automatically normalizes file paths:
- Log path:
tests/login.spec.ts - Actual path:
packages/playwright/tests/login.spec.ts
π€ Starting Agent for Run: 21857019114 (push)
π Identifying all distinct failures...
π Found 2 issues to address.
π οΈ Processing Issue in: tests/auth/login.spec.ts
π§ Attempt 1/3: Generating surgical fix...
π§ͺ Waiting for CI verification on branch: ai-fix-a3b92f...
β³ CI still in progress... (30s)
β³ CI still in progress... (60s)
β
SUCCESS: Fix verified by CI.
π Draft PR Created: https://github.com/owner/repo/pull/123
π οΈ Processing Issue in: tests/checkout/payment.spec.ts
π§ Attempt 1/3: Generating surgical fix...
π§ͺ Waiting for CI verification on branch: ai-fix-c7d45e...
β FAIL: Attempt 1 did not resolve the issue.
π Fetching new logs for re-analysis...
π§ Attempt 2/3: Generating surgical fix...
...
- Draft PRs: All PRs are created as drafts to allow human review before merging
- Branch Isolation: Each fix gets a unique branch to avoid conflicts
- CI Gating: Fixes are only proposed after passing CI verification
- Retry Logic: Failed fixes are retried with additional context from new logs
- Monorepo Aware: Handles complex repository structures
- Never commit
.envfile to version control - Use GitHub tokens with minimal required permissions
- Rotate API keys regularly
- Review all draft PRs before merging
- Consider rate limiting for API calls
Contributions are welcome! Areas for improvement:
- Support for additional test frameworks (Jest, Cypress, etc.)
- Parallel processing of multiple failures
- Cost tracking for OpenAI API calls
- Webhook-based automatic triggering
- Enhanced diff analysis for regression detection
- Support for flaky test identification
[Add your license here]
- Built on OpenAI's GPT models
- Utilizes GitHub REST API
- Designed for Playwright test automation
Note: This agent uses AI to generate code fixes. Always review generated changes before merging to production.