Skip to content

Commit 00c67ea

Browse files
authored
fix(tools): memory_get 输出 schema 复用 MEMORY_ITEM_SCHEMA——scope 标注行不再被拒收(#184) (#186)
0.8.1 的 #171 给 toApiList 增加了 scope 来源三键的条件展开 (agent_scope_source / workspace_scope_source / scope_decided_at), 共享的 MEMORY_ITEM_SCHEMA 同步声明了它们,但 memory_get 的输出 schema 用的是一份内联副本且 additionalProperties:false——任何被标注过的行 走 memory_get 都会被宿主 in-process 校验拒收(invalid output), 且每写一条新记忆就多一条读不回的行。 - memory_get 的 memory 属性直接引用 MEMORY_ITEM_SCHEMA(与 list/search 同源),内联副本删除——键集漂移不可能再发生; - 导出 MEMORY_ITEM_SCHEMA 供回归测试断言同源; - 新增 test/tools-memory-get-schema.test.js:①生产同款校验器 (validateJsonSchemaValue)+ 带全量来源标注的行复现 #184(修前 逐字复现报错三条、修后零违规)②键集/required 防漂移断言。 Fixes #184
1 parent 596348f commit 00c67ea

3 files changed

Lines changed: 77 additions & 38 deletions

File tree

‎dsh-mneme/lib/tools.js‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const MEMORY_ITEM_SCHEMA = {
3939
updated_at: { type: "string", required: true }
4040
}
4141
};
42+
// 导出供回归测试断言 memory_get 与共享 schema 同源(#184 防漂移护栏)。
43+
export { MEMORY_ITEM_SCHEMA };
4244

4345
// v0.8.0 A2:occurred 时间窗参数 → store 过滤选项(list/count 同口径);
4446
// 未传参返回空对象,既有调用方不受影响。(A3 复核:此处入参仅含 occurred 边界。)
@@ -277,25 +279,10 @@ export function createTools(ctx, service, config, embedder) {
277279
type: "object",
278280
additionalProperties: false,
279281
properties: {
280-
memory: {
281-
type: "object",
282-
additionalProperties: false,
283-
properties: {
284-
id: { type: "string", required: true },
285-
title: { type: "string", required: true },
286-
type: { type: "string", required: true },
287-
importance: { type: "integer", required: true },
288-
tags: { type: "array", items: { type: "string" } },
289-
content: { type: "string", required: true },
290-
source: { type: "string" },
291-
agent_scope: { type: "string" },
292-
workspace_scope: { type: "string" },
293-
sensitivity: { type: "string" },
294-
occurred_at: { type: "string" },
295-
created_at: { type: "string" },
296-
updated_at: { type: "string" }
297-
}
298-
}
282+
// 与 memory_search / memory_list 共用同一份 item schema——DTO 由
283+
// toApiList 统一产出,内联副本曾漏掉 v0.8.1 的 scope 来源三键,
284+
// 任何被标注过的行都过不了 in-process 校验(#184)。
285+
memory: MEMORY_ITEM_SCHEMA
299286
}
300287
},
301288
render: (_args, value) => {

‎dsh-mneme/src/tools.js‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const MEMORY_ITEM_SCHEMA = {
3939
updated_at: { type: "string", required: true }
4040
}
4141
};
42+
// 导出供回归测试断言 memory_get 与共享 schema 同源(#184 防漂移护栏)。
43+
export { MEMORY_ITEM_SCHEMA };
4244

