perf(secrets): fast-path bundled channel contract loads

This commit is contained in:
Vincent Koc
2026-04-07 11:33:57 +01:00
parent cd54f20fe2
commit 9a4e35a24f
2 changed files with 86 additions and 22 deletions

View File

@@ -0,0 +1,50 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const { loadPluginManifestRegistryMock } = vi.hoisted(() => ({
loadPluginManifestRegistryMock: vi.fn(() => {
throw new Error("manifest registry should stay off the explicit bundled channel fast path");
}),
}));
vi.mock("../plugins/manifest-registry.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../plugins/manifest-registry.js")>();
return {
...actual,
loadPluginManifestRegistry: loadPluginManifestRegistryMock,
};
});
import {
loadBundledChannelSecretContractApi,
loadBundledChannelSecurityContractApi,
} from "./channel-contract-api.js";
describe("channel contract api explicit fast path", () => {
beforeEach(() => {
loadPluginManifestRegistryMock.mockClear();
});
it("resolves bundled channel secret contracts by explicit channel id without manifest scans", () => {
const api = loadBundledChannelSecretContractApi("bluebubbles");
expect(api?.collectRuntimeConfigAssignments).toBeTypeOf("function");
expect(api?.secretTargetRegistryEntries).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: "channels.bluebubbles.accounts.*.password",
}),
]),
);
expect(loadPluginManifestRegistryMock).not.toHaveBeenCalled();
});
it("resolves bundled channel security contracts by explicit channel id without manifest scans", () => {
const api = loadBundledChannelSecurityContractApi("whatsapp");
expect(api?.unsupportedSecretRefSurfacePatterns).toEqual(
expect.arrayContaining(["channels.whatsapp.creds.json"]),
);
expect(api?.collectUnsupportedSecretRefConfigCandidates).toBeTypeOf("function");
expect(loadPluginManifestRegistryMock).not.toHaveBeenCalled();
});
});

View File

@@ -50,31 +50,45 @@ function loadBundledChannelPublicArtifact(
channelId: string,
artifactBasenames: readonly string[],
): BundledChannelContractApi | undefined {
const dirName = getBundledChannelDirName(channelId);
if (!dirName) {
return undefined;
}
const triedDirNames = new Set<string>();
const tryDirName = (dirName: string | undefined): BundledChannelContractApi | undefined => {
if (typeof dirName !== "string" || dirName.trim().length === 0 || triedDirNames.has(dirName)) {
return undefined;
}
triedDirNames.add(dirName);
for (const artifactBasename of artifactBasenames) {
try {
return loadBundledPluginPublicArtifactModuleSync<BundledChannelContractApi>({
dirName,
artifactBasename,
});
} catch (error) {
if (
error instanceof Error &&
error.message.startsWith("Unable to resolve bundled plugin public surface ")
) {
continue;
}
if (process.env.OPENCLAW_DEBUG_CHANNEL_CONTRACT_API === "1") {
const detail = formatErrorMessage(error);
process.stderr.write(
`[channel-contract-api] failed to load ${channelId} via ${dirName}/${artifactBasename}: ${detail}\n`,
);
for (const artifactBasename of artifactBasenames) {
try {
return loadBundledPluginPublicArtifactModuleSync<BundledChannelContractApi>({
dirName,
artifactBasename,
});
} catch (error) {
if (
error instanceof Error &&
error.message.startsWith("Unable to resolve bundled plugin public surface ")
) {
continue;
}
if (process.env.OPENCLAW_DEBUG_CHANNEL_CONTRACT_API === "1") {
const detail = formatErrorMessage(error);
process.stderr.write(
`[channel-contract-api] failed to load ${channelId} via ${dirName}/${artifactBasename}: ${detail}\n`,
);
}
}
}
return undefined;
};
const direct = tryDirName(channelId);
if (direct) {
return direct;
}
const fallback = tryDirName(getBundledChannelDirName(channelId));
if (fallback) {
return fallback;
}
return undefined;