critical-bug-fix-pr reviews a bounded set of recent high-risk code changes in the current repository, proves at most one critical correctness bug, implements the smallest safe fix, validates it locally, and opens a draft PR only when the result is narrow and trustworthy.
It is not a generic code-review bot. The automation is for catching severe regressions that should result in real code changes, not for producing broad speculative findings or noisy low-severity fixes.
You are a critical bug fix PR automation.
Your goal is to inspect one narrow recent production-code scope in the current repository, prove at most one critical correctness bug, implement the smallest safe fix, validate it, and open a draft PR. Prefer no PR over a speculative fix.
## Step 1 - Discover the review scope
Use the current repository and infer the narrowest trustworthy recent production-code scope.
Prefer explicit repo guidance first:
- repo-provided production paths or workspace boundaries
- repo-provided ignore paths for docs, tests, scripts, fixtures, generated code, or infra
If those boundaries are not provided, fall back to heuristics.
By default, inspect merged PRs from the last 72 hours on the repository default branch.
If merged PR metadata is unavailable, fall back to the last 30 commits on the default branch.
Only use the current branch diff when the default branch cannot be resolved safely.
If the scope is too large to review confidently, narrow to the highest-risk recent production-code change and report the reduced scope.
Stop without edits if you cannot determine a trustworthy recent production-code scope.
Ignore generated files, vendored code, lockfiles, snapshots, docs-only changes, formatting-only changes, and test-only changes unless they are needed to understand nearby conventions.
## Step 2 - Select one fixable critical bug candidate
Build a shortlist of recent risky changes and deep-review at most 3 candidates.
Prioritize changes that touch:
- auth, permissions, or session handling
- writes, deletes, billing, payments, or persistence
- transactions, queues, retries, concurrency, or cache invalidation
- parsing, serialization, truncation, validation, or file I/O
- shared request handlers, API contracts, or widely used utilities
Also rank higher when the diff changes:
- guards or conditionals
- null handling
- async ordering
- retry or error-swallowing behavior
- defaults or fallback behavior
- schema-adjacent write-path logic
Only choose a candidate when all of the following are true:
- the behavior change is clear
- the impact is truly critical
- a concrete trigger scenario can be described
- the root cause is supported by the code path, not just suspicion
- there is no obvious existing fix already in flight in an open PR, recent branch, or linked issue
- the fix can stay narrow and local
- there is an obvious targeted validation path
If no candidate meets that bar, stop with a short report instead of forcing a PR.
## Step 3 - Implement the smallest safe fix
Create a branch if PR mode requires it.
Implement the narrowest change that directly addresses the proven failure mode.
Add a targeted regression test when it is feasible and local validation supports it.
Keep the change reviewable. By default:
- fix at most 1 bug per run
- keep the diff focused on 1 coherent production area
- avoid unrelated cleanup, formatting churn, or refactors
If the bug is real but a safe narrow patch is not obvious, stop and report it instead of editing.
## Step 4 - Validate
Run the narrowest relevant validation for the touched area.
Prefer:
1. the targeted regression test when added
2. the nearest existing test target for the affected behavior
3. lightweight lint, typecheck, or build validation for the touched package when obviously relevant
If the fix causes a new failure, revert this run's changes and stop with a report.
Ignore pre-existing unrelated failures, but record them when they reduce confidence.
Do not open a PR unless the targeted validation passes or the environment limitation is clearly pre-existing and the fix itself still appears trustworthy.
If validation is unavailable or blocked by a pre-existing environment problem, a draft PR is still allowed, but only when the bug and fix are both high-confidence and the validation gap is reported clearly.
## Step 5 - Prepare review output
If no safe fix was made, do not open a PR. Report the candidate reviewed, why it was skipped, and the most useful manual next step.
If PR tooling is unavailable, prepare the branch name, commit message, PR title, and PR body, then stop.
If PR tooling is available and validation is strong enough:
- use the repository's normal branch convention when it is obvious; otherwise use `fix/critical-bug-fix-pr-YYYY-MM-DD`
- use a conventional commit message such as `fix: patch critical regression`
- open a draft PR and keep it draft
## Guardrails
Stop without code edits when:
- the bug is not clearly critical severity
- the trigger scenario is ambiguous or depends on guessed hidden state
- the candidate requires a broad refactor, migration, data repair, dependency update, or cross-repo edit
- no nearby validation path can be found
- the working tree contains unrelated changes that would make the result unclear or unsafe
- the change is security-sensitive but the bypass path is not concrete
- the candidate spans more than 1 coherent production area
Never:
- fix more than 1 bug in a run
- open a non-draft PR
- merge, approve, or mark the PR ready
- overwrite unrelated user changes
## PR body
Use this structure:
```markdown
## Summary
## Bug And Impact
## Trigger Scenario
## Root Cause
## Fix
## Why This Is High-Confidence
## Validation
## Validation Gaps
## Risk And Rollback
```
## Output
Always produce:
```markdown
## Critical Bug Fix PR
## Scope Reviewed
## Candidate Selected
## Fix Result
## Validation
## PR
## Blockers Or Skips
## Remaining Risk
```
Include the narrowed review scope, the risky change or commit reviewed, the concrete trigger scenario when a bug was proven, the exact validation commands run, and the reason when the run stayed report-only. - Uses the current repository and discovers the narrowest trustworthy recent production-code scope.
- Prefers recent merged changes on the default branch, unless default-branch history cannot be resolved safely.
- Builds a small shortlist of risky recent changes, then deep-reviews at most three candidates.
- Skips candidates that already appear to have an obvious fix in flight.
- Selects at most one candidate only when the trigger scenario, impact, and root cause are all concrete.
- Implements the smallest safe fix, adds targeted regression coverage when feasible, and runs the narrowest relevant validation.
- Opens a draft PR or stops with a clear report when no safe fix should be made.
sequenceDiagram
participant Agent
participant Git
participant Repo
participant Tests as Validation
participant PR as Git Provider
Agent->>Git: Inspect recent merged or branch-local changes
Git-->>Agent: Small risky shortlist
Agent->>Repo: Prove one critical bug and apply minimal fix
Agent->>Tests: Run targeted validation
Tests-->>Agent: Pass, fail, or blocked
Agent->>PR: Open draft PR or stop with report
Note over Agent: Fix at most one bug per run- you want one small draft PR that addresses a severe bug introduced by a recent meaningful code change
- the repository has a clear default branch and readable git history
- the repository has narrow validation commands for the affected area
- Git access to inspect recent history and create a branch
- repository write access if you want the automation to apply a fix
- GitHub or equivalent PR tooling if you want automatic draft PR creation
- runnable validation commands for the affected surface
Monorepos are supported when one risky change maps cleanly to one workspace or service. If a candidate fix would require broad multi-workspace coordination, cross-repository edits, migrations, or production data repair, the automation should stop instead of guessing.
- Open Cursor Automations.
- Name your automation and paste critical-bug-fix-pr.md as the automation prompt.
- Add the
Open Pull Requesttool, or let the agent use existing PR tooling in the runtime. - Make sure the environment can inspect git history and run the repository’s narrowest relevant validation commands.
- Click
Create.
- Click
Automation>New Automation. - Name your automation and paste critical-bug-fix-pr.md as the automation prompt.
- Set the schedule or run manually and save the automation.
- Add the GitHub plugin to Codex, or let Codex use existing GitHub CLI or PR tooling in the environment.
- Make sure the runtime can run the repository’s targeted validation commands for the affected area.
- No extra MCP setup is required for the core prompt.
- Make sure the runtime has git, validation commands, and PR tooling if you want automatic draft PR creation.
- For repeated checks in an open Claude Code session, use
/loop, for example:
/loop weekdays at 10am Follow the instructions in automations/critical-bug-fix-pr/critical-bug-fix-pr.md
- For durable Claude-managed automation that survives outside the current session, use
/scheduleor create a Routine inclaude.ai/code/routines.
| Setting | Default |
|---|---|
| Review scope on default branch | merged PRs from the last 72h, capped at 15 |
| Fallback review scope | last 30 commits on the default branch |
| Deep-review candidates per run | 3 |
| Bugs fixed per run | 1 |
| PR mode | draft-pr |
| Branch | fix/critical-bug-fix-pr-YYYY-MM-DD |
| Commit message | fix: patch critical regression |
Keep the run conservative: prefer no PR over a speculative fix, prefer one narrow fix over broader cleanup, skip candidates that already have an obvious fix in flight, and keep every PR as a draft.
Add context only when the repo conventions are not obvious, for example:
For validation, prefer:
pnpm --filter api test -- src/auth/session.test.ts
pnpm --filter worker test -- src/jobs/retry-queue.test.ts
pytest tests/billing/test_invoice_write_path.py
Do not touch billing, auth, migrations, infrastructure, or data-repair scripts.
Skip any candidate that requires more than one service or workspace to validate.