fix(c7): close common safety and lifecycle gaps

这个提交存在于:
Codex Local 2026-07-30 07:31:12 +08:00
父节点 d006b85773
当前提交 8faf8b5ef9
共有 4 个文件被更改,包括 243 次插入 和 66 次删除

查看文件

@ -13,12 +13,14 @@ from dataclasses import dataclass
from datetime import datetime, timezone from datetime import datetime, timezone
import hashlib import hashlib
import json import json
import logging
from typing import Any, Awaitable, Callable, Mapping from typing import Any, Awaitable, Callable, Mapping
from .bot_personal_dto import BotPersonalArchiveDTO, build_bot_personal_dto from .bot_personal_dto import BotPersonalArchiveDTO, build_bot_personal_dto
OUTBOX_STATES = frozenset({"pending", "retry", "sent", "deduplicated", "dead_letter", "invalid"}) OUTBOX_STATES = frozenset({"pending", "retry", "sent", "deduplicated", "dead_letter", "invalid"})
logger = logging.getLogger(__name__)
def _now_iso() -> str: def _now_iso() -> str:
@ -80,6 +82,7 @@ class BotPersonalOutbox:
max_attempts: int = 5, max_attempts: int = 5,
base_backoff_seconds: float = 30.0, base_backoff_seconds: float = 30.0,
max_backoff_seconds: float = 3600.0, max_backoff_seconds: float = 3600.0,
background_task: Callable[[Awaitable[Any], str], Any] | None = None,
) -> None: ) -> None:
self.data = data self.data = data
self.save = save self.save = save
@ -87,6 +90,8 @@ class BotPersonalOutbox:
self.max_attempts = max(1, int(max_attempts or 5)) self.max_attempts = max(1, int(max_attempts or 5))
self.base_backoff_seconds = max(0.0, float(base_backoff_seconds or 30.0)) self.base_backoff_seconds = max(0.0, float(base_backoff_seconds or 30.0))
self.max_backoff_seconds = max(self.base_backoff_seconds, float(max_backoff_seconds or 3600.0)) self.max_backoff_seconds = max(self.base_backoff_seconds, float(max_backoff_seconds or 3600.0))
self.background_task = background_task
self._persist_tasks: set[asyncio.Task] = set()
self._lock = asyncio.Lock() self._lock = asyncio.Lock()
entries = self.data.setdefault("bot_personal_outbox", []) entries = self.data.setdefault("bot_personal_outbox", [])
if not isinstance(entries, list): if not isinstance(entries, list):
@ -109,12 +114,34 @@ class BotPersonalOutbox:
try: try:
result = self.save() result = self.save()
if asyncio.iscoroutine(result) or hasattr(result, "__await__"): if asyncio.iscoroutine(result) or hasattr(result, "__await__"):
asyncio.create_task(result) if callable(self.background_task):
self.background_task(result, "bot_personal_outbox_save")
return
try:
task = asyncio.create_task(result)
except RuntimeError:
closer = getattr(result, "close", None)
if callable(closer):
closer()
return
self._persist_tasks.add(task)
task.add_done_callback(self._collect_persist_task)
except Exception: except Exception:
# The in-memory queue remains authoritative for this process; the # The in-memory queue remains authoritative for this process; the
# next normal store save can still persist the already-mutated data. # next normal store save can still persist the already-mutated data.
return return
def _collect_persist_task(self, task: asyncio.Task) -> None:
self._persist_tasks.discard(task)
if task.cancelled():
return
try:
error = task.exception()
except asyncio.CancelledError:
return
if error is not None:
logger.warning("Bot Personal outbox save task failed: %s", error, exc_info=error)
@staticmethod @staticmethod
def _key(memory_type: str, idempotency_key: str) -> str: def _key(memory_type: str, idempotency_key: str) -> str:
return f"{str(memory_type or '').strip()}::{str(idempotency_key or '').strip()}" return f"{str(memory_type or '').strip()}::{str(idempotency_key or '').strip()}"

查看文件

