Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3627,11 +3627,13 @@ export class Subscriber {
return keys.length === 0 ? "unknown" : "before";
},
nextStart: (from) => keys[keyIndex(keys, from)],
back: (latest, back) => {
// A lite-07 copy holds only part of the track, so it counts back as its publisher did,
// from the latest group the publisher counted from: a newer one moved the live edge.
// A lite-07 copy holds only part of the track, so it starts where its publisher did,
// counting from the same latest group: a newer one moved the live edge.
reportedStart: (latest, back) => {
const reported = receivedCopy(producer) ? anchors.relative.get(back) : undefined;
if (reported !== undefined && reported.latest === latest) return reported.start;
return reported !== undefined && reported.latest === latest ? reported.start : undefined;
},
back: (latest, back) => {
if (back === 0n) return latest;
// Bounded by the cache: a huge count reaches back to the oldest.
const index = BigInt(keyIndex(keys, latest)) - back;
Expand Down
16 changes: 16 additions & 0 deletions js/net/src/window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ describe("Window", () => {
expect(window.missable(0n)[0]).toEqual({ start: 5n, end: UNBOUNDED });
});

test("a reported start moves a relative start, never a relative end", () => {
const held = new Keys(keys(0n, 10n));
// The publisher reported starting the range counting back 2 from 9 at 8, past the 7 counted.
const reported: Holdings = {
resolves: () => true,
containing: (at) => held.containing(at),
nextStart: (from) => held.nextStart(from),
back: (latest, back) => held.back(latest, back),
reportedStart: (latest, back) => (latest === 9n && back === 2n ? 8n : undefined),
complete: () => false,
};
expect(new Window([range(relative(2n))], 9n, reported).start()).toBe(8n);
const ended = new Window([range(time(0n), relative(2n))], 9n, reported);
expect(covered(ended, held.held)).toEqual(keys(0n, 8n));
});

test("relative anchors count groups back from the latest", () => {
let held = new Keys([0n, 33n, 66n, 99n]);
let window = new Window([range(relative(2n), relative(1n))], 99n, held);
Expand Down
9 changes: 8 additions & 1 deletion js/net/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ export interface Holdings {
* undefined once fewer are held than that.
*/
back(latest: bigint, back: bigint): bigint | undefined;
/**
* Where the publisher reported starting a range counting `back` groups before `latest`, which a
* relative start, but never an end, takes over {@link back}: a delivery start may sit past the
* group counted, such as above groups too stale for the budget. Absent for none. Mirrors the
* Rust `Holdings::reported_start`.
*/
reportedStart?(latest: bigint, back: bigint): bigint | undefined;
/** Whether nothing more can arrive here, so a time start nothing contains reaches back to the oldest group rather than wait. */
complete(): boolean;
/**
Expand Down Expand Up @@ -785,7 +792,7 @@ function startOf(
if (anchor === undefined) return { group: 0n, floored: false };
if (anchor.relative !== undefined) {
if (latest === undefined) return "pending";
const group = holdings.back(latest, anchor.relative);
const group = holdings.reportedStart?.(latest, anchor.relative) ?? holdings.back(latest, anchor.relative);
// Fewer groups are held than it counts back, so reach back to the oldest.
return group === undefined ? { group: 0n, floored: false } : { group, floored: true };
}
Expand Down
35 changes: 35 additions & 0 deletions quest/m1/lite07-widened-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# [S] The @moq/net widened-range SUBSCRIBE_STARTED test passes again

## Goal

`js/net/src/lite/subscriber.test.ts` "a widened range's SUBSCRIBE_STARTED
resolves the feed's start" passes on `main`, with whatever it found fixed
at its root: after an update widens a lite-07 subscription's ranges, the
copy's start (`hooks.sourceStart`) stays pending until the widened range's
SUBSCRIBE_STARTED says where it starts.

## Plan

Found by [moq-dev/moq#9132](https://github.com/moq-dev/moq/pull/9132)'s
`just check`. It fails deterministically, with `sourceStart` reading `0n`
right after the update, where the test expects `"pending"`:

```sh
cd js/net && bun test src/lite/subscriber.test.ts -t "a widened range's SUBSCRIBE_STARTED"
```

It passes on 928d337396's parent and fails from
[moq-dev/moq#9045](https://github.com/moq-dev/moq/pull/9045) on, which
changed how a lite-07 relay fits its ranges into one SUBSCRIBE_UPDATE. The
merge queue runs only the tests for changed files, so it landed unseen.

- Decide whether the update's new range still needs its own
SUBSCRIBE_STARTED under #9045's fitting. If it does, fix `#declare` or
the update path; if a covering superset now legitimately keeps the start,
rewrite the test to that rule, mirrored by the Rust test that covers it.

Decided 2026-11-19 (coordinator, maintainer away, merge granted): keep this
follow-up while [moq-dev/moq#9147](https://github.com/moq-dev/moq/pull/9147),
which fixes it, is open. #9147 finds #9045 changed only timing (an async encode
before the update's declaration) and fixes the test; it should delete this
quest when it lands.
34 changes: 34 additions & 0 deletions quest/m2/time-addressing/late-relative-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# [M] A later reader's relative start on a partial lite-07 copy reads only its groups

## Goal

A reader that joins a lite-07 received copy already holding part of the
track, with a relative start, reads the groups its publisher counts back to
and nothing below them, in Rust and `@moq/net`. Today it resolves the start
against the groups the copy holds before SUBSCRIBE_STARTED can report the
publisher's: over a copy holding groups 1 and 2 of 0..9, a second reader of
`[{relative:2..time:8}]` reads 1, 2, 7, 8, then 3, 4, 5 and on, where it
should read 7 and 8 and end.

## Plan

Found by [moq-dev/moq#9132](https://github.com/moq-dev/moq/pull/9132), which
reports each live range's relative start on the copy
(`TrackState::relative_starts` in `rs/moq-net/src/model/track.rs`) but only
helps a reader whose start resolves after the report lands: the first reader,
whose relative start waits on SUBSCRIBE_START. A later reader's window
resolves at once (`Window::new` in `model/subscription/window.rs`), and
`Holdings::back` counts the copy's cache.

- Reproduce first: the second-reader case above, as a `range_anchors.rs`
integration test, and the same in `js/net/src/integration.test.ts`.
- Decided 2026-11-19 (coordinator, maintainer away, merge granted), taking
the recommendation: on a received copy, count back over the cache only where
the copy holds every group from the counted one to its latest (received
contiguously since it went live), and otherwise leave the relative start
waiting, as a time start waits (`Start::Waiting`), until the range's
SUBSCRIBE_STARTED reports it. Rejected: always waiting on the report,
since an update that coalesces the reader's range into another one
(`fit` in `lite/subscriber/fit.rs`) gets no SUBSCRIBE_STARTED of its own and would hang.
- The waiting reader must still settle when no report can come: the copy
ends, or its feed goes idle.
38 changes: 0 additions & 38 deletions quest/m2/time-addressing/rust-relative-start.md

This file was deleted.

61 changes: 47 additions & 14 deletions rs/moq-net/src/lite/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3220,6 +3220,7 @@ mod tests {
finishing: None,
parked: false,
starts: Default::default(),
edges: Default::default(),
declared: None,
end: None,
wanted: None,
Expand Down Expand Up @@ -3381,6 +3382,7 @@ mod tests {
finishing: None,
parked: false,
starts: Default::default(),
edges: Default::default(),
declared: None,
end: None,
wanted: None,
Expand Down Expand Up @@ -3859,6 +3861,7 @@ mod tests {
finishing: None,
parked: false,
starts: Default::default(),
edges: Default::default(),
declared: None,
end: Some(lite::SubscribeFinish { group: 10, error: None }),
wanted: None,
Expand Down Expand Up @@ -3975,6 +3978,7 @@ mod tests {
finishing: None,
parked: false,
starts: [(0, Some(4))].into(),
edges: Default::default(),
declared: None,
end: None,
wanted: None,
Expand Down Expand Up @@ -4043,6 +4047,7 @@ mod tests {
finishing: None,
parked: false,
starts: [(0, Some(4))].into(),
edges: Default::default(),
declared: None,
end: Some(lite::SubscribeFinish { group: 10, error: None }),
wanted: None,
Expand Down Expand Up @@ -5564,6 +5569,7 @@ mod tests {
finishing: None,
parked: false,
starts: Default::default(),
edges: Default::default(),
declared: None,
end: None,
wanted: None,
Expand Down Expand Up @@ -10055,6 +10061,10 @@ struct SubStream {
/// Where SUBSCRIBE_START or SUBSCRIBE_STARTED says each live range's delivery starts, by
/// Range ID, `None` for one with nothing to deliver.
starts: std::collections::BTreeMap<u64, Option<u64>>,
/// The latest group each live range's start was counted back from, by Range ID: the
/// publisher's Largest when SUBSCRIBE_START answered, or the copy's latest when
/// SUBSCRIBE_STARTED did. Set only with its start, so an update keeping a range keeps it.
edges: std::collections::BTreeMap<u64, Option<u64>>,
/// What [`Self::declare`] last declared on the copy, so an unchanged one is not declared
/// again: each declaration restarts the grace of the gaps above it.
declared: Option<(Option<u64>, bool)>,
Expand Down Expand Up @@ -10097,23 +10107,32 @@ impl SubStream {
Some((start, self.started.is_none()))
}

/// Tell the copy where the publisher started each live range asked from a group's start,
/// which a reader's time start there resolves to when the copy holds no group at it.
/// Tell the copy where the publisher started each live range: one asked from a group's
/// start, which a reader's time start there resolves to when the copy holds no group at
/// it, and one counting back, which the copy, holding only part of the track, can't count.
/// Lite-07 only, where each range's start is reported.
fn report_time_starts(&self, serving: &track::Producer) {
fn report_starts(&self, serving: &track::Producer) {
if !self.live.reports_starts() {
return;
}
let starts = self
.live
.iter()
.filter_map(|(id, range)| match (range.start, self.starts.get(&id)) {
(Some(lite::Anchor::Absolute(time)), Some(Some(start))) if time.frame == 0 => {
Some((time.group, *start))
let mut times = Vec::new();
let mut relative = Vec::new();
for (id, range) in self.live.iter() {
let Some(Some(start)) = self.starts.get(&id) else {
continue;
};
match range.start {
Some(lite::Anchor::Absolute(time)) if time.frame == 0 => times.push((time.group, *start)),
Some(lite::Anchor::Relative(back)) => {
if let Some(Some(latest)) = self.edges.get(&id) {
relative.push((back, *start, *latest));
}
}
_ => None,
});
serving.time_starts(starts);
_ => {}
}
}
serving.time_starts(times);
serving.relative_starts(relative);
}

/// Declare on the copy where the live feed starts, by [`Self::feed_start`], so a reader
Expand Down Expand Up @@ -10244,6 +10263,7 @@ impl SubStream {
let changed = !self.live.iter().map(|(id, _)| id).eq(before);
let live = &self.live;
self.starts.retain(|id, _| live.get(*id).is_some());
self.edges.retain(|id, _| live.get(*id).is_some());
if self.started.is_some() {
self.fill_ids.retain(|id| live.get(*id).is_some());
}
Expand Down Expand Up @@ -11195,6 +11215,7 @@ impl Establish {
finishing: None,
parked: self.parked,
starts: Default::default(),
edges: Default::default(),
declared: None,
end: None,
wanted: None,
Expand Down Expand Up @@ -12440,11 +12461,14 @@ impl ServeLoop {
// fill's range starting past where it asked skips groups it will not deliver.
let now = serve.subscriber.runtime.now();
let mut served: Option<u64> = None;
// What each range's relative start counted back from.
let edge = start.largest.map(|largest| largest.position.group);
for (id, start) in (0..).zip(&start.starts) {
// One an update retired since owes nothing, though before lite-07 its start
// still says where the SUBSCRIBE's demand was served from.
if active.live.get(id).is_some() {
active.starts.insert(id, *start);
active.edges.insert(id, edge);
}
match active.fill_range(id) {
Some(groups) => {
Expand All @@ -12467,6 +12491,15 @@ impl ServeLoop {
}
}
active.started = Some(served);
// Before the copy goes live, since a reader's relative start resolves only then:
// it counts back from the publisher's Largest, as the publisher did, rather than
// from whichever group landed first.
active.report_starts(&self.serving);
if active.live.reports_starts()
&& let Some(largest) = start.largest
{
self.serving.report_edge(largest.position.group);
}
// Only a live fill's Range ID matters from here.
let live = &active.live;
active.fill_ids.retain(|id| live.get(*id).is_some());
Expand Down Expand Up @@ -12500,7 +12533,6 @@ impl ServeLoop {
// Lite-07 says where every live range starts, so the declaration follows them
// whatever an update changed since.
if active.live.reports_starts() {
active.report_time_starts(&self.serving);
active.declare(&mut self.serving);
} else if active.start == active.requested && active.demand.is_some() {
let _ = match (serve.subscriber.version.resolves_start(), served) {
Expand Down Expand Up @@ -12532,6 +12564,7 @@ impl ServeLoop {
// One retired since is owed nothing.
if active.live.get(id).is_some() {
active.starts.insert(id, started.start);
active.edges.insert(id, self.serving.latest_live());
let now = serve.subscriber.runtime.now();
// A fill's range starting past where it asked skips groups it will not deliver.
if let Some(groups) = active.fill_range(id) {
Expand All @@ -12545,7 +12578,7 @@ impl ServeLoop {
if let Ok(mut tail) = active.tail.write() {
tail.expire(now);
}
active.report_time_starts(&self.serving);
active.report_starts(&self.serving);
active.declare(&mut self.serving);
}
}
Expand Down
43 changes: 43 additions & 0 deletions rs/moq-net/src/model/resume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3536,6 +3536,49 @@ mod test {
drop(held);
}

/// A route capped while replaced and then returned to reads on to the end of the reader's
/// bounded range: the cap never becomes the range's own end. Mirrors the `@moq/net` "a
/// returning route keeps the reader's bounded end across its cap".
#[kio::test]
async fn a_returning_route_keeps_the_readers_bounded_end_across_its_cap() {
let routes = Producer::new();
let logical = logical(&routes);
let a = copy();
let mut open = a.create_group(group::Info { start: 0 }).unwrap();
open.write_frame(ts(0), b"x".as_ref()).unwrap();
routes.serve(a.consume());
let subscription = Subscription::default()
.with_delay(Delay::default().with_max(HOUR))
.with_ranges([track::Range {
start: Anchor::Time(0),
end: Some(Anchor::Time(100)),
}]);
let mut sub = logical
.consume()
.subscribe(subscription.clone())
.now_or_never()
.unwrap()
.unwrap();
let held = recv(&mut sub);
assert_eq!(held.start, 0);

let b = copy();
routes.serve(b.consume());
write(&b, 10);
write(&b, 20);
assert_eq!(drain(&mut sub, false), [10, 20]);

routes.serve(a.consume());
write(&a, 30);
assert_eq!(drain(&mut sub, false), [30], "past the cap A had while replaced");
// An update keeping the ranges asks A again with its own resolution of them.
sub.update(subscription.with_priority(1)).unwrap();
write(&a, 40);
assert_eq!(drain(&mut sub, false), [40], "an update keeps the reader's end");
open.finish().unwrap();
drop(held);
}

/// A returning route resumes where a fresh copy would, and what it buffered while
/// replaced below what another route already handed out comes through the backfill:
/// the late group once, and nothing handed out again.
Expand Down
Loading