Skip to content

fix(webapp): precise S2 record cap + CORS 413 on session append#3720

Open
ericallam wants to merge 2 commits into
mainfrom
fix/realtime-append-precise-cap
Open

fix(webapp): precise S2 record cap + CORS 413 on session append#3720
ericallam wants to merge 2 commits into
mainfrom
fix/realtime-append-precise-cap

Conversation

@ericallam
Copy link
Copy Markdown
Member

Summary

Two improvements to session .in/append:

  • Oversize-body 413 responses now carry CORS headers, so browser fetches see a readable status instead of an opaque TypeError: Failed to fetch. App-side retry-on-disconnect loops no longer spin forever on a permanently-rejected payload.
  • The per-record cap is now computed precisely against S2's actual ceiling instead of the conservative 512 KiB floor. Legitimate ~600-900 KiB tool outputs (search results, file content) now succeed; pathological all-quote content that would double under JSON escape still rejects cleanly.

Design

S2 enforces a per-record metered size of 8 + 2*H + Σ(header name + value) + body ≤ 1048576 bytes. With no record headers (our case), the budget reduces to body ≤ 1048568. Verified empirically against cloud S2 — append succeeds at metered=1048576 and 422s at 1048577 with record must have metered size less than 1 MiB.

The old MAX_APPEND_BODY_BYTES = 512 KiB was derived by assuming worst-case JSON escape doubling (every byte becomes \" or \\), giving (1 MiB - overhead) / 2. Safe, but rejects ~half the legitimate input space.

The new flow:

  1. Pre-cap the HTTP body at 1 MiB (DoS guard against reading arbitrary garbage before we can compute the wrap).
  2. After reading, S2RealtimeStreams.#appendPartByName computes Buffer.byteLength(JSON.stringify({data: part, id: partId}), "utf8") + 8 and throws S2RecordTooLargeError (a ServiceValidationError with status 413) if it would exceed S2's ceiling. The route's existing error branch maps the throw to a 413 with a descriptive message.

The 413 CORS fix is a single-line change in apiBuilder.server.tswrapResponse was being skipped on the body-too-large branch; every other error branch wraps; the 413 was the exception.

Test plan

  • Empirically verified against cloud S2 with a boundary scan across [1048568, 1048569, ..., 1048576] and across H ∈ {0, 1×5 hdr bytes, 1×14 hdr bytes} — the formula matches exactly
  • Browser-side fetch on a 700 KiB POST now resolves with a readable status: 413 (no TypeError: Failed to fetch)
  • A 900 KiB ASCII tool output now passes (would have 413'd at 512 KiB pre-fix)

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 23, 2026

⚠️ No Changeset found

Latest commit: 62aaeef

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 23, 2026

Review Change Stack

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8f9f46c4-3832-4256-9d77-5458a44cbb2d

📥 Commits

Reviewing files that changed from the base of the PR and between 7b7a600 and 62aaeef.

📒 Files selected for processing (1)
  • apps/webapp/app/routes/realtime.v1.streams.$runId.input.$streamId.ts
 _____________________________________________________________________________________________________________________________________
< Prototype to learn. Prototyping is a learning experience. Its value lies not in the code you produce, but in the lessons you learn. >
 -------------------------------------------------------------------------------------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

Walkthrough

This PR adds per-record metered-size enforcement to realtime session append operations. The S2 service layer now exports size constants and a S2RecordTooLargeError class, validates records by JSON-encoding and computing UTF-8 byte length plus overhead, and rejects records exceeding the 1 MiB limit with HTTP 413. Route-level HTTP body caps are increased from 512 KiB to 1 MiB as a DoS pre-guard. Error responses are wrapped with CORS handling to respect configured CORS strategies. The changelog documents the transition from opaque fetch errors to readable 413 responses.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the two main changes: precise S2 record cap calculation and CORS headers on 413 responses for session append.
Description check ✅ Passed The description is comprehensive and follows the template structure, covering summary, design rationale, test plan, and technical details of both changes.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/realtime-append-precise-cap

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 and usage tips.

coderabbitai[bot]

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

View 3 additional findings in Devin Review.

Open in Devin Review

Comment thread apps/webapp/app/services/realtime/s2realtimeStreams.server.ts
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