Skip to content

Fix local Markdown images with shared preview and file-serving routing - #3200

Merged
ymichael merged 1 commit into
mainfrom
bb/markdown-image-routing-for-thread-storage-and-wo-thr_ut8j5aysed
Sep 8, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/markdown-image-routing-for-thread-storage-and-wo-thr_ut8j5aysed

Conversation

@ymichael

@ymichael ymichael commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Markdown image URLs were only routed to host files when a caller supplied linkRouting.localImage. Assistant messages supplied it, but file previews and several other Markdown surfaces did not. Consequently, absolute host paths were requested from the app origin and relative paths were resolved against the SPA URL instead of the Markdown file's directory. For example, docs/report.md containing ![chart](images/chart.png) should load docs/images/chart.png.

What changed

  • Share file-image routing across workspace, project, thread-host, host-scoped, and thread-storage previews. Resolve relative images from the Markdown directory using the existing local-path resolver, and use existing content endpoints for each source.
  • Share lease routing with local skill previews and message routing across assistant, user, generated/system, and thread-context Plugin SDK Markdown. Preserve intentionally suppressed images and explicit caller routing overrides.
  • Preserve existing root restrictions for scoped relative paths and existing trusted absolute-host access. These restrictions are endpoint constraints, not a sandbox for Markdown that can also reference absolute host paths.
  • Consolidate seven backend file-serving handlers through one host-read/error-handling function, retaining their existing target selection, caching, HTML sandbox headers, and size limits.

No public plugin API, CLI, endpoint contract, or server/daemon wire behavior changes; no daemon protocol bump is needed.

How you verified

  • Added rendered-preview regression coverage for absolute and file-relative images, all five preview adapters, root escapes, explicit routing overrides, nested lease paths, user/system messages, Plugin SDK Markdown, and local skill files.
  • pnpm exec turbo run typecheck lint test --filter=@bb/app: 481 test files passed; 3,890 tests passed and 3 skipped. App lint completed with 182 warnings and 0 errors.
  • pnpm exec turbo run typecheck lint --filter=@bb/server: typecheck passed (the server package has no lint task).
  • pnpm exec turbo run test --filter=@bb/server -- test/hosts/daemon-file-response.test.ts test/files/host-file-routes.test.ts test/public/public-projects-local-host.test.ts test/public/public-project-workspace-routing.test.ts test/public/public-thread-data.test.ts: 5 files / 100 tests passed, including byte responses, root propagation, host selection, validators, HTML policies, and error mapping.
  • Rebuilt and started the final code with pnpm start:worktree; confirmed HTTP 200 and daemon connectivity. The dev QA thread has workspace and thread-storage fixtures; the other preview types are covered by automated tests, not completed manual demos.

No linked GitHub issue.

AGENT GENERATED

@ymichael

ymichael commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator Author

@slopcop

@bb-slop-cop

bb-slop-cop Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

🚨 SLOP COP 🚨 · review

I’m reviewing this now. I’ll check the change for security problems, general code quality and architectural duplication, performance regressions, and—where the local setup permits—exercise the Markdown-image flow end to end in a real browser. I’ll post one consolidated review when those passes are complete.

