openclaw/test/scripts/runtime-process-build-graph.test.ts
RoboClaw 3f0eb3cbd7
fix(exec): keep worker-only code out of ordinary command startup (#199422)
* fix(exec): keep worker-only code out of ordinary command startup

Reuse the existing private runtime chunk policy for the service child group anchor. Keep optional lineage completion reachable without evaluating its state graph for ordinary commands, and cover emitted graph, npm inventory, and relocated relay retirement.

Co-authored-by: vincentkoc <25068+vincentkoc@users.noreply.github.com>
Co-authored-by: VACInc <3279061+VACInc@users.noreply.github.com>

* fix(exec): keep worker-only code out of ordinary command startup

Worked on by:
- @vincentkoc
- @VACInc

Co-authored-by: vincentkoc <25068+vincentkoc@users.noreply.github.com>
Co-authored-by: VACInc <3279061+VACInc@users.noreply.github.com>
OpenClaw-Publication: f66ccf8d-403b-415b-a2a2-5217dba18515

---------

Co-authored-by: vincentkoc <25068+vincentkoc@users.noreply.github.com>
Co-authored-by: VACInc <3279061+VACInc@users.noreply.github.com>
2026-11-15 17:18:26 +08:00

175 lines
5.8 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { build } from "esbuild";
import { describe, expect, it } from "vitest";
import {
sharedRuntimeProcessBuildEntries,
standaloneRuntimeProcessBuildEntries,
} from "../../scripts/lib/runtime-process-core-build-entries.mts";
import { createScriptTestHarness } from "./test-helpers.js";
const { createTempDir } = createScriptTestHarness();
const repoRoot = fileURLToPath(new URL("../../", import.meta.url));
const forbiddenInputs = [
/^src\/gateway\/server(\.|-|\/)/u,
/^src\/flows\//u,
/^src\/wizard\//u,
/^src\/agents\/tools\/in-process-gateway\.ts$/u,
/^src\/infra\/update-run-write\.ts$/u,
/^src\/infra\/update-failure-facts\.ts$/u,
];
describe("standalone runtime process build graph", () => {
it("keeps small workers out of shared Gateway chunks", () => {
const workers = [
"infra/update-state-sizes.entry",
"config/sessions/disk-budget.worker",
"media/attachment-processor.worker",
"agents/plugin-model-catalog-read.worker",
"agents/code-mode-node.worker",
];
expect(
Object.keys(
sharedRuntimeProcessBuildEntries({
...Object.fromEntries(workers.map((name) => [name, name])),
"fixture/shared": "fixture/shared.ts",
}),
),
).toEqual(["fixture/shared"]);
});
const lazyWorkers = [
...["state/openclaw-state-lease-heartbeat.worker", "cron/store/read-only.worker"].map(
(name) => ({
name,
optional: [
"src/state/openclaw-state-db-engine.ts",
"src/state/postgres-schema.generated/runtime.ts",
],
}),
),
{
name: "process/supervisor/service-child-group-anchor",
optional: ["src/node-host/node-worker-lineage-completion.ts"],
},
];
it.each(lazyWorkers)(
"keeps optional handlers outside $name static imports",
async ({ name, optional }) => {
const [{ build: buildRuntime }, { default: buildConfigs }] = await Promise.all([
import("tsdown"),
import("../../tsdown.config.ts"),
]);
const configs = Array.isArray(buildConfigs) ? buildConfigs : [buildConfigs];
const selected = configs.find(
(config) =>
config.entry &&
typeof config.entry === "object" &&
!Array.isArray(config.entry) &&
Object.hasOwn(config.entry, name),
);
if (!selected) {
throw new Error("Missing runtime build config");
}
const root = fs.realpathSync(createTempDir("runtime-private-chunks-"));
const { bundles } = await buildRuntime({
...selected,
config: false,
outDir: root,
dts: false,
sourcemap: false,
logLevel: "silent",
});
try {
const chunks = bundles.flatMap((bundle) =>
bundle.chunks.filter((chunk) => chunk.type === "chunk"),
);
const byName = new Map(chunks.map((chunk) => [chunk.fileName, chunk]));
const entry = chunks.find((chunk) => chunk.isEntry);
expect(entry).toBeDefined();
const closure = (includeDynamic: boolean) => {
const reached = new Set<string>();
const visit = (fileName: string) => {
if (reached.has(fileName)) {
return;
}
const chunk = byName.get(fileName);
if (!chunk) {
throw new Error(`Missing emitted chunk: ${fileName}`);
}
reached.add(fileName);
const imports = includeDynamic
? [...chunk.imports, ...chunk.dynamicImports]
: chunk.imports;
for (const imported of imports) {
if (byName.has(imported)) {
visit(imported);
} else if (imported.startsWith(".")) {
visit(path.posix.join(path.posix.dirname(fileName), imported));
}
}
};
visit(entry!.fileName);
return reached;
};
const eager = closure(false);
const reachable = closure(true);
for (const source of optional) {
const owners = chunks.filter((chunk) =>
Object.entries(chunk.modules).some(
([id, module]) =>
module.renderedLength > 0 && id.replaceAll("\\", "/").endsWith(`/${source}`),
),
);
expect(owners.length, `packaged ${source}`).toBeGreaterThan(0);
expect(
owners.every((chunk) => reachable.has(chunk.fileName)),
`reachable ${source}`,
).toBe(true);
expect(
owners.filter((chunk) => eager.has(chunk.fileName)).map((chunk) => chunk.fileName),
`${name} eagerly loads ${source}`,
).toEqual([]);
}
} finally {
for (const bundle of bundles) {
await bundle[Symbol.asyncDispose]();
}
}
},
60_000,
);
it.each(Object.entries(standaloneRuntimeProcessBuildEntries))(
"keeps %s out of the Gateway server graph",
async (name, entry) => {
const result = await build({
absWorkingDir: repoRoot,
entryPoints: [entry],
bundle: true,
platform: "node",
format: "esm",
packages: "external",
write: false,
metafile: true,
});
const inputs = Object.keys(result.metafile.inputs).map((input) =>
input.replaceAll("\\", "/"),
);
expect(
inputs.filter((input) => forbiddenInputs.some((pattern) => pattern.test(input))),
).toEqual([]);
if (name === "infra/sqlite-source-revision.worker") {
expect(
inputs.filter((input) =>
/^src\/(?:config\/|plugins\/|snapshot\/|infra\/(?:node-sqlite|update-database-generations)\.)/u.test(
input,
),
),
).toEqual([]);
}
},
);
});