Keep Claude NODE_OPTIONS restore module out of temp cleanup

Closed
AU
2mo ago
+2392-268

Summary

  • Move the Claude NODE_OPTIONS restore preload from macOS temp storage into Library/Application Support/cmux/node-options for the shell wrapper, Swift CLI path, and remote daemon launcher.
  • Recreate restore-node-options.cjs every time NODE_OPTIONS is computed, so a missing/deleted preload repairs itself on the next Claude launch.
  • Keep resume/env sanitizers aware of both the old temp path and the new durable path so persisted commands do not retain cmux-injected flags.

Why

The bug is a lifecycle mismatch: child processes inherit NODE_OPTIONS=--require=<file>, but the old file lived under $TMPDIR, which macOS can clean while the session is still alive. Once the file disappeared, every child Node process failed to load it and flooded the session.

Tests

  • Added a Swift regression test in the first commit that deletes the restore file, calls the wrapper again, and asserts the file is recreated under Application Support rather than temp storage.
  • bash -n Resources/bin/claude
  • python3 -m py_compile tests/test_claude_wrapper_hooks.py tests/test_cli_claude_teams_env.py
  • git diff --check
  • Manual wrapper smoke with the launched dev app: NODE_OPTIONS points at Library/Application Support/cmux/node-options/restore-node-options.cjs, Node runtime sees restored NODE_OPTIONS, and deleting the file is repaired on the next wrapper invocation.
  • Built and launched with ./scripts/reload.sh --tag issue-3512-node-options-tmpdir --launch.

Note: local Swift tests were not run per repo policy; CI should run them.

Closes #3512


[!NOTE]
Medium Risk
Touches Claude launch env handling across Swift CLI, shell wrapper, and remote daemon; mistakes could break agent startup or propagate incorrect NODE_OPTIONS in user shells. Changes are well-covered by new Swift/Go/Python regression tests but still impact a critical launch path.

Overview
Moves the Claude NODE_OPTIONS restore preload from temp directories to a durable per-user location (~/Library/Application Support/cmux/node-options), with a validated, secure temp fallback and optional override via CMUX_NODE_OPTIONS_RESTORE_DIR.

Centralizes and hardens NODE_OPTIONS merging/sanitization: quote-aware tokenization, correct quoting of --require paths (spaces/backslashes), stripping only cmux-injected restore --require entries (legacy + durable) and the paired --max-old-space-size=4096, and preserving user-specified flags (including user heap caps). CMUX_ORIGINAL_NODE_OPTIONS* is now set only when the restored value is non-empty.

Introduces a new Swift package CMUXNodeOptions used by the CLI and CMUXAgentLaunch, updates resume/env sanitizers accordingly, and adds focused regression tests (Swift unit tests plus expanded Go and Python wrapper/claude-teams tests) for directory selection, stale preload cleanup, and tricky quoting/escaping cases.

Reviewed by Cursor Bugbot for commit 4a3aac386b31494a4e63ae87aa2eb9dce7d16284. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Moves the Claude NODE_OPTIONS restore preload out of temp into a durable per‑user path and rebuilds it on each launch. Aligns quote‑aware sanitization and restore normalization across Swift, Bash, and Go so only cmux‑injected flags are removed and user options are preserved (addresses #3512).

  • Bug Fixes
    • Durable per‑user restore dir with secure temp fallback; normalized CMUX_NODE_OPTIONS_RESTORE_DIR to a managed suffix; hardened validation; component‑aware detection of current/legacy paths; remote daemon uses user config dir; resilient to bad/missing TMPDIR/HOME.
    • Shared, quote‑aware sanitizer via CMUXNodeOptions, Bash, and Go: preserves spaces/double quotes/apostrophes/backslashes; re‑quotes on join; strips cmux --require (legacy + durable) and only the immediately paired --max-old-space-size=4096; preserves user heap caps; Bash 3.2 empty‑array guards; sets CMUX_ORIGINAL_NODE_OPTIONS* only when the normalized original is non‑empty; guard aligned across CLI and resume.

Summary by CodeRabbit

  • New Features

    • Restore preload now lives in a stable per-user Application Support location and can be overridden via CMUX_NODE_OPTIONS_RESTORE_DIR.
  • Bug Fixes

    • Safer tokenization, quoting and reconstruction of NODE_OPTIONS preserves existing quoted require paths, normalizes heap-cap flags, and prevents tmpdir-related leaks while reliably stripping stale injected preload entries. Restore injection now works even when TMPDIR is invalid.
  • Tests

    • Added and expanded tests for quoted paths, durable restore location, merging/token behavior, and resume-time NODE_OPTIONS handling.