Skip to content

Commit 3686561

Browse files
committed
fix(acp): recover context for id-only permission requests
1 parent 9e09926 commit 3686561

3 files changed

Lines changed: 224 additions & 123 deletions

File tree

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

Lines changed: 197 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,134 +2985,209 @@ describe("AcpAdapterV2", () => {
29852985
}).pipe(Effect.provide(layerTest), Effect.scoped),
29862986
);
29872987

2988-
it.effect("does not turn an unknown permission approval into an execute grant", () =>
2989-
Effect.gen(function* () {
2990-
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2991-
const path = yield* Path.Path;
2992-
const selfInvocation = yield* resolveSelfInvocation();
2993-
const mockAgentPath = yield* path.fromFileUrl(
2994-
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
2995-
);
2996-
type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
2997-
let requestPermission: Parameters<RuntimeService["handleRequestPermission"]>[0] | undefined;
2998-
let createTerminal: Parameters<RuntimeService["handleCreateTerminal"]>[0] | undefined;
2999-
let runtimeInput: AcpAdapterV2RuntimeInput | undefined;
3000-
const makeRuntime = makeMockRuntime({
3001-
childProcessSpawner,
3002-
mockAgentPath,
3003-
environment: { T3_ACP_HANG_PROMPT_FOREVER: "1" },
3004-
wrapRuntime: (runtime) => ({
3005-
...runtime,
3006-
handleRequestPermission: (handler) =>
3007-
Effect.sync(() => {
3008-
requestPermission = handler;
3009-
}).pipe(Effect.andThen(runtime.handleRequestPermission(handler))),
3010-
handleCreateTerminal: (handler) =>
3011-
Effect.sync(() => {
3012-
createTerminal = handler;
3013-
}).pipe(Effect.andThen(runtime.handleCreateTerminal(handler))),
3014-
}),
3015-
});
3016-
const instanceId = ProviderInstanceId.make("acp-test-unknown-permission-grant");
3017-
const adapter = yield* makeAcpAdapterV2({
3018-
instanceId,
3019-
flavor: {
3020-
driver: ACP_TEST_DRIVER,
3021-
capabilities: AcpProviderCapabilitiesV2,
3022-
interruptPromptOnCancel: true,
3023-
makeRuntime: (input) =>
3024-
Effect.sync(() => {
3025-
runtimeInput = input;
3026-
}).pipe(Effect.andThen(makeRuntime(input))),
3027-
},
3028-
selfInvocation,
3029-
clientTerminals: {},
3030-
});
3031-
const threadId = ThreadId.make("thread-acp-unknown-permission-grant");
3032-
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3033-
runtimeMode: "approval-required",
3034-
interactionMode: "default",
3035-
cwd: process.cwd(),
3036-
});
3037-
const modelSelection = { instanceId, model: "default" } as const;
3038-
const runtime = yield* adapter.openSession({
3039-
threadId,
3040-
providerSessionId: ProviderSessionId.make("provider-session-acp-unknown-permission-grant"),
3041-
modelSelection,
3042-
runtimePolicy,
3043-
});
3044-
const providerThread = yield* runtime.ensureThread({
3045-
threadId,
3046-
modelSelection,
3047-
runtimePolicy,
3048-
});
3049-
const turnFiber = yield* runtime
3050-
.startTurn(
3051-
makeTurnInput({
3052-
threadId,
3053-
providerThread,
3054-
instanceId,
3055-
runtimePolicy,
3056-
now: yield* DateTime.now,
2988+
it.effect.each(["unknown", "tracked", "explicit-read"] as const)(
2989+
"authorizes terminals only with known permission context (%s)",
2990+
(context) =>
2991+
Effect.gen(function* () {
2992+
const tracked = context !== "unknown";
2993+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2994+
const path = yield* Path.Path;
2995+
const selfInvocation = yield* resolveSelfInvocation();
2996+
const mockAgentPath = yield* path.fromFileUrl(
2997+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
2998+
);
2999+
type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
3000+
let requestPermission: Parameters<RuntimeService["handleRequestPermission"]>[0] | undefined;
3001+
let sessionUpdate: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
3002+
let createTerminal: Parameters<RuntimeService["handleCreateTerminal"]>[0] | undefined;
3003+
let runtimeInput: AcpAdapterV2RuntimeInput | undefined;
3004+
const makeRuntime = makeMockRuntime({
3005+
childProcessSpawner,
3006+
mockAgentPath,
3007+
environment: { T3_ACP_HANG_PROMPT_FOREVER: "1" },
3008+
wrapRuntime: (runtime) => ({
3009+
...runtime,
3010+
handleRequestPermission: (handler) =>
3011+
Effect.sync(() => {
3012+
requestPermission = handler;
3013+
}).pipe(Effect.andThen(runtime.handleRequestPermission(handler))),
3014+
handleSessionUpdate: (handler) =>
3015+
Effect.sync(() => {
3016+
sessionUpdate = handler;
3017+
}).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
3018+
handleCreateTerminal: (handler) =>
3019+
Effect.sync(() => {
3020+
createTerminal = handler;
3021+
}).pipe(Effect.andThen(runtime.handleCreateTerminal(handler))),
30573022
}),
3058-
)
3059-
.pipe(Effect.forkScoped);
3060-
if (requestPermission === undefined || createTerminal === undefined) {
3061-
return yield* Effect.die("ACP runtime must register permission and terminal handlers");
3062-
}
3063-
3064-
const permissionFiber = yield* requestPermission(
3065-
{
3066-
sessionId: "mock-session-1",
3067-
toolCall: {
3068-
toolCallId: "unknown-permission-tool",
3069-
title: "Unknown permission kind",
3023+
});
3024+
const instanceId = ProviderInstanceId.make("acp-test-unknown-permission-grant");
3025+
const adapter = yield* makeAcpAdapterV2({
3026+
instanceId,
3027+
flavor: {
3028+
driver: ACP_TEST_DRIVER,
3029+
capabilities: AcpProviderCapabilitiesV2,
3030+
interruptPromptOnCancel: true,
3031+
permissionDisposition: () => "ask",
3032+
makeRuntime: (input) =>
3033+
Effect.sync(() => {
3034+
runtimeInput = input;
3035+
}).pipe(Effect.andThen(makeRuntime(input))),
30703036
},
3071-
options: [{ optionId: "allow-once", name: "Allow once", kind: "allow_once" }],
3072-
},
3073-
{ requestId: "unknown-permission-request", method: "session/request_permission" },
3074-
).pipe(Effect.forkScoped);
3075-
const pending = Option.getOrThrow(
3076-
yield* runtime.events.pipe(
3077-
Stream.filter(
3078-
(event) =>
3079-
event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending",
3037+
selfInvocation,
3038+
clientTerminals: {},
3039+
});
3040+
const threadId = ThreadId.make("thread-acp-unknown-permission-grant");
3041+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3042+
runtimeMode: "approval-required",
3043+
interactionMode: "default",
3044+
cwd: process.cwd(),
3045+
});
3046+
const modelSelection = { instanceId, model: "default" } as const;
3047+
const runtime = yield* adapter.openSession({
3048+
threadId,
3049+
providerSessionId: ProviderSessionId.make(
3050+
"provider-session-acp-unknown-permission-grant",
30803051
),
3052+
modelSelection,
3053+
runtimePolicy,
3054+
});
3055+
const providerThread = yield* runtime.ensureThread({
3056+
threadId,
3057+
modelSelection,
3058+
runtimePolicy,
3059+
});
3060+
const turnRunning = yield* runtime.events.pipe(
3061+
Stream.filter((event) => event.type === "provider_turn.updated"),
30813062
Stream.runHead,
3082-
),
3083-
);
3084-
if (
3085-
pending.type !== "runtime_request.updated" ||
3086-
pending.runtimeRequest.providerTurnId === null
3087-
) {
3088-
return yield* Effect.die("Expected an unknown ACP permission request");
3089-
}
3090-
const responseFiber = yield* runtime
3091-
.respondToRuntimeRequest({ requestId: pending.runtimeRequest.id, decision: "accept" })
3092-
.pipe(Effect.forkScoped);
3093-
assert.equal((yield* Fiber.join(permissionFiber)).outcome.outcome, "selected");
3094-
if (runtimeInput?.onOutgoingResponse === undefined) {
3095-
return yield* Effect.die("ACP runtime must expose native response acknowledgements");
3096-
}
3097-
yield* runtimeInput.onOutgoingResponse("unknown-permission-request");
3098-
yield* Fiber.join(responseFiber);
3063+
Effect.forkScoped,
3064+
);
3065+
const turnFiber = yield* runtime
3066+
.startTurn(
3067+
makeTurnInput({
3068+
threadId,
3069+
providerThread,
3070+
instanceId,
3071+
runtimePolicy,
3072+
now: yield* DateTime.now,
3073+
}),
3074+
)
3075+
.pipe(Effect.forkScoped);
3076+
if (
3077+
requestPermission === undefined ||
3078+
createTerminal === undefined ||
3079+
sessionUpdate === undefined
3080+
) {
3081+
return yield* Effect.die("ACP runtime must register permission and terminal handlers");
3082+
}
3083+
yield* Fiber.join(turnRunning);
3084+
if (tracked) {
3085+
const toolTracked = yield* runtime.events.pipe(
3086+
Stream.filter(
3087+
(event) =>
3088+
event.type === "turn_item.updated" &&
3089+
event.turnItem.nativeItemRef?.nativeId === "unknown-permission-tool",
3090+
),
3091+
Stream.runHead,
3092+
Effect.forkScoped,
3093+
);
3094+
yield* sessionUpdate({
3095+
sessionId: "mock-session-1",
3096+
update: {
3097+
sessionUpdate: "tool_call",
3098+
toolCallId: "unknown-permission-tool",
3099+
title: "Run command",
3100+
kind: "execute",
3101+
status: "pending",
3102+
rawInput: { command: "git status" },
3103+
},
3104+
});
3105+
yield* Fiber.join(toolTracked);
3106+
}
30993107

3100-
const terminalExit = yield* createTerminal(
3101-
{
3102-
sessionId: "mock-session-1",
3103-
command: process.execPath,
3104-
args: ["-e", "process.stdout.write('unexpected')"],
3105-
},
3106-
{ requestId: "unknown-permission-terminal", method: "terminal/create" },
3107-
).pipe(Effect.exit);
3108-
assert.isTrue(Exit.isFailure(terminalExit));
3108+
const permissionFiber = yield* requestPermission(
3109+
{
3110+
sessionId: "mock-session-1",
3111+
toolCall: {
3112+
toolCallId: "unknown-permission-tool",
3113+
...(tracked ? {} : { title: "Unknown permission kind" }),
3114+
...(context === "explicit-read"
3115+
? {
3116+
title: "Read file",
3117+
kind: "read" as const,
3118+
rawInput: { command: "cat README.md" },
3119+
}
3120+
: {}),
3121+
},
3122+
options: [{ optionId: "allow-once", name: "Allow once", kind: "allow_once" }],
3123+
},
3124+
{ requestId: "unknown-permission-request", method: "session/request_permission" },
3125+
).pipe(Effect.forkScoped);
3126+
const pending = Option.getOrThrow(
3127+
yield* runtime.events.pipe(
3128+
Stream.filter(
3129+
(event) =>
3130+
event.type === "runtime_request.updated" &&
3131+
event.runtimeRequest.status === "pending",
3132+
),
3133+
Stream.runHead,
3134+
),
3135+
);
3136+
if (
3137+
pending.type !== "runtime_request.updated" ||
3138+
pending.runtimeRequest.providerTurnId === null
3139+
) {
3140+
return yield* Effect.die("Expected an unknown ACP permission request");
3141+
}
3142+
const approval = Option.getOrThrow(
3143+
yield* runtime.events.pipe(
3144+
Stream.filter(
3145+
(event) =>
3146+
event.type === "turn_item.updated" && event.turnItem.type === "approval_request",
3147+
),
3148+
Stream.runHead,
3149+
),
3150+
);
3151+
if (
3152+
approval.type !== "turn_item.updated" ||
3153+
approval.turnItem.type !== "approval_request"
3154+
) {
3155+
return yield* Effect.die("Expected an ACP approval item");
3156+
}
3157+
assert.equal(
3158+
approval.turnItem.prompt,
3159+
context === "explicit-read"
3160+
? "cat README.md"
3161+
: tracked
3162+
? "git status"
3163+
: "Unknown permission kind",
3164+
);
3165+
const responseFiber = yield* runtime
3166+
.respondToRuntimeRequest({ requestId: pending.runtimeRequest.id, decision: "accept" })
3167+
.pipe(Effect.forkScoped);
3168+
assert.equal((yield* Fiber.join(permissionFiber)).outcome.outcome, "selected");
3169+
if (runtimeInput?.onOutgoingResponse === undefined) {
3170+
return yield* Effect.die("ACP runtime must expose native response acknowledgements");
3171+
}
3172+
yield* runtimeInput.onOutgoingResponse("unknown-permission-request");
3173+
yield* Fiber.join(responseFiber);
31093174

3110-
yield* runtime.interruptTurn({
3111-
providerThread,
3112-
providerTurnId: pending.runtimeRequest.providerTurnId,
3113-
});
3114-
yield* Fiber.join(turnFiber);
3115-
}).pipe(Effect.provide(layerTest), Effect.scoped),
3175+
const terminalExit = yield* createTerminal(
3176+
{
3177+
sessionId: "mock-session-1",
3178+
command: process.execPath,
3179+
args: ["-e", "process.stdout.write('unexpected')"],
3180+
},
3181+
{ requestId: "unknown-permission-terminal", method: "terminal/create" },
3182+
).pipe(Effect.exit);
3183+
assert.equal(Exit.isSuccess(terminalExit), context === "tracked");
3184+
3185+
yield* runtime.interruptTurn({
3186+
providerThread,
3187+
providerTurnId: pending.runtimeRequest.providerTurnId,
3188+
});
3189+
yield* Fiber.join(turnFiber);
3190+
}).pipe(Effect.provide(layerTest), Effect.scoped),
31163191
);
31173192

31183193
it.effect("replaces the ACP session and clears conversation state on rollback", () =>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import {
7474
findModelConfigOption,
7575
mergeToolCallState,
7676
parsePermissionRequest,
77+
withTrackedToolCall,
7778
parseSessionUpdateEvent,
7879
toolCallVisibleOutputChanged,
7980
type AcpPlanUpdate,
@@ -5974,9 +5975,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
59745975
projectAcpRuntimeSessionUpdate(handlerGeneration, rawNotification),
59755976
);
59765977
}
5977-
yield* targetRuntime.handleRequestPermission((params, requestContext) =>
5978+
yield* targetRuntime.handleRequestPermission((nativeParams, requestContext) =>
59785979
Effect.gen(function* () {
59795980
const transportRequestId = requestContext.requestId;
5981+
const params = withTrackedToolCall(
5982+
nativeParams,
5983+
(yield* Ref.get(activeTurn))?.tools.get(nativeParams.toolCall.toolCallId),
5984+
);
59805985
const permissionQuestion = flavor.extractPermissionQuestion?.(params);
59815986
if (permissionQuestion !== undefined) {
59825987
const userInput = yield* requestUserInputWithAdmission(

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,27 @@ export function embeddedTerminalIdsFromSessionUpdate(
11791179
return terminalIds.length === 0 ? undefined : { toolCallId: update.toolCallId, terminalIds };
11801180
}
11811181

1182+
/** ID-only permission requests reuse the tool context received earlier in the turn. */
1183+
export function withTrackedToolCall(
1184+
params: EffectAcpSchema.RequestPermissionRequest,
1185+
tracked: AcpToolCallState | undefined,
1186+
): EffectAcpSchema.RequestPermissionRequest {
1187+
if (tracked === undefined) return params;
1188+
const { toolCall } = params;
1189+
const title = typeof tracked.data.title === "string" ? tracked.data.title : tracked.title;
1190+
return {
1191+
...params,
1192+
toolCall: {
1193+
...toolCall,
1194+
...(!toolCall.title && title ? { title } : {}),
1195+
...(!toolCall.kind && tracked.kind ? { kind: tracked.kind } : {}),
1196+
...(toolCall.rawInput == null && tracked.data.rawInput != null
1197+
? { rawInput: tracked.data.rawInput }
1198+
: {}),
1199+
},
1200+
};
1201+
}
1202+
11821203
export function parsePermissionRequest(
11831204
params: EffectAcpSchema.RequestPermissionRequest,
11841205
): AcpPermissionRequest {

0 commit comments

Comments
 (0)