mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-26 07:32:35 +00:00
探查失败不再伪装成"还没探查":把实情和下一步交给 agent
从 run 554a0f92 查出来的。那次问「userID=2 是谁」,9 次工具调用花掉 6 次、
前 29 秒没碰到一点真数据:
+ 2.1s db_schema → "还没有探查过结构,请在设置页里执行一次"
+ 4.9s db_schema ×7 个猜的表名 → "没有 user / users / member / …"
+ 7.7s SELECT 1 → 不信任前面的结果,先探一下连通性
+12.8s information_schema.tables ┐
+18.3s information_schema.tables ├ 自己把 schema 重建一遍
+21.5s information_schema.columns ┘
+29.2s 终于开始查真数据
根因:shop 的探查**失败过**(Lost connection to MySQL server during query),
但失败被存成了一个"成功但没表"的缓存,schema_synced_at 照常盖戳:
{"tables": {}, "total": 0,
"error": "OperationalError: (2013, 'Lost connection...')",
"available_schemas": ["prod_shop", "shop_dev", "toolsdb", …10 个]}
于是三处同时说假话:界面显示"结构未探查"、工具对 agent 说"还没有探查过"、
而那 10 个候选 schema 就躺在缓存里没人交出去。**用户明明点过探查,是超时了。**
**分清"还没探查"和"探查失败了"。** introspect 的结果加 failed 标记;
失败照样把缓存存下来(候选是下一步的线索),但不盖"已同步"的戳;
界面把红字和候选列出来;探查按钮的提示也分清是超时还是 schema 填错。
**给 agent 一个它做得到的下一步。** 以前那句"请在设置页里执行一次结构探查"
——agent 进不了设置页。现在换成实情 + 候选 schema + "不要猜表名,直接查
information_schema / sqlite_master"。那条自救路线 agent 最后也走通了,
只是自己摸索了三轮才找到,直接告诉它就行。
describe_table 同样:缓存空的时候说"数据源里没有 user"是在误导,真相是我们
什么都不知道。那次一口气问了 7 个表名,收到 7 条一模一样的
"没有 X。现有的对象:(还没探查过结构)",白花一整步。
顺带把逐表反射换成 SQLAlchemy 2.0 的 get_multi_*,但**别指望它治超时**:
实测 60 张表逐表 121 次往返、批量 122 次——反射缓存本来就把 get_columns 和
get_pk_constraint 合并了,而 MySQL 方言压根没实现 get_multi_*,落到基类还是
每表一次 SHOW CREATE TABLE。换它是因为这是 2.0 的正经接口、少三层
try/except,不是因为它快。shop 那次超时多半是偶发的,所以真正的对策
不在那儿,在"失败要如实记成失败"。
重新探查了一次 shop:153 张表、38 秒、成功。之前 agent 要花 6 次调用、
29 秒拼出来的东西,现在工具描述第一眼就给了。
验证:后端 269 passed(test_datasource.py 新增 4 条,守"失败不能被说成没探查过"
和"给 agent 的下一步它做得到");tsc 干净;三个 check 不回归。
This commit is contained in:
@@ -15,6 +15,15 @@ import pytest
|
||||
from app.data.engine import build_url, run_query, test_connection
|
||||
from app.data.guard import QueryLimits, SqlRejected
|
||||
from app.data.introspect import describe_table, introspect, summary, table_names
|
||||
from app.main import app as _fastapi_app
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def client():
|
||||
transport = ASGITransport(app=_fastapi_app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -72,7 +81,7 @@ async def test_summary_is_compact_enough_for_a_prompt(source):
|
||||
|
||||
async def test_summary_says_so_when_never_introspected(source):
|
||||
"""没探查过就明说,别让 Copilot 对着空气编表名。"""
|
||||
assert "尚未探查" in summary(source)
|
||||
assert "还没有探查过" in summary(source)
|
||||
|
||||
|
||||
async def test_describe_table_accepts_qualified_name(source):
|
||||
@@ -225,3 +234,103 @@ def test_schema_stats_sql_covers_every_supported_kind():
|
||||
for kind in SUPPORTED_KINDS:
|
||||
assert kind in _SCHEMA_STATS_SQL or kind in ("sqlite", "mysql", "mariadb",
|
||||
"postgres", "postgresql")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 探查失败 ≠ 还没探查
|
||||
# --------------------------------------------------------------------------
|
||||
#
|
||||
# run 554a0f92:shop 的探查因为 "Lost connection to MySQL server during query"
|
||||
# 失败了,但失败被存成了一个"成功但没表"的缓存,schema_synced_at 照常盖戳。
|
||||
# 于是 db_schema 工具对 agent 说"还没有探查过结构,请在设置页里执行一次"——
|
||||
# 一句错话,配一个 agent 根本做不到的动作。它只好自己摸:猜 7 个表名、发一条
|
||||
# SELECT 1 试连通性、再用三条 information_schema 把 schema 重建一遍。
|
||||
# 9 次工具调用花掉 6 次,前 29 秒没碰到一点真数据。
|
||||
#
|
||||
# 而 available_schemas 那 10 个候选一直躺在缓存里,没人交给它。
|
||||
|
||||
|
||||
class _FailedCache:
|
||||
"""探查失败之后 schema_cache 的样子。"""
|
||||
|
||||
name = "shop"
|
||||
kind = "mysql"
|
||||
database = "prod_shop"
|
||||
description = ""
|
||||
readonly = True
|
||||
schema_cache = {
|
||||
"tables": {}, "total": 0, "failed": True,
|
||||
"error": "OperationalError: (2013, 'Lost connection to MySQL server during query')",
|
||||
"available_schemas": ["prod_shop", "shop_dev", "toolsdb"],
|
||||
}
|
||||
|
||||
|
||||
class _NeverRun(_FailedCache):
|
||||
"""从来没探查过的样子。和上面必须说不一样的话。"""
|
||||
|
||||
schema_cache: dict = {}
|
||||
|
||||
|
||||
def test_why_empty_tells_the_two_cases_apart():
|
||||
from app.data.introspect import why_empty
|
||||
|
||||
assert "失败" in why_empty(_FailedCache.schema_cache)
|
||||
assert "Lost connection" in why_empty(_FailedCache.schema_cache)
|
||||
assert why_empty({}) == "还没有探查过结构"
|
||||
|
||||
|
||||
def test_schema_tool_tells_the_agent_the_truth():
|
||||
"""给 agent 的那句话要是实情,而且要给一个它做得到的下一步。"""
|
||||
from app.tools.datasource import _no_schema_note
|
||||
|
||||
note = _no_schema_note(_FailedCache)
|
||||
assert "失败" in note and "Lost connection" in note
|
||||
# 候选一直在缓存里,以前没人交出去
|
||||
assert "prod_shop" in note and "toolsdb" in note
|
||||
# agent 进不了设置页。它做得到的是自己查数据字典——那条路它最后也走通了,
|
||||
# 只是自己摸索了三轮
|
||||
assert "information_schema" in note
|
||||
assert "不要猜表名" in note
|
||||
# 不能再说"请到设置页"
|
||||
assert "设置页" not in note
|
||||
|
||||
fresh = _no_schema_note(_NeverRun)
|
||||
assert "还没有探查过" in fresh
|
||||
assert "失败" not in fresh
|
||||
|
||||
|
||||
def test_describe_table_does_not_claim_the_table_is_missing():
|
||||
"""缓存是空的时候,说"里面没有这张表"是在误导——真相是我们什么都不知道。"""
|
||||
from app.data.introspect import describe_table
|
||||
|
||||
out = describe_table(_FailedCache, "user")
|
||||
assert "无法判断" in out
|
||||
assert "失败" in out
|
||||
# 以前是 "数据源「shop」里没有 user。现有的对象:(还没探查过结构)"
|
||||
assert "现有的对象" not in out
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_introspect_failure_does_not_get_a_synced_stamp(client, monkeypatch):
|
||||
"""失败要把缓存留下(候选是下一步的线索),但不能盖"已同步"的戳。
|
||||
|
||||
以前盖了,于是界面说同步过、工具说还没探查过,两边都不说真话。
|
||||
"""
|
||||
import app.api.datasources as mod
|
||||
|
||||
async def failed(source, schema=None):
|
||||
return dict(_FailedCache.schema_cache)
|
||||
|
||||
created = await client.post("/api/datasources", json={
|
||||
"name": "introspect_fail", "kind": "sqlite", "database": ":memory:",
|
||||
})
|
||||
assert created.status_code == 201, created.text
|
||||
sid = created.json()["id"]
|
||||
|
||||
monkeypatch.setattr(mod.introspect_mod, "introspect", failed)
|
||||
out = (await client.post(f"/api/datasources/{sid}/introspect")).json()
|
||||
|
||||
assert out["schema_synced_at"] is None, "失败不该盖已同步的戳"
|
||||
assert "Lost connection" in out["schema_error"]
|
||||
assert out["available_schemas"] == ["prod_shop", "shop_dev", "toolsdb"]
|
||||
assert out["table_count"] == 0
|
||||
|
||||
Reference in New Issue
Block a user