feat(api): 回忆录管线简化、路由延迟池与相关加固

- Phase1/2:移除 MemoirOrchestrator.run 与 process_memoir_segments 别名;文档改为 process_memoir_phase1。
- 槽位校验集中到 stage_constants(filter_stage_slots),批处理与顺序路径及 state_service 写库一致。
- StoryRoute:no_llm/parse_error/invalid_target 保守 new_story;短篇护栏不覆盖这些 fallback。
- Phase2 低置信单路径可选延迟(StoryPipelineResult.deferred):不写 Chapter/Story,Segment 记录 defer 元数据,冷却内不重复消费;上限后停自动重试,Phase1 同类目新段唤醒池内段。
- Alembic 0017:segments 表 narrative_defer_* 列。
- ProfileAgent:经 LlmGateway/注入 Provider 统一聊天与 JSON,新增测试。
- ImagePromptOrchestrator:LLM 初始化失败可依配置降级或硬失败;补充策略测试。
- 配套单测与 README/本地开发文档表述更新。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Kevin
2026-05-06 13:18:02 +08:00
parent 3234396254
commit 59d4b19d7d
24 changed files with 1182 additions and 183 deletions

View File

@@ -11,6 +11,7 @@ import re
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field
from typing import Any
from sqlalchemy import func, select
@@ -20,6 +21,7 @@ from app.agents.memoir.narrative_agent import NarrativeAgent
from app.agents.memoir.prompts import format_narrative_user_content
from app.agents.memoir.story_route_agent import (
APPEND_FIRST_CHAPTER_CATEGORIES,
FALLBACK_NEW_STORY_REASONS,
PLAN_BATCH_MAX_SEGMENTS,
StoryBatchPlan,
StoryRouteAgent,
@@ -70,6 +72,23 @@ from app.features.story.sync_write import (
logger = get_logger(__name__)
@dataclass
class StoryPipelineResult:
"""Phase2 故事管线结果。
- 正常写入:``deferred=False````chapter`` 非空。
- 低置信延迟:``deferred=True````chapter`` 为 None调用方应把 ``defer_segment_ids``
标记为延迟态,不要置 ``narrated/processed``,也不要触发后置任务。
"""
chapter: Chapter | None
needs_cover: bool
dispatch_ids: set[str]
deferred: bool = False
defer_reason: str | None = None
defer_segment_ids: list[str] = field(default_factory=list)
def _dialogue_lineage_dict_for_segment_ids(
category_segments: list,
segment_ids: list[str],
@@ -662,6 +681,7 @@ def _resolve_append_target(
route_decision == "new_story"
and chapter_category in APPEND_FIRST_CHAPTER_CATEGORIES
and candidate_stories
and decision_source not in FALLBACK_NEW_STORY_REASONS
and len(oral_norm)
<= int(settings.memoir_story_route_append_guardrail_oral_chars)
):
@@ -952,9 +972,10 @@ def run_story_pipeline_for_category_batch(
memoir_correlation_id: str | None = None,
llm_fast: Any | None = None,
memory_evidence: dict | None = None,
) -> tuple[Chapter | None, bool, set[str]]:
"""
返回 (chapter, needs_cover_enqueue, story_ids_to_dispatch_after_commit)。
) -> StoryPipelineResult:
"""运行某 chapter_category 的 Phase2 写入管线。
返回 :class:`StoryPipelineResult`。低置信路由会被延迟而不创建 Story/Chapter。
"""
pipeline_phase_timings: dict[str, float] = {}
narrative_agent = NarrativeAgent()
@@ -1074,8 +1095,46 @@ def run_story_pipeline_for_category_batch(
valid_story_ids=valid_ids,
story_meta=story_meta,
)
single_route: Any = None
if plan is None:
single_route = route_agent.decide(
chapter_category=chapter_category,
chapter_title=title,
batch_transcript=route_transcript,
candidate_stories=candidates,
llm=llm_route,
valid_story_ids=valid_ids,
story_meta=story_meta,
)
pipeline_phase_timings["route"] = time.perf_counter() - _t0
if (
plan is None
and single_route is not None
and single_route.reason in FALLBACK_NEW_STORY_REASONS
and bool(settings.memoir_route_defer_enabled)
):
defer_ids = [str(s.id) for s in category_segments]
logger.info(
"event=memoir_pipeline_route_deferred memoir_correlation_id={} user_id={} "
"chapter_category={} segment_count={} reason={} "
"msg=Phase2 路由低置信,本批 segment 进入延迟池",
memoir_correlation_id or "",
user_id,
chapter_category,
len(defer_ids),
single_route.reason,
)
return StoryPipelineResult(
chapter=None,
needs_cover=False,
dispatch_ids=set(),
deferred=True,
defer_reason=str(single_route.reason),
defer_segment_ids=defer_ids,
)
chapter = _ensure_chapter_record(
session,
user_id=user_id,
@@ -1110,17 +1169,12 @@ def run_story_pipeline_for_category_batch(
fidelity_llm=llm_fidelity,
)
else:
route = route_agent.decide(
chapter_category=chapter_category,
chapter_title=title,
batch_transcript=route_transcript,
candidate_stories=candidates,
llm=llm_route,
valid_story_ids=valid_ids,
story_meta=story_meta,
route = single_route
decision_source = (
route.reason
if route.reason in FALLBACK_NEW_STORY_REASONS
else ("fallback_no_llm" if not llm_route else "single_decide")
)
decision_source = "fallback_no_llm" if not llm else "single_decide"
target_story_id, existing_for_narrative, decision_source = (
_resolve_append_target(
session,
@@ -1191,4 +1245,8 @@ def run_story_pipeline_for_category_batch(
timing_parts,
)
return chapter, needs_cover, dispatch_ids
return StoryPipelineResult(
chapter=chapter,
needs_cover=needs_cover,
dispatch_ids=dispatch_ids,
)