Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/self-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ jobs:
- name: Run the review bot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
run: uv run --frozen multi-agent-code-review
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ wheels/

# Virtual environments
.venv

# Secrets / local environment
.env
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ authors = [
]
requires-python = ">=3.11"
dependencies = [
"google-genai>=2.10.0",
"langgraph>=1.0.10",
"pydantic>=2.13.4",
"pygithub>=2.9.1",
"python-dotenv>=1.2.2",
"unidiff>=0.7.5",
]

[project.scripts]
Expand Down
12 changes: 12 additions & 0 deletions sample_bad_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import hashlib
import pickle

PASSWORD = "admin123"


def load_session(raw):
return pickle.loads(raw)


def hash_pw(pw):
return hashlib.md5(pw.encode()).hexdigest()
57 changes: 57 additions & 0 deletions scripts/try_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Manual smoke test: run the full review graph on a sample diff.

Makes real Gemini calls (three specialists + one synthesizer). Run with:
uv run python scripts/try_graph.py
"""

from dotenv import load_dotenv
from google import genai

from multi_agent_code_review.graph import build_graph

load_dotenv()

SAMPLE_DIFF = """\
diff --git a/payments.py b/payments.py
--- a/payments.py
+++ b/payments.py
@@ -1,2 +1,16 @@
import hashlib
+import pickle
+
+STRIPE_KEY = "sk_live_51H8xQ2eZvKYlo3"
+
+def process(data, user, amount, currency, retries, discount, tax, region):
+ obj = pickle.loads(data)
+ token = hashlib.md5(str(amount).encode()).hexdigest()
+ total = amount
+ if amount > 0:
+ if currency == "USD":
+ if region == "US":
+ if discount:
+ total = amount - discount
+ return total
x = 1
"""


def main() -> None:
client = genai.Client()
graph = build_graph(
client=client,
specialist_model="gemini-2.5-flash",
synthesizer_model="gemini-2.5-flash", # dev: Flash for cost; Pro later
)
result = graph.invoke({"diff": SAMPLE_DIFF})

raw, filtered = result["findings"], result["filtered"]
print(f"Raw findings from all three specialists: {len(raw)}")
print(f"After deterministic filters: {len(filtered)}")
print("\n" + "=" * 70)
print("FINAL PR COMMENT")
print("=" * 70 + "\n")
print(result["comment"])


if __name__ == "__main__":
main()
54 changes: 54 additions & 0 deletions scripts/try_security_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Manual smoke test: run the security agent against a sample insecure diff.

Makes a REAL Gemini call. Run with:
uv run python scripts/try_security_agent.py
"""

from dotenv import load_dotenv
from google import genai

from multi_agent_code_review.agents.security import review_security

load_dotenv() # load GEMINI_API_KEY from .env into the environment

SAMPLE_DIFF = """\
diff --git a/auth.py b/auth.py
--- a/auth.py
+++ b/auth.py
@@ -1,3 +1,8 @@
import hashlib
+
+API_KEY = "sk-live-9f8a7b6c5d4e3f2g1h"
+
+def hash_password(password):
+ return hashlib.md5(password.encode()).hexdigest()
+
+def run_query(user_input):
+ cursor.execute("SELECT * FROM users WHERE name = '" + user_input + "'")
"""


def main() -> None:
client = genai.Client() # auto-reads GEMINI_API_KEY from the environment
findings = review_security(
client=client,
model="gemini-2.5-flash",
diff=SAMPLE_DIFF,
)

if not findings:
print("No findings returned.")
return

print(f"{len(findings)} finding(s):\n")
for f in findings:
print(f"[{f.severity.value.upper()}] {f.file}:{f.line_start}-{f.line_end}")
print(f" {f.title}")
print(f" {f.explanation}")
if f.suggested_fix:
print(f" Fix: {f.suggested_fix}")
print()


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions src/multi_agent_code_review/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Specialist review agents."""
40 changes: 40 additions & 0 deletions src/multi_agent_code_review/agents/coverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test-coverage specialist agent.

Reviews a unified diff for changed code that lacks adequate tests, focusing on
high-risk paths: public APIs, error handling, and branching logic. Returns
structured Findings, using suggested_fix to describe the specific test to add.
LLM-only for now; the test-file discovery heuristic gets wired in later.
"""

from google import genai

from multi_agent_code_review.llm_client import generate_findings
from multi_agent_code_review.schemas import Finding

TEST_COVERAGE_SYSTEM_PROMPT = """\
You are a test-coverage reviewer. You are given a unified diff from a pull request.
Identify changed code that is risky to leave untested, such as:
- new public functions or methods with no apparent tests
- error-handling and exception paths
- conditional branches and edge cases

Rules:
- Only report gaps for code you can see in the diff. Do not speculate about code you cannot see.
- Reference the file and line numbers of the changed code that needs testing.
- Use suggested_fix to describe the specific test that should be added.
- If the changed code does not need additional tests, return an empty list. Never invent findings.
- Keep explanations precise and concise.
"""


def review_test_coverage(
*, client: genai.Client, model: str, diff: str
) -> list[Finding]:
"""Run the test-coverage specialist over a unified diff."""
return generate_findings(
client=client,
model=model,
system_prompt=TEST_COVERAGE_SYSTEM_PROMPT,
user_content=diff,
schema=Finding,
)
42 changes: 42 additions & 0 deletions src/multi_agent_code_review/agents/maintainability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Maintainability specialist agent.

Reviews a unified diff for maintainability issues only: overly long functions,
high complexity, unclear naming, duplicated logic, missing docstrings on public
APIs, and dead code. Returns structured Findings. LLM-only for now; the static
tools (ruff, radon) get wired in later.
"""

from google import genai

from multi_agent_code_review.llm_client import generate_findings
from multi_agent_code_review.schemas import Finding

MAINTAINABILITY_SYSTEM_PROMPT = """\
You are a code maintainability reviewer. You are given a unified diff from a pull request.
Report only genuine maintainability issues introduced by the added lines, such as:
- functions that are too long (roughly over 50 lines) or do too much
- high cyclomatic complexity (deeply nested or heavily branching logic)
- unclear or misleading names for variables, functions, or classes
- duplicated logic that should be factored out
- missing docstrings on public functions, classes, or modules
- dead or unreachable code

Rules:
- Only report issues you can see in the diff. Do not speculate about code you cannot see.
- Reference the file and line numbers shown in the diff.
- If there are no genuine maintainability issues, return an empty list. Never invent findings.
- Keep explanations precise and concise.
"""


def review_maintainability(
*, client: genai.Client, model: str, diff: str
) -> list[Finding]:
"""Run the maintainability specialist over a unified diff."""
return generate_findings(
client=client,
model=model,
system_prompt=MAINTAINABILITY_SYSTEM_PROMPT,
user_content=diff,
schema=Finding,
)
37 changes: 37 additions & 0 deletions src/multi_agent_code_review/agents/security.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Security specialist agent.

Reviews a unified diff for security issues only: secrets, injection vectors,
unsafe deserialization, weak crypto. Returns structured Findings. This v1 is
LLM-only; the static-analysis tools (bandit, semgrep) get wired in next.
"""

from google import genai

from multi_agent_code_review.llm_client import generate_findings
from multi_agent_code_review.schemas import Finding

SECURITY_SYSTEM_PROMPT = """\
You are a security code reviewer. You are given a unified diff from a pull request.
Report only genuine security issues introduced by the added lines, such as:
- hardcoded secrets or credentials
- injection vectors (SQL, command, path traversal)
- unsafe deserialization (pickle, yaml.load without SafeLoader)
- weak cryptography used for security (MD5/SHA1, weak randomness)

Rules:
- Only report issues you can see in the diff. Do not speculate about code you cannot see.
- Reference the file and line numbers shown in the diff.
- If there are no genuine security issues, return an empty list. Never invent findings.
- Keep explanations precise and concise.
"""


