Skip to content

Commit b26a942

Browse files
committed
fix(scope): memory_update/memory_delete 补 strictScope 可见性校验——与 memory_get 对齐(issue #170 复核项 4)
- get 按「不存在」拒绝他 scope 的 explicit 行,update/delete 却能凭 id 直改 直删——补齐同款无存在性泄漏:strictScope 下不可见行 update 抛 not found、 delete 视作不存在;auto/存量行不构成硬墙照常可操作(与 get 同口径) - 工具层实现为 createTools 闭包内共享 guard(update 以「展开恒为 undefined」 形态前置校验);strictScope 关时零行为变化 - 测试 +1:explicit 拒改拒删/auto 可改/owner 可改/flag 关回归
1 parent 4588e47 commit b26a942

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

‎dsh-mneme/lib/tools.js‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ export function createTools(ctx, service, config, embedder) {
7979
// flag 关闭时恒返回 null——写入不标注,检索不加权,行为与 A1 前完全一致。
8080
// logger 透传:registry 反查失败时 warnOnce 才有出口(否则静默降级无观测)。
8181
const resolveSessionScope = createScopeResolver({ ctx, config, logger: ctx.logger });
82+
83+
// 复核项 4(issue #170):strictScope 下他 scope(explicit)的行对工具侧按
84+
// 「不存在」处理——update/delete 与 memory_get 同款无存在性泄漏。strictScope
85+
// 关、或会话身份取不到(scope 为 null)时恒可见(与 get 的短路语义一致)。
86+
const visibleUnderStrictScope = (memory, exec) => {
87+
if (config?.strictScope !== true) return true;
88+
const scope = resolveSessionScope(exec);
89+
return !scope || service.isVisibleInScope(memory, scope);
90+
};
91+
// 在调用点以「展开恒为 undefined」的形态前置校验(抛错即中止写入)。
92+
const assertVisibleForTool = (id, exec) => {
93+
const memory = id ? service.getById(id) : undefined;
94+
if (!visibleUnderStrictScope(memory, exec)) throw new Error("memory not found");
95+
return undefined;
96+
};
97+
// delete 侧需要目标行本身判断存在性(不可见 = 视作不存在,不抛错)。
98+
const visibleForTool = (id, exec) => {
99+
const memory = id ? service.getById(id) : undefined;
100+
return visibleUnderStrictScope(memory, exec) ? memory : undefined;
101+
};
82102
const tools = [
83103
defineTool({
84104
name: "memory_save",
@@ -336,6 +356,9 @@ export function createTools(ctx, service, config, embedder) {
336356
type: args.type,
337357
tags: args.tags,
338358
importance: args.importance,
359+
// 复核项 4(#170):strictScope 下他 scope 的行按「不存在」处理(与 get 对齐),
360+
// 展开恒为 undefined,仅承担写入前校验。
361+
...(assertVisibleForTool(args.id, arguments[1])),
339362
...(args.agent_scope !== undefined ? { agent_scope: args.agent_scope } : {}),
340363
...(args.workspace_scope !== undefined ? { workspace_scope: args.workspace_scope } : {})
341364
}, { query: args.reason });
@@ -358,9 +381,10 @@ export function createTools(ctx, service, config, embedder) {
358381
render: (_args, value) => TEXT_OUTPUT(value.deleted ? "Memory deleted." : "Memory not found.")
359382
},
360383
async execute(args) {
361-
const existed = service.getById(args.id) !== undefined;
362-
if (existed) service.remove(args.id);
363-
return { deleted: existed };
384+
// 复核项 4(#170):不可见(explicit 他 scope)= 视作不存在,无存在性泄漏。
385+
const target = visibleForTool(args.id, arguments[1]);
386+
if (target) service.remove(args.id);
387+
return { deleted: !!target };
364388
}
365389
}),
366390

‎dsh-mneme/src/tools.js‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ export function createTools(ctx, service, config, embedder) {
7979
// flag 关闭时恒返回 null——写入不标注,检索不加权,行为与 A1 前完全一致。
8080
// logger 透传:registry 反查失败时 warnOnce 才有出口(否则静默降级无观测)。
8181
const resolveSessionScope = createScopeResolver({ ctx, config, logger: ctx.logger });
82+
83+
// 复核项 4(issue #170):strictScope 下他 scope(explicit)的行对工具侧按
84+
// 「不存在」处理——update/delete 与 memory_get 同款无存在性泄漏。strictScope
85+
// 关、或会话身份取不到(scope 为 null)时恒可见(与 get 的短路语义一致)。
86+
const visibleUnderStrictScope = (memory, exec) => {
87+
if (config?.strictScope !== true) return true;
88+
const scope = resolveSessionScope(exec);
89+
return !scope || service.isVisibleInScope(memory, scope);
90+
};
91+
// 在调用点以「展开恒为 undefined」的形态前置校验(抛错即中止写入)。
92+
const assertVisibleForTool = (id, exec) => {
93+
const memory = id ? service.getById(id) : undefined;
94+
if (!visibleUnderStrictScope(memory, exec)) throw new Error("memory not found");
95+
return undefined;
96+
};
97+
// delete 侧需要目标行本身判断存在性(不可见 = 视作不存在,不抛错)。
98+
const visibleForTool = (id, exec) => {
99+
const memory = id ? service.getById(id) : undefined;
100+
return visibleUnderStrictScope(memory, exec) ? memory : undefined;
101+
};
82102
const tools = [
83103
defineTool({
84104
name: "memory_save",
@@ -336,6 +356,9 @@ export function createTools(ctx, service, config, embedder) {
336356
type: args.type,
337357
tags: args.tags,
338358
importance: args.importance,
359+
// 复核项 4(#170):strictScope 下他 scope 的行按「不存在」处理(与 get 对齐),
360+
// 展开恒为 undefined,仅承担写入前校验。
361+
...(assertVisibleForTool(args.id, arguments[1])),
339362
...(args.agent_scope !== undefined ? { agent_scope: args.agent_scope } : {}),
340363
...(args.workspace_scope !== undefined ? { workspace_scope: args.workspace_scope } : {})
341364
}, { query: args.reason });
@@ -358,9 +381,10 @@ export function createTools(ctx, service, config, embedder) {
358381
render: (_args, value) => TEXT_OUTPUT(value.deleted ? "Memory deleted." : "Memory not found.")
359382
},
360383
async execute(args) {
361-
const existed = service.getById(args.id) !== undefined;
362-
if (existed) service.remove(args.id);
363-
return { deleted: existed };
384+
// 复核项 4(#170):不可见(explicit 他 scope)= 视作不存在,无存在性泄漏。
385+
const target = visibleForTool(args.id, arguments[1]);
386+
if (target) service.remove(args.id);
387+
return { deleted: !!target };
364388
}
365389
}),
366390

‎dsh-mneme/test/scope-strict.test.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,37 @@ test("injectCandidates hard-filters explicit foreign rows when strictScope is on
131131
assert.deepEqual(unscoped.map((m) => m.title).sort(), ["auto-foreign", "foreign", "global"]);
132132
});
133133

134+
test("memory_update/memory_delete hide explicit foreign-scope rows under strictScope (review item 4, aligned with memory_get)", async () => {
135+
const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
136+
const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
137+
const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
138+
const update = pick("memory_update");
139+
const del = pick("memory_delete");
140+
141+
// explicit 他 scope:update 按不存在拒绝、delete 视作不存在——不能凭 id 直改直删。
142+
await assert.rejects(() => runHandler(update, { id: theirs.id, content: "tampered" }, STRICT_EXEC), /memory not found/);
143+
assert.equal(store.getById(theirs.id).content, "c", "row untouched by the rejected update");
144+
const delOut = await runHandler(del, { id: theirs.id }, STRICT_EXEC);
145+
assert.equal(delOut.deleted, false, "delete treats invisible rows as absent (no existence leak)");
146+
assert.ok(store.getById(theirs.id), "row NOT deleted");
147+
148+
// auto 他 scope:不构成硬墙,照常可改(与 get 的可见性口径一致)。
149+
const updAuto = await runHandler(update, { id: autoTheirs.id, content: "updated" }, STRICT_EXEC);
150+
assert.equal(updAuto.memory.id, autoTheirs.id);
151+
152+
// 行主人自己照常可改。
153+
const ownerOut = await runHandler(update, { id: theirs.id, content: "owner edit" }, OTHER_EXEC);
154+
assert.equal(ownerOut.memory.id, theirs.id);
155+
156+
// strictScope 关:update/delete 不做可见性校验(管理语义回归)。
157+
const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
158+
const t2 = store2.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
159+
await runHandler(pick2("memory_update"), { id: t2.id, content: "edited" }, STRICT_EXEC);
160+
assert.equal(store2.getById(t2.id).content, "edited");
161+
const del2 = await runHandler(pick2("memory_delete"), { id: t2.id }, STRICT_EXEC);
162+
assert.equal(del2.deleted, true);
163+
});
164+
134165
function setupTools(config) {
135166
const store = createStore(":memory:");
136167
const service = createService({ store, mirror: null, config });

0 commit comments

Comments
 (0)