name: Bundle MCP Server & CLI

on:
  workflow_dispatch:
  push:
    branches: [main]
    paths:
      - 'src/**'
      - 'package.json'
      - 'tsconfig.json'

permissions:
  contents: write

jobs:
  bundle:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "22.5"

      - run: npm install

      - run: npx tsc --noEmit

      - run: npm run bundle

      - name: Assert bundle invariants (G3 — Issue #511 guardrail)
        run: npm run assert-bundle

      - name: Commit bundle
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -f server.bundle.mjs cli.bundle.mjs hooks/session-extract.bundle.mjs hooks/session-snapshot.bundle.mjs hooks/session-db.bundle.mjs hooks/security.bundle.mjs
          git diff --staged --quiet && echo "No changes" && exit 0
          git commit -m "ci: update server.bundle.mjs, cli.bundle.mjs, session hook & security bundles"
          git push
