@240xu/dsh-message-ops 0.11.0 → 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 +39 -8
- package/package.json +1 -2
- package/src/client.js +468 -66
- package/src/index.js +30 -19
- package/src/ops-core.js +148 -23
- package/src/session-file.js +79 -13
- package/src/session-trash.js +38 -8
- 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 -363
- package/test/ops-search.test.js +0 -104
- package/test/redo-contract.test.js +0 -55
- package/test/revert-commit.test.js +0 -98
- package/test/revert-contract.test.js +0 -118
- package/test/session-file.test.js +0 -86
- package/test/session-trash.test.js +0 -179
- 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
|
@@ -1,118 +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
|
-
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
|
-
})
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-message-ops — 零依赖测试(node --test)。
|
|
3
|
-
* 覆盖:帧扫描/读写往返、消息列表、遮蔽推导、分支规划与落盘(临时目录)。
|
|
4
|
-
*/
|
|
5
|
-
import test from 'node:test'
|
|
6
|
-
import assert from 'node:assert/strict'
|
|
7
|
-
import fs from 'node:fs'
|
|
8
|
-
import os from 'node:os'
|
|
9
|
-
import path from 'node:path'
|
|
10
|
-
|
|
11
|
-
process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-msgops-'))
|
|
12
|
-
|
|
13
|
-
const { scanZstdFrames, readSessionFile, encodeSessionFile, findSessionDirs, listMessages, computeShadowed } =
|
|
14
|
-
await import('../src/session-file.js')
|
|
15
|
-
|
|
16
|
-
const MAGIC = 0xfd2fb528
|
|
17
|
-
|
|
18
|
-
function header(id) {
|
|
19
|
-
return { type: 'session', version: 3, id, createdAt: Date.now(), cwd: '/tmp/x', delegationDepth: 0 }
|
|
20
|
-
}
|
|
21
|
-
function msg(type, seq, text) {
|
|
22
|
-
return { type, seq, time: Date.now(), surfaceOp: 'append', data: { turn: 1, step: 1, message: { role: type === 'user/message' ? 'user' : 'assistant', content: [{ type: 'text', text }] } } }
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function writeLog(dir, header, events) {
|
|
26
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
27
|
-
const p = path.join(dir, 'session.v3.jsonl.zstd')
|
|
28
|
-
fs.writeFileSync(p, encodeSessionFile(header, events))
|
|
29
|
-
return p
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
test('zstd 帧扫描定位每个魔数边界', () => {
|
|
33
|
-
const a = encodeSessionFile(header('session-00000000-0000-0000-0000-000000000001'), [msg('user/message', 0, 'hi')])
|
|
34
|
-
const b = encodeSessionFile(header('session-00000000-0000-0000-0000-000000000002'), [msg('user/message', 0, 'yo')])
|
|
35
|
-
const buf = Buffer.concat([a, b])
|
|
36
|
-
const { frames } = scanZstdFrames(buf)
|
|
37
|
-
assert.equal(frames.length, 4) // 2 files × (header frame + event frame)
|
|
38
|
-
for (const f of frames) assert.equal(buf.readUInt32LE(f.start), MAGIC)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
test('读写往返:header 与事件完整恢复', () => {
|
|
42
|
-
const events = [
|
|
43
|
-
msg('user/message', 0, '你好'),
|
|
44
|
-
msg('assistant/message', 1, '你好!有什么可以帮你?'),
|
|
45
|
-
{ type: 'tool/call', seq: 2, data: { name: 'bash' } },
|
|
46
|
-
]
|
|
47
|
-
const p = writeLog(path.join(process.env.DSH_HOME, 'sessions', 'proj-a', 'session-00000000-0000-0000-0000-000000000003'), header('session-00000000-0000-0000-0000-000000000003'), events)
|
|
48
|
-
const { header: h, events: evs } = readSessionFile(p)
|
|
49
|
-
assert.equal(h.type, 'session')
|
|
50
|
-
assert.equal(evs.length, 3)
|
|
51
|
-
assert.equal(evs[0].data.message.content[0].text, '你好')
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test('listMessages 提取 role/snippet 与 tool 摘要', () => {
|
|
55
|
-
const events = [msg('user/message', 0, ' 多 空格\n文本 '), msg('assistant/message', 1, '回复'), { type: 'tool/call', seq: 2, data: { name: 'bash' } }]
|
|
56
|
-
const ms = listMessages(events)
|
|
57
|
-
assert.equal(ms.length, 3)
|
|
58
|
-
assert.equal(ms[0].role, 'user')
|
|
59
|
-
assert.equal(ms[0].snippet, '多 空格 文本')
|
|
60
|
-
assert.equal(ms[2].role, 'tool')
|
|
61
|
-
assert.match(ms[2].snippet, /\[tool\] bash/)
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test('computeShadowed 识别 surface replace 遮蔽区间', () => {
|
|
65
|
-
const events = [
|
|
66
|
-
msg('user/message', 0, 'a'),
|
|
67
|
-
msg('assistant/message', 1, 'b'),
|
|
68
|
-
msg('user/message', 2, 'c'),
|
|
69
|
-
{ type: 'system/message', seq: 3, surfaceOp: { op: 'replace', startSeq: 1, endSeq: 2 }, data: { message: { role: 'system', content: [{ type: 'text', text: '[回滚]' }] } } },
|
|
70
|
-
]
|
|
71
|
-
const shadowed = computeShadowed(events)
|
|
72
|
-
assert.ok(shadowed.has(1) && shadowed.has(2))
|
|
73
|
-
assert.ok(!shadowed.has(0))
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
test('findSessionDirs 两种 id 拼写均可定位', () => {
|
|
79
|
-
const uuid = '11111111-2222-3333-4444-555555555555'
|
|
80
|
-
writeLog(path.join(process.env.DSH_HOME, 'sessions', 'proj-c', `session-${uuid}`), header(`session-${uuid}`), [msg('user/message', 0, 'x')])
|
|
81
|
-
const byFull = findSessionDirs(`session-${uuid}`)
|
|
82
|
-
const byBare = findSessionDirs(uuid)
|
|
83
|
-
assert.equal(byFull.length, 1)
|
|
84
|
-
assert.equal(byBare.length, 1)
|
|
85
|
-
assert.equal(byFull[0].dir, byBare[0].dir)
|
|
86
|
-
})
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Session trash contract (TDD).
|
|
3
|
-
*
|
|
4
|
-
* The trash is a reversible move: the session directory is renamed into
|
|
5
|
-
* $DSH_HOME/trash/session-ops/<trashId>/data/... and the projection/workspace
|
|
6
|
-
* metadata is captured in a manifest before being removed from the live
|
|
7
|
-
* domains. Restore must put all of it back; purge must be explicit.
|
|
8
|
-
*/
|
|
9
|
-
import test from 'node:test'
|
|
10
|
-
import assert from 'node:assert/strict'
|
|
11
|
-
import fs from 'node:fs'
|
|
12
|
-
import os from 'node:os'
|
|
13
|
-
import path from 'node:path'
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
deleteSessionToTrash,
|
|
17
|
-
listTrash,
|
|
18
|
-
restoreTrash,
|
|
19
|
-
purgeTrash,
|
|
20
|
-
trashRoot,
|
|
21
|
-
} from '../src/session-trash.js'
|
|
22
|
-
|
|
23
|
-
const SID = 'session-11111111-1111-4111-8111-111111111111'
|
|
24
|
-
const OTHER = 'session-22222222-2222-4222-8222-222222222222'
|
|
25
|
-
|
|
26
|
-
function makeCtx({ running = false } = {}) {
|
|
27
|
-
const proj = new Map([[SID, {
|
|
28
|
-
identity: { createdAt: 1790000000000 },
|
|
29
|
-
rows: { title: { val: '会话标题' } },
|
|
30
|
-
}]])
|
|
31
|
-
const workspaces = {
|
|
32
|
-
w1: { path: '/tmp/project', title: '项目', sessionIds: [SID, OTHER] },
|
|
33
|
-
w2: { path: '/tmp/other', title: 'Other', sessionIds: [OTHER] },
|
|
34
|
-
}
|
|
35
|
-
const global = {
|
|
36
|
-
initialized: true,
|
|
37
|
-
archivedSessionIds: [SID],
|
|
38
|
-
pinnedSessionIds: [SID],
|
|
39
|
-
}
|
|
40
|
-
const projTable = {
|
|
41
|
-
get: (k) => proj.get(k),
|
|
42
|
-
delete: async (k) => { proj.delete(k) },
|
|
43
|
-
put: async (k, v) => { proj.set(k, v) },
|
|
44
|
-
entries: () => proj.entries(),
|
|
45
|
-
}
|
|
46
|
-
const wsTable = {
|
|
47
|
-
entries: () => Object.entries(workspaces),
|
|
48
|
-
put: async (k, v) => { workspaces[k] = v },
|
|
49
|
-
}
|
|
50
|
-
const sd = {
|
|
51
|
-
get(name) {
|
|
52
|
-
if (name === 'session_projcache') return { table: () => projTable }
|
|
53
|
-
if (name === 'workspace') return { table: () => wsTable, global: { get: () => global, set: async (v) => Object.assign(global, v) } }
|
|
54
|
-
return undefined
|
|
55
|
-
},
|
|
56
|
-
}
|
|
57
|
-
const ctx = {
|
|
58
|
-
get(name) {
|
|
59
|
-
if (name === 'storageDomain') return sd
|
|
60
|
-
if (name === 'agents') return { get: () => (running ? { status: 'running' } : undefined) }
|
|
61
|
-
if (name === 'sessions') return { get: () => undefined, flush: async () => {} }
|
|
62
|
-
return undefined
|
|
63
|
-
},
|
|
64
|
-
}
|
|
65
|
-
return { ctx, proj, workspaces, global }
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function setup(t) {
|
|
69
|
-
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'mops-trash-'))
|
|
70
|
-
const prev = process.env.DSH_HOME
|
|
71
|
-
process.env.DSH_HOME = home
|
|
72
|
-
const dir = path.join(home, 'sessions', '--tmp-project--', SID)
|
|
73
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
74
|
-
fs.writeFileSync(path.join(dir, 'session.v4.jsonl.zstd'), 'fake-frame')
|
|
75
|
-
const env = makeCtx()
|
|
76
|
-
t.after(() => {
|
|
77
|
-
if (prev === undefined) delete process.env.DSH_HOME
|
|
78
|
-
else process.env.DSH_HOME = prev
|
|
79
|
-
fs.rmSync(home, { recursive: true, force: true })
|
|
80
|
-
})
|
|
81
|
-
return { home, dir, ...env }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
test('delete moves the session directory into trash and strips metadata', async (t) => {
|
|
85
|
-
const { ctx, dir, proj, workspaces, global } = setup(t)
|
|
86
|
-
const result = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
87
|
-
|
|
88
|
-
assert.equal(result.ok, true)
|
|
89
|
-
assert.match(result.trashId, /^\d+-[0-9a-f]{8}$/)
|
|
90
|
-
assert.equal(fs.existsSync(dir), false, 'source session dir must be moved away')
|
|
91
|
-
assert.equal(proj.has(SID), false, 'projection row must be removed')
|
|
92
|
-
assert.deepEqual(workspaces.w1.sessionIds, [OTHER], 'workspace membership must be stripped')
|
|
93
|
-
assert.deepEqual(global.archivedSessionIds, [], 'archived marker must be stripped')
|
|
94
|
-
assert.deepEqual(global.pinnedSessionIds, [], 'pinned marker must be stripped')
|
|
95
|
-
|
|
96
|
-
const manifestPath = path.join(trashRoot(), result.trashId, 'manifest.json')
|
|
97
|
-
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
|
|
98
|
-
assert.equal(manifest.state, 'trashed')
|
|
99
|
-
assert.equal(manifest.sessionId, SID)
|
|
100
|
-
assert.equal(manifest.title, '会话标题')
|
|
101
|
-
assert.ok(manifest.projRow, 'projection row must be captured for restore')
|
|
102
|
-
assert.deepEqual(manifest.workspaceIds, ['w1'])
|
|
103
|
-
assert.equal(manifest.archived, true)
|
|
104
|
-
assert.equal(manifest.pinned, true)
|
|
105
|
-
assert.ok(fs.existsSync(path.join(trashRoot(), result.trashId, 'data', '--tmp-project--', SID, 'session.v4.jsonl.zstd')))
|
|
106
|
-
|
|
107
|
-
const entries = await listTrash(ctx)
|
|
108
|
-
assert.equal(entries.length, 1)
|
|
109
|
-
assert.equal(entries[0].trashId, result.trashId)
|
|
110
|
-
assert.equal(entries[0].title, '会话标题')
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
test('restore puts the directory and all metadata back', async (t) => {
|
|
114
|
-
const { ctx, dir, proj, workspaces, global } = setup(t)
|
|
115
|
-
const { trashId } = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
116
|
-
|
|
117
|
-
await restoreTrash(ctx, trashId)
|
|
118
|
-
|
|
119
|
-
assert.equal(fs.existsSync(path.join(dir, 'session.v4.jsonl.zstd')), true)
|
|
120
|
-
assert.equal(proj.has(SID), true)
|
|
121
|
-
assert.equal(proj.get(SID).rows.title.val, '会话标题')
|
|
122
|
-
assert.ok(workspaces.w1.sessionIds.includes(SID))
|
|
123
|
-
assert.ok(global.archivedSessionIds.includes(SID))
|
|
124
|
-
assert.ok(global.pinnedSessionIds.includes(SID))
|
|
125
|
-
assert.deepEqual(await listTrash(ctx), [])
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
test('delete refuses a running session', async (t) => {
|
|
129
|
-
const { home } = setup(t)
|
|
130
|
-
const { ctx } = makeCtx({ running: true })
|
|
131
|
-
await assert.rejects(
|
|
132
|
-
() => deleteSessionToTrash(ctx, SID, '会话标题'),
|
|
133
|
-
(e) => e.status === 409 && /running/i.test(e.message),
|
|
134
|
-
)
|
|
135
|
-
assert.equal(fs.existsSync(path.join(home, 'sessions', '--tmp-project--', SID)), true)
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
test('delete requires the exact title', async (t) => {
|
|
139
|
-
const { ctx, dir } = setup(t)
|
|
140
|
-
await assert.rejects(
|
|
141
|
-
() => deleteSessionToTrash(ctx, SID, 'wrong title'),
|
|
142
|
-
(e) => e.status === 400 && /title/i.test(e.message),
|
|
143
|
-
)
|
|
144
|
-
assert.equal(fs.existsSync(dir), true)
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
test('purge permanently removes a trash entry', async (t) => {
|
|
148
|
-
const { ctx } = setup(t)
|
|
149
|
-
const { trashId } = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
150
|
-
await purgeTrash(ctx, trashId)
|
|
151
|
-
assert.equal(fs.existsSync(path.join(trashRoot(), trashId)), false)
|
|
152
|
-
assert.deepEqual(await listTrash(ctx), [])
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
test('expired trash entries can be swept without touching fresh ones', async (t) => {
|
|
156
|
-
const { ctx } = setup(t)
|
|
157
|
-
const old = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
158
|
-
const manifestPath = path.join(trashRoot(), old.trashId, 'manifest.json')
|
|
159
|
-
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
|
|
160
|
-
manifest.expiresAt = Date.now() - 1000
|
|
161
|
-
fs.writeFileSync(manifestPath, JSON.stringify(manifest))
|
|
162
|
-
|
|
163
|
-
const entries = await listTrash(ctx, { purgeExpired: true })
|
|
164
|
-
assert.deepEqual(entries, [])
|
|
165
|
-
assert.equal(fs.existsSync(path.join(trashRoot(), old.trashId)), false)
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
test('restore refuses to overwrite an existing session directory', async (t) => {
|
|
169
|
-
const { ctx, dir } = setup(t)
|
|
170
|
-
const { trashId } = await deleteSessionToTrash(ctx, SID, '会话标题')
|
|
171
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
172
|
-
fs.writeFileSync(path.join(dir, 'session.v4.jsonl.zstd'), 'new-frame')
|
|
173
|
-
|
|
174
|
-
await assert.rejects(
|
|
175
|
-
() => restoreTrash(ctx, trashId),
|
|
176
|
-
(e) => e.status === 409 && /exists/i.test(e.message),
|
|
177
|
-
)
|
|
178
|
-
assert.equal(fs.readFileSync(path.join(dir, 'session.v4.jsonl.zstd'), 'utf8'), 'new-frame')
|
|
179
|
-
})
|
|
@@ -1,112 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,166 +0,0 @@
|
|
|
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
|
-
})
|