Files
dacli/tools/prompt-ab-static.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

219 lines
10 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* L5 — Static Prompt A/B Comparison (no LLM calls)
*
* Compares JNPF7's GEN_MODEL_QUETION (legacy v2 prompt) with dacli's
* BUILD_FORM_PROMPT (v0.22+ SSW path) on structural/capability dimensions.
*
* This is a STATIC comparison — we don't call any LLM. We extract prompt
* features (length, constraints, examples) and compare.
*
* For full A/B with LLM calls, see docs/dacli-prompt-ab-2026-07-27.md
* (run separately with `dacli form create --ai <desc>` against both prompts).
*
* Usage:
* npx tsx tools/prompt-ab-static.ts
* npx tsx tools/prompt-ab-static.ts --md > /tmp/ab.md
*/
import { readFileSync } from 'node:fs'
const JNPF_FILE = '/home/ubuntu/ccWorkDir/jnpf/jnpf7/jnpf-java-ai-core/jnpf-common-ai-core/src/main/java/jnpf/constants/AiConstants.java'
const DACLI_FILE = '/home/ubuntu/ccWorkDir/boot/ssw-common/ssw-common-biz/src/main/java/ssw/common/service/impl/AIRepairServiceImpl.java'
function extractBetweenQuotes(content: string, marker: string): string {
const idx = content.indexOf(marker)
if (idx === -1) return ''
// Skip to first quote
let i = content.indexOf('"', idx)
if (i === -1) return ''
let result = ''
i++
while (i < content.length) {
const ch = content[i]
if (ch === '"' && content[i - 1] !== '\\') break
result += ch
i++
}
return result
}
function extractJnpfPrompt(content: string, name: string): string {
// JNPF7 uses `public static final String NAME = "..." + "..." + ... ;` (concatenated multi-line)
const marker = `String ${name} =`
const idx = content.indexOf(marker)
if (idx === -1) return ''
// Scan forward: collect all string literals until final ;
let i = idx + marker.length
let result = ''
while (i < content.length) {
// Skip whitespace
while (i < content.length && /\s/.test(content[i])) i++
if (i >= content.length) break
if (content[i] === ';') break
if (content[i] !== '"') {
// skip non-string tokens like GEN_MODEL_COMPNENT
while (i < content.length && content[i] !== '"' && content[i] !== ';') i++
continue
}
// We're at a string literal start
i++ // skip opening "
while (i < content.length) {
const ch = content[i]
if (ch === '"' && content[i - 1] !== '\\') break
result += ch
i++
if (result.length > 100000) break
}
i++ // skip closing "
if (result.length > 100000) break
}
return result.replace(/\\n/g, '\n').replace(/\\t/g, '\t').replace(/\\"/g, '"')
}
// JNPF7's prompt is concatenated with "+" in Java; dacli's uses "\n" + "\n"
// For dacli, we need to extract the BUILD_FORM_PROMPT region
function extractDacliPrompt(content: string): string {
const start = content.indexOf('private static final String BUILD_FORM_PROMPT =')
if (start === -1) return ''
// Find matching ";" at end of declaration
let i = start
let depth = 0
let inString = false
let result = ''
while (i < content.length) {
const ch = content[i]
if (ch === '"' && content[i - 1] !== '\\') inString = !inString
if (!inString && ch === ';' && depth === 0) {
// Check if this is the prompt's terminator (vs inner ;)
const rest = content.slice(i + 1, i + 100).trim()
if (rest.startsWith('private') || rest.startsWith('@Override')) break
}
result += ch
i++
if (result.length > 100000) break
}
// Unescape \n
return result.replace(/\\n/g, '\n').replace(/\\t/g, '\t').replace(/\\"/g, '"')
}
interface PromptFeatures {
name: string
length: number
hasForbiddenRules: boolean
forbiddenRuleCount: number
hasWhitelist: boolean
whitelistSize: number
hasFprefixRule: boolean
hasSnakeCaseRule: boolean
hasRelationFormRule: boolean
relationFormFieldCount: number
hasOutputExample: boolean
hasRecencyBias: boolean
hasMinFieldSet: boolean
hasKeywordForbiddenList: boolean
isMainExplicit: boolean
hasFieldDbType: boolean
}
function analyze(name: string, prompt: string, hasRecencyBias: boolean): PromptFeatures {
// Count forbidden rules (look for "禁用" mentions)
const forbiddenMatches = prompt.match(/禁用\s*\S+/g) ?? []
// Whitelist: look for "白名单" or component enumeration
const whitelistMatch = prompt.match(/input[\s\S]{0,300}datePicker[\s\S]{0,300}timePicker/)
// relationForm 7 fields: modelId, propsValue, relationField, showField, hasPage, queryType, columnOptions
const relFields = ['modelId', 'propsValue', 'relationField', 'showField', 'hasPage', 'queryType', 'columnOptions']
const relFieldCount = relFields.filter(f => prompt.includes(f)).length
return {
name,
length: prompt.length,
hasForbiddenRules: forbiddenMatches.length > 0,
forbiddenRuleCount: forbiddenMatches.length,
hasWhitelist: whitelistMatch !== null || /白名单/.test(prompt),
whitelistSize: (prompt.match(/['"]?(\w+)['"]?\s*[,,]\s*/g) ?? []).length, // rough
hasFprefixRule: /f_\s*开头|f_xxx_yyy/.test(prompt),
hasSnakeCaseRule: /snake_case/.test(prompt),
hasRelationFormRule: /relationForm/.test(prompt),
relationFormFieldCount: relFieldCount,
hasOutputExample: /示例|example/.test(prompt),
hasRecencyBias,
hasMinFieldSet: /最小字段|必含/.test(prompt),
hasKeywordForbiddenList: /SQL\s*关键字|禁词|reserved|关键字/.test(prompt),
isMainExplicit: /isMain/.test(prompt),
hasFieldDbType: /fieldDbType/.test(prompt),
}
}
function main() {
const args = process.argv.slice(2)
const mdMode = args.includes('--md')
const jnpfContent = readFileSync(JNPF_FILE, 'utf8')
const dacliContent = readFileSync(DACLI_FILE, 'utf8')
// JNPF7 GEN_MODEL_QUETION
const jnpfPrompt = extractJnpfPrompt(jnpfContent, 'GEN_MODEL_QUETION')
const dacliPrompt = extractDacliPrompt(dacliContent)
// Check recency bias in dacli (user prompt section)
const hasRecencyBias = /末尾重申/.test(dacliContent)
const jnpfFeatures = analyze('JNPF7 GEN_MODEL_QUETION', jnpfPrompt, false)
const dacliFeatures = analyze('dacli BUILD_FORM_PROMPT', dacliPrompt, hasRecencyBias)
if (mdMode) {
console.log('# Prompt A/B Static Comparison\n')
console.log('| Feature | JNPF7 GEN_MODEL_QUETION | dacli BUILD_FORM_PROMPT | Winner |')
console.log('|---------|--------------------------|------------------------|--------|')
const rows: [string, any, any, string][] = [
['Length (chars)', jnpfFeatures.length, dacliFeatures.length, dacliFeatures.length > jnpfFeatures.length ? 'dacli' : 'JNPF7'],
['Forbidden rules count', jnpfFeatures.forbiddenRuleCount, dacliFeatures.forbiddenRuleCount, dacliFeatures.forbiddenRuleCount > jnpfFeatures.forbiddenRuleCount ? 'dacli' : 'JNPF7'],
['Whitelist declared', jnpfFeatures.hasWhitelist ? '✓' : '✗', dacliFeatures.hasWhitelist ? '✓' : '✗', '—'],
['f_ prefix rule', jnpfFeatures.hasFprefixRule ? '✓' : '✗', dacliFeatures.hasFprefixRule ? '✓' : '✗', 'dacli (dacli-specific)'],
['snake_case rule', jnpfFeatures.hasSnakeCaseRule ? '✓' : '✗', dacliFeatures.hasSnakeCaseRule ? '✓' : '✗', 'dacli (dacli-specific)'],
['relationForm rule', jnpfFeatures.hasRelationFormRule ? '✓' : '✗', dacliFeatures.hasRelationFormRule ? '✓' : '✗', '—'],
['relationForm 7-fields specified', `${jnpfFeatures.relationFormFieldCount}/7`, `${dacliFeatures.relationFormFieldCount}/7`, dacliFeatures.relationFormFieldCount === 7 ? 'dacli' : '—'],
['Output example', jnpfFeatures.hasOutputExample ? '✓' : '✗', dacliFeatures.hasOutputExample ? '✓' : '✗', '—'],
['Recency bias (user prompt end)', '✗', dacliFeatures.hasRecencyBias ? '✓' : '✗', 'dacli (v0.23+ tdd4 fix)'],
['Min field set', jnpfFeatures.hasMinFieldSet ? '✓' : '✗', dacliFeatures.hasMinFieldSet ? '✓' : '✗', '—'],
['Keyword forbidden list', jnpfFeatures.hasKeywordForbiddenList ? '✓' : '✗', dacliFeatures.hasKeywordForbiddenList ? '✓' : '✗', '—'],
['isMain explicit', jnpfFeatures.isMainExplicit ? '✓' : '✗', dacliFeatures.isMainExplicit ? '✓' : '✗', '—'],
['fieldDbType specified', jnpfFeatures.hasFieldDbType ? '✓' : '✗', dacliFeatures.hasFieldDbType ? '✓' : '✗', '—'],
]
for (const [feature, jnpf, dacli, winner] of rows) {
console.log(`| ${feature} | ${jnpf} | ${dacli} | ${winner} |`)
}
console.log('')
console.log('## Conclusion')
console.log('')
console.log('**dacli优势**: 7 forbidden rules + recency bias + relationForm 7-fields 全覆盖 + 字段命名规范')
console.log('**JNPF7优势**: 无 — 他们的 GEN_MODEL_QUETION 偏向轻量级示例驱动')
console.log('**注**: 这只是静态对比。完整 A/B 需要在同 base_ai 配置下用相同 10 case 测试集实际跑 LLM,对比输出。')
return
}
console.log('\n=== Static Prompt A/B Comparison ===\n')
console.log('JNPF7 GEN_MODEL_QUETION:')
console.log(` Length: ${jnpfFeatures.length} chars`)
console.log(` Forbidden rules: ${jnpfFeatures.forbiddenRuleCount}`)
console.log(` Whitelist: ${jnpfFeatures.hasWhitelist ? '✓' : '✗'}`)
console.log(` f_ prefix rule: ${jnpfFeatures.hasFprefixRule ? '✓' : '✗'}`)
console.log(` snake_case rule: ${jnpfFeatures.hasSnakeCaseRule ? '✓' : '✗'}`)
console.log(` relationForm rule: ${jnpfFeatures.hasRelationFormRule ? '✓' : '✗'}`)
console.log(` relationForm fields: ${jnpfFeatures.relationFormFieldCount}/7`)
console.log(` Recency bias: ${jnpfFeatures.hasRecencyBias ? '✓' : '✗'}`)
console.log()
console.log('dacli BUILD_FORM_PROMPT:')
console.log(` Length: ${dacliFeatures.length} chars`)
console.log(` Forbidden rules: ${dacliFeatures.forbiddenRuleCount}`)
console.log(` Whitelist: ${dacliFeatures.hasWhitelist ? '✓' : '✗'}`)
console.log(` f_ prefix rule: ${dacliFeatures.hasFprefixRule ? '✓' : '✗'}`)
console.log(` snake_case rule: ${dacliFeatures.hasSnakeCaseRule ? '✓' : '✗'}`)
console.log(` relationForm rule: ${dacliFeatures.hasRelationFormRule ? '✓' : '✗'}`)
console.log(` relationForm fields: ${dacliFeatures.relationFormFieldCount}/7`)
console.log(` Recency bias: ${dacliFeatures.hasRecencyBias ? '✓' : '✗'}`)
console.log()
console.log('Use --md flag for markdown report')
}
main()