fix(workflow): T6 — emitVerifyFailure stdout 路由 + CommandLog + extra fields
This commit is contained in:
1 parent
a455556340
commit
5b4edbff30
3 files changed
+93
-22
No files matched your search
@@ -61,12 +61,15 @@ export interface LaunchVerifyWebapi {
|
||||
export interface VerifyFailureSink {
|
||||
/** true 时 summary 走 stdout JSON, 否则走 stderr 文本 */
|
||||
json?: boolean
|
||||
/** json 模式的回调 (默认 console.log / printJson) */
|
||||
/** json 模式的回调 (默认 console.log / printJson). 一旦提供, sink 独占 stdout/stderr 路由,
|
||||
* 函数本身不再追加 process.stdout.write, 防止双写. */
|
||||
onJson?: (summary: unknown) => void
|
||||
/** 非 json 模式的错误日志回调 */
|
||||
onLog?: (msg: string) => void
|
||||
/** stdout 末尾追加 (默认 process.stdout.write, 测试可 spy) */
|
||||
/** 无 onJson 时的 stdout 写入 (默认 process.stdout.write, 测试可 spy) */
|
||||
onStdoutWrite?: (line: string) => boolean | void
|
||||
/** 合并进 summary 的额外字段 (e.g. flowId/formId/taskId), --json 消费方依赖这些 id 关联上下游 */
|
||||
extra?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,7 +78,10 @@ export interface VerifyFailureSink {
|
||||
* 之所以不走 ctx.fail/ctx.partial:
|
||||
* - ctx.partial 不调 process.exit (silent PASS, 违反严格策略)
|
||||
* - ctx.fail 硬编码 exit(1), 不能透传 2/3 区分
|
||||
* T10 阶段 wrapCommand 全局升级, 这时再迁移到 ctx.* helper.
|
||||
* T10 阶段 wrapCommand 全局升级, 那时再迁移到 ctx.* helper.
|
||||
*
|
||||
* stdout 路由: 优先走 sink.onJson (单一权威), 没提供时 fallback 到 process.stdout.write.
|
||||
* 这样避免双写 — a455556 修复前 sink.onJson + process.stdout.write 同时触发, JSON.parse 失败.
|
||||
*
|
||||
* 测试 (vi.spyOn(process, 'exit')): 验证 2/3 透传.
|
||||
*/
|
||||
@@ -88,18 +94,19 @@ export function emitVerifyFailure(verify: LaunchVerifyResult, sink: VerifyFailur
|
||||
autoAuthorize: verify.autoAuthorize,
|
||||
diagnostics: verify.diagnostics,
|
||||
verifyExitCode: verify.exitCode,
|
||||
...sink.extra,
|
||||
}
|
||||
const line = JSON.stringify(summary)
|
||||
if (sink.json) {
|
||||
if (sink.onJson) sink.onJson(summary)
|
||||
else console.log(line)
|
||||
const onStdoutWrite = sink.onStdoutWrite ?? process.stdout.write.bind(process.stdout)
|
||||
if (sink.onJson) {
|
||||
// sink 独占 stdout/stderr 路由 (e.g. printJson 走 stdout, log.error 走 stderr)
|
||||
sink.onJson(summary)
|
||||
} else {
|
||||
const msg = `workflow launch verify 失败 (verifyExitCode=${verify.exitCode}): ${verify.diagnostics.map(d => d.message).join('; ')}`
|
||||
if (sink.onLog) sink.onLog(msg)
|
||||
else console.error(msg)
|
||||
// fallback: stdout 写一行 JSON (便于脚本捕获)
|
||||
onStdoutWrite(JSON.stringify(summary) + '\n')
|
||||
}
|
||||
if (sink.onLog) {
|
||||
sink.onLog(`workflow launch verify 失败 (verifyExitCode=${verify.exitCode}): ${verify.diagnostics.map(d => d.message).join('; ')}`)
|
||||
}
|
||||
if (sink.onStdoutWrite) sink.onStdoutWrite(line + '\n')
|
||||
else process.stdout.write(line + '\n')
|
||||
process.exit(verify.exitCode)
|
||||
}
|
||||
|
||||
|
||||
@@ -124,16 +124,39 @@ export function buildWorkflowLaunchCommand(): Command {
|
||||
log.warn(` ↳ [${d.kind}] ${d.message}`)
|
||||
}
|
||||
|
||||
// 严格策略: verify 失败直接 process.exit(verify.exitCode)
|
||||
// ctx.partial 不调 process.exit (silent PASS), ctx.fail 硬编码 exit(1); T6 用 emitVerifyFailure 透传 2/3.
|
||||
// T10 (严格策略退出码归一化) 会统一 wrapCommand 的 exit 语义, 那时再迁移到 ctx.* helper.
|
||||
// 严格策略: verify 失败 → L3 CommandLog (ctx.partial) + stdout JSON + process.exit(verify.exitCode)
|
||||
// - ctx.partial 写 L3 CommandLog (供 dacli log last 查询); 本身不 exit
|
||||
// - emitVerifyFailure 写 stdout + 调 process.exit(2|3), 透传严格策略退出码
|
||||
// - 双写而非单一出口: partial 负责结构化日志, emitVerifyFailure 负责进程退出 + stdout JSON
|
||||
// T10 (严格策略退出码归一化) 会统一 wrapCommand 的 exit 语义, 那时再合并到一个 ctx.* helper.
|
||||
if (verify.exitCode !== 0) {
|
||||
// 1. L3 CommandLog (partial 走 printCommandEnd → saveCommandLog)
|
||||
ctx.partial(
|
||||
`workflow launch verify 失败 (verifyExitCode=${verify.exitCode})`,
|
||||
{
|
||||
flowId: opts.flowId,
|
||||
formId: opts.formId,
|
||||
taskId,
|
||||
launched: verify.launched,
|
||||
actionableTaskCreated: verify.actionableTaskCreated,
|
||||
operatorId: verify.operatorId,
|
||||
autoAuthorize: verify.autoAuthorize,
|
||||
diagnostics: verify.diagnostics,
|
||||
verifyExitCode: verify.exitCode,
|
||||
},
|
||||
)
|
||||
// 2. stdout JSON + process.exit (emitVerifyFailure returns never)
|
||||
emitVerifyFailure(verify, {
|
||||
json: !!opts.json,
|
||||
onJson: opts.json ? (s) => printJson(s as Record<string, unknown>) : undefined,
|
||||
onLog: (m) => log.error(m),
|
||||
extra: {
|
||||
flowId: opts.flowId,
|
||||
formId: opts.formId,
|
||||
taskId,
|
||||
},
|
||||
})
|
||||
// emitVerifyFailure returns never, 但 TS 在 return type never 的函数后不一定能 narrow 到此分支不会返回.
|
||||
// TS narrow: emitVerifyFailure returns never, 此行 unreachable. 写 return 让 wrapCommand handler 提前 return.
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -91,9 +91,7 @@ describe('workflow launch verify → exit code propagation (T6 fix)', () => {
|
||||
stdoutSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('verify exitCode=0 → emitVerifyFailure 不调 process.exit (actionable + authorize 都 ok)', async () => {
|
||||
// 注: 当前 emitVerifyFailure 实现里 exitCode=0 时也会 process.exit(0). 这是为了一致性 (test exitCode!==0 才走此路径).
|
||||
// 这里验证: exitCode=0 时 process.exit 被调, 但传 0 (成功)
|
||||
it('exitCode=0 → emitVerifyFailure 也走 sink (sanity, launch.ts 入口守卫避免误调)', async () => {
|
||||
const webapi = { getFlowTaskOperatorList: vi.fn().mockResolvedValue([{ id: 'op1', userId: 'u1' }]) }
|
||||
const verify = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
||||
expect(verify.exitCode).toBe(0)
|
||||
@@ -103,12 +101,55 @@ describe('workflow launch verify → exit code propagation (T6 fix)', () => {
|
||||
})
|
||||
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true)
|
||||
|
||||
// emitVerifyFailure 对 exitCode=0 也会调 process.exit(0) — 但 launch.ts 入口有 if (verify.exitCode !== 0) 守卫,
|
||||
// 正常路径不会走到这里. 测 emitVerifyFailure 直接行为: exitCode=0 时也走 exit(0).
|
||||
// launch.ts 入口有 if (verify.exitCode !== 0) 守卫, 不会走到 emitVerifyFailure.
|
||||
// 但 emitVerifyFailure 直接调用: exitCode=0 时也走 sink + process.exit(0) — sanity.
|
||||
expect(() => emitVerifyFailure(verify, { json: true })).toThrow('PROCESS_EXIT_THROWN:0')
|
||||
expect(exitSpy).toHaveBeenCalledWith(0)
|
||||
|
||||
exitSpy.mockRestore()
|
||||
stdoutSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
|
||||
/**
|
||||
* stdout 不重复 (a455556 fix regression guard):
|
||||
* - sink.onJson 提供时, emitVerifyFailure 必须 独占 stdout 路由
|
||||
* - 不再额外 process.stdout.write, 否则 JSON.parse 失败 ("Extra data")
|
||||
*/
|
||||
it('onJson + extra 透传 → stdout 只一份 JSON (no duplicate)', async () => {
|
||||
const webapi = { getFlowTaskOperatorList: vi.fn().mockResolvedValue([]) }
|
||||
const verify = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
||||
expect(verify.exitCode).toBe(2)
|
||||
|
||||
let captured = ''
|
||||
const onJsonSpy = vi.fn((s: unknown) => { captured = JSON.stringify(s) })
|
||||
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
|
||||
throw new Error(`PROCESS_EXIT_THROWN:${code}`)
|
||||
})
|
||||
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation((chunk: any) => {
|
||||
captured += String(chunk)
|
||||
return true
|
||||
})
|
||||
|
||||
expect(() =>
|
||||
emitVerifyFailure(verify, {
|
||||
json: true,
|
||||
onJson: onJsonSpy,
|
||||
extra: { flowId: 'flowX', formId: 'formY', taskId: 'taskZ' },
|
||||
}),
|
||||
).toThrow('PROCESS_EXIT_THROWN:2')
|
||||
|
||||
expect(onJsonSpy).toHaveBeenCalledTimes(1)
|
||||
expect(stdoutSpy).not.toHaveBeenCalled() // sink.onJson 提供时, 不再 process.stdout.write
|
||||
|
||||
// 单次 JSON.parse 应成功 (无 "Extra data")
|
||||
expect(() => JSON.parse(captured)).not.toThrow()
|
||||
const parsed = JSON.parse(captured)
|
||||
expect(parsed.verifyExitCode).toBe(2)
|
||||
expect(parsed.flowId).toBe('flowX')
|
||||
expect(parsed.formId).toBe('formY')
|
||||
expect(parsed.taskId).toBe('taskZ')
|
||||
|
||||
exitSpy.mockRestore()
|
||||
stdoutSpy.mockRestore()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user