fix(approval): keep dry-run paths free of remote and local mutations

This commit is contained in:
gitea committed 2026-10-20 17:50:06 +08:00
1 parent fac81b08e8
commit ae2a1a9d60
5 files changed
+231 -184

No files matched your search

+7
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env bun
import { isDryRun, mutationPreview } from "./cli/mutation.js";
import { parseArgs, stringOption, booleanOption, isJsonRequested } from "./cli/parser.js";
import { cliErrorResult } from "./cli/runner.js";
import { printHelp, printJson, redactSensitiveText } from "./cli/printer.js";
@@ -48,6 +49,12 @@ async function runAiProCliOnce(rawArgs: string[]): Promise<{ stdout: string; std
});
const corp = runtime.selectedCorp;
const previewPositional = parsed.positional || [];
const previewCommand = (previewPositional[0] || "help").toLowerCase();
const mutating = ["init", "start", "bind", "mount", "review", "eval", "evaluate", "decide", "approve", "reject"].includes(previewCommand) || (["points", "clauses"].includes(previewCommand) && previewPositional.length >= 3);
if (mutating && isDryRun(parsed, runtime)) {
return mutationPreview({ ...parsed, raw: ["aipro", ...rawArgs] }, runtime, "预览 AI PRO 审批操作", stringOption(parsed.options, ["run", "r", "code", "sn", "id", "target"]) || previewPositional[1] || "未指定", { action: previewCommand, corpId: corp?.corpId || corp?.id, apply: booleanOption(parsed.options, ["apply", "execute", "run", "do"]) });
}
const repo = new ApprovalRepository();
const service = new ApprovalService(repo);
let cachedSession: Awaited<ReturnType<typeof getSessionClientForRuntime>> | undefined;
+33
View File
@@ -0,0 +1,33 @@
import { afterEach, expect, test } from "bun:test";
import { mkdtempSync, mkdirSync, writeFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { runAiProCli } from "../aipro-cli.js";
import { runCli } from "./runner.js";
import { closeDatabase } from "../core/db/index.js";
const keys = ["HOSE_APPROVAL_DB", "HOSE_ENTERPRISES_DIR", "HOSE_CONFIG_FILE"] as const;
const original = keys.map(key => process.env[key]);
afterEach(() => { closeDatabase(); keys.forEach((key, i) => { if (original[i] === undefined) delete process.env[key]; else process.env[key] = original[i]; }); });
test("approval and aipro dry-runs override apply/yes without requests or database creation", async () => {
const root = mkdtempSync(join(tmpdir(), "hose-approval-preview-"));
const corps = join(root, "corps"); mkdirSync(corps);
writeFileSync(join(corps, "corp.json"), JSON.stringify({ id: "corp", name: "Preview", source: "manual", credentials: { accessToken: "fixture", corpId: "corp", staffId: "staff" } }));
process.env.HOSE_ENTERPRISES_DIR = corps;
process.env.HOSE_CONFIG_FILE = join(root, "config.json");
process.env.HOSE_APPROVAL_DB = join(root, "approval.db");
const originalFetch = globalThis.fetch;
let requests = 0;
globalThis.fetch = (async () => { requests++; throw new Error("No network during preview"); }) as any;
try {
for (const action of ["points", "review", "decide", "approve", "reject"]) {
const result = await runCli(["approval", action, "SN", "--apply", "--yes", "--dry-run", "--json"]);
expect(result.exitCode).toBe(0); expect(JSON.parse(result.stdout).dryRun).toBe(true);
}
for (const action of ["init", "bind", "review", "decide", "approve", "reject"]) {
const result = await runAiProCli([action, "approve", "--run", "ABC123", "--apply", "--yes", "--dry-run", "--json"]);
expect(result.exitCode).toBe(0); expect(JSON.parse(result.stdout).dryRun).toBe(true);
expect(JSON.parse(result.stdout).nextAction).toStartWith("hose-use aipro ");
}
expect(requests).toBe(0); expect(existsSync(process.env.HOSE_APPROVAL_DB)).toBe(false);
} finally { globalThis.fetch = originalFetch; }
});
+7 -2
View File
@@ -1,3 +1,4 @@
import { isDryRun, mutationPreview } from "../mutation.js";
import { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
import { printJson, printTable } from "../printer.js";
import { stringOption, booleanOption, numberOption } from "../parser.js";
@@ -19,9 +20,7 @@ export async function handleApprovalCommand(
);
}
const client = await createSessionClient(corp);
const corpId = corp.corpId || corp.id;
const service = new ApprovalService();
// 解析命令层级:
// 形式 A: hose-use approval audit points S26000001
@@ -40,6 +39,12 @@ export async function handleApprovalCommand(
const targetSn = args[0] || stringOption(parsed.options, ["code", "sn", "target", "id"]);
if (targetSn && isDryRun(parsed, runtime) && ["audit", "points", "inject", "review", "eval", "evaluate", "decide", "approve", "reject"].includes(subAction || "audit")) {
return mutationPreview(parsed, runtime, "预览审批操作", targetSn, { action: subAction || "audit", corpId, apply: booleanOption(parsed.options, ["apply", "execute", "run", "do"]) });
}
const client = await createSessionClient(corp);
const service = new ApprovalService();
switch (subAction) {
// ── 1. 规则注入与出明细级审核要点清单 ─────────────────────────────────────
case "points":
+2
View File
@@ -15,3 +15,5 @@
- 决策必须经 gateway 仲裁:`ApprovalDecision` 为封闭枚举(`APPROVE` / `REJECT` / `PASS_WITH_NOTE` / `REVIEW` / `PENDING` / `ERROR`),写动作由 `validateGatewayAction` 卡点。
- 规则严格度分四级:`硬性` / `弹性` / `建议` / `特殊`。
`approval` 和 AI PRO 的注入、核验与决策支持统一 `--dry-run` 预览;该选项优先于 `--apply --yes`,预览不创建客户端、会话数据库,也不更新核验结果、决策或下发审批。