Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Fix inline Python plot rendering and downloads (#10954)
Michael Hanf480c1a4ef02
diff --git a/.github/workflows/studio-inline-images.yml b/.github/workflows/studio-inline-images.ymlnew file mode 100644index 00000000000..eb7e801e55f--- /dev/null+++ b/.github/workflows/studio-inline-images.yml@@ -0,0 +1,110 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++name: Inline image compatibility++on:+ pull_request:+ paths:+ - 'studio/frontend/src/components/assistant-ui/markdown-text.tsx'+ - 'studio/frontend/src/components/assistant-ui/rehype-sandbox-images.ts'+ - 'studio/frontend/src/components/assistant-ui/sandbox-files.ts'+ - 'studio/frontend/src/components/assistant-ui/use-sandbox-image.ts'+ - 'studio/frontend/src/lib/markdown-data-images.ts'+ - 'studio/frontend/src/lib/safe-markdown-url.ts'+ - 'studio/frontend/src/lib/native-files.ts'+ - 'studio/frontend/src/lib/data-uri.ts'+ - 'studio/frontend/src/features/auth/**'+ - 'studio/frontend/src/index.css'+ - 'studio/frontend/package*.json'+ - 'studio/frontend/vite.config.ts'+ - 'studio/frontend/tests/markdown-image-pipeline.test.ts'+ - 'studio/frontend/tests/sandbox-image-lifecycle.test.ts'+ - 'studio/src-tauri/tauri.conf.json'+ - 'tests/studio/fixtures/inline-image-*'+ - 'tests/studio/playwright_inline_images.py'+ - 'tests/studio/_playwright_robust.py'+ - '.github/scripts/retry-with-apt-lock.sh'+ - 'scripts/lockfile_supply_chain_audit.py'+ - '.github/workflows/studio-inline-images.yml'+ workflow_dispatch:++permissions:+ contents: read++concurrency:+ group: ${{ github.workflow }}-${{ github.ref }}+ cancel-in-progress: true++jobs:+ images:+ name: Inline images (${{ matrix.os }})+ runs-on: ${{ matrix.os }}+ timeout-minutes: 35+ strategy:+ fail-fast: false+ matrix:+ os: [ubuntu-latest, macos-latest, windows-latest]+ defaults:+ run:+ shell: bash+ env:+ PLAYWRIGHT_BROWSERS_PATH: ${{ github.workspace }}/temp/inline-image-validation/browsers+ PIP_CACHE_DIR: ${{ github.workspace }}/temp/inline-image-validation/pip-cache+ steps:+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ persist-credentials: false+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0+ with:+ node-version: '22'+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0+ with:+ python-version: '3.13'+ - name: Audit dependencies+ run: python scripts/lockfile_supply_chain_audit.py+ - name: Install frontend dependencies+ working-directory: studio/frontend+ run: npm ci --ignore-scripts --no-fund --no-audit+ - name: Create isolated test environment+ run: |+ python -m venv temp/inline-image-validation/venv+ if [ "$RUNNER_OS" = Windows ]; then+ bin="$PWD/temp/inline-image-validation/venv/Scripts"+ else+ bin="$PWD/temp/inline-image-validation/venv/bin"+ fi+ "$bin/python" -m pip install playwright==1.62.0 pillow==12.3.0+ echo "$bin" >> "$GITHUB_PATH"+ - name: Install browser engines+ timeout-minutes: 10+ run: python -m playwright install chromium firefox webkit+ - name: Install missing Linux browser libraries+ if: runner.os == 'Linux'+ timeout-minutes: 17+ env:+ RETRY_ATTEMPTS: '2'+ RETRY_ATTEMPT_TIMEOUT: '420'+ APT_ACQUIRE_RETRIES: '0'+ run: |+ if ! python tests/studio/playwright_inline_images.py --probe; then+ bash .github/scripts/retry-with-apt-lock.sh python -m playwright install-deps chromium firefox webkit+ python tests/studio/playwright_inline_images.py --probe+ fi+ - name: Markdown path matrix+ working-directory: studio/frontend+ run: node --experimental-strip-types --test tests/markdown-image-pipeline.test.ts tests/sandbox-image-lifecycle.test.ts+ - name: Browser simulations+ timeout-minutes: 5+ run: python tests/studio/playwright_inline_images.py --browsers chromium firefox webkit chrome msedge --output temp/inline-image-validation/after+ - name: Upload failure reports+ if: failure()+ uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1+ with:+ name: inline-images-${{ runner.os }}+ path: |+ temp/inline-image-validation/after/*.json+ temp/inline-image-validation/after/*.png+ temp/inline-image-validation/after/server.log+ retention-days: 1+ if-no-files-found: ignorediff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsxindex 84bc96d6042..d3f90bbc3fb 100644--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx@@ -31,7 +31,7 @@ import { copyToClipboard } from "@/lib/copy-to-clipboard";import { normalizeEscapedInlineMath } from "@/lib/escaped-inline-math";import { preprocessLaTeX } from "@/lib/latex";import { withDataImageSupport } from "@/lib/markdown-data-images";-import { downloadFile, isDownloadCancelled } from "@/lib/native-files";+import { downloadFile, isDownloadCancelled, urlToBlob } from "@/lib/native-files";import { openLink } from "@/lib/open-link";import { safeMarkdownUrl } from "@/lib/safe-markdown-url";import { Tick02Icon } from "@/lib/tick-icon";@@ -85,6 +85,7 @@ import {} from "./sandbox-files";import { SearchImageElement, SearchImagesContext } from "./search-image";import { useSandboxImage } from "./use-sandbox-image";+import { rehypeSandboxImages } from "./rehype-sandbox-images";import { unslothDarkTheme, unslothLightTheme } from "./code-themes";import { stabilizeStreamingMarkdown } from "./streaming-markdown";import {@@ -230,16 +231,19 @@ const MarkdownImage = memo(function MarkdownImage(props: ComponentProps<"img">)</span>)}{/* Kept from the replaced renderer: the hover tint over the image. */}- <div className="pointer-events-none absolute inset-0 hidden rounded-lg bg-black/10 group-hover:block" />+ <span className="pointer-events-none absolute inset-0 hidden rounded-lg bg-black/10 group-hover:block" />{!failedNow && resolved ? (<buttontype="button"title="Download image"className="absolute right-2 bottom-2 flex h-8 w-8 cursor-pointer items-center justify-center rounded-md border border-border bg-background/90 opacity-0 backdrop-blur-sm transition-all duration-200 group-hover:opacity-100"onClick={async () => {- // By now the src is always blob:/data:, so a plain fetch carries it.+ // Reuse fetched bytes under the desktop CSP.try {- const blob = await (await fetch(resolved)).blob();+ const blob =+ file !== null && sandbox.state.status === "loaded"+ ? sandbox.state.blob+ : await urlToBlob(resolved);await downloadFile(blob, downloadName(blob.type), blob.type);} catch (error) {if (!isDownloadCancelled(error)) toast.error("Could not save file.");@@ -282,9 +286,6 @@ const STREAMDOWN_ALLOWED_TAGS = {[SEARCH_IMAGE_TAG]: ["token"],} satisfies NonNullable<StreamdownProps["allowedTags"]>;-// Module-scoped: Streamdown extends its sanitize schema only for its default pipeline, so the-// allowed-tag merge and the data-image protocol ride on a pipeline we pass ourselves (see lib).-const STREAMDOWN_REHYPE_PLUGINS = withDataImageSupport(STREAMDOWN_ALLOWED_TAGS);const COPY_RESET_MS = 2000;const MERMAID_SOURCE_RE = /```mermaid\s*([\s\S]*?)```/i;const ACTION_PANEL_CLASS =@@ -834,6 +835,20 @@ function MarkdownTextRenderer({statusType,text,}: MarkdownTextRendererProps) {+ const remoteId = useAuiState(({ threadListItem }) => threadListItem.remoteId);+ const activeThreadId = useChatRuntimeStore((state) => state.activeThreadId);+ const projectId = useChatProjectScope();+ const threadId = remoteId ?? activeThreadId ?? undefined;+ // Streamdown's memo comparator ignores rehypePlugins.+ const sandboxScopeKey = JSON.stringify([threadId, projectId]);+ const rehypePlugins = useMemo(+ () =>+ // Streamdown caches processors by plugin name and serialized options.+ withDataImageSupport(STREAMDOWN_ALLOWED_TAGS, [+ [rehypeSandboxImages, { threadId, projectId }],+ ]),+ [threadId, projectId],+ );const searchImages = useMemo(() => parseSearchImagesSignature(searchImagesKey),[searchImagesKey],@@ -896,7 +911,7 @@ function MarkdownTextRenderer({<SearchImagesContext.Provider value={searchImages}><div data-status={statusType} className="min-w-0 max-w-full"><Streamdown- key={`${messageId}:${incrementalCache.renderGeneration}:${renderKey}`}+ key={`${messageId}:${incrementalCache.renderGeneration}:${renderKey}:${sandboxScopeKey}`}mode="streaming"parseIncompleteMarkdown={!incrementalRender}parseMarkdownIntoBlocksFn={@@ -908,7 +923,7 @@ function MarkdownTextRenderer({plugins={STREAMDOWN_PLUGINS}components={STREAMDOWN_COMPONENTS}allowedTags={STREAMDOWN_ALLOWED_TAGS}- rehypePlugins={STREAMDOWN_REHYPE_PLUGINS}+ rehypePlugins={rehypePlugins}urlTransform={safeMarkdownUrl}controls={STREAMDOWN_CONTROLS}shikiTheme={STREAMDOWN_SHIKI_THEME}@@ -1002,4 +1017,4 @@ export const MarkdownText = withSmoothContextProvider(MarkdownTextImpl);// Reasoning pages render at message-group scope, where assistant-ui deliberately// exposes no `part`. Its smooth wrapper reads that property, so the source-fed// renderer must stay independent of both the part adapter and that wrapper.-export const MarkdownTextSource = MarkdownTextSourceImpl;\ No newline at end of file+export const MarkdownTextSource = MarkdownTextSourceImpl;diff --git a/studio/frontend/src/components/assistant-ui/rehype-sandbox-images.ts b/studio/frontend/src/components/assistant-ui/rehype-sandbox-images.tsnew file mode 100644index 00000000000..0f2796eda6c--- /dev/null+++ b/studio/frontend/src/components/assistant-ui/rehype-sandbox-images.ts@@ -0,0 +1,49 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import type { Root, RootContent } from "hast";+import type { Plugin } from "unified";++import { safeMarkdownUrl } from "../../lib/safe-markdown-url.ts";+import { decodeSegment, markdownSandboxImageSrc } from "./sandbox-files.ts";++type SandboxScope = Parameters<typeof markdownSandboxImageSrc>[1];++function unsafeImagePath(src: string): boolean {+ if (/^(data|blob):/i.test(src)) return false;+ return src+ .split(/[?#]/)[0]+ .split("/")+ .some((segment) => {+ const decoded = decodeSegment(segment);+ return (+ decoded === ".." ||+ decoded.includes("/") ||+ decoded.includes("\\") ||+ Array.from(decoded).some(+ (char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127,+ )+ );+ });+}++export const rehypeSandboxImages: Plugin<[SandboxScope], Root> =+ function rehypeSandboxImages(scope) {+ return (tree) => {+ function walk(node: Root | RootContent): void {+ if (node.type === "element" && node.tagName === "img") {+ const src = node.properties.src;+ if (typeof src === "string") {+ // Check URLs before harden changes relative paths or removes origins.+ const safe = safeMarkdownUrl(src, "src", node);+ node.properties.src =+ safe && !unsafeImagePath(safe)+ ? (markdownSandboxImageSrc(safe, scope) ?? safe)+ : undefined;+ }+ }+ if ("children" in node) node.children.forEach(walk);+ }+ walk(tree);+ };+ };diff --git a/studio/frontend/src/components/assistant-ui/use-sandbox-image.ts b/studio/frontend/src/components/assistant-ui/use-sandbox-image.tsindex 584232c3358..892e393503f 100644--- a/studio/frontend/src/components/assistant-ui/use-sandbox-image.ts+++ b/studio/frontend/src/components/assistant-ui/use-sandbox-image.ts@@ -8,7 +8,7 @@ import { type RefObject, useEffect, useRef, useState } from "react";export type SandboxImageState =| { status: "idle" }- | { status: "loaded"; url: string }+ | { status: "loaded"; url: string; blob: Blob }| { status: "failed" };const IDLE: SandboxImageState = { status: "idle" };@@ -79,7 +79,7 @@ export function useSandboxImage(url: string | null): {objectUrl = URL.createObjectURL(blob);setState({url,- load: { status: "loaded", url: objectUrl },+ load: { status: "loaded", url: objectUrl, blob },signal: controller.signal,});})diff --git a/studio/frontend/src/lib/markdown-data-images.ts b/studio/frontend/src/lib/markdown-data-images.tsindex 571f35f987c..4a6ee970672 100644--- a/studio/frontend/src/lib/markdown-data-images.ts+++ b/studio/frontend/src/lib/markdown-data-images.ts@@ -17,8 +17,11 @@ interface SanitizeSchema {protocols?: Record<string, string[]>;}-/** Streamdown's default raw/sanitize/harden pipeline with `data` added to image source protocols. */-export function withDataImageSupport(allowedTags: Record<string, string[]>): Pluggable[] {+/** Keep data images and resolve sandbox paths before URL hardening. */+export function withDataImageSupport(+ allowedTags: Record<string, string[]>,+ beforeHarden: Pluggable[] = [],+): Pluggable[] {const sanitize = defaultRehypePlugins.sanitize as [Plugin<[SanitizeSchema]>, SanitizeSchema];const [sanitizePlugin, schema] = sanitize;// Positional by design: Streamdown itself builds its default pipeline as `Object.values` of this@@ -40,6 +43,7 @@ export function withDataImageSupport(allowedTags: Record<string, string[]>): Plu},},],+ ...beforeHarden,harden,];}diff --git a/studio/frontend/tests/markdown-image-pipeline.test.ts b/studio/frontend/tests/markdown-image-pipeline.test.tsnew file mode 100644index 00000000000..bfa9b211526--- /dev/null+++ b/studio/frontend/tests/markdown-image-pipeline.test.ts@@ -0,0 +1,193 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import assert from "node:assert/strict";+import test from "node:test";+import { createElement } from "react";+import { renderToStaticMarkup } from "react-dom/server";+import { Streamdown } from "streamdown";++import { markdownSandboxImageSrc } from "../src/components/assistant-ui/sandbox-files.ts";+import { rehypeSandboxImages } from "../src/components/assistant-ui/rehype-sandbox-images.ts";+import { withDataImageSupport } from "../src/lib/markdown-data-images.ts";+import { safeMarkdownUrl } from "../src/lib/safe-markdown-url.ts";++const context = { threadId: "thread-1", projectId: null as string | null };++function render(markdown: string, scope = context) {+ const sources: string[] = [];+ const html = renderToStaticMarkup(+ createElement(Streamdown, {+ mode: "static",+ children: markdown,+ rehypePlugins: withDataImageSupport({}, [[rehypeSandboxImages, scope]]),+ urlTransform: safeMarkdownUrl,+ components: {+ img: ({ src }) => {+ if (src) sources.push(markdownSandboxImageSrc(src, scope) ?? src);+ return null;+ },+ },+ }),+ );+ return { sources, html };+}++test("relative Python images survive the full Markdown pipeline", () => {+ for (const [src, file] of [+ ["line_plot.png", "line_plot.png"],+ ["outputs/line_plot.png", "outputs/line_plot.png"],+ ["./line_plot.png", "line_plot.png"],+ ["./outputs/line_plot.png", "outputs/line_plot.png"],+ ["outputs/loss%20curve%20%231.png", "outputs/loss%20curve%20%231.png"],+ ]) {+ const result = render(``);+ assert.deepEqual(+ result.sources,+ [`/api/inference/sandbox/thread-1/${file}`],+ src,+ );+ assert.doesNotMatch(result.html, /Image blocked/, src);+ }+});++test("relative images use project scope and explicit URLs keep their session", () => {+ const scope = { threadId: "thread-2", projectId: "project-1" };+ assert.deepEqual(render("", scope).sources, [+ "/api/inference/sandbox/project-project-1/line_plot.png",+ ]);+ const src = "/api/inference/sandbox/original/line_plot.png";+ assert.deepEqual(render(``, scope).sources, [src]);+});++test("HTML image sources are resolved after raw HTML is parsed", () => {+ assert.deepEqual(+ render('<img src="outputs/line_plot.png" alt="Plot">').sources,+ ["/api/inference/sandbox/thread-1/outputs/line_plot.png"],+ );+});++test("data images still render and remote image URLs stay blocked", () => {+ const data = "data:image/png;base64,iVBORw0KGgo=";+ assert.deepEqual(render(``).sources, [data]);+ for (const src of [+ "https://example.com/plot.png",+ "http://127.0.0.1/plot.png",+ "//example.com/plot.png",+ "file:///tmp/plot.png",+ "javascript:alert%281%29",+ "data:text/html;base64,PGgxPkhlbGxvPC9oMT4=",+ ]) {+ assert.deepEqual(render(``).sources, [], src);+ }+});++test("URL normalization cannot turn traversal into another sandbox image", () => {+ for (const src of [+ "/api/inference/sandbox/thread-1/../other/secret.png",+ "/api/inference/sandbox/thread-1/%2e%2e/other/secret.png",+ "/api/inference/sandbox/thread-1/outputs/../../other/secret.png",+ "./api/inference/sandbox/thread-1/../other/secret.png",+ "./api/inference/sandbox/thread-1/%2e%2e/other/secret.png",+ "/api/inference/sandbox/thread-1/outputs%2F..%2F..%2Fother/secret.png",+ "/api/inference/sandbox/thread-1/..%5Cother/secret.png",+ ]) {+ assert.deepEqual(render(``).sources, [], src);+ }+});++test("encoded filenames and image formats resolve across Markdown, HTML, and scopes", () => {+ const names = [+ "plot",+ "loss curve",+ "café",+ "日本語",+ "100%",+ "plot #1",+ "plot?1",+ "literal%2F",+ "a+b",+ "paren(1)",+ ];+ const scopes = [+ {+ threadId: "thread-1",+ projectId: null,+ prefix: "/api/inference/sandbox/thread-1/",+ query: "",+ },+ {+ threadId: "thread-2",+ projectId: null,+ prefix: "/api/inference/sandbox/thread-2/",+ query: "",+ },+ {+ threadId: "thread-1",+ projectId: "project-1",+ prefix: "/api/inference/sandbox/project-project-1/",+ query: "",+ },+ {+ threadId: "thread/id",+ projectId: null,+ prefix: "/api/inference/sandbox/_/",+ query: "?session=thread%2Fid",+ },+ ];+ for (const ext of ["png", "jpg", "jpeg", "gif", "webp", "bmp", "avif"]) {+ for (const name of names) {+ const filename = encodeURIComponent(`${name}.${ext}`);+ for (const directory of ["", "./", "outputs/"]) {+ const src = `${directory}${filename}`;+ for (const scope of scopes) {+ const expected = `${scope.prefix}${directory === "outputs/" ? directory : ""}${filename}${scope.query}`;+ for (const markup of [+ ``,+ `<img alt="Plot" src="${src}">`,+ ]) {+ assert.deepEqual(+ render(markup, scope).sources,+ [expected],+ `${markup} in ${scope.threadId}/${scope.projectId}`,+ );+ }+ }+ }+ }+ }+});++test("path mutations cannot escape scope through Markdown or HTML", () => {+ const segments = [+ "..",+ "%2e%2e",+ ".%2E",+ "%2e.",+ "..%2fother",+ "..%5cother",+ "other%2f..",+ "%2F",+ "%5C",+ ];+ for (let code = 0; code < 32; code++)+ segments.push(`%${code.toString(16).padStart(2, "0")}`);+ segments.push("%7f");+ for (const prefix of [+ "",+ "./",+ "outputs/",+ "/api/inference/sandbox/thread-1/",+ "./api/inference/sandbox/thread-1/",+ ]) {+ for (const segment of segments) {+ const src = `${prefix}${segment}/secret.png`;+ for (const markup of [+ ``,+ `<img alt="Plot" src="${src}">`,+ ]) {+ assert.deepEqual(render(markup).sources, [], markup);+ }+ }+ }+});diff --git a/studio/frontend/tests/markdown-streaming-scheduling.test.ts b/studio/frontend/tests/markdown-streaming-scheduling.test.tsindex 8ccfbac5b89..f7a944def2b 100644--- a/studio/frontend/tests/markdown-streaming-scheduling.test.ts+++ b/studio/frontend/tests/markdown-streaming-scheduling.test.ts@@ -174,13 +174,16 @@ test("streaming reparses only the active Markdown tail", () => {);});-test("dropping retained blocks moves Streamdown's render identity", () => {- // Streamdown compares only the Markdown string, so an edit that clears the- // retained blocks while leaving the live tail alone has to remount instead.+test("retained block and sandbox changes move Streamdown's render identity", () => {+ // Streamdown also ignores rehypePlugins changes when memoizing.const streamdown = findChatStreamdown();assert.ok(streamdown, "chat <Streamdown> is missing");assert.equal(jsxAttribute(streamdown, "key")?.initializer?.getText(source),- "{`${messageId}:${incrementalCache.renderGeneration}:${renderKey}`}",+ "{`${messageId}:${incrementalCache.renderGeneration}:${renderKey}:${sandboxScopeKey}`}",+ );+ assert.match(+ source.getText(),+ /const sandboxScopeKey = JSON.stringify\(\[threadId, projectId\]\)/,);});diff --git a/studio/frontend/tests/math-block-containment-wiring.test.ts b/studio/frontend/tests/math-block-containment-wiring.test.tsindex 0fc9d5495f9..72ffc8d3bc3 100644--- a/studio/frontend/tests/math-block-containment-wiring.test.ts+++ b/studio/frontend/tests/math-block-containment-wiring.test.ts@@ -64,9 +64,9 @@ test("the chat renderer's rehypePlugins pipeline carries the allowedTags merge i"PRECONDITION: the chat renderer relies on the allowedTags sanitizer",);assert.ok(- /const STREAMDOWN_REHYPE_PLUGINS = withDataImageSupport\(STREAMDOWN_ALLOWED_TAGS\);/.test(+ /withDataImageSupport\(STREAMDOWN_ALLOWED_TAGS,/.test(MARKDOWN_TEXT,- ) && MARKDOWN_TEXT.includes("rehypePlugins={STREAMDOWN_REHYPE_PLUGINS}"),+ ) && MARKDOWN_TEXT.includes("rehypePlugins={rehypePlugins}"),"the passed pipeline must be derived from the defaults AND carry the allowedTags merge",);});diff --git a/studio/frontend/tests/sandbox-image-lifecycle.test.ts b/studio/frontend/tests/sandbox-image-lifecycle.test.tsindex 4a5d53beca0..ecc65f2a89e 100644--- a/studio/frontend/tests/sandbox-image-lifecycle.test.ts+++ b/studio/frontend/tests/sandbox-image-lifecycle.test.ts@@ -8,7 +8,7 @@ import ts from "typescript";import { readSrc } from "./helpers/kit.ts";-type Load = { status: string; url?: string };+type Load = { status: string; url?: string; blob?: unknown };function imageHook() {const source = readSrc("components/assistant-ui/use-sandbox-image.ts");@@ -93,7 +93,8 @@ function imageHook() {const flush = async () => {for (let i = 0; i < 8; i++) await Promise.resolve();};-const response = { ok: true, blob: async () => ({ type: "image/png" }) };+const imageBlob = { type: "image/png" };+const response = { ok: true, blob: async () => imageBlob };for (const previous of ["loaded", "failed"] as const) {test(`A to B to A does not reuse a cancelled ${previous} result`, async () => {@@ -104,7 +105,9 @@ for (const previous of ["loaded", "failed"] as const) {await flush();const old = hook.render("A");assert.equal(old.status, previous);- assert.equal(hook.render("B").status, "idle");+ const pendingB = hook.render("B");+ assert.equal(pendingB.status, "idle");+ assert.equal(pendingB.blob, undefined);hook.commit();assert.equal(hook.requests[0].signal.aborted, true);assert.equal(hook.live.size, 0);@@ -125,6 +128,7 @@ for (const previous of ["loaded", "failed"] as const) {await flush();const current = hook.render("A");assert.equal(current.status, "loaded");+ assert.equal(current.blob, imageBlob, "downloads reuse the fetched bytes");assert.ok(current.url && hook.live.has(current.url));assert.notEqual(current.url, old.url);hook.unmount();diff --git a/tests/studio/fixtures/inline-image-client.js b/tests/studio/fixtures/inline-image-client.jsnew file mode 100644index 00000000000..940f9ca7bd0--- /dev/null+++ b/tests/studio/fixtures/inline-image-client.js@@ -0,0 +1,443 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import React from "react";+import { createRoot } from "react-dom/client";+import { flushSync } from "react-dom";+import { AssistantRuntimeProvider, useLocalRuntime } from "@assistant-ui/react";+import { useChatRuntimeStore } from "/src/features/chat/index.ts";+import { MarkdownTextSource } from "/src/components/assistant-ui/markdown-text.tsx";+import { ChatProjectScopeContext } from "/src/features/chat/chat-project-scope.ts";+import { storeAuthTokens } from "/src/features/auth/session.ts";+import "/src/index.css";++document.addEventListener("securitypolicyviolation", (event) => {+ const message = `${event.effectiveDirective}: ${event.blockedURI}`;+ document+ .getElementById("errors")+ ?.append(document.createTextNode(message + "\n"));+});++const config = await (await fetch("/inline-fixture/config")).json();+storeAuthTokens("inline-image-fixture", "inline-image-fixture");+const adapter = { async *run() {} };+const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));+async function until(predicate, timeout = 5000) {+ const start = performance.now();+ while (!predicate()) {+ if (performance.now() - start > timeout)+ throw new Error("Condition timed out");+ await pause(20);+ }+}+const image = () =>+ document.querySelector('#subject img[data-streamdown="image"]');+const requests = async () =>+ await (await fetch("/inline-fixture/requests")).json();+const route = (session, filename) =>+ `/api/inference/sandbox/${session}/${filename}`;++function App() {+ const runtime = useLocalRuntime(adapter);+ const [view, setView] = React.useState({+ text: "Ready",+ thread: "thread-a",+ project: null,+ });+ const [status, setStatus] = React.useState("Ready");+ const [results, setResults] = React.useState([]);+ async function run() {+ const checks = [];+ const unsupportedFormats = [];+ const show = (next) =>+ flushSync(() => {+ useChatRuntimeStore.setState({+ activeThreadId: next.thread ?? "thread-a",+ });+ setView({ thread: "thread-a", project: null, ...next });+ });+ async function check(name, action) {+ setStatus(`Running: ${name}`);+ try {+ await action();+ checks.push({ name, passed: true });+ } catch (error) {+ checks.push({ name, passed: false, error: String(error) });+ }+ setResults([...checks]);+ }+ function assert(condition, message) {+ if (!condition) throw new Error(message);+ }+ async function clear() {+ show({ text: "Reset", id: "reset" });+ await fetch("/inline-fixture/requests", { method: "DELETE" });+ }+ async function loaded(width = 32) {+ await until(() => image()?.complete && image()?.naturalWidth === width);+ assert(+ image().currentSrc.startsWith("blob:"),+ "Sandbox image did not use a blob URL",+ );+ }+ async function success(name, src, expected, scope = {}) {+ await check(name, async () => {+ await clear();+ show({ text: ``, id: name, ...scope });+ await loaded(scope.width ?? 32);+ const seen = await requests();+ assert(+ seen.some((r) => r.path === expected && r.authorized),+ JSON.stringify(seen),+ );+ });+ }+ async function finish() {+ const report = {+ variant: config.variant,+ unsupportedFormats,+ userAgent: navigator.userAgent,+ checks,+ passed: checks.filter((c) => c.passed).length,+ failed: checks.filter((c) => !c.passed).length,+ };+ await fetch("/inline-fixture/report", {+ method: "POST",+ body: JSON.stringify(report),+ });+ setStatus(`Complete: ${report.passed} passed, ${report.failed} failed`);+ document.getElementById("report").textContent = JSON.stringify(report);+ document.getElementById("report").dataset.complete = "true";+ }+ if (config.variant === "before") {+ await success(+ "path: plot.png",+ "plot.png",+ route("thread-a", "plot.png"),+ );+ await success(+ "recorded scope",+ route("recorded", "plot.png"),+ route("recorded", "plot.png"),+ { width: 80 },+ );+ await finish();+ return;+ }+ for (const name of [+ "plot.png",+ "./plot.png",+ "outputs/plot.png",+ "./outputs/plot.png",+ "outputs/./plot.png",+ ]) {+ await success(+ `path: ${name}`,+ name,+ route("thread-a", name.replace(/^\.\//, "").replace("/./", "/")),+ );+ }+ for (const name of [+ "loss curve.png",+ "caf\u00e9.png",+ "\u65e5\u672c\u8a9e.png",+ "100%.png",+ "plot #1.png",+ "plot?1.png",+ "literal%2F.png",+ "a+b.png",+ "paren(1).png",+ ]) {+ await success(+ `filename: ${name}`,+ encodeURIComponent(name),+ route("thread-a", encodeURIComponent(name)),+ );+ }+ for (const [ext, data] of Object.entries(config.formats)) {+ await check(`format: ${ext}`, async () => {+ const control = new Image();+ control.src = data;+ let supported = true;+ try {+ await control.decode();+ } catch {+ supported = false;+ }+ await clear();+ show({ text: ``, id: `format-${ext}` });+ if (supported) {+ assert(control.naturalWidth === 32, "Invalid codec control");+ await loaded();+ } else {+ assert(ext === "avif", `Required raster codec unavailable: ${ext}`);+ unsupportedFormats.push(ext);+ await until(() =>+ document.querySelector(+ '#subject [data-streamdown="image-fallback"]',+ ),+ );+ assert(+ image().naturalWidth === 0,+ "Unsupported image decoded unexpectedly",+ );+ }+ assert(+ (await requests()).some(+ (r) => r.path === route("thread-a", `plot.${ext}`) && r.authorized,+ ),+ "Missing authenticated format request",+ );+ });+ }++ await success(+ "project scope",+ "plot.png",+ route("project-p1", "plot.png"),+ { project: "p1", width: 64 },+ );+ await success(+ "recorded scope",+ route("recorded", "plot.png"),+ route("recorded", "plot.png"),+ { project: "p1", width: 80 },+ );+ await success(+ "query session",+ "/api/inference/sandbox/_/plot.png?session=session%2Fid",+ "/api/inference/sandbox/_/plot.png?session=session%2Fid",+ { width: 96 },+ );+ for (const text of [+ '<img src="outputs/plot.png" alt="Plot">',+ "![Plot][chart]\n\n[chart]: outputs/plot.png",+ "[](https://example.invalid/)",+ ]) {+ await check(`markup: ${text.slice(0, 35)}`, async () => {+ await clear();+ show({ text, id: text });+ await loaded();+ assert(+ (await requests()).every((r) => r.authorized),+ "Missing Authorization header",+ );+ assert(+ !document.querySelector("#subject p div"),+ "Image contains a block inside a paragraph",+ );+ });+ }+ await check("embedded PNG", async () => {+ await clear();+ show({ text: ``, id: "data" });+ await until(() => image()?.complete && image()?.naturalWidth === 32);+ assert(+ (await requests()).length === 0,+ "Data image fetched a sandbox URL",+ );+ });+ await check("root asset remains supported", async () => {+ await clear();+ show({ text: "", id: "asset" });+ await until(() => image()?.complete && image()?.naturalWidth === 32);+ });+ const forbidden = [+ "../secret.png",+ "outputs/../../secret.png",+ "..%2Fsecret.png",+ "..%5Csecret.png",+ "/api/inference/sandbox/thread-a/../other/secret.png",+ "/api/inference/sandbox/thread-a/%2E%2E/other/secret.png",+ "./api/inference/sandbox/thread-a/../other/secret.png",+ "/api/inference/sandbox/thread-a/outputs%2F..%2F..%2Fother/secret.png",+ "/api/inference/sandbox/thread-a/%00plot.png",+ "//example.invalid/api/inference/sandbox/other/plot.png",+ "https://example.invalid/plot.png",+ "http://127.0.0.1/plot.png",+ "file:///tmp/plot.png",+ "C:\\Users\\test\\plot.png",+ "outputs\\plot.png",+ "javascript:alert%281%29",+ "data:text/html;base64,PGgxPnRlc3Q8L2gxPg==",+ ];+ for (const src of forbidden)+ await check(`reject: ${src}`, async () => {+ await clear();+ show({ text: ``, id: src });+ await pause(80);+ assert(+ !image()?.getAttribute("src"),+ "Rejected source reached an image",+ );+ assert(+ (await requests()).length === 0,+ "Rejected source requested a sandbox file",+ );+ });+ for (const text of [+ "plot.png",+ "``",+ "```python\n\n```",+ "[Documentation](https://example.invalid/page)",+ ]) {+ await check(`non-image: ${text.slice(0, 30)}`, async () => {+ await clear();+ show({ text, id: text });+ await pause(80);+ assert(!image(), "Non-image content became an image");+ assert(+ (await requests()).length === 0,+ "Non-image content fetched a file",+ );+ });+ }+ await check("HTML event handlers are removed", async () => {+ await clear();+ show({+ text: '<img src="plot.png" onload="document.title=\'UNSAFE\'" onerror="document.title=\'UNSAFE\'">',+ id: "handlers",+ });+ await loaded();+ assert(document.title !== "UNSAFE", "HTML handler executed");+ assert(+ !image().hasAttribute("onload") && !image().hasAttribute("onerror"),+ "Handler survived sanitization",+ );+ });+ for (const filename of ["missing.png", "forbidden.png", "broken.png"])+ await check(`failure and recovery: ${filename}`, async () => {+ await clear();+ show({ text: ``, id: "recover" });+ await until(() =>+ document.querySelector('#subject [data-streamdown="image-fallback"]'),+ );+ show({ text: "", id: "recover" });+ await loaded();+ assert(+ !document.querySelector(+ '#subject [data-streamdown="image-fallback"]',+ ),+ "Fallback stayed visible",+ );+ });+ await check("unauthenticated direct request is denied", async () => {+ assert(+ (await fetch(route("thread-a", "plot.png"))).status === 401,+ "Fixture accepted no token",+ );+ });+ await check("scope switches invalidate cached Markdown", async () => {+ await clear();+ for (const scope of [+ { thread: "thread-a", width: 32 },+ { thread: "thread-b", width: 48 },+ { project: "p1", width: 64 },+ { thread: "thread-a", width: 32 },+ ]) {+ show({ text: "", id: "scope-switch", ...scope });+ await loaded(scope.width);+ }+ });+ await check("late response cannot replace a newer scope", async () => {+ await clear();+ show({ text: "", id: "race", thread: "thread-a" });+ await pause(70);+ show({ text: "", id: "race", thread: "thread-b" });+ await loaded(48);+ await pause(650);+ assert(+ image().naturalWidth === 48,+ "Late image replaced the current scope",+ );+ });+ await check("offscreen images wait for visibility", async () => {+ await clear();+ show({ text: "", id: "offscreen", offscreen: true });+ window.scrollTo(0, 0);+ assert(+ image().getBoundingClientRect().top > innerHeight + 200,+ "Fixture image is inside the preload margin",+ );+ await pause(100);+ assert(+ (await requests()).length === 0,+ `Offscreen image loaded early: top=${image().getBoundingClientRect().top}, scroll=${scrollY}`,+ );+ document.getElementById("subject").scrollIntoView();+ await loaded();+ });+ await check("streaming image link completes", async () => {+ await clear();+ window.scrollTo(0, 0);+ const text = "Here is the plot:\n\n";+ for (let n = 1; n <= text.length; n += 3) {+ show({ text: text.slice(0, n), id: "stream", streaming: true });+ await pause(20);+ }+ show({ text, id: "stream", streaming: false });+ await loaded();+ });+ await finish();+ }+ return React.createElement(+ AssistantRuntimeProvider,+ { runtime },+ React.createElement(+ "main",+ { style: { padding: 24, maxWidth: 1000, margin: "auto" } },+ React.createElement("h1", null, "Inline image compatibility simulation"),+ React.createElement(+ "button",+ { onClick: run, disabled: status.startsWith("Running") },+ "Run simulations",+ ),+ ...[+ ["Show embedded download", ``],+ ["Show encoded download", ""],+ ].map(([label, text]) =>+ React.createElement(+ "button",+ {+ key: label,+ onClick: () =>+ setView({ text, id: label, thread: "thread-a", project: null }),+ },+ label,+ ),+ ),++ React.createElement("p", { id: "status", role: "status" }, status),+ React.createElement(+ "ul",+ null,+ ...results+ .filter((r) => !r.passed)+ .map((r) =>+ React.createElement("li", { key: r.name }, `${r.name}: ${r.error}`),+ ),+ ),+ React.createElement("pre", { id: "report", hidden: true }),+ React.createElement("pre", { id: "errors" }),+ React.createElement(+ "div",+ {+ id: "subject",+ key: view.offscreen ? "offscreen" : "visible",+ style: { marginTop: view.offscreen ? 2500 : 24 },+ },+ React.createElement(+ ChatProjectScopeContext.Provider,+ { value: view.project },+ React.createElement(MarkdownTextSource, {+ messageId: view.id ?? "case",+ sourceText: view.text,+ streaming: view.streaming ?? false,+ messageHasRenderableRenderHtmlTool: false,+ }),+ ),+ ),+ ),+ );+}+createRoot(document.getElementById("root")).render(React.createElement(App));diff --git a/tests/studio/fixtures/inline-image-server.mjs b/tests/studio/fixtures/inline-image-server.mjsnew file mode 100644index 00000000000..6eacb8d82f9--- /dev/null+++ b/tests/studio/fixtures/inline-image-server.mjs@@ -0,0 +1,179 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import { execFileSync } from "node:child_process";+import { readFileSync, writeFileSync } from "node:fs";+import path from "node:path";+import { fileURLToPath, pathToFileURL } from "node:url";++const repo = path.resolve(+ path.dirname(fileURLToPath(import.meta.url)),+ "../../..",+);+const frontend = path.join(repo, "studio/frontend");+const output = path.resolve(process.argv[2]);+const baseline = process.argv[3];+const { createServer } = await import(+ pathToFileURL(path.join(frontend, "node_modules/vite/dist/node/index.js"))+);+const images = JSON.parse(+ readFileSync(path.join(output, "images.json"), "utf8"),+);+const client = readFileSync(+ new URL("./inline-image-client.js", import.meta.url),+ "utf8",+);+const csp = JSON.parse(+ readFileSync(path.join(repo, "studio/src-tauri/tauri.conf.json"), "utf8"),+).app.security.csp;+const originals = new Map();+if (baseline) {+ if (!/^[a-f0-9]{40}$/.test(baseline))+ throw new Error("Expected a full baseline commit SHA");+ for (const file of [+ "components/assistant-ui/markdown-text.tsx",+ "lib/markdown-data-images.ts",+ ]) {+ originals.set(+ path.join(frontend, "src", file).replaceAll("\\", "/"),+ execFileSync("git", ["show", `${baseline}:studio/frontend/src/${file}`], {+ cwd: repo,+ encoding: "utf8",+ }),+ );+ }+}+let history = [];+let reportCount = 0;+let preamble = "";+function send(res, status, type, body) {+ res.writeHead(status, { "Content-Type": type });+ res.end(body);+}+const server = await createServer({+ root: frontend,+ configFile: path.join(frontend, "vite.config.ts"),+ cacheDir: path.join(output, baseline ? "cache-before" : "cache-after"),+ server: { host: "127.0.0.1", port: 0, watch: null, hmr: false },+ plugins: [+ {+ name: "inline-image-validation",+ enforce: "pre",+ resolveId(id) {+ if (id === "virtual:inline-image-validation")+ return "\0inline-image-validation";+ },+ load(id) {+ if (id === "\0inline-image-validation") return client;+ return originals.get(id.replaceAll("\\", "/"));+ },+ configureServer(vite) {+ vite.middlewares.use(async (req, res, next) => {+ const url = new URL(req.url, "http://fixture.invalid");+ if (url.pathname === "/inline-fixture/config") {+ send(+ res,+ 200,+ "application/json",+ JSON.stringify({+ variant: baseline ? "before" : "after",+ formats: Object.fromEntries(+ Object.entries(images["32"]).map(([ext, value]) => [+ ext,+ `data:${value.type};base64,${value.data}`,+ ]),+ ),+ dataImage: `data:image/png;base64,${images["32"].png.data}`,+ }),+ );+ } else if (url.pathname === "/inline-fixture/requests") {+ if (req.method === "DELETE") history = [];+ send(res, 200, "application/json", JSON.stringify(history));+ } else if (url.pathname === "/inline-fixture/report") {+ let body = "";+ for await (const chunk of req) body += chunk;+ const report = JSON.parse(body);+ const filename = `report-${baseline ? "before" : "after"}-${++reportCount}.json`;+ writeFileSync(+ path.join(output, filename),+ JSON.stringify(report, null, 2),+ );+ console.log(+ JSON.stringify({+ filename,+ passed: report.passed,+ failed: report.failed,+ userAgent: report.userAgent,+ }),+ );+ send(res, 200, "application/json", "{}");+ } else if (url.pathname === "/inline-fixture/preamble.js") {+ send(res, 200, "text/javascript", preamble);+ } else if (url.pathname === "/inline-images") {+ let html = await vite.transformIndexHtml(+ req.url,+ '<!doctype html><html><head><title>Inline image simulation</title></head><body><div id="root"></div><script type="module" src="/@id/__x00__inline-image-validation"></script></body></html>',+ );+ html = html.replace(+ /<script type="module">([\s\S]*?)<\/script>/,+ (_, source) => {+ preamble = source;+ return '<script type="module" src="/inline-fixture/preamble.js"></script>';+ },+ );+ res.setHeader("Content-Security-Policy", csp);+ send(res, 200, "text/html", html);+ } else if (+ url.pathname.startsWith("/api/inference/sandbox/") ||+ url.pathname === "/assets/fixture.png"+ ) {+ const asset = url.pathname.startsWith("/assets/");+ const authorized =+ req.headers.authorization === "Bearer inline-image-fixture";+ if (!asset) history.push({ path: req.url, authorized });+ if (!asset && !authorized)+ return send(res, 401, "application/json", "{}");+ const filename = decodeURIComponent(url.pathname.split("/").at(-1));+ if (filename === "missing.png")+ return send(res, 404, "application/json", "{}");+ if (filename === "forbidden.png")+ return send(res, 403, "application/json", "{}");+ if (filename === "broken.png")+ return send(res, 200, "image/png", "invalid image");+ const session =+ url.searchParams.get("session") ??+ decodeURIComponent(url.pathname.split("/")[4] ?? "");+ const width =+ {+ "thread-b": 48,+ "project-p1": 64,+ recorded: 80,+ "session/id": 96,+ }[session] ?? 32;+ const ext = filename.split(".").at(-1).toLowerCase();+ const data =+ images[String(width)][ext] ?? images[String(width)].png;+ const respond = () =>+ send(res, 200, data.type, Buffer.from(data.data, "base64"));+ if (filename === "slow.png") setTimeout(respond, 500);+ else respond();+ } else if (/^\/(api|v1|seed|tools)(\/|$)/.test(url.pathname)) {+ send(res, 404, "application/json", "{}");+ } else next();+ });+ },+ },+ ],+});+await server.listen();+const address = server.httpServer.address();+writeFileSync(+ path.join(output, "ready.json"),+ JSON.stringify({+ url: `http://127.0.0.1:${address.port}/inline-images`,+ pid: process.pid,+ }),+);+console.log(`Fixture ready on port ${address.port}`);+for (const signal of ["SIGINT", "SIGTERM"])+ process.on(signal, () => server.close().then(() => process.exit(0)));diff --git a/tests/studio/fixtures/inline-images.md b/tests/studio/fixtures/inline-images.mdnew file mode 100644index 00000000000..169d89dbdc5--- /dev/null+++ b/tests/studio/fixtures/inline-images.md@@ -0,0 +1,21 @@+# Inline image simulations++The fixture mounts the production Markdown renderer with the desktop content security policy. A loopback server supplies generated raster files, checks bearer authentication, and records the requested sandbox scope. It does not start an inference backend.++Before testing, consider path normalization, encoded separators, stale thread or project scope, streaming partial links, failed requests, delayed responses, and browser image decoding. The frontend pipeline test covers 2,100 path and markup combinations. The browser fixture covers 59 cases. The automated runner also downloads a sandbox image, an embedded PNG, and an encoded filename, verifying filenames, dimensions, and that saving does not fetch the sandbox file again.++From the repository root on macOS or Linux:++```sh+uv venv temp/inline-image-validation/venv+uv pip install --python temp/inline-image-validation/venv/bin/python playwright==1.62.0 pillow==12.3.0+export PLAYWRIGHT_BROWSERS_PATH="$PWD/temp/inline-image-validation/browsers"+temp/inline-image-validation/venv/bin/python -m playwright install chromium firefox webkit+temp/inline-image-validation/venv/bin/python tests/studio/playwright_inline_images.py+```++On Windows, use `venv/Scripts/python.exe` and set `PLAYWRIGHT_BROWSERS_PATH` in PowerShell. Install frontend dependencies first with `npm ci` in `studio/frontend`.++Use `--browsers chrome msedge` for installed Chrome and Edge. `--manual` prints a URL for testing installed browsers, including Safari. WebKit automation is engine coverage, not a Safari application test. Each format has an independent browser decode control. If the experimental Windows WebKit build cannot decode AVIF, the test requires the image fallback and records `unsupportedFormats`; required raster codecs must still decode.++To reproduce the original failure, pass `--baseline FULL_COMMIT_SHA --output temp/inline-image-validation/before`. The baseline must lack the relative-image fix. The control expects a bare filename to fail while an explicit sandbox URL loads. Reports, downloads, and failure screenshots stay beneath the selected output directory.diff --git a/tests/studio/playwright_inline_images.py b/tests/studio/playwright_inline_images.pynew file mode 100644index 00000000000..1597f0fe757--- /dev/null+++ b/tests/studio/playwright_inline_images.py@@ -0,0 +1,198 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Isolated browser tests for the production Markdown renderer."""++from __future__ import annotations++import argparse+import base64+import io+import json+import os+import sys+from pathlib import Path+import subprocess+import time++from PIL import Image++from _playwright_robust import stop_process++REPO = Path(__file__).resolve().parents[2]+++def fixtures(output: Path) -> None:+ images = {}+ formats = {+ "png": ("PNG", "image/png"),+ "jpg": ("JPEG", "image/jpeg"),+ "jpeg": ("JPEG", "image/jpeg"),+ "gif": ("GIF", "image/gif"),+ "webp": ("WEBP", "image/webp"),+ "bmp": ("BMP", "image/bmp"),+ "avif": ("AVIF", "image/avif"),+ }+ for width in [32, 48, 64, 80, 96]:+ image = Image.new("RGB", (width, 24), (width * 2, 90, 180))+ images[str(width)] = {}+ for ext, (codec, content_type) in formats.items():+ data = io.BytesIO()+ image.save(data, codec)+ images[str(width)][ext] = {+ "type": content_type,+ "data": base64.b64encode(data.getvalue()).decode(),+ }+ (output / "images.json").write_text(json.dumps(images), encoding = "utf-8")+++def launch_browser(playwright, name):+ if name in ("chrome", "msedge"):+ return playwright.chromium.launch(headless = True, channel = name)+ return getattr(playwright, name).launch(headless = True)+++def main() -> None:+ parser = argparse.ArgumentParser()+ parser.add_argument("--output", type = Path, default = REPO / "temp/inline-image-validation")+ parser.add_argument(+ "--browsers",+ nargs = "+",+ choices = ["chromium", "firefox", "webkit", "chrome", "msedge"],+ default = ["chromium", "firefox", "webkit"],+ )+ parser.add_argument("--probe", action = "store_true")+ parser.add_argument("--baseline")+ parser.add_argument("--manual", action = "store_true")+ args = parser.parse_args()+ output = args.output.resolve()+ if not output.is_relative_to(REPO):+ parser.error("Output must stay inside the repository")+ output.mkdir(parents = True, exist_ok = True)+ # Keep Chromium's Unix socket path below the platform limit.+ runtime = REPO / "temp" / "p"+ runtime.mkdir(parents = True, exist_ok = True)+ os.environ.update({key: str(runtime) for key in ("TMPDIR", "TMP", "TEMP")})+ if args.probe:+ from playwright.sync_api import sync_playwright+ with sync_playwright() as playwright:+ for name in args.browsers:+ browser = launch_browser(playwright, name)+ browser.close()+ return+ fixtures(output)+ ready = output / "ready.json"+ ready.unlink(missing_ok = True)+ command = ["node", str(REPO / "tests/studio/fixtures/inline-image-server.mjs"), str(output)]+ if args.baseline:+ command.append(args.baseline)+ with (output / "server.log").open("w", encoding = "utf-8") as log:+ server = subprocess.Popen(+ command,+ cwd = REPO,+ stdout = log,+ stderr = subprocess.STDOUT,+ start_new_session = os.name != "nt",+ )+ try:+ deadline = time.monotonic() + 60+ while not ready.exists():+ if server.poll() is not None or time.monotonic() > deadline:+ raise RuntimeError((output / "server.log").read_text(encoding = "utf-8"))+ time.sleep(0.1)+ url = json.loads(ready.read_text(encoding = "utf-8"))["url"]+ print(url, flush = True)+ if args.manual:+ server.wait()+ return+ from playwright.sync_api import expect, sync_playwright++ reports = []+ with sync_playwright() as playwright:+ for name in args.browsers:+ browser = None+ page = None+ report = {"browser": name, "platform": sys.platform, "failed": 1, "checks": []}+ errors = []+ try:+ browser = launch_browser(playwright, name)+ page = browser.new_page(+ viewport = {"width": 1280, "height": 800}, device_scale_factor = 1+ )+ page.on("pageerror", lambda error: errors.append(str(error)))+ page.set_default_timeout(30000)+ page.goto(url, wait_until = "networkidle")+ page.get_by_role("button", name = "Run simulations").click()+ page.locator('#report[data-complete="true"]').wait_for(+ state = "attached", timeout = 180000+ )+ report.update(json.loads(page.locator("#report").text_content()))+ report["browser_version"] = browser.version+ if not args.baseline and report["failed"] == 0:+ downloads = []+ for index, (button, filename) in enumerate(+ [+ (None, "plot.png"),+ ("Show embedded download", "Embedded.png"),+ ("Show encoded download", "loss curve #1.png"),+ ]+ ):+ if button:+ page.get_by_role("button", name = button).click()+ ready_image = page.locator('#subject img[data-streamdown="image"]')+ ready_image.wait_for(state = "visible")+ expect(ready_image).to_have_js_property("naturalWidth", 32)+ previous = page.request.get(+ url.replace("/inline-images", "/inline-fixture/requests")+ ).json()+ with page.expect_download(timeout = 10000) as downloaded:+ page.get_by_role("button", name = "Download image").click()+ download = downloaded.value+ assert download.suggested_filename == filename+ destination = output / f"{name}-download-{index}.png"+ download.save_as(destination)+ with Image.open(destination) as saved:+ assert saved.size == (32, 24)+ current = page.request.get(+ url.replace("/inline-images", "/inline-fixture/requests")+ ).json()+ assert (+ current == previous+ ), "Download fetched the sandbox image again"+ downloads.append(filename)+ report["downloads"] = downloads+ report["csp_violations"] = page.locator("#errors").text_content()+ assert not report["csp_violations"], report["csp_violations"]+ assert not errors, errors+ except Exception as error:+ report["failed"] = max(1, report["failed"])+ report["error"] = str(error)+ finally:+ report["page_errors"] = errors+ reports.append(report)+ (output / f"{name}.json").write_text(+ json.dumps(report, indent = 2), encoding = "utf-8"+ )+ print(json.dumps(report), flush = True)+ if report["failed"] and page:+ page.screenshot(+ path = str(output / f"{name}-failure.png"), full_page = True+ )+ if browser:+ browser.close()+ if args.baseline:+ assert all(+ any(not c["passed"] and c["name"] == "path: plot.png" for c in r["checks"])+ and any(c["passed"] and c["name"] == "recorded scope" for c in r["checks"])+ for r in reports+ ), "Baseline controls did not reproduce the bug"+ else:+ assert all(+ r["failed"] == 0 and not r["page_errors"] for r in reports+ ), "Browser simulation failed"+ finally:+ stop_process(server)+++if __name__ == "__main__":+ main()