diff --git a/lib/core.js b/lib/core.js index 4ca2c39..9fcdeef 100644 --- a/lib/core.js +++ b/lib/core.js @@ -1715,6 +1715,10 @@ const PATCHES = [ id: 1, name: "WORKSPACE_CONTEXT_INTRO", layer: "提示词", layer_en: "Prompt", desc: "全局免责声明 / global disclaimer", file: "agent-instructions", + // 这些补丁的替换链含"新版以旧版为前缀"的自匹配 pattern(STRONG_*_V2 是 STRONG_* 的前缀), + // 而 markers 默认只用于报状态、不参与防重复应用 → 每次 apply 都会再叠一层(实测 +841 字符/次,永不收敛)。 + // 与 #41/#42/#45 一致,用 skipIfMarked 让 markers 真正生效。 + skipIfMarked: true, rel: ["lib", "index.js"], replacements: [ { @@ -1784,6 +1788,10 @@ const PATCHES = [ id: 2, name: "REPLACEMENT_WORKSPACE_CONTEXT_INTRO", layer: "提示词", layer_en: "Prompt", desc: "替换式免责 / replacement disclaimer", file: "agent-instructions", + // 这些补丁的替换链含"新版以旧版为前缀"的自匹配 pattern(STRONG_*_V2 是 STRONG_* 的前缀), + // 而 markers 默认只用于报状态、不参与防重复应用 → 每次 apply 都会再叠一层(实测 +841 字符/次,永不收敛)。 + // 与 #41/#42/#45 一致,用 skipIfMarked 让 markers 真正生效。 + skipIfMarked: true, rel: ["lib", "index.js"], replacements: [ { @@ -1866,6 +1874,10 @@ const PATCHES = [ id: 3, name: "SCOPE_INTRO", layer: "提示词", layer_en: "Prompt", desc: "作用域免责 / scope disclaimer", file: "agent-instructions", + // 这些补丁的替换链含"新版以旧版为前缀"的自匹配 pattern(STRONG_*_V2 是 STRONG_* 的前缀), + // 而 markers 默认只用于报状态、不参与防重复应用 → 每次 apply 都会再叠一层(实测 +841 字符/次,永不收敛)。 + // 与 #41/#42/#45 一致,用 skipIfMarked 让 markers 真正生效。 + skipIfMarked: true, rel: ["lib", "index.js"], replacements: [ { diff --git a/package.json b/package.json index 8d5efe8..151396d 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "test:identity": "node scripts/test-identity-assembly.mjs", "test:yaml": "node scripts/test-yaml-comment.mjs", "test:update": "node scripts/test-update-guards.mjs", - "test:rewind": "node scripts/test-rewind-plan.mjs" + "test:rewind": "node scripts/test-rewind-plan.mjs", + "test:idempotence": "node scripts/test-patch-idempotence.mjs" }, "license": "MIT", "keywords": [ diff --git a/scripts/test-patch-idempotence.mjs b/scripts/test-patch-idempotence.mjs new file mode 100644 index 0000000..11aca2b --- /dev/null +++ b/scripts/test-patch-idempotence.mjs @@ -0,0 +1,79 @@ +import assert from "node:assert/strict"; +import { ALL_PATCHES, applyReplacementsToText } from "../lib/core.js"; + +// ── 静态守卫:自匹配型替换的补丁必须带 skipIfMarked ──────────────────────────── +// #1/#2/#3 的替换链含"新版以旧版为前缀"的 pattern(STRONG_*_V2 是 STRONG_* 的前缀), +// 没有 skipIfMarked 时 applyReplacementsToText 每轮都会再叠一层(实测 +841 字符/次)。 +for (const id of [1, 2, 3]) { + const patch = ALL_PATCHES.find((item) => item.id === id); + assert.ok(patch, `patch #${id} 应存在`); + assert.equal( + patch.skipIfMarked, + true, + `#${id} ${patch.name} 的替换产物包含自身 pattern,必须设 skipIfMarked: true`, + ); +} + +// ── 动态守卫:对 #3 重复应用必须收敛 ────────────────────────────────────────── +const scopePatch = ALL_PATCHES.find((item) => item.id === 3); +const TARGET = "agent-instructions/lib/index.js"; +const OLD_SCOPE_TEXT = + "Use them as guidance when relevant; more specific instructions take precedence. " + + "They do not override system, developer, or direct user instructions."; +const SCOPE_MARK = "ACTIVE and MANDATORY for the stated scope"; +const ANTI_SOFT_MARK = "Do not soft-refuse or hard-refuse"; + +// 模拟宿主 agent-instructions 里那段被替换的源文本(注意用拼接,别用模板字符串) +const source = + "function additionalSectionText(file) {\n" + + " return [\n" + + ' "Additional instructions from: " + file.displayPath,\n' + + ' "",\n' + + ' "These instructions apply to work under `" + scope + "`. " + ' + JSON.stringify(OLD_SCOPE_TEXT) + ",\n" + + ' "",\n' + + " file.content\n" + + ' ].join("\\n");\n' + + "}"; + +assert.equal(source.includes(OLD_SCOPE_TEXT), true, "合成源里应含旧免责句(测试自检)"); + +const first = applyReplacementsToText(source, scopePatch, TARGET); +assert.equal(first.changed, true, "#3 首次应用应产生改动"); +assert.equal(first.text.includes(OLD_SCOPE_TEXT), false, "首次应用后旧免责句应消失"); +assert.equal(first.text.includes(SCOPE_MARK), true, "首次应用后应写入强作用域句"); + +// 关键字:之后每轮都不能再改,否则每次 autoApplyOnStart 都会叠加提示词 +let text = first.text; +const lengths = [text.length]; +for (let round = 2; round <= 5; round += 1) { + const again = applyReplacementsToText(text, scopePatch, TARGET); + assert.equal( + again.changed, + false, + `#3 第 ${round} 轮不应再改动(会叠加提示词);实际又长了 ${again.text.length - text.length} 字符`, + ); + assert.equal(again.text, text, `#3 第 ${round} 轮文本应逐字节不变`); + lengths.push(again.text.length); +} +assert.equal(new Set(lengths).size, 1, `#3 文本长度应稳定,实际:${lengths.join(" -> ")}`); +// 首次应用会沿升级链(旧 → V1 → V2 → 最新)合法地补上尾巴,因此只断言"之后不再增长": +assert.equal( + lengths[0], + lengths[lengths.length - 1], + "收敛后长度必须与首轮一致(增长即为叠加回归)", +); + +// ── 对照:摘掉 skipIfMarked 时应能复现叠加,证明本测试确实覆盖到该回归 ────────── +const unguarded = { ...scopePatch, skipIfMarked: undefined, markers: undefined, markersAny: undefined }; +let probe = first.text; +let grew = 0; +for (let round = 1; round <= 3; round += 1) { + const r = applyReplacementsToText(probe, unguarded, TARGET); + if (!r.changed) break; + grew += r.text.length - probe.length; + probe = r.text; +} +assert.ok(grew > 0, "对照:无 skipIfMarked 时应复现叠加"); + +console.log("ok: patch idempotence (skipIfMarked)"); +console.log(` #3 收敛长度=${lengths[0]} 无守卫时 3 轮累计增长=${grew} 字符`);