iterate.com / ui

git clone https://git.iterate.com/repos/ui

Agent chat: a picture's row has its height at its first paint

A message with a picture was drawn three times at every mount of its row: a file
chip while its address was signed, a picture of 2 px, then 256 px when it had
loaded (130 > 106 > 360 px, measured on the Dash's /agents/main feed). The list
trusts a row's first measurement and corrects no later one while the reader goes
up, so each picture moved the view by 254 px.

Now a picture's box is there from the first paint, at the picture's height (256 px
until this page has seen the picture once), and the page keeps an attachment's
signed address and a picture's size by path, so a row that mounts again signs
nothing and is drawn at once at its size.

Session: claude e7bfeefe-1067-494b-8f63-82177cb1beb9 on beelink
Via: /agents/claude/dash-scroll-stability-mwzlcham-9677dd

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0158EjAGDfCugWqZ9a2JTTgL

Jonas Templestein, 2026-11-28 00:05 UTC · b6d1631f33dfebe4e016531fb0a87230f4a9e5d6 · parent ea4f24f

2 files changed

A src/components/agent-chat/agent-feed-attachment.test.tsx
@@ -0,0 +1,58 @@
+// @vitest-environment jsdom
+// A picture's row has its height at its first paint: the list trusts a row's first measurement.
+import { act } from "react";
+import { createRoot } from "react-dom/client";
+import { renderToStaticMarkup } from "react-dom/server";
+import { expect, test } from "vitest";
+
+import { attachmentBoxHeight, UserMessageBody } from "./agent-feed.tsx";
+
+const picture = (path: string) => ({
+  kind: "user" as const,
+  id: path,
+  offset: 1,
+  text: "look",
+  timestampMs: 0,
+  files: [{ contentType: "image/png", filename: "shot.png", path, size: 10 }],
+});
+
+test("a picture's box is 256 px high until its size is known, then its own height at most 256", () => {
+  expect(attachmentBoxHeight({})).toBe(256);
+  expect(attachmentBoxHeight({ width: 400, height: 91 })).toBe(91);
+  expect(attachmentBoxHeight({ width: 4000, height: 3000 })).toBe(256);
+});
+
+test("before its address is signed a picture already has its box, and no file chip", () => {
+  const markup = renderToStaticMarkup(
+    <UserMessageBody item={picture("/files/a.png")} signedUrl={() => new Promise(() => {})} />,
+  );
+  expect(markup).toContain('role="img"');
+  expect(markup).toContain("height:256px");
+  expect(markup).not.toContain("<img");
+});
+
+test("a row that mounts again signs nothing and draws the picture at the size it had", async () => {
+  (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+  let signings = 0;
+  const signedUrl = (path: string) => {
+    signings += 1;
+    return Promise.resolve(`https://files.test${path}?signed`);
+  };
+  const item = picture("/files/b.png");
+  const host = document.createElement("div");
+  const root = createRoot(host);
+  await act(async () => root.render(<UserMessageBody item={item} signedUrl={signedUrl} />));
+  const first = host.querySelector("img")!;
+  expect(first.style.height).toBe("256px");
+  // the picture loads: the page learns its size
+  Object.defineProperty(first, "naturalWidth", { value: 400 });
+  Object.defineProperty(first, "naturalHeight", { value: 91 });
+  await act(async () => void first.dispatchEvent(new Event("load")));
+  expect(host.querySelector("img")!.style.height).toBe("91px");
+  await act(async () => root.render(null));
+
+  const again = renderToStaticMarkup(<UserMessageBody item={item} signedUrl={signedUrl} />);
+  expect(again).toContain("<img");
+  expect(again).toContain("height:91px");
+  expect(signings).toBe(1);
+});
M src/components/agent-chat/agent-feed.tsx
@@ -1172,6 +1172,29 @@
   );
 }
 
