From 2accec248827581b741a12396273c4d1e39bdb37 Mon Sep 17 00:00:00 2001 From: Cowork 3P Date: Fri, 22 May 2026 14:31:02 +0000 Subject: [PATCH] fix(engine): prompt plaza edits not taking effect in generation (#146) Root cause: PromptRegistry caches prompt templates with a 300s TTL. After a user edits a prompt in the plaza, the API layer cache is cleared but the PromptRegistry cache is not invalidated, so the generation pipeline still serves stale templates for up to 5 minutes. Changes: 1. interfaces/api/v1/workbench/llm_control.py - _invalidate_plaza_cache() now also calls PromptRegistry.invalidate_cache() so all editing endpoints (update_node, rollback_node, create_node, delete_node, import_prompts, create_template) trigger full cache invalidation 2. infrastructure/ai/prompt_manager.py - update_node() now calls _invalidate_registry_cache() after commit - New _invalidate_registry_cache() static helper method 3. application/workflows/auto_novel_generation_workflow.py - Upgrade fallback logging from DEBUG to WARNING for both _get_workflow_system_template and _get_workflow_user_template - Makes it easier to diagnose when the pipeline is falling back to hardcoded templates instead of reading from the DB --- .../auto_novel_generation_workflow.py | 13 +++++++++++-- infrastructure/ai/prompt_manager.py | 18 ++++++++++++++++++ interfaces/api/v1/workbench/llm_control.py | 9 +++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/application/workflows/auto_novel_generation_workflow.py b/application/workflows/auto_novel_generation_workflow.py index 82c0fb97..ab6f11f7 100644 --- a/application/workflows/auto_novel_generation_workflow.py +++ b/application/workflows/auto_novel_generation_workflow.py @@ -1741,7 +1741,12 @@ def _get_workflow_system_template(self) -> str: "PromptRegistry 不可用 (node_key=%s): %s", _WORKFLOW_CHAPTER_GEN_NODE_KEY, exc ) - logger.debug("CPMS: 使用硬编码回退 system 模板") + # ★ 修复 #146:将降级日志提升至 WARNING,便于排查提示词广场编辑不生效的问题 + logger.warning( + "CPMS: 使用硬编码回退 system 模板 (node_key=%s)。" + "可能原因:PromptRegistry 不可用,或数据库中该节点未正确播种。", + _WORKFLOW_CHAPTER_GEN_NODE_KEY, + ) return _FALLBACK_SYSTEM_TEMPLATE def _get_workflow_user_template(self) -> str: @@ -1768,7 +1773,11 @@ def _get_workflow_user_template(self) -> str: "PromptRegistry 不可用 (node_key=%s): %s", _WORKFLOW_CHAPTER_GEN_NODE_KEY, exc ) - logger.debug("CPMS: 使用硬编码回退 user_template") + logger.warning( + "CPMS: 使用硬编码回退 user_template (node_key=%s)。" + "可能原因:PromptRegistry 不可用,或数据库中该节点未正确播种。", + _WORKFLOW_CHAPTER_GEN_NODE_KEY, + ) return _FALLBACK_USER_TEMPLATE async def _extract_chapter_state(self, content: str, chapter_number: int) -> ChapterState: diff --git a/infrastructure/ai/prompt_manager.py b/infrastructure/ai/prompt_manager.py index d949a26f..c83edf2b 100644 --- a/infrastructure/ai/prompt_manager.py +++ b/infrastructure/ai/prompt_manager.py @@ -905,6 +905,10 @@ def update_node( db.execute(sql, params) db.commit() + # ★ 修复 #146:节点更新后使 PromptRegistry 缓存失效, + # 确保生成管线能立即读取到最新模板。 + self._invalidate_registry_cache(node_id) + return self.get_node(node_id, by_key=False) def rollback_node(self, node_id: str, @@ -1083,6 +1087,20 @@ def _attach_active_versions(self, nodes: List[NodeInfo]) -> None: if node.active_version_id and node.active_version_id in ver_map: node.set_active_version(ver_map[node.active_version_id]) + @staticmethod + def _invalidate_registry_cache(node_key_or_id: str) -> None: + """使 PromptRegistry 缓存失效(node_key 或 node_id 均可)。""" + try: + from infrastructure.ai.prompt_registry import get_prompt_registry + registry = get_prompt_registry() + # 尝试按 node_key 失效;如果是 node_id 则清除全部缓存(更安全) + if '-' in node_key_or_id and len(node_key_or_id) < 50: + registry.invalidate_cache(node_key_or_id) + else: + registry.invalidate_cache() + except Exception: + pass # Registry 不可用时静默跳过 + @staticmethod def _get_current_version(db, node_id: str) -> Optional[VersionInfo]: """获取节点当前激活版本。""" diff --git a/interfaces/api/v1/workbench/llm_control.py b/interfaces/api/v1/workbench/llm_control.py index e3db1ca4..7c56a3d3 100644 --- a/interfaces/api/v1/workbench/llm_control.py +++ b/interfaces/api/v1/workbench/llm_control.py @@ -266,6 +266,15 @@ def _invalidate_plaza_cache() -> None: _plaza_cache = {} _plaza_cache_ts = 0.0 + # ★ 修复 #146:同时清除 PromptRegistry 缓存(TTL=300s),否则 + # 生成管线在 5 分钟内仍会读取旧版 DB 快照,导致提示词广场编辑不生效。 + try: + from infrastructure.ai.prompt_registry import get_prompt_registry + registry = get_prompt_registry() + registry.invalidate_cache() + except Exception: + pass # Registry 不可用时静默跳过,不影响 API 层功能 + @router.get('/prompts/plaza-init') async def plaza_init() -> Dict[str, Any]: