Skip to content

fix(video): anchor the buffer frontier at the consumer fetch target - #223

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

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

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Follow-up to #207, reported and root-caused by @fxndxs.

Symptom

With the opt-in whole-source prefetch active, clock.bufferedPosition collapses to exactly the playhead a few minutes into a session and stays there, while ~15 min of content is resident on disk. A host drawing a buffer bar from it watches the bar shrink to zero and vanish. Playback is unaffected: nothing is actually missing, only the reported frontier is wrong.

Root cause

An anchor mismatch between the frontier walk and the eviction window, not a byte-accounting problem.

  • SegmentCache.contiguousForwardFrontier(from:) started the walk at the playhead's segment and returns startIdx - 1 when that entry is absent. contiguousForwardReadAheadSeconds then fails its frontier >= playheadIdx guard and returns 0.
  • pruneOutsideWindow anchors on the consumer's fetch target: lo = currentTargetIndex - backwardWindow. AVPlayer's fetch target runs ~120 s (~30 segments) ahead of the playhead, so the retained low end sits around playhead + 10 segments. The playhead's own segment is an evictable extra.

Only an opt-in prefetch reaches the retention budget, so only there does the extras eviction actually run. With the historical 10-segment window the whole footprint stayed under the 2 GiB budget, the extras below lo always survived, and the walk found its starting entry. That is why this surfaces now rather than being an old bug.

From the reporter's session: resident span ~[131, 371] with the playhead at segment ~120, so the walk started below the resident range on every tick.

Fix

contiguousForwardFrontier(fromPlayhead:) anchors at max(playhead, currentTargetIndex). Everything below the fetch target has been handed to the consumer and sits in its own buffer, which makes the anchor safe rather than merely optimistic: declareTarget is only ever called from the segment-serve path (VideoSegmentProvider.mediaSegment(at:) / mediaSegmentURL(at:)), never speculatively.

Two properties worth keeping:

  • Skipping the leading hole unbounded would be unsafe. Playing at 10 min with 10-25 min resident, seek back to 2 min: the producer restarts at 2 min while the old band survives, because hi = max(target + forwardWindow, highestStoredIndex) keeps it and nothing forces its eviction while the budget has room. A walk that just skips the hole lands on that stale band and reports ~25 min buffered when almost nothing is available, i.e. it fails in the dangerous direction. Anchoring at the new target stops the walk at the hole above the freshly produced segments.
  • max, not the target alone. A backward refetch behind the playhead (Continuous-Audio handover) must not drag the anchor back below it.

Anchor and walk share one lock hold. Reading targetIndex separately, then walking, would let a target change land between the two and walk from an anchor that does not match the entries observed.

Nothing changes for sessions that do not opt in, and the frontier still never trails the rendered frame (#54).

Contract

The published contract had drifted and is settled here. PlaybackClock.bufferedPosition documented the native path as "end of the contiguous loadedTimeRanges span", which #105 replaced with the disk frontier, while the engine-side comment described disk only. It is the end of the contiguous safe range ahead of the playhead: what the consumer already holds, plus what sits contiguously above it on disk. The anchor change produces exactly that, and it also removes the under-report in the first seconds before the cache has built.

Residual

Between a backward seek and the consumer's first fetch at the new position the declared target is still the old one, so a tick or two can report the stale band before declareTarget lands. Same class as the plan-rebuild race the function already documents, transient by construction rather than the permanent mis-report a hole-skipping walk would produce, and it self-corrects on the next fetch. Documented at the call site rather than papered over with a separate seek-state bound.

Verification

  • 7 new tests in Issue207FrontierAnchorTests: the eviction precondition, the collapse, the target-anchored band, the backward-seek trap, the backward refetch, the unchanged default window, and a fresh cache with no declared target.
  • Full suite: 1081 tests in 174 suites green.
  • swift build -Xswiftc -strict-concurrency=complete clean.
  • tvOS Simulator build green.

Device verification of the buffer bar under an opt-in prefetch session is still outstanding and is what #207 stays open for.

The reported buffer frontier collapsed to the playhead a few minutes into a
session with the opt-in whole-source prefetch active, so a host drawing a
buffer bar from clock.bufferedPosition watched it shrink to nothing while
~15 min of content was resident on disk.

The frontier walk anchored on the playhead's segment while pruneOutsideWindow
retains from currentTargetIndex - backwardWindow upward. AVPlayer's fetch
target runs ~120 s ahead of the playhead, so the playhead's own segment sits
below that low end and is an evictable extra. Only an opt-in prefetch reaches
the retention budget, so only there does the extras eviction actually run,
which is why this surfaces now rather than being an old bug: the walk starts
on a hole and contiguousForwardReadAheadSeconds fails its frontier >= playhead
guard on every tick.

contiguousForwardFrontier(fromPlayhead:) anchors at max(playhead, fetch
target) instead. Everything below the fetch target has been handed to the
consumer (declareTarget is only ever called from the segment-serve path), so
the walk reports the end of the contiguous safe range: what the consumer holds
plus the disk band above it. Skipping the leading hole without that bound
would instead land on the stale band left above a backward seek and report it
as buffered, failing in the dangerous direction; anchoring at the new target
stops the walk at the hole above the freshly produced segments. Anchor and
walk share one lock hold, so a target change cannot land between them.

Nothing changes for sessions that do not opt in: with the historical window
the whole footprint stays under the budget, the extras below the low end
survive, and the anchor is the playhead.

Also settles the published contract, which had drifted: PlaybackClock
documented bufferedPosition as the loadedTimeRanges span, which AE#105
replaced with the disk frontier. It is the end of the contiguous safe range
ahead of the playhead, which is what a buffer bar wants to express.

Reported and root-caused by fxndxs, including the backward-seek trap.

Co-Authored-By: findussoft <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01QYhPvBB1sLCYoZNasEXFeB
@superuser404notfound
superuser404notfound merged commit b25d730 into main Jul 26, 2026
3 checks passed
ijuniorfu pushed a commit to ijuniorfu/AetherEngine that referenced this pull request Jul 26, 2026
…sequence

The superuser404notfound#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 superuser404notfound#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 deleted the fix/issue-207-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