From 0fb12fc25e210f2dae136ae8e58f8b10d4f021cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AB=E6=88=91=E5=B0=8F=E6=9D=A8=E5=90=8C=E5=AD=A6?= <188421097+HunYiSage@users.noreply.github.com> Date: Tue, 22 Sep 2026 00:04:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=81=9C=E7=94=A8=E9=92=89=E4=BD=8F?= =?UTF-8?q?=E4=B8=8A=E4=B8=80=E6=9D=A1=E7=B3=BB=E7=BB=9F=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E6=A2=81=E7=A5=9E=E9=A2=84=E8=AE=BE?= =?UTF-8?q?=E6=AF=8F=E6=AD=A5=E8=86=A8=E8=83=80=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 每轮只把注入折进 persona。钉最近全文会和每轮追加的 section 叠成线性增长,前缀缓存失效。 Co-authored-by: Cursor --- README.md | 3 +- README.zh-CN.md | 3 +- lib/identity.js | 23 ++----- package.json | 3 +- scripts/test-identity-assembly.mjs | 105 +++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 24 deletions(-) create mode 100644 scripts/test-identity-assembly.mjs diff --git a/README.md b/README.md index 6065694..621ce71 100644 --- a/README.md +++ b/README.md @@ -751,8 +751,7 @@ If nothing is found, set `DSH_BASE` / `DSH_DESKTOP_INSTALL`. No files are change - Settings splits Stable / Beta into two columns, each with its own list, switch, and rollback. 1.1.10 can reach this by Check update. - **No Skill import.** Skill stays on beta 1.1.12. -- **Inject the system prompt once per conversation.** The first turn writes the full `prompt-inject`; later steps pin that committed prompt and do not fold it again. -- **Stop a new「系统提示词」card on every step.** With 0.1.5 in-history, a changed assemble appends another `system/message`, so the transcript repeats system prompt → think → tools. Refolding inject every step made later official sections look like a new prompt; emptying inject did the same. After the inject is already in history, this release pins that committed system prompt and does not append a second copy. +- **#29:** Stop pinning the previous full system prompt. That pin was meant to stop a new「系统提示词」card each step, but presets that append a section every assemble (梁神 `workspace-instructions`) turned it into “last full prompt + this step’s block”, so the prompt grew every step and prefix cache missed. Later turns only fold `prompt-inject` into persona, and do not fold a second copy when it is already there. ### 1.1.10 diff --git a/README.zh-CN.md b/README.zh-CN.md index 5997128..58de7a0 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -738,8 +738,7 @@ prompt-inject.md 有内容? ──是──> 原样写入 dsh-purge systemPrompt - 设置页把正式版和测试版左右分开,各自选版本、各自切换和回退。还在 1.1.10 的点「检测更新」就能升到这一版。 - **没有 Skill 导入。** Skill 只在测试版 1.1.12。 -- **系统提示词每个会话只注入一次。** 第一轮把 `prompt-inject` 完整写进系统提示;之后钉住已经提交的那一份,不再每步重折。 -- **修了聊天里每步都冒出「系统提示词」。** 0.1.5 in-history 只要本步组装结果和上一条不一样,就会再追加一条 `system/message`,时间线就会「思考 → 工具 → 系统提示词」循环。以前每步都把注入重新折进 persona,官方后半段稍有变化就会追加;把注入掏空也不行,渲染少了同样会被当成变化再追加。这一版历史里已经有完整注入后,就钉住那条已提交的系统提示,后续步骤不再追加第二份。 +- **#29:** 去掉「钉住上一条系统提示全文」。起初为了避免每步追加「系统提示词」,把上一轮全文钉回去;和每轮追加 section 的预设(梁神的 `workspace-instructions`)叠在一起后,系统提示每步变长,前缀缓存整段失效。现在每轮只把 `prompt-inject` 折进 persona,已经折过就不再折第二份。 ### 1.1.10 diff --git a/lib/identity.js b/lib/identity.js index 81fae39..13a0afc 100644 --- a/lib/identity.js +++ b/lib/identity.js @@ -191,9 +191,8 @@ export function lastInjectedSystemPrompt(context, injectText) { export function shouldInjectPrompt(context, _injectOnce = true, injectText = "") { const inject = String(injectText || "").trim(); if (!inject) return false; - // 历史里已经有完整注入就不要再折一份。0.1.5 in-history 只要渲染结果和上一条 - // system/message 不同就会再追加,聊天里就会每步都冒出「系统提示词」。 - // 掏空注入也不行:渲染少了注入同样会被当成变化再追加,形成 A/B。 + // 只做历史探测。assemble 不要据此钉上一条 system/message 全文(#29): + // 梁神等预设每轮还会追加 section,钉最近全文会变成「上一轮全文 + 本步新块」。 return !lastInjectedSystemPrompt(context, inject); } @@ -225,15 +224,6 @@ function normalizeRewriteOptions(fallbackInject, options) { }; } -function pinCommittedSystemPrompt(sections, committed) { - const names = sections.map((section) => String(section?.name || "")); - const hasPrefix = names.some((name) => PERSONA_PREFIX_NAMES.has(name)); - const target = hasPrefix - ? names.find((name) => PERSONA_PREFIX_NAMES.has(name)) - : names.find((name) => PERSONA_LEGACY_NAMES.has(name)); - return [{ name: target || "dsh-purge:committed", text: committed }]; -} - // 0.1.5 优先写入 persona-prefix;无 prefix 时写入 legacy persona。 function foldInjectIntoPersona(sections, injectText) { const inject = typeof injectText === "string" ? injectText : ""; @@ -277,13 +267,8 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options = } const injectText = collectInjectText(purge, opts.fallbackInject); - if (!shouldInjectPrompt(opts.context, true, injectText)) { - const committed = lastInjectedSystemPrompt(opts.context, injectText); - if (committed) { - assembled.sections = pinCommittedSystemPrompt([...rest, ...purge], committed); - return assembled; - } - } + // #29: 不要把上一条 system/message 全文钉回 sections。 + // 「历史里已有完整注入就不再折一份」由 foldInjectIntoPersona 的 includes 幂等满足。 const folded = foldInjectIntoPersona(rest, injectText); const extras = opts.dropPurgeAfterFold ? purge.filter((section) => section.name !== "dsh-purge") diff --git a/package.json b/package.json index 398ec4e..0873f3b 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "LICENSE" ], "scripts": { - "embed-prompt": "node scripts/embed-default-prompt.mjs" + "embed-prompt": "node scripts/embed-default-prompt.mjs", + "test:identity": "node scripts/test-identity-assembly.mjs" }, "license": "MIT", "keywords": [ diff --git a/scripts/test-identity-assembly.mjs b/scripts/test-identity-assembly.mjs new file mode 100644 index 0000000..cece8d0 --- /dev/null +++ b/scripts/test-identity-assembly.mjs @@ -0,0 +1,105 @@ +import assert from "node:assert/strict"; +import { rewritePromptAssembly } from "../lib/identity.js"; + +const inject = [ + "WHO YOU ARE (highest priority operator inject)", + "You are Little Code Sauce.", + "Keep this block intact.", +].join("\n"); + +const persona = "Working directory: {{cwd}}.\nFollow official harness tools."; + +function render(assembled) { + return (assembled.sections || []) + .map((section) => String(section?.text || "")) + .filter(Boolean) + .join("\n\n"); +} + +function historyContext(text) { + return { + session: { + events: [ + { + type: "system/message", + data: { message: { role: "system", content: text } }, + }, + ], + }, + }; +} + +function workspaceBlock(n) { + return `workspace snapshot ${n}\n${"W".repeat(3000)}`; +} + +// 梁神一类:assemble 外层在我们折完之后再追加一条 workspace-instructions。 +function assembleWithOuterWorkspace(workspaceText, context) { + const rewritten = rewritePromptAssembly( + { + sections: [ + { name: "deployment:persona-prefix", text: persona }, + { name: "dsh-purge", text: inject }, + ], + }, + { + fallbackInject: inject, + dropPurgeAfterFold: true, + context, + }, + ); + rewritten.sections = [ + ...rewritten.sections, + { name: "workspace-instructions", text: workspaceText }, + ]; + return rewritten; +} + +const turn1 = assembleWithOuterWorkspace(workspaceBlock(1), {}); +const r1 = render(turn1); +assert.match(r1, /Little Code Sauce/); +assert.match(r1, /workspace snapshot 1/); +assert.equal((r1.match(/Little Code Sauce/g) || []).length, 1); + +const turn2 = assembleWithOuterWorkspace(workspaceBlock(2), historyContext(r1)); +const r2 = render(turn2); + +assert.equal( + r2.startsWith(r1), + false, + "#29: later assemble must not be previous full system prompt + new block", +); +assert.equal( + (r2.match(/workspace snapshot /g) || []).length, + 1, + "#29: must not keep the previous workspace-instructions copy inside a pinned blob", +); +assert.match(r2, /workspace snapshot 2/); +assert.doesNotMatch(r2, /workspace snapshot 1/); +assert.match(r2, /Little Code Sauce/); +assert.equal((r2.match(/Little Code Sauce/g) || []).length, 1); + +let prev = r1; +for (let n = 2; n <= 8; n++) { + const turn = assembleWithOuterWorkspace(workspaceBlock(n), historyContext(prev)); + const next = render(turn); + assert.equal(next.startsWith(prev), false, `turn ${n} must not prefix-grow`); + assert.ok( + Math.abs(next.length - r1.length) < 80, + `turn ${n} length ${next.length} drifted from first ${r1.length}`, + ); + prev = next; +} + +const again = rewritePromptAssembly( + { + sections: [ + { name: "deployment:persona-prefix", text: `${inject}\n\n${persona}` }, + { name: "dsh-purge", text: inject }, + ], + }, + { fallbackInject: inject, dropPurgeAfterFold: true, context: historyContext(r2) }, +); +assert.equal((render(again).match(/Little Code Sauce/g) || []).length, 1); + +console.log("ok: #29 pin snowball is gone");