画完先自查:条件写错在校验时就拦下,Copilot 交付前按同一套规则查一遍、查出问题先改

开发库里四次运行死在同一句"循环条件写错了:表达式里不允许出现 Set"。条件原文是
`{{ vars.gate }} != "ok"`——把模板写法带进了表达式:`{{ x }}` 在 Python 里是"装着
一个集合的集合"。Copilot 的提示词写着"模板语法在任意字符串里用",模型照做而已;
校验又不看表达式,写错了只有跑到那一步才知道,前面的步骤白跑。

1. 表达式:解析和静态检查收成一个 parse_expression,运行时、校验、Copilot 自查共用,
   不会出现"校验说能跑、跑起来报错"。多套的 {{ }} 意思无歧义,在语法树上剥掉(字符串
   里的花括号不动),按 vars.x 理解并提示。真写错的给能照改的提示:| 过滤器 → len(x)、
   集合 → 列表、不认识的函数列出能用的、= 当 ==、引号里的 {{ }} 永远不成立、推导式、
   切片、私有属性。and / or 短路掉的那半边也查。

2. 校验:按节点类型和模式挑出真正求值的字段(分支条件、while 条件、整形表达式、口径卡
   指标、skip_if)。写错是 error、挡住运行;漏写 vars. 的名字提示会取到空值;while 没写
   条件、整形没填表达式也说出来。foreach 的 condition、模板模式的 expression 不求值,不查。

3. 代码节点:assign_to 拿的是 stdout。「测试 1」修好条件后跑通了、结果却是空的——代码
   最后一行是裸表达式,那是 notebook 的行为。设了 assign_to 却找不到任何输出的 Python
   代码,校验直接拦下,提示 print(json.dumps(结果))。宁可漏判不可误判:子进程、exec、
   附带文件里的 print 都算有输出;整行只有一个 {{ }} 的代码是运行时才拿到的(⑤号示例
   整段就是 {{ vars.code }}),看不见,不猜。库里 20 张工作流无一误拦。

4. Copilot 自查:搭完用同一套校验过一遍,error 级的问题连同图交回模型——只修这些、只
   输出改动,最多两轮。改好了才自动运行;改不好照实交付、列出剩下的问题,不自动开跑。
   没问题的图不多花一次调用;只是多套了 {{ }} 的不返工。提示词改成"模板只在普通文本
   字段里用",并列出表达式字段的对 / 错写法和代码节点必须 print。

5. 前端:自查这一段在 Copilot 的过程里看得见——发现几处问题、交回去改、改好了没有;
   改不好标红并说明没有自动运行。"搭好了,共几步"那一行不会被挤掉。

测试:新增 test_expressions(开发库里真实死过的四条条件、静态提示、按模式挑字段、代码
输出、带 {{ }} 的循环真跑一遍)与 test_copilot_selfcheck(脚本化模型:改好 → 自动运行;
改不好 → 不运行、最多两轮;干净的图一次调用;多套 {{ }} 不返工)。test_continue_run 里
用来制造运行时失败的表达式换成 `2 ** 100000 > 1`——原来那条现在在校验阶段就被拦下了。
check-decode 补了自查的解码断言。
This commit is contained in:
ThearchyHelios
2026-09-25 17:04:39 +08:00
parent 03830d4815
commit 0997603ccf
11 changed files with 718 additions and 17 deletions
+1
View File
@@ -284,4 +284,5 @@ export const PHASE_TEXT: Record<string, string> = {
building: '正在放置节点…',
wiring: '正在连接数据流…',
finalizing: '正在排版和校验…',
repairing: '自查发现问题,正在修正…',
}
+30 -3
View File
@@ -1029,6 +1029,7 @@ const PHASE_LABEL: Record<string, string> = {
building: '正在搭建流程',
wiring: '正在连接数据流',
finalizing: '正在排版和校验',
repairing: '正在按自查结果修正',
}
/**
@@ -1117,13 +1118,39 @@ export function decodeCopilot(ops: CopilotOp[]): Step[] {
title: `流程搭好了,加了 ${nodeCount} 步`,
detail: String(op.explanation ?? '') || undefined })
break
case 'check': {
// 服务端自查:用运行时同一套规则过一遍,有问题交回模型改。
// 这件事得看得见——否则多出来的那十几秒像是卡住了,改了什么也无从知道
const issues = Array.isArray(op.issues) ? op.issues.map(String) : []
if (op.status === 'repairing') {
out.push({ id: `ck-${i}`, seq: i, kind: 'note', status: 'done',
title: `自查发现 ${issues.length} 处问题,交回去改`,
detail: issues.join('\n') || undefined })
} else if (op.status === 'passed') {
closeLifecycles(out, 'done')
out.push({ id: `ck-${i}`, seq: i, kind: 'note', status: 'done',
title: op.repaired ? '自查通过:问题已经改好' : '自查通过' })
} else {
closeLifecycles(out, 'failed')
out.push({ id: `ck-${i}`, seq: i, kind: 'error', level: 'error', status: 'failed',
title: op.status === 'failed'
? `自查后还有 ${issues.length} 处问题,没有自动运行`
: String(op.message ?? '自查没能完成'),
detail: issues.join('\n') || undefined })
}
break
}
case 'final': {
// 后端排版校验后的最终图才知道整张图有几步。nodeCount 只是这一轮
// 新增的数量——在"改图"场景下说"共 2 步"是错的,图上明明有四个节点
// 新增的数量——在"改图"场景下说"共 2 步"是错的,图上明明有四个节点。
// 自查的记录会排在"搭好了"后面,所以往回找那一行,不能只看最后一条
const total = op.graph?.nodes?.length
const last = out[out.length - 1]
if (total && last?.kind === 'lifecycle') {
last.title = nodeCount && nodeCount < total
// 流没等到 done 就断了时没有那一行,沿用原来的做法:改写最后一条生命周期
const built = [...out].reverse().find((s) => s.id.startsWith('cd-'))
?? (last?.kind === 'lifecycle' ? last : undefined)
if (total && built) {
built.title = nodeCount && nodeCount < total
? `流程搭好了,加了 ${nodeCount} 步,整张图共 ${total} 步`
: `流程搭好了,共 ${total} 步`
}
+9 -1
View File
@@ -156,7 +156,7 @@ interface StudioState {
error: string
model: string
// 从提交到第一个操作之间有 5~30 秒,得让用户看见这段在发生什么
phase: 'connecting' | 'planning' | 'building' | 'wiring' | 'finalizing' | ''
phase: 'connecting' | 'planning' | 'building' | 'wiring' | 'finalizing' | 'repairing' | ''
thinking: string // 模型的思考原文(只有支持 thinking 的模型有)
elapsedMs: number
lastInstruction: string // 失败后"用同一需求重试"要用
@@ -560,6 +560,14 @@ export const useStudio = create<StudioState>((set, get) => ({
case 'plan':
set({ copilot: { ...s.copilot, phase: 'planning', lastOp: op.summary ?? '规划中' } })
break
case 'check':
// 服务端自查:用运行时同一套规则过一遍,有问题交回模型改
set({ copilot: { ...s.copilot,
phase: op.status === 'repairing' ? 'repairing' : s.copilot.phase,
lastOp: op.status === 'repairing'
? `自查发现 ${(op.issues ?? []).length} 处问题,正在修正`
: op.status === 'passed' ? '自查通过' : '自查后仍有问题,先别急着运行' } })
break
case 'add_node': {
const n = op.node
// 类型不认识就不放上画布:节点卡片、属性面板都按类型查定义,查不到