Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Accept only the ghost text the user has been shown - #919

Draft
FuJacob wants to merge 1 commit into
mainfrom
security/accept-only-shown-text
Draft

FuJacob wants to merge 1 commit into
mainfrom
security/accept-only-shown-text

Conversation

@FuJacob

@FuJacob FuJacob commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Tab could insert text the user had never seen in two ways. On a host that offers one row (single-line fields, a caret with text below it, no measured line pitch), the inline ghost paints only the head that fits and stops at a newline, but full acceptance (and phrase acceptance) inserted the whole buffered tail. A Tab pressed during post-exhaustion regeneration was queued and replayed as an accept of the next continuation in the same run-loop turn that continuation was first presented, or while the overlay was still holding it off screen for a caret read. Tab now inserts only text that has been painted.

  • SuggestionOverlayControlling.truncatedDisplayText (default nil): OverlayController reports the painted head when the presented inline layout is truncated (GhostTextLayout.isTruncated / remainingText). It reads lifecycle.inlineSession, which exists only while an inline ghost is presented, so card mode and a hidden overlay report nothing. The coordinator reads this one value and never sees view code.
  • Pure helpers in SuggestionSessionReconciler: shownHead(of:overlayState:truncatedDisplayText:) re-bases the painted head onto the live tail (typed-through letters, an accept whose next present is still held, a held present that extends the painted one), and acceptanceChunk(_:limitedTo:) cuts a word, phrase or full-accept chunk back to it.
  • SuggestionInteractionState.prepareAcceptance / prepareFullAcceptance apply the cap through one shared step (limitingToShownHead) after the shared validation in prepare. The rest of the suggestion stays in the session and is shown by the existing partial-accept / advance path, so the next press takes it. I chose cap-and-continue over refusing because refusing would hand the key to the host and tear down the suggestion.
  • New SuggestionAcceptancePreparation.unshown: when none of the live tail has been painted yet (e.g. a capped accept took the whole head and the next present is held), the coordinator owns the key, inserts nothing, and keeps the session. It does not pass the key through, which would leak a focus-moving Tab (the case Keep rapid Tab accepts owned while the overlay holds its next present #838 fixed).
  • Queued Tab: PostExhaustionAcceptanceState now only swallows a press during the window (holdPressIfArmed, consumeHeldPress). flushQueuedPostExhaustionAcceptIfNeeded is replaced by endPostExhaustionWindowForFreshSuggestion (still called at the end of presentFreshResult), which ends the window and logs a dropped press but never accepts. I kept swallowing as that press's fate rather than sending it to the host: Tab reaching the host moves focus out of the field, which is the bug Fix rapid Tab leaking into the host after a final-word accept #484 fixed. Delivering it late would be worse.
  • Tests: pure tests for shownHead / acceptanceChunk, a GhostTextLayout contract test (a truncated head is a strict prefix of the unconsumed text, including after an advance), interaction-state tests for full, phrase and unshown preparation, and coordinator tests for capped full accept, the held-present unshown press, and a swallowed Tab that does not accept the continuation that follows it. The rig overlay gains a settable truncatedDisplayText.

Validation

Not built or run locally (no disk space on the authoring machine); CI build and tests are the compile check.

swiftlint lint --quiet <touched production files>
# no output, exit 0
swiftlint lint --quiet <touched test files>
# only warnings that already exist on main (large_tuple, type_name, one line_length in test support)

No files were added or removed and project.yml is unchanged. xcodegen generate produces no diff.

Rebuilt from scratch on main at c14b1700, after the #928–#955 refactors, instead of rebased: 15 of 18 files had moved. The behavior decisions are unchanged. The adaptations:

The first CI run failed one existing test, SuggestionCoordinatorWordCompletionTests.testAcceptedCorrectionWaitsForPublishedTextAndQueuesNextTab. It asserted that a Tab pressed while an accepted typo correction is still publishing inserts the next word ("the"). At that press there is no session, the correction accept has hidden the overlay, and "the package" exists only as a prefetched continuation. The test asserts the missing session itself. That text is first presented inside apply when the publish lands, and the old flush accepted it in that same turn, which is exactly the case this PR removes. The post-exhaustion window only arms with no session and a hidden overlay, so no held press can ever refer to text already on screen. I renamed the test to …AndSwallowsTheRapidTab and kept its lookahead and request assertions. After the publish it now asserts that nothing was inserted, the continuation was shown, and the window closed. A fresh press then inserts "the" and leaves " package", the same end state the old assertions checked.

Linked issues

None filed. Addresses M10 ("truncated ghost, full insert" and "queued Tab") from a static security review of 8cdbea2.

Risk / rollout notes

This changes the core accept flow. What users will notice:

  • Full accept on a truncated ghost takes two presses (or more). In single-line fields, fields with text below the caret, and hosts with no measured line pitch, the full-accept key (and a double-tap full accept) now inserts only the words on screen. The rest then appears as the next ghost and the next press takes it. A suggestion with a newline inserts only up to the newline. Phrase acceptance is capped the same way. Word acceptance is effectively unchanged, because the painted head always ends on a word boundary.
  • Rapid Tab across a suggestion boundary needs one more press. Tab, Tab quickly after the last word (or after accepting a typo correction): the second press is still swallowed (focus stays in the field), but it no longer accepts the first word of the continuation. The user sees the new ghost and presses Tab again. Previously, mashing Tab kept inserting across the boundary without a pause.
  • A Tab can now be swallowed with no visible effect in two narrow cases where none of the remaining tail is painted: during a held present right after a capped accept, and when the user has typed through the whole painted head before the ghost slides. The session survives and the next present shows the text. These cases used to insert the unseen next word.
  • Not covered: the card (mirror) mode. It collapses whitespace (including newlines) and lets SwiftUI place a trailing ellipsis at 520pt, so its painted extent is not known exactly and it is left uncapped. It does at least show the ellipsis. Inline layouts that need more than GhostTextLayout.maximumRows already fall back to the card. Worth a follow-up.
  • Not covered: a Tab pressed just after a fresh ghost appears but faster than a person can read it. Only presses that provably predate the presentation are affected.
  • Untested against a live app: I have not run this on real hosts. The cap depends on OverlayController's inline session and published state staying in step. I traced every place they change (present, advance, calibration re-layout, font-registration redraw, card fallback, hide), but CI and a manual check in a single-line web field and an Obsidian note with text below the caret are the real verification.
  • No settings, schema, or project changes. PostExhaustionAcceptanceState members are renamed (hasQueuedAccept → hasHeldPress, queueAcceptIfArmed → holdPressIfArmed, consumeQueuedAccept → consumeHeldPress, window case armedWithQueuedAccept → armedWithHeldPress) so the names match the new semantics.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@FuJacob
FuJacob force-pushed the security/accept-only-shown-text branch from 080f317 to 5418970 Compare November 23, 2026 04:12

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant