Skip to content

fix: prevent cross-context bleed and repetition loops in inline suggestions#33

Open
shreeraman96 wants to merge 1 commit into
johnbean393:mainfrom
shreeraman96:fix/context-bleed-and-repetition-guards
Open

fix: prevent cross-context bleed and repetition loops in inline suggestions#33
shreeraman96 wants to merge 1 commit into
johnbean393:mainfrom
shreeraman96:fix/context-bleed-and-repetition-guards

Conversation

@shreeraman96
Copy link
Copy Markdown

Fixes #32.

Inline suggestions could surface text the user never typed in the focused field (leaking across windows, apps, and browser tabs) and could loop on already-written phrases. The causes were spread across the OCR window picker, the output filter, writing-history selection, and the reuse cache. This adds layered, independently-testable guards plus the missing scoping.

Changes

Cross-window OCRScreenWindowSelector, WindowOCRCaptureEngine, ScreenContextController

  • Select the window under the caret, not just the largest. The caret point is converted AppKit→CG (new DisplayCoordinateConverter.coreGraphicsPoint) so it matches SCWindow.frame's coordinate space.
  • Tie-break by z-order (frontmost) so an overlapping background window can't win.
  • Clear the cached OCR text on window/tab change so a stale reading from the previous window can't be injected before the fresh capture lands.

Repetition loopsPrefixRepetitionGuard (new)

  • Suppress a completion reproducing a recent phrase from beforeCursor, catching both whole-completion repeats and leading repeats that then diverge.

Cross-app / cross-tab writing historyCompletionController, WritingHistory, PersistentWritingHistoryStore

  • Scope previousUserInputs to the same app and, for web fields, the same domain, so another app/tab's content can't be injected and parroted. Empty domains are normalized to nil.

Clipboard / OCR echoContextEchoGuard (new)

  • Suppress a completion that verbatim-reproduces injected side context (clipboard / on-screen OCR) the user did not type. Writing history is intentionally excluded — it is scoped, and echoing the user's own recurring phrases is the point of that feature.

Reuse cacheCompletionController

  • Re-check the context-dependent guards (prefix-repetition, suffix-overlap, echo) against the live context before re-showing a cached completion, so reuse can't bypass them as beforeCursor/clipboard change.

Refactor

  • Extract AlphanumericNormalizer + RepeatedSpanDetector, shared by the overlap guards (removes a duplicated normalizer).

Tests

New/extended unit tests across all of the above: caret→window selection incl. frontmost/overlap and AppKit↔CG round-trip on multi-display; prefix-repetition (whole + leading, boundary cases); context-echo; same-app + same-domain history scoping (in-memory and DB-level); heal-aware filtering; and reuse-path re-checks.

All Swift package suites pass (AutocompleteCore, MacContextCapture, ConstrainedGeneration, Personalization, Prompting) and the app target builds. Rebased on latest main (incl. the re-optimized mid-line completions work).

Notes

  • Touches opt-in features (Screen-context OCR, clipboard context, writing history).
  • The fixes are intentionally layered (selection scoping + output guards + cache re-check) so a miss in one layer is caught by another.

🤖 Generated with Claude Code

Inline suggestions could surface text the user never typed in the focused
field, and could loop on already-written phrases. Root causes spanned the
OCR window picker, the output filter, writing-history selection, and the
reuse cache. This adds layered guards plus the missing scoping.

Cross-window OCR (ScreenWindowSelector / WindowOCRCaptureEngine /
  ScreenContextController):
- Pick the window under the caret, not just the largest. The caret point is
  converted AppKit->CG (new DisplayCoordinateConverter.coreGraphicsPoint)
  so it matches SCWindow.frame's coordinate space.
- Break ties by z-order (frontmost) so an overlapping background window
  can't win.
- Clear the cached OCR text on window/tab change so a stale reading from the
  previous window can't be injected before the fresh capture lands.

Repetition loops (PrefixRepetitionGuard, new):
- Suppress a completion that reproduces a recent phrase from beforeCursor,
  catching both whole-completion repeats and leading repeats that then
  diverge.

Cross-app / cross-tab writing history (CompletionController, WritingHistory,
  PersistentWritingHistoryStore):
- Scope previousUserInputs to the same app and, for web fields, the same
  domain, so content from another app/tab can't be injected and parroted.

Clipboard / OCR echo (ContextEchoGuard, new):
- Suppress a completion that verbatim-reproduces injected side context
  (clipboard / on-screen OCR) the user did not type. History is excluded
  (it is scoped, and echoing the user's own recurring phrases is intended).

Reuse cache (CompletionController):
- Re-check the context-dependent guards (prefix-repetition, suffix-overlap,
  echo) against the live context before re-showing a cached completion, so
  reuse can't bypass them as beforeCursor/clipboard change.

Also: extract AlphanumericNormalizer + RepeatedSpanDetector shared by the
overlap guards. Adds unit tests across all of the above.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

Inline suggestions leak text across windows/apps/tabs and loop on repeated phrases

1 participant