From e20b13d7b0fd00cbfb8ae411df2582c9bead86c3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 25 Sep 2026 18:42:59 -0700 Subject: [PATCH 1/3] chore(quest): claim js-origin-scope Co-Authored-By: GPT-6 From d06fd5eb82e733a720c161cf3f3445443732c648 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 25 Sep 2026 19:42:03 -0700 Subject: [PATCH 2/3] feat(net): scope origin announcement interest Co-Authored-By: GPT-6 --- doc/concept/moq-lite.md | 12 +- js/net/bench/broadcasts.ts | 13 +- js/net/src/connection/forward.test.ts | 97 ++++++++++- js/net/src/connection/forward.ts | 101 ++++++------ js/net/src/connection/scope.test.ts | 55 +++++++ js/net/src/origin.test.ts | 70 ++++++++ js/net/src/origin.ts | 224 ++++++++++++++++++++------ js/net/src/wire.ts | 4 + quest/m1/README.md | 1 - quest/m1/js-origin-scope.md | 27 ---- 10 files changed, 468 insertions(+), 136 deletions(-) create mode 100644 js/net/src/connection/scope.test.ts delete mode 100644 quest/m1/js-origin-scope.md diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index ce9e8118cfe..3c3f4e53453 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -135,8 +135,8 @@ A subscriber watching under a root sees advertisements named relative to that root. The pattern scope filters which prefixes are visible without changing a route's prefix. Announce events carry the covered path, captures, and what happened to it: Rust -`announce::Update { path, captures: Option>, route, kind }` and -TypeScript `Announce.Update { path, captures, route, kind }`, where the kind is +`announce::Update { prefix, captures: Option>, route, kind }` and +TypeScript `Announce.Update { prefix, captures, route, kind }`, where the kind is announced, updated (a reprice in place), or retracted. Captures are present when the announced prefix pins every wildcard in the most-specific matching scope member. The Rust consumer is a `Stream` and the TypeScript one an async iterable. @@ -145,7 +145,13 @@ Announcements are hints; requests are the authority. When a subscriber asks for a covered path the advertiser will not serve, the advertiser refuses that request rather than narrowing the claim, and no message narrows a route. Token scope is any pattern union; the session asks for each member's literal head on -the prefix-only wire and filters locally. +the prefix-only wire and filters locally. In Rust and TypeScript, +`origin.scope(root, patterns)` narrows the handle's permissions and presents paths +relative to `root`. Nested scopes intersect with their parent. A session receiving +into that scoped origin asks for the literal heads of its allowed patterns, +coalescing duplicate or nested heads. An unscoped origin still asks for the empty +prefix, covering every namespace. These subscriptions include hidden routes; +each local announcement reader decides whether to show them. ```typescript import { Path } from "@moq/net"; diff --git a/js/net/bench/broadcasts.ts b/js/net/bench/broadcasts.ts index 90dae38c0ca..903ab897624 100644 --- a/js/net/bench/broadcasts.ts +++ b/js/net/bench/broadcasts.ts @@ -8,18 +8,23 @@ const updates = 32; let checksum = 0; console.log("scope,routes,observers,update_us"); -for (const scope of ["unscoped", "distinct-scopes"] as const) { +for (const scope of ["unscoped", "distinct-scopes", "producer-scopes", "rooted-producers"] as const) { for (const routeCount of routeCounts) { for (const observerCount of observerCounts) { const origin = new Producer(); const handles = Array.from({ length: routeCount }, (_, index) => origin.dynamic(Path.from(`room/${index}`)), ); - const touched = Path.from("room/0"); + const touched = Path.from(scope === "rooted-producers" ? "0" : "room/0"); let notifications = 0; const disposes = Array.from({ length: observerCount }, (_, index) => - origin - .broadcasts(scope === "unscoped" ? undefined : Path.Pattern.parse(`room/**/tag-${index}`)) + (scope === "producer-scopes" + ? origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse(`room/**/tag-${index}`)])) + : scope === "rooted-producers" + ? origin.scope(Path.from("room"), new Path.Patterns([Path.Pattern.parse(`**/tag-${index}`)])) + : origin + ) + .broadcasts(scope === "distinct-scopes" ? Path.Pattern.parse(`room/**/tag-${index}`) : undefined) .subscribe((routes) => { if (routes.get(touched) === undefined) throw new Error("touched route disappeared"); notifications++; diff --git a/js/net/src/connection/forward.test.ts b/js/net/src/connection/forward.test.ts index d9d9b744817..f115c38ca48 100644 --- a/js/net/src/connection/forward.test.ts +++ b/js/net/src/connection/forward.test.ts @@ -39,7 +39,7 @@ class FakeSession { }); } - announced(): Announce.Consumer { + announced(_scope?: Path.Pattern, _options?: Announce.Options): Announce.Consumer { return this.announces.consume(); } @@ -188,3 +188,98 @@ test("a request replaced across one coalesced wakeup still gets answered", async second.close(); origin.close(); }); + +test("scoped origins subscribe to disjoint literal heads with hidden routes", () => { + const origin = new OriginProducer().scope( + Path.empty(), + new Path.Patterns(["room/*/chat", "room/nested/**", "other/**"].map(Path.Pattern.parse)), + ); + const session = new FakeSession(); + const interests: { scope: string | undefined; hidden: boolean | undefined }[] = []; + session.announced = (scope?: Path.Pattern, options?: Announce.Options) => { + interests.push({ scope: scope?.text, hidden: options?.hidden }); + return session.announces.consume(); + }; + forwardAnnounced(session.session, origin); + expect(interests).toEqual([ + { scope: "other/**", hidden: true }, + { scope: "room/**", hidden: true }, + ]); + session.die(); + origin.close(); +}); + +test("an unscoped origin retains its subscription to every namespace", () => { + const origin = new OriginProducer(); + const session = new FakeSession(); + const interests: string[] = []; + session.announced = (scope?: Path.Pattern) => { + interests.push(scope?.text ?? "**"); + return session.announces.consume(); + }; + forwardAnnounced(session.session, origin); + expect(interests).toEqual(["**"]); + session.die(); + origin.close(); +}); + +test("a scoped session filters announcements and blind requests under its root", async () => { + const origin = new OriginProducer(); + const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*/chat")])); + const session = new FakeSession(); + forwardAnnounced(session.session, scoped); + for (const prefix of ["room/alice/video", "room/alice/chat"]) { + session.announces.append({ + prefix: Path.from(prefix), + captures: undefined, + kind: "announced", + route: Route.default, + }); + } + const allowed = origin.request(Path.from("tenant/room/bob/chat")); + const denied = origin.request(Path.from("tenant/room/bob/video")); + await settle(); + expect([...origin.broadcasts().peek().keys()]).toEqual([Path.from("tenant/room/alice/chat")]); + expect(session.consumes(Path.from("room/bob/chat"))).toBe(1); + expect(session.consumes(Path.from("room/bob/video"))).toBe(0); + const routed = origin.request(Path.from("tenant/room/alice/chat")); + await settle(); + expect(session.consumes(Path.from("room/alice/chat"))).toBe(1); + expect(routed.active.peek()).toBeDefined(); + allowed.close(); + denied.close(); + routed.close(); + session.die(); + origin.close(); +}); + +test("one failed scoped interest leaves the other routes live until the session ends", async () => { + const origin = new OriginProducer().scope( + Path.empty(), + new Path.Patterns(["a/**", "b/**"].map(Path.Pattern.parse)), + ); + const session = new FakeSession(); + const sources = new Map(); + session.announced = (scope?: Path.Pattern) => { + const producer = new Announce.Producer(); + sources.set(scope?.text ?? "**", producer); + return producer.consume(); + }; + forwardAnnounced(session.session, origin); + for (const name of ["a", "b"]) { + sources + .get(`${name}/**`) + ?.append({ prefix: Path.from(name), captures: undefined, kind: "announced", route: Route.default }); + } + await settle(); + expect(origin.broadcasts().peek().size).toBe(2); + sources.get("a/**")?.close(new Error("namespace rejected")); + await settle(); + expect([...origin.broadcasts().peek().keys()]).toEqual([Path.from("b")]); + expect(origin.discovery.peek()).toBe(false); + session.die(); + await settle(); + expect(origin.broadcasts().peek().size).toBe(0); + expect(origin.discovery.peek()).toBeUndefined(); + origin.close(); +}); diff --git a/js/net/src/connection/forward.ts b/js/net/src/connection/forward.ts index 66745398a92..f594d65fa55 100644 --- a/js/net/src/connection/forward.ts +++ b/js/net/src/connection/forward.ts @@ -6,7 +6,7 @@ import { type Dispose, race } from "@moq/signals"; import { isActive } from "../announced.ts"; import type { Dynamic, Producer as OriginProducer, RequestSlot } from "../origin.ts"; -import type * as Path from "../path.ts"; +import * as Path from "../path.ts"; import { wireOf } from "../wire.ts"; import type { Established } from "./established.ts"; @@ -35,6 +35,7 @@ export function forwardAnnounced(conn: Established, origin: OriginProducer): voi let detach = originWire.attach(conn.discovery); let dead = false; + let discovering = conn.discovery; void conn.closed.then(() => { dead = true; detach(); @@ -47,57 +48,61 @@ export function forwardAnnounced(conn: Established, origin: OriginProducer): voi return; } - // Hidden routes are mirrored too; each local reader opts in on its own. - const announced = conn.announced(undefined, { hidden: true }); - const inserted = new Map(); - - // End the stream the moment the session closes rather than waiting for the wire to - // error it, so the retractions below land promptly. - void conn.closed.then(() => announced.close()); - - void (async () => { - let failure: unknown; - try { - for (;;) { - const event = await announced.next(); - if (!event) break; - - if (isActive(event.kind)) { - const existing = inserted.get(event.prefix); - if (existing) { - existing.update(event.route); + for (const prefix of originWire.interests()) { + // Hidden routes are mirrored too; each local reader opts in on its own. + const announced = conn.announced(Path.Pattern.subtree(prefix), { hidden: true }); + const inserted = new Map(); + + // End the stream the moment the session closes rather than waiting for the wire to + // error it, so the retractions below land promptly. + void conn.closed.then(() => announced.close()); + + void (async () => { + let failure: unknown; + try { + for (;;) { + const event = await announced.next(); + if (!event) break; + if (!originWire.accepts(event.prefix)) continue; + + if (isActive(event.kind)) { + const existing = inserted.get(event.prefix); + if (existing) { + existing.update(event.route); + } else { + const handle = originWire.receive(event.prefix, event.route); + inserted.set(event.prefix, handle); + void drive(handle, conn); + } } else { - const handle = originWire.receive(event.prefix, event.route); - inserted.set(event.prefix, handle); - void drive(handle, conn); + const handle = inserted.get(event.prefix); + inserted.delete(event.prefix); + handle?.close(); } - } else { - const handle = inserted.get(event.prefix); - inserted.delete(event.prefix); - handle?.close(); + } + } catch (err) { + // The session died mid-stream, or the relay refused or reset the stream. The + // cleanup below retracts everything this stream fed either way. + failure = err; + } finally { + for (const handle of inserted.values()) handle.close(); + inserted.clear(); + announced.close(); + + // Discovery ended while the session lives, and nothing reopens the stream on this + // connection. Downgrade the attachment rather than leaving the origin claiming a + // discovery that no longer works: announcement-gated consumers would wait forever + // on a table this session can no longer fill. Now they fall back to standing + // requests, which this session still answers. + if (!dead && discovering) { + discovering = false; + console.warn("broadcast discovery failed; broadcasts resolve on request only.", failure); + detach(); + detach = originWire.attach(false); } } - } catch (err) { - // The session died mid-stream, or the relay refused or reset the stream. The - // cleanup below retracts everything this stream fed either way. - failure = err; - } finally { - for (const handle of inserted.values()) handle.close(); - inserted.clear(); - announced.close(); - - // Discovery ended while the session lives, and nothing reopens the stream on this - // connection. Downgrade the attachment rather than leaving the origin claiming a - // discovery that no longer works: announcement-gated consumers would wait forever - // on a table this session can no longer fill. Now they fall back to standing - // requests, which this session still answers. - if (!dead) { - console.warn("broadcast discovery failed; broadcasts resolve on request only.", failure); - detach(); - detach = originWire.attach(false); - } - } - })(); + })(); + } } /** diff --git a/js/net/src/connection/scope.test.ts b/js/net/src/connection/scope.test.ts new file mode 100644 index 00000000000..01bf4ab52a1 --- /dev/null +++ b/js/net/src/connection/scope.test.ts @@ -0,0 +1,55 @@ +import { expect, test } from "bun:test"; +import * as Ietf from "../ietf/index.ts"; +import * as Lite from "../lite/index.ts"; +import { createMockTransportPair } from "../mock.ts"; +import { Producer } from "../origin.ts"; +import * as Path from "../path.ts"; +import { withTimeout } from "../util/timeout.ts"; +import { accept } from "./accept.ts"; +import { connect } from "./connect.ts"; + +for (const protocol of [Lite.ALPN_07_WIP, Ietf.ALPN.DRAFT_19]) { + test(`scoped origins discover multiple prefixes over ${protocol}`, async () => { + const pair = createMockTransportPair(protocol); + const url = new URL("https://localhost/test"); + const source = new Producer(); + const destination = new Producer(); + const publish = source.scope(Path.from("server"), new Path.Patterns([Path.Pattern.all()])); + const consume = destination.scope( + Path.from("client"), + new Path.Patterns(["room/**", "other/**"].map(Path.Pattern.parse)), + ); + const broadcasts = ["room/live", "room/.hidden", "other/live", "outside"].map((path) => { + const broadcast = publish.createBroadcast(Path.from(path)); + broadcast.announce(); + return broadcast; + }); + const [client, server] = await Promise.all([ + connect({ url, transport: pair.client, consume }), + accept({ url, transport: pair.server, publish: publish.consume() }), + ]); + const announced = destination.announced(undefined, { hidden: true }); + try { + const received: string[] = []; + for (let index = 0; index < 3; index++) { + const update = await withTimeout(announced.next(), 1000, "scoped announcement did not arrive"); + if (!update) throw new Error("announcement stream ended before replay"); + expect(update.kind).toBe("announced"); + received.push(update.prefix); + } + expect(received.sort()).toEqual(["client/other/live", "client/room/.hidden", "client/room/live"]); + expect([...consume.broadcasts().peek().keys()].sort()).toEqual([ + Path.from("other/live"), + Path.from("room/live"), + ]); + } finally { + announced.close(); + client.close(); + server.close(); + for (const broadcast of broadcasts) broadcast.close(); + source.close(); + destination.close(); + await Promise.all([client.closed, server.closed]); + } + }); +} diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index 47d044158e2..af57c5720b3 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -1361,3 +1361,73 @@ test("announced filters by arbitrary patterns and reports captures", async () => broadcast.close(); origin.close(); }); + +test("a rooted producer shares the table and enforces its pattern union", async () => { + const origin = new Producer(); + const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*")])); + const broadcast = publish(scoped, Path.from("room/alice")); + expect(origin.broadcasts().peek().has(Path.from("tenant/room/alice"))).toBe(true); + expect([...scoped.broadcasts().peek().keys()]).toEqual([Path.from("room/alice")]); + const request = scoped.consume().request(Path.from("room/alice")); + expect(request.path).toBe(Path.from("room/alice")); + expect(request.active.peek()).toBeDefined(); + expect(() => scoped.createBroadcast(Path.from("other"))).toThrow("outside the origin scope"); + expect(() => scoped.request(Path.from("room/alice/deep"))).toThrow("outside the origin scope"); + expect(() => scoped.dynamic(Path.from("other"))).toThrow("outside the origin scope"); + expect(() => scoped.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("other/**")]))).toThrow( + "do not overlap", + ); + expect(() => origin.scope(Path.empty(), new Path.Patterns())).toThrow("do not overlap"); + + const nested = scoped.scope(Path.from("room"), new Path.Patterns([Path.Pattern.all()])); + expect([...nested.broadcasts().peek().keys()]).toEqual([Path.from("alice")]); + expect(() => nested.createBroadcast(Path.from("alice/deep"))).toThrow("outside the origin scope"); + const announced = nested.announced(); + expect((await announced.next())?.prefix).toBe(Path.from("alice")); + announced.close(); + request.close(); + broadcast.close(); + origin.close(); +}); + +test("a scoped dynamic only serves allowed paths and presents relative requests", async () => { + const origin = new Producer(); + const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/*/chat")])); + const dynamic = scoped.dynamic(Path.from("room")); + expect(dynamic.prefix).toBe(Path.from("room")); + const denied = origin.request(Path.from("tenant/room/alice/video")); + expect(denied.unroutable.peek()).toBe(true); + const request = origin.request(Path.from("tenant/room/alice/chat")); + const requests = dynamic.requested(); + const pending = await requests.next(); + expect(pending.value?.path).toBe(Path.from("room/alice/chat")); + const broadcast = new BroadcastProducer(); + pending.value?.accept(broadcast); + expect(request.active.peek()).toBeDefined(); + dynamic.update({ cost: 2n }); + expect(origin.broadcasts().peek().get(Path.from("tenant/room"))?.cost.warm).toBe(2n); + await requests.return?.(); + dynamic.close(); + request.close(); + denied.close(); + broadcast.close(); + origin.close(); +}); + +test("scoped wire views filter and rebase advertisements and blind requests", () => { + const origin = new Producer(); + const scoped = origin.scope(Path.from("tenant"), new Path.Patterns([Path.Pattern.parse("room/**")])); + const local = publish(origin, Path.from("tenant/room/live")); + const hidden = publish(origin, Path.from("tenant/other/live")); + const allowed = origin.request(Path.from("tenant/room/missing")); + const denied = origin.request(Path.from("tenant/other/missing")); + expect([...(wireOf(scoped).requests.peek()?.keys() ?? [])]).toEqual([Path.from("room/missing")]); + expect([...(wireOf(scoped.consume()).advertised.peek()?.keys() ?? [])]).toEqual([Path.from("room/live")]); + expect(wireOf(scoped.consume()).local(Path.from("room/live"))).toBeDefined(); + expect(() => wireOf(scoped.consume()).local(Path.from("other/live"))).toThrow("outside the origin scope"); + allowed.close(); + denied.close(); + local.close(); + hidden.close(); + origin.close(); +}); diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index 28dd06e62dc..3ae7d90d7c0 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -22,6 +22,69 @@ import { type Advertised, registerWire, wireOf } from "./wire.ts"; export type { Cost, Hop, Route } from "./hop.ts"; export { isAnonymous } from "./hop.ts"; +/** The rooted permissions shared by a handle and every route it inserts. */ +class Scope { + static readonly all = new Scope(Path.empty()); + + readonly root: Path.Valid; + readonly allowed?: Path.Patterns; + + constructor(root: Path.Valid, allowed?: Path.Patterns) { + this.root = root; + this.allowed = allowed; + } + + narrow(root: Path.Valid, patterns: Path.Patterns): Scope { + const joined = Path.encode(Path.join(this.root, root)); + const rooted = patterns.rooted(joined); + const allowed = this.allowed?.intersect(rooted) ?? rooted; + if (allowed.size === 0) throw new Error("origin scopes do not overlap"); + return new Scope(joined, allowed); + } + + matches(path: Path.Valid): boolean { + return this.allowed?.matches(path) ?? true; + } + + path(path: Path.Valid): Path.Valid { + const joined = Path.encode(Path.join(this.root, path)); + if (!this.matches(joined)) throw new Error("path is outside the origin scope"); + return joined; + } + + prefix(prefix: Path.Valid): Path.Valid { + const joined = Path.encode(Path.join(this.root, prefix)); + if (this.allowed && !this.allowed.overlaps(Path.Pattern.subtree(joined))) { + throw new Error("prefix is outside the origin scope"); + } + return joined; + } + + patterns(pattern: Path.Pattern = Path.Pattern.all()): Path.Patterns { + const rooted = new Path.Patterns([pattern.rooted(this.root)]); + return this.allowed?.intersect(rooted) ?? rooted; + } + + /** Omit nested heads because the outer subscription already carries their routes. */ + heads(): Path.Valid[] { + if (!this.allowed) return [Path.empty()]; + const heads = [...new Set([...this.allowed.rebase(this.root)].map(scopeHead))].sort(); + return heads.filter((head) => !heads.some((other) => other !== head && Path.hasPrefix(other, head))); + } + + project(values: ReadonlyMap | undefined, exact: boolean): ReadonlyMap | undefined { + if (!values || this === Scope.all) return values; + const out = new Map(); + for (const [path, value] of values) { + if (exact ? !this.matches(path) : !this.allowed?.overlaps(Path.Pattern.subtree(path))) continue; + const relative = Path.stripPrefix(this.root, path); + if (relative !== null) out.set(relative, value); + else if (!exact && Path.hasPrefix(path, this.root)) out.set(Path.empty(), value); + } + return out; + } +} + /** * One requested path: the notify node for everything watching it. * @@ -62,6 +125,7 @@ export interface RequestSlot { */ export interface RouteEntry { readonly identity: object; + readonly scope: Scope; readonly route: Signal; readonly originated: boolean; readonly server?: ServeState; @@ -104,6 +168,12 @@ function noCapacity(): StreamError { /** A served route from {@link Producer.dynamic}: the queue a handler drains. */ class ServeState { + readonly root: Path.Valid; + + constructor(root: Path.Valid) { + this.root = root; + } + queue = new Signal([]); pending = new Map(); served = new Map(); @@ -123,7 +193,7 @@ class ServeState { if (this.pending.has(path)) return; const live = this.served.get(path); if (live && live.closed.peek() === undefined) return; - const request = makeRequest(path, this); + const request = makeRequest(Path.stripPrefix(this.root, path) ?? Path.empty(), this); this.pending.set(path, request); this.queue.mutate((queue) => { queue.push(request); @@ -131,33 +201,35 @@ class ServeState { } accept(request: Request, front: broadcast.Consumer): void { - if (this.closed.peek() !== undefined || this.pending.get(request.path) !== request) { + const path = Path.join(this.root, request.path); + if (this.closed.peek() !== undefined || this.pending.get(path) !== request) { front.close(); return; } - this.pending.delete(request.path); - const existing = this.served.get(request.path); + this.pending.delete(path); + const existing = this.served.get(path); if (existing && existing.closed.peek() === undefined) { front.close(); - this.onChange(request.path); + this.onChange(path); this.settled.update((n) => n + 1); return; } - this.served.set(request.path, front); + this.served.set(path, front); void front.closed.then(() => { - if (this.served.get(request.path) !== front) return; - this.served.delete(request.path); - this.onChange(request.path); + if (this.served.get(path) !== front) return; + this.served.delete(path); + this.onChange(path); }); - this.onChange(request.path); + this.onChange(path); this.settled.update((n) => n + 1); } reject(request: Request, err: Error): void { - if (this.pending.get(request.path) !== request) return; - this.pending.delete(request.path); - if (this.demanding.has(request.path)) this.rejected.set(request.path, err); - this.onReject(request.path); + const path = Path.join(this.root, request.path); + if (this.pending.get(path) !== request) return; + this.pending.delete(path); + if (this.demanding.has(path)) this.rejected.set(path, err); + this.onReject(path); this.settled.update((n) => n + 1); } @@ -375,7 +447,10 @@ class OriginState { let best: RouteEntry | undefined; for (const [prefix, entries] of this.routes.peek() ?? []) { if (!Path.hasPrefix(prefix, path)) continue; - const entry = preferredEntry(entries, skip); + const entry = preferredEntry( + entries, + (candidate) => !candidate.scope.matches(path) || (skip?.(candidate) ?? false), + ); if (!entry) continue; if (bestPrefix === undefined || prefix.length > bestPrefix.length) { bestPrefix = prefix; @@ -492,27 +567,47 @@ export interface RequestOptions { */ export class Producer implements Table { #state = new OriginState(); + #scope = Scope.all; + #requests?: Getter | undefined>; // The reader backing the passthroughs, so holding a Producer never requires the // consume().x() stutter for everyday reads. One instance, so `discovery` keeps its // identity across reads. - #reader = makeConsumer(this.#state); + #reader = makeConsumer(this.#state, this.#scope); constructor() { const thisProducer = this; registerWire(this, { receive: (prefix, route) => this.#receive(prefix, route), + interests: () => this.#scope.heads(), + accepts: (prefix) => + !this.#scope.allowed || + this.#scope.allowed.overlaps(Path.Pattern.subtree(Path.join(this.#scope.root, prefix))), attach: (discovery) => this.#attach(discovery), expect: () => this.#expect(), get requests() { - return thisProducer.#state.requests; + if (thisProducer.#scope === Scope.all) return thisProducer.#state.requests; + thisProducer.#requests ??= new Derived([thisProducer.#state.requests], (requests) => + thisProducer.#scope.project(requests, true), + ); + return thisProducer.#requests; }, changed: () => this.#changed(), - answer: (path, front) => this.#answer(path, front), + answer: (path, front) => this.#answer(this.#scope.path(path), front), routes: (path) => wireOf(this.#reader).routes(path), }); } + /** Narrow this handle to patterns beneath root, presenting paths relative to that root. */ + scope(root: Path.Valid, patterns: Path.Patterns): Producer { + const scope = this.#scope.narrow(root, patterns); + const producer = new Producer(); + producer.#state = this.#state; + producer.#scope = scope; + producer.#reader = makeConsumer(this.#state, scope); + return producer; + } + /** * Settles once the origin closes: `null` on a clean close, or the abort {@link Error}. * Peek it synchronously (`undefined` while open), observe it reactively, or `await` it. @@ -535,6 +630,7 @@ export class Producer implements Table { * a remote route at the same path on cost, winning ties. */ createBroadcast(path: Path.Valid): broadcast.Producer { + path = this.#scope.path(path); const created = this.#state.created; if (!created) throw new Error("origin is closed"); @@ -621,10 +717,12 @@ export class Producer implements Table { } #insertRoute(prefix: Path.Valid, route: Route, originated: boolean): Dynamic { - const server = new ServeState(); + prefix = this.#scope.prefix(prefix); + const server = new ServeState(this.#scope.root); server.onChange = (path) => this.#state.refresh(path); const entry: RouteEntry = { identity: {}, + scope: this.#scope, route: new Signal(route), originated, server, @@ -760,7 +858,7 @@ export class Producer implements Table { /** A read handle for this origin, the side a connection's `publish` option borrows. */ consume(): Consumer { - return makeConsumer(this.#state); + return makeConsumer(this.#state, this.#scope); } /** Whether every attached session announces into the table; see {@link Consumer.discovery}. */ @@ -821,7 +919,7 @@ export class Producer implements Table { // Constructs a Consumer from within this module without exposing a public constructor // that would leak the unexported OriginState. Assigned in the class's static block. -let makeConsumer: (state: OriginState) => Consumer; +let makeConsumer: (state: OriginState, scope: Scope) => Consumer; // Same for Requesting: a public constructor would let a caller forge a handle that no origin // ever registered, whose lifecycle guarantees are then false. `@internal` alone would not @@ -912,9 +1010,11 @@ export class Requesting { */ export class Consumer { #state: OriginState; + #scope: Scope; - private constructor(state: OriginState) { + private constructor(state: OriginState, scope: Scope) { this.#state = state; + this.#scope = scope; // True only when every attached session announces. One session that cannot means the // table is an incomplete picture, so a consumer gated on it has to keep its blind // fallback: the paths only that session carries never reach the table at all. @@ -922,20 +1022,20 @@ export class Consumer { total === 0 ? undefined : discovery === total, ); registerWire(this, { - routes: (path) => this.#routes(path), - get broadcasts() { - return state.local; - }, - get advertised() { - return state.originated; - }, - local: (path) => this.#local(path), - demand: (path) => this.#demand(path), + routes: (path) => this.#routes(scope.path(path)), + broadcasts: + scope === Scope.all ? state.local : new Derived([state.local], (local) => scope.project(local, true)), + advertised: + scope === Scope.all + ? state.originated + : new Derived([state.originated], (routes) => scope.project(routes, false)), + local: (path) => this.#local(scope.path(path)), + demand: (path) => this.#demand(scope.path(path)), }); } static { - makeConsumer = (state) => new Consumer(state); + makeConsumer = (state, scope) => new Consumer(state, scope); } /** Settles once the origin closes; see {@link Producer.closed}. */ @@ -997,10 +1097,17 @@ export class Consumer { * feeds from more than one connection. */ request(path: Path.Valid, options: RequestOptions = {}): Requesting { + const relative = path; + path = this.#scope.path(path); const requests = this.#state.requests.peek(); if (!requests) { // Closed origin: a request that can never resolve, and says so. - return makeRequesting(path, new Signal(undefined), getter(true), () => {}); + return makeRequesting( + relative, + new Signal(undefined), + getter(true), + () => {}, + ); } let slot = requests.get(path); @@ -1077,7 +1184,7 @@ export class Consumer { // answer. const unroutable = new Derived([route, this.#state.answerers], (front, answerers) => !front && answerers === 0); - return makeRequesting(path, active, unroutable, () => { + return makeRequesting(relative, active, unroutable, () => { // Releases this request's handle; the route itself belongs to the table. released = true; unsubscribeDiscovery(); @@ -1116,10 +1223,11 @@ export class Consumer { */ broadcasts(scope?: Path.Pattern, options?: announce.Options): Getter> { const hidden = options?.hidden ?? false; - if (!scope) return hidden ? this.#state.available : this.#state.visible; + if (!scope && this.#scope === Scope.all) return hidden ? this.#state.available : this.#state.visible; + const patterns = this.#scope.patterns(scope); return new Derived([this.#state.available], () => { const routes = new Map(); - for (const [path, entry] of this.#listed(scope, hidden)) routes.set(path, entry.route); + for (const [path, entry] of this.#listed(patterns, hidden)) routes.set(path, entry.route); return routes; }); } @@ -1134,29 +1242,41 @@ export class Consumer { */ announced(scope: Path.Pattern = Path.Pattern.all(), options?: announce.Options): announce.Consumer { const producer = new announce.Producer(); - void this.#runAnnounced(producer, scope, options?.hidden ?? false); + void this.#runAnnounced(producer, this.#scope.patterns(scope), options?.hidden ?? false); return producer.consume(); } /** One snapshot shared by map readers and announcement-stream diffing. */ - #listed(scope: Path.Pattern, hidden: boolean): Map { + #listed(patterns: Path.Patterns, hidden: boolean): Map { const next = new Map(); const { remote, local } = this.#state.snapshot(); - const head = scopeHead(scope); - for (const [path, entry] of remote) { - if (!scopeOverlaps(scope, path)) continue; - if (!hidden && hiddenBelow(head, path)) continue; - next.set(path, { identity: entry.identity, route: entry.route, captures: scopeCaptures(scope, path) }); - } - for (const [path, entry] of local) { - if (!scope.matches(path)) continue; - if (!hidden && hiddenBelow(head, path)) continue; - next.set(path, { identity: entry.identity, route: entry.route, captures: scopeCaptures(scope, path) }); + const scopes = [...patterns] + .sort((a, b) => Path.compareSpecificity(b.specificity(), a.specificity())) + .map((pattern) => ({ pattern, head: scopeHead(pattern) })); + for (const [table, exact] of [ + [remote, false], + [local, true], + ] as const) { + for (const [path, entry] of table) { + const scope = scopes.find( + ({ pattern, head }) => + (exact ? pattern.matches(path) : scopeOverlaps(pattern, path)) && + (hidden || !hiddenBelow(head, path)), + ); + if (!scope) continue; + const relative = this.#scope.root === Path.empty() ? path : Path.stripPrefix(this.#scope.root, path); + if (relative === null && !Path.hasPrefix(path, this.#scope.root)) continue; + next.set(relative ?? Path.empty(), { + identity: entry.identity, + route: entry.route, + captures: scopeCaptures(scope.pattern, path), + }); + } } return next; } - async #runAnnounced(producer: announce.Producer, scope: Path.Pattern, hidden: boolean): Promise { + async #runAnnounced(producer: announce.Producer, patterns: Path.Patterns, hidden: boolean): Promise { // Keyed by the presented path (from the origin, not the scope), valued by identity // plus route. Diffing identity rather than mere presence means a republish emits a // retraction then a fresh announcement; a re-price of the same identity emits an @@ -1170,7 +1290,7 @@ export class Consumer { const routes = this.#state.routes.peek(); if (local === undefined && advertisedLocal === undefined && routes === undefined) break; - const next = this.#listed(scope, hidden); + const next = this.#listed(patterns, hidden); for (const [path, snap] of active) { const cur = next.get(path); @@ -1293,7 +1413,7 @@ export class Dynamic { #closed = false; private constructor(prefix: Path.Valid, entry: RouteEntry, state: OriginState, retract: Dispose) { - this.prefix = prefix; + this.prefix = Path.stripPrefix(entry.scope.root, prefix) ?? Path.empty(); this.#entry = entry; this.#state = state; this.#retract = retract; @@ -1308,7 +1428,7 @@ export class Dynamic { if (this.#closed) throw new Error("dynamic is closed"); this.#entry.route.set(Route.normalize(route)); this.#state.rebuildOriginated(); - this.#state.refreshPrefix(this.prefix); + this.#state.refreshPrefix(Path.join(this.#entry.scope.root, this.prefix)); this.#state.routes.mutate(() => {}); } diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 3348b172bce..035695b91fa 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -25,6 +25,10 @@ export interface Broadcast { /** The protocol-facing operations behind an origin producer. */ export interface OriginProducer { + /** Minimal root-relative namespace prefixes that cover this origin scope. */ + interests(): readonly Path.Valid[]; + /** Whether an advertised prefix overlaps the handle's allowed paths. */ + accepts(prefix: Path.Valid): boolean; receive( prefix: Path.Valid, route?: Route | { hops?: Route["hops"]; cost?: Route["cost"] | bigint }, diff --git a/quest/m1/README.md b/quest/m1/README.md index c0f32703096..5be330b53a2 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -33,7 +33,6 @@ transport, benchmark tooling); worktrees isolate commits, not semantics. - [Broadcast close](/quest/m1/broadcast-close/README.md) - `close()` is the one way to end a broadcast in every language, a permanent retraction that leaves in-flight tracks alone - [Relay peer set](/quest/m1/relay-peer-set.md) - a wire consumer tells a client hop from a peer hop, and every mesh credential can mark a peer - [CLI inspection](/quest/m1/cli-inspect/README.md) - `moq ls` lists what is live and `moq fetch` reads a group over MoQ, and a guide shows how to inspect a relay -- [JS origin scope](/quest/m1/js-origin-scope.md) - a scoped `@moq/net` origin subscribes only to its allowed prefixes - [Session close](/quest/m1/session-close.md) - a graceful session end withdraws announces and waits one second for the ack - [JS caught up](/quest/m1/js-announce-caught-up.md) - @moq/net's announce consumer says when the initial set has landed, like Rust - [Bindings caught up](/quest/m1/announce-live-bindings.md) - moq-ffi, libmoq, and every wrapper yield the same flat announce event, `Live` included diff --git a/quest/m1/js-origin-scope.md b/quest/m1/js-origin-scope.md deleted file mode 100644 index 9bc8530c60d..00000000000 --- a/quest/m1/js-origin-scope.md +++ /dev/null @@ -1,27 +0,0 @@ -# [M] JS origin scope drives announce interest - -## Goal - -A `@moq/net` origin scoped to a prefix asks the peer only for that prefix. -An origin that was never scoped still asks for every namespace. A relay that -ignores an empty `SUBSCRIBE_NAMESPACE` then delivers a namespace the caller -actually named. - -## Plan - -`Producer.scope(root, patterns)` matches Rust `origin::Producer::scope`: the -root is stripped from paths, and the patterns are what the handle may publish -and subscribe under. `forwardAnnounced` sends one `SUBSCRIBE_NAMESPACE`, and -the moq-lite equivalent, per literal head of those patterns, the same rule as -Rust `interest_prefixes`. Hidden routes stay opted in on those subscriptions. - -An unscoped origin still allows `**`. Its head is the empty prefix, so the -session sends one subscription for everything, which is today's behavior. - -`doc/concept/moq-lite.md` says the prefixes a session asks for are the -origin's scope. No new page. - -## Related - -- [JS caught up](/quest/m1/js-announce-caught-up.md) - when the replay of those prefixes has landed -- [IETF announce count](/quest/m1/ietf-announce-count.md) - how an IETF consumer knows that replay is finished From 0f61bdaeb10fb5dc254fed56c876f502d93b412e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 26 Sep 2026 07:41:45 -0700 Subject: [PATCH 3/3] fix(net): filter scoped routes by their origin scope and prefer the most specific cover A route inserted through a scoped producer now carries its claim, so readers whose scope it cannot serve no longer see it, locally or on the wire. Prefixes collapsing onto a rooted view's root present the most specific one, matching the route requests resolve through. Both mirror moq-net. Co-Authored-By: Claude Opus 5.5 --- js/net/src/origin.test.ts | 25 +++++++++++++ js/net/src/origin.ts | 75 ++++++++++++++++++++++++++++++++------- js/net/src/wire.ts | 2 ++ 3 files changed, 90 insertions(+), 12 deletions(-) diff --git a/js/net/src/origin.test.ts b/js/net/src/origin.test.ts index af57c5720b3..3062bbdb706 100644 --- a/js/net/src/origin.test.ts +++ b/js/net/src/origin.test.ts @@ -1431,3 +1431,28 @@ test("scoped wire views filter and rebase advertisements and blind requests", () hidden.close(); origin.close(); }); + +test("a scoped dynamic is announced only to readers its scope can serve", () => { + const origin = new Producer(); + const dynamic = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("*/chat")])).dynamic(Path.empty()); + const chat = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/chat")])); + const video = origin.scope(Path.empty(), new Path.Patterns([Path.Pattern.parse("room/video")])); + expect([...chat.broadcasts().peek().keys()]).toEqual([Path.empty()]); + expect([...video.broadcasts().peek().keys()]).toEqual([]); + expect([...(wireOf(chat.consume()).advertised.peek()?.keys() ?? [])]).toEqual([Path.empty()]); + expect([...(wireOf(video.consume()).advertised.peek()?.keys() ?? [])]).toEqual([]); + dynamic.close(); + origin.close(); +}); + +test("a rooted reader presents the most specific covering route", () => { + const origin = new Producer(); + const narrow = origin.dynamic(Path.from("room/alice"), { cost: 9n }); + const broad = origin.dynamic(Path.from("room"), { cost: 1n }); + const rooted = origin.scope(Path.from("room/alice"), new Path.Patterns([Path.Pattern.all()])); + expect(rooted.broadcasts().peek().get(Path.empty())?.cost.warm).toBe(9n); + expect(wireOf(rooted.consume()).advertised.peek()?.get(Path.empty())?.route.cost.warm).toBe(9n); + broad.close(); + narrow.close(); + origin.close(); +}); diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index 3ae7d90d7c0..27f8226cc51 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -72,17 +72,66 @@ class Scope { return heads.filter((head) => !heads.some((other) => other !== head && Path.hasPrefix(other, head))); } - project(values: ReadonlyMap | undefined, exact: boolean): ReadonlyMap | undefined { + /** The exact paths within this scope, relative to its root. */ + projectPaths(values: ReadonlyMap | undefined): ReadonlyMap | undefined { if (!values || this === Scope.all) return values; const out = new Map(); for (const [path, value] of values) { - if (exact ? !this.matches(path) : !this.allowed?.overlaps(Path.Pattern.subtree(path))) continue; + if (!this.matches(path)) continue; const relative = Path.stripPrefix(this.root, path); if (relative !== null) out.set(relative, value); - else if (!exact && Path.hasPrefix(path, this.root)) out.set(Path.empty(), value); } return out; } + + /** The advertised prefixes that may serve this scope, relative to its root. */ + projectRoutes( + values: ReadonlyMap | undefined, + ): ReadonlyMap | undefined { + if (!values || this === Scope.all) return values; + const out = new Map(); + const covering = new CoveringRoot(this.root); + for (const [path, value] of values) { + if (this.allowed && ![...this.allowed].some((pattern) => advertOverlaps(value, path, pattern))) continue; + const relative = covering.relative(path); + if (relative !== undefined) out.set(relative, value); + } + return out; + } +} + +/** Whether the route advertised at `prefix` may serve any path `pattern` admits. */ +function advertOverlaps(advert: Advertised, prefix: Path.Valid, pattern: Path.Pattern): boolean { + return advert.claim ? advert.claim.overlaps(pattern) : scopeOverlaps(pattern, prefix); +} + +/** The paths `entry` may serve beneath `prefix`, when its producer is scoped. */ +function claimOf(entry: RouteEntry, prefix: Path.Valid): Path.Patterns | undefined { + return entry.scope.allowed?.intersect(new Path.Patterns([Path.Pattern.subtree(prefix)])); +} + +/** + * Presents advertised prefixes relative to a root. Every prefix at or above the root + * collapses to the empty path, where the most specific one wins, since that is the route + * a request beneath the root resolves through. + */ +class CoveringRoot { + readonly #root: Path.Valid; + #covering?: Path.Valid; + + constructor(root: Path.Valid) { + this.#root = root; + } + + /** The presented path for `path`, or undefined when it is outside the root or a broader cover. */ + relative(path: Path.Valid): Path.Valid | undefined { + const relative = Path.stripPrefix(this.#root, path); + if (relative !== null && relative !== Path.empty()) return relative; + if (relative === null && !Path.hasPrefix(path, this.#root)) return undefined; + if (this.#covering !== undefined && !Path.hasPrefix(this.#covering, path)) return undefined; + this.#covering = path; + return Path.empty(); + } } /** @@ -315,7 +364,7 @@ class OriginState { for (const [path, routes] of this.routes.peek() ?? []) { const entry = preferredEntry(routes); if (!entry) continue; - const value = { identity: entry.identity, route: entry.route.peek() }; + const value = { identity: entry.identity, route: entry.route.peek(), claim: claimOf(entry, path) }; remote.set(path, value); available.set(path, value.route); } @@ -416,7 +465,8 @@ class OriginState { const next = new Map(); for (const [prefix, entries] of routes ?? []) { const mine = preferredEntry(entries, received); - if (mine) next.set(prefix, { identity: mine.identity, route: mine.route.peek() }); + if (mine) + next.set(prefix, { identity: mine.identity, route: mine.route.peek(), claim: claimOf(mine, prefix) }); } // A local broadcast and an originated dynamic at one path compete on cost, as they do for requests. for (const [path, route] of advertised ?? []) { @@ -588,7 +638,7 @@ export class Producer implements Table { get requests() { if (thisProducer.#scope === Scope.all) return thisProducer.#state.requests; thisProducer.#requests ??= new Derived([thisProducer.#state.requests], (requests) => - thisProducer.#scope.project(requests, true), + thisProducer.#scope.projectPaths(requests), ); return thisProducer.#requests; }, @@ -1024,11 +1074,11 @@ export class Consumer { registerWire(this, { routes: (path) => this.#routes(scope.path(path)), broadcasts: - scope === Scope.all ? state.local : new Derived([state.local], (local) => scope.project(local, true)), + scope === Scope.all ? state.local : new Derived([state.local], (local) => scope.projectPaths(local)), advertised: scope === Scope.all ? state.originated - : new Derived([state.originated], (routes) => scope.project(routes, false)), + : new Derived([state.originated], (routes) => scope.projectRoutes(routes)), local: (path) => this.#local(scope.path(path)), demand: (path) => this.#demand(scope.path(path)), }); @@ -1249,6 +1299,7 @@ export class Consumer { /** One snapshot shared by map readers and announcement-stream diffing. */ #listed(patterns: Path.Patterns, hidden: boolean): Map { const next = new Map(); + const covering = new CoveringRoot(this.#scope.root); const { remote, local } = this.#state.snapshot(); const scopes = [...patterns] .sort((a, b) => Path.compareSpecificity(b.specificity(), a.specificity())) @@ -1260,13 +1311,13 @@ export class Consumer { for (const [path, entry] of table) { const scope = scopes.find( ({ pattern, head }) => - (exact ? pattern.matches(path) : scopeOverlaps(pattern, path)) && + (exact ? pattern.matches(path) : advertOverlaps(entry, path, pattern)) && (hidden || !hiddenBelow(head, path)), ); if (!scope) continue; - const relative = this.#scope.root === Path.empty() ? path : Path.stripPrefix(this.#scope.root, path); - if (relative === null && !Path.hasPrefix(path, this.#scope.root)) continue; - next.set(relative ?? Path.empty(), { + const relative = covering.relative(path); + if (relative === undefined) continue; + next.set(relative, { identity: entry.identity, route: entry.route, captures: scopeCaptures(scope.pattern, path), diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 035695b91fa..1df7e62458b 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -55,6 +55,8 @@ export interface OriginConsumer { export interface Advertised { readonly identity: object; readonly route: Route; + /** The absolute paths a scoped route may serve beneath its prefix; unset for the whole subtree. */ + readonly claim?: Path.Patterns; } /** The protocol-facing operation behind an established session. */