Pranoy Mukherjee commited on
Commit
1c77a4c
·
1 Parent(s): 7a376ec

Add prioritized report summaries

Browse files
README.md CHANGED
@@ -58,6 +58,8 @@ Each finding includes:
58
  - suggested fix
59
  - agent source
60
 
 
 
61
  ## Current Agents
62
 
63
  - Security Agent: flags hardcoded secrets, disabled TLS verification, and dynamic code execution.
 
58
  - suggested fix
59
  - agent source
60
 
61
+ Reports preserve full finding totals while displaying a prioritized subset for readability. High-severity findings are shown first, repeated low-severity findings are summarized, and warnings explain when lower-priority findings are hidden from the demo report.
62
+
63
  ## Current Agents
64
 
65
  - Security Agent: flags hardcoded secrets, disabled TLS verification, and dynamic code execution.
app/agents/docs_agent.py CHANGED
@@ -86,7 +86,7 @@ class DocsAgent:
86
  return findings
87
 
88
  def _scan_python_docstrings(self, chunk: CodeChunk) -> list[Finding]:
89
- findings: list[Finding] = []
90
  lines = chunk.content.splitlines()
91
 
92
  for index, line in enumerate(lines):
@@ -101,19 +101,25 @@ class DocsAgent:
101
  continue
102
 
103
  line_number = chunk.line_start + index
104
- findings.append(
105
- self._finding(
106
- "Public Python symbol missing docstring",
107
- Severity.low,
108
- chunk,
109
- line_number,
110
- line_number,
111
- f"`{symbol_name}` is public but does not start with a docstring.",
112
- "Add a short docstring describing purpose, parameters, return value, or side effects.",
113
- )
 
 
 
 
 
 
 
114
  )
115
-
116
- return findings
117
 
118
  def _has_docstring(self, lines: list[str], definition_index: int) -> bool:
119
  for line in lines[definition_index + 1 : definition_index + 5]:
 
86
  return findings
87
 
88
  def _scan_python_docstrings(self, chunk: CodeChunk) -> list[Finding]:
89
+ missing_symbols: list[tuple[str, int]] = []
90
  lines = chunk.content.splitlines()
91
 
92
  for index, line in enumerate(lines):
 
101
  continue
102
 
103
  line_number = chunk.line_start + index
104
+ missing_symbols.append((symbol_name, line_number))
105
+
106
+ if not missing_symbols:
107
+ return []
108
+
109
+ examples = ", ".join(f"`{name}` line {line}" for name, line in missing_symbols[:5])
110
+ extra_count = len(missing_symbols) - 5
111
+ extra_note = f" plus {extra_count} more" if extra_count > 0 else ""
112
+ return [
113
+ self._finding(
114
+ "Public Python symbols missing docstrings",
115
+ Severity.low,
116
+ chunk,
117
+ missing_symbols[0][1],
118
+ missing_symbols[-1][1],
119
+ f"{len(missing_symbols)} public symbols in this file section are missing docstrings: {examples}{extra_note}.",
120
+ "Add short docstrings to public functions/classes, starting with exported APIs and complex behavior.",
121
  )
122
+ ]
 
123
 
124
  def _has_docstring(self, lines: list[str], definition_index: int) -> bool:
125
  for line in lines[definition_index + 1 : definition_index + 5]:
app/agents/synthesizer_agent.py CHANGED
@@ -8,26 +8,41 @@ SEVERITY_ORDER = {
8
  Severity.low: 3,
9
  }
10
 
 
 
 
 
 
 
 
 
11
 
12
  class SynthesizerAgent:
13
  name = "Synthesizer Agent"
14
 
15
  async def synthesize(self, repo: RepoScanResult, outputs: list[AgentOutput]) -> AuditReport:
16
- findings = self._dedupe([finding for output in outputs for finding in output.findings])
17
- findings.sort(key=lambda finding: (SEVERITY_ORDER[finding.severity], finding.file_path, finding.line_start))
18
 
