Skip to content

Commit 4e0b528

Browse files
fix(cursor): agent shell commands stream their output while they run (#34843)
1 parent 6be2685 commit 4e0b528

6 files changed

Lines changed: 143 additions & 23 deletions

File tree

‎packages/provider-cursor/src/server/CursorAgentSdk.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,55 @@ describe("CursorAgentSdkRunner", () => {
273273
}).pipe(Effect.provide(layerTest)),
274274
);
275275

276+
it.effect("forwards the agent's own shell stream output as shell-output-delta", () =>
277+
Effect.gen(function* () {
278+
const publish = (conversationId: string, data: string) =>
279+
(Reflect.get(globalThis, Symbol.for("@cursor/sdk.shellStreamOutput")) as Function)({
280+
conversationId,
281+
toolCallId: "tool-shell",
282+
event: { case: "stdout", value: { data } },
283+
});
284+
cursorSdkMock.runWait.mockImplementationOnce(async () => {
285+
publish("agent-cursor-agent-sdk-test", "tick 1\n");
286+
publish("agent-someone-else", "not ours\n");
287+
return {
288+
id: "run-cursor-agent-sdk-test",
289+
requestId: "request-cursor-agent-sdk-test",
290+
status: "finished",
291+
result: "ok",
292+
model: { id: "default" },
293+
durationMs: 1,
294+
};
295+
});
296+
const updates: Array<unknown> = [];
297+
const runner = yield* CursorAgentSdk.CursorAgentSdkRunner;
298+
const session = yield* runner.open({
299+
operation: "create",
300+
options: {},
301+
threadId: ThreadId.make("thread-cursor-agent-sdk-shell"),
302+
providerSessionId: ProviderSessionId.make("provider-session-cursor-agent-sdk-shell"),
303+
});
304+
const run = yield* session.send({
305+
message: "run it",
306+
onDelta: (update) => Effect.sync(() => updates.push(update)),
307+
});
308+
yield* run.wait;
309+
publish("agent-cursor-agent-sdk-test", "after the run\n");
310+
311+
assert.deepStrictEqual(
312+
updates.filter(
313+
(update) => (update as { readonly type: string }).type === "shell-output-delta",
314+
),
315+
[
316+
{
317+
type: "shell-output-delta",
318+
event: { case: "stdout", value: { data: "tick 1\n" }, toolCallId: "tool-shell" },
319+
},
320+
],
321+
);
322+
}).pipe(Effect.provide(layerTest)),
323+
);
324+
276325
it.effect("forwards Run.steer and reports a missing method as a follow-up", () =>
277326
Effect.gen(function* () {
278327
cursorSdkMock.runSteer.mockClear();

‎packages/provider-cursor/src/server/CursorAgentSdk.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,31 @@ function makeCursorAgentSdkProtocolLogger(input: {
367367
.pipe(Effect.ignore);
368368
}
369369

370+
interface CursorShellStreamOutput {
371+
readonly conversationId?: unknown;
372+
readonly toolCallId?: unknown;
373+
readonly event: {
374+
readonly case: "stdout" | "stderr";
375+
readonly value: { readonly data: unknown };
376+
};
377+
}
378+
379+
// The SDK reports shell-output-delta only for user-started shell commands, so
380+
// patches/@cursor__sdk@1.2.0.patch publishes agent shell output on this hook.
381+
const SHELL_STREAM_OUTPUT_HOOK = Symbol.for("@cursor/sdk.shellStreamOutput");
382+
const shellStreamListeners = new Set<(output: CursorShellStreamOutput) => void>();
383+
384+
function subscribeShellStreamOutput(listener: (output: CursorShellStreamOutput) => void) {
385+
const hooks = globalThis as Record<symbol, unknown>;
386+
hooks[SHELL_STREAM_OUTPUT_HOOK] ??= (output: CursorShellStreamOutput) => {
387+
for (const notify of shellStreamListeners) notify(output);
388+
};
389+
shellStreamListeners.add(listener);
390+
return () => {
391+
shellStreamListeners.delete(listener);
392+
};
393+
}
394+
370395
export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(function* (
371396
protocolLoggerFor: (input: CursorAgentSdkOpenInput) => CursorAgentSdkProtocolLogger | undefined,
372397
) {
@@ -592,6 +617,15 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
592617
},
593618
});
594619
callbacksReady = true;
620+
const unsubscribeShellOutput = subscribeShellStreamOutput((output) => {
621+
if (output.conversationId !== agent.agentId || typeof output.toolCallId !== "string") {
622+
return;
623+
}
624+
void dispatchUpdate({
625+
type: "shell-output-delta",
626+
event: { ...output.event, toolCallId: output.toolCallId },
627+
});
628+
});
595629
for (const update of pendingUpdates) {
596630
yield* Effect.tryPromise({
597631
try: () => dispatchUpdate(update),
@@ -613,6 +647,7 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
613647
},
614648
catch: (cause) => runnerError(cause, "run.wait"),
615649
}).pipe(
650+
Effect.ensuring(Effect.sync(unsubscribeShellOutput)),
616651
Effect.tap((result) =>
617652
log({
618653
direction: "incoming",

‎packages/provider-cursor/src/server/adapter.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,20 @@ describe("CursorAdapterV2", () => {
12191219
result: { status: "error", error: "lint failed" },
12201220
},
12211221
},
1222+
...["slow-shell", "fast-shell"].map((callId) => ({
1223+
type: "tool-call-started" as const,
1224+
modelCallId: "native-model-call",
1225+
callId,
1226+
toolCall: { type: "shell" as const, args: { command: callId } },
1227+
})),
1228+
{
1229+
type: "shell-output-delta",
1230+
event: { case: "stdout", value: { data: "tick 1\n" }, toolCallId: "slow-shell" },
1231+
},
1232+
{
1233+
type: "shell-output-delta",
1234+
event: { case: "exit", value: { code: 0 }, toolCallId: "slow-shell" },
1235+
},
12221236
];
12231237
const adapter = yield* makeCursorAdapterV2({
12241238
instanceId,
@@ -1332,6 +1346,21 @@ describe("CursorAdapterV2", () => {
13321346
});
13331347
assert.deepEqual(item.input, { city: "Berlin" });
13341348
}
1349+
const shellItemIds = new Map(
1350+
events.flatMap((event) =>
1351+
event.type === "turn_item.updated" && event.turnItem.type === "command_execution"
1352+
? [[event.turnItem.id, event.turnItem.input] as const]
1353+
: [],
1354+
),
1355+
);
1356+
assert.deepEqual(
1357+
events.flatMap((event) =>
1358+
event.type === "command_output.delta"
1359+
? [{ command: shellItemIds.get(event.itemId), chunk: event.chunk }]
1360+
: [],
1361+
),
1362+
[{ command: "slow-shell", chunk: "tick 1\n" }],
1363+
);
13351364
assert.deepEqual(toolOutputImages(mcpItems.at(-1)?.output), [
13361365
{ mimeType: "image/png", data: "iVBORw0KGgo=" },
13371366
]);

‎packages/provider-cursor/src/server/adapter.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,15 +1998,10 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
19981998
});
19991999

20002000
const shellOutputText = (event: Record<string, unknown>): string => {
2001-
const candidates = [
2002-
event.text,
2003-
event.output,
2004-
event.data,
2005-
event.stdout,
2006-
event.stderr,
2007-
event.chunk,
2008-
];
2009-
return candidates.filter((value): value is string => typeof value === "string").join("");
2001+
if (event.case !== "stdout" && event.case !== "stderr") return "";
2002+
const value = event.value;
2003+
if (typeof value !== "object" || value === null || !("data" in value)) return "";
2004+
return typeof value.data === "string" ? value.data : "";
20102005
};
20112006

20122007
const handleInteractionUpdate = Effect.fnUntraced(function* (
@@ -2071,10 +2066,14 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
20712066
yield* completeReasoning(context);
20722067
return;
20732068
case "shell-output-delta": {
2074-
const shell = Array.from(context.tools.values())
2075-
.toReversed()
2076-
.find((candidate) => candidate.toolCall.type === "shell");
2077-
if (shell === undefined) {
2069+
const toolCallId = update.event.toolCallId;
2070+
const shell =
2071+
typeof toolCallId === "string"
2072+
? context.tools.get(toolCallId)
2073+
: Array.from(context.tools.values())
2074+
.toReversed()
2075+
.find((candidate) => candidate.toolCall.type === "shell");
2076+
if (shell?.toolCall.type !== "shell") {
20782077
return;
20792078
}
20802079
const chunk = shellOutputText(update.event);

0 commit comments

Comments
 (0)