Skip to content

fix(session-view): inline images refresh when their file is overwritten at the same path - #2035

Open
R44VC0RP wants to merge 2 commits into
devfrom
vogel/inline-image-stale-cache
Open

R44VC0RP wants to merge 2 commits into
devfrom
vogel/inline-image-stale-cache

Conversation

@R44VC0RP

@R44VC0RP R44VC0RP commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

An inline image in a reply shows the file's current content after the file is overwritten at the same path.

🔍 Origin: seen in real use. A reply showed a generated PNG inline; the assistant then overwrote the same file, and the next reply's inline image still drew the old pixels, while the tool thumbnail's zoom view and the Files panel showed the new file.

❌ Before

  1. MdStore cached local markdown images by (directory, path) for good, with nothing that invalidated a slot.
  2. Tool-result thumbnails are content-addressed and the Files panel reads fresh, so only the inline image was stale.

✅ After

  1. A slot is marked stale when the app hears the file may have changed: filesystem.changed / file.edited, a tool result that wrote the path (any mutating tool such as a shell marks the directory's images for a lazy recheck), a step's file list, a revert, a reconnect, and a session switch or reopen.
  2. A stale slot revalidates with If-None-Match against the server's /api/fs/read ETag: 304 keeps the decoded picture, 200 downloads the new bytes.
  3. The previous picture stays drawn until the new one is decoded, so there is no blank frame and the row keeps its rect.
  4. The slot's lifecycle is an explicit state machine (images/markdown/slot.rs).

Consequence: an older reply that references the same path also shows the current file, as the Files panel does. There is one file; old versions are not snapshotted.

⚠️ For the reviewer: this is a large change for a cache bug (12 files, about 1,170 lines added, of which 282 are a story). The parts worth reading closely are the slot machine in slot.rs, the invalidation call sites in apps/desktop/src/session/events, and the conditional GET in opencode-kit's client/http.rs.

Checks run
  • New story Timeline/Inline image update (variants: File changed, Bash tool, Reopen session), headless: before the fix the inline image stays on the old version beside a fresh tool thumbnail in all three; after it, all three update
  • Unchanged images: 0 extra fetches on a file change; 304 with no body on the Bash tool and Reopen variants
  • 60 fps capture of the swap: no blank frame, row rect unchanged
  • cargo check --locked -p session-view --examples: pass; clippy on opencode-kit and session-view: no new warnings; scripts/guardrails.sh: 11/11
  • Not run against a live server session; reproduced through the story's fixture

…anges and session reopen

What changed:
- Add conditional GET (If-None-Match / ETag -> 304 Not Modified) to
  opencode_kit::client::Server::get_bytes_if_modified, matching the server's
  /api/fs/read/* validators (packages/server/src/handlers/fs.ts:124-137).
- Replace MdStore's permanent path-keyed slot cache with an explicit slot
  state machine (crates/session-view/src/images/markdown/slot.rs: Fetching,
  FetchingDirty, Decoding, Held, Stale, Reloading, ReloadingDirty, Promoting,
  Failed) plus invalidate_file, invalidate_directory, invalidate_all_files,
  and set_conditional_fetch.
- Wire apps/desktop to use conditional fetches in boot.rs and invalidate
  affected local markdown images on filesystem.changed / file.edited,
  session.tool.success (exact paths for file-targeted tools, lazy
  ETag-preserving invalidation for arbitrary mutating tools like bash),
  session.step.ended file lists, session.revert.committed,
  Incoming::Reconnected, and session switches/reopens in open_now.
- Add the Timeline/Inline image update story covering file-change, bash-tool,
  and session-reopen updates.

Why and replaced behaviour:
- Previously, MdStore::slots cached local markdown images indefinitely by
  (directory, path) with no invalidation (even rest() kept stale bytes in
  Slot::Held), so overwriting an image at the same path left inline markdown
  images stale while content-addressed tool-result thumbnails, their lightbox,
  and the Files panel showed the new file.
- Replaced permanent path caching with lazy invalidation and If-None-Match
  revalidation that keeps the previous ready picture visible through
  Stale -> Reloading -> Promoting until the new decode is ready, preventing
  blank flashes or row-height shifts. Older replies referencing the same path
  also update to the current file on disk, matching the Files viewer.

Verified:
- Headless Timeline/Inline image update before fix reproduced stale inline V1
  beside fresh V2 tool thumbnail across File changed, Bash tool, and Reopen
  session variants.
- After fix, all three variants update inline images to V2; unchanged images
  stay cached (0 extra fetches on File changed, 304 Not Modified with 0 body
  downloads on Bash tool and Reopen session); 60 fps swap capture shows zero
  blank frames and identical row rect (delta-y = 0.0, delta-h = 0.0).
- scripts/guardrails.sh (11/11), cargo check --locked -p session-view --examples,
  cargo check --locked -p opencode-desktop --no-default-features --features knobs,
  and cargo clippy on opencode-kit / session-view all pass with no new warnings.
…lots, and refine invalidation triggers

Addresses PR #2035 review findings on top of 0711001:
- Keep Held and Stale entries revalidatable without flashing blank:
  introduce HeldBytes, StaleHeld, ReloadingHeld, and ReloadingHeldDirty in
  MdStore's slot state machine; rest() demotes all pixel-holding states to
  zero-pixel held equivalents (preserving bytes and ETags); invalidating Held
  revalidates silently via If-None-Match (no version bump until new pixels
  finish decoding, so 304 Not Modified causes zero downloads, zero decodes,
  and zero blank frames).
- Settle off-screen Promoting slots: add SlotEvent::Settle run from poll(),
  land(), trim_pinned(), and rest(), and make off-screen Promoting slots
  evictable under pixel/byte budgets so reloads landing while scrolled away
  or after a tab switch release previous pixels immediately.
- Add MdStore::revalidate_file (keeps ETag) and use it for read-only tool
  hints (read) so unchanged images answer 304 Not Modified instead of a full
  200 download.
- Remove unused file.edited match and handle session.shell.ended (user !cmd
  runs, packages/core/src/session/session.ts:232-240) and shell.exited
  (background shells) in apps/desktop/src/session/events/images.rs.
- Delegate path equality and directory containment to
  opencode_kit::path::{same,contains,windows} (moving source/path parsing
  into crates/session-view/src/images/markdown/source.rs) so case folding
  follows path shape regardless of client OS.
- Drop SlotEffect::Enqueue and eager URL-heuristic re-enqueue in land();
  dirty in-flight fetches settle lazily to None / Stale / StaleHeld.
- Avoid double MdStore::changed() call in set_conditional_fetch.
- Extend Timeline/Inline image update story with Held 304 and Off-screen
  reload variants.

Verified:
- All 5 story variants pass headlessly: File changed, Bash tool, Reopen
  session, Held 304 (chart_200=1 chart_304=1 unchanged_200=1 unchanged_304=1
  decoded_pics=0, zero blank frames across 60 fps held-304.mp4), and
  Off-screen reload (decoded_pics=2 after V2 lands off-screen at y=-759.0).
- 60 fps swap.mp4 still shows zero blank frames and identical row rect
  (delta-y = 0.0, delta-h = 0.0).
- scripts/guardrails.sh (11/11), cargo check --locked -p session-view
  --examples, cargo check --locked -p opencode-desktop --no-default-features
  --features knobs, and cargo clippy on session-view / opencode-desktop pass
  with zero warnings in touched files.

This branch has not been deployed

No deployments
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