Ariver
2026-06-19 b1368b6a51379714958af82cb1e3dc24183b0583
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
import AppKit
import QuartzCore
import AlignerCore
 
@MainActor
final class QuickSwitchSessionController {
    static let overlayTitle = "Aligner Quick Switch"
    private static let closeRequestStillPresentMessage = "已发送关闭请求,但窗口仍在列表中。"
    private static let closeTargetNotFoundMessage = "没有找到这个窗口的关闭控件,已刷新列表。"
    private static let closeUnsupportedMessage = "这个窗口没有可用的关闭按钮。"
    private static let closeFailedMessage = "关闭请求没有被系统接受。"
    private static let closeVerificationRetryDelays: [TimeInterval] = [0.35, 0.70, 1.20]
 
    private let view = QuickSwitchRootView()
    private let snapshotLoader: any QuickSwitchSnapshotLoading
    private let screenshotProvider: any ScreenshotProviderProtocol
    private let windowActivationService: any WindowActivationServiceProtocol
    private let spaceActivationService: any SpaceActivationServiceProtocol
    private let windowCloseService: any WindowCloseServiceProtocol
    private let systemCriticalWindowDetector: any SystemCriticalWindowDetecting
    private let disableScreenshotRefresh: Bool
    private var waterfallViewMode: QuickSwitchWaterfallViewMode
    private var closeConfirmationRequired: Bool
    private let onCloseConfirmationDisabled: (() -> Void)?
    private let debugOverlayWidth: CGFloat?
    private let debugKeySequence: [String]
    private let debugMouseSequence: [String]
    private let debugSystemCriticalAfter: TimeInterval?
    private lazy var coordinator = WindowCoordinator(
        contentView: view,
        title: Self.overlayTitle,
        debugOverlayWidth: debugOverlayWidth
    )
    private var currentSnapshot: QuickSwitchSnapshot?
    private var sourceViewModel: QuickSwitchViewModel?
    private var currentViewModel: QuickSwitchViewModel?
    private var lockedSpaceFilterID: UInt64?
    private var selectionBeforeSpaceFilter: QuickSwitchSelection?
    private var lastSpaceFilterAction: String?
    private var pendingLockedSpaceUnlockSpaceID: UInt64?
    private var pendingLockedSpaceUnlockTask: Task<Void, Never>?
    private var lastSnapshotError: String?
    private var snapshotTask: Task<Void, Never>?
    private var screenshotTask: Task<Void, Never>?
    private var systemCriticalMonitorTimer: Timer?
    private var debugSystemCriticalTimer: Timer?
    private var screenshotSession = ScreenshotCaptureSession()
    private var sessionGeneration = 0
    private var showStartTime: CFTimeInterval?
    private var overlayOpenElapsedMilliseconds: Double?
    private var snapshotStartElapsedMilliseconds: Double?
    private var snapshotDurationMilliseconds: Double?
    private var snapshotRanOnMainThread: Bool?
    private var lastCommittedSelection: QuickSwitchSelection?
    private var lastCommitSource: QuickSwitchCommitSource?
    private var lastActivationWindowID: UInt32?
    private var lastActivationResult: WindowActivationResult?
    private var lastActivationError: String?
    private var lastSpaceActivationSpaceID: UInt64?
    private var lastSpaceActivationDidRequestFocus: Bool?
    private var lastSpaceActivationDisplayIdentifier: String?
    private var lastSpaceActivationPreviousCurrentSpaceID: UInt64?
    private var lastSpaceActivationError: String?
    private var lastCloseTargetKind: String?
    private var lastCloseAppGroupIndex: Int?
    private var lastCloseAppName: String?
    private var lastCloseWindowID: UInt32?
    private var lastCloseResult: WindowCloseResult?
    private var lastCloseError: String?
    private var pendingCloseVerification: PendingCloseVerification?
    private var suppressedCloseTargets: [SuppressedCloseTarget] = []
    private var lastDismissReason: DismissReason?
    private var lastSystemCriticalAction: String?
    private var lastSystemCriticalMonitorSource: String?
    private var lastSystemCriticalWindowCount = 0
    private var lastSystemCriticalWindowTitleHashes: [String] = []
    private var lastSystemCriticalWindowTitleLengths: [Int] = []
    private var lastSystemCriticalWindowTitleIsEmpty: [Bool] = []
    private var lastSystemCriticalWindowOwners: [String] = []
    private var lastSystemCriticalOverlayLevel: CGWindowLevel?
    private var systemCriticalDetectionCount = 0
    private var lifecycleTargetCycles = 0
    private var lifecycleCompletedCycles = 0
    private var lifecycleMaximumOverlayWindows = 0
    private var lifecycleMaximumVisibleOverlayWindows = 0
    private var lifecycleResidualOverlayWindows = 0
    private var lifecycleResidualVisibleOverlayWindows = 0
    var onSnapshotUpdated: (() -> Void)?
    var onToggleWaterfallViewMode: (() -> Void)?
 
    private struct PendingCloseVerification {
        let request: QuickSwitchCloseRequest
        let generation: Int
        let attempt: Int
    }
 
    private enum SuppressedCloseTarget: Equatable {
        case app(AlignerApp)
        case window(UInt32)
 
        init(_ request: QuickSwitchCloseRequest) {
            switch request {
            case .app:
                self = .app(request.app)
            case .window:
                self = .window(request.windowID ?? 0)
            }
        }
    }
 
    init(
        snapshotLoader: any QuickSwitchSnapshotLoading = LiveQuickSwitchSnapshotLoader(),
        screenshotProvider: any ScreenshotProviderProtocol = ScreenCaptureKitScreenshotProvider(
            debugLogger: StderrScreenshotDebugLogger()
        ),
        windowActivationService: any WindowActivationServiceProtocol = CGWindowAXWindowService(
            spaceIDsByWindowIDProvider: { _ in [:] }
        ),
        spaceActivationService: any SpaceActivationServiceProtocol = PrivateSpaceActivationService(),
        windowCloseService: any WindowCloseServiceProtocol = CGWindowAXWindowService(
            spaceIDsByWindowIDProvider: { _ in [:] }
        ),
        systemCriticalWindowDetector: any SystemCriticalWindowDetecting = CGWindowSystemCriticalWindowDetector(),
        disableScreenshotRefresh: Bool = false,
        waterfallViewMode: QuickSwitchWaterfallViewMode = .verticalColumns,
        closeConfirmationRequired: Bool = true,
        onCloseConfirmationDisabled: (() -> Void)? = nil,
        debugHoveredAppGroupIndex: Int? = nil,
        debugOverlayWidth: CGFloat? = nil,
        debugKeySequence: [String] = [],
        debugMouseSequence: [String] = [],
        debugSystemCriticalAfter: TimeInterval? = nil
    ) {
        self.snapshotLoader = snapshotLoader
        self.screenshotProvider = screenshotProvider
        self.windowActivationService = windowActivationService
        self.spaceActivationService = spaceActivationService
        self.windowCloseService = windowCloseService
        self.systemCriticalWindowDetector = systemCriticalWindowDetector
        self.disableScreenshotRefresh = disableScreenshotRefresh
        self.waterfallViewMode = waterfallViewMode
        self.closeConfirmationRequired = closeConfirmationRequired
        self.onCloseConfirmationDisabled = onCloseConfirmationDisabled
        self.debugOverlayWidth = debugOverlayWidth
        self.debugKeySequence = debugKeySequence
        self.debugMouseSequence = debugMouseSequence
        self.debugSystemCriticalAfter = debugSystemCriticalAfter
        view.setDebugHoveredAppGroupIndex(debugHoveredAppGroupIndex)
    }
 
    var isVisible: Bool {
        coordinator.isQuickSwitchVisible
    }
 
