Skip to content

Explain why a file preview failed instead of "Failed to load file" - #3944

Merged
ymichael merged 1 commit into
mainfrom
bb/file-preview-shows-failed-to-load-file-with-no-r-thr_ebwu27jpbd
Sep 19, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/file-preview-shows-failed-to-load-file-with-no-r-thr_ebwu27jpbd

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Reported on Discord: an agent mentions a filename in inline code, bb auto-links it, and clicking a file that is not in the workspace opens a preview panel that says Failed to load file and nothing else — no reason, for any kind of failure.

Two independent defects landed on the same blank message.

Server. GET /environments/:id/diff/file called host.read_file directly and never translated the daemon's error, so a missing file surfaced as 502 ENOENT rather than the 404 every /files/* route already returns. The remap lives inside serveDaemonFileContent, which every route returning a Response uses and therefore gets for free; this route returns JSON, builds its own call, and so silently opted out. It has never had the remap since the route was added in f7d3397 (2026-05-11).

App. resolveSecondaryPanelFilePreviewState classified not-found with error instanceof HttpError && error.status === 404. That was correct when written, but #739 (3d6a61a) moved these queries onto the typed SDK, which throws BbHttpError — structurally identical, different class, so instanceof silently answers false and never type-errors. The not-found branch became unreachable and every failure fell through to a branch that discarded the message.

That second defect is a real regression the report did not mention. For the workspace preview the symptom was unchanged, because the server was already returning 502 — but project file previews hit a route that does return a genuine 404, so they showed File not found. before 2026-07-16 and Failed to load file after. Host file previews regressed the same way in #2005 (9768d55, 2026-08-20). Thread-storage previews still fetch through api.*, which is why this stayed invisible.

What changed

  • apps/server/src/routes/environments.ts — .catch(remapDaemonFileRouteError) on the diff/file read. Missing file → 404, oversized → 413, bad path → 400. No wire change; the daemon protocol is untouched.
  • apps/app/src/lib/http-error.ts (new) — asHttpError / getHttpErrorMessage, covering both HttpError and BbHttpError. mutation-errors.ts had a private copy of the message logic and now imports this one instead (net −20 lines there).
  • apps/app/src/components/secondary-panel/ThreadStorageFilePreview.tsx — the 404 check and the message extraction both go through the shared helpers, so the preview reports the server's reason when there is one and keeps Failed to load file only as a true fallback.
  • apps/app/src/components/secondary-panel/FilePreview.tsx — "Preview not available for <mime>." moves to a new unsupported state. It had been riding on kind: "error", and the old code inferred role="alert" from whether a message existed; once real errors carry messages, every genuine failure would have silently stopped being announced to screen readers.

On surfacing daemon text: with the server fix a missing file is a plain 404 and renders File not found., so the daemon's path string no longer reaches the UI at all. What remains surfaceable are server-authored bodies (file_too_large, invalid_path, host_unavailable) describing host-local state the user already owns. Path traversal was checked — path=/etc/passwd resolves under the workspace root and 404s.

How you verified

Reproduced first, on a built instance from this worktree with a project checkout containing only exists.md. Before the change: 502 {"code":"ENOENT", ...} and a bare Failed to load file. After, in the running app:

Click Result
does-not-exist.md File not found., path shown in the header, no HTTP 404: prefix
too-big.md (30 MB) File size 31457280 bytes exceeds the 25 MB limit
exists.md renders normally

Tests (fail before, pass after — confirmed by reverting asHttpError to HttpError-only, which reproduces expected 'HTTP 404: Path does not exist: /works…' to be 'File not found.', the exact string the browser showed):

  • apps/server/test/public/public-environments.test.ts — 2 tests: ENOENT → 404 with the daemon command asserted, oversized → 413.
  • apps/app/src/components/secondary-panel/FilePreview.test.tsx — 6 tests across both error classes, the generic fallback, and the unsupported state not being an alert.
  • FilePreview.stories.tsx — rows for a failure with a reason and for an unsupported type.

Commands: turbo run typecheck lint test --filter='...[origin/main]'. Typecheck and lint clean (0 errors; the 191 app lint warnings are pre-existing and none are in the touched lines). Nine @bb/integration-tests and eight @bb/server files failed as 60s timeouts under parallel load, across plugins, install scripts, event pruning and shared ports — all unrelated to previews, and all pass when re-run in isolation.

Not addressed

bb auto-links any inline-code token ending in .md without checking it exists. Confirmed, but deliberately left alone: checking would mean a host round trip per filename mention during markdown render, and a file the agent just deleted or is about to create is legitimately mentionable. With the 404 remap in place a dead link now lands on a clear File not found. Worth its own issue if we want resolved-vs-unresolved link styling, ideally off a cached workspace listing rather than a probe per token.

AGENT GENERATED

🤖 Generated with Claude Code

Clicking an auto-linked file that is missing from the workspace showed a
bare "Failed to load file" with no reason.

Two independent causes:

- `GET /environments/:id/diff/file` called `host.read_file` without
  `remapDaemonFileRouteError`, so a daemon ENOENT surfaced as 502 rather
  than the 404 every `/files/*` route already returns. Oversized reads
  had the same problem and now return 413.
- The preview state mapper only recognised 404 on `HttpError`, but this
  preview is fetched through the typed SDK, which throws `BbHttpError`.
  The not-found branch could never match for workspace previews, and the
  error branch dropped the message entirely.

`asHttpError`/`getHttpErrorMessage` now live in `lib/http-error.ts` and
cover both HTTP error classes; `mutation-errors.ts` loses its private
copy. The preview mapper reports the server's reason when it has one,
and "Preview not available for <mime>." moves to a new `unsupported`
state so only real failures are announced as alerts.

Verified in the running app: the missing file now shows "File not
found." with its path, and the raw daemon message no longer reaches the
UI at all.

Co-Authored-By: Claude Opus 5 <[email protected]>
@ymichael
ymichael merged commit c1a64f4 into main Sep 19, 2026
16 checks passed
@ymichael
ymichael deleted the bb/file-preview-shows-failed-to-load-file-with-no-r-thr_ebwu27jpbd branch September 19, 2026 07:36
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