Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(chat): space blocks inside inline visualizations

zszz30ad7b68e4a25
diff --git a/apps/desktop/resources/gen-ui/visualize.css b/apps/desktop/resources/gen-ui/visualize.css
index eea78ec52..408fc589e 100644
--- a/apps/desktop/resources/gen-ui/visualize.css
+++ b/apps/desktop/resources/gen-ui/visualize.css
@@ -310,6 +310,30 @@ code:not(pre code) {
background: transparent !important;
}
+/*
+ * Vertical rhythm. PI-Desktop writes the fragment straight into the body (there
+ * is no #widget wrapper), and the skill asks for one root element with a unique
+ * id, so neither level spaced its blocks: headings, controls, charts, legends,
+ * and captions touched. Stack the blocks of the body and of a sole root container
+ * with the #widget gap. Zero specificity, so a fragment that lays out its own
+ * root (any display or gap) keeps it. Only a container with a block child is
+ * stacked, so a root of inline controls keeps flowing inline. Scripts, styles,
+ * and out-of-flow nodes such as the tooltip are not flex items and take no gap.
+ */
+:where(
+ :is(
+ body,
+ body:not(:has(> :not(script, style, template, .tooltip) ~ :not(script, style, template, .tooltip)))
+ > :is(div, section, article, main, form)
+ ):has(
+ > :is(div, p, section, article, header, footer, nav, figure, form, fieldset, details, table, ul, ol, dl, pre, blockquote, hr, h1, h2, h3, h4, h5, h6, svg, canvas)
+ )
+ ) {
+ display: flex;
+ flex-direction: column;
+ gap: 12px;
+}
+
.card {
min-width: 0;
padding: 12px;
diff --git a/docs/project/unreleased.md b/docs/project/unreleased.md
index 1cf4862a9..8343605cf 100644
--- a/docs/project/unreleased.md
+++ b/docs/project/unreleased.md
@@ -27,3 +27,9 @@
card now says so and shows the error instead of staying blank, and the
model's write result flags syntax errors and missing chart containers so it
can fix them with one edit.
+- Fixed cramped inline visualizations. A visualization's title, controls,
+ chart, legend, and captions no longer touch: top-level blocks, and the
+ blocks inside a single wrapping root element (the shape most visualizations
+ use), are now spaced 12px apart. Visualizations that lay out their own root
+ keep their layout, and existing cards pick up the spacing when they next
+ load.
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 7bd8a6909..94a2840e8 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -19304,7 +19304,7 @@ their separate platform acceptance.
including real protocol responses, bundled assets, the preloaded `viz`
helpers (`viz.chart` sizing, `viz.render` charts and their date, year,
re-render, and donut edge cases), a fragment script error shown in the
- card, focus/guest Escape,
+ card, block spacing inside a single wrapping root, focus/guest Escape,
viewport release, and unsandboxed callers through a real isolated Host.
Native directory, FIFO, and ABA regressions run in `visualizations/files`;
`gen-ui-file-safety.test.mjs` validates the private Host-to-Main boundary.
diff --git a/scripts/e2e/gen-ui-main.mjs b/scripts/e2e/gen-ui-main.mjs
index b0743a612..22181278a 100644
--- a/scripts/e2e/gen-ui-main.mjs
+++ b/scripts/e2e/gen-ui-main.mjs
@@ -48,6 +48,10 @@ async function run() {
"<p>CROSS_SESSION_PRIVATE_MARKER</p><script>parent.postMessage({type:'fixture:loaded'},'*')</script>");
await writeFile(join(sessionDir, "broken.html"),
`<div id="chart"></div><script>viz.render("missing-chart", { mark: "bar", data: [{ k: "a", v: 1 }], x: "k", y: "v" });</script>`);
+ // The usual model shape: one id'd root wrapping title, controls, stage, and
+ // caption, with the script beside it.
+ await writeFile(join(sessionDir, "stack.html"),
+ `<div id="stack-root"><h3>Title</h3><div class="viz-controls"><button class="btn" type="button">Next</button></div><div style="height:40px"></div><p>Caption</p></div><script>parent.postMessage({type:'fixture:loaded'},'*')</script>`);
await writeFile(join(sessionDir, "attacker.html"), `<p>attacker</p><script>addEventListener('message',event=>{if(event.data!=='attack')return;try{parent.postMessage({type:'cross-read',stolen:parent.frames[1].document.querySelector('p').textContent},'*')}catch(error){parent.postMessage({type:'cross-read',error:error.name},'*')}});parent.postMessage({type:'fixture:loaded'},'*')</script>`);
await writeFile(join(sessionDir, "limit.html"), Buffer.alloc(GEN_UI_MAX_FRAGMENT_BYTES, 0x20));
const limit = await readFragment({ sessionId: "probe_session", fileName: "limit.html" });
@@ -176,6 +180,20 @@ async function run() {
assert.equal(await host('document.querySelectorAll(".gen-ui-card iframe").length'), 1);
checks.push("fragment script errors show in the card");
+ // Blocks inside a single wrapping root used to touch (no gap at either
+ // level); the kit now stacks them, and a root that sets its own layout
+ // keeps it.
+ const stack = message(0).replace("chart0.html", "stack.html");
+ await host(`window.genUiProbe.mount(${JSON.stringify(stack)});window.genUiProbe.waitLoaded(1)`);
+ const stackGuest = guests()[0];
+ const gaps = 'new Promise((resolve)=>requestAnimationFrame(()=>{const boxes=Array.from(document.getElementById("stack-root").children,(el)=>el.getBoundingClientRect());resolve(boxes.slice(1).map((box,index)=>Math.round(box.top-boxes[index].bottom)))}))';
+ const stacked = await stackGuest.executeJavaScript(gaps);
+ assert.equal(stacked.length, 3);
+ for (const gap of stacked) assert.ok(gap >= 8, `blocks in a wrapping root are spaced: ${stacked}`);
+ const own = await stackGuest.executeJavaScript(`document.getElementById("stack-root").style.display="block";${gaps}`);
+ assert.deepEqual(own, [0, 0, 0]);
+ checks.push("blocks in a wrapping root are spaced; fragment layout wins");
+
window.setContentSize(900, 2600);
const all = Array.from({ length: 8 }, (_, index) => message(index)).join("\n");
await host(`window.genUiProbe.mount(${JSON.stringify(all)});window.genUiProbe.waitLoaded(8)`);