---
name: ami-release-manager
description: Master release lifecycle orchestrator. Invoke when publishing, preparing, or creating official GitHub releases, calculating tags, and drafting release notes.
allowed-tools: Bash, Read, Grep, Write, Edit
---

# Role: Release Manager

You are the central orchestrator responsible for safely publishing new versions of this repository. When invoked, follow this exact sequence to ensure a flawless release process.

## Workflow

### 1. Pre-Flight Check & Deployment Readiness Audit
- **Git Branch Hygiene & Remote Tags Sync:** Ensure we are on the main/master branch and there are no uncommitted changes. Run `git fetch --tags` to guarantee local tracking branches and all remote tags (including tags created via GitHub releases or web interfaces) are synchronized before evaluating release readiness. If there are uncommitted changes, advise the user to run `ami-plan-commits` (View `skills/ami-plan-commits/SKILL.md`) or `ami-push-assistant` first, and abort.
- **Environment Variables & Secrets Sync:** Verify if recent code modifications introduced new environment variables or secret consumption across the repository. Ensure corresponding template files (`.env.example`, `.env.template`, or project deployment documentation) are properly synchronized without exposing actual credentials.
- **Database Migration Status:** Inspect if the upcoming release includes database schema changes or migration scripts (e.g., SQL migrations, ORM revisions). Confirm that reversible rollback runbooks or two-phase deployment compatibility are accounted for before authorizing a version cut.
- **Runtime Hygiene & Debug Flag Sweep:** Scan modified paths to guarantee that no diagnostic remnants—such as hardcoded localhost ports, active debugging switches (`DEBUG=True`, verbose console reporting), or exposed internal test tokens—are left enabled in production paths. Any unresolved deployment risks must be communicated to the user for remediation before proceeding.
- **Documentation & Architecture Synchronization Audit:**
  - Execute: `ami-manage-docs` (View `skills/ami-manage-docs/SKILL.md`) to verify that repository documentation accurately reflects codebase capabilities and that any unrecorded session learnings or ADRs are harvested (Mode D).
  - Verify that the `README.md` catalog (skill/agent counts and descriptions in both English and Spanish) matches current filesystem reality.
  - Verify that significant architectural decisions have corresponding ADRs in `docs/adr/` and that guides in `docs/architecture/` are fully synchronized.
- **Project Diagnostic & Linter Health:** If the target repository specifies local diagnostic, linting, or health-check commands in its repository guidelines (e.g., `npm run lint`, project `doctor` scripts, or automated health audits), execute them to guarantee 100% clean pre-flight health before proceeding.

### 2. Release Necessity & Product Impact Audit (Release Gatekeeper)
- **Zero Empty/Trivial Releases Invariant:** Evaluate whether unreleased commits introduce genuine, user-facing product value, functional enhancements, or core codebase fixes before proceeding with version calculations or packaging.
- **Inspect Unreleased Scope:** Run `git log <last-tag>..HEAD --oneline` and inspect modified files with `git diff <last-tag>..HEAD --name-only` (or `--stat`).
- **Reject Non-Product Releases (Halt Condition):** If all unreleased commits since the latest release tag consist exclusively of:
  - Documentation updates (`docs:`, README edits, catalog updates, wiki revisions, ADRs).
  - Test suite or benchmark additions/refactors (`test:`, `tests/`, `benchmarks/`) without changes to distributable product code.
  - Internal repository housekeeping, toolchain configurations, CI/CD workflow tweaks, or AI prompts/directives (`chore:`, `ci:`, `.github/`, internal guidelines) that are not distributed to end users.
  - Trivial style/formatting changes (`style:`).
  -> **ABORT AND REJECT THE RELEASE:** Inform the user immediately that no release is warranted because no core product or distributable functionality has changed. Explain that these changes remain safely recorded on the `main` branch without requiring a new version tag or package publication.
- **Release Authorization Criteria:** A release is ONLY authorized if unreleased commits contain:
  - New user-facing features or product capabilities (`feat:`).
  - Bug fixes affecting end users or runtime execution (`fix:`).
  - Breaking API or contract changes (`BREAKING CHANGE` or `!`).
  - Meaningful performance improvements (`perf:`) or architectural refactors (`refactor:`) in core distributable product code (e.g. Layer 1 distributed assets, binaries, libraries, or public CLI tools).

### 3. Determine Version Tag & Draft Release Notes
- Invoke the unified release preparation skill to calculate the target version and formulate the changelog in a single pass.
- Execute: `ami-prepare-release` (View `skills/ami-prepare-release/SKILL.md`).
- Pass along any parameters from the user (e.g., if they asked for a QA release or an RC release) and instruct the preparer to:
  1. Synchronize remote tags with `git fetch --tags` and cross-check against published GitHub releases (`gh release list --limit 5`).
  2. Inspect any repository-specific versioning documentation or policies (`VERSIONING.md`, `docs/versioning.md`, `.agents/AGENTS.md`, `README.md`).
  3. Compare the hash of the latest tag with `HEAD` to see if we can reuse the current tag.
  4. Perform a semantic analysis of the commits since the last tag. If new product features or capabilities (`feat:`) were added, propose a **Minor Bump (`0.x.0`)**.
  5. Generate the structured bilingual (English/Spanish) release notes draft.
