Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
34 changes: 30 additions & 4 deletions Sources/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ class TabManager: ObservableObject {
let selectedTabId: UUID?
let selectedTabWasPinned: Bool
let preferredWorkingDirectory: String?
let inheritedTerminalConfig: ghostty_surface_config_s?
let inheritedTerminalFontPoints: Float?
}
private var agentPIDSweepTimer: DispatchSourceTimer?
private var workspaceGitMetadataPollTimer: DispatchSourceTimer?
Expand Down Expand Up @@ -1218,7 +1218,9 @@ class TabManager: ObservableObject {
sentryBreadcrumb("workspace.create", data: ["tabCount": nextTabCount])
let explicitWorkingDirectory = normalizedWorkingDirectory(overrideWorkingDirectory)
let workingDirectory = explicitWorkingDirectory ?? snapshot.preferredWorkingDirectory
let inheritedConfig = snapshot.inheritedTerminalConfig
let inheritedConfig = workspaceCreationConfigTemplate(
inheritedTerminalFontPoints: snapshot.inheritedTerminalFontPoints
)
// Resolve placement against the pre-creation snapshot before Workspace init
// boots terminal state. The ssh/new-workspace path can otherwise crash while
// reading @Published placement state from existing workspaces mid-creation.
Expand Down Expand Up @@ -2191,7 +2193,7 @@ class TabManager: ObservableObject {
selectedTabId: currentSelectedTabId,
selectedTabWasPinned: selectedTabSnapshot?.isPinned ?? false,
preferredWorkingDirectory: preferredWorkingDirectoryForNewTab(workspace: selectedWorkspace),
inheritedTerminalConfig: inheritedTerminalConfigForNewWorkspace(workspace: selectedWorkspace)
inheritedTerminalFontPoints: inheritedTerminalFontPointsForNewWorkspace(workspace: selectedWorkspace)
)
}

Expand Down Expand Up @@ -2251,7 +2253,7 @@ class TabManager: ObservableObject {
inheritedTerminalConfigForNewWorkspace(workspace: selectedWorkspace)
}

