Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1ee4d0e
quest(m1): claim public-model/serve
kixelated Oct 21, 2026
007e502
refactor(net)!: public serving cursor and group expiry in Rust
kixelated Oct 21, 2026
1bea3f9
refactor(net)!: public serving cursor, Recv, and track info hold in JS
kixelated Oct 21, 2026
85fe38a
Merge remote-tracking branch 'origin/main' into worktree-agent-ae3811…
kixelated Oct 21, 2026
ee27a0d
feat(js/net): a public group expired promise replaces the serving hooks
kixelated Oct 21, 2026
9b9a1c1
fix(js/net): satisfy biome on the expiry race
kixelated Oct 21, 2026
ec1b588
Merge remote-tracking branch 'origin/main' into pr6138
kixelated Oct 22, 2026
08a0c34
fix(js/net): a lowered range re-offers cuts and never a closed reader
kixelated Oct 22, 2026
6d7bdf3
Merge remote-tracking branch 'origin/main' into pr6138
kixelated Oct 22, 2026
1f01599
Merge remote-tracking branch 'origin/main' into pr6138
kixelated Oct 22, 2026
5fa2d17
fix(net): give up a written group on closed errors as well as expiry
kixelated Oct 22, 2026
e339b5d
Merge remote-tracking branch 'origin/main' into quest/m1/public-model…
kixelated Oct 22, 2026
bde8ad9
fix(json): move the PR's snapshot and window tests onto main's API; p…
kixelated Oct 22, 2026
98a7430
Merge remote-tracking branch 'origin/main' into quest/m1/public-model…
kixelated Oct 23, 2026
09b8eee
fix(net): keep the IETF drain's pause answer from main, and format
kixelated Oct 23, 2026
eef615f
Merge origin/main into quest/m1/public-model/serve
kixelated Oct 23, 2026
115c106
Merge origin/main into quest/m1/public-model/serve
kixelated Oct 23, 2026
87eab68
refactor(net)!: keep the serving items internal, public with_range only
kixelated Oct 23, 2026
d63e08c
Merge origin/main into quest/m1/public-model/serve
kixelated Oct 23, 2026
70535be
fix(js/net): tryNextGroup abandons the frame helpers' passed group
kixelated Oct 23, 2026
cd9b5ee
Merge origin/main into quest/m1/public-model/serve
kixelated Oct 23, 2026
f62f146
quest(m1): finish api-audit/serve
kixelated Oct 23, 2026
370dbcb
fix(net): cap served groups at the end only, keeping a missing prefix…
kixelated Oct 24, 2026
d4bee30
docs(net): lift a reader's cap without dropping its floor in MIGRATE
kixelated Oct 24, 2026
5f0380f
Merge origin/main into quest/m1/public-model/serve
kixelated Oct 24, 2026
f8ffb84
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Oct 24, 2026
46ae4ed
test(net): close the installed manual clock as main's helpers now do
kixelated Oct 24, 2026
fbf2a1b
Merge origin/main into quest/m1/public-model/serve
kixelated Oct 24, 2026
acfba05
test(net): poll main's held-successor tests through the folded poll_e…
kixelated Oct 24, 2026
bffc248
style(net): format the adapted held-successor tests
kixelated Oct 24, 2026
1416fe1
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Oct 24, 2026
decadbe
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Oct 24, 2026
29af63d
Merge remote-tracking branch 'origin/main' into HEAD
kixelated Oct 24, 2026
c269942
fix(net): rename main's new set_groups uses to with_range
kixelated Oct 24, 2026
aa73e7a
Merge remote-tracking branch 'origin/main' into rq-6138
kixelated Oct 24, 2026
4869c70
Merge remote-tracking branch 'origin/main' into rq-6138
kixelated Oct 24, 2026
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
68 changes: 47 additions & 21 deletions js/flate/src/snapshot/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { Decoder as Flate } from "../codec.ts";
import { isDeflate } from "../compression.ts";
import type { Config as CodecConfig } from "./producer.ts";

// Group errors that lose only that group, matching the JSON snapshot consumer.
const GAPS: Moq.StreamCode[] = [
Moq.StreamCode.TooFarBehind,
Moq.StreamCode.Old,
Moq.StreamCode.Evicted,
Moq.StreamCode.GroupTooLarge,
];

