|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { create, fromBinary } from "@bufbuild/protobuf"; |
| 3 | +import { handleCursorNativeKv } from "../src/adapters/cursor/native-exec"; |
| 4 | +import { encodeCursorRunRequest } from "../src/adapters/cursor/protobuf-request"; |
| 5 | +import { normalizeCursorToolResultText } from "../src/adapters/cursor/tool-result-normalize"; |
| 6 | +import { |
| 7 | + AgentClientMessageSchema, |
| 8 | + ConversationTurnStructureSchema, |
| 9 | + ConversationStepSchema, |
| 10 | + GetBlobArgsSchema, |
| 11 | + KvServerMessageSchema, |
| 12 | +} from "../src/adapters/cursor/gen/agent_pb"; |
| 13 | +import type { OcxMessage } from "../src/types"; |
| 14 | + |
| 15 | +function blobData(blobId: Uint8Array): Uint8Array { |
| 16 | + const reply = fromBinary(AgentClientMessageSchema, handleCursorNativeKv(create(KvServerMessageSchema, { |
| 17 | + id: 1, |
| 18 | + message: { case: "getBlobArgs", value: create(GetBlobArgsSchema, { blobId }) }, |
| 19 | + }))); |
| 20 | + if (reply.message.case !== "kvClientMessage") throw new Error("not kv"); |
| 21 | + const kv = reply.message.value; |
| 22 | + if (kv.message.case !== "getBlobResult") throw new Error("not blob result"); |
| 23 | + return kv.message.value.blobData; |
| 24 | +} |
| 25 | + |
| 26 | +/** Decode the native-wire McpToolResult attached to the first tool call step. */ |
| 27 | +function decodedToolResult(bytes: Uint8Array) { |
| 28 | + const msg = fromBinary(AgentClientMessageSchema, bytes); |
| 29 | + const run = msg.message.case === "runRequest" ? msg.message.value : undefined; |
| 30 | + const turnIds = run?.conversationState?.turns ?? []; |
| 31 | + for (const turnId of turnIds) { |
| 32 | + const turn = fromBinary(ConversationTurnStructureSchema, blobData(turnId)); |
| 33 | + if (turn.turn.case !== "agentConversationTurn") continue; |
| 34 | + for (const stepId of turn.turn.value.steps ?? []) { |
| 35 | + const step = fromBinary(ConversationStepSchema, blobData(stepId)); |
| 36 | + if (step.message.case !== "toolCall") continue; |
| 37 | + const tool = step.message.value.tool; |
| 38 | + if (tool.case !== "mcpToolCall") continue; |
| 39 | + const result = tool.value.result; |
| 40 | + if (result?.result.case !== "success") continue; |
| 41 | + return result.result.value; |
| 42 | + } |
| 43 | + } |
| 44 | + return undefined; |
| 45 | +} |
| 46 | + |
| 47 | +function requestWith(resultContent: string, toolOverrides: Partial<{ toolName: string; toolNamespace?: string; isError: boolean }> = {}) { |
| 48 | + const rawMessages: OcxMessage[] = [ |
| 49 | + { role: "user", content: "run it", timestamp: 1 }, |
| 50 | + { |
| 51 | + role: "assistant", |
| 52 | + model: "cursor/auto", |
| 53 | + timestamp: 2, |
| 54 | + content: [{ type: "toolCall", id: "call_1", name: toolOverrides.toolName ?? "js", namespace: toolOverrides.toolNamespace ?? "mcp__node_repl", arguments: {} }], |
| 55 | + }, |
| 56 | + { |
| 57 | + role: "toolResult", |
| 58 | + toolCallId: "call_1", |
| 59 | + toolName: toolOverrides.toolName ?? "js", |
| 60 | + toolNamespace: "toolNamespace" in toolOverrides ? toolOverrides.toolNamespace : "mcp__node_repl", |
| 61 | + content: resultContent, |
| 62 | + isError: toolOverrides.isError ?? false, |
| 63 | + timestamp: 3, |
| 64 | + }, |
| 65 | + ]; |
| 66 | + return encodeCursorRunRequest({ |
| 67 | + modelId: "composer-2.5", |
| 68 | + conversationId: "cursor_normalize_test", |
| 69 | + system: ["You are helpful."], |
| 70 | + messages: [{ role: "tool", content: "[tool_result]" }], |
| 71 | + rawMessages, |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +describe("normalizeCursorToolResultText (#1920/#1866 unit rows)", () => { |
| 76 | + test("blank node_repl output becomes an actionable error", () => { |
| 77 | + const out = normalizeCursorToolResultText("", { toolName: "js", toolNamespace: "mcp__node_repl" }); |
| 78 | + expect(out.isError).toBe(true); |
| 79 | + expect(out.text).toContain("get_app_state"); |
| 80 | + }); |
| 81 | + |
| 82 | + test("empty exec wrapper (Script completed + <empty>) normalizes for node_repl", () => { |
| 83 | + const out = normalizeCursorToolResultText("Script completed\nOutput:\n<empty>", { toolName: "node_repl" }); |
| 84 | + expect(out.isError).toBe(true); |
| 85 | + expect(out.text).toContain("[empty output"); |
| 86 | + }); |
| 87 | + |
| 88 | + test.each([ |
| 89 | + ["SkyComputerUseError: focus lost", "get_app_state"], |
| 90 | + ["ReferenceError: sky is not defined", "privileged node_repl"], |
| 91 | + ["SyntaxError: Identifier 'x' has already been declared", "redeclaring"], |
| 92 | + ["unsupported import in exec", "injected globals"], |
| 93 | + ])("runtime failure %p is marked as error with guidance", (payload, hint) => { |
| 94 | + const out = normalizeCursorToolResultText(payload, { toolName: "js", toolNamespace: "mcp__node_repl" }); |
| 95 | + expect(out.isError).toBe(true); |
| 96 | + expect(out.text).toContain(payload); |
| 97 | + expect(out.text).toContain(hint); |
| 98 | + }); |
| 99 | + |
| 100 | + test("a non-computer-use tool with empty output stays byte-identical", () => { |
| 101 | + const out = normalizeCursorToolResultText("", { toolName: "read_file" }); |
| 102 | + expect(out.changed).toBe(false); |
| 103 | + expect(out.text).toBe(""); |
| 104 | + expect(out.isError).toBe(false); |
| 105 | + }); |
| 106 | + |
| 107 | + test("ordinary non-empty output on node_repl stays byte-identical", () => { |
| 108 | + const out = normalizeCursorToolResultText("42", { toolName: "js", toolNamespace: "mcp__node_repl" }); |
| 109 | + expect(out.changed).toBe(false); |
| 110 | + expect(out.text).toBe("42"); |
| 111 | + }); |
| 112 | + |
| 113 | + test("an already-error result is not double-annotated", () => { |
| 114 | + const out = normalizeCursorToolResultText("SkyComputerUseError: x", { toolName: "js", toolNamespace: "mcp__node_repl", isError: true }); |
| 115 | + expect(out.changed).toBe(false); |
| 116 | + expect(out.isError).toBe(true); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe("native wire decode (#1920 disposition: formatted text at toolResultPart)", () => { |
| 121 | + test("an empty node_repl result decodes as normalized error text with isError=true on the wire", () => { |
| 122 | + const result = decodedToolResult(requestWith("")); |
| 123 | + expect(result).toBeDefined(); |
| 124 | + expect(result!.isError).toBe(true); |
| 125 | + const first = result!.content[0]; |
| 126 | + expect(first.content.case).toBe("text"); |
| 127 | + expect(first.content.case === "text" ? first.content.value.text : "").toContain("[empty output"); |
| 128 | + }); |
| 129 | + |
| 130 | + test("a failure-state node_repl result decodes with recovery guidance and isError=true", () => { |
| 131 | + const result = decodedToolResult(requestWith("ReferenceError: sky is not defined")); |
| 132 | + expect(result).toBeDefined(); |
| 133 | + expect(result!.isError).toBe(true); |
| 134 | + const first = result!.content[0]; |
| 135 | + expect(first.content.case === "text" ? first.content.value.text : "").toContain("recovery"); |
| 136 | + }); |
| 137 | + |
| 138 | + test("a normal tool result decodes byte-identical (no normalization side effects)", () => { |
| 139 | + const result = decodedToolResult(requestWith("plain output", { toolName: "read_file", toolNamespace: undefined })); |
| 140 | + expect(result).toBeDefined(); |
| 141 | + expect(result!.isError).toBe(false); |
| 142 | + const first = result!.content[0]; |
| 143 | + expect(first.content.case === "text" ? first.content.value.text : "").toBe("plain output"); |
| 144 | + }); |
| 145 | +}); |
0 commit comments