# Handle Disk Pressure on a Hosted Automation Mac

Canonical: https://figment.so/blog/mac-agent-disk-pressure

Write a storage budget, clean only an allowlist of safe paths, and run a launchd check that fires before the disk fills. Plan disks are 80GB (Intel VM, $25), 65GB (Apple Silicon VM, $39) and 512GB (Dedicated M4 Pro 48 GB, $349).

Prevent ENOSPC failures on a hosted Mac with three things: a written storage budget, a cleanup allowlist that names exactly what is safe to delete, and a launchd check that fires before the disk is full. When a download fills the drive, `df` returns 100% and the next write fails with no stack trace in the job log, only a stalled process. Figment plan storage is fixed and includes the OS: 80GB on the Intel Mac VM, 65GB on the Apple Silicon Mac VM and 512GB on the Dedicated M4 Pro.

## Which Figment plan has enough disk?

Match the plan to how much your jobs write. Storage is a fixed allocation per plan, so you move up a plan when the working set outgrows it.

| Plan | Disk | Month | Day | Fits this job when |
|---|---|---:|---:|---|
| [Intel Mac VM](/mac-hosting#intel-vm) (4 shared vCPUs, 4GB RAM) | 80GB | $25 | $2 | Light to medium automation: API calls, document processing, moderate scraping |
| [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) (4 shared vCPUs, 4GB RAM) | 65GB | $39 | $6 | arm64 tools with a working set that fits in 65GB |
| [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) (whole Mac, M4 Pro, 48GB RAM) | 512GB | $349 | Monthly only | Large media downloads, multi-gigabyte artifact caches, rolling browser profiles |

The Intel VM runs macOS Sequoia with Intel-compatible toolchains, including Xcode 26.0 to 26.3. If your jobs download large media, cache multi-gigabyte artifacts or keep rolling browser profiles, 80GB fills quickly, especially once system logs and build caches accumulate between runs. Figment runs the Mac and network layer. Your jobs, caches and cleanup are yours to manage.

## Which directories can the cleanup delete?

Only the ones on a written allowlist. Before writing any cleanup code, list who writes to disk and classify each path. A short table beats a shell wildcard that eats project data by accident.

| Directory | Typical size | Safe to auto-delete? |
|---|---|---|
| `~/Downloads/` | Varies | Yes, after the job validates each download |
| `~/Library/Caches/` | 2-10 GiB | Yes, caches rebuild on demand |
| `/Library/Logs/`, `~/Library/Logs/` | 500 MB-2 GiB | Yes, truncate or rotate |
| `~/Library/Developer/Xcode/DerivedData/` | 10-40 GiB | Yes, rebuilds on next build |
| `~/workdir/` (your project root) | Depends on project | **No, protect this** |
| `~/workdir/checkpoints/` | Small | **No, required for crash resume** |
| `~/workdir/artifacts/` | Varies | Only after upload is confirmed |

The script cleans from this allowlist only. It never traverses the whole filesystem looking for large files, because that risks deleting project data, provisioning profiles or keychain material. It runs as your agent user and never needs root for these paths.

## How do I check free space and clean up automatically?

Save this as `/usr/local/bin/disk-budget-check` and make it executable with `chmod 755`.

```bash
#!/usr/bin/env bash
# Checks free space, logs usage, and cleans allowlisted paths when
# free space falls below the warning or critical thresholds.
set -euo pipefail

WARN_GIB=15      # Alert at this many GiB free
CRITICAL_GIB=5   # Clean at this many GiB free
LOG="$HOME/logs/disk-budget.log"
mkdir -p "$(dirname "$LOG")"

free_gib() { df -g "$HOME" | awk 'NR==2 {print $4}'; }
log()      { echo "$(date -u +%FT%TZ) $*" | tee -a "$LOG"; }

FREE=$(free_gib)
log "Free: ${FREE} GiB"

if (( FREE >= WARN_GIB )); then
    log "OK: above warning threshold."
    exit 0
fi

log "WARNING: ${FREE} GiB free is below ${WARN_GIB} GiB."
log "--- Top consumers ---"
du -shx \
    "$HOME/Downloads" \
    "$HOME/Library/Caches" \
    "$HOME/Library/Logs" \
    "$HOME/Library/Developer/Xcode/DerivedData" \
    2>/dev/null | sort -h | tee -a "$LOG"

if (( FREE >= CRITICAL_GIB )); then
    log "Above critical threshold; alerting only."
    exit 1   # Non-zero so a monitoring wrapper can send a notification
fi

log "CRITICAL: ${FREE} GiB free. Running allowlist cleanup."

cleanup_path() {
    local path="$1"
    [[ -d "$path" ]] || return 0
    log "Cleaning: $path"
    find "${path:?}" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
}

cleanup_path "$HOME/Downloads"
cleanup_path "$HOME/Library/Caches"
cleanup_path "$HOME/Library/Logs"
cleanup_path "$HOME/Library/Developer/Xcode/DerivedData"

FREE_AFTER=$(free_gib)
log "Free after cleanup: ${FREE_AFTER} GiB"

if (( FREE_AFTER < CRITICAL_GIB )); then
    log "FAIL: still below critical after cleanup; needs manual review."
    exit 2
fi

log "Cleanup complete."
```

`find ... -mindepth 1 -maxdepth 1 -exec rm -rf {} +` removes only direct children of the named directory and handles empty directories cleanly. The `${path:?}` expansion aborts the script if the variable is ever empty, which guards against deleting at `/`. Review the script against your own project layout before running it. It is an example, not a reported benchmark.

## How do I schedule the check with launchd?

Save a property list in `~/Library/LaunchAgents/` as `com.yourorg.disk-budget.plist`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.yourorg.disk-budget</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/disk-budget-check</string>
    </array>
    <key>StartInterval</key>
    <integer>1800</integer>
    <key>StandardOutPath</key>
    <string>/Users/agent/logs/disk-budget.out.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/agent/logs/disk-budget.err.log</string>
</dict>
</plist>
```

Load it with `launchctl load ~/Library/LaunchAgents/com.yourorg.disk-budget.plist`. `StartInterval` is documented in Apple's [Creating Launchd Jobs](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html) reference. The agent and daemon distinction and session behaviour are in [Technical Note TN2083](https://developer.apple.com/library/archive/technotes/tn2083/_index.html). Run `launchctl load` from an SSH or remote desktop session as the agent user.

## How do I verify the script before a production run?

Three checks are enough to trust the schedule:

1. **Clean-disk path.** Run with the thresholds inverted so it exits early:
   `WARN_GIB=1 CRITICAL_GIB=0 /usr/local/bin/disk-budget-check`. Expect an `OK` line.
2. **Allowlist safety.** Create `~/Downloads/test-cleanup/` and `~/workdir/important-data/`, then run with very high thresholds so cleanup always fires. Confirm `~/workdir/important-data/` is untouched. This is the one test you cannot skip.
3. **Launchd fires on schedule.** `launchctl list | grep disk-budget` should show the label with PID `-` and last exit `0`. Force a run with `launchctl start com.yourorg.disk-budget` and tail the log.

After large jobs, run `df -h ~` before you queue the next one. If the disk sits under 15 GiB free for several checks in a row, that is the signal to move to a larger plan, not a cue to lower `CRITICAL_GIB`.

## How many runs fit before I cross the threshold?

Divide your headroom by what one run leaves behind. A nightly job that downloads 6 GiB, writes 2 GiB of derived data and leaves 1 GiB of logs touches 9 GiB per run. Starting from 75 GiB of headroom (80GB minus the 5 GiB critical reserve), that is 75 / 9, about 8 runs before you cross the threshold if nothing is cleaned. Storage includes the OS and your tools, so real headroom is lower. Read it with `df -g ~` instead of assuming it. With the allowlist above, each run returns to roughly the same free-space baseline and the plan holds.

Plan storage is fixed, with no on-demand volume expansion. If your baseline sits too close to the threshold, step up to the 512GB Dedicated M4 Pro. A persistent disk is not a backup. Figment does not back up your Mac, and the cleanup script deletes data for good. Upload artifacts before you clean them.

## How do I start, or move an existing workflow?

Pick a plan on the [Mac hosting page](/mac-hosting). Once the Mac is ready:

1. Install `/usr/local/bin/disk-budget-check` and `chmod 755` it.
2. Drop the plist into `~/Library/LaunchAgents/` and `launchctl load` it.
3. Run checks 1 and 2 above before the next production job.

To migrate an existing workflow, run `du -shx ~/* 2>/dev/null | sort -h` on the old host and confirm the allowlist covers the same categories before cutover. Caches on the new host start empty, which is the right moment to prove the allowlist is complete.

---

**Related guides:**
- [Mac Agent API Spend Caps](/blog/mac-agent-api-spend-caps)
- [Mac Agent Key Rotation](/blog/mac-agent-key-rotation)
- [GitHub Actions Self-Hosted Runner on Mac](/blog/mac-github-actions-self-hosted-runner)
