chore: 增加路由索引指纹检查

变更:
- 在 Repository Hygiene 中校验已提交的路由指纹,与生成器和加载器共用算法。

验证:
- 路由加载测试 5 项通过。
- 校验命令对有效索引通过,对过期、缺失和损坏的索引失败。
- 重新生成的索引没有差异,改动文件的 Ruff 检查通过。
This commit is contained in:
AleFlyX
2026-11-30 21:39:50 +08:00
parent 21e3c40505
commit b4da3fefba
4 changed files with 52 additions and 14 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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")

View File

@@ -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():