mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-26 04:42:09 +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:
@@ -72,12 +72,20 @@ class DataSourceOut(BaseModel):
|
||||
has_password: bool = False
|
||||
table_count: int = 0
|
||||
schema_synced_at: str | None = None
|
||||
#: 上次探查为什么没拿到表。"还没探查"和"探查失败了"是两回事:
|
||||
#: 前者去点一下按钮就行,后者点了也没用,得先解决超时或者换 schema
|
||||
schema_error: str = ""
|
||||
#: 这台服务器上还有哪些库/schema 可选。探不到表时它就是下一步的线索
|
||||
available_schemas: list[str] = Field(default_factory=list)
|
||||
tools: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
def _to_out(row: DataSource) -> DataSourceOut:
|
||||
tables = (row.schema_cache or {}).get("tables") or {}
|
||||
cache = row.schema_cache or {}
|
||||
tables = cache.get("tables") or {}
|
||||
return DataSourceOut(
|
||||
schema_error=str(cache.get("error") or "") if cache.get("failed") else "",
|
||||
available_schemas=list(cache.get("available_schemas") or []),
|
||||
id=row.id, name=row.name, kind=row.kind, host=row.host, port=row.port,
|
||||
database=row.database, username=row.username, options=row.options or {},
|
||||
readonly=row.readonly, description=row.description, enabled=row.enabled,
|
||||
@@ -190,7 +198,11 @@ async def introspect_source(
|
||||
row.schema_cache = await introspect_mod.introspect(row, schema=schema)
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise HTTPException(400, f"探查失败:{type(e).__name__}: {e}") from e
|
||||
row.schema_synced_at = datetime.now(timezone.utc)
|
||||
# 失败也要把缓存存下来——里面的 available_schemas 正是下一步的线索——
|
||||
# 但**不能盖上"已同步"的戳**。以前盖了,于是界面显示同步过、工具却说
|
||||
# "还没探查过",两边都不说真话(run 554a0f92)
|
||||
if not row.schema_cache.get("failed"):
|
||||
row.schema_synced_at = datetime.now(timezone.utc)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return _to_out(row)
|
||||
|
||||
Reference in New Issue
Block a user