The best way to control Claude Code, Codex and more from any computer or phone

main
ts 125 lines 4.81 KB
Raw
1 // TEMPORARY one-time workaround for a Node --build-sea bug on Linux arm64.
2 // It breaks kernels with 16 KiB or 64 KiB pages. Revert when Node ships a fix.
3 // Node issue: https://github.com/nodejs/node/issues/67048
4 // Workaround PR: https://github.com/pingdotgg/t3code/pull/25825
5 import * as NodeModule from "node:module";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Path from "effect/Path";
9 import * as Schema from "effect/Schema";
10 import { ChildProcess, ChildProcessSpawner } from "effect/process";
11
12 class LinuxArm64SeaError extends Schema.TaggedError<LinuxArm64SeaError>()("LinuxArm64SeaError", {
13 message: Schema.String,
14 }) {}
15
16 const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
17
18 const run = Effect.fn("LinuxArm64Sea.run")(function* (args: Array<string>, verbose: boolean) {
19 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
20 const child = yield* spawner.spawn(
21 ChildProcess.make(process.execPath, args, {
22 stdout: verbose ? "inherit" : "ignore",
23 stderr: "inherit",
24 }),
25 );
26 const exitCode = yield* child.exitCode;
27 if (exitCode !== 0) {
28 return yield* new LinuxArm64SeaError({ message: `${args[0]} exited with code ${exitCode}.` });
29 }
30 });
31
32 // Builds the SEA blob with this Node and injects it into a copy of this Node with postject.
33 export const injectLinuxArm64Sea = Effect.fn("LinuxArm64Sea.inject")(function* (input: {
34 bundlePath: string;
35 cliAssetPath: string;
36 outputPath: string;
37 nodeVersion: string;
38 verbose: boolean;
39 }) {
40 // The copied execPath is the shipped runtime; vp pack only checks its own Node.
41 if (process.versions.node !== input.nodeVersion) {
42 return yield* new LinuxArm64SeaError({
43 message: `Linux ARM64 executables must be built with Node ${input.nodeVersion}; running ${process.versions.node}.`,
44 });
45 }
46 const fs = yield* FileSystem.FileSystem;
47 const path = yield* Path.Path;
48 const temp = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sea-" });
49 const blobPath = path.join(temp, "sea.blob");
50 const configPath = path.join(temp, "sea.json");
51 yield* fs.writeFileString(
52 configPath,
53 yield* encodeJson({
54 main: input.bundlePath,
55 assets: { "binMain.mjs": input.cliAssetPath },
56 output: blobPath,
57 mainFormat: "module",
58 disableExperimentalSEAWarning: true,
59 // Node SEA useCodeCache breaks import(), which the server needs.
60 useCodeCache: false,
61 // The bootstrap loads the embedded CLI with require(esm).
62 execArgv: ["--require-module"],
63 }),
64 );
65 yield* run(["--experimental-sea-config", configPath], input.verbose);
66 yield* fs.copyFile(process.execPath, input.outputPath);
67 yield* fs.chmod(input.outputPath, 0o755);
68 const require = NodeModule.createRequire(import.meta.url);
69 yield* run(
70 [
71 require.resolve("postject/dist/cli.js"),
72 input.outputPath,
73 "NODE_SEA_BLOB",
74 blobPath,
75 "--sentinel-fuse",
76 "NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2",
77 ],
78 input.verbose,
79 );
80 }, Effect.scoped);
81
82 // Kernels map LOAD segments in whole pages. Checking at 64 KiB covers 4, 16, and 64 KiB kernels.
83 export function assertLinuxArm64PageCompatibility(bytes: Uint8Array) {
84 const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
85 if (
86 bytes.length < 64 ||
87 view.getUint32(0, false) !== 0x7f454c46 ||
88 bytes[4] !== 2 ||
89 bytes[5] !== 1 ||
90 view.getUint16(18, true) !== 183
91 ) {
92 throw new Error("Expected a little-endian ELF64 ARM64 executable.");
93 }
94 const tableOffset = Number(view.getBigUint64(32, true));
95 const entrySize = view.getUint16(54, true);
96 const count = view.getUint16(56, true);
97 if (
98 !Number.isSafeInteger(tableOffset) ||
99 entrySize < 56 ||
100 count === 0 ||
101 tableOffset + entrySize * count > bytes.length
102 ) {
103 throw new Error("Invalid ELF program header table.");
104 }
105 const pageSize = 0x10000n;
106 const ranges: Array<{ start: bigint; end: bigint }> = [];
107 for (let index = 0; index < count; index++) {
108 const header = tableOffset + index * entrySize;
109 if (view.getUint32(header, true) !== 1) continue;
110 const offset = view.getBigUint64(header + 8, true);
111 const address = view.getBigUint64(header + 16, true);
112 const size = view.getBigUint64(header + 40, true);
113 if (offset % pageSize !== address % pageSize) {
114 throw new Error(`ARM64 LOAD segment ${index} is incompatible with 64 KiB pages.`);
115 }
116 if (size === 0n) continue;
117 const start = address / pageSize;
118 const end = (address + size + pageSize - 1n) / pageSize;
119 if (ranges.some((range) => start < range.end && range.start < end)) {
120 throw new Error(`ARM64 LOAD segments overlap on a 64 KiB page (segment ${index}).`);
121 }
122 ranges.push({ start, end });
123 }
124 if (ranges.length === 0) throw new Error("ELF executable has no LOAD segments.");
125 }