-
-
Notifications
You must be signed in to change notification settings - Fork 51
Extract coordinator state mechanisms #800
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
59 changes: 59 additions & 0 deletions
59
Cotabby/Support/Suggestion/PostExhaustionAcceptanceState.swift
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,59 @@ | ||
| /// Pure state machine for the short Tab-ownership window after a suggestion is exhausted. | ||
| /// | ||
| /// The coordinator owns one instance for its lifetime and supplies the side effects: installing the | ||
| /// input interception, scheduling the timeout, and accepting a regenerated continuation. Keeping | ||
| /// those effects outside this value makes the hard rules explicit: only one accept may be queued, | ||
| /// every arm invalidates older timeout callbacks, and consuming or clearing the window is atomic. | ||
| struct PostExhaustionAcceptanceState: Equatable { | ||
| private(set) var backstopGeneration: UInt64 = 0 | ||
| private(set) var isArmed = false | ||
| private(set) var hasQueuedAccept = false | ||
|
|
||
| /// Whether a release has any state to clear. Kept separate from `isArmed` so an inconsistent | ||
| /// queued flag still fails safe by returning Tab ownership to the host. | ||
| var needsRelease: Bool { | ||
| isArmed || hasQueuedAccept | ||
| } | ||
|
|
||
| /// Opens a fresh window and returns the identity its timeout callback must capture. | ||
| @discardableResult | ||
| mutating func arm() -> UInt64 { | ||
| isArmed = true | ||
| hasQueuedAccept = false | ||
| backstopGeneration &+= 1 | ||
| return backstopGeneration | ||
| } | ||
|
|
||
| /// Queues one rapid accept while the continuation is unavailable. | ||
| /// | ||
| /// Repeated presses deliberately collapse into one Boolean so a user mashing Tab cannot cause a | ||
| /// regenerated suggestion to accept multiple unseen chunks. | ||
| @discardableResult | ||
| mutating func queueAcceptIfArmed() -> Bool { | ||
| guard isArmed else { | ||
| return false | ||
| } | ||
|
|
||
| hasQueuedAccept = true | ||
| return true | ||
| } | ||
|
|
||
| /// Returns true only for the timeout belonging to the current arm operation. | ||
| func ownsBackstop(generation: UInt64) -> Bool { | ||
| backstopGeneration == generation | ||
| } | ||
|
|
||
| /// Closes the window and invalidates every timeout callback already in flight. | ||
| mutating func clear() { | ||
| isArmed = false | ||
| hasQueuedAccept = false | ||
| backstopGeneration &+= 1 | ||
| } | ||
|
|
||
| /// Atomically closes the window and reports whether the regenerated continuation owes one accept. | ||
| mutating func consumeQueuedAccept() -> Bool { | ||
| let shouldAccept = isArmed && hasQueuedAccept | ||
| clear() | ||
| return shouldAccept | ||
| } | ||
| } |
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,69 @@ | ||
| /// Main-actor bookkeeping for one streamed suggestion delivery. | ||
| /// | ||
| /// `SuggestionCoordinator` owns this value for its lifetime. The value keeps token-rate engine | ||
| /// callbacks from leaking three related invariants into the coordinator: pending partials are | ||
| /// latest-wins, at most one drain is scheduled per runloop turn, and visible text may only grow | ||
| /// monotonically. It does not schedule work or render UI; the coordinator remains responsible for | ||
| /// those side effects. | ||
| struct SuggestionStreamingState { | ||
| /// One partial paired with the replaceable-work identity that produced it. | ||
| struct PendingPartial { | ||
| let result: SuggestionResult | ||
| let workID: UInt64 | ||
| } | ||
|
|
||
| private(set) var pendingPartial: PendingPartial? | ||
| private(set) var isDrainScheduled = false | ||
| private(set) var renderedText: String? | ||
|
|
||
| /// Starts a new stream without clearing an already-enqueued drain callback. | ||
| /// | ||
| /// A scheduled callback cannot be cancelled. Preserving `isDrainScheduled` lets the existing | ||
| /// callback drain a replacement partial from the new generation; clearing it here could enqueue | ||
| /// two drains for the same runloop turn. | ||
| mutating func beginGeneration() { | ||
| renderedText = nil | ||
| pendingPartial = nil | ||
| } | ||
|
|
||
| /// Stores the newest partial and returns whether the coordinator must schedule a drain. | ||
| @discardableResult | ||
| mutating func enqueue(_ result: SuggestionResult, workID: UInt64) -> Bool { | ||
| pendingPartial = PendingPartial(result: result, workID: workID) | ||
| guard !isDrainScheduled else { | ||
| return false | ||
| } | ||
|
|
||
| isDrainScheduled = true | ||
| return true | ||
| } | ||
|
|
||
| /// Ends the scheduled-drain window and returns the newest partial, if one survived teardown. | ||
| mutating func drain() -> PendingPartial? { | ||
| isDrainScheduled = false | ||
| defer { pendingPartial = nil } | ||
| return pendingPartial | ||
| } | ||
|
|
||
| /// Applies the backend-independent monotonic rendering rule to the current stream. | ||
| func canRender(_ candidate: String) -> Bool { | ||
| StreamedGhostTextPolicy.isRenderableExtension( | ||
| candidate: candidate, | ||
| currentlyRendered: renderedText | ||
| ) | ||
| } | ||
|
|
||
| /// Records text only after all coordinator freshness and seam guards have accepted it. | ||
| mutating func recordRendered(_ text: String) { | ||
| renderedText = text | ||
| } | ||
|
|
||
| /// Drops state associated with a torn-down suggestion session. | ||
| /// | ||
| /// As with `beginGeneration`, a scheduled callback remains responsible for clearing the drain | ||
| /// flag when it eventually runs. | ||
| mutating func clearSession() { | ||
| renderedText = nil | ||
| pendingPartial = nil | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MainActorannotationThe doc comment opens with "Main-actor bookkeeping" but the struct itself carries no
@MainActorisolation. The coordinator's own main-actor isolation provides the protection in practice, but an explicit annotation would make this a compile-time guarantee rather than an implicit contract — and would prevent a future caller from accidentally constructing and mutating aSuggestionStreamingStateoff the main actor.PostExhaustionAcceptanceStatehas only primitive fields so the omission there is less material, butSuggestionStreamingStatestores aSuggestionResultvalue.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!