Skip to content

fix(runtime): cap remote git.diff and file previews at the transport budget - #14160

Merged
nwparker merged 6 commits into
mainfrom
nwparker/git-diff-transport-cap
Aug 14, 2026
Merged

nwparker merged 6 commits into
mainfrom
nwparker/git-diff-transport-cap

Conversation

@nwparker

@nwparker nwparker commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

The bug

A remote or mobile user who opens the diff of a large image loses their whole WebSocket, not just that request. The E2EE channel closes with 1013 when a reply exceeds the 4 MiB outbound envelope, and two producers can exceed it unaided — no adversary required.

Producer Cap today Worst case in one envelope
git.diff text MAX_RENDERED_DIFF_COMBINED_CHARACTERS = 6M chars ~6.3 MiB
git.diff previewable binary MAX_GIT_SHOW_BYTES = 10 MiB per side ~26.7 MiB (base64, both sides)
files.readPreview RUNTIME_PREVIEWABLE_BINARY_MAX_BYTES = 10 MiB ~13.3 MiB

The text cap is a renderer budget picked for render cost, and it sits above the transport limit. Contrast files.read, which caps at 512 KiB and returns {content, truncated, byteLength} — git.diff had no equivalent and no mobile payload diet.

The fix

Cap the producers. The check lives in orca-runtime-git.ts, downstream of the dedupe and of both the SSH-provider and local branches — so a payload forwarded verbatim by an old relay is covered by the same code and src/relay/ needs no change. Local and in-process callers pass no budget and keep full fidelity; desktop-local diffs are untouched.

Measuring raw bytes would not work, which is the whole reason this needs a module rather than a constant:

  • JSON escaping turns one control byte into six (\u00XX), so worst-case expansion is 6x, not 2x.
  • binary-buffer.ts sniffs only for NUL in the first 8 KiB — so a NUL-free file of 0x01–0x1f bytes is classified as text, sails through a raw-byte cap, and then blows the envelope. That is the exact 1013 this PR exists to prevent.

The budget is escape-aware, with a three-branch fast path: raw * 6 <= budget → admit, raw > budget → reject, otherwise run the exact scan. Normal diffs cost two native byteLength calls.

Measurements are cached by result identity. This matters for SSH: up to 256 waiters can share one deduped result, and each now reuses its raw or exact measurement rather than synchronously rescanning a multi-megabyte string. The cache revalidates the serialized non-content skeleton and content values, and only stores a complete exact length.

The SSH branch of readFileExplorerPreview had the same raw-vs-escaped gap — its stat gate sizes base64 binaries, but text crossed unbounded. It now honours the same decoded-text limit the local branch already enforced. Local and SSH preview reads are bounded through file growth, and SSH stream allocation honours the caller-specific binary/text cap.

Wire compatibility

No wire change. GitDiffResult is untouched — no third kind, no new optional field, nothing changed about what the host publishes on success.

diff_too_large joins STRUCTURED_RUNTIME_PASSTHROUGH_CODES and arrives on the existing failure envelope:

Consumer Behavior
mobile-diff-review-loaders.ts maps diff_too_large to the existing too-large review state, including deleted files
mobile-file-tab-doc.ts throws response.error.message → existing error doc
Desktop remote rejects into the diff-load error UI already used for timeouts
Old client / new host one request errors instead of the connection dropping

A blanked success payload was considered and rejected: mobile-diff-review-rpc.ts reconstructs from {kind, originalContent, modifiedContent} and drops largeDiffRenderLimit, so it would render a silently empty diff; a blanked kind:'binary' would render "Text diff is unavailable for this file" — a false statement about a PNG.

Telemetry

The 1013 close was never instrumented, so its incidence has been unmeasurable — this cap had no acceptance signal. One .strict() schema, one helper over the six emitters. emitter is size (a producer emitted something too big; should fall to zero once producers cap themselves) or queue (a backed-up link). No paths, no user content.

Known regression

Remote image previews between ~3.096 MB and ~3.146 MB now return file_too_large. They only intermittently worked before — above ~3.0 MB they killed the socket — so this trades intermittent connection loss for a consistent, explained error. file_too_large is already a handled outcome on both clients.