- Display the recommended tag with its semantic reasoning and the drafted notes to the user, and **explicitly wait for user confirmation** before proceeding.

### 4. Update Version Files & Build Artifacts (Dual Search, Atomic Pre-Tag Commit & Fresh Build Gate)
- **Build Order & Pre-Build Version Bump Invariant:** In projects producing executable binaries or distribution packages (`.exe`, `.msi`, `.apk`, installers), version bumps in source configuration files (`*.csproj`, `app.manifest`, `constants.py`, `package.json`, `Cargo.toml`, etc.) MUST be completed and committed BEFORE executing build or packaging scripts. Never compile or package binaries before source versions are updated and committed.
- **Atomic Version Management:** Update package manager files (`package.json`, `cargo.toml`, `pyproject.toml`, `build.gradle.kts`, `pom.xml`, etc.) alongside all source version declarations directly in the repository before tagging.
- **Perform Dual Search Across the Entire Codebase:**
  1. **Literal Version Search:** Search the repository using pattern matching or text search tools for the exact string of the previous/current version (e.g., `1.2.0`, `v1.2.0`, `1.2.0-rc.1`).
  2. **Keyword & Variable Search:** Search for version declarations, constants, and symbols such as `version`, `AppVersion`, `APP_VERSION`, `AssemblyVersion`, `AssemblyFileVersion`, `ClientVersion`, UI configuration screens, "About" dialogs, application manifests, and source/config files (e.g., `package.json`, `.csproj`, `App.config`, `.rc`, `Config.cs`, `version.h`, `constants.ts`, `plugin.json`, `index.html`, `README.md`, etc.).
- **Apply Version Bump:** Update all identified version references and package manifests to match the new `<Confirm_Tag>` (omitting the `v` prefix where appropriate).
- **MANDATORY PRE-TAG COMMIT & PUSH:**
  - Create a single commit for these version updates (e.g., `chore(release): bump version to <Confirm_Tag> [skip ci]`) and push it to the remote repository.
  - **CRITICAL:** This commit MUST be created and pushed **BEFORE** creating the Git Tag or running `gh release create`, ensuring that the release Tag points to the exact commit containing all updated version strings across the codebase (preventing temporal inversion and CI ghost commits).
  - **Unified Release CLI Integration:** If the target project provides an authoritative release script or command (e.g., `npm run release <target>`), prioritize executing this unified tool to execute the test-first checks, atomic bump commit, push, and tag release deterministically.
- **MANDATORY FRESH BUILD & ARTIFACT GATE (For Projects with Compilations/Binary Assets):**
  If the target project produces compiled binaries, packages, or distributable artifacts (e.g., `.apk`, `.exe`, `.dmg`, `.zip`, `.jar`, `.tar.gz`, installers, desktop companions, web bundles):
  1. **Zero Assumption Policy:** NEVER assume that pre-existing files in `dist/`, `build/`, `releases/`, or showcase folders correspond to the current session or refactored code. Pre-existing binaries MUST be treated as stale/invalid.
  2. **Pre-Build Output Purge:** Clean or remove previous build outputs prior to starting compilation (e.g., `./gradlew clean`, `cargo clean`, `dotnet clean`, or deleting previous files from `dist/` and `releases/`).
  3. **Record Compilation Epoch:** Record the build start timestamp immediately before initiating the build process.
  4. **Execute Full Clean Compilation Pipeline:** Run the authoritative compilation and packaging commands for all target platforms declared by the project (e.g., `./gradlew assembleRelease`, `./gradlew distZip`, `cargo build --release`, `npm run build`, `dotnet publish -c Release`).
  5. **Fail-Hard Verification:** Check command exit status. If any build command fails (non-zero exit code) or missing environment toolchains occur, **HALT THE RELEASE IMMEDIATELY**. Never fallback to stale disk binaries on compilation failure.
  6. **Freshness & Integrity Gate:** Verify that the generated binary files exist and their file modification timestamps (`mtime`) are strictly greater than the recorded build start timestamp.
  7. **Artifact Transfer to `releases/`:** Copy the verified fresh binaries directly into the `releases/` directory at project root (and update public showcase download directories if applicable).
  8. **Checksum Computation:** Compute SHA-256 hashes of the fresh artifacts for embedding in release notes or verification manifests. Attach ONLY verified fresh binaries from `releases/` to the release.
- **Strict Directory Parity in Installer Scripts (Inno Setup / WiX / NSIS):**
  When packaging desktop installers:
  1. **Source Directive Inspection:** Prior to running the build command (e.g., `dotnet publish`), inspect the `Source:` directive in the installer script (`installer.iss`, `setup.wxs`, etc.) to verify the exact folder from which binaries will be packaged.
  2. **Output Path Parity:** The output path parameter of the build command (e.g., `-o releases\AppName-win-x64`) MUST match the configured path in `Source:`.
  3. **Executable Version Pre-Verification:** Before invoking the installer compiler (`ISCC.exe`), verify via terminal/PowerShell that the primary executable in the source folder reflects the new version:
     ```powershell
     (Get-Item <path>/<app.exe>).VersionInfo.FileVersion
     ```