19
  summary = {severity: 0 for severity in Severity}
20
- for finding in findings:
21
  summary[finding.severity] += 1
22
 
 
 
 
23
  return AuditReport(
24
  repo_url=repo.repo_url,
25
  scanned_file_count=len(repo.files),
26
  skipped_file_count=repo.skipped_files,
27
- findings=findings,
28
  severity_summary=summary,
 
 
 
 
29
  agents_run=[output.agent_name for output in outputs] + [self.name],
30
- warnings=repo.warnings,
31
  )
32
 
33
  def _dedupe(self, findings: list[Finding]) -> list[Finding]:
@@ -40,3 +55,44 @@ class SynthesizerAgent:
40
  seen.add(key)
41
  unique.append(finding)
42
  return unique
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  Severity.low: 3,
9
  }
10
 
11
+ MAX_DISPLAY_FINDINGS = 40
12
+ MAX_DISPLAY_FINDINGS_BY_AGENT = {
13
+ "Security Agent": 20,
14
+ "Performance Agent": 12,
15
+ "Quality Agent": 10,
16
+ "Docs Agent": 8,
17
+ }
18
+
19
 
20
  class SynthesizerAgent:
21
  name = "Synthesizer Agent"
22
 
23
  async def synthesize(self, repo: RepoScanResult, outputs: list[AgentOutput]) -> AuditReport:
24
+ all_findings = self._dedupe([finding for output in outputs for finding in output.findings])
25
+ all_findings.sort(key=self._sort_key)
26
 
27
  summary = {severity: 0 for severity in Severity}
28
+ for finding in all_findings:
29
  summary[finding.severity] += 1
30
 
31
+ agent_counts = {output.agent_name: len(output.findings) for output in outputs}
32
+ display_findings, hidden_count, warnings = self._select_display_findings(all_findings, agent_counts)
33
+
34
  return AuditReport(
35
  repo_url=repo.repo_url,
36
  scanned_file_count=len(repo.files),
37
  skipped_file_count=repo.skipped_files,
38
+ findings=display_findings,
39
  severity_summary=summary,
40
+ total_findings_count=len(all_findings),
41
+ displayed_findings_count=len(display_findings),
42
+ hidden_findings_count=hidden_count,
43
+ agent_finding_counts=agent_counts,
44
  agents_run=[output.agent_name for output in outputs] + [self.name],
45
+ warnings=repo.warnings + warnings,
46
  )
47
 
48
  def _dedupe(self, findings: list[Finding]) -> list[Finding]:
 
55
  seen.add(key)
56
  unique.append(finding)
57
  return unique
58
+
59
+ def _select_display_findings(
60
+ self,
61
+ findings: list[Finding],
62
+ agent_counts: dict[str, int],
63
+ ) -> tuple[list[Finding], int, list[str]]:
64
+ selected: list[Finding] = []
65
+ selected_by_agent = {agent_name: 0 for agent_name in agent_counts}
66
+
67
+ for finding in findings:
68
+ agent_limit = MAX_DISPLAY_FINDINGS_BY_AGENT.get(finding.agent_source, MAX_DISPLAY_FINDINGS)
69
+ if selected_by_agent.get(finding.agent_source, 0) >= agent_limit:
70
+ continue
71
+ if len(selected) >= MAX_DISPLAY_FINDINGS:
72
+ break
73
+ selected.append(finding)
74
+ selected_by_agent[finding.agent_source] = selected_by_agent.get(finding.agent_source, 0) + 1
75
+
76
+ hidden_count = max(0, len(findings) - len(selected))
77
+ warnings: list[str] = []
78
+ if hidden_count:
79
+ warnings.append(
80
+ f"Report display prioritized {len(selected)} of {len(findings)} findings; "
81
+ f"{hidden_count} lower-priority findings are hidden from the demo report."
82
+ )
83
+
84
+ for agent_name, total_count in agent_counts.items():
85
+ displayed_count = selected_by_agent.get(agent_name, 0)
86
+ hidden_for_agent = total_count - displayed_count
87
+ if hidden_for_agent > 0:
88
+ warnings.append(f"{agent_name}: displaying {displayed_count} of {total_count} findings.")
89
+
90
+ return selected, hidden_count, warnings
91
+
92
+ def _sort_key(self, finding: Finding) -> tuple[int, int, str, int]:
93
+ test_file_penalty = 1 if self._is_test_file(finding.file_path) and finding.severity != Severity.critical else 0
94
+ return (SEVERITY_ORDER[finding.severity], test_file_penalty, finding.file_path, finding.line_start)
95
+
96
+ def _is_test_file(self, file_path: str) -> bool:
97
+ normalized = file_path.lower().replace("\\", "/")
98
+ return "/test" in normalized or normalized.startswith("test") or "_test." in normalized
app/schemas.py CHANGED
@@ -63,6 +63,10 @@ class AuditReport(BaseModel):
63
  skipped_file_count: int