+/** A PICTURE'S ROW HAS ITS HEIGHT AT ITS FIRST PAINT. Measured on the Dash's /agents/main feed
+ *  (27 Nov 2026): a message with a picture was drawn three times: with a file chip (its address
+ *  not yet signed), then with a picture of 2 px, then at 256 px when the picture had loaded:
+ *  130 > 106 > 360 px, at every mount of the row. The list trusts a row's first measurement, and
+ *  corrects no later one while the reader goes up, so each picture moved all that the reader saw
+ *  by 254 px. So: a picture's box is there from the first paint, as high as the picture will be
+ *  (`ATTACHMENT_MOST_PX` until this page has seen it once), and what the page learned of an
+ *  attachment (its signed address, a picture's size) is kept by path for the next mount. */
+const ATTACHMENT_MOST_PX = 256;
+const attachmentsSeen = new Map<
+  string,
+  { url?: string; signing?: Promise<string>; width?: number; height?: number }
+>();
+function seenAttachment(path: string) {
+  let seen = attachmentsSeen.get(path);
+  if (!seen) attachmentsSeen.set(path, (seen = {}));
+  return seen;
+}
+/** The height of a picture's box: its own, at most `ATTACHMENT_MOST_PX`; that most while unknown. */
+export function attachmentBoxHeight(size: { width?: number; height?: number }): number {
+  return size.height ? Math.min(ATTACHMENT_MOST_PX, size.height) : ATTACHMENT_MOST_PX;
+}
+
 function MessageAttachment({
   file,
   signedUrl,
@@ -1179,25 +1202,66 @@
   file: AgentUiFileAttachment;
   signedUrl: SignedUrl;
 }) {
-  const [url, setUrl] = useState<string>();
+  const seen = seenAttachment(file.path);
+  const [url, setUrl] = useState<string | undefined>(seen.url);
+  const [size, setSize] = useState({ width: seen.width, height: seen.height });
   useEffect(() => {
     let live = true;
-    signedUrl(file.path).then(
-      (signed) => live && setUrl(signed),
-      () => undefined,
+    const kept = seenAttachment(file.path);
+    if (kept.url) {
+      setUrl(kept.url);
+      return;
+    }
+    // one signing for each path, also when several rows show it or a row mounts again
+    kept.signing ??= signedUrl(file.path);
+    kept.signing.then(
+      (signed) => {
+        kept.url = signed;
+        if (live) setUrl(signed);
+      },
+      () => void (kept.signing = undefined),
     );
     return () => void (live = false);
   }, [file.path, signedUrl]);
-  if (file.contentType.startsWith("image/") && url)
+  if (file.contentType.startsWith("image/")) {
+    const box = {
+      height: attachmentBoxHeight(size),
+      ...(size.width &&
+        size.height && { aspectRatio: `${String(size.width)} / ${String(size.height)}` }),
+    };
+    const className = "max-w-full rounded-lg border border-border/60 bg-background object-contain";
+    if (!url)
+      return (
+        <div
+          role="img"
+          aria-label={file.filename}
+          style={{ ...box, width: box.height }}
+          className={className}
+        />
+      );
     return (
       <a href={url} target="_blank" rel="noreferrer" className="block max-w-full">
         <img
           src={url}
           alt={file.filename}
-          className="max-h-64 max-w-full rounded-lg border border-border/60 bg-background object-contain"
+          style={box}
+          className={className}
+          onLoad={(event) => {
+            const { naturalWidth: width, naturalHeight: height } = event.currentTarget;
+            if (!width || !height) return;
+            Object.assign(seenAttachment(file.path), { width, height });
+            if (width !== size.width || height !== size.height) setSize({ width, height });
+          }}
+          onError={() => {
+            // a signed address ends: the next mount signs again
+            const kept = seenAttachment(file.path);
+            kept.url = undefined;
+            kept.signing = undefined;
+          }}
         />
       </a>
     );
+  }
   return (
     <a
       href={url}