dsh-memu-memory 1.2.6 → 1.2.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 (68) hide show
  1. package/AGENTS.md +1 -1
  2. package/README.md +10 -15
  3. package/README.zh.md +10 -15
  4. package/docs/contributing.md +1 -1
  5. package/docs/host-adaptation.md +1 -1
  6. package/docs/memory-semantics.md +26 -4
  7. package/docs/troubleshooting.md +3 -1
  8. package/lib/backup.js +1 -1
  9. package/lib/binding-diagnostics.js +2 -2
  10. package/lib/budget.js +31 -0
  11. package/lib/client.js +276 -28
  12. package/lib/commitment.js +138 -0
  13. package/lib/consolidation-receipt.js +2 -0
  14. package/lib/consolidation-worker.js +3 -0
  15. package/lib/curator-policy.js +98 -0
  16. package/lib/fork-model.js +312 -0
  17. package/lib/freshness.js +24 -0
  18. package/lib/inject.js +117 -10
  19. package/lib/lexicon.js +1 -1
  20. package/lib/local-vector-service.js +4 -1
  21. package/lib/memory.js +600 -43
  22. package/lib/memu.js +4 -0
  23. package/lib/private.js +49 -8
  24. package/lib/prompts/private-consolidation.mixed.md +3 -2
  25. package/lib/prompts/private-consolidation.rp.md +7 -1
  26. package/lib/prompts/private-policy.mixed.md +1 -0
  27. package/lib/prompts/private-policy.rp.md +5 -1
  28. package/lib/prompts/system-guide.rp.md +3 -0
  29. package/lib/recall-cache.js +212 -0
  30. package/lib/rpc.js +44 -6
  31. package/lib/snippet.js +25 -0
  32. package/lib/state.js +43 -1
  33. package/lib/temporal.js +329 -0
  34. package/lib/text-retrieval.js +292 -28
  35. package/lib/tokenizer-v2.js +295 -0
  36. package/lib/tools.js +81 -4
  37. package/lib/webui/standalone.css +17 -1
  38. package/package.json +1 -1
  39. package/scripts/bench-recall.mjs +142 -0
  40. package/scripts/check-seed-compat.mjs +118 -0
  41. package/tests/backup.test.mjs +2 -2
  42. package/tests/commitment.test.mjs +144 -0
  43. package/tests/consolidation-fork.test.mjs +122 -5
  44. package/tests/curator-clusters.test.mjs +7 -1
  45. package/tests/curator-pairing.test.mjs +52 -8
  46. package/tests/disclosure.test.mjs +34 -17
  47. package/tests/dynamic-rrf.test.mjs +179 -0
  48. package/tests/fact-version.test.mjs +128 -0
  49. package/tests/fork-model.test.mjs +345 -0
  50. package/tests/help-page.test.mjs +1 -1
  51. package/tests/inject-conflict.test.mjs +94 -0
  52. package/tests/inject-guide.test.mjs +5 -3
  53. package/tests/inject.test.mjs +2 -2
  54. package/tests/injection-intent-budget.test.mjs +92 -0
  55. package/tests/lexicon-frequency.test.mjs +1 -1
  56. package/tests/loader.mjs +3 -0
  57. package/tests/memory-chain.test.mjs +140 -0
  58. package/tests/memory-fields.test.mjs +134 -0
  59. package/tests/memu-resident.test.mjs +33 -0
  60. package/tests/negation-polarity.test.mjs +135 -0
  61. package/tests/prompt-semantics.test.mjs +51 -0
  62. package/tests/query-decompose.test.mjs +62 -0
  63. package/tests/recall-cache.test.mjs +292 -0
  64. package/tests/recall-intent.test.mjs +100 -0
  65. package/tests/speaker-provenance.test.mjs +120 -0
  66. package/tests/stubs/dsh-llm-pi-ai.js +13 -0
  67. package/tests/temporal.test.mjs +124 -0
  68. package/tests/tokenizer-v2.test.mjs +142 -0
