Skip to content

Commit 103d878

Browse files
jakeleventhalclaude
andcommitted
feat(fx): label a sub-agent's approval with that sub-agent
An fx change in review forwards a sub-agent's permission requests to the ACP client on the parent session and names the child in `_meta.fx.subagent.sessionId`. Without a label, the approval looked like the parent's own. When that child maps to a sub-agent T3 is following through fx's hooks, the approval on the parent carries "Sub-agent: <task or name>" as its reason, which web and mobile already show. The answer still goes back on the parent session. A request without the field, or for a child T3 has not mapped, stays a plain parent approval. Approvals stay on the parent because a runtime request belongs to a run, and child threads have none. The field is provisional until the fx change merges, so one schema in subagents.ts reads it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 77b6a16 commit 103d878

4 files changed

Lines changed: 165 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/Adapters/FxAdapterV2.test.ts‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,51 @@ const subagentPreview = (callId: string, reply: string) =>
177177
],
178178
});
179179

180+
/** fx asks, and takes the answer, on the parent session; a sub-agent's request names its child. */
181+
const writeApproval = (toolCallId: string, subagentSessionId?: string): ReadonlyArray<Frame> => [
182+
{
183+
type: "emit_inbound",
184+
label: `request:session/request_permission:${toolCallId}`,
185+
frame: {
186+
kind: "request",
187+
method: "session/request_permission",
188+
params: {
189+
sessionId: SESSION_ID,
190+
toolCall: {
191+
toolCallId:
192+
subagentSessionId === undefined ? toolCallId : `${subagentSessionId}/${toolCallId}`,
193+
name: "write_file",
194+
title: "Write notes.md",
195+
kind: "edit",
196+
status: "pending",
197+
rawInput: { path: "notes.md", content: "notes" },
198+
},
199+
options: [
200+
{ optionId: "allow_once", name: "Allow once", kind: "allow_once" },
201+
{ optionId: "allow_always", name: "Allow for this session", kind: "allow_always" },
202+
{ optionId: "reject_once", name: "Reject", kind: "reject_once" },
203+
],
204+
...(subagentSessionId === undefined
205+
? {}
206+
: {
207+
_meta: {
208+
fx: { subagent: { sessionId: subagentSessionId, workId: "work-1", toolCallId } },
209+
},
210+
}),
211+
},
212+
},
213+
},
214+
{
215+
type: "expect_outbound",
216+
label: `response:session/request_permission:${toolCallId}`,
217+
frame: {
218+
kind: "response",
219+
method: "session/request_permission",
220+
result: { outcome: { outcome: "selected", optionId: "allow_once" } },
221+
},
222+
},
223+
];
224+
180225
const initialize: ReadonlyArray<Frame> = [
181226
outbound("initialize"),
182227
answer("initialize", {
@@ -316,6 +361,15 @@ const openFxSession = Effect.fn("openFxSession")(function* (input: {
316361
runtimePolicy,
317362
});
318363
return yield* session.events.pipe(
364+
// Every approval is answered with Allow once.
365+
Stream.tap((event) =>
366+
event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending"
367+
? session.respondToRuntimeRequest({
368+
requestId: event.runtimeRequest.id,
369+
decision: "accept",
370+
})
371+
: Effect.void,
372+
),
319373
Stream.takeUntil((event) => event.type === "turn.terminal"),
320374
Stream.runCollect,
321375
);
@@ -547,4 +601,77 @@ describe("FxAdapterV2", () => {
547601
);
548602
}).pipe(Effect.provide(testLayer), Effect.scoped),
549603
);
604+
// vercel-labs/fx (unmerged): fx forwards a sub-agent's approval on the
605+
// parent session and names the child in `_meta.fx.subagent`.
606+
it.effect("labels a sub-agent's approval with that sub-agent on the parent", () =>
607+
Effect.gen(function* () {
608+
const childSessionId = "fx-session-02";
609+
const events = yield* openFxSession({
610+
scenario: "subagent-approval",
611+
runtimeMode: "approval-required",
612+
turnModel: "default",
613+
frames: [
614+
...initialize,
615+
outbound("session/new"),
616+
answer("session/new", {
617+
...fxSession({ model: FX_DEFAULT_MODEL, modes: FX_MAIN_MODES }),
618+
_meta: { fx: { hooksVersion: 1 } },
619+
}),
620+
...setConfig("mode", "ask", fxConfig({ model: FX_DEFAULT_MODEL, modes: FX_MAIN_MODES })),
621+
outbound("session/prompt"),
622+
subagentCall("call_sub", "Write notes.md for the release"),
623+
hook(SESSION_ID, "delegation", {
624+
callId: "call_sub",
625+
childSessionId,
626+
workId: "work-1",
627+
operationId: null,
628+
phase: "dispatched",
629+
}),
630+
...writeApproval("call_child_write", childSessionId),
631+
// No child is mapped yet, as without hooks.
632+
...writeApproval("call_unknown_write", "fx-session-09"),
633+
...writeApproval("call_parent_write"),
634+
hook(SESSION_ID, "delegation", {
635+
callId: "call_sub",
636+
childSessionId,
637+
workId: "work-1",
638+
operationId: null,
639+
phase: "completed",
640+
}),
641+
subagentPreview("call_sub", "Wrote notes.md."),
642+
hook(SESSION_ID, "tool.end", {
643+
call: { callId: "call_sub", name: "subagent", provenance: "model" },
644+
output: subagentReply("Wrote notes.md."),
645+
status: "success",
646+
error: null,
647+
}),
648+
answer("session/prompt", { stopReason: "end_turn" }),
649+
],
650+
});
651+
652+
const approvals = new Map(
653+
events.flatMap((event) =>
654+
event.type === "turn_item.updated" && event.turnItem.type === "approval_request"
655+
? [[event.turnItem.nativeItemRef?.nativeId, event.turnItem] as const]
656+
: [],
657+
),
658+
);
659+
assert.deepEqual(
660+
[...approvals].map(([nativeId, item]) => [nativeId, item.threadId, item.reason]),
661+
[
662+
[
663+
`${childSessionId}/call_child_write`,
664+
ThreadId.make("thread-fx-subagent-approval"),
665+
"Sub-agent: Write notes.md for the release",
666+
],
667+
[
668+
"fx-session-09/call_unknown_write",
669+
ThreadId.make("thread-fx-subagent-approval"),
670+
undefined,
671+
],
672+
["call_parent_write", ThreadId.make("thread-fx-subagent-approval"), undefined],
673+
],
674+
);
675+
}).pipe(Effect.provide(testLayer), Effect.scoped),
676+
);
550677
});

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ export interface AcpAdapterV2Flavor {
309309
readonly approvalOptions?: (
310310
request: EffectAcpSchema.RequestPermissionRequest,
311311
) => ReadonlyArray<ProviderApprovalOption>;
312+
/** The sub-agent session behind a request the agent sends, and takes answers for, on the parent. */
313+
readonly permissionSubagentSessionId?: (
314+
request: EffectAcpSchema.RequestPermissionRequest,
315+
) => string | undefined;
312316
readonly onSessionEvent?: (
313317
event: AcpSessionRuntime.AcpSessionRuntimeEvent,
314318
) => Effect.Effect<void>;
@@ -5104,6 +5108,20 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
51045108
yield* closeTextStreams(context);
51055109
const parsed = parsePermissionRequest(params);
51065110
const approvalOptions = flavor.approvalOptions?.(params);
5111+
const subagentSessionId = flavor.permissionSubagentSessionId?.(params);
5112+
const subagent =
5113+
subagentSessionId === undefined
5114+
? undefined
5115+
: context.subagentsBySessionId.get(subagentSessionId);
5116+
const reason =
5117+
subagent === undefined
5118+
? undefined
5119+
: `Sub-agent: ${subagentThreadTitle({
5120+
parentTitle: context.input.appThread.title,
5121+
title: subagent.task.title,
5122+
prompt: subagent.task.prompt,
5123+
ordinal: [...context.subagents.values()].indexOf(subagent) + 1,
5124+
})}`;
51075125
const nativeRequestId = params.toolCall.toolCallId;
51085126
const requestId = yield* idAllocator.allocate.runtimeRequest({
51095127
driver,
@@ -5178,6 +5196,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
51785196
requestId,
51795197
requestKind,
51805198
...(parsed.detail === undefined ? {} : { prompt: parsed.detail }),
5199+
...(reason === undefined ? {} : { reason }),
51815200
...(approvalOptions === undefined ? {} : { options: approvalOptions }),
51825201
};
51835202
yield* Ref.update(pendingRuntimeRequests, (current) => {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ import {
3838
type AcpAdapterV2Flavor,
3939
type AcpAdapterV2RuntimeInput,
4040
} from "@t3tools/provider-acp/server/adapter";
41-
import { extractFxSubagentUpdate, withFxSubagentHooks } from "./subagents.ts";
41+
import {
42+
extractFxSubagentUpdate,
43+
fxPermissionSubagentSessionId,
44+
withFxSubagentHooks,
45+
} from "./subagents.ts";
4246

4347
export { FX_DRIVER_KIND } from "./acpSupport.ts";
4448

@@ -112,6 +116,7 @@ function makeFxAcpAdapterFlavor(options: FxAdapterV2Options): AcpAdapterV2Flavor
112116
),
113117
sessionModeForPolicy: fxSessionModeForPolicy,
114118
permissionDisposition: fxPermissionDisposition,
119+
permissionSubagentSessionId: fxPermissionSubagentSessionId,
115120
extractSubagentUpdate: extractFxSubagentUpdate,
116121
...(options.assertComplete === undefined ? {} : { assertComplete: options.assertComplete }),
117122
};

‎packages/provider-fx/src/server/subagents.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ export function extractFxSubagentUpdate(
186186
};
187187
}
188188

189+
// Provisional: fx names the child behind a forwarded approval here in an
190+
// unmerged PR, so a rename changes this schema alone.
191+
const decodePermissionSubagent = Schema.decodeUnknownOption(
192+
Schema.Struct({ fx: Schema.Struct({ subagent: Schema.Struct({ sessionId: Schema.String }) }) }),
193+
);
194+
195+
/** The child session behind a sub-agent's approval, which fx asks and takes answers for on the parent. */
196+
export function fxPermissionSubagentSessionId(
197+
request: EffectAcpSchema.RequestPermissionRequest,
198+
): string | undefined {
199+
return Option.getOrUndefined(decodePermissionSubagent(request._meta))?.fx.subagent.sessionId;
200+
}
201+
189202
/** fx's own `tool_call` kinds (src/acp/tool_call_presentation.zig). */
190203
function fxToolKind(name: string): EffectAcpSchema.ToolKind {
191204
switch (name) {

0 commit comments

Comments
 (0)