feat(workflow): 灰度节点开通增加生产环境受控逃生口 --allow-prod

- 生产企业默认仍硬拦截;显式追加 --allow-prod 后才放行,且继续遵守
  --dry-run 预览与 --yes 确认的两阶段门禁;
- 拦截提示与 JSON 错误 nextAction 都给出 --allow-prod 的恢复命令;
  dry-run 预览和成功结果标注 productionOverride 与生产覆盖警告;
- 同步 CLI 帮助、hose-use Skill、workflow 域 README 与 CLI 分层说明;
- 新增单测覆盖生产预览、缺少 --yes、文本执行与 JSON 结果四种路径。

校验:bun test 1159 pass / 0 fail;bun run check 通过;
check:readme 报告的 specs/ 与 src/core/db/ 缺失为仓库既有问题,与本次改动无关。
This commit is contained in:
gitea committed 2026-11-18 15:25:33 +08:00
1 parent 235e4b806d
commit 8b0346221f
8 files changed
+142 -30

No files matched your search

+1 -1
View File
@@ -14,7 +14,7 @@
- `errors.ts`:`classifyCliError` 按 `EkbError` 子类与状态码映射退出码与恢复建议。
带结构化元数据的 `EkbError.details` 会保留在 JSON 错误中,例如工作流主保存已完成但元数据刷新失败时的 `remoteSaved` 和服务端版本;普通错误输出不变。
- `mutation.ts`:写操作统一防护(确认/dry-run 语义),边界由 `mutation-boundaries.test.ts` 强制。
- `生产环境安全阻断 (Production Safeguard)`:对带副作用的敏感调试命令(如 `workflow test`),在正式生产企业 (`env === "prod"`) 执行时触发硬阻断,要求必须显式带上 `--force` 确认生产风险;自省命令(`corps list`、`corps current`、`context`、`probe`)显式输出环境标识,并提供 `corps tag` 标记与 `corps list --test/--prod` 筛选。
- `生产环境安全阻断 (Production Safeguard)`:对带副作用的敏感命令,在正式生产企业 (`env === "prod"`) 执行时触发硬阻断。`workflow test` 需显式带 `--force` 确认生产风险;`workflow nodes grant` 需显式带 `--allow-prod`,并继续遵守 `--dry-run`/`--yes` 两阶段确认。自省命令(`corps list`、`corps current`、`context`、`probe`)显式输出环境标识,并提供 `corps tag` 标记与 `corps list --test/--prod` 筛选。
- `helpers.ts`:横切运行时助手(`initCorpRuntime` / `requireSelectedCorp` / `getSessionClientForRuntime`)。
- `service.ts`:常驻守护进程管理(安装/启停/状态)。
- `help.ts`:帮助文本组织。
@@ -3,29 +3,31 @@ import { handleWorkflowCommand } from "./workflow/index.js";
import { parseArgs } from "../parser.js";
import * as services from "../../domains/platform/workflow/services.js";
function prodRuntime(jsonMode = false) {
return {
selectedCorp: {
id: "prod-corp",
name: "正式生产企业",
env: "prod",
credentials: { corpId: "prod-corp", accessToken: "token" },
},
jsonMode,
} as any;
}
test("production corp is blocked before any grant request or confirmation", async () => {
const grant = spyOn(services, "grantWorkflowNodes").mockResolvedValue({
success: true, saved: ["code"], skipped: [], rawMessage: "ok", isTest: false,
});
try {
const runtime = {
selectedCorp: {
id: "prod-corp",
name: "正式生产企业",
env: "prod",
credentials: { corpId: "prod-corp", accessToken: "token" },
},
jsonMode: false,
} as any;
const result = await handleWorkflowCommand(
parseArgs(["workflow", "nodes", "grant", "code", "--yes"]),
runtime
prodRuntime()
);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain("生产环境安全阻断");
expect(result.stderr).toContain("未提供 --force 逃生口");
expect(result.stderr).toContain("--allow-prod");
expect(grant).not.toHaveBeenCalled();
} finally {
grant.mockRestore();
@@ -42,9 +44,85 @@ test("env-unmarked corp is conservatively treated as production", async () => {
{ selectedCorp: { id: "corp", name: "浙江麦瑞克科技有限公司" }, jsonMode: true } as any
);
expect(result.exitCode).toBe(1);
expect(JSON.parse(result.stdout).code).toBe("PROD_GRANT_BLOCKED");
const parsed = JSON.parse(result.stdout);
expect(parsed.code).toBe("PROD_GRANT_BLOCKED");
expect(parsed.nextAction).toContain("--allow-prod");
expect(grant).not.toHaveBeenCalled();
} finally {
grant.mockRestore();
}
});
test("--allow-prod dry-run previews the production grant without writing", async () => {
const grant = spyOn(services, "grantWorkflowNodes").mockResolvedValue({
success: true, saved: ["code"], skipped: [], rawMessage: "ok", isTest: false,
});
try {
const result = await handleWorkflowCommand(
parseArgs(["workflow", "nodes", "grant", "code", "--allow-prod", "--dry-run"]),
prodRuntime()
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("productionOverride: true");
expect(result.stdout).toContain("生产环境逃生口已启用");
expect(result.stdout).toContain("--allow-prod --yes");
expect(grant).not.toHaveBeenCalled();
} finally {
grant.mockRestore();
}
});
test("--allow-prod still requires --yes before the production grant runs", async () => {
const grant = spyOn(services, "grantWorkflowNodes").mockResolvedValue({
success: true, saved: ["code"], skipped: [], rawMessage: "ok", isTest: false,
});
try {
await expect(
handleWorkflowCommand(
parseArgs(["workflow", "nodes", "grant", "code", "--allow-prod"]),
prodRuntime()
)
).rejects.toThrow("拒绝执行开通企业工作流灰度节点");
expect(grant).not.toHaveBeenCalled();
} finally {
grant.mockRestore();
}
});
test("--allow-prod --yes executes the production grant and marks the result", async () => {
const grant = spyOn(services, "grantWorkflowNodes").mockResolvedValue({
success: true, saved: ["code"], skipped: [], rawMessage: "新开通的节点:code", isTest: false,
});
try {
const result = await handleWorkflowCommand(
parseArgs(["workflow", "nodes", "grant", "code", "--allow-prod", "--yes"]),
prodRuntime()
);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("生产环境逃生口已启用");
expect(result.stdout).toContain("灰度节点开通结果");
expect(grant).toHaveBeenCalledTimes(1);
} finally {
grant.mockRestore();
}
});
test("--allow-prod --yes JSON result exposes productionOverride", async () => {
const grant = spyOn(services, "grantWorkflowNodes").mockResolvedValue({
success: true, saved: ["code"], skipped: [], rawMessage: "新开通的节点:code", isTest: false,
});
try {
const result = await handleWorkflowCommand(
parseArgs(["workflow", "nodes", "grant", "code", "--allow-prod", "--yes", "--json"]),
prodRuntime(true)
);
expect(result.exitCode).toBe(0);
expect(JSON.parse(result.stdout).productionOverride).toBe(true);
expect(grant).toHaveBeenCalledTimes(1);
} finally {
grant.mockRestore();
}
});
+34 -9
View File
@@ -113,23 +113,34 @@ 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";
// 生产环境硬拦截:灰度节点开通属于不可逆企业级写操作,本项目禁止对生产企业执行。
// 与 workflow/test.ts 的调试阻断不同,这里不提供 --force 逃生口。
if (isCorpProd(corp)) {
// 生产环境默认硬拦截:灰度节点开通属于不可逆企业级写操作。
// 逃生口必须显式追加 --allow-prod,且仍受 --dry-run / --yes 两阶段确认约束。
if (isProd && !allowProd) {
return {
stdout: runtime.jsonMode
? printJson({ error: true, code: "PROD_GRANT_BLOCKED", message: "禁止对正式生产企业开通灰度节点" }) + "\n"
? printJson({
error: true,
code: "PROD_GRANT_BLOCKED",
message: "对正式生产企业开通灰度节点需显式追加 --allow-prod",
exitCode: 1,
nextAction: prodNextAction,
}) + "\n"
: "",
stderr: runtime.jsonMode
? ""
: [
"🛑 生产环境安全阻断 (Production Safeguard):",
`当前工作企业 [${corp.name || corp.id}] 被判定为【正式生产环境】。`,
"灰度节点开通是不可逆的企业级写操作,本项目禁止对生产企业执行,且未提供 --force 逃生口。",
"灰度节点开通是不可逆的企业级写操作,默认禁止对生产企业执行。",
"",
"如确需在生产环境开通,必须显式追加 --allow-prod 承担风险,并先预览再执行:",
" hose-use workflow nodes grant <type...> --allow-prod --dry-run",
` ${prodNextAction}`,
"",
"请切换到测试企业后重试:",
" hose-use corps switch <测试企业ID>",
"若企业环境标记有误,请检查 ~/.config/hose-use/enterprises/ 下的 env / isTest 字段。",
].join("\n") + "\n",
exitCode: 1,
@@ -159,7 +170,12 @@ export async function handleGrantNodes(
};
}
requireMutationConfirmation(parsed, runtime, "开通企业工作流灰度节点", "hose-use workflow nodes grant <type...> --yes");
requireMutationConfirmation(
parsed,
runtime,
"开通企业工作流灰度节点",
isProd ? prodNextAction : "hose-use workflow nodes grant <type...> --yes"
);
const isTest = booleanOption(parsed.options, ["is-test"]);
if (isDryRun(parsed, runtime)) {
return mutationPreview(parsed, runtime, "开通企业工作流灰度节点", corp.corpId || corp.credentials.corpId || corp.id, {
@@ -167,6 +183,12 @@ export async function handleGrantNodes(
isTest,
attachedOptions: GRANT_ATTACHED_OPTIONS,
adminPasswordRequired: Boolean(process.env.HOSE_ADMIN_PASSWORD),
...(isProd
? {
productionOverride: true,
productionWarning: "生产环境逃生口已启用:实际执行会变更正式生产企业,请再次核对企业与节点类型。",
}
: {}),
});
}
@@ -180,7 +202,7 @@ export async function handleGrantNodes(
if (runtime.jsonMode) {
return {
stdout: printJson(result, runtime.outputOptions),
stdout: printJson(isProd ? { ...result, productionOverride: true } : result, runtime.outputOptions),
stderr: "",
exitCode: result.success ? 0 : 1,
};
@@ -190,6 +212,9 @@ export async function handleGrantNodes(
}
const lines = [
...(isProd
? ["⚠️ 生产环境逃生口已启用 (--allow-prod):本次对正式生产企业执行了灰度节点开通。", ""]
: []),
`====== 灰度节点开通结果 [${corp.name}] ======`,
printKeyValue({
"申请开通节点": targetNodes.join(", "),
+3 -1
View File
@@ -88,13 +88,15 @@ export async function handleNodeCommand(
"",
"子命令 (企业灰度节点级别):",
" nodes [list] [--all] 列出当前企业已开通的节点字典 (别名: probe, catalog)",
" nodes grant <type...> [--is-test] 为当前企业开通灰度节点(标准/测试 Piper 二选一)",
" 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 node list 896398895352487950",
].join("\n") + "\n",
exitCode: 1,