    func show() {
        sessionGeneration += 1
        let generation = sessionGeneration
        DevelopmentDiagnostics.log("quickSwitch.session.show.start", [
            "generation": generation,
            "wasVisible": isVisible
        ])
        showStartTime = CACurrentMediaTime()
        overlayOpenElapsedMilliseconds = nil
        snapshotStartElapsedMilliseconds = nil
        snapshotDurationMilliseconds = nil
        snapshotRanOnMainThread = nil
        lastCommittedSelection = nil
        lastCommitSource = nil
        lastActivationWindowID = nil
        lastActivationResult = nil
        lastActivationError = nil
        lastSpaceActivationSpaceID = nil
        lastSpaceActivationDidRequestFocus = nil
        lastSpaceActivationDisplayIdentifier = nil
        lastSpaceActivationPreviousCurrentSpaceID = nil
        lastSpaceActivationError = nil
        lastCloseTargetKind = nil
        lastCloseAppGroupIndex = nil
        lastCloseAppName = nil
        lastCloseWindowID = nil
        lastCloseResult = nil
        lastCloseError = nil
        pendingCloseVerification = nil
        suppressedCloseTargets = []
        cancelPendingLockedSpaceUnlock()
        lockedSpaceFilterID = nil
        selectionBeforeSpaceFilter = nil
        lastSpaceFilterAction = nil
        lastDismissReason = nil
        lastSystemCriticalAction = nil
        lastSystemCriticalMonitorSource = nil
        lastSystemCriticalWindowCount = 0
        lastSystemCriticalWindowTitleHashes = []
        lastSystemCriticalWindowTitleLengths = []
        lastSystemCriticalWindowTitleIsEmpty = []
        lastSystemCriticalWindowOwners = []
        lastSystemCriticalOverlayLevel = nil
        systemCriticalDetectionCount = 0
        invalidateSystemCriticalMonitoring()
        screenshotTask?.cancel()
        screenshotTask = nil
        screenshotSession = ScreenshotCaptureSession()
        clearSnapshotState()
        view.onEscape = { [weak self] in
            self?.hide(reason: .escape)
        }
        view.onCommitSelection = { [weak self] selection, source in
            self?.commitSelection(selection, source: source)
        }
        view.onSpaceLaneClick = { [weak self] spaceID, clickCount in
            self?.handleSpaceLaneClick(spaceID, clickCount: clickCount)
        }
        view.onBackgroundClick = { [weak self] in
            self?.handleBackgroundClick() ?? false
        }
        view.onToggleWaterfallViewMode = { [weak self] in
            self?.onToggleWaterfallViewMode?()
        }
        view.setSuppressEventInput(!debugMouseSequence.isEmpty || !debugKeySequence.isEmpty)
        view.onRequestClose = { [weak self] request in
            self?.requestClose(request)
        }
        view.onCloseConfirmationDisabled = { [weak self] in
            guard let self else { return }
            self.closeConfirmationRequired = false
            self.onCloseConfirmationDisabled?()
        }
        view.setCloseConfirmationRequired(closeConfirmationRequired)
        view.setWaterfallViewMode(waterfallViewMode)
        view.apply(viewModel: nil)
        view.beginPerformanceFirstFrameMeasurement()
        coordinator.openQuickSwitch()
        coordinator.promoteForTextInput()
        view.window?.makeFirstResponder(view)
        overlayOpenElapsedMilliseconds = elapsedSinceShowStart()
        updateLifecycleWindowHighWaterMark()
        startSystemCriticalMonitoring()
        scheduleSnapshotRefresh(for: generation)
        DevelopmentDiagnostics.log("quickSwitch.session.show.end", [
            "generation": generation,
            "overlayOpenElapsedMilliseconds": overlayOpenElapsedMilliseconds,
            "overlayLevel": coordinator.currentLevel,
            "windowVisible": view.window?.isVisible ?? false
        ])
    }
 
    func hide(reason: DismissReason = .userClosed) {
        DevelopmentDiagnostics.log("quickSwitch.session.hide.start", [
            "reason": Self.dismissReasonString(reason),
            "wasVisible": isVisible,
            "generation": sessionGeneration
        ])
        sessionGeneration += 1
        lastDismissReason = reason
        snapshotTask?.cancel()
        snapshotTask = nil
        screenshotTask?.cancel()
        screenshotTask = nil
        cancelPendingLockedSpaceUnlock()
        pendingCloseVerification = nil
        view.clearCloseFeedback()
        invalidateSystemCriticalMonitoring()
        coordinator.closeQuickSwitch(reason: reason)
        lifecycleResidualOverlayWindows = overlayWindowCount()
        lifecycleResidualVisibleOverlayWindows = visibleOverlayWindowCount()
        updateLifecycleWindowHighWaterMark()
        DevelopmentDiagnostics.log("quickSwitch.session.hide.end", [
            "reason": Self.dismissReasonString(reason),
            "generation": sessionGeneration,
            "residualOverlayWindows": lifecycleResidualOverlayWindows,
            "residualVisibleOverlayWindows": lifecycleResidualVisibleOverlayWindows
        ])
    }
 
    func refocus() {
        DevelopmentDiagnostics.log("quickSwitch.session.refocus")
        coordinator.promoteForTextInput()
        view.window?.makeFirstResponder(view)
    }
 
    func retriggerFromShortcut() {
        DevelopmentDiagnostics.log("quickSwitch.session.retriggerFromShortcut")
        guard isVisible else {
            show()
            return
        }
 
        guard currentViewModel != nil else {
            DevelopmentDiagnostics.log("quickSwitch.session.retriggerFromShortcut.noSnapshot", [
                "action": "fallbackShow"
            ])
            show()
            return
        }
 
        let didCycle = view.hoverNextAppGroupIndex()
        DevelopmentDiagnostics.log("quickSwitch.session.retriggerFromShortcut.hoverApp", [
            "didCycle": didCycle
        ])
 
        if !didCycle {
            show()
        }
    }
 
    func setWaterfallViewMode(_ mode: QuickSwitchWaterfallViewMode) {
        guard waterfallViewMode != mode else { return }
 
        waterfallViewMode = mode
        DevelopmentDiagnostics.log("quickSwitch.session.waterfallViewModeChanged", [
            "mode": mode.rawValue,
            "visible": isVisible,
            "hasViewModel": currentViewModel != nil
        ])
 
        view.setWaterfallViewMode(mode)
        if isVisible, let currentViewModel {
            view.applyProjected(viewModel: currentViewModel)
        }
        onSnapshotUpdated?()
    }
 
