Excel 导入改造期 2:配方引擎与确认向导——表从哪一行哪一列开始、哪些是合计、日期怎么补年份,都写进一份可校验的配方;首次导入经人确认,之后每期按配方重放并核对

配方语言(agentlab-recipe/2,新 recipe_types、recipe)
- 封闭、不含正则、保存时静态校验:常量只能从表里的候选文字里选,占位符只能映射成空值,单位从固定词表里取
- 交叉表和列表两种版式;分段各自定位、不依赖先后顺序;合计按标签语义区间核对,关系恒等式、口径不同的声明都写在配方里
- 固定的解析器集合(parsers/2):全角、各类横线、千分位、月日写法宽容归一,规范写法存库、原文另记

执行器(新 xlsx_cells、recipe_engine)
- 两遍读取逐行对账,每个非空格都要有去向(格子账);交叉表逆透视成长表,按统计期补年份并断言覆盖整期
- 合计行改作核对并另存一张表,不和明细混在一列;区域外文字、隐藏行列、合并单元格、空行之后的文字都单独报出
- STRICT 加主键写临时库;按锚点段记溯源,供后续证据下钻用;大表流式读取,扫描在 sheetData 处停止

校验与说明(新 recipe_checks、recipe_notes)
- SQL 核对分通过、不一致、无法核对三种结果,失败分结构、数据质量、需确认三类;结构类直接拒收
- 数据质量类可以写理由接受,接受不改库、只改状态和说明
- 表的说明按核对结果生成、不含数字,经过数字检查;没核对过的不写「已核对」

起草(新 recipe_suggest、recipe_ai)
- 先用规则起草:建议卡片加待确认问题(关系要不要登记、占位符表示什么)
- 规则起草不完整时才提示 AI 兜底:界面先展示要发出的全部文字,用户同意才发;只发结构,数字、隐藏行列、遮罩列不发,公式先脱敏;同意绑定模型和服务方;AI 只写配方,有限次修订,记 token 和费用;截断单独识别
- 无论规则还是 AI 起草,都必须人工确认才能启用

导入流程(新 recipe_imports、recipe_confirm、recipe_diff、api/table_imports)
- 暂存 → 起草 → 回答问题或改配方 → 试运行 → 勾确认项、写接受理由 → 确认并启用;接入期 1 的构建、导入记录、快照和运行固定版本
- 上传新一期按已确认的配方重放,不调用模型,出回执和差异卡,手动启用
- 改配方报破坏性变更(breaking:*)、单位变化、工作表改名的兜底,都要逐项勾选确认

前端导入向导(新 pages/import)
- 原始网格(行列号、去向着色)、建议卡片、配方面板(表单,不露正则)、试运行回执、确认清单、差异卡、AI 起草的同意对话框
This commit is contained in:
ThearchyHelios
2026-10-01 22:00:07 +08:00
parent 8059cd5147
commit 2d5e50fa88
64 changed files with 36299 additions and 112 deletions
+30 -2
View File
@@ -134,6 +134,25 @@ def unshaped_tables(source: Any) -> list[str]:
if isinstance(meta, dict) and meta.get("comment") == UNSHAPED_NOTE]
def reported_total_tables(source: Any) -> list[str]:
"""按配方导入时另存的「原表写明的合计」表(表结构里 kind=reported_total,recipe_notes.apply_notes 记的)。"""
tables = (getattr(source, "schema_cache", None) or {}).get("tables") or {}
return [meta.get("qualified", name) for name, meta in tables.items()
if isinstance(meta, dict) and meta.get("kind") == "reported_total"]
#: 原表写明的合计表在工具描述、db_schema 表清单里的标记。不含数字(H11)
REPORTED_TOTAL_MARK = "(原表写明的合计,不要彼此相加,也不要与明细相加)"
#: 按配方导入的源在工具描述「可用表」之后多加的两句(P2-SPEC 5.4,不含数字)
RECIPE_QUERY_HINT = " 中文表名和列名请加双引号。查单个值时,把主键列一起选出来,证据面板才能追到原表格子。"
#: 冻结 schema_snapshot 时从 schema_cache 复制的顶层键。import_mode、import_manifests 只有按配方导入的源才有:
#: 冻结进来以后,schema_snapshot 的内容哈希就承诺了导入清单(证据链按哈希从封存事件走到清单,中间不经过
#: 可改的数据库列,H5);手工源和期 1 的上传源没有这两个键,冻结内容和原来一字不差
_FROZEN_SCHEMA_KEYS = ("schema", "synced_at", "truncated", "total", "import_mode", "import_manifests")
def _query_description(source: Any) -> str:
tables = introspect.table_names(source)
head = f"在数据源「{source.name}」上执行 SQL 查询。"
@@ -144,11 +163,18 @@ def _query_description(source: Any) -> str:
listed = "、".join(tables[:25])
more = f" 等 {len(tables)} 张表" if len(tables) > 25 else ""
head += f" 可用表:{listed}{more}。"
# 按配方导入的源:表名列名是中文(不加引号在多数方言里报错),主键是证据面板追溯原表格子的钥匙
if (getattr(source, "schema_cache", None) or {}).get("import_mode") == "recipe":
head += RECIPE_QUERY_HINT
# 未规整的说明以前只在 db_schema 查单表时看得到:只绑了查询工具的 Agent、调用工具节点接报告的
# 链路都看不到,照样对列求和。工具描述里点名(不含数字,H11)
if raw := unshaped_tables(source):
shown = "、".join(raw[:10]) + (f" 等 {len(raw)} 张" if len(raw) > 10 else "")
head += f" 其中 {shown} 按原样导入、未经规整:同一列里混有不同口径的行,不能直接对列求和。"
# 原表写明的合计表同理:「不要相加」的说明只在 db_schema 查单表时看得到(AU-5)
if totals := reported_total_tables(source):
shown = "、".join(totals[:10]) + (f" 等 {len(totals)} 张" if len(totals) > 10 else "")
head += f" 其中 {shown} 是原表写明的合计:各合计项可能互相重叠,不要彼此相加,也不要与明细表相加。"
head += " 字段不确定时先查结构,不要猜字段名。"
else:
head += _no_schema_note(source)
@@ -473,7 +499,7 @@ async def _store_schema(source: Any, ctx: ToolContext) -> str | None:
from app.engine.evidence import SCHEMA_SNAPSHOT
content = {"source": source.name, "tables": tables,
**{k: cache[k] for k in ("schema", "synced_at", "truncated", "total") if k in cache}}
**{k: cache[k] for k in _FROZEN_SCHEMA_KEYS if k in cache}}
try:
return await put_json(content, kind=SCHEMA_SNAPSHOT, run_id=ctx.run_id or "", node_id=ctx.node_id or "",
meta={"source": source.name, "tables": len(tables)})
@@ -497,8 +523,10 @@ def _make_schema_tool(source: Any, *, fixed: bool = False) -> StructuredTool:
# 把实情交出去,而不是让它去点一个它点不到的按钮
return f"数据源「{source.name}」的结构信息不可用。" + _no_schema_note(source)
raw = set(unshaped_tables(source))
totals = set(reported_total_tables(source))
return f"数据源「{source.name}」共 {len(tables)} 张表:\n" + "\n".join(
f" {n}" + ("(按原样导入、未经规整,不能直接对列求和)" if n in raw else "") for n in tables
f" {n}" + ("(按原样导入、未经规整,不能直接对列求和)" if n in raw else "")
+ (REPORTED_TOTAL_MARK if n in totals else "") for n in tables
)
return StructuredTool(