Skip to content

Headless mode runs one prompt without the TUI and is designed for scripts. This tutorial builds a CI job that reviews a pull request diff and emits a JSON result. The mechanics are documented in Headless mode.

Prerequisites ​

  • A GitHub Actions repository with Node.js 20 or newer.
  • An NVIDIA NIM API key stored as a repository secret (for example NVIDIA_API_KEY).
  • M31A available as a dependency or via the bundled entry point.

1. Try the command locally ​

bash
export NVIDIA_API_KEY="nvapi-..."
m31a -p "Review the uncommitted diff for correctness and security issues." \
  --output-format json > review.json

-p/--prompt runs non-interactively. The prompt is appended to anything piped on stdin, so you can feed it a diff explicitly:

bash
git diff | m31a -p "Review this diff and list findings by severity." -o json > review.json

-p cannot be combined with a positional prompt or with -i.

The JSON object written by packages/core/src/output/json-formatter.ts has:

json
{
  "session_id": "...",
  "response": "...",
  "stats": {},
  "warnings": []
}

An error field is added when the run fails. Diagnostic messages go to stderr in JSON mode so stdout stays parseable.

2. Inspect the result ​

bash
node -e "
const fs=require('node:fs');
const r=JSON.parse(fs.readFileSync('review.json','utf8'));
if (r.error) { console.error(r.error); process.exit(1); }
console.log(r.response);
"

This is the pattern to reuse in CI: parse first, check error, then act on response. Do not rely on human-readable text for downstream logic.

For streaming event-by-event output, use --output-format stream-json. Each line is a JSON event, including a final result event with status of success or error.

3. Make the run deterministic ​

Use Plan Mode so no tool call can modify the checkout:

bash
git diff | M31A_TRUST_WORKSPACE=true m31a \
  --approval-mode plan \
  -p "Review this diff. Return a JSON array of findings with severity, file, line, and message." \
  --output-format json > review.json

Why these settings:

  • plan denies writes and mutating shell commands through the policy engine.
  • Any tool call that would otherwise need confirmation is denied in a non-interactive run, not prompted for.
  • M31A_TRUST_WORKSPACE=true avoids an interactive trust prompt in CI.

Keep the model prompt strict about output format, then validate it in your script. If you need the agent to write a file, do it in a separate step with an explicit prompt and a writable directory.

4. GitHub Actions workflow ​

Create .github/workflows/m31a-review.yml:

yaml
name: M31A review

on:
  pull_request:

permissions:
  contents: read

jobs:
  review:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install M31A
        run: npm install -g m31a

      - name: Review diff
        env:
          NVIDIA_API_KEY: ${{ secrets.NVIDIA_API_KEY }}
          M31A_TRUST_WORKSPACE: 'true'
        run: |
          git diff "origin/${{ github.base_ref }}...HEAD" \
            | m31a --approval-mode plan \
                -p "Review this diff. Return a JSON array of findings with severity, file, line, and message." \
                --output-format json > review.json

      - name: Check result
        run: |
          node -e "
          const fs=require('node:fs');
          const r=JSON.parse(fs.readFileSync('review.json','utf8'));
          if (r.error) { console.error(r.error); process.exit(1); }
          console.log(r.response);
          "

      - name: Upload review artifact
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: m31a-review
          path: review.json

If you prefer not to install globally, run node bundle/m31a.js from a checkout of M31A, as shown in Headless mode.

5. Secrets and redaction ​

  • Store the key in GitHub Secrets, never in a committed settings file.
  • Do not place the key in M31A.md or in an extension manifest.
  • Enable security.environmentVariableRedaction.enabled if tool output might include environment values.
  • Use advanced.excludedEnvVars to keep sensitive variables out of project context; the default exclusions are DEBUG and DEBUG_MODE.
  • Avoid --raw-output; it disables ANSI sanitization. If you must use it, --accept-raw-output-risk suppresses the warning but not the risk.

6. Exit handling ​

M31A sets a non-zero process exit code on fatal errors, and writes an error field in JSON mode. CI should check both signals:

bash
set -o pipefail
if ! m31a -p "..." -o json > review.json; then
  echo "M31A exited non-zero" >&2
fi
node -e "const r=require('./review.json'); if (r.error) process.exit(1)"

Cancellation exits use distinct codes: 130 for SIGINT, 143 for SIGTERM, and 129 for SIGHUP. A job that times out should treat these as a failure rather than as a review result.

7. Cost and reliability ​

general.maxAttempts defaults to 10 and caps main-model attempts at 10. model.compressionThreshold defaults to 0.5; for very large diffs, pipe a summarized diff instead of the full patch. Set model.requestTimeoutMs if your runner has strict step timeouts.

Use stream-json when you want progress in CI logs; use json when you want a single artifact. Both are described in the CLI reference.

Troubleshooting ​

SymptomResolution
Empty stdoutCheck that stderr holds a diagnostic and that the key is present.
JSON parse failureRemove other stdout writes from the step; diagnostics go to stderr.
Confirmation errorYou are not in headless mode, or a tool required approval.
Trust promptSet M31A_TRUST_WORKSPACE=true or --skip-trust.
Tool deniedExpected in non-interactive mode; narrow the prompt or use Plan Mode.

Next steps ​

Built with VitePress. Powered by NVIDIA NIM.