    func reportDictionary() -> [String: Any] {
        let spaceCount = currentViewModel?.spaceCount ?? 0
        let appCount = currentViewModel?.appShelf.count ?? 0
        let columnCount = currentViewModel?.waterfallColumns.count ?? 0
        let windowCount = currentViewModel?.windowCount ?? 0
 
        return [
            "snapshotLoaded": currentSnapshot != nil,
            "snapshotError": lastSnapshotError ?? NSNull(),
            "quickSwitchVisible": isVisible,
            "diagnosticLogLocationKind": DevelopmentDiagnostics.pathSummary(DevelopmentDiagnostics.logPath)["locationKind"] ?? "unknown",
            "diagnosticLogBasename": DevelopmentDiagnostics.pathSummary(DevelopmentDiagnostics.logPath)["basename"] ?? "aligner-dev.log",
            "displayCount": currentViewModel?.displays.count ?? 0,
            "spaceCount": spaceCount,
            "appCount": appCount,
            "columnCount": columnCount,
            "windowCount": windowCount,
            "initialSelectionWindowID": currentViewModel?.initialSelection?.windowID ?? NSNull(),
            "appShelfNames": currentViewModel?.appShelf.map(\.app.name) ?? [],
            "spaceLabels": currentViewModel?.displays.flatMap { $0.spaces.map(\.label) } ?? [],
            "spaceFilterLockedSpaceID": lockedSpaceFilterID ?? NSNull(),
            "spaceFilterActive": lockedSpaceFilterID != nil,
            "lastSpaceFilterAction": lastSpaceFilterAction ?? NSNull(),
            "overlayOpenElapsedMilliseconds": overlayOpenElapsedMilliseconds ?? NSNull(),
            "snapshotStartElapsedMilliseconds": snapshotStartElapsedMilliseconds ?? NSNull(),
            "snapshotDurationMilliseconds": snapshotDurationMilliseconds ?? NSNull(),
            "snapshotRanOnMainThread": snapshotRanOnMainThread ?? NSNull(),
            "lastCommittedAppGroupIndex": lastCommittedSelection?.appGroupIndex ?? NSNull(),
            "lastCommittedWindowIndex": lastCommittedSelection?.windowIndex ?? NSNull(),
            "lastCommittedWindowID": lastCommittedSelection?.windowID ?? NSNull(),
            "lastCommitSource": lastCommitSource?.rawValue ?? NSNull(),
            "lastActivationWindowID": lastActivationWindowID ?? NSNull(),
            "lastActivationResult": lastActivationResult.map(Self.activationResultString) ?? NSNull(),
            "lastActivationError": lastActivationError ?? NSNull(),
            "lastSpaceActivationSpaceID": lastSpaceActivationSpaceID ?? NSNull(),
            "lastSpaceActivationDidRequestFocus": lastSpaceActivationDidRequestFocus ?? NSNull(),
            "lastSpaceActivationDisplayIdentifier": lastSpaceActivationDisplayIdentifier ?? NSNull(),
            "lastSpaceActivationPreviousCurrentSpaceID": lastSpaceActivationPreviousCurrentSpaceID ?? NSNull(),
            "lastSpaceActivationError": lastSpaceActivationError ?? NSNull(),
            "lastCloseTargetKind": lastCloseTargetKind ?? NSNull(),
            "lastCloseAppGroupIndex": lastCloseAppGroupIndex ?? NSNull(),
            "lastCloseAppName": lastCloseAppName ?? NSNull(),
            "lastCloseWindowID": lastCloseWindowID ?? NSNull(),
            "lastCloseResult": lastCloseResult.map(Self.closeResultString) ?? NSNull(),
            "lastCloseError": lastCloseError ?? NSNull(),
            "suppressedCloseTargetCount": suppressedCloseTargets.count,
            "lastDismissReason": lastDismissReason.map(Self.dismissReasonString) ?? NSNull(),
            "overlayLevel": Int(coordinator.currentLevel),
            "overlayMainMenuLevel": Int(CGWindowLevelForKey(.mainMenuWindow)),
            "overlayBelowRescueSystemWindows": coordinator.currentLevel < CGWindowLevelForKey(.mainMenuWindow),
            "lastSystemCriticalAction": lastSystemCriticalAction ?? NSNull(),
            "lastSystemCriticalMonitorSource": lastSystemCriticalMonitorSource ?? NSNull(),
            "lastSystemCriticalWindowCount": lastSystemCriticalWindowCount,
            "lastSystemCriticalWindowTitleHashes": lastSystemCriticalWindowTitleHashes,
            "lastSystemCriticalWindowTitleLengths": lastSystemCriticalWindowTitleLengths,
            "lastSystemCriticalWindowTitleIsEmpty": lastSystemCriticalWindowTitleIsEmpty,
            "lastSystemCriticalWindowOwners": lastSystemCriticalWindowOwners,
            "lastSystemCriticalOverlayLevel": lastSystemCriticalOverlayLevel.map { Int($0) } ?? NSNull(),
            "systemCriticalDetectionCount": systemCriticalDetectionCount,
            "lifecycleTargetCycles": lifecycleTargetCycles,
            "lifecycleCompletedCycles": lifecycleCompletedCycles,
            "lifecycleMaximumOverlayWindows": lifecycleMaximumOverlayWindows,
            "lifecycleMaximumVisibleOverlayWindows": lifecycleMaximumVisibleOverlayWindows,
            "lifecycleResidualOverlayWindows": lifecycleResidualOverlayWindows,
            "lifecycleResidualVisibleOverlayWindows": lifecycleResidualVisibleOverlayWindows,
            "lifecycleGatePassed": lifecycleTargetCycles == 0
                || (
                    lifecycleCompletedCycles == lifecycleTargetCycles
                        && lifecycleMaximumOverlayWindows <= 1
                        && lifecycleMaximumVisibleOverlayWindows <= 1
                        && lifecycleResidualOverlayWindows <= 1
                        && lifecycleResidualVisibleOverlayWindows == 0
                ),
            "rootView": view.reportDictionary()
        ]
    }
 
    func prepareLifecycleRun(targetCycles: Int) {
        lifecycleTargetCycles = max(0, targetCycles)
        lifecycleCompletedCycles = 0
        lifecycleMaximumOverlayWindows = 0
        lifecycleMaximumVisibleOverlayWindows = 0
        lifecycleResidualOverlayWindows = 0
        lifecycleResidualVisibleOverlayWindows = 0
        view.resetPerformanceMeasurements()
    }
 
    func markLifecycleCycleCompleted() {
        lifecycleCompletedCycles += 1
        updateLifecycleWindowHighWaterMark()
    }
 
    func finalizeLifecycleRun() {
        lifecycleResidualOverlayWindows = overlayWindowCount()
        lifecycleResidualVisibleOverlayWindows = visibleOverlayWindowCount()
        updateLifecycleWindowHighWaterMark()
    }
 
    private func scheduleSnapshotRefresh(
        for generation: Int,
        replayDebugCommands: Bool = true,
        delay: TimeInterval = 0
    ) {
        DevelopmentDiagnostics.log("quickSwitch.snapshot.schedule", [
            "generation": generation,
            "visible": isVisible,
            "replayDebugCommands": replayDebugCommands,
            "delayMilliseconds": Int(delay * 1_000)
        ])
        snapshotTask?.cancel()
        let loader = snapshotLoader
        let showStartTime = showStartTime
        snapshotTask = Task { [weak self] in
            guard let self, !Task.isCancelled else { return }
            if delay > 0 {
                try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
                guard !Task.isCancelled else { return }
            }
 
            let loadTask = Task.detached(priority: .userInitiated) {
                SnapshotLoadResult.load(using: loader, showStartTime: showStartTime)
            }
            let result = await withTaskCancellationHandler {
                await loadTask.value
            } onCancel: {
                loadTask.cancel()
            }
 
            guard !Task.isCancelled else { return }
            self.applySnapshotResult(
                result,
                generation: generation,
                replayDebugCommands: replayDebugCommands
            )
        }
    }
 
    private func clearSnapshotState() {
        currentSnapshot = nil
        sourceViewModel = nil
        currentViewModel = nil
        lastSnapshotError = nil
    }
 
    private func startSystemCriticalMonitoring() {
        invalidateSystemCriticalMonitoring()
        checkForSystemCriticalWindows(source: "initial")
        guard isVisible else { return }
 
        let monitorTimer = Timer(timeInterval: 0.20, repeats: true) { [weak self] _ in
            Task { @MainActor [weak self] in
                self?.checkForSystemCriticalWindows(source: "poll")
            }
        }
        RunLoop.main.add(monitorTimer, forMode: .common)
        systemCriticalMonitorTimer = monitorTimer
 
        if let debugSystemCriticalAfter {
            let debugTimer = Timer(timeInterval: debugSystemCriticalAfter, repeats: false) { [weak self] _ in
                Task { @MainActor [weak self] in
                    self?.handleSystemCriticalWindows(
                        [
                            SystemCriticalWindowRecord(
                                ownerName: "SecurityAgent",
                                title: "Debug Security Confirmation",
                                processIdentifier: 0,
                                layer: Int(CGWindowLevelForKey(.mainMenuWindow)),
                                bounds: .zero
                            )
                        ],
                        source: "debug"
                    )
                }
            }
            RunLoop.main.add(debugTimer, forMode: .common)
            debugSystemCriticalTimer = debugTimer
        }
    }
 
    private func invalidateSystemCriticalMonitoring() {
        systemCriticalMonitorTimer?.invalidate()
        systemCriticalMonitorTimer = nil
        debugSystemCriticalTimer?.invalidate()
        debugSystemCriticalTimer = nil
    }
 
    private func checkForSystemCriticalWindows(source: String) {
        guard isVisible else { return }
 
        let criticalWindows = systemCriticalWindowDetector.visibleSystemCriticalWindows()
        guard !criticalWindows.isEmpty else { return }
 
        handleSystemCriticalWindows(criticalWindows, source: source)
    }
 
    private func handleSystemCriticalWindows(_ windows: [SystemCriticalWindowRecord], source: String) {
        guard isVisible, !windows.isEmpty else { return }
 
        systemCriticalDetectionCount += 1
        lastSystemCriticalAction = "closeOverlay"
        lastSystemCriticalMonitorSource = source
        lastSystemCriticalWindowCount = windows.count
        lastSystemCriticalWindowTitleHashes = windows.map { DevelopmentDiagnostics.stableFingerprint($0.title) }
        lastSystemCriticalWindowTitleLengths = windows.map { $0.title.count }
        lastSystemCriticalWindowTitleIsEmpty = windows.map { $0.title.isEmpty }
        lastSystemCriticalWindowOwners = windows.map(\.ownerName)
        lastSystemCriticalOverlayLevel = coordinator.currentLevel
        hide(reason: .systemCriticalWindow)
        onSnapshotUpdated?()
    }
 