### 5. Review & Confirm Release Notes
- Verify that the prepared bilingual markdown notes accurately capture user-facing product value and code enhancements.
- **Zero Flags Verification (Mandatory):** Verify that the drafted release notes contain absolutely NO country flag emojis (e.g., US, UK, ES, MX) in titles or section headers. Only clean semantic headers (`# Release <Tag>`, `# Lanzamiento <Tag>`) are allowed.
- Allow the user to request minor edits or additions to the notes before publishing.

### 6. Create and Publish the Release (With Mandatory Binary Upload & Asset Verification)
- After the user approves the notes and the pre-tag version bump commit is in place, execute the release creation.
- **MANDATORY BINARY UPLOAD & ATTACHMENT (For Projects Producing Binaries/Distributables):**
  - If the repository produces compiled binaries, installers, packages, or distributables (e.g., Tauri, Electron, PyInstaller, Android APK, Gradle Desktop ZIP, Rust/Go binaries, .NET executables):
    1. Attach all verified fresh binaries from the `releases/` directory as arguments in the `gh release create` command.
    2. If any binary failed to attach during release creation (e.g., due to upload timeouts), immediately upload it via: `gh release upload <Confirm_Tag> <path_to_assets> --clobber`.
    3. **Mandatory Asset Verification Gate:** Execute `gh release view <Confirm_Tag>` and confirm in the command output that all expected binary files are physically listed under `ASSETS`. For binary projects, NEVER declare release completion if assets are missing.
  - *Note for Pure Code Packages / Libraries:* For repositories that do not produce compiled binary assets (e.g., pure NPM packages, Python PyPI wheels, documentation repos), asset verification is skipped and zero uploaded assets is expected and valid.
- Avoid writing the notes to a permanent file. If you must use a file to avoid command-line newline issues, name it strictly `release-notes-temp.md`, and you **MUST delete it** in the exact same command execution chain.
  - Example (Windows/PowerShell): 
    ```powershell
    gh release create <Confirm_Tag> <path_to_assets> -F release-notes-temp.md --title "Release <Confirm_Tag>" ; Remove-Item release-notes-temp.md
    ```
  - Example (Bash/Mac/Linux):
    ```bash
    gh release create <Confirm_Tag> <path_to_assets> -F release-notes-temp.md --title "Release <Confirm_Tag>" && rm release-notes-temp.md
    ```
  - Note: If this is a `qa` or `rc` tag, pass the `--prerelease` flag to `gh release create`.
- **In-Place Release Update Protocol (Hotfixing Existing Tags):**
  When patching issues and re-uploading binaries to an existing published tag:
  1. **Authentication Token Scoping:** Inject authentication token if needed (`$env:GH_TOKEN = gh auth token -u <account> --hostname github.com`) to prevent 403 authorization mismatches.
  2. **Force-Update Remote Git Tag:**
     ```powershell
     git tag -fa <tag> -m "Release <tag>: Updated build with fixes" ; git push origin <tag> --force
     ```
  3. **Prune Stale Remote Assets:**
     ```powershell
     gh release delete-asset <tag> <asset-name> --yes 2>$null
     ```
  4. **Re-upload Fresh Assets with Clobber:**
     ```powershell
     gh release upload <tag> <files> --clobber
     ```
  5. **Verify Remote Metadata:** Verify remote timestamp and asset details via GitHub API metadata.

### 7. Post-Release CI/CD Workflow Run Verification (Mandatory Gate)
- Immediately after creating the release, inspect whether any automated workflows are triggered on release publication (e.g., NPM publishing, container build, PyPI upload, Vercel/Pages deployment):
  `gh run list --limit 3`
- If an automated workflow was triggered for `<Confirm_Tag>`:
  1. Monitor execution until completion (`gh run watch <run-id>`).
  2. Verify that the workflow completed with `conclusion: success`.
  3. If the workflow failed:
     - Run `gh run view <run-id> --log-failed` to extract the exact error log.
     - Immediately diagnose and alert the user with the root cause (e.g., expired tokens/secrets, missing permissions, package registry errors).
     - Do NOT declare the release process completed successfully until the CI/CD pipeline passes or the user acknowledges the failure.

### 8. Post-Release Local Cleanup & Artifact Hygiene (Mandatory)
- Once release publication is verified on GitHub (`gh release view <Confirm_Tag>` confirming `asset_count > 0`):
  1. **Purge Distribution Folders:** Delete locally generated `.gitignore` distribution folders and standalone binaries (`releases/`, `dist/`).
  2. **Prune Heavy Incremental Build Caches:** Execute native toolchain cleanup commands (`cargo clean`, `dotnet clean`, `./gradlew clean`) to prune heavy intermediate object caches, strictly avoiding blind glob patterns.

### 9. Summary
- Output a success message confirming that the GitHub Release was created, live, assets verified, and that all triggered CI/CD publish workflows completed successfully.


---
**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.).
