Repository navigation
Sanitize Save Workspace Layout agent commands through the shared sanitizer; fail loudly on un-replayable commands; fix save dialog layout #7633
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
8f91d6b
Add failing regression tests for layout capture of cmux-injected code…
austinywang 160bb0c
Sanitize captured agent commands via shared sanitizer; fail loudly on…
austinywang 9e079ee
Strip only cmux's own codex hook flags; gate node/bun unwrap on node_…
austinywang 0398d3c
Gate node/bun agent unwrap on cmux wrapper-injected hook markers
austinywang e9573a9
Derive unwrap agent name from cmux marker for package entrypoints lik…
austinywang 266668c
Move file-local codex launch helpers to file scope
austinywang a920f3f
Require the marker agent's package path for basename-less unwrap fall…
austinywang 8de4c82
Replay marker-bearing native agent launches through the bare name
austinywang ce1da96
Fix save-dialog disclosure text views rendering blank
austinywang f25045b
Split save-dialog accessory regression test into its own wired test file
austinywang 7d6ca65
Normalize project.pbxproj
austinywang d815bca
Merge origin/main into save layout commands
austinywang 0bdb341
Fix save layout review findings
austinywang f354c9d
Tighten save layout wrapper proof
austinywang 1138177
Merge remote-tracking branch 'origin/main' into issue-7631-save-layou…
austinywang 6b4cce1
Add Claude session wrapper regression coverage
austinywang 8aaea83
Recognize Claude wrapper session marker
austinywang 7a84a1b
Use instance unwrapper for runtime agent argv
austinywang 1699b18
Add Claude wrapper identity regression coverage
austinywang ad693db
Require structured Claude wrapper marker
austinywang 5c7035b
Update Claude wrapper app test fixture
austinywang 3de96e6
Route marker-backed Codex replays through wrapper
austinywang 22759b2
Split JavaScript runtime launch unwrapper
austinywang 54ccbc7
Trim action saver test file under guard budget
austinywang 4346df8
Preserve explicit Codex executable with hook config
austinywang a0bfeb5
Sanitize node-hosted Codex resume captures
austinywang 956edff
Preserve hook config for captured agent paths
austinywang 1baeb11
Move Claude launch preservation out of sanitizer namespace
austinywang a7fa722
Strip cmux hook payloads from saved layout commands
austinywang a9eef49
Route stripped saved-layout agents through wrappers
austinywang 3288aca
Preserve captured agent executables for saved layouts
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
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
227 changes: 227 additions & 0 deletions
227
Packages/macOS/CMUXAgentLaunch/Sources/CMUXAgentLaunch/AgentLaunchSanitizerCodexLaunch.swift
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,227 @@ | ||
| import Foundation | ||
|
|
||
| extension AgentLaunchSanitizer { | ||
| static func preservedCodexLaunchArguments(args: [String]) -> [String]? { | ||
| let args = removingCmuxInjectedCodexHookArguments(args) | ||
| if let forkCommand = codexForkCommand(in: args) { | ||
| return CodexForkLaunchCapture( | ||
| args: args, | ||
| forkIndex: forkCommand.forkIndex, | ||
| sessionIndex: forkCommand.sessionIndex, | ||
| preserveOptions: preserveOptions | ||
| ).arguments() | ||
| } | ||
| return preservedArguments(kind: "codex", args: args) | ||
| } | ||
|
|
||
| static func removingCmuxInjectedCodexHookArguments(_ args: [String]) -> [String] { | ||
| guard containsCmuxInjectedCodexHookConfig(args) else { return args } | ||
| var result: [String] = [] | ||
| var index = 0 | ||
| while index < args.count { | ||
| let arg = args[index] | ||
| if isCmuxInjectedCodexHookConfigOption(arg) { | ||
| index += 1 | ||
| continue | ||
| } | ||
| if (arg == "-c" || arg == "--config"), | ||
| index + 1 < args.count, | ||
| isCmuxInjectedCodexHookConfigValue(args[index + 1]) { | ||
| index += 2 | ||
| continue | ||
| } | ||
| if arg == "--enable", index + 1 < args.count, args[index + 1] == "hooks" { | ||
| index += 2 | ||
| continue | ||
| } | ||
| if arg == "--enable=hooks" || arg == "--dangerously-bypass-hook-trust" { | ||
| index += 1 | ||
| continue | ||
| } | ||
| result.append(arg) | ||
| index += 1 | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| /// Unwraps a node/bun-hosted known agent to a bare agent executable argv. | ||
| /// | ||
| /// Captured foreground argv may look like `node .../bin/codex <flags>` when | ||
| /// cmux launched the agent through a JavaScript runtime wrapper. Returning a | ||
| /// bare executable name such as `codex` deliberately routes replay through | ||
| /// the per-surface PATH shim and cmux wrapper, so hooks are re-injected fresh | ||
| /// instead of persisting the runtime script path. | ||
| public static func unwrappedJavaScriptRuntimeAgentArgv( | ||
| _ argv: [String], | ||
| isKnownAgentExecutableName: (String) -> Bool | ||
| ) -> [String]? { | ||
| guard let executable = argv.first else { return nil } | ||
| let runtimeName = (executable as NSString).lastPathComponent.lowercased() | ||
| guard runtimeName == "node" || runtimeName == "bun", | ||
| let scriptIndex = javaScriptRuntimeScriptArgumentIndex(argv) else { | ||
| return nil | ||
| } | ||
| let scriptName = (argv[scriptIndex] as NSString).lastPathComponent | ||
| let matchedName: String | ||
| if isKnownAgentExecutableName(scriptName) { | ||
| matchedName = scriptName | ||
| } else if let strippedName = scriptName.removingSingleJavaScriptExtension(), | ||
| isKnownAgentExecutableName(strippedName) { | ||
| matchedName = strippedName | ||
| } else { | ||
| return nil | ||
| } | ||
| return [matchedName] + Array(argv.dropFirst(scriptIndex + 1)) | ||
| } | ||
|
|
||
| struct CodexForkCommand { | ||
| let forkIndex: Int | ||
| let sessionIndex: Int | ||
| } | ||
|
|
||
| static func codexForkCommand(in args: [String]) -> CodexForkCommand? { | ||
| var index = 0 | ||
| while index < args.count { | ||
| let arg = args[index] | ||
| if arg == "--" { | ||
| return nil | ||
| } | ||
| if !isOptionToken(arg) || arg == "-" { | ||
| guard arg == "fork", | ||
| let sessionIndex = codexForkCommandSessionIndex(args, forkIndex: index) else { | ||
| return nil | ||
| } | ||
| return CodexForkCommand(forkIndex: index, sessionIndex: sessionIndex) | ||
| } | ||
| let width = optionWidth(args, index: index, policy: codexPolicy) | ||
| if codexPolicy.variadicOptions.contains(arg) { | ||
| let end = min(args.count, index + width) | ||
| if index + 2 < end { | ||
| for candidateIndex in (index + 2)..<end where args[candidateIndex] == "fork" { | ||
| if let sessionIndex = codexForkCommandSessionIndex(args, forkIndex: candidateIndex) { | ||
| return CodexForkCommand(forkIndex: candidateIndex, sessionIndex: sessionIndex) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| index += width | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| static func codexForkCommandSessionIndex(_ args: [String], forkIndex: Int) -> Int? { | ||
| var index = forkIndex + 1 | ||
| while index < args.count { | ||
| let argument = args[index] | ||
| if argument == "--" { | ||
| return nil | ||
| } | ||
| if !argument.hasPrefix("-") || argument == "-" { | ||
| return looksLikeCodexSessionIdentifier(argument) ? index : nil | ||
| } | ||
| let width = optionWidth(args, index: index, policy: codexPolicy) | ||
| if codexPolicy.variadicOptions.contains(argument) { | ||
| let end = min(args.count, index + width) | ||
| if index + 2 < end { | ||
| for candidateIndex in (index + 2)..<end { | ||
| if looksLikeCodexSessionIdentifier(args[candidateIndex]) { | ||
| return candidateIndex | ||
| } | ||
| } | ||
| } | ||
| } | ||
| index += width | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| static func looksLikeCodexSessionIdentifier(_ value: String) -> Bool { | ||
| let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| guard trimmed.count >= 20 else { return false } | ||
| if trimmed.hasPrefix("019") { | ||
| return true | ||
| } | ||
| let allowed = CharacterSet(charactersIn: "0123456789abcdefABCDEF-") | ||
| return trimmed.unicodeScalars.allSatisfy { allowed.contains($0) } && trimmed.contains("-") | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| private static func containsCmuxInjectedCodexHookConfig(_ args: [String]) -> Bool { | ||
| var index = 0 | ||
| while index < args.count { | ||
| let arg = args[index] | ||
| if isCmuxInjectedCodexHookConfigOption(arg) { | ||
| return true | ||
| } | ||
| if (arg == "-c" || arg == "--config"), | ||
| index + 1 < args.count, | ||
| isCmuxInjectedCodexHookConfigValue(args[index + 1]) { | ||
| return true | ||
| } | ||
| index += 1 | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| private static func isCmuxInjectedCodexHookConfigOption(_ arg: String) -> Bool { | ||
| for prefix in ["-c=", "--config="] where arg.hasPrefix(prefix) { | ||
| return isCmuxInjectedCodexHookConfigValue(String(arg.dropFirst(prefix.count))) | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| private static func isCmuxInjectedCodexHookConfigValue(_ value: String) -> Bool { | ||
| value.hasPrefix("hooks.") && value.contains("cmux-codex-hook") | ||
| } | ||
|
|
||
| private static func javaScriptRuntimeScriptArgumentIndex(_ argv: [String]) -> Int? { | ||
| var index = 1 | ||
| while index < argv.count { | ||
| let argument = argv[index] | ||
| if argument == "--" { | ||
| let nextIndex = index + 1 | ||
| return nextIndex < argv.count ? nextIndex : nil | ||
| } | ||
| if argument.hasPrefix("-") { | ||
| if nodeOptionConsumesScript(argument) { | ||
| return nil | ||
| } | ||
| index += 1 + nodeOptionValueCount(argument) | ||
| continue | ||
| } | ||
| return index | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private static func nodeOptionConsumesScript(_ argument: String) -> Bool { | ||
| let option = argument.split(separator: "=", maxSplits: 1).first.map(String.init) ?? argument | ||
| switch option { | ||
| case "-e", "--eval", "-p", "--print", "-c", "--check": | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| private static func nodeOptionValueCount(_ argument: String) -> Int { | ||
| if argument.contains("=") { | ||
| return 0 | ||
| } | ||
| switch argument { | ||
| case "-r", "--require", "--import", "--loader", "--experimental-loader", | ||
| "--conditions", "-C", "--title": | ||
| return 1 | ||
| default: | ||
| return 0 | ||
| } | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| private extension String { | ||
| func removingSingleJavaScriptExtension() -> String? { | ||
| for suffix in [".js", ".mjs", ".cjs"] where hasSuffix(suffix) { | ||
| return String(dropLast(suffix.count)) | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a Codex argv contains both cmux-injected hook config and a user-owned hook config, the cmux marker makes this branch remove
--enable hooksglobally while the user hook config is preserved. The saved command can then replay with-c hooks...but without enabling hooks, so the user's hook no longer runs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 9e079ee: the stripper now removes exactly one instance of --enable hooks and one --dangerously-bypass-hook-trust (the single pair the cmux wrapper splices in alongside its marker configs), so a user's own enable flag and hooks.* config survive and stay enabled on replay. Regression test added: 'Keeps user hook enabling flags when cmux injection is stripped'.
— Claude Code