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
58 changes: 58 additions & 0 deletions backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,61 @@ class AnalyzeRequest(BaseModel):

files: list[UploadedFile]
project_name: str = "untitled"


class ComparisonSnapshot(AnalyzeRequest):
files: list[UploadedFile] = Field(max_length=500)


class CompareRequest(BaseModel):
before: ComparisonSnapshot
after: ComparisonSnapshot


class ComparedFile(BaseModel):
path: str
status: str
before_bytes: int
after_bytes: int
before_hash: str | None = None
after_hash: str | None = None
diff: str = ""
diff_truncated: bool = False


class ComparisonSummary(BaseModel):
added: int = 0
deleted: int = 0
modified: int = 0
unchanged: int = 0
not_compared: int = 0


class FindingChange(BaseModel):
status: str
before: SecurityFinding | None = None
after: SecurityFinding | None = None


class SecurityComparison(BaseModel):
complete: bool
before_total: int
after_total: int
introduced: int | None = None
no_longer_detected: int | None = None
persistent: int | None = None
changes: list[FindingChange] = []
display_truncated: bool = False


class ProjectComparison(BaseModel):
schema_version: int = 1
before_name: str
after_name: str
before_files: int
after_files: int
summary: ComparisonSummary
files: list[ComparedFile]
excluded_paths: list[str]
security: SecurityComparison
notes: list[str]
17 changes: 17 additions & 0 deletions backend/routers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
CliCommand,
CoChangeCoupling,
CodeWalkStep,
CompareRequest,
ComplexFile,
ConfigFile,
ContributionGuide,
Expand Down Expand Up @@ -54,6 +55,7 @@
LicenseSummary,
OrphanModule,
PackageDependency,
ProjectComparison,
ProjectHealth,
ProjectMeta,
ProjectSummary,
Expand Down Expand Up @@ -86,6 +88,7 @@
ci_checks,
codemap_export,
commands,
comparison,
complexity,
config_files,
contributing,
Expand Down Expand Up @@ -455,6 +458,20 @@ def _content_analyses(
}


@router.post("/project/compare", response_model=ProjectComparison)
def compare_uploaded_projects(req: CompareRequest):
"""Compare source snapshots without storing them or calling an LLM."""
try:
return comparison.compare_projects(
[file.model_dump() for file in req.before.files],
[file.model_dump() for file in req.after.files],
before_name=req.before.project_name,
after_name=req.after.project_name,
)
except ValueError as exc:
raise HTTPException(400, str(exc)) from exc


@router.post("/project/upload", response_model=ProjectMeta)
async def upload_project(req: AnalyzeRequest):
"""Receive files uploaded from the frontend (webkitdirectory)."""
Expand Down
168 changes: 168 additions & 0 deletions backend/services/comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
"""Compare two uploaded source snapshots without executing either one."""

from __future__ import annotations

import difflib
import hashlib
from collections import defaultdict, deque
from itertools import islice
from pathlib import PurePosixPath

from backend.services import scanner, security

_MAX_SNAPSHOT_BYTES = 5 * 1024 * 1024
_MAX_DIFF_LINES = 80
_MAX_DIFF_CHARS = 8000
_MAX_SOURCE_LINES_FOR_DIFF = 2000
_MAX_FINDINGS = 5000
_MAX_DISPLAY_FINDINGS = 200


def _snapshot(files: list[dict]) -> tuple[dict[str, str], set[str]]:
contents: dict[str, str] = {}
total_bytes = 0
for item in files:
path = str(PurePosixPath(item["path"].replace("\\", "/")))
if path in contents:
raise ValueError(f"Duplicate file path: {path}")
content = item["content"]
total_bytes += len(content.encode("utf-8"))
if total_bytes > _MAX_SNAPSHOT_BYTES:
raise ValueError("Each snapshot must contain at most 5 MiB of text")
contents[path] = content
scanned = scanner.scan_uploaded_files(
[{"path": p, "content": c} for p, c in contents.items() if "\0" not in c]
)
allowed = {item["path"] for item in scanned}
return {p: c for p, c in contents.items() if p in allowed}, contents.keys() - allowed


def _preview(path: str, before: str, after: str) -> tuple[str, bool]:
old_lines, new_lines = before.splitlines(), after.splitlines()
if max(len(old_lines), len(new_lines)) > _MAX_SOURCE_LINES_FOR_DIFF:
return "", True
lines = list(
islice(
difflib.unified_diff(
old_lines, new_lines, fromfile=f"before/{path}", tofile=f"after/{path}", lineterm=""
),
_MAX_DIFF_LINES + 1,
)
)
text = "\n".join(lines[:_MAX_DIFF_LINES])
return text[:_MAX_DIFF_CHARS], len(lines) > _MAX_DIFF_LINES or len(text) > _MAX_DIFF_CHARS


def _security_changes(before: dict[str, str], after: dict[str, str]) -> dict:
old = security.scan_security(before, limit=_MAX_FINDINGS)
new = security.scan_security(after, limit=_MAX_FINDINGS)
complete = old["total"] <= _MAX_FINDINGS and new["total"] <= _MAX_FINDINGS
result = {
"complete": complete,
"before_total": old["total"],
"after_total": new["total"],
"introduced": None,
"no_longer_detected": None,
"persistent": None,
"changes": [],
"display_truncated": False,
}
if not complete:
return result