Testing

  • 10,281 passed across src/main/runtime + src/shared + src/main/git; mobile 3,429 passed / 3 skipped across 437 files.
  • Each of the six budget-enforcement sites is independently mutation-killed — verified by neutering one assert at a time and confirming a distinct test fails for each. An earlier draft passed its whole suite with five of six neutered; that gap is closed.
  • Escaping fixtures (newline-dense, 0x01 control chars, CJK, lone surrogates, surrogate pairs, quotes/backslashes, base64) checked against native JSON.stringify at the boundary and one byte past it. The envelope-ceiling invariant is asserted on text fixtures, not just base64 — a base64-only version passes trivially and proves nothing.
  • SSH-provider path covered with an unclamped forwarded payload; local/in-process asserted uncapped.
  • Stateful differential fuzz ran 160,000 changing-budget/mutation cases against native JSON.stringify; all matched. A 256-waiter deduped SSH repro improved from 62.9–108 ms to 0.58–1.81 ms warmed.
  • Readiness-checklist sections 01–07 were independently reviewed with no P0/P1/P2 findings. tsc, oxlint, changed-code quality, max-lines, reliability, and PR-file oxfmt gates are clean.

Scope

The final reviewed diff is 29 files, +1,556/−117, including the focused regression tests and extracted budget/read-cap modules. It does not change the RPC wire schema, add an opcode, alter persisted state, add dependencies, or touch UI. Two earlier drafts added a per-request overflow envelope; that machinery was dropped in favor of producer-side caps.

Out of scope, with reasons:

  • Binary-channel routing for large diffs. The shared control connection has no binary handling, 13.3 MiB exceeds the 8 MiB frame limit so chunking and flow control become mandatory, and a new opcode needs capability negotiation. A chunked git.diffBinaryChunk RPC is the right shape if this is ever wanted, and needs no negotiation — this PR is a prerequisite either way.
  • Lowering MAX_RENDERED_DIFF_COMBINED_CHARACTERS. Tempting one-liner, but it counts UTF-16 units, so a CJK diff at the cap is still ~18 MiB of UTF-8, and it does nothing for the base64 path.
  • Residual 1013 sites that no producer cap can reach: outbound queue overflow, oversized binary frames, and injectDeviceScope re-serializing status.get. All narrow; the new telemetry will show whether any fire.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: aa5d6da5-b158-4897-b34d-4f3924766579

📥 Commits

Reviewing files that changed from the base of the PR and between 3035aa9 and 55e7922.

📒 Files selected for processing (29)
  • mobile/src/files/mobile-file-tab-doc.test.ts
  • mobile/src/session/mobile-diff-review-loaders.test.ts
  • mobile/src/session/mobile-diff-review-loaders.ts
  • src/main/providers/filesystem-provider-contract.ts
  • src/main/providers/ssh-filesystem-provider-stream.test.ts
  • src/main/providers/ssh-filesystem-provider.ts
  • src/main/providers/types.ts
  • src/main/runtime/orca-runtime-files.test.ts
  • src/main/runtime/orca-runtime-files.ts
  • src/main/runtime/orca-runtime-git-diff-budget.test.ts
  • src/main/runtime/orca-runtime-git.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/rpc/e2ee-channel-text-backpressure.test.ts
  • src/main/runtime/rpc/e2ee-channel.ts
  • src/main/runtime/rpc/errors.ts
  • src/main/runtime/rpc/methods/files-preview-transport-budget.test.ts
  • src/main/runtime/rpc/methods/files.ts
  • src/main/runtime/rpc/methods/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/methods/git.test.ts
  • src/main/runtime/rpc/methods/git.ts
  • src/main/runtime/runtime-rpc.test.ts
  • src/main/ssh/ssh-file-stream-read-cap.ts
  • src/main/ssh/ssh-filesystem-stream-reader.ts
  • src/shared/git-diff-transport-budget.test.ts
  • src/shared/git-diff-transport-budget.ts
  • src/shared/remote-rpc-content-budget.test.ts
  • src/shared/remote-rpc-content-budget.ts
  • src/shared/telemetry-events.test.ts
  • src/shared/telemetry-events.ts
