From b4da3fefbac8e4679eb9d41d7c64c2fd49d549b4 Mon Sep 17 00:00:00 2001 From: AleFlyX <167876494+AleFlyX@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:39:50 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=E8=B7=AF=E7=94=B1?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=8C=87=E7=BA=B9=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 变更: - 在 Repository Hygiene 中校验已提交的路由指纹,与生成器和加载器共用算法。 验证: - 路由加载测试 5 项通过。 - 校验命令对有效索引通过,对过期、缺失和损坏的索引失败。 - 重新生成的索引没有差异,改动文件的 Ruff 检查通过。 --- .github/workflows/repository-hygiene.yml | 11 +++++++ deeptutor/api/router_index.py | 39 ++++++++++++++++++++++++ deeptutor/api/router_loader.py | 14 +-------- scripts/build_router_index.py | 2 +- 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 deeptutor/api/router_index.py diff --git a/.github/workflows/repository-hygiene.yml b/.github/workflows/repository-hygiene.yml index 8ead8b173..bd93c824b 100644 --- a/.github/workflows/repository-hygiene.yml +++ b/.github/workflows/repository-hygiene.yml @@ -30,3 +30,14 @@ jobs: - name: Run repository hygiene check run: python3 scripts/check_workspace_hygiene.py + + router-index: + name: Check router index fingerprint + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Compare the router index with its sources + run: python3 -m deeptutor.api.router_index diff --git a/deeptutor/api/router_index.py b/deeptutor/api/router_index.py new file mode 100644 index 000000000..81b681762 --- /dev/null +++ b/deeptutor/api/router_index.py @@ -0,0 +1,39 @@ +"""Check the generated router index without loading the API.""" + +from __future__ import annotations + +import hashlib +import json +from pathlib import Path + +INDEX_PATH = Path(__file__).with_name("router_index.json") + + +def source_fingerprint() -> str: + digest = hashlib.sha256() + root = Path(__file__).with_name("routers") + for path in sorted(root.rglob("*.py")): + digest.update(str(path.relative_to(root)).encode()) + digest.update(path.read_bytes()) + return digest.hexdigest() + + +def main() -> None: + try: + recorded = json.loads(INDEX_PATH.read_text(encoding="utf-8"))["fingerprint"] + except (KeyError, OSError, TypeError, ValueError) as exc: + raise SystemExit( + f"Cannot read the router index fingerprint: {exc}. " + "Run python scripts/build_router_index.py." + ) from exc + + if recorded != source_fingerprint(): + raise SystemExit( + "The router index fingerprint is out of date. Run python scripts/build_router_index.py." + ) + + print("The router index fingerprint matches the router sources.") + + +if __name__ == "__main__": + main() diff --git a/deeptutor/api/router_loader.py b/deeptutor/api/router_loader.py index 2107b85e6..98bee99fb 100644 --- a/deeptutor/api/router_loader.py +++ b/deeptutor/api/router_loader.py @@ -8,28 +8,16 @@ index disables deferral; OpenAPI expands every router before schema generation. from __future__ import annotations from copy import copy -import hashlib from importlib import import_module import json -from pathlib import Path from typing import Any from fastapi import APIRouter, FastAPI from starlette.routing import BaseRoute, Match, Route, WebSocketRoute +from deeptutor.api.router_index import INDEX_PATH, source_fingerprint from deeptutor.services.file_io import read_text -INDEX_PATH = Path(__file__).with_name("router_index.json") - - -def source_fingerprint() -> str: - digest = hashlib.sha256() - root = Path(__file__).with_name("routers") - for path in sorted(root.rglob("*.py")): - digest.update(str(path.relative_to(root)).encode()) - digest.update(path.read_bytes()) - return digest.hexdigest() - async def _unreachable(*_args: Any) -> None: raise RuntimeError("Deferred routing pattern cannot execute a request") diff --git a/scripts/build_router_index.py b/scripts/build_router_index.py index 91619ecac..8b8794567 100644 --- a/scripts/build_router_index.py +++ b/scripts/build_router_index.py @@ -15,7 +15,7 @@ os.environ["DEEPTUTOR_CHAT_ENGINE"] = "python" from starlette.routing import WebSocketRoute # noqa: E402 from deeptutor.api.main import _router_loader # noqa: E402 -from deeptutor.api.router_loader import INDEX_PATH, source_fingerprint # noqa: E402 +from deeptutor.api.router_index import INDEX_PATH, source_fingerprint # noqa: E402 routers = {} for reference, router in _router_loader.records.items():