Repository navigation
Expand file tree
/
Copy pathcli-invocation.mjs
More file actions
157 lines (147 loc) 路 6.06 KB
/
Copy pathcli-invocation.mjs
File metadata and controls
157 lines (147 loc) 路 6.06 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
// 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";
/** 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 });
}
function readStandaloneManifest({ executable, runtimeRoot }) {
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 },
};
}
/** 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;
}
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;
}
/** 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,
};
}