🚧 Files skipped from review as they are similar to previous changes (28)
  • src/main/providers/ssh-filesystem-provider.ts
  • src/main/runtime/rpc/methods/files-preview-transport-budget.test.ts
  • src/shared/telemetry-events.test.ts
  • src/main/runtime/runtime-rpc.test.ts
  • mobile/src/session/mobile-diff-review-loaders.test.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/rpc/errors.ts
  • mobile/src/files/mobile-file-tab-doc.test.ts
  • src/main/providers/filesystem-provider-contract.ts
  • src/main/ssh/ssh-file-stream-read-cap.ts
  • src/main/ssh/ssh-filesystem-stream-reader.ts
  • src/main/providers/types.ts
  • src/main/runtime/rpc/e2ee-channel-text-backpressure.test.ts
  • mobile/src/session/mobile-diff-review-loaders.ts
  • src/main/runtime/rpc/methods/git.test.ts
  • src/main/runtime/orca-runtime-git-diff-budget.test.ts
  • src/main/providers/ssh-filesystem-provider-stream.test.ts
  • src/main/runtime/orca-runtime-git.ts
  • src/shared/remote-rpc-content-budget.test.ts
  • src/shared/telemetry-events.ts
  • src/main/runtime/rpc/methods/git.ts
  • src/main/runtime/rpc/methods/files.ts
  • src/shared/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/methods/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/e2ee-channel.ts
  • src/main/runtime/orca-runtime-files.test.ts
  • src/main/runtime/orca-runtime-files.ts
  • src/shared/git-diff-transport-budget.ts

📝 Walkthrough

Walkthrough

The changes add JSON-aware transport budgets for Git diffs and file previews. Runtime Git commands and remote RPC handlers enforce these budgets while local callers remain uncapped without a limit. File preview reads apply binary and text limits across local and SSH paths, including base64 and serialized-result sizing. Structured diff_too_large errors pass through RPC mapping and produce mobile fallback states. Outbound RPC budget closures now emit telemetry that identifies size or queue causes.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed and relevant but omits the required Linked Issue and Visual Proof sections. Add a linked issue using Fixes #... and add the Visual Proof section with N/A plus a reason if no UI or interaction changes exist.
Docstring Coverage ⚠️ Warning Docstring coverage is 23.33% which is insufficient. The required threshold is 80.00%. 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 summarizes the main change: enforcing transport budgets for remote Git diffs and file previews.
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.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/runtime/rpc/e2ee-channel-text-backpressure.test.ts (1)

123-126: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a size-path telemetry assertion.

The new assertion covers only emitter: 'queue'. Add an oversized legacy or V2 reply case and assert emitter: 'size'. This verifies the channel wiring in src/main/runtime/rpc/e2ee-channel.ts; the schema test only verifies that the value is accepted.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7becc225-9b27-4781-9aeb-7f37f6834212

📥 Commits

Reviewing files that changed from the base of the PR and between 150cc6f and f41018b.

📒 Files selected for processing (16)
  • mobile/src/files/mobile-file-tab-doc.test.ts
  • src/main/runtime/orca-runtime-files.test.ts
  • src/main/runtime/orca-runtime-files.ts
  • src/main/runtime/orca-runtime-git-diff-budget.test.ts
  • src/main/runtime/orca-runtime-git.ts
  • src/main/runtime/rpc/e2ee-channel-text-backpressure.test.ts
  • src/main/runtime/rpc/e2ee-channel.ts
  • src/main/runtime/rpc/errors.ts
  • src/main/runtime/rpc/methods/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/methods/git.test.ts
  • src/main/runtime/rpc/methods/git.ts
  • src/main/runtime/runtime-rpc.test.ts
  • src/shared/git-diff-transport-budget.test.ts
  • src/shared/git-diff-transport-budget.ts
  • src/shared/telemetry-events.test.ts
  • src/shared/telemetry-events.ts

Comment thread src/shared/telemetry-events.ts Resolved

