目录视图接缝 data/catalog_view.py(细案 01 做法 5,总方案 X7):读目录的四处统一经它,「开目录 / 关目录 / 只用已核实的项 / 目录快照」的对照才做得了

- CatalogView{mode: live | snapshot | off, statuses: all | reviewed, snapshot}:缺省 live + all,行为和以前一样;reviewed 只留已核实、已确认的项(驳回的项原样留着交给各处,照旧经 visible_notes 去掉,冻结快照和 SQL 检查靠驳回的关系编号认出不该现推的关系——为此在 catalog.py 加 restrict_notes,没给 visible_notes 加参数)
- 视图从哪来:运行从 Run.options['catalog_view'] 读(catalog_view.of_run:RunContext.extra 里的发起快照,审批恢复、续跑沿用),ToolContext 多一个 catalog_view,节点(调用工具、llm/agent、多 agent)构造时带上;生成过程从参数读(下一个提交接 answer_ops)
- 四处消费一律经它:运行时 db_schema / db_query(tools/datasource._catalog_of,冻结进表结构快照的也是这一份)、look 表详情(TableKind.load、copilot_context.look_service 收 catalog_view)、自查的 SQL 检查器(sqlcheck.load_checkers 收 catalog_view,改用 catalog.read_catalogs 一次读出)、双盲复算(_Sources 带视图,交给盲算的目录和比较 SQL 用的中文名都按它;核查 agent、反方审稿的 db_schema 也按这次运行的视图)
- 冻结快照:不是缺省视图的在引用 meta 里记下 catalog_view(不进内容哈希,缺省视图不记,快照和以前一字不差),证据面板据此写得出「本次运行目录:关」
- snapshot 模式只定读法(kind=eval_catalog_snapshot 的工件,{sources: {源 id: {tables: {表: {version, notes}}}}}),内容由第 2 波的题集包写;快照不在了照实报错(CatalogSnapshotMissing),不悄悄退回当前目录
- 新增 tests/test_catalog_view.py:四种视图在四处给的是同一份、缺省等于当前目录、运行从 options 读且冻结 meta 记下、lint(消费模块不许直接 read_catalog / read_entry / 查 CatalogNote);去掉 _catalog_of 里的视图回跑两项失败
This commit is contained in:
ThearchyHelios
2026-10-16 10:28:01 +08:00
parent dd00234622
commit 4c0e83bc80
14 changed files with 548 additions and 47 deletions
+13 -13
View File
@@ -1084,36 +1084,36 @@ def graph_checks(nodes: Iterable[Any], checkers: Mapping[str, SqlChecker], *,
return out
async def load_checkers(session: Any, sources: Iterable[Any]) -> dict[str, SqlChecker]:
async def load_checkers(session: Any, sources: Iterable[Any], *, catalog_view: Any = None) -> dict[str, SqlChecker]:
"""给一批数据源(DataSource 行)各建一个检查器:{源名: SqlChecker}。没有目录的源不建。
一次查询读出这些源的全部目录。读不到就当没有目录(返回空 dict):检查是附加的,不能挡住自查和发布本身。
一次查询读出这些源的全部目录,读哪一份由 catalog_view 定(data/catalog_view.py:当前目录、目录快照、关;只用已核实
的项),不给是当前目录。读不到当前目录就当没有目录(返回空 dict):检查是附加的,不能挡住自查和发布本身;视图指定的
目录快照不在了照实报错。
"""
from sqlalchemy import select
from app.db.models import CatalogNote
from app.data.catalog_view import CatalogSnapshotMissing, CatalogView, read_many
rows = [s for s in sources if getattr(s, "id", None) and dialect_of(getattr(s, "kind", None))]
if not rows:
return {}
try:
notes = (await session.execute(
select(CatalogNote.source_id, CatalogNote.table_name, CatalogNote.notes)
.where(CatalogNote.source_id.in_([r.id for r in rows]))
)).all()
entries = await read_many(session, CatalogView.parse(catalog_view), [r.id for r in rows])
except CatalogSnapshotMissing:
raise
except Exception: # noqa: BLE001
logger.exception("读取数据目录失败,本次不做基于目录的 SQL 检查")
return {}
by_source: dict[str, dict[str, Any]] = {}
for source_id, table, value in notes:
if isinstance(value, dict) and value:
by_source.setdefault(source_id, {})[table] = value
for source_id, tables in entries.items():
for table, entry in tables.items():
if isinstance(entry.notes, dict) and entry.notes:
by_source.setdefault(source_id, {})[table] = entry.notes
return {r.name: SqlChecker(kind=r.kind, schema_cache=r.schema_cache, notes=by_source[r.id], source_name=r.name)
for r in rows if r.id in by_source}
async def load_checkers_by_name(session: Any, names: Iterable[str]) -> dict[str, SqlChecker]:
"""按数据源名建检查器(只看启用着的源)。发布门禁用:图里写的是源名。"""
"""按数据源名建检查器(只看启用着的源)。发布门禁用:图里写的是源名,读的是当前目录。"""
from sqlalchemy import select
from app.db.models import DataSource