diff options
| author | Claude <noreply@anthropic.com> | 2026-11-12 23:04:18 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-13 00:10:48 +0000 |
| commit | f838db4bd88d1c79be222bca73ebb8af46c49fcf (patch) | |
| tree | d538e6619109f35c7cc9cf902db0cf628685f24c /plugins | |
| parent | f75f5b25ae94f4332be4ff5f18435741d2589bdb (diff) | |
security-guidance: keep a review's result and time on full metric rows
Only the first 20 keys of a hook's metrics are kept. Commit reviews with findings, and failed reviews that used tokens, often have more, so their time and sometimes their result were lost. The result, kind and time of a review now come first.
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
| -rwxr-xr-x | plugins/security-guidance/hooks/security_reminder_hook.py | 21 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_review_result_keys.py | 131 |
2 files changed, 147 insertions, 5 deletions
diff --git a/plugins/security-guidance/hooks/security_reminder_hook.py b/plugins/security-guidance/hooks/security_reminder_hook.py index fcd20a7f..4768ca9c 100755 --- a/plugins/security-guidance/hooks/security_reminder_hook.py +++ b/plugins/security-guidance/hooks/security_reminder_hook.py @@ -264,6 +264,14 @@ _LAST_METRIC_KEYS = ("diff_seen", "cr_pack") # CC keeps this many keys of a row. _METRIC_KEY_CAP = 20 +# A review row's result, kind and time. emit_metrics puts them right after its +# own keys (pv, launcher, usage, net_err), so the key cap cuts descriptive keys +# before any of these. Keep it short: a failed row can carry 12 keys ahead of +# them, and commit_review / push_sweep tell the rows apart under the old cap +# of 10. +_LEAD_METRIC_KEYS = ("review_failed", "api_error", "vulns_found", + "commit_review", "push_sweep", "review_ms", + "files_reviewed", "agentic", "deduped") # How long a Stop row waits for its commit count. _TURN_COMMITS_WAIT_S = 2.0 # Set by the Stop hook: queues the commit count for the row being emitted. @@ -374,7 +382,8 @@ def emit_metrics( the same reason but stale: once `rate_count` was added to every commit-review emit, the with-vulns dict hit 10 keys, `pv` was skipped, and findings metrics landed without a plugin version attached, breaking - per-version breakdowns. + per-version breakdowns. `_LEAD_METRIC_KEYS` come next on a review row, + wherever the caller put them. `rewake_summary` (asyncRewake only): per-run override of the static rewakeSummary in hooks.json, shown to the user in the terminal as the @@ -431,8 +440,10 @@ def emit_metrics( net_err = _net_error_metric() if net_err: head["net_err"] = net_err - if head: - metrics = {**head, **metrics} + # Skip rows are left as they are: theirs is the skip reason. + lead = {} if metrics.get("skipped") else { + k: metrics[k] for k in _LEAD_METRIC_KEYS if k in metrics} + metrics = {**head, **lead, **metrics} # Diagnostic keys go last, so they are the first dropped when a row is # over CC's key cap and never displace an existing key. tail = {} @@ -2176,7 +2187,7 @@ def handle_commit_review_posttooluse(input_data): # severity filter; `survived_after_sev` is the count the user actually # sees. Include `survived_after_sev` ONLY when the filter actually # dropped candidates — otherwise it's redundant with `survived` and eats - # into CC's metric-key cap, pushing files_reviewed/review_ms out of the + # into CC's metric-key cap, pushing critical_count/high_count out of the # emitted metrics. # # CC accepts only booleans and finite numbers as metric values. @@ -3214,7 +3225,7 @@ def handle_stop_hook(input_data): record_reviewed_diff(session_id, diff_hash) wake = _left_out_wake() # CC keeps the first 20 metric keys (10 in older versions), in insertion - # order, after the pv and usage keys emit_metrics puts first. The previous + # order, after the keys emit_metrics puts first. The previous # **sweep,**v2_metrics tail meant the 3 v2_metrics keys were always sliced # off this most-common path, so the diff-strategy diagnostics never reached # telemetry. Drop sweep here (it's PostToolUse-warning state, orthogonal to diff --git a/plugins/security-guidance/tests/test_review_result_keys.py b/plugins/security-guidance/tests/test_review_result_keys.py new file mode 100644 index 00000000..2cd05438 --- /dev/null +++ b/plugins/security-guidance/tests/test_review_result_keys.py @@ -0,0 +1,131 @@ +"""Review rows keep their result, kind and time when they have more keys than +Claude Code keeps (the first 20).""" +import json + +import pytest + +import security_reminder_hook as hook + +_USAGE = {"tok_in": 1, "tok_out": 1, "tok_cache_r": 0, "tok_cache_w": 0, + "cost_usd": 0.1, "api_calls": 3} +_HTTP_ERR = {"http_err_last": 429, "http_err_count": 2} +# The keys the commit and push handlers pass, in their order. +_REFLOG = {"sha_via_reflog": True, "reflog_shas_n": 1} +_COMMIT_BASE = {"commit_review": True, "commit_review_on": True, "cwd_is_repo": False, + "repo_resolution": 2, **_REFLOG, "diff_files_dropped": 3, "rate_count": 1} +_PUSH_BASE = {"push_sweep": True, "push_sweep_on": True, "cwd_is_repo": False, + "repo_resolution": 2, "diff_files_dropped": 3, "rate_count": 1} +# A commit made with `git commit -q`, the common shape of a row with findings. +_QUIET_COMMIT = {"vulns_found": 1, "commit_review": True, "commit_review_on": True, + **_REFLOG, "rate_count": 1, "agentic": True, "candidates": 1, "survived": 1, + "race_winner": 1, "critical_count": 0, "high_count": 1, + "files_reviewed": 4, "review_ms": 9500} +_COMMIT_FINDINGS = {"vulns_found": 2, **_COMMIT_BASE, "agentic": True, "candidates": 3, + "survived": 3, "survived_after_sev": 2, "race_winner": 1, + "critical_count": 1, "high_count": 1, + "files_reviewed": 4, "review_ms": 9500, "deduped": 1} +_COMMIT_CLEAN = {"vulns_found": 0, **_COMMIT_BASE, "agentic": True, "candidates": 3, + "survived": 0, "race_winner": 1, "files_reviewed": 4, "review_ms": 9500} +_COMMIT_KNOWN = {"vulns_found": 0, **_COMMIT_BASE, "agentic": True, "candidates": 3, + "survived": 3, "race_winner": 1, "deduped": 3, + "files_reviewed": 4, "review_ms": 9500} +_PUSH = {**_PUSH_BASE, "pushed": 2, "unreviewed": 1, "prefix_advanced": 1, "vulns_found": 2, + "files_reviewed": 4, "review_ms": 9500, "deduped": 1, "inflight_wait_s": 4} +_COMMIT_FAILED = {**_COMMIT_BASE, "agentic": False, "agentic_fallback": 2, + "race_winner": 1, "files_reviewed": 4, "review_ms": 9500} +_PUSH_FAILED = {**_PUSH_BASE, "inflight_wait_s": 4, "pushed": 2, "unreviewed": 1, + "files_reviewed": 4, "review_ms": 9500} + + +@pytest.fixture(autouse=True) +def _clean(monkeypatch): + monkeypatch.setattr(hook, "_PV", 20000) + monkeypatch.setattr(hook, "_TAIL_METRICS", {"diff_seen": 0, "child_stderr": 2}) + monkeypatch.setattr(hook, "_REVIEWER_EFFORT_METRICS", {"rv_effort": 5}) + monkeypatch.setattr(hook, "_LAUNCHER_METRICS", {}) + monkeypatch.setattr(hook, "_LAUNCHER_DIRECT", {}) + monkeypatch.setattr(hook, "_log_usage", lambda usage: None) + monkeypatch.setattr(hook, "_usage_metrics", lambda: dict(_USAGE)) + monkeypatch.setattr(hook, "_precheck_miss", False) + monkeypatch.setattr(hook, "_net_error_metric", lambda: 0) + monkeypatch.setattr(hook.llm, "gateway_cfg", lambda: 3) + monkeypatch.delenv("SG_LAUNCHER_FALLBACK", raising=False) + + +def _row(capsys): + return json.loads(capsys.readouterr().out)["metrics"] + + +def _kept(row, cap=hook._METRIC_KEY_CAP): + """The part of a row Claude Code forwards.""" + return dict(list(row.items())[:cap]) + + +_ROWS = {"quiet-commit": _QUIET_COMMIT, "commit-findings": _COMMIT_FINDINGS, + "commit-clean": _COMMIT_CLEAN, "commit-known": _COMMIT_KNOWN, "push": _PUSH} + + +@pytest.mark.parametrize("http_err", [{}, _HTTP_ERR], ids=["", "http-errors"]) +@pytest.mark.parametrize("name", _ROWS) +def test_result_and_time_are_kept(capsys, monkeypatch, name, http_err): + metrics = _ROWS[name] + monkeypatch.setattr(hook, "_usage_metrics", lambda: {**_USAGE, **http_err}) + hook.emit_metrics(metrics) + row = _row(capsys) + assert len(row) > hook._METRIC_KEY_CAP + kept = _kept(row) + must = ("vulns_found", "review_ms", "files_reviewed", "commit_review", + "push_sweep", "agentic", "deduped") + assert {k: kept.get(k) for k in must} == {k: metrics.get(k) for k in must} + assert {"pv", *_USAGE, *http_err} <= set(kept) + + +def test_quiet_commit_loses_one_descriptive_key(capsys): + hook.emit_metrics(_QUIET_COMMIT) + row = _row(capsys) + assert set(_QUIET_COMMIT) - set(_kept(row)) == {"high_count"} + + +@pytest.mark.parametrize("kind, metrics, split", [ + ("Commit", _COMMIT_FAILED, "commit_review"), ("Push", _PUSH_FAILED, "push_sweep")]) +def test_failed_review_keeps_result_and_time(capsys, monkeypatch, kind, metrics, split): + """With every key that can come ahead of them.""" + monkeypatch.setattr(hook, "_usage_metrics", lambda: {**_USAGE, **_HTTP_ERR}) + monkeypatch.setattr(hook, "_net_error_metric", lambda: 2001) + monkeypatch.setattr(hook, "_precheck_miss", True) + monkeypatch.setenv("SG_LAUNCHER_FALLBACK", "1") + monkeypatch.setattr(hook, "atomic_check_and_mark_warning", lambda *a: False) + with pytest.raises(SystemExit): + hook._exit_review_failed(kind, "s1", metrics, 429) + kept = _kept(_row(capsys)) + assert kept["review_failed"] is True and kept[split] is True + assert (kept["api_error"], kept["vulns_found"]) == (429, 0) + assert (kept["review_ms"], kept["files_reviewed"]) == (9500, 4) + assert kept["net_err"] == 2001 and kept.get("agentic") == metrics.get("agentic") + + +@pytest.mark.parametrize("metrics, split", [ + (_QUIET_COMMIT, "commit_review"), (_PUSH, "push_sweep")]) +def test_kind_and_time_fit_the_old_cap_of_ten(capsys, metrics, split): + hook.emit_metrics(metrics) + kept = _kept(_row(capsys), 10) + assert {"vulns_found", split, "review_ms"} <= set(kept) + + +def test_only_the_order_changes(capsys): + hook.emit_metrics(_COMMIT_FINDINGS) + row = _row(capsys) + added = {"pv", *_USAGE, "gateway_cfg", "diff_seen", "child_stderr", "rv_effort"} + assert {k: v for k, v in row.items() if k not in added} == _COMMIT_FINDINGS + rest = [k for k in _COMMIT_FINDINGS if k not in hook._LEAD_METRIC_KEYS] + assert [k for k in row if k in rest] == rest + + +@pytest.mark.parametrize("metrics", [ + {"skipped": True, "skip_reason": 21, "commit_review": True, "commit_review_on": True}, + {"skipped": True, "skip_reason": 35, "diff_strategy_v2": True, "deduped": 2, "review_ms": 900}, +]) +def test_skip_rows_are_unchanged(capsys, monkeypatch, metrics): + monkeypatch.setattr(hook, "_usage_metrics", dict) + hook.emit_metrics(metrics) + assert list(_row(capsys))[:1 + len(metrics)] == ["pv", *metrics] |
