# Send Useful Failure Alerts from Hosted Mac Jobs

Canonical: https://figment.so/blog/mac-agent-failure-alerts

Call a small alert.sh helper from every job: it posts a structured Slack webhook message, suppresses repeats for 5 minutes, and counts consecutive failures. Alerting is light work, so a Figment Intel Mac VM ($25 a month) is enough.

Give every job a one-line call to a small alert helper that posts a structured Slack message, suppresses repeats of the same failure for five minutes, and counts consecutive failures for escalation. A job that fails silently backs up the queue and stalls upstream services, and you hear about it hours later from a downstream complaint. The fix is not another dashboard. Alert delivery is negligible work, so a Figment Intel Mac VM at $25 a month is enough for the alerter and a light launchd pipeline beside it.

This guide gives you a working helper script, a payload structure that fits how [Slack incoming webhooks](https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks) behave, and an acceptance worksheet so you can verify the helper on your own Mac before you depend on it.

## Which Figment plan should run the alerter?

Any Figment plan handles a JSON POST via `curl`, so choose by the companion workload the alerter sits next to.

| Companion workload | Plan | Month | Day |
|---|---|---:|---:|
| Light cron or launchd pipeline that is fine on Intel | [Intel Mac VM](/mac-hosting#intel-vm), 4 shared vCPUs, 4GB, 80GB, macOS Sequoia | $25 | $2 |
| Device builds, simulators or arm64 execution beside the alerter | [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm), 4 shared vCPUs, 4GB, 65GB | $39 | $6 |
| Heavy builds or an isolated production pipeline | [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro), whole Mac, M4 Pro, 48GB, 512GB | $349 | Monthly only |

Outbound HTTPS to `hooks.slack.com` (or your own incident endpoint) is the only network requirement. If a receiving system restricts source IPs, update its allowlist from the egress IP you observe on the host. Slack does not publish a per-workspace webhook source range, and Apple does not publish one for its developer services either. Figment runs the Mac and network layer. The helper, your webhooks and your Slack workspace are yours to manage. Support at Figment is by dashboard ticket with email replies, so keep your own alert path for the machine itself.

## What do Slack incoming webhooks accept?

Two details trip people up, and both are documented on Slack's incoming webhooks page:

- The destination channel is bound to the webhook URL when the app is installed. A `channel` field in the JSON body is ignored. If you want `#ops-alerts` and `#ops-critical` to receive different alerts, install the app twice (once per channel) and store two webhook URLs.
- The minimum required field is `text`. Rich layout is done with `blocks`. The older `attachments` array still works, but the docs now call it legacy. The script below sends a short top-level `text` for notification previews plus a `blocks` array for readable formatting.

## What does the helper need?

The script needs very little:

- **`SLACK_WEBHOOK_URL`** (normal severity) and optionally **`SLACK_WEBHOOK_URL_CRITICAL`** (installed on a separate channel), both stored in your CI service's secret manager (GitHub Actions secret, Vault, 1Password CLI) and injected only into the job that posts the alert. Do not export the entire environment to a plaintext file, because it can leak unrelated keys. Never commit the URLs.
- `curl` and `jq` on the Mac. `curl` ships with macOS. Install `jq` with `brew install jq` so JSON escaping is safe for arbitrary error text.
- Write access to a local dedup state directory under `$HOME`.
- No TCC prompts, because the script only uses the network and reads and writes files in `$HOME`.

To create each webhook, follow Slack's [incoming webhooks guide](https://docs.slack.dev/messaging/sending-messages-using-incoming-webhooks) and paste each URL into your secret manager. Load it into an interactive shell with `read -s SLACK_WEBHOOK_URL; export SLACK_WEBHOOK_URL` so the literal never lands in shell history.

## What should an alert say?

A useful alert answers four questions immediately: what broke, where, when, and what to look at first.

| Field | Purpose |
|-------|---------|
| `job` | Which automation pipeline fired the alert |
| `host` | Short hostname, useful when more than one Mac is in play |
| `severity` | `warning`, `error`, or `critical` |
| `step` | The specific pipeline step that failed |
| `message` | Error message or last log line |
| `run_id` | Timestamp-based ID for correlating alerts to logs |
| `escalation_count` | Consecutive failures for this `job`+`step` |

## What does the alert helper look like?

Save `alert.sh` to `~/scripts/alert.sh`. This is an example to review and run against your own webhook in a test channel. Treat the acceptance worksheet below as the gate before depending on it.

```bash
#!/usr/bin/env bash
# alert.sh: structured Slack alert with dedup window and escalation count.
# Usage: alert.sh <job> <severity> <step> <message> [extra-context]
# Requires: SLACK_WEBHOOK_URL (and optionally SLACK_WEBHOOK_URL_CRITICAL).
set -euo pipefail

JOB="$1"
SEVERITY="$2"
STEP="$3"
MESSAGE="$4"
CONTEXT="${5:-}"

: "${SLACK_WEBHOOK_URL:?Set SLACK_WEBHOOK_URL in environment}"
command -v jq   >/dev/null || { echo "jq not installed (brew install jq)"   >&2; exit 2; }
command -v curl >/dev/null || { echo "curl not installed"                   >&2; exit 2; }

# Sanitize identifiers that become filenames.
safe() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; }
JOB_S=$(safe "$JOB")
STEP_S=$(safe "$STEP")

HOST=$(hostname -s)
RUN_ID=$(date +%Y%m%d-%H%M%S)
SEVERITY_UP=$(printf '%s' "$SEVERITY" | tr '[:lower:]' '[:upper:]')

umask 077
DEDUP_DIR="${HOME}/.alert-dedup"
mkdir -p "$DEDUP_DIR"

# --- Deduplication: suppress if same job+step fired within the last 5 minutes.
DEDUP_LOCK="${DEDUP_DIR}/${JOB_S}__${STEP_S}.lock"
if [[ -f "$DEDUP_LOCK" ]]; then
    LAST_FIRED=$(stat -f %m "$DEDUP_LOCK")
    NOW=$(date +%s)
    if (( NOW - LAST_FIRED < 300 )); then
        echo "SUPPRESSED (dedup ${JOB_S}/${STEP_S}, $(( 300 - (NOW - LAST_FIRED) ))s left)" >&2
        exit 0
    fi
fi
touch "$DEDUP_LOCK"

# --- Escalation count: consecutive failures for this job+step.
ESCALATION_FILE="${DEDUP_DIR}/${JOB_S}__${STEP_S}.count"
COUNT=1
if [[ -f "$ESCALATION_FILE" ]]; then
    COUNT=$(( $(cat "$ESCALATION_FILE") + 1 ))
fi
echo "$COUNT" > "$ESCALATION_FILE"

# --- Route by severity.
# Slack binds the channel to the webhook URL at install time, so a "channel"
# field in the payload is ignored. Install the app twice if you want two
# channels and set SLACK_WEBHOOK_URL_CRITICAL to the second URL.
WEBHOOK="$SLACK_WEBHOOK_URL"
if [[ "$SEVERITY" == "critical" ]] || (( COUNT >= 3 )); then
    WEBHOOK="${SLACK_WEBHOOK_URL_CRITICAL:-$SLACK_WEBHOOK_URL}"
fi

# --- Build the payload with jq so arbitrary error text is escaped correctly.
PAYLOAD=$(jq -n \
  --arg sev   "$SEVERITY_UP" \
  --arg job   "$JOB" \
  --arg step  "$STEP" \
  --arg msg   "$MESSAGE" \
  --arg ctx   "$CONTEXT" \
  --arg host  "$HOST" \
  --arg run   "$RUN_ID" \
  --arg count "$COUNT" \
  '{
    text: ("[" + $sev + "] " + $job + " > " + $step + ": " + $msg),
    blocks: [
      { type: "header",
        text: { type: "plain_text",
                text: ("[" + $sev + "] " + $job + " > " + $step) } },
      { type: "section",
        fields: [
          { type: "mrkdwn", text: ("*Message*\n" + $msg) },
          { type: "mrkdwn", text: ("*Context*\n" + (if $ctx == "" then "-" else $ctx end)) },
          { type: "mrkdwn", text: ("*Host*\n" + $host) },
          { type: "mrkdwn", text: ("*Run ID*\n" + $run) },
          { type: "mrkdwn", text: ("*Escalation*\n#" + $count) }
        ] }
    ]
  }')

RESPONSE_FILE=$(mktemp -t alert-last.XXXXXX)
HTTP_STATUS=$(curl -sS -o "$RESPONSE_FILE" -w "%{http_code}" \
  -X POST -H 'Content-type: application/json' \
  --data "$PAYLOAD" "$WEBHOOK")

if [[ "$HTTP_STATUS" != "200" ]]; then
    echo "ALERT_SEND_FAILED: HTTP ${HTTP_STATUS} body=$(cat "$RESPONSE_FILE")" >&2
    rm -f "$RESPONSE_FILE"
    exit 1
fi
rm -f "$RESPONSE_FILE"
echo "Alert sent: ${JOB_S}/${STEP_S} escalation=#${COUNT}"
```

Make it executable with `chmod +x ~/scripts/alert.sh`. Clear the counter and lock on a clean run:

```bash
alert_reset() {
    local job step job_s step_s
    job="$1"; step="$2"
    job_s=$(printf '%s' "$job"  | tr -c 'A-Za-z0-9._-' '_')
    step_s=$(printf '%s' "$step" | tr -c 'A-Za-z0-9._-' '_')
    rm -f "${HOME}/.alert-dedup/${job_s}__${step_s}.count" \
          "${HOME}/.alert-dedup/${job_s}__${step_s}.lock"
}
```

## How do deduplication and escalation work?

The five-minute dedup window (`300` seconds) prevents alert storms when a flaky upstream causes rapid repeated failures. Match it to your batch cadence: a 10-minute OCR job should use `600`.

Apply these rules in your Slack workflow or PagerDuty integration. Because channels are per-webhook, "post to a different channel" means "send to a different webhook URL":

| Escalation count | Response |
|------------------|----------|
| 1 | Send via `SLACK_WEBHOOK_URL`, no mention |
| 3 | Send via `SLACK_WEBHOOK_URL_CRITICAL`, add an `@oncall` user-group mention in the text |
| 5 | Trigger a PagerDuty high-urgency incident from a Slack workflow or directly |
| 10+ | Auto-unload the launchd plist and require manual re-enable |

Auto-disable at 10 protects you from an unbounded series of alerts when a job cannot recover without a human:

```bash
if (( COUNT >= 10 )); then
    launchctl unload "${HOME}/Library/LaunchAgents/com.yourdomain.failed-job.plist" || true
    SLACK_WEBHOOK_URL="${SLACK_WEBHOOK_URL_CRITICAL:-$SLACK_WEBHOOK_URL}" \
      ~/scripts/alert.sh "$JOB" "critical" "auto-disabled" \
        "Disabled after 10 consecutive failures. Re-enable manually."
fi
```

## How do I add alerting to a job in one line?

Use `trap ERR` so unexpected non-zero exits fire an alert automatically. `set -E` makes the trap inherit into functions and subshells:

```bash
#!/usr/bin/env bash
set -eEuo pipefail
trap 'rc=$?; ~/scripts/alert.sh "pdf-check" "error" "unexpected-exit" \
      "Script failed at line ${LINENO} (rc=${rc})" \
      "$(tail -n 20 /var/log/pdf_check/err.log 2>/dev/null || echo no-log)"' ERR

# ... job body ...

# On a clean success path, clear the counter and lock.
source ~/scripts/alert_reset.sh
alert_reset "pdf-check" "unexpected-exit"
```

For expected failure points where you want a specific message:

```bash
~/scripts/pdf_check.sh ~/pdf-spool ~/pdf-results || \
    ~/scripts/alert.sh "pdf-check" "error" "batch-run" \
      "PDF check batch exited non-zero" \
      "$(tail -n 20 /var/log/pdf_check/err.log 2>/dev/null || echo no-log)"
```

## What if the alerter itself fails?

The helper can fail when the network is down, the webhook is revoked, `SLACK_WEBHOOK_URL` is unset or `jq` is missing. Protect against a silent alerter:

- Redirect stderr to a dedicated log: `2>>"${HOME}/Library/Logs/alert_errors.log"`.
- Add a separate periodic [launchd job](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html) using `StartInterval 3600` that checks whether `alert_errors.log` grew in the last hour and falls back to `mail` or an `osascript -e 'display notification "..."'` call.
- Do not let `alert.sh` retry itself on failure. Pick a quiet secondary path instead.

## How do I confirm the helper works before relying on it?

Run these checks on the Mac you intend to use, and record the outcome. Do not mark the helper production-ready until each row passes.

1. `brew install jq` succeeds, and `jq --version` prints a version.
2. `SLACK_WEBHOOK_URL=... ~/scripts/alert.sh smoke warning startup "routing ok"` returns `Alert sent: ...` and the message appears in the installed channel.
3. Run the same command twice within 5 minutes. The second call prints `SUPPRESSED` to stderr and sends no second message.
4. Wait 5 minutes and re-run. A second message appears and the escalation counter increments (you will see `#2` in the payload).
5. Pass a `message` argument containing double quotes, a newline (`$'\n'`) and a backslash. Confirm Slack renders the message without breaking the JSON.
6. Temporarily rename the webhook URL to force a 404. The script exits non-zero and writes `ALERT_SEND_FAILED` to stderr. The dedup lock is still created (expected, because this also rate-limits a failing sender). Delete it manually if you want an immediate retry.
7. Simulate the critical path: set `SLACK_WEBHOOK_URL_CRITICAL` to a second webhook installed in a different channel and call `alert.sh job critical step msg`. Confirm the second channel receives it.
8. Call `alert_reset` for the job+step and confirm the count file and lock file are gone from `~/.alert-dedup/`.

## What does alerting cost?

It adds no meaningful load to any Figment plan, so the Intel Mac VM ($25 a month) handles it next to a launchd cron workload. Slack's incoming webhooks page does not publish a specific per-webhook rate limit, so if a failure loop pushes more than a few alerts per second, insert `sleep 1` or push through a small queue. At that point the real fix is the upstream job, not the alerter. A Figment Mac bills the same when the jobs are idle.

## How do I start, or move existing alerting?

1. Pick a plan on the [Mac hosting page](/mac-hosting). The Intel Mac VM is enough for alert delivery. Choose an Apple silicon plan only if the companion workload needs arm64 execution or more RAM.
2. Create a Slack app with an incoming webhook for `#ops-alerts` and, if you want a separate critical channel, install a second webhook for `#ops-critical`.
3. Store `SLACK_WEBHOOK_URL` and `SLACK_WEBHOOK_URL_CRITICAL` in your CI secret manager and inject them into the launchd or CI job that sends alerts.
4. Copy `alert.sh` to `~/scripts/` and run `chmod +x` on it.
5. Walk through the acceptance worksheet above.
6. Add `trap ERR` or explicit `alert.sh` calls to each automation script, and call `alert_reset` on successful completion paths.

**Related guides:** [Reboot recovery for Mac agents](/blog/mac-agent-reboot-recovery) · [Task queue for Mac agents](/blog/mac-agent-task-queue) · [Self-hosted GitHub Actions runner on Mac](/blog/mac-github-actions-self-hosted-runner)
