BS-18 BUG 修 + BS-新62 失败兜底 + form/update 物理表同步 log (2026-07-16 实战)

## 改动

### src/commands/form/_relationForm_auto.ts (修 BS-18 BUG)
- v0.22+: modelId 用 matched.id (formId 数字) 而非 targetEnc (enCode 字符串)
- JNPF 后端期望 formId, enCode 是给人看的
- 历史: log 写 'BS-18: 自动注入 1 个 relationForm' 但实际存为 select with empty options

### src/commands/form/_smart_props.ts (加 applyHeuristicRelationForm 失败兜底)
- v0.22+: smartProps AI Login expired → BS-新62 失败兜底
- 强匹配: 字段名含 ctx.forms enCode (case-insensitive) → modelId
- 弱匹配: label 含 ctx.forms fullName
- 0 候选 → 跳过 (user 后续手工补)
- 跟 applyHeuristicRequired 并列, 不依赖 AI

### src/commands/form/create.ts
- v0.22+: form create 成功后追加 dm list 查物理表状态 log
- '✓ 物理表已建: <enCode> (N 列) — form create 走 web 端建表流程'
- 缺物理表时 warn 'autoTable 未触发或 web 端跳过'
- 集成 applyHeuristicRelationForm 到 smartProps 失败兜底分支

### src/commands/form/update.ts
- v0.22+: form update 成功后对比 --fields 新增 vModel vs 物理表实际字段
- 缺物理表列时 warn + 给修复路径 (form import --autoTable / dm drop + form create 重建 / 手工 ALTER)
- 避免 user 不知道 form update 不同步物理表

来源: jxcj M1 阶段 7 重建 2026-07-16 实战教训 + 干净子代理 testspace 验证
详见 .claude/skills/dacli-ts/references/anti-patterns §15 / form-physical-table-sync.md

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-16 12:14:42 +08:00
1 parent 18bcfda3e8
commit 27d508ac11
4 files changed
+170 -4

No files matched your search