@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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/main/runtime/rpc/methods/files-preview-transport-budget.test.ts (1)

13-61: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Cover the runtime client kind.

This test only dispatches with clientKind: 'mobile'. Parameterize it for mobile and runtime so a regression that caps only mobile previews fails.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5c6d4115-9819-499a-a828-6fcbce389f2f

📥 Commits

Reviewing files that changed from the base of the PR and between f41018b and e53a216.

📒 Files selected for processing (23)
  • mobile/src/session/mobile-diff-review-loaders.test.ts
  • mobile/src/session/mobile-diff-review-loaders.ts
  • src/main/providers/filesystem-provider-contract.ts
  • src/main/providers/ssh-filesystem-provider-stream.test.ts
  • src/main/providers/ssh-filesystem-provider.ts
  • src/main/providers/types.ts
  • src/main/runtime/orca-runtime-files.test.ts
  • src/main/runtime/orca-runtime-files.ts
  • src/main/runtime/orca-runtime-git-diff-budget.test.ts
  • src/main/runtime/orca-runtime.test.ts
  • src/main/runtime/rpc/e2ee-channel-text-backpressure.test.ts
  • src/main/runtime/rpc/e2ee-channel.ts
  • src/main/runtime/rpc/methods/files-preview-transport-budget.test.ts
  • src/main/runtime/rpc/methods/files.ts
  • src/main/runtime/rpc/methods/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/methods/git.ts
  • src/main/runtime/runtime-rpc.test.ts
  • src/main/ssh/ssh-file-stream-read-cap.ts
  • src/main/ssh/ssh-filesystem-stream-reader.ts
  • src/shared/git-diff-transport-budget.test.ts
  • src/shared/git-diff-transport-budget.ts
  • src/shared/remote-rpc-content-budget.test.ts
  • src/shared/remote-rpc-content-budget.ts
🚧 Files skipped from review as they are similar to previous changes (6)
  • src/main/runtime/orca-runtime-git-diff-budget.test.ts
  • src/shared/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/methods/git-diff-transport-budget.test.ts
  • src/main/runtime/rpc/methods/git.ts
  • src/main/runtime/rpc/e2ee-channel.ts
  • src/main/runtime/runtime-rpc.test.ts

@nwparker

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

nwparker and others added 5 commits August 13, 2026 23:11
…budget

A remote or mobile user who opens the diff of a large image loses their whole
WebSocket, not just that request: the E2EE channel closes with 1013 when a reply
exceeds the 4 MiB outbound envelope. Two producers can exceed it unaided.

git.diff/branchDiff/commitDiff cap text with MAX_RENDERED_DIFF_COMBINED_CHARACTERS
(6M chars) -- a *renderer* budget that sits above the transport limit -- and return
base64 for previewable binaries bounded only by MAX_GIT_SHOW_BYTES, so a 10 MiB PNG
changed in place is ~26.7 MiB in one envelope. files.readPreview inlines base64 up
to 10 MiB, and mobile calls it for every image tab.

Both now measure against a budget derived from the outbound limit. The check sits in
orca-runtime-git.ts, downstream of the dedupe and of both the SSH-provider and local
branches, so a payload forwarded verbatim by an old relay is covered by the same code
and src/relay needs no change. Local and in-process callers pass no budget and keep
full fidelity.

Measuring raw bytes would not work, which is the whole reason this needs a module.
JSON escaping turns one control byte into six (\u00XX), and binary-buffer.ts sniffs
only for NUL in the first 8 KiB -- so a NUL-free file of 0x01-0x1f bytes is classified
as *text*, would pass a raw-byte cap, and would then blow the envelope. The budget is
escape-aware, with a three-branch fast path that keeps normal diffs at two native
byteLength calls and scans only the ambiguous band.

The SSH branch of readFileExplorerPreview had the same raw-vs-escaped gap: its stat
gate sizes base64 binaries, but text crossed unbounded. It now honours the same
decoded-text limit the local branch already enforced.