    private func commitSelection(
        _ selection: QuickSwitchSelection,
        source: QuickSwitchCommitSource,
        in viewModel: QuickSwitchViewModel? = nil
    ) {
        DevelopmentDiagnostics.log("quickSwitch.activation.commit.start", [
            "source": source.rawValue,
            "appGroupIndex": selection.appGroupIndex,
            "windowIndex": selection.windowIndex,
            "windowID": selection.windowID
        ])
        lastCommittedSelection = selection
        lastCommitSource = source
        lastActivationWindowID = selection.windowID
        lastActivationError = nil
 
        guard let window = window(for: selection, in: viewModel ?? currentViewModel) else {
            lastActivationResult = .windowNotFound
            DevelopmentDiagnostics.log("quickSwitch.activation.commit.windowNotFound", [
                "source": source.rawValue,
                "appGroupIndex": selection.appGroupIndex,
                "windowIndex": selection.windowIndex,
                "windowID": selection.windowID
            ])
            onSnapshotUpdated?()
            return
        }
 
        DevelopmentDiagnostics.log("quickSwitch.activation.commit.hideBeforeActivation", [
            "source": source.rawValue,
            "windowID": window.id
        ])
        hide(reason: .userClosed)
 
        do {
            lastActivationResult = try windowActivationService.activate(window: window)
            DevelopmentDiagnostics.log("quickSwitch.activation.commit.result", [
                "source": source.rawValue,
                "windowID": window.id,
                "appName": window.app.name,
                "appPID": window.app.processIdentifier,
                "titleHash": DevelopmentDiagnostics.stableFingerprint(window.title),
                "titleLength": window.title.count,
                "titleIsEmpty": window.title.isEmpty,
                "result": lastActivationResult.map(Self.activationResultString)
            ])
        } catch {
            let errorSummary = DevelopmentDiagnostics.errorSummaryString(error)
            lastActivationResult = .failed(errorSummary)
            lastActivationError = errorSummary
            var fields: [String: CustomStringConvertible?] = [
                "source": source.rawValue,
                "windowID": window.id,
                "appName": window.app.name,
                "titleHash": DevelopmentDiagnostics.stableFingerprint(window.title),
                "titleLength": window.title.count,
                "titleIsEmpty": window.title.isEmpty
            ]
            DevelopmentDiagnostics.errorSummaryFields(error).forEach { fields[$0.key] = $0.value }
            DevelopmentDiagnostics.log("quickSwitch.activation.commit.error", fields)
        }
 
        if shouldCloseAfterActivation(lastActivationResult) {
            DevelopmentDiagnostics.log("quickSwitch.activation.commit.closeAfterActivation", [
                "result": lastActivationResult.map(Self.activationResultString)
            ])
        } else {
            DevelopmentDiagnostics.log("quickSwitch.activation.commit.refreshAfterActivation", [
                "result": lastActivationResult.map(Self.activationResultString)
            ])
        }
        onSnapshotUpdated?()
    }
 
    private func handleSpaceLaneClick(_ spaceID: UInt64, clickCount: Int) {
        if clickCount >= 2 {
            if pendingLockedSpaceUnlockSpaceID == spaceID {
                cancelPendingLockedSpaceUnlock()
                activateLockedSpace(spaceID)
            } else {
                DevelopmentDiagnostics.log("quickSwitch.spaceFilter.doubleClick.ignored", [
                    "spaceID": spaceID,
                    "lockedSpaceID": lockedSpaceFilterID ?? NSNull(),
                    "pendingUnlockSpaceID": pendingLockedSpaceUnlockSpaceID ?? NSNull()
                ])
            }
            return
        }
 
        cancelPendingLockedSpaceUnlock()
        if lockedSpaceFilterID == spaceID {
            scheduleLockedSpaceUnlock(spaceID)
            return
        }
 
        applySpaceLaneSingleClick(spaceID)
    }
 
    private func applySpaceLaneSingleClick(_ spaceID: UInt64) {
        guard let sourceViewModel else {
            lastSpaceFilterAction = "blockedNoSourceViewModel"
            DevelopmentDiagnostics.log("quickSwitch.spaceFilter.click.blocked", [
                "spaceID": spaceID,
                "reason": "noSourceViewModel"
            ])
            onSnapshotUpdated?()
            return
        }
 
        if let fullscreenSelection = QuickSwitchSpaceFilterPolicy.singleFullscreenSelection(
            inSpaceID: spaceID,
            viewModel: sourceViewModel
        ) {
            lockedSpaceFilterID = nil
            selectionBeforeSpaceFilter = nil
            lastSpaceFilterAction = "activateSingleFullscreen"
            let visibleViewModel = projectedViewModel(from: sourceViewModel)
            currentViewModel = visibleViewModel
            view.applyProjected(viewModel: visibleViewModel)
            DevelopmentDiagnostics.log("quickSwitch.spaceFilter.singleFullscreenActivate", [
                "spaceID": spaceID,
                "windowID": fullscreenSelection.windowID
            ])
            commitSelection(fullscreenSelection, source: .mouse, in: sourceViewModel)
            return
        }
 
        let preferredSelection: QuickSwitchSelection?
        if lockedSpaceFilterID == spaceID {
            lockedSpaceFilterID = nil
            preferredSelection = selectionBeforeSpaceFilter
            selectionBeforeSpaceFilter = nil
            lastSpaceFilterAction = "unlock"
        } else {
            if lockedSpaceFilterID == nil {
                selectionBeforeSpaceFilter = view.effectiveSelectionForProjection()
                lastSpaceFilterAction = "lock"
            } else {
                lastSpaceFilterAction = "switch"
            }
            lockedSpaceFilterID = spaceID
            preferredSelection = nil
        }
 
        let visibleViewModel = projectedViewModel(
            from: sourceViewModel,
            preferredSelection: preferredSelection
        )
        currentViewModel = visibleViewModel
        view.applyProjected(viewModel: visibleViewModel)
        DevelopmentDiagnostics.log("quickSwitch.spaceFilter.apply", [
            "spaceID": spaceID,
            "lockedSpaceID": lockedSpaceFilterID,
            "action": lastSpaceFilterAction,
            "visibleAppCount": visibleViewModel.appShelf.count,
            "visibleWindowCount": visibleViewModel.windowCount
        ])
 
        if disableScreenshotRefresh {
            view.markScreenshotsNotRequested(for: visibleViewModel, reason: "disabledByLaunchOption")
            screenshotTask?.cancel()
            screenshotTask = nil
        } else {
            scheduleScreenshotRefresh(for: visibleViewModel, generation: sessionGeneration)
        }
 
        onSnapshotUpdated?()
    }
 
    @discardableResult
    private func handleBackgroundClick() -> Bool {
        guard lockedSpaceFilterID != nil else { return false }
 
        cancelPendingLockedSpaceUnlock()
        let unlocked = unlockSpaceFilter(action: "unlockBackground")
        DevelopmentDiagnostics.log("quickSwitch.spaceFilter.backgroundUnlock", [
            "unlocked": unlocked
        ])
        return unlocked
    }
 
    private func scheduleLockedSpaceUnlock(_ spaceID: UInt64) {
        pendingLockedSpaceUnlockSpaceID = spaceID
        let delay = max(0.12, NSEvent.doubleClickInterval + 0.03)
        DevelopmentDiagnostics.log("quickSwitch.spaceFilter.unlock.pending", [
            "spaceID": spaceID,
            "delay": delay
        ])
 
        pendingLockedSpaceUnlockTask = Task { @MainActor [weak self] in
            try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
            guard !Task.isCancelled,
                  let self,
                  self.pendingLockedSpaceUnlockSpaceID == spaceID
            else {
                return
            }
 
            self.pendingLockedSpaceUnlockSpaceID = nil
            self.pendingLockedSpaceUnlockTask = nil
            self.applySpaceLaneSingleClick(spaceID)
        }
    }
 
