Skip to content

Commit abf31ad

Browse files
fix: cancel aborted plugin approvals
1 parent 99bce5f commit abf31ad

22 files changed

Lines changed: 316 additions & 11 deletions

‎extensions/discord/src/approval-handler.runtime.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,17 @@ function createExecResolvedContainer(params: {
255255
? "Allowed (once)"
256256
: params.view.decision === "allow-always"
257257
? "Allowed (always)"
258-
: "Denied";
258+
: params.view.decision === "deny"
259+
? "Denied"
260+
: "Cancelled";
259261
const accentColor =
260262
params.view.decision === "deny"
261263
? "#ED4245"
262264
: params.view.decision === "allow-always"
263265
? "#5865F2"
264-
: "#57F287";
266+
: params.view.decision === "allow-once"
267+
? "#57F287"
268+
: "#99AAB5";
265269

266270
return new ExecApprovalContainer({
267271
cfg: params.cfg,

‎extensions/googlechat/src/approval-handler.runtime.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ function buildMetadataText(metadata: readonly { label: string; value: string }[]
9999
.join("<br>");
100100
}
101101

102-
function formatDecision(decision: ExecApprovalDecision): string {
102+
function formatDecision(decision: ExecApprovalDecision | null): string {
103103
return decision === "allow-once"
104104
? "Allowed once"
105105
: decision === "allow-always"
106106
? "Allowed always"
107-
: "Denied";
107+
: decision === "deny"
108+
? "Denied"
109+
: "Cancelled";
108110
}
109111

110112
function buildMainTextWidget(text: string) {

‎extensions/slack/src/approval-handler.runtime.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ function buildSlackMetadataContextBlocks(metadata: readonly SlackMetadataItem[])
150150
}
151151

152152
function resolveSlackApprovalDecisionLabel(
153-
decision: "allow-once" | "allow-always" | "deny",
153+
decision: "allow-once" | "allow-always" | "deny" | null,
154154
): string {
155155
return decision === "allow-once"
156156
? "Allowed once"
157157
: decision === "allow-always"
158158
? "Allowed always"
159-
: "Denied";
159+
: decision === "deny"
160+
? "Denied"
161+
: "Cancelled";
160162
}
161163

162164
function buildSlackPluginMetadata(view: SlackPluginApprovalView): SlackMetadataItem[] {

‎packages/gateway-protocol/src/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ import {
209209
ExecApprovalRequestParamsSchema,
210210
type ExecApprovalResolveParams,
211211
ExecApprovalResolveParamsSchema,
212+
type PluginApprovalCancelParams,
213+
PluginApprovalCancelParamsSchema,
212214
type PluginApprovalRequestParams,
213215
PluginApprovalRequestParamsSchema,
214216
type PluginApprovalResolveParams,
@@ -887,6 +889,9 @@ export const validatePluginApprovalRequestParams = lazyCompile<PluginApprovalReq
887889
export const validatePluginApprovalResolveParams = lazyCompile<PluginApprovalResolveParams>(
888890
PluginApprovalResolveParamsSchema,
889891
);
892+
export const validatePluginApprovalCancelParams = lazyCompile<PluginApprovalCancelParams>(
893+
PluginApprovalCancelParamsSchema,
894+
);
890895
export const validatePluginsUiDescriptorsParams = lazyCompile<PluginsUiDescriptorsParams>(
891896
PluginsUiDescriptorsParamsSchema,
892897
);

‎packages/gateway-protocol/src/schema/plugin-approvals.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ export const PluginApprovalResolveParamsSchema = Type.Object(
4848
},
4949
{ additionalProperties: false },
5050
);
51+
52+
/** Requester-side cancellation for a pending plugin approval. */
53+
export const PluginApprovalCancelParamsSchema = Type.Object(
54+
{
55+
id: NonEmptyString,
56+
},
57+
{ additionalProperties: false },
58+
);

‎packages/gateway-protocol/src/schema/protocol-schemas.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ import {
232232
NodeRenameParamsSchema,
233233
} from "./nodes.js";
234234
import {
235+
PluginApprovalCancelParamsSchema,
235236
PluginApprovalRequestParamsSchema,
236237
PluginApprovalResolveParamsSchema,
237238
} from "./plugin-approvals.js";
@@ -564,6 +565,7 @@ export const ProtocolSchemas = {
564565
ExecApprovalGetParams: ExecApprovalGetParamsSchema,
565566
ExecApprovalRequestParams: ExecApprovalRequestParamsSchema,
566567
ExecApprovalResolveParams: ExecApprovalResolveParamsSchema,
568+
PluginApprovalCancelParams: PluginApprovalCancelParamsSchema,
567569
PluginApprovalRequestParams: PluginApprovalRequestParamsSchema,
568570
PluginApprovalResolveParams: PluginApprovalResolveParamsSchema,
569571
PluginControlUiDescriptor: PluginControlUiDescriptorSchema,

‎packages/gateway-protocol/src/schema/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ export type ExecApprovalsSnapshot = SchemaType<"ExecApprovalsSnapshot">;
275275
export type ExecApprovalGetParams = SchemaType<"ExecApprovalGetParams">;
276276
export type ExecApprovalRequestParams = SchemaType<"ExecApprovalRequestParams">;
277277
export type ExecApprovalResolveParams = SchemaType<"ExecApprovalResolveParams">;
278+
export type PluginApprovalCancelParams = SchemaType<"PluginApprovalCancelParams">;
278279
export type PluginApprovalRequestParams = SchemaType<"PluginApprovalRequestParams">;
279280
export type PluginApprovalResolveParams = SchemaType<"PluginApprovalResolveParams">;
280281
export type DevicePairListParams = SchemaType<"DevicePairListParams">;

‎src/agents/agent-tools.before-tool-call.e2e.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,11 @@ describe("before_tool_call requireApproval handling", () => {
16111611

16121612
expect(result.blocked).toBe(true);
16131613
expect(result).toHaveProperty("reason", "Approval cancelled (run aborted)");
1614-
expect(mockCallGateway).toHaveBeenCalledTimes(2);
1614+
expect(mockCallGateway).toHaveBeenCalledTimes(3);
1615+
const cancelCall = requireGatewayCall(2);
1616+
expect(cancelCall[0]).toBe("plugin.approval.cancel");
1617+
expect(cancelCall[1]).toEqual({ timeoutMs: 5_000 });
1618+
expect(cancelCall[2]).toEqual({ id: "server-id-abort" });
16151619
});
16161620

16171621
it("classifies non-Error abort reasons as run abort cancellation", async () => {

‎src/agents/agent-tools.before-tool-call.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,20 @@ function notifyPluginApprovalResolution(
643643
}
644644
}
645645

646+
function cancelPluginToolApprovalRequest(id: string): void {
647+
void Promise.resolve(
648+
callGatewayTool(
649+
"plugin.approval.cancel",
650+
{ timeoutMs: 5_000 },
651+
{
652+
id,
653+
},
654+
),
655+
).catch((err: unknown) => {
656+
log.warn(`plugin approval cancel failed: ${String(err)}`);
657+
});
658+
}
659+
646660
async function requestPluginToolApproval(params: {
647661
approval: PluginApprovalRequest;
648662
toolName: string;
@@ -655,6 +669,7 @@ async function requestPluginToolApproval(params: {
655669
const approval = params.approval;
656670
const timeoutMs = resolvePluginToolApprovalTimeoutMs(approval);
657671
const gatewayTimeoutMs = resolvePluginToolApprovalGatewayTimeoutMs(timeoutMs);
672+
let approvalId: string | undefined;
658673
try {
659674
const requestResult: {
660675
id?: string;
@@ -695,6 +710,7 @@ async function requestPluginToolApproval(params: {
695710
params: params.baseParams,
696711
};
697712
}
713+
approvalId = id;
698714
const hasImmediateDecision = Object.hasOwn(requestResult ?? {}, "decision");
699715
let decision: string | null | undefined;
700716
if (hasImmediateDecision) {
@@ -795,6 +811,9 @@ async function requestPluginToolApproval(params: {
795811
(err instanceof Error &&
796812
(err.name === "AbortError" || ("cause" in err && err.cause === signal.reason))));
797813
if (abortCancelled) {
814+
if (approvalId) {
815+
cancelPluginToolApprovalRequest(approvalId);
816+
}
798817
log.warn(`plugin approval wait cancelled by run abort: ${String(err)}`);
799818
return {
800819
blocked: true,

‎src/agents/tools/gateway.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,18 @@ describe("gateway tool defaults", () => {
488488
expect(call.deviceIdentity).toEqual(mocks.deviceIdentity);
489489
});
490490

491+
it("marks local plugin approval cancel calls with runtime and device identity", async () => {
492+
mocks.callGateway.mockResolvedValueOnce({ ok: true });
493+
494+
await callGatewayTool("plugin.approval.cancel", {}, { id: "approval-id" });
495+
496+
const call = capturedGatewayCall();
497+
expect(call.method).toBe("plugin.approval.cancel");
498+
expect(call.scopes).toEqual(["operator.approvals"]);
499+
expect(call.approvalRuntimeToken).toEqual(expect.any(String));
500+
expect(call.deviceIdentity).toEqual(mocks.deviceIdentity);
501+
});
502+
491503
it("marks local plugin approval request calls with runtime and device identity", async () => {
492504
mocks.callGateway.mockResolvedValueOnce({ id: "plugin:approval-id" });
493505

0 commit comments

Comments
 (0)