@h-rig/runtime 0.0.6-alpha.0

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 (176) hide show
  1. package/README.md +27 -0
  2. package/dist/bin/rig-agent-dispatch.js +9615 -0
  3. package/dist/bin/rig-agent.js +9512 -0
  4. package/dist/bin/rig-browser-tool.js +269 -0
  5. package/dist/src/agent-mode.js +48 -0
  6. package/dist/src/baked-secrets.js +121 -0
  7. package/dist/src/binary-build-worker.js +312 -0
  8. package/dist/src/binary-run.js +540 -0
  9. package/dist/src/boundaries.js +1 -0
  10. package/dist/src/build-time-config.js +25 -0
  11. package/dist/src/control-plane/agent-roles.js +27 -0
  12. package/dist/src/control-plane/agent-wrapper.js +9621 -0
  13. package/dist/src/control-plane/authority-files.js +582 -0
  14. package/dist/src/control-plane/browser-contract.js +135 -0
  15. package/dist/src/control-plane/controlled-bash.js +1111 -0
  16. package/dist/src/control-plane/errors.js +13 -0
  17. package/dist/src/control-plane/harness-main.js +10828 -0
  18. package/dist/src/control-plane/hook-materializer.js +75 -0
  19. package/dist/src/control-plane/hooks/audit-trail.js +353 -0
  20. package/dist/src/control-plane/hooks/completion-verification.js +7552 -0
  21. package/dist/src/control-plane/hooks/import-guard.js +890 -0
  22. package/dist/src/control-plane/hooks/inject-context.js +4189 -0
  23. package/dist/src/control-plane/hooks/post-edit-lint.js +43 -0
  24. package/dist/src/control-plane/hooks/safety-guard.js +910 -0
  25. package/dist/src/control-plane/hooks/scope-guard.js +907 -0
  26. package/dist/src/control-plane/hooks/shared.js +44 -0
  27. package/dist/src/control-plane/hooks/submodule-branch.js +7797 -0
  28. package/dist/src/control-plane/hooks/task-runtime-start.js +7799 -0
  29. package/dist/src/control-plane/hooks/test-integrity-guard.js +891 -0
  30. package/dist/src/control-plane/materialize-task-config.js +453 -0
  31. package/dist/src/control-plane/memory-sync/cli.js +2019 -0
  32. package/dist/src/control-plane/memory-sync/db.js +753 -0
  33. package/dist/src/control-plane/memory-sync/embed.js +281 -0
  34. package/dist/src/control-plane/memory-sync/index.js +2049 -0
  35. package/dist/src/control-plane/memory-sync/query.js +294 -0
  36. package/dist/src/control-plane/memory-sync/read.js +784 -0
  37. package/dist/src/control-plane/memory-sync/types.js +6 -0
  38. package/dist/src/control-plane/memory-sync/write.js +1547 -0
  39. package/dist/src/control-plane/native/git-native.js +490 -0
  40. package/dist/src/control-plane/native/git-ops.js +2860 -0
  41. package/dist/src/control-plane/native/harness-cli.js +9721 -0
  42. package/dist/src/control-plane/native/pr-automation.js +373 -0
  43. package/dist/src/control-plane/native/profile-ops.js +481 -0
  44. package/dist/src/control-plane/native/repo-ops.js +2342 -0
  45. package/dist/src/control-plane/native/root-resolver.js +66 -0
  46. package/dist/src/control-plane/native/run-ops.js +3281 -0
  47. package/dist/src/control-plane/native/runtime-native-sidecar.js +299 -0
  48. package/dist/src/control-plane/native/runtime-native.js +392 -0
  49. package/dist/src/control-plane/native/scope-rules.js +17 -0
  50. package/dist/src/control-plane/native/task-ops.js +6320 -0
  51. package/dist/src/control-plane/native/task-state.js +1512 -0
  52. package/dist/src/control-plane/native/utils.js +535 -0
  53. package/dist/src/control-plane/native/validator-binaries.js +889 -0
  54. package/dist/src/control-plane/native/validator.js +2197 -0
  55. package/dist/src/control-plane/native/verifier.js +3249 -0
  56. package/dist/src/control-plane/native/workspace-ops.js +1635 -0
  57. package/dist/src/control-plane/plugin-host-context.js +334 -0
  58. package/dist/src/control-plane/project-main-pre-run-sync.js +630 -0
  59. package/dist/src/control-plane/provider/claude-stream-records.js +158 -0
  60. package/dist/src/control-plane/provider/codex-app-server.js +885 -0
  61. package/dist/src/control-plane/provider/codex-exec-records.js +203 -0
  62. package/dist/src/control-plane/provider/rig-task-run-skill.js +39 -0
  63. package/dist/src/control-plane/provider/runtime-instructions.js +96 -0
  64. package/dist/src/control-plane/remote.js +854 -0
  65. package/dist/src/control-plane/repos/index.js +473 -0
  66. package/dist/src/control-plane/repos/layout.js +124 -0
  67. package/dist/src/control-plane/repos/mirror/bootstrap.js +268 -0
  68. package/dist/src/control-plane/repos/mirror/refresh.js +398 -0
  69. package/dist/src/control-plane/repos/mirror/state.js +167 -0
  70. package/dist/src/control-plane/repos/registry.js +77 -0
  71. package/dist/src/control-plane/repos/types.js +1 -0
  72. package/dist/src/control-plane/runtime/agent-mode.js +48 -0
  73. package/dist/src/control-plane/runtime/baked-secrets.js +120 -0
  74. package/dist/src/control-plane/runtime/claude-tool-router-binary.js +343 -0
  75. package/dist/src/control-plane/runtime/claude-tool-router.js +520 -0
  76. package/dist/src/control-plane/runtime/context.js +216 -0
  77. package/dist/src/control-plane/runtime/events.js +218 -0
  78. package/dist/src/control-plane/runtime/guard-types.js +6 -0
  79. package/dist/src/control-plane/runtime/guard.js +880 -0
  80. package/dist/src/control-plane/runtime/image/fingerprint-sidecar.js +1194 -0
  81. package/dist/src/control-plane/runtime/image/index.js +2255 -0
  82. package/dist/src/control-plane/runtime/image-fingerprint-sidecar.js +1191 -0
  83. package/dist/src/control-plane/runtime/image.js +2255 -0
  84. package/dist/src/control-plane/runtime/index.js +8511 -0
  85. package/dist/src/control-plane/runtime/isolation/discovery.js +599 -0
  86. package/dist/src/control-plane/runtime/isolation/home.js +1217 -0
  87. package/dist/src/control-plane/runtime/isolation/index.js +8193 -0
  88. package/dist/src/control-plane/runtime/isolation/runner.js +2651 -0
  89. package/dist/src/control-plane/runtime/isolation/shared.js +501 -0
  90. package/dist/src/control-plane/runtime/isolation/toolchain.js +1892 -0
  91. package/dist/src/control-plane/runtime/isolation/types.js +1 -0
  92. package/dist/src/control-plane/runtime/isolation/worktree.js +509 -0
  93. package/dist/src/control-plane/runtime/isolation.js +8193 -0
  94. package/dist/src/control-plane/runtime/overlay.js +67 -0
  95. package/dist/src/control-plane/runtime/plugin-mode.js +41 -0
  96. package/dist/src/control-plane/runtime/plugins.js +1131 -0
  97. package/dist/src/control-plane/runtime/provisioning-env.js +220 -0
  98. package/dist/src/control-plane/runtime/queue.js +8358 -0
  99. package/dist/src/control-plane/runtime/rig-shell.js +205 -0
  100. package/dist/src/control-plane/runtime/rig-tools.js +182 -0
  101. package/dist/src/control-plane/runtime/runner-context.js +1 -0
  102. package/dist/src/control-plane/runtime/runtime-paths.js +184 -0
  103. package/dist/src/control-plane/runtime/sandbox/backend-bwrap.js +311 -0
  104. package/dist/src/control-plane/runtime/sandbox/backend-none.js +21 -0
  105. package/dist/src/control-plane/runtime/sandbox/backend-seatbelt.js +268 -0
  106. package/dist/src/control-plane/runtime/sandbox/backend.js +1718 -0
  107. package/dist/src/control-plane/runtime/sandbox/orchestrator.js +1745 -0
  108. package/dist/src/control-plane/runtime/sandbox/utils.js +137 -0
  109. package/dist/src/control-plane/runtime/sandbox-backend-bwrap.js +311 -0
  110. package/dist/src/control-plane/runtime/sandbox-backend-none.js +21 -0
  111. package/dist/src/control-plane/runtime/sandbox-backend-seatbelt.js +268 -0
  112. package/dist/src/control-plane/runtime/sandbox-backend.js +1718 -0
  113. package/dist/src/control-plane/runtime/sandbox-orchestrator.js +1745 -0
  114. package/dist/src/control-plane/runtime/sandbox-utils.js +137 -0
  115. package/dist/src/control-plane/runtime/snapshot/index.js +454 -0
  116. package/dist/src/control-plane/runtime/snapshot/sidecar.js +502 -0
  117. package/dist/src/control-plane/runtime/snapshot/task-run.js +1578 -0
  118. package/dist/src/control-plane/runtime/snapshot-sidecar.js +498 -0
  119. package/dist/src/control-plane/runtime/snapshot.js +454 -0
  120. package/dist/src/control-plane/runtime/task-run-snapshot.js +1578 -0
  121. package/dist/src/control-plane/runtime/tool-gateway.js +422 -0
  122. package/dist/src/control-plane/runtime/tooling/browser-tools.js +32 -0
  123. package/dist/src/control-plane/runtime/tooling/claude-router-binary.js +343 -0
  124. package/dist/src/control-plane/runtime/tooling/claude-router.js +524 -0
  125. package/dist/src/control-plane/runtime/tooling/file-tools.js +182 -0
  126. package/dist/src/control-plane/runtime/tooling/gateway.js +422 -0
  127. package/dist/src/control-plane/runtime/tooling/index.js +1290 -0
  128. package/dist/src/control-plane/runtime/tooling/shell.js +205 -0
  129. package/dist/src/control-plane/runtime/types.js +1 -0
  130. package/dist/src/control-plane/setup-version.js +14 -0
  131. package/dist/src/control-plane/state-sync/index.js +1509 -0
  132. package/dist/src/control-plane/state-sync/read.js +856 -0
  133. package/dist/src/control-plane/state-sync/reconcile.js +260 -0
  134. package/dist/src/control-plane/state-sync/repo.js +302 -0
  135. package/dist/src/control-plane/state-sync/types.js +111 -0
  136. package/dist/src/control-plane/state-sync/write.js +1469 -0
  137. package/dist/src/control-plane/task-fields.js +38 -0
  138. package/dist/src/control-plane/task-source-bootstrap.js +46 -0
  139. package/dist/src/control-plane/task-source.js +30 -0
  140. package/dist/src/control-plane/tasks/legacy-task-config-source.js +130 -0
  141. package/dist/src/control-plane/tasks/plugin-task-source.js +103 -0
  142. package/dist/src/control-plane/tasks/source-aware-task-config-source.js +611 -0
  143. package/dist/src/control-plane/tasks/source-lifecycle.js +1093 -0
  144. package/dist/src/control-plane/tasks/task-record-reader.js +9 -0
  145. package/dist/src/control-plane/validators/boundary/public-apis.js +107 -0
  146. package/dist/src/control-plane/validators/integration/_shared.js +51 -0
  147. package/dist/src/control-plane/validators/integration/adm-audit-http.js +85 -0
  148. package/dist/src/control-plane/validators/integration/adm-auth-http.js +78 -0
  149. package/dist/src/control-plane/validators/integration/adm-issuer-http.js +80 -0
  150. package/dist/src/control-plane/validators/integration/adm-migration.js +78 -0
  151. package/dist/src/control-plane/validators/integration/adm-scaffold.js +78 -0
  152. package/dist/src/control-plane/validators/runtime-registration.js +64 -0
  153. package/dist/src/control-plane/validators/shared.js +683 -0
  154. package/dist/src/events.js +218 -0
  155. package/dist/src/execution.js +35 -0
  156. package/dist/src/index.js +1633 -0
  157. package/dist/src/layout.js +145 -0
  158. package/dist/src/local-server.js +202 -0
  159. package/dist/src/plugins.js +329 -0
  160. package/dist/src/remote-http.js +83 -0
  161. package/dist/src/runtime-context.js +216 -0
  162. package/dist/src/types.js +1 -0
  163. package/native/darwin-arm64/bin/rig-git +0 -0
  164. package/native/darwin-arm64/bin/rig-shell +0 -0
  165. package/native/darwin-arm64/bin/rig-tools +0 -0
  166. package/native/darwin-arm64/lib/runtime-native-darwin-arm64.dylib +0 -0
  167. package/native/darwin-arm64/lib/runtime-native.dylib +0 -0
  168. package/native/darwin-arm64/manifest.json +1 -0
  169. package/native/linux-x64/bin/rig-git +0 -0
  170. package/native/linux-x64/bin/rig-shell +0 -0
  171. package/native/linux-x64/bin/rig-tools +0 -0
  172. package/native/linux-x64/lib/runtime-native-linux-x64.so +0 -0
  173. package/native/linux-x64/lib/runtime-native.so +0 -0
  174. package/native/linux-x64/manifest.json +1 -0
  175. package/package.json +74 -0
  176. package/skills/rig-task-run.md +71 -0
