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
116 changes: 114 additions & 2 deletions apps/server/scripts/antigravity-mock-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ const program = Effect.gen(function* () {
{ optionId: "deny", name: "Deny", kind: "reject_once" },
],
});
// Threadlines room tool: auto-approved once even in Supervised mode.
// Threadlines room tool (legacy call_mcp_tool wrapper): auto-approved once even in Supervised mode.
yield* ask({
toolCallId: "call_room",
title: "MCP Tool room_ask",
Expand All @@ -576,7 +576,23 @@ const program = Effect.gen(function* () {
Arguments: { agent: "Codex", question: "Ready?" },
},
});
// Threadlines browser tool: must still ask in Supervised mode.
// Direct HTTP MCP room tool (`agy acp` 1.5 shape): auto-approved once without asking.
yield* ask({
toolCallId: "call_room_direct",
title: "Run mcp_threadlines_room_room_diff",
kind: "other",
status: "pending",
rawInput: { view: "status", toolSummary: "Check git status" },
});
// Direct HTTP MCP browser tool: must still ask in Supervised mode.
yield* ask({
toolCallId: "call_browser_direct",
title: "Run mcp_threadlines_browser_browser_snapshot",
kind: "other",
status: "pending",
rawInput: { toolSummary: "Read page" },
});
// Threadlines browser tool (wrapper): must still ask in Supervised mode.
yield* ask({
toolCallId: "call_browser",
title: "MCP Tool browser_status",
Expand Down Expand Up @@ -604,6 +620,102 @@ const program = Effect.gen(function* () {
yield* modelCallEnds(sessionId);
return { stopReason: "end_turn" as const };
}
if (scenario === "session-approvals") {
const ask = (
toolCall: AcpSchema.ToolCallUpdate,
warning?: { readonly title: string; readonly message: string },
) =>
agent.client.requestPermission({
sessionId,
toolCall,
options: [
{
optionId: "allow",
name: "Allow",
kind: "allow_once",
...(warning ? { _meta: { "agy.security.warning": warning } } : {}),
},
{ optionId: "deny", name: "Deny", kind: "reject_once" },
],
});
if (promptsSeen === 1) {
yield* ask(
{
toolCallId: "call_gh_view_1",
title: "gh pr view 101 --json state",
kind: "execute",
status: "pending",
rawInput: { CommandLine: "gh pr view 101 --json state" },
},
{ title: "Network access", message: "Runs outside the terminal sandbox." },
);
// Covered by the `gh pr view` session grant.
yield* ask({
toolCallId: "call_gh_view_2",
title: "gh pr view 102 --json body",
kind: "execute",
status: "pending",
rawInput: { CommandLine: "gh pr view 102 --json body" },
});
// Mutating sibling: must still ask.
yield* ask({
toolCallId: "call_gh_merge",
title: "gh pr merge 101 --squash",
kind: "execute",
status: "pending",
rawInput: { CommandLine: "gh pr merge 101 --squash" },
});
// Two concurrent browser_snapshot calls: granting the first resolves both.
yield* Effect.all(
[
ask({
toolCallId: "call_snap_1",
title: "Run mcp_threadlines_browser_browser_snapshot",
kind: "other",
status: "pending",
rawInput: { tabId: "t1" },
}),
ask({
toolCallId: "call_snap_2",
title: "Run mcp_threadlines_browser_browser_snapshot",
kind: "other",
status: "pending",
rawInput: { tabId: "t2" },
}),
],
{ concurrency: "unbounded" },
);
// Subsequent browser_snapshot is auto-approved; browser_click still asks.
yield* ask({
toolCallId: "call_snap_3",
title: "Run mcp_threadlines_browser_browser_snapshot",
kind: "other",
status: "pending",
rawInput: { tabId: "t3" },
});
yield* ask({
toolCallId: "call_click",
title: "Run mcp_threadlines_browser_browser_click",
kind: "other",
status: "pending",
rawInput: { target: "btn" },
});
yield* say(sessionId, "Turn 1 done.");
yield* modelCallEnds(sessionId);
return { stopReason: "end_turn" as const };
}
// Second turn (sent in plan mode): prior `gh pr view` grant must not auto-approve.
yield* ask({
toolCallId: "call_gh_view_plan",
title: "gh pr view 103 --json state",
kind: "execute",
status: "pending",
rawInput: { CommandLine: "gh pr view 103 --json state" },
});
yield* say(sessionId, "Plan turn done.");
yield* modelCallEnds(sessionId);
return { stopReason: "end_turn" as const };
}
if (scenario === "permission-second") {
if (promptsSeen === 1) {
yield* say(sessionId, "First done.");
Expand Down
114 changes: 101 additions & 13 deletions apps/server/src/provider/acp/AcpAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import {
isAcpAuthRequiredError,
mapAcpToAdapterError,
} from "./AcpAdapterSupport.ts";
import { acpSessionApprovalGrantKey, isAcpSessionApprovalGranted } from "./AcpSessionApprovals.ts";
import {
makeAcpAssistantItemEvent,
makeAcpContentDeltaEvent,
Expand Down Expand Up @@ -277,6 +278,8 @@ export interface AcpAdapterOptions<Settings extends AcpProviderSettings> {
interface PendingApproval {
readonly decision: Deferred.Deferred<ProviderApprovalDecision>;
readonly kind: string | "unknown";
/** Re-checks if a client-side session grant added by another approval covers this pending card. */
readonly grantIfCovered?: () => Effect.Effect<void>;
}

interface PendingUserInput {
Expand Down Expand Up @@ -334,6 +337,10 @@ interface AcpSessionContext {
readonly acpSessionId: string;
/** Attached Threadlines MCP servers pre-approved without asking (room, pages, commands, device). */
readonly preapprovedMcpServers: ReadonlySet<string>;
/** Client-side session approval grants remembered in memory (`descriptor.clientSessionApprovals`). */
readonly sessionApprovalGrants: Set<string>;
/** Interaction mode of the active prompt (`plan` disables client-side session approval auto-grants). */
activeInteractionMode: ProviderInteractionMode | undefined;
/** What the agent's `initialize` answer says it reads in a prompt. */
readonly promptCapabilities: EffectAcpSchema.PromptCapabilities | null | undefined;
/** The subagents this session's tool calls launched (`subagents`), as the thread shows them. */
Expand Down Expand Up @@ -2369,13 +2376,32 @@ export function makeAcpAdapter<Settings extends AcpProviderSettings>(
(option) => !descriptor.isLastingPermissionOption!(option),
)
: params.options;
// Require an explicit allow_once option (never a synthetic fallback
// for an empty options list) and no agent-native allow_always.
const hasExplicitAllowOnce = offered.some(
(option) => option.kind === "allow_once" && option.optionId.trim().length > 0,
);
const clientSessionApprovalEligible =
descriptor.clientSessionApprovals === true &&
ownSession &&
ctx.activeInteractionMode !== "plan" &&
!offered.some((option) => option.kind === "allow_always") &&
hasExplicitAllowOnce;
const sessionGranted =
clientSessionApprovalEligible &&
isAcpSessionApprovalGranted(
permissionRequest,
ctx.sessionApprovalGrants,
ctx.cwdRoots,
);
// Full access answers each request once: an "always" option
// would leave a standing rule behind in the agent's own store.
// A request with no "once" to give goes to the user instead.
if (
input.runtimeMode === "full-access" ||
planFileWrite ||
preapprovedThreadlinesMcpCall
preapprovedThreadlinesMcpCall ||
sessionGranted
) {
const once = acpPermissionOptionId("accept", offered);
if (once !== undefined) return selected(once);
Expand All @@ -2387,25 +2413,65 @@ export function makeAcpAdapter<Settings extends AcpProviderSettings>(
requestId: ApprovalRequestId.make(crypto.randomUUID()),
permissionRequest,
};
pendingApprovals.set(card.requestId, { decision, kind: permissionRequest.kind });
const grantIfCovered =
clientSessionApprovalEligible && ctx !== undefined
? () =>
ctx.activeInteractionMode !== "plan" &&
isAcpSessionApprovalGranted(
card.permissionRequest,
ctx.sessionApprovalGrants,
ctx.cwdRoots,
)
? Deferred.succeed(decision, "accept").pipe(Effect.asVoid)
: Effect.void
: undefined;
pendingApprovals.set(card.requestId, {
decision,
kind: permissionRequest.kind,
...(grantIfCovered ? { grantIfCovered } : {}),
});
const warningFor = (kind: EffectAcpSchema.PermissionOption["kind"]) => {
const option = offered.find((candidate) => candidate.kind === kind);
return option ? descriptor.permissionOptionWarning?.(option) : undefined;
};
const acceptForSessionWarning = warningFor("allow_always");
const acceptWarning = warningFor("allow_once");
const decisionWarnings =
acceptForSessionWarning || acceptWarning
? {
...(acceptWarning ? { accept: acceptWarning } : {}),
...(acceptForSessionWarning
? { acceptForSession: acceptForSessionWarning }
: {}),
}
: undefined;
const availableDecisions = acpAvailableDecisions(offered);
const baseDecisions = acpAvailableDecisions(offered);
const decisionsForCard = () => {
if (
!baseDecisions ||
!clientSessionApprovalEligible ||
baseDecisions.includes("acceptForSession") ||
acpSessionApprovalGrantKey(card.permissionRequest, ctx.cwdRoots) === undefined
) {
return baseDecisions;
}
return [
...baseDecisions.filter((candidate) => candidate !== "accept"),
"acceptForSession" as const,
...(baseDecisions.includes("accept") ? (["accept"] as const) : []),
];
};
const openCard = (turnId: TurnId | undefined) =>
Effect.gen(function* () {
const availableDecisions = decisionsForCard();
// A synthesized session grant authorizes the same `allow_once`
// option, so it must carry any warning attached to `allow_once`.
const effectiveSessionWarning =
acceptForSessionWarning ??
(clientSessionApprovalEligible &&
availableDecisions?.includes("acceptForSession")
? acceptWarning
: undefined);
const decisionWarnings =
effectiveSessionWarning || acceptWarning
? {
...(acceptWarning ? { accept: acceptWarning } : {}),
...(effectiveSessionWarning
? { acceptForSession: effectiveSessionWarning }
: {}),
}
: undefined;
yield* offerRuntimeEvent(
makeAcpRequestOpenedEvent({
stamp: yield* makeEventStamp(),
Expand Down Expand Up @@ -2454,7 +2520,11 @@ export function makeAcpAdapter<Settings extends AcpProviderSettings>(
);
card.requestId = ApprovalRequestId.make(crypto.randomUUID());
card.permissionRequest = next;
pendingApprovals.set(card.requestId, { decision, kind: next.kind });
pendingApprovals.set(card.requestId, {
decision,
kind: next.kind,
...(grantIfCovered ? { grantIfCovered } : {}),
});
yield* openCard(turnId);
}),
};
Expand All @@ -2472,6 +2542,21 @@ export function makeAcpAdapter<Settings extends AcpProviderSettings>(
}),
),
);
if (
resolved === "acceptForSession" &&
clientSessionApprovalEligible &&
ctx.activeInteractionMode !== "plan"
) {
const grantKey = acpSessionApprovalGrantKey(card.permissionRequest, ctx.cwdRoots);
if (grantKey !== undefined) {
ctx.sessionApprovalGrants.add(grantKey);
for (const [otherId, other] of pendingApprovals) {
if (otherId !== card.requestId && other.grantIfCovered) {
yield* other.grantIfCovered();
}
}
}
}
const turnId = yield* Deferred.await(shown);
yield* offerRuntimeEvent(
makeAcpRequestResolvedEvent({
Expand Down Expand Up @@ -2615,6 +2700,8 @@ export function makeAcpAdapter<Settings extends AcpProviderSettings>(
lastContextUsageKey: undefined,
acpSessionId: started.sessionId,
preapprovedMcpServers,
sessionApprovalGrants: new Set(),
activeInteractionMode: undefined,
promptCapabilities: started.initializeResult.agentCapabilities?.promptCapabilities,
subagentRoster:
descriptor.subagents === undefined
Expand Down Expand Up @@ -4106,6 +4193,7 @@ export function makeAcpAdapter<Settings extends AcpProviderSettings>(
}
// The mode and model the message asks for, set before its prompt goes out.
const configure = Effect.gen(function* () {
ctx.activeInteractionMode = input.interactionMode;
yield* applyRequestedSessionConfiguration({
runtime: ctx.acp,
threadId: input.threadId,
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/provider/acp/AcpProviderDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,13 @@ export interface AcpProviderDescriptor<Settings extends AcpProviderSettings> {
* chosen; "Accept for session" then appears only for a real session grant.
*/
readonly isLastingPermissionOption?: (option: EffectAcpSchema.PermissionOption) => boolean;
/**
* Offers "Always allow this session" client-side when a permission request
* includes `allow_once` without `allow_always` (e.g. Antigravity's `agy acp`).
* Grants live in memory on the open session and answer matching subsequent
* requests with `allow_once`, never persisting rules on disk.
*/
readonly clientSessionApprovals?: boolean;
/** A risk the agent attached to a permission option, shown with the approval. */
readonly permissionOptionWarning?: (
option: EffectAcpSchema.PermissionOption,
Expand Down
8 changes: 8 additions & 0 deletions apps/server/src/provider/acp/AcpRuntimeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export interface AcpToolCallState {
* `normalizeToolCall`; absent, the row type follows `kind`.
*/
readonly itemType?: ToolLifecycleItemType;
/**
* In-memory tag of which MCP wire shape `normalizeToolCall` established
* (`wrapper` vs `direct`), preserved across merges so later updates are only
* validated against that same form. Not written to `data`.
*/
readonly mcpCallMode?: "wrapper" | "direct";
readonly data: Record<string, unknown>;
}

Expand Down Expand Up @@ -591,6 +597,7 @@ export function mergeToolCallState(
const status = next.status ?? previous?.status;
const command = next.command ?? previous?.command;
const detail = next.detail ?? previous?.detail;
const mcpCallMode = next.mcpCallMode ?? previous?.mcpCallMode;
return {
toolCallId: next.toolCallId,
...(kind ? { kind } : {}),
Expand All @@ -599,6 +606,7 @@ export function mergeToolCallState(
...(status ? { status } : {}),
...(command ? { command } : {}),
...(detail ? { detail } : {}),
...(mcpCallMode ? { mcpCallMode } : {}),
data: mergeToolCallData(previous?.data, next.data, kind, command),
};
}
Expand Down
Loading
Loading