@modusensus/dsh-mneme 0.7.31 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -27
- package/lib/api.js +139 -6
- package/lib/client.js +369 -32
- package/lib/commands.js +36 -5
- package/lib/config.js +101 -14
- package/lib/dream/decisions.js +147 -14
- package/lib/dream/sleep.js +84 -34
- package/lib/dream.js +250 -60
- package/lib/embedding.js +22 -2
- package/lib/index.js +68 -37
- package/lib/inject.js +54 -13
- package/lib/lang.js +168 -0
- package/lib/local-embedder.js +23 -9
- package/lib/mirror.js +17 -14
- package/lib/reranker.js +23 -8
- package/lib/runtime/adopt.js +176 -0
- package/lib/runtime/closure.js +172 -0
- package/lib/runtime/download.js +247 -0
- package/lib/runtime/layout.js +235 -0
- package/lib/runtime/loader.js +249 -0
- package/lib/runtime/provision.js +237 -0
- package/lib/runtime/tarball.js +130 -0
- package/lib/runtime/verify.js +191 -0
- package/lib/scope.js +83 -0
- package/lib/service.js +374 -55
- package/lib/settings.js +43 -2
- package/lib/store.js +126 -13
- package/lib/summarize.js +108 -35
- package/lib/tools.js +205 -12
- package/package.json +7 -2
- package/runtime-manifest.json +596 -0
- package/scripts/build-runtime-manifest.mjs +217 -0
- package/scripts/mneme-runtime.mjs +277 -0
- package/src/api.js +139 -6
- package/src/commands.js +36 -5
- package/src/config.js +101 -14
- package/src/dream/decisions.js +147 -14
- package/src/dream/sleep.js +84 -34
- package/src/dream.js +250 -60
- package/src/embedding.js +22 -2
- package/src/index.js +68 -37
- package/src/inject.js +54 -13
- package/src/lang.js +168 -0
- package/src/local-embedder.js +23 -9
- package/src/mirror.js +17 -14
- package/src/reranker.js +23 -8
- package/src/runtime/adopt.js +176 -0
- package/src/runtime/closure.js +172 -0
- package/src/runtime/download.js +247 -0
- package/src/runtime/layout.js +235 -0
- package/src/runtime/loader.js +249 -0
- package/src/runtime/provision.js +237 -0
- package/src/runtime/tarball.js +130 -0
- package/src/runtime/verify.js +191 -0
- package/src/scope.js +83 -0
- package/src/service.js +374 -55
- package/src/settings.js +43 -2
- package/src/store.js +126 -13
- package/src/summarize.js +108 -35
- package/src/tools.js +205 -12
- package/test/api.test.js +175 -9
- package/test/client.test.js +152 -10
- package/test/commands.test.js +40 -3
- package/test/config.test.js +17 -1
- package/test/dream-candidate-hybrid.test.js +281 -0
- package/test/dream.test.js +172 -0
- package/test/embedding-ready.test.js +196 -0
- package/test/helpers/runtime-source.js +60 -0
- package/test/helpers/tar-builder.js +80 -0
- package/test/inject.test.js +130 -1
- package/test/lang.test.js +87 -0
- package/test/quality-filter.test.js +68 -0
- package/test/reasoning-effort.test.js +106 -15
- package/test/reindex-backfill.test.js +62 -0
- package/test/runtime-adopt.test.js +206 -0
- package/test/runtime-cli.test.js +89 -0
- package/test/runtime-closure.test.js +193 -0
- package/test/runtime-download.test.js +302 -0
- package/test/runtime-layout.test.js +163 -0
- package/test/runtime-loader.test.js +237 -0
- package/test/runtime-manifest.test.js +158 -0
- package/test/runtime-provision.test.js +101 -0
- package/test/runtime-tarball.test.js +82 -0
- package/test/runtime-verify.test.js +214 -0
- package/test/scope-retrieval.test.js +197 -0
- package/test/scope-storage.test.js +247 -0
- package/test/scope-strict.test.js +165 -0
- package/test/service.test.js +76 -0
- package/test/sleep-action-set.test.js +422 -0
- package/test/store.test.js +14 -0
- package/test/summarize.test.js +224 -0
- package/test/tools.test.js +30 -5
- package/test/webServer-optional.test.js +34 -1
package/src/tools.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
2
|
+
import { createScopeResolver } from "./scope.js";
|
|
3
|
+
import { describeLocalRuntime, resolveRuntimeEntry } from "./runtime/loader.js";
|
|
4
|
+
import { defaultRuntimeDir } from "./runtime/layout.js";
|
|
5
|
+
import { hostModulesDir, loadRuntimeManifest, provisionRuntime } from "./runtime/provision.js";
|
|
6
|
+
import { matchesPlatform } from "./runtime/closure.js";
|
|
7
|
+
import { verifyPayload } from "./runtime/verify.js";
|
|
2
8
|
|
|
3
9
|
const TEXT_OUTPUT = (text) => [{ type: "text", text }];
|
|
4
10
|
// Per-registry tool-name registry: guards against duplicate registration on
|
|
@@ -20,11 +26,44 @@ const MEMORY_ITEM_SCHEMA = {
|
|
|
20
26
|
tags: { type: "array", items: { type: "string" } },
|
|
21
27
|
importance: { type: "integer", required: true },
|
|
22
28
|
source: { type: "string" },
|
|
29
|
+
// v0.8.0 A2:scope 标注与事件发生时间随行透出(未标注时缺省,可选属性)。
|
|
30
|
+
agent_scope: { type: "string" },
|
|
31
|
+
workspace_scope: { type: "string" },
|
|
32
|
+
sensitivity: { type: "string" },
|
|
33
|
+
occurred_at: { type: "string" },
|
|
23
34
|
created_at: { type: "string", required: true },
|
|
24
35
|
updated_at: { type: "string", required: true }
|
|
25
36
|
}
|
|
26
37
|
};
|
|
27
38
|
|
|
39
|
+
// v0.8.0 A2:occurred 时间窗参数 → store 过滤选项(list/count 同口径);
|
|
40
|
+
// 未传参返回空对象,既有调用方不受影响。(A3 复核:此处入参仅含 occurred 边界。)
|
|
41
|
+
function occurredWindow(args) {
|
|
42
|
+
const from = args?.occurred_from ?? null;
|
|
43
|
+
const to = args?.occurred_to ?? null;
|
|
44
|
+
return from !== null || to !== null ? { occurredFrom: from, occurredTo: to } : {};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// v0.8.0 A3:strictScope 硬过滤的 visibility 选项(store.list/count 同口径)。
|
|
48
|
+
// flag 关或会话 scope 未解析出(flag 关时解析器返回 null)→ 空对象不过滤;
|
|
49
|
+
// scope 为 {null,null}(匿名会话)时照传——store 侧 fail-closed 只放行未标注行。
|
|
50
|
+
function strictVisibility(config, scope) {
|
|
51
|
+
if (config?.strictScope !== true || !scope) return {};
|
|
52
|
+
return { visibility: { agentScope: scope.agent_scope, workspaceScope: scope.workspace_scope } };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// v0.8.0 A2:检索结果的 provenance 附加段——只在有标注时出现,未标注行渲染
|
|
56
|
+
// 与 A2 前逐字节一致。
|
|
57
|
+
const MEMORY_PROVENANCE = (m) => {
|
|
58
|
+
const parts = [];
|
|
59
|
+
if (m.occurred_at) parts.push(`occurred: ${m.occurred_at}`);
|
|
60
|
+
if (m.agent_scope || m.workspace_scope) {
|
|
61
|
+
parts.push(`scope: ${m.agent_scope ?? "global"} / ${m.workspace_scope ?? "global"}`);
|
|
62
|
+
}
|
|
63
|
+
if (m.sensitivity) parts.push(`sensitivity: ${m.sensitivity}`);
|
|
64
|
+
return parts.length ? ` | ${parts.join(" | ")}` : "";
|
|
65
|
+
};
|
|
66
|
+
|
|
28
67
|
export function createTools(ctx, service, config, embedder) {
|
|
29
68
|
const toolsRegistry = ctx.tools;
|
|
30
69
|
let registeredTools = REGISTERED_TOOLS.get(toolsRegistry);
|
|
@@ -32,6 +71,10 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
32
71
|
registeredTools = new Set();
|
|
33
72
|
REGISTERED_TOOLS.set(toolsRegistry, registeredTools);
|
|
34
73
|
}
|
|
74
|
+
// v0.8.0 A1/A2(issue #17):会话 scope 解析(agentPreset + workspace 反查)。
|
|
75
|
+
// flag 关闭时恒返回 null——写入不标注,检索不加权,行为与 A1 前完全一致。
|
|
76
|
+
// logger 透传:registry 反查失败时 warnOnce 才有出口(否则静默降级无观测)。
|
|
77
|
+
const resolveSessionScope = createScopeResolver({ ctx, config, logger: ctx.logger });
|
|
35
78
|
const tools = [
|
|
36
79
|
defineTool({
|
|
37
80
|
name: "memory_save",
|
|
@@ -45,7 +88,9 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
45
88
|
content: { type: "string", required: true, description: "Memory body" },
|
|
46
89
|
tags: { type: "array", items: { type: "string" }, description: "Optional tags" },
|
|
47
90
|
importance: { type: "integer", description: "1-5; >= threshold auto-injects into future sessions" },
|
|
48
|
-
source: { type: "string", description: "Optional provenance" }
|
|
91
|
+
source: { type: "string", description: "Optional provenance" },
|
|
92
|
+
sensitivity: { type: "string", description: "Optional sensitivity label (free-form, e.g. personal). Same-title entries with different sensitivity stay separate instead of merging." },
|
|
93
|
+
occurred_at: { type: "string", description: "Optional ISO-8601 instant the remembered event happened (differs from write time). Invalid values are ignored." }
|
|
49
94
|
},
|
|
50
95
|
output: {
|
|
51
96
|
schema: {
|
|
@@ -58,14 +103,21 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
58
103
|
},
|
|
59
104
|
render: (_args, value) => TEXT_OUTPUT(`memory ${value.action}: ${value.id}`)
|
|
60
105
|
},
|
|
61
|
-
async execute(args) {
|
|
106
|
+
async execute(args, exec) {
|
|
107
|
+
// scope 标注:agent_scope/workspace_scope 由会话身份解析(不作为模型
|
|
108
|
+
// 参数),sensitivity/occurred_at 来自显式参数。解析绝不抛错、取不到
|
|
109
|
+
// 落 NULL;flag 关闭时 scope 为 null,一个字段都不标注。
|
|
110
|
+
const scope = resolveSessionScope(exec);
|
|
62
111
|
const { action, memory } = service.saveWithDedupe({
|
|
63
112
|
type: args.type,
|
|
64
113
|
title: args.title,
|
|
65
114
|
content: args.content,
|
|
66
115
|
tags: args.tags ?? [],
|
|
67
116
|
importance: args.importance ?? 3,
|
|
68
|
-
source: args.source ?? "tool"
|
|
117
|
+
source: args.source ?? "tool",
|
|
118
|
+
...(scope ? { agent_scope: scope.agent_scope, workspace_scope: scope.workspace_scope } : {}),
|
|
119
|
+
...(args.sensitivity !== undefined ? { sensitivity: args.sensitivity } : {}),
|
|
120
|
+
...(args.occurred_at !== undefined ? { occurred_at: args.occurred_at } : {})
|
|
69
121
|
});
|
|
70
122
|
return { action, id: memory.id };
|
|
71
123
|
}
|
|
@@ -74,12 +126,15 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
74
126
|
defineTool({
|
|
75
127
|
name: "memory_search",
|
|
76
128
|
description: "Search the cross-session memory store. Use when you need past context: how a problem was solved, user preferences, project decisions. Substring-matches title/content/tags, and augments results with semantic (vector) recall + optional rerank when an embeddings provider is configured. Returns matching entries with source and timestamps.",
|
|
129
|
+
// A2 检索接线:occurred 时间窗 + 会话 scope 加成(见 execute)。
|
|
77
130
|
parameters: {
|
|
78
131
|
query: { type: "string", required: true, description: "Search text; substring match over title/content/tags" },
|
|
79
132
|
limit: { type: "integer", description: "Max results (default 20)" },
|
|
80
133
|
mode: { type: "string", enum: ["auto", "keyword", "vector", "hybrid"], description: "auto (default) = keyword hits first + vector fill when enabled; keyword = text only; vector = semantic recall first (falls back to keyword); hybrid = vector leads, keyword fills remaining slots" },
|
|
81
134
|
semantic: { type: "boolean", description: "Shorthand: enable semantic (vector) recall (same as mode=vector when true)" },
|
|
82
|
-
rerank: { type: "boolean", description: "Run cross-encoder rerank over candidates when a local reranker is configured (default true)" }
|
|
135
|
+
rerank: { type: "boolean", description: "Run cross-encoder rerank over candidates when a local reranker is configured (default true)" },
|
|
136
|
+
occurred_from: { type: "string", description: "Optional ISO date/timestamp lower bound on when the remembered event happened (occurred_at, falling back to created_at). Date-only values are inclusive from that day's 00:00Z." },
|
|
137
|
+
occurred_to: { type: "string", description: "Optional ISO date/timestamp upper bound on occurred_at. Date-only values are inclusive through that day's 23:59:59.999Z." }
|
|
83
138
|
},
|
|
84
139
|
output: {
|
|
85
140
|
schema: {
|
|
@@ -99,19 +154,26 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
99
154
|
.map((m, i) => {
|
|
100
155
|
const preview = (m.content ?? "").replace(/\s+/g, " ").trim();
|
|
101
156
|
const cut = preview.length > 200 ? `${preview.slice(0, 200)}…` : preview;
|
|
102
|
-
return `[${i + 1}] ${m.title}\n ID: ${m.id} | type: ${m.type} | importance: ${m.importance} | updated: ${m.updated_at}\n ${cut}`;
|
|
157
|
+
return `[${i + 1}] ${m.title}\n ID: ${m.id} | type: ${m.type} | importance: ${m.importance} | updated: ${m.updated_at}${MEMORY_PROVENANCE(m)}\n ${cut}`;
|
|
103
158
|
})
|
|
104
159
|
.join("\n\n");
|
|
105
160
|
return TEXT_OUTPUT(`Found ${items.length} memory entr${items.length === 1 ? "y" : "ies"}:\n\n${body}`);
|
|
106
161
|
}
|
|
107
162
|
},
|
|
108
|
-
async execute(args) {
|
|
163
|
+
async execute(args, exec) {
|
|
109
164
|
const limit = args.limit ?? 20;
|
|
110
165
|
const mode = args.semantic === true && !args.mode ? "vector" : args.mode ?? "auto";
|
|
166
|
+
// v0.8.0 A2(issue #17):scope 检索加权 + occurred 时间窗的检索接线。
|
|
167
|
+
// flag 关闭或解析不出 scope 时为 null,检索行为与 A2 前一致。
|
|
168
|
+
const scope = resolveSessionScope(exec);
|
|
169
|
+
const occurredFrom = args.occurred_from ?? null;
|
|
170
|
+
const occurredTo = args.occurred_to ?? null;
|
|
111
171
|
const rows = await service.searchMemories(args.query, {
|
|
112
172
|
mode,
|
|
113
173
|
topK: limit,
|
|
114
|
-
useRerank: args.rerank !== false
|
|
174
|
+
useRerank: args.rerank !== false,
|
|
175
|
+
...(scope ? { scope } : {}),
|
|
176
|
+
...(occurredFrom !== null || occurredTo !== null ? { occurredFrom, occurredTo } : {})
|
|
115
177
|
});
|
|
116
178
|
return { items: service.toApiList(rows) };
|
|
117
179
|
}
|
|
@@ -124,7 +186,9 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
124
186
|
type: { type: "string", enum: ["preference", "project", "decision", "history", "rejected_solution", "pitfall", "constraint"], description: "Filter by type; omit for all" },
|
|
125
187
|
limit: { type: "integer", description: "Page size (default 50)" },
|
|
126
188
|
offset: { type: "integer", description: "Page offset (default 0)" },
|
|
127
|
-
include_archived: { type: "boolean", description: "Include archived (hidden) entries so they can be found and restored (default false)" }
|
|
189
|
+
include_archived: { type: "boolean", description: "Include archived (hidden) entries so they can be found and restored (default false)" },
|
|
190
|
+
occurred_from: { type: "string", description: "Optional ISO date/timestamp lower bound on when the remembered event happened (occurred_at, falling back to created_at). Date-only values are inclusive from that day's 00:00Z." },
|
|
191
|
+
occurred_to: { type: "string", description: "Optional ISO date/timestamp upper bound on occurred_at. Date-only values are inclusive through that day's 23:59:59.999Z." }
|
|
128
192
|
},
|
|
129
193
|
output: {
|
|
130
194
|
schema: {
|
|
@@ -142,7 +206,7 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
142
206
|
const items = value.items ?? [];
|
|
143
207
|
if (items.length === 0) return TEXT_OUTPUT(`0 memory entries (of ${value.total}).`);
|
|
144
208
|
const body = items
|
|
145
|
-
.map((m, i) => `[${i + 1}] ${m.title} (type=${m.type}, importance=${m.importance})\n ID: ${m.id} | updated: ${m.updated_at}`)
|
|
209
|
+
.map((m, i) => `[${i + 1}] ${m.title} (type=${m.type}, importance=${m.importance})\n ID: ${m.id} | updated: ${m.updated_at}${MEMORY_PROVENANCE(m)}`)
|
|
146
210
|
.join("\n\n");
|
|
147
211
|
return TEXT_OUTPUT(`${items.length} memory entries (of ${value.total}):\n\n${body}`);
|
|
148
212
|
}
|
|
@@ -153,9 +217,13 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
153
217
|
type: args.type,
|
|
154
218
|
limit: args.limit ?? 50,
|
|
155
219
|
offset: args.offset ?? 0,
|
|
156
|
-
includeArchived
|
|
220
|
+
includeArchived,
|
|
221
|
+
...occurredWindow(args),
|
|
222
|
+
// A3:strict 开启时按会话 scope 硬过滤(宿主按 (args, runContext) 调用,
|
|
223
|
+
// 从 arguments 取第二参)。
|
|
224
|
+
...strictVisibility(config, resolveSessionScope(arguments[1]))
|
|
157
225
|
}));
|
|
158
|
-
return { items: rows, total: service.count(args.type, { includeArchived }) };
|
|
226
|
+
return { items: rows, total: service.count(args.type, { includeArchived, ...occurredWindow(args), ...strictVisibility(config, resolveSessionScope(arguments[1])) }) };
|
|
159
227
|
}
|
|
160
228
|
}),
|
|
161
229
|
|
|
@@ -181,6 +249,10 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
181
249
|
tags: { type: "array", items: { type: "string" } },
|
|
182
250
|
content: { type: "string", required: true },
|
|
183
251
|
source: { type: "string" },
|
|
252
|
+
agent_scope: { type: "string" },
|
|
253
|
+
workspace_scope: { type: "string" },
|
|
254
|
+
sensitivity: { type: "string" },
|
|
255
|
+
occurred_at: { type: "string" },
|
|
184
256
|
created_at: { type: "string" },
|
|
185
257
|
updated_at: { type: "string" }
|
|
186
258
|
}
|
|
@@ -189,12 +261,19 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
189
261
|
},
|
|
190
262
|
render: (_args, value) => {
|
|
191
263
|
const m = value.memory;
|
|
192
|
-
return TEXT_OUTPUT(`${m.title}\nID: ${m.id} | type: ${m.type} | importance: ${m.importance}\n\n${m.content}`);
|
|
264
|
+
return TEXT_OUTPUT(`${m.title}\nID: ${m.id} | type: ${m.type} | importance: ${m.importance}${MEMORY_PROVENANCE(m)}\n\n${m.content}`);
|
|
193
265
|
}
|
|
194
266
|
},
|
|
195
267
|
async execute(args) {
|
|
196
268
|
const memory = service.getById(args.id);
|
|
197
269
|
if (memory === undefined) throw new Error("memory not found");
|
|
270
|
+
// v0.8.0 A3:strictScope 下他 scope 的行按不存在处理(无存在性泄漏)。
|
|
271
|
+
// 宿主注册表按 (args, runContext) 两参调用处理器,从 arguments 取第二参。
|
|
272
|
+
const runContext = arguments[1];
|
|
273
|
+
const scope = resolveSessionScope(runContext);
|
|
274
|
+
if (config.strictScope === true && scope && !service.isVisibleInScope(memory, scope)) {
|
|
275
|
+
throw new Error("memory not found");
|
|
276
|
+
}
|
|
198
277
|
return { memory: service.toApiList([memory])[0] };
|
|
199
278
|
}
|
|
200
279
|
}),
|
|
@@ -331,6 +410,120 @@ export function createTools(ctx, service, config, embedder) {
|
|
|
331
410
|
const memory = service.setArchived(args.id, args.archived ?? true);
|
|
332
411
|
return { memory: { id: memory.id, archived: memory.archived } };
|
|
333
412
|
}
|
|
413
|
+
}),
|
|
414
|
+
|
|
415
|
+
// 运行时口子(issue #131 / PR-C)。
|
|
416
|
+
//
|
|
417
|
+
// 为什么要有这个工具:mneme 的取向是「本体保持完善但轻量;重的功能存在、默认不开、不被依赖」。
|
|
418
|
+
// 本地向量化就是那个重功能 —— 它需要额外一份运行时(解包后数百 MB)。面板上有按钮,但用户
|
|
419
|
+
// 可能根本不看面板;这时 agent 需要一个入口,能先查状态、把代价告诉用户,再按需取回。
|
|
420
|
+
//
|
|
421
|
+
// 关于「要不要直接 provision」:它会联网、可能传几十到上百 MB、耗时数分钟。所以工具描述里
|
|
422
|
+
// 明确要求先告知用户;只有在用户已经表示要本地向量化时才直接动手。
|
|
423
|
+
defineTool({
|
|
424
|
+
name: "memory_runtime",
|
|
425
|
+
description:
|
|
426
|
+
"Inspect or provision the LOCAL inference runtime that dsh-mneme's local (offline) embedding and rerank need: " +
|
|
427
|
+
"the transformers + onnxruntime dependency closure, hundreds of MB unpacked. It is deliberately NOT a default " +
|
|
428
|
+
"dependency — mneme's core stays lightweight and local vectorization is opt-in — so this tool is the hook that " +
|
|
429
|
+
"makes the heavy capability available on demand. Use action=status first: it is read-only and reports whether " +
|
|
430
|
+
"the runtime is ready plus what provisioning would cost. action=provision tries, in order: (1) adopting an " +
|
|
431
|
+
"existing copy from this profile's node_modules (no network; hardlinked when on the same volume, so no extra " +
|
|
432
|
+
"disk), (2) a configured local .tgz directory, (3) downloading from the npm registry against a pinned manifest, " +
|
|
433
|
+
"verifying every tarball's sha512. Provisioning can transfer tens to hundreds of MB and take minutes: unless " +
|
|
434
|
+
"the user has already asked for local vectorization, tell them that cost before calling it. action=verify loads " +
|
|
435
|
+
"the runtime and runs one real inference (strictly offline, so the model must already be cached).",
|
|
436
|
+
parameters: {
|
|
437
|
+
action: { type: "string", enum: ["status", "provision", "verify"], required: true, description: "status = read-only; provision = fetch/adopt the runtime; verify = run one real inference" },
|
|
438
|
+
overwrite: { type: "boolean", description: "provision only: replace an existing payload (use when repairing a broken one)" }
|
|
439
|
+
},
|
|
440
|
+
output: {
|
|
441
|
+
schema: {
|
|
442
|
+
type: "object",
|
|
443
|
+
additionalProperties: false,
|
|
444
|
+
properties: {
|
|
445
|
+
summary: { type: "string", required: true },
|
|
446
|
+
status: { type: "string", required: true },
|
|
447
|
+
cost: { type: "string", required: true },
|
|
448
|
+
strategy: { type: "string" },
|
|
449
|
+
payloadId: { type: "string" },
|
|
450
|
+
packages: { type: "integer" },
|
|
451
|
+
files: { type: "integer" },
|
|
452
|
+
bytes: { type: "integer" },
|
|
453
|
+
dimension: { type: "integer" },
|
|
454
|
+
reason: { type: "string" }
|
|
455
|
+
}
|
|
456
|
+
},
|
|
457
|
+
render: (_args, value) => TEXT_OUTPUT(value.summary)
|
|
458
|
+
},
|
|
459
|
+
async execute(args) {
|
|
460
|
+
const runtimeDir = config?.runtimeDir ?? "";
|
|
461
|
+
const manifest = loadRuntimeManifest();
|
|
462
|
+
const forPlatform = Array.isArray(manifest?.packages) ? manifest.packages.filter((pkg) => matchesPlatform(pkg, process.platform, process.arch)) : null;
|
|
463
|
+
const cost =
|
|
464
|
+
`Local vectorization needs an extra local inference runtime (transformers + onnxruntime closure, ` +
|
|
465
|
+
`hundreds of MB unpacked${forPlatform ? `, ${forPlatform.length} packages for this platform` : ""}). ` +
|
|
466
|
+
`It is not a default dependency: provisioning adopts an existing copy when possible, otherwise downloads it.`;
|
|
467
|
+
|
|
468
|
+
if (args.action === "provision") {
|
|
469
|
+
const result = await provisionRuntime({
|
|
470
|
+
hostModulesDir: hostModulesDir(import.meta.url),
|
|
471
|
+
runtimeDir,
|
|
472
|
+
localTarballDir: config?.runtimeTarballDir ?? "",
|
|
473
|
+
mirror: config?.runtimeMirror ?? "",
|
|
474
|
+
overwrite: args.overwrite === true,
|
|
475
|
+
manifest
|
|
476
|
+
});
|
|
477
|
+
return {
|
|
478
|
+
summary: result.ok
|
|
479
|
+
? `Runtime ${result.strategy === "download" ? "downloaded from npm" : "adopted from this machine"}: ` +
|
|
480
|
+
`${result.packages} packages / ${result.files} files. Local embedding can be used after a DSH reload.`
|
|
481
|
+
: `Could not provision the runtime: ${result.reason}`,
|
|
482
|
+
status: result.ok ? "available" : "failed",
|
|
483
|
+
cost,
|
|
484
|
+
strategy: result.strategy,
|
|
485
|
+
...(result.payloadId ? { payloadId: result.payloadId } : {}),
|
|
486
|
+
...(result.packages === undefined ? {} : { packages: result.packages }),
|
|
487
|
+
...(result.files === undefined ? {} : { files: result.files }),
|
|
488
|
+
...(result.bytes === undefined ? {} : { bytes: result.bytes }),
|
|
489
|
+
...(result.reason ? { reason: result.reason } : {})
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (args.action === "verify") {
|
|
494
|
+
const candidate = resolveRuntimeEntry({ runtimeDir: runtimeDir || undefined });
|
|
495
|
+
if (candidate === null) {
|
|
496
|
+
return { summary: "No usable runtime payload to verify.", status: "missing", cost };
|
|
497
|
+
}
|
|
498
|
+
const report = await verifyPayload(candidate.dir, { cacheDir: config?.embedModelCacheDir || undefined });
|
|
499
|
+
return {
|
|
500
|
+
summary: report.ok
|
|
501
|
+
? `Runtime verified: real inference succeeded (dim ${report.functional.dim}, ${report.functional.rows} rows, ` +
|
|
502
|
+
`${report.functional.elapsedMs}ms, offline). Integrity: ${report.integrity.status}.`
|
|
503
|
+
: `Runtime verification failed — structural: ${report.structural.ok}, functional: ${report.functional.ok} (${report.functional.reason}).`,
|
|
504
|
+
status: report.ok ? "verified" : "failed",
|
|
505
|
+
cost,
|
|
506
|
+
payloadId: candidate.payloadId,
|
|
507
|
+
...(report.functional.dim ? { dimension: report.functional.dim } : {}),
|
|
508
|
+
...(report.ok ? {} : { reason: report.functional.reason })
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// status:只读,且刻意不加载模型(那要几百毫秒且碰缓存)。报告要给到 agent 足够的信息去
|
|
513
|
+
// 决定「要不要打扰用户」。
|
|
514
|
+
const report = describeLocalRuntime({ runtimeDir: runtimeDir || defaultRuntimeDir() });
|
|
515
|
+
return {
|
|
516
|
+
summary:
|
|
517
|
+
`Local inference runtime: ${report.status}` +
|
|
518
|
+
(report.payloadId ? ` (${report.payloadId}, ${report.procedure ?? "?"}, ${report.materialize ?? "?"})` : "") +
|
|
519
|
+
`. ${report.status === "available" ? "Local embedding/rerank can be used." : "Local embedding is unavailable — " + (report.hint ?? "")}`,
|
|
520
|
+
status: report.status,
|
|
521
|
+
cost,
|
|
522
|
+
...(report.payloadId ? { payloadId: report.payloadId } : {}),
|
|
523
|
+
...(forPlatform ? { packages: forPlatform.length } : {}),
|
|
524
|
+
...(report.reason ? { reason: report.reason } : {})
|
|
525
|
+
};
|
|
526
|
+
}
|
|
334
527
|
})
|
|
335
528
|
];
|
|
336
529
|
|
package/test/api.test.js
CHANGED
|
@@ -33,7 +33,7 @@ function req(path, method = "GET", body = null) {
|
|
|
33
33
|
return r;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
function setup(embedder, apiToken = "", config = null) {
|
|
36
|
+
function setup(embedder, apiToken = "", config = null, semantic = undefined) {
|
|
37
37
|
const store = createStore(":memory:");
|
|
38
38
|
const service = createService({ store, mirror: null, config: {} });
|
|
39
39
|
const settings = createSettings(store.db);
|
|
@@ -51,7 +51,7 @@ function setup(embedder, apiToken = "", config = null) {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
|
-
const api = createApi(ctx, service, settings, commands, embedder,
|
|
54
|
+
const api = createApi(ctx, service, settings, commands, embedder, semantic, apiToken, config);
|
|
55
55
|
return { store, service, routes, api, settings, apiToken };
|
|
56
56
|
}
|
|
57
57
|
|
|
@@ -470,6 +470,73 @@ test("GET /api/dsh-mneme/semantic reports ready state per embedder", async () =>
|
|
|
470
470
|
assert.equal(named.ready, true);
|
|
471
471
|
});
|
|
472
472
|
|
|
473
|
+
// --- #135: /semantic must tell "not configured" apart from "degraded" ---------
|
|
474
|
+
|
|
475
|
+
test("GET /api/dsh-mneme/semantic reports configured/coverage/degraded", async () => {
|
|
476
|
+
const fetchSem = async (embedder, semantic) => {
|
|
477
|
+
const { routes } = setup(embedder, "", null, semantic);
|
|
478
|
+
const sem = routes.find((r) => r.path === "/api/dsh-mneme/semantic");
|
|
479
|
+
const res = new FakeRes();
|
|
480
|
+
await sem.handler(req("/api/dsh-mneme/semantic"), res);
|
|
481
|
+
assert.equal(res.statusCode, 200);
|
|
482
|
+
return JSON.parse(res.body);
|
|
483
|
+
};
|
|
484
|
+
// Stats double: `total` is the memory count, `embedded` the indexed count.
|
|
485
|
+
const index = (embeddedCount, totalCount) => ({
|
|
486
|
+
vectorIndex: { getStats: () => ({ embeddedCount, totalCount }) }
|
|
487
|
+
});
|
|
488
|
+
const openai = (ready, configured) => ({
|
|
489
|
+
name: "OpenAI",
|
|
490
|
+
get ready() { return ready; },
|
|
491
|
+
get configured() { return configured; },
|
|
492
|
+
embed() {}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
// No embedder at all → everything reports absence, not a false green.
|
|
496
|
+
const none = await fetchSem(undefined, index(0, 0));
|
|
497
|
+
assert.equal(none.configured, null);
|
|
498
|
+
assert.equal(none.reason, "no-embedder");
|
|
499
|
+
assert.equal(none.degraded, false);
|
|
500
|
+
assert.deepEqual(none.coverage, { embedded: 0, total: 0, ratio: null },
|
|
501
|
+
"empty store → ratio null (0 would read as 'nothing embedded')");
|
|
502
|
+
|
|
503
|
+
// Configured embedder still initializing: unreachable ≠ unconfigured.
|
|
504
|
+
assert.equal((await fetchSem(openai(false, true), index(0, 346))).reason, "initializing");
|
|
505
|
+
|
|
506
|
+
// Never configured: reported as such even though the index has memories.
|
|
507
|
+
const unconfigured = await fetchSem(openai(false, false), index(0, 346));
|
|
508
|
+
assert.equal(unconfigured.ready, false);
|
|
509
|
+
assert.equal(unconfigured.configured, false);
|
|
510
|
+
assert.equal(unconfigured.reason, "not-configured");
|
|
511
|
+
assert.equal(unconfigured.degraded, false, "unconfigured was never meant to index");
|
|
512
|
+
assert.equal(unconfigured.coverage.ratio, 0);
|
|
513
|
+
|
|
514
|
+
// Configured, memories present, zero vectors — the state this machine sat in
|
|
515
|
+
// for weeks while the card showed a healthy provider.
|
|
516
|
+
const degraded = await fetchSem(openai(true, true), index(0, 346));
|
|
517
|
+
assert.equal(degraded.reason, null, "a complete config has no abnormal reason");
|
|
518
|
+
assert.equal(degraded.degraded, true);
|
|
519
|
+
assert.equal(degraded.coverage.ratio, 0);
|
|
520
|
+
|
|
521
|
+
// Healthy index.
|
|
522
|
+
const ok = await fetchSem(openai(true, true), index(173, 346));
|
|
523
|
+
assert.equal(ok.degraded, false);
|
|
524
|
+
assert.equal(ok.coverage.ratio, 0.5);
|
|
525
|
+
|
|
526
|
+
// Adapters without a `configured` field (third-party/test doubles) keep the
|
|
527
|
+
// old contract: assumed configured, `ready` decides.
|
|
528
|
+
const legacy = await fetchSem({ embed() {} }, index(5, 10));
|
|
529
|
+
assert.equal(legacy.configured, true);
|
|
530
|
+
assert.equal(legacy.reason, null);
|
|
531
|
+
assert.equal(legacy.ready, true);
|
|
532
|
+
|
|
533
|
+
// No stats provider (no vector index) must not throw — coverage falls back
|
|
534
|
+
// to zeros and an empty store is never "degraded".
|
|
535
|
+
const noIndex = await fetchSem(openai(true, true), null);
|
|
536
|
+
assert.deepEqual(noIndex.coverage, { embedded: 0, total: 0, ratio: null });
|
|
537
|
+
assert.equal(noIndex.degraded, false);
|
|
538
|
+
});
|
|
539
|
+
|
|
473
540
|
// --- Bug8: llm-audit API (pagination + stats) --------------------------------
|
|
474
541
|
|
|
475
542
|
test("GET /api/dsh-mneme/semantic/llm-audit returns paginated rows", async () => {
|
|
@@ -577,19 +644,21 @@ test("GET /api/dsh-mneme/features returns empty overrides and effective config d
|
|
|
577
644
|
assert.equal(res.statusCode, 200);
|
|
578
645
|
const data = JSON.parse(res.body);
|
|
579
646
|
assert.deepEqual(data.overrides, {});
|
|
580
|
-
// effective 覆盖全部
|
|
647
|
+
// effective 覆盖全部 51 个白名单键(含 v0.7.20 heatEnabled、Issue #89 新增
|
|
581
648
|
// dreamSkipInvalid/allowCrossTypeMerge/dreamMinIntervalMinutes、面板可调的
|
|
582
649
|
// dreamMaxTokens、睡眠路由 sleepProvider/sleepModel、PR1 新增的
|
|
583
|
-
// recallFusion/signalTransparency
|
|
584
|
-
// entityExtractionProvider/entityExtractionModel/entityExtractionReasoning
|
|
585
|
-
//
|
|
650
|
+
// recallFusion/signalTransparency、issue #109 新增的实体抽取路由
|
|
651
|
+
// entityExtractionProvider/entityExtractionModel/entityExtractionReasoning、
|
|
652
|
+
// issue #127 新增的 summarize 五键、issue #126 新增的 sleepActionSet,
|
|
653
|
+
// 以及 issue #125 新增的候选集三键、issue #17(v0.8.0 A1/A3)新增的
|
|
654
|
+
// scopeEnabled/strictScope),未覆盖时取 bundle 配置的解析默认值;
|
|
586
655
|
// dreamProvider/dreamModel 无 schema 默认值(Config({}) 解析为 undefined),
|
|
587
|
-
// 不编造给前端 →
|
|
588
|
-
assert.equal(Object.keys(data.effective).length,
|
|
656
|
+
// 不编造给前端 → 53 - 2 = 51
|
|
657
|
+
assert.equal(Object.keys(data.effective).length, 51);
|
|
589
658
|
assert.equal(data.effective.dreamSkipInvalid, true);
|
|
590
659
|
assert.equal(data.effective.allowCrossTypeMerge, false);
|
|
591
660
|
assert.equal(data.effective.dreamMinIntervalMinutes, 0);
|
|
592
|
-
assert.equal(data.effective.dreamMaxTokens,
|
|
661
|
+
assert.equal(data.effective.dreamMaxTokens, 131072);
|
|
593
662
|
assert.equal(data.effective.sleepProvider, "");
|
|
594
663
|
assert.equal(data.effective.sleepModel, "");
|
|
595
664
|
assert.equal(data.effective.autoInject, true);
|
|
@@ -615,6 +684,18 @@ test("GET /api/dsh-mneme/features returns empty overrides and effective config d
|
|
|
615
684
|
assert.equal(data.effective.entityExtractionProvider, "");
|
|
616
685
|
assert.equal(data.effective.entityExtractionModel, "");
|
|
617
686
|
assert.equal(data.effective.entityExtractionReasoning, "none");
|
|
687
|
+
// issue #127:summarize 节流五键(均有默认值,故计入 effective 计数)
|
|
688
|
+
assert.equal(data.effective.summarizeMinIntervalMinutes, 0);
|
|
689
|
+
assert.equal(data.effective.summarizeMaxEntriesPerRun, 0);
|
|
690
|
+
assert.equal(data.effective.summarizeDedupeMode, "off");
|
|
691
|
+
assert.equal(data.effective.summarizeDedupeMinSim, 0.92);
|
|
692
|
+
assert.equal(data.effective.summarizeDedupeWindowHours, 24);
|
|
693
|
+
// issue #126:sleep 动作集(默认 conflict = 现状;面板可切 full)
|
|
694
|
+
assert.equal(data.effective.sleepActionSet, "conflict");
|
|
695
|
+
// issue #125:候选集三键(默认 window / 0 / 0.85)
|
|
696
|
+
assert.equal(data.effective.dreamCandidateMode, "window");
|
|
697
|
+
assert.equal(data.effective.dreamCandidateMax, 0);
|
|
698
|
+
assert.equal(data.effective.dreamCandidateMinSim, 0.85);
|
|
618
699
|
});
|
|
619
700
|
|
|
620
701
|
test("PUT /api/dsh-mneme/features round-trips, overrides effective and persists", async () => {
|
|
@@ -988,6 +1069,91 @@ test("GET /api/dsh-mneme/dream-status returns runs and pending conflict ids", as
|
|
|
988
1069
|
assert.deepEqual(afterData.pendingMemoryIds, []);
|
|
989
1070
|
});
|
|
990
1071
|
|
|
1072
|
+
// v0.8.0 冲突集中处理:GET /conflicts 队列联表 + POST /conflicts/resolve 人工确认。
|
|
1073
|
+
test("GET /api/dsh-mneme/conflicts joins both sides of unresolved conflicts", async () => {
|
|
1074
|
+
const { routes, service } = setup();
|
|
1075
|
+
const a = service.saveWithDedupe({ type: "history", title: "界面暗色偏好", content: "用户当前使用暗色界面主题", importance: 3 }).memory;
|
|
1076
|
+
const b = service.saveWithDedupe({ type: "history", title: "界面亮色切换", content: "用户界面亮色切换计划", importance: 3 }).memory;
|
|
1077
|
+
service.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "主题偏好前后矛盾" });
|
|
1078
|
+
|
|
1079
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/conflicts");
|
|
1080
|
+
const res = new FakeRes();
|
|
1081
|
+
await route.handler(req("/api/dsh-mneme/conflicts"), res);
|
|
1082
|
+
assert.equal(res.statusCode, 200);
|
|
1083
|
+
const data = JSON.parse(res.body);
|
|
1084
|
+
assert.equal(data.items.length, 1);
|
|
1085
|
+
assert.equal(data.total, 1);
|
|
1086
|
+
assert.equal(data.items[0].reason, "主题偏好前后矛盾");
|
|
1087
|
+
// saveConflictPending 会按 id 字典序归一 A/B 侧位——断言集合而非位置。
|
|
1088
|
+
assert.deepEqual(
|
|
1089
|
+
[data.items[0].memory_a.title, data.items[0].memory_b.title].sort(),
|
|
1090
|
+
["界面亮色切换", "界面暗色偏好"]
|
|
1091
|
+
);
|
|
1092
|
+
assert.equal(data.items[0].memory_a.archived, false);
|
|
1093
|
+
assert.equal(data.items[0].memory_b.archived, false);
|
|
1094
|
+
// 幽灵侧容忍缺失
|
|
1095
|
+
service.saveConflictPending({ memory_a: a.id, memory_b: "ghost-id", reason: "对方已删" });
|
|
1096
|
+
const res2 = new FakeRes();
|
|
1097
|
+
await route.handler(req("/api/dsh-mneme/conflicts"), res2);
|
|
1098
|
+
const data2 = JSON.parse(res2.body);
|
|
1099
|
+
assert.equal(data2.items.length, 2);
|
|
1100
|
+
assert.equal(data2.items.find((x) => x.reason === "对方已删").memory_b.missing, true);
|
|
1101
|
+
});
|
|
1102
|
+
|
|
1103
|
+
test("POST /api/dsh-mneme/conflicts/resolve stamps and applies the disposition", async () => {
|
|
1104
|
+
const { store, routes, service } = setup();
|
|
1105
|
+
const a = service.saveWithDedupe({ type: "history", title: "界面暗色偏好", content: "用户当前使用暗色界面主题", importance: 3 }).memory;
|
|
1106
|
+
const b = service.saveWithDedupe({ type: "history", title: "界面亮色切换", content: "用户界面亮色切换计划", importance: 3 }).memory;
|
|
1107
|
+
const pending = service.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "主题偏好前后矛盾" });
|
|
1108
|
+
|
|
1109
|
+
// store 会把 pair 按 id 字典序归一——从队列行反查「界面亮色切换」所在侧再选。
|
|
1110
|
+
const queueRoute = routes.find((r) => r.path === "/api/dsh-mneme/conflicts");
|
|
1111
|
+
const queueRes = new FakeRes();
|
|
1112
|
+
await queueRoute.handler(req("/api/dsh-mneme/conflicts"), queueRes);
|
|
1113
|
+
const queued = JSON.parse(queueRes.body).items.find((x) => x.id === pending.id);
|
|
1114
|
+
const keepSide = queued.memory_a.id === b.id ? "a" : "b";
|
|
1115
|
+
// loser 恒为 a(keeper 恒为「界面亮色切换」= b),与 A/B 侧位字母无关
|
|
1116
|
+
const loserId = a.id;
|
|
1117
|
+
|
|
1118
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/conflicts/resolve");
|
|
1119
|
+
// 保留「界面亮色切换」所在侧(apply 默认 true)→ 该侧保留,另一侧归档
|
|
1120
|
+
const res = new FakeRes();
|
|
1121
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: pending.id, winner: keepSide }), res);
|
|
1122
|
+
assert.equal(res.statusCode, 200);
|
|
1123
|
+
const data = JSON.parse(res.body);
|
|
1124
|
+
assert.equal(data.ok, true);
|
|
1125
|
+
assert.equal(data.conflict.resolved_winner, b.id);
|
|
1126
|
+
assert.equal(data.conflict.disposition.ok, true);
|
|
1127
|
+
assert.equal(store.getById(b.id).archived, false, "winner kept");
|
|
1128
|
+
assert.equal(store.getById(loserId).archived, true, "loser archived by apply");
|
|
1129
|
+
|
|
1130
|
+
// 处理过的 id 再 resolve → 幂等重盖(仍是 200;队列视图只列未解决行,
|
|
1131
|
+
// 正常 UI 不会再发这个请求,但接口层保持幂等)
|
|
1132
|
+
const res2 = new FakeRes();
|
|
1133
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: pending.id, winner: "a" }), res2);
|
|
1134
|
+
assert.equal(res2.statusCode, 200);
|
|
1135
|
+
assert.equal(JSON.parse(res2.body).ok, true);
|
|
1136
|
+
|
|
1137
|
+
// 未知 id → 404;缺 id → 400
|
|
1138
|
+
const res3 = new FakeRes();
|
|
1139
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: "nope", winner: "a" }), res3);
|
|
1140
|
+
assert.equal(res3.statusCode, 404);
|
|
1141
|
+
const res4 = new FakeRes();
|
|
1142
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", {}), res4);
|
|
1143
|
+
assert.equal(res4.statusCode, 400);
|
|
1144
|
+
|
|
1145
|
+
// apply:false 只盖章不处置
|
|
1146
|
+
const c = service.saveWithDedupe({ type: "decision", title: "丙", content: "内容丙" }).memory;
|
|
1147
|
+
const d = service.saveWithDedupe({ type: "decision", title: "丁", content: "内容丁" }).memory;
|
|
1148
|
+
const p2 = service.saveConflictPending({ memory_a: c.id, memory_b: d.id, reason: "r2" });
|
|
1149
|
+
const res5 = new FakeRes();
|
|
1150
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: p2.id, apply: false }), res5);
|
|
1151
|
+
assert.equal(res5.statusCode, 200);
|
|
1152
|
+
assert.equal(JSON.parse(res5.body).conflict.disposition, null);
|
|
1153
|
+
assert.equal(store.getById(c.id).archived, false);
|
|
1154
|
+
assert.equal(store.getById(d.id).archived, false);
|
|
1155
|
+
});
|
|
1156
|
+
|
|
991
1157
|
test("parseHumanEdits is the pure core of readHumanEdits (CRLF tolerant)", () => {
|
|
992
1158
|
const digest = "0".repeat(64);
|
|
993
1159
|
const text = [
|