mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 21:51:59 +00:00
助手搭图时就发现「查的区间里表里没有数据」:自查(GraphChecker)对工具节点写死的 SQL 按今天跑一遍覆盖检查(data/coverage.sql_gaps:不跑原 SQL,只跑算界和现查范围的两条小查询,SQL 里所有带时间谓词的表都查,按表去重、限时,异步查库,带模板的 SQL 不查);完全不重叠的作为一条 sql_no_data 问题交回模型改一轮(换有这段数据的表,没有就在 done 的说明里照实告诉用户数据只到哪天),修正后仍在的不挡运行,作为提醒留在检查结果和交付的问题清单里(问数据流式、画布非流式都是)。look / db_schema 的剖析信息在日期范围最早和最晚是同一天时加一句「剖析时这张表的 X 只有一天的数据,可能每天整表重灌:算历史周期请换有历史的表」;助手节点手册写明按周期取数前先看表的日期范围,只有一天、最新日期早于要查的区间的表不能用来算这段时间
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
# ==========================================================================
|
||||
# 应用的落地
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
# ==========================================================================
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user