Repository navigation
Transcript: a sent prompt of megabytes never freezes the chat (literal, collapsed to its head) #29840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transcript: a sent prompt of megabytes never freezes the chat (literal, collapsed to its head) #29840
Changes from 5 commits
46b8c1b
f7fae02
44d19ff
353db48
82d3f2e
415434f
5b14814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // l10n-allow-file: fixture prompts, not shipped UI. | ||
| // A prompt of megabytes (a pasted log, a base64 blob) never freezes the chat that holds it (Leo, | ||
| // 2026-10-31: "this is completely hardlocked bc of message size"). Past HUGE_PROMPT_CHARS it draws as | ||
| // one literal text node (no Markdown, no link scan, no <wbr> per separator), collapsed it draws only | ||
| // its head, and the row estimate is its collapsed lines without lexing or measuring the text. | ||
| import { expect, test } from "bun:test"; | ||
| import { renderToStaticMarkup } from "react-dom/server"; | ||
| import { PromptText, PromptTyped } from "./PromptText"; | ||
| import { InlinePromptContent } from "./InlinePromptContent"; | ||
| import { | ||
| COLLAPSED_PROMPT_LINES, | ||
| HUGE_PROMPT_CHARS, | ||
| HUGE_PROMPT_HEAD_CHARS, | ||
| PROMPT_MORE_HEIGHT, | ||
| isHugePrompt, | ||
| promptHead, | ||
| } from "./promptCollapse"; | ||
| import { layoutConversation } from "../model"; | ||
| import { currentChatMetrics } from "../chatDisplay"; | ||
|
|
||
| const same = (text: string) => text; | ||
| // Base64-like: no spaces, a `/` every 64 characters, so softBreaks would put a <wbr> after each. | ||
| const blob = (chars: number) => | ||
| "QUJD/" | ||
| .concat("A".repeat(59)) | ||
| .repeat(Math.ceil(chars / 64)) | ||
| .slice(0, chars); | ||
| // Markdown-shaped, so a small one would go through the Markdown renderer. | ||
| const markdown = (chars: number) => `# Log\n- item **bold** \`code\`\n${"x".repeat(chars)}`; | ||
|
|
||
| test("a huge typed part draws as one literal text node, with no break points or Markdown", () => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The test title says 'one literal text node', but the assertions only check for absent tags and the outer span. Either assert a single text node or rename the test to match what it checks. Prompt for AI agents |
||
| const text = markdown(HUGE_PROMPT_CHARS); | ||
| const html = renderToStaticMarkup(<PromptTyped text={text} show={same} />); | ||
| expect(html.startsWith('<span class="cv-user__typed">')).toBe(true); | ||
| expect(html).not.toContain("<h"); | ||
| expect(html).not.toContain("<li"); | ||
| expect(html).not.toContain("<wbr"); | ||
| // A small one still renders its Markdown. | ||
| expect(renderToStaticMarkup(<PromptTyped text={markdown(10)} show={same} />)).toContain("<li"); | ||
| }); | ||
|
|
||
| test("a huge sent prompt opens collapsed on its head only", () => { | ||
| const text = blob(5 * 1024 * 1024); | ||
| const html = renderToStaticMarkup(<PromptText rowId="huge" text={text} show={same} />); | ||
| expect(html).toContain("is-clamped"); | ||
| expect(html).not.toContain("<wbr"); | ||
| expect(html.length).toBeLessThan(HUGE_PROMPT_HEAD_CHARS + 1_000); | ||
| // A prompt under the threshold keeps its whole text in the clamp, as before. | ||
| const small = blob(HUGE_PROMPT_CHARS - 1); | ||
| expect(renderToStaticMarkup(<PromptText rowId="small" text={small} show={same} />).length).toBeGreaterThan( | ||
| HUGE_PROMPT_CHARS - 1, | ||
| ); | ||
| }); | ||
|
|
||
| test("a huge prompt's head never ends inside a surrogate pair", () => { | ||
| const text = "a".repeat(9) + "😀" + "b".repeat(10); | ||
| expect(promptHead(text, 10)).toBe("a".repeat(9)); | ||
| expect(promptHead(text, 11)).toBe("a".repeat(9) + "😀"); | ||
| expect(promptHead("short", 10)).toBe("short"); | ||
| expect(isHugePrompt("x".repeat(HUGE_PROMPT_CHARS))).toBe(false); | ||
| expect(isHugePrompt("x".repeat(HUGE_PROMPT_CHARS + 1))).toBe(true); | ||
| }); | ||
|
|
||
| test("a huge prompt's row is estimated as its collapsed lines, quickly", () => { | ||
| const { lineHeight } = currentChatMetrics(); | ||
| const row = (id: string, text: string) => ({ id, version: 1, at: 0, kind: "user", text }); | ||
| const started = performance.now(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: The wall-clock Prompt for AI agents |
||
| const [huge] = [...layoutConversation([row("h", markdown(20 * 1024 * 1024))] as never, 720).heights]; | ||
| const elapsed = performance.now() - started; | ||
| const [long] = [...layoutConversation([row("l", "A line that wraps.\n".repeat(200))] as never, 720).heights]; | ||
| // The same collapsed height as any long prompt: ten lines and Show more. | ||
| expect(huge).toBe(long); | ||
| expect(huge!).toBeGreaterThanOrEqual(COLLAPSED_PROMPT_LINES * lineHeight + PROMPT_MORE_HEIGHT); | ||
| expect(elapsed).toBeLessThan(2_000); | ||
| }); | ||
|
|
||
| test("a huge prompt with placed attachments opens collapsed on its head only", () => { | ||
| const parts = [ | ||
| { kind: "text" as const, text: blob(3 * 1024 * 1024) }, | ||
| { kind: "image" as const, id: "img", mimeType: "image/png", data: "", placed: true as const }, | ||
| { kind: "text" as const, text: "after the image" }, | ||
| ]; | ||
| const html = renderToStaticMarkup( | ||
| <InlinePromptContent rowId="inline-huge" parts={parts as never} show={same} renderImage={() => <i />} />, | ||
| ); | ||
| expect(html).toContain("is-clamped"); | ||
| expect(html).not.toContain("<wbr"); | ||
| expect(html.length).toBeLessThan(HUGE_PROMPT_HEAD_CHARS + 1_000); | ||
| }); | ||
|
|
||
| test("a short Markdown note beside a huge paste keeps its Markdown", () => { | ||
| const note = "- one\n- two"; | ||
| const text = `${note}\n${"y".repeat(HUGE_PROMPT_CHARS + 10)}`; | ||
| const html = renderToStaticMarkup( | ||
| <PromptText rowId="note" text={text} pasted={[{ start: note.length + 1, end: text.length }]} show={same} />, | ||
| ); | ||
| expect(html).toContain("<li"); | ||
| }); | ||
|
|
||
| test("a huge typed part beside a paste opens collapsed on its head", () => { | ||
| const typed = blob(HUGE_PROMPT_CHARS + 10); | ||
| const text = `${typed}\n${"pasted line\n".repeat(5)}`; | ||
| const html = renderToStaticMarkup( | ||
| <PromptText rowId="typed-huge" text={text} pasted={[{ start: typed.length + 1, end: text.length }]} show={same} />, | ||
| ); | ||
| expect(html).toContain("is-clamped"); | ||
| expect(html.length).toBeLessThan(HUGE_PROMPT_HEAD_CHARS + 2_000); | ||
| }); | ||
|
|
||
| test("a huge prompt whose drawn head measures short stays collapsed, never flipping", async () => { | ||
| const { JSDOM } = await import("jsdom"); | ||
| const { act } = await import("react"); | ||
| const { createRoot } = await import("react-dom/client"); | ||
| const dom = new JSDOM("<!doctype html><html><body><main id=root></main></body></html>"); | ||
| const scope = globalThis as Record<string, unknown>; | ||
| const keys = ["window", "document", "navigator", "Node", "HTMLElement", "Element", "IS_REACT_ACT_ENVIRONMENT"]; | ||
| const saved = keys.map((key) => [key, scope[key]] as const); | ||
| Object.assign(scope, { | ||
| window: dom.window, | ||
| document: dom.window.document, | ||
| navigator: dom.window.navigator, | ||
| Node: dom.window.Node, | ||
| HTMLElement: dom.window.HTMLElement, | ||
| Element: dom.window.Element, | ||
| IS_REACT_ACT_ENVIRONMENT: true, | ||
| }); | ||
| // Layout as a browser does it here: 20px lines, each 100 characters one line, so the head that | ||
| // `show` shrinks to a label fits one line and the whole text is thousands. | ||
| const height = (node: Node) => Math.ceil((node.textContent?.length ?? 0) / 100) * 20; | ||
| dom.window.getComputedStyle = (() => ({ lineHeight: "20px" })) as never; | ||
| Object.defineProperty(dom.window.HTMLElement.prototype, "scrollHeight", { | ||
| get(this: HTMLElement) { | ||
| return height(this); | ||
| }, | ||
| }); | ||
| const createRange = dom.window.document.createRange.bind(dom.window.document); | ||
| dom.window.document.createRange = () => { | ||
| const range = createRange(); | ||
| let of: Node | undefined; | ||
| range.selectNodeContents = (node: Node) => void (of = node); | ||
| range.getBoundingClientRect = () => ({ height: of ? height(of) : 0 }) as DOMRect; | ||
| return range; | ||
| }; | ||
| let renders = 0; | ||
| const show = (text: string) => { | ||
| renders++; | ||
| return text.startsWith("data:") ? "[Image]" : text; | ||
| }; | ||
| const uri = `data:text/plain;base64,${"A".repeat(HUGE_PROMPT_HEAD_CHARS + 1_000)}`; | ||
| const text = `${uri}${"\nline".repeat(HUGE_PROMPT_CHARS / 4)}`; | ||
| const host = dom.window.document.getElementById("root")!; | ||
| const root = createRoot(host); | ||
| try { | ||
| act(() => root.render(<PromptText rowId="flip" text={text} show={show} />)); | ||
| expect(host.querySelector(".cv-user__text")!.classList.contains("is-clamped")).toBe(true); | ||
| expect(host.textContent!.length).toBeLessThan(HUGE_PROMPT_HEAD_CHARS); | ||
| expect(renders).toBeLessThan(10); | ||
| } finally { | ||
| act(() => root.unmount()); | ||
| for (const [key, value] of saved) scope[key] = value; | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,49 @@ export function collapsedPromptHeight(height: number, lineHeight: number): numbe | |
| return height > limit + 1 ? limit + PROMPT_MORE_HEIGHT : height; | ||
| } | ||
|
|
||
| /// A prompt longer than this many characters (a pasted log, a base64 blob) draws as literal text in | ||
| /// one text node: no Markdown, no link scan and no break points, each of which costs the main thread | ||
| /// per character or per token, and a prompt of megabytes froze the chat that held it (Leo, | ||
| /// 2026-10-31: "this is completely hardlocked bc of message size"). Collapsed, it draws only its | ||
| /// head (`HUGE_PROMPT_HEAD_CHARS`), far more than ten lines at any width; Show more draws it whole. | ||
| export const HUGE_PROMPT_CHARS = 200_000; | ||
| export const HUGE_PROMPT_HEAD_CHARS = 4_000; | ||
|
|
||
| export const isHugePrompt = (text: string) => text.length > HUGE_PROMPT_CHARS; | ||
|
|
||
| /// A huge prompt's typed parts as its collapsed bubble draws them: the parts within its first `chars` | ||
| /// characters of typed text, the last one cut short, and nothing after. A paste or an attachment is | ||
| /// a chip whatever its length, so it counts nothing. `huge` marks a part cut from a huge one, which | ||
| /// draws literally as its whole text does once open; a short part beside a big paste keeps its | ||
| /// Markdown. | ||
| export function headParts<T extends { kind: string }>( | ||
| parts: readonly T[], | ||
| chars: number, | ||
| typed: (part: T) => string | undefined, | ||
| ): (T & { huge?: boolean })[] { | ||
| const out: (T & { huge?: boolean })[] = []; | ||
| let left = chars; | ||
| for (const part of parts) { | ||
| if (left <= 0) break; | ||
| const text = typed(part); | ||
| if (text === undefined) { | ||
| out.push(part); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Prompt for AI agents |
||
| continue; | ||
| } | ||
| const head = promptHead(text, left); | ||
| out.push(head === text ? part : { ...part, text: head, huge: isHugePrompt(text) }); | ||
| left -= head.length; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /// The first `chars` UTF-16 units of `text`, never ending inside a surrogate pair. | ||
| export function promptHead(text: string, chars: number): string { | ||
| if (text.length <= chars) return text; | ||
| const code = text.charCodeAt(chars - 1); | ||
| return text.slice(0, code >= 0xd800 && code <= 0xdbff ? chars - 1 : chars); | ||
| } | ||
|
|
||
| /// Whether a prompt is likely taller than the collapsed lines, before it is laid out. | ||
| export const looksLongPrompt = (text: string) => | ||
| text.length > LONG_PROMPT_CHARS || text.split("\n", COLLAPSED_PROMPT_LINES + 1).length > COLLAPSED_PROMPT_LINES; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2:
PromptTypedselects literal mode using only this part’s length. A prompt with more than 200,000 typed characters split across smaller parts still parses each part as Markdown or scans it for links and break points when expanded; compute the threshold from aggregate typed text and pass the result to each part.Prompt for AI agents