fix(P1): 6 priority-1 bug fixes for autonomous engine and mood system

#4  Sleep/mood desync — set_server_mood() now clears is_sleeping when
    mood changes away from 'asleep', preventing ghost-sleep state.

#5  Race condition in _check_and_act — added per-guild asyncio.Lock so
    overlapping ticks + message-triggered calls cannot fire concurrently.

#6  Class-level attrs on ServerConfig — sleepy_responses_left,
    angry_wakeup_timer, and forced_angry_until are now proper dataclass
    fields with defaults, so asdict()/from_dict() round-trip correctly.
    Also strips unknown keys in from_dict() to survive schema changes.

#7  Persistence decay_factor crash — initialise decay_factor = 1.0
    before the loop so empty-server or zero-downtime paths don't
    raise NameError.

#8  Double record_action — removed the redundant call in
    autonomous_tick_v2(); only _check_and_act records the action now.

#9  Engine mood desync — on_mood_change() is now called inside
    set_server_mood() (single source of truth) and removed from 4
    call-sites in api.py, moods.py, and server_manager wakeup task.
This commit is contained in:
2026-02-23 13:31:15 +02:00
parent 422366df4c
commit 0e4aebf353
6 changed files with 98 additions and 101 deletions

View File

@@ -762,13 +762,6 @@ async def set_server_mood_endpoint(guild_id: int, data: MoodSetRequest):
logger.debug(f"Server mood set result: {success}")
if success:
# V2: Notify autonomous engine of mood change
try:
from utils.autonomous import on_mood_change
on_mood_change(guild_id, data.mood)
except Exception as e:
logger.error(f"Failed to notify autonomous engine of mood change: {e}")
# Update the nickname for this server
from utils.moods import update_server_nickname
logger.debug(f"Updating nickname for server {guild_id}")
@@ -793,13 +786,6 @@ async def reset_server_mood_endpoint(guild_id: int):
logger.debug(f"Server mood reset result: {success}")
if success:
# V2: Notify autonomous engine of mood change
try:
from utils.autonomous import on_mood_change
on_mood_change(guild_id, "neutral")
except Exception as e:
logger.error(f"Failed to notify autonomous engine of mood reset: {e}")
# Update the nickname for this server
from utils.moods import update_server_nickname
logger.debug(f"Updating nickname for server {guild_id}")
@@ -1860,14 +1846,6 @@ async def test_mood_change(guild_id: int, data: MoodSetRequest):
logger.debug(f"TEST: Mood set result: {success}")
if success:
# V2: Notify autonomous engine of mood change
try:
from utils.autonomous import on_mood_change
on_mood_change(guild_id, data.mood)
logger.debug(f"TEST: Notified autonomous engine of mood change")
except Exception as e:
logger.error(f"TEST: Failed to notify autonomous engine: {e}")
# Try to update nickname
from utils.moods import update_server_nickname
logger.debug(f"TEST: Attempting nickname update...")