diff --git a/lib/core.js b/lib/core.js index 72b4878..b084eb7 100644 --- a/lib/core.js +++ b/lib/core.js @@ -3699,6 +3699,57 @@ const NEW_TOOL_PATCHES = [ ], markers: ["[dsh-purge] system prompt replaces the first node"], }, + { + id: 66, + name: "SYSTEM_PROMPT_RENDER_GUARD", + layer: "代码", + layer_en: "Code", + desc: "renderPrompt 对 undefined section.text 兜底,避免第二轮 gateway/internal / guard undefined section text in renderPrompt", + file: ["system-prompt", "system-prompt-nested"], + replacements: [ + { + pattern: + "function renderPrompt(assembly) {\n" + + '\treturn assembly.sections.map((section) => section.interpolate === false ? section.text : interpolate(section, assembly.variables, "section")).filter((text) => text.length > 0).join("\\n\\n");\n' + + "}", + replace: + "function renderPrompt(assembly) {\n" + + '\treturn assembly.sections.map((section) => section.interpolate === false ? section.text : interpolate(section, assembly.variables, "section")).filter((text) => typeof text === "string" && text.length > 0).join("\\n\\n"); // [dsh-purge] guard undefined section text\n' + + "}", + }, + { + pattern: "function interpolate(input, variables, kind) {\n\tconst text = input.text;", + replace: + "function interpolate(input, variables, kind) {\n" + + '\tconst text = typeof input.text === "string" ? input.text : ""; // [dsh-purge] guard undefined section text', + }, + { + pattern: "})).filter((section) => section.text.length > 0);", + replace: + '})).filter((section) => typeof section.text === "string" && section.text.length > 0); // [dsh-purge] guard undefined section text', + }, + ], + markers: ["[dsh-purge] guard undefined section text"], + }, + { + id: 67, + name: "AGENT_LOOP_MESSAGES_GUARD", + layer: "代码", + layer_en: "Code", + desc: "decision.messages 缺省时按空数组处理,避免第二轮 gateway/internal / treat missing decision.messages as empty", + file: ["agent-loop"], + replacements: [ + { + pattern: + "\t\t\t\tif (turnEnds && decision.messages.length === 0) break;\n" + + "\t\t\t\tif (phase.step === 0 && decision.messages.length === 0) {\n", + replace: + "\t\t\t\tif (turnEnds && (decision.messages?.length ?? 0) === 0) break; // [dsh-purge] guard undefined messages\n" + + "\t\t\t\tif (phase.step === 0 && (decision.messages?.length ?? 0) === 0) { // [dsh-purge] guard undefined messages\n", + }, + ], + markers: ["[dsh-purge] guard undefined messages"], + }, { id: 56, name: "REWIND_HIDE_CUT_ONLY", diff --git a/lib/identity.js b/lib/identity.js index d062a78..0df55aa 100644 --- a/lib/identity.js +++ b/lib/identity.js @@ -582,44 +582,27 @@ function withOwnServers(injectText) { } } -/** 同一会话内系统提示文本钉死,避免每步改写触发模型二次读人设后软拒绝。 */ -const pinnedSectionsBySession = new Map(); - -function sessionPinId(context) { - const session = sessionOfContext(context); - const id = session?.id ?? session?.header?.id; - return id == null || id === "" ? "" : String(id); -} - -function clonePinnedSections(sections) { - return sections.map((section) => { - if (!section || typeof section !== "object") return section; - return { ...section, text: typeof section.text === "string" ? section.text : section.text }; - }); -} - -function sectionsFingerprint(sections) { - return sections - .map((section) => `${String(section?.name || "")}\n${typeof section?.text === "string" ? section.text : ""}`) - .join("\n\u0001\n"); -} - -function pinHasRoleOrCard(sections) { - return sections.some((section) => { - const name = String(section?.name || ""); - return name === "dsh-purge:redteam-role" || name === "dsh-purge:objective"; - }); -} - -export function clearPinnedSystemPrompt(sessionId) { - if (sessionId == null || sessionId === "") return; - pinnedSectionsBySession.delete(String(sessionId)); +/** 组装结果里每段 text 必须是字符串;否则官方 renderPrompt 的 text.length 会直接炸掉。 */ +function sanitizeAssemblySections(sections) { + const list = Array.isArray(sections) ? sections : []; + const out = []; + for (const section of list) { + if (!section || typeof section !== "object") continue; + if (typeof section.text === "function") { + out.push(section); + continue; + } + out.push({ + ...section, + text: typeof section.text === "string" ? section.text : "", + }); + } + return out; } export function rewritePromptAssembly(assembled, fallbackInject = "", options = {}) { if (!assembled || !Array.isArray(assembled.sections)) return assembled; const opts = normalizeRewriteOptions(fallbackInject, options); - const pinId = sessionPinId(opts.context); const rest = []; const purge = []; for (const section of assembled.sections) { @@ -659,21 +642,8 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options = ? insertRoleSection(insertCardSection(sections, opts.cardText), opts.roleText) : withOps; } - sections = leadWithInject(sections, injectText); - - if (pinId && injectText.trim()) { - const pinned = pinnedSectionsBySession.get(pinId); - const nextHasRole = Boolean(opts.roleText || opts.cardText) || pinHasRoleOrCard(sections); - if (pinned && !(nextHasRole && !pinHasRoleOrCard(pinned))) { - assembled.sections = clonePinnedSections(pinned); - return assembled; - } - const freeze = clonePinnedSections(sections); - if (!pinned || sectionsFingerprint(freeze) !== sectionsFingerprint(pinned)) { - pinnedSectionsBySession.set(pinId, freeze); - } - } - - assembled.sections = sections; + assembled.sections = sanitizeAssemblySections(leadWithInject(sections, injectText)); + if (!Array.isArray(assembled.contexts)) assembled.contexts = []; + if (!Array.isArray(assembled.tools)) assembled.tools = []; return assembled; } diff --git a/lib/index.js b/lib/index.js index 87cca8b..72991a0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,7 +8,6 @@ import * as reapply from "./reapply.js"; import * as officialUpdate from "./official-update.js"; import { GLOBAL_PROMPT_ORDER, - clearPinnedSystemPrompt, isMnemonPluginMessage, isRedteamSession, rewritePromptAssembly, @@ -217,15 +216,17 @@ function readInjectFallback(context) { } function installMnemonMessageStrip(ctx, config = {}) { - if (config.stripMnemon === false) return; if (typeof ctx.on !== "function") return; if (ctx.__dshPurgeMnemonStrip) return; ctx.__dshPurgeMnemonStrip = true; + const strip = config.stripMnemon !== false; const wrap = async (_payload, next) => { const decision = await next(); - if (!decision || !Array.isArray(decision.messages)) return decision; - const messages = decision.messages.filter((message) => !isMnemonPluginMessage(message)); - if (messages.length === decision.messages.length) return decision; + if (!decision) return decision; + // agent-loop 会读 decision.messages.length;缺数组时第二轮直接 gateway/internal 炸掉。 + const incoming = Array.isArray(decision.messages) ? decision.messages : []; + const messages = strip ? incoming.filter((message) => !isMnemonPluginMessage(message)) : incoming; + if (messages.length === incoming.length && Array.isArray(decision.messages)) return decision; return { ...decision, messages }; }; const late = () => { @@ -329,12 +330,6 @@ function installIdentityOverride(ctx, config = {}) { }; // 只挂一次。不改 contexts,不改 pre-step 白名单,梁神 phase-1 和其它插件的消息原样保留。 ctx.on("system-prompt/assemble", hook, { global: true, prepend: true }); - try { - ctx.on("session/created", (session) => { - const id = session?.id ?? session?.header?.id; - if (id != null && id !== "") clearPinnedSystemPrompt(id); - }); - } catch { /* ignore */ } // 0.1.5 起 complete:true 在 waterfall 之后用调用前的快照盖掉 sections。 // 换宿主版本时文件补丁还没写上,或换插件版本时旧钩子把 dsh-purge 段删掉,注入都会丢。 // 包住 assemble 的返回值,按 prompt-inject.md 再补回。 diff --git a/lib/redteam/ui.js b/lib/redteam/ui.js index 55d2895..b09b0bd 100644 --- a/lib/redteam/ui.js +++ b/lib/redteam/ui.js @@ -791,6 +791,7 @@ export function apply(ctx) { kind: 'reject', reason: status.message || '环境未适配:请打开 dsh-purge → 环境适配配置工具,或确认暂不配置后再发送。', + messages: [], } } } diff --git a/release-notes.md b/release-notes.md index 9ad5fb3..0f476cc 100644 --- a/release-notes.md +++ b/release-notes.md @@ -6,7 +6,8 @@ - 修复注入不稳定:第二轮或任务做到一半人设失效、被当成 `project_instructions` 拆掉的问题。 - 工作区替换稿免责声明补丁不再被主 intro 的完成标记冒充跳过;替换稿与作用域 intro 与主 intro 对齐。 - 进模型前剥掉外层 ChatML / `` 壳,避免模型把真实 system 注入当成项目说明。 -- 同一会话内系统提示段首次注入成功后钉死,减少每步改写触发的二次读人设。 +- 组装后的系统提示段统一保证 `text` 为字符串;不再会话钉死整段 sections,避免第二轮 `renderPrompt` 读 `undefined.length` 直接死掉并报 `(gateway/internal)`。 +- `agent/pre-step` 始终把 `decision.messages` 规范成数组,避免第二轮发送时读空 messages 炸掉。 - 清洗进度:未定位宿主时不再显示吓人的 `0/63`;本机不需要或组件未装的项计入已就绪,避免假的 `48/63`。 - 点「应用」就会写入默认提示词并注入,框空时用内置默认,不必先点保存。 - 更新并重新加密默认提示词。 @@ -18,7 +19,8 @@ - Fix unstable inject: persona dropping on the second turn or mid-task, including cases where the model treated the system inject as `project_instructions` and rejected it. - The replacement workspace-intro patch no longer skips because of the main intro's completion marker; replacement and scope intros match the main intro. - Strip outer ChatML / `` wrappers before the text reaches the model, so a real system inject is not read as project instructions. -- Pin system-prompt sections for a session after the first successful inject, so per-step rewrites are less likely to trigger a second identity read. +- Sanitize assembled system-prompt sections so `text` is always a string; stop pinning full sections for a session, which could make the second turn crash in `renderPrompt` on `undefined.length` with `(gateway/internal)`. +- Always normalize `decision.messages` to an array in `agent/pre-step`, so a second send does not die on a missing messages list. - Clean progress: do not show a scary `0/63` before the host is located; items not needed on this install count as ready, so a fake `48/63` no longer appears. - Apply writes and injects the default prompt; an empty box uses the bundled default, so a separate Save is not required. - Refresh and re-encrypt the bundled default prompt.