dsh-knit 0.24.0 → 0.25.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.
@@ -0,0 +1,285 @@
1
+ /**
2
+ * v0.25:**对话里「工具产出」的图**(`src/host/attachments.js` + 两条宿主路由)。
3
+ *
4
+ * 这份测试的重点不是「能不能显示」,而是**那条边界**:本插件唯一一处读工作区之外的地方。
5
+ * 所以除纯函数外,还有一组**真实回环端口**的 e2e,逐条钉住:
6
+ * · 只认 `sha256:<64 hex>`(给不出路径 ⇒ 穿越无从谈起)
7
+ * · 只读**本会话 `tool/result`** 里的 id(跨会话、用户自己贴的图,一律 404)
8
+ * · 工作区里已有同内容文件的 ⇒ 列表里不再出现(按 sha256,不是按文件名)
9
+ *
10
+ * ⚠️ e2e 要把 `DSH_HOME` 指到临时目录 —— 那条路由按 `attachmentsRoot()` 算路径,
11
+ * 所以这里造的是**真的附件库形状**(`attachments/v1/objects/<前两位>/<sha256>`),不是假路径。
12
+ */
13
+ import test from 'node:test'
14
+ import assert from 'node:assert/strict'
15
+ import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises'
16
+ import { createHash } from 'node:crypto'
17
+ import { tmpdir } from 'node:os'
18
+ import { join } from 'node:path'
19
+
20
+ import {
21
+ isAttachmentId, attachmentHex, attachmentsRoot, objectPath, attachmentsFromEvent,
22
+ createAttachmentStore, dropSaved,
23
+ } from '../src/host/attachments.js'
24
+ import { startKnitServer } from './http-harness.mjs'
25
+ import { resetAttachments, resetScanMemo } from '../src/host/index.js'
26
+
27
+ const sha256 = (buf) => createHash('sha256').update(buf).digest('hex')
28
+
29
+ /** 造一条 `tool/result` 事件(工具产出的图)。 */
30
+ function toolEvent(meta, time = 1000) {
31
+ return {
32
+ type: 'tool/result',
33
+ time,
34
+ data: { message: { role: 'tool', content: [{ type: 'image', attachment: meta }] } },
35
+ }
36
+ }
37
+
38
+ const META = {
39
+ attachmentId: 'sha256:' + 'a'.repeat(64),
40
+ mediaType: 'image/png',
41
+ width: 64,
42
+ height: 64,
43
+ bytes: 128,
44
+ name: 'D1_测试.png',
45
+ }
46
+
47
+ /* ── 纯函数 ───────────────────────────────────────────── */
48
+
49
+ test('附件(纯):只认 sha256 一种形状 —— 给不出路径,就没有穿越', () => {
50
+ assert.equal(isAttachmentId('sha256:' + 'a'.repeat(64)), true)
51
+ assert.equal(attachmentHex('sha256:' + 'a'.repeat(64)), 'a'.repeat(64))
52
+ for (const bad of [
53
+ '', null, undefined, 'a'.repeat(64), 'sha256:', 'sha256:' + 'A'.repeat(64), // 大写不认
54
+ 'sha256:' + 'a'.repeat(63), 'sha256:' + 'a'.repeat(65),
55
+ 'sha256:../../etc/passwd', '/etc/passwd', '../a', 'sha512:' + 'a'.repeat(64),
56
+ ]) {
57
+ assert.equal(isAttachmentId(bad), false, `该拒:${bad}`)
58
+ assert.equal(attachmentHex(bad), '', `该拒:${bad}`)
59
+ assert.equal(objectPath('/tmp/objects', bad), '', `不合法 id 不许算出路径:${bad}`)
60
+ }
61
+ // 合法 id:路径**由哈希算出来**(前两位做目录),与 DSH 的布局一致
62
+ const abs = objectPath('/tmp/objects', 'sha256:' + 'abcdef'.padEnd(64, '0'))
63
+ assert.equal(abs, join('/tmp/objects', 'ab', 'abcdef'.padEnd(64, '0')))
64
+ })
65
+
66
+ test('附件(纯):附件库根认 DSH_HOME,否则退回 ~/.dsh', () => {
67
+ assert.equal(attachmentsRoot({ DSH_HOME: '/x/dsh' }), '/x/dsh/attachments/v1/objects')
68
+ assert.match(attachmentsRoot({}), /\.dsh\/attachments\/v1\/objects$/)
69
+ })
70
+
71
+ test('附件(纯):只收 tool/result 里的图 —— 用户自己贴的图**一条都不收**', () => {
72
+ const got = attachmentsFromEvent(toolEvent(META))
73
+ assert.equal(got.length, 1)
74
+ assert.equal(got[0].id, META.attachmentId)
75
+ assert.equal(got[0].kind, 'image')
76
+ assert.equal(got[0].at, 1000, '时间戳要带出来(客户端按它排序)')
77
+
78
+ // ⚠️ 这是隐私那一条:用户贴的图**默认不进媒体档**。
79
+ // 两层都要钉:① 真实信封(`data.content` / `data.inserted`);② **同形状**的假想信封 ——
80
+ // 后者才是「类型过滤」本身,少了它这条守卫会空转(信封不同替过滤挡了枪)。
81
+ for (const type of ['user/message', 'agent/inbox/spliced', 'assistant/message']) {
82
+ const ev = { type, time: 1, data: { content: [{ type: 'image', attachment: META }] } }
83
+ assert.deepEqual(attachmentsFromEvent(ev), [], `${type} 不该被收`)
84
+ const sameShape = { type, time: 1, data: { message: { role: 'user', content: [{ type: 'image', attachment: META }] } } }
85
+ assert.deepEqual(attachmentsFromEvent(sameShape), [], `${type}(与 tool/result 同形状)也不许收`)
86
+ }
87
+ // 工具事件里但形状不对的,也不收
88
+ assert.deepEqual(attachmentsFromEvent({ type: 'tool/result', data: {} }), [])
89
+ assert.deepEqual(attachmentsFromEvent({ type: 'tool/result', data: { message: { content: [{ type: 'text', text: 'x' }] } } }), [])
90
+ assert.deepEqual(attachmentsFromEvent(null), [])
91
+ })
92
+
93
+ test('附件(纯):类型 / 大小不合规的条目被拒(太大既不列也不读)', () => {
94
+ const base = { ...META, attachmentId: 'sha256:' + 'b'.repeat(64) }
95
+ const cases = [
96
+ [{ ...base, mediaType: 'text/plain' }, '不是图片/视频'],
97
+ [{ ...base, mediaType: 'application/pdf' }, '不是图片/视频'],
98
+ [{ ...base, mediaType: '' }, '没有类型'],
99
+ [{ ...base, bytes: 0 }, '字节数为 0'],
100
+ [{ ...base, bytes: -5 }, '负数'],
101
+ [{ ...base, bytes: 12 * 1024 * 1024 + 1 }, '超过 12 MB 上限'],
102
+ [{ ...base, attachmentId: 'nope' }, 'id 不合法'],
103
+ ]
104
+ for (const [meta, why] of cases) assert.deepEqual(attachmentsFromEvent(toolEvent(meta)), [], why)
105
+ // 视频要认成 video(媒体档要按视频渲染)
106
+ const video = attachmentsFromEvent(toolEvent({ ...base, mediaType: 'video/mp4' }))[0]
107
+ assert.equal(video.kind, 'video')
108
+ // 没有名字就给一个由哈希派生的名字(列表里不能出现空白名)
109
+ const unnamed = attachmentsFromEvent(toolEvent({ ...base, name: '' }))[0]
110
+ assert.match(unnamed.name, /^attachment-[0-9a-f]{8}$/)
111
+ })
112
+
113
+ test('附件(纯):登记簿按会话隔离,且**跨会话一律不认**(读字节的唯一闸门)', () => {
114
+ const store = createAttachmentStore()
115
+ store.note('s1', toolEvent(META))
116
+ assert.equal(store.has('s1', META.attachmentId), true)
117
+ assert.equal(store.has('s2', META.attachmentId), false, '另一个会话不许认这条 id')
118
+ assert.equal(store.list('s2').length, 0)
119
+ assert.equal(store.get('s2', META.attachmentId), undefined)
120
+ // 同一条重复记:不重复、元数据覆盖
121
+ store.note('s1', toolEvent({ ...META, name: '改名.png' }))
122
+ assert.equal(store.list('s1').length, 1)
123
+ assert.equal(store.list('s1')[0].name, '改名.png')
124
+ // 不是带图的事件:什么都不做
125
+ assert.equal(store.note('s1', { type: 'tool/result', data: {} }), 0)
126
+ assert.equal(store.note('', toolEvent(META)), 0, '没有会话 id 不记')
127
+ })
128
+
129
+ test('附件(纯):登记簿有界 —— 每会话丢最老的,会话数按 LRU 丢', () => {
130
+ const store = createAttachmentStore({ maxPerSession: 2, maxSessions: 2 })
131
+ const id = (n) => 'sha256:' + String(n).repeat(64).slice(0, 64)
132
+ store.note('s1', toolEvent({ ...META, attachmentId: id(1), name: '1' }))
133
+ store.note('s1', toolEvent({ ...META, attachmentId: id(2), name: '2' }))
134
+ store.note('s1', toolEvent({ ...META, attachmentId: id(3), name: '3' }))
135
+ assert.deepEqual(store.list('s1').map((x) => x.name), ['2', '3'], '超上限丢最老的')
136
+ store.note('s2', toolEvent({ ...META, attachmentId: id(4), name: '4' }))
137
+ store.note('s3', toolEvent({ ...META, attachmentId: id(5), name: '5' }))
138
+ assert.equal(store.size, 2, '会话数也封顶')
139
+ assert.equal(store.has('s1', id(2)), false, '最久没动的会话被丢(LRU)')
140
+ assert.equal(store.has('s3', id(5)), true)
141
+ })
142
+
143
+ test('附件(纯):去重按 **sha256**,且只给「大小相同」的候选算哈希(不扫全库)', async () => {
144
+ const items = [
145
+ { id: 'sha256:' + 'a'.repeat(64), bytes: 100 },
146
+ { id: 'sha256:' + 'b'.repeat(64), bytes: 200 },
147
+ ]
148
+ const hashed = []
149
+ const sha256Of = async (rel) => { hashed.push(rel); return rel === 'shot-a.png' ? 'a'.repeat(64) : 'f'.repeat(64) }
150
+ const candidates = [
151
+ { rel: 'shot-a.png', bytes: 100 }, // 大小与 a 相同 ⇒ 算哈希 ⇒ 命中 ⇒ 去掉
152
+ { rel: 'other.png', bytes: 200 }, // 大小与 b 相同 ⇒ 算哈希 ⇒ 不命中 ⇒ 留着
153
+ { rel: 'big.png', bytes: 999 }, // 大小都不像 ⇒ **根本不算哈希**
154
+ ]
155
+ const kept = await dropSaved(items, { candidates, sha256Of })
156
+ assert.deepEqual(kept.map((x) => x.bytes), [200], '已经在工作区里的那条被去掉')
157
+ assert.deepEqual(hashed, ['shot-a.png', 'other.png'], '不同大小的候选一次哈希都不该算')
158
+ // 没有候选 / 没有哈希器:原样返回(不假装去重过)
159
+ assert.equal((await dropSaved(items, { candidates: [] })).length, 2)
160
+ assert.equal((await dropSaved(items, {})).length, 2)
161
+ assert.deepEqual(await dropSaved([], { candidates }), [])
162
+ // 哈希器抛错:那条候选当作没命中(宁可重复出现,也不许误删)
163
+ const bad = await dropSaved(items, { candidates, sha256Of: () => { throw new Error('boom') } })
164
+ assert.equal(bad.length, 2)
165
+ })
166
+
167
+ /* ── 真实回环端口的 e2e:把那条边界逐条钉住 ───────────────────── */
168
+
169
+ /** 造「附件库 + 工作区」两个临时目录,并把 DSH_HOME 指过去。 */
170
+ async function makeWorld(options = {}) {
171
+ const home = await mkdtemp(join(tmpdir(), 'knit-attach-home-'))
172
+ const root = await mkdtemp(join(tmpdir(), 'knit-attach-root-'))
173
+ const bytes = Buffer.from('fake-png-bytes-' + 'x'.repeat(20))
174
+ const hex = sha256(bytes)
175
+ const id = 'sha256:' + hex
176
+ // 真的附件库形状:attachments/v1/objects/<前两位>/<sha256>
177
+ await mkdir(join(home, 'attachments', 'v1', 'objects', hex.slice(0, 2)), { recursive: true })
178
+ await writeFile(join(home, 'attachments', 'v1', 'objects', hex.slice(0, 2), hex), bytes)
179
+ if (options.saveToWorkspace) await writeFile(join(root, 'shot.png'), bytes)
180
+ if (options.otherWorkspaceFile) await writeFile(join(root, 'other.png'), Buffer.from('different-bytes'))
181
+ const meta = { attachmentId: id, mediaType: 'image/png', width: 8, height: 8, bytes: bytes.length, name: '生成图.png' }
182
+ return { home, root, bytes, hex, id, meta }
183
+ }
184
+
185
+ async function withServer(world, events, fn) {
186
+ // ⚠️ 附件登记簿是**模块级**的(生产里就是进程级的),用例之间必须清 ——
187
+ // 不清的话上一个用例的 id 还挂着,下一个用例的「用户贴的图不许读」会假红。
188
+ resetAttachments()
189
+ const prev = process.env.DSH_HOME
190
+ process.env.DSH_HOME = world.home
191
+ const session = { header: { cwd: world.root, id: 's1' }, snapshotEvents: () => events }
192
+ const server = await startKnitServer({ root: world.root, session, sessionId: 's1' })
193
+ try {
194
+ await fn(server)
195
+ } finally {
196
+ await server.close()
197
+ if (prev === undefined) delete process.env.DSH_HOME
198
+ else process.env.DSH_HOME = prev
199
+ await rm(world.home, { recursive: true, force: true })
200
+ await rm(world.root, { recursive: true, force: true })
201
+ }
202
+ }
203
+
204
+ test('附件(e2e):/api/attachments 列出本会话工具产出的图;存进工作区之后就不出现了', async () => {
205
+ const world = await makeWorld()
206
+ const events = [toolEvent(world.meta)]
207
+ await withServer(world, events, async (server) => {
208
+ let res = await fetch(`${server.base}/api/attachments?sessionId=s1`)
209
+ let data = await res.json()
210
+ assert.equal(res.status, 200)
211
+ assert.deepEqual(data.items.map((x) => x.id), [world.id], '还没保存 ⇒ 出现在列表里')
212
+
213
+ // 把同样的字节存进工作区(模拟「拿到本地」)
214
+ await writeFile(join(world.root, 'shot.png'), world.bytes)
215
+ // 扫描结果有 8 秒备忘(与面板轮询同窗)⇒ 要看「下一次扫描」,必须把它清掉
216
+ resetScanMemo()
217
+ res = await fetch(`${server.base}/api/attachments?sessionId=s1`)
218
+ data = await res.json()
219
+ assert.deepEqual(data.items, [], '工作区里已有同内容文件 ⇒ 列表里不再出现(按 sha256 去重)')
220
+ })
221
+ })
222
+
223
+ test('附件(e2e):不同内容的同名文件**不会**把附件误删(去重看内容不看名字)', async () => {
224
+ const world = await makeWorld({ otherWorkspaceFile: true })
225
+ await withServer(world, [toolEvent(world.meta)], async (server) => {
226
+ const res = await fetch(`${server.base}/api/attachments?sessionId=s1`)
227
+ const data = await res.json()
228
+ assert.equal(data.items.length, 1, '工作区里那几张图内容不同 ⇒ 附件仍然显示')
229
+ })
230
+ })
231
+
232
+ test('附件(e2e):/api/attachment 按 sha256 回字节,类型取事件里的 mediaType', async () => {
233
+ const world = await makeWorld()
234
+ await withServer(world, [toolEvent(world.meta)], async (server) => {
235
+ const res = await fetch(`${server.base}/api/attachment?sessionId=s1&id=${encodeURIComponent(world.id)}`)
236
+ assert.equal(res.status, 200)
237
+ assert.equal(res.headers.get('content-type'), 'image/png', '附件对象没有扩展名 ⇒ 类型只能取事件里的')
238
+ assert.equal(res.headers.get('x-content-type-options'), 'nosniff')
239
+ assert.match(String(res.headers.get('content-security-policy')), /sandbox/, '与 /api/raw 同一套防护')
240
+ const got = Buffer.from(await res.arrayBuffer())
241
+ assert.deepEqual(got, world.bytes, '回的必须是那份字节')
242
+ })
243
+ })
244
+
245
+ test('附件(e2e):只认本会话的 id —— 跨会话 / 没出现过 / 用户贴的图 / 畸形 id 一律 404', async () => {
246
+ const world = await makeWorld()
247
+ // 同一个附件:一条来自 tool/result(会被收),一条来自 user/message(不会被收)
248
+ const pastedOnly = {
249
+ // 刻意用**与 tool/result 相同**的信封:这样 404 只能归因于「类型不是 tool/result」,
250
+ // 而不是「信封字段凑巧不一样」—— 后者会让这条 e2e 变成空转的绿灯。
251
+ type: 'user/message', time: 5,
252
+ data: { message: { role: 'user', content: [{ type: 'image', attachment: world.meta }] } },
253
+ }
254
+ await withServer(world, [toolEvent(world.meta)], async (server) => {
255
+ const url = (q) => `${server.base}/api/attachment?${q}`
256
+ assert.equal((await fetch(url(`sessionId=s1&id=${encodeURIComponent(world.id)}`))).status, 200, '本会话:可以读')
257
+
258
+ const otherId = 'sha256:' + 'c'.repeat(64)
259
+ assert.equal((await fetch(url(`sessionId=s2&id=${encodeURIComponent(world.id)}`))).status, 404, '别的会话:404')
260
+ assert.equal((await fetch(url(`sessionId=s1&id=${encodeURIComponent(otherId)}`))).status, 404, '没出现过:404')
261
+ assert.equal((await fetch(url(`sessionId=&id=${encodeURIComponent(world.id)}`))).status, 404, '没有会话:404')
262
+ for (const bad of ['../../etc/passwd', '/etc/passwd', 'sha256:../../x', 'sha256:' + 'A'.repeat(64)]) {
263
+ const res = await fetch(url(`sessionId=s1&id=${encodeURIComponent(bad)}`))
264
+ assert.equal(res.status, 404, `畸形 id 必须 404:${bad}`)
265
+ }
266
+ })
267
+
268
+ // 用户贴的图:即便本会话真的有这条事件,也**不许**读(只收 tool/result)
269
+ await withServer(world, [pastedOnly], async (server) => {
270
+ const res = await fetch(`${server.base}/api/attachment?sessionId=s1&id=${encodeURIComponent(world.id)}`)
271
+ assert.equal(res.status, 404, '用户自己贴的图默认不进媒体档,也不给读')
272
+ const list = await (await fetch(`${server.base}/api/attachments?sessionId=s1`)).json()
273
+ assert.deepEqual(list.items, [], '列表里也不该有')
274
+ })
275
+ })
276
+
277
+ test('附件(e2e):对象文件不存在时 404(不抛、不挂)', async () => {
278
+ const world = await makeWorld()
279
+ // 事件里登记了 id,但把对象文件删掉
280
+ await rm(join(world.home, 'attachments', 'v1', 'objects', world.hex.slice(0, 2), world.hex), { force: true })
281
+ await withServer(world, [toolEvent(world.meta)], async (server) => {
282
+ const res = await fetch(`${server.base}/api/attachment?sessionId=s1&id=${encodeURIComponent(world.id)}`)
283
+ assert.equal(res.status, 404)
284
+ })
285
+ })