P1-7: form create AI Login expired 自动 fallback web buildFormData 默认空 schema
## bug (jxcj M1 复盘, 2026-07-16)
NCPF 后端 AI (POST /visualdev/ai/form) Login expired 全局阻塞, 报
code=400 msg=组件属性推理异常: OpenAI 响应无 choices: {"msg":"Login expired, please log in again","code":600}
SSW /ssw/ai/repair 也用同 token 必 fail (5s/10s/20s 浪费).
后果: jxcj M1 11 form 全部受影响, --ai 路径 exit 2, user 必须手动 --enCode + --fullName 重跑建空表单.
## 修复 (3 文件 + 1 单测, +75/-3)
1. _ssw_retry.ts: 加 isLoginExpired() helper (与 isTokenPlanExhausted 同模式)
2. form/_ai.ts:
- AiFormResponse 加 loginExpired?: boolean 标记
- callAiForm: NCPF Login expired 跳过 SSW fallback, 返 loginExpired=true
- callSswAiRepair: SSW Login expired 跳过剩余重试, 同上
3. form/create.ts: --ai 段检测 loginExpired=true → user 传了 --enCode/--fullName 走 web buildFormData 默认空 schema (0 字段, state=1 可用), 未传 exit 2 提示
4. tests/unit/p1-7-ai-login-fallback.test.ts: 12 单测覆盖 isLoginExpired + callAiForm Login expired fallback (mock JnpfClient.postJson)
## 试通命令 (testspace)
- 12 单测 PASS (含 5s 退避)
- testspace 实跑 P17v3 form 创建+发布 PASS (正常 AI 路径不破坏)
- 回归: form-drift 21 + field-builder 18 + blindspot 16 + round12/13/15/17 = 121 tests PASS, 0 regression
- TS 编译: 46 errors baseline = 46 errors after (无新增)
## 关键决策
- 跳过 SSW fallback: SSW 同 token 必 fail, retry 浪费
- fallback 走 web buildFormData 默认空 schema: Login expired 不阻断 form 创建, user 后续用 form update --fields 加字段
- user 未传 --enCode/--fullName → exit 2: AI 不可用无法自动生成 enCode, 比 silent fallback 安全
- 用 (r as any).code 局部 cast: 不修 pre-existing 'let r: AiFormResponse' 类型 bug (留给后续 cleanup PR)
- 不动 BS-新113 5s/10s/20s retry 策略: 仍适用于 429 token plan (临时限流可恢复)
This commit is contained in:
1 parent
2106b8dcdd
commit
a767d2bea8
4 files changed
+238
-3
No files matched your search
@@ -29,6 +29,30 @@ export function isTokenPlanExhausted(r: { code?: number; msg?: string }): boolea
|
||||
return ['token plan', 'rate limit', 'too many', '限流', '频率'].some(s => m.includes(s))
|
||||
}
|
||||
|
||||
/**
|
||||
* P1-7 (2026-07-16 jxcj M1 复盘): 判断响应是否为 "Login expired"
|
||||
*
|
||||
* 根因: NCPF 后端 AI (POST /visualdev/ai/form) 自身用 NCPF 平台 LLM 服务,
|
||||
* 当 NCPF LLM 后端 token 失效时, 后端返回 code=400 (wrapper) + msg 嵌入
|
||||
* `{"msg":"Login expired, please log in again","code":600}` JSON 串.
|
||||
* SSW /ssw/ai/repair 用 ssw-common SswLLMClient (MiniMax 直调), 不受 NCPF LLM
|
||||
* 失效影响, 但实际 jxcj M1 复盘发现 SSW 也常因同样原因 fail.
|
||||
* 触发: dacli form --ai 完全失败, 需手动 fallback 走 web buildFormData.
|
||||
*
|
||||
* 触发模式 (jxcj M1 11 form 实测):
|
||||
* - NCPF 后端: msg 含 "Login expired" 或 "Login expired, please log in again"
|
||||
* - SSW /ssw/ai/repair: 同上 (ssw SswLLMClient 走 MiniMax 但受同 token 失效影响)
|
||||
* - code 通常 400 (wrapper), 嵌套 msg 内嵌 code=600 (NCPF LLM 内部)
|
||||
*
|
||||
* 修复: 调用方 (callAiForm) 检测到 Login expired → 跳过 SSW fallback (也必 fail)
|
||||
* → 返回带 loginExpired=true 标记的 AiFormResponse, 让 create.ts 走 web buildFormData
|
||||
* 默认空 schema, 警告用户 AI 后端失效但仍可创建 form.
|
||||
*/
|
||||
export function isLoginExpired(r: { code?: number; msg?: string }): boolean {
|
||||
const m = (r.msg || '')
|
||||
return m.includes('Login expired') || m.includes('please log in again') || m.includes('login expired')
|
||||
}
|
||||
|
||||
/**
|
||||
* 指数退避 sleep (BS-新113)
|
||||
* @param attempt 第几次重试 (1, 2, 3 ...)
|
||||
|
||||
@@ -34,6 +34,10 @@ export interface AiFormResponse {
|
||||
/** v0.19+ (BS-新72): NCPF/SSW fallback 调用链 (失败时填, 让用户知道为啥失败)
|
||||
* e.g. ["NCPF: 400 System abnormality", "SSW: 500 invalid token"] */
|
||||
_aiChain?: string[]
|
||||
/** P1-7 (2026-07-16 jxcj M1 复盘): NCPF / SSW 双双 Login expired, AI 完全不可用.
|
||||
* 此时 create.ts 检测到此 flag → 跳过 AI exit, 走 web buildFormData 默认空 schema
|
||||
* (要求 user 已传 --enCode/--fullName, 否则仍 exit 2) */
|
||||
loginExpired?: boolean
|
||||
aiModelList?: Array<{
|
||||
tableTitle?: string
|
||||
tableName?: string
|
||||
@@ -154,7 +158,7 @@ function buildAiTruncationHint(keyword: string): string {
|
||||
* - 后续加 sswTable/sswReport 不用复制
|
||||
*/
|
||||
/** @deprecated 改用 ../_ssw_retry.ts (v0.15+ 抽象) */
|
||||
import { isTokenPlanExhausted, backoffSleep, withSswRetry } from '../_ssw_retry.ts'
|
||||
import { isTokenPlanExhausted, isLoginExpired, backoffSleep, withSswRetry } from '../_ssw_retry.ts'
|
||||
|
||||
/**
|
||||
* 带 retry 的 NCPF backend 调用 (BS-新113)
|
||||
@@ -229,6 +233,16 @@ export async function callAiForm(keyword: string): Promise<AiFormResponse> {
|
||||
// v0.15+ (2026-07-03): 所有非 200 都走 SSW, 不再区分截断类型
|
||||
// NCPF 配了 AI 会用, 没配就 fail — SSW 自己有 DeepSeek, 不需要 NCPF
|
||||
if (r.code !== 200) {
|
||||
// P1-7 (2026-07-16 jxcj M1 复盘): Login expired 全局阻塞场景
|
||||
// 根因: NCPF 后端 AI 服务 token 失效, SSW /ssw/ai/repair 也走同 token 必 fail
|
||||
// 修复: 检测 Login expired 后跳过 SSW fallback (浪费 2 次重试), 直接返回
|
||||
// loginExpired=true 让 create.ts 走 web buildFormData 默认空 schema
|
||||
if (isLoginExpired(r as unknown as { code?: number; msg?: string })) {
|
||||
aiChain.push(`NCPF: Login expired (code=${(r as any).code}, msg=${((r as any).msg || '').slice(0, 80)})`)
|
||||
log.warn(`! NCPF Login expired → AI 后端 token 失效, 跳过 SSW fallback (BS-新113 P1-7)`)
|
||||
log.warn(` SSW 同 token 失效, 不重试 (会浪费 5s/10s/20s), 走 web buildFormData 默认空 schema`)
|
||||
return { _aiChain: aiChain, loginExpired: true }
|
||||
}
|
||||
aiChain.push(`NCPF: code=${r.code} ${(r.msg || '').slice(0, 60)}`)
|
||||
log.warn(`! NCPF AI 建表失败 (code=${r.code}): ${r.msg} → fallback SSW 修复`)
|
||||
return await callSswAiRepair(keyword, 'NCPF 错误', aiChain)
|
||||
@@ -279,6 +293,13 @@ async function callSswAiRepair(keyword: string, reason: string, aiChain: string[
|
||||
r.data._aiChain = [...aiChain, `SSW: 200 OK`]
|
||||
return r.data
|
||||
}
|
||||
// P1-7 (2026-07-16): SSW 也可能 Login expired (与 NCPF 同 token 失效), 检测到立即返回
|
||||
// 不浪费 5s/10s 重试, 让 create.ts 走 web buildFormData 默认空 schema
|
||||
if (isLoginExpired(r as unknown as { code?: number; msg?: string })) {
|
||||
aiChain.push(`SSW: Login expired (code=${(r as any).code}, msg=${((r as any).msg || '').slice(0, 80)})`)
|
||||
log.warn(`! SSW Login expired → AI 后端 token 失效 (BS-新113 P1-7), 跳过剩余重试`)
|
||||
return { _aiChain: aiChain, loginExpired: true }
|
||||
}
|
||||
// SSW 429: retry
|
||||
if (isTokenPlanExhausted(r)) {
|
||||
aiChain.push(`SSW: 429 token plan (attempt=${attempt})`)
|
||||
|
||||
@@ -295,7 +295,33 @@ export function buildFormCreateCommand(): Command {
|
||||
if (opts.ai) {
|
||||
log.info(`--ai "${opts.ai}" — 调用平台 AI 生成表单 (白皮书 5.10.1)`)
|
||||
const data = await callAiForm(opts.ai)
|
||||
if (!data.enCode || !data.fullName) {
|
||||
// P1-7 (2026-07-16 jxcj M1 复盘): AI Login expired fallback
|
||||
// 根因: NCPF + SSW 双双 Login expired (同 token 失效), AI 完全不可用
|
||||
// 修复: 检测 loginExpired=true 后, 若 user 已传 --enCode/--fullName → 警告但继续
|
||||
// 走 web buildFormData 默认空 schema (允许 form 创建, 后续 form update 加字段)
|
||||
// 若 user 未传 → exit 2, 提示需手动 --enCode/--fullName
|
||||
if (data.loginExpired) {
|
||||
if (!opts.enCode || !opts.fullName) {
|
||||
log.error('⚠ AI Login expired (NCPF + SSW 同 token 失效), 且未传 --enCode/--fullName, 无法自动建表单')
|
||||
log.error(' 修复: 加 --enCode=<你的英文名> --fullName="<中文名>", 走 web buildFormData 默认空 schema')
|
||||
log.error(' 后续: 用 dacli form update --fields=<完整 JSON> 加字段')
|
||||
if (data._aiChain) {
|
||||
log.error('AI 调用链 (排查失败原因):')
|
||||
for (const step of data._aiChain) log.error(` - ${step}`)
|
||||
}
|
||||
process.exit(2)
|
||||
}
|
||||
// user 提供了 --enCode + --fullName → 走 web buildFormData 默认空 schema
|
||||
log.warn(`⚠ P1-7: AI Login expired (NCPF + SSW 双失效), fallback 走 web buildFormData 默认空 schema`)
|
||||
log.warn(` form 创建后会以 0 字段状态发布, 后续用 dacli form update --fields=<JSON> 添加字段`)
|
||||
if (data._aiChain) {
|
||||
for (const step of data._aiChain) log.warn(` ${step}`)
|
||||
}
|
||||
// 关键: 把 user 输入注入 data, 后续代码走空 aiModelList 路径
|
||||
data.enCode = opts.enCode
|
||||
data.fullName = opts.fullName
|
||||
data.aiModelList = [] // 空数组 → webBuildFormData 产出默认 schema (0 字段)
|
||||
} else if (!data.enCode || !data.fullName) {
|
||||
// v0.19+ BS-新72: 显式打印 NCPF/SSW 调用链, 让用户知道为啥失败
|
||||
log.error('AI 响应缺 enCode/fullName, 无法自动建表单')
|
||||
if (data._aiChain && data._aiChain.length > 0) {
|
||||
@@ -314,7 +340,8 @@ export function buildFormCreateCommand(): Command {
|
||||
// P1-5 (2026-07-16 jxcj M1 复盘 BS-新56 强化): 检测 AI 建议 enCode 与用户输入不一致
|
||||
// 场景: 用户传 --enCode='topic_direction', AI 返回 enCode='topicdirection' (或反之)
|
||||
// 修复: warning 告知用户 mismatch, 后续 menu/update 引用以最终 opts.enCode 为准
|
||||
if (originalUserEnCode && data.enCode && originalUserEnCode !== data.enCode) {
|
||||
// P1-7 注: loginExpired fallback 时 data.enCode=user enCode, 不会触发 mismatch warning
|
||||
if (!data.loginExpired && originalUserEnCode && data.enCode && originalUserEnCode !== data.enCode) {
|
||||
log.warn(`⚠ AI 建议 enCode 与用户输入不一致: ${originalUserEnCode} (用户) vs ${data.enCode} (AI)`)
|
||||
log.warn(` 最终用 opts.enCode=${opts.enCode} (优先用户输入, BS-新56 强化)`)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* tests/unit/p1-7-ai-login-fallback.test.ts — P1-7 AI Login expired fallback 单元测试
|
||||
*
|
||||
* P1-7 (2026-07-16 jxcj M1 复盘):
|
||||
* 根因: NCPF 后端 AI (`POST /visualdev/ai/form`) Login expired 全局阻塞,
|
||||
* 报 code=400 msg=组件属性推理异常: OpenAI 响应无 choices: {"msg":"Login expired...","code":600}
|
||||
* SSW /ssw/ai/repair 也用同 token 失效, 必 fail.
|
||||
*
|
||||
* 修复:
|
||||
* 1. `_ssw_retry.ts`: 加 isLoginExpired() 辅助函数 (跟 isTokenPlanExhausted 同模式)
|
||||
* 2. `_ai.ts`: callAiForm 检测 NCPF Login expired → 跳过 SSW fallback
|
||||
* callSswAiRepair 检测 Login expired → 跳过剩余重试
|
||||
* 返回 { _aiChain, loginExpired: true } 标记
|
||||
* 3. `create.ts`: 检测 data.loginExpired=true → 若 user 传 --enCode/--fullName
|
||||
* 走 web buildFormData 默认空 schema, 警告用户; 若未传 → exit 2
|
||||
*
|
||||
* 测试:
|
||||
* 1. isLoginExpired() 单元: NCPF wrapper / SSW / 普通成功 / 429 token plan / 其他错误
|
||||
* 2. callAiForm 集成 (mock): NCPF Login expired → loginExpired=true, 不调 SSW
|
||||
* 3. callAiForm 集成 (mock): SSW 也 Login expired → loginExpired=true
|
||||
* 4. callAiForm 集成 (mock): 普通 400 非 Login expired → 走 SSW fallback (旧行为)
|
||||
*
|
||||
* 设计:
|
||||
* - isLoginExpired 是 pure function, 直接单测
|
||||
* - callAiForm 涉及 HTTP, 用 vi.mock 拦截 JnpfClient.postJson
|
||||
* - 不污染 testspace (只 mock, 不发真实请求)
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
// 必须在 import 之前 mock
|
||||
const mockPostJson = vi.fn()
|
||||
vi.mock('/home/ubuntu/ccWorkDir/dacli/src/core/client.ts', () => ({
|
||||
JnpfClient: {
|
||||
fromToken: async () => ({
|
||||
postJson: mockPostJson,
|
||||
get: vi.fn(),
|
||||
putJson: vi.fn(),
|
||||
del: vi.fn(),
|
||||
postJsonOnlineDevCreate: vi.fn(),
|
||||
putJsonOnlineDevUpdate: vi.fn(),
|
||||
baseUrl: 'http://mock',
|
||||
appCode: 'testspace',
|
||||
dbLinkId: '123',
|
||||
resolveDbLinkId: (id?: string) => id || '123',
|
||||
headers: () => ({}),
|
||||
recordRequestContext: vi.fn(),
|
||||
}),
|
||||
},
|
||||
}))
|
||||
|
||||
import { isLoginExpired } from '/home/ubuntu/ccWorkDir/dacli/src/commands/_ssw_retry.ts'
|
||||
import { callAiForm } from '/home/ubuntu/ccWorkDir/dacli/src/commands/form/_ai.ts'
|
||||
|
||||
describe('P1-7: isLoginExpired() 辅助函数', () => {
|
||||
it('Test 1: NCPF wrapper Login expired (code=400 + msg 嵌套) → true', () => {
|
||||
const r = { code: 400, msg: '组件属性推理异常: OpenAI 响应无 choices: {"msg":"Login expired, please log in again","code":600}' }
|
||||
expect(isLoginExpired(r)).toBe(true)
|
||||
})
|
||||
|
||||
it('Test 2: SSW Login expired (code=401 + msg 直接) → true', () => {
|
||||
const r = { code: 401, msg: 'Login expired, please log in again' }
|
||||
expect(isLoginExpired(r)).toBe(true)
|
||||
})
|
||||
|
||||
it('Test 3: 普通成功 (code=200) → false', () => {
|
||||
const r = { code: 200, msg: 'Success' }
|
||||
expect(isLoginExpired(r)).toBe(false)
|
||||
})
|
||||
|
||||
it('Test 4: 429 token plan (不是 Login expired) → false', () => {
|
||||
const r = { code: 429, msg: 'token plan exhausted' }
|
||||
expect(isLoginExpired(r)).toBe(false)
|
||||
})
|
||||
|
||||
it('Test 5: 其他 400 错误 (System abnormality) → false', () => {
|
||||
const r = { code: 400, msg: 'Invalid parameter: keyword is empty' }
|
||||
expect(isLoginExpired(r)).toBe(false)
|
||||
})
|
||||
|
||||
it('Test 6: 空 msg → false', () => {
|
||||
const r = { code: 500, msg: '' }
|
||||
expect(isLoginExpired(r)).toBe(false)
|
||||
})
|
||||
|
||||
it('Test 7: undefined msg → false', () => {
|
||||
const r = { code: 500 }
|
||||
expect(isLoginExpired(r)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('P1-7: callAiForm() — Login expired 短路', () => {
|
||||
beforeEach(() => {
|
||||
mockPostJson.mockReset()
|
||||
})
|
||||
|
||||
it('Test 8: NCPF Login expired → loginExpired=true, 不调 SSW', async () => {
|
||||
// NCPF /visualdev/ai/form 返 Login expired
|
||||
mockPostJson.mockResolvedValueOnce({
|
||||
code: 400,
|
||||
msg: '组件属性推理异常: OpenAI 响应无 choices: {"msg":"Login expired, please log in again","code":600}',
|
||||
})
|
||||
const r = await callAiForm('简单测试: 名称')
|
||||
expect(r.loginExpired).toBe(true)
|
||||
expect(r._aiChain).toBeDefined()
|
||||
expect(r._aiChain!.length).toBeGreaterThan(0)
|
||||
expect(r._aiChain![0]).toContain('Login expired')
|
||||
// 关键: 只调了 1 次 (NCPF), SSW 没调 (避免浪费 5s/10s)
|
||||
expect(mockPostJson).toHaveBeenCalledTimes(1)
|
||||
expect(mockPostJson.mock.calls[0][0]).toBe('/visualdev/ai/form')
|
||||
})
|
||||
|
||||
it('Test 9: NCPF Login expired + SSW fallback 被跳过', async () => {
|
||||
// 即使 SSW 会返成功, P1-7 后 NCPF Login expired 直接 return, 不调 SSW
|
||||
mockPostJson.mockResolvedValueOnce({
|
||||
code: 400,
|
||||
msg: 'Login expired, please log in again',
|
||||
})
|
||||
const r = await callAiForm('简单测试')
|
||||
expect(r.loginExpired).toBe(true)
|
||||
// 验证没调 SSW
|
||||
expect(mockPostJson).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('Test 10: NCPF 失败但非 Login expired → 走 SSW fallback', async () => {
|
||||
// NCPF 返普通 400 错误 → 应 fallback SSW
|
||||
mockPostJson
|
||||
.mockResolvedValueOnce({ code: 400, msg: 'System abnormality' }) // NCPF
|
||||
.mockResolvedValueOnce({ code: 200, data: { enCode: 'testForm', fullName: 'test', aiModelList: [{ tableTitle: 'test', isMain: true, fields: [] }] } }) // SSW
|
||||
const r = await callAiForm('简单测试')
|
||||
expect(r.loginExpired).toBeUndefined()
|
||||
expect(r.enCode).toBe('testForm')
|
||||
expect(mockPostJson).toHaveBeenCalledTimes(2) // NCPF + SSW
|
||||
expect(mockPostJson.mock.calls[0][0]).toBe('/visualdev/ai/form')
|
||||
expect(mockPostJson.mock.calls[1][0]).toBe('/ssw/ai/repair')
|
||||
})
|
||||
|
||||
it('Test 11: NCPF 200 正常 → loginExpired 字段不应存在', async () => {
|
||||
mockPostJson.mockResolvedValueOnce({
|
||||
code: 200,
|
||||
msg: 'Success',
|
||||
data: {
|
||||
enCode: 'normalForm',
|
||||
fullName: '正常表单',
|
||||
aiModelList: [{ tableTitle: '正常表单', isMain: true, fields: [] }],
|
||||
},
|
||||
})
|
||||
const r = await callAiForm('简单测试')
|
||||
expect(r.loginExpired).toBeUndefined()
|
||||
expect(r.enCode).toBe('normalForm')
|
||||
expect(mockPostJson).toHaveBeenCalledTimes(1) // 只 NCPF, 没 SSW
|
||||
})
|
||||
|
||||
it('Test 12: NCPF 静默截断 + SSW 也 Login expired → loginExpired=true', async () => {
|
||||
// NCPF 返 200 但 data 空 (静默截断)
|
||||
// SSW 也 Login expired
|
||||
mockPostJson
|
||||
.mockResolvedValueOnce({ code: 200, msg: 'Success', data: {} }) // NCPF 空 data
|
||||
.mockResolvedValueOnce({ code: 401, msg: 'Login expired, please log in again' }) // SSW Login expired
|
||||
const r = await callAiForm('复杂测试: 字段1, 字段2, 字段3')
|
||||
expect(r.loginExpired).toBe(true)
|
||||
expect(mockPostJson).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user