Skip to content

fix(mobile): truncate oversize markdown reads instead of failing them - #23676

Merged
Jinwoo-H merged 6 commits into
mainfrom
mobile-markdown-read-truncate
Sep 28, 2026
Merged

Jinwoo-H merged 6 commits into
mainfrom
mobile-markdown-read-truncate

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​208 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​202
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​98 $\color{#cf222e}{\Huge{\mathbf{−}}}$​42 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​56

What

A markdown tab over 512 KiB showed "Couldn't load markdown" on the phone (native and OTA alike) while the desktop displayed the same file fine.

Cause. The desktop renderer's mobile markdown bridge kept a private 512 KiB cap and threw file_too_large above it. That code is not on the RPC passthrough list, so the phone received a generic runtime_error; the markdown disk fallback only reacts to renderer_unavailable, and the reader's catch discarded the error and printed the generic message. The cap arrived with the first mobile markdown editor and had no comment or test.

Fix. Refusing is replaced by truncating. One shared budget, MOBILE_MARKDOWN_READ_MAX_BYTES = 2 MiB (sized like the terminal snapshot budget; the relay splice frame cap is 8 MiB), lives in src/shared/mobile-markdown-document.ts next to the existing 256 KiB edit cap. Above it the bridge returns a UTF-8-boundary prefix (existing clampUtf8TextPrefix) with truncated: true and the full byteLength; the document is read-only through the existing file_too_large reason, and version hashes the prefix. The private constant and the throw are gone.

On the phone the reply schema accepts the two optional fields, the reader status line says "Preview truncated. File size: X." (same wording and formatter as file tabs; before this any read-only reason showed only "Read only"), and markdown and file tabs share one refusal mapper so binary_file / file_too_large get real copy and only unknown errors keep the generic message.

Compatibility. No new params; the result gains two optional fields sent only when truncated. An older desktop still refuses, and the new phone now shows "File too large for mobile preview" instead of the generic message. An older phone ignores the fields. Host-local; nothing crosses the SSH boundary. The file preview path keeps its own 512 KiB truncation (syntax-highlighted on the phone, different cost).

Tests (red first)

  • Bridge: a 632 KB read now succeeds (expected { ok: false } to match { ok: true } before); a read between the edit and read budgets returns whole and read-only with no truncated field; a read over budget is cut on a UTF-8 boundary, editable: false, readOnlyReason: 'file_too_large', truncated: true, full byteLength.
  • Hook: an old desktop's runtime_error with message file_too_large renders "File too large for mobile preview"; a truncated reply publishes truncated/byteLength into the ready state.
  • Status line: truncated wording wins over "Read only"; save errors still win over both.
  • Route-parity hash and string count re-pinned for the reader change.

The desktop bridge refused markdown over a private 512 KiB cap with
file_too_large, which reached the phone as a generic runtime_error that
the reader discarded, so a 632 KB file showed "Couldn't load markdown".
Reads now return a UTF-8-boundary prefix under one shared 2 MiB budget,
marked truncated with the full byteLength and read-only. The phone shows
the truncation like file tabs do and maps refusal codes to real copy, so
an older desktop's refusal reads "File too large for mobile preview".

Claude-Session: https://claude.ai/code/session_01JNnE9qzUZMMnqpZWCqM3nb
The read budget sits above the edit budget, so every truncated document is
already read-only as file_too_large. Pin that ordering so a future budget
change cannot make a prefix editable, and name the constant as the markdown
preview budget, separate from the file preview's own.

Claude-Session: https://claude.ai/code/session_01JNnE9qzUZMMnqpZWCqM3nb
A phone holding a truncated prefix must not write it back; the 256 KiB
save guard refuses it as file_too_large before any version check. The
shared budget test shrinks to the ordering it pins and names the bridge
test as the behavioural pin.

Claude-Session: https://claude.ai/code/session_01JNnE9qzUZMMnqpZWCqM3nb
@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: faf8a8cd-86ce-4d14-8787-7bbc7ed7d4c6

📥 Commits

Reviewing files that changed from the base of the PR and between 0c34d1e and b0423ab.

📒 Files selected for processing (5)
  • mobile/src/session/mobile-markdown-disk-fallback.test.ts
  • mobile/src/session/mobile-markdown-disk-fallback.ts
  • mobile/src/session/mobile-session-route-types.ts
  • mobile/src/session/session-read-reply-schema.ts
  • src/shared/mobile-markdown-document.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • mobile/src/session/session-read-reply-schema.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.


📝 Walkthrough

Walkthrough

Mobile Markdown reads now allow content up to 2 MiB and truncate larger documents at a UTF-8 boundary. Truncated responses carry the full byte length through mobile document state and disk fallback. The reader displays status text for truncation, read-only state, stale state, or save errors. Preview failures use specific messages for binary files and files that exceed the mobile preview limit.

Priority: ➖ Normal

Merge Risk: 🟡 Moderate · up to b0423

An oversized Markdown document can still be replaced through a save request even though mobile presents it as read-only. Prevent that replacement before merging.

Security Architecture Review

Security architecture risk: 🟡 Moderate · up to b0423

Large Markdown files now open as read-only previews, but a client able to send its own save request may be able to replace some files despite that read-only designation. The exposure is limited to an accessible open Markdown tab; the normal phone interface disables saving.

Retained concerns

  • Medium · security · inferred: Newly readable 512 KiB–2 MiB read-only documents expose a full-content version that a crafted short save can use, because the save boundary checks replacement size rather than current-document size.
Security review details

Security Blast Radius

  • inferred — The widened overwrite scenario is confined to a client able to address an accessible open Markdown tab and submit a save request; the evidence does not establish exposure to unauthenticated callers.

Security Findings and Attack Paths

  • inferred — For a current file above 512 KiB and at most 2 MiB, a client can obtain its full-content version through the newly successful read, then submit that version with short replacement content. The unchanged save check can accept it despite the read-only reply. The ordinary phone interface does not offer this save action for a non-editable document.

Trust Boundaries and Controls

  • observed — The mobile reader derives editability from the reply's editable field rather than truncation metadata. The mobile editor disables editing and saving for non-editable documents; the host save path checks replacement size and the current-content version.

Resilience and Maintainability Implications

  • observed — Saves are queued per source file, compare against freshly read current content, and restore the previous draft on a failed write. These controls do not check whether the current document exceeds the edit limit.

Hardening Proposals

  • proposed — Enforce the edit-size and read-only policy against freshly read current content inside the queued save operation, independently of the client-provided version or replacement size.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description gives a detailed explanation of the problem, cause, fix, compatibility impact, and tests. However, it omits several required template sections, including Linked Issue, Visual Proof or … Add the missing template sections. Include a linked issue under “Linked Issue,” provide before/after visual proof or write N/A with a reason, complete the AI Disclosure and Agent skill upstream boundary sections as applicable, address the…
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 14 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: oversized mobile Markdown reads are truncated instead of rejected.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description gives a detailed explanation of the problem, cause, fix, compatibility impact, and tests. However, it omits several required template sections, including Linked Issue, Visual Proof or an explicit N/A explanation, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist.

Resolution

Add the missing template sections. Include a linked issue under “Linked Issue,” provide before/after visual proof or write N/A with a reason, complete the AI Disclosure and Agent skill upstream boundary sections as applicable, address the Notes items, and complete the Checklist.

✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Reject saves based on the current document size. · mobile-markdown-bridge.ts:97-104

src/renderer/src/runtime/mobile-markdown-bridge.ts:97-104
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Reject saves based on the current document size.

For a document over 2 MiB, a save request containing at most 256 KiB passes both size checks because they inspect the submitted content. If baseVersion matches the full document, the request can replace the truncated document with that short content. If the request uses the returned prefix version, it reports conflict instead of the required file_too_large. After readCurrentContent, reject an oversized current document before the version and duplicate-save checks.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 8b96e7fd-d75a-4827-a609-a148abc3b434

📥 Commits

Reviewing files that changed from the base of the PR and between 84daac2 and 0c34d1e.

📒 Files selected for processing (14)
  • mobile/src/session/MobileSessionMarkdownReader.tsx
  • mobile/src/session/mobile-markdown-disk-fallback.test.ts
  • mobile/src/session/mobile-markdown-disk-fallback.ts
  • mobile/src/session/mobile-markdown-reader-status.test.ts
  • mobile/src/session/mobile-markdown-reader-status.ts
  • mobile/src/session/mobile-session-route-parity.test.ts
  • mobile/src/session/mobile-session-route-types.ts
  • mobile/src/session/session-read-reply-schema.ts
  • mobile/src/session/use-mobile-session-document-readers.test.ts
  • mobile/src/session/use-mobile-session-document-readers.ts
  • src/renderer/src/runtime/mobile-markdown-bridge.test.ts
  • src/renderer/src/runtime/mobile-markdown-bridge.ts
  • src/shared/mobile-markdown-document.test.ts
  • src/shared/mobile-markdown-document.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

The read truncation measures the document once. The disk fallback drops
its truncated-only read-only text, which the status line never showed,
and both truncation fields are optional there. A markdown doc's flag is
only ever true, and the schema comment names the hook, not line numbers.

Claude-Session: https://claude.ai/code/session_01JNnE9qzUZMMnqpZWCqM3nb

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes — a mobile markdown read over the old private 512 KiB cap no longer fails; the host returns a UTF-8-boundary prefix marked truncated, and the phone renders a size note instead of the generic error.

  • Shared read budget: MOBILE_MARKDOWN_READ_MAX_BYTES (2 MiB) added beside the 256 KiB edit cap, with truncateMobileMarkdownRead returning either the whole document or a clampUtf8TextPrefix prefix plus the full byte length.
  • Bridge: readMobileMarkdownTab truncates rather than throwing file_too_large, hashes the prefix for version, and forwards optional truncated/byteLength; the private cap and its throw were removed from readFileContent.
  • Mobile reader: session-read-reply-schema accepts the two optional fields, the ready state carries them, and the disk fallback propagates byteLength.
  • Status line: the inline status logic moved to markdownReaderStatusText, which shows "Preview truncated. File size: X." (the file tab's wording and formatter) ahead of "Read only".
  • Refusal mapping: markdown and file tabs share documentReadErrorMessage, so binary_file/file_too_large get real copy and only unknown errors keep their fallback.
  • Tests: red-first bridge tests (a 632 KB read now succeeds, an over-budget read is cut on a UTF-8 boundary, a save of the prefix is refused), reader tests for an old desktop's refusal and truncation propagation, status precedence, plus a re-pinned route-parity hash and string count.

I traced the save path for data loss and found none: truncated implies over MOBILE_MARKDOWN_EDIT_MAX_BYTES, so the document is never editable, the mobile save action returns early on !editable, and a save carrying the prefix hash conflicts against the full-file hash. Mixed-version behavior matches the described compatibility story. All changed suites, pnpm tc:web/tc:node, the mobile typecheck, and the changed-code quality gate pass locally.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes — the delta since the prior review (0c34d1e → b0423ab) is a type/behavior refactor plus a doc fix on top of the truncation change.

  • Measure once: truncateMobileMarkdownRead now computes utf8ByteLength(content) a single time and branches on <= MOBILE_MARKDOWN_READ_MAX_BYTES, replacing the limit check followed by a second full measurement. Same whole/truncated decision, one scan.
  • Fallback doc typed and simplified: buildMarkdownDiskFallbackDoc takes truncated?: boolean, returns Extract<MarkdownDocState, { status: 'ready' }>, and no longer sets a truncated-specific readOnlyReason — the status line owns that copy, so a truncated disk read still renders "Preview truncated. File size: X.".
  • Narrowed state: MarkdownDocState.truncated tightened from boolean to true, matching the only two writers (the reader and the disk fallback), which both assign the literal.
  • Stale reference dropped: the session-read-reply-schema JSDoc's line-range citation was replaced with named readMarkdownTab / useMobileSessionDocumentReaders references, so it no longer drifts when the reader moves.

Re-ran the mobile typecheck, tc:node, and the four mobile suites plus the shared/renderer suites — all pass.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@Jinwoo-H
Jinwoo-H merged commit fe34acd into main Sep 28, 2026
64 of 66 checks passed
@Jinwoo-H
Jinwoo-H deleted the mobile-markdown-read-truncate branch September 29, 2026 03:50
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.

1 participant