-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: add adaptive refresh frequency option #1861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
551a684
feat: add adaptive refresh frequency option
hhh2210 9e497b5
Keep reset-boundary refresh scheduling active in adaptive mode
hhh2210 fb4d30e
Pin the codex account in the reset-boundary heuristics fixture
hhh2210 071f93b
fix: advance adaptive refresh after interaction
steipete e9a956f
Merge remote-tracking branch 'origin/main' into codex/adaptive-refres…
steipete 30f2de7
Merge remote-tracking branch 'origin/main' into codex/adaptive-refres…
steipete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import Foundation | ||
|
|
||
| /// Decides how long to wait before the next automatic usage refresh. | ||
| /// Pure by construction: every signal arrives via `Input`, so the same | ||
| /// input always yields the same `Decision` with no clock or system reads. | ||
| struct AdaptiveRefreshPolicy: Sendable { | ||
| struct Input: Sendable, Equatable { | ||
| let now: Date | ||
| let lastMenuOpenAt: Date? | ||
| let lowPowerModeEnabled: Bool | ||
| let thermalState: ProcessInfo.ThermalState | ||
| } | ||
|
|
||
| enum Reason: String, Sendable { | ||
| case recentInteraction | ||
| case warm | ||
| case idle | ||
| case longIdle | ||
| case constrained | ||
| } | ||
|
|
||
| struct Decision: Sendable, Equatable { | ||
| let delay: Duration | ||
| let reason: Reason | ||
| } | ||
|
|
||
| private static let recentInteractionThreshold: TimeInterval = 5 * 60 | ||
| private static let warmThreshold: TimeInterval = 60 * 60 | ||
| private static let idleThreshold: TimeInterval = 4 * 60 * 60 | ||
|
|
||
| /// Representative cadence for consumers that need a single interval but cannot reach live | ||
| /// signals (`ProviderRegistry` builds provider specs before a `UsageStore` exists). Matches | ||
| /// `warmDelay`: the steady-state cadence while the user is active, which is when | ||
| /// interval-derived heuristics such as the persistent-CLI-session idle window matter most. | ||
| static let nominalIntervalForHeuristics: TimeInterval = 5 * 60 | ||
|
|
||
| private static let recentInteractionDelay: Duration = .seconds(2 * 60) | ||
| private static let warmDelay: Duration = .seconds(5 * 60) | ||
| private static let idleDelay: Duration = .seconds(15 * 60) | ||
| private static let longIdleDelay: Duration = .seconds(30 * 60) | ||
| private static let constrainedDelay: Duration = .seconds(30 * 60) | ||
|
|
||
| func nextDelay(for input: Input) -> Decision { | ||
| if input.lowPowerModeEnabled || Self.isConstrained(input.thermalState) { | ||
| return Decision(delay: Self.constrainedDelay, reason: .constrained) | ||
| } | ||
|
|
||
| guard let lastMenuOpenAt = input.lastMenuOpenAt else { | ||
| return Decision(delay: Self.longIdleDelay, reason: .longIdle) | ||
| } | ||
|
|
||
| // A future or clock-adjusted timestamp yields a negative age, which reads as recent. | ||
| let age = input.now.timeIntervalSince(lastMenuOpenAt) | ||
|
|
||
| if age <= Self.recentInteractionThreshold { | ||
| return Decision(delay: Self.recentInteractionDelay, reason: .recentInteraction) | ||
| } | ||
| if age <= Self.warmThreshold { | ||
| return Decision(delay: Self.warmDelay, reason: .warm) | ||
| } | ||
| if age < Self.idleThreshold { | ||
| return Decision(delay: Self.idleDelay, reason: .idle) | ||
| } | ||
| return Decision(delay: Self.longIdleDelay, reason: .longIdle) | ||
| } | ||
|
|
||
| private static func isConstrained(_ state: ProcessInfo.ThermalState) -> Bool { | ||
| state == .serious || state == .critical | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.