Skip to content
Closed
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
119 changes: 119 additions & 0 deletions Packages/macOS/CmuxGhosttyRenderService/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// swift-tools-version: 6.0

import Foundation
import PackageDescription

let packageDirectory = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
let ghosttyArchivePath = packageDirectory
.appendingPathComponent(
"../../../GhosttyKit.xcframework/macos-arm64_x86_64/ghostty-internal.a"
)
.standardizedFileURL
.path

let package = Package(
name: "CmuxGhosttyRenderService",
platforms: [
.macOS(.v14),
],
products: [
.library(
name: "CmuxGhosttyRenderClient",
targets: ["CmuxGhosttyRenderClient"]
),
.library(
name: "CmuxGhosttyRenderWorker",
targets: ["CmuxGhosttyRenderWorker"]
),
.executable(
name: "cmux-ghostty-render-fixture",
targets: ["cmux-ghostty-render-fixture"]
),
.executable(
name: "cmux-ghostty-render-worker-test-host",
targets: ["cmux-ghostty-render-worker-test-host"]
),
],
dependencies: [
.package(path: "../CmuxTerminalRenderTransport"),
],
targets: [
.target(
name: "CmuxGhosttyRenderClient",
dependencies: [
.product(
name: "CmuxTerminalRenderTransport",
package: "CmuxTerminalRenderTransport"
),
],
swiftSettings: [
.swiftLanguageMode(.v6),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("InternalImportsByDefault"),
]
),
.systemLibrary(
name: "CmuxGhosttyRenderWorkerGhosttyKit",
path: "Sources/GhosttyKit"
),
.target(
name: "CmuxGhosttyRenderWorker",
dependencies: [
.product(
name: "CmuxTerminalRenderTransport",
package: "CmuxTerminalRenderTransport"
),
"CmuxGhosttyRenderWorkerGhosttyKit",
],
swiftSettings: [
.swiftLanguageMode(.v6),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("InternalImportsByDefault"),
],
linkerSettings: [
// The Ghostty XCFramework's macOS archive lacks SwiftPM's
// required `lib` prefix. Import its headers through the local
// shim target and link the same archive explicitly.
.unsafeFlags([ghosttyArchivePath]),
.linkedLibrary("c++"),
.linkedFramework("Metal"),
.linkedFramework("QuartzCore"),
.linkedFramework("IOSurface"),
.linkedFramework("UniformTypeIdentifiers"),
.linkedFramework("Carbon"),
]
),
.executableTarget(
name: "cmux-ghostty-render-fixture",
dependencies: [
.product(
name: "CmuxTerminalRenderTransport",
package: "CmuxTerminalRenderTransport"
),
],
swiftSettings: [
.swiftLanguageMode(.v6),
]
),
.executableTarget(
name: "cmux-ghostty-render-worker-test-host",
dependencies: ["CmuxGhosttyRenderWorker"],
swiftSettings: [
.swiftLanguageMode(.v6),
]
),
.testTarget(
name: "CmuxGhosttyRenderClientTests",
dependencies: [
"CmuxGhosttyRenderClient",
"cmux-ghostty-render-fixture",
"cmux-ghostty-render-worker-test-host",
],
swiftSettings: [
.swiftLanguageMode(.v6),
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("InternalImportsByDefault"),
]
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public import CmuxTerminalRenderTransport
internal import Dispatch

/// Thread-safe, totally ordered ingress for terminal renderer commands.
///
/// Ghostty PTY output arrives on I/O threads while AppKit mutations arrive on
/// the main actor. Both enter this one serial queue before the client actor
/// consumes them, preventing unstructured Tasks from reordering output,
/// resize, focus, and pointer changes.
public final class GhosttyRenderCommandSink: @unchecked Sendable {
private let queue = DispatchQueue(
label: "dev.cmux.ghostty-render.command-outbox",
qos: .userInitiated
)
private let continuation: AsyncStream<TerminalRenderWorkerCommand>.Continuation
let stream: AsyncStream<TerminalRenderWorkerCommand>

init() {
let pair = AsyncStream.makeStream(
of: TerminalRenderWorkerCommand.self,
bufferingPolicy: .unbounded
)
self.stream = pair.stream
self.continuation = pair.continuation
Comment on lines +18 to +24

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

The output/recovery path has several independent unbounded buffers, allowing worker stalls to become host OOMs.

  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderCommandSink.swift#L18-L24: replace the unbounded command stream with a byte-budgeted ingress.
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L40-L45: centralize pending output and subscriber backlog ownership.
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L69-L74: bound or coalesce high-frequency control ingress.
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L126-L133: give event subscribers an explicit bounded/coalesced policy.
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L207-L223: cap pending mutations and request an authoritative snapshot when ordered output exceeds that cap.
📍 Affects 2 files
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderCommandSink.swift#L18-L24 (this comment)
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L40-L45
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L69-L74
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L126-L133
  • Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderWorkerClient.swift#L207-L223
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@Packages/macOS/CmuxGhosttyRenderService/Sources/CmuxGhosttyRenderClient/GhosttyRenderCommandSink.swift`
around lines 18 - 24, Replace the unbounded command stream in
GhosttyRenderCommandSink.init with a byte-budgeted ingress. In
GhosttyRenderWorkerClient.swift, centralize pending output and subscriber
backlog ownership, bound or coalesce high-frequency control ingress, give event
subscribers an explicit bounded/coalesced policy, and cap pending mutations so
ordered output beyond the cap triggers an authoritative snapshot request; apply
these changes at lines 40-45, 69-74, 126-133, and 207-223 respectively.

Source: Coding guidelines

}

/// Adds one command to the global worker ordering lane without blocking
/// the caller on process I/O.
public func enqueue(_ command: TerminalRenderWorkerCommand) {
queue.async { [continuation] in
continuation.yield(command)
}
}

/// Ends the ordering lane during client shutdown.
func finish() {
queue.async { [continuation] in
continuation.finish()
}
}
}
Loading