@h-rig/cli-surface-plugin 0.0.6-alpha.156 → 0.0.6-alpha.158

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/dist/src/app/drone-ui.d.ts +0 -11
  2. package/dist/src/app/drone-ui.js +0 -114
  3. package/dist/src/commands/_async-ui.d.ts +1 -1
  4. package/dist/src/commands/_cli-format.d.ts +0 -29
  5. package/dist/src/commands/_cli-format.js +59 -113
  6. package/dist/src/commands/_connection-state.d.ts +6 -33
  7. package/dist/src/commands/_connection-state.js +654 -138
  8. package/dist/src/commands/_doctor-checks.d.ts +2 -5
  9. package/dist/src/commands/_doctor-checks.js +10 -9
  10. package/dist/src/commands/_help-catalog.d.ts +2 -1
  11. package/dist/src/commands/_help-catalog.js +654 -7
  12. package/dist/src/commands/_inprocess-services.d.ts +5 -5
  13. package/dist/src/commands/_inprocess-services.js +1 -1
  14. package/dist/src/commands/_parsers.js +651 -12
  15. package/dist/src/commands/_paths.d.ts +0 -2
  16. package/dist/src/commands/_paths.js +2 -10
  17. package/dist/src/commands/_pi-install.d.ts +2 -12
  18. package/dist/src/commands/_pi-install.js +3 -36
  19. package/dist/src/commands/_policy.js +659 -20
  20. package/dist/src/commands/agent.d.ts +1 -1
  21. package/dist/src/commands/agent.js +675 -24
  22. package/dist/src/commands/config.d.ts +1 -1
  23. package/dist/src/commands/config.js +656 -21
  24. package/dist/src/commands/dist.d.ts +1 -1
  25. package/dist/src/commands/dist.js +828 -102
  26. package/dist/src/commands/doctor.d.ts +1 -1
  27. package/dist/src/commands/doctor.js +658 -12
  28. package/dist/src/commands/github.d.ts +1 -1
  29. package/dist/src/commands/github.js +658 -19
  30. package/dist/src/commands/inbox.d.ts +12 -8
  31. package/dist/src/commands/inbox.js +741 -22
  32. package/dist/src/commands/init.d.ts +17 -19
  33. package/dist/src/commands/init.js +836 -306
  34. package/dist/src/commands/inspect.d.ts +5 -6
  35. package/dist/src/commands/inspect.js +754 -42
  36. package/dist/src/commands/pi.d.ts +1 -1
  37. package/dist/src/commands/pi.js +655 -16
  38. package/dist/src/commands/plugin.d.ts +9 -9
  39. package/dist/src/commands/plugin.js +652 -13
  40. package/dist/src/commands/profile-and-review.d.ts +1 -1
  41. package/dist/src/commands/profile-and-review.js +655 -16
  42. package/dist/src/commands/queue.d.ts +1 -1
  43. package/dist/src/commands/queue.js +871 -12
  44. package/dist/src/commands/remote-client.d.ts +152 -0
  45. package/dist/src/commands/remote-client.js +475 -0
  46. package/dist/src/commands/remote.d.ts +1 -1
  47. package/dist/src/commands/remote.js +1100 -29
  48. package/dist/src/commands/repo-git-harness.d.ts +1 -1
  49. package/dist/src/commands/repo-git-harness.js +2321 -47
  50. package/dist/src/commands/run.d.ts +10 -6
  51. package/dist/src/commands/run.js +830 -50
  52. package/dist/src/commands/server.d.ts +1 -1
  53. package/dist/src/commands/server.js +649 -11
  54. package/dist/src/commands/setup.d.ts +2 -2
  55. package/dist/src/commands/setup.js +829 -18
  56. package/dist/src/commands/stats.d.ts +2 -4
  57. package/dist/src/commands/stats.js +1299 -20
  58. package/dist/src/commands/test.d.ts +1 -1
  59. package/dist/src/commands/test.js +648 -9
  60. package/dist/src/commands/triage.d.ts +2 -3
  61. package/dist/src/commands/triage.js +657 -11
  62. package/dist/src/commands/workspace.d.ts +1 -1
  63. package/dist/src/commands/workspace.js +1280 -15
  64. package/dist/src/control-plane/agent-binary-build.d.ts +9 -0
  65. package/dist/src/control-plane/agent-binary-build.js +88 -0
  66. package/dist/src/control-plane/embedded-native-assets.d.ts +7 -0
  67. package/dist/src/control-plane/embedded-native-assets.js +6 -0
  68. package/dist/src/control-plane/guard.d.ts +17 -0
  69. package/dist/src/control-plane/guard.js +684 -0
  70. package/dist/src/control-plane/harness-cli.d.ts +12 -0
  71. package/dist/src/control-plane/harness-cli.js +1623 -0
  72. package/dist/src/control-plane/native/git-ops.d.ts +67 -0
  73. package/dist/src/control-plane/native/git-ops.js +1381 -0
  74. package/dist/src/control-plane/native/github-auth-env.d.ts +1 -0
  75. package/dist/src/control-plane/native/github-auth-env.js +21 -0
  76. package/dist/src/control-plane/native/host-git.d.ts +4 -0
  77. package/dist/src/control-plane/native/host-git.js +51 -0
  78. package/dist/src/control-plane/priority-queue.d.ts +22 -0
  79. package/dist/src/control-plane/priority-queue.js +212 -0
  80. package/dist/src/control-plane/rigfig.d.ts +9 -0
  81. package/dist/src/control-plane/rigfig.js +70 -0
  82. package/dist/src/control-plane/scope.d.ts +3 -0
  83. package/dist/src/control-plane/scope.js +58 -0
  84. package/dist/src/control-plane/setup-status.d.ts +44 -0
  85. package/dist/src/control-plane/setup-status.js +164 -0
  86. package/dist/src/control-plane/task-data.d.ts +2 -0
  87. package/dist/src/control-plane/task-data.js +12 -0
  88. package/dist/src/control-plane/workspace-ops.d.ts +79 -0
  89. package/dist/src/control-plane/workspace-ops.js +639 -0
  90. package/dist/src/help-catalog-data.d.ts +7 -0
  91. package/dist/src/help-catalog-data.js +660 -0
  92. package/dist/src/kernel-dispatch.js +1 -3
  93. package/dist/src/plugin.js +10072 -30
  94. package/dist/src/runner.d.ts +7 -9
  95. package/dist/src/runner.js +750 -30
  96. package/package.json +12 -13
  97. package/dist/src/commands/_json-output.d.ts +0 -11
  98. package/dist/src/commands/_json-output.js +0 -54
  99. package/dist/src/commands/_pi-frontend.d.ts +0 -35
  100. package/dist/src/commands/_pi-frontend.js +0 -64
  101. package/dist/src/commands/_run-driver-helpers.d.ts +0 -26
  102. package/dist/src/commands/_run-driver-helpers.js +0 -132
  103. package/dist/src/commands/task-run-driver.d.ts +0 -93
  104. package/dist/src/commands/task-run-driver.js +0 -136
  105. package/dist/src/commands/task.d.ts +0 -46
  106. package/dist/src/commands/task.js +0 -555
  107. package/dist/src/provider-model.d.ts +0 -34
  108. package/dist/src/provider-model.js +0 -56
  109. package/dist/src/rig-config-package-deps.d.ts +0 -10
  110. package/dist/src/rig-config-package-deps.js +0 -272
  111. package/dist/src/version.d.ts +0 -8
  112. package/dist/src/version.js +0 -47
