Repository navigation
Expand file tree
/
Copy pathcli-invocation.mjs
More file actions
267 lines (253 loc) 路 10.1 KB
/
Copy pathcli-invocation.mjs
File metadata and controls
267 lines (253 loc) 路 10.1 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Bootstrap-safe ownership of OpenClaw executable identity and CLI invocation shape.
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { CLI_PARENT_BOUND_PRELOAD, CLI_RESPAWN_PARENT_ENV } from "./cli-parent-lifetime.mjs";
import { hasNodeStartupHooks } from "./node-runtime-env.mjs";
/** Packaging owns hashes; launch planning consumes only the artifact identity. */
export function readStandaloneRuntimeManifest(executable = process.execPath) {
return readStandaloneManifest({ executable });
}
/** Resolve a peer package through the same manifest contract, including renamed executables. */
export function readStandaloneRuntimeManifestForRoot(runtimeRoot) {
return readStandaloneManifest({ runtimeRoot });
}
/** The executing image owns its build facts, even if its sidecar is replaced. */
export function getStandaloneRuntimeBuild(executable = process.execPath) {
if (executable === process.execPath) {
// eslint-disable-next-line no-underscore-dangle -- Compiler-owned bootstrap contract.
const embedded = globalThis.__openclawStandaloneBuildInfo;
if (embedded !== undefined) {
return embedded;
}
}
return readStandaloneManifest({ executable, includeBuild: true });
}
function projectStandaloneBuildInfo(value) {
const nonBlank = (text) => typeof text === "string" && text.trim().length > 0;
const stringList = (items) => Array.isArray(items) && items.every(nonBlank);
const result = {
mode: value.mode,
openclaw: {
version: value.openclaw.version,
...(nonBlank(value.openclaw.commit) ? { commit: value.openclaw.commit } : {}),
},
bun: { version: value.bun.version },
};
for (const key of ["revision", "commit", "executableSha256"]) {
if (nonBlank(value.bun[key])) {
result.bun[key] = value.bun[key];
}
}
if (value.build !== undefined) {
const { id, flavor, pluginSetSha256, moduleGraphSha256 } = value.build;
if (
!nonBlank(id) ||
!["standard", "support"].includes(flavor) ||
!/^[a-f0-9]{64}$/.test(pluginSetSha256) ||
!/^[a-f0-9]{64}$/.test(moduleGraphSha256)
) {
throw new Error("Invalid standalone build provenance");
}
result.build = { id, flavor, pluginSetSha256, moduleGraphSha256 };
if (value.build.debugEntry !== undefined) {
const entry = value.build.debugEntry;
if (
typeof entry !== "string" ||
!entry.startsWith("debug/") ||
entry.includes("\\") ||
entry.split("/").some((part) => !part || part === "." || part === "..")
) {
throw new Error("Invalid standalone debugger entry");
}
result.build.debugEntry = entry;
}
}
if (value.target !== undefined) {
if (!nonBlank(value.target.platform) || !nonBlank(value.target.arch)) {
throw new Error("Invalid standalone build target");
}
result.target = { platform: value.target.platform, arch: value.target.arch };
}
if (value.plugins !== undefined) {
if (!stringList(value.plugins.embedded) || !stringList(value.plugins.omitted)) {
throw new Error("Invalid standalone plugin inventory");
}
result.plugins = { embedded: [...value.plugins.embedded], omitted: [...value.plugins.omitted] };
}
if (value.nativeModules !== undefined) {
if (
!Array.isArray(value.nativeModules) ||
value.nativeModules.some(
(item) =>
!item ||
!nonBlank(item.name) ||
!nonBlank(item.version) ||
!stringList(item.artifacts) ||
item.qualification !== "unverified" ||
!nonBlank(item.backend),
)
) {
throw new Error("Invalid standalone native inventory");
}
result.nativeModules = value.nativeModules.map(
({ name, version, artifacts, qualification, backend }) => ({
name,
version,
artifacts: [...artifacts],
qualification,
backend,
}),
);
}
return result;
}
function readStandaloneManifest({ executable, runtimeRoot, includeBuild = false }) {
const location = executable ?? runtimeRoot;
if (!path.isAbsolute(location)) {
return undefined;
}
const root = path.dirname(location);
const manifestPath = path.join(root, "manifest.json");
let raw;
try {
raw = readFileSync(manifestPath, "utf8");
} catch (error) {
if (error?.code === "ENOENT" || error?.code === "ENOTDIR") {
return undefined;
}
throw error;
}
let manifest;
try {
manifest = JSON.parse(raw);
} catch (cause) {
throw new Error(`Invalid standalone runtime manifest: ${manifestPath}`, { cause });
}
const nonBlank = (value) => typeof value === "string" && value.trim().length > 0;
if (
manifest?.schemaVersion !== 1 ||
!["jit", "bytecode", "aot"].includes(manifest.mode) ||
!nonBlank(manifest.executable?.path) ||
!nonBlank(manifest.runtime?.path) ||
!nonBlank(manifest.openclaw?.version) ||
!nonBlank(manifest.bun?.version)
) {
throw new Error(`Invalid standalone runtime manifest: ${manifestPath}`);
}
const executableRelative = path.relative(root, path.resolve(root, manifest.executable.path));
const sidecarRelative = path.relative(root, path.resolve(root, manifest.runtime.path));
if (
path.isAbsolute(manifest.executable.path) ||
!executableRelative ||
executableRelative === ".." ||
executableRelative.startsWith(`..${path.sep}`) ||
(executable && path.resolve(root, manifest.executable.path) !== path.resolve(executable)) ||
(runtimeRoot && path.resolve(root, manifest.runtime.path) !== path.resolve(runtimeRoot)) ||
path.isAbsolute(manifest.runtime.path) ||
!sidecarRelative ||
sidecarRelative === ".." ||
sidecarRelative.startsWith(`..${path.sep}`)
) {
throw new Error(`Invalid standalone runtime manifest: ${manifestPath}`);
}
return {
schemaVersion: 1,
mode: manifest.mode,
executable: {
path: manifest.executable.path,
...(nonBlank(manifest.executable.sha256) ? { sha256: manifest.executable.sha256 } : {}),
},
runtime: {
path: manifest.runtime.path,
...(nonBlank(manifest.runtime.sha256) ? { sha256: manifest.runtime.sha256 } : {}),
},
openclaw: {
version: manifest.openclaw.version,
...(nonBlank(manifest.openclaw.commit) ? { commit: manifest.openclaw.commit } : {}),
},
bun: { version: manifest.bun.version },
...(includeBuild ? projectStandaloneBuildInfo(manifest) : {}),
};
}
/** The compiler publishes this fact before any embedded module is loaded. */
export function getEmbeddedStandaloneRuntimeRoot() {
// eslint-disable-next-line no-underscore-dangle -- Matches the existing compiler bootstrap contract.
const rootUrl = globalThis.__openclawStandaloneRootUrl;
return typeof rootUrl === "string" ? fileURLToPath(rootUrl) : undefined;
}
/** Image membership comes from the compiler's module inventory, never disk contents. */
export function isEmbeddedStandaloneModule(modulePath) {
const filename = modulePath.startsWith("file:") ? fileURLToPath(modulePath) : modulePath;
// eslint-disable-next-line no-underscore-dangle -- Compiler bootstrap contract.
return globalThis.__openclawStandaloneIsEmbeddedModule?.(filename) === true;
}
function resolveStandaloneSidecarRoot(executable) {
if (executable === process.execPath) {
const embeddedRoot = getEmbeddedStandaloneRuntimeRoot();
if (embeddedRoot !== undefined) {
return embeddedRoot;
}
}
const manifest = readStandaloneRuntimeManifest(executable);
return manifest ? path.resolve(path.dirname(executable), manifest.runtime.path) : undefined;
}
export function isStandaloneRuntimeExecutable(executable = process.execPath) {
return resolveStandaloneSidecarRoot(executable) !== undefined;
}
/** Startup hooks can own a separate compiler/reaper; source filenames cannot prove that. */
export function runtimeHasSourceLoader() {
// BUN_BE_BUN executes disk programs, even when the executable has a manifest.
if (process.env.BUN_BE_BUN !== "1" && isStandaloneRuntimeExecutable()) {
return false;
}
return hasNodeStartupHooks(process.execArgv, process.env.NODE_OPTIONS);
}
/** Disk-executed package-manager programs belong to the standalone sidecar. */
export function resolveStandaloneNpmCliPath() {
const root = resolveStandaloneSidecarRoot(process.execPath);
return root === undefined ? undefined : path.join(root, "npm", "bin", "npm-cli.js");
}
/** A compiled Bun executable needs this mode to execute a disk program as Bun. */
export function resolveRuntimeScriptEnv(env) {
return isStandaloneRuntimeExecutable() ? { ...env, BUN_BE_BUN: "1" } : { ...env };
}
/** A null entry explicitly selects a CLI command; undefined inherits the current script. */
export function resolveOpenClawCliInvocation({
execPath = process.execPath,
entryPath,
args = process.argv.slice(2),
execArgv = process.execArgv,
} = {}) {
const standaloneRoot = resolveStandaloneSidecarRoot(execPath);
if (standaloneRoot !== undefined || entryPath === null) {
if (standaloneRoot !== undefined && entryPath !== undefined && entryPath !== null) {
const relative = path.relative(standaloneRoot, path.resolve(entryPath));
if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
throw new Error(
"A standalone OpenClaw executable cannot run another installation's entry script.",
);
}
}
return { command: execPath, args: [...args] };
}
const entry = entryPath ?? process.argv[1];
if (!entry) {
throw new Error("Cannot invoke the loose OpenClaw CLI without an entry script.");
}
return { command: execPath, args: [...execArgv, entry, ...args] };
}
/** Bind only this CLI child to its launcher; runtime flags belong before a loose entry. */
export function resolveOpenClawCliRespawn({ env = process.env, ...params } = {}) {
const execPath = params.execPath ?? process.execPath;
const invocation = resolveOpenClawCliInvocation({
...params,
execArgv: ["--import", CLI_PARENT_BOUND_PRELOAD, ...(params.execArgv ?? process.execArgv)],
});
return {
...invocation,
env: isStandaloneRuntimeExecutable(execPath)
? { ...env, [CLI_RESPAWN_PARENT_ENV]: String(process.pid) }
: env,
};
}