From bfa391e3445d194c4600c21f9a7c0562103374d0 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Sat, 3 Oct 2026 12:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9B=AE=E5=BD=95=E9=98=B6?= =?UTF-8?q?=E6=AE=B5=204A=20=E5=8F=91=E5=B8=83=E4=B8=8E=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=EF=BC=9A=E5=8F=91=E5=B8=83=E6=97=B6=E8=AE=B0=E4=B8=8B=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=20SQL=20=E7=94=A8=E5=88=B0=E7=9A=84=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E7=89=88=E6=9C=AC=EF=BC=8C=E6=AD=A3=E5=BC=8F?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E7=9B=AE=E5=BD=95=E6=9C=89=E5=8F=98?= =?UTF-8?q?=E5=8C=96=E5=B0=B1=E5=8F=91=E6=8F=90=E9=86=92=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E3=80=81=E4=B8=8D=E6=8B=A6=E8=BF=90=E8=A1=8C=EF=BC=8C=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E8=AF=A6=E6=83=85=E5=92=8C=E6=97=B6=E9=97=B4=E7=BA=BF?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=8F=90=E9=86=92=EF=BC=8C=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E6=98=BE=E7=A4=BA=E5=8F=91=E5=B8=83=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E7=9B=AE=E5=BD=95=E7=89=88=E6=9C=AC=E5=8F=8A=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/runs.py | 9 + backend/app/api/workflows.py | 10 + backend/app/core/events.py | 3 + backend/app/db/base.py | 2 + backend/app/db/models.py | 4 + backend/tests/test_catalog_publish_drift.py | 199 ++++++++++++++++++++ frontend/src/canvas/VersionsSheet.tsx | 62 +++++- frontend/src/lib/terms.ts | 23 +++ frontend/src/pages/runs/Banners.tsx | 48 ++++- frontend/src/pages/runs/RunDetailView.tsx | 9 +- frontend/src/run/decode.ts | 37 +++- frontend/src/types.ts | 16 ++ scripts/check-decode.mjs | 20 ++ scripts/check-runs.mjs | 44 +++++ scripts/check-studio.mjs | 16 +- 15 files changed, 489 insertions(+), 13 deletions(-) create mode 100644 backend/tests/test_catalog_publish_drift.py diff --git a/backend/app/api/runs.py b/backend/app/api/runs.py index 7578d1bd..bae67e6d 100644 --- a/backend/app/api/runs.py +++ b/backend/app/api/runs.py @@ -17,6 +17,7 @@ from sqlalchemy import select as _select from app.api.coded import CodedHTTPException from app.core.bus import bus from app.core import artifact_store +from app.data.catalog_impact import catalog_drift from app.db.base import SessionLocal, get_session from app.db.models import Approval, Artifact, Run, RunEvent, Workflow, WorkflowVersion from app.engine.governance import governance_note, unresolved_caliber_upgrades @@ -141,9 +142,13 @@ async def start_run( errors, upgrade_events = await unresolved_caliber_upgrades(session, spec) if errors: raise HTTPException(409, ";".join(errors)) + # 数据目录和口径卡不同:目录是给人和助手看的说明,不是运行的输入。发布之后变了只提醒、不拦—— + # 拦下来等于让一次文字修订卡住正式出具 + catalog_changes = await catalog_drift(session, snapshot.catalog_versions) else: graph = payload.graph upgrade_events = [] + catalog_changes = [] if payload.workflow_id: workflow = await session.get(Workflow, payload.workflow_id) if not workflow: @@ -180,6 +185,10 @@ async def start_run( # 审批停下再恢复时再去读它,报告节点和出口复核可能各读到不同的值 await run_manager.note(run.id, "log", **governance_note( governed=governed, workflow_name=name, version=version)) + if catalog_changes: + # 落在封存范围内:之后回看这次正式出具,能知道当时的目录和发布时不一样、差在哪几张表 + await run_manager.note(run.id, "catalog.drift", version=version, count=len(catalog_changes), + tables=catalog_changes) # 已声明策略的升版记入事件流,出具物上能看到"这期换口径了、怎么处置的" for ev in upgrade_events: payload_ev = dict(ev) diff --git a/backend/app/api/workflows.py b/backend/app/api/workflows.py index 5038dfc0..fef036fa 100644 --- a/backend/app/api/workflows.py +++ b/backend/app/api/workflows.py @@ -10,6 +10,7 @@ from sqlalchemy.ext.asyncio import AsyncSession from app.core.errors import graph_error from app.api.runs import actor_of from app.core.artifact_store import graph_hash +from app.data.catalog_impact import catalog_drift, catalog_versions_for from app.db.base import get_session from app.db.models import Run, Workflow, WorkflowVersion from app.engine.governance import publish_issues @@ -209,6 +210,10 @@ class VersionDetailOut(VersionOut): #: 画布上改过入口字段时,拿当前画布的字段去跑已发布版本会传错参数 input_fields: list[dict[str, Any]] = Field(default_factory=list) published: bool = False + #: 发布时这一版 SQL 用到的表的数据目录版本 {源名: {表名: 版本}};没发布过、或者记录之前发布的为 null + catalog_versions: dict[str, Any] | None = None + #: 发布之后目录有变化的表:[{source, source_id, table, label, published, current}] + catalog_changes: list[dict[str, Any]] = Field(default_factory=list) @router.get("/{workflow_id}/versions/{version}", response_model=VersionDetailOut) @@ -243,6 +248,8 @@ async def get_version( created_at=snapshot.created_at, workflow_id=workflow_id, graph=graph, graph_hash=snapshot.graph_hash or graph_hash(graph), input_fields=fields, published=workflow.published_version == snapshot.version, level=snapshot.level, + catalog_versions=snapshot.catalog_versions, + catalog_changes=await catalog_drift(session, snapshot.catalog_versions), ) @@ -333,6 +340,8 @@ async def publish_workflow( snapshot.graph_hash = graph_hash(snapshot.graph) # 级别记在这一版上:之后改画布(status 退回 draft),从这一版发起的正式运行照样按发布时的级别出具 snapshot.level = payload.level + # 发布时的目录版本也记在这一版上:重新发布同一版等于按现在的目录重新认可它,跟着更新 + snapshot.catalog_versions = await catalog_versions_for(session, spec) workflow.status = payload.level workflow.published_version = version workflow.published_by = actor_of(x_actor) @@ -343,6 +352,7 @@ async def publish_workflow( "version": version, "graph_hash": snapshot.graph_hash, "published_by": workflow.published_by, + "catalog_versions": snapshot.catalog_versions, "issues": issues, # 剩下的都是警告 } diff --git a/backend/app/core/events.py b/backend/app/core/events.py index abef3e77..c264829d 100644 --- a/backend/app/core/events.py +++ b/backend/app/core/events.py @@ -69,6 +69,9 @@ class EventType(StrEnum): # 出具与口径治理 ISSUANCE = "issuance" # 出具契约的判定结果(档位 / 缺数据 / 未回指数字) CALIBER_UPGRADE = "caliber.upgrade" # 钉住的方法卡有新版本,按声明的策略处置 + # 从发布版本发起的正式运行:发布之后这一版 SQL 用到的表的数据目录有变化(data/catalog_impact.catalog_drift)。 + # 只提醒、不拦运行;落在封存范围内,出具物上看得到「当时的目录和发布时不一样」 + CATALOG_DRIFT = "catalog.drift" # 报告撰写节点的自查结果:报告文档工件 id、统计、违规清单。落在封存范围内, # 证据接口靠它找到文档——不去翻不受封存保护的 artifacts 表 REPORT_CHECKED = "report.checked" diff --git a/backend/app/db/base.py b/backend/app/db/base.py index 374829ae..2f1e5a4a 100644 --- a/backend/app/db/base.py +++ b/backend/app/db/base.py @@ -152,6 +152,8 @@ _COLUMN_MIGRATIONS: list[tuple[str, str, str]] = [ ("workflow_versions", "graph_hash", "VARCHAR(64)"), # 存量版本回填成 NULL:不知道当时按哪一档发布的,正式运行照旧看工作流的 status ("workflow_versions", "level", "VARCHAR(20)"), + # 发布时的数据目录版本(阶段 4A)。存量版本回填成 NULL:不知道当时按哪一版目录发布的,正式运行不提醒 + ("workflow_versions", "catalog_versions", "JSON"), ("approvals", "resolved_by", "VARCHAR(100)"), # 存量向量没有这两列,回填成空串 / 0 —— 正好表示"不知道是谁建的", # 检索侧会把它们当成对不上当前 embedder 处理 diff --git a/backend/app/db/models.py b/backend/app/db/models.py index 200850d1..403b9177 100644 --- a/backend/app/db/models.py +++ b/backend/app/db/models.py @@ -432,6 +432,10 @@ class WorkflowVersion(Base, TimestampMixin): # 这一版按哪一档发布的:published | governed。没发布过、或者是加这一列之前发布的为空。 # 正式运行按不按受管出具看它,而不是工作流的 status——status 说的是当前画布,改一笔就退回 draft level: Mapped[str | None] = mapped_column(String(20), default=None) + # 发布时这一版 SQL 用到的表的数据目录版本:{源名: {表名: 版本}},还没有目录的表记 0(data/catalog_impact.py)。 + # 从这一版发起正式运行时和当前的比,有变化只提醒、不拦。没发布过、或者是加这一列之前发布的为空——不知道 + # 当时是哪一版,不提醒 + catalog_versions: Mapped[dict[str, Any] | None] = mapped_column(default=None) workflow: Mapped[Workflow] = relationship(back_populates="versions") diff --git a/backend/tests/test_catalog_publish_drift.py b/backend/tests/test_catalog_publish_drift.py new file mode 100644 index 00000000..062e0102 --- /dev/null +++ b/backend/tests/test_catalog_publish_drift.py @@ -0,0 +1,199 @@ +"""发布时记下目录版本,正式运行时提示目录的变化(阶段 4A)。 + +- 发布(把某一版立为正式版本)时,记下这个模板 SQL 用到的每张表的目录版本 {源名: {表名: 版本}},存在 + workflow_versions.catalog_versions;还没有目录的表记 0(之后补了目录也算变化)。 +- 从这一版发起正式运行时和当前的目录版本比:有变化发一条 catalog.drift 事件(列出表和前后版本),**不拦运行**; + 目录没变不发。 +- 版本详情给出发布时的目录版本,以及哪些表在那之后变了。 +""" +from __future__ import annotations + +import asyncio +import uuid +from types import SimpleNamespace + +import pytest +from httpx import ASGITransport, AsyncClient +from sqlalchemy import select + +from app.data import catalog +from app.data.engine import engines +from app.data.introspect import introspect +from app.db.base import SessionLocal +from app.db.models import DataSource, Run, RunEvent, Workflow, WorkflowVersion +from app.engine.runner import run_manager +from app.main import app +from tests.fixtures.catalog import scenic_notes + +_CACHE: dict[str, dict] = {} +SQL = "SELECT COUNT(*) AS n FROM visits v JOIN parks p ON p.id = v.park_id WHERE v.status = 1" + + +async def _schema(path: str) -> dict: + if path not in _CACHE: + probe = SimpleNamespace(id=f"probe-{uuid.uuid4().hex[:6]}", name="scenic", kind="sqlite", database=path, + host=None, port=None, username=None, password=None, options={}, readonly=True, + description="", schema_cache={}, origin="manual") + try: + _CACHE[path] = await introspect(probe) + finally: + await engines.invalidate(probe.id) + return _CACHE[path] + + +@pytest.fixture(autouse=True) +async def engine_up(): + await run_manager.setup() + yield + await run_manager.shutdown() + + +@pytest.fixture +async def client(): + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c: + yield c + + +@pytest.fixture +async def source(scenic_db): + """带目录的景区数据源;用完连同模板一起删掉(删源会级联删掉目录)。""" + name = f"scenic_{uuid.uuid4().hex[:8]}" + async with SessionLocal() as session: + row = DataSource(name=name, kind="sqlite", database=scenic_db, readonly=True) + row.schema_cache = await _schema(scenic_db) + session.add(row) + await session.commit() + for table, notes in scenic_notes.notes().items(): + await catalog.write_entry(session, row.id, table, notes, if_version=0, actor="王敏") + sid = row.id + made: list[str] = [] + yield SimpleNamespace(id=sid, name=name, made=made) + async with SessionLocal() as session: + for wid in made: + if (wf := await session.get(Workflow, wid)) is not None: + await session.delete(wf) + if (row := await session.get(DataSource, sid)) is not None: + await session.delete(row) + await session.commit() + + +def node(nid, ntype, **config): + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +def daily(source: str) -> dict: + nodes = [ + node("start", "input"), + node("fetch", "tool", tool=f"db_query__{source}", args={"sql": SQL}), + node("out", "output", fields=[{"name": "结果", "value": "{{ nodes.fetch }}"}]), + ] + return {"nodes": nodes, "edges": [{"source": a["id"], "target": b["id"]} for a, b in zip(nodes, nodes[1:])]} + + +async def _publish(client, source) -> str: + wf = (await client.post("/api/workflows", json={"name": f"入园日报-{uuid.uuid4().hex[:6]}", + "graph": daily(source.name)})).json()["id"] + source.made.append(wf) + body = (await client.post(f"/api/workflows/{wf}/publish", json={"level": "published"})).json() + assert body["ok"] is True, body + return wf + + +async def _events(run_id: str, kind: str) -> list[dict]: + async with SessionLocal() as session: + rows = (await session.execute(select(RunEvent).where( + RunEvent.run_id == run_id, RunEvent.type == kind).order_by(RunEvent.seq))).scalars() + return [e.data for e in rows] + + +async def _settle(run_id: str) -> Run: + for _ in range(400): + 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: + return row + raise AssertionError(f"run {run_id} 没跑完:{row.status}") + + +async def _edit_visits(source_id: str) -> int: + async with SessionLocal() as session: + entry = await catalog.read_entry(session, source_id, "visits") + saved = await catalog.save_patch(session, source_id, "visits", + [{"path": "columns.visitor_count.measure", "value": "stock"}], + if_version=entry.version, actor="李雷") + return saved.version + + +async def test_publish_records_catalog_versions(client, source): + wf = await _publish(client, source) + async with SessionLocal() as session: + snap = (await session.execute(select(WorkflowVersion).where( + WorkflowVersion.workflow_id == wf, WorkflowVersion.version == 1))).scalar_one() + # 入园记录有目录(第 1 版);景区表还没有目录,记 0 + assert snap.catalog_versions == {source.name: {"parks": 0, "visits": 1}} + detail = (await client.get(f"/api/workflows/{wf}/versions/1")).json() + assert detail["catalog_versions"] == {source.name: {"parks": 0, "visits": 1}} + assert detail["catalog_changes"] == [] + + +async def test_formal_run_warns_on_catalog_change_but_runs(client, source): + wf = await _publish(client, source) + now = await _edit_visits(source.id) + r = await client.post("/api/runs", json={"workflow_id": wf, "run_class": "formal"}) + assert r.status_code == 201, r.text # 不拦 + run_id = r.json()["id"] + row = await _settle(run_id) + assert row.status == "succeeded", row.error + [drift] = await _events(run_id, "catalog.drift") + assert drift["count"] == 1 and drift["version"] == 1 + [table] = drift["tables"] + assert table == {"source": source.name, "source_id": source.id, "table": "visits", "label": "入园记录", + "published": 1, "current": now} + # 版本详情也写明哪些表在发布之后变了 + detail = (await client.get(f"/api/workflows/{wf}/versions/1")).json() + assert [(c["table"], c["published"], c["current"]) for c in detail["catalog_changes"]] == [("visits", 1, now)] + + +async def test_catalog_added_after_publish_counts_as_change(client, source): + wf = await _publish(client, source) + async with SessionLocal() as session: + await catalog.write_entry(session, source.id, "parks", {"label": catalog.make_item("景区", "human")}, + if_version=0, actor="王敏") + r = await client.post("/api/runs", json={"workflow_id": wf, "run_class": "formal"}) + run_id = r.json()["id"] + await _settle(run_id) + [drift] = await _events(run_id, "catalog.drift") + assert [(t["table"], t["published"], t["current"], t["label"]) for t in drift["tables"]] == [("parks", 0, 1, "景区")] + + +async def test_no_change_no_warning(client, source): + wf = await _publish(client, source) + r = await client.post("/api/runs", json={"workflow_id": wf, "run_class": "formal"}) + run_id = r.json()["id"] + row = await _settle(run_id) + assert row.status == "succeeded", row.error + assert await _events(run_id, "catalog.drift") == [] + # 探索运行不比对(不是从发布版本发起的) + r = await client.post("/api/runs", json={"workflow_id": wf, "run_class": "exploratory"}) + await _edit_visits(source.id) + run_id = r.json()["id"] + await _settle(run_id) + assert await _events(run_id, "catalog.drift") == [] + + +async def test_versions_published_before_the_record_existed_get_no_warning(client, source): + """这个功能之前发布的版本没有记录:不知道当时是哪一版,不提醒。""" + wf = await _publish(client, source) + async with SessionLocal() as session: + snap = (await session.execute(select(WorkflowVersion).where( + WorkflowVersion.workflow_id == wf, WorkflowVersion.version == 1))).scalar_one() + snap.catalog_versions = None + await session.commit() + await _edit_visits(source.id) + r = await client.post("/api/runs", json={"workflow_id": wf, "run_class": "formal"}) + run_id = r.json()["id"] + await _settle(run_id) + assert await _events(run_id, "catalog.drift") == [] + detail = (await client.get(f"/api/workflows/{wf}/versions/1")).json() + assert detail["catalog_versions"] is None and detail["catalog_changes"] == [] diff --git a/frontend/src/canvas/VersionsSheet.tsx b/frontend/src/canvas/VersionsSheet.tsx index 76989237..5ea271f5 100644 --- a/frontend/src/canvas/VersionsSheet.tsx +++ b/frontend/src/canvas/VersionsSheet.tsx @@ -7,10 +7,10 @@ import { } from '../store/studio' import { ErrorState, Skeleton, toast } from '../components/ui' import { formatDateTime, formatTime } from '../lib/format' -import { WORKFLOW_STATUS_LABEL } from '../lib/terms' +import { CATALOG_DRIFT_TEXT, WORKFLOW_STATUS_LABEL } from '../lib/terms' import { hintOf } from './shortcuts' import type { Edge } from '@xyflow/react' -import type { GraphSpec, Workflow, WorkflowVersion } from '../types' +import type { CatalogDriftTable, GraphSpec, Workflow, WorkflowVersion } from '../types' /** * 版本历史。盖在右栏上的一层,和属性面板同一种「临时造访」。 @@ -27,7 +27,7 @@ export function VersionsSheet({ workflow, onClose }: { workflow: Workflow; onClo const [list, setList] = useState(null) const [error, setError] = useState(null) const [picked, setPicked] = useState(null) - const [detail, setDetail] = useState<{ v: number; graph: GraphSpec } | { v: number; error: unknown } | null>(null) + const [detail, setDetail] = useState<{ v: number; graph: GraphSpec; version: WorkflowVersion } | { v: number; error: unknown } | null>(null) const nodes = useStudio((s) => s.nodes) const edges = useStudio((s) => s.edges) const dirty = useStudio((s) => s.dirty) @@ -61,7 +61,7 @@ export function VersionsSheet({ workflow, onClose }: { workflow: Workflow; onClo let alive = true setDetail(null) api.workflows.version(workflow.id, picked) - .then((v) => { if (alive) setDetail({ v: picked, graph: v.graph ?? { nodes: [], edges: [] } }) }) + .then((v) => { if (alive) setDetail({ v: picked, graph: v.graph ?? { nodes: [], edges: [] }, version: v }) }) .catch((e) => { if (alive) setDetail({ v: picked, error: e }) }) return () => { alive = false } }, [workflow.id, picked]) @@ -137,8 +137,14 @@ export function VersionsSheet({ workflow, onClose }: { workflow: Workflow; onClo {!detail ? : 'error' in detail ? : target && diff && ( - + <> + + {detail.version.catalog_versions && ( + + )} + )} )} @@ -205,6 +211,50 @@ function VersionPreview({ version, target, diff, lock, onRestore }: { const DIFF_COLOR = { add: 'var(--ok)', del: 'var(--err)', mod: 'var(--warn)' } as const +/** + * 发布时的目录版本:这一版 SQL 用到的表在发布时是数据目录的第几版,之后改过的标出来——下次从这一版发起正式 + * 运行时会提醒「数据目录有变化」,这里先让人看到是哪几张表 + */ +function CatalogVersions({ versions, changes }: { + versions: Record> + changes: CatalogDriftTable[] +}) { + const rows = Object.entries(versions).flatMap(([source, tables]) => + Object.entries(tables).map(([table, v]) => ({ source, table, v }))) + const changed = new Map(changes.map((c) => [`${c.source}/${c.table}`, c])) + return ( +
+
{CATALOG_DRIFT_TEXT.section}
+ {!rows.length ?
{CATALOG_DRIFT_TEXT.empty}
: ( + <> +
    + {rows.map(({ source, table, v }) => { + const c = changed.get(`${source}/${table}`) + return ( +
  • + {table} + {CATALOG_DRIFT_TEXT.version(v)} + {c && ( + + {CATALOG_DRIFT_TEXT.changedSince(CATALOG_DRIFT_TEXT.version(c.current))} + + )} +
  • + ) + })} +
+
+ + {changes.length ? CATALOG_DRIFT_TEXT.changedCount(changes.length) : CATALOG_DRIFT_TEXT.unchanged} + +
+ + )} +
+ ) +} + /** * 缩略图:这一版的节点按真实坐标缩进一个小框,类型色填充;恢复后会被拿掉的 * (只在画布上有)画成红色虚框叠在同一个坐标系里,改过配置的描琥珀边 diff --git a/frontend/src/lib/terms.ts b/frontend/src/lib/terms.ts index 56bc8e08..5f1a9e89 100644 --- a/frontend/src/lib/terms.ts +++ b/frontend/src/lib/terms.ts @@ -2218,3 +2218,26 @@ export const CATALOG_IMPACT_TEXT = { error: '无法统计引用这张表的模板', retry: '重试', } + +/** + * 发布时的目录版本:发布时记下这一版 SQL 用到的表的数据目录版本;从这一版发起正式运行时目录有变化,提醒而不拦。 + * 版本号写「第 N 版」,还没有目录的表写「尚无目录」。 + */ +export const CATALOG_DRIFT_TEXT = { + /** 运行里的提醒:「自发布以来,数据目录中「入园记录」等 2 张表有变化」 */ + title: (first: string, n: number) => (n > 1 + ? `自发布以来,数据目录中「${first}」等 ${formatNumber(n)} 张表有变化` + : `自发布以来,数据目录中「${first}」有变化`), + sub: '运行未被拦截。请核对这些表的目录修改是否影响本次结果', + table: (label: string | null, table: string) => (label ? `${label}(${table})` : table), + version: (v: number | null) => (v ? `第 ${formatNumber(v)} 版` : '尚无目录'), + line: (source: string, table: string, from: string, to: string) => `「${source}」${table}:发布时${from},现为${to}`, + open: '查看目录', + // ---- 版本历史里的「发布时的目录版本」 + section: '发布时的目录版本', + sectionHint: '这一版 SQL 用到的表在发布时的数据目录版本', + changedSince: (now: string) => `之后有变化,现为${now}`, + changedCount: (n: number) => `${formatNumber(n)} 张表在发布之后有变化,下次正式运行时会提醒`, + unchanged: '发布之后这些表的目录没有变化', + empty: '这一版的 SQL 没有用到数据库表', +} diff --git a/frontend/src/pages/runs/Banners.tsx b/frontend/src/pages/runs/Banners.tsx index b0296d0e..28643dad 100644 --- a/frontend/src/pages/runs/Banners.tsx +++ b/frontend/src/pages/runs/Banners.tsx @@ -1,9 +1,10 @@ import type { ReactNode } from 'react' import { Link } from 'react-router-dom' -import { ArrowDown, Ban, ChartGantt, Copy, Crosshair, Play, RotateCcw, Settings2, Wrench, X } from 'lucide-react' +import { ArrowDown, Ban, BookMarked, ChartGantt, Copy, Crosshair, Play, RotateCcw, Settings2, Wrench, X } from 'lucide-react' import { CopyButton, Spinner, StatusBadge } from '../../components/ui' import { formatDateTime, formatSpan, formatTime } from '../../lib/format' -import type { Approval } from '../../types' +import type { Approval, CatalogDriftTable } from '../../types' +import { CATALOG_DRIFT_TEXT, CATALOG_IMPACT_TEXT } from '../../lib/terms' import type { RunErrorExplain } from '../../lib/explain' import { LONG_WAIT_MS, ageMs } from './model' import { copyText } from './parts' @@ -22,6 +23,49 @@ function Shell({ color, children, data }: { color: string; children: ReactNode; ) } +// ------------------------------------------------------------------------- +// 发布之后数据目录有变化(catalog.drift) +// ------------------------------------------------------------------------- + +/** + * 从发布版本发起的正式运行,开始时发现这一版 SQL 用到的表的数据目录在发布之后改过。运行照常(目录是说明, + * 不是运行的输入),但看结果的人要知道:「在园人数」在发布之后被改成了存量,这一版的 SQL 可能还在跨天相加。 + * 每张表给到数据目录的入口;表多时只列前几张 + */ +export function CatalogDriftBanner({ drift }: { drift: { title: string; tables: CatalogDriftTable[] } }) { + const shown = drift.tables.slice(0, DRIFT_SHOWN) + return ( + +
+ +
+

{drift.title}

+

{CATALOG_DRIFT_TEXT.sub}

+
    + {shown.map((t) => ( +
  • + + {CATALOG_DRIFT_TEXT.line(t.source, CATALOG_DRIFT_TEXT.table(t.label, t.table), + CATALOG_DRIFT_TEXT.version(t.published), CATALOG_DRIFT_TEXT.version(t.current))} + + {t.source_id && ( + + {CATALOG_DRIFT_TEXT.open} + + )} +
  • + ))} + {drift.tables.length > shown.length &&
  • {CATALOG_IMPACT_TEXT.more(drift.tables.length - shown.length)}
  • } +
+
+
+
+ ) +} + +const DRIFT_SHOWN = 6 + // ------------------------------------------------------------------------- // 失败 // ------------------------------------------------------------------------- diff --git a/frontend/src/pages/runs/RunDetailView.tsx b/frontend/src/pages/runs/RunDetailView.tsx index e43d2794..98a64ef1 100644 --- a/frontend/src/pages/runs/RunDetailView.tsx +++ b/frontend/src/pages/runs/RunDetailView.tsx @@ -9,13 +9,13 @@ import { import { formatDateTime, formatNumber, formatTime, shortId } from '../../lib/format' import { resolveStatus, statusLabel, type StatusCode } from '../../lib/status' import { AssistantStream, type StreamTurn, type TurnFailure } from '../../run/AssistantStream' -import { decodePhase, decodeRun, exitLabels, summarizeRun, type RunFinal } from '../../run/decode' +import { catalogDrift, decodePhase, decodeRun, exitLabels, summarizeRun, type RunFinal } from '../../run/decode' import { sourceHandles } from '../../canvas/nodeDefs' import { ApprovalCard } from '../../run/RunPanel' import { runStatusOf, type RunPhase } from '../../run/trace' import { useCatalog } from '../../store/catalog' import type { Approval, Run, RunEvent } from '../../types' -import { FailedBanner, FeedbackStrip, HeldBanner, WaitingBanner, type Feedback } from './Banners' +import { CatalogDriftBanner, FailedBanner, FeedbackStrip, HeldBanner, WaitingBanner, type Feedback } from './Banners' import { localActor } from '../../lib/actor' import { explainRunError, explainStartError } from '../../lib/explain' import { canLeave, leavePass } from '../../lib/leave' @@ -520,6 +520,10 @@ export function RunDetailView({ runId, onChange, onDeleted }: { ) : null + // 发布之后数据目录有变化:正式运行开始时记下的提醒,横幅一直挂着(不拦运行,但看结果的人得知道)。 + // 上面有提前返回,这里不能再用 hook;只解一条事件,直接算 + const catalogEvent = lastOf(d.events, 'catalog.drift') + const catalogChange = catalogEvent ? catalogDrift(catalogEvent.data ?? {}) : null const unsaved = isUnsaved(run) const artifactCount = artifacts.items?.length const viewTabs: TabItem[] = [ @@ -612,6 +616,7 @@ export function RunDetailView({ runId, onChange, onDeleted }: { void abandon()} busy={busy === 'abandon'} now={now} /> )} + {catalogChange && } {feedback && setFeedback(null)} />}
string | undefine return (id) => map.get(id) ?? fallback?.(id) } +/** + * catalog.drift 事件 → 提醒的标题和逐表明细。标题按第一张表的中文名说(「入园记录」等 2 张表),明细每张表一行: + * 发布时第几版、现在第几版(0 写「尚无目录」)。认不出的形状返回 null + */ +export function catalogDrift(d: Record): { title: string; detail: string; tables: CatalogDriftTable[] } | null { + const tables: CatalogDriftTable[] = (Array.isArray(d?.tables) ? d.tables : []) + .filter((t: any) => t && typeof t === 'object' && typeof t.table === 'string' && typeof t.source === 'string') + .map((t: any) => ({ + source: t.source, source_id: String(t.source_id ?? ''), table: t.table, + label: typeof t.label === 'string' && t.label ? t.label : null, + published: num(t.published) ?? null, current: num(t.current) ?? 0, + })) + if (!tables.length) return null + const first = tables[0].label ?? tables[0].table + return { + title: CATALOG_DRIFT_TEXT.title(first, tables.length), + detail: tables.map((t) => CATALOG_DRIFT_TEXT.line(t.source, CATALOG_DRIFT_TEXT.table(t.label, t.table), + CATALOG_DRIFT_TEXT.version(t.published), CATALOG_DRIFT_TEXT.version(t.current))).join('\n'), + tables, + } +} + /** context 操作里一个数据源的那一项(服务端 copilot_context.DatasourceContext.op) */ export interface CopilotContextSource { source: string diff --git a/frontend/src/types.ts b/frontend/src/types.ts index f1eb560f..5ea9f0ec 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -56,6 +56,22 @@ export interface WorkflowVersion { input_fields?: { name: string; required?: boolean; [key: string]: any }[] /** 是不是当前的已发布版本 */ published?: boolean + /** 发布时这一版 SQL 用到的表的数据目录版本 {源名: {表名: 版本}},0 表示当时还没有目录;没记过为 null */ + catalog_versions?: Record> | null + /** 发布之后目录有变化的表 */ + catalog_changes?: CatalogDriftTable[] +} + +/** 发布之后数据目录有变化的一张表(服务端 catalog_impact.catalog_drift;运行里的 catalog.drift 事件同形) */ +export interface CatalogDriftTable { + source: string + source_id: string + table: string + /** 表现在的中文名 */ + label: string | null + /** 发布时的版本,0 表示当时还没有目录 */ + published: number | null + current: number } /** diff --git a/scripts/check-decode.mjs b/scripts/check-decode.mjs index 52ae8b22..1691644f 100644 --- a/scripts/check-decode.mjs +++ b/scripts/check-decode.mjs @@ -720,6 +720,26 @@ await section('Copilot:建议更新数据目录(catalog_patch 操作)', as ]).length === 0 && mod.decodeCopilot([{ op: 'catalog_patch', source: 's', table: 't' }]).length === 0) }) +await section('正式运行:发布之后数据目录有变化(catalog.drift 事件)', async () => { + const steps = mod.decodeRun([ + { seq: 1, type: 'run.started', node_id: null, ts: 1, data: { nodes: 1 } }, + { seq: 2, type: 'catalog.drift', node_id: null, ts: 1.01, data: { version: 3, count: 2, tables: [ + { source: 'scenic', source_id: 'src-scenic', table: 'visits', label: '入园记录', published: 3, current: 5 }, + { source: 'scenic', source_id: 'src-scenic', table: 'parks', label: null, published: 0, current: 1 }, + ] } }, + ]) + const row = steps.find((s) => s.code === 'catalog_drift') + check('一行提醒:「入园记录」等 2 张表有变化', row?.title === '自发布以来,数据目录中「入园记录」等 2 张表有变化', row?.title) + check('是警告、不是失败', row?.level === 'warn' && row?.status === 'done' && row?.kind === 'note') + check('副标题写明运行未被拦截', !!row?.sub?.includes('运行未被拦截'), row?.sub) + check('展开逐表写版本,没有目录写「尚无目录」', !!row?.detail?.includes('「scenic」入园记录(visits):发布时第 3 版,现为第 5 版') + && row.detail.includes('「scenic」parks:发布时尚无目录,现为第 1 版'), row?.detail) + const one = mod.catalogDrift({ tables: [{ source: 's', table: 't', label: null, published: 1, current: 2 }] }) + check('只有一张表:标题不写「等」', one?.title === '自发布以来,数据目录中「t」有变化', one?.title) + check('形状不对不出行', mod.decodeRun([{ seq: 1, type: 'catalog.drift', node_id: null, ts: 1, data: { tables: 'x' } }]) + .every((s) => s.code !== 'catalog_drift')) +}) + await section('Copilot:心跳穿插、回话、少了一步、报错', async () => { const steps = mod.decodeCopilot(synthetic.COPILOT_STUCK, { context: 'canvas' }) check('心跳穿插的思考仍然并成一条', steps.filter((s) => s.kind === 'think').length === 1, diff --git a/scripts/check-runs.mjs b/scripts/check-runs.mjs index 49c44775..c86ba48c 100644 --- a/scripts/check-runs.mjs +++ b/scripts/check-runs.mjs @@ -690,6 +690,50 @@ await section('提取模板', async () => { fakes.delete('GET /api/conversations') }) +// ------------------------------------------------------------------ 发布之后数据目录有变化(catalog.drift) + +await section('正式运行:发布之后数据目录有变化,提醒但不拦', async () => { + const id = 'fake0catdrift000000000000000000' + const t = Date.now() / 1000 - 30 + const run = { + ...base, id, status: 'succeeded', run_class: 'formal', version: 3, workflow_id: null, + created_at: new Date(t * 1000).toISOString(), started_at: new Date(t * 1000).toISOString(), + finished_at: new Date((t + 2) * 1000).toISOString(), output: { 结果: '共 128 人次' }, + } + const graph = { nodes: [ + { id: 'a', type: 'input', position: { x: 0, y: 0 }, data: { label: '输入', config: {} } }, + { id: 'q', type: 'tool', position: { x: 200, y: 0 }, data: { label: '查询入园人数', config: {} } }, + ], edges: [{ source: 'a', target: 'q' }] } + const events = [ + { seq: 1, type: 'run.started', node_id: null, ts: t, data: { nodes: 2 } }, + { seq: 2, type: 'catalog.drift', node_id: null, ts: t + 0.01, data: { version: 3, count: 2, tables: [ + { source: 'scenic', source_id: 'src-scenic', table: 'visits', label: '入园记录', published: 3, current: 5 }, + { source: 'scenic', source_id: 'src-scenic', table: 'parks', label: null, published: 0, current: 1 }, + ] } }, + { seq: 3, type: 'node.started', node_id: 'a', ts: t + 0.1, data: {} }, + { seq: 4, type: 'node.finished', node_id: 'a', ts: t + 0.2, data: { duration_ms: 100 } }, + { seq: 5, type: 'node.started', node_id: 'q', ts: t + 0.3, data: {} }, + { seq: 6, type: 'node.finished', node_id: 'q', ts: t + 0.4, data: { duration_ms: 100 } }, + { seq: 7, type: 'run.finished', node_id: null, ts: t + 2, data: { output: run.output } }, + ] + fakes.set(`GET /api/runs/${id}`, () => ({ status: 200, json: run })) + fakes.set(`GET /api/runs/${id}/events`, () => ({ status: 200, json: events })) + fakes.set(`GET /api/runs/${id}/graph`, () => ({ status: 200, json: { graph, workflow_id: null, version: 3 } })) + await page.goto(`${WEB}/runs/${id}`, { waitUntil: 'networkidle' }) + const banner = page.locator('[data-run-banner="catalog-drift"]') + check('详情显示目录变化的横幅', await banner.waitFor({ timeout: 5000 }).then(() => true, () => false)) + const text = await banner.innerText().catch(() => '') + check('横幅标题:「入园记录」等 2 张表有变化', text.includes('自发布以来,数据目录中「入园记录」等 2 张表有变化'), text.split('\n')[0]) + check('写明运行未被拦截', text.includes('运行未被拦截')) + check('逐表写发布时和现在的版本,没有目录写「尚无目录」', text.includes('「scenic」入园记录(visits):发布时第 3 版,现为第 5 版') + && text.includes('「scenic」parks:发布时尚无目录,现为第 1 版'), text.replace(/\s+/g, ' ')) + check('每张表给到数据目录的入口', (await banner.locator('[data-drift-open="visits"]').getAttribute('href')) === '/data/catalog/src-scenic/visits') + check('运行照常完成:没有失败横幅', await page.locator('[data-run-banner=failed]').count() === 0) + const row = page.locator('[data-step-code="catalog_drift"]') + check('时间线里也有一行提醒', await row.count() === 1 && (await row.innerText()).includes('等 2 张表有变化')) + for (const k of [`GET /api/runs/${id}`, `GET /api/runs/${id}/events`, `GET /api/runs/${id}/graph`]) fakes.delete(k) +}) + // ------------------------------------------------------------------ 运行中:接流、停止、不能删 const FAKE = 'fake0running0000000000000000000' diff --git a/scripts/check-studio.mjs b/scripts/check-studio.mjs index 025427ee..02bd4b84 100644 --- a/scripts/check-studio.mjs +++ b/scripts/check-studio.mjs @@ -263,8 +263,11 @@ async function open({ width = 1440, height = 900, path = '/studio/st-main', pref if (sub === 'versions' && !v) return json(route, VERSIONS) if (sub === 'versions' && v) { const n = Number(v) + // 已发布的 v2 记着发布时的目录版本:入园记录之后改过(第 3 版 → 第 5 版),景区表没变 return json(route, { ...VERSIONS.find((x) => x.version === n), workflow_id: id, - graph: n === 1 ? V1 : GRAPH, graph_hash: 'x', input_fields: [{ name: 'goal' }], published: n === 2 }) + graph: n === 1 ? V1 : GRAPH, graph_hash: 'x', input_fields: [{ name: 'goal' }], published: n === 2, + catalog_versions: n === 2 ? { shop: { parks: 1, visits: 3 } } : null, + catalog_changes: n === 2 ? [{ source: 'shop', source_id: 'src-shop', table: 'visits', label: '入园记录', published: 3, current: 5 }] : [] }) } if (method === 'GET' && !sub) return state.deleted.has(id) ? json(route, { detail: '工作流不存在,可能已被删除' }, 404) : json(route, FAKES[id]) if (method === 'DELETE' && !sub) { @@ -1001,7 +1004,18 @@ await section('版本历史:预览、恢复成一次可撤销的改动', async const rows = await count(page, 'ol[aria-label="版本"] > li') check('列出全部版本', rows === 3, `${rows} 版`) check('标出已发布的那一版', (await page.locator('ol[aria-label="版本"] > li').nth(1).innerText()).includes('已发布')) + // 已发布的那一版:看得到发布时的目录版本,之后改过的表标出来 + await page.locator('ol[aria-label="版本"] > li').nth(1).locator('button').first().click() + const cat = page.locator('[data-version-catalog]') + await cat.waitFor({ timeout: 3000 }).catch(() => {}) + const catText = await cat.innerText().catch(() => '') + check('已发布的版本写「发布时的目录版本」', catText.includes('发布时的目录版本') && catText.includes('parks') && catText.includes('第 1 版'), catText) + check('之后改过的表标出来', (await page.locator('[data-version-catalog-table="visits"]').getAttribute('data-changed')) === 'true' + && catText.includes('之后有变化,现为第 5 版') && catText.includes('1 张表在发布之后有变化'), catText.replace(/\s+/g, ' ')) + check('没改过的表不标', (await page.locator('[data-version-catalog-table="parks"]').getAttribute('data-changed')) === null) await page.locator('ol[aria-label="版本"] > li').nth(2).locator('button').first().click() + await page.waitForTimeout(300) + check('没发布过的版本没有目录版本这一栏', await count(page, '[data-version-catalog]') === 0) await page.waitForTimeout(500) check('选一版给出缩略图预览', await count(page, 'svg[aria-label^="缩略图"]') === 1) check('预览列出恢复后将移除的节点', (await page.locator('[role="dialog"][aria-label="版本历史"]').innerText()).includes('将移除'))