Drive linter exposure from a manifest and drop the cwd linter dir
- checker implementations move to linter/checkers/, eliminating the builtin/strict/hlab + index + types files sitting flat in linter/ - manifest.ts is the single source of truth for which official linters exist and which are plaintext-exposed (plaintext flag) - plaintext.generated.ts replaces the hand-maintained official-templates.ts (580 lines): generated from checkers/ by scripts/generate-linter-plaintext.mjs, refreshed by bump-version, guarded in CI by verify:linter-plaintext - loader is manifest-driven (list, resolve, aliases); cwd linter loading is removed - custom linters resolve only from ~/.config/seal-use/linters/
This commit is contained in:
1 parent
cf9b7ab7e2
commit
8155c1ebb7
14 files changed
+199
-709
No files matched your search
@@ -38,6 +38,9 @@ jobs:
|
||||
- name: Lint
|
||||
run: bun run lint
|
||||
|
||||
- name: Linter Plaintext Sync
|
||||
run: bun run verify:linter-plaintext
|
||||
|
||||
- name: Colocated Unit Tests
|
||||
run: bun test
|
||||
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
"release": "bun run scripts/release.ts",
|
||||
"dev:override": "bun run scripts/dev-override.ts",
|
||||
"dev:link": "bun run scripts/dev-override.ts",
|
||||
"generate:linter-plaintext": "bun run scripts/generate-linter-plaintext.mjs",
|
||||
"verify:linter-plaintext": "bun run scripts/check-linter-plaintext.mjs",
|
||||
"test": "bun test",
|
||||
"prepare": "git config core.hooksPath .githooks"
|
||||
},
|
||||
|
||||
@@ -99,6 +99,9 @@ if (existsSync(globalSkillDir)) {
|
||||
}
|
||||
|
||||
// 6. Rebuild bundles
|
||||
|
||||
// 同步官方校验器明文嵌入物(checkers/ 源码 -> plaintext.generated.ts)
|
||||
execSync("bun run generate:linter-plaintext", { cwd: ROOT, stdio: "inherit" });
|
||||
console.log("==> Rebuilding binary bundle...");
|
||||
execSync("bun run build:binary", { cwd: ROOT, stdio: "inherit" });
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bun
|
||||
// CI/本地防漂移:checkers/ 源码改动后若忘记跑 generate:linter-plaintext,这里红。
|
||||
import { execSync } from "node:child_process";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
const ROOT = join(import.meta.dirname, "..");
|
||||
const generated = join(ROOT, "src/domains/seal/linter/plaintext.generated.ts");
|
||||
const before = readFileSync(generated, "utf8");
|
||||
execSync("bun run scripts/generate-linter-plaintext.mjs", { cwd: ROOT, stdio: "ignore" });
|
||||
const after = readFileSync(generated, "utf8");
|
||||
if (before !== after) {
|
||||
console.error("❌ linter/checkers/ 与 plaintext.generated.ts 不同步;请运行 bun run generate:linter-plaintext 并提交");
|
||||
process.exit(1);
|
||||
}
|
||||
console.log("✓ linter plaintext 与 checkers 源码同步");
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bun
|
||||
// 生成 src/domains/seal/linter/plaintext.generated.ts:
|
||||
// 把 manifest 中 plaintext: true 的官方校验器源码嵌入为字符串,
|
||||
// 供 syncOfficialLinters 写入 ~/.config/seal-use/linters/official/。
|
||||
// 修改 checkers/ 下任一源码后运行 `bun run generate:linter-plaintext` 刷新。
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
const ROOT = join(import.meta.dirname, "..");
|
||||
const manifestPath = join(ROOT, "src/domains/seal/linter/manifest.ts");
|
||||
const generatedPath = join(ROOT, "src/domains/seal/linter/plaintext.generated.ts");
|
||||
|
||||
// 从 manifest.ts 提取 (name, file, plaintext) —— 保持与运行时同一份清单
|
||||
const manifestSource = readFileSync(manifestPath, "utf8");
|
||||
const entries = [...manifestSource.matchAll(
|
||||
/name: "([a-z0-9-]+)",\s*\n\s*file: "([a-z.]+)",\s*\n\s*plaintext: (true|false)/g
|
||||
)].map((m) => ({ name: m[1], file: m[2], plaintext: m[3] === "true" }));
|
||||
|
||||
if (entries.length === 0) {
|
||||
console.error("❌ 未从 manifest.ts 解析到任何校验器条目");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const plaintext = {};
|
||||
for (const entry of entries) {
|
||||
if (!entry.plaintext) continue;
|
||||
const filePath = join(ROOT, "src/domains/seal/linter/checkers", entry.file);
|
||||
plaintext[entry.file] = readFileSync(filePath, "utf8");
|
||||
}
|
||||
|
||||
const banner =
|
||||
"/**\n * 生成文件 —— 由 scripts/generate-linter-plaintext.mjs 从 linter/checkers/ 生成,勿手改。\n" +
|
||||
` * 明文下发的官方校验器源码(manifest.ts 中 plaintext: true 的条目)。\n */\n`;
|
||||
|
||||
const content =
|
||||
banner +
|
||||
`export const OFFICIAL_LINTER_PLAINTEXT: Record<string, string> = ${JSON.stringify(plaintext, null, 2)};\n`;
|
||||
|
||||
writeFileSync(generatedPath, content);
|
||||
console.log(`✓ 已生成 plaintext.generated.ts(${Object.keys(plaintext).length} 个明文校验器: ${Object.keys(plaintext).join(", ")})`);
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "./types.js";
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "../types.js";
|
||||
|
||||
export const BUILTIN_VAGUE_WORDS = [
|
||||
"原则上",
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "./types.js";
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "../types.js";
|
||||
|
||||
export const HLAB_DISPOSAL_WORDS = [
|
||||
"驳回单据",
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "./types.js";
|
||||
import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from "../types.js";
|
||||
|
||||
export const STRICT_VAGUE_WORDS = [
|
||||
"原则上",
|
||||
@@ -1,7 +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 "./manifest.js";
|
||||
export * from "./checkers/builtin.js";
|
||||
export * from "./checkers/strict.js";
|
||||
export * from "./checkers/hlab.js";
|
||||
export * from "./loader.js";
|
||||
export * from "./service.js";
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { builtinRuleLinter } from "./builtin.js";
|
||||
import { builtinRuleLinter } from "./checkers/builtin.js";
|
||||
import { loadRuleLinter } from "./loader.js";
|
||||
import { lintRuleBatch } from "./service.js";
|
||||
import { writeFileSync, unlinkSync, mkdirSync, existsSync } from "node:fs";
|
||||
@@ -111,7 +111,7 @@ describe("Rule Linter", () => {
|
||||
});
|
||||
|
||||
test("strictRuleLinter enforces higher standards for quantitative constraints, approvers, and exceptions", async () => {
|
||||
const { strictRuleLinter } = await import("./strict.js");
|
||||
const { strictRuleLinter } = await import("./checkers/strict.js");
|
||||
|
||||
// 1. 少于 10 字告警
|
||||
const shortRes = strictRuleLinter({ description: "不能报销打车费" });
|
||||
@@ -160,16 +160,16 @@ describe("Rule Linter", () => {
|
||||
expect(existsSync(join(testOfficialDir, "hlab.ts"))).toBe(true);
|
||||
|
||||
const builtinPlaintext = readFileSync(join(testOfficialDir, "builtin.ts"), "utf8");
|
||||
expect(builtinPlaintext).toContain("Seal 官方标准规则校验器");
|
||||
expect(builtinPlaintext).toContain("export function lintRule");
|
||||
expect(builtinPlaintext).toContain("BUILTIN_VAGUE_WORDS");
|
||||
expect(builtinPlaintext).toContain("export function builtinRuleLinter");
|
||||
|
||||
const strictPlaintext = readFileSync(join(testOfficialDir, "strict.ts"), "utf8");
|
||||
expect(strictPlaintext).toContain("Seal 官方严格风控规则校验器");
|
||||
expect(strictPlaintext).toContain("STRICT_QUANTITATIVE_INDICATORS");
|
||||
expect(strictPlaintext).toContain("export function strictRuleLinter");
|
||||
|
||||
const hlabPlaintext = readFileSync(join(testOfficialDir, "hlab.ts"), "utf8");
|
||||
expect(hlabPlaintext).toContain("合思实验室 (HLab) 结构化规则校验器");
|
||||
expect(hlabPlaintext).toContain("HLAB_DISPOSAL_WORDS");
|
||||
expect(hlabPlaintext).toContain("export function hlabRuleLinter");
|
||||
|
||||
// 2. 模拟客户私自篡改 official/builtin.ts
|
||||
writeFileSync(join(testOfficialDir, "builtin.ts"), "// HACKED CONTENT", "utf8");
|
||||
@@ -187,7 +187,7 @@ describe("Rule Linter", () => {
|
||||
|
||||
// 验证官方文件已恢复明文
|
||||
const restoredContent = readFileSync(join(testOfficialDir, "builtin.ts"), "utf8");
|
||||
expect(restoredContent).toContain("Seal 官方标准规则校验器");
|
||||
expect(restoredContent).toContain("export function builtinRuleLinter");
|
||||
expect(restoredContent).not.toContain("// HACKED CONTENT");
|
||||
|
||||
// 验证多余文件已被清理干掉
|
||||
@@ -211,7 +211,7 @@ describe("Rule Linter", () => {
|
||||
});
|
||||
|
||||
test("hlabRuleLinter enforces HLab SOP structure, ternary closure, action decoupling, and false-skip prevention", async () => {
|
||||
const { hlabRuleLinter } = await import("./hlab.js");
|
||||
const { hlabRuleLinter } = await import("./checkers/hlab.js");
|
||||
|
||||
// 1. 标准 HLab 结构化规则:满分通过
|
||||
const perfectRule = {
|
||||
|
||||
@@ -2,15 +2,14 @@ import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, unlinkSync, w
|
||||
import { isAbsolute, join, resolve } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { PRIMARY_LINTERS_DIR, OFFICIAL_LINTERS_DIR } from "../../../core/paths.js";
|
||||
import { builtinRuleLinter } from "./builtin.js";
|
||||
import { hlabRuleLinter } from "./hlab.js";
|
||||
import { OFFICIAL_LINTER_TEMPLATES } from "./official-templates.js";
|
||||
import { strictRuleLinter } from "./strict.js";
|
||||
import { builtinRuleLinter } from "./checkers/builtin.js";
|
||||
import { OFFICIAL_LINTERS } from "./manifest.js";
|
||||
import { OFFICIAL_LINTER_PLAINTEXT } from "./plaintext.generated.js";
|
||||
|
||||
// 明文源码字符串由生成物提供(见 scripts/generate-linter-plaintext.mjs),勿手改
|
||||
import type { RuleLintContext, RuleLintResult, RuleLintTarget, RuleLinterFn } from "./types.js";
|
||||
|
||||
export { PRIMARY_LINTERS_DIR, OFFICIAL_LINTERS_DIR };
|
||||
export const OFFICIAL_SUBDIR_NAME = "official";
|
||||
export const LOCAL_LINTERS_DIR = join(process.cwd(), "linters");
|
||||
|
||||
export interface LoadedLinter {
|
||||
name: string;
|
||||
@@ -27,8 +26,10 @@ export interface AvailableLinterItem {
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制同步官方内置校验器明文文件,并清理 official 子目录下的任何非官方多余文件。
|
||||
* 用户 AI Agent 可直接阅读 official/ 下的源码;用户在 official/ 内所作的修改会被自动覆盖。
|
||||
* 强制同步"明文下发"的官方校验器到 ~/.config/seal-use/linters/official/,
|
||||
* 并清理 official 目录下任何非清单内的多余文件。
|
||||
* 哪些校验器明文下发由 manifest.ts 的 plaintext 标记决定;
|
||||
* 明文源码字符串由 plaintext.generated.ts 提供(生成物,勿手改)。
|
||||
*/
|
||||
export function syncOfficialLinters(officialDir = OFFICIAL_LINTERS_DIR): {
|
||||
synced: string[];
|
||||
@@ -42,33 +43,32 @@ export function syncOfficialLinters(officialDir = OFFICIAL_LINTERS_DIR): {
|
||||
mkdirSync(officialDir, { recursive: true });
|
||||
}
|
||||
|
||||
const officialFiles = Object.keys(OFFICIAL_LINTER_TEMPLATES);
|
||||
const exposed = OFFICIAL_LINTERS.filter((meta) => meta.plaintext);
|
||||
const exposedFiles = exposed.map((meta) => meta.file);
|
||||
|
||||
// 1. 强制写入/覆盖官方校验器明文源码
|
||||
for (const [filename, templateContent] of Object.entries(OFFICIAL_LINTER_TEMPLATES)) {
|
||||
const targetPath = join(officialDir, filename);
|
||||
for (const meta of exposed) {
|
||||
const targetPath = join(officialDir, meta.file);
|
||||
const templateContent = OFFICIAL_LINTER_PLAINTEXT[meta.file];
|
||||
if (!templateContent) continue;
|
||||
let needsWrite = true;
|
||||
if (existsSync(targetPath)) {
|
||||
try {
|
||||
const currentContent = readFileSync(targetPath, "utf8");
|
||||
if (currentContent === templateContent) {
|
||||
if (readFileSync(targetPath, "utf8") === templateContent) {
|
||||
needsWrite = false;
|
||||
}
|
||||
} catch {
|
||||
needsWrite = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (needsWrite) {
|
||||
writeFileSync(targetPath, templateContent, "utf8");
|
||||
}
|
||||
synced.push(filename);
|
||||
synced.push(meta.file);
|
||||
}
|
||||
|
||||
// 2. 清理 official/ 目录下用户误建或第三方写入的多余文件/子目录
|
||||
const existingEntries = readdirSync(officialDir, { withFileTypes: true });
|
||||
for (const entry of existingEntries) {
|
||||
if (!officialFiles.includes(entry.name)) {
|
||||
if (!exposedFiles.includes(entry.name)) {
|
||||
const fullPath = join(officialDir, entry.name);
|
||||
try {
|
||||
if (entry.isDirectory()) {
|
||||
@@ -90,61 +90,41 @@ export function syncOfficialLinters(officialDir = OFFICIAL_LINTERS_DIR): {
|
||||
}
|
||||
|
||||
/**
|
||||
* 列出所有可用的规则校验器:包括 official/ 目录下的官方规则和外层目录下的用户自定义规则。
|
||||
* 列出所有可用的规则校验器:官方清单 + ~/.config/seal-use/linters/ 下的用户自定义脚本。
|
||||
*/
|
||||
export function listAvailableLinters(options: { syncOfficial?: boolean } = { syncOfficial: true }): AvailableLinterItem[] {
|
||||
if (options.syncOfficial !== false) {
|
||||
syncOfficialLinters();
|
||||
}
|
||||
|
||||
const list: AvailableLinterItem[] = [
|
||||
{
|
||||
name: "builtin",
|
||||
type: "builtin",
|
||||
path: join(OFFICIAL_LINTERS_DIR, "builtin.ts"),
|
||||
description: "Seal 官方标准校验器 (拦截过短描述、主观模糊词与悬空例外条款)"
|
||||
},
|
||||
{
|
||||
name: "strict",
|
||||
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 四段论结构、三元闭环判定、剥离处置动作与防漏判)"
|
||||
}
|
||||
];
|
||||
const list: AvailableLinterItem[] = OFFICIAL_LINTERS.map((meta) => ({
|
||||
name: meta.name,
|
||||
type: meta.name === "builtin" ? ("builtin" as const) : ("official" as const),
|
||||
path: join(OFFICIAL_LINTERS_DIR, meta.file),
|
||||
description: meta.description
|
||||
}));
|
||||
|
||||
// 扫描用户自定义规则(仅扫描外层目录,忽略 official 子目录)
|
||||
const searchDirs = [PRIMARY_LINTERS_DIR, LOCAL_LINTERS_DIR];
|
||||
for (const dir of searchDirs) {
|
||||
if (existsSync(dir)) {
|
||||
try {
|
||||
const entries = readdirSync(dir, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory()) {
|
||||
// 排除 official 子目录及任何其他子目录
|
||||
continue;
|
||||
}
|
||||
const file = entry.name;
|
||||
if (file.endsWith(".ts") || file.endsWith(".js") || file.endsWith(".mjs")) {
|
||||
const baseName = file.replace(/\.(ts|js|mjs)$/, "");
|
||||
if (!list.some((item) => item.name === baseName)) {
|
||||
list.push({
|
||||
name: baseName,
|
||||
type: "custom",
|
||||
path: join(dir, file),
|
||||
description: `用户自定义规则校验器 (${file})`
|
||||
});
|
||||
}
|
||||
// 用户自定义:仅扫描全局私有目录(忽略 official 子目录)
|
||||
if (existsSync(PRIMARY_LINTERS_DIR)) {
|
||||
try {
|
||||
const entries = readdirSync(PRIMARY_LINTERS_DIR, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
if (entry.isDirectory()) continue;
|
||||
const file = entry.name;
|
||||
if (file.endsWith(".ts") || file.endsWith(".js") || file.endsWith(".mjs")) {
|
||||
const baseName = file.replace(/\.(ts|js|mjs)$/, "");
|
||||
if (!list.some((item) => item.name === baseName)) {
|
||||
list.push({
|
||||
name: baseName,
|
||||
type: "custom",
|
||||
path: join(PRIMARY_LINTERS_DIR, file),
|
||||
description: `用户自定义规则校验器 (${file})`
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// 忽略目录读取异常
|
||||
}
|
||||
} catch {
|
||||
// 忽略目录读取异常
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,38 +134,15 @@ export function listAvailableLinters(options: { syncOfficial?: boolean } = { syn
|
||||
export function resolveLinterPath(nameOrPath: string): {
|
||||
path?: string;
|
||||
isOfficial?: boolean;
|
||||
officialKind?: "builtin" | "strict" | "hlab";
|
||||
officialKind?: string;
|
||||
} {
|
||||
const norm = (nameOrPath || "").trim();
|
||||
if (!norm || norm === "builtin" || norm === "default" || norm === "official/builtin" || norm === "official/builtin.ts") {
|
||||
return {
|
||||
path: join(OFFICIAL_LINTERS_DIR, "builtin.ts"),
|
||||
isOfficial: true,
|
||||
officialKind: "builtin"
|
||||
};
|
||||
}
|
||||
|
||||
if (norm === "strict" || norm === "official/strict" || norm === "official/strict.ts") {
|
||||
return {
|
||||
path: join(OFFICIAL_LINTERS_DIR, "strict.ts"),
|
||||
isOfficial: true,
|
||||
officialKind: "strict"
|
||||
};
|
||||
}
|
||||
|
||||
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"
|
||||
};
|
||||
const official = OFFICIAL_LINTERS.find(
|
||||
(meta) => norm === meta.name || (meta.aliases ?? []).includes(norm) || norm === `official/${meta.name}` || norm === `official/${meta.file}`
|
||||
);
|
||||
if (official) {
|
||||
return { path: join(OFFICIAL_LINTERS_DIR, official.file), isOfficial: true, officialKind: official.name };
|
||||
}
|
||||
|
||||
// 显式相对或绝对路径
|
||||
@@ -197,18 +154,10 @@ export function resolveLinterPath(nameOrPath: string): {
|
||||
}
|
||||
}
|
||||
|
||||
// 自定义规则名称:仅在外层目录中搜索,绝不在 official/ 子目录中搜索
|
||||
const candidates = [
|
||||
join(PRIMARY_LINTERS_DIR, `${norm}.ts`),
|
||||
join(PRIMARY_LINTERS_DIR, `${norm}.js`),
|
||||
join(PRIMARY_LINTERS_DIR, `${norm}.mjs`),
|
||||
join(LOCAL_LINTERS_DIR, `${norm}.ts`),
|
||||
join(LOCAL_LINTERS_DIR, `${norm}.js`),
|
||||
join(LOCAL_LINTERS_DIR, `${norm}.mjs`)
|
||||
];
|
||||
|
||||
for (const cPath of candidates) {
|
||||
if (existsSync(cPath)) return { path: cPath };
|
||||
// 自定义校验器:仅搜索全局私有目录
|
||||
for (const ext of [".ts", ".js", ".mjs"]) {
|
||||
const candidate = join(PRIMARY_LINTERS_DIR, `${norm}${ext}`);
|
||||
if (existsSync(candidate)) return { path: candidate };
|
||||
}
|
||||
|
||||
return {};
|
||||
@@ -220,14 +169,10 @@ export async function loadRuleLinter(nameOrPath?: string): Promise<LoadedLinter>
|
||||
|
||||
const resolution = resolveLinterPath(nameOrPath ?? "builtin");
|
||||
|
||||
// 1. 处理官方内置校验器 (builtin / strict / hlab)
|
||||
// 1. 官方校验器(builtin / strict / hlab)
|
||||
if (resolution.isOfficial) {
|
||||
const fallbackFn =
|
||||
resolution.officialKind === "strict"
|
||||
? strictRuleLinter
|
||||
: resolution.officialKind === "hlab"
|
||||
? hlabRuleLinter
|
||||
: builtinRuleLinter;
|
||||
const meta = OFFICIAL_LINTERS.find((item) => item.name === resolution.officialKind)!;
|
||||
const fallbackFn = meta.fallback;
|
||||
const kind = resolution.officialKind === "builtin" ? "builtin" : "official";
|
||||
const officialPath = resolution.path;
|
||||
|
||||
@@ -238,7 +183,7 @@ export async function loadRuleLinter(nameOrPath?: string): Promise<LoadedLinter>
|
||||
const candidateFn = mod.default ?? mod.lintRule ?? mod.linter ?? mod.lint;
|
||||
if (typeof candidateFn === "function") {
|
||||
return {
|
||||
name: resolution.officialKind ?? "builtin",
|
||||
name: meta.name,
|
||||
type: kind,
|
||||
filePath: officialPath,
|
||||
linter: wrapSafeLinter(candidateFn, fallbackFn)
|
||||
@@ -250,14 +195,14 @@ export async function loadRuleLinter(nameOrPath?: string): Promise<LoadedLinter>
|
||||
}
|
||||
|
||||
return {
|
||||
name: resolution.officialKind ?? "builtin",
|
||||
name: meta.name,
|
||||
type: kind,
|
||||
filePath: officialPath,
|
||||
linter: fallbackFn
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 处理用户自定义校验器
|
||||
// 2. 用户自定义校验器
|
||||
const resolvedPath = resolution.path;
|
||||
if (!resolvedPath) {
|
||||
console.error(`⚠️ [linter] 未找到自定义校验器 "${nameOrPath}",已自动安全降级至内置标准校验器。`);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import type { RuleLinterFn } from "./types.js";
|
||||
import { builtinRuleLinter } from "./checkers/builtin.js";
|
||||
import { hlabRuleLinter } from "./checkers/hlab.js";
|
||||
import { strictRuleLinter } from "./checkers/strict.js";
|
||||
|
||||
/**
|
||||
* 官方校验器清单 —— "存在哪些校验器、是否明文下发"的唯一事实源。
|
||||
*
|
||||
* - `plaintext: true` 的条目,其源码会被 `syncOfficialLinters` 明文写入
|
||||
* `~/.config/seal-use/linters/official/<file>`(源码字符串由
|
||||
* `plaintext.generated.ts` 提供,经 `bun run generate:linter-plaintext` 从本目录生成,勿手改)。
|
||||
* - `plaintext: false` 的条目只以进程内函数存在,不下发明文。
|
||||
*/
|
||||
export interface OfficialLinterMeta {
|
||||
/** 调用名(rules lint --linter <name>) */
|
||||
name: string;
|
||||
/** linters/ 下的源码文件名 */
|
||||
file: string;
|
||||
/** 是否明文下发到 ~/.config/seal-use/linters/official/ */
|
||||
plaintext: boolean;
|
||||
description: string;
|
||||
/** 兼容别名 */
|
||||
aliases?: string[];
|
||||
/** 明文文件缺失/损坏时的进程内回退实现 */
|
||||
fallback: RuleLinterFn;
|
||||
}
|
||||
|
||||
export const OFFICIAL_LINTERS: OfficialLinterMeta[] = [
|
||||
{
|
||||
name: "builtin",
|
||||
file: "builtin.ts",
|
||||
plaintext: true,
|
||||
description: "Seal 官方标准校验器 (拦截过短描述、主观模糊词与悬空例外条款)",
|
||||
aliases: ["default"],
|
||||
fallback: builtinRuleLinter
|
||||
},
|
||||
{
|
||||
name: "strict",
|
||||
file: "strict.ts",
|
||||
plaintext: true,
|
||||
description: "Seal 官方严格风控校验器 (强校验字数、刚性定量约束、凭证依据与具体审批人角色)",
|
||||
fallback: strictRuleLinter
|
||||
},
|
||||
{
|
||||
name: "hlab",
|
||||
file: "hlab.ts",
|
||||
plaintext: true,
|
||||
description: "合思实验室 (HLab) 结构化规则校验器 (SOP 四段论结构、三元闭环判定、剥离处置动作与防漏判)",
|
||||
aliases: ["hlab-sop", "hlab-rules", "hlab-standard"],
|
||||
fallback: hlabRuleLinter
|
||||
}
|
||||
];
|
||||
@@ -1,580 +0,0 @@
|
||||
/**
|
||||
* 官方内置规则校验器源码模板。
|
||||
* 这些文件以明文 TypeScript 格式写入 ~/.config/seal-use/linters/official/ 目录,
|
||||
* 供用户和 AI Agent 直接阅读、学习和执行。
|
||||
*/
|
||||
|
||||
export const OFFICIAL_BUILTIN_LINTER_SOURCE = `/**
|
||||
* ==============================================================================
|
||||
* Seal 官方标准规则校验器 (Official Standard Rule Linter)
|
||||
* ==============================================================================
|
||||
*
|
||||
* 存放路径: ~/.config/seal-use/linters/official/builtin.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 BUILTIN_VAGUE_WORDS = [
|
||||
"原则上",
|
||||
"尽量",
|
||||
"适当",
|
||||
"酌情",
|
||||
"一般情况下",
|
||||
"尽可能",
|
||||
"合理控制",
|
||||
"视情况",
|
||||
"视具体情况"
|
||||
];
|
||||
|
||||
export const BUILTIN_EXCEPTION_TRIGGERS = [
|
||||
"除非",
|
||||
"特殊情况",
|
||||
"特殊原因",
|
||||
"经批准",
|
||||
"经审批",
|
||||
"经领导同意",
|
||||
"除外",
|
||||
"例外"
|
||||
];
|
||||
|
||||
export const BUILTIN_EVIDENCE_KEYWORDS = [
|
||||
"备注",
|
||||
"说明",
|
||||
"附件",
|
||||
"证明",
|
||||
"邮件",
|
||||
"截图",
|
||||
"凭证",
|
||||
"审批",
|
||||
"单据",
|
||||
"发票"
|
||||
];
|
||||
|
||||
export function lintRule(rule: RuleLintTarget): RuleLintResult {
|
||||
const text = (rule.description || "").trim();
|
||||
const issues: RuleLintIssue[] = [];
|
||||
|
||||
// 1. 规则过短检查
|
||||
if (text.length < 5) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "TOO_SHORT",
|
||||
message: "规则描述过于简短(少于 5 个字),大模型难以形成明确的判定逻辑",
|
||||
suggestion: "请补充具体的费用类型、核验条件与限制标准"
|
||||
});
|
||||
}
|
||||
|
||||
// 2. 主观模糊词拦截
|
||||
for (const word of BUILTIN_VAGUE_WORDS) {
|
||||
if (text.includes(word)) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "VAGUE_WORD",
|
||||
snippet: word,
|
||||
message: \`包含主观模糊词 "\${word}",容易导致大模型判定标准产生波动与幻觉\`,
|
||||
suggestion: \`建议移除 "\${word}",改为具体的金额上限、枚举范围或刚性判定条件\`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 悬空例外条款检测
|
||||
const matchedTrigger = BUILTIN_EXCEPTION_TRIGGERS.find((trigger) => text.includes(trigger));
|
||||
const hasEvidence = BUILTIN_EVIDENCE_KEYWORDS.some((kw) => text.includes(kw));
|
||||
|
||||
if (matchedTrigger && !hasEvidence) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "HANGING_EXCEPTION",
|
||||
snippet: matchedTrigger,
|
||||
message: \`包含例外条款 "\${matchedTrigger}",但未指定 AI 从何处核验该例外成立的依据\`,
|
||||
suggestion: "建议补充明确的凭据来源(例如:需在【单据备注】中说明原因,或在【附件】中上传审批截图)"
|
||||
});
|
||||
}
|
||||
|
||||
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_STRICT_LINTER_SOURCE = `/**
|
||||
* ==============================================================================
|
||||
* Seal 官方严格风控规则校验器 (Official Strict Risk Rule Linter)
|
||||
* ==============================================================================
|
||||
*
|
||||
* 存放路径: ~/.config/seal-use/linters/official/strict.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 STRICT_VAGUE_WORDS = [
|
||||
"原则上",
|
||||
"尽量",
|
||||
"适当",
|
||||
"酌情",
|
||||
"一般情况下",
|
||||
"尽可能",
|
||||
"合理控制",
|
||||
"视情况",
|
||||
"视具体情况",
|
||||
"必要时",
|
||||
"及时",
|
||||
"适量",
|
||||
"差不多",
|
||||
"相关部门"
|
||||
];
|
||||
|
||||
export const STRICT_EXCEPTION_TRIGGERS = [
|
||||
"除非",
|
||||
"特殊情况",
|
||||
"特殊原因",
|
||||
"经批准",
|
||||
"经审批",
|
||||
"经领导同意",
|
||||
"除外",
|
||||
"例外"
|
||||
];
|
||||
|
||||
export const STRICT_EVIDENCE_KEYWORDS = [
|
||||
"备注",
|
||||
"说明",
|
||||
"附件",
|
||||
"证明",
|
||||
"邮件",
|
||||
"截图",
|
||||
"凭证",
|
||||
"审批",
|
||||
"单据",
|
||||
"发票"
|
||||
];
|
||||
|
||||
export const STRICT_QUANTITATIVE_INDICATORS = [
|
||||
"¥",
|
||||
"$",
|
||||
"€",
|
||||
"元",
|
||||
"不得超过",
|
||||
"不超过",
|
||||
"超过",
|
||||
"低于",
|
||||
"不低于",
|
||||
"最高",
|
||||
"封顶",
|
||||
"限额",
|
||||
"禁止",
|
||||
"严禁",
|
||||
"必须",
|
||||
"不予",
|
||||
"仅限",
|
||||
"每人",
|
||||
"每日",
|
||||
"每月",
|
||||
"每年",
|
||||
"每次",
|
||||
"单笔"
|
||||
];
|
||||
|
||||
export const STRICT_SPECIFIC_APPROVERS = [
|
||||
"总监",
|
||||
"VP",
|
||||
"总经理",
|
||||
"直接上级",
|
||||
"部门负责人",
|
||||
"项目经理",
|
||||
"财务",
|
||||
"分管领导",
|
||||
"HR",
|
||||
"CEO"
|
||||
];
|
||||
|
||||
export function lintRule(rule: RuleLintTarget): RuleLintResult {
|
||||
const text = (rule.description || "").trim();
|
||||
const issues: RuleLintIssue[] = [];
|
||||
|
||||
// 1. 规则长度检查 (严格模式下少于 10 字进行警告)
|
||||
if (text.length < 5) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "TOO_SHORT",
|
||||
message: "规则描述过于简短(少于 5 个字),大模型难以形成明确的判定逻辑",
|
||||
suggestion: "请补充具体的费用类型、核验条件与限制标准"
|
||||
});
|
||||
} else if (text.length < 10) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "SHORT_DESCRIPTION",
|
||||
message: "规则描述偏短(少于 10 个字),可能缺少适用的单据范围或执行细节",
|
||||
suggestion: "建议扩充核验场景、限制标准或异常处理方式"
|
||||
});
|
||||
}
|
||||
|
||||
// 2. 主观模糊词拦截(严格词库)
|
||||
for (const word of STRICT_VAGUE_WORDS) {
|
||||
if (text.includes(word)) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "VAGUE_WORD",
|
||||
snippet: word,
|
||||
message: \`包含主观模糊词 "\${word}",在严格风控模式下容易引起审核判定分歧\`,
|
||||
suggestion: \`请移除 "\${word}",明确量化标准或枚举具体允许的情形\`
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 例外条款与凭据要求(严格模式下无凭据直接判定为 Error 阻断)
|
||||
const matchedTrigger = STRICT_EXCEPTION_TRIGGERS.find((trigger) => text.includes(trigger));
|
||||
const hasEvidence = STRICT_EVIDENCE_KEYWORDS.some((kw) => text.includes(kw));
|
||||
|
||||
if (matchedTrigger && !hasEvidence) {
|
||||
issues.push({
|
||||
level: "error",
|
||||
code: "HANGING_EXCEPTION",
|
||||
snippet: matchedTrigger,
|
||||
message: \`包含例外条款 "\${matchedTrigger}",但未指定 AI 从何处核验该例外成立的凭证依据\`,
|
||||
suggestion: "严格模式要求必须补充明确的核验来源(如:需在【单据备注】中说明原因,或在【附件】中提供审批截图)"
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 定量约束或刚性边界检查
|
||||
const hasNumber = /\\d+/.test(text);
|
||||
const hasConstraint = STRICT_QUANTITATIVE_INDICATORS.some((kw) => text.includes(kw));
|
||||
if (!hasNumber && !hasConstraint) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "LACKS_EXPLICIT_CONSTRAINT",
|
||||
message: "规则缺乏明确的定量标准(如金额、天数、频次)或绝对刚性边界词(如不得、禁止、仅限)",
|
||||
suggestion: "建议设定具体数值阈值或明确的禁止/准入条件,以便大模型精确判定"
|
||||
});
|
||||
}
|
||||
|
||||
// 5. 审批角色具体化检查
|
||||
const mentionsApproval = /经(批准|审批|领导同意|领导签字)|审批通过/.test(text);
|
||||
const hasSpecificApprover = STRICT_SPECIFIC_APPROVERS.some((approver) => text.includes(approver));
|
||||
if (mentionsApproval && !hasSpecificApprover) {
|
||||
issues.push({
|
||||
level: "warning",
|
||||
code: "AMBIGUOUS_APPROVER",
|
||||
message: "规则提及审批例外,但未明确指定具体的审批角色(如部门总监、直接上级、财务负责人)",
|
||||
suggestion: "建议明确审批人角色(例如:经【部门总监】批准,经【直接上级】邮件确认)"
|
||||
});
|
||||
}
|
||||
|
||||
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 * 30 - warningCount * 15);
|
||||
|
||||
return {
|
||||
score,
|
||||
passed: errorCount === 0,
|
||||
issues
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
"hlab.ts": OFFICIAL_HLAB_LINTER_SOURCE
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* 生成文件 —— 由 scripts/generate-linter-plaintext.mjs 从 linter/checkers/ 生成,勿手改。
|
||||
* 明文下发的官方校验器源码(manifest.ts 中 plaintext: true 的条目)。
|
||||
*/
|
||||
export const OFFICIAL_LINTER_PLAINTEXT: Record<string, string> = {
|
||||
"builtin.ts": "import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from \"../types.js\";\n\nexport const BUILTIN_VAGUE_WORDS = [\n \"原则上\",\n \"尽量\",\n \"适当\",\n \"酌情\",\n \"一般情况下\",\n \"尽可能\",\n \"合理控制\",\n \"视情况\",\n \"视具体情况\"\n];\n\nexport const BUILTIN_EXCEPTION_TRIGGERS = [\n \"除非\",\n \"特殊情况\",\n \"特殊原因\",\n \"经批准\",\n \"经审批\",\n \"经领导同意\",\n \"除外\",\n \"例外\"\n];\n\nexport const BUILTIN_EVIDENCE_KEYWORDS = [\n \"备注\",\n \"说明\",\n \"附件\",\n \"证明\",\n \"邮件\",\n \"截图\",\n \"凭证\",\n \"审批\",\n \"单据\",\n \"发票\"\n];\n\nexport function builtinRuleLinter(rule: RuleLintTarget): RuleLintResult {\n const text = (rule.description || \"\").trim();\n const issues: RuleLintIssue[] = [];\n\n // 1. 规则过短检查\n if (text.length < 5) {\n issues.push({\n level: \"error\",\n code: \"TOO_SHORT\",\n message: \"规则描述过于简短(少于 5 个字),大模型难以形成明确的判定逻辑\",\n suggestion: \"请补充具体的费用类型、核验条件与限制标准\"\n });\n }\n\n // 2. 主观模糊词拦截\n for (const word of BUILTIN_VAGUE_WORDS) {\n if (text.includes(word)) {\n issues.push({\n level: \"error\",\n code: \"VAGUE_WORD\",\n snippet: word,\n message: `包含主观模糊词 \"${word}\",容易导致大模型判定标准产生波动与幻觉`,\n suggestion: `建议移除 \"${word}\",改为具体的金额上限、枚举范围或刚性判定条件`\n });\n }\n }\n\n // 3. 悬空例外条款检测\n const matchedTrigger = BUILTIN_EXCEPTION_TRIGGERS.find((trigger) => text.includes(trigger));\n const hasEvidence = BUILTIN_EVIDENCE_KEYWORDS.some((kw) => text.includes(kw));\n\n if (matchedTrigger && !hasEvidence) {\n issues.push({\n level: \"warning\",\n code: \"HANGING_EXCEPTION\",\n snippet: matchedTrigger,\n message: `包含例外条款 \"${matchedTrigger}\",但未指定 AI 从何处核验该例外成立的依据`,\n suggestion: `建议补充明确的凭据来源(例如:\"需在【单据备注】中说明原因\" 或 \"需在【附件】中上传审批截图\")`\n });\n }\n\n const errorCount = issues.filter((i) => i.level === \"error\").length;\n const warningCount = issues.filter((i) => i.level === \"warning\").length;\n const score = Math.max(0, 100 - errorCount * 25 - warningCount * 10);\n\n return {\n score,\n passed: errorCount === 0,\n issues\n };\n}\n",
|
||||
"strict.ts": "import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from \"../types.js\";\n\nexport const STRICT_VAGUE_WORDS = [\n \"原则上\",\n \"尽量\",\n \"适当\",\n \"酌情\",\n \"一般情况下\",\n \"尽可能\",\n \"合理控制\",\n \"视情况\",\n \"视具体情况\",\n \"必要时\",\n \"及时\",\n \"适量\",\n \"差不多\",\n \"相关部门\"\n];\n\nexport const STRICT_EXCEPTION_TRIGGERS = [\n \"除非\",\n \"特殊情况\",\n \"特殊原因\",\n \"经批准\",\n \"经审批\",\n \"经领导同意\",\n \"除外\",\n \"例外\"\n];\n\nexport const STRICT_EVIDENCE_KEYWORDS = [\n \"备注\",\n \"说明\",\n \"附件\",\n \"证明\",\n \"邮件\",\n \"截图\",\n \"凭证\",\n \"审批\",\n \"单据\",\n \"发票\"\n];\n\nexport const STRICT_QUANTITATIVE_INDICATORS = [\n \"¥\",\n \"$\",\n \"€\",\n \"元\",\n \"不得超过\",\n \"不超过\",\n \"超过\",\n \"低于\",\n \"不低于\",\n \"最高\",\n \"封顶\",\n \"限额\",\n \"禁止\",\n \"严禁\",\n \"必须\",\n \"不予\",\n \"仅限\",\n \"每人\",\n \"每日\",\n \"每月\",\n \"每年\",\n \"每次\",\n \"单笔\"\n];\n\nexport const STRICT_SPECIFIC_APPROVERS = [\n \"总监\",\n \"VP\",\n \"总经理\",\n \"直接上级\",\n \"部门负责人\",\n \"项目经理\",\n \"财务\",\n \"分管领导\",\n \"HR\",\n \"CEO\"\n];\n\nexport function strictRuleLinter(rule: RuleLintTarget): RuleLintResult {\n const text = (rule.description || \"\").trim();\n const issues: RuleLintIssue[] = [];\n\n // 1. 规则长度检查\n if (text.length < 5) {\n issues.push({\n level: \"error\",\n code: \"TOO_SHORT\",\n message: \"规则描述过于简短(少于 5 个字),大模型难以形成明确的判定逻辑\",\n suggestion: \"请补充具体的费用类型、核验条件与限制标准\"\n });\n } else if (text.length < 10) {\n issues.push({\n level: \"warning\",\n code: \"SHORT_DESCRIPTION\",\n message: \"规则描述偏短(少于 10 个字),可能缺少适用的单据范围或执行细节\",\n suggestion: \"建议扩充核验场景、限制标准或异常处理方式\"\n });\n }\n\n // 2. 主观模糊词拦截(严格集合)\n for (const word of STRICT_VAGUE_WORDS) {\n if (text.includes(word)) {\n issues.push({\n level: \"error\",\n code: \"VAGUE_WORD\",\n snippet: word,\n message: `包含主观模糊词 \"${word}\",在严格风控模式下容易引起审核判定分歧`,\n suggestion: `请移除 \"${word}\",明确量化标准或枚举具体允许的情形`\n });\n }\n }\n\n // 3. 例外条款与凭据要求(严格模式下无凭证直接判定为 error)\n const matchedTrigger = STRICT_EXCEPTION_TRIGGERS.find((trigger) => text.includes(trigger));\n const hasEvidence = STRICT_EVIDENCE_KEYWORDS.some((kw) => text.includes(kw));\n\n if (matchedTrigger && !hasEvidence) {\n issues.push({\n level: \"error\",\n code: \"HANGING_EXCEPTION\",\n snippet: matchedTrigger,\n message: `包含例外条款 \"${matchedTrigger}\",但未指定 AI 从何处核验该例外成立的凭证依据`,\n suggestion: `严格模式要求必须补充明确的核验来源(如:\"需在【单据备注】中说明原因\" 或 \"需在【附件】中提供审批截图\")`\n });\n }\n\n // 4. 定量约束或刚性边界检查\n const hasNumber = /\\d+/.test(text);\n const hasConstraint = STRICT_QUANTITATIVE_INDICATORS.some((kw) => text.includes(kw));\n if (!hasNumber && !hasConstraint) {\n issues.push({\n level: \"warning\",\n code: \"LACKS_EXPLICIT_CONSTRAINT\",\n message: \"规则缺乏明确的定量标准(如金额、天数、频次)或绝对刚性边界词(如不得、禁止、仅限)\",\n suggestion: \"建议设定具体数值阈值或明确的禁止/准入条件,以便大模型精确判定\"\n });\n }\n\n // 5. 审批角色具体化检查\n const mentionsApproval = /经(批准|审批|领导同意|领导签字)|审批通过/.test(text);\n const hasSpecificApprover = STRICT_SPECIFIC_APPROVERS.some((approver) => text.includes(approver));\n if (mentionsApproval && !hasSpecificApprover) {\n issues.push({\n level: \"warning\",\n code: \"AMBIGUOUS_APPROVER\",\n message: \"规则提及审批例外,但未明确指定具体的审批角色(如部门总监、直接上级、财务负责人)\",\n suggestion: \"建议明确审批人角色(例如:经【部门总监】批准,经【直接上级】邮件确认)\"\n });\n }\n\n const errorCount = issues.filter((i) => i.level === \"error\").length;\n const warningCount = issues.filter((i) => i.level === \"warning\").length;\n const score = Math.max(0, 100 - errorCount * 30 - warningCount * 15);\n\n return {\n score,\n passed: errorCount === 0,\n issues\n };\n}\n\nexport default strictRuleLiLine truncated
|
||||
"hlab.ts": "import type { RuleLintIssue, RuleLintResult, RuleLintTarget } from \"../types.js\";\n\nexport const HLAB_DISPOSAL_WORDS = [\n \"驳回单据\",\n \"直接驳回\",\n \"整单退回\",\n \"退回单据\",\n \"退单处理\",\n \"进行提醒\",\n \"请进行提醒\",\n \"予以驳回\",\n \"直接退回\",\n \"无需人工关注\",\n \"无需展示在最后的审核结论中\"\n];\n\nexport const HLAB_SCOPE_SKIP_WORDS = [\n \"若上传了\",\n \"如上传了\",\n \"若提供了\",\n \"如提供了\",\n \"附件需上传\",\n \"上传了发票\",\n \"提供了支付截图\",\n \"提供了行程单\",\n \"上传了行程单\"\n];\n\nexport const HLAB_RISK_SUPPRESSION_WORDS = [\n \"忽略行程冲突\",\n \"忽略重复报销\",\n \"忽略跨城一致性\",\n \"不识别为风险\",\n \"不作为风险事项\",\n \"无需作为风险\"\n];\n\nexport const HLAB_EXCEPTION_TRIGGERS = [\n \"除非\",\n \"特殊情况\",\n \"特殊原因\",\n \"经批准\",\n \"经审批\",\n \"经领导同意\",\n \"除外\",\n \"例外\"\n];\n\nexport const HLAB_EVIDENCE_KEYWORDS = [\n \"备注\",\n \"说明\",\n \"附件\",\n \"证明\",\n \"邮件\",\n \"截图\",\n \"凭证\",\n \"审批\",\n \"单据\",\n \"发票\",\n \"流程\"\n];\n\n/**\n * 合思实验室 (HLab) 结构化审批规则校验器。\n * 遵循 HLab 规则工业化编写规范 (Rule-as-SOP):\n * 1. 结构完备性:必须包含【检查方法】与【判定标准】结构块\n * 2. 三元闭环判定:必须具备 PASS、FAIL 与 证据不足 三个分支,且证据不足须明确转人工复核\n * 3. 处置话术解耦:严禁在规则正文写整单处置动作(如驳回单据、直接驳回、进行提醒)\n * 4. 适用场景防漏判:拦截在适用场景中设置材料存在前置条件,避免缺失材料时被静默跳过 (False SKIP)\n * 5. 消极风险压制拦截:严禁在规则中配置忽略行程冲突或不识别风险等反模式\n * 6. 例外条款闭环:包含例外必须指定核验凭据\n */\nexport function hlabRuleLinter(rule: RuleLintTarget): RuleLintResult {\n const text = (rule.description || \"\").trim();\n const scopeText = (rule.scope || \"\").trim();\n const issues: RuleLintIssue[] = [];\n\n // 1. 规则长度检查\n if (text.length < 10) {\n issues.push({\n level: \"error\",\n code: \"TOO_SHORT\",\n message: \"规则描述过于简短(少于 10 个字),无法承载 HLab 结构化规范所要求的判定逻辑\",\n suggestion: \"请按照 HLab 工业化规范补充【检查方法】与【判定标准】\"\n });\n } else if (text.length < 30 && !text.includes(\"**\") && !text.includes(\"检查方法\")) {\n issues.push({\n level: \"warning\",\n code: \"SHORT_DESCRIPTION\",\n message: \"规则正文偏短且缺少结构化要素,可能缺少执行细节或证据边界\",\n suggestion: \"建议扩充核验场景、限制标准或异常处理方式\"\n });\n }\n\n // 2. 结构完备性检查\n const hasMethod = /\\*\\*检查方法\\*\\*|检查方法[::]/.test(text);\n if (!hasMethod) {\n issues.push({\n level: \"error\",\n code: \"MISSING_CHECK_METHOD\",\n message: \"规则正文缺少【检查方法】结构块,大模型难以确定数据提取来源与对齐逻辑\",\n suggestion: \"建议在正文前部增加 **检查方法**:明确 SealAI 应读取的凭证类型、字段名称、归一化方式或前置规则依赖\"\n });\n }\n\n const hasStandard = /\\*\\*判定标准\\*\\*|判定标准[::]/.test(text);\n if (!hasStandard) {\n issues.push({\n level: \"error\",\n code: \"MISSING_EVAL_STANDARD\",\n message: \"规则正文缺少【判定标准】结构块,缺乏标准化判定边界\",\n suggestion: \"建议在正文后部增加 **判定标准**:包含清晰的 PASS、FAIL 以及证据不足分支\"\n });\n }\n\n // 3. 三元闭环判定检查\n const hasPass = /[-*•]?\\s*PASS[::]/i.test(text);\n const hasFail = /[-*•]?\\s*FAIL[::]/i.test(text);\n const hasInsufficient = /[-*•]?\\s*证据不足[::]/.test(text);\n\n if (!hasPass || !hasFail || !hasInsufficient) {\n const missing: string[] = [];\n if (!hasPass) missing.push(\"PASS\");\n if (!hasFail) missing.push(\"FAIL\");\n if (!hasInsufficient) missing.push(\"证据不足\");\n issues.push({\n level: \"error\",\n code: \"INCOMPLETE_TERNARY_BRANCH\",\n message: `判定标准未形成三元闭环,缺少分支:${missing.join(\"、\")}`,\n suggestion: \"HLab 规范要求必须提供完整的三元分支:- PASS(充分合规条件)、- FAIL(明确违规条件)、- 证据不足(容错与复核条件)\"\n });\n }\n\n // 4. 证据不足分支人工复核兜底检查\n if (hasInsufficient) {\n const snippet = text.split(\"证据不足\")?.[1]?.slice(0, 100) || \"\";\n const hasManualReview = /人工Line truncated
|
||||
};
|
||||
Reference in new issue
Block a user