sync update from LAN repo (source master-nj @ 067eb808)

This commit is contained in:
gozeros committed 2026-11-10 14:04:09 +08:00
1 parent 1244fb1f2f
commit 1d5dd8fc90
182 files changed
+18973 -769

No files matched your search

@@ -57,6 +57,9 @@ class StrictWikiEmbeddingClient:
self._local_started_at: str | None = None
self._local_ready_at: str | None = None
self._local_warmup_task: asyncio.Task[None] | None = None
self._lifecycle_condition = asyncio.Condition()
self._active_uses = 0
self._retired = False
@property
def dimensions(self) -> int | None:
@@ -64,7 +67,21 @@ class StrictWikiEmbeddingClient:
@property
def fingerprint(self) -> str:
raw = "\0".join((self.config.provider.strip().lower(), self.config.base_url.rstrip("/").lower(), self.config.model.strip(), str(self._resolved_dimensions or "auto")))
parts = [
self.config.provider.strip().lower(),
self.config.base_url.rstrip("/").lower(),
self.config.model.strip(),
str(self._resolved_dimensions or "auto"),
]
if self.config.provider == "local":
model_path = self._local_model_path()
parts.extend(
(
str(model_path) if model_path is not None else "",
self.config.local_model_revision,
)
)
raw = "\0".join(parts)
return hashlib.sha256(raw.encode("utf-8")).hexdigest()
async def close(self) -> None:
@@ -73,6 +90,15 @@ class StrictWikiEmbeddingClient:
await self._client.aclose()
self._client = None
async def retire(self) -> None:
"""Reject future work, drain active calls, then release model resources."""
async with self._lifecycle_condition:
self._retired = True
async with self._lifecycle_condition:
await self._lifecycle_condition.wait_for(lambda: self._active_uses == 0)
await self.close()
async def reset_local_encoder(self, *, model_path: str | None = None) -> None:
"""Stop only the embedded worker and optionally switch its model path.
@@ -82,27 +108,80 @@ class StrictWikiEmbeddingClient:
"""
warmup = self._local_warmup_task
if warmup is not None and not warmup.done():
warmup.cancel()
# Local inference runs in a worker thread. Cancelling the asyncio
# wrapper does not stop that thread and could let it publish a new
# process/model after cleanup. Drain it before touching resources.
await asyncio.gather(warmup, return_exceptions=True)
self._local_warmup_task = None
process = self._local_process
self._local_process = None
if process is not None:
await asyncio.to_thread(self._stop_local_process, process)
self._local_model = None
self._local_state = "idle"
self._local_error = None
self._local_started_at = None
self._local_ready_at = None
if model_path is not None:
self.config = self.config.model_copy(update={"local_model_path": model_path})
# Wait for a non-warmup embedding batch to finish before replacing or
# stopping the local worker. This prevents an admin upload from
# breaking a concurrent vectorization request halfway through IPC.
async with self._local_lock:
process = self._local_process
self._local_process = None
if process is not None:
await self._run_thread_drained(self._stop_local_process, process)
self._local_model = None
self._local_state = "idle"
self._local_error = None
self._local_started_at = None
self._local_ready_at = None
if model_path is not None:
self.config = self.config.model_copy(update={"local_model_path": model_path})
async def _get_client(self) -> httpx.AsyncClient:
if self._client is None:
self._client = httpx.AsyncClient(timeout=self.config.timeout_seconds)
return self._client
@staticmethod
async def _run_thread_drained(function, *args):
"""Do not release lifecycle locks while cancelled thread work still runs."""
worker = asyncio.create_task(asyncio.to_thread(function, *args))
cancelled = False
worker_error: BaseException | None = None
result = None
while not worker.done():
try:
result = await asyncio.shield(worker)
except asyncio.CancelledError:
# A caller can be cancelled more than once during shutdown.
# Swallow every cancellation until the non-cancellable thread
# has really stopped, then restore cancellation semantics.
cancelled = True
except BaseException as exc:
worker_error = exc
break
if worker.done() and worker_error is None:
try:
result = worker.result()
except BaseException as exc:
worker_error = exc
if cancelled:
raise asyncio.CancelledError from None
if worker_error is not None:
raise worker_error
return result
async def embed_texts(self, texts: list[str]) -> list[list[float]]:
async with self._lifecycle_condition:
if self._retired:
raise WikiEmbeddingError(
"Wiki embedding runtime was replaced; retry with the active provider",
category="runtime_retired",
retryable=True,
)
self._active_uses += 1
try:
return await self._embed_texts_active(texts)
finally:
async with self._lifecycle_condition:
self._active_uses -= 1
if self._active_uses == 0:
self._lifecycle_condition.notify_all()
async def _embed_texts_active(self, texts: list[str]) -> list[list[float]]:
if self.config.provider == "local":
if not texts:
return []
@@ -110,7 +189,10 @@ class StrictWikiEmbeddingClient:
self._local_state = "starting"
self._local_started_at = self._local_started_at or datetime.now(UTC).isoformat()
try:
values = await asyncio.to_thread(self._embed_local, texts)
# `to_thread` keeps running after its awaiting task is
# cancelled. Keep the lifecycle lease and local lock until
# the real inference call has stopped.
values = await self._run_thread_drained(self._embed_local, texts)
except Exception as exc:
self._local_state = "failed"
self._local_error = " ".join(str(exc).split())[:1000] or type(exc).__name__
@@ -452,6 +534,8 @@ class StrictWikiEmbeddingClient:
def start_local_encoder(self) -> dict[str, object]:
"""Warm the embedded CPU encoder without coupling it to app startup."""
if self._retired:
raise ValueError("当前本地编码器运行时已切换,请刷新状态后重试")
if self.config.provider != "local":
raise ValueError("当前 embedding provider 不是 local")
if not self.config.enabled:
@@ -11,7 +11,7 @@ from uuid import uuid4
from deerflow.config.llmwiki_config import LocalWikiIndexConfig
from deerflow.integrations.weknora.client import WeKnoraClient
from deerflow.integrations.weknora.local_index.chunking import chunk_wiki_page
from deerflow.integrations.weknora.local_index.embedding import StrictWikiEmbeddingClient
from deerflow.integrations.weknora.local_index.embedding import StrictWikiEmbeddingClient, WikiEmbeddingError
from deerflow.integrations.weknora.local_index.normalize import normalize_wiki_page
from deerflow.integrations.weknora.local_index.vector_cache import WikiVectorCache
from deerflow.integrations.weknora.local_index.vector_codec import encode_vector
@@ -137,6 +137,8 @@ class WikiSyncService:
except Exception as exc:
if isinstance(exc, WikiSyncLeaseLost):
raise
if isinstance(exc, WikiEmbeddingError) and exc.category == "runtime_retired":
raise
failed += 1
try:
fallback = normalize_wiki_page(mapping_id, summary)