    private func cancelPendingLockedSpaceUnlock() {
        pendingLockedSpaceUnlockTask?.cancel()
        pendingLockedSpaceUnlockTask = nil
        pendingLockedSpaceUnlockSpaceID = nil
    }
 
    @discardableResult
    private func unlockSpaceFilter(action: String) -> Bool {
        guard let sourceViewModel, lockedSpaceFilterID != nil else {
            return false
        }
 
        lockedSpaceFilterID = nil
        let preferredSelection = selectionBeforeSpaceFilter
        selectionBeforeSpaceFilter = nil
        lastSpaceFilterAction = action
 
        let visibleViewModel = projectedViewModel(
            from: sourceViewModel,
            preferredSelection: preferredSelection
        )
        currentViewModel = visibleViewModel
        view.applyProjected(viewModel: visibleViewModel)
 
        if disableScreenshotRefresh {
            view.markScreenshotsNotRequested(for: visibleViewModel, reason: "disabledByLaunchOption")
            screenshotTask?.cancel()
            screenshotTask = nil
        } else {
            scheduleScreenshotRefresh(for: visibleViewModel, generation: sessionGeneration)
        }
 
        onSnapshotUpdated?()
        return true
    }
 
    private func activateLockedSpace(_ spaceID: UInt64) {
        guard lockedSpaceFilterID == spaceID else {
            DevelopmentDiagnostics.log("quickSwitch.spaceFilter.activateSpace.ignored", [
                "spaceID": spaceID,
                "lockedSpaceID": lockedSpaceFilterID ?? NSNull()
            ])
            return
        }
 
        guard let sourceViewModel else {
            lastSpaceFilterAction = "blockedNoSourceViewModel"
            DevelopmentDiagnostics.log("quickSwitch.spaceFilter.activateSpace.blocked", [
                "spaceID": spaceID,
                "reason": "noSourceViewModel"
            ])
            onSnapshotUpdated?()
            return
        }
 
        lockedSpaceFilterID = nil
        selectionBeforeSpaceFilter = nil
        lastSpaceFilterAction = "activateSpace"
        lastSpaceActivationSpaceID = spaceID
        lastSpaceActivationDidRequestFocus = nil
        lastSpaceActivationDisplayIdentifier = nil
        lastSpaceActivationPreviousCurrentSpaceID = nil
        lastSpaceActivationError = nil
 
        let visibleViewModel = projectedViewModel(from: sourceViewModel)
        currentViewModel = visibleViewModel
        view.applyProjected(viewModel: visibleViewModel)
 
        DevelopmentDiagnostics.log("quickSwitch.spaceFilter.activateSpace.start", [
            "spaceID": spaceID
        ])
        hide(reason: .userClosed)
        let outcome = spaceActivationService.activate(spaceID: spaceID)
        lastSpaceActivationSpaceID = outcome.targetSpaceID
        lastSpaceActivationDidRequestFocus = outcome.didRequestFocus
        lastSpaceActivationDisplayIdentifier = outcome.displayIdentifier
        lastSpaceActivationPreviousCurrentSpaceID = outcome.previousCurrentSpaceID
        lastSpaceActivationError = outcome.error
        DevelopmentDiagnostics.log("quickSwitch.spaceFilter.activateSpace.result", [
            "spaceID": outcome.targetSpaceID,
            "displayIdentifier": outcome.displayIdentifier ?? NSNull(),
            "previousCurrentSpaceID": outcome.previousCurrentSpaceID ?? NSNull(),
            "didRequestFocus": outcome.didRequestFocus,
            "error": outcome.error ?? NSNull()
        ])
        onSnapshotUpdated?()
    }
 
    private func requestClose(_ request: QuickSwitchCloseRequest) {
        DevelopmentDiagnostics.log("quickSwitch.close.request.start", [
            "targetKind": request.kindDescription,
            "appGroupIndex": request.appGroupIndex,
            "appName": request.appName,
            "windowID": request.windowID,
            "titleHash": DevelopmentDiagnostics.stableFingerprint(request.title),
            "titleLength": request.title.count
        ])
        lastCloseTargetKind = request.kindDescription
        lastCloseAppGroupIndex = request.appGroupIndex
        lastCloseAppName = request.appName
        lastCloseWindowID = request.windowID
        lastCloseError = nil
        pendingCloseVerification = nil
        view.showCloseProgress(for: request)
 
        do {
            let result: WindowCloseResult
            switch request {
            case .app:
                result = try windowCloseService.close(app: request.app)
            case .window:
                guard let window = request.window else {
                    lastCloseResult = .windowNotFound
                    suppressCloseTarget(request)
                    view.clearCloseFeedback()
                    onSnapshotUpdated?()
                    return
                }
                result = try windowCloseService.close(window: window)
            }
            lastCloseResult = result
            DevelopmentDiagnostics.log("quickSwitch.close.request.result", [
                "targetKind": request.kindDescription,
                "appGroupIndex": request.appGroupIndex,
                "appName": request.appName,
                "windowID": request.windowID,
                "result": Self.closeResultString(result)
            ])
            if result == .requested {
                DevelopmentDiagnostics.log("quickSwitch.close.request.refreshAfterClose", [
                    "targetKind": request.kindDescription,
                    "appGroupIndex": request.appGroupIndex,
                    "appName": request.appName,
                    "windowID": request.windowID,
                    "optimisticRemove": false
                ])
                pendingCloseVerification = PendingCloseVerification(
                    request: request,
                    generation: sessionGeneration,
                    attempt: 0
                )
                scheduleSnapshotRefresh(
                    for: sessionGeneration,
                    replayDebugCommands: false,
                    delay: Self.closeVerificationRetryDelays[0]
                )
            } else if result == .windowNotFound {
                suppressCloseTarget(request)
                view.clearCloseFeedback()
                DevelopmentDiagnostics.log("quickSwitch.close.request.staleTargetRemoved", [
                    "targetKind": request.kindDescription,
                    "appGroupIndex": request.appGroupIndex,
                    "appName": request.appName,
                    "windowID": request.windowID
                ])
                scheduleSnapshotRefresh(
                    for: sessionGeneration,
                    replayDebugCommands: false,
                    delay: 0.10
                )
            } else {
                view.showCloseFailure(message: closeFailureMessage(for: result), for: request)
            }
        } catch {
            let errorSummary = DevelopmentDiagnostics.errorSummaryString(error)
            lastCloseResult = .failed(errorSummary)
            lastCloseError = errorSummary
            pendingCloseVerification = nil
            view.showCloseFailure(message: Self.closeFailedMessage, for: request)
            var fields: [String: CustomStringConvertible?] = [
                "targetKind": request.kindDescription,
                "appGroupIndex": request.appGroupIndex,
                "appName": request.appName,
                "windowID": request.windowID,
                "titleHash": DevelopmentDiagnostics.stableFingerprint(request.title),
                "titleLength": request.title.count
            ]
            DevelopmentDiagnostics.errorSummaryFields(error).forEach { fields[$0.key] = $0.value }
            DevelopmentDiagnostics.log("quickSwitch.close.request.error", fields)
        }
 
        onSnapshotUpdated?()
    }
 
    private func suppressCloseTarget(_ request: QuickSwitchCloseRequest) {
        let target = SuppressedCloseTarget(request)
        if !suppressedCloseTargets.contains(target) {
            suppressedCloseTargets.append(target)
        }
 
        guard let sourceViewModel else { return }
        let visibleViewModel = projectedViewModel(from: sourceViewModel, preferredSelection: view.effectiveSelectionForProjection())
        self.currentViewModel = visibleViewModel
        view.applyProjected(viewModel: visibleViewModel)
        DevelopmentDiagnostics.log("quickSwitch.close.optimisticRemove", [
            "targetKind": request.kindDescription,
            "appGroupIndex": request.appGroupIndex,
            "appName": request.appName,
            "windowID": request.windowID,
            "remainingWindowCount": visibleViewModel.windowCount,
            "suppressedCloseTargetCount": suppressedCloseTargets.count
        ])
    }
 
