Ariver
2026-06-12 6f0ede789115d393e1236d98776e91fb52c90c79
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import AppKit
import CoreImage
import CoreMedia
import CoreGraphics
import Foundation
@preconcurrency import ScreenCaptureKit
 
public enum ScreenshotProviderError: Error, Equatable, Sendable {
    case windowNotFound(UInt32)
    case captureTimedOut(windowID: UInt32, timeoutMilliseconds: Int)
    case invalidCapture(windowID: UInt32, pixelWidth: Int, pixelHeight: Int, pointWidth: Double, pointHeight: Double)
}
 
public struct WindowScreenshotCapture: Sendable {
    public let image: CGImage
    public let pointSize: NSSize
 
    public init(image: CGImage, pointSize: NSSize) {
        self.image = image
        self.pointSize = pointSize
    }
}
 
public enum WindowServerWindowScreenshotCapturer {
    public static func capture(windowID: UInt32) -> WindowScreenshotCapture? {
        guard let image = captureImage(windowID: windowID) else {
            return nil
        }
 
        return WindowScreenshotCapture(
            image: image,
            pointSize: NSSize(width: image.width, height: image.height)
        )
    }
 
    public static func captureImage(windowID: UInt32) -> CGImage? {
        var mutableWindowID = CGWindowID(windowID)
        let images = CGSHWCaptureWindowList(
            CGSMainConnectionID(),
            &mutableWindowID,
            1,
            [.ignoreGlobalClipShape, .bestResolution, .fullSize]
        ).takeRetainedValue() as NSArray
 
        guard let firstObject = images.firstObject else {
            return nil
        }
 
        let cfObject = firstObject as CFTypeRef
        guard CFGetTypeID(cfObject) == CGImage.typeID else {
            return nil
        }
 
        return (firstObject as! CGImage)
    }
}
 
@MainActor
public protocol WindowScreenshotCapturing: Sendable {
    func captureWindow(windowID: UInt32) async throws -> WindowScreenshotCapture
}
 
public struct ScreenshotCapturePolicy: Equatable, Sendable {
    public static let round01Default = ScreenshotCapturePolicy(
        timeoutMilliseconds: 300,
        maxRetriesPerSession: 1
    )
 
    public let timeoutMilliseconds: Int
    public let maxRetriesPerSession: Int
 
    public init(timeoutMilliseconds: Int, maxRetriesPerSession: Int) {
        self.timeoutMilliseconds = max(1, timeoutMilliseconds)
        self.maxRetriesPerSession = max(0, maxRetriesPerSession)
    }
 
    var timeoutNanoseconds: UInt64 {
        UInt64(timeoutMilliseconds) * 1_000_000
    }
}
 
public enum ScreenshotFallbackReason: Equatable, Sendable {
    case screenRecordingDenied
    case captureFailed(String)
    case timedOut(windowID: UInt32, timeoutMilliseconds: Int)
    case invalidCapture(windowID: UInt32, pixelWidth: Int, pixelHeight: Int, pointWidth: Double, pointHeight: Double)
    case retryLimitReached(failedAttempts: Int)
    case syntheticWindowID(windowID: UInt32)
}
 
public enum ScreenshotResolutionSource: Equatable, Sendable {
    case realScreenshot
    case skeletonFallback(ScreenshotFallbackReason)
}
 
@MainActor
public struct ScreenshotResolution {
    public let image: NSImage
    public let source: ScreenshotResolutionSource
 
    public init(image: NSImage, source: ScreenshotResolutionSource) {
        self.image = image
        self.source = source
    }
}
 
public enum ScreenshotDebugEvent: Equatable, Sendable {
    case captureStarted(windowID: UInt32)
    case captureSucceeded(windowID: UInt32)
    case captureFailed(windowID: UInt32, reason: ScreenshotFallbackReason)
    case retrySkipped(windowID: UInt32, failedAttempts: Int)
    case fallbackUsed(windowID: UInt32, reason: ScreenshotFallbackReason)
}
 
public extension ScreenshotFallbackReason {
    var diagnosticDescription: String {
        switch self {
        case .screenRecordingDenied:
            return "screenRecordingDenied"
        case .captureFailed(let summary):
            return "captureFailed(\(summary))"
        case .timedOut(let windowID, let timeoutMilliseconds):
            return "timedOut(windowID: \(windowID), timeoutMilliseconds: \(timeoutMilliseconds))"
        case .invalidCapture(let windowID, let pixelWidth, let pixelHeight, let pointWidth, let pointHeight):
            return "invalidCapture(windowID: \(windowID), pixelSize: \(pixelWidth)x\(pixelHeight), pointSize: \(pointWidth)x\(pointHeight))"
        case .retryLimitReached(let failedAttempts):
            return "retryLimitReached(failedAttempts: \(failedAttempts))"
        case .syntheticWindowID(let windowID):
            return "syntheticWindowID(windowID: \(windowID))"
        }
    }
}
 
