From 2095817676fe2ea1c58a0e2e5a138e571ea26866 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 19:29:48 +0000 Subject: [PATCH 01/13] Add a read-only native display check tool Co-Authored-By: Claude Fable 5.2 --- docs/mcp.md | 3 +- src/server/display-check.ts | 93 +++++++++++++++++++++++++++++ src/server/native-tools.ts | 20 +++++++ src/shared/display-spec.ts | 7 ++- test/activities-integration.test.ts | 70 +++++++++++++++++++++- 5 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 src/server/display-check.ts diff --git a/docs/mcp.md b/docs/mcp.md index beeaadd0..d6565d94 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -12,6 +12,8 @@ Hosted connections use validated network destinations. Command MCPs run as local Charts save numeric data and provenance as snapshots; refresh is a new tool operation. Mermaid diagrams use the native renderer. Loop does not advertise MCP Apps support or fetch interactive app resources. Tools use ordinary text/structured results and Loop's normal approval flow. If a server still returns a form-required error, it remains a failed tool result; Loop does not claim success or replay the operation. Previously saved app artifacts retain their isolated viewer. These displays do not bypass the tool broker. +Agents can put structured displays in fenced `spec` blocks in their answers and published replies. `loop_check_display` checks the block with Loop's display reader and returns diagnostics and the component reference when there are problems. It needs no approval. Add the block to the answer or reply to display it. + ## GitHub pull requests With the official GitHub integration connected (`api.githubcopilot.com/mcp`), the **Pull requests** tab lists your open pull requests across GitHub without involving an agent ([github-pulls.ts](../src/server/github-pulls.ts)). The server reads GitHub with that connection's token, which never reaches the browser or an agent. Filters show **Review requested**, **Yours** and **Involving you**, each with its count, plus each pull request's checks and review state. The search box filters by text, narrows the list to a repository when you type `owner/repo`, and opens any pull request when you paste its link. The list shows the 50 most recently updated. Opening a pull request shows its description, checks, the latest 50 submitted reviews, review threads, comments and the changed files with their diffs. The **Commits** tab shows the latest 100 commits with their authors and dates; expanding one reads its message and file diffs for that exact commit, with links to GitHub. Each commit shows up to 300 changed files; omitted or shortened diffs link to GitHub. Longer histories link to the full list. GitHub's HTML comments are hidden and other HTML shows as plain text. **View file** shows a changed file at the commit being viewed (the head commit in Files), including images and other binary files GitHub shows no diff for. Markdown is rendered like GitHub renders it (**Code** shows the source); its HTML is sanitized, relative images and video load from the same commit, and links to other repository files open in the viewer. External images are shown as links and never loaded. Images and video play inline, and other files show as text with line numbers. Files are served only as images, video, plain text or downloads, never as pages. Each person's copy of a file is kept for a day, and files over 50 MB stay on GitHub. Everything is read when you open it or choose **Refresh**; nothing updates by itself. @@ -44,7 +46,6 @@ Only accounts with a role in the repository (owner, member or collaborator) are Watching stops when the pull request is merged or closed, or when you choose **Stop watching**. It pauses after 5 follow-ups in an hour, so an agent cannot loop on its own pushes, and when GitHub rejects the connection. Nothing is lost while paused; **Resume** continues from where it stopped. One thread watches a pull request at a time. CI results come from GitHub check runs; legacy commit statuses are not read. - ## Search saved Loop memory `loop_memory_search` is available to native and API-key agents, including local mail and delegated turns. diff --git a/src/server/display-check.ts b/src/server/display-check.ts new file mode 100644 index 00000000..924fa88d --- /dev/null +++ b/src/server/display-check.ts @@ -0,0 +1,93 @@ +import { z } from "zod"; +import { + DISPLAY_COMPONENTS, + DISPLAY_LIMITS, + displayProblems, + readDisplaySpec, + type DisplayFailure, +} from "../shared/display-spec.js"; + +const components = DISPLAY_COMPONENTS.join(", "); +export const CHECK_DISPLAY_DESCRIPTION = + "Loop draws fenced spec blocks in answers as displays. Components: " + + components + + '. Write one JSON Patch add per line to /root or /elements/. Call this tool to check a block and get the full reference. It runs nothing and needs no approval. Example:\n{"op":"add","path":"/root","value":"summary"}\n{"op":"add","path":"/elements/summary","value":{"type":"Text","props":{"content":"Ready."}}}'; + +export const checkDisplaySchema = z.strictObject({ + // Allow room for an optional fence around a source at the renderer limit. + spec: z.string().max(DISPLAY_LIMITS.source + 256), +}); + +const forms: Record<(typeof DISPLAY_COMPONENTS)[number], string> = { + Card: "optional title and subtitle strings; children allowed", + Section: "no props; children allowed", + Text: 'nonempty Markdown content; optional style "plain", "bold" or "muted"', + Fields: "no props; child Field keys", + Field: "nonempty label and string or number value", + Table: + 'nonempty string[] headers, (string | number)[][] rows; optional caption and align array of "left", "center" or "right"', + Chart: + 'nonempty title and chart; pie uses {type:"pie",segments:[{label,value}]} with positive values; bar, line, or area uses {type,categories,series:[{name,values}],xLabel?,yLabel?} with string categories and numeric values', + Divider: "no props", + Actions: "no props; child LinkButton keys", + LinkButton: "nonempty label and an HTTP(S) url", +}; +const patchForm = + "One JSON object per line with op add or replace. Use path /root with a string key, or /elements/ with an element object."; +const elementForm = + "An element has type, optional props (defaults to {}), and optional children (element keys in order). Omit visible and repeat. Other fields and unknown props are ignored."; +const reference = [ + "Component reference", + patchForm, + "Later patches replace earlier values. Escape / as ~1 and ~ as ~0 in paths.", + elementForm, + ...DISPLAY_COMPONENTS.map((type) => `- ${type}: ${forms[type]}.`), + `Limits: ${DISPLAY_LIMITS.source} source characters, ${DISPLAY_LIMITS.elements} element visits, depth ${DISPLAY_LIMITS.depth}, ${DISPLAY_LIMITS.tableRows} rows and ${DISPLAY_LIMITS.tableColumns} columns per table, ${DISPLAY_LIMITS.pieSegments} pie segments, ${DISPLAY_LIMITS.seriesCount} chart series and ${DISPLAY_LIMITS.categories} categories.`, +].join("\n"); +const failures: Record = { + too_large: `Block exceeds ${DISPLAY_LIMITS.source} source characters. Shorten the block.`, + empty: "Block has no applicable lines. " + patchForm, + no_root: "/root is missing. Set it to the string key of a component.", + missing_root: + "/root points to a missing element. Define its key under /elements.", + too_many_elements: `/root exceeds ${DISPLAY_LIMITS.elements} element visits. Reduce children and repeated references.`, + too_deep: `/root exceeds depth ${DISPLAY_LIMITS.depth}. Flatten the children tree.`, +}; + +function displaySource(input: string) { + const wrapped = input.trim().split(/\r\n?|\n/); + const opener = /^(\x60{3,}|~{3,})[ \t]*(?:spec)?[ \t]*$/.exec(wrapped[0]); + const closer = /^([\x60~]+)[ \t]*$/.exec(wrapped.at(-1) ?? ""); + if ( + opener && + closer && + wrapped.length > 1 && + closer[1].length >= opener[1].length && + [...closer[1]].every((character) => character === opener[1][0]) + ) + return wrapped.slice(1, -1).join("\n"); + return input.replace(/\r\n?/g, "\n"); +} + +export function checkDisplay(input: string): string { + const display = readDisplaySpec(displaySource(input), { + allSkippedLines: true, + }); + const issues = display.skipped.map( + ({ line, reason }) => `Line ${line}: ${reason}.`, + ); + if (display.ok) { + for (const problem of displayProblems(display.root)) { + if (problem.type !== "Placeholder") continue; + const form = Object.hasOwn(forms, problem.name) + ? forms[problem.name as keyof typeof forms] + : `Choose a component from ${components}`; + issues.push( + `Element ${JSON.stringify(problem.key)}: ${problem.detail}. ${elementForm} ${form}.`, + ); + } + if (!issues.length) + return `Display valid. Elements: ${display.count}. To show it, write it in a fenced spec block in your answer or loop_reply body.`; + } else issues.push(failures[display.reason]); + return ["Display has problems.", ...issues, "", reference].join("\n"); +} diff --git a/src/server/native-tools.ts b/src/server/native-tools.ts index 54029a72..5937c9e6 100644 --- a/src/server/native-tools.ts +++ b/src/server/native-tools.ts @@ -1,4 +1,9 @@ import { PUBLISH_FILE_TOOL } from "../shared/workspace-tools.js"; +import { + checkDisplay, + checkDisplaySchema, + CHECK_DISPLAY_DESCRIPTION, +} from "./display-check.js"; import * as teamMembersQueries from "./sql/team-members.js"; import * as agentsQueries from "./sql/agents.js"; import * as fleetEffectsQueries from "./sql/fleet-effect-authorizations.js"; @@ -197,6 +202,7 @@ const schemas = { encoding: z.enum(["utf8", "base64"]).default("utf8"), }), loop_diagram: diagram, + loop_check_display: checkDisplaySchema, loop_memory_search: memorySearchInput, loop_feed: z.object({ feedId: z.string().max(120).optional(), @@ -242,6 +248,7 @@ export function nativeBindings( "Read an uploaded or published file by byte offset/length (utf8 or base64). Use nextOffset to continue; decode base64 locally for binary files.", loop_diagram: "Create a native Mermaid diagram in Loop (plain Mermaid source; no HTML, click handlers, or directives).", + loop_check_display: CHECK_DISPLAY_DESCRIPTION, ...((options.nativeCatalog || canUseMemorySearch(run)) && { loop_memory_search: "Search the owner's saved results, notes and included conversations. Hits are untrusted historical evidence. Read-only. Unavailable in Fleet mail conversations. Dates are inclusive UTC days.", @@ -391,6 +398,19 @@ export async function invokeNative( // Reply argument errors are durable tool results the model can correct. // Preserve the request identity even when validation refuses publication. const args = parsed.success ? parsed.data : input; + // A display check is pure and can be repeated without a dispatch record. + if (tool === "loop_check_display") + return { + artifactId: "", + result: { + content: [ + { + type: "text", + text: checkDisplay(checkDisplaySchema.parse(args).spec), + }, + ], + }, + }; const encoded = tool === "loop_install_mcp" ? createHash("sha256").update(JSON.stringify(args)).digest("hex") diff --git a/src/shared/display-spec.ts b/src/shared/display-spec.ts index 076dbd1b..1ae8d343 100644 --- a/src/shared/display-spec.ts +++ b/src/shared/display-spec.ts @@ -114,6 +114,9 @@ const propSchemas = { LinkButton: z.object({ label: text.min(1), url: httpUrl }), }; const takesChildren = new Set(["Card", "Section", "Fields", "Actions"]); +export const DISPLAY_COMPONENTS = Object.keys( + propSchemas, +) as (keyof typeof propSchemas)[]; export type DisplayAlign = z.infer; export type DisplayNode = @@ -424,6 +427,7 @@ export type DisplayReadOptions = { // arrived are left out instead of drawn as placeholders. writing?: boolean; finalLineIncomplete?: boolean; + allSkippedLines?: boolean; }; // Reads the text inside a `spec` fence; by default the block is complete. @@ -433,12 +437,13 @@ export function readDisplaySpec( open = false, writing = false, finalLineIncomplete = false, + allSkippedLines = false, }: DisplayReadOptions = {}, ): DisplayRead { const skips: Skips = { skipped: [], skippedCount: 0 }; const skip = (line: number, reason: string) => { skips.skippedCount++; - if (skips.skipped.length < DISPLAY_LIMITS.skippedDetail) + if (allSkippedLines || skips.skipped.length < DISPLAY_LIMITS.skippedDetail) skips.skipped.push({ line, reason }); }; if (source.length > DISPLAY_LIMITS.source) diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index b72dba6e..e85c248d 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -21,13 +21,15 @@ import { type ActivityItem, } from "../src/client/activityView"; import { seedConversation } from "./helpers/seed.js"; +import { DISPLAY_COMPONENTS, DISPLAY_LIMITS } from "../src/shared/display-spec"; testDirectory(); const { db } = await import("../src/server/db"); const { enqueue, claim } = await import("../src/server/queue"); const { applyActivity, listActivities, ingestNativeActivity } = await import("../src/server/activities"); -const { nativeBindings } = await import("../src/server/native-tools"); +const { invokeNative, nativeBindings } = + await import("../src/server/native-tools"); const { historyActivities, historyProjection } = await import("../src/server/native-history"); afterAll(() => { @@ -358,6 +360,72 @@ it("offers loop_plan with a provider-compatible, validated schema and agent-faci expect(plan.description).toMatch(/never infers a plan/); }); +it("validates display blocks through the native binding without recording a dispatch", async () => { + conversation("check-display"); + enqueue("owner", "t", "check-display", { conversation: "check-display" }); + const run = claim("w")!; + const find = (options: Parameters[1]) => + nativeBindings(run, options).find( + (tool) => tool.tool === "loop_check_display", + )!; + const binding = find({ plans: true }); + expect(find({ nativeCatalog: true })).toEqual(binding); + expect(binding.description.length).toBeLessThan(700); + expect( + nativeBindings({ + ...run, + payload: { ...run.payload, reviewId: "fixture" }, + }).map(({ tool }) => tool), + ).toEqual(["loop_review_read"]); + const validate = new Ajv2020({ strict: false }).compile(binding.inputSchema); + expect(validate({ spec: "" })).toBe(true); + expect(validate({ spec: 42 })).toBe(false); + expect(validate({})).toBe(false); + const root = JSON.stringify({ op: "add", path: "/root", value: "card" }); + const card = JSON.stringify({ + op: "add", + path: "/elements/card", + value: { type: "Card" }, + }); + const valid = root + "\n" + card; + const changes = () => + db.prepare("SELECT total_changes() AS count").pluck().get(); + const before = changes(); + const call = async (spec: string) => { + const outcome = await invokeNative(run, binding, "check-fixture", { spec }); + expect(outcome).toMatchObject({ artifactId: "" }); + if (!("result" in outcome)) + throw new Error("Display check requested approval"); + expect(outcome.result.isError).toBeUndefined(); + return outcome.result.content[0].text as string; + }; + expect(await call(valid)).toContain("Display valid. Elements: 1."); + expect(await call("````spec\n" + valid + "\n````")).toContain( + "Display valid.", + ); + expect(await call(root)).toContain("/root points to a missing element"); + expect(await call("[]")).toContain("Line 1:"); + expect(await call("!\n".repeat(DISPLAY_LIMITS.skippedDetail + 1))).toContain( + `Line ${DISPLAY_LIMITS.skippedDetail + 1}: Not valid JSON.`, + ); + const invalid = await call( + valid.replace('"type":"Card"', '"type":"Card","children":["bad"]') + + '\n{"op":"add","path":"/elements/bad","value":{"type":"LinkButton","props":{"label":"Bad","url":"file:///file"}}}\nno json', + ); + expect(invalid).toContain('Element "bad"'); + expect(invalid).toContain("HTTP(S) url"); + expect(invalid).toContain("Line 4:"); + for (const component of DISPLAY_COMPONENTS) + expect(invalid).toContain("- " + component + ":"); + const atLimit = valid + " ".repeat(DISPLAY_LIMITS.source - valid.length); + expect(await call(atLimit)).toContain("Display valid."); + expect(await call(atLimit + " ")).toContain("Block exceeds"); + await expect( + invokeNative(run, binding, "bad-shape", { spec: 3 }), + ).rejects.toThrow(); + expect(changes()).toBe(before); +}); + it("migration 030 is additive and preserves conversations and native bindings", () => { const old = new Database(":memory:"); try { From a641c608de4ce6a8a48f46ec057912c642a19558 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:06:13 +0000 Subject: [PATCH 02/13] Bound display check diagnostics and accept indented fences Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 55 +++++++++++++++++++++-------- test/activities-integration.test.ts | 21 +++++++++++ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 924fa88d..535eedf6 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -55,39 +55,66 @@ const failures: Record = { }; function displaySource(input: string) { - const wrapped = input.trim().split(/\r\n?|\n/); - const opener = /^(\x60{3,}|~{3,})[ \t]*(?:spec)?[ \t]*$/.exec(wrapped[0]); - const closer = /^([\x60~]+)[ \t]*$/.exec(wrapped.at(-1) ?? ""); + const lines = input.split(/\r\n?|\n/); + const first = lines.findIndex((line) => line.trim() !== ""); + const last = lines.findLastIndex((line) => line.trim() !== ""); + const opener = /^ {0,3}(\x60{3,}|~{3,})[ \t]*(?:spec)?[ \t]*$/.exec( + lines[first] ?? "", + ); + const closer = /^ {0,3}(\x60{3,}|~{3,})[ \t]*$/.exec(lines[last] ?? ""); if ( opener && closer && - wrapped.length > 1 && + last > first && closer[1].length >= opener[1].length && - [...closer[1]].every((character) => character === opener[1][0]) + closer[1][0] === opener[1][0] ) - return wrapped.slice(1, -1).join("\n"); - return input.replace(/\r\n?/g, "\n"); + return lines.slice(first + 1, last).join("\n"); + return lines.join("\n"); } +// Leave room for the failure summary and reference within the API runtime's +// ordinary tool result limit, including when diagnostics reflect caller text. +const DIAGNOSTIC_BUDGET = 8_000; +const quotedDiagnostic = (value: string) => + JSON.stringify(value.length > 160 ? value.slice(0, 159) + "…" : value); + export function checkDisplay(input: string): string { const display = readDisplaySpec(displaySource(input), { allSkippedLines: true, }); - const issues = display.skipped.map( - ({ line, reason }) => `Line ${line}: ${reason}.`, - ); + const issues: string[] = []; + let remaining = DIAGNOSTIC_BUDGET; + let omitted = 0; + const add = (issue: string) => { + if (issue.length + 1 > remaining) { + omitted++; + return; + } + remaining -= issue.length + 1; + issues.push(issue); + }; + for (const { line, reason } of display.skipped) + add(`Line ${line}: ${reason}.`); if (display.ok) { for (const problem of displayProblems(display.root)) { if (problem.type !== "Placeholder") continue; const form = Object.hasOwn(forms, problem.name) ? forms[problem.name as keyof typeof forms] : `Choose a component from ${components}`; - issues.push( - `Element ${JSON.stringify(problem.key)}: ${problem.detail}. ${elementForm} ${form}.`, + add( + `Element ${quotedDiagnostic(problem.key)}: ${quotedDiagnostic(problem.detail)}. ${elementForm} ${form}.`, ); } if (!issues.length) return `Display valid. Elements: ${display.count}. To show it, write it in a fenced spec block in your answer or loop_reply body.`; - } else issues.push(failures[display.reason]); - return ["Display has problems.", ...issues, "", reference].join("\n"); + } + const summary = display.ok + ? "Display has problems." + : failures[display.reason]; + if (omitted) + issues.push( + `${omitted} additional diagnostics omitted. Fix the listed issues and check the block again.`, + ); + return [summary, ...issues, "", reference].join("\n"); } diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index e85c248d..76afcd30 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -403,6 +403,9 @@ it("validates display blocks through the native binding without recording a disp expect(await call("````spec\n" + valid + "\n````")).toContain( "Display valid.", ); + expect(await call(" ~~~spec\r\n" + valid + "\r\n ~~~\r\n")).toContain( + "Display valid.", + ); expect(await call(root)).toContain("/root points to a missing element"); expect(await call("[]")).toContain("Line 1:"); expect(await call("!\n".repeat(DISPLAY_LIMITS.skippedDetail + 1))).toContain( @@ -417,6 +420,24 @@ it("validates display blocks through the native binding without recording a disp expect(invalid).toContain("Line 4:"); for (const component of DISPLAY_COMPONENTS) expect(invalid).toContain("- " + component + ":"); + const manyInvalid = await call("!\n".repeat(2000)); + expect(manyInvalid.length).toBeLessThan(10_000); + expect(manyInvalid).toContain("Block has no applicable lines."); + expect(manyInvalid).toContain("additional diagnostics omitted"); + expect(manyInvalid).toContain("Component reference"); + const longKey = "unterminated\n" + "k".repeat(512); + const repeatedMissing = await call( + root + + "\n" + + JSON.stringify({ + op: "add", + path: "/elements/card", + value: { type: "Card", children: Array(80).fill(longKey) }, + }), + ); + expect(repeatedMissing.length).toBeLessThan(10_000); + expect(repeatedMissing).toContain("additional diagnostics omitted"); + expect(repeatedMissing).not.toContain(longKey); const atLimit = valid + " ".repeat(DISPLAY_LIMITS.source - valid.length); expect(await call(atLimit)).toContain("Display valid."); expect(await call(atLimit + " ")).toContain("Block exceeds"); From 91a978c66f85e95a37eda4ef1367879803287d15 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:27:38 +0000 Subject: [PATCH 03/13] Match display checker fences to the renderer Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 29 +++++++++++++++++++++-------- src/shared/steering-replies.ts | 4 +++- test/activities-integration.test.ts | 15 ++++++++++++++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 535eedf6..f8d50d5e 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -6,6 +6,8 @@ import { readDisplaySpec, type DisplayFailure, } from "../shared/display-spec.js"; +import { PUBLISHED_REPLY_BODY_LIMIT } from "../shared/steering-replies.js"; +import { isDisplayFenceToken } from "../shared/text.js"; const components = DISPLAY_COMPONENTS.join(", "); export const CHECK_DISPLAY_DESCRIPTION = @@ -58,9 +60,7 @@ function displaySource(input: string) { const lines = input.split(/\r\n?|\n/); const first = lines.findIndex((line) => line.trim() !== ""); const last = lines.findLastIndex((line) => line.trim() !== ""); - const opener = /^ {0,3}(\x60{3,}|~{3,})[ \t]*(?:spec)?[ \t]*$/.exec( - lines[first] ?? "", - ); + const opener = /^ {0,3}(\x60{3,}|~{3,})([^\r\n]*)$/.exec(lines[first] ?? ""); const closer = /^ {0,3}(\x60{3,}|~{3,})[ \t]*$/.exec(lines[last] ?? ""); if ( opener && @@ -68,9 +68,19 @@ function displaySource(input: string) { last > first && closer[1].length >= opener[1].length && closer[1][0] === opener[1][0] - ) - return lines.slice(first + 1, last).join("\n"); - return lines.join("\n"); + ) { + const info = opener[2]; + const token = info.replace(/^[ \t]+/, "").split(/[ \t]/, 1)[0]; + if ( + !isDisplayFenceToken(token) || + (opener[1][0] === "`" && info.includes("`")) + ) + return { + issue: `Line ${first + 1}: Use spec as the fenced block's language.`, + }; + return { source: lines.slice(first + 1, last).join("\n") }; + } + return { source: lines.join("\n") }; } // Leave room for the failure summary and reference within the API runtime's @@ -80,7 +90,10 @@ const quotedDiagnostic = (value: string) => JSON.stringify(value.length > 160 ? value.slice(0, 159) + "…" : value); export function checkDisplay(input: string): string { - const display = readDisplaySpec(displaySource(input), { + const source = displaySource(input); + if (source.issue !== undefined) + return [source.issue, "", reference].join("\n"); + const display = readDisplaySpec(source.source, { allSkippedLines: true, }); const issues: string[] = []; @@ -107,7 +120,7 @@ export function checkDisplay(input: string): string { ); } if (!issues.length) - return `Display valid. Elements: ${display.count}. To show it, write it in a fenced spec block in your answer or loop_reply body.`; + return `Display valid. Elements: ${display.count}. Put it in a fenced spec block in your answer. You can also use a loop_reply body if the entire reply, including the fence, fits within ${PUBLISHED_REPLY_BODY_LIMIT} characters.`; } const summary = display.ok ? "Display has problems." diff --git a/src/shared/steering-replies.ts b/src/shared/steering-replies.ts index 64f355d7..c4a32e57 100644 --- a/src/shared/steering-replies.ts +++ b/src/shared/steering-replies.ts @@ -1,6 +1,8 @@ import { MessageId, requestId } from "./contract-ids.js"; import { z } from "zod"; +export const PUBLISHED_REPLY_BODY_LIMIT = 30_000; + export const PublishReply = z.strictObject({ replyTo: requestId(MessageId) .min(1) @@ -10,7 +12,7 @@ export const PublishReply = z.strictObject({ .string() .trim() .min(1) - .max(30000) + .max(PUBLISHED_REPLY_BODY_LIMIT) .describe("Your complete reply to that message."), }); diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index 76afcd30..cfdd4364 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -406,6 +406,15 @@ it("validates display blocks through the native binding without recording a disp expect(await call(" ~~~spec\r\n" + valid + "\r\n ~~~\r\n")).toContain( "Display valid.", ); + expect(await call("```spec example\n" + valid + "\n```")).toContain( + "Display valid.", + ); + expect(await call("~~~spec example\n" + valid + "\n~~~")).toContain( + "Display valid.", + ); + expect(await call("```\n" + valid + "\n```")).toContain( + "Line 1: Use spec as the fenced block's language.", + ); expect(await call(root)).toContain("/root points to a missing element"); expect(await call("[]")).toContain("Line 1:"); expect(await call("!\n".repeat(DISPLAY_LIMITS.skippedDetail + 1))).toContain( @@ -439,7 +448,11 @@ it("validates display blocks through the native binding without recording a disp expect(repeatedMissing).toContain("additional diagnostics omitted"); expect(repeatedMissing).not.toContain(longKey); const atLimit = valid + " ".repeat(DISPLAY_LIMITS.source - valid.length); - expect(await call(atLimit)).toContain("Display valid."); + const largeDisplay = await call(atLimit); + expect(largeDisplay).toContain("Display valid."); + expect(largeDisplay).toContain( + "if the entire reply, including the fence, fits within 30000 characters.", + ); expect(await call(atLimit + " ")).toContain("Block exceeds"); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), From db591a56472f0bee20180947de40774f95140084 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 20:49:39 +0000 Subject: [PATCH 04/13] Check oversized and indented displays at the tool boundary Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 29 +++++++++++++------- src/server/native-tool-bridge.ts | 15 ++++++++++- test/activities-integration.test.ts | 12 ++++++++- test/activities-server.test.ts | 42 +++++++++++++++++++++++++++++ test/agent-mail-native.test.ts | 34 +++++++++++++++++++++++ 5 files changed, 120 insertions(+), 12 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index f8d50d5e..b6f9879e 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -1,13 +1,15 @@ import { z } from "zod"; +import remarkParse from "remark-parse"; +import { unified } from "unified"; import { DISPLAY_COMPONENTS, DISPLAY_LIMITS, displayProblems, + isDisplayFenceLanguage, readDisplaySpec, type DisplayFailure, } from "../shared/display-spec.js"; import { PUBLISHED_REPLY_BODY_LIMIT } from "../shared/steering-replies.js"; -import { isDisplayFenceToken } from "../shared/text.js"; const components = DISPLAY_COMPONENTS.join(", "); export const CHECK_DISPLAY_DESCRIPTION = @@ -16,9 +18,11 @@ export const CHECK_DISPLAY_DESCRIPTION = '. Write one JSON Patch add per line to /root or /elements/. Call this tool to check a block and get the full reference. It runs nothing and needs no approval. Example:\n{"op":"add","path":"/root","value":"summary"}\n{"op":"add","path":"/elements/summary","value":{"type":"Text","props":{"content":"Ready."}}}'; export const checkDisplaySchema = z.strictObject({ - // Allow room for an optional fence around a source at the renderer limit. - spec: z.string().max(DISPLAY_LIMITS.source + 256), + spec: z.string(), }); +const markdown = unified().use(remarkParse).freeze(); +// Allow three spaces of fence indentation per line, CRLF and the wrapper. +const MAX_DISPLAY_INPUT = 5 * DISPLAY_LIMITS.source + 256; const forms: Record<(typeof DISPLAY_COMPONENTS)[number], string> = { Card: "optional title and subtitle strings; children allowed", @@ -60,7 +64,7 @@ function displaySource(input: string) { const lines = input.split(/\r\n?|\n/); const first = lines.findIndex((line) => line.trim() !== ""); const last = lines.findLastIndex((line) => line.trim() !== ""); - const opener = /^ {0,3}(\x60{3,}|~{3,})([^\r\n]*)$/.exec(lines[first] ?? ""); + const opener = /^ {0,3}(\x60{3,}|~{3,})/.exec(lines[first] ?? ""); const closer = /^ {0,3}(\x60{3,}|~{3,})[ \t]*$/.exec(lines[last] ?? ""); if ( opener && @@ -69,16 +73,19 @@ function displaySource(input: string) { closer[1].length >= opener[1].length && closer[1][0] === opener[1][0] ) { - const info = opener[2]; - const token = info.replace(/^[ \t]+/, "").split(/[ \t]/, 1)[0]; + const { children } = markdown.parse( + lines.slice(first, last + 1).join("\n"), + ); + const node = children[0]; if ( - !isDisplayFenceToken(token) || - (opener[1][0] === "`" && info.includes("`")) + children.length !== 1 || + node?.type !== "code" || + !isDisplayFenceLanguage(node.lang) ) return { - issue: `Line ${first + 1}: Use spec as the fenced block's language.`, + issue: `Line ${first + 1}: Use a single fenced block with spec as the fenced block's language.`, }; - return { source: lines.slice(first + 1, last).join("\n") }; + return { source: node.value }; } return { source: lines.join("\n") }; } @@ -90,6 +97,8 @@ const quotedDiagnostic = (value: string) => JSON.stringify(value.length > 160 ? value.slice(0, 159) + "…" : value); export function checkDisplay(input: string): string { + if (input.length > MAX_DISPLAY_INPUT) + return [failures.too_large, "", reference].join("\n"); const source = displaySource(input); if (source.issue !== undefined) return [source.issue, "", reference].join("\n"); diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 02814123..093888a1 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -24,6 +24,10 @@ import { toolBindings, invokeTool, type ToolBinding } from "./mcp/broker.js"; import { db, now } from "./db.js"; import { Problem } from "../shared/problem.js"; +const TOOL_BODY_LIMIT = 128_000; +// A display's character budget can produce a much larger UTF-8 JSON request. +const DISPLAY_CHECK_BODY_LIMIT = 512 * 1024; + type Ticket = { conversation: string; generation: string; @@ -275,13 +279,22 @@ export class NativeToolBridge { const chunks: Buffer[] = []; for await (const chunk of req) { size += chunk.length; - if (size > 128000) { + if (size > DISPLAY_CHECK_BODY_LIMIT) { res.writeHead(413).end(); return; } chunks.push(chunk); } body = JSON.parse(Buffer.concat(chunks).toString()); + if ( + size > TOOL_BODY_LIMIT && + (body?.method !== "tools/call" || + (match![2] ? body.name : body.params?.name) !== + "loop_check_display") + ) { + res.writeHead(413).end(); + return; + } } if (match![2]) { if (req.method !== "POST" || !body || typeof body !== "object") { diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index cfdd4364..b71664a8 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -413,7 +413,7 @@ it("validates display blocks through the native binding without recording a disp "Display valid.", ); expect(await call("```\n" + valid + "\n```")).toContain( - "Line 1: Use spec as the fenced block's language.", + "Line 1: Use a single fenced block with spec as the fenced block's language.", ); expect(await call(root)).toContain("/root points to a missing element"); expect(await call("[]")).toContain("Line 1:"); @@ -454,6 +454,16 @@ it("validates display blocks through the native binding without recording a disp "if the entire reply, including the fence, fits within 30000 characters.", ); expect(await call(atLimit + " ")).toContain("Block exceeds"); + expect(await call(" ".repeat(DISPLAY_LIMITS.source * 6))).toContain( + "Block exceeds", + ); + const indented = atLimit + .split("\n") + .map((line) => " " + line) + .join("\r\n"); + expect(await call(" ~~~spec\r\n" + indented + "\r\n ~~~")).toContain( + "Display valid.", + ); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), ).rejects.toThrow(); diff --git a/test/activities-server.test.ts b/test/activities-server.test.ts index 90922bc7..650861d4 100644 --- a/test/activities-server.test.ts +++ b/test/activities-server.test.ts @@ -3,6 +3,7 @@ import { productionApp } from "./helpers/app.js"; import { testDirectory } from "./helpers/test-workspace.js"; import { settleRun } from "./helpers/run-outcome.js"; import { afterAll, expect, it } from "vitest"; +import { DISPLAY_LIMITS } from "../src/shared/display-spec.js"; testDirectory(); const { db, id, now } = await import("../src/server/db"); @@ -45,6 +46,47 @@ function agent(owner = "alice") { const steps = (statuses: string[]) => statuses.map((status, i) => ({ id: "s" + i, title: "Step " + i, status })); +it("returns an oversized display diagnostic to the API provider and continues the run", async () => { + const a = agent(); + submit("alice", { + agentId: a.id, + input: "Check my display", + idempotencyKey: id(), + }); + const run = claim("display-check-worker")!; + const restore = withTestAdapter("openai", { + async models() { + return []; + }, + async *stream(_secret, input) { + const result = input.messages.find((message) => message.role === "tool"); + if (!result) { + yield { + toolCall: { + id: "oversized-display", + name: "loop_check_display", + arguments: { spec: " ".repeat(6 * DISPLAY_LIMITS.source) }, + }, + }; + return; + } + expect(result.content).toContain("Block exceeds"); + yield { text: "I can shorten the display." }; + }, + }); + try { + await expect(executeAgent(run)).resolves.toMatchObject({ + kind: "finished", + }); + transition(run, "completed"); + expect( + db.prepare("SELECT result FROM runs WHERE id = ?").get(run.id), + ).toEqual({ result: "I can shorten the display." }); + } finally { + restore(); + } +}); + it("API-provider runs publish structured plans, tool lifecycle and confirmed settings", async () => { const a = agent(); submit("alice", { diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index 4fcb005e..8e6cccc2 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -417,6 +417,40 @@ it.each([ }) ).json(); await call({ method: "tools/list" }); + if (cause === "cancel") { + const children = Array.from( + { length: 16 }, + (_, index) => "text" + index, + ); + const spec = [ + { path: "/root", value: "card" }, + { path: "/elements/card", value: { type: "Card", children } }, + ...children.map((key) => ({ + path: "/elements/" + key, + value: { type: "Text", props: { content: "語".repeat(3000) } }, + })), + ] + .map((patch) => JSON.stringify({ op: "add", ...patch })) + .join("\n"); + expect(Buffer.byteLength(spec)).toBeGreaterThan(128_000); + const display = { + method: "tools/call", + id: "display", + name: "loop_check_display", + arguments: { spec }, + }; + expect(await call(display)).toMatchObject({ + content: [ + { text: expect.stringContaining("Display valid. Elements: 17.") }, + ], + }); + const extra = await fetch(endpoint.url + "/pi", { + method: "POST", + headers: { authorization: "Bearer " + endpoint.token }, + body: JSON.stringify({ ...display, name: "loop_message" }), + }); + expect(extra.status).toBe(413); + } const concurrentExpiry = cause === "concurrent-expiry"; if (cause === "expiry" || concurrentExpiry) db.exec( From 0ae83afda97d59090ba975a7247a7760fcaea4bf Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:37:24 +0000 Subject: [PATCH 05/13] Bound native request identity and match display fence line endings Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 20 +++------- src/server/native-tool-bridge.ts | 5 ++- test/activities-integration.test.ts | 6 ++- test/agent-mail-native.test.ts | 57 +++++++++++++++++++++-------- test/display-spec-render.test.tsx | 4 ++ 5 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index b6f9879e..f906e240 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -62,20 +62,10 @@ const failures: Record = { function displaySource(input: string) { const lines = input.split(/\r\n?|\n/); - const first = lines.findIndex((line) => line.trim() !== ""); - const last = lines.findLastIndex((line) => line.trim() !== ""); - const opener = /^ {0,3}(\x60{3,}|~{3,})/.exec(lines[first] ?? ""); - const closer = /^ {0,3}(\x60{3,}|~{3,})[ \t]*$/.exec(lines[last] ?? ""); - if ( - opener && - closer && - last > first && - closer[1].length >= opener[1].length && - closer[1][0] === opener[1][0] - ) { - const { children } = markdown.parse( - lines.slice(first, last + 1).join("\n"), - ); + const first = lines.findIndex((line) => /[^ \t]/.test(line)); + if (/^ {0,3}(\x60{3,}|~{3,})/.test(lines[first] ?? "")) { + // Markdown preserves internal line endings and removes fence indentation. + const { children } = markdown.parse(input); const node = children[0]; if ( children.length !== 1 || @@ -87,7 +77,7 @@ function displaySource(input: string) { }; return { source: node.value }; } - return { source: lines.join("\n") }; + return { source: input }; } // Leave room for the failure summary and reference within the API runtime's diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 093888a1..8e81cdc4 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -128,8 +128,11 @@ export class NativeToolBridge { throw new Error( "Tool was not advertised in this native session. Reopen the conversation to refresh tools.", ); + // Keep the request identity without retaining a potentially large payload. const key = run.id + ":" + requestId, - digest = JSON.stringify([name, args]); + digest = createHash("sha256") + .update(JSON.stringify([name, args])) + .digest("hex"); const previous = t.calls.get(key); if (previous && previous.digest !== digest) throw new Error("Tool request identity was reused"); diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index b71664a8..6dbccade 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -409,6 +409,7 @@ it("validates display blocks through the native binding without recording a disp expect(await call("```spec example\n" + valid + "\n```")).toContain( "Display valid.", ); + expect(await call("```spec\n" + valid)).toContain("Display valid."); expect(await call("~~~spec example\n" + valid + "\n~~~")).toContain( "Display valid.", ); @@ -461,8 +462,11 @@ it("validates display blocks through the native binding without recording a disp .split("\n") .map((line) => " " + line) .join("\r\n"); + expect( + await call(" ~~~spec\r\n" + indented.slice(0, -1) + "\r\n ~~~"), + ).toContain("Display valid."); expect(await call(" ~~~spec\r\n" + indented + "\r\n ~~~")).toContain( - "Display valid.", + "Block exceeds", ); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index 8e6cccc2..a44906b9 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -3,6 +3,8 @@ import { productionApp } from "./helpers/app.js"; import { testDirectory } from "./helpers/test-workspace.js"; import { until } from "./helpers/wait.js"; import { createHash } from "node:crypto"; +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; import { afterAll, expect, it, vi } from "vitest"; import { realpathSync } from "node:fs"; @@ -408,14 +410,13 @@ it.each([ )?.state === "accepted", ); const endpoint = { ...ticket }; - const call = async (body: object) => - ( - await fetch(endpoint.url + "/pi", { - method: "POST", - headers: { authorization: "Bearer " + endpoint.token }, - body: JSON.stringify(body), - }) - ).json(); + const requestPi = (body: object) => + fetch(endpoint.url + "/pi", { + method: "POST", + headers: { authorization: "Bearer " + endpoint.token }, + body: JSON.stringify(body), + }); + const call = async (body: object) => (await requestPi(body)).json(); await call({ method: "tools/list" }); if (cause === "cancel") { const children = Array.from( @@ -439,17 +440,41 @@ it.each([ name: "loop_check_display", arguments: { spec }, }; - expect(await call(display)).toMatchObject({ + const validResult = { content: [ { text: expect.stringContaining("Display valid. Elements: 17.") }, ], - }); - const extra = await fetch(endpoint.url + "/pi", { - method: "POST", - headers: { authorization: "Bearer " + endpoint.token }, - body: JSON.stringify({ ...display, name: "loop_message" }), - }); - expect(extra.status).toBe(413); + }; + expect(await call(display)).toMatchObject(validResult); + expect(await call(display)).toMatchObject(validResult); + expect( + (await requestPi({ ...display, arguments: { spec: spec + "\n" } })) + .status, + ).toBe(409); + expect( + (await requestPi({ ...display, name: "loop_message" })).status, + ).toBe(413); + const client = new Client({ name: "display-fixture", version: "1" }); + try { + await client.connect( + new StreamableHTTPClientTransport(new URL(endpoint.url), { + requestInit: { + headers: { Authorization: "Bearer " + endpoint.token }, + }, + }), + ); + expect((await client.listTools()).tools).toContainEqual( + expect.objectContaining({ name: display.name }), + ); + expect( + await client.callTool({ + name: display.name, + arguments: display.arguments, + }), + ).toMatchObject(validResult); + } finally { + await client.close(); + } } const concurrentExpiry = cause === "concurrent-expiry"; if (cause === "expiry" || concurrentExpiry) diff --git a/test/display-spec-render.test.tsx b/test/display-spec-render.test.tsx index 1d6cc8ac..6f82df15 100644 --- a/test/display-spec-render.test.tsx +++ b/test/display-spec-render.test.tsx @@ -299,6 +299,10 @@ it("draws or falls back at the source size limit as the reader does", () => { const atHtml = render(fence(at!.source)); expect(atHtml).toContain('class="display-spec"'); expect(atHtml).not.toContain("This display could not be drawn"); + expect(render("```spec\n" + at!.source)).toContain('class="display-spec"'); + expect(render(fence(at!.source.replace("\n", "\r\n")))).toContain( + "This display could not be drawn", + ); const overHtml = render(fence(over!.source)); expect(overHtml).not.toContain('class="display-spec"'); expect(overHtml).toContain("This display could not be drawn"); From dc68ca61c4d833eaf91c1b0b474cb7435f87a35b Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:36:50 +0000 Subject: [PATCH 06/13] Let providers correct malformed display checks Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 33 +++++++---- src/server/native-tool-bridge.ts | 8 ++- src/server/native-tools.ts | 21 ++++--- test/activities-integration.test.ts | 30 +++++++++- test/activities-server.test.ts | 86 ++++++++++++++++------------- test/agent-mail-native.test.ts | 6 ++ 6 files changed, 124 insertions(+), 60 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index f906e240..51e13574 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -4,10 +4,10 @@ import { unified } from "unified"; import { DISPLAY_COMPONENTS, DISPLAY_LIMITS, - displayProblems, isDisplayFenceLanguage, readDisplaySpec, type DisplayFailure, + type DisplayNode, } from "../shared/display-spec.js"; import { PUBLISHED_REPLY_BODY_LIMIT } from "../shared/steering-replies.js"; @@ -33,7 +33,7 @@ const forms: Record<(typeof DISPLAY_COMPONENTS)[number], string> = { Table: 'nonempty string[] headers, (string | number)[][] rows; optional caption and align array of "left", "center" or "right"', Chart: - 'nonempty title and chart; pie uses {type:"pie",segments:[{label,value}]} with positive values; bar, line, or area uses {type,categories,series:[{name,values}],xLabel?,yLabel?} with string categories and numeric values', + 'nonempty title and chart; pie uses {type:"pie",segments:[{label,value}]} with positive values; bar, line, or area uses {type,categories,series:[{name,values}],xLabel?,yLabel?} with string categories and at most one numeric value per category in each series', Divider: "no props", Actions: "no props; child LinkButton keys", LinkButton: "nonempty label and an HTTP(S) url", @@ -109,15 +109,26 @@ export function checkDisplay(input: string): string { for (const { line, reason } of display.skipped) add(`Line ${line}: ${reason}.`); if (display.ok) { - for (const problem of displayProblems(display.root)) { - if (problem.type !== "Placeholder") continue; - const form = Object.hasOwn(forms, problem.name) - ? forms[problem.name as keyof typeof forms] - : `Choose a component from ${components}`; - add( - `Element ${quotedDiagnostic(problem.key)}: ${quotedDiagnostic(problem.detail)}. ${elementForm} ${form}.`, - ); - } + const visit = (node: DisplayNode) => { + if (node.type === "Placeholder") { + const form = Object.hasOwn(forms, node.name) + ? forms[node.name as keyof typeof forms] + : `Choose a component from ${components}`; + add( + `Element ${quotedDiagnostic(node.key)}: ${quotedDiagnostic(node.detail)}. ${elementForm} ${form}.`, + ); + } else if (node.type === "Chart" && node.chart.type !== "pie") { + const { categories, series } = node.chart; + for (const { name, values } of series) + if (values.length > categories.length) + add( + `Element ${quotedDiagnostic(node.key)}: Chart series ${quotedDiagnostic(name)} has more values (${values.length}) than categories (${categories.length}). Add categories or remove the extra values before publishing.`, + ); + } else if ("children" in node) { + node.children.forEach(visit); + } + }; + visit(display.root); if (!issues.length) return `Display valid. Elements: ${display.count}. Put it in a fenced spec block in your answer. You can also use a loop_reply body if the entire reply, including the fence, fits within ${PUBLISHED_REPLY_BODY_LIMIT} characters.`; } diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 8e81cdc4..34828a05 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -288,7 +288,13 @@ export class NativeToolBridge { } chunks.push(chunk); } - body = JSON.parse(Buffer.concat(chunks).toString()); + try { + body = JSON.parse(Buffer.concat(chunks).toString()); + } catch (error) { + if (size <= TOOL_BODY_LIMIT) throw error; + res.writeHead(413).end(); + return; + } if ( size > TOOL_BODY_LIMIT && (body?.method !== "tools/call" || diff --git a/src/server/native-tools.ts b/src/server/native-tools.ts index 5937c9e6..c94a8bee 100644 --- a/src/server/native-tools.ts +++ b/src/server/native-tools.ts @@ -392,25 +392,30 @@ export async function invokeNative( return unavailable(); throw new Problem(403, "Native tool is unavailable to this agent"); } - const parsed = schemas[tool as keyof typeof schemas].safeParse(input); - if (!parsed.success && tool !== "loop_reply" && tool !== "loop_memory_search") - throw parsed.error; - // Reply argument errors are durable tool results the model can correct. - // Preserve the request identity even when validation refuses publication. - const args = parsed.success ? parsed.data : input; // A display check is pure and can be repeated without a dispatch record. - if (tool === "loop_check_display") + if (tool === "loop_check_display") { + const parsed = checkDisplaySchema.safeParse(input); return { artifactId: "", result: { + ...(parsed.success ? {} : { isError: true }), content: [ { type: "text", - text: checkDisplay(checkDisplaySchema.parse(args).spec), + text: parsed.success + ? checkDisplay(parsed.data.spec) + : "Pass a single spec field containing the display source as a string.", }, ], }, }; + } + const parsed = schemas[tool as keyof typeof schemas].safeParse(input); + if (!parsed.success && tool !== "loop_reply" && tool !== "loop_memory_search") + throw parsed.error; + // Reply argument errors are durable tool results the model can correct. + // Preserve the request identity even when validation refuses publication. + const args = parsed.success ? parsed.data : input; const encoded = tool === "loop_install_mcp" ? createHash("sha256").update(JSON.stringify(args)).digest("hex") diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index 6dbccade..7152f19e 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -468,9 +468,37 @@ it("validates display blocks through the native binding without recording a disp expect(await call(" ~~~spec\r\n" + indented + "\r\n ~~~")).toContain( "Block exceeds", ); + const chart = (values: number[]) => + root + + "\n" + + JSON.stringify({ + op: "add", + path: "/elements/card", + value: { + type: "Chart", + props: { + title: "Week", + chart: { + type: "bar", + categories: ["Monday"], + series: [{ name: "Visits", values }], + }, + }, + }, + }); + expect(await call(chart([10]))).toContain("Display valid."); + expect(await call(chart([10, 20]))).toContain( + 'Element "card": Chart series "Visits" has more values (2) than categories (1).', + ); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), - ).rejects.toThrow(); + ).resolves.toMatchObject({ + artifactId: "", + result: { + isError: true, + content: [{ text: expect.stringContaining("source as a string") }], + }, + }); expect(changes()).toBe(before); }); diff --git a/test/activities-server.test.ts b/test/activities-server.test.ts index 650861d4..34512a07 100644 --- a/test/activities-server.test.ts +++ b/test/activities-server.test.ts @@ -46,46 +46,54 @@ function agent(owner = "alice") { const steps = (statuses: string[]) => statuses.map((status, i) => ({ id: "s" + i, title: "Step " + i, status })); -it("returns an oversized display diagnostic to the API provider and continues the run", async () => { - const a = agent(); - submit("alice", { - agentId: a.id, - input: "Check my display", - idempotencyKey: id(), - }); - const run = claim("display-check-worker")!; - const restore = withTestAdapter("openai", { - async models() { - return []; - }, - async *stream(_secret, input) { - const result = input.messages.find((message) => message.role === "tool"); - if (!result) { - yield { - toolCall: { - id: "oversized-display", - name: "loop_check_display", - arguments: { spec: " ".repeat(6 * DISPLAY_LIMITS.source) }, - }, - }; - return; - } - expect(result.content).toContain("Block exceeds"); - yield { text: "I can shorten the display." }; - }, - }); - try { - await expect(executeAgent(run)).resolves.toMatchObject({ - kind: "finished", +it.each([ + ["oversized source", " ".repeat(6 * DISPLAY_LIMITS.source), "Block exceeds"], + ["invalid arguments", 42, "source as a string"], +])( + "returns a checker diagnostic to the API provider for %s and continues the run", + async (_case, spec, diagnostic) => { + const a = agent(); + submit("alice", { + agentId: a.id, + input: "Check my display", + idempotencyKey: id(), }); - transition(run, "completed"); - expect( - db.prepare("SELECT result FROM runs WHERE id = ?").get(run.id), - ).toEqual({ result: "I can shorten the display." }); - } finally { - restore(); - } -}); + const run = claim("display-check-worker")!; + const restore = withTestAdapter("openai", { + async models() { + return []; + }, + async *stream(_secret, input) { + const result = input.messages.find( + (message) => message.role === "tool", + ); + if (!result) { + yield { + toolCall: { + id: "check-display", + name: "loop_check_display", + arguments: { spec }, + }, + }; + return; + } + expect(result.content).toContain(diagnostic); + yield { text: "I can fix the display." }; + }, + }); + try { + await expect(executeAgent(run)).resolves.toMatchObject({ + kind: "finished", + }); + transition(run, "completed"); + expect( + db.prepare("SELECT result FROM runs WHERE id = ?").get(run.id), + ).toEqual({ result: "I can fix the display." }); + } finally { + restore(); + } + }, +); it("API-provider runs publish structured plans, tool lifecycle and confirmed settings", async () => { const a = agent(); diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index a44906b9..f48ef246 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -454,6 +454,12 @@ it.each([ expect( (await requestPi({ ...display, name: "loop_message" })).status, ).toBe(413); + const malformed = await fetch(endpoint.url + "/pi", { + method: "POST", + headers: { authorization: "Bearer " + endpoint.token }, + body: " ".repeat(128_001) + "{", + }); + expect(malformed.status).toBe(413); const client = new Client({ name: "display-fixture", version: "1" }); try { await client.connect( From 4a61802d1628e9bdbe32a3ef549eebff0ebf742c Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:09:57 +0000 Subject: [PATCH 07/13] Report extra table cells in display checks Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 15 +++++++++--- src/server/native-tool-bridge.ts | 5 +++- src/shared/display-spec.ts | 24 ++++++++++++++++++- test/activities-integration.test.ts | 12 ++++++++++ test/agent-mail-native.test.ts | 36 +++++++++++++++++++++++------ 5 files changed, 80 insertions(+), 12 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 51e13574..9080271b 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -92,9 +92,6 @@ export function checkDisplay(input: string): string { const source = displaySource(input); if (source.issue !== undefined) return [source.issue, "", reference].join("\n"); - const display = readDisplaySpec(source.source, { - allSkippedLines: true, - }); const issues: string[] = []; let remaining = DIAGNOSTIC_BUDGET; let omitted = 0; @@ -106,6 +103,18 @@ export function checkDisplay(input: string): string { remaining -= issue.length + 1; issues.push(issue); }; + const warnedRows = new Set(); + const display = readDisplaySpec(source.source, { + allSkippedLines: true, + onTableOverflow(key, row, cells, headers) { + const location = key + ":" + row; + if (warnedRows.has(location)) return; + warnedRows.add(location); + add( + `Element ${quotedDiagnostic(key)}: Table row ${row} has more cells (${cells}) than headers (${headers}). Add headers or remove the extra cells before publishing.`, + ); + }, + }); for (const { line, reason } of display.skipped) add(`Line ${line}: ${reason}.`); if (display.ok) { diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 34828a05..7a38b412 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -25,6 +25,7 @@ import { db, now } from "./db.js"; import { Problem } from "../shared/problem.js"; const TOOL_BODY_LIMIT = 128_000; +const TOOL_REQUEST_ID_LIMIT = 200; // A display's character budget can produce a much larger UTF-8 JSON request. const DISPLAY_CHECK_BODY_LIMIT = 512 * 1024; @@ -121,6 +122,8 @@ export class NativeToolBridge { args: Record, signal?: AbortSignal, ) { + if (requestId.length >= TOOL_REQUEST_ID_LIMIT) + throw new Error("Native tool request ID is too long"); const live = nativeSessions.current(t.conversation, t.generation); const run = live.run!; const binding = t.bindings.get(name); @@ -315,7 +318,7 @@ export class NativeToolBridge { ? this.list(t) : body.method === "tools/call" && typeof body.id === "string" && - body.id.length < 200 && + body.id.length < TOOL_REQUEST_ID_LIMIT && typeof body.name === "string" ? await this.call(t, body.id, body.name, body.arguments || {}) : null; diff --git a/src/shared/display-spec.ts b/src/shared/display-spec.ts index 1ae8d343..3f285788 100644 --- a/src/shared/display-spec.ts +++ b/src/shared/display-spec.ts @@ -172,6 +172,12 @@ export type DisplayRead = | ({ ok: false; reason: DisplayFailure } & Skips); type RawElement = Record; +type TableOverflow = ( + key: string, + row: number, + cells: number, + headers: number, +) => void; class DisplayLimit extends Error { constructor(readonly reason: DisplayFailure) { @@ -203,6 +209,7 @@ function build( type: string, props: unknown, children: DisplayNode[], + onTableOverflow?: TableOverflow, ): DisplayNode | string { switch (type) { case "Card": { @@ -239,6 +246,11 @@ function build( const p = propSchemas.Table.safeParse(props); if (!p.success) return problem(p.error); const width = p.data.headers.length; + if (onTableOverflow) + p.data.rows.forEach((row, index) => { + if (row.length > width) + onTableOverflow(key, index + 1, row.length, width); + }); return { type, key, @@ -294,6 +306,7 @@ function draw( text: number; drawn: number; omitMissing: boolean; + onTableOverflow?: TableOverflow; }, ): DisplayNode { if (depth > DISPLAY_LIMITS.depth) throw new DisplayLimit("too_deep"); @@ -336,7 +349,13 @@ function draw( ); // Props are checked before children are visited, so an invalid container // is one placeholder and its children count toward no limit. - const built = build(key, type, element.props ?? {}, []); + const built = build( + key, + type, + element.props ?? {}, + [], + state.onTableOverflow, + ); if (typeof built === "string") return placeholder(key, type, "invalid", built); // An element drawn as a placeholder charges none of the budgets. @@ -428,6 +447,7 @@ export type DisplayReadOptions = { writing?: boolean; finalLineIncomplete?: boolean; allSkippedLines?: boolean; + onTableOverflow?: TableOverflow; }; // Reads the text inside a `spec` fence; by default the block is complete. @@ -438,6 +458,7 @@ export function readDisplaySpec( writing = false, finalLineIncomplete = false, allSkippedLines = false, + onTableOverflow, }: DisplayReadOptions = {}, ): DisplayRead { const skips: Skips = { skipped: [], skippedCount: 0 }; @@ -500,6 +521,7 @@ export function readDisplaySpec( text: 0, drawn: 0, omitMissing: open && writing, + onTableOverflow, }; try { return { diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index 7152f19e..f40e7d25 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -490,6 +490,18 @@ it("validates display blocks through the native binding without recording a disp expect(await call(chart([10, 20]))).toContain( 'Element "card": Chart series "Visits" has more values (2) than categories (1).', ); + const table = (rows: number[][]) => + root + + "\n" + + JSON.stringify({ + op: "add", + path: "/elements/card", + value: { type: "Table", props: { headers: ["Amount"], rows } }, + }); + expect(await call(table([[], [10]]))).toContain("Display valid."); + expect(await call(table([[10], [20, 30]]))).toContain( + 'Element "card": Table row 2 has more cells (2) than headers (1).', + ); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), ).resolves.toMatchObject({ diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index f48ef246..ceb5349f 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -461,14 +461,16 @@ it.each([ }); expect(malformed.status).toBe(413); const client = new Client({ name: "display-fixture", version: "1" }); + const transport = new StreamableHTTPClientTransport( + new URL(endpoint.url), + { + requestInit: { + headers: { Authorization: "Bearer " + endpoint.token }, + }, + }, + ); try { - await client.connect( - new StreamableHTTPClientTransport(new URL(endpoint.url), { - requestInit: { - headers: { Authorization: "Bearer " + endpoint.token }, - }, - }), - ); + await client.connect(transport); expect((await client.listTools()).tools).toContainEqual( expect.objectContaining({ name: display.name }), ); @@ -478,6 +480,26 @@ it.each([ arguments: display.arguments, }), ).toMatchObject(validResult); + const longId = await fetch(endpoint.url, { + method: "POST", + headers: { + Authorization: "Bearer " + endpoint.token, + Accept: "application/json, text/event-stream", + "Content-Type": "application/json", + "Mcp-Session-Id": transport.sessionId!, + }, + body: JSON.stringify({ + jsonrpc: "2.0", + id: "x".repeat(200), + method: "tools/call", + params: { name: display.name, arguments: display.arguments }, + }), + }); + expect(await longId.json()).toMatchObject({ + error: { + message: expect.stringContaining("request ID is too long"), + }, + }); } finally { await client.close(); } From e6371681ca3b0ebe76309116186acfb6564974ad Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:45:23 +0000 Subject: [PATCH 08/13] Flag omitted display children and preserve native request IDs Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 17 +++++----- src/server/native-tool-bridge.ts | 9 +++--- src/shared/display-spec.ts | 49 ++++++++++++++++------------- test/activities-integration.test.ts | 16 ++++++++++ test/agent-mail-native.test.ts | 9 +++--- test/display-spec.test.ts | 20 ++++++++++++ 6 files changed, 80 insertions(+), 40 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 9080271b..06071309 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -41,7 +41,7 @@ const forms: Record<(typeof DISPLAY_COMPONENTS)[number], string> = { const patchForm = "One JSON object per line with op add or replace. Use path /root with a string key, or /elements/ with an element object."; const elementForm = - "An element has type, optional props (defaults to {}), and optional children (element keys in order). Omit visible and repeat. Other fields and unknown props are ignored."; + "An element has type and optional props (defaults to {}). Supply children (element keys in order) only for Card, Section, Fields, and Actions. Omit visible and repeat. Other fields and unknown props are ignored."; const reference = [ "Component reference", patchForm, @@ -103,16 +103,15 @@ export function checkDisplay(input: string): string { remaining -= issue.length + 1; issues.push(issue); }; - const warnedRows = new Set(); + const warnings = new Map>(); const display = readDisplaySpec(source.source, { allSkippedLines: true, - onTableOverflow(key, row, cells, headers) { - const location = key + ":" + row; - if (warnedRows.has(location)) return; - warnedRows.add(location); - add( - `Element ${quotedDiagnostic(key)}: Table row ${row} has more cells (${cells}) than headers (${headers}). Add headers or remove the extra cells before publishing.`, - ); + onWarning(key, detail) { + const details = warnings.get(key) ?? new Set(); + if (details.has(detail)) return; + details.add(detail); + warnings.set(key, details); + add(`Element ${quotedDiagnostic(key)}: ${detail}`); }, }); for (const { line, reason } of display.skipped) diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 7a38b412..6da627bc 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -122,8 +122,6 @@ export class NativeToolBridge { args: Record, signal?: AbortSignal, ) { - if (requestId.length >= TOOL_REQUEST_ID_LIMIT) - throw new Error("Native tool request ID is too long"); const live = nativeSessions.current(t.conversation, t.generation); const run = live.run!; const binding = t.bindings.get(name); @@ -131,8 +129,9 @@ export class NativeToolBridge { throw new Error( "Tool was not advertised in this native session. Reopen the conversation to refresh tools.", ); - // Keep the request identity without retaining a potentially large payload. - const key = run.id + ":" + requestId, + // Bound cached data without changing the call ID used at dispatch. + const callId = run.id + ":" + requestId, + key = createHash("sha256").update(callId).digest("hex"), digest = createHash("sha256") .update(JSON.stringify([name, args])) .digest("hex"); @@ -216,7 +215,7 @@ export class NativeToolBridge { } const response = await ( binding.connectionId === "__loop__" ? invokeNative : invokeTool - )(run, binding, key, args); + )(run, binding, callId, args); if ("result" in response) { if (binding.tool === "loop_install_mcp") { this.list(t); diff --git a/src/shared/display-spec.ts b/src/shared/display-spec.ts index 3f285788..4a93370b 100644 --- a/src/shared/display-spec.ts +++ b/src/shared/display-spec.ts @@ -172,12 +172,7 @@ export type DisplayRead = | ({ ok: false; reason: DisplayFailure } & Skips); type RawElement = Record; -type TableOverflow = ( - key: string, - row: number, - cells: number, - headers: number, -) => void; +type DisplayWarning = (key: string, detail: string) => void; class DisplayLimit extends Error { constructor(readonly reason: DisplayFailure) { @@ -209,7 +204,7 @@ function build( type: string, props: unknown, children: DisplayNode[], - onTableOverflow?: TableOverflow, + onWarning?: DisplayWarning, ): DisplayNode | string { switch (type) { case "Card": { @@ -246,10 +241,13 @@ function build( const p = propSchemas.Table.safeParse(props); if (!p.success) return problem(p.error); const width = p.data.headers.length; - if (onTableOverflow) + if (onWarning) p.data.rows.forEach((row, index) => { if (row.length > width) - onTableOverflow(key, index + 1, row.length, width); + onWarning( + key, + `Table row ${index + 1} has more cells (${row.length}) than headers (${width}). Add headers or remove the extra cells before publishing.`, + ); }); return { type, @@ -306,7 +304,8 @@ function draw( text: number; drawn: number; omitMissing: boolean; - onTableOverflow?: TableOverflow; + onWarning?: DisplayWarning; + tables?: Map; }, ): DisplayNode { if (depth > DISPLAY_LIMITS.depth) throw new DisplayLimit("too_deep"); @@ -339,7 +338,14 @@ function draw( "unsupported", "visible and repeat are not supported", ); - const list = takesChildren.has(type) ? (element.children ?? []) : []; + const hasChildren = takesChildren.has(type); + const children = element.children ?? []; + const list = hasChildren ? children : []; + if (!hasChildren && (!Array.isArray(children) || children.length > 0)) + state.onWarning?.( + key, + `${type} does not draw children. Move them into a component that accepts children, such as Card or Section.`, + ); if (!Array.isArray(list) || !list.every((k) => typeof k === "string")) return placeholder( key, @@ -349,13 +355,13 @@ function draw( ); // Props are checked before children are visited, so an invalid container // is one placeholder and its children count toward no limit. - const built = build( - key, - type, - element.props ?? {}, - [], - state.onTableOverflow, - ); + // The checker can reuse validated tables. Every visit still charges the + // display budgets, and each table is a leaf with no per-visit children. + let built = state.tables?.get(key); + if (built === undefined) { + built = build(key, type, element.props ?? {}, [], state.onWarning); + if (type === "Table") state.tables?.set(key, built); + } if (typeof built === "string") return placeholder(key, type, "invalid", built); // An element drawn as a placeholder charges none of the budgets. @@ -447,7 +453,7 @@ export type DisplayReadOptions = { writing?: boolean; finalLineIncomplete?: boolean; allSkippedLines?: boolean; - onTableOverflow?: TableOverflow; + onWarning?: DisplayWarning; }; // Reads the text inside a `spec` fence; by default the block is complete. @@ -458,7 +464,7 @@ export function readDisplaySpec( writing = false, finalLineIncomplete = false, allSkippedLines = false, - onTableOverflow, + onWarning, }: DisplayReadOptions = {}, ): DisplayRead { const skips: Skips = { skipped: [], skippedCount: 0 }; @@ -521,7 +527,8 @@ export function readDisplaySpec( text: 0, drawn: 0, omitMissing: open && writing, - onTableOverflow, + onWarning, + tables: onWarning ? new Map() : undefined, }; try { return { diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index f40e7d25..442faa74 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -502,6 +502,22 @@ it("validates display blocks through the native binding without recording a disp expect(await call(table([[10], [20, 30]]))).toContain( 'Element "card": Table row 2 has more cells (2) than headers (1).', ); + const leaf = + root + + "\n" + + JSON.stringify({ + op: "add", + path: "/elements/card", + value: { + type: "Text", + props: { content: "Summary" }, + children: ["details"], + }, + }) + + '\n{"op":"add","path":"/elements/details","value":{"type":"Divider"}}'; + expect(await call(leaf)).toContain( + 'Element "card": Text does not draw children.', + ); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), ).resolves.toMatchObject({ diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index ceb5349f..ff72675b 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -495,11 +495,10 @@ it.each([ params: { name: display.name, arguments: display.arguments }, }), }); - expect(await longId.json()).toMatchObject({ - error: { - message: expect.stringContaining("request ID is too long"), - }, - }); + expect(await longId.json()).toMatchObject({ result: validResult }); + expect( + (await requestPi({ ...display, id: "x".repeat(200) })).status, + ).toBe(400); } finally { await client.close(); } diff --git a/test/display-spec.test.ts b/test/display-spec.test.ts index f8ba6b28..d9189e05 100644 --- a/test/display-spec.test.ts +++ b/test/display-spec.test.ts @@ -65,6 +65,26 @@ it("lists placeholders with a detail an agent can act on", () => { ]); }); +it("checks a shared table once and draws it at each reference", () => { + const warnings: string[] = []; + const read = readDisplaySpec( + [ + '{"op":"add","path":"/root","value":"section"}', + '{"op":"add","path":"/elements/section","value":{"type":"Section","children":["table","table"]}}', + '{"op":"add","path":"/elements/table","value":{"type":"Table","props":{"headers":["Amount"],"rows":[[10,20]]}}}', + ].join("\n"), + { onWarning: (key, detail) => warnings.push(key + ": " + detail) }, + ); + expect(warnings).toEqual([ + "table: Table row 1 has more cells (2) than headers (1). Add headers or remove the extra cells before publishing.", + ]); + expect(read).toMatchObject({ + ok: true, + count: 3, + root: { children: [{ rows: [["10"]] }, { rows: [["10"]] }] }, + }); +}); + it("never throws on hostile input", () => { const nest = "[".repeat(20000); const inputs = [ From 2e8e40e6b337f120b4c92f53384cae19ebecdab4 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:16:35 +0000 Subject: [PATCH 09/13] Preserve native request ID code units and check group children Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 18 +++++++++++++++++- src/server/native-tool-bridge.ts | 5 +++-- test/activities-integration.test.ts | 17 +++++++++++++++++ test/agent-mail-native.test.ts | 10 ++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 06071309..008dcdb6 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -133,7 +133,23 @@ export function checkDisplay(input: string): string { `Element ${quotedDiagnostic(node.key)}: Chart series ${quotedDiagnostic(name)} has more values (${values.length}) than categories (${categories.length}). Add categories or remove the extra values before publishing.`, ); } else if ("children" in node) { - node.children.forEach(visit); + const childType = + node.type === "Fields" + ? "Field" + : node.type === "Actions" + ? "LinkButton" + : undefined; + for (const child of node.children) { + if ( + childType && + child.type !== childType && + child.type !== "Placeholder" + ) + add( + `Element ${quotedDiagnostic(node.key)}: ${node.type} expects ${childType} children. Move ${quotedDiagnostic(child.key)} (${child.type}) to a Card or Section, or use a ${childType} here.`, + ); + visit(child); + } } }; visit(display.root); diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 6da627bc..2f3f17fe 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -129,9 +129,10 @@ export class NativeToolBridge { throw new Error( "Tool was not advertised in this native session. Reopen the conversation to refresh tools.", ); - // Bound cached data without changing the call ID used at dispatch. + // Hash the original JS code units, including unpaired surrogates. + // Keep the original call ID at dispatch. const callId = run.id + ":" + requestId, - key = createHash("sha256").update(callId).digest("hex"), + key = createHash("sha256").update(callId, "utf16le").digest("hex"), digest = createHash("sha256") .update(JSON.stringify([name, args])) .digest("hex"); diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index 442faa74..7b94cbe9 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -518,6 +518,23 @@ it("validates display blocks through the native binding without recording a disp expect(await call(leaf)).toContain( 'Element "card": Text does not draw children.', ); + for (const [type, childType] of [ + ["Fields", "Field"], + ["Actions", "LinkButton"], + ]) { + const group = [ + root, + JSON.stringify({ + op: "add", + path: "/elements/card", + value: { type, children: ["details"] }, + }), + '{"op":"add","path":"/elements/details","value":{"type":"Text","props":{"content":"Details"}}}', + ].join("\n"); + expect(await call(group)).toContain( + `Element "card": ${type} expects ${childType} children.`, + ); + } await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), ).resolves.toMatchObject({ diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index ff72675b..3561cd49 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -447,6 +447,16 @@ it.each([ }; expect(await call(display)).toMatchObject(validResult); expect(await call(display)).toMatchObject(validResult); + expect(await call({ ...display, id: "\uD800" })).toMatchObject( + validResult, + ); + expect( + await call({ ...display, id: "\uD801", arguments: { spec: "" } }), + ).toMatchObject({ + content: [ + { text: expect.stringContaining("Block has no applicable lines") }, + ], + }); expect( (await requestPi({ ...display, arguments: { spec: spec + "\n" } })) .status, From f48472fab98e05d2d8c0286db1c42c42a6b35910 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:54:51 +0000 Subject: [PATCH 10/13] Recognize display fences after a byte-order mark Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 3 ++- test/display-spec-render.test.tsx | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 008dcdb6..7050bc03 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -61,7 +61,8 @@ const failures: Record = { }; function displaySource(input: string) { - const lines = input.split(/\r\n?|\n/); + // Remark strips a leading BOM before identifying Markdown blocks. + const lines = input.replace(/^\uFEFF/, "").split(/\r\n?|\n/); const first = lines.findIndex((line) => /[^ \t]/.test(line)); if (/^ {0,3}(\x60{3,}|~{3,})/.test(lines[first] ?? "")) { // Markdown preserves internal line endings and removes fence indentation. diff --git a/test/display-spec-render.test.tsx b/test/display-spec-render.test.tsx index 6f82df15..f9adf50a 100644 --- a/test/display-spec-render.test.tsx +++ b/test/display-spec-render.test.tsx @@ -2,6 +2,7 @@ import { expect, it } from "vitest"; import { renderToStaticMarkup } from "react-dom/server"; import { RichText } from "../src/client/components"; import { compactMarkdown } from "../src/client/preview"; +import { checkDisplay } from "../src/server/display-check"; import { displayFixtures } from "./display-spec-fixtures"; const patch = (...lines: unknown[]) => @@ -300,9 +301,24 @@ it("draws or falls back at the source size limit as the reader does", () => { expect(atHtml).toContain('class="display-spec"'); expect(atHtml).not.toContain("This display could not be drawn"); expect(render("```spec\n" + at!.source)).toContain('class="display-spec"'); - expect(render(fence(at!.source.replace("\n", "\r\n")))).toContain( - "This display could not be drawn", - ); + const crlfSource = at!.source.replaceAll("\n", "\r\n"); + const crlfFence = (source: string) => + " ~~~spec\r\n" + + source + .split("\r\n") + .map((line) => " " + line) + .join("\r\n") + + "\r\n ~~~"; + const crlfAt = crlfFence(crlfSource.slice(0, at!.source.length)); + const crlfOver = crlfFence(crlfSource); + for (const prefix of ["", "\uFEFF"]) { + expect(checkDisplay(prefix + crlfAt)).toContain("Display valid."); + expect(render(prefix + crlfAt)).toContain('class="display-spec"'); + expect(checkDisplay(prefix + crlfOver)).toContain("Block exceeds"); + expect(render(prefix + crlfOver)).toContain( + "This display could not be drawn", + ); + } const overHtml = render(fence(over!.source)); expect(overHtml).not.toContain('class="display-spec"'); expect(overHtml).toContain("This display could not be drawn"); From 498c39f7a05c7bfef5ffdc34429d370127aa7079 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:29:04 +0000 Subject: [PATCH 11/13] Accept rendered Actions children in display checks Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 14 ++++---------- src/shared/display-spec.ts | 15 +++++++++------ test/activities-integration.test.ts | 15 ++++++--------- test/display-spec-render.test.tsx | 30 +++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 7050bc03..9885d5e4 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -35,7 +35,7 @@ const forms: Record<(typeof DISPLAY_COMPONENTS)[number], string> = { Chart: 'nonempty title and chart; pie uses {type:"pie",segments:[{label,value}]} with positive values; bar, line, or area uses {type,categories,series:[{name,values}],xLabel?,yLabel?} with string categories and at most one numeric value per category in each series', Divider: "no props", - Actions: "no props; child LinkButton keys", + Actions: "no props; children allowed, typically LinkButton elements", LinkButton: "nonempty label and an HTTP(S) url", }; const patchForm = @@ -134,20 +134,14 @@ export function checkDisplay(input: string): string { `Element ${quotedDiagnostic(node.key)}: Chart series ${quotedDiagnostic(name)} has more values (${values.length}) than categories (${categories.length}). Add categories or remove the extra values before publishing.`, ); } else if ("children" in node) { - const childType = - node.type === "Fields" - ? "Field" - : node.type === "Actions" - ? "LinkButton" - : undefined; for (const child of node.children) { if ( - childType && - child.type !== childType && + node.type === "Fields" && + child.type !== "Field" && child.type !== "Placeholder" ) add( - `Element ${quotedDiagnostic(node.key)}: ${node.type} expects ${childType} children. Move ${quotedDiagnostic(child.key)} (${child.type}) to a Card or Section, or use a ${childType} here.`, + `Element ${quotedDiagnostic(node.key)}: Fields expects Field children. Move ${quotedDiagnostic(child.key)} (${child.type}) to a Card or Section, or use a Field here.`, ); visit(child); } diff --git a/src/shared/display-spec.ts b/src/shared/display-spec.ts index 4a93370b..17a25ff3 100644 --- a/src/shared/display-spec.ts +++ b/src/shared/display-spec.ts @@ -305,7 +305,7 @@ function draw( drawn: number; omitMissing: boolean; onWarning?: DisplayWarning; - tables?: Map; + tablesAndCharts?: Map; }, ): DisplayNode { if (depth > DISPLAY_LIMITS.depth) throw new DisplayLimit("too_deep"); @@ -355,12 +355,13 @@ function draw( ); // Props are checked before children are visited, so an invalid container // is one placeholder and its children count toward no limit. - // The checker can reuse validated tables. Every visit still charges the - // display budgets, and each table is a leaf with no per-visit children. - let built = state.tables?.get(key); + // The checker can reuse validated tables and charts, which have no + // per-visit children. Every visit still charges the display budgets. + let built = state.tablesAndCharts?.get(key); if (built === undefined) { built = build(key, type, element.props ?? {}, [], state.onWarning); - if (type === "Table") state.tables?.set(key, built); + if (type === "Table" || type === "Chart") + state.tablesAndCharts?.set(key, built); } if (typeof built === "string") return placeholder(key, type, "invalid", built); @@ -528,7 +529,9 @@ export function readDisplaySpec( drawn: 0, omitMissing: open && writing, onWarning, - tables: onWarning ? new Map() : undefined, + tablesAndCharts: onWarning + ? new Map() + : undefined, }; try { return { diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index 7b94cbe9..385545ce 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -518,11 +518,8 @@ it("validates display blocks through the native binding without recording a disp expect(await call(leaf)).toContain( 'Element "card": Text does not draw children.', ); - for (const [type, childType] of [ - ["Fields", "Field"], - ["Actions", "LinkButton"], - ]) { - const group = [ + const group = (type: "Fields" | "Actions") => + [ root, JSON.stringify({ op: "add", @@ -531,10 +528,10 @@ it("validates display blocks through the native binding without recording a disp }), '{"op":"add","path":"/elements/details","value":{"type":"Text","props":{"content":"Details"}}}', ].join("\n"); - expect(await call(group)).toContain( - `Element "card": ${type} expects ${childType} children.`, - ); - } + expect(await call(group("Fields"))).toContain( + 'Element "card": Fields expects Field children.', + ); + expect(await call(group("Actions"))).toContain("Display valid. Elements: 2."); await expect( invokeNative(run, binding, "bad-shape", { spec: 3 }), ).resolves.toMatchObject({ diff --git a/test/display-spec-render.test.tsx b/test/display-spec-render.test.tsx index f9adf50a..63a7989f 100644 --- a/test/display-spec-render.test.tsx +++ b/test/display-spec-render.test.tsx @@ -324,6 +324,36 @@ it("draws or falls back at the source size limit as the reader does", () => { expect(overHtml).toContain("This display could not be drawn"); }); +it("checks and draws a text explanation alongside a link in Actions", () => { + const source = fence( + patch( + { op: "add", path: "/root", value: "actions" }, + { + op: "add", + path: "/elements/actions", + value: { type: "Actions", children: ["details", "link"] }, + }, + { + op: "add", + path: "/elements/details", + value: { type: "Text", props: { content: "Read the full report." } }, + }, + { + op: "add", + path: "/elements/link", + value: { + type: "LinkButton", + props: { label: "Open report", url: "https://example.com/report" }, + }, + }, + ), + ); + expect(checkDisplay(source)).toContain("Display valid. Elements: 3."); + const html = render(source); + expect(html).toContain("Read the full report."); + expect(html).toContain('href="https://example.com/report"'); +}); + it("cuts a spec fence to ten lines in a compact preview with displays off", () => { const lines = Array.from({ length: 40 }, (_, i) => ({ op: "add", From 1cb5d029ac59127e4ad72694904ffd92d6c2ae5c Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Sat, 21 Nov 2026 02:14:52 +0000 Subject: [PATCH 12/13] Release display checks during approval and preserve source lines Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 29 ++++++++++++++++-------- src/server/native-tool-bridge.ts | 9 ++++++++ test/activities-integration.test.ts | 6 +++++ test/agent-mail-native.test.ts | 34 ++++++++++++++++++++++++++++- test/display-spec-render.test.tsx | 22 +++++++++++++++++++ 5 files changed, 90 insertions(+), 10 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 9885d5e4..7a176ba8 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -64,21 +64,32 @@ function displaySource(input: string) { // Remark strips a leading BOM before identifying Markdown blocks. const lines = input.replace(/^\uFEFF/, "").split(/\r\n?|\n/); const first = lines.findIndex((line) => /[^ \t]/.test(line)); - if (/^ {0,3}(\x60{3,}|~{3,})/.test(lines[first] ?? "")) { + const opening = lines[first] ?? ""; + if ( + /^ {0,3}(\x60{3,}|~{3,})/.test(opening) || + (/^ {0,3}(>|(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$))/.test(opening) && + (input.includes("```") || input.includes("~~~"))) + ) { // Markdown preserves internal line endings and removes fence indentation. const { children } = markdown.parse(input); - const node = children[0]; - if ( - children.length !== 1 || - node?.type !== "code" || - !isDisplayFenceLanguage(node.lang) + let node = children.length === 1 ? children[0] : undefined; + // A single block can be pasted inside quotes or a single list item. + while ( + node?.type === "blockquote" || + node?.type === "list" || + node?.type === "listItem" ) + node = node.children.length === 1 ? node.children[0] : undefined; + if (node?.type !== "code" || !isDisplayFenceLanguage(node.lang)) return { issue: `Line ${first + 1}: Use a single fenced block with spec as the fenced block's language.`, }; - return { source: node.value }; + return { + source: node.value, + lineOffset: node.position?.start.line ?? first + 1, + }; } - return { source: input }; + return { source: input, lineOffset: 0 }; } // Leave room for the failure summary and reference within the API runtime's @@ -116,7 +127,7 @@ export function checkDisplay(input: string): string { }, }); for (const { line, reason } of display.skipped) - add(`Line ${line}: ${reason}.`); + add(`Line ${source.lineOffset + line}: ${reason}.`); if (display.ok) { const visit = (node: DisplayNode) => { if (node.type === "Placeholder") { diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 2f3f17fe..39983404 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -211,6 +211,15 @@ export class NativeToolBridge { // Wait only on known pending approvals/delegations. Uncertain external // calls throw from the broker and are never repeated. if (state === "awaiting_approval") { + // Display checks are read-only and may have large arguments. + // Return promptly instead of retaining them during the wait. + if ( + binding.connectionId === "__loop__" && + binding.tool === "loop_check_display" + ) + throw new Error( + "The run is waiting for approval. Start a new display check after it resumes.", + ); await wait(revision); continue; } diff --git a/test/activities-integration.test.ts b/test/activities-integration.test.ts index 385545ce..a3233c4e 100644 --- a/test/activities-integration.test.ts +++ b/test/activities-integration.test.ts @@ -418,6 +418,12 @@ it("validates display blocks through the native binding without recording a disp ); expect(await call(root)).toContain("/root points to a missing element"); expect(await call("[]")).toContain("Line 1:"); + expect(await call("\n\n```spec\nnot JSON\n" + valid + "\n```")).toContain( + "Line 4: Not valid JSON.", + ); + expect(await call("> ```spec\n> not JSON\n> ```")).toContain( + "Line 2: Not valid JSON.", + ); expect(await call("!\n".repeat(DISPLAY_LIMITS.skippedDetail + 1))).toContain( `Line ${DISPLAY_LIMITS.skippedDetail + 1}: Not valid JSON.`, ); diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index 3561cd49..32ba1893 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -410,11 +410,12 @@ it.each([ )?.state === "accepted", ); const endpoint = { ...ticket }; - const requestPi = (body: object) => + const requestPi = (body: object, signal?: AbortSignal) => fetch(endpoint.url + "/pi", { method: "POST", headers: { authorization: "Bearer " + endpoint.token }, body: JSON.stringify(body), + signal, }); const call = async (body: object) => (await requestPi(body)).json(); await call({ method: "tools/list" }); @@ -544,6 +545,37 @@ it.each([ .prepare("SELECT id FROM approvals WHERE run_id=?") .get(run.id) as { id: string } | undefined), ); + if (cause === "approval") { + await until( + () => + db + .prepare("SELECT state FROM runs WHERE id=?") + .pluck() + .get(run.id) === "awaiting_approval", + ); + const check = await requestPi( + { + method: "tools/call", + id: "check-during-approval", + name: "loop_check_display", + arguments: { spec: "語".repeat(50_000) }, + }, + AbortSignal.timeout(1500), + ); + expect(await check.json()).toMatchObject({ + isError: true, + content: [ + { + text: expect.stringContaining( + "Start a new display check after it resumes.", + ), + }, + ], + }); + expect( + db.prepare("SELECT state FROM runs WHERE id=?").pluck().get(run.id), + ).toBe("awaiting_approval"); + } const at = Date.now(); if (concurrentExpiry) { // Another request's approval must not permanently cache an expiry error diff --git a/test/display-spec-render.test.tsx b/test/display-spec-render.test.tsx index 63a7989f..5cf79a6e 100644 --- a/test/display-spec-render.test.tsx +++ b/test/display-spec-render.test.tsx @@ -354,6 +354,28 @@ it("checks and draws a text explanation alongside a link in Actions", () => { expect(html).toContain('href="https://example.com/report"'); }); +it("checks and draws a standalone display nested in a quote or list item", () => { + const source = fence( + patch( + { op: "add", path: "/root", value: "details" }, + { + op: "add", + path: "/elements/details", + value: { type: "Text", props: { content: "Nested display." } }, + }, + ), + ); + const lines = source.split("\n"); + const quoted = lines.map((line) => "> " + line).join("\n"); + const listed = lines + .map((line, index) => (index === 0 ? "- " : " ") + line) + .join("\n"); + for (const nested of [quoted, listed]) { + expect(checkDisplay(nested)).toContain("Display valid. Elements: 1."); + expect(render(nested)).toContain('class="display-spec"'); + } +}); + it("cuts a spec fence to ten lines in a compact preview with displays off", () => { const lines = Array.from({ length: 40 }, (_, i) => ({ op: "add", From 62496f8b4eda759ed80b1540b0ad3881f04aeff7 Mon Sep 17 00:00:00 2001 From: Jamie Asefa <13409617+j-asefa@users.noreply.github.com> Date: Sat, 21 Nov 2026 02:49:58 +0000 Subject: [PATCH 13/13] Separate display-check quota and wrapper limits Co-Authored-By: Claude Fable 5.2 --- src/server/display-check.ts | 10 +++++++--- src/server/native-tool-bridge.ts | 23 +++++++++++++-------- test/agent-mail-native.test.ts | 33 +++++++++++++++++++++++++++++++ test/display-spec-render.test.tsx | 14 +++++++++++++ 4 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/server/display-check.ts b/src/server/display-check.ts index 7a176ba8..107d3984 100644 --- a/src/server/display-check.ts +++ b/src/server/display-check.ts @@ -21,8 +21,8 @@ export const checkDisplaySchema = z.strictObject({ spec: z.string(), }); const markdown = unified().use(remarkParse).freeze(); -// Allow three spaces of fence indentation per line, CRLF and the wrapper. -const MAX_DISPLAY_INPUT = 5 * DISPLAY_LIMITS.source + 256; +// Bound Markdown parsing separately from the extracted display source. +const MAX_DISPLAY_INPUT = 512 * 1024; const forms: Record<(typeof DISPLAY_COMPONENTS)[number], string> = { Card: "optional title and subtitle strings; children allowed", @@ -100,7 +100,11 @@ const quotedDiagnostic = (value: string) => export function checkDisplay(input: string): string { if (input.length > MAX_DISPLAY_INPUT) - return [failures.too_large, "", reference].join("\n"); + return [ + `Input exceeds ${MAX_DISPLAY_INPUT} characters. Submit the patch lines directly without Markdown wrappers.`, + "", + reference, + ].join("\n"); const source = displaySource(input); if (source.issue !== undefined) return [source.issue, "", reference].join("\n"); diff --git a/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 39983404..8a3b3413 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -26,6 +26,7 @@ import { Problem } from "../shared/problem.js"; const TOOL_BODY_LIMIT = 128_000; const TOOL_REQUEST_ID_LIMIT = 200; +const TOOL_CALL_LIMIT = 2000; // A display's character budget can produce a much larger UTF-8 JSON request. const DISPLAY_CHECK_BODY_LIMIT = 512 * 1024; @@ -35,6 +36,7 @@ type Ticket = { token: string; bindings: Map; calls: Map }>; + displayChecks: Ticket["calls"]; server?: Server; transport?: StreamableHTTPServerTransport; }; @@ -70,6 +72,7 @@ export class NativeToolBridge { token, bindings: new Map(), calls: new Map(), + displayChecks: new Map(), }); return { url: `http://127.0.0.1:${this.port}/${key}`, token }; } @@ -129,6 +132,10 @@ export class NativeToolBridge { throw new Error( "Tool was not advertised in this native session. Reopen the conversation to refresh tools.", ); + const isDisplayCheck = + binding.connectionId === "__loop__" && + binding.tool === "loop_check_display"; + const calls = isDisplayCheck ? t.displayChecks : t.calls; // Hash the original JS code units, including unpaired surrogates. // Keep the original call ID at dispatch. const callId = run.id + ":" + requestId, @@ -136,7 +143,8 @@ export class NativeToolBridge { digest = createHash("sha256") .update(JSON.stringify([name, args])) .digest("hex"); - const previous = t.calls.get(key); + // Request IDs still belong to the whole run, even across the two quotas. + const previous = t.calls.get(key) ?? t.displayChecks.get(key); if (previous && previous.digest !== digest) throw new Error("Tool request identity was reused"); if ( @@ -161,9 +169,11 @@ export class NativeToolBridge { ) return previous.result; } - if (!previous && t.calls.size >= 2000) + if (!previous && calls.size >= TOOL_CALL_LIMIT) throw new Error( - "Native tool request limit reached; start a new conversation.", + isDisplayCheck + ? "Display check request limit reached; start a new conversation to check more drafts." + : "Native tool request limit reached; start a new conversation.", ); const result = (async () => { let waiting = false; @@ -213,10 +223,7 @@ export class NativeToolBridge { if (state === "awaiting_approval") { // Display checks are read-only and may have large arguments. // Return promptly instead of retaining them during the wait. - if ( - binding.connectionId === "__loop__" && - binding.tool === "loop_check_display" - ) + if (isDisplayCheck) throw new Error( "The run is waiting for approval. Start a new display check after it resumes.", ); @@ -260,7 +267,7 @@ export class NativeToolBridge { if (waiting) nativeSessions.state.bridgeWait(live, false); } })(); - t.calls.set(key, { digest, result }); + calls.set(key, { digest, result }); return result; } private async handle(req: IncomingMessage, res: ServerResponse) { diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index 32ba1893..30cf9997 100644 --- a/test/agent-mail-native.test.ts +++ b/test/agent-mail-native.test.ts @@ -513,6 +513,39 @@ it.each([ } finally { await client.close(); } + for (let start = 0; start < 2000; start += 50) { + await Promise.all( + Array.from({ length: 50 }, async (_, offset) => { + const response = await requestPi({ + ...display, + id: "check-quota-" + (start + offset), + arguments: { spec: "" }, + }); + await response.text(); + if (start === 0) expect(response.status).toBe(200); + }), + ); + } + expect( + ( + await requestPi({ + ...display, + id: "check-quota-exhausted", + arguments: { spec: "" }, + }) + ).status, + ).toBe(409); + expect( + ( + await requestPi({ + ...display, + name: "loop_diagram", + id: "check-quota-0", + arguments: {}, + }) + ).status, + ).toBe(409); + // The separate tool call below can still reach its approval path. } const concurrentExpiry = cause === "concurrent-expiry"; if (cause === "expiry" || concurrentExpiry) diff --git a/test/display-spec-render.test.tsx b/test/display-spec-render.test.tsx index 5cf79a6e..ff064119 100644 --- a/test/display-spec-render.test.tsx +++ b/test/display-spec-render.test.tsx @@ -374,6 +374,20 @@ it("checks and draws a standalone display nested in a quote or list item", () => expect(checkDisplay(nested)).toContain("Display valid. Elements: 1."); expect(render(nested)).toContain('class="display-spec"'); } + const quotePrefix = "> ".repeat(32); + const padded = [ + ...lines.slice(0, -1), + ...Array(5_500).fill(""), + lines.at(-1)!, + ] + .map((line) => quotePrefix + line) + .join("\n"); + expect(padded.length).toBeGreaterThan(5 * 65_536 + 256); + expect(checkDisplay(padded)).toContain("Display valid. Elements: 1."); + expect(render(padded)).toContain('class="display-spec"'); + expect(checkDisplay(" ".repeat(512 * 1024 + 1))).toContain( + "Submit the patch lines directly without Markdown wrappers.", + ); }); it("cuts a spec fence to ten lines in a compact preview with displays off", () => {