feat: gate prefix-ordered consumption core (algo #6)
CE-Gate 语义修订获批:块序贯 → 阶梯序前缀逐对序贯。新增 GateSpec/_UnitSlot/ _GateRun 数据结构与 _advance_prefix 纯逻辑(乱序到达下统计严格按预声明阶梯序 消费,INFRA 剔除后重判防 continue 悬置,过线即冻结)。旧块路径共存,Task 6 删。
This commit is contained in:
@@ -793,3 +793,138 @@ async def validate_skill_local(
|
||||
shutil.rmtree(cand_dir)
|
||||
except OSError as e:
|
||||
logger.warning("候选临时目录清理失败 {}: {}", cand_dir, e)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 连续并发 gate:数据结构 + 前缀消费(algo #6 语义修订:块序贯 → 阶梯序前缀逐对序贯)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GateSpec:
|
||||
"""单题型 gate 验证规格(runner 装配阶段产物,调度器输入)。
|
||||
|
||||
字段:
|
||||
task_type: 题型。
|
||||
target_file: 解析后生效 skill 文件名(候选物化写此文件)。
|
||||
candidate_content: 候选 skill 全文。
|
||||
base_skill_content: 基线侧生效 skill 全文(skill_hash 作缓存键)。
|
||||
units: 阶梯序单元列表(已排除案例单元、截断 gate_n_max)。
|
||||
gate_run_prefix: run_id 前缀,必须含 "_gate_"(防泄露过滤依赖)。
|
||||
"""
|
||||
|
||||
task_type: str
|
||||
target_file: str
|
||||
candidate_content: str
|
||||
base_skill_content: str
|
||||
units: list[QuestionUnit]
|
||||
gate_run_prefix: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class _UnitSlot:
|
||||
"""单个阶梯单元的双臂到达状态。
|
||||
|
||||
base 为单元级对错(AR pair 已折叠);cand_per_q 为逐题对错(折叠交给消费时,
|
||||
以复用 unit_correctness_view 并保留逐题溯源)。INFRA 标志与结果互斥。
|
||||
"""
|
||||
|
||||
unit: QuestionUnit
|
||||
base: bool | None = None
|
||||
cand_per_q: dict[str, bool] | None = None
|
||||
base_infra: bool = False
|
||||
cand_infra: bool = False
|
||||
|
||||
def resolved(self) -> bool:
|
||||
"""双臂均已出结果(含 INFRA 判定)。"""
|
||||
base_done = self.base is not None or self.base_infra
|
||||
cand_done = self.cand_per_q is not None or self.cand_infra
|
||||
return base_done and cand_done
|
||||
|
||||
def excluded(self) -> bool:
|
||||
"""任一臂 INFRA 即整单元剔除(不入配对)。"""
|
||||
return self.base_infra or self.cand_infra
|
||||
|
||||
|
||||
@dataclass
|
||||
class _GateRun:
|
||||
"""单题型 gate 的运行时状态(计数器 + 前缀指针 + 证据)。"""
|
||||
|
||||
spec: GateSpec
|
||||
slots: list[_UnitSlot]
|
||||
s_hash: str
|
||||
prefix_ptr: int = 0
|
||||
w: int = 0
|
||||
l: int = 0 # noqa: E741
|
||||
n_used: int = 0
|
||||
n_excluded: int = 0
|
||||
errors: int = 0
|
||||
infra_denom: int = 0
|
||||
frozen: bool = False
|
||||
verdict: GateVerdict | None = None
|
||||
base_obs: dict[str, bool] = field(default_factory=dict)
|
||||
cand_obs: dict[str, bool] = field(default_factory=dict)
|
||||
candidate_per_q: dict[str, bool] = field(default_factory=dict)
|
||||
evidence_rows: list[dict] = field(default_factory=list)
|
||||
|
||||
@classmethod
|
||||
def from_spec(cls, spec: GateSpec) -> _GateRun:
|
||||
"""由规格构造初始状态(slots 与阶梯序一一对应)。"""
|
||||
return cls(
|
||||
spec=spec,
|
||||
slots=[_UnitSlot(unit=u) for u in spec.units],
|
||||
s_hash=skill_hash(spec.base_skill_content),
|
||||
)
|
||||
|
||||
|
||||
def _advance_prefix(run: _GateRun, params: GateParams) -> None:
|
||||
"""沿阶梯序消费"已配齐前缀",逐单元更新 (W,L) 并判定,过线即冻结。
|
||||
|
||||
统计合法性关键(设计 v3 §1 / Codex C1):严禁按到达序消费——base 臂缓存命中
|
||||
瞬间返回、cand 臂必新鲜跑,两臂延迟不对称,若 cand 延迟与对错相关,早到翻转
|
||||
对系统性偏向 W 型 → e-值虚高假接受。前缀消费把判定顺序钉回预声明阶梯序,
|
||||
anytime-valid 无条件成立;INFRA 单元视为"已解决(剔除)"不阻塞前缀。
|
||||
"""
|
||||
while not run.frozen and run.prefix_ptr < len(run.slots):
|
||||
slot = run.slots[run.prefix_ptr]
|
||||
if not slot.resolved():
|
||||
return
|
||||
rank = run.prefix_ptr
|
||||
run.prefix_ptr += 1
|
||||
if slot.excluded():
|
||||
run.n_excluded += 1
|
||||
# 剔除使 n_remaining 缩小,必须重判(Codex plan 审 C1):否则尾部全 INFRA
|
||||
# 时 verdict 停留在 "continue",绕过题尽第四出口且 _finalize_outcome
|
||||
# 查 stop_reason 映射 KeyError。n_used==0(纯前导 INFRA)时无证据可判,跳过。
|
||||
if run.n_used > 0:
|
||||
n_remaining = (len(run.slots) - run.n_excluded) - run.n_used
|
||||
run.verdict = gate_decision(run.w, run.l, run.n_used, n_remaining, params=params)
|
||||
if run.verdict.decision != "continue":
|
||||
run.frozen = True
|
||||
continue
|
||||
uid = slot.unit.unit_id
|
||||
assert slot.base is not None and slot.cand_per_q is not None
|
||||
c_units = unit_correctness_view([slot.unit], slot.cand_per_q)
|
||||
pair_result = pair_block({uid: slot.base}, c_units, [uid])
|
||||
run.candidate_per_q.update(slot.cand_per_q)
|
||||
for u, (b, c) in pair_result.observed.items():
|
||||
run.base_obs[u] = b
|
||||
run.cand_obs[u] = c
|
||||
run.w += pair_result.w
|
||||
run.l += pair_result.l
|
||||
run.n_used += 1
|
||||
n_remaining = (len(run.slots) - run.n_excluded) - run.n_used
|
||||
run.verdict = gate_decision(run.w, run.l, run.n_used, n_remaining, params=params)
|
||||
run.evidence_rows.append(
|
||||
{
|
||||
"question_id": uid,
|
||||
"task_type": run.spec.task_type,
|
||||
"ladder_rank": rank,
|
||||
"baseline_correct": slot.base,
|
||||
"candidate_correct": c_units[uid],
|
||||
"e_value": run.verdict.e_value,
|
||||
"stop_reason": "",
|
||||
}
|
||||
)
|
||||
if run.verdict.decision != "continue":
|
||||
run.frozen = True
|
||||
|
||||
Reference in New Issue
Block a user