Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

/follow: size and rate checks #44

Parked opened this
Closed without merging

/follow now has a size limit and a rate limit, on both the arm server (src/server.ts) and the relay (src/relay.ts). The type check didn't run here: tsc isn't installed locally and bun x tsc failed on a read-only tempdir. The other tests I ran pass except one failure that isn't from this change.

Notes

stopped parked by the crew deploy 1be63a3

opened this

/follow now has a size limit and a rate limit, on both the arm server (src/server.ts) and the relay (src/relay.ts). The type check didn't run here: tsc isn't installed locally and bun x tsc failed on a read-only tempdir. The other tests I ran pass except one failure that isn't from this change.

  • Size: a frame over 1 KiB closes the socket with code 1009.
  • Rate: frames pass at up to 200 per second, with a burst of 50 after a quiet spell. Frames past that are dropped, and the arm server counts them as refused in the stream stats. Leaders send 50–100 frames a second, so normal driving isn't affected.
  • New files: the limits, a byte-size helper and the token bucket are in src/frame-limit.ts. src/frame-limit.test.ts covers the bucket and the 1009 close on the arm server.
  • maxPayload under Bun: I also set the ws maxPayload option on both servers, but Bun's ws doesn't enforce it, so the real size check is in the message handlers. The option only helps under Node.
  • What I ran: frame-limit, server, relay and lobby tests.
  • Pre-existing failure: one test in server.test.ts ("serves the model, its meshes and the vendored libraries…") fails with a 404. It fails the same way without my change.
  • Relay not covered: it has the same two checks, but no test sends it an oversized or too-fast stream.

Closes #36

7 earlier comments · 3 events
commented

/follow now has a size limit and a rate limit, on both the arm server (src/server.ts) and the relay (src/relay.ts). The type check didn't run here: tsc isn't installed locally and bun x tsc failed on a read-only tempdir. The other tests I ran pass except one failure that isn't from this change.

  • Size: a frame over 1 KiB closes the socket with code 1009.
  • Rate: frames pass at up to 200 per second, with a burst of 50 after a quiet spell. Frames past that are dropped, and the arm server counts them as refused in the stream stats. Leaders send 50–100 frames a second, so normal driving isn't affected.
  • New files: the limits, a byte-size helper and the token bucket are in src/frame-limit.ts. src/frame-limit.test.ts covers the bucket and the 1009 close on the arm server.
  • maxPayload under Bun: I also set the ws maxPayload option on both servers, but Bun's ws doesn't enforce it, so the real size check is in the message handlers. The option only helps under Node.
  • What I ran: frame-limit, server, relay and lobby tests.
  • Pre-existing failure: one test in server.test.ts ("serves the model, its meshes and the vendored libraries…") fails with a 404. It fails the same way without my change.
  • Relay not covered: it has the same two checks, but no test sends it an oversized or too-fast stream.
Changes requested — before the latest push

The limits are sensible and the arm-server path is tested, but two things block this. (1) Software/so101/metrics.jsonl is a runtime artifact committed by accident (a zeroed stream sample); remove it from the diff and keep runtime state under FORGE_APP_DATA / out of git. (2) The relay half of the change has no test: the size close (1009) and the rate drop in src/relay.ts would not fail any test if removed. Add a relay test that sends an oversized message and a burst past FRAME_BURST, and assert the close code and that surplus frames never reach the arm. Smaller: the relay drops rate-limited frames silently while the arm server counts them as refused; count them there too. The bucket is per socket, so a client opening many sockets multiplies its allowance; consider keying on the driver identity or socket address. The author also could not run the type check; please run bun x tsc --noEmit before pushing.

commented

I've addressed all of juniper's points and pushed to the same branch. tsc --noEmit is clean, and the relay, frame-limit, server and lobby tests pass (64 tests).

  • metrics.jsonl: removed from the diff, and metrics.jsonl is now in .gitignore. It came from the tests writing to the working directory.
  • Relay test: a new test in relay.test.ts runs the relay in front of a stand-in arm that records what reaches it.

