Previews are served by the workspace agent because the files the agent
writes live in the workspace, and the agent already has an HTTP API that
coderd and workspace proxies can reach. Serving from disk on each
request makes a file preview a live view: edits show on reload, and
coderd stores nothing.
The ID is a hash of the path the agent asked for, so a stored
`open_preview` result keeps pointing at the same preview after a
republish, an agent restart, or a change to a symlink in that path. The
model chooses the path, so `os.Root` keeps every request inside it, and
names that start with a dot below it are refused.
<details>
<summary>Implementation details</summary>
The workspace agent can publish an absolute `.html` file, or a directory
containing `index.html`, and serve it from its existing HTTP API (port
4) under `/api/v0/previews/{id}/`. Publishing does not resolve symlinks,
so a path under a symlinked directory such as macOS `/tmp` publishes as
given, which the response returns. The preview ID is the first eight hex
digits of the SHA-256 of that path, so publishing a path again, even
after the agent restarts, returns the same ID. A different path whose ID
is already taken gets a 409. Adds `AgentConn.OpenPreview` to the
workspace SDK, which decodes the ID as `appurl.PreviewID` and returns
`ErrPreviewsUnsupported` when the agent predates the route.
Each request opens the published directory again through `os.Root`, so a
directory that is deleted and rebuilt keeps serving without publishing
it again. Symlinks and junctions are followed only while they stay
inside the published path, only regular files are served, and names that
start with a dot below a published directory, such as `.git` and `.env`,
are refused. Publishing a directory requires `index.html` at its root,
and there are no listings. On Windows, names containing `\`, `:` or `~`
are refused, because Windows reads them as a path separator, an
alternate data stream or a short name such as `HIDDEN~1`. Responses are
`no-store` and `nosniff`.
</details>
**Stack** (review bottom-up). Replaces the closed#31282 stack. Security
review: coder/security-assessments#437.
1. #32438 Agent file previews, this PR
2. #32439 Strict preview hosting and experiment
3. #32522 Preview sign-in
4. #32440 open_preview chat tool
5. #32441 Agents UI
6. #32442 Docs
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>