forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartupWarmupPolicy.swift
More file actions
132 lines (113 loc) · 4.58 KB
/
Copy pathStartupWarmupPolicy.swift
File metadata and controls
132 lines (113 loc) · 4.58 KB
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
import Foundation
struct StartupWarmupScheduleState {
private var didScheduleServiceWarmup = false
private var didScheduleDatabaseWarmup = false
mutating func reserveServiceWarmup() -> Bool {
guard !didScheduleServiceWarmup else { return false }
didScheduleServiceWarmup = true
return true
}
mutating func releaseServiceWarmup() {
didScheduleServiceWarmup = false
}
mutating func reserveDatabaseWarmup(dbAvailable: Bool) -> Bool {
guard dbAvailable, !didScheduleDatabaseWarmup else { return false }
didScheduleDatabaseWarmup = true
return true
}
mutating func releaseDatabaseWarmup() {
didScheduleDatabaseWarmup = false
}
}
struct RetryableDelayedStartGate {
private var isAttemptReserved = false
mutating func reserve() -> Bool {
guard !isAttemptReserved else { return false }
isAttemptReserved = true
return true
}
mutating func finishAttempt() {
isAttemptReserved = false
}
}
enum StartupWarmupTaskID: Hashable {
case serviceWarmup
case databaseWarmup
case dashboardNetworkRefresh
case chatPromptContextWarmup
case mcpKeyWarmup
case databaseRetry
case agentVMProvisioning
case conversationWarmup
case initialFileIndexing
case proactiveAssistantsStart
}
struct StartupWarmupSessionScope: Equatable {
let userId: String?
func matches(currentUserId: String?, isSignedIn: Bool) -> Bool {
isSignedIn && userId != nil && currentUserId == userId
}
}
struct DelayedFileIndexingBackfillState {
private var isScheduled = false
private(set) var shouldMarkComplete = false
mutating func reserveIfNeeded(hasCompletedBackfill: Bool) -> Bool {
guard !hasCompletedBackfill, !isScheduled else { return false }
isScheduled = true
shouldMarkComplete = false
return true
}
mutating func markScanCompleted() {
isScheduled = false
shouldMarkComplete = true
}
mutating func releaseReservation() {
isScheduled = false
shouldMarkComplete = false
}
}
enum TasksPageFirstUseLoadPolicy {
static func shouldLoadTasks(isActiveViewLoaded: Bool, isActiveViewLoading: Bool) -> Bool {
!isActiveViewLoaded && !isActiveViewLoading
}
}
enum StartupWarmupPolicy {
static let immediateWarmupDelay: TimeInterval = 0.25
static let deferredWarmupDelay: TimeInterval = 2.0
static let databaseRetryInitialDelay: TimeInterval = 1.0
static let databaseRetryMaxDelay: TimeInterval = 30.0
/// Bounds `RewindDatabase.initialize()` so a wedged open/migration (e.g. a
/// stalled disk, an actor deadlock) can't strand the app on the "Preparing
/// your data…" screen forever. On timeout, startup treats init as failed —
/// the UI unblocks and the existing exponential-backoff retry loop takes
/// over. Generous relative to typical (sub-second to low-single-digit-second)
/// opens so it never false-trips a merely slow first-launch migration.
static let databaseInitTimeout: Duration = .seconds(30)
static let mcpKeyWarmupDelay: TimeInterval = 0.5
static let dashboardNetworkRefreshDelay: TimeInterval = 4.0
static let initialSettingsSyncDelay: TimeInterval = 5.0
static let apiKeyFetchDelay: TimeInterval = 9.0
static let chatPromptContextWarmupDelay: TimeInterval = 10.0
static let screenActivitySyncInitialDelay: TimeInterval = 10.0
static let floatingBarPlanFetchDelay: TimeInterval = 0.0
static let agentVMProvisioningDelay: TimeInterval = 20.0
static let proactiveAssistantsStartDelay: TimeInterval = 6.0
static let conversationWarmupDelay: TimeInterval = 6.0
static let transcriptionRetryRecoveryDelay: TimeInterval = 8.0
static let initialFileIndexingDelay: TimeInterval = 45.0
/// Remaining warmup delay, measured from a launch anchor rather than from
/// the triggering event. Warmup delays exist to keep heavy work out of the
/// busy launch window, so late triggers must not re-pay the full delay:
/// that compounding made the Capture pill sit "paused" for 30-60+ seconds
/// after launch, and made conversations/tasks/memories show stale or empty
/// data for several seconds when the main UI first appears long after
/// launch (finishing onboarding, switching accounts). Negative elapsed
/// values (clock changes) clamp to the full delay.
static func remainingDelay(_ delay: TimeInterval, elapsedSinceLaunch: TimeInterval) -> TimeInterval {
max(0, delay - max(0, elapsedSinceLaunch))
}
/// Remaining delay before proactive monitoring may start (launch-anchored).
static func remainingProactiveAssistantsStartDelay(elapsedSinceLaunch: TimeInterval) -> TimeInterval {
remainingDelay(proactiveAssistantsStartDelay, elapsedSinceLaunch: elapsedSinceLaunch)
}
}