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..107d3984 --- /dev/null +++ b/src/server/display-check.ts @@ -0,0 +1,177 @@ +import { z } from "zod"; +import remarkParse from "remark-parse"; +import { unified } from "unified"; +import { + DISPLAY_COMPONENTS, + DISPLAY_LIMITS, + isDisplayFenceLanguage, + readDisplaySpec, + type DisplayFailure, + type DisplayNode, +} from "../shared/display-spec.js"; +import { PUBLISHED_REPLY_BODY_LIMIT } from "../shared/steering-replies.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({ + spec: z.string(), +}); +const markdown = unified().use(remarkParse).freeze(); +// 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", + 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 at most one numeric value per category in each series', + Divider: "no props", + Actions: "no props; children allowed, typically LinkButton elements", + 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 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, + "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) { + // 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)); + 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); + 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, + lineOffset: node.position?.start.line ?? first + 1, + }; + } + return { source: input, lineOffset: 0 }; +} + +// 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 { + if (input.length > MAX_DISPLAY_INPUT) + 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"); + 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); + }; + const warnings = new Map>(); + const display = readDisplaySpec(source.source, { + allSkippedLines: true, + 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) + add(`Line ${source.lineOffset + line}: ${reason}.`); + if (display.ok) { + 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) { + for (const child of node.children) { + if ( + node.type === "Fields" && + child.type !== "Field" && + child.type !== "Placeholder" + ) + add( + `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); + } + } + }; + 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.`; + } + 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/src/server/native-tool-bridge.ts b/src/server/native-tool-bridge.ts index 02814123..8a3b3413 100644 --- a/src/server/native-tool-bridge.ts +++ b/src/server/native-tool-bridge.ts @@ -24,12 +24,19 @@ 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; +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; + type Ticket = { conversation: string; generation: string; token: string; bindings: Map; calls: Map }>; + displayChecks: Ticket["calls"]; server?: Server; transport?: StreamableHTTPServerTransport; }; @@ -65,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 }; } @@ -124,9 +132,19 @@ export class NativeToolBridge { throw new Error( "Tool was not advertised in this native session. Reopen the conversation to refresh tools.", ); - const key = run.id + ":" + requestId, - digest = JSON.stringify([name, args]); - const previous = t.calls.get(key); + 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, + key = createHash("sha256").update(callId, "utf16le").digest("hex"), + digest = createHash("sha256") + .update(JSON.stringify([name, args])) + .digest("hex"); + // 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 ( @@ -151,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; @@ -201,12 +221,18 @@ 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 (isDisplayCheck) + throw new Error( + "The run is waiting for approval. Start a new display check after it resumes.", + ); await wait(revision); continue; } 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); @@ -241,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) { @@ -275,13 +301,28 @@ 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()); + 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" || + (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") { @@ -293,7 +334,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/server/native-tools.ts b/src/server/native-tools.ts index 54029a72..c94a8bee 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.", @@ -385,6 +392,24 @@ export async function invokeNative( return unavailable(); throw new Problem(403, "Native tool is unavailable to this agent"); } + // A display check is pure and can be repeated without a dispatch record. + if (tool === "loop_check_display") { + const parsed = checkDisplaySchema.safeParse(input); + return { + artifactId: "", + result: { + ...(parsed.success ? {} : { isError: true }), + content: [ + { + type: "text", + 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; diff --git a/src/shared/display-spec.ts b/src/shared/display-spec.ts index 076dbd1b..17a25ff3 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 = @@ -169,6 +172,7 @@ export type DisplayRead = | ({ ok: false; reason: DisplayFailure } & Skips); type RawElement = Record; +type DisplayWarning = (key: string, detail: string) => void; class DisplayLimit extends Error { constructor(readonly reason: DisplayFailure) { @@ -200,6 +204,7 @@ function build( type: string, props: unknown, children: DisplayNode[], + onWarning?: DisplayWarning, ): DisplayNode | string { switch (type) { case "Card": { @@ -236,6 +241,14 @@ function build( const p = propSchemas.Table.safeParse(props); if (!p.success) return problem(p.error); const width = p.data.headers.length; + if (onWarning) + p.data.rows.forEach((row, index) => { + if (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, key, @@ -291,6 +304,8 @@ function draw( text: number; drawn: number; omitMissing: boolean; + onWarning?: DisplayWarning; + tablesAndCharts?: Map; }, ): DisplayNode { if (depth > DISPLAY_LIMITS.depth) throw new DisplayLimit("too_deep"); @@ -323,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, @@ -333,7 +355,14 @@ 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 ?? {}, []); + // 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" || type === "Chart") + state.tablesAndCharts?.set(key, built); + } if (typeof built === "string") return placeholder(key, type, "invalid", built); // An element drawn as a placeholder charges none of the budgets. @@ -424,6 +453,8 @@ export type DisplayReadOptions = { // arrived are left out instead of drawn as placeholders. writing?: boolean; finalLineIncomplete?: boolean; + allSkippedLines?: boolean; + onWarning?: DisplayWarning; }; // Reads the text inside a `spec` fence; by default the block is complete. @@ -433,12 +464,14 @@ export function readDisplaySpec( open = false, writing = false, finalLineIncomplete = false, + allSkippedLines = false, + onWarning, }: 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) @@ -495,6 +528,10 @@ export function readDisplaySpec( text: 0, drawn: 0, omitMissing: open && writing, + onWarning, + tablesAndCharts: onWarning + ? new Map() + : undefined, }; try { return { 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 b72dba6e..a3233c4e 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,196 @@ 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(" ~~~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\n" + valid)).toContain("Display valid."); + expect(await call("~~~spec example\n" + valid + "\n~~~")).toContain( + "Display valid.", + ); + expect(await call("```\n" + valid + "\n```")).toContain( + "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:"); + 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.`, + ); + 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 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); + 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"); + 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.slice(0, -1) + "\r\n ~~~"), + ).toContain("Display valid."); + 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).', + ); + 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).', + ); + 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.', + ); + const group = (type: "Fields" | "Actions") => + [ + 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("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({ + artifactId: "", + result: { + isError: true, + content: [{ text: expect.stringContaining("source as a string") }], + }, + }); + expect(changes()).toBe(before); +}); + it("migration 030 is additive and preserves conversations and native bindings", () => { const old = new Database(":memory:"); try { diff --git a/test/activities-server.test.ts b/test/activities-server.test.ts index 90922bc7..34512a07 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,55 @@ function agent(owner = "alice") { const steps = (statuses: string[]) => statuses.map((status, i) => ({ id: "s" + i, title: "Step " + i, status })); +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(), + }); + 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(); submit("alice", { diff --git a/test/agent-mail-native.test.ts b/test/agent-mail-native.test.ts index 4fcb005e..30cf9997 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,15 +410,143 @@ 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, 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" }); + 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 }, + }; + const validResult = { + content: [ + { text: expect.stringContaining("Display valid. Elements: 17.") }, + ], + }; + 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, + ).toBe(409); + 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" }); + const transport = new StreamableHTTPClientTransport( + new URL(endpoint.url), + { + requestInit: { + headers: { Authorization: "Bearer " + endpoint.token }, + }, + }, + ); + try { + await client.connect(transport); + expect((await client.listTools()).tools).toContainEqual( + expect.objectContaining({ name: display.name }), + ); + expect( + await client.callTool({ + name: display.name, + 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({ result: validResult }); + expect( + (await requestPi({ ...display, id: "x".repeat(200) })).status, + ).toBe(400); + } 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) db.exec( @@ -448,6 +578,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 1d6cc8ac..ff064119 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[]) => @@ -299,11 +300,96 @@ 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"'); + 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"); }); +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("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"'); + } + 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", () => { const lines = Array.from({ length: 40 }, (_, i) => ({ op: "add", 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 = [