fix(cursor): stop AgentService empty turns and silent tool hangs
Cursor-hosted models (cu/composer-2.5, cu/cursor-grok-*, cu/default) returned HTTP 200 with an empty turn, or hung, whenever a client sent tools. - Fold system prompts into the current user message. custom_system_prompt (RunRequest field 8) makes AgentService return an empty turn. - Send ModelDetails (field 3); thinking variants (Composer, Grok, *-thinking) return an empty turn when only requested_model (field 9) is set. - Route tool-call history and declared tool schemas through AgentService: encode OpenAI tools into mcp_tools (field 4), decode McpArgs and emit real tool_calls with finish_reason tool_calls. - Map Composer thinking / Grok thinking_delta (field 4) into visible content instead of dropping the answer with the unsigned reasoning. - Ack request_context without echoing MCP tools (double-advertise stalls the HTTP/2 stream) and ack kv_server_message so the run proceeds. - Reject IDE builtin execs instead of failing the turn, so the model can continue with MCP tools or a text answer. - Add google.protobuf.Value / MCP encoders and a FIXED64 branch to encodeField in cursorProtobuf.js. RTK now compresses the source-format body before translation for cursor only: its translator rewrites role:tool into user XML, so the post-translate pass missed those tool results. Every other provider keeps the post-translate pass unchanged.
This commit is contained in:
@@ -18,6 +18,18 @@ function textFrame(text) {
|
||||
return Buffer.from(wrapConnectRPCFrame(encodeField(1, LEN, update)));
|
||||
}
|
||||
|
||||
// InteractionUpdate.thinking_delta (field 4) + turn_ended (field 14).
|
||||
function thinkingFrame(text) {
|
||||
const thinkingPart = Buffer.from(encodeField(1, LEN, text));
|
||||
const update = Buffer.from(encodeField(4, LEN, thinkingPart));
|
||||
return Buffer.from(wrapConnectRPCFrame(encodeField(1, LEN, update)));
|
||||
}
|
||||
|
||||
function turnEndedFrame() {
|
||||
const update = Buffer.from(encodeField(14, LEN, new Uint8Array()));
|
||||
return Buffer.from(wrapConnectRPCFrame(encodeField(1, LEN, update)));
|
||||
}
|
||||
|
||||
function stubAgentSession(executor, frames) {
|
||||
const written = [];
|
||||
const queue = [...frames];
|
||||
@@ -48,12 +60,12 @@ function parseSSE(text) {
|
||||
.map((data) => JSON.parse(data));
|
||||
}
|
||||
|
||||
async function runAgent({ frames, stream }) {
|
||||
async function runAgent({ frames, stream, model = "gpt-5.2", tools }) {
|
||||
const executor = new CursorExecutor();
|
||||
const written = stubAgentSession(executor, frames);
|
||||
const result = await executor.executeAgent({
|
||||
model: "gpt-5.2",
|
||||
body: { messages: [{ role: "user", content: "hi" }] },
|
||||
model,
|
||||
body: { messages: [{ role: "user", content: "hi" }], ...(tools ? { tools } : {}) },
|
||||
stream,
|
||||
credentials,
|
||||
});
|
||||
@@ -73,32 +85,45 @@ describe("CursorExecutor AgentService exec_request handling", () => {
|
||||
expect(content).toBe("hello");
|
||||
});
|
||||
|
||||
it("does not echo client tools on the request_context ack", async () => {
|
||||
const { written, result } = await runAgent({
|
||||
tools: [{ function: { name: "read_file", parameters: { type: "object" } } }],
|
||||
frames: [execRequestFrame(10), textFrame("hello")],
|
||||
stream: true,
|
||||
});
|
||||
|
||||
expect(written.length).toBe(2);
|
||||
expect(written[1].toString("utf8")).not.toContain("read_file");
|
||||
const content = parseSSE(await result.response.text())
|
||||
.map((e) => e.choices?.[0]?.delta?.content || "")
|
||||
.join("");
|
||||
expect(content).toBe("hello");
|
||||
});
|
||||
|
||||
it("does not render an unsupported exec request as assistant content", async () => {
|
||||
const { result } = await runAgent({
|
||||
frames: [textFrame("partial answer"), execRequestFrame(2)],
|
||||
const { result, written } = await runAgent({
|
||||
frames: [textFrame("partial answer"), execRequestFrame(2), textFrame(" more")],
|
||||
stream: true,
|
||||
});
|
||||
|
||||
const body = await result.response.text();
|
||||
expect(body).not.toContain("unsupported IDE tool\\n");
|
||||
expect(body).not.toContain("unsupported IDE tool");
|
||||
const events = parseSSE(body);
|
||||
const content = events.map((e) => e.choices?.[0]?.delta?.content || "").join("");
|
||||
expect(content).toBe("partial answer");
|
||||
|
||||
const errorEvent = events.find((e) => e.error);
|
||||
expect(errorEvent?.error?.message).toContain("unsupported IDE tool");
|
||||
expect(events.some((e) => e.choices?.[0]?.finish_reason === "stop")).toBe(false);
|
||||
expect(content).toBe("partial answer more");
|
||||
expect(events.some((e) => e.error)).toBe(false);
|
||||
expect(written.length).toBe(2); // run frame + IDE rejection
|
||||
});
|
||||
|
||||
it("drops frames batched behind an unsupported exec request in the same read", async () => {
|
||||
it("still emits later text after rejecting an IDE exec in the same read", async () => {
|
||||
const { result } = await runAgent({
|
||||
frames: [Buffer.concat([execRequestFrame(2), textFrame("late")])],
|
||||
stream: true,
|
||||
});
|
||||
|
||||
const body = await result.response.text();
|
||||
expect(body).toContain("unsupported IDE tool");
|
||||
expect(body).not.toContain("late");
|
||||
expect(body).not.toContain("unsupported IDE tool");
|
||||
expect(body).toContain("late");
|
||||
});
|
||||
|
||||
it("returns a non-200 error body for an unsupported exec request when not streaming", async () => {
|
||||
@@ -111,4 +136,32 @@ describe("CursorExecutor AgentService exec_request handling", () => {
|
||||
const payload = await result.response.json();
|
||||
expect(payload.error.message).toContain("unsupported IDE tool");
|
||||
});
|
||||
|
||||
it("streams Composer visible content from thinking_delta after </think>", async () => {
|
||||
const { result } = await runAgent({
|
||||
model: "composer-2.5",
|
||||
frames: [
|
||||
thinkingFrame("private reasoning that must not leak</think>OK"),
|
||||
turnEndedFrame(),
|
||||
],
|
||||
stream: true,
|
||||
});
|
||||
|
||||
const events = parseSSE(await result.response.text());
|
||||
const content = events.map((e) => e.choices?.[0]?.delta?.content || "").join("");
|
||||
expect(content).toBe("OK");
|
||||
expect(JSON.stringify(events)).not.toContain("private reasoning");
|
||||
});
|
||||
|
||||
it("flushes Grok thinking as visible content when the turn has no text_delta", async () => {
|
||||
const { result } = await runAgent({
|
||||
model: "grok-4.5",
|
||||
frames: [thinkingFrame("hello from grok"), turnEndedFrame()],
|
||||
stream: true,
|
||||
});
|
||||
|
||||
const events = parseSSE(await result.response.text());
|
||||
const content = events.map((e) => e.choices?.[0]?.delta?.content || "").join("");
|
||||
expect(content).toBe("hello from grok");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -246,6 +246,15 @@ describe("Cursor AgentService executor helpers (cursor.js)", () => {
|
||||
const run = decodeMessage(clientMsg.get(1)[0].value);
|
||||
expect(run.has(2)).toBe(true); // action
|
||||
expect(run.has(9)).toBe(true); // requested_model
|
||||
// custom_system_prompt (field 8) makes AgentService return an empty turn.
|
||||
expect(run.has(8)).toBe(false);
|
||||
expect(run.has(3)).toBe(true); // ModelDetails — required for thinking variants
|
||||
const action = decodeMessage(run.get(2)[0].value);
|
||||
const userAction = decodeMessage(action.get(1)[0].value);
|
||||
const userMessage = decodeMessage(userAction.get(1)[0].value);
|
||||
const userText = Buffer.from(userMessage.get(1)[0].value).toString("utf8");
|
||||
expect(userText).toContain("be brief");
|
||||
expect(userText).toContain("hi");
|
||||
});
|
||||
|
||||
it("encodes mcp_tools (field 4) when tools are provided", () => {
|
||||
|
||||
131
tests/unit/rtk-cursor-pretranslate.test.js
Normal file
131
tests/unit/rtk-cursor-pretranslate.test.js
Normal file
@@ -0,0 +1,131 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
const { executeMock } = vi.hoisted(() => ({
|
||||
executeMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../../open-sse/executors/index.js", () => ({
|
||||
getExecutor: () => ({
|
||||
noAuth: true,
|
||||
execute: executeMock,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("../../open-sse/utils/requestLogger.js", () => ({
|
||||
createRequestLogger: async () => ({
|
||||
logClientRawRequest: vi.fn(),
|
||||
logRawRequest: vi.fn(),
|
||||
logTargetRequest: vi.fn(),
|
||||
logProviderResponse: vi.fn(),
|
||||
logConvertedResponse: vi.fn(),
|
||||
logError: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("../../open-sse/utils/stream.js", () => ({
|
||||
COLORS: { red: "", reset: "" },
|
||||
createPassthroughStreamWithLogger: vi.fn(() => new TransformStream()),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/usageDb.js", () => ({
|
||||
trackPendingRequest: vi.fn(),
|
||||
appendRequestLog: vi.fn(async () => {}),
|
||||
saveRequestDetail: vi.fn(async () => {}),
|
||||
}));
|
||||
|
||||
const { handleChatCore } = await import("../../open-sse/handlers/chatCore.js");
|
||||
|
||||
function makeLongDiff() {
|
||||
const lines = ["diff --git a/foo.js b/foo.js", "index abc..def 100644", "--- a/foo.js", "+++ b/foo.js", "@@ -1,3 +1,200 @@"];
|
||||
for (let i = 0; i < 200; i++) lines.push(`+added line ${i} UNIQUE_PADDING_${i} ${"x".repeat(20)}`);
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
describe("token savers on Cursor (pre-translate RTK)", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
global.fetch = vi.fn(async (url, init) => {
|
||||
if (String(url).includes("/v1/compress")) {
|
||||
const payload = JSON.parse(init.body);
|
||||
return new Response(JSON.stringify({
|
||||
messages: payload.messages,
|
||||
tokens_before: 8000,
|
||||
tokens_after: 2500,
|
||||
tokens_saved: 5500,
|
||||
}), { status: 200, headers: { "content-type": "application/json" } });
|
||||
}
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
executeMock.mockResolvedValue({
|
||||
response: new Response(JSON.stringify({
|
||||
id: "chatcmpl-test",
|
||||
object: "chat.completion",
|
||||
choices: [{ message: { role: "assistant", content: "ok" }, finish_reason: "stop", index: 0 }],
|
||||
}), { status: 200, headers: { "content-type": "application/json" } }),
|
||||
url: "https://api2.cursor.sh/agent",
|
||||
headers: {},
|
||||
transformedBody: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("compresses role:tool git diffs before openai→cursor rewrite, then injects Headroom/Caveman/Ponytail", async () => {
|
||||
const diff = makeLongDiff();
|
||||
const log = { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), line: vi.fn() };
|
||||
|
||||
await handleChatCore({
|
||||
body: {
|
||||
model: "cu/default",
|
||||
stream: false,
|
||||
messages: [
|
||||
{ role: "system", content: "hi" },
|
||||
{ role: "user", content: "run git diff" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: null,
|
||||
tool_calls: [{ id: "call_1", type: "function", function: { name: "Bash", arguments: JSON.stringify({ command: "git diff" }) } }],
|
||||
},
|
||||
{ role: "tool", tool_call_id: "call_1", content: diff },
|
||||
{ role: "user", content: "summarize" },
|
||||
],
|
||||
},
|
||||
modelInfo: { provider: "cursor", model: "default" },
|
||||
credentials: { apiKey: "test-key", providerSpecificData: {} },
|
||||
log,
|
||||
connectionId: "test-conn",
|
||||
rtkEnabled: true,
|
||||
headroomEnabled: true,
|
||||
headroomUrl: "http://localhost:8787",
|
||||
cavemanEnabled: true,
|
||||
cavemanLevel: "full",
|
||||
ponytailEnabled: true,
|
||||
ponytailLevel: "full",
|
||||
clientRawRequest: {
|
||||
endpoint: "/v1/chat/completions",
|
||||
body: { model: "cu/default" },
|
||||
headers: { accept: "application/json" },
|
||||
},
|
||||
});
|
||||
|
||||
expect(executeMock).toHaveBeenCalled();
|
||||
const dispatched = executeMock.mock.calls[0][0].body;
|
||||
const blob = JSON.stringify(dispatched.messages);
|
||||
|
||||
expect(dispatched.messages.some((m) => m.role === "tool")).toBe(false);
|
||||
expect(blob).toContain("<tool_result>");
|
||||
expect(blob).toContain("lines truncated");
|
||||
expect(blob).not.toContain("UNIQUE_PADDING_150");
|
||||
expect(blob).toContain("lazy senior developer");
|
||||
expect(blob).toMatch(/Respond like a caveman|drop filler|ACTIVE EVERY RESPONSE/i);
|
||||
|
||||
expect(global.fetch).toHaveBeenCalledWith(
|
||||
"http://localhost:8787/v1/compress",
|
||||
expect.any(Object)
|
||||
);
|
||||
|
||||
const xf = log.line.mock.calls.find((c) => c[1] === "⚙");
|
||||
expect(xf, "expected ⚙ saver log").toBeTruthy();
|
||||
expect(xf[2]).toContain("RTK:");
|
||||
expect(xf[2]).toContain("CAVEMAN:full");
|
||||
expect(xf[2]).toContain("PONYTAIL:full");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user