Files
dacli/src/commands/_anti_patterns.ts
T
sswandClaude b4dce306a0 revert: 删除 auto-gen anti-patterns.md 链路 (_anti_patterns.ts 保留给 --help Pitfalls) (2026-07-23)
撤回上一轮尝试 (commit 9e21fea 之前 '加 AP-* entry + 重生成 md' 意向未落 commit 但改了文件) 涉及的反模式 auto-gen 链路.

# 之前考虑
- _anti_patterns.ts 是 13 个命令 --help Pitfalls 段的 compile-time SSOT (installHelpHookWithPatterns → enrichHelp 自动注入)
- docs/anti-patterns.md 通过 gen-anti-patterns-md.sh 从 ts 生成, 用于人类/外部 AI 读

# 撤回原因
- 重生成时 4 个手动加的内容被覆盖丢失: AP-cross-business-info-leak + AP-cross-smartprops-missing-update + BS-新132 修订 + BS-新133 修订
- 根因: AntiPattern schema (1 行 wrong / 1-2 行 right / 1-2 句 rationale) 信息密度太低, 装不下 'mitigation 多步骤 + BS 修订 + jxcj M2 验证实验数据'
- 强行加进去违反 schema 精神, 不加进去就丢内容
- 两难说明: ts→md auto-gen 对反模式这种文档不合适 (schema 跟实际信息密度不匹配)

# 此 commit
1. 删除 docs/anti-patterns.md (304 行, 人类阅读目的)
2. 删除 scripts/gen-anti-patterns-md.sh (49 行, generator)
3. 删除 scripts/extract_anti_patterns.ts (20 行, extractor)
4. _anti_patterns.ts: 文件头注释 '由 TS 自动生成 md' → 'compile-time SSOT, 不再 auto-gen md'
5. ANTI_PATTERNS 撤回到 253 行纯净版 (撤上一轮加的 'form codegen' + 'cross' CommandKey + 3 个 entry)

# 保留
- _anti_patterns.ts 本身保留 (installHelpHookWithPatterns 仍依赖 enrichHelp 注入 AP-* 给 --help 输出)
- 实测: 'dacli form create --help' 仍输出 'Pitfalls (踩过的坑):' 段
- 13 个命令 (form create/info/update/delete, di create, app context, test run, workflow create, menu create, dm create, dict typeCreate) --help 仍自动含 AP-* 反模式

# 后续
- 4 个丢失内容 (AP-cross-business-info-leak + AP-cross-smartprops-missing-update + BS-新132/133 修订) 暂未沉淀, 后续如需可入 docs/{app}/jxcj/learnings.md (应用特定) 或 ssw-loop SKILL.md (通用元规则)
- 反模式新内容优先入 _anti_patterns.ts 走 --help 自动路径 (machine-readable 优势)

Co-Authored-By: Claude <[email protected]>
2026-07-23 15:37:40 +08:00

288 lines
11 KiB
TypeScript

