From c12f0f2852ad1a371d7bda58e8626cc5898f5020 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Sun, 4 Oct 2026 01:07:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9B=AE=E5=BD=95=20F3=20?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E6=80=A7=EF=BC=9A=E5=8A=A9=E6=89=8B=E4=B8=8A?= =?UTF-8?q?=E4=B8=8B=E6=96=87=E9=87=8C=E3=80=8C=E7=8E=B0=E6=9C=89=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E6=B5=81=E7=94=A8=E5=88=B0=E7=9A=84=E8=A1=A8=E3=80=8D?= =?UTF-8?q?=E6=94=B9=E7=94=A8=20tool=5Fqueries=20=E8=AE=A4=E8=8A=82?= =?UTF-8?q?=E7=82=B9=EF=BC=88=E4=BB=A5=20sqlcheck.tool=5Fquery=20=E4=B8=BA?= =?UTF-8?q?=E5=87=86=EF=BC=8C=E6=8D=A2=E4=BA=86=E7=B1=BB=E5=9E=8B=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=AE=8B=E7=95=99=E7=9A=84=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E7=AE=97=EF=BC=89=EF=BC=9B=E6=AD=A3=E5=BC=8F?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E7=9A=84=E7=9B=AE=E5=BD=95=E5=8F=98=E5=8C=96?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=94=B9=E7=94=A8=20EventType.CATALOG=5FDRIF?= =?UTF-8?q?T=EF=BC=9B=E5=86=99=E6=98=8E=E5=85=B3=E7=B3=BB=E7=BC=96?= =?UTF-8?q?=E5=8F=B7=E5=8C=BA=E5=88=86=E6=96=B9=E5=90=91=E3=80=81=E6=9C=89?= =?UTF-8?q?=E6=84=8F=E4=B8=8D=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/copilot_context.py | 37 ++++++++++++++-------------- backend/app/api/runs.py | 3 ++- backend/tests/test_catalog_impact.py | 22 +++++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/backend/app/api/copilot_context.py b/backend/app/api/copilot_context.py index d2893c1f..86dc1205 100644 --- a/backend/app/api/copilot_context.py +++ b/backend/app/api/copilot_context.py @@ -212,35 +212,28 @@ def _modes(sources: list[Any]) -> list[Literal["all", "pick"]]: def graph_tables(graph: Mapping[str, Any] | None, sources: Iterable[Any]) -> dict[str, list[str]]: """现有工作流里查库节点的 SQL 用到的表:{源名: [schema_cache 的键]},按出现顺序。 - 只认调用工具节点上写死的 SQL(db_query__<源>);Agent 运行时自己写的 SQL 图里看不到。节点认两种写法: - 画布的 {data: {config}} 和 GraphSpec 的 {config}。SQL 解析不了的跳过。 + 只认调用工具节点上写死的 SQL(db_query__<源>);Agent 运行时自己写的 SQL 图里看不到。哪些节点算写死的查询 + 以 sqlcheck.tool_query 为准(catalog_impact.tool_queries,和发布记录、SQL 检查同一份):换成别的类型的节点 + 配置里残留的 tool、args 不算。节点认两种写法:画布的 {data: {config}} 和 GraphSpec 的 {config}。SQL 解析 + 不了的跳过。 """ + from app.data.catalog_impact import tool_queries from app.engine.evidence import sql_tables - from app.tools.datasource import QUERY_PREFIX by_name = {getattr(s, "name", None): s for s in sources} out: dict[str, list[str]] = {} - for node in (graph or {}).get("nodes") or []: - if not isinstance(node, dict): - continue - config = node.get("config") - if not isinstance(config, dict): - config = (node.get("data") or {}).get("config") if isinstance(node.get("data"), dict) else None - if not isinstance(config, dict): - continue - tool = str(config.get("tool") or "") - source = by_name.get(tool[len(QUERY_PREFIX):]) if tool.startswith(QUERY_PREFIX) else None - args = config.get("args") - sql = args.get("sql") if isinstance(args, dict) else None - if source is None or not isinstance(sql, str): + nodes = (graph or {}).get("nodes") or [] + for _, source_name, sql in tool_queries(n for n in nodes if isinstance(n, dict)): + source = by_name.get(source_name) + if source is None: continue try: names = sql_tables(sql) except Exception: # noqa: BLE001 - 认不出表就当没用到,只少一个提示 continue found = out.setdefault(source.name, []) - for name in names: - key = catalog.resolve_table_name(source.schema_cache, name) + for table in names: + key = catalog.resolve_table_name(source.schema_cache, table) if key and key not in found: found.append(key) return {k: v for k, v in out.items() if v} @@ -604,7 +597,13 @@ def _edge_line(edge: JoinEdge, qualified) -> str: def _edges_within(graph: Mapping[str, list[JoinEdge]], tables: list[str]) -> list[JoinEdge]: - """两端都在 tables 里的关系,每条一次;一对多的翻过来,从「多」的一端写起(外键所在的表在左)。""" + """两端都在 tables 里的关系,每条一次;一对多的翻过来,从「多」的一端写起(外键所在的表在左)。 + + 「每条一次」按 relation_id 去重,去掉的是同一条关系在关系图里的正反两条边。注意 catalog.relation_id 区分方向: + A 表目录里记的 A→B 和 B 表目录里记的 B→A 是两个编号,两边都记了的话这里会各出一行(内容相同、写法相反)。 + 这是有意不改的:编号存在每条关系上,也是审阅路径 relations.<编号> 的一部分,改成不分方向会让已有目录里的 + 编号全部对不上。 + """ wanted = set(tables) seen: set[str] = set() out = [] diff --git a/backend/app/api/runs.py b/backend/app/api/runs.py index fb424fe1..d61bd8a0 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.core.events import EventType 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 @@ -188,7 +189,7 @@ async def start_run( if catalog_changes: # 落在封存范围内:之后回看这次正式出具,能知道当时的目录和发布时不一样、差在哪几张表。 # 每张表带 impact:direct 写死的 SQL 用到的,possible Agent 可能查询的(界面分开说) - await run_manager.note(run.id, "catalog.drift", version=version, count=len(catalog_changes), + await run_manager.note(run.id, EventType.CATALOG_DRIFT, version=version, count=len(catalog_changes), tables=catalog_changes) # 已声明策略的升版记入事件流,出具物上能看到"这期换口径了、怎么处置的" for ev in upgrade_events: diff --git a/backend/tests/test_catalog_impact.py b/backend/tests/test_catalog_impact.py index a25d23f3..bf932094 100644 --- a/backend/tests/test_catalog_impact.py +++ b/backend/tests/test_catalog_impact.py @@ -183,3 +183,25 @@ def test_graph_sql_tables_uses_the_evidence_rules(): )) tables = {"shop": {"orders": {}, "order_items": {}, "customers": {}}, "other": {"orders": {}}} assert graph_sql_tables(spec, tables) == {"shop": {"orders", "order_items", "customers"}, "other": {"orders"}} + + +def test_assistant_seeds_read_tool_nodes_the_same_way(): + """助手给全字段的「现有工作流用到的表」和发布记录、SQL 检查认的是同一种节点(sqlcheck.tool_query)。 + + 画布上把调用工具节点换成别的类型,配置里的 tool、args 还留着:那已经不是一条会执行的查询,不能当成用到的表。 + """ + from app.api.copilot_context import graph_tables + + shop = SimpleNamespace(name="shop", schema_cache={"tables": { + "orders": {"qualified": "orders"}, "customers": {"qualified": "customers"}, "stores": {"qualified": "stores"}}}) + graph = _graph( + _query("q1", "shop", "SELECT * FROM customers c JOIN orders o ON o.customer_id = c.id"), + # 换成了 Agent 的节点:残留的写死查询不算 + _node("ask", "agent", tool="db_query__shop", args={"sql": "SELECT * FROM stores"}, tools=["db_query__shop"]), + # GraphSpec 的写法(config 在节点上)照样认 + {"id": "q2", "type": "tool", "config": {"tool": "db_query__shop", "args": {"sql": "SELECT * FROM stores"}}}, + _query("q3", "shop", " "), + ) + assert graph_tables(graph, [shop]) == {"shop": ["customers", "orders", "stores"]} + graph["nodes"] = [n for n in graph["nodes"] if n["id"] != "q2"] + assert graph_tables(graph, [shop]) == {"shop": ["customers", "orders"]}