From 6c0c813c2c63711b506c211285674f7f19fd62d5 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 19 Oct 2026 21:41:41 +0100 Subject: [PATCH] web: render markdown stage bodies as prose The turn protocol writes a markdown body; the run detail now renders a field as markdown when it has markdown structure (heading, fence, list, table) and keeps the honest monospace block for historical plain-text fields. Pure helper with tests. --- web/run-detail.css | 6 ++++++ web/src/runDetail.js | 12 ++++++++++++ web/src/views/runDetail.js | 26 ++++++++++++++++++++++---- web/test/runDetail.test.mjs | 12 ++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/web/run-detail.css b/web/run-detail.css index 66b55cb2..bc2bdde2 100644 --- a/web/run-detail.css +++ b/web/run-detail.css @@ -263,3 +263,9 @@ .diff-add { color: var(--ok); } .diff-remove { color: var(--bad); } .diff-context { color: var(--muted); } + +/* A markdown stage body (the turn protocol) is prose, not a code block: keep the + app's reading measure and its natural spacing. */ +.field-markdown { max-width: var(--layout-measure); padding: 2px 0; } +.field-markdown > :first-child { margin-top: 0; } +.field-markdown > :last-child { margin-bottom: 0; } diff --git a/web/src/runDetail.js b/web/src/runDetail.js index 83779ea8..fffde545 100644 --- a/web/src/runDetail.js +++ b/web/src/runDetail.js @@ -592,6 +592,18 @@ export function clampVisitIndex(index, count) { * artifacts section always shows for a reached visit. Mirrors * `stageSectionSpecs`. */ +/** + * Whether a recorded field value is markdown rather than plain text: a heading, a + * fenced block, a bullet/ordered list or a table. The new stage turns write + * markdown bodies (`# outcome` …), while historical steps store plain maps; this + * keeps the old ones rendering as code. + */ +export function looksLikeMarkdown(value) { + const text = typeof value === "string" ? value : ""; + if (!text) return false; + return /^#{1,6}\s/m.test(text) || /```/.test(text) || /^\s*(?:[-*]|\d+\.)\s+/m.test(text) || /^\|.+\|/m.test(text); +} + export function stageSectionSpecs(stageKey, graph, runSteps, visitIndex, runContext) { const specs = []; const stage = (graph?.stages ?? []).find((candidate) => candidate?.key === stageKey); diff --git a/web/src/views/runDetail.js b/web/src/views/runDetail.js index 39ff2674..d50aea8a 100644 --- a/web/src/views/runDetail.js +++ b/web/src/views/runDetail.js @@ -31,6 +31,7 @@ import { verdictReasonSegments, outcomeColorToken, stateGlyph, + looksLikeMarkdown, } from "../runDetail.js"; import { ArtifactKind, @@ -261,8 +262,27 @@ function renderCodeBlock(label, text) { } /** One stage-pane field as either the code card (blocks) or a key/value row. */ +/** + * One recorded field value: a markdown body (the new stage turns) is rendered as + * markdown; anything else stays an honest monospace block. + */ +function renderFieldValue(value) { + const text = typeof value === "string" ? value : String(value ?? ""); + if (looksLikeMarkdown(text)) { + return h("div", { class: "field-markdown" }, rt.views.chat.renderMarkdown(text)); + } + return h("pre", { class: "field-block" }, h("code", { text })); +} + function renderStageField(field) { - if (field.block) return renderCodeBlock(field.label, field.value); + if (field.block) { + return h( + "div", + { class: "codeblock" }, + h("div", { class: "codeblock-label", text: field.label }), + renderFieldValue(field.value), + ); + } return h( "div", { class: "kv-row" }, @@ -282,9 +302,7 @@ function renderRunFieldTab(map, keyPrefix, emptyMessage) { for (const section of sections) { const key = `${keyPrefix}-${section.key}`; wrap.append( - renderCollapsible(key, section.title, () => - h("pre", { class: "field-block" }, h("code", { text: section.value })), - ), + renderCollapsible(key, section.title, () => renderFieldValue(section.value)), ); } return wrap; diff --git a/web/test/runDetail.test.mjs b/web/test/runDetail.test.mjs index aa8d1300..e8fe2d92 100644 --- a/web/test/runDetail.test.mjs +++ b/web/test/runDetail.test.mjs @@ -8,6 +8,7 @@ import assert from "node:assert/strict"; import { RUN_DETAIL_TABS, + looksLikeMarkdown, RENDERED_RUN_TABS, EMPTY_INPUT_MESSAGE, EMPTY_OUTPUT_MESSAGE, @@ -608,3 +609,14 @@ test("outcomeColorToken uses the status palette then the flow palette", () => { assert.equal(stateGlyph("succeeded"), "✓"); assert.equal(stateGlyph("nonsense"), "?"); }); + +test("looksLikeMarkdown recognises stage bodies and leaves legacy maps alone", () => { + assert.equal(looksLikeMarkdown("# outcome\nsuccess\n\n# notes\nDone."), true); + assert.equal(looksLikeMarkdown("Some text\n\n- one\n- two"), true); + assert.equal(looksLikeMarkdown("```js\nconst a = 1;\n```"), true); + assert.equal(looksLikeMarkdown("| a | b |\n| --- | --- |\n| 1 | 2 |"), true); + // Historical field text is a plain map, not markdown. + assert.equal(looksLikeMarkdown("summary: ok\nfindings: none"), false); + assert.equal(looksLikeMarkdown(""), false); + assert.equal(looksLikeMarkdown(null), false); +});