auto(09213057c521): session end @ 2026-06-27T10:17:27 (+6f)

This commit is contained in:
ssw committed 2026-06-27 10:17:27 +08:00
1 parent 2dec33611f
commit 2e81214506
6 files changed
+199 -8

No files matched your search

+36
View File
@@ -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<AppCo
let forms: FormSummary[] = []
let tables: string[] = []
let menus: string[] = []
let systemExists: boolean | null | undefined = undefined
let systemMissingError: string | undefined = undefined
try {
const c = await JnpfClient.fromToken(opts?.appCode ? { appCode: opts.appCode } : {})
if (opts?.appCode) c.appCode = opts.appCode
// v0.11e+ Phase E: 早期检测 base_system 缺失, 避免后续 form/dm/menu 返间接错误
// (P0 经验: dacli app clear 后, 旧版返 'System abnormality' / 'No permission',
// 用户以为是 token 问题, 实际是 app 不存在)
if (appCode) {
systemExists = await checkSystemExists(c, appCode)
if (systemExists === false) {
const hint = formatSystemMissingHint(appCode, cached?.appName)
warnings.push(hint)
systemMissingError = hint
}
}
const f = await tryFetchForms(c)
forms = f.forms
if (f.warning) warnings.push(f.warning)
@@ -127,6 +145,8 @@ export async function loadAppContext(opts?: { appCode?: string }): Promise<AppCo
menus,
currentVersion: readCliVersion(),
warnings,
systemExists,
error: systemMissingError,
}
}
@@ -192,14 +212,28 @@ export function buildAppContextCommand(): Command {
// P1.2: 复用 loadAppContext 提取的核心采集逻辑
const ctx = await loadAppContext(opts.app ? { appCode: opts.app } : undefined)
// v0.11e+ Phase E: system 缺失时退出码 2 (AI/脚本可据此停下, 不要继续 CRUD)
// 仍输出 JSON 便于消费方诊断
const exitCode = ctx.systemExists === false ? 2 : 0
// 输出
if (opts.json) {
// 单行 JSON (AI 友好, 便于 grep/管道)
console.log(JSON.stringify(ctx))
if (exitCode !== 0) process.exit(exitCode)
return
}
// 人读友好模式
if (ctx.systemExists === false) {
log.error(`appCode=${ctx.appCode} 在 base_system 不存在`)
log.warn(` 可能原因: 该应用已被删除 (dacli app clear --app-code=${ctx.appCode}), 或 DB 还原失败`)
log.info(` 重建命令: dacli app create --appCode=${ctx.appCode} --appName="未知应用"`)
log.info(` 自动恢复: dacli app init --appCode=${ctx.appCode} --auto-recover (v0.11e+, 自动重建+重登)`)
log.dim(` ↑ 不要继续 form/menu CRUD, 会触发后端 NPE (Phase 4 复盘 2026-06-27)`)
process.exit(exitCode)
}
log.info(`app : ${ctx.appCode || '(未登录)'}`)
log.info(`dbLinkId: ${ctx.dbLinkId || '(unknown)'}`)
log.info(`token : ${ctx.tokenStatus}${ctx.tokenExpiry ? ' (expires ' + ctx.tokenExpiry + ')' : ''}`)
@@ -231,6 +265,8 @@ export function buildAppContextCommand(): Command {
'AI 新会话第一步先跑此命令',
'所有参数从此快照取, 不要猜测 (尤其 dbLinkId / enCode 唯一性 / 表存在性)',
'输出含 warnings 字段: 任一 API 失败时会填入, AI 据此判断上下文是否完整',
'**v0.11e+ Phase E**: 检测 base_system 是否含 appCode, 缺失时 systemExists=false + error 含重建命令 + 退出码 2',
' (修复盲点: dacli app clear 后旧版返间接错误 "System abnormality"/"No permission" 误导用户)',
],
})
+45 -3
View File
@@ -1,11 +1,22 @@
/**
* commands/app/login.ts — `dacli app login` (原顶层 `dacli app login`, v0.9 重命名归入 app 命名空间)
*
* v0.11e+ Phase E: 登录成功后立即验证 base_system 存在性
* 缺失时清除 token + 视为登录失败 + 退出码 1 + 提示重建命令
* 解决盲点: 旧版返 "✓ 登录成功" 但实际 app 不存在, 后续所有命令返间接错误
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { DEFAULT_ACCOUNT, DEFAULT_APP_CODE, DEFAULT_PASSWORD, getTokenPath } from '../../core/config.ts'
import {
DEFAULT_ACCOUNT,
DEFAULT_APP_CODE,
DEFAULT_PASSWORD,
clearToken,
getTokenPath,
} from '../../core/config.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log } from '../../core/log.ts'
import { checkSystemExists } from './_check_system.ts'
export function buildAppLoginCommand(): Command {
const cmd = new Command('login')
@@ -14,7 +25,14 @@ export function buildAppLoginCommand(): Command {
.option('--password <p>', '密码 (明文; 96 字符 hex 视为已加密)', DEFAULT_PASSWORD)
.option('--appCode <c>', '应用代码 (developSystem | workSystem | tds)', DEFAULT_APP_CODE)
.option('--baseUrl <u>', '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
}
}
+58 -5
View File
@@ -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
}
}
+24
View File
@@ -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({
+27
View File
@@ -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!)
+9
View File
@@ -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
}