diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-11-09 17:01:09 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-09 17:01:09 -0800 |
| commit | 4e39a22ba3e8139c20992ea534fec644c2ef0cd1 (patch) | |
| tree | 91c7dda10972ab95c257c66e313c224aee5dfb9f /plugins/security-guidance/hooks/diffstate.py | |
| parent | ea686bae367e20a4092b2fd4327ffd299bb60f24 (diff) | |
| parent | 9ea2fbca5b6c62113e011a8f183bb336a26f9883 (diff) | |
Merge pull request #7030 from anthropics/mhegazy/sg-custom-rule-counts
security-guidance: count how custom rules are written
Diffstat (limited to 'plugins/security-guidance/hooks/diffstate.py')
| -rw-r--r-- | plugins/security-guidance/hooks/diffstate.py | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py index 174207f7..2dac1208 100644 --- a/plugins/security-guidance/hooks/diffstate.py +++ b/plugins/security-guidance/hooks/diffstate.py @@ -63,7 +63,7 @@ def load_baseline_sha(session_id): return with_locked_state(session_id, _load) -def record_touched_path(session_id, file_path): +def record_touched_path(session_id, file_path, custom_rule_stats=None): """Append a file path to the touched_paths list (deduped, capped at 200). Stop is the consumer and clears under the same lock it reads with; UPS @@ -72,6 +72,9 @@ def record_touched_path(session_id, file_path): stale paths so over-retention is harmless, just wasteful. """ def _record(state): + # In the save this hook makes anyway: one more could drop another + # hook's change where there is no state lock (Windows). + _fold_custom_rule_stats(state, custom_rule_stats) paths = state.setdefault("touched_paths", []) if file_path not in paths: paths.append(file_path) @@ -80,6 +83,36 @@ def record_touched_path(session_id, file_path): with_locked_state(session_id, _record) +# Reported on the Stop row; see extensibility.edit_stats. The first three are +# kept as maximum or union over the turn's edits, the others are added up. +CUSTOM_RULE_KEYS = ("cr_rules", "cr_slow_ms", "cr_shapes", "cr_gain", "cr_loss") + + +def _fold_custom_rule_stats(state, stats): + if not stats: + return + kept = state.get("custom_rules") + if not isinstance(kept, dict): + kept = state["custom_rules"] = {} + for key in CUSTOM_RULE_KEYS: + new, old = stats.get(key, 0), kept.get(key, 0) + if not isinstance(old, int): + old = 0 + if key == "cr_shapes": + value = old | new + elif key in ("cr_rules", "cr_slow_ms"): + value = max(old, new) + else: + value = old + new + if value: + kept[key] = value + + +def record_custom_rule_stats(session_id, stats): + """Fold one edit's custom rule counts into the turn's. Telemetry only.""" + with_locked_state(session_id, lambda state: _fold_custom_rule_stats(state, stats)) + + def consume_stop_state(session_id, clear=True): """Atomically snapshot all state the Stop hook needs and clear touched_paths. @@ -121,6 +154,10 @@ def consume_stop_state(session_id, clear=True): "state_resets": state.get("state_resets", 0), "state_overwrites": state.get("state_overwrites", 0), "baseline_fail": state.get("baseline_fail") or 0, + "custom_rules": ( + dict(state["custom_rules"]) + if isinstance(state.get("custom_rules"), dict) else {} + ), } # touched_paths is cleared per Stop, but one baseline can be # reviewed by several Stops (rewakes). turn_touched keeps every path @@ -139,6 +176,7 @@ def consume_stop_state(session_id, clear=True): state.pop("reviewed_diff_hash", None) state.pop("state_resets", None) state.pop("state_overwrites", None) + state.pop("custom_rules", None) return snap return with_locked_state(session_id, _snap) or { @@ -147,7 +185,7 @@ def consume_stop_state(session_id, clear=True): "baseline_kept": 0, "turn_started": None, "fire_count": 0, "fire_count_expired": False, "previous_findings": [], "reviewed_diff_hash": None, "state_resets": 0, "state_overwrites": 0, - "baseline_fail": 0, "turn_touched": [], + "baseline_fail": 0, "turn_touched": [], "custom_rules": {}, } |
