Skip to content

shaping: let records span full-size packets, as real servers' do - #44

Merged
myleshorton merged 2 commits into
mainfrom
fisk/shaping-spanning-records
Nov 25, 2026
Merged

myleshorton merged 2 commits into
mainfrom
fisk/shaping-spanning-records

Conversation

@myleshorton

@myleshorton myleshorton commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Why

Shaping draws the sizes and directions of a connection's first 10 packets from the CenKL model, but every packet then went out as its own TLS record, so within the shaped window no record was ever longer than a packet. Record lengths are readable from the unencrypted record headers, and real servers routinely write records that span several packets (a 16 KB record leaves as a run of full-MSS packets and a remainder).

What

shapingSpanRecords turns the packet schedule into records: a full-size packet (shapingMaxPacket, 1448) and the next packet from the same side are one record. That record is one write, and TCP cuts it back into exactly the scheduled packets, so on a 1448-byte MSS path, packet sizes and directions are unchanged; only the record headers differ. About 13% of drawn schedules contain such a run; the rest are identical to before.

Only exactly-full packets start a span: cutting after e.g. a 1440-byte packet would make TCP split the write at 1448 and change the packets. On a 1460-byte MSS path (no TCP timestamps), a spanning record leaves as full 1460-byte packets, as a real server's would.

shapingRecords is renamed shapingPackets, since it counts packets.

Compatibility

  • A plan whose records span packets gets its own marker (a version byte in the marker HMAC), so it differs from the legacy marker even when nothing merges.
  • Servers accept both markers (acceptShaping), so existing clients keep working.
  • Clients send only the new plan, so servers need this before clients do: roll lantern-box to the fleet first, then bump it in radiance/lantern. The legacy plan can be removed once old clients are gone.

Performance

  • About 3.9 µs per new connection to work out both plans, almost all of it the schedule draw that already happened; the new part is one extra HMAC.
  • Fewer records means fewer shapingGrace waits on an idle side: an idle schedule with two full-size runs finished in 23 ms vs 56 ms before (10 records → 4).
  • After the window nothing changes.

Testing

  • TestShapingSpanRecords: runs, direction changes, a trailing full packet, near-full sizes that must not merge, the 16 KB record cap.
  • TestShapingRecordsSpanPackets: on-the-wire record sizes for both HTTP/2 servers, for new clients and for a legacy client against an updated server. Verified to fail with merging disabled.
  • TestShapingScheduleDeterministic checks, for 198 seeds, that the current plan's records are the legacy plan's packets spanned and that the two markers differ.
  • TestShapingEndToEnd no longer expects exactly 10 records (it failed intermittently when a drawn schedule merged).
  • go build ./..., go vet ./..., go test -race -count=1 ./... pass (as CI runs them); the end-to-end shaping tests passed 40× in a row.
  • Not tested: real-path packet segmentation (loopback MSS is far larger than 1448), so packet sizes on the wire follow from TCP's segmentation rather than being observed.

Risk

  • A client with this change fails against a server without it (the server takes the new marker for an HTTP/2 preface). Hence servers first.
  • Riskiest file: shaping.go (shapingPlans, acceptShaping).

How to revert

Revert the merge commit. If clients already shipped it, servers must keep accepting the spanning marker until those clients are gone, so revert servers last.

Pre-PR review (local gate: Claude code review, in place of Codex)

  • Codex gate not run: the Codex workspace is out of credits (codex exec failed twice). At the author's request, Claude Code's /code-review replaced it.
  • Reviewed at: 20bd5c2; fixes in 76dec7a
  • Exit: all remaining findings dismissed with reasons
  • CodeRabbit: cr=r1 CODERABBIT GATE: status=completed critical=0 important=0 minor=0 nits=0 reviewed_files=4 reviewed_sha=20bd5c2
  • Description check: not run (needs Codex); checked by hand against the diff
  • Step back: not needed
  • Fixed:
    • [claude] README.md:102, shaping.go:56 — the MSS caveat only covered a smaller MSS; now covers any MSS other than 1448, including 1460
    • [claude] README.md:77, shaping.go:60 — "after the 10th record" and shapingRecords counted records where the schedule counts packets
  • Dismissed:
    • [claude] client.go:202 — new clients fail on old servers — by design; the marker must change with the framing. Rollout order stated above.
    • [claude] shaping.go:117 — spanning records cut at 1460 on paths without timestamps — on such a path they leave as full 1460-byte packets, as a real server's would; the legacy plan's 1448-byte packets mid-burst there were the less realistic shape. Documented.
    • [claude] shaping.go:90 — shapingUseLegacy is an unsynchronized test global — same pattern as shapingScheduleOverride; each subtest's cleanup closes its client before the next one writes it; every -race run was clean.
    • [claude] shaping_test.go:310 — loosened end-to-end length check — the spanned-records equivalence is asserted for 198 seeds in TestShapingScheduleDeterministic and the edge cases in TestShapingSpanRecords.
  • Pre-existing (not introduced here, worth filing): none
  • Proposed AGENTS.md additions: none