@@ -0,0 +1,205 @@
1
+ // @bun
2
+ // packages/runtime/src/control-plane/runtime/tooling/shell.ts
3
+ import { chmodSync, copyFileSync, existsSync, mkdirSync } from "fs";
4
+ import { tmpdir } from "os";
5
+ import { basename, dirname, resolve } from "path";
6
+ var sharedNativeShellOutputDir = resolve(tmpdir(), "rig-native");
7
+ var sharedNativeShellOutputPath = resolve(sharedNativeShellOutputDir, `rig-shell-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
8
+ function runtimeRigShellFileName() {
9
+ return `rig-shell${process.platform === "win32" ? ".exe" : ""}`;
10
+ }
11
+ async function ensureRigShellBinaryPath(outputPath = sharedNativeShellOutputPath) {
12
+ const sourcePath = resolveRigShellSourcePath();
13
+ if (!sourcePath) {
14
+ const bundledBinary = resolveBundledRigShellBinaryPath();
15
+ if (bundledBinary) {
16
+ return bundledBinary;
17
+ }
18
+ throw new Error("rig-shell.zig source file not found.");
19
+ }
20
+ const zigBinary = Bun.which("zig");
21
+ if (!zigBinary) {
22
+ throw new Error("zig is required to build the native Rig shell.");
23
+ }
24
+ mkdirSync(dirname(outputPath), { recursive: true });
25
+ const sourceDigest = await sha256File(sourcePath);
26
+ const buildKey = JSON.stringify({
27
+ version: 1,
28
+ zigBinary,
29
+ platform: process.platform,
30
+ arch: process.arch,
31
+ sourcePath,
32
+ sourceDigest
33
+ });
34
+ const manifestPath = nativeBuildManifestPath(outputPath);
35
+ const needsBuild = !existsSync(outputPath) || !await hasMatchingNativeBuildManifest(manifestPath, buildKey);
36
+ if (!needsBuild) {
37
+ return outputPath;
38
+ }
39
+ const build = Bun.spawn([
40
+ zigBinary,
41
+ "build-exe",
42
+ sourcePath,
43
+ "-O",
44
+ "ReleaseFast",
45
+ `-femit-bin=${outputPath}`
46
+ ], {
47
+ cwd: dirname(sourcePath),
48
+ stdout: "pipe",
49
+ stderr: "pipe"
50
+ });
51
+ const [exitCode, stdout, stderr] = await Promise.all([
52
+ build.exited,
53
+ new Response(build.stdout).text(),
54
+ new Response(build.stderr).text()
55
+ ]);
56
+ if (exitCode !== 0 || !existsSync(outputPath)) {
57
+ const details = [stderr.trim(), stdout.trim()].filter(Boolean).join(`
58
+ `);
59
+ throw new Error(`Failed to build native Rig shell: ${details || `zig exited with code ${exitCode}`}`);
60
+ }
61
+ await Bun.write(manifestPath, `${JSON.stringify({ version: 1, buildKey }, null, 2)}
62
+ `);
63
+ return outputPath;
64
+ }
65
+ async function materializeRigShellBinary(targetDir) {
66
+ const sourcePath = await ensureRigShellBinaryPath();
67
+ const targetPath = resolve(targetDir, runtimeRigShellFileName());
68
+ mkdirSync(targetDir, { recursive: true });
69
+ const sourceDigest = await sha256File(sourcePath);
70
+ const buildKey = JSON.stringify({
71
+ version: 1,
72
+ sourcePath,
73
+ sourceDigest
74
+ });
75
+ const needsCopy = !existsSync(targetPath) || !await hasMatchingNativeBuildManifest(nativeBuildManifestPath(targetPath), buildKey);
76
+ if (needsCopy) {
77
+ copyFileSync(sourcePath, targetPath);
78
+ chmodSync(targetPath, 493);
79
+ await Bun.write(nativeBuildManifestPath(targetPath), `${JSON.stringify({ version: 1, buildKey }, null, 2)}
80
+ `);
81
+ }
82
+ return targetPath;
83
+ }
84
+ function resolveRigShellSourcePath() {
85
+ for (const candidate of rigShellSourceCandidates()) {
86
+ if (candidate && existsSync(candidate)) {
87
+ return candidate;
88
+ }
89
+ }
90
+ return null;
91
+ }
92
+ function resolveBundledRigShellBinaryPath() {
93
+ for (const candidate of rigShellBinaryCandidates()) {
94
+ if (candidate && existsSync(candidate)) {
95
+ return candidate;
96
+ }
97
+ }
98
+ return null;
99
+ }
100
+ function rigShellSourceCandidates() {
101
+ const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
102
+ const cwd = process.cwd()?.trim() || "";
103
+ const projectRoot = process.env.PROJECT_RIG_ROOT?.trim() || "";
104
+ const hostProjectRoot = process.env.RIG_HOST_PROJECT_ROOT?.trim() || "";
105
+ return [...new Set([
106
+ process.env.RIG_NATIVE_SHELL_SOURCE?.trim() || "",
107
+ cwd ? resolve(cwd, "packages/runtime/native/rig-shell.zig") : "",
108
+ projectRoot ? resolve(projectRoot, "packages/runtime/native/rig-shell.zig") : "",
109
+ hostProjectRoot ? resolve(hostProjectRoot, "packages/runtime/native/rig-shell.zig") : "",
110
+ execDir ? resolve(execDir, "..", "..", "packages/runtime/native/rig-shell.zig") : "",
111
+ execDir ? resolve(execDir, "..", "native", "rig-shell.zig") : "",
112
+ resolve(import.meta.dir, "../../../../native/rig-shell.zig")
113
+ ].filter(Boolean))];
114
+ }
115
+ function nativePackageBinaryCandidates(fromDir, fileName) {
116
+ const candidates = [];
117
+ let cursor = resolve(fromDir);
118
+ for (let index = 0;index < 8; index += 1) {
119
+ candidates.push(resolve(cursor, "native", `${process.platform}-${process.arch}`, fileName), resolve(cursor, "native", `${process.platform}-${process.arch}`, "bin", fileName), resolve(cursor, "native", fileName), resolve(cursor, "native", "bin", fileName));
120
+ const parent = dirname(cursor);
121
+ if (parent === cursor)
122
+ break;
123
+ cursor = parent;
124
+ }
125
+ return candidates;
126
+ }
127
+ function rigShellBinaryCandidates() {
128
+ const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
129
+ const fileName = runtimeRigShellFileName();
130
+ return [...new Set([
131
+ process.env.RIG_NATIVE_SHELL_BIN?.trim() || "",
132
+ ...nativePackageBinaryCandidates(import.meta.dir, fileName),
133
+ execDir ? resolve(execDir, fileName) : "",
134
+ execDir ? resolve(execDir, "..", fileName) : "",
135
+ execDir ? resolve(execDir, "..", "bin", fileName) : ""
136
+ ].filter(Boolean))];
137
+ }
138
+ function runtimeToolGatewayNames() {
139
+ return [
140
+ "bash",
141
+ "sh",
142
+ "zsh",
143
+ "git",
144
+ "bun",
145
+ "node",
146
+ "python3",
147
+ "rg",
148
+ "grep",
149
+ "sed",
150
+ "cat",
151
+ "ls",
152
+ "find",
153
+ "tsc",
154
+ "gh",
155
+ "mkdir",
156
+ "rm",
157
+ "mv",
158
+ "cp",
159
+ "touch",
160
+ "pwd",
161
+ "head",
162
+ "tail",
163
+ "wc",
164
+ "sort",
165
+ "uniq",
166
+ "awk",
167
+ "xargs",
168
+ "dirname",
169
+ "basename",
170
+ "realpath",
171
+ "env",
172
+ "jq",
173
+ "tee",
174
+ "which"
175
+ ];
176
+ }
177
+ function shellBinaryBasename(path) {
178
+ return basename(path);
179
+ }
180
+ function nativeBuildManifestPath(outputPath) {
181
+ return `${outputPath}.build-manifest.json`;
182
+ }
183
+ async function hasMatchingNativeBuildManifest(manifestPath, buildKey) {
184
+ if (!existsSync(manifestPath)) {
185
+ return false;
186
+ }
187
+ try {
188
+ const manifest = await Bun.file(manifestPath).json();
189
+ return manifest.version === 1 && manifest.buildKey === buildKey;
190
+ } catch {
191
+ return false;
192
+ }
193
+ }
194
+ async function sha256File(path) {
195
+ const hasher = new Bun.CryptoHasher("sha256");
196
+ hasher.update(await Bun.file(path).arrayBuffer());
197
+ return hasher.digest("hex");
198
+ }
199
+ export {
200
+ shellBinaryBasename,
201
+ runtimeToolGatewayNames,
202
+ runtimeRigShellFileName,
203
+ materializeRigShellBinary,
204
+ ensureRigShellBinaryPath
205
+ };
@@ -0,0 +1,182 @@
1
+ // @bun
2
+ // packages/runtime/src/control-plane/runtime/tooling/file-tools.ts
3
+ import { chmodSync, copyFileSync, existsSync, mkdirSync, rmSync, symlinkSync } from "fs";
4
+ import { tmpdir } from "os";
5
+ import { basename, dirname, resolve } from "path";
6
+ var sharedNativeToolsOutputDir = resolve(tmpdir(), "rig-native");
7
+ var sharedNativeToolsOutputPath = resolve(sharedNativeToolsOutputDir, `rig-tools-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
8
+ function runtimeRigToolsFileName() {
9
+ return `rig-tools${process.platform === "win32" ? ".exe" : ""}`;
10
+ }
11
+ function runtimeFileToolNames() {
12
+ return [
13
+ "rig-read",
14
+ "rig-write",
15
+ "rig-edit",
16
+ "rig-glob",
17
+ "rig-grep"
18
+ ];
19
+ }
20
+ async function ensureRigToolsBinaryPath(outputPath = sharedNativeToolsOutputPath) {
21
+ const sourcePath = resolveRigToolsSourcePath();
22
+ if (!sourcePath) {
23
+ const bundledBinary = resolveBundledRigToolsBinaryPath();
24
+ if (bundledBinary) {
25
+ return bundledBinary;
26
+ }
27
+ throw new Error("rig-tools.zig source file not found.");
28
+ }
29
+ const zigBinary = Bun.which("zig");
30
+ if (!zigBinary) {
31
+ throw new Error("zig is required to build native Rig file tools.");
32
+ }
33
+ mkdirSync(dirname(outputPath), { recursive: true });
34
+ const sourceDigest = await sha256File(sourcePath);
35
+ const buildKey = JSON.stringify({
36
+ version: 1,
37
+ zigBinary,
38
+ platform: process.platform,
39
+ arch: process.arch,
40
+ sourcePath,
41
+ sourceDigest
42
+ });
43
+ const manifestPath = nativeBuildManifestPath(outputPath);
44
+ const needsBuild = !existsSync(outputPath) || !await hasMatchingNativeBuildManifest(manifestPath, buildKey);
45
+ if (!needsBuild) {
46
+ return outputPath;
47
+ }
48
+ const build = Bun.spawn([
49
+ zigBinary,
50
+ "build-exe",
51
+ sourcePath,
52
+ "-O",
53
+ "ReleaseFast",
54
+ `-femit-bin=${outputPath}`
55
+ ], {
56
+ cwd: dirname(sourcePath),
57
+ stdout: "pipe",
58
+ stderr: "pipe"
59
+ });
60
+ const [exitCode, stdout, stderr] = await Promise.all([
61
+ build.exited,
62
+ new Response(build.stdout).text(),
63
+ new Response(build.stderr).text()
64
+ ]);
65
+ if (exitCode !== 0 || !existsSync(outputPath)) {
66
+ const details = [stderr.trim(), stdout.trim()].filter(Boolean).join(`
67
+ `);
68
+ throw new Error(`Failed to build native Rig file tools: ${details || `zig exited with code ${exitCode}`}`);
69
+ }
70
+ await Bun.write(manifestPath, `${JSON.stringify({ version: 1, buildKey }, null, 2)}
71
+ `);
72
+ return outputPath;
73
+ }
74
+ async function materializeRuntimeFileTools(targetDir) {
75
+ const sourcePath = await ensureRigToolsBinaryPath();
76
+ const targetPath = resolve(targetDir, runtimeRigToolsFileName());
77
+ mkdirSync(targetDir, { recursive: true });
78
+ const sourceDigest = await sha256File(sourcePath);
79
+ const buildKey = JSON.stringify({
80
+ version: 1,
81
+ sourcePath,
82
+ sourceDigest
83
+ });
84
+ const needsCopy = !existsSync(targetPath) || !await hasMatchingNativeBuildManifest(nativeBuildManifestPath(targetPath), buildKey);
85
+ if (needsCopy) {
86
+ copyFileSync(sourcePath, targetPath);
87
+ chmodSync(targetPath, 493);
88
+ await Bun.write(nativeBuildManifestPath(targetPath), `${JSON.stringify({ version: 1, buildKey }, null, 2)}
89
+ `);
90
+ }
91
+ for (const tool of runtimeFileToolNames()) {
92
+ const toolPath = resolve(targetDir, tool);
93
+ if (existsSync(toolPath)) {
94
+ rmSync(toolPath, { force: true, recursive: true });
95
+ }
96
+ symlinkSync(targetPath, toolPath);
97
+ }
98
+ return targetPath;
99
+ }
100
+ function resolveRigToolsSourcePath() {
101
+ for (const candidate of rigToolsSourceCandidates()) {
102
+ if (candidate && existsSync(candidate)) {
103
+ return candidate;
104
+ }
105
+ }
106
+ return null;
107
+ }
108
+ function resolveBundledRigToolsBinaryPath() {
109
+ for (const candidate of rigToolsBinaryCandidates()) {
110
+ if (candidate && existsSync(candidate)) {
111
+ return candidate;
112
+ }
113
+ }
114
+ return null;
115
+ }
116
+ function rigToolsSourceCandidates() {
117
+ const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
118
+ const cwd = process.cwd()?.trim() || "";
119
+ const projectRoot = process.env.PROJECT_RIG_ROOT?.trim() || "";
120
+ const hostProjectRoot = process.env.RIG_HOST_PROJECT_ROOT?.trim() || "";
121
+ return [...new Set([
122
+ process.env.RIG_NATIVE_TOOLS_SOURCE?.trim() || "",
123
+ cwd ? resolve(cwd, "packages/runtime/native/rig-tools.zig") : "",
124
+ projectRoot ? resolve(projectRoot, "packages/runtime/native/rig-tools.zig") : "",
125
+ hostProjectRoot ? resolve(hostProjectRoot, "packages/runtime/native/rig-tools.zig") : "",
126
+ execDir ? resolve(execDir, "..", "..", "packages/runtime/native/rig-tools.zig") : "",
127
+ execDir ? resolve(execDir, "..", "native", "rig-tools.zig") : "",
128
+ resolve(import.meta.dir, "../../../../native/rig-tools.zig")
129
+ ].filter(Boolean))];
130
+ }
131
+ function nativePackageBinaryCandidates(fromDir, fileName) {
132
+ const candidates = [];
133
+ let cursor = resolve(fromDir);
134
+ for (let index = 0;index < 8; index += 1) {
135
+ candidates.push(resolve(cursor, "native", `${process.platform}-${process.arch}`, fileName), resolve(cursor, "native", `${process.platform}-${process.arch}`, "bin", fileName), resolve(cursor, "native", fileName), resolve(cursor, "native", "bin", fileName));
136
+ const parent = dirname(cursor);
137
+ if (parent === cursor)
138
+ break;
139
+ cursor = parent;
140
+ }
141
+ return candidates;
142
+ }
143
+ function rigToolsBinaryCandidates() {
144
+ const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
145
+ const fileName = runtimeRigToolsFileName();
146
+ return [...new Set([
147
+ process.env.RIG_NATIVE_TOOLS_BIN?.trim() || "",
148
+ ...nativePackageBinaryCandidates(import.meta.dir, fileName),
149
+ execDir ? resolve(execDir, fileName) : "",
150
+ execDir ? resolve(execDir, "..", fileName) : "",
151
+ execDir ? resolve(execDir, "..", "bin", fileName) : ""
152
+ ].filter(Boolean))];
153
+ }
154
+ function runtimeFileToolBasename(path) {
155
+ return basename(path);
156
+ }
157
+ function nativeBuildManifestPath(outputPath) {
158
+ return `${outputPath}.build-manifest.json`;
159
+ }
160
+ async function hasMatchingNativeBuildManifest(manifestPath, buildKey) {
161
+ if (!existsSync(manifestPath)) {
162
+ return false;
163
+ }
164
+ try {
165
+ const manifest = await Bun.file(manifestPath).json();
166
+ return manifest.version === 1 && manifest.buildKey === buildKey;
167
+ } catch {
168
+ return false;
169
+ }
170
+ }
171
+ async function sha256File(path) {
172
+ const hasher = new Bun.CryptoHasher("sha256");
173
+ hasher.update(await Bun.file(path).arrayBuffer());
174
+ return hasher.digest("hex");
175
+ }
176
+ export {
177
+ runtimeRigToolsFileName,
178
+ runtimeFileToolNames,
179
+ runtimeFileToolBasename,
180
+ materializeRuntimeFileTools,
181
+ ensureRigToolsBinaryPath
182
+ };
@@ -0,0 +1,184 @@
1
+ // @bun
2
+ // packages/runtime/src/control-plane/runtime/runtime-paths.ts
3
+ import { existsSync, readdirSync, realpathSync } from "fs";
4
+ import { resolve } from "path";
5
+
6
+ // packages/runtime/src/control-plane/runtime/sandbox/utils.ts
7
+ function uniq(values) {
8
+ return [...new Set(values)];
9
+ }
10
+ // packages/runtime/src/control-plane/runtime/runtime-paths.ts
11
+ function resolveBunBinaryPath() {
12
+ const explicit = normalizeExecutablePath(process.env.RIG_BUN_PATH?.trim());
13
+ if (explicit) {
14
+ return explicit;
15
+ }
16
+ const pathBun = normalizeExecutablePath(Bun.which("bun")?.trim());
17
+ if (pathBun && !looksLikeRuntimeGateway(pathBun)) {
18
+ return pathBun;
19
+ }
20
+ const home = process.env.HOME?.trim();
21
+ const fallbackCandidates = [
22
+ home ? resolve(home, ".bun/bin/bun") : "",
23
+ "/opt/homebrew/bin/bun",
24
+ "/usr/local/bin/bun",
25
+ "/usr/bin/bun"
26
+ ];
27
+ for (const candidate of fallbackCandidates) {
28
+ const normalized = normalizeExecutablePath(candidate);
29
+ if (normalized) {
30
+ return normalized;
31
+ }
32
+ }
33
+ const execPath = normalizeExecutablePath(process.execPath?.trim());
34
+ if (execPath && !looksLikeRuntimeGateway(execPath)) {
35
+ return execPath;
36
+ }
37
+ throw new Error("bun not found in PATH");
38
+ }
39
+ function resolveClaudeBinaryPath() {
40
+ const explicit = normalizeExecutablePath(process.env.RIG_CLAUDE_PATH?.trim());
41
+ if (explicit) {
42
+ return explicit;
43
+ }
44
+ const pathClaude = normalizeExecutablePath(Bun.which("claude")?.trim());
45
+ if (pathClaude && !looksLikeRuntimeGateway(pathClaude)) {
46
+ return pathClaude;
47
+ }
48
+ const home = process.env.HOME?.trim();
49
+ const fallbackCandidates = [
50
+ home ? resolve(home, ".local/bin/claude") : "",
51
+ home ? resolve(home, ".local/share/claude/local/claude") : "",
52
+ "/opt/homebrew/bin/claude",
53
+ "/usr/local/bin/claude",
54
+ "/usr/bin/claude"
55
+ ];
56
+ for (const candidate of fallbackCandidates) {
57
+ const normalized = normalizeExecutablePath(candidate);
58
+ if (normalized) {
59
+ return normalized;
60
+ }
61
+ }
62
+ throw new Error("claude not found in PATH");
63
+ }
64
+ function resolveBunInstallDir(bunBinaryPath = resolveBunBinaryPath()) {
65
+ return resolve(bunBinaryPath, "../..");
66
+ }
67
+ function resolveClaudeInstallDir() {
68
+ const realPath = resolveClaudeBinaryPath();
69
+ return resolve(realPath, "..");
70
+ }
71
+ function resolveNodeInstallDir() {
72
+ const preferredNode = resolvePreferredNodeBinary();
73
+ if (!preferredNode)
74
+ return null;
75
+ const explicitNode = process.env.RIG_NODE_BIN?.trim();
76
+ if (explicitNode && resolve(explicitNode) === resolve(preferredNode)) {
77
+ return preferredNode.endsWith("/bin/node") ? resolve(preferredNode, "../..") : resolve(preferredNode, "..");
78
+ }
79
+ try {
80
+ const realPath = realpathSync(preferredNode);
81
+ if (realPath.endsWith("/bin/node")) {
82
+ return resolve(realPath, "../..");
83
+ }
84
+ return resolve(realPath, "..");
85
+ } catch {
86
+ return resolve(preferredNode, "..");
87
+ }
88
+ }
89
+ function resolveRuntimeDependencyRoots(runtimeDirs) {
90
+ const roots = [];
91
+ if (process.platform === "darwin") {
92
+ for (const macPath of ["/opt/homebrew", "/opt/homebrew/opt"]) {
93
+ if (existsSync(macPath)) {
94
+ roots.push(macPath);
95
+ }
96
+ }
97
+ }
98
+ for (const dir of runtimeDirs) {
99
+ if (dir.startsWith("/opt/homebrew/Cellar/")) {
100
+ roots.push("/opt/homebrew/opt");
101
+ } else if (dir.startsWith("/usr/local/Cellar/")) {
102
+ roots.push("/usr/local/opt");
103
+ }
104
+ }
105
+ return uniq(roots);
106
+ }
107
+ function resolvePreferredNodeBinary() {
108
+ const candidates = [];
109
+ const envNode = process.env.RIG_NODE_BIN?.trim();
110
+ if (envNode) {
111
+ const explicit = resolve(envNode);
112
+ if (existsSync(explicit)) {
113
+ return explicit;
114
+ }
115
+ }
116
+ const nvmBin = process.env.NVM_BIN?.trim();
117
+ if (nvmBin) {
118
+ candidates.push(resolve(nvmBin, "node"));
119
+ }
120
+ const home = process.env.HOME?.trim();
121
+ if (home) {
122
+ const nvmVersionsDir = resolve(home, ".nvm/versions/node");
123
+ if (existsSync(nvmVersionsDir)) {
124
+ try {
125
+ const versionDirs = readdirSync(nvmVersionsDir).map((entry) => entry.trim()).filter((entry) => /^v\d+\.\d+\.\d+$/.test(entry)).sort((a, b) => Bun.semver.order(b.replace(/^v/, ""), a.replace(/^v/, "")));
126
+ for (const versionDir of versionDirs) {
127
+ candidates.push(resolve(nvmVersionsDir, versionDir, "bin/node"));
128
+ }
129
+ } catch {}
130
+ }
131
+ }
132
+ const whichNode = Bun.which("node");
133
+ if (whichNode) {
134
+ candidates.push(whichNode);
135
+ }
136
+ const deduped = uniq(candidates.map((candidate) => resolve(candidate)));
137
+ const existing = deduped.filter((candidate) => existsSync(candidate));
138
+ if (existing.length === 0) {
139
+ return null;
140
+ }
141
+ const stable = existing.find((candidate) => {
142
+ const major = inferNodeMajor(candidate);
143
+ return typeof major === "number" && major >= 18 && major <= 24;
144
+ });
145
+ if (stable) {
146
+ return stable;
147
+ }
148
+ return existing[0] ?? null;
149
+ }
150
+ function inferNodeMajor(nodeBinaryPath) {
151
+ const normalized = resolve(nodeBinaryPath).replace(/\\/g, "/");
152
+ const match = normalized.match(/(?:^|\/)(?:node-)?v?(\d+)\.\d+\.\d+(?:\/|$)/);
153
+ if (!match) {
154
+ return null;
155
+ }
156
+ const major = Number.parseInt(match[1], 10);
157
+ return Number.isFinite(major) ? major : null;
158
+ }
159
+ function normalizeExecutablePath(candidate) {
160
+ if (!candidate) {
161
+ return "";
162
+ }
163
+ const normalized = resolve(candidate);
164
+ if (!existsSync(normalized)) {
165
+ return "";
166
+ }
167
+ try {
168
+ return realpathSync(normalized);
169
+ } catch {
170
+ return normalized;
171
+ }
172
+ }
173
+ function looksLikeRuntimeGateway(candidate) {
174
+ const normalized = resolve(candidate).replace(/\\/g, "/");
175
+ return normalized.includes("/.rig/bin/") || normalized.endsWith("/rig-shell") || normalized.endsWith("/rig-agent");
176
+ }
177
+ export {
178
+ resolveRuntimeDependencyRoots,
179
+ resolveNodeInstallDir,
180
+ resolveClaudeInstallDir,
181
+ resolveClaudeBinaryPath,
182
+ resolveBunInstallDir,
183
+ resolveBunBinaryPath
184
+ };