mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-20 03:21:59 +00:00
业务本体与标准实体:跨源键值对齐、对账共变算子、健康证据链与实体助手
- 新增标准业务实体(CanonicalEntity)模型、确定性跨源编码翻译、后台候选实体扫描与文档驱动实体智能助手 - 新增指标本体图谱(factor / reconciles_with / upstream_driver)、DAG 防环校验以及 ontology_neighbors / source_health 内置工具 - 新增跨源对账(analysis_reconcile)与滞后共变(analysis_cochange)分析算子,打通 [[h:H1]] 数据源健康证据链、报告导出与本体消融评测 - 数据目录新增「标准业务实体」视图与候选审阅工作台,口径页集成指标本体拓扑面板与端到端浏览器检查
This commit is contained in:
@@ -0,0 +1,693 @@
|
||||
"""跨源指标本体穿透验收与回归评测用的三源虚构制造库(与任何真实业务无关)。
|
||||
|
||||
python -m tests.fixtures.acceptance.multi_source_ontology <输出目录> [--today YYYY-MM-DD]
|
||||
|
||||
生成「今天」之前 24 天(截止昨天)的三份独立 SQLite 业务库,随机数种子固定:同一个「今天」生成的三份库逐字节一致。
|
||||
三个数据源使用异构物理列名与编码格式,只能经标准实体 `production_line`(产线)对齐:
|
||||
|
||||
1. `ops_shift`(班次报工库):
|
||||
- `shift_output(id, shift_date, line_code, actual_qty, good_qty, scrap_qty, run_hours, operator_phone)`
|
||||
其中 `line_code` 取值 `L1..L4`,`operator_phone` 为遮罩敏感列。
|
||||
- `energy_readings(id, shift_date, line_code, meter_kwh, settled_mwh)`
|
||||
用于核对跨量纲换算(千瓦时 vs 兆瓦时,`unit_factor = 1000`)的对账场景。
|
||||
2. `equip_master`(设备与停机主数据库):
|
||||
- `line_specs(equip_line_id, line_name, rated_hourly_qty, updated_day)`
|
||||
其中 `equip_line_id` 取值 `EQ-L1..EQ-L4`。
|
||||
- `downtime_events(id, event_date, equip_line_id, net_run_ratio, unplanned_min, fault_category)`
|
||||
记录各设备产线每日有效运转率与非计划停机分钟数。
|
||||
3. `quality_scrap`(质量报废台账库):
|
||||
- `scrap_ledger(id, inspect_date, workcenter_code, scrap_units, defect_family, inspector_id)`
|
||||
其中 `workcenter_code` 取值 `WC-01..WC-04`,`inspector_id` 为遮罩敏感列。
|
||||
|
||||
埋入的确定性效应(互不干扰):
|
||||
- **S1(契约越界 → 跨库主数据过期失配)**:`ops_shift` 中 `L3` 昨日实际每小时产量 `108` 件(8 小时产 864 件),但 `equip_master.line_specs`
|
||||
中 `EQ-L3` 的 `rated_hourly_qty` 仍停留在旧版 `80` 件/小时,导致负荷率算得 `135%`(违反 `[0, 100]` 契约上界)。
|
||||
- **S2a(跨源对账断更 → 对侧源停更早停)**:`quality_scrap.scrap_ledger` 截止于「今天 - 3 天」(最近两天断更,0 行),而 `ops_shift`
|
||||
照常落库;健康检查应识别 `date_stalled` 并在阶段一停止,不误判为业务报废清零。
|
||||
- **S2b(跨源量纲与超差对账)**:`ops_shift.energy_readings` 昨日 `L1..L3` 按 `1000 kWh = 1 MWh` 完全对齐,`L4` 抄表 `14800 kWh`
|
||||
但结算导入记为 `12.5 MWh`(换算后相差 `+2300 kWh`,`+18.4%`),`reconcile` 应判 `diverged`。
|
||||
- **S3(统计异动 → 切片定位 `L2` → 跨源设备停机同步异动 `EQ-L2`)**:昨日 `L2` 合格产量从常态约 `900` 跌至 `612`(`-32%`),
|
||||
穿透标准实体 `L2 -> EQ-L2` 后,`equip_master.downtime_events` 中 `EQ-L2` 非计划停机从常态 `11..14` 分钟飙升至 `96` 分钟,
|
||||
`cochange` 判 `co_anomalous`(仅陈述同期同步异动,不下因果断言)。
|
||||
- **对照 1(常规波动不误报)**:`L1` 过去 24 天合格产量在 `892..916` 窄幅波动,昨日 `898` 相对前日 `908` 属常规波动(`within_band`)。
|
||||
- **对照 2(主指标异动但上游正常 → `target_normal`)**:`今天 - 3 天` 时 `EQ-L4` 有效运转率从 `0.90` 跌至 `0.73`,但同期非计划停机
|
||||
仍为常态 `12` 分钟,`cochange` 判 `target_normal`,不强行归咎于设备非计划停机。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import random
|
||||
import sqlite3
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
DAYS = 24
|
||||
SEED = 20261022
|
||||
|
||||
SOURCE_OPS = "ops_shift"
|
||||
SOURCE_EQUIP = "equip_master"
|
||||
SOURCE_QUALITY = "quality_scrap"
|
||||
ALL_SOURCES = (SOURCE_OPS, SOURCE_EQUIP, SOURCE_QUALITY)
|
||||
|
||||
LINES: tuple[tuple[str, str, str, str], ...] = (
|
||||
("L1", "EQ-L1", "WC-01", "星梭一线"),
|
||||
("L2", "EQ-L2", "WC-02", "星梭二线"),
|
||||
("L3", "EQ-L3", "WC-03", "星梭三线"),
|
||||
("L4", "EQ-L4", "WC-04", "星梭四线"),
|
||||
)
|
||||
|
||||
OPS_SCHEMA = """
|
||||
CREATE TABLE shift_output (
|
||||
id INTEGER PRIMARY KEY,
|
||||
shift_date DATE NOT NULL,
|
||||
line_code TEXT NOT NULL,
|
||||
actual_qty INTEGER NOT NULL,
|
||||
good_qty INTEGER NOT NULL,
|
||||
scrap_qty INTEGER NOT NULL,
|
||||
run_hours REAL NOT NULL,
|
||||
operator_phone TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE energy_readings (
|
||||
id INTEGER PRIMARY KEY,
|
||||
shift_date DATE NOT NULL,
|
||||
line_code TEXT NOT NULL,
|
||||
meter_kwh REAL NOT NULL,
|
||||
settled_mwh REAL NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
EQUIP_SCHEMA = """
|
||||
CREATE TABLE line_specs (
|
||||
equip_line_id TEXT PRIMARY KEY,
|
||||
line_name TEXT NOT NULL,
|
||||
rated_hourly_qty REAL NOT NULL,
|
||||
updated_day DATE NOT NULL
|
||||
);
|
||||
CREATE TABLE downtime_events (
|
||||
id INTEGER PRIMARY KEY,
|
||||
event_date DATE NOT NULL,
|
||||
equip_line_id TEXT NOT NULL,
|
||||
net_run_ratio REAL NOT NULL,
|
||||
unplanned_min REAL NOT NULL,
|
||||
fault_category TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
QUALITY_SCHEMA = """
|
||||
CREATE TABLE scrap_ledger (
|
||||
id INTEGER PRIMARY KEY,
|
||||
inspect_date DATE NOT NULL,
|
||||
workcenter_code TEXT NOT NULL,
|
||||
scrap_units INTEGER NOT NULL,
|
||||
defect_family TEXT NOT NULL,
|
||||
inspector_id TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BuiltDatabases:
|
||||
today: date
|
||||
ops_path: Path
|
||||
equip_path: Path
|
||||
quality_path: Path
|
||||
|
||||
|
||||
def build(out_dir: Path | str, today: date | None = None) -> BuiltDatabases:
|
||||
"""在 out_dir 下生成 ops_shift.db、equip_master.db、quality_scrap.db(已存在则覆盖)。"""
|
||||
root = Path(out_dir)
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
today = today or date(2026, 10, 22)
|
||||
rng = random.Random(SEED)
|
||||
|
||||
ops_path = root / f"{SOURCE_OPS}.db"
|
||||
equip_path = root / f"{SOURCE_EQUIP}.db"
|
||||
quality_path = root / f"{SOURCE_QUALITY}.db"
|
||||
for p in (ops_path, equip_path, quality_path):
|
||||
if p.exists():
|
||||
p.unlink()
|
||||
|
||||
yesterday = today - timedelta(days=1)
|
||||
control2_day = yesterday - timedelta(days=2)
|
||||
stalled_after = yesterday - timedelta(days=2) # quality_scrap 停在 yesterday - 2 天,最近两天缺失
|
||||
days = [today - timedelta(days=offset) for offset in range(DAYS, 0, -1)]
|
||||
|
||||
shift_rows: list[tuple[Any, ...]] = []
|
||||
energy_rows: list[tuple[Any, ...]] = []
|
||||
downtime_rows: list[tuple[Any, ...]] = []
|
||||
scrap_rows: list[tuple[Any, ...]] = []
|
||||
|
||||
base_good = {"L1": 904, "L2": 900, "L3": 844, "L4": 888}
|
||||
base_scrap = {"L1": 18, "L2": 20, "L3": 19, "L4": 17}
|
||||
phones = {
|
||||
"L1": "138-0010-0101",
|
||||
"L2": "138-0010-0102",
|
||||
"L3": "138-0010-0103",
|
||||
"L4": "138-0010-0104",
|
||||
}
|
||||
inspectors = {
|
||||
"WC-01": "INSP-9001",
|
||||
"WC-02": "INSP-9002",
|
||||
"WC-03": "INSP-9003",
|
||||
"WC-04": "INSP-9004",
|
||||
}
|
||||
|
||||
shift_id = 1
|
||||
energy_id = 1
|
||||
downtime_id = 1
|
||||
scrap_id = 1
|
||||
|
||||
for idx, d in enumerate(days):
|
||||
ds = d.isoformat()
|
||||
wave = (-1 if idx % 2 else 1) * (4 + (idx % 3) * 2) # 确定性小幅波动 ±4..±8
|
||||
for line_code, equip_id, wc_code, _ in LINES:
|
||||
noise = rng.choice((-3, -1, 1, 3))
|
||||
good = base_good[line_code] + wave + noise
|
||||
scrap = base_scrap[line_code] + (idx % 3) - 1
|
||||
run_hours = 8.0
|
||||
net_ratio = round(0.905 + ((idx % 4) - 1.5) * 0.006 + noise * 0.001, 4)
|
||||
unplanned = float(12 + (-1 if idx % 2 else 1) * (1 + (idx % 3)) + (0.5 if noise > 0 else -0.5))
|
||||
fault_cat = "routine_check"
|
||||
|
||||
# 对照 1:L1 末尾两日固定为平缓常规波动(前日 908 → 昨日 898,-1.1%)
|
||||
if line_code == "L1" and d == yesterday - timedelta(days=1):
|
||||
good = 908
|
||||
elif line_code == "L1" and d == yesterday:
|
||||
good = 898
|
||||
|
||||
# S1:L3 昨日实际产率 108 件/小时(864 件 / 8 小时),对比 equip_master 过期额定 80 件/小时 = 135%
|
||||
if line_code == "L3" and d == yesterday:
|
||||
good = 844
|
||||
scrap = 20
|
||||
|
||||
# S3:L2 昨日产率显著下跌,同时 EQ-L2 非计划停机飙升至 96 分钟(统计伴随异动)
|
||||
if line_code == "L2" and d == yesterday:
|
||||
good = 612
|
||||
net_ratio = 0.6320
|
||||
unplanned = 96.0
|
||||
fault_cat = "feeder_jam"
|
||||
|
||||
# 对照 2:EQ-L4 在 control2_day 有效运转率明显下跌,但非计划停机保持常态 12 分钟(target_normal)
|
||||
if line_code == "L4" and d == control2_day:
|
||||
good = 720
|
||||
net_ratio = 0.7280
|
||||
unplanned = 12.0
|
||||
fault_cat = "routine_check"
|
||||
|
||||
actual = good + scrap
|
||||
shift_rows.append((shift_id, ds, line_code, actual, good, scrap, run_hours, phones[line_code]))
|
||||
shift_id += 1
|
||||
|
||||
# 能耗表:昨日 L4 出现结算导入偏差(14800 kWh vs 12.5 MWh),其余对齐
|
||||
kwh = round(actual * 14.0, 1)
|
||||
mwh = round(kwh / 1000.0, 4)
|
||||
if line_code == "L4" and d == yesterday:
|
||||
kwh = 14800.0
|
||||
mwh = 12.5
|
||||
elif d == yesterday:
|
||||
kwh = 12000.0 + (LINES.index((line_code, equip_id, wc_code, _)) * 500.0)
|
||||
mwh = round(kwh / 1000.0, 4)
|
||||
energy_rows.append((energy_id, ds, line_code, kwh, mwh))
|
||||
energy_id += 1
|
||||
|
||||
downtime_rows.append((downtime_id, ds, equip_id, net_ratio, unplanned, fault_cat))
|
||||
downtime_id += 1
|
||||
|
||||
# S2a:quality_scrap 在 stalled_after 之后断更(最近两天 0 行)
|
||||
if d <= stalled_after:
|
||||
scrap_rows.append((scrap_id, ds, wc_code, scrap, "surface_burr", inspectors[wc_code]))
|
||||
scrap_id += 1
|
||||
|
||||
specs_rows = [
|
||||
("EQ-L1", "星梭一线", 120.0, (today - timedelta(days=30)).isoformat()),
|
||||
("EQ-L2", "星梭二线", 120.0, (today - timedelta(days=30)).isoformat()),
|
||||
# S1 根因:EQ-L3 技改提速后主数据未更新,仍为 80 件/小时
|
||||
("EQ-L3", "星梭三线", 80.0, "2026-06-01"),
|
||||
("EQ-L4", "星梭四线", 120.0, (today - timedelta(days=30)).isoformat()),
|
||||
]
|
||||
|
||||
ops_db = sqlite3.connect(ops_path)
|
||||
ops_db.executescript(OPS_SCHEMA)
|
||||
ops_db.executemany("INSERT INTO shift_output VALUES (?, ?, ?, ?, ?, ?, ?, ?)", shift_rows)
|
||||
ops_db.executemany("INSERT INTO energy_readings VALUES (?, ?, ?, ?, ?)", energy_rows)
|
||||
ops_db.commit()
|
||||
ops_db.close()
|
||||
|
||||
equip_db = sqlite3.connect(equip_path)
|
||||
equip_db.executescript(EQUIP_SCHEMA)
|
||||
equip_db.executemany("INSERT INTO line_specs VALUES (?, ?, ?, ?)", specs_rows)
|
||||
equip_db.executemany("INSERT INTO downtime_events VALUES (?, ?, ?, ?, ?, ?)", downtime_rows)
|
||||
equip_db.commit()
|
||||
equip_db.close()
|
||||
|
||||
quality_db = sqlite3.connect(quality_path)
|
||||
quality_db.executescript(QUALITY_SCHEMA)
|
||||
quality_db.executemany("INSERT INTO scrap_ledger VALUES (?, ?, ?, ?, ?, ?)", scrap_rows)
|
||||
quality_db.commit()
|
||||
quality_db.close()
|
||||
|
||||
return BuiltDatabases(today=today, ops_path=ops_path, equip_path=equip_path, quality_path=quality_path)
|
||||
|
||||
|
||||
async def seed_ontology_fixture(
|
||||
tmp_path: Path,
|
||||
scratch_source: Any,
|
||||
*,
|
||||
today: date | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""登记三源 SQLite、标准实体 `production_line`、断更变更记录及三条跨源已发布口径工作流。"""
|
||||
from app.api.workflows import commit_publish
|
||||
from app.data import source_changes as sc
|
||||
from app.data.entities import create_entity, entity_to_dict
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Workflow, WorkflowVersion
|
||||
|
||||
built = build(tmp_path, today=today)
|
||||
ops_id = await scratch_source(
|
||||
SOURCE_OPS, built.ops_path, options={"mask_columns": ["operator_phone"]}, schema=True
|
||||
)
|
||||
equip_id = await scratch_source(SOURCE_EQUIP, built.equip_path, schema=True)
|
||||
quality_id = await scratch_source(
|
||||
SOURCE_QUALITY, built.quality_path, options={"mask_columns": ["inspector_id"]}, schema=True
|
||||
)
|
||||
|
||||
stalled_day = (built.today - timedelta(days=3)).isoformat()
|
||||
async with SessionLocal() as session:
|
||||
quality_src = await session.get(DataSource, quality_id)
|
||||
await sc.record(
|
||||
session,
|
||||
quality_src,
|
||||
sc.NewChange(
|
||||
kind=sc.DATE_STALLED,
|
||||
table="scrap_ledger",
|
||||
column="inspect_date",
|
||||
severity="action",
|
||||
before={"max_date": stalled_day},
|
||||
after={"max_date": stalled_day, "missing_days": 2},
|
||||
key=stalled_day,
|
||||
),
|
||||
sc.Detected(job_kind="profile", trigger="manual", actor="fixture_builder"),
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
async with SessionLocal() as session:
|
||||
# 建三条分属不同数据源的口径工作流:先落基础工作流以拿到固定 workflow_id,再互相挂本体边
|
||||
wf_equip = Workflow(name="设备主数据与停机口径", description="设备额定产能与非计划停机口径", graph={"nodes": [], "edges": []})
|
||||
wf_quality = Workflow(name="质量报废台账口径", description="质量检验报废台账口径", graph={"nodes": [], "edges": []})
|
||||
wf_ops = Workflow(name="班次产率与负荷率口径", description="班次合格产率、负荷率与报工报废口径", graph={"nodes": [], "edges": []})
|
||||
session.add_all([wf_equip, wf_quality, wf_ops])
|
||||
await session.flush()
|
||||
|
||||
equip_graph = {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "in_eq",
|
||||
"type": "input",
|
||||
"position": {"x": 0, "y": 0},
|
||||
"data": {
|
||||
"label": "入口",
|
||||
"config": {
|
||||
"fields": [
|
||||
{"name": "equip_line", "label": "设备产线", "type": "text", "default": "EQ-L3"},
|
||||
{"name": "stat_day", "label": "统计日期", "type": "text", "default": (built.today - timedelta(days=1)).isoformat()},
|
||||
]
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "q_spec",
|
||||
"type": "tool",
|
||||
"position": {"x": 220, "y": 0},
|
||||
"data": {
|
||||
"label": "查额定产能",
|
||||
"config": {
|
||||
"tool": f"db_query__{SOURCE_EQUIP}",
|
||||
"args": {
|
||||
"sql": "SELECT equip_line_id, rated_hourly_qty FROM line_specs WHERE equip_line_id = '{{ input.equip_line }}'",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "q_down",
|
||||
"type": "tool",
|
||||
"position": {"x": 220, "y": 140},
|
||||
"data": {
|
||||
"label": "查停机时长",
|
||||
"config": {
|
||||
"tool": f"db_query__{SOURCE_EQUIP}",
|
||||
"args": {
|
||||
"sql": "SELECT event_date, unplanned_min FROM downtime_events WHERE equip_line_id = '{{ input.equip_line }}' AND event_date = '{{ input.stat_day }}'",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "m_equip",
|
||||
"type": "metrics",
|
||||
"position": {"x": 480, "y": 60},
|
||||
"data": {
|
||||
"label": "设备指标卡",
|
||||
"config": {
|
||||
"caliber": "设备主数据与非计划停机日口径",
|
||||
"metrics": [
|
||||
{
|
||||
"id": "rated_hourly_qty",
|
||||
"name": "额定每小时产量",
|
||||
"expression": "cell(nodes.q_spec, 0, 'rated_hourly_qty')",
|
||||
"unit": "件/小时",
|
||||
"semantic": {
|
||||
"entities": [{"name": "产线", "key": "equip_line_id", "entity_key": "production_line"}]
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "unplanned_downtime_min",
|
||||
"name": "非计划停机时长",
|
||||
"expression": "cell(nodes.q_down, 0, 'unplanned_min')",
|
||||
"unit": "分钟",
|
||||
"semantic": {
|
||||
"grain": "day",
|
||||
"time": {"column": "event_date", "timezone": "Asia/Shanghai"},
|
||||
"entities": [{"name": "产线", "key": "equip_line_id", "entity_key": "production_line"}],
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{"id": "out_eq", "type": "output", "position": {"x": 720, "y": 60}, "data": {"label": "出口", "config": {}}},
|
||||
],
|
||||
"edges": [
|
||||
{"source": "in_eq", "target": "q_spec"},
|
||||
{"source": "in_eq", "target": "q_down"},
|
||||
{"source": "q_spec", "target": "m_equip"},
|
||||
{"source": "q_down", "target": "m_equip"},
|
||||
{"source": "m_equip", "target": "out_eq"},
|
||||
],
|
||||
}
|
||||
|
||||
quality_graph = {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "in_q",
|
||||
"type": "input",
|
||||
"position": {"x": 0, "y": 0},
|
||||
"data": {
|
||||
"label": "入口",
|
||||
"config": {
|
||||
"fields": [
|
||||
{"name": "workcenter", "label": "工作中心", "type": "text", "default": "WC-02"},
|
||||
{"name": "inspect_day", "label": "检验日期", "type": "text", "default": (built.today - timedelta(days=1)).isoformat()},
|
||||
]
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "q_scrap",
|
||||
"type": "tool",
|
||||
"position": {"x": 240, "y": 0},
|
||||
"data": {
|
||||
"label": "查质量报废",
|
||||
"config": {
|
||||
"tool": f"db_query__{SOURCE_QUALITY}",
|
||||
"args": {
|
||||
"sql": "SELECT COALESCE(SUM(scrap_units), 0) AS scrap_units FROM scrap_ledger WHERE workcenter_code = '{{ input.workcenter }}' AND inspect_date = '{{ input.inspect_day }}'",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "m_quality",
|
||||
"type": "metrics",
|
||||
"position": {"x": 480, "y": 0},
|
||||
"data": {
|
||||
"label": "质量报废卡",
|
||||
"config": {
|
||||
"caliber": "质量检验台账日报废口径",
|
||||
"metrics": [
|
||||
{
|
||||
"id": "ledger_scrap_units",
|
||||
"name": "台账报废件数",
|
||||
"expression": "cell(nodes.q_scrap, 0, 'scrap_units')",
|
||||
"unit": "件",
|
||||
"semantic": {
|
||||
"grain": "day",
|
||||
"time": {"column": "inspect_date", "timezone": "Asia/Shanghai"},
|
||||
"entities": [{"name": "产线", "key": "workcenter_code", "entity_key": "production_line"}],
|
||||
},
|
||||
}
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{"id": "out_q", "type": "output", "position": {"x": 720, "y": 0}, "data": {"label": "出口", "config": {}}},
|
||||
],
|
||||
"edges": [
|
||||
{"source": "in_q", "target": "q_scrap"},
|
||||
{"source": "q_scrap", "target": "m_quality"},
|
||||
{"source": "m_quality", "target": "out_q"},
|
||||
],
|
||||
}
|
||||
|
||||
ops_graph = {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "in_ops",
|
||||
"type": "input",
|
||||
"position": {"x": 0, "y": 0},
|
||||
"data": {
|
||||
"label": "入口",
|
||||
"config": {
|
||||
"fields": [
|
||||
{"name": "line_code", "label": "产线编码", "type": "text", "default": "L3"},
|
||||
{"name": "shift_day", "label": "班次日期", "type": "text", "default": (built.today - timedelta(days=1)).isoformat()},
|
||||
]
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "q_ops",
|
||||
"type": "tool",
|
||||
"position": {"x": 240, "y": 0},
|
||||
"data": {
|
||||
"label": "查班次产出",
|
||||
"config": {
|
||||
"tool": f"db_query__{SOURCE_OPS}",
|
||||
"args": {
|
||||
"sql": "SELECT actual_qty, good_qty, scrap_qty, run_hours FROM shift_output WHERE line_code = '{{ input.line_code }}' AND shift_date = '{{ input.shift_day }}'",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "m_ops",
|
||||
"type": "metrics",
|
||||
"position": {"x": 500, "y": 0},
|
||||
"data": {
|
||||
"label": "班次指标卡",
|
||||
"config": {
|
||||
"caliber": "班次负荷率、合格产量与报工报废口径",
|
||||
"metrics": [
|
||||
{
|
||||
"id": "load_rate_pct",
|
||||
"name": "产线负荷率",
|
||||
"expression": "round((cell(nodes.q_ops, 0, 'actual_qty') / cell(nodes.q_ops, 0, 'run_hours')) / 80.0 * 100, 2)",
|
||||
"unit": "%",
|
||||
"contract": {"minimum": 0, "maximum": 100, "unit": "%"},
|
||||
"semantic": {
|
||||
"grain": "day",
|
||||
"time": {"column": "shift_date", "timezone": "Asia/Shanghai"},
|
||||
"entities": [{"name": "产线", "key": "line_code", "entity_key": "production_line"}],
|
||||
},
|
||||
"links": [
|
||||
{
|
||||
"kind": "factor",
|
||||
"target": {
|
||||
"workflow_id": wf_equip.id,
|
||||
"card_id": "m_equip",
|
||||
"metric_id": "rated_hourly_qty",
|
||||
},
|
||||
"role": "denominator",
|
||||
"entity_key": "production_line",
|
||||
"status": "confirmed",
|
||||
"rationale": "负荷率分母取设备主数据的额定每小时产量",
|
||||
},
|
||||
{
|
||||
"kind": "upstream_driver",
|
||||
"target": {
|
||||
"workflow_id": wf_equip.id,
|
||||
"card_id": "m_equip",
|
||||
"metric_id": "unplanned_downtime_min",
|
||||
},
|
||||
"entity_key": "production_line",
|
||||
"status": "proposed",
|
||||
"rationale": "候选未确认边:验证运行期不会自动沿 proposed 边穿透",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "good_output_qty",
|
||||
"name": "班次合格产量",
|
||||
"expression": "cell(nodes.q_ops, 0, 'good_qty')",
|
||||
"unit": "件",
|
||||
"semantic": {
|
||||
"grain": "day",
|
||||
"time": {"column": "shift_date", "timezone": "Asia/Shanghai"},
|
||||
"entities": [{"name": "产线", "key": "line_code", "entity_key": "production_line"}],
|
||||
},
|
||||
"links": [
|
||||
{
|
||||
"kind": "upstream_driver",
|
||||
"target": {
|
||||
"workflow_id": wf_equip.id,
|
||||
"card_id": "m_equip",
|
||||
"metric_id": "unplanned_downtime_min",
|
||||
},
|
||||
"entity_key": "production_line",
|
||||
"lag_periods": 0,
|
||||
"status": "confirmed",
|
||||
"rationale": "设备非计划停机时长升高会同步压缩班次有效产出时间",
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
"id": "shift_scrap_qty",
|
||||
"name": "报工报废件数",
|
||||
"expression": "cell(nodes.q_ops, 0, 'scrap_qty')",
|
||||
"unit": "件",
|
||||
"semantic": {
|
||||
"grain": "day",
|
||||
"time": {"column": "shift_date", "timezone": "Asia/Shanghai"},
|
||||
"entities": [{"name": "产线", "key": "line_code", "entity_key": "production_line"}],
|
||||
},
|
||||
"links": [
|
||||
{
|
||||
"kind": "reconciles_with",
|
||||
"target": {
|
||||
"workflow_id": wf_quality.id,
|
||||
"card_id": "m_quality",
|
||||
"metric_id": "ledger_scrap_units",
|
||||
},
|
||||
"entity_key": "production_line",
|
||||
"rel_tol": 0.02,
|
||||
"status": "confirmed",
|
||||
"rationale": "班次报工报废数应与质量检验台账日报废件数在 2% 容差内对齐",
|
||||
}
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{"id": "out_ops", "type": "output", "position": {"x": 740, "y": 0}, "data": {"label": "出口", "config": {}}},
|
||||
],
|
||||
"edges": [
|
||||
{"source": "in_ops", "target": "q_ops"},
|
||||
{"source": "q_ops", "target": "m_ops"},
|
||||
{"source": "m_ops", "target": "out_ops"},
|
||||
],
|
||||
}
|
||||
|
||||
snapshots: list[tuple[Workflow, WorkflowVersion]] = []
|
||||
for wf, graph in ((wf_equip, equip_graph), (wf_quality, quality_graph), (wf_ops, ops_graph)):
|
||||
wf.graph = graph
|
||||
wf.version = 1
|
||||
snap = WorkflowVersion(workflow_id=wf.id, version=1, graph=graph, note="初始版本")
|
||||
session.add(snap)
|
||||
snapshots.append((wf, snap))
|
||||
# 先提交未写锁的草稿行,再逐一走 commit_publish(避免 SQLite 单写锁下嵌套会话写工件抢锁)
|
||||
await session.commit()
|
||||
|
||||
for wf, snap in snapshots:
|
||||
await session.refresh(wf)
|
||||
await session.refresh(snap)
|
||||
await commit_publish(session, wf, snap, "published", "fixture_builder")
|
||||
|
||||
entity_row = await create_entity(
|
||||
session,
|
||||
{
|
||||
"key": "production_line",
|
||||
"label": "产线",
|
||||
"description": "星梭示范装配厂标准产线实体,桥接班次报工、设备主数据与质量报废台账三套异构编码",
|
||||
"aliases": ["装配产线", "车间产线", "工作中心"],
|
||||
"bindings": [
|
||||
{
|
||||
"source_name": SOURCE_OPS,
|
||||
"table_name": "shift_output",
|
||||
"column_name": "line_code",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": SOURCE_OPS,
|
||||
"table_name": "energy_readings",
|
||||
"column_name": "line_code",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": SOURCE_EQUIP,
|
||||
"table_name": "line_specs",
|
||||
"column_name": "equip_line_id",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": SOURCE_EQUIP,
|
||||
"table_name": "downtime_events",
|
||||
"column_name": "equip_line_id",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": SOURCE_QUALITY,
|
||||
"table_name": "scrap_ledger",
|
||||
"column_name": "workcenter_code",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": SOURCE_QUALITY,
|
||||
"table_name": "unverified_archive",
|
||||
"column_name": "legacy_line",
|
||||
"status": "proposed",
|
||||
},
|
||||
],
|
||||
"caliber_params": [
|
||||
{"workflow_id": wf_ops.id, "param_name": "line_code", "status": "confirmed"},
|
||||
{"workflow_id": wf_equip.id, "param_name": "equip_line", "status": "confirmed"},
|
||||
{"workflow_id": wf_quality.id, "param_name": "workcenter", "status": "confirmed"},
|
||||
],
|
||||
"members": [
|
||||
{
|
||||
"value": code,
|
||||
"label": name,
|
||||
"aliases": [code, equip_id, wc_code],
|
||||
"source_codes": {SOURCE_OPS: code, SOURCE_EQUIP: equip_id, SOURCE_QUALITY: wc_code},
|
||||
}
|
||||
for code, equip_id, wc_code, name in LINES
|
||||
],
|
||||
},
|
||||
actor="fixture_builder",
|
||||
)
|
||||
entity_dict = entity_to_dict(entity_row)
|
||||
await session.commit()
|
||||
|
||||
return {
|
||||
"today": built.today,
|
||||
"paths": {"ops": built.ops_path, "equip": built.equip_path, "quality": built.quality_path},
|
||||
"source_ids": {SOURCE_OPS: ops_id, SOURCE_EQUIP: equip_id, SOURCE_QUALITY: quality_id},
|
||||
"workflow_ids": {"ops": wf_ops.id, "equip": wf_equip.id, "quality": wf_quality.id},
|
||||
"entity_id": entity_dict["id"],
|
||||
"entity": entity_dict,
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="生成跨源指标本体穿透用的三源虚构制造库")
|
||||
parser.add_argument("out_dir")
|
||||
parser.add_argument("--today", type=date.fromisoformat, default=None)
|
||||
args = parser.parse_args()
|
||||
built = build(args.out_dir, args.today)
|
||||
print(f"{built.ops_path}\n{built.equip_path}\n{built.quality_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+1
-1
@@ -13,7 +13,7 @@ from app.analysis.testing import fake_result
|
||||
|
||||
#: 每个算子缺省给的判定(fake_result 有现成判定语的那几个)
|
||||
VERDICT = {"decompose": "driver", "baseline": "outside_band", "trend": "rising", "differ": "no_evidence",
|
||||
"comparable": "comparable"}
|
||||
"comparable": "comparable", "reconcile": "diverged", "cochange": "co_anomalous"}
|
||||
|
||||
|
||||
class FakeOps:
|
||||
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
{
|
||||
"format": "agentlab-eval-suite",
|
||||
"version": 1,
|
||||
"exported_at": "2026-10-22T00:00:00+00:00",
|
||||
"suite": {
|
||||
"name": "跨源指标本体穿透题集",
|
||||
"description": "配 tests/fixtures/acceptance/multi_source_ontology.py 的三源虚构制造库(ops_shift、equip_master、quality_scrap)与标准实体 production_line:覆盖契约越界穿透主数据失配(S1)、对账源断更早停(S2a)、量纲换算与超差对账(S2b)、切片定位加跨源设备停机伴随异动(S3),以及常规波动不误报(对照 1)、主指标异动但上游正常不乱归因(对照 2)。全部期望由 SQL 探针与平台确定性算子重算,零人工填数",
|
||||
"timezone": "Asia/Shanghai",
|
||||
"budget": {}
|
||||
},
|
||||
"cases": [
|
||||
{
|
||||
"seq": 1,
|
||||
"kind": "ask",
|
||||
"question": "为什么昨天 L3 产线的负荷率超过了 100%?请沿指标本体排查是不是关联主数据出了问题。",
|
||||
"datasources": ["ops_shift", "equip_master"],
|
||||
"tags": ["本体穿透", "S1", "契约越界", "主数据失配"],
|
||||
"gold": {
|
||||
"kind": "sql",
|
||||
"datasource": "equip_master",
|
||||
"sql": "SELECT equip_line_id AS 设备产线, rated_hourly_qty AS 额定每小时产量 FROM line_specs WHERE equip_line_id = 'EQ-L3'"
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"id": "stale_spec",
|
||||
"kind": "scalar",
|
||||
"column": "额定每小时产量",
|
||||
"row": 0,
|
||||
"label": "穿透定位到 EQ-L3 在设备主数据中的过期额定每小时产量"
|
||||
},
|
||||
{
|
||||
"id": "inv_s1",
|
||||
"kind": "investigation",
|
||||
"status": "complete",
|
||||
"minimum_depth": 1,
|
||||
"min_ontology_hops": 1,
|
||||
"diagnosis_stage": "ontology_penetration",
|
||||
"label": "完成至少 1 跳跨源本体穿透定位因子分母"
|
||||
}
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"seq": 2,
|
||||
"kind": "ask",
|
||||
"question": "对比班次报工和质量报废台账,昨天各产线的报废数为什么对不上?是业务真变了还是数据源异常?",
|
||||
"datasources": ["ops_shift", "quality_scrap"],
|
||||
"tags": ["本体穿透", "S2a", "跨源对账", "数据源断更"],
|
||||
"gold": {
|
||||
"kind": "sql",
|
||||
"datasource": "quality_scrap",
|
||||
"sql": "SELECT workcenter_code AS 工作中心, scrap_units AS 报废件数 FROM scrap_ledger WHERE inspect_date = '{{ schedule.yesterday }}'"
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"id": "stalled_zero_rows",
|
||||
"kind": "none",
|
||||
"label": "质量报废台账昨天断更无记录(0 行)"
|
||||
},
|
||||
{
|
||||
"id": "inv_s2a",
|
||||
"kind": "investigation",
|
||||
"status": "complete",
|
||||
"require_source_health": true,
|
||||
"diagnosis_stage": "health_and_contract",
|
||||
"label": "先核查关联数据源健康度并在阶段一识别断更停止"
|
||||
}
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"seq": 3,
|
||||
"kind": "ask",
|
||||
"question": "核对昨天各产线能耗的抄表千瓦时与结算兆瓦时(1 兆瓦时 = 1000 千瓦时),换算后两侧对账是否一致?",
|
||||
"datasources": ["ops_shift"],
|
||||
"tags": ["本体穿透", "S2b", "跨源对账", "量纲换算"],
|
||||
"gold": {
|
||||
"kind": "sql",
|
||||
"datasource": "ops_shift",
|
||||
"sql": "SELECT line_code AS 产线, meter_kwh AS 抄表千瓦时, settled_mwh AS 结算兆瓦时 FROM energy_readings WHERE shift_date = '{{ schedule.yesterday }}' ORDER BY line_code"
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"id": "reconcile_energy",
|
||||
"kind": "verdict",
|
||||
"op": "reconcile",
|
||||
"params": {
|
||||
"by": ["产线"],
|
||||
"measure": "抄表千瓦时",
|
||||
"target_measure": "结算兆瓦时",
|
||||
"unit_factor": 1000,
|
||||
"label": "产线能耗"
|
||||
},
|
||||
"label": "按 1000 倍量纲换算后判定能耗对账存在显著超差"
|
||||
}
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"seq": 4,
|
||||
"kind": "ask",
|
||||
"question": "昨天产线综合合格产量明显下滑,请先拆解定位哪条产线拖累,再沿本体穿透上游设备数据源检验是否有同步异动。",
|
||||
"datasources": ["ops_shift", "equip_master"],
|
||||
"tags": ["本体穿透", "S3", "切片定位", "伴随异动"],
|
||||
"gold": {
|
||||
"kind": "sql",
|
||||
"datasource": "equip_master",
|
||||
"sql": "SELECT event_date AS 日期, net_run_ratio AS 有效运转率, unplanned_min AS 非计划停机分钟 FROM downtime_events WHERE equip_line_id = 'EQ-L2' AND event_date BETWEEN date('{{ schedule.yesterday }}', '-19 days') AND '{{ schedule.yesterday }}' ORDER BY event_date"
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"id": "cochange_l2",
|
||||
"kind": "verdict",
|
||||
"op": "cochange",
|
||||
"params": {
|
||||
"period": "日期",
|
||||
"measure": "有效运转率",
|
||||
"driver_measure": "非计划停机分钟",
|
||||
"target": "{{ schedule.yesterday }}",
|
||||
"label": "有效运转率"
|
||||
},
|
||||
"label": "EQ-L2 有效运转率下滑与非计划停机分钟飙升呈同期伴随异动"
|
||||
},
|
||||
{
|
||||
"id": "inv_s3",
|
||||
"kind": "investigation",
|
||||
"status": "complete",
|
||||
"minimum_depth": 1,
|
||||
"min_ontology_hops": 1,
|
||||
"require_source_health": true,
|
||||
"diagnosis_stage": "ontology_penetration",
|
||||
"label": "经健康核查后完成跨源本体穿透检验"
|
||||
}
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"seq": 5,
|
||||
"kind": "ask",
|
||||
"question": "昨天 L1 产线的合格产量比前一天略少一点,这算异常波动吗?",
|
||||
"datasources": ["ops_shift"],
|
||||
"tags": ["本体穿透", "对照", "常规波动"],
|
||||
"gold": {
|
||||
"kind": "sql",
|
||||
"datasource": "ops_shift",
|
||||
"sql": "SELECT shift_date AS 班次日期, good_qty AS 合格产量 FROM shift_output WHERE line_code = 'L1' AND shift_date BETWEEN date('{{ schedule.yesterday }}', '-19 days') AND '{{ schedule.yesterday }}' ORDER BY shift_date"
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"id": "normal_l1",
|
||||
"kind": "verdict",
|
||||
"op": "baseline",
|
||||
"params": {
|
||||
"period": "班次日期",
|
||||
"measure": "合格产量",
|
||||
"label": "L1合格产量"
|
||||
},
|
||||
"label": "L1 昨日波动处于历史常规波动带内,不误报异常"
|
||||
}
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"seq": 6,
|
||||
"kind": "ask",
|
||||
"question": "三天前 L4 产线的有效运转率出现明显下滑,是不是上游设备非计划停机异常引起的?",
|
||||
"datasources": ["ops_shift", "equip_master"],
|
||||
"tags": ["本体穿透", "对照", "排除上游"],
|
||||
"gold": {
|
||||
"kind": "sql",
|
||||
"datasource": "equip_master",
|
||||
"sql": "SELECT event_date AS 日期, net_run_ratio AS 有效运转率, unplanned_min AS 非计划停机分钟 FROM downtime_events WHERE equip_line_id = 'EQ-L4' AND event_date BETWEEN date('{{ schedule.yesterday }}', '-21 days') AND date('{{ schedule.yesterday }}', '-2 days') ORDER BY event_date"
|
||||
},
|
||||
"expectations": [
|
||||
{
|
||||
"id": "upstream_normal_l4",
|
||||
"kind": "verdict",
|
||||
"op": "cochange",
|
||||
"params": {
|
||||
"period": "日期",
|
||||
"measure": "有效运转率",
|
||||
"driver_measure": "非计划停机分钟",
|
||||
"label": "有效运转率"
|
||||
},
|
||||
"label": "主指标超出常规波动但同期设备非计划停机处于常规波动范围内(target_normal)"
|
||||
}
|
||||
],
|
||||
"enabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,823 @@
|
||||
"""标准业务实体 Agent 化治理单元测试(E1 + E2):附件/知识库联动、增量补丁护栏、交互式实体助手 SSE 与后台扫描发现任务。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import sqlite3
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from langchain_core.messages import AIMessage, AIMessageChunk
|
||||
from sqlalchemy import delete, select
|
||||
|
||||
from app.data import catalog_assistant, entities, entity_assistant, entity_discovery, entity_docs, entity_patch
|
||||
from app.data.engine import CATALOG_PROFILE_OPTION, MASK_COLUMNS_OPTION
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Chunk, Document, Workflow
|
||||
from app.db.models_ontology import CanonicalEntity, EntityDiscoveryJob
|
||||
from app.main import app
|
||||
from tests.fixtures.sources import forget_source, sqlite_source
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Turn:
|
||||
text: list[str] = field(default_factory=list)
|
||||
calls: list[tuple[str, dict[str, Any]]] = field(default_factory=list)
|
||||
usage: tuple[int, int] = (80, 30)
|
||||
|
||||
|
||||
class _ScriptedModel:
|
||||
def __init__(self, turns: list[_Turn] | None = None, *, invoke_reply: str = "") -> None:
|
||||
self.turns = list(turns or [])
|
||||
self.invoke_reply = invoke_reply
|
||||
self.seen: list[list[Any]] = []
|
||||
self.tools: list[Any] | None = None
|
||||
|
||||
def bind_tools(self, tools: list[Any], **_: Any) -> "_ScriptedModel":
|
||||
self.tools = tools
|
||||
return self
|
||||
|
||||
async def astream(self, messages: list[Any]):
|
||||
self.seen.append(list(messages))
|
||||
turn = self.turns.pop(0)
|
||||
for piece in turn.text:
|
||||
yield AIMessageChunk(content=piece)
|
||||
for i, (name, args) in enumerate(turn.calls):
|
||||
yield AIMessageChunk(
|
||||
content="",
|
||||
tool_call_chunks=[
|
||||
{
|
||||
"name": name,
|
||||
"args": json.dumps(args, ensure_ascii=False),
|
||||
"id": f"tc_{len(self.seen)}_{i}",
|
||||
"index": i,
|
||||
}
|
||||
],
|
||||
)
|
||||
yield AIMessageChunk(
|
||||
content="",
|
||||
usage_metadata={
|
||||
"input_tokens": turn.usage[0],
|
||||
"output_tokens": turn.usage[1],
|
||||
"total_tokens": sum(turn.usage),
|
||||
},
|
||||
response_metadata={"finish_reason": "stop"},
|
||||
)
|
||||
|
||||
async def ainvoke(self, messages: list[Any]) -> AIMessage:
|
||||
self.seen.append(list(messages))
|
||||
return AIMessage(content=self.invoke_reply)
|
||||
|
||||
|
||||
async def _make_sqlite_source(
|
||||
tmp_path: Path,
|
||||
*,
|
||||
name: str,
|
||||
setup_sql: str,
|
||||
profiling: bool = True,
|
||||
mask_columns: str = "",
|
||||
) -> str:
|
||||
db_file = tmp_path / f"{name}_{uuid.uuid4().hex[:6]}.db"
|
||||
conn = sqlite3.connect(db_file)
|
||||
try:
|
||||
conn.executescript(setup_sql)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
options: dict[str, Any] = {CATALOG_PROFILE_OPTION: {"enabled": profiling}}
|
||||
if mask_columns:
|
||||
options[MASK_COLUMNS_OPTION] = mask_columns
|
||||
|
||||
return await sqlite_source(name, db_file, options=options, schema=True)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def multi_source_env(tmp_path: Path):
|
||||
mes_name = f"mes_ag_{uuid.uuid4().hex[:6]}"
|
||||
erp_name = f"erp_ag_{uuid.uuid4().hex[:6]}"
|
||||
wf_id = f"wf_ag_{uuid.uuid4().hex[:6]}"
|
||||
|
||||
mes_id = await _make_sqlite_source(
|
||||
tmp_path,
|
||||
name=mes_name,
|
||||
setup_sql="""
|
||||
CREATE TABLE dwd_workorder (
|
||||
line_code TEXT,
|
||||
line_name TEXT,
|
||||
operator_phone TEXT,
|
||||
good_qty INTEGER
|
||||
);
|
||||
INSERT INTO dwd_workorder VALUES
|
||||
('L1', '一号产线', '13900000011', 100),
|
||||
('L2', '二号产线', '13900000022', 95);
|
||||
""",
|
||||
profiling=True,
|
||||
mask_columns="operator_phone",
|
||||
)
|
||||
erp_id = await _make_sqlite_source(
|
||||
tmp_path,
|
||||
name=erp_name,
|
||||
setup_sql="""
|
||||
CREATE TABLE dwd_settlement (
|
||||
workshop_line TEXT,
|
||||
settled_qty INTEGER
|
||||
);
|
||||
INSERT INTO dwd_settlement VALUES
|
||||
('LINE_01', 98),
|
||||
('LINE_02', 95);
|
||||
""",
|
||||
profiling=True,
|
||||
)
|
||||
|
||||
async with SessionLocal() as session:
|
||||
wf = Workflow(
|
||||
id=wf_id,
|
||||
name=f"良率核算_{wf_id}",
|
||||
version=1,
|
||||
graph={
|
||||
"nodes": [
|
||||
{
|
||||
"id": "in_1",
|
||||
"type": "input",
|
||||
"data": {"config": {"fields": [{"name": "p_line", "label": "产线编码", "type": "string"}]}},
|
||||
},
|
||||
{
|
||||
"id": "card_yield",
|
||||
"type": "metrics",
|
||||
"data": {"config": {"metrics": [{"id": "fpy", "name": "一次良率"}]}},
|
||||
},
|
||||
],
|
||||
"edges": [],
|
||||
},
|
||||
)
|
||||
session.add(wf)
|
||||
await session.commit()
|
||||
|
||||
created_doc_ids: list[str] = []
|
||||
try:
|
||||
yield {
|
||||
"mes_id": mes_id,
|
||||
"mes_name": mes_name,
|
||||
"erp_id": erp_id,
|
||||
"erp_name": erp_name,
|
||||
"wf_id": wf_id,
|
||||
"doc_ids": created_doc_ids,
|
||||
}
|
||||
finally:
|
||||
await forget_source(mes_id)
|
||||
await forget_source(erp_id)
|
||||
async with SessionLocal() as session:
|
||||
await session.execute(delete(CanonicalEntity))
|
||||
await session.execute(delete(EntityDiscoveryJob))
|
||||
await session.execute(delete(Workflow).where(Workflow.id == wf_id))
|
||||
if created_doc_ids:
|
||||
await session.execute(delete(Chunk).where(Chunk.document_id.in_(created_doc_ids)))
|
||||
await session.execute(delete(Document).where(Document.id.in_(created_doc_ids)))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def test_attachment_upload_and_optional_save_to_kb(multi_source_env):
|
||||
csv_bytes = "标准编码,产线名称,MES编码,ERP编码\nL1,一号产线,L1,LINE_01\nL2,二号产线,L2,LINE_02\n".encode("utf-8")
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
# 1) 不勾选存入知识库:提取表格内容但不创建 Document
|
||||
r1 = await client.post(
|
||||
"/api/ontology/entities/attachments",
|
||||
files={"file": ("line_mapping.csv", csv_bytes, "text/csv")},
|
||||
data={"save_to_kb": "false"},
|
||||
)
|
||||
assert r1.status_code == 201, r1.text
|
||||
d1 = r1.json()
|
||||
assert d1["saved_to_kb"] is False
|
||||
assert d1["document_id"] is None
|
||||
assert d1["chunk_count"] >= 1
|
||||
|
||||
# 2) 勾选存入知识库:同步创建 Document 并返回 document_id
|
||||
r2 = await client.post(
|
||||
"/api/ontology/entities/attachments",
|
||||
files={"file": ("line_mapping_kb.csv", csv_bytes, "text/csv")},
|
||||
data={"save_to_kb": "true", "collection": "default"},
|
||||
)
|
||||
assert r2.status_code == 201, r2.text
|
||||
d2 = r2.json()
|
||||
assert d2["saved_to_kb"] is True
|
||||
assert d2["document_id"]
|
||||
multi_source_env["doc_ids"].append(d2["document_id"])
|
||||
|
||||
async with SessionLocal() as session:
|
||||
doc_row = await session.get(Document, d2["document_id"])
|
||||
assert doc_row is not None
|
||||
assert "LINE_01" in doc_row.content
|
||||
|
||||
# 同时传 attachment_id 与对应 document_id 时自动去重为 1 份
|
||||
bundles = await entity_docs.load_doc_bundles(
|
||||
session,
|
||||
attachment_ids=[d2["attachment_id"]],
|
||||
document_ids=[d2["document_id"]],
|
||||
)
|
||||
assert len(bundles) == 1
|
||||
hits = entity_docs.search_doc_bundles(bundles, query="LINE_01")
|
||||
assert len(hits) == 1
|
||||
assert "文档《line_mapping_kb.csv》" in hits[0]["citation"]
|
||||
|
||||
|
||||
async def test_entity_patch_guardrails_masked_columns_and_cas(multi_source_env):
|
||||
mes = multi_source_env["mes_name"]
|
||||
erp = multi_source_env["erp_name"]
|
||||
wf_id = multi_source_env["wf_id"]
|
||||
|
||||
async with SessionLocal() as session:
|
||||
# 1) 校验不存在的列、不存在的参数与遮罩列枚举阻断
|
||||
bad_plan = await entity_patch.plan_entity_patch(
|
||||
session,
|
||||
[
|
||||
{"op": "upsert_entity", "entity_key": "phone_ent", "label": "作业员号码"},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "phone_ent",
|
||||
"source_name": mes,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "operator_phone",
|
||||
},
|
||||
{
|
||||
"op": "upsert_member",
|
||||
"entity_key": "phone_ent",
|
||||
"value": "P1",
|
||||
"label": "张三",
|
||||
"source_codes": {mes: "13900000011"},
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "phone_ent",
|
||||
"source_name": mes,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "no_such_col",
|
||||
},
|
||||
{
|
||||
"op": "upsert_caliber_param",
|
||||
"entity_key": "phone_ent",
|
||||
"workflow_id": wf_id,
|
||||
"param_name": "wrong_param",
|
||||
},
|
||||
],
|
||||
mode="confirmed",
|
||||
)
|
||||
assert len(bad_plan.problems) == 3
|
||||
assert any("遮罩列" in p for p in bad_plan.problems)
|
||||
assert any("no_such_col" in p for p in bad_plan.problems)
|
||||
assert any("wrong_param" in p for p in bad_plan.problems)
|
||||
|
||||
# 2) mode="confirmed" 正常落库,随后测试 expected_versions CAS 乐观锁与 mode="proposed" 隔离
|
||||
res = await entity_patch.apply_entity_patch(
|
||||
session,
|
||||
[
|
||||
{
|
||||
"op": "upsert_entity",
|
||||
"entity_key": "prod_line_t",
|
||||
"label": "产线",
|
||||
"status": "proposed", # 模型自报状态将被忽略,confirmed 模式下按 confirmed 落库
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "prod_line_t",
|
||||
"source_name": mes,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "line_code",
|
||||
"reason": "使用者说:工单产线列是 line_code",
|
||||
},
|
||||
],
|
||||
mode="confirmed",
|
||||
actor="builder_a",
|
||||
)
|
||||
await session.commit()
|
||||
assert res["created_entities"] == 1
|
||||
ent = res["entities"][0]
|
||||
assert ent["bindings"][0]["status"] == "confirmed"
|
||||
|
||||
# 将一条 ERP 绑定预先设为 rejected,验证 mode="proposed" 绝不覆盖 confirmed 或复活 rejected
|
||||
ent_row = await entities.get_entity_by_key(session, "prod_line_t")
|
||||
assert ent_row is not None
|
||||
await entities.update_entity(
|
||||
session,
|
||||
ent_row.id,
|
||||
{
|
||||
"version": ent_row.version,
|
||||
"bindings": [
|
||||
*ent_row.bindings,
|
||||
{
|
||||
"source_name": erp,
|
||||
"table_name": "dwd_settlement",
|
||||
"column_name": "settled_qty",
|
||||
"status": "rejected",
|
||||
"source": "human",
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
# 3) mode="proposed":尝试重提 rejected 列 + 新增 workshop_line + 自封 status="confirmed"
|
||||
prop_res = await entity_patch.apply_entity_patch(
|
||||
session,
|
||||
[
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "prod_line_t",
|
||||
"source_name": erp,
|
||||
"table_name": "dwd_settlement",
|
||||
"column_name": "settled_qty",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "prod_line_t",
|
||||
"source_name": erp,
|
||||
"table_name": "dwd_settlement",
|
||||
"column_name": "workshop_line",
|
||||
"status": "confirmed",
|
||||
"reason": "跨源目录对齐:结算产线",
|
||||
},
|
||||
{
|
||||
"op": "upsert_caliber_param",
|
||||
"entity_key": "prod_line_t",
|
||||
"workflow_id": wf_id,
|
||||
"card_id": "card_yield",
|
||||
"param_name": "p_line",
|
||||
"status": "confirmed",
|
||||
},
|
||||
],
|
||||
mode="proposed",
|
||||
)
|
||||
await session.commit()
|
||||
assert prop_res["bindings_changed"] == 1
|
||||
assert prop_res["caliber_params_changed"] == 1
|
||||
updated_ent = prop_res["entities"][0]
|
||||
by_col = {b["column_name"]: b["status"] for b in updated_ent["bindings"]}
|
||||
assert by_col["line_code"] == "confirmed"
|
||||
assert by_col["settled_qty"] == "rejected"
|
||||
assert by_col["workshop_line"] == "proposed"
|
||||
assert updated_ent["caliber_params"][0]["status"] == "proposed"
|
||||
|
||||
# 4) CAS 版本冲突应抛 EntityConflict
|
||||
with pytest.raises(entities.EntityConflict):
|
||||
await entity_patch.apply_entity_patch(
|
||||
session,
|
||||
[{"op": "upsert_entity", "entity_key": "prod_line_t", "label": "新产线名"}],
|
||||
mode="confirmed",
|
||||
expected_versions={"prod_line_t": 1},
|
||||
)
|
||||
|
||||
|
||||
async def test_entity_assistant_sse_and_apply_patch(multi_source_env, monkeypatch):
|
||||
mes = multi_source_env["mes_name"]
|
||||
erp = multi_source_env["erp_name"]
|
||||
wf_id = multi_source_env["wf_id"]
|
||||
|
||||
scripted = _ScriptedModel(
|
||||
turns=[
|
||||
_Turn(
|
||||
calls=[
|
||||
("inspect_sources", {"keyword": "line"}),
|
||||
("read_document", {"query": "LINE_01"}),
|
||||
("run_query", {"source_name": mes, "sql": "SELECT line_code, operator_phone FROM dwd_workorder"}),
|
||||
]
|
||||
),
|
||||
_Turn(
|
||||
calls=[
|
||||
(
|
||||
"propose_entity_changes",
|
||||
{
|
||||
"operations": [
|
||||
{
|
||||
"op": "upsert_entity",
|
||||
"entity_key": "workshop_line_ent",
|
||||
"label": "车间产线",
|
||||
"aliases": ["产线"],
|
||||
"reason": "使用者说:建立车间产线标准实体",
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "workshop_line_ent",
|
||||
"source_name": mes,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "line_code",
|
||||
"reason": "数据探查显示:工单表产线列为 line_code",
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "workshop_line_ent",
|
||||
"source_name": erp,
|
||||
"table_name": "dwd_settlement",
|
||||
"column_name": "workshop_line",
|
||||
"reason": "文档《对照表.md》写着:ERP 结算表对应 workshop_line",
|
||||
},
|
||||
{
|
||||
"op": "upsert_member",
|
||||
"entity_key": "workshop_line_ent",
|
||||
"value": "L1",
|
||||
"label": "一号产线",
|
||||
"source_codes": {mes: "L1", erp: "LINE_01"},
|
||||
"reason": "文档《对照表.md》写着:L1 对应 LINE_01",
|
||||
},
|
||||
{
|
||||
"op": "upsert_caliber_param",
|
||||
"entity_key": "workshop_line_ent",
|
||||
"workflow_id": wf_id,
|
||||
"card_id": "card_yield",
|
||||
"param_name": "p_line",
|
||||
"reason": "口径入口对齐",
|
||||
},
|
||||
]
|
||||
},
|
||||
)
|
||||
]
|
||||
),
|
||||
_Turn(text=["已核对实表与对照文档,生成车间产线标准实体及跨源编码提案,请确认保存。"]),
|
||||
]
|
||||
)
|
||||
|
||||
async def _fake_resolve(_session):
|
||||
return scripted, "mock-draft-model"
|
||||
|
||||
monkeypatch.setattr(entity_assistant, "resolve_model", _fake_resolve)
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
att_res = await client.post(
|
||||
"/api/ontology/entities/attachments",
|
||||
files={"file": ("对照表.md", "一号产线:MES 为 L1,ERP dwd_settlement.workshop_line 为 LINE_01".encode("utf-8"), "text/markdown")},
|
||||
data={"save_to_kb": "false"},
|
||||
)
|
||||
assert att_res.status_code == 201
|
||||
att_id = att_res.json()["attachment_id"]
|
||||
|
||||
sse_res = await client.post(
|
||||
"/api/ontology/entities/assistant",
|
||||
json={
|
||||
"messages": [{"role": "user", "content": "请根据对照表帮我关联车间产线实体"}],
|
||||
"attachment_ids": [att_id],
|
||||
},
|
||||
)
|
||||
assert sse_res.status_code == 200
|
||||
events = [
|
||||
json.loads(line[5:].strip())
|
||||
for line in sse_res.text.splitlines()
|
||||
if line.startswith("data:")
|
||||
]
|
||||
types = [ev["type"] for ev in events]
|
||||
assert "query" in types
|
||||
assert "proposal" in types
|
||||
assert types[-1] == "done"
|
||||
|
||||
# 确认只读查询中的敏感列已被遮罩为「已遮罩」
|
||||
q_ev = next(ev for ev in events if ev["type"] == "query")
|
||||
assert q_ev["source"] == mes
|
||||
assert all(row[1] == "已遮罩" for row in q_ev["rows"])
|
||||
|
||||
# 提案尚未落库
|
||||
list_before = await client.get("/api/ontology/entities")
|
||||
assert all(e["key"] != "workshop_line_ent" for e in list_before.json()["items"])
|
||||
|
||||
# 人工确认并应用提案
|
||||
prop_ev = next(ev for ev in events if ev["type"] == "proposal")
|
||||
ops = [c["op_payload"] for c in prop_ev["changes"]]
|
||||
apply_res = await client.post(
|
||||
"/api/ontology/entities/apply-patch",
|
||||
json={"operations": ops, "expected_versions": prop_ev["expected_versions"]},
|
||||
headers={"X-Actor": "builder_one"},
|
||||
)
|
||||
assert apply_res.status_code == 200, apply_res.text
|
||||
applied = apply_res.json()
|
||||
assert applied["created_entities"] == 1
|
||||
saved = applied["entities"][0]
|
||||
assert saved["key"] == "workshop_line_ent"
|
||||
assert all(b["status"] == "confirmed" for b in saved["bindings"])
|
||||
assert saved["members"][0]["source_codes"] == {mes: "L1", erp: "LINE_01"}
|
||||
|
||||
|
||||
async def test_entity_discovery_job_lifecycle_and_proposed_isolation(multi_source_env, monkeypatch):
|
||||
mes = multi_source_env["mes_name"]
|
||||
erp = multi_source_env["erp_name"]
|
||||
|
||||
discovery_json = json.dumps(
|
||||
{
|
||||
"operations": [
|
||||
{
|
||||
"op": "upsert_entity",
|
||||
"entity_key": "discovered_line",
|
||||
"label": "自动发现产线",
|
||||
"description": "跨 MES 与 ERP 的产线对象",
|
||||
"aliases": ["产线"],
|
||||
"reason": "跨源目录对齐:工单与结算产线",
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "discovered_line",
|
||||
"entity_label": "自动发现产线",
|
||||
"source_name": mes,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "line_code",
|
||||
"status": "confirmed", # 后台发现必须强制降为 proposed
|
||||
"reason": "跨源目录对齐:MES 工单产线",
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "discovered_line",
|
||||
"entity_label": "自动发现产线",
|
||||
"source_name": erp,
|
||||
"table_name": "dwd_settlement",
|
||||
"column_name": "workshop_line",
|
||||
"reason": "跨源目录对齐:ERP 结算产线",
|
||||
},
|
||||
{
|
||||
"op": "upsert_member",
|
||||
"entity_key": "discovered_line",
|
||||
"entity_label": "自动发现产线",
|
||||
"value": "L1",
|
||||
"label": "一号产线",
|
||||
"source_codes": {mes: "L1", erp: "LINE_01"},
|
||||
"reason": "文档对照",
|
||||
},
|
||||
]
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
fake_model = _ScriptedModel(invoke_reply=f"```json\n{discovery_json}\n```")
|
||||
|
||||
async def _fake_resolve(_session):
|
||||
return fake_model, "mock-discovery-model"
|
||||
|
||||
monkeypatch.setattr(catalog_assistant, "resolve_model", _fake_resolve)
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
r = await client.post(
|
||||
"/api/ontology/discovery-jobs",
|
||||
json={
|
||||
"source_ids": [multi_source_env["mes_id"], multi_source_env["erp_id"]],
|
||||
"note": "重点扫描产线",
|
||||
},
|
||||
headers={"X-Actor": "builder_scan"},
|
||||
)
|
||||
assert r.status_code == 202, r.text
|
||||
job_id = r.json()["id"]
|
||||
|
||||
# 轮询等待完成
|
||||
final_job = None
|
||||
for _ in range(40):
|
||||
jr = await client.get(f"/api/ontology/discovery-jobs/{job_id}")
|
||||
assert jr.status_code == 200
|
||||
body = jr.json()
|
||||
if body["status"] in ("succeeded", "failed", "cancelled"):
|
||||
final_job = body
|
||||
break
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
assert final_job is not None
|
||||
assert final_job["status"] == "succeeded", final_job
|
||||
assert final_job["summary"]["created_entities"] == 1
|
||||
assert final_job["summary"]["proposed_bindings"] == 2
|
||||
assert final_job["summary"]["added_members"] == 1
|
||||
|
||||
ents = (await client.get("/api/ontology/entities")).json()["items"]
|
||||
disc = next(e for e in ents if e["key"] == "discovered_line")
|
||||
assert all(b["status"] == "proposed" and b["source"] == "ai" for b in disc["bindings"])
|
||||
|
||||
# 运行期 confirmed_bindings 必须为 0(状态门禁隔离)
|
||||
assert entities.confirmed_bindings(disc) == []
|
||||
|
||||
|
||||
async def test_entity_discovery_job_busy_and_cancel(multi_source_env, monkeypatch):
|
||||
entered = asyncio.Event()
|
||||
|
||||
class _SlowModel:
|
||||
async def ainvoke(self, _messages):
|
||||
entered.set()
|
||||
await asyncio.sleep(30)
|
||||
return AIMessage(content='{"operations": []}')
|
||||
|
||||
async def _fake_resolve(_session):
|
||||
return _SlowModel(), "mock-slow-model"
|
||||
|
||||
monkeypatch.setattr(catalog_assistant, "resolve_model", _fake_resolve)
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
r1 = await client.post(
|
||||
"/api/ontology/discovery-jobs",
|
||||
json={"source_ids": [multi_source_env["mes_id"]]},
|
||||
)
|
||||
assert r1.status_code == 202
|
||||
job_id = r1.json()["id"]
|
||||
|
||||
await asyncio.wait_for(entered.wait(), timeout=3.0)
|
||||
|
||||
# 并发再次发起应返回 409 discovery_job_busy
|
||||
r2 = await client.post(
|
||||
"/api/ontology/discovery-jobs",
|
||||
json={"source_ids": [multi_source_env["mes_id"]]},
|
||||
)
|
||||
assert r2.status_code == 409
|
||||
assert r2.json().get("code") == "discovery_job_busy"
|
||||
|
||||
# 主动停止扫描
|
||||
rc = await client.post(f"/api/ontology/discovery-jobs/{job_id}/cancel")
|
||||
assert rc.status_code == 200
|
||||
assert rc.json()["status"] == "cancelled"
|
||||
|
||||
|
||||
async def test_entity_assistant_source_scope_isolates_unrelated_domains(multi_source_env):
|
||||
"""进入单个数据源目录页时,传入 source_ids 仅暴露所选数据源的表、查询权限与关联工作流,隔离其他业务域。"""
|
||||
mes_id = multi_source_env["mes_id"]
|
||||
mes_name = multi_source_env["mes_name"]
|
||||
erp_id = multi_source_env["erp_id"]
|
||||
erp_name = multi_source_env["erp_name"]
|
||||
wf_mes_id = f"wf_mes_only_{uuid.uuid4().hex[:6]}"
|
||||
wf_erp_id = f"wf_erp_only_{uuid.uuid4().hex[:6]}"
|
||||
|
||||
async with SessionLocal() as session:
|
||||
# 构造一个仅归属 MES 域的实体与两个分别绑定不同数据源工具的工作流
|
||||
await entities.create_entity(
|
||||
session,
|
||||
{
|
||||
"key": "mes_only_line",
|
||||
"label": "车间专属产线",
|
||||
"bindings": [
|
||||
{
|
||||
"source_id": mes_id,
|
||||
"source_name": mes_name,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "line_code",
|
||||
"status": "confirmed",
|
||||
"source": "human",
|
||||
}
|
||||
],
|
||||
},
|
||||
actor="tester",
|
||||
)
|
||||
session.add_all(
|
||||
[
|
||||
Workflow(
|
||||
id=wf_mes_id,
|
||||
name="车间工单良率",
|
||||
version=1,
|
||||
graph={
|
||||
"nodes": [
|
||||
{
|
||||
"id": "in_m",
|
||||
"type": "input",
|
||||
"data": {"config": {"fields": [{"name": "scx_code", "label": "产线", "type": "string"}]}},
|
||||
},
|
||||
{
|
||||
"id": "q_m",
|
||||
"type": "tool",
|
||||
"data": {"config": {"tool": f"db_query__{mes_name}", "args": {"sql": "SELECT 1"}}},
|
||||
},
|
||||
{
|
||||
"id": "c_m",
|
||||
"type": "metrics",
|
||||
"data": {"config": {"metrics": [{"id": "m1", "name": "良率"}]}},
|
||||
},
|
||||
],
|
||||
"edges": [],
|
||||
},
|
||||
),
|
||||
Workflow(
|
||||
id=wf_erp_id,
|
||||
name="结算金额口径",
|
||||
version=1,
|
||||
graph={
|
||||
"nodes": [
|
||||
{
|
||||
"id": "in_e",
|
||||
"type": "input",
|
||||
"data": {"config": {"fields": [{"name": "store_id", "label": "门店", "type": "string"}]}},
|
||||
},
|
||||
{
|
||||
"id": "q_e",
|
||||
"type": "tool",
|
||||
"data": {"config": {"tool": f"db_query__{erp_name}", "args": {"sql": "SELECT 1"}}},
|
||||
},
|
||||
{
|
||||
"id": "c_e",
|
||||
"type": "metrics",
|
||||
"data": {"config": {"metrics": [{"id": "e1", "name": "结算量"}]}},
|
||||
},
|
||||
],
|
||||
"edges": [],
|
||||
},
|
||||
),
|
||||
]
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
try:
|
||||
async with SessionLocal() as session:
|
||||
ctx = await entity_assistant.prepare(session, source_ids=[erp_id])
|
||||
|
||||
# 1. 只加载 erp_name,不包含 mes_name
|
||||
assert list(ctx.sources.keys()) == [erp_name.lower()]
|
||||
assert ctx.queryable_sources == [erp_name]
|
||||
|
||||
# 2. 系统提示词不泄露 mes_name 的物理表名 dwd_workorder
|
||||
assert "dwd_workorder" not in ctx.system
|
||||
assert erp_name in ctx.system
|
||||
|
||||
# 3. inspect_sources 只返回 erp 的表与 erp 关联的口径工作流,不包含车间工单良率工作流
|
||||
tools = entity_assistant._EntityTools(ctx)
|
||||
inspected = tools.inspect("", "")
|
||||
assert "dwd_settlement" in inspected
|
||||
assert "dwd_workorder" not in inspected
|
||||
assert wf_erp_id in inspected
|
||||
assert wf_mes_id not in inspected
|
||||
|
||||
# 4. 对未纳入范围的 mes_name 发起只读查询会被立即拒绝,不尝试连接该库
|
||||
ev, msg = await tools.query(mes_name, "SELECT * FROM dwd_workorder LIMIT 5")
|
||||
assert ev is None
|
||||
assert erp_name in msg
|
||||
finally:
|
||||
async with SessionLocal() as session:
|
||||
await session.execute(delete(Workflow).where(Workflow.id.in_([wf_mes_id, wf_erp_id])))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def test_entity_discovery_streaming_progress_and_summary(multi_source_env, monkeypatch):
|
||||
"""扫描发现任务支持 astream 流式输出并在 summary 中记录量化扫描统计与新增实体清单。"""
|
||||
mes_id = multi_source_env["mes_id"]
|
||||
mes_name = multi_source_env["mes_name"]
|
||||
stream_payload = json.dumps(
|
||||
{
|
||||
"operations": [
|
||||
{
|
||||
"op": "upsert_entity",
|
||||
"entity_key": "stream_line",
|
||||
"label": "流式识别产线",
|
||||
"reason": "目录语义对齐:产线编码",
|
||||
},
|
||||
{
|
||||
"op": "upsert_binding",
|
||||
"entity_key": "stream_line",
|
||||
"entity_label": "流式识别产线",
|
||||
"source_name": mes_name,
|
||||
"table_name": "dwd_workorder",
|
||||
"column_name": "line_code",
|
||||
"reason": "目录语义对齐:产线字段",
|
||||
},
|
||||
]
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
class _StreamingDiscoveryModel:
|
||||
async def astream(self, _messages):
|
||||
mid = len(stream_payload) // 2
|
||||
yield AIMessageChunk(content=stream_payload[:mid])
|
||||
await asyncio.sleep(0.02)
|
||||
yield AIMessageChunk(content=stream_payload[mid:])
|
||||
|
||||
async def _fake_resolve(_session):
|
||||
return _StreamingDiscoveryModel(), "mock-stream-model"
|
||||
|
||||
monkeypatch.setattr(catalog_assistant, "resolve_model", _fake_resolve)
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
r = await client.post(
|
||||
"/api/ontology/discovery-jobs",
|
||||
json={"source_ids": [mes_id]},
|
||||
)
|
||||
assert r.status_code == 202
|
||||
job_id = r.json()["id"]
|
||||
|
||||
final_job = None
|
||||
for _ in range(40):
|
||||
jr = await client.get(f"/api/ontology/discovery-jobs/{job_id}")
|
||||
body = jr.json()
|
||||
if body["status"] in ("succeeded", "failed", "cancelled"):
|
||||
final_job = body
|
||||
break
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
assert final_job is not None
|
||||
assert final_job["status"] == "succeeded", final_job
|
||||
assert isinstance(final_job["started_at"], str) and (
|
||||
final_job["started_at"].endswith("+00:00") or final_job["started_at"].endswith("Z")
|
||||
)
|
||||
assert isinstance(final_job["finished_at"], str) and (
|
||||
final_job["finished_at"].endswith("+00:00") or final_job["finished_at"].endswith("Z")
|
||||
)
|
||||
summary = final_job["summary"]
|
||||
assert summary["progress_pct"] == 100
|
||||
assert summary["sources_scanned"] == 1
|
||||
assert summary["source_names"] == [mes_name]
|
||||
assert summary["tables_scanned"] >= 1
|
||||
assert summary["ops_detected"] == 2
|
||||
assert any(t["key"] == "stream_line" for t in summary["touched_entities"])
|
||||
|
||||
|
||||
|
||||
@@ -99,13 +99,15 @@ def test_scores_compare_and_catalog_modes():
|
||||
|
||||
|
||||
def test_analysis_tools_and_presets_follow_the_server():
|
||||
"""变体的分析工具开、关和预设对照(api/evals.PRESETS):前端的取值和服务端一一对上。叫法写「开」「关」,和服务端
|
||||
"""变体的分析工具与本体穿透开、关及预设对照(api/evals.PRESETS):前端的取值和服务端一一对上。叫法写「开」「关」,和服务端
|
||||
describe、对比里写的同一种说法。"""
|
||||
from app.api.evals import PRESETS
|
||||
from app.eval.variants import ANALYSIS_TOOLS, ANALYSIS_TOOLS_LABEL
|
||||
from app.eval.variants import ANALYSIS_TOOLS, ANALYSIS_TOOLS_LABEL, ONTOLOGY_TOOLS, ONTOLOGY_TOOLS_LABEL
|
||||
|
||||
assert _union("EvalAnalysisTools") == list(ANALYSIS_TOOLS)
|
||||
assert _pairs("start.analysisToolsMode") == list(ANALYSIS_TOOLS_LABEL.items())
|
||||
assert _union("EvalOntologyTools") == list(ONTOLOGY_TOOLS)
|
||||
assert _pairs("start.ontologyToolsMode") == list(ONTOLOGY_TOOLS_LABEL.items())
|
||||
assert _union("EvalPreset") == list(PRESETS)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,481 @@
|
||||
"""Wave 2-b 单元测试:三段式穿透充分性评估 investigation.assess、H{n} 源健康证据引用与离线包核验、analysis_claims V2 非因果护栏(D6)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from datetime import date
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from sqlalchemy import delete
|
||||
|
||||
import app.analysis as analysis_ops
|
||||
from app.analysis import contract as analysis_contract
|
||||
from app.bundle.build import build_bundle
|
||||
from app.core.artifact_store import canonical_json, content_hash, manifest_hash
|
||||
from app.data import source_health as sh
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models_ontology import CanonicalEntity
|
||||
from app.engine import analysis_claims, analysis_scope, evidence, investigation
|
||||
from app.engine.investigation import InvestigationPolicy
|
||||
from app.engine.runner import run_manager
|
||||
from app.tools.analysis import AnalysisScope, input_snapshot
|
||||
from tests.fixtures.acceptance import multi_source_ontology as mso
|
||||
from tests.test_bundle_shown import NODE, doc_of, node, reseal
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def _setup_and_clean():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
async with SessionLocal() as session:
|
||||
await session.execute(delete(CanonicalEntity))
|
||||
await session.commit()
|
||||
|
||||
|
||||
def _snap(ref: str, columns: list[str], rows: list[list], *, source: str = "ops_shift") -> dict[str, Any]:
|
||||
return input_snapshot(
|
||||
{
|
||||
"columns": columns,
|
||||
"rows": rows,
|
||||
"sql": f"SELECT {', '.join(columns)} FROM t",
|
||||
"source": source,
|
||||
"truncated": False,
|
||||
},
|
||||
ref=ref,
|
||||
artifact=ref.lower().ljust(64, "0")[:64],
|
||||
node_id="bot",
|
||||
masked=[],
|
||||
)
|
||||
|
||||
|
||||
async def _probe(source: str, table: str, column: str) -> dict[str, Any]:
|
||||
return {
|
||||
"source": source,
|
||||
"table": table,
|
||||
"column": column,
|
||||
"min": "2026-10-01",
|
||||
"max": "2026-10-21",
|
||||
"basis": "live",
|
||||
"at": "2026-10-22T00:00:00+00:00",
|
||||
}
|
||||
|
||||
|
||||
async def _run_op(req: dict[str, Any], scope: AnalysisScope, cfg: dict[str, Any]) -> dict[str, Any]:
|
||||
return await analysis_ops.run(
|
||||
req,
|
||||
scope.resolve,
|
||||
settings=cfg,
|
||||
context={"today": date(2026, 10, 22), "probe": _probe},
|
||||
refuse_data_errors=False,
|
||||
)
|
||||
|
||||
|
||||
async def test_investigation_three_stage_s1_s2_s3_and_early_stop():
|
||||
cfg = analysis_contract.effective_settings({})
|
||||
policy = InvestigationPolicy(minimum_depth=0)
|
||||
|
||||
# --- S1:契约越界(load_rate_pct > 100%),尚未做 ontology_neighbors 排查 -> 要求沿本体排查构成因子 ---
|
||||
s1_scope = AnalysisScope(node_id="bot", snapshot=cfg)
|
||||
s1_scope.record_query_target(
|
||||
sources=["ops_shift"],
|
||||
workflow_id="wf_ops",
|
||||
card_id="m_ops",
|
||||
violations=[{"metric_id": "load_rate_pct", "detail": "指标值超出声明的范围"}],
|
||||
)
|
||||
g_s1 = investigation.assess(s1_scope, "为什么昨天三线产线负荷率达到 135% 越界了?", policy)
|
||||
assert g_s1["status"] == "partial"
|
||||
assert "ontology_neighbors" in g_s1["next_step"] or "契约" in g_s1["reason"]
|
||||
|
||||
# 发现 factor 邻居但尚未跨源核对 -> 提示按实体桥查询关联因子
|
||||
s1_scope.record_ontology_neighbors(
|
||||
[
|
||||
{
|
||||
"kind": "factor",
|
||||
"role": "denominator",
|
||||
"target": {
|
||||
"workflow_id": "wf_equip",
|
||||
"card_id": "m_equip",
|
||||
"metric_id": "rated_hourly_qty",
|
||||
"sources": ["equip_master"],
|
||||
},
|
||||
}
|
||||
],
|
||||
focal_metrics=[{"workflow_id": "wf_ops", "card_id": "m_ops", "sources": ["ops_shift"]}],
|
||||
)
|
||||
g_s1_mid = investigation.assess(s1_scope, "为什么昨天三线产线负荷率达到 135% 越界了?", policy)
|
||||
assert g_s1_mid["status"] == "partial"
|
||||
assert g_s1_mid["stage"] == "ontology_penetration"
|
||||
|
||||
# 完成 1 跳跨源核对后 -> S1 收口通过
|
||||
s1_scope.record_query_target(sources=["equip_master"], workflow_id="wf_equip", card_id="m_equip")
|
||||
g_s1_done = investigation.assess(s1_scope, "为什么昨天三线产线负荷率达到 135% 越界了?", policy)
|
||||
assert g_s1_done["status"] == "complete", g_s1_done
|
||||
assert g_s1_done["stage"] == "ontology_penetration"
|
||||
|
||||
# --- S2:数据源健康检查发现断更(anomalous) -> 立即早停(complete),绝不误导继续做业务归因拆解! ---
|
||||
s2_scope = AnalysisScope(node_id="bot", snapshot=cfg)
|
||||
s2_scope.add_query("Q1", _snap("Q1", ["dt", "scrap"], [["2026-10-20", 18], ["2026-10-21", 20]], source="ops_shift"))
|
||||
s2_scope.add_health(
|
||||
{
|
||||
"kind": "source_health",
|
||||
"verdict": "anomalous",
|
||||
"phrase": "数据源「quality_scrap」存在断更告警(最新日期停在 2026-10-19)",
|
||||
"sources": [{"source": "quality_scrap", "verdict": "anomalous"}],
|
||||
"issues": [{"kind": "date_stalled", "severity": "alert", "detail": "最新日期停在 2026-10-19"}],
|
||||
},
|
||||
artifact="a" * 64,
|
||||
)
|
||||
g_s2_health = investigation.assess(s2_scope, "为什么昨天质检台账报废数突然掉到 0,和车间对不上?", policy)
|
||||
assert g_s2_health["status"] == "complete"
|
||||
assert g_s2_health["stage"] == "health_and_contract"
|
||||
|
||||
# --- S2 变体:reconcile 判定 diverged -> 同样在 health_and_contract 阶段立即早停收口 ---
|
||||
s2_rec_scope = AnalysisScope(node_id="bot", snapshot=cfg)
|
||||
q_left = _snap("Q1", ["line_code", "qty"], [["L1", 18], ["L2", 22]], source="ops_shift")
|
||||
q_right = _snap("Q2", ["line_code", "qty"], [["L1", 18], ["L2", 2]], source="quality_scrap")
|
||||
s2_rec_scope.add_query("Q1", q_left)
|
||||
s2_rec_scope.add_query("Q2", q_right)
|
||||
rec_res = await _run_op(
|
||||
{
|
||||
"op": "reconcile",
|
||||
"mode": None,
|
||||
"params": {"base_query": "Q1", "target_query": "Q2", "by": ["line_code"], "measure": "qty"},
|
||||
},
|
||||
s2_rec_scope,
|
||||
cfg,
|
||||
)
|
||||
assert rec_res["verdict"]["code"] == "diverged"
|
||||
s2_rec_scope.add_result(rec_res, artifact="b" * 64)
|
||||
g_s2_rec = investigation.assess(s2_rec_scope, "为什么两套报废数对不上?", policy)
|
||||
assert g_s2_rec["status"] == "complete"
|
||||
assert g_s2_rec["stage"] == "health_and_contract"
|
||||
|
||||
# --- S3:源健康正常、同源基线异常 + 已拆解定位切片 L2,存在 upstream_driver 邻居未做跨源 cochange 验证 -> 提示跨源穿透 ---
|
||||
s3_scope = AnalysisScope(node_id="bot", snapshot=cfg)
|
||||
days = [f"2026-10-{d:02d}" for d in range(7, 22)] # 15 天序列
|
||||
agg_vals = [1800, 1804, 1796, 1802, 1800, 1798, 1806, 1802, 1796, 1804, 1800, 1796, 1802, 1800, 1512]
|
||||
q_agg = _snap("Q1", ["dt", "good_qty"], [[d, v] for d, v in zip(days, agg_vals)], source="ops_shift")
|
||||
q_slice = _snap(
|
||||
"Q2",
|
||||
["dt", "line_code", "good_qty"],
|
||||
[
|
||||
["2026-10-20", "L1", 900],
|
||||
["2026-10-20", "L2", 900],
|
||||
["2026-10-21", "L1", 900],
|
||||
["2026-10-21", "L2", 612],
|
||||
],
|
||||
source="ops_shift",
|
||||
)
|
||||
s3_scope.add_query("Q1", q_agg)
|
||||
s3_scope.add_query("Q2", q_slice)
|
||||
s3_scope.record_query_target(sources=["ops_shift"], workflow_id="wf_ops", card_id="m_ops")
|
||||
|
||||
base_res = await _run_op(
|
||||
{"op": "baseline", "mode": None, "params": {"query": "Q1", "period": "dt", "measure": "good_qty", "lag": 1}},
|
||||
s3_scope,
|
||||
cfg,
|
||||
)
|
||||
assert base_res["verdict"]["code"] in ("outside_band", "far_outside_band")
|
||||
decomp_res = await _run_op(
|
||||
{
|
||||
"op": "decompose",
|
||||
"mode": "additive",
|
||||
"params": {
|
||||
"query": "Q2",
|
||||
"period": "dt",
|
||||
"by": ["line_code"],
|
||||
"measure": "good_qty",
|
||||
"base": "2026-10-20",
|
||||
"target": "2026-10-21",
|
||||
"headline": {"base": "Q1.r13.good_qty", "target": "Q1.r14.good_qty"},
|
||||
},
|
||||
},
|
||||
s3_scope,
|
||||
cfg,
|
||||
)
|
||||
assert decomp_res["status"] == "ok"
|
||||
s3_scope.add_result(base_res, artifact="d" * 64)
|
||||
s3_scope.add_result(decomp_res, artifact="e" * 64)
|
||||
s3_scope.record_ontology_neighbors(
|
||||
[
|
||||
{
|
||||
"kind": "upstream_driver",
|
||||
"entity_key": "production_line",
|
||||
"target": {
|
||||
"workflow_id": "wf_equip",
|
||||
"card_id": "m_equip",
|
||||
"metric_id": "unplanned_downtime_min",
|
||||
"sources": ["equip_master"],
|
||||
},
|
||||
}
|
||||
],
|
||||
focal_metrics=[{"workflow_id": "wf_ops", "card_id": "m_ops", "sources": ["ops_shift"]}],
|
||||
)
|
||||
g_s3_before = investigation.assess(s3_scope, "为什么昨天车间合格产量突然异常下跌?", policy)
|
||||
assert g_s3_before["status"] == "partial"
|
||||
assert g_s3_before["stage"] == "ontology_penetration"
|
||||
|
||||
# 补上跨源驱动序列 + cochange 后 -> S3 完整收口!
|
||||
l2_vals = [900, 902, 898, 901, 900, 899, 903, 901, 898, 902, 900, 898, 901, 900, 612]
|
||||
drv_vals = [12.0, 12.5, 11.8, 12.2, 12.0, 11.6, 12.4, 12.1, 11.9, 12.3, 12.0, 11.7, 12.2, 12.0, 96.0]
|
||||
q_l2 = _snap("Q3", ["dt", "good_qty"], [[d, v] for d, v in zip(days, l2_vals)], source="ops_shift")
|
||||
q_driver = _snap("Q4", ["dt", "downtime_min"], [[d, v] for d, v in zip(days, drv_vals)], source="equip_master")
|
||||
s3_scope.add_query("Q3", q_l2)
|
||||
s3_scope.add_query("Q4", q_driver)
|
||||
s3_scope.record_query_target(sources=["equip_master"], workflow_id="wf_equip", card_id="m_equip")
|
||||
co_res = await _run_op(
|
||||
{
|
||||
"op": "cochange",
|
||||
"mode": None,
|
||||
"params": {
|
||||
"base_query": "Q3",
|
||||
"target_query": "Q4",
|
||||
"period": "dt",
|
||||
"measure": "good_qty",
|
||||
"driver_measure": "downtime_min",
|
||||
},
|
||||
},
|
||||
s3_scope,
|
||||
cfg,
|
||||
)
|
||||
assert co_res["verdict"]["code"] == "co_anomalous"
|
||||
s3_scope.add_result(co_res, artifact="f" * 64)
|
||||
g_s3_after = investigation.assess(s3_scope, "为什么昨天车间合格产量突然异常下跌?", policy)
|
||||
assert g_s3_after["status"] == "complete", g_s3_after
|
||||
assert g_s3_after["stage"] == "ontology_penetration"
|
||||
|
||||
|
||||
async def test_analysis_claims_v2_reconcile_cochange_data_fault_and_d6_non_causal():
|
||||
lex_v2 = analysis_claims.lexicon(2)
|
||||
lex_v1 = analysis_claims.lexicon(1)
|
||||
v2_keys = {c.key for c in lex_v2.claims}
|
||||
v1_keys = {c.key for c in lex_v1.claims}
|
||||
assert {"reconcile_bad", "cochange", "data_fault"}.issubset(v2_keys)
|
||||
assert "reconcile_bad" not in v1_keys # 老封存报告按 v1 复核不漂移
|
||||
|
||||
cfg = analysis_contract.effective_settings({})
|
||||
scope = AnalysisScope(node_id="bot", snapshot=cfg)
|
||||
days = [f"2026-10-{d:02d}" for d in range(7, 22)]
|
||||
q_p = _snap(
|
||||
"Q1",
|
||||
["dt", "good_qty"],
|
||||
[[d, v] for d, v in zip(days, [900, 902, 898, 901, 900, 899, 903, 901, 898, 902, 900, 898, 901, 900, 612])],
|
||||
source="ops_shift",
|
||||
)
|
||||
q_d = _snap(
|
||||
"Q2",
|
||||
["dt", "downtime_min"],
|
||||
[[d, v] for d, v in zip(days, [12.0, 12.5, 11.8, 12.2, 12.0, 11.6, 12.4, 12.1, 11.9, 12.3, 12.0, 11.7, 12.2, 12.0, 96.0])],
|
||||
source="equip_master",
|
||||
)
|
||||
q_r = _snap("Q3", ["line_code", "scrap_qty"], [["L1", 18], ["L2", 22]], source="ops_shift")
|
||||
q_c = _snap("Q4", ["line_code", "scrap_units"], [["L1", 18], ["L2", 2]], source="quality_scrap")
|
||||
for alias, q in (("Q1", q_p), ("Q2", q_d), ("Q3", q_r), ("Q4", q_c)):
|
||||
scope.add_query(alias, q)
|
||||
|
||||
co_res = await _run_op(
|
||||
{
|
||||
"op": "cochange",
|
||||
"mode": None,
|
||||
"params": {"base_query": "Q1", "target_query": "Q2", "period": "dt", "measure": "good_qty", "driver_measure": "downtime_min"},
|
||||
},
|
||||
scope,
|
||||
cfg,
|
||||
)
|
||||
rec_res = await _run_op(
|
||||
{
|
||||
"op": "reconcile",
|
||||
"mode": None,
|
||||
"params": {"base_query": "Q3", "target_query": "Q4", "by": ["line_code"], "measure": "scrap_qty", "target_measure": "scrap_units"},
|
||||
},
|
||||
scope,
|
||||
cfg,
|
||||
)
|
||||
h_res = {
|
||||
"kind": "source_health",
|
||||
"verdict": "anomalous",
|
||||
"phrase": "数据源「quality_scrap」存在断更告警(最新日期停在 2026-10-19)",
|
||||
"sources": [{"source": "quality_scrap", "verdict": "anomalous", "issue_count": 1}],
|
||||
"issues": [{"kind": "date_stalled", "severity": "alert", "detail": "最新日期停在 2026-10-19"}],
|
||||
"tables": [{"table": "scrap_ledger", "max_date": "2026-10-19"}],
|
||||
}
|
||||
|
||||
store: dict[str, Any] = {}
|
||||
|
||||
def put(obj: Any) -> str:
|
||||
sha = content_hash(canonical_json(obj).encode())
|
||||
store[sha] = obj
|
||||
return sha
|
||||
|
||||
a1_art = put(co_res)
|
||||
a2_art = put(rec_res)
|
||||
h1_art = put(h_res)
|
||||
ledger = [
|
||||
analysis_contract.ledger_entry(co_res, node_id="bot", exec_no=1, artifact=a1_art, call_id="c1"),
|
||||
analysis_contract.ledger_entry(rec_res, node_id="bot", exec_no=1, artifact=a2_art, call_id="c2"),
|
||||
sh.ledger_entry(h_res, node_id="bot", exec_no=1, artifact=h1_art, call_id="h1"),
|
||||
]
|
||||
catalog = evidence.build_catalog(nodes={}, ledger=ledger, loader=store.get)
|
||||
assert "H1" in catalog and catalog["H1"]["kind"] == sh.LEDGER_KIND
|
||||
|
||||
# 1) D6 核心铁律:只挂 cochange(A1)、没有知识库/输入依据时,写「由于设备停机导致合格产量下降」必须被拦成 causal_unsupported!
|
||||
bad_doc = evidence.compose_doc(
|
||||
"星梭二线由于非计划停机时长上升导致合格产量下降[[see:A1]]。",
|
||||
catalog,
|
||||
node_id="write",
|
||||
run_id="r1",
|
||||
loader=store.get,
|
||||
analysis_claims="strict",
|
||||
)
|
||||
v_bad = analysis_claims.check_doc(bad_doc, catalog)
|
||||
assert any(v["code"] == analysis_claims.CAUSAL for v in v_bad), v_bad
|
||||
|
||||
# 2) 合规写法:引用系统判定语 [[a:A1]] 并写统计伴随异动 -> 0 违规通过!
|
||||
good_cochange = "[[a:A1]]。两者在统计上伴随同步异动,具体工艺原因待结合现场检修记录核实[[see:A1]]。"
|
||||
doc_good = evidence.compose_doc(
|
||||
good_cochange,
|
||||
catalog,
|
||||
node_id="write",
|
||||
run_id="r1",
|
||||
loader=store.get,
|
||||
analysis_claims="strict",
|
||||
)
|
||||
assert analysis_claims.check_doc(doc_good, catalog) == []
|
||||
|
||||
# 3) 数据源断更/对账不一致:挂 H1 与 A2 -> 0 违规通过;无依据空口写「数据源断更」被拦成 analysis_claim_uncited
|
||||
good_fault = "[[a:A2]]。[[h:H1]],由于数据源断更导致双源报废数未对齐,不作业务滑坡归因[[see:A2,H1]]。"
|
||||
doc_fault = evidence.compose_doc(
|
||||
good_fault,
|
||||
catalog,
|
||||
node_id="write",
|
||||
run_id="r1",
|
||||
loader=store.get,
|
||||
analysis_claims="strict",
|
||||
)
|
||||
assert analysis_claims.check_doc(doc_fault, catalog) == []
|
||||
|
||||
doc_unbacked = evidence.compose_doc(
|
||||
"质检台账昨天数据源断更,而且双源对账不一致。",
|
||||
catalog,
|
||||
node_id="write",
|
||||
run_id="r1",
|
||||
loader=store.get,
|
||||
analysis_claims="strict",
|
||||
)
|
||||
v_unbacked = analysis_claims.check_doc(doc_unbacked, catalog)
|
||||
assert any(v["code"] == analysis_claims.UNCITED for v in v_unbacked), v_unbacked
|
||||
|
||||
# 4) verify_doc 复核通过且 analysis_scope.build 把 reconcile / cochange 收入 phenomena
|
||||
verify_res = evidence.verify_doc(doc_fault, catalog, loader=store.get)
|
||||
assert verify_res["ok"] is True, verify_res
|
||||
scope_sec = analysis_scope.build(catalog, sources={}, load=store.get)
|
||||
assert scope_sec is not None
|
||||
ops_in_phenomena = {item["op"] for item in scope_sec["phenomena"]}
|
||||
assert {"reconcile", "cochange"}.issubset(ops_in_phenomena)
|
||||
|
||||
|
||||
@pytest.mark.skipif(NODE is None, reason="没有 node,无法执行离线包 verifier.js")
|
||||
async def test_health_citation_evidence_chain_and_offline_bundle_verifier(tmp_path, scratch_source):
|
||||
seeded = await mso.seed_ontology_fixture(tmp_path, scratch_source, today=date(2026, 10, 22))
|
||||
yesterday = "2026-10-21"
|
||||
|
||||
async with SessionLocal() as session:
|
||||
health_res = await sh.check_source_health(
|
||||
session,
|
||||
workflow_id=seeded["workflow_ids"]["quality"],
|
||||
card_id="m_quality",
|
||||
metric_id="ledger_scrap_units",
|
||||
start=yesterday,
|
||||
end=yesterday,
|
||||
)
|
||||
assert health_res["verdict"] == "anomalous"
|
||||
|
||||
files: dict[str, bytes] = {}
|
||||
|
||||
def put(obj: Any) -> str:
|
||||
raw = canonical_json(obj).encode()
|
||||
sha = content_hash(raw)
|
||||
files[sha] = raw
|
||||
return sha
|
||||
|
||||
def load(sha: str) -> Any:
|
||||
raw = files.get(sha)
|
||||
return json.loads(raw) if raw is not None else None
|
||||
|
||||
h_art = put(health_res)
|
||||
h_entry = sh.ledger_entry(health_res, node_id="bot", exec_no=1, artifact=h_art, call_id="call_h1")
|
||||
catalog = evidence.build_catalog(nodes={}, ledger=[h_entry], loader=load)
|
||||
assert "H1" in catalog
|
||||
|
||||
# 报告正文使用 [[h:H1]] 渲染源健康判定语,并用 [[see:H1]] 挂接句末依据
|
||||
report_md = "## 跨源对账核查\n\n[[h:H1]]。因此昨日台账缺失属于源表同步断更,不做业务归因[[see:H1]]。"
|
||||
run_id = "9a1e0c3a5d9f4a2e8c6b4d2f0a1e3c5b"
|
||||
doc = evidence.compose_doc(report_md, catalog, node_id="write", run_id=run_id, loader=load)
|
||||
doc_id = put(doc)
|
||||
|
||||
# 1) 服务端 verify_doc 校验通过
|
||||
checked = evidence.verify_doc(doc, catalog, loader=load)
|
||||
assert checked["ok"] is True, checked
|
||||
|
||||
# 2) 构建离线可验证报告包并用 node 执行 verifier.js 逐字节核验
|
||||
run_meta = {
|
||||
"id": run_id,
|
||||
"workflow_id": "wf-ontology-s2",
|
||||
"workflow_name": "跨源健康排查",
|
||||
"version": 1,
|
||||
"version_hash": "ab" * 32,
|
||||
"run_class": "formal",
|
||||
"status": "succeeded",
|
||||
"started_at": "2026-10-22T01:00:00+00:00",
|
||||
"finished_at": "2026-10-22T01:01:00+00:00",
|
||||
}
|
||||
graph = {
|
||||
"nodes": [
|
||||
{"id": "bot", "type": "agent", "data": {"label": "本体排查Agent"}},
|
||||
{"id": "write", "type": "report", "data": {"label": "排查报告"}},
|
||||
{"id": "out", "type": "output", "data": {"label": "成果"}},
|
||||
]
|
||||
}
|
||||
events: list[tuple[int, str, str | None, Any]] = [
|
||||
(1, "run.started", None, {"nodes": 3, "resumed": False, "tool_trust": "formal", "replay_protocol": 2}),
|
||||
(
|
||||
2,
|
||||
"tool.end",
|
||||
"bot",
|
||||
{
|
||||
"tool": "source_health",
|
||||
"duration_ms": 4,
|
||||
"health_artifact": {"ref": "H1", "artifact": h_art, "verdict": "anomalous", "phrase": health_res["phrase"]},
|
||||
},
|
||||
),
|
||||
(3, "node.finished", "bot", {"duration_ms": 10, "attempt": 1, "evidence": [h_entry]}),
|
||||
(4, "report.checked", "write", {"doc_artifact": doc_id, "ok": True}),
|
||||
(5, "node.finished", "write", {"duration_ms": 8, "attempt": 1, "preview": {"text": "(报告正文)", "doc_artifact": doc_id}}),
|
||||
(6, "issuance", "out", {"tier": "formal", "mode": "citations"}),
|
||||
(7, "run.finished", None, {"output": {"报告": "(报告正文)"}}),
|
||||
]
|
||||
bundle = build_bundle(
|
||||
run_meta,
|
||||
events,
|
||||
manifest_seq=len(events),
|
||||
manifest_hash=manifest_hash(events),
|
||||
read=files.get,
|
||||
exported_at="2026-10-22T02:00:00+00:00",
|
||||
graph=graph,
|
||||
source_masks={},
|
||||
)
|
||||
assert any(a["id"] == h_art and a["kind"] == sh.ARTIFACT_KIND for a in bundle["artifacts"])
|
||||
verdict = node("verify", data=bundle, seal=bundle["meta"]["manifest_hash"])
|
||||
assert verdict["overall"]["result"] == "pass", verdict
|
||||
|
||||
# 3) 篡改报告里 [[h:H1]] 展示文字并重封事件哈希 -> verifier.js 必须精准检出健康判定语不一致!
|
||||
art, forged_doc = doc_of(bundle)
|
||||
seg = next(s for s in evidence.iter_segments(forged_doc) if s.get("ref") == "h:H1")
|
||||
fake_text = "数据源「quality_scrap」完全正常无任何延迟"
|
||||
forged_doc["markdown"] = forged_doc["markdown"].replace(seg["text"], fake_text, 1)
|
||||
seg["cite"]["rendered"] = seg["text"] = fake_text
|
||||
resealed = reseal(bundle, art, forged_doc)
|
||||
bad_verdict = node("verify", data=resealed, seal=resealed["meta"]["manifest_hash"])
|
||||
assert bad_verdict["overall"]["result"] == "fail", bad_verdict
|
||||
@@ -0,0 +1,353 @@
|
||||
"""Wave 1-a 单元测试:标准实体 CanonicalEntity、指标本体 links、DAG 无环门禁、指纹/发布清单后向兼容与 /api/ontology 接口。"""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy import delete
|
||||
|
||||
from app.api.workflows import commit_publish
|
||||
from app.apps import publications
|
||||
from app.apps.validation import gate_issues
|
||||
from app.core import artifact_store
|
||||
from app.data import caliber_identity
|
||||
from app.data.entities import (
|
||||
EntityConflict,
|
||||
EntityInvalid,
|
||||
confirmed_bindings,
|
||||
create_entity,
|
||||
delete_entity,
|
||||
entities_bridging_sources,
|
||||
entity_to_dict,
|
||||
resolve_caliber_param,
|
||||
review_entity_item,
|
||||
translate_entity_value,
|
||||
update_entity,
|
||||
)
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Workflow, WorkflowPublication, WorkflowVersion
|
||||
from app.db.models_ontology import CanonicalEntity
|
||||
from app.ledger.docdraft import normalize_metrics
|
||||
from app.ledger.ontology import (
|
||||
ONTOLOGY_CYCLE_CODE,
|
||||
OntologyCycleError,
|
||||
active_links,
|
||||
build_published_ontology_graph,
|
||||
card_metric_lineage,
|
||||
check_factor_dag,
|
||||
find_factor_cycles,
|
||||
ontology_manifest_section,
|
||||
)
|
||||
from app.main import app
|
||||
from tests.fixtures.acceptance import multi_source_ontology as mso
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def _clean_entities():
|
||||
yield
|
||||
async with SessionLocal() as session:
|
||||
await session.execute(delete(CanonicalEntity))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def test_canonical_entity_crud_optimistic_lock_and_proposed_gating():
|
||||
async with SessionLocal() as session:
|
||||
row = await create_entity(
|
||||
session,
|
||||
{
|
||||
"key": "work_line_test",
|
||||
"label": "示范产线",
|
||||
"aliases": ["产线编码", "工位线"],
|
||||
"bindings": [
|
||||
{
|
||||
"source_name": "ops_shift",
|
||||
"table_name": "shift_output",
|
||||
"column_name": "line_code",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": "equip_master",
|
||||
"table_name": "line_specs",
|
||||
"column_name": "equip_line_id",
|
||||
"status": "confirmed",
|
||||
},
|
||||
{
|
||||
"source_name": "quality_scrap",
|
||||
"table_name": "scrap_ledger",
|
||||
"column_name": "workcenter_code",
|
||||
"source": "ai",
|
||||
},
|
||||
],
|
||||
"caliber_params": [
|
||||
{"workflow_id": "wf_eq", "param_name": "equip_line", "status": "confirmed"},
|
||||
{"workflow_id": "wf_q", "param_name": "wc_code", "status": "proposed"},
|
||||
],
|
||||
"members": [
|
||||
{
|
||||
"value": "L1",
|
||||
"label": "星梭一线",
|
||||
"aliases": ["一线"],
|
||||
"source_codes": {"ops_shift": "L1", "equip_master": "EQ-L1", "quality_scrap": "WC-01"},
|
||||
}
|
||||
],
|
||||
},
|
||||
actor="builder_a",
|
||||
)
|
||||
await session.commit()
|
||||
entity_id = row.id
|
||||
assert row.version == 1
|
||||
|
||||
# AI 来源未显式指定 status 时自动落为 proposed,不进 confirmed_bindings
|
||||
active = confirmed_bindings(row)
|
||||
assert {b["source_name"] for b in active} == {"ops_shift", "equip_master"}
|
||||
|
||||
# 确定性跨源值翻译:精确命中 L1 -> EQ-L1,子串或未知编码返回 None(绝不模糊猜测)
|
||||
translated = translate_entity_value(row, "L1", from_source="ops_shift", to_source="equip_master")
|
||||
assert translated == {"canonical": "L1", "label": "星梭一线", "target_value": "EQ-L1"}
|
||||
assert translate_entity_value(row, "L10", from_source="ops_shift", to_source="equip_master") is None
|
||||
|
||||
# 口径参数解析:confirmed 可见,proposed 被拦下
|
||||
assert resolve_caliber_param(row, workflow_id="wf_eq") == "equip_line"
|
||||
assert resolve_caliber_param(row, workflow_id="wf_q") is None
|
||||
|
||||
# 跨源桥接:只列出 confirmed 绑定的对侧数据源
|
||||
bridges = entities_bridging_sources([row], source_name="ops_shift", tables=["shift_output"])
|
||||
assert len(bridges) == 1 and bridges[0]["peer_sources"] == ["equip_master"]
|
||||
|
||||
# 受限视角(restricted=True):隐去物理绑定、物理表列名与底层 source_codes
|
||||
restricted_view = entity_to_dict(row, restricted=True)
|
||||
assert "bindings" not in restricted_view
|
||||
assert "shift_output" not in str(restricted_view) and "line_code" not in str(restricted_view)
|
||||
for m in restricted_view["members"]:
|
||||
assert "source_codes" not in m
|
||||
|
||||
# 审阅确认 AI 候选绑定后,quality_scrap 进入生效绑定,版本号自增
|
||||
proposed_item = next(b for b in (row.bindings or []) if b["source_name"] == "quality_scrap")
|
||||
updated = await review_entity_item(
|
||||
session,
|
||||
entity_id,
|
||||
target_kind="binding",
|
||||
item_id=proposed_item["id"],
|
||||
status="confirmed",
|
||||
actor="reviewer_b",
|
||||
expected_version=1,
|
||||
)
|
||||
await session.commit()
|
||||
assert updated.version == 2
|
||||
assert {b["source_name"] for b in confirmed_bindings(updated)} == {
|
||||
"ops_shift",
|
||||
"equip_master",
|
||||
"quality_scrap",
|
||||
}
|
||||
|
||||
# 乐观锁冲突:携带旧版本号 v1 更新或审阅必须抛 EntityConflict
|
||||
with pytest.raises(EntityConflict):
|
||||
await update_entity(session, entity_id, {"label": "过期写入", "version": 1}, actor="builder_a")
|
||||
|
||||
# 唯一键冲突与非法 key 校验
|
||||
with pytest.raises(EntityConflict):
|
||||
await create_entity(session, {"key": "work_line_test", "label": "重复"})
|
||||
with pytest.raises(EntityInvalid):
|
||||
await create_entity(session, {"key": "Bad-Key!", "label": "非法"})
|
||||
|
||||
assert await delete_entity(session, entity_id) is True
|
||||
await session.commit()
|
||||
|
||||
|
||||
def test_factor_cycle_detection_and_docdraft_proposed_links():
|
||||
# 无环 factor 链放行
|
||||
check_factor_dag([("wf1:c1:oee", "wf1:c1:avail"), ("wf1:c1:avail", "wf2:c2:run_time")])
|
||||
|
||||
# 二跳环与三跳环均精准检出
|
||||
cycles_2 = find_factor_cycles([("a", "b"), ("b", "a")])
|
||||
assert len(cycles_2) == 1
|
||||
with pytest.raises(OntologyCycleError) as exc_info:
|
||||
check_factor_dag([("m1", "m2"), ("m2", "m3"), ("m3", "m1")])
|
||||
assert exc_info.value.code == ONTOLOGY_CYCLE_CODE
|
||||
|
||||
# 文档起草(docdraft)即使模型在草稿里写了 status="confirmed",落库前一律强制降为 proposed 且带 source="docdraft"
|
||||
draft_metrics, dropped = normalize_metrics(
|
||||
[
|
||||
{
|
||||
"id": "load_rate",
|
||||
"name": "产线负荷率",
|
||||
"unit": "%",
|
||||
"citations": [{"chunk": "c0", "quote": "负荷率等于实际每小时产量除以额定每小时产量"}],
|
||||
"links": [
|
||||
{
|
||||
"kind": "factor",
|
||||
"target": {"workflow_id": "wf_eq", "card_id": "m_eq", "metric_id": "rated_qty"},
|
||||
"role": "denominator",
|
||||
"status": "confirmed",
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
chunks={"c0": {"content": "负荷率等于实际每小时产量除以额定每小时产量。", "document_id": "doc_1"}},
|
||||
)
|
||||
assert dropped == []
|
||||
assert len(draft_metrics) == 1
|
||||
links = draft_metrics[0]["links"]
|
||||
assert len(links) == 1
|
||||
assert links[0]["status"] == "proposed" and links[0]["source"] == "docdraft"
|
||||
assert active_links(links) == []
|
||||
|
||||
|
||||
async def test_definition_hash_and_publication_manifest_backward_compatibility(tmp_path, scratch_source):
|
||||
await scratch_source("demo_ops", mso.build(tmp_path).ops_path)
|
||||
|
||||
base_metric = {
|
||||
"id": "good_qty",
|
||||
"name": "合格产量",
|
||||
"expression": "{{ nodes.q1.rows[0].good_qty }}",
|
||||
"unit": "件",
|
||||
}
|
||||
h_plain = caliber_identity.definition_hash("日产率", "", [base_metric])
|
||||
h_empty = caliber_identity.definition_hash("日产率", "", [{**base_metric, "links": []}])
|
||||
assert h_plain == h_empty
|
||||
|
||||
with_link = {
|
||||
**base_metric,
|
||||
"links": [
|
||||
{
|
||||
"kind": "upstream_driver",
|
||||
"target": {"workflow_id": "wf_up", "card_id": "c_up", "metric_id": "down_min"},
|
||||
"status": "confirmed",
|
||||
}
|
||||
],
|
||||
}
|
||||
assert caliber_identity.definition_hash("日产率", "", [with_link]) != h_plain
|
||||
|
||||
def make_graph(metrics_list):
|
||||
return {
|
||||
"nodes": [
|
||||
{"id": "in1", "type": "input", "position": {"x": 0, "y": 0}, "data": {"label": "入口", "config": {}}},
|
||||
{
|
||||
"id": "q1",
|
||||
"type": "tool",
|
||||
"position": {"x": 200, "y": 0},
|
||||
"data": {
|
||||
"label": "取数",
|
||||
"config": {
|
||||
"tool": "db_query__demo_ops",
|
||||
"args": {"sql": "SELECT good_qty FROM shift_output WHERE operator_phone <> 'SECRET-PHONE'"},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "m1",
|
||||
"type": "metrics",
|
||||
"position": {"x": 420, "y": 0},
|
||||
"data": {"label": "指标卡", "config": {"caliber": "日产率", "metrics": metrics_list}},
|
||||
},
|
||||
{"id": "out1", "type": "output", "position": {"x": 640, "y": 0}, "data": {"label": "出口", "config": {}}},
|
||||
],
|
||||
"edges": [
|
||||
{"source": "in1", "target": "q1"},
|
||||
{"source": "q1", "target": "m1"},
|
||||
{"source": "m1", "target": "out1"},
|
||||
],
|
||||
}
|
||||
|
||||
# 1) 无 links 时:ontology_manifest_section 为 None,发布清单完全不含 "ontology" 键(存量哈希零变动)
|
||||
plain_graph = make_graph([base_metric])
|
||||
assert ontology_manifest_section(workflow_id="wf_plain", version=1, graph=plain_graph) is None
|
||||
|
||||
# 自动反向血缘只记 (source, tables),绝不泄漏 SQL 字面量里的敏感字符串
|
||||
lineage = card_metric_lineage(plain_graph, "m1")
|
||||
assert lineage["tables_by_source"] == {"demo_ops": ["shift_output"]}
|
||||
assert "SECRET-PHONE" not in str(lineage)
|
||||
|
||||
async with SessionLocal() as session:
|
||||
wf1 = Workflow(name="无本体边工作流测试", description="", graph=plain_graph, version=1)
|
||||
session.add(wf1)
|
||||
await session.flush()
|
||||
snap1 = WorkflowVersion(workflow_id=wf1.id, version=1, graph=plain_graph, note="v1")
|
||||
session.add(snap1)
|
||||
await session.commit()
|
||||
await session.refresh(wf1)
|
||||
await session.refresh(snap1)
|
||||
pub1 = await commit_publish(session, wf1, snap1, "published", "tester")
|
||||
manifest1 = artifact_store.load(pub1["manifest"])
|
||||
assert "ontology" not in manifest1
|
||||
|
||||
# 2) 含自环 factor 边的工作流在发布预检 gate_issues 阶段被直接拦下
|
||||
cyclic_graph = make_graph(
|
||||
[
|
||||
{
|
||||
**base_metric,
|
||||
"links": [
|
||||
{
|
||||
"kind": "factor",
|
||||
"target": {"workflow_id": wf1.id, "card_id": "m1", "metric_id": "good_qty"},
|
||||
"status": "confirmed",
|
||||
}
|
||||
],
|
||||
}
|
||||
]
|
||||
)
|
||||
issues = await gate_issues(session, cyclic_graph, "published", workflow_id=wf1.id)
|
||||
assert issues is not None
|
||||
assert any(i["level"] == "error" and i.get("code") == ONTOLOGY_CYCLE_CODE for i in issues)
|
||||
|
||||
# 3) 合法本体边发布后物化进清单,且 publications.rebuild 重建结果逐字节一致
|
||||
linked_graph = make_graph([with_link])
|
||||
wf2 = Workflow(name="含本体边工作流测试", description="", graph=linked_graph, version=1)
|
||||
session.add(wf2)
|
||||
await session.flush()
|
||||
snap2 = WorkflowVersion(workflow_id=wf2.id, version=1, graph=linked_graph, note="v1")
|
||||
session.add(snap2)
|
||||
await session.commit()
|
||||
await session.refresh(wf2)
|
||||
await session.refresh(snap2)
|
||||
pub2 = await commit_publish(session, wf2, snap2, "published", "tester")
|
||||
manifest2 = artifact_store.load(pub2["manifest"])
|
||||
assert "ontology" in manifest2
|
||||
assert "SECRET-PHONE" not in str(manifest2["ontology"])
|
||||
pub_row = await session.get(WorkflowPublication, pub2["publication"]["id"])
|
||||
assert pub_row is not None
|
||||
rebuilt = await publications.rebuild(session, pub_row)
|
||||
assert rebuilt["manifest"] == pub2["manifest"]
|
||||
assert rebuilt["matches"] is True
|
||||
|
||||
|
||||
async def test_ontology_http_endpoints_and_published_graph(tmp_path, scratch_source):
|
||||
seeded = await mso.seed_ontology_fixture(tmp_path, scratch_source, today=date(2026, 10, 22))
|
||||
ops_wf = seeded["workflow_ids"]["ops"]
|
||||
|
||||
async with SessionLocal() as session:
|
||||
active_graph = await build_published_ontology_graph(session, include_proposed=False)
|
||||
all_graph = await build_published_ontology_graph(session, include_proposed=True)
|
||||
# ops 工作流里有 3 条 confirmed 边 + 1 条 proposed 边
|
||||
ops_active = [e for e in active_graph["edges"] if e["from"]["workflow_id"] == ops_wf]
|
||||
ops_all = [e for e in all_graph["edges"] if e["from"]["workflow_id"] == ops_wf]
|
||||
assert len(ops_active) == 3
|
||||
assert len(ops_all) == 4
|
||||
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
res_list = await client.get("/api/ontology/entities", params={"source_name": "ops_shift"})
|
||||
assert res_list.status_code == 200
|
||||
keys = [e["key"] for e in res_list.json()["items"]]
|
||||
assert "production_line" in keys
|
||||
|
||||
res_graph = await client.get("/api/ontology/graph")
|
||||
assert res_graph.status_code == 200
|
||||
payload = res_graph.json()
|
||||
assert any(e["key"] == "production_line" for e in payload["entities"])
|
||||
assert any(edge["kind"] == "factor" for edge in payload["edges"])
|
||||
|
||||
res_wf = await client.get(f"/api/ontology/workflows/{ops_wf}")
|
||||
assert res_wf.status_code == 200
|
||||
wf_index = res_wf.json()
|
||||
assert wf_index["workflow_id"] == ops_wf
|
||||
assert wf_index["cards"][0]["tables_by_source"] == {"ops_shift": ["shift_output"]}
|
||||
|
||||
# 乐观锁 409 验证
|
||||
entity_id = seeded["entity_id"]
|
||||
res_conflict = await client.patch(
|
||||
f"/api/ontology/entities/{entity_id}",
|
||||
json={"label": "冲突测试", "version": 999},
|
||||
)
|
||||
assert res_conflict.status_code == 409
|
||||
@@ -0,0 +1,369 @@
|
||||
"""Wave 1-b 单元测试:数据源健康度检查 check_source_health、跨源对账算子 reconcile、伴随异动算子 cochange 及 Agent 工具接入。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from datetime import date
|
||||
|
||||
import pytest
|
||||
from langchain_core.messages import AIMessage, ToolMessage
|
||||
from sqlalchemy import delete, select
|
||||
|
||||
import app.analysis as analysis_ops
|
||||
from app.analysis import contract
|
||||
from app.analysis.contract import CallError
|
||||
from app.core import artifact_store
|
||||
from app.data import source_changes as sc
|
||||
from app.data import source_health as sh
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import DataSource, Run, RunEvent
|
||||
from app.db.models_ontology import CanonicalEntity
|
||||
from app.engine.runner import run_manager
|
||||
from app.tools.analysis import input_snapshot
|
||||
from tests.fixtures.acceptance import multi_source_ontology as mso
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def _engine_lifecycle():
|
||||
await run_manager.setup()
|
||||
yield
|
||||
await run_manager.shutdown()
|
||||
async with SessionLocal() as session:
|
||||
await session.execute(delete(CanonicalEntity))
|
||||
await session.commit()
|
||||
|
||||
|
||||
def _snap(ref: str, columns: list[str], rows: list[list], *, masked: list[str] | None = None,
|
||||
truncated: bool = False, source: str = "ops_shift", artifact: str | None = None) -> dict:
|
||||
return input_snapshot(
|
||||
{
|
||||
"columns": columns,
|
||||
"rows": rows,
|
||||
"sql": f"SELECT {', '.join(columns)} FROM t",
|
||||
"source": source,
|
||||
"truncated": truncated,
|
||||
},
|
||||
ref=ref,
|
||||
artifact=artifact or (ref.lower().ljust(64, "0")[:64]),
|
||||
node_id="bot",
|
||||
masked=masked or [],
|
||||
)
|
||||
|
||||
|
||||
async def test_source_health_verdicts_masking_and_caliber_reverse_lineage(tmp_path, scratch_source, monkeypatch):
|
||||
seeded = await mso.seed_ontology_fixture(tmp_path, scratch_source, today=date(2026, 10, 22))
|
||||
yesterday = "2026-10-21"
|
||||
|
||||
async with SessionLocal() as session:
|
||||
# 1) ops_shift 数据最新覆盖到昨天 -> healthy,且探查绝不触碰遮罩列 operator_phone
|
||||
healthy = await sh.check_source_health(
|
||||
session,
|
||||
source_name=mso.SOURCE_OPS,
|
||||
tables=["shift_output"],
|
||||
start="2026-10-15",
|
||||
end=yesterday,
|
||||
)
|
||||
assert healthy["verdict"] == sh.HEALTHY
|
||||
assert "operator_phone" not in str(healthy)
|
||||
assert "138-0010" not in str(healthy)
|
||||
|
||||
# 2) quality_scrap 在昨天断更(SourceChange date_stalled + 现查最大日期 2026-10-19 < 2026-10-21)-> anomalous
|
||||
anomalous = await sh.check_source_health(
|
||||
session,
|
||||
source_name=mso.SOURCE_QUALITY,
|
||||
tables=["scrap_ledger"],
|
||||
start=yesterday,
|
||||
end=yesterday,
|
||||
)
|
||||
assert anomalous["verdict"] == sh.ANOMALOUS
|
||||
assert any(i["kind"] == sc.DATE_STALLED for i in anomalous["issues"])
|
||||
assert "inspector_id" not in str(anomalous)
|
||||
assert "INSP-900" not in str(anomalous)
|
||||
|
||||
# 3) restricted=True 受限视角隐去物理表名、物理列名与底层数据源标识
|
||||
restricted_res = await sh.check_source_health(
|
||||
session,
|
||||
source_name=mso.SOURCE_QUALITY,
|
||||
tables=["scrap_ledger"],
|
||||
start=yesterday,
|
||||
end=yesterday,
|
||||
restricted=True,
|
||||
)
|
||||
assert restricted_res["verdict"] == sh.ANOMALOUS
|
||||
assert "scrap_ledger" not in str(restricted_res)
|
||||
assert "inspect_date" not in str(restricted_res)
|
||||
|
||||
# 4) 按口径卡反向血缘自动解析底层数据源与物理表
|
||||
via_caliber = await sh.check_source_health(
|
||||
session,
|
||||
workflow_id=seeded["workflow_ids"]["quality"],
|
||||
card_id="m_quality",
|
||||
metric_id="ledger_scrap_units",
|
||||
start=yesterday,
|
||||
end=yesterday,
|
||||
)
|
||||
assert via_caliber["verdict"] == sh.ANOMALOUS
|
||||
assert via_caliber["caliber_ref"]["workflow_id"] == seeded["workflow_ids"]["quality"]
|
||||
|
||||
# 5) codes_new 新码值提醒 -> degraded
|
||||
equip_src = await session.get(DataSource, seeded["source_ids"][mso.SOURCE_EQUIP])
|
||||
await sc.record(
|
||||
session,
|
||||
equip_src,
|
||||
sc.NewChange(kind=sc.CODES_NEW, table="downtime_events", column="fault_category", severity="warning", key="new_code"),
|
||||
sc.Detected(job_kind="profile", trigger="manual", actor="tester"),
|
||||
)
|
||||
await session.commit()
|
||||
degraded = await sh.check_source_health(
|
||||
session,
|
||||
source_name=mso.SOURCE_EQUIP,
|
||||
tables=["downtime_events"],
|
||||
start="2026-10-15",
|
||||
end=yesterday,
|
||||
)
|
||||
assert degraded["verdict"] == sh.DEGRADED
|
||||
|
||||
# 6) 工件存储与台账条目形状校验
|
||||
art_id = await sh.store_source_health(anomalous, run_id=None, node_id="bot")
|
||||
loaded = artifact_store.load(art_id)
|
||||
assert sh.is_health_result(loaded)
|
||||
entry = sh.ledger_entry(anomalous, node_id="bot", exec_no=1, artifact=art_id, call_id="c1")
|
||||
assert entry["kind"] == "source_health" and entry["verdict"] == sh.ANOMALOUS
|
||||
|
||||
# 7) 实时探查异常降级为 unverified 而不抛未捕获异常
|
||||
async def _boom(*_a, **_kw):
|
||||
raise TimeoutError("simulated timeout")
|
||||
|
||||
monkeypatch.setattr(sh, "run_query", _boom)
|
||||
fallback = await sh.check_source_health(session, source_name=mso.SOURCE_OPS, tables=["shift_output"])
|
||||
assert fallback["verdict"] == sh.UNVERIFIED
|
||||
|
||||
|
||||
async def _run_op(req: dict, resolve, *, refuse_data_errors: bool = False) -> dict:
|
||||
return await analysis_ops.run(
|
||||
req,
|
||||
resolve,
|
||||
settings=contract.DEFAULTS,
|
||||
context={},
|
||||
refuse_data_errors=refuse_data_errors,
|
||||
)
|
||||
|
||||
|
||||
async def test_reconcile_operator_verdicts_errors_and_byte_identical_recompute():
|
||||
q_left = _snap("Q1", ["line_code", "scrap_qty", "operator_phone"], [["L1", 20, "***"], ["L2", 50, "***"]], masked=["operator_phone"])
|
||||
q_right_ok = _snap("Q2", ["wc_line", "scrap_units"], [["L1", 20], ["L2", 50]], source="quality_scrap")
|
||||
q_right_div = _snap("Q2", ["wc_line", "scrap_units"], [["L1", 20], ["L2", 35]], source="quality_scrap")
|
||||
q_right_miss = _snap("Q2", ["wc_line", "scrap_units"], [["L1", 20]], source="quality_scrap")
|
||||
|
||||
# 1) 完全对齐 -> aligned
|
||||
res_aligned = await _run_op(
|
||||
{
|
||||
"op": "reconcile",
|
||||
"mode": None,
|
||||
"params": {
|
||||
"base_query": "Q1",
|
||||
"target_query": "Q2",
|
||||
"by": ["line_code"],
|
||||
"target_by": ["wc_line"],
|
||||
"measure": "scrap_qty",
|
||||
"target_measure": "scrap_units",
|
||||
"label": "报废件数",
|
||||
},
|
||||
},
|
||||
lambda name: {"Q1": q_left, "Q2": q_right_ok}.get(name),
|
||||
)
|
||||
assert res_aligned["status"] == "ok" and res_aligned["verdict"]["code"] == "aligned"
|
||||
|
||||
# 2) 超差 -> diverged,且双盲复算重算逐字节一致(contract.same)
|
||||
req_div = {
|
||||
"op": "reconcile",
|
||||
"mode": None,
|
||||
"params": {
|
||||
"base_query": "Q1",
|
||||
"target_query": "Q2",
|
||||
"by": ["line_code"],
|
||||
"target_by": ["wc_line"],
|
||||
"measure": "scrap_qty",
|
||||
"target_measure": "scrap_units",
|
||||
"label": "报废件数",
|
||||
},
|
||||
}
|
||||
res_div = await _run_op(req_div, lambda name: {"Q1": q_left, "Q2": q_right_div}.get(name))
|
||||
res_div_again = await _run_op(req_div, lambda name: {"Q1": q_left, "Q2": q_right_div}.get(name))
|
||||
assert res_div["status"] == "ok" and res_div["verdict"]["code"] == "diverged"
|
||||
assert res_div["verdict"]["subjects"] == [["L2"]]
|
||||
assert contract.same(res_div, res_div_again)
|
||||
|
||||
# 3) 对侧缺失 -> missing_counterpart
|
||||
res_miss = await _run_op(
|
||||
{**req_div},
|
||||
lambda name: {"Q1": q_left, "Q2": q_right_miss}.get(name),
|
||||
)
|
||||
assert res_miss["status"] == "ok" and res_miss["verdict"]["code"] == "missing_counterpart"
|
||||
|
||||
# 4) 单份查询 + unit_factor 换算(千瓦时 vs 兆瓦时 * 1000)
|
||||
q_single = _snap("Q3", ["line_code", "kwh", "mwh"], [["L1", 12000, 12.0], ["L2", 13000, 13.0]])
|
||||
res_unit = await _run_op(
|
||||
{
|
||||
"op": "reconcile",
|
||||
"mode": None,
|
||||
"params": {"query": "Q3", "by": ["line_code"], "measure": "kwh", "target_measure": "mwh", "unit_factor": 1000},
|
||||
},
|
||||
lambda name: q_single if name == "Q3" else None,
|
||||
)
|
||||
assert res_unit["status"] == "ok" and res_unit["verdict"]["code"] == "aligned"
|
||||
|
||||
# 5) 遮罩列拒收 + 截断数据在 refuse_data_errors=True 时返回 refused
|
||||
with pytest.raises(CallError):
|
||||
await _run_op(
|
||||
{
|
||||
"op": "reconcile",
|
||||
"mode": None,
|
||||
"params": {"base_query": "Q1", "target_query": "Q2", "by": ["operator_phone"], "measure": "scrap_qty"},
|
||||
},
|
||||
lambda name: {"Q1": q_left, "Q2": q_right_ok}.get(name),
|
||||
)
|
||||
q_trunc = _snap("QT", ["line_code", "kwh", "mwh"], [["L1", 12000, 10.0]], truncated=True)
|
||||
res_trunc = await _run_op(
|
||||
{"op": "reconcile", "mode": None, "params": {"query": "QT", "by": ["line_code"], "measure": "kwh", "target_measure": "mwh"}},
|
||||
lambda name: q_trunc if name == "QT" else None,
|
||||
refuse_data_errors=True,
|
||||
)
|
||||
assert res_trunc["status"] == "refused"
|
||||
|
||||
|
||||
async def test_cochange_operator_verdicts_lag_non_causal_phrase_and_recompute():
|
||||
days = [f"2026-10-{d:02d}" for d in range(1, 16)]
|
||||
# 前 14 期对称平稳小幅波动,第 15 期主指标暴跌、驱动指标暴涨
|
||||
p_base = [100, 102, 99, 101, 100, 98, 103, 101, 99, 102, 100, 98, 101, 100]
|
||||
d_base = [12.0, 12.5, 11.8, 12.2, 12.0, 11.6, 12.4, 12.1, 11.9, 12.3, 12.0, 11.7, 12.2, 12.0]
|
||||
p_vals = p_base + [68]
|
||||
d_spike = d_base + [85.0]
|
||||
d_flat = d_base + [12.1]
|
||||
|
||||
q_primary = _snap("Q1", ["shift_date", "good_qty"], [[d, v] for d, v in zip(days, p_vals)])
|
||||
q_driver_spike = _snap("Q2", ["event_date", "unplanned_min"], [[d, v] for d, v in zip(days, d_spike)], source="equip_master")
|
||||
q_driver_flat = _snap("Q2", ["event_date", "unplanned_min"], [[d, v] for d, v in zip(days, d_flat)], source="equip_master")
|
||||
|
||||
req_co = {
|
||||
"op": "cochange",
|
||||
"mode": None,
|
||||
"params": {
|
||||
"base_query": "Q1",
|
||||
"target_query": "Q2",
|
||||
"period": "shift_date",
|
||||
"target_period": "event_date",
|
||||
"measure": "good_qty",
|
||||
"driver_measure": "unplanned_min",
|
||||
"label": "合格产量",
|
||||
},
|
||||
}
|
||||
res_co = await _run_op(req_co, lambda name: {"Q1": q_primary, "Q2": q_driver_spike}.get(name))
|
||||
res_co_2 = await _run_op(req_co, lambda name: {"Q1": q_primary, "Q2": q_driver_spike}.get(name))
|
||||
assert res_co["status"] == "ok" and res_co["verdict"]["code"] == "co_anomalous"
|
||||
assert contract.same(res_co, res_co_2)
|
||||
# 决策 D6:系统判定语严禁输出因果词(导致、引起、造成)
|
||||
for forbidden in ("导致", "引起", "造成", "归因于"):
|
||||
assert forbidden not in res_co["phrase"]
|
||||
|
||||
# 上游平稳 -> target_normal
|
||||
res_norm_target = await _run_op(req_co, lambda name: {"Q1": q_primary, "Q2": q_driver_flat}.get(name))
|
||||
assert res_norm_target["status"] == "ok" and res_norm_target["verdict"]["code"] == "target_normal"
|
||||
|
||||
# 主指标平稳 -> primary_normal
|
||||
q_primary_steady = _snap("Q1", ["shift_date", "good_qty"], [[d, v] for d, v in zip(days, p_base + [101])])
|
||||
res_norm_primary = await _run_op(
|
||||
req_co, lambda name: {"Q1": q_primary_steady, "Q2": q_driver_spike}.get(name)
|
||||
)
|
||||
assert res_norm_primary["status"] == "ok" and res_norm_primary["verdict"]["code"] == "primary_normal"
|
||||
|
||||
# lag=1 领先期检验:驱动指标在第 14 期飙升,主指标在第 15 期下跌 -> co_anomalous
|
||||
d_lead = d_base[:-1] + [88.0, 12.0]
|
||||
q_driver_lead = _snap("Q2", ["event_date", "unplanned_min"], [[d, v] for d, v in zip(days, d_lead)], source="equip_master")
|
||||
res_lag = await _run_op(
|
||||
{**req_co, "params": {**req_co["params"], "lag": 1}},
|
||||
lambda name: {"Q1": q_primary, "Q2": q_driver_lead}.get(name),
|
||||
)
|
||||
assert res_lag["status"] == "ok" and res_lag["verdict"]["code"] == "co_anomalous"
|
||||
|
||||
# 历史不足 -> refused (history_short)
|
||||
q_short = _snap("QS", ["shift_date", "good_qty", "unplanned_min"], [[days[i], p_vals[i], d_spike[i]] for i in range(5)])
|
||||
res_short = await _run_op(
|
||||
{
|
||||
"op": "cochange",
|
||||
"mode": None,
|
||||
"params": {"query": "QS", "period": "shift_date", "measure": "good_qty", "driver_measure": "unplanned_min"},
|
||||
},
|
||||
lambda name: q_short if name == "QS" else None,
|
||||
)
|
||||
assert res_short["status"] == "refused" and res_short["reason_code"] == "history_short"
|
||||
|
||||
|
||||
async def test_agent_executes_analysis_reconcile_and_cochange_and_records_ledger(tmp_path, scratch_source, monkeypatch):
|
||||
from app.providers import mock_model
|
||||
|
||||
await mso.seed_ontology_fixture(tmp_path, scratch_source, today=date(2026, 10, 22))
|
||||
calls = [
|
||||
("db_query__ops_shift", {"sql": "SELECT line_code, meter_kwh, settled_mwh FROM energy_readings WHERE shift_date = '2026-10-21' ORDER BY line_code"}),
|
||||
(
|
||||
"analysis_reconcile",
|
||||
{"query": "Q1", "by": ["line_code"], "measure": "meter_kwh", "target_measure": "settled_mwh", "unit_factor": 1000, "label": "产线能耗"},
|
||||
),
|
||||
("db_query__equip_master", {"sql": "SELECT event_date, net_run_ratio, unplanned_min FROM downtime_events WHERE equip_line_id = 'EQ-L2' ORDER BY event_date"}),
|
||||
(
|
||||
"analysis_cochange",
|
||||
{"query": "Q2", "period": "event_date", "measure": "net_run_ratio", "driver_measure": "unplanned_min", "target": "2026-10-21", "label": "有效运转率"},
|
||||
),
|
||||
]
|
||||
|
||||
def decide(model, messages):
|
||||
done = sum(isinstance(m, ToolMessage) for m in messages)
|
||||
if done < len(calls):
|
||||
name, args = calls[done]
|
||||
return AIMessage(content="", tool_calls=[{"name": name, "args": args, "id": f"call_{done}"}])
|
||||
return AIMessage(content="对账与伴随异动核查已完成。")
|
||||
|
||||
monkeypatch.setattr(mock_model.MockChatModel, "_decide", decide)
|
||||
|
||||
nodes = [
|
||||
{"id": "start", "type": "input", "position": {"x": 0, "y": 0}, "data": {"label": "入口", "config": {}}},
|
||||
{
|
||||
"id": "bot",
|
||||
"type": "agent",
|
||||
"position": {"x": 220, "y": 0},
|
||||
"data": {
|
||||
"label": "诊断助手",
|
||||
"config": {
|
||||
"prompt": "执行跨源能耗对账与设备停机伴随异动检验",
|
||||
"tools": ["db_query__ops_shift", "db_query__equip_master", "analysis_reconcile", "analysis_cochange"],
|
||||
"approval": "never",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "out",
|
||||
"type": "output",
|
||||
"position": {"x": 460, "y": 0},
|
||||
"data": {"label": "出口", "config": {"fields": [{"name": "answer", "value": "{{ nodes.bot.text }}"}]}},
|
||||
},
|
||||
]
|
||||
graph = {"nodes": nodes, "edges": [{"source": "start", "target": "bot"}, {"source": "bot", "target": "out"}]}
|
||||
run = await run_manager.start(graph=graph, input_payload={})
|
||||
for _ in range(300):
|
||||
await asyncio.sleep(0.05)
|
||||
async with SessionLocal() as session:
|
||||
row = await session.get(Run, run.id)
|
||||
if row.status in ("succeeded", "failed") and row.manifest_seq is not None:
|
||||
break
|
||||
assert row.status == "succeeded", row.error
|
||||
|
||||
async with SessionLocal() as session:
|
||||
ev = (
|
||||
await session.execute(
|
||||
select(RunEvent).where(RunEvent.run_id == run.id, RunEvent.type == "node.finished", RunEvent.node_id == "bot")
|
||||
)
|
||||
).scalar_one()
|
||||
analysis_entries = [e for e in ev.data["evidence"] if e.get("kind") == "analysis"]
|
||||
assert [(e["op"], e["verdict"]) for e in analysis_entries] == [
|
||||
("reconcile", "diverged"),
|
||||
("cochange", "co_anomalous"),
|
||||
]
|
||||
@@ -38,8 +38,8 @@ def graph() -> dict:
|
||||
|
||||
def test_the_template_is_listed_once_after_the_others():
|
||||
names = [t["name"] for t in TEMPLATES]
|
||||
assert names.count(NAME) == 1 and names[-1] == NAME
|
||||
assert any(n.startswith("⑨ 可追溯周报") for n in names)
|
||||
nine = next(n for n in names if n.startswith("⑨ 可追溯周报"))
|
||||
assert names.count(NAME) == 1 and names.index(NAME) > names.index(nine)
|
||||
|
||||
|
||||
def test_the_template_passes_validation():
|
||||
|
||||
Reference in New Issue
Block a user