@@ -1,21 +1,660 @@
1
1
  // @bun
2
2
  // packages/cli-surface-plugin/src/commands/_policy.ts
3
- import { appendFileSync, existsSync, mkdirSync, readFileSync } from "fs";
4
- import { resolve as resolve2 } from "path";
3
+ import { appendFileSync, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2 } from "fs";
4
+ import { resolve as resolve3 } from "path";
5
5
 
6
6
  // packages/cli-surface-plugin/src/runner.ts
7
- import { EventBus } from "@rig/runtime/control-plane/runtime/events";
8
- import { CliError as RuntimeCliError } from "@rig/runtime/control-plane/errors";
9
- import { evaluate, loadPolicy, resolveAction } from "@rig/runtime/control-plane/runtime/guard";
10
- import { buildBinary } from "@rig/runtime/control-plane/runtime/isolation";
7
+ import { EventBus } from "@rig/core/runtime-events";
8
+ import { CliError as RuntimeCliError } from "@rig/contracts";
9
+
10
+ // packages/cli-surface-plugin/src/control-plane/guard.ts
11
+ import { optimizeNextInvocation } from "bun:jsc";
12
+ import { existsSync, readFileSync, statSync } from "fs";
13
+ import { resolve } from "path";
11
14
 
15
+ // packages/cli-surface-plugin/src/control-plane/scope.ts
16
+ import { getScopeRules } from "@rig/core/scope-rules";
17
+ var scopeRegexCache = new Map;
18
+ function unique(values) {
19
+ return [...new Set(values)];
20
+ }
21
+ function normalizeRelativeScopePath(inputPath) {
22
+ let normalized = inputPath.replace(/^\.\//, "");
23
+ const rules = getScopeRules();
24
+ if (rules?.stripPrefixes) {
25
+ for (const prefix of rules.stripPrefixes) {
26
+ if (normalized.startsWith(prefix)) {
27
+ normalized = normalized.slice(prefix.length);
28
+ }
29
+ }
30
+ }
31
+ return normalized;
32
+ }
33
+ function normalizePathToScope(projectRoot, monorepoRoot, inputPath) {
34
+ let normalized = inputPath.replace(/^\.\//, "");
35
+ if (normalized.startsWith(projectRoot + "/")) {
36
+ normalized = normalized.slice(projectRoot.length + 1);
37
+ }
38
+ if (normalized.startsWith(monorepoRoot + "/")) {
39
+ normalized = normalized.slice(monorepoRoot.length + 1);
40
+ }
41
+ return normalizeRelativeScopePath(normalized);
42
+ }
43
+ function scopeGlobToRegex(glob) {
44
+ const cached = scopeRegexCache.get(glob);
45
+ if (cached) {
46
+ return cached;
47
+ }
48
+ const escaped = glob.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, "__GLOBSTAR__").replace(/\*/g, "[^/]*").replace(/__GLOBSTAR__/g, ".*");
49
+ const compiled = new RegExp(`^${escaped}$`);
50
+ scopeRegexCache.set(glob, compiled);
51
+ return compiled;
52
+ }
53
+ function scopeMatches(path, scopes) {
54
+ const pathVariants = unique([path, normalizeRelativeScopePath(path)]);
55
+ for (const scope of scopes) {
56
+ const scopeVariants = unique([scope, normalizeRelativeScopePath(scope)]);
57
+ for (const candidatePath of pathVariants) {
58
+ for (const candidateScope of scopeVariants) {
59
+ if (candidatePath === candidateScope || scopeGlobToRegex(candidateScope).test(candidatePath)) {
60
+ return true;
61
+ }
62
+ }
63
+ }
64
+ }
65
+ return false;
66
+ }
67
+
68
+ // packages/cli-surface-plugin/src/control-plane/guard.ts
69
+ import {
70
+ POLICY_VERSION
71
+ } from "@rig/contracts";
72
+ var DEFAULT_SCOPE = {
73
+ fail_closed: true,
74
+ harness_paths_exempt: true,
75
+ runtime_paths_exempt: true
76
+ };
77
+ var DEFAULT_SANDBOX = {
78
+ mode: "enforce",
79
+ network: true,
80
+ read_deny: [],
81
+ write_allow_from_runtime: true
82
+ };
83
+ var DEFAULT_ISOLATION = {
84
+ default_mode: "worktree",
85
+ repo_symlink_fallback: false,
86
+ strict_provisioning: true,
87
+ fail_closed_on_provision_error: true
88
+ };
89
+ var DEFAULT_COMPLETION = {
90
+ derive_checks_from_scope: true,
91
+ checks: [],
92
+ typescript_config_probe: ["tsconfig.json"],
93
+ eslint_config_probe: [".eslintrc.js", ".eslintrc.json", "eslint.config.js"]
94
+ };
95
+ var DEFAULT_RUNTIME_IMAGE = {
96
+ deps: {
97
+ monorepo_install: false,
98
+ hp_next_install: false
99
+ },
100
+ plugins_require_binaries: true
101
+ };
102
+ var DEFAULT_RUNTIME_SNAPSHOT = {
103
+ enabled: true
104
+ };
105
+ function defaultPolicy() {
106
+ return {
107
+ version: POLICY_VERSION,
108
+ mode: "enforce",
109
+ scope: { ...DEFAULT_SCOPE },
110
+ rules: [],
111
+ sandbox: { ...DEFAULT_SANDBOX },
112
+ isolation: { ...DEFAULT_ISOLATION },
113
+ completion: { ...DEFAULT_COMPLETION },
114
+ runtime_image: {
115
+ deps: { ...DEFAULT_RUNTIME_IMAGE.deps },
116
+ plugins_require_binaries: DEFAULT_RUNTIME_IMAGE.plugins_require_binaries
117
+ },
118
+ runtime_snapshot: { ...DEFAULT_RUNTIME_SNAPSHOT }
119
+ };
120
+ }
121
+ var policyCache = null;
122
+ var policyCachePath = null;
123
+ var seededPolicyConfig = null;
124
+ var compiledRegexCache = new Map;
125
+ function loadPolicy(projectRoot) {
126
+ if (seededPolicyConfig) {
127
+ return seededPolicyConfig;
128
+ }
129
+ const configPath = resolve(projectRoot, "rig/policy/policy.json");
130
+ if (!existsSync(configPath)) {
131
+ return defaultPolicy();
132
+ }
133
+ let mtimeMs;
134
+ try {
135
+ mtimeMs = statSync(configPath).mtimeMs;
136
+ } catch {
137
+ return defaultPolicy();
138
+ }
139
+ if (policyCache && policyCachePath === configPath && policyCache.mtimeMs === mtimeMs) {
140
+ return policyCache.config;
141
+ }
142
+ let parsed;
143
+ try {
144
+ parsed = JSON.parse(readFileSync(configPath, "utf-8"));
145
+ } catch {
146
+ return defaultPolicy();
147
+ }
148
+ const config = mergeWithDefaults(parsed);
149
+ policyCache = { mtimeMs, config };
150
+ policyCachePath = configPath;
151
+ return config;
152
+ }
153
+ function mergeWithDefaults(parsed) {
154
+ const base = defaultPolicy();
155
+ if (typeof parsed.mode === "string" && isValidMode(parsed.mode)) {
156
+ base.mode = parsed.mode;
157
+ }
158
+ if (parsed.scope && typeof parsed.scope === "object" && !Array.isArray(parsed.scope)) {
159
+ const s = parsed.scope;
160
+ if (typeof s.fail_closed === "boolean")
161
+ base.scope.fail_closed = s.fail_closed;
162
+ if (typeof s.harness_paths_exempt === "boolean")
163
+ base.scope.harness_paths_exempt = s.harness_paths_exempt;
164
+ if (typeof s.runtime_paths_exempt === "boolean")
165
+ base.scope.runtime_paths_exempt = s.runtime_paths_exempt;
166
+ }
167
+ if (Array.isArray(parsed.rules)) {
168
+ base.rules = precompilePolicyRuleRegexes(parsed.rules.filter(isValidRule));
169
+ }
170
+ if (Array.isArray(parsed.deny) && base.rules.length === 0) {
171
+ base.rules = precompilePolicyRuleRegexes(migrateLegacyDeny(parsed.deny));
172
+ }
173
+ if (parsed.sandbox && typeof parsed.sandbox === "object" && !Array.isArray(parsed.sandbox)) {
174
+ const sb = parsed.sandbox;
175
+ if (typeof sb.mode === "string" && isValidMode(sb.mode))
176
+ base.sandbox.mode = sb.mode;
177
+ if (typeof sb.network === "boolean")
178
+ base.sandbox.network = sb.network;
179
+ if (Array.isArray(sb.read_deny))
180
+ base.sandbox.read_deny = sb.read_deny.filter((v) => typeof v === "string");
181
+ if (typeof sb.write_allow_from_runtime === "boolean")
182
+ base.sandbox.write_allow_from_runtime = sb.write_allow_from_runtime;
183
+ }
184
+ if (parsed.isolation && typeof parsed.isolation === "object" && !Array.isArray(parsed.isolation)) {
185
+ const iso = parsed.isolation;
186
+ if (iso.default_mode === "worktree")
187
+ base.isolation.default_mode = iso.default_mode;
188
+ if (typeof iso.repo_symlink_fallback === "boolean")
189
+ base.isolation.repo_symlink_fallback = iso.repo_symlink_fallback;
190
+ if (typeof iso.strict_provisioning === "boolean")
191
+ base.isolation.strict_provisioning = iso.strict_provisioning;
192
+ if (typeof iso.fail_closed_on_provision_error === "boolean")
193
+ base.isolation.fail_closed_on_provision_error = iso.fail_closed_on_provision_error;
194
+ }
195
+ if (parsed.completion && typeof parsed.completion === "object" && !Array.isArray(parsed.completion)) {
196
+ const comp = parsed.completion;
197
+ if (typeof comp.derive_checks_from_scope === "boolean")
198
+ base.completion.derive_checks_from_scope = comp.derive_checks_from_scope;
199
+ if (Array.isArray(comp.checks))
200
+ base.completion.checks = comp.checks.filter((v) => typeof v === "string");
201
+ if (Array.isArray(comp.typescript_config_probe))
202
+ base.completion.typescript_config_probe = comp.typescript_config_probe.filter((v) => typeof v === "string");
203
+ if (Array.isArray(comp.eslint_config_probe))
204
+ base.completion.eslint_config_probe = comp.eslint_config_probe.filter((v) => typeof v === "string");
205
+ }
206
+ if (parsed.runtime_image && typeof parsed.runtime_image === "object" && !Array.isArray(parsed.runtime_image)) {
207
+ const runtimeImage = parsed.runtime_image;
208
+ if (runtimeImage.deps && typeof runtimeImage.deps === "object" && !Array.isArray(runtimeImage.deps)) {
209
+ const deps = runtimeImage.deps;
210
+ if (typeof deps.monorepo_install === "boolean") {
211
+ base.runtime_image.deps.monorepo_install = deps.monorepo_install;
212
+ }
213
+ if (typeof deps.hp_next_install === "boolean") {
214
+ base.runtime_image.deps.hp_next_install = deps.hp_next_install;
215
+ }
216
+ }
217
+ if (typeof runtimeImage.plugins_require_binaries === "boolean") {
218
+ base.runtime_image.plugins_require_binaries = runtimeImage.plugins_require_binaries;
219
+ }
220
+ }
221
+ if (parsed.runtime_snapshot && typeof parsed.runtime_snapshot === "object" && !Array.isArray(parsed.runtime_snapshot)) {
222
+ const runtimeSnapshot = parsed.runtime_snapshot;
223
+ if (typeof runtimeSnapshot.enabled === "boolean") {
224
+ base.runtime_snapshot.enabled = runtimeSnapshot.enabled;
225
+ }
226
+ }
227
+ return base;
228
+ }
229
+ function isValidMode(value) {
230
+ return value === "off" || value === "observe" || value === "enforce";
231
+ }
232
+ function isValidRule(value) {
233
+ if (!value || typeof value !== "object" || Array.isArray(value))
234
+ return false;
235
+ const r = value;
236
+ return typeof r.id === "string" && typeof r.category === "string" && r.match != null && typeof r.match === "object";
237
+ }
238
+ function migrateLegacyDeny(deny) {
239
+ const rules = [];
240
+ for (const entry of deny) {
241
+ if (typeof entry.id !== "string")
242
+ continue;
243
+ const match = {};
244
+ if (typeof entry.pattern === "string")
245
+ match.pattern = entry.pattern;
246
+ if (typeof entry.regex === "string")
247
+ match.regex = entry.regex;
248
+ if (!match.pattern && !match.regex)
249
+ continue;
250
+ rules.push({
251
+ id: entry.id,
252
+ category: "command",
253
+ match,
254
+ action: "block",
255
+ ...typeof entry.description === "string" ? { description: entry.description } : {}
256
+ });
257
+ }
258
+ return rules;
259
+ }
260
+ function precompilePolicyRuleRegexes(rules) {
261
+ return rules.map((rule) => {
262
+ const compiledRegex = rule.match.regex ? compileSafeRegex(rule.match.regex, `rules.${rule.id}.match.regex`, true) : undefined;
263
+ const compiledUnlessRegex = rule.unless?.regex ? compileSafeRegex(rule.unless.regex, `rules.${rule.id}.unless.regex`, true) : undefined;
264
+ return {
265
+ ...rule,
266
+ ...compiledRegex ? { compiledRegex } : {},
267
+ ...compiledUnlessRegex ? { compiledUnlessRegex } : {}
268
+ };
269
+ });
270
+ }
271
+ function getRegexUnsafeReason(pattern) {
272
+ if (pattern.length > 512) {
273
+ return "pattern exceeds max safe length (512 chars)";
274
+ }
275
+ if (/\\[1-9]/.test(pattern)) {
276
+ return "pattern uses backreferences";
277
+ }
278
+ if (/\((?:[^()\\]|\\.)*[+*](?:[^()\\]|\\.)*\)\s*[*+{]/.test(pattern)) {
279
+ return "pattern contains nested quantifiers";
280
+ }
281
+ if (/\((?:[^()\\]|\\.)*\.\\?[+*](?:[^()\\]|\\.)*\)\s*[*+{]/.test(pattern)) {
282
+ return "pattern contains nested broad quantifiers";
283
+ }
284
+ return null;
285
+ }
286
+ function compileSafeRegex(pattern, sourceLabel, logOnFailure) {
287
+ const cached = compiledRegexCache.get(pattern);
288
+ if (cached !== undefined) {
289
+ return cached ?? undefined;
290
+ }
291
+ const unsafeReason = getRegexUnsafeReason(pattern);
292
+ if (unsafeReason) {
293
+ if (logOnFailure) {
294
+ console.warn(`[policy] Skipping unsafe regex in ${sourceLabel}: ${unsafeReason}`);
295
+ }
296
+ compiledRegexCache.set(pattern, null);
297
+ return;
298
+ }
299
+ try {
300
+ const compiled = new RegExp(pattern);
301
+ compiledRegexCache.set(pattern, compiled);
302
+ return compiled;
303
+ } catch (error) {
304
+ if (logOnFailure) {
305
+ const message = error instanceof Error ? error.message : String(error);
306
+ console.warn(`[policy] Skipping invalid regex in ${sourceLabel}: ${message}`);
307
+ }
308
+ compiledRegexCache.set(pattern, null);
309
+ return;
310
+ }
311
+ }
312
+ function matchRule(rule, input) {
313
+ const { match } = rule;
314
+ if (match.pattern && input.includes(match.pattern)) {
315
+ return true;
316
+ }
317
+ if (match.regex) {
318
+ const compiled = rule.compiledRegex || compileSafeRegex(match.regex, `rules.${rule.id}.match.regex`, false);
319
+ if (!compiled) {
320
+ return false;
321
+ }
322
+ try {
323
+ return compiled.test(input);
324
+ } catch {
325
+ return false;
326
+ }
327
+ }
328
+ return false;
329
+ }
330
+ function matchRuleUnless(rule, command, taskId) {
331
+ if (!rule.unless)
332
+ return false;
333
+ if (rule.unless.regex) {
334
+ const compiled = rule.compiledUnlessRegex || compileSafeRegex(rule.unless.regex, `rules.${rule.id}.unless.regex`, false);
335
+ if (!compiled) {
336
+ return false;
337
+ }
338
+ try {
339
+ if (compiled.test(command))
340
+ return true;
341
+ } catch {}
342
+ }
343
+ if (rule.unless.task_in && taskId) {
344
+ if (rule.unless.task_in.includes(taskId))
345
+ return true;
346
+ }
347
+ return false;
348
+ }
349
+ function resolveAction(mode, matched) {
350
+ if (matched.length === 0)
351
+ return "allow";
352
+ if (mode === "off")
353
+ return "allow";
354
+ if (mode === "observe")
355
+ return "warn";
356
+ return "block";
357
+ }
358
+ function resolveAbsolutePath(projectRoot, rawPath) {
359
+ if (rawPath.startsWith("/"))
360
+ return resolve(rawPath);
361
+ return resolve(projectRoot, rawPath);
362
+ }
363
+ function isHarnessPath(projectRoot, rawPath) {
364
+ const absPath = resolveAbsolutePath(projectRoot, rawPath);
365
+ const managedRoots = [
366
+ resolve(projectRoot, "rig"),
367
+ resolve(projectRoot, ".rig"),
368
+ resolve(projectRoot, "artifacts")
369
+ ];
370
+ return managedRoots.some((root) => absPath === root || absPath.startsWith(root + "/"));
371
+ }
372
+ function isRuntimePath(projectRoot, rawPath, taskWorkspace) {
373
+ const absPath = resolveAbsolutePath(projectRoot, rawPath);
374
+ if (taskWorkspace) {
375
+ const workspaceRigRoot = resolve(taskWorkspace, ".rig");
376
+ const workspaceArtifactsRoot = resolve(taskWorkspace, "artifacts");
377
+ if (absPath === workspaceRigRoot || absPath.startsWith(workspaceRigRoot + "/") || absPath === workspaceArtifactsRoot || absPath.startsWith(workspaceArtifactsRoot + "/")) {
378
+ return true;
379
+ }
380
+ }
381
+ const runtimeRoot = resolve(projectRoot, ".rig/runtime/agents");
382
+ return absPath === runtimeRoot || absPath.startsWith(runtimeRoot + "/");
383
+ }
384
+ function isTestFile(path) {
385
+ return /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(path) || /\/(__tests__|tests|test)\//.test(path);
386
+ }
387
+ function evaluate(context) {
388
+ const policy = loadPolicy(context.projectRoot);
389
+ switch (context.evaluation.type) {
390
+ case "tool-call":
391
+ return evaluateToolCall(policy, context);
392
+ case "command":
393
+ return evaluateCommand(policy, context);
394
+ case "content-write":
395
+ return evaluateContent(policy, context);
396
+ case "file-access":
397
+ return evaluateScope(policy, context, context.evaluation.file_path, context.evaluation.access);
398
+ }
399
+ }
400
+ function evaluateScope(policy, context, filePath, access) {
401
+ const allowed = () => ({
402
+ allowed: true,
403
+ matchedRules: [],
404
+ action: "allow",
405
+ failClosed: false
406
+ });
407
+ if (policy.scope.harness_paths_exempt && isHarnessPath(context.projectRoot, filePath)) {
408
+ return allowed();
409
+ }
410
+ if (policy.scope.runtime_paths_exempt && isRuntimePath(context.projectRoot, filePath, context.taskWorkspace)) {
411
+ return allowed();
412
+ }
413
+ if (!context.taskId) {
414
+ if (access === "write" && policy.scope.fail_closed) {
415
+ return {
416
+ allowed: false,
417
+ matchedRules: [],
418
+ action: resolveAction(policy.mode, [{ id: "scope:no-task", category: "command", reason: "No active task; fail-closed for write operations" }]),
419
+ failClosed: true
420
+ };
421
+ }
422
+ return allowed();
423
+ }
424
+ const scopes = context.taskScopes || [];
425
+ if (scopes.length === 0) {
426
+ return allowed();
427
+ }
428
+ if (context.taskWorkspace && context.taskWorkspace !== context.projectRoot && filePath.startsWith("/")) {
429
+ const absPath = resolve(filePath);
430
+ if (!absPath.startsWith(context.taskWorkspace + "/") && !isHarnessPath(context.projectRoot, filePath)) {
431
+ const reason2 = `Absolute path '${filePath}' is outside task runtime boundary. Allowed root: ${context.taskWorkspace}`;
432
+ const matched2 = [{ id: "scope:workspace-boundary", category: "command", reason: reason2 }];
433
+ return {
434
+ allowed: policy.mode !== "enforce",
435
+ matchedRules: matched2,
436
+ action: resolveAction(policy.mode, matched2),
437
+ failClosed: false
438
+ };
439
+ }
440
+ }
441
+ const monorepoRoot = context.monorepoRoot || process.env.MONOREPO_ROOT?.trim() || context.taskWorkspace || context.projectRoot;
442
+ let normalizedPath = filePath;
443
+ if (context.taskWorkspace && context.taskWorkspace !== context.projectRoot && filePath.startsWith(context.taskWorkspace + "/")) {
444
+ normalizedPath = filePath.slice(context.taskWorkspace.length + 1);
445
+ }
446
+ normalizedPath = normalizePathToScope(context.projectRoot, monorepoRoot, normalizedPath);
447
+ if (scopeMatches(filePath, scopes) || scopeMatches(normalizedPath, scopes)) {
448
+ return allowed();
449
+ }
450
+ const reason = `File '${filePath}' (normalized: '${normalizedPath}') is outside scope of task ${context.taskId}`;
451
+ const matched = [{ id: "scope:out-of-scope", category: "command", reason }];
452
+ return {
453
+ allowed: policy.mode !== "enforce",
454
+ matchedRules: matched,
455
+ action: resolveAction(policy.mode, matched),
456
+ failClosed: false
457
+ };
458
+ }
459
+ function evaluateCommand(policy, context) {
460
+ const evaluation = context.evaluation;
461
+ if (evaluation.type !== "command") {
462
+ return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
463
+ }
464
+ const command = evaluation.command;
465
+ const matchedRules = [];
466
+ for (const rule of policy.rules) {
467
+ if (rule.category !== "command")
468
+ continue;
469
+ if (!matchRule(rule, command))
470
+ continue;
471
+ if (matchRuleUnless(rule, command, context.taskId))
472
+ continue;
473
+ matchedRules.push({
474
+ id: rule.id,
475
+ category: rule.category,
476
+ ...rule.description !== undefined ? { description: rule.description } : {},
477
+ reason: rule.description || `Matched rule ${rule.id}`
478
+ });
479
+ }
480
+ const writeTarget = extractWriteTarget(command);
481
+ if (writeTarget && !/^\/dev\//.test(writeTarget) && !/^\/proc\//.test(writeTarget)) {
482
+ const scopeResult = evaluateScope(policy, context, writeTarget, "write");
483
+ if (!scopeResult.allowed || scopeResult.matchedRules.length > 0) {
484
+ matchedRules.push(...scopeResult.matchedRules);
485
+ }
486
+ }
487
+ const action = resolveAction(policy.mode, matchedRules);
488
+ return {
489
+ allowed: action !== "block",
490
+ matchedRules,
491
+ action,
492
+ failClosed: false
493
+ };
494
+ }
495
+ function extractWriteTarget(command) {
496
+ const redirect = command.match(/>>?\s+([^\s;|&]+)/);
497
+ if (redirect?.[1])
498
+ return redirect[1];
499
+ const tee = command.match(/tee\s+(-a\s+)?([^\s;|&]+)/);
500
+ if (tee?.[2])
501
+ return tee[2];
502
+ return "";
503
+ }
504
+ function evaluateContent(policy, context) {
505
+ const evaluation = context.evaluation;
506
+ if (evaluation.type !== "content-write") {
507
+ return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
508
+ }
509
+ const { content, file_path } = evaluation;
510
+ const matchedRules = [];
511
+ const scopeResult = evaluateScope(policy, context, file_path, "write");
512
+ if (scopeResult.matchedRules.length > 0) {
513
+ matchedRules.push(...scopeResult.matchedRules);
514
+ }
515
+ for (const rule of policy.rules) {
516
+ if (rule.category !== "content" && rule.category !== "import" && rule.category !== "test-integrity")
517
+ continue;
518
+ if (rule.applies_to === "test-files" && !isTestFile(file_path))
519
+ continue;
520
+ if (!matchRule(rule, content))
521
+ continue;
522
+ if (matchRuleUnless(rule, content, context.taskId))
523
+ continue;
524
+ matchedRules.push({
525
+ id: rule.id,
526
+ category: rule.category,
527
+ ...rule.description !== undefined ? { description: rule.description } : {},
528
+ reason: rule.description || `Matched rule ${rule.id}`
529
+ });
530
+ }
531
+ const action = resolveAction(policy.mode, matchedRules);
532
+ return {
533
+ allowed: action !== "block",
534
+ matchedRules,
535
+ action,
536
+ failClosed: false
537
+ };
538
+ }
539
+ function evaluateToolCall(policy, context) {
540
+ const evaluation = context.evaluation;
541
+ if (evaluation.type !== "tool-call") {
542
+ return { allowed: true, matchedRules: [], action: "allow", failClosed: false };
543
+ }
544
+ const { tool_name, tool_input } = evaluation;
545
+ const allMatched = [];
546
+ const filePaths = extractFilePathsFromToolInput(tool_name, tool_input);
547
+ for (const fp of filePaths) {
548
+ const access = isWriteTool(tool_name) ? "write" : "read";
549
+ const scopeResult = evaluateScope(policy, context, fp, access);
550
+ if (scopeResult.matchedRules.length > 0) {
551
+ allMatched.push(...scopeResult.matchedRules);
552
+ }
553
+ }
554
+ const content = extractContentFromToolInput(tool_input);
555
+ if (content) {
556
+ const filePath = filePaths[0] || "";
557
+ const contentContext = {
558
+ ...context,
559
+ evaluation: { type: "content-write", file_path: filePath, content }
560
+ };
561
+ const contentPolicy = loadPolicy(context.projectRoot);
562
+ for (const rule of contentPolicy.rules) {
563
+ if (rule.category !== "content" && rule.category !== "import" && rule.category !== "test-integrity")
564
+ continue;
565
+ if (rule.applies_to === "test-files" && !isTestFile(filePath))
566
+ continue;
567
+ if (!matchRule(rule, content))
568
+ continue;
569
+ if (matchRuleUnless(rule, content, context.taskId))
570
+ continue;
571
+ allMatched.push({
572
+ id: rule.id,
573
+ category: rule.category,
574
+ ...rule.description !== undefined ? { description: rule.description } : {},
575
+ reason: rule.description || `Matched rule ${rule.id}`
576
+ });
577
+ }
578
+ }
579
+ if (tool_name === "Bash") {
580
+ const command = String(tool_input.command || tool_input.cmd || "");
581
+ if (command) {
582
+ const cmdContext = {
583
+ ...context,
584
+ evaluation: { type: "command", command }
585
+ };
586
+ const cmdResult = evaluateCommand(policy, cmdContext);
587
+ if (cmdResult.matchedRules.length > 0) {
588
+ allMatched.push(...cmdResult.matchedRules);
589
+ }
590
+ }
591
+ }
592
+ const seen = new Set;
593
+ const deduplicated = [];
594
+ for (const rule of allMatched) {
595
+ if (!seen.has(rule.id)) {
596
+ seen.add(rule.id);
597
+ deduplicated.push(rule);
598
+ }
599
+ }
600
+ const action = resolveAction(policy.mode, deduplicated);
601
+ return {
602
+ allowed: action !== "block",
603
+ matchedRules: deduplicated,
604
+ action,
605
+ failClosed: false
606
+ };
607
+ }
608
+ function isWriteTool(toolName) {
609
+ return toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit";
610
+ }
611
+ function extractFilePathsFromToolInput(toolName, input) {
612
+ const paths = [];
613
+ const add = (value) => {
614
+ if (typeof value === "string" && value.trim()) {
615
+ paths.push(value.trim());
616
+ }
617
+ };
618
+ if (toolName === "Read" || toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit") {
619
+ add(input.file_path);
620
+ add(input.path);
621
+ } else if (toolName === "Glob") {
622
+ add(input.path);
623
+ } else if (toolName === "Grep") {
624
+ add(input.path);
625
+ } else {
626
+ add(input.file_path);
627
+ add(input.path);
628
+ }
629
+ return paths;
630
+ }
631
+ function extractContentFromToolInput(input) {
632
+ if (typeof input.content === "string")
633
+ return input.content;
634
+ if (typeof input.new_string === "string")
635
+ return input.new_string;
636
+ return "";
637
+ }
638
+ var guardHotPathPrimed = false;
639
+ function primeGuardHotPaths() {
640
+ if (guardHotPathPrimed) {
641
+ return;
642
+ }
643
+ guardHotPathPrimed = true;
644
+ try {
645
+ optimizeNextInvocation(matchRule);
646
+ optimizeNextInvocation(evaluate);
647
+ } catch {}
648
+ }
649
+ primeGuardHotPaths();
650
+
651
+ // packages/cli-surface-plugin/src/control-plane/agent-binary-build.ts
652
+ import { runtimeProvisioningEnv } from "@rig/core/runtime-provisioning-env";
653
+
654
+ // packages/cli-surface-plugin/src/runner.ts
12
655
  class CliError extends RuntimeCliError {
13
- hint;
14
656
  constructor(message, exitCode = 1, options = {}) {
15
- super(message, exitCode);
16
- if (options.hint?.trim()) {
17
- this.hint = options.hint.trim();
18
- }
657
+ super(message, exitCode, options);
19
658
  }
20
659
  }
21
660
  function formatCommand(parts) {
@@ -23,26 +662,26 @@ function formatCommand(parts) {
23
662
  }
24
663
 
25
664
  // packages/cli-surface-plugin/src/commands/_paths.ts
26
- import { resolve } from "path";
27
- import { resolveMonorepoRoot } from "@rig/runtime/layout";
665
+ import { resolve as resolve2 } from "path";
666
+ import { resolveMonorepoRoot } from "@rig/core/layout";
28
667
  function resolveControlPlaneHostStateRoot(projectRoot) {
29
- return resolve(projectRoot, ".rig");
668
+ return resolve2(projectRoot, ".rig");
30
669
  }
31
670
  function resolveControlPlaneHostLogsDir(projectRoot) {
32
- return resolve(resolveControlPlaneHostStateRoot(projectRoot), "logs");
671
+ return resolve2(resolveControlPlaneHostStateRoot(projectRoot), "logs");
33
672
  }
34
673
  function resolveControlPlaneDefinitionRoot(projectRoot) {
35
- return resolve(projectRoot, "rig");
674
+ return resolve2(projectRoot, "rig");
36
675
  }
37
676
 
38
677
  // packages/cli-surface-plugin/src/commands/_policy.ts
39
678
  function loadPolicyFile(projectRoot) {
40
- const policyPath = resolve2(resolveControlPlaneDefinitionRoot(projectRoot), "policy", "policy.json");
41
- if (!existsSync(policyPath)) {
679
+ const policyPath = resolve3(resolveControlPlaneDefinitionRoot(projectRoot), "policy", "policy.json");
680
+ if (!existsSync2(policyPath)) {
42
681
  return { mode: "observe", rules: [] };
43
682
  }
44
683
  try {
45
- const parsed = JSON.parse(readFileSync(policyPath, "utf8"));
684
+ const parsed = JSON.parse(readFileSync2(policyPath, "utf8"));
46
685
  const mode = parsed.mode === "off" || parsed.mode === "observe" || parsed.mode === "enforce" ? parsed.mode : "observe";
47
686
  const rules = Array.isArray(parsed.rules) ? parsed.rules.filter((value) => Boolean(value && typeof value === "object" && !Array.isArray(value))).map((value, index) => {
48
687
  const rule = {
@@ -90,7 +729,7 @@ function appendControlledBashAudit(context, mode, command, args, matchedRuleIds,
90
729
  return;
91
730
  }
92
731
  const logsDir = resolveControlPlaneHostLogsDir(context.projectRoot);
93
- const logFile = resolve2(logsDir, "controlled-bash.jsonl");
732
+ const logFile = resolve3(logsDir, "controlled-bash.jsonl");
94
733
  mkdirSync(logsDir, { recursive: true });
95
734
  const payload = {
96
735
  timestamp: new Date().toISOString(),