Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions CLI/cmux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1684,15 +1684,19 @@ struct CMUXCLI {

case "new-workspace":
let (commandOpt, rem0) = parseOption(commandArgs, name: "--command")
let (cwdOpt, remaining) = parseOption(rem0, name: "--cwd")
let (cwdOpt, rem1) = parseOption(rem0, name: "--cwd")
let (nameOpt, remaining) = parseOption(rem1, name: "--name")
if let unknown = remaining.first(where: { $0.hasPrefix("--") }) {
throw CLIError(message: "new-workspace: unknown flag '\(unknown)'. Known flags: --command <text>, --cwd <path>")
throw CLIError(message: "new-workspace: unknown flag '\(unknown)'. Known flags: --name <title>, --command <text>, --cwd <path>")
}
Comment on lines 1689 to 1691

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Reject unexpected positional arguments for new-workspace.

Line [1689] only rejects unknown flags, so stray positional tokens are silently ignored (e.g., cmux new-workspace --name Build Server drops Server). Please fail fast on unexpected non-flag args.

💡 Suggested fix
-            if let unknown = remaining.first(where: { $0.hasPrefix("--") }) {
+            let unexpectedArgs = remaining.filter { $0 != "--" }
+            if let unknown = unexpectedArgs.first(where: { $0.hasPrefix("--") }) {
                 throw CLIError(message: "new-workspace: unknown flag '\(unknown)'. Known flags: --name <title>, --command <text>, --cwd <path>")
             }
+            if let extra = unexpectedArgs.first(where: { !$0.hasPrefix("--") }) {
+                throw CLIError(message: "new-workspace: unexpected argument '\(extra)'")
+            }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if let unknown = remaining.first(where: { $0.hasPrefix("--") }) {
throw CLIError(message: "new-workspace: unknown flag '\(unknown)'. Known flags: --command <text>, --cwd <path>")
throw CLIError(message: "new-workspace: unknown flag '\(unknown)'. Known flags: --name <title>, --command <text>, --cwd <path>")
}
let unexpectedArgs = remaining.filter { $0 != "--" }
if let unknown = unexpectedArgs.first(where: { $0.hasPrefix("--") }) {
throw CLIError(message: "new-workspace: unknown flag '\(unknown)'. Known flags: --name <title>, --command <text>, --cwd <path>")
}
if let extra = unexpectedArgs.first(where: { !$0.hasPrefix("--") }) {
throw CLIError(message: "new-workspace: unexpected argument '\(extra)'")
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CLI/cmux.swift` around lines 1689 - 1691, The handler for the new-workspace
command currently only rejects unknown flags but ignores stray positional tokens
in the remaining array; update the logic around remaining to also detect any
elements that do not start with "--" (i.e., unexpected positional args) and
throw a CLIError with a clear message (similar to the existing flag error) for
new-workspace so inputs like "cmux new-workspace --name Build Server" fail
rather than silently drop "Server"; reference the existing remaining variable
and CLIError to implement this validation.

var params: [String: Any] = [:]
if let cwdOpt {
let resolved = resolvePath(cwdOpt)
params["cwd"] = resolved
}
if let nameOpt {
params["title"] = nameOpt
}
let response = try client.sendV2(method: "workspace.create", params: params)
let wsId = (response["workspace_ref"] as? String) ?? (response["workspace_id"] as? String) ?? ""
print("OK \(wsId)")
Expand Down Expand Up @@ -6335,16 +6339,18 @@ struct CMUXCLI {
"""
case "new-workspace":
return """
Usage: cmux new-workspace [--cwd <path>] [--command <text>]
Usage: cmux new-workspace [--name <title>] [--cwd <path>] [--command <text>]

Create a new workspace in the current window.

Flags:
--name <title> Set a custom name for the new workspace
--cwd <path> Set the working directory for the new workspace
--command <text> Send text+Enter to the new workspace after creation

Example:
cmux new-workspace
cmux new-workspace --name "Build Server"
cmux new-workspace --cwd ~/projects/myapp
cmux new-workspace --cwd . --command "npm test"
"""
Expand Down Expand Up @@ -11996,7 +12002,7 @@ struct CMUXCLI {
reorder-workspace --workspace <id|ref|index> (--index <n> | --before <id|ref|index> | --after <id|ref|index>) [--window <id|ref|index>]
workspace-action --action <name> [--workspace <id|ref|index>] [--title <text>] [--color <name|#hex>]
list-workspaces
new-workspace [--cwd <path>] [--command <text>]
new-workspace [--name <title>] [--cwd <path>] [--command <text>]
ssh <destination> [--name <title>] [--port <n>] [--identity <path>] [--ssh-option <opt>] [-- <remote-command-args>]
remote-daemon-status [--os <darwin|linux>] [--arch <arm64|amd64>]
new-split <left|right|up|down> [--workspace <id|ref>] [--surface <id|ref>] [--panel <id|ref>]
Expand Down
6 changes: 5 additions & 1 deletion Sources/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,7 @@ class TabManager: ObservableObject {

@discardableResult
func addWorkspace(
title: String? = nil,
workingDirectory overrideWorkingDirectory: String? = nil,
initialTerminalCommand: String? = nil,
initialTerminalEnvironment: [String: String] = [:],
Expand Down Expand Up @@ -1226,14 +1227,17 @@ class TabManager: ObservableObject {
let ordinal = Self.nextPortOrdinal
Self.nextPortOrdinal += 1
let newWorkspace = makeWorkspaceForCreation(
title: "Terminal \(nextTabCount)",
title: title ?? "Terminal \(nextTabCount)",
workingDirectory: workingDirectory,
portOrdinal: ordinal,
configTemplate: inheritedConfig,
initialTerminalCommand: initialTerminalCommand,
initialTerminalEnvironment: initialTerminalEnvironment
)
newWorkspace.owningTabManager = self
if title != nil {
newWorkspace.setCustomTitle(title)
}
Comment on lines +1238 to +1240

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.

P2 Use if let instead of != nil guard

setCustomTitle(_:) accepts String?, so passing the still-wrapped optional compiles fine — but it's non-idiomatic Swift and masks the intent. Prefer unwrapping explicitly so it's obvious you're passing a concrete value:

Suggested change
if title != nil {
newWorkspace.setCustomTitle(title)
}
if let title {
newWorkspace.setCustomTitle(title)
}

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

wireClosedBrowserTracking(for: newWorkspace)
if eagerLoadTerminal && !select {
requestBackgroundWorkspaceLoad(for: newWorkspace.id)
Expand Down
13 changes: 10 additions & 3 deletions Sources/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1673,7 +1673,7 @@ class TerminalController {
return listWorkspaces()

case "new_workspace":
return newWorkspace()
return newWorkspace(args)

case "new_split":
return newSplit(args)
Expand Down Expand Up @@ -3336,10 +3336,14 @@ class TerminalController {
cwd = nil
}

let requestedTitle = v2RawString(params, "title")?.trimmingCharacters(in: .whitespacesAndNewlines)
let title = (requestedTitle?.isEmpty == false) ? requestedTitle : nil

var newId: UUID?
let shouldFocus = v2FocusAllowed()
v2MainSync {
let ws = tabManager.addWorkspace(
title: title,
workingDirectory: cwd,
initialTerminalCommand: initialCommand,
initialTerminalEnvironment: initialEnv,
Expand Down Expand Up @@ -12036,13 +12040,16 @@ class TerminalController {
return result.isEmpty ? "No workspaces" : result
}

private func newWorkspace() -> String {
private func newWorkspace(_ args: String = "") -> String {
guard let tabManager = tabManager else { return "ERROR: TabManager not available" }

let trimmed = args.trimmingCharacters(in: .whitespacesAndNewlines)
let title: String? = trimmed.isEmpty ? nil : trimmed

var newTabId: UUID?
let focus = socketCommandAllowsInAppFocusMutations()
DispatchQueue.main.sync {
let workspace = tabManager.addTab(select: focus, eagerLoadTerminal: !focus)
let workspace = tabManager.addWorkspace(title: title, select: focus, eagerLoadTerminal: !focus)
newTabId = workspace.id
}
return "OK \(newTabId?.uuidString ?? "unknown")"
Expand Down