File size: 5,371 Bytes
e249c6d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import { afterEach, describe, expect, it, vi } from "vitest";
import { createDeferred } from "../../../test/helpers/promise.js";
import { createReplyOperation } from "../../auto-reply/reply/reply-run-registry.operation.js";
import {
activateMcpLoopbackClientGrantCapture,
deactivateMcpLoopbackClientGrantCapture,
mintMcpLoopbackClientGrant,
resolveMcpLoopbackClientGrant,
revokeMcpLoopbackClientGrant,
transferMcpLoopbackClientGrant,
} from "../../gateway/mcp-grant-store.js";
import type { CliBackendLiveSessionHandle } from "../../plugins/cli-backend.types.js";
import { getAdmittedRunDelegatedAuthority } from "../admitted-run-context.js";
import {
closePluginTestAdmissions,
createExecution,
runPlugin,
SUCCESS_RESULT,
waitUntilAborted,
} from "./execute-plugin.test-support.js";
import { createCliToolTracking } from "./execute-tool-tracking.js";
vi.mock("../tools/gateway.js", () => ({ callGatewayTool: vi.fn() }));
const activeSessions = new Set<CliBackendLiveSessionHandle>();
afterEach(() => {
for (const session of activeSessions) {
session.close("restart");
}
activeSessions.clear();
closePluginTestAdmissions();
vi.restoreAllMocks();
vi.useRealTimers();
});
describe("CLI MCP capture authority", () => {
it.each([
{ name: "one-shot timeout", liveSession: false },
{ name: "live backend cancellation", liveSession: true },
])("revokes MCP authority before iterator cleanup after $name", async ({ liveSession }) => {
vi.useFakeTimers();
const source = new AbortController();
const { context } = await createExecution({ abortSignal: source.signal, timeoutMs: 100 });
const operation = createReplyOperation({
sessionKey: context.params.sessionKey!,
sessionId: context.params.sessionId,
resetTriggered: false,
});
context.params.replyOperation = operation;
const runtimeOwnerToken = `runtime-${context.params.runId}`;
const grant = mintMcpLoopbackClientGrant({
context: { sessionKey: context.params.sessionKey!, senderIsOwner: false },
runtimeOwnerToken,
admittedRunContext: context.params.admittedRunContext,
abortSignal: source.signal,
});
context.preparedBackend.mcpClientGrantCapture = {
transportToken: grant.token,
adoptProcessToken: (targetToken) => {
transferMcpLoopbackClientGrant({
sourceToken: grant.token,
targetToken,
runtimeOwnerToken,
});
},
revokeProcessToken: () => {
revokeMcpLoopbackClientGrant(grant.token);
},
activate: (captureKey, assertCurrent) => {
activateMcpLoopbackClientGrantCapture({
token: grant.token,
runtimeOwnerToken,
captureKey,
assertCurrent,
});
},
deactivate: (captureKey) => {
deactivateMcpLoopbackClientGrantCapture({
token: grant.token,
runtimeOwnerToken,
captureKey,
});
},
};
const tracking = createCliToolTracking(context);
const captureKey = `capture-${context.params.runId}`;
const capture = { token: grant.token, runtimeOwnerToken, captureKey };
const streamStarted = createDeferred();
const streamClosing = createDeferred();
const releaseCleanup = createDeferred();
const run = runPlugin(
context,
async function* (execution) {
if (liveSession) {
const capability = execution.liveSession;
if (!capability) {
throw new Error("expected live CLI session capability");
}
const handle: CliBackendLiveSessionHandle = {
generation: context.params.runId,
fingerprint: capability.fingerprint,
isIdle: () => true,
close: () => capability.remove(handle),
waitForExit: async () => {},
};
capability.register(handle);
activeSessions.add(handle);
capability.activate(handle);
}
const aborted = waitUntilAborted(execution);
streamStarted.resolve();
try {
await aborted;
yield SUCCESS_RESULT;
} finally {
streamClosing.resolve();
await releaseCleanup.promise;
}
},
{ liveSession, mcpCapture: { captureKey, beginCapture: tracking.beginGatewayCapture } },
);
const observedRun = run.catch((error: unknown) => error);
try {
await streamStarted.promise;
const retained = resolveMcpLoopbackClientGrant(capture);
expect(retained?.isCurrent()).toBe(true);
if (liveSession) {
expect(operation.abortByUser()).toBe(true);
} else {
await vi.advanceTimersByTimeAsync(100);
}
await streamClosing.promise;
expect(source.signal.aborted).toBe(false);
expect(getAdmittedRunDelegatedAuthority(context.params.admittedRunContext)).toBeDefined();
expect(retained?.isCurrent()).toBe(false);
expect(resolveMcpLoopbackClientGrant(capture)).toBeUndefined();
} finally {
releaseCleanup.resolve();
await observedRun;
tracking.finalizeCapture(() => {});
revokeMcpLoopbackClientGrant(grant.token);
operation.complete();
}
expect(await observedRun).toMatchObject(
liveSession ? { name: "AbortError" } : { reason: "overall-timeout", timedOut: true },
);
});
});
|