Skip to content

Commit 6484b41

Browse files
committed
Approvals: replay pending requests on startup
1 parent a00b01f commit 6484b41

10 files changed

Lines changed: 313 additions & 26 deletions

‎src/gateway/exec-approval-manager.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ export class ExecApprovalManager<TPayload = ExecApprovalRequestPayload> {
152152
return entry?.record ?? null;
153153
}
154154

155+
listPendingRecords(): ExecApprovalRecord<TPayload>[] {
156+
return Array.from(this.pending.values())
157+
.map((entry) => entry.record)
158+
.filter((record) => record.resolvedAtMs === undefined);
159+
}
160+
155161
consumeAllowOnce(recordId: string): boolean {
156162
const entry = this.pending.get(recordId);
157163
if (!entry) {

‎src/gateway/method-scopes.test.ts‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe("operator scope authorization", () => {
9797
});
9898
});
9999

100-
it.each(["exec.approval.get", "exec.approval.resolve"])(
100+
it.each(["exec.approval.get", "exec.approval.list", "exec.approval.resolve"])(
101101
"requires approvals scope for %s",
102102
(method) => {
103103
expect(authorizeOperatorScopesForMethod(method, ["operator.write"])).toEqual({
@@ -110,18 +110,20 @@ describe("operator scope authorization", () => {
110110
},
111111
);
112112

113-
it.each(["plugin.approval.request", "plugin.approval.waitDecision", "plugin.approval.resolve"])(
114-
"requires approvals scope for %s",
115-
(method) => {
116-
expect(authorizeOperatorScopesForMethod(method, ["operator.write"])).toEqual({
117-
allowed: false,
118-
missingScope: "operator.approvals",
119-
});
120-
expect(authorizeOperatorScopesForMethod(method, ["operator.approvals"])).toEqual({
121-
allowed: true,
122-
});
123-
},
124-
);
113+
it.each([
114+
"plugin.approval.list",
115+
"plugin.approval.request",
116+
"plugin.approval.waitDecision",
117+
"plugin.approval.resolve",
118+
])("requires approvals scope for %s", (method) => {
119+
expect(authorizeOperatorScopesForMethod(method, ["operator.write"])).toEqual({
120+
allowed: false,
121+
missingScope: "operator.approvals",
122+
});
123+
expect(authorizeOperatorScopesForMethod(method, ["operator.approvals"])).toEqual({
124+
allowed: true,
125+
});
126+
});
125127

126128
it("requires admin for unknown methods", () => {
127129
expect(authorizeOperatorScopesForMethod("unknown.method", ["operator.read"])).toEqual({
@@ -145,12 +147,14 @@ describe("operator scope authorization", () => {
145147
describe("plugin approval method registration", () => {
146148
it("lists all plugin approval methods", () => {
147149
const methods = listGatewayMethods();
150+
expect(methods).toContain("plugin.approval.list");
148151
expect(methods).toContain("plugin.approval.request");
149152
expect(methods).toContain("plugin.approval.waitDecision");
150153
expect(methods).toContain("plugin.approval.resolve");
151154
});
152155

153156
it("classifies plugin approval methods", () => {
157+
expect(isGatewayMethodClassified("plugin.approval.list")).toBe(true);
154158
expect(isGatewayMethodClassified("plugin.approval.request")).toBe(true);
155159
expect(isGatewayMethodClassified("plugin.approval.waitDecision")).toBe(true);
156160
expect(isGatewayMethodClassified("plugin.approval.resolve")).toBe(true);

‎src/gateway/method-scopes.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ const NODE_ROLE_METHODS = new Set([
4242
const METHOD_SCOPE_GROUPS: Record<OperatorScope, readonly string[]> = {
4343
[APPROVALS_SCOPE]: [
4444
"exec.approval.get",
45+
"exec.approval.list",
4546
"exec.approval.request",
4647
"exec.approval.waitDecision",
4748
"exec.approval.resolve",
49+
"plugin.approval.list",
4850
"plugin.approval.request",
4951
"plugin.approval.waitDecision",
5052
"plugin.approval.resolve",

‎src/gateway/server-methods-list.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ const BASE_METHODS = [
2828
"exec.approvals.node.get",
2929
"exec.approvals.node.set",
3030
"exec.approval.get",
31+
"exec.approval.list",
3132
"exec.approval.request",
3233
"exec.approval.waitDecision",
3334
"exec.approval.resolve",
35+
"plugin.approval.list",
3436
"plugin.approval.request",
3537
"plugin.approval.waitDecision",
3638
"plugin.approval.resolve",

‎src/gateway/server-methods/exec-approval.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ export function createExecApprovalHandlers(
9393
undefined,
9494
);
9595
},
96+
"exec.approval.list": async ({ respond }) => {
97+
respond(
98+
true,
99+
manager.listPendingRecords().map((record) => ({
100+
id: record.id,
101+
request: record.request,
102+
createdAtMs: record.createdAtMs,
103+
expiresAtMs: record.expiresAtMs,
104+
})),
105+
undefined,
106+
);
107+
},
96108
"exec.approval.request": async ({ params, respond, context, client }) => {
97109
if (!validateExecApprovalRequestParams(params)) {
98110
respond(

‎src/gateway/server-methods/plugin-approval.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,56 @@ describe("createPluginApprovalHandlers", () => {
324324
});
325325
});
326326

327+
describe("plugin.approval.list", () => {
328+
it("lists pending plugin approvals", async () => {
329+
const handlers = createPluginApprovalHandlers(manager);
330+
const respond = vi.fn();
331+
const requestOpts = createMockOptions(
332+
"plugin.approval.request",
333+
{
334+
title: "Sensitive action",
335+
description: "Desc",
336+
twoPhase: true,
337+
},
338+
{ respond },
339+
);
340+
341+
const handlerPromise = handlers["plugin.approval.request"](requestOpts);
342+
await vi.waitFor(() => {
343+
expect(respond).toHaveBeenCalledWith(
344+
true,
345+
expect.objectContaining({ status: "accepted", id: expect.any(String) }),
346+
undefined,
347+
);
348+
});
349+
350+
const listRespond = vi.fn();
351+
await handlers["plugin.approval.list"](
352+
createMockOptions("plugin.approval.list", {}, { respond: listRespond }),
353+
);
354+
expect(listRespond).toHaveBeenCalledWith(
355+
true,
356+
expect.arrayContaining([
357+
expect.objectContaining({
358+
id: expect.stringMatching(/^plugin:/),
359+
request: expect.objectContaining({
360+
title: "Sensitive action",
361+
description: "Desc",
362+
}),
363+
}),
364+
]),
365+
undefined,
366+
);
367+
368+
const acceptedCall = respond.mock.calls.find(
369+
(c) => (c[1] as Record<string, unknown>)?.status === "accepted",
370+
);
371+
const approvalId = (acceptedCall?.[1] as Record<string, unknown>)?.id as string;
372+
manager.resolve(approvalId, "allow-once");
373+
await handlerPromise;
374+
});
375+
});
376+
327377
describe("plugin.approval.waitDecision", () => {
328378
it("rejects missing id", async () => {
329379
const handlers = createPluginApprovalHandlers(manager);

‎src/gateway/server-methods/plugin-approval.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ export function createPluginApprovalHandlers(
2727
opts?: { forwarder?: ExecApprovalForwarder },
2828
): GatewayRequestHandlers {
2929
return {
30+
"plugin.approval.list": async ({ respond }) => {
31+
respond(
32+
true,
33+
manager.listPendingRecords().map((record) => ({
34+
id: record.id,
35+
request: record.request,
36+
createdAtMs: record.createdAtMs,
37+
expiresAtMs: record.expiresAtMs,
38+
})),
39+
undefined,
40+
);
41+
},
3042
"plugin.approval.request": async ({ params, client, respond, context }) => {
3143
if (!validatePluginApprovalRequestParams(params)) {
3244
respond(

‎src/gateway/server-methods/server-methods.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,20 @@ describe("exec approval handlers", () => {
377377
});
378378
}
379379

380+
async function listExecApprovals(params: {
381+
handlers: ExecApprovalHandlers;
382+
respond: ReturnType<typeof vi.fn>;
383+
}) {
384+
return params.handlers["exec.approval.list"]({
385+
params: {} as never,
386+
respond: params.respond as never,
387+
context: {} as never,
388+
client: null,
389+
req: { id: "req-list", type: "req", method: "exec.approval.list" },
390+
isWebchatConnect: execApprovalNoop,
391+
});
392+
}
393+
380394
async function requestExecApproval(params: {
381395
handlers: ExecApprovalHandlers;
382396
respond: ReturnType<typeof vi.fn>;
@@ -607,6 +621,47 @@ describe("exec approval handlers", () => {
607621
await requestPromise;
608622
});
609623

624+
it("lists pending exec approvals", async () => {
625+
const { handlers, respond, context } = createExecApprovalFixture();
626+
const requestPromise = requestExecApproval({
627+
handlers,
628+
respond,
629+
context,
630+
params: {
631+
id: "approval-list-1",
632+
twoPhase: true,
633+
host: "gateway",
634+
systemRunPlan: undefined,
635+
nodeId: undefined,
636+
},
637+
});
638+
639+
const listRespond = vi.fn();
640+
await listExecApprovals({ handlers, respond: listRespond });
641+
642+
expect(listRespond).toHaveBeenCalledWith(
643+
true,
644+
expect.arrayContaining([
645+
expect.objectContaining({
646+
id: "approval-list-1",
647+
request: expect.objectContaining({
648+
command: "echo ok",
649+
}),
650+
}),
651+
]),
652+
undefined,
653+
);
654+
655+
const resolveRespond = vi.fn();
656+
await resolveExecApproval({
657+
handlers,
658+
id: "approval-list-1",
659+
respond: resolveRespond,
660+
context,
661+
});
662+
await requestPromise;
663+
});
664+
610665
it("returns not found for stale exec.approval.get ids", async () => {
611666
const { handlers, respond, context } = createExecApprovalFixture();
612667

0 commit comments

Comments
 (0)