fix(rag): 移除 service 未用参数 + 引擎构建加锁 + 修正文档措辞

This commit is contained in:
lhl
2026-08-30 00:31:18 +08:00
parent f77fab707d
commit 9a02fcd00f
2 changed files with 8 additions and 10 deletions
+1 -1
View File
@@ -1930,6 +1930,6 @@ Document(注入后 Word 文档)
- **scope = session_id**:每个会话的既有系统源码独立索引到 `RagStore` 的同一 scope,互不串扰。
- **上传即索引(D1**`GenesisService.upload_file` 在 `file_type == "existing_system"` 且 `self.rag is not None` 时,解压完成后立即调用 `self.rag.index_dir(session_id, path)`;索引异常仅记录日志(`_LOGGER.warning`)不阻断上传。
- **`use_rag` 默认关闭**`GenesisService` 构造参数 `use_rag` 默认 `False``rag=None` 表示不启用(向后兼容)。`run_impact(session_id, use_rag=None)` 中 `eff = self.use_rag if use_rag is None else use_rag`;仅当 `eff 且 self.rag is not None` 时走 LLM+RAG 路径,否则走原确定性 `ImpactAgent().run(...)` 路径(行为不变)。
- **异步链路**`run_impact` 为 `async def`RAG 路径 `await ImpactAgent(engine=..., rag=..., use_rag=True).run_impact(...)``app.start_impact` 端点同步改为 `async def` 并 `await service.run_impact(sid, use_rag=use_rag)``chat/agent.py` 调用处以 `asyncio.run(...)` 包裹以兼容同步消息处理。
- **异步链路**`run_impact` 为 `async def`RAG 路径 `await ImpactAgent(engine=..., rag=..., use_rag=True).run_impact(...)``app.start_impact` 端点改为 `async def` 并 `await service.run_impact(sid, use_rag=use_rag)``chat/agent.py` 调用处以 `asyncio.run(...)` 包裹以兼容同步消息处理。
- **线程安全(D2**`RagStore` 构造使用 `sqlite3.connect(db_path, check_same_thread=False)` 并加 `threading.Lock`,读写均加锁串行化,适配 Web 服务端 worker 线程复用连接。
- **向后兼容**`use_rag=False` 时 prompt 不含 RAG 小节标题(`_RAG_CONTEXT_TITLE`),影响报告为确定性 `impact-report.json`,不调用 LLM。
+7 -9
View File
@@ -15,6 +15,7 @@ import html
import json
import logging
import shutil
import threading
import zipfile
from pathlib import Path
@@ -62,8 +63,6 @@ class GenesisService:
projects: "ProjectsStore | None" = None,
rag: "ImpactRAG | None" = None,
use_rag: bool = False,
rag_db_path: str | None = None,
embedder=None,
) -> None:
self.store = store
self.data_root = Path(data_root)
@@ -74,11 +73,8 @@ class GenesisService:
# RAGrag 为 None 表示不启用(向后兼容默认关闭)
self.rag = rag
self.use_rag = use_rag
self.rag_db_path = rag_db_path
if embedder is None:
from genesis.rag.embeddings import get_embedder
embedder = get_embedder("fake")
self.embedder = embedder
# 引擎构建锁:避免 worker 线程并发下共享可变属性的竞态(D2)
self._engine_lock = threading.Lock()
# ---------- 会话与文件 ----------
@@ -234,8 +230,10 @@ class GenesisService:
if eff and self.rag is not None:
# RAG 路径:LLM 驱动,检索既有系统上下文注入 prompt
if self.engine is None:
from genesis.inference.factory import build_inference_engine
self.engine = build_inference_engine()
with self._engine_lock:
if self.engine is None:
from genesis.inference.factory import build_inference_engine
self.engine = build_inference_engine()
from genesis.impact.impact_agent import ImpactAgent
requirements_text = rec.files.get("requirements", {}).get("name") or "要件定義"
result = await ImpactAgent(engine=self.engine, rag=self.rag, use_rag=True).run_impact(