@ -15913,8 +15913,9 @@ class DailyStateMixin:
} }
if memory_companion_proactive_payload: if memory_companion_proactive_payload:
await self._memory_companion_record_proactive_message(**memory_companion_proactive_payload) await self._memory_companion_record_proactive_message(**memory_companion_proactive_payload)
asyncio.create_task( self._create_lifecycle_background_task(
self._refresh_persona_relationship(user_id, current_snapshot, trigger="proactive") self._refresh_persona_relationship(user_id, current_snapshot, trigger="proactive"),
label="refresh_persona_relationship_proactive",
) )
await self._run_proactive_maintenance_tasks() await self._run_proactive_maintenance_tasks()

219
main.py
查看文件

@ -2,6 +2,7 @@ from __future__ import annotations
import asyncio import asyncio
import base64 import base64
from contextlib import asynccontextmanager
import functools import functools
import gc import gc
import hashlib import hashlib
@ -2375,6 +2376,10 @@ class PrivateCompanionPlugin(
self._bot_personal_outbox = BotPersonalOutbox( self._bot_personal_outbox = BotPersonalOutbox(
self.data, self.data,
save=lambda: self._schedule_data_save(delay=0.5), save=lambda: self._schedule_data_save(delay=0.5),
background_task=lambda operation, label: self._create_lifecycle_background_task(
operation,
label=label,
),
) )
self.unified_person_registry = UnifiedPersonRegistry(self.data) self.unified_person_registry = UnifiedPersonRegistry(self.data)
self._apply_tts_runtime_overrides() self._apply_tts_runtime_overrides()
@ -2841,10 +2846,50 @@ class PrivateCompanionPlugin(
def discard_finished_task(finished: asyncio.Task) -> None: def discard_finished_task(finished: asyncio.Task) -> None:
if self._startup_background_tasks.get(label) is finished: if self._startup_background_tasks.get(label) is finished:
self._startup_background_tasks.pop(label, None) self._startup_background_tasks.pop(label, None)
if finished.cancelled():
return
try:
error = finished.exception()
except asyncio.CancelledError:
return
if error is not None:
logger.warning(
"[PrivateCompanion] 启动后台任务异常结束: task=%s error=%s",
_single_line(label, 100) or "startup",
_single_line(error, 180),
exc_info=(type(error), error, error.__traceback__),
)
task.add_done_callback(discard_finished_task) task.add_done_callback(discard_finished_task)
return task return task
@asynccontextmanager
async def _temporarily_release_data_lock(self):
"""Release the data lock only around an external await, then reacquire it.
Callers use this inside an existing ``async with self._data_lock`` block.
Reacquisition is cancellation-safe so a cancelled visual/LLM operation
cannot leave the shared state lock open.
"""
lock = getattr(self, "_data_lock", None)
if lock is None or not lock.locked():
yield
return
lock.release()
reacquire_cancelled = False
try:
yield
finally:
while True:
try:
await lock.acquire()
break
except asyncio.CancelledError:
reacquire_cancelled = True
if reacquire_cancelled:
raise asyncio.CancelledError
def _create_lifecycle_background_task( def _create_lifecycle_background_task(
self, self,
operation: Any, operation: Any,
@ -11199,17 +11244,21 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
tts_value = tts_parts[1].strip() if len(tts_parts) >= 2 else "" tts_value = tts_parts[1].strip() if len(tts_parts) >= 2 else ""
response = self._set_tts_voice_language_from_command(tts_value) response = self._set_tts_voice_language_from_command(tts_value)
elif action in companion_manual_confirm_actions: elif action in companion_manual_confirm_actions:
response = await self._companion_manual_apply_pending_config(event) async with self._temporarily_release_data_lock():
response = await self._companion_manual_apply_pending_config(event)
elif action in companion_manual_cancel_actions: elif action in companion_manual_cancel_actions:
response = self._companion_manual_cancel_pending_config(event) response = self._companion_manual_cancel_pending_config(event)
elif action in companion_manual_setting_actions: elif action in companion_manual_setting_actions:
response = await self._companion_manual_apply_setting_command(event, value) async with self._temporarily_release_data_lock():
response = await self._companion_manual_apply_setting_command(event, value)
elif action in companion_manual_query_actions: elif action in companion_manual_query_actions:
response = "正在结合说明书和当前运行状态做诊断。" response = "正在结合说明书和当前运行状态做诊断。"
elif action in {"参考图", "人设参考图", "自拍参考图"}: elif action in {"参考图", "人设参考图", "自拍参考图"}:
response, response_image_path = await self._photo_reference_command_payload(event, user_id, value) async with self._temporarily_release_data_lock():
response, response_image_path = await self._photo_reference_command_payload(event, user_id, value)
elif action == "参考图库": elif action == "参考图库":
response, response_image_path = await self._photo_reference_library_command_payload(event, user_id, value) async with self._temporarily_release_data_lock():
response, response_image_path = await self._photo_reference_library_command_payload(event, user_id, value)
elif action in daily_outfit_view_actions: elif action in daily_outfit_view_actions:
response, response_image_path = self._daily_outfit_command_payload() response, response_image_path = self._daily_outfit_command_payload()
elif action in image_api_status_actions: elif action in image_api_status_actions:
@ -11276,8 +11325,9 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
elif action in {"书柜密码", "夹层密码", "抽屉密码", "书柜暗格"}: elif action in {"书柜密码", "夹层密码", "抽屉密码", "书柜暗格"}:
response = "这个要直接问我本人。她会不会说、怎么说,要看当时的人格和心情。" response = "这个要直接问我本人。她会不会说、怎么说,要看当时的人格和心情。"
elif bookshelf_password_output_requested: elif bookshelf_password_output_requested:
password = await self._ensure_bookshelf_password_async() async with self._temporarily_release_data_lock():
password_reason = await self._ensure_bookshelf_password_reason_async(password) password = await self._ensure_bookshelf_password_async()
password_reason = await self._ensure_bookshelf_password_reason_async(password)
secret = self.data.get("bookshelf_secret", {}) if isinstance(self.data.get("bookshelf_secret"), dict) else {} secret = self.data.get("bookshelf_secret", {}) if isinstance(self.data.get("bookshelf_secret"), dict) else {}
response = ( response = (
"当前书柜夹层密码:\n" "当前书柜夹层密码:\n"
@ -11292,7 +11342,8 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
self.data["bookshelf_secret"] = secret self.data["bookshelf_secret"] = secret
secret.pop("password", None) secret.pop("password", None)
secret["reset_at"] = _now_ts() secret["reset_at"] = _now_ts()
await self._ensure_bookshelf_password_async() async with self._temporarily_release_data_lock():
await self._ensure_bookshelf_password_async()
self._save_data_sync() self._save_data_sync()
response = "已重新设置书柜夹层密码。需要查看真实密码可用:陪伴 输出夹层密码" response = "已重新设置书柜夹层密码。需要查看真实密码可用:陪伴 输出夹层密码"
elif action in {"发说说", "发QQ空间", "发布说说", "空间发布", "发布空间"}: elif action in {"发说说", "发QQ空间", "发布说说", "空间发布", "发布空间"}:
@ -11906,7 +11957,8 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
and self._private_event_has_image_safe(event, label="private_text_image") and self._private_event_has_image_safe(event, label="private_text_image")
): ):
try: try:
persisted_images = await self._persist_private_inbound_images(event, user_id) async with self._temporarily_release_data_lock():
persisted_images = await self._persist_private_inbound_images(event, user_id)
usable_images = [source for source in persisted_images if self._private_image_source_to_model_url(source)] usable_images = [source for source in persisted_images if self._private_image_source_to_model_url(source)]
except Exception as exc: except Exception as exc:
missing = _missing_optional_model_dependency(exc) missing = _missing_optional_model_dependency(exc)
@ -11948,15 +12000,16 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
setattr(event, "private_companion_delayed_image_mode", image_mode) setattr(event, "private_companion_delayed_image_mode", image_mode)
if image_mode == "caption": if image_mode == "caption":
try: try:
vision_text = _single_line( async with self._temporarily_release_data_lock():
await self._transcribe_private_inbound_images( vision_text = _single_line(
usable_images[:5], await self._transcribe_private_inbound_images(
umo=umo, usable_images[:5],
user_text=text, umo=umo,
force_contextual=self._private_image_user_mentions_combo_result(text) or self._private_image_user_has_specific_vision_request(text), user_text=text,
), force_contextual=self._private_image_user_mentions_combo_result(text) or self._private_image_user_has_specific_vision_request(text),
self._private_image_vision_text_limit(len(usable_images)), ),
) self._private_image_vision_text_limit(len(usable_images)),
)
except Exception as exc: except Exception as exc:
missing = _missing_optional_model_dependency(exc) missing = _missing_optional_model_dependency(exc)
if not missing: if not missing:
@ -12011,7 +12064,8 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
) )
buffers = getattr(self, "_semantic_message_buffers", None) buffers = getattr(self, "_semantic_message_buffers", None)
if isinstance(buffers, dict) and isinstance(buffers.get(key), dict): if isinstance(buffers, dict) and isinstance(buffers.get(key), dict):
persisted_images = await self._persist_private_inbound_images(event, user_id) async with self._temporarily_release_data_lock():
persisted_images = await self._persist_private_inbound_images(event, user_id)
has_model_usable_image = any(self._private_image_source_to_model_url(source) for source in persisted_images) has_model_usable_image = any(self._private_image_source_to_model_url(source) for source in persisted_images)
if not persisted_images: if not persisted_images:
buffers.pop(key, None) buffers.pop(key, None)
@ -12100,14 +12154,15 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
) )
else: else:
try: try:
smart_wait = await self._smart_message_debounce_wait_seconds_for_event( async with self._temporarily_release_data_lock():
event, smart_wait = await self._smart_message_debounce_wait_seconds_for_event(
key=key, event,
text=text, key=key,
sender_id=user_id, text=text,
sender_name=sender_display_name, sender_id=user_id,
private_chat=True, sender_name=sender_display_name,
) private_chat=True,
)
except Exception as exc: except Exception as exc:
missing = _missing_optional_model_dependency(exc) missing = _missing_optional_model_dependency(exc)
if not missing: if not missing:
@ -12232,7 +12287,10 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
"created_at": _now_ts(), "created_at": _now_ts(),
"local": deepcopy(intent_profile), "local": deepcopy(intent_profile),
} }
asyncio.create_task(self._refine_inbound_emotion_with_model(user_id, text, deepcopy(intent_profile))) self._create_lifecycle_background_task(
self._refine_inbound_emotion_with_model(user_id, text, deepcopy(intent_profile)),
label="inbound_emotion_refine",
)
else: else:
self._update_relationship_state_from_intent(user, intent_profile) self._update_relationship_state_from_intent(user, intent_profile)
if is_target_user and self._cancel_inbound_conflicting_greeting( if is_target_user and self._cancel_inbound_conflicting_greeting(
@ -12323,18 +12381,28 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
self._stop_private_reply_after_user_rest_signal(event, user_id, rest_silence_early_text or text) self._stop_private_reply_after_user_rest_signal(event, user_id, rest_silence_early_text or text)
return return
if is_target_user and schedule_adjustment_applied: if is_target_user and schedule_adjustment_applied:
asyncio.create_task(self._kick_proactive_loop_once()) self._create_lifecycle_background_task(
self._kick_proactive_loop_once(),
label="kick_proactive_loop_inbound",
)
if response: if response:
await self._reply(event, response) await self._reply(event, response)
event.stop_event() event.stop_event()
elif is_target_user: elif is_target_user:
pass pass
if is_target_user: if is_target_user:
asyncio.create_task( self._create_lifecycle_background_task(
self._refresh_persona_relationship(user_id, user_snapshot, trigger="inbound") self._refresh_persona_relationship(user_id, user_snapshot, trigger="inbound"),
label="refresh_persona_relationship_inbound",
)
self._create_lifecycle_background_task(
self._maybe_refresh_companion_memory(user_id, user_snapshot),
label="refresh_companion_memory_inbound",
)
self._create_lifecycle_background_task(
self._maybe_refresh_dialogue_episode(user_id, user_snapshot),
label="refresh_dialogue_episode_inbound",
) )
asyncio.create_task(self._maybe_refresh_companion_memory(user_id, user_snapshot))
asyncio.create_task(self._maybe_refresh_dialogue_episode(user_id, user_snapshot))
def _record_c3_inbound_activity( def _record_c3_inbound_activity(
self, self,
@ -12380,7 +12448,10 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
if result is not None and scope == "private": if result is not None and scope == "private":
recorder = getattr(self, "_memory_companion_record_observed_activity", None) recorder = getattr(self, "_memory_companion_record_observed_activity", None)
if callable(recorder): if callable(recorder):
asyncio.create_task(recorder(result)) self._create_lifecycle_background_task(
recorder(result),
label="record_observed_activity",
)
return result return result
except Exception as exc: except Exception as exc:
logger.debug( logger.debug(
@ -12642,8 +12713,14 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
_single_line(text, 80), _single_line(text, 80),
_single_line(existing_reply_preview, 120), _single_line(existing_reply_preview, 120),
) )
asyncio.create_task(self._maybe_refresh_group_episode(group_id, group_snapshot)) self._create_lifecycle_background_task(
asyncio.create_task(self._maybe_refresh_group_slang_meanings(group_id, group_snapshot)) self._maybe_refresh_group_episode(group_id, group_snapshot),
label="refresh_group_episode_existing_reply",
)
self._create_lifecycle_background_task(
self._maybe_refresh_group_slang_meanings(group_id, group_snapshot),
label="refresh_group_slang_existing_reply",
)
return return
if self._group_llm_reply_blocked(group_id): if self._group_llm_reply_blocked(group_id):
logger.debug( logger.debug(
@ -12766,14 +12843,15 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
) )
high_intensity_state = self._group_high_intensity_state(group) high_intensity_state = self._group_high_intensity_state(group)
if not resting_mention_notice: if not resting_mention_notice:
continuation = await self._group_message_is_bot_continuation( async with self._temporarily_release_data_lock():
group, continuation = await self._group_message_is_bot_continuation(
sender_id, group,
sender_name, sender_id,
scene, sender_name,
text, scene,
allow_llm=False, text,
) allow_llm=False,
)
if continuation is None: if continuation is None:
if high_intensity_state.get("active"): if high_intensity_state.get("active"):
continuation = False continuation = False
@ -13001,7 +13079,8 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
setattr(event, "private_companion_group_wakeup_state_effect", dict(wakeup_state_effect)) setattr(event, "private_companion_group_wakeup_state_effect", dict(wakeup_state_effect))
group_reference_media_with_text = False group_reference_media_with_text = False
if talking_to_bot and text: if talking_to_bot and text:
group_reference_media_with_text = await self._event_references_media_or_forward_with_text(event, text) async with self._temporarily_release_data_lock():
group_reference_media_with_text = await self._event_references_media_or_forward_with_text(event, text)
if group_reference_media_with_text: if group_reference_media_with_text:
logger.info( logger.info(
"[PrivateCompanion] 群聊引用媒体/合并消息附带文字,跳过群聊收口等待: group=%s sender=%s text=%s", "[PrivateCompanion] 群聊引用媒体/合并消息附带文字,跳过群聊收口等待: group=%s sender=%s text=%s",
@ -13044,13 +13123,14 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
event.stop_event() event.stop_event()
return return
if talking_to_bot and not high_intensity_merge_active and not group_reference_media_with_text: if talking_to_bot and not high_intensity_merge_active and not group_reference_media_with_text:
air_guard = await self._group_air_reply_guard_decision( async with self._temporarily_release_data_lock():
group, air_guard = await self._group_air_reply_guard_decision(
sender_id=sender_id, group,
sender_name=sender_name, sender_id=sender_id,
text=text, sender_name=sender_name,
scene=scene, text=text,
) scene=scene,
)
if isinstance(air_guard, dict) and air_guard.get("block"): if isinstance(air_guard, dict) and air_guard.get("block"):
self._capture_group_observation_once( self._capture_group_observation_once(
group, group,
@ -13091,14 +13171,15 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
text=text, text=text,
now=_now_ts(), now=_now_ts(),
) )
group_smart_wait = await self._smart_message_debounce_wait_seconds_for_event( async with self._temporarily_release_data_lock():
event, group_smart_wait = await self._smart_message_debounce_wait_seconds_for_event(
key=group_buffer_key, event,
text=text, key=group_buffer_key,
sender_id=sender_id, text=text,
sender_name=sender_name, sender_id=sender_id,
private_chat=False, sender_name=sender_name,
) private_chat=False,
)
group_smart_result = getattr(event, "private_companion_smart_message_debounce_result", None) group_smart_result = getattr(event, "private_companion_smart_message_debounce_result", None)
group_smart_decision = str(group_smart_result.get("decision") or "") if isinstance(group_smart_result, dict) else "" group_smart_decision = str(group_smart_result.get("decision") or "") if isinstance(group_smart_result, dict) else ""
group_smart_handled = group_smart_decision in {"complete", "incomplete"} group_smart_handled = group_smart_decision in {"complete", "incomplete"}
@ -13159,12 +13240,24 @@ wakeup_type={_single_line(wakeup.get('type'), 40)} score={_single_line(wakeup.ge
event.stop_event() event.stop_event()
return return
if registration_payload and registration_payload.get("user_id"): if registration_payload and registration_payload.get("user_id"):
asyncio.create_task(self._refresh_worldbook_self_registration_impression(registration_payload)) self._create_lifecycle_background_task(
self._refresh_worldbook_self_registration_impression(registration_payload),
label="refresh_worldbook_self_registration_impression",
)
if share_scheduled: if share_scheduled:
asyncio.create_task(self._kick_proactive_loop_once()) self._create_lifecycle_background_task(
self._kick_proactive_loop_once(),
label="kick_proactive_loop_group",
)
if not group_snapshot_high_intensity.get("active"): if not group_snapshot_high_intensity.get("active"):
asyncio.create_task(self._maybe_refresh_group_episode(group_id, group_snapshot)) self._create_lifecycle_background_task(
asyncio.create_task(self._maybe_refresh_group_slang_meanings(group_id, group_snapshot)) self._maybe_refresh_group_episode(group_id, group_snapshot),
label="refresh_group_episode",
)
self._create_lifecycle_background_task(
self._maybe_refresh_group_slang_meanings(group_id, group_snapshot),
label="refresh_group_slang",
)
await self._maybe_group_interject(event, group_snapshot, text) await self._maybe_group_interject(event, group_snapshot, text)
else: else:
logger.info( logger.info(

查看文件

@ -0,0 +1,56 @@
from __future__ import annotations
import asyncio
from pathlib import Path
import sys
import types
import unittest
ROOT = Path(__file__).resolve().parents[1]
PACKAGE_NAME = "c7_companion_test_package"
package = types.ModuleType(PACKAGE_NAME)
package.__path__ = [str(ROOT)]
sys.modules.setdefault(PACKAGE_NAME, package)
from c7_companion_test_package.bot_personal_outbox import BotPersonalOutbox
class OutboxLifecycleTests(unittest.TestCase):
def test_async_save_uses_lifecycle_callback_when_provided(self):
labels: list[str] = []
def create_task(operation, label):
labels.append(label)
operation.close()
return None
async def save():
return None
outbox = BotPersonalOutbox({}, save=save, background_task=create_task)
asyncio.run(asyncio.to_thread(outbox._persist))
self.assertEqual(["bot_personal_outbox_save"], labels)
class CompanionConcurrencyStaticTests(unittest.TestCase):
def test_data_lock_external_awaits_use_temporary_release_context(self):
source = (ROOT / "main.py").read_text(encoding="utf-8")
self.assertIn("async def _temporarily_release_data_lock", source)
self.assertGreaterEqual(source.count("async with self._temporarily_release_data_lock()"), 12)
self.assertIn('label="refresh_group_episode"', source)
self.assertIn('label="refresh_persona_relationship_inbound"', source)
def test_untracked_main_background_tasks_are_not_left_in_message_paths(self):
source = (ROOT / "main.py").read_text(encoding="utf-8")
for marker in (
"asyncio.create_task(self._refine_inbound_emotion_with_model",
"asyncio.create_task(self._maybe_refresh_companion_memory",
"asyncio.create_task(self._maybe_refresh_group_episode",
"asyncio.create_task(self._kick_proactive_loop_once",
):
self.assertNotIn(marker, source)
if __name__ == "__main__":
unittest.main()