fix(engine): prompt plaza edits not taking effect in generation (#146)#170
Conversation
…minglinyi#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
📝 WalkthroughWalkthroughThis PR adds PromptRegistry cache invalidation to keep node updates immediately visible across infrastructure and API layers. Logging for template fallbacks escalates to WARNING level with explicit fallback messages. A cache invalidation helper is introduced and integrated into node update and API cache operations. ChangesCache invalidation and logging improvements
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
infrastructure/ai/prompt_manager.py (1)
1091-1100: ⚡ Quick winMake registry invalidation explicit instead of heuristic key-shape detection.
Line 1097 relies on string shape to infer key type. UUID-style IDs with hyphens can be treated as node keys, which risks missing the real cache entry. Prefer explicit invalidation by resolved
node_key(or an explicitby_keyflag) to avoid stale cache edge cases.Suggested change
- def _invalidate_registry_cache(node_key_or_id: str) -> None: - """使 PromptRegistry 缓存失效(node_key 或 node_id 均可)。""" + def _invalidate_registry_cache(node_key: Optional[str] = None) -> None: + """使 PromptRegistry 缓存失效。传 node_key 时做定向失效,否则全量失效。""" 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) + if node_key: + registry.invalidate_cache(node_key) else: registry.invalidate_cache() except Exception: pass # Registry 不可用时静默跳过- self._invalidate_registry_cache(node_id) + updated_node = self.get_node(node_id, by_key=False) + self._invalidate_registry_cache(updated_node.node_key if updated_node else None) - return self.get_node(node_id, by_key=False) + return updated_node🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@infrastructure/ai/prompt_manager.py` around lines 1091 - 1100, The _invalidate_registry_cache function currently heuristically treats node_key_or_id as a key when it contains a '-' and is short, which can misclassify UUID-style IDs; change it to resolve the canonical node_key (or accept an explicit by_key boolean) and call get_prompt_registry() and registry.invalidate_cache with the resolved node_key only when you truly have a key, otherwise call registry.invalidate_cache() to clear all; update the function signature (e.g., _invalidate_registry_cache(node_key_or_id: str, by_key: Optional[bool]=None)) or perform a lookup to convert a node_id to node_key before calling registry.invalidate_cache to ensure deterministic invalidation.interfaces/api/v1/workbench/llm_control.py (1)
275-276: ⚡ Quick winKeep non-blocking behavior, but emit a debug log on invalidation failure.
Line 275-276 fully suppresses failures, which makes stale-cache incidents harder to diagnose in production.
Suggested change
- except Exception: - pass # Registry 不可用时静默跳过,不影响 API 层功能 + except Exception as exc: + logger.debug("PromptRegistry cache invalidation skipped: %s", exc, exc_info=True)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@interfaces/api/v1/workbench/llm_control.py` around lines 275 - 276, The except Exception: pass in interfaces/api/v1/workbench/llm_control.py should remain non-blocking but emit a debug-level log when registry invalidation fails; replace the pass with a debug log call (e.g., logger.debug("Registry invalidation failed, continuing without aborting", exc_info=True)) using the existing logger variable (or process_logger) so the exception and stack trace are recorded for diagnosis while keeping the API behavior unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@infrastructure/ai/prompt_manager.py`:
- Around line 1091-1100: The _invalidate_registry_cache function currently
heuristically treats node_key_or_id as a key when it contains a '-' and is
short, which can misclassify UUID-style IDs; change it to resolve the canonical
node_key (or accept an explicit by_key boolean) and call get_prompt_registry()
and registry.invalidate_cache with the resolved node_key only when you truly
have a key, otherwise call registry.invalidate_cache() to clear all; update the
function signature (e.g., _invalidate_registry_cache(node_key_or_id: str,
by_key: Optional[bool]=None)) or perform a lookup to convert a node_id to
node_key before calling registry.invalidate_cache to ensure deterministic
invalidation.
In `@interfaces/api/v1/workbench/llm_control.py`:
- Around line 275-276: The except Exception: pass in
interfaces/api/v1/workbench/llm_control.py should remain non-blocking but emit a
debug-level log when registry invalidation fails; replace the pass with a debug
log call (e.g., logger.debug("Registry invalidation failed, continuing without
aborting", exc_info=True)) using the existing logger variable (or
process_logger) so the exception and stack trace are recorded for diagnosis
while keeping the API behavior unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 098e8fd3-c16d-484b-8cf8-f091b94c2f15
📒 Files selected for processing (3)
application/workflows/auto_novel_generation_workflow.pyinfrastructure/ai/prompt_manager.pyinterfaces/api/v1/workbench/llm_control.py
变更类型
fixBug 修复变更说明
修复提示词广场编辑后不生效的问题。用户在提示词广场修改提示词并保存后,章节生成仍使用旧版本提示词。
根因分析
PromptRegistry对提示词节点有 300 秒(5 分钟)的内存缓存。用户编辑提示词后,API 层的_plaza_cache会被清除,但PromptRegistry的缓存未同步失效,导致生成管线在 5 分钟内仍读取旧版 DB 快照。修复内容
_invalidate_plaza_cache()扩展 — 所有提示词写操作(编辑、回滚、新建、删除、导入)现在同时清除PromptRegistry缓存,确保生成管线立即读取到最新版本。PromptManager.update_node()补充失效 — 数据库层更新节点后主动调用_invalidate_registry_cache(),作为 API 层失效的兜底。降级日志级别提升 —
_get_workflow_system_template()和_get_workflow_user_template()的硬编码回退日志从DEBUG提升至WARNING,便于排查管道是否意外降级到硬编码模板。架构影响
interfaces/infrastructure/application测试
_invalidate_registry_cache()包含异常捕获,PromptRegistry不可用时静默跳过Summary by CodeRabbit
Bug Fixes
Chores