public extension ScreenshotDebugEvent {
    var diagnosticDescription: String {
        switch self {
        case .captureStarted(let windowID):
            return "captureStarted(\(windowID))"
        case .captureSucceeded(let windowID):
            return "captureSucceeded(\(windowID))"
        case .captureFailed(let windowID, let reason):
            return "captureFailed(\(windowID), \(reason.diagnosticDescription))"
        case .retrySkipped(let windowID, let failedAttempts):
            return "retrySkipped(\(windowID), failedAttempts: \(failedAttempts))"
        case .fallbackUsed(let windowID, let reason):
            return "fallbackUsed(\(windowID), \(reason.diagnosticDescription))"
        }
    }
}
 
@MainActor
public protocol ScreenshotDebugLogging: AnyObject {
    func record(_ event: ScreenshotDebugEvent)
}
 
@MainActor
public final class NoopScreenshotDebugLogger: ScreenshotDebugLogging {
    public init() {}
 
    public func record(_ event: ScreenshotDebugEvent) {}
}
 
@MainActor
public final class ScreenshotCaptureSession {
    private var failedAttemptsByWindowID: [UInt32: Int] = [:]
 
    public init() {}
 
    func failedAttempts(for windowID: UInt32) -> Int {
        failedAttemptsByWindowID[windowID, default: 0]
    }
 
    func recordFailure(for windowID: UInt32) {
        failedAttemptsByWindowID[windowID, default: 0] += 1
    }
 
    func recordSuccess(for windowID: UInt32) {
        failedAttemptsByWindowID[windowID] = nil
    }
}
 
@MainActor
private final class ScreenshotCaptureRace {
    private var continuation: CheckedContinuation<WindowScreenshotCapture, any Error>?
    private var result: Result<WindowScreenshotCapture, any Error>?
    private var didFinish = false
 
    func wait() async throws -> WindowScreenshotCapture {
        if let result {
            return try result.get()
        }
 
        return try await withCheckedThrowingContinuation { continuation in
            self.continuation = continuation
        }
    }
 
    func finish(_ result: Result<WindowScreenshotCapture, any Error>) {
        guard !didFinish else {
            return
        }
 
        didFinish = true
        if let continuation {
            self.continuation = nil
            continuation.resume(with: result)
        } else {
            self.result = result
        }
    }
}
 
@MainActor
public final class ScreenCaptureKitWindowScreenshotCapturer: WindowScreenshotCapturing {
    public init() {}
 
    public func captureWindow(windowID: UInt32) async throws -> WindowScreenshotCapture {
        if let privateCapture = WindowServerWindowScreenshotCapturer.capture(windowID: windowID) {
            return privateCapture
        }
 
        let content = try await SCShareableContent.current
 
        guard let captureWindow = content.windows.first(where: { $0.windowID == windowID }) else {
            throw ScreenshotProviderError.windowNotFound(windowID)
        }
 
        let filter = SCContentFilter(desktopIndependentWindow: captureWindow)
        let contentInfo = SCShareableContent.info(for: filter)
        let configuration = configuration(for: contentInfo)
        let image: CGImage
        do {
            image = try await SCScreenshotManager.captureImage(contentFilter: filter, configuration: configuration)
        } catch {
            if let sampleBufferImage = try? await captureSampleBufferImage(
                filter: filter,
                configuration: configuration,
                windowID: windowID,
                pointSize: NSSize(width: contentInfo.contentRect.width, height: contentInfo.contentRect.height)
            ) {
                image = sampleBufferImage
            } else {
                throw error
            }
        }
 
        return WindowScreenshotCapture(
            image: image,
            pointSize: NSSize(width: contentInfo.contentRect.width, height: contentInfo.contentRect.height)
        )
    }
 
    private func configuration(for contentInfo: SCShareableContentInfo) -> SCStreamConfiguration {
        let configuration = SCStreamConfiguration()
        configuration.width = max(1, Int(contentInfo.contentRect.width * CGFloat(contentInfo.pointPixelScale)))
        configuration.height = max(1, Int(contentInfo.contentRect.height * CGFloat(contentInfo.pointPixelScale)))
        configuration.showsCursor = false
        configuration.ignoreShadowsSingleWindow = true
        configuration.includeChildWindows = true
        return configuration
    }
 