64
  findings: list[Finding]
65
  severity_summary: dict[Severity, int]
 
 
 
 
66
  generated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
67
  agents_run: list[str]
68
  warnings: list[str] = Field(default_factory=list)
 
63
  skipped_file_count: int
64
  findings: list[Finding]
65
  severity_summary: dict[Severity, int]
66
+ total_findings_count: int = 0
67
+ displayed_findings_count: int = 0
68
+ hidden_findings_count: int = 0
69
+ agent_finding_counts: dict[str, int] = Field(default_factory=dict)
70
  generated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
71
  agents_run: list[str]
72
  warnings: list[str] = Field(default_factory=list)
app/services/report_formatter.py CHANGED
@@ -8,6 +8,7 @@ def format_report_markdown(report: AuditReport) -> str:
8
  f"Repository: `{report.repo_url}`",
9
  f"Files scanned: `{report.scanned_file_count}`",
10
  f"Files skipped: `{report.skipped_file_count}`",
 
11
  "",
12
  "## Severity Summary",
13
  "",
@@ -16,6 +17,11 @@ def format_report_markdown(report: AuditReport) -> str:
16
  for severity in [Severity.critical, Severity.high, Severity.medium, Severity.low]:
17
  lines.append(f"- **{severity.value}**: {report.severity_summary.get(severity, 0)}")
18
 
 
 
 
 
 
19
  if report.warnings:
20
  lines.extend(["", "## Warnings", ""])
21
  lines.extend(f"- {warning}" for warning in report.warnings)
 
8
  f"Repository: `{report.repo_url}`",
9
  f"Files scanned: `{report.scanned_file_count}`",
10
  f"Files skipped: `{report.skipped_file_count}`",
11
+ f"Findings shown: `{report.displayed_findings_count}` of `{report.total_findings_count}`",
12
  "",
13
  "## Severity Summary",
14
  "",
 
17
  for severity in [Severity.critical, Severity.high, Severity.medium, Severity.low]:
18
  lines.append(f"- **{severity.value}**: {report.severity_summary.get(severity, 0)}")
19
 
20
+ if report.agent_finding_counts:
21
+ lines.extend(["", "## Agent Summary", ""])
22
+ for agent_name, count in report.agent_finding_counts.items():
23
+ lines.append(f"- **{agent_name}**: {count}")
24
+
25
  if report.warnings:
26
  lines.extend(["", "## Warnings", ""])
27
  lines.extend(f"- {warning}" for warning in report.warnings)
tests/test_docs_agent.py CHANGED
@@ -49,6 +49,25 @@ async def test_docs_agent_flags_public_python_symbol_without_docstring():
49
 
50
  output = await DocsAgent().analyze([chunk])
51
 
