diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-12 17:36:29 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-12 17:36:29 +0000 |
| commit | ed2ae7cd72fb60b4b3e58d622745870072cfd2aa (patch) | |
| tree | 155384ce450ed6dd08ce6012f1e9a2538b4e33bb | |
| parent | ccdb984025c4115b7d534a41a9fdad43a5283f74 (diff) | |
security-guidance: count diff headers the parser could not readmhegazy/sg-diff-parsing
A file whose header cannot be parsed is left out of the review without a trace. The Stop hook now reports diff_hdr_unparsed (a count, only when nonzero) so a header shape the parser misses shows up in metrics.
| -rw-r--r-- | plugins/security-guidance/hooks/gitutil.py | 8 | ||||
| -rwxr-xr-x | plugins/security-guidance/hooks/security_reminder_hook.py | 7 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_diff_parsing.py | 10 |
3 files changed, 25 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py index 553ce8e0..93b7a87e 100644 --- a/plugins/security-guidance/hooks/gitutil.py +++ b/plugins/security-guidance/hooks/gitutil.py @@ -1030,6 +1030,12 @@ def _parse_diff_header(header): return old[2:], new[2:] +# Section headers that could not be parsed during this hook run; that file is +# left out of the review. The Stop hook reports the count so a header shape +# this parser misses shows up in telemetry instead of passing silently. +_unparsed_diff_headers = set() + + def _iter_diff_sections(diff_output): """Yield (new_path, lines after the header line) per file section.""" for section in _DIFF_SECTION_RE.split(diff_output)[1:]: @@ -1037,6 +1043,8 @@ def _iter_diff_sections(diff_output): paths = _parse_diff_header(lines[0]) if paths: yield paths[1] or paths[0], lines[1:] + else: + _unparsed_diff_headers.add(lines[0]) def extract_file_paths_from_diff(diff_output): diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index e8df7a1c..3516a5a7 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -125,6 +125,7 @@ from reporesolve import ( # noqa: E402,F401 toplevel_from_command, repo_containing_commit, scan_roots, resolve_repo_root, save_repo_hint, load_repo_hint, ) +import gitutil # noqa: E402 module ref for per-run counters import llm # noqa: E402 module ref for reassignable globals (_last_call_claude_http_error etc.) from llm import ( # noqa: E402,F401 ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, HAS_API_CREDENTIALS, @@ -1959,6 +1960,8 @@ def handle_stop_hook(input_data): "skipped": True, "skip_reason": reason, "fire_index": fire_count + 1, "diff_strategy_v2": True, **v2_metrics, **extra, **sweep, + **({"diff_hdr_unparsed": len(gitutil._unparsed_diff_headers)} + if gitutil._unparsed_diff_headers else {}), }) sys.exit(0) @@ -2174,6 +2177,8 @@ def handle_stop_hook(input_data): if llm._last_review_truncated_bytes else {}), **({"repo_resolution": repo_res} if res_metrics else {}), **sweep_trimmed, + **({"diff_hdr_unparsed": len(gitutil._unparsed_diff_headers)} + if gitutil._unparsed_diff_headers else {}), }, rewake_summary=_format_vulns_summary(vulns), additional_context=(PROVENANCE_BANNER + "\n\n" + concrete_guidance + CONTINUATION_SUFFIX + "\n"), @@ -2204,6 +2209,8 @@ def handle_stop_hook(input_data): **({"diff_truncated": llm._last_review_truncated_bytes} if llm._last_review_truncated_bytes else {}), **v2_metrics, + **({"diff_hdr_unparsed": len(gitutil._unparsed_diff_headers)} + if gitutil._unparsed_diff_headers else {}), }) sys.exit(0) diff --git a/plugins/security-guidance/tests/test_diff_parsing.py b/plugins/security-guidance/tests/test_diff_parsing.py index 54d01f7b..7665a064 100644 --- a/plugins/security-guidance/tests/test_diff_parsing.py +++ b/plugins/security-guidance/tests/test_diff_parsing.py @@ -87,3 +87,13 @@ class TestCommitOutput: ) parsed = gitutil.parse_diff_into_files(r.stdout.decode()) assert [p for p, _ in parsed] == ["app.py"] + + +def test_unparsed_headers_are_counted_for_telemetry(): + import gitutil + gitutil._unparsed_diff_headers.clear() + diff = 'diff --git "a/x.py b/x.py\n@@ -0,0 +1 @@\n+x = 1\n' + assert gitutil.parse_diff_into_files(diff) == [] + assert gitutil.extract_file_paths_from_diff(diff) == [] + assert len(gitutil._unparsed_diff_headers) == 1 + gitutil._unparsed_diff_headers.clear() |
