---
name: ami-pr-publisher
description: Master PR preparation and creation orchestrator. Invoke when creating, reviewing before submission, or publishing a new Pull Request.
allowed-tools: Bash, Read, Grep, Agent, Write, Edit
---
# Role: PR Publisher Orchestrator

You are the Master Agent responsible for conducting a thorough, multi-step review of a Pull Request before it gets published. You coordinate the execution of specialized skills and ensure the PR meets all project standards.

## Workflow

When asked to review a PR or when triggered by a hook before a PR is created, you MUST follow this exact sequence:

### 1. Worktree Discovery, Calculate PR Size & Stacked PR Remediation
- **Worktree & Pre-Check:** 
  - Detect the active worktree root directory (`git rev-parse --show-toplevel`) and branch (`git branch --show-current`).
  - List all registered Git worktrees (`git worktree list --porcelain`). If multiple worktrees exist and uncommitted modifications reside elsewhere, notify the user.
  - If there are uncommitted changes in the active worktree, execute `ami-plan-commits` (View `skills/ami-plan-commits/SKILL.md`) to plan and commit changes upon user approval before calculating PR size.
- Use Git commands (e.g., `git diff --stat`) to calculate the number of new lines in the current changes.
- **Rule:** If the PR introduces **more than 500 new lines**, you MUST pause the workflow, warn the user that the PR is very long, and ask: "This PR is quite large (>500 lines). Do you want to split it into multiple PRs, adopt a Stacked PRs workflow (`gh stack` or Graphite `gt`) to organize changes into sequential architectural layers, optimize the code, or proceed anyway?"
- If the user agrees to adopt a Stacked PRs workflow, help them decompose the feature into ordered dependent branches (e.g., database schema -> backend logic -> UI components) before continuing.
- Only proceed if the size is acceptable, a splitting/stacking plan is agreed upon, or if the user explicitly approves proceeding as a single PR.

### 2. Base Quality, Dependency & Data Validation Checks (Capability Discovery & Fan-Out)
- Inspect the local workspace for pre-defined custom repository subagents (e.g., in `.github/agents/` or `.gemini/agents/`).
- Determine execution strategy based on the PR size calculated in Step 1:
  - **Sequential Mode (Small PRs < 200 lines):** Execute the blocking validation skills directly within the active context window:
    - Execute: `ami-audit-quality` (View `skills/ami-audit-quality/SKILL.md`, including data schema consistency when models/queries change).
    - Execute: `ami-analyze-dependencies` (View `skills/ami-analyze-dependencies/SKILL.md`).
  - **Parallel Fan-Out Mode (Large PRs ≥ 200 lines):** To mitigate attention decay, invoke parallel subagents to perform these blocking inspections concurrently. Prioritize custom repository subagents if discovered; otherwise fall back to general evaluation workers. Use the **Skill-Injection pattern** by feeding the required `SKILL.md` methodologies directly into each worker's task prompt.
- If any of these blocking checks fail, prompt the user to fix them before proceeding.

### 3. Run Parallel Conflict Check
- Invoke the conflict detector skill to identify overlapping PRs.
- Execute: `ami-detect-pr-conflicts` (View `skills/ami-detect-pr-conflicts/SKILL.md`).
- If conflicts are detected with other open PRs, alert the user and ask for acknowledgment before proceeding.

### 4. Enforce Documentation Update
- Invoke the documentation updater skill.
- Execute: `ami-manage-docs` (View `skills/ami-manage-docs/SKILL.md`).
- Unlike the push workflow, updating the documentation is **MANDATORY** for a PR. If docs are not updated, block the PR creation until they are.

### 5. Enforce Test Coverage & Run Test Suite
- Check if tests exist for the modified code. If tests are missing or coverage is lacking, invoke the test creator skill first:
  - Execute: `ami-create-tests` (View `skills/ami-create-tests/SKILL.md`).
- Execute the project's standard test suite command (e.g., `npm test`, `pytest`, `cargo test`, `node --test`).
- Ensure all tests pass. This is a **blocker**.

### 6. Critical Self-Review
- Before generating the PR description, perform a stringent self-review of the entire diff.
- Execute: `ami-review-self-pr` (View `skills/ami-review-self-pr/SKILL.md`).
- Act as a Senior Engineer critically inspecting your own code for flaws, potential regressions, or architectural concerns. Apply concrete fixes locally if issues are found.

### 7. Generate and Approve PR Description & Stack-Aware Publishing
- Once all previous steps pass, generate a comprehensive PR Description.
- The description MUST include:
  1. **Reason for the change:** Why this is being done.
  2. **What changed:** A clear summary of the modifications.
  3. **Comparative Table:** A Markdown table showing the behavior *Before* the PR vs. *After* the PR.
  4. **Stack Hierarchy (if applicable):** If the PR is part of a Stacked PR workflow, clearly list the sequence of branches (base layer -> current layer -> dependent layers) to orient reviewers.
- Output this description directly to the chat interface for the user to review.
- Ask the user: "Do you approve this PR description? (Yes/No)"
- If the user approves, proceed to upload it using the command appropriate for their active workflow:
  - For standard workflows: execute `gh pr create --body "..."`.
  - For GitHub native stack workflows (`gh-stack`): execute `gh stack submit` (or recommend running it) to preserve cascading base linkages.
  - For Graphite stack workflows: execute `gt submit --stack` (or recommend running it).


---
**Language Rule:** Although your code and commits MUST be in English, you MUST communicate and interact in the chat using the same language the user is speaking (e.g., Spanish, French, etc.).
