Skip to content

The agent that grows with you

fix(webapp): play a Docker session's media from its own workspace sandbox

unsupportedpastelsc5ec24ac2018
diff --git a/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx b/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx
index 9ea6643f40d..e8a4dcc1729 100644
--- a/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx
+++ b/apps/desktop/src/components/assistant-ui/tool-media-slot.tsx
@@ -167,10 +167,10 @@ const ToolMediaLeaf: FC<{
// a split-view tile can belong to another connection than the foreground.
resolveUrl: () =>
['audio', 'video'].includes(mediaKind(path))
- ? resolveMediaPlaybackSrc(path)
+ ? resolveMediaPlaybackSrc(path, owner, sessionId ?? undefined)
: resolveMediaDisplaySrc(path, owner)
}),
- [owner, path]
+ [owner, path, sessionId]
)
const args = useMemo(
diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts
index f85725e1a35..bb69f6cf3e8 100644
--- a/apps/desktop/src/global.d.ts
+++ b/apps/desktop/src/global.d.ts
@@ -350,6 +350,8 @@ declare global {
connectionId?: null | string
path: string
profile?: null | string
+ /** The chat the clip belongs to: its `/workspace` is that session's sandbox. */
+ sessionId?: string
}) => Promise<string>
saveImageFromUrl: (url: string) => Promise<boolean>
/** Edit verb against the window's focused element (the custom context
diff --git a/apps/desktop/src/hooks/use-media-playback-src.ts b/apps/desktop/src/hooks/use-media-playback-src.ts
index ece3b29bb68..1db092ec94e 100644
--- a/apps/desktop/src/hooks/use-media-playback-src.ts
+++ b/apps/desktop/src/hooks/use-media-playback-src.ts
@@ -1,6 +1,8 @@
+import { useStore } from '@nanostores/react'
import { type SyntheticEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useComposerScope } from '@/app/chat/composer/scope'
+import { useSessionView } from '@/app/chat/session-view'
import { mediaKind, resolveMediaPlaybackSrc } from '@/lib/media'
type PlaybackEvent = SyntheticEvent<HTMLAudioElement | HTMLVideoElement>
@@ -31,6 +33,12 @@ export function useMediaPlaybackSrc(path: string) {
[scope.connectionId, scope.profile]
)
+ // The transcript's own session: a Docker agent's /workspace clip lives in its sandbox.
+ const view = useSessionView()
+ const storedId = useStore(view.$storedId)
+ const runtimeId = useStore(view.$runtimeId)
+ const sessionId = storedId || runtimeId || undefined
+
const [media, setMedia] = useState({ failed: false, path, src: '' })
const loadedSrc = useRef('')
// Minted for a source that failed before it ever loaded; it gets no retry of its own.
@@ -47,7 +55,7 @@ export function useMediaPlaybackSrc(path: string) {
return
}
- void resolveMediaPlaybackSrc(path, owner)
+ void resolveMediaPlaybackSrc(path, owner, sessionId)
.then(value => {
if (value.startsWith('blob:')) {
objectUrl = value
@@ -72,7 +80,7 @@ export function useMediaPlaybackSrc(path: string) {
URL.revokeObjectURL(objectUrl)
}
}
- }, [kind, owner, path])
+ }, [kind, owner, path, sessionId])
const onLoadedMetadata = useCallback((event: PlaybackEvent) => {
const element = event.currentTarget
@@ -115,7 +123,7 @@ export function useMediaPlaybackSrc(path: string) {
const point = { element, paused: element.paused, time: element.currentTime }
- void resolveMediaPlaybackSrc(path, owner)
+ void resolveMediaPlaybackSrc(path, owner, sessionId)
.then(fresh => {
if (fresh === failing || fresh.startsWith('blob:')) {
if (fresh !== failing) {
@@ -136,7 +144,7 @@ export function useMediaPlaybackSrc(path: string) {
})
.catch(fail)
},
- [owner, path]
+ [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/browser-files.ts b/apps/desktop/src/lib/browser-files.ts
index 0bf0a44f67d..13c12ec877e 100644
--- a/apps/desktop/src/lib/browser-files.ts
+++ b/apps/desktop/src/lib/browser-files.ts
@@ -74,7 +74,8 @@ export function createBrowserFilesBridge({
return (await fileEndpointUrl(bootstrap, 'stream', {
path: payload.path,
- profile: payload.profile ?? currentProfile()
+ profile: payload.profile ?? currentProfile(),
+ session_id: payload.sessionId
})).href
},
readDir: (path: string) =>
diff --git a/apps/desktop/src/lib/browser-session-workspace-files.test.tsx b/apps/desktop/src/lib/browser-session-workspace-files.test.tsx
new file mode 100644
index 00000000000..2ba2b2cf726
--- /dev/null
+++ b/apps/desktop/src/lib/browser-session-workspace-files.test.tsx
@@ -0,0 +1,90 @@
+import { LOCAL_CONNECTION_ID } from '@hermes/shared'
+import { cleanup, render, waitFor } from '@testing-library/react'
+import { atom } from 'nanostores'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { setApiRequestConnection, setApiRequestProfile } from '@/api/client'
+import { SessionViewProvider } from '@/app/chat/session-view'
+import { MarkdownImage } from '@/components/assistant-ui/markdown-text'
+import { $connection } from '@/store/session'
+import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
+
+import { installBrowserDesktopBridge } from './browser-desktop-bridge'
+import { resolveMediaPlaybackSrc } from './media'
+
+// WA-5: a Docker agent's /workspace is its own session's sandbox, so a Webapp file request names
+// the session it belongs to. The gateway half is tests/hermes_cli/test_web_server_fs_session_workspace.py.
+const OWNER = { connectionId: LOCAL_CONNECTION_ID, profile: 'default' }
+const CLIP = '/workspace/clip.mp4'
+const win = window
+let requests: { body: Record<string, unknown>; method: string; url: URL }[] = []
+
+beforeEach(() => {
+ requests = []
+ seedWebappSession('served-token')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (input: URL, init?: RequestInit) => {
+ requests.push({ body: JSON.parse(String(init?.body ?? '{}')), method: init?.method ?? 'GET', url: input })
+
+ const answers: Record<string, unknown> = {
+ '/api/files/ticket': { ticket: 'stream-ticket' },
+ '/api/fs/read-text': { path: '/workspace/note.txt', text: 'session note' },
+ '/api/fs/write-text': { ok: true, path: '/workspace/note.txt' }
+ }
+
+ return new Response(JSON.stringify(answers[input.pathname] ?? {}), { status: 200 })
+ })
+ )
+ expect(installBrowserDesktopBridge()).toBe(true)
+ setApiRequestConnection(LOCAL_CONNECTION_ID)
+ setApiRequestProfile('default')
+ $connection.set({ connectionId: LOCAL_CONNECTION_ID, mode: 'remote', profile: 'default' } as never)
+})
+
+afterEach(() => {
+ cleanup()
+ clearWebappSession()
+ Reflect.deleteProperty(win, 'hermesDesktop')
+ document.documentElement.removeAttribute('data-hermes-desktop-host')
+ setApiRequestConnection(null)
+ setApiRequestProfile(null)
+ $connection.set(null)
+ window.dispatchEvent(new Event('beforeunload'))
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+})
+
+describe('media in a session sandbox', () => {
+ it('tickets a clip for the session it belongs to', async () => {
+ const src = new URL(await resolveMediaPlaybackSrc(CLIP, OWNER, 'sess-1'))
+ const [ticket] = requests
+
+ expect(ticket.body).toEqual({ path: CLIP, profile: 'default', route: 'stream', session_id: 'sess-1' })
+ expect(Object.fromEntries(src.searchParams)).toEqual({
+ path: CLIP,
+ profile: 'default',
+ session_id: 'sess-1',
+ ticket: 'stream-ticket'
+ })
+ })
+
+ it('names no session when the caller has none', async () => {
+ await resolveMediaPlaybackSrc(CLIP, OWNER)
+
+ expect(requests[0].body).toEqual({ path: CLIP, profile: 'default', route: 'stream' })
+ })
+
+ it('plays a transcript clip from the transcript session', async () => {
+ const view = { $runtimeId: atom<null | string>('runtime-1'), $storedId: atom<null | string>('stored-1') }
+
+ const { container } = render(
+ <SessionViewProvider value={view as never}>
+ <MarkdownImage alt="clip" src={CLIP} />
+ </SessionViewProvider>
+ )
+
+ await waitFor(() => expect(container.querySelector('video')?.getAttribute('src')).toContain('ticket='))
+ expect(requests[0].body).toMatchObject({ path: CLIP, route: 'stream', session_id: 'stored-1' })
+ })
+})
diff --git a/apps/desktop/src/lib/media.ts b/apps/desktop/src/lib/media.ts
index ccf10f0ec81..f2b1fb8c32f 100644
--- a/apps/desktop/src/lib/media.ts
+++ b/apps/desktop/src/lib/media.ts
@@ -239,7 +239,7 @@ export function rememberMediaImageFailure(key: string): void {
// Audio/video need a seekable source instead of a whole-file data URL. Keep
// remote URLs untouched. The Webapp supplies an authenticated HTTP source;
// Electron's media protocol reads local files or proxies remote gateways.
-export async function resolveMediaPlaybackSrc(path: string, owner?: OwnerScope): Promise<string> {
+export async function resolveMediaPlaybackSrc(path: string, owner?: OwnerScope, sessionId?: string): Promise<string> {
if (isInlineMediaSrc(path)) {
return path
}
@@ -253,7 +253,9 @@ export async function resolveMediaPlaybackSrc(path: string, owner?: OwnerScope):
connectionId: owner?.connectionId || conn?.connectionId,
// The gateway OS owns file-URI conversion, including drive letters and UNC hosts.
path,
- profile: owner?.profile || conn?.profile
+ profile: owner?.profile || conn?.profile,
+ // A Docker agent's /workspace is its session's own sandbox.
+ ...(sessionId ? { sessionId } : {})
})
}
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.py
index 2ee8edf6fae..c669a861075 100644
--- a/hermes_cli/web_routers/files.py
+++ b/hermes_cli/web_routers/files.py
@@ -812,14 +812,19 @@ async def download_managed_file(request: Request, path: str):
@router.get("/api/files/stream")
@router.head("/api/files/stream")
-async def stream_managed_file(request: Request, path: str, profile: Optional[str] = None):
+async def stream_managed_file(
+ request: Request, path: str, profile: Optional[str] = None, session_id: Optional[str] = None,
+):
"""Stream managed audio/video inline with HTTP Range support — Electron's
media pipeline may reject an attachment response as an ``<audio>``/
``<video>`` source. Browser media elements authenticate with a reusable
path-bound ``?ticket=``; the session token is never accepted in this URL.
- Same size cap, sensitive guard and MIME detection as download."""
- if getattr(request.state, "file_ticket_authority", None) is None and path.lower().startswith("file:"):
- path = str(await _fs_download_path(path, profile, None))
+ Same size cap, sensitive guard and MIME detection as download. A
+ ``session_id`` resolves ``/workspace`` in that session's own sandbox, as the
+ ticket route does."""
+ if getattr(request.state, "file_ticket_authority", None) is None and (
+ session_id is not None or path.lower().startswith("file:")):
+ path = str(await _fs_download_path(path, profile, session_id))
return await _managed_file_response(request, path, content_disposition_type="inline", media_only=True)
diff --git a/tests/hermes_cli/test_web_server_fs_session_workspace.py b/tests/hermes_cli/test_web_server_fs_session_workspace.py
new file mode 100644
index 00000000000..44a2de00a36
--- /dev/null
+++ b/tests/hermes_cli/test_web_server_fs_session_workspace.py
@@ -0,0 +1,129 @@
+"""WA-5: a Webapp file route that names a session resolves ``/workspace`` in THAT session's
+per-workspace Docker sandbox (#143655's mapping), and refuses a session it cannot see."""
+
+from pathlib import Path
+
+import pytest
+from fastapi import HTTPException
+
+from hermes_cli import web_server
+
+pytest.importorskip("starlette.testclient")
+from starlette.testclient import TestClient
+
+
+def client():
+ previous = getattr(web_server.app.state, "auth_required", None)
+ web_server.app.state.auth_required = False
+ test_client = TestClient(web_server.app)
+ test_client.headers[web_server._SESSION_HEADER_NAME] = web_server._SESSION_TOKEN
+ try:
+ yield test_client
+ finally:
+ if previous is None:
+ if hasattr(web_server.app.state, "auth_required"):
+ delattr(web_server.app.state, "auth_required")
+ else:
+ web_server.app.state.auth_required = previous
+
+
+def _workspace_sandbox(sandbox: Path, cwd: Path) -> Path:
+ from tools.environments.path_utils import sanitize_task_id_for_path
+ from tools.terminal_tool import workspace_key_for
+
+ name = sanitize_task_id_for_path(f"default:ws:{workspace_key_for(str(cwd))}")
+ workspace = sandbox / "docker" / name / "workspace"
+ workspace.mkdir(parents=True)
+ return workspace
+
+
+def sandboxes(monkeypatch, tmp_path):
+ """A persistent Docker backend with three ``/workspace`` copies: this session's, another
+ session's, and the shared profile sandbox. ``sess-ws`` belongs to the default profile,
+ ``sess-work`` to the ``work`` profile, so the default profile's lookup cannot see it."""
+ project, other = tmp_path / "project", tmp_path / "other"
+ project.mkdir()
+ other.mkdir()
+ sandbox = tmp_path / "sandboxes"
+ own = _workspace_sandbox(sandbox, project)
+ foreign = _workspace_sandbox(sandbox, other)
+ shared = sandbox / "docker" / "default" / "workspace"
+ shared.mkdir(parents=True)
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ for root, label in ((own, "session"), (foreign, "foreign"), (shared, "shared")):
+ (root / "clip.mp4").write_bytes(f"{label}-clip".encode())
+ (root / "note.txt").write_text(f"{label} note", encoding="utf-8")
+ (outside / "secret.mp4").write_bytes(b"outside-clip")
+ (outside / "secret.txt").write_text("outside note", encoding="utf-8")
+ (own / "escape.mp4").symlink_to(outside / "secret.mp4")
+ (own / "escape.txt").symlink_to(outside / "secret.txt")
+ monkeypatch.setenv("TERMINAL_SANDBOX_DIR", str(sandbox))
+ monkeypatch.setenv("TERMINAL_ENV", "docker")
+ monkeypatch.setenv("TERMINAL_CONTAINER_PERSISTENT", "true")
+ monkeypatch.delenv("TERMINAL_DOCKER_VOLUMES", raising=False)
+ monkeypatch.delenv("TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE", raising=False)
+ rows = {
+ "sess-ws": ("default", str(project)),
+ "sess-other": ("default", str(other)),
+ "sess-work": ("work", str(project)),
+ }
+
+ async def detail(session_id, profile=None):
+ # The real lookup reads only the requested profile's state.db.
+ row = rows.get(session_id)
+ if row is None or row[0] != (profile or "default"):
+ raise HTTPException(status_code=404, detail="Session not found")
+ return {"cwd": row[1], "id": session_id}
+
+ monkeypatch.setattr("hermes_cli.web_routers.sessions.get_session_detail", detail)
+ return {"own": own, "foreign": foreign, "shared": shared, "outside": outside}
+
+
+# Part 1: media streams
+
+
+def test_stream_plays_the_sessions_own_workspace_clip(client, sandboxes):
+ params = {"path": "/workspace/clip.mp4", "session_id": "sess-ws"}
+
+ full = client.get("/api/files/stream", params=params)
+ partial = client.get("/api/files/stream", params=params, headers={"Range": "bytes=0-6"})
+ other = client.get("/api/files/stream", params={**params, "session_id": "sess-other"})
+
+ assert (full.status_code, full.content) == (200, b"session-clip")
+ assert (partial.status_code, partial.content) == (206, b"session")
+ assert (other.status_code, other.content) == (200, b"foreign-clip")
+ # No session keeps the shared profile sandbox, as before.
+ bare = client.get("/api/files/stream", params={"path": "/workspace/clip.mp4"})
+ assert (bare.status_code, bare.content) == (200, b"shared-clip")
+
+
+def test_a_stream_ticket_for_a_session_plays_that_sessions_clip(client, sandboxes, monkeypatch):
+ monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)
+ query = {"path": "/workspace/clip.mp4", "session_id": "sess-ws"}
+ minted = client.post("/api/files/ticket", json={"route": "stream", **query})
+ assert minted.status_code == 200, minted.text
+ del client.headers[web_server._SESSION_HEADER_NAME]
+
+ played = client.get("/api/files/stream", params={**query, "ticket": minted.json()["ticket"]})
+
+ assert (played.status_code, played.content) == (200, b"session-clip")
+
+
[email protected]("session_id", ["sess-work", "missing-session", " "])
+def test_stream_refuses_a_session_this_profile_does_not_own(client, sandboxes, monkeypatch, session_id):
+ params = {"path": "/workspace/clip.mp4", "session_id": session_id}
+
+ assert client.get("/api/files/stream", params=params).status_code == 404
+ monkeypatch.setattr(web_server.app.state, "ui_surface", "webapp", raising=False)
+ assert client.post("/api/files/ticket", json={"route": "stream", **params}).status_code == 404
+
+
[email protected]("path", ["/workspace/escape.mp4", "/workspace/../outside/secret.mp4"])
+def test_stream_refuses_a_clip_outside_the_sessions_workspace(client, sandboxes, path):
+ response = client.get("/api/files/stream", params={"path": path, "session_id": "sess-ws"})
+
+ assert response.status_code in (400, 403, 404), response.status_code
+ assert b"outside-clip" not in response.content