fix: importable headless (no screenshot at import) + persist pixel_ratio on the recording model#24
Merged
Merged
Conversation
The recorder no longer takes a screenshot at module load (PR #23), but the package import guard only caught ImportError. Broaden it so a display/screenshot failure (mss ScreenShotError) during recorder import also degrades to `Recorder = None` instead of crashing the whole package on a headless host (CI, containers, sandboxes). Add a runtime headless-import test that imports the package fresh in a subprocess where mss.grab raises ScreenShotError, complementing the existing static (AST) guard. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CKrVJJy5jWVCkXAqgUqtqZ
CaptureSession.pixel_ratio read `pixel_ratio` off the SQLAlchemy Recording model, but that model had no such column (only the legacy raw-sqlite schema did), so a HiDPI capture whose config JSON lacked it silently scaled at 1.0, under-scaling downstream coordinate mapping. Add a nullable `pixel_ratio` column to the Recording model and write the captured display's ratio (platform.get_display_pixel_ratio()) at record time in create_recording. Add an additive ALTER TABLE ADD COLUMN migration (migrate_missing_columns) run when opening any per-capture DB, so older recording.db files that predate the column still load: the column is added as NULL, and CaptureSession.pixel_ratio falls back to the config JSON, then 1.0 when genuinely unknown. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CKrVJJy5jWVCkXAqgUqtqZ
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.
Fixes two defects surfaced by the downstream openadapt-flow capture adapter.
Fix 1 (HIGH): package must be importable on a headless host
import openadapt_capturemust never crash where there is no display (CI, containers, sandboxes). PR #23 already moved the module-level monitor-dimension screenshot into a function, but the package import guard aroundfrom openadapt_capture.recorder import Recorderonly caughtImportError.mss.exception.ScreenShotError) andOSError, so a headless import degrades toRecorder = Noneinstead of crashing the whole package.CaptureSessionfresh in a subprocess wheremss.grabraisesScreenShotErrorandtake_screenshotraises, asserting the import still succeeds. Complements the existing static (AST) guard.Fix 2 (MEDIUM): pixel_ratio wasn't persisted on the SQLAlchemy recording
CaptureSession.pixel_ratioreadpixel_ratiooff the SQLAlchemyRecordingmodel, but that model had no such column (only the legacy raw-sqlite schema did). A HiDPI capture whose config JSON lacked it silently scaled at 1.0, under-scaling downstream coordinate mapping.pixel_ratiocolumn to theRecordingmodel.platform.get_display_pixel_ratio()) into it at record time increate_recording.ALTER TABLE ... ADD COLUMNmigration (migrate_missing_columns) run when opening any per-capture DB (these ephemeral SQLite DBs have no migration framework). Olderrecording.dbfiles that predate the column still load: the column is added as NULL andCaptureSession.pixel_ratiofalls back to the config JSON, then 1.0 when genuinely unknown.Tests
tests/test_headless_import.py: import +CaptureSessionsucceed when the screenshot call raises (simulated headless).tests/test_highlevel.py::TestPixelRatio: pixel_ratio 2.0 round-trips through the SQLAlchemy model; an old recording without the column still loads and falls back (config JSON -> 1.5; no config -> 1.0).Full fast suite green (153 passed), ruff clean.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CKrVJJy5jWVCkXAqgUqtqZ