mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-05-15 22:30:38 +08:00
Merge PR #7: notice display and MVU diagnostics
This commit is contained in:
@@ -2157,6 +2157,62 @@ async function testExtractorNormalizesFlatCreateOperation() {
|
||||
}
|
||||
}
|
||||
|
||||
async function testExtractorSupportsLegacyFlatNodeOperations() {
|
||||
const graph = createEmptyGraph();
|
||||
const restoreOverrides = pushTestOverrides({
|
||||
llm: {
|
||||
async callLLMForJSON() {
|
||||
return {
|
||||
operations: [
|
||||
{
|
||||
type: "event",
|
||||
id: "evt-legacy",
|
||||
title: "夜间喂食",
|
||||
summary: "角色完成了一次深夜喂食。",
|
||||
participants: "悟岳, 访客",
|
||||
status: "resolved",
|
||||
importance: 6,
|
||||
},
|
||||
{
|
||||
type: "character",
|
||||
id: "char-legacy",
|
||||
name: "悟岳",
|
||||
state: "放松下来",
|
||||
},
|
||||
],
|
||||
};
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await extractMemories({
|
||||
graph,
|
||||
messages: [{ seq: 7, role: "assistant", content: "测试旧版扁平提取输出" }],
|
||||
startSeq: 7,
|
||||
endSeq: 7,
|
||||
schema,
|
||||
embeddingConfig: null,
|
||||
settings: {},
|
||||
});
|
||||
|
||||
assert.equal(result.success, true);
|
||||
assert.equal(result.newNodes, 2);
|
||||
assert.equal(graph.lastProcessedSeq, 7);
|
||||
|
||||
const eventNode = graph.nodes.find((node) => node.type === "event");
|
||||
const characterNode = graph.nodes.find((node) => node.type === "character");
|
||||
assert.ok(eventNode);
|
||||
assert.ok(characterNode);
|
||||
assert.equal(eventNode.fields?.title, "夜间喂食");
|
||||
assert.equal(eventNode.fields?.summary, "角色完成了一次深夜喂食。");
|
||||
assert.equal(characterNode.fields?.name, "悟岳");
|
||||
assert.equal(characterNode.fields?.state, "放松下来");
|
||||
} finally {
|
||||
restoreOverrides();
|
||||
}
|
||||
}
|
||||
|
||||
async function testExtractorNormalizesArrayPayloadAndPreservesScopeField() {
|
||||
const graph = createEmptyGraph();
|
||||
const restoreOverrides = pushTestOverrides({
|
||||
@@ -5119,6 +5175,8 @@ await testVectorIndexKeepsDirtyOnDirectPartialEmbeddingFailure();
|
||||
await testExtractorFailsOnUnknownOperation();
|
||||
await testExtractorNormalizesFlatCreateOperation();
|
||||
await testExtractorNormalizesArrayPayloadAndPreservesScopeField();
|
||||
await testExtractorSupportsLegacyFlatNodeOperations();
|
||||
await testExtractorNormalizesArrayPayloadAndPreservesScopeField();
|
||||
await testConsolidatorMergeUpdatesSeqRange();
|
||||
await testConsolidatorMergeFallbackKeepsNodeWhenTargetMissing();
|
||||
await testBatchJournalVectorDeltaCapturesRecoveryFields();
|
||||
|
||||
@@ -516,6 +516,341 @@ try {
|
||||
promptBuild.debug.mvu.sanitizedFieldCount,
|
||||
);
|
||||
|
||||
// ── 新增测试:passive mode 字段族不被整段 drop ───────────────────────────────
|
||||
|
||||
// helpers
|
||||
function buildExtractSettings() {
|
||||
const taskProfiles = createDefaultTaskProfiles();
|
||||
return {
|
||||
llmApiUrl: "https://example.com/v1",
|
||||
llmApiKey: "sk-test",
|
||||
llmModel: "gpt-test",
|
||||
timeoutMs: 4321,
|
||||
taskProfilesVersion: 3,
|
||||
taskProfiles,
|
||||
};
|
||||
}
|
||||
|
||||
function buildExtractBlock(id, name, sourceKey, order) {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
type: "builtin",
|
||||
enabled: true,
|
||||
role: "system",
|
||||
sourceKey,
|
||||
sourceField: "",
|
||||
content: "",
|
||||
injectionMode: "relative",
|
||||
order,
|
||||
};
|
||||
}
|
||||
|
||||
function buildMinimalExtractSettings() {
|
||||
const base = buildExtractSettings();
|
||||
base.taskProfiles.extract = {
|
||||
activeProfileId: "extract-passive-test",
|
||||
profiles: [
|
||||
{
|
||||
id: "extract-passive-test",
|
||||
name: "passive test",
|
||||
taskType: "extract",
|
||||
builtin: false,
|
||||
version: 3,
|
||||
enabled: true,
|
||||
blocks: [
|
||||
buildExtractBlock("blk-char", "charDescription", "charDescription", 0),
|
||||
buildExtractBlock("blk-persona", "userPersona", "userPersona", 1),
|
||||
buildExtractBlock("blk-recent", "recentMessages", "recentMessages", 2),
|
||||
buildExtractBlock("blk-candidate", "candidateText", "candidateText", 3),
|
||||
],
|
||||
generation: createDefaultTaskProfiles().extract.profiles[0].generation,
|
||||
regex: { enabled: false, inheritStRegex: false, stages: {}, localRules: [] },
|
||||
},
|
||||
],
|
||||
};
|
||||
return base;
|
||||
}
|
||||
|
||||
// 测试 1:recentMessages 含多次 getvar 宏 — 不被整段 drop,宏被剥离
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const s = buildMinimalExtractSettings();
|
||||
const pb = await buildTaskPrompt(s, "extract", {
|
||||
recentMessages: "#0 [assistant]: {{get_message_variable::stat_data.hp}} 今晚的气氛很好。{{get_message_variable::display_data.mood}}",
|
||||
charDescription: "普通角色描述,不含 MVU。",
|
||||
userPersona: "普通用户设定。",
|
||||
candidateText: "",
|
||||
});
|
||||
const rendered = JSON.stringify(pb.executionMessages);
|
||||
assert.match(rendered, /今晚的气氛很好/,
|
||||
"T1: recentMessages 的叙述文本必须保留");
|
||||
assert.doesNotMatch(rendered, /get_message_variable/i,
|
||||
"T1: getvar 宏必须被剥离");
|
||||
const droppedField = pb.debug.mvu.sanitizedFields.find(
|
||||
(e) => e.name === "recentMessages" && e.dropped,
|
||||
);
|
||||
assert.equal(droppedField, undefined,
|
||||
"T1: recentMessages 不应被整段 drop(passive mode)");
|
||||
}
|
||||
|
||||
// 测试 2:recentMessages 叙述里提到 stat_data 字样 — 不被整段 drop
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const s = buildMinimalExtractSettings();
|
||||
const pb = await buildTaskPrompt(s, "extract", {
|
||||
recentMessages: "#0 [assistant]: 墙上的 stat_data 标签被撕掉了,角色叹了口气。",
|
||||
charDescription: "",
|
||||
userPersona: "",
|
||||
candidateText: "",
|
||||
});
|
||||
const rendered = JSON.stringify(pb.executionMessages);
|
||||
assert.match(rendered, /墙上的/,
|
||||
"T2: recentMessages 叙述文本必须保留");
|
||||
const droppedField = pb.debug.mvu.sanitizedFields.find(
|
||||
(e) => e.name === "recentMessages" && e.dropped,
|
||||
);
|
||||
assert.equal(droppedField, undefined,
|
||||
"T2: recentMessages 不应被整段 drop");
|
||||
}
|
||||
|
||||
// 测试 3:charDescription 含 MVU 宏 — 不被整段 drop,宏被剥离
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const s = buildMinimalExtractSettings();
|
||||
const pb = await buildTaskPrompt(s, "extract", {
|
||||
recentMessages: "普通对话。",
|
||||
charDescription: "角色叫 Alice。<StatusPlaceHolderImpl/> 她性格温柔。",
|
||||
userPersona: "",
|
||||
candidateText: "",
|
||||
});
|
||||
const rendered = JSON.stringify(pb.executionMessages);
|
||||
assert.match(rendered, /她性格温柔/,
|
||||
"T3: charDescription 叙述文本必须保留");
|
||||
assert.doesNotMatch(rendered, /StatusPlaceHolderImpl/i,
|
||||
"T3: 占位符必须被剥离");
|
||||
const droppedField = pb.debug.mvu.sanitizedFields.find(
|
||||
(e) => e.name === "charDescription" && e.dropped,
|
||||
);
|
||||
assert.equal(droppedField, undefined,
|
||||
"T3: charDescription 不应被整段 drop");
|
||||
}
|
||||
|
||||
// 测试 4:userPersona 是 MVU 规则内容 — 不被整段 drop
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const s = buildMinimalExtractSettings();
|
||||
const pb = await buildTaskPrompt(s, "extract", {
|
||||
recentMessages: "普通对话。",
|
||||
charDescription: "",
|
||||
userPersona: "变量更新规则:\ntype: state\n当前时间: 12:00",
|
||||
candidateText: "",
|
||||
});
|
||||
const rendered = JSON.stringify(pb.executionMessages);
|
||||
assert.match(rendered, /变量更新规则/,
|
||||
"T4: userPersona 文本必须保留");
|
||||
const droppedField = pb.debug.mvu.sanitizedFields.find(
|
||||
(e) => e.name === "userPersona" && e.dropped,
|
||||
);
|
||||
assert.equal(droppedField, undefined,
|
||||
"T4: userPersona 不应被整段 drop(passive mode)");
|
||||
}
|
||||
|
||||
// 测试 5:candidateNodes 含 stat_data/getvar — 字符串叶子保留,容器键剥离
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const s = buildMinimalExtractSettings();
|
||||
s.taskProfiles.extract.profiles[0].blocks.push(
|
||||
buildExtractBlock("blk-nodes", "candidateNodes", "candidateNodes", 4),
|
||||
);
|
||||
const pb = await buildTaskPrompt(s, "extract", {
|
||||
recentMessages: "",
|
||||
charDescription: "",
|
||||
userPersona: "",
|
||||
candidateText: "",
|
||||
candidateNodes: [
|
||||
{
|
||||
id: "node-a",
|
||||
summary: "这是一个有意义的候选摘要,说明了角色的决定。",
|
||||
note: "{{get_message_variable::stat_data.地点}} 某地区的行动。",
|
||||
variables: {
|
||||
0: {
|
||||
stat_data: { 地点: "学校" },
|
||||
display_data: { 地点: "教室" },
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
const rendered = JSON.stringify(pb.executionMessages);
|
||||
assert.match(rendered, /有意义的候选摘要/,
|
||||
"T5: candidateNodes 的 summary 文本必须保留");
|
||||
assert.doesNotMatch(rendered, /get_message_variable/i,
|
||||
"T5: getvar 宏必须被剥离");
|
||||
const containerDropped = pb.debug.mvu.sanitizedFields.find(
|
||||
(e) => String(e.name || "").startsWith("candidateNodes[0].variables"),
|
||||
);
|
||||
assert.ok(containerDropped,
|
||||
"T5: stat_data/display_data 容器键必须仍被剥离");
|
||||
}
|
||||
|
||||
// 测试 6:world info 仍然 aggressive drop(守卫 6cec031 正收益)
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const mvuWorldbookEntry = [
|
||||
createWorldbookEntry({
|
||||
uid: 999,
|
||||
name: "mvu-statusbar",
|
||||
comment: "mvu-statusbar",
|
||||
content: "变量输出格式: 严格 <UpdateVariable>\ntype: state\nformat: |-\n stat_data:",
|
||||
strategyType: "constant",
|
||||
keys: [],
|
||||
order: 1,
|
||||
}),
|
||||
];
|
||||
globalThis.getCharWorldbookNames = () => ({
|
||||
primary: "mvu-guard-worldbook",
|
||||
additional: [],
|
||||
});
|
||||
globalThis.getWorldbook = async (name) =>
|
||||
name === "mvu-guard-worldbook" ? mvuWorldbookEntry : [];
|
||||
globalThis.getLorebookEntries = async (name) =>
|
||||
(name === "mvu-guard-worldbook" ? mvuWorldbookEntry : []).map((e) => ({
|
||||
uid: e.uid, comment: e.comment,
|
||||
}));
|
||||
globalThis.__promptBuilderMvuContext = {
|
||||
...globalThis.__promptBuilderMvuContext,
|
||||
chatId: "mvu-guard-chat",
|
||||
chatMetadata: {},
|
||||
};
|
||||
|
||||
const s = buildExtractSettings();
|
||||
// 使用含 worldInfo 块的 extract 默认 profile
|
||||
const pb = await buildTaskPrompt(s, "extract", {
|
||||
recentMessages: "普通对话,用于触发世界书。",
|
||||
userMessage: "普通消息。",
|
||||
chatMessages: [],
|
||||
});
|
||||
const rendered = JSON.stringify(pb);
|
||||
assert.doesNotMatch(rendered, /UpdateVariable/,
|
||||
"T6: MVU 世界书条目必须仍被 aggressive drop");
|
||||
}
|
||||
|
||||
// 测试 6b:warn 路径 — 双断言
|
||||
// 构造一个故意用 aggressive mode 且会 drop 的字段(绕过策略表用内部 API)
|
||||
// 通过检验 sanitizedFields 中的 dropped + reasons 来验证 warn 的依据已正确记录
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const { sanitizeMvuContent, MVU_SANITIZE_MODES } = await import("../mvu-compat.js");
|
||||
assert.ok(MVU_SANITIZE_MODES, "mvu-compat 必须导出 MVU_SANITIZE_MODES");
|
||||
assert.equal(MVU_SANITIZE_MODES.AGGRESSIVE, "aggressive",
|
||||
"MVU_SANITIZE_MODES.AGGRESSIVE 应为 'aggressive'");
|
||||
assert.equal(MVU_SANITIZE_MODES.PASSIVE, "passive",
|
||||
"MVU_SANITIZE_MODES.PASSIVE 应为 'passive'");
|
||||
|
||||
// aggressive mode 下 MVU 世界书内容应被 drop
|
||||
const aggressiveResult = sanitizeMvuContent(
|
||||
"变量输出格式: 严格 <UpdateVariable>\ntype: state\nformat: |-\n stat_data:",
|
||||
{ mode: MVU_SANITIZE_MODES.AGGRESSIVE },
|
||||
);
|
||||
assert.equal(aggressiveResult.dropped, true,
|
||||
"T6b: aggressive mode 命中 likely_mvu_content 应 dropped=true");
|
||||
assert.ok(aggressiveResult.reasons.includes("likely_mvu_content"),
|
||||
"T6b: reasons 应含 likely_mvu_content");
|
||||
|
||||
// passive mode 下相同内容不应被整段 drop
|
||||
const passiveResult = sanitizeMvuContent(
|
||||
"变量更新规则:\ntype: state\n当前时间: 12:00",
|
||||
{ mode: MVU_SANITIZE_MODES.PASSIVE },
|
||||
);
|
||||
assert.equal(passiveResult.dropped, false,
|
||||
"T6b: passive mode 不应整段 drop");
|
||||
|
||||
// warn 路径:手动 mock console.warn 验证关键字段清空时 warn 触发
|
||||
const warnCalls = [];
|
||||
const originalWarn = console.warn;
|
||||
console.warn = (...args) => warnCalls.push(args);
|
||||
try {
|
||||
// 构建一个 extract 任务,把一个关键字段故意设成 aggressive 会 drop 的内容
|
||||
// 为触发 warn,我们在 sanitizePromptContextInputs 里必须 omit 且原始非空
|
||||
// 因为 passive 策略会保留,我们直接用 recentMessages 传入一段
|
||||
// 绕过策略表的方式是:在 world info 条目里触发 aggressive(不经过字段策略表)
|
||||
// 这里改为:直接测试 sanitizeMvuContent 在 PASSIVE mode 下 dropped=false,即 warn 不触发
|
||||
// 然后对 AGGRESSIVE 手动调用相同逻辑,断言 warn 输出
|
||||
//
|
||||
// 实际场景 warn 触发点:在 sanitizePromptContextInputs 里检测到 CRITICAL 字段 omit
|
||||
// 修复后正常场景不应触发;我们用 debug.mvu.sanitizedFields 来断言"字段未被 drop"
|
||||
const s2 = buildMinimalExtractSettings();
|
||||
const pb2 = await buildTaskPrompt(s2, "extract", {
|
||||
recentMessages: "变量更新规则:\ntype: state\n当前时间: 12:00",
|
||||
charDescription: "",
|
||||
userPersona: "",
|
||||
candidateText: "",
|
||||
});
|
||||
// passive 模式下不应 warn 关键字段 drop
|
||||
const criticalDropWarn = warnCalls.find(
|
||||
(args) => String(args[0] || "").includes("关键任务输入字段被 MVU 策略清空"),
|
||||
);
|
||||
assert.equal(criticalDropWarn, undefined,
|
||||
"T6b: passive 模式下关键字段不应触发 warn");
|
||||
// 且字段不应在 sanitizedFields 中被标记为 dropped
|
||||
const recentDropped = pb2.debug.mvu.sanitizedFields.find(
|
||||
(e) => e.name === "recentMessages" && e.dropped,
|
||||
);
|
||||
assert.equal(recentDropped, undefined,
|
||||
"T6b: recentMessages 不应在 debug.mvu.sanitizedFields 中 dropped");
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
}
|
||||
}
|
||||
|
||||
// 测试 6c:warn 诊断字段包含 reasons 和 before/after preview
|
||||
{
|
||||
delete globalThis.__stBmeRuntimeDebugState;
|
||||
const warnCalls = [];
|
||||
const originalWarn = console.warn;
|
||||
console.warn = (...args) => warnCalls.push(args);
|
||||
try {
|
||||
const s = buildMinimalExtractSettings();
|
||||
await buildTaskPrompt(s, "extract", {
|
||||
recentMessages:
|
||||
"{{get_message_variable::stat_data.hp}}\n{{get_message_variable::display_data.hp}}",
|
||||
charDescription: "",
|
||||
userPersona: "",
|
||||
candidateText: "",
|
||||
});
|
||||
const criticalDropWarn = warnCalls.find(
|
||||
(args) => String(args[0] || "").includes("关键任务输入字段被 MVU 策略清空"),
|
||||
);
|
||||
assert.ok(criticalDropWarn, "T6c: 清洗后为空时应触发关键字段 warn");
|
||||
assert.equal(criticalDropWarn[1]?.fieldName, "recentMessages",
|
||||
"T6c: warn 应指向 recentMessages");
|
||||
assert.equal(criticalDropWarn[1]?.mode, "passive",
|
||||
"T6c: recentMessages 应以 passive mode 清洗");
|
||||
assert.ok(
|
||||
Array.isArray(criticalDropWarn[1]?.reasons) &&
|
||||
criticalDropWarn[1].reasons.includes("artifact_stripped"),
|
||||
"T6c: warn 应携带 artifact_stripped reason",
|
||||
);
|
||||
assert.match(
|
||||
String(criticalDropWarn[1]?.rawPreview || ""),
|
||||
/get_message_variable/,
|
||||
"T6c: warn 应携带原始内容 preview",
|
||||
);
|
||||
assert.equal(
|
||||
String(criticalDropWarn[1]?.sanitizedPreview || ""),
|
||||
"",
|
||||
"T6c: 清洗为空时 sanitizedPreview 应为空串",
|
||||
);
|
||||
assert.ok(
|
||||
Number(criticalDropWarn[1]?.artifactRemovedCount || 0) >= 2,
|
||||
"T6c: warn 应记录 artifactRemovedCount",
|
||||
);
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("prompt-builder-mvu tests passed");
|
||||
} finally {
|
||||
if (originalRequire === undefined) {
|
||||
|
||||
@@ -329,6 +329,54 @@ assert.equal(state.llmOptions[0].returnFailureDetails, true);
|
||||
assert.equal(state.llmOptions[0].maxRetries, 2);
|
||||
assert.equal(state.llmOptions[0].maxCompletionTokens, 512);
|
||||
|
||||
state.vectorCalls.length = 0;
|
||||
state.diffusionCalls.length = 0;
|
||||
state.llmCalls.length = 0;
|
||||
state.llmOptions.length = 0;
|
||||
state.llmCandidateCount = 0;
|
||||
state.llmResponse = { selectedIds: ["rule-1"] };
|
||||
const llmCamelCaseResult = await retrieve({
|
||||
graph,
|
||||
userMessage: "换个 JSON 键名也应该兼容",
|
||||
recentMessages: [],
|
||||
embeddingConfig: {},
|
||||
schema,
|
||||
options: {
|
||||
topK: 4,
|
||||
maxRecallNodes: 2,
|
||||
enableVectorPrefilter: true,
|
||||
enableGraphDiffusion: false,
|
||||
enableLLMRecall: true,
|
||||
llmCandidatePool: 2,
|
||||
},
|
||||
});
|
||||
assert.deepEqual(Array.from(llmCamelCaseResult.selectedNodeIds), ["rule-1"]);
|
||||
assert.equal(llmCamelCaseResult.meta.retrieval.llm.status, "llm");
|
||||
|
||||
state.vectorCalls.length = 0;
|
||||
state.diffusionCalls.length = 0;
|
||||
state.llmCalls.length = 0;
|
||||
state.llmOptions.length = 0;
|
||||
state.llmCandidateCount = 0;
|
||||
state.llmResponse = { data: { selected_ids: ["rule-2"] } };
|
||||
const llmNestedResult = await retrieve({
|
||||
graph,
|
||||
userMessage: "嵌套 JSON 结构也应该兼容",
|
||||
recentMessages: [],
|
||||
embeddingConfig: {},
|
||||
schema,
|
||||
options: {
|
||||
topK: 4,
|
||||
maxRecallNodes: 2,
|
||||
enableVectorPrefilter: true,
|
||||
enableGraphDiffusion: false,
|
||||
enableLLMRecall: true,
|
||||
llmCandidatePool: 2,
|
||||
},
|
||||
});
|
||||
assert.deepEqual(Array.from(llmNestedResult.selectedNodeIds), ["rule-2"]);
|
||||
assert.equal(llmNestedResult.meta.retrieval.llm.status, "llm");
|
||||
|
||||
state.vectorCalls.length = 0;
|
||||
state.diffusionCalls.length = 0;
|
||||
state.llmCalls.length = 0;
|
||||
|
||||
Reference in New Issue
Block a user