feat(selector): 增加离线候选遍历与严格回放验证 Demo
- 新增 selector_candidate_demo,移除 provenance intent 后枚举候选 selector - 对候选分支执行有界重建与严格 STEP 比较 - 仅在候选遍历完整且唯一 strict 通过时生成 selector 映射记录 - 增加 selector 候选搜索、预算限制和记录生成的测试 - 保持生产 selector resolver 不受 Demo 逻辑影响 - 更新 CADFS 能力台账,记录 IMPRINT 派生 profile 的 lineage selector 缺口
This commit is contained in:
+51
-37
@@ -6,36 +6,38 @@ import re
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _last_executable_prefix(
|
||||
cdsl: dict[str, Any],
|
||||
failed_feature_id: str | None,
|
||||
output: Path,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Bind and export the longest verified prefix before a failed feature."""
|
||||
from .selector_binding import bind_candidate_selectors
|
||||
from engine.cdsl_engine.runtime import rebuild_cdsl
|
||||
def _last_executable_prefix(error: Exception, output: Path) -> dict[str, Any] | None:
|
||||
"""Export the checkpoint already produced by the failed live replay."""
|
||||
execution = getattr(error, "incremental_execution", None)
|
||||
bound = getattr(error, "bound_cdsl", None)
|
||||
if execution is None or not isinstance(bound, dict):
|
||||
return None
|
||||
session = execution.session
|
||||
if not session.results or (session.body is None and not session.surface_members):
|
||||
return None
|
||||
from engine.cdsl_engine.runtime import finalize_cdsl_execution
|
||||
|
||||
features = list(cdsl.get("features") or [])
|
||||
failed_index = next(
|
||||
(index for index, feature in enumerate(features) if feature.get("id") == failed_feature_id),
|
||||
len(features),
|
||||
try:
|
||||
result = finalize_cdsl_execution(execution, output)
|
||||
except Exception:
|
||||
return None
|
||||
last_feature_id = next(reversed(session.results))
|
||||
features = list(bound.get("features") or [])
|
||||
feature_count = next(
|
||||
(index + 1 for index, feature in enumerate(features) if feature.get("id") == last_feature_id),
|
||||
0,
|
||||
)
|
||||
for feature_count in range(failed_index, 0, -1):
|
||||
prefix = deepcopy(cdsl)
|
||||
prefix["features"] = features[:feature_count]
|
||||
try:
|
||||
bound_prefix, _binding = bind_candidate_selectors(prefix)
|
||||
result = rebuild_cdsl(bound_prefix, output, strict=True)
|
||||
except Exception:
|
||||
continue
|
||||
return {
|
||||
"failed_feature_id": failed_feature_id,
|
||||
"feature_count": feature_count,
|
||||
"last_feature_id": str(features[feature_count - 1].get("id") or ""),
|
||||
"bound_cdsl": bound_prefix,
|
||||
"result": result,
|
||||
}
|
||||
return None
|
||||
if feature_count == 0:
|
||||
return None
|
||||
prefix = deepcopy(bound)
|
||||
prefix["features"] = features[:feature_count]
|
||||
return {
|
||||
"failed_feature_id": getattr(error, "failed_feature_id", None) or _failed_feature_id(error),
|
||||
"feature_count": feature_count,
|
||||
"last_feature_id": last_feature_id,
|
||||
"bound_cdsl": prefix,
|
||||
"result": result,
|
||||
}
|
||||
|
||||
|
||||
def _failed_feature_id(error: Exception) -> str | None:
|
||||
@@ -48,22 +50,34 @@ def _failed_feature_id(error: Exception) -> str | None:
|
||||
|
||||
|
||||
def rebuild_candidate(cdsl: dict[str, Any], output: Path) -> dict[str, Any]:
|
||||
from engine.cdsl_engine.runtime import analyze_cdsl, rebuild_cdsl
|
||||
from .selector_binding import bind_candidate_selectors
|
||||
from engine.cdsl_engine.runtime import analyze_cdsl, finalize_cdsl_execution
|
||||
from .selector_binding import bind_and_execute_candidate_selectors
|
||||
|
||||
analysis = analyze_cdsl(cdsl)
|
||||
analysis_dict = analysis.as_dict() if hasattr(analysis, "as_dict") else {"runtime_eligible": analysis.runtime_eligible}
|
||||
if not analysis.runtime_eligible:
|
||||
return {"status": "runtime_ineligible", "analysis": analysis_dict}
|
||||
bound: dict[str, Any] | None = None
|
||||
try:
|
||||
bound, binding = bind_candidate_selectors(cdsl)
|
||||
result = rebuild_cdsl(bound, output, strict=True)
|
||||
return {"status": "rebuilt", "analysis": analysis_dict, "selector_binding": binding, "bound_cdsl": bound, "result": result}
|
||||
replay = bind_and_execute_candidate_selectors(cdsl)
|
||||
result = finalize_cdsl_execution(replay.execution, output)
|
||||
return {
|
||||
"status": "rebuilt",
|
||||
"analysis": analysis_dict,
|
||||
"selector_binding": replay.evidence,
|
||||
"bound_cdsl": replay.bound_cdsl,
|
||||
"result": result,
|
||||
}
|
||||
except Exception as exc:
|
||||
detail = {"type": type(exc).__name__, "message": str(exc)}
|
||||
if hasattr(exc, "selector_resolutions"): detail["selector_resolutions"] = exc.selector_resolutions
|
||||
prefix = _last_executable_prefix(cdsl, _failed_feature_id(exc), output)
|
||||
if hasattr(exc, "selector_resolutions"):
|
||||
detail["selector_resolutions"] = exc.selector_resolutions
|
||||
prefix = _last_executable_prefix(exc, output)
|
||||
result = {"status": "rebuild_failed", "analysis": analysis_dict, "error": detail}
|
||||
if bound is not None: result["bound_cdsl"] = bound
|
||||
bound = getattr(exc, "bound_cdsl", None)
|
||||
if isinstance(bound, dict):
|
||||
result["bound_cdsl"] = bound
|
||||
binding = getattr(exc, "selector_binding", None)
|
||||
if isinstance(binding, list):
|
||||
result["selector_binding"] = binding
|
||||
if prefix is not None: result["last_executable_prefix"] = prefix
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user