fix(cli): 统一生产环境确认协议与预览行为

- 新增 src/cli/production.ts:--allow-prod 为唯一生产覆盖参数,
  --allow-production / --force 仅作兼容别名;JSON 阻断统一为
  PROD_OVERRIDE_REQUIRED,并携带 nextAction 与 details.operation
- workflow test 与 workflow nodes grant 统一提示文案、错误结构与
  nextAction 生成;workflow test 的 JSON 阻断不再返回空 stdout
- 预览 --dry-run 始终放行,实际执行才要求覆盖参数;nodes grant
  参数校验前置,缺少节点类型时不再先报生产阻断
- 两个命令的成功结果统一携带 productionOverride / productionWarning
- corps tag 将企业从 prod 降级为 test/demo 时输出显著警告
- 同步 CLI 帮助、CLI/workflow README 与 Skill;补充对应单测

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
giteaandClaude committed 2026-11-18 16:44:57 +08:00
1 parent 038aca10fb
commit 5be2bd5432
16 files changed
+575 -127

No files matched your search

+49 -43
View File
@@ -12,7 +12,8 @@ import { stringOption, booleanOption, numberOption } from "../../parser.js";
import { printJson, printTable, printKeyValue } from "../../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
import { createWorkflowDetailClient, extractCodeNodeSummaries, formatCodeNodeSummariesCard } from "./shared.js";
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
import { buildConfirmedCommand, isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
import { hasProductionOverride, productionGuard, productionOverrideMeta } from "../../production.js";
export async function handleWorkflowTest(
parsed: CliParsedArgs,
@@ -47,7 +48,7 @@ export async function handleWorkflowTest(
"通用选项:",
" --timeout=<ms> 设置轮询超时时间 (默认 15000ms)",
" --auto-publish 是否自动发布测试快照 (默认 false)",
" --force, -f 允许在正式生产环境 (env: prod) 下执行调试",
" --allow-prod 确认在正式生产环境 (env: prod) 下执行调试(--force 为兼容别名)",
" --dry-run 只预览,不发起调试",
" --yes/--confirm 确认执行真实工作流节点",
"",
@@ -63,36 +64,50 @@ export async function handleWorkflowTest(
const filePath = stringOption(parsed.options, ["file", "path"]);
const isEmpty = booleanOption(parsed.options, ["empty", "none"]);
const sampleOption = stringOption(parsed.options, ["sample", "sample-id", "sampleId"]);
const forceProd = booleanOption(parsed.options, ["force", "f", "allow-prod", "allow-production"]);
const allowProd = hasProductionOverride(parsed);
const isProductionCorp = Boolean(runtime.selectedCorp && isCorpProd(runtime.selectedCorp));
const warnings = ["调试会在真实环境执行工作流节点,可能改变业务数据、发送通知并生成快照;auto-publish 还可能发布流程。"];
if (isProductionCorp) {
warnings.push(`⚠️ 当前企业 [${runtime.selectedCorp?.name || runtime.selectedCorp?.id}] 为正式生产环境 (env: prod)!正式执行时需显式追加 --force 参数。`);
warnings.push(`⚠️ 当前企业 [${runtime.selectedCorp?.name || runtime.selectedCorp?.id}] 为正式生产环境 (env: prod)!正式执行时需显式追加 --allow-prod。`);
}
const nextActionCmd = isProductionCorp
? `hose-use workflow test ${pipelineId} --yes --force`
: `hose-use workflow test ${pipelineId} --yes`;
const nextActionCmd = buildConfirmedCommand(parsed, isProductionCorp ? ["--allow-prod"] : []);
const confirmExecution = () => requireMutationConfirmation(parsed, runtime, "执行工作流调试", nextActionCmd);
if (isDryRun(parsed, runtime)) {
const previewResult = mutationPreview(parsed, runtime, "执行工作流调试", pipelineId, { flowCodeOrId, rawBody, filePath, empty: isEmpty, autoPublish, timeoutMs, warnings });
if (isProductionCorp) {
if (runtime.jsonMode) {
try {
const parsedJson = JSON.parse(previewResult.stdout);
if (parsedJson.nextAction && !parsedJson.nextAction.includes("--force")) {
parsedJson.nextAction = `${parsedJson.nextAction} --force`;
previewResult.stdout = `${JSON.stringify(parsedJson, null, 2)}\n`;
}
} catch {
// ignore
}
} else {
previewResult.stdout = previewResult.stdout.replace(/--yes/g, "--yes --force");
}
}
return previewResult;
return mutationPreview(
parsed,
runtime,
"执行工作流调试",
pipelineId,
{
flowCodeOrId,
rawBody,
filePath,
empty: isEmpty,
autoPublish,
timeoutMs,
warnings,
...(isProductionCorp
? {
productionOverride: allowProd,
productionWarning: allowProd
? "生产环境覆盖已启用:实际执行会在正式生产企业产生真实业务变更。"
: "当前为正式生产企业:实际执行需追加 --allow-prod 确认。",
}
: {}),
},
{ extraArgs: isProductionCorp && !allowProd ? ["--allow-prod"] : [] }
);
}
// 生产确认只要求“用户明确知道在操作生产企业”,不做功能禁止;预览已在上面放行。
const blocked = productionGuard(parsed, runtime, {
operation: "执行工作流调试",
operationCode: "workflow.test",
risk: "调试会真实执行工作流节点,可能产生单据流转、第三方调用、通知与快照发布等不可逆业务变更。",
nextAction: nextActionCmd,
});
if (blocked) return blocked;
if (flowCodeOrId || rawBody || filePath || isEmpty) confirmExecution();
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
@@ -390,24 +405,6 @@ export async function handleWorkflowTest(
// 3. 执行端到端调试测试
confirmExecution();
if (isProductionCorp && !forceProd) {
return {
stdout: "",
stderr: [
`🛑 生产环境安全阻断 (Production Safeguard):`,
`当前工作企业 [${runtime.selectedCorp?.name || runtime.selectedCorp?.id}] 被标记为【🏭 正式生产环境】(env: prod)!`,
`在正式生产企业执行工作流调试可能产生不可逆的单据流转、第三方系统调用与真实业务变更。`,
``,
`如确需在生产环境进行带副作用的真实调试,必须显式附加 --force 参数确认生产风险:`,
` hose-use workflow test ${pipelineId} ${parsed.raw.filter((a) => !["--yes", "-y", "--dry-run"].includes(a)).slice(2).join(" ")} --yes --force`,
``,
`建议切换至测试企业进行调试:`,
` hose-use corps switch <测试企业ID>`,
].join("\n") + "\n",
exitCode: 1,
};
}
const execution = await runWorkflowDebugTest(client, pipelineId, {
flowCodeOrId: targetId,
data: customPayload,
@@ -417,7 +414,16 @@ export async function handleWorkflowTest(
autoPublish,
timeoutMs,
});
const report = { ...execution, productionEnabled: detail ? isEnabled : null, warnings };
const report = {
...execution,
productionEnabled: detail ? isEnabled : null,
warnings,
...productionOverrideMeta(
parsed,
runtime,
"生产环境覆盖已启用 (--allow-prod):本次调试在正式生产企业真实执行。"
),
};
const exitCode = report.outcome === "completed" ? 0 : 1;
if (runtime.jsonMode) {