    private func captureSampleBufferImage(
        filter: SCContentFilter,
        configuration: SCStreamConfiguration,
        windowID: UInt32,
        pointSize: NSSize
    ) async throws -> CGImage {
        try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<CGImage, any Error>) in
            SCScreenshotManager.captureSampleBuffer(
                contentFilter: filter,
                configuration: configuration
            ) { sampleBuffer, error in
                if let error {
                    continuation.resume(throwing: error)
                    return
                }
                guard let sampleBuffer else {
                    continuation.resume(throwing: ScreenshotProviderError.invalidCapture(
                        windowID: windowID,
                        pixelWidth: 0,
                        pixelHeight: 0,
                        pointWidth: Double(pointSize.width),
                        pointHeight: Double(pointSize.height)
                    ))
                    return
                }
 
                guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
                    continuation.resume(throwing: ScreenshotProviderError.invalidCapture(
                        windowID: windowID,
                        pixelWidth: 0,
                        pixelHeight: 0,
                        pointWidth: Double(pointSize.width),
                        pointHeight: Double(pointSize.height)
                    ))
                    return
                }
 
                let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
                guard let cgImage = CIContext().createCGImage(ciImage, from: ciImage.extent) else {
                    continuation.resume(throwing: ScreenshotProviderError.invalidCapture(
                        windowID: windowID,
                        pixelWidth: CVPixelBufferGetWidth(pixelBuffer),
                        pixelHeight: CVPixelBufferGetHeight(pixelBuffer),
                        pointWidth: Double(pointSize.width),
                        pointHeight: Double(pointSize.height)
                    ))
                    return
                }
 
                continuation.resume(returning: cgImage)
            }
        }
    }
}
 
private typealias CGSConnectionID = UInt32
 
private struct CGSWindowCaptureOptions: OptionSet {
    let rawValue: UInt32
 
    static let ignoreGlobalClipShape = CGSWindowCaptureOptions(rawValue: 1 << 11)
    static let bestResolution = CGSWindowCaptureOptions(rawValue: 1 << 8)
    static let fullSize = CGSWindowCaptureOptions(rawValue: 1 << 19)
}
 
@_silgen_name("CGSMainConnectionID")
private func CGSMainConnectionID() -> CGSConnectionID
 
@_silgen_name("CGSHWCaptureWindowList")
private func CGSHWCaptureWindowList(
    _ connectionID: CGSConnectionID,
    _ windowList: UnsafeMutablePointer<CGWindowID>,
    _ windowCount: UInt32,
    _ options: CGSWindowCaptureOptions
) -> Unmanaged<CFArray>
 
@MainActor
public final class ScreenCaptureKitScreenshotProvider: ScreenshotProviderProtocol {
    private let capturer: any WindowScreenshotCapturing
    private let skeletonProvider: any SkeletonThumbnailProviderProtocol
    private let policy: ScreenshotCapturePolicy
    private let debugLogger: any ScreenshotDebugLogging
    private let permissionChecker: any SystemPermissionChecking
    private let defaultSession = ScreenshotCaptureSession()
 
    public init(
        capturer: any WindowScreenshotCapturing = ScreenCaptureKitWindowScreenshotCapturer(),
        skeletonProvider: any SkeletonThumbnailProviderProtocol = NativeSkeletonThumbnailProvider(),
        policy: ScreenshotCapturePolicy = .round01Default,
        debugLogger: any ScreenshotDebugLogging = NoopScreenshotDebugLogger(),
        permissionChecker: any SystemPermissionChecking = SystemPermissionChecker()
    ) {
        self.capturer = capturer
        self.skeletonProvider = skeletonProvider
        self.policy = policy
        self.debugLogger = debugLogger
        self.permissionChecker = permissionChecker
    }
 
    public func screenshot(for window: AlignerWindow) async throws -> NSImage {
        let capture = try await captureWindowWithinTimeout(windowID: window.id)
        try validateCapture(capture, windowID: window.id)
        return NSImage(cgImage: capture.image, size: capture.pointSize)
    }
 
    public func resolvedScreenshot(for window: AlignerWindow) async -> ScreenshotResolution {
        await resolvedScreenshot(for: window, in: defaultSession)
    }
 
