@h-rig/isolation-plugin 0.0.6-alpha.156 → 0.0.6-alpha.158
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/embedded-native-assets.d.ts +7 -0
- package/dist/src/embedded-native-assets.js +6 -0
- package/dist/src/image-fingerprint-sidecar.d.ts +1 -0
- package/dist/src/image-fingerprint-sidecar.js +515 -0
- package/dist/src/image.d.ts +40 -0
- package/dist/src/image.js +1498 -0
- package/dist/src/index.js +4220 -20
- package/dist/src/isolation/binary-build-worker.d.ts +1 -0
- package/dist/src/isolation/binary-build-worker.js +323 -0
- package/dist/src/isolation/discovery.d.ts +7 -0
- package/dist/src/isolation/discovery.js +477 -0
- package/dist/src/isolation/git-native.d.ts +28 -0
- package/dist/src/isolation/git-native.js +598 -0
- package/dist/src/isolation/home.d.ts +25 -0
- package/dist/src/isolation/home.js +929 -0
- package/dist/src/isolation/index.d.ts +43 -0
- package/dist/src/isolation/index.js +4062 -0
- package/dist/src/isolation/provisioning-env.d.ts +1 -0
- package/dist/src/isolation/provisioning-env.js +6 -0
- package/dist/src/isolation/runner.d.ts +20 -0
- package/dist/src/isolation/runner.js +1881 -0
- package/dist/src/isolation/runtime-binary-build.d.ts +88 -0
- package/dist/src/isolation/runtime-binary-build.js +480 -0
- package/dist/src/isolation/shared.d.ts +29 -0
- package/dist/src/isolation/shared.js +283 -0
- package/dist/src/isolation/toolchain.d.ts +71 -0
- package/dist/src/isolation/toolchain.js +1348 -0
- package/dist/src/isolation/types.d.ts +15 -0
- package/dist/src/isolation/types.js +1 -0
- package/dist/src/isolation/worktree.d.ts +22 -0
- package/dist/src/isolation/worktree.js +353 -0
- package/dist/src/native-extract.d.ts +2 -0
- package/dist/src/native-extract.js +44 -0
- package/dist/src/plugin.d.ts +2 -2
- package/dist/src/plugin.js +4219 -19
- package/dist/src/runtime-config.d.ts +3 -0
- package/dist/src/runtime-config.js +215 -0
- package/dist/src/runtime-native-sidecar.d.ts +8 -0
- package/dist/src/runtime-native-sidecar.js +368 -0
- package/dist/src/runtime-native.d.ts +51 -0
- package/dist/src/runtime-native.js +485 -0
- package/dist/src/sandbox/backend-bwrap.d.ts +20 -0
- package/dist/src/sandbox/backend-bwrap.js +268 -0
- package/dist/src/sandbox/backend-none.d.ts +11 -0
- package/dist/src/sandbox/backend-none.js +20 -0
- package/dist/src/sandbox/backend-seatbelt.d.ts +13 -0
- package/dist/src/sandbox/backend-seatbelt.js +225 -0
- package/dist/src/sandbox/backend.d.ts +117 -0
- package/dist/src/sandbox/backend.js +864 -0
- package/dist/src/sandbox/orchestrator.d.ts +21 -0
- package/dist/src/sandbox/orchestrator.js +895 -0
- package/dist/src/sandbox/utils.d.ts +43 -0
- package/dist/src/sandbox/utils.js +94 -0
- package/dist/src/service.d.ts +10 -5
- package/dist/src/service.js +4145 -2
- package/dist/src/sidecar-arg.d.ts +7 -0
- package/dist/src/sidecar-arg.js +6 -0
- package/dist/src/sidecar-entrypoint.d.ts +9 -0
- package/dist/src/sidecar-entrypoint.js +401 -0
- package/dist/src/snapshot-sidecar.d.ts +2 -0
- package/dist/src/snapshot-sidecar.js +566 -0
- package/dist/src/snapshot.d.ts +64 -0
- package/dist/src/snapshot.js +515 -0
- package/dist/src/task-run-snapshot.d.ts +26 -0
- package/dist/src/task-run-snapshot.js +713 -0
- package/native/darwin-arm64/rig-git +0 -0
- package/native/darwin-arm64/rig-git.build-manifest.json +4 -0
- package/native/darwin-arm64/runtime-native.dylib +0 -0
- package/native/darwin-x64/rig-git +0 -0
- package/native/darwin-x64/runtime-native.dylib +0 -0
- package/native/linux-arm64/rig-git +0 -0
- package/native/linux-arm64/runtime-native.so +0 -0
- package/native/linux-x64/rig-git +0 -0
- package/native/linux-x64/runtime-native.so +0 -0
- package/native/win32-x64/rig-git.exe +0 -0
- package/native/win32-x64/runtime-native.dll +0 -0
- package/package.json +45 -5
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/isolation-plugin/src/runtime-native.ts
|
|
3
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
4
|
+
import { copyFileSync, existsSync as existsSync2, mkdirSync as mkdirSync2, renameSync as renameSync2, rmSync, statSync as statSync2 } from "fs";
|
|
5
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
6
|
+
import { dirname, resolve as resolve2 } from "path";
|
|
7
|
+
|
|
8
|
+
// packages/isolation-plugin/src/native-extract.ts
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "fs";
|
|
10
|
+
import { tmpdir } from "os";
|
|
11
|
+
import { resolve } from "path";
|
|
12
|
+
|
|
13
|
+
// packages/isolation-plugin/src/embedded-native-assets.ts
|
|
14
|
+
var embeddedNatives = null;
|
|
15
|
+
|
|
16
|
+
// packages/isolation-plugin/src/native-extract.ts
|
|
17
|
+
var sharedNativeOutputDir = resolve(tmpdir(), "rig-native");
|
|
18
|
+
var extractionCache = {};
|
|
19
|
+
function hasEmbeddedNatives() {
|
|
20
|
+
return embeddedNatives != null;
|
|
21
|
+
}
|
|
22
|
+
function extractEmbeddedNative(name) {
|
|
23
|
+
if (name in extractionCache) {
|
|
24
|
+
return extractionCache[name] ?? null;
|
|
25
|
+
}
|
|
26
|
+
const entry = embeddedNatives?.[name];
|
|
27
|
+
if (!entry) {
|
|
28
|
+
extractionCache[name] = null;
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const targetPath = resolve(sharedNativeOutputDir, entry.fileName);
|
|
33
|
+
mkdirSync(sharedNativeOutputDir, { recursive: true });
|
|
34
|
+
const upToDate = existsSync(targetPath) && statSync(targetPath).size === entry.size;
|
|
35
|
+
if (!upToDate) {
|
|
36
|
+
const bytes = readFileSync(entry.filePath);
|
|
37
|
+
const tempPath = `${targetPath}.${process.pid}.${Date.now()}.tmp`;
|
|
38
|
+
writeFileSync(tempPath, bytes, { mode: 493 });
|
|
39
|
+
renameSync(tempPath, targetPath);
|
|
40
|
+
}
|
|
41
|
+
extractionCache[name] = targetPath;
|
|
42
|
+
} catch {
|
|
43
|
+
extractionCache[name] = null;
|
|
44
|
+
}
|
|
45
|
+
return extractionCache[name] ?? null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// packages/isolation-plugin/src/sidecar-arg.ts
|
|
49
|
+
var RIG_NATIVE_RUNTIME_SIDECAR_ARG = "__rig_native_runtime_sidecar";
|
|
50
|
+
|
|
51
|
+
// packages/isolation-plugin/src/runtime-native.ts
|
|
52
|
+
var sharedNativeRuntimeOutputDir = resolve2(tmpdir2(), "rig-native");
|
|
53
|
+
var sharedNativeRuntimeOutputPath = resolve2(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
54
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
55
|
+
var OPERATION_RESULT_SIZE = 24;
|
|
56
|
+
var RUNTIME_SCAN_ENTRY_SIZE = 64;
|
|
57
|
+
var RUNTIME_SCAN_RESULT_SIZE = 40;
|
|
58
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
59
|
+
function requireNativeRuntimeLibrary(feature) {
|
|
60
|
+
if (!nativeRuntimeLibrary) {
|
|
61
|
+
throw new Error(`Native Zig runtime is required for ${feature}`);
|
|
62
|
+
}
|
|
63
|
+
return nativeRuntimeLibrary;
|
|
64
|
+
}
|
|
65
|
+
function runtimeNativeLibraryFileName() {
|
|
66
|
+
return colocatedNativeRuntimeFileName;
|
|
67
|
+
}
|
|
68
|
+
function runtimePrepareTrackedPathsNative(input) {
|
|
69
|
+
const response = runNativeRuntimeSidecar({
|
|
70
|
+
op: "prepare-paths",
|
|
71
|
+
input
|
|
72
|
+
});
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
throw new Error(response.error);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function runtimeLinkDependencyLayerNative(sourceDir, targetDir) {
|
|
78
|
+
const response = runNativeRuntimeSidecar({
|
|
79
|
+
op: "link-dependency-layer",
|
|
80
|
+
input: { sourceDir, targetDir }
|
|
81
|
+
});
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
throw new Error(response.error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function runtimeScanWorktreesNative(worktreesRoot) {
|
|
87
|
+
const response = runNativeRuntimeSidecar({
|
|
88
|
+
op: "scan-worktrees",
|
|
89
|
+
input: { worktreesRoot }
|
|
90
|
+
});
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
throw new Error(response.error);
|
|
93
|
+
}
|
|
94
|
+
return response.entries ?? [];
|
|
95
|
+
}
|
|
96
|
+
function runtimePrepareTrackedPathsInProcess(input) {
|
|
97
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime dependency layer linking");
|
|
98
|
+
const logsDir = Buffer.from(input.logsDir, "utf8");
|
|
99
|
+
const stateDir = Buffer.from(input.stateDir, "utf8");
|
|
100
|
+
const sessionDir = Buffer.from(input.sessionDir, "utf8");
|
|
101
|
+
const eventsFile = Buffer.from(input.eventsFile, "utf8");
|
|
102
|
+
const controlledBashLogFile = Buffer.from(input.controlledBashLogFile, "utf8");
|
|
103
|
+
const resultPtr = runtimeLibrary.symbols.runtime_prepare_paths(Number(ptr(logsDir)), logsDir.byteLength, Number(ptr(stateDir)), stateDir.byteLength, Number(ptr(sessionDir)), sessionDir.byteLength, Number(ptr(eventsFile)), eventsFile.byteLength, Number(ptr(controlledBashLogFile)), controlledBashLogFile.byteLength);
|
|
104
|
+
if (!resultPtr) {
|
|
105
|
+
throw new Error("runtime_prepare_paths returned null");
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
const view = viewAt(resultPtr, OPERATION_RESULT_SIZE);
|
|
109
|
+
throwIfOperationError(view, "runtime_prepare_paths");
|
|
110
|
+
} finally {
|
|
111
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function runtimeLinkDependencyLayerInProcess(sourceDir, targetDir) {
|
|
115
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime worktree discovery");
|
|
116
|
+
const sourceBuffer = Buffer.from(sourceDir, "utf8");
|
|
117
|
+
const targetBuffer = Buffer.from(targetDir, "utf8");
|
|
118
|
+
const resultPtr = runtimeLibrary.symbols.runtime_link_dependency_layer(Number(ptr(sourceBuffer)), sourceBuffer.byteLength, Number(ptr(targetBuffer)), targetBuffer.byteLength);
|
|
119
|
+
if (!resultPtr) {
|
|
120
|
+
throw new Error("runtime_link_dependency_layer returned null");
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
const view = viewAt(resultPtr, OPERATION_RESULT_SIZE);
|
|
124
|
+
throwIfOperationError(view, "runtime_link_dependency_layer");
|
|
125
|
+
} finally {
|
|
126
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function runtimeScanWorktreesInProcess(worktreesRoot) {
|
|
130
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime worktree discovery");
|
|
131
|
+
const rootBuffer = Buffer.from(worktreesRoot, "utf8");
|
|
132
|
+
const resultPtr = runtimeLibrary.symbols.runtime_scan_worktrees(Number(ptr(rootBuffer)), rootBuffer.byteLength);
|
|
133
|
+
if (!resultPtr) {
|
|
134
|
+
throw new Error(`runtime_scan_worktrees returned null for ${worktreesRoot}`);
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const view = viewAt(resultPtr, RUNTIME_SCAN_RESULT_SIZE);
|
|
138
|
+
throwIfScanError(view, "runtime_scan_worktrees");
|
|
139
|
+
const entriesPtr = readU64(view, 8);
|
|
140
|
+
const entriesLen = readU64(view, 16);
|
|
141
|
+
const entries = [];
|
|
142
|
+
for (let index = 0;index < entriesLen; index += 1) {
|
|
143
|
+
const entryView = viewAt(entriesPtr + index * RUNTIME_SCAN_ENTRY_SIZE, RUNTIME_SCAN_ENTRY_SIZE);
|
|
144
|
+
entries.push({
|
|
145
|
+
workspaceDir: readString(readU64(entryView, 0), readU64(entryView, 8)),
|
|
146
|
+
runtimeDir: readString(readU64(entryView, 16), readU64(entryView, 24)),
|
|
147
|
+
contextPath: readString(readU64(entryView, 32), readU64(entryView, 40)),
|
|
148
|
+
metadataPath: readString(readU64(entryView, 48), readU64(entryView, 56))
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return entries;
|
|
152
|
+
} finally {
|
|
153
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function defaultNativeRuntimeLibraryPath() {
|
|
157
|
+
return sharedNativeRuntimeOutputPath;
|
|
158
|
+
}
|
|
159
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
160
|
+
const explicitLib = process.env.RIG_NATIVE_RUNTIME_LIB?.trim();
|
|
161
|
+
if (explicitLib && existsSync2(explicitLib)) {
|
|
162
|
+
return explicitLib;
|
|
163
|
+
}
|
|
164
|
+
const embeddedPath = extractEmbeddedNative("snapshot");
|
|
165
|
+
if (embeddedPath) {
|
|
166
|
+
return embeddedPath;
|
|
167
|
+
}
|
|
168
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
169
|
+
return outputPath;
|
|
170
|
+
}
|
|
171
|
+
return !options.force && existsSync2(outputPath) ? outputPath : null;
|
|
172
|
+
}
|
|
173
|
+
async function materializeNativeRuntimeLibrary(targetDir) {
|
|
174
|
+
const sourcePath = await ensureNativeRuntimeLibraryPath();
|
|
175
|
+
if (!sourcePath) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
const targetPath = resolve2(targetDir, colocatedNativeRuntimeFileName);
|
|
179
|
+
mkdirSync2(targetDir, { recursive: true });
|
|
180
|
+
const needsCopy = !existsSync2(targetPath) || statSync2(sourcePath).mtimeMs > statSync2(targetPath).mtimeMs;
|
|
181
|
+
if (needsCopy) {
|
|
182
|
+
copyFileSync(sourcePath, targetPath);
|
|
183
|
+
}
|
|
184
|
+
return targetPath;
|
|
185
|
+
}
|
|
186
|
+
async function loadNativeRuntimeLibrary() {
|
|
187
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
const explicitLib = process.env.RIG_NATIVE_RUNTIME_LIB?.trim();
|
|
191
|
+
if (explicitLib && existsSync2(explicitLib)) {
|
|
192
|
+
const loaded = tryDlopenNativeRuntimeLibrary(explicitLib);
|
|
193
|
+
if (loaded) {
|
|
194
|
+
return loaded;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const embeddedPath = extractEmbeddedNative("snapshot");
|
|
198
|
+
if (embeddedPath) {
|
|
199
|
+
const loaded = tryDlopenNativeRuntimeLibrary(embeddedPath);
|
|
200
|
+
if (loaded) {
|
|
201
|
+
return loaded;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
205
|
+
if (!candidate || !existsSync2(candidate)) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
209
|
+
if (loaded) {
|
|
210
|
+
return loaded;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
214
|
+
if (!builtLibraryPath) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
218
|
+
}
|
|
219
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
220
|
+
const candidates = [];
|
|
221
|
+
let cursor = resolve2(fromDir);
|
|
222
|
+
for (let index = 0;index < 8; index += 1) {
|
|
223
|
+
for (const name of names) {
|
|
224
|
+
candidates.push(resolve2(cursor, "native", `${process.platform}-${process.arch}`, name), resolve2(cursor, "native", `${process.platform}-${process.arch}`, "lib", name), resolve2(cursor, "native", name), resolve2(cursor, "native", "lib", name));
|
|
225
|
+
}
|
|
226
|
+
const parent = dirname(cursor);
|
|
227
|
+
if (parent === cursor)
|
|
228
|
+
break;
|
|
229
|
+
cursor = parent;
|
|
230
|
+
}
|
|
231
|
+
return candidates;
|
|
232
|
+
}
|
|
233
|
+
function nativeRuntimeLibraryCandidates() {
|
|
234
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
235
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
236
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
237
|
+
return [...new Set([
|
|
238
|
+
explicit,
|
|
239
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
240
|
+
execDir ? resolve2(execDir, colocatedNativeRuntimeFileName) : "",
|
|
241
|
+
execDir ? resolve2(execDir, platformSpecific) : "",
|
|
242
|
+
execDir ? resolve2(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
243
|
+
execDir ? resolve2(execDir, "..", platformSpecific) : "",
|
|
244
|
+
execDir ? resolve2(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
245
|
+
execDir ? resolve2(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
246
|
+
sharedNativeRuntimeOutputPath
|
|
247
|
+
].filter(Boolean))];
|
|
248
|
+
}
|
|
249
|
+
function resolveNativeRuntimeSourcePath() {
|
|
250
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
251
|
+
if (explicit && existsSync2(explicit)) {
|
|
252
|
+
return explicit;
|
|
253
|
+
}
|
|
254
|
+
const bundled = resolve2(import.meta.dir, "../native/snapshot.zig");
|
|
255
|
+
return existsSync2(bundled) ? bundled : null;
|
|
256
|
+
}
|
|
257
|
+
function resolveNativeRuntimeSidecarSourcePath() {
|
|
258
|
+
const envRoots = [
|
|
259
|
+
process.cwd()?.trim(),
|
|
260
|
+
process.env.RIG_HOST_PROJECT_ROOT?.trim(),
|
|
261
|
+
process.env.PROJECT_RIG_ROOT?.trim()
|
|
262
|
+
].filter(Boolean);
|
|
263
|
+
for (const root of envRoots) {
|
|
264
|
+
for (const relative of [
|
|
265
|
+
"packages/isolation-plugin/src/runtime-native-sidecar.ts",
|
|
266
|
+
"packages/isolation-plugin/dist/src/runtime-native-sidecar.js"
|
|
267
|
+
]) {
|
|
268
|
+
const candidate = resolve2(root, relative);
|
|
269
|
+
if (existsSync2(candidate)) {
|
|
270
|
+
return candidate;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
for (const localCandidate of [
|
|
275
|
+
resolve2(import.meta.dir, "runtime-native-sidecar.js"),
|
|
276
|
+
resolve2(import.meta.dir, "runtime-native-sidecar.ts")
|
|
277
|
+
]) {
|
|
278
|
+
if (existsSync2(localCandidate))
|
|
279
|
+
return localCandidate;
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
284
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
const zigBinary = Bun.which("zig");
|
|
288
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
289
|
+
if (!zigBinary || !sourcePath) {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
293
|
+
try {
|
|
294
|
+
mkdirSync2(dirname(outputPath), { recursive: true });
|
|
295
|
+
const needsBuild = options.force === true || !existsSync2(outputPath) || statSync2(sourcePath).mtimeMs > statSync2(outputPath).mtimeMs;
|
|
296
|
+
if (!needsBuild) {
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
const build = Bun.spawn([
|
|
300
|
+
zigBinary,
|
|
301
|
+
"build-lib",
|
|
302
|
+
sourcePath,
|
|
303
|
+
"-dynamic",
|
|
304
|
+
"-O",
|
|
305
|
+
"ReleaseFast",
|
|
306
|
+
`-femit-bin=${tempOutputPath}`
|
|
307
|
+
], {
|
|
308
|
+
cwd: import.meta.dir,
|
|
309
|
+
stdout: "pipe",
|
|
310
|
+
stderr: "pipe"
|
|
311
|
+
});
|
|
312
|
+
const exitCode = await build.exited;
|
|
313
|
+
if (exitCode !== 0 || !existsSync2(tempOutputPath)) {
|
|
314
|
+
rmSync(tempOutputPath, { force: true });
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
renameSync2(tempOutputPath, outputPath);
|
|
318
|
+
return true;
|
|
319
|
+
} catch {
|
|
320
|
+
rmSync(tempOutputPath, { force: true });
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
325
|
+
try {
|
|
326
|
+
return dlopen(outputPath, {
|
|
327
|
+
rig_scope_match: {
|
|
328
|
+
args: ["ptr", "ptr"],
|
|
329
|
+
returns: "u8"
|
|
330
|
+
},
|
|
331
|
+
snapshot_capture: {
|
|
332
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
333
|
+
returns: "ptr"
|
|
334
|
+
},
|
|
335
|
+
snapshot_delta: {
|
|
336
|
+
args: ["ptr", "ptr"],
|
|
337
|
+
returns: "ptr"
|
|
338
|
+
},
|
|
339
|
+
snapshot_store_delta: {
|
|
340
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
341
|
+
returns: "ptr"
|
|
342
|
+
},
|
|
343
|
+
snapshot_inspect_delta: {
|
|
344
|
+
args: ["ptr", "u64"],
|
|
345
|
+
returns: "ptr"
|
|
346
|
+
},
|
|
347
|
+
snapshot_apply_delta: {
|
|
348
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
349
|
+
returns: "ptr"
|
|
350
|
+
},
|
|
351
|
+
snapshot_release: {
|
|
352
|
+
args: ["ptr"],
|
|
353
|
+
returns: "void"
|
|
354
|
+
},
|
|
355
|
+
runtime_hash_file: {
|
|
356
|
+
args: ["ptr", "u64"],
|
|
357
|
+
returns: "ptr"
|
|
358
|
+
},
|
|
359
|
+
runtime_hash_tree: {
|
|
360
|
+
args: ["ptr", "u64"],
|
|
361
|
+
returns: "ptr"
|
|
362
|
+
},
|
|
363
|
+
runtime_prepare_paths: {
|
|
364
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
365
|
+
returns: "ptr"
|
|
366
|
+
},
|
|
367
|
+
runtime_link_dependency_layer: {
|
|
368
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
369
|
+
returns: "ptr"
|
|
370
|
+
},
|
|
371
|
+
runtime_scan_worktrees: {
|
|
372
|
+
args: ["ptr", "u64"],
|
|
373
|
+
returns: "ptr"
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
} catch {
|
|
377
|
+
return null;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function resolveNativeRuntimeSidecarCommand(request) {
|
|
381
|
+
if (hasEmbeddedNatives()) {
|
|
382
|
+
return {
|
|
383
|
+
argv: [process.execPath, RIG_NATIVE_RUNTIME_SIDECAR_ARG, JSON.stringify(request)],
|
|
384
|
+
env: {}
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
const hostBinary = process.env.RIG_NATIVE_HOST_BINARY?.trim();
|
|
388
|
+
if (hostBinary) {
|
|
389
|
+
return {
|
|
390
|
+
argv: [hostBinary, RIG_NATIVE_RUNTIME_SIDECAR_ARG, JSON.stringify(request)],
|
|
391
|
+
env: {}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
const sidecarSourcePath = resolveNativeRuntimeSidecarSourcePath();
|
|
395
|
+
if (!sidecarSourcePath) {
|
|
396
|
+
throw new Error("runtime-native-sidecar.ts source file not found.");
|
|
397
|
+
}
|
|
398
|
+
const bunCli = resolveNativeRuntimeSidecarInvocation();
|
|
399
|
+
return {
|
|
400
|
+
argv: [bunCli.command, sidecarSourcePath, JSON.stringify(request)],
|
|
401
|
+
env: bunCli.env
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
function runNativeRuntimeSidecar(request) {
|
|
405
|
+
const { argv, env } = resolveNativeRuntimeSidecarCommand(request);
|
|
406
|
+
const proc = Bun.spawnSync(argv, {
|
|
407
|
+
cwd: process.env.RIG_HOST_PROJECT_ROOT?.trim() || process.cwd(),
|
|
408
|
+
stdout: "pipe",
|
|
409
|
+
stderr: "pipe",
|
|
410
|
+
env: {
|
|
411
|
+
...process.env,
|
|
412
|
+
...env,
|
|
413
|
+
RIG_NATIVE_RUNTIME_SIDECAR: "1"
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
if (proc.exitCode !== 0) {
|
|
417
|
+
throw new Error(proc.stderr.toString() || proc.stdout.toString() || `runtime native sidecar exited ${proc.exitCode}`);
|
|
418
|
+
}
|
|
419
|
+
const stdout = proc.stdout.toString().trim();
|
|
420
|
+
if (!stdout) {
|
|
421
|
+
throw new Error("runtime native sidecar returned empty output.");
|
|
422
|
+
}
|
|
423
|
+
const responseLine = stdout.split(`
|
|
424
|
+
`).map((line) => line.trim()).filter((line) => line.startsWith("{")).at(-1);
|
|
425
|
+
if (!responseLine) {
|
|
426
|
+
throw new Error(`runtime native sidecar returned no JSON response: ${stdout}`);
|
|
427
|
+
}
|
|
428
|
+
return JSON.parse(responseLine);
|
|
429
|
+
}
|
|
430
|
+
function resolveNativeRuntimeSidecarInvocation() {
|
|
431
|
+
const bunPath = Bun.which("bun");
|
|
432
|
+
if (bunPath) {
|
|
433
|
+
return { command: bunPath, env: {} };
|
|
434
|
+
}
|
|
435
|
+
if (process.execPath?.trim()) {
|
|
436
|
+
return {
|
|
437
|
+
command: process.execPath,
|
|
438
|
+
env: { BUN_BE_BUN: "1" }
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
throw new Error("bun is required to run the runtime native sidecar.");
|
|
442
|
+
}
|
|
443
|
+
function viewAt(address, size) {
|
|
444
|
+
return Buffer.from(toBuffer(address, 0, size));
|
|
445
|
+
}
|
|
446
|
+
function readU64(view, offset) {
|
|
447
|
+
return Number(view.readBigUInt64LE(offset));
|
|
448
|
+
}
|
|
449
|
+
function readString(address, length) {
|
|
450
|
+
if (!address || !length) {
|
|
451
|
+
return "";
|
|
452
|
+
}
|
|
453
|
+
return Buffer.from(toBuffer(address, 0, length)).toString("utf8");
|
|
454
|
+
}
|
|
455
|
+
function throwIfOperationError(view, opName) {
|
|
456
|
+
const errorPtr = readU64(view, 8);
|
|
457
|
+
const errorLen = readU64(view, 16);
|
|
458
|
+
if (!errorPtr || errorLen === 0) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
throw new Error(`${opName} failed: ${readString(errorPtr, errorLen)}`);
|
|
462
|
+
}
|
|
463
|
+
function throwIfScanError(view, opName) {
|
|
464
|
+
const errorPtr = readU64(view, 24);
|
|
465
|
+
const errorLen = readU64(view, 32);
|
|
466
|
+
if (!errorPtr || errorLen === 0) {
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
throw new Error(`${opName} failed: ${readString(errorPtr, errorLen)}`);
|
|
470
|
+
}
|
|
471
|
+
export {
|
|
472
|
+
runtimeScanWorktreesNative,
|
|
473
|
+
runtimeScanWorktreesInProcess,
|
|
474
|
+
runtimePrepareTrackedPathsNative,
|
|
475
|
+
runtimePrepareTrackedPathsInProcess,
|
|
476
|
+
runtimeNativeLibraryFileName,
|
|
477
|
+
runtimeLinkDependencyLayerNative,
|
|
478
|
+
runtimeLinkDependencyLayerInProcess,
|
|
479
|
+
requireNativeRuntimeLibrary,
|
|
480
|
+
nativeRuntimeLibrary,
|
|
481
|
+
materializeNativeRuntimeLibrary,
|
|
482
|
+
ensureNativeRuntimeLibraryPath,
|
|
483
|
+
defaultNativeRuntimeLibraryPath,
|
|
484
|
+
RIG_NATIVE_RUNTIME_SIDECAR_ARG
|
|
485
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SandboxConfig } from "@rig/contracts";
|
|
2
|
+
import type { FilesystemContext, ResolvedPaths, RuntimeSandboxPlan, SandboxBackendKind, SandboxWrapOptions, SandboxWrapper } from "./backend";
|
|
3
|
+
export declare class BwrapBackend implements SandboxWrapper {
|
|
4
|
+
readonly kind: SandboxBackendKind;
|
|
5
|
+
private readonly binaryPath;
|
|
6
|
+
private readonly config;
|
|
7
|
+
private readonly ctx;
|
|
8
|
+
private readonly resolvedPaths;
|
|
9
|
+
private readonly which;
|
|
10
|
+
constructor(binaryPath: string, config: SandboxConfig, ctx: FilesystemContext, resolvedPaths: ResolvedPaths, which?: (bin: string) => string | null);
|
|
11
|
+
wrap(options: SandboxWrapOptions): RuntimeSandboxPlan;
|
|
12
|
+
private buildCommand;
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the first element of a command array to its real absolute path.
|
|
15
|
+
* Required for bwrap: the sandbox has no PATH pointing to ~/.local/bin,
|
|
16
|
+
* so "claude" would fail. We resolve via the injected which function,
|
|
17
|
+
* then follow symlinks with realpathSync.
|
|
18
|
+
*/
|
|
19
|
+
private resolveCommandBinary;
|
|
20
|
+
}
|