# A line insertion must not turn the same warning into a resolved/new pair.
previous: dict[tuple, deque] = defaultdict(deque)
for finding in old["findings"]:
key = (finding["file"], finding["category"], finding["snippet"])
previous[key].append(finding)
changes = []
for finding in new["findings"]:
key = (finding["file"], finding["category"], finding["snippet"])
match = previous[key].popleft() if previous[key] else None
changes.append(
{
"status": "persistent" if match else "introduced",
"before": match,
"after": finding,
}
)
for remaining in previous.values():
for finding in remaining:
changes.append({"status": "no_longer_detected", "before": finding, "after": None})
order = {"introduced": 0, "no_longer_detected": 1, "persistent": 2}
changes.sort(
key=lambda change: (
order[change["status"]],
(change["after"] or change["before"])["file"],
(change["after"] or change["before"])["line"],
)
)
for status in order:
result[status] = sum(change["status"] == status for change in changes)
result["changes"] = changes[:_MAX_DISPLAY_FINDINGS]
result["display_truncated"] = len(changes) > _MAX_DISPLAY_FINDINGS
return result


def compare_projects(
before_files: list[dict],
after_files: list[dict],
*,
before_name: str = "before",
after_name: str = "after",
) -> dict:
before, before_excluded = _snapshot(before_files)
after, after_excluded = _snapshot(after_files)
excluded = before_excluded | after_excluded
# An unreadable/filtered new version is unknown, not a deleted source file.
before = {p: c for p, c in before.items() if p not in excluded}
after = {p: c for p, c in after.items() if p not in excluded}
summary = {
"added": 0,
"deleted": 0,
"modified": 0,
"unchanged": 0,
"not_compared": len(excluded),
}
changes = []
for path in sorted(before.keys() | after.keys()):
old, new = before.get(path), after.get(path)
if old == new:
summary["unchanged"] += 1
continue
status = "added" if old is None else "deleted" if new is None else "modified"
summary[status] += 1
preview, truncated = _preview(path, old or "", new or "")
changes.append(
{
"path": path,
"status": status,
"before_bytes": len(old.encode("utf-8")) if old is not None else 0,
"after_bytes": len(new.encode("utf-8")) if new is not None else 0,
"before_hash": hashlib.sha256(old.encode("utf-8")).hexdigest()
if old is not None
else None,
"after_hash": hashlib.sha256(new.encode("utf-8")).hexdigest()
if new is not None
else None,
"diff": preview,
"diff_truncated": truncated,
}
)
return {
"schema_version": 1,
"before_name": before_name,
"after_name": after_name,
"before_files": len(before),
"after_files": len(after),
"summary": summary,
"files": changes,
"excluded_paths": sorted(excluded),
"security": _security_changes(before, after),
"notes": [
"Only the supplied readable text is compared; filtered files are unknown, not deleted.",
"Files are matched by relative path. A rename appears as a deletion and an addition.",
"Risk warnings are static pattern matches, not confirmed vulnerabilities or fixes.",
"No source code is executed and no language model is called.",
],
}
2 changes: 2 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./pages/Home";
import PrView from "./pages/PrView";
import CompareView from "./pages/CompareView";
import Overview from "./pages/Overview";
import FileView from "./pages/FileView";
import { I18nProvider } from "./lib/i18n";
Expand All @@ -12,6 +13,7 @@ function App() {
<Routes>
<Route path="/" element={<Home />} />
<Route path="/pr" element={<PrView />} />
<Route path="/compare" element={<CompareView />} />
<Route path="/project/:projectId" element={<Overview />} />
<Route path="/project/:projectId/file/*" element={<FileView />} />
</Routes>
Expand Down
73 changes: 73 additions & 0 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,76 @@ export async function getPrReading(url: string, explain = false): Promise<PrRead
return res.json();
}


export interface ComparedFile {
path: string;
status: "added" | "deleted" | "modified";
before_bytes: number;
after_bytes: number;
before_hash: string | null;
after_hash: string | null;
diff: string;
diff_truncated: boolean;
}

export interface ComparisonSummary {
added: number;
deleted: number;
modified: number;
unchanged: number;
not_compared: number;
}

export interface FindingChange {
status: "introduced" | "no_longer_detected" | "persistent";
before: SecurityFinding | null;
after: SecurityFinding | null;
}

export interface SecurityComparison {
complete: boolean;
before_total: number;
after_total: number;
introduced: number | null;
no_longer_detected: number | null;
persistent: number | null;
changes: FindingChange[];
display_truncated: boolean;
}

export interface ProjectComparison {
schema_version: number;
before_name: string;
after_name: string;
before_files: number;
after_files: number;
summary: ComparisonSummary;
files: ComparedFile[];
excluded_paths: string[];
security: SecurityComparison;
notes: string[];
}

/** Compare two local snapshots (before/after) without uploading them anywhere
* else: the backend matches files by path, diffs the changed ones, and diffs
* the static risk-warning sets. No LLM is involved. */
export async function compareProjects(
beforeFiles: { path: string; content: string }[],
beforeName: string,
afterFiles: { path: string; content: string }[],
afterName: string,
): Promise<ProjectComparison> {
const res = await apiFetch(`${BASE}/project/compare`, {
method: "POST",
headers: getHeaders(),
body: JSON.stringify({
before: { files: beforeFiles, project_name: beforeName },
after: { files: afterFiles, project_name: afterName },
}),
});
if (!res.ok) {
const err = await res.json().catch(() => ({ detail: "" }));
throw new Error(err.detail || "Failed to compare the two folders");
}
return res.json();
}
Loading
Loading