Apply Chromium AX-publish delay to no-session keystroke path#378
Merged
Conversation
PR #376 routed the with-session textMutation / shortcutMutation reschedules through schedulePredictionAfterHostPublishDelay so Chromium-based editors (Chrome, Edge, Slack, Discord, Notion, every Electron app) have time to publish the new contenteditable text to AX before generation reads it. The no-session path in handleInputEvent still called focusModel.refreshNow() + schedulePrediction() synchronously from inside the CGEvent tap. In practice this is reached two ways: - Fresh keystroke into a field that does not yet have an active suggestion (after dismissal, focus arrival, or the Tab that exhausted the last suggestion). - Rapid follow-up typing — the previous keystroke just ran the with-session branch and called invalidateActiveSuggestion, which clears the session. The next keystroke arrives within the 150ms publish-delay window and finds activeSession == nil, falling through to this no-session path. Both produce the same "Cotabby swallowed my key" symptom: the new suggestion appears, but it reflects the pre-keystroke text because the synchronous AX read in the CGEvent tap fired before the host app had a chance to write the new character. Sharing the same delay closes the gap. The immediate refreshNow() is dropped; schedulePredictionAfterHostPublishDelay calls focusModel.refreshNow() inside its main-queue trampoline, so AX freshness is preserved at the point that actually matters (right before schedulePrediction materializes the request).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
PR #376 routed the with-session textMutation / shortcutMutation reschedules through
schedulePredictionAfterHostPublishDelayso Chromium-based editors (Chrome, Edge, Slack, Discord, Notion, every Electron app) have time to publish the new contenteditable text to AX before generation reads it. The no-session path inhandleInputEventstill calledfocusModel.refreshNow()+schedulePrediction()synchronously from inside the CGEvent tap, which reproduces the same "Cotabby swallowed my key" symptom on the first keystroke into a field and on rapid follow-up typing afterinvalidateActiveSuggestionclears the session.Validation
xcodebuild build -project Cotabby.xcodeproj -scheme Cotabby -destination 'platform=macOS' CODE_SIGNING_ALLOWED=NO→
** BUILD SUCCEEDED **swiftlint lint --quiet→ no violations.Manual: typing rapidly into a Chrome contenteditable while the previous suggestion is mid-flight; new suggestion now reflects the most recently typed character instead of the pre-keystroke text.
Linked issues
Refs #376.
Risk / rollout notes
focusModel.refreshNow()is dropped from this branch;schedulePredictionAfterHostPublishDelaycallsfocusModel.refreshNow()inside its main-queue trampoline, so AX freshness is preserved at the point that actually matters (right beforeschedulePredictionmaterializes the request).Greptile Summary
This PR extends the Chromium AX-publish delay fix from PR #376 to cover the no-session keystroke path in
handleInputEvent. The synchronousfocusModel.refreshNow()+schedulePrediction()pair that ran directly from inside the CGEvent tap is replaced withschedulePredictionAfterHostPublishDelay(), which defers both calls 150ms onto the main queue so Chromium-based editors have time to publish updated contenteditable text to AX before generation reads it.schedulePredictionAfterHostPublishDelay()helper already wired into the with-sessiontextMutationandshortcutMutationpaths.schedulePrediction()retains its ownSuggestionAvailabilityEvaluator.disabledReasonguard, andgenerateFromCurrentFocuscallsfocusModel.refreshNow()again after the debounce, so the 150ms window does not introduce a stale-context hazard.Confidence Score: 5/5
Safe to merge. The one-line change is a minimal, focused extension of an already-validated pattern.
The diff is a single call-site replacement that mirrors the with-session fix from PR #376 exactly. schedulePrediction() has its own availability guard, and generateFromCurrentFocus calls focusModel.refreshNow() again after the debounce, so neither the 150ms window nor the removed eager refresh can leave the coordinator in a bad state.
No files require special attention.
Important Files Changed
Sequence Diagram
sequenceDiagram participant CGEventTap participant handleInputEvent participant MainQueue participant focusModel participant schedulePrediction participant generateFromCurrentFocus Note over CGEventTap,handleInputEvent: Before this PR (no-session path) CGEventTap->>handleInputEvent: "event (shouldSchedulePrediction=true)" handleInputEvent->>focusModel: refreshNow() [pre-keystroke AX state!] handleInputEvent->>schedulePrediction: schedulePrediction() schedulePrediction->>generateFromCurrentFocus: "debounce -> generate (stale text)" Note over CGEventTap,generateFromCurrentFocus: After this PR (no-session path) CGEventTap->>handleInputEvent: "event (shouldSchedulePrediction=true)" handleInputEvent->>MainQueue: asyncAfter(+150ms) Note over MainQueue: host app processes keystroke, Chromium AX tree updates MainQueue->>focusModel: refreshNow() [post-keystroke AX state] MainQueue->>schedulePrediction: schedulePrediction() schedulePrediction->>generateFromCurrentFocus: "debounce -> refreshNow() again -> generate (correct text)"Reviews (1): Last reviewed commit: "Apply Chromium AX-publish delay to no-se..." | Re-trigger Greptile