Skip to content

fix(net): select scoped routes after filtering, not before - #4363

Merged
kixelated merged 4 commits into
mainfrom
fix/js-scoped-route-select
Sep 28, 2026
Merged

kixelated merged 4 commits into
mainfrom
fix/js-scoped-route-select

Conversation

@kixelated

@kixelated kixelated commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Follow-up to #4234, addressing the unanswered review comment Select scoped routes before collapsing each prefix.

The JS origin collapsed each prefix to its single cheapest route before a reader's scope was applied. When a cheap route scoped to */chat and a costlier one scoped to */video both advertised the same prefix, a reader scoped to room/video saw no announcement at all, even though requests for room/video routed through the video entry. The same early collapse hit:

  • the originated table a session publishes, so a scoped publish view, or a peer's SUBSCRIBE_NAMESPACE at room/video, never heard the video route;
  • an announced local broadcast that lost to a cheaper route outside the reader's scope;
  • a served root, where the narrowest covering route was kept even when its claim could not serve the requested prefix and a broader one could.

Rust moq-net already filters by the cursor's scope before picking the best entry (sync_cursor), so only JS was affected.

Approach

Keep every candidate per prefix, most preferred first, and let each reader take the first one its scope admits:

  • OriginState.candidates() builds the per-prefix lists (routes plus the local broadcast, local winning ties) once per mutation, shared by the snapshot and the originated table.
  • #listed, Scope.projectRoutes, and the wire presented() pick the first visible candidate. projectRoutes merges every prefix covering a root into the empty key, most specific first.
  • A route's claim is computed once at insert instead of per rebuild.

Impact

  • Public API: none. Origin.Producer.scope, broadcasts, and announced keep their signatures; scoped readers now see routes they previously missed.
  • Wire: none. Encoding is unchanged; a scoped session now announces the best route its scope allows instead of omitting the prefix.
  • Package-private: the wire view's advertised is now Advertisements (candidate lists per prefix) instead of one Advertised per prefix.

Validation

  • New JS tests fail before the fix: a scoped reader picks the best route its scope can see (map, stream, unscoped pattern, and wire view); a scoped reader sees a local broadcast that only loses to an out-of-scope route; an IETF subscription at room/video hears the costlier video route; a served root falls back to a broader covering route when the narrowest cannot serve the requested prefix.
  • Added a Rust parity test (scoped_cursor_selects_among_the_routes_it_can_see), which already passes.
  • just check passes.
  • js/net/bench/broadcasts.ts before and after: separate runs were too noisy to read (load average 25 to 60 on 32 cores; cells swung 0.6x to 3x both ways). An in-process A/B that alternates the two origins, 9 rounds with the median taken after warmup, shows scoped readers unchanged (0.97x to 1.00x) and the unscoped fast path within about 10% (128 routes, 32 observers: 174 µs to 188 µs per update). The snapshot still builds once per mutation, with one extra small array per prefix.

Announce fan-out

forwardAnnounced opens one announce stream per interest head, so a peer route above several heads lands in the table once per head and is repriced once per head. The new nightly benchmark js/net/bench/forward.ts sweeps heads (1, 4, 16) against received routes (8, 32, 128) and reprices either the covering route (fanned out to every head) or a single route under one head. Median-ish per-update cost on the loaded host:

heads routes covering single
1 128 ~0.2-1.0 ms ~0.35 ms
4 128 ~0.55 ms ~0.6-0.8 ms
16 8 ~0.4-0.9 ms ~0.04-0.3 ms
16 128 ~0.8-1.1 ms ~0.3-0.8 ms

The covering reprice grows with heads, but stays around a millisecond even at 16 heads and 128 routes. Real scopes usually have one to four literal heads. Per the quest, I left sharing received prefixes across streams out of this PR. When one stream closes, a request using its copy of the route re-resolves through the identical copy left on another stream. It sees a short gap while that copy accepts, but it is not stranded.

Completes and deletes quest/m1/js-scoped-routes.md.

