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

Migrated from github.com/Trevato/SO-ARM100

src/hops.ts: NTP-style clock offset and per-hop p50/p95 from timestamps - pure module and test only #483

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

I added two new files, Software/so101/src/hops.ts and hops.test.ts. Nothing else changes, so precision.ts and server.ts are untouched. hops.test.ts passes (10 tests) and tsc reports nothing for hops.

Notes

shipped merged

opened this

I added two new files, Software/so101/src/hops.ts and hops.test.ts. Nothing else changes, so precision.ts and server.ts are untouched. hops.test.ts passes (10 tests) and tsc reports nothing for hops. I did not run the rest of the suite; the forge runs the full check on this push.

The old smith/458-… branch wasn't reachable from this clone, so I wrote hops.ts fresh instead of borrowing from it.

  • exchangeSync: takes the four timestamps (t0 client send, t1 server receive, t2 server send, t3 client receive). It returns {offsetMs, delayMs} using the NTP formulas, with delay excluding the server's hold time. It returns null for non-finite timestamps or a negative delay.
  • bestSync: takes the offset from the lowest-delay exchange among the last N, with N defaulting to 8. It skips malformed exchanges and returns null if none are usable.
  • Hop: keeps a rolling window of durations, 256 by default, and reports nearest-rank p50 and p95 through stats(). Negative or non-finite samples make add return false and are counted in refused.
  • Tests: they use synthetic exchanges with a known skew and cover:

- asymmetric legs biasing the offset by half the leg difference; - the lowest-delay pick winning over exchanges with a worse delay; - the last-N cutoff; - the window dropping old samples; - refused samples being counted.

🤖 Generated with Claude Code

Closes #480

commented

I added two new files, Software/so101/src/hops.ts and hops.test.ts. Nothing else changes, so precision.ts and server.ts are untouched. hops.test.ts passes (10 tests) and tsc reports nothing for hops. I did not run the rest of the suite; the forge runs the full check on this push.

The old smith/458-… branch wasn't reachable from this clone, so I wrote hops.ts fresh instead of borrowing from it.

  • exchangeSync: takes the four timestamps (t0 client send, t1 server receive, t2 server send, t3 client receive). It returns {offsetMs, delayMs} using the NTP formulas, with delay excluding the server's hold time. It returns null for non-finite timestamps or a negative delay.
  • bestSync: takes the offset from the lowest-delay exchange among the last N, with N defaulting to 8. It skips malformed exchanges and returns null if none are usable.
  • Hop: keeps a rolling window of durations, 256 by default, and reports nearest-rank p50 and p95 through stats(). Negative or non-finite samples make add return false and are counted in refused.
  • Tests: they use synthetic exchanges with a known skew and cover:

- asymmetric legs biasing the offset by half the leg difference; - the lowest-delay pick winning over exchanges with a worse delay; - the last-N cutoff; - the window dropping old samples; - refused samples being counted.

🤖 Generated with Claude Code

Approved

Pure module plus tests, exactly the scope of the issue, no new dependency, no I/O, no surface to gate. I could not run the tests here: this clone does not contain the head files. I read the diff instead. The NTP formulas are right (offset = ((t1-t0)+(t2-t3))/2, delay excludes the server hold time), the asymmetry test checks the 260 ms bias by hand, and every branch has a test that would fail without it. Nothing here touches the page, tty or the preview, so there is no experience to break. Next two things: (1) make bestSync treat a non-finite n as the default, because slice(-NaN) silently uses every exchange; (2) when this is wired in, wire it through precision.ts and /live and expose it in src/tty.ts as well as the page, with the README saying how to read it.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs