@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.
- package/README.md +27 -0
- package/dist/bin/rig-agent-dispatch.js +9615 -0
- package/dist/bin/rig-agent.js +9512 -0
- package/dist/bin/rig-browser-tool.js +269 -0
- package/dist/src/agent-mode.js +48 -0
- package/dist/src/baked-secrets.js +121 -0
- package/dist/src/binary-build-worker.js +312 -0
- package/dist/src/binary-run.js +540 -0
- package/dist/src/boundaries.js +1 -0
- package/dist/src/build-time-config.js +25 -0
- package/dist/src/control-plane/agent-roles.js +27 -0
- package/dist/src/control-plane/agent-wrapper.js +9621 -0
- package/dist/src/control-plane/authority-files.js +582 -0
- package/dist/src/control-plane/browser-contract.js +135 -0
- package/dist/src/control-plane/controlled-bash.js +1111 -0
- package/dist/src/control-plane/errors.js +13 -0
- package/dist/src/control-plane/harness-main.js +10828 -0
- package/dist/src/control-plane/hook-materializer.js +75 -0
- package/dist/src/control-plane/hooks/audit-trail.js +353 -0
- package/dist/src/control-plane/hooks/completion-verification.js +7552 -0
- package/dist/src/control-plane/hooks/import-guard.js +890 -0
- package/dist/src/control-plane/hooks/inject-context.js +4189 -0
- package/dist/src/control-plane/hooks/post-edit-lint.js +43 -0
- package/dist/src/control-plane/hooks/safety-guard.js +910 -0
- package/dist/src/control-plane/hooks/scope-guard.js +907 -0
- package/dist/src/control-plane/hooks/shared.js +44 -0
- package/dist/src/control-plane/hooks/submodule-branch.js +7797 -0
- package/dist/src/control-plane/hooks/task-runtime-start.js +7799 -0
- package/dist/src/control-plane/hooks/test-integrity-guard.js +891 -0
- package/dist/src/control-plane/materialize-task-config.js +453 -0
- package/dist/src/control-plane/memory-sync/cli.js +2019 -0
- package/dist/src/control-plane/memory-sync/db.js +753 -0
- package/dist/src/control-plane/memory-sync/embed.js +281 -0
- package/dist/src/control-plane/memory-sync/index.js +2049 -0
- package/dist/src/control-plane/memory-sync/query.js +294 -0
- package/dist/src/control-plane/memory-sync/read.js +784 -0
- package/dist/src/control-plane/memory-sync/types.js +6 -0
- package/dist/src/control-plane/memory-sync/write.js +1547 -0
- package/dist/src/control-plane/native/git-native.js +490 -0
- package/dist/src/control-plane/native/git-ops.js +2860 -0
- package/dist/src/control-plane/native/harness-cli.js +9721 -0
- package/dist/src/control-plane/native/pr-automation.js +373 -0
- package/dist/src/control-plane/native/profile-ops.js +481 -0
- package/dist/src/control-plane/native/repo-ops.js +2342 -0
- package/dist/src/control-plane/native/root-resolver.js +66 -0
- package/dist/src/control-plane/native/run-ops.js +3281 -0
- package/dist/src/control-plane/native/runtime-native-sidecar.js +299 -0
- package/dist/src/control-plane/native/runtime-native.js +392 -0
- package/dist/src/control-plane/native/scope-rules.js +17 -0
- package/dist/src/control-plane/native/task-ops.js +6320 -0
- package/dist/src/control-plane/native/task-state.js +1512 -0
- package/dist/src/control-plane/native/utils.js +535 -0
- package/dist/src/control-plane/native/validator-binaries.js +889 -0
- package/dist/src/control-plane/native/validator.js +2197 -0
- package/dist/src/control-plane/native/verifier.js +3249 -0
- package/dist/src/control-plane/native/workspace-ops.js +1635 -0
- package/dist/src/control-plane/plugin-host-context.js +334 -0
- package/dist/src/control-plane/project-main-pre-run-sync.js +630 -0
- package/dist/src/control-plane/provider/claude-stream-records.js +158 -0
- package/dist/src/control-plane/provider/codex-app-server.js +885 -0
- package/dist/src/control-plane/provider/codex-exec-records.js +203 -0
- package/dist/src/control-plane/provider/rig-task-run-skill.js +39 -0
- package/dist/src/control-plane/provider/runtime-instructions.js +96 -0
- package/dist/src/control-plane/remote.js +854 -0
- package/dist/src/control-plane/repos/index.js +473 -0
- package/dist/src/control-plane/repos/layout.js +124 -0
- package/dist/src/control-plane/repos/mirror/bootstrap.js +268 -0
- package/dist/src/control-plane/repos/mirror/refresh.js +398 -0
- package/dist/src/control-plane/repos/mirror/state.js +167 -0
- package/dist/src/control-plane/repos/registry.js +77 -0
- package/dist/src/control-plane/repos/types.js +1 -0
- package/dist/src/control-plane/runtime/agent-mode.js +48 -0
- package/dist/src/control-plane/runtime/baked-secrets.js +120 -0
- package/dist/src/control-plane/runtime/claude-tool-router-binary.js +343 -0
- package/dist/src/control-plane/runtime/claude-tool-router.js +520 -0
- package/dist/src/control-plane/runtime/context.js +216 -0
- package/dist/src/control-plane/runtime/events.js +218 -0
- package/dist/src/control-plane/runtime/guard-types.js +6 -0
- package/dist/src/control-plane/runtime/guard.js +880 -0
- package/dist/src/control-plane/runtime/image/fingerprint-sidecar.js +1194 -0
- package/dist/src/control-plane/runtime/image/index.js +2255 -0
- package/dist/src/control-plane/runtime/image-fingerprint-sidecar.js +1191 -0
- package/dist/src/control-plane/runtime/image.js +2255 -0
- package/dist/src/control-plane/runtime/index.js +8511 -0
- package/dist/src/control-plane/runtime/isolation/discovery.js +599 -0
- package/dist/src/control-plane/runtime/isolation/home.js +1217 -0
- package/dist/src/control-plane/runtime/isolation/index.js +8193 -0
- package/dist/src/control-plane/runtime/isolation/runner.js +2651 -0
- package/dist/src/control-plane/runtime/isolation/shared.js +501 -0
- package/dist/src/control-plane/runtime/isolation/toolchain.js +1892 -0
- package/dist/src/control-plane/runtime/isolation/types.js +1 -0
- package/dist/src/control-plane/runtime/isolation/worktree.js +509 -0
- package/dist/src/control-plane/runtime/isolation.js +8193 -0
- package/dist/src/control-plane/runtime/overlay.js +67 -0
- package/dist/src/control-plane/runtime/plugin-mode.js +41 -0
- package/dist/src/control-plane/runtime/plugins.js +1131 -0
- package/dist/src/control-plane/runtime/provisioning-env.js +220 -0
- package/dist/src/control-plane/runtime/queue.js +8358 -0
- package/dist/src/control-plane/runtime/rig-shell.js +205 -0
- package/dist/src/control-plane/runtime/rig-tools.js +182 -0
- package/dist/src/control-plane/runtime/runner-context.js +1 -0
- package/dist/src/control-plane/runtime/runtime-paths.js +184 -0
- package/dist/src/control-plane/runtime/sandbox/backend-bwrap.js +311 -0
- package/dist/src/control-plane/runtime/sandbox/backend-none.js +21 -0
- package/dist/src/control-plane/runtime/sandbox/backend-seatbelt.js +268 -0
- package/dist/src/control-plane/runtime/sandbox/backend.js +1718 -0
- package/dist/src/control-plane/runtime/sandbox/orchestrator.js +1745 -0
- package/dist/src/control-plane/runtime/sandbox/utils.js +137 -0
- package/dist/src/control-plane/runtime/sandbox-backend-bwrap.js +311 -0
- package/dist/src/control-plane/runtime/sandbox-backend-none.js +21 -0
- package/dist/src/control-plane/runtime/sandbox-backend-seatbelt.js +268 -0
- package/dist/src/control-plane/runtime/sandbox-backend.js +1718 -0
- package/dist/src/control-plane/runtime/sandbox-orchestrator.js +1745 -0
- package/dist/src/control-plane/runtime/sandbox-utils.js +137 -0
- package/dist/src/control-plane/runtime/snapshot/index.js +454 -0
- package/dist/src/control-plane/runtime/snapshot/sidecar.js +502 -0
- package/dist/src/control-plane/runtime/snapshot/task-run.js +1578 -0
- package/dist/src/control-plane/runtime/snapshot-sidecar.js +498 -0
- package/dist/src/control-plane/runtime/snapshot.js +454 -0
- package/dist/src/control-plane/runtime/task-run-snapshot.js +1578 -0
- package/dist/src/control-plane/runtime/tool-gateway.js +422 -0
- package/dist/src/control-plane/runtime/tooling/browser-tools.js +32 -0
- package/dist/src/control-plane/runtime/tooling/claude-router-binary.js +343 -0
- package/dist/src/control-plane/runtime/tooling/claude-router.js +524 -0
- package/dist/src/control-plane/runtime/tooling/file-tools.js +182 -0
- package/dist/src/control-plane/runtime/tooling/gateway.js +422 -0
- package/dist/src/control-plane/runtime/tooling/index.js +1290 -0
- package/dist/src/control-plane/runtime/tooling/shell.js +205 -0
- package/dist/src/control-plane/runtime/types.js +1 -0
- package/dist/src/control-plane/setup-version.js +14 -0
- package/dist/src/control-plane/state-sync/index.js +1509 -0
- package/dist/src/control-plane/state-sync/read.js +856 -0
- package/dist/src/control-plane/state-sync/reconcile.js +260 -0
- package/dist/src/control-plane/state-sync/repo.js +302 -0
- package/dist/src/control-plane/state-sync/types.js +111 -0
- package/dist/src/control-plane/state-sync/write.js +1469 -0
- package/dist/src/control-plane/task-fields.js +38 -0
- package/dist/src/control-plane/task-source-bootstrap.js +46 -0
- package/dist/src/control-plane/task-source.js +30 -0
- package/dist/src/control-plane/tasks/legacy-task-config-source.js +130 -0
- package/dist/src/control-plane/tasks/plugin-task-source.js +103 -0
- package/dist/src/control-plane/tasks/source-aware-task-config-source.js +611 -0
- package/dist/src/control-plane/tasks/source-lifecycle.js +1093 -0
- package/dist/src/control-plane/tasks/task-record-reader.js +9 -0
- package/dist/src/control-plane/validators/boundary/public-apis.js +107 -0
- package/dist/src/control-plane/validators/integration/_shared.js +51 -0
- package/dist/src/control-plane/validators/integration/adm-audit-http.js +85 -0
- package/dist/src/control-plane/validators/integration/adm-auth-http.js +78 -0
- package/dist/src/control-plane/validators/integration/adm-issuer-http.js +80 -0
- package/dist/src/control-plane/validators/integration/adm-migration.js +78 -0
- package/dist/src/control-plane/validators/integration/adm-scaffold.js +78 -0
- package/dist/src/control-plane/validators/runtime-registration.js +64 -0
- package/dist/src/control-plane/validators/shared.js +683 -0
- package/dist/src/events.js +218 -0
- package/dist/src/execution.js +35 -0
- package/dist/src/index.js +1633 -0
- package/dist/src/layout.js +145 -0
- package/dist/src/local-server.js +202 -0
- package/dist/src/plugins.js +329 -0
- package/dist/src/remote-http.js +83 -0
- package/dist/src/runtime-context.js +216 -0
- package/dist/src/types.js +1 -0
- package/native/darwin-arm64/bin/rig-git +0 -0
- package/native/darwin-arm64/bin/rig-shell +0 -0
- package/native/darwin-arm64/bin/rig-tools +0 -0
- package/native/darwin-arm64/lib/runtime-native-darwin-arm64.dylib +0 -0
- package/native/darwin-arm64/lib/runtime-native.dylib +0 -0
- package/native/darwin-arm64/manifest.json +1 -0
- package/native/linux-x64/bin/rig-git +0 -0
- package/native/linux-x64/bin/rig-shell +0 -0
- package/native/linux-x64/bin/rig-tools +0 -0
- package/native/linux-x64/lib/runtime-native-linux-x64.so +0 -0
- package/native/linux-x64/lib/runtime-native.so +0 -0
- package/native/linux-x64/manifest.json +1 -0
- package/package.json +74 -0
- package/skills/rig-task-run.md +71 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/runtime/src/control-plane/native/runtime-native.ts
|
|
3
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
4
|
+
import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync, statSync } from "fs";
|
|
5
|
+
import { tmpdir } from "os";
|
|
6
|
+
import { dirname, resolve } from "path";
|
|
7
|
+
var sharedNativeRuntimeOutputDir = resolve(tmpdir(), "rig-native");
|
|
8
|
+
var sharedNativeRuntimeOutputPath = resolve(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
9
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
10
|
+
var OPERATION_RESULT_SIZE = 24;
|
|
11
|
+
var RUNTIME_SCAN_ENTRY_SIZE = 64;
|
|
12
|
+
var RUNTIME_SCAN_RESULT_SIZE = 40;
|
|
13
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
14
|
+
function requireNativeRuntimeLibrary(feature) {
|
|
15
|
+
if (!nativeRuntimeLibrary) {
|
|
16
|
+
throw new Error(`Native Zig runtime is required for ${feature}`);
|
|
17
|
+
}
|
|
18
|
+
return nativeRuntimeLibrary;
|
|
19
|
+
}
|
|
20
|
+
function runtimePrepareTrackedPathsInProcess(input) {
|
|
21
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime dependency layer linking");
|
|
22
|
+
const logsDir = Buffer.from(input.logsDir, "utf8");
|
|
23
|
+
const stateDir = Buffer.from(input.stateDir, "utf8");
|
|
24
|
+
const sessionDir = Buffer.from(input.sessionDir, "utf8");
|
|
25
|
+
const eventsFile = Buffer.from(input.eventsFile, "utf8");
|
|
26
|
+
const controlledBashLogFile = Buffer.from(input.controlledBashLogFile, "utf8");
|
|
27
|
+
const resultPtr = runtimeLibrary.symbols.runtime_prepare_paths(Number(ptr(logsDir)), logsDir.byteLength, Number(ptr(stateDir)), stateDir.byteLength, Number(ptr(sessionDir)), sessionDir.byteLength, Number(ptr(eventsFile)), eventsFile.byteLength, Number(ptr(controlledBashLogFile)), controlledBashLogFile.byteLength);
|
|
28
|
+
if (!resultPtr) {
|
|
29
|
+
throw new Error("runtime_prepare_paths returned null");
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const view = viewAt(resultPtr, OPERATION_RESULT_SIZE);
|
|
33
|
+
throwIfOperationError(view, "runtime_prepare_paths");
|
|
34
|
+
} finally {
|
|
35
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function runtimeLinkDependencyLayerInProcess(sourceDir, targetDir) {
|
|
39
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime worktree discovery");
|
|
40
|
+
const sourceBuffer = Buffer.from(sourceDir, "utf8");
|
|
41
|
+
const targetBuffer = Buffer.from(targetDir, "utf8");
|
|
42
|
+
const resultPtr = runtimeLibrary.symbols.runtime_link_dependency_layer(Number(ptr(sourceBuffer)), sourceBuffer.byteLength, Number(ptr(targetBuffer)), targetBuffer.byteLength);
|
|
43
|
+
if (!resultPtr) {
|
|
44
|
+
throw new Error("runtime_link_dependency_layer returned null");
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const view = viewAt(resultPtr, OPERATION_RESULT_SIZE);
|
|
48
|
+
throwIfOperationError(view, "runtime_link_dependency_layer");
|
|
49
|
+
} finally {
|
|
50
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function runtimeScanWorktreesInProcess(worktreesRoot) {
|
|
54
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime worktree discovery");
|
|
55
|
+
const rootBuffer = Buffer.from(worktreesRoot, "utf8");
|
|
56
|
+
const resultPtr = runtimeLibrary.symbols.runtime_scan_worktrees(Number(ptr(rootBuffer)), rootBuffer.byteLength);
|
|
57
|
+
if (!resultPtr) {
|
|
58
|
+
throw new Error(`runtime_scan_worktrees returned null for ${worktreesRoot}`);
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const view = viewAt(resultPtr, RUNTIME_SCAN_RESULT_SIZE);
|
|
62
|
+
throwIfScanError(view, "runtime_scan_worktrees");
|
|
63
|
+
const entriesPtr = readU64(view, 8);
|
|
64
|
+
const entriesLen = readU64(view, 16);
|
|
65
|
+
const entries = [];
|
|
66
|
+
for (let index = 0;index < entriesLen; index += 1) {
|
|
67
|
+
const entryView = viewAt(entriesPtr + index * RUNTIME_SCAN_ENTRY_SIZE, RUNTIME_SCAN_ENTRY_SIZE);
|
|
68
|
+
entries.push({
|
|
69
|
+
workspaceDir: readString(readU64(entryView, 0), readU64(entryView, 8)),
|
|
70
|
+
runtimeDir: readString(readU64(entryView, 16), readU64(entryView, 24)),
|
|
71
|
+
contextPath: readString(readU64(entryView, 32), readU64(entryView, 40)),
|
|
72
|
+
metadataPath: readString(readU64(entryView, 48), readU64(entryView, 56))
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return entries;
|
|
76
|
+
} finally {
|
|
77
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
81
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
82
|
+
return outputPath;
|
|
83
|
+
}
|
|
84
|
+
return !options.force && existsSync(outputPath) ? outputPath : null;
|
|
85
|
+
}
|
|
86
|
+
async function loadNativeRuntimeLibrary() {
|
|
87
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
91
|
+
if (!candidate || !existsSync(candidate)) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
95
|
+
if (loaded) {
|
|
96
|
+
return loaded;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
100
|
+
if (!builtLibraryPath) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
104
|
+
}
|
|
105
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
106
|
+
const candidates = [];
|
|
107
|
+
let cursor = resolve(fromDir);
|
|
108
|
+
for (let index = 0;index < 8; index += 1) {
|
|
109
|
+
for (const name of names) {
|
|
110
|
+
candidates.push(resolve(cursor, "native", `${process.platform}-${process.arch}`, name), resolve(cursor, "native", `${process.platform}-${process.arch}`, "lib", name), resolve(cursor, "native", name), resolve(cursor, "native", "lib", name));
|
|
111
|
+
}
|
|
112
|
+
const parent = dirname(cursor);
|
|
113
|
+
if (parent === cursor)
|
|
114
|
+
break;
|
|
115
|
+
cursor = parent;
|
|
116
|
+
}
|
|
117
|
+
return candidates;
|
|
118
|
+
}
|
|
119
|
+
function nativeRuntimeLibraryCandidates() {
|
|
120
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
121
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
122
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
123
|
+
return [...new Set([
|
|
124
|
+
explicit,
|
|
125
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
126
|
+
execDir ? resolve(execDir, colocatedNativeRuntimeFileName) : "",
|
|
127
|
+
execDir ? resolve(execDir, platformSpecific) : "",
|
|
128
|
+
execDir ? resolve(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
129
|
+
execDir ? resolve(execDir, "..", platformSpecific) : "",
|
|
130
|
+
execDir ? resolve(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
131
|
+
execDir ? resolve(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
132
|
+
sharedNativeRuntimeOutputPath
|
|
133
|
+
].filter(Boolean))];
|
|
134
|
+
}
|
|
135
|
+
function resolveNativeRuntimeSourcePath() {
|
|
136
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
137
|
+
if (explicit && existsSync(explicit)) {
|
|
138
|
+
return explicit;
|
|
139
|
+
}
|
|
140
|
+
const bundled = resolve(import.meta.dir, "../../../native/snapshot.zig");
|
|
141
|
+
return existsSync(bundled) ? bundled : null;
|
|
142
|
+
}
|
|
143
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
144
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
const zigBinary = Bun.which("zig");
|
|
148
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
149
|
+
if (!zigBinary || !sourcePath) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
153
|
+
try {
|
|
154
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
155
|
+
const needsBuild = options.force === true || !existsSync(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
|
|
156
|
+
if (!needsBuild) {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
const build = Bun.spawn([
|
|
160
|
+
zigBinary,
|
|
161
|
+
"build-lib",
|
|
162
|
+
sourcePath,
|
|
163
|
+
"-dynamic",
|
|
164
|
+
"-O",
|
|
165
|
+
"ReleaseFast",
|
|
166
|
+
`-femit-bin=${tempOutputPath}`
|
|
167
|
+
], {
|
|
168
|
+
cwd: import.meta.dir,
|
|
169
|
+
stdout: "pipe",
|
|
170
|
+
stderr: "pipe"
|
|
171
|
+
});
|
|
172
|
+
const exitCode = await build.exited;
|
|
173
|
+
if (exitCode !== 0 || !existsSync(tempOutputPath)) {
|
|
174
|
+
rmSync(tempOutputPath, { force: true });
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
renameSync(tempOutputPath, outputPath);
|
|
178
|
+
return true;
|
|
179
|
+
} catch {
|
|
180
|
+
rmSync(tempOutputPath, { force: true });
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
185
|
+
try {
|
|
186
|
+
return dlopen(outputPath, {
|
|
187
|
+
rig_scope_match: {
|
|
188
|
+
args: ["ptr", "ptr"],
|
|
189
|
+
returns: "u8"
|
|
190
|
+
},
|
|
191
|
+
snapshot_capture: {
|
|
192
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
193
|
+
returns: "ptr"
|
|
194
|
+
},
|
|
195
|
+
snapshot_delta: {
|
|
196
|
+
args: ["ptr", "ptr"],
|
|
197
|
+
returns: "ptr"
|
|
198
|
+
},
|
|
199
|
+
snapshot_store_delta: {
|
|
200
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
201
|
+
returns: "ptr"
|
|
202
|
+
},
|
|
203
|
+
snapshot_inspect_delta: {
|
|
204
|
+
args: ["ptr", "u64"],
|
|
205
|
+
returns: "ptr"
|
|
206
|
+
},
|
|
207
|
+
snapshot_apply_delta: {
|
|
208
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
209
|
+
returns: "ptr"
|
|
210
|
+
},
|
|
211
|
+
snapshot_release: {
|
|
212
|
+
args: ["ptr"],
|
|
213
|
+
returns: "void"
|
|
214
|
+
},
|
|
215
|
+
runtime_hash_file: {
|
|
216
|
+
args: ["ptr", "u64"],
|
|
217
|
+
returns: "ptr"
|
|
218
|
+
},
|
|
219
|
+
runtime_hash_tree: {
|
|
220
|
+
args: ["ptr", "u64"],
|
|
221
|
+
returns: "ptr"
|
|
222
|
+
},
|
|
223
|
+
runtime_prepare_paths: {
|
|
224
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
225
|
+
returns: "ptr"
|
|
226
|
+
},
|
|
227
|
+
runtime_link_dependency_layer: {
|
|
228
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
229
|
+
returns: "ptr"
|
|
230
|
+
},
|
|
231
|
+
runtime_scan_worktrees: {
|
|
232
|
+
args: ["ptr", "u64"],
|
|
233
|
+
returns: "ptr"
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function viewAt(address, size) {
|
|
241
|
+
return Buffer.from(toBuffer(address, 0, size));
|
|
242
|
+
}
|
|
243
|
+
function readU64(view, offset) {
|
|
244
|
+
return Number(view.readBigUInt64LE(offset));
|
|
245
|
+
}
|
|
246
|
+
function readString(address, length) {
|
|
247
|
+
if (!address || !length) {
|
|
248
|
+
return "";
|
|
249
|
+
}
|
|
250
|
+
return Buffer.from(toBuffer(address, 0, length)).toString("utf8");
|
|
251
|
+
}
|
|
252
|
+
function throwIfOperationError(view, opName) {
|
|
253
|
+
const errorPtr = readU64(view, 8);
|
|
254
|
+
const errorLen = readU64(view, 16);
|
|
255
|
+
if (!errorPtr || errorLen === 0) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
throw new Error(`${opName} failed: ${readString(errorPtr, errorLen)}`);
|
|
259
|
+
}
|
|
260
|
+
function throwIfScanError(view, opName) {
|
|
261
|
+
const errorPtr = readU64(view, 24);
|
|
262
|
+
const errorLen = readU64(view, 32);
|
|
263
|
+
if (!errorPtr || errorLen === 0) {
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
throw new Error(`${opName} failed: ${readString(errorPtr, errorLen)}`);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// packages/runtime/src/control-plane/native/runtime-native-sidecar.ts
|
|
270
|
+
var rawRequest = process.argv[2];
|
|
271
|
+
if (!rawRequest) {
|
|
272
|
+
console.error("usage: runtime-native-sidecar <request-json>");
|
|
273
|
+
process.exit(2);
|
|
274
|
+
}
|
|
275
|
+
try {
|
|
276
|
+
const request = JSON.parse(rawRequest);
|
|
277
|
+
const response = handleRequest(request);
|
|
278
|
+
process.stdout.write(`${JSON.stringify(response)}
|
|
279
|
+
`);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
282
|
+
process.stdout.write(`${JSON.stringify({ ok: false, error: message })}
|
|
283
|
+
`);
|
|
284
|
+
process.exit(1);
|
|
285
|
+
}
|
|
286
|
+
function handleRequest(request) {
|
|
287
|
+
switch (request.op) {
|
|
288
|
+
case "prepare-paths":
|
|
289
|
+
runtimePrepareTrackedPathsInProcess(request.input);
|
|
290
|
+
return { ok: true };
|
|
291
|
+
case "link-dependency-layer":
|
|
292
|
+
runtimeLinkDependencyLayerInProcess(request.input.sourceDir, request.input.targetDir);
|
|
293
|
+
return { ok: true };
|
|
294
|
+
case "scan-worktrees":
|
|
295
|
+
return { ok: true, entries: runtimeScanWorktreesInProcess(request.input.worktreesRoot) };
|
|
296
|
+
default:
|
|
297
|
+
return { ok: false, error: `Unsupported runtime native sidecar op: ${request.op ?? "<missing>"}` };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/runtime/src/control-plane/native/runtime-native.ts
|
|
3
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
4
|
+
import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync, statSync } from "fs";
|
|
5
|
+
import { tmpdir } from "os";
|
|
6
|
+
import { dirname, resolve } from "path";
|
|
7
|
+
var sharedNativeRuntimeOutputDir = resolve(tmpdir(), "rig-native");
|
|
8
|
+
var sharedNativeRuntimeOutputPath = resolve(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
9
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
10
|
+
var OPERATION_RESULT_SIZE = 24;
|
|
11
|
+
var RUNTIME_SCAN_ENTRY_SIZE = 64;
|
|
12
|
+
var RUNTIME_SCAN_RESULT_SIZE = 40;
|
|
13
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
14
|
+
function requireNativeRuntimeLibrary(feature) {
|
|
15
|
+
if (!nativeRuntimeLibrary) {
|
|
16
|
+
throw new Error(`Native Zig runtime is required for ${feature}`);
|
|
17
|
+
}
|
|
18
|
+
return nativeRuntimeLibrary;
|
|
19
|
+
}
|
|
20
|
+
function runtimeNativeLibraryFileName() {
|
|
21
|
+
return colocatedNativeRuntimeFileName;
|
|
22
|
+
}
|
|
23
|
+
function runtimePrepareTrackedPathsNative(input) {
|
|
24
|
+
const response = runNativeRuntimeSidecar({
|
|
25
|
+
op: "prepare-paths",
|
|
26
|
+
input
|
|
27
|
+
});
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new Error(response.error);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function runtimeLinkDependencyLayerNative(sourceDir, targetDir) {
|
|
33
|
+
const response = runNativeRuntimeSidecar({
|
|
34
|
+
op: "link-dependency-layer",
|
|
35
|
+
input: { sourceDir, targetDir }
|
|
36
|
+
});
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
throw new Error(response.error);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function runtimeScanWorktreesNative(worktreesRoot) {
|
|
42
|
+
const response = runNativeRuntimeSidecar({
|
|
43
|
+
op: "scan-worktrees",
|
|
44
|
+
input: { worktreesRoot }
|
|
45
|
+
});
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
throw new Error(response.error);
|
|
48
|
+
}
|
|
49
|
+
return response.entries ?? [];
|
|
50
|
+
}
|
|
51
|
+
function runtimePrepareTrackedPathsInProcess(input) {
|
|
52
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime dependency layer linking");
|
|
53
|
+
const logsDir = Buffer.from(input.logsDir, "utf8");
|
|
54
|
+
const stateDir = Buffer.from(input.stateDir, "utf8");
|
|
55
|
+
const sessionDir = Buffer.from(input.sessionDir, "utf8");
|
|
56
|
+
const eventsFile = Buffer.from(input.eventsFile, "utf8");
|
|
57
|
+
const controlledBashLogFile = Buffer.from(input.controlledBashLogFile, "utf8");
|
|
58
|
+
const resultPtr = runtimeLibrary.symbols.runtime_prepare_paths(Number(ptr(logsDir)), logsDir.byteLength, Number(ptr(stateDir)), stateDir.byteLength, Number(ptr(sessionDir)), sessionDir.byteLength, Number(ptr(eventsFile)), eventsFile.byteLength, Number(ptr(controlledBashLogFile)), controlledBashLogFile.byteLength);
|
|
59
|
+
if (!resultPtr) {
|
|
60
|
+
throw new Error("runtime_prepare_paths returned null");
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
const view = viewAt(resultPtr, OPERATION_RESULT_SIZE);
|
|
64
|
+
throwIfOperationError(view, "runtime_prepare_paths");
|
|
65
|
+
} finally {
|
|
66
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function runtimeLinkDependencyLayerInProcess(sourceDir, targetDir) {
|
|
70
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime worktree discovery");
|
|
71
|
+
const sourceBuffer = Buffer.from(sourceDir, "utf8");
|
|
72
|
+
const targetBuffer = Buffer.from(targetDir, "utf8");
|
|
73
|
+
const resultPtr = runtimeLibrary.symbols.runtime_link_dependency_layer(Number(ptr(sourceBuffer)), sourceBuffer.byteLength, Number(ptr(targetBuffer)), targetBuffer.byteLength);
|
|
74
|
+
if (!resultPtr) {
|
|
75
|
+
throw new Error("runtime_link_dependency_layer returned null");
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
const view = viewAt(resultPtr, OPERATION_RESULT_SIZE);
|
|
79
|
+
throwIfOperationError(view, "runtime_link_dependency_layer");
|
|
80
|
+
} finally {
|
|
81
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function runtimeScanWorktreesInProcess(worktreesRoot) {
|
|
85
|
+
const runtimeLibrary = requireNativeRuntimeLibrary("runtime worktree discovery");
|
|
86
|
+
const rootBuffer = Buffer.from(worktreesRoot, "utf8");
|
|
87
|
+
const resultPtr = runtimeLibrary.symbols.runtime_scan_worktrees(Number(ptr(rootBuffer)), rootBuffer.byteLength);
|
|
88
|
+
if (!resultPtr) {
|
|
89
|
+
throw new Error(`runtime_scan_worktrees returned null for ${worktreesRoot}`);
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
const view = viewAt(resultPtr, RUNTIME_SCAN_RESULT_SIZE);
|
|
93
|
+
throwIfScanError(view, "runtime_scan_worktrees");
|
|
94
|
+
const entriesPtr = readU64(view, 8);
|
|
95
|
+
const entriesLen = readU64(view, 16);
|
|
96
|
+
const entries = [];
|
|
97
|
+
for (let index = 0;index < entriesLen; index += 1) {
|
|
98
|
+
const entryView = viewAt(entriesPtr + index * RUNTIME_SCAN_ENTRY_SIZE, RUNTIME_SCAN_ENTRY_SIZE);
|
|
99
|
+
entries.push({
|
|
100
|
+
workspaceDir: readString(readU64(entryView, 0), readU64(entryView, 8)),
|
|
101
|
+
runtimeDir: readString(readU64(entryView, 16), readU64(entryView, 24)),
|
|
102
|
+
contextPath: readString(readU64(entryView, 32), readU64(entryView, 40)),
|
|
103
|
+
metadataPath: readString(readU64(entryView, 48), readU64(entryView, 56))
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return entries;
|
|
107
|
+
} finally {
|
|
108
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function defaultNativeRuntimeLibraryPath() {
|
|
112
|
+
return sharedNativeRuntimeOutputPath;
|
|
113
|
+
}
|
|
114
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
115
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
116
|
+
return outputPath;
|
|
117
|
+
}
|
|
118
|
+
return !options.force && existsSync(outputPath) ? outputPath : null;
|
|
119
|
+
}
|
|
120
|
+
async function materializeNativeRuntimeLibrary(targetDir) {
|
|
121
|
+
const sourcePath = await ensureNativeRuntimeLibraryPath();
|
|
122
|
+
if (!sourcePath) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
const targetPath = resolve(targetDir, colocatedNativeRuntimeFileName);
|
|
126
|
+
mkdirSync(targetDir, { recursive: true });
|
|
127
|
+
const needsCopy = !existsSync(targetPath) || statSync(sourcePath).mtimeMs > statSync(targetPath).mtimeMs;
|
|
128
|
+
if (needsCopy) {
|
|
129
|
+
copyFileSync(sourcePath, targetPath);
|
|
130
|
+
}
|
|
131
|
+
return targetPath;
|
|
132
|
+
}
|
|
133
|
+
async function loadNativeRuntimeLibrary() {
|
|
134
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
138
|
+
if (!candidate || !existsSync(candidate)) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
142
|
+
if (loaded) {
|
|
143
|
+
return loaded;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
147
|
+
if (!builtLibraryPath) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
151
|
+
}
|
|
152
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
153
|
+
const candidates = [];
|
|
154
|
+
let cursor = resolve(fromDir);
|
|
155
|
+
for (let index = 0;index < 8; index += 1) {
|
|
156
|
+
for (const name of names) {
|
|
157
|
+
candidates.push(resolve(cursor, "native", `${process.platform}-${process.arch}`, name), resolve(cursor, "native", `${process.platform}-${process.arch}`, "lib", name), resolve(cursor, "native", name), resolve(cursor, "native", "lib", name));
|
|
158
|
+
}
|
|
159
|
+
const parent = dirname(cursor);
|
|
160
|
+
if (parent === cursor)
|
|
161
|
+
break;
|
|
162
|
+
cursor = parent;
|
|
163
|
+
}
|
|
164
|
+
return candidates;
|
|
165
|
+
}
|
|
166
|
+
function nativeRuntimeLibraryCandidates() {
|
|
167
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
168
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
169
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
170
|
+
return [...new Set([
|
|
171
|
+
explicit,
|
|
172
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
173
|
+
execDir ? resolve(execDir, colocatedNativeRuntimeFileName) : "",
|
|
174
|
+
execDir ? resolve(execDir, platformSpecific) : "",
|
|
175
|
+
execDir ? resolve(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
176
|
+
execDir ? resolve(execDir, "..", platformSpecific) : "",
|
|
177
|
+
execDir ? resolve(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
178
|
+
execDir ? resolve(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
179
|
+
sharedNativeRuntimeOutputPath
|
|
180
|
+
].filter(Boolean))];
|
|
181
|
+
}
|
|
182
|
+
function resolveNativeRuntimeSourcePath() {
|
|
183
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
184
|
+
if (explicit && existsSync(explicit)) {
|
|
185
|
+
return explicit;
|
|
186
|
+
}
|
|
187
|
+
const bundled = resolve(import.meta.dir, "../../../native/snapshot.zig");
|
|
188
|
+
return existsSync(bundled) ? bundled : null;
|
|
189
|
+
}
|
|
190
|
+
function resolveNativeRuntimeSidecarSourcePath() {
|
|
191
|
+
const envRoots = [
|
|
192
|
+
process.cwd()?.trim(),
|
|
193
|
+
process.env.RIG_HOST_PROJECT_ROOT?.trim(),
|
|
194
|
+
process.env.PROJECT_RIG_ROOT?.trim()
|
|
195
|
+
].filter(Boolean);
|
|
196
|
+
for (const root of envRoots) {
|
|
197
|
+
for (const relative of [
|
|
198
|
+
"packages/runtime/src/control-plane/native/runtime-native-sidecar.ts",
|
|
199
|
+
"packages/runtime/dist/src/control-plane/native/runtime-native-sidecar.js"
|
|
200
|
+
]) {
|
|
201
|
+
const candidate = resolve(root, relative);
|
|
202
|
+
if (existsSync(candidate)) {
|
|
203
|
+
return candidate;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
for (const localCandidate of [
|
|
208
|
+
resolve(import.meta.dir, "runtime-native-sidecar.js"),
|
|
209
|
+
resolve(import.meta.dir, "runtime-native-sidecar.ts")
|
|
210
|
+
]) {
|
|
211
|
+
if (existsSync(localCandidate))
|
|
212
|
+
return localCandidate;
|
|
213
|
+
}
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
217
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
const zigBinary = Bun.which("zig");
|
|
221
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
222
|
+
if (!zigBinary || !sourcePath) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
226
|
+
try {
|
|
227
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
228
|
+
const needsBuild = options.force === true || !existsSync(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
|
|
229
|
+
if (!needsBuild) {
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
const build = Bun.spawn([
|
|
233
|
+
zigBinary,
|
|
234
|
+
"build-lib",
|
|
235
|
+
sourcePath,
|
|
236
|
+
"-dynamic",
|
|
237
|
+
"-O",
|
|
238
|
+
"ReleaseFast",
|
|
239
|
+
`-femit-bin=${tempOutputPath}`
|
|
240
|
+
], {
|
|
241
|
+
cwd: import.meta.dir,
|
|
242
|
+
stdout: "pipe",
|
|
243
|
+
stderr: "pipe"
|
|
244
|
+
});
|
|
245
|
+
const exitCode = await build.exited;
|
|
246
|
+
if (exitCode !== 0 || !existsSync(tempOutputPath)) {
|
|
247
|
+
rmSync(tempOutputPath, { force: true });
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
renameSync(tempOutputPath, outputPath);
|
|
251
|
+
return true;
|
|
252
|
+
} catch {
|
|
253
|
+
rmSync(tempOutputPath, { force: true });
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
258
|
+
try {
|
|
259
|
+
return dlopen(outputPath, {
|
|
260
|
+
rig_scope_match: {
|
|
261
|
+
args: ["ptr", "ptr"],
|
|
262
|
+
returns: "u8"
|
|
263
|
+
},
|
|
264
|
+
snapshot_capture: {
|
|
265
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
266
|
+
returns: "ptr"
|
|
267
|
+
},
|
|
268
|
+
snapshot_delta: {
|
|
269
|
+
args: ["ptr", "ptr"],
|
|
270
|
+
returns: "ptr"
|
|
271
|
+
},
|
|
272
|
+
snapshot_store_delta: {
|
|
273
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
274
|
+
returns: "ptr"
|
|
275
|
+
},
|
|
276
|
+
snapshot_inspect_delta: {
|
|
277
|
+
args: ["ptr", "u64"],
|
|
278
|
+
returns: "ptr"
|
|
279
|
+
},
|
|
280
|
+
snapshot_apply_delta: {
|
|
281
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
282
|
+
returns: "ptr"
|
|
283
|
+
},
|
|
284
|
+
snapshot_release: {
|
|
285
|
+
args: ["ptr"],
|
|
286
|
+
returns: "void"
|
|
287
|
+
},
|
|
288
|
+
runtime_hash_file: {
|
|
289
|
+
args: ["ptr", "u64"],
|
|
290
|
+
returns: "ptr"
|
|
291
|
+
},
|
|
292
|
+
runtime_hash_tree: {
|
|
293
|
+
args: ["ptr", "u64"],
|
|
294
|
+
returns: "ptr"
|
|
295
|
+
},
|
|
296
|
+
runtime_prepare_paths: {
|
|
297
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
298
|
+
returns: "ptr"
|
|
299
|
+
},
|
|
300
|
+
runtime_link_dependency_layer: {
|
|
301
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
302
|
+
returns: "ptr"
|
|
303
|
+
},
|
|
304
|
+
runtime_scan_worktrees: {
|
|
305
|
+
args: ["ptr", "u64"],
|
|
306
|
+
returns: "ptr"
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
} catch {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function runNativeRuntimeSidecar(request) {
|
|
314
|
+
const sidecarSourcePath = resolveNativeRuntimeSidecarSourcePath();
|
|
315
|
+
if (!sidecarSourcePath) {
|
|
316
|
+
throw new Error("runtime-native-sidecar.ts source file not found.");
|
|
317
|
+
}
|
|
318
|
+
const bunCli = resolveNativeRuntimeSidecarInvocation();
|
|
319
|
+
const proc = Bun.spawnSync([bunCli.command, sidecarSourcePath, JSON.stringify(request)], {
|
|
320
|
+
cwd: process.env.RIG_HOST_PROJECT_ROOT?.trim() || process.cwd(),
|
|
321
|
+
stdout: "pipe",
|
|
322
|
+
stderr: "pipe",
|
|
323
|
+
env: {
|
|
324
|
+
...process.env,
|
|
325
|
+
...bunCli.env,
|
|
326
|
+
RIG_NATIVE_RUNTIME_SIDECAR: "1"
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
if (proc.exitCode !== 0) {
|
|
330
|
+
throw new Error(proc.stderr.toString() || proc.stdout.toString() || `runtime native sidecar exited ${proc.exitCode}`);
|
|
331
|
+
}
|
|
332
|
+
const stdout = proc.stdout.toString().trim();
|
|
333
|
+
if (!stdout) {
|
|
334
|
+
throw new Error("runtime native sidecar returned empty output.");
|
|
335
|
+
}
|
|
336
|
+
return JSON.parse(stdout);
|
|
337
|
+
}
|
|
338
|
+
function resolveNativeRuntimeSidecarInvocation() {
|
|
339
|
+
const bunPath = Bun.which("bun");
|
|
340
|
+
if (bunPath) {
|
|
341
|
+
return { command: bunPath, env: {} };
|
|
342
|
+
}
|
|
343
|
+
if (process.execPath?.trim()) {
|
|
344
|
+
return {
|
|
345
|
+
command: process.execPath,
|
|
346
|
+
env: { BUN_BE_BUN: "1" }
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
throw new Error("bun is required to run the runtime native sidecar.");
|
|
350
|
+
}
|
|
351
|
+
function viewAt(address, size) {
|
|
352
|
+
return Buffer.from(toBuffer(address, 0, size));
|
|
353
|
+
}
|
|
354
|
+
function readU64(view, offset) {
|
|
355
|
+
return Number(view.readBigUInt64LE(offset));
|
|
356
|
+
}
|
|
357
|
+
function readString(address, length) {
|
|
358
|
+
if (!address || !length) {
|
|
359
|
+
return "";
|
|
360
|
+
}
|
|
361
|
+
return Buffer.from(toBuffer(address, 0, length)).toString("utf8");
|
|
362
|
+
}
|
|
363
|
+
function throwIfOperationError(view, opName) {
|
|
364
|
+
const errorPtr = readU64(view, 8);
|
|
365
|
+
const errorLen = readU64(view, 16);
|
|
366
|
+
if (!errorPtr || errorLen === 0) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
throw new Error(`${opName} failed: ${readString(errorPtr, errorLen)}`);
|
|
370
|
+
}
|
|
371
|
+
function throwIfScanError(view, opName) {
|
|
372
|
+
const errorPtr = readU64(view, 24);
|
|
373
|
+
const errorLen = readU64(view, 32);
|
|
374
|
+
if (!errorPtr || errorLen === 0) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
throw new Error(`${opName} failed: ${readString(errorPtr, errorLen)}`);
|
|
378
|
+
}
|
|
379
|
+
export {
|
|
380
|
+
runtimeScanWorktreesNative,
|
|
381
|
+
runtimeScanWorktreesInProcess,
|
|
382
|
+
runtimePrepareTrackedPathsNative,
|
|
383
|
+
runtimePrepareTrackedPathsInProcess,
|
|
384
|
+
runtimeNativeLibraryFileName,
|
|
385
|
+
runtimeLinkDependencyLayerNative,
|
|
386
|
+
runtimeLinkDependencyLayerInProcess,
|
|
387
|
+
requireNativeRuntimeLibrary,
|
|
388
|
+
nativeRuntimeLibrary,
|
|
389
|
+
materializeNativeRuntimeLibrary,
|
|
390
|
+
ensureNativeRuntimeLibraryPath,
|
|
391
|
+
defaultNativeRuntimeLibraryPath
|
|
392
|
+
};
|