(Written by Opus 5.5)

🤖 Generated with Claude Code

The JS origin collapsed each prefix to its cheapest route before applying a
reader's scope, so a cheap route scoped elsewhere hid a usable one at the same
prefix: from scoped announcement readers, scoped publish views, and peer
namespace subscriptions. Keep every candidate per prefix, most preferred
first, and let each reader take the first one it may see, matching Rust's
sync_cursor. Follow-up to #4234.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 33c0e645-07e4-4b05-84fb-c24b85cf9910

📥 Commits

Reviewing files that changed from the base of the PR and between 3d2a7bd and e30953b.

📒 Files selected for processing (13)
  • .github/workflows/nightly.yml
  • doc/concept/moq-lite.md
  • js/net/bench/forward.ts
  • js/net/src/ietf/publisher.test.ts
  • js/net/src/ietf/publisher.ts
  • js/net/src/internal.ts
  • js/net/src/lite/publisher.ts
  • js/net/src/origin.test.ts
  • js/net/src/origin.ts
  • js/net/src/wire.ts
  • quest/m1/README.md
  • quest/m1/js-scoped-routes.md
  • rs/moq-net/src/model/origin.rs
💤 Files with no reviewable changes (2)
  • quest/m1/README.md
  • quest/m1/js-scoped-routes.md

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


Walkthrough

Origin advertisements now retain ordered route candidates so readers can select the best route visible within their scope. Publisher paths and route projections consume the candidate lists. New tests cover scoped selection, and a benchmark measures route-update forwarding across interest-head and route counts. The nightly workflow runs the benchmark. The scoped-route quest entry and document were removed.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to e3095

No identified issue prevents merging after normal checks.

Security Architecture Review

Security architecture risk: 🟡 Moderate · up to e3095

Scoped readers should now receive routes they previously missed. Request handling still checks route scope, and no new unauthorized access path was established. The change affects several forwarding paths, however, and the behavior of every downstream announcement consumer has not been verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected scope is advertisement and forwarding behavior for readers and peers sharing a JS origin, rather than a demonstrated expansion of content-request authority.

Trust Boundaries and Controls

  • observed — A reader-controlled namespace can influence which advertisements are presented, but candidate filtering uses origin-held claims; serving a concrete path requires a matching route scope.

Resilience and Maintainability Implications

  • inferred — An overlapping announcement does not itself prove that its listed prefix can serve every descendant: announcements omit the selected claim, whereas concrete request selection checks scope. The downstream interpretation of these hints remains unverified.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main route-selection fix: applying scope filtering before selecting routes.
