mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 08:59:57 +08:00
* feat(knowledge): add GitHub source sync
* fix(github-sync): contain downloaded paths inside the KB raw dir
Destinations were built as `raw_dir / rel` where `rel` is derived from the
GitHub tree/compare response — remote input. Two shapes escaped: a `..`
segment, and an absolute path, which is the quieter one because
`Path("/kb") / "/etc/x"` is `/etc/x`, silently discarding the base. The only
filters in the way checked the file extension and the glob, so a matching
path would have been written — and, on the removal branch, deleted — outside
the knowledge base.
git rejects both shapes in tree entries today, so this is defence in depth
rather than a live exploit; a downloader still should not delegate to the
remote the question of where it writes. One `_contained_dest` seam now guards
the full-sync write, the incremental write, and the removal, and logs what it
refused.
---------
Co-authored-by: XZH <cto@xzh.ai>
Co-authored-by: Frank <bingxizhao@novix.science>
72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
"""Background service that periodically syncs GitHub sources into KBs."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import logging
|
|
|
|
from deeptutor.services.base_sync import BaseSourceSyncService, is_stale
|
|
from deeptutor.services.github_source.sync import SYNC_INTERVAL_HOURS
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class GitHubSourceSyncService(BaseSourceSyncService):
|
|
"""Periodically syncs all GitHub sources."""
|
|
|
|
def __init__(self, *, base_dir=None, client=None, check_interval_s=3600):
|
|
super().__init__(base_dir=base_dir, check_interval_s=check_interval_s)
|
|
self._client = client
|
|
|
|
@property
|
|
def task_name(self) -> str:
|
|
return "github-source-sync"
|
|
|
|
async def _sync_one_cycle(self) -> None:
|
|
from deeptutor.knowledge.manager import KnowledgeBaseManager
|
|
from deeptutor.services.github_source.sync import sync_source
|
|
|
|
base_dir = self.effective_base_dir
|
|
manager = KnowledgeBaseManager(base_dir=base_dir)
|
|
|
|
all_sources = manager.get_all_github_sources()
|
|
for kb_name, source in all_sources:
|
|
if not source.get("enabled", True):
|
|
continue
|
|
if not is_stale(source, stale_hours=SYNC_INTERVAL_HOURS):
|
|
continue
|
|
sid = source.get("id", "?")
|
|
try:
|
|
result = await sync_source(
|
|
kb_name=kb_name,
|
|
source=source,
|
|
base_dir=base_dir,
|
|
client=self._client,
|
|
)
|
|
if not result.ok:
|
|
manager.update_github_source_state(
|
|
kb_name=kb_name,
|
|
source_id=sid,
|
|
last_sync_status="error",
|
|
last_sync_error=result.error,
|
|
last_synced_at=datetime.now(timezone.utc).isoformat(),
|
|
)
|
|
except Exception as exc:
|
|
manager.update_github_source_state(
|
|
kb_name=kb_name,
|
|
source_id=sid,
|
|
last_sync_status="error",
|
|
last_sync_error=str(exc),
|
|
last_synced_at=datetime.now(timezone.utc).isoformat(),
|
|
)
|
|
|
|
|
|
_sync_service = None
|
|
|
|
|
|
def get_sync_service():
|
|
global _sync_service
|
|
if _sync_service is None:
|
|
_sync_service = GitHubSourceSyncService()
|
|
return _sync_service
|