From 05fab0a8a0b69f86448eda784cee2b6250670eaa Mon Sep 17 00:00:00 2001 From: Aziz Albahar Date: Tue, 2 Jun 2026 15:16:44 -0500 Subject: [PATCH 1/2] Inject FocusLogStore instead of a shared singleton FocusLogStore self-vended a `static let shared`. AppDelegate now owns the single instance (next to updateLog), the in-AppDelegate diagnostic sites use it directly, and the GhosttyTerminalView/Workspace sites reach it via AppDelegate.shared?.focusLog, matching the UpdateLogStore de-singletoning. No behavior change. Co-Authored-By: Claude Opus 4.8 --- Sources/AppDelegate.swift | 13 +++++++------ Sources/GhosttyTerminalView.swift | 2 +- Sources/Update/UpdateLogStore.swift | 9 +++++---- Sources/Workspace.swift | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index 2417128d52e7..b9ce024b7cfe 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -866,6 +866,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent private var browserAddressBarBlurObserver: NSObjectProtocol? private var browserWebViewFirstResponderObserver: NSObjectProtocol? let updateLog = UpdateLogStore() + let focusLog = FocusLogStore() private lazy var updateController = UpdateController(log: updateLog) private lazy var titlebarAccessoryController = UpdateTitlebarAccessoryController(updateLog: updateLog) private let windowDecorationsController = WindowDecorationsController() @@ -1123,7 +1124,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent let mainWindowNumber = NSApp.mainWindow?.windowNumber ?? -1 let ws = workspaceId.map { String($0.uuidString.prefix(8)) } ?? "nil" let wd = workingDirectory.map { String($0.prefix(120)) } ?? "-" - FocusLogStore.shared.append( + focusLog.append( "cmdn.route phase=\(phase) src=\(source) reason=\(reason) eventWin=\(eventWindowNumber) eventNum=\(eventNumber) keyCode=\(eventKeyCode) chars=\(eventChars) keyWin=\(keyWindowNumber) mainWin=\(mainWindowNumber) activeTM=\(pointerString(tabManager)) chosen={\(summarizeContextForWorkspaceRouting(chosenContext))} ws=\(ws) wd=\(wd) contexts=[\(summarizeAllContextsForWorkspaceRouting())]" ) } @@ -7817,7 +7818,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent private func synchronizeShortcutRoutingContext(event: NSEvent) -> Bool { guard let context = preferredMainWindowContextForShortcutRouting(event: event) else { #if DEBUG - FocusLogStore.shared.append( + focusLog.append( "shortcut.route reason=no_context_no_fallback eventWin=\(event.windowNumber) keyCode=\(event.keyCode)" ) #endif @@ -7841,7 +7842,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent } #if DEBUG - FocusLogStore.shared.append( + focusLog.append( "shortcut.route reason=sync activeTM=\(pointerString(tabManager)) chosen={\(summarizeContextForWorkspaceRouting(context))}" ) #endif @@ -8590,12 +8591,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent pasteboard.setString(payload, forType: .string) } @objc func copyFocusLogs(_ sender: Any?) { - let logText = FocusLogStore.shared.snapshot() + let logText = focusLog.snapshot() let payload: String if logText.isEmpty { - payload = "No focus logs captured.\nLog file: \(FocusLogStore.shared.logPath())" + payload = "No focus logs captured.\nLog file: \(focusLog.logPath())" } else { - payload = logText + "\nLog file: \(FocusLogStore.shared.logPath())" + payload = logText + "\nLog file: \(focusLog.logPath())" } let pasteboard = NSPasteboard.general pasteboard.clearContents() diff --git a/Sources/GhosttyTerminalView.swift b/Sources/GhosttyTerminalView.swift index f8c664a7a319..4903192cab82 100644 --- a/Sources/GhosttyTerminalView.swift +++ b/Sources/GhosttyTerminalView.swift @@ -7488,7 +7488,7 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations { fileprivate static func focusLog(_ message: String) { guard focusDebugEnabled else { return } - FocusLogStore.shared.append(message) + AppDelegate.shared?.focusLog.append(message) NSLog("[FOCUSDBG] %@", message) } diff --git a/Sources/Update/UpdateLogStore.swift b/Sources/Update/UpdateLogStore.swift index 6611a9800af8..fa5189ae4fed 100644 --- a/Sources/Update/UpdateLogStore.swift +++ b/Sources/Update/UpdateLogStore.swift @@ -66,16 +66,17 @@ final class UpdateLogStore: UpdateLogging, @unchecked Sendable { } } -final class FocusLogStore { - static let shared = FocusLogStore() - +// @unchecked Sendable: all mutable state (`entries`) is confined to the serial `queue`; the other +// stored properties are immutable. Owned and injected by `AppDelegate` (see `AppDelegate.focusLog`) +// rather than self-vending a global, so its lifecycle has a single composition root. +final class FocusLogStore: @unchecked Sendable { private let queue = DispatchQueue(label: "cmux.focus.log") private var entries: [String] = [] private let maxEntries = 400 private let logURL: URL private let formatter: ISO8601DateFormatter - private init() { + init() { formatter = ISO8601DateFormatter() formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] let logsDir = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first diff --git a/Sources/Workspace.swift b/Sources/Workspace.swift index 1690686dac1d..5f252c99aae2 100644 --- a/Sources/Workspace.swift +++ b/Sources/Workspace.swift @@ -16043,7 +16043,7 @@ final class Workspace: Identifiable, ObservableObject { let pane = bonsplitController.focusedPaneId?.id.uuidString.prefix(5) ?? "nil" let triggerLabel = trigger == .terminalFirstResponder ? "firstResponder" : "standard" cmuxDebugLog("focus.panel panel=\(panelId.uuidString.prefix(5)) pane=\(pane) trigger=\(triggerLabel)") - FocusLogStore.shared.append( + AppDelegate.shared?.focusLog.append( "Workspace.focusPanel panelId=\(panelId.uuidString) focusedPane=\(pane) trigger=\(triggerLabel)" ) #endif @@ -18661,7 +18661,7 @@ extension Workspace: BonsplitDelegate { // When a pane is focused, focus its selected tab's panel guard let tab = controller.selectedTab(inPane: pane) else { return } #if DEBUG - FocusLogStore.shared.append( + AppDelegate.shared?.focusLog.append( "Workspace.didFocusPane paneId=\(pane.id.uuidString) tabId=\(tab.id) focusedPane=\(controller.focusedPaneId?.id.uuidString ?? "nil")" ) #endif From b1f952c69c2ef41a4313c195d0ead646a2fbd6b0 Mon Sep 17 00:00:00 2001 From: Aziz Albahar Date: Tue, 2 Jun 2026 15:25:52 -0500 Subject: [PATCH 2/2] Compile-time guard the FOCUSDBG NSLog in focusLog CodeRabbit's "Cmux Swift Logging" check flagged the NSLog in GhosttyNSView.focusLog as runtime-gated only. Wrap it in #if DEBUG so it is stripped from release builds, matching the repo debug-log requirement. Co-Authored-By: Claude Opus 4.8 --- Sources/GhosttyTerminalView.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/GhosttyTerminalView.swift b/Sources/GhosttyTerminalView.swift index 4903192cab82..80a09b63bb1c 100644 --- a/Sources/GhosttyTerminalView.swift +++ b/Sources/GhosttyTerminalView.swift @@ -7489,7 +7489,9 @@ class GhosttyNSView: NSView, NSUserInterfaceValidations { fileprivate static func focusLog(_ message: String) { guard focusDebugEnabled else { return } AppDelegate.shared?.focusLog.append(message) + #if DEBUG NSLog("[FOCUSDBG] %@", message) + #endif } weak var terminalSurface: TerminalSurface?