Skip to content

Commit b78202d

Browse files
committed
fix(exec): harden stale/replay/live requests
1 parent e418a6d commit b78202d

3 files changed

Lines changed: 95 additions & 8 deletions

File tree

‎docs/gateway/protocol.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,18 @@ implemented in `src/gateway/server-methods/*.ts`.
381381

382382
#### Approval families
383383

384-
- `exec.approval.request` and `exec.approval.resolve` cover one-shot exec
385-
approval requests.
384+
- `exec.approval.request`, `exec.approval.get`, `exec.approval.list`, and
385+
`exec.approval.resolve` cover one-shot exec approval requests plus pending
386+
approval lookup/replay.
386387
- `exec.approval.waitDecision` waits on one pending exec approval and returns
387388
the final decision (or `null` on timeout).
388389
- `exec.approvals.get` and `exec.approvals.set` manage gateway exec approval
389390
policy snapshots.
390391
- `exec.approvals.node.get` and `exec.approvals.node.set` manage node-local exec
391392
approval policy via node relay commands.
392-
- `plugin.approval.request`, `plugin.approval.waitDecision`, and
393-
`plugin.approval.resolve` cover plugin-defined approval flows.
393+
- `plugin.approval.request`, `plugin.approval.list`,
394+
`plugin.approval.waitDecision`, and `plugin.approval.resolve` cover
395+
plugin-defined approval flows.
394396

395397
#### Other major families
396398

‎src/infra/exec-approval-channel-runtime.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,56 @@ describe("createExecApprovalChannelRuntime", () => {
491491
expect(deliverRequested).toHaveBeenCalledTimes(1);
492492
});
493493

494+
it("does not replay approvals after stop wins once hello is already complete", async () => {
495+
const replayDeferred = createDeferred<
496+
Array<{
497+
id: string;
498+
request: { command: string };
499+
createdAtMs: number;
500+
expiresAtMs: number;
501+
}>
502+
>();
503+
mockGatewayClientRequests.mockImplementation(async (method) => {
504+
if (method === "exec.approval.list") {
505+
return replayDeferred.promise;
506+
}
507+
return { ok: true };
508+
});
509+
const deliverRequested = vi.fn(async (request) => [{ id: request.id }]);
510+
const runtime = createExecApprovalChannelRuntime({
511+
label: "test/replay-stop-after-ready",
512+
clientDisplayName: "Test Replay Stop",
513+
cfg: {} as never,
514+
isConfigured: () => true,
515+
shouldHandle: () => true,
516+
deliverRequested,
517+
finalizeResolved: async () => undefined,
518+
});
519+
520+
const startPromise = runtime.start();
521+
await vi.waitFor(() => {
522+
expect(mockGatewayClientRequests).toHaveBeenCalledWith("exec.approval.list", {});
523+
});
524+
525+
const stopPromise = runtime.stop();
526+
replayDeferred.resolve([
527+
{
528+
id: "abc",
529+
request: {
530+
command: "echo abc",
531+
},
532+
createdAtMs: 1000,
533+
expiresAtMs: 2000,
534+
},
535+
]);
536+
537+
await startPromise;
538+
await stopPromise;
539+
540+
expect(deliverRequested).not.toHaveBeenCalled();
541+
expect(mockGatewayClientStops).toHaveBeenCalled();
542+
});
543+
494544
it("clears pending state when delivery throws", async () => {
495545
const deliverRequested = vi
496546
.fn<() => Promise<Array<{ id: string }>>>()

‎src/infra/exec-approval-channel-runtime.ts‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,24 @@ export function createExecApprovalChannelRuntime<
8989
let shouldRun = false;
9090
let startPromise: Promise<void> | null = null;
9191

92+
const shouldKeepRunning = (): boolean => shouldRun;
93+
9294
const spawn = (label: string, promise: Promise<void>): void => {
9395
void promise.catch((err: unknown) => {
9496
const message = formatErrorMessage(err);
9597
log.error(`${label}: ${message}`);
9698
});
9799
};
98100

101+
const stopClientIfInactive = (client: GatewayClient): boolean => {
102+
if (shouldKeepRunning()) {
103+
return false;
104+
}
105+
gatewayClient = null;
106+
client.stop();
107+
return true;
108+
};
109+
99110
const clearPendingEntry = (
100111
approvalId: string,
101112
): PendingApprovalEntry<TPending, TRequest, TResolved> | null => {
@@ -122,7 +133,13 @@ export function createExecApprovalChannelRuntime<
122133
});
123134
};
124135

125-
const handleRequested = async (request: TRequest): Promise<void> => {
136+
const handleRequested = async (
137+
request: TRequest,
138+
opts?: { ignoreIfInactive?: boolean },
139+
): Promise<void> => {
140+
if (opts?.ignoreIfInactive && !shouldKeepRunning()) {
141+
return;
142+
}
126143
if (!adapter.shouldHandle(request)) {
127144
return;
128145
}
@@ -202,11 +219,17 @@ export function createExecApprovalChannelRuntime<
202219

203220
const handleGatewayEvent = (evt: EventFrame): void => {
204221
if (evt.event === "exec.approval.requested" && eventKinds.has("exec")) {
205-
spawn("error handling approval request", handleRequested(evt.payload as TRequest));
222+
spawn(
223+
"error handling approval request",
224+
handleRequested(evt.payload as TRequest, { ignoreIfInactive: true }),
225+
);
206226
return;
207227
}
208228
if (evt.event === "plugin.approval.requested" && eventKinds.has("plugin")) {
209-
spawn("error handling approval request", handleRequested(evt.payload as TRequest));
229+
spawn(
230+
"error handling approval request",
231+
handleRequested(evt.payload as TRequest, { ignoreIfInactive: true }),
232+
);
210233
return;
211234
}
212235
if (evt.event === "exec.approval.resolved" && eventKinds.has("exec")) {
@@ -278,10 +301,22 @@ export function createExecApprovalChannelRuntime<
278301
try {
279302
client.start();
280303
await ready;
304+
if (stopClientIfInactive(client)) {
305+
return;
306+
}
281307
for (const method of resolveApprovalReplayMethods(eventKinds)) {
308+
if (stopClientIfInactive(client)) {
309+
return;
310+
}
282311
const pendingRequests = await client.request<Array<TRequest>>(method, {});
312+
if (stopClientIfInactive(client)) {
313+
return;
314+
}
283315
for (const request of pendingRequests) {
284-
await handleRequested(request);
316+
if (stopClientIfInactive(client)) {
317+
return;
318+
}
319+
await handleRequested(request, { ignoreIfInactive: true });
285320
}
286321
}
287322
started = true;

0 commit comments

Comments
 (0)