Skip to content

feat(chat): render on-disk sandbox images in chat markdown - #10361

Merged
Etherll merged 6 commits into
unslothai:mainfrom
CelesteHeartsong:feat/sandbox-markdown-images
Sep 6, 2026
Merged

Etherll merged 6 commits into
unslothai:mainfrom
CelesteHeartsong:feat/sandbox-markdown-images

Conversation

@CelesteHeartsong

Copy link
Copy Markdown
Contributor

TL;DR

A model writes ![plot](/api/inference/sandbox/<sid>/plot.png) into its answer. The src carries no scheme, so sanitize keeps it — but keeping it was never the same as being able to fetch it: the route answers on the Authorization header (_authenticate_header_or_query), a bare <img src> 401s, and the answer renders "Image not available". Only tool cards had an authed fetch; markdown was the one path left.

Scope after rebase (supersedes #10281)

#10281 was closed unmerged because its data-URI half landed on its own as #10269 (a2a4143). This branch is that PR rebased --onto current main with the merged commit dropped, so it now carries only the sandbox-image work: 9 files, +526/−81. No new commits were authored during the rebase; it applied clean.

Fix

The src is re-derived from this chat's scope and fetched with auth into an object URL before it reaches the DOM:

  • sandboxFileForSrc rewrites only scheme-less paths (sanitize dropped every scheme-carrying src already) and discards the sid segment the model happened to write; markdownSandboxImageSrc rebuilds it via sandboxSessionIdFor — project-${projectId} for project threads, else this thread's remoteId.
  • One hook, useSandboxImage, does authFetch → createObjectURL for markdown and the Python card, which had its own copy; abort + revokeObjectURL on cleanup, keyed by url so a stale response can never write state for an old file.
  • Registering img replaces Streamdown's default renderer WHOLESALE (its Components map is keyed by tag), so everything that renderer did is restated: wrapper, hidden-when-failed, fallback text, hover download. downloadName follows it exactly: a real extension on the path wins whole; otherwise any extension in alt is stripped and one inferred from the blob's type is appended (alt="plot" downloads plot.png).

Why the scheme-less src survives sanitize (fact, read off the installed packages)

hast-util-sanitize's safeProtocol returns true when the first : precedes no /-precedes-no-colon case — i.e. when colon < 0 || colon > slash, a scheme-less path is never protocol-checked. The bug was never sanitize; it was the missing auth header.

The two extension lists agree, in two languages

SANDBOX_INLINE_IMAGE_EXTS mirrors _SANDBOX_MEDIA_TYPES (frontend test reads inference.py as text and pins it): .avif joins it (a raster codec; nosniff pins the type either way), .svg stays OUT on purpose — the filename is model-chosen, so inline SVG would be same-origin script execution; non-image extensions stay download cards.

What was deliberately NOT ported

The replaced renderer's gating machinery (decoded state, mount-time complete check, onLoad/onError forwarding, loaded||sized gates): a sandbox image is hidden until the authed fetch lands and fetch state already drives visibility, so nothing here needs a decode gate. What WAS kept — string-level lines with no machinery, pinned by name in tests: data-streamdown="image", the hover overlay div, the download-name regex.

Verification / Tests

  • tsc -b and tsc -p tsconfig.test.json: 0 errors (exit 0, no output). Frontend suite on the rebased branch: 6881 pass / 0 fail. tests/studio/test_tauri_python_tool_images.py: 3 passed. The sandbox-roots backend suite shows pre-existing home-directory failures in this Windows sandbox — byte-identical counts (28F/241P/3S) on untouched origin/main in a throwaway worktree, so they are environmental and not introduced here.
  • Pinned by tests: rewrite-before-DOM and authed-fetch/revoke (markdown-sandbox-image.test.ts); %23 round-trips while a raw # is a fragment delimiter → null; project scope beats threadId (search-images.test.ts).

Follow-up (not blocking)

Upstream, this is one line: Streamdown shipping its own img swap point. If that lands, the wholesale restatement shrinks back to just the resolve-and-fetch step.

A model that writes `![plot](/api/inference/sandbox/<sid>/plot.png)` got "[Image blocked]"
before, and "Image not available" after unslothai#10269: the src carries no scheme, so sanitize keeps it
and safeMarkdownUrl returns it unchanged -- but the route answers on the Authorization header
(_authenticate_header_or_query), so a bare <img src> hits it unauthenticated and 401s. Only
tool cards did the authed fetch (tool-ui-python.tsx), so markdown was the one path left.

Resolve the src through sandboxFilePath(sandboxSessionIdFor(threadId, projectId)) and fetch it
into an object URL before it reaches the DOM: the session segment the model happened to write is
discarded and rebuilt from this chat's scope, so a project thread reads the project's workspace.
One hook, useSandboxImage, now does that for markdown AND for the Python card, which had its own
copy; abort + revokeObjectURL on cleanup in both.

Registering `img` replaces Streamdown's default renderer wholesale (its Components map is keyed by
tag), so everything that renderer did for a data: image is restated: wrapper, hidden-when-failed,
fallback text, hover download. The two features are load-bearing on each other rather than merely
compatible: unslothai#10269's early return for `data:`/`blob:` is exactly why the rewritten src survives the
way back out, and it returns those unchanged, so scheme-carrying sources never reach this path.

