dsh-memu-memory 1.3.6 → 1.3.8

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.
Files changed (61) hide show
  1. package/AGENTS.md +2 -1
  2. package/CHANGELOG.md +87 -0
  3. package/README.md +7 -7
  4. package/README.zh.md +7 -7
  5. package/docs/client-conventions.md +4 -0
  6. package/docs/contributing.md +2 -2
  7. package/docs/memory-semantics.md +10 -7
  8. package/docs/troubleshooting.md +132 -7
  9. package/lib/binding-diagnostics.js +2 -2
  10. package/lib/client.js +296 -122
  11. package/lib/consolidation-ledger.js +6 -2
  12. package/lib/consolidation-worker.js +7 -0
  13. package/lib/inbound.js +24 -9
  14. package/lib/inject.js +379 -215
  15. package/lib/lexicon.js +5 -0
  16. package/lib/local-embedding-manager.js +4 -0
  17. package/lib/local-vector-service.js +65 -1
  18. package/lib/memory.js +711 -175
  19. package/lib/paths.js +24 -0
  20. package/lib/private.js +284 -127
  21. package/lib/rpc.js +144 -45
  22. package/lib/snippet.js +49 -28
  23. package/lib/state.js +185 -26
  24. package/lib/tools.js +150 -31
  25. package/lib/webui/standalone.css +28 -1
  26. package/lib/webui.js +25 -1
  27. package/lib/worker-pool.js +260 -26
  28. package/package.json +1 -1
  29. package/scripts/check-i18n.mjs +19 -0
  30. package/scripts/recall-bench.mjs +4 -4
  31. package/scripts/simulate-injection.mjs +3 -0
  32. package/tests/activity-wiring.test.mjs +52 -0
  33. package/tests/client-render.test.mjs +207 -0
  34. package/tests/consolidation-feed.test.mjs +26 -0
  35. package/tests/consolidation-fork.test.mjs +618 -33
  36. package/tests/consolidation-snapshot.test.mjs +2 -1
  37. package/tests/cross-library-recall.test.mjs +156 -0
  38. package/tests/cue.test.mjs +15 -2
  39. package/tests/hybrid-recall.test.mjs +488 -0
  40. package/tests/inbound.test.mjs +49 -4
  41. package/tests/inject-conflict.test.mjs +70 -9
  42. package/tests/inject-guide.test.mjs +13 -1
  43. package/tests/inject.test.mjs +10 -0
  44. package/tests/injection-budget.test.mjs +101 -0
  45. package/tests/injection-chain.test.mjs +37 -0
  46. package/tests/injection-rollback.test.mjs +172 -9
  47. package/tests/issue2-chain.test.mjs +169 -0
  48. package/tests/lexicon-inject.test.mjs +33 -5
  49. package/tests/lexicon-ui.test.mjs +99 -2
  50. package/tests/managed-memory.test.mjs +625 -1
  51. package/tests/media.test.mjs +1 -1
  52. package/tests/new-session-gate.test.mjs +280 -25
  53. package/tests/open-loop-date.test.mjs +11 -1
  54. package/tests/recall-intent.test.mjs +52 -3
  55. package/tests/simulate-injection.test.mjs +4 -4
  56. package/tests/snippet.test.mjs +33 -1
  57. package/tests/state.test.mjs +8 -3
  58. package/tests/tool-schema.test.mjs +52 -0
  59. package/tests/tools.test.mjs +394 -0
  60. package/tests/webui.test.mjs +16 -2
  61. package/tests/worker-lifecycle.test.mjs +419 -36
