codeburn/mac/Sources/CodeBurnMenubar/Data/CapacityDockProviderCredential.swift
Aditya Vikram Singh 84fea9d550
feat(menubar): add native Capacity Dock (#1167)
* feat(menubar): add native capacity dock

* fix(menubar): harden dock interaction boundaries

* feat(menubar): polish native Capacity Dock for launch

- Flush top-edge docking via NSHostingView safe-area opt-out; shielding
  window level so the rail necks into the physical top like the system notch
- Liquid Glass legibility scrim so labels stay readable on light wallpapers
- Weekly-else-monthly headline window; unified severity colour bands
  (green <50 / yellow / orange / red) across rings and detail bars
- Squircle gauge + graphite theme defaults; auto-show connected
  subscriptions (up to 5) until the user edits the set, then respect it
- Capacity Dock Settings menu item; re-centre the settings window; info
  glyph for the About row
- Trim dead provider-credential override fields down to apiKey only
- Reconcile and extend tests for all of the above

---------

Co-authored-by: iamtoruk <hello@agentseal.org>
2026-08-29 00:37:30 -07:00

228 lines
8.6 KiB
Swift

import Foundation
/// User-entered overrides for a provider. Passive local/browser/CLI discovery
/// never gets copied here; this record exists only after an explicit Save.
struct CapacityDockProviderCredential: Codable, Equatable, Sendable {
var sourceMode: String = ProviderReferenceSourceMode.automatic.rawValue
var apiKey: String = ""
var resolvedSourceMode: ProviderReferenceSourceMode {
ProviderReferenceSourceMode(rawValue: sourceMode) ?? .automatic
}
var isEmpty: Bool {
apiKey.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
&& resolvedSourceMode == .automatic
}
/// Sanitized override values that a fetch adapter can project into its own
/// provider-specific config. The adapter owns that mapping; this record stays
/// CodeBurn-owned and provider-scoped by Keychain account.
var sanitizedOverride: CapacityDockProviderCredentialOverride {
CapacityDockProviderCredentialOverride(
sourceMode: resolvedSourceMode,
apiKey: cleaned(apiKey)
)
}
private func cleaned(_ value: String) -> String? {
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
}
struct CapacityDockProviderCredentialOverride: Equatable, Sendable {
var sourceMode: ProviderReferenceSourceMode
var apiKey: String?
}
enum CapacityDockProviderCredentialStoreError: LocalizedError, Equatable {
case timedOut
var errorDescription: String? {
"Keychain did not respond. Unlock your login keychain, then reopen this provider or re-enter its credential."
}
}
/// Bridges a *read* from blocking Security.framework into async UI work without
/// making the caller wait forever. Classic login-keychain IPC has no
/// cancellation API, so only idempotent reads use this timeout gate. Mutations
/// await their real completion and can therefore never report failure while a
/// late write or delete is still able to change state in the background.
private final class CredentialContinuationGate<Value: Sendable>: @unchecked Sendable {
private let lock = NSLock()
private var continuation: CheckedContinuation<Value, Error>?
init(_ continuation: CheckedContinuation<Value, Error>) {
self.continuation = continuation
}
func resume(with result: Result<Value, Error>) {
lock.lock()
guard let continuation else {
lock.unlock()
return
}
self.continuation = nil
lock.unlock()
continuation.resume(with: result)
}
}
enum CapacityDockProviderCredentialStore {
static let service = "org.agentseal.codeburn.menubar.provider.v1"
nonisolated(unsafe) static var keychainCache: any KeychainCredentialCaching = LiveKeychainCredentialCache()
nonisolated(unsafe) static var userDefaults = UserDefaults.standard
static func load(for providerID: String) throws -> CapacityDockProviderCredential {
guard let data = try keychainCache.read(service: service, account: providerID) else {
CapacityDockProviderCredentialPresence.set(false, for: providerID, defaults: userDefaults)
return CapacityDockProviderCredential()
}
let credential = try JSONDecoder().decode(CapacityDockProviderCredential.self, from: data)
CapacityDockProviderCredentialPresence.set(
!credential.isEmpty,
for: providerID,
defaults: userDefaults
)
return credential
}
static func save(_ credential: CapacityDockProviderCredential, for providerID: String) throws {
if credential.isEmpty {
try keychainCache.delete(service: service, account: providerID)
CapacityDockProviderCredentialPresence.set(false, for: providerID, defaults: userDefaults)
return
}
let data = try JSONEncoder().encode(credential)
try keychainCache.upsert(service: service, account: providerID, data: data)
CapacityDockProviderCredentialPresence.set(true, for: providerID, defaults: userDefaults)
}
static func remove(for providerID: String) throws {
try keychainCache.delete(service: service, account: providerID)
CapacityDockProviderCredentialPresence.set(false, for: providerID, defaults: userDefaults)
}
/// Keychain IPC can wait on securityd even when authentication UI is
/// suppressed. Keep that work off the main actor so opening Settings,
/// changing providers, and dock connection actions remain responsive.
static func loadAsync(
for providerID: String,
timeout: TimeInterval = 2.5
) async throws -> CapacityDockProviderCredential {
try await performAsync(timeout: timeout) {
try load(for: providerID)
}
}
static func saveAsync(
_ credential: CapacityDockProviderCredential,
for providerID: String
) async throws {
try await performBlocking {
try save(credential, for: providerID)
}
}
static func removeAsync(for providerID: String) async throws {
try await performBlocking {
try remove(for: providerID)
}
}
static func performBlocking<Value: Sendable>(
operation: @escaping @Sendable () throws -> Value
) async throws -> Value {
try await withCheckedThrowingContinuation { continuation in
DispatchQueue.global(qos: .userInitiated).async {
continuation.resume(with: Result { try operation() })
}
}
}
static func performAsync<Value: Sendable>(
timeout: TimeInterval = 2.5,
operation: @escaping @Sendable () throws -> Value
) async throws -> Value {
try await withCheckedThrowingContinuation { continuation in
let gate = CredentialContinuationGate(continuation)
DispatchQueue.global(qos: .userInitiated).async {
do {
gate.resume(with: .success(try operation()))
} catch {
gate.resume(with: .failure(error))
}
}
DispatchQueue.global(qos: .userInitiated).asyncAfter(
deadline: .now() + max(0.05, timeout)
) {
gate.resume(with: .failure(CapacityDockProviderCredentialStoreError.timedOut))
}
}
}
}
/// Non-secret index used only to keep credentialed providers manageable in
/// General Settings without synchronously querying Keychain while SwiftUI
/// renders. Values are stable provider IDs; credentials remain solely in their
/// provider-scoped Keychain accounts. A successful Keychain read repairs this
/// index, and a missing item or explicit delete removes it.
enum CapacityDockProviderCredentialPresence {
static let key = "CodeBurnCapacityDockCredentialProviderIDs"
private static let lock = NSLock()
static func providerIDs(defaults: UserDefaults = .standard) -> Set<String> {
lock.lock()
defer { lock.unlock() }
return providerIDsWithoutLock(defaults: defaults)
}
private static func providerIDsWithoutLock(defaults: UserDefaults) -> Set<String> {
let known = Set(CapacityDockPreferences.supportedProviders.map(\.id))
return Set(defaults.stringArray(forKey: key) ?? []).intersection(known)
}
static func contains(_ providerID: String, defaults: UserDefaults = .standard) -> Bool {
providerIDs(defaults: defaults).contains(providerID)
}
static func set(
_ present: Bool,
for providerID: String,
defaults: UserDefaults = .standard
) {
guard CapacityDockProvider(rawValue: providerID) != nil else { return }
lock.lock()
var identifiers = providerIDsWithoutLock(defaults: defaults)
if present {
identifiers.insert(providerID)
} else {
identifiers.remove(providerID)
}
let ordered = CapacityDockPreferences.supportedProviders
.map(\.id)
.filter(identifiers.contains)
defaults.set(ordered, forKey: key)
lock.unlock()
if Thread.isMainThread {
NotificationCenter.default.post(
name: .capacityDockCredentialPresenceDidChange,
object: nil
)
} else {
DispatchQueue.main.async {
NotificationCenter.default.post(
name: .capacityDockCredentialPresenceDidChange,
object: nil
)
}
}
}
}
extension Notification.Name {
static let capacityDockCredentialPresenceDidChange = Notification.Name(
"com.codeburn.capacityDockCredentialPresenceDidChange"
)
}