Fix/66 safety monitor multi turn metrics - #1022
Open
aliabbaka wants to merge 12 commits into
Open
Conversation
…t_count Add a failing unit test showing SafetyMonitor.get_event_count returns the all-time event total regardless of window_hours, so a multi-turn bypass that trips the content filter across turns is never reflected in a windowed metric. Refs ascherj#66 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Document root cause (untimestamped counter + ignored window_hours), the reproduction, and the sorted-set fix mirroring rate_limiter. Refs ascherj#66 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
log_event stored a single cumulative counter with no per-event timestamps, so get_event_count ignored window_hours and always returned the all-time total. Store each event as a timestamped member in a sorted set and count with zremrangebyscore + zcard, mirroring RateLimiter. Fixes ascherj#66
aliabbaka
marked this pull request as ready for review
August 10, 2026 03:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SafetyMonitor.get_event_countaccepted awindow_hoursargument but ignored it,because events were stored as a single cumulative
INCRcounter with no per-eventtimestamps. A 1-hour window and a 1000-hour window returned the same number, so a
multi-turn conversation that trips the content filter across several turns was never
reflected in a time-windowed metric. This switches storage to a Redis sorted set of
timestamped events and computes windowed counts with
zremrangebyscore+zcard,reusing the exact pattern already in
RateLimiter.Issue
Closes #66
Changes
log_event: store each event as a timestamped member in a sorted set(
zadd {str(now): now}) with a 24h expiry, instead ofincr.get_event_count: computewindow_start = now - window_hours*3600, trim oldentries with
zremrangebyscore, returnzcard.datetimeimport.the existing reproduction test now passes.
Testing
.venv/bin/pytest tests/unit/test_monitoring.py -v— 5/5)make test-integration) — N/A, no integration surfacemake linton changed files)mypy safety/)Notes for Reviewers
safety/rate_limiter.py's sorted-set approach for consistency.INCRkey don't carry over. Acceptable —safety metrics are ephemeral (24h expiry, dev-only).
SafetyMonitorcurrently has no callers; wiringget_event_countinto the content-filter/prompt-defense path (PLAN.md §3.3) is intentionally left as a follow-up to
keep this PR focused on the bug.