mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-26 12:12:10 +00:00
业务本体与标准实体:跨源键值对齐、对账共变算子、健康证据链与实体助手
- 新增标准业务实体(CanonicalEntity)模型、确定性跨源编码翻译、后台候选实体扫描与文档驱动实体智能助手 - 新增指标本体图谱(factor / reconciles_with / upstream_driver)、DAG 防环校验以及 ontology_neighbors / source_health 内置工具 - 新增跨源对账(analysis_reconcile)与滞后共变(analysis_cochange)分析算子,打通 [[h:H1]] 数据源健康证据链、报告导出与本体消融评测 - 数据目录新增「标准业务实体」视图与候选审阅工作台,口径页集成指标本体拓扑面板与端到端浏览器检查
This commit is contained in:
@@ -6,7 +6,7 @@ from typing import Any
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
INVESTIGATION_VERSION = 1
|
||||
INVESTIGATION_VERSION = 2
|
||||
|
||||
|
||||
class InvestigationPolicy(BaseModel):
|
||||
@@ -15,6 +15,9 @@ class InvestigationPolicy(BaseModel):
|
||||
max_rounds: int = Field(default=2, ge=0, le=5)
|
||||
minimum_depth: int = Field(default=1, ge=0, le=5)
|
||||
min_coverage: float = Field(default=0.8, ge=0, le=1)
|
||||
max_ontology_hops: int = Field(default=2, ge=0, le=5)
|
||||
max_cross_queries: int = Field(default=6, ge=0, le=20)
|
||||
check_source_health_first: bool = True
|
||||
|
||||
|
||||
def _number(value: Any) -> float:
|
||||
@@ -25,24 +28,117 @@ def _number(value: Any) -> float:
|
||||
return 0.0
|
||||
|
||||
|
||||
def _neighbor_queried(neighbor: dict[str, Any], queried_calibers: set[str], queried_sources: set[str],
|
||||
focal_sources: set[str], ontology_hops: int) -> bool:
|
||||
if ontology_hops >= 1:
|
||||
target = neighbor.get("target") or {}
|
||||
wf_id = str(target.get("workflow_id") or "")
|
||||
card_id = str(target.get("card_id") or "")
|
||||
if wf_id and (f"{wf_id}:{card_id}" in queried_calibers or wf_id in queried_calibers):
|
||||
return True
|
||||
peer_sources = [str(s) for s in (target.get("sources") or []) if s]
|
||||
if any(s in queried_sources and s not in focal_sources for s in peer_sources):
|
||||
return True
|
||||
return not wf_id and not peer_sources
|
||||
return False
|
||||
|
||||
|
||||
def assess(scope: Any, question: str, policy: InvestigationPolicy) -> dict[str, Any]:
|
||||
from app.analysis.intent import analysis_intent
|
||||
|
||||
results = list(scope.results.values()) if scope is not None else []
|
||||
results = list(scope.results.values()) if scope is not None and hasattr(scope, "results") else []
|
||||
healths = [h for h in (getattr(scope, "source_healths", None) or {}).values() if isinstance(h, dict)]
|
||||
neighbors = [n for n in (getattr(scope, "ontology_neighbors", None) or []) if isinstance(n, dict)]
|
||||
contract_violations = list(getattr(scope, "contract_violations", None) or [])
|
||||
ontology_hops = int(getattr(scope, "ontology_hops", 0) or 0)
|
||||
cross_queries = int(getattr(scope, "cross_queries", 0) or 0)
|
||||
queried_calibers = set(getattr(scope, "queried_calibers", None) or ())
|
||||
queried_sources = set(getattr(scope, "queried_sources", None) or ())
|
||||
focal_sources = set(getattr(scope, "focal_sources", None) or ())
|
||||
|
||||
has_health_anomaly = any(h.get("verdict") == "anomalous" for h in healths)
|
||||
has_reconcile_fault = any(
|
||||
r.get("status") == "ok"
|
||||
and r.get("op") == "reconcile"
|
||||
and (r.get("verdict") or {}).get("code") in ("diverged", "missing_counterpart")
|
||||
for r in results
|
||||
)
|
||||
intent = analysis_intent(question)
|
||||
active = bool(policy.enabled and scope is not None and (
|
||||
(intent is not None and intent.kind == "why") or any(
|
||||
(intent is not None and intent.kind == "why")
|
||||
or any(
|
||||
r.get("status") == "ok" and (r.get("verdict") or {}).get("code") in ("outside_band", "far_outside_band")
|
||||
for r in results)))
|
||||
out: dict[str, Any] = {"version": INVESTIGATION_VERSION, "active": active, "status": "not_required", "depth": 0, "coverage": None,
|
||||
"query_count": len(scope.queries) if scope is not None else 0,
|
||||
"analysis_count": len(results), "reason": "", "next_step": ""}
|
||||
for r in results
|
||||
)
|
||||
or has_health_anomaly
|
||||
or has_reconcile_fault
|
||||
or bool(contract_violations)
|
||||
))
|
||||
out: dict[str, Any] = {
|
||||
"version": INVESTIGATION_VERSION,
|
||||
"active": active,
|
||||
"status": "not_required",
|
||||
"depth": 0,
|
||||
"coverage": None,
|
||||
"query_count": len(scope.queries) if scope is not None and hasattr(scope, "queries") else 0,
|
||||
"analysis_count": len(results),
|
||||
"ontology_hops": ontology_hops,
|
||||
"source_health_checked": bool(healths),
|
||||
"reason": "",
|
||||
"next_step": "",
|
||||
}
|
||||
if not active:
|
||||
return out
|
||||
|
||||
def pending(reason: str, next_step: str, stage: str) -> dict[str, Any]:
|
||||
return {**out, "status": "partial", "stage": stage, "reason": reason, "next_step": next_step}
|
||||
|
||||
# 第一阶段:数据源健康度、跨源对账与物理契约检查(发现源表故障或对账超差立即早停)
|
||||
if has_health_anomaly:
|
||||
return {
|
||||
**out,
|
||||
"status": "complete",
|
||||
"stage": "health_and_contract",
|
||||
"reason": "关联数据源健康度核查发现断更、断档或数据骤降异常,已锁定数据质量问题,停止业务归因拆解",
|
||||
}
|
||||
if has_reconcile_fault:
|
||||
return {
|
||||
**out,
|
||||
"status": "complete",
|
||||
"stage": "health_and_contract",
|
||||
"reason": "跨源镜像口径对账超差或对侧缺失,已锁定跨源数据或单位不一致问题,停止业务归因拆解",
|
||||
}
|
||||
if contract_violations:
|
||||
if not neighbors:
|
||||
return pending(
|
||||
"指标触发契约或值域越界,尚未查询本体构成因子与关联数据源",
|
||||
"请先调用 ontology_neighbors 查看该指标的构成因子(factor)与跨源实体桥,再核查对侧基准值或数据源健康度。",
|
||||
"health_and_contract",
|
||||
)
|
||||
if ontology_hops < 1 and policy.max_ontology_hops > 0 and cross_queries < policy.max_cross_queries:
|
||||
return pending(
|
||||
"已发现构成因子或跨源实体桥,尚未穿透核对对侧主数据或因子口径",
|
||||
"请按 ontology_neighbors 的 recommended_caliber_query 或标准实体桥查询关联因子与对侧主数据,核实是否存在跨库失配。",
|
||||
"ontology_penetration",
|
||||
)
|
||||
return {
|
||||
**out,
|
||||
"status": "complete",
|
||||
"stage": "ontology_penetration",
|
||||
"reason": "已完成契约越界指标的跨源本体穿透与因子核对",
|
||||
}
|
||||
if (
|
||||
policy.check_source_health_first
|
||||
and any(n.get("kind") == "reconciles_with" for n in neighbors)
|
||||
and not healths
|
||||
and not any(r.get("op") == "reconcile" for r in results)
|
||||
):
|
||||
return pending(
|
||||
"发现已确认的跨源对账镜像口径,尚未核查对侧数据源健康度或对账一致性",
|
||||
"请先调用 source_health 核查对侧数据源健康度,或调用 analysis_reconcile 核对镜像口径,排除断更与单位失配后再拆解业务维度。",
|
||||
"health_and_contract",
|
||||
)
|
||||
|
||||
done = [r for r in results if r.get("status") == "ok"]
|
||||
decomposed = [r for r in done if r.get("op") == "decompose"]
|
||||
if not decomposed:
|
||||
@@ -99,4 +195,38 @@ def assess(scope: Any, question: str, policy: InvestigationPolicy) -> dict[str,
|
||||
return pending("主要贡献项尚未进一步下钻", "对主要贡献项选择更细的业务维度,用 parent 关联上层结果继续下钻;没有合适维度时说明限制。", "drill")
|
||||
if last.get("mode") == "additive" and coverage < policy.min_coverage:
|
||||
return pending("主要贡献项的解释覆盖程度不足", "检查其他候选维度和剩余差异,保留未解释的部分,不把贡献分解写成因果证明。", "coverage")
|
||||
|
||||
# 第三阶段:同源切片定位完成后,对已发现的 1 跳 factor / upstream_driver 本体邻居执行有界跨源穿透核查
|
||||
driver_neighbors = [n for n in neighbors if n.get("kind") in ("upstream_driver", "factor")]
|
||||
if driver_neighbors and policy.max_ontology_hops > 0:
|
||||
within_budget = ontology_hops < policy.max_ontology_hops and cross_queries < policy.max_cross_queries
|
||||
if not any(_neighbor_queried(n, queried_calibers, queried_sources, focal_sources, ontology_hops)
|
||||
for n in driver_neighbors) and within_budget:
|
||||
return pending(
|
||||
"已完成同源切片定位,尚未沿本体穿透核查关联因子或上游驱动口径",
|
||||
"请将定位到的异常切片通过标准实体桥对齐后调用 caliber_query 查询邻居指标,验证跨源表现。",
|
||||
"ontology_penetration",
|
||||
)
|
||||
if any(n.get("kind") == "upstream_driver" for n in driver_neighbors):
|
||||
cochange_done = [r for r in done if r.get("op") == "cochange"]
|
||||
if not cochange_done:
|
||||
cochange_refused = [r for r in results if r.get("op") == "cochange" and r.get("status") == "refused"]
|
||||
if cochange_refused:
|
||||
return pending(
|
||||
str(cochange_refused[-1].get("reason") or "上游共变检验样本不足"),
|
||||
"请检查两序列的时间窗口与有效点数;无法补齐时明确说明统计共变尚未证实,不写因果结论。",
|
||||
"ontology_penetration",
|
||||
)
|
||||
if cross_queries <= policy.max_cross_queries:
|
||||
return pending(
|
||||
"已获取跨源上游驱动数据,尚未执行同步共变统计检验",
|
||||
"请调用 analysis_cochange 检验主指标与上游驱动序列是否存在同窗或滞后共变,注意相关性不等于因果。",
|
||||
"ontology_penetration",
|
||||
)
|
||||
return {
|
||||
**out,
|
||||
"status": "complete",
|
||||
"stage": "ontology_penetration",
|
||||
"reason": "已完成同源切片定位与跨源本体穿透核查,相关性结论仍需独立业务证据确认因果",
|
||||
}
|
||||
return {**out, "status": "complete", "stage": "contribution", "reason": "完成贡献分析,因果关系仍需独立证据"}
|
||||
|
||||
Reference in New Issue
Block a user