Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

fix cursor bridge idle timeout during tools

Raine Virtac3095e1bfece
diff --git a/src/providers/cursor/client.ts b/src/providers/cursor/client.ts
index dc90bab..19e5c57 100644
--- a/src/providers/cursor/client.ts
+++ b/src/providers/cursor/client.ts
@@ -127,7 +127,7 @@ export class CursorError extends Error {
}
const HEARTBEAT_INTERVAL_MS = 5_000;
-const OUTPUT_IDLE_TIMEOUT_MS = 30_000;
+export const CURSOR_OUTPUT_IDLE_TIMEOUT_MS = 30_000;
export async function runCursorAgent(opts: CursorRunOptions): Promise<ReadableStream<Uint8Array>> {
const proto = opts.proto ?? loadCursorProto();
@@ -1049,7 +1049,7 @@ export async function* decodeCursorStream(
let outputSeen = false;
try {
while (true) {
- const idleMs = opts.outputIdleTimeoutMs ?? OUTPUT_IDLE_TIMEOUT_MS;
+ const idleMs = opts.outputIdleTimeoutMs ?? CURSOR_OUTPUT_IDLE_TIMEOUT_MS;
const read = await readWithOutputIdleTimeout(reader, outputSeen ? idleMs : undefined);
if (read === "idle") {
opts.log?.warn("cursor stream idle after output", { idleMs });
diff --git a/src/providers/cursor/index.test.ts b/src/providers/cursor/index.test.ts
index f3470e0..33d3376 100644
--- a/src/providers/cursor/index.test.ts
+++ b/src/providers/cursor/index.test.ts
@@ -28,28 +28,35 @@ type CursorSseEvent = (Awaited<ReturnType<typeof collectCursorSse>>)[number];
type CursorProviderOptions = NonNullable<Parameters<typeof createCursorProvider>[0]>;
type CursorRunAgent = CursorProviderOptions["runAgent"];
-function createCursorTestProvider(runAgent: CursorRunAgent) {
+function createCursorTestProvider(
+ runAgent: CursorRunAgent,
+ overrides: Partial<CursorProviderOptions> = {},
+) {
return createCursorProvider({
loadAuth: async () => ({ accessToken: "token", source: "test" }),
runAgent,
proto: fakeProto,
+ ...overrides,
});
}
function createRunStreamHarness(
- initialServerMessage: Record<string, any>,
+ initialServerMessage: Record<string, any> | Array<Record<string, any>>,
onWrite: (
message: Record<string, any>,
serverController: ReadableStreamDefaultController<Uint8Array>,
) => Promise<void> | void,
) {
+ const initialServerMessages = Array.isArray(initialServerMessage)
+ ? initialServerMessage
+ : [initialServerMessage];
const sentFrames: Array<Record<string, any>> = [];
let serverController!: ReadableStreamDefaultController<Uint8Array>;
const serverReadable = new ReadableStream<Uint8Array>({
start(controller) {
serverController = controller;
queueMicrotask(() => {
- controller.enqueue(frame(initialServerMessage));
+ for (const message of initialServerMessages) controller.enqueue(frame(message));
});
},
});
@@ -310,6 +317,93 @@ describe("Cursor provider messages", () => {
expectMessageStop(resumeEvents);
});
+ it("keeps native shell bridge state while a long Claude Bash result is pending", async () => {
+ let finalResponseSent = false;
+ const { sentFrames, runAgent } = createRunStreamHarness(
+ [
+ { interactionUpdate: { textDelta: { text: "preparing consult" } } },
+ {
+ message: {
+ case: "execServerMessage",
+ value: {
+ id: 31,
+ execId: "exec-long-shell",
+ message: {
+ case: "shellStreamArgs",
+ value: {
+ command: "consult-llm --task review",
+ workingDirectory: "/tmp",
+ timeout: 300000,
+ },
+ },
+ },
+ },
+ },
+ ],
+ (message, serverController) => {
+ if (message.execClientMessage?.shellStream?.exit && !finalResponseSent) {
+ finalResponseSent = true;
+ enqueueFinalAssistantResponse(serverController, "review results consumed", "12", "4");
+ }
+ },
+ );
+ const provider = createCursorTestProvider(runAgent, { bridgeOutputIdleTimeoutMs: 5 });
+
+ const initial = await provider.handleMessages(
+ {
+ model: "cursor",
+ stream: true,
+ tools: [{ name: "Bash", input_schema: { type: "object" } }],
+ messages: [{ role: "user", content: "review change" }],
+ },
+ fakeCursorCtx({ sessionId: "session-long-shell" }),
+ );
+ const initialEvents = await collectCursorSse(initial);
+ const toolStart = getToolUseStartEvent(initialEvents);
+
+ expect(getTextDeltaEvent(initialEvents)?.data.delta.text).toBe("preparing consult");
+ expect(toolStart?.data.content_block.name).toBe("Bash");
+ expectMessageStopReason(initialEvents, "tool_use");
+
+ await new Promise((resolve) => setTimeout(resolve, 25));
+
+ const resume = await provider.handleMessages(
+ {
+ model: "cursor",
+ stream: true,
+ messages: [
+ {
+ role: "assistant",
+ content: [{
+ type: "tool_use",
+ id: toolStart!.data.content_block.id,
+ name: "Bash",
+ input: { command: "consult-llm --task review" },
+ }],
+ },
+ {
+ role: "user",
+ content: [{
+ type: "tool_result",
+ tool_use_id: toolStart!.data.content_block.id,
+ content: "consult findings",
+ is_error: false,
+ }],
+ },
+ ],
+ },
+ fakeCursorCtx({ sessionId: "session-long-shell" }),
+ );
+ const resumeEvents = await collectCursorSse(resume);
+
+ expect(sentFrames.some((message) =>
+ message.execClientMessage?.shellStream?.stdout?.data === "consult findings"
+ )).toBe(true);
+ expect(getTextDeltaEvent(resumeEvents)?.data.delta.text).toBe("review results consumed");
+ expectMessageStopReason(resumeEvents, "end_turn");
+ expectMessageStop(resumeEvents);
+ });
+
it("recovers XML tool_use text while the native Cursor bridge is active", async () => {
const provider = createCursorTestProvider(async () => {
return streamFromChunks([
diff --git a/src/providers/cursor/index.ts b/src/providers/cursor/index.ts
index e4e79e0..0fd63d4 100644
--- a/src/providers/cursor/index.ts
+++ b/src/providers/cursor/index.ts
@@ -46,6 +46,7 @@ export interface CursorProviderDeps {
loadAuth: () => Promise<CursorAuth | undefined>;
runAgent: (opts: CursorRunOptions) => Promise<ReadableStream<Uint8Array>>;
proto?: CursorProto;
+ bridgeOutputIdleTimeoutMs?: number;
}
const defaultDeps: CursorProviderDeps = {
@@ -110,6 +111,7 @@ async function handleMessages(
proto: deps.proto,
onSession,
allowedToolNames,
+ outputIdleTimeoutMs: deps.bridgeOutputIdleTimeoutMs,
})
: undefined;
const bridgeRead = canBridgeCursorReadTool(body);
diff --git a/src/providers/cursor/tool-bridge.ts b/src/providers/cursor/tool-bridge.ts
index 7182ba6..58a45e5 100644
--- a/src/providers/cursor/tool-bridge.ts
+++ b/src/providers/cursor/tool-bridge.ts
@@ -10,6 +10,7 @@ import {
buildCursorReadResultFromNativeToolResult,
buildCursorShellStreamResultFromNativeToolResult,
buildCursorWriteResultFromNativeToolResult,
+ CURSOR_OUTPUT_IDLE_TIMEOUT_MS,
cursorReadArgs,
cursorShellStreamArgs,
cursorWriteArgs,
@@ -74,6 +75,8 @@ interface CursorBridgeState {
traffic?: RequestContext["traffic"];
onSession?: (sessionId: string) => void;
allowedToolNames?: ReadonlySet<string>;
+ outputSeen: boolean;
+ outputIdleTimeoutMs: number;
}
const bridgeStates = new Map<string, CursorBridgeState>();
@@ -143,6 +146,7 @@ export function createCursorShellToolBridge(opts: {
proto?: CursorProto;
onSession?: (sessionId: string) => void;
allowedToolNames?: ReadonlySet<string>;
+ outputIdleTimeoutMs?: number;
}): {
readHandler: (exec: CursorReadExec, append: CursorAppendMessage) => Promise<void>;
shellStreamHandler: (exec: CursorShellStreamExec, append: CursorAppendMessage) => Promise<void>;
@@ -160,6 +164,8 @@ export function createCursorShellToolBridge(opts: {
traffic: opts.traffic,
onSession: opts.onSession,
allowedToolNames: opts.allowedToolNames,
+ outputSeen: false,
+ outputIdleTimeoutMs: opts.outputIdleTimeoutMs ?? CURSOR_OUTPUT_IDLE_TIMEOUT_MS,
};
const notifyTool = (tool: PendingNativeTool) => {
@@ -283,6 +289,7 @@ export function createCursorShellToolBridge(opts: {
state.iterator = decodeCursorStream(upstream, opts.proto, {
traffic: opts.traffic,
log: opts.log,
+ outputIdleTimeoutMs: 0,
});
bridgeStates.set(opts.sessionId, state);
return streamBridgeUntilToolOrEnd(state, signal);
@@ -417,20 +424,50 @@ function streamBridgeUntilToolOrEnd(
while (!signal?.aborted) {
const next = state.pendingNext ?? state.iterator.next();
state.pendingNext = next;
- const result = await Promise.race([
+ let idleTimer: ReturnType<typeof setTimeout> | undefined;
+ const race: Array<Promise<
+ | { type: "event"; value: IteratorResult<CursorStreamEvent> }
+ | { type: "tool"; tool: PendingNativeTool }
+ | { type: "idle" }
+ >> = [
next.then((value) => ({ type: "event" as const, value })),
waitForPendingTool(state).then((tool) => ({ type: "tool" as const, tool })),
- ]);
+ ];
+ if (state.outputSeen && state.outputIdleTimeoutMs > 0) {
+ race.push(new Promise<{ type: "idle" }>((resolve) => {
+ idleTimer = setTimeout(() => resolve({ type: "idle" }), state.outputIdleTimeoutMs);
+ }));
+ }
+ const result = await Promise.race(race).finally(() => {
+ if (idleTimer) clearTimeout(idleTimer);
+ });
if (result.type === "tool") {
emitToolUseAndPause(result.tool);
return;
}
+ if (result.type === "idle") {
+ state.log.warn("cursor bridge stream idle after output", {
+ idleMs: state.outputIdleTimeoutMs,
+ });
+ state.traffic?.writeJsonEvent("040-cursor-event", {
+ type: "end",
+ reason: "output_idle_timeout",
+ idleMs: state.outputIdleTimeoutMs,
+ });
+ state.pendingNext = undefined;
+ await state.iterator.return?.(undefined);
+ break;
+ }
+
state.pendingNext = undefined;
if (result.value.done) break;
const event = result.value.value;
state.traffic?.writeJsonEvent("040-cursor-event", event);
+ if (event.type === "thinking_delta" || event.type === "text_delta" || event.type === "usage") {
+ state.outputSeen = true;
+ }
switch (event.type) {
case "session":
state.onSession?.(event.sessionId);