mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-30 17:50:22 +08:00
fix: 发布 1.1.38,红队守住目标并修好模式不显示
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
da980e5730
commit
701b4188cd
31 files changed
+1037
-949
No files matched your search
+52
-8
@@ -116,23 +116,61 @@ function redteamOpsBody(text) {
|
||||
return raw.slice(at).trim();
|
||||
}
|
||||
|
||||
function sessionOfContext(context) {
|
||||
return context?.agent?.session || context?.session || null;
|
||||
}
|
||||
|
||||
export function isRedteamSubagent(context) {
|
||||
const header = sessionOfContext(context)?.header;
|
||||
return header?.origin === "subagent" && header?.agentPreset === "redteam";
|
||||
}
|
||||
|
||||
/**
|
||||
* 主模式仍只有清洗提示词。
|
||||
* 红队模式把同一份清洗提示词放在最前,后面只接派活和操作说明,不保留红队身份句。
|
||||
* 红队指挥:清洗稿、目标卡、运行环境前提,不保留红队身份句。
|
||||
* 红队子会话:清洗稿、目标卡、角色稿、运行环境前提。roleText 为空时与指挥相同。
|
||||
*/
|
||||
function attachInjectToRedteamPersona(sections, inject) {
|
||||
function attachInjectToRedteamPersona(sections, inject, roleText, cardText) {
|
||||
let attached = false;
|
||||
const role = typeof roleText === "string" ? roleText.trim() : "";
|
||||
const card = typeof cardText === "string" ? cardText.trim() : "";
|
||||
const next = sections.map((section) => {
|
||||
const ops = redteamOpsBody(section?.text);
|
||||
if (ops == null) return section;
|
||||
attached = true;
|
||||
const text = inject ? `${inject}\n\n${ops}` : ops;
|
||||
return { ...section, text, order: -10000 };
|
||||
const parts = [];
|
||||
if (inject) parts.push(inject);
|
||||
if (card) parts.push(card);
|
||||
if (role) parts.push(role);
|
||||
parts.push(ops);
|
||||
return { ...section, text: parts.join("\n\n"), order: -10000 };
|
||||
});
|
||||
if (!attached) return sections;
|
||||
return next.filter((section) => section?.name !== "dsh-purge");
|
||||
}
|
||||
|
||||
function insertCardSection(sections, cardText) {
|
||||
const card = typeof cardText === "string" ? cardText.trim() : "";
|
||||
if (!card) return sections;
|
||||
if (sections.some((section) => String(section?.text || "").includes(card))) return sections;
|
||||
const section = { name: "dsh-purge:objective", text: card, order: -9995 };
|
||||
const purgeAt = sections.findIndex((item) => item?.name === "dsh-purge");
|
||||
const next = sections.slice();
|
||||
next.splice(purgeAt >= 0 ? purgeAt + 1 : 0, 0, section);
|
||||
return next;
|
||||
}
|
||||
|
||||
function insertRoleSection(sections, roleText) {
|
||||
const role = typeof roleText === "string" ? roleText.trim() : "";
|
||||
if (!role) return sections;
|
||||
if (sections.some((section) => String(section?.text || "").includes(role))) return sections;
|
||||
const section = { name: "dsh-purge:redteam-role", text: role, order: -9990 };
|
||||
const purgeAt = sections.findIndex((item) => item?.name === "dsh-purge");
|
||||
const next = sections.slice();
|
||||
next.splice(purgeAt >= 0 ? purgeAt + 1 : 0, 0, section);
|
||||
return next;
|
||||
}
|
||||
|
||||
function stripIdentityPrefix(text) {
|
||||
let rest = String(text || "").trim();
|
||||
if (rest.startsWith(IDENTITY_CARD_PREFIX)) {
|
||||
@@ -241,6 +279,8 @@ function normalizeRewriteOptions(fallbackInject, options) {
|
||||
dropMnemon: fallbackInject.dropMnemon !== false,
|
||||
dropPurgeAfterFold: fallbackInject.dropPurgeAfterFold !== false,
|
||||
context: fallbackInject.context,
|
||||
roleText: typeof fallbackInject.roleText === "string" ? fallbackInject.roleText : "",
|
||||
cardText: typeof fallbackInject.cardText === "string" ? fallbackInject.cardText : "",
|
||||
};
|
||||
}
|
||||
return {
|
||||
@@ -248,6 +288,8 @@ function normalizeRewriteOptions(fallbackInject, options) {
|
||||
dropMnemon: options?.dropMnemon !== false,
|
||||
dropPurgeAfterFold: options?.dropPurgeAfterFold !== false,
|
||||
context: options?.context,
|
||||
roleText: typeof options?.roleText === "string" ? options.roleText : "",
|
||||
cardText: typeof options?.cardText === "string" ? options.cardText : "",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -294,6 +336,9 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
|
||||
}
|
||||
|
||||
const injectText = collectInjectText(purge, opts.fallbackInject);
|
||||
const redteamChild = isRedteamSubagent(opts.context);
|
||||
const roleText = redteamChild ? opts.roleText : "";
|
||||
const cardText = opts.cardText.trim();
|
||||
// #29: 不要把上一条 system/message 全文钉回 sections。
|
||||
// 默认留下 dsh-purge 段。0.1.5 在 waterfall 之后若 complete:true,只认这段把注入抄进最终提示词;
|
||||
// 提前折进 persona 再删掉本段,重启加载该逻辑后注入会丢。
|
||||
@@ -301,9 +346,8 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
|
||||
const extras = opts.dropPurgeAfterFold
|
||||
? purge.filter((section) => section.name !== "dsh-purge")
|
||||
: purge;
|
||||
assembled.sections = ensureFullInject(
|
||||
attachInjectToRedteamPersona([...folded, ...extras], injectText),
|
||||
injectText,
|
||||
);
|
||||
const withPersona = attachInjectToRedteamPersona([...folded, ...extras], injectText, roleText, cardText);
|
||||
const withRole = insertRoleSection(ensureFullInject(withPersona, injectText), roleText);
|
||||
assembled.sections = redteamChild ? insertCardSection(withRole, cardText) : withRole;
|
||||
return assembled;
|
||||
}
|
||||
Reference in new issue
Block a user