sync update from LAN repo (source master @ 6e3f02a2)

This commit is contained in:
gozeros committed 2026-11-17 11:28:22 +08:00
1 parent 66a443e915
commit 6c25c89d93
431 files changed
+68511 -2240

No files matched your search

@@ -742,6 +742,29 @@ def _auto_sync_ready(app: FastAPI, config: Any) -> bool:
)
def _scheduled_auto_sync_slot(config: Any, now: datetime | None = None) -> str | None:
"""Return the current configured schedule slot, if it is due.
The settings screen expresses times in the Gateway server's local time.
Keeping this as a minute-sized slot also makes a ten-second scheduler poll
idempotent: each worker starts at most one scan for the same slot.
``None`` retains the previous interval-based behavior for hand-built
legacy config objects used by extensions and older tests.
"""
schedule = getattr(config, "auto_sync_schedule", None)
if schedule not in {"hourly", "daily"}:
return None
current = now or datetime.now().astimezone()
if current.minute != 0:
return None
if schedule == "daily" and current.hour != int(
getattr(config, "auto_sync_hour", 0)
):
return None
return f"{schedule}:{current:%Y-%m-%dT%H}:00"
async def _sync_once(
app: FastAPI,
*,
@@ -857,6 +880,7 @@ async def local_wiki_index_scheduler_loop(app: FastAPI) -> None:
last_synced_runtime_signature: str | None = None
last_full_scan_started_at: float | None = None
last_prompt_schema_upgrade_started_at: float | None = None
last_scheduled_slot: str | None = None
while True:
try:
# Rebuild checks both config.yaml mtime and persisted runtime
@@ -872,43 +896,65 @@ async def local_wiki_index_scheduler_loop(app: FastAPI) -> None:
# for remote refreshes.
last_sync_started_at = None
last_synced_runtime_signature = None
last_scheduled_slot = None
else:
if (
last_prompt_schema_upgrade_started_at is None
or now - last_prompt_schema_upgrade_started_at
>= _WIKI_CONFIG_UPGRADE_RETRY_SECONDS
):
last_prompt_schema_upgrade_started_at = now
await _upgrade_wiki_generation_configs_once(app)
interval = float(config.sync_interval_seconds)
runtime_signature = getattr(
app.state,
"llmwiki_runtime_signature",
None,
)
if (
last_sync_started_at is None
or runtime_signature != last_synced_runtime_signature
or now - last_sync_started_at >= interval
):
sync_started_at = now
force_scan = bool(
last_full_scan_started_at is None
or runtime_signature != last_synced_runtime_signature
or now - last_full_scan_started_at >= max(
_FULL_SCAN_FALLBACK_SECONDS,
interval,
)
scheduled_slot = _scheduled_auto_sync_slot(config)
if scheduled_slot is not None:
if scheduled_slot != last_scheduled_slot:
now = monotonic()
if (
last_prompt_schema_upgrade_started_at is None
or now - last_prompt_schema_upgrade_started_at
>= _WIKI_CONFIG_UPGRADE_RETRY_SECONDS
):
last_prompt_schema_upgrade_started_at = now
await _upgrade_wiki_generation_configs_once(app)
app.state.llmwiki_force_full_scan_once = True
try:
await _sync_once(app, config=config, force_scan=True)
finally:
app.state.llmwiki_force_full_scan_once = False
last_scheduled_slot = scheduled_slot
else:
# Compatibility for integrations that construct a config
# object without the new calendar-schedule fields. Normal
# Gateway runtime config always uses the setting above.
if (
last_prompt_schema_upgrade_started_at is None
or now - last_prompt_schema_upgrade_started_at
>= _WIKI_CONFIG_UPGRADE_RETRY_SECONDS
):
last_prompt_schema_upgrade_started_at = now
await _upgrade_wiki_generation_configs_once(app)
interval = float(config.sync_interval_seconds)
runtime_signature = getattr(
app.state,
"llmwiki_runtime_signature",
None,
)
app.state.llmwiki_force_full_scan_once = force_scan
try:
await _sync_once(app, config=config)
finally:
app.state.llmwiki_force_full_scan_once = False
last_sync_started_at = sync_started_at
last_synced_runtime_signature = runtime_signature
if force_scan:
last_full_scan_started_at = sync_started_at
if (
last_sync_started_at is None
or runtime_signature != last_synced_runtime_signature
or now - last_sync_started_at >= interval
):
sync_started_at = now
force_scan = bool(
last_full_scan_started_at is None
or runtime_signature != last_synced_runtime_signature
or now - last_full_scan_started_at >= max(
_FULL_SCAN_FALLBACK_SECONDS,
interval,
)
)
app.state.llmwiki_force_full_scan_once = force_scan
try:
await _sync_once(app, config=config)
finally:
app.state.llmwiki_force_full_scan_once = False
last_sync_started_at = sync_started_at
last_synced_runtime_signature = runtime_signature
if force_scan:
last_full_scan_started_at = sync_started_at
except asyncio.CancelledError:
raise
except Exception: