From 4d7412df3e193214486c945957f1948a6f310a32 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 09:33:38 -0700 Subject: [PATCH 1/5] quest: claim quest/m0/wildcard/js-origin.md Co-Authored-By: Claude Opus 5.5 From 9262272c3d4efef269f594791f7fbe917b2ef3f0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 09:55:01 -0700 Subject: [PATCH 2/5] fix(net): track a reply's origin per track copy and fetched group in JS Match Rust's track::Provenance: the origin SUBSCRIBE_START names lives on the track copy, FETCH_OK's on the fetched group, and a copy whose reply names a second origin is dropped instead of relabeled. The lite publisher names the origin of the content it actually serves. Co-Authored-By: Claude Opus 5.5 --- js/net/src/broadcast.ts | 25 +++--- js/net/src/internal.ts | 28 ++++++- js/net/src/lite/origin.test.ts | 126 +++++++++++++++++++++++++++-- js/net/src/lite/publisher.ts | 8 +- js/net/src/lite/subscriber.test.ts | 17 ++-- js/net/src/lite/subscriber.ts | 41 +++++----- js/net/src/track.ts | 5 ++ js/net/src/wire.ts | 9 +-- quest/m0/wildcard/README.md | 1 - quest/m0/wildcard/js-origin.md | 35 -------- 10 files changed, 197 insertions(+), 98 deletions(-) delete mode 100644 quest/m0/wildcard/js-origin.md diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index 8f78e993a9b..592bf935898 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -7,7 +7,7 @@ import { type GetPromise, Once, Signal } from "@moq/signals"; import { NotFound } from "./error.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; import { type Hop, Route, randomHop } from "./hop.ts"; -import { hooks, type TrackSequence } from "./internal.ts"; +import { hooks, namedOrigin, type TrackSequence } from "./internal.ts"; import * as Path from "./path.ts"; import * as track from "./track.ts"; import { untilAborted } from "./util/abort.ts"; @@ -34,15 +34,16 @@ class BroadcastState { // Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast // closes once the last one closes, so a shared consumer can be handed to several callers. consumers = 0; - // The origin serving this broadcast (see the wire's `origin`): named by an upstream - // reply, or generated on first use for content nobody named. + // The origin named for content no upstream reply named, generated on first use. origin?: Hop; } -// The origin a peer is told serves this broadcast: proxied from upstream, or a random -// one for content originating here, stable for the broadcast's life and shared by every -// session serving it. -function origin(state: BroadcastState): Hop { +// The origin a peer is told serves `content`: the one an upstream reply named for that track +// copy or fetched group, or else a random one for content originating here, stable for the +// broadcast's life and shared by every session serving it. +function origin(state: BroadcastState, content: track.Subscriber | GroupConsumer): Hop { + const named = content instanceof track.Subscriber ? hooks.trackOrigin(content) : namedOrigin(content); + if (named !== undefined) return named; state.origin ??= randomHop(); return state.origin; } @@ -258,10 +259,7 @@ export class Producer { resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), - origin: () => origin(this.#state), - name: (named) => { - this.#state.origin = named; - }, + origin: (content) => origin(this.#state, content), }; } @@ -335,10 +333,7 @@ export class Consumer { resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), - origin: () => origin(this.#state), - name: (named) => { - this.#state.origin = named; - }, + origin: (content) => origin(this.#state, content), }); } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index e809fe0e98c..08d0759e6e3 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -7,8 +7,8 @@ */ import type { Dispose, Getter } from "@moq/signals"; import type { Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; -import type { Frame, Consumer as GroupConsumer } from "./group.ts"; -import type { Route } from "./hop.ts"; +import type { Frame, Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; +import type { Hop, Route } from "./hop.ts"; import * as Path from "./path.ts"; import type { Timestamp } from "./time.ts"; import type { Groups, Producer, Request, Subscriber } from "./track.ts"; @@ -160,6 +160,8 @@ export const hooks: { * `setGroups`, which never rewinds. */ replaceGroups: (subscriber: Subscriber, groups: Groups) => void; + /** The origin an upstream reply named for the track copy a subscriber reads, if any. */ + trackOrigin: (subscriber: Subscriber) => Hop | undefined; /** Return a group's first timestamp, retained even after its first frame is read. */ groupTimestamp: (group: GroupConsumer) => Timestamp | undefined; groupLatest: (group: GroupConsumer) => Timestamp | undefined; @@ -200,6 +202,9 @@ export const hooks: { replaceGroups: () => { throw new Error("track.ts not loaded"); }, + trackOrigin: () => { + throw new Error("track.ts not loaded"); + }, groupTimestamp: () => { throw new Error("group.ts not loaded"); }, @@ -226,6 +231,25 @@ export const hooks: { }, }; +// Weak, so a label lives exactly as long as the content it names. +const origins = new WeakMap(); + +/** + * Record the origin an upstream reply named for `content`: a track copy's producer, or a + * fetched group. Content has one origin for its whole life, so a reply naming another means + * upstream is serving something else. That throws, and the caller drops the content. + */ +export function nameOrigin(content: Producer | GroupProducer | GroupConsumer, origin: Hop): void { + const named = origins.get(content); + if (named === undefined) origins.set(content, origin); + else if (named !== origin) throw new Error(`origin changed: ${named} to ${origin}`); +} + +/** The origin an upstream reply named for `content`, if any. */ +export function namedOrigin(content: Producer | GroupProducer | GroupConsumer): Hop | undefined { + return origins.get(content); +} + /** * Spreads equal routes across paths: FNV-1a 64 of `path` then each hop, oldest first, as 8 * little-endian bytes. Keyed on the requested path so an equal-cost pool advertising one diff --git a/js/net/src/lite/origin.test.ts b/js/net/src/lite/origin.test.ts index 6518cb44ef7..3a19fbfa970 100644 --- a/js/net/src/lite/origin.test.ts +++ b/js/net/src/lite/origin.test.ts @@ -1,10 +1,11 @@ import { expect, test } from "bun:test"; import * as broadcast from "../broadcast.ts"; -import { HopSchema, randomHop, UNKNOWN_HOP } from "../hop.ts"; +import { type Hop, HopSchema, randomHop, UNKNOWN_HOP } from "../hop.ts"; import { createMockTransportPair } from "../mock.ts"; import * as Path from "../path.ts"; import { Reader, Stream } from "../stream.ts"; import { wireOf } from "../wire.ts"; +import { Fetch, FetchOk } from "./fetch.ts"; import { Group as GroupMessage } from "./group.ts"; import { StreamId } from "./stream.ts"; import { encodeSubscribeResponse, Subscribe, SubscribeStart } from "./subscribe.ts"; @@ -27,14 +28,123 @@ async function settlesWithin(promise: Promise, ms: number): Promise { const producer = new broadcast.Producer(); - const origin = wireOf(producer).origin(); + producer.createTrack("video"); + producer.createTrack("audio"); + + const origin = served(producer, "video"); expect(origin).not.toBe(UNKNOWN_HOP); - expect(wireOf(producer).origin()).toBe(origin); - // Every handle, and so every session serving it, names the same one. - expect(wireOf(producer.consume()).origin()).toBe(origin); - expect(wireOf(new broadcast.Producer()).origin()).not.toBe(origin); + expect(served(producer, "video")).toBe(origin); + // Every track and every handle, and so every session serving it, names the same one. + expect(served(producer, "audio")).toBe(origin); + expect(served(producer.consume(), "video")).toBe(origin); + + const other = new broadcast.Producer(); + other.createTrack("video"); + expect(served(other, "video")).not.toBe(origin); +}); + +// Plays the upstream publisher on a mock draft-07 session, answering the subscriber's streams. +class Upstream { + readonly pair = createMockTransportPair(ALPN_07_WIP); + readonly subscriber = new Subscriber(this.pair.client, VERSION, randomHop()); + + // Answer a TRACK stream, then accept the SUBSCRIBE or FETCH stream that follows it. + async accept(): Promise { + const info = await Stream.accept(this.pair.server); + if (!info) throw new Error("the subscriber never asked for TRACK_INFO"); + expect(await info.reader.u53()).toBe(StreamId.Track); + await TrackMessage.decode(info.reader, VERSION); + await new TrackInfo({ maxAge: 60_000 }).encode(info.writer, VERSION); + info.close(); + + const stream = await Stream.accept(this.pair.server); + if (!stream) throw new Error("the subscriber never requested the track"); + return stream; + } + + // Accept a SUBSCRIBE and start it at group 0, naming `origin`. + async subscribe(origin: Hop): Promise { + const sub = await this.accept(); + expect(await sub.reader.u53()).toBe(StreamId.Subscribe); + await Subscribe.decode(sub.reader, VERSION); + await this.start(sub, origin); + return sub; + } + + async start(sub: Stream, origin: Hop): Promise { + await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, origin) }, VERSION); + } + + // Accept a FETCH and answer it with an empty group, naming `origin`. + async fetch(origin: Hop): Promise { + const stream = await this.accept(); + expect(await stream.reader.u53()).toBe(StreamId.Fetch); + await Fetch.decode(stream.reader, VERSION); + await new FetchOk(origin).encode(stream.writer, VERSION); + stream.close(); + } +} + +// Resolves once `check` holds, polling across macrotasks for the subscriber to read a reply. +async function until(check: () => boolean): Promise { + for (let i = 0; i < 100 && !check(); i++) await new Promise((resolve) => setTimeout(resolve, 1)); + expect(check()).toBe(true); +} + +// JS used to keep one origin per broadcast, so the latest reply relabeled content an earlier +// one named. Each track copy and each fetched group now keeps the origin its own reply named. +test("tracks and fetched groups of one broadcast keep the origins their own replies named", async () => { + const upstream = new Upstream(); + const consumer = upstream.subscriber.consume(Path.from("room")); + const video = HopSchema.parse(42n); + const audio = HopSchema.parse(43n); + const archive = HopSchema.parse(44n); + + // A republishing session serves a subscription of its own off each cached copy. + const videoReader = consumer.track("video").subscribe({}); + await upstream.subscribe(video); + await until(() => served(consumer, "video") === video); + const audioReader = consumer.track("audio").subscribe({}); + await upstream.subscribe(audio); + await until(() => served(consumer, "audio") === audio); + + const fetched = consumer.track("video").fetchGroup(0); + await upstream.fetch(archive); + const group = await fetched; + + expect(wireOf(consumer).origin(group)).toBe(archive); + expect(served(consumer, "video")).toBe(video); + expect(served(consumer, "audio")).toBe(audio); + expect(videoReader.closed.peek()).toBeUndefined(); + expect(audioReader.closed.peek()).toBeUndefined(); + + upstream.subscriber.close(); +}); + +// Mirrors Rust's track::Provenance: a copy has one origin, so a reply naming another means +// upstream is serving a different track. The copy is dropped rather than relabeled. +test("a SUBSCRIBE_START naming a second origin drops the copy", async () => { + const upstream = new Upstream(); + const consumer = upstream.subscriber.consume(Path.from("room")); + const origin = HopSchema.parse(42n); + + const reader = consumer.track("video").subscribe({}); + const sub = await upstream.subscribe(origin); + await until(() => served(consumer, "video") === origin); + + await upstream.start(sub, HopSchema.parse(43n)); + const closed = await reader.closed; + expect(closed).toBeInstanceOf(Error); + expect((closed as Error).message).toContain("origin changed"); + + upstream.subscriber.close(); }); test("a draft-07 group waits for SUBSCRIBE_START, whose origin the broadcast then names", async () => { @@ -71,6 +181,6 @@ test("a draft-07 group waits for SUBSCRIBE_START, whose origin the broadcast the expect(group?.sequence).toBe(0); expect(await group?.readString()).toBe("x"); - // Republishing the broadcast proxies the origin upstream named. - expect(wireOf(consumer).origin()).toBe(origin); + // Republishing the track proxies the origin upstream named. + expect(wireOf(consumer).origin(reader)).toBe(origin); }); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 94fc5a46a20..77933afea4d 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -670,9 +670,9 @@ export class Publisher { start: { included: sequence }, end: bounds.endGroup === undefined ? undefined : { included: bounds.endGroup }, }); - // Read once content flowed: an upstream's SUBSCRIBE_START named the origin - // before any of its content did. - const start = new SubscribeStart(sequence, wireOf(front).origin()); + // Read once content flowed: an upstream's SUBSCRIBE_START named the track + // copy's origin before any of its content did. + const start = new SubscribeStart(sequence, wireOf(front).origin(track)); await encodeSubscribeResponse(stream.writer, { start }, this.version); }); @@ -742,7 +742,7 @@ export class Publisher { const info = await this.#resolveTrackInfo(front, msg.track); group = await wireOf(front).fetchGroup(msg.track, msg.group, { priority: msg.priority }); if (hasOrigin(this.version)) { - await new FetchOk(wireOf(front).origin()).encode(stream.writer, this.version); + await new FetchOk(wireOf(front).origin(group)).encode(stream.writer, this.version); } await this.#runFetchGroup(group, stream.writer, { timescale: Timescale(info.timescale), diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index c586bb042ba..62e26e084e9 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1,6 +1,5 @@ import { expect, spyOn, test } from "bun:test"; import { Signal } from "@moq/signals"; -import { Producer as BroadcastProducer } from "../broadcast.ts"; import type { Probe as ProbeStats } from "../connection/stats.ts"; import { error, fromTransport, reason, StreamCode, StreamError } from "../error.ts"; import { HopSchema, isAnonymous, MAX_HOPS, Route, UNKNOWN_HOP } from "../hop.ts"; @@ -803,7 +802,7 @@ test.each([ const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); let settled = false; - const fetch = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0).then( + const fetch = subscriber.fetchGroup(Path.from("room"), "video", 0).then( () => { settled = true; return undefined; @@ -837,9 +836,7 @@ test("a fetch started after the subscriber closes rejects without opening a stre const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); subscriber.close(); - const err = await subscriber - .fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0) - .catch((err: unknown) => err); + const err = await subscriber.fetchGroup(Path.from("room"), "video", 0).catch((err: unknown) => err); expectCut(err, undefined); expect(streams.length).toBe(0); }); @@ -850,7 +847,7 @@ test("an already-aborted fetch rejects without opening a stream", async () => { const cause = new Error("gone"); const err = await subscriber - .fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, { + .fetchGroup(Path.from("room"), "video", 0, { signal: AbortSignal.abort(cause), }) .catch((err: unknown) => err); @@ -865,10 +862,10 @@ test("one of two fetch sharers aborting leaves the other's fetch", async () => { const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); const controller = new AbortController(); - const a = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, { + const a = subscriber.fetchGroup(Path.from("room"), "video", 0, { signal: controller.signal, }); - const b = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0); + const b = subscriber.fetchGroup(Path.from("room"), "video", 0); await drainUntil(() => streams.length === 1); await answerTrackInfo(streams[0]); @@ -901,10 +898,10 @@ test.each([ const first = new AbortController(); const second = new AbortController(); - const a = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, { + const a = subscriber.fetchGroup(Path.from("room"), "video", 0, { signal: first.signal, }); - const b = subscriber.fetchGroup(new BroadcastProducer().consume(), Path.from("room"), "video", 0, { + const b = subscriber.fetchGroup(Path.from("room"), "video", 0, { signal: second.signal, }); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 44c07531b83..8b4f714e4a7 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -6,7 +6,15 @@ import { BroadcastCache } from "../consume.ts"; import { controlTimeout, error, ProtocolViolation, reason, StreamCode, StreamError, sessionCause } from "../error.ts"; import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, type Route, randomHop, routesEqual, stampHops, UNKNOWN_HOP } from "../hop.ts"; -import { groupBounds, hiddenBelow, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; +import { + groupBounds, + hiddenBelow, + namedOrigin, + nameOrigin, + scopeCaptures, + scopeHead, + scopeOverlaps, +} from "../internal.ts"; import * as Path from "../path.ts"; import { type OpenOptions, type Reader, Stream } from "../stream.ts"; import { TAIL_GRACE_MS, Tail } from "../tail.ts"; @@ -90,9 +98,6 @@ interface SubscribeEntry { // (SUBSCRIBE_END), once it declares them. start?: number; end?: number; - // The broadcast the subscription belongs to, which records the origin SUBSCRIBE_START - // names so a session republishing it names the same one. - broadcast: broadcast.Consumer; // Whether SUBSCRIBE_START arrived. On draft-07, which names the serving origin there, // group streams wait for it: until then nobody knows whose content they carry. started: Signal; @@ -506,14 +511,14 @@ export class Subscriber { for (;;) { const request = await wireOf(consumer).requested(); if (!request) break; - void this.#runSubscribe(consumer, path, request); + void this.#runSubscribe(path, request); } })(); return consumer; } - async #runSubscribe(consumer: broadcast.Consumer, broadcast: Path.Valid, request: track.Request) { + async #runSubscribe(broadcast: Path.Valid, request: track.Request) { const id = this.#subscribeNext++; const subscription = request.subscription; const initialBounds = groupBounds(subscription.groups); @@ -542,7 +547,7 @@ export class Subscriber { // Open the stream under a timeout. The stream handle flows back via `state` // so the timeout path can abort it if it finishes opening after the deadline. const state: { stream?: Stream } = {}; - const setup = this.#openSubscribe(state, msg, request, id, timescale, consumer); + const setup = this.#openSubscribe(state, msg, request, id, timescale); let opened: { stream: Stream; entry: SubscribeEntry }; try { @@ -637,7 +642,6 @@ export class Subscriber { request: track.Request, id: bigint, timescale: Signal, - consumer: broadcast.Consumer, ): Promise<{ stream: Stream; entry: SubscribeEntry }> { let producer: track.Producer; let drainOk = false; @@ -668,7 +672,6 @@ export class Subscriber { }, }), requested: msg.startGroup, - broadcast: consumer, started: new Signal(!hasOrigin(this.version)), }; this.#subscribes.set(id, entry); @@ -744,7 +747,6 @@ export class Subscriber { // Open a FETCH stream for one group and stream its bare frames into a group, for the // ConsumeBroadcast backing track.Consumer.fetchGroup() (lite-05+). async fetchGroup( - front: broadcast.Consumer, broadcast: Path.Valid, track: string, sequence: number, @@ -769,7 +771,7 @@ export class Subscriber { consumer = group.mirror(); entry = { group, - accepted: this.#runFetch(front, broadcast, track, sequence, options.priority ?? 0, group), + accepted: this.#runFetch(broadcast, track, sequence, options.priority ?? 0, group), }; this.#fetches.set(key, entry); void group.closed.then(() => { @@ -779,6 +781,9 @@ export class Subscriber { try { await untilAborted(entry.accepted, options.signal); + // Each mirror carries the origin the shared FETCH_OK named, for a session republishing it. + const origin = namedOrigin(entry.group); + if (origin !== undefined) nameOrigin(consumer, origin); return consumer; } catch (err) { consumer.close(); @@ -790,7 +795,6 @@ export class Subscriber { // group, evict the entry, and reject every caller waiting for acceptance. A setup every caller // has abandoned is cancelled the same way. async #runFetch( - front: broadcast.Consumer, broadcast: Path.Valid, track: string, sequence: number, @@ -805,7 +809,7 @@ export class Subscriber { // A publisher that never answers would hold the setup forever. Subscriber.close() // closing the group releases every caller at any stage, and resets the streams the // setup opened. - const setup = this.#fetchSetup(front, broadcast, track, sequence, priority, group); + const setup = this.#fetchSetup(broadcast, track, sequence, priority, group); let accepted: { stream: Stream; info: TrackInfo }; try { accepted = await untilAbandoned(group, setup); @@ -828,7 +832,6 @@ export class Subscriber { // Resolve the track's timescale, then open the FETCH stream and wait for it to be accepted. // Closing the group during that wait resets the stream. async #fetchSetup( - front: broadcast.Consumer, broadcast: Path.Valid, track: string, sequence: number, @@ -841,9 +844,9 @@ export class Subscriber { await new FetchMessage({ broadcast, track, priority, group: sequence }).encode(stream.writer, this.version); if (hasOrigin(this.version)) { // Draft-07 accepts with FETCH_OK naming the serving origin before any frame; - // record it so a session republishing the broadcast names the same one. + // record it on the group so a session republishing it names the same one. const ok = await untilClosed(group, FetchOk.decode(stream.reader, this.version)); - wireOf(front).name(ok.origin); + nameOrigin(group, ok.origin); } else { // A byte or an empty-group FIN accepts the fetch; a reset rejects it. // done() buffers that byte so the response pump can decode it normally. @@ -914,7 +917,9 @@ export class Subscriber { if ("start" in resp) { entry.start = resp.start.group; if (hasOrigin(this.version)) { - wireOf(entry.broadcast).name(resp.start.origin); + // A copy has one origin for its whole life: a START naming another means + // upstream is serving a different track, so this throws and drops the copy. + nameOrigin(entry.track, resp.start.origin); entry.started.set(true); } // The groups the SUBSCRIBE asked for below it are unavailable, whatever the @@ -1307,7 +1312,7 @@ class ConsumeBroadcast extends broadcast.Consumer { super(state); overrideBroadcastWire(this, { resolveTrackInfo: (name) => subscriber.resolveTrackInfo(path, name), - fetchGroup: (name, sequence, options) => subscriber.fetchGroup(this, path, name, sequence, options), + fetchGroup: (name, sequence, options) => subscriber.fetchGroup(path, name, sequence, options), }); this.#subscriber = subscriber; this.#path = path; diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 52dc4034e01..b83cb5d215a 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -10,6 +10,7 @@ import { type Frame, type Consumer as GroupConsumer, Producer as GroupProducer } import { groupBounds, hooks, + namedOrigin, type Recv, type TrackRequestOptions, type TrackSequence, @@ -1088,6 +1089,10 @@ export class Subscriber { subscriber.#enforceLatency = false; }; hooks.replaceGroups = (subscriber, groups) => subscriber.#replaceGroups(groups); + hooks.trackOrigin = (subscriber) => { + const producer = subscriber.#state.producer; + return producer && namedOrigin(producer); + }; // The sequence cursor lives here (it shares the buffer and the drift anchor with // the arrival cursor); `Ordered` is the handle that reaches it. ordered_ = { diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index bb9af836281..a8892273095 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -22,12 +22,11 @@ export interface Broadcast { fetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise; requested(): Promise; /** - * The origin serving this broadcast, named in SUBSCRIBE_OK and FETCH_OK: the one an - * upstream reply named, or else a random one generated once for the broadcast. + * The origin serving `content` (a served subscription or a fetched group), named in + * SUBSCRIBE_START and FETCH_OK: the one an upstream reply named for it, or else a random + * one generated once for the broadcast. */ - origin(): Hop; - /** Record the origin an upstream reply named for this broadcast. */ - name(origin: Hop): void; + origin(content: track.Subscriber | GroupConsumer): Hop; } /** The protocol-facing operations behind an origin producer. */ diff --git a/quest/m0/wildcard/README.md b/quest/m0/wildcard/README.md index a6fbb088993..c29f7dbd2e8 100644 --- a/quest/m0/wildcard/README.md +++ b/quest/m0/wildcard/README.md @@ -192,7 +192,6 @@ distinguish recording generations reads the catalog's archive entry ## Required - [Datagrams behind SUBSCRIBE_START](/quest/m0/wildcard/datagram-start.md) - no datagram crosses a subscription's start in either direction, in Rust or JS -- [JS origin granularity](/quest/m0/wildcard/js-origin.md) - `@moq/net` tracks a reply's origin at the same granularity as Rust - [Pool resolution benchmark](/quest/m0/wildcard/pool-bench.md) - route resolution is benchmarked over pool size and requested paths ## Related diff --git a/quest/m0/wildcard/js-origin.md b/quest/m0/wildcard/js-origin.md deleted file mode 100644 index df54d0bf6a8..00000000000 --- a/quest/m0/wildcard/js-origin.md +++ /dev/null @@ -1,35 +0,0 @@ -# [S] JS origin granularity - -## Goal - -`@moq/net` tracks the origin an upstream reply names at the same granularity -as Rust, and handles a second, different origin the way Rust does, so a JS -node that republishes never labels one origin's content as another's. - -## Plan - -[#4279](https://github.com/moq-dev/moq/pull/4279) had JS record the origin -an upstream SUBSCRIBE_OK or FETCH_OK names on the consumed broadcast's shared -state (`js/net/src/broadcast.ts`), and the latest reply wins. Rust records it -per copy of a track (`track::Provenance`) and only admits a replacement -naming the same origin. Codex asked for a conflicting name to be refused -([r4112837960](https://github.com/moq-dev/moq/pull/4279#discussion_r4112837960)). -The agent declined, since a later request for the same broadcast can -legitimately land on a front serving another origin, and left the -granularity question to the maintainer -([r4113954489](https://github.com/moq-dev/moq/pull/4279#discussion_r4113954489)). -The maintainer decided in the 09-28 merged-PR audit that JS must match Rust. - -- Move the origin to the level Rust keeps it at, so two tracks of one - broadcast served by different origins stay distinct. -- A reply naming a different origin for content already labeled follows - Rust's rule rather than overwriting silently. -- A republished broadcast advertises, per track, the origin that actually - served it, or a random one when nothing upstream named one. - -Test the mid-life origin change JS previously absorbed, and pin that JS and -Rust agree on it in the interop suite if the scenario is reachable there. - -## Related - -- [Wildcard](/quest/m0/wildcard/README.md) - the line this blocks From 56f91c96d1f5155300b134655a289c0d22d05166 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 10:02:04 -0700 Subject: [PATCH 3/5] wip(net): session-origin fallback checkpoint before the publish-only scope Co-Authored-By: Claude Opus 5.5 --- js/net/src/broadcast.ts | 19 +++------- js/net/src/lite/origin.test.ts | 67 +++++++++++++++++++++++++--------- js/net/src/lite/publisher.ts | 11 +++++- js/net/src/wire.ts | 7 ++-- 4 files changed, 68 insertions(+), 36 deletions(-) diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index 592bf935898..348af5cb98c 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -6,7 +6,7 @@ import { type GetPromise, Once, Signal } from "@moq/signals"; import { NotFound } from "./error.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; -import { type Hop, Route, randomHop } from "./hop.ts"; +import { type Hop, Route } from "./hop.ts"; import { hooks, namedOrigin, type TrackSequence } from "./internal.ts"; import * as Path from "./path.ts"; import * as track from "./track.ts"; @@ -34,18 +34,11 @@ class BroadcastState { // Live consumer handles sharing this state (see {@link Consumer.clone}). The broadcast // closes once the last one closes, so a shared consumer can be handed to several callers. consumers = 0; - // The origin named for content no upstream reply named, generated on first use. - origin?: Hop; } -// The origin a peer is told serves `content`: the one an upstream reply named for that track -// copy or fetched group, or else a random one for content originating here, stable for the -// broadcast's life and shared by every session serving it. -function origin(state: BroadcastState, content: track.Subscriber | GroupConsumer): Hop { - const named = content instanceof track.Subscriber ? hooks.trackOrigin(content) : namedOrigin(content); - if (named !== undefined) return named; - state.origin ??= randomHop(); - return state.origin; +// The origin an upstream reply named for `content`, a served track copy or a fetched group. +function origin(content: track.Subscriber | GroupConsumer): Hop | undefined { + return content instanceof track.Subscriber ? hooks.trackOrigin(content) : namedOrigin(content); } function dequeueRequest(state: BroadcastState): track.Request | undefined { @@ -259,7 +252,7 @@ export class Producer { resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), - origin: (content) => origin(this.#state, content), + origin, }; } @@ -333,7 +326,7 @@ export class Consumer { resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), - origin: (content) => origin(this.#state, content), + origin, }); } diff --git a/js/net/src/lite/origin.test.ts b/js/net/src/lite/origin.test.ts index 3a19fbfa970..bc8cf5b8369 100644 --- a/js/net/src/lite/origin.test.ts +++ b/js/net/src/lite/origin.test.ts @@ -1,14 +1,17 @@ import { expect, test } from "bun:test"; import * as broadcast from "../broadcast.ts"; -import { type Hop, HopSchema, randomHop, UNKNOWN_HOP } from "../hop.ts"; +import { Producer as GroupProducer } from "../group.ts"; +import { type Hop, HopSchema, randomHop } from "../hop.ts"; import { createMockTransportPair } from "../mock.ts"; +import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { Reader, Stream } from "../stream.ts"; import { wireOf } from "../wire.ts"; import { Fetch, FetchOk } from "./fetch.ts"; import { Group as GroupMessage } from "./group.ts"; +import { Publisher } from "./publisher.ts"; import { StreamId } from "./stream.ts"; -import { encodeSubscribeResponse, Subscribe, SubscribeStart } from "./subscribe.ts"; +import { decodeSubscribeResponse, encodeSubscribeResponse, Subscribe, SubscribeStart } from "./subscribe.ts"; import { Subscriber } from "./subscriber.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { ALPN_07_WIP, Version } from "./version.ts"; @@ -28,26 +31,56 @@ async function settlesWithin(promise: Promise, ms: number): Promise { +// Nobody upstream named content that originates here: the serving session names its own hop. +test("a broadcast originating here carries no upstream origin", () => { const producer = new broadcast.Producer(); producer.createTrack("video"); - producer.createTrack("audio"); - - const origin = served(producer, "video"); - expect(origin).not.toBe(UNKNOWN_HOP); - expect(served(producer, "video")).toBe(origin); - // Every track and every handle, and so every session serving it, names the same one. - expect(served(producer, "audio")).toBe(origin); - expect(served(producer.consume(), "video")).toBe(origin); - - const other = new broadcast.Producer(); - other.createTrack("video"); - expect(served(other, "video")).not.toBe(origin); + expect(served(producer, "video")).toBeUndefined(); + expect(served(producer.consume(), "video")).toBeUndefined(); +}); + +// A session names its own hop, as Rust names its origin's, for content nobody upstream named. +test("a draft-07 publisher names its own hop for content originating here", async () => { + const pair = createMockTransportPair(ALPN_07_WIP); + const origin = new OriginProducer(); + const hop = randomHop(); + const publisher = new Publisher(pair.server, VERSION, hop, origin.consume()); + const produced = origin.createBroadcast(Path.from("room")); + produced.announce(); + const track = produced.createTrack("video"); + const group = new GroupProducer(0); + group.writeString("x"); + group.close(); + track.writeGroup(group); + + const sub = await Stream.open(pair.client); + const serving = await Stream.accept(pair.server); + if (!serving) throw new Error("the publisher never accepted the subscribe stream"); + void publisher.runSubscribe( + new Subscribe({ id: 0n, broadcast: Path.from("room"), track: "video", priority: 0 }), + serving, + ); + const start = await decodeSubscribeResponse(sub.reader, VERSION); + if (!("start" in start)) throw new Error("expected SUBSCRIBE_START"); + expect(start.start.origin).toBe(hop); + + const fetch = await Stream.open(pair.client); + const fetching = await Stream.accept(pair.server); + if (!fetching) throw new Error("the publisher never accepted the fetch stream"); + void publisher.runFetch( + new Fetch({ broadcast: Path.from("room"), track: "video", priority: 0, group: 0 }), + fetching, + ); + expect((await FetchOk.decode(fetch.reader, VERSION)).origin).toBe(hop); + + publisher.close(); + sub.close(); + fetch.close(); }); // Plays the upstream publisher on a mock draft-07 session, answering the subscriber's streams. diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 4a9e6fd5c08..29ebdb068a9 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -570,6 +570,13 @@ export class Publisher { } } + // The origin serving `content`, for SUBSCRIBE_START and FETCH_OK: the one an upstream reply + // named, or else this session's own hop for content originating here, as Rust names its + // origin's hop. + #origin(front: broadcast.Consumer, content: track.Subscriber | group.Consumer): Hop { + return wireOf(front).origin(content) ?? this.hop; + } + /** * Handles a subscribe message. * @param msg - The subscribe message @@ -672,7 +679,7 @@ export class Publisher { }); // Read once content flowed: an upstream's SUBSCRIBE_START named the track // copy's origin before any of its content did. - const start = new SubscribeStart(sequence, wireOf(front).origin(track)); + const start = new SubscribeStart(sequence, this.#origin(front, track)); await encodeSubscribeResponse(stream.writer, { start }, this.version); }); @@ -742,7 +749,7 @@ export class Publisher { const info = await this.#resolveTrackInfo(front, msg.track); group = await wireOf(front).fetchGroup(msg.track, msg.group, { priority: msg.priority }); if (hasOrigin(this.version)) { - await new FetchOk(wireOf(front).origin(group)).encode(stream.writer, this.version); + await new FetchOk(this.#origin(front, group)).encode(stream.writer, this.version); } await this.#runFetchGroup(group, stream.writer, { timescale: Timescale(info.timescale), diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index a8892273095..4b9c34ed320 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -22,11 +22,10 @@ export interface Broadcast { fetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise; requested(): Promise; /** - * The origin serving `content` (a served subscription or a fetched group), named in - * SUBSCRIBE_START and FETCH_OK: the one an upstream reply named for it, or else a random - * one generated once for the broadcast. + * The origin an upstream reply named for `content` (a served subscription or a fetched + * group), if any. Content nobody upstream named originates on the serving session. */ - origin(content: track.Subscriber | GroupConsumer): Hop; + origin(content: track.Subscriber | GroupConsumer): Hop | undefined; } /** The protocol-facing operations behind an origin producer. */ From 38c37527777fde73ba90f1a94c054a057a5fadbc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 10:12:28 -0700 Subject: [PATCH 4/5] fix(net): JS names its own origin and drops a copy whose origin changes @moq/net publishes only what it produces, so the lite publisher names its own session hop in SUBSCRIBE_START and FETCH_OK, as Rust names its origin's. The subscriber keeps the origin per subscription (one track copy) and drops the copy when a later SUBSCRIBE_START names another, matching Rust's track::Provenance. The broadcast-level origin plumbing is removed, and fetch.test.ts adopts main's versioned streams. Co-Authored-By: Claude Opus 5.5 --- js/net/src/broadcast.ts | 11 +-- js/net/src/internal.ts | 28 +------- js/net/src/lite/fetch.test.ts | 3 +- js/net/src/lite/origin.test.ts | 119 ++++++++++----------------------- js/net/src/lite/publisher.ts | 15 ++--- js/net/src/lite/subscriber.ts | 32 ++++----- js/net/src/track.ts | 5 -- js/net/src/wire.ts | 7 +- 8 files changed, 60 insertions(+), 160 deletions(-) diff --git a/js/net/src/broadcast.ts b/js/net/src/broadcast.ts index 348af5cb98c..fa94ed42dd8 100644 --- a/js/net/src/broadcast.ts +++ b/js/net/src/broadcast.ts @@ -6,8 +6,8 @@ import { type GetPromise, Once, Signal } from "@moq/signals"; import { NotFound } from "./error.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; -import { type Hop, Route } from "./hop.ts"; -import { hooks, namedOrigin, type TrackSequence } from "./internal.ts"; +import { Route } from "./hop.ts"; +import { hooks, type TrackSequence } from "./internal.ts"; import * as Path from "./path.ts"; import * as track from "./track.ts"; import { untilAborted } from "./util/abort.ts"; @@ -36,11 +36,6 @@ class BroadcastState { consumers = 0; } -// The origin an upstream reply named for `content`, a served track copy or a fetched group. -function origin(content: track.Subscriber | GroupConsumer): Hop | undefined { - return content instanceof track.Subscriber ? hooks.trackOrigin(content) : namedOrigin(content); -} - function dequeueRequest(state: BroadcastState): track.Request | undefined { const requested = state.requested.peek(); requested.sort((a, b) => a.priority - b.priority); @@ -252,7 +247,6 @@ export class Producer { resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), - origin, }; } @@ -326,7 +320,6 @@ export class Consumer { resolveTrackInfo: (name) => resolveTrackInfo(this.#state, name), fetchGroup: (name, sequence, options) => fetchGroup(this.#state, name, sequence, options), requested: () => this.#requested(), - origin, }); } diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 08d0759e6e3..e809fe0e98c 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -7,8 +7,8 @@ */ import type { Dispose, Getter } from "@moq/signals"; import type { Consumer as BroadcastConsumer, Producer as BroadcastProducer } from "./broadcast.ts"; -import type { Frame, Consumer as GroupConsumer, Producer as GroupProducer } from "./group.ts"; -import type { Hop, Route } from "./hop.ts"; +import type { Frame, Consumer as GroupConsumer } from "./group.ts"; +import type { Route } from "./hop.ts"; import * as Path from "./path.ts"; import type { Timestamp } from "./time.ts"; import type { Groups, Producer, Request, Subscriber } from "./track.ts"; @@ -160,8 +160,6 @@ export const hooks: { * `setGroups`, which never rewinds. */ replaceGroups: (subscriber: Subscriber, groups: Groups) => void; - /** The origin an upstream reply named for the track copy a subscriber reads, if any. */ - trackOrigin: (subscriber: Subscriber) => Hop | undefined; /** Return a group's first timestamp, retained even after its first frame is read. */ groupTimestamp: (group: GroupConsumer) => Timestamp | undefined; groupLatest: (group: GroupConsumer) => Timestamp | undefined; @@ -202,9 +200,6 @@ export const hooks: { replaceGroups: () => { throw new Error("track.ts not loaded"); }, - trackOrigin: () => { - throw new Error("track.ts not loaded"); - }, groupTimestamp: () => { throw new Error("group.ts not loaded"); }, @@ -231,25 +226,6 @@ export const hooks: { }, }; -// Weak, so a label lives exactly as long as the content it names. -const origins = new WeakMap(); - -/** - * Record the origin an upstream reply named for `content`: a track copy's producer, or a - * fetched group. Content has one origin for its whole life, so a reply naming another means - * upstream is serving something else. That throws, and the caller drops the content. - */ -export function nameOrigin(content: Producer | GroupProducer | GroupConsumer, origin: Hop): void { - const named = origins.get(content); - if (named === undefined) origins.set(content, origin); - else if (named !== origin) throw new Error(`origin changed: ${named} to ${origin}`); -} - -/** The origin an upstream reply named for `content`, if any. */ -export function namedOrigin(content: Producer | GroupProducer | GroupConsumer): Hop | undefined { - return origins.get(content); -} - /** * Spreads equal routes across paths: FNV-1a 64 of `path` then each hop, oldest first, as 8 * little-endian bytes. Keyed on the requested path so an equal-cost pool advertising one diff --git a/js/net/src/lite/fetch.test.ts b/js/net/src/lite/fetch.test.ts index eb484a7f590..a38cb9160c8 100644 --- a/js/net/src/lite/fetch.test.ts +++ b/js/net/src/lite/fetch.test.ts @@ -51,13 +51,14 @@ test("FetchOk names the origin on draft-07 only", async () => { const written: Uint8Array[] = []; const writer = new Writer( new WritableStream({ write: (chunk) => void written.push(new Uint8Array(chunk)) }), + Version.DRAFT_07, ); await new FetchOk(HopSchema.parse(42n)).encode(writer, Version.DRAFT_07); writer.close(); await writer.closed; const buf = concat(written); expect(buf).toEqual(new Uint8Array([1, 42])); - const got = await FetchOk.decode(new Reader(undefined, buf), Version.DRAFT_07); + const got = await FetchOk.decode(new Reader(undefined, buf, Version.DRAFT_07), Version.DRAFT_07); expect(got.origin).toBe(HopSchema.parse(42n)); await expect(new FetchOk(HopSchema.parse(42n)).encode(writer, Version.DRAFT_06)).rejects.toThrow(); diff --git a/js/net/src/lite/origin.test.ts b/js/net/src/lite/origin.test.ts index bc8cf5b8369..7587b87974a 100644 --- a/js/net/src/lite/origin.test.ts +++ b/js/net/src/lite/origin.test.ts @@ -1,12 +1,10 @@ import { expect, test } from "bun:test"; -import * as broadcast from "../broadcast.ts"; import { Producer as GroupProducer } from "../group.ts"; import { type Hop, HopSchema, randomHop } from "../hop.ts"; import { createMockTransportPair } from "../mock.ts"; import { Producer as OriginProducer } from "../origin.ts"; import * as Path from "../path.ts"; import { Reader, Stream } from "../stream.ts"; -import { wireOf } from "../wire.ts"; import { Fetch, FetchOk } from "./fetch.ts"; import { Group as GroupMessage } from "./group.ts"; import { Publisher } from "./publisher.ts"; @@ -31,21 +29,9 @@ async function settlesWithin(promise: Promise, ms: number): Promise { - const producer = new broadcast.Producer(); - producer.createTrack("video"); - expect(served(producer, "video")).toBeUndefined(); - expect(served(producer.consume(), "video")).toBeUndefined(); -}); - -// A session names its own hop, as Rust names its origin's, for content nobody upstream named. -test("a draft-07 publisher names its own hop for content originating here", async () => { +// JS publishes only what it produces, so the session is always the origin and names its own +// hop, as Rust names its origin's. +test("a draft-07 publisher names its own hop in SUBSCRIBE_START and FETCH_OK", async () => { const pair = createMockTransportPair(ALPN_07_WIP); const origin = new OriginProducer(); const hop = randomHop(); @@ -58,8 +44,8 @@ test("a draft-07 publisher names its own hop for content originating here", asyn group.close(); track.writeGroup(group); - const sub = await Stream.open(pair.client); - const serving = await Stream.accept(pair.server); + const sub = await Stream.open(pair.client, { version: VERSION }); + const serving = await Stream.accept(pair.server, VERSION); if (!serving) throw new Error("the publisher never accepted the subscribe stream"); void publisher.runSubscribe( new Subscribe({ id: 0n, broadcast: Path.from("room"), track: "video", priority: 0 }), @@ -69,8 +55,8 @@ test("a draft-07 publisher names its own hop for content originating here", asyn if (!("start" in start)) throw new Error("expected SUBSCRIBE_START"); expect(start.start.origin).toBe(hop); - const fetch = await Stream.open(pair.client); - const fetching = await Stream.accept(pair.server); + const fetch = await Stream.open(pair.client, { version: VERSION }); + const fetching = await Stream.accept(pair.server, VERSION); if (!fetching) throw new Error("the publisher never accepted the fetch stream"); void publisher.runFetch( new Fetch({ broadcast: Path.from("room"), track: "video", priority: 0, group: 0 }), @@ -88,23 +74,18 @@ class Upstream { readonly pair = createMockTransportPair(ALPN_07_WIP); readonly subscriber = new Subscriber(this.pair.client, VERSION, randomHop()); - // Answer a TRACK stream, then accept the SUBSCRIBE or FETCH stream that follows it. - async accept(): Promise { - const info = await Stream.accept(this.pair.server); + // Answer a TRACK stream, then accept and start the SUBSCRIBE that follows it at group 0, + // naming `origin`. + async subscribe(origin: Hop): Promise { + const info = await Stream.accept(this.pair.server, VERSION); if (!info) throw new Error("the subscriber never asked for TRACK_INFO"); expect(await info.reader.u53()).toBe(StreamId.Track); await TrackMessage.decode(info.reader, VERSION); await new TrackInfo({ maxAge: 60_000 }).encode(info.writer, VERSION); info.close(); - const stream = await Stream.accept(this.pair.server); - if (!stream) throw new Error("the subscriber never requested the track"); - return stream; - } - - // Accept a SUBSCRIBE and start it at group 0, naming `origin`. - async subscribe(origin: Hop): Promise { - const sub = await this.accept(); + const sub = await Stream.accept(this.pair.server, VERSION); + if (!sub) throw new Error("the subscriber never subscribed"); expect(await sub.reader.u53()).toBe(StreamId.Subscribe); await Subscribe.decode(sub.reader, VERSION); await this.start(sub, origin); @@ -114,49 +95,22 @@ class Upstream { async start(sub: Stream, origin: Hop): Promise { await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, origin) }, VERSION); } - - // Accept a FETCH and answer it with an empty group, naming `origin`. - async fetch(origin: Hop): Promise { - const stream = await this.accept(); - expect(await stream.reader.u53()).toBe(StreamId.Fetch); - await Fetch.decode(stream.reader, VERSION); - await new FetchOk(origin).encode(stream.writer, VERSION); - stream.close(); - } -} - -// Resolves once `check` holds, polling across macrotasks for the subscriber to read a reply. -async function until(check: () => boolean): Promise { - for (let i = 0; i < 100 && !check(); i++) await new Promise((resolve) => setTimeout(resolve, 1)); - expect(check()).toBe(true); } -// JS used to keep one origin per broadcast, so the latest reply relabeled content an earlier -// one named. Each track copy and each fetched group now keeps the origin its own reply named. -test("tracks and fetched groups of one broadcast keep the origins their own replies named", async () => { +// JS used to keep one origin per broadcast, which the latest reply overwrote. Each track copy +// now keeps its own, so two tracks served by different origins are both fine. +test("tracks of one broadcast keep the origins their own replies named", async () => { const upstream = new Upstream(); const consumer = upstream.subscriber.consume(Path.from("room")); - const video = HopSchema.parse(42n); - const audio = HopSchema.parse(43n); - const archive = HopSchema.parse(44n); - - // A republishing session serves a subscription of its own off each cached copy. - const videoReader = consumer.track("video").subscribe({}); - await upstream.subscribe(video); - await until(() => served(consumer, "video") === video); - const audioReader = consumer.track("audio").subscribe({}); - await upstream.subscribe(audio); - await until(() => served(consumer, "audio") === audio); - - const fetched = consumer.track("video").fetchGroup(0); - await upstream.fetch(archive); - const group = await fetched; - - expect(wireOf(consumer).origin(group)).toBe(archive); - expect(served(consumer, "video")).toBe(video); - expect(served(consumer, "audio")).toBe(audio); - expect(videoReader.closed.peek()).toBeUndefined(); - expect(audioReader.closed.peek()).toBeUndefined(); + + const video = consumer.track("video").subscribe({}); + const videoSub = await upstream.subscribe(HopSchema.parse(42n)); + const audio = consumer.track("audio").subscribe({}); + await upstream.subscribe(HopSchema.parse(43n)); + // Repeating a copy's own origin is not a change. + await upstream.start(videoSub, HopSchema.parse(42n)); + + expect(await settlesWithin(Promise.race([video.closed, audio.closed]), 50)).toBe(false); upstream.subscriber.close(); }); @@ -166,13 +120,11 @@ test("tracks and fetched groups of one broadcast keep the origins their own repl test("a SUBSCRIBE_START naming a second origin drops the copy", async () => { const upstream = new Upstream(); const consumer = upstream.subscriber.consume(Path.from("room")); - const origin = HopSchema.parse(42n); const reader = consumer.track("video").subscribe({}); - const sub = await upstream.subscribe(origin); - await until(() => served(consumer, "video") === origin); - + const sub = await upstream.subscribe(HopSchema.parse(42n)); await upstream.start(sub, HopSchema.parse(43n)); + const closed = await reader.closed; expect(closed).toBeInstanceOf(Error); expect((closed as Error).message).toContain("origin changed"); @@ -180,20 +132,20 @@ test("a SUBSCRIBE_START naming a second origin drops the copy", async () => { upstream.subscriber.close(); }); -test("a draft-07 group waits for SUBSCRIBE_START, whose origin the broadcast then names", async () => { +test("a draft-07 group waits for SUBSCRIBE_START", async () => { const pair = createMockTransportPair(ALPN_07_WIP); const subscriber = new Subscriber(pair.client, VERSION, randomHop()); const consumer = subscriber.consume(Path.from("room")); const reader = consumer.track("video").subscribe({}); - const info = await Stream.accept(pair.server); + const info = await Stream.accept(pair.server, VERSION); if (!info) throw new Error("the subscriber never asked for TRACK_INFO"); expect(await info.reader.u53()).toBe(StreamId.Track); await TrackMessage.decode(info.reader, VERSION); await new TrackInfo({ maxAge: 60_000 }).encode(info.writer, VERSION); info.close(); - const sub = await Stream.accept(pair.server); + const sub = await Stream.accept(pair.server, VERSION); if (!sub) throw new Error("the subscriber never subscribed"); expect(await sub.reader.u53()).toBe(StreamId.Subscribe); await Subscribe.decode(sub.reader, VERSION); @@ -201,19 +153,20 @@ test("a draft-07 group waits for SUBSCRIBE_START, whose origin the broadcast the // The group's stream races ahead of the subscribe stream. let controller!: ReadableStreamDefaultController; const readable = new ReadableStream({ start: (c) => (controller = c) }); - void subscriber.runGroup(new GroupMessage({ subscribe: 0n, sequence: 0 }), new Reader(readable)); + void subscriber.runGroup( + new GroupMessage({ subscribe: 0n, sequence: 0 }), + new Reader(readable, undefined, VERSION), + ); controller.enqueue(new Uint8Array([0, 1, 120])); controller.close(); const next = reader.recvGroup(); expect(await settlesWithin(next, 50)).toBe(false); - const origin = HopSchema.parse(42n); - await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, origin) }, VERSION); + await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, HopSchema.parse(42n)) }, VERSION); const group = await next; expect(group?.sequence).toBe(0); expect(await group?.readString()).toBe("x"); - // Republishing the track proxies the origin upstream named. - expect(wireOf(consumer).origin(reader)).toBe(origin); + subscriber.close(); }); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 29ebdb068a9..f465f655019 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -570,13 +570,6 @@ export class Publisher { } } - // The origin serving `content`, for SUBSCRIBE_START and FETCH_OK: the one an upstream reply - // named, or else this session's own hop for content originating here, as Rust names its - // origin's hop. - #origin(front: broadcast.Consumer, content: track.Subscriber | group.Consumer): Hop { - return wireOf(front).origin(content) ?? this.hop; - } - /** * Handles a subscribe message. * @param msg - The subscribe message @@ -677,9 +670,9 @@ export class Publisher { start: { included: sequence }, end: bounds.endGroup === undefined ? undefined : { included: bounds.endGroup }, }); - // Read once content flowed: an upstream's SUBSCRIBE_START named the track - // copy's origin before any of its content did. - const start = new SubscribeStart(sequence, this.#origin(front, track)); + // This session is the origin: JS publishes only what it produces, so it names + // its own hop, as Rust names its origin's. + const start = new SubscribeStart(sequence, this.hop); await encodeSubscribeResponse(stream.writer, { start }, this.version); }); @@ -749,7 +742,7 @@ export class Publisher { const info = await this.#resolveTrackInfo(front, msg.track); group = await wireOf(front).fetchGroup(msg.track, msg.group, { priority: msg.priority }); if (hasOrigin(this.version)) { - await new FetchOk(this.#origin(front, group)).encode(stream.writer, this.version); + await new FetchOk(this.hop).encode(stream.writer, this.version); } await this.#runFetchGroup(group, stream.writer, { timescale: Timescale(info.timescale), diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 9a7dea77b3a..759c6284dff 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -6,15 +6,7 @@ import { BroadcastCache } from "../consume.ts"; import { controlTimeout, error, ProtocolViolation, reason, StreamCode, StreamError, sessionCause } from "../error.ts"; import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, type Route, randomHop, routesEqual, stampHops, UNKNOWN_HOP } from "../hop.ts"; -import { - groupBounds, - hiddenBelow, - namedOrigin, - nameOrigin, - scopeCaptures, - scopeHead, - scopeOverlaps, -} from "../internal.ts"; +import { groupBounds, hiddenBelow, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; import * as Path from "../path.ts"; import { type OpenOptions, type Reader, Stream } from "../stream.ts"; import { TAIL_GRACE_MS, Tail } from "../tail.ts"; @@ -98,6 +90,9 @@ interface SubscribeEntry { // (SUBSCRIBE_END), once it declares them. start?: number; end?: number; + // The origin SUBSCRIBE_START named (draft-07). The subscription feeds one track copy, + // and a copy has one origin for its whole life, as Rust's track::Provenance. + origin?: Hop; // Whether SUBSCRIBE_START arrived. On draft-07, which names the serving origin there, // group streams wait for it: until then nobody knows whose content they carry. started: Signal; @@ -781,9 +776,6 @@ export class Subscriber { try { await untilAborted(entry.accepted, options.signal); - // Each mirror carries the origin the shared FETCH_OK named, for a session republishing it. - const origin = namedOrigin(entry.group); - if (origin !== undefined) nameOrigin(consumer, origin); return consumer; } catch (err) { consumer.close(); @@ -843,10 +835,9 @@ export class Subscriber { await stream.writer.u53(StreamId.Fetch); await new FetchMessage({ broadcast, track, priority, group: sequence }).encode(stream.writer, this.version); if (hasOrigin(this.version)) { - // Draft-07 accepts with FETCH_OK naming the serving origin before any frame; - // record it on the group so a session republishing it names the same one. - const ok = await untilClosed(group, FetchOk.decode(stream.reader, this.version)); - nameOrigin(group, ok.origin); + // Draft-07 accepts with FETCH_OK before any frame. It names the serving origin, + // which only a republisher would need, and JS does not republish. + await untilClosed(group, FetchOk.decode(stream.reader, this.version)); } else { // A byte or an empty-group FIN accepts the fetch; a reset rejects it. // done() buffers that byte so the response pump can decode it normally. @@ -917,9 +908,12 @@ export class Subscriber { if ("start" in resp) { entry.start = resp.start.group; if (hasOrigin(this.version)) { - // A copy has one origin for its whole life: a START naming another means - // upstream is serving a different track, so this throws and drops the copy. - nameOrigin(entry.track, resp.start.origin); + // A START naming another origin means upstream is serving a different + // track: drop the copy rather than splice another origin's content into it. + if (entry.origin !== undefined && entry.origin !== resp.start.origin) { + throw new Error(`origin changed: ${entry.origin} to ${resp.start.origin}`); + } + entry.origin = resp.start.origin; entry.started.set(true); } // The groups the SUBSCRIBE asked for below it are unavailable, whatever the diff --git a/js/net/src/track.ts b/js/net/src/track.ts index b83cb5d215a..52dc4034e01 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -10,7 +10,6 @@ import { type Frame, type Consumer as GroupConsumer, Producer as GroupProducer } import { groupBounds, hooks, - namedOrigin, type Recv, type TrackRequestOptions, type TrackSequence, @@ -1089,10 +1088,6 @@ export class Subscriber { subscriber.#enforceLatency = false; }; hooks.replaceGroups = (subscriber, groups) => subscriber.#replaceGroups(groups); - hooks.trackOrigin = (subscriber) => { - const producer = subscriber.#state.producer; - return producer && namedOrigin(producer); - }; // The sequence cursor lives here (it shares the buffer and the drift anchor with // the arrival cursor); `Ordered` is the handle that reaches it. ordered_ = { diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 4b9c34ed320..9065dc3561d 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -10,7 +10,7 @@ import type { Dispose, Getter } from "@moq/signals"; import type * as broadcast from "./broadcast.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; -import type { Hop, Route } from "./hop.ts"; +import type { Route } from "./hop.ts"; import type * as origin from "./origin.ts"; import type * as Path from "./path.ts"; import type * as track from "./track.ts"; @@ -21,11 +21,6 @@ export interface Broadcast { resolveTrackInfo(name: string): Promise; fetchGroup(name: string, sequence: number, options?: track.FetchGroupOptions): Promise; requested(): Promise; - /** - * The origin an upstream reply named for `content` (a served subscription or a fetched - * group), if any. Content nobody upstream named originates on the serving session. - */ - origin(content: track.Subscriber | GroupConsumer): Hop | undefined; } /** The protocol-facing operations behind an origin producer. */ From eb7d6e27cc1dbb3c5114553e071e77ee7190be7f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 30 Sep 2026 10:32:54 -0700 Subject: [PATCH 5/5] fix(net): name the origin's stable hop, not the connection's Rust's self_origin is the origin's hop, shared by every session serving it, so a relay failing over between two sessions of one origin sees the same identity. A JS origin now carries one random hop, and the lite publisher names it in SUBSCRIBE_START and FETCH_OK. The same-origin test ends both subscriptions with SUBSCRIBE_END instead of a wall-clock wait. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/origin.test.ts | 77 ++++++++++++++++++++++------------ js/net/src/lite/publisher.ts | 12 ++++-- js/net/src/origin.ts | 6 ++- js/net/src/wire.ts | 4 +- 4 files changed, 67 insertions(+), 32 deletions(-) diff --git a/js/net/src/lite/origin.test.ts b/js/net/src/lite/origin.test.ts index 7587b87974a..6670ac3075e 100644 --- a/js/net/src/lite/origin.test.ts +++ b/js/net/src/lite/origin.test.ts @@ -9,7 +9,13 @@ import { Fetch, FetchOk } from "./fetch.ts"; import { Group as GroupMessage } from "./group.ts"; import { Publisher } from "./publisher.ts"; import { StreamId } from "./stream.ts"; -import { decodeSubscribeResponse, encodeSubscribeResponse, Subscribe, SubscribeStart } from "./subscribe.ts"; +import { + decodeSubscribeResponse, + encodeSubscribeResponse, + Subscribe, + SubscribeEnd, + SubscribeStart, +} from "./subscribe.ts"; import { Subscriber } from "./subscriber.ts"; import { TrackInfo, Track as TrackMessage } from "./track.ts"; import { ALPN_07_WIP, Version } from "./version.ts"; @@ -29,44 +35,53 @@ async function settlesWithin(promise: Promise, ms: number): Promise { +// Serve one SUBSCRIBE and one FETCH of `track` over a fresh draft-07 session publishing `origin`, +// returning the origins its SUBSCRIBE_START and FETCH_OK name. +async function servedOrigins(origin: OriginProducer, track: string): Promise<[Hop, Hop]> { const pair = createMockTransportPair(ALPN_07_WIP); - const origin = new OriginProducer(); - const hop = randomHop(); - const publisher = new Publisher(pair.server, VERSION, hop, origin.consume()); - const produced = origin.createBroadcast(Path.from("room")); - produced.announce(); - const track = produced.createTrack("video"); - const group = new GroupProducer(0); - group.writeString("x"); - group.close(); - track.writeGroup(group); + const publisher = new Publisher(pair.server, VERSION, randomHop(), origin.consume()); const sub = await Stream.open(pair.client, { version: VERSION }); const serving = await Stream.accept(pair.server, VERSION); if (!serving) throw new Error("the publisher never accepted the subscribe stream"); - void publisher.runSubscribe( - new Subscribe({ id: 0n, broadcast: Path.from("room"), track: "video", priority: 0 }), - serving, - ); + void publisher.runSubscribe(new Subscribe({ id: 0n, broadcast: Path.from("room"), track, priority: 0 }), serving); const start = await decodeSubscribeResponse(sub.reader, VERSION); if (!("start" in start)) throw new Error("expected SUBSCRIBE_START"); - expect(start.start.origin).toBe(hop); const fetch = await Stream.open(pair.client, { version: VERSION }); const fetching = await Stream.accept(pair.server, VERSION); if (!fetching) throw new Error("the publisher never accepted the fetch stream"); - void publisher.runFetch( - new Fetch({ broadcast: Path.from("room"), track: "video", priority: 0, group: 0 }), - fetching, - ); - expect((await FetchOk.decode(fetch.reader, VERSION)).origin).toBe(hop); + void publisher.runFetch(new Fetch({ broadcast: Path.from("room"), track, priority: 0, group: 0 }), fetching); + const ok = await FetchOk.decode(fetch.reader, VERSION); publisher.close(); sub.close(); fetch.close(); + return [start.start.origin, ok.origin]; +} + +// JS publishes only what it produces, so the origin it names is its own: one identity per +// origin, shared by every session serving it (Rust's `origin.hop()`). A relay failing over +// from one session to another sees the same origin and resumes rather than ending the broadcast. +test("every draft-07 session serving an origin names the same origin", async () => { + const origin = new OriginProducer(); + const produced = origin.createBroadcast(Path.from("room")); + produced.announce(); + const track = produced.createTrack("video"); + const group = new GroupProducer(0); + group.writeString("x"); + group.close(); + track.writeGroup(group); + + const [start, fetched] = await servedOrigins(origin, "video"); + expect(fetched).toBe(start); + expect(await servedOrigins(origin, "video")).toEqual([start, start]); + + const other = new OriginProducer(); + const elsewhere = other.createBroadcast(Path.from("room")); + elsewhere.announce(); + elsewhere.createTrack("video").writeGroup(group); + expect((await servedOrigins(other, "video"))[0]).not.toBe(start); }); // Plays the upstream publisher on a mock draft-07 session, answering the subscriber's streams. @@ -95,6 +110,12 @@ class Upstream { async start(sub: Stream, origin: Hop): Promise { await encodeSubscribeResponse(sub.writer, { start: new SubscribeStart(0, origin) }, VERSION); } + + // End a started subscription cleanly with no groups served. + async end(sub: Stream): Promise { + await encodeSubscribeResponse(sub.writer, { end: new SubscribeEnd(0, 0) }, VERSION); + sub.close(); + } } // JS used to keep one origin per broadcast, which the latest reply overwrote. Each track copy @@ -106,11 +127,15 @@ test("tracks of one broadcast keep the origins their own replies named", async ( const video = consumer.track("video").subscribe({}); const videoSub = await upstream.subscribe(HopSchema.parse(42n)); const audio = consumer.track("audio").subscribe({}); - await upstream.subscribe(HopSchema.parse(43n)); + const audioSub = await upstream.subscribe(HopSchema.parse(43n)); // Repeating a copy's own origin is not a change. await upstream.start(videoSub, HopSchema.parse(42n)); - expect(await settlesWithin(Promise.race([video.closed, audio.closed]), 50)).toBe(false); + // Both end cleanly, after every START was read, rather than with a mismatch. + await upstream.end(videoSub); + await upstream.end(audioSub); + expect(await video.closed).toBeNull(); + expect(await audio.closed).toBeNull(); upstream.subscriber.close(); }); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index f465f655019..c7069a47fc4 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -380,6 +380,10 @@ export class Publisher { #publish?: OriginConsumer; + // The origin named in SUBSCRIBE_START and FETCH_OK: the published origin's identity, shared by + // every session serving it. Unused without one, since nothing is served. + #origin: Hop; + // TRACK_INFO is immutable per track, so resolve it from the application once // (via a throwaway subscribe whose info() resolves when the app calls accept) // and reuse it for every later TRACK request of the same track. Keyed by the @@ -404,6 +408,7 @@ export class Publisher { const origin = publish && wireOf(publish); this.#advertised = origin?.advertised ?? new Signal(new Map()); this.#publish = publish; + this.#origin = origin?.hop ?? hop; // Grab the datagram writer up front when the transport carries datagrams (no group // fallback, so it stays undefined otherwise). One writer for all subscriptions. @@ -670,9 +675,8 @@ export class Publisher { start: { included: sequence }, end: bounds.endGroup === undefined ? undefined : { included: bounds.endGroup }, }); - // This session is the origin: JS publishes only what it produces, so it names - // its own hop, as Rust names its origin's. - const start = new SubscribeStart(sequence, this.hop); + // JS publishes only what it produces, so the origin serving it is ours. + const start = new SubscribeStart(sequence, this.#origin); await encodeSubscribeResponse(stream.writer, { start }, this.version); }); @@ -742,7 +746,7 @@ export class Publisher { const info = await this.#resolveTrackInfo(front, msg.track); group = await wireOf(front).fetchGroup(msg.track, msg.group, { priority: msg.priority }); if (hasOrigin(this.version)) { - await new FetchOk(this.hop).encode(stream.writer, this.version); + await new FetchOk(this.#origin).encode(stream.writer, this.version); } await this.#runFetchGroup(group, stream.writer, { timescale: Timescale(info.timescale), diff --git a/js/net/src/origin.ts b/js/net/src/origin.ts index c06c5970969..2c3702ce650 100644 --- a/js/net/src/origin.ts +++ b/js/net/src/origin.ts @@ -14,7 +14,7 @@ import { Derived, type Dispose, type GetPromise, type Getter, getter, Once, Sign import * as announce from "./announced.ts"; import * as broadcast from "./broadcast.ts"; import { StreamCode, StreamError } from "./error.ts"; -import { isAnonymous, Route, routesEqual } from "./hop.ts"; +import { type Hop, isAnonymous, Route, randomHop, routesEqual } from "./hop.ts"; import { hiddenBelow, hooks, scopeCaptures, scopeHead, scopeOverlaps, spreadHash } from "./internal.ts"; import * as Path from "./path.ts"; import { type Advertised, type Advertisements, registerWire, wireOf } from "./wire.ts"; @@ -383,6 +383,9 @@ class OriginState { // received entry back to a peer, which is what makes an origin shared by both // directions echo-free. created: Map | undefined = new Map(); + // This origin's identity, which its replies name as the origin of what it serves. Shared by + // every session serving it, so a peer can fail over between them (Rust's `origin.hop()`). + readonly hop: Hop = randomHop(); local = new VersionedSignal | undefined>(new Map()); advertisedLocal = new VersionedSignal | undefined>(new Map()); routes = new VersionedSignal | undefined>(new Map()); @@ -1147,6 +1150,7 @@ export class Consumer { total === 0 ? undefined : discovery === total, ); registerWire(this, { + hop: state.hop, routes: (path) => this.#routes(scope.path(path)), broadcasts: scope === Scope.all ? state.local : new Derived([state.local], (local) => scope.projectPaths(local)), diff --git a/js/net/src/wire.ts b/js/net/src/wire.ts index 9065dc3561d..501c4d3586e 100644 --- a/js/net/src/wire.ts +++ b/js/net/src/wire.ts @@ -10,7 +10,7 @@ import type { Dispose, Getter } from "@moq/signals"; import type * as broadcast from "./broadcast.ts"; import type { Consumer as GroupConsumer } from "./group.ts"; -import type { Route } from "./hop.ts"; +import type { Hop, Route } from "./hop.ts"; import type * as origin from "./origin.ts"; import type * as Path from "./path.ts"; import type * as track from "./track.ts"; @@ -43,6 +43,8 @@ export interface OriginProducer { /** The protocol-facing operations behind an origin consumer. */ export interface OriginConsumer { + /** The origin's identity, named in SUBSCRIBE_START and FETCH_OK for everything it serves. */ + readonly hop: Hop; routes(path: Path.Valid): boolean; readonly broadcasts: Getter | undefined>; readonly advertised: Getter;