rulecloud 0.0.0-stage → 0.1.1
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 +44 -2
- package/dist/api.d.ts +30 -0
- package/dist/api.js +74 -0
- package/dist/autofix.d.ts +14 -0
- package/dist/autofix.js +159 -0
- package/dist/commands/check.d.ts +1 -0
- package/dist/commands/check.js +65 -0
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.js +52 -0
- package/dist/commands/watch.d.ts +6 -0
- package/dist/commands/watch.js +257 -0
- package/dist/config.d.ts +6 -0
- package/dist/config.js +67 -0
- package/dist/eval.d.ts +17 -0
- package/dist/eval.js +159 -0
- package/dist/exclusions.d.ts +4 -0
- package/dist/exclusions.js +50 -0
- package/dist/glob.d.ts +5 -0
- package/dist/glob.js +63 -0
- package/dist/hud.d.ts +14 -0
- package/dist/hud.js +63 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +60 -0
- package/dist/rules-config.d.ts +10 -0
- package/dist/rules-config.js +33 -0
- package/dist/severity.d.ts +13 -0
- package/dist/severity.js +48 -0
- package/dist/walk.d.ts +1 -0
- package/dist/walk.js +52 -0
- package/package.json +43 -3
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rulecloud CLI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Real-time guardrails for AI coding agents. Pulls **Cloud Rulespacks** from your RuleCloud workspace, watches file saves, and reports violations in the terminal (optionally syncing to the Live Agent Monitor).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g rulecloud
|
|
9
|
+
# or run without installing:
|
|
10
|
+
npx rulecloud init
|
|
11
|
+
npx rulecloud watch
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Develop from the monorepo `cli/` folder:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install
|
|
18
|
+
npm run build
|
|
19
|
+
npm link
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Configure
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
export RULECLOUD_API_URL=https://rulecloud-production.up.railway.app
|
|
26
|
+
export RULECLOUD_API_KEY=rc_live_...
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Create API keys in the [RuleCloud console](https://rulecloud.vercel.app/dashboard/keys).
|
|
30
|
+
|
|
31
|
+
## Commands
|
|
32
|
+
|
|
33
|
+
| Command | Description |
|
|
34
|
+
|---------|-------------|
|
|
35
|
+
| `rulecloud init` | Download merged `.guardrail.yml`, wire `.cursor/mcp.json` + agent rule |
|
|
36
|
+
| `rulecloud watch` | Live HUD: initial scan + file saves (`r` rescan, `c` clear, `q` quit) |
|
|
37
|
+
| `rulecloud watch --plain` | No colors / no TTY shortcuts (CI) |
|
|
38
|
+
| `rulecloud watch` | Cloud Sync **ON** automatically when `RULECLOUD_API_KEY` or `.rulecloud.json` is set |
|
|
39
|
+
| `rulecloud fix [file]` | Pro AI Auto-Fix via API (`f` in watch) |
|
|
40
|
+
| `rulecloud check` | Scan entire repo; exit `1` on high/critical violations |
|
|
41
|
+
| `rulecloud check --staged` | Pre-commit scan of staged git files only |
|
|
42
|
+
|
|
43
|
+
## Ignored paths
|
|
44
|
+
|
|
45
|
+
`node_modules`, `.git`, `.next`, `dist`, `venv`, `.venv`, `build`, `target`, `__pycache__`
|
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CliConfig } from "./config.js";
|
|
2
|
+
export type BundleResult = {
|
|
3
|
+
rules_yaml: string;
|
|
4
|
+
etag: string;
|
|
5
|
+
notModified?: boolean;
|
|
6
|
+
source: "rulespacks" | "defaults";
|
|
7
|
+
};
|
|
8
|
+
export declare function fetchBundle(cfg: CliConfig, etag?: string): Promise<BundleResult>;
|
|
9
|
+
export type AutofixResult = {
|
|
10
|
+
explanation?: string;
|
|
11
|
+
patched_code?: string;
|
|
12
|
+
suggestion_lines?: string;
|
|
13
|
+
diff?: string;
|
|
14
|
+
error?: string;
|
|
15
|
+
detail?: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function postAutofix(cfg: CliConfig, payload: {
|
|
18
|
+
code: string;
|
|
19
|
+
violation: Record<string, unknown>;
|
|
20
|
+
rules_yaml: string;
|
|
21
|
+
}): Promise<{
|
|
22
|
+
status: number;
|
|
23
|
+
body: AutofixResult;
|
|
24
|
+
}>;
|
|
25
|
+
export declare function postWatchEvent(cfg: CliConfig, payload: {
|
|
26
|
+
file: string;
|
|
27
|
+
findings: Record<string, unknown>[];
|
|
28
|
+
duration_ms: number;
|
|
29
|
+
repo_label?: string;
|
|
30
|
+
}): Promise<void>;
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
async function fetchDefaults(cfg) {
|
|
3
|
+
const res = await fetch(`${cfg.apiUrl}/api/defaults`);
|
|
4
|
+
if (!res.ok) {
|
|
5
|
+
const text = await res.text();
|
|
6
|
+
throw new Error(`/api/defaults failed (${res.status}): ${text}`);
|
|
7
|
+
}
|
|
8
|
+
const data = (await res.json());
|
|
9
|
+
const yaml = data.rules_yaml || "";
|
|
10
|
+
const etag = createHash("sha256").update(yaml).digest("hex").slice(0, 16);
|
|
11
|
+
return { rules_yaml: yaml, etag, source: "defaults" };
|
|
12
|
+
}
|
|
13
|
+
export async function fetchBundle(cfg, etag) {
|
|
14
|
+
const headers = {
|
|
15
|
+
"X-API-Key": cfg.apiKey,
|
|
16
|
+
};
|
|
17
|
+
if (etag)
|
|
18
|
+
headers["If-None-Match"] = `"${etag}"`;
|
|
19
|
+
const bundleUrl = `${cfg.apiUrl}/api/rulespacks/bundle`;
|
|
20
|
+
const res = await fetch(bundleUrl, { headers });
|
|
21
|
+
if (res.status === 304) {
|
|
22
|
+
return {
|
|
23
|
+
rules_yaml: "",
|
|
24
|
+
etag: etag || "",
|
|
25
|
+
notModified: true,
|
|
26
|
+
source: "rulespacks",
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (res.status === 404) {
|
|
30
|
+
console.warn(`Cloud Rulespacks API is not on ${cfg.apiUrl} yet (404).\n` +
|
|
31
|
+
" → Deploy the latest RuleCloud API and run supabase/migrations/010_rulespacks.sql\n" +
|
|
32
|
+
" → Or point at a local server: export RULECLOUD_API_URL=http://127.0.0.1:8000\n" +
|
|
33
|
+
"Using /api/defaults for now.");
|
|
34
|
+
return fetchDefaults(cfg);
|
|
35
|
+
}
|
|
36
|
+
if (!res.ok) {
|
|
37
|
+
const text = await res.text();
|
|
38
|
+
throw new Error(`Bundle fetch failed (${res.status}) from ${bundleUrl}: ${text}`);
|
|
39
|
+
}
|
|
40
|
+
const data = (await res.json());
|
|
41
|
+
return { ...data, source: "rulespacks" };
|
|
42
|
+
}
|
|
43
|
+
export async function postAutofix(cfg, payload) {
|
|
44
|
+
const res = await fetch(`${cfg.apiUrl}/api/ai/autofix`, {
|
|
45
|
+
method: "POST",
|
|
46
|
+
headers: {
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
"X-API-Key": cfg.apiKey,
|
|
49
|
+
},
|
|
50
|
+
body: JSON.stringify(payload),
|
|
51
|
+
});
|
|
52
|
+
let body = {};
|
|
53
|
+
try {
|
|
54
|
+
body = (await res.json());
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
body = { detail: await res.text() };
|
|
58
|
+
}
|
|
59
|
+
return { status: res.status, body };
|
|
60
|
+
}
|
|
61
|
+
export async function postWatchEvent(cfg, payload) {
|
|
62
|
+
const res = await fetch(`${cfg.apiUrl}/api/watch/events`, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
"X-API-Key": cfg.apiKey,
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify(payload),
|
|
69
|
+
});
|
|
70
|
+
if (!res.ok && res.status !== 202) {
|
|
71
|
+
const text = await res.text();
|
|
72
|
+
throw new Error(`Watch event failed (${res.status}): ${text}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CliConfig } from "./config.js";
|
|
2
|
+
import type { Finding } from "./eval.js";
|
|
3
|
+
export declare function runAutofixForFile(opts: {
|
|
4
|
+
cwd: string;
|
|
5
|
+
cfg: CliConfig;
|
|
6
|
+
relPath: string;
|
|
7
|
+
absPath: string;
|
|
8
|
+
code: string;
|
|
9
|
+
findings: Finding[];
|
|
10
|
+
rulesYaml: string;
|
|
11
|
+
plain: boolean;
|
|
12
|
+
interactive: boolean;
|
|
13
|
+
}): Promise<boolean>;
|
|
14
|
+
export declare function runFixCommand(cwd: string, cfg: CliConfig, relFile: string | undefined, plain: boolean): Promise<void>;
|
package/dist/autofix.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import readline from "node:readline";
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
import { postAutofix } from "./api.js";
|
|
6
|
+
import { requireApiKey } from "./config.js";
|
|
7
|
+
import { severityTier } from "./severity.js";
|
|
8
|
+
function printColoredDiff(diff, plain) {
|
|
9
|
+
for (const line of diff.split(/\r?\n/)) {
|
|
10
|
+
if (!line)
|
|
11
|
+
continue;
|
|
12
|
+
if (line.startsWith("+++") || line.startsWith("---")) {
|
|
13
|
+
console.log(plain ? line : pc.cyan(line));
|
|
14
|
+
}
|
|
15
|
+
else if (line.startsWith("+")) {
|
|
16
|
+
console.log(plain ? line : pc.green(line));
|
|
17
|
+
}
|
|
18
|
+
else if (line.startsWith("-")) {
|
|
19
|
+
console.log(plain ? line : pc.red(line));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
console.log(plain ? line : pc.dim(line));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async function confirm(prompt) {
|
|
27
|
+
const rl = readline.createInterface({
|
|
28
|
+
input: process.stdin,
|
|
29
|
+
output: process.stdout,
|
|
30
|
+
});
|
|
31
|
+
return new Promise((resolve) => {
|
|
32
|
+
rl.question(prompt, (answer) => {
|
|
33
|
+
rl.close();
|
|
34
|
+
resolve(/^y(es)?$/i.test(answer.trim()));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export async function runAutofixForFile(opts) {
|
|
39
|
+
try {
|
|
40
|
+
requireApiKey(opts.cfg);
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
const message = err instanceof Error ? err.message : "API key required";
|
|
44
|
+
console.log(opts.plain
|
|
45
|
+
? `✖ Auto-fix unavailable: ${message}`
|
|
46
|
+
: pc.red(`✖ Auto-fix unavailable: ${message}`));
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const targets = opts.findings
|
|
50
|
+
.filter((f) => f.file === opts.relPath)
|
|
51
|
+
.sort((a, b) => {
|
|
52
|
+
const rank = (f) => severityTier(f.severity) === "error" ? 0 : severityTier(f.severity) === "warn" ? 1 : 2;
|
|
53
|
+
return rank(a) - rank(b) || a.line - b.line;
|
|
54
|
+
});
|
|
55
|
+
if (targets.length === 0) {
|
|
56
|
+
console.log(pc.yellow(`No violations in ${opts.relPath}`));
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
const violation = targets[0];
|
|
60
|
+
let status;
|
|
61
|
+
let body;
|
|
62
|
+
try {
|
|
63
|
+
({ status, body } = await postAutofix(opts.cfg, {
|
|
64
|
+
code: opts.code,
|
|
65
|
+
violation: violation,
|
|
66
|
+
rules_yaml: opts.rulesYaml,
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
const message = err instanceof Error ? err.message : "Network error calling Auto-Fix API";
|
|
71
|
+
console.log(opts.plain
|
|
72
|
+
? `✖ Auto-fix unavailable: ${message}`
|
|
73
|
+
: pc.red(`✖ Auto-fix unavailable: ${message}`));
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (status === 402) {
|
|
77
|
+
const msg = body.detail || "AI Auto-Fix requires RuleCloud Pro";
|
|
78
|
+
console.log(opts.plain
|
|
79
|
+
? `✖ Auto-fix unavailable: ${msg}`
|
|
80
|
+
: pc.red(`✖ Auto-fix unavailable: ${msg}`));
|
|
81
|
+
console.log(opts.plain
|
|
82
|
+
? "Upgrade: https://rulecloud.vercel.app/dashboard/billing"
|
|
83
|
+
: pc.cyan("Upgrade: https://rulecloud.vercel.app/dashboard/billing"));
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (status === 503) {
|
|
87
|
+
const msg = body.detail ||
|
|
88
|
+
body.error ||
|
|
89
|
+
"AI Copilot is not configured on the server";
|
|
90
|
+
console.log(opts.plain
|
|
91
|
+
? `✖ Auto-fix unavailable: ${msg}`
|
|
92
|
+
: pc.red(`✖ Auto-fix unavailable: ${msg}`));
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (status >= 400) {
|
|
96
|
+
const msg = body.detail || body.error || `Auto-fix failed (${status})`;
|
|
97
|
+
console.log(opts.plain
|
|
98
|
+
? `✖ Auto-fix unavailable: ${msg}`
|
|
99
|
+
: pc.red(`✖ Auto-fix unavailable: ${msg}`));
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (body.explanation) {
|
|
103
|
+
console.log(opts.plain ? body.explanation : pc.bold(body.explanation));
|
|
104
|
+
}
|
|
105
|
+
if (body.diff) {
|
|
106
|
+
printColoredDiff(body.diff, opts.plain);
|
|
107
|
+
}
|
|
108
|
+
const patched = (body.patched_code || "").trim();
|
|
109
|
+
if (!patched) {
|
|
110
|
+
console.log(opts.plain ? "No patched_code returned" : pc.yellow("No patched_code returned"));
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (!opts.interactive)
|
|
114
|
+
return false;
|
|
115
|
+
const ok = await confirm(`Apply fix to ${opts.relPath}? [y/N] `);
|
|
116
|
+
if (!ok)
|
|
117
|
+
return false;
|
|
118
|
+
writeFileSync(opts.absPath, patched, "utf8");
|
|
119
|
+
console.log(opts.plain ? "Applied." : pc.green("Applied."));
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
export async function runFixCommand(cwd, cfg, relFile, plain) {
|
|
123
|
+
const { loadRulesFromRepo } = await import("./rules-config.js");
|
|
124
|
+
const { evaluateFile } = await import("./eval.js");
|
|
125
|
+
const { readFileSync } = await import("node:fs");
|
|
126
|
+
const loaded = loadRulesFromRepo(cwd);
|
|
127
|
+
let targetRel = relFile;
|
|
128
|
+
if (!targetRel) {
|
|
129
|
+
const { walkSourceFiles } = await import("./walk.js");
|
|
130
|
+
const { evaluateFile } = await import("./eval.js");
|
|
131
|
+
const { severityTier } = await import("./severity.js");
|
|
132
|
+
for (const abs of walkSourceFiles(cwd)) {
|
|
133
|
+
const rel = path.relative(cwd, abs).replace(/\\/g, "/");
|
|
134
|
+
const findings = evaluateFile(rel, readFileSync(abs, "utf8"), loaded.doc);
|
|
135
|
+
if (findings.some((f) => severityTier(f.severity) === "error")) {
|
|
136
|
+
targetRel = rel;
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!targetRel) {
|
|
142
|
+
console.log(plain ? "No file with errors found" : pc.yellow("No file with errors found"));
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const absPath = path.join(cwd, targetRel);
|
|
146
|
+
const code = readFileSync(absPath, "utf8");
|
|
147
|
+
const findings = evaluateFile(targetRel, code, loaded.doc);
|
|
148
|
+
await runAutofixForFile({
|
|
149
|
+
cwd,
|
|
150
|
+
cfg,
|
|
151
|
+
relPath: targetRel.replace(/\\/g, "/"),
|
|
152
|
+
absPath,
|
|
153
|
+
code,
|
|
154
|
+
findings,
|
|
155
|
+
rulesYaml: loaded.rulesText,
|
|
156
|
+
plain,
|
|
157
|
+
interactive: true,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runCheck(cwd: string, staged: boolean, plain?: boolean): number;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { spawnSync } from "node:child_process";
|
|
4
|
+
import pc from "picocolors";
|
|
5
|
+
import { evaluateFile } from "../eval.js";
|
|
6
|
+
import { hasBlocking } from "../severity.js";
|
|
7
|
+
import { printFinding, printRulesWarning } from "../hud.js";
|
|
8
|
+
import { loadRulesFromRepo } from "../rules-config.js";
|
|
9
|
+
import { shouldIgnorePath } from "../eval.js";
|
|
10
|
+
import { walkSourceFiles } from "../walk.js";
|
|
11
|
+
function stagedFiles(cwd) {
|
|
12
|
+
const r = spawnSync("git", ["diff", "--cached", "--name-only", "--diff-filter=ACMR"], {
|
|
13
|
+
cwd,
|
|
14
|
+
encoding: "utf8",
|
|
15
|
+
});
|
|
16
|
+
if (r.status !== 0)
|
|
17
|
+
return null;
|
|
18
|
+
return r.stdout
|
|
19
|
+
.split(/\r?\n/)
|
|
20
|
+
.map((l) => l.trim())
|
|
21
|
+
.filter(Boolean)
|
|
22
|
+
.map((rel) => path.join(cwd, rel));
|
|
23
|
+
}
|
|
24
|
+
export function runCheck(cwd, staged, plain = false) {
|
|
25
|
+
const loaded = loadRulesFromRepo(cwd);
|
|
26
|
+
console.log(plain
|
|
27
|
+
? `Loaded ${loaded.configLabel} — ${loaded.ruleCount} rules`
|
|
28
|
+
: pc.cyan(`Loaded ${loaded.configLabel} — ${loaded.ruleCount} rules`));
|
|
29
|
+
if (loaded.warnReason)
|
|
30
|
+
printRulesWarning(loaded.warnReason, plain);
|
|
31
|
+
const doc = loaded.doc;
|
|
32
|
+
const files = staged ? stagedFiles(cwd) : walkSourceFiles(cwd);
|
|
33
|
+
if (staged && files === null) {
|
|
34
|
+
console.error(pc.red("git not available — scanning full tree"));
|
|
35
|
+
return runCheck(cwd, false, plain);
|
|
36
|
+
}
|
|
37
|
+
const targets = files ?? walkSourceFiles(cwd);
|
|
38
|
+
const all = [];
|
|
39
|
+
for (const abs of targets) {
|
|
40
|
+
if (shouldIgnorePath(abs, cwd))
|
|
41
|
+
continue;
|
|
42
|
+
const rel = path.relative(cwd, abs).replace(/\\/g, "/");
|
|
43
|
+
let content;
|
|
44
|
+
try {
|
|
45
|
+
content = readFileSync(abs, "utf8");
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
all.push(...evaluateFile(rel, content, doc));
|
|
51
|
+
}
|
|
52
|
+
const unique = all.filter((f, i, arr) => {
|
|
53
|
+
const k = `${f.id}:${f.file}:${f.line}`;
|
|
54
|
+
return arr.findIndex((x) => `${x.id}:${x.file}:${x.line}` === k) === i;
|
|
55
|
+
});
|
|
56
|
+
if (unique.length === 0) {
|
|
57
|
+
console.log(plain ? "PASS (0)" : pc.green("✓ PASS (0)"));
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
for (const f of unique) {
|
|
61
|
+
printFinding(f, plain);
|
|
62
|
+
}
|
|
63
|
+
console.log(plain ? `\n${unique.length} finding(s)` : pc.dim(`\n${unique.length} finding(s)`));
|
|
64
|
+
return hasBlocking(unique) ? 1 : 0;
|
|
65
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fetchBundle } from "../api.js";
|
|
4
|
+
import { requireApiKey } from "../config.js";
|
|
5
|
+
const CURSOR_RULE = `---
|
|
6
|
+
description: Run RuleCloud guardrail checks before finishing edits
|
|
7
|
+
globs:
|
|
8
|
+
- "**/*"
|
|
9
|
+
alwaysApply: true
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Before marking a coding task complete:
|
|
13
|
+
|
|
14
|
+
1. Run \`rulecloud check\` on changed files (or use the RuleCloud MCP \`scan_code\` tool).
|
|
15
|
+
2. Fix any \`[BLOCKED]\` violations before proceeding.
|
|
16
|
+
3. Keep \`.guardrail.yml\` synced via \`rulecloud init\` when Cloud Rulespacks change in the console.
|
|
17
|
+
`;
|
|
18
|
+
export async function runInit(cwd, cfg) {
|
|
19
|
+
requireApiKey(cfg);
|
|
20
|
+
console.log(`API: ${cfg.apiUrl}`);
|
|
21
|
+
console.log("Fetching Cloud Rulespacks bundle…");
|
|
22
|
+
const bundle = await fetchBundle(cfg);
|
|
23
|
+
if (bundle.source === "defaults") {
|
|
24
|
+
console.log("(Starter defaults — redeploy API for full Cloud Rulespacks merge)");
|
|
25
|
+
}
|
|
26
|
+
const guardrailPath = path.join(cwd, ".guardrail.yml");
|
|
27
|
+
writeFileSync(guardrailPath, bundle.rules_yaml, "utf8");
|
|
28
|
+
console.log(`Wrote ${guardrailPath} (etag: ${bundle.etag})`);
|
|
29
|
+
const rcPath = path.join(cwd, ".rulecloud.json");
|
|
30
|
+
writeFileSync(rcPath, JSON.stringify({
|
|
31
|
+
apiUrl: cfg.apiUrl,
|
|
32
|
+
apiKey: cfg.apiKey,
|
|
33
|
+
}, null, 2) + "\n", "utf8");
|
|
34
|
+
console.log(`Wrote ${rcPath} (Cloud Sync enabled for watch)`);
|
|
35
|
+
const cursorDir = path.join(cwd, ".cursor");
|
|
36
|
+
mkdirSync(cursorDir, { recursive: true });
|
|
37
|
+
mkdirSync(path.join(cursorDir, "rules"), { recursive: true });
|
|
38
|
+
const mcpJson = {
|
|
39
|
+
mcpServers: {
|
|
40
|
+
rulecloud: {
|
|
41
|
+
url: `${cfg.apiUrl}/mcp/`,
|
|
42
|
+
headers: {
|
|
43
|
+
"X-API-Key": "${env:RULECLOUD_API_KEY}",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
writeFileSync(path.join(cursorDir, "mcp.json"), JSON.stringify(mcpJson, null, 2) + "\n", "utf8");
|
|
49
|
+
writeFileSync(path.join(cursorDir, "rules", "rulecloud.mdc"), CURSOR_RULE, "utf8");
|
|
50
|
+
console.log("Configured .cursor/mcp.json and .cursor/rules/rulecloud.mdc");
|
|
51
|
+
console.log("\nNext: rulecloud watch # real-time agent guardrails");
|
|
52
|
+
}
|