/**
* 反模式字典 (compile-time SSOT).
* installHelpHookWithPatterns 通过 enrichHelp() 自动把 AP-* 注入到命令 --help 的 Pitfalls 段.
* 不再 auto-gen docs/anti-patterns.md (2026-07-23 撤, schema 信息密度不适配).
*/
export interface AntiPattern {
/** 唯一 ID, 格式 AP-<command>-<n> */
id: string
/** 严重程度 */
severity: 'P0' | 'P1' | 'P2'
/** 反例 (1 行) */
wrong: string
/** 正例 (1-2 行) */
right: string
/** 根因 (1-2 句) */
rationale: string
/** 来源 (drift round / CLAUDE.md / decision number) */
source: string
/** 跨引用 (decisions 编号等) */
crossRefs?: string[]
}
export type CommandKey =
| 'form create'
| 'form update'
| 'form info'
| 'form delete'
| 'menu create'
| 'dict typeCreate'
| 'workflow create'
| 'dm create'
| 'di create'
| 'test run'
| 'app context'
/** 中央反模式字典 (按命令分组) */
export const ANTI_PATTERNS: Record<CommandKey, AntiPattern[]> = {
'form create': [
{
id: 'AP-fc-1',
severity: 'P0',
wrong: 'f_user_id (BIGINT 必填, 未指定FK关联关系)',
right: 'f_user_id (BIGINT FK 必填 relationForm 关联 user 表 显示字段 f_user_name)',
rationale: 'AI 默认推成 inputNumber, 列表显示 ID 数字而非名称',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0001-fk-relation-form-required'],
},
{
id: 'AP-fc-2',
severity: 'P0',
wrong: 'f_status (int 必填)',
right: 'f_status (int 字典 userstatus:0正常/1冻结, 必填 select 组件)',
rationale: 'int + dictType 不会自动转 select 组件 (BS-新117 限制 1)',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
},
{
id: 'AP-fc-3',
severity: 'P1',
wrong: 'f_create_time (datetime 必填)',
right: 'f_create_time (datetime 必填 datePicker 列 format=yyyy-MM-dd HH:mm:ss)',
rationale: '列缺 format 时显示 ISO 时间戳 (BS-新117 限制 2)',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
},
{
id: 'AP-fc-4',
severity: 'P0',
wrong: '1 句话建多表 (含 FK 字段)',
right: '拆步: 先建被关联表 (不含 FK), 再 form update --fields 补 FK',
rationale: '主表先建时 FK 字段无 modelId, step machine 校验失败',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0007-dacli-form-ai-not-in-dependency'],
},
{
id: 'AP-fc-5',
severity: 'P1',
wrong: '--tables 简化版 (缺 use*Permission 等关键字段)',
right: '传完整 JSON (含 usePermission/createPermission 等 16+ 字段)',
rationale: 'dacli 自动推导的 --tables 简化版会报 "Database exception"',
source: 'blindspots/BS-48',
},
{
id: 'AP-fc-6',
severity: 'P0',
wrong: 'AI 描述未提 FK 处理',
right: 'AI 描述结尾加 "本表不建FK字段"',
rationale: '物理表缺 varchar FK 列, form POST 报 "Field cannot be empty"',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0003-form-update-fields-no-alter-table'],
},
{
id: 'AP-fc-7',
severity: 'P1',
wrong: '--enCode 含 - / _ / 空格 / 括号',
right: 'camelCase 无下划线, 例: tdsmyform / tdscompetition',
rationale: 'BS-56 正则 `[一-龥a-z0-9]+` 严格, 不允许这些字符',
source: 'archive/drift-rounds-2026-07/2026-07-15-round12-fix-batch1.md §BS-新38',
},
{
id: 'AP-fc-8',
severity: 'P0',
wrong: '建表后未跑 dacli form info 验证',
right: '必跑 `dacli form info <enCode>` 验证 4 项 (字段数/jnpfKey/relationForm/required)',
rationale: 'BS-新117 4 限制入生产会报 "Field cannot be empty"',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
},
],
'form update': [
{
id: 'AP-fu-1', severity: 'P0',
wrong: 'form update --fields 加 FK 字段 (varchar)',
right: 'form import --autoTable 重建物理表 / dm drop + form delete + form create 重建',
rationale: 'form update --fields 不会 ALTER 物理表 (BS-新117 限制 4)',
source: 'archive/drift-rounds-2026-07/2026-07-15-round12-summary.md',
crossRefs: ['decisions/0003-form-update-fields-no-alter-table'],
},
{
id: 'AP-fu-2', severity: 'P1',
wrong: '--fields 字段名不带 F_ 前缀',
right: '所有字段名必带 F_ 前缀',
rationale: '列名缺 F_ 前缀, 列表显示异常',
source: 'archive/drift-rounds-2026-07/2026-07-15-round12-summary.md §column_drift',
},
{
id: 'AP-fu-3', severity: 'P2',
wrong: '--fields 缺 category 字段',
right: '必传 category (用于菜单挂载)',
rationale: '菜单找不到对应 form',
source: 'blindspots/BS-新44',
},
],
'form info': [
{
id: 'AP-fi-1', severity: 'P1',
wrong: '建表后未跑 form info 验证',
right: '必跑 `dacli form info <enCode>` 检查字段数/jnpfKey/relationForm/required 4 项',
rationale: 'BS-新117 4 限制只能通过 form info 验证, 提前发现问题',
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
},
],
'form delete': [
{
id: 'AP-fd-1', severity: 'P1',
wrong: 'form delete 不带 --cascade',
right: '删 form 前必带 --cascade 删关联菜单/物理表',
rationale: '否则留下孤立菜单和物理表',
source: 'lessons/jxcj1',
},
],
'menu create': [
{
id: 'AP-mc-1', severity: 'P0',
wrong: 'menu create --type=3 不传 moduleId',
right: '必传 --moduleId=<formId>',
rationale: '后端 400 (BS-drift-M-1)',
source: 'archive/drift-rounds-2026-07/2026-07-14-round5-menu-workflow-summary.md',
crossRefs: ['decisions/0004-menu-create-type3-moduleid'],
},
{
id: 'AP-mc-2', severity: 'P0',
wrong: 'menu create --formId + type=3',
right: '用 type=3 --urlAddress=model/<enCode> --moduleId=<formId>',
rationale: '走 Actions/Module 时 form schema 缺主表信息报 "主表不存在"',
source: 'blindspots/BS-50',
},
{
id: 'AP-mc-3', severity: 'P1',
wrong: '跨 app 引用未传 --urlAddress',
right: '传 --urlAddress=model/<formEnCode>',
rationale: 'SPA 找不到 form 路由 404',
source: 'archive/drift-rounds-2026-07/2026-07-14-round5-menu-workflow-summary.md',
},
],
'dict typeCreate': [
{
id: 'AP-dt-1', severity: 'P0',
wrong: 'dict typeCreate 不查就建',
right: '先 `dacli dict dataList` 看已有字典, 复用或加 <app>_ 前缀',
rationale: 'enCode 和 fullName 都查重 (全平台唯一, 跨 app)',
source: 'blindspots/BS-47',
crossRefs: ['decisions/0005-dict-typecreate-uniqueness'],
},
],
'workflow create': [
{
id: 'AP-wc-1', severity: 'P0',
wrong: 'workflow create --ai 不传 --startFormId',
right: '必传 --startFormId=<formId>',
rationale: '浏览器报 workFlowForm/undefined.vue',
source: 'blindspots/BS-E3',
crossRefs: ['decisions/0006-workflow-create-startformid'],
},
{
id: 'AP-wc-2', severity: 'P1',
wrong: 'workflow create --ai 不传 --flowXml',
right: '传 --flowXml=完整 BPMN XML',
rationale: 'AI 默认生 trivial 3-node BPMN, 不实用',
source: 'archive/drift-rounds-2026-07/2026-07-15-round12-fix-batch2.md §F3',
},
],
'dm create': [
{
id: 'AP-dm-1', severity: 'P0',
wrong: '凭印象写 dbLinkId',
right: '必跑 `dacli app context --json` 拿 dbLinkId',
rationale: '"Database connection failed" (BS-E14)',
source: 'blindspots/BS-E14',
},
],
'di create': [
{
id: 'AP-di-1', severity: 'P0',
wrong: 'di create 不传 --fieldJson',
right: '传 --fieldJson=<完整 schema JSON>',
rationale: '后端拿不到字段定义, 报 400',
source: 'archive/drift-rounds-2026-07/2026-07-14-round9-dict-di-drift-summary.md §BS-DI-1',
},
{
id: 'AP-di-2', severity: 'P0',
wrong: 'di create 不传 --parameterJson',
right: '传 --parameterJson=<参数 JSON>',
rationale: '后端拿不到参数定义',
source: 'archive/drift-rounds-2026-07/2026-07-14-round9-dict-di-drift-summary.md §BS-DI-2',
},
{
id: 'AP-di-3', severity: 'P0',
wrong: 'di create 不传 --dataConfigJson',
right: '传 --dataConfigJson=<数据配置 JSON>',
rationale: '后端拿不到数据源配置',
source: 'archive/drift-rounds-2026-07/2026-07-14-round9-dict-di-drift-summary.md §BS-DI-3',
},
],
'test run': [
{
id: 'AP-tr-1', severity: 'P0',
wrong: 'test run 推 sample 数据 f_xxx_id 为空',
right: '先 POST 关联表拿真实 ID, 再 POST 主表',
rationale: '含 FK 字段的 form POST 报 "Field cannot be empty"',
source: 'lessons/jxcj1',
crossRefs: ['decisions/0008-test-run-no-fk-injection'],
},
],
'app context': [
{
id: 'AP-ac-1', severity: 'P0',
wrong: 'dbLinkId 从印象写',
right: '必跑 `dacli app context --json` 拿 dbLinkId / existingEnCodes',
rationale: '"Database connection failed" 或 enCode 冲突',
source: 'lessons/jxcj1',
},
],
}
import type { DacliHelp } from '../core/help'
/** enrichHelp 返回类型 (DacliHelp with pitfalls/relatedRefs auto-populated) */
export interface DacliHelpEnriched extends DacliHelp {
pitfalls?: NonNullable<DacliHelp['pitfalls']>
relatedRefs?: NonNullable<DacliHelp['relatedRefs']>
}
/** 把 ANTI_PATTERNS 注入到 DacliHelp */
export function enrichHelp(fullCmd: string, local: DacliHelp): DacliHelpEnriched {
const patterns = ANTI_PATTERNS[fullCmd as CommandKey] ?? []
const localPitfalls = local.pitfalls ?? []
const patternPitfalls = patterns.map(p => ({
wrong: p.wrong,
right: p.right,
rationale: p.rationale,
severity: p.severity,
}))
const mergedPitfalls = [...localPitfalls, ...patternPitfalls]
const localRefs = local.relatedRefs ?? []
const patternRefs = patterns.flatMap(p => p.crossRefs ?? [])
const mergedRefs = [...new Set([...localRefs, ...patternRefs])]
return {
...local,
pitfalls: mergedPitfalls.length > 0 ? mergedPitfalls : undefined,
relatedRefs: mergedRefs.length > 0 ? mergedRefs : undefined,
}
}