@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,880 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/runtime/src/control-plane/runtime/guard.ts
|
|
3
|
+
import { optimizeNextInvocation } from "bun:jsc";
|
|
4
|
+
import { existsSync as existsSync2, readFileSync, statSync as statSync2 } from "fs";
|
|
5
|
+
import { resolve as resolve2 } from "path";
|
|
6
|
+
|
|
7
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
8
|
+
import { ptr as ptr2 } from "bun:ffi";
|
|
9
|
+
|
|
10
|
+
// packages/runtime/src/control-plane/native/runtime-native.ts
|
|
11
|
+
import { dlopen, ptr, suffix, toBuffer } from "bun:ffi";
|
|
12
|
+
import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync, statSync } from "fs";
|
|
13
|
+
import { tmpdir } from "os";
|
|
14
|
+
import { dirname, resolve } from "path";
|
|
15
|
+
var sharedNativeRuntimeOutputDir = resolve(tmpdir(), "rig-native");
|
|
16
|
+
var sharedNativeRuntimeOutputPath = resolve(sharedNativeRuntimeOutputDir, `runtime-native-${process.platform}-${process.arch}.${suffix}`);
|
|
17
|
+
var colocatedNativeRuntimeFileName = `runtime-native.${suffix}`;
|
|
18
|
+
var nativeRuntimeLibrary = await loadNativeRuntimeLibrary();
|
|
19
|
+
function requireNativeRuntimeLibrary(feature) {
|
|
20
|
+
if (!nativeRuntimeLibrary) {
|
|
21
|
+
throw new Error(`Native Zig runtime is required for ${feature}`);
|
|
22
|
+
}
|
|
23
|
+
return nativeRuntimeLibrary;
|
|
24
|
+
}
|
|
25
|
+
async function ensureNativeRuntimeLibraryPath(outputPath = sharedNativeRuntimeOutputPath, options = {}) {
|
|
26
|
+
if (await buildNativeRuntimeLibrary(outputPath, options)) {
|
|
27
|
+
return outputPath;
|
|
28
|
+
}
|
|
29
|
+
return !options.force && existsSync(outputPath) ? outputPath : null;
|
|
30
|
+
}
|
|
31
|
+
async function loadNativeRuntimeLibrary() {
|
|
32
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
for (const candidate of nativeRuntimeLibraryCandidates()) {
|
|
36
|
+
if (!candidate || !existsSync(candidate)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const loaded = tryDlopenNativeRuntimeLibrary(candidate);
|
|
40
|
+
if (loaded) {
|
|
41
|
+
return loaded;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const builtLibraryPath = await ensureNativeRuntimeLibraryPath(sharedNativeRuntimeOutputPath, { force: true });
|
|
45
|
+
if (!builtLibraryPath) {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return tryDlopenNativeRuntimeLibrary(builtLibraryPath);
|
|
49
|
+
}
|
|
50
|
+
function nativePackageLibraryCandidates(fromDir, names) {
|
|
51
|
+
const candidates = [];
|
|
52
|
+
let cursor = resolve(fromDir);
|
|
53
|
+
for (let index = 0;index < 8; index += 1) {
|
|
54
|
+
for (const name of names) {
|
|
55
|
+
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));
|
|
56
|
+
}
|
|
57
|
+
const parent = dirname(cursor);
|
|
58
|
+
if (parent === cursor)
|
|
59
|
+
break;
|
|
60
|
+
cursor = parent;
|
|
61
|
+
}
|
|
62
|
+
return candidates;
|
|
63
|
+
}
|
|
64
|
+
function nativeRuntimeLibraryCandidates() {
|
|
65
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_LIB?.trim() || "";
|
|
66
|
+
const execDir = process.execPath?.trim() ? dirname(process.execPath.trim()) : "";
|
|
67
|
+
const platformSpecific = `runtime-native-${process.platform}-${process.arch}.${suffix}`;
|
|
68
|
+
return [...new Set([
|
|
69
|
+
explicit,
|
|
70
|
+
...nativePackageLibraryCandidates(import.meta.dir, [colocatedNativeRuntimeFileName, platformSpecific]),
|
|
71
|
+
execDir ? resolve(execDir, colocatedNativeRuntimeFileName) : "",
|
|
72
|
+
execDir ? resolve(execDir, platformSpecific) : "",
|
|
73
|
+
execDir ? resolve(execDir, "..", colocatedNativeRuntimeFileName) : "",
|
|
74
|
+
execDir ? resolve(execDir, "..", platformSpecific) : "",
|
|
75
|
+
execDir ? resolve(execDir, "lib", colocatedNativeRuntimeFileName) : "",
|
|
76
|
+
execDir ? resolve(execDir, "..", "lib", colocatedNativeRuntimeFileName) : "",
|
|
77
|
+
sharedNativeRuntimeOutputPath
|
|
78
|
+
].filter(Boolean))];
|
|
79
|
+
}
|
|
80
|
+
function resolveNativeRuntimeSourcePath() {
|
|
81
|
+
const explicit = process.env.RIG_NATIVE_RUNTIME_SOURCE?.trim();
|
|
82
|
+
if (explicit && existsSync(explicit)) {
|
|
83
|
+
return explicit;
|
|
84
|
+
}
|
|
85
|
+
const bundled = resolve(import.meta.dir, "../../../native/snapshot.zig");
|
|
86
|
+
return existsSync(bundled) ? bundled : null;
|
|
87
|
+
}
|
|
88
|
+
async function buildNativeRuntimeLibrary(outputPath, options = {}) {
|
|
89
|
+
if (process.env.RIG_DISABLE_ZIG_NATIVE === "1") {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const zigBinary = Bun.which("zig");
|
|
93
|
+
const sourcePath = resolveNativeRuntimeSourcePath();
|
|
94
|
+
if (!zigBinary || !sourcePath) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const tempOutputPath = `${outputPath}.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
98
|
+
try {
|
|
99
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
100
|
+
const needsBuild = options.force === true || !existsSync(outputPath) || statSync(sourcePath).mtimeMs > statSync(outputPath).mtimeMs;
|
|
101
|
+
if (!needsBuild) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
const build = Bun.spawn([
|
|
105
|
+
zigBinary,
|
|
106
|
+
"build-lib",
|
|
107
|
+
sourcePath,
|
|
108
|
+
"-dynamic",
|
|
109
|
+
"-O",
|
|
110
|
+
"ReleaseFast",
|
|
111
|
+
`-femit-bin=${tempOutputPath}`
|
|
112
|
+
], {
|
|
113
|
+
cwd: import.meta.dir,
|
|
114
|
+
stdout: "pipe",
|
|
115
|
+
stderr: "pipe"
|
|
116
|
+
});
|
|
117
|
+
const exitCode = await build.exited;
|
|
118
|
+
if (exitCode !== 0 || !existsSync(tempOutputPath)) {
|
|
119
|
+
rmSync(tempOutputPath, { force: true });
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
renameSync(tempOutputPath, outputPath);
|
|
123
|
+
return true;
|
|
124
|
+
} catch {
|
|
125
|
+
rmSync(tempOutputPath, { force: true });
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function tryDlopenNativeRuntimeLibrary(outputPath) {
|
|
130
|
+
try {
|
|
131
|
+
return dlopen(outputPath, {
|
|
132
|
+
rig_scope_match: {
|
|
133
|
+
args: ["ptr", "ptr"],
|
|
134
|
+
returns: "u8"
|
|
135
|
+
},
|
|
136
|
+
snapshot_capture: {
|
|
137
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
138
|
+
returns: "ptr"
|
|
139
|
+
},
|
|
140
|
+
snapshot_delta: {
|
|
141
|
+
args: ["ptr", "ptr"],
|
|
142
|
+
returns: "ptr"
|
|
143
|
+
},
|
|
144
|
+
snapshot_store_delta: {
|
|
145
|
+
args: ["ptr", "ptr", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
146
|
+
returns: "ptr"
|
|
147
|
+
},
|
|
148
|
+
snapshot_inspect_delta: {
|
|
149
|
+
args: ["ptr", "u64"],
|
|
150
|
+
returns: "ptr"
|
|
151
|
+
},
|
|
152
|
+
snapshot_apply_delta: {
|
|
153
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
154
|
+
returns: "ptr"
|
|
155
|
+
},
|
|
156
|
+
snapshot_release: {
|
|
157
|
+
args: ["ptr"],
|
|
158
|
+
returns: "void"
|
|
159
|
+
},
|
|
160
|
+
runtime_hash_file: {
|
|
161
|
+
args: ["ptr", "u64"],
|
|
162
|
+
returns: "ptr"
|
|
163
|
+
},
|
|
164
|
+
runtime_hash_tree: {
|
|
165
|
+
args: ["ptr", "u64"],
|
|
166
|
+
returns: "ptr"
|
|
167
|
+
},
|
|
168
|
+
runtime_prepare_paths: {
|
|
169
|
+
args: ["ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64", "ptr", "u64"],
|
|
170
|
+
returns: "ptr"
|
|
171
|
+
},
|
|
172
|
+
runtime_link_dependency_layer: {
|
|
173
|
+
args: ["ptr", "u64", "ptr", "u64"],
|
|
174
|
+
returns: "ptr"
|
|
175
|
+
},
|
|
176
|
+
runtime_scan_worktrees: {
|
|
177
|
+
args: ["ptr", "u64"],
|
|
178
|
+
returns: "ptr"
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// packages/runtime/src/control-plane/native/scope-rules.ts
|
|
187
|
+
var activeRules = null;
|
|
188
|
+
function getScopeRules() {
|
|
189
|
+
return activeRules;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// packages/runtime/src/control-plane/native/utils.ts
|
|
193
|
+
var nativeScopeMatcher = null;
|
|
194
|
+
var scopeRegexCache = new Map;
|
|
195
|
+
function unique(values) {
|
|
196
|
+
return [...new Set(values)];
|
|
197
|
+
}
|
|
198
|
+
function normalizeRelativeScopePath(inputPath) {
|
|
199
|
+
let normalized = inputPath.replace(/^\.\//, "");
|
|
200
|
+
const rules = getScopeRules();
|
|
201
|
+
if (rules?.stripPrefixes) {
|
|
202
|
+
for (const prefix of rules.stripPrefixes) {
|
|
203
|
+
if (normalized.startsWith(prefix)) {
|
|
204
|
+
normalized = normalized.slice(prefix.length);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return normalized;
|
|
209
|
+
}
|
|
210
|
+
function normalizePathToScope(projectRoot, monorepoRoot, inputPath) {
|
|
211
|
+
let normalized = inputPath.replace(/^\.\//, "");
|
|
212
|
+
if (normalized.startsWith(projectRoot + "/")) {
|
|
213
|
+
normalized = normalized.slice(projectRoot.length + 1);
|
|
214
|
+
}
|
|
215
|
+
if (normalized.startsWith(monorepoRoot + "/")) {
|
|
216
|
+
normalized = normalized.slice(monorepoRoot.length + 1);
|
|
217
|
+
}
|
|
218
|
+
return normalizeRelativeScopePath(normalized);
|
|
219
|
+
}
|
|
220
|
+
function scopeMatches(path, scopes) {
|
|
221
|
+
const matcher = getNativeScopeMatcher();
|
|
222
|
+
const pathVariants = unique([path, normalizeRelativeScopePath(path)]);
|
|
223
|
+
for (const scope of scopes) {
|
|
224
|
+
const scopeVariants = unique([scope, normalizeRelativeScopePath(scope)]);
|
|
225
|
+
for (const candidatePath of pathVariants) {
|
|
226
|
+
for (const candidateScope of scopeVariants) {
|
|
227
|
+
if (candidatePath === candidateScope) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
if (matcher.match(candidateScope, candidatePath)) {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
function getNativeScopeMatcher() {
|
|
239
|
+
if (nativeScopeMatcher) {
|
|
240
|
+
return nativeScopeMatcher;
|
|
241
|
+
}
|
|
242
|
+
nativeScopeMatcher = createNativeScopeMatcher();
|
|
243
|
+
return nativeScopeMatcher;
|
|
244
|
+
}
|
|
245
|
+
function createNativeScopeMatcher() {
|
|
246
|
+
const library = requireNativeRuntimeLibrary("scope matching");
|
|
247
|
+
return {
|
|
248
|
+
match: (pattern, path) => {
|
|
249
|
+
const patternBuffer = Buffer.from(`${pattern}\x00`);
|
|
250
|
+
const pathBuffer = Buffer.from(`${path}\x00`);
|
|
251
|
+
return library.symbols.rig_scope_match(Number(ptr2(patternBuffer)), Number(ptr2(pathBuffer))) !== 0;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// packages/runtime/src/control-plane/runtime/guard-types.ts
|
|
257
|
+
var POLICY_VERSION = 1;
|
|
258
|
+
|
|
259
|
+
// packages/runtime/src/control-plane/runtime/guard.ts
|
|
260
|
+
var DEFAULT_SCOPE = {
|
|
261
|
+
fail_closed: true,
|
|
262
|
+
harness_paths_exempt: true,
|
|
263
|
+
runtime_paths_exempt: true
|
|
264
|
+
};
|
|
265
|
+
var DEFAULT_SANDBOX = {
|
|
266
|
+
mode: "enforce",
|
|
267
|
+
network: true,
|
|
268
|
+
read_deny: [],
|
|
269
|
+
write_allow_from_runtime: true
|
|
270
|
+
};
|
|
271
|
+
var DEFAULT_ISOLATION = {
|
|
272
|
+
default_mode: "worktree",
|
|
273
|
+
repo_symlink_fallback: false,
|
|
274
|
+
strict_provisioning: true,
|
|
275
|
+
fail_closed_on_provision_error: true
|
|
276
|
+
};
|
|
277
|
+
var DEFAULT_COMPLETION = {
|
|
278
|
+
derive_checks_from_scope: true,
|
|
279
|
+
checks: [],
|
|
280
|
+
typescript_config_probe: ["tsconfig.json"],
|
|
281
|
+
eslint_config_probe: [".eslintrc.js", ".eslintrc.json", "eslint.config.js"]
|
|
282
|
+
};
|
|
283
|
+
var DEFAULT_RUNTIME_IMAGE = {
|
|
284
|
+
deps: {
|
|
285
|
+
monorepo_install: false,
|
|
286
|
+
hp_next_install: false
|
|
287
|
+
},
|
|
288
|
+
plugins_require_binaries: true
|
|
289
|
+
};
|
|
290
|
+
var DEFAULT_RUNTIME_SNAPSHOT = {
|
|
291
|
+
enabled: true
|
|
292
|
+
};
|
|
293
|
+
function defaultPolicy() {
|
|
294
|
+
return {
|
|
295
|
+
version: POLICY_VERSION,
|
|
296
|
+
mode: "enforce",
|
|
297
|
+
scope: { ...DEFAULT_SCOPE },
|
|
298
|
+
rules: [],
|
|
299
|
+
sandbox: { ...DEFAULT_SANDBOX },
|
|
300
|
+
isolation: { ...DEFAULT_ISOLATION },
|
|
301
|
+
completion: { ...DEFAULT_COMPLETION },
|
|
302
|
+
runtime_image: {
|
|
303
|
+
deps: { ...DEFAULT_RUNTIME_IMAGE.deps },
|
|
304
|
+
plugins_require_binaries: DEFAULT_RUNTIME_IMAGE.plugins_require_binaries
|
|
305
|
+
},
|
|
306
|
+
runtime_snapshot: { ...DEFAULT_RUNTIME_SNAPSHOT }
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
var policyCache = null;
|
|
310
|
+
var policyCachePath = null;
|
|
311
|
+
var seededPolicyConfig = null;
|
|
312
|
+
var compiledRegexCache = new Map;
|
|
313
|
+
function resetPolicyCache() {
|
|
314
|
+
policyCache = null;
|
|
315
|
+
policyCachePath = null;
|
|
316
|
+
seededPolicyConfig = null;
|
|
317
|
+
}
|
|
318
|
+
function seedPolicyFromContent(rawJson) {
|
|
319
|
+
try {
|
|
320
|
+
const parsed = JSON.parse(rawJson);
|
|
321
|
+
seededPolicyConfig = mergeWithDefaults(parsed);
|
|
322
|
+
} catch {}
|
|
323
|
+
}
|
|
324
|
+
function loadPolicy(projectRoot) {
|
|
325
|
+
if (seededPolicyConfig) {
|
|
326
|
+
return seededPolicyConfig;
|
|
327
|
+
}
|
|
328
|
+
const configPath = resolve2(projectRoot, "rig/policy/policy.json");
|
|
329
|
+
if (!existsSync2(configPath)) {
|
|
330
|
+
return defaultPolicy();
|
|
331
|
+
}
|
|
332
|
+
let mtimeMs;
|
|
333
|
+
try {
|
|
334
|
+
mtimeMs = statSync2(configPath).mtimeMs;
|
|
335
|
+
} catch {
|
|
336
|
+
return defaultPolicy();
|
|
337
|
+
}
|
|
338
|
+
if (policyCache && policyCachePath === configPath && policyCache.mtimeMs === mtimeMs) {
|
|
339
|
+
return policyCache.config;
|
|
340
|
+
}
|
|
341
|
+
let parsed;
|
|
342
|
+
try {
|
|
343
|
+
parsed = JSON.parse(readFileSync(configPath, "utf-8"));
|
|
344
|
+
} catch {
|
|
345
|
+
return defaultPolicy();
|
|
346
|
+
}
|
|
347
|
+
const config = mergeWithDefaults(parsed);
|
|
348
|
+
policyCache = { mtimeMs, config };
|
|
349
|
+
policyCachePath = configPath;
|
|
350
|
+
return config;
|
|
351
|
+
}
|
|
352
|
+
function mergeWithDefaults(parsed) {
|
|
353
|
+
const base = defaultPolicy();
|
|
354
|
+
if (typeof parsed.mode === "string" && isValidMode(parsed.mode)) {
|
|
355
|
+
base.mode = parsed.mode;
|
|
356
|
+
}
|
|
357
|
+
if (parsed.scope && typeof parsed.scope === "object" && !Array.isArray(parsed.scope)) {
|
|
358
|
+
const s = parsed.scope;
|
|
359
|
+
if (typeof s.fail_closed === "boolean")
|
|
360
|
+
base.scope.fail_closed = s.fail_closed;
|
|
361
|
+
if (typeof s.harness_paths_exempt === "boolean")
|
|
362
|
+
base.scope.harness_paths_exempt = s.harness_paths_exempt;
|
|
363
|
+
if (typeof s.runtime_paths_exempt === "boolean")
|
|
364
|
+
base.scope.runtime_paths_exempt = s.runtime_paths_exempt;
|
|
365
|
+
}
|
|
366
|
+
if (Array.isArray(parsed.rules)) {
|
|
367
|
+
base.rules = precompilePolicyRuleRegexes(parsed.rules.filter(isValidRule));
|
|
368
|
+
}
|
|
369
|
+
if (Array.isArray(parsed.deny) && base.rules.length === 0) {
|
|
370
|
+
base.rules = precompilePolicyRuleRegexes(migrateLegacyDeny(parsed.deny));
|
|
371
|
+
}
|
|
372
|
+
if (parsed.sandbox && typeof parsed.sandbox === "object" && !Array.isArray(parsed.sandbox)) {
|
|
373
|
+
const sb = parsed.sandbox;
|
|
374
|
+
if (typeof sb.mode === "string" && isValidMode(sb.mode))
|
|
375
|
+
base.sandbox.mode = sb.mode;
|
|
376
|
+
if (typeof sb.network === "boolean")
|
|
377
|
+
base.sandbox.network = sb.network;
|
|
378
|
+
if (Array.isArray(sb.read_deny))
|
|
379
|
+
base.sandbox.read_deny = sb.read_deny.filter((v) => typeof v === "string");
|
|
380
|
+
if (typeof sb.write_allow_from_runtime === "boolean")
|
|
381
|
+
base.sandbox.write_allow_from_runtime = sb.write_allow_from_runtime;
|
|
382
|
+
}
|
|
383
|
+
if (parsed.isolation && typeof parsed.isolation === "object" && !Array.isArray(parsed.isolation)) {
|
|
384
|
+
const iso = parsed.isolation;
|
|
385
|
+
if (iso.default_mode === "worktree")
|
|
386
|
+
base.isolation.default_mode = iso.default_mode;
|
|
387
|
+
if (typeof iso.repo_symlink_fallback === "boolean")
|
|
388
|
+
base.isolation.repo_symlink_fallback = iso.repo_symlink_fallback;
|
|
389
|
+
if (typeof iso.strict_provisioning === "boolean")
|
|
390
|
+
base.isolation.strict_provisioning = iso.strict_provisioning;
|
|
391
|
+
if (typeof iso.fail_closed_on_provision_error === "boolean")
|
|
392
|
+
base.isolation.fail_closed_on_provision_error = iso.fail_closed_on_provision_error;
|
|
393
|
+
}
|
|
394
|
+
if (parsed.completion && typeof parsed.completion === "object" && !Array.isArray(parsed.completion)) {
|
|
395
|
+
const comp = parsed.completion;
|
|
396
|
+
if (typeof comp.derive_checks_from_scope === "boolean")
|
|
397
|
+
base.completion.derive_checks_from_scope = comp.derive_checks_from_scope;
|
|
398
|
+
if (Array.isArray(comp.checks))
|
|
399
|
+
base.completion.checks = comp.checks.filter((v) => typeof v === "string");
|
|
400
|
+
if (Array.isArray(comp.typescript_config_probe))
|
|
401
|
+
base.completion.typescript_config_probe = comp.typescript_config_probe.filter((v) => typeof v === "string");
|
|
402
|
+
if (Array.isArray(comp.eslint_config_probe))
|
|
403
|
+
base.completion.eslint_config_probe = comp.eslint_config_probe.filter((v) => typeof v === "string");
|
|
404
|
+
}
|
|
405
|
+
if (parsed.runtime_image && typeof parsed.runtime_image === "object" && !Array.isArray(parsed.runtime_image)) {
|
|
406
|
+
const runtimeImage = parsed.runtime_image;
|
|
407
|
+
if (runtimeImage.deps && typeof runtimeImage.deps === "object" && !Array.isArray(runtimeImage.deps)) {
|
|
408
|
+
const deps = runtimeImage.deps;
|
|
409
|
+
if (typeof deps.monorepo_install === "boolean") {
|
|
410
|
+
base.runtime_image.deps.monorepo_install = deps.monorepo_install;
|
|
411
|
+
}
|
|
412
|
+
if (typeof deps.hp_next_install === "boolean") {
|
|
413
|
+
base.runtime_image.deps.hp_next_install = deps.hp_next_install;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (typeof runtimeImage.plugins_require_binaries === "boolean") {
|
|
417
|
+
base.runtime_image.plugins_require_binaries = runtimeImage.plugins_require_binaries;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (parsed.runtime_snapshot && typeof parsed.runtime_snapshot === "object" && !Array.isArray(parsed.runtime_snapshot)) {
|
|
421
|
+
const runtimeSnapshot = parsed.runtime_snapshot;
|
|
422
|
+
if (typeof runtimeSnapshot.enabled === "boolean") {
|
|
423
|
+
base.runtime_snapshot.enabled = runtimeSnapshot.enabled;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
return base;
|
|
427
|
+
}
|
|
428
|
+
function isValidMode(value) {
|
|
429
|
+
return value === "off" || value === "observe" || value === "enforce";
|
|
430
|
+
}
|
|
431
|
+
function isValidRule(value) {
|
|
432
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
433
|
+
return false;
|
|
434
|
+
const r = value;
|
|
435
|
+
return typeof r.id === "string" && typeof r.category === "string" && r.match != null && typeof r.match === "object";
|
|
436
|
+
}
|
|
437
|
+
function migrateLegacyDeny(deny) {
|
|
438
|
+
const rules = [];
|
|
439
|
+
for (const entry of deny) {
|
|
440
|
+
if (typeof entry.id !== "string")
|
|
441
|
+
continue;
|
|
442
|
+
const match = {};
|
|
443
|
+
if (typeof entry.pattern === "string")
|
|
444
|
+
match.pattern = entry.pattern;
|
|
445
|
+
if (typeof entry.regex === "string")
|
|
446
|
+
match.regex = entry.regex;
|
|
447
|
+
if (!match.pattern && !match.regex)
|
|
448
|
+
continue;
|
|
449
|
+
rules.push({
|
|
450
|
+
id: entry.id,
|
|
451
|
+
category: "command",
|
|
452
|
+
match,
|
|
453
|
+
action: "block",
|
|
454
|
+
...typeof entry.description === "string" ? { description: entry.description } : {}
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
return rules;
|
|
458
|
+
}
|
|
459
|
+
function precompilePolicyRuleRegexes(rules) {
|
|
460
|
+
return rules.map((rule) => {
|
|
461
|
+
const compiledRegex = rule.match.regex ? compileSafeRegex(rule.match.regex, `rules.${rule.id}.match.regex`, true) : undefined;
|
|
462
|
+
const compiledUnlessRegex = rule.unless?.regex ? compileSafeRegex(rule.unless.regex, `rules.${rule.id}.unless.regex`, true) : undefined;
|
|
463
|
+
return {
|
|
464
|
+
...rule,
|
|
465
|
+
...compiledRegex ? { compiledRegex } : {},
|
|
466
|
+
...compiledUnlessRegex ? { compiledUnlessRegex } : {}
|
|
467
|
+
};
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
function getRegexUnsafeReason(pattern) {
|
|
471
|
+
if (pattern.length > 512) {
|
|
472
|
+
return "pattern exceeds max safe length (512 chars)";
|
|
473
|
+
}
|
|
474
|
+
if (/\\[1-9]/.test(pattern)) {
|
|
475
|
+
return "pattern uses backreferences";
|
|
476
|
+
}
|
|
477
|
+
if (/\((?:[^()\\]|\\.)*[+*](?:[^()\\]|\\.)*\)\s*[*+{]/.test(pattern)) {
|
|
478
|
+
return "pattern contains nested quantifiers";
|
|
479
|
+
}
|
|
480
|
+
if (/\((?:[^()\\]|\\.)*\.\\?[+*](?:[^()\\]|\\.)*\)\s*[*+{]/.test(pattern)) {
|
|
481
|
+
return "pattern contains nested broad quantifiers";
|
|
482
|
+
}
|
|
483
|
+
return null;
|
|
484
|
+
}
|
|
485
|
+
function compileSafeRegex(pattern, sourceLabel, logOnFailure) {
|
|
486
|
+
const cached = compiledRegexCache.get(pattern);
|
|
487
|
+
if (cached !== undefined) {
|
|
488
|
+
return cached ?? undefined;
|
|
489
|
+
}
|
|
490
|
+
const unsafeReason = getRegexUnsafeReason(pattern);
|
|
491
|
+
if (unsafeReason) {
|
|
492
|
+
if (logOnFailure) {
|
|
493
|
+
console.warn(`[policy] Skipping unsafe regex in ${sourceLabel}: ${unsafeReason}`);
|
|
494
|
+
}
|
|
495
|
+
compiledRegexCache.set(pattern, null);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
const compiled = new RegExp(pattern);
|
|
500
|
+
compiledRegexCache.set(pattern, compiled);
|
|
501
|
+
return compiled;
|
|
502
|
+
} catch (error) {
|
|
503
|
+
if (logOnFailure) {
|
|
504
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
505
|
+
console.warn(`[policy] Skipping invalid regex in ${sourceLabel}: ${message}`);
|
|
506
|
+
}
|
|
507
|
+
compiledRegexCache.set(pattern, null);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
function matchRule(rule, input) {
|
|
512
|
+
const { match } = rule;
|
|
513
|
+
if (match.pattern && input.includes(match.pattern)) {
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
if (match.regex) {
|
|
517
|
+
const compiled = rule.compiledRegex || compileSafeRegex(match.regex, `rules.${rule.id}.match.regex`, false);
|
|
518
|
+
if (!compiled) {
|
|
519
|
+
return false;
|
|
520
|
+
}
|
|
521
|
+
try {
|
|
522
|
+
return compiled.test(input);
|
|
523
|
+
} catch {
|
|
524
|
+
return false;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
function matchRuleUnless(rule, command, taskId) {
|
|
530
|
+
if (!rule.unless)
|
|
531
|
+
return false;
|
|
532
|
+
if (rule.unless.regex) {
|
|
533
|
+
const compiled = rule.compiledUnlessRegex || compileSafeRegex(rule.unless.regex, `rules.${rule.id}.unless.regex`, false);
|
|
534
|
+
if (!compiled) {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
try {
|
|
538
|
+
if (compiled.test(command))
|
|
539
|
+
return true;
|
|
540
|
+
} catch {}
|
|
541
|
+
}
|
|
542
|
+
if (rule.unless.task_in && taskId) {
|
|
543
|
+
if (rule.unless.task_in.includes(taskId))
|
|
544
|
+
return true;
|
|
545
|
+
}
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
function resolveAction(mode, matched) {
|
|
549
|
+
if (matched.length === 0)
|
|
550
|
+
return "allow";
|
|
551
|
+
if (mode === "off")
|
|
552
|
+
return "allow";
|
|
553
|
+
if (mode === "observe")
|
|
554
|
+
return "warn";
|
|
555
|
+
return "block";
|
|
556
|
+
}
|
|
557
|
+
function resolveAbsolutePath(projectRoot, rawPath) {
|
|
558
|
+
if (rawPath.startsWith("/"))
|
|
559
|
+
return resolve2(rawPath);
|
|
560
|
+
return resolve2(projectRoot, rawPath);
|
|
561
|
+
}
|
|
562
|
+
function isHarnessPath(projectRoot, rawPath) {
|
|
563
|
+
const absPath = resolveAbsolutePath(projectRoot, rawPath);
|
|
564
|
+
const managedRoots = [
|
|
565
|
+
resolve2(projectRoot, "rig"),
|
|
566
|
+
resolve2(projectRoot, ".rig"),
|
|
567
|
+
resolve2(projectRoot, "artifacts")
|
|
568
|
+
];
|
|
569
|
+
return managedRoots.some((root) => absPath === root || absPath.startsWith(root + "/"));
|
|
570
|
+
}
|
|
571
|
+
function isRuntimePath(projectRoot, rawPath, taskWorkspace) {
|
|
572
|
+
const absPath = resolveAbsolutePath(projectRoot, rawPath);
|
|
573
|
+
if (taskWorkspace) {
|
|
574
|
+
const workspaceRigRoot = resolve2(taskWorkspace, ".rig");
|
|
575
|
+
const workspaceArtifactsRoot = resolve2(taskWorkspace, "artifacts");
|
|
576
|
+
if (absPath === workspaceRigRoot || absPath.startsWith(workspaceRigRoot + "/") || absPath === workspaceArtifactsRoot || absPath.startsWith(workspaceArtifactsRoot + "/")) {
|
|
577
|
+
return true;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
const runtimeRoot = resolve2(projectRoot, ".rig/runtime/agents");
|
|
581
|
+
return absPath === runtimeRoot || absPath.startsWith(runtimeRoot + "/");
|
|
582
|
+
}
|
|
583
|
+
function isTestFile(path) {
|
|
584
|
+
return /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(path) || /\/(__tests__|tests|test)\//.test(path);
|
|
585
|
+
}
|
|
586
|
+
function evaluate(context) {
|
|
587
|
+
const policy = loadPolicy(context.projectRoot);
|
|
588
|
+
switch (context.evaluation.type) {
|
|
589
|
+
case "tool-call":
|
|
590
|
+
return evaluateToolCall(policy, context);
|
|
591
|
+
case "command":
|
|
592
|
+
return evaluateCommand(policy, context);
|
|
593
|
+
case "content-write":
|
|
594
|
+
return evaluateContent(policy, context);
|
|
595
|
+
case "file-access":
|
|
596
|
+
return evaluateScope(policy, context, context.evaluation.file_path, context.evaluation.access);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function evaluateScope(policy, context, filePath, access) {
|
|
600
|
+
const allowed = () => ({
|
|
601
|
+
allowed: true,
|
|
602
|
+
matchedRules: [],
|
|
603
|
+
action: "allow",
|
|
604
|
+
failClosed: false
|
|
605
|
+
});
|
|
606
|
+
if (policy.scope.harness_paths_exempt && isHarnessPath(context.projectRoot, filePath)) {
|
|
607
|
+
return allowed();
|
|
608
|
+
}
|
|
609
|
+
if (policy.scope.runtime_paths_exempt && isRuntimePath(context.projectRoot, filePath, context.taskWorkspace)) {
|
|
610
|
+
return allowed();
|
|
611
|
+
}
|
|
612
|
+
if (!context.taskId) {
|
|
613
|
+
if (access === "write" && policy.scope.fail_closed) {
|
|
614
|
+
return {
|
|
615
|
+
allowed: false,
|
|
616
|
+
matchedRules: [],
|
|
617
|
+
action: resolveAction(policy.mode, [{ id: "scope:no-task", category: "command", reason: "No active task; fail-closed for write operations" }]),
|
|
618
|
+
failClosed: true
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
return allowed();
|
|
622
|
+
}
|
|
623
|
+
const scopes = context.taskScopes || [];
|
|
624
|
+
if (scopes.length === 0) {
|
|
625
|
+
return allowed();
|
|
626
|
+
}
|
|
627
|
+
if (context.taskWorkspace && context.taskWorkspace !== context.projectRoot && filePath.startsWith("/")) {
|
|
628
|
+
const absPath = resolve2(filePath);
|
|
629
|
+
if (!absPath.startsWith(context.taskWorkspace + "/") && !isHarnessPath(context.projectRoot, filePath)) {
|
|
630
|
+
const reason2 = `Absolute path '${filePath}' is outside task runtime boundary. Allowed root: ${context.taskWorkspace}`;
|
|
631
|
+
const matched2 = [{ id: "scope:workspace-boundary", category: "command", reason: reason2 }];
|
|
632
|
+
return {
|
|
633
|
+
allowed: policy.mode !== "enforce",
|
|
634
|
+
matchedRules: matched2,
|
|
635
|
+
action: resolveAction(policy.mode, matched2),
|
|
636
|
+
failClosed: false
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
const monorepoRoot = context.monorepoRoot || process.env.MONOREPO_ROOT?.trim() || context.taskWorkspace || context.projectRoot;
|
|
641
|
+
let normalizedPath = filePath;
|
|
642
|
+
if (context.taskWorkspace && context.taskWorkspace !== context.projectRoot && filePath.startsWith(context.taskWorkspace + "/")) {
|
|
643
|
+
normalizedPath = filePath.slice(context.taskWorkspace.length + 1);
|
|
644
|
+
}
|
|
645
|
+
normalizedPath = normalizePathToScope(context.projectRoot, monorepoRoot, normalizedPath);
|
|
646
|
+
if (scopeMatches(filePath, scopes) || scopeMatches(normalizedPath, scopes)) {
|
|
647
|
+
return allowed();
|
|
648
|
+
}
|
|
649
|
+
const reason = `File '${filePath}' (normalized: '${normalizedPath}') is outside scope of task ${context.taskId}`;
|
|
650
|
+
const matched = [{ id: "scope:out-of-scope", category: "command", reason }];
|
|
651
|
+
return {
|
|
652
|
+
allowed: policy.mode !== "enforce",
|
|
653
|
+
matchedRules: matched,
|
|
654
|
+
action: resolveAction(policy.mode, matched),
|
|
655
|
+
failClosed: false
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
function evaluateCommand(policy, context) {
|
|
659
|
+
const evaluation = context.evaluation;
|
|
660
|
+
if (evaluation.type !== "command") {
|
|
661
|
+
return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
|
|
662
|
+
}
|
|
663
|
+
const command = evaluation.command;
|
|
664
|
+
const matchedRules = [];
|
|
665
|
+
for (const rule of policy.rules) {
|
|
666
|
+
if (rule.category !== "command")
|
|
667
|
+
continue;
|
|
668
|
+
if (!matchRule(rule, command))
|
|
669
|
+
continue;
|
|
670
|
+
if (matchRuleUnless(rule, command, context.taskId))
|
|
671
|
+
continue;
|
|
672
|
+
matchedRules.push({
|
|
673
|
+
id: rule.id,
|
|
674
|
+
category: rule.category,
|
|
675
|
+
description: rule.description,
|
|
676
|
+
reason: rule.description || `Matched rule ${rule.id}`
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
const writeTarget = extractWriteTarget(command);
|
|
680
|
+
if (writeTarget && !/^\/dev\//.test(writeTarget) && !/^\/proc\//.test(writeTarget)) {
|
|
681
|
+
const scopeResult = evaluateScope(policy, context, writeTarget, "write");
|
|
682
|
+
if (!scopeResult.allowed || scopeResult.matchedRules.length > 0) {
|
|
683
|
+
matchedRules.push(...scopeResult.matchedRules);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
const action = resolveAction(policy.mode, matchedRules);
|
|
687
|
+
return {
|
|
688
|
+
allowed: action !== "block",
|
|
689
|
+
matchedRules,
|
|
690
|
+
action,
|
|
691
|
+
failClosed: false
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
function extractWriteTarget(command) {
|
|
695
|
+
const redirect = command.match(/>>?\s+([^\s;|&]+)/);
|
|
696
|
+
if (redirect?.[1])
|
|
697
|
+
return redirect[1];
|
|
698
|
+
const tee = command.match(/tee\s+(-a\s+)?([^\s;|&]+)/);
|
|
699
|
+
if (tee?.[2])
|
|
700
|
+
return tee[2];
|
|
701
|
+
return "";
|
|
702
|
+
}
|
|
703
|
+
function evaluateContent(policy, context) {
|
|
704
|
+
const evaluation = context.evaluation;
|
|
705
|
+
if (evaluation.type !== "content-write") {
|
|
706
|
+
return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
|
|
707
|
+
}
|
|
708
|
+
const { content, file_path } = evaluation;
|
|
709
|
+
const matchedRules = [];
|
|
710
|
+
const scopeResult = evaluateScope(policy, context, file_path, "write");
|
|
711
|
+
if (scopeResult.matchedRules.length > 0) {
|
|
712
|
+
matchedRules.push(...scopeResult.matchedRules);
|
|
713
|
+
}
|
|
714
|
+
for (const rule of policy.rules) {
|
|
715
|
+
if (rule.category !== "content" && rule.category !== "import" && rule.category !== "test-integrity")
|
|
716
|
+
continue;
|
|
717
|
+
if (rule.applies_to === "test-files" && !isTestFile(file_path))
|
|
718
|
+
continue;
|
|
719
|
+
if (!matchRule(rule, content))
|
|
720
|
+
continue;
|
|
721
|
+
if (matchRuleUnless(rule, content, context.taskId))
|
|
722
|
+
continue;
|
|
723
|
+
matchedRules.push({
|
|
724
|
+
id: rule.id,
|
|
725
|
+
category: rule.category,
|
|
726
|
+
description: rule.description,
|
|
727
|
+
reason: rule.description || `Matched rule ${rule.id}`
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
const action = resolveAction(policy.mode, matchedRules);
|
|
731
|
+
return {
|
|
732
|
+
allowed: action !== "block",
|
|
733
|
+
matchedRules,
|
|
734
|
+
action,
|
|
735
|
+
failClosed: false
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
function evaluateToolCall(policy, context) {
|
|
739
|
+
const evaluation = context.evaluation;
|
|
740
|
+
if (evaluation.type !== "tool-call") {
|
|
741
|
+
return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
|
|
742
|
+
}
|
|
743
|
+
const { tool_name, tool_input } = evaluation;
|
|
744
|
+
const allMatched = [];
|
|
745
|
+
const filePaths = extractFilePathsFromToolInput(tool_name, tool_input);
|
|
746
|
+
for (const fp of filePaths) {
|
|
747
|
+
const access = isWriteTool(tool_name) ? "write" : "read";
|
|
748
|
+
const scopeResult = evaluateScope(policy, context, fp, access);
|
|
749
|
+
if (scopeResult.matchedRules.length > 0) {
|
|
750
|
+
allMatched.push(...scopeResult.matchedRules);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
const content = extractContentFromToolInput(tool_input);
|
|
754
|
+
if (content) {
|
|
755
|
+
const filePath = filePaths[0] || "";
|
|
756
|
+
const contentContext = {
|
|
757
|
+
...context,
|
|
758
|
+
evaluation: { type: "content-write", file_path: filePath, content }
|
|
759
|
+
};
|
|
760
|
+
const contentPolicy = loadPolicy(context.projectRoot);
|
|
761
|
+
for (const rule of contentPolicy.rules) {
|
|
762
|
+
if (rule.category !== "content" && rule.category !== "import" && rule.category !== "test-integrity")
|
|
763
|
+
continue;
|
|
764
|
+
if (rule.applies_to === "test-files" && !isTestFile(filePath))
|
|
765
|
+
continue;
|
|
766
|
+
if (!matchRule(rule, content))
|
|
767
|
+
continue;
|
|
768
|
+
if (matchRuleUnless(rule, content, context.taskId))
|
|
769
|
+
continue;
|
|
770
|
+
allMatched.push({
|
|
771
|
+
id: rule.id,
|
|
772
|
+
category: rule.category,
|
|
773
|
+
description: rule.description,
|
|
774
|
+
reason: rule.description || `Matched rule ${rule.id}`
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
if (tool_name === "Bash") {
|
|
779
|
+
const command = String(tool_input.command || tool_input.cmd || "");
|
|
780
|
+
if (command) {
|
|
781
|
+
const cmdContext = {
|
|
782
|
+
...context,
|
|
783
|
+
evaluation: { type: "command", command }
|
|
784
|
+
};
|
|
785
|
+
const cmdResult = evaluateCommand(policy, cmdContext);
|
|
786
|
+
if (cmdResult.matchedRules.length > 0) {
|
|
787
|
+
allMatched.push(...cmdResult.matchedRules);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
const seen = new Set;
|
|
792
|
+
const deduplicated = [];
|
|
793
|
+
for (const rule of allMatched) {
|
|
794
|
+
if (!seen.has(rule.id)) {
|
|
795
|
+
seen.add(rule.id);
|
|
796
|
+
deduplicated.push(rule);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
const action = resolveAction(policy.mode, deduplicated);
|
|
800
|
+
return {
|
|
801
|
+
allowed: action !== "block",
|
|
802
|
+
matchedRules: deduplicated,
|
|
803
|
+
action,
|
|
804
|
+
failClosed: false
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
function isWriteTool(toolName) {
|
|
808
|
+
return toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit";
|
|
809
|
+
}
|
|
810
|
+
function extractFilePathsFromToolInput(toolName, input) {
|
|
811
|
+
const paths = [];
|
|
812
|
+
const add = (value) => {
|
|
813
|
+
if (typeof value === "string" && value.trim()) {
|
|
814
|
+
paths.push(value.trim());
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
if (toolName === "Read" || toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit") {
|
|
818
|
+
add(input.file_path);
|
|
819
|
+
add(input.path);
|
|
820
|
+
} else if (toolName === "Glob") {
|
|
821
|
+
add(input.path);
|
|
822
|
+
} else if (toolName === "Grep") {
|
|
823
|
+
add(input.path);
|
|
824
|
+
} else {
|
|
825
|
+
add(input.file_path);
|
|
826
|
+
add(input.path);
|
|
827
|
+
}
|
|
828
|
+
return paths;
|
|
829
|
+
}
|
|
830
|
+
function extractContentFromToolInput(input) {
|
|
831
|
+
if (typeof input.content === "string")
|
|
832
|
+
return input.content;
|
|
833
|
+
if (typeof input.new_string === "string")
|
|
834
|
+
return input.new_string;
|
|
835
|
+
return "";
|
|
836
|
+
}
|
|
837
|
+
function loadSandboxConfig(projectRoot) {
|
|
838
|
+
return loadPolicy(projectRoot).sandbox;
|
|
839
|
+
}
|
|
840
|
+
function loadIsolationConfig(projectRoot) {
|
|
841
|
+
return loadPolicy(projectRoot).isolation;
|
|
842
|
+
}
|
|
843
|
+
function loadCompletionConfig(projectRoot) {
|
|
844
|
+
return loadPolicy(projectRoot).completion;
|
|
845
|
+
}
|
|
846
|
+
function loadRuntimeImageConfig(projectRoot) {
|
|
847
|
+
return loadPolicy(projectRoot).runtime_image ?? {
|
|
848
|
+
deps: { ...DEFAULT_RUNTIME_IMAGE.deps },
|
|
849
|
+
plugins_require_binaries: DEFAULT_RUNTIME_IMAGE.plugins_require_binaries
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
function loadRuntimeSnapshotConfig(projectRoot) {
|
|
853
|
+
return loadPolicy(projectRoot).runtime_snapshot ?? { ...DEFAULT_RUNTIME_SNAPSHOT };
|
|
854
|
+
}
|
|
855
|
+
var guardHotPathPrimed = false;
|
|
856
|
+
function primeGuardHotPaths() {
|
|
857
|
+
if (guardHotPathPrimed) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
guardHotPathPrimed = true;
|
|
861
|
+
try {
|
|
862
|
+
optimizeNextInvocation(matchRule);
|
|
863
|
+
optimizeNextInvocation(evaluate);
|
|
864
|
+
} catch {}
|
|
865
|
+
}
|
|
866
|
+
primeGuardHotPaths();
|
|
867
|
+
export {
|
|
868
|
+
seedPolicyFromContent,
|
|
869
|
+
resolveAction,
|
|
870
|
+
resetPolicyCache,
|
|
871
|
+
matchRule,
|
|
872
|
+
loadSandboxConfig,
|
|
873
|
+
loadRuntimeSnapshotConfig,
|
|
874
|
+
loadRuntimeImageConfig,
|
|
875
|
+
loadPolicy,
|
|
876
|
+
loadIsolationConfig,
|
|
877
|
+
loadCompletionConfig,
|
|
878
|
+
isHarnessPath,
|
|
879
|
+
evaluate
|
|
880
|
+
};
|