- An oversized message closes with 1009, and nothing reaches the arm. - Two sockets from one address send 50 frames, then 40 more. The arm gets roughly the 50-frame burst plus a small refill, and the relay's dropped-frame count is above 20. - I did not assert that none of the 40 surplus frames get through, because the bucket refills while the test sends. Those frames can legitimately pass, so the test bounds the count instead.

  • Dropped-frame count: the relay now counts dropped frames, as server.ts does with stats.refused. It's exposed as droppedFrames on RunningRelay.
  • Per-socket allowance: the allowance is now shared across sockets. The key is the driver identity in the relay when there is a lobby, and the client address otherwise, including on the arm server. The allowance is released when the last socket for that key closes. This is a new FrameRates class in frame-limit.ts. The remote address is only a rough key when clients sit behind a shared proxy.
  • Type check: it needed BUN_TMPDIR pointed at $TMPDIR to install and run. It found a real error in FrameRate (a constructor parameter property that erasableSyntaxOnly rejects), which I fixed.
  • server.test.ts: the test that failed before ("serves the model, its meshes and the vendored libraries…") now passes, since node_modules is installed.
commented

I merged main into the branch, resolved the conflicts in relay.ts, relay.test.ts and server.ts, and pushed. The full bun test run passes (265 tests) and tsc --noEmit is clean.

  • server.ts: main rewrote the stream handler, so I kept its version and put my checks back in its message handler. Each frame is checked for size first, then against the rate allowance. Frames over MAX_FRAME_BYTES (1 KiB) close the socket with 1009, and frames over the allowance are dropped and counted as stats.refused. I replaced main's maxPayload: 4096 with MAX_FRAME_BYTES so the size limit is one number.
  • relay.ts: I kept both sides: my frame-limit import and the 1 KiB maxPayload, plus main's egress import and env setup.
  • relay.test.ts: the imports from both sides are kept.
  • Burst size: main's stream merges bursts down to the command rate, and its test sends 100 frames at once. My original burst of 50 dropped the latest frames, which broke that test and the dead-man hold test (the arm never received the final target). I raised FRAME_BURST from 50 to 200, and the sustained rate stays at 200 per second. I adjusted my relay test to send FRAME_BURST surplus frames, so it still checks that surplus frames are dropped and counted.
Changes requested — before the latest push

The size and rate checks are sound in shape and the relay is tested. Three things stop it landing. Fix first: the arm server's rate-limit drop path has no test (item 1).

  1. src/server.ts drops over-allowance frames and counts stats.refused, but no test sends it a fast stream. frame-limit.test.ts only covers the bucket in isolation and the 1009 close. Deleting the take() check in server.ts would leave every test green. Add a test that sends FRAME_BURST + N frames to /follow on startServer and asserts stats.refused > 0.
  1. The README does not mention the 1 KiB frame cap, the 200 frames/s allowance, close code 1009, or that surplus frames are dropped. Anyone writing a leader client needs this. Add a short paragraph under the /follow docs.
  1. The arm server keys its bucket on req.socket.remoteAddress. Behind the relay or the forge proxy every client shares one address, hence one bucket, so one noisy client starves the rest. The relay falls back to the address too when there is no lobby. Key on the verified identity where one exists, or state in the README that the limit is per upstream peer. Also, the size check runs after ws has buffered the whole message, since Bun ignores maxPayload. Say so in a comment at the check, so nobody believes memory is capped.

Looked at the diff only. I did not exercise the preview, since /follow is a WebSocket stream.

commented

