fix(tasks): replace fire-and-forget asyncio.create_task with create_tracked_task

Add utils/task_tracker.py with create_tracked_task() that wraps background
tasks with error logging, cancellation handling, and reference tracking.

Replace all 17 fire-and-forget asyncio.create_task() calls across 7 files:
- bot/bot.py (5 interjection checks)
- bot/utils/autonomous.py (2 check-and-act/react tasks)
- bot/utils/bipolar_mode.py (3 argument tasks)
- bot/commands/uno.py (1 game loop task)
- bot/utils/voice_receiver.py (3 STT/interruption callbacks)
- bot/utils/persona_dialogue.py (4 dialogue turn/interjection tasks)

Previously-tracked tasks (voice_audio.py, voice_manager.py) were left as-is
since they already store task references for cancellation.

Closes #1
This commit is contained in:
2026-02-18 12:01:08 +02:00
parent cf55b15745
commit 7b7abcfc68
7 changed files with 104 additions and 25 deletions

View File

@@ -20,6 +20,7 @@ import asyncio
import time
import globals
from utils.logger import get_logger
from utils.task_tracker import create_tracked_task
logger = get_logger('persona')
@@ -668,15 +669,16 @@ You can use emojis naturally! ✨💙"""
opposite = "evil" if responding_persona == "miku" else "miku"
if should_continue and confidence in ["HIGH", "MEDIUM"]:
asyncio.create_task(self._next_turn(channel, opposite))
create_tracked_task(self._next_turn(channel, opposite), task_name="persona_next_turn")
elif should_continue and confidence == "LOW":
asyncio.create_task(self._next_turn(channel, opposite))
create_tracked_task(self._next_turn(channel, opposite), task_name="persona_next_turn")
elif not should_continue and confidence == "LOW":
# Offer opposite persona the last word
asyncio.create_task(
self._offer_last_word(channel, opposite, context + f"\n{responding_persona}: {response_text}")
create_tracked_task(
self._offer_last_word(channel, opposite, context + f"\n{responding_persona}: {response_text}"),
task_name="persona_last_word"
)
else:
# Clear signal to end
@@ -788,7 +790,7 @@ Don't force a response if you have nothing meaningful to contribute."""
logger.info(f"Dialogue ended after last word, {state['turn_count']} turns total")
self.end_dialogue(channel.id)
else:
asyncio.create_task(self._next_turn(channel, opposite))
create_tracked_task(self._next_turn(channel, opposite), task_name="persona_next_turn")
# ========================================================================
# ARGUMENT ESCALATION
@@ -953,8 +955,9 @@ async def check_for_interjection(message: discord.Message, current_persona: str)
# Start dialogue with the opposite persona responding first
dialogue_manager.start_dialogue(message.channel.id)
asyncio.create_task(
dialogue_manager.handle_dialogue_turn(message.channel, opposite_persona, trigger_reason=reason)
create_tracked_task(
dialogue_manager.handle_dialogue_turn(message.channel, opposite_persona, trigger_reason=reason),
task_name="persona_dialogue_turn"
)
return True