private func inheritedTerminalConfigForNewWorkspace(
func inheritedTerminalConfigForNewWorkspace(
workspace: Workspace?
) -> ghostty_surface_config_s? {
if let panel = terminalPanelForWorkspaceConfigInheritanceSource(workspace: workspace),
Expand All @@ -2271,6 +2273,30 @@ class TabManager: ObservableObject {
return nil
}

func inheritedTerminalFontPointsForNewWorkspace(
workspace: Workspace?
) -> Float? {
guard let inheritedConfig = inheritedTerminalConfigForNewWorkspace(workspace: workspace),
inheritedConfig.font_size > 0 else {
return nil
}
return inheritedConfig.font_size
}

func workspaceCreationConfigTemplate(
inheritedTerminalFontPoints: Float?
) -> ghostty_surface_config_s? {
guard let inheritedTerminalFontPoints, inheritedTerminalFontPoints > 0 else {
return nil
}
// ghostty_surface_config_s can carry raw C pointers owned by the source surface.
// New workspace creation only needs the inherited zoom level, so rebuild a clean
// config instead of snapshotting pointer-backed fields across workspace creation.
var config = ghostty_surface_config_new()
config.font_size = inheritedTerminalFontPoints
return config
}

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 Helper functions unnecessarily exposed as internal

inheritedTerminalFontPointsForNewWorkspace and workspaceCreationConfigTemplate are both declared func (internal visibility) but neither is overridden anywhere — the test subclass only overrides inheritedTerminalConfigForNewWorkspace (which does need to be internal for the @testable import override). These two helpers could be private func to keep the internal surface of TabManager minimal.

Suggested change
func inheritedTerminalFontPointsForNewWorkspace(
workspace: Workspace?
) -> Float? {
guard let inheritedConfig = inheritedTerminalConfigForNewWorkspace(workspace: workspace),
inheritedConfig.font_size > 0 else {
return nil
}
return inheritedConfig.font_size
}
func workspaceCreationConfigTemplate(
inheritedTerminalFontPoints: Float?
) -> ghostty_surface_config_s? {
guard let inheritedTerminalFontPoints, inheritedTerminalFontPoints > 0 else {
return nil
}
// ghostty_surface_config_s can carry raw C pointers owned by the source surface.
// New workspace creation only needs the inherited zoom level, so rebuild a clean
// config instead of snapshotting pointer-backed fields across workspace creation.
var config = ghostty_surface_config_new()
config.font_size = inheritedTerminalFontPoints
return config
}
private func inheritedTerminalFontPointsForNewWorkspace(
workspace: Workspace?
) -> Float? {
guard let inheritedConfig = inheritedTerminalConfigForNewWorkspace(workspace: workspace),
inheritedConfig.font_size > 0 else {
return nil
}
return inheritedConfig.font_size
}
private func workspaceCreationConfigTemplate(
inheritedTerminalFontPoints: Float?
) -> ghostty_surface_config_s? {
guard let inheritedTerminalFontPoints, inheritedTerminalFontPoints > 0 else {
return nil
}
// ghostty_surface_config_s can carry raw C pointers owned by the source surface.
// New workspace creation only needs the inherited zoom level, so rebuild a clean
// config instead of snapshotting pointer-backed fields across workspace creation.
var config = ghostty_surface_config_new()
config.font_size = inheritedTerminalFontPoints
return config
}

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!


private func normalizedWorkingDirectory(_ directory: String?) -> String? {
guard let directory else { return nil }
let normalized = normalizeDirectory(directory)
Expand Down
99 changes: 99 additions & 0 deletions cmuxTests/WorkspaceUnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,105 @@ final class WorkspaceCreationPlacementTests: XCTestCase {
}


final class WorkspaceCreationConfigSanitizationTests: XCTestCase {
private final class UnsafeConfigSnapshotTabManager: TabManager {
private var retainedCStringPointers: [UnsafeMutablePointer<CChar>] = []
private var retainedEnvVars: UnsafeMutablePointer<ghostty_env_var_s>?
private var injectedConfig: ghostty_surface_config_s?
var capturedConfigTemplate: ghostty_surface_config_s?

func installInjectedConfig(fontSize: Float) {
invalidateInjectedConfig()

let workingDirectory = strdup("/tmp/cmux-workspace-snapshot")
let command = strdup("echo snapshot")
let envKey = strdup("CMUX_INHERITED_ENV")
let envValue = strdup("1")
let envVars = UnsafeMutablePointer<ghostty_env_var_s>.allocate(capacity: 1)
envVars.initialize(
to: ghostty_env_var_s(
key: UnsafePointer(envKey),
value: UnsafePointer(envValue)
)
)

retainedCStringPointers = [workingDirectory, command, envKey, envValue].compactMap { $0 }
retainedEnvVars = envVars

var config = ghostty_surface_config_new()
config.font_size = fontSize
config.working_directory = UnsafePointer(workingDirectory)
config.command = UnsafePointer(command)
config.env_vars = envVars
config.env_var_count = 1
injectedConfig = config
}

override func didCaptureWorkspaceCreationSnapshot() {
invalidateInjectedConfig()
}

override func inheritedTerminalConfigForNewWorkspace(
workspace: Workspace?
) -> ghostty_surface_config_s? {
injectedConfig ?? super.inheritedTerminalConfigForNewWorkspace(workspace: workspace)
}

override func makeWorkspaceForCreation(
title: String,
workingDirectory: String?,
portOrdinal: Int,
configTemplate: ghostty_surface_config_s?,
initialTerminalCommand: String?,
initialTerminalEnvironment: [String: String]
) -> Workspace {
capturedConfigTemplate = configTemplate
// The assertion is on the captured template; avoid dereferencing any injected
// pointer-backed fields here so the test can safely detect unsanitized state.
return super.makeWorkspaceForCreation(
title: title,
workingDirectory: workingDirectory,
portOrdinal: portOrdinal,
configTemplate: nil,
initialTerminalCommand: initialTerminalCommand,
initialTerminalEnvironment: initialTerminalEnvironment
)
}

private func invalidateInjectedConfig() {
injectedConfig = nil
if let retainedEnvVars {
retainedEnvVars.deinitialize(count: 1)
retainedEnvVars.deallocate()
self.retainedEnvVars = nil
}
for pointer in retainedCStringPointers {
free(pointer)
}
retainedCStringPointers.removeAll(keepingCapacity: false)
}
}

@MainActor
func testAddWorkspacePassesSanitizedInheritedConfigTemplateAfterSourceBuffersAreReleased() {
let manager = UnsafeConfigSnapshotTabManager()
manager.installInjectedConfig(fontSize: 19)
_ = manager.addWorkspace()

guard let capturedConfig = manager.capturedConfigTemplate else {
XCTFail("Expected captured config template for new workspace")
return
}

XCTAssertEqual(capturedConfig.font_size, 19, accuracy: 0.001)
XCTAssertNil(capturedConfig.working_directory)
XCTAssertNil(capturedConfig.command)
XCTAssertNil(capturedConfig.env_vars)
XCTAssertEqual(capturedConfig.env_var_count, 0)
}
}


final class WorkspaceTabColorSettingsTests: XCTestCase {
func testNormalizedHexAcceptsAndNormalizesValidInput() {
XCTAssertEqual(WorkspaceTabColorSettings.normalizedHex("#abc123"), "#ABC123")
Expand Down
Loading