@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,1191 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
3
|
+
|
|
4
|
+
// packages/runtime/src/layout.ts
|
|
5
|
+
var init_layout = () => {};
|
|
6
|
+
|
|
7
|
+
// packages/runtime/src/control-plane/runtime/sandbox/utils.ts
|
|
8
|
+
var init_utils = __esm(() => {
|
|
9
|
+
init_layout();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// packages/runtime/src/control-plane/runtime/image.ts
|
|
13
|
+
init_layout();
|
|
14
|
+
import { ptr as ptr3, toBuffer as toBuffer2 } from "bun:ffi";
|
|
15
|
+
import {
|
|
16
|
+
existsSync as existsSync3,
|
|
17
|
+
readFileSync as readFileSync2,
|
|
18
|
+
readdirSync,
|
|
19
|
+
mkdirSync as mkdirSync2,
|
|
20
|
+
writeFileSync,
|
|
21
|
+
renameSync as renameSync2,
|
|
22
|
+
rmSync as rmSync2,
|
|
23
|
+
cpSync,
|
|
24
|
+
lstatSync,
|
|
25
|
+
unlinkSync,
|
|
26
|
+
symlinkSync
|
|
27
|
+
} from "fs";
|
|
28
|
+
import { resolve as resolve7 } from "path";
|
|
29
|
+
|
|
30
|
+
// packages/runtime/src/control-plane/native/runtime-native.ts
|
|
31
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
32
|
+
import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync, statSync } from "fs";
|
|
33
|
+
import { tmpdir } from "os";
|
|
34
|
+
import { dirname, resolve } from "path";
|
|
35
|
+
var sharedNativeRuntimeOutputDir = resolve(tmpdir(), "rig-native");
|
|
36
|
+
var sharedNativeRuntimeOutputPath = resolve(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
37
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
38
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
39
|
+
function requireNativeRuntimeLibrary(feature) {
|
|
40
|
+
if (!nativeRuntimeLibrary) {
|
|
41
|
+
throw new Error(`Native Zig runtime is required for ${feature}`);
|
|
42
|
+
}
|
|
43
|
+
return nativeRuntimeLibrary;
|
|
44
|
+
}
|
|
45
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
46
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
47
|
+
return outputPath;
|
|
48
|
+
}
|
|
49
|
+
return !options.force && existsSync(outputPath) ? outputPath : null;
|
|
50
|
+
}
|
|
51
|
+
async function loadNativeRuntimeLibrary() {
|
|
52
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
56
|
+
if (!candidate || !existsSync(candidate)) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
60
|
+
if (loaded) {
|
|
61
|
+
return loaded;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
65
|
+
if (!builtLibraryPath) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
69
|
+
}
|
|
70
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
71
|
+
const candidates = [];
|
|
72
|
+
let cursor = resolve(fromDir);
|
|
73
|
+
for (let index = 0;index < 8; index += 1) {
|
|
74
|
+
for (const name of names) {
|
|
75
|
+
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));
|
|
76
|
+
}
|
|
77
|
+
const parent = dirname(cursor);
|
|
78
|
+
if (parent === cursor)
|
|
79
|
+
break;
|
|
80
|
+
cursor = parent;
|
|
81
|
+
}
|
|
82
|
+
return candidates;
|
|
83
|
+
}
|
|
84
|
+
function nativeRuntimeLibraryCandidates() {
|
|
85
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
86
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
87
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
88
|
+
return [...new Set([
|
|
89
|
+
explicit,
|
|
90
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
91
|
+
execDir ? resolve(execDir, colocatedNativeRuntimeFileName) : "",
|
|
92
|
+
execDir ? resolve(execDir, platformSpecific) : "",
|
|
93
|
+
execDir ? resolve(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
94
|
+
execDir ? resolve(execDir, "..", platformSpecific) : "",
|
|
95
|
+
execDir ? resolve(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
96
|
+
execDir ? resolve(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
97
|
+
sharedNativeRuntimeOutputPath
|
|
98
|
+
].filter(Boolean))];
|
|
99
|
+
}
|
|
100
|
+
function resolveNativeRuntimeSourcePath() {
|
|
101
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
102
|
+
if (explicit && existsSync(explicit)) {
|
|
103
|
+
return explicit;
|
|
104
|
+
}
|
|
105
|
+
const bundled = resolve(import.meta.dir, "../../../native/snapshot.zig");
|
|
106
|
+
return existsSync(bundled) ? bundled : null;
|
|
107
|
+
}
|
|
108
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
109
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const zigBinary = Bun.which("zig");
|
|
113
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
114
|
+
if (!zigBinary || !sourcePath) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
118
|
+
try {
|
|
119
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
120
|
+
const needsBuild = options.force === true || !existsSync(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
|
|
121
|
+
if (!needsBuild) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
const build = Bun.spawn([
|
|
125
|
+
zigBinary,
|
|
126
|
+
"build-lib",
|
|
127
|
+
sourcePath,
|
|
128
|
+
"-dynamic",
|
|
129
|
+
"-O",
|
|
130
|
+
"ReleaseFast",
|
|
131
|
+
`-femit-bin=${tempOutputPath}`
|
|
132
|
+
], {
|
|
133
|
+
cwd: import.meta.dir,
|
|
134
|
+
stdout: "pipe",
|
|
135
|
+
stderr: "pipe"
|
|
136
|
+
});
|
|
137
|
+
const exitCode = await build.exited;
|
|
138
|
+
if (exitCode !== 0 || !existsSync(tempOutputPath)) {
|
|
139
|
+
rmSync(tempOutputPath, { force: true });
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
renameSync(tempOutputPath, outputPath);
|
|
143
|
+
return true;
|
|
144
|
+
} catch {
|
|
145
|
+
rmSync(tempOutputPath, { force: true });
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
150
|
+
try {
|
|
151
|
+
return dlopen(outputPath, {
|
|
152
|
+
rig_scope_match: {
|
|
153
|
+
args: ["ptr", "ptr"],
|
|
154
|
+
returns: "u8"
|
|
155
|
+
},
|
|
156
|
+
snapshot_capture: {
|
|
157
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
158
|
+
returns: "ptr"
|
|
159
|
+
},
|
|
160
|
+
snapshot_delta: {
|
|
161
|
+
args: ["ptr", "ptr"],
|
|
162
|
+
returns: "ptr"
|
|
163
|
+
},
|
|
164
|
+
snapshot_store_delta: {
|
|
165
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
166
|
+
returns: "ptr"
|
|
167
|
+
},
|
|
168
|
+
snapshot_inspect_delta: {
|
|
169
|
+
args: ["ptr", "u64"],
|
|
170
|
+
returns: "ptr"
|
|
171
|
+
},
|
|
172
|
+
snapshot_apply_delta: {
|
|
173
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
174
|
+
returns: "ptr"
|
|
175
|
+
},
|
|
176
|
+
snapshot_release: {
|
|
177
|
+
args: ["ptr"],
|
|
178
|
+
returns: "void"
|
|
179
|
+
},
|
|
180
|
+
runtime_hash_file: {
|
|
181
|
+
args: ["ptr", "u64"],
|
|
182
|
+
returns: "ptr"
|
|
183
|
+
},
|
|
184
|
+
runtime_hash_tree: {
|
|
185
|
+
args: ["ptr", "u64"],
|
|
186
|
+
returns: "ptr"
|
|
187
|
+
},
|
|
188
|
+
runtime_prepare_paths: {
|
|
189
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
190
|
+
returns: "ptr"
|
|
191
|
+
},
|
|
192
|
+
runtime_link_dependency_layer: {
|
|
193
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
194
|
+
returns: "ptr"
|
|
195
|
+
},
|
|
196
|
+
runtime_scan_worktrees: {
|
|
197
|
+
args: ["ptr", "u64"],
|
|
198
|
+
returns: "ptr"
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
} catch {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// packages/runtime/src/control-plane/native/git-native.ts
|
|
207
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
208
|
+
import { dirname as dirname2, isAbsolute, resolve as resolve2 } from "path";
|
|
209
|
+
var sharedGitNativeOutputDir = resolve2(tmpdir2(), "rig-native");
|
|
210
|
+
var sharedGitNativeOutputPath = resolve2(sharedGitNativeOutputDir, `rig-git-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
|
|
211
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
212
|
+
import { ptr as ptr2 } from "bun:ffi";
|
|
213
|
+
init_layout();
|
|
214
|
+
|
|
215
|
+
// packages/runtime/src/control-plane/native/scope-rules.ts
|
|
216
|
+
var activeRules = null;
|
|
217
|
+
function getScopeRules() {
|
|
218
|
+
return activeRules;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
222
|
+
var nativeScopeMatcher = null;
|
|
223
|
+
var scopeRegexCache = new Map;
|
|
224
|
+
function unique(values) {
|
|
225
|
+
return [...new Set(values)];
|
|
226
|
+
}
|
|
227
|
+
function normalizeRelativeScopePath(inputPath) {
|
|
228
|
+
let normalized = inputPath.replace(/^\.\//, "");
|
|
229
|
+
const rules = getScopeRules();
|
|
230
|
+
if (rules?.stripPrefixes) {
|
|
231
|
+
for (const prefix of rules.stripPrefixes) {
|
|
232
|
+
if (normalized.startsWith(prefix)) {
|
|
233
|
+
normalized = normalized.slice(prefix.length);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return normalized;
|
|
238
|
+
}
|
|
239
|
+
function normalizePathToScope(projectRoot, monorepoRoot, inputPath) {
|
|
240
|
+
let normalized = inputPath.replace(/^\.\//, "");
|
|
241
|
+
if (normalized.startsWith(projectRoot + "/")) {
|
|
242
|
+
normalized = normalized.slice(projectRoot.length + 1);
|
|
243
|
+
}
|
|
244
|
+
if (normalized.startsWith(monorepoRoot + "/")) {
|
|
245
|
+
normalized = normalized.slice(monorepoRoot.length + 1);
|
|
246
|
+
}
|
|
247
|
+
return normalizeRelativeScopePath(normalized);
|
|
248
|
+
}
|
|
249
|
+
function scopeMatches(path, scopes) {
|
|
250
|
+
const matcher = getNativeScopeMatcher();
|
|
251
|
+
const pathVariants = unique([path, normalizeRelativeScopePath(path)]);
|
|
252
|
+
for (const scope of scopes) {
|
|
253
|
+
const scopeVariants = unique([scope, normalizeRelativeScopePath(scope)]);
|
|
254
|
+
for (const candidatePath of pathVariants) {
|
|
255
|
+
for (const candidateScope of scopeVariants) {
|
|
256
|
+
if (candidatePath === candidateScope) {
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
if (matcher.match(candidateScope, candidatePath)) {
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
function getNativeScopeMatcher() {
|
|
268
|
+
if (nativeScopeMatcher) {
|
|
269
|
+
return nativeScopeMatcher;
|
|
270
|
+
}
|
|
271
|
+
nativeScopeMatcher = createNativeScopeMatcher();
|
|
272
|
+
return nativeScopeMatcher;
|
|
273
|
+
}
|
|
274
|
+
function createNativeScopeMatcher() {
|
|
275
|
+
const library = requireNativeRuntimeLibrary("scope matching");
|
|
276
|
+
return {
|
|
277
|
+
match: (pattern, path) => {
|
|
278
|
+
const patternBuffer = Buffer.from(`${pattern}\x00`);
|
|
279
|
+
const pathBuffer = Buffer.from(`${path}\x00`);
|
|
280
|
+
return library.symbols.rig_scope_match(Number(ptr2(patternBuffer)), Number(ptr2(pathBuffer))) !== 0;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
// packages/runtime/src/control-plane/runtime/context.ts
|
|
285
|
+
var DEFAULT_RUNTIME_MEMORY_RETRIEVAL = {
|
|
286
|
+
topK: 5,
|
|
287
|
+
lexicalWeight: 0.35,
|
|
288
|
+
vectorWeight: 0.45,
|
|
289
|
+
recencyWeight: 0.1,
|
|
290
|
+
confidenceWeight: 0.1
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// packages/runtime/src/control-plane/memory-sync/query.ts
|
|
294
|
+
var DEFAULT_RESULT_LIMIT = DEFAULT_RUNTIME_MEMORY_RETRIEVAL.topK;
|
|
295
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
296
|
+
// packages/runtime/src/build-time-config.ts
|
|
297
|
+
function normalizeBuildConfig(value) {
|
|
298
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
299
|
+
return {};
|
|
300
|
+
}
|
|
301
|
+
return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string"));
|
|
302
|
+
}
|
|
303
|
+
function readBuildConfig() {
|
|
304
|
+
if (typeof __RIG_BUILD_CONFIG__ !== "undefined") {
|
|
305
|
+
return normalizeBuildConfig(__RIG_BUILD_CONFIG__);
|
|
306
|
+
}
|
|
307
|
+
const raw = process.env.RIG_BUILD_CONFIG_JSON?.trim();
|
|
308
|
+
if (!raw) {
|
|
309
|
+
return {};
|
|
310
|
+
}
|
|
311
|
+
try {
|
|
312
|
+
return normalizeBuildConfig(JSON.parse(raw));
|
|
313
|
+
} catch {
|
|
314
|
+
return {};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// packages/runtime/src/control-plane/runtime/tooling/shell.ts
|
|
319
|
+
import { tmpdir as tmpdir3 } from "os";
|
|
320
|
+
import { basename, dirname as dirname3, resolve as resolve3 } from "path";
|
|
321
|
+
var sharedNativeShellOutputDir = resolve3(tmpdir3(), "rig-native");
|
|
322
|
+
var sharedNativeShellOutputPath = resolve3(sharedNativeShellOutputDir, `rig-shell-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
|
|
323
|
+
// packages/runtime/src/control-plane/runtime/tooling/file-tools.ts
|
|
324
|
+
import { tmpdir as tmpdir4 } from "os";
|
|
325
|
+
import { basename as basename2, dirname as dirname4, resolve as resolve4 } from "path";
|
|
326
|
+
var sharedNativeToolsOutputDir = resolve4(tmpdir4(), "rig-native");
|
|
327
|
+
var sharedNativeToolsOutputPath = resolve4(sharedNativeToolsOutputDir, `rig-tools-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
|
|
328
|
+
// packages/runtime/src/control-plane/plugin-host-context.ts
|
|
329
|
+
import { createPluginHost } from "@rig/core";
|
|
330
|
+
import { loadConfig } from "@rig/core/load-config";
|
|
331
|
+
|
|
332
|
+
// packages/runtime/src/control-plane/repos/registry.ts
|
|
333
|
+
var MANAGED_REPOS = new Map;
|
|
334
|
+
|
|
335
|
+
// packages/runtime/src/control-plane/tasks/source-aware-task-config-source.ts
|
|
336
|
+
var STATUS_LABELS = new Set(["ready", "blocked", "in-progress", "under-review", "failed", "cancelled"]);
|
|
337
|
+
|
|
338
|
+
// packages/runtime/src/control-plane/state-sync/types.ts
|
|
339
|
+
var CANONICAL_TASK_LIFECYCLE_STATUSES = new Set([
|
|
340
|
+
"draft",
|
|
341
|
+
"open",
|
|
342
|
+
"ready",
|
|
343
|
+
"queued",
|
|
344
|
+
"in_progress",
|
|
345
|
+
"under_review",
|
|
346
|
+
"blocked",
|
|
347
|
+
"completed",
|
|
348
|
+
"cancelled"
|
|
349
|
+
]);
|
|
350
|
+
// packages/runtime/src/control-plane/repos/layout.ts
|
|
351
|
+
init_layout();
|
|
352
|
+
// packages/runtime/src/control-plane/state-sync/reconcile.ts
|
|
353
|
+
var STALE_CLAIM_MS = 24 * 60 * 60 * 1000;
|
|
354
|
+
// packages/runtime/src/control-plane/native/validator.ts
|
|
355
|
+
init_layout();
|
|
356
|
+
|
|
357
|
+
// packages/runtime/src/binary-run.ts
|
|
358
|
+
init_layout();
|
|
359
|
+
var runtimeBinaryBuildQueue = Promise.resolve();
|
|
360
|
+
|
|
361
|
+
// packages/runtime/src/control-plane/runtime/sandbox-utils.ts
|
|
362
|
+
init_utils();
|
|
363
|
+
|
|
364
|
+
// packages/runtime/src/control-plane/native/git-ops.ts
|
|
365
|
+
var TASK_ARTIFACT_STAGE_FALLBACK = new Set([
|
|
366
|
+
"changed-files.txt",
|
|
367
|
+
"contract-changes.md",
|
|
368
|
+
"decision-log.md",
|
|
369
|
+
"git-state.txt",
|
|
370
|
+
"next-actions.md",
|
|
371
|
+
"pr-state.json",
|
|
372
|
+
"task-result.json",
|
|
373
|
+
"validation-summary.json"
|
|
374
|
+
]);
|
|
375
|
+
|
|
376
|
+
// packages/runtime/src/control-plane/provider/runtime-instructions.ts
|
|
377
|
+
var CLAUDE_ROUTER_TOOL_NAMES = [
|
|
378
|
+
"`mcp__rig_runtime_tools__read`",
|
|
379
|
+
"`mcp__rig_runtime_tools__write`",
|
|
380
|
+
"`mcp__rig_runtime_tools__edit`",
|
|
381
|
+
"`mcp__rig_runtime_tools__glob`",
|
|
382
|
+
"`mcp__rig_runtime_tools__grep`"
|
|
383
|
+
].join(", ");
|
|
384
|
+
var CODEX_DYNAMIC_TOOL_NAMES = [
|
|
385
|
+
"`shell`",
|
|
386
|
+
"`read`",
|
|
387
|
+
"`write`",
|
|
388
|
+
"`edit`",
|
|
389
|
+
"`glob`",
|
|
390
|
+
"`grep`"
|
|
391
|
+
].join(", ");
|
|
392
|
+
|
|
393
|
+
// packages/runtime/src/control-plane/native/task-ops.ts
|
|
394
|
+
var BUILD_CONFIG = readBuildConfig();
|
|
395
|
+
var BAKED_INFO_OUTPUT = BUILD_CONFIG.AGENT_INFO_OUTPUT ?? "";
|
|
396
|
+
var BAKED_DEPS_OUTPUT = BUILD_CONFIG.AGENT_DEPS_OUTPUT ?? "";
|
|
397
|
+
var BAKED_STATUS_OUTPUT = BUILD_CONFIG.AGENT_STATUS_OUTPUT ?? "";
|
|
398
|
+
var REOPENABLE_TASK_STATUSES = new Set(["completed", "cancelled", "blocked"]);
|
|
399
|
+
var GENERATED_TASK_ARTIFACT_FILES = new Set([
|
|
400
|
+
"changed-files.txt",
|
|
401
|
+
"decision-log.md",
|
|
402
|
+
"next-actions.md",
|
|
403
|
+
"task-result.json",
|
|
404
|
+
"validation-summary.json",
|
|
405
|
+
"review-feedback.md",
|
|
406
|
+
"review-state.json",
|
|
407
|
+
"review-status.txt",
|
|
408
|
+
"review-greptile-raw.json",
|
|
409
|
+
"pr-state.json",
|
|
410
|
+
"git-state.txt"
|
|
411
|
+
]);
|
|
412
|
+
|
|
413
|
+
// packages/runtime/src/control-plane/runtime/isolation/index.ts
|
|
414
|
+
init_layout();
|
|
415
|
+
|
|
416
|
+
// packages/runtime/src/control-plane/runtime/overlay.ts
|
|
417
|
+
init_layout();
|
|
418
|
+
|
|
419
|
+
// packages/runtime/src/control-plane/runtime/isolation/shared.ts
|
|
420
|
+
var generatedCredentialFiles = new Set;
|
|
421
|
+
|
|
422
|
+
// packages/runtime/src/control-plane/runtime/isolation/home.ts
|
|
423
|
+
var GITHUB_KNOWN_HOSTS = [
|
|
424
|
+
"github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl",
|
|
425
|
+
"github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=",
|
|
426
|
+
"github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk="
|
|
427
|
+
].join(`
|
|
428
|
+
`);
|
|
429
|
+
|
|
430
|
+
// packages/runtime/src/control-plane/runtime/tooling/claude-router.ts
|
|
431
|
+
if (false) {}
|
|
432
|
+
// packages/runtime/src/control-plane/runtime/isolation/discovery.ts
|
|
433
|
+
init_layout();
|
|
434
|
+
|
|
435
|
+
// packages/runtime/src/control-plane/runtime/tooling/claude-router-binary.ts
|
|
436
|
+
import { tmpdir as tmpdir5 } from "os";
|
|
437
|
+
import { dirname as dirname5, resolve as resolve5 } from "path";
|
|
438
|
+
var sharedRouterOutputDir = resolve5(tmpdir5(), "rig-native");
|
|
439
|
+
var sharedRouterOutputPath = resolve5(sharedRouterOutputDir, `rig-tool-router-${process.platform}-${process.arch}${process.platform === "win32" ? ".exe" : ""}`);
|
|
440
|
+
// packages/runtime/src/control-plane/runtime/guard.ts
|
|
441
|
+
import { optimizeNextInvocation } from "bun:jsc";
|
|
442
|
+
import { existsSync as existsSync2, readFileSync, statSync as statSync2 } from "fs";
|
|
443
|
+
import { resolve as resolve6 } from "path";
|
|
444
|
+
|
|
445
|
+
// packages/runtime/src/control-plane/runtime/guard-types.ts
|
|
446
|
+
var POLICY_VERSION = 1;
|
|
447
|
+
|
|
448
|
+
// packages/runtime/src/control-plane/runtime/guard.ts
|
|
449
|
+
var DEFAULT_SCOPE = {
|
|
450
|
+
fail_closed: true,
|
|
451
|
+
harness_paths_exempt: true,
|
|
452
|
+
runtime_paths_exempt: true
|
|
453
|
+
};
|
|
454
|
+
var DEFAULT_SANDBOX = {
|
|
455
|
+
mode: "enforce",
|
|
456
|
+
network: true,
|
|
457
|
+
read_deny: [],
|
|
458
|
+
write_allow_from_runtime: true
|
|
459
|
+
};
|
|
460
|
+
var DEFAULT_ISOLATION = {
|
|
461
|
+
default_mode: "worktree",
|
|
462
|
+
repo_symlink_fallback: false,
|
|
463
|
+
strict_provisioning: true,
|
|
464
|
+
fail_closed_on_provision_error: true
|
|
465
|
+
};
|
|
466
|
+
var DEFAULT_COMPLETION = {
|
|
467
|
+
derive_checks_from_scope: true,
|
|
468
|
+
checks: [],
|
|
469
|
+
typescript_config_probe: ["tsconfig.json"],
|
|
470
|
+
eslint_config_probe: [".eslintrc.js", ".eslintrc.json", "eslint.config.js"]
|
|
471
|
+
};
|
|
472
|
+
var DEFAULT_RUNTIME_IMAGE = {
|
|
473
|
+
deps: {
|
|
474
|
+
monorepo_install: false,
|
|
475
|
+
hp_next_install: false
|
|
476
|
+
},
|
|
477
|
+
plugins_require_binaries: true
|
|
478
|
+
};
|
|
479
|
+
var DEFAULT_RUNTIME_SNAPSHOT = {
|
|
480
|
+
enabled: true
|
|
481
|
+
};
|
|
482
|
+
function defaultPolicy() {
|
|
483
|
+
return {
|
|
484
|
+
version: POLICY_VERSION,
|
|
485
|
+
mode: "enforce",
|
|
486
|
+
scope: { ...DEFAULT_SCOPE },
|
|
487
|
+
rules: [],
|
|
488
|
+
sandbox: { ...DEFAULT_SANDBOX },
|
|
489
|
+
isolation: { ...DEFAULT_ISOLATION },
|
|
490
|
+
completion: { ...DEFAULT_COMPLETION },
|
|
491
|
+
runtime_image: {
|
|
492
|
+
deps: { ...DEFAULT_RUNTIME_IMAGE.deps },
|
|
493
|
+
plugins_require_binaries: DEFAULT_RUNTIME_IMAGE.plugins_require_binaries
|
|
494
|
+
},
|
|
495
|
+
runtime_snapshot: { ...DEFAULT_RUNTIME_SNAPSHOT }
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
var policyCache = null;
|
|
499
|
+
var policyCachePath = null;
|
|
500
|
+
var seededPolicyConfig = null;
|
|
501
|
+
var compiledRegexCache = new Map;
|
|
502
|
+
function loadPolicy(projectRoot) {
|
|
503
|
+
if (seededPolicyConfig) {
|
|
504
|
+
return seededPolicyConfig;
|
|
505
|
+
}
|
|
506
|
+
const configPath = resolve6(projectRoot, "rig/policy/policy.json");
|
|
507
|
+
if (!existsSync2(configPath)) {
|
|
508
|
+
return defaultPolicy();
|
|
509
|
+
}
|
|
510
|
+
let mtimeMs;
|
|
511
|
+
try {
|
|
512
|
+
mtimeMs = statSync2(configPath).mtimeMs;
|
|
513
|
+
} catch {
|
|
514
|
+
return defaultPolicy();
|
|
515
|
+
}
|
|
516
|
+
if (policyCache && policyCachePath === configPath && policyCache.mtimeMs === mtimeMs) {
|
|
517
|
+
return policyCache.config;
|
|
518
|
+
}
|
|
519
|
+
let parsed;
|
|
520
|
+
try {
|
|
521
|
+
parsed = JSON.parse(readFileSync(configPath, "utf-8"));
|
|
522
|
+
} catch {
|
|
523
|
+
return defaultPolicy();
|
|
524
|
+
}
|
|
525
|
+
const config = mergeWithDefaults(parsed);
|
|
526
|
+
policyCache = { mtimeMs, config };
|
|
527
|
+
policyCachePath = configPath;
|
|
528
|
+
return config;
|
|
529
|
+
}
|
|
530
|
+
function mergeWithDefaults(parsed) {
|
|
531
|
+
const base = defaultPolicy();
|
|
532
|
+
if (typeof parsed.mode === "string" && isValidMode(parsed.mode)) {
|
|
533
|
+
base.mode = parsed.mode;
|
|
534
|
+
}
|
|
535
|
+
if (parsed.scope && typeof parsed.scope === "object" && !Array.isArray(parsed.scope)) {
|
|
536
|
+
const s = parsed.scope;
|
|
537
|
+
if (typeof s.fail_closed === "boolean")
|
|
538
|
+
base.scope.fail_closed = s.fail_closed;
|
|
539
|
+
if (typeof s.harness_paths_exempt === "boolean")
|
|
540
|
+
base.scope.harness_paths_exempt = s.harness_paths_exempt;
|
|
541
|
+
if (typeof s.runtime_paths_exempt === "boolean")
|
|
542
|
+
base.scope.runtime_paths_exempt = s.runtime_paths_exempt;
|
|
543
|
+
}
|
|
544
|
+
if (Array.isArray(parsed.rules)) {
|
|
545
|
+
base.rules = precompilePolicyRuleRegexes(parsed.rules.filter(isValidRule));
|
|
546
|
+
}
|
|
547
|
+
if (Array.isArray(parsed.deny) && base.rules.length === 0) {
|
|
548
|
+
base.rules = precompilePolicyRuleRegexes(migrateLegacyDeny(parsed.deny));
|
|
549
|
+
}
|
|
550
|
+
if (parsed.sandbox && typeof parsed.sandbox === "object" && !Array.isArray(parsed.sandbox)) {
|
|
551
|
+
const sb = parsed.sandbox;
|
|
552
|
+
if (typeof sb.mode === "string" && isValidMode(sb.mode))
|
|
553
|
+
base.sandbox.mode = sb.mode;
|
|
554
|
+
if (typeof sb.network === "boolean")
|
|
555
|
+
base.sandbox.network = sb.network;
|
|
556
|
+
if (Array.isArray(sb.read_deny))
|
|
557
|
+
base.sandbox.read_deny = sb.read_deny.filter((v) => typeof v === "string");
|
|
558
|
+
if (typeof sb.write_allow_from_runtime === "boolean")
|
|
559
|
+
base.sandbox.write_allow_from_runtime = sb.write_allow_from_runtime;
|
|
560
|
+
}
|
|
561
|
+
if (parsed.isolation && typeof parsed.isolation === "object" && !Array.isArray(parsed.isolation)) {
|
|
562
|
+
const iso = parsed.isolation;
|
|
563
|
+
if (iso.default_mode === "worktree")
|
|
564
|
+
base.isolation.default_mode = iso.default_mode;
|
|
565
|
+
if (typeof iso.repo_symlink_fallback === "boolean")
|
|
566
|
+
base.isolation.repo_symlink_fallback = iso.repo_symlink_fallback;
|
|
567
|
+
if (typeof iso.strict_provisioning === "boolean")
|
|
568
|
+
base.isolation.strict_provisioning = iso.strict_provisioning;
|
|
569
|
+
if (typeof iso.fail_closed_on_provision_error === "boolean")
|
|
570
|
+
base.isolation.fail_closed_on_provision_error = iso.fail_closed_on_provision_error;
|
|
571
|
+
}
|
|
572
|
+
if (parsed.completion && typeof parsed.completion === "object" && !Array.isArray(parsed.completion)) {
|
|
573
|
+
const comp = parsed.completion;
|
|
574
|
+
if (typeof comp.derive_checks_from_scope === "boolean")
|
|
575
|
+
base.completion.derive_checks_from_scope = comp.derive_checks_from_scope;
|
|
576
|
+
if (Array.isArray(comp.checks))
|
|
577
|
+
base.completion.checks = comp.checks.filter((v) => typeof v === "string");
|
|
578
|
+
if (Array.isArray(comp.typescript_config_probe))
|
|
579
|
+
base.completion.typescript_config_probe = comp.typescript_config_probe.filter((v) => typeof v === "string");
|
|
580
|
+
if (Array.isArray(comp.eslint_config_probe))
|
|
581
|
+
base.completion.eslint_config_probe = comp.eslint_config_probe.filter((v) => typeof v === "string");
|
|
582
|
+
}
|
|
583
|
+
if (parsed.runtime_image && typeof parsed.runtime_image === "object" && !Array.isArray(parsed.runtime_image)) {
|
|
584
|
+
const runtimeImage = parsed.runtime_image;
|
|
585
|
+
if (runtimeImage.deps && typeof runtimeImage.deps === "object" && !Array.isArray(runtimeImage.deps)) {
|
|
586
|
+
const deps = runtimeImage.deps;
|
|
587
|
+
if (typeof deps.monorepo_install === "boolean") {
|
|
588
|
+
base.runtime_image.deps.monorepo_install = deps.monorepo_install;
|
|
589
|
+
}
|
|
590
|
+
if (typeof deps.hp_next_install === "boolean") {
|
|
591
|
+
base.runtime_image.deps.hp_next_install = deps.hp_next_install;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
if (typeof runtimeImage.plugins_require_binaries === "boolean") {
|
|
595
|
+
base.runtime_image.plugins_require_binaries = runtimeImage.plugins_require_binaries;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (parsed.runtime_snapshot && typeof parsed.runtime_snapshot === "object" && !Array.isArray(parsed.runtime_snapshot)) {
|
|
599
|
+
const runtimeSnapshot = parsed.runtime_snapshot;
|
|
600
|
+
if (typeof runtimeSnapshot.enabled === "boolean") {
|
|
601
|
+
base.runtime_snapshot.enabled = runtimeSnapshot.enabled;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return base;
|
|
605
|
+
}
|
|
606
|
+
function isValidMode(value) {
|
|
607
|
+
return value === "off" || value === "observe" || value === "enforce";
|
|
608
|
+
}
|
|
609
|
+
function isValidRule(value) {
|
|
610
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
611
|
+
return false;
|
|
612
|
+
const r = value;
|
|
613
|
+
return typeof r.id === "string" && typeof r.category === "string" && r.match != null && typeof r.match === "object";
|
|
614
|
+
}
|
|
615
|
+
function migrateLegacyDeny(deny) {
|
|
616
|
+
const rules = [];
|
|
617
|
+
for (const entry of deny) {
|
|
618
|
+
if (typeof entry.id !== "string")
|
|
619
|
+
continue;
|
|
620
|
+
const match = {};
|
|
621
|
+
if (typeof entry.pattern === "string")
|
|
622
|
+
match.pattern = entry.pattern;
|
|
623
|
+
if (typeof entry.regex === "string")
|
|
624
|
+
match.regex = entry.regex;
|
|
625
|
+
if (!match.pattern && !match.regex)
|
|
626
|
+
continue;
|
|
627
|
+
rules.push({
|
|
628
|
+
id: entry.id,
|
|
629
|
+
category: "command",
|
|
630
|
+
match,
|
|
631
|
+
action: "block",
|
|
632
|
+
...typeof entry.description === "string" ? { description: entry.description } : {}
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
return rules;
|
|
636
|
+
}
|
|
637
|
+
function precompilePolicyRuleRegexes(rules) {
|
|
638
|
+
return rules.map((rule) => {
|
|
639
|
+
const compiledRegex = rule.match.regex ? compileSafeRegex(rule.match.regex, `rules.${rule.id}.match.regex`, true) : undefined;
|
|
640
|
+
const compiledUnlessRegex = rule.unless?.regex ? compileSafeRegex(rule.unless.regex, `rules.${rule.id}.unless.regex`, true) : undefined;
|
|
641
|
+
return {
|
|
642
|
+
...rule,
|
|
643
|
+
...compiledRegex ? { compiledRegex } : {},
|
|
644
|
+
...compiledUnlessRegex ? { compiledUnlessRegex } : {}
|
|
645
|
+
};
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
function getRegexUnsafeReason(pattern) {
|
|
649
|
+
if (pattern.length > 512) {
|
|
650
|
+
return "pattern exceeds max safe length (512 chars)";
|
|
651
|
+
}
|
|
652
|
+
if (/\\[1-9]/.test(pattern)) {
|
|
653
|
+
return "pattern uses backreferences";
|
|
654
|
+
}
|
|
655
|
+
if (/\((?:[^()\\]|\\.)*[+*](?:[^()\\]|\\.)*\)\s*[*+{]/.test(pattern)) {
|
|
656
|
+
return "pattern contains nested quantifiers";
|
|
657
|
+
}
|
|
658
|
+
if (/\((?:[^()\\]|\\.)*\.\\?[+*](?:[^()\\]|\\.)*\)\s*[*+{]/.test(pattern)) {
|
|
659
|
+
return "pattern contains nested broad quantifiers";
|
|
660
|
+
}
|
|
661
|
+
return null;
|
|
662
|
+
}
|
|
663
|
+
function compileSafeRegex(pattern, sourceLabel, logOnFailure) {
|
|
664
|
+
const cached = compiledRegexCache.get(pattern);
|
|
665
|
+
if (cached !== undefined) {
|
|
666
|
+
return cached ?? undefined;
|
|
667
|
+
}
|
|
668
|
+
const unsafeReason = getRegexUnsafeReason(pattern);
|
|
669
|
+
if (unsafeReason) {
|
|
670
|
+
if (logOnFailure) {
|
|
671
|
+
console.warn(`[policy] Skipping unsafe regex in ${sourceLabel}: ${unsafeReason}`);
|
|
672
|
+
}
|
|
673
|
+
compiledRegexCache.set(pattern, null);
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
try {
|
|
677
|
+
const compiled = new RegExp(pattern);
|
|
678
|
+
compiledRegexCache.set(pattern, compiled);
|
|
679
|
+
return compiled;
|
|
680
|
+
} catch (error) {
|
|
681
|
+
if (logOnFailure) {
|
|
682
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
683
|
+
console.warn(`[policy] Skipping invalid regex in ${sourceLabel}: ${message}`);
|
|
684
|
+
}
|
|
685
|
+
compiledRegexCache.set(pattern, null);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
function matchRule(rule, input) {
|
|
690
|
+
const { match } = rule;
|
|
691
|
+
if (match.pattern && input.includes(match.pattern)) {
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
if (match.regex) {
|
|
695
|
+
const compiled = rule.compiledRegex || compileSafeRegex(match.regex, `rules.${rule.id}.match.regex`, false);
|
|
696
|
+
if (!compiled) {
|
|
697
|
+
return false;
|
|
698
|
+
}
|
|
699
|
+
try {
|
|
700
|
+
return compiled.test(input);
|
|
701
|
+
} catch {
|
|
702
|
+
return false;
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
return false;
|
|
706
|
+
}
|
|
707
|
+
function matchRuleUnless(rule, command, taskId) {
|
|
708
|
+
if (!rule.unless)
|
|
709
|
+
return false;
|
|
710
|
+
if (rule.unless.regex) {
|
|
711
|
+
const compiled = rule.compiledUnlessRegex || compileSafeRegex(rule.unless.regex, `rules.${rule.id}.unless.regex`, false);
|
|
712
|
+
if (!compiled) {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
try {
|
|
716
|
+
if (compiled.test(command))
|
|
717
|
+
return true;
|
|
718
|
+
} catch {}
|
|
719
|
+
}
|
|
720
|
+
if (rule.unless.task_in && taskId) {
|
|
721
|
+
if (rule.unless.task_in.includes(taskId))
|
|
722
|
+
return true;
|
|
723
|
+
}
|
|
724
|
+
return false;
|
|
725
|
+
}
|
|
726
|
+
function resolveAction(mode, matched) {
|
|
727
|
+
if (matched.length === 0)
|
|
728
|
+
return "allow";
|
|
729
|
+
if (mode === "off")
|
|
730
|
+
return "allow";
|
|
731
|
+
if (mode === "observe")
|
|
732
|
+
return "warn";
|
|
733
|
+
return "block";
|
|
734
|
+
}
|
|
735
|
+
function resolveAbsolutePath(projectRoot, rawPath) {
|
|
736
|
+
if (rawPath.startsWith("/"))
|
|
737
|
+
return resolve6(rawPath);
|
|
738
|
+
return resolve6(projectRoot, rawPath);
|
|
739
|
+
}
|
|
740
|
+
function isHarnessPath(projectRoot, rawPath) {
|
|
741
|
+
const absPath = resolveAbsolutePath(projectRoot, rawPath);
|
|
742
|
+
const managedRoots = [
|
|
743
|
+
resolve6(projectRoot, "rig"),
|
|
744
|
+
resolve6(projectRoot, ".rig"),
|
|
745
|
+
resolve6(projectRoot, "artifacts")
|
|
746
|
+
];
|
|
747
|
+
return managedRoots.some((root) => absPath === root || absPath.startsWith(root + "/"));
|
|
748
|
+
}
|
|
749
|
+
function isRuntimePath(projectRoot, rawPath, taskWorkspace) {
|
|
750
|
+
const absPath = resolveAbsolutePath(projectRoot, rawPath);
|
|
751
|
+
if (taskWorkspace) {
|
|
752
|
+
const workspaceRigRoot = resolve6(taskWorkspace, ".rig");
|
|
753
|
+
const workspaceArtifactsRoot = resolve6(taskWorkspace, "artifacts");
|
|
754
|
+
if (absPath === workspaceRigRoot || absPath.startsWith(workspaceRigRoot + "/") || absPath === workspaceArtifactsRoot || absPath.startsWith(workspaceArtifactsRoot + "/")) {
|
|
755
|
+
return true;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
const runtimeRoot = resolve6(projectRoot, ".rig/runtime/agents");
|
|
759
|
+
return absPath === runtimeRoot || absPath.startsWith(runtimeRoot + "/");
|
|
760
|
+
}
|
|
761
|
+
function isTestFile(path) {
|
|
762
|
+
return /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(path) || /\/(__tests__|tests|test)\//.test(path);
|
|
763
|
+
}
|
|
764
|
+
function evaluate(context) {
|
|
765
|
+
const policy = loadPolicy(context.projectRoot);
|
|
766
|
+
switch (context.evaluation.type) {
|
|
767
|
+
case "tool-call":
|
|
768
|
+
return evaluateToolCall(policy, context);
|
|
769
|
+
case "command":
|
|
770
|
+
return evaluateCommand(policy, context);
|
|
771
|
+
case "content-write":
|
|
772
|
+
return evaluateContent(policy, context);
|
|
773
|
+
case "file-access":
|
|
774
|
+
return evaluateScope(policy, context, context.evaluation.file_path, context.evaluation.access);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
function evaluateScope(policy, context, filePath, access) {
|
|
778
|
+
const allowed = () => ({
|
|
779
|
+
allowed: true,
|
|
780
|
+
matchedRules: [],
|
|
781
|
+
action: "allow",
|
|
782
|
+
failClosed: false
|
|
783
|
+
});
|
|
784
|
+
if (policy.scope.harness_paths_exempt && isHarnessPath(context.projectRoot, filePath)) {
|
|
785
|
+
return allowed();
|
|
786
|
+
}
|
|
787
|
+
if (policy.scope.runtime_paths_exempt && isRuntimePath(context.projectRoot, filePath, context.taskWorkspace)) {
|
|
788
|
+
return allowed();
|
|
789
|
+
}
|
|
790
|
+
if (!context.taskId) {
|
|
791
|
+
if (access === "write" && policy.scope.fail_closed) {
|
|
792
|
+
return {
|
|
793
|
+
allowed: false,
|
|
794
|
+
matchedRules: [],
|
|
795
|
+
action: resolveAction(policy.mode, [{ id: "scope:no-task", category: "command", reason: "No active task; fail-closed for write operations" }]),
|
|
796
|
+
failClosed: true
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
return allowed();
|
|
800
|
+
}
|
|
801
|
+
const scopes = context.taskScopes || [];
|
|
802
|
+
if (scopes.length === 0) {
|
|
803
|
+
return allowed();
|
|
804
|
+
}
|
|
805
|
+
if (context.taskWorkspace && context.taskWorkspace !== context.projectRoot && filePath.startsWith("/")) {
|
|
806
|
+
const absPath = resolve6(filePath);
|
|
807
|
+
if (!absPath.startsWith(context.taskWorkspace + "/") && !isHarnessPath(context.projectRoot, filePath)) {
|
|
808
|
+
const reason2 = `Absolute path '${filePath}' is outside task runtime boundary. Allowed root: ${context.taskWorkspace}`;
|
|
809
|
+
const matched2 = [{ id: "scope:workspace-boundary", category: "command", reason: reason2 }];
|
|
810
|
+
return {
|
|
811
|
+
allowed: policy.mode !== "enforce",
|
|
812
|
+
matchedRules: matched2,
|
|
813
|
+
action: resolveAction(policy.mode, matched2),
|
|
814
|
+
failClosed: false
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
const monorepoRoot = context.monorepoRoot || process.env.MONOREPO_ROOT?.trim() || context.taskWorkspace || context.projectRoot;
|
|
819
|
+
let normalizedPath = filePath;
|
|
820
|
+
if (context.taskWorkspace && context.taskWorkspace !== context.projectRoot && filePath.startsWith(context.taskWorkspace + "/")) {
|
|
821
|
+
normalizedPath = filePath.slice(context.taskWorkspace.length + 1);
|
|
822
|
+
}
|
|
823
|
+
normalizedPath = normalizePathToScope(context.projectRoot, monorepoRoot, normalizedPath);
|
|
824
|
+
if (scopeMatches(filePath, scopes) || scopeMatches(normalizedPath, scopes)) {
|
|
825
|
+
return allowed();
|
|
826
|
+
}
|
|
827
|
+
const reason = `File '${filePath}' (normalized: '${normalizedPath}') is outside scope of task ${context.taskId}`;
|
|
828
|
+
const matched = [{ id: "scope:out-of-scope", category: "command", reason }];
|
|
829
|
+
return {
|
|
830
|
+
allowed: policy.mode !== "enforce",
|
|
831
|
+
matchedRules: matched,
|
|
832
|
+
action: resolveAction(policy.mode, matched),
|
|
833
|
+
failClosed: false
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
function evaluateCommand(policy, context) {
|
|
837
|
+
const evaluation = context.evaluation;
|
|
838
|
+
if (evaluation.type !== "command") {
|
|
839
|
+
return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
|
|
840
|
+
}
|
|
841
|
+
const command = evaluation.command;
|
|
842
|
+
const matchedRules = [];
|
|
843
|
+
for (const rule of policy.rules) {
|
|
844
|
+
if (rule.category !== "command")
|
|
845
|
+
continue;
|
|
846
|
+
if (!matchRule(rule, command))
|
|
847
|
+
continue;
|
|
848
|
+
if (matchRuleUnless(rule, command, context.taskId))
|
|
849
|
+
continue;
|
|
850
|
+
matchedRules.push({
|
|
851
|
+
id: rule.id,
|
|
852
|
+
category: rule.category,
|
|
853
|
+
description: rule.description,
|
|
854
|
+
reason: rule.description || `Matched rule ${rule.id}`
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
const writeTarget = extractWriteTarget(command);
|
|
858
|
+
if (writeTarget && !/^\/dev\//.test(writeTarget) && !/^\/proc\//.test(writeTarget)) {
|
|
859
|
+
const scopeResult = evaluateScope(policy, context, writeTarget, "write");
|
|
860
|
+
if (!scopeResult.allowed || scopeResult.matchedRules.length > 0) {
|
|
861
|
+
matchedRules.push(...scopeResult.matchedRules);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
const action = resolveAction(policy.mode, matchedRules);
|
|
865
|
+
return {
|
|
866
|
+
allowed: action !== "block",
|
|
867
|
+
matchedRules,
|
|
868
|
+
action,
|
|
869
|
+
failClosed: false
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
function extractWriteTarget(command) {
|
|
873
|
+
const redirect = command.match(/>>?\s+([^\s;|&]+)/);
|
|
874
|
+
if (redirect?.[1])
|
|
875
|
+
return redirect[1];
|
|
876
|
+
const tee = command.match(/tee\s+(-a\s+)?([^\s;|&]+)/);
|
|
877
|
+
if (tee?.[2])
|
|
878
|
+
return tee[2];
|
|
879
|
+
return "";
|
|
880
|
+
}
|
|
881
|
+
function evaluateContent(policy, context) {
|
|
882
|
+
const evaluation = context.evaluation;
|
|
883
|
+
if (evaluation.type !== "content-write") {
|
|
884
|
+
return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
|
|
885
|
+
}
|
|
886
|
+
const { content, file_path } = evaluation;
|
|
887
|
+
const matchedRules = [];
|
|
888
|
+
const scopeResult = evaluateScope(policy, context, file_path, "write");
|
|
889
|
+
if (scopeResult.matchedRules.length > 0) {
|
|
890
|
+
matchedRules.push(...scopeResult.matchedRules);
|
|
891
|
+
}
|
|
892
|
+
for (const rule of policy.rules) {
|
|
893
|
+
if (rule.category !== "content" && rule.category !== "import" && rule.category !== "test-integrity")
|
|
894
|
+
continue;
|
|
895
|
+
if (rule.applies_to === "test-files" && !isTestFile(file_path))
|
|
896
|
+
continue;
|
|
897
|
+
if (!matchRule(rule, content))
|
|
898
|
+
continue;
|
|
899
|
+
if (matchRuleUnless(rule, content, context.taskId))
|
|
900
|
+
continue;
|
|
901
|
+
matchedRules.push({
|
|
902
|
+
id: rule.id,
|
|
903
|
+
category: rule.category,
|
|
904
|
+
description: rule.description,
|
|
905
|
+
reason: rule.description || `Matched rule ${rule.id}`
|
|
906
|
+
});
|
|
907
|
+
}
|
|
908
|
+
const action = resolveAction(policy.mode, matchedRules);
|
|
909
|
+
return {
|
|
910
|
+
allowed: action !== "block",
|
|
911
|
+
matchedRules,
|
|
912
|
+
action,
|
|
913
|
+
failClosed: false
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function evaluateToolCall(policy, context) {
|
|
917
|
+
const evaluation = context.evaluation;
|
|
918
|
+
if (evaluation.type !== "tool-call") {
|
|
919
|
+
return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
|
|
920
|
+
}
|
|
921
|
+
const { tool_name, tool_input } = evaluation;
|
|
922
|
+
const allMatched = [];
|
|
923
|
+
const filePaths = extractFilePathsFromToolInput(tool_name, tool_input);
|
|
924
|
+
for (const fp of filePaths) {
|
|
925
|
+
const access = isWriteTool(tool_name) ? "write" : "read";
|
|
926
|
+
const scopeResult = evaluateScope(policy, context, fp, access);
|
|
927
|
+
if (scopeResult.matchedRules.length > 0) {
|
|
928
|
+
allMatched.push(...scopeResult.matchedRules);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
const content = extractContentFromToolInput(tool_input);
|
|
932
|
+
if (content) {
|
|
933
|
+
const filePath = filePaths[0] || "";
|
|
934
|
+
const contentContext = {
|
|
935
|
+
...context,
|
|
936
|
+
evaluation: { type: "content-write", file_path: filePath, content }
|
|
937
|
+
};
|
|
938
|
+
const contentPolicy = loadPolicy(context.projectRoot);
|
|
939
|
+
for (const rule of contentPolicy.rules) {
|
|
940
|
+
if (rule.category !== "content" && rule.category !== "import" && rule.category !== "test-integrity")
|
|
941
|
+
continue;
|
|
942
|
+
if (rule.applies_to === "test-files" && !isTestFile(filePath))
|
|
943
|
+
continue;
|
|
944
|
+
if (!matchRule(rule, content))
|
|
945
|
+
continue;
|
|
946
|
+
if (matchRuleUnless(rule, content, context.taskId))
|
|
947
|
+
continue;
|
|
948
|
+
allMatched.push({
|
|
949
|
+
id: rule.id,
|
|
950
|
+
category: rule.category,
|
|
951
|
+
description: rule.description,
|
|
952
|
+
reason: rule.description || `Matched rule ${rule.id}`
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
if (tool_name === "Bash") {
|
|
957
|
+
const command = String(tool_input.command || tool_input.cmd || "");
|
|
958
|
+
if (command) {
|
|
959
|
+
const cmdContext = {
|
|
960
|
+
...context,
|
|
961
|
+
evaluation: { type: "command", command }
|
|
962
|
+
};
|
|
963
|
+
const cmdResult = evaluateCommand(policy, cmdContext);
|
|
964
|
+
if (cmdResult.matchedRules.length > 0) {
|
|
965
|
+
allMatched.push(...cmdResult.matchedRules);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
const seen = new Set;
|
|
970
|
+
const deduplicated = [];
|
|
971
|
+
for (const rule of allMatched) {
|
|
972
|
+
if (!seen.has(rule.id)) {
|
|
973
|
+
seen.add(rule.id);
|
|
974
|
+
deduplicated.push(rule);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
const action = resolveAction(policy.mode, deduplicated);
|
|
978
|
+
return {
|
|
979
|
+
allowed: action !== "block",
|
|
980
|
+
matchedRules: deduplicated,
|
|
981
|
+
action,
|
|
982
|
+
failClosed: false
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
function isWriteTool(toolName) {
|
|
986
|
+
return toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit";
|
|
987
|
+
}
|
|
988
|
+
function extractFilePathsFromToolInput(toolName, input) {
|
|
989
|
+
const paths = [];
|
|
990
|
+
const add = (value) => {
|
|
991
|
+
if (typeof value === "string" && value.trim()) {
|
|
992
|
+
paths.push(value.trim());
|
|
993
|
+
}
|
|
994
|
+
};
|
|
995
|
+
if (toolName === "Read" || toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit") {
|
|
996
|
+
add(input.file_path);
|
|
997
|
+
add(input.path);
|
|
998
|
+
} else if (toolName === "Glob") {
|
|
999
|
+
add(input.path);
|
|
1000
|
+
} else if (toolName === "Grep") {
|
|
1001
|
+
add(input.path);
|
|
1002
|
+
} else {
|
|
1003
|
+
add(input.file_path);
|
|
1004
|
+
add(input.path);
|
|
1005
|
+
}
|
|
1006
|
+
return paths;
|
|
1007
|
+
}
|
|
1008
|
+
function extractContentFromToolInput(input) {
|
|
1009
|
+
if (typeof input.content === "string")
|
|
1010
|
+
return input.content;
|
|
1011
|
+
if (typeof input.new_string === "string")
|
|
1012
|
+
return input.new_string;
|
|
1013
|
+
return "";
|
|
1014
|
+
}
|
|
1015
|
+
var guardHotPathPrimed = false;
|
|
1016
|
+
function primeGuardHotPaths() {
|
|
1017
|
+
if (guardHotPathPrimed) {
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
guardHotPathPrimed = true;
|
|
1021
|
+
try {
|
|
1022
|
+
optimizeNextInvocation(matchRule);
|
|
1023
|
+
optimizeNextInvocation(evaluate);
|
|
1024
|
+
} catch {}
|
|
1025
|
+
}
|
|
1026
|
+
primeGuardHotPaths();
|
|
1027
|
+
|
|
1028
|
+
// packages/runtime/src/control-plane/runtime/sandbox/backend.ts
|
|
1029
|
+
init_utils();
|
|
1030
|
+
// packages/runtime/src/control-plane/runtime/isolation/runner.ts
|
|
1031
|
+
init_layout();
|
|
1032
|
+
// packages/runtime/src/control-plane/runtime/image.ts
|
|
1033
|
+
var HASH_RESULT_SIZE = 40;
|
|
1034
|
+
var nativeRuntimeLibrary2 = null;
|
|
1035
|
+
function sha256Hex2(input) {
|
|
1036
|
+
const hasher = new Bun.CryptoHasher("sha256");
|
|
1037
|
+
hasher.update(input);
|
|
1038
|
+
return hasher.digest("hex");
|
|
1039
|
+
}
|
|
1040
|
+
function hashFile(filePath) {
|
|
1041
|
+
if (!existsSync3(filePath)) {
|
|
1042
|
+
return "";
|
|
1043
|
+
}
|
|
1044
|
+
return hashPathWithNative("runtime_hash_file", filePath);
|
|
1045
|
+
}
|
|
1046
|
+
function hashDirectory(dir) {
|
|
1047
|
+
if (!existsSync3(dir)) {
|
|
1048
|
+
return sha256Hex2("(empty)");
|
|
1049
|
+
}
|
|
1050
|
+
return hashPathWithNative("runtime_hash_tree", dir);
|
|
1051
|
+
}
|
|
1052
|
+
function hashDirectories(dirs) {
|
|
1053
|
+
const combined = new Bun.CryptoHasher("sha256");
|
|
1054
|
+
for (const { label, path } of dirs) {
|
|
1055
|
+
const dirHash = hashDirectory(path);
|
|
1056
|
+
combined.update(`${label}:${dirHash}
|
|
1057
|
+
`);
|
|
1058
|
+
}
|
|
1059
|
+
return combined.digest("hex");
|
|
1060
|
+
}
|
|
1061
|
+
async function probeVersion(args) {
|
|
1062
|
+
try {
|
|
1063
|
+
const proc = Bun.spawn(args, { stdout: "pipe", stderr: "pipe" });
|
|
1064
|
+
const [exitCode, stdout] = await Promise.all([
|
|
1065
|
+
proc.exited,
|
|
1066
|
+
new Response(proc.stdout).text()
|
|
1067
|
+
]);
|
|
1068
|
+
if (exitCode === 0) {
|
|
1069
|
+
return stdout.trim().split(`
|
|
1070
|
+
`)[0] ?? "";
|
|
1071
|
+
}
|
|
1072
|
+
return "";
|
|
1073
|
+
} catch {
|
|
1074
|
+
return "";
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
async function computeRuntimeImageFingerprintNative(projectRoot) {
|
|
1078
|
+
const [nodeVersion, claudeVersion] = await Promise.all([
|
|
1079
|
+
probeVersion(["node", "--version"]),
|
|
1080
|
+
probeVersion(["claude", "--version"])
|
|
1081
|
+
]);
|
|
1082
|
+
const runtimeDir = resolve7(projectRoot, "packages", "runtime");
|
|
1083
|
+
const cliDir = resolve7(projectRoot, "packages", "cli");
|
|
1084
|
+
const serverDir = resolve7(projectRoot, "packages", "server");
|
|
1085
|
+
const runtimeCodeHash = hashDirectories([
|
|
1086
|
+
{ label: "packages/runtime", path: runtimeDir },
|
|
1087
|
+
{ label: "packages/cli", path: cliDir },
|
|
1088
|
+
{ label: "packages/server", path: serverDir }
|
|
1089
|
+
]);
|
|
1090
|
+
const policyPath = resolve7(projectRoot, "rig/policy/policy.json");
|
|
1091
|
+
const policyHash = hashFile(policyPath);
|
|
1092
|
+
const pluginsDir = resolve7(projectRoot, "rig/plugins");
|
|
1093
|
+
const pluginsHash = hashDirectory(pluginsDir);
|
|
1094
|
+
const baseLockfiles = [
|
|
1095
|
+
"bun.lock",
|
|
1096
|
+
"bun.lockb",
|
|
1097
|
+
"package-lock.json",
|
|
1098
|
+
"package.json",
|
|
1099
|
+
"tsconfig.json"
|
|
1100
|
+
];
|
|
1101
|
+
const extra = process.env.RIG_RUNTIME_IMAGE_EXTRA_LOCKFILES?.trim();
|
|
1102
|
+
const lockfileCandidates = extra ? [...baseLockfiles, ...extra.split(",").map((p) => p.trim()).filter(Boolean)] : baseLockfiles;
|
|
1103
|
+
const lockfileHashes = {};
|
|
1104
|
+
for (const relPath of lockfileCandidates) {
|
|
1105
|
+
const fullPath = resolve7(projectRoot, relPath);
|
|
1106
|
+
if (existsSync3(fullPath)) {
|
|
1107
|
+
lockfileHashes[relPath] = hashFile(fullPath);
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
return {
|
|
1111
|
+
platform: process.platform,
|
|
1112
|
+
bunVersion: Bun.version,
|
|
1113
|
+
nodeVersion,
|
|
1114
|
+
claudeVersion,
|
|
1115
|
+
runtimeCodeHash,
|
|
1116
|
+
policyHash,
|
|
1117
|
+
pluginsHash,
|
|
1118
|
+
lockfileHashes
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
function hashPathWithNative(symbol, path) {
|
|
1122
|
+
const pathBuffer = Buffer.from(path, "utf8");
|
|
1123
|
+
const runtimeLibrary = getNativeRuntimeLibrary();
|
|
1124
|
+
const resultPtr = runtimeLibrary.symbols[symbol](Number(ptr3(pathBuffer)), pathBuffer.byteLength);
|
|
1125
|
+
if (!resultPtr) {
|
|
1126
|
+
throw new Error(`${symbol} returned null for ${path}`);
|
|
1127
|
+
}
|
|
1128
|
+
try {
|
|
1129
|
+
const view = viewAt(resultPtr, HASH_RESULT_SIZE);
|
|
1130
|
+
const error = readError(view, 24, 32);
|
|
1131
|
+
if (error) {
|
|
1132
|
+
throw new Error(`${symbol} failed: ${error}`);
|
|
1133
|
+
}
|
|
1134
|
+
return readString(readU64(view, 8), readU64(view, 16));
|
|
1135
|
+
} finally {
|
|
1136
|
+
runtimeLibrary.symbols.snapshot_release(resultPtr);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
function getNativeRuntimeLibrary() {
|
|
1140
|
+
if (nativeRuntimeLibrary2) {
|
|
1141
|
+
return nativeRuntimeLibrary2;
|
|
1142
|
+
}
|
|
1143
|
+
nativeRuntimeLibrary2 = requireNativeRuntimeLibrary("runtime image hashing");
|
|
1144
|
+
return nativeRuntimeLibrary2;
|
|
1145
|
+
}
|
|
1146
|
+
function viewAt(pointer, size) {
|
|
1147
|
+
const buffer = toBuffer2(pointer, 0, size);
|
|
1148
|
+
return new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
1149
|
+
}
|
|
1150
|
+
function readString(pointer, length) {
|
|
1151
|
+
if (!pointer || length === 0) {
|
|
1152
|
+
return "";
|
|
1153
|
+
}
|
|
1154
|
+
return Buffer.from(toBuffer2(pointer, 0, length)).toString("utf8");
|
|
1155
|
+
}
|
|
1156
|
+
function readU64(view, offset) {
|
|
1157
|
+
return Number(view.getBigUint64(offset, true));
|
|
1158
|
+
}
|
|
1159
|
+
function readError(view, pointerOffset, lengthOffset) {
|
|
1160
|
+
const errorPointer = readU64(view, pointerOffset);
|
|
1161
|
+
const errorLength = readU64(view, lengthOffset);
|
|
1162
|
+
if (!errorPointer || !errorLength) {
|
|
1163
|
+
return null;
|
|
1164
|
+
}
|
|
1165
|
+
return readString(errorPointer, errorLength);
|
|
1166
|
+
}
|
|
1167
|
+
var LOCK_STALE_MS = 10 * 60 * 1000;
|
|
1168
|
+
|
|
1169
|
+
// packages/runtime/src/control-plane/runtime/image-fingerprint-sidecar.ts
|
|
1170
|
+
function parseProjectRoot(argv) {
|
|
1171
|
+
for (let index = 0;index < argv.length; index += 1) {
|
|
1172
|
+
if (argv[index] === "--project-root") {
|
|
1173
|
+
return argv[index + 1] ?? "";
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
throw new Error("Usage: image-fingerprint-sidecar.ts --project-root <dir>");
|
|
1177
|
+
}
|
|
1178
|
+
async function runRuntimeImageFingerprintSidecar(argv = process.argv.slice(2)) {
|
|
1179
|
+
const projectRoot = parseProjectRoot(argv);
|
|
1180
|
+
const fingerprint = await computeRuntimeImageFingerprintNative(projectRoot);
|
|
1181
|
+
process.stdout.write(JSON.stringify(fingerprint));
|
|
1182
|
+
}
|
|
1183
|
+
if (import.meta.main) {
|
|
1184
|
+
runRuntimeImageFingerprintSidecar().catch((error) => {
|
|
1185
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
1186
|
+
process.exit(1);
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
export {
|
|
1190
|
+
runRuntimeImageFingerprintSidecar
|
|
1191
|
+
};
|