From c4aef1a7a21476b9277ebad78e16f89f965ef3e4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 04:20:55 -0800 Subject: [PATCH 01/25] chore(quest): claim quest/m1/relay-inflight-replay.md Quest-Claim: 4a1debf177ebe0bb From 74909c81dec73363cba75ac36471e104d8eba5a3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 04:39:17 -0800 Subject: [PATCH 02/25] fix(net): join unresolved legacy tracks at the live edge Co-Authored-By: GPT-6.1 Sol --- quest/m1/relay-inflight-replay.md | 30 ++++ rs/moq-net/src/lite/publisher.rs | 144 +++++++++++++++++- rs/moq-net/tests/integration/range_anchors.rs | 2 +- 3 files changed, 168 insertions(+), 8 deletions(-) diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index a46d7a5a1d5..fad30e6b997 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -31,3 +31,33 @@ pipeline-requests epic, so this sits at the m1 top level. cells here. Public API: none. Wire: none. + +Implementation checkpoint: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737) +removes the confirmation-time version guard and adds a controlled held-answer test. +The test exposes cached groups 0, 3, and 7 with source start 0, and checks the +encoded group streams. Lite03/04 join at 7; explicit starts and arrival floors +remain covered, with lite05/06 controls. The range-anchor integration expectation +now includes lite03/04 among the versions whose unfloored copy joins live. + +## Waiting + +- Maintainer: settle the HLS recorder startup contract before this behavior lands. + The live-edge fix makes + `moq-hls::integration server::a_cursor_records_a_publisher_without_fetch` + time out with `moq-lite-03: no segment recorded`; the same test passes with + `origin/main`'s publisher implementation. The normal test first reports this + on lite01; a diagnostic run limited to lite03 confirms that restricting the + fix to lite03/04 would not avoid it. `Demand::wanted` includes queries, and + pre05 copies expose synthetic info before their upstream SUBSCRIBE answer, + so neither signal establishes that a media subscription can retain the + recorder's first segment. Options: (1) preserve the settled live-edge join + and make HLS recording request an explicit media floor derived from its + records (recommended), or (2) revisit how a pending join distinguishes cached + history from media produced in response to its demand. Do not weaken the + recorder assertion or add a timing wait. Complete the source fix, rerun + `just ci`, and run all-language interop if this extends into the gateway. + The twelve requested lite03/04 Rust/browser-JS publisher by Rust/Node/Bun + subscriber matrix cells passed. The broader interop invocation also runs + finite-finish and CLI FETCH cases that failed on these old versions; their + applicability and base behavior still need verification before claiming + that broader suite passes. diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 865488d068f..c7e901f8c73 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -121,9 +121,9 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// must not read as a request to replay the whole cache on join. /// /// `latest` is the newest group when the SUBSCRIBE arrived, not once the subscription -/// resolved: anything written in between is newer, so it is delivered. On lite-05, a -/// SUBSCRIBE sent alongside its TRACK can arrive before the track resolves, when no group -/// is known yet: the newest once it resolves stands in, as it would for one sent after. +/// resolved: anything written in between is newer, so it is delivered. A SUBSCRIBE on +/// a copy still fetching upstream (or alongside its TRACK on lite-05) can arrive before +/// any group is known: the newest once it resolves stands in, as for one sent after. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -2256,10 +2256,7 @@ impl Request for SubscribeServe { ietf, }; - let latest = match shared.version.has_track_stream() { - true => latest.or_else(|| track.latest_sequence()), - false => latest, - }; + let latest = latest.or_else(|| track.latest_sequence()); let floor = cursor_floor(shared.version, bounds.groups().start(), latest); let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); if let Some(update) = update { @@ -8798,6 +8795,139 @@ mod tests { } } + /// A join on an unresolved copy uses its live edge once the upstream answer lands. + /// Named starts and joins whose live edge was already known keep their original floor. + #[kio::test] + async fn a_pending_subscribe_joins_the_resolved_live_edge() { + use crate::lite::test_transport::{Log, Sent}; + + for version in [Version::Lite03, Version::Lite04, Version::Lite05, Version::Lite06] { + for unresolved in [true, false] { + for start in [None, Some(0)] { + let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); + let broadcast = origin.create_broadcast("room").unwrap(); + let mut dynamic = broadcast.requests(); + broadcast.announce(Default::default()).unwrap(); + let copy = broadcast.consume().track("video").unwrap(); + let held = copy.info().into_inner(); + assert!(held.poll_ok(&kio::Waiter::noop()).is_pending()); + let request = dynamic.requested_track().await.unwrap(); + let staged = request.stage(track::Info::timed()); + if !unresolved { + for sequence in [0, 3] { + let mut group = staged.create_group(group::Info { sequence }).unwrap(); + group.write_frame(crate::Timestamp::ZERO, b"x".as_slice()).unwrap(); + group.finish().unwrap(); + } + } + let mut staged = Some(staged); + let mut track = (!unresolved).then(|| staged.take().unwrap().commit()); + + let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); + peer_setup.set(crate::lite::Setup::default()); + let (_, goaway) = crate::goaway::Handle::new(true); + let log = Log::default(); + let publisher = Publisher::new(PublisherConfig { + runtime: crate::time::sim(), + session: crate::driver::Transport::new(SinkSession::new(log.clone())), + origin: origin.consume(), + version, + peer_setup, + goaway, + peer_hop: None, + auth: crate::auth::Link::new(false), + peer_grant: Default::default(), + client: false, + subscriptions: Default::default(), + estimate: kio::Producer::default().weak(), + }); + let msg = lite::Subscribe { + order: crate::track::Order::Ascending, + instance: None, + id: 0, + broadcast: crate::Path::from_static("room"), + track: "video".into(), + priority: 0, + max_delay: Duration::from_secs(60), + min_delay: None, + ranges: vec![lite::Range::span(start, 0, None, None)], + }; + let mut serve = + SubscribeServe::start(&publisher.shared, msg, "video", broadcast.consume()).unwrap(); + let mut writer = Writer::new( + crate::transport::boxed::SendStream::new(SinkSend::new(Log::default())), + version, + ); + if unresolved { + assert!( + serve + .poll(&publisher.shared, &mut writer, &kio::Waiter::noop()) + .is_pending() + ); + } + // The answer exposes retained history below the live edge, and advertises + // a source start below it. A join must use the newest group, not either. + let producer = match &mut track { + Some(track) => track, + None => &mut *staged.as_mut().unwrap(), + }; + producer.start_at(0).unwrap(); + for &sequence in if unresolved { &[0, 3, 7][..] } else { &[7][..] } { + let mut group = producer.create_group(group::Info { sequence }).unwrap(); + group.write_frame(crate::Timestamp::ZERO, b"x".as_slice()).unwrap(); + group.finish().unwrap(); + } + if unresolved { + track = Some(staged.take().unwrap().commit()); + } + let track = track.unwrap(); + let outcome = kio::clock::timeout( + Duration::from_millis(1), + kio::wait(|waiter| { + loop { + match ready!(serve.poll(&publisher.shared, &mut writer, waiter))? { + ControlFlow::Continue(()) => {} + ControlFlow::Break(()) => return Poll::Ready(Ok::<_, Error>(())), + } + } + }), + ) + .await; + assert!(outcome.is_err(), "the live subscription ended"); + let mut streams = std::collections::BTreeMap::<_, Vec>::new(); + for (id, sent) in log.trail() { + if let Sent::Write(bytes) = sent { + streams.entry(id).or_default().extend(bytes); + } + } + let mut sequences = streams + .values() + .map(|bytes| { + crate::coding::decode_buf(&mut bytes.as_slice(), version, |r, v| { + assert_eq!(lite::DataType::decode(r, v)?, lite::DataType::Group); + Ok(lite::Group::decode(r, v)?.sequence) + }) + .unwrap() + }) + .collect::>(); + sequences.sort(); + let expected = if start.is_some() || version.resolves_start() { + vec![0, 3, 7] + } else if unresolved { + vec![7] + } else { + vec![3, 7] + }; + assert_eq!( + sequences, expected, + "{version}, unresolved={unresolved}, start={start:?}" + ); + track.abort(Error::Cancel).unwrap(); + } + } + } + } + /// SUBSCRIBE_UPDATE is not a close: an update arriving while the subscription /// resolves keeps it waiting, and the latest applies once it starts. Its wanted group /// is state each update restates: past the largest it joins, and once that group diff --git a/rs/moq-net/tests/integration/range_anchors.rs b/rs/moq-net/tests/integration/range_anchors.rs index 8f3ead08c41..f4c5e1b42c5 100644 --- a/rs/moq-net/tests/integration/range_anchors.rs +++ b/rs/moq-net/tests/integration/range_anchors.rs @@ -156,7 +156,7 @@ async fn every_wire_carries_the_live_and_oldest_starts() { let mut oldest = read(Some(version), Range::oldest()).await.unwrap(); oldest.sort_unstable(); let want: &[u64] = match *version { - "moq-lite-05" | "moq-transport-18" | "moq-transport-22" => &[4], + "moq-lite-03" | "moq-lite-04" | "moq-lite-05" | "moq-transport-18" | "moq-transport-22" => &[4], _ => &[0, 1, 2, 3, 4], }; assert_eq!(oldest, want, "{version}: oldest reads every group its start reaches"); From 0942320d071033f84c969e4a652008f1c5afd63f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 04:44:30 -0800 Subject: [PATCH 03/25] fix(net): preserve lite01/02 joins and align live-edge expectations Co-Authored-By: GPT-6.1 Sol --- quest/m1/relay-inflight-replay.md | 12 +++++++----- rs/moq-net/src/lite/publisher.rs | 5 ++++- rs/moq-net/tests/integration/widened_max_age.rs | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index fad30e6b997..07cfedca707 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -33,11 +33,14 @@ pipeline-requests epic, so this sits at the m1 top level. Public API: none. Wire: none. Implementation checkpoint: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737) -removes the confirmation-time version guard and adds a controlled held-answer test. +extends the confirmation-time lookup to lite03/04 and adds a controlled held-answer test. The test exposes cached groups 0, 3, and 7 with source start 0, and checks the encoded group streams. Lite03/04 join at 7; explicit starts and arrival floors remain covered, with lite05/06 controls. The range-anchor integration expectation -now includes lite03/04 among the versions whose unfloored copy joins live. +and budget integration expectations now include lite03/04 among the versions +whose unfloored copy joins live. Lite01/02 retain their existing behavior. +`just rs test -p moq-net --no-fail-fast` passes all 3,343 selected tests +(13 skipped by the standard profile). ## Waiting @@ -45,9 +48,8 @@ now includes lite03/04 among the versions whose unfloored copy joins live. The live-edge fix makes `moq-hls::integration server::a_cursor_records_a_publisher_without_fetch` time out with `moq-lite-03: no segment recorded`; the same test passes with - `origin/main`'s publisher implementation. The normal test first reports this - on lite01; a diagnostic run limited to lite03 confirms that restricting the - fix to lite03/04 would not avoid it. `Demand::wanted` includes queries, and + `origin/main`'s publisher implementation. A diagnostic run limited to lite03 + confirmed this before the fix was restricted to lite03/04. `Demand::wanted` includes queries, and pre05 copies expose synthetic info before their upstream SUBSCRIBE answer, so neither signal establishes that a media subscription can retain the recorder's first segment. Options: (1) preserve the settled live-edge join diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index c7e901f8c73..2c1af47d615 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2256,7 +2256,10 @@ impl Request for SubscribeServe { ietf, }; - let latest = latest.or_else(|| track.latest_sequence()); + let latest = match shared.version { + Version::Lite01 | Version::Lite02 => latest, + _ => latest.or_else(|| track.latest_sequence()), + }; let floor = cursor_floor(shared.version, bounds.groups().start(), latest); let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); if let Some(update) = update { diff --git a/rs/moq-net/tests/integration/widened_max_age.rs b/rs/moq-net/tests/integration/widened_max_age.rs index 243ac580130..e9aacd5e61e 100644 --- a/rs/moq-net/tests/integration/widened_max_age.rs +++ b/rs/moq-net/tests/integration/widened_max_age.rs @@ -135,8 +135,8 @@ async fn received(subscriber: &mut track::Subscriber) -> BTreeSet { #[kio::test] async fn a_fresh_reader_fills_nothing_below_its_start() { for (version, want) in [ - ("moq-lite-04", 6..GROUPS), // Starts at the live edge. + ("moq-lite-04", GROUPS - 1..GROUPS), ("moq-lite-05", GROUPS - 1..GROUPS), ("moq-lite-06", 6..GROUPS), ("moq-lite-07-wip", 6..GROUPS), From 6babd67f12405201b1c44f7a7a51b55bfb83715c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 04:45:49 -0800 Subject: [PATCH 04/25] docs(net): clarify the legacy unresolved join floor Co-Authored-By: GPT-6.1 Sol --- rs/moq-net/src/lite/publisher.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 2c1af47d615..848b301a61f 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -121,9 +121,10 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// must not read as a request to replay the whole cache on join. /// /// `latest` is the newest group when the SUBSCRIBE arrived, not once the subscription -/// resolved: anything written in between is newer, so it is delivered. A SUBSCRIBE on -/// a copy still fetching upstream (or alongside its TRACK on lite-05) can arrive before -/// any group is known: the newest once it resolves stands in, as for one sent after. +/// resolved: anything written in between is newer, so it is delivered. On lite03-05, a +/// SUBSCRIBE on a copy still fetching upstream (or alongside its TRACK on lite-05) can +/// arrive before any group is known: the newest once it resolves stands in, as for one +/// sent after. Lite01/02 keep a zero floor when no group was known on arrival. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; From 9d239c61855219edc7f81f85800241f948ba410d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 13:48:36 -0800 Subject: [PATCH 05/25] fix(net): floor a pending pre-06 join at the newest group as of its request A lite-03/04/05 SUBSCRIBE on a track still resolving read the newest group when its subscription was confirmed, which also counted groups written after the answer, including those written for its own demand. The model now snapshots the newest group per request: the track's newest then once it resolved, or else what its answer held (a committed copy's newest at commit, a front's serving copy as of the front's own request for it). A front now splices its copy before accepting the logical track, so a subscription that resolves with the accept finds it. JS records the same snapshot on each reader as its info resolves. Records the 2026-11-03 maintainer decision in the quest. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 17 ++++++- js/net/src/lite/publisher.test.ts | 36 +++++++++++--- js/net/src/lite/publisher.ts | 9 ++-- js/net/src/resume.ts | 2 +- js/net/src/track.ts | 18 +++++-- quest/m1/relay-inflight-replay.md | 51 ++++++++----------- rs/moq-net/src/lite/publisher.rs | 32 ++++++++---- rs/moq-net/src/model/origin.rs | 10 ++-- rs/moq-net/src/model/resume.rs | 33 ++++++++---- rs/moq-net/src/model/track.rs | 83 ++++++++++++++++++++++++++++++- 10 files changed, 217 insertions(+), 74 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index c2b21129ee1..91f10cb79dc 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -259,8 +259,11 @@ export interface ResumeShell { readonly closed: GetPromise; /** The properties the first copy committed, if any yet. */ info(): Info | undefined; - /** Commit the logical track's properties, once. */ - commit(info: Info): void; + /** + * Commit the logical track's properties, once, with the newest group the committing copy + * found as of its request (see `hooks.requestedSequence`). + */ + commit(info: Info, requested: number | undefined): void; /** End the logical track: cleanly, or with `abort`. */ close(abort?: Error): void; } @@ -516,6 +519,13 @@ export const hooks: { tailGrace: (producer: Producer) => Milli; /** Snapshot the latest group sequence for wire positioning; assigned by `track.ts`. */ latestSequence: (subscriber: Subscriber) => number | undefined; + /** + * The newest group as of the subscription's request, once its info resolved: the track's + * newest then, or, for a track still resolving then, the newest its answer held. Nothing + * written since counts, even in response to the subscription's own demand. Assigned by + * `track.ts`. + */ + requestedSequence: (subscriber: Subscriber) => number | undefined; /** * The source's declared start, raised to the subscriber's cursor floor, or `"pending"` while * the upstream START that resolves it has yet to arrive; assigned by `track.ts`. @@ -778,6 +788,9 @@ export const hooks: { latestSequence: () => { throw new Error("track.ts not loaded"); }, + requestedSequence: () => { + throw new Error("track.ts not loaded"); + }, sourceStart: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index ed80f6d2fec..ea12297056e 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -780,11 +780,17 @@ async function servedSubscription( serving, parked: gate.parked, release: gate.release, - // Resolve a pending track with the publisher's request for it. - async accept() { + // Resolve a pending track with the publisher's request for it, answering with `held`. + async accept(held: number[] = []) { const pulled = await request; if (!pulled) throw new Error("the publisher never requested the track"); - track = pulled.accept(info); + const staged = hooks.stageRequest(pulled, info); + for (const sequence of held) { + const group = staged.producer.createGroup(sequence); + for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(sequence))); + group.finish(); + } + track = staged.commit(); }, // Stamped by sequence, so a group served out of order still starts in order. serve(sequence: number) { @@ -1401,10 +1407,11 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", }); // A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching -// upstream. Groups the track holds once it does are older than the join, so an absent Group -// Start is still the latest group. +// upstream. An absent Group Start is still the newest group as of the request, which the +// answer gives: groups written once it resolved are newer, even those written for this +// subscription's demand, so they are delivered. test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_05])( - "lite %s: a subscribe that beats its track resolving joins at the latest group", + "lite %s: a subscribe that beats its track resolving delivers what follows the answer", async (version) => { const sub = await servedSubscription({ version, pending: true }); try { @@ -1412,7 +1419,24 @@ test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_05])( sub.serve(0); sub.serve(1); sub.serve(2); + // Headers only: frames before lite-05 carry no timestamp for `servedGroup` to read. + expect(await sub.servedHeader()).toBe(0); + expect(await sub.servedHeader()).toBe(1); expect(await sub.servedHeader()).toBe(2); + } finally { + await sub.close(); + } + }, +); + +// The answer's own groups are history as of the request: the join starts at its newest. +test.each([Version.DRAFT_03, Version.DRAFT_04, Version.DRAFT_05])( + "lite %s: a subscribe that beats its track resolving joins at the answer's newest group", + async (version) => { + const sub = await servedSubscription({ version, pending: true }); + try { + await sub.accept([0, 3, 7]); + expect(await sub.servedHeader()).toBe(7); expect(await sub.servedSequence()).toBeUndefined(); } finally { await sub.close(); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index bf59ef19506..63a0c7a4587 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -967,10 +967,11 @@ export class Publisher { const info = await untilAborted(track.info(), left.signal); const publisher = info.priority; // A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a - // copy still fetching upstream, or on lite-05 one sent alongside its TRACK, when no - // group is known yet: the newest once it resolves stands in, as it would for one - // sent after. - const resolved = cursorFloor(this.version, msg.startGroup, hooks.latestSequence(track)); + // copy still fetching upstream, an on-demand track, or on lite-05 one sent alongside + // its TRACK, when no group is known yet. The newest as of the request is then the + // newest the answer held, or the first group when it held none: cached history below + // it is skipped, and anything written since, even for this demand, is delivered. + const resolved = cursorFloor(this.version, msg.startGroup, hooks.requestedSequence(track)); if (latest === undefined && resolved !== floor) { floor = resolved; track.withRange({ start: { included: floor } }); diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 9c9af64eea6..97eebaaf843 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -456,7 +456,7 @@ class Reader implements ResumeSource { copy.sub.info().then( (info) => { record(info, source); - shell.commit(info); + shell.commit(info, hooks.requestedSequence(copy.sub)); }, () => {}, ); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index f429235ce5a..3b602032c20 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -660,6 +660,9 @@ class TrackState { update: Signal; /** Resolved once the producer commits the immutable properties. */ info = new Signal(undefined); + // The newest group as of this reader's request, recorded as {@link info} resolves: the + // track's newest then, or what its answer held for a request that beat it. `null` for none. + requested?: number | null; /** Newest group head, datagram, frame, or location reported by the publisher. */ largest = new Signal(undefined); // The group {@link largest} first named, where a relative anchor resolved before any group @@ -1101,6 +1104,8 @@ export class Producer { throw err; } this.#state.info.set(resolved); + // What the answer holds, written before accept, is where a sink that beat it joins. + const requested = this.#state.latest ?? null; // Propagate to any sink handed out before accept (the on-demand path). const compressed = new Map(); for (const sink of this.#sinks) { @@ -1117,6 +1122,7 @@ export class Producer { } }); } + sink.requested ??= requested; sink.info.set(resolved); } this.#updateSubscription(); @@ -1169,7 +1175,10 @@ export class Producer { #addSink(sink: TrackState): void { sink.producer = this; const info = this.#state.info.peek(); - if (info) sink.info.set(info); + if (info) { + sink.requested = this.#state.latest ?? null; + sink.info.set(info); + } sink.largest.set(this.#state.largest.peek()); sink.first = this.#state.largest.peek()?.group; sink.largestEnded.set(this.#state.largestEnded.peek()); @@ -2611,8 +2620,10 @@ export class Subscriber { cursor: subscriber.#cursor, closed: state.closed, info: () => state.info.peek(), - commit: (info) => { - if (state.info.peek() === undefined) state.info.set(info); + commit: (info, requested) => { + if (state.info.peek() !== undefined) return; + state.requested = requested ?? null; + state.info.set(info); }, close: (abort) => { closeTrackState(state, abort); @@ -2622,6 +2633,7 @@ export class Subscriber { }; hooks.latestSequence = (subscriber) => subscriber.#resume ? subscriber.#resume.latest() : subscriber.#state.latest; + hooks.requestedSequence = (subscriber) => subscriber.#state.requested ?? undefined; hooks.bufferedLargest = (subscriber) => subscriber.#bufferedLargest(); hooks.objectLargest = (subscriber) => { if (subscriber.#resume) return subscriber.#resume.objectLargest(); diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index 07cfedca707..127f968a9b8 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -32,34 +32,23 @@ pipeline-requests epic, so this sits at the m1 top level. Public API: none. Wire: none. -Implementation checkpoint: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737) -extends the confirmation-time lookup to lite03/04 and adds a controlled held-answer test. -The test exposes cached groups 0, 3, and 7 with source start 0, and checks the -encoded group streams. Lite03/04 join at 7; explicit starts and arrival floors -remain covered, with lite05/06 controls. The range-anchor integration expectation -and budget integration expectations now include lite03/04 among the versions -whose unfloored copy joins live. Lite01/02 retain their existing behavior. -`just rs test -p moq-net --no-fail-fast` passes all 3,343 selected tests -(13 skipped by the standard profile). - -## Waiting - -- Maintainer: settle the HLS recorder startup contract before this behavior lands. - The live-edge fix makes - `moq-hls::integration server::a_cursor_records_a_publisher_without_fetch` - time out with `moq-lite-03: no segment recorded`; the same test passes with - `origin/main`'s publisher implementation. A diagnostic run limited to lite03 - confirmed this before the fix was restricted to lite03/04. `Demand::wanted` includes queries, and - pre05 copies expose synthetic info before their upstream SUBSCRIBE answer, - so neither signal establishes that a media subscription can retain the - recorder's first segment. Options: (1) preserve the settled live-edge join - and make HLS recording request an explicit media floor derived from its - records (recommended), or (2) revisit how a pending join distinguishes cached - history from media produced in response to its demand. Do not weaken the - recorder assertion or add a timing wait. Complete the source fix, rerun - `just ci`, and run all-language interop if this extends into the gateway. - The twelve requested lite03/04 Rust/browser-JS publisher by Rust/Node/Bun - subscriber matrix cells passed. The broader interop invocation also runs - finite-finish and CLI FETCH cases that failed on these old versions; their - applicability and base behavior still need verification before claiming - that broader suite passes. +Decided 2026-11-03 (maintainer, /quest-unblock): ✅ **A pending join floors at +request time.** A lite-03/04 SUBSCRIBE on a still-resolving copy floors at the +newest group as of the SUBSCRIBE's arrival, which is the upstream's answer, or +the first group when none existed then. It does not use the local cache's newest +group when the copy later resolves. A group produced after the request, +including in response to its demand, is delivered, and cached history before +the request is skipped. This is a relay rule that covers HLS recording and every +other late-resolving consumer. + +- Not chosen: the HLS recorder requesting its own floor; waiting on + [moq-dev/moq#7652](https://github.com/moq-dev/moq/pull/7652). +- `rs/moq-hls/tests/integration/server.rs` + `a_cursor_records_a_publisher_without_fetch` must pass unchanged, with no + weakened assertion or timing wait. The controlled held-answer test (cached + groups 0, 3 and 7 join at 7) keeps passing. Apply the same rule to lite-05+ + (`SubscribeServe`) and JS where they share the gap. +- Rerun `just ci`. Check whether the old-version finite-finish and CLI FETCH + interop failures reproduce on base main, and record them as pre-existing if so. + +Implementation: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737). diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 848b301a61f..f59a3aa8676 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -122,9 +122,12 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// /// `latest` is the newest group when the SUBSCRIBE arrived, not once the subscription /// resolved: anything written in between is newer, so it is delivered. On lite03-05, a -/// SUBSCRIBE on a copy still fetching upstream (or alongside its TRACK on lite-05) can -/// arrive before any group is known: the newest once it resolves stands in, as for one -/// sent after. Lite01/02 keep a zero floor when no group was known on arrival. +/// SUBSCRIBE on a copy still resolving (fetching upstream, served on request, or alongside +/// its TRACK on lite-05) learns that newest group from the answer: the newest the copy held +/// once it resolved ([`track::Subscriber::requested_sequence`]), or the first group when it +/// held none. Cached history below it is skipped, and anything written after it, even in +/// response to this subscription's demand, is delivered. Lite01/02 keep a zero floor when no +/// group was known on arrival. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -2259,7 +2262,7 @@ impl Request for SubscribeServe { let latest = match shared.version { Version::Lite01 | Version::Lite02 => latest, - _ => latest.or_else(|| track.latest_sequence()), + _ => latest.or_else(|| track.requested_sequence()), }; let floor = cursor_floor(shared.version, bounds.groups().start(), latest); let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); @@ -8799,14 +8802,17 @@ mod tests { } } - /// A join on an unresolved copy uses its live edge once the upstream answer lands. - /// Named starts and joins whose live edge was already known keep their original floor. + /// A join on an unresolved copy floors at the newest group as of its request, which the + /// answer gives: history the answer holds is skipped, and groups written after it, even + /// before the subscription is served, are delivered. Named starts and joins whose live + /// edge was already known keep their original floor. #[kio::test] - async fn a_pending_subscribe_joins_the_resolved_live_edge() { + async fn a_pending_subscribe_joins_where_the_answer_stood() { use crate::lite::test_transport::{Log, Sent}; for version in [Version::Lite03, Version::Lite04, Version::Lite05, Version::Lite06] { - for unresolved in [true, false] { + // Unresolved on arrival: whether the answer holds the groups, or they follow it. + for (unresolved, cached) in [(true, true), (true, false), (false, false)] { for start in [None, Some(0)] { let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); let broadcast = origin.create_broadcast("room").unwrap(); @@ -8871,6 +8877,10 @@ mod tests { } // The answer exposes retained history below the live edge, and advertises // a source start below it. A join must use the newest group, not either. + // An answer without them is followed by them, before the join is served. + if unresolved && !cached { + track = Some(staged.take().unwrap().commit()); + } let producer = match &mut track { Some(track) => track, None => &mut *staged.as_mut().unwrap(), @@ -8881,7 +8891,7 @@ mod tests { group.write_frame(crate::Timestamp::ZERO, b"x".as_slice()).unwrap(); group.finish().unwrap(); } - if unresolved { + if cached { track = Some(staged.take().unwrap().commit()); } let track = track.unwrap(); @@ -8915,7 +8925,7 @@ mod tests { }) .collect::>(); sequences.sort(); - let expected = if start.is_some() || version.resolves_start() { + let expected = if start.is_some() || version.resolves_start() || (unresolved && !cached) { vec![0, 3, 7] } else if unresolved { vec![7] @@ -8924,7 +8934,7 @@ mod tests { }; assert_eq!( sequences, expected, - "{version}, unresolved={unresolved}, start={start:?}" + "{version}, unresolved={unresolved}, cached={cached}, start={start:?}" ); track.abort(Error::Cancel).unwrap(); } diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index bbf3a98691c..a180bbc7f96 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -3257,14 +3257,16 @@ struct TrackIo { } impl TrackIo { - /// Serve the track from `asked`'s copy, accepting it with that copy's info first. + /// Serve the track from `asked`'s copy, accepting it with that copy's info. Spliced + /// first, so a subscription that resolves with the accept finds the copy it joins. fn splice(&mut self, asked: Asked) { self.ready.wake_by_ref(); + let info = asked.info.clone(); + let resolved = asked.query.requested_sequence(); + self.routes.splice(asked.copy.clone(), asked.staged, resolved); if let Some(request) = self.request.take() { - let info = asked.info.clone().unwrap_or_default(); - self.accepted = Some(request.accept(info)); + self.accepted = Some(request.accept(info.unwrap_or_default())); } - self.routes.splice(asked.copy.clone(), asked.staged); self.copy = Some((asked.source, asked.copy, asked.query)); self.held = asked.sub; } diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 0fb11665f3a..5208f3d061b 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -74,6 +74,9 @@ struct Route { staged: Option<(u64, track::Consumer)>, /// The stage the serving copy was spliced from, if any. spliced: Option, + /// The newest sequence as of the front's request for the serving copy, where a + /// subscription made before the track resolved joins; see [`track::Subscriber::requested_sequence`]. + resolved: Option, /// The id the next stage takes. next_stage: u64, } @@ -192,18 +195,21 @@ impl Producer { /// Serve the track from `copy` from now on, subscribing every reader to it. #[cfg(test)] pub(super) fn serve(&self, copy: track::Consumer) { - self.serve_from(copy, None); + let resolved = copy.latest_sequence(); + self.serve_from(copy, None, resolved); } /// Serve the track from `copy`, the one `staged` was made for, subscribing every reader - /// to it: what was fetched from it is handed out from here on. - pub(super) fn splice(&self, copy: track::Consumer, staged: Staged) { - self.serve_from(copy, Some(staged.id)); + /// to it: what was fetched from it is handed out from here on. `resolved` is the newest + /// sequence as of the front's request for it. + pub(super) fn splice(&self, copy: track::Consumer, staged: Staged, resolved: Option) { + self.serve_from(copy, Some(staged.id), resolved); } - fn serve_from(&self, copy: track::Consumer, stage: Option) { + fn serve_from(&self, copy: track::Consumer, stage: Option, resolved: Option) { if let Ok(mut route) = self.state.write() { route.generation += 1; + route.resolved = resolved; route.copy = Some(copy); route.parked = None; route.spliced = stage; @@ -317,6 +323,11 @@ impl Consumer { self.state.read().copy.clone() } + /// The newest sequence as of the front's request for the serving copy, as it was spliced. + pub(super) fn resolved(&self) -> Option { + self.state.read().resolved + } + /// The copy the front parked, while it serves none and may still serve one again. pub(super) fn parked(&self) -> Option { let route = self.state.read(); @@ -510,9 +521,9 @@ struct Copy { feeds: bool, } -// Boxed: a subscriber is twice a pending answer, and a copy holds one for its whole life. +// Both boxed: either is large, and a copy holds one for its whole life. enum Sub { - Pending(kio::Pending), + Pending(Box>), Ready(Box), } @@ -1265,7 +1276,7 @@ impl Reader { let wire = self.mirrored.clamped(floor, None, &resolved); self.copies.push(Copy { generation, - sub: Sub::Pending(track.subscribe(wire)), + sub: Sub::Pending(Box::new(track.subscribe(wire))), track, floor, from: self.mirrored.ranges.clone(), @@ -1979,7 +1990,7 @@ impl Reader { let wire = self.mirrored.clamped(floor, Some(lowered.end), &resolved); let copy = Copy { generation, - sub: Sub::Pending(track.subscribe(wire)), + sub: Sub::Pending(Box::new(track.subscribe(wire))), floor, from: self.mirrored.ranges.clone(), resolved, @@ -3502,7 +3513,7 @@ mod test { ); let _logical = logical.accept(crate::track::Info::timed()); - routes.splice(copy.consume(), staged); + routes.splice(copy.consume(), staged, None); let mut fetched = fetching.now_or_never().expect("spliced").unwrap(); assert_eq!(read(&mut fetched), Some(b"early".to_vec())); } @@ -3539,7 +3550,7 @@ mod test { group.write_frame(ts(0), b"right".as_ref()).unwrap(); group.finish().unwrap(); let _logical = logical.accept(crate::track::Info::timed()); - routes.splice(copy.consume(), staged); + routes.splice(copy.consume(), staged, None); let mut fetched = fetching.now_or_never().expect("spliced").unwrap(); assert_eq!(read(&mut fetched), Some(b"right".to_vec())); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c1f90a1c3aa..d8ddcd55f30 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -412,6 +412,10 @@ pub(crate) struct TrackState { // relative starts count back from (see `latest_live`). newest_produced: Option, + // `max_sequence` when waiting subscribers first resolved, or `None` before then; see + // `TrackState::request_answered`. + resolved: Option>, + // The checked starts cached groups hold, by sequence (see [`group::Origin::Bound`]), so a // first frame finds its neighbors without walking groups not yet stamped. Filled through // the read guard a first frame holds, so it needs its own lock, never contended. @@ -1191,6 +1195,7 @@ impl TrackState { fn commit(&mut self) { debug_assert_eq!(self.publication, Publication::Staged, "only a staged track commits"); self.publication = Publication::Published; + self.resolve(); } /// Mint the Producer and resolve waiting subscribers at once. @@ -2164,6 +2169,51 @@ impl TrackState { if self.info.replace(info).is_some() { self.cache.wakes().wake_all(); } + if self.publication != Publication::Staged { + self.resolve(); + } + } + + /// Record the newest sequence as waiting subscribers resolve (see [`Self::poll_info`]), + /// the first time they do. + fn resolve(&mut self) { + self.resolved.get_or_insert(self.max_sequence); + } + + /// The newest sequence as of a request made now: `Some` with it (`None` within for none) + /// once the track resolved, or `None` while it still resolves, when [`Self::request_answered`] + /// says once it does. A logical track reads the copy serving it, or the parked one. + fn requested(state: &kio::Consumer) -> Option> { + let (ready, max, routes) = { + let state = state.read(); + (state.poll_info().is_ready(), state.max_sequence, state.routes.clone()) + }; + if !ready { + return None; + } + // Read outside the track's lock: the copy has a lock of its own. + Some(match routes { + Some(routes) => routes + .serving() + .or_else(|| routes.parked()) + .and_then(|copy| copy.latest_sequence()), + None => max, + }) + } + + /// The newest sequence a request made while the track was still resolving finds in its + /// answer: what the track held once it resolved, or a logical track's serving copy as of + /// the front's own request for it. Nothing written after the answer counts, even in + /// response to the request's own demand: it is newer, so it is delivered. + fn request_answered(state: &kio::Consumer) -> Option { + let (resolved, routes) = { + let state = state.read(); + (state.resolved.flatten(), state.routes.clone()) + }; + match routes { + Some(routes) => routes.resolved(), + None => resolved, + } } /// Create the shared state for a track under `broadcast`, along with the cache @@ -4086,6 +4136,7 @@ impl Producer { // subscriber surfaces the close/abort on its first read; the preferences are // simply never registered (nothing aggregates them anymore). let info = self.info.clone(); + let requested = self.state.read().max_sequence; let subscription = Preferences::register(self.state.read(), preferences); // Hoisted: an inline `read()` guard would live to the end of the struct literal, @@ -4096,6 +4147,7 @@ impl Producer { broadcast, info, inner: Inner::Plain(Cursor::new(self.state.consume(), subscription)), + requested, delivered: Delivered::default(), // A producer-side (in-process) subscribe is not egress: stay untagged. stats: stats::Scope::default(), @@ -4981,6 +5033,7 @@ impl Consumer { kio::Pending::new(Subscribing { name: self.name.clone(), broadcast: self.broadcast.clone(), + requested: TrackState::requested(&state), state, subscription, stats: self.stats.clone(), @@ -5405,10 +5458,12 @@ impl Consumer { /// for a relay), and errors with the track's abort error if it closes first. /// [`Subscriber::info`] is the already-resolved counterpart. pub fn info(&self) -> kio::Pending { + let state = self.resolve(None).0; kio::Pending::new(Resolving { name: self.name.clone(), broadcast: self.broadcast.clone(), - state: self.resolve(None).0, + requested: TrackState::requested(&state), + state, stats: self.stats.clone(), }) } @@ -5521,6 +5576,8 @@ pub struct Subscribing { name: Arc, broadcast: Arc, state: kio::Consumer, + /// The newest sequence when requested, if the track had resolved; see [`TrackState::requested`]. + requested: Option>, subscription: Preferences, stats: stats::Scope, } @@ -5532,6 +5589,7 @@ impl Subscribing { name: self.name.clone(), broadcast: self.broadcast.clone(), state: self.state.clone(), + requested: self.requested, stats: self.stats.clone(), } } @@ -5560,6 +5618,9 @@ impl Subscribing { broadcast: self.broadcast.clone(), info, inner, + requested: self + .requested + .unwrap_or_else(|| TrackState::request_answered(&self.state)), delivered: Delivered::default(), stats: self.stats.clone(), _stats_sub: self.stats.subscribe(), @@ -5596,6 +5657,8 @@ pub struct Resolving { name: Arc, broadcast: Arc, state: kio::Consumer, + /// The newest sequence when requested, if the track had resolved; see [`TrackState::requested`]. + requested: Option>, stats: stats::Scope, } @@ -5618,6 +5681,14 @@ impl Resolving { .map_err(|e| e.abort.clone().unwrap_or(Error::Dropped))??; Poll::Ready(Ok(info)) } + + /// The newest sequence as of this request, once [`Self::poll_ok`] is ready; see + /// [`Subscriber::requested_sequence`]. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn requested_sequence(&self) -> Option { + self.requested + .unwrap_or_else(|| TrackState::request_answered(&self.state)) + } } impl kio::Task for Resolving { @@ -5929,6 +6000,8 @@ pub struct Subscriber { broadcast: Arc, info: Info, inner: Inner, + /// The newest sequence as of the request; see [`Self::requested_sequence`]. + requested: Option, /// The groups handed out, so none is handed out twice: not when its sequence is /// re-created, nor when another route delivers it too. delivered: Delivered, @@ -9102,6 +9175,14 @@ impl Subscriber { } } + /// The newest sequence as of this subscription's request, `None` when there was none; + /// see [`TrackState::requested`]. Unlike [`Self::latest_sequence`], nothing written since + /// counts, even in response to this subscription's own demand. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn requested_sequence(&self) -> Option { + self.requested + } + /// Join again at the live edge, judged by [`Info::refresh`] like a new subscriber: a /// moq-transport NEW_GROUP_REQUEST. A front's reader joins the copy serving it. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] From 2cce053d597754112a8158ded02218de0b4e98b1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 14:51:56 -0800 Subject: [PATCH 06/25] fix(net): keep a subscription's request snapshot off Subscribing Storing it on `Subscribing` grew the public type past clippy's large variant threshold for every enum holding one (moq-stats among them). It now rides on the subscription's own preferences, which are already shared per request. Also records the maintainer decision this rule now needs: the HLS recorder test cannot pass under it unchanged. Co-Authored-By: Claude Opus 5.5 --- quest/m1/relay-inflight-replay.md | 25 ++++++++++++++++++++++++- rs/moq-net/src/model/resume.rs | 8 ++++---- rs/moq-net/src/model/track.rs | 11 ++++++----- rs/moq-net/src/model/track/demand.rs | 13 +++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index 127f968a9b8..3632a69af03 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -51,4 +51,27 @@ other late-resolving consumer. - Rerun `just ci`. Check whether the old-version finite-finish and CLI FETCH interop failures reproduce on base main, and record them as pre-existing if so. -Implementation: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737). +Implementation: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737) +implements this rule in Rust (`SubscribeServe` for lite-03 through lite-05, +the track model, and a front's splice) and JS. The rule holds in every +`moq-net` and JS test, but the HLS recorder test still fails; see Waiting. + +## Waiting + +- Maintainer: the decided rule cannot pass + `a_cursor_records_a_publisher_without_fetch` unchanged. A trace of the + lite-03 run shows the publisher writes media groups 0 to 2 as soon as + moq-mux's `hls::Lend` subscribes to `video0` for the records track + (`video0.hls`). That is before the recorder's media subscription exists on + either session. The recorder's lite-03 media SUBSCRIBE then reaches the + publisher with group 2 already its newest, so the rule floors it at 2 and + segment 0 is never sent. Base main passes only because it floors a pending + logical track at 0, which is the cache replay this quest removes. Options: + (1) the recorder names its floor: moq-hls subscribes to media from the group + its first wanted record names, so the SUBSCRIBE carries a Group Start + (recommended; nothing then depends on when the SUBSCRIBE arrives); + (2) apply the rule only to received copies (an in-flight upstream request), + so a publisher serving its own producer through a pending front keeps base + main's zero floor, which still replays a local cache to a first pre-05 + subscriber; (3) relax the recorder test's expectation for lite-03/04. + Check: the chosen option passes the recorder test and the held-answer test. diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 3476e311a54..bde68493681 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -521,9 +521,9 @@ struct Copy { feeds: bool, } -// Both boxed: either is large, and a copy holds one for its whole life. +// Boxed: a subscriber is twice a pending answer, and a copy holds one for its whole life. enum Sub { - Pending(Box>), + Pending(kio::Pending), Ready(Box), } @@ -1285,7 +1285,7 @@ impl Reader { let wire = self.mirrored.clamped(floor, None, &resolved); self.copies.push(Copy { generation, - sub: Sub::Pending(Box::new(track.subscribe(wire))), + sub: Sub::Pending(track.subscribe(wire)), track, floor, from: self.mirrored.ranges.clone(), @@ -1999,7 +1999,7 @@ impl Reader { let wire = self.mirrored.clamped(floor, Some(lowered.end), &resolved); let copy = Copy { generation, - sub: Sub::Pending(Box::new(track.subscribe(wire))), + sub: Sub::Pending(track.subscribe(wire)), floor, from: self.mirrored.ranges.clone(), resolved, diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 238321f7ab1..01b6835ed6c 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5049,11 +5049,13 @@ impl Consumer { pub fn subscribe(&self, subscription: impl Into>) -> kio::Pending { let (state, subscription) = self.resolve(Some(&subscription.into().unwrap_or_default())); let subscription = subscription.expect("a subscription was requested"); + if let Some(newest) = TrackState::requested(&state) { + subscription.set_requested(newest); + } kio::Pending::new(Subscribing { name: self.name.clone(), broadcast: self.broadcast.clone(), - requested: TrackState::requested(&state), state, subscription, stats: self.stats.clone(), @@ -5599,8 +5601,6 @@ pub struct Subscribing { name: Arc, broadcast: Arc, state: kio::Consumer, - /// The newest sequence when requested, if the track had resolved; see [`TrackState::requested`]. - requested: Option>, subscription: Preferences, stats: stats::Scope, } @@ -5612,7 +5612,7 @@ impl Subscribing { name: self.name.clone(), broadcast: self.broadcast.clone(), state: self.state.clone(), - requested: self.requested, + requested: self.subscription.requested(), stats: self.stats.clone(), } } @@ -5642,7 +5642,8 @@ impl Subscribing { info, inner, requested: self - .requested + .subscription + .requested() .unwrap_or_else(|| TrackState::request_answered(&self.state)), delivered: Delivered::default(), stats: self.stats.clone(), diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index d7ed553ead5..f1bad8b5870 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -209,6 +209,9 @@ struct Preference { subscriptions: Option>, /// Bumped under the write whenever the ranges change. ranges_revision: AtomicU64, + /// The track's newest sequence when the subscription was requested, set only if the track + /// had resolved by then; see `TrackState::requested`. + requested: std::sync::OnceLock>, } impl Preferences { @@ -223,6 +226,7 @@ impl Preferences { value: kio::Producer::new(value), subscriptions, ranges_revision: AtomicU64::new(0), + requested: Default::default(), })) } @@ -263,6 +267,14 @@ impl Preferences { pub(in crate::model) fn consume(&self) -> kio::Consumer { self.0.value.consume() } + /// Record the track's newest sequence as the subscription is requested, once. + pub(in crate::model) fn set_requested(&self, newest: Option) { + let _ = self.0.requested.set(newest); + } + /// The track's newest sequence when the subscription was requested, if it had resolved. + pub(in crate::model) fn requested(&self) -> Option> { + self.0.requested.get().copied() + } pub(in crate::model) fn poll_ref( &self, waiter: &kio::Waiter, @@ -291,6 +303,7 @@ impl From> for Preferences { value, subscriptions: None, ranges_revision: AtomicU64::new(0), + requested: Default::default(), })) } } From 3f2e01d09c9439988d965293edd5d98373c80c07 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 3 Nov 2026 15:07:45 -0800 Subject: [PATCH 07/25] docs(quest): sequence relay-inflight-replay after the HLS grid The maintainer chose to keep the request-time floor and land after quest/m0/hls-media-grid.md, which deletes the record-track subscription that captures media before the recorder's own SUBSCRIBE. Co-Authored-By: Claude Opus 5.5 --- quest/m1/relay-inflight-replay.md | 36 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index 3632a69af03..d3bd2d668df 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -9,6 +9,20 @@ for every version. It never replays the cache from group 0. ## Plan +Decided 2026-11-03 (maintainer, /quest-complete): ✅ land after +[HLS export on a declared grid](/quest/m0/hls-media-grid.md). The rule +(a pending join floors at request time) stays as implemented in +[moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737). The one failing +test, `a_cursor_records_a_publisher_without_fetch`, fails because +moq-mux's `hls::Lend` subscribes to `video0` to build the `video0.hls` record +track before the recorder's media SUBSCRIBE exists, so groups 0 to 2 predate +that request. The HLS grid quest deletes the `.hls` record tracks and has +the recorder read media tracks directly, which removes that early subscription. +Not chosen: the recorder naming a floor from its records (deleted with the +records); applying the rule only to upstream copies (still replays a local +cache to a first pre-05 subscriber); relaxing the test. Once the HLS grid lands, +merge main and confirm the recorder test passes unchanged. + Decided 2026-10-31 (user, /quest-plan of the follow-ups from the 2026-10-31 /quest-complete run), from [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328). @@ -54,24 +68,8 @@ other late-resolving consumer. Implementation: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737) implements this rule in Rust (`SubscribeServe` for lite-03 through lite-05, the track model, and a front's splice) and JS. The rule holds in every -`moq-net` and JS test, but the HLS recorder test still fails; see Waiting. +`moq-net` and JS test, but the HLS recorder test still fails until the HLS grid lands; see the decision above. -## Waiting +## Required -- Maintainer: the decided rule cannot pass - `a_cursor_records_a_publisher_without_fetch` unchanged. A trace of the - lite-03 run shows the publisher writes media groups 0 to 2 as soon as - moq-mux's `hls::Lend` subscribes to `video0` for the records track - (`video0.hls`). That is before the recorder's media subscription exists on - either session. The recorder's lite-03 media SUBSCRIBE then reaches the - publisher with group 2 already its newest, so the rule floors it at 2 and - segment 0 is never sent. Base main passes only because it floors a pending - logical track at 0, which is the cache replay this quest removes. Options: - (1) the recorder names its floor: moq-hls subscribes to media from the group - its first wanted record names, so the SUBSCRIBE carries a Group Start - (recommended; nothing then depends on when the SUBSCRIBE arrives); - (2) apply the rule only to received copies (an in-flight upstream request), - so a publisher serving its own producer through a pending front keeps base - main's zero floor, which still replays a local cache to a first pre-05 - subscriber; (3) relax the recorder test's expectation for lite-03/04. - Check: the chosen option passes the recorder test and the held-answer test. +- [HLS export on a declared grid](/quest/m0/hls-media-grid.md) - removes the record-track subscription that captures media before the recorder's own SUBSCRIBE From 796bbcab471cbbcc95a65226539b42dc4586d233 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 13:37:18 -0800 Subject: [PATCH 08/25] docs(quest): complete relay inflight replay after HLS grid Co-Authored-By: GPT-6.1 Sol --- quest/m1/README.md | 1 - quest/m1/relay-inflight-replay.md | 75 ------------------------------- 2 files changed, 76 deletions(-) delete mode 100644 quest/m1/relay-inflight-replay.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 5b0beb8392f..04ad898ab2c 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -176,7 +176,6 @@ their end report, so an embedder's drain alert pages on real cuts. - [HLS export lists a same-codec reconfigure's first group under the new init](/quest/m1/hls-reinit-same-codec.md) - the hand-over pick, whichever of the catalog and the records arrives first, after the catalog `hls` removal is planned - [Ordered reads can skip owed holes, and snapshot latest() does](/quest/m1/ordered-owed-holes.md) - moq-net's `Ordered` gains a read that skips past an owed hole to the newest buffered group, while keeping the reader's bounds - [The relay reports native sessions it force-closes at the drain deadline](/quest/m1/relay-drain-cuts.md) - the drain's public outcome and live tally separate sessions cut while open from sessions only finishing their end report, so an embedder's drain alert pages on real cuts -- [A Rust relay never replays from group 0 to a lite-03/04 subscriber](/quest/m1/relay-inflight-replay.md) - A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track copy that is still resolving starts at the newest group once the copy resolves - [Every moq-mux consumer gets the catalog warmup from one entry point](/quest/m1/mux-warmup.md) - one moq-mux call widens the subscription and configures the consumer, adopted by moq-audio, moq-c, moq-ffi's raw consumer, and moqsrc - [Codex shells never load another checkout's dev environment](/quest/m1/codex-env-snapshot.md) - the Codex dev-shell snapshot is skipped when stale or inside `nix develop` - [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md deleted file mode 100644 index d3bd2d668df..00000000000 --- a/quest/m1/relay-inflight-replay.md +++ /dev/null @@ -1,75 +0,0 @@ -# [S] A Rust relay never replays from group 0 to a lite-03/04 subscriber - -## Goal - -A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track -copy that is still resolving (an in-flight upstream request) starts at the -newest group once the copy resolves, as it does for lite-05+ and as JS does -for every version. It never replays the cache from group 0. - -## Plan - -Decided 2026-11-03 (maintainer, /quest-complete): ✅ land after -[HLS export on a declared grid](/quest/m0/hls-media-grid.md). The rule -(a pending join floors at request time) stays as implemented in -[moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737). The one failing -test, `a_cursor_records_a_publisher_without_fetch`, fails because -moq-mux's `hls::Lend` subscribes to `video0` to build the `video0.hls` record -track before the recorder's media SUBSCRIBE exists, so groups 0 to 2 predate -that request. The HLS grid quest deletes the `.hls` record tracks and has -the recorder read media tracks directly, which removes that early subscription. -Not chosen: the recorder naming a floor from its records (deleted with the -records); applying the rule only to upstream copies (still replays a local -cache to a first pre-05 subscriber); relaxing the test. Once the HLS grid lands, -merge main and confirm the recorder test passes unchanged. - -Decided 2026-10-31 (user, /quest-plan of the follow-ups from the 2026-10-31 -/quest-complete run), from -[moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328). - -Milestone: ✅ m1 (moved from m2). Not chosen: m2. There is no m1 -pipeline-requests epic, so this sits at the m1 top level. - -- `cursor_floor` (`rs/moq-net/src/lite/publisher.rs:127` at be39188000) - floors a pre-06 SUBSCRIBE at `latest`, the newest group when it arrived, - and at 0 when the copy has none yet. [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328) (merged) re-reads the newest group of an unresolved track - once it resolves, but only for lite-05+ (`SubscribeServe`, the - `has_track_stream()` match at `:2259`). JS no longer replays for any - version. -- Extend the re-read to lite-03/04, so a resolved copy floors them at its - newest group, never at a lower first group it reports; match what JS - does. -- Test with a lite-03 and a lite-04 subscriber on a relay whose upstream - answers after the SUBSCRIBE, with cached groups below the live edge. - [moq-dev/moq#7328](https://github.com/moq-dev/moq/pull/7328)'s interop was not rerun after its last changes; run the lite-03/04 - cells here. - -Public API: none. Wire: none. - -Decided 2026-11-03 (maintainer, /quest-unblock): ✅ **A pending join floors at -request time.** A lite-03/04 SUBSCRIBE on a still-resolving copy floors at the -newest group as of the SUBSCRIBE's arrival, which is the upstream's answer, or -the first group when none existed then. It does not use the local cache's newest -group when the copy later resolves. A group produced after the request, -including in response to its demand, is delivered, and cached history before -the request is skipped. This is a relay rule that covers HLS recording and every -other late-resolving consumer. - -- Not chosen: the HLS recorder requesting its own floor; waiting on - [moq-dev/moq#7652](https://github.com/moq-dev/moq/pull/7652). -- `rs/moq-hls/tests/integration/server.rs` - `a_cursor_records_a_publisher_without_fetch` must pass unchanged, with no - weakened assertion or timing wait. The controlled held-answer test (cached - groups 0, 3 and 7 join at 7) keeps passing. Apply the same rule to lite-05+ - (`SubscribeServe`) and JS where they share the gap. -- Rerun `just ci`. Check whether the old-version finite-finish and CLI FETCH - interop failures reproduce on base main, and record them as pre-existing if so. - -Implementation: [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737) -implements this rule in Rust (`SubscribeServe` for lite-03 through lite-05, -the track model, and a front's splice) and JS. The rule holds in every -`moq-net` and JS test, but the HLS recorder test still fails until the HLS grid lands; see the decision above. - -## Required - -- [HLS export on a declared grid](/quest/m0/hls-media-grid.md) - removes the record-track subscription that captures media before the recorder's own SUBSCRIBE From 9ce32d3b695b2215c24cc2add1f48c3796e4349a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 13:55:09 -0800 Subject: [PATCH 09/25] docs(quest): retain replay quest for review decisions Co-Authored-By: GPT-6.1 Sol --- quest/m1/README.md | 1 + quest/m1/relay-inflight-replay.md | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 quest/m1/relay-inflight-replay.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 04ad898ab2c..5b0beb8392f 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -176,6 +176,7 @@ their end report, so an embedder's drain alert pages on real cuts. - [HLS export lists a same-codec reconfigure's first group under the new init](/quest/m1/hls-reinit-same-codec.md) - the hand-over pick, whichever of the catalog and the records arrives first, after the catalog `hls` removal is planned - [Ordered reads can skip owed holes, and snapshot latest() does](/quest/m1/ordered-owed-holes.md) - moq-net's `Ordered` gains a read that skips past an owed hole to the newest buffered group, while keeping the reader's bounds - [The relay reports native sessions it force-closes at the drain deadline](/quest/m1/relay-drain-cuts.md) - the drain's public outcome and live tally separate sessions cut while open from sessions only finishing their end report, so an embedder's drain alert pages on real cuts +- [A Rust relay never replays from group 0 to a lite-03/04 subscriber](/quest/m1/relay-inflight-replay.md) - A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track copy that is still resolving starts at the newest group once the copy resolves - [Every moq-mux consumer gets the catalog warmup from one entry point](/quest/m1/mux-warmup.md) - one moq-mux call widens the subscription and configures the consumer, adopted by moq-audio, moq-c, moq-ffi's raw consumer, and moqsrc - [Codex shells never load another checkout's dev environment](/quest/m1/codex-env-snapshot.md) - the Codex dev-shell snapshot is skipped when stale or inside `nix develop` - [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md new file mode 100644 index 00000000000..2963b21d9e3 --- /dev/null +++ b/quest/m1/relay-inflight-replay.md @@ -0,0 +1,24 @@ +# [S] A Rust relay never replays from group 0 to a lite-03/04 subscriber + +## Goal + +A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track +copy that is still resolving starts at the request-time live edge, skipping +cached history while delivering groups produced after the request. + +## Plan + +The maintainer decided on 2026-11-03 to keep the request-time floor and land +after HLS grid export. [moq-dev/moq#7652](https://github.com/moq-dev/moq/pull/7652) +has landed. Its unchanged `a_cursor_records_a_publisher_without_fetch` test, +`just ci origin/main`, and `just test interop --all` pass after merging main +into [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737). + +The implementation snapshots the request/answer boundary in Rust and JS. +The local Claude review raised the remaining choices below. Public API and +wire format are unchanged. + +## Waiting + +- Maintainer: decide the request-time floor for a logical track whose metadata is ready but which has neither a serving nor a parked copy. Options: defer the wire join until the next copy supplies its request snapshot (recommended, so that copy's cached history is skipped), or retain an empty request snapshot and deliver that copy's entire cache. The current subscription resolves from retained metadata before the next route splices, so simply deferring the snapshot lookup does not settle when it is captured. +- Maintainer: settle lite-01/02 parity. Rust deliberately retains the arrival-only floor on those versions, while JS uses the answer snapshot. Options: mirror the Rust exception in JS (recommended, preserving the earlier review's Rust carveout), or extend answer snapshots to both implementations on lite-01/02. After these choices, implement them and rerun checks and local review. From f87d0ea0bf8c65687e20601ce3310dddd23cfedd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 15:53:27 -0800 Subject: [PATCH 10/25] fix(net): defer copyless wire joins and align legacy floors --- js/net/src/lite/publisher.test.ts | 16 +++++++++++++ js/net/src/lite/publisher.ts | 7 +++++- quest/m1/relay-inflight-replay.md | 8 ++++--- rs/moq-net/src/lite/publisher.rs | 2 +- rs/moq-net/src/model/resume.rs | 36 ++++++++++++++++++++++++++++ rs/moq-net/src/model/track.rs | 39 +++++++++++++++++++++++++++---- 6 files changed, 99 insertions(+), 9 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index dd4a088569f..c745d7da824 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1455,6 +1455,22 @@ test.each([Version.DRAFT_03, Version.DRAFT_04, Version.DRAFT_05])( }, ); +// Lite-01/02 retain the arrival-only floor even when the answer contains history. +test.each([Version.DRAFT_01, Version.DRAFT_02])( + "lite %s: a pending join retains its arrival-only floor", + async (version) => { + const sub = await servedSubscription({ version, pending: true }); + try { + await sub.accept([0n, 3n, 7n]); + expect(await sub.servedHeader()).toBe(0n); + expect(await sub.servedHeader()).toBe(3n); + expect(await sub.servedHeader()).toBe(7n); + } finally { + await sub.close(); + } + }, +); + // On an empty track the re-pin has no latest group to name, so the first group the // track produces is served, even below the dropped floor. test("lite draft-05: an update dropping the floor on an empty track serves the next group", async () => { diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index e9b8f6e972b..023105e5feb 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -994,7 +994,12 @@ export class Publisher { // newest the answer held, or the first group when it held none: cached history below // it is skipped, and anything written since, even for this demand, is delivered. const resolved = cursorFloor(this.version, msg.startGroup, hooks.requestedSequence(track)); - if (latest === undefined && resolved !== floor) { + if ( + this.version !== Version.DRAFT_01 && + this.version !== Version.DRAFT_02 && + latest === undefined && + resolved !== floor + ) { floor = resolved; track.withRange({ start: { included: floor } }); } diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index 2963b21d9e3..3e94451df9c 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -18,7 +18,9 @@ The implementation snapshots the request/answer boundary in Rust and JS. The local Claude review raised the remaining choices below. Public API and wire format are unchanged. -## Waiting +Decided 2026-11-04 (maintainer, /quest-unblock): subscription front with +metadata but no current copy: ✅ defer the wire join until the next copy +supplies its snapshot. Not chosen: joining at the edge the metadata implies. -- Maintainer: decide the request-time floor for a logical track whose metadata is ready but which has neither a serving nor a parked copy. Options: defer the wire join until the next copy supplies its request snapshot (recommended, so that copy's cached history is skipped), or retain an empty request snapshot and deliver that copy's entire cache. The current subscription resolves from retained metadata before the next route splices, so simply deferring the snapshot lookup does not settle when it is captured. -- Maintainer: settle lite-01/02 parity. Rust deliberately retains the arrival-only floor on those versions, while JS uses the answer snapshot. Options: mirror the Rust exception in JS (recommended, preserving the earlier review's Rust carveout), or extend answer snapshots to both implementations on lite-01/02. After these choices, implement them and rerun checks and local review. +Decided 2026-11-04 (maintainer, /quest-unblock): lite-01/02 parity: +✅ mirror Rust's arrival-only exception in JS. Not chosen: leaving JS different. diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 43257240b40..862eadea0b1 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2369,7 +2369,7 @@ impl Request for SubscribeServe { subscribing, legacy, .. } => { let ietf = ready!(shared.peer_setup.poll_ietf(waiter)); - let track = match subscribing.poll_ok(waiter) { + let track = match subscribing.poll_wire(waiter) { Poll::Ready(track) => track, Poll::Pending => { let consumer = ready!(Legacy::poll_datagrams(legacy, waiter)); diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index bc9965ce72f..e36fe05fdb3 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -323,6 +323,19 @@ impl Consumer { self.state.read().copy.clone() } + /// Wait for a copy to supply a join snapshot, or for the route to end. + pub(super) fn poll_join(&self, waiter: &kio::Waiter) -> Poll<()> { + self.state + .poll(waiter, |route| { + if route.copy.is_some() || route.parked.is_some() || route.phase.terminal() { + Poll::Ready(()) + } else { + Poll::Pending + } + }) + .map(|_| ()) + } + /// The newest sequence as of the front's request for the serving copy, as it was spliced. pub(super) fn resolved(&self) -> Option { self.state.read().resolved @@ -2527,6 +2540,29 @@ mod test { .accept(crate::track::Info::timed()) } + #[test] + fn a_wire_join_waits_for_a_copy_despite_retained_metadata() { + let routes = Producer::new(); + let logical = logical(&routes); + let pending = logical.consume().subscribe(None); + let noop = kio::Waiter::noop(); + assert!(pending.poll_wire(&noop).is_pending()); + let copy = copy(); + copy.create_group(group::Info { sequence: 7 }) + .unwrap() + .finish() + .unwrap(); + routes.serve(copy.consume()); + copy.create_group(group::Info { sequence: 8 }) + .unwrap() + .finish() + .unwrap(); + let Poll::Ready(Ok(sub)) = pending.poll_wire(&noop) else { + panic!("the copy supplies the snapshot"); + }; + assert_eq!(sub.requested_sequence(), Some(7)); + } + fn subscribe(logical: &track::Producer, max_delay: Duration) -> track::Subscriber { let subscription = Subscription::default().with_delay(Delay::default().with_max(max_delay)); logical diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index cb96f2a397a..4e99a397a39 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -2135,13 +2135,13 @@ impl TrackState { return None; } // Read outside the track's lock: the copy has a lock of its own. - Some(match routes { + match routes { Some(routes) => routes .serving() .or_else(|| routes.parked()) - .and_then(|copy| copy.latest_sequence()), - None => max, - }) + .map(|copy| copy.latest_sequence()), + None => Some(max), + } } /// The newest sequence a request made while the track was still resolving finds in its @@ -5414,6 +5414,16 @@ impl Subscribing { } } + /// Wait for a front's copy before pinning a wire join from retained metadata. + pub(crate) fn poll_wire(&self, waiter: &kio::Waiter) -> Poll> { + if self.subscription.requested().is_none() + && let Some(routes) = self.state.read().routes.clone() + { + ready!(routes.poll_join(waiter)); + } + self.poll_ok(waiter) + } + /// Poll until the peer confirms the subscription, yielding the [`Subscriber`]. /// Errors if the track is aborted or not found. pub fn poll_ok(&self, waiter: &kio::Waiter) -> Poll> { @@ -9447,6 +9457,27 @@ mod test { Producer::new(Arc::new(broadcast::Info::default()), name, info) } + #[test] + fn a_provisional_install_is_the_first_visible_answer() { + let request = Request::new(Arc::new(broadcast::Info::default()), "test"); + let consumer = request.consume(); + let pending = consumer.subscribe(None); + { + let mut state = request.state.write().unwrap(); + state + .insert_group_request(3, group::Bounds { start: 0, end: Some(1) }, None) + .unwrap(); + assert!(state.poll_info().is_ready()); + } + let Poll::Ready(Ok(sub)) = pending.poll_ok(&kio::Waiter::noop()) else { + panic!("provisional metadata already resolves subscribers"); + }; + assert_eq!(sub.requested_sequence(), None); + // A later publisher acceptance must not move the first visible answer's boundary. + let producer = request.accept(Info::default().with_max_age(None)); + assert_eq!(TrackState::request_answered(&producer.state.consume()), None); + } + #[test] fn drop_changes_follow_split_coalesced_and_recreated_runs() { let producer = track_producer("test", Info::default()); From 13941018f766dcc72cc92bdc6cb57d46308e3cba Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 15:53:40 -0800 Subject: [PATCH 11/25] chore(quest): finish relay request-time joins --- quest/m1/README.md | 1 - quest/m1/relay-inflight-replay.md | 26 -------------------------- 2 files changed, 27 deletions(-) delete mode 100644 quest/m1/relay-inflight-replay.md diff --git a/quest/m1/README.md b/quest/m1/README.md index c986b48cae6..de0451a1b70 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -179,7 +179,6 @@ their end report, so an embedder's drain alert pages on real cuts. - [Poll-first async](/quest/m1/poll-first/README.md) - library crates compile no coroutines by default: async wrappers are generated from `poll_*` and gated behind an `async` feature - [Ordered reads can skip owed holes, and snapshot latest() does](/quest/m1/ordered-owed-holes.md) - moq-net's `Ordered` gains a read that skips past an owed hole to the newest buffered group, while keeping the reader's bounds - [The relay reports native sessions it force-closes at the drain deadline](/quest/m1/relay-drain-cuts.md) - the drain's public outcome and live tally separate sessions cut while open from sessions only finishing their end report, so an embedder's drain alert pages on real cuts -- [A Rust relay never replays from group 0 to a lite-03/04 subscriber](/quest/m1/relay-inflight-replay.md) - A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track copy that is still resolving starts at the newest group once the copy resolves - [Every moq-mux consumer gets the catalog warmup from one entry point](/quest/m1/mux-warmup.md) - one moq-mux call widens the subscription and configures the consumer, adopted by moq-audio, moq-c, moq-ffi's raw consumer, and moqsrc - [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport - [Vendor and trim the WebRTC stack](/quest/m1/webrtc-vendor.md) - str0m, dimpl, `is`, and sctp-proto live in moq-webrtc with unused code removed, so moq-rtc can take rind diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md deleted file mode 100644 index 3e94451df9c..00000000000 --- a/quest/m1/relay-inflight-replay.md +++ /dev/null @@ -1,26 +0,0 @@ -# [S] A Rust relay never replays from group 0 to a lite-03/04 subscriber - -## Goal - -A Rust relay or publisher serving a lite-03 or lite-04 SUBSCRIBE from a track -copy that is still resolving starts at the request-time live edge, skipping -cached history while delivering groups produced after the request. - -## Plan - -The maintainer decided on 2026-11-03 to keep the request-time floor and land -after HLS grid export. [moq-dev/moq#7652](https://github.com/moq-dev/moq/pull/7652) -has landed. Its unchanged `a_cursor_records_a_publisher_without_fetch` test, -`just ci origin/main`, and `just test interop --all` pass after merging main -into [moq-dev/moq#7737](https://github.com/moq-dev/moq/pull/7737). - -The implementation snapshots the request/answer boundary in Rust and JS. -The local Claude review raised the remaining choices below. Public API and -wire format are unchanged. - -Decided 2026-11-04 (maintainer, /quest-unblock): subscription front with -metadata but no current copy: ✅ defer the wire join until the next copy -supplies its snapshot. Not chosen: joining at the edge the metadata implies. - -Decided 2026-11-04 (maintainer, /quest-unblock): lite-01/02 parity: -✅ mirror Rust's arrival-only exception in JS. Not chosen: leaving JS different. From a05de1aa9a831f966a0b07a5c9af1cc6fa749856 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 15:54:38 -0800 Subject: [PATCH 12/25] test(net): unwrap the provisional track write guard --- rs/moq-net/src/model/track.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 4e99a397a39..820053a3799 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -9463,7 +9463,7 @@ mod test { let consumer = request.consume(); let pending = consumer.subscribe(None); { - let mut state = request.state.write().unwrap(); + let mut state = request.state.write().ok().unwrap(); state .insert_group_request(3, group::Bounds { start: 0, end: Some(1) }, None) .unwrap(); From e0f8f10bd18bc53f9288d482adb9a96eac42c508 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 15:56:27 -0800 Subject: [PATCH 13/25] test(net): retain the provisional group handle --- rs/moq-net/src/model/track.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 820053a3799..aae2633d665 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -9464,7 +9464,7 @@ mod test { let pending = consumer.subscribe(None); { let mut state = request.state.write().ok().unwrap(); - state + let _group = state .insert_group_request(3, group::Bounds { start: 0, end: Some(1) }, None) .unwrap(); assert!(state.poll_info().is_ready()); From fa492a3d2dedea4e8530fce75a94d12e9296867b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 16:17:24 -0800 Subject: [PATCH 14/25] fix(net): freeze pending front snapshots at their first answer --- rs/moq-net/src/model/resume.rs | 46 ++++++++++++++++++++++++++++ rs/moq-net/src/model/track.rs | 6 ++++ rs/moq-net/src/model/track/demand.rs | 10 +++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index e36fe05fdb3..61e3ba70cd1 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -77,6 +77,8 @@ struct Route { /// The newest sequence as of the front's request for the serving copy, where a /// subscription made before the track resolved joins; see [`track::Subscriber::requested_sequence`]. resolved: Option, + /// Shared by requests waiting for the next splice, then frozen for those requests. + answer: Arc>>, /// The id the next stage takes. next_stage: u64, } @@ -209,6 +211,8 @@ impl Producer { fn serve_from(&self, copy: track::Consumer, stage: Option, resolved: Option) { if let Ok(mut route) = self.state.write() { route.generation += 1; + let _ = route.answer.set(resolved); + route.answer = Default::default(); route.resolved = resolved; route.copy = Some(copy); route.parked = None; @@ -323,6 +327,16 @@ impl Consumer { self.state.read().copy.clone() } + /// The first copy's answer for a request that has no arrival snapshot. + pub(super) fn answer(&self) -> Arc>> { + let route = self.state.read(); + if route.copy.is_some() || route.parked.is_some() { + Arc::new(std::sync::OnceLock::from(route.resolved)) + } else { + route.answer.clone() + } + } + /// Wait for a copy to supply a join snapshot, or for the route to end. pub(super) fn poll_join(&self, waiter: &kio::Waiter) -> Poll<()> { self.state @@ -2563,6 +2577,38 @@ mod test { assert_eq!(sub.requested_sequence(), Some(7)); } + #[test] + fn a_pending_wire_join_keeps_the_first_answer_across_splices() { + let routes = Producer::new(); + let logical = logical(&routes); + let pending = logical.consume().subscribe(None); + let first = copy(); + first + .create_group(group::Info { sequence: 7 }) + .unwrap() + .finish() + .unwrap(); + routes.serve(first.consume()); + routes.park(); + routes.unpark(); + let second_pending = logical.consume().subscribe(None); + let second = copy(); + second + .create_group(group::Info { sequence: 9 }) + .unwrap() + .finish() + .unwrap(); + routes.serve(second.consume()); + let Poll::Ready(Ok(first_sub)) = pending.poll_wire(&kio::Waiter::noop()) else { + panic!("first answer"); + }; + let Poll::Ready(Ok(second_sub)) = second_pending.poll_wire(&kio::Waiter::noop()) else { + panic!("second answer"); + }; + assert_eq!(first_sub.requested_sequence(), Some(7)); + assert_eq!(second_sub.requested_sequence(), Some(9)); + } + fn subscribe(logical: &track::Producer, max_delay: Duration) -> track::Subscriber { let subscription = Subscription::default().with_delay(Delay::default().with_max(max_delay)); logical diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index aae2633d665..244287661df 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -4849,6 +4849,8 @@ impl Consumer { let subscription = subscription.expect("a subscription was requested"); if let Some(newest) = TrackState::requested(&state) { subscription.set_requested(newest); + } else if let Some(routes) = state.read().routes.clone() { + subscription.set_answer(routes.answer()); } kio::Pending::new(Subscribing { @@ -5285,6 +5287,7 @@ impl Consumer { name: self.name.clone(), broadcast: self.broadcast.clone(), requested: TrackState::requested(&state), + answer: state.read().routes.as_ref().map(|routes| routes.answer()), state, stats: self.stats.clone(), }) @@ -5410,6 +5413,7 @@ impl Subscribing { broadcast: self.broadcast.clone(), state: self.state.clone(), requested: self.subscription.requested(), + answer: self.subscription.answer(), stats: self.stats.clone(), } } @@ -5490,6 +5494,7 @@ pub struct Resolving { state: kio::Consumer, /// The newest sequence when requested, if the track had resolved; see [`TrackState::requested`]. requested: Option>, + answer: Option>>>, stats: stats::Scope, } @@ -5518,6 +5523,7 @@ impl Resolving { #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn requested_sequence(&self) -> Option { self.requested + .or_else(|| self.answer.as_ref()?.get().copied()) .unwrap_or_else(|| TrackState::request_answered(&self.state)) } } diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index aa4dc17695d..29e9de13029 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -212,6 +212,7 @@ struct Preference { /// The track's newest sequence when the subscription was requested, set only if the track /// had resolved by then; see `TrackState::requested`. requested: std::sync::OnceLock>, + answer: std::sync::OnceLock>>>, } impl Preferences { @@ -227,6 +228,7 @@ impl Preferences { subscriptions, ranges_revision: AtomicU64::new(0), requested: Default::default(), + answer: Default::default(), })) } @@ -273,7 +275,13 @@ impl Preferences { } /// The track's newest sequence when the subscription was requested, if it had resolved. pub(in crate::model) fn requested(&self) -> Option> { - self.0.requested.get().copied() + self.0.requested.get().or_else(|| self.0.answer.get()?.get()).copied() + } + pub(in crate::model) fn answer(&self) -> Option>>> { + self.0.answer.get().cloned() + } + pub(in crate::model) fn set_answer(&self, answer: Arc>>) { + let _ = self.0.answer.set(answer); } pub(in crate::model) fn poll_ref( &self, From 1e575c37a65e70967a3dac24c264ed01d5a98ebc Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 16:17:42 -0800 Subject: [PATCH 15/25] test(net): cover info snapshots across route replacements --- rs/moq-net/src/model/resume.rs | 2 ++ rs/moq-net/src/model/track.rs | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 61e3ba70cd1..b2eddcda51a 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -2582,6 +2582,7 @@ mod test { let routes = Producer::new(); let logical = logical(&routes); let pending = logical.consume().subscribe(None); + let info = pending.info(); let first = copy(); first .create_group(group::Info { sequence: 7 }) @@ -2606,6 +2607,7 @@ mod test { panic!("second answer"); }; assert_eq!(first_sub.requested_sequence(), Some(7)); + assert_eq!(info.requested_sequence(), Some(7)); assert_eq!(second_sub.requested_sequence(), Some(9)); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 244287661df..514b3652cac 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5283,11 +5283,12 @@ impl Consumer { /// [`Subscriber::info`] is the already-resolved counterpart. pub fn info(&self) -> kio::Pending { let state = self.resolve(None).0; + let answer = state.read().routes.as_ref().map(|routes| routes.answer()); kio::Pending::new(Resolving { name: self.name.clone(), broadcast: self.broadcast.clone(), requested: TrackState::requested(&state), - answer: state.read().routes.as_ref().map(|routes| routes.answer()), + answer, state, stats: self.stats.clone(), }) From 8a321d144aeed36eefca778a831ffca1658f2b45 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 16:22:44 -0800 Subject: [PATCH 16/25] test(net): initialize shared answers in preference fixtures --- rs/moq-net/src/model/track/demand.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 29e9de13029..e0ea28236e9 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -312,6 +312,7 @@ impl From> for Preferences { subscriptions: None, ranges_revision: AtomicU64::new(0), requested: Default::default(), + answer: Default::default(), })) } } From 5e2ead50245e55ef16a6c81a116b95d8413ed9f5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 16:36:10 -0800 Subject: [PATCH 17/25] chore(quest): checkpoint remaining snapshot review findings --- quest/m1/README.md | 1 + quest/m1/relay-inflight-replay.md | 37 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 quest/m1/relay-inflight-replay.md diff --git a/quest/m1/README.md b/quest/m1/README.md index deefd14e186..366834342a9 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -182,6 +182,7 @@ their end report, so an embedder's drain alert pages on real cuts. - [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport - [Vendor and trim the WebRTC stack](/quest/m1/webrtc-vendor.md) - str0m, dimpl, `is`, and sctp-proto live in moq-webrtc with unused code removed, so moq-rtc can take rind - [MoQ on rind](/quest/m1/rind/README.md) - every MoQ crate takes its crypto from rind, a pure-Rust graviola fork, so no build compiles C crypto +- [Finish request-time relay join snapshots](/quest/m1/relay-inflight-replay.md) - settle the fourth-round checkpoint and remaining request snapshot findings ## Related diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md new file mode 100644 index 00000000000..6d4a34ffa73 --- /dev/null +++ b/quest/m1/relay-inflight-replay.md @@ -0,0 +1,37 @@ +# [S] Finish request-time relay join snapshots + +## Goal + +Pending lite-03/04/05 joins skip history before their request-time answer and +deliver every group produced afterward, in Rust and JS. + +## Plan + +Decided 2026-11-03 (maintainer, /quest-unblock): keep the request-time floor +and land after HLS grid export. The grid prerequisite has landed. + +Decided 2026-11-04 (maintainer, /quest-unblock): subscription front with +metadata but no current copy: ✅ defer the wire join until the next copy +supplies its snapshot. Not chosen: joining at the edge the metadata implies. + +Decided 2026-11-04 (maintainer, /quest-unblock): lite-01/02 parity: +✅ mirror Rust's arrival-only exception in JS. Not chosen: leaving JS different. + +Both decisions are implemented. Pending Rust fronts retain a shared immutable +first-answer cell across splices and copyless intervals. The provisional-install +finding was refuted with a passing visibility regression and accepted by the +local reviewer. Local Claude round 4 reviewed implementation +[8a321d144a](https://github.com/moq-dev/moq/commit/8a321d144aeed36eefca778a831ffca1658f2b45) +with zero findings; local CI (10,814 Rust tests plus JS/repository checks) and +full interop passed. + +Subsequent online findings remain: +- Rust captures the snapshot after demand registration wakes producers, allowing + demand-triggered groups to enter the floor. Capture before publishing demand. +- JS accept uses nullish assignment, which overwrites an empty first snapshot + on a repeated accept. Preserve null and assign only when undefined. +- Add routed pending JS coverage, so the copy-to-front snapshot handoff is tested. + +## Waiting + +- Maintainer: the fourth local review round triggers the required design checkpoint before further fixes. Options: continue with the targeted fixes and routed regression above (recommended; the shared immutable snapshot design remains bounded and appropriate), or rethink the approach. Once authorized, implement the remaining findings, rerun checks and local review, and mark the PR ready. From 899160c38f690c9b81b884efa0ea5e319c593b10 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 22:42:48 -0800 Subject: [PATCH 18/25] fix(net): settle legacy relay joins past the upstream answer Replace the request-time snapshot machinery with the maintainer's 2026-11-05 design: a lite-03/04 join that found no newest group on a remotely fed track waits 50 ms past the answer and floors at the newest group known by then. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 19 +- js/net/src/lite/publisher.test.ts | 80 +++- js/net/src/lite/publisher.ts | 45 +- js/net/src/resume.ts | 7 +- js/net/src/track.ts | 26 +- quest/m1/README.md | 2 +- quest/m1/relay-inflight-replay.md | 57 ++- rs/moq-net/src/lite/publisher.rs | 390 ++++++++++++------ rs/moq-net/src/model/origin.rs | 6 +- rs/moq-net/src/model/resume.rs | 123 ++---- rs/moq-net/src/model/track.rs | 142 +------ rs/moq-net/src/model/track/demand.rs | 22 - .../tests/integration/widened_max_age.rs | 41 ++ 13 files changed, 491 insertions(+), 469 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index af5c3475490..ff0887c06ef 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -268,11 +268,8 @@ export interface ResumeShell { readonly closed: GetPromise; /** The properties the first copy committed, if any yet. */ info(): Info | undefined; - /** - * Commit the logical track's properties, once, with the newest group the committing copy - * found as of its request (see `hooks.requestedSequence`). - */ - commit(info: Info, requested: bigint | undefined): void; + /** Commit the logical track's properties, once. */ + commit(info: Info): void; /** End the logical track: cleanly, or with `abort`. */ close(abort?: Error): void; } @@ -372,6 +369,8 @@ export interface ResumeSource { forgetPastBudget(delivered: Delivered): void; /** The newest sequence any copy has seen. */ latest(): bigint | undefined; + /** Whether a wire subscription feeds the serving copy. */ + received(): boolean; /** The serving copy's declared start, raised to its cursor floor, or `"pending"` until resolved. */ start(): bigint | "pending" | undefined; /** The logical track's declared exclusive end, as the first copy to know it declared it. */ @@ -552,12 +551,10 @@ export const hooks: { /** Snapshot the latest group sequence for wire positioning; assigned by `track.ts`. */ latestSequence: (subscriber: Subscriber) => bigint | undefined; /** - * The newest group as of the subscription's request, once its info resolved: the track's - * newest then, or, for a track still resolving then, the newest its answer held. Nothing - * written since counts, even in response to the subscription's own demand. Assigned by - * `track.ts`. + * Whether a wire subscription feeds the subscriber's track, or a front's serving copy; + * assigned by `track.ts`. */ - requestedSequence: (subscriber: Subscriber) => bigint | undefined; + received: (subscriber: Subscriber) => boolean; /** * The source's declared start, raised to the subscriber's cursor floor, or `"pending"` while * the upstream START that resolves it has yet to arrive; assigned by `track.ts`. @@ -839,7 +836,7 @@ export const hooks: { latestSequence: () => { throw new Error("track.ts not loaded"); }, - requestedSequence: () => { + received: () => { throw new Error("track.ts not loaded"); }, sourceStart: () => { diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index ab1115fa5fc..b19e0bd6c94 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -797,11 +797,13 @@ async function servedSubscription( serving, parked: gate.parked, release: gate.release, - // Resolve a pending track with the publisher's request for it, answering with `held`. - async accept(held: bigint[] = []) { + // Resolve a pending track with the publisher's request for it, answering with `held`, + // as a copy a wire subscription feeds when `received`. + async accept(held: bigint[] = [], received = false) { const pulled = await request; if (!pulled) throw new Error("the publisher never requested the track"); const staged = hooks.stageRequest(pulled, info); + if (received) hooks.receiveTrack(staged.producer); for (const sequence of held) { const group = staged.producer.createGroup(sequence); for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(Number(sequence)))); @@ -1569,43 +1571,77 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", }); // A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching -// upstream. An absent Group Start is still the newest group as of the request, which the -// answer gives: groups written once it resolved are newer, even those written for this -// subscription's demand, so they are delivered. -test.each([Version.DRAFT_01, Version.DRAFT_03, Version.DRAFT_05])( - "lite %s: a subscribe that beats its track resolving delivers what follows the answer", - async (version) => { - const sub = await servedSubscription({ version, pending: true }); +// upstream. What a lite-03/04 copy holds then is not the live edge, so the join waits 50 ms +// past the answer and starts at the newest group known by then: older ones, cached or +// arriving after, are history. +// Routed, the join reads the front's serving copy, which the wire feeds. +test.each([ + [Version.DRAFT_03, false], + [Version.DRAFT_04, false], + [Version.DRAFT_04, true], +])( + "lite %s (routed %s): a subscribe that beats a copy resolving starts at the newest group once settled", + async (version, routed) => { + const clock = new Manual(); + const installed = clock.install(); + const sub = await servedSubscription({ version, pending: true, routed }); try { - await sub.accept(); - sub.serve(0n); - sub.serve(1n); - sub.serve(2n); + await sub.accept([0n, 3n], true); + await clock.advance(Nano.zero); + sub.serve(5n); + await clock.advance(Nano.fromMilli(Milli(50))); + sub.serve(4n); + sub.serve(9n); // Headers only: frames before lite-05 carry no timestamp for `servedGroup` to read. - expect(await sub.servedHeader()).toBe(0n); - expect(await sub.servedHeader()).toBe(1n); - expect(await sub.servedHeader()).toBe(2n); + expect(await sub.servedHeader()).toBe(5n); + expect(await sub.servedHeader()).toBe(9n); } finally { await sub.close(); + installed.close(); } }, ); -// The answer's own groups are history as of the request: the join starts at its newest. -test.each([Version.DRAFT_03, Version.DRAFT_04, Version.DRAFT_05])( - "lite %s: a subscribe that beats its track resolving joins at the answer's newest group", +// Nothing known by the settle leaves no history to skip: every group that follows is served. +test.each([Version.DRAFT_03, Version.DRAFT_04])( + "lite %s: a subscribe that beats an empty copy resolving serves every later group", async (version) => { + const clock = new Manual(); + const installed = clock.install(); const sub = await servedSubscription({ version, pending: true }); try { - await sub.accept([0n, 3n, 7n]); - expect(await sub.servedHeader()).toBe(7n); - expect(await sub.servedSequence()).toBeUndefined(); + await sub.accept([], true); + await clock.advance(Nano.zero); + await clock.advance(Nano.fromMilli(Milli(50))); + sub.serve(0n); + sub.serve(1n); + expect(await sub.servedHeader()).toBe(0n); + expect(await sub.servedHeader()).toBe(1n); } finally { await sub.close(); + installed.close(); } }, ); +// A local track's answer is its own newest group, as is a lite-05 copy's, whose first-frame +// latency allows no wait: the join starts there at once. +test.each([ + [Version.DRAFT_03, false], + [Version.DRAFT_04, false], + [Version.DRAFT_05, false], + [Version.DRAFT_05, true], +])("lite %s (received %s): a subscribe that beats its track resolving joins at its newest group", async (version, received) => { + const sub = await servedSubscription({ version, pending: true }); + try { + await sub.accept([0n, 3n, 7n], received); + expect(await sub.servedHeader()).toBe(7n); + expect(await sub.servedSequence()).toBeUndefined(); + } finally { + await sub.close(); + } +}); + // Lite-01/02 retain the arrival-only floor even when the answer contains history. test.each([Version.DRAFT_01, Version.DRAFT_02])( "lite %s: a pending join retains its arrival-only floor", diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 114f3d4d850..99814150258 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -471,6 +471,19 @@ function carriesMaxDelay(version: Version): boolean { return version !== Version.DRAFT_01 && version !== Version.DRAFT_02; } +/** + * How long a lite-03/04 join on a received copy that held no group on arrival waits past the + * answer before it takes the newest group as the live edge. Lite-01..06 answers carry no + * largest group (lite-03/04's SUBSCRIBE_OK names only the requested start), so the newest + * group to arrive soon after the answer stands in, and a later one older than it is skipped. + */ +const SETTLE = Nano.fromMilli(Milli(50)); + +/** Whether a join on this version keeps the floor its arrival gave, as lite-01/02 do in Rust. */ +function arrivalOnly(version: Version): boolean { + return version === Version.DRAFT_01 || version === Version.DRAFT_02; +} + /** * The floor of a subscription's read cursor for the wire serving it. * @@ -1029,20 +1042,24 @@ export class Publisher { // priority. const info = await untilAborted(track.info(), left.signal); const publisher = info.priority; - // A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a - // copy still fetching upstream, an on-demand track, or on lite-05 one sent alongside - // its TRACK, when no group is known yet. The newest as of the request is then the - // newest the answer held, or the first group when it held none: cached history below - // it is skipped, and anything written since, even for this demand, is delivered. - const resolved = cursorFloor(this.version, msg.startGroup, hooks.requestedSequence(track)); - if ( - this.version !== Version.DRAFT_01 && - this.version !== Version.DRAFT_02 && - latest === undefined && - resolved !== floor - ) { - floor = resolved; - track.withRange({ start: { included: floor } }); + // Without a newest group on arrival, such as a SUBSCRIBE that beat its track + // resolving, the newest once it resolves stands in, as it would for one sent after. + // A lite-03/04 copy's newest group then is not what its source's answer brings, but + // an older one cached or none yet: it waits {@link SETTLE} past the answer and starts + // at the newest group known by then, including a largest the answer reported. + // Lite-01/02 keep the floor their arrival gave, as Rust does. + if (latest === undefined && msg.startGroup === undefined && !arrivalOnly(this.version)) { + const settles = + (this.version === Version.DRAFT_03 || this.version === Version.DRAFT_04) && hooks.received(track); + if (settles) await sleep(SETTLE, { signal: left.signal }); + const known = hooks.latestSequence(track); + const reported = settles ? hooks.peekLargest(track).largest?.group : undefined; + const newest = known === undefined ? reported : reported === undefined ? known : max(known, reported); + const resolved = cursorFloor(this.version, msg.startGroup, newest); + if (resolved !== floor) { + floor = resolved; + track.withRange({ start: { included: floor } }); + } } // Older drafts carry no timestamps. let stamp: Stamp | undefined; diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index df1d9c29224..2dee6252d96 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -489,7 +489,7 @@ class Reader implements ResumeSource { copy.sub.info().then( (info) => { record(info, source); - shell.commit(info, hooks.requestedSequence(copy.sub)); + shell.commit(info); }, () => {}, ); @@ -1156,6 +1156,11 @@ class Reader implements ResumeSource { if (newest) hooks.forgetPastBudget(newest.sub, delivered); } + received(): boolean { + const serving = this.#serving(); + return serving !== undefined && hooks.received(serving.sub); + } + latest(): bigint | undefined { let latest: bigint | undefined; for (const copy of this.#copies) { diff --git a/js/net/src/track.ts b/js/net/src/track.ts index c0f5d3b6a5b..878d41c5418 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -718,9 +718,6 @@ class TrackState { update: Signal; /** Resolved once the producer commits the immutable properties. */ info = new Signal(undefined); - // The newest group as of this reader's request, recorded as {@link info} resolves: the - // track's newest then, or what its answer held for a request that beat it. `null` for none. - requested?: bigint | null; /** Newest group head, datagram, frame, or location reported by the publisher. */ largest = new Signal(undefined); // The group {@link largest} first named, where a relative anchor resolved before any group @@ -805,6 +802,8 @@ let owedRuns: (producer: Producer, start: bigint, end: bigint) => Iterable { sequence: bigint; folds?: number } | undefined; let fillEdge: (producer: Producer) => bigint | undefined; let sourceStart: (producer: Producer) => bigint | "pending" | undefined; +// Whether a wire subscription feeds the producer; assigned in its static block. +let fed: (producer: Producer) => boolean; // The lowest group a reader naming `named` asks a fill for; see `Producer.#fillFloor`. let fillFloor: (producer: Producer, named: bigint | undefined) => bigint | undefined; // The producer's checked group starts, sorted by sequence; assigned in its static block. @@ -1022,6 +1021,7 @@ export class Producer { !producer.#subscribed && producer.#state.info.peek()?.ietf !== undefined ? producer.#state.largest.peek()?.group : undefined; + fed = (producer) => producer.#subscribed; hooks.receiveTrack = (producer, starts = {}) => { producer.#subscribed = true; producer.#starts = starts; @@ -1232,8 +1232,6 @@ export class Producer { throw err; } this.#state.info.set(resolved); - // What the answer holds, written before accept, is where a sink that beat it joins. - const requested = this.#state.latest ?? null; // Propagate to any sink handed out before accept (the on-demand path). const compressed = new Map(); for (const sink of this.#sinks) { @@ -1250,7 +1248,6 @@ export class Producer { } }); } - sink.requested ??= requested; sink.info.set(resolved); } this.#updateSubscription(); @@ -1320,10 +1317,7 @@ export class Producer { #addSink(sink: TrackState): void { sink.producer = this; const info = this.#state.info.peek(); - if (info) { - sink.requested = this.#state.latest ?? null; - sink.info.set(info); - } + if (info) sink.info.set(info); sink.largest.set(this.#state.largest.peek()); sink.first = this.#state.largest.peek()?.group; sink.largestEnded.set(this.#state.largestEnded.peek()); @@ -3009,10 +3003,8 @@ export class Subscriber { cursor: subscriber.#cursor, closed: state.closed, info: () => state.info.peek(), - commit: (info, requested) => { - if (state.info.peek() !== undefined) return; - state.requested = requested ?? null; - state.info.set(info); + commit: (info) => { + if (state.info.peek() === undefined) state.info.set(info); }, close: (abort) => { closeTrackState(state, abort); @@ -3022,7 +3014,11 @@ export class Subscriber { }; hooks.latestSequence = (subscriber) => subscriber.#resume ? subscriber.#resume.latest() : subscriber.#state.latest; - hooks.requestedSequence = (subscriber) => subscriber.#state.requested ?? undefined; + hooks.received = (subscriber) => { + if (subscriber.#resume) return subscriber.#resume.received(); + const producer = subscriber.#state.producer; + return producer !== undefined && fed(producer); + }; hooks.bufferedLargest = (subscriber) => subscriber.#bufferedLargest(); hooks.objectLargest = (subscriber) => { if (subscriber.#resume) return subscriber.#resume.objectLargest(); diff --git a/quest/m1/README.md b/quest/m1/README.md index 366834342a9..029602583aa 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -182,7 +182,7 @@ their end report, so an embedder's drain alert pages on real cuts. - [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport - [Vendor and trim the WebRTC stack](/quest/m1/webrtc-vendor.md) - str0m, dimpl, `is`, and sctp-proto live in moq-webrtc with unused code removed, so moq-rtc can take rind - [MoQ on rind](/quest/m1/rind/README.md) - every MoQ crate takes its crypto from rind, a pure-Rust graviola fork, so no build compiles C crypto -- [Finish request-time relay join snapshots](/quest/m1/relay-inflight-replay.md) - settle the fourth-round checkpoint and remaining request snapshot findings +- [Legacy relay joins start at the answer's live edge](/quest/m1/relay-inflight-replay.md) - a lite-03/04 join on a relay copy settles 50 ms past the upstream answer and starts at its newest group ## Related diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md index 6d4a34ffa73..886d76be25a 100644 --- a/quest/m1/relay-inflight-replay.md +++ b/quest/m1/relay-inflight-replay.md @@ -1,37 +1,32 @@ -# [S] Finish request-time relay join snapshots +# [S] Legacy relay joins start at the answer's live edge ## Goal -Pending lite-03/04/05 joins skip history before their request-time answer and -deliver every group produced afterward, in Rust and JS. +A lite-03/04 SUBSCRIBE without a start, served by a Rust or JS relay from a +copy its upstream feeds, starts at the newest group the upstream's answer +brings, never at history the relay's cache or feed holds below it. ## Plan -Decided 2026-11-03 (maintainer, /quest-unblock): keep the request-time floor -and land after HLS grid export. The grid prerequisite has landed. - -Decided 2026-11-04 (maintainer, /quest-unblock): subscription front with -metadata but no current copy: ✅ defer the wire join until the next copy -supplies its snapshot. Not chosen: joining at the edge the metadata implies. - -Decided 2026-11-04 (maintainer, /quest-unblock): lite-01/02 parity: -✅ mirror Rust's arrival-only exception in JS. Not chosen: leaving JS different. - -Both decisions are implemented. Pending Rust fronts retain a shared immutable -first-answer cell across splices and copyless intervals. The provisional-install -finding was refuted with a passing visibility regression and accepted by the -local reviewer. Local Claude round 4 reviewed implementation -[8a321d144a](https://github.com/moq-dev/moq/commit/8a321d144aeed36eefca778a831ffca1658f2b45) -with zero findings; local CI (10,814 Rust tests plus JS/repository checks) and -full interop passed. - -Subsequent online findings remain: -- Rust captures the snapshot after demand registration wakes producers, allowing - demand-triggered groups to enter the floor. Capture before publishing demand. -- JS accept uses nullish assignment, which overwrites an empty first snapshot - on a repeated accept. Preserve null and assign only when undefined. -- Add routed pending JS coverage, so the copy-to-front snapshot handoff is tested. - -## Waiting - -- Maintainer: the fourth local review round triggers the required design checkpoint before further fixes. Options: continue with the targeted fixes and routed regression above (recommended; the shared immutable snapshot design remains bounded and appropriate), or rethink the approach. Once authorized, implement the remaining findings, rerun checks and local review, and mark the PR ready. +Decided 2026-11-05 (maintainer, /quest-unblock), replacing the 2026-11-03 +request-time floor and the 2026-11-04 copyless-front decision where they +conflict: ✅ rethink after the fourth review round's design checkpoint. Not +chosen: continuing the shared immutable snapshot design with targeted fixes. + +- A pending join's start comes from the upstream answer's latest group, + never from the relay's cache. Drop the snapshot machinery this makes + unnecessary. +- An answer that carries no largest group waits about 50 ms past the answer + and starts from the newest group received by then. Lite-03/04's + SUBSCRIBE_OK carries none (Rust sends no `start_group`; JS echoes the + requested one), nor do lite-01..06 answers. Mock time in its tests. +- Legacy lite-03/04 may keep minor known bugs; the PR body lists them. + +Implemented: the publisher settles a lite-03/04 join that found no newest +group on arrival when a wire subscription feeds its track (a received copy, +or a front whose serving copy is one), then floors at the newest group known, +which includes a largest a lite-07 or moq-transport answer reported. Local +tracks and lite-05 take the newest group once the track resolves (lite-05's +first-frame latency allows no wait); lite-01/02 keep their arrival floor +(the 2026-11-04 parity decision still holds). Remaining: the restarted local +review rounds (`quest review --new`). diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index f175bfd6fb5..5b78da95fbf 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -123,6 +123,12 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option version.carries_max_delay().then_some(requested) } +/// How long a lite-03/04 join on a received copy that held no group on arrival waits past the +/// answer before it takes the newest group as the live edge. Lite01-06 answers carry no +/// largest group (lite-03/04's SUBSCRIBE_OK names only the requested start), so the newest +/// group to arrive soon after the answer stands in, and a later one older than it is skipped. +const SETTLE: Duration = Duration::from_millis(50); + /// The floor of a subscription's read cursor for the wire serving it. /// /// On lite-06 there is nothing to do: `Consumer::subscribe` resolves the cursor from the @@ -135,14 +141,14 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// an unbounded budget so nothing is dropped under them (see [`serving_max_delay`]), which /// must not read as a request to replay the whole cache on join. /// -/// `latest` is the newest group when the SUBSCRIBE arrived, not once the subscription -/// resolved: anything written in between is newer, so it is delivered. On lite03-05, a -/// SUBSCRIBE on a copy still resolving (fetching upstream, served on request, or alongside -/// its TRACK on lite-05) learns that newest group from the answer: the newest the copy held -/// once it resolved ([`track::Subscriber::requested_sequence`]), or the first group when it -/// held none. Cached history below it is skipped, and anything written after it, even in -/// response to this subscription's demand, is delivered. Lite01/02 keep a zero floor when no -/// group was known on arrival. +/// `latest` is the newest group when the SUBSCRIBE arrived: anything written since is newer, +/// so it is delivered. A SUBSCRIBE that found none, such as one that beat its track resolving, +/// takes the newest group once the track resolves. On a lite-03/04 copy received from a +/// remote publisher that group is not what the source's answer brings, but an older one +/// cached or none yet: the join waits [`SETTLE`] past the answer and starts at the newest group +/// known by then, which includes the largest a source's answer reported (lite-07's +/// SUBSCRIBE_START, moq-transport's SUBSCRIBE_OK). Lite01/02 keep a zero floor when no group +/// was known on arrival. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -2278,6 +2284,17 @@ enum SubscribeServe { update: Option, legacy: Option>, }, + /// A lite-03/04 join on a received copy, waiting [`SETTLE`] past the answer for the newest + /// group it brings; see [`cursor_floor`]. + Settle { + /// Boxed: the request is far larger than the other states' handles. + msg: Box>, + bounds: Bounds, + track: Box, + due: crate::time::Deadline, + update: Option, + legacy: Option>, + }, /// Streaming groups. Boxed: by far the largest state, and the enum is moved on every /// transition. Run(Box), @@ -2357,7 +2374,9 @@ impl Request for SubscribeServe { fn update(&mut self, update: LiveUpdate) { match self { - Self::Query { update: pending, .. } | Self::Confirm { update: pending, .. } => *pending = Some(update), + Self::Query { update: pending, .. } + | Self::Confirm { update: pending, .. } + | Self::Settle { update: pending, .. } => *pending = Some(update), Self::Run(run) => run.update(update), Self::Datagrams { consumer, @@ -2433,7 +2452,7 @@ impl Request for SubscribeServe { subscribing, legacy, .. } => { let ietf = ready!(shared.peer_setup.poll_ietf(waiter)); - let track = match subscribing.poll_wire(waiter) { + let track = match subscribing.poll_ok(waiter) { Poll::Ready(track) => track, Poll::Pending => { let consumer = ready!(Legacy::poll_datagrams(legacy, waiter)); @@ -2474,68 +2493,53 @@ impl Request for SubscribeServe { continue; } }; - crate::ietf::flate::egress(track.info(), shared.version.has_compression())?; - - // `None` omits the per-frame timestamp prefix: an untimed track on - // Lite07+, or any track before Lite05 (whose frames still carry - // timestamps in the model, just not on this wire). - let stamp = Stamp::new(shared.version, track.info().timescale); - - // Lite05+ accepts implicitly: no SUBSCRIBE_OK, the immutable properties - // live in TRACK_INFO, and the resolved range arrives as - // SUBSCRIBE_START/END from the run loop. Older drafts still acknowledge - // with SUBSCRIBE_OK here. - if !shared.version.has_track_stream() { - let info = lite::SubscribeOk { - priority: msg.priority, - max_delay: Duration::ZERO, - start_group: None, - end_group: None, + // A copy's newest group then is not what its source's answer brings: an older + // one cached, or none yet. + if latest.is_none() + && bounds.groups().start().is_none() + && matches!(shared.version, Version::Lite03 | Version::Lite04) + && track.is_received() + { + *self = Self::Settle { + msg, + bounds, + track: Box::new(track), + due: crate::time::Deadline::after(&shared.runtime, SETTLE), + update, + legacy, }; - writer.buffer(&lite::SubscribeResponse::Ok(info))?; + continue; } - - // Track-level subscriber priority and group order. SUBSCRIBE_UPDATE - // messages broadcast new values to the run loop (so future groups inherit - // them) and the in-flight group machines (so they update via - // PriorityHandle::steer). - let steer = Steer { - priority: msg.priority, - order: msg.order, - }; - let steer_tx = kio::Producer::new(steer); - - let session = shared.session.clone(); - let publisher = track.info().priority; - let timescale = track.info().timescale; - let group = session.send_group(crate::priority::group(msg.priority, publisher)); - let sub = Subscription { - session, - id: msg.id, - track_name: Arc::from(track.name()), - priority: shared.priority.clone(), - publisher, - group, - steer: steer_tx.consume(), - steer_seen: steer, - version: shared.version, - stamp, - runtime: shared.runtime.clone(), - outcomes: Default::default(), - ietf, - }; - + // A local track's newest group once it resolves is its answer's. let latest = match shared.version { Version::Lite01 | Version::Lite02 => latest, - _ => latest.or_else(|| track.requested_sequence()), + _ => latest.or_else(|| track.latest_sequence()), }; - let floor = cursor_floor(shared.version, bounds.groups().start(), latest); - let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); - run.datagrams = LegacyDatagrams::join(legacy, msg.id, msg.priority, timescale); - if let Some(update) = update { - run.update(update); - } - *self = Self::Run(Box::new(run)); + *self = Self::run(shared, *msg, bounds, track, latest, update, legacy, ietf, writer)?; + } + Self::Settle { due, .. } => { + let ietf = ready!(shared.peer_setup.poll_ietf(waiter)); + ready!(due.poll(waiter)); + let Self::Settle { + msg, + bounds, + mut track, + update, + legacy, + .. + } = std::mem::replace( + self, + Self::Drain { + children: Default::default(), + _track: None, + }, + ) + else { + unreachable!() + }; + // Past what the cache holds once the source reported a newer group. + let latest = track.peek_largest().map(|largest| largest.group); + *self = Self::run(shared, *msg, bounds, *track, latest, update, legacy, ietf, writer)?; } Self::Run(run) => { // One datagram a step, beside the groups. @@ -2957,6 +2961,80 @@ impl SubscribeServe { } } + /// Serve the confirmed `track` from `latest`, the newest group as the join starts; see + /// [`cursor_floor`]. + #[allow(clippy::too_many_arguments)] + fn run( + shared: &Shared, + msg: lite::Subscribe<'static>, + bounds: Bounds, + track: track::Subscriber, + latest: Option, + update: Option, + legacy: Option>, + ietf: bool, + writer: &mut Writer, + ) -> Result { + crate::ietf::flate::egress(track.info(), shared.version.has_compression())?; + + // `None` omits the per-frame timestamp prefix: an untimed track on + // Lite07+, or any track before Lite05 (whose frames still carry + // timestamps in the model, just not on this wire). + let stamp = Stamp::new(shared.version, track.info().timescale); + + // Lite05+ accepts implicitly: no SUBSCRIBE_OK, the immutable properties + // live in TRACK_INFO, and the resolved range arrives as + // SUBSCRIBE_START/END from the run loop. Older drafts still acknowledge + // with SUBSCRIBE_OK here. + if !shared.version.has_track_stream() { + let info = lite::SubscribeOk { + priority: msg.priority, + max_delay: Duration::ZERO, + start_group: None, + end_group: None, + }; + writer.buffer(&lite::SubscribeResponse::Ok(info))?; + } + + // Track-level subscriber priority and group order. SUBSCRIBE_UPDATE + // messages broadcast new values to the run loop (so future groups inherit + // them) and the in-flight group machines (so they update via + // PriorityHandle::steer). + let steer = Steer { + priority: msg.priority, + order: msg.order, + }; + let steer_tx = kio::Producer::new(steer); + + let session = shared.session.clone(); + let publisher = track.info().priority; + let timescale = track.info().timescale; + let group = session.send_group(crate::priority::group(msg.priority, publisher)); + let sub = Subscription { + session, + id: msg.id, + track_name: Arc::from(track.name()), + priority: shared.priority.clone(), + publisher, + group, + steer: steer_tx.consume(), + steer_seen: steer, + version: shared.version, + stamp, + runtime: shared.runtime.clone(), + outcomes: Default::default(), + ietf, + }; + + let floor = cursor_floor(shared.version, bounds.groups().start(), latest); + let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); + run.datagrams = LegacyDatagrams::join(legacy, msg.id, msg.priority, timescale); + if let Some(update) = update { + run.update(update); + } + Ok(Self::Run(Box::new(run))) + } + /// Serve `msg` from the name's datagram track, at the priority of its latest `update`. fn datagrams(mut consumer: datagram::Consumer, msg: &lite::Subscribe<'static>, update: Option) -> Self { let priority = update.map_or(msg.priority, |update| update.priority); @@ -9460,18 +9538,46 @@ mod tests { } } - /// A join on an unresolved copy floors at the newest group as of its request, which the - /// answer gives: history the answer holds is skipped, and groups written after it, even - /// before the subscription is served, are delivered. Named starts and joins whose live - /// edge was already known keep their original floor. + /// A lite-03/04 join that beats a received copy resolving waits [`SETTLE`] past the + /// answer and starts at the newest group known by then: one the answer reported but has + /// yet to deliver, or the newest delivered. A later group older than it is skipped, while + /// groups first arriving past the settle are all delivered. A local track's join, and any + /// on lite-05, starts at the newest group as it resolves. Named starts, lite-06, and joins + /// whose newest group was known on arrival keep their floors. #[kio::test] - async fn a_pending_subscribe_joins_where_the_answer_stood() { + async fn a_pending_subscribe_joins_the_newest_group_after_the_answer() { use crate::lite::test_transport::{Log, Sent}; + enum Answer { + /// Groups 0, 3, and 7 written before the SUBSCRIBE arrives. + Resolved, + /// The answer holds groups 0, 3, and 7. + Cached, + /// Groups 0, 3, and 7 arrive within the settle. + Settling, + /// The answer holds group 3 and reports group 9 as its largest. + Reported, + /// Groups 0, 3, and 7 arrive only past the settle. + Late, + } + + fn write(track: &mut track::Producer, sequences: &[u64]) { + for &sequence in sequences { + let mut group = track.create_group(group::Info { sequence }).unwrap(); + group.write_frame(crate::Timestamp::ZERO, b"x".as_slice()).unwrap(); + group.finish().unwrap(); + } + } + for version in [Version::Lite03, Version::Lite04, Version::Lite05, Version::Lite06] { - // Unresolved on arrival: whether the answer holds the groups, or they follow it. - for (unresolved, cached) in [(true, true), (true, false), (false, false)] { - for start in [None, Some(0)] { + for answer in [ + Answer::Resolved, + Answer::Cached, + Answer::Settling, + Answer::Reported, + Answer::Late, + ] { + for (start, received) in [(None, false), (None, true), (Some(0), true)] { let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); let broadcast = origin.create_broadcast("room").unwrap(); let mut dynamic = broadcast.requests(); @@ -9480,16 +9586,16 @@ mod tests { let held = copy.info().into_inner(); assert!(held.poll_ok(&kio::Waiter::noop()).is_pending()); let request = dynamic.requested_track().await.unwrap(); - let staged = request.stage(track::Info::timed()); - if !unresolved { - for sequence in [0, 3] { - let mut group = staged.create_group(group::Info { sequence }).unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"x".as_slice()).unwrap(); - group.finish().unwrap(); - } + let mut staged = Some(request.stage(track::Info::timed())); + if received { + staged.as_ref().unwrap().received(); + } + let mut track = None; + if let Answer::Resolved = answer { + let mut resolved = staged.take().unwrap().commit(); + write(&mut resolved, &[0, 3]); + track = Some(resolved); } - let mut staged = Some(staged); - let mut track = (!unresolved).then(|| staged.take().unwrap().commit()); let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup); peer_setup.set(crate::lite::Setup::default()); @@ -9526,46 +9632,61 @@ mod tests { crate::transport::boxed::SendStream::new(SinkSend::new(Log::default())), version, ); - if unresolved { - assert!( - serve - .poll(&publisher.shared, &mut writer, &kio::Waiter::noop()) - .is_pending() - ); - } - // The answer exposes retained history below the live edge, and advertises - // a source start below it. A join must use the newest group, not either. - // An answer without them is followed by them, before the join is served. - if unresolved && !cached { - track = Some(staged.take().unwrap().commit()); - } - let producer = match &mut track { - Some(track) => track, - None => &mut *staged.as_mut().unwrap(), + // Serve for `wait`: a live subscription never ends on its own. + let mut drive = async |wait: Duration| { + let outcome = kio::clock::timeout( + wait, + kio::wait(|waiter| { + loop { + match ready!(serve.poll(&publisher.shared, &mut writer, waiter))? { + ControlFlow::Continue(()) => {} + ControlFlow::Break(()) => return Poll::Ready(Ok::<_, Error>(())), + } + } + }), + ) + .await; + assert!(outcome.is_err(), "the live subscription ended"); }; - producer.start_at(0).unwrap(); - for &sequence in if unresolved { &[0, 3, 7][..] } else { &[7][..] } { - let mut group = producer.create_group(group::Info { sequence }).unwrap(); - group.write_frame(crate::Timestamp::ZERO, b"x".as_slice()).unwrap(); - group.finish().unwrap(); - } - if cached { - track = Some(staged.take().unwrap().commit()); + drive(Duration::from_millis(1)).await; + + let mut track = match answer { + Answer::Resolved => { + let mut track = track.take().unwrap(); + write(&mut track, &[7]); + track + } + Answer::Cached => { + let mut staged = staged.take().unwrap(); + write(&mut *staged, &[0, 3, 7]); + staged.commit() + } + Answer::Settling => { + let mut track = staged.take().unwrap().commit(); + drive(Duration::from_millis(10)).await; + write(&mut track, &[0, 3, 7]); + track + } + Answer::Reported => { + let mut staged = staged.take().unwrap(); + write(&mut *staged, &[3]); + staged.report_largest(track::Position::group(9)); + staged.commit() + } + Answer::Late => { + let track = staged.take().unwrap().commit(); + drive(SETTLE * 2).await; + track + } + }; + drive(SETTLE * 2).await; + match answer { + Answer::Late => write(&mut track, &[0, 3, 7]), + Answer::Reported => write(&mut track, &[7, 9]), + _ => {} } - let track = track.unwrap(); - let outcome = kio::clock::timeout( - Duration::from_millis(1), - kio::wait(|waiter| { - loop { - match ready!(serve.poll(&publisher.shared, &mut writer, waiter))? { - ControlFlow::Continue(()) => {} - ControlFlow::Break(()) => return Poll::Ready(Ok::<_, Error>(())), - } - } - }), - ) - .await; - assert!(outcome.is_err(), "the live subscription ended"); + drive(Duration::from_millis(1)).await; + let mut streams = std::collections::BTreeMap::<_, Vec>::new(); for (id, sent) in log.trail() { if let Sent::Write(bytes) = sent { @@ -9583,17 +9704,24 @@ mod tests { }) .collect::>(); sequences.sort(); - let expected = if start.is_some() || version.resolves_start() || (unresolved && !cached) { - vec![0, 3, 7] - } else if unresolved { - vec![7] + let all: &[u64] = match answer { + Answer::Reported => &[3, 7, 9], + _ => &[0, 3, 7], + }; + let settles = received && matches!(version, Version::Lite03 | Version::Lite04); + let expected: &[u64] = if start.is_some() || version.resolves_start() { + all } else { - vec![3, 7] + match answer { + Answer::Resolved => &[3, 7], + Answer::Cached => &[7], + Answer::Settling | Answer::Reported if !settles => all, + Answer::Settling => &[7], + Answer::Reported => &[9], + Answer::Late => &[0, 3, 7], + } }; - assert_eq!( - sequences, expected, - "{version}, unresolved={unresolved}, cached={cached}, start={start:?}" - ); + assert_eq!(sequences, expected, "{version}, start={start:?}, received={received}"); track.abort(Error::Cancel).unwrap(); } } diff --git a/rs/moq-net/src/model/origin.rs b/rs/moq-net/src/model/origin.rs index 94574bd5d8b..b08a1aaec0a 100644 --- a/rs/moq-net/src/model/origin.rs +++ b/rs/moq-net/src/model/origin.rs @@ -3297,17 +3297,15 @@ struct TrackIo { } impl TrackIo { - /// Serve the track from `asked`'s copy, accepting it with that copy's info. Spliced - /// first, so a subscription that resolves with the accept finds the copy it joins. + /// Serve the track from `asked`'s copy, accepting it with that copy's info first. fn splice(&mut self, asked: Asked) { self.ready.wake_by_ref(); - let resolved = asked.query.requested_sequence(); - self.routes.splice(asked.copy.clone(), asked.staged, resolved); if let Some(info) = asked.info.clone() && let Some(request) = self.request.take() { self.accepted = Some(request.accept(info)); } + self.routes.splice(asked.copy.clone(), asked.staged); self.copy = Some((asked.source, asked.copy, asked.query)); self.held = asked.sub; } diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 266ceea1eb9..b293a262092 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -74,11 +74,6 @@ struct Route { staged: Option<(u64, track::Consumer)>, /// The stage the serving copy was spliced from, if any. spliced: Option, - /// The newest sequence as of the front's request for the serving copy, where a - /// subscription made before the track resolved joins; see [`track::Subscriber::requested_sequence`]. - resolved: Option, - /// Shared by requests waiting for the next splice, then frozen for those requests. - answer: Arc>>, /// The id the next stage takes. next_stage: u64, } @@ -197,23 +192,18 @@ impl Producer { /// Serve the track from `copy` from now on, subscribing every reader to it. #[cfg(test)] pub(super) fn serve(&self, copy: track::Consumer) { - let resolved = copy.latest_sequence(); - self.serve_from(copy, None, resolved); + self.serve_from(copy, None); } /// Serve the track from `copy`, the one `staged` was made for, subscribing every reader - /// to it: what was fetched from it is handed out from here on. `resolved` is the newest - /// sequence as of the front's request for it. - pub(super) fn splice(&self, copy: track::Consumer, staged: Staged, resolved: Option) { - self.serve_from(copy, Some(staged.id), resolved); + /// to it: what was fetched from it is handed out from here on. + pub(super) fn splice(&self, copy: track::Consumer, staged: Staged) { + self.serve_from(copy, Some(staged.id)); } - fn serve_from(&self, copy: track::Consumer, stage: Option, resolved: Option) { + fn serve_from(&self, copy: track::Consumer, stage: Option) { if let Ok(mut route) = self.state.write() { route.generation += 1; - let _ = route.answer.set(resolved); - route.answer = Default::default(); - route.resolved = resolved; route.copy = Some(copy); route.parked = None; route.spliced = stage; @@ -327,34 +317,6 @@ impl Consumer { self.state.read().copy.clone() } - /// The first copy's answer for a request that has no arrival snapshot. - pub(super) fn answer(&self) -> Arc>> { - let route = self.state.read(); - if route.copy.is_some() || route.parked.is_some() { - Arc::new(std::sync::OnceLock::from(route.resolved)) - } else { - route.answer.clone() - } - } - - /// Wait for a copy to supply a join snapshot, or for the route to end. - pub(super) fn poll_join(&self, waiter: &kio::Waiter) -> Poll<()> { - self.state - .poll(waiter, |route| { - if route.copy.is_some() || route.parked.is_some() || route.phase.terminal() { - Poll::Ready(()) - } else { - Poll::Pending - } - }) - .map(|_| ()) - } - - /// The newest sequence as of the front's request for the serving copy, as it was spliced. - pub(super) fn resolved(&self) -> Option { - self.state.read().resolved - } - /// The copy the front parked, while it serves none and may still serve one again. pub(super) fn parked(&self) -> Option { let route = self.state.read(); @@ -967,6 +929,14 @@ impl Subscriber { self.reader().latest() } + /// Whether the copy serving, or parked, was received from a remote publisher. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(super) fn is_received(&self) -> bool { + let reader = self.reader(); + let copy = reader.route.serving().or_else(|| reader.route.parked()); + copy.is_some_and(|copy| copy.is_received()) + } + /// Poll for a largest location within `bounds` newer than `seen`; see [`Consumer::poll_largest`]. pub(super) fn poll_largest( &mut self, @@ -2559,61 +2529,22 @@ mod test { .accept(crate::track::Info::timed()) } + /// A front's reader is fed remotely when its serving copy is, or its parked one. #[test] - fn a_wire_join_waits_for_a_copy_despite_retained_metadata() { + fn a_front_reader_is_received_through_its_copy() { let routes = Producer::new(); let logical = logical(&routes); - let pending = logical.consume().subscribe(None); - let noop = kio::Waiter::noop(); - assert!(pending.poll_wire(&noop).is_pending()); - let copy = copy(); - copy.create_group(group::Info { sequence: 7 }) - .unwrap() - .finish() - .unwrap(); - routes.serve(copy.consume()); - copy.create_group(group::Info { sequence: 8 }) - .unwrap() - .finish() - .unwrap(); - let Poll::Ready(Ok(sub)) = pending.poll_wire(&noop) else { - panic!("the copy supplies the snapshot"); - }; - assert_eq!(sub.requested_sequence(), Some(7)); - } - - #[test] - fn a_pending_wire_join_keeps_the_first_answer_across_splices() { - let routes = Producer::new(); - let logical = logical(&routes); - let pending = logical.consume().subscribe(None); - let info = pending.info(); - let first = copy(); - first - .create_group(group::Info { sequence: 7 }) - .unwrap() - .finish() - .unwrap(); - routes.serve(first.consume()); + let reader = subscribe(&logical, Duration::from_secs(1)); + assert!(!reader.is_received(), "no copy serves yet"); + let local = copy(); + routes.serve(local.consume()); + assert!(!reader.is_received(), "a local copy"); + let remote = copy(); + remote.received(); + routes.serve(remote.consume()); + assert!(reader.is_received(), "a received copy"); routes.park(); - routes.unpark(); - let second_pending = logical.consume().subscribe(None); - let second = copy(); - second - .create_group(group::Info { sequence: 9 }) - .unwrap() - .finish() - .unwrap(); - routes.serve(second.consume()); - let Poll::Ready(Ok(first_sub)) = pending.poll_wire(&kio::Waiter::noop()) else { - panic!("first answer"); - }; - let Poll::Ready(Ok(second_sub)) = second_pending.poll_wire(&kio::Waiter::noop()) else { - panic!("second answer"); - }; - assert_eq!(first_sub.requested_sequence(), Some(7)); - assert_eq!(info.requested_sequence(), Some(7)); - assert_eq!(second_sub.requested_sequence(), Some(9)); + assert!(reader.is_received(), "a parked received copy"); } fn subscribe(logical: &track::Producer, max_delay: Duration) -> track::Subscriber { @@ -3629,7 +3560,7 @@ mod test { ); let _logical = logical.accept(crate::track::Info::timed()); - routes.splice(copy.consume(), staged, None); + routes.splice(copy.consume(), staged); let mut fetched = fetching.now_or_never().expect("spliced").unwrap(); assert_eq!(read(&mut fetched), Some(b"early".to_vec())); } @@ -3666,7 +3597,7 @@ mod test { group.write_frame(ts(0), b"right".as_ref()).unwrap(); group.finish().unwrap(); let _logical = logical.accept(crate::track::Info::timed()); - routes.splice(copy.consume(), staged, None); + routes.splice(copy.consume(), staged); let mut fetched = fetching.now_or_never().expect("spliced").unwrap(); assert_eq!(read(&mut fetched), Some(b"right".to_vec())); } diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index b4582b15523..b5a08bb8d7d 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -421,10 +421,6 @@ pub(crate) struct TrackState { // relative starts count back from (see `latest_live`). newest_produced: Option, - // `max_sequence` when waiting subscribers first resolved, or `None` before then; see - // `TrackState::request_answered`. - resolved: Option>, - // The checked starts cached groups hold, by sequence (see [`group::Origin::Bound`]), so a // first frame finds its neighbors without walking groups not yet stamped. Filled through // the read guard a first frame holds, so it needs its own lock, never contended. @@ -1203,7 +1199,6 @@ impl TrackState { fn commit(&mut self) { debug_assert_eq!(self.publication, Publication::Staged, "only a staged track commits"); self.publication = Publication::Published; - self.resolve(); } /// Mint the Producer and resolve waiting subscribers at once. @@ -2113,51 +2108,6 @@ impl TrackState { if self.info.replace(info).is_some() { self.cache.wakes().wake_all(); } - if self.publication != Publication::Staged { - self.resolve(); - } - } - - /// Record the newest sequence as waiting subscribers resolve (see [`Self::poll_info`]), - /// the first time they do. - fn resolve(&mut self) { - self.resolved.get_or_insert(self.max_sequence); - } - - /// The newest sequence as of a request made now: `Some` with it (`None` within for none) - /// once the track resolved, or `None` while it still resolves, when [`Self::request_answered`] - /// says once it does. A logical track reads the copy serving it, or the parked one. - fn requested(state: &kio::Consumer) -> Option> { - let (ready, max, routes) = { - let state = state.read(); - (state.poll_info().is_ready(), state.max_sequence, state.routes.clone()) - }; - if !ready { - return None; - } - // Read outside the track's lock: the copy has a lock of its own. - match routes { - Some(routes) => routes - .serving() - .or_else(|| routes.parked()) - .map(|copy| copy.latest_sequence()), - None => Some(max), - } - } - - /// The newest sequence a request made while the track was still resolving finds in its - /// answer: what the track held once it resolved, or a logical track's serving copy as of - /// the front's own request for it. Nothing written after the answer counts, even in - /// response to the request's own demand: it is newer, so it is delivered. - fn request_answered(state: &kio::Consumer) -> Option { - let (resolved, routes) = { - let state = state.read(); - (state.resolved.flatten(), state.routes.clone()) - }; - match routes { - Some(routes) => routes.resolved(), - None => resolved, - } } /// Create the shared state for a track under `broadcast`, along with the cache @@ -3941,7 +3891,6 @@ impl Producer { // subscriber surfaces the close/abort on its first read; the preferences are // simply never registered (nothing aggregates them anymore). let info = self.info.clone(); - let requested = self.state.read().max_sequence; let subscription = Preferences::register(self.state.read(), preferences); // Hoisted: an inline `read()` guard would live to the end of the struct literal, @@ -3952,7 +3901,6 @@ impl Producer { broadcast, info, inner: Inner::Plain(Cursor::new(self.state.consume(), subscription)), - requested, delivered: Delivered::default(), // A producer-side (in-process) subscribe is not egress: stay untagged. stats: stats::Scope::default(), @@ -4854,11 +4802,6 @@ impl Consumer { pub fn subscribe(&self, subscription: impl Into>) -> kio::Pending { let (state, subscription) = self.resolve(Some(&subscription.into().unwrap_or_default())); let subscription = subscription.expect("a subscription was requested"); - if let Some(newest) = TrackState::requested(&state) { - subscription.set_requested(newest); - } else if let Some(routes) = state.read().routes.clone() { - subscription.set_answer(routes.answer()); - } kio::Pending::new(Subscribing { name: self.name.clone(), @@ -5289,14 +5232,10 @@ impl Consumer { /// for a relay), and errors with the track's abort error if it closes first. /// [`Subscriber::info`] is the already-resolved counterpart. pub fn info(&self) -> kio::Pending { - let state = self.resolve(None).0; - let answer = state.read().routes.as_ref().map(|routes| routes.answer()); kio::Pending::new(Resolving { name: self.name.clone(), broadcast: self.broadcast.clone(), - requested: TrackState::requested(&state), - answer, - state, + state: self.resolve(None).0, stats: self.stats.clone(), }) } @@ -5353,6 +5292,16 @@ impl Consumer { self.with_state(|state| state?.read().max_sequence) } + /// Whether a remote publisher feeds this track: a copy received from one, or a front's + /// track it serves through one. + #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] + pub(crate) fn is_received(&self) -> bool { + if let Some(serving) = self.serving() { + return serving.is_received(); + } + self.with_state(|state| state.is_some_and(|state| state.read().received)) + } + /// The first group the declared end excludes whole, or `None` while the track is open ended. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn final_sequence(&self) -> Option { @@ -5420,22 +5369,10 @@ impl Subscribing { name: self.name.clone(), broadcast: self.broadcast.clone(), state: self.state.clone(), - requested: self.subscription.requested(), - answer: self.subscription.answer(), stats: self.stats.clone(), } } - /// Wait for a front's copy before pinning a wire join from retained metadata. - pub(crate) fn poll_wire(&self, waiter: &kio::Waiter) -> Poll> { - if self.subscription.requested().is_none() - && let Some(routes) = self.state.read().routes.clone() - { - ready!(routes.poll_join(waiter)); - } - self.poll_ok(waiter) - } - /// Poll until the peer confirms the subscription, yielding the [`Subscriber`]. /// Errors if the track is aborted or not found. pub fn poll_ok(&self, waiter: &kio::Waiter) -> Poll> { @@ -5460,10 +5397,6 @@ impl Subscribing { broadcast: self.broadcast.clone(), info, inner, - requested: self - .subscription - .requested() - .unwrap_or_else(|| TrackState::request_answered(&self.state)), delivered: Delivered::default(), stats: self.stats.clone(), _stats_sub: self.stats.subscribe(), @@ -5500,9 +5433,6 @@ pub struct Resolving { name: Arc, broadcast: Arc, state: kio::Consumer, - /// The newest sequence when requested, if the track had resolved; see [`TrackState::requested`]. - requested: Option>, - answer: Option>>>, stats: stats::Scope, } @@ -5525,15 +5455,6 @@ impl Resolving { .map_err(|e| e.abort.clone().unwrap_or(Error::Dropped))??; Poll::Ready(Ok(info)) } - - /// The newest sequence as of this request, once [`Self::poll_ok`] is ready; see - /// [`Subscriber::requested_sequence`]. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn requested_sequence(&self) -> Option { - self.requested - .or_else(|| self.answer.as_ref()?.get().copied()) - .unwrap_or_else(|| TrackState::request_answered(&self.state)) - } } impl kio::Task for Resolving { @@ -5841,8 +5762,6 @@ pub struct Subscriber { broadcast: Arc, info: Info, inner: Inner, - /// The newest sequence as of the request; see [`Self::requested_sequence`]. - requested: Option, /// The groups handed out, so none is handed out twice: not when its sequence is /// re-created, nor when another route delivers it too. delivered: Delivered, @@ -8932,20 +8851,22 @@ impl Subscriber { } } + /// Whether a remote publisher feeds this track: a copy received from one, or a front's + /// logical track served through one. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn latest_sequence(&self) -> Option { + pub(crate) fn is_received(&self) -> bool { match &self.inner { - Inner::Plain(cursor) => cursor.state.read().max_sequence, - Inner::Resume(resume, _) => resume.latest(), + Inner::Plain(cursor) => cursor.state.read().received, + Inner::Resume(resume, _) => resume.is_received(), } } - /// The newest sequence as of this subscription's request, `None` when there was none; - /// see [`TrackState::requested`]. Unlike [`Self::latest_sequence`], nothing written since - /// counts, even in response to this subscription's own demand. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn requested_sequence(&self) -> Option { - self.requested + pub(crate) fn latest_sequence(&self) -> Option { + match &self.inner { + Inner::Plain(cursor) => cursor.state.read().max_sequence, + Inner::Resume(resume, _) => resume.latest(), + } } /// Join again at the live edge, judged by [`Info::refresh`] like a new subscriber: a @@ -9486,27 +9407,6 @@ mod test { Producer::new(Arc::new(broadcast::Info::default()), name, info) } - #[test] - fn a_provisional_install_is_the_first_visible_answer() { - let request = Request::new(Arc::new(broadcast::Info::default()), "test"); - let consumer = request.consume(); - let pending = consumer.subscribe(None); - { - let mut state = request.state.write().ok().unwrap(); - let _group = state - .insert_group_request(3, group::Bounds { start: 0, end: Some(1) }, None) - .unwrap(); - assert!(state.poll_info().is_ready()); - } - let Poll::Ready(Ok(sub)) = pending.poll_ok(&kio::Waiter::noop()) else { - panic!("provisional metadata already resolves subscribers"); - }; - assert_eq!(sub.requested_sequence(), None); - // A later publisher acceptance must not move the first visible answer's boundary. - let producer = request.accept(Info::default().with_max_age(None)); - assert_eq!(TrackState::request_answered(&producer.state.consume()), None); - } - #[test] fn drop_changes_follow_split_coalesced_and_recreated_runs() { let producer = track_producer("test", Info::default()); diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index e0ea28236e9..8fc49494f23 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -209,10 +209,6 @@ struct Preference { subscriptions: Option>, /// Bumped under the write whenever the ranges change. ranges_revision: AtomicU64, - /// The track's newest sequence when the subscription was requested, set only if the track - /// had resolved by then; see `TrackState::requested`. - requested: std::sync::OnceLock>, - answer: std::sync::OnceLock>>>, } impl Preferences { @@ -227,8 +223,6 @@ impl Preferences { value: kio::Producer::new(value), subscriptions, ranges_revision: AtomicU64::new(0), - requested: Default::default(), - answer: Default::default(), })) } @@ -269,20 +263,6 @@ impl Preferences { pub(in crate::model) fn consume(&self) -> kio::Consumer { self.0.value.consume() } - /// Record the track's newest sequence as the subscription is requested, once. - pub(in crate::model) fn set_requested(&self, newest: Option) { - let _ = self.0.requested.set(newest); - } - /// The track's newest sequence when the subscription was requested, if it had resolved. - pub(in crate::model) fn requested(&self) -> Option> { - self.0.requested.get().or_else(|| self.0.answer.get()?.get()).copied() - } - pub(in crate::model) fn answer(&self) -> Option>>> { - self.0.answer.get().cloned() - } - pub(in crate::model) fn set_answer(&self, answer: Arc>>) { - let _ = self.0.answer.set(answer); - } pub(in crate::model) fn poll_ref( &self, waiter: &kio::Waiter, @@ -311,8 +291,6 @@ impl From> for Preferences { value, subscriptions: None, ranges_revision: AtomicU64::new(0), - requested: Default::default(), - answer: Default::default(), })) } } diff --git a/rs/moq-net/tests/integration/widened_max_age.rs b/rs/moq-net/tests/integration/widened_max_age.rs index 6c2bc21f068..a79ddb930a7 100644 --- a/rs/moq-net/tests/integration/widened_max_age.rs +++ b/rs/moq-net/tests/integration/widened_max_age.rs @@ -338,3 +338,44 @@ async fn wider_newcomers_after_the_first_pull_nothing_upstream() { } assert_eq!(sent_groups(®istry), widened, "later newcomers pull nothing upstream"); } + +/// A lite-03/04 viewer that beats a relay's copy resolving starts at the live edge, though +/// the relay's lite-07 feed delivers the history the budget reaches after its answer: the +/// SUBSCRIBE names no start, which those drafts define as the latest group. +#[kio::test] +async fn a_legacy_viewer_beating_a_relay_copy_joins_at_the_live_edge() { + for legacy in ["moq-lite-03", "moq-lite-04"] { + let publisher = origin(1); + let relay = origin(2); + let viewer = origin(3); + let mut options = MockConnectOptions::new(VERSION.parse().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(relay.clone()); + let upstream = connect_mock(options).await; + let _downstream = peer(legacy.parse().unwrap(), &relay, &viewer).await; + let broadcast = publisher.create_broadcast("live").unwrap(); + let source = broadcast.create_track("video", timed()).unwrap(); + broadcast.announce(Default::default()).unwrap(); + for sequence in 0..GROUPS { + let mut group = source.create_group(moq_net::group::Info { sequence }).unwrap(); + group + .write_frame(Timestamp::from_millis(sequence * 1000).unwrap(), &b"x"[..]) + .unwrap(); + group.finish().unwrap(); + } + + let consumer = viewer.consume(); + let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + upstream.server_transport.hold_unis(); + let subscription = Subscription::default().with_delay(Delay::default().with_max(WIDE)); + let mut reader = kio::clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(subscription)) + .await + .unwrap() + .unwrap(); + // The groups follow the answer, within the settle. + kio::clock::sleep(Duration::from_millis(10)).await; + upstream.server_transport.release_unis(); + let got = kio::clock::timeout(TIMEOUT, received(&mut reader)).await.unwrap(); + assert_eq!(got, BTreeSet::from([GROUPS - 1]), "{legacy}"); + } +} From fbaa9f666197a3ed8e88f92a4122d4bbbc797764 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 22:53:59 -0800 Subject: [PATCH 19/25] style(net): satisfy clippy and biome Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 23 +++++++++++++---------- rs/moq-net/src/lite/publisher.rs | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index b19e0bd6c94..42530bdc7fa 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1631,16 +1631,19 @@ test.each([ [Version.DRAFT_04, false], [Version.DRAFT_05, false], [Version.DRAFT_05, true], -])("lite %s (received %s): a subscribe that beats its track resolving joins at its newest group", async (version, received) => { - const sub = await servedSubscription({ version, pending: true }); - try { - await sub.accept([0n, 3n, 7n], received); - expect(await sub.servedHeader()).toBe(7n); - expect(await sub.servedSequence()).toBeUndefined(); - } finally { - await sub.close(); - } -}); +])( + "lite %s (received %s): a subscribe that beats its track resolving joins at its newest group", + async (version, received) => { + const sub = await servedSubscription({ version, pending: true }); + try { + await sub.accept([0n, 3n, 7n], received); + expect(await sub.servedHeader()).toBe(7n); + expect(await sub.servedSequence()).toBeUndefined(); + } finally { + await sub.close(); + } + }, +); // Lite-01/02 retain the arrival-only floor even when the answer contains history. test.each([Version.DRAFT_01, Version.DRAFT_02])( diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 5b78da95fbf..dab266ab87b 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2963,7 +2963,7 @@ impl SubscribeServe { /// Serve the confirmed `track` from `latest`, the newest group as the join starts; see /// [`cursor_floor`]. - #[allow(clippy::too_many_arguments)] + #[allow(clippy::too_many_arguments, reason = "the parts of a confirmed join, unpacked from either state")] fn run( shared: &Shared, msg: lite::Subscribe<'static>, From 1172db137d5f64533285ed43800832724e3be3c6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 23:06:52 -0800 Subject: [PATCH 20/25] style(net): format the run helper's lint allowance Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index dab266ab87b..5270df4f3ab 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2963,7 +2963,10 @@ impl SubscribeServe { /// Serve the confirmed `track` from `latest`, the newest group as the join starts; see /// [`cursor_floor`]. - #[allow(clippy::too_many_arguments, reason = "the parts of a confirmed join, unpacked from either state")] + #[allow( + clippy::too_many_arguments, + reason = "the parts of a confirmed join, unpacked from either state" + )] fn run( shared: &Shared, msg: lite::Subscribe<'static>, From 7175eef9495cb19a5f5d588c7cc383479611de9d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 23:16:53 -0800 Subject: [PATCH 21/25] test(net): drop explicit derefs in the settle test Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/publisher.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 5270df4f3ab..64880ac0191 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -9661,7 +9661,7 @@ mod tests { } Answer::Cached => { let mut staged = staged.take().unwrap(); - write(&mut *staged, &[0, 3, 7]); + write(&mut staged, &[0, 3, 7]); staged.commit() } Answer::Settling => { @@ -9672,7 +9672,7 @@ mod tests { } Answer::Reported => { let mut staged = staged.take().unwrap(); - write(&mut *staged, &[3]); + write(&mut staged, &[3]); staged.report_largest(track::Position::group(9)); staged.commit() } From a11a5e24ba8d996334a07d30874b9e5f48214a43 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 23:25:14 -0800 Subject: [PATCH 22/25] chore(quest): finish relay-inflight-replay Co-Authored-By: Claude Opus 5.5 --- quest/m1/README.md | 1 - quest/m1/relay-inflight-replay.md | 32 ------------------------------- 2 files changed, 33 deletions(-) delete mode 100644 quest/m1/relay-inflight-replay.md diff --git a/quest/m1/README.md b/quest/m1/README.md index 029602583aa..deefd14e186 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -182,7 +182,6 @@ their end report, so an embedder's drain alert pages on real cuts. - [Rename moq-rtc to moq-webrtc](/quest/m1/webrtc-rename.md) - one published `moq-webrtc` crate holds the gateway, the ICE driver, and the data-channel transport - [Vendor and trim the WebRTC stack](/quest/m1/webrtc-vendor.md) - str0m, dimpl, `is`, and sctp-proto live in moq-webrtc with unused code removed, so moq-rtc can take rind - [MoQ on rind](/quest/m1/rind/README.md) - every MoQ crate takes its crypto from rind, a pure-Rust graviola fork, so no build compiles C crypto -- [Legacy relay joins start at the answer's live edge](/quest/m1/relay-inflight-replay.md) - a lite-03/04 join on a relay copy settles 50 ms past the upstream answer and starts at its newest group ## Related diff --git a/quest/m1/relay-inflight-replay.md b/quest/m1/relay-inflight-replay.md deleted file mode 100644 index 886d76be25a..00000000000 --- a/quest/m1/relay-inflight-replay.md +++ /dev/null @@ -1,32 +0,0 @@ -# [S] Legacy relay joins start at the answer's live edge - -## Goal - -A lite-03/04 SUBSCRIBE without a start, served by a Rust or JS relay from a -copy its upstream feeds, starts at the newest group the upstream's answer -brings, never at history the relay's cache or feed holds below it. - -## Plan - -Decided 2026-11-05 (maintainer, /quest-unblock), replacing the 2026-11-03 -request-time floor and the 2026-11-04 copyless-front decision where they -conflict: ✅ rethink after the fourth review round's design checkpoint. Not -chosen: continuing the shared immutable snapshot design with targeted fixes. - -- A pending join's start comes from the upstream answer's latest group, - never from the relay's cache. Drop the snapshot machinery this makes - unnecessary. -- An answer that carries no largest group waits about 50 ms past the answer - and starts from the newest group received by then. Lite-03/04's - SUBSCRIBE_OK carries none (Rust sends no `start_group`; JS echoes the - requested one), nor do lite-01..06 answers. Mock time in its tests. -- Legacy lite-03/04 may keep minor known bugs; the PR body lists them. - -Implemented: the publisher settles a lite-03/04 join that found no newest -group on arrival when a wire subscription feeds its track (a received copy, -or a front whose serving copy is one), then floors at the newest group known, -which includes a largest a lite-07 or moq-transport answer reported. Local -tracks and lite-05 take the newest group once the track resolves (lite-05's -first-frame latency allows no wait); lite-01/02 keep their arrival floor -(the 2026-11-04 parity decision still holds). Remaining: the restarted local -review rounds (`quest review --new`). From 5f55182c9b64507bf0fe4d4116085f144dbf3343 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 4 Nov 2026 23:40:54 -0800 Subject: [PATCH 23/25] fix(net): answer a settling legacy join at once The lite-03/04 SUBSCRIBE_OK names no start, so it goes out before the settle rather than after it; only the media waits. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 2 ++ js/net/src/lite/publisher.ts | 40 +++++++++++++------------ rs/moq-net/src/lite/publisher.rs | 50 +++++++++++++++++-------------- 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 42530bdc7fa..6f5dec72e80 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -1588,6 +1588,8 @@ test.each([ try { await sub.accept([0n, 3n], true); await clock.advance(Nano.zero); + // The SUBSCRIBE_OK names no start, so it never waits on the settle. + expect("ok" in (await decodeSubscribeResponse(sub.client.reader, version))).toBe(true); sub.serve(5n); await clock.advance(Nano.fromMilli(Milli(50))); sub.serve(4n); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 99814150258..6f5e549c290 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1042,25 +1042,6 @@ export class Publisher { // priority. const info = await untilAborted(track.info(), left.signal); const publisher = info.priority; - // Without a newest group on arrival, such as a SUBSCRIBE that beat its track - // resolving, the newest once it resolves stands in, as it would for one sent after. - // A lite-03/04 copy's newest group then is not what its source's answer brings, but - // an older one cached or none yet: it waits {@link SETTLE} past the answer and starts - // at the newest group known by then, including a largest the answer reported. - // Lite-01/02 keep the floor their arrival gave, as Rust does. - if (latest === undefined && msg.startGroup === undefined && !arrivalOnly(this.version)) { - const settles = - (this.version === Version.DRAFT_03 || this.version === Version.DRAFT_04) && hooks.received(track); - if (settles) await sleep(SETTLE, { signal: left.signal }); - const known = hooks.latestSequence(track); - const reported = settles ? hooks.peekLargest(track).largest?.group : undefined; - const newest = known === undefined ? reported : reported === undefined ? known : max(known, reported); - const resolved = cursorFloor(this.version, msg.startGroup, newest); - if (resolved !== floor) { - floor = resolved; - track.withRange({ start: { included: floor } }); - } - } // Older drafts carry no timestamps. let stamp: Stamp | undefined; let compress = false; @@ -1086,6 +1067,27 @@ export class Publisher { await encodeSubscribeResponse(stream.writer, { ok }, this.version); } + // Without a newest group on arrival, such as a SUBSCRIBE that beat its track + // resolving, the newest once it resolves stands in, as it would for one sent after. + // A lite-03/04 copy's newest group then is not what its source's answer brings, but + // an older one cached or none yet: it waits {@link SETTLE} past the answer and starts + // at the newest group known by then, including a largest the answer reported. + // Lite-01/02 keep the floor their arrival gave, as Rust does. After the SUBSCRIBE_OK, + // which names no start, so the answer never waits on the settle. + if (latest === undefined && msg.startGroup === undefined && !arrivalOnly(this.version)) { + const settles = + (this.version === Version.DRAFT_03 || this.version === Version.DRAFT_04) && hooks.received(track); + if (settles) await sleep(SETTLE, { signal: left.signal }); + const known = hooks.latestSequence(track); + const reported = settles ? hooks.peekLargest(track).largest?.group : undefined; + const newest = known === undefined ? reported : reported === undefined ? known : max(known, reported); + const resolved = cursorFloor(this.version, msg.startGroup, newest); + if (resolved !== floor) { + floor = resolved; + track.withRange({ start: { included: floor } }); + } + } + console.debug(`publish ok: broadcast=${msg.broadcast} track=${track.name}`); // Serve datagrams concurrently with groups whenever the transport carries them diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 64880ac0191..4cbac49438a 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2493,6 +2493,21 @@ impl Request for SubscribeServe { continue; } }; + crate::ietf::flate::egress(track.info(), shared.version.has_compression())?; + // Lite05+ accepts implicitly: no SUBSCRIBE_OK, the immutable properties + // live in TRACK_INFO, and the resolved range arrives as + // SUBSCRIBE_START/END from the run loop. Older drafts still acknowledge + // with SUBSCRIBE_OK here, at once: it names no start, so it never waits on + // the settle below. + if !shared.version.has_track_stream() { + let info = lite::SubscribeOk { + priority: msg.priority, + max_delay: Duration::ZERO, + start_group: None, + end_group: None, + }; + writer.buffer(&lite::SubscribeResponse::Ok(info))?; + } // A copy's newest group then is not what its source's answer brings: an older // one cached, or none yet. if latest.is_none() @@ -2515,9 +2530,13 @@ impl Request for SubscribeServe { Version::Lite01 | Version::Lite02 => latest, _ => latest.or_else(|| track.latest_sequence()), }; - *self = Self::run(shared, *msg, bounds, track, latest, update, legacy, ietf, writer)?; + *self = Self::run(shared, *msg, bounds, track, latest, update, legacy, ietf); } Self::Settle { due, .. } => { + // The SUBSCRIBE_OK goes out now, not once the settle ends. + if let Poll::Ready(Err(err)) = writer.poll_flush(waiter) { + return Poll::Ready(Err(err)); + } let ietf = ready!(shared.peer_setup.poll_ietf(waiter)); ready!(due.poll(waiter)); let Self::Settle { @@ -2539,7 +2558,7 @@ impl Request for SubscribeServe { }; // Past what the cache holds once the source reported a newer group. let latest = track.peek_largest().map(|largest| largest.group); - *self = Self::run(shared, *msg, bounds, *track, latest, update, legacy, ietf, writer)?; + *self = Self::run(shared, *msg, bounds, *track, latest, update, legacy, ietf); } Self::Run(run) => { // One datagram a step, beside the groups. @@ -2976,29 +2995,12 @@ impl SubscribeServe { update: Option, legacy: Option>, ietf: bool, - writer: &mut Writer, - ) -> Result { - crate::ietf::flate::egress(track.info(), shared.version.has_compression())?; - + ) -> Self { // `None` omits the per-frame timestamp prefix: an untimed track on // Lite07+, or any track before Lite05 (whose frames still carry // timestamps in the model, just not on this wire). let stamp = Stamp::new(shared.version, track.info().timescale); - // Lite05+ accepts implicitly: no SUBSCRIBE_OK, the immutable properties - // live in TRACK_INFO, and the resolved range arrives as - // SUBSCRIBE_START/END from the run loop. Older drafts still acknowledge - // with SUBSCRIBE_OK here. - if !shared.version.has_track_stream() { - let info = lite::SubscribeOk { - priority: msg.priority, - max_delay: Duration::ZERO, - start_group: None, - end_group: None, - }; - writer.buffer(&lite::SubscribeResponse::Ok(info))?; - } - // Track-level subscriber priority and group order. SUBSCRIBE_UPDATE // messages broadcast new values to the run loop (so future groups inherit // them) and the in-flight group machines (so they update via @@ -3035,7 +3037,7 @@ impl SubscribeServe { if let Some(update) = update { run.update(update); } - Ok(Self::Run(Box::new(run))) + Self::Run(Box::new(run)) } /// Serve `msg` from the name's datagram track, at the priority of its latest `update`. @@ -9631,8 +9633,9 @@ mod tests { }; let mut serve = SubscribeServe::start(&publisher.shared, msg, "video", broadcast.consume()).unwrap(); + let control = Log::default(); let mut writer = Writer::new( - crate::transport::boxed::SendStream::new(SinkSend::new(Log::default())), + crate::transport::boxed::SendStream::new(SinkSend::new(control.clone())), version, ); // Serve for `wait`: a live subscription never ends on its own. @@ -9667,6 +9670,9 @@ mod tests { Answer::Settling => { let mut track = staged.take().unwrap().commit(); drive(Duration::from_millis(10)).await; + // The SUBSCRIBE_OK names no start, so it never waits on the settle. + let answered = control.trail().iter().any(|(_, sent)| matches!(sent, Sent::Write(_))); + assert_eq!(answered, !version.has_track_stream(), "{version}: answered at once"); write(&mut track, &[0, 3, 7]); track } From e4136ec7f42e582c41ae47180c9e95742915ea0a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 09:26:50 -0800 Subject: [PATCH 24/25] fix(net): drop the legacy join settle; take the newest group at resolution A lite-03/04 join that found no group on arrival now takes the newest group known when its subscription resolves, including a largest the upstream's answer reported, with no 50 ms wait and no received-copy check. A newer group the answer brings later is not waited for. Co-Authored-By: Claude Opus 5.5 --- js/net/src/internal.ts | 10 - js/net/src/lite/publisher.test.ts | 89 ++---- js/net/src/lite/publisher.ts | 50 +--- js/net/src/resume.ts | 5 - js/net/src/track.ts | 8 - rs/moq-net/src/lite/publisher.rs | 281 ++++++------------ rs/moq-net/src/model/resume.rs | 26 -- rs/moq-net/src/model/track.rs | 20 -- .../tests/integration/widened_max_age.rs | 16 +- 9 files changed, 133 insertions(+), 372 deletions(-) diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index 3ebf193462b..b038b8445c0 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -357,8 +357,6 @@ export interface ResumeSource { forgetPastBudget(delivered: Delivered): void; /** The newest sequence any copy has seen. */ latest(): bigint | undefined; - /** Whether a wire subscription feeds the serving copy. */ - received(): boolean; /** The serving copy's declared start, raised to its cursor floor, or `"pending"` until resolved. */ start(): bigint | "pending" | undefined; /** The logical track's declared exclusive end, as the first copy to know it declared it. */ @@ -543,11 +541,6 @@ export const hooks: { tailGrace: (producer: Producer) => Milli; /** Snapshot the latest group sequence for wire positioning; assigned by `track.ts`. */ latestSequence: (subscriber: Subscriber) => bigint | undefined; - /** - * Whether a wire subscription feeds the subscriber's track, or a front's serving copy; - * assigned by `track.ts`. - */ - received: (subscriber: Subscriber) => boolean; /** * The source's declared start, raised to the subscriber's cursor floor, or `"pending"` while * the upstream START that resolves it has yet to arrive; assigned by `track.ts`. @@ -834,9 +827,6 @@ export const hooks: { latestSequence: () => { throw new Error("track.ts not loaded"); }, - received: () => { - throw new Error("track.ts not loaded"); - }, sourceStart: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 60330ebb381..7ed16f5b853 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -796,13 +796,13 @@ async function servedSubscription( serving, parked: gate.parked, release: gate.release, - // Resolve a pending track with the publisher's request for it, answering with `held`, - // as a copy a wire subscription feeds when `received`. - async accept(held: bigint[] = [], received = false) { + // Resolve a pending track with the publisher's request for it, answering with `held` + // and, when `reported`, an upstream's report of a newer largest group. + async accept(held: bigint[] = [], reported?: bigint) { const pulled = await request; if (!pulled) throw new Error("the publisher never requested the track"); const staged = hooks.stageRequest(pulled, info); - if (received) hooks.receiveTrack(staged.producer); + if (reported !== undefined) hooks.reportedGroup(staged.producer, reported); for (const sequence of held) { const group = staged.producer.createGroup(sequence); for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(Number(sequence)))); @@ -1472,75 +1472,26 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", }); // A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching -// upstream. What a lite-03/04 copy holds then is not the live edge, so the join waits 50 ms -// past the answer and starts at the newest group known by then: older ones, cached or -// arriving after, are history. -// Routed, the join reads the front's serving copy, which the wire feeds. +// upstream. Groups the track holds once it does are older than the join, so an absent Group +// Start is still the latest group known then, including one the upstream reported. test.each([ - [Version.DRAFT_03, false], - [Version.DRAFT_04, false], - [Version.DRAFT_04, true], + [Version.DRAFT_03, undefined], + [Version.DRAFT_04, undefined], + [Version.DRAFT_04, 9n], + [Version.DRAFT_05, undefined], ])( - "lite %s (routed %s): a subscribe that beats a copy resolving starts at the newest group once settled", - async (version, routed) => { - const clock = new Manual(); - const installed = clock.install(); - const sub = await servedSubscription({ version, pending: true, routed }); - try { - await sub.accept([0n, 3n], true); - await clock.advance(Nano.zero); - // The SUBSCRIBE_OK names no start, so it never waits on the settle. - expect("ok" in (await decodeSubscribeResponse(sub.client.reader, version))).toBe(true); - sub.serve(5n); - await clock.advance(Nano.fromMilli(Milli(50))); - sub.serve(4n); - sub.serve(9n); - // Headers only: frames before lite-05 carry no timestamp for `servedGroup` to read. - expect(await sub.servedHeader()).toBe(5n); - expect(await sub.servedHeader()).toBe(9n); - } finally { - await sub.close(); - installed.close(); - } - }, -); - -// Nothing known by the settle leaves no history to skip: every group that follows is served. -test.each([Version.DRAFT_03, Version.DRAFT_04])( - "lite %s: a subscribe that beats an empty copy resolving serves every later group", - async (version) => { - const clock = new Manual(); - const installed = clock.install(); - const sub = await servedSubscription({ version, pending: true }); - try { - await sub.accept([], true); - await clock.advance(Nano.zero); - await clock.advance(Nano.fromMilli(Milli(50))); - sub.serve(0n); - sub.serve(1n); - expect(await sub.servedHeader()).toBe(0n); - expect(await sub.servedHeader()).toBe(1n); - } finally { - await sub.close(); - installed.close(); - } - }, -); - -// A local track's answer is its own newest group, as is a lite-05 copy's, whose first-frame -// latency allows no wait: the join starts there at once. -test.each([ - [Version.DRAFT_03, false], - [Version.DRAFT_04, false], - [Version.DRAFT_05, false], - [Version.DRAFT_05, true], -])( - "lite %s (received %s): a subscribe that beats its track resolving joins at its newest group", - async (version, received) => { + "lite %s (reported %s): a subscribe that beats its track resolving joins at its newest group", + async (version, reported) => { const sub = await servedSubscription({ version, pending: true }); try { - await sub.accept([0n, 3n, 7n], received); - expect(await sub.servedHeader()).toBe(7n); + await sub.accept([0n, 3n, 7n], reported); + if (reported !== undefined) { + // The SUBSCRIBE_OK follows the floor, so these arrive after the join took it. + expect("ok" in (await decodeSubscribeResponse(sub.client.reader, version))).toBe(true); + sub.serve(8n); + sub.serve(9n); + } + expect(await sub.servedHeader()).toBe(reported ?? 7n); expect(await sub.servedSequence()).toBeUndefined(); } finally { await sub.close(); diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 101e2595cc7..d8ee6fc3171 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -474,19 +474,6 @@ function carriesMaxDelay(version: Version): boolean { return version !== Version.DRAFT_01 && version !== Version.DRAFT_02; } -/** - * How long a lite-03/04 join on a received copy that held no group on arrival waits past the - * answer before it takes the newest group as the live edge. Lite-01..06 answers carry no - * largest group (lite-03/04's SUBSCRIBE_OK names only the requested start), so the newest - * group to arrive soon after the answer stands in, and a later one older than it is skipped. - */ -const SETTLE = Nano.fromMilli(Milli(50)); - -/** Whether a join on this version keeps the floor its arrival gave, as lite-01/02 do in Rust. */ -function arrivalOnly(version: Version): boolean { - return version === Version.DRAFT_01 || version === Version.DRAFT_02; -} - /** * The floor of a subscription's read cursor for the wire serving it. * @@ -1052,6 +1039,22 @@ export class Publisher { throw err; } const publisher = info.priority; + // A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a + // copy still fetching upstream, or on lite-05 one sent alongside its TRACK, when no + // group is known yet: the newest known once it resolves stands in, including a largest + // the upstream's answer reported, as it would for one sent after. A newer group the + // answer brings later is not waited for. Lite-01/02 keep their arrival floor, as Rust does. + if (latest === undefined && this.version !== Version.DRAFT_01 && this.version !== Version.DRAFT_02) { + const cached = hooks.latestSequence(track); + const reported = hooks.peekLargest(track).largest?.group; + const newest = + cached === undefined ? reported : reported === undefined ? cached : max(cached, reported); + const resolved = cursorFloor(this.version, msg.startGroup, newest); + if (resolved !== floor) { + floor = resolved; + track.withRange({ start: { included: floor } }); + } + } // Older drafts carry no timestamps. let stamp: Stamp | undefined; let compress = false; @@ -1077,27 +1080,6 @@ export class Publisher { await encodeSubscribeResponse(stream.writer, { ok }, this.version); } - // Without a newest group on arrival, such as a SUBSCRIBE that beat its track - // resolving, the newest once it resolves stands in, as it would for one sent after. - // A lite-03/04 copy's newest group then is not what its source's answer brings, but - // an older one cached or none yet: it waits {@link SETTLE} past the answer and starts - // at the newest group known by then, including a largest the answer reported. - // Lite-01/02 keep the floor their arrival gave, as Rust does. After the SUBSCRIBE_OK, - // which names no start, so the answer never waits on the settle. - if (latest === undefined && msg.startGroup === undefined && !arrivalOnly(this.version)) { - const settles = - (this.version === Version.DRAFT_03 || this.version === Version.DRAFT_04) && hooks.received(track); - if (settles) await sleep(SETTLE, { signal: left.signal }); - const known = hooks.latestSequence(track); - const reported = settles ? hooks.peekLargest(track).largest?.group : undefined; - const newest = known === undefined ? reported : reported === undefined ? known : max(known, reported); - const resolved = cursorFloor(this.version, msg.startGroup, newest); - if (resolved !== floor) { - floor = resolved; - track.withRange({ start: { included: floor } }); - } - } - console.debug(`publish ok: broadcast=${msg.broadcast} track=${track.name}`); // Serve datagrams concurrently with groups whenever the transport carries them diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 1e863a44b01..c4868c61f1e 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -1094,11 +1094,6 @@ class Reader implements ResumeSource { if (newest) hooks.forgetPastBudget(newest.sub, delivered); } - received(): boolean { - const serving = this.#serving(); - return serving !== undefined && hooks.received(serving.sub); - } - latest(): bigint | undefined { let latest: bigint | undefined; for (const copy of this.#copies) { diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 12a7b821189..8c9f549eeda 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -787,8 +787,6 @@ let owedRuns: (producer: Producer, start: bigint, end: bigint) => Iterable { sequence: bigint; folds?: number } | undefined; let fillEdge: (producer: Producer) => bigint | undefined; let sourceStart: (producer: Producer) => bigint | "pending" | undefined; -// Whether a wire subscription feeds the producer; assigned in its static block. -let fed: (producer: Producer) => boolean; // The lowest group a reader naming `named` asks a fill for; see `Producer.#fillFloor`. let fillFloor: (producer: Producer, named: bigint | undefined) => bigint | undefined; // The producer's checked group starts, sorted by sequence; assigned in its static block. @@ -996,7 +994,6 @@ export class Producer { !producer.#subscribed && producer.#state.info.peek()?.ietf !== undefined ? producer.#state.largest.peek()?.group : undefined; - fed = (producer) => producer.#subscribed; hooks.receiveTrack = (producer, starts = {}) => { producer.#subscribed = true; producer.#starts = starts; @@ -2849,11 +2846,6 @@ export class Subscriber { }; hooks.latestSequence = (subscriber) => subscriber.#resume ? subscriber.#resume.latest() : subscriber.#state.latest; - hooks.received = (subscriber) => { - if (subscriber.#resume) return subscriber.#resume.received(); - const producer = subscriber.#state.producer; - return producer !== undefined && fed(producer); - }; hooks.bufferedLargest = (subscriber) => subscriber.#bufferedLargest(); hooks.objectLargest = (subscriber) => { if (subscriber.#resume) return subscriber.#resume.objectLargest(); diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index bb7d7a4412a..2d75c1add7b 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -123,12 +123,6 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option version.carries_max_delay().then_some(requested) } -/// How long a lite-03/04 join on a received copy that held no group on arrival waits past the -/// answer before it takes the newest group as the live edge. Lite01-06 answers carry no -/// largest group (lite-03/04's SUBSCRIBE_OK names only the requested start), so the newest -/// group to arrive soon after the answer stands in, and a later one older than it is skipped. -const SETTLE: Duration = Duration::from_millis(50); - /// The floor of a subscription's read cursor for the wire serving it. /// /// On lite-06 there is nothing to do: `Consumer::subscribe` resolves the cursor from the @@ -142,13 +136,10 @@ const SETTLE: Duration = Duration::from_millis(50); /// must not read as a request to replay the whole cache on join. /// /// `latest` is the newest group when the SUBSCRIBE arrived: anything written since is newer, -/// so it is delivered. A SUBSCRIBE that found none, such as one that beat its track resolving, -/// takes the newest group once the track resolves. On a lite-03/04 copy received from a -/// remote publisher that group is not what the source's answer brings, but an older one -/// cached or none yet: the join waits [`SETTLE`] past the answer and starts at the newest group -/// known by then, which includes the largest a source's answer reported (lite-07's -/// SUBSCRIBE_START, moq-transport's SUBSCRIBE_OK). Lite01/02 keep a zero floor when no group -/// was known on arrival. +/// so it is delivered. A SUBSCRIBE that found none, such as one a relay serves from a copy +/// still fetching upstream, or on lite-05 one sent alongside its TRACK, takes the newest group +/// known once the track resolves, including a largest the upstream's answer reported. A newer +/// group the answer brings later is not waited for. Lite01/02 keep a zero floor. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -2288,17 +2279,6 @@ enum SubscribeServe { update: Option, legacy: Option>, }, - /// A lite-03/04 join on a received copy, waiting [`SETTLE`] past the answer for the newest - /// group it brings; see [`cursor_floor`]. - Settle { - /// Boxed: the request is far larger than the other states' handles. - msg: Box>, - bounds: Bounds, - track: Box, - due: crate::time::Deadline, - update: Option, - legacy: Option>, - }, /// Streaming groups. Boxed: by far the largest state, and the enum is moved on every /// transition. Run(Box), @@ -2378,9 +2358,7 @@ impl Request for SubscribeServe { fn update(&mut self, update: LiveUpdate) { match self { - Self::Query { update: pending, .. } - | Self::Confirm { update: pending, .. } - | Self::Settle { update: pending, .. } => *pending = Some(update), + Self::Query { update: pending, .. } | Self::Confirm { update: pending, .. } => *pending = Some(update), Self::Run(run) => run.update(update), Self::Datagrams { consumer, @@ -2490,7 +2468,7 @@ impl Request for SubscribeServe { else { unreachable!() }; - let track = match track { + let mut track = match track { Ok(track) => track, Err(err) => { *self = Self::datagrams(Legacy::datagrams(legacy, err)?, &msg, update); @@ -2498,11 +2476,16 @@ impl Request for SubscribeServe { } }; crate::ietf::flate::egress(track.info(), shared.version.has_compression())?; + + // `None` omits the per-frame timestamp prefix: an untimed track on + // Lite07+, or any track before Lite05 (whose frames still carry + // timestamps in the model, just not on this wire). + let stamp = Stamp::new(shared.version, track.info().timescale); + // Lite05+ accepts implicitly: no SUBSCRIBE_OK, the immutable properties // live in TRACK_INFO, and the resolved range arrives as // SUBSCRIBE_START/END from the run loop. Older drafts still acknowledge - // with SUBSCRIBE_OK here, at once: it names no start, so it never waits on - // the settle below. + // with SUBSCRIBE_OK here. if !shared.version.has_track_stream() { let info = lite::SubscribeOk { priority: msg.priority, @@ -2512,57 +2495,52 @@ impl Request for SubscribeServe { }; writer.buffer(&lite::SubscribeResponse::Ok(info))?; } - // A copy's newest group then is not what its source's answer brings: an older - // one cached, or none yet. - if latest.is_none() - && bounds.groups().start().is_none() - && matches!(shared.version, Version::Lite03 | Version::Lite04) - && track.is_received() - { - *self = Self::Settle { - msg, - bounds, - track: Box::new(track), - due: crate::time::Deadline::after(&shared.runtime, SETTLE), - update, - legacy, - }; - continue; - } - // A local track's newest group once it resolves is its answer's. + + // Track-level subscriber priority and group order. SUBSCRIBE_UPDATE + // messages broadcast new values to the run loop (so future groups inherit + // them) and the in-flight group machines (so they update via + // PriorityHandle::steer). + let steer = Steer { + priority: msg.priority, + order: msg.order, + }; + let steer_tx = kio::Producer::new(steer); + + let session = shared.session.clone(); + let publisher = track.info().priority; + let timescale = track.info().timescale; + let group = session.send_group(crate::priority::group(msg.priority, publisher)); + let sub = Subscription { + session, + id: msg.id, + track_name: Arc::from(track.name()), + priority: shared.priority.clone(), + publisher, + group, + steer: steer_tx.consume(), + steer_seen: steer, + version: shared.version, + stamp, + runtime: shared.runtime.clone(), + outcomes: Default::default(), + ietf, + }; + let latest = match shared.version { Version::Lite01 | Version::Lite02 => latest, - _ => latest.or_else(|| track.latest_sequence()), + _ => latest.or_else(|| { + track + .latest_sequence() + .max(track.peek_largest().map(|largest| largest.group)) + }), }; - *self = Self::run(shared, *msg, bounds, track, latest, update, legacy, ietf); - } - Self::Settle { due, .. } => { - // The SUBSCRIBE_OK goes out now, not once the settle ends. - if let Poll::Ready(Err(err)) = writer.poll_flush(waiter) { - return Poll::Ready(Err(err)); + let floor = cursor_floor(shared.version, bounds.groups().start(), latest); + let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); + run.datagrams = LegacyDatagrams::join(legacy, msg.id, msg.priority, timescale); + if let Some(update) = update { + run.update(update); } - let ietf = ready!(shared.peer_setup.poll_ietf(waiter)); - ready!(due.poll(waiter)); - let Self::Settle { - msg, - bounds, - mut track, - update, - legacy, - .. - } = std::mem::replace( - self, - Self::Drain { - children: Default::default(), - _track: None, - }, - ) - else { - unreachable!() - }; - // Past what the cache holds once the source reported a newer group. - let latest = track.peek_largest().map(|largest| largest.group); - *self = Self::run(shared, *msg, bounds, *track, latest, update, legacy, ietf); + *self = Self::Run(Box::new(run)); } Self::Run(run) => { // One datagram a step, beside the groups. @@ -2984,66 +2962,6 @@ impl SubscribeServe { } } - /// Serve the confirmed `track` from `latest`, the newest group as the join starts; see - /// [`cursor_floor`]. - #[allow( - clippy::too_many_arguments, - reason = "the parts of a confirmed join, unpacked from either state" - )] - fn run( - shared: &Shared, - msg: lite::Subscribe<'static>, - bounds: Bounds, - track: track::Subscriber, - latest: Option, - update: Option, - legacy: Option>, - ietf: bool, - ) -> Self { - // `None` omits the per-frame timestamp prefix: an untimed track on - // Lite07+, or any track before Lite05 (whose frames still carry - // timestamps in the model, just not on this wire). - let stamp = Stamp::new(shared.version, track.info().timescale); - - // Track-level subscriber priority and group order. SUBSCRIBE_UPDATE - // messages broadcast new values to the run loop (so future groups inherit - // them) and the in-flight group machines (so they update via - // PriorityHandle::steer). - let steer = Steer { - priority: msg.priority, - order: msg.order, - }; - let steer_tx = kio::Producer::new(steer); - - let session = shared.session.clone(); - let publisher = track.info().priority; - let timescale = track.info().timescale; - let group = session.send_group(crate::priority::group(msg.priority, publisher)); - let sub = Subscription { - session, - id: msg.id, - track_name: Arc::from(track.name()), - priority: shared.priority.clone(), - publisher, - group, - steer: steer_tx.consume(), - steer_seen: steer, - version: shared.version, - stamp, - runtime: shared.runtime.clone(), - outcomes: Default::default(), - ietf, - }; - - let floor = cursor_floor(shared.version, bounds.groups().start(), latest); - let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); - run.datagrams = LegacyDatagrams::join(legacy, msg.id, msg.priority, timescale); - if let Some(update) = update { - run.update(update); - } - Self::Run(Box::new(run)) - } - /// Serve `msg` from the name's datagram track, at the priority of its latest `update`. fn datagrams(mut consumer: datagram::Consumer, msg: &lite::Subscribe<'static>, update: Option) -> Self { let priority = update.map_or(msg.priority, |update| update.priority); @@ -9555,27 +9473,23 @@ mod tests { } } - /// A lite-03/04 join that beats a received copy resolving waits [`SETTLE`] past the - /// answer and starts at the newest group known by then: one the answer reported but has - /// yet to deliver, or the newest delivered. A later group older than it is skipped, while - /// groups first arriving past the settle are all delivered. A local track's join, and any - /// on lite-05, starts at the newest group as it resolves. Named starts, lite-06, and joins + /// A pre-06 SUBSCRIBE that beats its track resolving starts at the newest group known once + /// it resolves: one cached, or one the answer reported but has yet to deliver. An answer + /// with nothing known serves every later group. Named starts, lite-01, lite-06, and joins /// whose newest group was known on arrival keep their floors. #[kio::test] - async fn a_pending_subscribe_joins_the_newest_group_after_the_answer() { + async fn a_pending_subscribe_joins_the_newest_group_once_resolved() { use crate::lite::test_transport::{Log, Sent}; enum Answer { - /// Groups 0, 3, and 7 written before the SUBSCRIBE arrives. + /// Groups 0 and 3 written before the SUBSCRIBE arrives, 7 after. Resolved, /// The answer holds groups 0, 3, and 7. Cached, - /// Groups 0, 3, and 7 arrive within the settle. - Settling, /// The answer holds group 3 and reports group 9 as its largest. Reported, - /// Groups 0, 3, and 7 arrive only past the settle. - Late, + /// The answer holds nothing; groups 0, 3, and 7 follow it. + Empty, } fn write(track: &mut track::Producer, sequences: &[u64]) { @@ -9586,15 +9500,15 @@ mod tests { } } - for version in [Version::Lite03, Version::Lite04, Version::Lite05, Version::Lite06] { - for answer in [ - Answer::Resolved, - Answer::Cached, - Answer::Settling, - Answer::Reported, - Answer::Late, - ] { - for (start, received) in [(None, false), (None, true), (Some(0), true)] { + for version in [ + Version::Lite01, + Version::Lite03, + Version::Lite04, + Version::Lite05, + Version::Lite06, + ] { + for answer in [Answer::Resolved, Answer::Cached, Answer::Reported, Answer::Empty] { + for start in [None, Some(0)] { let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); let broadcast = origin.create_broadcast("room").unwrap(); let mut dynamic = broadcast.requests(); @@ -9604,9 +9518,7 @@ mod tests { assert!(held.poll_ok(&kio::Waiter::noop()).is_pending()); let request = dynamic.requested_track().await.unwrap(); let mut staged = Some(request.stage(track::Info::timed())); - if received { - staged.as_ref().unwrap().received(); - } + staged.as_ref().unwrap().received(); let mut track = None; if let Answer::Resolved = answer { let mut resolved = staged.take().unwrap().commit(); @@ -9645,15 +9557,14 @@ mod tests { }; let mut serve = SubscribeServe::start(&publisher.shared, msg, "video", broadcast.consume()).unwrap(); - let control = Log::default(); let mut writer = Writer::new( - crate::transport::boxed::SendStream::new(SinkSend::new(control.clone())), + crate::transport::boxed::SendStream::new(SinkSend::new(Log::default())), version, ); - // Serve for `wait`: a live subscription never ends on its own. - let mut drive = async |wait: Duration| { + // Serve for a moment: a live subscription never ends on its own. + let mut drive = async || { let outcome = kio::clock::timeout( - wait, + Duration::from_millis(1), kio::wait(|waiter| { loop { match ready!(serve.poll(&publisher.shared, &mut writer, waiter))? { @@ -9666,47 +9577,31 @@ mod tests { .await; assert!(outcome.is_err(), "the live subscription ended"); }; - drive(Duration::from_millis(1)).await; + drive().await; let mut track = match answer { - Answer::Resolved => { - let mut track = track.take().unwrap(); - write(&mut track, &[7]); - track - } + Answer::Resolved => track.take().unwrap(), Answer::Cached => { let mut staged = staged.take().unwrap(); write(&mut staged, &[0, 3, 7]); staged.commit() } - Answer::Settling => { - let mut track = staged.take().unwrap().commit(); - drive(Duration::from_millis(10)).await; - // The SUBSCRIBE_OK names no start, so it never waits on the settle. - let answered = control.trail().iter().any(|(_, sent)| matches!(sent, Sent::Write(_))); - assert_eq!(answered, !version.has_track_stream(), "{version}: answered at once"); - write(&mut track, &[0, 3, 7]); - track - } Answer::Reported => { let mut staged = staged.take().unwrap(); write(&mut staged, &[3]); staged.report_largest(track::Position::group(9)); staged.commit() } - Answer::Late => { - let track = staged.take().unwrap().commit(); - drive(SETTLE * 2).await; - track - } + Answer::Empty => staged.take().unwrap().commit(), }; - drive(SETTLE * 2).await; + drive().await; match answer { - Answer::Late => write(&mut track, &[0, 3, 7]), + Answer::Resolved => write(&mut track, &[7]), Answer::Reported => write(&mut track, &[7, 9]), - _ => {} + Answer::Empty => write(&mut track, &[0, 3, 7]), + Answer::Cached => {} } - drive(Duration::from_millis(1)).await; + drive().await; let mut streams = std::collections::BTreeMap::<_, Vec>::new(); for (id, sent) in log.trail() { @@ -9729,20 +9624,18 @@ mod tests { Answer::Reported => &[3, 7, 9], _ => &[0, 3, 7], }; - let settles = received && matches!(version, Version::Lite03 | Version::Lite04); let expected: &[u64] = if start.is_some() || version.resolves_start() { all } else { match answer { Answer::Resolved => &[3, 7], + _ if version == Version::Lite01 => all, Answer::Cached => &[7], - Answer::Settling | Answer::Reported if !settles => all, - Answer::Settling => &[7], Answer::Reported => &[9], - Answer::Late => &[0, 3, 7], + Answer::Empty => &[0, 3, 7], } }; - assert_eq!(sequences, expected, "{version}, start={start:?}, received={received}"); + assert_eq!(sequences, expected, "{version}, start={start:?}"); track.abort(Error::Cancel).unwrap(); } } diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index b3913fb3608..fc386ece1d9 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -929,14 +929,6 @@ impl Subscriber { self.reader().latest() } - /// Whether the copy serving, or parked, was received from a remote publisher. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(super) fn is_received(&self) -> bool { - let reader = self.reader(); - let copy = reader.route.serving().or_else(|| reader.route.parked()); - copy.is_some_and(|copy| copy.is_received()) - } - /// Poll for a largest location within `bounds` newer than `seen`; see [`Consumer::poll_largest`]. pub(super) fn poll_largest( &mut self, @@ -2529,24 +2521,6 @@ mod test { .accept(crate::track::Info::timed()) } - /// A front's reader is fed remotely when its serving copy is, or its parked one. - #[test] - fn a_front_reader_is_received_through_its_copy() { - let routes = Producer::new(); - let logical = logical(&routes); - let reader = subscribe(&logical, Duration::from_secs(1)); - assert!(!reader.is_received(), "no copy serves yet"); - let local = copy(); - routes.serve(local.consume()); - assert!(!reader.is_received(), "a local copy"); - let remote = copy(); - remote.received(); - routes.serve(remote.consume()); - assert!(reader.is_received(), "a received copy"); - routes.park(); - assert!(reader.is_received(), "a parked received copy"); - } - fn subscribe(logical: &track::Producer, max_delay: Duration) -> track::Subscriber { let subscription = Subscription::default().with_delay(Delay::default().with_max(max_delay)); logical diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c6c09855226..782f4d194d4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5255,16 +5255,6 @@ impl Consumer { self.with_state(|state| state?.read().max_sequence) } - /// Whether a remote publisher feeds this track: a copy received from one, or a front's - /// track it serves through one. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn is_received(&self) -> bool { - if let Some(serving) = self.serving() { - return serving.is_received(); - } - self.with_state(|state| state.is_some_and(|state| state.read().received)) - } - /// The first group the declared end excludes whole, or `None` while the track is open ended. #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn final_sequence(&self) -> Option { @@ -8790,16 +8780,6 @@ impl Subscriber { } } - /// Whether a remote publisher feeds this track: a copy received from one, or a front's - /// logical track served through one. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn is_received(&self) -> bool { - match &self.inner { - Inner::Plain(cursor) => cursor.state.read().received, - Inner::Resume(resume, _) => resume.is_received(), - } - } - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] pub(crate) fn latest_sequence(&self) -> Option { match &self.inner { diff --git a/rs/moq-net/tests/integration/widened_max_age.rs b/rs/moq-net/tests/integration/widened_max_age.rs index 914d98cd18f..2e4d9711eca 100644 --- a/rs/moq-net/tests/integration/widened_max_age.rs +++ b/rs/moq-net/tests/integration/widened_max_age.rs @@ -351,9 +351,9 @@ async fn wider_newcomers_after_the_first_pull_nothing_upstream() { assert_eq!(sent_groups(®istry), widened, "later newcomers pull nothing upstream"); } -/// A lite-03/04 viewer that beats a relay's copy resolving starts at the live edge, though -/// the relay's lite-07 feed delivers the history the budget reaches after its answer: the -/// SUBSCRIBE names no start, which those drafts define as the latest group. +/// A lite-03/04 viewer that beats a relay's copy resolving starts at the largest group the +/// relay's lite-07 feed reports, though the feed delivers the history the budget reaches after +/// its answer: the SUBSCRIBE names no start, which those drafts define as the latest group. #[kio::test] async fn a_legacy_viewer_beating_a_relay_copy_joins_at_the_live_edge() { for legacy in ["moq-lite-03", "moq-lite-04"] { @@ -377,15 +377,19 @@ async fn a_legacy_viewer_beating_a_relay_copy_joins_at_the_live_edge() { } let consumer = viewer.consume(); - let remote = consumer.follow("live", None).unwrap().available().await.unwrap(); + let remote = consumer + .follow("live", None) + .unwrap() + .available() + .await + .unwrap() + .expect("broadcast resolves"); upstream.server_transport.hold_unis(); let subscription = Subscription::default().with_delay(Delay::default().with_max(WIDE)); let mut reader = kio::clock::timeout(TIMEOUT, remote.track("video").unwrap().subscribe(subscription)) .await .unwrap() .unwrap(); - // The groups follow the answer, within the settle. - kio::clock::sleep(Duration::from_millis(10)).await; upstream.server_transport.release_unis(); let got = kio::clock::timeout(TIMEOUT, received(&mut reader)).await.unwrap(); assert_eq!(got, BTreeSet::from([GROUPS - 1]), "{legacy}"); From 3f0a3cc7ec124abb4216bade5d947b12427281fd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 5 Nov 2026 10:24:48 -0800 Subject: [PATCH 25/25] fix(net): a legacy join takes a reported largest even on a warm copy The resolution-time re-read ran only when the SUBSCRIBE found no group on arrival, so a copy already holding a group joined there while a cold one joined at the largest its upstream reported. Take the newest of the arrival group, the cached group at resolution, and the reported largest in both languages, so the start no longer depends on how warm the copy is. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/publisher.test.ts | 45 ++++++++++++++++++++++++++----- js/net/src/lite/publisher.ts | 18 ++++++------- rs/moq-net/src/lite/publisher.rs | 40 +++++++++++++++++---------- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index 7ed16f5b853..f2aa0d241e9 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -714,6 +714,8 @@ async function servedSubscription( routed?: boolean; // Groups the track holds before the SUBSCRIBE arrives, stamped by sequence. prefill?: bigint[]; + // A newer largest group the track's source reported before the SUBSCRIBE arrives. + reported?: bigint; // Leave the track unresolved until `accept`, as an on-demand track the SUBSCRIBE beats. pending?: boolean; } = {}, @@ -749,6 +751,10 @@ async function servedSubscription( for (const frame of frames) group.writeFrame(textFrame(frame, Timestamp.fromMillis(Number(sequence)))); group.finish(); } + if (options.reported !== undefined) { + if (!track) throw new Error("a pending track has no source to report"); + hooks.reportedGroup(track, options.reported); + } // Pulled before the SUBSCRIBE arrives: a broadcast nobody pulls refuses an unknown track. const request = options.pending ? wireOf(broadcast).requested() : undefined; @@ -1474,15 +1480,18 @@ test("lite draft-05: an update dropping the floor re-pins to the latest group", // A SUBSCRIBE can beat its track resolving, as one a relay serves from a copy still fetching // upstream. Groups the track holds once it does are older than the join, so an absent Group // Start is still the latest group known then, including one the upstream reported. +// Routed, the join reads the front's serving copy. test.each([ - [Version.DRAFT_03, undefined], - [Version.DRAFT_04, undefined], - [Version.DRAFT_04, 9n], - [Version.DRAFT_05, undefined], + [Version.DRAFT_03, undefined, false], + [Version.DRAFT_04, undefined, false], + [Version.DRAFT_04, 9n, false], + [Version.DRAFT_04, 9n, true], + [Version.DRAFT_05, undefined, false], + [Version.DRAFT_05, undefined, true], ])( - "lite %s (reported %s): a subscribe that beats its track resolving joins at its newest group", - async (version, reported) => { - const sub = await servedSubscription({ version, pending: true }); + "lite %s (reported %s, routed %s): a subscribe that beats its track resolving joins at its newest group", + async (version, reported, routed) => { + const sub = await servedSubscription({ version, pending: true, routed }); try { await sub.accept([0n, 3n, 7n], reported); if (reported !== undefined) { @@ -1499,6 +1508,28 @@ test.each([ }, ); +// A track that already holds groups when the SUBSCRIBE arrives still joins at a newer largest +// its source reported, as a cold one would. +test.each([Version.DRAFT_04, Version.DRAFT_05])( + "lite %s: a join on a track already holding groups starts at a reported newer group", + async (version) => { + const sub = await servedSubscription({ version, prefill: [0n, 3n], reported: 9n }); + try { + if (version === Version.DRAFT_04) { + expect("ok" in (await decodeSubscribeResponse(sub.client.reader, version))).toBe(true); + } else { + await flush(); + } + sub.serve(7n); + sub.serve(9n); + expect(await sub.servedHeader()).toBe(9n); + expect(await sub.servedSequence()).toBeUndefined(); + } finally { + await sub.close(); + } + }, +); + // Lite-01/02 retain the arrival-only floor even when the answer contains history. test.each([Version.DRAFT_01, Version.DRAFT_02])( "lite %s: a pending join retains its arrival-only floor", diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index d8ee6fc3171..8210e0b0115 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -1040,15 +1040,15 @@ export class Publisher { } const publisher = info.priority; // A SUBSCRIBE can arrive before its track resolves, such as one a relay serves from a - // copy still fetching upstream, or on lite-05 one sent alongside its TRACK, when no - // group is known yet: the newest known once it resolves stands in, including a largest - // the upstream's answer reported, as it would for one sent after. A newer group the - // answer brings later is not waited for. Lite-01/02 keep their arrival floor, as Rust does. - if (latest === undefined && this.version !== Version.DRAFT_01 && this.version !== Version.DRAFT_02) { - const cached = hooks.latestSequence(track); - const reported = hooks.peekLargest(track).largest?.group; - const newest = - cached === undefined ? reported : reported === undefined ? cached : max(cached, reported); + // copy still fetching upstream, or on lite-05 one sent alongside its TRACK: the newest + // group known once it resolves stands in, including a largest the upstream's answer + // reported, whether or not the copy already held a group. A newer group the answer + // brings later is not waited for. Lite-01/02 keep their arrival floor, as Rust does. + if (this.version !== Version.DRAFT_01 && this.version !== Version.DRAFT_02) { + let newest = latest; + for (const known of [hooks.latestSequence(track), hooks.peekLargest(track).largest?.group]) { + if (known !== undefined) newest = newest === undefined ? known : max(newest, known); + } const resolved = cursorFloor(this.version, msg.startGroup, newest); if (resolved !== floor) { floor = resolved; diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index 2d75c1add7b..dbabbd57ab9 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -135,11 +135,11 @@ fn serving_max_delay(version: Version, requested: Duration) -> Option /// an unbounded budget so nothing is dropped under them (see [`serving_max_delay`]), which /// must not read as a request to replay the whole cache on join. /// -/// `latest` is the newest group when the SUBSCRIBE arrived: anything written since is newer, -/// so it is delivered. A SUBSCRIBE that found none, such as one a relay serves from a copy -/// still fetching upstream, or on lite-05 one sent alongside its TRACK, takes the newest group -/// known once the track resolves, including a largest the upstream's answer reported. A newer -/// group the answer brings later is not waited for. Lite01/02 keep a zero floor. +/// `latest` is the newest group known once the track resolves, which a SUBSCRIBE can beat, as +/// one a relay serves from a copy still fetching upstream, or on lite-05 one sent alongside its +/// TRACK. It includes a largest the upstream's answer reported, whether or not the copy already +/// held a group, so a join starts at the same group warm or cold. A newer group the answer +/// brings later is not waited for. Lite01/02 keep the newest group on arrival, or a zero floor. fn cursor_floor(version: Version, start_group: Option, latest: Option) -> u64 { if version.resolves_start() || start_group.is_some() { return 0; @@ -2528,11 +2528,9 @@ impl Request for SubscribeServe { let latest = match shared.version { Version::Lite01 | Version::Lite02 => latest, - _ => latest.or_else(|| { - track - .latest_sequence() - .max(track.peek_largest().map(|largest| largest.group)) - }), + _ => latest + .max(track.latest_sequence()) + .max(track.peek_largest().map(|largest| largest.group)), }; let floor = cursor_floor(shared.version, bounds.groups().start(), latest); let mut run = TrackRun::new(sub, track, bounds, floor, steer_tx); @@ -9484,6 +9482,8 @@ mod tests { enum Answer { /// Groups 0 and 3 written before the SUBSCRIBE arrives, 7 after. Resolved, + /// Groups 0 and 3 written and group 9 reported before the SUBSCRIBE arrives, 7 and 9 after. + Warm, /// The answer holds groups 0, 3, and 7. Cached, /// The answer holds group 3 and reports group 9 as its largest. @@ -9507,7 +9507,13 @@ mod tests { Version::Lite05, Version::Lite06, ] { - for answer in [Answer::Resolved, Answer::Cached, Answer::Reported, Answer::Empty] { + for answer in [ + Answer::Resolved, + Answer::Warm, + Answer::Cached, + Answer::Reported, + Answer::Empty, + ] { for start in [None, Some(0)] { let origin = crate::origin::Config::new(crate::Hop::new(1).unwrap()).produce(); let broadcast = origin.create_broadcast("room").unwrap(); @@ -9520,9 +9526,12 @@ mod tests { let mut staged = Some(request.stage(track::Info::timed())); staged.as_ref().unwrap().received(); let mut track = None; - if let Answer::Resolved = answer { + if let Answer::Resolved | Answer::Warm = answer { let mut resolved = staged.take().unwrap().commit(); write(&mut resolved, &[0, 3]); + if let Answer::Warm = answer { + resolved.report_largest(track::Position::group(9)); + } track = Some(resolved); } @@ -9580,7 +9589,7 @@ mod tests { drive().await; let mut track = match answer { - Answer::Resolved => track.take().unwrap(), + Answer::Resolved | Answer::Warm => track.take().unwrap(), Answer::Cached => { let mut staged = staged.take().unwrap(); write(&mut staged, &[0, 3, 7]); @@ -9597,7 +9606,7 @@ mod tests { drive().await; match answer { Answer::Resolved => write(&mut track, &[7]), - Answer::Reported => write(&mut track, &[7, 9]), + Answer::Reported | Answer::Warm => write(&mut track, &[7, 9]), Answer::Empty => write(&mut track, &[0, 3, 7]), Answer::Cached => {} } @@ -9622,6 +9631,7 @@ mod tests { sequences.sort(); let all: &[u64] = match answer { Answer::Reported => &[3, 7, 9], + Answer::Warm => &[0, 3, 7, 9], _ => &[0, 3, 7], }; let expected: &[u64] = if start.is_some() || version.resolves_start() { @@ -9629,7 +9639,9 @@ mod tests { } else { match answer { Answer::Resolved => &[3, 7], + Answer::Warm if version == Version::Lite01 => &[3, 7, 9], _ if version == Version::Lite01 => all, + Answer::Warm => &[9], Answer::Cached => &[7], Answer::Reported => &[9], Answer::Empty => &[0, 3, 7],