Files
life-echo/api/tests/test_recompose_retry_policy.py
Kevin 07c6478742 feat(api): 访谈路径轻量门控、Memoir Phase1 批处理与叙事/记忆管线加固
- 新增 utterance_substance:短时/应答/元话语可跳过记忆检索、阶段 LLM 与资料抽取 LLM;可配置
- 输入归一化:LLM 模式默认仅语音/ASR;配置项写入 .env.example
- Memoir Phase1:可选 batch LLM 一次性抽取+分类(失败回退逐段);Extraction 空槽位时阶段与 current_stage 对齐,prompt 约束收紧
- 叙事与忠实度:narrative_safety、证据重叠/场合锚点、标题 slots 与履历短语 grounded;fidelity 解析失败 fail-open 可配置
- 章节管线:锁 TTL 上调、锁竞争 Celery 重试、Phase2 immediate singleflight 等;story_pipeline_sync / chapter_compose / memoir_tasks 联动
- Memory:compaction / repo / summarizer / evidence 小修;事实 FTS 未命中是否回退最近事实可配置
- 新增 memoir_pipeline_trace;补充 memoir_reliability 文档与多项回归/门控测试
2026-04-03 10:12:59 +08:00

88 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""recompose_chapter锁竞争时 retry可配置"""
from __future__ import annotations
from contextlib import contextmanager
from unittest.mock import MagicMock, patch
import pytest
from celery.exceptions import Retry
from app.core.config import settings
from app.tasks.chapter_compose_tasks import recompose_chapter
class _FakeSyncDb:
"""模拟 `with get_sync_db() as session`。"""
def __init__(self, session: MagicMock) -> None:
self._session = session
def __enter__(self) -> MagicMock:
return self._session
def __exit__(self, *exc: object) -> bool:
return False
def test_recompose_retries_when_lock_busy_and_flag_on(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
settings, "memoir_recompose_retry_on_lock_contention", True, raising=False
)
session = MagicMock()
ch = MagicMock()
ch.markdown_compose_dirty = True
ch.user_id = "user-1"
ch.category = "childhood"
session.get.return_value = ch
with (
patch(
"app.tasks.chapter_compose_tasks.get_sync_db",
return_value=_FakeSyncDb(session),
),
patch(
"app.tasks.chapter_compose_tasks.acquire_chapter_pipeline_lock",
return_value=None,
),
):
with patch.object(
recompose_chapter, "retry", side_effect=Retry("lock_busy")
) as mock_retry:
with pytest.raises(Retry):
recompose_chapter.run("chapter-1")
mock_retry.assert_called_once()
_, kwargs = mock_retry.call_args
assert "countdown" in kwargs
def test_recompose_skips_when_lock_busy_and_flag_off(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
settings, "memoir_recompose_retry_on_lock_contention", False, raising=False
)
session = MagicMock()
ch = MagicMock()
ch.markdown_compose_dirty = True
ch.user_id = "user-1"
ch.category = "childhood"
session.get.return_value = ch
with (
patch(
"app.tasks.chapter_compose_tasks.get_sync_db",
return_value=_FakeSyncDb(session),
),
patch(
"app.tasks.chapter_compose_tasks.acquire_chapter_pipeline_lock",
return_value=None,
),
):
with patch.object(recompose_chapter, "retry") as mock_retry:
out = recompose_chapter.run("chapter-1")
assert out == {"status": "skip_lock_contention"}
mock_retry.assert_not_called()