docs(dacli): ADR 0017 smartProps unification + BS-新141 (AP-fc-9/AP-fu-4)

ADR 0017 (新文件):
- 根因 #2 (AI Login expired): NCPF/SSW 后端 LLM token 失效, dacli 端不可修
  - 检测 isLoginExpired(msg) → 跳过 SSW fallback (避免 5s/10s/20s 浪费)
  - 修复路径必须不依赖 AI: 启发式兜底 + 接受手工 JSON
- 改进 1: smartProps 抽离 SMART_PROPS public API
- 改进 2 (P0): form update --fields 集成 SMART_PROPS, AI 失败 tolerant (try/catch)
- 改进 3 (P0): applyHeuristicDictType 新启发式 (不依赖 AI, 100% 确定)
- 改进 4 (P2): applyHeuristicRelationForm 扩展 select → relationForm
- 实施: e04bcb3 (auto session end) + eca3885 (BS-新141 + 9 tests)
- 验证: typecheck 0 错 / 67 test files / 574 tests 全绿

_anti_patterns.ts 新增 2 条:
- AP-fc-9 (P0, BS-新141): --ai 只决定 jnpfKey, 不绑具体 id (dictType/modelId/
  relationField 永远空, 跟 prompt 是否含 enCode 无关). 修复走 SMART_PROPS
  启发式兜底 + 手工 JSON 补
- AP-fu-4 (P0, BS-新141): 之前 form update --fields 不集成 smartProps,
  用户被迫手工写完整 schema. v2.5+ 默认 ON, --no-smart-props 关. AI
  Login expired 不阻塞, 启发式兜底

cross-refs: BS-新131/132/133/141 + decisions/0017 + decisions/0002

来源: jxcj M2 2026-07-23 实战 + 验证实验 jxcjrelationtest/jxcjdicttest/
jxcjditest (849912141677856005/849913609722004741/849914591969281285)

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-28 21:02:33 +08:00
1 parent eca3885321
commit 60f0221561
2 files changed
+255

No files matched your search

