Skip to content

fix(desktop): resolve Rewind OCR languages from the user locale - #12888

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
woahwhattheheck:fix/rewind-ocr-recognition-languages
Sep 7, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
woahwhattheheck:fix/rewind-ocr-recognition-languages

Conversation

@woahwhattheheck

@woahwhattheheck woahwhattheheck commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #12797

What

On macOS, Rewind's screen OCR recognises no Japanese text at all. Latin characters on the same frame come back fine, so this is not degraded recognition — the script is out of scope, and Rewind's search is silently non-functional for anyone working in a non-Latin script.

Why it happens

RewindOCRService pins the recognition language list to a compile-time constant:

private static let recognitionLanguages: [String] = ["en-US"]

VNRecognizeTextRequest treats that list as the entire recognition scope. A language absent from it is not recognised poorly; it is not recognised at all. There is no setting that changes this.

That constant is not arbitrary, though — the comment above it records why it must stay a process-lifetime constant: Vision's TextRecognition enumerates the bridged NSArray asynchronously, and an array literal whose storage is tied to the call frame can be freed mid-enumeration, tripping _assertionFailure (#5891, #5151). Any fix has to widen the scope without reintroducing a per-call array.

The change

recognitionLanguages is now resolved once, from the user's own locale, into the same pinned static let:

private static let recognitionLanguages: [String] = resolveRecognitionLanguages(
  preferred: Locale.preferredLanguages,
  supported: visionSupportedRecognitionLanguages()
)
  • Preferred languages lead. Vision treats the list as a priority order.
  • Unsupported tags are dropped, not passed through. perform throws on an unsupported language, which would fail the whole request — turning a partial-recognition bug into total OCR loss.
  • A region-less preference still matches. ja selects Vision's ja-JP.
  • English is always retained, even when it is not preferred. Screen text is routinely mixed (identifiers, URLs, product names), and dropping English would regress the Latin half of exactly the mixed lines this fixes.
  • The capability query degrades to English rather than propagating, so an unavailable list never leaves the request with an empty language scope.

The crash-safety property is preserved: this still resolves to exactly one array held for the process lifetime.

Scope is deliberately limited to the recognition language list. I have not touched capture cadence, recognition level, or usesLanguageCorrection.

Verification

RewindOCRQualityTests gains five cases against the pure resolver, so the ordering and filtering rules are testable without invoking Vision or changing the host's locale:

case asserts
testRecognitionLanguagesFollowThePreferredLocaleInsteadOfAPinnedOne ja-JP preferred → ["ja-JP", "en-US"] — fails on main, which yields ["en-US"]
testRegionlessPreferredLanguageSelectsASupportedRegionalVariant ja → ja-JP
testUnsupportedPreferredLanguagesAreDroppedRatherThanPassedToVision xx-YY never reaches Vision
testEnglishIsNotDuplicatedWhenItIsAlreadyPreferred no duplicate entry
testAnUnavailableSupportedListStillYieldsAUsableLanguage empty capability list → ["en-US"]

The first case is the regression test: on main the resolver does not exist and the constant is ["en-US"], so a Japanese-preferring host cannot produce ja-JP.

I do not have a macOS host or an Xcode toolchain in this environment, so I have not run the Swift suite myself and am not claiming a device-level repro of the Japanese sample. The change was derived from the source and the issue's measurements. Please let the Desktop Swift checks run; I will fix anything they surface.

Failure-Class: new

FC-recognition-scope-pinned-below-user-locale, added in .github/failure-classes/ in this change.

Root cause. The constant was introduced to solve a lifetime problem — keep one array alive for the process — and the language policy was silently absorbed into that fix as a literal. Once a scope value lives inside a constant justified on other grounds, nothing points at it when the user's locale differs from it. The failure is quiet by construction: OCR returns text, the row is written, search runs, and every layer above reports success over content it never saw.

Durable guard. The canonical prevention artifact is RewindOCRQualityTests.swift, which asserts the resolver's behaviour rather than a fixed language list. A future change to the language set stays correct as long as it derives from the user's locale, and any reintroduction of a pinned scope fails the first case.

The distinction from the two neighbouring locale classes: FC-repository-text-io-inherits-host-locale and FC-subprocess-output-inherits-host-locale are both about inheriting an ambient locale where an explicit one was required. This is the inverse — an explicit constant pinned below the user's locale.

Invariants

None named. No locked invariant globs Sources/Rewind/Core/RewindOCRService.swift; the Rewind paths that are covered are RewindDatabase/RewindIndexer/RewindStorage (INV-AUTH), TaskChatMessageStorage (INV-CHAT-1), MemoryStorage/MemoryModels (memory tiers) and ActionItemStorage (INV-TASK-1).

Review in cubic

RewindOCRService pinned recognitionLanguages to ["en-US"], and Vision
treats that list as the whole recognition scope, so non-Latin screen text
was not recognised at all and Rewind search was silently non-functional
for those users.

Resolve the list once from Locale.preferredLanguages, filtered to what
Vision reports as supported and always retaining an English fallback for
mixed screen text. It still resolves into a single process-lifetime
static let, so the bridged-NSArray lifetime fix from BasedHardware#5891/BasedHardware#5151 is
preserved.

Fixes BasedHardware#12797

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Two review findings on the locale resolver:

- When the capability list was non-empty but shared nothing with the user,
  the resolver returned a literal en-US that Vision may not support, and an
  unsupported tag makes perform throw -- the total OCR loss the filtering was
  meant to avoid. That branch now picks from Vision's own reported list;
  en-US stays reserved for the empty-capability case, where there is no list.