No wire change: GitDiffResult is untouched -- no third kind, no new field. Old clients
see an error for one request instead of a dropped connection. diff_too_large joins the
structured passthrough codes and lands on an existing error arm in both mobile
consumers and the desktop remote path; file_too_large was already handled on both.

Instruments the 1013 close, which nothing measured before, so the incidence this cap
is meant to drive to zero is finally observable. `emitter` separates a producer size
bug from a wedged link.

Known regression: remote image previews between ~3.096 and ~3.146 MB now return
file_too_large. They only intermittently worked before -- above ~3.0 MB they killed
the socket -- so this trades intermittent connection loss for a consistent error.

Test: 10281 passed in src/main/runtime + src/shared + src/main/git; mobile 3427
passed. Each of the six budget-enforcement sites is independently mutation-killed.
Escaping fixtures cover newline-dense, control-char, CJK, lone-surrogate and base64
content against native JSON.stringify. tsc clean for node, web and cli; oxlint clean.

Co-authored-by: Orca <[email protected]>
…emoval

Upstream #14447 dropped the shared/types barrel; GitDiffResult now lives in
git-diff-compare-types and GlobalSettings in global-settings-types.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the nwparker/git-diff-transport-cap branch from c620ddc to 55e7922 Compare August 14, 2026 06:17
@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

The stream reader aborts an over-cap read with StreamProtocolError, whose numeric
code falls through mapRuntimeError to a generic runtime_error carrying the raw
"Reported totalSize N exceeds client cap M" string. Neither preview client
recognizes that: runtime-file-client.ts and mobile-file-preview-response.ts both
key on file_too_large. It also made the two file_too_large guards directly below
the read unreachable on the streaming path.

Gives the cap its own error type so the caller can translate it, keeping the
bandwidth saving the cap exists for. A genuine protocol fault still propagates
unmasked.

Found by the readiness review. Mutation-verified: removing the translation fails
exactly the new test.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker merged commit 2100fb2 into main Aug 14, 2026
51 checks passed
@nwparker
nwparker deleted the nwparker/git-diff-transport-cap branch August 14, 2026 07:25
nwparker added a commit that referenced this pull request Aug 17, 2026
requestGitStreamable trusted the relay's declared size: every chunk was pushed
into `parts`, then Buffer.concat'd and JSON.parse'd with no ceiling. A buggy or
hostile relay could name any totalBytes and the host would allocate it.

The file path already had this gate — ssh-filesystem-stream-reader checks the
declared totalSize against a cap before accepting a stream. #14160 added a read
cap for files only; this closes the same hole for git.

Rejects at the marker, before a single chunk transfers, and cancels the stream so
the relay stops sending instead of us draining a payload we already refused.

The cap is 32 MiB, above anything a legitimate diff can produce: the relay caps
each git exec at MAX_GIT_BUFFER (10 MiB), so two blobs base64-inflated top out
near 27 MiB. So no diff that works today starts failing.

Test: mutation-verified — disabling the gate hangs the over-cap case until the
inactivity timeout instead of rejecting. 7471 tests pass across ssh, providers,
runtime and relay; node and cli typechecks clean.