    private func viewModel(
        _ viewModel: QuickSwitchViewModel,
        excluding suppressedTargets: [SuppressedCloseTarget]
    ) -> QuickSwitchViewModel {
        guard !suppressedTargets.isEmpty else { return viewModel }
 
        var nextGlobalIndex = 0
        let columns = viewModel.waterfallColumns.compactMap { column -> QuickSwitchWaterfallColumnViewModel? in
            let windows = column.windows.filter { card in
                !suppressedTargets.contains { target in
                    suppressedTarget(target, matches: card)
                }
            }
            guard !windows.isEmpty else { return nil }
 
            let reindexedWindows = windows.enumerated().map { windowIndex, card in
                defer { nextGlobalIndex += 1 }
                return QuickSwitchWindowCardViewModel(
                    appGroupIndex: column.appGroupIndex,
                    windowIndex: windowIndex,
                    globalIndex: nextGlobalIndex,
                    window: card.window,
                    primarySpaceID: card.primarySpaceID,
                    primarySpaceLabel: card.primarySpaceLabel
                )
            }
 
            return QuickSwitchWaterfallColumnViewModel(
                appGroupIndex: column.appGroupIndex,
                app: column.app,
                windows: reindexedWindows
            )
        }
 
        let columnsByAppGroupIndex = Dictionary(uniqueKeysWithValues: columns.map { ($0.appGroupIndex, $0) })
        let appShelf = viewModel.appShelf.compactMap { item -> QuickSwitchAppShelfItemViewModel? in
            guard let column = columnsByAppGroupIndex[item.appGroupIndex] else { return nil }
            return QuickSwitchAppShelfItemViewModel(
                appGroupIndex: item.appGroupIndex,
                app: item.app,
                windowCount: column.windows.count,
                primarySpaceIDs: orderedPrimarySpaceIDs(in: column.windows),
                hasMinimizedWindows: column.windows.contains { $0.window.isMinimized },
                hasFullscreenWindows: column.windows.contains { $0.window.isFullscreen }
            )
        }
 
        return QuickSwitchViewModel(
            displays: viewModel.displays,
            appShelf: appShelf,
            waterfallColumns: columns,
            initialSelection: initialSelection(in: columns, fallback: viewModel.initialSelection),
            lockedSpaceID: viewModel.lockedSpaceID
        )
    }
 
    private func projectedViewModel(
        from sourceViewModel: QuickSwitchViewModel,
        preferredSelection: QuickSwitchSelection? = nil
    ) -> QuickSwitchViewModel {
        let closeProjectedViewModel = viewModel(sourceViewModel, excluding: suppressedCloseTargets)
        return QuickSwitchSpaceFilterPolicy.projectedViewModel(
            from: closeProjectedViewModel,
            lockedSpaceID: lockedSpaceFilterID,
            preferredSelection: preferredSelection
        )
    }
 
    private func orderedPrimarySpaceIDs(in cards: [QuickSwitchWindowCardViewModel]) -> [UInt64] {
        var seen = Set<UInt64>()
        var result: [UInt64] = []
        for card in cards {
            guard let spaceID = card.primarySpaceID,
                  !seen.contains(spaceID)
            else {
                continue
            }
            seen.insert(spaceID)
            result.append(spaceID)
        }
        return result
    }
 
    private func suppressedTarget(
        _ target: SuppressedCloseTarget,
        matches card: QuickSwitchWindowCardViewModel
    ) -> Bool {
        switch target {
        case .window(let windowID):
            return card.window.id == windowID
        case .app(let app):
            return appMatches(card.window.app, app)
        }
    }
 
    private func initialSelection(
        in columns: [QuickSwitchWaterfallColumnViewModel],
        fallback: QuickSwitchSelection?
    ) -> QuickSwitchSelection? {
        if let fallback,
           columns.contains(where: { column in
               column.appGroupIndex == fallback.appGroupIndex
                   && column.windows.contains { $0.window.id == fallback.windowID }
           }) {
            return fallback
        }
 
        guard let firstCard = columns.first?.windows.first else { return nil }
        return QuickSwitchSelection(
            appGroupIndex: firstCard.appGroupIndex,
            windowIndex: firstCard.windowIndex,
            windowID: firstCard.window.id
        )
    }
 
    private func shouldCloseAfterActivation(_ result: WindowActivationResult?) -> Bool {
        switch result {
        case .activated, .restored, .appActivatedOnly:
            return true
        case .windowNotFound, .unsupported, .activationFailed, .failed, nil:
            return false
        }
    }
 
    private func window(
        for selection: QuickSwitchSelection,
        in viewModel: QuickSwitchViewModel?
    ) -> AlignerWindow? {
        viewModel?
            .waterfallColumns
            .flatMap(\.windows)
            .first { card in
                card.appGroupIndex == selection.appGroupIndex
                    && card.windowIndex == selection.windowIndex
                    && card.window.id == selection.windowID
            }?
            .window
    }
 
    private func applySnapshotResult(
        _ result: SnapshotLoadResult,
        generation: Int,
        replayDebugCommands: Bool
    ) {
        guard generation == sessionGeneration, isVisible else {
            DevelopmentDiagnostics.log("quickSwitch.snapshot.drop", [
                "resultGeneration": generation,
                "currentGeneration": sessionGeneration,
                "visible": isVisible
            ])
            return
        }
 
        switch result {
        case .success(let success):
            currentSnapshot = success.snapshot
            sourceViewModel = success.viewModel
            let visibleViewModel = projectedViewModel(
                from: success.viewModel,
                preferredSelection: view.effectiveSelectionForProjection()
            )
            currentViewModel = visibleViewModel
            lastSnapshotError = nil
            snapshotStartElapsedMilliseconds = success.timing.startElapsedMilliseconds
            snapshotDurationMilliseconds = success.timing.durationMilliseconds
            snapshotRanOnMainThread = success.timing.ranOnMainThread
            view.apply(viewModel: visibleViewModel)
            view.recordPerformanceFirstFrameIfNeeded()
            if replayDebugCommands {
                view.performDebugKeyboardCommands(debugKeySequence)
                view.performDebugMouseCommands(debugMouseSequence)
            }
            verifyPendingCloseIfNeeded(in: success.viewModel)
            DevelopmentDiagnostics.log("quickSwitch.snapshot.success", [
                "generation": generation,
                "displayCount": success.viewModel.displays.count,
                "spaceCount": success.viewModel.spaceCount,
                "appCount": success.viewModel.appShelf.count,
                "windowCount": success.viewModel.windowCount,
                "durationMilliseconds": success.timing.durationMilliseconds,
                "ranOnMainThread": success.timing.ranOnMainThread,
                "initialSelectionWindowID": success.viewModel.initialSelection?.windowID
            ])
            guard isVisible else { return }
            if disableScreenshotRefresh {
                view.markScreenshotsNotRequested(for: visibleViewModel, reason: "disabledByLaunchOption")
                screenshotTask?.cancel()
                screenshotTask = nil
                DevelopmentDiagnostics.log("quickSwitch.screenshot.disabledByLaunchOption", [
                    "generation": generation
                ])
            } else {
                scheduleScreenshotRefresh(for: visibleViewModel, generation: generation)
            }
        case .failure(let failure):
            currentSnapshot = nil
            sourceViewModel = nil
            currentViewModel = nil
            lastSnapshotError = failure.message
            snapshotStartElapsedMilliseconds = failure.timing.startElapsedMilliseconds
            snapshotDurationMilliseconds = failure.timing.durationMilliseconds
            snapshotRanOnMainThread = failure.timing.ranOnMainThread
            view.apply(viewModel: nil)
            screenshotTask?.cancel()
            screenshotTask = nil
            failPendingCloseVerificationIfNeeded(reason: "snapshotFailure")
            DevelopmentDiagnostics.log("quickSwitch.snapshot.failure", [
                "generation": generation,
                "message": failure.message,
                "durationMilliseconds": failure.timing.durationMilliseconds,
                "ranOnMainThread": failure.timing.ranOnMainThread
            ])
        }
 
        onSnapshotUpdated?()
    }
 
