CLAUDE: Fix critical bug preventing Monday freeze/thaw from executing

## Problem
The weekly freeze/thaw system had a state flag initialization bug that prevented
both Monday freeze operations and Saturday thaw operations from executing.

## Root Cause (Introduced in commit 07f69eb on Oct 25, 2025)
The `weekly_warning_sent` boolean flag was being used for TWO different purposes:
1. Preventing duplicate freeze/thaw operations (state machine)
2. Preventing duplicate error notifications

The flag was initialized to `False`, but the Monday freeze condition required
it to be `True`:
- Line 205: `if ... and self.weekly_warning_sent:` (required True)
- Line 160: `self.weekly_warning_sent = False` (initialized to False)

This created a deadlock:
- Monday freeze: Never ran (flag was False, needed True)
- Saturday thaw: Never ran (freeze flag in DB never set to True)

## Impact
- First failure: Monday October 27, 2025
- Affected weeks: 2+ weeks of missed freeze/thaw operations
- Result: Week did NOT increment, transactions did NOT execute

## Solution
Separated the two concerns and replaced boolean flag with week tracking:

1. **Deduplication**: Track `last_freeze_week` and `last_thaw_week` (int | None)
   - Monday: Execute if `last_freeze_week != current.week`
   - Saturday: Execute if `last_thaw_week != current.week`
   - Prevents duplicate operations during same hour (loop runs every minute)

2. **Error notifications**: Separate `error_notification_sent` boolean flag
   - Only used for preventing duplicate error notifications
   - Clear separation of concerns

## Validation
Created and ran validation script simulating 7 scenarios across multiple weeks:
-  Monday freeze executes on first check
-  Monday freeze skips on subsequent checks same hour
-  Saturday thaw executes on first check
-  Saturday thaw skips on subsequent checks same hour
-  Next Monday freeze executes for new week
-  Week numbers increment correctly (19→20→21→22)
-  All state transitions work as expected

## Files Changed
- tasks/transaction_freeze.py:157-167 - Separated state tracking from error flags
- tasks/transaction_freeze.py:211-231 - Fixed freeze/thaw conditions with week tracking
- tasks/transaction_freeze.py:248-250 - Updated error notification flag name

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Claude 2025-11-05 17:56:44 +00:00
parent 1286a5952b
commit 57b4dc0ce9
No known key found for this signature in database

View File

@ -157,7 +157,14 @@ class TransactionFreezeTask:
def __init__(self, bot: commands.Bot):
self.bot = bot
self.logger = get_contextual_logger(f'{__name__}.TransactionFreezeTask')
self.weekly_warning_sent = False # Prevent duplicate error notifications
# Track last execution to prevent duplicate operations
self.last_freeze_week: int | None = None
self.last_thaw_week: int | None = None
# Track error notifications separately
self.error_notification_sent = False
self.logger.info("Transaction freeze/thaw task initialized")
# Start the weekly loop
@ -202,16 +209,27 @@ class TransactionFreezeTask:
)
# BEGIN FREEZE: Monday at 00:00, not already frozen
if now.weekday() == 0 and now.hour == 0 and not current.freeze and self.weekly_warning_sent:
self.logger.info("Triggering freeze begin")
if now.weekday() == 0 and now.hour == 0 and not current.freeze:
# Only run if we haven't already frozen this week
# Track the week we're freezing FROM (before increment)
if self.last_freeze_week != current.week:
self.logger.info("Triggering freeze begin", current_week=current.week)
await self._begin_freeze(current)
self.weekly_warning_sent = False
self.last_freeze_week = current.week # Track the week we froze (before increment)
self.error_notification_sent = False # Reset error flag for new cycle
else:
self.logger.debug("Freeze already executed for week", week=current.week)
# END FREEZE: Saturday at 00:00, currently frozen
elif now.weekday() == 5 and now.hour == 0 and current.freeze and not self.weekly_warning_sent:
self.logger.info("Triggering freeze end")
elif now.weekday() == 5 and now.hour == 0 and current.freeze:
# Only run if we haven't already thawed this week
if self.last_thaw_week != current.week:
self.logger.info("Triggering freeze end", current_week=current.week)
await self._end_freeze(current)
self.weekly_warning_sent = True
self.last_thaw_week = current.week
self.error_notification_sent = False # Reset error flag for new cycle
else:
self.logger.debug("Thaw already executed for week", week=current.week)
else:
self.logger.debug("No freeze/thaw action needed at this time")
@ -228,9 +246,9 @@ class TransactionFreezeTask:
)
try:
if not self.weekly_warning_sent:
if not self.error_notification_sent:
await self._send_owner_notification(error_message)
self.weekly_warning_sent = True
self.error_notification_sent = True
except Exception as notify_error:
self.logger.error(f"Failed to send error notification: {notify_error}")