+6 -2
View File
@@ -220,8 +220,12 @@ export async function autoDetectAndInjectRelationForm(
const relationField = candidateRelationFields[0]
// 注入 fieldRelationConfig (让 B-13 逻辑接管注入 formData 字段)
// v0.22+ (2026-07-16 jxcj M1 阶段 7 干净子代理 testspace 验证): 修 BS-18 BUG
// 历史: modelId = targetEnc (enCode 字符串), JNPF 后端期望 formId (数字 UUID)
// 症状: log 写 "BS-18: 自动注入 1 个 relationForm" 但实际存为 select with empty options
// 修复: modelId = matched.id (formId), enCode 仅用于日志
fld.fieldRelationConfig = {
modelId: targetEnc,
modelId: matched.id || targetEnc, // 优先 formId, 兜底 enCode (兼容老 code)
propsValue: vmodel,
relationField,
columnOptions: [
@@ -235,7 +239,7 @@ export async function autoDetectAndInjectRelationForm(
fld.fieldComponent = 'relationForm'
}
injected++
log.info(` BS-18: ${vmodel} → relationForm → ${targetEnc} (relationField=${relationField})`)
log.info(` BS-18: ${vmodel} → relationForm → ${targetEnc} (id=${matched.id}, relationField=${relationField})`)
}
}
+95
View File
@@ -263,6 +263,101 @@ export async function applySmartProps(
return { formData, applied, source: 'ai' }
}
// ============================================================================
// v0.22+ (2026-07-16 jxcj M1 阶段 7 实战, anti-patterns §15.1): smartProps AI 失败 fallback
// 不只填 required, 还做 input FK → relationForm 推断
// ============================================================================
/**
* 启发式 relationForm 推断 (smartProps AI 失败 / Login expired 兜底).
*
* 历史 (2026-07-16 jxcj M1 阶段 7):
* - BS-新62 (_smart_props.ts:230-241) 仅在 AI 成功时执行 input FK → relationForm
* - AI Login expired → 整段跳过, input 字段保持 input, 列表显示 ID 数字
* - applyHeuristicRequired 只填 required, 不做 jnpfKey 转换
*
* 修复: 失败兜底自己做 relationForm 推断 (不靠 AI):
* - 字段名匹配 FK 模式 (_id / _fk, 排除 f_id / f_tenant_id / f_xxx_user_id / f_flow_*) → jnpfKey=relationForm
* - 字段名含 ctx.forms enCode 强匹配 (f_competition_id 含 "competition" → competitionv2)
* 或 label 含 ctx.forms fullName 弱匹配 ("所属大赛" 含 "大赛介绍")
* - 0 候选 → 警告 (user 后续手工补)
*
* @returns { applied: count of fields converted, matched: with formId, skipped: no candidate }
*/
export function applyHeuristicRelationForm(
formData: any,
ctx: SmartPropsContext,
): { applied: number; matched: number; skipped: number } {
let fields: any[] = []
let isStringBacked = false
if (typeof formData?.fields === 'string') {
try { fields = JSON.parse(formData.fields); isStringBacked = true } catch { return { applied: 0, matched: 0, skipped: 0 } }
} else if (Array.isArray(formData?.fields)) {
fields = formData.fields
}
if (fields.length === 0) return { applied: 0, matched: 0, skipped: 0 }
// 系统字段白名单 (非关联语义, 跳过)
const SYSTEM_FIELDS = /^(f_id|f_tenant_id|f_create_user_id|f_modify_user_id|f_user_id|f_flow_id|f_flow_task_id|f_version)$/i
// FK 后缀 (input → relationForm 触发条件, 排除 _code 业务编码)
const FK_SUFFIX = /(_id|_fk)$/i
let applied = 0
let matched = 0
let skipped = 0
for (const fld of fields) {
const cfg = fld?.__config__
const vm = fld?.__vModel__ || ''
if (!cfg || !vm) continue
if (cfg.jnpfKey === 'table' || cfg.isSubTable) continue
if (SYSTEM_FIELDS.test(vm)) continue
if (cfg.jnpfKey === 'relationForm') { matched++; continue }
if (cfg.jnpfKey !== 'input' && cfg.jnpfKey !== 'inputNumber') continue
if (!FK_SUFFIX.test(vm)) continue
// 强匹配: 字段名含 ctx.forms enCode (case-insensitive)
let target = ctx.forms.find((f) => {
if (!f.enCode) return false
const encLower = f.enCode.toLowerCase()
const vmClean = vm.toLowerCase().replace(/^f_/, '').replace(/(_id|_fk)$/, '')
return vmClean.includes(encLower) || encLower.includes(vmClean)
}) || null
// 弱匹配: label 含 ctx.forms fullName
if (!target) {
const label = String(cfg.label || '')
target = ctx.forms.find((f) => {
if (!f.fullName) return false
return label.includes(f.fullName)
}) || null
}
if (!target) { skipped++; continue }
// 推断 relationField: 优先 f_<stem>_name
const stem = (target.enCode || '').replace(/^tds_/, '').replace(/^jxcj/, '')
const relationField = stem ? `f_${stem}_name` : 'f_id'
// 注入 relationForm 配置
cfg.jnpfKey = 'relationForm'
cfg.modelId = target.id
cfg.relationField = relationField
cfg.propsValue = 'f_id'
fld.columnOptions = fld.columnOptions || []
applied++
matched++
}
// 写回
if (isStringBacked) {
formData.fields = JSON.stringify(fields)
} else {
formData.fields = fields
}
if (applied > 0) {
log.info(` ↳ BS-新62 失败兜底: applyHeuristicRelationForm 转换 ${applied} 字段 (matched=${matched}, skipped=${skipped})`)
}
return { applied, matched, skipped }
}
// ============================================================================
// v0.21+ (Round 12 BS-新41): smartProps AI 失败 fallback, 用启发式推断 required
// ============================================================================
+31 -2
View File
@@ -15,7 +15,7 @@ import { homedir } from 'node:os'
import { join } from 'node:path'
import { callAiForm, printAiFormResult } from './_ai.ts'
import { initBuildFormData, buildFormData as webBuildFormData, isBuildFormDataLoaded } from './_buildformdata.ts'
import { applySmartProps, loadSmartPropsContext, applyHeuristicRequired } from './_smart_props.ts'
import { applySmartProps, loadSmartPropsContext, applyHeuristicRequired, applyHeuristicRelationForm } from './_smart_props.ts'
import { repairFormDataFromString } from './_form_repair.ts'
import { browserVerify } from './_browser_verify.ts'
import { createFormViaUi } from './_create_ui.ts'
@@ -398,13 +398,21 @@ export function buildFormCreateCommand(): Command {
// 旧: source='none' → required 全 false → 5/5 form drift (web 默认 false)
// 新: fallback 跑启发式 (label 含* / 必, vmodel 末为 id/code/key → required=true)
let heuristicApplied = { applied: 0, alreadyHad: 0 }
let relationFormApplied = { applied: 0, matched: 0, skipped: 0 }
let finalFormData = enhancedFormData
if (source === 'none') {
heuristicApplied = applyHeuristicRequired(enhancedFormData)
if (heuristicApplied.applied > 0) {
log.warn(` ↳ smartProps AI 失败 + 启发式 required 兜底: ${heuristicApplied.applied} 字段标 required=true (BS-新41)`)
} else {
log.warn(` ↳ smartProps AI 推理失败/返回空, 启发式无候选 (audit 兜底)`)
log.warn(` ↳ smartProps AI 推理失败/返回空, 启发式 required 无候选 (audit 兜底)`)
}
// v0.22+ (2026-07-16 jxcj M1 阶段 7 实战, anti-patterns §15.1): BS-新62 失败兜底
// 历史: BS-新62 (_smart_props.ts:230-241) 仅 AI 成功才执行 input FK → relationForm
// 修复: 失败兜底用 applyHeuristicRelationForm (字段名含 enCode 强匹配 + label 弱匹配)
relationFormApplied = applyHeuristicRelationForm(enhancedFormData, ctx)
if (relationFormApplied.skipped > 0) {
log.warn(` ↳ applyHeuristicRelationForm: ${relationFormApplied.skipped} FK 字段 0 候选 (user 后续手工补 modelId)`)
}
}
// 写回 formData string
@@ -1308,6 +1316,27 @@ export function buildFormCreateCommand(): Command {
}
if (rr.code === 200 || rr.code === undefined) {
log.ok('发布成功 (state=1)')
// v0.22+ (2026-07-16 jxcj M1 阶段 7 实战教训, anti-patterns §15): 透明化物理表状态
// form create 走 web 端建表流程, 物理表 schema 在 autoTable 默认 ON 时跟 formData 同步
// 但如果 user 漏 --autoTable 或 web 端校验失败, 物理表可能没建/缺列
try {
const { JnpfClient } = await import('../../core/client.ts')
const { initWebapi: initWebapiDM, loadDataModel } = await import('../../webapi/dataModel.ts')
const dc = await JnpfClient.fromToken()
await initWebapiDM(dc)
// dm 列表拿表信息 (formInfo.tables 反映 formData 推的, 不一定是真实物理表)
const dmList = (await loadDataModel.getDataModelList({ dbLinkId })) as { data?: { list?: { table: string; tableFieldList?: unknown[] }[] } }
const tbl = dmList?.data?.list?.find((t) => t.table === opts.enCode)
if (tbl) {
const colCount = Array.isArray(tbl.tableFieldList) ? tbl.tableFieldList.length : 0
log.info(` ℹ 物理表已建: ${opts.enCode} (${colCount} 列) — form create 走 web 端建表流程, autoTable 自动同步`)
} else {
log.warn(` ⚠ 物理表 ${opts.enCode} 在数据建模列表中未找到, 可能未建 (autoTable 未触发或 web 端跳过)`)
}
} catch (e) {
// dm list 失败不阻塞 (主流程已成功)
log.debug(` (物理表状态查询失败: ${(e as Error).message?.slice(0, 80)})`)
}
} else {
log.error(`发布失败 (code=${rr.code}): ${rr.msg}`)
log.error(
+38
View File
@@ -191,6 +191,44 @@ export function buildFormUpdateCommand(): Command {
}
log.ok(`✓ 表单更新成功 (id=${opts.id})`)
// v0.22+ (2026-07-16 jxcj M1 阶段 7 实战教训, anti-patterns §15): 物理表不同步警告
// form update --fields 改 formData/columnData 但不调 addTableFields, 物理表 schema 不会变
// 如果 user 加了 FK 字段 (jnpfKey=relationForm) 但物理表缺对应 varchar 列, POST 数据会报 "Field cannot be empty"
// 修复路径: form import --autoTable 重建物理表 / dm drop + form delete + form create 重建 / 手工 ALTER
if (opts.fields) {
try {
// 对比 formData 新增字段 vs 物理表实际字段
const { JnpfClient } = await import('../../core/client.ts')
const { initWebapi: initWebapiDM, loadDataModel } = await import('../../webapi/dataModel.ts')
const dc = await JnpfClient.fromToken()
await initWebapiDM(dc)
// 解析 --fields JSON 拿新增的 vModel
let newVModels: string[] = []
try {
const parsed = JSON.parse(opts.fields)
if (Array.isArray(parsed)) newVModels = parsed.map((f: any) => f.vmodel || f.__vModel__).filter(Boolean)
} catch { /* ignore parse error */ }
// form enCode 查物理表
const enCode = (orig as any).enCode
if (enCode) {
const dmList = (await loadDataModel.getDataModelList({ dbLinkId: orig.dbLinkId })) as { data?: { list?: { table: string; tableFieldList?: { field: string }[] }[] } }
const tbl = dmList?.data?.list?.find((t) => t.table === enCode)
const physicalFields = new Set((tbl?.tableFieldList || []).map((f) => f.field))
const missing = newVModels.filter((v) => !physicalFields.has(v))
if (missing.length > 0) {
log.warn(` ⚠ 物理表未同步: form 新增 ${missing.length} 字段 (${missing.join(', ')}) 不在物理表 ${enCode} 中`)
log.warn(` PUT form update 不同步物理表 schema (web 端不调 addTableFields)`)
log.warn(` 修复: form import --autoTable 重建物理表 / dm drop + form delete + form create 重建 / 手工 ALTER`)
log.warn(` 验证: dacli dm fields --table=${enCode} | grep -E "${missing.join('|')}"`)
} else if (newVModels.length > 0) {
log.info(` ℹ 物理表已包含 ${newVModels.length} 新增字段 (formData 与物理表一致)`)
}
}
} catch (e) {
log.debug(` (物理表同步检查失败: ${(e as Error).message?.slice(0, 80)})`)
}
}
// === step 4 BS-53 防护 / PUT 后自动重新发布 ===
// 5. BS-53 防护: PUT 后自动重新发布
if (opts.autoRelease) {