Skip to content

Commit b261f1c

Browse files
committed
fix(cursor): handle omitted subagent result fields
Fixes #26486
1 parent 5211acf commit b261f1c

4 files changed

Lines changed: 259 additions & 44 deletions

File tree

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, describe, it } from "@effect/vitest";
33
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
44
import * as Effect from "effect/Effect";
5+
import * as Fiber from "effect/Fiber";
56
import * as Layer from "effect/Layer";
67
import { vi } from "vite-plus/test";
78

@@ -22,7 +23,9 @@ const cursorSdkMock = vi.hoisted(() => {
2223
model: { id: "default" },
2324
durationMs: 1,
2425
}));
26+
let rejectRunWait: ((cause: unknown) => void) | undefined;
2527
const runCancel = vi.fn(async () => {});
28+
let onDelta: ((input: { readonly update: unknown }) => Promise<void>) | undefined;
2629
const agentClose = vi.fn(() => {
2730
closeExecutionOrder.push("sdk:agent.close");
2831
if (agentCloseFailure !== undefined) {
@@ -35,6 +38,7 @@ const cursorSdkMock = vi.hoisted(() => {
3538
options: { readonly onDelta?: (input: { readonly update: unknown }) => Promise<void> },
3639
) => {
3740
runOperations.push("run.start");
41+
onDelta = options.onDelta;
3842
if (staleRunActive) {
3943
throw new Error("Agent agent-cursor-agent-sdk-test already has active run");
4044
}
@@ -84,6 +88,16 @@ const cursorSdkMock = vi.hoisted(() => {
8488
runCancel,
8589
runWait,
8690
send,
91+
deliverDelta: async (update: unknown) => onDelta?.({ update }),
92+
setRunWaitPending: () => {
93+
runWait.mockImplementationOnce(
94+
() =>
95+
new Promise((_resolve, reject) => {
96+
rejectRunWait = reject;
97+
}),
98+
);
99+
},
100+
rejectRunWait: (cause: unknown) => rejectRunWait?.(cause),
87101
setAgentCloseFailure: (failure: unknown) => {
88102
agentCloseFailure = failure;
89103
},
@@ -132,6 +146,85 @@ const layerTest = CursorAgentSdk.layer.pipe(
132146
);
133147

134148
describe("CursorAgentSdkRunner", () => {
149+
it.effect("keeps dispatching after a callback failure and reports its method", () =>
150+
Effect.gen(function* () {
151+
const runner = yield* CursorAgentSdk.CursorAgentSdkRunner;
152+
const session = yield* runner.open({
153+
operation: "create",
154+
options: { model: { id: "default" }, mode: "agent", local: { cwd: process.cwd() } },
155+
threadId: ThreadId.make("thread-cursor-agent-sdk-callback-failure-test"),
156+
providerSessionId: ProviderSessionId.make(
157+
"provider-session-cursor-agent-sdk-callback-failure-test",
158+
),
159+
});
160+
const received: Array<string> = [];
161+
const run = yield* session.send({
162+
message: "continue",
163+
onDelta: (update) => {
164+
if (update.type === "text-delta" && update.text === "bad") {
165+
return Effect.die(new Error("local callback failed"));
166+
}
167+
if (update.type === "text-delta") received.push(update.text);
168+
return Effect.void;
169+
},
170+
});
171+
172+
yield* Effect.promise(() => cursorSdkMock.deliverDelta({ type: "text-delta", text: "bad" }));
173+
yield* Effect.promise(() =>
174+
cursorSdkMock.deliverDelta({ type: "text-delta", text: "after failure" }),
175+
);
176+
const error = yield* run.wait.pipe(Effect.flip);
177+
178+
assert.deepEqual(received, ["after failure"]);
179+
assert.equal(error.method, "run.onDelta");
180+
}).pipe(Effect.provide(layerTest)),
181+
);
182+
183+
it.effect("waits for an in-flight callback before classifying a run.wait failure", () =>
184+
Effect.gen(function* () {
185+
cursorSdkMock.setRunWaitPending();
186+
cursorSdkMock.runCancel.mockClear();
187+
let startCallback!: () => void;
188+
const callbackStarted = new Promise<void>((resolve) => {
189+
startCallback = resolve;
190+
});
191+
let failCallback!: () => void;
192+
const runner = yield* CursorAgentSdk.CursorAgentSdkRunner;
193+
const session = yield* runner.open({
194+
operation: "create",
195+
options: { model: { id: "default" }, mode: "agent", local: { cwd: process.cwd() } },
196+
threadId: ThreadId.make("thread-cursor-agent-sdk-wait-race-test"),
197+
providerSessionId: ProviderSessionId.make(
198+
"provider-session-cursor-agent-sdk-wait-race-test",
199+
),
200+
});
201+
const run = yield* session.send({
202+
message: "continue",
203+
onDelta: (update) => {
204+
if (update.type !== "text-delta" || update.text !== "pending") return Effect.void;
205+
return Effect.promise(
206+
() =>
207+
new Promise<void>((_resolve, reject) => {
208+
failCallback = () => reject(new Error("local callback failed"));
209+
startCallback();
210+
}),
211+
);
212+
},
213+
});
214+
215+
const wait = yield* run.wait.pipe(Effect.flip, Effect.forkChild({ startImmediately: true }));
216+
const delivery = cursorSdkMock.deliverDelta({ type: "text-delta", text: "pending" });
217+
yield* Effect.promise(() => callbackStarted);
218+
yield* Effect.sync(() => cursorSdkMock.rejectRunWait(new Error("SDK wait failed")));
219+
yield* Effect.sync(() => failCallback());
220+
yield* Effect.promise(() => delivery);
221+
222+
const error = yield* Fiber.join(wait);
223+
assert.equal(error.method, "run.onDelta");
224+
assert.equal(cursorSdkMock.runCancel.mock.calls.length, 0);
225+
}).pipe(Effect.provide(layerTest)),
226+
);
227+
135228
it.effect("retries a resumed send after cancelling its abandoned local run", () =>
136229
Effect.gen(function* () {
137230
cursorSdkMock.runOperations.length = 0;

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
421421
const dispatchUpdate = (update: InteractionUpdate): Promise<void> => {
422422
callbackChain = callbackChain
423423
.then(() => {
424-
if (callbackFailure !== undefined) {
425-
return;
426-
}
427424
return Effect.runPromiseWith(context)(
428425
log({
429426
direction: "incoming",
@@ -510,14 +507,25 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
510507
agentId: run.agentId,
511508
wait: Effect.tryPromise({
512509
try: async () => {
513-
const result = await run.wait();
514-
await callbackChain;
510+
const [runResult] = await Promise.allSettled([run.wait()]);
511+
let completedCallbacks: Promise<void>;
512+
do {
513+
completedCallbacks = callbackChain;
514+
await completedCallbacks;
515+
} while (callbackChain !== completedCallbacks);
515516
if (callbackFailure !== undefined) {
516517
throw callbackFailure.cause;
517518
}
518-
return result;
519+
if (runResult.status === "rejected") {
520+
throw runResult.reason;
521+
}
522+
return runResult.value;
519523
},
520-
catch: (cause) => runnerError(cause, "run.wait"),
524+
catch: (cause) =>
525+
runnerError(
526+
callbackFailure?.cause ?? cause,
527+
callbackFailure === undefined ? "run.wait" : "run.onDelta",
528+
),
521529
}).pipe(
522530
Effect.tap((result) =>
523531
log({

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ describe("CursorAdapterV2", () => {
586586
),
587587
);
588588

589-
it.effect("projects Cursor directory trees and lint diagnostics as file search results", () =>
589+
it.effect("projects Cursor directory trees after a buffered callback failure", () =>
590590
Effect.gen(function* () {
591591
const fileSystem = yield* FileSystem.FileSystem;
592592
const path = yield* Path.Path;
@@ -606,7 +606,53 @@ describe("CursorAdapterV2", () => {
606606
fullSubtreeExtensionCounts: {},
607607
numFiles: 0,
608608
};
609+
const omittedChildrenPath = path.join(workspace, "empty-json");
610+
const omittedRootPath = path.join(workspace, "missing-json-root");
611+
const wrappedLs = nestedToolCallFromEnvelope({
612+
toolCallId: "ls-omitted-children",
613+
lsToolCall: {
614+
args: { path: omittedChildrenPath },
615+
result: {
616+
success: {
617+
directoryTreeRoot: { absPath: omittedChildrenPath },
618+
},
619+
},
620+
},
621+
});
622+
const wrappedLsWithoutRoot = nestedToolCallFromEnvelope({
623+
toolCallId: "ls-omitted-root",
624+
lsToolCall: {
625+
args: { path: omittedRootPath },
626+
result: { success: {} },
627+
},
628+
});
629+
const wrappedLints = nestedToolCallFromEnvelope({
630+
toolCallId: "lints-omitted-arrays",
631+
readLintsToolCall: { args: {}, result: { success: {} } },
632+
});
633+
const wrappedMcp = nestedToolCallFromEnvelope({
634+
toolCallId: "mcp-omitted-content",
635+
mcpToolCall: {
636+
args: { providerIdentifier: "empty", toolName: "tool" },
637+
result: { success: {} },
638+
},
639+
});
640+
const wrappedTodos = nestedToolCallFromEnvelope({
641+
toolCallId: "todos-omitted",
642+
updateTodosToolCall: { args: {}, result: { success: {} } },
643+
});
644+
assert.isDefined(wrappedLs);
645+
assert.isDefined(wrappedLsWithoutRoot);
646+
assert.isDefined(wrappedLints);
647+
assert.isDefined(wrappedMcp);
648+
assert.isDefined(wrappedTodos);
609649
const updates: ReadonlyArray<InteractionUpdate> = [
650+
{
651+
type: "tool-call-completed",
652+
modelCallId: "native-model-call",
653+
callId: "malformed-early-call",
654+
toolCall: null,
655+
} as unknown as InteractionUpdate,
610656
...(["tool-call-started", "tool-call-completed"] as const).map((type) => ({
611657
type,
612658
modelCallId: "native-model-call",
@@ -684,6 +730,21 @@ describe("CursorAdapterV2", () => {
684730
},
685731
},
686732
},
733+
{
734+
type: "tool-call-completed",
735+
modelCallId: "native-model-call",
736+
callId: wrappedLs.callId,
737+
toolCall: wrappedLs.toolCall,
738+
} as unknown as InteractionUpdate,
739+
...[wrappedLsWithoutRoot, wrappedLints, wrappedMcp, wrappedTodos].map(
740+
(wrapped) =>
741+
({
742+
type: "tool-call-completed" as const,
743+
modelCallId: "native-model-call",
744+
callId: wrapped.callId,
745+
toolCall: wrapped.toolCall,
746+
}) as unknown as InteractionUpdate,
747+
),
687748
{
688749
type: "tool-call-completed",
689750
modelCallId: "native-model-call",
@@ -884,6 +945,9 @@ describe("CursorAdapterV2", () => {
884945
Stream.takeUntil((event) => event.type === "turn.terminal"),
885946
Stream.runCollect,
886947
);
948+
const terminal = events.find((event) => event.type === "turn.terminal");
949+
assert.equal(terminal?.status, "failed");
950+
assert.equal(terminal?.failure?.class, "provider_error");
887951
const mcpItems = events.flatMap((event) =>
888952
event.type === "turn_item.updated" &&
889953
event.turnItem.type === "dynamic_tool" &&
@@ -944,6 +1008,21 @@ describe("CursorAdapterV2", () => {
9441008
status: "completed",
9451009
results: undefined,
9461010
},
1011+
{
1012+
pattern: omittedChildrenPath,
1013+
status: "completed",
1014+
results: undefined,
1015+
},
1016+
{
1017+
pattern: omittedRootPath,
1018+
status: "completed",
1019+
results: undefined,
1020+
},
1021+
{
1022+
pattern: "",
1023+
status: "completed",
1024+
results: undefined,
1025+
},
9471026
{
9481027
pattern: path.join(workspace, "missing"),
9491028
status: "failed",

0 commit comments

Comments
 (0)