I've addressed all three points and pushed to the branch. tsc --noEmit is clean and the frame-limit, server and relay tests pass (63 tests).

  1. Rate-drop test: a new test in frame-limit.test.ts sends FRAME_BURST * 2 frames to /follow on startServer. It then reads /status and asserts stream.refused is above FRAME_BURST / 2. Deleting the take() check leaves refused at 0, so that test fails.
  2. README: Software/README.md has a new paragraph under the /follow docs. It covers the 1 KiB frame cap, close code 1009, the 200 frames/s allowance with a burst of 200, surplus frames being dropped (not queued), and the refused count in /status.
  3. Keying and the buffering comment: I did not change the arm server's key, because it has no verified identity to use. It sees only the peer's address, so behind the relay it is one allowance for the relay's streams together. A comment in server.ts says this, and the README states that the limit there is per peer. The relay's per-driver bucket is what keeps one noisy client from starving the others. Both size checks, in server.ts and relay.ts, now carry a comment that Bun ignores maxPayload. So the check runs after ws has buffered the message, and it limits what is used, not what is held in memory.
Changes requested — before the latest push

Rate limiting and the 1 KiB cap are well shaped: the bucket keys on the driver identity or the socket address, never on a header. The tests pass here (frame-limit and relay, 14 tests). I could not reach the preview from my sandbox, so I judged from the source and tests, not from the running page.

Fix first: the size cap is not enforced on the bytes read. Both servers set maxPayload, but the pull says Bun ignores it, so the real check runs in the message handler after ws has buffered the whole message. One client can send a 1 GB frame and the server holds all of it before closing with 1009. The list requires a body cap by the bytes actually read. Enforce the cap while the data arrives, for example with Bun.serve websocket maxPayloadLength, or by counting bytes on the raw upgraded socket and destroying it past the limit. Keep the handler check as a second line. Add a test that fails if the cap is only post-hoc.

Second: the relay no-lobby key is req.socket.remoteAddress. Behind the forge proxy that is one shared address, so all anonymous drivers would share one 200/s allowance. Say in the README what the relay sees in the hosted case, or key on the verified identity there.

Third: the relay test asserts on timing (sleep 150 plus a refill margin of 10 frames). That will be flaky on a slow machine. Inject the clock or lower the rate for the test.

commented

Handed to a person by the crew: the reviewer asked for changes three times with no approval between, so the planner re-scopes it: split, replaced, or one more round with the reviewer's ask restated.

  1. juniper: The limits are sensible and the arm-server path is tested, but two things block this. (1) Software/so101/metrics.jsonl is a runtime artifact
  2. juniper: The size and rate checks are sound in shape and the relay is tested. Three things stop it landing. Fix first: the arm server's rate-limit dr
  3. juniper: Rate limiting and the 1 KiB cap are well shaped: the bucket keys on the driver identity or the socket address, never on a header. The tests
commented

I merged main into the branch and pushed. The only conflict was .gitignore, and I kept both sides: our metrics.jsonl entry and main's sandbox home-directory placeholder entries. relay.ts and Software/README.md merged without conflicts.

All 271 tests pass. tsc --noEmit reports one error, in src/admin-relay.test.ts line 101 ('after' is of type 'unknown'). That file is identical to origin/main, so the error comes from main, not this branch. I left it alone.

commented

I merged main into this branch and pushed. The type check is clean and all 293 tests pass (none failed). The forge's own check on the push hasn't reported yet.

main had changed how the arm server accepts WebSocket connections and added the reconnecting-link behaviour in the relay. I kept both sides in the three conflicting files:

  • server.ts: /follow now uses main's noServer upgrade routing, with this branch's MAX_FRAME_BYTES payload cap and per-address FrameRates. The req argument is still passed through to the connection handler, so the rate limit still sees the address.
  • relay.ts: bridge keeps the per-driver rate limiter and dropped-frame count from this branch. It also keeps main's real check, which stops frames being passed on while the link to the real arm is reconnecting.
  • relay.test.ts: both sides' tests are kept, along with the frame-limit import for the oversize and burst test.
commented

Closed by the crew: main moved under it 2 times and the branch could not be brought up to date. Conflicting files: Software/so101/src/relay.ts.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs