个人长报告同盘同日重试改为秒回

完整数据版在版本号明确时复用当天的结果。渲染快捷算法留下但默认关闭,本机只快了大约两成。

Co-Authored-By: Grok (agent loop)
This commit is contained in:
jesse-ux
2026-10-14 06:37:40 +08:00
co-authored by Grok (agent loop)
parent 66cd60081f
commit 69c739fbee
12 changed files with 1334 additions and 35 deletions
+20 -11
View File
@@ -57,8 +57,8 @@ def _release_warm_slot() -> None:
_warm_inflight -= 1
def cache_dir() -> Path:
path = ROOT / "scratch" / "local" / "consult_full_data_cache"
def cache_dir(name: str = "consult_full_data_cache") -> Path:
path = ROOT / "scratch" / "local" / name
path.mkdir(parents=True, exist_ok=True)
return path
@@ -118,15 +118,15 @@ def cache_key(identity: dict[str, Any]) -> str:
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
def _cache_path(identity: dict[str, Any]) -> Path:
return cache_dir() / f"{cache_key(identity)}.json"
def _cache_path(identity: dict[str, Any], name: str = "consult_full_data_cache") -> Path:
return cache_dir(name) / f"{cache_key(identity)}.json"
def prune_cache(now: float | None = None) -> int:
def prune_cache(now: float | None = None, *, name: str = "consult_full_data_cache") -> int:
"""Drop cache files last written more than two days ago."""
cutoff = (time.time() if now is None else now) - KEEP_SECONDS
removed = 0
folder = cache_dir()
folder = cache_dir(name)
for path in folder.glob("*.json"):
try:
if path.stat().st_mtime < cutoff:
@@ -147,15 +147,21 @@ def get_or_build(
builder: PacketBuilder | None = None,
*,
clock: Callable[[], float] | None = None,
cache_name: str = "consult_full_data_cache",
json_default: Callable[..., Any] | None = None,
) -> dict[str, Any]:
"""Return the packet. Concurrent callers for one key share one build."""
prune_cache()
"""Return the packet. Concurrent callers for one key share one build.
``cache_name`` selects the scratch subdirectory. The consult card keeps the
default. A non-default name does not change the consult key or its files.
"""
prune_cache(name=cache_name)
now = clock or time.perf_counter
started = now()
path = _cache_path(identity)
path = _cache_path(identity, cache_name)
if path.is_file():
return _read_cached(path, started, now)
key = cache_key(identity)
key = f"{cache_name}\n{cache_key(identity)}"
with _flights_guard:
flight = _flights.get(key)
owner = flight is None
@@ -177,7 +183,10 @@ def get_or_build(
builder = build_full_data_packet
packet = builder(identity)
temporary = path.with_suffix(".json.tmp")
temporary.write_text(json.dumps(packet, ensure_ascii=False, separators=(",", ":")), encoding="utf-8")
dump_kwargs: dict[str, Any] = {"ensure_ascii": False, "separators": (",", ":")}
if json_default is not None:
dump_kwargs["default"] = json_default
temporary.write_text(json.dumps(packet, **dump_kwargs), encoding="utf-8")
temporary.replace(path)
elapsed = now() - started
return {