    private func verifyPendingCloseIfNeeded(in viewModel: QuickSwitchViewModel) {
        guard let pendingCloseVerification else { return }
 
        self.pendingCloseVerification = nil
        let request = pendingCloseVerification.request
        let stillPresent = closeTargetStillPresent(request, in: viewModel)
        DevelopmentDiagnostics.log("quickSwitch.close.verify", [
            "targetKind": request.kindDescription,
            "appGroupIndex": request.appGroupIndex,
            "appName": request.appName,
            "windowID": request.windowID,
            "requestedGeneration": pendingCloseVerification.generation,
            "currentGeneration": sessionGeneration,
            "attempt": pendingCloseVerification.attempt,
            "stillPresent": stillPresent
        ])
 
        if stillPresent {
            let nextAttempt = pendingCloseVerification.attempt + 1
            if nextAttempt < Self.closeVerificationRetryDelays.count {
                self.pendingCloseVerification = PendingCloseVerification(
                    request: request,
                    generation: sessionGeneration,
                    attempt: nextAttempt
                )
                scheduleSnapshotRefresh(
                    for: sessionGeneration,
                    replayDebugCommands: false,
                    delay: Self.closeVerificationRetryDelays[nextAttempt]
                )
            } else {
                view.showCloseFailure(message: Self.closeRequestStillPresentMessage, for: request)
            }
        } else {
            view.clearCloseFeedback()
        }
    }
 
    private func failPendingCloseVerificationIfNeeded(reason: String) {
        guard let pendingCloseVerification else { return }
 
        self.pendingCloseVerification = nil
        let request = pendingCloseVerification.request
        DevelopmentDiagnostics.log("quickSwitch.close.verify.failed", [
            "targetKind": request.kindDescription,
            "appGroupIndex": request.appGroupIndex,
            "appName": request.appName,
            "windowID": request.windowID,
            "requestedGeneration": pendingCloseVerification.generation,
            "currentGeneration": sessionGeneration,
            "reason": reason
        ])
        view.showCloseFailure(message: Self.closeFailedMessage, for: request)
    }
 
    private func closeFailureMessage(for result: WindowCloseResult) -> String {
        switch result {
        case .requested:
            return Self.closeRequestStillPresentMessage
        case .windowNotFound:
            return Self.closeTargetNotFoundMessage
        case .unsupported:
            return Self.closeUnsupportedMessage
        case .appNotFound, .failed:
            return Self.closeFailedMessage
        }
    }
 
    private func closeTargetStillPresent(
        _ request: QuickSwitchCloseRequest,
        in viewModel: QuickSwitchViewModel
    ) -> Bool {
        switch request {
        case .window:
            guard let windowID = request.windowID else { return false }
            return viewModel.waterfallColumns
                .flatMap(\.windows)
                .contains { $0.window.id == windowID }
        case .app:
            return viewModel.appShelf.contains { appMatches($0.app, request.app) }
                || viewModel.waterfallColumns.contains { appMatches($0.app, request.app) }
        }
    }
 
    private func appMatches(_ candidate: AlignerApp, _ target: AlignerApp) -> Bool {
        if let candidatePID = candidate.processIdentifier,
           let targetPID = target.processIdentifier {
            return candidatePID == targetPID
        }
 
        if !candidate.bundleIdentifier.isEmpty,
           candidate.bundleIdentifier == target.bundleIdentifier {
            return true
        }
 
        return candidate.name == target.name
    }
 
    private func scheduleScreenshotRefresh(
        for viewModel: QuickSwitchViewModel,
        generation: Int
    ) {
        screenshotTask?.cancel()
        let provider = screenshotProvider
        let session = screenshotSession
        let allWindows = viewModel.waterfallColumns.flatMap { column in
            column.windows.map(\.window)
        }
        let windows = allWindows.filter(Self.shouldRequestScreenshot)
        let skippedWindows = allWindows.filter { !Self.shouldRequestScreenshot(for: $0) }
        DevelopmentDiagnostics.log("quickSwitch.screenshot.schedule", [
            "generation": generation,
            "eligibleCount": windows.count,
            "skippedCount": skippedWindows.count
        ])
 
        for window in skippedWindows {
            view.markScreenshotNotRequested(for: window.id, reason: "skeletonPreferred")
        }
        if !skippedWindows.isEmpty {
            onSnapshotUpdated?()
        }
        guard !windows.isEmpty else {
            screenshotTask = nil
            return
        }
 
        screenshotTask = Task { [weak self] in
            guard let self else { return }
 
            var retryWindows: [AlignerWindow] = []
            for window in windows {
                guard !Task.isCancelled else { return }
                let resolution = await provider.resolvedScreenshot(for: window, in: session)
                guard !Task.isCancelled, generation == self.sessionGeneration, self.isVisible else { return }
                self.view.applyScreenshot(resolution, for: window.id)
                DevelopmentDiagnostics.log("quickSwitch.screenshot.resolved", [
                    "generation": generation,
                    "windowID": window.id,
                    "appName": window.app.name,
                    "source": Self.screenshotSourceString(resolution.source)
                ])
                self.onSnapshotUpdated?()
 
                if case .skeletonFallback(let reason) = resolution.source,
                   Self.shouldRetryScreenshot(after: reason) {
                    retryWindows.append(window)
                }
            }
 
            for window in retryWindows {
                guard !Task.isCancelled else { return }
                let retryResolution = await provider.resolvedScreenshot(for: window, in: session)
                guard !Task.isCancelled, generation == self.sessionGeneration, self.isVisible else { return }
                self.view.applyScreenshot(retryResolution, for: window.id)
                DevelopmentDiagnostics.log("quickSwitch.screenshot.retryResolved", [
                    "generation": generation,
                    "windowID": window.id,
                    "appName": window.app.name,
                    "source": Self.screenshotSourceString(retryResolution.source)
                ])
                self.onSnapshotUpdated?()
            }
        }
    }
 
    private static func shouldRequestScreenshot(for window: AlignerWindow) -> Bool {
        switch ThumbnailPolicy.preferredStrategy(for: window.app.category) {
        case .screenshotPreferred:
            return true
        case .skeletonPreferred, .fallbackSkeleton:
            return false
        }
    }
 
    private static func shouldRetryScreenshot(after reason: ScreenshotFallbackReason) -> Bool {
        switch reason {
        case .screenRecordingDenied, .syntheticWindowID, .retryLimitReached:
            return false
        case .captureFailed, .timedOut, .invalidCapture:
            return true
        }
    }
 
    private func elapsedSinceShowStart() -> Double? {
        guard let showStartTime else { return nil }
        return (CACurrentMediaTime() - showStartTime) * 1000
    }
 
    private func updateLifecycleWindowHighWaterMark() {
        lifecycleMaximumOverlayWindows = max(
            lifecycleMaximumOverlayWindows,
            overlayWindowCount()
        )
        lifecycleMaximumVisibleOverlayWindows = max(
            lifecycleMaximumVisibleOverlayWindows,
            visibleOverlayWindowCount()
        )
    }
 
    private func overlayWindowCount() -> Int {
        NSApp.windows.filter { window in
            window.title == Self.overlayTitle
        }.count
    }
 
    private func visibleOverlayWindowCount() -> Int {
        NSApp.windows.filter { window in
            window.title == Self.overlayTitle && window.isVisible
        }.count
    }
 
    private static func activationResultString(_ result: WindowActivationResult) -> String {
        switch result {
        case .activated:
            return "activated"
        case .restored:
            return "restored"
        case .appActivatedOnly:
            return "appActivatedOnly"
        case .windowNotFound:
            return "windowNotFound"
        case .unsupported:
            return "unsupported"
        case .activationFailed:
            return "activationFailed"
        case .failed(let message):
            return "failed(\(message))"
        }
    }
 
    private static func closeResultString(_ result: WindowCloseResult) -> String {
        switch result {
        case .requested:
            return "requested"
        case .windowNotFound:
            return "windowNotFound"
        case .appNotFound:
            return "appNotFound"
        case .unsupported:
            return "unsupported"
        case .failed(let message):
            return "failed(\(message))"
        }
    }
 
    private static func screenshotSourceString(_ source: ScreenshotResolutionSource) -> String {
        switch source {
        case .realScreenshot:
            return "realScreenshot"
        case .skeletonFallback(let reason):
            return "skeletonFallback(\(reason))"
        }
    }
 
