-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
288 lines (234 loc) · 10.1 KB
/
cli.py
File metadata and controls
288 lines (234 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
sentinel review [repo] [--pr URL] [--rules path] [--exit-on-blocker]
sentinel init [repo] [--domain fintech|healthcare]
sentinel hook [repo]
"""
import argparse
import os
import re
import shutil
import stat
import sys
import yaml
from pathlib import Path
from rich.console import Console
from rich.panel import Panel
from engine.agent import run_agent
console = Console()
SEVERITY_COLOR = {"BLOCKER": "red", "RISKY": "yellow", "NIT": "blue"}
_SENTINEL_ROOT = Path(__file__).resolve().parent
AVAILABLE_DOMAINS = ["fintech", "healthcare"]
STARTER_RULES = """\
extends: {domain}
domain: {domain}-custom
# Add your project-specific rules below.
# Each rule uses plain English — the LLM interprets it against your diff.
#
# rules:
# - id: my-custom-rule
# severity: BLOCKER # BLOCKER | RISKY | NIT
# when: |
# Describe when this rule fires in plain English.
# unless: |
# Describe exceptions (optional).
# why: |
# Why this matters. Paste a real past incident if you have one.
"""
# ── Helpers ──────────────────────────────────────────────────────────────────
def _rules_dir() -> Path:
"""Locate bundled rules whether running from source or installed via pip."""
try:
import importlib.resources as pkg_resources
ref = pkg_resources.files("rules")
path = Path(str(ref))
if path.is_dir():
return path
except Exception:
pass
return _SENTINEL_ROOT / "rules"
def _rule_prompt_block(r: dict) -> str:
lines = [
f"[{r['severity']}] {r['id']}: {r['when'].strip()}",
f" Why: {r['why'].strip()}",
]
unless = r.get("unless")
if unless:
lines.append(f" Unless: {unless.strip()}")
return "\n".join(lines)
def load_rules(path: str) -> tuple[str, str]:
child_path = Path(path)
child = yaml.safe_load(child_path.read_text())
extends_name = child.get("extends")
if extends_name:
base_file = _rules_dir() / f"{extends_name}.yaml"
if not base_file.is_file():
raise FileNotFoundError(
f"Base rule pack not found: {base_file}\n"
f"Available packs: {', '.join(AVAILABLE_DOMAINS)}"
)
base = yaml.safe_load(base_file.read_text())
domain = child.get("domain", base.get("domain", "general"))
merged_rules = list(base.get("rules") or []) + list(child.get("rules") or [])
else:
domain = child.get("domain", "general")
merged_rules = child.get("rules") or []
rules_text = "\n".join(_rule_prompt_block(r) for r in merged_rules)
return domain, rules_text
def print_review(findings):
if not findings:
console.print(Panel("[green]No issues found.[/green]", title="Sentinel"))
return
blockers = sum(1 for f in findings if f["severity"] == "BLOCKER")
for f in findings:
color = SEVERITY_COLOR.get(f["severity"], "white")
loc = f"{f['file']}" + (f":{f['line']}" if f.get("line") else "")
console.print(
Panel(
f"[bold]{f['message']}[/bold]\n[dim]{loc}[/dim]",
title=f"[{color}]{f['severity']}[/{color}]",
border_style=color,
)
)
console.rule(
f"[red]BLOCKERS: {blockers}[/red] — commit rejected"
if blockers else "[green]Safe to ship[/green]"
)
# ── Subcommands ──────────────────────────────────────────────────────────────
def cmd_init(args):
repo = Path(args.repo).resolve()
if not repo.is_dir():
console.print(f"[red]Not a directory: {repo}[/red]")
sys.exit(1)
sentinel_dir = repo / ".sentinel"
rules_file = sentinel_dir / "rules.yaml"
if rules_file.exists() and not args.force:
console.print(f"[yellow]Already exists:[/yellow] {rules_file}")
console.print("Use --force to overwrite.")
sys.exit(0)
sentinel_dir.mkdir(exist_ok=True)
rules_file.write_text(STARTER_RULES.format(domain=args.domain))
console.print(f"[green]✓[/green] Created [bold]{rules_file}[/bold]")
console.print(f"\nExtends the [bold]{args.domain}[/bold] rule pack.")
console.print("\nNext steps:")
console.print(f" 1. Edit [dim]{rules_file}[/dim] — add project-specific rules")
console.print(f" 2. Review: sentinel review {repo} --rules {rules_file}")
console.print(f" 3. Hook: sentinel hook {repo} (auto-runs on every commit)")
def cmd_hook(args):
repo = Path(args.repo).resolve()
if not (repo / ".git").is_dir():
console.print(f"[red]Not a git repository: {repo}[/red]")
sys.exit(1)
rules_file = repo / ".sentinel" / "rules.yaml"
if not rules_file.exists():
console.print(
f"[yellow]No rules file at {rules_file}[/yellow]\n"
f"Run: sentinel init {repo}"
)
sys.exit(1)
sentinel_dir = str(_SENTINEL_ROOT)
husky_dir = repo / ".husky"
hook_file = (husky_dir / "pre-commit") if husky_dir.is_dir() else (repo / ".git" / "hooks" / "pre-commit")
hook_file.parent.mkdir(parents=True, exist_ok=True)
hook_content = f"""#!/usr/bin/env bash
# Sentinel pre-commit hook — installed by: sentinel hook
set -e
SENTINEL_DIR="{sentinel_dir}"
RULES="{rules_file}"
if [ ! -f "$RULES" ]; then
echo "Sentinel: no rules file at $RULES — skipping."
exit 0
fi
source "$SENTINEL_DIR/venv/bin/activate" 2>/dev/null || true
cd "$SENTINEL_DIR"
python cli.py "{repo}" --rules "$RULES" --exit-on-blocker
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo ""
echo "Sentinel: BLOCKER findings — commit rejected."
echo "Fix the issues above, or run: git commit --no-verify to bypass."
exit 1
fi
exit 0
"""
hook_file.write_text(hook_content)
hook_file.chmod(hook_file.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
alt = repo / ".git" / "hooks" / "pre-commit"
if hook_file != alt:
alt.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(hook_file, alt)
alt.chmod(alt.stat().st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)
console.print(f"[green]✓[/green] Hook installed at [bold]{hook_file}[/bold]")
console.print("\nEvery [bold]git commit[/bold] now runs Sentinel automatically.")
console.print("BLOCKER findings will reject the commit.")
console.print("Bypass with [dim]git commit --no-verify[/dim]")
def cmd_review(args):
domain, rules_text = load_rules(args.rules)
if args.pr:
m = re.match(r"https://github\.com/([^/]+)/([^/]+)/pull/(\d+)", args.pr.strip())
if not m:
console.print("[red]Invalid PR URL (expected github.com/owner/repo/pull/N)[/red]")
sys.exit(1)
owner, repo_name, pr_num = m.group(1), m.group(2), int(m.group(3))
token = os.environ.get("GITHUB_PAT", "")
if not token:
console.print("[red]GITHUB_PAT not set[/red]")
sys.exit(1)
from engine.github_mcp import fetch_pr_diff, post_pr_review
from engine.mcp_tools import build_github_mcp_toolset
pr_data = fetch_pr_diff(owner, repo_name, pr_num, token)
console.print(f"[bold]PR:[/bold] {pr_data['title']}")
mcp_toolset = build_github_mcp_toolset()
known_paths = {f["filename"] for f in pr_data["files"] if f.get("filename")}
console.print(f"[bold cyan]Sentinel[/bold cyan] PR + MCP ({domain})")
findings = run_agent(
args.repo, domain, rules_text,
extra_tools=[mcp_toolset],
diff_override=pr_data["diff"],
)
console.rule("[bold]Review")
print_review(findings)
if findings:
console.print("\n[cyan]Posting review to GitHub...[/cyan]")
result = post_pr_review(
owner, repo_name, pr_num, pr_data["head_sha"],
findings, token, known_paths=known_paths,
)
console.print(f"[green]Review posted:[/green] {result.get('html_url', '(no url)')}")
else:
console.print(f"[bold cyan]Sentinel[/bold cyan] reviewing {args.repo} ({domain})")
findings = run_agent(args.repo, domain, rules_text)
console.rule("[bold]Review")
print_review(findings)
if args.exit_on_blocker:
sys.exit(1 if any(f.get("severity") == "BLOCKER" for f in findings) else 0)
# ── Entry point ───────────────────────────────────────────────────────────────
def main():
# Backwards-compatible: if no subcommand given, default to 'review'
known_subcmds = {"review", "init", "hook"}
if len(sys.argv) < 2 or sys.argv[1] not in known_subcmds:
sys.argv.insert(1, "review")
p = argparse.ArgumentParser(prog="sentinel", description="Domain-aware AI code reviewer")
sub = p.add_subparsers(dest="cmd", required=True)
# review
r = sub.add_parser("review", help="Review staged changes or a GitHub PR")
r.add_argument("repo", nargs="?", default=".", help="Path to local repo (default: .)")
r.add_argument("--pr", metavar="URL", default=None, help="GitHub PR URL")
r.add_argument("--rules", default=str(_rules_dir() / "fintech.yaml"), help="Rule pack YAML")
r.add_argument("--exit-on-blocker", action="store_true", default=False)
# init
i = sub.add_parser("init", help="Create .sentinel/rules.yaml in a repo")
i.add_argument("repo", nargs="?", default=".", help="Path to target repo (default: .)")
i.add_argument("--domain", choices=AVAILABLE_DOMAINS, default="fintech")
i.add_argument("--force", action="store_true", help="Overwrite existing rules file")
# hook
h = sub.add_parser("hook", help="Install Sentinel as a pre-commit hook")
h.add_argument("repo", nargs="?", default=".", help="Path to target repo (default: .)")
args = p.parse_args()
if args.cmd == "init":
cmd_init(args)
elif args.cmd == "hook":
cmd_hook(args)
else:
cmd_review(args)
if __name__ == "__main__":
main()