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

+50 -54
View File
@@ -3,7 +3,8 @@ import { GRAY_NODES_CATALOG, normalizeGrayNodes } from "../../../domains/platfor
import { isCorpProd } from "../../../core/config/types.js";
import { booleanOption, stringOption } from "../../parser.js";
import { printJson, printTable, printKeyValue } from "../../printer.js";
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
import { buildConfirmedCommand, isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
import { hasProductionOverride, productionGuard, productionOverrideMeta } from "../../production.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
import { createPiperClientForCorp } from "../../helpers.js";
@@ -113,40 +114,6 @@ export async function handleGrantNodes(
runtime: CliRuntime
): Promise<CliResult> {
const corp = runtime.selectedCorp!;
const isProd = isCorpProd(corp);
const allowProd = booleanOption(parsed.options, ["allow-prod", "allow-production"]);
const prodNextAction = "hose-use workflow nodes grant <type...> --allow-prod --yes";
// 生产环境默认硬拦截:灰度节点开通属于不可逆企业级写操作。
// 逃生口必须显式追加 --allow-prod,且仍受 --dry-run / --yes 两阶段确认约束。
if (isProd && !allowProd) {
return {
stdout: runtime.jsonMode
? printJson({
error: true,
code: "PROD_GRANT_BLOCKED",
message: "对正式生产企业开通灰度节点需显式追加 --allow-prod",
exitCode: 1,
nextAction: prodNextAction,
}) + "\n"
: "",
stderr: runtime.jsonMode
? ""
: [
"🛑 生产环境安全阻断 (Production Safeguard):",
`当前工作企业 [${corp.name || corp.id}] 被判定为【正式生产环境】。`,
"灰度节点开通是不可逆的企业级写操作,默认禁止对生产企业执行。",
"",
"如确需在生产环境开通,必须显式追加 --allow-prod 承担风险,并先预览再执行:",
" hose-use workflow nodes grant <type...> --allow-prod --dry-run",
` ${prodNextAction}`,
"",
"若企业环境标记有误,请检查 ~/.config/hose-use/enterprises/ 下的 env / isTest 字段。",
].join("\n") + "\n",
exitCode: 1,
};
}
const rawNodes = parsed.args.slice(1);
const optionNodes = stringOption(parsed.options, ["nodes", "node", "type", "types"]);
const collected: string[] = [];
@@ -170,27 +137,51 @@ export async function handleGrantNodes(
};
}
const isProd = isCorpProd(corp);
const allowProd = hasProductionOverride(parsed);
const isTest = booleanOption(parsed.options, ["is-test"]);
const confirmNextAction = buildConfirmedCommand(parsed);
const prodNextAction = buildConfirmedCommand(parsed, ["--allow-prod"]);
// 预览不产生写操作,始终放行;生产环境只在 nextAction 中补上覆盖参数。
if (isDryRun(parsed, runtime)) {
return mutationPreview(
parsed,
runtime,
"开通企业工作流灰度节点",
corp.corpId || corp.credentials.corpId || corp.id,
{
nodes: normalizeGrayNodes(targetNodes),
isTest,
attachedOptions: GRANT_ATTACHED_OPTIONS,
adminPasswordRequired: Boolean(process.env.HOSE_ADMIN_PASSWORD),
...(isProd
? {
productionOverride: allowProd,
productionWarning: allowProd
? "生产环境覆盖已启用:实际执行会变更正式生产企业,请再次核对企业与节点类型。"
: "当前为正式生产企业:实际执行需追加 --allow-prod 确认。",
}
: {}),
},
{ extraArgs: isProd && !allowProd ? ["--allow-prod"] : [] }
);
}
const blocked = productionGuard(parsed, runtime, {
operation: "开通企业工作流灰度节点",
operationCode: "workflow.nodes.grant",
risk: "灰度节点开通是不可逆的企业级写操作,会改变正式生产企业的可用节点能力。",
nextAction: prodNextAction,
});
if (blocked) return blocked;
requireMutationConfirmation(
parsed,
runtime,
"开通企业工作流灰度节点",
isProd ? prodNextAction : "hose-use workflow nodes grant <type...> --yes"
isProd ? prodNextAction : confirmNextAction
);
const isTest = booleanOption(parsed.options, ["is-test"]);
if (isDryRun(parsed, runtime)) {
return mutationPreview(parsed, runtime, "开通企业工作流灰度节点", corp.corpId || corp.credentials.corpId || corp.id, {
nodes: normalizeGrayNodes(targetNodes),
isTest,
attachedOptions: GRANT_ATTACHED_OPTIONS,
adminPasswordRequired: Boolean(process.env.HOSE_ADMIN_PASSWORD),
...(isProd
? {
productionOverride: true,
productionWarning: "生产环境逃生口已启用:实际执行会变更正式生产企业,请再次核对企业与节点类型。",
}
: {}),
});
}
const password = stringOption(parsed.options, ["password", "pwd", "admin-key"]);
@@ -200,9 +191,14 @@ export async function handleGrantNodes(
isTest,
});
const overrideMeta = productionOverrideMeta(
parsed,
runtime,
"生产环境覆盖已启用 (--allow-prod):本次对正式生产企业执行了灰度节点开通。"
);
if (runtime.jsonMode) {
return {
stdout: printJson(isProd ? { ...result, productionOverride: true } : result, runtime.outputOptions),
stdout: printJson({ ...result, ...overrideMeta }, runtime.outputOptions),
stderr: "",
exitCode: result.success ? 0 : 1,
};
@@ -212,8 +208,8 @@ export async function handleGrantNodes(
}
const lines = [
...(isProd
? ["⚠️ 生产环境逃生口已启用 (--allow-prod):本次对正式生产企业执行了灰度节点开通。", ""]
...(overrideMeta.productionOverride
? [`⚠️ ${overrideMeta.productionWarning}`, ""]
: []),
`====== 灰度节点开通结果 [${corp.name}] ======`,
printKeyValue({
+3 -3
View File
@@ -88,15 +88,15 @@ export async function handleNodeCommand(
"",
"子命令 (企业灰度节点级别):",
" nodes [list] [--all] 列出当前企业已开通的节点字典 (别名: probe, catalog)",
" nodes grant <type...> [--is-test] [--allow-prod] 为当前企业开通灰度节点(生产环境需显式 --allow-prod)",
" nodes grant <type...> [--is-test] [--allow-prod] 为当前企业开通灰度节点(生产企业执行需显式 --allow-prod)",
"",
"示例:",
" hose-use workflow nodes",
" hose-use workflow nodes --all",
" hose-use workflow nodes grant code-js2",
" hose-use workflow nodes grant code-js2 --is-test # 仅用于工作流位于测试 Piper 的企业",
" hose-use workflow nodes grant code-js2 --allow-prod --dry-run # 生产企业:先预览",
" hose-use workflow nodes grant code-js2 --allow-prod --yes # 生产企业:确认执行",
" hose-use workflow nodes grant code-js2 --dry-run # 生产企业:预览(无需覆盖参数)",
" hose-use workflow nodes grant code-js2 --allow-prod --yes # 生产企业:确认执行(--force 兼容)",
" hose-use workflow node list 896398895352487950",
].join("\n") + "\n",
exitCode: 1,
+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) {