dsh-sixlayer-memory 0.0.0-stage → 2.0.3
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/CHANGELOG.md +123 -0
- package/README.md +292 -3
- package/catalog/.index.json +111704 -0
- package/catalog/parts.json +8808 -0
- package/cordis.patch.yml +8 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/preview.svg +51 -0
- package/docs//347/224/237/345/221/275/345/221/250/346/234/237-/350/207/252/345/212/250/345/217/254/345/233/236/344/270/216/345/200/231/351/200/211.md +103 -0
- package/icon.svg +8 -0
- package/lib/client.js +1209 -0
- package/lib/index.js +741 -0
- package/lib/memory/affect-alert.js +60 -0
- package/lib/memory/affect-events.js +201 -0
- package/lib/memory/affect-report.js +61 -0
- package/lib/memory/api.js +194 -0
- package/lib/memory/crypto.js +68 -0
- package/lib/memory/detail.js +43 -0
- package/lib/memory/embed.js +394 -0
- package/lib/memory/emotion.js +99 -0
- package/lib/memory/explain.js +207 -0
- package/lib/memory/importer.js +215 -0
- package/lib/memory/inject.js +193 -0
- package/lib/memory/ledger.js +101 -0
- package/lib/memory/lifecycle.js +491 -0
- package/lib/memory/notify.js +164 -0
- package/lib/memory/policy.js +182 -0
- package/lib/memory/qq-official.js +196 -0
- package/lib/memory/qq.js +326 -0
- package/lib/memory/recorder.js +241 -0
- package/lib/memory/redact.js +120 -0
- package/lib/memory/scanners.js +142 -0
- package/lib/memory/schema.js +205 -0
- package/lib/memory/service-plugin.js +69 -0
- package/lib/memory/service.js +123 -0
- package/lib/memory/shadow.js +133 -0
- package/lib/memory/store.js +1138 -0
- package/lib/memory/timeline.js +74 -0
- package/lib/memory/tokenize.js +66 -0
- package/lib/memory/tools.js +1371 -0
- package/lib/memory/util.js +129 -0
- package/lib/parts/index.js +154 -0
- package/lib/parts/installer.js +294 -0
- package/lib/parts/jobs.js +91 -0
- package/lib/parts/library.js +91 -0
- package/lib/probe-plugin.js +38 -0
- package/lib/voice/board-asr.js +31 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +114 -3
- package/tools/backfill-affect.mjs +82 -0
- package/tools/eval.mjs +88 -0
- package/tools/p1.mjs +108 -0
- package/tools/p10.mjs +36 -0
- package/tools/p11.mjs +51 -0
- package/tools/p12.mjs +53 -0
- package/tools/p13.mjs +65 -0
- package/tools/p14.mjs +66 -0
- package/tools/p15.mjs +43 -0
- package/tools/p16.mjs +42 -0
- package/tools/p17.mjs +29 -0
- package/tools/p18.mjs +48 -0
- package/tools/p19.mjs +50 -0
- package/tools/p2.mjs +59 -0
- package/tools/p20.mjs +54 -0
- package/tools/p21.mjs +42 -0
- package/tools/p22.mjs +32 -0
- package/tools/p23.mjs +43 -0
- package/tools/p24.mjs +40 -0
- package/tools/p25.mjs +151 -0
- package/tools/p26.mjs +113 -0
- package/tools/p27.mjs +89 -0
- package/tools/p28.mjs +60 -0
- package/tools/p3.mjs +54 -0
- package/tools/p4.mjs +73 -0
- package/tools/p5.mjs +117 -0
- package/tools/p6.mjs +122 -0
- package/tools/p8.mjs +50 -0
- package/tools/p9.mjs +35 -0
- package/tools/phase2.mjs +159 -0
- package/tools/preflight.mjs +320 -0
- package/tools/push-via-api.mjs +187 -0
- package/tools/quarterly.mjs +129 -0
- package/tools/refresh-catalog.mjs +60 -0
- package/tools/register-bundle.mjs +38 -0
- package/tools/review.mjs +156 -0
- package/tools/selfcheck.mjs +80 -0
- package/tools/smoke.mjs +112 -0
- package/tools/testset-v1.json +70 -0
- package/tools/validate-tools.mjs +150 -0
- package/tools/verify-all.mjs +39 -0
- package/tools/write-chain.mjs +139 -0
package/tools/p5.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// P5 回归:派单状态机 / 台账 / 回执 / 修改权分档(迁移自 luanyujia-workbench 协作机制)。
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import MemoryStore from '../lib/memory/store.js'
|
|
6
|
+
import { canWrite } from '../lib/memory/policy.js'
|
|
7
|
+
import { buildRegistry } from '../lib/memory/tools.js'
|
|
8
|
+
|
|
9
|
+
let pass = 0
|
|
10
|
+
let fail = 0
|
|
11
|
+
function check(name, ok, detail = '') {
|
|
12
|
+
if (ok) {
|
|
13
|
+
pass++
|
|
14
|
+
console.log(' PASS ' + name)
|
|
15
|
+
} else {
|
|
16
|
+
fail++
|
|
17
|
+
console.log(' FAIL ' + name + (detail ? ' ' + detail : ''))
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-p5-'))
|
|
22
|
+
try {
|
|
23
|
+
const store = new MemoryStore({ dataDir: dir }).open()
|
|
24
|
+
check('schema 升到 v9', store.getMeta('schema_version') === '9', String(store.getMeta('schema_version')))
|
|
25
|
+
check('tasks 表存在', Boolean(store.db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='tasks'").get()))
|
|
26
|
+
|
|
27
|
+
// 四件套
|
|
28
|
+
let missErr = ''
|
|
29
|
+
try {
|
|
30
|
+
store.dispatchCreate({ from: 'A', to: 'B', title: '改索引' })
|
|
31
|
+
} catch (e) {
|
|
32
|
+
missErr = e.message
|
|
33
|
+
}
|
|
34
|
+
check('派单缺四件套被拒', missErr.includes('四件套'), missErr)
|
|
35
|
+
|
|
36
|
+
const t = store.dispatchCreate({
|
|
37
|
+
from: 'A', to: 'B', title: '补索引',
|
|
38
|
+
phenomenon: '体检报 3 条未登记', howTo: '照 §6.4', howVerify: '跑体检应 0', deadlineMinutes: 60,
|
|
39
|
+
})
|
|
40
|
+
check('派单创建成功', t.status === 'sent' && t.kind === 'dispatch' && t.deadline_at, JSON.stringify(t))
|
|
41
|
+
|
|
42
|
+
// 公告不需要四件套
|
|
43
|
+
const n = store.dispatchCreate({ kind: 'notice', from: 'A', to: 'B', title: '以后这么办' })
|
|
44
|
+
check('公告可只写标题', n.kind === 'notice' && n.status === 'sent')
|
|
45
|
+
|
|
46
|
+
// 回执必须带证据
|
|
47
|
+
let evErr = ''
|
|
48
|
+
try {
|
|
49
|
+
store.taskReceipt({ taskId: t.task_id, conclusion: '好了' })
|
|
50
|
+
} catch (e) {
|
|
51
|
+
evErr = e.message
|
|
52
|
+
}
|
|
53
|
+
check('回执缺证据被拒', evErr.includes('证据'), evErr)
|
|
54
|
+
|
|
55
|
+
const r = store.taskReceipt({ taskId: t.task_id, conclusion: '已补齐 3 条', evidence: '体检 ⛔0 ⚠0 ✅4', path: 'x/索引.md' })
|
|
56
|
+
check('回执写入成功', r.status === 'acked' && r.ack.evidence.includes('⛔0') && r.messages === 2, JSON.stringify(r))
|
|
57
|
+
|
|
58
|
+
// ≤3 条上限
|
|
59
|
+
store.taskReceipt({ taskId: t.task_id, conclusion: '补充', evidence: '再跑一次通过' })
|
|
60
|
+
let capErr = ''
|
|
61
|
+
try {
|
|
62
|
+
store.taskReceipt({ taskId: t.task_id, conclusion: '再来', evidence: 'x' })
|
|
63
|
+
} catch (e) {
|
|
64
|
+
capErr = e.message
|
|
65
|
+
}
|
|
66
|
+
check('同一件事超 3 条被拒', capErr.includes('3 条'), capErr)
|
|
67
|
+
|
|
68
|
+
// 状态推进
|
|
69
|
+
const done = store.taskMark({ taskId: t.task_id, status: 'done' })
|
|
70
|
+
check('acked → done 销账', done.status === 'done')
|
|
71
|
+
let bad = ''
|
|
72
|
+
try {
|
|
73
|
+
store.taskMark({ taskId: n.task_id, status: 'done' })
|
|
74
|
+
} catch (e) {
|
|
75
|
+
bad = e.message
|
|
76
|
+
}
|
|
77
|
+
check('只有 acked 能销账', bad.includes('acked'), bad)
|
|
78
|
+
|
|
79
|
+
// 台账
|
|
80
|
+
const out = store.taskList({ scene: 'A', scope: 'outbound' })
|
|
81
|
+
const inbound = store.taskList({ scene: 'B', scope: 'inbound' })
|
|
82
|
+
check('台账:我发出没回的', out.length === 1 && out[0].kind === 'notice', JSON.stringify(out.map((x) => x.kind)))
|
|
83
|
+
check('台账:别人等我回的', inbound.length === 1, String(inbound.length))
|
|
84
|
+
|
|
85
|
+
// 超时判定
|
|
86
|
+
store.db.prepare("UPDATE tasks SET deadline_at = '2000-01-01T00:00:00.000Z' WHERE task_id = ?").run(n.task_id)
|
|
87
|
+
const overdue = store.taskList({ scene: 'A', scope: 'outbound' })
|
|
88
|
+
check('超时未回执被标出', overdue[0].overdue === true && overdue[0].effective_status === 'overdue')
|
|
89
|
+
|
|
90
|
+
// 修改权分档
|
|
91
|
+
check('D 档:自己的地盘可写', canWrite({ actor: 'A', target: 'A' }).allowed === true)
|
|
92
|
+
const e = canWrite({ actor: 'A', target: 'B' })
|
|
93
|
+
check('E 档:别人的地盘拒绝', e.allowed === false && e.tier === 'E' && e.request)
|
|
94
|
+
check('A 档:负责人可改', canWrite({ actor: 'owner', target: 'rule', kind: 'rule', ruleOwner: 'owner' }).allowed === true)
|
|
95
|
+
check('A 档:非负责人只能提请求', canWrite({ actor: 'A', target: 'rule', kind: 'rule', ruleOwner: 'owner' }).allowed === false)
|
|
96
|
+
check('B 档:只能写自己的行', canWrite({ actor: 'A', target: 'board', kind: 'board', rowOwner: 'A' }).allowed === true)
|
|
97
|
+
check('B 档:不能写别人的行', canWrite({ actor: 'A', target: 'board', kind: 'board', rowOwner: 'B' }).allowed === false)
|
|
98
|
+
|
|
99
|
+
// 工具注册
|
|
100
|
+
const host = { open() {}, injected() { return [] }, closeAll() {} }
|
|
101
|
+
const registry = buildRegistry(store, host)
|
|
102
|
+
for (const name of ['mem_dispatch', 'mem_receipt', 'mem_task', 'mem_scope']) {
|
|
103
|
+
check('注册了 ' + name, registry.has(name))
|
|
104
|
+
}
|
|
105
|
+
const out5 = await registry.get('mem_dispatch').definition.execute({
|
|
106
|
+
from: 'A', to: 'B', title: '工具派单',
|
|
107
|
+
phenomenon: 'p', how_to: 'h', how_verify: 'v',
|
|
108
|
+
})
|
|
109
|
+
check('工具可建派单', out5.status === 'sent' && out5.task_id)
|
|
110
|
+
|
|
111
|
+
store.close()
|
|
112
|
+
} finally {
|
|
113
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
console.log(`\nP5 ${fail === 0 ? 'OK' : 'FAILED'} pass=${pass} fail=${fail}`)
|
|
117
|
+
if (fail > 0) process.exitCode = 1
|
package/tools/p6.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// P6 回归:服务化契约(recall / remember / forget / status)+ 注册行为。
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import MemoryStore from '../lib/memory/store.js'
|
|
6
|
+
import { buildService, SERVICE_API_VERSION } from '../lib/memory/service.js'
|
|
7
|
+
import { apply as applyServicePlugin } from '../lib/memory/service-plugin.js'
|
|
8
|
+
|
|
9
|
+
let pass = 0
|
|
10
|
+
let fail = 0
|
|
11
|
+
function check(name, ok, detail = '') {
|
|
12
|
+
if (ok) {
|
|
13
|
+
pass++
|
|
14
|
+
console.log(' PASS ' + name)
|
|
15
|
+
} else {
|
|
16
|
+
fail++
|
|
17
|
+
console.log(' FAIL ' + name + (detail ? ' ' + detail : ''))
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ---------- 1. 服务契约(直接调用) ----------
|
|
22
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-p6-'))
|
|
23
|
+
try {
|
|
24
|
+
const store = new MemoryStore({ dataDir: dir }).open()
|
|
25
|
+
const svc = buildService({ store })
|
|
26
|
+
|
|
27
|
+
check('apiVersion = 1', svc.apiVersion === SERVICE_API_VERSION)
|
|
28
|
+
check('四个方法都在', ['recall', 'remember', 'forget', 'status'].every((k) => typeof svc[k] === 'function'))
|
|
29
|
+
check('没有暴露 handoff', svc.handoff === undefined)
|
|
30
|
+
|
|
31
|
+
// remember:未验证来源 → 走闸门,落 pending
|
|
32
|
+
const w = svc.remember({ content: '插件写入的一条未验证笔记,用于测试闸门。', source: 'web', principal: 'qq:1' })
|
|
33
|
+
check('remember 走写入链(未验证→pending)', w.status === 'pending' && w.trust_level === 'unverified', JSON.stringify(w))
|
|
34
|
+
|
|
35
|
+
// 用户来源 → trusted + active
|
|
36
|
+
const u = svc.remember({ content: '用户亲手写的一条记忆,应该直接可信。', source: 'user', principal: 'desktop:owner' })
|
|
37
|
+
check('用户来源可直接落库', u.status === 'active' && u.trust_level === 'trusted', JSON.stringify(u))
|
|
38
|
+
|
|
39
|
+
// recall:只给指针,不给正文
|
|
40
|
+
const unvActive = store.write({
|
|
41
|
+
content: '来源未验证但已放行的一条记录,用于测试指针化。',
|
|
42
|
+
sourceChannel: 'user',
|
|
43
|
+
trustLevel: 'unverified',
|
|
44
|
+
})
|
|
45
|
+
const r = svc.recall({ query: '记忆', topK: 10 })
|
|
46
|
+
check('recall 返回 pointers 数组', Array.isArray(r.pointers))
|
|
47
|
+
check('recall 不含 content 字段', r.pointers.every((p) => p.content === undefined))
|
|
48
|
+
const r3 = svc.recall({ query: '指针化', topK: 10 })
|
|
49
|
+
const unvPtr = r3.pointers.find((p) => p.record_id === unvActive.record_id)
|
|
50
|
+
check('未验证来源不给摘要', unvPtr !== undefined && unvPtr.summary === undefined, JSON.stringify(unvPtr))
|
|
51
|
+
|
|
52
|
+
// S 级不出现
|
|
53
|
+
const s = store.write({ content: '密码:abcd1234efgh', sourceChannel: 'user', trustLevel: 'trusted' })
|
|
54
|
+
const r2 = svc.recall({ query: '密码', topK: 10 })
|
|
55
|
+
check('S 级不出现在 recall', r2.pointers.every((p) => p.record_id !== s.record_id))
|
|
56
|
+
|
|
57
|
+
// group 范围不返回个人记忆
|
|
58
|
+
const g = svc.recall({ query: '记忆', scope: 'group' })
|
|
59
|
+
check('群聊范围返回空', g.pointers.length === 0)
|
|
60
|
+
|
|
61
|
+
// forget:归档,不物理删除
|
|
62
|
+
const f = svc.forget({ record_id: u.record_id })
|
|
63
|
+
const still = store.db.prepare('SELECT status FROM records WHERE record_id = ?').get(u.record_id)
|
|
64
|
+
check('forget 只归档不删除', f.status === 'archived' && still && still.status === 'archived', JSON.stringify(f))
|
|
65
|
+
|
|
66
|
+
// status:只给摘要
|
|
67
|
+
const st = svc.status()
|
|
68
|
+
check('status 有核心字段', typeof st.records === 'number' && st.audit && st.schemaVersion === '9', JSON.stringify(st))
|
|
69
|
+
check('status 不暴露 dataDir', st.dataDir === undefined)
|
|
70
|
+
|
|
71
|
+
// 降级
|
|
72
|
+
const degraded = buildService({ store: null })
|
|
73
|
+
check('无库时 recall 降级', degraded.recall({ query: 'x' }).degraded === true)
|
|
74
|
+
check('无库时 status 降级', degraded.status().degraded === true)
|
|
75
|
+
|
|
76
|
+
store.close()
|
|
77
|
+
} finally {
|
|
78
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------- 2. 注册行为(mock ctx) ----------
|
|
82
|
+
const dir2 = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-p6-ctx-'))
|
|
83
|
+
try {
|
|
84
|
+
const provided = {}
|
|
85
|
+
const disposers = []
|
|
86
|
+
const mockCtx = {
|
|
87
|
+
logger: { info() {}, warn() {} },
|
|
88
|
+
get(name) {
|
|
89
|
+
if (name === 'webServer') return { register: () => () => {} }
|
|
90
|
+
return undefined
|
|
91
|
+
},
|
|
92
|
+
provide(name, value) {
|
|
93
|
+
provided[name] = value
|
|
94
|
+
return () => {}
|
|
95
|
+
},
|
|
96
|
+
effect(fn) {
|
|
97
|
+
const d = fn()
|
|
98
|
+
disposers.push(d)
|
|
99
|
+
return () => {}
|
|
100
|
+
},
|
|
101
|
+
tools: { register: () => () => {} },
|
|
102
|
+
systemPrompt: { section: () => () => {}, context: () => () => {} },
|
|
103
|
+
}
|
|
104
|
+
applyServicePlugin(mockCtx, { dataDir: dir2 })
|
|
105
|
+
await new Promise(function (r) { setTimeout(r, 30) })
|
|
106
|
+
check('apply 注册了 memory 服务', Boolean(provided.memory), Object.keys(provided).join(','))
|
|
107
|
+
check('注册的服务 apiVersion = 1', provided.memory && provided.memory.apiVersion === 1)
|
|
108
|
+
for (const d of disposers) {
|
|
109
|
+
try {
|
|
110
|
+
if (typeof d === 'function') d()
|
|
111
|
+
} catch {
|
|
112
|
+
/* 忽略 */
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} finally {
|
|
116
|
+
fs.rmSync(dir2, { recursive: true, force: true })
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
console.log(`\nP6 ${fail === 0 ? 'OK' : 'FAILED'} pass=${pass} fail=${fail}`)
|
|
120
|
+
if (fail > 0) process.exitCode = 1
|
|
121
|
+
|
|
122
|
+
|
package/tools/p8.mjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** p8:认知层第一增量(情绪标注 + 联想)验收 */
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import MemoryStore from '../lib/memory/store.js'
|
|
6
|
+
import { analyzeAffect, initialImportance } from '../lib/memory/emotion.js'
|
|
7
|
+
|
|
8
|
+
let pass = 0
|
|
9
|
+
let fail = 0
|
|
10
|
+
function ok(name, cond, extra = '') {
|
|
11
|
+
if (cond) { pass++; console.log(' ok ' + name) } else { fail++; console.log(' FAIL ' + name + (extra ? ' :: ' + extra : '')) }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 0 纯函数
|
|
15
|
+
const neg = analyzeAffect('那个插件崩溃了,一直报错,太烦了,紧急!')
|
|
16
|
+
const pos = analyzeAffect('修好了,测试全部通过,感谢')
|
|
17
|
+
const neu = analyzeAffect('今天的会议改到下午三点')
|
|
18
|
+
ok('负面被识别', neg.label === 'urgent' || neg.label === 'negative', neg.label)
|
|
19
|
+
ok('负面效价 < 0', neg.valence < 0, String(neg.valence))
|
|
20
|
+
ok('正面效价 > 0', pos.valence > 0, String(pos.valence))
|
|
21
|
+
ok('中性不误判', neu.label === 'neutral', neu.label)
|
|
22
|
+
ok('重要性落在 0..1', initialImportance(neg, 'L2') >= 0 && initialImportance(neg, 'L2') <= 1)
|
|
23
|
+
ok('L2 重要性高于 L1', initialImportance(pos, 'L2') > initialImportance(pos, 'L1'))
|
|
24
|
+
|
|
25
|
+
// 1 迁移 + 写入 + 联想
|
|
26
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-p8-'))
|
|
27
|
+
fs.mkdirSync(path.join(dir, 'db'), { recursive: true })
|
|
28
|
+
fs.copyFileSync('D:/001/CH002/dsh-memory-data/db/memory.db', path.join(dir, 'db', 'memory.db'))
|
|
29
|
+
const store = new MemoryStore({ dataDir: dir, authority: 'shadow' }).open()
|
|
30
|
+
ok('schema 迁移到 v9', store.getMeta('schema_version') === '9', String(store.getMeta('schema_version')))
|
|
31
|
+
const cols = store.db.prepare('PRAGMA table_info(records)').all().map((c) => c.name)
|
|
32
|
+
ok('有 affect_label 列', cols.includes('affect_label'))
|
|
33
|
+
ok('有 importance 列', cols.includes('importance'))
|
|
34
|
+
|
|
35
|
+
const w = store.write({ content: '插件崩溃了,一直报错,紧急处理!', layer: 'L2', sourceChannel: 'user', trustLevel: 'trusted' })
|
|
36
|
+
const row = store.db.prepare('SELECT affect_label, affect_valence, affect_arousal, importance FROM records WHERE record_id = ?').get(w.record_id)
|
|
37
|
+
ok('写入带情绪标签', row.affect_label === 'urgent' || row.affect_label === 'negative', row.affect_label)
|
|
38
|
+
ok('写入带效价', typeof row.affect_valence === 'number' && row.affect_valence < 0, String(row.affect_valence))
|
|
39
|
+
ok('写入带重要性', typeof row.importance === 'number' && row.importance > 0.5, String(row.importance))
|
|
40
|
+
|
|
41
|
+
const w2 = store.write({ content: '记忆底座修好了,测试全部通过,感谢', layer: 'L2', sourceChannel: 'user', trustLevel: 'trusted' })
|
|
42
|
+
const a = store.associate({ recordId: w2.record_id, topK: 5 })
|
|
43
|
+
ok('联想返回结构', a && Array.isArray(a.items))
|
|
44
|
+
ok('联想有理由', a.items.length === 0 || a.items.every((i) => Array.isArray(i.reasons) && i.reasons.length > 0))
|
|
45
|
+
const aq = store.associate({ query: '崩溃 报错', topK: 5 })
|
|
46
|
+
ok('纯文本也能联想', Array.isArray(aq.items))
|
|
47
|
+
|
|
48
|
+
store.close()
|
|
49
|
+
console.log(`\nP8 ${fail === 0 ? 'OK' : 'FAIL'} pass=${pass} fail=${fail}`)
|
|
50
|
+
process.exit(fail === 0 ? 0 : 1)
|
package/tools/p9.mjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** p9:B2 注入情绪加权 + B3 关联提示 */
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import MemoryStore from '../lib/memory/store.js'
|
|
6
|
+
import { selectSnapshot, Injector } from '../lib/memory/inject.js'
|
|
7
|
+
|
|
8
|
+
let pass = 0, fail = 0
|
|
9
|
+
function ok(name, cond, extra = '') {
|
|
10
|
+
if (cond) { pass++; console.log(' ok ' + name) } else { fail++; console.log(' FAIL ' + name + (extra ? ' :: ' + extra : '')) }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-p9-'))
|
|
14
|
+
const store = new MemoryStore({ dataDir: dir }).open()
|
|
15
|
+
|
|
16
|
+
const neutral = store.write({ content: '今天的会议改到下午三点', layer: 'L2', sourceChannel: 'user', trustLevel: 'trusted', tags: ['meeting'] })
|
|
17
|
+
const negative = store.write({ content: '插件崩溃了,一直报错,紧急!', layer: 'L2', sourceChannel: 'user', trustLevel: 'trusted', tags: ['crash'] })
|
|
18
|
+
store.write({ content: '崩溃的线索:错误码 500 出现在网关', layer: 'L2', sourceChannel: 'user', trustLevel: 'trusted', tags: ['crash'] })
|
|
19
|
+
const secret = store.write({ content: '崩溃现场的密钥 sk-test-123', layer: 'L2', sadLevel: 'S', sourceChannel: 'user', trustLevel: 'trusted', tags: ['crash'] })
|
|
20
|
+
|
|
21
|
+
const rows = selectSnapshot(store, { maxRecords: 10 })
|
|
22
|
+
const idxNeg = rows.findIndex((r) => r.record_id === negative.record_id)
|
|
23
|
+
const idxNeu = rows.findIndex((r) => r.record_id === neutral.record_id)
|
|
24
|
+
ok('负面高唤醒排在前面', idxNeg >= 0 && idxNeu >= 0 && idxNeg < idxNeu, `neg=${idxNeg} neu=${idxNeu}`)
|
|
25
|
+
ok('S 级不进注入池', !rows.some((r) => r.record_id === secret.record_id))
|
|
26
|
+
|
|
27
|
+
const injector = new Injector({ store, maxRecords: 3, maxChars: 2000 })
|
|
28
|
+
const text = injector.text()
|
|
29
|
+
ok('注入文本非空', text.length > 0)
|
|
30
|
+
ok('注入带关联行', text.includes('> 关联:'), text.slice(0, 300))
|
|
31
|
+
ok('关联不带 S 正文', !text.includes('sk-test-123'))
|
|
32
|
+
|
|
33
|
+
store.close()
|
|
34
|
+
console.log(`\nP9 ${fail === 0 ? 'OK' : 'FAIL'} pass=${pass} fail=${fail}`)
|
|
35
|
+
process.exit(fail === 0 ? 0 : 1)
|
package/tools/phase2.mjs
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* phase2.mjs — 二期回归(记录仪 + 导入 + 影子读 + 审计)。
|
|
3
|
+
* node tools/phase2.mjs
|
|
4
|
+
* 用临时库与临时"旧记忆目录",不动真实数据。
|
|
5
|
+
*/
|
|
6
|
+
import fs from 'node:fs'
|
|
7
|
+
import os from 'node:os'
|
|
8
|
+
import path from 'node:path'
|
|
9
|
+
import MemoryStore from '../lib/memory/store.js'
|
|
10
|
+
import { Recorder } from '../lib/memory/recorder.js'
|
|
11
|
+
|
|
12
|
+
let pass = 0
|
|
13
|
+
let fail = 0
|
|
14
|
+
const out = []
|
|
15
|
+
const check = (label, ok, detail = '') => {
|
|
16
|
+
if (ok) {
|
|
17
|
+
pass += 1
|
|
18
|
+
out.push(` PASS ${label}`)
|
|
19
|
+
} else {
|
|
20
|
+
fail += 1
|
|
21
|
+
out.push(` FAIL ${label}${detail ? ` ← ${detail}` : ''}`)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ---------- 造一个假的"旧记忆"目录 ----------
|
|
26
|
+
const legacyHome = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-legacy-'))
|
|
27
|
+
fs.mkdirSync(path.join(legacyHome, 'runtime'), { recursive: true })
|
|
28
|
+
fs.mkdirSync(path.join(legacyHome, 'documents', 'active'), { recursive: true })
|
|
29
|
+
|
|
30
|
+
fs.writeFileSync(
|
|
31
|
+
path.join(legacyHome, 'runtime', 'memories.json'),
|
|
32
|
+
JSON.stringify(
|
|
33
|
+
{
|
|
34
|
+
version: 1,
|
|
35
|
+
entries: [
|
|
36
|
+
{ content: '板子风扇阈值 60℃ 升档、55℃ 降档,实测空载 55-57℃。', target: 'memory', importance: 'normal', created_at: '2026-10-01T00:00:00Z' },
|
|
37
|
+
{ content: '开发板 SSH 密码:Abc123456,串口 COM8 115200。', target: 'memory', importance: 'critical', created_at: '2026-10-02T00:00:00Z' },
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
null,
|
|
41
|
+
2,
|
|
42
|
+
),
|
|
43
|
+
'utf8',
|
|
44
|
+
)
|
|
45
|
+
fs.writeFileSync(
|
|
46
|
+
path.join(legacyHome, 'documents', 'active', 'ip-wifi-ssh-vnc.md'),
|
|
47
|
+
[
|
|
48
|
+
'# 设备凭证',
|
|
49
|
+
'',
|
|
50
|
+
'| SSID | 密码 | 用途 |',
|
|
51
|
+
'|---|---|---|',
|
|
52
|
+
'| CALP | `ch1949101` | 当前 |',
|
|
53
|
+
'',
|
|
54
|
+
'SSH root/Mind@123,VNC 5900/aipro2026',
|
|
55
|
+
'',
|
|
56
|
+
].join('\n'),
|
|
57
|
+
'utf8',
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-p2-'))
|
|
61
|
+
const store = new MemoryStore({ dataDir, authority: 'shadow' }).open()
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
// ---------- 1. 记录仪基础 ----------
|
|
65
|
+
const w = store.write({ content: '二期联调:记录仪写入一条测试记录。', layer: 'L1', sourceChannel: 'user' })
|
|
66
|
+
check('写入成功并返回 record_id', Boolean(w.record_id) && w.status === 'active', JSON.stringify(w))
|
|
67
|
+
|
|
68
|
+
const inv = store.checkInvariants()
|
|
69
|
+
check('二期后不变量仍一致', inv.ok === true, JSON.stringify(inv))
|
|
70
|
+
|
|
71
|
+
const audit1 = store.audit({ deep: true })
|
|
72
|
+
check('记录仪:写入后哈希链一致', audit1.ok === true, JSON.stringify(audit1.broken_at))
|
|
73
|
+
check('记录仪:产生了 trace 行', audit1.lines >= 1 && audit1.files >= 1, JSON.stringify({ lines: audit1.lines, files: audit1.files }))
|
|
74
|
+
|
|
75
|
+
// ---------- 2. 禁记:查询里的口令不得落盘 ----------
|
|
76
|
+
store.search({ query: '密码是 TopSecret999 的记录', topK: 3 })
|
|
77
|
+
const auditFile = fs
|
|
78
|
+
.readdirSync(path.join(dataDir, 'audit'))
|
|
79
|
+
.filter((f) => f.endsWith('.jsonl'))
|
|
80
|
+
.map((f) => path.join(dataDir, 'audit', f))[0]
|
|
81
|
+
const auditText = fs.readFileSync(auditFile, 'utf8')
|
|
82
|
+
check('禁记:查询明文不落盘', !auditText.includes('TopSecret999'), '发现明文口令')
|
|
83
|
+
check('禁记:留了脱敏占位', auditText.includes('«REDACTED:'), '没有脱敏标记')
|
|
84
|
+
check('禁记:存了查询哈希', /"query_hash":"[0-9a-f]{64}"/.test(auditText), '缺 query_hash')
|
|
85
|
+
|
|
86
|
+
// ---------- 3. 断链能被查出来 ----------
|
|
87
|
+
const lines = auditText.trimEnd().split('\n')
|
|
88
|
+
const first = JSON.parse(lines[0])
|
|
89
|
+
first.payload.op = 'tampered'
|
|
90
|
+
lines[0] = JSON.stringify(first)
|
|
91
|
+
fs.writeFileSync(auditFile, `${lines.join('\n')}\n`, 'utf8')
|
|
92
|
+
const audit2 = store.audit({ deep: true })
|
|
93
|
+
check('篡改一行 → deep 校验报断链', audit2.ok === false && Boolean(audit2.brokenAt), JSON.stringify(audit2.broken_at))
|
|
94
|
+
|
|
95
|
+
// 恢复现场(后续用例需要正常链)
|
|
96
|
+
fs.unlinkSync(auditFile)
|
|
97
|
+
const stateFile = path.join(dataDir, 'audit', '.state.json')
|
|
98
|
+
fs.writeFileSync(stateFile, JSON.stringify({ seq: 0, hash: 'GENESIS' }), 'utf8')
|
|
99
|
+
store.recorder.state = { seq: 0, hash: 'GENESIS' }
|
|
100
|
+
|
|
101
|
+
// ---------- 4. 导入:只读 + 脱敏 + 幂等 ----------
|
|
102
|
+
const imp1 = store.importLegacy({ home: legacyHome })
|
|
103
|
+
check('导入:扫到 2 条记忆 + 1 份文档', imp1.scanned.memory === 2 && imp1.scanned.documents === 1, JSON.stringify(imp1.scanned))
|
|
104
|
+
check('导入:3 条全部入库', imp1.imported === 3 && imp1.errors.length === 0, JSON.stringify({ imported: imp1.imported, errors: imp1.errors }))
|
|
105
|
+
check('导入:2 条被判 S 级', imp1.redacted === 2, JSON.stringify(imp1.redacted_kinds))
|
|
106
|
+
|
|
107
|
+
const dbText = JSON.stringify(store.db.prepare('SELECT content FROM records').all())
|
|
108
|
+
check('导入:库里没有明文口令', !dbText.includes('Abc123456') && !dbText.includes('Mind@123') && !dbText.includes('ch1949101'), '发现明文')
|
|
109
|
+
check('导入:留了脱敏占位', dbText.includes('«REDACTED:'))
|
|
110
|
+
|
|
111
|
+
check('导入:凭证文档只导指针(不导正文)', (imp1.pointer_only ?? 0) >= 1, JSON.stringify({ pointer_only: imp1.pointer_only }))
|
|
112
|
+
const docRow = store.db.prepare("SELECT content FROM records WHERE legacy_ref LIKE 'documents/%'").get()
|
|
113
|
+
check('导入:指针记录里没有正文', Boolean(docRow) && docRow.content.length < 400 && docRow.content.includes('【指针】'), JSON.stringify(docRow)?.slice(0, 120))
|
|
114
|
+
|
|
115
|
+
const imp2 = store.importLegacy({ home: legacyHome })
|
|
116
|
+
check('导入:二次执行幂等(imported=0)', imp2.imported === 0 && imp2.skipped === 3, JSON.stringify({ imported: imp2.imported, skipped: imp2.skipped }))
|
|
117
|
+
|
|
118
|
+
const dry = store.importLegacy({ home: legacyHome, dryRun: true })
|
|
119
|
+
check('导入:dry_run 不写库', dry.dry_run === true && dry.imported === 0 && dry.skipped === 3, JSON.stringify(dry))
|
|
120
|
+
|
|
121
|
+
// ---------- 5. 影子读 ----------
|
|
122
|
+
const shadow = store.shadow({ query: '风扇阈值 温度', topK: 5, home: legacyHome })
|
|
123
|
+
check('影子读:两路都有结果', shadow.legacy.length >= 1 && shadow.mine.length >= 1, JSON.stringify({ legacy: shadow.legacy.length, mine: shadow.mine.length }))
|
|
124
|
+
check('影子读:旧库命中能在新库找到(一致)', shadow.consistent === true, JSON.stringify(shadow.diff))
|
|
125
|
+
|
|
126
|
+
// ---------- 6. 回放 ----------
|
|
127
|
+
const recent = store.audit({}).recent[0]
|
|
128
|
+
check('回放:能拿到最近一条 trace', Boolean(recent?.trace_id), JSON.stringify(recent))
|
|
129
|
+
const trace = store.traceLookup(recent.trace_id)
|
|
130
|
+
check('回放:按 trace_id 取回详情', Boolean(trace) && trace.trace_id === recent.trace_id && Array.isArray(trace.hits), JSON.stringify(trace)?.slice(0, 160))
|
|
131
|
+
|
|
132
|
+
// ---------- 7. 滚动 ----------
|
|
133
|
+
const oldFile = path.join(dataDir, 'audit', 'audit-202001.jsonl')
|
|
134
|
+
fs.writeFileSync(oldFile, '{"payload":{"seq":0},"prev":"GENESIS","hash":"x"}\n', 'utf8')
|
|
135
|
+
const longAgo = new Date(Date.now() - 200 * 86400_000)
|
|
136
|
+
fs.utimesSync(oldFile, longAgo, longAgo)
|
|
137
|
+
const pruned = store.recorder.prune()
|
|
138
|
+
check('滚动:超过 90 天的文件被清掉', !fs.existsSync(oldFile), JSON.stringify(pruned.removed))
|
|
139
|
+
} finally {
|
|
140
|
+
store.close()
|
|
141
|
+
fs.rmSync(dataDir, { recursive: true, force: true })
|
|
142
|
+
fs.rmSync(legacyHome, { recursive: true, force: true })
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ---------- 8. Recorder 单独:空目录也能开 ----------
|
|
146
|
+
const emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'memcore-rec-'))
|
|
147
|
+
try {
|
|
148
|
+
const rec = new Recorder({ dir: emptyDir }).open()
|
|
149
|
+
const r1 = rec.record({ op: 'test', note: 'hello' })
|
|
150
|
+
const v = rec.verify({ deep: true })
|
|
151
|
+
check('Recorder:空目录可开、可写、可验', v.ok === true && v.lines === 1 && r1.seq === 1, JSON.stringify(v))
|
|
152
|
+
rec.close?.()
|
|
153
|
+
} finally {
|
|
154
|
+
fs.rmSync(emptyDir, { recursive: true, force: true })
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
console.log(out.join('\n'))
|
|
158
|
+
console.log(`\n${fail === 0 ? 'PHASE2 OK' : 'PHASE2 FAILED'} pass=${pass} fail=${fail}`)
|
|
159
|
+
process.exit(fail === 0 ? 0 : 1)
|