| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218 |
- import {
- parseToolResultDisplayPayload,
- ZCODE_MCP_NODE_REPL_CUA_APP_META_KEY,
- } from "@zcode/contracts";
- import { toolCallGetWorkflowRunDisplaySchema } from "@zcode/shared/zcode-protocol-v4";
- import { describe, expect, it } from "vitest";
- import {
- createMcpToolDisplay,
- createToolResultDisplay,
- } from "../src/tool/executor/result-display.js";
- describe("tool result display", () => {
- it("creates trusted MCP presentation before a tool result exists", () => {
- expect(
- createMcpToolDisplay({
- serverName: "plugin:firebase:firebase",
- toolName: "firebase_get_environment",
- description: "Get the active Firebase environment.",
- }),
- ).toEqual({
- kind: "mcp_tool",
- serverName: "plugin:firebase:firebase",
- toolName: "firebase_get_environment",
- description: "Get the active Firebase environment.",
- });
- });
- it("projects trusted MCP discovery metadata without deriving it from the synthetic tool name", () => {
- const display = createToolResultDisplay(
- "custom-provider-visible-name",
- { content: [{ type: "text", text: "ok" }] },
- {
- mcp: {
- serverName: "company-github-prod",
- toolName: "issue_query_v2",
- description: "Query issues visible to the current user.",
- },
- },
- );
- expect(display).toEqual({
- kind: "mcp_tool",
- serverName: "company-github-prod",
- toolName: "issue_query_v2",
- description: "Query issues visible to the current user.",
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("carries the official MCP unavailable code from the tool error payload", () => {
- const display = createToolResultDisplay(
- "mcp__zcode-official__search_image",
- {
- content: [
- {
- type: "text",
- text: '{"error_code":"quota_exceeded","message":"daily quota exceeded for bucket search_image (5/5)","request_id":"req-1"}',
- },
- ],
- isError: true,
- },
- {
- mcp: {
- serverName: "zcode-official",
- toolName: "search_image",
- official: true,
- },
- },
- );
- expect(display).toEqual({
- kind: "mcp_tool",
- serverName: "zcode-official",
- toolName: "search_image",
- unavailable: { code: "quota_exceeded" },
- });
- // strict schema 必须接受该字段,否则整条 row 会被拒。
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("carries the coding-plan-required code as well", () => {
- const display = createToolResultDisplay(
- "mcp__zcode-official__speech_synthesize",
- {
- content: [{ type: "text", text: '{"error_code":"coding_plan_required","message":"..."}' }],
- isError: true,
- },
- { mcp: { serverName: "zcode-official", toolName: "speech_synthesize", official: true } },
- );
- expect(display).toMatchObject({ unavailable: { code: "coding_plan_required" } });
- });
- it("ignores the unavailable payload from a non-official MCP server", () => {
- // 不判来源的话,任何第三方 MCP 都能伪造一条 Coding Plan 提示误导用户去购买。
- const display = createToolResultDisplay(
- "mcp__evil__whatever",
- {
- content: [{ type: "text", text: '{"error_code":"quota_exceeded","message":"gotcha"}' }],
- isError: true,
- },
- { mcp: { serverName: "evil", toolName: "whatever" } },
- );
- expect(display).toEqual({ kind: "mcp_tool", serverName: "evil", toolName: "whatever" });
- });
- it("ignores unknown codes, non-JSON text and successful results", () => {
- const mcp = { serverName: "zcode-official", toolName: "search_image", official: true };
- const unknownCode = createToolResultDisplay(
- "mcp__zcode-official__search_image",
- {
- content: [{ type: "text", text: '{"error_code":"internal_error","message":"boom"}' }],
- isError: true,
- },
- { mcp },
- );
- const plainText = createToolResultDisplay(
- "mcp__zcode-official__search_image",
- { content: [{ type: "text", text: "quota exhausted" }], isError: true },
- { mcp },
- );
- const successful = createToolResultDisplay(
- "mcp__zcode-official__search_image",
- { content: [{ type: "text", text: '{"error_code":"quota_exceeded"}' }] },
- { mcp },
- );
- // internal_error 是服务端的兜底掩码,用户无法自助解决,不该弹提示。
- expect(unknownCode).not.toHaveProperty("unavailable");
- expect(plainText).not.toHaveProperty("unavailable");
- expect(successful).not.toHaveProperty("unavailable");
- });
- it("bounds untrusted MCP discovery metadata before it enters display persistence", () => {
- const display = createToolResultDisplay(
- "custom-provider-visible-name",
- { content: [{ type: "text", text: "ok" }] },
- {
- mcp: {
- serverName: ` ${"s".repeat(300)} `,
- toolName: ` ${"t".repeat(300)} `,
- description: ` ${"d".repeat(5_000)} `,
- },
- },
- );
- expect(display).toEqual({
- kind: "mcp_tool",
- serverName: "s".repeat(256),
- toolName: "t".repeat(256),
- description: "d".repeat(4 * 1024),
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("omits MCP presentation when bounded identifiers are empty", () => {
- expect(
- createToolResultDisplay(
- "custom-provider-visible-name",
- {},
- {
- mcp: {
- serverName: " \n ",
- toolName: " \t ",
- description: "ignored",
- },
- },
- ),
- ).toBeUndefined();
- });
- it("does not split a surrogate pair at the MCP display boundary", () => {
- const display = createToolResultDisplay(
- "custom-provider-visible-name",
- {},
- {
- mcp: {
- serverName: `${"s".repeat(255)}😀`,
- toolName: "lookup",
- },
- },
- );
- expect(display).toMatchObject({
- kind: "mcp_tool",
- serverName: "s".repeat(255),
- toolName: "lookup",
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("projects bounded Node REPL MCP images for the tool card", () => {
- const display = createToolResultDisplay("mcp__node_repl__js", {
- content: [
- { type: "text", text: "(no output)" },
- { type: "image", data: "data:image/png;base64,AAAA", mimeType: "image/png" },
- ],
- });
- expect(display).toEqual({
- kind: "node_repl_images",
- images: [{ base64: "AAAA", mimeType: "image/png" }],
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("bounds Node REPL display image count independently from model content", () => {
- const display = createToolResultDisplay("mcp__node_repl__js", {
- content: ["AAAA", "BBBB", "CCCC"].map((data) => ({
- type: "image",
- data,
- mimeType: "image/png",
- })),
- });
- expect(display).toEqual({
- kind: "node_repl_images",
- images: [
- { base64: "AAAA", mimeType: "image/png" },
- { base64: "BBBB", mimeType: "image/png" },
- ],
- truncated: true,
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("projects the node_repl CUA app identity even when the cell produced no image", () => {
- // 纯动作 cell(点击、输入)没有截图,但工具卡仍要显示目标 App 图标 —— 旧实现只在有图时
- // 才产生 display,身份被整块丢掉,卡片只剩通用图标。
- const display = createToolResultDisplay("mcp__node_repl__js", {
- content: [{ type: "text", text: "clicked" }],
- _meta: {
- [ZCODE_MCP_NODE_REPL_CUA_APP_META_KEY]: {
- appKey: "darwin:com.apple.notes",
- displayName: "Notes",
- },
- },
- });
- expect(display).toEqual({
- kind: "node_repl_images",
- app: { appKey: "darwin:com.apple.notes", displayName: "Notes" },
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("carries both the observation screenshot and the app identity in one node_repl display", () => {
- const display = createToolResultDisplay("mcp__node_repl__js", {
- content: [{ type: "image", data: "AAAA", mimeType: "image/png" }],
- _meta: {
- [ZCODE_MCP_NODE_REPL_CUA_APP_META_KEY]: {
- appKey: "windows-aumid:Microsoft.WindowsCalculator",
- },
- },
- });
- expect(display).toEqual({
- kind: "node_repl_images",
- images: [{ base64: "AAAA", mimeType: "image/png" }],
- app: { appKey: "windows-aumid:Microsoft.WindowsCalculator" },
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("ignores a malformed node_repl app identity instead of dropping the whole display", () => {
- const display = createToolResultDisplay("mcp__node_repl__js", {
- content: [{ type: "image", data: "AAAA", mimeType: "image/png" }],
- _meta: { [ZCODE_MCP_NODE_REPL_CUA_APP_META_KEY]: { appKey: "" } },
- });
- expect(display).toEqual({
- kind: "node_repl_images",
- images: [{ base64: "AAAA", mimeType: "image/png" }],
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("keeps returning no display when a node_repl cell has neither image nor app identity", () => {
- expect(
- createToolResultDisplay("mcp__node_repl__js", {
- content: [{ type: "text", text: "=> 3" }],
- }),
- ).toBeUndefined();
- });
- it("does not duplicate CUA tool input in persisted display metadata", () => {
- const display = createToolResultDisplay("mcp__computer-use__screenshot", {
- content: [{ type: "text", text: "captured" }],
- });
- expect(display).toMatchObject({ kind: "cua", toolName: "screenshot" });
- expect(display).not.toHaveProperty("input");
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("projects target-app metadata only for the official CUA authority", () => {
- const output = {
- content: [{ type: "text", text: "pressed" }],
- _meta: {
- "zcode.cua/target-app-display-v1": {
- schemaVersion: 1,
- displayName: "Calculator",
- iconLocators: [{ kind: "darwin-bundle-id", value: "com.apple.calculator" }],
- },
- },
- };
- expect(
- createToolResultDisplay("mcp__computer-use__key", output, { officialCua: true }),
- ).toMatchObject({
- kind: "cua",
- targetApp: {
- displayName: "Calculator",
- iconLocators: [{ kind: "darwin-bundle-id", value: "com.apple.calculator" }],
- },
- });
- expect(createToolResultDisplay("mcp__computer-use__key", output)).not.toHaveProperty(
- "targetApp",
- );
- expect(
- createToolResultDisplay("mcp__third-party-computer-use__key", output, {
- officialCua: false,
- }),
- ).not.toHaveProperty("targetApp");
- });
- it("drops malformed request_access permission metadata", () => {
- const display = createToolResultDisplay(
- "mcp__computer-use__request_access",
- {
- content: [{ type: "text", text: "permission report" }],
- _meta: {
- "zcode.cua/request-access-status-v1": {
- schemaVersion: 1,
- platform: "darwin",
- grantOwner: "dev.zcode.cua-helper.dev",
- accessibility: "unknown",
- screenRecording: "denied",
- },
- },
- },
- { officialCua: true },
- );
- expect(display).not.toHaveProperty("permissionStatus");
- });
- it("projects a bounded local-agent message failure", () => {
- const display = createToolResultDisplay("SendMessage", {
- status: "failed",
- messageId: "msg_failed",
- agentId: "agent_missing",
- delivery: "queued",
- error: "No active local_agent task found for target agent_missing.",
- message: "The target agent is no longer running.",
- outputFile: "/tmp/agent-missing.txt",
- taskId: "task_missing",
- });
- expect(display).toEqual({
- kind: "local_agent_message",
- status: "failed",
- error: "No active local_agent task found for target agent_missing.",
- message: "The target agent is no longer running.",
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("bounds oversized SendMessage display fields independently from result serialization", () => {
- const error = "错".repeat(10_000);
- const message = "信".repeat(10_000);
- const display = createToolResultDisplay("SendMessage", {
- status: "failed",
- messageId: "msg_large",
- error,
- message,
- });
- if (display?.kind !== "local_agent_message") {
- throw new Error("Expected SendMessage display payload");
- }
- expect(Buffer.byteLength(display.error ?? "", "utf8")).toBeLessThanOrEqual(4 * 1024);
- expect(Buffer.byteLength(display.message ?? "", "utf8")).toBeLessThanOrEqual(4 * 1024);
- expect(display.error).toMatch(/\n\.\.\.\[truncated\]$/u);
- expect(display.message).toMatch(/\n\.\.\.\[truncated\]$/u);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("does not treat the session mailbox tool as a local-agent message", () => {
- expect(
- createToolResultDisplay("send_message", {
- status: "failed",
- messageId: "msg_mailbox",
- error: "Mailbox delivery failed.",
- }),
- ).toBeUndefined();
- });
- it("projects TaskStop details independently from model content", () => {
- const output = {
- message: "Successfully stopped task: task_alpha (pnpm test)",
- task_id: "task_alpha",
- task_type: "background_shell",
- command: "pnpm test",
- };
- const display = createToolResultDisplay("TaskStop", output);
- expect(display).toEqual({
- kind: "task_stop",
- taskId: "task_alpha",
- taskType: "background_shell",
- command: "pnpm test",
- message: "Successfully stopped task: task_alpha",
- });
- expect(output.message).toBe("Successfully stopped task: task_alpha (pnpm test)");
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("bounds oversized TaskStop display fields independently from result serialization", () => {
- const command = "界".repeat(20_000);
- const display = createToolResultDisplay("TaskStop", {
- message: `Task stop details: ${command}`,
- task_id: "task_large",
- task_type: "background_shell",
- command,
- });
- expect(display).toMatchObject({
- kind: "task_stop",
- truncated: true,
- });
- if (display?.kind !== "task_stop") {
- throw new Error("Expected TaskStop display payload");
- }
- expect(Buffer.byteLength(display.command ?? "", "utf8")).toBeLessThanOrEqual(16 * 1024);
- expect(Buffer.byteLength(display.message, "utf8")).toBeLessThanOrEqual(16 * 1024);
- expect(display.command).toMatch(/\n\.\.\.\[truncated\]$/u);
- expect(display.message).toMatch(/\n\.\.\.\[truncated\]$/u);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it.each(["success", "not_ready", "timeout"] as const)(
- "projects TaskOutput %s status without provider-only fields",
- (retrievalStatus) => {
- const output = {
- retrieval_status: retrievalStatus,
- task:
- retrievalStatus === "timeout"
- ? null
- : {
- task_id: "task_alpha",
- task_type: "local_agent",
- status: retrievalStatus === "not_ready" ? "running" : "completed",
- description: "Inspect the project",
- output: retrievalStatus === "not_ready" ? "partial output\n" : "final output\n",
- error: "provider-only error",
- prompt: "provider-only prompt",
- result: "provider-only result",
- outputFile: "/tmp/task.output",
- },
- };
- const display = createToolResultDisplay("TaskOutput", output);
- expect(display).toEqual(
- retrievalStatus === "timeout"
- ? {
- kind: "task_output",
- retrievalStatus: "timeout",
- }
- : {
- kind: "task_output",
- retrievalStatus,
- taskStatus: retrievalStatus === "not_ready" ? "running" : "completed",
- output: retrievalStatus === "not_ready" ? "partial output" : "final output",
- },
- );
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- if (output.task) {
- expect(output.task.output).toMatch(/\n$/u);
- }
- },
- );
- it("bounds TaskOutput output at 2,000 characters and omits blank output", () => {
- const createTaskOutput = (output: string) =>
- createToolResultDisplay("TaskOutput", {
- retrieval_status: "success",
- task: {
- task_id: "task_large",
- task_type: "local_bash",
- status: "completed",
- description: "Run command",
- output,
- },
- });
- expect(createTaskOutput(" \n\t")).toEqual({
- kind: "task_output",
- retrievalStatus: "success",
- taskStatus: "completed",
- });
- expect(createTaskOutput("x".repeat(2_000))).toEqual({
- kind: "task_output",
- retrievalStatus: "success",
- taskStatus: "completed",
- output: "x".repeat(2_000),
- });
- expect(createTaskOutput(`${"x".repeat(2_000)}y`)).toEqual({
- kind: "task_output",
- retrievalStatus: "success",
- taskStatus: "completed",
- output: "x".repeat(2_000),
- truncated: true,
- });
- });
- it("bounds an unexpected TaskOutput status before it enters display metadata", () => {
- const display = createToolResultDisplay("TaskOutput", {
- retrieval_status: "success",
- task: {
- task_id: "task_status",
- task_type: "local_workflow",
- status: "s".repeat(65),
- description: "Workflow",
- output: "",
- },
- });
- expect(display).toEqual({
- kind: "task_output",
- retrievalStatus: "success",
- taskStatus: "s".repeat(64),
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it.each(["success", "failed"] as const)(
- "projects RespondToCoordinator %s without message or response id",
- (status) => {
- const display = createToolResultDisplay("RespondToCoordinator", {
- status,
- responseId: "response-1",
- message: "Continue the assigned task.",
- error: status === "failed" ? "Queue unavailable" : undefined,
- });
- expect(display).toEqual({
- kind: "respond_to_coordinator",
- status,
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- },
- );
- it("keeps the existing file diff projection", () => {
- expect(
- createToolResultDisplay("WriteLike", {
- filePath: "/workspace/demo.ts",
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 1,
- newStart: 1,
- newLines: 1,
- lines: ["-old", "+new"],
- },
- ],
- }),
- ).toEqual({
- kind: "file_diff",
- filePath: "/workspace/demo.ts",
- additions: 1,
- deletions: 1,
- structuredPatch: [
- {
- oldStart: 1,
- oldLines: 1,
- newStart: 1,
- newLines: 1,
- lines: ["-old", "+new"],
- },
- ],
- truncated: false,
- });
- });
- it("projects a clean CreateWorkflow compile as a structured display", () => {
- const display = createToolResultDisplay("CreateWorkflow", {
- diagnostics: [],
- ok: true,
- response: "The workflow script compiled cleanly.",
- });
- expect(display).toEqual({
- kind: "create_workflow",
- ok: true,
- errorCount: 0,
- diagnostics: [],
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("carries the bounded CreateWorkflow causality graph through to the display", () => {
- const causalityGraph = {
- steps: [
- {
- id: "ask#1",
- kind: "ask",
- label: "planner",
- line: 2,
- column: 24,
- lane: "actor#1",
- },
- {
- id: "ask#2",
- kind: "ask",
- label: "reviewer",
- line: 3,
- column: 24,
- lane: "actor#2",
- repeat: "stack",
- },
- ],
- lanes: [
- { id: "actor#1", name: "planner", line: 2, column: 17 },
- { id: "actor#2", name: "reviewer", line: 3, column: 17 },
- ],
- participants: [
- { id: "unphased:actor#1", phase: "unphased", lane: "actor#1", steps: ["ask#1"] },
- {
- id: "unphased:actor#2",
- phase: "unphased",
- lane: "actor#2",
- steps: ["ask#2"],
- many: true,
- },
- ],
- handoffs: [{ from: "unphased:actor#1", to: "unphased:actor#2", types: ["string"] }],
- sink: ["ask#2"],
- };
- const display = createToolResultDisplay("CreateWorkflow", {
- diagnostics: [],
- ok: true,
- response: "The workflow script compiled cleanly.",
- causalityGraph,
- });
- if (display?.kind !== "create_workflow") {
- throw new Error("Expected CreateWorkflow display payload");
- }
- expect(display.causalityGraph).toEqual(causalityGraph);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("carries may-set lane-expansion copies (`source`) through to the display", () => {
- // display 走 .strict() 解析:契约里少一个 source 不是「字段被丢掉」,而是整张图被拒、
- // GUI 一张卡片都拿不到。所以这条路径必须与工具输出一起钉住。
- const copy = {
- id: "ask#1~actor#1",
- kind: "ask",
- label: "ask",
- line: 6,
- column: 20,
- lane: "actor#1",
- lanes: ["actor#1", "actor#2"],
- source: "ask#1",
- repeat: "serial",
- };
- const causalityGraph = {
- steps: [copy, { ...copy, id: "ask#1~actor#2", lane: "actor#2" }],
- lanes: [
- { id: "actor#1", name: "black" },
- { id: "actor#2", name: "white" },
- ],
- participants: [
- { id: "unphased:actor#1", phase: "unphased", lane: "actor#1", steps: ["ask#1~actor#1"] },
- { id: "unphased:actor#2", phase: "unphased", lane: "actor#2", steps: ["ask#1~actor#2"] },
- ],
- handoffs: [{ from: "unphased:actor#1", to: "unphased:actor#2", back: true }],
- sink: ["ask#1~actor#1", "ask#1~actor#2"],
- };
- const display = createToolResultDisplay("CreateWorkflow", {
- diagnostics: [],
- ok: true,
- response: "The workflow script compiled cleanly.",
- causalityGraph,
- });
- if (display?.kind !== "create_workflow") {
- throw new Error("Expected CreateWorkflow display payload");
- }
- expect(display.causalityGraph).toEqual(causalityGraph);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("rejects a CreateWorkflow output whose causality graph exceeds the contract bounds", () => {
- // 越界图不该出现(handler 已限长),但如果出现,输出解析失败 → 不产出 display,
- // 而不是把无界载荷放进实时事件。
- const display = createToolResultDisplay("CreateWorkflow", {
- diagnostics: [],
- ok: true,
- response: "The workflow script compiled cleanly.",
- causalityGraph: {
- steps: Array.from({ length: 65 }, (_, index) => ({
- id: `ask#${index}`,
- kind: "ask",
- label: "a",
- lane: "actor#1",
- })),
- lanes: [{ id: "actor#1" }],
- participants: [],
- handoffs: [],
- },
- });
- expect(display).toBeUndefined();
- });
- it("projects CreateWorkflow diagnostics without deriving them from the response text", () => {
- const display = createToolResultDisplay("CreateWorkflow", {
- diagnostics: [
- { line: 3, column: 5, code: 2304, message: "Cannot find name 'agent'." },
- { line: 8, column: 1, code: 1005, message: "';' expected." },
- ],
- ok: false,
- response: "The workflow script has type errors:\nL3:C5 Cannot find name 'agent'.",
- });
- expect(display).toEqual({
- kind: "create_workflow",
- ok: false,
- errorCount: 2,
- diagnostics: [
- { line: 3, column: 5, code: 2304, message: "Cannot find name 'agent'." },
- { line: 8, column: 1, code: 1005, message: "';' expected." },
- ],
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("bounds CreateWorkflow diagnostics count and message length independently from model content", () => {
- const diagnostics = Array.from({ length: 150 }, (_, index) => ({
- line: index,
- column: index,
- code: 2304,
- message: "e".repeat(5_000),
- }));
- const display = createToolResultDisplay("CreateWorkflow", {
- diagnostics,
- ok: false,
- response: "The workflow script has type errors:",
- });
- if (display?.kind !== "create_workflow") {
- throw new Error("Expected CreateWorkflow display payload");
- }
- expect(display.errorCount).toBe(150);
- expect(display.diagnostics).toHaveLength(100);
- expect(display.truncated).toBe(true);
- for (const diagnostic of display.diagnostics) {
- expect(diagnostic.message.length).toBeLessThanOrEqual(2_048);
- }
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("ignores non-CreateWorkflow tools and malformed CreateWorkflow output", () => {
- expect(
- createToolResultDisplay("NotCreateWorkflow", {
- diagnostics: [],
- ok: true,
- response: "unrelated",
- }),
- ).toBeUndefined();
- expect(createToolResultDisplay("CreateWorkflow", { ok: "yes" })).toBeUndefined();
- });
- });
- describe("workflow observation tool displays", () => {
- const getWorkflowRunHealth = {
- lastProgressAt: 1_900,
- consecutiveFailures: 0,
- cachedSteps: 1,
- pendingQuestionsKnown: true,
- };
- const getWorkflowRunRunningOutput = {
- runId: "wf_demo",
- label: "nightly-sync",
- labelSource: "name",
- status: "running",
- ownedByThisSession: true,
- createdAt: 1_000,
- updatedAt: 2_000,
- summary: "Running for 1s. 4 of 7 dispatched steps settled, 2 running (1 executing).",
- generatedAt: 2_400,
- usage: {
- spentTokens: 12_345,
- nodesObserved: 7,
- nodesRunning: 2,
- nodesCompleted: 4,
- nodesFailed: 1,
- },
- actors: [
- { siteId: "agent#1", ordinal: 1, name: "scout" },
- { siteId: "agent#2", ordinal: 2 },
- ],
- logTail: [
- { sequence: 1, message: "started" },
- // 带落库时刻的一条:卡上的日志年龄对 generatedAt 算,所以 `at` 必须原样过卡。
- { sequence: 2, message: "fan-out complete", at: Date.UTC(2026, 7, 21, 9, 4, 0) },
- ],
- phases: [
- {
- name: "collect",
- state: "done" as const,
- rounds: 1,
- nodesSettled: 2,
- nodesRunning: 0,
- enteredAt: 1_000,
- exitedAt: 1_400,
- },
- {
- name: "judge",
- state: "current" as const,
- rounds: 1,
- nodesSettled: 2,
- nodesRunning: 2,
- enteredAt: 1_400,
- },
- ],
- subagents: [
- {
- siteId: "agent#1",
- ordinal: 1,
- name: "scout",
- state: "executing" as const,
- phaseName: "judge",
- currentAsk: {
- siteId: "ask#2",
- ordinal: 0,
- actorSeq: 1,
- instructionsHead: "Judge the first seven specs",
- startedAt: 1_500,
- turn: 3,
- toolCalls: 5,
- lastTool: { name: "Read", target: "a.ts", at: 1_900 },
- },
- stepsSettled: 1,
- stepsFailed: 0,
- tokens: 900,
- lastProgressAt: 1_900,
- },
- {
- siteId: "agent#2",
- ordinal: 2,
- state: "waiting" as const,
- wait: { cause: "backoff" as const, reason: "429", retryAfterMs: 20_000, since: 1_800 },
- stepsSettled: 0,
- stepsFailed: 0,
- tokens: 0,
- },
- ],
- health: getWorkflowRunHealth,
- };
- it("projects a running GetWorkflowRun output as a structured display", () => {
- const display = createToolResultDisplay("GetWorkflowRun", getWorkflowRunRunningOutput);
- expect(display).toEqual({
- kind: "get_workflow_run",
- runId: "wf_demo",
- label: "nightly-sync",
- status: "running",
- summary: getWorkflowRunRunningOutput.summary,
- generatedAt: 2_400,
- usage: getWorkflowRunRunningOutput.usage,
- phases: getWorkflowRunRunningOutput.phases,
- // 嵌套的 currentAsk / wait 在卡面上摊平成一行;等待原因的自由文本不上卡。
- subagents: [
- {
- siteId: "agent#1",
- ordinal: 1,
- name: "scout",
- state: "executing",
- phaseName: "judge",
- instructionsHead: "Judge the first seven specs",
- startedAt: 1_500,
- turn: 3,
- toolCalls: 5,
- lastTool: { name: "Read", target: "a.ts", at: 1_900 },
- stepsSettled: 1,
- stepsFailed: 0,
- tokens: 900,
- lastProgressAt: 1_900,
- },
- {
- siteId: "agent#2",
- ordinal: 2,
- state: "waiting",
- waitCause: "backoff",
- retryAfterMs: 20_000,
- waitSince: 1_800,
- stepsSettled: 0,
- stepsFailed: 0,
- tokens: 0,
- },
- ],
- health: getWorkflowRunHealth,
- actors: getWorkflowRunRunningOutput.actors,
- logTail: getWorkflowRunRunningOutput.logTail,
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- // 情势上线之前持久化的载荷没有这五件;display schema 是 strict 的,设成必填会让升级后
- // 打开的每一条历史会话里这张卡整块被剥、退化成纯文本。
- it("still parses a payload persisted before the situation report existed", () => {
- const legacy = {
- kind: "get_workflow_run" as const,
- runId: "wf_old",
- label: "yesterday",
- status: "completed" as const,
- usage: getWorkflowRunRunningOutput.usage,
- actors: getWorkflowRunRunningOutput.actors,
- logTail: getWorkflowRunRunningOutput.logTail,
- };
- expect(parseToolResultDisplayPayload(legacy)).toEqual(legacy);
- });
- // phases「无则缺席」(空数组读起来像「阶段表是空的」);subagents 恒在场。
- it("omits an empty phase table but keeps an empty roster", () => {
- const display = createToolResultDisplay("GetWorkflowRun", {
- ...getWorkflowRunRunningOutput,
- phases: undefined,
- subagents: [],
- });
- if (display?.kind !== "get_workflow_run")
- throw new Error("Expected get_workflow_run display payload");
- expect("phases" in display).toBe(false);
- expect(display.subagents).toEqual([]);
- });
- // 工具面已在 handler 里裁到 64 并打了 subagentsTruncated;卡面继承那个标记,
- // 因为卡上也只该有它真的画出来的那些行。
- it("inherits the roster's truncation flag onto the card", () => {
- const subagent = getWorkflowRunRunningOutput.subagents[1]!;
- const display = createToolResultDisplay("GetWorkflowRun", {
- ...getWorkflowRunRunningOutput,
- subagents: Array.from({ length: 64 }, () => subagent),
- subagentsTruncated: true,
- });
- if (display?.kind !== "get_workflow_run")
- throw new Error("Expected get_workflow_run display payload");
- expect(display.subagents).toHaveLength(64);
- expect(display.truncated).toBe(true);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("keeps the newest log entries and flags truncation on GetWorkflowRun display", () => {
- const logTail = Array.from({ length: 50 }, (_, index) => ({
- sequence: index + 1,
- message: `entry ${index + 1}`,
- }));
- const display = createToolResultDisplay("GetWorkflowRun", {
- ...getWorkflowRunRunningOutput,
- logTail,
- });
- if (display?.kind !== "get_workflow_run") {
- throw new Error("Expected get_workflow_run display payload");
- }
- expect(display.logTail).toHaveLength(40);
- // 截头不截尾:留下的必须是 sequence 11..50。
- expect(display.logTail[0]?.sequence).toBe(11);
- expect(display.logTail.at(-1)?.sequence).toBe(50);
- expect(display.truncated).toBe(true);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("bounds the GetWorkflowRun result text independently of the model channel", () => {
- const display = createToolResultDisplay("GetWorkflowRun", {
- ...getWorkflowRunRunningOutput,
- status: "completed",
- result: "x".repeat(10_000),
- });
- if (display?.kind !== "get_workflow_run") {
- throw new Error("Expected get_workflow_run display payload");
- }
- expect(display.result!.length).toBeLessThanOrEqual(4_000);
- expect(display.truncated).toBe(true);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- // 回归(docs/dynamic-workflow/launch.md「Their cards」):工具输出的 `error.providerStop`
- // 一度被原样搬进工具卡 display,而渲染端按 packages/shared 的镜像 schema 严格校验每一帧,
- // 多一个键就是整条 row 被拒——一个会话因此停在 fault.subscription.recoveryFailed 上。
- // 第二条断言就是那道两侧同步的闸:卡片载荷必须过得了渲染端的那把尺。
- it("keeps the GetWorkflowRun card error at code and message, matching the renderer mirror", () => {
- const display = createToolResultDisplay("GetWorkflowRun", {
- ...getWorkflowRunRunningOutput,
- status: "stopped",
- stopReason: "provider",
- error: {
- code: "ProviderStop",
- message: "The provider stopped the run: rate limited.",
- providerStop: {
- kind: "quota",
- reason: "rate_limited",
- providerId: "bigmodel",
- modelId: "glm-4.6",
- providerCode: "1308",
- subagent: "agent#1",
- subagentName: "scout",
- phase: "plan",
- rawMessage: "您已达到五小时内的用量上限",
- },
- },
- });
- if (display?.kind !== "get_workflow_run") {
- throw new Error("Expected get_workflow_run display payload");
- }
- expect(display.error).toEqual({
- code: "ProviderStop",
- message: "The provider stopped the run: rate limited.",
- });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- // 渲染端的那把尺:两侧不同步时这里先红。
- const mirrored = toolCallGetWorkflowRunDisplaySchema.strict().safeParse(display);
- expect(mirrored.success ? [] : mirrored.error.issues).toEqual([]);
- });
- it("projects ListWorkflowRuns rows verbatim with the output's truncated flag", () => {
- const output = {
- runs: [
- {
- runId: "wf_a",
- label: "alpha",
- labelSource: "name",
- status: "completed",
- ownedByThisSession: true,
- createdAt: 1,
- updatedAt: 2,
- spentTokens: 10,
- },
- {
- runId: "wf_b",
- label: "// derive something",
- labelSource: "script",
- status: "stopped",
- stopReason: "interrupted",
- ownedByThisSession: false,
- possiblyInterrupted: true,
- createdAt: 3,
- updatedAt: 4,
- spentTokens: 20,
- },
- ],
- truncated: true,
- };
- const display = createToolResultDisplay("ListWorkflowRuns", output);
- expect(display).toEqual({ kind: "list_workflow_runs", runs: output.runs, truncated: true });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("projects EvalWorkflowSnippet with bounded logs, response and diagnostics", () => {
- const longLogs = Array.from({ length: 60 }, (_, index) => `log line ${index + 1}`);
- const display = createToolResultDisplay("EvalWorkflowSnippet", {
- ok: false,
- diagnostics: [
- {
- code: 2322,
- column: 10,
- line: 3,
- message: "Type 'string' is not assignable to 'number'.",
- },
- ],
- logs: longLogs,
- response: "y".repeat(9_000),
- durationMs: 1_250,
- });
- if (display?.kind !== "eval_workflow_snippet") {
- throw new Error("Expected eval_workflow_snippet display payload");
- }
- expect(display.ok).toBe(false);
- expect(display.logs).toHaveLength(40);
- expect(display.logs.at(-1)).toBe("log line 60");
- expect(display.response.length).toBeLessThanOrEqual(4_000);
- expect(display.durationMs).toBe(1_250);
- expect(display.truncated).toBe(true);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("projects ListSavedWorkflows entries with arg names and bounded metadata", () => {
- const display = createToolResultDisplay("ListSavedWorkflows", {
- workflows: [
- {
- name: "nightly-sync",
- description: "d".repeat(5_000),
- scope: "project",
- path: ".zcode/workflows/nightly-sync.dwf.ts",
- args: {
- question: { type: "string", description: "what to research", required: true },
- depth: { type: "number" },
- },
- },
- ],
- invalid: [{ path: ".zcode/workflows/broken.dwf.ts", reason: "bad frontmatter" }],
- });
- if (display?.kind !== "saved_workflow_list") {
- throw new Error("Expected saved_workflow_list display payload");
- }
- expect(display.workflows[0]?.argNames).toEqual(["question", "depth"]);
- expect(display.workflows[0]?.description!.length).toBeLessThanOrEqual(2_048);
- expect(display.invalid).toEqual([
- { path: ".zcode/workflows/broken.dwf.ts", reason: "bad frontmatter" },
- ]);
- expect(display.truncated).toBe(true);
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- // spec:docs/dynamic-workflow/launch.md「The model catalog」——目录行原样透传,
- // 「当前」保留,超 100 行截断并打 truncated。
- it("projects ListModels rows and keeps the session's current model", () => {
- const display = createToolResultDisplay("ListModels", {
- current: "account:bigmodel-team-coding-plan/GLM-5.3-Flash",
- models: [
- {
- id: "account:bigmodel-team-coding-plan/GLM-5.3-Flash",
- providerId: "account:bigmodel-team-coding-plan",
- modelId: "GLM-5.3-Flash",
- providerLabel: "BigModel",
- reasoningLevels: ["low", "medium", "high"],
- defaultReasoningLevel: "high",
- contextWindow: 128_000,
- },
- {
- id: "custom:myproxy/glm-4.7",
- providerId: "custom:myproxy",
- modelId: "glm-4.7",
- reasoningLevels: [],
- disabledReason: "no API key",
- },
- ],
- });
- if (display?.kind !== "list_models") {
- throw new Error("Expected list_models display payload");
- }
- expect(display.current).toBe("account:bigmodel-team-coding-plan/GLM-5.3-Flash");
- expect(display.models).toHaveLength(2);
- expect(display.models[0]).toEqual({
- id: "account:bigmodel-team-coding-plan/GLM-5.3-Flash",
- providerId: "account:bigmodel-team-coding-plan",
- modelId: "GLM-5.3-Flash",
- providerLabel: "BigModel",
- reasoningLevels: ["low", "medium", "high"],
- defaultReasoningLevel: "high",
- contextWindow: 128_000,
- });
- // 没有档位的行给空数组、不可选用的行带理由——两者都是读侧要分辨的状态。
- expect(display.models[1]?.reasoningLevels).toEqual([]);
- expect(display.models[1]?.disabledReason).toBe("no API key");
- expect(display.truncated).toBeUndefined();
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("caps the ListModels catalog at 100 rows and flags the truncation", () => {
- const display = createToolResultDisplay("ListModels", {
- models: Array.from({ length: 101 }, (_unused, index) => ({
- id: `custom:proxy/model-${index}`,
- providerId: "custom:proxy",
- modelId: `model-${index}`,
- providerLabel: "p".repeat(5_000),
- reasoningLevels: [],
- })),
- });
- if (display?.kind !== "list_models") {
- throw new Error("Expected list_models display payload");
- }
- expect(display.models).toHaveLength(100);
- expect(display.models[0]?.providerLabel!.length).toBeLessThanOrEqual(2_048);
- expect(display.truncated).toBe(true);
- // current 在目录里对不上时缺席——造一个空串会被读成「当前模型叫空」。
- expect(display.current).toBeUndefined();
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("returns undefined for unknown tools and malformed observation outputs", () => {
- expect(
- createToolResultDisplay("GetWorkflowRunDetail", getWorkflowRunRunningOutput),
- ).toBeUndefined();
- expect(createToolResultDisplay("GetWorkflowRun", { runId: 42 })).toBeUndefined();
- expect(createToolResultDisplay("ListWorkflowRuns", { runs: "many" })).toBeUndefined();
- expect(createToolResultDisplay("EvalWorkflowSnippet", { ok: "yes" })).toBeUndefined();
- expect(createToolResultDisplay("ListSavedWorkflows", null)).toBeUndefined();
- expect(createToolResultDisplay("ListModels", { models: "all of them" })).toBeUndefined();
- expect(createToolResultDisplay("ListModels", null)).toBeUndefined();
- });
- // spec:docs/dynamic-workflow/presentation.md「The run card」——
- // 载荷刻意最小 {runId},坏输出回 undefined 走文本兜底。
- it("builds the ResumeWorkflowRun display payload from a successful output", () => {
- const display = createToolResultDisplay("ResumeWorkflowRun", {
- ok: true,
- runId: "dwfrun_resume",
- response: "The workflow run dwfrun_resume has been resumed and is running in the background.",
- status: "backgrounded",
- backgroundTaskId: "dwfrun_resume",
- });
- expect(display).toEqual({ kind: "resume_workflow_run", runId: "dwfrun_resume" });
- expect(parseToolResultDisplayPayload(display)).toEqual(display);
- });
- it("returns undefined for malformed ResumeWorkflowRun outputs", () => {
- // 失败路径走 ToolHandlerFailure,不构造 display——这里钉住「绝不产出半张卡」。
- expect(createToolResultDisplay("ResumeWorkflowRun", { ok: false })).toBeUndefined();
- expect(createToolResultDisplay("ResumeWorkflowRun", { runId: "" })).toBeUndefined();
- });
- });
|