Skip to content

The agent that grows with you

fix(webapp): reuse a clip's stream URL when its transcript row remounts

Tekniumc45eca76270b
diff --git a/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx b/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx
new file mode 100644
index 00000000000..727d1eb9e57
--- /dev/null
+++ b/apps/desktop/src/hooks/use-media-playback-src.remount.test.tsx
@@ -0,0 +1,51 @@
+import { fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import type * as Media from '@/lib/media'
+import { resolveMediaPlaybackSrc } from '@/lib/media'
+
+import { useMediaPlaybackSrc } from './use-media-playback-src'
+
+// Transcript rows remount routinely (a finished turn re-renders its markdown,
+// a reconnect re-resumes the chat). A new stream ticket per mount made the
+// browser fetch the clip again under a new URL, so a remount reuses the URL.
+
+vi.mock('@/lib/media', async importOriginal => ({
+ ...(await importOriginal<typeof Media>()),
+ resolveMediaPlaybackSrc: vi.fn()
+}))
+
+const resolve = vi.mocked(resolveMediaPlaybackSrc)
+
+function Player() {
+ const { onError, src } = useMediaPlaybackSrc('/srv/clip.mp4')
+
+ return src ? <video data-testid="video" onError={onError} preload="metadata" src={src} /> : null
+}
+
+const src = () => screen.getByTestId('video').getAttribute('src')
+
+beforeEach(() => {
+ let minted = 0
+ resolve.mockImplementation(async () => `http://127.0.0.1/api/files/stream?ticket=t${++minted}`)
+})
+
+afterEach(() => {
+ resolve.mockReset()
+})
+
+it('a remounted clip keeps its stream URL until that URL fails', async () => {
+ const first = render(<Player />)
+ await waitFor(() => expect(src()).toMatch(/ticket=t1$/))
+ first.unmount()
+
+ const second = render(<Player />)
+ expect(src()).toMatch(/ticket=t1$/)
+ expect(resolve).toHaveBeenCalledTimes(1)
+
+ // A source that fails before it ever loaded is a real failure: the next mount resolves afresh.
+ fireEvent.error(screen.getByTestId('video'))
+ second.unmount()
+ render(<Player />)
+ await waitFor(() => expect(src()).toMatch(/ticket=t2$/))
+})
diff --git a/apps/desktop/src/hooks/use-media-playback-src.ts b/apps/desktop/src/hooks/use-media-playback-src.ts
index 1db092ec94e..0dee78a7a1a 100644
--- a/apps/desktop/src/hooks/use-media-playback-src.ts
+++ b/apps/desktop/src/hooks/use-media-playback-src.ts
@@ -4,6 +4,8 @@ import { type SyntheticEvent, useCallback, useEffect, useMemo, useRef, useState
import { useComposerScope } from '@/app/chat/composer/scope'
import { useSessionView } from '@/app/chat/session-view'
import { mediaKind, resolveMediaPlaybackSrc } from '@/lib/media'
+import { cachedPlaybackSrc, forgetPlaybackSrc, rememberPlaybackSrc } from '@/lib/playback-source-cache'
+import { $connection } from '@/store/session'
type PlaybackEvent = SyntheticEvent<HTMLAudioElement | HTMLVideoElement>
@@ -39,7 +41,18 @@ export function useMediaPlaybackSrc(path: string) {
const runtimeId = useStore(view.$runtimeId)
const sessionId = storedId || runtimeId || undefined
- const [media, setMedia] = useState({ failed: false, path, src: '' })
+ // A remount reuses the URL an earlier mount resolved: the browser keeps what it fetched, and the
+ // clip's remembered position (keyed by URL) still applies.
+ const conn = useStore($connection)
+
+ const cacheKey = JSON.stringify([
+ path,
+ owner?.connectionId ?? conn?.connectionId ?? '',
+ owner?.profile ?? conn?.profile ?? '',
+ sessionId ?? ''
+ ])
+
+ const [media, setMedia] = useState(() => ({ failed: false, path, src: cachedPlaybackSrc(cacheKey) ?? '' }))
const loadedSrc = useRef('')
// Minted for a source that failed before it ever loaded; it gets no retry of its own.
const retriedSrc = useRef('')
@@ -48,10 +61,13 @@ export function useMediaPlaybackSrc(path: string) {
useEffect(() => {
let cancelled = false
let objectUrl = ''
+ const cached = kind === 'file' ? undefined : cachedPlaybackSrc(cacheKey)
- setMedia({ failed: kind === 'file', path, src: '' })
+ setMedia(current =>
+ cached && current.path === path && current.src === cached ? current : { failed: kind === 'file', path, src: cached ?? '' }
+ )
- if (kind === 'file') {
+ if (kind === 'file' || cached) {
return
}
@@ -59,6 +75,8 @@ export function useMediaPlaybackSrc(path: string) {
.then(value => {
if (value.startsWith('blob:')) {
objectUrl = value
+ } else {
+ rememberPlaybackSrc(cacheKey, value)
}
if (!cancelled) {
@@ -80,7 +98,7 @@ export function useMediaPlaybackSrc(path: string) {
URL.revokeObjectURL(objectUrl)
}
}
- }, [kind, owner, path, sessionId])
+ }, [cacheKey, kind, owner, path, sessionId])
const onLoadedMetadata = useCallback((event: PlaybackEvent) => {
const element = event.currentTarget
@@ -107,8 +125,10 @@ export function useMediaPlaybackSrc(path: string) {
const element = event.currentTarget
const failing = element.getAttribute('src') ?? ''
- const fail = () =>
+ const fail = () => {
+ forgetPlaybackSrc(cacheKey)
setMedia(current => (current.src === failing ? { ...current, failed: true } : current))
+ }
const neverLoaded = loadedSrc.current !== failing
// A source that never loaded can only have expired if its element waited to fetch it
@@ -139,12 +159,13 @@ export function useMediaPlaybackSrc(path: string) {
retriedSrc.current = fresh
}
+ rememberPlaybackSrc(cacheKey, fresh)
resume.current = { ...point, src: fresh }
setMedia(current => (current.src === failing ? { ...current, src: fresh } : current))
})
.catch(fail)
},
- [owner, path, sessionId]
+ [cacheKey, owner, path, sessionId]
)
// An element with preload="none" fetches nothing for a new source until asked, so one
diff --git a/apps/desktop/src/lib/playback-source-cache.ts b/apps/desktop/src/lib/playback-source-cache.ts
new file mode 100644
index 00000000000..4f43ccc6a69
--- /dev/null
+++ b/apps/desktop/src/lib/playback-source-cache.ts
@@ -0,0 +1,54 @@
+// Resolved audio/video stream URLs, kept across remounts. Transcript rows
+// remount routinely (a finished turn re-renders its markdown, the render
+// budget recycles older rows, a reconnect re-resumes the chat). In the Webapp
+// every mount minted a new stream ticket, so the browser fetched the clip
+// again under a new URL and the clip's remembered position (keyed by URL)
+// missed: over a slow link the clip went blank and restarted.
+//
+// Only URL strings are kept, never bytes, and for well under a ticket's life
+// (the server's stream tickets last an hour). A URL that fails anyway is
+// re-resolved by its player, which replaces the entry.
+
+const PLAYBACK_TTL_MS = 20 * 60_000
+
+const playback = new Map<string, { at: number; src: string }>()
+
+export function cachedPlaybackSrc(key: string, now = Date.now()): string | undefined {
+ const entry = playback.get(key)
+
+ if (entry && now - entry.at < PLAYBACK_TTL_MS) {
+ return entry.src
+ }
+
+ playback.delete(key)
+
+ return undefined
+}
+
+export function rememberPlaybackSrc(key: string, src: string, now = Date.now()): void {
+ playback.delete(key)
+
+ // A blob URL is revoked by the element that minted it; never hand one to another mount.
+ if (!src || src.startsWith('blob:')) {
+ return
+ }
+
+ playback.set(key, { at: now, src })
+
+ for (const [stale, entry] of playback) {
+ if (now - entry.at < PLAYBACK_TTL_MS) {
+ break
+ }
+
+ playback.delete(stale)
+ }
+}
+
+export function forgetPlaybackSrc(key: string): void {
+ playback.delete(key)
+}
+
+/** Process-memory only; tests reset it between cases that reuse one path. */
+export function clearPlaybackSourceCache(): void {
+ playback.clear()
+}
diff --git a/apps/desktop/vitest.setup.ts b/apps/desktop/vitest.setup.ts
index d245e110dfc..7a88ce26330 100644
--- a/apps/desktop/vitest.setup.ts
+++ b/apps/desktop/vitest.setup.ts
@@ -1,5 +1,7 @@
import { configure } from '@testing-library/react'
+import { afterEach } from 'vitest'
+import { clearPlaybackSourceCache } from './src/lib/playback-source-cache'
import { InertResizeObserver } from './src/test/jsdom'
// Shared tooltips measure their arrow through Radix's useSize hook.
@@ -78,3 +80,7 @@ if ('Element' in globalThis) {
// as the 15s testTimeout above it while still finishing below it, so a
// genuinely hung await still surfaces as this assertion, not a test timeout.
configure({ asyncUtilTimeout: 12_000 })
+
+// Resolved stream URLs outlive a mount on purpose; a test file reusing one path must not
+// inherit the previous case's source.
+afterEach(() => clearPlaybackSourceCache())