Ashtottari 在适用时把出生前已经走过的部分算进时间线。 Co-Authored-By: Grok (agent loop)
144 lines
6.1 KiB
Python
144 lines
6.1 KiB
Python
"""Freeze coupling in scripts/jyotish_api_server.py.
|
|
|
|
New endpoints and features must live in new modules and be thinly registered
|
|
from the main file. Line count is only a coarse guardrail; the live gates are
|
|
JyotishAPIHandler method count and JyotishAPIHandler.__new__ forgery sites.
|
|
|
|
Baselines re-measured 2026-09-16 after TASK-api-server-backdoor-close-20260916
|
|
moved seven compute-only methods into scripts/offline_compute_mixins.py and
|
|
closed two of the four production forgeries.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ast
|
|
import re
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
API_SERVER = ROOT / "scripts" / "jyotish_api_server.py"
|
|
AGENTS = ROOT / "AGENTS.md"
|
|
|
|
# Live `wc -l scripts/jyotish_api_server.py` equivalent:
|
|
# Path.read_bytes().count(b"\n"), measured 2026-09-16 on origin/staging @ 51a65d92.
|
|
# New features must not consume this budget; open a module instead.
|
|
JYOTISH_API_SERVER_LINE_COUNT_BASELINE = 10924
|
|
JYOTISH_API_SERVER_LINE_COUNT_CAP = JYOTISH_API_SERVER_LINE_COUNT_BASELINE + 300
|
|
|
|
# Whole-file indent match `^ (?:async )?def \w+`, same count as
|
|
# TASK-freeze-metric-change-20260915 §1. Re-measured 2026-09-16 after
|
|
# TASK-api-server-backdoor-close-20260916 lifted 7 compute-only methods into
|
|
# scripts/offline_compute_mixins.py (225 -> 218).
|
|
# 原值:218(删占位回退之前)
|
|
# 新值:217
|
|
# 原因:BUG-1341 删除 JyotishAPIHandler._fallback_dasha_periods,类方法少 1 个,没有新增方法
|
|
JYOTISH_API_HANDLER_METHOD_COUNT_BASELINE = 217
|
|
|
|
# `JyotishAPIHandler.__new__` in scripts/ and tests/ `*.py`, excluding this file.
|
|
# Re-measured 2026-09-16 after TASK-api-server-backdoor-close-20260916:
|
|
# scripts/ production forgeries 4 -> 2, tests/ unchanged at 29.
|
|
JYOTISH_API_HANDLER_NEW_COUNT_BASELINE = 31
|
|
|
|
# Production forgeries only. The two survivors both sit on the consultation
|
|
# workflow chain (`consultation_workflow_service.execute_consultation_workflow`
|
|
# and `capture_report_blocked_repairs_golden`); closing them needs the 4,086-line
|
|
# mixin extraction that the 2026-09-16 spike showed breaks cross-module
|
|
# monkeypatching in tests/test_api_server_security.py. Target stays 0.
|
|
JYOTISH_API_HANDLER_NEW_SCRIPTS_BASELINE = 2
|
|
|
|
# Compute-only mixins lifted out of the handler must never depend back on the
|
|
# HTTP monolith, or the backdoor simply moves house.
|
|
OFFLINE_MIXINS = ROOT / "scripts" / "offline_compute_mixins.py"
|
|
|
|
HANDLER_METHOD_RE = re.compile(r"^ (?:async )?def \w+", re.MULTILINE)
|
|
NEW_MARKER = "JyotishAPIHandler.__new__"
|
|
|
|
|
|
def _handler_method_count(source: str) -> int:
|
|
return len(HANDLER_METHOD_RE.findall(source))
|
|
|
|
|
|
def _new_hits(*folders: Path) -> Counter[str]:
|
|
hits: Counter[str] = Counter()
|
|
skip = Path(__file__).resolve()
|
|
for folder in folders or (ROOT / "scripts", ROOT / "tests"):
|
|
for path in sorted(folder.rglob("*.py")):
|
|
if path.resolve() == skip:
|
|
continue
|
|
count = path.read_text(encoding="utf-8").count(NEW_MARKER)
|
|
if count:
|
|
hits[path.relative_to(ROOT).as_posix()] = count
|
|
return hits
|
|
|
|
|
|
def test_jyotish_api_handler_method_count_must_not_grow() -> None:
|
|
source = API_SERVER.read_text(encoding="utf-8")
|
|
count = _handler_method_count(source)
|
|
assert count <= JYOTISH_API_HANDLER_METHOD_COUNT_BASELINE, (
|
|
f"{API_SERVER.as_posix()} has {count} four-space def methods; "
|
|
f"cap is {JYOTISH_API_HANDLER_METHOD_COUNT_BASELINE}. Move behaviour "
|
|
"into a dedicated module; thinly registered from this file."
|
|
)
|
|
|
|
|
|
def test_jyotish_api_handler_new_count_must_not_grow() -> None:
|
|
hits = _new_hits()
|
|
total = sum(hits.values())
|
|
listed = ", ".join(f"{path}:{count}" for path, count in sorted(hits.items()))
|
|
assert total <= JYOTISH_API_HANDLER_NEW_COUNT_BASELINE, (
|
|
f"{NEW_MARKER} appears {total} times under scripts/ and tests/; "
|
|
f"cap is {JYOTISH_API_HANDLER_NEW_COUNT_BASELINE}. Hits: {listed}"
|
|
)
|
|
|
|
|
|
def test_scripts_handler_forgeries_must_not_grow() -> None:
|
|
hits = _new_hits(ROOT / "scripts")
|
|
total = sum(hits.values())
|
|
listed = ", ".join(f"{path}:{count}" for path, count in sorted(hits.items()))
|
|
assert total <= JYOTISH_API_HANDLER_NEW_SCRIPTS_BASELINE, (
|
|
f"{NEW_MARKER} appears {total} times under scripts/; cap is "
|
|
f"{JYOTISH_API_HANDLER_NEW_SCRIPTS_BASELINE} and the target is 0. "
|
|
f"Offline callers must instantiate a compute mixin instead. Hits: {listed}"
|
|
)
|
|
|
|
|
|
def test_offline_mixins_must_not_import_the_http_monolith() -> None:
|
|
tree = ast.parse(OFFLINE_MIXINS.read_text(encoding="utf-8"))
|
|
imported: list[str] = []
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Import):
|
|
imported.extend(alias.name for alias in node.names)
|
|
elif isinstance(node, ast.ImportFrom):
|
|
imported.append(node.module or "")
|
|
offenders = [name for name in imported if "jyotish_api_server" in name]
|
|
assert not offenders, (
|
|
f"{OFFLINE_MIXINS.as_posix()} imports {offenders}; the compute mixins must "
|
|
"not depend on the HTTP monolith or the __new__ backdoor just moves house."
|
|
)
|
|
|
|
|
|
def test_jyotish_api_server_must_not_grow_beyond_bugfix_slack() -> None:
|
|
line_count = API_SERVER.read_bytes().count(b"\n")
|
|
assert line_count <= JYOTISH_API_SERVER_LINE_COUNT_CAP, (
|
|
f"{API_SERVER.as_posix()} has {line_count} lines; cap is "
|
|
f"{JYOTISH_API_SERVER_LINE_COUNT_CAP} ({JYOTISH_API_SERVER_LINE_COUNT_BASELINE} "
|
|
"baseline + 300 coarse guardrail). New endpoints and features must be new "
|
|
"modules, thinly registered from this file."
|
|
)
|
|
|
|
|
|
def test_agents_forbids_growing_jyotish_api_server() -> None:
|
|
agents = AGENTS.read_text(encoding="utf-8")
|
|
assert "scripts/jyotish_api_server.py" in agents
|
|
assert "must not grow" in agents
|
|
assert "thinly registered" in agents
|
|
|
|
|
|
def test_quality_gate_runs_api_server_growth_contract() -> None:
|
|
from scripts.run_quality_gate import CORE_PYTEST_TARGETS
|
|
|
|
assert "tests/test_api_server_growth_contract.py" in CORE_PYTEST_TARGETS
|
|
quality_gate = (ROOT / "scripts" / "run_quality_gate.py").read_text(encoding="utf-8")
|
|
assert '"tests/test_api_server_growth_contract.py"' in quality_gate
|