Co-authored-by: Orca <[email protected]>
nwparker added a commit that referenced this pull request Aug 17, 2026
When a diff RPC fails, useEditorPanelContentState fabricates a result whose
modifiedContent is \`Error loading diff: \${err}\` so the pane has something to
show. For an unstaged diff that pane is editable, so Cmd+S calls onSave with the
editor body — and attemptEditorFileSave falls back to that body when no draft
exists (editor-autosave-controller). The error sentence gets written over the
user's file.

Reachable from any diff-load failure: SSH drop, git error, permission denied, or
the diff_too_large introduced in #14160.

The existing largeDiffSaveContentAvailable guard does not cover this. It only
gates the LargeDiffFallback button, which renders when largeDiffRenderLimit is
limited; on the error path that field is absent, so the full editable DiffEditor
mounts and installs the save shortcut instead.

Marks the fabricated result and holds the pane read-only, which blocks the save
shortcut and prevents a draft being minted — a draft would otherwise be saveable
even with onSave gated. loadError mirrors the field FileContent already carries
for the plain-file path, and lives on the renderer-local DiffContent, so nothing
on the wire changes.

Test: mutation-verified — dropping the guard fails exactly the new read-only
case. 23094 renderer tests pass; web and node typechecks clean.

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…budget (stablyai#14160)

* fix(runtime): cap remote git.diff and file previews at the transport budget

A remote or mobile user who opens the diff of a large image loses their whole
WebSocket, not just that request: the E2EE channel closes with 1013 when a reply
exceeds the 4 MiB outbound envelope. Two producers can exceed it unaided.

git.diff/branchDiff/commitDiff cap text with MAX_RENDERED_DIFF_COMBINED_CHARACTERS
(6M chars) -- a *renderer* budget that sits above the transport limit -- and return
base64 for previewable binaries bounded only by MAX_GIT_SHOW_BYTES, so a 10 MiB PNG
changed in place is ~26.7 MiB in one envelope. files.readPreview inlines base64 up
to 10 MiB, and mobile calls it for every image tab.

Both now measure against a budget derived from the outbound limit. The check sits in
orca-runtime-git.ts, downstream of the dedupe and of both the SSH-provider and local
branches, so a payload forwarded verbatim by an old relay is covered by the same code
and src/relay needs no change. Local and in-process callers pass no budget and keep
full fidelity.

Measuring raw bytes would not work, which is the whole reason this needs a module.
JSON escaping turns one control byte into six (\u00XX), and binary-buffer.ts sniffs
only for NUL in the first 8 KiB -- so a NUL-free file of 0x01-0x1f bytes is classified
as *text*, would pass a raw-byte cap, and would then blow the envelope. The budget is
escape-aware, with a three-branch fast path that keeps normal diffs at two native
byteLength calls and scans only the ambiguous band.

The SSH branch of readFileExplorerPreview had the same raw-vs-escaped gap: its stat
gate sizes base64 binaries, but text crossed unbounded. It now honours the same
decoded-text limit the local branch already enforced.

No wire change: GitDiffResult is untouched -- no third kind, no new field. Old clients
see an error for one request instead of a dropped connection. diff_too_large joins the
structured passthrough codes and lands on an existing error arm in both mobile
consumers and the desktop remote path; file_too_large was already handled on both.

Instruments the 1013 close, which nothing measured before, so the incidence this cap
is meant to drive to zero is finally observable. `emitter` separates a producer size
bug from a wedged link.

Known regression: remote image previews between ~3.096 and ~3.146 MB now return
file_too_large. They only intermittently worked before -- above ~3.0 MB they killed
the socket -- so this trades intermittent connection loss for a consistent error.

Test: 10281 passed in src/main/runtime + src/shared + src/main/git; mobile 3427
passed. Each of the six budget-enforcement sites is independently mutation-killed.
Escaping fixtures cover newline-dense, control-char, CJK, lone-surrogate and base64
content against native JSON.stringify. tsc clean for node, web and cli; oxlint clean.

Co-authored-by: Orca <[email protected]>

* fix(runtime): harden remote reply transport budgets

* test(runtime): cover desktop remote preview budgets

* test(runtime): close telemetry review gaps

* chore(shared): repoint budget imports after the shared/types barrel removal

Upstream stablyai#14447 dropped the shared/types barrel; GitDiffResult now lives in
git-diff-compare-types and GlobalSettings in global-settings-types.

Co-authored-by: Orca <[email protected]>

* fix(ssh): surface an over-cap preview read as file_too_large

The stream reader aborts an over-cap read with StreamProtocolError, whose numeric
code falls through mapRuntimeError to a generic runtime_error carrying the raw
"Reported totalSize N exceeds client cap M" string. Neither preview client
recognizes that: runtime-file-client.ts and mobile-file-preview-response.ts both
key on file_too_large. It also made the two file_too_large guards directly below
the read unreachable on the streaming path.

Gives the cap its own error type so the caller can translate it, keeping the
bandwidth saving the cap exists for. A genuine protocol fault still propagates
unmasked.

Found by the readiness review. Mutation-verified: removing the translation fails
exactly the new test.

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Orca <[email protected]>
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