package/lib/lexicon.js CHANGED
@@ -97,6 +97,11 @@ function lexiconDir() {
97
97
  // 是某本私有库——`data/lexicon/global.json` 与 `index/global.json`(全局记忆)对齐。
98
98
  export const GLOBAL_LEXICON_ID = 'global'
99
99
 
100
+ // 「全部词典」聚合浏览的保留作用域名(不是存储):lexiconList 收到它时合并全局词典
101
+ // 与所有私有库的词条,只读聚合,写操作仍按词条所属库路由。与 GLOBAL_LEXICON_ID 一样
102
+ // 不可能撞私有库 id(`lib_<hex>`)。前端 client.js 复写同一个值,漂移由测试钉住。
103
+ export const ALL_LEXICON_ID = 'all'
104
+
100
105
  function lexiconFile(libraryId) {
101
106
  // isSafeId 只放行 [A-Za-z0-9._-]{1,128} 且拒绝 . / ..:绝对路径、反斜杠、
102
107
  // ../ 和任意文件名在这里就死掉,不会拼进路径。
@@ -36,6 +36,10 @@ export function childEnvironment(extra, { platform = process.platform, env = pro
36
36
  if (typeof value !== 'string') continue
37
37
  const lower = name.toLowerCase()
38
38
  if (DENIED_ENV_NAMES.includes(lower) || lower.includes('proxy')) continue
39
+ // 2026-11-15 sweep: the vendor prefix list chases a moving target (glm_ leaked).
40
+ // Generalize to the documented intent: any API_KEY is a cloud credential and
41
+ // never reaches the worker environment.
42
+ if (lower === 'api_key' || lower.endsWith('_api_key')) continue
39
43
  if (DENIED_ENV_PREFIXES.some(prefix => lower.startsWith(prefix))) continue
40
44
  inherited[name] = value
41
45
  }
@@ -1,6 +1,7 @@
1
1
  import fs from 'node:fs'
2
2
  import path from 'node:path'
3
3
  import { downloadsDir, warn } from './paths.js'
4
+ import { record } from './activity-log.js'
4
5
  import { listLibraries, loadPrivateIndex, loadGlobalIndex, saveGlobalIndex, updatePrivateIndex } from './state.js'
5
6
  import { commit as memuCommit, listFiles as memuListFiles, forget as memuForget } from './memu.js'
6
7
  import { getEmbeddingCatalog, prefixesRequireRole, resolveEmbeddingSources } from './embedding-catalog.js'
@@ -262,7 +263,8 @@ export function getLocalVectorState() {
262
263
  catalog,
263
264
  installations: Object.fromEntries(catalog.map(entry => [entry.id, installationFor(entry.id, runtime)])),
264
265
  runtime,
265
- vector: { ...vectorStatus(), indexed },
266
+ // 2026-11-15 扫雷:向量代际缺库显式化——WebUI/RPC 一眼看到哪个库没进向量库、怎么办。
267
+ vector: { ...vectorStatus(), indexed, missingLibraries: getMissingVectorLibraries({ status: vectorStatus() }) },
266
268
  }
267
269
  }
268
270
 
@@ -311,6 +313,68 @@ export function abortLocalVectorMigration() {
311
313
  return { result, state: getLocalVectorState() }
312
314
  }
313
315
 
316
+ // 2026-11-15 扫雷补强:向量代际缺库的显式探测与自动追平。
317
+ // 背景:非空库不在当前代际 databases.libraries 里时(profile 切换/迁移中断时缺席),
318
+ // 写入侧 enroll 抛 REBUILD_REQUIRED → commit 降级"仅写索引",召回侧语义腿无 db。
319
+ // 之前只有静默降级,用户没有可行动线索;这里给出状态出口 + 一次自动 force 重建。
320
+ /** 列出「索引非空但不在当前向量代际」的私有库。空库不算:写入路径的 enroll 会自愈。 */
321
+ export function getMissingVectorLibraries({ status = null } = {}) {
322
+ const view = status || vectorStatus()
323
+ if (view.status !== 'active' || !view.active?.databases) return []
324
+ const enrolled = new Set(Object.keys(view.active.databases.libraries || {}))
325
+ const missing = []
326
+ for (const item of listLibraries()) {
327
+ const libraryId = String(item?.id || item || '')
328
+ if (!libraryId || libraryId === 'global' || enrolled.has(libraryId)) continue
329
+ let entries = 0
330
+ try { entries = Object.keys(loadPrivateIndex(libraryId).entries || {}).length } catch { continue }
331
+ if (entries > 0) missing.push(libraryId)
332
+ }
333
+ return missing
334
+ }
335
+
336
+ // 每个向量代际最多自动重建 2 次(防 embed 端点不可用时空转重试);进程重启后清零,
337
+ // 重启后再试一次是可接受的(缺库状态本身就在账上,不会丢)。
338
+ const autoRebuildAttempts = new Map()
339
+ let autoRebuildInFlight = false
340
+
341
+ export function autoRebuildAttemptCount(generation) {
342
+ return Number(autoRebuildAttempts.get(generation)) || 0
343
+ }
344
+
345
+ /**
346
+ * 探测到缺库时自动 force 重建向量代际(重嵌入全部库,本地 worker,无 API 费用)。
347
+ * 幂等护栏:向量未激活 / 有迁移在途 / 本进程同代际已试过 2 次 / 已有重建在跑 → 跳过。
348
+ * 失败不抛——调用方(召回降级分支)是热路径,结果走 activity-log 可观测。
349
+ */
350
+ export async function maybeAutoRebuildVectorGeneration() {
351
+ if (autoRebuildInFlight) return { skipped: 'already-in-flight' }
352
+ const view = vectorStatus()
353
+ if (view.status !== 'active' || !view.active) return { skipped: 'vector-not-active' }
354
+ if (view.pending) return { skipped: 'migration-in-progress' }
355
+ const missing = getMissingVectorLibraries({ status: view })
356
+ if (!missing.length) return { skipped: 'nothing-missing' }
357
+ const generation = view.active.generation
358
+ const attempts = Number(autoRebuildAttempts.get(generation)) || 0
359
+ if (attempts >= 2) return { skipped: 'attempt-cap-reached', generation, missing }
360
+ autoRebuildAttempts.set(generation, attempts + 1)
361
+ autoRebuildInFlight = true
362
+ try {
363
+ warn('vector auto-rebuild: generation ' + generation + ' is missing libraries (' + missing.join(', ') + ') — forcing a shadow rebuild')
364
+ record({ kind: 'vector', level: 'warn', scope: 'global', message: '向量代际缺库,自动 force 重建开始', detail: { generation, missing } })
365
+ const result = await switchLocalVectorProfile({ id: view.active.profile?.catalogId ?? null, force: true })
366
+ record({ kind: 'vector', level: 'info', scope: 'global', message: '向量代际缺库,自动 force 重建结束', detail: { generation, missing, result: result?.result ?? null } })
367
+ return { started: true, generation, missing, result: result?.result ?? null }
368
+ } catch (error) {
369
+ const message = String(error?.message || error)
370
+ warn('vector auto-rebuild failed: ' + message)
371
+ record({ kind: 'vector', level: 'warn', scope: 'global', message: '向量代际缺库,自动 force 重建失败', detail: { generation, missing, error: message.slice(0, 300) } })
372
+ return { started: false, generation, missing, error: message.slice(0, 300) }
373
+ } finally {
374
+ autoRebuildInFlight = false
375
+ }
376
+ }
377
+
314
378
  /**
315
379
  * 启动 loopback worker 并真的打一次 POST /v1/embeddings:200 + 正确维度 + 有限数值才算就绪。
316
380
  * 失败一律带 fallback: 'lexical',上游照旧 fail-open,但错误必须留在日志/lastRecallError 里。