Skip to content

maintenance(shell): guard against unwritable inherited TMPDIR in headless contexts#51

Merged
abarrows merged 1 commit into
productionfrom
chore/headless-agent-tmpdir-guard
Jul 1, 2026
Merged

maintenance(shell): guard against unwritable inherited TMPDIR in headless contexts#51
abarrows merged 1 commit into
productionfrom
chore/headless-agent-tmpdir-guard

Conversation

@abarrows

@abarrows abarrows commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Problem

The 24/7 headless Mac mini agent (and any launchd-launched shell) inherits the generic macOS fallback temp dir:

TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T   # owned root:wheel, mode 0700

instead of the per-user temp — because a launchd-less/headless context never sets up the DARWIN_USER_TEMP_DIR bootstrap. That directory is not writable by the user, so any tool that writes to $TMPDIR fails with EACCES:

  • vitest run — its coverage provider mkdirs a working dir under $TMPDIR
  • npm, esbuild, and other temp-writing tooling, intermittently

It is not a sandbox/permissions issue — the bad TMPDIR is inherited from the launch environment.

Fix

Add a guard to shell/.zprofile that repoints TMPDIR/TMP/TEMP at ~/.claude/tmp only when the inherited value is unset or not writable. Healthy interactive machines (which get a proper writable per-user temp) are left untouched.

if [ -z "$TMPDIR" ] || [ ! -w "${TMPDIR%/}" ]; then
  export TMPDIR="$HOME/.claude/tmp"
  export TMP="$TMPDIR"
  export TEMP="$TMPDIR"
  mkdir -p "$TMPDIR"
fi

Testing

  • bash -n and zsh -n pass.
  • Guard verified across states: unset → override; root-owned zz fallback → override; writable dir / real per-user temp → keep system temp.

Relates to the Mac mini agent hardening (WR-18897). Claude Code itself is already covered live via env.TMPDIR in ~/.claude/settings.json; this extends the fix to the whole shell-launched agent context.

🤖 Generated with Claude Code

Summary by Sourcery

Enhancements:

  • Add a zprofile guard that rewrites TMPDIR/TMP/TEMP to a per-user temp directory when the inherited TMPDIR is unset or not writable to prevent EACCES failures in headless environments.

…less contexts

A headless / launchd-launched agent (the 24/7 Mac mini) inherits the generic
macOS fallback temp dir (TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n0000000000000/T,
owned root:wheel mode 0700) instead of the per-user temp, because launchd never
set up the DARWIN_USER_TEMP_DIR bootstrap. Any tool that writes to $TMPDIR then
fails with EACCES — e.g. `vitest run` (its coverage provider mkdir's under
$TMPDIR), npm, and esbuild.

Add a guard to .zprofile that points TMPDIR/TMP/TEMP at ~/.claude/tmp ONLY when
the inherited value is unset or not writable, so healthy interactive machines
keep the system temp dir untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a guarded TMPDIR/TMP/TEMP override in the shell profile to ensure a writable temp directory in headless or launchd-launched contexts while leaving normal interactive shells untouched.

Flow diagram for guarded TMPDIR override in .zprofile

flowchart TD
  A[Shell startup
.zprofile] --> B[Check TMPDIR is set]
  B -->|unset| D[Override TMPDIR TMP TEMP to ~/.claude/tmp]
  B -->|set| C[Check TMPDIR is writable]
  C -->|not writable| D
  C -->|writable| E[Keep inherited TMPDIR]
  D --> F[mkdir -p ~/.claude/tmp]
  F --> G[Continue shell init]
  E --> G
Loading

File-Level Changes

Change Details Files
Guard TMPDIR/TMP/TEMP to point to a user-writable temp directory when the inherited TMPDIR is unset or not writable.
  • Insert a comment block documenting the macOS headless TMPDIR issue and the rationale for the guard.
  • Add a conditional that detects unset or non-writable TMPDIR (trimming any trailing slash) before overriding.
  • Export TMPDIR, TMP, and TEMP to a user-specific temp path under the home directory and ensure it exists via mkdir -p.
  • Place the guard early in the profile so later commands run with a valid temp directory while preserving system temp on healthy interactive shells.
shell/.zprofile

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@abarrows abarrows merged commit 4e123e4 into production Jul 1, 2026
1 of 2 checks passed
@abarrows abarrows deleted the chore/headless-agent-tmpdir-guard branch July 1, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant