|
| 1 | +import Foundation |
| 2 | +import IOKit.ps |
| 3 | + |
| 4 | +enum CodexCostCatchUpMode: String, Sendable { |
| 5 | + case automatic |
| 6 | + case accelerated |
| 7 | +} |
| 8 | + |
| 9 | +enum CodexCostCatchUpPowerSource: String, Sendable { |
| 10 | + case ac |
| 11 | + case battery |
| 12 | + case unknown |
| 13 | + |
| 14 | + static func current() -> Self { |
| 15 | + guard let info = IOPSCopyPowerSourcesInfo()?.takeRetainedValue(), |
| 16 | + let source = IOPSGetProvidingPowerSourceType(info)?.takeUnretainedValue() as String? |
| 17 | + else { |
| 18 | + return .unknown |
| 19 | + } |
| 20 | + if source == kIOPSACPowerValue as String { |
| 21 | + return .ac |
| 22 | + } |
| 23 | + if source == kIOPSBatteryPowerValue as String { |
| 24 | + return .battery |
| 25 | + } |
| 26 | + return .unknown |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +enum CodexCostCatchUpPauseReason: Sendable, Equatable { |
| 31 | + case lowPower |
| 32 | + case thermal |
| 33 | + case user |
| 34 | + case noProgress |
| 35 | + case error(String) |
| 36 | +} |
| 37 | + |
| 38 | +struct CodexCostCatchUpActivity: Sendable, Equatable { |
| 39 | + enum Phase: Sendable, Equatable { |
| 40 | + case indexing |
| 41 | + case paused |
| 42 | + case complete |
| 43 | + } |
| 44 | + |
| 45 | + let phase: Phase |
| 46 | + let mode: CodexCostCatchUpMode |
| 47 | + let processedBytes: Int64 |
| 48 | + let totalBytes: Int64 |
| 49 | + let completedFiles: Int |
| 50 | + let totalFiles: Int |
| 51 | + let pauseReason: CodexCostCatchUpPauseReason? |
| 52 | + let staleSnapshotUpdatedAt: Date? |
| 53 | + |
| 54 | + var fractionCompleted: Double? { |
| 55 | + guard self.totalBytes > 0 else { |
| 56 | + guard self.totalFiles > 0 else { return nil } |
| 57 | + return min(1, max(0, Double(self.completedFiles) / Double(self.totalFiles))) |
| 58 | + } |
| 59 | + return min(1, max(0, Double(self.processedBytes) / Double(self.totalBytes))) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +struct CodexCostCatchUpPolicy: Sendable { |
| 64 | + struct Input: Sendable { |
| 65 | + let mode: CodexCostCatchUpMode |
| 66 | + let previousActiveDuration: TimeInterval? |
| 67 | + let powerSource: CodexCostCatchUpPowerSource |
| 68 | + let lowPowerModeEnabled: Bool |
| 69 | + let thermalState: ProcessInfo.ThermalState |
| 70 | + } |
| 71 | + |
| 72 | + struct Decision: Sendable, Equatable { |
| 73 | + enum Action: Sendable, Equatable { |
| 74 | + case runAfter(TimeInterval) |
| 75 | + case pause(TimeInterval, CodexCostCatchUpPauseReason) |
| 76 | + } |
| 77 | + |
| 78 | + let action: Action |
| 79 | + let targetDutyCycle: Double? |
| 80 | + } |
| 81 | + |
| 82 | + static let automaticBurstDuration: TimeInterval = 2 |
| 83 | + static let constrainedRetryDelay: TimeInterval = 60 |
| 84 | + |
| 85 | + func decision(for input: Input) -> Decision { |
| 86 | + if input.thermalState == .critical { |
| 87 | + return Decision( |
| 88 | + action: .pause(Self.constrainedRetryDelay, .thermal), |
| 89 | + targetDutyCycle: nil) |
| 90 | + } |
| 91 | + if input.mode == .automatic { |
| 92 | + if input.lowPowerModeEnabled { |
| 93 | + return Decision( |
| 94 | + action: .pause(Self.constrainedRetryDelay, .lowPower), |
| 95 | + targetDutyCycle: nil) |
| 96 | + } |
| 97 | + if input.thermalState == .serious { |
| 98 | + return Decision( |
| 99 | + action: .pause(Self.constrainedRetryDelay, .thermal), |
| 100 | + targetDutyCycle: nil) |
| 101 | + } |
| 102 | + } |
| 103 | + if input.mode == .accelerated { |
| 104 | + return Decision(action: .runAfter(0), targetDutyCycle: 1) |
| 105 | + } |
| 106 | + |
| 107 | + let dutyCycle = switch input.powerSource { |
| 108 | + case .ac: 0.20 |
| 109 | + case .battery: 0.05 |
| 110 | + case .unknown: 0.15 |
| 111 | + } |
| 112 | + let activeDuration = max(0, input.previousActiveDuration ?? Self.automaticBurstDuration) |
| 113 | + let delay = activeDuration * (1 - dutyCycle) / dutyCycle |
| 114 | + return Decision(action: .runAfter(delay), targetDutyCycle: dutyCycle) |
| 115 | + } |
| 116 | +} |
0 commit comments