- Matching collapsed a tag to its primary language, so zh-TW could be handed
  the zh-Hans scope and read Traditional text with the Simplified model.
  Matching now compares language and script from the maximised identifier,
  which keeps zh-TW on zh-Hant while still letting ja reach ja-JP.
@Git-on-my-level

Copy link
Copy Markdown
Member

Verified against the diff and the surrounding code — this is a clean, well-scoped fix for #12797. Notes per file:

desktop/macos/Desktop/Sources/Rewind/Core/RewindOCRService.swift

  • recognitionLanguages is now resolved once from Locale.preferredLanguages ∩ VNRecognizeTextRequest.supportedRecognitionLanguages(...) and still lands in a static let, so the process-lifetime pin that guards the macOS Screen Capture crash on enable - VNRecognizeTextRequest array bounds failure #5891/Crash in RewindOCRService.extractTextWithBounds — array index out of bounds #5151 bridged-NSArray race is preserved — the consumption site (request.recognitionLanguages = Self.recognitionLanguages) is unchanged in kind.
  • visionSupportedRecognitionLanguages() uses try? with an en-US fallback, so a failed capability query can't take OCR down entirely.
  • bestSupportedMatch compares maximized identifiers (Locale.Language.maximalIdentifier), which keeps zh-TW on zh-Hant instead of collapsing to zh-Hans, while ja still reaches ja-JP; that API needs macOS 13, comfortably below the .macOS("14.0") floor in Desktop/Package.swift.
  • For an en-US-only user the resolution yields exactly ["en-US"] — behavior unchanged for the existing population.
  • resolveRecognitionLanguages only ever returns tags Vision reported (resolved.isEmpty ? [supported[0]] : resolved), avoiding the unsupported-tag perform throw the description calls out.

desktop/macos/Desktop/Tests/RewindOCRQualityTests.swift

  • The eight new tests are pure and injectable — ordering, region-less matching, unsupported-tag dropping, dedup, no-overlap fallback, and the zh-Hans/zh-Hant split are all covered without invoking Vision or mutating the host locale. Nicely designed.

.github/failure-classes/FC-recognition-scope-pinned-below-user-locale.json

  • Matches the existing failure-class registry's schema and the documented fix-PR convention, with canonical_prevention_artifact pointing at the tests added here.

One follow-up worth tracking separately (not a reason to expand this PR): #12797 also lists three sibling pinned constants still on main — desktop/context-for-claude/Sources/ContextApp/Capture/ScreenWatcher.swift:1064, desktop/context-for-claude/Sources/ContextApp/Onboarding/SettingsRowSighting.swift:387, and desktop/macos/Desktop/Sources/CloudConnectorFormAutomation.swift:1481. Fixing the Rewind indexing path first is the right scoped unit; the others would make a natural follow-up issue/PR.

No CI runs against this target, so the one thing static review can't cover is runtime behavior on a non-Latin-locale machine — the issue's repro document would be a quick pre-merge spot-check for whoever merges.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution macOS labels Sep 6, 2026
@woahwhattheheck

woahwhattheheck commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the careful read — and agreed on keeping the three sibling constants out of this PR. ScreenWatcher.swift:1064, SettingsRowSighting.swift:387 and CloudConnectorFormAutomation.swift:1481 are the same class of pin, but they sit on different capture paths, so folding them in here would mix a Rewind-indexing fix with changes to onboarding and form automation in one diff. Happy to open the follow-up issue for them if that is useful, or to leave it to whoever picks up the rest of #12797.

On the runtime gap you flagged: that is the right limitation to name. I do not have a macOS host or an Xcode toolchain in this environment, so I have not run the Swift suite or reproduced the Japanese sample on a device, and I said so in the description rather than implying otherwise. The eight tests are deliberately pure so the ordering, script-matching and fallback rules can be checked without Vision — but they cannot tell you what a non-Latin-locale machine actually recognises. The issue's repro document is the right pre-merge spot-check.

One thing worth having explicitly, since it is the part a reviewer would most reasonably worry about: the change cannot regress an existing en-US user. Locale.preferredLanguages leads the list, the English fallback is appended only if not already present, and with English preferred the resolution is exactly ["en-US"] — the same single-element array the constant held before.


Post-merge validation addendum — 2026-09-14 (MICA-IO)

Additional reduced-target validation was run against the exact merged PR head 131a93e21fb15aa6605548631863be0802049b9e; no source was changed in this pass. Swift 6.2.1 on x86_64 Linux, Swift 6 mode, warnings-as-errors: 33 named fixtures passed (including equivalent inputs for the eight PR cases), plus 1,372 generated input combinations exercising five invariants each for 6,893 assertions with zero failures. Three constructed functional regressions compiled and then failed assertions as expected.

Preserved replay package identity: MICA-IO_omi_12888_validation.zip, 16,895 bytes / 15 members / SHA-256 82c2bf93d8560f0a687ab207f5c159d92f5d51db3590d15c9801a1ff2622c48a.

The original runtime caveat still stands: this executed a connector-transcribed excerpt of the three pure language-selection methods, not a full-file/blob-verified application checkout. Upstream XCTest, native macOS OCR/Vision execution, the static capability query, and array-lifetime behavior were not exercised here. The native non-Latin-locale OCR spot-check therefore remains outstanding. This is supplementary evidence, not device-level validation or a new bounty/payment claim.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Desktop bug fix (owner-override): root cause is clear (RewindOCRService pinned recognitionLanguages to en-US only, so non-Latin scripts like Japanese get zero OCR recognition), scoped diff, 5 new regression tests against the pure resolver, confirmed still broken on main. No abuse signals.

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

Labels

macOS positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewind screen OCR is hardcoded to en-US, making non-Latin screen text silently unsearchable

3 participants