@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,481 @@
1
+ // @bun
2
+ // packages/runtime/src/control-plane/native/profile-ops.ts
3
+ import { existsSync as existsSync3, mkdirSync as mkdirSync2, writeFileSync } from "fs";
4
+
5
+ // packages/runtime/src/control-plane/native/utils.ts
6
+ import { resolve as resolve3 } from "path";
7
+
8
+ // packages/runtime/src/layout.ts
9
+ import { existsSync } from "fs";
10
+ import { basename, dirname, resolve } from "path";
11
+ var RIG_DEFINITION_DIRNAME = "rig";
12
+ var RIG_ARTIFACTS_DIRNAME = "artifacts";
13
+ function resolveMonorepoRoot(projectRoot) {
14
+ const normalizedProjectRoot = resolve(projectRoot);
15
+ const explicit = process.env.MONOREPO_ROOT?.trim();
16
+ if (explicit) {
17
+ const explicitRoot = resolve(explicit);
18
+ const explicitParent = dirname(explicitRoot);
19
+ if (basename(explicitParent) === ".worktrees") {
20
+ const owner = dirname(explicitParent);
21
+ const ownerHasGit = existsSync(resolve(owner, ".git"));
22
+ const ownerHasTaskConfig = existsSync(resolve(owner, ".rig", "task-config.json"));
23
+ const ownerHasRigConfig = existsSync(resolve(owner, "rig.config.ts"));
24
+ if (ownerHasGit && (ownerHasTaskConfig || ownerHasRigConfig)) {
25
+ return owner;
26
+ }
27
+ throw new Error(`MONOREPO_ROOT points to worktree ${explicitRoot}, but the owner checkout is incomplete at ${owner}.`);
28
+ }
29
+ if (!existsSync(resolve(explicitRoot, ".git"))) {
30
+ throw new Error(`MONOREPO_ROOT points to ${explicitRoot}, but no git checkout was found there.`);
31
+ }
32
+ const hasTaskConfig = existsSync(resolve(explicitRoot, ".rig", "task-config.json"));
33
+ const hasRigConfig = existsSync(resolve(explicitRoot, "rig.config.ts"));
34
+ if (!hasTaskConfig && !hasRigConfig) {
35
+ throw new Error(`MONOREPO_ROOT points to ${explicitRoot}, but neither .rig/task-config.json nor rig.config.ts exists there.`);
36
+ }
37
+ return explicitRoot;
38
+ }
39
+ const projectParent = dirname(normalizedProjectRoot);
40
+ if (basename(projectParent) === ".worktrees") {
41
+ const worktreeOwner = dirname(projectParent);
42
+ const ownerHasGit = existsSync(resolve(worktreeOwner, ".git"));
43
+ const ownerHasTaskConfig = existsSync(resolve(worktreeOwner, ".rig", "task-config.json"));
44
+ const ownerHasRigConfig = existsSync(resolve(worktreeOwner, "rig.config.ts"));
45
+ if (ownerHasGit && (ownerHasTaskConfig || ownerHasRigConfig)) {
46
+ return worktreeOwner;
47
+ }
48
+ }
49
+ return normalizedProjectRoot;
50
+ }
51
+ function resolveRuntimeWorkspaceLayout(workspaceDir) {
52
+ const root = resolve(workspaceDir);
53
+ const rigRoot = resolve(root, ".rig");
54
+ const logsDir = resolve(rigRoot, "logs");
55
+ const stateDir = resolve(rigRoot, "state");
56
+ const runtimeDir = resolve(rigRoot, "runtime");
57
+ const binDir = resolve(rigRoot, "bin");
58
+ return {
59
+ workspaceDir: root,
60
+ rigRoot,
61
+ stateDir,
62
+ logsDir,
63
+ artifactsRoot: resolve(root, RIG_ARTIFACTS_DIRNAME),
64
+ runtimeDir,
65
+ homeDir: resolve(rigRoot, "home"),
66
+ tmpDir: resolve(rigRoot, "tmp"),
67
+ cacheDir: resolve(rigRoot, "cache"),
68
+ sessionDir: resolve(rigRoot, "session"),
69
+ binDir,
70
+ distDir: resolve(rigRoot, "dist"),
71
+ pluginBinDir: resolve(binDir, "plugins"),
72
+ contextPath: resolve(rigRoot, "runtime-context.json"),
73
+ controlPlaneEventsFile: resolve(logsDir, "control-plane.events.jsonl")
74
+ };
75
+ }
76
+ function resolveActiveRuntimeWorkspaceRoot(monorepoRoot) {
77
+ const explicit = process.env.RIG_TASK_WORKSPACE?.trim();
78
+ if (!explicit) {
79
+ throw new Error("No active runtime workspace. Set RIG_TASK_WORKSPACE or provision a task runtime first.");
80
+ }
81
+ return resolve(explicit);
82
+ }
83
+ function resolveRigLayout(projectRoot) {
84
+ const monorepoRoot = resolveMonorepoRoot(projectRoot);
85
+ const definitionRoot = resolve(projectRoot, RIG_DEFINITION_DIRNAME);
86
+ const runtimeWorkspaceRoot = resolveActiveRuntimeWorkspaceRoot(monorepoRoot);
87
+ const runtimeLayout = resolveRuntimeWorkspaceLayout(runtimeWorkspaceRoot);
88
+ const policyDir = resolve(definitionRoot, "policy");
89
+ return {
90
+ projectRoot,
91
+ monorepoRoot,
92
+ definitionRoot,
93
+ runtimeWorkspaceRoot,
94
+ stateRoot: runtimeLayout.rigRoot,
95
+ artifactsRoot: runtimeLayout.artifactsRoot,
96
+ configPath: resolve(definitionRoot, "config.sh"),
97
+ taskConfigPath: resolve(runtimeWorkspaceRoot, ".rig", "task-config.json"),
98
+ policyDir,
99
+ policyFile: resolve(policyDir, "policy.json"),
100
+ pluginsDir: resolve(definitionRoot, "plugins"),
101
+ hooksDir: resolve(definitionRoot, "hooks"),
102
+ toolsDir: resolve(definitionRoot, "tools"),
103
+ templatesDir: resolve(definitionRoot, "templates"),
104
+ validationDir: resolve(definitionRoot, "validation"),
105
+ stateDir: runtimeLayout.stateDir,
106
+ logsDir: runtimeLayout.logsDir,
107
+ notificationsDir: resolve(definitionRoot, "notifications"),
108
+ runtimeDir: runtimeLayout.runtimeDir,
109
+ distDir: runtimeLayout.distDir,
110
+ binDir: runtimeLayout.binDir,
111
+ pluginBinDir: runtimeLayout.pluginBinDir,
112
+ keybindingsPath: resolve(definitionRoot, "keybindings.json"),
113
+ controlPlaneEventsFile: runtimeLayout.controlPlaneEventsFile
114
+ };
115
+ }
116
+
117
+ // packages/runtime/src/control-plane/native/runtime-native.ts
118
+ import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
119
+ import { copyFileSync, existsSync as existsSync2, mkdirSync, renameSync, rmSync, statSync } from "fs";
120
+ import { tmpdir } from "os";
121
+ import { dirname as dirname2, resolve as resolve2 } from "path";
122
+ var sharedNativeRuntimeOutputDir = resolve2(tmpdir(), "rig-native");
123
+ var sharedNativeRuntimeOutputPath = resolve2(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
124
+ var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
125
+ var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
126
+ async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
127
+ if (await buildNativeRuntimeLibrary(outputPath, options)) {
128
+ return outputPath;
129
+ }
130
+ return !options.force && existsSync2(outputPath) ? outputPath : null;
131
+ }
132
+ async function loadNativeRuntimeLibrary() {
133
+ if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
134
+ return null;
135
+ }
136
+ for (const candidate of nativeRuntimeLibraryCandidates()) {
137
+ if (!candidate || !existsSync2(candidate)) {
138
+ continue;
139
+ }
140
+ const loaded = tryDlopenNativeRuntimeLibrary(candidate);
141
+ if (loaded) {
142
+ return loaded;
143
+ }
144
+ }
145
+ const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
146
+ if (!builtLibraryPath) {
147
+ return null;
148
+ }
149
+ return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
150
+ }
151
+ function nativePackageLibraryCandidates(fromDir, names) {
152
+ const candidates = [];
153
+ let cursor = resolve2(fromDir);
154
+ for (let index = 0;index < 8; index += 1) {
155
+ for (const name of names) {
156
+ 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));
157
+ }
158
+ const parent = dirname2(cursor);
159
+ if (parent === cursor)
160
+ break;
161
+ cursor = parent;
162
+ }
163
+ return candidates;
164
+ }
165
+ function nativeRuntimeLibraryCandidates() {
166
+ const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
167
+ const execDir = process.execPath?.trim() ? dirname2(process.execPath.trim()) : "";
168
+ const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
169
+ return [...new Set([
170
+ explicit,
171
+ ...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
172
+ execDir ? resolve2(execDir, colocatedNativeRuntimeFileName) : "",
173
+ execDir ? resolve2(execDir, platformSpecific) : "",
174
+ execDir ? resolve2(execDir, "..", colocatedNativeRuntimeFileName) : "",
175
+ execDir ? resolve2(execDir, "..", platformSpecific) : "",
176
+ execDir ? resolve2(execDir, "lib", colocatedNativeRuntimeFileName) : "",
177
+ execDir ? resolve2(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
178
+ sharedNativeRuntimeOutputPath
179
+ ].filter(Boolean))];
180
+ }
181
+ function resolveNativeRuntimeSourcePath() {
182
+ const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
183
+ if (explicit && existsSync2(explicit)) {
184
+ return explicit;
185
+ }
186
+ const bundled = resolve2(import.meta.dir, "../../../native/snapshot.zig");
187
+ return existsSync2(bundled) ? bundled : null;
188
+ }
189
+ async function buildNativeRuntimeLibrary(outputPath, options = {}) {
190
+ if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
191
+ return false;
192
+ }
193
+ const zigBinary = Bun.which("zig");
194
+ const sourcePath = resolveNativeRuntimeSourcePath();
195
+ if (!zigBinary || !sourcePath) {
196
+ return false;
197
+ }
198
+ const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
199
+ try {
200
+ mkdirSync(dirname2(outputPath), { recursive: true });
201
+ const needsBuild = options.force === true || !existsSync2(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
202
+ if (!needsBuild) {
203
+ return true;
204
+ }
205
+ const build = Bun.spawn([
206
+ zigBinary,
207
+ "build-lib",
208
+ sourcePath,
209
+ "-dynamic",
210
+ "-O",
211
+ "ReleaseFast",
212
+ `-femit-bin=${tempOutputPath}`
213
+ ], {
214
+ cwd: import.meta.dir,
215
+ stdout: "pipe",
216
+ stderr: "pipe"
217
+ });
218
+ const exitCode = await build.exited;
219
+ if (exitCode !== 0 || !existsSync2(tempOutputPath)) {
220
+ rmSync(tempOutputPath, { force: true });
221
+ return false;
222
+ }
223
+ renameSync(tempOutputPath, outputPath);
224
+ return true;
225
+ } catch {
226
+ rmSync(tempOutputPath, { force: true });
227
+ return false;
228
+ }
229
+ }
230
+ function tryDlopenNativeRuntimeLibrary(outputPath) {
231
+ try {
232
+ return dlopen(outputPath, {
233
+ rig_scope_match: {
234
+ args: ["ptr", "ptr"],
235
+ returns: "u8"
236
+ },
237
+ snapshot_capture: {
238
+ args: ["ptr", "u64", "ptr", "u64"],
239
+ returns: "ptr"
240
+ },
241
+ snapshot_delta: {
242
+ args: ["ptr", "ptr"],
243
+ returns: "ptr"
244
+ },
245
+ snapshot_store_delta: {
246
+ args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
247
+ returns: "ptr"
248
+ },
249
+ snapshot_inspect_delta: {
250
+ args: ["ptr", "u64"],
251
+ returns: "ptr"
252
+ },
253
+ snapshot_apply_delta: {
254
+ args: ["ptr", "u64", "ptr", "u64"],
255
+ returns: "ptr"
256
+ },
257
+ snapshot_release: {
258
+ args: ["ptr"],
259
+ returns: "void"
260
+ },
261
+ runtime_hash_file: {
262
+ args: ["ptr", "u64"],
263
+ returns: "ptr"
264
+ },
265
+ runtime_hash_tree: {
266
+ args: ["ptr", "u64"],
267
+ returns: "ptr"
268
+ },
269
+ runtime_prepare_paths: {
270
+ args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
271
+ returns: "ptr"
272
+ },
273
+ runtime_link_dependency_layer: {
274
+ args: ["ptr", "u64", "ptr", "u64"],
275
+ returns: "ptr"
276
+ },
277
+ runtime_scan_worktrees: {
278
+ args: ["ptr", "u64"],
279
+ returns: "ptr"
280
+ }
281
+ });
282
+ } catch {
283
+ return null;
284
+ }
285
+ }
286
+
287
+ // packages/runtime/src/control-plane/native/utils.ts
288
+ function resolveMonorepoRoot2(projectRoot) {
289
+ return resolveMonorepoRoot(projectRoot);
290
+ }
291
+ var scopeRegexCache = new Map;
292
+ function resolveHarnessPaths(projectRoot) {
293
+ const hasRuntimeWorkspace = Boolean(process.env.RIG_TASK_WORKSPACE?.trim());
294
+ const monorepoRoot = resolveMonorepoRoot2(projectRoot);
295
+ const harnessRoot = resolve3(projectRoot, "rig");
296
+ const stateRoot = resolve3(projectRoot, ".rig");
297
+ const layout = hasRuntimeWorkspace ? resolveRigLayout(projectRoot) : null;
298
+ const stateDir = layout?.stateDir ?? resolve3(stateRoot, "state");
299
+ const logsDir = layout?.logsDir ?? resolve3(stateRoot, "logs");
300
+ const artifactsDir = layout?.artifactsRoot ?? resolve3(monorepoRoot, "artifacts");
301
+ const taskConfigPath = layout?.taskConfigPath ?? resolve3(monorepoRoot, ".rig", "task-config.json");
302
+ const binDir = layout?.binDir ?? resolve3(stateRoot, "bin");
303
+ return {
304
+ harnessRoot,
305
+ stateDir: process.env.RIG_STATE_DIR || stateDir,
306
+ artifactsDir,
307
+ logsDir: process.env.RIG_LOGS_DIR || logsDir,
308
+ binDir,
309
+ hooksDir: resolve3(harnessRoot, "hooks"),
310
+ validationDir: resolve3(harnessRoot, "validation"),
311
+ taskConfigPath,
312
+ sessionPath: process.env.RIG_SESSION_FILE || resolve3(stateRoot, "session", "session.json"),
313
+ monorepoRoot,
314
+ tsApiTestsDir: process.env.TS_API_TESTS_DIR || resolve3(monorepoRoot, "TSAPITests"),
315
+ taskRepoCommitsPath: resolve3(stateDir, "task-repo-commits.json"),
316
+ baseRepoPinsPath: resolve3(stateDir, "base-repo-pins.json"),
317
+ failedApproachesPath: resolve3(stateDir, "failed_approaches.md"),
318
+ agentProfilePath: resolve3(stateDir, "agent-profile.json"),
319
+ reviewProfilePath: resolve3(stateDir, "review-profile.json")
320
+ };
321
+ }
322
+
323
+ // packages/runtime/src/control-plane/native/profile-ops.ts
324
+ var DEFAULTS = {
325
+ model: parseEnvOrDefault(process.env.DEFAULT_AGENT_MODEL, ["claude", "gpt-codex", "pi"], "pi"),
326
+ runtime: parseEnvOrDefault(process.env.DEFAULT_AGENT_RUNTIME, ["claude-code", "codex-app-server", "pi"], "pi"),
327
+ plugin: parseEnvOrDefault(process.env.DEFAULT_AGENT_PLUGIN, ["claude", "codex", "pi"], "pi"),
328
+ reviewMode: parseEnvOrDefault(process.env.AI_REVIEW_MODE, ["off", "advisory", "required"], "advisory"),
329
+ reviewProvider: parseEnvOrDefault(process.env.AI_REVIEW_PROVIDER, ["greptile"], "greptile")
330
+ };
331
+ function parseEnvOrDefault(value, allowed, fallback) {
332
+ if (!value) {
333
+ return fallback;
334
+ }
335
+ return allowed.includes(value) ? value : fallback;
336
+ }
337
+ async function readProfileFile(path) {
338
+ if (!existsSync3(path)) {
339
+ return null;
340
+ }
341
+ try {
342
+ return await Bun.file(path).json();
343
+ } catch {
344
+ return null;
345
+ }
346
+ }
347
+ async function showProfile(projectRoot, compact = false) {
348
+ const paths = resolveHarnessPaths(projectRoot);
349
+ const profile = await loadProfile(paths.agentProfilePath);
350
+ if (compact) {
351
+ console.log(`model=${profile.model} runtime=${profile.runtime} plugin=${profile.agent_plugin}`);
352
+ return;
353
+ }
354
+ console.log("Execution Profile:");
355
+ console.log(` model: ${profile.model}`);
356
+ console.log(` runtime: ${profile.runtime}`);
357
+ console.log(` plugin: ${profile.agent_plugin}`);
358
+ }
359
+ async function setProfile(projectRoot, options) {
360
+ let model = options.model;
361
+ let runtime = normalizeRuntimeAlias(options.runtime);
362
+ let plugin = options.plugin;
363
+ if (options.preset === "claude-code") {
364
+ model = "claude";
365
+ runtime = "claude-code";
366
+ } else if (options.preset === "codex-cli" || options.preset === "codex-app-server") {
367
+ model = "gpt-codex";
368
+ runtime = "codex-app-server";
369
+ } else if (options.preset === "pi" || options.preset === "rig-pi" || options.preset === "@rig/pi") {
370
+ model = "pi";
371
+ runtime = "pi";
372
+ }
373
+ if (!model && runtime) {
374
+ model = runtime === "codex-app-server" ? "gpt-codex" : runtime === "pi" ? "pi" : "claude";
375
+ }
376
+ if (!runtime && model) {
377
+ runtime = model === "gpt-codex" ? "codex-app-server" : model === "pi" ? "pi" : "claude-code";
378
+ }
379
+ model = model || DEFAULTS.model;
380
+ runtime = runtime || DEFAULTS.runtime;
381
+ if (model !== "claude" && model !== "gpt-codex" && model !== "pi") {
382
+ throw new Error(`Unsupported model: ${model}. Use claude, gpt-codex, or pi.`);
383
+ }
384
+ if (runtime !== "claude-code" && runtime !== "codex-app-server" && runtime !== "pi") {
385
+ throw new Error(`Unsupported runtime: ${runtime}. Use claude-code, codex-app-server, or pi.`);
386
+ }
387
+ if (!plugin) {
388
+ plugin = model === "gpt-codex" ? "codex" : model === "pi" ? "pi" : "claude";
389
+ }
390
+ const paths = resolveHarnessPaths(projectRoot);
391
+ mkdirSync2(paths.stateDir, { recursive: true });
392
+ const next = {
393
+ model,
394
+ runtime,
395
+ agent_plugin: plugin,
396
+ updated_at: new Date().toISOString()
397
+ };
398
+ writeFileSync(paths.agentProfilePath, `${JSON.stringify(next, null, 2)}
399
+ `, "utf-8");
400
+ await showProfile(projectRoot, false);
401
+ }
402
+ function normalizeRuntimeAlias(value) {
403
+ if (!value) {
404
+ return;
405
+ }
406
+ if (value === "codex-cli" || value === "codex-app-server") {
407
+ return "codex-app-server";
408
+ }
409
+ if (value === "pi" || value === "rig-pi" || value === "@rig/pi") {
410
+ return "pi";
411
+ }
412
+ if (value === "claude-code") {
413
+ return "claude-code";
414
+ }
415
+ return value;
416
+ }
417
+ async function showReviewProfile(projectRoot) {
418
+ const paths = resolveHarnessPaths(projectRoot);
419
+ const profile = await loadReviewProfile(paths.reviewProfilePath);
420
+ console.log("AI Review Profile:");
421
+ console.log(` mode: ${profile.mode}`);
422
+ console.log(` provider: ${profile.provider}`);
423
+ console.log(` file: ${paths.reviewProfilePath}`);
424
+ }
425
+ async function setReviewProfile(projectRoot, mode, provider) {
426
+ if (mode !== "off" && mode !== "advisory" && mode !== "required") {
427
+ throw new Error(`Invalid mode: ${mode}. Use off|advisory|required.`);
428
+ }
429
+ const resolvedProvider = provider || DEFAULTS.reviewProvider;
430
+ if (resolvedProvider !== "greptile") {
431
+ throw new Error(`Invalid provider: ${resolvedProvider}. Supported: greptile.`);
432
+ }
433
+ const paths = resolveHarnessPaths(projectRoot);
434
+ mkdirSync2(paths.stateDir, { recursive: true });
435
+ const next = {
436
+ mode,
437
+ provider: resolvedProvider,
438
+ updated_at: new Date().toISOString()
439
+ };
440
+ writeFileSync(paths.reviewProfilePath, `${JSON.stringify(next, null, 2)}
441
+ `, "utf-8");
442
+ await showReviewProfile(projectRoot);
443
+ }
444
+ async function loadProfile(path) {
445
+ const parsed = await readProfileFile(path);
446
+ if (parsed) {
447
+ return {
448
+ model: parsed.model || DEFAULTS.model,
449
+ runtime: parsed.runtime || DEFAULTS.runtime,
450
+ agent_plugin: parsed.agent_plugin || DEFAULTS.plugin,
451
+ updated_at: parsed.updated_at || new Date().toISOString()
452
+ };
453
+ }
454
+ return {
455
+ model: DEFAULTS.model,
456
+ runtime: DEFAULTS.runtime,
457
+ agent_plugin: DEFAULTS.plugin,
458
+ updated_at: new Date().toISOString()
459
+ };
460
+ }
461
+ async function loadReviewProfile(path) {
462
+ const parsed = await readProfileFile(path);
463
+ if (parsed) {
464
+ return {
465
+ mode: parsed.mode || DEFAULTS.reviewMode,
466
+ provider: parsed.provider || DEFAULTS.reviewProvider,
467
+ updated_at: parsed.updated_at || new Date().toISOString()
468
+ };
469
+ }
470
+ return {
471
+ mode: DEFAULTS.reviewMode,
472
+ provider: DEFAULTS.reviewProvider,
473
+ updated_at: new Date().toISOString()
474
+ };
475
+ }
476
+ export {
477
+ showReviewProfile,
478
+ showProfile,
479
+ setReviewProfile,
480
+ setProfile
481
+ };