mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-16 23:01:58 +00:00
修 critical:中文叙述里的数字校验一直是空转的
_NUM_RE 的后瞻写的是 (?<![\w.]),而 Python 的 \w 按 Unicode 判定,汉字也算
词字符。中文写作数字紧贴汉字不加空格是常态,于是整个 token 抽不出来:
extract_numbers('本周营收9999元,订单7777单。') → [] 全部漏检
extract_numbers('本周营收 9999 元,订单 7777 单。') → ['9999','7777']
等于说"叙述层无算术权限 + 数字回指校验"这套机制只对西式排版生效,中文叙述
完全不设防,而出具物照样盖 formal 章、印 matched_numbers: 0——读的人无法
分辨是"查过都对"还是"根本没查"。改成只排 ASCII 词字符,版本号 v1.2.3、
标识符 sha1 这些仍然豁免。
顺带修同一文件里的两条 high:
## 跳过规则只看形态不看语境
年份规则的"年"字是可选的,等于把 1900–2099 的任何独立整数都免检了——而
客户数、门店数、万元金额大量落在这个区间。日期规则不限年份范围,"9600/12"
这种比值写法会被整段当日期吞掉。引用标记 \[\d+\] 不限位数,[98765] 也放行。
'本周新增客户 2050 家,流失 1987 家。' → [] 两个数字都免检
'全年目标 9600/12 个月' → [] 斜杠两边都免检
年份改成必须带"年"字,日期的年份限 19xx/20xx、月份限 1-12,引用标记限三位。
宁可误杀(要求它回指指标集)也不放过——这是校验不是排版。
## 校验没发生被当成校验通过
decide_tier 只看 missing/unmatched,而这些列表全空既可能是"查过了都对",
也可能是"根本没查":叙述模板里一个笔误让 narrative 渲染成空串、metrics_from
指向不存在的节点,都会得到一模一样的空列表,然后盖 formal 章。
contract.narrative = "{{ vars.does_not_exist }}" → tier=formal, matched=0
contract.metrics_from = "压根没这个节点" → tier=formal, metrics=0
新增 gaps 承载"校验为什么不完整"(叙述渲染为空、指标源未解析、指标集为空),
非空就不允许 formal,并把原因印进 issuance。校验没发生却盖 formal,比漏检
某个数字更糟:它让整套机制看起来在工作。
端到端四例验证:编造数字→withheld、真实数字→formal、模板写错→degraded
(带 gaps)、指标源不存在→withheld(带 gaps)。
This commit is contained in:
@@ -13,15 +13,33 @@ from typing import Any
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
# 数字 token:可带千分位逗号、小数,后缀 %/% 或 万/亿
|
||||
_NUM_RE = re.compile(r"(?<![\w.])(-?\d[\d,]*(?:\.\d+)?)\s*(%|%|万亿|亿|万)?")
|
||||
#
|
||||
# 后瞻里刻意只排 ASCII 词字符,**不能**写成 (?<![\w.]):Python 的 \w 按 Unicode
|
||||
# 判定,汉字也算词字符,于是"营收9999元"这种中文常态(数字紧贴汉字、不加空格)
|
||||
# 整个 token 都抽不出来。那等于说校验器只对西式排版生效,中文叙述完全不设防——
|
||||
# 而这套机制的全部意义就是拦住叙述层现编数字。
|
||||
# 排 ASCII 字母是为了放过 v2 / sha1 这类标识符,排 . 是为了放过 v1.2.3 的版本号。
|
||||
_NUM_RE = re.compile(r"(?<![0-9A-Za-z_.])(-?\d[\d,]*(?:\.\d+)?)\s*(%|%|万亿|亿|万)?")
|
||||
|
||||
# 不参与回指的数字形态——这些不是"结论数字"
|
||||
# 不参与回指的数字形态——这些不是"结论数字"。
|
||||
#
|
||||
# 这里的规则一律要求**语境**,不能只看形态:免检名单放宽一分,叙述层就多一分
|
||||
# 现编数字的空间。之前 `年` 是可选的,等于把 1900–2099 的任何独立整数都放行了——
|
||||
# 而客户数、门店数、万元金额大量落在这个区间,"本周新增客户 2050 家"里那个
|
||||
# 2050 就这么免检了。
|
||||
_SKIP_PATTERNS = [
|
||||
re.compile(r"\d{4}\s*[-/年]\s*\d{1,2}(\s*[-/月]\s*\d{1,2}\s*日?)?"), # 日期
|
||||
re.compile(r"(?<!\d)(19|20)\d{2}\s*年?(?!\d)"), # 年份
|
||||
# 日期:年份限 19xx/20xx、月份限 1-12,否则 "9600/12"(比值)这类写法
|
||||
# 会被整段当日期吞掉,斜杠两边的数字就都免检了
|
||||
re.compile(
|
||||
r"(?<!\d)(19|20)\d{2}\s*[-/年]\s*(0?[1-9]|1[0-2])"
|
||||
r"(\s*[-/月]\s*(0?[1-9]|[12]\d|3[01])\s*日?)?(?!\d)"
|
||||
),
|
||||
# 年份:必须带"年"字。没有"年"的四位数按结论数字处理——
|
||||
# 宁可误杀(要求它回指指标集)也不放过,这是校验不是排版
|
||||
re.compile(r"(?<!\d)(19|20)\d{2}\s*年(?!\d)"),
|
||||
re.compile(r"\d{4}-W\d{1,2}", re.IGNORECASE), # ISO 周(2026-W37)
|
||||
re.compile(r"^\s*\d{1,2}[.、)]\s", re.MULTILINE), # 行首枚举 "1. " "2、"
|
||||
re.compile(r"\[\d+\]"), # 引用标记 [1]
|
||||
re.compile(r"\[\d{1,3}\]"), # 引用标记 [1],限三位——[98765] 不是引用
|
||||
re.compile(r"\d{1,2}:\d{2}(:\d{2})?"), # 时间
|
||||
]
|
||||
|
||||
@@ -158,18 +176,25 @@ def decide_tier(
|
||||
missing_expected: list[str],
|
||||
unmatched: list[dict[str, Any]],
|
||||
strict: bool,
|
||||
gaps: list[str] | None = None,
|
||||
) -> str:
|
||||
"""三档出具的判定。
|
||||
|
||||
withheld(不予出具):必需指标缺失,或 strict 下有未回指数字——
|
||||
结论的地基不完整,宁可不出。
|
||||
degraded(降档出具):期望指标缺失或有未回指数字,出但明确声明缺口。
|
||||
formal(正式出具):清单齐、数字全部可回指。
|
||||
degraded(降档出具):期望指标缺失、有未回指数字,或校验本身没能完整跑起来。
|
||||
formal(正式出具):清单齐、数字全部可回指,**且校验确实发生过**。
|
||||
|
||||
最后那句是这个函数最容易出错的地方:missing/unmatched 全空既可能是
|
||||
"查过了都对",也可能是"根本没查"——叙述模板里一个笔误让 narrative 渲染成
|
||||
空串、metrics_from 指向不存在的节点,都会得到一模一样的空列表。
|
||||
校验没发生就盖 formal 章,比漏检某个数字更糟:它让整套机制看起来在工作。
|
||||
所以 gaps 用来承载"校验为什么不完整",非空就不允许 formal。
|
||||
"""
|
||||
if missing_required:
|
||||
return "withheld"
|
||||
if strict and unmatched:
|
||||
return "withheld"
|
||||
if missing_expected or unmatched:
|
||||
if missing_expected or unmatched or gaps:
|
||||
return "degraded"
|
||||
return "formal"
|
||||
|
||||
@@ -74,6 +74,10 @@ def _apply_contract(
|
||||
sources = contract.get("metrics_from") or []
|
||||
if isinstance(sources, str):
|
||||
sources = [sources]
|
||||
# gaps 记的是"校验本身哪里没跑起来",和"校验跑了但没通过"是两回事。
|
||||
# 没有它,一个写错的节点名会让所有列表都是空的,看起来和"全部通过"一模一样。
|
||||
gaps: list[str] = []
|
||||
unresolved: list[str] = []
|
||||
for source in sources:
|
||||
payload = nodes.get(source)
|
||||
if isinstance(payload, dict) and payload.get("kind") == "metric_set":
|
||||
@@ -85,25 +89,44 @@ def _apply_contract(
|
||||
"version": payload.get("caliber_version", ""),
|
||||
}
|
||||
)
|
||||
else:
|
||||
# 节点不存在、还没跑到、或者根本不是口径卡——指标集就是缺的,
|
||||
# 不能当作"这次没有指标要查"
|
||||
unresolved.append(source)
|
||||
if unresolved:
|
||||
gaps.append(f"指标源未解析:{', '.join(unresolved[:5])}")
|
||||
|
||||
present = {m["id"] for m in metrics}
|
||||
missing_required = [m for m in (contract.get("required") or []) if m not in present]
|
||||
missing_expected = [m for m in (contract.get("expected") or []) if m not in present]
|
||||
|
||||
# 2) 叙述数字回指
|
||||
narrative = ctx.render_str(str(contract.get("narrative", "")), state)
|
||||
declared_narrative = str(contract.get("narrative", ""))
|
||||
narrative = ctx.render_str(declared_narrative, state)
|
||||
# 模板取不到路径时 render 出来是空串,所以"声明了叙述但渲染成空"必须单独识别:
|
||||
# 节点改名、vars 写错都会让回指校验静默变成空操作
|
||||
if declared_narrative.strip() and not narrative.strip():
|
||||
gaps.append("叙述模板渲染为空(路径可能写错了)")
|
||||
elif not declared_narrative.strip():
|
||||
gaps.append("契约没有声明叙述,数字回指未执行")
|
||||
|
||||
trace = (
|
||||
trace_numbers(narrative, metrics, allow=contract.get("allow_numbers"))
|
||||
if narrative
|
||||
if narrative.strip()
|
||||
else None
|
||||
)
|
||||
unmatched = trace.unmatched if trace else []
|
||||
|
||||
# 声明了 metrics_from 却一个指标都没收到,同样是"没查成"
|
||||
if sources and not metrics:
|
||||
gaps.append("指标集为空,叙述里的数字无从回指")
|
||||
|
||||
tier = decide_tier(
|
||||
missing_required=missing_required,
|
||||
missing_expected=missing_expected,
|
||||
unmatched=unmatched,
|
||||
strict=bool(contract.get("strict")),
|
||||
gaps=gaps,
|
||||
)
|
||||
|
||||
issuance = {
|
||||
@@ -114,6 +137,8 @@ def _apply_contract(
|
||||
"missing_expected": missing_expected, # 缺数据声明照常印
|
||||
"unmatched_numbers": unmatched[:20],
|
||||
"matched_numbers": len(trace.matched) if trace else 0,
|
||||
# 校验没跑全的原因照实印出来——读的人要能分辨"查过都对"和"根本没查"
|
||||
"gaps": gaps,
|
||||
"declared_at": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
ctx.emit(
|
||||
|
||||
Reference in New Issue
Block a user