threadId: threadId ?? null,
linkRouting: markdownLinkRouting,
resolveRelativeSrc: (path) => {
if (threadId && source.kind === "working-tree") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — Revision previews resolve images from the live checkout.

For source.kind === "head" or "merge-base", the Markdown body is read through diffFile with the appropriate Git ref, but this branch sends relative images to the ordinary project-content route, which reads the current environment. If an image changed or was deleted between the selected revision and the working tree, the preview shows the wrong bytes or a 404. Please add a raw revision-aware file-content route that preserves EnvironmentFilePreviewSource/ref and cover both head and merge-base assets.

const primaryHost = usePrimaryHost({ enabled: skill !== null });
const previewHostId =
primaryHost?.status === "connected" ? primaryHost.id : null;
const skillFilePreview = useHostFilePreview(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — Server-owned skill images are leased from the primary host.

The catalog also contains bb-builtin, server data-dir bb-user, and BB plugin skills; skill-listing.ts explicitly resolves those files on the server filesystem. Sending their filePath to the primary host works only when server and host happen to share that path, so images fail with a remote primary host. This hook also rereads SKILL.md after creating the lease even though useSkillContent already fetched it. A skill-aware asset endpoint that selects server storage versus the workspace host, paired with a lease-only client query, would fix both the routing gap and duplicate read.

buildMarkdownMessageLinkRouting({
onOpenLink,
onOpenLocalFileLink,
threadId,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — Agent-sent images resolve against the recipient thread.

Agent-origin messages carry sourceThreadId, and the server permits that sender to live in a different project/environment, but this routes image paths through the current/recipient threadId and workspace root. Cross-host messages therefore request the sender's path from the wrong machine (or can display a same-named recipient file). Select the source thread for agent messages and use its worktree route for relative paths; keep recipient routing only for system-generated content. A cross-environment sender test would catch this.

environmentId,
environmentProjectId,
environmentRootPath,
markdownLinkRouting,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — The fresh image resolver defeats Markdown memoization.

ThreadDetailView constructs a new markdownLinkRouting object on each render, so this dependency rebuilds resolvedMarkdownLinkRouting and its resolveSrc closure every time. MarkdownPreview's comparator requires resolveSrc identity, which means an open Markdown document is reparsed during unrelated thread/timeline renders (including streaming). Stabilize the caller routing or derive a stable resolver from primitive route inputs, and add a render-count regression test.

},
(result) => {
const headers = new Headers({
"cache-control": "no-store",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 slopcop/review — Newly routed image responses cannot use their ETags.

The browser flow confirms these image URLs return Cache-Control: no-store alongside an ETag. Now that Markdown previews reuse this lease for potentially many images, toggling/reopening the preview downloads every body again. Keep the strict HTML policy, but consider private, no-cache plus If-None-Match for non-HTML assets (or bounded versioned URLs) so correctness is preserved while unchanged image bytes can be reused.

@bb-slop-cop bb-slop-cop Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🚨 SLOP COP 🚨 · review

Plain-English summary: this PR teaches more Markdown views to load images that live beside local files, instead of asking the web app for paths it cannot serve. It also centralizes several server handlers that read file bytes from a BB host.

The ordinary working-tree flow works end to end, and I found no security issue in the new path handling. I did find three correctness gaps and two performance regressions worth fixing before landing:

  • Historical head and merge-base Markdown previews read the document from the selected Git revision but fetch its images from the live checkout, so changed/deleted assets can be wrong or missing.
  • Skill image leases always target the primary host even though built-in, BB-user, and BB-plugin skills can be server-owned. That breaks when the server and primary host do not share a filesystem; the same path also downloads SKILL.md a second time solely to obtain the lease.
  • Agent-origin messages use the recipient thread/environment for image URLs rather than sourceThreadId, so cross-project or cross-host agent messages can 404 or display the wrong same-named file.
  • File-preview routing now creates a fresh resolveSrc closure on unrelated parent renders, defeating MarkdownPreview's memo comparator and reparsing an open document during timeline updates.
  • The newly reused image endpoints return Cache-Control: no-store. The ETag is therefore not useful to the browser, and reopening a preview redownloads unchanged image bodies.

The server-side serveDaemonFileContent extraction itself looks good: it preserves host/root selection, timeout/retry behavior, response construction, and error remapping. I also scanned the remaining direct host.read_file call sites; they serve distinct JSON/command responsibilities, so I would not broaden this helper mechanically. The clean architectural seam is a source-aware asset route (revision, server-owned skill, or host-owned file) plus a lease-only query, while keeping contained preview paths separate from trusted message routing.

Verification performed at 8daf5ef78b1a1dd469cda14b7d2c892fcc3e545e:

  • App: 7 focused files, 83 tests passed.
  • Server: 5 focused files, 100 tests passed.
  • App and server typechecks passed; git diff --check passed.
  • Current GitHub CI is green (the iOS and Node compatibility matrix jobs are skipped).
  • Real browser: started an isolated source dev app, created a disposable project/thread, opened a Markdown fixture through the BB file panel, toggled Raw → Preview with doobie, and verified two relative PNGs returned 200 with the expected MIME type and byte dimensions. Both remained loaded after a full page reload.
  • The live instance had one local host, so the revision, remote-primary-host, and cross-host sender cases were established from the routing contracts and are not claimed as live passes.

@ymichael
ymichael merged commit 081e2e3 into main Sep 8, 2026
16 checks passed
@ymichael
ymichael deleted the bb/markdown-image-routing-for-thread-storage-and-wo-thr_ut8j5aysed branch September 8, 2026 16:35
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