Skip to content

fix(relay): bound control RTT samples per ping and per flush window - #19268

Merged
Jinwoo-H merged 2 commits into
mainfrom
relay/bound-control-rtt-samples
Sep 7, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
relay/bound-control-rtt-samples

Conversation

@Jinwoo-H

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

Copy link
Copy Markdown
Contributor

ELI5

The relay times how long a desktop takes to answer its heartbeat. It used to time any reply, so a desktop could send a million replies and make the relay hold a million measurements in memory until the next 30-second metrics flush. Now the relay only times the reply that answers the heartbeat it actually sent, and it never keeps more than 1024 measurements per window.

What Changed

Follow-up to #19232 (merged as ecfcc0d), addressing two CodeRabbit findings on that PR.

  • cloud/apps/relay/src/host-session-registry.ts: each session tracks pendingPingAt, the t of the ping still awaiting an echo. A pong is timed only when it echoes that exact value, and the slot is cleared on use, on rebind, and on a fresh session. One sample per ping the cell actually sent, whatever a host floods. A pong that lost the race to the next ping is dropped for timing but still updates lastPongAt, so control-silence liveness is unchanged.
  • cloud/apps/relay/src/relay-observability.ts: the process-wide window is now a 1024-sample reservoir (Algorithm R), so a flood cannot grow the heap and the percentiles stay unbiased rather than truncated to the first N. controlRttSamplesDelta keeps its meaning, round trips observed in the interval, and the new controlRttSamplesDroppedDelta reports what the reservoir did not keep, so a non-zero value says the percentiles summarise a uniform sample.
  • cloud/infra/terraform/relay-observability.tf: descriptor for the new field, plus a clarified description on control_rtt_samples.
  • cloud/apps/relay/src/relay-observability.test.ts: the leak guard now clones the entries and renames only clientAcceptsAbandonedByStageDelta.credential and clientAcceptCredentialMsP95, both path- or key-exact, so a stray raw field or value elsewhere still trips it. The regex is case-insensitive now that nothing legitimate matches.

Why

1. A host could choose how many RTT samples a cell recorded (major). recordControlRtt timed every pong whose echoed t was a finite number within 120s of now. Nothing required that pong to answer a ping the cell had sent, so an authenticated host could stream pongs with fresh timestamps and push one sample per frame into the process-wide controlRttSamplesMs array. That array was only reset by the 30s flush, which copies and sorts it twice for the p50/p95. Heap growth and flush CPU were both attacker-chosen. The per-session window was already capped at 8, but it was capped after the process-wide forward.

Requiring the echo to match the outstanding ping removes the attack surface at the source rather than clamping its symptom. The reservoir is the second line: it bounds any future caller of recordControlRtt too, and it keeps percentiles honest where a plain "drop beyond N" would bias every busy window toward its first samples.

2. The leak guard could mask a real leak (minor). scrubStageNames ran JSON.stringify(entries).replaceAll('"credential":', '"stage":') over the whole serialized event to exempt the credential accept stage. Any accidental raw "credential" field anywhere in a future event would have been scrubbed the same way.

Linked Issue

No issue; this is direct follow-up review feedback on #19232.

Visual Proof

N/A — relay server-side sampling and log-based metric fields only, no UI surface.

Testing

Four new tests, each confirmed to fail against the pre-fix source and pass after:

  • a host flooding 10,000 pongs against one outstanding ping yields exactly one sample and no per-session log line (was 10,000);
  • a pong that answers no outstanding ping, or guesses a nearby timestamp, is not timed;
  • flooding 20,480 samples publishes controlRttSamplesDelta: 20480 with controlRttSamplesDroppedDelta: 19456, which pins the retained window at the 1024 cap;
  • flooding 10,240 fast samples then 10,240 slow ones publishes the slow value at p95, so the reservoir provably samples the whole window. A truncating implementation fails this test.
cd cloud/apps/relay && npx vitest run                    # 54 files, 530 passed, 95 skipped
cd cloud && pnpm --filter @orca-cloud/relay typecheck    # clean
terraform fmt -check cloud/infra/terraform/relay-observability.tf   # clean
  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Review

Relay-only change. No wire-format change: the ping and pong frames are byte-identical to before, so an old desktop paired with a new cell behaves exactly as it does today, and a new cell paired with any desktop build that echoes t keeps producing samples. Hosts that never echo t produced no samples before this change either.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Security: this is the fix. An authenticated host can no longer drive relay heap or flush CPU through pong volume.
  • Cross-platform / SSH / mobile: relay-side only, no client change, no path or shell handling.
  • Backwards compatibility: controlRttSamplesDelta keeps its published meaning; controlRttSamplesDroppedDelta is additive and defaults to 0.
  • Performance: the flush now sorts at most 1024 samples for the RTT percentiles instead of an unbounded array.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

An authenticated host chose how many round-trip samples a cell recorded: every
pong carrying a recent plausible `t` was forwarded to the process-wide window,
which grew unbounded until the 30s flush copied and sorted it for percentiles.

Time a pong only when it echoes the `t` of the ping still outstanding on that
session, so a flood yields at most one sample per ping the cell actually sent.
A pong that lost the race to the next ping is dropped for timing but still
counts as proof of life for the silence watchdog. Bound the process-wide window
with a 1024-sample reservoir (Algorithm R) so the percentiles stay unbiased,
keep `controlRttSamplesDelta` meaning round trips observed, and publish
`controlRttSamplesDroppedDelta` for the ones the reservoir did not keep.

Replace the leak guard's blanket `"credential":` string rewrite with an exact,
path-scoped rename of the two schema keys that spell a policed word, and make
the guard case-insensitive now that nothing legitimate trips it.

Follow-up to #19232.
@pullfrog

pullfrog Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

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

@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 3dd71fcd-971e-49dd-b7c1-41f140185bf9

📥 Commits

Reviewing files that changed from the base of the PR and between fede3eb and 3225354.

📒 Files selected for processing (5)
  • cloud/apps/relay/src/host-session-client-accept.test.ts
  • cloud/apps/relay/src/host-session-registry.ts
  • cloud/apps/relay/src/relay-observability.test.ts
  • cloud/apps/relay/src/relay-observability.ts
  • cloud/infra/terraform/relay-observability.tf

Included review availability: Your plan provides up to 10 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The relay now records RTT samples only when a pong matches the outstanding control ping timestamp. Host sessions reset this state during initialization and rebind operations. RTT observations use a 1,024-entry reservoir with Algorithm R replacement. Flush output reports observed and dropped sample counts. Tests cover unmatched pongs, pong floods, reservoir limits, and empty flushes. Terraform adds the dropped-sample metric and updates the sample metric description.

Merge Risk: ⚪ Minimal · up to 32253

This change bounds relay RTT sampling and rejects unmatched pong timestamps while preserving liveness tracking. The updated metrics and Terraform descriptors align, with no current merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The description explains the change, rationale, testing, compatibility, security, performance, and required checklist items. However, the required Linked Issue section explicitly states “No issue,” al… Add the linked issue reference in the required “Linked Issue” section, using the format “Fixes #” or the repository-approved issue-link format.
✅ Passed checks (3 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly and concisely summarizes the primary change: bounding control RTT samples per ping and per flush window.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. (1 skipped: 1 unsupported.)

Full details: Description check

Explanation

The description explains the change, rationale, testing, compatibility, security, performance, and required checklist items. However, the required Linked Issue section explicitly states “No issue,” although the template requires an issue link.

  • Fix all pre-merge checks with AI
✨ 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.

@Jinwoo-H

Jinwoo-H commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

On the docstring-coverage pre-merge warning (33.33% against an 80% threshold): declining, with the description check now fixed above.

This repository's AGENTS.md sets the opposite rule for comments: "Concise/Brief Non-obvious Comments ONLY - DO NOT: be verbose, explain the obvious, walk through the code ('WHY not HOW'). BE CONCISE. 1 LINE if possible." Neither relay-observability.ts nor host-session-registry.ts carries a single JSDoc block today, so per-function docstrings would make this diff inconsistent with every function around it. The three functions the check analysed are a test-only key-scrubbing helper, its stage-bucket sub-helper, and a test-only ping helper. Each already carries a why-comment directly above it explaining the non-obvious part: which schema keys are exempt and why blanket string replacement was wrong, and that only the outstanding ping's t is timed. The two behaviour changes in production code, the outstanding-ping match and the reservoir, are likewise commented at the point of the decision rather than in a signature block.

@Jinwoo-H
Jinwoo-H merged commit 9c8f4c3 into main Sep 7, 2026
24 checks passed
ivanvykopal pushed a commit to ivanvykopal/orca that referenced this pull request Sep 7, 2026
…tablyai#19268)

* fix(relay): bound control RTT samples per ping and per flush window

An authenticated host chose how many round-trip samples a cell recorded: every
pong carrying a recent plausible `t` was forwarded to the process-wide window,
which grew unbounded until the 30s flush copied and sorted it for percentiles.

Time a pong only when it echoes the `t` of the ping still outstanding on that
session, so a flood yields at most one sample per ping the cell actually sent.
A pong that lost the race to the next ping is dropped for timing but still
counts as proof of life for the silence watchdog. Bound the process-wide window
with a 1024-sample reservoir (Algorithm R) so the percentiles stay unbiased,
keep `controlRttSamplesDelta` meaning round trips observed, and publish
`controlRttSamplesDroppedDelta` for the ones the reservoir did not keep.

Replace the leak guard's blanket `"credential":` string rewrite with an exact,
path-scoped rename of the two schema keys that spell a policed word, and make
the guard case-insensitive now that nothing legitimate trips it.

Follow-up to stablyai#19232.

* test(relay): prove the RTT reservoir samples the whole window
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