    public func resolvedScreenshot(for window: AlignerWindow, in session: ScreenshotCaptureSession) async -> ScreenshotResolution {
        if MinimizedWindowFallbackPolicy.isSyntheticWindow(window) {
            return fallbackResolution(for: window, reason: .syntheticWindowID(windowID: window.id))
        }
        if permissionChecker.status(for: .screenRecording) != .granted {
            return fallbackResolution(for: window, reason: .screenRecordingDenied)
        }
 
        let failedAttempts = session.failedAttempts(for: window.id)
        if failedAttempts > policy.maxRetriesPerSession {
            let reason = ScreenshotFallbackReason.retryLimitReached(failedAttempts: failedAttempts)
            debugLogger.record(.retrySkipped(windowID: window.id, failedAttempts: failedAttempts))
            return fallbackResolution(for: window, reason: reason)
        }
 
        debugLogger.record(.captureStarted(windowID: window.id))
 
        do {
            let image = try await screenshot(for: window)
            session.recordSuccess(for: window.id)
            debugLogger.record(.captureSucceeded(windowID: window.id))
            return ScreenshotResolution(image: image, source: .realScreenshot)
        } catch {
            let reason = fallbackReason(for: error)
            session.recordFailure(for: window.id)
            debugLogger.record(.captureFailed(windowID: window.id, reason: reason))
            return fallbackResolution(for: window, reason: reason)
        }
    }
 
    private func captureWindowWithinTimeout(windowID: UInt32) async throws -> WindowScreenshotCapture {
        let capturer = self.capturer
        let race = ScreenshotCaptureRace()
        let captureTask = Task { @MainActor in
            do {
                let capture = try await capturer.captureWindow(windowID: windowID)
                race.finish(.success(capture))
            } catch {
                race.finish(.failure(error))
            }
        }
        let timeoutNanoseconds = policy.timeoutNanoseconds
        let timeoutMilliseconds = policy.timeoutMilliseconds
        let timeoutTask = Task {
            do {
                try await Task.sleep(nanoseconds: timeoutNanoseconds)
                await MainActor.run {
                    race.finish(.failure(ScreenshotProviderError.captureTimedOut(
                        windowID: windowID,
                        timeoutMilliseconds: timeoutMilliseconds
                    )))
                }
            } catch {}
        }
 
        return try await withTaskCancellationHandler {
            defer {
                captureTask.cancel()
                timeoutTask.cancel()
            }
            return try await race.wait()
        } onCancel: {
            captureTask.cancel()
            timeoutTask.cancel()
            Task { @MainActor in
                race.finish(.failure(CancellationError()))
            }
        }
    }
 
    private func fallbackResolution(for window: AlignerWindow, reason: ScreenshotFallbackReason) -> ScreenshotResolution {
        let title = window.title.isEmpty ? window.app.name : window.title
        let image = skeletonProvider.withOverlayTitle(title, for: window.app)
        debugLogger.record(.fallbackUsed(windowID: window.id, reason: reason))
        return ScreenshotResolution(image: image, source: .skeletonFallback(reason))
    }
 
    private func fallbackReason(for error: Error) -> ScreenshotFallbackReason {
        if case let ScreenshotProviderError.captureTimedOut(windowID, timeoutMilliseconds) = error {
            return .timedOut(windowID: windowID, timeoutMilliseconds: timeoutMilliseconds)
        }
        if case let ScreenshotProviderError.invalidCapture(windowID, pixelWidth, pixelHeight, pointWidth, pointHeight) = error {
            return .invalidCapture(
                windowID: windowID,
                pixelWidth: pixelWidth,
                pixelHeight: pixelHeight,
                pointWidth: pointWidth,
                pointHeight: pointHeight
            )
        }
        return .captureFailed(Self.sanitizedCaptureFailureSummary(for: error))
    }
 
    private static func sanitizedCaptureFailureSummary(for error: Error) -> String {
        let nsError = error as NSError
        return [
            "domain=\(nsError.domain)",
            "code=\(nsError.code)",
            "descriptionHash=\(stableFingerprint(nsError.localizedDescription))",
            "descriptionLength=\(nsError.localizedDescription.count)"
        ].joined(separator: " ")
    }
 
    private static func stableFingerprint(_ value: String) -> String {
        var hash: UInt64 = 0xcbf29ce484222325
        for byte in value.utf8 {
            hash ^= UInt64(byte)
            hash = hash &* 0x100000001b3
        }
        return String(format: "%016llx", hash)
    }
 
    private func validateCapture(_ capture: WindowScreenshotCapture, windowID: UInt32) throws {
        let pointWidth = Double(capture.pointSize.width)
        let pointHeight = Double(capture.pointSize.height)
        guard capture.image.width > 0, capture.image.height > 0, pointWidth > 0, pointHeight > 0 else {
            throw ScreenshotProviderError.invalidCapture(
                windowID: windowID,
                pixelWidth: capture.image.width,
                pixelHeight: capture.image.height,
                pointWidth: pointWidth,
                pointHeight: pointHeight
            )
        }
    }
}