From ca700f15248002ed86f2be1f48743325a88328d9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 14:36:57 -0800 Subject: [PATCH 1/4] chore(quest): claim quest/m2/anchor-oldest.md Quest-Claim: d753a2adbda5b35b From c5f236e9466294bb44a2763d52a864e566a0dc77 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:20:08 -0800 Subject: [PATCH 2/4] feat(net)!: Anchor::Oldest replaces the Relative(u64::MAX) sentinel Range::oldest starts at a dedicated Anchor::Oldest, so every Relative(n) means n groups back from the latest one, clamped to group 0, however large n is. A peer's Relative(u64::MAX) no longer differs from u64::MAX - 1 in window start, demand shape or wire form, and the lite encoder clamps a count past the varint instead of turning it into the oldest start. An Oldest end is refused with InvalidRange. The C and UniFFI bindings gain the variant. Co-Authored-By: Claude Opus 5.5 --- dart/moq/lib/src/aliases.dart | 2 +- dart/moq_ffi/lib/src/moq.dart | 36 ++++++++++++++++++ go/wrapper/types.go | 4 +- .../kotlin/dev/moq/Aliases.kt | 8 +++- py/moq-rs/moq/_records.py | 16 ++++++-- quest/m2/README.md | 1 - quest/m2/anchor-oldest.md | 13 ------- rs/moq-c/src/api.rs | 3 ++ rs/moq-ffi/src/consumer.rs | 3 ++ rs/moq-net/src/error.rs | 2 +- rs/moq-net/src/ietf/subscriber.rs | 5 +-- rs/moq-net/src/lite/subscriber.rs | 24 +++++++++--- rs/moq-net/src/model/resume.rs | 4 +- rs/moq-net/src/model/subscription.rs | 27 ++++++++----- rs/moq-net/src/model/subscription/window.rs | 38 ++++++++++++++++--- rs/moq-net/src/model/track.rs | 2 +- rs/moq-net/src/model/track/demand.rs | 2 +- rs/moq-net/src/model/track/demand/ranges.rs | 6 ++- swift/Sources/Moq/Aliases.swift | 5 ++- 19 files changed, 148 insertions(+), 53 deletions(-) delete mode 100644 quest/m2/anchor-oldest.md diff --git a/dart/moq/lib/src/aliases.dart b/dart/moq/lib/src/aliases.dart index e456a825512..55c9ab60c04 100644 --- a/dart/moq/lib/src/aliases.dart +++ b/dart/moq/lib/src/aliases.dart @@ -160,7 +160,7 @@ Subscription defaultSubscription() => Subscription._(moqSubscriptionDefaults()); /// A span of a track to deliver: from `start` up to `end`, or onward when `end` is null. typedef Range = MoqRange; -/// Where one end of a [Range] falls: absolute, relative to the latest group, or the group containing a time. +/// Where one end of a [Range] falls: the oldest group (a start only), absolute, relative to the latest group, or the group containing a time. typedef Anchor = MoqAnchor; /// Which pending group the publisher sends first. diff --git a/dart/moq_ffi/lib/src/moq.dart b/dart/moq_ffi/lib/src/moq.dart index 572cdaf62f8..16e2073a0eb 100644 --- a/dart/moq_ffi/lib/src/moq.dart +++ b/dart/moq_ffi/lib/src/moq.dart @@ -8,7 +8,9 @@ import "dart:ffi"; import "dart:io" show Platform, File, Directory; import "dart:isolate"; import "dart:typed_data"; + import "package:ffi/ffi.dart"; + import "uniffi_runtime.dart"; export "uniffi_runtime.dart"; @@ -5506,6 +5508,12 @@ class FfiConverterMoqAnchor { lifted.value, lifted.bytesRead - subview.offsetInBytes + 4, ); + case 4: + final lifted = OldestMoqAnchor.read(subview); + return LiftRetVal( + lifted.value, + lifted.bytesRead - subview.offsetInBytes + 4, + ); default: throw UniffiInternalError( UniffiInternalError.unexpectedEnumCase, @@ -5641,6 +5649,34 @@ class TimeMoqAnchor extends MoqAnchor { } } +class OldestMoqAnchor extends MoqAnchor { + OldestMoqAnchor(); + OldestMoqAnchor._(); + static LiftRetVal read(Uint8List buf) { + int new_offset = buf.offsetInBytes; + return LiftRetVal(OldestMoqAnchor._(), new_offset); + } + + @override + RustBuffer lower() { + final buf = Uint8List(allocationSize()); + write(buf); + return toRustBuffer(buf); + } + + @override + int allocationSize() { + return 4; + } + + @override + int write(Uint8List buf) { + buf.buffer.asByteData(buf.offsetInBytes).setInt32(0, 4); + int new_offset = buf.offsetInBytes + 4; + return new_offset; + } +} + enum MoqOrder { ascending, descending } class FfiConverterMoqOrder { diff --git a/go/wrapper/types.go b/go/wrapper/types.go index 787a68d6edb..9367d94be2e 100644 --- a/go/wrapper/types.go +++ b/go/wrapper/types.go @@ -59,8 +59,10 @@ type ( // A relative start is the first frame of its group, a relative end includes its whole // group, and an absolute end is exclusive. Range = ffi.MoqRange - // Anchor is where one end of a Range falls: AnchorAbsolute, AnchorRelative, or AnchorTime. + // Anchor is where one end of a Range falls: AnchorOldest, AnchorAbsolute, AnchorRelative, or AnchorTime. Anchor = ffi.MoqAnchor + // AnchorOldest is the oldest group still around, as far back as the max delay allows. A start only. + AnchorOldest = ffi.MoqAnchorOldest // AnchorAbsolute is a group, delivered from its first frame. AnchorAbsolute = ffi.MoqAnchorAbsolute // AnchorRelative is the group Groups before the latest one, clamped to group 0: 0 is the latest. diff --git a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt index 356134c8b47..ce09864972f 100644 --- a/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt +++ b/kt/moq/src/jvmAndAndroidMain/kotlin/dev/moq/Aliases.kt @@ -186,9 +186,15 @@ fun uniffi.moq.MoqSubscription.Companion.default(): Subscription = /** A span of a track to deliver: from `start` up to `end`, or onward when `end` is null. */ typealias Range = uniffi.moq.MoqRange -/** Where one end of a [Range] falls: [AnchorAbsolute], [AnchorRelative], or [AnchorTime]. */ +/** + * Where one end of a [Range] falls: [AnchorOldest], [AnchorAbsolute], [AnchorRelative], or + * [AnchorTime]. + */ typealias Anchor = uniffi.moq.MoqAnchor +/** The oldest group still around, as far back as the max delay allows. A start only. */ +typealias AnchorOldest = uniffi.moq.MoqAnchor.Oldest + /** A group, delivered from its first frame. */ typealias AnchorAbsolute = uniffi.moq.MoqAnchor.Absolute diff --git a/py/moq-rs/moq/_records.py b/py/moq-rs/moq/_records.py index d445a85e38d..b034d4d48e3 100644 --- a/py/moq-rs/moq/_records.py +++ b/py/moq-rs/moq/_records.py @@ -142,19 +142,28 @@ def _from_ffi(datagram: MoqDatagram) -> Datagram: class Anchor: """Where one end of a :class:`Range` falls, mirroring moq-net's ``track::Anchor``. - Build one with ``Anchor.ABSOLUTE(group=...)``, ``Anchor.RELATIVE(groups=...)``, or - ``Anchor.TIME(timestamp=...)``. + Build one with ``Anchor.OLDEST()``, ``Anchor.ABSOLUTE(group=...)``, + ``Anchor.RELATIVE(groups=...)``, or ``Anchor.TIME(timestamp=...)``. """ + OLDEST: ClassVar[type[_AnchorOldest]] ABSOLUTE: ClassVar[type[_AnchorAbsolute]] RELATIVE: ClassVar[type[_AnchorRelative]] TIME: ClassVar[type[_AnchorTime]] def _ffi(self) -> MoqAnchor: - raise TypeError("build an Anchor with Anchor.ABSOLUTE, Anchor.RELATIVE, or Anchor.TIME") + raise TypeError("build an Anchor with Anchor.OLDEST, Anchor.ABSOLUTE, Anchor.RELATIVE, or Anchor.TIME") # The generated variants subclass MoqAnchor only at runtime, so each _ffi casts for pyright. +@dataclass(frozen=True) +class _AnchorOldest(Anchor): + """The oldest group still around, as far back as the max delay allows. A start only.""" + + def _ffi(self) -> MoqAnchor: + return cast(MoqAnchor, MoqAnchor.OLDEST()) + + @dataclass(frozen=True) class _AnchorAbsolute(Anchor): """This group, delivered from its first frame.""" @@ -185,6 +194,7 @@ def _ffi(self) -> MoqAnchor: return cast(MoqAnchor, MoqAnchor.TIME(timestamp=self.timestamp._ffi())) +Anchor.OLDEST = _AnchorOldest Anchor.ABSOLUTE = _AnchorAbsolute Anchor.RELATIVE = _AnchorRelative Anchor.TIME = _AnchorTime diff --git a/quest/m2/README.md b/quest/m2/README.md index 8c9d864ec75..70545095d22 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -186,7 +186,6 @@ quests that require them. - [Publish reports rendition stats as hang's type](/quest/m2/rendition-stats-type.md) - `Rendition.stats` and `Report.Writer` take the rendition stats schema's input type, so encoders write plain numbers - [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 (held in m1 by an open claim) - [A recreated or restarted track cannot reuse group sequences](/quest/m2/sequence-reuse.md) - recreating a track or restarting an instance cannot give new content an old group identity unless the app seeds past it, and datagrams are documented as exempt -- [Subscriptions name the oldest group without a u64::MAX sentinel](/quest/m2/anchor-oldest.md) - `Anchor` gets an explicit oldest variant, decoded the same way on every wire - [moq auth serve bounds what a leaked token or public caller can do](/quest/m2/auth-serve-policy.md) - tokens need an `exp` unless the operator opts out, and a public listener authenticates relays - [Cut readiness](/quest/m2/cut-gate/README.md) - the checks the cut needs that break nothing: main no slower than release, vendored fixes ported, and moq-c at parity - [Probe](/quest/m2/probe/README.md) - a session probes up to the bitrate it wants to send or receive, so a stopped simulcast rendition resumes and a viewer up-switches; browser padding is parked in m3 (held in m2 by open claims; moves to m3) diff --git a/quest/m2/anchor-oldest.md b/quest/m2/anchor-oldest.md deleted file mode 100644 index f479a911d38..00000000000 --- a/quest/m2/anchor-oldest.md +++ /dev/null @@ -1,13 +0,0 @@ -# [S] Subscriptions name the oldest group without a u64::MAX sentinel - -## Goal - -`Anchor` has an explicit variant for "the oldest group", and `Relative(n)` always means n groups before the latest. Today `Range::oldest` is `Anchor::Relative(u64::MAX)` (`rs/moq-net/src/model/subscription.rs`), against the repository's rule never to use a huge value as a marker. A peer on lite-07 or IETF can send `u64::MAX` or `u64::MAX - 1`, which clamp to the same group but differ in window start, demand shape and `is_wire`, and the lite encoder treats any `back > MAX_QUIC` as oldest, which the model does not. - -## Plan - -Found by the 2026-11-07 invariant audit (model-4). Add the variant, decode and encode it consistently on every wire (lite-07, lite-06 and earlier, IETF), and mirror it in JS and the bindings. m2 because `Anchor` is public API. - -## Related - -- [Subscribe ranges](/quest/m2/subscribe-ranges/README.md) - owns the range model this anchor lives in diff --git a/rs/moq-c/src/api.rs b/rs/moq-c/src/api.rs index 57822e7a74d..a5b6fa89451 100644 --- a/rs/moq-c/src/api.rs +++ b/rs/moq-c/src/api.rs @@ -830,6 +830,8 @@ pub enum moq_anchor_kind { MOQ_ANCHOR_RELATIVE = 1, /// The group containing a presentation time. An untimed track refuses it. MOQ_ANCHOR_TIME = 2, + /// The oldest group still around, as far back as `delay.max_us` allows. A start only. + MOQ_ANCHOR_OLDEST = 3, } /// Which of a subscription's pending groups the publisher sends first. Zero is the default. @@ -848,6 +850,7 @@ fn parse_anchor(anchor: &moq_anchor) -> Result { v if v == moq_anchor_kind::MOQ_ANCHOR_ABSOLUTE as u32 => moq_net::track::Anchor::Absolute(anchor.group), v if v == moq_anchor_kind::MOQ_ANCHOR_RELATIVE as u32 => moq_net::track::Anchor::Relative(anchor.group), v if v == moq_anchor_kind::MOQ_ANCHOR_TIME as u32 => moq_net::track::Anchor::Time(anchor.timestamp.try_into()?), + v if v == moq_anchor_kind::MOQ_ANCHOR_OLDEST as u32 => moq_net::track::Anchor::Oldest, _ => return Err(Error::InvalidCode), }) } diff --git a/rs/moq-ffi/src/consumer.rs b/rs/moq-ffi/src/consumer.rs index e4db35cdfb9..fa5d7b6f2ed 100644 --- a/rs/moq-ffi/src/consumer.rs +++ b/rs/moq-ffi/src/consumer.rs @@ -126,6 +126,8 @@ pub enum MoqAnchor { /// The presentation time. timestamp: MoqTimestamp, }, + /// The oldest group still around, as far back as the max delay allows. A start only. + Oldest, } /// Which of a subscription's pending groups the publisher sends first, mirroring @@ -143,6 +145,7 @@ impl TryFrom for moq_net::track::Anchor { fn try_from(anchor: MoqAnchor) -> Result { Ok(match anchor { + MoqAnchor::Oldest => Self::Oldest, MoqAnchor::Absolute { group } => Self::Absolute(group), MoqAnchor::Relative { groups } => Self::Relative(groups), MoqAnchor::Time { timestamp } => Self::Time(timestamp.try_into()?), diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index e2edb909549..f1650a078e8 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -420,7 +420,7 @@ pub enum Error { #[error("invalid joining request ID")] InvalidJoiningRequestId, - /// A FETCH range is empty or lies beyond the published objects. + /// A range is empty, lies beyond the published objects, or ends at the oldest group. #[error("invalid fetch range")] InvalidRange, diff --git a/rs/moq-net/src/ietf/subscriber.rs b/rs/moq-net/src/ietf/subscriber.rs index 3b8eb9ccf88..a21c369f6e6 100644 --- a/rs/moq-net/src/ietf/subscriber.rs +++ b/rs/moq-net/src/ietf/subscriber.rs @@ -14233,9 +14233,8 @@ fn has_time(subscription: &track::Subscription) -> bool { fn needs_resolution(subscription: &track::Subscription) -> bool { subscription.ranges.iter().any(|range| { let start = match range.start { - track::Anchor::Absolute(_) => false, - track::Anchor::Relative(back) => back != 0 && back != u64::MAX, - track::Anchor::Time(_) => true, + track::Anchor::Oldest | track::Anchor::Absolute(_) | track::Anchor::Relative(0) => false, + track::Anchor::Relative(_) | track::Anchor::Time(_) => true, }; start || matches!(range.end, Some(track::Anchor::Time(_) | track::Anchor::Relative(_))) }) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 58f24509804..cee17e400b3 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -7710,6 +7710,18 @@ mod tests { ..Default::default() }; assert!(matches!(wire_ranges(&timed, None), Err(Error::TimestampMismatch))); + + // Only the oldest start goes out without an anchor; a count back past the varint + // stays one, clamped to the largest it can spell. + let start = |start| { + let subscription = Subscription::default().with_ranges([track::Range { start, end: None }]); + wire_ranges(&subscription, None).unwrap().unwrap()[0].start + }; + assert_eq!(start(Anchor::Oldest), None); + assert_eq!( + start(Anchor::Relative(u64::MAX)), + Some(lite::Anchor::Relative(MAX_QUIC)) + ); } /// A wire without ranges asks from group 0 when no single start covers both a count @@ -9319,10 +9331,10 @@ fn wire_ranges(subscription: &Subscription, timescale: Option) -> Res continue; } let start = match range.start { - // Past the varint's reach, a count back clamps to group 0 just as the oldest - // start does, which only that start can spell. - Anchor::Relative(back) if back > MAX_QUIC => None, - Anchor::Relative(back) => Some(lite::Anchor::Relative(back)), + Anchor::Oldest => None, + // No group sequence reaches past the varint, so a count back that does clamps to + // group 0 just as the largest one it can spell. + Anchor::Relative(back) => Some(lite::Anchor::Relative(back.min(MAX_QUIC))), Anchor::Absolute(group) => Some(lite::Anchor::Absolute(Position::group(group))), Anchor::Time(pts) => Some(time(pts)?), }; @@ -9331,7 +9343,7 @@ fn wire_ranges(subscription: &Subscription, timescale: Option) -> Res // nothing names a resolved end, so one resolved upstream would leave the copy short. let end = match range.end { Some(Anchor::Absolute(group)) => Some(lite::Anchor::Absolute(Position::group(group))), - None | Some(Anchor::Relative(_) | Anchor::Time(_)) => None, + None | Some(Anchor::Oldest | Anchor::Relative(_) | Anchor::Time(_)) => None, }; ranges.push(lite::Range { start, end }); } @@ -10144,7 +10156,7 @@ impl TrackServe { || subscription .ranges .iter() - .all(|range| range.end.is_none() && matches!(range.start, Anchor::Relative(0 | u64::MAX)))); + .all(|range| range.end.is_none() && matches!(range.start, Anchor::Oldest | Anchor::Relative(0)))); if opening && idle { subscription = subscription.with_start(newest); } diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index 5dcd5c43f23..e982ac6a63f 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -1108,9 +1108,7 @@ impl Reader { let end = previous.and_then(|previous| previous.end).or(resolved.end); track::Range { start: match range.start { - // The farthest offset reaches back to the oldest group, which names no floor. - Anchor::Relative(back) if back != u64::MAX => Anchor::Absolute(start), - Anchor::Time(_) => Anchor::Absolute(start), + Anchor::Relative(_) | Anchor::Time(_) => Anchor::Absolute(start), start => start, }, // A time end the copy has not settled stays one: its cap is not the end. diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index a2e52d9ecf7..9eaba85f4d6 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -167,7 +167,7 @@ pub enum Order { } /// Where one end of a [`Range`] falls: a fixed group, a count of groups back from the -/// latest one, or a presentation time. +/// latest one, the oldest group, or a presentation time. /// /// Every anchor names a whole group: a subscription always starts at a group's first frame, /// since a group is the unit of decodability. @@ -182,8 +182,8 @@ pub enum Order { /// A Rust moq-transport copy resolves a relative anchor from the Largest Object its live /// join reports, and accepts time anchors its cache can already resolve, translating both /// to group bounds upstream; any other time anchor is refused. Older moq-lite versions -/// accept absolute anchors but refuse time anchors and relative anchors other -/// than [`Range::live`] and [`Range::oldest`] with [`Error::Unsupported`](crate::Error::Unsupported) +/// accept absolute and [`Anchor::Oldest`] starts but refuse time anchors and relative +/// anchors other than [`Range::live`]'s with [`Error::Unsupported`](crate::Error::Unsupported) /// rather than guess from a partial copy. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Anchor { @@ -191,6 +191,12 @@ pub enum Anchor { Absolute(u64), /// The group this many before the latest, clamped to group 0: `Relative(0)` is the latest group. Relative(u64), + /// The oldest group still around, as far back as [`Delay::max`] allows, naming no floor. + /// + /// A start only: [`Range::oldest`] is the range starting here. An end here is refused + /// with [`Error::InvalidRange`](crate::Error::InvalidRange), since no hop can tell + /// which group is the oldest still around anywhere upstream. + Oldest, /// The group containing this presentation time: the newest one starting at or before it. /// /// Resolved from the starts of the groups the serving hop holds. When none starts at @@ -236,7 +242,7 @@ impl Range { /// starts at the live edge, filling nothing below it. pub fn oldest() -> Self { Self { - start: Anchor::Relative(u64::MAX), + start: Anchor::Oldest, end: None, } } @@ -261,9 +267,8 @@ impl Range { /// the live and oldest starts it spells with no floor. pub(super) fn is_wire(&self) -> bool { let start = match self.start { - Anchor::Absolute(_) => true, - Anchor::Relative(back) => back == 0 || back == u64::MAX, - Anchor::Time(_) => false, + Anchor::Oldest | Anchor::Absolute(_) | Anchor::Relative(0) => true, + Anchor::Relative(_) | Anchor::Time(_) => false, }; start && matches!(self.end, None | Some(Anchor::Absolute(_))) } @@ -329,6 +334,8 @@ pub(super) fn union(ranges: impl IntoIterator) -> Vec { (Anchor::Absolute(_), None | Some(Anchor::Absolute(_))) | (Anchor::Relative(_), None) => false, (Anchor::Absolute(start), _) => tail.is_none_or(|tail| start < tail), (Anchor::Relative(back), _) => farthest.is_none_or(|farthest| back > farthest), + // Nothing but the open oldest range, handled above, covers the oldest group. + (Anchor::Oldest, _) => true, // A time can't be placed against the others without the hop's groups. (Anchor::Time(_), _) => true, })); @@ -462,7 +469,9 @@ impl Subscription { .map(|(index, range)| { let resolved = resolved.get(index).copied().flatten().filter(|_| clamping); let start = match (range.start, resolved) { - (Anchor::Relative(_) | Anchor::Time(_), Some(resolved)) => Anchor::Absolute(resolved.start), + (Anchor::Oldest | Anchor::Relative(_) | Anchor::Time(_), Some(resolved)) => { + Anchor::Absolute(resolved.start) + } (start, _) => start, }; let requested = match (range.end, resolved.and_then(|resolved| resolved.end)) { @@ -501,7 +510,7 @@ impl Subscription { pub(super) fn joins_live(&self) -> bool { self.ranges .iter() - .any(|range| matches!(range.start, Anchor::Relative(_)) && range.end.is_none()) + .any(|range| matches!(range.start, Anchor::Oldest | Anchor::Relative(_)) && range.end.is_none()) } } diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 9c9d45bed1c..6784cac05f3 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -568,8 +568,9 @@ impl Walk { impl Window { /// Resolve `ranges`, counting relative anchors back from `latest`. /// - /// Refuses a time anchor on an untimed track with [`Error::TimestampMismatch`], and an - /// anchor a received copy's wire drops with [`Error::Unsupported`]. + /// Refuses an [`Anchor::Oldest`] end with [`Error::InvalidRange`], a time anchor on an + /// untimed track with [`Error::TimestampMismatch`], and an anchor a received copy's wire + /// drops with [`Error::Unsupported`]. pub fn new(ranges: &[Range], latest: Option, holdings: &impl Holdings) -> Result { let mut window = Self { ranges: ranges.to_vec(), @@ -1062,6 +1063,9 @@ impl Window { } fn place(&mut self, latest: Option, holdings: &impl Holdings) -> Result<()> { + if self.ranges.iter().any(|range| range.end == Some(Anchor::Oldest)) { + return Err(Error::InvalidRange); + } let time = |anchor: Option| matches!(anchor, Some(Anchor::Time(_))); if !holdings.timed() && self @@ -1239,8 +1243,8 @@ enum Start { fn start(anchor: Anchor, latest: Option, holdings: &impl Holdings) -> Start { match anchor { Anchor::Absolute(start) => Start::At(start, true), - // Nothing sits below group 0, so the farthest offset needs no latest group. - Anchor::Relative(u64::MAX) => Start::At(0, false), + // Nothing sits below group 0, so the oldest start needs no latest group. + Anchor::Oldest => Start::At(0, false), Anchor::Relative(back) => latest.map_or(Start::Pending, |latest| Start::At(latest.saturating_sub(back), true)), Anchor::Time(pts) => match holdings.containing(pts) { Containing::Group(group) => Start::At(group, true), @@ -1286,6 +1290,8 @@ fn end(anchor: Option, latest: Option) -> Option { .checked_add(1) .map_or(End::Open, End::Before), Some(Anchor::Time(pts)) => End::Time(pts), + // Refused before any range resolves; ending before group 0 would deliver nothing. + Some(Anchor::Oldest) => End::Before(0), }) } @@ -1500,9 +1506,31 @@ mod tests { assert_eq!(covered(&window, 8), vec![5, 6, 7]); assert!(window.is_settled()); - // The farthest offset needs no latest group, and names no floor. + // The oldest start needs no latest group, and names no floor. let window = Window::new(&[Range::oldest()], None, &Untimed).unwrap(); assert_eq!((window.floor(), window.start()), (Some(0), None)); + + // Any count back, however far, waits on a latest group and names the group it clamps + // to, as a peer's largest counts must agree. + for back in [u64::MAX, u64::MAX - 1] { + let open = Range { + start: Anchor::Relative(back), + end: None, + }; + assert!(!open.is_wire()); + assert!(Window::new(&[open], None, &Untimed).unwrap().is_pending()); + assert_eq!(Window::new(&[open], Some(7), &Untimed).unwrap().start(), Some(0)); + } + } + + /// No hop can place the oldest group as an end, so it is refused. + #[test] + fn an_oldest_end_is_refused() { + let ranges = [range(Anchor::Absolute(0), Anchor::Oldest)]; + assert!(matches!( + Window::new(&ranges, Some(3), &Untimed), + Err(Error::InvalidRange) + )); } /// The named floor is the lowest start, unless a range reaches back to the oldest diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 0ee302943ad..59324e10fa9 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -4059,7 +4059,7 @@ impl Producer { for (range, resolved) in subscription.ranges.iter_mut().zip(resolved) { if match range.start { super::subscription::Anchor::Time(_) => true, - super::subscription::Anchor::Relative(back) => back != 0 && back != u64::MAX, + super::subscription::Anchor::Relative(back) => back != 0, _ => false, } { range.start = super::subscription::Anchor::Absolute(resolved.ok_or(Error::Unsupported)?.start); diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 8fc49494f23..4b29e19edd9 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -314,7 +314,7 @@ mod tests { let index = rng.random_range(0..slots.len()); let anchor = |rng: &mut rand::rngs::StdRng| match rng.random_range(0..4) { 0 => Anchor::Relative(rng.random_range(0..3)), - 1 => Anchor::Relative(u64::MAX), + 1 => Anchor::Oldest, _ => Anchor::Absolute(rng.random_range(0..8)), }; let ranges: Vec = (0..rng.random_range(0..3)) diff --git a/rs/moq-net/src/model/track/demand/ranges.rs b/rs/moq-net/src/model/track/demand/ranges.rs index 30f4595b89d..4cdcbb5a1c8 100644 --- a/rs/moq-net/src/model/track/demand/ranges.rs +++ b/rs/moq-net/src/model/track/demand/ranges.rs @@ -41,7 +41,7 @@ impl Shape { fn of(range: &Range) -> Self { match (range.start, range.end) { _ if range.is_empty() => Self::Empty, - (Anchor::Relative(u64::MAX), None) => Self::Oldest, + (Anchor::Oldest, None) => Self::Oldest, (Anchor::Absolute(start), None) => Self::Absolute(start, None), (Anchor::Absolute(start), Some(Anchor::Absolute(end))) => Self::Absolute(start, Some(end)), (Anchor::Relative(back), None) => Self::Relative(back), @@ -140,6 +140,8 @@ impl Ranges { match range.start { Anchor::Absolute(start) => self.absolute.tail().is_some_and(|tail| tail <= start), Anchor::Relative(back) => self.farthest().is_some_and(|farthest| back <= farthest), + // Only the open oldest range, which hides every other, covers the oldest group. + Anchor::Oldest => false, // Where a time falls among the groups is the serving hop's to say. Anchor::Time(_) => false, } @@ -354,7 +356,7 @@ mod tests { let mut rng = rand::rngs::StdRng::seed_from_u64(0x72616e676573); let anchor = |rng: &mut rand::rngs::StdRng| match rng.random_range(0..6) { 0 => Anchor::Relative(rng.random_range(0..4)), - 1 => Anchor::Relative(u64::MAX), + 1 => Anchor::Oldest, _ => Anchor::Absolute(rng.random_range(0..12)), }; let mut ranges = Ranges::default(); diff --git a/swift/Sources/Moq/Aliases.swift b/swift/Sources/Moq/Aliases.swift index 2a9bf07b2cd..b30d1a828e3 100644 --- a/swift/Sources/Moq/Aliases.swift +++ b/swift/Sources/Moq/Aliases.swift @@ -129,8 +129,9 @@ public typealias Position = MoqFFI.MoqPosition extension Subscription { /// A span of a track to deliver: from `start` up to `end`, or onward when `end` is `nil`. public typealias Range = MoqFFI.MoqRange - /// Where one end of a `Range` falls: `.absolute(group:)`, `.relative(groups:)` - /// back from the latest group, or `.time(timestamp:)` for the group containing it. + /// Where one end of a `Range` falls: `.oldest` (a start only), `.absolute(group:)`, + /// `.relative(groups:)` back from the latest group, or `.time(timestamp:)` for the group + /// containing it. public typealias Anchor = MoqFFI.MoqAnchor /// Which pending group the publisher sends first: `.ascending` or `.descending`. public typealias Order = MoqFFI.MoqOrder From 0c2125fe8cc9777ac4cbfccd251a49c77a789bf3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 15:41:12 -0800 Subject: [PATCH 3/4] fix(net): routed readers leave an oldest end to their copies, and lite-07 keeps full counts Co-Authored-By: Claude Opus 5.5 --- rs/moq-net/src/lite/subscriber.rs | 11 +++----- rs/moq-net/src/model/resume.rs | 28 +++++++++++++++++++++ rs/moq-net/src/model/subscription/window.rs | 19 +++++++++++--- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index cee17e400b3..7eaf0ba3eb0 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -10,7 +10,7 @@ use std::{ use crate::{ Error, Path, Timescale, Timestamp, bandwidth, - coding::{Decode as _, Reader, Stream, varint::MAX_QUIC}, + coding::{Decode as _, Reader, Stream}, lite, track::{Anchor, Position, Subscription}, }; @@ -7711,8 +7711,7 @@ mod tests { }; assert!(matches!(wire_ranges(&timed, None), Err(Error::TimestampMismatch))); - // Only the oldest start goes out without an anchor; a count back past the varint - // stays one, clamped to the largest it can spell. + // Only the oldest start goes out without an anchor; any count back stays one. let start = |start| { let subscription = Subscription::default().with_ranges([track::Range { start, end: None }]); wire_ranges(&subscription, None).unwrap().unwrap()[0].start @@ -7720,7 +7719,7 @@ mod tests { assert_eq!(start(Anchor::Oldest), None); assert_eq!( start(Anchor::Relative(u64::MAX)), - Some(lite::Anchor::Relative(MAX_QUIC)) + Some(lite::Anchor::Relative(u64::MAX)) ); } @@ -9332,9 +9331,7 @@ fn wire_ranges(subscription: &Subscription, timescale: Option) -> Res } let start = match range.start { Anchor::Oldest => None, - // No group sequence reaches past the varint, so a count back that does clamps to - // group 0 just as the largest one it can spell. - Anchor::Relative(back) => Some(lite::Anchor::Relative(back.min(MAX_QUIC))), + Anchor::Relative(back) => Some(lite::Anchor::Relative(back)), Anchor::Absolute(group) => Some(lite::Anchor::Absolute(Position::group(group))), Anchor::Time(pts) => Some(time(pts)?), }; diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index e982ac6a63f..aeb531b2e8e 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -7042,6 +7042,34 @@ mod test { assert_eq!(sub.floor(), Some(0)); } + /// No hop can place an oldest end: a routed reader's copy refuses it, rather than the + /// reader's own window panicking on it. + #[test] + #[cfg(feature = "async")] + fn a_routed_reader_refuses_an_oldest_end() { + let routes = Producer::new(); + let logical = logical(&routes); + let a = copy(); + write(&a, 0); + routes.serve(a.consume()); + let subscription = Subscription::default().with_ranges([track::Range { + start: Anchor::Absolute(0), + end: Some(Anchor::Oldest), + }]); + let mut sub = logical + .consume() + .subscribe(subscription) + .now_or_never() + .unwrap() + .unwrap(); + let result = sub.poll_recv_group(&kio::Waiter::noop()); + assert!( + matches!(result, Poll::Ready(Err(Error::InvalidRange))), + "{:?}", + result.map(|r| r.map(|g| g.map(|g| g.sequence))) + ); + } + /// A time end resolves on the serving copy even when the start is already absolute. #[test] #[cfg(feature = "async")] diff --git a/rs/moq-net/src/model/subscription/window.rs b/rs/moq-net/src/model/subscription/window.rs index 6784cac05f3..b0c0fa58e30 100644 --- a/rs/moq-net/src/model/subscription/window.rs +++ b/rs/moq-net/src/model/subscription/window.rs @@ -57,6 +57,13 @@ pub(in crate::model) trait Holdings { false } + /// Whether this hop resolves ranges against groups of its own, so it refuses an + /// [`Anchor::Oldest`] end, which names no group. A hop whose copies resolve them leaves + /// the refusal to them. + fn resolves(&self) -> bool { + true + } + /// Whether a [`Requests`](crate::track::Requests) handler serving the track's history /// may look up a time its groups can't resolve: a time start before every group with a /// known start then waits on that lookup, and a sequence the live feed never delivered, @@ -129,6 +136,10 @@ pub(in crate::model) enum Held { pub(in crate::model) struct Unheld; impl Holdings for Unheld { + fn resolves(&self) -> bool { + false + } + fn timed(&self) -> bool { true } @@ -568,7 +579,8 @@ impl Walk { impl Window { /// Resolve `ranges`, counting relative anchors back from `latest`. /// - /// Refuses an [`Anchor::Oldest`] end with [`Error::InvalidRange`], a time anchor on an + /// Refuses an [`Anchor::Oldest`] end with [`Error::InvalidRange`] where the hop + /// [resolves](Holdings::resolves) ranges itself, a time anchor on an /// untimed track with [`Error::TimestampMismatch`], and an anchor a received copy's wire /// drops with [`Error::Unsupported`]. pub fn new(ranges: &[Range], latest: Option, holdings: &impl Holdings) -> Result { @@ -1063,7 +1075,7 @@ impl Window { } fn place(&mut self, latest: Option, holdings: &impl Holdings) -> Result<()> { - if self.ranges.iter().any(|range| range.end == Some(Anchor::Oldest)) { + if holdings.resolves() && self.ranges.iter().any(|range| range.end == Some(Anchor::Oldest)) { return Err(Error::InvalidRange); } let time = |anchor: Option| matches!(anchor, Some(Anchor::Time(_))); @@ -1290,7 +1302,8 @@ fn end(anchor: Option, latest: Option) -> Option { .checked_add(1) .map_or(End::Open, End::Before), Some(Anchor::Time(pts)) => End::Time(pts), - // Refused before any range resolves; ending before group 0 would deliver nothing. + // Refused where groups are held; a hop that holds none lets its copies refuse it, and + // delivers nothing meanwhile. Some(Anchor::Oldest) => End::Before(0), }) } From e62390cef4fe0456ca3a4c909df172f87e9c2bf4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 7 Nov 2026 16:06:55 -0800 Subject: [PATCH 4/4] fix(net): an oldest end asks nothing upstream, and routed readers refuse it without a copy A range ending at Anchor::Oldest counts as empty, so no subscriber's refused range reaches the demand a lite or IETF hop asks upstream. A routed reader refuses it before it waits on a copy. moq.h now declares moq_anchor_kind, so C callers can name MOQ_ANCHOR_OLDEST. Co-Authored-By: Claude Opus 5.5 --- rs/moq-c/build.rs | 1 + rs/moq-net/src/model/resume.rs | 43 +++++++++++++++++++++------- rs/moq-net/src/model/subscription.rs | 4 +++ rs/moq-net/src/model/track/demand.rs | 13 +++++++++ 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/rs/moq-c/build.rs b/rs/moq-c/build.rs index c9ed5f95bbe..420816a9213 100644 --- a/rs/moq-c/build.rs +++ b/rs/moq-c/build.rs @@ -18,6 +18,7 @@ const ENUMS: &[&str] = &[ "moq_protocol_kind", "moq_demand", "moq_decode_start", + "moq_anchor_kind", ]; fn main() { diff --git a/rs/moq-net/src/model/resume.rs b/rs/moq-net/src/model/resume.rs index aeb531b2e8e..457df779de0 100644 --- a/rs/moq-net/src/model/resume.rs +++ b/rs/moq-net/src/model/resume.rs @@ -1315,6 +1315,20 @@ impl Reader { } } + /// Why the reader can read nothing more: copies contradicted the track's end, or its + /// ranges hold an [`Anchor::Oldest`] end. That end is refused here, before waiting on a + /// copy to refuse it, since the demand asked upstream leaves it out. + fn refusal(&self) -> Option { + if self.refused { + return Some(Error::ProtocolViolation); + } + self.mirrored + .ranges + .iter() + .any(|range| range.end == Some(Anchor::Oldest)) + .then_some(Error::InvalidRange) + } + /// Whether `at` is past the declared end, which refuses the track: another copy /// contradicts the copy that declared it. fn past_end(&mut self, at: Position) -> bool { @@ -1495,8 +1509,8 @@ impl Reader { waiter: &kio::Waiter, ) -> Option>>> { self.sync(waiter); - if self.refused { - return Some(Poll::Ready(Err(Error::ProtocolViolation))); + if let Some(err) = self.refusal() { + return Some(Poll::Ready(Err(err))); } self.groups_read = true; self.backfill_gaps(delivered); @@ -1704,8 +1718,8 @@ impl Reader { loop { self.sync(waiter); self.learn_end(Some(waiter)); - if self.refused { - return Poll::Ready(Err(Error::ProtocolViolation)); + if let Some(err) = self.refusal() { + return Poll::Ready(Err(err)); } if let Some(end) = self.declared { return Poll::Ready(Ok(end)); @@ -2184,8 +2198,8 @@ impl Recover { if let Some(reader) = self.reader.as_ref().and_then(Weak::upgrade) { let mut reader = reader.lock().unwrap_or_else(PoisonError::into_inner); reader.sync(waiter); - if reader.refused { - return Poll::Ready(Err(Error::ProtocolViolation)); + if let Some(err) = reader.refusal() { + return Poll::Ready(Err(err)); } } loop { @@ -7042,16 +7056,25 @@ mod test { assert_eq!(sub.floor(), Some(0)); } - /// No hop can place an oldest end: a routed reader's copy refuses it, rather than the - /// reader's own window panicking on it. + /// No hop can place an oldest end: a routed reader refuses it, with or without a copy, + /// rather than its own window panicking on it. #[test] #[cfg(feature = "async")] fn a_routed_reader_refuses_an_oldest_end() { + for served in [true, false] { + a_routed_reader_refuses_an_oldest_end_when(served); + } + } + + #[cfg(feature = "async")] + fn a_routed_reader_refuses_an_oldest_end_when(served: bool) { let routes = Producer::new(); let logical = logical(&routes); let a = copy(); write(&a, 0); - routes.serve(a.consume()); + if served { + routes.serve(a.consume()); + } let subscription = Subscription::default().with_ranges([track::Range { start: Anchor::Absolute(0), end: Some(Anchor::Oldest), @@ -7065,7 +7088,7 @@ mod test { let result = sub.poll_recv_group(&kio::Waiter::noop()); assert!( matches!(result, Poll::Ready(Err(Error::InvalidRange))), - "{:?}", + "served={served}: {:?}", result.map(|r| r.map(|g| g.map(|g| g.sequence))) ); } diff --git a/rs/moq-net/src/model/subscription.rs b/rs/moq-net/src/model/subscription.rs index 9eaba85f4d6..687825d0c77 100644 --- a/rs/moq-net/src/model/subscription.rs +++ b/rs/moq-net/src/model/subscription.rs @@ -274,8 +274,12 @@ impl Range { } /// Whether this range certainly delivers nothing, wherever the latest group lands. + /// + /// An [`Anchor::Oldest`] end delivers nothing either: every reader holding one is + /// refused, so it never reaches the demand asked upstream. pub(super) fn is_empty(&self) -> bool { match (self.start, self.end) { + (_, Some(Anchor::Oldest)) => true, (Anchor::Absolute(start), Some(Anchor::Absolute(end))) => end <= start, _ => false, } diff --git a/rs/moq-net/src/model/track/demand.rs b/rs/moq-net/src/model/track/demand.rs index 4b29e19edd9..df0d27cc32c 100644 --- a/rs/moq-net/src/model/track/demand.rs +++ b/rs/moq-net/src/model/track/demand.rs @@ -372,6 +372,19 @@ mod tests { } } + /// An oldest end is refused wherever it is read, so it asks nothing upstream, even + /// past an absolute start. + #[test] + fn an_oldest_end_adds_no_demand() { + let broadcast = crate::broadcast::Info::default().produce(); + let producer = broadcast.create_track("demand", None).unwrap(); + let _sub = producer.subscribe(Subscription::default().with_ranges([Range { + start: Anchor::Absolute(3), + end: Some(Anchor::Oldest), + }])); + assert_eq!(producer.subscription().map(|sub| sub.ranges), Some(Vec::new())); + } + #[test] fn unchanged_demand_does_not_wake_the_publisher() { let broadcast = crate::broadcast::Info::default().produce();