Description check ✅ Passed The description directly explains the scoped-route bug, the implementation approach, affected behavior, tests, benchmarks, and validation results.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 9 files. (2 skipped: 2 …
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.
✨ Finishing Touches
✨ Simplify code
  • Commit to this branch
  • Create a new PR

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

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 ✅ Completed 2026-09-28T19:16:27.536548Z e30953b New commits
ℹ️ 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.

@kixelated

Copy link
Copy Markdown
Collaborator Author

MERGE

This is a clear positive improvement. Collapsing each prefix to a single cheapest route in snapshot / rebuildOriginated before a reader's scope ran was a real correctness bug: a cheap */chat dynamic at "" could hide a usable */video route from scoped announcement readers, scoped publish views, and IETF SUBSCRIBE_NAMESPACE at room/video. Deferring selection until after scope filtering matches what Rust sync_cursor already does (cursor.visible then best among those), so JS and Rust behave the same for the same topology.

The complexity is justified and well contained. OriginState.candidates() builds preferred-first lists once per mutation (routes plus local, with compareCandidates preserving the old cost / local-on-tie / hops ordering), Consumer.#listed and Scope.projectRoutes take the first visible entry, and wire presented() picks the first candidate whose claim can serve the requested prefix—including the served-root case where a narrow covering claim must fall through to a broader one. Caching claim on RouteEntry at insert is a small win. The package-private Advertisements reshape (Map of candidate lists) is the right surface for that; public scope / broadcasts / announced stay unchanged, and encoding is unchanged.

No better approach stands out. Filtering inside a per-reader preferredEntry would re-collapse too early or rebuild per observer; keeping the full candidate list and selecting after filter is the straightforward fix and what the #4234 review asked for. Tests cover the failure modes that mattered (scoped map/stream/wire, local that only loses to an out-of-scope route, IETF subscribe at room/video, broader covering on a served root) and the Rust parity test documents the intended behavior. Ship it.

This is an automated review, not the maintainer's decision
(Written by Grok)

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d3303f0fcd

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread js/net/src/origin.ts
const above = relative === null || relative === Path.empty();
if (above && !Path.hasPrefix(path, this.root)) continue;
const visible = candidates
.filter((value) => !allowed || allowed.some((pattern) => advertOverlaps(value, path, pattern)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve exactness when filtering fallback candidates

When a cheap dynamic route scoped to room/chat and a costlier exact local broadcast at room share a prefix, a consumer scoped to room/video filters out the dynamic here but accepts the local candidate because advertOverlaps treats every claimless advertisement as covering a subtree. Retaining all candidates makes this a new fallback path: the publisher announces that room covers room/video, although local() and demand() cannot serve that path. Preserve the candidate's exact marker through Advertisements and require exact candidates to match the permitted path rather than merely overlap its subtree.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this here: the PR matches Rust, and the gap Codex describes is older than this PR.

Rust gives a local broadcast the claim subtree(prefix) (prefix_claim), and TableCursor::visible checks it by overlap. I ran a probe with a broadcast at room (cost 5) and a cheaper dynamic at room scoped to room/chat (cost 1). A consumer scoped to room/video announces room at cost 5, and a consumer rooted at room/video announces "" at cost 5. That is the same fallback this PR adds to JS.

JS projectRoutes also showed a lone local broadcast at room to a room/video scope before this PR, because a claimless advert is checked with scopeOverlaps. This PR only makes that reachable when a cheaper out-of-scope route shares the prefix.

Requiring exact candidates to match would make JS disagree with Rust. The open question is whether an exact broadcast above a scope should be visible to that scope at all. That covers both languages, plus the older JS split where #listed already uses pattern.matches for exact candidates. I'm leaving it as a follow-up.

(Written by Opus 5.5)

kixelated and others added 3 commits September 28, 2026 10:29
A route above several interest heads lands once per head. Measure its re-price
across heads and received routes in the nightly JS benchmarks, and complete the
js-scoped-routes quest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	quest/m1/README.md
# Conflicts:
#	quest/m1/README.md
@kixelated

kixelated commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Merge summary

  • Merged main in twice to fix quest/m1/README.md conflicts. In each, both sides had removed a different finished quest (js-pattern-depth, then play-decode-schedule), so both lines were dropped. No code changes.
  • Codex P2 "Preserve exactness when filtering fallback candidates": replied without changing the code. The PR matches Rust. A probe in moq-net confirmed that a room/video-scoped cursor announces an exact room broadcast after filtering out a cheaper dynamic at room scoped to room/chat. JS projectRoutes already showed a lone local broadcast above the scope before this PR. Whether exact broadcasts above a scope should be visible at all is a cross-language follow-up.
  • Checks: just check passed locally, CI (Check, Test, WASM, Android) is green, and Codex gave a thumbs up on e30953b.
  • The nightly benchmark in .github/workflows/nightly.yml was not dispatched on this branch.

(Written by Opus 5.5)

@kixelated
kixelated merged commit e1b70c2 into main Sep 28, 2026
6 checks passed
@kixelated
kixelated deleted the fix/js-scoped-route-select branch September 28, 2026 19:54
@moq-bot moq-bot Bot mentioned this pull request Sep 28, 2026
@moq-bot moq-bot Bot mentioned this pull request Sep 30, 2026
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