Build: mirror Matrix crypto WASM dependency

This commit is contained in:
Gustavo Madeira Santana
2026-03-29 14:46:23 -04:00
parent 1769d76e81
commit 0e6424cf1d
12 changed files with 278 additions and 101 deletions

View File

@@ -5,6 +5,7 @@
"type": "module",
"dependencies": {
"@matrix-org/matrix-sdk-crypto-nodejs": "^0.4.0",
"@matrix-org/matrix-sdk-crypto-wasm": "18.0.0",
"fake-indexeddb": "^6.2.5",
"markdown-it": "14.1.1",
"matrix-js-sdk": "41.2.0",
@@ -44,9 +45,9 @@
},
"releaseChecks": {
"rootDependencyMirrorAllowlist": [
"@matrix-org/matrix-sdk-crypto-wasm",
"@matrix-org/matrix-sdk-crypto-nodejs",
"matrix-js-sdk",
"music-metadata"
"matrix-js-sdk"
]
}
}

View File

@@ -5,7 +5,11 @@ import path from "node:path";
import { fileURLToPath } from "node:url";
import type { RuntimeEnv } from "../runtime-api.js";
const REQUIRED_MATRIX_PACKAGES = ["matrix-js-sdk", "@matrix-org/matrix-sdk-crypto-nodejs"];
const REQUIRED_MATRIX_PACKAGES = [
"matrix-js-sdk",
"@matrix-org/matrix-sdk-crypto-nodejs",
"@matrix-org/matrix-sdk-crypto-wasm",
];
type MatrixCryptoRuntimeDeps = {
requireFn?: (id: string) => unknown;
@@ -184,11 +188,11 @@ export async function ensureMatrixSdkInstalled(params: {
const confirm = params.confirm;
if (confirm) {
const ok = await confirm(
"Matrix requires matrix-js-sdk and @matrix-org/matrix-sdk-crypto-nodejs. Install now?",
"Matrix requires matrix-js-sdk, @matrix-org/matrix-sdk-crypto-nodejs, and @matrix-org/matrix-sdk-crypto-wasm. Install now?",
);
if (!ok) {
throw new Error(
"Matrix requires matrix-js-sdk and @matrix-org/matrix-sdk-crypto-nodejs (install dependencies first).",
"Matrix requires matrix-js-sdk, @matrix-org/matrix-sdk-crypto-nodejs, and @matrix-org/matrix-sdk-crypto-wasm (install dependencies first).",
);
}
}

View File

@@ -566,7 +566,7 @@ export const matrixOnboardingAdapter: MatrixOnboardingAdapter = {
channel,
configured: false,
statusLines: ['Matrix: set "channels.matrix.defaultAccount" to select a named account'],
selectionHint: !sdkReady ? "install matrix-js-sdk" : "set defaultAccount",
selectionHint: !sdkReady ? "install Matrix deps" : "set defaultAccount",
};
}
const account = resolveMatrixAccount({
@@ -580,7 +580,7 @@ export const matrixOnboardingAdapter: MatrixOnboardingAdapter = {
statusLines: [
`Matrix: ${configured ? "configured" : "needs homeserver + access token or password"}`,
],
selectionHint: !sdkReady ? "install matrix-js-sdk" : configured ? "configured" : "needs auth",
selectionHint: !sdkReady ? "install Matrix deps" : configured ? "configured" : "needs auth",
};
},
configure: async ({

View File

@@ -1183,6 +1183,7 @@
"@mariozechner/pi-ai": "0.63.2",
"@mariozechner/pi-coding-agent": "0.63.2",
"@mariozechner/pi-tui": "0.63.2",
"@matrix-org/matrix-sdk-crypto-wasm": "18.0.0",
"@modelcontextprotocol/sdk": "1.28.0",
"@mozilla/readability": "^0.6.0",
"@openclaw/plugin-package-contract": "workspace:*",

16
pnpm-lock.yaml generated
View File

@@ -60,6 +60,9 @@ importers:
'@mariozechner/pi-tui':
specifier: 0.63.2
version: 0.63.2
'@matrix-org/matrix-sdk-crypto-wasm':
specifier: 18.0.0
version: 18.0.0
'@modelcontextprotocol/sdk':
specifier: 1.28.0
version: 1.28.0(zod@4.3.6)
@@ -259,6 +262,8 @@ importers:
extensions/anthropic: {}
extensions/anthropic-vertex: {}
extensions/bluebubbles:
devDependencies:
openclaw:
@@ -436,6 +441,9 @@ importers:
'@matrix-org/matrix-sdk-crypto-nodejs':
specifier: ^0.4.0
version: 0.4.0
'@matrix-org/matrix-sdk-crypto-wasm':
specifier: 18.0.0
version: 18.0.0
fake-indexeddb:
specifier: ^6.2.5
version: 6.2.5
@@ -3568,8 +3576,8 @@ packages:
link-preview-js:
optional: true
'@whiskeysockets/libsignal-node@https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67':
resolution: {tarball: https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67}
'@whiskeysockets/libsignal-node@git+https://git@github.com:whiskeysockets/libsignal-node.git#1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67':
resolution: {commit: 1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67, repo: git@github.com:whiskeysockets/libsignal-node.git, type: git}
version: 2.0.1
abbrev@1.1.1:
@@ -9972,7 +9980,7 @@ snapshots:
'@cacheable/node-cache': 1.7.6
'@hapi/boom': 9.1.4
async-mutex: 0.5.0
libsignal: '@whiskeysockets/libsignal-node@https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67'
libsignal: '@whiskeysockets/libsignal-node@git+https://git@github.com:whiskeysockets/libsignal-node.git#1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67'
lru-cache: 11.2.6
music-metadata: 11.12.3
p-queue: 9.1.0
@@ -9988,7 +9996,7 @@ snapshots:
- supports-color
- utf-8-validate
'@whiskeysockets/libsignal-node@https://codeload.github.com/whiskeysockets/libsignal-node/tar.gz/1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67':
'@whiskeysockets/libsignal-node@git+https://git@github.com:whiskeysockets/libsignal-node.git#1c30d7d7e76a3b0aa120b04dc6a26f5a12dccf67':
dependencies:
curve25519-js: 0.0.4
protobufjs: 6.8.8

View File

@@ -1,34 +0,0 @@
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { removePathIfExists } from "./runtime-postbuild-shared.mjs";
/**
* @param {{
* cwd?: string;
* repoRoot?: string;
* }} [params]
*/
export function copyMatrixCryptoWasmPkg(params = {}) {
const repoRoot = params.cwd ?? params.repoRoot ?? process.cwd();
const sourcePkgDir = path.join(
repoRoot,
"node_modules",
"@matrix-org",
"matrix-sdk-crypto-wasm",
"pkg",
);
const targetPkgDir = path.join(repoRoot, "dist", "pkg");
removePathIfExists(targetPkgDir);
if (!fs.existsSync(sourcePkgDir)) {
return;
}
fs.mkdirSync(path.dirname(targetPkgDir), { recursive: true });
fs.cpSync(sourcePkgDir, targetPkgDir, { force: true, recursive: true });
}
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
copyMatrixCryptoWasmPkg();
}

View File

@@ -11,6 +11,7 @@ export type ExtensionPackageJson = {
optionalDependencies?: Record<string, string>;
openclaw?: {
install?: unknown;
releaseChecks?: unknown;
};
};

View File

@@ -28,7 +28,6 @@ const requiredPathGroups = [
"dist/build-info.json",
"dist/channel-catalog.json",
"dist/control-ui/index.html",
"dist/pkg/matrix_sdk_crypto_wasm_bg.wasm",
];
const forbiddenPrefixes = ["dist-runtime/", "dist/OpenClaw.app/"];
// 2026.3.12 ballooned to ~213.6 MiB unpacked and correlated with low-memory
@@ -64,15 +63,78 @@ function collectBundledExtensions(): BundledExtension[] {
function checkBundledExtensionMetadata() {
const extensions = collectBundledExtensions();
const manifestErrors = collectBundledExtensionManifestErrors(extensions);
if (manifestErrors.length > 0) {
const rootPackage = JSON.parse(readFileSync(resolve("package.json"), "utf8")) as {
dependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
};
const rootRuntimeDeps = new Set([
...Object.keys(rootPackage.dependencies ?? {}),
...Object.keys(rootPackage.optionalDependencies ?? {}),
]);
const rootMirrorErrors = collectBundledExtensionRootDependencyMirrorErrors(
extensions,
rootRuntimeDeps,
);
const errors = [...manifestErrors, ...rootMirrorErrors];
if (errors.length > 0) {
console.error("release-check: bundled extension manifest validation failed:");
for (const error of manifestErrors) {
for (const error of errors) {
console.error(` - ${error}`);
}
process.exit(1);
}
}
export function collectBundledExtensionRootDependencyMirrorErrors(
extensions: BundledExtension[],
rootRuntimeDeps: ReadonlySet<string>,
): string[] {
const errors: string[] = [];
for (const extension of extensions) {
const rawReleaseChecks = extension.packageJson.openclaw?.releaseChecks;
const allowlist = (rawReleaseChecks as { rootDependencyMirrorAllowlist?: unknown } | undefined)
?.rootDependencyMirrorAllowlist;
if (allowlist === undefined) {
continue;
}
if (!Array.isArray(allowlist)) {
errors.push(
`bundled extension '${extension.id}' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist must be an array`,
);
continue;
}
const extensionRuntimeDeps = new Set([
...Object.keys(extension.packageJson.dependencies ?? {}),
...Object.keys(extension.packageJson.optionalDependencies ?? {}),
]);
for (const entry of allowlist) {
if (typeof entry !== "string" || entry.trim().length === 0) {
errors.push(
`bundled extension '${extension.id}' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist entries must be non-empty strings`,
);
continue;
}
if (!extensionRuntimeDeps.has(entry)) {
errors.push(
`bundled extension '${extension.id}' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist entry '${entry}' must be declared in extension runtime dependencies`,
);
}
if (!rootRuntimeDeps.has(entry)) {
errors.push(
`bundled extension '${extension.id}' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist entry '${entry}' must be mirrored in root runtime dependencies`,
);
}
}
}
return errors;
}
function runPackDry(): PackResult[] {
const raw = execSync("npm pack --dry-run --json --ignore-scripts", {
encoding: "utf8",

View File

@@ -1,6 +1,5 @@
import { pathToFileURL } from "node:url";
import { copyBundledPluginMetadata } from "./copy-bundled-plugin-metadata.mjs";
import { copyMatrixCryptoWasmPkg } from "./copy-matrix-crypto-wasm-pkg.mjs";
import { copyPluginSdkRootAlias } from "./copy-plugin-sdk-root-alias.mjs";
import { stageBundledPluginRuntimeDeps } from "./stage-bundled-plugin-runtime-deps.mjs";
import { stageBundledPluginRuntime } from "./stage-bundled-plugin-runtime.mjs";
@@ -9,7 +8,6 @@ import { writeOfficialChannelCatalog } from "./write-official-channel-catalog.mj
export function runRuntimePostBuild(params = {}) {
copyPluginSdkRootAlias(params);
copyBundledPluginMetadata(params);
copyMatrixCryptoWasmPkg(params);
writeOfficialChannelCatalog(params);
stageBundledPluginRuntimeDeps(params);
stageBundledPluginRuntime(params);

View File

@@ -1,6 +1,9 @@
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import os from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { describe, expect, it } from "vitest";
import { pluginSdkEntrypoints } from "./entrypoints.js";
@@ -56,6 +59,32 @@ function readRootPackageJson(): {
};
}
function createRootPackageRequire() {
return createRequire(pathToFileURL(resolve(REPO_ROOT, "package.json")).href);
}
function resolvePackageManagerCommand(name: "npm" | "pnpm"): string {
return process.platform === "win32" ? `${name}.cmd` : name;
}
function packOpenClawToTempDir(packDir: string): string {
const raw = execFileSync(
resolvePackageManagerCommand("npm"),
["pack", "--ignore-scripts", "--json", "--pack-destination", packDir],
{
cwd: REPO_ROOT,
encoding: "utf8",
env: { ...process.env, COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
},
);
const parsed = JSON.parse(raw) as Array<{ filename?: string }>;
const filename = parsed[0]?.filename?.trim();
if (!filename) {
throw new Error(`npm pack did not return a filename: ${raw}`);
}
return join(packDir, filename);
}
function readGeneratedFacadeTypeMap(): string {
return readFileSync(
resolve(REPO_ROOT, "src/generated/plugin-sdk-facade-type-map.generated.ts"),
@@ -99,10 +128,67 @@ describe("plugin-sdk package contract guardrails", () => {
it("mirrors matrix runtime deps needed by the bundled host graph", () => {
const { dependencies = {}, optionalDependencies = {} } = readRootPackageJson();
expect(dependencies["@matrix-org/matrix-sdk-crypto-wasm"]).toBe("18.0.0");
expect(dependencies["matrix-js-sdk"]).toBe("41.2.0");
expect(optionalDependencies["@matrix-org/matrix-sdk-crypto-nodejs"]).toBe("^0.4.0");
});
it("resolves matrix crypto WASM from the root runtime surface", () => {
const rootRequire = createRootPackageRequire();
expect(rootRequire.resolve("@matrix-org/matrix-sdk-crypto-wasm")).toContain(
"@matrix-org/matrix-sdk-crypto-wasm",
);
});
it("resolves matrix crypto WASM from an installed packed artifact", () => {
const tempRoot = mkdtempSync(join(os.tmpdir(), "openclaw-matrix-wasm-pack-"));
try {
const packDir = join(tempRoot, "pack");
const consumerDir = join(tempRoot, "consumer");
mkdirSync(packDir, { recursive: true });
mkdirSync(consumerDir, { recursive: true });
writeFileSync(
join(consumerDir, "package.json"),
`${JSON.stringify({ name: "matrix-wasm-smoke", private: true }, null, 2)}\n`,
"utf8",
);
const archivePath = packOpenClawToTempDir(packDir);
execFileSync(
resolvePackageManagerCommand("pnpm"),
["add", "--offline", "--ignore-scripts", archivePath],
{
cwd: consumerDir,
encoding: "utf8",
env: { ...process.env, COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
stdio: ["ignore", "pipe", "pipe"],
},
);
const installedPackageJsonPath = join(
consumerDir,
"node_modules",
"openclaw",
"package.json",
);
const installedPackageJson = JSON.parse(readFileSync(installedPackageJsonPath, "utf8")) as {
dependencies?: Record<string, string>;
};
const installedRequire = createRequire(pathToFileURL(installedPackageJsonPath).href);
expect(installedPackageJson.dependencies?.["@matrix-org/matrix-sdk-crypto-wasm"]).toBe(
"18.0.0",
);
expect(installedRequire.resolve("@matrix-org/matrix-sdk-crypto-wasm")).toContain(
"@matrix-org/matrix-sdk-crypto-wasm",
);
} finally {
rmSync(tempRoot, { recursive: true, force: true });
}
});
it("keeps generated facade types on package-style module specifiers", () => {
expect(readGeneratedFacadeTypeMap()).not.toContain("../../extensions/");
expect(readGeneratedFacadeTypeMap()).not.toContain(buildLegacyPluginSourceAlias());

View File

@@ -4,6 +4,7 @@ import { listPluginSdkDistArtifacts } from "../scripts/lib/plugin-sdk-entries.mj
import {
collectAppcastSparkleVersionErrors,
collectBundledExtensionManifestErrors,
collectBundledExtensionRootDependencyMirrorErrors,
collectForbiddenPackPaths,
collectMissingPackPaths,
collectPackUnpackedSizeErrors,
@@ -20,7 +21,6 @@ function makePackResult(filename: string, unpackedSize: number) {
const requiredPluginSdkPackPaths = [...listPluginSdkDistArtifacts(), "dist/plugin-sdk/compat.js"];
const requiredBundledPluginPackPaths = listBundledPluginPackArtifacts();
const requiredMatrixCryptoWasmPackPaths = ["dist/pkg/matrix_sdk_crypto_wasm_bg.wasm"];
describe("collectAppcastSparkleVersionErrors", () => {
it("accepts legacy 9-digit calver builds before lane-floor cutover", () => {
@@ -110,6 +110,103 @@ describe("collectBundledExtensionManifestErrors", () => {
});
});
describe("collectBundledExtensionRootDependencyMirrorErrors", () => {
it("flags a non-array mirror allowlist", () => {
expect(
collectBundledExtensionRootDependencyMirrorErrors(
[
{
id: "matrix",
packageJson: {
openclaw: {
releaseChecks: {
rootDependencyMirrorAllowlist: true,
},
},
},
},
],
new Set(),
),
).toEqual([
"bundled extension 'matrix' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist must be an array",
]);
});
it("flags mirror entries missing from extension runtime dependencies", () => {
expect(
collectBundledExtensionRootDependencyMirrorErrors(
[
{
id: "matrix",
packageJson: {
dependencies: {
"matrix-js-sdk": "41.2.0",
},
openclaw: {
releaseChecks: {
rootDependencyMirrorAllowlist: ["@matrix-org/matrix-sdk-crypto-wasm"],
},
},
},
},
],
new Set(["@matrix-org/matrix-sdk-crypto-wasm"]),
),
).toEqual([
"bundled extension 'matrix' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist entry '@matrix-org/matrix-sdk-crypto-wasm' must be declared in extension runtime dependencies",
]);
});
it("flags mirror entries missing from root runtime dependencies", () => {
expect(
collectBundledExtensionRootDependencyMirrorErrors(
[
{
id: "matrix",
packageJson: {
dependencies: {
"@matrix-org/matrix-sdk-crypto-wasm": "18.0.0",
},
openclaw: {
releaseChecks: {
rootDependencyMirrorAllowlist: ["@matrix-org/matrix-sdk-crypto-wasm"],
},
},
},
},
],
new Set(),
),
).toEqual([
"bundled extension 'matrix' manifest invalid | openclaw.releaseChecks.rootDependencyMirrorAllowlist entry '@matrix-org/matrix-sdk-crypto-wasm' must be mirrored in root runtime dependencies",
]);
});
it("accepts mirror entries declared by both the extension and root package", () => {
expect(
collectBundledExtensionRootDependencyMirrorErrors(
[
{
id: "matrix",
packageJson: {
dependencies: {
"@matrix-org/matrix-sdk-crypto-wasm": "18.0.0",
},
openclaw: {
releaseChecks: {
rootDependencyMirrorAllowlist: ["@matrix-org/matrix-sdk-crypto-wasm"],
},
},
},
},
],
new Set(["@matrix-org/matrix-sdk-crypto-wasm"]),
),
).toEqual([]);
});
});
describe("collectForbiddenPackPaths", () => {
it("allows bundled plugin runtime deps under dist/extensions but still blocks other node_modules", () => {
expect(
@@ -139,7 +236,6 @@ describe("collectMissingPackPaths", () => {
expect.arrayContaining([
"dist/channel-catalog.json",
"dist/control-ui/index.html",
"dist/pkg/matrix_sdk_crypto_wasm_bg.wasm",
bundledDistPluginFile("matrix", "helper-api.js"),
bundledDistPluginFile("matrix", "runtime-api.js"),
bundledDistPluginFile("matrix", "thread-bindings-runtime.js"),
@@ -160,7 +256,6 @@ describe("collectMissingPackPaths", () => {
"dist/entry.js",
"dist/control-ui/index.html",
...requiredBundledPluginPackPaths,
...requiredMatrixCryptoWasmPackPaths,
...requiredPluginSdkPackPaths,
"dist/plugin-sdk/root-alias.cjs",
"dist/build-info.json",

View File

@@ -1,45 +0,0 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { copyMatrixCryptoWasmPkg } from "../../scripts/copy-matrix-crypto-wasm-pkg.mjs";
function createRepoFixture() {
return fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-matrix-wasm-pkg-"));
}
describe("copyMatrixCryptoWasmPkg", () => {
it("stages the matrix crypto wasm package into dist/pkg", () => {
const repoRoot = createRepoFixture();
const sourcePkgDir = path.join(
repoRoot,
"node_modules",
"@matrix-org",
"matrix-sdk-crypto-wasm",
"pkg",
);
fs.mkdirSync(sourcePkgDir, { recursive: true });
fs.writeFileSync(path.join(sourcePkgDir, "matrix_sdk_crypto_wasm_bg.wasm"), "wasm\n", "utf8");
fs.writeFileSync(path.join(sourcePkgDir, "matrix_sdk_crypto_wasm_bg.js"), "js\n", "utf8");
copyMatrixCryptoWasmPkg({ cwd: repoRoot });
expect(
fs.readFileSync(path.join(repoRoot, "dist", "pkg", "matrix_sdk_crypto_wasm_bg.wasm"), "utf8"),
).toBe("wasm\n");
expect(
fs.readFileSync(path.join(repoRoot, "dist", "pkg", "matrix_sdk_crypto_wasm_bg.js"), "utf8"),
).toBe("js\n");
});
it("removes stale dist/pkg output when the source package is unavailable", () => {
const repoRoot = createRepoFixture();
const staleTargetDir = path.join(repoRoot, "dist", "pkg");
fs.mkdirSync(staleTargetDir, { recursive: true });
fs.writeFileSync(path.join(staleTargetDir, "stale.txt"), "stale\n", "utf8");
copyMatrixCryptoWasmPkg({ cwd: repoRoot });
expect(fs.existsSync(staleTargetDir)).toBe(false);
});
});