Files
dacli/tools/check-prompt-rules.ts
T
sswandClaude 3f1cb3a03b feat(dacli): JNPF7 验证闭环 Stage 0 + P0-6 preview + workflow canvas 回归修复
Stage 0 (验证基础设施):
- 5 层验证工具链 (tools/) + JSON Schema + validate-baseline.sh CI gate
- P0-9: components-whitelist.yaml 单一来源 + parseFlowXml 消漂移 (create.ts tag 改消费 component-map.json)
- E-2: docs/components.md 文档化 6 个 pass-through 组件
- P1-16: validate-baseline 接入 quality-gate.sh
- L3 回归测试 (bs117 / bpmn-bs / antipattern-output) + mock LLM 库

P0-6 (user-in-the-loop):
- form create --ai --preview: 提交前渲染字段表格 (CJK 对齐) + TTY 交互确认 (_preview.ts)

workflow canvas 回归修复 (v0.22+ SSW 迁移引入):
- parseFlowXml 命名空间无关 (flowNodes 缺口)
- _autoLayout 无 DI 时合成完整 BPMNDiagram+Shape+Edge + bpmn2 归一化 (canvas 缺口)
- 真实后端 + 浏览器设计器验证画布渲染; 详见 jnpf7-analysis §14

quality-gate 全绿: tsc 0 · vitest 483 · doc:check · baseline L1-L5

Co-Authored-By: Claude <[email protected]>
2026-07-27 12:49:44 +08:00

256 lines
7.6 KiB
TypeScript

