From f87b07694d6a170413b2c8722e4275cafb3ad9ff Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Tue, 20 Oct 2026 10:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A9=E6=89=8B=E6=90=AD=E5=9B=BE=E6=97=B6?= =?UTF-8?q?=E5=B0=B1=E5=8F=91=E7=8E=B0=E3=80=8C=E6=9F=A5=E7=9A=84=E5=8C=BA?= =?UTF-8?q?=E9=97=B4=E9=87=8C=E8=A1=A8=E9=87=8C=E6=B2=A1=E6=9C=89=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E3=80=8D=EF=BC=9A=E8=87=AA=E6=9F=A5=EF=BC=88GraphChec?= =?UTF-8?q?ker=EF=BC=89=E5=AF=B9=E5=B7=A5=E5=85=B7=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E5=86=99=E6=AD=BB=E7=9A=84=20SQL=20=E6=8C=89=E4=BB=8A=E5=A4=A9?= =?UTF-8?q?=E8=B7=91=E4=B8=80=E9=81=8D=E8=A6=86=E7=9B=96=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=EF=BC=88data/coverage.sql=5Fgaps=EF=BC=9A=E4=B8=8D=E8=B7=91?= =?UTF-8?q?=E5=8E=9F=20SQL=EF=BC=8C=E5=8F=AA=E8=B7=91=E7=AE=97=E7=95=8C?= =?UTF-8?q?=E5=92=8C=E7=8E=B0=E6=9F=A5=E8=8C=83=E5=9B=B4=E7=9A=84=E4=B8=A4?= =?UTF-8?q?=E6=9D=A1=E5=B0=8F=E6=9F=A5=E8=AF=A2=EF=BC=8CSQL=20=E9=87=8C?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=B8=A6=E6=97=B6=E9=97=B4=E8=B0=93=E8=AF=8D?= =?UTF-8?q?=E7=9A=84=E8=A1=A8=E9=83=BD=E6=9F=A5=EF=BC=8C=E6=8C=89=E8=A1=A8?= =?UTF-8?q?=E5=8E=BB=E9=87=8D=E3=80=81=E9=99=90=E6=97=B6=EF=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8C=E5=8F=AA=E6=9C=89=E4=B8=80=E5=A4=A9=E3=80=81=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E6=97=A5=E6=9C=9F=E6=97=A9=E4=BA=8E=E8=A6=81=E6=9F=A5?= =?UTF-8?q?=E7=9A=84=E5=8C=BA=E9=97=B4=E7=9A=84=E8=A1=A8=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E7=94=A8=E6=9D=A5=E7=AE=97=E8=BF=99=E6=AE=B5=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/copilot.py | 10 +- backend/app/api/copilot_gen.py | 81 ++++++++++++++- backend/app/data/schema_view.py | 15 ++- backend/tests/test_copilot_coverage.py | 134 +++++++++++++++++++++++++ 4 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 backend/tests/test_copilot_coverage.py diff --git a/backend/app/api/copilot.py b/backend/app/api/copilot.py index 85c0a489..fb8b7ade 100644 --- a/backend/app/api/copilot.py +++ b/backend/app/api/copilot.py @@ -296,7 +296,9 @@ NODE_REFERENCE = """\ 6. 问题里的「今天、昨天、上周、本月、去年」这类相对日期一律按运行当天算:SQL 用数据源方言里取当天的写法(CURRENT_DATE、 CURDATE()、date('now')、SYSDATE),agent 先调 current_time 拿到当天日期再写条件(agent 的 prompt 里写明这一点、tools 绑上 current_time)。不要用表里最大的日期(MAX(日期))去推「上周」「本月」:数据没更新到那段时间时,推出来的是另一段,报告还会 - 把它当成上周来写。想让报告说清数据最新到哪天,另取一列 MAX(日期) AS 数据最新日期 + 把它当成上周来写。想让报告说清数据最新到哪天,另取一列 MAX(日期) AS 数据最新日期。 + 写按周期取数的 SQL 前先看表的日期范围(look 结果里的剖析、取值观察):只有一天的数据、或者最新日期早于要查的区间的表, + 不能用来算这段时间(查出来全是 0)——换有历史的表;没有这样的表,就在说明里照实告诉用户这段时间没有数据 模型字段(llm / agent / supervisor / report 的 config.model): - **不要填**。留空表示跟随当前供应商的默认模型,这几乎总是对的。 @@ -953,6 +955,7 @@ async def generate( applier.replay_onto(prev_graph) async for _op in gen.repair(): pass + covered = await gen.checker.coverage(level="warning") if isinstance(gen.checker, GraphChecker) else [] try: spec = GraphSpec.model_validate({"nodes": list(nodes.values()), "edges": edges}) @@ -971,7 +974,7 @@ async def generate( explanation=applier.explanation or str((gen.asked or {}).get("question") or ""), issues=[i.model_dump() for i in report.issues if i.level != "info"] + _issue_dicts(scope_issues(out["nodes"], scope), "datasource_out_of_scope") - + dropped_tool_warnings(changes, payload.instruction), + + dropped_tool_warnings(changes, payload.instruction) + covered, layout=layout, tool_changes=changes, ) @@ -1812,6 +1815,9 @@ async def answer_ops(payload: GenerateIn, *, catalog_view: Any = None): # 能跑,只是跑出来的是模型「假设」查过库的答案 changes = tool_changes(baseline, list(nodes.values())) warnings = dropped_tool_warnings(changes, payload.instruction) + # 工具节点写死的 SQL 查的区间里表里没有数据(data/coverage.py):自查时交回模型改过一轮,仍在的不挡运行,留在检查 + # 结果里当提醒(模型照要求在说明里告诉用户) + warnings += await gen.checker.coverage(level="warning") if isinstance(gen.checker, GraphChecker) else [] # 建完要不要跑,由模型在 done.run 里表态;问了用户的不跑:要等他回答 autorun = applier.autorun and gen.asked is None if remaining: diff --git a/backend/app/api/copilot_gen.py b/backend/app/api/copilot_gen.py index fbe0ce58..68c7a888 100644 --- a/backend/app/api/copilot_gen.py +++ b/backend/app/api/copilot_gen.py @@ -723,6 +723,11 @@ class GraphChecker: self.defaults = defaults self.checkers = checkers self.known = known + #: 工具节点写死的 SQL 查的区间里表里没有数据(data/coverage.py):同一个节点、同一条 SQL 只查一次库 + self._coverage: dict[tuple[str, str], list[dict[str, Any]]] = {} + self._coverage_cache: Any = None + #: 这类问题已经交回模型改过一轮:之后不再挡(修正后仍在的留在检查结果里当提醒,由模型在说明里照实告诉用户) + self._coverage_given = False def blocking(self) -> list[dict[str, Any]] | None: from app.api.copilot import _blocking_issues @@ -735,7 +740,10 @@ class GraphChecker: errors = self.blocking() refused = _refused_issues(self.applier) if self.known is None: - return [*refused, *(errors or [])] + # 查的区间里表里没有数据:交回模型改一轮(换有这段数据的表,或者在说明里照实告诉用户);改过一轮仍在的不挡 + covered = [] if self._coverage_given or errors is None else await self.coverage() + self._coverage_given = self._coverage_given or bool(covered) + return [*refused, *(errors or []), *covered] from app.engine.autofix import _sig if not self.applier.effective: @@ -747,6 +755,77 @@ class GraphChecker: return _repair_request(self.applier.nodes, self.applier.edges, issues, looks=looks_text) + async def coverage(self, *, level: str = "error") -> list[dict[str, Any]]: + """工具节点写死的 SQL 里,查的区间和表里的范围完全不重叠的(data/coverage.sql_gaps):搭图时没有查询结果,SQL 里 + 所有带时间谓词的表都查(按表去重、限时,不跑原 SQL,只跑算界和现查范围的两条小查询)。一条一个问题(形状同自查的 + 问题,code 是 sql_no_data);level 给 warning 时是收尾那一步的提醒。""" + out: list[dict[str, Any]] = [] + for nid, node in self.applier.nodes.items(): + cfg = ((node or {}).get("data") or {}).get("config") or {} + tool, args = cfg.get("tool"), cfg.get("args") + sql = args.get("sql") if isinstance(args, dict) else None + if not isinstance(tool, str) or not tool.startswith("db_query__") or not isinstance(sql, str) \ + or not sql.strip() or "{{" in sql: + continue + key = (nid, sql) + if key not in self._coverage: + self._coverage[key] = await self._gaps(tool[len("db_query__"):], sql) + out.extend({"level": level, "node_id": nid, "edge_id": None, "field": "args.sql", "code": "sql_no_data", + "message": f"{gap['message']}:这段时间表里没有数据,查出来会是 0", + "for_model": f"{gap['message']}:换有这段数据的表;没有这样的表,就在 done 的 explanation 里照实" + "告诉用户这段时间表里没有数据、数据只到哪天(写具体日期)"} + for gap in self._coverage[key]) + return out + + async def _gaps(self, name: str, sql: str) -> list[dict[str, Any]]: + from app.data import coverage + from app.data.engine import masked_columns + + row = next((s for s in self.sources or [] if getattr(s, "name", None) == name), None) + if row is None or not getattr(row, "enabled", True): + return [] + if self._coverage_cache is None: + self._coverage_cache = coverage.Cache() + try: + source = await _probe_source(row) + except Exception: # noqa: BLE001 - 上传表格没有可用的版本这类:不判 + return [] + return await coverage.sql_gaps(source, sql, facts=_profile_facts(row), masked=masked_columns(row.options), + cache=self._coverage_cache, budget=COVERAGE_BUDGET_S) + + +#: 自查时一条 SQL 的覆盖检查最多花多少秒(data/coverage.py):搭图要等它,比查询时的 BUDGET_S 短 +COVERAGE_BUDGET_S = 8.0 + + +async def _probe_source(row: Any) -> Any: + """自查时探查用的源:上传表格换成当前版本的快照视图(table_versions.resolve_source),别的原样。""" + from app.data import table_versions + from app.db.base import SessionLocal + + if (getattr(row, "origin", None) or "manual") != "upload": + return row + async with SessionLocal() as session: + return await table_versions.resolve_source(session, row) + + +def _profile_facts(row: Any) -> Any: + """现查表的范围不成时退回的剖析事实(catalog_profile_jobs.last_facts)。""" + async def facts(table: str) -> Any: + from app.data import catalog as data_catalog + from app.data.catalog_profile_jobs import last_facts + from app.db.base import SessionLocal + + name = data_catalog.resolve_table_name(getattr(row, "schema_cache", None), table) or table + async with SessionLocal() as session: + found = (await last_facts(session, str(row.id), [name])).get(name) + current = getattr(row, "current_snapshot_id", None) if (getattr(row, "origin", None) or "") == "upload" else None + if found and current and found.get("snapshot_id") != current: + return None # 上传表格:剖析时是另一版数据 + return found + + return facts + # ========================================================================== # 应用的落地 diff --git a/backend/app/data/schema_view.py b/backend/app/data/schema_view.py index 1b3ef7cb..6baed35d 100644 --- a/backend/app/data/schema_view.py +++ b/backend/app/data/schema_view.py @@ -69,6 +69,8 @@ PROFILE_ROWS_PROMPT = {"stats": "约 {n} 行(数据库统计估算)", "count "at_least": "至少 {n} 行(数到上限为止)"} PROFILE_RANGE_PROMPT = "{column} 从 {low} 到 {high}" PROFILE_HINT_PROMPT = "只供写查询参考(定时间范围、估表大小),不是查询结果,不能作为回答里的数" +#: 日期范围最早和最晚是同一天:这张表很可能只存最新一天(每天整表重灌),拿它算上周、上月只会查出 0 +PROFILE_ONE_DAY_PROMPT = "剖析时这张表的 {column} 只有一天({day})的数据,可能每天整表重灌:算历史周期请换有历史的表" PROFILE_STALE_PROMPT = "数据剖析:剖析早于当前版本,不给" #: 取值观察的结论(next-round 03 B2):最新业务日期;行数比剖析的新时用观察的 PROFILE_LATEST_PROMPT = "最新业务日期 {column} 到 {value}(取值观察,{day})" @@ -241,6 +243,7 @@ def profile_lines(facts: Mapping[str, Any] | None, masked: set[str], snapshot_id if upload and (not snapshot_id or facts.get("snapshot_id") != snapshot_id): return [indent + PROFILE_STALE_PROMPT] parts: list[str] = [] + one_day: list[str] = [] estimate = facts.get("row_estimate") if isinstance(facts.get("row_estimate"), Mapping) else None seen_rows = observed.get("rows") if isinstance(observed.get("rows"), Mapping) else None if seen_rows and str(seen_rows.get("at") or "") > str(facts.get("at") or ""): @@ -259,6 +262,8 @@ def profile_lines(facts: Mapping[str, Any] | None, masked: set[str], snapshot_id if not column or column.lower() in masked or found.get("min") is None or found.get("max") is None: continue parts.append(PROFILE_RANGE_PROMPT.format(column=column, low=_when(found["min"]), high=_when(found["max"]))) + if (day := _same_day(found["min"], found["max"])) is not None: + one_day.append(PROFILE_ONE_DAY_PROMPT.format(column=column, day=day)) for found in observed.get("freshness") or []: column = str((found or {}).get("column") or "") if column and column.lower() not in masked and found.get("max"): @@ -271,7 +276,15 @@ def profile_lines(facts: Mapping[str, Any] | None, masked: set[str], snapshot_id # 只有取值观察、没剖析过:照实说是观察 latest = max((str(f.get("at") or "") for f in [seen_rows or {}, *(observed.get("freshness") or [])]), default="") head = OBSERVE_HEAD_PROMPT.format(day=_day(latest), facts=";".join(parts)) - return [indent + head, indent + " " + PROFILE_HINT_PROMPT] + return [indent + head, *(indent + " " + line for line in one_day), indent + " " + PROFILE_HINT_PROMPT] + + +def _same_day(low: Any, high: Any) -> str | None: + """日期范围的两头是同一天(DATE、DATETIME、'YYYY-MM-DD'、'YYYYMMDD' 都认,data/coverage 同一个归一):那一天,不是为 None。""" + from app.data.coverage import _as_day + + lo, hi = _as_day(low), _as_day(high) + return lo.day.isoformat() if lo is not None and hi is not None and lo.day == hi.day else None # ========================================================================== diff --git a/backend/tests/test_copilot_coverage.py b/backend/tests/test_copilot_coverage.py new file mode 100644 index 00000000..0fc6624f --- /dev/null +++ b/backend/tests/test_copilot_coverage.py @@ -0,0 +1,134 @@ +"""助手搭图时就发现「查的区间里表里没有数据」(copilot_gen.GraphChecker.coverage、data/coverage.sql_gaps)。 + +部署上撞过:周报的取数节点用一张只存最新一天的表算上周、上上周,跑完才发现全是 0。现在: + +- 自查:助手搭的图里工具节点写死的 SQL,按今天跑一遍覆盖检查(不跑原 SQL,只跑算界和现查范围的两条小查询,按表去重、 + 限时);完全不重叠的作为一条问题交回模型改一轮(换有这段数据的表,或者在说明里照实告诉用户);修正后仍在的不挡运行, + 作为提醒留在检查结果里; +- look / db_schema 的剖析信息:日期范围最早和最晚是同一天时加一句「可能每天整表重灌:算历史周期请换有历史的表」; +- 助手提示:写按周期取数的 SQL 前先看表的日期范围。 + +库是现造的 SQLite 文件,表名、列名、数字都是虚构的。 +""" +from __future__ import annotations + +import json +import sqlite3 +import uuid + +import pytest +from langchain_core.messages import AIMessageChunk + +from app.data import introspect +from app.data.engine import engines +from app.data.schema_view import PROFILE_HINT_PROMPT, profile_lines +from app.db.base import SessionLocal +from app.db.models import DataSource +from tests.test_copilot_answer_ops import _sse, _use, client # noqa: F401 + +NO_DATA_SQL = "SELECT COUNT(*) AS n FROM gate_events WHERE event_date >= '20261012' AND event_date < '20261019'" +HAS_DATA_SQL = "SELECT COUNT(*) AS n FROM gate_events WHERE event_date >= '20200101'" + + +@pytest.fixture +async def plant(tmp_path): + path = tmp_path / f"{uuid.uuid4().hex[:8]}.db" + with sqlite3.connect(path) as db: + db.executescript("CREATE TABLE gate_events (event_date TEXT, member_id INTEGER);" + "INSERT INTO gate_events VALUES ('20200101', 1), ('20200101', 2);") + name = f"plant_{uuid.uuid4().hex[:6]}" + async with SessionLocal() as session: + row = DataSource(name=name, kind="sqlite", database=str(path), readonly=True, enabled=True) + session.add(row) + await session.flush() + row.schema_cache = await introspect.introspect(row) + await session.commit() + source_id = row.id + async with SessionLocal() as session: + source = await session.get(DataSource, source_id) + yield name, source + await engines.invalidate(source_id) + + +def _node(nid: str, ntype: str, config: dict) -> dict: + return {"id": nid, "type": ntype, "position": {"x": 0, "y": 0}, "data": {"label": nid, "config": config}} + + +def _graph(name: str, sql: str) -> tuple[dict, list]: + nodes = {"start": _node("start", "input", {"fields": [{"name": "question"}]}), + "fetch": _node("fetch", "tool", {"tool": f"db_query__{name}", "args": {"sql": sql}}), + "out": _node("out", "output", {"fields": [{"name": "n", "value": "{{ nodes.fetch }}"}]})} + return nodes, [{"source": "start", "target": "fetch"}, {"source": "fetch", "target": "out"}] + + +async def test_the_checker_returns_it_once_then_keeps_it_as_a_warning(plant): + from app.api.copilot_gen import GraphApplier, GraphChecker + + name, source = plant + nodes, edges = _graph(name, NO_DATA_SQL) + checker = GraphChecker(GraphApplier(nodes, edges), sources=[source]) + [issue] = [i for i in await checker.issues() if i["code"] == "sql_no_data"] + assert issue["level"] == "error" and issue["node_id"] == "fetch" and issue["field"] == "args.sql" + assert "查的区间是 2026-10-12 到 2026-10-18,但 gate_events 的 event_date 只有 2020-01-01 这一天的数据" \ + in issue["message"] + assert "换有这段数据的表" in issue["for_model"] and "explanation" in issue["for_model"] + # 交回模型改过一轮:之后不再挡;收尾时作为提醒 + assert not [i for i in await checker.issues() if i["code"] == "sql_no_data"] + [warning] = await checker.coverage(level="warning") + assert warning["level"] == "warning" and warning["code"] == "sql_no_data" + + +async def test_a_window_with_data_and_templated_sql_are_left_alone(plant): + from app.api.copilot_gen import GraphApplier, GraphChecker + + name, source = plant + for sql in (HAS_DATA_SQL, "SELECT COUNT(*) AS n FROM gate_events WHERE event_date >= '{{ input.day }}'"): + nodes, edges = _graph(name, sql) + checker = GraphChecker(GraphApplier(nodes, edges), sources=[source]) + assert not [i for i in await checker.issues() if i["code"] == "sql_no_data"], sql + + +async def test_the_stream_asks_for_one_repair_then_runs_with_a_warning(client, monkeypatch, plant): # noqa: F811 + name, _ = plant + nodes, edges = _graph(name, NO_DATA_SQL) + build = [{"op": "plan", "summary": "数一数"}, + *({"op": "add_node", "node": {"id": n["id"], "type": n["type"], "label": n["id"], + "config": n["data"]["config"]}} for n in nodes.values()), + *({"op": "add_edge", "edge": e} for e in edges), {"op": "done", "explanation": "数一数", "run": True}] + keep = [{"op": "done", "explanation": "gate_events 只有 2020-01-01 的数据,这段时间没有数据"}] + seen: list[str] = [] + + class Scripted: + async def astream(self, messages): + human = next(text for role, text in messages if role == "human") + seen.append(human) + for op in (keep if "上线前自查" in human else build): + yield AIMessageChunk(content=json.dumps(op, ensure_ascii=False) + "\n") + + _use(monkeypatch, Scripted()) + got = await _sse(client, {"instruction": "上周到访多少次", "intent": "answer"}) + [repairing] = [op for op in got if op["op"] == "check" and op["status"] == "repairing"] + assert [i["code"] for i in repairing["issues"]] == ["sql_no_data"] + assert any("上线前自查" in h and "换有这段数据的表" in h for h in seen), "交回模型改一轮" + [passed] = [op for op in got if op["op"] == "check" and op["status"] == "passed"] + assert [w["code"] for w in passed["warnings"]] == ["sql_no_data"] + [final] = [op for op in got if op["op"] == "final"] + assert final["next"] == "run", "修正后仍在的不挡运行" + assert any(i["code"] == "sql_no_data" and i["level"] == "warning" for i in final["issues"]) + + +def test_a_one_day_profile_says_so(): + facts = {"at": "2026-10-18T02:00:00+00:00", "date_ranges": [{"column": "event_date", "min": "20261017", + "max": "20261017"}]} + lines = profile_lines(facts, set(), None) + assert any("剖析时这张表的 event_date 只有一天(2026-10-17)的数据,可能每天整表重灌:算历史周期请换有历史的表" in ln + for ln in lines), lines + assert lines[-1].strip() == PROFILE_HINT_PROMPT + wide = {**facts, "date_ranges": [{"column": "event_date", "min": "20260101", "max": "20261017"}]} + assert not any("整表重灌" in ln for ln in profile_lines(wide, set(), None)) + + +def test_the_copilot_is_told_to_look_at_the_dates_first(): + from app.api.copilot import NODE_REFERENCE + + assert "先看表的日期范围" in NODE_REFERENCE and "只有一天的数据" in NODE_REFERENCE