@@ -0,0 +1,142 @@
1
+ import test from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import {
4
+ normalizeAndLower,
5
+ tokenize,
6
+ tokenizeStats,
7
+ tokenizeV2,
8
+ TEST_CASES,
9
+ tokenStreamV2,
10
+ } from '../lib/tokenizer-v2.js'
11
+
12
+ // —— 召回升级 P0-1:Tokenizer V2(CJK 三层 n-gram + 实体保护)——
13
+ // 开关在 lib/curator-policy.js(TOKENIZER_V2);这里测 V2 本身的行为契约。
14
+
15
+ test('normalizeAndLower: NFKC 折叠全角并小写化', () => {
16
+ assert.equal(normalizeAndLower('ABc DEF'), 'abc def')
17
+ assert.equal(normalizeAndLower('Cache-COHERENCE'), 'cache-coherence')
18
+ assert.equal(normalizeAndLower(null), '')
19
+ })
20
+
21
+ test('纯中文短句:4 字 run 出 unigram+bigram+trigram+完整 run,插入序确定', () => {
22
+ // Level 1 unigram → Level 2 bigram → Level 3 trigram → 完整 run 收尾
23
+ assert.deepEqual(tokenize('部署窗口'), [
24
+ '部', '署', '窗', '口',
25
+ '部署', '署窗', '窗口',
26
+ '部署窗', '署窗口',
27
+ '部署窗口',
28
+ ])
29
+ })
30
+
31
+ test('纯中文短句:2 字 run 不重复收 bigram(流里 tf 不虚高)', () => {
32
+ assert.deepEqual(tokenize('缓存 缓存'), ['缓', '存', '缓存'])
33
+ })
34
+
35
+ test('中英混合:日期实体完整保留、英文同 V1 口径、CJK 三层展开', () => {
36
+ const tokens = tokenize('2026-11-05 AI 技术在快速发展')
37
+ // 实体:完整日期,不被拆散(Set 插入序让实体排最前)
38
+ assert.ok(tokens.includes('2026-11-05'))
39
+ // 英文:≥2 字符的 latin 词
40
+ assert.ok(tokens.includes('ai'))
41
+ // CJK:unigram/bigram/trigram 三层都在
42
+ assert.ok(tokens.includes('技') && tokens.includes('技术') && tokens.includes('速发展'))
43
+ // 长句 run(7 字 > 4)不出完整 run token
44
+ assert.ok(!tokens.includes('技术在快速发展'))
45
+ })
46
+
47
+ test('日期/时间实体:周一 / 星期一 / 周日 都能成整 token(空格 bug 已修)', () => {
48
+ assert.equal(tokenize('周一开会')[0], '周一')
49
+ assert.ok(tokenize('星期一例会').includes('星期一'))
50
+ assert.ok(tokenize('周日 拖地').includes('周日'))
51
+ assert.ok(tokenize('星期天大扫除').includes('星期天'))
52
+ })
53
+
54
+ test('日期/时间实体:YYYY-MM-DD 整体保留;M月D日 成整 token', () => {
55
+ assert.deepEqual(tokenize('截止 2026-11-05 完成'), ['2026-11-05', '截', '止', '截止', '完', '成', '完成'])
56
+ assert.ok(tokenize('11月5日 提交').includes('11月5日'))
57
+ })
58
+
59
+ test('数字/百分比:独立数字与百分比都成 token;标识符内部的数字不拆出噪声', () => {
60
+ assert.deepEqual(tokenize('增长 85%').slice(0, 2), ['85', '85%'])
61
+ // 'v2.today' 不拆出 '2';'30-40' 由英文连接词口径整收
62
+ assert.deepEqual(tokenize('use dsh-memu-memory_v2.today ok'), ['use', 'dsh-memu-memory_v2.today', 'ok'])
63
+ assert.ok(!tokenize('Qwen3-Embedding 在 2026 年发布').includes('3'))
64
+ assert.ok(tokenize('Qwen3-Embedding 在 2026 年发布').includes('2026'))
65
+ // 完整日期的子串(2026/11/05)不再作为数字实体重复出现
66
+ assert.ok(!tokenize('2026-11-05 提交').includes('2026'))
67
+ })
68
+
69
+ test('邮箱与 URL:整体成 token;URL 句尾标点被剥掉', () => {
70
+ assert.ok(tokenize('发到 user@example.com 即可').includes('user@example.com'))
71
+ assert.ok(tokenize('看 https://example.com/docs。').includes('https://example.com/docs'))
72
+ assert.ok(!tokenize('看 https://example.com/docs。').includes('https://example.com/docs。'))
73
+ })
74
+
75
+ test('单字 run:只保留 unigram;跨标点的 run 各自独立', () => {
76
+ assert.deepEqual([...tokenizeV2('猫')], ['猫'])
77
+ assert.deepEqual(tokenize('猫。狗'), ['猫', '狗'])
78
+ })
79
+
80
+ test('长 run 的 trigram 边界:>4 字不出完整 run,首尾 trigram 都在', () => {
81
+ const tokens = tokenize('人工智能技术') // 6 字
82
+ assert.ok(tokens.includes('人工智'), '首个 trigram')
83
+ assert.ok(tokens.includes('能技术'), '末尾 trigram')
84
+ assert.ok(!tokens.includes('人工智能技术'), '长 run 不收完整串')
85
+ // 4 字 run 是唯一补收完整串的长度
86
+ assert.ok(tokenize('部署窗口').includes('部署窗口'))
87
+ })
88
+
89
+ test('英文与 NFKC 口径与 V1 完全一致:连接词、全角折叠、单字符丢弃', () => {
90
+ assert.deepEqual(tokenize('use dsh-memu-memory_v2.today ok'), ['use', 'dsh-memu-memory_v2.today', 'ok'])
91
+ assert.deepEqual(tokenize('a b cd'), ['cd'])
92
+ assert.deepEqual(tokenize('per-turn retry'), ['per-turn', 'retry'])
93
+ assert.deepEqual(tokenize('RISCV CACHE CACHE'), ['riscv', 'cache'])
94
+ })
95
+
96
+ test('去重确定性:同输入两次调用结果一致(含 TEST_CASES 全部场景)', () => {
97
+ for (const { input } of TEST_CASES) {
98
+ assert.deepEqual(tokenize(input), tokenize(input), input)
99
+ assert.deepEqual([...tokenizeV2(input)], [...tokenizeV2(input)], input)
100
+ }
101
+ })
102
+
103
+ test('tokenize 兼容接口:返回数组,且与 tokenizeV2 的 Set 展开一致', () => {
104
+ assert.equal(Array.isArray(tokenize('部署窗口')), true)
105
+ assert.deepEqual(tokenize('部署窗口'), [...tokenizeV2('部署窗口')])
106
+ })
107
+
108
+ test('tokenStreamV2 与 tokenizeV2 一致:流的去重集合 ⊆/= V2 的 Set,且含重复语义', () => {
109
+ const stream = tokenStreamV2('缓存 2026-11-05 缓存过期')
110
+ const set = tokenizeV2('缓存 2026-11-05 缓存过期')
111
+ for (const token of stream) assert.equal(set.has(token), true, `流 token ${token} 必须在 Set 中`)
112
+ assert.ok(stream.length >= set.size, '流不去重,长度不小于集合')
113
+ // 同一生成器:流去重后与 Set 完全相等
114
+ assert.deepEqual(new Set(stream), set)
115
+ // 重复出现的词在流里保留频率(BM25 tf 的来源)
116
+ const repeated = tokenStreamV2('缓存缓存')
117
+ assert.ok(repeated.filter((t) => t === '缓存').length >= 2)
118
+ })
119
+
120
+ test('tokenizeStats:total 正确计数(数组 length),byType 覆盖全部 token', () => {
121
+ const stats = tokenizeStats('猫 2026-11-05 user@example.com https://a.io 85% 部署窗')
122
+ assert.equal(stats.total, 16)
123
+ const sum = Object.values(stats.byType).reduce((a, b) => a + b, 0)
124
+ assert.equal(sum, stats.total, 'byType 各类之和必须等于 total')
125
+ assert.equal(stats.byType.date_iso, 1)
126
+ assert.equal(stats.byType.email, 1)
127
+ assert.equal(stats.byType.url, 1)
128
+ assert.equal(stats.byType.percent, 1)
129
+ assert.equal(stats.byType.number, 1) // 85(日期子串不再算数字)
130
+ assert.equal(stats.byType.cjk_unigram, 4) // 猫/部/署/窗
131
+ assert.equal(stats.byType.cjk_bigram, 2) // 部署/署窗
132
+ assert.equal(stats.byType.cjk_trigram, 1) // 部署窗
133
+ })
134
+
135
+ test('TEST_CASES 导出形状不变:input/description 字段齐全且可分词', () => {
136
+ assert.ok(Array.isArray(TEST_CASES) && TEST_CASES.length >= 4)
137
+ for (const item of TEST_CASES) {
138
+ assert.equal(typeof item.input, 'string')
139
+ assert.equal(typeof item.description, 'string')
140
+ assert.ok(tokenizeV2(item.input).size > 0)
141
+ }
142
+ })