Repository navigation
Expand file tree
/
Copy pathbuild-native-installer.mjs
More file actions
201 lines (192 loc) · 12.9 KB
/
Copy pathbuild-native-installer.mjs
File metadata and controls
201 lines (192 loc) · 12.9 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
#!/usr/bin/env node
// Package trusted, already-built native artifacts. This does not install on a host.
import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { execFileSync, spawnSync } from 'node:child_process';
import { chmod, copyFile, lstat, mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises';
import { basename, dirname, isAbsolute, join, relative, sep } from 'node:path';
import { createRequire } from 'node:module';
export const pins = { node: '22.22.0', ...JSON.parse(readFileSync(new URL('./native-pins.json', import.meta.url), 'utf8')) };
const platforms = { linux: 'linux', darwin: 'darwin', win32: 'windows' };
const architectures = { x64: 'amd64', arm64: 'arm64' };
const inside = (root, path) => {
const rel = relative(root, path);
return rel !== '..' && !rel.startsWith('..' + sep) && !isAbsolute(rel);
};
// Resolve existing parents even when the output does not exist yet. macOS
// temporary paths and Windows short paths can alias a component source.
async function outputRealPath(path) {
try { return await realpath(path); }
catch (error) {
if (error.code !== 'ENOENT' || dirname(path) === path) throw error;
return join(await outputRealPath(dirname(path)), basename(path));
}
}
// Flatten contained links, including directory links used by pnpm exports. Every
// traversal checks its resolved target; active ancestors detect directory cycles.
export async function copyComponent(source, destination, omit = []) {
const root = await realpath(source);
if (!(await stat(root)).isDirectory()) throw new Error('Component source must be a directory');
const files = {};
async function visit(path, target, ancestors) {
if (omit.includes(relative(destination, target).split(sep).join('/'))) return;
const resolved = await realpath(path);
if (!inside(root, resolved)) throw new Error('Component link escapes its source');
const info = await stat(resolved);
if (info.isDirectory()) {
if (ancestors.has(resolved)) throw new Error('Component contains a directory link cycle');
const next = new Set([...ancestors, resolved]);
await mkdir(target);
for (const name of (await readdir(resolved)).sort()) {
// Manifest paths are portable slash paths, never Windows aliases/streams.
if (/[\\:\x00-\x1f]/.test(name) || /[. ]$/.test(name) || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(name)) {
throw new Error('Component contains a non-portable file name');
}
await visit(join(resolved, name), join(target, name), next);
}
return;
}
if (!info.isFile()) throw new Error('Component contains a non-regular file');
await copyFile(resolved, target);
const executable = Boolean(info.mode & 0o111) || /\.(exe|cmd|bat)$/i.test(target);
await chmod(target, executable ? 0o755 : 0o644);
files[relative(destination, target).split(sep).join('/')] = {
sha256: createHash('sha256').update(await readFile(target)).digest('hex'), executable,
};
}
await visit(root, destination, new Set());
if (!Object.keys(files).length) throw new Error('Component is empty');
return files;
}
function probe(command, args, cwd, env) {
const result = spawnSync(command, args, { cwd, env, input: '', encoding: 'utf8', timeout: 30000, killSignal: 'SIGKILL', maxBuffer: 1024 * 1024 });
if (result.error || result.status !== 0) throw new Error('Native component compatibility probe failed');
return result.stdout.trim();
}
// Files that only build or install tooling and that no Harness or the daemon loads at run time.
// pnpm is the CI runner's global install, not part of the Node distribution. better-sqlite3 loads only
// build/Release/better_sqlite3.node, with SQLite compiled in; deps/ and src/ are its compiler inputs.
// Names cover the Unix and Windows layouts.
const buildOnly = {
node: ['bin/pnpm', 'bin/pnpx', 'lib/node_modules/pnpm', 'node_modules/pnpm', 'pnpm', 'pnpm.cmd', 'pnpm.ps1', 'pnpx', 'pnpx.cmd', 'pnpx.ps1'],
minimax: ['native/node_modules/better-sqlite3/deps', 'native/node_modules/better-sqlite3/src', 'native/node_modules/better-sqlite3/binding.gyp'],
};
const json = async path => JSON.parse(await readFile(path, 'utf8'));
const requireFile = async path => { if (!(await stat(path)).isFile()) throw new Error('Required component file is missing'); };
// Flattening Node's Unix bin links moves npm's relative requires. Keep the
// original package entrypoints and publish relocatable regular-file launchers.
export async function prepareNodeEntrypoints(root, files) {
if (process.platform === 'win32') return;
for (const name of ['npm', 'npx']) {
const entry = `lib/node_modules/npm/bin/${name}-cli.js`;
await requireFile(join(root, entry));
const launcher = '#!/bin/sh\n' +
'basedir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) || exit 1\n' +
`exec "$basedir/node" "$basedir/../${entry}" "$@"\n`;
const path = `bin/${name}`;
await writeFile(join(root, path), launcher, { mode: 0o755 });
await chmod(join(root, path), 0o755);
files[path] = { sha256: createHash('sha256').update(launcher).digest('hex'), executable: true };
}
}
export async function validateNodeCommands(nodeRoot, cwd) {
const windows = process.platform === 'win32';
const bin = windows ? nodeRoot : join(nodeRoot, 'bin');
const npmRoot = join(nodeRoot, windows ? 'node_modules/npm' : 'lib/node_modules/npm');
const version = (await json(join(npmRoot, 'package.json'))).version;
const env = { ...process.env, PATH: [bin, process.env.PATH ?? ''].join(windows ? ';' : ':') };
for (const name of ['npm', 'npx']) {
await requireFile(join(bin, windows ? `${name}.cmd` : name));
// Windows command scripts require cmd.exe; only these fixed command names
// enter the shell. Unix probes use executable lookup from the installed PATH.
const observed = windows
? probe(process.env.ComSpec ?? 'cmd.exe', ['/d', '/s', '/c', `${name} --version`], cwd, env)
: probe(name, ['--version'], cwd, env);
if (observed !== version) throw new Error('Bundled npm entrypoint compatibility failed');
}
}
export async function validateComponents(root, names, daemonVersion) {
const windows = process.platform === 'win32';
const nodeRoot = join(root, 'components', 'node');
const node = join(nodeRoot, windows ? 'node.exe' : 'bin/node');
const env = { ...process.env, PATH: [dirname(node), join(root, 'components', 'minimax', 'bin'), process.env.PATH ?? ''].join(sep === '\\' ? ';' : ':') };
if (probe(node, ['--version'], root, env) !== `v${pins.node}`) throw new Error('Node version does not match the pin');
const identity = JSON.parse(probe(node, ['-p', 'JSON.stringify([process.platform,process.arch])'], root, env));
if (identity[0] !== process.platform || identity[1] !== process.arch) throw new Error('Node platform does not match the build host');
await validateNodeCommands(nodeRoot, root);
const daemon = join(root, windows ? 'oac-daemon.exe' : 'oac-daemon');
if (probe(daemon, ['version'], root, env) !== daemonVersion) throw new Error('Daemon identity changed during packaging');
for (const name of names) {
const component = join(root, 'components', name);
if (name === 'codex') {
if (!(await stat(join(component, 'codex-resources'))).isDirectory()) throw new Error('Codex resources are missing');
const binary = join(component, 'bin', windows ? 'codex.exe' : 'codex');
if (probe(binary, ['--version'], component, env) !== `codex-cli ${pins.codex}`) throw new Error('Codex version does not match the pin');
} else if (name === 'claude') {
const manifest = await json(join(component, 'package.json'));
if (manifest.dependencies?.['@anthropic-ai/claude-agent-sdk']?.split('(')[0] !== pins.claude) throw new Error('Claude SDK pin mismatch');
await requireFile(join(component, 'pnpm-lock.yaml'));
await requireFile(join(component, 'dist/main.js'));
const info = JSON.parse(probe(node, [join(component, 'dist/runtime_check.js')], component, env));
if (info.type !== 'runtime_ready' || info.protocol !== 3 || info.sdk !== pins.claude || !info.features?.includes('local_runtime_v2')) throw new Error('Claude runtime compatibility failed');
} else if (name === 'minimax') {
if (windows) throw new Error('MiniMax is not supported on Windows');
const upstream = await json(join(component, 'source.json'));
const expected = await json(new URL('../packages/mcode-harness/source.json', import.meta.url));
if (upstream.version !== pins.minimax || upstream.revision !== expected.revision) throw new Error('MiniMax source pin mismatch');
for (const entry of ['check.mjs', 'subagent-snapshot.mjs', 'provenance.json', 'native-patch.json']) await requireFile(join(component, entry));
if (probe(node, [join(component, 'native/cli.js'), '--version'], component, env) !== pins.minimax) throw new Error('MiniMax native pin mismatch');
const info = JSON.parse(probe(node, [join(component, 'check.mjs')], component, env));
if (info.protocol !== 3 || info.native !== pins.minimax || info.source !== expected.revision) throw new Error('MiniMax companion compatibility failed');
}
}
}
export async function buildBundle(options) {
if (!platforms[process.platform] || !architectures[process.arch]) throw new Error('Unsupported native bundle platform');
for (const required of ['daemon', 'node', 'output']) if (!options[required]) throw new Error(`Missing --${required}`);
for (const path of Object.values(options)) if (!isAbsolute(path)) throw new Error('Bundle paths must be absolute');
const names = ['codex', 'claude', 'minimax'].filter(name => options[name]);
if (!names.length) throw new Error('At least one Harness source is required');
if (process.platform === 'win32' && options.minimax) throw new Error('MiniMax is not supported on Windows');
try { await lstat(options.output); throw new Error('Bundle output already exists'); } catch (error) { if (error.code !== 'ENOENT') throw error; }
const output = await outputRealPath(options.output);
// Refuse staging inside a source: recursive copying must not ingest its own output.
for (const name of ['node', ...names]) if (inside(await realpath(options[name]), output)) throw new Error('Output must be outside component sources');
const daemonVersion = probe(options.daemon, ['version'], dirname(options.daemon), process.env);
if (!/^[0-9A-Za-z][0-9A-Za-z.+_-]{0,127}$/.test(daemonVersion)) throw new Error('Invalid daemon version');
await mkdir(dirname(output), { recursive: true });
const staging = await mkdtemp(join(dirname(output), '.native-bundle-'));
try {
const daemon = join(staging, process.platform === 'win32' ? 'oac-daemon.exe' : 'oac-daemon');
await copyFile(options.daemon, daemon); await chmod(daemon, 0o755);
await mkdir(join(staging, 'components'));
const components = {};
for (const name of ['node', ...names]) components[name] = { version: pins[name], files: await copyComponent(options[name], join(staging, 'components', name), buildOnly[name]) };
await prepareNodeEntrypoints(join(staging, 'components', 'node'), components.node.files);
if (components.minimax) {
const component = join(staging, 'components', 'minimax');
const nativeRequire = createRequire(join(component, 'native', 'cli.js'));
const rg = await realpath(nativeRequire('@vscode/ripgrep').rgPath);
if (!inside(component, rg)) throw new Error('MiniMax ripgrep escaped the component');
await requireFile(rg);
await mkdir(join(component, 'bin'), { recursive: true });
await copyFile(rg, join(component, 'bin', 'rg')); await chmod(join(component, 'bin', 'rg'), 0o755);
components.minimax.files['bin/rg'] = { sha256: createHash('sha256').update(await readFile(rg)).digest('hex'), executable: true };
}
await validateComponents(staging, names, daemonVersion);
const manifest = { schema: 1, daemon_version: daemonVersion, os: platforms[process.platform], arch: architectures[process.arch], components };
await writeFile(join(staging, 'bundle.json'), JSON.stringify(manifest, null, 2) + '\n');
await rename(staging, output);
return manifest;
} finally { await rm(staging, { recursive: true, force: true }); }
}
// Split a built bundle into the small base archive (the daemon and bundle.json) and one archive
// per component. Names are the artifact names Core serves: <os>-<arch>[-<component>].
export async function archiveBundle(bundle, output) {
const { os, arch, components } = await json(join(bundle, 'bundle.json'));
const tar = process.platform === 'win32' ? join(process.env.SystemRoot, 'System32', 'tar.exe') : 'tar';
const pack = (name, entries) => execFileSync(tar, ['-czf', join(output, `${name}.tar.gz`), '-C', bundle, ...entries], { env: { ...process.env, COPYFILE_DISABLE: '1' } });
await mkdir(output, { recursive: true });
pack(`${os}-${arch}`, [process.platform === 'win32' ? 'oac-daemon.exe' : 'oac-daemon', 'bundle.json']);
for (const name of Object.keys(components)) pack(`${os}-${arch}-${name}`, [`components/${name}`]);
}