每日账泛化成 Ledger(key)(细案 01 做法 17):核查、裁判以前各写一份一模一样的预留、找补、按日滚动,评测再抄就是第三份

- engine/spend.py:Ledger(key) 一套代码,三个实例各记各的键——verify_spend(核查)、judge_spend(裁判)、eval_spend(评测,预留,设置和接口第 3 波加);锁按(事件循环, 键)各一把,一本触顶不挤占另外两本
- 核查的模块函数(SPEND_KEY、daily_spend、charge、settle)照旧,委托给 verify_spend
- engine/judge.py:_charge / _settle 和它自己那份 _today、_spend_of、模块级锁去掉,改用 spend.judge_spend;SPEND_KEY、daily_spend 照旧,裁判的预留、找补、上限行为不变
- 三本账按同一个本地日期滚动(spend._today):test_evidence_judge 里模拟跨天改成换 spend._today(以前换的是裁判自己那份时钟,已经不在了)
- 新增 tests/test_spend_ledger.py(三本账三个键、预留找补与上限、跨天、互不挤占、同一个时钟)
This commit is contained in:
ThearchyHelios
2026-10-16 10:02:53 +08:00
parent 3565227748
commit 0ce272e94b
4 changed files with 190 additions and 118 deletions
+4 -2
View File
@@ -30,6 +30,7 @@ from app.core.events import EventType
from app.db.base import SessionLocal
from app.db.models import Setting
from app.engine import judge as judge_mod
from app.engine import spend
from app.engine.evidence import build_catalog, compose_doc, iter_units
from app.engine.judge import (
BATCH_SIZE,
@@ -503,7 +504,7 @@ async def test_unlimited_never_truncates(monkeypatch, stored):
raw = "".join(f"第 {i} 个区域的销售额增长来自新客。[[see:m:gmv]]" for i in range(BATCH_SIZE + 5))
doc = compose_doc(raw, catalog)
async with SessionLocal() as session: # 今天已经花掉很多:不限就是不限
session.add(Setting(key=SPEND_KEY, value={"date": judge_mod._today(), "usd": 999.0, "calls": 9}))
session.add(Setting(key=SPEND_KEY, value={"date": spend._today(), "usd": 999.0, "calls": 9}))
await session.commit()
outcome = await judge_doc(doc, catalog, spec=SPEC, budget=UNLIMITED)
assert [len(units) for units in judge.judged] == [BATCH_SIZE, 5]
@@ -517,7 +518,8 @@ async def test_the_daily_cap_resets_the_next_day(monkeypatch, stored):
cost = prepare(doc, catalog).cost(PRICED, prepare(doc, catalog).cands)
budget = Budget(max_claims=None, max_cost_usd=None, timeout_s=None, daily_max_usd=cost * 1.05)
day = {"today": "2026-09-28"}
monkeypatch.setattr(judge_mod, "_today", lambda: day["today"])
# 裁判的账和核查、评测按同一个本地日期滚动(engine/spend.py 的 Ledger)
monkeypatch.setattr(spend, "_today", lambda: day["today"])
judge = Judge(monkeypatch)
first = await judge_doc(doc, catalog, spec=SPEC, budget=budget)
+78
View File
@@ -0,0 +1,78 @@
"""每日账泛化成 Ledger(key)(docs/design/next-round/01-效果度量与回归评测.md 做法 17)。
核查、裁判以前各写一份一模一样的「预留 → 找补 → 按本地日期滚动」,评测要是再抄一份就是第三份。现在三本账是同一个
Ledger 的三个实例,各记各的键、各管各的上限:一本触顶不挤占另外两本。裁判、核查原有的行为(模块级的 SPEND_KEY、
daily_spend、charge / settle)不变,已有的测试照旧守着;这里守账本本身和互不挤占。
"""
from __future__ import annotations
import pytest
from sqlalchemy import delete
from app.db.base import SessionLocal
from app.db.models import Setting
from app.engine import judge, spend
from app.engine.spend import Ledger, eval_spend, judge_spend, verify_spend
@pytest.fixture(autouse=True)
async def _clean():
keys = [verify_spend.key, judge_spend.key, eval_spend.key, "test_ledger"]
async with SessionLocal() as session:
await session.execute(delete(Setting).where(Setting.key.in_(keys)))
await session.commit()
yield
async with SessionLocal() as session:
await session.execute(delete(Setting).where(Setting.key.in_(keys)))
await session.commit()
def test_three_books_three_keys():
assert (verify_spend.key, judge_spend.key, eval_spend.key) == ("verify_spend", "judge_spend", "eval_spend")
# 老名字照旧指向各自那本:核查的模块函数、裁判的 SPEND_KEY / daily_spend
assert spend.SPEND_KEY == verify_spend.key
assert judge.SPEND_KEY == judge_spend.key
async def test_reserve_then_settle_and_the_limit():
book = Ledger("test_ledger")
day = await book.charge(0.4, limit=1.0, priced=True)
assert day is not None
assert await book.charge(0.7, limit=1.0, priced=True) is None, "加上这笔超过上限就不记"
await book.settle(day, -0.1)
got = await book.daily()
assert got["usd"] == pytest.approx(0.3) and got["calls"] == 1 and got["unpriced_calls"] == 0
# 估不出金额的照记次数,不比上限
assert await book.charge(0.0, limit=None, priced=False) is not None
assert (await book.daily())["unpriced_calls"] == 1
async def test_a_new_day_starts_from_zero():
day = {"today": "2026-09-28"}
book = Ledger("test_ledger", today=lambda: day["today"])
first = await book.charge(0.9, limit=1.0, priced=True)
assert first == "2026-09-28"
assert await book.charge(0.2, limit=1.0, priced=True) is None
day["today"] = "2026-09-29"
assert await book.charge(0.2, limit=1.0, priced=True) == "2026-09-29"
await book.settle(first, -0.5) # 跨了天的预留不找补:今天从零算
assert (await book.daily())["usd"] == pytest.approx(0.2)
async def test_one_book_hitting_its_cap_does_not_squeeze_the_others():
"""核查花光了当天的额度,裁判、评测照常记:三本账互不挤占。"""
assert await verify_spend.charge(2.0, limit=2.0, priced=True) is not None
assert await verify_spend.charge(0.01, limit=2.0, priced=True) is None
assert await judge_spend.charge(1.0, limit=2.0, priced=True) is not None
assert await eval_spend.charge(1.5, limit=2.0, priced=True) is not None
assert (await judge.daily_spend())["usd"] == pytest.approx(1.0)
assert (await spend.daily_spend())["usd"] == pytest.approx(2.0)
assert (await eval_spend.daily())["usd"] == pytest.approx(1.5)
async def test_all_books_roll_over_on_the_same_clock(monkeypatch):
"""三本账按同一个本地日期滚动(spend._today):裁判不再自己另有一份时钟。"""
monkeypatch.setattr(spend, "_today", lambda: "2026-01-02")
assert await judge_spend.charge(0.1, limit=None, priced=True) == "2026-01-02"
assert (await judge.daily_spend())["date"] == "2026-01-02"
assert (await eval_spend.daily())["date"] == "2026-01-02"