/**
* L2.1 — Prompt rules coverage scanner
*
* Scans BUILD_FORM_PROMPT and BUILD_WORKFLOW_PROMPT in AIRepairServiceImpl.java
* for required rules. Outputs PASS/FAIL matrix.
*
* Rules:
* PR-1 7 forbidden rules (select→relationForm, userSelect→input, etc.)
* PR-2 f_ prefix enforced
* PR-3 fieldName snake_case
* PR-4 JPFS jnpfKey whitelist
* PR-5 7-relationForm schema fields listed (modelId/propsValue/...)
* PR-6 7 forbidden rules recency bias repetition at user prompt end
* WR-1 ≥4 BPMN nodes rule
* WR-2 No URL-encoded flowXml
* WR-3 No empty flowXml (≥1 userTask)
* WR-4 process id == enCode rule
* WR-5 flowable:candidateGroups / assignee required
* WR-6 ≥1 exclusiveGateway branch (no single-line flow)
*
* Usage:
* npx tsx tools/check-prompt-rules.ts
* npx tsx tools/check-prompt-rules.ts --json
*
* Exit codes:
* 0 = all rules pass
* 1 = one or more rules missing
*/
import { readFileSync } from 'node:fs'
const FILE = '/home/ubuntu/ccWorkDir/boot/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/AIRepairServiceImpl.java'
interface Rule {
id: string
scope: 'form' | 'workflow' | 'form-recency'
description: string
pattern: RegExp
}
const RULES: Rule[] = [
// PR-1: 7 forbidden rules (select→relationForm, userSelect→input, etc.)
{
id: 'PR-1a-select-not-relationForm',
scope: 'form',
description: '禁用 select 替代 relationForm',
pattern: /禁用\s*select\s*替代\s*relationForm/,
},
{
id: 'PR-1b-switch-not-select',
scope: 'form',
description: '禁用 switch 替代 select',
pattern: /禁用\s*switch\s*替代\s*select/,
},
{
id: 'PR-1c-userSelect-not-input',
scope: 'form',
description: '禁用 userSelect 替代 input',
pattern: /禁用\s*userSelect\s*替代\s*input/,
},
{
id: 'PR-1d-radio-checkbox-default',
scope: 'form',
description: '禁用 radio/checkbox 默认选',
pattern: /禁用\s*radio\s*\/\s*checkbox\s*默认/,
},
{
id: 'PR-1e-textarea-default',
scope: 'form',
description: '禁用 textarea 默认',
pattern: /禁用\s*textarea\s*默认/,
},
{
id: 'PR-1f-input-numNNN-hallucination',
scope: 'form',
description: '禁用 input_num001 幻觉命名',
pattern: /禁用\s*input_num001|input_num.*幻觉|_num\s*后缀/,
},
{
id: 'PR-1g-chinese-fieldName',
scope: 'form',
description: '禁用含中文字段名',
pattern: /禁用.*中文字段名/,
},
// PR-2: f_ prefix
{
id: 'PR-2-f-prefix',
scope: 'form',
description: 'f_ 前缀强制',
pattern: /f_\s*开头|f_xxx_yyy|以\s*f_\s*开头/,
},
// PR-3: snake_case
{
id: 'PR-3-snake-case',
scope: 'form',
description: 'fieldName 必须 snake_case',
pattern: /snake_case/,
},
// PR-4: component whitelist
{
id: 'PR-4-component-whitelist',
scope: 'form',
description: 'JNPF 组件白名单',
pattern: /白名单|whitelist/,
},
// PR-5: 7 relationForm fields
{
id: 'PR-5-relationForm-7-fields',
scope: 'form',
description: 'relationForm 7 字段 schema',
pattern: /modelId[\s\S]{0,50}propsValue[\s\S]{0,50}relationField[\s\S]{0,50}showField[\s\S]{0,50}hasPage[\s\S]{0,50}queryType[\s\S]{0,50}columnOptions/,
},
// PR-6: recency bias repetition
{
id: 'PR-6-recency-bias',
scope: 'form-recency',
description: 'user prompt 末尾 7 forbidden 规则重申',
pattern: /末尾重申.*7\s*禁用规则|7\s*禁用规则.*末尾重申/,
},
// WR-1: ≥4 BPMN nodes
{
id: 'WR-1-min-4-nodes',
scope: 'workflow',
description: '≥4 BPMN 节点强制',
pattern: /≥\s*4\s*节点|至少.*4\s*节点/,
},
// WR-2: no URL-encoded flowXml
{
id: 'WR-2-no-url-encoding',
scope: 'workflow',
description: '禁用 URL-encoded flowXml',
pattern: /禁用\s*URL[- ]?encoded|不\s*URL[- ]?encode/,
},
// WR-3: no empty flowXml
{
id: 'WR-3-no-empty-flowXml',
scope: 'workflow',
description: '禁用空 flowXml (≥1 userTask)',
pattern: /禁用空\s*flowXml|至少\s*1\s*个.*bpmn:userTask/,
},
// WR-4: process id == enCode
{
id: 'WR-4-process-id-encode',
scope: 'workflow',
description: 'process id 必须等于 enCode',
pattern: /process\s*id\s*.*enCode|process id='?<流程\s*enCode>/,
},
// WR-5: flowable:assignee / candidateGroups
{
id: 'WR-5-flowable-assignee',
scope: 'workflow',
description: 'flowable:assignee / candidateGroups 强制',
pattern: /flowable:assignee|flowable:candidateGroups|assignee\s*或\s*candidateGroups/,
},
// WR-6: ≥1 exclusiveGateway
{
id: 'WR-6-min-1-gateway',
scope: 'workflow',
description: '≥1 exclusiveGateway 分支强制',
pattern: /exclusiveGateway|至少\s*1\s*个.*exclusiveGateway|至少加\s*exclusiveGateway/,
},
]
interface RuleResult {
id: string
scope: string
description: string
pass: boolean
}
function main() {
const args = process.argv.slice(2)
const jsonMode = args.includes('--json')
const content = readFileSync(FILE, 'utf8')
// Extract the two prompt regions
// BUILD_FORM_PROMPT region: from "private static final String BUILD_FORM_PROMPT = " to next ;
// (Actually it's a multi-line concat ending with ";). For robustness, scan whole file.
const formPromptRegion = extractPromptRegion(content, 'BUILD_FORM_PROMPT')
const workflowPromptRegion = extractPromptRegion(content, 'BUILD_WORKFLOW_PROMPT')
// Recency bias section is in buildForm() method
const recencyBiasRegion = extractRecencyBiasRegion(content)
const results: RuleResult[] = []
for (const r of RULES) {
let region = ''
if (r.scope === 'form') region = formPromptRegion
else if (r.scope === 'workflow') region = workflowPromptRegion
else if (r.scope === 'form-recency') region = recencyBiasRegion
results.push({
id: r.id,
scope: r.scope,
description: r.description,
pass: r.pattern.test(region),
})
}
if (jsonMode) {
console.log(JSON.stringify(results, null, 2))
return
}
const passCount = results.filter(r => r.pass).length
const failCount = results.length - passCount
console.log(`\n=== Prompt Rules Coverage: ${passCount}/${results.length} pass ===\n`)
for (const r of results) {
const icon = r.pass ? '✓' : '✗'
console.log(` ${icon} [${r.scope.padEnd(13)}] ${r.id.padEnd(32)} ${r.description}`)
}
console.log('')
if (failCount > 0) {
console.error(`✗ ${failCount} rule(s) missing — review prompts`)
process.exit(1)
}
console.log('✓ All required rules present')
process.exit(0)
}
function extractPromptRegion(content: string, name: string): string {
const idx = content.indexOf(`private static final String ${name} =`)
if (idx === -1) return ''
// Scan forward until we hit the closing ";\n private" or end of class
let i = idx
let depth = 0
let inString = false
let result = ''
while (i < content.length) {
const ch = content[i]
result += ch
if (ch === '"' && content[i - 1] !== '\\') {
inString = !inString
}
if (!inString && ch === ';') {
// End of declaration; check if next non-ws is "private" or class end
const rest = content.slice(i + 1, i + 100).trim()
if (rest.startsWith('private') || rest.startsWith('@Override') || rest.startsWith('/')) {
break
}
}
i++
if (result.length > 100000) break // safety
}
return result
}
function extractRecencyBiasRegion(content: string): string {
const idx = content.indexOf('末尾重申')
if (idx === -1) return ''
// Take 2000 chars around it
return content.slice(Math.max(0, idx - 200), idx + 2000)
}
main()