GitHub review (/review-pr)

  • COPILOT GATE: unresolved_threads=0 threads_capped=false head=76dec7a copilot_reviewed=76dec7a comments_at_head=0 summary_missed=false: Copilot reviewed this head (automatic, 1 review) with no open issues. It flags that loopback tests can't confirm packet sizes on a real 1448-byte MSS path, as Testing above says.
  • CodeRabbit check at 76dec7a: success, "Review completed", no findings.
  • Verification rounds: none needed (no fixes after the reviews). Codex is out of credits; any such round would have used Claude Code's /code-review instead.

🤖 Generated with Claude Code

myleshorton and others added 2 commits November 25, 2026 07:13
The schedule is of packets, and every packet became its own TLS record,
so no record in the shaped window was longer than a packet. Censors read
record lengths from the headers, and real servers write records that
span packets. Now a full-size packet (1448 bytes) and the next packet
from the same side are one record: one write that TCP cuts back into the
scheduled packets, with a header only on the first.

Plans that span packets get their own marker (a version byte in the
HMAC), and servers accept both it and the legacy marker, so clients from
before this keep working. Clients send only the new plan, so servers need
this first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T1ZGojMkTHq5tE2Y6VNyAg
…packets

The schedule counts packets, not records, so shapingRecords is now
shapingPackets and the README says data passes through after the 10th
packet. Where the MSS isn't 1448, TCP cuts a spanning record at the
path's MSS: at 1460, as full packets, as a real server's would.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:59
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The client now builds current and legacy TLS record plans from a 10-packet schedule. The server accepts either plan’s marker and selects its records. The README and tests describe and verify packet grouping, marker selection, and record sizes.

Changes: Packet-based TLS shaping

Layer / File(s) Summary
Packet schedules and record plans
shaping.go, client.go, shaping_test.go, README.md
The client draws a 10-packet schedule and groups eligible same-direction packets into records for the current plan. It retains a legacy plan and configures the selected plan. Tests and README text cover schedules and packetization.
Server marker selection
shaping.go, shaping_test.go
The server selects current or legacy records by matching the marker. If neither marker matches, it replays the bytes as an unshaped client. End-to-end tests cover both plans.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Client.createTransport
  participant newShapedConn
  participant acceptShaping
  Client.createTransport->>newShapedConn: Configure selected records and marker
  newShapedConn->>acceptShaping: Send marker bytes
  acceptShaping->>acceptShaping: Match current or legacy marker and select records
Loading

Suggested reviewers: claude

Merge Risk: Minimal · up to 76dec

This change lets TLS records span full-size packets and keeps the legacy marker accepted on the server. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: shaping now lets a TLS record span full-size packets, matching how real servers behave. The raw summary and PR objectives both describe this as the core change in shap…
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.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the main change: shaping now lets a TLS record span full-size packets, matching how real servers behave. The raw summary and PR objectives both describe this as the core change in shaping.go, client.go, and the tests. The title is specific and accurate, though it is a little informal with the trailing 'as real servers' do'.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a578dfae-0ec3-4f2e-bd0d-1eeb4a4cf68f
Commits

Reviewing files that changed from the base of the PR and between f66a028 and 76dec7a.

Files selected for processing: 4

  • README.md
  • client.go
  • shaping.go
  • shaping_test.go

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Existing loopback tests inspect TLS records rather than actual TCP packets, so validating the packet-size and direction guarantees requires human review of packet captures on representative 1448-byte MSS paths.

No open issues 🎉

What changed in this PR

Updates the initial 10-packet shaping plan so TLS records can span full-size packets, making visible record lengths more like real TLS traffic.

Changes:

  • Merge eligible same-direction packet runs into records within the TLS size limit.
  • Give new clients a distinct marker while servers accept both current and legacy plans; servers must be deployed first.
  • Extend tests for spanning and legacy compatibility, and document packet counts and TCP segmentation limits.
File Description
shaping.go Builds both shaping plans, merges eligible packets, and accepts either marker.
shaping_test.go Tests spanning rules, marker separation, and legacy-client compatibility.
README.md Explains packet-based shaping, spanning records, and segmentation caveats.
client.go Uses the current shaping plan, with a legacy override for tests.

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants