HydraIssues

HydraBrainServer: the chat shows the model's markdown as raw characters
done unclassified Project: hydra-experiencenet Parent: #659 Reporter: 15 Oct 2026 11:24

Description

Every answer arrives as Markdown and the page prints it unchanged.

Measured on the live service 2026-10-15 (guest chat, cosmic-pretzel-98, qwen3.8-27b Q6_K). The model answered with exactly this text:

# Demo

This is a sentence with **bold**, *italic*, and `inline code`.

- Item one
- Item two

```go
package main
...
```

| Column A | Column B |
| :--- | :--- |

The page shows the hashes, the asterisks, the backticks and the pipes. A table is unreadable. A code block has no box and no horizontal scroll, so long lines wrap into the prose.

Cause: internal/web/assets/app.js writes the answer with element.textContent and app.css keeps the message in white-space: pre-wrap. There is no renderer.

Plan:

  1. Add assets/markdown.js: a pure parser that turns the answer into a node spec, plus a builder that makes DOM nodes with createElement and textContent only.
  2. Render the assistant body through it, both while streaming and at the end. Keep the person's own message as typed, never as Markdown.
  3. Style headings, lists, inline code, fenced code, blockquotes, rules, links and tables for the dark panel. A table and a code block must scroll sideways on a phone instead of widening the page.

Constraints:

  • The page sends Content-Security-Policy script-src 'self', so no CDN library and no inline script. The renderer ships as an asset.
  • No HTML string may ever be built from model text. An answer that contains or an onerror attribute must stay visible text, and a javascript: link must not become a link. This is the whole reason for the spec plus builder split: the builder sets only href, target, rel, class and start, and only on tags it knows.
  • An answer must never fail to appear because the renderer tripped over it.

Acceptance: the demo prompt above renders as a heading, bold and italic text, an inline code chip, two lists, a fenced Go block, a bordered table, a quote and one link, in a signed-out browser at 1280 px and at 390 px, with no horizontal page scroll.

Session Context

Venue
cloud-seven

Comments (1)

api 15 Oct 2026 11:42

Done and live. hydrabrainserver v0.4.0 on pi-node-001, rebuilt in 12s, image digest sha256:077c66ec97bb4b067c1bafaba3d529dc3ecf8a7923036606f94803c83605cfc0.

What changed

assets/markdown.js renders the answer in two halves, and the split is the safety rule:

  • markdownToSpec(text) is pure. It turns the answer into a tree of {t, a, c} nodes and never touches the DOM.
  • buildNode(spec, doc) turns that tree into elements with createElement and text nodes, and holds the only tags and attributes a rendered answer may carry: href, target, rel, class, start.

No HTML string is ever built from model text. The page sends script-src 'self', so the renderer is a served asset, not a CDN library. If the renderer throws, renderMarkdown falls back to plain text, because an answer must still reach the person.

Rendered: headings (an answer's # becomes an h2, a chat answer is not a page), bold, italic, strikethrough, inline code, fenced code with its language, bullet and numbered lists with nesting and a kept start, blockquotes, rules, links and tables with alignment. A table and a code block scroll inside their own box. The person's own message stays as typed and is never read as Markdown. A streaming answer re-renders once per frame, and an open fence renders as code until it closes.

Verified on the live service, signed out, 2026-10-15

  1. The demo prompt from the issue now renders as a heading, a bold run, an italic run, a code chip, two lists, a Go code box, a bordered table, a quote and one link. No stray #, *, backtick or |.
  2. Hostile answer text: the model was asked to return <script>alert(1)</script>, <img src=x onerror=alert(1)> and [click](javascript:alert(1)). All three read as words. document.querySelectorAll(".body script, .body img, .body iframe").length is 0, and the javascript: link did not become a link. Every rendered link carries rel="noopener noreferrer" and target="_blank".
  3. The streaming cursor rides the last block while the answer arrives, and the body loses it when the stream ends.
  4. At 390 CSS px, with the same bytes now served (sha256 of each asset compared against the deployed copy), the page does not scroll sideways: scrollWidth == clientWidth. The code box scrolls inside itself (1124 against 333) and a six column table scrolls in its wrapper (685 against 335).
  5. A second live answer, a runbook with three-space bullets, a list nested under a numbered step and a fenced block inside the nested item, parses into exactly that shape. It is now a case in the test suite.

Tests

internal/web/testdata/markdown_test.js holds the renderer checks, including both live answers and the hostile text. go test ./... runs them through node and skips when node is absent, so run node internal/web/testdata/markdown_test.js by hand before releasing a renderer change. TestAnswersNeverBecomeMarkup fails the build if innerHTML, outerHTML, insertAdjacentHTML, document.write, eval( or new Function appears in the assets, or if index.html stops loading markdown.js before app.js.

README, runbook and testbook updated. Runbook note for operations: a report of "the answer looks like raw Markdown again" means the fallback fired or the asset failed to load, so check the console and GET /assets/markdown.js before touching the model or the provider.