diff options
| author | Claude <noreply@anthropic.com> | 2026-11-10 02:52:01 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-10 02:52:01 +0000 |
| commit | fa2face5178e7c20f8d0195728056d8bdad82c2f (patch) | |
| tree | d03e5df0e15d67840cab48afd7cb449b8045a563 /plugins/security-guidance/hooks/diffstate.py | |
| parent | dceb15d50832e6adf380bedc26dde03d251324b9 (diff) | |
security-guidance: pack the custom rule counts into one telemetry keymhegazy/sg-pack-custom-rule-counts
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins/security-guidance/hooks/diffstate.py')
| -rw-r--r-- | plugins/security-guidance/hooks/diffstate.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py index 2dac1208..6df7a5c6 100644 --- a/plugins/security-guidance/hooks/diffstate.py +++ b/plugins/security-guidance/hooks/diffstate.py @@ -83,9 +83,31 @@ def record_touched_path(session_id, file_path, custom_rule_stats=None): 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") +# A full Stop row lost these and the turn's commit count, so they travel as +# one decimal number, each field capped to its digits: +# rules (2) shapes (3) gain (3) loss (3) slow_ms (4) +# 15 digits at most, well inside what a JSON number holds exactly. +_CUSTOM_RULE_PACK = (("cr_rules", 99), ("cr_shapes", 999), ("cr_gain", 999), + ("cr_loss", 999), ("cr_slow_ms", 9999)) + + +# Kept in the state per turn. See extensibility.edit_stats. cr_rules, cr_shapes +# and cr_slow_ms are maximum or union over the turn's edits, the others are +# added up. +CUSTOM_RULE_KEYS = tuple(key for key, _ in _CUSTOM_RULE_PACK) + + +def custom_rule_pack(kept): + """The turn's custom rule counts as one number; 0 when there are none.""" + if not isinstance(kept, dict): + return 0 + value = 0 + for key, cap in _CUSTOM_RULE_PACK: + part = kept.get(key, 0) + if not isinstance(part, int) or isinstance(part, bool) or part < 0: + part = 0 + value = value * (10 ** len(str(cap))) + min(part, cap) + return value def _fold_custom_rule_stats(state, stats): |
