Repository navigation
Keep Claude NODE_OPTIONS restore shim out of TMPDIR #12067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 45 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
ffb6d1d
test: reproduce Claude NODE_OPTIONS tmpdir purge
austinywang 8440946
fix: keep Claude NODE_OPTIONS restore shim durable
austinywang 6f41dfd
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang b079d3e
chore: normalize CLI source wiring
austinywang 1d7a358
test: clean up Claude wrapper regression harness
austinywang d692992
fix: restore legacy Claude preload paths
austinywang 5a06975
test: cover legacy Claude preload migration
austinywang e5ec22e
fix: preserve quoted Claude node options
austinywang 41a82ff
fix: localize Claude node options error
austinywang 989359a
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang a70322a
fix: place Claude error localization in catalog
austinywang a0bc896
fix: restore main build compatibility
austinywang 1c7bdeb
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang b909c31
fix(cloud): restore catalog rename compatibility APIs
austinywang 8635e54
fix: clear current main warning budget
austinywang 5af2d06
fix: correct browser payload mutability
austinywang 89959e7
Merge branch 'main' into issue-12022-wrapper-shim-tmpdir
austinywang b83b585
Merge branch 'main' into issue-12022-wrapper-shim-tmpdir
austinywang 348021c
Merge branch 'main' into issue-12022-wrapper-shim-tmpdir
austinywang 5017b6e
Merge branch 'main' into issue-12022-wrapper-shim-tmpdir
austinywang 8e73509
Merge branch 'main' into issue-12022-wrapper-shim-tmpdir
austinywang 1aaabcc
fix: wire local tmux helpers into app-host tests
austinywang 9f204cc
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang 6060750
fix: filter duplicate Claude restore preloads
austinywang 1afa1ee
fix: remove duplicate cli definitions after main extraction
austinywang 72f4796
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang 6c50790
fix: restore semantic hook delivery after merge
austinywang 2a1dc98
fix: expose shared CLI helpers across files
austinywang dde447a
fix: wire Claude node options into CLI target
austinywang 9902ade
fix: remove duplicate Claude node option helpers
austinywang d0fe0f7
fix: wire cloud rename compatibility into app target
austinywang d4e26db
fix: preserve unset NODE_OPTIONS during legacy migration
austinywang 250a4b2
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang 12737f0
chore: remove obsolete Swift warning budget entries
austinywang eb47397
chore: normalize Xcode project wiring
austinywang 91271ef
fix: restore semantic hook journal delivery
austinywang e236673
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang fa993cf
fix: preserve approval clears without request identity
austinywang 40877a2
fix: harden Claude hook fallback paths
austinywang 18e69b3
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang 201268e
ci: rerun required checks after hosted runner flakes
austinywang 58e39b8
Merge remote-tracking branch 'origin/main' into issue-12022-wrapper-s…
austinywang fc9fc6e
ci: grandfather pre-existing determinism findings
austinywang a6fc232
ci: install Node for semantic integration tests
austinywang cfb4826
test: pass explicit Node path to Xcode harness
austinywang e177006
ci: forward Node path to Xcode test runner
austinywang 0c36a75
test: keep OpenCode socket path short on macOS
austinywang 6820f8f
ci: install Node for app-host tests
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,366 @@ | ||
| import Darwin | ||
| import Foundation | ||
|
|
||
| extension CMUXCLI { | ||
| private static let claudeNodeOptionsRestoreModule = """ | ||
| const hadOriginalNodeOptions = process.env.CMUX_ORIGINAL_NODE_OPTIONS_PRESENT === "1"; | ||
| if (hadOriginalNodeOptions) { | ||
| process.env.NODE_OPTIONS = process.env.CMUX_ORIGINAL_NODE_OPTIONS ?? ""; | ||
| } else { | ||
| delete process.env.NODE_OPTIONS; | ||
| } | ||
| delete process.env.CMUX_ORIGINAL_NODE_OPTIONS; | ||
| delete process.env.CMUX_ORIGINAL_NODE_OPTIONS_PRESENT; | ||
| """ + "\n" | ||
|
|
||
| private struct ClaudeNodeOptionsCachePathError: LocalizedError { | ||
| let reason: String | ||
| let path: String | ||
|
|
||
| var errorDescription: String? { | ||
| String( | ||
| localized: "cli.claude.nodeOptionsRestoreError", | ||
| defaultValue: "Claude integration could not prepare its runtime environment." | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private struct CachePathState { | ||
| let owner: uid_t | ||
| let isSymbolicLink: Bool | ||
| } | ||
|
|
||
| private static let unsafeNodeOptionsPathCharacters = CharacterSet(charactersIn: "\\\"'") | ||
|
|
||
| private static func nodeOptionsPathIsSafe(_ path: String) -> Bool { | ||
| path.hasPrefix("/") | ||
| && path.rangeOfCharacter(from: unsafeNodeOptionsPathCharacters) == nil | ||
| } | ||
|
|
||
| private static func unquoteNodeOptionsPath(_ path: String) -> String { | ||
| guard path.count >= 2, | ||
| let first = path.first, | ||
| let last = path.last, | ||
| (first == "\"" && last == "\"") || (first == "'" && last == "'") else { | ||
| return path | ||
| } | ||
| return String(path.dropFirst().dropLast()) | ||
| } | ||
|
|
||
| private static func nodeOptionsTokens(_ existing: String) -> [String]? { | ||
| var tokens: [String] = [] | ||
| var current = "" | ||
| var quote: Character? | ||
| var escaped = false | ||
| var tokenStarted = false | ||
|
|
||
| for character in existing { | ||
| if escaped { | ||
| current.append(character) | ||
| escaped = false | ||
| tokenStarted = true | ||
| continue | ||
| } | ||
| if character == "\\" && quote != "'" { | ||
| current.append(character) | ||
| escaped = true | ||
| tokenStarted = true | ||
| continue | ||
| } | ||
| if let activeQuote = quote { | ||
| current.append(character) | ||
| if character == activeQuote { | ||
| quote = nil | ||
| } | ||
| tokenStarted = true | ||
| continue | ||
| } | ||
| if character == "'" || character == "\"" { | ||
| quote = character | ||
| current.append(character) | ||
| tokenStarted = true | ||
| } else if character.isWhitespace { | ||
| if tokenStarted { | ||
| tokens.append(current) | ||
| current = "" | ||
| tokenStarted = false | ||
| } | ||
| } else { | ||
| current.append(character) | ||
| tokenStarted = true | ||
| } | ||
| } | ||
|
|
||
| guard !escaped, quote == nil else { return nil } | ||
| if tokenStarted { | ||
| tokens.append(current) | ||
| } | ||
| return tokens | ||
| } | ||
|
|
||
| private static func cachePathState(at url: URL) throws -> CachePathState? { | ||
| var statValue = stat() | ||
| guard lstat(url.path, &statValue) == 0 else { | ||
| guard errno == ENOENT else { | ||
| throw NSError( | ||
| domain: NSPOSIXErrorDomain, | ||
| code: Int(errno), | ||
| userInfo: [NSFilePathErrorKey: url.path] | ||
| ) | ||
| } | ||
| return nil | ||
| } | ||
| return CachePathState( | ||
| owner: statValue.st_uid, | ||
| isSymbolicLink: (statValue.st_mode & mode_t(S_IFMT)) == mode_t(S_IFLNK) | ||
| ) | ||
| } | ||
|
|
||
| private static func validateCachePath(_ url: URL, expectedOwner: uid_t) throws { | ||
| guard let state = try cachePathState(at: url) else { return } | ||
| guard !state.isSymbolicLink else { | ||
| throw ClaudeNodeOptionsCachePathError(reason: "cache path is a symlink", path: url.path) | ||
| } | ||
| guard state.owner == expectedOwner else { | ||
| throw ClaudeNodeOptionsCachePathError( | ||
| reason: "cache path is owned by a different uid", | ||
| path: url.path | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private static func claudeNodeOptionsRestoreModuleRoot() throws -> URL { | ||
| let environment = ProcessInfo.processInfo.environment | ||
| guard let home = environment["HOME"], !home.isEmpty else { | ||
| throw ClaudeNodeOptionsCachePathError(reason: "HOME is unavailable", path: "") | ||
| } | ||
| let root = URL(fileURLWithPath: home, isDirectory: true) | ||
| .appendingPathComponent(".cmuxterm", isDirectory: true) | ||
| .appendingPathComponent("cmux-claude-node-options", isDirectory: true) | ||
| guard nodeOptionsPathIsSafe(root.path) else { | ||
| throw ClaudeNodeOptionsCachePathError(reason: "path is unsafe for --require", path: root.path) | ||
| } | ||
| return root | ||
| } | ||
|
|
||
| private func nodeOptionsRequirePaths(_ existing: String) -> [String] { | ||
| guard let tokens = Self.nodeOptionsTokens(existing) else { return [] } | ||
| var paths: [String] = [] | ||
| var index = 0 | ||
| while index < tokens.count { | ||
| let token = tokens[index] | ||
| if token == "--require" || token == "-r" { | ||
| if index + 1 < tokens.count { | ||
| paths.append(Self.unquoteNodeOptionsPath(tokens[index + 1])) | ||
| index += 2 | ||
| continue | ||
| } | ||
| } else if token.hasPrefix("--require=") { | ||
| paths.append(Self.unquoteNodeOptionsPath(String(token.dropFirst("--require=".count)))) | ||
| } else if token.hasPrefix("-r=") { | ||
| paths.append(Self.unquoteNodeOptionsPath(String(token.dropFirst("-r=".count)))) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| index += 1 | ||
| } | ||
| return paths | ||
| } | ||
|
|
||
| private func isClaudeNodeOptionsRestorePath(_ path: String) -> Bool { | ||
| path.hasSuffix("/cmux-claude-node-options/restore-node-options.cjs") | ||
| } | ||
|
|
||
| private func recreateLegacyNodeOptionsRestoreModules(excluding currentURL: URL) { | ||
| guard let existing = ProcessInfo.processInfo.environment["NODE_OPTIONS"] else { return } | ||
| let legacySuffix = "/cmux-claude-node-options/restore-node-options.cjs" | ||
| let expectedOwner = getuid() | ||
| for path in nodeOptionsRequirePaths(existing) { | ||
| guard path.hasSuffix(legacySuffix), | ||
| Self.nodeOptionsPathIsSafe(path), | ||
| path != currentURL.path else { continue } | ||
| let url = URL(fileURLWithPath: path, isDirectory: false) | ||
| let directory = url.deletingLastPathComponent() | ||
| do { | ||
| try Self.validateCachePath(directory, expectedOwner: expectedOwner) | ||
| try FileManager.default.createDirectory( | ||
| at: directory, | ||
| withIntermediateDirectories: true, | ||
| attributes: [.posixPermissions: 0o700] | ||
| ) | ||
| try Self.validateCachePath(directory, expectedOwner: expectedOwner) | ||
| if try Self.cachePathState(at: url) != nil { | ||
| try Self.validateCachePath(url, expectedOwner: expectedOwner) | ||
| continue | ||
| } | ||
| try writeShimIfChanged(Self.claudeNodeOptionsRestoreModule, to: url) | ||
| try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: url.path) | ||
| try Self.validateCachePath(url, expectedOwner: expectedOwner) | ||
| } catch { | ||
| continue | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| func createClaudeNodeOptionsRestoreModule() throws -> URL { | ||
| let root = try Self.claudeNodeOptionsRestoreModuleRoot() | ||
| try Self.validateCachePath(root, expectedOwner: getuid()) | ||
| try FileManager.default.createDirectory( | ||
| at: root, | ||
| withIntermediateDirectories: true, | ||
| attributes: [.posixPermissions: 0o700] | ||
| ) | ||
| try Self.validateCachePath(root, expectedOwner: getuid()) | ||
| try FileManager.default.setAttributes([.posixPermissions: 0o700], ofItemAtPath: root.path) | ||
|
|
||
| let restoreModuleURL = root.appendingPathComponent("restore-node-options.cjs", isDirectory: false) | ||
| try writeShimIfChanged(Self.claudeNodeOptionsRestoreModule, to: restoreModuleURL) | ||
| try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: restoreModuleURL.path) | ||
| recreateLegacyNodeOptionsRestoreModules(excluding: restoreModuleURL) | ||
| return restoreModuleURL | ||
| } | ||
|
|
||
| func mergedNodeOptions(existing: String?, restoreModulePath: String) -> String { | ||
| let escapedPath = restoreModulePath | ||
| .replacingOccurrences(of: "\\", with: "\\\\") | ||
| .replacingOccurrences(of: "\"", with: "\\\"") | ||
| let requireOption = "--require=\"\(escapedPath)\"" | ||
| let memoryOption = "--max-old-space-size=4096" | ||
| let cleanedExisting = cleanedNodeOptions(existing) | ||
| guard !cleanedExisting.isEmpty else { | ||
| return "\(requireOption) \(memoryOption)" | ||
| } | ||
| return "\(requireOption) \(memoryOption) \(cleanedExisting)" | ||
| } | ||
|
|
||
| func configureClaudeNodeOptionsEnvironment( | ||
| processEnvironment: [String: String], | ||
| restoreModuleURL: URL | ||
| ) { | ||
| switch processEnvironment["CMUX_ORIGINAL_NODE_OPTIONS_PRESENT"] { | ||
| case "1": | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS_PRESENT", "1", 1) | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS", processEnvironment["CMUX_ORIGINAL_NODE_OPTIONS"] ?? "", 1) | ||
| case "0": | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS_PRESENT", "0", 1) | ||
| unsetenv("CMUX_ORIGINAL_NODE_OPTIONS") | ||
| default: | ||
| if let existing = processEnvironment["NODE_OPTIONS"] { | ||
| let normalized = normalizedNodeOptionsForRestore(existing) | ||
| if !normalized.isEmpty || !nodeOptionsContainOnlyManagedRestoreModules(existing) { | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS_PRESENT", "1", 1) | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS", normalized, 1) | ||
| } else { | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS_PRESENT", "0", 1) | ||
| unsetenv("CMUX_ORIGINAL_NODE_OPTIONS") | ||
| } | ||
| } else { | ||
| setenv("CMUX_ORIGINAL_NODE_OPTIONS_PRESENT", "0", 1) | ||
| unsetenv("CMUX_ORIGINAL_NODE_OPTIONS") | ||
| } | ||
| } | ||
|
austinywang marked this conversation as resolved.
Resolved
|
||
| setenv( | ||
| "NODE_OPTIONS", | ||
| mergedNodeOptions( | ||
| existing: processEnvironment["NODE_OPTIONS"], | ||
| restoreModulePath: restoreModuleURL.path | ||
| ), | ||
| 1 | ||
| ) | ||
| } | ||
|
|
||
| private func cleanedNodeOptions(_ existing: String?) -> String { | ||
| guard let tokens = Self.nodeOptionsTokens(existing ?? "") else { return existing ?? "" } | ||
| guard !tokens.isEmpty else { return "" } | ||
|
|
||
| var filtered: [String] = [] | ||
| var index = 0 | ||
| while index < tokens.count { | ||
| let token = tokens[index] | ||
| if token == "--require" || token == "-r", | ||
| index + 1 < tokens.count, | ||
| isClaudeNodeOptionsRestorePath(Self.unquoteNodeOptionsPath(tokens[index + 1])) { | ||
| index += 2 | ||
| continue | ||
| } | ||
| if token.hasPrefix("--require=") || token.hasPrefix("-r=") { | ||
| let separator = token.hasPrefix("--require=") ? "--require=" : "-r=" | ||
| let path = Self.unquoteNodeOptionsPath(String(token.dropFirst(separator.count))) | ||
| if isClaudeNodeOptionsRestorePath(path) { | ||
| index += 1 | ||
| continue | ||
| } | ||
| } | ||
| if token == "--max-old-space-size" { | ||
| index += min(2, tokens.count - index) | ||
| continue | ||
| } | ||
| if token.hasPrefix("--max-old-space-size=") { | ||
| index += 1 | ||
| continue | ||
| } | ||
| filtered.append(token) | ||
| index += 1 | ||
| } | ||
| return filtered.joined(separator: " ") | ||
| } | ||
|
|
||
| private func nodeOptionsContainOnlyManagedRestoreModules(_ existing: String) -> Bool { | ||
| guard let tokens = Self.nodeOptionsTokens(existing), !tokens.isEmpty else { return false } | ||
| var foundRestoreModule = false | ||
| var index = 0 | ||
| while index < tokens.count { | ||
| let token = tokens[index] | ||
| if token == "--require" || token == "-r" { | ||
| guard index + 1 < tokens.count, | ||
| isClaudeNodeOptionsRestorePath(Self.unquoteNodeOptionsPath(tokens[index + 1])) else { | ||
| return false | ||
| } | ||
| foundRestoreModule = true | ||
| index += 2 | ||
| continue | ||
| } | ||
| if token.hasPrefix("--require=") || token.hasPrefix("-r=") { | ||
| let separator = token.hasPrefix("--require=") ? "--require=" : "-r=" | ||
| let path = Self.unquoteNodeOptionsPath(String(token.dropFirst(separator.count))) | ||
| guard isClaudeNodeOptionsRestorePath(path) else { return false } | ||
| foundRestoreModule = true | ||
| index += 1 | ||
| continue | ||
| } | ||
| return false | ||
| } | ||
| return foundRestoreModule | ||
| } | ||
|
|
||
| func normalizedNodeOptionsForRestore(_ existing: String) -> String { | ||
| guard let tokens = Self.nodeOptionsTokens(existing) else { return existing } | ||
| guard !tokens.isEmpty else { return "" } | ||
|
|
||
| var normalized: [String] = [] | ||
| var index = 0 | ||
| while index < tokens.count { | ||
| let token = tokens[index] | ||
| if token == "--require" || token == "-r", | ||
| index + 1 < tokens.count, | ||
| isClaudeNodeOptionsRestorePath(Self.unquoteNodeOptionsPath(tokens[index + 1])) { | ||
| index += 2 | ||
| continue | ||
| } | ||
| if token.hasPrefix("--require=") || token.hasPrefix("-r=") { | ||
| let separator = token.hasPrefix("--require=") ? "--require=" : "-r=" | ||
| let path = Self.unquoteNodeOptionsPath(String(token.dropFirst(separator.count))) | ||
| if isClaudeNodeOptionsRestorePath(path) { | ||
| index += 1 | ||
| continue | ||
| } | ||
| } | ||
| if token == "--max-old-space-size", index + 1 < tokens.count { | ||
| normalized.append("--max-old-space-size=\(tokens[index + 1])") | ||
| index += 2 | ||
| continue | ||
| } | ||
| normalized.append(token) | ||
| index += 1 | ||
| } | ||
| return normalized.joined(separator: " ") | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.