From 2e812145065b80fd754c585e17e9fd2494bc104f Mon Sep 17 00:00:00 2001 From: ssw Date: Sat, 27 Jun 2026 10:17:27 +0800 Subject: [PATCH] auto(09213057c521): session end @ 2026-06-27T10:17:27 (+6f) --- src/commands/app/context.ts | 36 +++++++++++++++++++++ src/commands/app/login.ts | 48 +++++++++++++++++++++++++-- src/commands/app/status.ts | 63 +++++++++++++++++++++++++++++++++--- src/commands/dm/_validate.ts | 24 ++++++++++++++ src/commands/form/create.ts | 27 ++++++++++++++++ src/core/types.ts | 9 ++++++ 6 files changed, 199 insertions(+), 8 deletions(-) diff --git a/src/commands/app/context.ts b/src/commands/app/context.ts index ff37995..5a407f2 100644 --- a/src/commands/app/context.ts +++ b/src/commands/app/context.ts @@ -17,6 +17,10 @@ import { installAiHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' import { loadToken } from '../../core/config.ts' import type { AppContext, FormSummary } from '../../core/types.ts' +import { + checkSystemExists, + formatSystemMissingHint, +} from './_check_system.ts' // 复用 cli.ts 的 VERSION 常量 — 避免重复硬编码, 也避免外部依赖 (cat package.json) // 路径相对 src/commands/app/context.ts → ../../../src/cli.ts @@ -77,11 +81,25 @@ export async function loadAppContext(opts?: { appCode?: string }): Promise', '密码 (明文; 96 字符 hex 视为已加密)', DEFAULT_PASSWORD) .option('--appCode ', '应用代码 (developSystem | workSystem | tds)', DEFAULT_APP_CODE) .option('--baseUrl ', 'JNPF API 根地址 (含 /api)') - .action(async (opts: { account: string; password: string; appCode: string; baseUrl?: string }) => { + .option('--no-check-system', '跳过 base_system 存在性检测 (默认 ON, v0.11e+ Phase E)') + .action(async (opts: { + account: string + password: string + appCode: string + baseUrl?: string + checkSystem?: boolean + }) => { const c = await JnpfClient.login({ account: opts.account, password: opts.password, @@ -22,6 +40,26 @@ export function buildAppLoginCommand(): Command { baseUrl: opts.baseUrl, forceLogin: true, }) + + // v0.11e+ Phase E: 登录成功后立即验证 base_system 存在性 + // 缺失时: 清除 token (避免后续命令复用无用 token) + 提示重建 + 退出码 1 + const checkSystem = opts.checkSystem !== false // 默认 ON + if (checkSystem) { + const exists = await checkSystemExists(c, opts.appCode) + if (exists === false) { + // 清除已持久化的 token (避免污染 .dacli-token/token.json) + clearToken() + console.error('') + log.error(`登录失败: appCode=${opts.appCode} 在 base_system 不存在`) + log.warn(` 可能原因: 该应用已被删除 (dacli app clear --app-code=${opts.appCode})`) + log.info(` 重建命令: dacli app create --appCode=${opts.appCode} --appName="未知应用"`) + log.info(` 自动恢复: dacli app init --appCode=${opts.appCode} --auto-recover (v0.11e+, 自动重建+重登)`) + log.dim(` 提示: 不要先 login (会拿到无用的 token), 先重建 system`) + log.dim(` 已清除 token: ${getTokenPath()}`) + process.exit(1) + } + } + log.ok(`登录成功: ${c.account} @ ${c.appCode}`) log.dim(`baseUrl : ${c.baseUrl}`) log.dim(`token : ${c.token.slice(0, 32)}... (${c.token.length} chars)`) @@ -36,6 +74,7 @@ export function buildAppLoginCommand(): Command { '--password': '密码 (默认 123456; 已是 96 字符 hex 时跳过加密)', '--appCode': '应用代码 (默认 developSystem)', '--baseUrl': '强制指定 baseUrl, 否则按 [env JNPF_BASE_URL → http://nginx/api → http://127.0.0.1:30000/api] 依次尝试', + '--no-check-system': '跳过 base_system 存在性检测 (默认 ON)', }, example: 'dacli app login --appCode=tds', source: { web: 'web/.../auth.ts:27 loginApi', api: 'POST /api/oauth/Login' }, @@ -43,8 +82,11 @@ export function buildAppLoginCommand(): Command { 'token 7 天有效', '已加密的 96 字符 hex 不会重复加密', 'detectDbLinkId 自动从 /system/DataSource 拉业务库 id', + '**v0.11e+ Phase E**: 登录成功后默认验证 base_system 含 appCode', + ' 缺失时: 清除 token + 报登录失败 + 提示重建命令 + 退出码 1', + ' (修复盲点: 旧版返 "✓ 登录成功" 但实际 app 不存在, 浪费后续 debug 时间)', ], }) return cmd -} +} \ No newline at end of file diff --git a/src/commands/app/status.ts b/src/commands/app/status.ts index cb511a3..527c8c9 100644 --- a/src/commands/app/status.ts +++ b/src/commands/app/status.ts @@ -1,17 +1,46 @@ /** * commands/app/status.ts — `dacli app status` (原顶层 `dacli app status`, v0.9 重命名归入 app 命名空间) + * + * v0.11e+ Phase E: 加 --check-system (默认 ON) — 检测 base_system 是否含 appCode + * 解决盲点: dacli app clear 后旧版返 '未登录' / token 错误, 用户不知道 app 已删 */ import { Command } from 'commander' import { loadToken, getTokenPath, currentBaseUrl } from '../../core/config.ts' import { installAiHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' +import { JnpfClient } from '../../core/client.ts' +import { + checkSystemExists, + formatSystemMissingForHuman, +} from './_check_system.ts' export function buildAppStatusCommand(): Command { const cmd = new Command('status') - .description('显示当前登录状态 + baseUrl + appCode + dbLinkId + token 路径') + .description('显示当前登录状态 + baseUrl + appCode + dbLinkId + token 路径 + base_system 存在性') .option('--json', '以 JSON 输出 (便于脚本)', false) - .action((opts: { json?: boolean }) => { + .option('--no-check-system', '跳过 base_system 存在性检测 (默认 ON, v0.11e+ Phase E)') + .action(async (opts: { json?: boolean; checkSystem?: boolean }) => { const t = loadToken() + const checkSystem = opts.checkSystem !== false // 默认 ON + + // v0.11e+ Phase E: 检测 base_system 存在性, 缺失时返明确错误 (而非间接错误) + let systemExists: boolean | null | undefined = undefined + let systemError: string | undefined = undefined + if (checkSystem && t?.appCode) { + try { + // 复用 token 构造 client (避免重复登录) + const c = await JnpfClient.fromToken({}) + systemExists = await checkSystemExists(c, t.appCode) + if (systemExists === false) { + systemError = formatSystemMissingForHuman(t.appCode) + } + } catch (e) { + // 检测失败不致命, 仅记录 (与 init.ts:120 模式一致) + log.debug(`base_system 检测异常: ${(e as Error).message}`) + systemExists = null + } + } + if (opts.json) { console.log(JSON.stringify({ tokenPath: getTokenPath(), @@ -22,28 +51,52 @@ export function buildAppStatusCommand(): Command { dbLinkId: t?.dbLinkId, loginAt: t?.loginAt, tokenLen: t?.token.length, + systemExists, + error: systemError, }, null, 2)) + if (systemExists === false) process.exit(1) return } + if (!t) { log.warn('未登录. 先跑: dacli app login') log.dim(`baseUrl: ${currentBaseUrl()}`) log.dim(`token path: ${getTokenPath()}`) return } + log.ok(`已登录: ${t.account} @ ${t.appCode}`) log.dim(`baseUrl : ${t.baseUrl}`) log.dim(`dbLinkId : ${t.dbLinkId || '(unknown)'}`) log.dim(`loginAt : ${t.loginAt}`) log.dim(`token len : ${t.token.length}`) log.dim(`token path: ${getTokenPath()}`) + + // v0.11e+ Phase E: system 缺失时明确提示 + 退出码 1 + if (systemExists === false) { + console.error('') + console.error(systemError) + console.error('') + process.exit(1) + } + if (systemExists === null) { + log.warn(`base_system 检测失败 (API 异常), 跳过. 用 --no-check-system 关闭此提示`) + } }) installAiHelpHook(cmd, { purpose: '显示当前登录状态. 写脚本前先跑一下确认登录态', - optional: { '--json': '以 JSON 输出' }, - example: 'dacli app status', + optional: { + '--json': '以 JSON 输出', + '--no-check-system': '跳过 base_system 存在性检测 (默认 ON)', + }, + example: ['dacli app status', 'dacli app status --json'], + notes: [ + '**v0.11e+ Phase E**: 默认检测 base_system 是否含当前 appCode', + ' 缺失时返明确错误 + 重建命令, 退出码 1 (旧版返间接错误, 用户困惑)', + ' AI 看到 systemExists=false 应立即停下, 不要继续 form/menu CRUD', + ], }) return cmd -} +} \ No newline at end of file diff --git a/src/commands/dm/_validate.ts b/src/commands/dm/_validate.ts index 31e6384..addd198 100644 --- a/src/commands/dm/_validate.ts +++ b/src/commands/dm/_validate.ts @@ -104,6 +104,16 @@ const SYSTEM_COLUMNS = new Set([ 'f_description', ]) +/** B-8 (2026-06-27): 业务字段禁止 PK + * 后端 FlowFormDataUtil.createMast() getKey() 查 primaryKey=1 字段 (排除 f_tenant_id) 作为物理主键名. + * 如果用户把业务字段 (如 f_connector_code) 标 primaryKey=1: + * 1. dm 建表时该字段成为 PK (后端 metadata 表记下) + * 2. form create 提交 tables[].fields 也带这个字段 (dacli 自动补 primaryKey:0 但用户填的 1 优先) + * 3. 后端 INSERT 拼 SQL: set(keyName=业务字段).set(flowTaskId)... 遍历 mastForm 又 set(业务字段) → 重复 + * 4. SQL: insert into connector (f_connector_code, f_flow_task_id, f_connector_code, ...) → "specified twice" + * 修复: 业务字段 (非 f_id) 禁 primaryKey=1. PK 必须留给 JNPF f_id, 业务唯一性用 unique 约束或 billRule jnpfKey */ +const PRIMARY_KEY_FIELDS = new Set(['f_id']) + /** Java 关键字子集 — 镜像后端 GenerateConstant.getJavaKeyword * 完整列表见 jnpf/.../util/GenerateConstant.getJavaKeyword() */ const JAVA_KEYWORDS = new Set([ @@ -264,6 +274,20 @@ export function validateFields(opts: { }) } + // B-8 (2026-06-27 tds): 业务字段禁止 primaryKey=1 (PK 必须留给 JNPF f_id) + // 后端 FlowFormDataUtil.getKey() (L170-187) 查 primaryKey=1 字段 (排除 f_tenant_id) 作为物理 PK 名. + // 用户把 f_connector_code 标 primaryKey=1 → getKey() 返 f_connector_code → INSERT SQL 重复. + // 唯一例外: f_id (JNPF 默认 PK, dacli 自动补全). 用户业务唯一性请改用 unique 约束或 billRule jnpfKey. + if (f.primaryKey && !PRIMARY_KEY_FIELDS.has(String(f.field).toLowerCase())) { + errors.push({ + field: `fields[${i}].primaryKey`, + message: `第${row}行业务字段'${f.field}'禁止设为主键 (PK 必须留给 JNPF f_id). ` + + `如需业务唯一性请用 unique 约束或 billRule jnpfKey. (后端 FlowFormDataUtil.getKey() 选 PK → ` + + `form INSERT SQL 会把该字段既作 PK 又作字段拼两次, 报 "Column '${f.field}' specified twice")`, + code: 'B-8', + }) + } + // 镜像 L202-206 fieldName 必填 if (!f.fieldName || !String(f.fieldName).trim()) { errors.push({ diff --git a/src/commands/form/create.ts b/src/commands/form/create.ts index ea92731..000c93d 100644 --- a/src/commands/form/create.ts +++ b/src/commands/form/create.ts @@ -592,6 +592,33 @@ export function buildFormCreateCommand(): Command { }) } + // 2026-06-27 B-8 修复 (防御深度 #2): form 提交前, 过滤 tables[].fields 中与 system 字段重名的列. + // 根因 (见 docs/tds/debug.html §B-8 + 后端 FlowFormDataUtil.createMast L820-897): + // 1. 用户 --tables 标了 primaryKey=1 的业务字段 (如 f_connector_code) + // 2. 后端 getKey() (L170-187) 选 primaryKey=1 字段作为 PK 名 → f_connector_code + // 3. createMast() L846-850: INSERT 先 set(f_connector_code=id).set(f_flow_task_id=id) + // 4. L857-875 遍历 mastForm (formData.fields), 又 set(f_connector_code=value) → 重复 + // 5. SQL: insert into connector (f_connector_code, f_flow_task_id, f_connector_code, ...) → ERROR + // 防御策略: + // - 方案 D (主): dm create step machine 拦截业务字段 primaryKey=1 (见 dm/_validate.ts PRIMARY_KEY_FIELDS) + // - 方案 C (辅): form create 提交前再过滤一次, 即使用户用 web/AI 路径绕过 dm 也安全 + // SYSTEM_FIELDS: 镜像后端 L844-850 (keyName + flowTaskId) + L890 (addTenantId f_tenant_id) + L884-887 (FLOWID/FLOWSTATE) + f_description (BS-38 已知). + // 子表 tableField/relationField 不会被注入到 INSERT SQL, 不需过滤 (只是 ref 不进 columns). + if (Array.isArray(tables) && tables.length > 0) { + tables.forEach((t: any) => { + if (!t || !Array.isArray(t.fields)) return + const before = t.fields.length + t.fields = t.fields.filter((f: any) => { + if (!f || !f.field) return true + return !SYSTEM_FIELDS.has(String(f.field).toLowerCase()) + }) + const removed = before - t.fields.length + if (removed > 0) { + log.warn(`B-8 防御: 表 ${t.table} 过滤了 ${removed} 个 system 重名字段 (避免 INSERT SQL "specified twice", 后端 FlowFormDataUtil.createMast 镜像)`) + } + }) + } + // 2026-06-26 v0.11d 反思: columnList 不由 dacli 自动生成 // web ColumnDesign/src/components/Main.vue:226-771 真实流程: // columnOptions (可选字段池) ← formData.fields (不是 tables[0].fields!) diff --git a/src/core/types.ts b/src/core/types.ts index dd1fe72..a07abb2 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -101,4 +101,13 @@ export interface AppContext { currentVersion: string /** 采集过程警告 (任一 API 失败时填充, AI 据此判断上下文是否完整) */ warnings: string[] + /** base_system 是否含当前 appCode (v0.11e+ Phase E, 2026-06-27) + * - true = 系统存在 + * - false = 系统缺失 (很可能 dacli app clear 过, 或 DB 还原失败) + * - undefined = API 调用失败, 无法判断 + * AI 看到 false 应立即停下, 提示用户重建应用, 不要继续 form/menu CRUD + */ + systemExists?: boolean + /** base_system 缺失时的明确错误信息 (含重建命令, 仅 systemExists===false 时填充) */ + error?: string }