    private static func dismissReasonString(_ reason: DismissReason) -> String {
        switch reason {
        case .escape:
            return "escape"
        case .focusLost:
            return "focusLost"
        case .systemCriticalWindow:
            return "systemCriticalWindow"
        case .timeout:
            return "timeout"
        case .userClosed:
            return "userClosed"
        }
    }
 
}
 
private struct SnapshotLoadTiming: Sendable {
    let startElapsedMilliseconds: Double?
    let durationMilliseconds: Double
    let ranOnMainThread: Bool
}
 
private struct SnapshotLoadSuccess: Sendable {
    let snapshot: QuickSwitchSnapshot
    let viewModel: QuickSwitchViewModel
    let timing: SnapshotLoadTiming
}
 
private struct SnapshotLoadFailure: Sendable {
    let message: String
    let timing: SnapshotLoadTiming
}
 
private enum SnapshotLoadResult: Sendable {
    case success(SnapshotLoadSuccess)
    case failure(SnapshotLoadFailure)
 
    static func load(
        using loader: any QuickSwitchSnapshotLoading,
        showStartTime: CFTimeInterval?
    ) -> SnapshotLoadResult {
        let startedAt = CACurrentMediaTime()
        let startedOnMainThread = Thread.isMainThread
 
        do {
            let load = try loader.snapshot()
            let viewModel = QuickSwitchViewModelBuilder.viewModel(
                from: load.snapshot,
                currentSpaceIDs: load.currentSpaceIDs
            )
            return .success(SnapshotLoadSuccess(
                snapshot: load.snapshot,
                viewModel: viewModel,
                timing: SnapshotLoadTiming(
                    startElapsedMilliseconds: showStartTime.map { (startedAt - $0) * 1000 },
                    durationMilliseconds: (CACurrentMediaTime() - startedAt) * 1000,
                    ranOnMainThread: startedOnMainThread
                )
            ))
        } catch {
            return .failure(SnapshotLoadFailure(
                message: DevelopmentDiagnostics.errorSummaryString(error),
                timing: SnapshotLoadTiming(
                    startElapsedMilliseconds: showStartTime.map { (startedAt - $0) * 1000 },
                    durationMilliseconds: (CACurrentMediaTime() - startedAt) * 1000,
                    ranOnMainThread: startedOnMainThread
                )
            ))
        }
    }
}
 
struct Round1QuickSwitchLaunchOptions {
    let openQuickSwitch: Bool
    let simulateAccessibilityDenied: Bool
    let simulateScreenRecordingDenied: Bool
    let autoHideAfter: TimeInterval?
    let quitAfter: TimeInterval?
    let reportPath: String?
    let snapshotLoadDelay: TimeInterval?
    let fixtureAppCount: Int?
    let fixtureWindowsPerApp: Int?
    let fixtureMultiDisplay: Bool
    let fixtureCandidateFiltering: Bool
    let fixtureActivation: Bool
    let fixtureSpaceFilter: Bool
    let fixtureSplitView: Bool
    let fixtureMultiPageIdentity: Bool
    let debugHoveredAppGroupIndex: Int?
    let debugOverlayWidth: CGFloat?
    let debugKeySequence: [String]
    let debugMouseSequence: [String]
    let debugSystemCriticalAfter: TimeInterval?
    let debugWindowActivation: Bool
    let debugWindowClose: Bool
    let disableScreenshotRefresh: Bool
    let waterfallViewMode: QuickSwitchWaterfallViewMode?
    let lifecycleCycles: Int?
    let lifecycleInterval: TimeInterval
    let lifecycleVisibleDuration: TimeInterval
 
    static func parse(arguments: [String]) -> Round1QuickSwitchLaunchOptions {
        Round1QuickSwitchLaunchOptions(
            openQuickSwitch: arguments.contains("--round01-open-quick-switch"),
            simulateAccessibilityDenied: arguments.contains("--round01-simulate-accessibility-denied"),
            simulateScreenRecordingDenied: arguments.contains("--round01-simulate-screen-recording-denied"),
            autoHideAfter: timeInterval(for: "--round01-quick-switch-auto-hide-after", in: arguments),
            quitAfter: timeInterval(for: "--round01-quick-switch-quit-after", in: arguments),
            reportPath: stringValue(for: "--round01-quick-switch-report", in: arguments),
            snapshotLoadDelay: timeInterval(for: "--round01-snapshot-load-delay", in: arguments),
            fixtureAppCount: intValue(for: "--round01-fixture-app-count", in: arguments),
            fixtureWindowsPerApp: intValue(for: "--round01-fixture-windows-per-app", in: arguments),
            fixtureMultiDisplay: arguments.contains("--round01-fixture-multi-display"),
            fixtureCandidateFiltering: arguments.contains("--round01-fixture-candidate-filtering"),
            fixtureActivation: arguments.contains("--round01-fixture-window-activation"),
            fixtureSpaceFilter: arguments.contains("--round01-fixture-space-filter"),
            fixtureSplitView: arguments.contains("--round01-fixture-split-view"),
            fixtureMultiPageIdentity: arguments.contains("--round01-fixture-multi-page-identity"),
            debugHoveredAppGroupIndex: intValue(for: "--round01-debug-hover-app-index", in: arguments),
            debugOverlayWidth: cgFloatValue(for: "--round01-debug-overlay-width", in: arguments),
            debugKeySequence: stringListValue(for: "--round01-debug-key-sequence", in: arguments),
            debugMouseSequence: stringListValue(for: "--round01-debug-mouse-sequence", in: arguments),
            debugSystemCriticalAfter: timeInterval(for: "--round01-debug-system-critical-after", in: arguments),
            debugWindowActivation: arguments.contains("--round01-debug-window-activation"),
            debugWindowClose: arguments.contains("--round01-debug-window-close"),
            disableScreenshotRefresh: arguments.contains("--round01-disable-screenshot-refresh"),
            waterfallViewMode: waterfallViewModeValue(for: "--round01-waterfall-view-mode", in: arguments),
            lifecycleCycles: intValue(for: "--round01-quick-switch-lifecycle-cycles", in: arguments),
            lifecycleInterval: timeInterval(for: "--round01-quick-switch-lifecycle-interval", in: arguments) ?? 0.02,
            lifecycleVisibleDuration: timeInterval(for: "--round01-quick-switch-lifecycle-visible-duration", in: arguments) ?? 0.16
        )
    }
 
    private static func timeInterval(for key: String, in arguments: [String]) -> TimeInterval? {
        let prefix = "\(key)="
        guard let argument = arguments.first(where: { $0.hasPrefix(prefix) }) else {
            return nil
        }
 
        return TimeInterval(argument.dropFirst(prefix.count))
    }
 
    private static func stringValue(for key: String, in arguments: [String]) -> String? {
        let prefix = "\(key)="
        guard let argument = arguments.first(where: { $0.hasPrefix(prefix) }) else {
            return nil
        }
 
        return String(argument.dropFirst(prefix.count))
    }
 
    private static func waterfallViewModeValue(
        for key: String,
        in arguments: [String]
    ) -> QuickSwitchWaterfallViewMode? {
        guard let value = stringValue(for: key, in: arguments) else { return nil }
 
        switch value {
        case "vertical", "vertical-columns", "verticalColumns":
            return .verticalColumns
        case "horizontal", "horizontal-masonry", "horizontalMasonry":
            return .horizontalMasonry
        default:
            return nil
        }
    }
 
    private static func intValue(for key: String, in arguments: [String]) -> Int? {
        stringValue(for: key, in: arguments).flatMap(Int.init)
    }
 
    private static func cgFloatValue(for key: String, in arguments: [String]) -> CGFloat? {
        stringValue(for: key, in: arguments)
            .flatMap(Double.init)
            .map { CGFloat($0) }
    }
 
    private static func stringListValue(for key: String, in arguments: [String]) -> [String] {
        guard let value = stringValue(for: key, in: arguments) else { return [] }
 
        return value
            .split(separator: ",")
            .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
            .filter { !$0.isEmpty }
    }
}
 
@MainActor
private final class StderrScreenshotDebugLogger: ScreenshotDebugLogging {
    func record(_ event: ScreenshotDebugEvent) {
        DevelopmentDiagnostics.log("quickSwitch.screenshot.debug", [
            "event": event.diagnosticDescription
        ])
        fputs("Round01 screenshot debug: \(event.diagnosticDescription)\n", stderr)
    }
}