@h-rig/isolation-plugin 0.0.6-alpha.157 → 0.0.6-alpha.159

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,283 @@
1
+ // @bun
2
+ // packages/isolation-plugin/src/isolation/shared.ts
3
+ import { existsSync, readFileSync, rmSync } from "fs";
4
+ import { resolve } from "path";
5
+ import { agentId, safeGitRefComponent, taskRuntimeId } from "@rig/core/safe-identifiers";
6
+ import { resolveCheckoutRoot } from "@rig/core/checkout-root";
7
+ function isRuntimeGatewayGitPath(candidate) {
8
+ return /\/\.rig\/bin\/git$/.test(candidate.replace(/\\/g, "/"));
9
+ }
10
+ function isRuntimeGatewayGhPath(candidate) {
11
+ return /\/\.rig\/bin\/gh$/.test(candidate.replace(/\\/g, "/"));
12
+ }
13
+ function resolveHostGitBinary() {
14
+ const candidates = [
15
+ process.env.RIG_GIT_BIN?.trim() || "",
16
+ "/usr/bin/git",
17
+ "/opt/homebrew/bin/git",
18
+ "/usr/local/bin/git"
19
+ ];
20
+ const bunResolved = Bun.which("git");
21
+ if (bunResolved && !isRuntimeGatewayGitPath(bunResolved)) {
22
+ candidates.push(bunResolved);
23
+ }
24
+ for (const candidate of candidates) {
25
+ if (candidate && !isRuntimeGatewayGitPath(candidate) && existsSync(candidate)) {
26
+ return candidate;
27
+ }
28
+ }
29
+ return "git";
30
+ }
31
+ function resolveGithubCliBinary(options = {}) {
32
+ const candidates = new Set;
33
+ const explicit = process.env.RIG_GH_BIN?.trim();
34
+ if (explicit) {
35
+ candidates.add(explicit);
36
+ }
37
+ for (const candidate of ["/usr/bin/gh", "/opt/homebrew/bin/gh", "/usr/local/bin/gh"]) {
38
+ candidates.add(candidate);
39
+ }
40
+ if (options.scanPath) {
41
+ for (const entry of (process.env.PATH || "").split(":").map((value) => value.trim()).filter(Boolean)) {
42
+ candidates.add(resolve(entry, "gh"));
43
+ }
44
+ }
45
+ const bunResolved = Bun.which("gh");
46
+ if (bunResolved) {
47
+ candidates.add(bunResolved);
48
+ }
49
+ for (const candidate of candidates) {
50
+ if (candidate && existsSync(candidate) && !isRuntimeGatewayGhPath(candidate)) {
51
+ return candidate;
52
+ }
53
+ }
54
+ return "";
55
+ }
56
+ var generatedCredentialFiles = new Set;
57
+ var credentialCleanupRegistered = false;
58
+ function resolveMonorepoRoot(projectRoot) {
59
+ return resolveCheckoutRoot(projectRoot);
60
+ }
61
+ async function runGitCommand(repoRoot, args) {
62
+ const gitBinary = resolveHostGitBinary();
63
+ return Bun.$`${gitBinary} -C ${repoRoot} ${args}`.quiet().nothrow();
64
+ }
65
+ async function readGitConfigValue(repoRoot, key, global = false) {
66
+ const args = ["config", ...global ? ["--global"] : [], "--get", key];
67
+ const result = await runGitCommand(repoRoot, args);
68
+ if (result.exitCode !== 0) {
69
+ return "";
70
+ }
71
+ return String(result.stdout).trim();
72
+ }
73
+ async function readGitStdout(repoRoot, args) {
74
+ const result = await runGitCommand(repoRoot, args);
75
+ if (result.exitCode !== 0) {
76
+ throw new Error(`git -C ${repoRoot} ${args.join(" ")} failed: ${result.stderr || result.stdout}`);
77
+ }
78
+ return String(result.stdout).trim();
79
+ }
80
+ async function hasGitRemote(repoRoot, remote) {
81
+ const result = await runGitCommand(repoRoot, ["remote", "get-url", remote]);
82
+ return result.exitCode === 0;
83
+ }
84
+ async function ensureFullGitHistory(repoRoot) {
85
+ const shallow = await runGitCommand(repoRoot, ["rev-parse", "--is-shallow-repository"]);
86
+ if (shallow.exitCode !== 0 || String(shallow.stdout).trim() !== "true") {
87
+ return;
88
+ }
89
+ const unshallow = await runGitCommand(repoRoot, ["fetch", "--unshallow", "--tags", "origin"]);
90
+ if (unshallow.exitCode === 0) {
91
+ return;
92
+ }
93
+ const output = `${unshallow.stderr}
94
+ ${unshallow.stdout}`.trim();
95
+ if (/--unshallow on a complete repository|does not make sense/i.test(output)) {
96
+ return;
97
+ }
98
+ throw new Error(`Failed to expand git history for ${repoRoot}: ${output}`);
99
+ }
100
+ async function refreshRemoteBranch(repoRoot, remote, branch) {
101
+ if (!await hasGitRemote(repoRoot, remote)) {
102
+ return;
103
+ }
104
+ try {
105
+ await ensureFullGitHistory(repoRoot);
106
+ const fetch = await runGitCommand(repoRoot, [
107
+ "fetch",
108
+ "--prune",
109
+ "--tags",
110
+ remote,
111
+ `+refs/heads/${branch}:refs/remotes/${remote}/${branch}`
112
+ ]);
113
+ if (fetch.exitCode !== 0) {
114
+ return;
115
+ }
116
+ } catch {
117
+ return;
118
+ }
119
+ }
120
+ async function tryReadGitHead(repoRoot) {
121
+ if (!existsSync(resolve(repoRoot, ".git"))) {
122
+ return;
123
+ }
124
+ const result = await runGitCommand(repoRoot, ["rev-parse", "HEAD"]);
125
+ if (result.exitCode !== 0) {
126
+ return;
127
+ }
128
+ const value = String(result.stdout).trim();
129
+ return value || undefined;
130
+ }
131
+ async function captureRepoDirtyFiles(repoRoot) {
132
+ if (!existsSync(resolve(repoRoot, ".git"))) {
133
+ return [];
134
+ }
135
+ const files = new Set;
136
+ for (const args of [
137
+ ["diff", "--name-only"],
138
+ ["diff", "--cached", "--name-only"],
139
+ ["ls-files", "--others", "--exclude-standard"]
140
+ ]) {
141
+ const result = await runGitCommand(repoRoot, args);
142
+ if (result.exitCode !== 0) {
143
+ continue;
144
+ }
145
+ for (const line of String(result.stdout).split(/\r?\n/)) {
146
+ const trimmed = line.trim();
147
+ if (trimmed) {
148
+ files.add(trimmed);
149
+ }
150
+ }
151
+ }
152
+ return [...files].sort();
153
+ }
154
+ function sha256Hex(input) {
155
+ const hasher = new Bun.CryptoHasher("sha256");
156
+ hasher.update(input);
157
+ return hasher.digest("hex");
158
+ }
159
+ function registerCredentialCleanup(path) {
160
+ generatedCredentialFiles.add(path);
161
+ if (credentialCleanupRegistered) {
162
+ return;
163
+ }
164
+ credentialCleanupRegistered = true;
165
+ const cleanup = () => {
166
+ for (const filePath of generatedCredentialFiles) {
167
+ try {
168
+ rmSync(filePath, { force: true });
169
+ } catch {}
170
+ }
171
+ generatedCredentialFiles.clear();
172
+ };
173
+ process.on("exit", cleanup);
174
+ process.on("beforeExit", cleanup);
175
+ }
176
+ async function captureStdout(fn) {
177
+ const chunks = [];
178
+ const originalWrite = process.stdout.write.bind(process.stdout);
179
+ const originalLog = console.log;
180
+ const originalError = console.error;
181
+ process.stdout.write = (chunk, encoding, cb) => {
182
+ chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString(typeof encoding === "string" ? encoding : undefined));
183
+ const callback = typeof encoding === "function" ? encoding : cb;
184
+ callback?.(null);
185
+ return true;
186
+ };
187
+ console.log = (...args) => {
188
+ chunks.push(`${args.map((value) => String(value)).join(" ")}
189
+ `);
190
+ };
191
+ console.error = (...args) => {
192
+ chunks.push(`${args.map((value) => String(value)).join(" ")}
193
+ `);
194
+ };
195
+ try {
196
+ await fn();
197
+ return chunks.join("");
198
+ } finally {
199
+ process.stdout.write = originalWrite;
200
+ console.log = originalLog;
201
+ console.error = originalError;
202
+ }
203
+ }
204
+ function sanitizeRuntimeRefSegment(value) {
205
+ return safeGitRefComponent(value, { fallback: "runtime", maxLength: 64 });
206
+ }
207
+ function runtimeBranchBackupName(branch) {
208
+ const branchId = branch.replace(/^rig\//, "");
209
+ return `rig-backup/${sanitizeRuntimeRefSegment(branchId)}-${Date.now()}`;
210
+ }
211
+ function hashProjectPath(workspaceDir) {
212
+ return sha256Hex(workspaceDir).slice(0, 16);
213
+ }
214
+ async function resolveGithubCliAuthToken(ghBinary = "") {
215
+ const gh = ghBinary || resolveGithubCliBinary();
216
+ if (!gh) {
217
+ return "";
218
+ }
219
+ const auth = Bun.spawn([gh, "auth", "token"], {
220
+ stdout: "pipe",
221
+ stderr: "pipe"
222
+ });
223
+ const [exitCode, stdout] = await Promise.all([
224
+ auth.exited,
225
+ new Response(auth.stdout).text()
226
+ ]);
227
+ if (exitCode !== 0) {
228
+ return "";
229
+ }
230
+ return stdout.trim();
231
+ }
232
+ function resolveSystemCertBundlePath() {
233
+ const candidates = [
234
+ process.env.SSL_CERT_FILE?.trim(),
235
+ "/etc/ssl/cert.pem",
236
+ "/private/etc/ssl/cert.pem",
237
+ "/opt/homebrew/etc/openssl@3/cert.pem"
238
+ ];
239
+ for (const candidate of candidates) {
240
+ if (candidate && existsSync(candidate)) {
241
+ return resolve(candidate);
242
+ }
243
+ }
244
+ return "";
245
+ }
246
+ var __testOnly = {
247
+ cleanupGeneratedCredentialFiles() {
248
+ generatedCredentialFiles.clear();
249
+ }
250
+ };
251
+ function readKnownHosts(path) {
252
+ if (!existsSync(path)) {
253
+ return new Set;
254
+ }
255
+ return new Set(readFileSync(path, "utf-8").split(/\r?\n/).map((line) => line.trim()).filter(Boolean));
256
+ }
257
+ export {
258
+ tryReadGitHead,
259
+ taskRuntimeId,
260
+ sha256Hex,
261
+ sanitizeRuntimeRefSegment,
262
+ runtimeBranchBackupName,
263
+ runGitCommand,
264
+ resolveSystemCertBundlePath,
265
+ resolveMonorepoRoot,
266
+ resolveHostGitBinary,
267
+ resolveGithubCliBinary,
268
+ resolveGithubCliAuthToken,
269
+ registerCredentialCleanup,
270
+ refreshRemoteBranch,
271
+ readKnownHosts,
272
+ readGitStdout,
273
+ readGitConfigValue,
274
+ isRuntimeGatewayGitPath,
275
+ isRuntimeGatewayGhPath,
276
+ hashProjectPath,
277
+ hasGitRemote,
278
+ ensureFullGitHistory,
279
+ captureStdout,
280
+ captureRepoDirtyFiles,
281
+ agentId,
282
+ __testOnly
283
+ };
@@ -0,0 +1,71 @@
1
+ import { buildBinary } from "./runtime-binary-build";
2
+ /**
3
+ * Resolve the per-run hook binary specs + the controlled-bash source path from
4
+ * the toolchain-source contributions of the loaded plugins. Returns the merged
5
+ * hook list (guard + lifecycle) and the named standalone sources.
6
+ */
7
+ export declare function resolveContributedToolchainSources(projectRoot: string): Promise<{
8
+ hookSources: ReadonlyArray<readonly [name: string, source: string]>;
9
+ namedSources: Record<string, string>;
10
+ }>;
11
+ import type { RuntimeTaskEntry } from "./types";
12
+ export type RuntimeToolchainConfig = {
13
+ projectRoot: string;
14
+ workspaceDir: string;
15
+ binDir: string;
16
+ distDir: string;
17
+ taskId: string;
18
+ runtimeId: string;
19
+ manifestPath: string;
20
+ taskEntry: RuntimeTaskEntry;
21
+ bakedScopeHash: string;
22
+ bakedInfoOutput: string;
23
+ bakedDepsOutput: string;
24
+ bakedStatusOutput: string;
25
+ runtimeSecretDefines: Record<string, string>;
26
+ logsDir: string;
27
+ };
28
+ export declare function prepareTrackedRuntimePaths(logsDir: string, stateDir: string, sessionDir: string): void;
29
+ export declare function initializeRuntimeStateFiles(stateDir: string, sessionDir: string, taskId: string): Promise<void>;
30
+ export declare function resetEphemeralTaskArtifacts(workspaceDir: string, taskId: string): Promise<void>;
31
+ export declare function prepareRuntimeWorkspace(projectRoot: string, workspaceDir: string): void;
32
+ export declare function materializeRuntimeHostToolWrappers(binDir: string): Promise<void>;
33
+ export declare function buildRuntimeToolchain(options: RuntimeToolchainConfig): Promise<void>;
34
+ export declare function writeRuntimeManifest(config: {
35
+ runtimeRoot: string;
36
+ workspaceDir: string;
37
+ runtimeId: string;
38
+ taskId: string;
39
+ scopes: string[];
40
+ binaryPath: string;
41
+ binDir: string;
42
+ }): Promise<void>;
43
+ export { buildBinary };
44
+ export declare function syncRuntimeWorkspaceSources(projectRoot: string, workspaceDir: string): void;
45
+ declare function restoreTrackedArtifactPathWithRetry(workspaceDir: string, relativePath: string, restorePath: () => Promise<{
46
+ exitCode: number;
47
+ output: string;
48
+ }>, options?: {
49
+ sleep?: (ms: number) => Promise<unknown>;
50
+ maxRetries?: number;
51
+ retryDelayMs?: number;
52
+ now?: () => number;
53
+ statMtimeMs?: (path: string) => number | null;
54
+ removeFile?: (path: string) => void;
55
+ log?: (message: string) => void;
56
+ }): Promise<void>;
57
+ declare function isGitIndexLockError(output: string): boolean;
58
+ declare function parseGitIndexLockPath(output: string): string | null;
59
+ declare function tryClearStaleGitIndexLock(lockPath: string, options: {
60
+ now: () => number;
61
+ statMtimeMs: (path: string) => number | null;
62
+ removeFile: (path: string) => void;
63
+ log: (message: string) => void;
64
+ }): boolean;
65
+ export declare function runtimeWorktreeId(workspaceDir: string): string;
66
+ export declare const __testOnly: {
67
+ isGitIndexLockError: typeof isGitIndexLockError;
68
+ parseGitIndexLockPath: typeof parseGitIndexLockPath;
69
+ restoreTrackedArtifactPathWithRetry: typeof restoreTrackedArtifactPathWithRetry;
70
+ tryClearStaleGitIndexLock: typeof tryClearStaleGitIndexLock;
71
+ };