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
47 changes: 47 additions & 0 deletions js/net/src/ordered.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,3 +799,50 @@ test("an ordered read whose window ends past a group it passed as stale reports
expect(ordered.tryNextItem()).toBeUndefined();
expect(ordered.tryNextGroup()).toBeUndefined();
}));

// A time start before every group held waits on its history lookup, and the group the lookup finds
// comes before the later one held. Ports the Rust
// `waiting_time_start::a_looked_up_time_start_reads_before_a_held_later_group`.
test("a looked-up time start reads before a held later group", async () => {
const producer = new TrackProducer("test").accept({ maxAge: null });
const requests = producer.requests();
put(producer, 66n, false);
const ordered = producer
.subscribe(withMax({ delay: { max: null }, ranges: [{ start: 10n, end: 20n }, { start: 66n }] }))
.ordered();
expect(items(ordered)).toEqual([]);
const fill = await requests.requestedFill();
const group = fill?.accept(10n);
group?.writeFrame(textFrame("x", Timestamp.unscaled(10n)));
group?.finish();
expect(items(ordered)).toEqual(["group 10"]);
requests.close();
});

// A stale live group waits behind a time start still looked up, which keeps its own budget: the read
// never passes the group the lookup finds.
test("a stale group waits behind a looked-up time start", async () => {
using clock = manual();
const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI, maxAge: null });
const requests = producer.requests();
put(producer, 66n);
clock.jump(Duration.secs(5));
put(producer, 5000n);
clock.jump(Duration.secs(2));
const ordered = producer
.subscribe({
ranges: [
{ start: 10n, end: 20n },
{ start: { live: { min: Duration.millis(4934), max: Duration.secs(1) } } },
],
})
.ordered();
expect(items(ordered)).toEqual([]);
const fill = await requests.requestedFill();
expect(fill?.start).toBe(10n);
const group = fill?.accept(10n);
group?.writeFrame(textFrame("x", Timestamp.fromMillis(10)));
group?.finish();
expect(items(ordered)[0]).toBe("group 10");
requests.close();
});
8 changes: 5 additions & 3 deletions js/net/src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6492,7 +6492,7 @@ export class Subscriber {
return runs;
}

// In order, the group a waiting time start resolves to comes first: it starts at or before that
// In order, the group an unresolved time start resolves to comes first: it starts at or before that
// time, below `key`, and may still land at or past the read's floor `start`, until the budget
// gives up on it as on an owed group. Its landing, the answer placing the start, or that verdict
// wakes the read; a closed track places the start.
Expand All @@ -6501,7 +6501,7 @@ export class Subscriber {
const producer = this.#state.producer;
let waits = false;
const coverage = this.#coverage();
for (const time of this.#window.waitingIn(start, key)) {
for (const time of this.#window.unresolvedIn(start, key)) {
if (producer && wireFed(producer)) {
// With every key from the floor through the time accounted for, a group still to come
// containing it lies below the floor, which the read never hands out.
Expand Down Expand Up @@ -6667,7 +6667,9 @@ export class Subscriber {
// scanning: one pass walks a whole backlog off rather than replaying it.
if (this.#staleAt(group.start, drift)) {
// Passing it passes the owed keys below it too, which the budget gives up with it, but
// for one a wider budget still holds.
// for one a wider budget still holds, and for the group a time start below it resolves
// to, judged by its own budget.
if (this.#waitTimeStart(start, group.start, drift)) return { kind: "idle" };
if (this.#waitWider(start, group.start, drift, drift.budget.at(group.start))) return { kind: "idle" };
this.#passOwed(start, group.start, drift);
this.#giveUp({ start: group.start, end: group.start + 1n }, staleHole(drift.budget.at(group.start)));
Expand Down
17 changes: 11 additions & 6 deletions js/net/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,13 +954,15 @@ export class Window {
}

/**
* The times in `[start, end)` of the time starts still waiting on the groups held. The group
* containing each starts at or before its time, so one at or past a read's floor and below a
* group may still land between them. Mirrors the Rust `Window::waiting_in`.
* The times in `[start, end)` of the time starts not yet resolved: waiting on the groups held, or
* on a lookup of the track's history. The group containing each starts at or before its time, so
* one at or past a read's floor and below a group may still land between them. Mirrors the Rust
* `Window::unresolved_in`.
*/
waitingIn(start: bigint, end: bigint): bigint[] {
unresolvedIn(start: bigint, end: bigint): bigint[] {
const times: bigint[] = [];
for (const entry of this.#waiting.in(start, end)) times.push(entry.time);
for (const entry of this.#looking.in(start, end)) times.push(entry.time);
return times;
}

Expand Down Expand Up @@ -988,9 +990,12 @@ export class Window {
return this.#groups.prev(before);
}

/** The lowest group in range, undefined while a start waits. Nothing in range floors at group 0. */
/**
* The lowest group in range, undefined while a start waits, a time start's lookup included: it may
* resolve to a group below every one in range. Nothing in range floors at group 0.
*/
floor(): bigint | undefined {
if (this.pending || this.#unplaced()) return undefined;
if (this.pending || this.#unplaced() || this.#looking.size > 0) return undefined;
return this.#groups.lowest() ?? 0n;
}

Expand Down
71 changes: 65 additions & 6 deletions quest/m1/relayed-refused-fill-gap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [S] A relayed reader reports a refused fill as a gap a direct reader never sees
# [L] A relayed reader reports a refused fill as a gap a direct reader never sees

## Goal

Expand All @@ -17,8 +17,67 @@ plans applied: `waiting_time_start::a_range_before_every_group_reads_past_it` in
`rs/moq-net/tests/integration/` fails there, the relayed reader reporting a NotFound gap over
`10..21` where the direct one reports none.

The copy asks upstream to fill the range before the only group; upstream refuses it, since no
group exists there, and the copy records the refusal as a gap. A range that resolves empty at the
publisher is no loss: the copy must answer it the same way, per the cache rule in `AGENTS.md`.
Start by checking how the refusal reaches `find_gap` on the copy, and whether lite-07's
SUBSCRIBE_DROP or the fill's refusal carries it. Check JS's twin of the test too.
[moq-dev/moq#9639](https://github.com/moq-dev/moq/pull/9639) traced it. The relayed reader is not
wrong: the gap is the publisher's own answer. The two readers differ in what they ask the
publisher's history handler (`track::Requests`):

- A copy whose window covers `Time(10)..20` asks upstream for keys `10..21` with a range fill. The
test's handler drops it unanswered, which records a `NotFound` miss on the publisher's own track
(`fill::Inner::drop`), so the copy reports `Err(10..21)`. A direct reader opened at the publisher
after that miss, asking only `Time(10)..20`, reports the same gap.
- A direct Rust reader at the publisher looks up only the group containing time 10, and never asks
the handler for the rest of the range: a local track owes no key between groups
(`TrackState::owed`). With a handler holding groups 10 and 15, it reads `[10, 66]`, while the
relayed reader reads `[10, 15, Err(16..21), 66]`.
- A direct JS reader does ask the handler for the rest (a fill over `11..21`), and reads
`[10, Err(11..15), 15, Err(16..21), 66]`.

[moq-dev/moq#9639](https://github.com/moq-dev/moq/pull/9639) fixed the separate bug that hid this:
a direct ordered reader handed out group 66 before its time start's lookup answered, then never
returned the group the lookup found.

Decided 2026-11-25 (maintainer, /quest-run): a local Rust reader whose time range resolves through
the history handler asks it for the keys with no group held, as copies and JS already do, narrowed
to ranges whose start a history lookup placed (`Window::found` with `Found::Group` or
`Found::Dropped`) (rejected: ask nothing, unanswered means empty)
([answer](https://github.com/moq-dev/moq/pull/9639#issuecomment-7177793296)).

Decided 2026-11-25 (maintainer, /quest-run): once a history lookup comes back empty from key 0, no
group starts at or before the time, so the range starts one past the time, in Rust
(`Window::found`) and JS (`Window.found`): the time's own key is out of range everywhere, and the
lookup's own NotFound drop of it is no gap (rejected: include the key everywhere). This changes the
deliberate `a_time_start_the_groups_cant_place_is_looked_up` and
`a_time_start_before_the_cache_asks_nothing_below_it`.

Decided 2026-11-25 (maintainer, /quest-run): a publisher answers a range with a start, never "no
group to deliver", whenever it dropped keys in it, so a copy waits for those drops rather than
handing out a later group first (rejected: the copy waits on every key of a range answered empty).
Built on the lite-07 Previous Delta wire, which changes how a copy learns a range is empty.

What a broad first attempt found, on top of [moq-dev/moq#9639](https://github.com/moq-dev/moq/pull/9639):

- Making `TrackState::unfed` treat every hole on a local track below the first group the producer
created (`arrivals.first()`) as `never`, so `Misses::judge` asks it now rather than after the miss
grace, makes a history handler holding groups 10 and 15 read the same through direct, relayed,
and JS readers: `[10, Err(11..15), 15, Err(16..21), 66]`. It breaks 23 tests of the deliberate
grace for local holes, such as `a_hole_below_an_undeclared_start_is_asked_after_a_bounded_grace`
and `creating_a_group_past_a_gap_skips_nothing`, which is why the ask is narrowed: thread the runs
of lookup-placed spans from `Window` through `Cursor::probe` and `Misses::judge` into `unfed`.
- With that ask, a relay subscribing after the publisher already recorded the drops hung: the
publisher answered the range "no group to deliver", and the copy placed the time before every
group (`TimeStart::containing` gives `Containing::Before`), reading back to key 0 and waiting on
keys no upstream accounts for. The publisher-side answer above removes that case.
- Gaps split by when their drops land, so the publisher reports `Err(10..11)` then `Err(11..21)`
where a relay gets one drop over `10..21`. Compare readers with adjacent gaps joined, and take
the excluded time key above into account.

Then `a_range_before_every_group_reads_past_it` asserts the same items for both readers through
the later group, before the front-drop fix lands.

## Waiting

- moq-dev/moq: [moq-dev/moq#9626](https://github.com/moq-dev/moq/pull/9626) (lite-07 Previous Delta) lands, since the publisher's answer for a range with dropped keys is built on its wire; check that the PR is merged.

## Related

- [Previous Delta](/quest/m2/lite07-subscribe/previous-delta.md) - the wire this work builds on, in [moq-dev/moq#9626](https://github.com/moq-dev/moq/pull/9626).
29 changes: 18 additions & 11 deletions rs/moq-net/src/model/subscription/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,19 +710,25 @@ impl Window {
self.waiting.last().is_some_and(|&(time, _)| time > floor)
}

/// The times in `times` of the time starts still waiting on the groups held. The group
/// containing each starts at or before its time, so one at or past a read's floor and
/// below a group may still land between them.
pub fn waiting_in<R: ops::RangeBounds<u64> + 'static>(&self, times: R) -> impl Iterator<Item = u64> + '_ {
/// The times in `times` of the time starts not yet resolved: waiting on the groups held,
/// or on a lookup of the track's history. The group containing each starts at or before
/// its time, so one at or past a read's floor and below a group may still land between
/// them. Unsorted, and a time two spans share comes twice.
pub fn unresolved_in<R: ops::RangeBounds<u64> + Clone + 'static>(
&self,
times: R,
) -> impl Iterator<Item = u64> + '_ {
let from = match times.start_bound() {
ops::Bound::Included(&start) => start,
ops::Bound::Excluded(&start) => start.saturating_add(1),
ops::Bound::Unbounded => 0,
};
self.waiting
.range((from, 0)..)
.map(|&(time, _)| time)
.take_while(move |time| times.contains(time))
let looked = times.clone();
let waiting =
(self.waiting.range((from, 0)..).map(|&(time, _)| time)).take_while(move |time| times.contains(time));
let looking =
(self.looking.range((from, 0)..).map(|&(time, _)| time)).take_while(move |time| looked.contains(time));
waiting.chain(looking)
}

/// Resolve what the groups now held settle: live joins once `latest` exists or an
Expand Down Expand Up @@ -815,10 +821,11 @@ impl Window {
self.groups.contains(key)
}

/// The lowest group in range, `None` while an anchor waits. Nothing in range floors
/// nowhere: group 0.
/// The lowest group in range, `None` while an anchor waits, a time start's lookup included:
/// it may resolve to a group below every one in range. Nothing in range floors nowhere:
/// group 0.
pub fn floor(&self) -> Option<u64> {
match self.is_pending() || self.unplaced() {
match self.is_pending() || self.unplaced() || !self.looking.is_empty() {
true => None,
false => Some(self.groups.lowest().unwrap_or(0)),
}
Expand Down
13 changes: 7 additions & 6 deletions rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11082,14 +11082,15 @@ impl Cursor {
})
}

/// Pending while a time start in `times` waits to be placed and the group containing it,
/// at or before its time and so below `times.end`, may still land at or past the read's
/// floor `times.start` within the drift budget. Its landing, the answer placing the start,
/// Pending while a time start in `times` waits to be placed, on the groups held or a lookup
/// of the track's history, and the group containing it, at or before its time and so below
/// `times.end`, may still land at or past the read's floor `times.start` within the drift
/// budget. Its landing, the answer placing the start,
/// the grace for a gap below it folding, or the budget giving up on it wakes us; a closed
/// track places it. `Ready(true)` when a change placed one meanwhile, so the caller looks
/// again.
fn poll_time_start(&mut self, times: ops::Range<u64>, drift: &Drift, waiter: &kio::Waiter) -> Poll<bool> {
if self.filter.window.is_settled() || self.filter.window.waiting_in(times.clone()).next().is_none() {
if self.filter.window.is_settled() || self.filter.window.unresolved_in(times.clone()).next().is_none() {
return Poll::Ready(false);
}
let window = &self.filter.window;
Expand All @@ -11100,7 +11101,7 @@ impl Cursor {
// An unjudgeable old gap cannot hold a finite read, as for an owed group.
let floor = times.start.max(drift.budget.floor(state.live_floor));
let grace = state.arrival_grace();
for time in window.waiting_in(floor..times.end) {
for time in window.unresolved_in(floor..times.end) {
// With every key from the floor through the time accounted for, a group still
// to come containing it lies below the floor, which the read never hands out.
let unaccounted = match state.anchors.received() {
Expand Down Expand Up @@ -11135,7 +11136,7 @@ impl Cursor {
let window = &self.filter.window;
if waited
.iter()
.any(|&time| window.waiting_in(time..=time).next().is_none())
.any(|&time| window.unresolved_in(time..=time).next().is_none())
{
return Poll::Ready(true);
}
Expand Down
41 changes: 41 additions & 0 deletions rs/moq-net/tests/integration/waiting_time_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,47 @@ async fn a_range_before_every_group_reads_past_it() {
fixture.serving.abort();
}

/// An ordered reader at the publisher waits for the history lookup of a time start before
/// every group held, and reads the group the lookup finds before the later one held.
#[kio::test]
async fn a_looked_up_time_start_reads_before_a_held_later_group() {
let publisher = origin(1);
let broadcast = publisher
.route("bcast", moq_net::origin::RouteOptions::hidden())
.unwrap();
let track = broadcast
.broadcast()
.create_track("value", moq_net::track::Info::default().with_max_age(None))
.unwrap();
let requests = track.requests();
let serving = kio::clock::sim::spawn(async move {
while let Ok(fill) = requests.requested_fill().await {
if (fill.start().group..fill.end()).contains(&10)
&& let Ok(Some(group)) = fill.accept(10, None)
{
write(group);
}
}
});
write(track.create_group(moq_net::group::Info { start: LIVE }).unwrap());
let subscription = Subscription::default()
.with_max(None)
.with_order(Order::Ascending)
.with_ranges([
Range {
start: moq_net::track::Start::Time(10),
end: Some(20),
},
Range {
start: moq_net::track::Start::Time(LIVE),
end: None,
},
]);
let mut reader = track.subscribe(subscription).ordered();
assert_eq!(read(&mut reader, 1).await, [Ok(10)]);
serving.abort();
}

/// A copy holding the groups on either side of a time, from fetches, says nothing of a group
/// between them: a reader asking from that time starts where the publisher would.
#[kio::test]
Expand Down