@240xu/dsh-message-ops 0.7.0 → 0.9.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.
- package/README.md +121 -7
- package/package.json +2 -2
- package/src/client.js +148 -117
- package/src/index.js +109 -103
- package/src/ops-core.js +287 -62
- 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 +112 -0
- package/test/user-revert-anchor.test.js +103 -0
- package/src/branch.js +0 -57
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* injected() 正则回归(0.9.0 修复)。
|
|
3
|
+
*
|
|
4
|
+
* 实机 bug:client.js 的 `norm()` 是 `replace(/\s+/g,'')` —— 删掉**全部**空白,
|
|
5
|
+
* 而 `injected()` 里的 `current\s+runtime\s+context` 跑在 norm 之后的字符串上,
|
|
6
|
+
* `\s+` 永远匹配不上 → 「Current runtime context…」伪 user 行漏进 DOM 配对,
|
|
7
|
+
* 导致按序配对错位(实测 [8,9,54],应为 [8,54])。
|
|
8
|
+
*
|
|
9
|
+
* 修法:injected() 必须对**未去空白的原始 snippet** 判定。
|
|
10
|
+
*/
|
|
11
|
+
import test from 'node:test'
|
|
12
|
+
import assert from 'node:assert/strict'
|
|
13
|
+
|
|
14
|
+
// 与 client.js:957 保持一致的归一化
|
|
15
|
+
const norm = (x) => String(x || '').replace(/\s+/g, '')
|
|
16
|
+
|
|
17
|
+
/** 修复后的判定:吃 raw(原始 snippet),不依赖 norm */
|
|
18
|
+
export function injectedFixed(raw) {
|
|
19
|
+
return /^<system-reminder|current\s+runtime\s+context|^<system-Reminder/i.test(String(raw || ''))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 修复前的判定(回归对照:对 norm 后的串跑) */
|
|
23
|
+
function injectedBuggy(normalised) {
|
|
24
|
+
return /^<system-reminder|current\s+runtime\s+context|^<system-Reminder/i.test(String(normalised || ''))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
test('injectedFixed:Current runtime context(大小写混合+空格)被识别', () => {
|
|
28
|
+
assert.equal(injectedFixed('Current runtime context. This snapshot supersedes earlier'), true)
|
|
29
|
+
assert.equal(injectedFixed('current runtime context'), true)
|
|
30
|
+
assert.equal(injectedFixed('Current runtime context'), true)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('回归证明:对 norm 后的串判定必然失败(这正是原 bug)', () => {
|
|
34
|
+
const raw = 'Current runtime context. This snapshot supersedes earlier runtime-context snapshots.'
|
|
35
|
+
assert.equal(injectedBuggy(norm(raw)), false, 'norm 删掉空白后 \\s+ 永不匹配')
|
|
36
|
+
assert.equal(injectedFixed(raw), true)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('injectedFixed:<system-reminder> 前缀仍被识别', () => {
|
|
40
|
+
assert.equal(injectedFixed('<system-reminder> A skill is a reusable set of instructions.'), true)
|
|
41
|
+
assert.equal(injectedFixed('<system-Reminder> The following workspace instructions'), true)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('injectedFixed:真实用户消息不被误判', () => {
|
|
45
|
+
assert.equal(injectedFixed('继续,你把 mimo 的搞好'), false)
|
|
46
|
+
assert.equal(injectedFixed('基础价格按官方 api 价格来,缓存命中价格加上。'), false)
|
|
47
|
+
assert.equal(injectedFixed(''), false)
|
|
48
|
+
assert.equal(injectedFixed(null), false)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('fetchRows 行构造:保留 raw 供 injected 判定', () => {
|
|
52
|
+
// 模拟 client.js:969 的 map:既要 norm 后的 sn(配对用),也要 raw(判定用)
|
|
53
|
+
const msgs = [
|
|
54
|
+
{ seq: 8, role: 'user', snippet: '分配几个子代理把模型广场的模型价格' },
|
|
55
|
+
{ seq: 9, role: 'user', snippet: 'Current runtime context. This snapshot supersedes earlier runtime-context snapshots.' },
|
|
56
|
+
{ seq: 10, role: 'user', snippet: '<system-reminder> A skill is a reusable set of task-specific instructions.' },
|
|
57
|
+
{ seq: 54, role: 'user', snippet: '基础价格按官方api价格来,缓存命中价格加上。' },
|
|
58
|
+
]
|
|
59
|
+
const rows = msgs.map((m) => ({ seq: m.seq, sn: norm(m.snippet), raw: String(m.snippet ?? '') }))
|
|
60
|
+
.filter((r) => r.sn && !injectedFixed(r.raw))
|
|
61
|
+
assert.deepEqual(rows.map((r) => r.seq), [8, 54], '伪 user 行必须被滤掉')
|
|
62
|
+
})
|
|
@@ -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/dsh-seqguard/lib/seqcore.js'
|
|
19
|
+
import { officialValidate } from '/data/data/com.termux/files/home/dsh-plugins-src/dsh-seqguard/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'
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { planRestore, applyRestore, restoreProgress, pendingRestoreTurns } from "../src/ops-core.js";
|
|
4
|
+
|
|
5
|
+
// 构造:标记(20) 遮蔽 11..15;其中 11(user) 12(assistant) 13(user) 14(assistant) 15(tool)
|
|
6
|
+
function fixtures() {
|
|
7
|
+
const events = [];
|
|
8
|
+
const mk = (seq, type, text, extra) => Object.assign({
|
|
9
|
+
seq, type, time: 1700000000000 + seq,
|
|
10
|
+
data: { turn: 1, step: 1, message: { id: "m" + seq, role: type === "assistant/message" ? "assistant" : "user", source: { kind: "chat" }, content: [{ type: "text", text }] } },
|
|
11
|
+
surfaceOp: "append",
|
|
12
|
+
}, extra || {});
|
|
13
|
+
events.push(mk(11, "user/message", "U1"));
|
|
14
|
+
events.push(mk(12, "assistant/message", "A1"));
|
|
15
|
+
events.push(mk(13, "user/message", "U2"));
|
|
16
|
+
events.push(mk(14, "assistant/message", "A2"));
|
|
17
|
+
events.push({ seq: 15, type: "tool/result", time: 1700000000015, data: { turn: 1, step: 1, message: { id: "m15", role: "tool", source: { kind: "tool" }, content: [{ type: "text", text: "T" }] } }, surfaceOp: "append" });
|
|
18
|
+
events.push({
|
|
19
|
+
seq: 20, type: "system/message", time: 1700000000020,
|
|
20
|
+
data: { turn: 1, step: 1, message: { id: "mk20", role: "system", source: { kind: "system-prompt" }, content: [{ type: "text", text: "marker" }] } },
|
|
21
|
+
surfaceOp: { op: "replace", startSeq: 11, endSeq: 15 },
|
|
22
|
+
sourceEventSeqs: [11, 12, 13, 14, 15],
|
|
23
|
+
});
|
|
24
|
+
return events;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
test("0.8.0 planRestore(upToSeq) 只重放到该 seq(按轮步进)", () => {
|
|
28
|
+
const events = fixtures();
|
|
29
|
+
const full = planRestore(events, 20);
|
|
30
|
+
assert.deepEqual(full.replayable.map((i) => i.seq), [11, 12, 13, 14]);
|
|
31
|
+
const step1 = planRestore(events, 20, 12); // 只到第一轮结束
|
|
32
|
+
assert.deepEqual(step1.replayable.map((i) => i.seq), [11, 12], "upToSeq=12 → 只重放 11/12");
|
|
33
|
+
assert.equal(step1.upToSeq, 12);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("0.8.0 applyRestore 的 notice 带 restoredSourceSeqs", () => {
|
|
37
|
+
const events = fixtures();
|
|
38
|
+
const plan = planRestore(events, 20, 12);
|
|
39
|
+
const appended = [];
|
|
40
|
+
const fake = { append: (type, data) => { const e = { seq: 100 + appended.length, type, data }; appended.push(e); return e } };
|
|
41
|
+
const r = applyRestore(fake, plan, {});
|
|
42
|
+
assert.equal(r.restoredCount, 2);
|
|
43
|
+
assert.equal(appended[0].type, "system/message");
|
|
44
|
+
assert.equal(appended[0].data.restoresSeq, 20);
|
|
45
|
+
assert.deepEqual(appended[0].data.restoredSourceSeqs, [11, 12]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("0.8.0 restoreProgress:部分恢复后仍未完成,pending 为剩余轮", () => {
|
|
49
|
+
const events = fixtures();
|
|
50
|
+
const before = restoreProgress(events, 20);
|
|
51
|
+
assert.equal(before.complete, false, "无 notice → 未完成");
|
|
52
|
+
assert.deepEqual(before.pendingSeqs, [11, 12, 13, 14]);
|
|
53
|
+
|
|
54
|
+
// 加一条只恢复了 11/12 的 notice
|
|
55
|
+
events.push({
|
|
56
|
+
seq: 30, type: "system/message", time: 1700000000030,
|
|
57
|
+
data: { turn: 1, step: 1, message: { id: "m30", role: "system", source: { kind: "system-prompt" }, content: [{ type: "text", text: "notice" }] }, restoresSeq: 20, restoredSourceSeqs: [11, 12] },
|
|
58
|
+
surfaceOp: "append",
|
|
59
|
+
});
|
|
60
|
+
const mid = restoreProgress(events, 20);
|
|
61
|
+
assert.equal(mid.complete, false, "部分恢复 → 仍活跃");
|
|
62
|
+
assert.deepEqual(mid.restoredSeqs, [11, 12]);
|
|
63
|
+
assert.deepEqual(mid.pendingSeqs, [13, 14]);
|
|
64
|
+
const turns = pendingRestoreTurns(events, 20);
|
|
65
|
+
assert.deepEqual(turns, [{ turnSeq: 13, seqs: [13, 14] }], "剩余一轮:U2 + A2");
|
|
66
|
+
|
|
67
|
+
// 再补一条恢复 13/14
|
|
68
|
+
events.push({
|
|
69
|
+
seq: 31, type: "system/message", time: 1700000000031,
|
|
70
|
+
data: { turn: 1, step: 1, message: { id: "m31", role: "system", source: { kind: "system-prompt" }, content: [{ type: "text", text: "notice" }] }, restoresSeq: 20, restoredSourceSeqs: [13, 14] },
|
|
71
|
+
surfaceOp: "append",
|
|
72
|
+
});
|
|
73
|
+
const done = restoreProgress(events, 20);
|
|
74
|
+
assert.equal(done.complete, true, "全部重放完 → 标记不再活跃");
|
|
75
|
+
assert.deepEqual(pendingRestoreTurns(events, 20), []);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("0.8.0 兼容旧通知(无 restoredSourceSeqs)→ 视为整段已恢复", () => {
|
|
79
|
+
const events = fixtures();
|
|
80
|
+
events.push({
|
|
81
|
+
seq: 40, type: "system/message", time: 1700000000040,
|
|
82
|
+
data: { turn: 1, step: 1, message: { id: "m40", role: "system", source: { kind: "system-prompt" }, content: [{ type: "text", text: "legacy notice" }] }, restoresSeq: 20 },
|
|
83
|
+
surfaceOp: "append",
|
|
84
|
+
});
|
|
85
|
+
const p = restoreProgress(events, 20);
|
|
86
|
+
assert.equal(p.legacy, true);
|
|
87
|
+
assert.equal(p.complete, true, "旧通知 → 整段视为已恢复(向后兼容)");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("0.8.0 B1:planRestore(excludeSeqs) 排除已重放,杜绝副本刷屏", () => {
|
|
91
|
+
const events = fixtures();
|
|
92
|
+
const p = planRestore(events, 20, 14, [11, 12]);
|
|
93
|
+
assert.deepEqual(p.replayable.map((i) => i.seq), [13, 14], "已重放的 11/12 不再重放");
|
|
94
|
+
const all = planRestore(events, 20, 23, [11, 12, 13, 14]);
|
|
95
|
+
assert.deepEqual(all.replayable.map((i) => i.seq), [], "全部已重放 → 空计划(只补 notice)");
|
|
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,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
|
+
})
|