/**
* Consumes an opaque value from a track, yielding each with the timestamp of its frame.
*
Expand All @@ -17,8 +25,8 @@ export class Consumer {
#track: Moq.Track.Ordered;
#decompress: boolean;

// The group the current window belongs to, so a boundary restarts it.
#group?: number;
// The group being read.
#group?: Moq.Group.Consumer;
// The DEFLATE window for the current group, present while decompressing. A snapshot group is
// normally one frame, but the window is per group either way.
#flate?: Flate;
Expand All @@ -44,9 +52,9 @@ export class Consumer {
/**
* Get the newest value, or `undefined` once the track ends.
*
* Every group is a complete value, so any older group is already superseded. Raising the read
* floor to the newest sequence before each read does two things: it discards a backlog instead
* of decoding every superseded value in turn, and it abandons a group a newer one has
* Every group is a complete value, so any older group is already superseded. Jumping to the
* newest group the reader holds before each read does two things: it discards a backlog
* instead of decoding every superseded value in turn, and it abandons a group a newer one has
* superseded rather than waiting out its close. A reader's latency therefore never grows with
* the queue, and never depends on a stale group's FIN arriving.
*/
Expand All @@ -57,35 +65,53 @@ export class Consumer {
async #read(skip: boolean): Promise<Timed<Uint8Array> | undefined> {
for (;;) {
if (skip) {
const latest = await this.#track.largest();
if (latest !== undefined) this.#track.setGroups({ start: { included: latest.group } });
// Jump to the newest group the reader already has; a newer one supersedes the rest.
// Taking groups, not the track's largest position, ignores datagrams (which share
// the sequence space) and stays inside any range the caller set on the reader.
// Mirrors the Rust consumer.
for (let group = this.#track.tryNextGroup(); group; group = this.#track.tryNextGroup()) {
this.#open(group);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}

if (!this.#group) {
const group = await this.#track.nextGroup();
if (!group) return undefined;
this.#open(group);
}
const group = this.#group;
if (!group) continue;

let next: Awaited<ReturnType<Moq.Track.Ordered["readFrame"]>>;
let next: Moq.Group.Frame | undefined;
try {
next = await this.#track.readFrame();
next = await group.readFrame();
} catch (err) {
// Falling behind a group's eviction window is recoverable: the next group carries a
// complete value of its own, so resync there rather than surfacing a partial read.
// Anything else is the track's terminal error, which every later read would throw
// again; swallowing it would spin here instead of telling the caller the
// subscription died.
if (!(err instanceof Moq.Error.TooFarBehind)) throw err;
// Losing one group (reset, or fallen behind its eviction window) is recoverable: the
// next group carries a complete value of its own, so resync there rather than
// surfacing a partial read. Anything else is the track's terminal error, which every
// later read would throw again; swallowing it would spin here instead of telling the
// caller the subscription died.
if (!(err instanceof Moq.Error.Stream && GAPS.includes(err.code))) throw err;
this.#group = undefined;
continue;
}

if (!next) return undefined;

// Each group is its own compressed stream, so a boundary starts a cold window.
if (next.group !== this.#group) {
this.#group = next.group;
this.#flate = this.#decompress ? new Flate() : undefined;
if (!next) {
this.#group = undefined;
continue;
}

return { value: this.#flate ? this.#flate.frame(next.payload) : next.payload, at: next.timestamp };
}
}

// Each group is its own compressed stream, so a boundary starts a cold window.
#open(group: Moq.Group.Consumer): void {
this.#group?.close();
this.#group = group;
this.#flate = this.#decompress ? new Flate() : undefined;
}

/** Iterate over every value in group order, as {@link next} yields them, until the track ends. */
async *[Symbol.asyncIterator](): AsyncIterator<Timed<Uint8Array>> {
for (;;) {
Expand Down
14 changes: 14 additions & 0 deletions js/flate/src/snapshot/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ test("latest lets a newer group preempt an open one", async () => {
track.close();
});

// Skipping to the head never widens the reader's range: a capped reader reads the newest value
// below its cap, and a datagram past every group does not strand it.
test("latest stays inside the reader's range", async () => {
const track = new Track.Producer("test");
const producer = new Producer({ track });
const subscriber = track.subscribe({ maxDelay: REPLAY_LATENCY }).withRange({ end: { excluded: 2 } });
const consumer = new Consumer({ track: subscriber });

for (let n = 0; n < 4; n++) producer.update({ value: bytes(n) });
track.appendDatagram(Time.Timestamp.now(), bytes(9));
expect((await consumer.latest())?.value).toEqual(bytes(1));
producer.finish();
});

test("an aborted track surfaces its error instead of spinning", async () => {
// Every read after an abort throws the same terminal error, so swallowing it as if it were a
// recoverable Lagged would loop forever on a rejected promise rather than telling the caller
Expand Down
18 changes: 9 additions & 9 deletions js/json/src/snapshot/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ export class Consumer<T> {
async #read(skip: boolean): Promise<Timed<T> | undefined> {
for (;;) {
if (skip) {
// A newer group supersedes everything the current one still holds: it restarts from a
// full snapshot, so switching mid-group loses nothing but stale state (mirrors the Rust
// consumer). Older groups only hold superseded state too, so raise the floor to the
// newest instead of replaying each one.
const latest = await this.#track.largest();
if (this.#group !== undefined && latest !== undefined && latest.group > this.#group.sequence) {
this.#group.close();
this.#group = undefined;
// Jump to the newest group the reader already has; a newer one supersedes the rest.
// Taking groups, not the track's largest position, ignores datagrams (which share
// the sequence space) and stays inside any range the caller set on the reader.
// Mirrors the Rust consumer. A newer group restarts from a full snapshot, so switching mid-group loses
// nothing but stale state.
for (let group = this.#track.tryNextGroup(); group; group = this.#track.tryNextGroup()) {
this.#group?.close();
this.#group = group;
this.#framesRead = 0;
}
if (!this.#group && latest !== undefined) this.#track.setGroups({ start: { included: latest.group } });
}

if (!this.#group) {
Expand Down
14 changes: 14 additions & 0 deletions js/json/src/snapshot/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ test("latest abandons a held group when a newer snapshot group rolls", async ()
expect(await consumer.latest()).toBeUndefined();
});

// Skipping to the head never widens the reader's range: a capped reader reads the newest state
// below its cap, and a datagram past every group does not strand it.
test("latest stays inside the reader's range", async () => {
const track = new Track.Producer("test");
const producer = new Producer<Value>({ track, deltas: false });
const subscriber = track.subscribe({ maxDelay: REPLAY_LATENCY }).withRange({ end: { excluded: 2 } });
const consumer = new Consumer<Value>({ track: subscriber });

for (let n = 0; n < 4; n++) producer.update({ value: { a: n } });
track.appendDatagram(Time.Timestamp.now(), new Uint8Array([0]));
expect((await consumer.latest())?.value).toEqual({ a: 1 });
producer.finish();
});

// `next()` finishes the held group before the newer one, so a playhead sees every state.
test("next reads a held group out before a newer snapshot group", async () => {
const track = new Track.Producer("test");
Expand Down
6 changes: 3 additions & 3 deletions js/json/src/window/window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ test("a lagging consumer is told what it missed", async () => {
producer.push({ value: { n } });
producer.pop(1);
}
subscriber.setGroups({ start: { included: (await subscriber.largest())?.group ?? 0 } });
subscriber.withRange({ start: { included: (await subscriber.largest())?.group ?? 0 } });
const events = await live.finish();

const skipped = events.flatMap((e) => ("skip" in e ? span(e.skip) : []));
Expand Down Expand Up @@ -424,7 +424,7 @@ test("a fresh consumer adopts the current offset", async () => {
producer.pop(3);

const subscriber = track.subscribe();
subscriber.setGroups({ start: { included: (await subscriber.largest())?.group ?? 0 } });
subscriber.withRange({ start: { included: (await subscriber.largest())?.group ?? 0 } });
const consumer = new Consumer<Rec>({ track: subscriber });
producer.finish();
const events: Event<Rec>[] = [];
Expand Down Expand Up @@ -594,7 +594,7 @@ test("events carry their frame's timestamp, and a header's restated records get
producer.cut();
producer.push({ value: { n: 3 }, at: at(400) });
producer.finish();
late.setGroups({ start: { included: 1 } });
late.withRange({ start: { included: 1 } });
const events: Timed<Event<Rec>>[] = [];
for await (const event of new Consumer<Rec>({ track: late })) events.push(event);
expect(events.map((event) => event.value)).toEqual([
Expand Down
6 changes: 3 additions & 3 deletions js/net/bench/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import { Milli } from "@moq/clock";
import { type Consumer as GroupConsumer, Producer as GroupProducer } from "../src/group.ts";
import { hooks } from "../src/internal.ts";
import { Timescale, Timestamp } from "../src/time.ts";
import { Producer, type Subscriber } from "../src/track.ts";
import { untilGivenUp } from "../src/util/given_up.ts";

const retainedCounts = [25, 100, 400, 1500];
const subscriberCounts = [1, 4, 16];
Expand Down Expand Up @@ -82,10 +82,10 @@ async function measure(
for (const consumer of received) {
if (hold.operation === "read") reads.push(consumer.readFrame());
else {
// The guard a publisher holds while a group stream waits for credit.
// The watch a publisher holds while a group stream waits for credit.
const opening = Promise.withResolvers<void>();
opened.push(opening.resolve);
reads.push(hooks.guardGroup(consumer, () => opening.promise));
reads.push(untilGivenUp(consumer, () => opening.promise));
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions js/net/src/flate/group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ test("synchronous writers store slices; plain readers share one charged inflated
expect(stored).toBeLessThan(payload.length);
const inflating = spyOn(Decoder.prototype, "frame");
try {
const slice = await hooks.readGroupFrame(raw, undefined, true);
const slice = await hooks.readGroupFrame(raw, true);
expect(slice?.inflated).toBe(payload.length);
expect(inflating).toHaveBeenCalledTimes(0);
slice?.complete();
const a = await first.readFrame();
const b = await second.readFrame();
expect(a?.payload).toBe(b?.payload);
Expand All @@ -46,9 +45,8 @@ test("received slices are forwarded byte-for-byte and exact declarations fail al
const slice = new Encoder().frame(payload);
hooks.writeGroupFrame(group, { payload: slice }, payload.length + 1);
group.close();
const raw = await hooks.readGroupFrame(group.mirror(), undefined, true);
const raw = await hooks.readGroupFrame(group.mirror(), true);
expect(raw?.frame.payload).toBe(slice);
raw?.complete();
const bad = group.mirror();
await expect(bad.readFrame()).rejects.toThrow(/declared size/);
expect(bad.closed.peek()).toBeInstanceOf(Error);
Expand Down
Loading
Loading