.avif joins _SANDBOX_MEDIA_TYPES (a raster codec, and nosniff pins the type either way); .svg stays
out on purpose -- the filename is model-chosen, so inline SVG would be same-origin script execution,
and a non-image extension stays the download card the file list already renders. Tests included.
@Etherll

Etherll commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-06T09:39:21.074893Z ae1d8c1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Etherll Etherll self-assigned this Sep 5, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9c0b4557ae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
CelesteHeartsong and others added 2 commits September 5, 2026 13:33
…n wins, decoded names, avif parity

Three review threads and one manual note, all in the same seam.

The session a src RECORDS wins when it names one (r3941897041). Replay shows the model its own
tool-result stdout, not the envelope -- and stdout carries real workdir paths, so the sid a model
echoes into a URL IS the folder its files were written to. That is exactly what the tool card above
the prose already resolves from (the persisted envelope's sessionId); re-deriving from current scope
was the one place that disagreed with it, and it broke older answers' images after a project move.
sandboxSessionInSrc reads the recorded session back out (path segment, or ?session= under `_`);
markdownSandboxImageSrc falls back to this chat's scope only when nothing was recorded. A bare
outputs/plot.png still takes project-<id> else threadId, exactly as before.

And a dot segment can no longer smuggle a scope: sandboxFileForSrc decodes BEFORE judging (so %2e%2e
is caught too) and returns null on `..` -- one dot pops the prepended scope into another chat's
folder, two land on another route; a bare `.` is dropped as noise.

Download names decode (r3941897043): the tail is split on raw [?#] first, so delimiters keep their
meaning, then decodedSegment'd -- `loss curve #1.png` saves under its real name now.

.avif joins _IMAGE_EXTS (r3941897045), and the frontend pin now pins set(_SANDBOX_MEDIA_TYPES) ==
_IMAGE_EXTS outright: what the route serves inline, what a tool call reports inline, and what the
frontend renders as an <img> are one set in three files and must not drift apart again.

Tests re-pinned for recorded-wins (a moved chat's older answer keeps resolving to the folder named
in its own URL even when projectId is set; bare paths still beat scope from project over threadId),
the `?session=` form round-trips, dot segments fail honestly, and the decode pin rides the restatement.
6881 pass / 0 fail on the frontend suite, tsc -b clean; backend suites byte-identical to main here.
@CelesteHeartsong

Copy link
Copy Markdown
Contributor Author

Review round pushed as 7e1e8a0: recorded session wins when a src names one (moved chats keep their images; bare paths still take current scope), dot segments rejected before they can pop the prepended scope, download names decoded after delimiter split, .avif at parity across all three lists with the equality pinned. Frontend 6881 pass / 0 fail, tsc -b clean; backend suites byte-identical to main on this machine (environmental home-dir failures unchanged).

@danielhanchen

Copy link
Copy Markdown
Member

This registers img in STREAMDOWN_COMPONENTS, which swaps Streamdown's renderer for every image in chat markdown, not just sandbox paths, and the decode and onLoad gating is not carried over. Could you confirm the data-URI images from #10269 and plain sized images still render identically before I review?

@Etherll

Etherll commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e29ac429d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Etherll

Etherll commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: e4af6ab651

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Etherll

Etherll commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae1d8c1815

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Etherll

Etherll commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: ae1d8c1815

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Etherll

Etherll commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Stored sandbox images load through authenticated requests, and returning to a previous image source no longer reuses its revoked URL. In isolated Studio builds using the same synthetic stored-message fixture against the actual app and file endpoint, the base image request returned 401 and displayed no image; this head returned 200 and displayed the 320×180 PNG. Native Chrome and Edge component retry checks pass.

At ae1d8c1, all 6,888 frontend tests, type checks, and the production build pass. The broader backend run has 269 passes and 2 skips, with four Windows-specific failures reproduced on the exact base involving permission bits, open-file unlink, and newlines. Current-head review has no outstanding findings. CI remains queued with no failures observed; other platforms were not run natively.

PR #10361 before and after evidence

Caption: Base ecb20837374c52db082bab179bb90f815d68322f; head ae1d8c18150e4f0e351935cb07cba44c7fa45e7f.

@Etherll
Etherll merged commit df0c5ce into unslothai:main Sep 6, 2026
53 checks passed
LeoBorcherding added a commit to mahiatlinux/unsloth that referenced this pull request Sep 9, 2026
…x images

Main's unslothai#10361 resolves a model-written `![plot](plot.png)` through
sandboxSessionIdFor(threadId, projectId), and this branch made the third
argument, the project's current workspace session, required: changing a
project's working directory rotates it, and the pre-rotation `project-<id>`
answers 410. The merge left that call two arguments short, so the frontend
did not typecheck, and a project chat whose folder had been changed would
have read its images from the folder before the change.

The renderer passes the project row's workspaceSessionId through and holds a
sandbox src until the row has loaded, as the sidebar's file cards do. Test
added.
@CelesteHeartsong
CelesteHeartsong deleted the feat/sandbox-markdown-images branch October 7, 2026 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants