refactor(n2): dedupe data-URL header validation in n2_payload.py - #376
Merged
Conversation
_decode_data_url and _data_url_media_type each hand-rolled the identical
"data:...,...;base64..." validation and the same header[5:].split(";", 1)[0]
media-type extraction -- the second copy was added right alongside the first
in #374 (prune-to-budget) without noticing the first already existed a few
lines up. _decode_data_url now calls _data_url_media_type for validation and
media-type extraction, then does only its own base64 decode, so there is one
definition of what a valid n2 screenshot data URL looks like.
No public behavior change: both functions are module-private (not exported
from yutori.navigator), same exceptions raised under the same conditions,
same return values. Full non-slow suite (914 passed, 3 skipped) and the n2
payload/harness/compaction suites pass unmodified; ruff check/format clean.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DvN8Ceba13sWCcsPbFmBUP
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.
Problem
yutori/navigator/n2_payload.pyhas two private helpers that each hand-roll the exact same base64 data-URL validation:_data_url_media_typewas added right alongside_decode_data_urlin #374 (the just-merged "send the whole screenshot history" fix) as a deliberate perf optimization — reading the media type off the header without base64-decoding the payload, since a request now walks every frame each step. But it duplicated_decode_data_url's validation and header-parsing wholesale instead of factoring it out.Change
_decode_data_urlnow calls_data_url_media_typefor validation + media-type extraction, then does only its own base64 decode:One definition of what a valid n2 screenshot data URL looks like, instead of two that have to be kept in sync by hand.
Why it's safe
_-prefixed, not exported fromyutori.navigatoror documented inapi.md) — no public API surface touches this.ValueError, same messages) raised under the same conditions, same return values in both success and failure cases — I traced every branch by hand._data_url_media_type's whole reason to exist (avoid decoding the payload) is preserved:_decode_data_urlstill does exactly one base64 decode, no more.914 passed, 3 skipped. The n2-specific suites (test_navigator_n2.py,test_navigator_n2_compaction.py,test_navigator_n2_harness.py, 123 tests) also pass directly.ruff check/ruff format --checkclean on the touched file.One file, 13/13 lines changed, no behavior change.
Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01DvN8Ceba13sWCcsPbFmBUP
🤖 Generated with Claude Code
https://claude.ai/code/session_01DvN8Ceba13sWCcsPbFmBUP
Generated by Claude Code
Note
Low Risk
Internal refactor in a single private helper module with no public API or logic change beyond deduplication.
Overview
Refactors n2 screenshot data-URL handling so validation and media-type parsing live in one place instead of two copies that had to stay in sync.
_decode_data_urlnow delegates to_data_url_media_typefor the shared checks and header parsing, then only base64-decodes the payload._data_url_media_typeis defined first and still avoids decoding when callers (e.g.prepare_n2_image_data_url) only need the format for a pass-through check.No intended behavior change: same
ValueErrormessages, same return shapes, and the decode-once path for full reads is unchanged.Reviewed by Cursor Bugbot for commit baceed4. Bugbot is set up for automated code reviews on this repo. Configure here.