feat(dacli): CI 关卡 G 加故意 FAIL 白名单 (修 hook 误报)

按 cmd-review/regression-2026-06-29 §4.4 修复 hook 误报问题.

**问题**:
- di/_validate.ts web 端明确无 formValidate 正则 (架构差异, 不是漏校验)
- 之前 hook 把 di 当作真实 FAIL 阻塞 commit, 需要 DACLI_SKIP_GATE=1 才能提交
- 浪费 CI 体验, 掩盖真正应该阻塞的问题

**修复**: 加 KNOWN_FALSE_POSITIVES 白名单机制

**新增**:
- const KNOWN_FALSE_POSITIVES: Record<cmd, reason>
  - di: web 端 di/Form.vue:172-178 明确无 formValidate 正则
- getKnownReason(cmd): 查表获取原因
- checkCommandDir/checkValidate: 白名单命中 → FAIL 转 WARN + 填充 knownReason
- 汇总新增 knownFalsePositives / realFailures 字段
- 退出码: realFailures > 0 才 FAIL, 否则根据 WARN 决定
- 输出新增 '白名单详情' 段, 标注每条已知故意 FAIL 的原因

**CI 关卡 G 输出增强**:
- status 列加 '(已知)' 标签 (白名单命中)
- 汇总新增 2 行: 白名单命中 / 真实 FAIL
- 总体: WARN (有白名单) 但不阻塞 commit
- 改进建议新增 '已知故意 FAIL (白名单)' 类别, 明确指引

**实测验证**:
- 之前: di FAIL → 退出码 1 (阻塞 commit, 需 DACLI_SKIP_GATE=1)
- 现在: di 转 WARN → 退出码 2 (不阻塞 commit, 自动通过 hook)

**扩展指南**: 添加新白名单需明确 web 端架构差异, 避免掩盖真实问题
格式: KNOWN_FALSE_POSITIVES[cmd] = 'web 端 xxx/Form.vue:XX 明确...'

测试: TS 编译干净 + 真实 FAIL=0 + exit code 2 (WARN, 不阻塞)
This commit is contained in:
ssw committed 2026-06-29 18:31:05 +08:00
1 parent 3eb31b02c2
commit f115fe1e26
1 file changed
+92 -19
+92 -19
View File
@@ -20,12 +20,17 @@
*
* 退出码:
* 0: PASS (无违规)
* 1: FAIL (有违规, 阻塞 commit)
* 1: FAIL (有违规, 阻塞 commit) — 排除已知故意 FAIL
* 2: WARN (有提示但不阻塞)
*
* 已知故意 FAIL 白名单 (v3.0+, regression audit §4.4 防御误报):
* - **di**: web 端 di/Form.vue:172-178 明确"无 formValidate 正则, 只必填+maxlength"
* (架构差异, 不是 dacli 漏校验)
*
* 用法:
* - CI: `npx tsx src/cli.ts _gate_webapi` (或通过 dacli 命令)
* - 调试: 加 --verbose 看详情
* - CI: `npx tsx src/commands/_gate_webapi.ts`
* - Hook: dacli/.git/hooks/pre-commit 自动跑
* - 跳过: DACLI_SKIP_GATE=1 环境变量 / git config dacli.gate.enabled false
*
* 注意: 这是 v3.0+ regression audit §4.4 建议的"CI 关卡 G 自动防御机制".
*/
@@ -42,6 +47,18 @@ const META_TOOLS = ['apis', 'doc'] as const
/** 已知"经验复刻"高风险目录 (web 端无对应 Form.vue, 应该标注) */
const EXPERIENCE_REPLICA_CANDIDATES = ['storage', 'integration'] as const
/**
* v3.0+ 已知故意 FAIL 白名单 (BS-47 类架构差异, 不是 dacli 漏校验)
*
* 格式: { cmd → 原因描述 }
* - 列入白名单的 FAIL 会被转为 WARN, 不阻塞 commit
* - 添加新白名单前必须明确 web 端架构差异, 避免掩盖真实问题
* - 白名单中的 FAIL 在输出中明确标注 "(已知)", 避免误读
*/
const KNOWN_FALSE_POSITIVES: Record<string, string> = {
di: 'web 端 di/Form.vue:172-178 明确"无 formValidate 正则, 只必填+maxlength" (架构差异, 不是 dacli 漏校验)',
}
interface CommandCheck {
cmd: string
fileCount: number
@@ -49,6 +66,8 @@ interface CommandCheck {
/** 0 = 元工具 / 1 = 高风险 / 2 = 正常 */
category: 'meta' | 'high-risk' | 'normal'
status: 'PASS' | 'WARN' | 'FAIL'
/** 已知故意 FAIL 原因 (白名单命中时填充) */
knownReason?: string
}
interface ValidateCheck {
@@ -59,6 +78,8 @@ interface ValidateCheck {
hasEncodeRegexp: boolean
hasEnCodeField: boolean
status: 'PASS' | 'WARN' | 'FAIL' | 'N/A'
/** 已知故意 FAIL 原因 (白名单命中时填充) */
knownReason?: string
}
interface GateReport {
@@ -70,6 +91,10 @@ interface GateReport {
passCommands: number
warnCommands: number
failCommands: number
/** 已知故意 FAIL 数 (白名单命中, 算 WARN, 不阻塞) */
knownFalsePositives: number
/** 真实 FAIL 数 (排除白名单, 阻塞 commit) */
realFailures: number
totalValidates: number
mirrorCommentRate: string
encodeRegexpRate: string
@@ -105,6 +130,11 @@ function readText(p: string): string {
}
}
/** 是否在已知故意 FAIL 白名单中 */
function getKnownReason(cmd: string): string | undefined {
return KNOWN_FALSE_POSITIVES[cmd]
}
/** 检查单个命令目录 */
function checkCommandDir(cmd: string): CommandCheck {
const dir = join(DACLI_COMMANDS_DIR, cmd)
@@ -119,6 +149,8 @@ function checkCommandDir(cmd: string): CommandCheck {
else if ((EXPERIENCE_REPLICA_CANDIDATES as readonly string[]).includes(cmd)) category = 'high-risk'
let status: CommandCheck['status'] = 'PASS'
const knownReason = getKnownReason(cmd)
if (category === 'meta' && webapiRefs > 0) {
// 元工具但有 webapi 引用 (可疑, 可能过度镜像)
status = 'WARN'
@@ -128,9 +160,11 @@ function checkCommandDir(cmd: string): CommandCheck {
} else if (category === 'normal' && webapiRefs === 0 && fileCount > 2) {
// 正常命令无 webapi 引用 (主命令无 webapi 是 P0)
status = 'FAIL'
// v3.0+ 白名单: 已知故意 FAIL 转 WARN, 不阻塞 commit
if (knownReason) status = 'WARN'
}
return { cmd, fileCount, webapiRefs, category, status }
return { cmd, fileCount, webapiRefs, category, status, knownReason }
}
/** 检查单个 _validate.ts */
@@ -161,14 +195,18 @@ function checkValidate(cmd: string): ValidateCheck {
// 4. 该文件处理 enCode 字段?
const hasEnCodeField = /\benCode\b/.test(text)
const knownReason = getKnownReason(cmd)
let status: ValidateCheck['status'] = 'PASS'
if (mirrorComments === 0 && cmd !== 'storage' && cmd !== 'integration') {
// 没有"镜像 web"注释 (除已知无 Form.vue 的命令)
// 没有"镜像 web" 注释 (除已知无 Form.vue 的命令)
status = 'WARN'
}
if (hasEnCodeField && !hasEncodeRegexp) {
// 处理 enCode 但没复用 ENCODE_REGEXP (硬编码正则)
status = 'FAIL'
// v3.0+ 白名单: 已知故意 FAIL 转 WARN, 不阻塞 commit
if (knownReason) status = 'WARN'
}
if (mirrorComments >= 4 && hasLineRef && hasEncodeRegexp) {
status = 'PASS'
@@ -182,6 +220,7 @@ function checkValidate(cmd: string): ValidateCheck {
hasEncodeRegexp,
hasEnCodeField,
status,
knownReason,
}
}
@@ -206,13 +245,20 @@ export function runWebapiGate(): GateReport {
const passCommands = commands.filter((c) => c.status === 'PASS').length
const warnCommands = commands.filter((c) => c.status === 'WARN').length
const failCommands = commands.filter((c) => c.status === 'FAIL').length
const knownFalsePositives = commands.filter((c) => c.knownReason).length
const totalValidates = validates.length
const mirrorWithComments = validates.filter((v) => v.mirrorComments > 0).length
const encodeWithRegexp = validates.filter((v) => v.hasEncodeRegexp).length
// 真实 FAIL: 排除白名单命中 (白名单命中的 FAIL 已转 WARN)
const realFailures =
commands.filter((c) => c.status === 'FAIL').length +
validates.filter((v) => v.status === 'FAIL').length
// overall: 真实 FAIL 才阻塞, 否则根据 WARN 决定
const overall: GateReport['overall'] =
failCommands > 0 || validates.some((v) => v.status === 'FAIL')
realFailures > 0
? 'FAIL'
: warnCommands > 0 || validates.some((v) => v.status === 'WARN')
? 'WARN'
@@ -220,22 +266,30 @@ export function runWebapiGate(): GateReport {
// 4. 改进建议
const recommendations: string[] = []
const failCmds = commands.filter((c) => c.status === 'FAIL')
if (failCmds.length > 0) {
const realFailCmds = commands.filter((c) => c.status === 'FAIL')
if (realFailCmds.length > 0) {
recommendations.push(
`**P0**: ${failCmds.map((c) => c.cmd).join(', ')} 无 webapi 引用 — 应走 webapi 镜像 (参考 app/list.ts 的重构模式)`,
`**P0**: ${realFailCmds.map((c) => c.cmd).join(', ')} 无 webapi 引用 — 应走 webapi 镜像 (参考 app/list.ts 的重构模式)`,
)
}
const knownFalsePositiveCmds = commands.filter((c) => c.knownReason && c.status === 'WARN')
if (knownFalsePositiveCmds.length > 0) {
recommendations.push(
`**已知故意 FAIL (白名单)**: ${knownFalsePositiveCmds.map((c) => c.cmd).join(', ')} — 不阻塞 commit, 详见 KNOWN_FALSE_POSITIVES (v3.0+ regression audit §4.4)`,
)
}
const noMirrorValidate = validates.filter((v) => v.mirrorComments === 0 && v.status === 'WARN')
if (noMirrorValidate.length > 0) {
recommendations.push(
`**P1**: ${noMirrorValidate.map((v) => v.cmd).join(', ')} _validate.ts 缺"镜像 web"注释 — 至少标注 web 端 Form.vue 镜像源`,
`**P1**: ${noMirrorValidate.map((v) => v.cmd).join(', ')} _validate.ts 缺"镜像 web" 注释 — 至少标注 web 端 Form.vue 镜像源`,
)
}
const noRegexpValidate = validates.filter((v) => v.hasEnCodeField && !v.hasEncodeRegexp && v.status === 'FAIL')
if (noRegexpValidate.length > 0) {
const realFailRegexp = validates.filter(
(v) => v.hasEnCodeField && !v.hasEncodeRegexp && v.status === 'FAIL',
)
if (realFailRegexp.length > 0) {
recommendations.push(
`**BS-56**: ${noRegexpValidate.map((v) => v.cmd).join(', ')} 用了 enCode 但未复用 ENCODE_REGEXP — 改用统一常量 (防止正则硬编码)`,
`**BS-56**: ${realFailRegexp.map((v) => v.cmd).join(', ')} 用了 enCode 但未复用 ENCODE_REGEXP — 改用统一常量 (防止正则硬编码)`,
)
}
if (recommendations.length === 0) {
@@ -251,6 +305,8 @@ export function runWebapiGate(): GateReport {
passCommands,
warnCommands,
failCommands,
knownFalsePositives,
realFailures,
totalValidates,
mirrorCommentRate: totalValidates > 0 ? `${mirrorWithComments}/${totalValidates}` : 'N/A',
encodeRegexpRate: totalValidates > 0 ? `${encodeWithRegexp}/${totalValidates}` : 'N/A',
@@ -269,10 +325,11 @@ export function printGateReport(verbose = false): number {
// 命令目录
console.log('【维度 1】命令 webapi 引用率:')
console.log(' cmd files webapi_refs category status')
console.log(' ' + '-'.repeat(70))
console.log(' ' + '-'.repeat(80))
for (const c of report.commands) {
const catStr = c.category.padEnd(12)
const statusStr = statusEmoji(c.status).padEnd(2) + c.status
const knownTag = c.knownReason ? ' (已知)' : ''
const statusStr = statusEmoji(c.status).padEnd(2) + c.status + knownTag
console.log(
` ${c.cmd.padEnd(20)}${String(c.fileCount).padEnd(7)}${String(c.webapiRefs).padEnd(13)}${catStr}${statusStr}`,
)
@@ -281,10 +338,11 @@ export function printGateReport(verbose = false): number {
// _validate.ts
console.log('\n【维度 2】_validate.ts 镜像精度:')
console.log(' cmd mirror_comments line_ref encode_regexp status')
console.log(' ' + '-'.repeat(70))
console.log(' ' + '-'.repeat(80))
for (const v of report.validates) {
if (v.status === 'N/A') continue
const statusStr = statusEmoji(v.status).padEnd(2) + v.status
const knownTag = v.knownReason ? ' (已知)' : ''
const statusStr = statusEmoji(v.status).padEnd(2) + v.status + knownTag
console.log(
` ${v.cmd.padEnd(20)}${String(v.mirrorComments).padEnd(17)}${(v.hasLineRef ? 'yes' : 'no').padEnd(10)}${(v.hasEncodeRegexp ? 'yes' : 'no').padEnd(15)}${statusStr}`,
)
@@ -294,6 +352,8 @@ export function printGateReport(verbose = false): number {
console.log('\n【汇总】')
console.log(` 命令: PASS ${report.summary.passCommands} / WARN ${report.summary.warnCommands} / FAIL ${report.summary.failCommands} / 总 ${report.summary.totalCommands}`)
console.log(` _validate.ts: 总 ${report.summary.totalValidates}, "镜像 web" 注释率 ${report.summary.mirrorCommentRate}, ENCODE_REGEXP 复用率 ${report.summary.encodeRegexpRate}`)
console.log(` 白名单命中: ${report.summary.knownFalsePositives} 个 (已知故意 FAIL, 不阻塞 commit)`)
console.log(` 真实 FAIL: ${report.summary.realFailures} 个 (排除白名单, 阻塞 commit)`)
console.log(` 总体: ${statusEmoji(report.overall)} ${report.overall}`)
// 建议
@@ -302,9 +362,22 @@ export function printGateReport(verbose = false): number {
console.log(` ${r}`)
}
// 白名单详情 (有命中时显示)
const knownItems = [
...report.commands.filter((c) => c.knownReason),
...report.validates.filter((v) => v.knownReason),
]
if (knownItems.length > 0) {
console.log('\n【白名单详情 (已知故意 FAIL)】')
for (const item of knownItems) {
console.log(` ${item.cmd}:`)
console.log(` ${item.knownReason}`)
}
}
console.log('')
// 退出码
// 退出码: 真实 FAIL 才阻塞
if (report.overall === 'FAIL') return 1
if (report.overall === 'WARN') return 2
return 0
@@ -318,4 +391,4 @@ function statusEmoji(status: string): string {
}// Run if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
process.exit(printGateReport());
}
}