@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.
Files changed (77) hide show
  1. package/dist/src/embedded-native-assets.d.ts +7 -0
  2. package/dist/src/embedded-native-assets.js +6 -0
  3. package/dist/src/image-fingerprint-sidecar.d.ts +1 -0
  4. package/dist/src/image-fingerprint-sidecar.js +515 -0
  5. package/dist/src/image.d.ts +40 -0
  6. package/dist/src/image.js +1498 -0
  7. package/dist/src/index.js +4220 -20
  8. package/dist/src/isolation/binary-build-worker.d.ts +1 -0
  9. package/dist/src/isolation/binary-build-worker.js +323 -0
  10. package/dist/src/isolation/discovery.d.ts +7 -0
  11. package/dist/src/isolation/discovery.js +477 -0
  12. package/dist/src/isolation/git-native.d.ts +28 -0
  13. package/dist/src/isolation/git-native.js +598 -0
  14. package/dist/src/isolation/home.d.ts +25 -0
  15. package/dist/src/isolation/home.js +929 -0
  16. package/dist/src/isolation/index.d.ts +43 -0
  17. package/dist/src/isolation/index.js +4062 -0
  18. package/dist/src/isolation/provisioning-env.d.ts +1 -0
  19. package/dist/src/isolation/provisioning-env.js +6 -0
  20. package/dist/src/isolation/runner.d.ts +20 -0
  21. package/dist/src/isolation/runner.js +1881 -0
  22. package/dist/src/isolation/runtime-binary-build.d.ts +88 -0
  23. package/dist/src/isolation/runtime-binary-build.js +480 -0
  24. package/dist/src/isolation/shared.d.ts +29 -0
  25. package/dist/src/isolation/shared.js +283 -0
  26. package/dist/src/isolation/toolchain.d.ts +71 -0
  27. package/dist/src/isolation/toolchain.js +1348 -0
  28. package/dist/src/isolation/types.d.ts +15 -0
  29. package/dist/src/isolation/types.js +1 -0
  30. package/dist/src/isolation/worktree.d.ts +22 -0
  31. package/dist/src/isolation/worktree.js +353 -0
  32. package/dist/src/native-extract.d.ts +2 -0
  33. package/dist/src/native-extract.js +44 -0
  34. package/dist/src/plugin.d.ts +2 -2
  35. package/dist/src/plugin.js +4219 -19
  36. package/dist/src/runtime-config.d.ts +3 -0
  37. package/dist/src/runtime-config.js +215 -0
  38. package/dist/src/runtime-native-sidecar.d.ts +8 -0
  39. package/dist/src/runtime-native-sidecar.js +368 -0
  40. package/dist/src/runtime-native.d.ts +51 -0
  41. package/dist/src/runtime-native.js +485 -0
  42. package/dist/src/sandbox/backend-bwrap.d.ts +20 -0
  43. package/dist/src/sandbox/backend-bwrap.js +268 -0
  44. package/dist/src/sandbox/backend-none.d.ts +11 -0
  45. package/dist/src/sandbox/backend-none.js +20 -0
  46. package/dist/src/sandbox/backend-seatbelt.d.ts +13 -0
  47. package/dist/src/sandbox/backend-seatbelt.js +225 -0
  48. package/dist/src/sandbox/backend.d.ts +117 -0
  49. package/dist/src/sandbox/backend.js +864 -0
  50. package/dist/src/sandbox/orchestrator.d.ts +21 -0
  51. package/dist/src/sandbox/orchestrator.js +895 -0
  52. package/dist/src/sandbox/utils.d.ts +43 -0
  53. package/dist/src/sandbox/utils.js +94 -0
  54. package/dist/src/service.d.ts +10 -5
  55. package/dist/src/service.js +4145 -2
  56. package/dist/src/sidecar-arg.d.ts +7 -0
  57. package/dist/src/sidecar-arg.js +6 -0
  58. package/dist/src/sidecar-entrypoint.d.ts +9 -0
  59. package/dist/src/sidecar-entrypoint.js +401 -0
  60. package/dist/src/snapshot-sidecar.d.ts +2 -0
  61. package/dist/src/snapshot-sidecar.js +566 -0
  62. package/dist/src/snapshot.d.ts +64 -0
  63. package/dist/src/snapshot.js +515 -0
  64. package/dist/src/task-run-snapshot.d.ts +26 -0
  65. package/dist/src/task-run-snapshot.js +713 -0
  66. package/native/darwin-arm64/rig-git +0 -0
  67. package/native/darwin-arm64/rig-git.build-manifest.json +4 -0
  68. package/native/darwin-arm64/runtime-native.dylib +0 -0
  69. package/native/darwin-x64/rig-git +0 -0
  70. package/native/darwin-x64/runtime-native.dylib +0 -0
  71. package/native/linux-arm64/rig-git +0 -0
  72. package/native/linux-arm64/runtime-native.so +0 -0
  73. package/native/linux-x64/rig-git +0 -0
  74. package/native/linux-x64/runtime-native.so +0 -0
  75. package/native/win32-x64/rig-git.exe +0 -0
  76. package/native/win32-x64/runtime-native.dll +0 -0
  77. package/package.json +45 -5
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,323 @@
1
+ // @bun
2
+ // packages/isolation-plugin/src/isolation/runtime-binary-build.ts
3
+ import { chmodSync, copyFileSync, cpSync, existsSync, linkSync, mkdirSync, renameSync, rmSync, writeFileSync } from "fs";
4
+ import { basename, dirname, resolve as resolve2 } from "path";
5
+ import { fileURLToPath } from "url";
6
+ import { drainMicrotasks, gcAndSweep } from "bun:jsc";
7
+ import { resolveRigLayout } from "@rig/core/layout";
8
+ import { runtimeProvisioningEnv } from "@rig/core/runtime-provisioning-env";
9
+
10
+ // packages/isolation-plugin/src/native-extract.ts
11
+ import { tmpdir } from "os";
12
+ import { resolve } from "path";
13
+ var sharedNativeOutputDir = resolve(tmpdir(), "rig-native");
14
+
15
+ // packages/isolation-plugin/src/isolation/runtime-binary-build.ts
16
+ var runtimeBinaryBuildQueue = Promise.resolve();
17
+ async function buildRuntimeBinary(options) {
18
+ return runSerializedRuntimeBinaryBuild(async () => {
19
+ const resolved = resolveRuntimeBinaryBuildOptions(options);
20
+ runBestEffortBuildGc();
21
+ const manifestPath = runtimeBinaryCacheManifestPath(resolved.outputPath);
22
+ const buildKey = createRuntimeBinaryBuildKey({
23
+ entrypoint: resolved.entrypoint,
24
+ define: resolved.define,
25
+ env: resolved.env,
26
+ external: resolved.external
27
+ });
28
+ if (await isRuntimeBinaryBuildFresh({ outputPath: resolved.outputPath, manifestPath, buildKey })) {
29
+ return;
30
+ }
31
+ if (shouldUseRuntimeBinaryBuildWorker()) {
32
+ await buildRuntimeBinaryViaWorker(resolved);
33
+ } else {
34
+ await buildRuntimeBinaryInProcess(resolved, { manifestPath, buildKey });
35
+ }
36
+ runBestEffortBuildGc();
37
+ });
38
+ }
39
+ async function buildRuntimeBinaryInProcess(options, manifest) {
40
+ const tempBuildDir = resolve2(dirname(options.outputPath), `.bun-build-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`);
41
+ const tempOutputPath = resolve2(tempBuildDir, basename(options.outputPath));
42
+ mkdirSync(tempBuildDir, { recursive: true });
43
+ await withTemporaryEnv({
44
+ ...options.env,
45
+ ...options.define ? { RIG_BUILD_CONFIG_JSON: JSON.stringify(options.define) } : {}
46
+ }, async () => withTemporaryCwd(tempBuildDir, async () => {
47
+ const buildResult = await Bun.build({
48
+ entrypoints: [options.entrypoint],
49
+ compile: {
50
+ target: currentCompileTarget(),
51
+ outfile: tempOutputPath
52
+ },
53
+ target: "bun",
54
+ format: "esm",
55
+ minify: true,
56
+ bytecode: true,
57
+ metafile: true,
58
+ ...options.external ? { external: options.external } : {},
59
+ ...options.define ? { define: { __RIG_BUILD_CONFIG__: JSON.stringify(options.define) } } : {}
60
+ });
61
+ if (!buildResult.success) {
62
+ const details = buildResult.logs.map((log) => [log.message, log.position?.file ? `${log.position.file}:${log.position.line}:${log.position.column}` : ""].filter(Boolean).join(" ")).filter(Boolean).join(`
63
+ `);
64
+ throw new Error(`Failed to build ${options.entrypoint}: ${details || "Bun.build() returned errors"}`);
65
+ }
66
+ if (!existsSync(tempOutputPath)) {
67
+ const emitted = buildResult.outputs.map((output) => output.path).join(", ") || "(none)";
68
+ throw new Error(`Failed to build ${options.entrypoint}: Bun.build() did not emit ${tempOutputPath}. Emitted: ${emitted}`);
69
+ }
70
+ renameSync(tempOutputPath, options.outputPath);
71
+ chmodSync(options.outputPath, 493);
72
+ if (manifest) {
73
+ await writeRuntimeBinaryCacheManifest({
74
+ manifestPath: manifest.manifestPath,
75
+ buildKey: manifest.buildKey,
76
+ cwd: tempBuildDir,
77
+ metafile: buildResult.metafile
78
+ });
79
+ }
80
+ })).finally(() => {
81
+ rmSync(tempBuildDir, { recursive: true, force: true });
82
+ });
83
+ }
84
+ function runBestEffortBuildGc() {
85
+ try {
86
+ drainMicrotasks();
87
+ } catch {}
88
+ try {
89
+ gcAndSweep();
90
+ } catch {}
91
+ }
92
+ function runtimeBinaryCacheManifestPath(outputPath) {
93
+ return `${outputPath}.build-manifest.json`;
94
+ }
95
+ function resolveRuntimeBinaryBuildOptions(options) {
96
+ return {
97
+ ...options,
98
+ entrypoint: resolve2(options.cwd, options.sourcePath),
99
+ outputPath: resolve2(options.outputPath)
100
+ };
101
+ }
102
+ function shouldUseRuntimeBinaryBuildWorker() {
103
+ if (process.env.RIG_RUNTIME_BUILD_WORKER === "1") {
104
+ return false;
105
+ }
106
+ if (process.env.RIG_RUNTIME_BUILD_IN_PROCESS === "1") {
107
+ return false;
108
+ }
109
+ return true;
110
+ }
111
+ async function buildRuntimeBinaryViaWorker(options) {
112
+ const workerSourcePath = resolveRuntimeBinaryBuildWorkerSourcePath(options);
113
+ if (!workerSourcePath || !existsSync(workerSourcePath)) {
114
+ await buildRuntimeBinaryInProcess(options, {
115
+ manifestPath: runtimeBinaryCacheManifestPath(options.outputPath),
116
+ buildKey: createRuntimeBinaryBuildKey({
117
+ entrypoint: options.entrypoint,
118
+ define: options.define,
119
+ env: options.env,
120
+ external: options.external
121
+ })
122
+ });
123
+ return;
124
+ }
125
+ const payloadPath = createRuntimeBinaryBuildWorkerPayloadPath(options.outputPath);
126
+ const bunCli = resolveRuntimeBinaryBuildWorkerInvocation();
127
+ await Bun.write(payloadPath, `${JSON.stringify(options)}
128
+ `);
129
+ const build = Bun.spawn([bunCli.command, workerSourcePath, payloadPath], {
130
+ cwd: options.cwd,
131
+ stdout: "pipe",
132
+ stderr: "pipe",
133
+ env: {
134
+ ...process.env,
135
+ ...options.env,
136
+ ...bunCli.env,
137
+ RIG_RUNTIME_BUILD_WORKER: "1"
138
+ }
139
+ });
140
+ const [exitCode, stdout, stderr] = await Promise.all([
141
+ build.exited,
142
+ new Response(build.stdout).text(),
143
+ new Response(build.stderr).text()
144
+ ]);
145
+ rmSync(payloadPath, { force: true });
146
+ if (exitCode !== 0) {
147
+ throw new Error(`Failed to build ${options.entrypoint}: ${(stderr || stdout || `worker exited ${exitCode}`).trim()}`);
148
+ }
149
+ }
150
+ function createRuntimeBinaryBuildWorkerPayloadPath(outputPath) {
151
+ return resolve2(dirname(outputPath), `.bun-build-worker-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}.json`);
152
+ }
153
+ function resolveRuntimeBinaryBuildWorkerSourcePath(options) {
154
+ const envRoots = [
155
+ options.cwd?.trim(),
156
+ process.env.RIG_HOST_PROJECT_ROOT?.trim(),
157
+ process.env.PROJECT_RIG_ROOT?.trim()
158
+ ].filter(Boolean);
159
+ for (const root of envRoots) {
160
+ const candidate = resolve2(root, "packages/isolation-plugin/src/isolation/binary-build-worker.ts");
161
+ if (existsSync(candidate)) {
162
+ return candidate;
163
+ }
164
+ }
165
+ const localCandidate = resolve2(import.meta.dir, "binary-build-worker.ts");
166
+ return existsSync(localCandidate) ? localCandidate : null;
167
+ }
168
+ function resolveRuntimeBinaryBuildWorkerInvocation() {
169
+ const bunPath = Bun.which("bun");
170
+ if (bunPath) {
171
+ return { command: bunPath, env: {} };
172
+ }
173
+ if (process.execPath?.trim()) {
174
+ return {
175
+ command: process.execPath,
176
+ env: { BUN_BE_BUN: "1" }
177
+ };
178
+ }
179
+ throw new Error("bun is required to run the runtime binary build worker.");
180
+ }
181
+ function currentCompileTarget() {
182
+ if (process.platform === "darwin") {
183
+ return process.arch === "arm64" ? "bun-darwin-arm64" : "bun-darwin-x64";
184
+ }
185
+ if (process.platform === "linux") {
186
+ return process.arch === "arm64" ? "bun-linux-arm64" : "bun-linux-x64";
187
+ }
188
+ return "bun-windows-x64";
189
+ }
190
+ function createRuntimeBinaryBuildKey(input) {
191
+ return JSON.stringify({
192
+ version: 1,
193
+ bunVersion: Bun.version,
194
+ platform: process.platform,
195
+ arch: process.arch,
196
+ entrypoint: input.entrypoint,
197
+ define: sortRecord(input.define),
198
+ external: input.external ? [...input.external].sort() : undefined,
199
+ env: sortRecord(input.env)
200
+ });
201
+ }
202
+ async function isRuntimeBinaryBuildFresh(input) {
203
+ if (!existsSync(input.outputPath) || !existsSync(input.manifestPath)) {
204
+ return false;
205
+ }
206
+ let manifest = null;
207
+ try {
208
+ manifest = await Bun.file(input.manifestPath).json();
209
+ } catch {
210
+ return false;
211
+ }
212
+ if (!manifest || manifest.version !== 1 || manifest.buildKey !== input.buildKey) {
213
+ return false;
214
+ }
215
+ for (const [filePath, expectedDigest] of Object.entries(manifest.inputs || {})) {
216
+ if (!existsSync(filePath)) {
217
+ return false;
218
+ }
219
+ if (await sha256File(filePath) !== expectedDigest) {
220
+ return false;
221
+ }
222
+ }
223
+ return true;
224
+ }
225
+ async function writeRuntimeBinaryCacheManifest(input) {
226
+ const inputs = {};
227
+ for (const inputPath of Object.keys(input.metafile?.inputs || {}).sort()) {
228
+ const normalized = normalizeBuildInputPath(input.cwd, inputPath);
229
+ if (!normalized || !existsSync(normalized)) {
230
+ continue;
231
+ }
232
+ inputs[normalized] = await sha256File(normalized);
233
+ }
234
+ const manifest = {
235
+ version: 1,
236
+ buildKey: input.buildKey,
237
+ inputs
238
+ };
239
+ await Bun.write(input.manifestPath, `${JSON.stringify(manifest, null, 2)}
240
+ `);
241
+ }
242
+ function normalizeBuildInputPath(cwd, inputPath) {
243
+ if (!inputPath) {
244
+ return null;
245
+ }
246
+ if (inputPath.startsWith("file://")) {
247
+ return fileURLToPath(inputPath);
248
+ }
249
+ if (inputPath.startsWith("<")) {
250
+ return null;
251
+ }
252
+ return resolve2(cwd, inputPath);
253
+ }
254
+ async function sha256File(path) {
255
+ const hasher = new Bun.CryptoHasher("sha256");
256
+ hasher.update(await Bun.file(path).arrayBuffer());
257
+ return hasher.digest("hex");
258
+ }
259
+ function sortRecord(value) {
260
+ if (!value) {
261
+ return;
262
+ }
263
+ return Object.fromEntries(Object.entries(value).sort(([left], [right]) => left.localeCompare(right)));
264
+ }
265
+ async function runSerializedRuntimeBinaryBuild(action) {
266
+ const previous = runtimeBinaryBuildQueue;
267
+ let release;
268
+ runtimeBinaryBuildQueue = new Promise((resolve3) => {
269
+ release = resolve3;
270
+ });
271
+ await previous;
272
+ try {
273
+ return await action();
274
+ } finally {
275
+ release();
276
+ }
277
+ }
278
+ async function withTemporaryEnv(env, action) {
279
+ if (!env) {
280
+ return action();
281
+ }
282
+ const previousValues = new Map;
283
+ for (const [key, value] of Object.entries(env)) {
284
+ previousValues.set(key, process.env[key]);
285
+ if (value === undefined) {
286
+ delete process.env[key];
287
+ } else {
288
+ process.env[key] = value;
289
+ }
290
+ }
291
+ try {
292
+ return await action();
293
+ } finally {
294
+ for (const [key, value] of previousValues.entries()) {
295
+ if (value === undefined) {
296
+ delete process.env[key];
297
+ } else {
298
+ process.env[key] = value;
299
+ }
300
+ }
301
+ }
302
+ }
303
+ async function withTemporaryCwd(cwd, action) {
304
+ const previousCwd = process.cwd();
305
+ process.chdir(cwd);
306
+ try {
307
+ return await action();
308
+ } finally {
309
+ process.chdir(previousCwd);
310
+ }
311
+ }
312
+
313
+ // packages/isolation-plugin/src/isolation/binary-build-worker.ts
314
+ var payloadPath = process.argv[2];
315
+ if (!payloadPath) {
316
+ console.error("usage: binary-build-worker <payload.json>");
317
+ process.exit(2);
318
+ }
319
+ var payload = await Bun.file(payloadPath).json();
320
+ await buildRuntimeBinary({
321
+ ...payload,
322
+ outputPath: payload.outputPath
323
+ });
@@ -0,0 +1,7 @@
1
+ import type { AgentRuntime } from "./types";
2
+ export declare function listAgentRuntimes(projectRoot: string): Promise<AgentRuntime[]>;
3
+ export declare function runtimeRootForCleanup(projectRoot: string, runtimeId: string): {
4
+ monorepoRoot: string | null;
5
+ initialWorkspaceDir: string;
6
+ runtimeRoot: string;
7
+ };