52
- assert output.findings[0].title == "Public Python symbol missing docstring"
53
  assert output.findings[0].severity == Severity.low
54
  assert output.findings[0].line_start == 10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  output = await DocsAgent().analyze([chunk])
51
 
52
+ assert output.findings[0].title == "Public Python symbols missing docstrings"
53
  assert output.findings[0].severity == Severity.low
54
  assert output.findings[0].line_start == 10
55
+
56
+
57
+ @pytest.mark.anyio
58
+ async def test_docs_agent_summarizes_missing_docstrings_per_chunk():
59
+ chunk = CodeChunk(
60
+ file_path="service.py",
61
+ language="Python",
62
+ line_start=1,
63
+ line_end=4,
64
+ content="def first():\n pass\n\ndef second():\n pass",
65
+ )
66
+
67
+ output = await DocsAgent().analyze([chunk])
68
+
69
+ docstring_findings = [
70
+ finding for finding in output.findings if finding.title == "Public Python symbols missing docstrings"
71
+ ]
72
+ assert len(docstring_findings) == 1
73
+ assert "2 public symbols" in docstring_findings[0].description
tests/test_security_report.py CHANGED
@@ -26,3 +26,6 @@ async def test_security_agent_and_synthesizer_return_structured_report():
26
  assert report.findings[0].file_path == "app.py"
27
  assert report.findings[0].line_start == 10
28
  assert report.severity_summary[Severity.high] == 1
 
 
 
 
26
  assert report.findings[0].file_path == "app.py"
27
  assert report.findings[0].line_start == 10
28
  assert report.severity_summary[Severity.high] == 1
29
+ assert report.total_findings_count == 1
30
+ assert report.displayed_findings_count == 1
31
+ assert report.hidden_findings_count == 0
tests/test_synthesizer_agent.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from app.agents.synthesizer_agent import SynthesizerAgent
4
+ from app.schemas import AgentOutput, Finding, RepoScanResult, Severity
5
+
6
+
7
+ def make_finding(index: int, agent: str = "Docs Agent", severity: Severity = Severity.low) -> Finding:
8
+ return Finding(
9
+ title=f"Finding {index}",
10
+ severity=severity,
11
+ file_path=f"file_{index}.py",
12
+ line_start=1,
13
+ line_end=1,
14
+ description="Description",
15
+ why_it_matters="Why",
16
+ suggested_fix="Fix",
17
+ agent_source=agent,
18
+ )
19
+
20
+
21
+ @pytest.mark.anyio
22
+ async def test_synthesizer_preserves_totals_when_display_is_truncated():
23
+ output = AgentOutput(
24
+ agent_name="Docs Agent",
25
+ findings=[make_finding(index) for index in range(20)],
26
+ )
27
+ repo = RepoScanResult(repo_url="https://github.com/example/project", local_path=".", files=[], skipped_files=0)
28
+
29
+ report = await SynthesizerAgent().synthesize(repo, [output])
30
+
31
+ assert report.total_findings_count == 20
32
+ assert report.displayed_findings_count == 8
33
+ assert report.hidden_findings_count == 12
34
+ assert report.agent_finding_counts["Docs Agent"] == 20
35
+ assert any("displaying 8 of 20" in warning for warning in report.warnings)
36
+
37
+
38
+ @pytest.mark.anyio
39
+ async def test_synthesizer_keeps_high_severity_before_low_findings():
40
+ outputs = [
41
+ AgentOutput(agent_name="Docs Agent", findings=[make_finding(1, severity=Severity.low)]),
42
+ AgentOutput(agent_name="Security Agent", findings=[make_finding(2, "Security Agent", Severity.high)]),
43
+ ]
44
+ repo = RepoScanResult(repo_url="https://github.com/example/project", local_path=".", files=[], skipped_files=0)
45
+
46
+ report = await SynthesizerAgent().synthesize(repo, outputs)
47
+
48
+ assert report.findings[0].severity == Severity.high