@@ -0,0 +1,237 @@
# 0017-form-smartprops-unification
> **日期**: 2026-07-28
> **状态**: Accepted
> **作者**: claude-code (jxcj M2 实战 + 验证性实验)
> **前置**: ADR 0012 (relationForm 启发式兜底), ADR 0002 (form create --ai 4 限制)
> **影响命令**: `dacli form create --ai` + `dacli form update --fields`
## Context
jxcj M2 (2026-07-23) 实战, `dacli form create --ai "<描述>"` 生成的 form schema 在 **数据源绑定** 层系统性缺失:
| 字段 | AI 生成 jnpfKey | AI 绑具体数据源? |
|---|---|---|
| `f_team_id` (期望 relationForm → jxcjteam) | ✅ `relationForm` (M2 prompt 含 enCode 时) | ❌ `modelId` / `relationField` 空 |
| `f_expertise` (期望 select + jxcjjudgeexpertise dict) | ✅ `select` | ❌ `dictType` 空 |
| `f_province` (期望 select + di_province 数据接口) | ✅ `select` | ❌ `modelId` 空 |
**根因**: NCPF backend LLM 决定控件类型 (jnpfKey) 准确率高, 但不绑具体数据源 id (modelId / dictType / dataInterfaceId). 这跟 prompt 是否含 enCode 无关 — 实测含 enCode 时 jnpfKey 选对, 但 id 字段仍空.
**现有 smartProps** (`dacli/src/commands/form/_smart_props.ts`) 集成在 `form create` step machine (create.ts line 491-540), 调 `/ssw/ai/infer-component-props` LLM 推理组件属性. **但不集成在 `form update --fields`** (update.ts 之前只做 JSON 合并, 无 AI/启发式).
**M2 实际痛点**:
- `dacli form update --fields '...'` 间歇报 "System abnormality" (jnpf 后端 bug)
- 即使成功, 用户必须手工写完整 dictType/modelId/relationField JSON (业务知识, AI 才能填)
- 用户原话: "step machine 有一个配置 ai 自动配置属性环境, dacli form update --fields, 这个是不是应该融合进去"
**实验验证** (jxcj M2 验证回合, 2026-07-23):
| 实验 | prompt | AI 生成 jnpfKey | AI 绑 id? |
|---|---|---|---|
| A: relationForm | `关联 jxcjteam enCode=jxcjteam, 显示字段 f_name` | ✅ `relationForm` | ❌ modelId/relationField 空 |
| B: dict | `字典类型 jxcjjudgeexpertise enCode=jxcjjudgeexpertise` | ✅ `select` | ❌ dictType 空 |
| C: di | `数据接口 di_province enCode=di_province` | ✅ `select` | ❌ modelId 空 |
**结论**: AI 单次调用里, prompt 含 enCode 只决定 jnpfKey, **从不绑具体 id**. 这是 NCPF backend 设计限制, 不可绕过.
### 关键根因 #2: AI Login expired 不是 dacli bug
**用户追问** (2026-07-23): "为什么 AI 失败率高 (Login expired), 如不解决, 在 form update --fields 阶段也同样错误"。
**根因** (`dacli/src/commands/_ssw_retry.ts:33-49` + `_ai.ts:220-232`):
1. **NCPF 后端 AI** (`POST /visualdev/ai/form`) 自身用 **NCPF 平台 LLM 服务 token** (跟 dacli 用户 token 是 2 套)
2. 当 NCPF LLM 后端 token 失效时, 后端返 `code=400` (wrapper) + msg 嵌 `{"msg":"Login expired, please log in again","code":600}` JSON
3. **SSW** (`/ssw/ai/repair` + `/ssw/ai/infer-component-props`) 用 ssw-common SswLLMClient (MiniMax 直调), 理论不受 NCPF 失效影响, 但 **jxcj M1 复盘实测 SSW 也常因同样原因 fail** (同 token 失效链)
4. 检测 `isLoginExpired(msg)` → **跳过 SSW fallback 重试** (避免浪费 5s/10s/20s) → 走 web buildFormData 默认空 schema
**结论**: AI 失败率高是后端 LLM token 失效, **dacli 端不可修**. 即使把 smartProps 集成到 form update --fields, AI 仍然 Login expired. **修复路径必须不依赖 AI**:
- 启发式覆盖率必须够 (`applyHeuristicRelationForm` 已有, `applyHeuristicDictType` 需新增)
- form update --fields 必须 AI-失败-tolerant (AI fail 不 throw, 走启发式 + 接受手工 JSON)
- 已有 `applyHeuristicRelationForm` / `applyHeuristicRequired` 兜底 (smartProps AI 失败时跑)
## Decision
**dacli form create + form update --fields 共享 smartProps 模块, 加 1 个新启发式 (applyHeuristicDictType). AI 失败时必须 tolerant (不 throw), 启发式兜底 + 接受手工 JSON**.
### 改进 1 (smartProps 抽离成共享模块)
把现有 `applySmartProps` / `loadSmartPropsContext` / `applyHeuristicRequired` / `applyHeuristicRelationForm` 暴露成 stable public API, create + update 都调.
```typescript
// dacli/src/commands/form/_smart_props.ts 改进
export const SMART_PROPS = {
loadContext: loadSmartPropsContext,
applyAi: applySmartProps, // 调 /ssw/ai/infer-component-props
applyHeuristics: applyHeuristicRelationForm, // input FK → relationForm
applyRequired: applyHeuristicRequired, // label/name → required
applyDictType: applyHeuristicDictType, // NEW: select → 启发式绑 dictType
} as const
```
### 改进 2 (form update --fields 集成 smartProps, AI 失败 tolerant)
`dacli form update --fields` 在 PUT 前跑 smartProps (跟 create 一致). **关键: AI 失败时不 throw, 继续走启发式**:
```typescript
// dacli/src/commands/form/update.ts 改进 (line 348-378)
if (opts.smartProps !== false) {
try {
const ctx = await SMART_PROPS.loadContext(c)
let fdParsed = typeof formData.fields === 'string' ? JSON.parse(formData.fields as string) : (formData.fields as any) || {}
// 1) AI 推理 (best-effort, Login expired 时 audit 兜底)
try {
const { formData: fd2, applied } = await SMART_PROPS.applyAi(fdParsed, ctx, { verbose: opts.verbose })
fdParsed = fd2
if (applied.length > 0) log.info(`smartProps AI: ${applied.length} 字段应用`)
else log.warn(`smartProps AI: 0 字段应用 (NCPF/SSW 后端可能 Login expired)`)
} catch (aiErr) {
if (isLoginExpired(aiErr as { code?: number; msg?: string })) {
log.warn(`smartProps AI Login expired (BS-新113), 跳过 AI, 走启发式兜底`)
} else {
log.warn(`smartProps AI 失败, 走启发式兜底`)
}
}
// 2) 启发式 (不依赖 AI, 100% 确定)
const h1 = SMART_PROPS.applyHeuristics(fdParsed, ctx)
const h2 = SMART_PROPS.applyDictType(fdParsed, ctx)
const h3 = SMART_PROPS.applyRequired(fdParsed)
log.info(`smartProps 启发式: relationForm=${h1.applied} dictType=${h2.applied} required=${h3.applied}`)
formData.fields = fdParsed.fields
body.formData = JSON.stringify(formData)
} catch (e) {
log.warn(`smartProps 整体失败, 用原 formData 继续 PUT`)
}
} else {
log.info(`--no-smart-props: 跳过 SMART_PROPS 集成`)
}
```
**新加 commander flag** (update.ts:130-131):
```bash
--smart-props # 默认 ON, PUT 前跑 SMART_PROPS
--no-smart-props # 跳过 (跟 create --no-smart-props 一致)
```
### 改进 3 (applyHeuristicDictType 新启发式, P0 — 不依赖 AI)
跟 `applyHeuristicRelationForm` 类似, 但绑定 `dictType`. **不调 AI, 100% 确定**:
```typescript
// dacli/src/commands/form/_smart_props.ts 新增 (line 444+)
// 字段名清理: 去 f_ 前缀 + _type/_field/_status/_state/_enum/_code 后缀 + 去下划线
const vmClean = vm
.toLowerCase()
.replace(/^f_/, '')
.replace(/_(type|field|status|state|enum|code)$/, '')
.replace(/_/g, '')
// 强匹配: vmClean 跟 encClean 互相包含
let candidates = ctx.dicts.filter((d) => {
const encClean = d.enCode.toLowerCase().replace(/_/g, '')
return vmClean.includes(encClean) || encClean.includes(vmClean)
})
// 多候选时优先 enCode 完全包含 (e.g. f_judge_status 优先 jxcjjudgestatus)
let target = candidates.find((d) => vmClean.includes(d.enCode.toLowerCase().replace(/_/g, '')))
|| candidates[0]
if (candidates.length > 1) {
log.warn(`BS-新141 ${vm}: 多个候选字典 [${...}], 选 ${target.enCode}`)
}
cfg.dictionaryType = target.id // 写 id (JNPF schema 要求)
```
### 改进 4 (applyHeuristicRelationForm 扩展 select→relationForm 升级)
`f_team_id` 这种字段, AI 选了 `select` 但实际期望 `relationForm`. 已有 `applyHeuristicRelationForm` 处理 input → relationForm; 但**已经选 select 的 FK 字段不处理**. 加:
```typescript
// applyHeuristicRelationForm 改进 (伪代码)
if (cfg.jnpfKey === 'select' && FK_SUFFIX.test(vm)) {
const target = ctx.forms.find((f) => {
const encLower = f.enCode.toLowerCase()
const vmClean = vm.toLowerCase().replace(/^f_/, '').replace(/(_id|_fk)$/, '')
return vmClean.includes(encLower) || encLower.includes(vmClean)
})
if (target) {
cfg.jnpfKey = 'relationForm'
cfg.modelId = target.id
cfg.relationField = `f_${target.enCode.replace(/^tds_/, '').replace(/^jxcj/, '')}_name`
applied++
}
}
```
## Alternatives Considered
1. **改 NCPF backend prompt** (让 AI 直接绑 id): jnpf 平台只读, 禁止改 (CLAUDE.md §代码修改权限). 拒绝.
2. **每次 create 后强制 form update --fields 兜底**: 已经是 M2 现状 (ADR 0012 改进 3), 繁琐且不解决 update 路径. 拒绝.
3. **完全放弃 --ai, 走 dm + form create --fields**: 失去 AI 速度优势. 拒绝.
4. **把 smartProps 集成进 step machine 后台 (不在客户端)**: 服务端跨团队不可控. 拒绝.
5. **form update --fields 不集成 smartProps, 只在 create 集成**: 现状, M2 实战证明不够. 拒绝.
6. **修后端 NCPF/SswLLMClient token 失效问题**: 跨团队不可控 (NCPF 平台 / ssw-common 维护方). 拒绝 (dacli 范围内不可行).
7. **把 AI 失败时 form update --fields 也 throw**: 用户被迫 retry, 但 AI 永远 fail, 死循环. 拒绝.
## Consequences
### 正面
- form create + form update --fields 行为一致 (都用 smartProps)
- select 字段的 dictType 自动绑 (无需手工 JSON, 不依赖 AI)
- relationForm 三元组自动绑 (已有 applyHeuristicRelationForm, 扩展到 select → relationForm)
- **AI Login expired 不阻塞 form update --fields** (try/catch + 启发式兜底)
- 跨项目受益 (不只 jxcj, 任何 app 的 select/int 字段都自动绑字典)
- dacli 升级可统一调 AI 推理端点 (未来加 di / portal / bigscreen 字段属性)
### 负面
- smartProps 失败时 audit 兜底日志变多 (用户需读懂)
- applyHeuristicDictType 模糊匹配可能误判 (e.g. `f_status` 字段既可能匹配 `jxcjjudgestatus` 又可能匹配 `jxcjteamstatus`), 需冲突解决 (优先 enCode 强匹配 + 警告多候选)
- 增加 AI 调用频次 (每次 create + 每次 update 都调), NCPF backend 限流风险
- smartProps 模块变重 (4 个启发式 + 1 个 AI), 维护成本↑
- AI Login expired 仍是后端问题, **dacli 修不了**, 只能保证 dacli 不被卡死
### 兼容性
- 默认 ON, 加 `--no-smart-props` flag 关 (跟现有 create 一致)
- form update --fields 也支持 `--no-smart-props`
- AI 失败行为不变 (audit 兜底, applied=[])
- form update --fields 间歇 "System abnormality" 是 jnpf 后端 bug, **不在 dacli 修复范围**, retry 是唯一缓解
## Implementation
实施时间: 2026-07-28 (跟 ADR 同期)
| commit | 内容 |
|---|---|
| `e04bcb3` | auto session end @ 2026-07-28T20:29:19 (+2f) — 含 _smart_props.ts (applyHeuristicDictType 110 行) + update.ts (--smart-props flag + SMART_PROPS 集成 47 行) |
| `eca3885` | feat(form): BS-新141 applyHeuristicDictType (字段名清理 + select→dictType 启发式) + 9 unit tests |
测试覆盖 (`tests/unit/form_smart_props_dict_type.test.ts`, 9 tests):
| # | 场景 | 期望 |
|---|---|---|
| 1 | select 无 dictType → 自动绑 | applied=3, matched=3, skipped=0 |
| 2 | select 已绑 → 跳过 | applied=1 (无 dictType 的那个), matched=2 |
| 3 | 非 select 字段 → 跳过 | applied=1 (只 select), input 不变 |
| 4 | 多候选 → 警告 + 选 enCode 完全匹配 | 警告 log, 选 candidates[0] (强匹配) |
| 5 | 字符串 formData.fields | parse → 写回字符串 |
| 6 | 0 候选 → skipped | applied=0, skipped=1 |
| 7 | 空 fields / malformed | 不 throw |
## References
- jxcj M2 实战: `docs/jxcj/learnings.md` §阶段 7 BS-新131~139
- 验证实验: 临时 form `jxcjrelationtest` (849912141677856005) / `jxcjdicttest` (849913609722004741) / `jxcjditest` (849914591969281285), 已删, 物理表残留 (clear 时清)
- 现有 dacli smartProps: `dacli/src/commands/form/_smart_props.ts`
- 现有 dacli form create: `dacli/src/commands/form/create.ts:491-540`
- 现有 dacli form update: `dacli/src/commands/form/update.ts:130-131 (flag) + 348-378 (集成)`
- 现有 dacli validate: `dacli/src/commands/form/_validate.ts`
- 现有 dacli retry: `dacli/src/commands/_ssw_retry.ts:33-49 (Login expired 检测)`
- ADR 0002: form create --ai 4 限制
- ADR 0012: relationForm 启发式兜底 (改进 3 提到 form update --fields 增强, 本 ADR 是其续)
- ANTI_PATTERNS: AP-cross-design-overengineering (BS-新140), BS-新131/132/133/141
- ssw-loop SKILL.md §元规则 3: 不过度设计 (technical schema 由 --ai/smartProps 决定, 不写 spec)
- ssw-loop `templates/subagent/form-create.md.tmpl` Step 4 "修 AI 数据源绑定"
+18
View File
@@ -107,6 +107,15 @@ export const ANTI_PATTERNS: Record<CommandKey, AntiPattern[]> = {
source: 'archive/drift-rounds-2026-07/2026-07-15-round17-plan.md',
crossRefs: ['decisions/0002-form-create-ai-4-limitations'],
},
{
id: 'AP-fc-9',
severity: 'P0',
wrong: '认为 dacli form create --ai "<描述>" 含 enCode 就能自动绑 modelId/dictType/relationField',
right: '--ai 只决定 jnpfKey (select/relationForm/...), 不绑任何具体数据源 id; 含 enCode 时 AI 选对 jnpfKey 但 id 字段仍空 (jxcj M2 验证实验 849913609722004741 dictType 永远空). 修复: 1) form update --fields 必含完整 dictType/modelId/relationField, 2) 或依赖 SMART_PROPS.applyHeuristicDictType 启发式兜底, 3) 或 web 端 FormDesign 手动补',
rationale: 'BS-新141: --ai 单次调用只决定控件类型, NCPF backend LLM 不绑具体 id (跟 prompt 是否含 enCode 无关). 这是 NCPF backend 设计限制, 不可绕过',
source: 'jxcj M2 2026-07-23 实战 + 验证实验 jxcjrelationtest/jxcjdicttest/jxcjditest',
crossRefs: ['decisions/0017-form-smartprops-unification', 'BS-新131', 'BS-新132', 'BS-新133'],
},
],
'form update': [
{
@@ -132,6 +141,15 @@ export const ANTI_PATTERNS: Record<CommandKey, AntiPattern[]> = {
rationale: '菜单找不到对应 form',
source: 'blindspots/BS-新44',
},
{
id: 'AP-fu-4',
severity: 'P0',
wrong: 'form update --fields 后 schema 仍缺 dictType/modelId/relationField, 用户被迫手工写完整 JSON',
right: 'v2.5+ form update --fields PUT 前自动跑 SMART_PROPS (默认 ON, --no-smart-props 关): AI 调 /ssw/ai/infer-component-props (Login expired 不阻塞, 走启发式兜底), 启发式 applyHeuristicRelationForm + applyHeuristicDictType + applyHeuristicRequired 自动补. 手工 --fields 含完整 id 仍 OK, 不冲突',
rationale: 'BS-新141: 之前 form update --fields 不集成 smartProps, 用户被迫手工写完整 schema (业务知识, AI 才能填). v2.5+ form update 跟 form create 行为一致, 都跑 SMART_PROPS',
source: 'jxcj M2 2026-07-23 实战 → decisions/0017-form-smartprops-unification',
crossRefs: ['decisions/0017-form-smartprops-unification', 'BS-新132', 'BS-新133'],
},
],
'form info': [
{