Files
openclaw/scripts/build-all.mjs
Peter Steinberger bb46b79d3c refactor: internalize OpenClaw agent runtime (#85341)
* refactor: extract agent core package

Introduce packages/agent-core as the OpenClaw-owned home for reusable agent loop, harness, session, prompt, and runtime dependency contracts.

* refactor: extract shared llm runtime

Move provider model registries, stream wrappers, OAuth helpers, and LLM utilities into src/llm with plugin-sdk barrels instead of depending on the old embedded runtime layout.

* refactor: remove pi runtime internals

Rename remaining Pi-shaped agent surfaces to OpenClaw agent runtime names, delete obsolete Pi docs and package graph checks, and add the third-party notice for incorporated code.

* refactor: tighten agent session runtime

Make agent-core/runtime dependencies explicit, consolidate compaction and session transcript helpers, and move model/session helpers behind OpenClaw-owned contracts.

* refactor: remove static model and pi auth paths

Drop static model catalogs and Pi auth bridges, move model/provider facts to manifest-owned runtime contracts, and harden internal embedded-agent utilities.

* refactor: remove legacy provider compat paths

* docs: remove agent parity notes

* fix: skip provider wildcard metadata parsing

* refactor: share session extension sdk loading

* refactor: inline acpx proxy error formatter

* refactor: fold edit recovery into edit tool

* fix: accept extension batch separator

* test: align startup provider plugin expectations

* fix: restore provider-scoped release discovery

* test: align static asset packaging expectations

* fix: run static provider catalogs during scoped discovery

* fix: add provider entry catalogs for scoped live discovery

* fix: load lightweight provider catalog entries

* fix: refresh provider-scoped plugin metadata

* fix: keep provider catalog entries on release live path

* fix: keep static manifest models in release live checks

* fix: harden release model discovery

* fix: reduce OpenAI live cache probe reasoning

* fix: disable OpenAI cache probe reasoning

* ci: extend OpenAI gateway live timeout

* fix: extend live gateway model budget

* fix: stabilize release validation regressions

* fix: honor provider aliases in model rows

* fix: stabilize release validation lanes

* fix: stabilize release memory qa

* ci: stabilize release validation lanes

* ci: prefer ipv4 for live docker node calls

* fix: restore shared tool-call stream wrapper

* ci: remove legacy pi test shard alias

* fix: clean up embedded agent test drift

* fix: stabilize runtime alias status

* fix: clean up embedded agent ci drift

* fix: restore release ci invariants

* fix: clean up post-rebase runtime drift

* fix: restore release ci checks

* fix: restore release ci after rebase

* fix: remove stale pi runtime path

* test: align compaction runtime expectations

* test: update plugin prerelease expectations

* fix: handle claude live tool approvals

* fix: stabilize release validation gates

* fix: finish agent runtime import

* test: finish post-rebase agent runtime mocks

* fix: keep codex compaction native

* fix: stabilize codex app-server hook tests

* test: isolate codex diagnostic active run

* test: remove codex diagnostic completion race

# Conflicts:
#	extensions/codex/src/app-server/run-attempt.test.ts

* ci: fix full release manifest performance run id

* refactor: narrow llm plugin sdk boundary

* chore: drop generated google boundary stamps

* fix: repair rebase fallout

* fix: clean up rebased runtime references

* fix: decode codex jwt payloads as base64url

* fix: preserve shipped pi runtime alias

* fix: add scoped sdk virtual modules

* fix: decode llm codex oauth jwt as base64url

* fix: avoid stale vertex adc negative cache

* fix: harden tool arg decoding and codeql path

* fix: keep vertex adc negative checks live

* refactor: consolidate codex jwt and edit helpers

* fix: await codex oauth node runtime imports

* fix: preserve sdk tool and notice contracts

* fix: preserve shipped compat config boundaries

* fix: align codex oauth callback host

* fix: terminate agent-core loop streams on failure

* fix: keep codex oauth callback alive during fallback

* ci: include session tools in critical codeql scans

* fix: keep Cloudflare Anthropic provider auth header

* docs: redirect legacy pi runtime pages

* fix: honor bundled web provider compat discovery

* fix: protect session output spill files

* fix: keep legacy agent dir env blocked

* fix: contain auto-discovered skill symlinks

* fix: harden agent core sdk proxy surfaces

* fix: restore approval reaction sdk compat

* fix: keep live docker runs bounded

* fix: keep codex oauth redirect host aligned

* fix: resolve post-rebase agent runtime drift

* fix: redact anthropic oauth parse failures

* fix: preserve responses strict tool shaping

* fix: repair agent runtime rebase cleanup

* docs: redirect retired parity pages

* fix: bound auto-discovered resources to roots

* fix: repair post-rebase agent test drift

* fix: preserve bundled provider allowlist migration

* fix: preserve manifest-owned provider aliases

* fix: declare photon image dependency

* fix: keep provider headers out of proxy body

* fix: preserve shipped env aliases

* fix: refresh control ui i18n generated state

* fix: quote read fallback paths

* fix: preview edits through configured backend

* test: satisfy core test typecheck

* fix: preserve ZAI usage auth fallback

* test: repair codex diagnostic test

* fix: repair agent runtime rebase drift

* test: finish embedded runner import rename

* fix: repair agent runtime rebase integrations

* test: align compaction oauth fallback expectations

* fix: allow sdk-auth session models

* fix: update doctor tool schema import

* fix: preserve bedrock plugin region

* fix: stream harmony-like prose immediately

* ci: include session runtime in codeql shards

* fix: repair latest rebase integrations

* fix: honor explicit codex websocket transport

* fix: keep openai-compatible credentials provider-scoped

* fix: refresh sdk api baseline after rebase

* fix: route cli runtime aliases through openclaw harness

* test: rename stale harness mock expectation

* test: rename embedded agent overflow calls

* test: clean embedded auth test wording

* test: use openclaw stream types in deepinfra cache test

* fix: refresh sdk api baseline on latest main

* fix: honor bundled discovery compat allowlists

* fix: refresh sdk api baseline after latest rebase

* fix: remove stale rebase imports

* test: rename stale model catalog mock

* test: mock renamed doctor runtime modules

* fix: map canonical kimi env auth

* fix: use internal model registry in bench script

* fix: migrate deepinfra provider catalog entry

* fix: enforce builtin tool suppression

* fix: route compaction auth and proxy payloads safely

* refactor: prune unused llm registry leftovers

* test: update codex hooks session import

* test: fix model picker ci coverage

* test: align model picker auth mock types
2026-05-27 19:24:04 +01:00

431 lines
13 KiB
JavaScript

#!/usr/bin/env node
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { resolvePnpmRunner } from "./pnpm-runner.mjs";
const nodeBin = process.execPath;
const WINDOWS_BUILD_MAX_OLD_SPACE_MB = 4096;
const BUILD_CACHE_VERSION = 2;
const PNPM_STEP_NODE_FALLBACKS = new Map([
["plugins:assets:build", ["scripts/bundled-plugin-assets.mjs", "--phase", "build"]],
[
"build:plugin-sdk:dts",
["scripts/run-tsgo.mjs", "-p", "tsconfig.plugin-sdk.dts.json", "--declaration", "true"],
],
["plugins:assets:copy", ["scripts/bundled-plugin-assets.mjs", "--phase", "copy"]],
["ui:build", ["scripts/ui.js", "build"]],
]);
export const BUILD_ALL_STEPS = [
{ label: "plugins:assets:build", kind: "pnpm", pnpmArgs: ["plugins:assets:build"] },
{ label: "tsdown", kind: "node", args: ["scripts/tsdown-build.mjs"] },
{
label: "check-cli-bootstrap-imports",
kind: "node",
args: ["scripts/check-cli-bootstrap-imports.mjs"],
},
{ label: "runtime-postbuild", kind: "node", args: ["scripts/runtime-postbuild.mjs"] },
{ label: "build-stamp", kind: "node", args: ["scripts/build-stamp.mjs"] },
{
label: "runtime-postbuild-stamp",
kind: "node",
args: ["scripts/runtime-postbuild-stamp.mjs"],
},
{
label: "build:plugin-sdk:dts",
kind: "pnpm",
pnpmArgs: ["build:plugin-sdk:dts"],
windowsNodeOptions: `--max-old-space-size=${WINDOWS_BUILD_MAX_OLD_SPACE_MB}`,
cache: {
inputs: [
"tsconfig.json",
"tsconfig.plugin-sdk.dts.json",
"src/plugin-sdk",
"packages/memory-host-sdk/src",
"src/types",
"src/video-generation/dashscope-compatible.ts",
"src/video-generation/types.ts",
],
outputs: ["dist/plugin-sdk/.tsbuildinfo", "dist/plugin-sdk/packages", "dist/plugin-sdk/src"],
},
},
{
label: "write-plugin-sdk-entry-dts",
kind: "node",
args: ["--experimental-strip-types", "scripts/write-plugin-sdk-entry-dts.ts"],
},
{
label: "check-plugin-sdk-exports",
kind: "node",
args: ["scripts/check-plugin-sdk-exports.mjs"],
},
{
label: "plugins:assets:copy",
kind: "pnpm",
pnpmArgs: ["plugins:assets:copy"],
},
{
label: "copy-hook-metadata",
kind: "node",
args: ["--experimental-strip-types", "scripts/copy-hook-metadata.ts"],
},
{
label: "copy-export-html-templates",
kind: "node",
args: ["--experimental-strip-types", "scripts/copy-export-html-templates.ts"],
cache: {
inputs: [
"scripts/copy-export-html-templates.ts",
"scripts/lib/copy-assets.ts",
"src/auto-reply/reply/export-html",
],
outputs: ["dist/auto-reply/reply/export-html"],
},
},
{
label: "ui:build",
kind: "pnpm",
pnpmArgs: ["ui:build"],
// No build-all cache: ui/vite.config.ts derives the Control UI build ID
// from package.json, git HEAD, and OPENCLAW_CONTROL_UI_BUILD_ID env, so a
// file-input signature cannot exactly invalidate generated assets and a
// warm hit could restore stale service-worker/app cache metadata.
cache: undefined,
},
{
label: "write-build-info",
kind: "node",
args: ["--experimental-strip-types", "scripts/write-build-info.ts"],
},
{
label: "write-cli-startup-metadata",
kind: "node",
args: ["--experimental-strip-types", "scripts/write-cli-startup-metadata.ts"],
},
{
label: "write-cli-compat",
kind: "node",
args: ["--experimental-strip-types", "scripts/write-cli-compat.ts"],
},
];
export const BUILD_ALL_PROFILES = {
full: BUILD_ALL_STEPS.map((step) => step.label),
ciArtifacts: [
"plugins:assets:build",
"tsdown",
"check-cli-bootstrap-imports",
"runtime-postbuild",
"build-stamp",
"runtime-postbuild-stamp",
"build:plugin-sdk:dts",
"write-plugin-sdk-entry-dts",
"check-plugin-sdk-exports",
"plugins:assets:copy",
"copy-hook-metadata",
"copy-export-html-templates",
"ui:build",
"write-build-info",
"write-cli-startup-metadata",
"write-cli-compat",
],
gatewayWatch: [
"tsdown",
"check-cli-bootstrap-imports",
"runtime-postbuild",
"build-stamp",
"runtime-postbuild-stamp",
],
cliStartup: [
"tsdown",
"check-cli-bootstrap-imports",
"runtime-postbuild",
"build-stamp",
"runtime-postbuild-stamp",
"write-cli-startup-metadata",
"write-cli-compat",
],
};
export function resolveBuildAllSteps(profile = "full") {
const labels = BUILD_ALL_PROFILES[profile];
if (!labels) {
throw new Error(`Unknown build profile: ${profile}`);
}
const selected = labels.map((label) => BUILD_ALL_STEPS.find((step) => step.label === label));
if (selected.some((step) => !step)) {
const missing = labels.filter((label) => !BUILD_ALL_STEPS.some((step) => step.label === label));
throw new Error(`Build profile ${profile} references unknown steps: ${missing.join(", ")}`);
}
return selected;
}
function resolveStepEnv(step, env, platform) {
if (platform !== "win32" || !step.windowsNodeOptions) {
return env;
}
const currentNodeOptions = env.NODE_OPTIONS?.trim() ?? "";
if (currentNodeOptions.includes(step.windowsNodeOptions)) {
return env;
}
return {
...env,
NODE_OPTIONS: currentNodeOptions
? `${currentNodeOptions} ${step.windowsNodeOptions}`
: step.windowsNodeOptions,
};
}
export function resolveBuildAllStep(step, params = {}) {
const platform = params.platform ?? process.platform;
const env = resolveStepEnv(step, params.env ?? process.env, platform);
if (step.kind === "pnpm") {
const nodeFallbackArgs =
env.OPENCLAW_BUILD_ALL_NO_PNPM === "1" ? PNPM_STEP_NODE_FALLBACKS.get(step.label) : undefined;
if (nodeFallbackArgs) {
return {
command: params.nodeExecPath ?? nodeBin,
args: nodeFallbackArgs,
options: {
stdio: "inherit",
env,
},
};
}
const runner = resolvePnpmRunner({
pnpmArgs: step.pnpmArgs,
nodeExecPath: params.nodeExecPath ?? nodeBin,
npmExecPath: params.npmExecPath ?? env.npm_execpath,
comSpec: params.comSpec ?? env.ComSpec,
platform,
});
return {
command: runner.command,
args: runner.args,
options: {
stdio: "inherit",
env,
shell: runner.shell,
windowsVerbatimArguments: runner.windowsVerbatimArguments,
},
};
}
return {
command: params.nodeExecPath ?? nodeBin,
args: step.args,
options: {
stdio: "inherit",
env,
},
};
}
function listFilesRecursively(rootPath, fsImpl) {
let stat;
try {
stat = fsImpl.statSync(rootPath);
} catch {
return [];
}
if (stat.isFile()) {
return [rootPath];
}
if (!stat.isDirectory()) {
return [];
}
const out = [];
const entries = fsImpl.readdirSync(rootPath, { withFileTypes: true });
for (const entry of entries) {
if (entry.name === ".DS_Store") {
continue;
}
const entryPath = path.join(rootPath, entry.name);
if (entry.isDirectory()) {
out.push(...listFilesRecursively(entryPath, fsImpl));
} else if (entry.isFile()) {
out.push(entryPath);
}
}
return out;
}
function listCacheFiles(rootDir, entries, fsImpl) {
return entries
.flatMap((entry) => listFilesRecursively(path.resolve(rootDir, entry), fsImpl))
.toSorted();
}
function portableRelativePath(rootDir, filePath) {
return path.relative(rootDir, filePath).split(path.sep).join("/");
}
function normalizePortablePath(filePath) {
return filePath.replaceAll("\\", "/");
}
function resolveCachePaths(rootDir, step) {
const safeLabel = step.label.replace(/[^a-zA-Z0-9._-]+/g, "_");
const cacheDir = path.resolve(rootDir, ".artifacts/build-all-cache", safeLabel);
return {
cacheDir,
outputRoot: path.join(cacheDir, "outputs"),
stampPath: path.join(cacheDir, "stamp.json"),
};
}
function hashInputFiles(rootDir, files, fsImpl) {
const hash = createHash("sha256");
hash.update(`v${BUILD_CACHE_VERSION}\0`);
for (const file of files) {
hash.update(portableRelativePath(rootDir, file));
hash.update("\0");
hash.update(fsImpl.readFileSync(file));
hash.update("\0");
}
return hash.digest("hex");
}
function readCacheStamp(stampPath, fsImpl) {
try {
return JSON.parse(fsImpl.readFileSync(stampPath, "utf8"));
} catch {
return undefined;
}
}
function hasAllFiles(rootDir, relativeFiles, fsImpl) {
return relativeFiles.every((relativeFile) => {
try {
return fsImpl.statSync(path.resolve(rootDir, relativeFile)).isFile();
} catch {
return false;
}
});
}
function copyFileSync(fsImpl, sourcePath, targetPath) {
fsImpl.mkdirSync(path.dirname(targetPath), { recursive: true });
fsImpl.copyFileSync(sourcePath, targetPath);
}
export function resolveBuildAllStepCacheState(step, params = {}) {
if (!step.cache) {
return { cacheable: false, fresh: false, reason: "no-cache" };
}
const rootDir = params.rootDir ?? process.cwd();
const fsImpl = params.fs ?? fs;
const inputFiles = listCacheFiles(rootDir, step.cache.inputs, fsImpl);
if (inputFiles.length === 0) {
return { cacheable: true, fresh: false, reason: "missing-inputs" };
}
const signature = hashInputFiles(rootDir, inputFiles, fsImpl);
const { outputRoot, stampPath } = resolveCachePaths(rootDir, step);
const stamp = readCacheStamp(stampPath, fsImpl);
const outputFiles = listCacheFiles(rootDir, step.cache.outputs, fsImpl);
const relativeOutputFiles = outputFiles.map((file) => portableRelativePath(rootDir, file));
const stampedOutputs = Array.isArray(stamp?.outputs)
? stamp.outputs.map((entry) => normalizePortablePath(entry))
: [];
const stampMatches = stamp?.version === BUILD_CACHE_VERSION && stamp.signature === signature;
const actualOutputsPresent =
stampedOutputs.length > 0 && hasAllFiles(rootDir, stampedOutputs, fsImpl);
const cachedOutputsPresent =
stampedOutputs.length > 0 && hasAllFiles(outputRoot, stampedOutputs, fsImpl);
const restorable = stampMatches && !actualOutputsPresent && cachedOutputsPresent;
const fresh = stampMatches && (actualOutputsPresent || cachedOutputsPresent);
return {
cacheable: true,
fresh,
restorable,
reason: fresh ? (restorable ? "fresh-cache" : "fresh") : "stale",
signature,
outputRoot,
stampPath,
inputFiles: inputFiles.length,
outputFiles: outputFiles.length,
relativeOutputFiles,
stampedOutputs,
};
}
export function writeBuildAllStepCacheStamp(step, cacheState, params = {}) {
if (
!cacheState.cacheable ||
!cacheState.signature ||
!cacheState.stampPath ||
!cacheState.outputRoot ||
!cacheState.relativeOutputFiles?.length
) {
return;
}
const fsImpl = params.fs ?? fs;
const rootDir = params.rootDir ?? process.cwd();
for (const relativeFile of cacheState.relativeOutputFiles) {
copyFileSync(
fsImpl,
path.resolve(rootDir, relativeFile),
path.resolve(cacheState.outputRoot, relativeFile),
);
}
fsImpl.mkdirSync(path.dirname(cacheState.stampPath), { recursive: true });
fsImpl.writeFileSync(
cacheState.stampPath,
`${JSON.stringify({
version: BUILD_CACHE_VERSION,
label: step.label,
signature: cacheState.signature,
outputs: cacheState.relativeOutputFiles,
})}\n`,
);
}
export function restoreBuildAllStepCacheOutputs(cacheState, params = {}) {
if (!cacheState.restorable || !cacheState.outputRoot || !cacheState.stampedOutputs?.length) {
return false;
}
const fsImpl = params.fs ?? fs;
const rootDir = params.rootDir ?? process.cwd();
for (const relativeFile of cacheState.stampedOutputs) {
copyFileSync(
fsImpl,
path.resolve(cacheState.outputRoot, relativeFile),
path.resolve(rootDir, relativeFile),
);
}
return true;
}
function isMainModule() {
const argv1 = process.argv[1];
if (!argv1) {
return false;
}
return import.meta.url === pathToFileURL(argv1).href;
}
if (isMainModule()) {
const profile = process.argv[2] ?? "full";
for (const step of resolveBuildAllSteps(profile)) {
const cacheState = resolveBuildAllStepCacheState(step);
if (process.env.OPENCLAW_BUILD_CACHE !== "0" && cacheState.fresh) {
restoreBuildAllStepCacheOutputs(cacheState);
console.error(`[build-all] ${step.label} (cached)`);
continue;
}
console.error(`[build-all] ${step.label}`);
const invocation = resolveBuildAllStep(step);
const result = spawnSync(invocation.command, invocation.args, invocation.options);
if (typeof result.status === "number") {
if (result.status !== 0) {
process.exit(result.status);
}
writeBuildAllStepCacheStamp(step, resolveBuildAllStepCacheState(step));
continue;
}
process.exit(1);
}
}