Skip to content

Commit b3aebd6

Browse files
authored
Merge branch 'main' into fix/pin-explicit-store
2 parents 8f9d8a9 + 954f5ca commit b3aebd6

4 files changed

Lines changed: 83 additions & 8 deletions

File tree

‎dsh-mneme/lib/user-settings.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,37 @@ export function escapePromptVars(config, text) {
4343
* - 开:不注入人格(`source: "none"`)。default 退回它的本意:只兜「这一帧解析不到
4444
* 身份」的会话(无名会话本来就是 default 卡的受众)。
4545
* 默认关是有意的:这是一次口径变更,不能让老用户的注入文本在升级后静默变化。
46+
*
47+
* 严格模式有一格例外(#587):`byAgent` **整体为空**时仍回落 default。两种处境必须
48+
* 分开——「建了卡、只是没这张身份的卡」是真该按 none 处理(否则关掉一张卡等于没关),
49+
* 而「一张身份卡都没建」时 default 是用户手里唯一的一张卡,按 none 处理等于把它永久
50+
* 废掉,且面板不报错。DS Bot 这类宿主里所有 bot 会话共用 `agentPreset: "standard"`
51+
* (`ds-bot/src/host/roster.js:72-80` 强制写入),用户按 bot 名字建卡永远不会命中,
52+
* 于是必然落进后一种处境:开了严格寻址就把唯一那张卡静默关死。
4653
*/
4754
export function resolvePersonaCard(settings, agentId, { strict = false } = {}) {
4855
const store = settings.getPersona();
4956
const id = typeof agentId === "string" && agentId.trim() ? agentId.trim() : "";
5057
const card = id ? store.byAgent[id] : null;
5158
if (card && card.enabled === true) return { card, source: "byAgent" };
52-
if (id && strict) return { card: {}, source: "none" };
59+
// 严格模式只在「确实建过身份卡」时才拒绝兜底:一张都没建时兜底是唯一的出路。
60+
if (id && strict && Object.keys(store.byAgent).length > 0) return { card: {}, source: "none" };
5361
return { card: store.default, source: "default" };
5462
}
5563

5664
/**
5765
* 按会话身份取自动事实卡(resolvePersonaCard 的镜像,形状不同:facts 没有
5866
* enabled 位,「卡存在」就是非空数组)。strict 语义与人格一致:有身份但没这张
5967
* 身份的卡时不落 default——两个 bot 对用户的认知不互串是 store 层分区(输入行)
60-
* 与本层寻址(输出注入)共同守住的边界。
68+
* 与本层寻址(输出注入)共同守住的边界。与人格同一条例外(#587):`byAgent` 整体
69+
* 为空时仍落 default,「一张都没建」不等于「这一张没建」。
6170
*/
6271
export function resolveFactsCard(factsStore, agentId, { strict = false } = {}) {
6372
const id = typeof agentId === "string" && agentId.trim() ? agentId.trim() : "";
64-
const card = id ? (factsStore?.byAgent?.[id] ?? null) : null;
73+
const byAgent = factsStore?.byAgent ?? {};
74+
const card = id ? (byAgent[id] ?? null) : null;
6575
if (card && card.length) return { facts: card, source: "byAgent" };
66-
if (id && strict) return { facts: [], source: "none" };
76+
if (id && strict && Object.keys(byAgent).length > 0) return { facts: [], source: "none" };
6777
return { facts: factsStore?.default ?? [], source: "default" };
6878
}
6979

‎dsh-mneme/src/user-settings.js‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,37 @@ export function escapePromptVars(config, text) {
4343
* - 开:不注入人格(`source: "none"`)。default 退回它的本意:只兜「这一帧解析不到
4444
* 身份」的会话(无名会话本来就是 default 卡的受众)。
4545
* 默认关是有意的:这是一次口径变更,不能让老用户的注入文本在升级后静默变化。
46+
*
47+
* 严格模式有一格例外(#587):`byAgent` **整体为空**时仍回落 default。两种处境必须
48+
* 分开——「建了卡、只是没这张身份的卡」是真该按 none 处理(否则关掉一张卡等于没关),
49+
* 而「一张身份卡都没建」时 default 是用户手里唯一的一张卡,按 none 处理等于把它永久
50+
* 废掉,且面板不报错。DS Bot 这类宿主里所有 bot 会话共用 `agentPreset: "standard"`
51+
* (`ds-bot/src/host/roster.js:72-80` 强制写入),用户按 bot 名字建卡永远不会命中,
52+
* 于是必然落进后一种处境:开了严格寻址就把唯一那张卡静默关死。
4653
*/
4754
export function resolvePersonaCard(settings, agentId, { strict = false } = {}) {
4855
const store = settings.getPersona();
4956
const id = typeof agentId === "string" && agentId.trim() ? agentId.trim() : "";
5057
const card = id ? store.byAgent[id] : null;
5158
if (card && card.enabled === true) return { card, source: "byAgent" };
52-
if (id && strict) return { card: {}, source: "none" };
59+
// 严格模式只在「确实建过身份卡」时才拒绝兜底:一张都没建时兜底是唯一的出路。
60+
if (id && strict && Object.keys(store.byAgent).length > 0) return { card: {}, source: "none" };
5361
return { card: store.default, source: "default" };
5462
}
5563

5664
/**
5765
* 按会话身份取自动事实卡(resolvePersonaCard 的镜像,形状不同:facts 没有
5866
* enabled 位,「卡存在」就是非空数组)。strict 语义与人格一致:有身份但没这张
5967
* 身份的卡时不落 default——两个 bot 对用户的认知不互串是 store 层分区(输入行)
60-
* 与本层寻址(输出注入)共同守住的边界。
68+
* 与本层寻址(输出注入)共同守住的边界。与人格同一条例外(#587):`byAgent` 整体
69+
* 为空时仍落 default,「一张都没建」不等于「这一张没建」。
6170
*/
6271
export function resolveFactsCard(factsStore, agentId, { strict = false } = {}) {
6372
const id = typeof agentId === "string" && agentId.trim() ? agentId.trim() : "";
64-
const card = id ? (factsStore?.byAgent?.[id] ?? null) : null;
73+
const byAgent = factsStore?.byAgent ?? {};
74+
const card = id ? (byAgent[id] ?? null) : null;
6575
if (card && card.length) return { facts: card, source: "byAgent" };
66-
if (id && strict) return { facts: [], source: "none" };
76+
if (id && strict && Object.keys(byAgent).length > 0) return { facts: [], source: "none" };
6777
return { facts: factsStore?.default ?? [], source: "default" };
6878
}
6979

‎dsh-mneme/test/persona-settings.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,34 @@ test("personaEnabled is in the feature-flag boolean whitelist", () => {
165165
assert.throws(() => settings.setFeatureFlags({ notARealFlag: true }), TypeError);
166166
store.close();
167167
});
168+
169+
test("strict 寻址:一张身份卡都没建时仍回落 default(#587 的例外)", () => {
170+
// 两种处境必须分开:
171+
// A) 建了身份卡、只是没这一张 → strict 下不注入(见上一条用例,行为不变)
172+
// B) 一张身份卡都没建 → default 是用户手里唯一那张卡,strict 不许把它关死
173+
// DS Bot 这类宿主里所有 bot 会话共用 agentPreset "standard",用户按 bot 名字建卡
174+
// 永远不命中,于是必然落进 B——此时按 none 处理等于把唯一那张卡静默废掉。
175+
const { store, settings } = setup();
176+
settings.setPersona({ name: "Amarre", enabled: true });
177+
assert.deepEqual(settings.getPersona().byAgent, {}, "前提:byAgent 整体为空");
178+
const { card, source } = resolvePersonaCard(settings, "standard", { strict: true });
179+
assert.equal(source, "default", "一张卡都没建时严格寻址仍回落 default");
180+
assert.equal(card.name, "Amarre");
181+
// 建了任意一张身份卡之后,例外立即失效——兜底退回严格语义
182+
settings.setPersonaFor("zephyr", { name: "泽菲尔", enabled: true });
183+
assert.equal(resolvePersonaCard(settings, "standard", { strict: true }).source, "none");
184+
// 而这张卡自己仍然命中
185+
assert.equal(resolvePersonaCard(settings, "zephyr", { strict: true }).source, "byAgent");
186+
store.close();
187+
});
188+
189+
test("strict 寻址:身份卡存在但全被关掉,仍算「建过卡」——例外只看有没有键", () => {
190+
// 例外判的是 byAgent 的键数,不是「有没有可用的卡」:一张 enabled=false 的卡
191+
// 也算「用户已经做过分区」,此时不该再拿 default 去兜。这条锁住判据不会被
192+
// 改成「有可用卡才算建过」,否则关掉唯一那张身份卡会重新落回 default。
193+
const { store, settings } = setup();
194+
settings.setPersona({ name: "Amarre", enabled: true });
195+
settings.setPersonaFor("zephyr", { name: "泽菲尔", enabled: false });
196+
assert.equal(resolvePersonaCard(settings, "standard", { strict: true }).source, "none");
197+
store.close();
198+
});

‎dsh-mneme/test/profile-facts.test.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,27 @@ test("run 的 policy 是 token:配了 token 又不带 token 的 POST 被挡在
466466
assert.equal(called, 0, "鉴权必须在这次模型调用之前");
467467
store.close();
468468
});
469+
470+
test("strict 寻址:事实一张身份卡都没建时仍落 default(#587 例外,与人格同口径)", () => {
471+
// 人格那张例外同样适用于事实:只建了别的身份卡、没这一张 → 不落 default(上面
472+
// 那条已锁);一张都没建 → default 是唯一那份认知,不能因为开了严格寻址就丢掉。
473+
const onlyDefault = () => ({ default: ["默认卡事实"], byAgent: {} });
474+
const strictEmpty = renderUserSettingsBlock({
475+
settings: settingsStub,
476+
config: { ...configOn, personaStrictAddressing: true },
477+
agentId: "standard",
478+
factsProvider: onlyDefault
479+
});
480+
assert.ok(strictEmpty.text.includes("默认卡事实"), "byAgent 整体为空时严格寻址仍落 default 事实");
481+
assert.equal(resolveFactsCard(onlyDefault(), "standard", { strict: true }).source, "default");
482+
// 一旦建过任意一张身份卡,例外失效,严格语义恢复
483+
const withOne = () => ({ default: ["默认卡事实"], byAgent: { botA: ["botA 卡事实"] } });
484+
assert.equal(resolveFactsCard(withOne(), "standard", { strict: true }).source, "none");
485+
assert.equal(resolveFactsCard(withOne(), "botA", { strict: true }).source, "byAgent");
486+
// 关着的身份卡也算「建过」。⚠️ 生产不产空数组键:唯一注入读口 listActiveFactsGrouped
487+
// 建键必带一条内容((grouped.byAgent[k] ??= []).push(content)),settings.js 三个写入点
488+
// 也是空卡直接 delete。这条是防御性形状断言——锁住判据只看键数、不数条目,将来谁改成
489+
// 「有内容才算建过」就会被它拦住(Ephor 复核 #587 时指出,2026-11-06)。
490+
const closedOne = () => ({ default: ["默认卡事实"], byAgent: { botA: [] } });
491+
assert.equal(resolveFactsCard(closedOne(), "standard", { strict: true }).source, "none");
492+
});

0 commit comments

Comments
 (0)