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

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

fix(acp): project and resume native child sessions (#21617)

Co-authored-by: Wout Stiens <71498452+StiensWout@users.noreply.github.com>

Julius Marminge committed Oct 19, 2026 at 21:13 UTC 6958838214aef13770989d057d6df751b1e95cc4
2 files changed +511 -76
packages/provider-acp/src/server/adapter.ts
+508 -76
@@ -289,9 +289,13 @@ export interface AcpAdapterV2Flavor {
289 readonly registerExtensions?: (
290 context: AcpAdapterV2ExtensionContext,
291 ) => Effect.Effect<void, EffectAcpErrors.AcpError>;
292 + readonly extractSubagentSessionEvent?: (
293 + notification: EffectAcpSchema.SessionNotification,
294 + ) => AcpSubagentSessionEvent | undefined;
295 readonly extractSubagentUpdate?: (
296 toolCall: AcpToolCallState,
297 ) => AcpAdapterV2SubagentUpdate | undefined;
298 + readonly isProvisionalSubagentTool?: (toolCall: AcpToolCallState) => boolean;
299 readonly normalizeToolCall?: (toolCall: AcpToolCallState) => AcpToolCallState;
300 readonly extractProposedPlanMarkdown?: (toolCall: AcpToolCallState) => string | undefined;
301 readonly extractBackgroundTaskId?: (toolCall: AcpToolCallState) => string | undefined;
@@ -358,6 +362,22 @@ export function acpProviderItemNativeId(input: {
362 : input.nativeId;
363 }
364
365 +export type AcpSubagentSessionEvent =
366 + | {
367 + readonly type: "state";
368 + readonly childSessionId: string;
369 + readonly parentSessionId: string;
370 + readonly status: "running" | "completed" | "failed" | "cancelled";
371 + readonly result: string | null;
372 + }
373 + | {
374 + readonly type: "delegation";
375 + readonly childSessionId: string;
376 + readonly parentSessionId: string | null;
377 + readonly delegationId: string;
378 + readonly prompt: string;
379 + };
380 +
381 export interface AcpAdapterV2SubagentUpdate {
382 readonly nativeTaskId: string;
383 readonly prompt: string;
@@ -1203,8 +1223,8 @@ interface ActiveAcpSubagent {
1223 readonly childThreadId: ThreadId;
1224 readonly childRootNodeId: OrchestrationV2ExecutionNode["id"];
1225 readonly turnItemId: OrchestrationV2TurnItem["id"];
1206 - readonly turnItemOrdinal: number;
1207 - readonly providerTurnId: OrchestrationV2ProviderTurn["id"];
1226 + turnItemOrdinal: number;
1227 + providerTurnId: OrchestrationV2ProviderTurn["id"];
1228 readonly parentProviderThreadId: ProviderThreadId;
1229 childSessionId: string | null;
1230 assistantText: string;
@@ -1212,6 +1232,19 @@ interface ActiveAcpSubagent {
1232 readonly childItemOrdinals: Map<string, number>;
1233 nextChildOrdinal: number;
1234 terminalStatusProjected: boolean;
1235 + readonly toolState: Pick<ActiveAcpTurn, "tools" | "toolUpdatesSkipped" | "toolStartedAt">;
1236 + readonly toolSessionIds: Map<string, string>;
1237 +}
1238 +
1239 +interface AcpNativeChildSession {
1240 + parentSessionId: string | null;
1241 + status: "running" | "completed" | "failed" | "cancelled";
1242 + result: string | null;
1243 + delegationId: string | null;
1244 + prompt: string;
1245 + subagent: ActiveAcpSubagent | undefined;
1246 + owner: ActiveAcpTurn | undefined;
1247 + readonly pending: Array<EffectAcpSchema.SessionNotification>;
1248 }
1249
1250 function acpSubagentStatusIsTerminal(status: OrchestrationV2Subagent["status"]): boolean {
@@ -1276,7 +1309,7 @@ const TOOL_UPDATE_PERSIST_EVERY = 10;
1309 * resend the whole call each time.
1310 */
1311 function shouldPersistToolUpdate(
1279 - context: ActiveAcpTurn,
1312 + context: Pick<ActiveAcpTurn, "toolUpdatesSkipped">,
1313 key: string,
1314 previous: AcpToolCallState | undefined,
1315 next: AcpToolCallState,
@@ -1503,6 +1536,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
1536 { sessionUpdate: "state_update" }
1537 > | null = null;
1538 const agentRequests = new Set<object>();
1539 + const nativeChildren = new Map<string, AcpNativeChildSession>();
1540 + const extractSubagentUpdate = (tool: AcpToolCallState) =>
1541 + flavor.extractSubagentSessionEvent !== undefined && !followsAgentWorkState
1542 + ? undefined
1543 + : flavor.extractSubagentUpdate?.(tool);
1544 let turnAttached = yield* Deferred.make<boolean>();
1545 const activeSessionId = yield* Ref.make<string | null>(null);
1546 const contextUsageBySessionId = yield* Ref.make(
@@ -1530,6 +1568,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
1568 setup.initializeResult.protocolVersion === 2;
1569 agentWork = null;
1570 agentRequests.clear();
1571 + nativeChildren.clear();
1572 yield* Ref.set(activeSessionSetup, setup);
1573 });
1574 const activeSelection = yield* Ref.make<ModelSelection | null>(null);
@@ -1905,6 +1944,24 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
1944 yield* Ref.set(runtimeRestartRequired, true);
1945 if (!followsAgentWorkState) return;
1946 agentWork = { sessionUpdate: "state_update", state: "idle", stopReason: "error" };
1947 + for (const child of nativeChildren.values()) {
1948 + if (child.status !== "running") continue;
1949 + child.status = "failed";
1950 + child.result = null;
1951 + }
1952 + for (const child of nativeChildren.values()) {
1953 + if (
1954 + child.subagent === undefined ||
1955 + acpSubagentStatusIsTerminal(child.subagent.task.status)
1956 + )
1957 + continue;
1958 + child.result = null;
1959 + yield* publishNativeChildStatus(child, "failed");
1960 + }
1961 + agentRequests.clear();
1962 + yield* Ref.set(wakeBuffer, []);
1963 + yield* Ref.set(continuationRequested, false);
1964 + yield* cancelPendingRuntimeRequests();
1965 const context = yield* Ref.get(activeTurn);
1966 if (context !== null)
1967 yield* finalizeTurn(
@@ -2543,6 +2600,101 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2600 }
2601 });
2602
2603 + const projectSubagentTool = Effect.fnUntraced(function* (
2604 + subagent: ActiveAcpSubagent,
2605 + sessionId: string,
2606 + toolCall: AcpToolCallState,
2607 + projectedStatus?: ProjectedToolStatus,
2608 + ) {
2609 + const nativeTaskId = subagent.task.nativeTaskRef?.nativeId ?? String(subagent.task.id);
2610 + const key =
2611 + sessionId === subagent.childSessionId
2612 + ? `${nativeTaskId}:tool:${toolCall.toolCallId}`
2613 + : `${nativeTaskId}:session:${sessionId}:tool:${toolCall.toolCallId}`;
2614 + const context = subagent.toolState;
2615 + const rootSessionId = yield* Ref.get(activeSessionId);
2616 + subagent.toolSessionIds.set(key, sessionId);
2617 + const previous = context.tools.get(key);
2618 + const merged = mergeToolCallState(previous, toolCall);
2619 + context.tools.set(key, merged);
2620 + if (
2621 + projectedStatus === undefined &&
2622 + !shouldPersistToolUpdate(context, key, previous, merged, merged.status)
2623 + )
2624 + return;
2625 + const mcpIdentity = extractMcpToolCallIdentity(merged, {
2626 + embeddedTerminalCommands: [
2627 + ...embeddedTerminalCommands(sessionId, toolCall.toolCallId),
2628 + ...(rootSessionId === null
2629 + ? []
2630 + : embeddedTerminalCommands(rootSessionId, toolCall.toolCallId)),
2631 + ],
2632 + });
2633 + const now = yield* DateTime.now;
2634 + const status = projectedStatus ?? toolStatus(merged.status);
2635 + const startedAt = context.toolStartedAt.get(key) ?? now;
2636 + context.toolStartedAt.set(key, startedAt);
2637 + const ordinal = resolveSubagentChildOrdinal(subagent, key);
2638 + yield* emitProviderEvent({
2639 + type: "turn_item.updated",
2640 + driver,
2641 + turnItem: {
2642 + id: providerTurnItemId(key),
2643 + threadId: subagent.childThreadId,
2644 + runId: null,
2645 + nodeId: subagent.childRootNodeId,
2646 + providerThreadId: subagent.task.providerThreadId,
2647 + providerTurnId: null,
2648 + nativeItemRef: { driver, nativeId: key, strength: "strong" },
2649 + parentItemId: null,
2650 + ordinal,
2651 + status,
2652 + title: merged.title ?? merged.kind ?? "Tool",
2653 + startedAt,
2654 + completedAt: completedAtForStatus(status, now),
2655 + updatedAt: now,
2656 + type: "dynamic_tool",
2657 + ...(mcpIdentity === undefined
2658 + ? {}
2659 + : mcpToolPresentation({
2660 + serverName: mcpIdentity.server,
2661 + toolName: mcpIdentity.tool,
2662 + source: unknownRecord(
2663 + (
2664 + unknownRecord(unknownRecord(merged.data.rawOutput)?.result) ??
2665 + unknownRecord(merged.data.rawOutput)
2666 + )?._meta,
2667 + )?.source,
2668 + })),
2669 + toolName:
2670 + mcpIdentity === undefined
2671 + ? (merged.title ?? merged.kind ?? "Tool")
2672 + : `${mcpIdentity.server}.${mcpIdentity.tool}`,
2673 + input: merged.data.rawInput ?? null,
2674 + output: merged.data.rawOutput ?? merged.data.content ?? null,
2675 + },
2676 + });
2677 + });
2678 +
2679 + const terminalizeSubagentTools = Effect.fnUntraced(function* (subagent: ActiveAcpSubagent) {
2680 + for (const [key, tool] of subagent.toolState.tools) {
2681 + if (
2682 + toolStatus(tool.status) !== "running" &&
2683 + toolStatus(tool.status) !== "pending" &&
2684 + toolStatus(tool.status) !== "waiting"
2685 + )
2686 + continue;
2687 + const sessionId = subagent.toolSessionIds.get(key);
2688 + if (sessionId !== undefined)
2689 + yield* projectSubagentTool(
2690 + subagent,
2691 + sessionId,
2692 + { ...tool, status: "failed" },
2693 + "interrupted",
2694 + );
2695 + }
2696 + });
2697 +
2698 const emitSubagent = Effect.fnUntraced(function* (
2699 context: ActiveAcpTurn,
2700 update: AcpAdapterV2SubagentUpdate,
@@ -2639,6 +2791,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2791 childItemOrdinals: new Map(),
2792 nextChildOrdinal: 101,
2793 terminalStatusProjected: false,
2794 + toolState: {
2795 + tools: new Map(),
2796 + toolUpdatesSkipped: new Map(),
2797 + toolStartedAt: new Map(),
2798 + },
2799 + toolSessionIds: new Map(),
2800 };
2801 subagent.task = task;
2802 context.subagents.set(nativeTaskId, subagent);
@@ -2823,6 +2981,301 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
2981 }
2982 });
2983
2984 + const nativeChild = (sessionId: string) => {
2985 + let child = nativeChildren.get(sessionId);
2986 + if (child === undefined) {
2987 + child = {
2988 + parentSessionId: null,
2989 + status: "running",
2990 + result: null,
2991 + delegationId: null,
2992 + prompt: "",
2993 + subagent: undefined,
2994 + owner: undefined,
2995 + pending: [],
2996 + };
2997 + nativeChildren.set(sessionId, child);
2998 + }
2999 + return child;
3000 + };
3001 +
3002 + const setNativeChildState = (
3003 + child: AcpNativeChildSession,
3004 + status: AcpNativeChildSession["status"],
3005 + result: string | null,
3006 + ) => {
3007 + if (child.status !== "running" && status === "running" && child.subagent !== undefined) {
3008 + // A new execution replaces its result; live ownership transfers keep their progress.
3009 + child.subagent.task = { ...child.subagent.task, result: null };
3010 + child.subagent.assistantText = "";
3011 + child.subagent.assistantMessages.clear();
3012 + }
3013 + child.status = status;
3014 + child.result = result;
3015 + };
3016 +
3017 + const nativeChildOwner = (sessionId: string): AcpNativeChildSession | undefined => {
3018 + const visited = new Set<string>();
3019 + let child = nativeChildren.get(sessionId);
3020 + while (child !== undefined && !visited.has(sessionId)) {
3021 + if (child.subagent !== undefined) return child;
3022 + visited.add(sessionId);
3023 + if (child.parentSessionId === null) return;
3024 + sessionId = child.parentSessionId;
3025 + child = nativeChildren.get(sessionId);
3026 + }
3027 + };
3028 +
3029 + const hasWorkingDescendant = (sessionId: string) => {
3030 + for (const [id, child] of nativeChildren) {
3031 + if (id === sessionId || child.status !== "running") continue;
3032 + const visited = new Set<string>();
3033 + let parent = child.parentSessionId;
3034 + while (parent !== null && !visited.has(parent)) {
3035 + if (parent === sessionId) return true;
3036 + visited.add(parent);
3037 + parent = nativeChildren.get(parent)?.parentSessionId ?? null;
3038 + }
3039 + }
3040 + return false;
3041 + };
3042 +
3043 + const publishNativeChildStatus = Effect.fnUntraced(function* (
3044 + child: AcpNativeChildSession,
3045 + status: AcpAdapterV2SubagentUpdate["status"],
3046 + ) {
3047 + const { subagent, owner } = child;
3048 + if (subagent === undefined || owner === undefined) return;
3049 + if (acpSubagentStatusIsTerminal(status)) yield* terminalizeSubagentTools(subagent);
3050 + yield* emitSubagent(owner, {
3051 + nativeTaskId: subagent.task.nativeTaskRef?.nativeId ?? subagent.task.id,
3052 + childSessionId: subagent.childSessionId,
3053 + prompt: subagent.task.prompt,
3054 + title: subagent.task.title,
3055 + model: subagent.task.model,
3056 + status,
3057 + result: child.result,
3058 + });
3059 + });
3060 +
3061 + const syncNativeChildren = Effect.fnUntraced(function* (
3062 + context: ActiveAcpTurn,
3063 + ): Effect.fn.Return<void> {
3064 + const rootSessionId = yield* Ref.get(activeSessionId);
3065 + for (const [sessionId, child] of nativeChildren) {
3066 + if (child.parentSessionId !== rootSessionId) continue;
3067 + if (child.subagent === undefined && child.delegationId !== null) {
3068 + const subagent = context.subagents.get(child.delegationId);
3069 + if (subagent !== undefined) {
3070 + child.subagent = subagent;
3071 + child.owner = context;
3072 + yield* emitSubagent(context, {
3073 + nativeTaskId: child.delegationId,
3074 + childSessionId: sessionId,
3075 + prompt: subagent.task.prompt,
3076 + title: subagent.task.title,
3077 + model: subagent.task.model,
3078 + status: "running",
3079 + result: child.result,
3080 + });
3081 + }
3082 + }
3083 + const subagent = child.subagent;
3084 + if (subagent === undefined) continue;
3085 + const newDelegation =
3086 + child.delegationId !== null &&
3087 + child.delegationId !== subagent.task.nativeTaskRef?.nativeId &&
3088 + !subagent.childItemOrdinals.has(`${sessionId}:prompt:${child.delegationId}`);
3089 + if (newDelegation && child.status !== "running")
3090 + setNativeChildState(child, "running", null);
3091 + const hasNativeWork = child.status === "running" || hasWorkingDescendant(sessionId);
3092 + if (
3093 + (hasNativeWork && acpSubagentStatusIsTerminal(subagent.task.status)) ||
3094 + (child.owner !== context &&
3095 + (child.owner?.finalizedStatus !== "completed" || newDelegation) &&
3096 + (hasNativeWork || child.pending.length > 0))
3097 + ) {
3098 + // The ownership event precedes child tools so the old run can detach first.
3099 + child.owner = context;
3100 + subagent.task = {
3101 + ...subagent.task,
3102 + runId: context.input.runId,
3103 + parentNodeId: context.input.rootNodeId,
3104 + status: "running",
3105 + completedAt: null,
3106 + };
3107 + subagent.providerTurnId = context.providerTurnId;
3108 + subagent.turnItemOrdinal = yield* resolveItemOrdinal(
3109 + context,
3110 + subagent.task.nativeTaskRef?.nativeId ?? subagent.task.id,
3111 + );
3112 + subagent.terminalStatusProjected = false;
3113 + context.subagents.set(
3114 + subagent.task.nativeTaskRef?.nativeId ?? subagent.task.id,
3115 + subagent,
3116 + );
3117 + context.subagentsBySessionId.set(sessionId, subagent);
3118 + yield* publishNativeChildStatus(child, "running");
3119 + for (const [key, tool] of subagent.toolState.tools) {
3120 + const toolSessionId = subagent.toolSessionIds.get(key);
3121 + if (
3122 + toolSessionId === undefined ||
3123 + (toolStatus(tool.status) !== "running" && toolStatus(tool.status) !== "pending")
3124 + )
3125 + continue;
3126 + subagent.toolState.toolUpdatesSkipped.delete(key);
3127 + yield* projectSubagentTool(subagent, toolSessionId, tool, toolStatus(tool.status));
3128 + }
3129 + }
3130 + if (
3131 + child.delegationId !== null &&
3132 + child.delegationId !== subagent.task.nativeTaskRef?.nativeId
3133 + ) {
3134 + const nativeItemId = `${sessionId}:prompt:${child.delegationId}`;
3135 + if (!subagent.childItemOrdinals.has(nativeItemId)) {
3136 + const now = yield* DateTime.now;
3137 + const artifacts = makeSubagentConversationArtifacts({
3138 + senderThreadId: subagent.task.threadId,
3139 + messageId: providerMessageId(nativeItemId),
3140 + turnItemId: providerTurnItemId(nativeItemId),
3141 + threadId: subagent.childThreadId,
3142 + rootNodeId: subagent.childRootNodeId,
3143 + providerThreadId: subagent.task.providerThreadId,
3144 + providerTurnId: null,
3145 + nativeItemRef: { driver, nativeId: nativeItemId, strength: "weak" },
3146 + role: "user",
3147 + text: child.prompt,
3148 + ordinal: resolveSubagentChildOrdinal(subagent, nativeItemId),
3149 + now,
3150 + });
3151 + yield* emitProviderEvent({
3152 + type: "message.updated",
3153 + driver,
3154 + message: artifacts.message,
3155 + });
3156 + yield* emitProviderEvent({
3157 + type: "turn_item.updated",
3158 + driver,
3159 + turnItem: artifacts.turnItem,
3160 + });
3161 + }
3162 + }
3163 + }
3164 + for (const [sessionId, child] of nativeChildren) {
3165 + const owner = nativeChildOwner(sessionId);
3166 + if (owner?.owner !== context && owner?.owner?.finalizedStatus !== "completed") continue;
3167 + for (const notification of child.pending.splice(0))
3168 + yield* projectNativeChildFrame(notification);
3169 + if (
3170 + child.subagent?.childSessionId != null &&
3171 + child.status !== "running" &&
3172 + !hasWorkingDescendant(child.subagent.childSessionId)
3173 + ) {
3174 + yield* publishNativeChildStatus(child, child.status);
3175 + }
3176 + }
3177 + });
3178 +
3179 + const projectNativeChildFrame = Effect.fnUntraced(function* (
3180 + notification: EffectAcpSchema.SessionNotification,
3181 + ): Effect.fn.Return<void> {
3182 + const child = nativeChild(notification.sessionId);
3183 + const owner = nativeChildOwner(notification.sessionId);
3184 + const subagent = owner?.subagent;
3185 + if (
3186 + subagent === undefined ||
3187 + owner?.owner === undefined ||
3188 + (owner.owner.finalized && owner.owner.finalizedStatus !== "completed") ||
3189 + (acpSubagentStatusIsTerminal(subagent.task.status) && child.status === "running")
3190 + ) {
3191 + if (child.pending.length < 256) child.pending.push(notification);
3192 + if (subagent !== undefined) {
3193 + const root = yield* Ref.get(activeSessionId);
3194 + if (root !== null) yield* offerContinuationRun(root);
3195 + }
3196 + return;
3197 + }
3198 + const update = notification.update;
3199 + if (update.sessionUpdate === "tool_call" || update.sessionUpdate === "tool_call_update") {
3200 + for (const event of parseSessionUpdateEvent(notification).events) {
3201 + if (event._tag === "ToolCallUpdated") {
3202 + yield* projectSubagentTool(
3203 + subagent,
3204 + notification.sessionId,
3205 + flavor.normalizeToolCall?.(event.toolCall) ?? event.toolCall,
3206 + );
3207 + }
3208 + }
3209 + } else {
3210 + yield* projectSubagentNotification(subagent, notification);
3211 + }
3212 + });
3213 +
3214 + const followNativeChild = Effect.fnUntraced(function* (
3215 + notification: EffectAcpSchema.SessionNotification,
3216 + context: ActiveAcpTurn | null,
3217 + ): Effect.fn.Return<boolean> {
3218 + if (!followsAgentWorkState || flavor.extractSubagentSessionEvent === undefined)
3219 + return false;
3220 + const root = yield* Ref.get(activeSessionId);
3221 + const event = flavor.extractSubagentSessionEvent(notification);
3222 + if (event !== undefined) {
3223 + if (yield* Ref.get(stoppedRunQuarantine)) return true;
3224 + const child = nativeChild(event.childSessionId);
3225 + child.parentSessionId = event.parentSessionId ?? child.parentSessionId;
3226 + if (event.type === "state") {
3227 + setNativeChildState(child, event.status, event.result);
3228 + } else {
3229 + child.delegationId = event.delegationId;
3230 + child.prompt = event.prompt;
3231 + }
3232 + if (context !== null) yield* syncNativeChildren(context);
3233 + const owner = nativeChildOwner(event.childSessionId);
3234 + if (owner?.subagent !== undefined && owner.owner !== undefined) {
3235 + const sessionId = owner.subagent.childSessionId;
3236 + if (
3237 + owner.status !== "running" &&
3238 + sessionId !== null &&
3239 + !hasWorkingDescendant(sessionId)
3240 + ) {
3241 + yield* publishNativeChildStatus(owner, owner.status);
3242 + } else if (acpSubagentStatusIsTerminal(owner.subagent.task.status)) {
3243 + if (context !== null) yield* syncNativeChildren(context);
3244 + else if (root !== null) yield* offerContinuationRun(root);
3245 + }
3246 + }
3247 + return true;
3248 + }
3249 + if (notification.sessionId === root) return false;
3250 + if (yield* Ref.get(stoppedRunQuarantine)) return true;
3251 + const update = notification.update;
3252 + if (update.sessionUpdate === "state_update") {
3253 + const child = nativeChild(notification.sessionId);
3254 + setNativeChildState(
3255 + child,
3256 + update.state !== "idle"
3257 + ? "running"
3258 + : update.stopReason === "error"
3259 + ? "failed"
3260 + : update.stopReason === "cancelled"
3261 + ? "cancelled"
3262 + : "completed",
3263 + update.state === "idle" ? (update.error?.message ?? null) : null,
3264 + );
3265 + }
3266 + if (context !== null) yield* syncNativeChildren(context);
3267 + yield* projectNativeChildFrame(notification);
3268 + const owner = nativeChildOwner(notification.sessionId);
3269 + if (
3270 + owner?.subagent?.childSessionId != null &&
3271 + owner.status !== "running" &&
3272 + !hasWorkingDescendant(owner.subagent.childSessionId)
3273 + ) {
3274 + yield* publishNativeChildStatus(owner, owner.status);
3275 + }
3276 + return true;
3277 + });
3278 +
3279 const toolOutputText = (toolCall: AcpToolCallState): string => {
3280 if (typeof toolCall.data.rawOutput === "string") {
3281 return toolCall.data.rawOutput;
@@ -2951,6 +3404,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3404 const merged = mergeToolCallState(previous, incoming);
3405 const toolCall = flavor.normalizeToolCall?.(merged) ?? merged;
3406 context.tools.set(toolCall.toolCallId, toolCall);
3407 + if (
3408 + followsAgentWorkState &&
3409 + projectedStatus === undefined &&
3410 + flavor.isProvisionalSubagentTool?.(toolCall)
3411 + ) {
3412 + yield* rearmDeferredFinalize(context);
3413 + return;
3414 + }
3415 const backgroundTaskId = flavor.extractBackgroundTaskId?.(toolCall);
3416 if (backgroundTaskId !== undefined) {
3417 context.toolCallIdsByBackgroundTaskId.set(backgroundTaskId, toolCall.toolCallId);
@@ -3036,9 +3497,10 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
3497 // hydration; do not spawn a phantom subagent for a registered monitor.
3498 const subagentUpdate = hydratedRegisteredMonitor
3499 ? undefined
3039 - : flavor.extractSubagentUpdate?.(toolCall);
3500 + : extractSubagentUpdate(toolCall);
3501 if (subagentUpdate !== undefined) {
3502 yield* emitSubagent(context, subagentUpdate);
3503 + if (followsAgentWorkState) yield* syncNativeChildren(context);
3504 if (subagentUpdate.suppressNormalTool !== false) {
3505 yield* rearmDeferredFinalize(context);
3506 return;
@@ -3849,7 +4311,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4311 return true;
4312 });
4313
3852 - const settleIdleWork = Effect.fnUntraced(function* (context: ActiveAcpTurn) {
4314 + const settleIdleWork = Effect.fnUntraced(function* (
4315 + context: ActiveAcpTurn,
4316 + ): Effect.fn.Return<void> {
4317 if (
4318 !followsAgentWorkState ||
4319 context.finalized ||
@@ -3937,6 +4401,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4401 ) {
4402 const context = yield* Ref.get(activeTurn);
4403 const update = notification.update;
4404 + if (
4405 + followsAgentWorkState &&
4406 + flavor.extractSubagentSessionEvent !== undefined &&
4407 + notification.sessionId !== (yield* Ref.get(activeSessionId)) &&
4408 + (yield* Ref.get(stoppedRunQuarantine))
4409 + )
4410 + return;
4411 if (yield* projectAgentTerminalUpdate(notification, context)) return;
4412 if (
4413 update.sessionUpdate === "usage_update" ||
@@ -3996,6 +4467,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4467 }
4468 if (update.sessionUpdate !== "state_update") return;
4469 }
4470 + if (yield* followNativeChild(notification, context)) return;
4471 if (
4472 followsAgentWorkState &&
4473 !(yield* Ref.get(stoppedRunQuarantine)) &&
@@ -4090,7 +4562,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4562 for (const event of parseSessionUpdateEvent(notification).events) {
4563 if (event._tag !== "ToolCallUpdated") continue;
4564 const toolCall = flavor.normalizeToolCall?.(event.toolCall) ?? event.toolCall;
4093 - const subagentUpdate = flavor.extractSubagentUpdate(toolCall);
4565 + const subagentUpdate = extractSubagentUpdate(toolCall);
4566 if (subagentUpdate === undefined) continue;
4567 if (!acpSubagentStatusIsTerminal(subagentUpdate.status)) {
4568 continue;
@@ -4241,7 +4713,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4713 for (const event of parseSessionUpdateEvent(notification).events) {
4714 if (event._tag !== "ToolCallUpdated") continue;
4715 const toolCall = flavor.normalizeToolCall?.(event.toolCall) ?? event.toolCall;
4244 - const subagentUpdate = flavor.extractSubagentUpdate(toolCall);
4716 + const subagentUpdate = extractSubagentUpdate(toolCall);
4717 if (subagentUpdate !== undefined) {
4718 if (
4719 subagentUpdate.nativeTaskId === nativeTaskId ||
@@ -4251,64 +4723,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
4723 }
4724 continue;
4725 }
4254 - const key = `${nativeTaskId}:tool:${toolCall.toolCallId}`;
4255 - const previous = context.tools.get(key);
4256 - const merged = mergeToolCallState(previous, toolCall);
4257 - context.tools.set(key, merged);
4258 - if (!shouldPersistToolUpdate(context, key, previous, merged, merged.status))
4259 - continue;
4260 - const mcpIdentity = extractMcpToolCallIdentity(merged, {
4261 - embeddedTerminalCommands: [
4262 - ...embeddedTerminalCommands(notification.sessionId, toolCall.toolCallId),
4263 - ...(rootSessionId === null
4264 - ? []
4265 - : embeddedTerminalCommands(rootSessionId, toolCall.toolCallId)),
4266 - ],
4267 - });
4268 - const now = yield* DateTime.now;
4269 - const status = toolStatus(merged.status);
4270 - const startedAt = context.toolStartedAt.get(key) ?? now;
4271 - context.toolStartedAt.set(key, startedAt);
4272 - const ordinal = resolveSubagentChildOrdinal(subagent, key);
4273 - yield* emitProviderEvent({
4274 - type: "turn_item.updated",
4275 - driver,
4276 - turnItem: {
4277 - id: providerTurnItemId(key),
4278 - threadId: subagent.childThreadId,
4279 - runId: null,
4280 - nodeId: subagent.childRootNodeId,
4281 - providerThreadId: subagent.task.providerThreadId,
4282 - providerTurnId: null,
4283 - nativeItemRef: { driver, nativeId: key, strength: "strong" },
4284 - parentItemId: null,
4285 - ordinal,
4286 - status,
4287 - title: merged.title ?? merged.kind ?? "Tool",
4288 - startedAt,
4289 - completedAt: completedAtForStatus(status, now),
4290 - updatedAt: now,
4291 - type: "dynamic_tool",
4292 - ...(mcpIdentity === undefined
4293 - ? {}
4294 - : mcpToolPresentation({
4295 - serverName: mcpIdentity.server,
4296 - toolName: mcpIdentity.tool,
4297 - source: unknownRecord(
4298 - (
4299 - unknownRecord(unknownRecord(merged.data.rawOutput)?.result) ??
4300 - unknownRecord(merged.data.rawOutput)
4301 - )?._meta,
4302 - )?.source,
4303 - })),
4304 - toolName:
4305 - mcpIdentity === undefined
4306 - ? (merged.title ?? merged.kind ?? "Tool")
4307 - : `${mcpIdentity.server}.${mcpIdentity.tool}`,
4308 - input: merged.data.rawInput ?? null,
4309 - output: merged.data.rawOutput ?? merged.data.content ?? null,
4310 - },
4311 - });
4726 + yield* projectSubagentTool(subagent, notification.sessionId, toolCall);
4727 }
4728 return;
4729 }
@@ -5167,7 +5582,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5582 for (const event of parseSessionUpdateEvent(notification).events) {
5583 if (event._tag !== "ToolCallUpdated") continue;
5584 const toolCall = flavor.normalizeToolCall?.(event.toolCall) ?? event.toolCall;
5170 - const subagentUpdate = flavor.extractSubagentUpdate(toolCall);
5585 + const subagentUpdate = extractSubagentUpdate(toolCall);
5586 if (
5587 subagentUpdate === undefined ||
5588 !acpSubagentStatusIsTerminal(subagentUpdate.status)
@@ -5246,6 +5661,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5661 ) {
5662 if (carryover === null) return;
5663 for (const subagent of carryover.subagents) {
5664 + yield* terminalizeSubagentTools(subagent);
5665 if (acpSubagentStatusIsTerminal(subagent.task.status)) {
5666 if (!subagent.terminalStatusProjected) {
5667 yield* projectCarryoverSubagentStatus(
@@ -5264,14 +5680,12 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
5680 /* Provider events include streaming deltas; avoid a span for each update. */
5681 Effect.fnUntraced(
5682 function* (rawNotification: EffectAcpSchema.SessionNotification) {
5267 - if (clientTerminals !== undefined) {
5268 - const embedded = embeddedTerminalIdsFromSessionUpdate(rawNotification);
5269 - if (embedded !== undefined) {
5270 - rememberEmbeddedTerminals({
5271 - ...embedded,
5272 - sessionId: rawNotification.sessionId,
5273 - });
5274 - }
5683 + const embedded = embeddedTerminalIdsFromSessionUpdate(rawNotification);
5684 + if (embedded !== undefined) {
5685 + rememberEmbeddedTerminals({
5686 + ...embedded,
5687 + sessionId: rawNotification.sessionId,
5688 + });
5689 }
5690 const notification =
5691 clientTerminals === undefined
@@ -6326,6 +6740,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6740 }
6741 if (!options.terminalizeSubagents) return;
6742 for (const subagent of context.subagents.values()) {
6743 + yield* terminalizeSubagentTools(subagent);
6744 if (subagent.task.status !== "running" && subagent.task.status !== "pending") {
6745 continue;
6746 }
@@ -6346,7 +6761,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6761 context: ActiveAcpTurn,
6762 ) {
6763 for (const tool of context.tools.values()) {
6349 - if (!acpCompletedTurnShouldTerminalizeTool(tool, flavor)) continue;
6764 + if (!acpCompletedTurnShouldTerminalizeTool(tool, { ...flavor, extractSubagentUpdate }))
6765 + continue;
6766 yield* emitTool(context, tool, "completed");
6767 }
6768 });
@@ -6371,7 +6787,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6787 context: ActiveAcpTurn,
6788 status: "completed" | "interrupted" | "failed" | "cancelled",
6789 failure?: OrchestrationV2ProviderFailure,
6374 - ) {
6790 + ): Effect.fn.Return<void> {
6791 if (context.finalized) return;
6792 const settledStatus = context.interrupted ? "interrupted" : status;
6793 context.finalizedStatus = settledStatus;
@@ -6464,6 +6880,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6880 driver,
6881 providerThread: updatedProviderThread,
6882 });
6883 + const rootSessionId = yield* Ref.get(activeSessionId);
6884 yield* emitProviderEvent(
6885 settledStatus === "failed"
6886 ? {
@@ -6477,6 +6894,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
6894 `terminal-failure:${context.providerTurnId}`,
6895 ),
6896 status: settledStatus,
6897 + ...(followsAgentWorkState &&
6898 + rootSessionId !== null &&
6899 + hasWorkingDescendant(rootSessionId)
6900 + ? { childTurnsContinue: true }
6901 + : {}),
6902 failure: failure ?? makeProviderFailure({ class: "provider_error" }),
6903 ...(context.providerRetry === undefined
6904 ? {}
@@ -6868,6 +7290,8 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7290 createdAt: startedAt,
7291 updatedAt: startedAt,
7292 });
7293 + if (followsAgentWorkState)
7294 + yield* runtimeCallbackPermit.withPermit(syncNativeChildren(context));
7295 const attached = turnAttached;
7296 turnAttached = yield* Deferred.make<boolean>();
7297 yield* Deferred.succeed(attached, true);
@@ -7112,6 +7536,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7536 ...(postSettleContinuationEnabled || followsAgentWorkState
7537 ? {
7538 hasPendingBackgroundWork: Effect.gen(function* () {
7539 + const rootSessionId = yield* Ref.get(activeSessionId);
7540 + if (
7541 + followsAgentWorkState &&
7542 + rootSessionId !== null &&
7543 + hasWorkingDescendant(rootSessionId)
7544 + )
7545 + return true;
7546 if (
7547 followsAgentWorkState &&
7548 (agentWork?.state === "running" ||
@@ -7299,6 +7730,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
7730 yield* Ref.set(runtimeRestartRequired, true);
7731 agentWork = null;
7732 agentRequests.clear();
7733 + nativeChildren.clear();
7734 yield* quarantineStoppedRun();
7735 if (context !== null) context.interrupted = true;
7736 yield* cancelPendingRuntimeRequests();
packages/provider-acp/src/server/runtimeModel.ts
+3
@@ -814,6 +814,7 @@ export function canonicalItemTypeFromAcpToolKind(kind: string | undefined): Tool
814 function makeToolCallState(
815 input: {
816 readonly toolCallId: string;
817 + readonly name?: string | null | undefined;
818 readonly title?: string | null | undefined;
819 readonly kind?: EffectAcpSchema.ToolKind | null | undefined;
820 readonly status?: EffectAcpSchema.ToolCallStatus | null | undefined;
@@ -841,6 +842,7 @@ function makeToolCallState(
842 ? title
843 : undefined;
844 const data: Record<string, unknown> = { toolCallId };
845 + if (input.name != null) data.name = input.name;
846 if (kind) {
847 data.kind = kind;
848 }
@@ -911,6 +913,7 @@ function parseTypedToolCallState(
913 return makeToolCallState(
914 {
915 toolCallId: event.toolCallId,
916 + name: event.name,
917 title: event.title,
918 kind: event.kind,
919 status: event.status,