def review_security(*, client: genai.Client, model: str, diff: str) -> list[Finding]:
"""Run the security specialist over a unified diff."""
return generate_findings(
client=client,
model=model,
system_prompt=SECURITY_SYSTEM_PROMPT,
user_content=diff,
schema=Finding,
)
98 changes: 98 additions & 0 deletions src/multi_agent_code_review/agents/synthesizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Synthesizer: turn filtered specialist findings into one PR comment.

A single LLM call decides which findings matter most and writes a short summary;
the markdown itself is rendered deterministically from the original Finding
objects. The model can only *select and order* findings by index — it cannot
invent findings or alter their file, line, severity, or text, because those are
read back from the validated Finding objects, not from the model's output.
"""

from google import genai
from pydantic import BaseModel

from multi_agent_code_review.llm_client import generate_structured
from multi_agent_code_review.schemas import Finding

MAX_FINDINGS = 7

SYNTHESIZER_SYSTEM_PROMPT = """\
You are the lead reviewer. You are given a numbered list of validated findings
from specialist reviewers, plus the pull request diff for context.

Your job:
- Decide which findings are worth surfacing in the final review, most important first.
- Prioritize by severity and genuine impact.
- Write a short (one or two sentence) summary of the review.

Return JSON with:
- "summary": your short summary.
- "finding_indices": indices (from the numbered list) of findings to include,
ordered most important first.

Rules:
- Only use indices from the numbered list. Never invent findings.
- Do not restate file names, line numbers, or severities; those are handled separately.
"""


class SynthesisPlan(BaseModel):
"""What the synthesizer LLM returns: a summary and an ordering of findings."""

summary: str
finding_indices: list[int]


def _select(plan: SynthesisPlan, findings: list[Finding]) -> list[Finding]:
"""Resolve indices into findings: in-range, de-duplicated, capped."""
seen: set[int] = set()
selected: list[Finding] = []
for i in plan.finding_indices:
if 0 <= i < len(findings) and i not in seen:
seen.add(i)
selected.append(findings[i])
if len(selected) >= MAX_FINDINGS:
break
return selected


def render_comment(summary: str, findings: list[Finding]) -> str:
"""Render the final PR comment markdown deterministically from findings."""
lines = ["## Automated code review", "", summary, ""]
if not findings:
return "\n".join(lines).rstrip() + "\n"
lines += ["### Findings", ""]
for n, f in enumerate(findings, start=1):
sev = f.severity.value.upper()
cat = f.category.value if f.category else "general"
lines.append(
f"{n}. **[{sev}]** {f.title} — `{f.file}:{f.line_start}-{f.line_end}` ({cat})"
)
lines.append(f" {f.explanation}")
if f.suggested_fix:
lines.append(f" *Suggested fix:* {f.suggested_fix}")
lines.append("")
return "\n".join(lines).rstrip() + "\n"


def synthesize(
*, client: genai.Client, model: str, diff: str, findings: list[Finding]
) -> str:
"""Produce the final PR comment markdown from filtered findings."""
if not findings:
return render_comment("No significant issues found.", [])

numbered = "\n".join(
f"{i}: [{f.severity.value}] {f.file}:{f.line_start}-{f.line_end} "
f"({f.category.value if f.category else 'general'}) {f.title} — {f.explanation}"
for i, f in enumerate(findings)
)
user_content = f"FINDINGS:\n{numbered}\n\nDIFF:\n{diff}"

plan = generate_structured(
client=client,
model=model,
system_prompt=SYNTHESIZER_SYSTEM_PROMPT,
user_content=user_content,
schema=SynthesisPlan,
)
return render_comment(plan.summary, _select(plan, findings))
Loading