fix: 0.2.0 把注入打进原生 system 槽,子代理走同一条 assemble

openai-completions / anthropic-messages 不再塞 last user;子代理 applyChildComposition 后走同一套 fold 与满权限钉钉。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
叫我小杨同学andCursor committed 2026-10-16 13:59:28 +08:00
1 parent e1d8e59087
commit 0a04bee3a4
4 files changed
+402 -29

No files matched your search

+66 -12
View File
@@ -127,6 +127,67 @@ function ensureFullInject(sections, inject) {
];
}
const IDENTITY_CARD_PREFIX = "WHO YOU ARE (highest priority";
function isPersonaFoldTarget(name, preferPrefix) {
if (preferPrefix) return PERSONA_PREFIX_NAMES.has(name);
return PERSONA_PREFIX_NAMES.has(name) || PERSONA_LEGACY_NAMES.has(name);
}
function stripIdentityPrefix(text) {
let rest = String(text || "").trim();
if (rest.startsWith(IDENTITY_CARD_PREFIX)) {
const split = rest.indexOf("\n\n");
rest = split >= 0 ? rest.slice(split + 2).trim() : "";
}
if (rest.startsWith(OLD_IDENTITY_LOCK)) {
rest = rest.slice(OLD_IDENTITY_LOCK.length).trim();
}
return rest;
}
function hoistFoldedPersona(sections, hasPrefix) {
const list = Array.isArray(sections) ? sections.slice() : [];
const idx = list.findIndex((section) => isPersonaFoldTarget(String(section?.name || ""), hasPrefix));
if (idx <= 0) return list;
const [hit] = list.splice(idx, 1);
return [hit, ...list];
}
/** 1.1.34:把 inject 写进 persona-prefix 正文开头,不再另开 dsh-purge 段。 */
function foldInjectIntoPersona(sections, injectText) {
const inject = typeof injectText === "string" ? injectText : "";
const list = Array.isArray(sections) ? sections : [];
const names = list.map((section) => String(section?.name || ""));
const hasPrefix = names.some((name) => PERSONA_PREFIX_NAMES.has(name));
let folded = false;
const out = list.map((section) => {
const name = String(section?.name || "");
if (!isPersonaFoldTarget(name, hasPrefix)) return section;
folded = true;
const prior = stripIdentityPrefix(String(section.text || ""));
if (!inject) return { ...section, text: prior, order: GLOBAL_PROMPT_ORDER };
if (prior.includes(inject)) return { ...section, order: GLOBAL_PROMPT_ORDER, interpolate: false };
return {
...section,
text: prior ? `${inject}\n\n${prior}` : inject,
order: GLOBAL_PROMPT_ORDER,
interpolate: false,
};
});
if (folded) return hoistFoldedPersona(out, hasPrefix);
if (!inject) return out;
return hoistFoldedPersona([
{
name: "deployment:persona-prefix",
text: inject,
order: GLOBAL_PROMPT_ORDER,
interpolate: false,
},
...out,
], true);
}
/** 插件写死的执行句,不是用户的规则集。任何模式都不注入。 */
function isHardcodedExecRule(section) {
if (section?.name !== "dsh-purge:rules") return false;
@@ -395,8 +456,8 @@ function isRedteamSubagent(context) {
}
/**
* 红队 persona 只留调度正文,不再把 inject / 目标卡 / 角色折进去。
* inject 和 cap 由 rewritePromptAssembly 置顶;卡和角色用不带 dsh-purge 前缀的段,避免 host 过滤器提到 cap 前面。
* 红队 persona 只留调度正文。inject 由 foldInjectIntoPersona 写进 prefix 开头;
* 卡和角色用不带 dsh-purge 前缀的段,避免 host 过滤器提到 cap 前面。
*/
function attachRedteamOpsOnly(sections) {
let attached = false;
@@ -689,15 +750,7 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
sections = insertRoleSection(insertCardSection(withOps, opts.cardText), opts.roleText);
}
const body = overlayOfficialIdentity(sanitizeAssemblySections(sections));
const head = [];
if (String(injectText || "").trim()) {
head.push({
name: "dsh-purge",
text: injectText,
order: -100000,
interpolate: false,
});
}
const folded = foldInjectIntoPersona(body, injectText);
const tail = [];
if (String(protocol || "").trim()) {
tail.push({
@@ -707,7 +760,8 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
interpolate: false,
});
}
const outSections = ensureFullInject([...head, ...body, ...tail], injectText);
const withoutCapDup = folded.filter((section) => section?.name !== CAPABILITY_SECTION);
const outSections = ensureFullInject([...withoutCapDup, ...tail], injectText);
return {
...assembled,
sections: outSections,