@240xu/dsh-message-ops 0.8.0 → 0.9.2
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 +162 -7
- package/package.json +2 -2
- package/src/client.js +312 -132
- package/src/index.js +114 -103
- package/src/ops-core.js +213 -65
- package/src/session-file.js +29 -1
- package/test/branch-export-removed.test.js +73 -0
- package/test/fence.test.js +0 -27
- package/test/injected-regex.test.js +62 -0
- package/test/legacy-format.test.js +0 -17
- package/test/ops-core.test.js +42 -45
- package/test/revert-contract.test.js +118 -0
- package/test/session-file.test.js +0 -24
- package/test/step-restore.test.js +16 -0
- package/test/surface-visibility.test.js +166 -0
- package/test/user-revert-anchor.test.js +103 -0
- package/src/branch.js +0 -57
|
@@ -13,7 +13,6 @@ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-msgops-legacy-
|
|
|
13
13
|
|
|
14
14
|
const { readSessionFile, readSessionFileAsync, encodeSessionFile, findSessionDirs, listMessages } =
|
|
15
15
|
await import('../src/session-file.js')
|
|
16
|
-
const { applyBranch } = await import('../src/branch.js')
|
|
17
16
|
|
|
18
17
|
const HEADER = { type: 'session', version: 3, id: 'session-22222222-2222-2222-2222-222222222222', createdAt: 1, cwd: '/tmp/x' }
|
|
19
18
|
const EVENTS = [
|
|
@@ -119,19 +118,3 @@ test('findSessionDirs:v4 文件名优先发现;同目录 v4 压过 v3/legacy
|
|
|
119
118
|
assert.match(dirs[0].logPath, /session\.v4\.jsonl\.zstd$/)
|
|
120
119
|
})
|
|
121
120
|
|
|
122
|
-
test('applyBranch:v4 会话分支写回 v4 文件名与 version=4', () => {
|
|
123
|
-
const id = 'session-66666666-6666-6666-6666-666666666666'
|
|
124
|
-
const dir = path.join(process.env.DSH_HOME, 'sessions', 'v4-b', id)
|
|
125
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
126
|
-
const origLog = path.join(dir, 'session.v4.jsonl.zstd')
|
|
127
|
-
fs.writeFileSync(origLog, encodeSessionFile({ ...V4_HEADER, id }, [
|
|
128
|
-
{ type: 'user/message', seq: 0, surfaceOp: 'append', data: { message: { role: 'user', content: [{ type: 'text', text: 'a' }] } } },
|
|
129
|
-
{ type: 'user/message', seq: 1, surfaceOp: 'append', data: { message: { role: 'user', content: [{ type: 'text', text: 'b' }] } } },
|
|
130
|
-
]))
|
|
131
|
-
const result = applyBranch(origLog, 0)
|
|
132
|
-
assert.match(result.logPath, /session\.v4\.jsonl\.zstd$/)
|
|
133
|
-
const { header, events } = readSessionFile(result.logPath)
|
|
134
|
-
assert.equal(header.version, 4)
|
|
135
|
-
assert.equal(header.parentSession, id)
|
|
136
|
-
assert.equal(events.length, 1)
|
|
137
|
-
})
|
package/test/ops-core.test.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import test from 'node:test'
|
|
7
7
|
import assert from 'node:assert/strict'
|
|
8
8
|
|
|
9
|
-
const { OpsError, planRestore, applyRestore,
|
|
9
|
+
const { OpsError, planRestore, applyRestore, messageText } =
|
|
10
10
|
await import('../src/ops-core.js')
|
|
11
11
|
const { createMessageOpsTool, apply } = await import('../src/index.js')
|
|
12
12
|
|
|
@@ -38,32 +38,8 @@ const header = { type: 'session', version: 3, id: 'session-aaaaaaaa-bbbb-cccc-dd
|
|
|
38
38
|
|
|
39
39
|
// --- exportMarkdown ----------------------------------------------------------
|
|
40
40
|
|
|
41
|
-
test('exportMarkdown:消息按角色小节展开,工具调用折叠为单行', () => {
|
|
42
|
-
const md = exportMarkdown(header, sampleEvents())
|
|
43
|
-
assert.match(md, /^# DSH 会话导出:session-aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/m)
|
|
44
|
-
assert.match(md, /## \[seq 0\] user\n\n第一问/)
|
|
45
|
-
assert.match(md, /## \[seq 1\] assistant\n\n第一答/)
|
|
46
|
-
// 工具调用是单行引用,且含参数提示
|
|
47
|
-
const toolLines = md.split('\n').filter((l) => l.startsWith('> [seq 2]'))
|
|
48
|
-
assert.equal(toolLines.length, 1)
|
|
49
|
-
assert.match(toolLines[0], /🔧 工具调用:bash/)
|
|
50
|
-
assert.match(toolLines[0], /command/)
|
|
51
|
-
// revert 标记本身是 system 消息,也展开
|
|
52
|
-
assert.match(md, /## \[seq 5\] system/)
|
|
53
|
-
})
|
|
54
41
|
|
|
55
|
-
test('exportMarkdown:seq 上界只导出 ≤ 上界的事件', () => {
|
|
56
|
-
const md = exportMarkdown(header, sampleEvents(), 1)
|
|
57
|
-
assert.match(md, /seq ≤ 1/)
|
|
58
|
-
assert.match(md, /第一问/)
|
|
59
|
-
assert.doesNotMatch(md, /第二问/)
|
|
60
|
-
assert.doesNotMatch(md, /工具调用:bash/)
|
|
61
|
-
})
|
|
62
42
|
|
|
63
|
-
test('exportMarkdown:非法 seq 抛 OpsError(400)', () => {
|
|
64
|
-
assert.throws(() => exportMarkdown(header, sampleEvents(), -1), (e) => e instanceof OpsError && e.status === 400)
|
|
65
|
-
assert.throws(() => exportMarkdown(header, sampleEvents(), 1.5), OpsError)
|
|
66
|
-
})
|
|
67
43
|
|
|
68
44
|
// --- planRestore / applyRestore ----------------------------------------------
|
|
69
45
|
|
|
@@ -124,9 +100,15 @@ test('applyRestore:重放 append 干净文本(0.6.0 无前缀)+ system 说
|
|
|
124
100
|
assert.equal(appended[0].type, 'system/message')
|
|
125
101
|
assert.match(appended[0].data.message.content[0].text, /\[消息恢复\] 重放 seq 3\.\.4 的 2 条消息/)
|
|
126
102
|
assert.equal(appended[1].type, 'user/message')
|
|
127
|
-
|
|
128
|
-
assert.equal(appended[
|
|
103
|
+
// v4 契约:user/message 的消息本体字段直接摊在 data 上(0.8.1 P0 修正)
|
|
104
|
+
assert.equal(appended[1].data.content[0].text, '第二问', '0.6.0 对齐 opencode:无前缀')
|
|
105
|
+
assert.equal(appended[1].data.role, 'user')
|
|
106
|
+
assert.equal(appended[2].data.message.content[0].text, '第二答', 'assistant/message 才包 data.message')
|
|
129
107
|
for (const a of appended) assert.equal(a.opts.surfaceOp, 'append')
|
|
108
|
+
for (const a of appended) {
|
|
109
|
+
const m = a.type === 'user/message' ? a.data : a.data.message
|
|
110
|
+
assert.ok(m.source && m.source.kind, 'v4 准入:每条消息带 producer-owned source')
|
|
111
|
+
}
|
|
130
112
|
})
|
|
131
113
|
|
|
132
114
|
test('messageText:数组 content 提取首个非空 text;data.content 兼容', () => {
|
|
@@ -151,7 +133,7 @@ test('createMessageOpsTool:defineTool 桩拿到 name/parameters/execute,exec
|
|
|
151
133
|
const tool = createMessageOpsTool({ defineTool: stubDefineTool, ops, ctx: {} })
|
|
152
134
|
assert.equal(tool.name, 'message_ops')
|
|
153
135
|
assert.ok(tool.__compiled)
|
|
154
|
-
assert.equal(tool.parameters.action.enum.length,
|
|
136
|
+
assert.equal(tool.parameters.action.enum.length, 4, '0.9.0:移除 branch/export(用官方能力)')
|
|
155
137
|
assert.ok(tool.parameters.sessionId.required)
|
|
156
138
|
const out = await tool.execute({ action: 'list', sessionId: 'session-x' })
|
|
157
139
|
assert.match(out, /session session-x/)
|
|
@@ -192,19 +174,17 @@ test('apply:无 webServer、无 tools 时走 inject 等待,不抛错', () =>
|
|
|
192
174
|
assert.equal(ctx.registrations.length, 0)
|
|
193
175
|
})
|
|
194
176
|
|
|
195
|
-
test('apply:webServer 存在时注册全部
|
|
177
|
+
test('apply:webServer 存在时注册全部 5 条路由;tools 缺失走 inject 等待', () => {
|
|
196
178
|
const ctx = mockCtx({ webServer: { register: (route) => ctx.registrations.push(route) } })
|
|
197
179
|
assert.doesNotThrow(() => apply(ctx))
|
|
198
180
|
const paths = ctx.registrations.map((r) => r.path).sort()
|
|
199
181
|
assert.deepEqual(paths, [
|
|
200
|
-
'/api/message-ops/branch',
|
|
201
182
|
'/api/message-ops/delete',
|
|
202
|
-
'/api/message-ops/export',
|
|
203
183
|
'/api/message-ops/messages',
|
|
204
184
|
'/api/message-ops/restore',
|
|
205
185
|
'/api/message-ops/revert',
|
|
206
186
|
'/api/message-ops/text',
|
|
207
|
-
])
|
|
187
|
+
], '0.9.0:branch/export 路由已移除(改用官方 session/fork 与 /api/session.export)')
|
|
208
188
|
// tools 缺失 → 容错等待而非 fatal
|
|
209
189
|
assert.ok(ctx.injects.some(([deps]) => deps[0] === 'tools'))
|
|
210
190
|
})
|
|
@@ -220,7 +200,7 @@ test('apply:tools 服务存在时直接进入注册路径(包缺失时 catch
|
|
|
220
200
|
|
|
221
201
|
// ── 0.5.2 回归:工具层必须 await async ops(0.2.1 改 async 后曾漏同步化,
|
|
222
202
|
// list/restore 抛 TypeError、export 渲出空 text;旧桩是同步的故漏检)──────
|
|
223
|
-
test('工具 execute:async ops 被 await(list/
|
|
203
|
+
test('工具 execute:async ops 被 await(list/revert/delete/restore 全路径)', async () => {
|
|
224
204
|
let def
|
|
225
205
|
const defineTool = (d) => { def = d; return d }
|
|
226
206
|
const asyncOps = {
|
|
@@ -228,23 +208,23 @@ test('工具 execute:async ops 被 await(list/branch/restore/export 全路
|
|
|
228
208
|
messages: [{ seq: 10, role: 'user', visible: true, snippet: 'hello' }] }),
|
|
229
209
|
revert: async () => ({ mode: 'revert', shadowedCount: 2, seq: 10, eventSeq: 99 }),
|
|
230
210
|
delete: async () => ({ mode: 'delete', shadowedCount: 1, seq: 10, eventSeq: 98 }),
|
|
231
|
-
branch: async () => ({ newId: 'session-child', keptEvents: 42, parentSession: undefined }),
|
|
232
211
|
restore: async () => ({ restoredCount: 1, skipped: 0, range: { startSeq: 5, endSeq: 6 }, eventSeqs: [7, 8] }),
|
|
233
|
-
export: async () => ({ markdown: '# md' }),
|
|
234
212
|
}
|
|
235
213
|
createMessageOpsTool({ defineTool, ops: asyncOps, ctx: {} })
|
|
236
214
|
assert.ok(def, 'tool defined')
|
|
237
215
|
const outList = await def.execute({ action: 'list', sessionId: 'session-abc' })
|
|
238
216
|
assert.match(String(outList), /session session-abc/)
|
|
239
217
|
assert.doesNotMatch(String(outList), /failed:/)
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
assert.match(String(
|
|
218
|
+
const outRevert = await def.execute({ action: 'revert', sessionId: 'session-abc', seq: 10 })
|
|
219
|
+
assert.match(String(outRevert), /revert ok: shadowed 2 node\(s\) from seq 10/)
|
|
220
|
+
const outDelete = await def.execute({ action: 'delete', sessionId: 'session-abc', seq: 10 })
|
|
221
|
+
assert.match(String(outDelete), /delete ok: shadowed 1 node\(s\) from seq 10/)
|
|
244
222
|
const outRestore = await def.execute({ action: 'restore', sessionId: 'session-abc', seq: 6 })
|
|
245
223
|
assert.match(String(outRestore), /replayed 1 message/)
|
|
246
|
-
|
|
247
|
-
|
|
224
|
+
// 0.9.0:branch / export 不再是本工具的动作(改用官方 session/fork 与 /api/session.export)
|
|
225
|
+
for (const gone of ['branch', 'export']) {
|
|
226
|
+
assert.equal(def.parameters.action.enum.includes(gone), false, gone + ' 不应出现在 enum')
|
|
227
|
+
}
|
|
248
228
|
// 同步抛出的 ops 仍走错误文案路径
|
|
249
229
|
const errTool = await (async () => {
|
|
250
230
|
let d2
|
|
@@ -354,8 +334,25 @@ test('磁盘计划/构建器:可见节点剔除遮蔽、marker 字段逐项镜
|
|
|
354
334
|
const nt = core.buildRestoreNoticeEvent({ seq: 16, time: 2, turnStep: { turn: 9, step: 6 }, text: 'n', restoreSeq: 13 });
|
|
355
335
|
assert.equal(nt.surfaceOp, 'append');
|
|
356
336
|
assert.equal(nt.data.restoresSeq, 13);
|
|
357
|
-
const rp = core.buildReplayEvent({ seq: 17, time: 3, item: { type: 'user/message', role: 'user', text: 'hi' } })
|
|
358
|
-
assert.equal(rp.surfaceOp, 'append')
|
|
359
|
-
|
|
360
|
-
assert.
|
|
337
|
+
const rp = core.buildReplayEvent({ seq: 17, time: 3, turnStep: { turn: 3, step: 7 }, item: { type: 'user/message', role: 'user', text: 'hi' } })
|
|
338
|
+
assert.equal(rp.surfaceOp, 'append')
|
|
339
|
+
// 0.8.1 P0 修正:user/message 本体摊在 data 上(不是 data.message)
|
|
340
|
+
assert.equal(rp.data.role, 'user')
|
|
341
|
+
assert.deepEqual(rp.data.content, [{ type: 'text', text: 'hi' }]) // 0.6.0 无前缀
|
|
342
|
+
assert.ok(!('message' in rp.data), 'user/message 不双重包裹')
|
|
343
|
+
// 契约断言:v3 要求 system 类消息 kind==='plugin',v4 只拒**字面量** 'plugin',
|
|
344
|
+
// 故只能是 {kind:'plugin', plugin:<name>}(迁移后成 plugin:<name>,v4 接受)
|
|
345
|
+
assert.equal(rp.data.source.kind, 'plugin:@240xu/dsh-message-ops', 'v4 直接写必须用 producerKind 形态')
|
|
346
|
+
// assistant 必须带**打开的**坐标,且不是写死的 1/1
|
|
347
|
+
const rp2 = core.buildReplayEvent({ seq: 18, time: 4, turnStep: { turn: 3, step: 7 }, item: { type: 'assistant/message', role: 'assistant', text: 'yo' } })
|
|
348
|
+
assert.equal(rp2.data.turn, 3, 'replayed assistant must use the open turn')
|
|
349
|
+
assert.equal(rp2.data.step, 7, 'replayed assistant must use the open step')
|
|
350
|
+
assert.equal(rp2.data.message.role, 'assistant')
|
|
351
|
+
assert.equal(rp2.data.message.source.kind, 'plugin:@240xu/dsh-message-ops')
|
|
352
|
+
// v3 目标日志:必须用 {kind:'plugin', plugin:<name>}(v3 要求字面量 plugin)
|
|
353
|
+
const rp3 = core.buildReplayEvent({ seq: 20, time: 6, version: 3, turnStep: { turn: 1, step: 1 }, item: { type: 'user/message', role: 'user', text: 'v3' } })
|
|
354
|
+
assert.equal(rp3.data.source.kind, 'plugin', 'v3 日志要求字面量 plugin')
|
|
355
|
+
assert.equal(rp3.data.source.plugin, '@240xu/dsh-message-ops')
|
|
356
|
+
// 缺 turnStep 必须抛错,绝不静默落 1/1
|
|
357
|
+
assert.throws(() => core.buildReplayEvent({ seq: 19, time: 5, item: { type: 'assistant/message', role: 'assistant', text: 'z' } }), (e) => e.status === 500)
|
|
361
358
|
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 契约测试:revert / restore 写出的事件必须通过 **dsh 官方三层闸**。
|
|
3
|
+
*
|
|
4
|
+
* 三层缺一不可(每一层都真实咬过人):
|
|
5
|
+
* 1. 格式层 assertV4RowAdmission + restoreReleasedV4Artifact
|
|
6
|
+
* 2. 运行时层 role 匹配 / source.kind 非空 / system/message 必须 system-prompt
|
|
7
|
+
* 3. UI 层 turn/end.data.reason 必填(trajectory 装配器无防护读 reason.kind,
|
|
8
|
+
* 缺字段 → TypeError → 整个会话页白屏)
|
|
9
|
+
*
|
|
10
|
+
* 只查第 1 层会得到「全绿但 UI 崩」的假象(0.8.x 实机踩过)。
|
|
11
|
+
*/
|
|
12
|
+
import test from 'node:test'
|
|
13
|
+
import assert from 'node:assert/strict'
|
|
14
|
+
import { zstdCompressSync } from 'node:zlib'
|
|
15
|
+
import { createRequire } from 'node:module'
|
|
16
|
+
import { join, dirname } from 'node:path'
|
|
17
|
+
|
|
18
|
+
import { decodeFrames, parseRows } from '/data/data/com.termux/files/home/dsh-plugins-src/sessionfix/lib/seqcore.js'
|
|
19
|
+
import { officialValidate } from '/data/data/com.termux/files/home/dsh-plugins-src/sessionfix/lib/v4gate.mjs'
|
|
20
|
+
import {
|
|
21
|
+
planNoticeWindow, noticeWindowPreamble, noticeWindowPostamble,
|
|
22
|
+
buildMarkerEvent, buildRestoreNoticeEvent, buildReplayEvent, planRestore, restoreProgress,
|
|
23
|
+
} from '../src/ops-core.js'
|
|
24
|
+
|
|
25
|
+
const HEADER = { type: 'session', version: 4, id: 'contract-test', createdAt: 1, isSeeded: false, delegationDepth: 0 }
|
|
26
|
+
|
|
27
|
+
const pack = (events) => zstdCompressSync(Buffer.from([JSON.stringify(HEADER), ...events.map((e) => JSON.stringify(e))].join('\n') + '\n'))
|
|
28
|
+
|
|
29
|
+
/** 空闲会话骨架:turn 1 已闭合,turn 2 是合成的 notice 窗口 */
|
|
30
|
+
function idleSessionBase() {
|
|
31
|
+
return [
|
|
32
|
+
{ type: 'turn/start', data: { turn: 1 } },
|
|
33
|
+
{ type: 'step/start', data: { turn: 1, step: 1 } },
|
|
34
|
+
{ type: 'system/message', data: { turn: 1, step: 1, message: { id: 'sys0', role: 'system', source: { kind: 'system-prompt' }, content: [{ type: 'text', text: 'sys' }] } }, surfaceOp: 'append' },
|
|
35
|
+
{ type: 'user/message', data: { id: 'u1', role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: '问题一' }] }, surfaceOp: 'append' },
|
|
36
|
+
{ type: 'assistant/message', data: { turn: 1, step: 1, message: { id: 'a1', role: 'assistant', source: { kind: 'assistant' }, content: [{ type: 'text', text: '答一' }] } }, surfaceOp: 'append' },
|
|
37
|
+
{ type: 'step/end', data: { turn: 1, step: 1 } },
|
|
38
|
+
{ type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } },
|
|
39
|
+
].map((e, i) => ({ ...e, seq: i, time: 1000 + i }))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test('契约:合成窗口承载的 revert marker 通过官方三层闸', () => {
|
|
43
|
+
const base = idleSessionBase()
|
|
44
|
+
const win = planNoticeWindow(base)
|
|
45
|
+
assert.equal(win.synthetic, true, '空闲会话应规划合成窗口')
|
|
46
|
+
assert.equal(win.turn, 2, '合成 turn 应为 maxTurn+1')
|
|
47
|
+
|
|
48
|
+
let cursor = base.length, t = 5000
|
|
49
|
+
const events = [...base]
|
|
50
|
+
for (const pre of noticeWindowPreamble(win)) events.push({ type: pre.kind, seq: cursor++, time: t++, data: pre.data })
|
|
51
|
+
events.push(buildMarkerEvent({
|
|
52
|
+
seq: cursor++, time: t++, turnStep: { turn: win.turn, step: win.step },
|
|
53
|
+
text: '[消息回滚] 测试', version: 4, startSeq: 3, endSeq: 4, shadowedSeqs: [3, 4],
|
|
54
|
+
}))
|
|
55
|
+
for (const post of noticeWindowPostamble(win)) events.push({ type: post.kind, seq: cursor++, time: t++, data: post.data })
|
|
56
|
+
|
|
57
|
+
const v = officialValidate(pack(events))
|
|
58
|
+
assert.ok(v.ok, `官方三层闸拒绝:${v.error}`)
|
|
59
|
+
assert.equal(v.events, events.length)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('契约:合成窗口必须自带 reason(UI 层硬要求)', () => {
|
|
63
|
+
const win = { synthetic: true, turn: 9, step: 1 }
|
|
64
|
+
const post = noticeWindowPostamble(win)
|
|
65
|
+
const turnEnd = post.find((e) => e.kind === 'turn/end')
|
|
66
|
+
assert.ok(turnEnd, '缺少 turn/end 后缀')
|
|
67
|
+
assert.ok(turnEnd.data.reason, 'turn/end 缺 data.reason → UI 装配器崩')
|
|
68
|
+
assert.equal(turnEnd.data.reason.kind, 'interrupted')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('契约:缺 reason 的 turn/end 会被官方闸拦下(回归证明这条规则有效)', () => {
|
|
72
|
+
const base = idleSessionBase()
|
|
73
|
+
const bad = [...base, { type: 'turn/start', seq: base.length, time: 6000, data: { turn: 2 } },
|
|
74
|
+
{ type: 'step/start', seq: base.length + 1, time: 6001, data: { turn: 2, step: 1 } },
|
|
75
|
+
{ type: 'system/message', seq: base.length + 2, time: 6002, data: { turn: 2, step: 1, message: { id: 'n', role: 'system', source: { kind: 'system-prompt' }, content: [{ type: 'text', text: 'x' }] } }, surfaceOp: 'append' },
|
|
76
|
+
{ type: 'step/end', seq: base.length + 3, time: 6003, data: { turn: 2, step: 1 } },
|
|
77
|
+
{ type: 'turn/end', seq: base.length + 4, time: 6004, data: { turn: 2 } }] // ← 缺 reason
|
|
78
|
+
const v = officialValidate(pack(bad))
|
|
79
|
+
assert.equal(v.ok, false, '缺 reason 竟然通过了 —— UI 层规则失效')
|
|
80
|
+
assert.match(v.error, /reason/, `错误信息应指向 reason,实际:${v.error}`)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('契约:完整 restore 链路(notice + 重放)通过官方三层闸', () => {
|
|
84
|
+
const base = idleSessionBase()
|
|
85
|
+
let cursor = base.length, t = 5000
|
|
86
|
+
const win = planNoticeWindow(base)
|
|
87
|
+
const afterRevert = [...base]
|
|
88
|
+
for (const pre of noticeWindowPreamble(win)) afterRevert.push({ type: pre.kind, seq: cursor++, time: t++, data: pre.data })
|
|
89
|
+
const marker = buildMarkerEvent({ seq: cursor++, time: t++, turnStep: { turn: win.turn, step: win.step }, text: '[消息回滚]', version: 4, startSeq: 3, endSeq: 4, shadowedSeqs: [3, 4] })
|
|
90
|
+
afterRevert.push(marker)
|
|
91
|
+
for (const post of noticeWindowPostamble(win)) afterRevert.push({ type: post.kind, seq: cursor++, time: t++, data: post.data })
|
|
92
|
+
assert.ok(officialValidate(pack(afterRevert)).ok, 'revert 产物不合法')
|
|
93
|
+
|
|
94
|
+
// restore:重新规划窗口(上一个已被占用)
|
|
95
|
+
const win2 = planNoticeWindow(afterRevert)
|
|
96
|
+
const plan = planRestore(afterRevert, marker.seq, undefined, restoreProgress(afterRevert, marker.seq)?.restoredSeqs ?? null)
|
|
97
|
+
plan.turnStep = { turn: win2.turn, step: win2.step }
|
|
98
|
+
const afterRestore = [...afterRevert]
|
|
99
|
+
for (const pre of noticeWindowPreamble(win2)) afterRestore.push({ type: pre.kind, seq: cursor++, time: t++, data: pre.data })
|
|
100
|
+
afterRestore.push(buildRestoreNoticeEvent({ seq: cursor++, time: t++, turnStep: plan.turnStep, version: 4, text: '[消息恢复]', restoreSeq: plan.restoreSeq }))
|
|
101
|
+
let off = 0
|
|
102
|
+
for (const item of plan.replayable) afterRestore.push(buildReplayEvent({ seq: cursor++, time: t + (++off), item, turnStep: plan.turnStep, version: 4 }))
|
|
103
|
+
for (const post of noticeWindowPostamble(win2)) afterRestore.push({ type: post.kind, seq: cursor++, time: t + off + (off++), data: post.data })
|
|
104
|
+
|
|
105
|
+
const v = officialValidate(pack(afterRestore))
|
|
106
|
+
assert.ok(v.ok, `restore 产物被官方闸拒绝:${v.error}`)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('契约:buildReplayEvent 缺 turnStep 必须抛错(绝不静默落 1/1)', () => {
|
|
110
|
+
assert.throws(
|
|
111
|
+
() => buildReplayEvent({ seq: 1, time: 1, item: { type: 'assistant/message', role: 'assistant', text: 'z' } }),
|
|
112
|
+
(e) => e.status === 500,
|
|
113
|
+
)
|
|
114
|
+
assert.throws(
|
|
115
|
+
() => buildReplayEvent({ seq: 1, time: 1, turnStep: { turn: 0, step: 1 }, item: { type: 'assistant/message', role: 'assistant', text: 'z' } }),
|
|
116
|
+
(e) => e.status === 500,
|
|
117
|
+
)
|
|
118
|
+
})
|
|
@@ -12,7 +12,6 @@ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-msgops-'))
|
|
|
12
12
|
|
|
13
13
|
const { scanZstdFrames, readSessionFile, encodeSessionFile, findSessionDirs, listMessages, computeShadowed } =
|
|
14
14
|
await import('../src/session-file.js')
|
|
15
|
-
const { planBranch, applyBranch } = await import('../src/branch.js')
|
|
16
15
|
|
|
17
16
|
const MAGIC = 0xfd2fb528
|
|
18
17
|
|
|
@@ -74,30 +73,7 @@ test('computeShadowed 识别 surface replace 遮蔽区间', () => {
|
|
|
74
73
|
assert.ok(!shadowed.has(0))
|
|
75
74
|
})
|
|
76
75
|
|
|
77
|
-
test('planBranch 生成新 id + parentSession,事件截断正确', () => {
|
|
78
|
-
const h = header('session-00000000-0000-0000-0000-000000000004')
|
|
79
|
-
const events = [msg('user/message', 0, 'a'), msg('assistant/message', 1, 'b'), msg('user/message', 2, 'c')]
|
|
80
|
-
const { header: nh, events: kept } = planBranch(h, events, 1)
|
|
81
|
-
assert.notEqual(nh.id, h.id)
|
|
82
|
-
assert.equal(nh.parentSession, h.id)
|
|
83
|
-
assert.match(nh.id, /^session-[0-9a-f-]{36}$/)
|
|
84
|
-
assert.equal(kept.length, 2)
|
|
85
|
-
assert.throws(() => planBranch(h, events, -1))
|
|
86
|
-
})
|
|
87
76
|
|
|
88
|
-
test('applyBranch 落盘:新目录 + 新日志 + 原文件字节不变', () => {
|
|
89
|
-
const id = 'session-00000000-0000-0000-0000-000000000005'
|
|
90
|
-
const events = [msg('user/message', 0, 'a'), msg('assistant/message', 1, 'b'), msg('user/message', 2, 'c')]
|
|
91
|
-
const origLog = writeLog(path.join(process.env.DSH_HOME, 'sessions', 'proj-b', id), header(id), events)
|
|
92
|
-
const before = fs.readFileSync(origLog)
|
|
93
|
-
const result = applyBranch(origLog, 1)
|
|
94
|
-
assert.match(result.newId, /^session-[0-9a-f-]{36}$/)
|
|
95
|
-
assert.ok(fs.existsSync(result.logPath))
|
|
96
|
-
const { header: nh, events: kept } = readSessionFile(result.logPath)
|
|
97
|
-
assert.equal(nh.parentSession, id)
|
|
98
|
-
assert.equal(kept.length, 2)
|
|
99
|
-
assert.deepEqual(fs.readFileSync(origLog), before)
|
|
100
|
-
})
|
|
101
77
|
|
|
102
78
|
test('findSessionDirs 两种 id 拼写均可定位', () => {
|
|
103
79
|
const uuid = '11111111-2222-3333-4444-555555555555'
|
|
@@ -94,3 +94,19 @@ test("0.8.0 B1:planRestore(excludeSeqs) 排除已重放,杜绝副本刷屏",
|
|
|
94
94
|
const all = planRestore(events, 20, 23, [11, 12, 13, 14]);
|
|
95
95
|
assert.deepEqual(all.replayable.map((i) => i.seq), [], "全部已重放 → 空计划(只补 notice)");
|
|
96
96
|
});
|
|
97
|
+
|
|
98
|
+
test("0.8.1 discard:notice 带 discarded → 标记完成,不重放内容", () => {
|
|
99
|
+
const events = fixtures();
|
|
100
|
+
const plan = planRestore(events, 20);
|
|
101
|
+
plan.discard = true;
|
|
102
|
+
const appended = [];
|
|
103
|
+
const fake = { append: (type, data) => { const e = { seq: 100 + appended.length, type, data }; appended.push(e); return e } };
|
|
104
|
+
const r = applyRestore(fake, plan, {});
|
|
105
|
+
assert.equal(r.restoredCount, 0, "丢弃 → 不重放");
|
|
106
|
+
assert.equal(appended.length, 1, "只有 notice");
|
|
107
|
+
assert.equal(appended[0].data.discarded, true);
|
|
108
|
+
events.push({ seq: 50, type: "system/message", time: 1700000000050, data: { turn: 1, step: 1, message: { id: "m50", role: "system", source: { kind: "system-prompt" }, content: [{ type: "text", text: "n" }] }, restoresSeq: 20, discarded: true }, surfaceOp: "append" });
|
|
109
|
+
const prog = restoreProgress(events, 20);
|
|
110
|
+
assert.equal(prog.discarded, true);
|
|
111
|
+
assert.equal(prog.complete, true, "丢弃 → 完成");
|
|
112
|
+
});
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.9.1 transcript 可见性映射 —— 回归锁死。
|
|
3
|
+
*
|
|
4
|
+
* 背景:dsh 的 transcript **刻意不做 surface 折叠**(surface.d.ts:51-63
|
|
5
|
+
* "replacement copies stay model-only")。回滚要「看得见」只能在渲染层处理,
|
|
6
|
+
* 而唯一可靠的行↔日志映射是 `data-chat-node-key` 里内嵌的官方 id。
|
|
7
|
+
*
|
|
8
|
+
* 本文件锁三件事:
|
|
9
|
+
* ① node-key 解析与查表(含 assistant-step 一 node 两行)
|
|
10
|
+
* ② **不能用 `visible` 当隐藏判据** —— tool/call 等非 surface 事件永远 visible=false,
|
|
11
|
+
* 照抄会把所有工具调用行永久藏掉(实测 3/3 全被误藏)
|
|
12
|
+
* ③ 查不到时保持可见,绝不误藏
|
|
13
|
+
*/
|
|
14
|
+
import test from 'node:test'
|
|
15
|
+
import assert from 'node:assert/strict'
|
|
16
|
+
|
|
17
|
+
/** 与 client.js 的 parseNodeKey / buildIndex / seqOfNodeKey 同构 */
|
|
18
|
+
function parseNodeKey(k) {
|
|
19
|
+
const m = /^(\d+):/.exec(k || '')
|
|
20
|
+
if (!m) return null
|
|
21
|
+
const digits = m[1].length
|
|
22
|
+
const kindLen = Number(m[1])
|
|
23
|
+
const start = digits + 1
|
|
24
|
+
if (!Number.isSafeInteger(kindLen) || kindLen <= 0 || k.length < start + kindLen) return null
|
|
25
|
+
return { kind: k.slice(start, start + kindLen), id: k.slice(start + kindLen) }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function buildIndex(list) {
|
|
29
|
+
const dupByMessageId = new Map()
|
|
30
|
+
const byMessageId = new Map(), byTurnStep = new Map(), byCallId = new Map(), byTurn = new Map()
|
|
31
|
+
const visible = new Set(), reverted = new Set()
|
|
32
|
+
let dupMessageId = 0
|
|
33
|
+
for (const m of list || []) {
|
|
34
|
+
if (!m || typeof m.seq !== 'number') continue
|
|
35
|
+
if (m.id != null) {
|
|
36
|
+
const k = String(m.id)
|
|
37
|
+
if (byMessageId.has(k)) {
|
|
38
|
+
dupMessageId++
|
|
39
|
+
const arr = dupByMessageId.get(k) || [byMessageId.get(k)]
|
|
40
|
+
arr.push(m.seq); dupByMessageId.set(k, arr); byMessageId.set(k, m.seq)
|
|
41
|
+
} else { byMessageId.set(k, m.seq) }
|
|
42
|
+
}
|
|
43
|
+
if (m.type === 'assistant/message' && m.turn != null && m.step != null) byTurnStep.set(m.turn + ':' + m.step, m.seq)
|
|
44
|
+
if (m.type === 'tool/call' && m.id != null) byCallId.set(String(m.id), m.seq)
|
|
45
|
+
if (m.turn != null && !byTurn.has(String(m.turn))) byTurn.set(String(m.turn), m.seq)
|
|
46
|
+
if (m.visible === true) visible.add(m.seq)
|
|
47
|
+
if (m.reverted === true) reverted.add(m.seq)
|
|
48
|
+
}
|
|
49
|
+
return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, visible, reverted, dupMessageId, total: (list || []).length }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function seqOfNodeKey(key, idx) {
|
|
53
|
+
const p = parseNodeKey(key)
|
|
54
|
+
if (!p || !idx) return null
|
|
55
|
+
switch (p.kind) {
|
|
56
|
+
case 'input-message': case 'developer-message': case 'trajectory-note': {
|
|
57
|
+
const h = idx.byMessageId.get(p.id)
|
|
58
|
+
return h === undefined ? null : { seq: h, all: idx.dupByMessageId.get(p.id) || [h] }
|
|
59
|
+
}
|
|
60
|
+
case 'assistant-step': { const h = idx.byTurnStep.get(p.id); return h === undefined ? null : { seq: h, all: [h] } }
|
|
61
|
+
case 'tool-call': { const h = idx.byCallId.get(p.id); return h === undefined ? null : { seq: h, all: [h] } }
|
|
62
|
+
case 'turn-tail': case 'turn-process': case 'turn-error': case 'turn-max-tokens': case 'model-retry': {
|
|
63
|
+
const h = idx.byTurn.get(p.id); return h === undefined ? null : { seq: h, all: [h] }
|
|
64
|
+
}
|
|
65
|
+
default: return null
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ── 真实形状的语料(取自分支会话 session-0da37218)─────────────────────────
|
|
70
|
+
const K = {
|
|
71
|
+
user: (id) => `13:input-message${id}`,
|
|
72
|
+
step: (t, s) => `14:assistant-step${t}:${s}`,
|
|
73
|
+
tool: (id) => `9:tool-call${id}`,
|
|
74
|
+
tail: (t) => `9:turn-tail${t}`,
|
|
75
|
+
process: (t) => `12:turn-process${t}`,
|
|
76
|
+
dev: (id) => `17:developer-message${id}`,
|
|
77
|
+
}
|
|
78
|
+
const CORPUS = [
|
|
79
|
+
{ seq: 8, type: 'user/message', id: '8b5093a5-205e-4108-a886-00981bbd57da', visible: true, reverted: false },
|
|
80
|
+
{ seq: 18, type: 'assistant/message', id: 'a1', turn: 1, step: 1, visible: true, reverted: false },
|
|
81
|
+
{ seq: 19, type: 'tool/call', id: 'call_2a9430d7030b4c04a75e7e87', turn: 1, step: 1, visible: false, reverted: false },
|
|
82
|
+
{ seq: 21, type: 'tool/call', id: 'call_2da69ea515e64d6181458e2f', turn: 1, step: 2, visible: false, reverted: false },
|
|
83
|
+
{ seq: 25, type: 'assistant/message', id: 'a2', turn: 1, step: 2, visible: true, reverted: false },
|
|
84
|
+
{ seq: 26, type: 'tool/call', id: 'call_c30c0f21878a43f0926c77a9', turn: 1, step: 2, visible: false, reverted: false },
|
|
85
|
+
{ seq: 54, type: 'user/message', id: '71921f48-3f8a-4f97-951f-122cfd64cb2e', visible: true, reverted: true },
|
|
86
|
+
{ seq: 63, type: 'system/message', id: 'm1', visible: true, reverted: false, marker: true, range: { start: 54, end: 54 } },
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
test('parseNodeKey:解析 <len>:<kind><id>', () => {
|
|
90
|
+
assert.deepEqual(parseNodeKey(K.user('abc')), { kind: 'input-message', id: 'abc' })
|
|
91
|
+
assert.deepEqual(parseNodeKey(K.step(1, 2)), { kind: 'assistant-step', id: '1:2' })
|
|
92
|
+
assert.deepEqual(parseNodeKey(K.tail(3)), { kind: 'turn-tail', id: '3' })
|
|
93
|
+
assert.equal(parseNodeKey(''), null)
|
|
94
|
+
assert.equal(parseNodeKey('garbage'), null)
|
|
95
|
+
assert.equal(parseNodeKey('99:tool-call'), null, '声明长度超出字符串 → null')
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('查表:六种行全部命中真实 seq', () => {
|
|
99
|
+
const idx = buildIndex(CORPUS)
|
|
100
|
+
assert.equal(seqOfNodeKey(K.user('8b5093a5-205e-4108-a886-00981bbd57da'), idx)?.seq, 8)
|
|
101
|
+
assert.equal(seqOfNodeKey(K.step(1, 1), idx)?.seq, 18)
|
|
102
|
+
assert.equal(seqOfNodeKey(K.tool('call_2a9430d7030b4c04a75e7e87'), idx)?.seq, 19)
|
|
103
|
+
assert.equal(seqOfNodeKey(K.tail(1), idx)?.seq, 18, 'turn 号映射到该 turn 首个带 turn 字段的消息 seq(回合页脚不是消息,用锚点近似)')
|
|
104
|
+
assert.equal(seqOfNodeKey(K.dev('nope'), idx), null, '未知 id → null')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('回归:用 visible 当隐藏判据会误藏全部工具调用行', () => {
|
|
108
|
+
const idx = buildIndex(CORPUS)
|
|
109
|
+
const wronglyHidden = ['call_2a9430d7030b4c04a75e7e87', 'call_2da69ea515e64d6181458e2f', 'call_c30c0f21878a43f0926c77a9']
|
|
110
|
+
.filter((id) => !idx.visible.has(seqOfNodeKey(K.tool(id), idx)?.seq))
|
|
111
|
+
assert.equal(wronglyHidden.length, 3, '三个 tool-call 都 visible=false —— 这就是为什么不能用 visible')
|
|
112
|
+
// 用 reverted 则一个都不该被误藏
|
|
113
|
+
const withReverted = wronglyHidden.filter((id) => idx.reverted.has(seqOfNodeKey(K.tool(id), idx)?.seq))
|
|
114
|
+
assert.equal(withReverted.length, 0)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('reverted 口径:只藏边界及其后', () => {
|
|
118
|
+
const idx = buildIndex(CORPUS)
|
|
119
|
+
assert.equal(idx.reverted.has(54), true, '边界消息自己也要藏(opencode slice(0, boundaryIndex))')
|
|
120
|
+
assert.equal(idx.reverted.has(26), false, '边界之前的不藏')
|
|
121
|
+
assert.equal(idx.reverted.has(8), false)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('查不到时返回 null → 调用方保持可见(绝不误藏)', () => {
|
|
125
|
+
const idx = buildIndex(CORPUS)
|
|
126
|
+
assert.equal(seqOfNodeKey(K.user('00000000-0000-0000-0000-000000000000'), idx), null)
|
|
127
|
+
assert.equal(seqOfNodeKey(K.step(9, 9), idx), null)
|
|
128
|
+
assert.equal(seqOfNodeKey(K.tail(9), idx), null)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('compaction / command 等不参与映射(它们不是回撤对象)', () => {
|
|
132
|
+
const idx = buildIndex(CORPUS)
|
|
133
|
+
assert.equal(seqOfNodeKey('9:compactionxx', idx), null)
|
|
134
|
+
assert.equal(seqOfNodeKey('7:commandyy', idx), null)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('重复 messageId(restore 重放)保留全部 seq,指向最新', () => {
|
|
138
|
+
const idx = buildIndex([
|
|
139
|
+
{ seq: 5, type: 'user/message', id: 'dup', visible: true },
|
|
140
|
+
{ seq: 99, type: 'user/message', id: 'dup', visible: true }, // 重放副本
|
|
141
|
+
])
|
|
142
|
+
assert.equal(idx.byMessageId.get('dup'), 99, '指向最新(当前 surface 上的那条)')
|
|
143
|
+
assert.deepEqual(idx.dupByMessageId.get('dup'), [5, 99])
|
|
144
|
+
assert.equal(idx.dupMessageId, 1)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('回归:恢复后再回滚,副本必须被藏(用最早 seq 判会漏)', () => {
|
|
148
|
+
// fence=70;原件 seq=54、restore 副本 seq=70
|
|
149
|
+
const idx = buildIndex([
|
|
150
|
+
{ seq: 54, type: 'user/message', id: 'x', visible: true },
|
|
151
|
+
{ seq: 70, type: 'user/message', id: 'x', visible: true },
|
|
152
|
+
])
|
|
153
|
+
idx.fences = [70]
|
|
154
|
+
const hit = seqOfNodeKey(K.user('x'), idx)
|
|
155
|
+
const cands = hit.all
|
|
156
|
+
const hiddenByEarliestOnly = idx.fences.some((f) => cands[0] >= f) // 54 >= 70 → false
|
|
157
|
+
const hiddenByAny = idx.fences.some((f) => cands.some((q) => q >= f)) // 70 >= 70 → true
|
|
158
|
+
assert.equal(hiddenByEarliestOnly, false, '只用最早 seq 会漏藏')
|
|
159
|
+
assert.equal(hiddenByAny, true, '任一 seq 越过 fence 就要藏')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('空/畸形 payload 不抛异常', () => {
|
|
163
|
+
const idx = buildIndex(undefined)
|
|
164
|
+
assert.equal(idx.total, 0)
|
|
165
|
+
assert.equal(seqOfNodeKey(K.user('x'), idx), null)
|
|
166
|
+
})
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 用户消息回滚按钮的 DOM 配对(0.9.0 修复)。
|
|
3
|
+
*
|
|
4
|
+
* 实机 bug:4 个 `.mopsUserRevert` 按钮全部 `data-mops-seq === null`,点击必报
|
|
5
|
+
* `revert target verification failed` —— **任何发生过回滚的会话,这个按钮都是装饰品**。
|
|
6
|
+
*
|
|
7
|
+
* 根因:宿主聊天视图**不施加**本插件的遮蔽模型,照常渲染 seq 114 之后的全部消息;
|
|
8
|
+
* 而 `fetchRows` 在 client.js:968 硬过滤 `m.visible !== false`,配对候选只剩
|
|
9
|
+
* 可见的 [8,9,54],而 DOM 块对应的是被遮蔽的 seq 897/1074 → 零交集 → hit 恒 -1。
|
|
10
|
+
*
|
|
11
|
+
* 修法:配对行**必须覆盖宿主实际渲染的全部 user 行(含遮蔽态)**。
|
|
12
|
+
* 注意:对话框的可选目标列表(client.js:436)仍应过滤 visible —— 那是「可回滚目标」
|
|
13
|
+
* 语义,与 DOM 配对是两回事,不要一起改。
|
|
14
|
+
*/
|
|
15
|
+
import test from 'node:test'
|
|
16
|
+
import assert from 'node:assert/strict'
|
|
17
|
+
|
|
18
|
+
const norm = (x) => String(x || '').replace(/\s+/g, '')
|
|
19
|
+
const injectedFixed = (raw) =>
|
|
20
|
+
/^<system-reminder|current\s+runtime\s+context|^<system-Reminder/i.test(String(raw || ''))
|
|
21
|
+
|
|
22
|
+
/** client.js:965-972 的行构造(修复版:不过滤 visible,且保留 raw) */
|
|
23
|
+
export function buildPairRows(messages) {
|
|
24
|
+
return (messages || [])
|
|
25
|
+
.filter((m) => m && m.role === 'user')
|
|
26
|
+
.map((m) => ({ seq: m.seq, sn: norm(m.snippet), raw: String(m.snippet ?? '') }))
|
|
27
|
+
.filter((r) => r.sn && !injectedFixed(r.raw))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** client.js:1000-1012 的按序配对(纯函数化,便于测试) */
|
|
31
|
+
export function assignSeqs(rows, blocks) {
|
|
32
|
+
const assigned = []
|
|
33
|
+
let ri = 0
|
|
34
|
+
for (const headText of blocks) {
|
|
35
|
+
const head = norm(headText.slice(0, 240))
|
|
36
|
+
let hit = -1
|
|
37
|
+
for (let j = ri; j < rows.length; j++) {
|
|
38
|
+
const a = (rows[j].sn || '').slice(0, 40)
|
|
39
|
+
const b = (head || '').slice(0, 40)
|
|
40
|
+
if (a && b && (head.includes(a) || rows[j].sn.includes(b))) { hit = j; break }
|
|
41
|
+
}
|
|
42
|
+
assigned.push(hit >= 0 ? rows[hit].seq : null)
|
|
43
|
+
if (hit >= 0) ri = hit + 1
|
|
44
|
+
}
|
|
45
|
+
return assigned
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 实机语料:393 条消息中,宿主渲染的 user 块对应的是**被遮蔽**的那批
|
|
49
|
+
const MESSAGES = [
|
|
50
|
+
{ seq: 8, role: 'user', visible: true, snippet: '分配几个子代理把模型广场的模型价格什么的做好' },
|
|
51
|
+
{ seq: 9, role: 'user', visible: true, snippet: 'Current runtime context. This snapshot supersedes earlier runtime-context snapshots.' },
|
|
52
|
+
{ seq: 10, role: 'user', visible: true, snippet: '<system-reminder> A skill is a reusable set of task-specific instructions.' },
|
|
53
|
+
{ seq: 54, role: 'user', visible: true, snippet: '基础价格按官方api价格来,缓存命中价格加上。' },
|
|
54
|
+
{ seq: 897, role: 'user', visible: false, snippet: '继续,你把mimo的搞好' },
|
|
55
|
+
{ seq: 1074, role: 'user', visible: false, snippet: '你自己看看playright去调用一下' },
|
|
56
|
+
]
|
|
57
|
+
// 宿主 DOM 实际渲染的 user 块(前 4 条可见的 + 被遮蔽后��主照常渲染的)
|
|
58
|
+
const DOM_BLOCKS = [
|
|
59
|
+
'分配几个子代理把模型广场的模型价格什么的做好,渠道应该写好了吧',
|
|
60
|
+
'基础价格按官方api价格来,缓存命中价格加上。',
|
|
61
|
+
'继续,你把mimo的搞好',
|
|
62
|
+
'你自己看看playright去调用一下',
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
test('buildPairRows 不过滤 visible(遮蔽消息必须进入配对候选)', () => {
|
|
66
|
+
const rows = buildPairRows(MESSAGES)
|
|
67
|
+
assert.deepEqual(rows.map((r) => r.seq), [8, 54, 897, 1074],
|
|
68
|
+
'伪 user 行(seq9/10)被滤掉,遮蔽行(897/1074)必须保留')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('回归:旧的 visible 过滤会让遮蔽行消失(这正是原 bug)', () => {
|
|
72
|
+
const buggy = MESSAGES
|
|
73
|
+
.filter((m) => m && m.role === 'user' && m.visible !== false)
|
|
74
|
+
.map((m) => ({ seq: m.seq, sn: norm(m.snippet) }))
|
|
75
|
+
assert.deepEqual(buggy.map((r) => r.seq), [8, 9, 10, 54])
|
|
76
|
+
assert.deepEqual(assignSeqs(buggy, DOM_BLOCKS), [8, 54, null, null],
|
|
77
|
+
'旧逻辑:两个被遮蔽块拿不到 seq → 点击必失败')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('修复后:原本配不上的遮蔽块现在能配到 seq(核心回归)', () => {
|
|
81
|
+
const assigned = assignSeqs(buildPairRows(MESSAGES), DOM_BLOCKS)
|
|
82
|
+
assert.deepEqual(assigned, [8, 54, 897, 1074])
|
|
83
|
+
assert.ok(assigned.every((s) => s !== null), '存在配对失败的块 → 按钮是装饰品')
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('已知局限:DOM 文本与日志摘要不一致时配不上(走兜底而非报错)', () => {
|
|
87
|
+
// 实机观测:宿主渲染 `playwright`,日志 snippet 是 `playright`(模型自己写的)。
|
|
88
|
+
// 配对是前 40 字的子串包含,措辞漂移就会失配 —— 这是已知局限,
|
|
89
|
+
// 0.9.0 的处理是「配不上就 disabled + title 提示」,而不是点了弹错。
|
|
90
|
+
const assigned = assignSeqs(buildPairRows(MESSAGES), ['你自己看看playwright去调用一下'])
|
|
91
|
+
assert.deepEqual(assigned, [null])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('纯 user ��息(未发生回滚的会话)行为不变', () => {
|
|
95
|
+
const msgs = MESSAGES.filter((m) => m.visible !== false)
|
|
96
|
+
const blocks = ['分配几个子代理把模型广场的模型价格什么的做好,渠道应该写好了吧', '基础价格按官方api价格来,缓存命中价格加上。']
|
|
97
|
+
assert.deepEqual(assignSeqs(buildPairRows(msgs), blocks), [8, 54])
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('配对失败的块返回 null(调用方据此 disabled + title 提示,而不是点了报错)', () => {
|
|
101
|
+
const assigned = assignSeqs(buildPairRows(MESSAGES), ['一个不存在的消息'])
|
|
102
|
+
assert.deepEqual(assigned, [null])
|
|
103
|
+
})
|