Add exit codes 13 and 14 for config validation and safety blocks

The classifier only matched English usage wording, so every Chinese-language
usage error, every enterprise-selection failure, and every Zod/SyntaxError
fell through to exit 1 ("upstream failure"), while environment safety guards
were reported as exit 2 or 1. Add two semantic codes:

- 13 (validationError): well-formed command, unusable value — missing --corp,
  unparseable enterprise JSON, missing required parameter, ambiguous webhook
  channel selection.
- 14 (safetyBlocked): the target environment forbids the action — replaying
  into production, regression outside a test tenant.

classifyCliError now recognises Chinese usage wording, enterprise-selection
phrasing, local file problems, Zod issue lists (structural, not textual) and
SyntaxError; English Usage:/Unknown-command stays 2, domain failures and
quality gates stay 1. Docs (AGENTS, cli-protocol, skill cli.md) and the exit
code assertions follow.
This commit is contained in:
gitea committed 2026-09-29 22:04:12 +08:00
1 parent 5f4906758e
commit 66eb81f3d1
12 files changed
+177 -134

No files matched your search

+3 -4
View File
@@ -1,5 +1,5 @@
import type { KyInstance } from "ky";
import { CliError } from "../../../core/errors.js";
import { CliError, safetyBlocked } from "../../../core/errors.js";
import { formatBeijingIso } from "../../../core/config/timestamps.js";
import type { CorpConfig } from "../../../core/config/types.js";
import { resolveSealCustomHeaders } from "../../../core/http/request-headers.js";
@@ -98,10 +98,9 @@ export async function runRegressionTest(
// 1. Red Line 1: Enterprise Isolation Guard
if (targetCorp.environment !== "test") {
throw new CliError(
throw safetyBlocked(
`重放目标企业「${targetCorp.name}」(${targetCorp.id}) 的环境为「${targetCorp.environment}」,非测试环境!\n` +
`硬性安全规定:自动化回归测试矩阵严禁在生产环境直接运行。必须绑定并指定测试环境(testCorp)。`,
2,
`seal-use corps bind-test ${sourceCorp.id} <testCorpId>`
);
}
@@ -365,7 +364,7 @@ export async function runRegressionTest(
}
export function formatRegressionDashboard(result: RegressionTestResult): string {
const { sourceCorp, targetCorp, verdict, safeToPublish, metrics, quota, flips, records } = result;
const { sourceCorp, targetCorp, verdict, safeToPublish, metrics, quota, flips } = result;
const verdictBadge =
verdict === "CLEAN"
+6 -110
View File
@@ -1,25 +1,12 @@
import { z } from "zod";
import type { KyInstance } from "ky";
import { readFileSync } from "node:fs";
import { randomBytes } from "node:crypto";
import * as api from "../api.js";
import { defaultSealUrl, ensureHoseCloseApiToken, getHoseProvisionalAuthLink, probeHoseDomain } from "../../../core/auth/hose.js";
import type { CorpConfig } from "../../../core/config/types.js";
import { validationError } from "../../../core/errors.js";
import { loadGlobalConfig, loadCorpConfigs, getEnterprisesDirCandidates } from "../../../core/config/loader.js";
import { resolveCorpConfig } from "../../../core/config/manager.js";
import { createSealClient, createSealClientForToken } from "../../../core/http/factory.js";
import { createSealClient } from "../../../core/http/factory.js";
import { resolveSealCustomHeaders } from "../../../core/http/request-headers.js";
import { searchApprovalContent } from "../search.js";
import { diffRuleVersions, normalizedRuleCode, truncateRuleDiff } from "../rule-diff.js";
import { hydrateRuleVersionStableIds, savePublishedRuleVersionSnapshot } from "../rule-version-snapshot.js";
import { executeApprovalRunsReplay, resolveReplayCorpPair } from "../webhook-replay.js";
import { loadRuleLinter, lintRuleBatch, lintSingleRule, type RuleLintTarget } from "../linter/index.js";
import {
loadLocalRulesFile,
saveLocalRulesFile,
localRulesToLintTargets,
type LocalRuleItem
} from "../rules-local.js";
import {
checkWebhookReadiness,
previewMaterialPack,
@@ -27,110 +14,19 @@ import {
readMaterialPackFile
} from "../material-pack/index.js";
import { MaterialPackFilePolicyPatchSchema, MaterialPackFilePolicySchema } from "../material-pack/policy.js";
import { extractApprovalRunAttachments } from "../attachments.js";
import { exportApprovalRunsToExcel, normalizeTimestamp } from "../approval-runs-export.js";
import {
alignApprovalRunsWithHoseFlow,
createHoseOpenClient,
getHoseOpenFlow
} from "../../hose/index.js";
import { exportInsightReportToExcel, type InsightExportOptions } from "../insight-export.js";
import {
ThinkingPolicySchema,
type ApprovalRule,
type ApprovalRun,
type RuleSetVersion,
type SimulationBatch,
type WebhookIntegration,
type WebhookMode,
type ThinkingPolicy,
type DecisionPreferences,
type HosecloudConfigInput
type WebhookMode
} from "../types.js";
import type {
ToolContext,
SealToolDefinition,
ApprovalRunSummary,
ApprovalRunsSummaryOptions,
RuleField,
RulePushPlan,
SimulationBatchCreateInput,
HumanResultMatch
ToolContext
} from "./helpers.js";
import * as helpers from "./helpers.js";
const {
presentWebhookIntegration,
getConfiguredSealBaseUrl,
projectApprovalRules,
pickRuleFields,
buildRulePushPlan,
rulesHaveSameContent,
ruleChangedFields,
normalizeRuleStrictness,
parseDurationMs,
redactIntegrationSecrets,
mutationPreview,
redactPreviewValue,
buildSimulationBatchParams,
summarizeSimulationBatch,
summarizeApprovalRun,
summarizeApprovalRuns,
parseFieldPaths,
pickRunFields,
approvalRunMetadata,
summarizeApprovalRunDetail,
summarizeApprovalRunResult,
summarizeApprovalRunDocument,
extractCitedRules,
firstPath,
countPathCandidates,
getPath,
setPath,
compactObject,
asRecord,
numberFromUnknown,
flattenDocumentFields,
collectDocumentFields,
firstByLabel,
collectInvoiceSummary,
collectInvoiceObjects,
findKeywordLines,
summarizeRunBridge,
summarizeRuleVersion,
resolveRuleVersionForLookup,
findRuleInVersion,
normalizeRuleCode,
runtimeRuleIndex,
runtimeRuleIdToCode,
formatRuleCode,
pickApprovalRunCandidate,
resolveApprovalRunForUrl,
resolveSealBaseUrl,
normalizeUrl,
stringFromUnknown,
pickApprovalRuns,
timestampOf,
resolveRuleVersion,
resolveRuleVersionSelector,
filterRuns,
scalarIncludes,
objectContainsScalar,
filterRunsByHumanResult,
withApprovalRunFilters,
resolveManualApprovalStatus,
prepareApprovalRuns,
approvalRunAliases,
extractHumanResult,
findHumanResultValue,
isHumanResultKey,
stringifyHumanResultValue,
matchesRunLookup,
countBy,
localDateKey,
formatLocalDateTime,
toDate,
localParts
mutationPreview
} = helpers;
export const webhookTools = [
@@ -444,7 +340,7 @@ export const webhookTools = [
integration = integrations[0]!;
} else if (integrations.length > 1) {
const options = integrations.map((i) => `"${i.name}" (${i.id})`).join(", ");
throw new Error(`当前租户存在多个 Webhook 通道 [${options}],请通过 webhook 参数指定目标通道。`);
throw validationError(`当前租户存在多个 Webhook 通道 [${options}],请通过 webhook 参数指定目标通道。`);
} else {
throw new Error("当前租户未配置任何 Webhook 集成通道。");
}
+3 -4
View File
@@ -2,7 +2,7 @@ import type { KyInstance } from "ky";
import type { ApprovalRun, WebhookIntegration } from "./types.js";
import type { MaterialField } from "./material-pack/types.js";
import { webhookHeaders } from "./material-pack/signature.js";
import { CliError } from "../../core/errors.js";
import { CliError, safetyBlocked, validationError } from "../../core/errors.js";
import type { CorpConfig } from "../../core/config/types.js";
import { resolveSealCustomHeaders } from "../../core/http/request-headers.js";
import * as api from "./api.js";
@@ -198,7 +198,7 @@ export function selectWebhookIntegration(
return replayHook;
}
const options = integrations.map((i) => `"${i.name}" (${i.id})`).join(", ");
throw new Error(`当前租户存在多个 Webhook 通道 [${options}],请通过 --webhook <idOrName> 指定目标通道。`);
throw validationError(`当前租户存在多个 Webhook 通道 [${options}],请通过 --webhook <idOrName> 指定目标通道。`);
}
throw new CliError(
@@ -274,10 +274,9 @@ export function resolveReplayCorpPair(
// Safety guard: prevent pushing replay test records to production
if (targetCorp.environment === "production" && !force) {
throw new CliError(
throw safetyBlocked(
`目标企业「${targetCorp.name}」(${targetCorp.id}) 标记为生产正式环境 (production)。\n` +
`为防止测试单据污染生产审批流,禁止向生产环境重放单据。请指定测试企业 (--target-corp <testCorpId>),或在确有需要时添加 --force。`,
1,
`seal-use webhook replay --target-corp <testCorpId>${sourceCorp.id !== targetCorp.id ? ` --source-corp ${sourceCorp.id}` : ""}`
);
}