@240xu/dsh-message-ops 0.11.1 → 0.11.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 +59 -27
- package/package.json +1 -2
- package/src/client.js +1048 -1
- package/src/index.js +212 -25
- package/src/ops-core.js +148 -23
- package/src/session-file.js +79 -13
- package/src/session-trash.js +432 -0
- package/test/branch-export-removed.test.js +0 -73
- package/test/client-p0-locks.test.js +0 -131
- package/test/client-perf-locks.test.js +0 -259
- package/test/composer-refill.test.js +0 -106
- package/test/derived-cache.test.js +0 -202
- package/test/dock-preview.test.js +0 -666
- package/test/fence.test.js +0 -181
- package/test/injected-regex.test.js +0 -62
- package/test/legacy-format.test.js +0 -120
- package/test/ops-core.test.js +0 -358
- package/test/redo-contract.test.js +0 -55
- package/test/revert-commit.test.js +0 -98
- package/test/revert-contract.test.js +0 -130
- package/test/session-file.test.js +0 -86
- package/test/step-restore.test.js +0 -112
- package/test/surface-visibility.test.js +0 -166
- package/test/transcript-visibility.test.js +0 -111
- package/test/undo-slash.test.js +0 -89
- package/test/user-revert-anchor.test.js +0 -103
- package/test/v4-stream-contract.test.js +0 -62
package/test/ops-core.test.js
DELETED
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-message-ops — 新功能零依赖测试(node --test)。
|
|
3
|
-
* 覆盖:export Markdown、restore 重放规划/落定、message_ops 工具构造与
|
|
4
|
-
* 容错注册(缺 tools 服务 / 缺 dsh-tools 包均不 fatal)。
|
|
5
|
-
*/
|
|
6
|
-
import test from 'node:test'
|
|
7
|
-
import assert from 'node:assert/strict'
|
|
8
|
-
|
|
9
|
-
const { OpsError, planRestore, applyRestore, messageText } =
|
|
10
|
-
await import('../src/ops-core.js')
|
|
11
|
-
const { createMessageOpsTool, apply } = await import('../src/index.js')
|
|
12
|
-
|
|
13
|
-
function msg(type, seq, text, role) {
|
|
14
|
-
return {
|
|
15
|
-
type, seq, time: Date.now(), surfaceOp: 'append',
|
|
16
|
-
data: { turn: 1, message: { role: role || (type === 'user/message' ? 'user' : 'assistant'), content: [{ type: 'text', text }] } },
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function sampleEvents() {
|
|
21
|
-
return [
|
|
22
|
-
msg('user/message', 0, '第一问'),
|
|
23
|
-
msg('assistant/message', 1, '第一答'),
|
|
24
|
-
{ type: 'tool/call', seq: 2, data: { name: 'bash', call: { name: 'bash', arguments: { command: 'ls' } } } },
|
|
25
|
-
msg('user/message', 3, '第二问'),
|
|
26
|
-
msg('assistant/message', 4, '第二答'),
|
|
27
|
-
// 一次 revert 落定:遮蔽 3..4,sourceEventSeqs 覆盖被遮蔽节点
|
|
28
|
-
{
|
|
29
|
-
type: 'system/message', seq: 5, time: Date.now(),
|
|
30
|
-
surfaceOp: { op: 'replace', startSeq: 3, endSeq: 4 },
|
|
31
|
-
sourceEventSeqs: [3, 4],
|
|
32
|
-
data: { message: { role: 'system', content: [{ type: 'text', text: '[消息回滚] 已回滚到 seq 3(含)之后的 2 个节点' }] } },
|
|
33
|
-
},
|
|
34
|
-
]
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const header = { type: 'session', version: 3, id: 'session-aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', createdAt: 1, cwd: '/tmp/x' }
|
|
38
|
-
|
|
39
|
-
// --- exportMarkdown ----------------------------------------------------------
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
// --- planRestore / applyRestore ----------------------------------------------
|
|
45
|
-
|
|
46
|
-
test('planRestore:从 revert 标记事件规划重放(含 user/assistant,跳过 tool)', () => {
|
|
47
|
-
const plan = planRestore(sampleEvents(), 5)
|
|
48
|
-
assert.equal(plan.startSeq, 3)
|
|
49
|
-
assert.equal(plan.endSeq, 4)
|
|
50
|
-
assert.equal(plan.replayable.length, 2)
|
|
51
|
-
assert.equal(plan.skipped, 0)
|
|
52
|
-
assert.deepEqual(plan.replayable.map((r) => [r.role, r.seq, r.text]), [
|
|
53
|
-
['user', 3, '第二问'],
|
|
54
|
-
['assistant', 4, '第二答'],
|
|
55
|
-
])
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
test('planRestore:兼容 start/end 拼写(dsh-src 较新引擎形状)', () => {
|
|
59
|
-
const events = [
|
|
60
|
-
msg('user/message', 0, 'a'),
|
|
61
|
-
{ type: 'system/message', seq: 1, surfaceOp: { op: 'replace', start: 0, end: 0 }, sourceEventSeqs: [0] },
|
|
62
|
-
]
|
|
63
|
-
const plan = planRestore(events, 1)
|
|
64
|
-
assert.equal(plan.startSeq, 0)
|
|
65
|
-
assert.equal(plan.replayable[0].text, 'a')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
test('planRestore:非标记事件 / 越界 seq 拒绝;空可重放区间改为接受(0.5.6 停用语义)', () => {
|
|
69
|
-
const events = sampleEvents()
|
|
70
|
-
assert.throws(() => planRestore(events, 99), (e) => e instanceof OpsError && e.status === 404)
|
|
71
|
-
assert.throws(() => planRestore(events, 0), (e) => e instanceof OpsError && e.status === 409) // 普通消息非标记
|
|
72
|
-
assert.throws(() => planRestore(events, -1), (e) => e instanceof OpsError && e.status === 400)
|
|
73
|
-
// 0.5.6:区间内只有 tool 事件 → 不再 409,返回空可重放计划(停用语义)
|
|
74
|
-
const onlyTool = [
|
|
75
|
-
{ type: 'tool/call', seq: 0, data: { name: 'bash' } },
|
|
76
|
-
{ type: 'system/message', seq: 1, surfaceOp: { op: 'replace', startSeq: 0, endSeq: 0 }, sourceEventSeqs: [0] },
|
|
77
|
-
]
|
|
78
|
-
const plan = planRestore(onlyTool, 1)
|
|
79
|
-
assert.equal(plan.replayable.length, 0, '空可重放 = 接受为停用计划')
|
|
80
|
-
assert.equal(plan.startSeq, 0)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('applyRestore:重放 append 干净文本(0.6.0 无前缀)+ system 说明,flush 被调用', () => {
|
|
84
|
-
const appended = []
|
|
85
|
-
let seq = 10
|
|
86
|
-
const session = {
|
|
87
|
-
append(type, data, opts) {
|
|
88
|
-
const event = { seq: seq++, type, data, opts }
|
|
89
|
-
appended.push(event)
|
|
90
|
-
return event
|
|
91
|
-
},
|
|
92
|
-
}
|
|
93
|
-
const plan = planRestore(sampleEvents(), 5)
|
|
94
|
-
let flushed = 0
|
|
95
|
-
const result = applyRestore(session, plan, { flush: () => flushed++ })
|
|
96
|
-
assert.equal(flushed, 1)
|
|
97
|
-
assert.equal(result.restoredCount, 2)
|
|
98
|
-
assert.equal(result.skipped, 0)
|
|
99
|
-
assert.equal(appended.length, 3)
|
|
100
|
-
assert.equal(appended[0].type, 'system/message')
|
|
101
|
-
assert.match(appended[0].data.message.content[0].text, /\[消息恢复\] 重放 seq 3\.\.4 的 2 条消息/)
|
|
102
|
-
assert.equal(appended[1].type, 'user/message')
|
|
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')
|
|
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
|
-
}
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
test('messageText:数组 content 提取首个非空 text;data.content 兼容', () => {
|
|
115
|
-
assert.equal(messageText(msg('user/message', 0, 'hi')), 'hi')
|
|
116
|
-
assert.equal(messageText({ type: 'user/message', data: { content: [{ type: 'text', text: 'legacy' }] } }), 'legacy')
|
|
117
|
-
assert.equal(messageText({ type: 'user/message', data: {} }), '')
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
// --- message_ops 工具(桩 defineTool + 桩 ops) --------------------------------
|
|
121
|
-
|
|
122
|
-
function stubDefineTool(def) {
|
|
123
|
-
def.__compiled = true
|
|
124
|
-
return def
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
test('createMessageOpsTool:defineTool 桩拿到 name/parameters/execute,execute 分发到 ops', async () => {
|
|
128
|
-
const calls = []
|
|
129
|
-
const ops = {
|
|
130
|
-
list: (ctx, id) => { calls.push(['list', id]); return { ok: true, session: { id }, running: false, total: 0, visibleCount: 0, messages: [] } },
|
|
131
|
-
revert: (ctx, id, seq) => { calls.push(['revert', id, seq]); return { mode: 'revert', seq, shadowedCount: 2, eventSeq: 9 } },
|
|
132
|
-
}
|
|
133
|
-
const tool = createMessageOpsTool({ defineTool: stubDefineTool, ops, ctx: {} })
|
|
134
|
-
assert.equal(tool.name, 'message_ops')
|
|
135
|
-
assert.ok(tool.__compiled)
|
|
136
|
-
assert.equal(tool.parameters.action.enum.length, 4, '0.9.0:移除 branch/export(用官方能力)')
|
|
137
|
-
assert.ok(tool.parameters.sessionId.required)
|
|
138
|
-
const out = await tool.execute({ action: 'list', sessionId: 'session-x' })
|
|
139
|
-
assert.match(out, /session session-x/)
|
|
140
|
-
const out2 = await tool.execute({ action: 'revert', sessionId: 'session-x', seq: 3 })
|
|
141
|
-
assert.match(out2, /revert ok: shadowed 2 node\(s\) from seq 3; marker event seq 9/)
|
|
142
|
-
assert.deepEqual(calls[1], ['revert', 'session-x', 3])
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
test('createMessageOpsTool:execute 捕获 OpsError 为失败文本而非抛出', async () => {
|
|
146
|
-
const ops = { delete: () => { throw new OpsError('seq 3 not visible on current surface', 409) } }
|
|
147
|
-
const tool = createMessageOpsTool({ defineTool: stubDefineTool, ops, ctx: {} })
|
|
148
|
-
const out = await tool.execute({ action: 'delete', sessionId: 'session-x', seq: 3 })
|
|
149
|
-
assert.match(out, /^delete failed: seq 3 not visible/)
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
// --- index.js apply:工具注册容错(不 fatal) ----------------------------------
|
|
153
|
-
|
|
154
|
-
function mockCtx({ tools, webServer } = {}) {
|
|
155
|
-
const registrations = []
|
|
156
|
-
const injects = []
|
|
157
|
-
const host = { register: (route) => registrations.push(route) }
|
|
158
|
-
return {
|
|
159
|
-
registrations, injects, host,
|
|
160
|
-
get(key) {
|
|
161
|
-
if (key === 'webServer') return webServer === undefined ? undefined : webServer
|
|
162
|
-
if (key === 'tools') return tools
|
|
163
|
-
return undefined
|
|
164
|
-
},
|
|
165
|
-
inject(deps, fn) { injects.push([deps, fn]) },
|
|
166
|
-
effect(fn) { return fn() },
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
test('apply:无 webServer、无 tools 时走 inject 等待,不抛错', () => {
|
|
171
|
-
const ctx = mockCtx()
|
|
172
|
-
assert.doesNotThrow(() => apply(ctx))
|
|
173
|
-
assert.deepEqual(ctx.injects.map((i) => i[0]), [['webServer'], ['tools']])
|
|
174
|
-
assert.equal(ctx.registrations.length, 0)
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
test('apply:webServer 存在时注册全部 5 条路由;tools 缺失走 inject 等待', () => {
|
|
178
|
-
const ctx = mockCtx({ webServer: { register: (route) => ctx.registrations.push(route) } })
|
|
179
|
-
assert.doesNotThrow(() => apply(ctx))
|
|
180
|
-
const paths = ctx.registrations.map((r) => r.path).sort()
|
|
181
|
-
assert.deepEqual(paths, [
|
|
182
|
-
'/api/message-ops/delete',
|
|
183
|
-
'/api/message-ops/messages',
|
|
184
|
-
'/api/message-ops/restore',
|
|
185
|
-
'/api/message-ops/revert',
|
|
186
|
-
'/api/message-ops/text',
|
|
187
|
-
], '0.9.0:branch/export 路由已移除(改用官方 session/fork 与 /api/session.export)')
|
|
188
|
-
// tools 缺失 → 容错等待而非 fatal
|
|
189
|
-
assert.ok(ctx.injects.some(([deps]) => deps[0] === 'tools'))
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
test('apply:tools 服务存在时直接进入注册路径(包缺失时 catch 静默跳过)', () => {
|
|
193
|
-
const registered = []
|
|
194
|
-
const ctx = mockCtx({ webServer: { register: () => {} }, tools: { register: (t) => registered.push(t) } })
|
|
195
|
-
assert.doesNotThrow(() => apply(ctx))
|
|
196
|
-
// 动态 import('@deepseek-ai/dsh-tools') 在本测试环境不可解析 → 工具跳过,
|
|
197
|
-
// 但绝不能让插件 apply 抛错;异步分支也不能产生未处理拒绝。
|
|
198
|
-
assert.equal(registered.length, 0)
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
// ── 0.5.2 回归:工具层必须 await async ops(0.2.1 改 async 后曾漏同步化,
|
|
202
|
-
// list/restore 抛 TypeError、export 渲出空 text;旧桩是同步的故漏检)──────
|
|
203
|
-
test('工具 execute:async ops 被 await(list/revert/delete/restore 全路径)', async () => {
|
|
204
|
-
let def
|
|
205
|
-
const defineTool = (d) => { def = d; return d }
|
|
206
|
-
const asyncOps = {
|
|
207
|
-
list: async () => ({ session: { id: 'session-abc', parentSession: null }, running: false, total: 1, visibleCount: 1,
|
|
208
|
-
messages: [{ seq: 10, role: 'user', visible: true, snippet: 'hello' }] }),
|
|
209
|
-
revert: async () => ({ mode: 'revert', shadowedCount: 2, seq: 10, eventSeq: 99 }),
|
|
210
|
-
delete: async () => ({ mode: 'delete', shadowedCount: 1, seq: 10, eventSeq: 98 }),
|
|
211
|
-
restore: async () => ({ restoredCount: 1, skipped: 0, range: { startSeq: 5, endSeq: 6 }, eventSeqs: [7, 8] }),
|
|
212
|
-
}
|
|
213
|
-
createMessageOpsTool({ defineTool, ops: asyncOps, ctx: {} })
|
|
214
|
-
assert.ok(def, 'tool defined')
|
|
215
|
-
const outList = await def.execute({ action: 'list', sessionId: 'session-abc' })
|
|
216
|
-
assert.match(String(outList), /session session-abc/)
|
|
217
|
-
assert.doesNotMatch(String(outList), /failed:/)
|
|
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/)
|
|
222
|
-
const outRestore = await def.execute({ action: 'restore', sessionId: 'session-abc', seq: 6 })
|
|
223
|
-
assert.match(String(outRestore), /replayed 1 message/)
|
|
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
|
-
}
|
|
228
|
-
// 同步抛出的 ops 仍走错误文案路径
|
|
229
|
-
const errTool = await (async () => {
|
|
230
|
-
let d2
|
|
231
|
-
createMessageOpsTool({ defineTool: (x) => { d2 = x }, ops: { list: () => { throw new Error('boom') } }, ctx: {} })
|
|
232
|
-
return d2.execute({ action: 'list', sessionId: 'session-abc' })
|
|
233
|
-
})()
|
|
234
|
-
assert.match(String(errTool), /list failed: boom/)
|
|
235
|
-
})
|
|
236
|
-
|
|
237
|
-
// ── 0.5.4 P0 回归:system/message 必须带正 turn/step(0.2.0 v4 准入收紧,
|
|
238
|
-
// 缺字段 → 持久层 encodeEventBatch 未捕获 → **整个 dsh 进程 exit=1**)────
|
|
239
|
-
test('applySurfaceReplace/applyRestore 附带正整数 turn/step(v4 准入)', async () => {
|
|
240
|
-
const { applySurfaceReplace, applyRestore, deriveTurnStep } = await import('../src/ops-core.js');
|
|
241
|
-
const appended = [];
|
|
242
|
-
const fakeSession = { append: (type, data, opts) => { appended.push({ type, data, opts }); return { seq: 999 }; } };
|
|
243
|
-
// 1) deriveTurnStep:尾部回溯 / 兜底
|
|
244
|
-
assert.deepEqual(deriveTurnStep([{ data: { turn: 7, step: 3 } }]), { turn: 7, step: 3 });
|
|
245
|
-
assert.deepEqual(deriveTurnStep([{ data: {} }, { data: { turn: 2, step: 5 } }]), { turn: 2, step: 5 });
|
|
246
|
-
assert.deepEqual(deriveTurnStep([]), { turn: 1, step: 1 });
|
|
247
|
-
assert.deepEqual(deriveTurnStep(undefined), { turn: 1, step: 1 });
|
|
248
|
-
// 2) revert/delete 通知
|
|
249
|
-
applySurfaceReplace(fakeSession, 10, 20, [10], '[消息回滚] x', { turn: 9, step: 4 });
|
|
250
|
-
assert.equal(appended[0].type, 'system/message');
|
|
251
|
-
assert.ok(appended[0].data.turn > 0 && Number.isInteger(appended[0].data.turn), 'data.turn 正整数');
|
|
252
|
-
assert.ok(appended[0].data.step > 0 && Number.isInteger(appended[0].data.step), 'data.step 正整数');
|
|
253
|
-
assert.ok(typeof appended[0].data.message.id === 'string' && appended[0].data.message.id.length > 0, 'message.id 非空字符串(v4 准入第 3 关)');
|
|
254
|
-
// 无坐标参数时兜底 1/1(调用方漏传不致崩)
|
|
255
|
-
applySurfaceReplace(fakeSession, 10, 20, [10], 'x2');
|
|
256
|
-
assert.ok(appended[1].data.turn >= 1 && appended[1].data.step >= 1);
|
|
257
|
-
// 3) restore 通知(plan.turnStep)
|
|
258
|
-
applyRestore(fakeSession, {
|
|
259
|
-
restoreSeq: 5, startSeq: 1, endSeq: 3, skipped: 0, turnStep: { turn: 12, step: 6 },
|
|
260
|
-
replayable: [{ type: 'user/message', role: 'user', text: 'hi' }],
|
|
261
|
-
});
|
|
262
|
-
const notice = appended[2];
|
|
263
|
-
assert.equal(notice.type, 'system/message');
|
|
264
|
-
assert.equal(notice.data.turn, 12);
|
|
265
|
-
assert.equal(notice.data.step, 6);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
// 0.5.6 回归:空可重放区间 → 优雅停用而非 409(dock 死按钮修复)
|
|
269
|
-
test('planRestore 空可重放区间返回空计划;applyRestore 发停用 notice', async () => {
|
|
270
|
-
const { planRestore, applyRestore } = await import('../src/ops-core.js');
|
|
271
|
-
// 事件流:startSeq..endSeq 内只有一条 tool 事件(非 user/assistant)
|
|
272
|
-
const events = [
|
|
273
|
-
{ seq: 1, type: 'system/message', data: { turn: 1, step: 1, message: {} } },
|
|
274
|
-
{ seq: 8, type: 'tool/result', data: { message: {} } },
|
|
275
|
-
{ seq: 9, type: 'system/message', data: { turn: 1, step: 2, message: {} },
|
|
276
|
-
surfaceOp: { op: 'replace', startSeq: 8, endSeq: 8 } },
|
|
277
|
-
];
|
|
278
|
-
const plan = planRestore(events, 9);
|
|
279
|
-
assert.equal(plan.replayable.length, 0, '无可重放');
|
|
280
|
-
assert.equal(plan.startSeq, 8);
|
|
281
|
-
const appended = [];
|
|
282
|
-
const fakeSession = { append: (type, data, opts) => { appended.push({ type, data, opts }); return { seq: 42 }; } };
|
|
283
|
-
const out = applyRestore(fakeSession, plan);
|
|
284
|
-
assert.equal(out.restoredCount, 0);
|
|
285
|
-
// notice 停用 + restoresSeq 指向标记(dock 由此移除该行)
|
|
286
|
-
assert.equal(appended.length, 1, '只发 notice');
|
|
287
|
-
assert.equal(appended[0].data.restoresSeq, 9);
|
|
288
|
-
assert.match(appended[0].data.message.content[0].text, /无可重放/);
|
|
289
|
-
assert.ok(appended[0].data.turn > 0 && appended[0].data.message.id, 'v4 准入字段齐');
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
// ── 0.5.7 磁盘路径回归:字段镜像引擎金标准 + 计划语义一致 ──────────────────
|
|
293
|
-
test('磁盘计划/构建器:可见节点剔除遮蔽、marker 字段逐项镜像金标准', async () => {
|
|
294
|
-
const core = await import('../src/ops-core.js');
|
|
295
|
-
// 事件流:user10 → assistant11 → step/end12(非节点)→ 既有标记 13 遮蔽 11
|
|
296
|
-
const events = [
|
|
297
|
-
{ type: 'user/message', seq: 10, data: { message: { role: 'user', content: [] } } },
|
|
298
|
-
{ type: 'assistant/message', seq: 11, data: { message: { role: 'assistant', content: [] } } },
|
|
299
|
-
{ type: 'step/end', seq: 12, data: {} },
|
|
300
|
-
{ type: 'system/message', seq: 13, data: { turn: 1, step: 1, message: {} },
|
|
301
|
-
surfaceOp: { op: 'replace', startSeq: 11, endSeq: 11 }, sourceEventSeqs: [11] },
|
|
302
|
-
{ type: 'user/message', seq: 14, data: { message: { role: 'user', content: [] } } },
|
|
303
|
-
];
|
|
304
|
-
// 可见节点 = [10, 14](11 已遮蔽;12 非 message;13 是标记本身也是 system 可见)
|
|
305
|
-
const nodes = core.diskVisibleNodes(events);
|
|
306
|
-
assert.deepEqual(nodes, [10, 13, 14], '剔除遮蔽 + 只取 message 类: ' + JSON.stringify(nodes));
|
|
307
|
-
// revert at 14 → 遮蔽 [14](末尾)
|
|
308
|
-
const plan = core.planRevertFromEvents(events, 14);
|
|
309
|
-
assert.deepEqual(plan.shadowedSeqs, [14]);
|
|
310
|
-
assert.equal(plan.endSeq, 14);
|
|
311
|
-
// revert at 10 → 遮蔽 [10, 13, 14]
|
|
312
|
-
const plan2 = core.planRevertFromEvents(events, 10);
|
|
313
|
-
assert.deepEqual(plan2.shadowedSeqs, [10, 13, 14]);
|
|
314
|
-
// 不可见 seq → 409
|
|
315
|
-
assert.throws(() => core.planRevertFromEvents(events, 11), (e) => e.status === 409);
|
|
316
|
-
// nextSeq
|
|
317
|
-
assert.equal(core.nextSeqFrom(events), 15);
|
|
318
|
-
// marker 构建器字段镜像(对照引擎金标准形状)
|
|
319
|
-
const ev = core.buildMarkerEvent({
|
|
320
|
-
seq: 15, time: 1791105740958, turnStep: { turn: 9, step: 6 }, text: 'x',
|
|
321
|
-
startSeq: 14, endSeq: 14, shadowedSeqs: [14],
|
|
322
|
-
});
|
|
323
|
-
assert.deepEqual(Object.keys(ev), ['type', 'seq', 'time', 'data', 'sourceEventSeqs', 'surfaceOp'], '根键序镜像');
|
|
324
|
-
assert.equal(ev.type, 'system/message');
|
|
325
|
-
assert.equal(ev.data.turn, 9);
|
|
326
|
-
assert.equal(ev.data.step, 6);
|
|
327
|
-
assert.ok(ev.data.message.id.length > 0);
|
|
328
|
-
assert.equal(ev.data.message.role, 'system');
|
|
329
|
-
assert.deepEqual(ev.data.message.content[0], { type: 'text', text: 'x' });
|
|
330
|
-
assert.deepEqual(ev.sourceEventSeqs, [14]);
|
|
331
|
-
assert.deepEqual(ev.surfaceOp, { op: 'replace', startSeq: 14, endSeq: 14 });
|
|
332
|
-
assert.equal(Object.keys(ev.surfaceOp).length, 3, 'replace op 恰好 3 键(引擎 isReplaceOp)');
|
|
333
|
-
// restore notice + replay 镜像
|
|
334
|
-
const nt = core.buildRestoreNoticeEvent({ seq: 16, time: 2, turnStep: { turn: 9, step: 6 }, text: 'n', restoreSeq: 13 });
|
|
335
|
-
assert.equal(nt.surfaceOp, 'append');
|
|
336
|
-
assert.equal(nt.data.restoresSeq, 13);
|
|
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)
|
|
358
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 0.9.5 /redo 的 seq 契约 —— 回归「传了消息 seq 导致空转」。
|
|
3
|
-
*
|
|
4
|
-
* `POST /api/message-ops/restore` 的 seq 是 **marker seq**:
|
|
5
|
-
* opsRestore(targetCtx, sessionId, restoreSeq, ...)
|
|
6
|
-
* → restoreProgress(events, restoreSeq) / planRestore(events, restoreSeq, ...)
|
|
7
|
-
* dock 传的 row.seq 来自 activeMarkers(marker 行)。
|
|
8
|
-
*
|
|
9
|
-
* 0.9.4 之前 /redo 传的是 lastRevertable() —— 那是**用户消息** seq,
|
|
10
|
-
* planRestore 找不到对应 marker → 计划为空 → 恢复不发生。
|
|
11
|
-
*/
|
|
12
|
-
import test from 'node:test'
|
|
13
|
-
import assert from 'node:assert/strict'
|
|
14
|
-
|
|
15
|
-
const activeMarkers = (msgs) => (msgs || []).filter(
|
|
16
|
-
(m) => m.marker && m.sourceKind !== 'compact-checkpoint' && m.restoreComplete !== true,
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
const FIXTURE = [
|
|
20
|
-
{ seq: 8, type: 'user/message', marker: false, visible: true },
|
|
21
|
-
{ seq: 63, type: 'system/message', marker: true, range: { start: 54, end: 54 }, restoreComplete: true },
|
|
22
|
-
{ seq: 76, type: 'system/message', marker: true, range: { start: 70, end: 70 }, restoreComplete: false },
|
|
23
|
-
{ seq: 90, type: 'user/message', marker: false, visible: false },
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
test('activeMarkers 只留未恢复、且非 compact-checkpoint 的 marker', () => {
|
|
27
|
-
const ms = activeMarkers(FIXTURE)
|
|
28
|
-
assert.equal(ms.length, 1)
|
|
29
|
-
assert.equal(ms[0].seq, 76)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test('/redo 必须传 marker seq,不是消息 seq', () => {
|
|
33
|
-
const markers = activeMarkers(FIXTURE)
|
|
34
|
-
const redoTarget = markers[markers.length - 1]
|
|
35
|
-
assert.equal(redoTarget.seq, 76, 'redo 目标 = 最新未恢复 marker')
|
|
36
|
-
// 反例:旧实现取最后一条用户消息
|
|
37
|
-
const messageTarget = FIXTURE.filter((m) => m.type === 'user/message').slice(-1)[0]
|
|
38
|
-
assert.notEqual(messageTarget.seq, redoTarget.seq,
|
|
39
|
-
'消息 seq 与 marker seq 不同 —— 混用会让 planRestore 找不到 marker')
|
|
40
|
-
assert.equal(redoTarget.marker, true, 'redo 目标必须是 marker')
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
test('全部恢复完毕 → 不发请求,给出无事可做的提示', () => {
|
|
44
|
-
const done = FIXTURE.map((m) => ({ ...m, restoreComplete: true }))
|
|
45
|
-
assert.equal(activeMarkers(done).length, 0, 'activeMarkers 为空 → 应短路')
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
test('/undo 与 /redo 的目标集合互不重叠', () => {
|
|
49
|
-
// undo 取 surface 上最后一条用户消息;redo 取最新未恢复 marker
|
|
50
|
-
const undoTarget = FIXTURE.filter((m) => m.type === 'user/message' && m.visible).slice(-1)[0]
|
|
51
|
-
const redoTarget = activeMarkers(FIXTURE).slice(-1)[0]
|
|
52
|
-
assert.equal(undoTarget.seq, 8)
|
|
53
|
-
assert.equal(redoTarget.seq, 76)
|
|
54
|
-
assert.notEqual(undoTarget.seq, redoTarget.seq)
|
|
55
|
-
})
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 0.10.1 revert 的 commit 生命周期(对齐 opencode)。
|
|
3
|
-
*
|
|
4
|
-
* 用户反馈:「我把消息改完发出去了,恢复弹窗仍然存在,我不知道它让我恢复到哪去」。
|
|
5
|
-
*
|
|
6
|
-
* opencode 源码(anomalyco/opencode @ dev)核对结论:
|
|
7
|
-
* core/src/session/session.ts:165
|
|
8
|
-
* // Commit a staged revert only after preparation succeeds, before admitting new work.
|
|
9
|
-
* if (session.revert) yield* SessionRevert.commit(bus, session)
|
|
10
|
-
* → 用户发出下一条消息时**自动 commit**
|
|
11
|
-
* core/src/session/projector.ts:759 → `DELETE WHERE seq >= boundary.seq` + `revert: null`
|
|
12
|
-
* app/.../use-session-commands.tsx:287
|
|
13
|
-
* disabled: !actions.session.data.revertMessageID()
|
|
14
|
-
* → opencode **没有常驻弹窗**,只有会灰掉的 undo/redo 命令;状态清零 = affordance 消失
|
|
15
|
-
*
|
|
16
|
-
* 我们是 append-only 日志,marker 不会真消失 → 由 markCommitted 自算。
|
|
17
|
-
*/
|
|
18
|
-
import test from 'node:test'
|
|
19
|
-
import assert from 'node:assert/strict'
|
|
20
|
-
import { markCommitted } from '../src/session-file.js'
|
|
21
|
-
|
|
22
|
-
const marker = (seq, extra = {}) => ({ seq, type: 'system/message', marker: true, ...extra })
|
|
23
|
-
const realUser = (seq) => ({ seq, type: 'user/message', sourceKind: 'user' })
|
|
24
|
-
const kinded = (seq, sourceKind) => ({ seq, type: 'user/message', sourceKind })
|
|
25
|
-
|
|
26
|
-
test('未发新消息 → marker 未提交(dock 应该显示)', () => {
|
|
27
|
-
const msgs = [realUser(8), marker(63)]
|
|
28
|
-
markCommitted(msgs)
|
|
29
|
-
assert.equal(msgs[1].committed, false)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
test('发出下一条真实消息 → marker 已提交(dock 应消失)', () => {
|
|
33
|
-
const msgs = [realUser(8), marker(63), realUser(70)]
|
|
34
|
-
markCommitted(msgs)
|
|
35
|
-
assert.equal(msgs[1].committed, true, '这是用户反馈的核心场景')
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
test('注入行不算提交(否则 background job 落地就会让弹窗自己消失)', () => {
|
|
39
|
-
const injectedKinds = ['tool-jobs', 'agent-instructions', 'runtime-context',
|
|
40
|
-
'skill-catalog', 'subagent-settled', 'model-selection', 'team-message',
|
|
41
|
-
'goal', 'agent-message', 'compact-checkpoint', 'user-approval']
|
|
42
|
-
for (const k of injectedKinds) {
|
|
43
|
-
const msgs = [realUser(8), marker(63), kinded(70, k)]
|
|
44
|
-
markCommitted(msgs)
|
|
45
|
-
assert.equal(msgs[1].committed, false, `${k} 不应触发提交`)
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test('restore 重放副本不算提交(否则恢复一次弹窗就没了)', () => {
|
|
50
|
-
const msgs = [realUser(8), marker(63), kinded(70, 'plugin:@240xu/dsh-message-ops')]
|
|
51
|
-
markCommitted(msgs)
|
|
52
|
-
assert.equal(msgs[1].committed, false)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('边界之前的真实消息不影响判定', () => {
|
|
56
|
-
const msgs = [realUser(8), realUser(30), marker(63)]
|
|
57
|
-
markCommitted(msgs)
|
|
58
|
-
assert.equal(msgs[2].committed, false, '边界之前的真实消息不该提交它')
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
test('多个 marker 各自独立判定', () => {
|
|
62
|
-
const msgs = [realUser(8), marker(63), realUser(70), marker(80)]
|
|
63
|
-
markCommitted(msgs)
|
|
64
|
-
assert.equal(msgs[1].committed, true, '63 之后有真实消息 → 已提交')
|
|
65
|
-
assert.equal(msgs[3].committed, false, '80 之后没有 → 仍可恢复')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
test('非 marker 行不被打上 committed(避免污染普通消息)', () => {
|
|
69
|
-
const msgs = [realUser(8), marker(63), realUser(70)]
|
|
70
|
-
markCommitted(msgs)
|
|
71
|
-
assert.ok(!('committed' in msgs[0]))
|
|
72
|
-
assert.ok(!('committed' in msgs[2]))
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('activeMarkers 过滤条件(与 client.js 同构)', () => {
|
|
76
|
-
const active = (msgs) => (msgs || []).filter((m) =>
|
|
77
|
-
m.marker && m.sourceKind !== 'compact-checkpoint'
|
|
78
|
-
&& m.restoreComplete !== true && m.committed !== true)
|
|
79
|
-
const msgs = [
|
|
80
|
-
{ seq: 63, marker: true, committed: false, restoreComplete: false }, // 活跃
|
|
81
|
-
{ seq: 70, marker: true, committed: true, restoreComplete: false }, // 已提交 → 排除
|
|
82
|
-
{ seq: 80, marker: true, committed: false, restoreComplete: true }, // 已恢复 → 排除
|
|
83
|
-
{ seq: 90, marker: true, sourceKind: 'compact-checkpoint', committed: false, restoreComplete: false },
|
|
84
|
-
]
|
|
85
|
-
const out = active(msgs)
|
|
86
|
-
assert.equal(out.length, 1)
|
|
87
|
-
assert.equal(out[0].seq, 63)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
test('源码锁:activeMarkers 必须排 committed', async () => {
|
|
91
|
-
const { readFileSync } = await import('node:fs')
|
|
92
|
-
const { fileURLToPath } = await import('node:url')
|
|
93
|
-
const { dirname, join } = await import('node:path')
|
|
94
|
-
const src = readFileSync(join(dirname(fileURLToPath(import.meta.url)), '..', 'src', 'client.js'), 'utf8')
|
|
95
|
-
const m = src.match(/function activeMarkers[\s\S]{0,2000}?\n {4}\}/)
|
|
96
|
-
assert.ok(m, '找不到 activeMarkers')
|
|
97
|
-
assert.ok(/committed !== true/.test(m[0]), 'activeMarkers 未排除已提交的 marker —— 弹窗会常驻(用户报告的 bug)')
|
|
98
|
-
})
|
|
@@ -1,130 +0,0 @@
|
|
|
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
|
-
// Optional local contract fixtures. CI does not ship sessionfix; these tests
|
|
19
|
-
// are skipped there rather than hard-failing on a machine-specific path.
|
|
20
|
-
const SEQ_IMPL = process.env.SESSIONFIX_SEQCORE ?? '/data/data/com.termux/files/home/dsh-plugins-src/sessionfix/lib/seqcore.js'
|
|
21
|
-
const GATE_IMPL = process.env.SESSIONFIX_V4GATE ?? '/data/data/com.termux/files/home/dsh-plugins-src/sessionfix/lib/v4gate.mjs'
|
|
22
|
-
let decodeFrames, parseRows, officialValidate
|
|
23
|
-
try {
|
|
24
|
-
({ decodeFrames, parseRows } = await import(SEQ_IMPL))
|
|
25
|
-
;({ officialValidate } = await import(GATE_IMPL))
|
|
26
|
-
} catch {
|
|
27
|
-
decodeFrames = parseRows = officialValidate = undefined
|
|
28
|
-
}
|
|
29
|
-
const contractSkip = process.env.SESSIONFIX_CONTRACT !== '1'
|
|
30
|
-
? 'optional sessionfix contract tests; set SESSIONFIX_CONTRACT=1 to run'
|
|
31
|
-
: (decodeFrames === undefined ? 'sessionfix contract fixtures not available' : false)
|
|
32
|
-
import {
|
|
33
|
-
planNoticeWindow, noticeWindowPreamble, noticeWindowPostamble,
|
|
34
|
-
buildMarkerEvent, buildRestoreNoticeEvent, buildReplayEvent, planRestore, restoreProgress,
|
|
35
|
-
} from '../src/ops-core.js'
|
|
36
|
-
|
|
37
|
-
const HEADER = { type: 'session', version: 4, id: 'contract-test', createdAt: 1, isSeeded: false, delegationDepth: 0 }
|
|
38
|
-
|
|
39
|
-
const pack = (events) => zstdCompressSync(Buffer.from([JSON.stringify(HEADER), ...events.map((e) => JSON.stringify(e))].join('\n') + '\n'))
|
|
40
|
-
|
|
41
|
-
/** 空闲会话骨架:turn 1 已闭合,turn 2 是合成的 notice 窗口 */
|
|
42
|
-
function idleSessionBase() {
|
|
43
|
-
return [
|
|
44
|
-
{ type: 'turn/start', data: { turn: 1 } },
|
|
45
|
-
{ type: 'step/start', data: { turn: 1, step: 1 } },
|
|
46
|
-
{ type: 'system/message', data: { turn: 1, step: 1, message: { id: 'sys0', role: 'system', source: { kind: 'system-prompt' }, content: [{ type: 'text', text: 'sys' }] } }, surfaceOp: 'append' },
|
|
47
|
-
{ type: 'user/message', data: { id: 'u1', role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: '问题一' }] }, surfaceOp: 'append' },
|
|
48
|
-
{ type: 'assistant/message', data: { turn: 1, step: 1, message: { id: 'a1', role: 'assistant', source: { kind: 'assistant' }, content: [{ type: 'text', text: '答一' }] } }, surfaceOp: 'append' },
|
|
49
|
-
{ type: 'step/end', data: { turn: 1, step: 1 } },
|
|
50
|
-
{ type: 'turn/end', data: { turn: 1, reason: { kind: 'completed' } } },
|
|
51
|
-
].map((e, i) => ({ ...e, seq: i, time: 1000 + i }))
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
test('契约:合成窗口承载的 revert marker 通过官方三层闸', { skip: contractSkip }, () => {
|
|
55
|
-
const base = idleSessionBase()
|
|
56
|
-
const win = planNoticeWindow(base)
|
|
57
|
-
assert.equal(win.synthetic, true, '空闲会话应规划合成窗口')
|
|
58
|
-
assert.equal(win.turn, 2, '合成 turn 应为 maxTurn+1')
|
|
59
|
-
|
|
60
|
-
let cursor = base.length, t = 5000
|
|
61
|
-
const events = [...base]
|
|
62
|
-
for (const pre of noticeWindowPreamble(win)) events.push({ type: pre.kind, seq: cursor++, time: t++, data: pre.data })
|
|
63
|
-
events.push(buildMarkerEvent({
|
|
64
|
-
seq: cursor++, time: t++, turnStep: { turn: win.turn, step: win.step },
|
|
65
|
-
text: '[消息回滚] 测试', version: 4, startSeq: 3, endSeq: 4, shadowedSeqs: [3, 4],
|
|
66
|
-
}))
|
|
67
|
-
for (const post of noticeWindowPostamble(win)) events.push({ type: post.kind, seq: cursor++, time: t++, data: post.data })
|
|
68
|
-
|
|
69
|
-
const v = officialValidate(pack(events))
|
|
70
|
-
assert.ok(v.ok, `官方三层闸拒绝:${v.error}`)
|
|
71
|
-
assert.equal(v.events, events.length)
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
test('契约:合成窗口必须自带 reason(UI 层硬要求)', () => {
|
|
75
|
-
const win = { synthetic: true, turn: 9, step: 1 }
|
|
76
|
-
const post = noticeWindowPostamble(win)
|
|
77
|
-
const turnEnd = post.find((e) => e.kind === 'turn/end')
|
|
78
|
-
assert.ok(turnEnd, '缺少 turn/end 后缀')
|
|
79
|
-
assert.ok(turnEnd.data.reason, 'turn/end 缺 data.reason → UI 装配器崩')
|
|
80
|
-
assert.equal(turnEnd.data.reason.kind, 'interrupted')
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('契约:缺 reason 的 turn/end 会被官方闸拦下(回归证明这条规则有效)', { skip: contractSkip }, () => {
|
|
84
|
-
const base = idleSessionBase()
|
|
85
|
-
const bad = [...base, { type: 'turn/start', seq: base.length, time: 6000, data: { turn: 2 } },
|
|
86
|
-
{ type: 'step/start', seq: base.length + 1, time: 6001, data: { turn: 2, step: 1 } },
|
|
87
|
-
{ 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' },
|
|
88
|
-
{ type: 'step/end', seq: base.length + 3, time: 6003, data: { turn: 2, step: 1 } },
|
|
89
|
-
{ type: 'turn/end', seq: base.length + 4, time: 6004, data: { turn: 2 } }] // ← 缺 reason
|
|
90
|
-
const v = officialValidate(pack(bad))
|
|
91
|
-
assert.equal(v.ok, false, '缺 reason 竟然通过了 —— UI 层规则失效')
|
|
92
|
-
assert.match(v.error, /reason/, `错误信息应指向 reason,实际:${v.error}`)
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
test('契约:完整 restore 链路(notice + 重放)通过官方三层闸', { skip: contractSkip }, () => {
|
|
96
|
-
const base = idleSessionBase()
|
|
97
|
-
let cursor = base.length, t = 5000
|
|
98
|
-
const win = planNoticeWindow(base)
|
|
99
|
-
const afterRevert = [...base]
|
|
100
|
-
for (const pre of noticeWindowPreamble(win)) afterRevert.push({ type: pre.kind, seq: cursor++, time: t++, data: pre.data })
|
|
101
|
-
const marker = buildMarkerEvent({ seq: cursor++, time: t++, turnStep: { turn: win.turn, step: win.step }, text: '[消息回滚]', version: 4, startSeq: 3, endSeq: 4, shadowedSeqs: [3, 4] })
|
|
102
|
-
afterRevert.push(marker)
|
|
103
|
-
for (const post of noticeWindowPostamble(win)) afterRevert.push({ type: post.kind, seq: cursor++, time: t++, data: post.data })
|
|
104
|
-
assert.ok(officialValidate(pack(afterRevert)).ok, 'revert 产物不合法')
|
|
105
|
-
|
|
106
|
-
// restore:重新规划窗口(上一个已被占用)
|
|
107
|
-
const win2 = planNoticeWindow(afterRevert)
|
|
108
|
-
const plan = planRestore(afterRevert, marker.seq, undefined, restoreProgress(afterRevert, marker.seq)?.restoredSeqs ?? null)
|
|
109
|
-
plan.turnStep = { turn: win2.turn, step: win2.step }
|
|
110
|
-
const afterRestore = [...afterRevert]
|
|
111
|
-
for (const pre of noticeWindowPreamble(win2)) afterRestore.push({ type: pre.kind, seq: cursor++, time: t++, data: pre.data })
|
|
112
|
-
afterRestore.push(buildRestoreNoticeEvent({ seq: cursor++, time: t++, turnStep: plan.turnStep, version: 4, text: '[消息恢复]', restoreSeq: plan.restoreSeq }))
|
|
113
|
-
let off = 0
|
|
114
|
-
for (const item of plan.replayable) afterRestore.push(buildReplayEvent({ seq: cursor++, time: t + (++off), item, turnStep: plan.turnStep, version: 4 }))
|
|
115
|
-
for (const post of noticeWindowPostamble(win2)) afterRestore.push({ type: post.kind, seq: cursor++, time: t + off + (off++), data: post.data })
|
|
116
|
-
|
|
117
|
-
const v = officialValidate(pack(afterRestore))
|
|
118
|
-
assert.ok(v.ok, `restore 产物被官方闸拒绝:${v.error}`)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
test('契约:buildReplayEvent 缺 turnStep 必须抛错(绝不静默落 1/1)', () => {
|
|
122
|
-
assert.throws(
|
|
123
|
-
() => buildReplayEvent({ seq: 1, time: 1, item: { type: 'assistant/message', role: 'assistant', text: 'z' } }),
|
|
124
|
-
(e) => e.status === 500,
|
|
125
|
-
)
|
|
126
|
-
assert.throws(
|
|
127
|
-
() => buildReplayEvent({ seq: 1, time: 1, turnStep: { turn: 0, step: 1 }, item: { type: 'assistant/message', role: 'assistant', text: 'z' } }),
|
|
128
|
-
(e) => e.status === 500,
|
|
129
|
-
)
|
|
130
|
-
})
|