Single source of truth for agent scratch, temporary files, disposable builds, and evidence paths across macOS and Linux. Use whenever an agent, skill, or workflow needs a scratch directory, temporary file path, build output folder, or evidence directory instead of hardcoding /tmp or OS-specific mountpoints.
6 stars
0 votes
0 copies
3 views
Added November 26, 2026
toolsrustshellbashgit
Works with
claude code
Security analysis
C60/100
highPerforms destructive filesystem operations
criticalModifies startup scripts or system services for persistence
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Scratch Space?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/jleechanorg-scratch-space)
---
name: scratch-space
description: Single source of truth for agent scratch, temporary files, disposable builds, and evidence paths across macOS and Linux. Use whenever an agent, skill, or workflow needs a scratch directory, temporary file path, build output folder, or evidence directory instead of hardcoding /tmp or OS-specific mountpoints.
---
# Scratch Space (`/scratch-space`) — Single Source of Truth
## Purpose (Ponytail / DRY / ZFC Invariant)
Do **not** hardcode `/tmp/...`, `/private/tmp/...`, `/Volumes/diskm-scratch`, or `~/Scratch` across individual skills, prompts, or agent instruction files.
All temporary, disposable build, test scratch, and evidence paths resolve dynamically through **one canonical interface** backed by `diskm` (`~/.config/disk-magician/config.json`) and exported by default in all shell/agent environments via `$DISKM_SCRATCH` and `$CLAUDE_CODE_TMPDIR`:
0. **Default Environment Variables (Pre-exported in `~/.zshenv`, `~/.bashrc`, `~/.profile`, and `~/.claude/settings.json`)**
- `DISKM_SCRATCH`: Pre-set to the host's active Scratch volume (`/Volumes/diskm-scratch` on macOS, `~/Scratch` / `${HOME}/Scratch` on Linux).
- `CLAUDE_CODE_TMPDIR`: Pre-set to `${DISKM_SCRATCH}/tmp` so Claude Code task scratchpads (`claude-*`) use the Scratch volume automatically by default.
- `PYTEST_DEBUG_TEMPROOT`: Pre-set to `${DISKM_SCRATCH}/tmp` so pytest debug artifacts and test scratch stay off the host root filesystem.
- `BASH_ENV`: Pre-set to `$HOME/.bashrc` across shell configurations and `settings.json` so non-interactive agent bash subshells inherit the Scratch environment automatically before any interactive shell guards.
1. **Repo-Scoped Build, Test & Evidence Directories (Managed Lease + Admission)**
```bash
# Allocate a managed build or evidence directory. If no Scratch volume is configured,
# stop and report it: /tmp workspaces are prohibited (see the section below), so the
# helper must not be used as a /tmp fallback.
OUT_DIR="$(diskm scratch-new <repo-path> <purpose> --kind build)"
EVIDENCE_DIR="$(diskm scratch-new <repo-path> <purpose> --kind evidence)"
# ...produce output in "$OUT_DIR"...
diskm scratch-finish "$OUT_DIR" completed # or: abandoned
```
- Any process of the same user may run `scratch-finish`; it does not have to be the allocating shell.
- A build run that is never finished becomes removable once its recorded owner has exited and its files have been untouched for the default content floor (`CONTENT_FLOOR_HOURS` in diskm's `rust/src/time_windows.rs`). Open files, live working directories, repositories and PR-evidence holds still protect it. Evidence runs are never removed this way.
- Pass `--owner-pid <PID>` (a process of the same user) to bind the run to a long-lived producer; the run is then kept while that process lives.
- Put run output here, not under `scratch-dir`: files outside a leased run are never removed automatically.
- `diskm scratch-new` enforces volume health, admission control, default content-floor recency, and active-lease protection.
2. **Ad-Hoc Helper Files, PR Bodies, Frame Dumps, Server Logs & Ephemeral Scripts**
```bash
# Resolve the host's active Scratch root (or create/return a subdirectory inside it)
SCRATCH_ROOT="$(~/.claude/skills/scratch-space/scripts/scratch-dir.sh)"
SCRATCH_TMP="$(~/.claude/skills/scratch-space/scripts/scratch-dir.sh tmp)"
```
- Example (`gh` PR/issue body file):
```bash
BODY_FILE="$(~/.claude/skills/scratch-space/scripts/scratch-dir.sh gh)/pr_body_$$.md"
```
- Example (ephemeral `mktemp` directory for video frames, clones, or probes):
```bash
SCRATCH_TMP="$(~/.claude/skills/scratch-space/scripts/scratch-dir.sh tmp)" || {
echo "Scratch resolver unavailable; refusing allocation" >&2
exit 75
}
[ -n "$SCRATCH_TMP" ] || {
echo "Scratch resolver returned an empty path; refusing allocation" >&2
exit 75
}
WORK_DIR="$(mktemp -d "$SCRATCH_TMP/work.XXXXXX")" || {
echo "Scratch allocation failed" >&2
exit 75
}
```
- Example (dev/test server logs):
```bash
LOG_DIR="$(~/.claude/skills/scratch-space/scripts/scratch-dir.sh "logs/${REPO_NAME}/${BRANCH_NAME}")"
```
## Task-Scoped Teardown
For an aged task-scratch container, preview with `diskm cleanup-task-scratch --dry-run --roots <exact-dedicated-task-parent>`. This scans eligible task-container names beneath the supplied root; use it only when that root is dedicated to this task and the preview identifies only its disposable container. Its safety checks preserve live/open paths, secret-like filenames, and containers with dirty or unpushed nested repositories, but it can remove clean, pushed linked worktrees, so do not use it where those are intentional. The filename check is not a scan for secrets inside ordinary files; inspect the disposable scope rather than treating it as content protection. Add `--clean` only when the preview is correct and removal is authorized.
`diskm scratch-prune` is a pool-wide C14 cleanup command, dry-run by default; it does not accept an arbitrary single-path target. Do not use it to tear down one task directory. If the existing scoped cleanup command cannot safely identify the task container, leave it in place and report that limitation; never substitute `rm -rf` on a variable path.
## What NOT to Put in Scratch
- **Git worktrees**: Always use `~/.worktrees/<repo>/<name>` via `diskm worktree-new <repo-path> <branch>`. Never put git worktrees in Scratch or `/tmp`.
- **Durable reports & roadmaps**: Keep in `~/roadmap/`.
- **Mutable agent state, credentials & transcripts**: Keep in `$HOME` (`~/.claude`, `~/.codex`, `~/.gemini`, `~/.config`).
- **Global `TMPDIR`**: Never remap global `TMPDIR` or replace `/tmp`.
## Host Provisioning Reference
Operators provision or inspect the quota-bounded Scratch volume via `diskm`:
- Provision (optional, recommended 200 GiB): `diskm setup --scratch [--quota-gb 200]`
- Query active mountpoint: `diskm volume path scratch`
- Check volume status: `diskm volume status scratch`
## `/tmp` workspaces are prohibited — enforcement depends on the host
A `PreToolUse` scratch-routing guard, where the host installs one (a hook wired into the
active runtime's settings, exit 2 = blocked), rejects tool calls that would create a
**workspace** under `/tmp`. It fires for `git clone`/`git worktree add`/`mkdir`/`mktemp
-d`/`cp`/`rsync`/`tar -C`/`unzip -d`/`pip --target` destinations and for document
writes under `/tmp`.
Why blocking rather than advice: `/tmp` is on the root filesystem whose free space gates
self-hosted runner admission, and lane clones there have drained a CI fleet twice
(20 -> 0 containers, then 20 -> 7). Relocating existing directories did not stop new ones
appearing, so a host that installs the guard enforces the rule there.
**This export does not ship that hook.** Enabling it is a host decision; where it is not
installed, the rules below are still the required behavior for an agent, and nothing
rejects a violation automatically.
`/tmp` remains correct — and is allowed — for: logs (`.log`), sockets, pid/lock/marker
files, single temp files, `.json`/`.jsonl` state, scripts (`.sh`/`.py`), pip unpack
dirs, X11/ssh-agent/chromium profile dirs. Durable documents belong in the reports
repository, never `/tmp`.
Where the runtime's own temp already points at the scratch volume, the guard covers the
Bash commands an agent types, which is where the drift was. A human operator can bypass
one call with `SCRATCH_GUARD_ALLOW_TMP=1`; an agent never sets it on its own behalf.
Global `TMPDIR` is still never remapped and `/tmp` is never replaced.
Do not "fix" disk pressure by moving workspaces into the scratch volume and calling it
reclaimed: on Linux, the scratch volume is a loop-mounted image inside the same root
filesystem, so relocating data there returns zero host free space (freeing blocks inside
the image requires root `fstrim`/`discard`). Reclamation means deleting, not relocating.