Apply approval rule pushes incrementally
This commit is contained in:
1 parent
0b10a70924
commit
1910faa8ea
4 files changed
+199
-21
No files matched your search
@@ -142,7 +142,7 @@ Approval rule maintenance:
|
||||
- `seal-use rules list --summary [--corp <corpId>]`
|
||||
- `seal-use rules pull [--corp <corpId>] [--version <id|num|latest>] [--out <file>]`
|
||||
- `seal-use rules lint [--file <path>] [--corp <corpId>] [--linter <name|path>] [--version <num>] [--code <code>] [--min-score <score>] [--summary]`
|
||||
- `seal-use rules push [--file <path>] [--corp <corpId>] [--publish] [--version-name <name>] [--dry-run|--yes]`
|
||||
- `seal-use rules push [--file <path>] [--corp <corpId>] [--publish] [--version-name <name>] [--dry-run|--yes]` incrementally applies the local file: matched IDs are updated, missing local IDs are created, and remote IDs absent from the file are deleted. It does not clear/recreate the whole draft or recode it. Run `--dry-run` first; a local file exported with `rules pull` is preferred.
|
||||
- `seal-use rules clear [--corp <corpId>] [--dry-run|--yes]`
|
||||
|
||||
Approval run lightweight queries:
|
||||
|
||||
@@ -133,6 +133,8 @@ seal-use tool seal_approval_rule_update --corp <corpId> \
|
||||
|
||||
Do not send `scope` or `strictness` when they are unchanged. Fetch the draft again and verify the exact description, scope, strictness, and draft status before publishing.
|
||||
|
||||
For a complete local rule-set sync, `rules push` is incremental: it matches pulled rules by stable `id`, updates only changed fields, creates new entries, and removes entries omitted from the local file. It preserves matched rule IDs and does not recode the whole draft. Preview it with `rules push --dry-run` before applying.
|
||||
|
||||
## 9. Publish and verify
|
||||
|
||||
Publish only when explicitly authorized:
|
||||
|
||||
@@ -5,7 +5,7 @@ import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import { findSealTool, sealTools } from "./tools.js";
|
||||
import { buildRulePushPlan, findSealTool, sealTools } from "./tools.js";
|
||||
|
||||
type ToolHandler = (
|
||||
client: KyInstance,
|
||||
@@ -223,6 +223,87 @@ describe("sealTools", () => {
|
||||
expect(await countFakeRules(client)).toBe(0);
|
||||
});
|
||||
|
||||
test("plans incremental rule updates without rebuilding matched rules", () => {
|
||||
const plan = buildRulePushPlan(
|
||||
[
|
||||
{
|
||||
id: "rule-1",
|
||||
code: 1,
|
||||
description: "Keep receipts",
|
||||
scope: "travel",
|
||||
strictness: "MUST_FOLLOW"
|
||||
},
|
||||
{
|
||||
id: "rule-2",
|
||||
code: 2,
|
||||
description: "Keep attendee names",
|
||||
scope: "meal",
|
||||
strictness: "SHOULD_FOLLOW"
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
id: "rule-1",
|
||||
description: "Keep complete receipts",
|
||||
scope: "travel",
|
||||
strictness: "MUST_FOLLOW"
|
||||
},
|
||||
{
|
||||
description: "Require cost center",
|
||||
scope: "all",
|
||||
strictness: "SUGGESTION"
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
expect(plan.updated).toMatchObject([
|
||||
{ id: "rule-1", changedFields: ["description"] }
|
||||
]);
|
||||
expect(plan.created).toMatchObject([
|
||||
{ description: "Require cost center" }
|
||||
]);
|
||||
expect(plan.deleted).toMatchObject([{ id: "rule-2", code: 2 }]);
|
||||
expect(plan.unchanged).toEqual([]);
|
||||
});
|
||||
|
||||
test("matches legacy local rules by unique content when no id is present", () => {
|
||||
const plan = buildRulePushPlan(
|
||||
[{
|
||||
id: "rule-1",
|
||||
description: "Keep receipts",
|
||||
scope: "travel",
|
||||
strictness: "MUST_FOLLOW"
|
||||
}],
|
||||
[{
|
||||
description: "Keep receipts",
|
||||
scope: "travel",
|
||||
strictness: "MUST_FOLLOW"
|
||||
}]
|
||||
);
|
||||
|
||||
expect(plan.created).toEqual([]);
|
||||
expect(plan.updated).toEqual([]);
|
||||
expect(plan.deleted).toEqual([]);
|
||||
expect(plan.unchanged).toEqual([{ id: "rule-1" }]);
|
||||
});
|
||||
|
||||
test("rejects local rule ids that are absent from the current draft", () => {
|
||||
expect(() => buildRulePushPlan(
|
||||
[{
|
||||
id: "rule-1",
|
||||
description: "Keep receipts",
|
||||
scope: "travel",
|
||||
strictness: "MUST_FOLLOW"
|
||||
}],
|
||||
[{
|
||||
id: "stale-rule",
|
||||
description: "Keep receipts",
|
||||
scope: "travel",
|
||||
strictness: "MUST_FOLLOW"
|
||||
}]
|
||||
)).toThrow("stale-rule");
|
||||
});
|
||||
|
||||
test("includes approval run Langfuse bridge tools", () => {
|
||||
expect(sealTools.find((item) => item.name === "seal_approval_runs_search")).toBeDefined();
|
||||
expect(sealTools.find((item) => item.name === "seal_approval_runs_summary")).toBeDefined();
|
||||
|
||||
+114
-19
@@ -625,7 +625,7 @@ export const sealTools = [
|
||||
},
|
||||
{
|
||||
name: "seal_approval_rules_push",
|
||||
description: "将本地规则文件推送到远端草稿(本地调优通过后一键应用,支持直接发布为正式版本)",
|
||||
description: "将本地规则文件增量同步到远端草稿;按规则 ID 更新/删除并保留未变更规则,支持直接发布为正式版本",
|
||||
parameters: z.object({
|
||||
file: z.string().optional().describe("要推送的本地规则文件路径,默认为 ./rules.local.json"),
|
||||
dryRun: z.boolean().optional().describe("是否仅预览变更而不实际写入远端(默认 true,传 --yes 或 confirm: true 执行写入)"),
|
||||
@@ -650,6 +650,7 @@ export const sealTools = [
|
||||
const isDryRun = params.dryRun ?? !isConfirmed;
|
||||
|
||||
const before = await api.listApprovalRules(client);
|
||||
const plan = buildRulePushPlan(before.rules, local.rules);
|
||||
|
||||
if (isDryRun) {
|
||||
return {
|
||||
@@ -658,39 +659,42 @@ export const sealTools = [
|
||||
localFile: local.resolvedPath,
|
||||
localRulesCount: local.rules.length,
|
||||
remoteDraftCount: before.rules.length,
|
||||
changes: plan,
|
||||
impactCount: plan.created.length + plan.updated.length + plan.deleted.length,
|
||||
preservedRuleIds: [
|
||||
...plan.updated.map((change) => change.id),
|
||||
...plan.unchanged.map((rule) => rule.id)
|
||||
],
|
||||
warning: "Incremental mode preserves matched rule IDs and does not recode the draft rule set.",
|
||||
willPublish: Boolean(params.publish),
|
||||
message: "Preview mode. Use --yes or confirm=true to apply local rules to remote draft."
|
||||
message: "Preview mode. Use --yes or confirm=true to apply the listed incremental changes to the remote draft."
|
||||
};
|
||||
}
|
||||
|
||||
// Step 1: Clear existing draft rules
|
||||
let deleted = 0;
|
||||
for (const rule of before.rules) {
|
||||
for (const rule of plan.deleted) {
|
||||
await api.deleteApprovalRule(client, rule.id);
|
||||
deleted += 1;
|
||||
}
|
||||
|
||||
// Step 2: Create new rules from local
|
||||
const created: Array<{ id: string; description: string; code?: number }> = [];
|
||||
for (const rule of local.rules) {
|
||||
const strictness = rule.strictness?.toUpperCase()?.includes("SHOULD")
|
||||
? "SHOULD_FOLLOW"
|
||||
: rule.strictness?.toUpperCase()?.includes("SUGGEST")
|
||||
? "SUGGESTION"
|
||||
: "MUST_FOLLOW";
|
||||
|
||||
for (const rule of plan.created) {
|
||||
const newRule = await api.createApprovalRule(client, {
|
||||
description: rule.description,
|
||||
scope: rule.scope ?? "ALL",
|
||||
strictness
|
||||
strictness: normalizeRuleStrictness(rule.strictness)
|
||||
});
|
||||
created.push({ id: newRule.id, description: newRule.description, code: newRule.code });
|
||||
}
|
||||
|
||||
// Step 3: Recode
|
||||
await api.recodeApprovalRules(client);
|
||||
const updated: string[] = [];
|
||||
for (const change of plan.updated) {
|
||||
await api.updateApprovalRule(client, change.id, {
|
||||
description: change.rule.description,
|
||||
scope: change.rule.scope ?? "ALL",
|
||||
strictness: normalizeRuleStrictness(change.rule.strictness)
|
||||
});
|
||||
updated.push(change.id);
|
||||
}
|
||||
|
||||
// Step 4: Optional publish
|
||||
let publishedVersion: unknown = undefined;
|
||||
if (params.publish) {
|
||||
const dateStr = new Date().toISOString().slice(0, 10).replace(/-/g, "");
|
||||
@@ -702,8 +706,15 @@ export const sealTools = [
|
||||
corpId: context.corp.id,
|
||||
dryRun: false,
|
||||
localFile: local.resolvedPath,
|
||||
clearedDraftRules: deleted,
|
||||
deletedRules: plan.deleted.length,
|
||||
createdRules: created.length,
|
||||
updatedRules: updated.length,
|
||||
unchangedRules: plan.unchanged.length,
|
||||
preservedRuleIds: [
|
||||
...updated,
|
||||
...plan.unchanged.map((rule) => rule.id)
|
||||
],
|
||||
recoded: false,
|
||||
published: publishedVersion ?? false
|
||||
};
|
||||
}
|
||||
@@ -2184,6 +2195,90 @@ function pickRuleFields(rule: ApprovalRule, fields: readonly RuleField[]) {
|
||||
return output;
|
||||
}
|
||||
|
||||
type RulePushPlan = {
|
||||
created: LocalRuleItem[];
|
||||
updated: Array<{ id: string; rule: LocalRuleItem; changedFields: string[] }>;
|
||||
deleted: ApprovalRule[];
|
||||
unchanged: Array<Pick<ApprovalRule, "id">>;
|
||||
};
|
||||
|
||||
export function buildRulePushPlan(
|
||||
remoteRules: ApprovalRule[],
|
||||
localRules: LocalRuleItem[]
|
||||
): RulePushPlan {
|
||||
const remoteById = new Map(remoteRules.map((rule) => [rule.id, rule]));
|
||||
const usedRemoteIds = new Set<string>();
|
||||
const created: LocalRuleItem[] = [];
|
||||
const updated: RulePushPlan["updated"] = [];
|
||||
const unchanged: RulePushPlan["unchanged"] = [];
|
||||
|
||||
for (const localRule of localRules) {
|
||||
let remoteRule: ApprovalRule | undefined;
|
||||
if (localRule.id) {
|
||||
remoteRule = remoteById.get(localRule.id);
|
||||
if (!remoteRule) {
|
||||
throw new Error(
|
||||
`本地规则 ${localRule.id} 不存在于当前草稿。请先用 rules pull 获取当前草稿,再重新应用修改。`
|
||||
);
|
||||
}
|
||||
if (usedRemoteIds.has(localRule.id)) {
|
||||
throw new Error(`本地规则文件包含重复的规则 ID:${localRule.id}`);
|
||||
}
|
||||
} else {
|
||||
const matches = remoteRules.filter(
|
||||
(candidate) => !usedRemoteIds.has(candidate.id) && rulesHaveSameContent(candidate, localRule)
|
||||
);
|
||||
if (matches.length > 1) {
|
||||
throw new Error(
|
||||
`本地规则“${localRule.description}”无法唯一匹配远端草稿,请在规则文件中补充稳定的 id。`
|
||||
);
|
||||
}
|
||||
remoteRule = matches[0];
|
||||
}
|
||||
|
||||
if (!remoteRule) {
|
||||
created.push(localRule);
|
||||
continue;
|
||||
}
|
||||
|
||||
usedRemoteIds.add(remoteRule.id);
|
||||
const changedFields = ruleChangedFields(remoteRule, localRule);
|
||||
if (changedFields.length) {
|
||||
updated.push({ id: remoteRule.id, rule: localRule, changedFields });
|
||||
} else {
|
||||
unchanged.push({ id: remoteRule.id });
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
created,
|
||||
updated,
|
||||
deleted: remoteRules.filter((rule) => !usedRemoteIds.has(rule.id)),
|
||||
unchanged
|
||||
};
|
||||
}
|
||||
|
||||
function rulesHaveSameContent(remoteRule: ApprovalRule, localRule: LocalRuleItem): boolean {
|
||||
return remoteRule.description === localRule.description.trim() &&
|
||||
remoteRule.scope === (localRule.scope ?? "ALL") &&
|
||||
remoteRule.strictness === normalizeRuleStrictness(localRule.strictness);
|
||||
}
|
||||
|
||||
function ruleChangedFields(remoteRule: ApprovalRule, localRule: LocalRuleItem): string[] {
|
||||
const changed: string[] = [];
|
||||
if (remoteRule.description !== localRule.description.trim()) changed.push("description");
|
||||
if (remoteRule.scope !== (localRule.scope ?? "ALL")) changed.push("scope");
|
||||
if (remoteRule.strictness !== normalizeRuleStrictness(localRule.strictness)) changed.push("strictness");
|
||||
return changed;
|
||||
}
|
||||
|
||||
function normalizeRuleStrictness(value?: string): string {
|
||||
const normalized = value?.toUpperCase() ?? "MUST_FOLLOW";
|
||||
if (normalized.includes("SHOULD")) return "SHOULD_FOLLOW";
|
||||
if (normalized.includes("SUGGEST")) return "SUGGESTION";
|
||||
return "MUST_FOLLOW";
|
||||
}
|
||||
|
||||
export type SimulationBatchCreateInput = {
|
||||
mode?: string;
|
||||
sourceSystem?: string;
|
||||
|
||||
Reference in new issue
Block a user