Add official HLab structured rule linter
- Implement hlabRuleLinter enforcing HLab SOP structure, ternary decision closure, disposal action decoupling, and false-skip prevention - Register hlab.ts template in official templates for automatic plaintext sync - Support hlab and aliases (hlab-sop, hlab-rules, hlab-standard) in linter loader - Document HLab linter and detection rules in skill references - Add comprehensive unit tests in linter.test.ts
This commit is contained in:
1 parent
38968f2373
commit
8e06b7035c
8 files changed
+1017
-141
No files matched your search
@@ -0,0 +1,217 @@
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "./types.js";
|
||||
|
||||
export const HLAB_DISPOSAL_WORDS = [
|
||||
"驳回单据",
|
||||
"直接驳回",
|
||||
"整单退回",
|
||||
"退回单据",
|
||||
"退单处理",
|
||||
"进行提醒",
|
||||
"请进行提醒",
|
||||
"予以驳回",
|
||||
"直接退回",
|
||||
"无需人工关注",
|
||||
"无需展示在最后的审核结论中"
|
||||
];
|
||||
|
||||
export const HLAB_SCOPE_SKIP_WORDS = [
|
||||
"若上传了",
|
||||
"如上传了",
|
||||
"若提供了",
|
||||
"如提供了",
|
||||
"附件需上传",
|
||||
"上传了发票",
|
||||
"提供了支付截图",
|
||||
"提供了行程单",
|
||||
"上传了行程单"
|
||||
];
|
||||
|
||||
export const HLAB_RISK_SUPPRESSION_WORDS = [
|
||||
"忽略行程冲突",
|
||||
"忽略重复报销",
|
||||
"忽略跨城一致性",
|
||||
"不识别为风险",
|
||||
"不作为风险事项",
|
||||
"无需作为风险"
|
||||
];
|
||||
|
||||
export const HLAB_EXCEPTION_TRIGGERS = [
|
||||
"除非",
|
||||
"特殊情况",
|
||||
"特殊原因",
|
||||
"经批准",
|
||||
"经审批",
|
||||
"经领导同意",
|
||||
"除外",
|
||||
"例外"
|
||||
];
|
||||
|
||||
export const HLAB_EVIDENCE_KEYWORDS = [
|
||||
"备注",
|
||||
"说明",
|
||||
"附件",
|
||||
"证明",
|
||||
"邮件",
|
||||
"截图",
|
||||
"凭证",
|
||||
"审批",
|
||||
"单据",
|
||||
"发票",
|
||||
"流程"
|
||||
];
|
||||
|
||||
/**
|
||||
* 合思实验室 (HLab) 结构化审批规则校验器。
|
||||
* 遵循 HLab 规则工业化编写规范 (Rule-as-SOP):
|
||||
* 1. 结构完备性:必须包含【检查方法】与【判定标准】结构块
|
||||
* 2. 三元闭环判定:必须具备 PASS、FAIL 与 证据不足 三个分支,且证据不足须明确转人工复核
|
||||
* 3. 处置话术解耦:严禁在规则正文写整单处置动作(如驳回单据、直接驳回、进行提醒)
|
||||
* 4. 适用场景防漏判:拦截在适用场景中设置材料存在前置条件,避免缺失材料时被静默跳过 (False SKIP)
|
||||
* 5. 消极风险压制拦截:严禁在规则中配置忽略行程冲突或不识别风险等反模式
|
||||
* 6. 例外条款闭环:包含例外必须指定核验凭据
|
||||
*/
|
||||
export function hlabRuleLinter(rule: RuleLintTarget): RuleLintResult {
|
||||
const text = (rule.description || "").trim();
|
||||
const scopeText = (rule.scope || "").trim();
|
||||
const issues: RuleLintIssue[] = [];
|
||||
|
||||
// 1. 规则长度检查
|
||||
if (text.length < 10) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "TOO_SHORT",
|
||||
message: "规则描述过于简短(少于 10 个字),无法承载 HLab 结构化规范所要求的判定逻辑",
|
||||
suggestion: "请按照 HLab 工业化规范补充【检查方法】与【判定标准】"
|
||||
});
|
||||
} else if (text.length < 30 && !text.includes("**") && !text.includes("检查方法")) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "SHORT_DESCRIPTION",
|
||||
message: "规则正文偏短且缺少结构化要素,可能缺少执行细节或证据边界",
|
||||
suggestion: "建议扩充核验场景、限制标准或异常处理方式"
|
||||
});
|
||||
}
|
||||
|
||||
// 2. 结构完备性检查
|
||||
const hasMethod = /\*\*检查方法\*\*|检查方法[::]/.test(text);
|
||||
if (!hasMethod) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "MISSING_CHECK_METHOD",
|
||||
message: "规则正文缺少【检查方法】结构块,大模型难以确定数据提取来源与对齐逻辑",
|
||||
suggestion: "建议在正文前部增加 **检查方法**:明确 SealAI 应读取的凭证类型、字段名称、归一化方式或前置规则依赖"
|
||||
});
|
||||
}
|
||||
|
||||
const hasStandard = /\*\*判定标准\*\*|判定标准[::]/.test(text);
|
||||
if (!hasStandard) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "MISSING_EVAL_STANDARD",
|
||||
message: "规则正文缺少【判定标准】结构块,缺乏标准化判定边界",
|
||||
suggestion: "建议在正文后部增加 **判定标准**:包含清晰的 PASS、FAIL 以及证据不足分支"
|
||||
});
|
||||
}
|
||||
|
||||
// 3. 三元闭环判定检查
|
||||
const hasPass = /[-*•]?\s*PASS[::]/i.test(text);
|
||||
const hasFail = /[-*•]?\s*FAIL[::]/i.test(text);
|
||||
const hasInsufficient = /[-*•]?\s*证据不足[::]/.test(text);
|
||||
|
||||
if (!hasPass || !hasFail || !hasInsufficient) {
|
||||
const missing: string[] = [];
|
||||
if (!hasPass) missing.push("PASS");
|
||||
if (!hasFail) missing.push("FAIL");
|
||||
if (!hasInsufficient) missing.push("证据不足");
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "INCOMPLETE_TERNARY_BRANCH",
|
||||
message: `判定标准未形成三元闭环,缺少分支:${missing.join("、")}`,
|
||||
suggestion: "HLab 规范要求必须提供完整的三元分支:- PASS(充分合规条件)、- FAIL(明确违规条件)、- 证据不足(容错与复核条件)"
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 证据不足分支人工复核兜底检查
|
||||
if (hasInsufficient) {
|
||||
const snippet = text.split("证据不足")?.[1]?.slice(0, 100) || "";
|
||||
const hasManualReview = /人工复核|人工核对|人工审核|转人工/.test(snippet);
|
||||
if (!hasManualReview) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "INSUFFICIENT_WITHOUT_MANUAL_REVIEW",
|
||||
message: "【证据不足】分支未明确指定转【人工复核】兜底,大模型在材料模糊或冲突时可能失控",
|
||||
suggestion: "建议在【证据不足】分支末尾补充标准处置话术:'转人工复核',确保容错降级路径统一"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 整单处置动作越界拦截
|
||||
for (const word of HLAB_DISPOSAL_WORDS) {
|
||||
if (text.includes(word)) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "DISPOSAL_ACTION_LEAK",
|
||||
snippet: word,
|
||||
message: `规则正文包含整单处置动作话术 "${word}",违反 HLab 规则事实判定与处置动作解耦规范`,
|
||||
suggestion: `请移除正文中的 "${word}";单据处置动作(驳回/提醒)应交由平台严格程度(必须遵守/仅建议)或业务工作流定义`
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 适用场景防静默漏判检查
|
||||
if (scopeText) {
|
||||
for (const word of HLAB_SCOPE_SKIP_WORDS) {
|
||||
if (scopeText.includes(word)) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "SCOPE_FALSE_SKIP_PRONE",
|
||||
snippet: word,
|
||||
message: `适用场景包含材料上传前置判断 "${word}",当材料未上传时可能导致该规则被静默跳过(SKIP)而非报违规(FAIL)`,
|
||||
suggestion: `建议适用场景仅保留业务与单据触发条件(如单据模板、费用类型);材料缺失的检查应显式置于【判定标准】的 FAIL 分支中`
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 消极风险压制拦截
|
||||
for (const word of HLAB_RISK_SUPPRESSION_WORDS) {
|
||||
if (text.includes(word)) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "RISK_SUPPRESSION",
|
||||
snippet: word,
|
||||
message: `规则正文包含消极风险压制话术 "${word}",可能破坏模型正常审计底线或压制有效违规`,
|
||||
suggestion: "请移除该规则或改写为正向合规要求;若需豁免特定场景,请在 **例外** 结构块中封闭说明豁免条件与凭证依据"
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 8. 悬空例外与凭据检查
|
||||
const matchedTrigger = HLAB_EXCEPTION_TRIGGERS.find((trigger) => text.includes(trigger));
|
||||
const hasEvidence = HLAB_EVIDENCE_KEYWORDS.some((kw) => text.includes(kw));
|
||||
|
||||
if (matchedTrigger && !hasEvidence) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "HANGING_EXCEPTION",
|
||||
snippet: matchedTrigger,
|
||||
message: `包含例外条款 "${matchedTrigger}",但未指定 AI 从何处核验该例外成立的凭据来源`,
|
||||
suggestion: `HLab 规范要求例外条款必须明确凭据载体(如:在【**例外**】块中注明需提供专项 OA 审批 PDF 附件或商家盖章证明)`
|
||||
});
|
||||
}
|
||||
|
||||
const errorCount = issues.filter((i) => i.level === "error").length;
|
||||
const warningCount = issues.filter((i) => i.level === "warning").length;
|
||||
const score = Math.max(0, 100 - errorCount * 25 - warningCount * 10);
|
||||
|
||||
return {
|
||||
score,
|
||||
passed: errorCount === 0,
|
||||
issues
|
||||
};
|
||||
}
|
||||
|
||||
export default hlabRuleLinter;
|
||||
@@ -1,6 +1,7 @@
|
||||
export * from "./types.js";
|
||||
export * from "./builtin.js";
|
||||
export * from "./strict.js";
|
||||
export * from "./hlab.js";
|
||||
export * from "./official-templates.js";
|
||||
export * from "./loader.js";
|
||||
export * from "./service.js";
|
||||
@@ -152,11 +152,12 @@ describe("Rule Linter", () => {
|
||||
const testOfficialDir = join(tmpdir(), `seal-official-linter-test-${Date.now()}`);
|
||||
|
||||
try {
|
||||
// 1. 首次同步:生成 builtin.ts 与 strict.ts 明文文件
|
||||
// 1. 首次同步:生成 builtin.ts、strict.ts 与 hlab.ts 明文文件
|
||||
const firstSync = syncOfficialLinters(testOfficialDir);
|
||||
expect(firstSync.synced.sort()).toEqual(["builtin.ts", "strict.ts"]);
|
||||
expect(firstSync.synced.sort()).toEqual(["builtin.ts", "hlab.ts", "strict.ts"]);
|
||||
expect(existsSync(join(testOfficialDir, "builtin.ts"))).toBe(true);
|
||||
expect(existsSync(join(testOfficialDir, "strict.ts"))).toBe(true);
|
||||
expect(existsSync(join(testOfficialDir, "hlab.ts"))).toBe(true);
|
||||
|
||||
const builtinPlaintext = readFileSync(join(testOfficialDir, "builtin.ts"), "utf8");
|
||||
expect(builtinPlaintext).toContain("Seal 官方标准规则校验器");
|
||||
@@ -166,6 +167,10 @@ describe("Rule Linter", () => {
|
||||
expect(strictPlaintext).toContain("Seal 官方严格风控规则校验器");
|
||||
expect(strictPlaintext).toContain("STRICT_QUANTITATIVE_INDICATORS");
|
||||
|
||||
const hlabPlaintext = readFileSync(join(testOfficialDir, "hlab.ts"), "utf8");
|
||||
expect(hlabPlaintext).toContain("合思实验室 (HLab) 结构化规则校验器");
|
||||
expect(hlabPlaintext).toContain("HLAB_DISPOSAL_WORDS");
|
||||
|
||||
// 2. 模拟客户私自篡改 official/builtin.ts
|
||||
writeFileSync(join(testOfficialDir, "builtin.ts"), "// HACKED CONTENT", "utf8");
|
||||
expect(readFileSync(join(testOfficialDir, "builtin.ts"), "utf8")).toBe("// HACKED CONTENT");
|
||||
@@ -177,7 +182,7 @@ describe("Rule Linter", () => {
|
||||
|
||||
// 4. 再次执行同步:篡改的内容必须被官方重写,多余文件和子目录必须全部被干掉
|
||||
const secondSync = syncOfficialLinters(testOfficialDir);
|
||||
expect(secondSync.synced.sort()).toEqual(["builtin.ts", "strict.ts"]);
|
||||
expect(secondSync.synced.sort()).toEqual(["builtin.ts", "hlab.ts", "strict.ts"]);
|
||||
expect(secondSync.pruned.sort()).toEqual(["extra-subfolder", "my-custom-rule.ts", "random.txt"]);
|
||||
|
||||
// 验证官方文件已恢复明文
|
||||
@@ -204,5 +209,116 @@ describe("Rule Linter", () => {
|
||||
expect(res.passed).toBe(false);
|
||||
expect(res.issues.some((i) => i.code === "VAGUE_WORD")).toBe(true);
|
||||
});
|
||||
|
||||
test("hlabRuleLinter enforces HLab SOP structure, ternary closure, action decoupling, and false-skip prevention", async () => {
|
||||
const { hlabRuleLinter } = await import("./hlab.js");
|
||||
|
||||
// 1. 标准 HLab 结构化规则:满分通过
|
||||
const perfectRule = {
|
||||
scope: "单据模板或费用类型为业务招待费。",
|
||||
description: `**检查方法**:
|
||||
读取菜单凭证中的用餐开始时间和结束时间;两项均可识别时,按完整时间计算两者间隔。
|
||||
|
||||
**判定标准**:
|
||||
- PASS:开始时间和结束时间均可识别,且用餐时间间隔大于15分钟。
|
||||
- FAIL:开始时间和结束时间均可识别,且用餐时间间隔小于或等于15分钟;15分钟作为边界值计入不符合范围。
|
||||
- 证据不足:菜单只记录其中一个时间、任一时间不可读或结束时间早于开始时间时,转人工复核。`
|
||||
};
|
||||
const perfectRes = hlabRuleLinter(perfectRule);
|
||||
expect(perfectRes.passed).toBe(true);
|
||||
expect(perfectRes.score).toBe(100);
|
||||
expect(perfectRes.issues.length).toBe(0);
|
||||
|
||||
// 2. 缺失【检查方法】与【判定标准】
|
||||
const noStructureRes = hlabRuleLinter({
|
||||
description: "报销金额不能超过实际支付金额,否则驳回单据。"
|
||||
});
|
||||
expect(noStructureRes.passed).toBe(false);
|
||||
expect(noStructureRes.issues.some((i) => i.code === "MISSING_CHECK_METHOD")).toBe(true);
|
||||
expect(noStructureRes.issues.some((i) => i.code === "MISSING_EVAL_STANDARD")).toBe(true);
|
||||
expect(noStructureRes.issues.some((i) => i.code === "DISPOSAL_ACTION_LEAK")).toBe(true);
|
||||
|
||||
// 3. 判定标准缺少三元分支中的 FAIL 与 证据不足
|
||||
const partialBranchRes = hlabRuleLinter({
|
||||
description: `**检查方法**:
|
||||
核对发票金额与报销金额。
|
||||
|
||||
**判定标准**:
|
||||
- PASS:发票金额等于报销金额。`
|
||||
});
|
||||
expect(partialBranchRes.passed).toBe(false);
|
||||
const branchIssue = partialBranchRes.issues.find((i) => i.code === "INCOMPLETE_TERNARY_BRANCH");
|
||||
expect(branchIssue).toBeDefined();
|
||||
expect(branchIssue?.message).toContain("FAIL");
|
||||
expect(branchIssue?.message).toContain("证据不足");
|
||||
|
||||
// 4. 证据不足分支未指定转人工复核
|
||||
const noManualReviewRes = hlabRuleLinter({
|
||||
description: `**检查方法**:
|
||||
核对发票金额。
|
||||
|
||||
**判定标准**:
|
||||
- PASS:发票金额合规。
|
||||
- FAIL:发票金额不合规。
|
||||
- 证据不足:发票不可读时不处理。`
|
||||
});
|
||||
expect(noManualReviewRes.passed).toBe(true); // 只有 warning 不会使 passed 为 false
|
||||
expect(noManualReviewRes.issues.some((i) => i.code === "INSUFFICIENT_WITHOUT_MANUAL_REVIEW")).toBe(true);
|
||||
|
||||
// 5. 适用场景包含上传材料前置条件(防 False SKIP)
|
||||
const falseSkipRes = hlabRuleLinter({
|
||||
scope: "若上传了发票,费用类型为餐饮费。",
|
||||
description: `**检查方法**:
|
||||
检查发票抬头。
|
||||
|
||||
**判定标准**:
|
||||
- PASS:抬头一致。
|
||||
- FAIL:抬头不一致。
|
||||
- 证据不足:抬头模糊时转人工复核。`
|
||||
});
|
||||
expect(falseSkipRes.issues.some((i) => i.code === "SCOPE_FALSE_SKIP_PRONE")).toBe(true);
|
||||
|
||||
// 6. 消极风险压制拦截
|
||||
const suppressionRes = hlabRuleLinter({
|
||||
description: `**检查方法**:
|
||||
忽略行程冲突,只检查金额。
|
||||
|
||||
**判定标准**:
|
||||
- PASS:金额合规。
|
||||
- FAIL:金额超标。
|
||||
- 证据不足:模糊时转人工复核。`
|
||||
});
|
||||
expect(suppressionRes.passed).toBe(false);
|
||||
expect(suppressionRes.issues.some((i) => i.code === "RISK_SUPPRESSION")).toBe(true);
|
||||
|
||||
// 7. 悬空例外拦截
|
||||
const hangingExceptionRes = hlabRuleLinter({
|
||||
description: `**检查方法**:
|
||||
检查差旅住宿费。特殊情况除外。
|
||||
|
||||
**判定标准**:
|
||||
- PASS:住宿费未超标。
|
||||
- FAIL:住宿费超标。
|
||||
- 证据不足:转人工复核。`
|
||||
});
|
||||
expect(hangingExceptionRes.passed).toBe(false);
|
||||
expect(hangingExceptionRes.issues.some((i) => i.code === "HANGING_EXCEPTION")).toBe(true);
|
||||
});
|
||||
|
||||
test("loadRuleLinter loads hlab linter and aliases correctly", async () => {
|
||||
const loadedHlab = await loadRuleLinter("hlab");
|
||||
expect(loadedHlab.name).toBe("hlab");
|
||||
expect(loadedHlab.type).toBe("official");
|
||||
|
||||
const loadedAlias = await loadRuleLinter("hlab-sop");
|
||||
expect(loadedAlias.name).toBe("hlab");
|
||||
expect(loadedAlias.type).toBe("official");
|
||||
|
||||
const { listAvailableLinters } = await import("./loader.js");
|
||||
const linters = listAvailableLinters({ syncOfficial: false });
|
||||
const hlabItem = linters.find((l) => l.name === "hlab");
|
||||
expect(hlabItem).toBeDefined();
|
||||
expect(hlabItem?.description).toContain("合思实验室 (HLab)");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@ import { homedir } from "node:os";
|
||||
import { isAbsolute, join, resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { builtinRuleLinter } from "./builtin.js";
|
||||
import { hlabRuleLinter } from "./hlab.js";
|
||||
import { OFFICIAL_LINTER_TEMPLATES } from "./official-templates.js";
|
||||
import { strictRuleLinter } from "./strict.js";
|
||||
import type { RuleLintContext, RuleLintResult, RuleLintTarget, RuleLinterFn } from "./types.js";
|
||||
@@ -109,6 +110,12 @@ export function listAvailableLinters(options: { syncOfficial?: boolean } = { syn
|
||||
type: "official",
|
||||
path: join(OFFICIAL_LINTERS_DIR, "strict.ts"),
|
||||
description: "Seal 官方严格风控校验器 (强校验字数、刚性定量约束、凭证依据与具体审批人角色)"
|
||||
},
|
||||
{
|
||||
name: "hlab",
|
||||
type: "official",
|
||||
path: join(OFFICIAL_LINTERS_DIR, "hlab.ts"),
|
||||
description: "合思实验室 (HLab) 结构化规则校验器 (SOP 四段论结构、三元闭环判定、剥离处置动作与防漏判)"
|
||||
}
|
||||
];
|
||||
|
||||
@@ -148,7 +155,7 @@ export function listAvailableLinters(options: { syncOfficial?: boolean } = { syn
|
||||
export function resolveLinterPath(nameOrPath: string): {
|
||||
path?: string;
|
||||
isOfficial?: boolean;
|
||||
officialKind?: "builtin" | "strict";
|
||||
officialKind?: "builtin" | "strict" | "hlab";
|
||||
} {
|
||||
const norm = (nameOrPath || "").trim();
|
||||
if (!norm || norm === "builtin" || norm === "default" || norm === "official/builtin" || norm === "official/builtin.ts") {
|
||||
@@ -167,6 +174,21 @@ export function resolveLinterPath(nameOrPath: string): {
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
norm === "hlab" ||
|
||||
norm === "hlab-sop" ||
|
||||
norm === "hlab-rules" ||
|
||||
norm === "hlab-standard" ||
|
||||
norm === "official/hlab" ||
|
||||
norm === "official/hlab.ts"
|
||||
) {
|
||||
return {
|
||||
path: join(OFFICIAL_LINTERS_DIR, "hlab.ts"),
|
||||
isOfficial: true,
|
||||
officialKind: "hlab"
|
||||
};
|
||||
}
|
||||
|
||||
// 显式相对或绝对路径
|
||||
if (isAbsolute(norm) || norm.startsWith("./") || norm.startsWith("../")) {
|
||||
const absPath = resolve(norm);
|
||||
@@ -199,10 +221,15 @@ export async function loadRuleLinter(nameOrPath?: string): Promise<LoadedLinter>
|
||||
|
||||
const resolution = resolveLinterPath(nameOrPath ?? "builtin");
|
||||
|
||||
// 1. 处理官方内置校验器 (builtin / strict)
|
||||
// 1. 处理官方内置校验器 (builtin / strict / hlab)
|
||||
if (resolution.isOfficial) {
|
||||
const fallbackFn = resolution.officialKind === "strict" ? strictRuleLinter : builtinRuleLinter;
|
||||
const kind = resolution.officialKind === "strict" ? "official" : "builtin";
|
||||
const fallbackFn =
|
||||
resolution.officialKind === "strict"
|
||||
? strictRuleLinter
|
||||
: resolution.officialKind === "hlab"
|
||||
? hlabRuleLinter
|
||||
: builtinRuleLinter;
|
||||
const kind = resolution.officialKind === "builtin" ? "builtin" : "official";
|
||||
const officialPath = resolution.path;
|
||||
|
||||
if (officialPath && existsSync(officialPath)) {
|
||||
|
||||
@@ -331,7 +331,250 @@ export function lintRule(rule: RuleLintTarget): RuleLintResult {
|
||||
export default lintRule;
|
||||
`;
|
||||
|
||||
export const OFFICIAL_HLAB_LINTER_SOURCE = `/**
|
||||
* ==============================================================================
|
||||
* 合思实验室 (HLab) 结构化规则校验器 (HLab Structured SOP Rule Linter)
|
||||
* ==============================================================================
|
||||
*
|
||||
* 存放路径: ~/.config/seal-use/linters/official/hlab.ts
|
||||
* 维护说明: 本文件由 seal-use 官方自动同步并保护。
|
||||
* 每次运行时系统会自动重写本文件并清理 official/ 目录下的非官方文件。
|
||||
* 若您需要编写企业私有规则校验器,请将自定义 .ts 文件放置在外层的用户目录:
|
||||
* ~/.config/seal-use/linters/ 即可,系统将自动识别并加载。
|
||||
*/
|
||||
|
||||
export interface RuleLintTarget {
|
||||
id?: string;
|
||||
code?: string;
|
||||
name?: string;
|
||||
description: string;
|
||||
scope?: string;
|
||||
strictness?: string;
|
||||
}
|
||||
|
||||
export interface RuleLintIssue {
|
||||
level: "error" | "warning";
|
||||
code: string;
|
||||
message: string;
|
||||
snippet?: string;
|
||||
suggestion?: string;
|
||||
}
|
||||
|
||||
export interface RuleLintResult {
|
||||
score: number;
|
||||
passed: boolean;
|
||||
issues: RuleLintIssue[];
|
||||
}
|
||||
|
||||
export const HLAB_DISPOSAL_WORDS = [
|
||||
"驳回单据",
|
||||
"直接驳回",
|
||||
"整单退回",
|
||||
"退回单据",
|
||||
"退单处理",
|
||||
"进行提醒",
|
||||
"请进行提醒",
|
||||
"予以驳回",
|
||||
"直接退回",
|
||||
"无需人工关注",
|
||||
"无需展示在最后的审核结论中"
|
||||
];
|
||||
|
||||
export const HLAB_SCOPE_SKIP_WORDS = [
|
||||
"若上传了",
|
||||
"如上传了",
|
||||
"若提供了",
|
||||
"如提供了",
|
||||
"附件需上传",
|
||||
"上传了发票",
|
||||
"提供了支付截图",
|
||||
"提供了行程单",
|
||||
"上传了行程单"
|
||||
];
|
||||
|
||||
export const HLAB_RISK_SUPPRESSION_WORDS = [
|
||||
"忽略行程冲突",
|
||||
"忽略重复报销",
|
||||
"忽略跨城一致性",
|
||||
"不识别为风险",
|
||||
"不作为风险事项",
|
||||
"无需作为风险"
|
||||
];
|
||||
|
||||
export const HLAB_EXCEPTION_TRIGGERS = [
|
||||
"除非",
|
||||
"特殊情况",
|
||||
"特殊原因",
|
||||
"经批准",
|
||||
"经审批",
|
||||
"经领导同意",
|
||||
"除外",
|
||||
"例外"
|
||||
];
|
||||
|
||||
export const HLAB_EVIDENCE_KEYWORDS = [
|
||||
"备注",
|
||||
"说明",
|
||||
"附件",
|
||||
"证明",
|
||||
"邮件",
|
||||
"截图",
|
||||
"凭证",
|
||||
"审批",
|
||||
"单据",
|
||||
"发票",
|
||||
"流程"
|
||||
];
|
||||
|
||||
export function lintRule(rule: RuleLintTarget): RuleLintResult {
|
||||
const text = (rule.description || "").trim();
|
||||
const scopeText = (rule.scope || "").trim();
|
||||
const issues: RuleLintIssue[] = [];
|
||||
|
||||
// 1. 规则长度检查
|
||||
if (text.length < 10) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "TOO_SHORT",
|
||||
message: "规则描述过于简短(少于 10 个字),无法承载 HLab 结构化规范所要求的判定逻辑",
|
||||
suggestion: "请按照 HLab 工业化规范补充【检查方法】与【判定标准】"
|
||||
});
|
||||
} else if (text.length < 30 && !text.includes("**") && !text.includes("检查方法")) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "SHORT_DESCRIPTION",
|
||||
message: "规则正文偏短且缺少结构化要素,可能缺少执行细节或证据边界",
|
||||
suggestion: "建议扩充核验场景、限制标准或异常处理方式"
|
||||
});
|
||||
}
|
||||
|
||||
// 2. 结构完备性检查
|
||||
const hasMethod = /\\*\\*检查方法\\*\\*|检查方法[::]/.test(text);
|
||||
if (!hasMethod) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "MISSING_CHECK_METHOD",
|
||||
message: "规则正文缺少【检查方法】结构块,大模型难以确定数据提取来源与对齐逻辑",
|
||||
suggestion: "建议在正文前部增加 **检查方法**:明确 SealAI 应读取的凭证类型、字段名称、归一化方式或前置规则依赖"
|
||||
});
|
||||
}
|
||||
|
||||
const hasStandard = /\\*\\*判定标准\\*\\*|判定标准[::]/.test(text);
|
||||
if (!hasStandard) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "MISSING_EVAL_STANDARD",
|
||||
message: "规则正文缺少【判定标准】结构块,缺乏标准化判定边界",
|
||||
suggestion: "建议在正文后部增加 **判定标准**:包含清晰的 PASS、FAIL 以及证据不足分支"
|
||||
});
|
||||
}
|
||||
|
||||
// 3. 三元闭环判定检查
|
||||
const hasPass = /[-*•]?\\s*PASS[::]/i.test(text);
|
||||
const hasFail = /[-*•]?\\s*FAIL[::]/i.test(text);
|
||||
const hasInsufficient = /[-*•]?\\s*证据不足[::]/.test(text);
|
||||
|
||||
if (!hasPass || !hasFail || !hasInsufficient) {
|
||||
const missing: string[] = [];
|
||||
if (!hasPass) missing.push("PASS");
|
||||
if (!hasFail) missing.push("FAIL");
|
||||
if (!hasInsufficient) missing.push("证据不足");
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "INCOMPLETE_TERNARY_BRANCH",
|
||||
message: \`判定标准未形成三元闭环,缺少分支:\${missing.join("、")}\`,
|
||||
suggestion: "HLab 规范要求必须提供完整的三元分支:- PASS(充分合规条件)、- FAIL(明确违规条件)、- 证据不足(容错与复核条件)"
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 证据不足分支人工复核兜底检查
|
||||
if (hasInsufficient) {
|
||||
const snippet = text.split("证据不足")?.[1]?.slice(0, 100) || "";
|
||||
const hasManualReview = /人工复核|人工核对|人工审核|转人工/.test(snippet);
|
||||
if (!hasManualReview) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "INSUFFICIENT_WITHOUT_MANUAL_REVIEW",
|
||||
message: "【证据不足】分支未明确指定转【人工复核】兜底,大模型在材料模糊或冲突时可能失控",
|
||||
suggestion: "建议在【证据不足】分支末尾补充标准处置话术:'转人工复核',确保容错降级路径统一"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 整单处置动作越界拦截
|
||||
for (const word of HLAB_DISPOSAL_WORDS) {
|
||||
if (text.includes(word)) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "DISPOSAL_ACTION_LEAK",
|
||||
snippet: word,
|
||||
message: \`规则正文包含整单处置动作话术 "\${word}",违反 HLab 规则事实判定与处置动作解耦规范\`,
|
||||
suggestion: \`请移除正文中的 "\${word}";单据处置动作(驳回/提醒)应交由平台严格程度(必须遵守/仅建议)或业务工作流定义\`
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 适用场景防静默漏判检查
|
||||
if (scopeText) {
|
||||
for (const word of HLAB_SCOPE_SKIP_WORDS) {
|
||||
if (scopeText.includes(word)) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "SCOPE_FALSE_SKIP_PRONE",
|
||||
snippet: word,
|
||||
message: \`适用场景包含材料上传前置判断 "\${word}",当材料未上传时可能导致该规则被静默跳过(SKIP)而非报违规(FAIL)\`,
|
||||
suggestion: "建议适用场景仅保留业务与单据触发条件(如单据模板、费用类型);材料缺失的检查应显式置于【判定标准】的 FAIL 分支中"
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 消极风险压制拦截
|
||||
for (const word of HLAB_RISK_SUPPRESSION_WORDS) {
|
||||
if (text.includes(word)) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "RISK_SUPPRESSION",
|
||||
snippet: word,
|
||||
message: \`规则正文包含消极风险压制话术 "\${word}",可能破坏模型正常审计底线或压制有效违规\`,
|
||||
suggestion: "请移除该规则或改写为正向合规要求;若需豁免特定场景,请在 **例外** 结构块中封闭说明豁免条件与凭证依据"
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 8. 悬空例外与凭据检查
|
||||
const matchedTrigger = HLAB_EXCEPTION_TRIGGERS.find((trigger) => text.includes(trigger));
|
||||
const hasEvidence = HLAB_EVIDENCE_KEYWORDS.some((kw) => text.includes(kw));
|
||||
|
||||
if (matchedTrigger && !hasEvidence) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "HANGING_EXCEPTION",
|
||||
snippet: matchedTrigger,
|
||||
message: \`包含例外条款 "\${matchedTrigger}",但未指定 AI 从何处核验该例外成立的凭据来源\`,
|
||||
suggestion: "HLab 规范要求例外条款必须明确凭据载体(如:在【**例外**】块中注明需提供专项 OA 审批 PDF 附件或商家盖章证明)"
|
||||
});
|
||||
}
|
||||
|
||||
const errorCount = issues.filter((i) => i.level === "error").length;
|
||||
const warningCount = issues.filter((i) => i.level === "warning").length;
|
||||
const score = Math.max(0, 100 - errorCount * 25 - warningCount * 10);
|
||||
|
||||
return {
|
||||
score,
|
||||
passed: errorCount === 0,
|
||||
issues
|
||||
};
|
||||
}
|
||||
|
||||
export default lintRule;
|
||||
`;
|
||||
|
||||
export const OFFICIAL_LINTER_TEMPLATES: Record<string, string> = {
|
||||
"builtin.ts": OFFICIAL_BUILTIN_LINTER_SOURCE,
|
||||
"strict.ts": OFFICIAL_STRICT_LINTER_SOURCE
|
||||
"strict.ts": OFFICIAL_STRICT_LINTER_SOURCE,
|
||||
"hlab.ts": OFFICIAL_HLAB_LINTER_SOURCE
|
||||
};
|
||||
Reference in new issue
Block a user