@devries-cameron20 / t3code-1 / commits / 1c01db02b3
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(cursor): stream Task subagent work into child threads live (#30539)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Humberto Gontijo committed Nov 14, 2026 at 20:40 UTC 1c01db02b3d2e7c06bdcdb6d648bddb2146ff36f
2 files changed +526 -53
packages/provider-cursor/src/server/adapter.test.ts
+311
@@ -1749,6 +1749,168 @@ describe("CursorAdapterV2", () => {
1749 ),
1750 );
1751
1752 + it.effect("streams Cursor Task updates into the child thread before the task completes", () =>
1753 + Effect.gen(function* () {
1754 + const read = { type: "read" as const, args: { path: "package.json" } };
1755 + const { events, childThreadId } = yield* runCursorTaskTurn(
1756 + [
1757 + { type: "thinking-delta", text: "Reading package.json." },
1758 + { type: "thinking-completed", thinkingDurationMs: 1 },
1759 + { type: "text-delta", text: "Looking." },
1760 + { type: "step-completed", stepId: 1, stepDurationMs: 1 },
1761 + { type: "text-delta", text: "Reading it." },
1762 + {
1763 + type: "tool-call-started",
1764 + modelCallId: "nested",
1765 + callId: "nested-read",
1766 + toolCall: read,
1767 + },
1768 + {
1769 + type: "tool-call-completed",
1770 + modelCallId: "nested",
1771 + callId: "nested-read",
1772 + toolCall: {
1773 + ...read,
1774 + result: {
1775 + status: "success",
1776 + value: { content: '{"name":"fixture"}', totalLines: 1, fileSize: 18 },
1777 + },
1778 + },
1779 + },
1780 + ],
1781 + [
1782 + { assistantMessage: { text: "Looking." } },
1783 + { assistantMessage: { text: "Reading it." } },
1784 + {
1785 + toolCall: {
1786 + toolCallId: "nested-read",
1787 + readToolCall: { args: { path: "package.json" }, result: { success: {} } },
1788 + },
1789 + },
1790 + { assistantMessage: { text: "The name is fixture." } },
1791 + ],
1792 + );
1793 + const taskCompletedAt = events.findIndex(
1794 + (event) => event.type === "subagent.updated" && event.subagent.status === "completed",
1795 + );
1796 + const childItems = events.flatMap((event, index) =>
1797 + event.type === "turn_item.updated" && event.turnItem.threadId === childThreadId
1798 + ? [{ index, item: event.turnItem }]
1799 + : [],
1800 + );
1801 + for (const type of ["reasoning", "dynamic_tool", "assistant_message"] as const) {
1802 + assert.isTrue(
1803 + childItems.some(({ index, item }) => item.type === type && index < taskCompletedAt),
1804 + `${type} reached the child thread before the task completed`,
1805 + );
1806 + }
1807 + assert.deepEqual(
1808 + childItems.flatMap(({ item }): ReadonlyArray<unknown> =>
1809 + item.status !== "completed"
1810 + ? []
1811 + : item.type === "reasoning" || item.type === "assistant_message"
1812 + ? [{ type: item.type, text: item.text }]
1813 + : item.type === "dynamic_tool"
1814 + ? [{ type: item.type, toolName: item.toolName }]
1815 + : [],
1816 + ),
1817 + [
1818 + { type: "reasoning", text: "Reading package.json." },
1819 + { type: "assistant_message", text: "Looking." },
1820 + { type: "assistant_message", text: "Reading it." },
1821 + { type: "dynamic_tool", toolName: "Read" },
1822 + { type: "assistant_message", text: "The name is fixture." },
1823 + ],
1824 + );
1825 + }).pipe(
1826 + Effect.scoped,
1827 + Effect.provide(
1828 + Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer),
1829 + ),
1830 + ),
1831 + );
1832 +
1833 + it.effect("fills a Cursor Task child from its saved steps where live updates fell short", () =>
1834 + Effect.gen(function* () {
1835 + const { events, childThreadId } = yield* runCursorTaskTurn(
1836 + [
1837 + { type: "step-started", stepId: 1 },
1838 + {
1839 + type: "tool-call-started",
1840 + modelCallId: "nested",
1841 + callId: "open",
1842 + toolCall: { type: "read", args: { path: "open.json" } },
1843 + },
1844 + {
1845 + type: "tool-call-started",
1846 + modelCallId: "nested",
1847 + callId: "lost",
1848 + toolCall: { type: "read", args: { path: "lost.json" } },
1849 + },
1850 + {
1851 + type: "tool-call-completed",
1852 + modelCallId: "nested",
1853 + callId: "live",
1854 + toolCall: {
1855 + type: "read",
1856 + args: { path: "live.json" },
1857 + result: {
1858 + status: "success",
1859 + value: { content: "{}", totalLines: 1, fileSize: 2 },
1860 + },
1861 + },
1862 + },
1863 + { type: "step-completed", stepId: 1, stepDurationMs: 1 },
1864 + ],
1865 + [
1866 + {
1867 + toolCall: {
1868 + toolCallId: "open",
1869 + readToolCall: { args: { path: "open.json" }, result: { success: {} } },
1870 + },
1871 + },
1872 + {
1873 + toolCall: {
1874 + toolCallId: "live",
1875 + readToolCall: { args: { path: "live.json" }, result: { success: {} } },
1876 + },
1877 + },
1878 + {
1879 + toolCall: {
1880 + toolCallId: "saved",
1881 + readToolCall: { args: { path: "saved.json" }, result: { success: {} } },
1882 + },
1883 + },
1884 + { assistantMessage: { text: "Saved answer." } },
1885 + ],
1886 + );
1887 + const finalItems = new Map<string, string>();
1888 + for (const event of events) {
1889 + if (event.type !== "turn_item.updated" || event.turnItem.threadId !== childThreadId) {
1890 + continue;
1891 + }
1892 + const item = event.turnItem;
1893 + if (item.type === "dynamic_tool") {
1894 + finalItems.set(item.title ?? "", item.status);
1895 + } else if (item.type === "assistant_message") {
1896 + finalItems.set(item.text, item.status);
1897 + }
1898 + }
1899 + assert.deepEqual(Object.fromEntries(finalItems), {
1900 + "Read open.json": "completed",
1901 + "Read lost.json": "interrupted",
1902 + "Read live.json": "completed",
1903 + "Read saved.json": "completed",
1904 + "Saved answer.": "completed",
1905 + });
1906 + }).pipe(
1907 + Effect.scoped,
1908 + Effect.provide(
1909 + Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer),
1910 + ),
1911 + ),
1912 + );
1913 +
1914 it.effect("keeps the last 256 KiB of streamed shell output when the result carries none", () =>
1915 Effect.gen(function* () {
1916 const fileSystem = yield* FileSystem.FileSystem;
@@ -2731,6 +2893,155 @@ describe("CursorAdapterV2", () => {
2893 });
2894 });
2895
2896 +type CursorTaskUpdate = Extract<
2897 + InteractionUpdate,
2898 + { readonly type: "tool-call-delta" }
2899 +>["taskUpdate"];
2900 +
2901 +// Runs one Cursor turn whose only tool is a Task that streams `taskUpdates`, then completes with
2902 +// `conversationSteps` as the saved transcript.
2903 +const runCursorTaskTurn = Effect.fnUntraced(function* (
2904 + taskUpdates: ReadonlyArray<CursorTaskUpdate>,
2905 + conversationSteps: ReadonlyArray<unknown>,
2906 +) {
2907 + const fileSystem = yield* FileSystem.FileSystem;
2908 + const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-task-" });
2909 + const instanceId = ProviderInstanceId.make("cursor");
2910 + const threadId = ThreadId.make("cursor-task-thread");
2911 + const modelSelection = { instanceId, model: "composer-2.5" };
2912 + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2913 + runtimeMode: "full-access",
2914 + interactionMode: "default",
2915 + cwd: workspace,
2916 + });
2917 + const args = {
2918 + description: "Explore",
2919 + prompt: "Read package.json.",
2920 + subagentType: { kind: "unspecified" as const },
2921 + agentId: "cursor-task-agent",
2922 + mode: "unspecified" as const,
2923 + };
2924 + const adapter = yield* makeCursorAdapterV2({
2925 + instanceId,
2926 + environment: { HOME: workspace },
2927 + }).pipe(
2928 + Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
2929 + assertComplete: Effect.void,
2930 + open: () =>
2931 + Effect.succeed({
2932 + agentId: "native-cursor-task",
2933 + listMessages: Effect.succeed([]),
2934 + close: Effect.void,
2935 + send: (input) =>
2936 + Effect.gen(function* () {
2937 + yield* input.onDelta!({
2938 + type: "tool-call-started",
2939 + modelCallId: "model-call",
2940 + callId: "task-call",
2941 + toolCall: { type: "task", args },
2942 + });
2943 + for (const taskUpdate of taskUpdates) {
2944 + yield* input.onDelta!({
2945 + type: "tool-call-delta",
2946 + modelCallId: "model-call",
2947 + callId: "task-call",
2948 + taskUpdate,
2949 + });
2950 + }
2951 + yield* input.onDelta!({
2952 + type: "tool-call-completed",
2953 + modelCallId: "model-call",
2954 + callId: "task-call",
2955 + toolCall: {
2956 + type: "task",
2957 + args,
2958 + result: {
2959 + status: "success",
2960 + value: {
2961 + conversationSteps: [...conversationSteps],
2962 + isBackground: false,
2963 + backgroundReason: "unspecified",
2964 + },
2965 + },
2966 + },
2967 + });
2968 + return {
2969 + agentId: "native-cursor-task",
2970 + runId: "native-cursor-run",
2971 + wait: Effect.succeed({
2972 + id: "native-cursor-run",
2973 + requestId: "native-request",
2974 + status: "finished" as const,
2975 + model: { id: "composer-2.5" },
2976 + durationMs: 1,
2977 + }),
2978 + cancel: Effect.void,
2979 + };
2980 + }).pipe(Effect.orDie),
2981 + }),
2982 + }),
2983 + Effect.provide(TestProviderHost.layer({ cwd: workspace })),
2984 + );
2985 + const runtime = yield* adapter.openSession({
2986 + threadId,
2987 + providerSessionId: ProviderSessionId.make("cursor-task-session"),
2988 + modelSelection,
2989 + runtimePolicy,
2990 + });
2991 + const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
2992 + const now = yield* DateTime.now;
2993 + yield* runtime.startTurn({
2994 + threadId,
2995 + providerThread,
2996 + modelSelection,
2997 + runtimePolicy,
2998 + runId: RunId.make("cursor-task-run"),
2999 + runOrdinal: 1,
3000 + providerTurnOrdinal: 1,
3001 + attemptId: RunAttemptId.make("cursor-task-attempt"),
3002 + rootNodeId: NodeId.make("cursor-task-root"),
3003 + appThread: {
3004 + id: threadId,
3005 + projectId: ProjectId.make("cursor-task-project"),
3006 + createdBy: "user",
3007 + creationSource: "web",
3008 + title: "Cursor subagent",
3009 + providerInstanceId: instanceId,
3010 + modelSelection,
3011 + runtimeMode: "full-access",
3012 + interactionMode: "default",
3013 + branch: null,
3014 + worktreePath: null,
3015 + activeProviderThreadId: providerThread.id,
3016 + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
3017 + forkedFrom: null,
3018 + createdAt: now,
3019 + updatedAt: now,
3020 + archivedAt: null,
3021 + settledOverride: null,
3022 + settledAt: null,
3023 + lastVisitedAt: null,
3024 + deletedAt: null,
3025 + },
3026 + message: {
3027 + messageId: MessageId.make("cursor-task-message"),
3028 + createdBy: "user",
3029 + creationSource: "web",
3030 + text: "Spawn a subagent.",
3031 + attachments: [],
3032 + },
3033 + });
3034 + const events = yield* runtime.events.pipe(
3035 + Stream.takeUntil((event) => event.type === "turn.terminal"),
3036 + Stream.runCollect,
3037 + );
3038 + assert.equal(events.find((event) => event.type === "turn.terminal")?.status, "completed");
3039 + const childThreadId = events.findLast((event) => event.type === "subagent.updated")?.subagent
3040 + .childThreadId;
3041 + assert.isDefined(childThreadId);
3042 + return { events, childThreadId };
3043 +});
3044 +
3045 const scopedProcessEnv = (values: Record<string, string>) =>
3046 Effect.acquireRelease(
3047 Effect.sync(() => {
packages/provider-cursor/src/server/adapter.ts
+215 -53
@@ -937,11 +937,18 @@ interface ActiveCursorSubagent {
937 readonly turnItemOrdinal: number;
938 nextChildOrdinal: number;
939 resultProjected: boolean;
940 + // Nested tools and text Cursor streamed live; the completion transcript fills in only the rest.
941 + readonly liveToolCallIds: Set<string>;
942 + streamedAssistantText: string;
943 + readonly tools: Map<string, ActiveCursorToolCall>;
944 + readonly assistant: ActiveCursorTextStream;
945 + readonly reasoning: ActiveCursorTextStream;
946 }
947
948 interface ActiveCursorTextSegment {
949 readonly nativeItemId: string;
950 readonly startedAt: DateTime.Utc;
951 + readonly ordinal: number;
952 text: string;
953 }
954
@@ -1100,16 +1107,24 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1107 providerTurnId: context.providerTurnId,
1108 });
1109
1110 + const childProjectionTarget = (subagent: ActiveCursorSubagent): CursorProjectionTarget => ({
1111 + threadId: subagent.childThreadId,
1112 + runId: null,
1113 + rootNodeId: subagent.childRootNodeId,
1114 + parentNodeId: subagent.childRootNodeId,
1115 + providerThreadId: null,
1116 + providerTurnId: null,
1117 + });
1118 +
1119 const emitAssistant = Effect.fnUntraced(function* (
1104 - context: ActiveCursorTurn,
1120 + target: CursorProjectionTarget,
1121 + segment: ActiveCursorTextSegment | null,
1122 completed: boolean,
1123 ) {
1107 - const segment = context.assistant.current;
1124 if (segment === null || segment.text.length === 0) {
1125 return;
1126 }
1127 const now = yield* DateTime.now;
1112 - const ordinal = yield* resolveItemOrdinal(context, segment.nativeItemId);
1128 const nodeId = idAllocator.derive.nodeFromProviderItem({
1129 driver: CursorAgentSdk.CURSOR_PROVIDER,
1130 nativeItemId: segment.nativeItemId,
@@ -1132,15 +1147,15 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1147 driver: CursorAgentSdk.CURSOR_PROVIDER,
1148 node: {
1149 id: nodeId,
1135 - threadId: context.input.threadId,
1136 - runId: context.input.runId,
1137 - parentNodeId: context.input.rootNodeId,
1138 - rootNodeId: context.input.rootNodeId,
1150 + threadId: target.threadId,
1151 + runId: target.runId,
1152 + parentNodeId: target.parentNodeId,
1153 + rootNodeId: target.rootNodeId,
1154 kind: "assistant_message",
1155 status: completed ? "completed" : "running",
1156 countsForRun: false,
1142 - providerThreadId: context.input.providerThread.id,
1143 - providerTurnId: context.providerTurnId,
1157 + providerThreadId: target.providerThreadId,
1158 + providerTurnId: target.providerTurnId,
1159 nativeItemRef,
1160 runtimeRequestId: null,
1161 checkpointScopeId: null,
@@ -1155,8 +1170,8 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1170 createdBy: "agent",
1171 creationSource: "provider",
1172 id: messageId,
1158 - threadId: context.input.threadId,
1159 - runId: context.input.runId,
1173 + threadId: target.threadId,
1174 + runId: target.runId,
1175 nodeId,
1176 role: "assistant",
1177 text: segment.text,
@@ -1171,14 +1186,14 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1186 driver: CursorAgentSdk.CURSOR_PROVIDER,
1187 turnItem: {
1188 id: turnItemId,
1174 - threadId: context.input.threadId,
1175 - runId: context.input.runId,
1189 + threadId: target.threadId,
1190 + runId: target.runId,
1191 nodeId,
1177 - providerThreadId: context.input.providerThread.id,
1178 - providerTurnId: context.providerTurnId,
1192 + providerThreadId: target.providerThreadId,
1193 + providerTurnId: target.providerTurnId,
1194 nativeItemRef,
1195 parentItemId: null,
1181 - ordinal,
1196 + ordinal: segment.ordinal,
1197 status: completed ? "completed" : "running",
1198 title: null,
1199 startedAt: segment.startedAt,
@@ -1193,15 +1208,14 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1208 });
1209
1210 const emitReasoning = Effect.fnUntraced(function* (
1196 - context: ActiveCursorTurn,
1211 + target: CursorProjectionTarget,
1212 + segment: ActiveCursorTextSegment | null,
1213 completed: boolean,
1214 ) {
1199 - const segment = context.reasoning.current;
1215 if (segment === null || segment.text.length === 0) {
1216 return;
1217 }
1218 const now = yield* DateTime.now;
1204 - const ordinal = yield* resolveItemOrdinal(context, segment.nativeItemId);
1219 const nodeId = idAllocator.derive.nodeFromProviderItem({
1220 driver: CursorAgentSdk.CURSOR_PROVIDER,
1221 nativeItemId: segment.nativeItemId,
@@ -1220,15 +1234,15 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1234 driver: CursorAgentSdk.CURSOR_PROVIDER,
1235 node: {
1236 id: nodeId,
1223 - threadId: context.input.threadId,
1224 - runId: context.input.runId,
1225 - parentNodeId: context.input.rootNodeId,
1226 - rootNodeId: context.input.rootNodeId,
1237 + threadId: target.threadId,
1238 + runId: target.runId,
1239 + parentNodeId: target.parentNodeId,
1240 + rootNodeId: target.rootNodeId,
1241 kind: "reasoning",
1242 status: completed ? "completed" : "running",
1243 countsForRun: false,
1230 - providerThreadId: context.input.providerThread.id,
1231 - providerTurnId: context.providerTurnId,
1244 + providerThreadId: target.providerThreadId,
1245 + providerTurnId: target.providerTurnId,
1246 nativeItemRef,
1247 runtimeRequestId: null,
1248 checkpointScopeId: null,
@@ -1241,14 +1255,14 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1255 driver: CursorAgentSdk.CURSOR_PROVIDER,
1256 turnItem: {
1257 id: turnItemId,
1244 - threadId: context.input.threadId,
1245 - runId: context.input.runId,
1258 + threadId: target.threadId,
1259 + runId: target.runId,
1260 nodeId,
1247 - providerThreadId: context.input.providerThread.id,
1248 - providerTurnId: context.providerTurnId,
1261 + providerThreadId: target.providerThreadId,
1262 + providerTurnId: target.providerTurnId,
1263 nativeItemRef,
1264 parentItemId: null,
1251 - ordinal,
1265 + ordinal: segment.ordinal,
1266 status: completed ? "completed" : "running",
1267 title: null,
1268 startedAt: segment.startedAt,
@@ -1269,9 +1283,11 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1283 }) {
1284 if (input.stream.current === null) {
1285 input.stream.nextSegment += 1;
1286 + const nativeItemId = `${input.kind}:${input.context.run.runId}:${input.stream.nextSegment}`;
1287 input.stream.current = {
1273 - nativeItemId: `${input.kind}:${input.context.run.runId}:${input.stream.nextSegment}`,
1288 + nativeItemId,
1289 startedAt: yield* DateTime.now,
1290 + ordinal: yield* resolveItemOrdinal(input.context, nativeItemId),
1291 text: "",
1292 };
1293 }
@@ -1282,7 +1298,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1298 if (context.assistant.current === null) {
1299 return;
1300 }
1285 - yield* emitAssistant(context, true);
1301 + yield* emitAssistant(parentTarget(context), context.assistant.current, true);
1302 context.assistant.current = null;
1303 });
1304
@@ -1290,7 +1306,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1306 if (context.reasoning.current === null) {
1307 return;
1308 }
1293 - yield* emitReasoning(context, true);
1309 + yield* emitReasoning(parentTarget(context), context.reasoning.current, true);
1310 context.reasoning.current = null;
1311 });
1312
@@ -1704,12 +1720,11 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1720 ? "failed"
1721 : "completed"
1722 : "running");
1707 - const resultText = [
1723 + const resultParts = [
1724 ...assistantTextsFromConversationSteps(result?.conversationSteps ?? []),
1725 ...(result?.resultSuffix === undefined ? [] : [result.resultSuffix]),
1710 - ]
1711 - .filter((part) => part.trim().length > 0)
1712 - .join("\n");
1726 + ].filter((part) => part.trim().length > 0);
1727 + const resultText = resultParts.join("\n");
1728 const nativeItemId = input.callId;
1729 const nodeId =
1730 existing?.task.id ??
@@ -1778,6 +1793,11 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1793 existing?.turnItemOrdinal ?? (yield* resolveItemOrdinal(input.context, nativeItemId)),
1794 nextChildOrdinal: existing?.nextChildOrdinal ?? 100,
1795 resultProjected: existing?.resultProjected ?? false,
1796 + liveToolCallIds: existing?.liveToolCallIds ?? new Set(),
1797 + streamedAssistantText: existing?.streamedAssistantText ?? "",
1798 + tools: existing?.tools ?? new Map(),
1799 + assistant: existing?.assistant ?? { current: null, nextSegment: 0 },
1800 + reasoning: existing?.reasoning ?? { current: null, nextSegment: 0 },
1801 };
1802 input.context.subagents.set(input.callId, subagent);
1803
@@ -1917,29 +1937,48 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1937 });
1938
1939 if (input.completed && !subagent.resultProjected) {
1920 - for (const [index, nestedTool] of toolCallsFromConversationSteps(
1921 - result?.conversationSteps ?? [],
1922 - ).entries()) {
1940 + yield* completeSubagentReasoning(subagent);
1941 + yield* completeSubagentAssistant(subagent);
1942 + // A tool with neither a live completion nor a saved step has an unknown outcome.
1943 + const unfinishedStatus =
1944 + status === "interrupted" || status === "failed" || status === "cancelled"
1945 + ? status
1946 + : "interrupted";
1947 + const savedTools = toolCallsFromConversationSteps(result?.conversationSteps ?? []);
1948 + for (const tool of subagent.tools.values()) {
1949 + const saved = savedTools.find(
1950 + (nestedTool) => `${nativeItemId}:child-tool:${nestedTool.callId}` === tool.callId,
1951 + );
1952 + if (saved !== undefined) {
1953 + tool.toolCall = saved.toolCall;
1954 + }
1955 + yield* emitToolArtifacts({
1956 + active: tool,
1957 + completed: true,
1958 + ...(saved === undefined ? { unfinishedStatus } : {}),
1959 + });
1960 + }
1961 + subagent.tools.clear();
1962 + for (const [index, nestedTool] of savedTools.entries()) {
1963 + if (subagent.liveToolCallIds.has(nestedTool.callId)) {
1964 + continue;
1965 + }
1966 const callId = `${nativeItemId}:child-tool:${nestedTool.callId || index + 1}`;
1967 const startedAt = task.startedAt ?? now;
1968 const active: ActiveCursorToolCall = {
1969 callId,
1970 toolCall: nestedTool.toolCall,
1928 - target: {
1929 - threadId: childThreadId,
1930 - runId: null,
1931 - rootNodeId: childRootNodeId,
1932 - parentNodeId: childRootNodeId,
1933 - providerThreadId: null,
1934 - providerTurnId: null,
1935 - },
1971 + target: childProjectionTarget(subagent),
1972 ordinal: ++subagent.nextChildOrdinal,
1973 startedAt,
1974 streamedOutput: "",
1975 };
1976 yield* emitToolArtifacts({ active, completed: true });
1977 }
1942 - if (task.result !== null && task.result.length > 0) {
1978 + const unstreamedResult = resultParts
1979 + .filter((part) => !subagent.streamedAssistantText.includes(part.trim()))
1980 + .join("\n");
1981 + if (unstreamedResult.length > 0) {
1982 const resultNativeId = `${nativeItemId}:result`;
1983 const resultArtifacts = makeSubagentConversationArtifacts({
1984 messageId: idAllocator.derive.messageFromProviderItem({
@@ -1960,7 +1999,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
1999 strength: "weak",
2000 },
2001 role: "assistant",
1963 - text: task.result,
2002 + text: unstreamedResult,
2003 ordinal: ++subagent.nextChildOrdinal,
2004 now,
2005 });
@@ -1979,6 +2018,125 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2018 }
2019 });
2020
2021 + const appendSubagentText = Effect.fnUntraced(function* (input: {
2022 + readonly subagent: ActiveCursorSubagent;
2023 + readonly stream: ActiveCursorTextStream;
2024 + readonly kind: "assistant" | "reasoning";
2025 + readonly text: string;
2026 + }) {
2027 + if (input.stream.current === null) {
2028 + input.stream.nextSegment += 1;
2029 + input.stream.current = {
2030 + nativeItemId: `${input.subagent.callId}:${input.kind}:${input.stream.nextSegment}`,
2031 + startedAt: yield* DateTime.now,
2032 + ordinal: ++input.subagent.nextChildOrdinal,
2033 + text: "",
2034 + };
2035 + }
2036 + input.stream.current.text += input.text;
2037 + });
2038 +
2039 + const completeSubagentAssistant = Effect.fnUntraced(function* (
2040 + subagent: ActiveCursorSubagent,
2041 + ) {
2042 + if (subagent.assistant.current === null) {
2043 + return;
2044 + }
2045 + yield* emitAssistant(childProjectionTarget(subagent), subagent.assistant.current, true);
2046 + subagent.assistant.current = null;
2047 + });
2048 +
2049 + const completeSubagentReasoning = Effect.fnUntraced(function* (
2050 + subagent: ActiveCursorSubagent,
2051 + ) {
2052 + if (subagent.reasoning.current === null) {
2053 + return;
2054 + }
2055 + yield* emitReasoning(childProjectionTarget(subagent), subagent.reasoning.current, true);
2056 + subagent.reasoning.current = null;
2057 + });
2058 +
2059 + const handleTaskUpdate = Effect.fnUntraced(function* (
2060 + context: ActiveCursorTurn,
2061 + update: Extract<InteractionUpdate, { readonly type: "tool-call-delta" }>,
2062 + ) {
2063 + const subagent = context.subagents.get(update.callId);
2064 + if (subagent === undefined || !isOrchestrationV2WorkActive(subagent.task.status)) {
2065 + return;
2066 + }
2067 + const taskUpdate = update.taskUpdate;
2068 + switch (taskUpdate.type) {
2069 + case "text-delta":
2070 + subagent.streamedAssistantText += taskUpdate.text;
2071 + yield* completeSubagentReasoning(subagent);
2072 + yield* appendSubagentText({
2073 + subagent,
2074 + stream: subagent.assistant,
2075 + kind: "assistant",
2076 + text: taskUpdate.text,
2077 + });
2078 + yield* emitAssistant(
2079 + childProjectionTarget(subagent),
2080 + subagent.assistant.current,
2081 + false,
2082 + );
2083 + return;
2084 + case "thinking-delta":
2085 + yield* completeSubagentAssistant(subagent);
2086 + yield* appendSubagentText({
2087 + subagent,
2088 + stream: subagent.reasoning,
2089 + kind: "reasoning",
2090 + text: taskUpdate.text,
2091 + });
2092 + yield* emitReasoning(
2093 + childProjectionTarget(subagent),
2094 + subagent.reasoning.current,
2095 + false,
2096 + );
2097 + return;
2098 + case "thinking-completed":
2099 + yield* completeSubagentReasoning(subagent);
2100 + return;
2101 + case "step-completed":
2102 + yield* completeSubagentAssistant(subagent);
2103 + yield* completeSubagentReasoning(subagent);
2104 + return;
2105 + case "tool-call-started":
2106 + case "partial-tool-call":
2107 + case "tool-call-completed": {
2108 + subagent.liveToolCallIds.add(taskUpdate.callId);
2109 + yield* completeSubagentAssistant(subagent);
2110 + yield* completeSubagentReasoning(subagent);
2111 + const nestedCallId = `${subagent.callId}:child-tool:${taskUpdate.callId}`;
2112 + const completed = taskUpdate.type === "tool-call-completed";
2113 + let active = subagent.tools.get(nestedCallId);
2114 + if (active === undefined) {
2115 + active = {
2116 + callId: nestedCallId,
2117 + toolCall: taskUpdate.toolCall,
2118 + target: childProjectionTarget(subagent),
2119 + ordinal: ++subagent.nextChildOrdinal,
2120 + startedAt: yield* DateTime.now,
2121 + streamedOutput: "",
2122 + };
2123 + subagent.tools.set(nestedCallId, active);
2124 + yield* emitToolArtifacts({ active, completed: false });
2125 + }
2126 + active.toolCall = taskUpdate.toolCall;
2127 + if (taskUpdate.type !== "tool-call-started") {
2128 + yield* emitToolArtifacts({ active, completed });
2129 + }
2130 + if (completed) {
2131 + subagent.tools.delete(nestedCallId);
2132 + }
2133 + return;
2134 + }
2135 + default:
2136 + return;
2137 + }
2138 + });
2139 +
2140 const handleToolUpdate = Effect.fnUntraced(function* (
2141 context: ActiveCursorTurn,
2142 update: Extract<
@@ -2058,7 +2216,8 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2216 update.type === "text-delta" ||
2217 update.type === "thinking-delta" ||
2218 update.type === "partial-tool-call" ||
2061 - update.type === "tool-call-started"
2219 + update.type === "tool-call-started" ||
2220 + update.type === "tool-call-delta"
2221 ) {
2222 context.turnEnded = false;
2223 }
@@ -2077,7 +2236,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2236 kind: "assistant",
2237 text: update.text,
2238 });
2080 - yield* emitAssistant(context, false);
2239 + yield* emitAssistant(parentTarget(context), context.assistant.current, false);
2240 return;
2241 case "thinking-delta":
2242 yield* completeAssistant(context);
@@ -2087,7 +2246,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2246 kind: "reasoning",
2247 text: update.text,
2248 });
2090 - yield* emitReasoning(context, false);
2249 + yield* emitReasoning(parentTarget(context), context.reasoning.current, false);
2250 return;
2251 case "thinking-completed":
2252 yield* completeReasoning(context);
@@ -2099,6 +2258,9 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2258 yield* completeReasoning(context);
2259 yield* handleToolUpdate(context, update);
2260 return;
2261 + case "tool-call-delta":
2262 + yield* handleTaskUpdate(context, update);
2263 + return;
2264 case "step-completed":
2265 case "turn-ended":
2266 yield* completeAssistant(context);