Skip to content

remote link: the direct bucket spends a token only on a socket that opened - #457

Merged
SSFSKIM merged 2 commits into
masterfrom
direct-token-spend
Nov 3, 2026
Merged

SSFSKIM merged 2 commits into
masterfrom
direct-token-spend

Conversation

@SSFSKIM

@SSFSKIM SSFSKIM commented Nov 3, 2026

Copy link
Copy Markdown
Owner

The bug

The direct carrier's token bucket was spent on dials that never reached the listener.

  • viaDirect (src/main/machines/remote/link.ts) ran bucket.take() before the direct dial. Every reconnect attempt during an outage spent a token, even when the dial was refused or timed out.
  • The listener charges a source only for an upgrade it accepts (src/endpoint/direct-listener.ts #upgrade; a 503 takes nothing). The two counts drifted apart in one direction: the link counted dials the listener never saw.
  • The bucket holds a burst of 3 and refills at 5 per minute, one token every 12 s (timing.ts, mirroring HANDSHAKE_LIMITS.perSource).

Measured

e2e:remote-latency on the Mac mini, on master:

run validity direct resume relay resume
41557747690 void (Rule B: load peaked at 27.5) 7441.9 ms 72.4 ms
41558674583 valid 7647.8 ms, over its 176.3 ms bound 69.5 ms

The arithmetic

The first up takes one token (2 left). After a loss, attempts come at 0, 1, 3, 7 and 12 s (BACKOFF_MS), each taking a token before it dials. The attempts at 0 and 1 s leave about 0.08 tokens, and later attempts skip direct until the bucket refills. When the network returns after a 6.9 s outage, the probe's attempt sees about 0.57 tokens. It skips direct, the hub is unreachable in this scene, and the link waits on the backoff (+1, +3, +7 s). Direct resumes at +7 s, when the bucket reaches 1.16 tokens: 7000 ms.

A unit reproduction gave 7000 ms after a 6.9 s outage, and 12000 ms after a 3.5 s or a 15 s outage (in the 15 s case, the 12 s attempt took the token that had just refilled).

Real-world cost: when the hub can't be reached (a LAN with no internet, or a hub outage), a returning network stalls for 0 to 12 s. When the hub can be reached, the session falls to the relay for up to 60 s (DIRECT_RECHECK_MS).

The fix

  • TokenBucket.holds() (timing.ts) refills the bucket and reports whether a whole token is there, without taking it. take() now runs through it.
  • viaDirect dials only when holds() is true, and calls take() only after raceDirect resolves with an opened socket. A refused dial, a timeout or a 503 spends nothing. A socket that opens and then fails its handshake (protocol, …) still spends a token, as the listener charged its upgrade.
  • The catch-up re-dial follows the same pattern: holds() before it dials, take() once its socket opens.
  • One attempt runs at a time (attempting guards both startAttempt and recheckDirect), so the token holds() saw is still there when the socket opens.
  • A lost 101 means the listener counted an upgrade the link didn't. At worst, the link's next dial is answered 503 and falls to the relay while the listener refills. A comment at the gate says so.
  • e2e/remote-latency.spec.ts's header said the forced drop restores both of A's ways to B. It restores only the case's own way, and the other only after the gap's turn has arrived. The header now says so.

Tests

All were written before the fix. The new resume and catch-up cases failed on master at expect(h.link.state()).toBe('up'), and the holds() case failed with bucket.holds is not a function.

  • link.test.ts › grace and backoff:
    • resumes over the direct carrier at once when the network returns after a 3500 / 6900 / 15000 ms outage with no hub: with direct and relay down, the channel ends and the clock runs for the outage. Then direct returns and a network probe follows. The link is up over direct in the probe's own attempt, and every attempt during the outage dialed direct first.
    • still holds the listener's limit: three opened direct handshakes that fail protocol inside a minute spend the burst. The fourth attempt, at 7 s, makes no direct dial and comes up over the hub. This case also passes on master; it guards against the fix overcorrecting.
    • spends no token on a catch-up connection that does not open: with a burst of 2 and almost no refill, a behind triggers a catch-up whose next connection is refused. The next probe still finds the remaining token and goes up over direct. The harness's fake handshake now dials again on behind, as connectChannel does.
  • timing.test.ts: says whether a whole token is there, refilled, without taking it.

Run locally, one file at a time: link.test.ts 49/49 and timing.test.ts 3/3, plus eslint on each changed file.

Gates

  • Static only: run 41560912470, success, 0 flaky lines.
  • Landing (--gates all --class1a pool --runner macos; the mini had no hold): run 41561328996.
    • Attempt 1: 51 green, including e2e:remote and e2e:machines, and 0 flaky lines.
    • The mini refused e2e:discover and e2e:render-host at its desktop check. A Claude Code auto-update's TCC dialog had focus from 01:41:05Z, before any test ran.
    • packaged 1/2 failed in e2e/dashboard.spec.ts:548 (the chart comment test), which this change doesn't reach.
    • Attempt 2 reran the three failed jobs, and all passed (gh run rerun --failed): packaged 1/2 98 passed, e2e:discover 9, e2e:render-host 2, with 0 flaky lines. The run's conclusion is success.
  • Master moved to 787aa90 (O7 M5c, PR P5 O7 M5c: O4's surfaces on another machine (leases, delivery, consent, the client port, dashboard holds) #454), which edits link.ts and link.test.ts in other functions. It merged cleanly as 530abe8. Locally, link.test.ts passed 56/56 and timing.test.ts 3/3.
    • Static only on the merge: run 41569386695, success, 0 flaky lines.
    • Class 1a plus e2e:remote and e2e:machines on the merge: run 41571726165, success. Every job passed (static, unit 1/4 to 4/4, probes, e2e:remote 1 passed, e2e:machines 2 passed), with 0 flaky lines.

Review

doperpowers:reviewer-medium on 01d4488cf..c50d7274a, through the lens "the direct carrier's rate limit stays bound to the listener's own count, and no path dials direct without holding a token": correct, with no material findings.

Sminos-Session: GUI AI agent workspace charter#37052769 (MAWS on macbook)

…pened (TokenBucket.holds gates the dial, take follows the open, the catch-up re-dial too), as the listener charges only an accepted upgrade; an outage's refused dials no longer empty it, so a returning network resumes direct at once instead of 7–12 s later; remote-latency's header says the drop restores only the case's own way

Sminos-Session: GUI AI agent workspace charter#37052769 (MAWS on macbook)
Sminos-Session: GUI AI agent workspace charter#37052769 (MAWS on macbook)
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-03T03:30:36.737629Z
We couldn’t complete this review. Comment @codex review to try again.
530abe8 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

SSFSKIM added a commit that referenced this pull request Nov 3, 2026
…g 208; the hosted row's signature (22) tallied

Sminos-Session: GUI AI agent workspace charter#37052769 (MAWS on macbook)
@SSFSKIM
SSFSKIM merged commit da7b8f1 into master Nov 3, 2026
SSFSKIM added a commit that referenced this pull request Nov 4, 2026
…rpowers #456, #457)

Sminos-Session: GUI AI agent workspace charter#37052769 (MAWS on macbook)
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