Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ describe("draft session lifecycle", () => {
"mock-agent",
"thread-1",
"project-1",
[{ type: "text", text: "hello" }]
[{ type: "text", text: "hello" }],
"turn-1"
);
expect(prompt.isOk()).toBe(true);
if (prompt.isErr()) throw new Error("expected prompt to succeed");
Expand Down
2 changes: 2 additions & 0 deletions apps/cli/__tests__/integration/wiring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("acp mock runtime", () => {
agentName: "mock-agent",
threadId: "thread-1",
projectId: "project-1",
turnId: "turn-1",
message: textMessage("ping"),
emit: (event) => {
emitted.push({ type: event.type });
Expand Down Expand Up @@ -59,6 +60,7 @@ describe("acp mock runtime", () => {
agentName: "mock-agent",
threadId: "thread-1",
projectId: "project-1",
turnId: "turn-1",
message: textMessage("ping"),
emit: () => Promise.resolve(),
emitTerminal: (event) => {
Expand Down
35 changes: 27 additions & 8 deletions apps/cli/src/core/acp/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { RuntimeSessionEvent } from "@acp-kit/core";
import type {
RequestPermissionRequest,
RuntimePermissionRequest,
RuntimeSessionEvent,
} from "@acp-kit/core";
import type {
SessionNotification,
SessionUpdate,
ToolCallUpdate,
Expand Down Expand Up @@ -193,17 +195,34 @@ export function mapSessionNotification(
}

export function mapApprovalRequest(
request: RequestPermissionRequest
request: RuntimePermissionRequest
): AgentEvent {
const rawToolCall = request.raw.toolCall as ToolCallUpdate & { id?: string };
const fields = mapToolCallUpdateFields(rawToolCall);
const toolCallId =
request.toolCallId ||
fields.toolCallId ||
rawToolCall.toolCallId ||
rawToolCall.id ||
"unknown-tool-call";

return ApprovalRequestEventSchema.parse({
type: "approval_request",
request: {
sessionId: request.sessionId,
toolCall: mapToolCallUpdateFields(request.toolCall),
options: request.options.map((option) => ({
optionId: option.optionId,
name: option.name,
kind: option.kind,
toolCall: {
...fields,
toolCallId,
title:
request.title ||
fields.title ||
rawToolCall.title ||
"Permission required",
},
options: (request.options ?? []).map((option) => ({
optionId: option.optionId ?? option.kind ?? "deny",
name: option.name ?? option.optionId ?? option.kind ?? "Option",
kind: option.kind ?? option.optionId ?? "reject_once",
})),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
},
});
Expand Down
21 changes: 4 additions & 17 deletions apps/cli/src/core/acp/host.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { PermissionDecision, type RuntimeHost } from "@acp-kit/core";
import type { RuntimeHost } from "@acp-kit/core";
import { createInteractiveHost } from "./interactive";

/** Interactive ACP host (permissions block until respondApproval). */
export function createDefaultHost(
onAgentExit?: RuntimeHost["onAgentExit"]
): RuntimeHost {
return {
requestPermission: (request) => {
const allow =
request.options.find((o) => o.kind === "allow_once") ??
request.options.find((o) => o.kind === "allow_always");

if (!allow?.optionId) return Promise.resolve(PermissionDecision.Deny);

return Promise.resolve(
allow.kind === "allow_always"
? PermissionDecision.AllowAlways
: PermissionDecision.AllowOnce
);
},
onAgentExit,
};
return createInteractiveHost(onAgentExit);
}
228 changes: 228 additions & 0 deletions apps/cli/src/core/acp/interactive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import { describe, expect, test } from "bun:test";
import { PermissionDecision } from "@acp-kit/core";
import { InteractivePendingRegistry } from "./interactive";

describe("InteractivePendingRegistry", () => {
test("blocks permission until respondApproval resolves", async () => {
const registry = new InteractivePendingRegistry();
const events: unknown[] = [];
registry.bindTurn({
sessionId: "session-1",
threadId: "thread-1",
turnId: "turn-1",
pushEvent: (event) => events.push(event),
});

const pending = registry.requestPermission({
sessionId: "session-1",
toolCallId: "tool-1",
toolName: "edit",
title: "Write file",
input: {},
options: [
{ optionId: "allow-once", name: "Allow", kind: "allow_once" },
{ optionId: "reject-once", name: "Reject", kind: "reject_once" },
],
raw: {
sessionId: "session-1",
toolCall: { toolCallId: "tool-1", title: "Write file" },
options: [
{ optionId: "allow-once", name: "Allow", kind: "allow_once" },
{ optionId: "reject-once", name: "Reject", kind: "reject_once" },
],
} as never,
});

expect(events).toHaveLength(1);
expect(events[0]).toMatchObject({ type: "approval_request" });

let settled = false;
pending
.then(() => {
settled = true;
})
.catch(() => undefined);
await Promise.resolve();
expect(settled).toBe(false);

expect(
registry.respondApproval({
threadId: "thread-1",
toolCallId: "tool-1",
optionId: "allow-once",
})
).toEqual({ turnId: "turn-1" });

await expect(pending).resolves.toBe(PermissionDecision.AllowOnce);
});

test("cancel denies pending permissions and declines elicitations", async () => {
const registry = new InteractivePendingRegistry();
registry.bindTurn({
sessionId: "session-1",
threadId: "thread-1",
turnId: "turn-1",
pushEvent: () => undefined,
});

const permission = registry.requestPermission({
sessionId: "session-1",
toolCallId: "tool-1",
toolName: "bash",
title: "Run",
input: {},
options: [{ optionId: "allow-once", name: "Allow", kind: "allow_once" }],
raw: {
sessionId: "session-1",
toolCall: { toolCallId: "tool-1", title: "Run" },
options: [
{ optionId: "allow-once", name: "Allow", kind: "allow_once" },
],
} as never,
});

const elicitation = registry.awaitElicitation({
sessionId: "session-1",
elicitationId: "elicit-1",
event: {
type: "elicitation_request",
sessionId: "session-1",
request: {
mode: "form",
elicitationId: "elicit-1",
message: "Name?",
requestedSchema: {
type: "object",
properties: { name: { type: "string" } },
},
},
},
});

registry.clearThread("thread-1");

await expect(permission).resolves.toBe(PermissionDecision.Deny);
await expect(elicitation).resolves.toEqual({ action: "decline" });
});

test("elicitation blocks until respondElicitation", async () => {
const registry = new InteractivePendingRegistry();
registry.bindTurn({
sessionId: "session-1",
threadId: "thread-1",
turnId: "turn-1",
pushEvent: () => undefined,
});

const pending = registry.awaitElicitation({
sessionId: "session-1",
elicitationId: "elicit-1",
event: {
type: "elicitation_request",
sessionId: "session-1",
request: {
mode: "url",
elicitationId: "elicit-1",
url: "https://example.com",
},
},
});

expect(
registry.respondElicitation({
threadId: "thread-1",
elicitationId: "elicit-1",
action: "decline",
})
).toEqual({ turnId: "turn-1" });

await expect(pending).resolves.toEqual({ action: "decline" });
});

test("duplicate elicitation declines without orphaning the first wait", async () => {
const registry = new InteractivePendingRegistry();
registry.bindTurn({
sessionId: "session-1",
threadId: "thread-1",
turnId: "turn-1",
pushEvent: () => undefined,
});

const event = {
type: "elicitation_request" as const,
sessionId: "session-1",
request: {
mode: "url" as const,
elicitationId: "elicit-1",
url: "https://example.com",
},
};

const first = registry.awaitElicitation({
sessionId: "session-1",
elicitationId: "elicit-1",
event,
});

await expect(
registry.awaitElicitation({
sessionId: "session-1",
elicitationId: "elicit-1",
event,
})
).resolves.toEqual({ action: "decline" });

expect(
registry.respondElicitation({
threadId: "thread-1",
elicitationId: "elicit-1",
action: "accept",
})
).toEqual({ turnId: "turn-1" });

await expect(first).resolves.toEqual({ action: "accept" });
});

test("respondApproval rejects wrong thread", () => {
const registry = new InteractivePendingRegistry();
registry.bindTurn({
sessionId: "session-1",
threadId: "thread-1",
turnId: "turn-1",
pushEvent: () => undefined,
});

const pending = registry.requestPermission({
sessionId: "session-1",
toolCallId: "tool-1",
toolName: "edit",
title: "Write",
input: {},
options: [{ optionId: "allow-once", name: "Allow", kind: "allow_once" }],
raw: {
sessionId: "session-1",
toolCall: { toolCallId: "tool-1", title: "Write" },
options: [
{ optionId: "allow-once", name: "Allow", kind: "allow_once" },
],
} as never,
});
pending.catch(() => undefined);

expect(
registry.respondApproval({
threadId: "other-thread",
toolCallId: "tool-1",
optionId: "allow-once",
})
).toBeNull();

expect(
registry.respondApproval({
threadId: "thread-1",
toolCallId: "tool-1",
optionId: "allow-once",
})
).toEqual({ turnId: "turn-1" });
});
});
Loading