Keep Claude NODE_OPTIONS restore preload out of TMPDIR

Merged
TE
2mo ago
+301-33

Fixes #12022.

Claude's NODE_OPTIONS restore preload lived in $TMPDIR/cmux-claude-node-options/. Every Node child of a wrapped Claude session loads it through --require, so once macOS purges $TMPDIR under a long-lived session, each new Node child exits with MODULE_NOT_FOUND.

The preload now lives in ~/.cmuxterm/cmux-claude-node-options/restore-node-options.cjs, next to the other CLI shims (createTmuxCompatShimDirectory already uses ~/.cmuxterm/<name>). Both writers move:

  • Resources/bin/cmux-claude-wrapper (ensure_node_options_restore_module): directory is 0700, symlinked directory or file is refused, and the module is written to a temp file in the same directory and renamed into place.
  • CLI/cmux.swift (createClaudeNodeOptionsRestoreModule, used by claude-teams and omc): directory is created and kept at 0700; the write goes through the existing atomic writeShimIfChanged.

--require is quoted when the path contains whitespace, since NODE_OPTIONS splits on it and $HOME is no longer a system-chosen path.

This is a minimal re-cut of #12067 by @austinywang, which bundles this fix with unrelated agent-journal, cloud rename and CI changes. It leaves out #12067's legacy $TMPDIR module recreation and quoted-token parser.

Related, not fixed here: #14681. With the native (Mach-O) Claude Code binary the preload never runs, so the injected NODE_OPTIONS leaks to every child, and a sandboxed app that cannot read the preload crash-loops. Moving the file out of $TMPDIR does not change that: a sandbox cannot read ~/.cmuxterm either. The fix there is to skip the injection for non-Node claude binaries.

Validation

  • tests/test_claude_wrapper_node_options_survives_tmpdir_purge.py (ported from #12067, registered in tests/test-execution.toml): launches the wrapper with HOME containing a space, purges $TMPDIR while the fake Claude is alive, then starts a Node child.
    • d7a23bc7991 (test only): fails with Error: Cannot find module '.../session-tmp/cmux-claude-node-options/restore-node-options.cjs', the #12022 symptom.
    • a1414162aca (fix): passes; the child restores the user's original NODE_OPTIONS and the directory is 0700.
  • tests/test_claude_wrapper_hooks.py: the old "unusable TMPDIR skips injection" case now expects injection from ~/.cmuxterm; it and the other NODE_OPTIONS cases pass locally. Two subcommand-cache cases failed once under a load average of ~300 and passed on rerun, same as on main.
  • bash -n Resources/bin/cmux-claude-wrapper.
  • The Swift change is not compiled locally (no local app builds on this machine); CI compiles cmux-cli.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Summary by cubic

Fixes Claude sessions failing with MODULE_NOT_FOUND once macOS purges $TMPDIR under long-lived sessions (issue #12022).

The restore preload every Node child loads via NODE_OPTIONS now lives in ~/.cmuxterm/cmux-claude-node-options/ instead of $TMPDIR.

  • Both the bash wrapper and the Swift claude-teams/omc launchers write it there with 0700 permissions and atomic temp-and-rename.
  • --require is quoted when the path contains whitespace, and session resume now tokenizes NODE_OPTIONS the way Node does so the quoted preload is stripped instead of leaving a broken fragment.
  • Adds a regression test that purges $TMPDIR mid-session and verifies a Node child still restores the user's original NODE_OPTIONS; a bypass-flag check dropped during that rewrite was restored.

Summary by CodeRabbit

  • Bug Fixes
    • Claude launches now retain user-provided Node.js options if temporary files are cleared during a session.
    • Preload paths containing spaces are handled correctly, and unrelated Node.js options are preserved when resuming a Claude session.
    • Restore files are kept in a private, restricted-access location under the home directory, so temporary-directory cleanup won’t interrupt launches.