Skip to content

Keep viewer images on screen; share one image cache - #159

Merged
kommander merged 1 commit into
devfrom
fix/viewer-image-cache
Nov 18, 2026
Merged

kommander merged 1 commit into
devfrom
fix/viewer-image-cache

Conversation

@kommander

@kommander kommander commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Revisiting a file image in the full-screen viewer (paging back, reopening, after a resume) showed Opening/Downloading or a spinner and re-read the original. Offline, pages that needed a check failed.

  • AgentImageCache: one identity per image. A validated file stays fresh, with no timer, until AgentFileChecks.invalidate (open/resume/run state/settled reply). Decodes coalesce; disk copies are shared with transcript previews. Disk copies are trimmed oldest-first to keep the app cache within StorageManager.getCacheQuotaBytes; with no quota, nothing is trimmed.
  • The viewer shows resident pixels or the disk copy first and checks behind them. With validators the check is a single conditional GET (no range probe): 304 keeps the pixels, 200 replaces them, a failure keeps them, 404 fails. Video and audio still probe for streaming.
  • Only the selection and its ±1 neighbours hold pixels. Neighbours decode ahead from disk, and a page opens on its preview at full size until the full image decodes.

Port of 3c21cbb3, e81d7472, 773cbcd3, 36dbc563 without Session Media or debug tracing.

Verified: full assemble, lint and unit tests (only AttachmentCameraLensTests fails locally, because of the en_DE locale; untouched). 9 new MediaReferenceTests (the disk test injects the quota); 5 fail with their fix reverted. Emulator against session-lab images (10-file gallery): after a resume, revisiting every page sent exactly one 304 per file, with no 206, no 200 and no spinner. Close/reopen and offline: 0 reads, no blank pages.

@kommander
kommander force-pushed the fix/viewer-image-cache branch 2 times, most recently from d7c3d3f to 15f4eed Compare November 18, 2026 23:09
…e transcript

The full-screen viewer showed Opening/Downloading or the pager spinner
when you came back to a file image (paging back, reopening, after a
resume) and re-read the original each visit. Offline, every page that
needed a check failed.

- AgentImageCache (new dependency): one identity per image (server +
  absolute path, or media:<hash>). A validated file stays fresh, with no
  timer, until AgentFileChecks.invalidate (session open or resume, run
  state change, settled reply) bumps the server's epoch. A fresh file
  opens with no request or decode. Decoded pixels stay in the shared
  memory budget. After each preview write, disk copies are trimmed
  oldest-first until the whole app cache fits
  StorageManager.getCacheQuotaBytes (read off the main thread). With no
  quota (error or <= 0) nothing is trimmed and Android cleans up.
  Downloads still being written are skipped.
  Decodes coalesce and downloads to one destination run one at a time.
- AgentFileViewerModel shows resident pixels or the decoded disk copy
  first and checks behind them. With a disk copy that has validators, the
  check is a single conditional GET (no range probe): a 304 keeps the
  pixels, a 200 replaces them, a failed check keeps them, a 404 fails.
  Video and audio still probe first, for streaming.
- FullscreenViewer: only the selection and its ±1 neighbours hold
  full-size pixels. Neighbours are decoded ahead from disk copies or
  inline bytes (NeighbourPrepared, no network). A file page with no
  full-resolution pixels opens on its transcript preview at the file's
  size until the full image decodes (placeholderSize), so zoom and fit
  don't change when the full image arrives.
- AgentImagePreviews writes its bytes and validators to the viewer's disk
  copy. A preview whose pixels were dropped reads that copy back (as is
  while fresh, confirmed by a conditional GET, or as is when the server
  can't be reached), not the whole original. A local read never extends
  freshness.

Ported from debug/device-swipe-trace 3c21cbb3, e81d7472, 773cbcd3 and
36dbc563, without Session Media and without the ViewerTrace/ImageTrace/
SwipeTrace diagnostics, onto dev. Decoded pixels live in dev's
Coil-backed DecodedImageCache, so the two cache tests use a MemoryCache
with weak references off. Review fixes on top: a disk read is capped at
MAX_BYTES, trim skips files still being written, and a changed file's
decode never joins one of its previous copy.

Tests: 9 new MediaReferenceTests. aStaleCopyIsCheckedByOneConditionalGet...
fails on the probe-first path (2 requests, not 1). With their fix reverted in place,
revisitedFileImagePagesComeBack..., selectingAPageDecodesItsNeighbours...,
aFileImageOpensOnItsSharedPreview... and aFileThatNeedsACheck... fail.
The other 4 cover the new cache. ComposerPhotoViewerTests now expects the
neighbour decoded ahead (parity row updated).

Verified:
- ./gradlew --continue assembleDebug lintDebug testDebugUnitTest on dev:
  all modules pass. The only failure is AttachmentCameraLensTests, which expects
  "1.6×" and gets "1,6×" from this machine's en_DE locale. This change
  doesn't touch it.
- Emulator (Medium_Phone_API_35, read-only, port 5640) against
  session-lab's images checkpoint (11-page transcript gallery). Opened,
  swiped forward and back across all pages, closed and reopened, resumed,
  then went offline (airplane mode) after a resume and after a cold
  start. No spinner or blank page on any revisit. Reopening draws the
  image at once with 0 file reads. After a resume, revisiting all 10 cached files
  over 30 swipes sent exactly one conditional GET per file (10 x 304): no
  206, no 200, no spinner. Offline: the viewer and transcript images come from disk copies
  with 0 reads.
@kommander
kommander force-pushed the fix/viewer-image-cache branch from 15f4eed to 1c51f07 Compare November 18, 2026 23:09
@kommander
kommander merged commit 2ad1b37 into dev Nov 18, 2026
2 checks passed
@kommander
kommander deleted the fix/viewer-image-cache branch November 18, 2026 23:56
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