4345
// v0.8.0 A2:occurred 时间窗参数 → store 过滤选项(list/count 同口径);
4446
// 未传参返回空对象,既有调用方不受影响。(A3 复核:此处入参仅含 occurred 边界。)
@@ -277,25 +279,10 @@ export function createTools(ctx, service, config, embedder) {
277279
type: "object",
278280
additionalProperties: false,
279281
properties: {
280-
memory: {
281-
type: "object",
282-
additionalProperties: false,
283-
properties: {
284-
id: { type: "string", required: true },
285-
title: { type: "string", required: true },
286-
type: { type: "string", required: true },
287-
importance: { type: "integer", required: true },
288-
tags: { type: "array", items: { type: "string" } },
289-
content: { type: "string", required: true },
290-
source: { type: "string" },
291-
agent_scope: { type: "string" },
292-
workspace_scope: { type: "string" },
293-
sensitivity: { type: "string" },
294-
occurred_at: { type: "string" },
295-
created_at: { type: "string" },
296-
updated_at: { type: "string" }
297-
}
298-
}
282+
// 与 memory_search / memory_list 共用同一份 item schema——DTO 由
283+
// toApiList 统一产出,内联副本曾漏掉 v0.8.1 的 scope 来源三键,
284+
// 任何被标注过的行都过不了 in-process 校验(#184)。
285+
memory: MEMORY_ITEM_SCHEMA
299286
}
300287
},
301288
render: (_args, value) => {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// #184 回归:memory_get 的内联输出 schema 漏声明 v0.8.1 的 scope 来源三键
2+
// (agent_scope_source / workspace_scope_source / scope_decided_at),且
3+
// additionalProperties:false → toApiList 对被标注行条件展开这些键后,宿主
4+
// in-process 校验直接拒收("invalid output"),表现为「个别 id 必失败、多数
5+
// 正常」。本文件用生产同款校验器(@deepseek-ai/dsh-tools 的
6+
// validateJsonSchemaValue)复现并防漂移;修复 = memory_get 复用与
7+
// list/search 共享的 MEMORY_ITEM_SCHEMA,不再维护内联副本。
8+
import test from "node:test";
9+
import assert from "node:assert/strict";
10+
import { validateJsonSchemaValue } from "@deepseek-ai/dsh-tools";
11+
import { createStore } from "../src/store.js";
12+
import { createService } from "../src/service.js";
13+
import { createTools, MEMORY_ITEM_SCHEMA } from "../src/tools.js";
14+
15+
function setup() {
16+
const store = createStore(":memory:");
17+
const service = createService({ store, mirror: null, config: {} });
18+
const registered = [];
19+
const ctx = {
20+
tools: {
21+
register(def) {
22+
registered.push(def);
23+
return () => {};
24+
}
25+
}
26+
};
27+
createTools(ctx, service, {}, undefined);
28+
const byName = Object.fromEntries(registered.map((def) => [def.name, def]));
29+
return { store, service, byName };
30+
}
31+
32+
// memory_get.execute 的返回 = { memory: toApiList([getById(id)])[0] }——契约
33+
// 测试直接对「toApiList 产出的 DTO ↔ 声明的 output schema」做校验(DTO 键的
34+
// 唯一来源就是 toApiList,execute 只是包装)。
35+
test("memory_get output validates for fully scope-annotated rows (#184)", () => {
36+
const { store, service, byName } = setup();
37+
const saved = store.save({
38+
type: "preference",
39+
title: "annotated row",
40+
content: "带全量 scope 来源标注的记忆行",
41+
source: "test",
42+
agent_scope: "coder",
43+
agent_scope_source: "auto",
44+
workspace_scope: "proj-alpha",
45+
workspace_scope_source: "auto",
46+
scope_decided_at: "2026-09-15T00:00:00Z"
47+
});
48+
const get = byName.memory_get;
49+
const dto = service.toApiList([store.getById(saved.id)])[0];
50+
const res = { memory: dto };
51+
assert.deepEqual(validateJsonSchemaValue(get.output.schema, res), []);
52+
});
53+
54+
test("memory_get item schema reuses MEMORY_ITEM_SCHEMA (no inline copy) (#184)", () => {
55+
const { byName } = setup();
56+
const get = byName.memory_get;
57+
// defineTool 编译会克隆 schema,恒等不成立——断言键集与严格性同源。
58+
const compiled = get.output.schema.properties.memory;
59+
assert.deepEqual(
60+
Object.keys(compiled.properties).sort(),
61+
Object.keys(MEMORY_ITEM_SCHEMA.properties).sort()
62+
);
63+
assert.equal(compiled.additionalProperties, false);
64+
assert.deepEqual(compiled.required.sort(), ["content", "created_at", "id", "importance", "title", "type", "updated_at"]);
65+
});

0 commit comments

Comments
 (0)