Skip to content

fix: handle invalid and extreme image scaling bounds - #366

Merged
lzhgus merged 1 commit into
lzhgus:mainfrom
sivyrn:fix/image-scaling-bounds
Oct 20, 2026
Merged

lzhgus merged 1 commit into
lzhgus:mainfrom
sivyrn:fix/image-scaling-bounds

Conversation

@sivyrn

@sivyrn sivyrn commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Image scaling could attempt to create a zero-pixel bitmap for invalid bounds or extremely thin images. Reject nonpositive bounds, preserve the original image when no downscaling is needed, and clamp downscaled dimensions to at least one pixel.

This is the ImageUtilities-only split requested in #357. It targets main independently of the Quick Access changes.

Validation: all 279 SharedKit tests passed on Apple Silicon with Xcode 27, including invalid dimensions, no-upscaling identity, and extreme aspect-ratio cases. git diff --check passed.

@sivyrn
sivyrn marked this pull request as ready for review October 19, 2026 08:40

@lzhgus lzhgus left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks @sivyrn! Small and well-tested: rejecting invalid bounds, reusing images that don't need downscaling, and keeping at least one pixel on thin captures. SharedKit tests all pass locally. Merging. πŸ™

@lzhgus
lzhgus merged commit 8a04e62 into lzhgus:main Oct 20, 2026
2 checks passed
lzhgus pushed a commit that referenced this pull request Oct 20, 2026
## Problem and behavior

After automatic-language OCR hits a persistent
`TextRecognition.CRImageReaderError` and the existing fresh-process
helper succeeds, subsequent captures currently retry the same broken
in-process Vision path. Remember that confirmed recovery for the app
lifetime and route later automatic-language requests directly to the
helper.

Successful primary requests, unrelated errors, failed helpers, and
cancelled requests do not enable this routing. Explicit-language
requests still try the primary recognizer. Cancellation is checked
before routing and after helper completion, including when the helper
route is already remembered.

## Scope

Only `OCRProcessFallback.swift` and the new
`OCRFallbackRoutingTests.swift` differ from main. Startup/prewarming,
OCRKit, shortcut recording, the capture cursor, and Quick Access
animations retain main's behavior. No helper is launched at app startup
by this change.

The other independent fixes are in #365 (translation sessions), #366
(image scaling), and #367 (Quick Access).

## Reproduction and validation

The targeted failure is the process-local Vision failure described by
the existing helper implementation from #338: accurate
automatic-language OCR fails with `TextRecognition.CRImageReaderError`
in the current process while the same request succeeds in a fresh
helper. Repeated requests in that affected process are the case this
router avoids.

Current live probe: on macOS 27.2 (26B5112a), three consecutive
`VNRecognizeTextRequest` calls in one process, using the same generated
English/Chinese image, `.accurate`, automatic language detection, and
language correction, all completed without that engine error. The
historical OS fault was **not reproduced on this run**, and this PR
makes no startup or first-request performance claim.

The deterministic regression tests inject that specific error followed
by helper success and assert that three requests use the primary once
and the helper three times. Separate tests cover successful primary
calls, explicit languages, unrelated errors, helper failure,
cancellation during primary/helper work, and cancellation after the
helper route is remembered. Tests live in their own file, with
explicitly `@Sendable` closures to address the older-Xcode CI
diagnostic.

Local validation (Apple Silicon, Xcode 27, English/US):
- XcodeGen generation and full App test suite: 219 tests passed,
including all 8 routing tests and the real executable-helper integration
test.
- `git diff --check` passed.

Ready for review of the OS-specific routing policy. GitHub Actions and
GitGuardian passed on commit `28310cb`. The live reproduction limitation
described above remains unchanged.
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.

2 participants