sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -7,6 +7,7 @@ from typing import Any
import pytest
import app.gateway.llmwiki_index_scheduler as scheduler
from app.gateway.llmwiki_index_scheduler import (
_mapping_is_due,
schedule_local_wiki_mutation_sync,
@@ -51,6 +52,31 @@ class _SyncService:
return {"status": "completed"}
class _BlockingBackoffSyncService(_SyncService):
def __init__(self) -> None:
super().__init__([False, False])
self.started = asyncio.Event()
self.release = asyncio.Event()
async def sync_mapping(
self,
mapping: dict[str, Any],
*,
force: bool,
) -> dict[str, Any]:
assert force is False
self.sync_calls += 1
self.seen_mappings.append(dict(mapping))
if self.sync_calls == 1:
self.started.set()
await self.release.wait()
return {
"status": "provider_backoff",
"failure_scope": "embedding_provider",
"retry_after_seconds": 60,
}
def _app(service: _SyncService) -> SimpleNamespace:
return SimpleNamespace(state=SimpleNamespace(llmwiki_sync_service=service))
@@ -73,6 +99,33 @@ async def test_mutation_sync_reserves_scan_until_remote_processing_finishes() ->
assert service.sync_calls == 2
@pytest.mark.asyncio
async def test_mutation_sync_timeout_starts_from_latest_processing_observation(
monkeypatch: pytest.MonkeyPatch,
) -> None:
service = _SyncService([True, False, False, False])
clock = [0.0, 9.0, 11.0, 15.0, 16.0, 17.0]
monkeypatch.setattr(scheduler, "_MUTATION_SYNC_MAX_SECONDS", 10.0)
monkeypatch.setattr(
scheduler,
"monotonic",
lambda: clock.pop(0) if clock else 17.0,
)
task = schedule_local_wiki_mutation_sync(
_app(service),
{"id": "mapping-kb", "weknora_id": "remote-kb"},
delays=(0.0,),
)
assert task is not None
await asyncio.wait_for(task, timeout=1)
# The second poll happens after the original upload-based deadline. A
# confirmed in-progress observation must refresh that deadline so the
# completed snapshot is still synchronized.
assert service.sync_calls == 2
@pytest.mark.asyncio
async def test_mutation_sync_stops_after_a_quiet_scan_observes_delta() -> None:
service = _SyncService(
@@ -129,6 +182,56 @@ async def test_mutation_sync_retries_service_processing_fence_then_confirms() ->
assert service.sync_calls == 3
@pytest.mark.asyncio
async def test_mutation_sync_stops_when_embedding_provider_breaker_is_open() -> None:
service = _SyncService(
[False],
results=[
{
"status": "provider_backoff",
"failure_scope": "embedding_provider",
"retry_after_seconds": 60,
}
],
)
task = schedule_local_wiki_mutation_sync(
_app(service),
{"id": "mapping-kb", "weknora_id": "remote-kb"},
delays=(0.001, 0.001, 0.001),
)
assert task is not None
await asyncio.wait_for(task, timeout=1)
assert service.sync_calls == 1
@pytest.mark.asyncio
async def test_new_mutation_during_provider_backoff_is_consumed_and_cleaned_up() -> None:
service = _BlockingBackoffSyncService()
app = _app(service)
first = schedule_local_wiki_mutation_sync(
app,
{"id": "mapping-kb", "weknora_id": "remote-kb", "name": "old"},
delays=(0,),
)
assert first is not None
await asyncio.wait_for(service.started.wait(), timeout=1)
second = schedule_local_wiki_mutation_sync(
app,
{"id": "mapping-kb", "weknora_id": "remote-kb", "name": "new"},
delays=(0,),
)
assert second is first
service.release.set()
await asyncio.wait_for(first, timeout=1)
assert service.sync_calls == 2
assert [mapping["name"] for mapping in service.seen_mappings] == ["old", "new"]
assert app.state.llmwiki_mutation_sync_tasks == {}
assert app.state.llmwiki_mutation_sync_plans == {}
@pytest.mark.asyncio
async def test_new_generation_wakes_backoff_and_uses_latest_mapping() -> None:
service = _SyncService(