Repository navigation
fix(video): anchor the buffer frontier at the consumer fetch target - #223
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #207, reported and root-caused by @fxndxs.
Symptom
With the opt-in whole-source prefetch active,
clock.bufferedPositioncollapses 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 returnsstartIdx - 1when that entry is absent.contiguousForwardReadAheadSecondsthen fails itsfrontier >= playheadIdxguard and returns 0.pruneOutsideWindowanchors 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 aroundplayhead + 10segments. 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
loalways 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 atmax(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:declareTargetis only ever called from the segment-serve path (VideoSegmentProvider.mediaSegment(at:)/mediaSegmentURL(at:)), never speculatively.Two properties worth keeping:
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
targetIndexseparately, 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.bufferedPositiondocumented the native path as "end of the contiguousloadedTimeRangesspan", 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
declareTargetlands. 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
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.swift build -Xswiftc -strict-concurrency=completeclean.Device verification of the buffer bar under an opt-in prefetch session is still outstanding and is what #207 stays open for.