fix: persist last_offense_time and reset offenses after 24h
last_offense_time was in-memory only — lost on restart, so the offense_reset_minutes check never fired after a reboot. Now persisted as LastOffenseAt FLOAT in UserState. On startup hydration, stale offenses (and warned flag) are auto-cleared if the reset window has passed. Bumped offense_reset_minutes from 2h to 24h. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+17
-8
@@ -132,6 +132,12 @@ class Database:
|
||||
ALTER TABLE UserState ADD Warned BIT NOT NULL DEFAULT 0
|
||||
""")
|
||||
|
||||
# --- Schema migration for persisting last offense time ---
|
||||
cursor.execute("""
|
||||
IF COL_LENGTH('UserState', 'LastOffenseAt') IS NULL
|
||||
ALTER TABLE UserState ADD LastOffenseAt FLOAT NULL
|
||||
""")
|
||||
|
||||
cursor.execute("""
|
||||
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'BotSettings')
|
||||
CREATE TABLE BotSettings (
|
||||
@@ -291,19 +297,20 @@ class Database:
|
||||
baseline_coherence: float = 0.85,
|
||||
user_notes: str | None = None,
|
||||
warned: bool = False,
|
||||
last_offense_at: float | None = None,
|
||||
) -> None:
|
||||
"""Upsert user state (offense count, immunity, off-topic count, coherence baseline, notes, warned)."""
|
||||
"""Upsert user state (offense count, immunity, off-topic count, coherence baseline, notes, warned, last offense time)."""
|
||||
if not self._available:
|
||||
return
|
||||
try:
|
||||
await asyncio.to_thread(
|
||||
self._save_user_state_sync,
|
||||
user_id, offense_count, immune, off_topic_count, baseline_coherence, user_notes, warned,
|
||||
user_id, offense_count, immune, off_topic_count, baseline_coherence, user_notes, warned, last_offense_at,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to save user state")
|
||||
|
||||
def _save_user_state_sync(self, user_id, offense_count, immune, off_topic_count, baseline_coherence, user_notes, warned):
|
||||
def _save_user_state_sync(self, user_id, offense_count, immune, off_topic_count, baseline_coherence, user_notes, warned, last_offense_at):
|
||||
conn = self._connect()
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
@@ -314,13 +321,14 @@ class Database:
|
||||
WHEN MATCHED THEN
|
||||
UPDATE SET OffenseCount = ?, Immune = ?, OffTopicCount = ?,
|
||||
BaselineCoherence = ?, UserNotes = ?, Warned = ?,
|
||||
LastOffenseAt = ?,
|
||||
UpdatedAt = SYSUTCDATETIME()
|
||||
WHEN NOT MATCHED THEN
|
||||
INSERT (UserId, OffenseCount, Immune, OffTopicCount, BaselineCoherence, UserNotes, Warned)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?);""",
|
||||
INSERT (UserId, OffenseCount, Immune, OffTopicCount, BaselineCoherence, UserNotes, Warned, LastOffenseAt)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?);""",
|
||||
user_id,
|
||||
offense_count, 1 if immune else 0, off_topic_count, baseline_coherence, user_notes, 1 if warned else 0,
|
||||
user_id, offense_count, 1 if immune else 0, off_topic_count, baseline_coherence, user_notes, 1 if warned else 0,
|
||||
offense_count, 1 if immune else 0, off_topic_count, baseline_coherence, user_notes, 1 if warned else 0, last_offense_at,
|
||||
user_id, offense_count, 1 if immune else 0, off_topic_count, baseline_coherence, user_notes, 1 if warned else 0, last_offense_at,
|
||||
)
|
||||
cursor.close()
|
||||
finally:
|
||||
@@ -363,7 +371,7 @@ class Database:
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT UserId, OffenseCount, Immune, OffTopicCount, BaselineCoherence, UserNotes, Warned FROM UserState"
|
||||
"SELECT UserId, OffenseCount, Immune, OffTopicCount, BaselineCoherence, UserNotes, Warned, LastOffenseAt FROM UserState"
|
||||
)
|
||||
rows = cursor.fetchall()
|
||||
cursor.close()
|
||||
@@ -376,6 +384,7 @@ class Database:
|
||||
"baseline_coherence": float(row[4]),
|
||||
"user_notes": row[5] or "",
|
||||
"warned": bool(row[6]),
|
||||
"last_offense_at": float(row[7]) if row[7] is not None else 0.0,
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user