Skip to content

fix(video): bound the buffer frontier anchor to the consumer's fetch sequence - #226

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/issue-207-post-seek-frontier-anchor
Jul 26, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/issue-207-post-seek-frontier-anchor

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Follow-up to #223, from the field report on 5.23.4 in #207.

The report

With the opt-in prefetch active, a far seek made clock.bufferedPosition claim a lead the size of the seek distance for one tick: jumping out of the resident band and then forward to segment 729, the tick taken while the clock still read the old position showed ~35 min of lead over a playhead with nothing resident ahead of it. It corrected as soon as the clock landed.

Root cause

The #223 anchor is max(playhead, consumer fetch target), and its safety rests on everything below the target sitting in the consumer's own buffer. That is true only inside an uninterrupted fetch sequence: AVPlayer fetches no further ahead than its buffer reaches, which is what keeps target - playhead bounded during normal playback (~30 segments, ~120 s).

A seek removes that bound. declareTarget lands at the destination before currentTime() reports it (AVPlayer needs the data to seek), so for the ticks in between the walk starts at the destination, runs through the freshly produced band, and measures it against the position the seek jumped away from.

Worth noting for the reading of the report: the quoted fwd=2089.6 is AVPlayer's own loadedTimeRanges value, not bufferedPosition; AVPlayer makes the same axis error during the seek. The engine-side number lands in the same magnitude for the reason above.

The change

SegmentCache tracks the current fetch sequence (fetchFloor, fetchFront), and the anchor falls back to the playhead outside it:

let anchor = playheadIdx >= fetchFloor ? max(playheadIdx, currentTargetIndex) : playheadIdx

A jump is an index above fetchFront + 1 or more than backwardWindow below the front. The second clause is load-bearing: the Continuous-Audio handover refetches ~7-10 segments backward and returns to the fetch front, and reading that as a seek would re-open the #207 collapse this anchor exists to prevent (the guard test covers it, and it fails against the naive "any non-sequential index ends the sequence" rule).

Ending the sequence on a backward seek is what lets a later forward seek that lands below the pre-seek front still be recognised as a jump.

The four #223 invariants are unchanged: no unbounded hole skip, max still keeps a backward refetch from dragging the anchor back, anchor and walk still share one lock hold, and the caller's guard still measures against the playhead (#54 clamp).

Residual

A scrub shorter than the backward window stays inside the sequence, so a tick or two around it can still anchor on the old target. The error is bounded by that window instead of by the seek distance, and it is documented at the call site.

Verification

  • 4 new tests in Issue207FrontierAnchorTests (forward-seek claim with the old band resident and with it evicted, forward seek below the pre-seek front, handover guard), each watched failing first
  • Issue207FrontierAnchorTests also now models the consumer's sequential fetches instead of a single declared target, which is what the anchor's premise is about
  • 1100 tests green (baseline 1096)
  • swift build -Xswiftc -strict-concurrency=complete clean
  • tvOS Simulator build green

Analysis and device logs by @fxndxs, credited as co-author.

…sequence

The #223 anchor (max(playhead, consumer fetch target)) rests on everything below
the target sitting in the consumer's buffer. That holds only inside an
uninterrupted fetch sequence: AVPlayer fetches no further ahead than its own
buffer reaches, which is what bounds target - playhead during normal playback. A
seek removes the bound, and since declareTarget lands at the destination before
currentTime() reports it, the walk measured the freshly produced band there
against the position the seek jumped away from and reported a lead the size of
the seek distance for one tick (field report on 5.23.4, ~35 min claimed over a
playhead with nothing resident ahead of it).

SegmentCache now tracks the current sequence (floor plus front) and the anchor
falls back to the playhead outside it, which reports what is genuinely reachable
from there or nothing when its own segment is gone. A jump is an index above
front + 1 or more than backwardWindow below the front; the Continuous-Audio
handover refetch and its return to the front stay inside the sequence, since
treating those as a seek would re-open the #207 collapse the anchor exists to
prevent.

4 new tests: the forward-seek claim with the old band resident and evicted, a
forward seek below the pre-seek front (which needs the backward seek to end the
sequence too), and the handover guard. 1100 green, strict-concurrency clean.

Co-Authored-By: findussoft <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@superuser404notfound
superuser404notfound merged commit f162616 into main Jul 26, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue-207-post-seek-frontier-anchor branch July 27, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant