TypeScript
fix(cursor): subagent ls, lint and MCP results no longer fail the parent run (#26592)
Julius Marminge37a9ca049c9a
diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.tsindex 5e7093b67ae..d91d3bc3399 100644--- a/packages/provider-cursor/src/server/adapter.test.ts+++ b/packages/provider-cursor/src/server/adapter.test.ts@@ -1140,6 +1140,227 @@ describe("CursorAdapterV2", () => {),);+ it.effect("projects subagent directory trees Cursor serialized without empty lists", () =>+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-task-ls-" });+ const instanceId = ProviderInstanceId.make("cursor");+ const threadId = ThreadId.make("cursor-task-ls-thread");+ const modelSelection = { instanceId, model: "composer-2.5" };+ const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({+ runtimeMode: "full-access",+ interactionMode: "default",+ cwd: workspace,+ });+ // The SDK serializes subagent steps with protobuf toJson(), which omits empty repeated fields.+ const conversationSteps = [+ {+ toolCall: {+ toolCallId: "tool:ls",+ lsToolCall: {+ args: { path: workspace },+ result: {+ success: {+ directoryTreeRoot: {+ absPath: workspace,+ childrenWereProcessed: true,+ childrenDirs: [+ {+ absPath: path.join(workspace, "src"),+ childrenFiles: [{ name: "index.ts" }],+ childrenWereProcessed: true,+ numFiles: 1,+ },+ ],+ },+ },+ },+ },+ },+ },+ {+ toolCall: {+ toolCallId: "tool:lints",+ readLintsToolCall: { args: {}, result: { success: {} } },+ },+ },+ {+ toolCall: {+ toolCallId: "tool:lints-clean-file",+ readLintsToolCall: {+ args: {},+ result: { success: { fileDiagnostics: [{ path: "src/clean.ts" }] } },+ },+ },+ },+ {+ toolCall: {+ toolCallId: "tool:mcp",+ mcpToolCall: {+ args: { providerIdentifier: "docs", toolName: "lookup", args: { query: "ls" } },+ result: { success: {} },+ },+ },+ },+ ];+ const adapter = yield* makeCursorAdapterV2({+ instanceId,+ environment: { HOME: workspace },+ }).pipe(+ Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {+ assertComplete: Effect.void,+ open: () =>+ Effect.succeed({+ agentId: "native-cursor-task-ls",+ listMessages: Effect.succeed([]),+ close: Effect.void,+ send: (input) =>+ Effect.gen(function* () {+ const args = {+ description: "Explore",+ prompt: "List the workspace.",+ subagentType: { kind: "unspecified" as const },+ agentId: "cursor-task-agent",+ mode: "unspecified" as const,+ };+ yield* input.onDelta!({+ type: "tool-call-started",+ modelCallId: "model-call",+ callId: "task-call",+ toolCall: { type: "task", args },+ });+ yield* input.onDelta!({+ type: "tool-call-completed",+ modelCallId: "model-call",+ callId: "task-call",+ toolCall: {+ type: "task",+ args,+ result: {+ status: "success",+ value: {+ conversationSteps,+ isBackground: false,+ backgroundReason: "unspecified",+ },+ },+ },+ });+ return {+ agentId: "native-cursor-task-ls",+ runId: "native-cursor-run",+ wait: Effect.succeed({+ id: "native-cursor-run",+ requestId: "native-request",+ status: "finished" as const,+ model: { id: "composer-2.5" },+ durationMs: 1,+ }),+ cancel: Effect.void,+ };+ }).pipe(Effect.orDie),+ }),+ }),+ Effect.provide(TestProviderHost.layer({ cwd: workspace })),+ );+ const runtime = yield* adapter.openSession({+ threadId,+ providerSessionId: ProviderSessionId.make("cursor-task-ls-session"),+ modelSelection,+ runtimePolicy,+ });+ const providerThread = yield* runtime.ensureThread({+ threadId,+ modelSelection,+ runtimePolicy,+ });+ const now = yield* DateTime.now;+ yield* runtime.startTurn({+ threadId,+ providerThread,+ modelSelection,+ runtimePolicy,+ runId: RunId.make("cursor-task-ls-run"),+ runOrdinal: 1,+ providerTurnOrdinal: 1,+ attemptId: RunAttemptId.make("cursor-task-ls-attempt"),+ rootNodeId: NodeId.make("cursor-task-ls-root"),+ appThread: {+ id: threadId,+ projectId: ProjectId.make("cursor-task-ls-project"),+ createdBy: "user",+ creationSource: "web",+ title: "Cursor subagent ls",+ providerInstanceId: instanceId,+ modelSelection,+ runtimeMode: "full-access",+ interactionMode: "default",+ branch: null,+ worktreePath: null,+ activeProviderThreadId: providerThread.id,+ lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },+ forkedFrom: null,+ createdAt: now,+ updatedAt: now,+ archivedAt: null,+ settledOverride: null,+ settledAt: null,+ lastVisitedAt: null,+ deletedAt: null,+ },+ message: {+ messageId: MessageId.make("cursor-task-ls-message"),+ createdBy: "user",+ creationSource: "web",+ text: "List the workspace with a subagent.",+ attachments: [],+ },+ });+ const events = yield* runtime.events.pipe(+ Stream.takeUntil((event) => event.type === "turn.terminal"),+ Stream.runCollect,+ );+ const terminal = events.find((event) => event.type === "turn.terminal");+ assert.equal(terminal?.status, "completed");+ const childThreadId = events.findLast((event) => event.type === "subagent.updated")?.subagent+ .childThreadId;+ assert.isDefined(childThreadId);+ assert.deepEqual(+ events.flatMap((event): ReadonlyArray<unknown> => {+ if (event.type !== "turn_item.updated" || event.turnItem.threadId !== childThreadId) {+ return [];+ }+ const item = event.turnItem;+ return item.type === "file_search"+ ? [{ status: item.status, pattern: item.pattern, results: item.results ?? [] }]+ : item.type === "dynamic_tool"+ ? [{ status: item.status, toolName: item.toolName, output: item.output }]+ : [];+ }),+ [+ {+ status: "completed",+ pattern: workspace,+ results: [{ fileName: path.join(workspace, "src", "index.ts") }],+ },+ { status: "completed", pattern: "", results: [] },+ { status: "completed", pattern: "", results: [] },+ {+ status: "completed",+ toolName: "mcp__docs__lookup",+ output: { content: [], isError: false },+ },+ ],+ );+ }).pipe(+ Effect.scoped,+ Effect.provide(+ Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer),+ ),+ ),+ );+it.effect("names a custom model Cursor's account catalog rejects", () =>Effect.gen(function* () {const fileSystem = yield* FileSystem.FileSystem;diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.tsindex 1d98c4523ff..93652e492e0 100644--- a/packages/provider-cursor/src/server/adapter.ts+++ b/packages/provider-cursor/src/server/adapter.ts@@ -625,6 +625,29 @@ function nestedGrepWorkspaceResults(success: Record<string, unknown>): Record<st);}+function nestedArray(value: unknown): ReadonlyArray<unknown> {+ return Array.isArray(value) ? value : [];+}++function nestedLsDirectory(rawNode: unknown): CursorLsDirectoryNode {+ const node = unknownRecord(rawNode);+ return {+ absPath: typeof node?.absPath === "string" ? node.absPath : "",+ childrenDirs: nestedArray(node?.childrenDirs).map(nestedLsDirectory),+ childrenFiles: nestedArray(node?.childrenFiles).flatMap((rawFile) => {+ const name = unknownRecord(rawFile)?.name;+ return typeof name === "string" ? [{ name }] : [];+ }),+ childrenWereProcessed: node?.childrenWereProcessed === true,+ fullSubtreeExtensionCounts: Object.fromEntries(+ Object.entries(unknownRecord(node?.fullSubtreeExtensionCounts) ?? {}).filter(+ (entry): entry is [string, number] => typeof entry[1] === "number",+ ),+ ),+ numFiles: typeof node?.numFiles === "number" ? node.numFiles : 0,+ };+}+export function nestedToolCallFromEnvelope(envelope: Record<string, unknown>,): { readonly callId: string; readonly toolCall: ToolCall } | undefined {@@ -730,6 +753,66 @@ export function nestedToolCallFromEnvelope(} as unknown as ToolCall,};}+ case "lsToolCall":+ return {+ callId: nestedCallId,+ toolCall: {+ type: "ls",+ args: { path: typeof args.path === "string" ? args.path : "" },+ result: nestedToolResult(call.result, (value) => ({+ directoryTreeRoot: nestedLsDirectory(value.directoryTreeRoot),+ })),+ } as unknown as ToolCall,+ };+ case "readLintsToolCall":+ return {+ callId: nestedCallId,+ toolCall: {+ type: "readLints",+ args: {+ paths: nestedArray(args.paths).filter(+ (path): path is string => typeof path === "string",+ ),+ },+ result: nestedToolResult(call.result, (value) => ({+ fileDiagnostics: nestedArray(value.fileDiagnostics).map((rawFile) => {+ const file = unknownRecord(rawFile);+ return {+ path: typeof file?.path === "string" ? file.path : "",+ diagnostics: nestedArray(file?.diagnostics).flatMap((rawDiagnostic) => {+ const diagnostic = unknownRecord(rawDiagnostic);+ return diagnostic === undefined+ ? []+ : [+ {+ ...diagnostic,+ message: typeof diagnostic.message === "string" ? diagnostic.message : "",+ },+ ];+ }),+ };+ }),+ totalFiles: typeof value.totalFiles === "number" ? value.totalFiles : 0,+ totalDiagnostics:+ typeof value.totalDiagnostics === "number" ? value.totalDiagnostics : 0,+ })),+ } as unknown as ToolCall,+ };+ case "mcpToolCall":+ return {+ callId: nestedCallId,+ toolCall: {+ type: "mcp",+ args,+ result: nestedToolResult(call.result, (value) => ({+ content: nestedArray(value.content).flatMap((rawPart) => {+ const part = unknownRecord(rawPart);+ return part === undefined ? [] : [part];+ }),+ isError: value.isError === true,+ })),+ } as unknown as ToolCall,+ };default: {const type = wrapperName.slice(0, -"ToolCall".length);return {