@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
|
@@ -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,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
|
-
})
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 0.9.4 transcript 隐藏判据 —— 回归「正文几乎空白」事故。
|
|
3
|
-
*
|
|
4
|
-
* 🔴🔴 本文件是「历史判据的回归样本」,**不是**当前实现的规格 🔴🔴
|
|
5
|
-
*
|
|
6
|
-
* 文件里的 `decide()` / `visible` 集合 / `fences` 全是 **0.9.4 时代的判据副本**,
|
|
7
|
-
* 与 `src/client.js` 当前(0.9.6+)的 `shadowed` 判据**不同源**:
|
|
8
|
-
* - 当前真判据:`shouldHide = idx.shadowed.has(hit.seq)`(replace 区间)+ 回合 chrome
|
|
9
|
-
* 的 `!idx.visibleTurns.has(turn)`;权威锁在 `test/client-perf-locks.test.js`。
|
|
10
|
-
* - 本文件保留的是「当年为什么会出事、当年怎么算的」的原始样本,全绿只因测的是自己的副本。
|
|
11
|
-
* ⛔ **禁止**把这里的 helper 与其它测试的 helper 合并去重 —— 合并等于把 0.9.4 的
|
|
12
|
-
* 过期契约焊死成「现行契约」。`test/undo-slash.test.js` 的
|
|
13
|
-
* `shouldHide(cands, fences, reverted)` 与本文件属同一批,同样不许合并。
|
|
14
|
-
* (唯一仍与现行实现同源的是 `pickLastVisible`:它对齐 client.js 的
|
|
15
|
-
* `lastRevertable`「可回滚目标」语义,那一套至今仍用 `visible`。)
|
|
16
|
-
*
|
|
17
|
-
* 事故:会话有 19 个**离散** marker([9,9]、[10,4368]、[6830,8573] …),
|
|
18
|
-
* 服务端把它们塌缩成 revertFences = range.start 的列表 [10, 6830, 9829, 11429],
|
|
19
|
-
* 客户端判据 `seq >= min(fences)` 等价于 `seq >= 10`,
|
|
20
|
-
* 于是 4792/4793 条全被 hidden —— 截图里正文几乎空白。
|
|
21
|
-
*
|
|
22
|
-
* 当时(0.9.4)认为的正确判据 = 模型 surface(`m.visible`)。实测它与宿主
|
|
23
|
-
* visibleCount 精确吻合:
|
|
24
|
-
* 156b9ecf: total 4793, visibleCount 2083 → !visible 应为 2710
|
|
25
|
-
* 4e10c1a2: total 5038, visibleCount 464 → !visible 应为 4574
|
|
26
|
-
* ⚠️ 0.9.6 又把它换成了 `shadowed`(live 路径下 tool/call 恒 visible=false,
|
|
27
|
-
* 拿 visible 当判据会整页误藏工具行)—— 见文件末尾的「当前判据对照」测试。
|
|
28
|
-
*/
|
|
29
|
-
import test from 'node:test'
|
|
30
|
-
import assert from 'node:assert/strict'
|
|
31
|
-
import fs from 'node:fs'
|
|
32
|
-
|
|
33
|
-
/** 0.9.4 判据(已废弃)的历史副本 —— 与 client.js 当前 shadowed 判据不同源,禁止合并。 */
|
|
34
|
-
function decide(hit, idx) {
|
|
35
|
-
if (hit === null) return null // 查不到 → 保持可见
|
|
36
|
-
if (hit.turn !== undefined) return !idx.visibleTurns.has(String(hit.turn))
|
|
37
|
-
return !idx.visible.has(hit.seq)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
test('回归:fence 塌缩会把离散区间放大成「seq >= 最小值」', () => {
|
|
41
|
-
// [0.9.4 判据(已废弃)] fences 只存在于历史实现,client.js 早已不发 revertFences。
|
|
42
|
-
const fences = [10, 6830, 9829, 11429] // 实测(19 个 marker 的 range.start)
|
|
43
|
-
const total = 4793
|
|
44
|
-
const hiddenByFence = Array.from({ length: total }, (_, i) => i + 7).filter(s => fences.some(f => s >= f))
|
|
45
|
-
// seq 从 7 起;min fence=10 → 几乎全中
|
|
46
|
-
assert.ok(hiddenByFence.length >= 4790,
|
|
47
|
-
`fence 塌缩藏了 ${hiddenByFence.length}/${total} —— 正文必然空白`)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test('正确判据与宿主 visibleCount 精确吻合(会话 156b9ecf)', () => {
|
|
51
|
-
// [0.9.4 判据(已废弃)] 当时的「正确判据」= m.visible;0.9.6 起真判据是 shadowed。
|
|
52
|
-
const total = 4793, visibleCount = 2083
|
|
53
|
-
// 构造:前 visibleCount 条可见,其余被遮蔽
|
|
54
|
-
const visible = new Set()
|
|
55
|
-
for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
|
|
56
|
-
const hidden = Array.from({ length: total }, (_, i) => i + 7).filter(s => !visible.has(s))
|
|
57
|
-
assert.equal(hidden.length, total - visibleCount, 'hidden 必须等于 total - visibleCount')
|
|
58
|
-
assert.equal(total - hidden.length, visibleCount)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
test('第二条会话同样吻合(4e10c1a2)', () => {
|
|
62
|
-
// [0.9.4 判据(已废弃)] 同上:visible 口径的量化样本。
|
|
63
|
-
const total = 5038, visibleCount = 464
|
|
64
|
-
const visible = new Set()
|
|
65
|
-
for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
|
|
66
|
-
const hidden = Array.from({ length: total }, (_, i) => i + 7).filter(s => !visible.has(s))
|
|
67
|
-
assert.equal(hidden.length, 4574)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
test('回合 chrome 按「本 turn 有无可见消息」判,不按 seq', () => {
|
|
71
|
-
const idx = { visible: new Set([7]), visibleTurns: new Set(['1']) }
|
|
72
|
-
// turn 1 有可见消息 → chrome 露出(即使 turn/end 的 seq 不在 surface 上)
|
|
73
|
-
assert.equal(decide({ kind: 'turn-tail', seq: 48, turn: '1' }, idx), false)
|
|
74
|
-
// turn 2 没有可见消息 → chrome 隐藏
|
|
75
|
-
assert.equal(decide({ kind: 'turn-tail', seq: 57, turn: '2' }, idx), true)
|
|
76
|
-
// 无可见消息的 turn 一律藏
|
|
77
|
-
const empty = { visible: new Set(), visibleTurns: new Set() }
|
|
78
|
-
assert.equal(decide({ kind: 'turn-process', seq: 9, turn: '9' }, empty), true)
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
test('消息类:在 surface 上就露,不在就藏', () => {
|
|
82
|
-
// [0.9.4 判据(已废弃)] 消息类分支用的 idx.visible 已被 0.9.6 的 shadowed 取代;
|
|
83
|
-
// 回合 chrome 分支(visibleTurns)仍与现行实现同源。
|
|
84
|
-
const idx = { visible: new Set([8, 18]), visibleTurns: new Set() }
|
|
85
|
-
assert.equal(decide({ kind: 'input-message', seq: 8, all: [8] }, idx), false, '在 surface → 露')
|
|
86
|
-
assert.equal(decide({ kind: 'input-message', seq: 54, all: [54] }, idx), true, '不在 → 藏')
|
|
87
|
-
assert.equal(decide({ kind: 'tool-call', seq: 19, all: [19] }, idx), true)
|
|
88
|
-
assert.equal(decide({ kind: 'tool-call', seq: 18, all: [18] }, idx), false, 'tool/call 也可能在 surface 上(实测 957 条 visible)')
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
test('查不到一律保持可见(绝不误藏)', () => {
|
|
92
|
-
const idx = { visible: new Set([1]), visibleTurns: new Set() }
|
|
93
|
-
assert.equal(decide(null, idx), null)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
// ── 对照:现行实现(0.9.6+)用的是 shadowed,不是本文件上面那些副本 ──────────
|
|
97
|
-
// 上面所有断言都是历史判据的回归样本;这一条负责把「真判据在 client.js 哪里」钉死,
|
|
98
|
-
// 免得后人误以为本文件的 decide() 就是现行规格。
|
|
99
|
-
test('[当前判据对照] client.js 藏行判据 = idx.shadowed,不是本文件的 visible/fences 副本', () => {
|
|
100
|
-
const src = fs.readFileSync(new URL('../src/client.js', import.meta.url), 'utf8')
|
|
101
|
-
assert.ok(/shouldHide = idx\.shadowed\.has\(hit\.seq\)/.test(src),
|
|
102
|
-
'消息/工具类判据必须是 idx.shadowed(0.9.6)')
|
|
103
|
-
assert.ok(/shouldHide = !idx\.visibleTurns\.has\(String\(hit\.turn\)\)/.test(src),
|
|
104
|
-
'回合 chrome 判据必须是 idx.visibleTurns')
|
|
105
|
-
assert.ok(!/idx\.visible\.has\(/.test(src),
|
|
106
|
-
'client.js 不得回退到 idx.visible 判据(live 路径下 tool/call 恒 false → 整页误藏)')
|
|
107
|
-
// revertFences 允许出现在**注释**里(指向 README「历史事故记录」#4 的演进史指针),
|
|
108
|
-
// 但任何**代码行**出现 = 塌缩判据复活 —— 所以先剥掉整行注释再查。
|
|
109
|
-
const codeOnly = src.split('\n').filter((l) => !/^\s*(\/\/|\/\*|\*)/.test(l)).join('\n')
|
|
110
|
-
assert.ok(!/\brevertFences\b/.test(codeOnly), 'client.js 代码不得复活 revertFences 塌缩判据(注释指针除外)')
|
|
111
|
-
})
|
package/test/undo-slash.test.js
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 0.9.3 /undo 的纯逻辑回归。
|
|
3
|
-
*
|
|
4
|
-
* /undo 走官方斜杠管线 `ctx.inputTriggers.registerSource`,注册路径踩过四个坑,
|
|
5
|
-
* 全部锁在这里(宿主侧的契约坑无法用单测覆盖,故此文件锁「我方判定」那一半)。
|
|
6
|
-
*
|
|
7
|
-
* 🔴🔴 文件里的 helper 分两类,**不许与其它测试的 helper 合并去重** 🔴🔴
|
|
8
|
-
*
|
|
9
|
-
* 1. `shouldHide(cands, fences, reverted)` —— **0.9.4 历史判据(已废弃)** 的
|
|
10
|
-
* 独立副本。client.js 0.9.6 起藏行判据已是 `idx.shadowed`(replace 区间),
|
|
11
|
-
* `fences` / `reverted` 两套都不再存在。本 helper 全绿只因测的是自己的副本;
|
|
12
|
-
* 它与 `test/transcript-visibility.test.js` 的 `decide()` 属同一批历史样本。
|
|
13
|
-
* ⛔ 合并 = 把过期契约焊死成现行契约。
|
|
14
|
-
* 2. `pickLastVisible(rows)` —— **仍与现行实现同源**:对齐 client.js 的
|
|
15
|
-
* `lastRevertable`(「可回滚目标」必须挑模型 surface 上的那一条,至今用
|
|
16
|
-
* `visible`)。那是另一套语义,与藏行判据无关,不要混淆。
|
|
17
|
-
*/
|
|
18
|
-
import test from 'node:test'
|
|
19
|
-
import assert from 'node:assert/strict'
|
|
20
|
-
import fs from 'node:fs'
|
|
21
|
-
|
|
22
|
-
const bare = (id) => String(id || '').replace(/^session-/, '')
|
|
23
|
-
const pickLastVisible = (rows) => {
|
|
24
|
-
for (let i = rows.length - 1; i >= 0; i--) if (rows[i] && rows[i].seq != null && rows[i].visible) return rows[i]
|
|
25
|
-
return null
|
|
26
|
-
}
|
|
27
|
-
/** [0.9.4 判据(已废弃)] 历史副本 —— 现行判据是 client.js 的 idx.shadowed。 */
|
|
28
|
-
const shouldHide = (cands, fences, reverted) =>
|
|
29
|
-
fences.length ? cands.some((q) => fences.some((f) => q >= f)) : cands.some((q) => reverted.has(q))
|
|
30
|
-
|
|
31
|
-
test('sessionId 归一化:DOM dataset 带 session- 前缀', () => {
|
|
32
|
-
assert.equal(bare('session-0da37218-2672-48b7-adaa-7735af4adee2'), '0da37218-2672-48b7-adaa-7735af4adee2')
|
|
33
|
-
assert.equal(bare('0da37218-2672'), '0da37218-2672')
|
|
34
|
-
assert.equal(bare(null), '')
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
test('回归:__currentSessionId 带前缀会让 API 请求静默失败', () => {
|
|
38
|
-
// 送 "session-<uuid>" 给 /api/message-ops/revert 会落不到会话目录
|
|
39
|
-
assert.notEqual(bare('session-x'), 'session-x')
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
test('lastRevertable 必须挑 surface 上的那条', () => {
|
|
43
|
-
// [现行同源] 对齐 client.js 的 lastRevertable(visible = 模型 surface,仍在用)
|
|
44
|
-
const rows = [
|
|
45
|
-
{ seq: 8, visible: true },
|
|
46
|
-
{ seq: 54, visible: true },
|
|
47
|
-
{ seq: 70, visible: false }, // restore 重放副本:不在模型 surface 上
|
|
48
|
-
]
|
|
49
|
-
// 无脑取最后一条 → 70 → 服务端报 "start seq 70 not found in surface"(实测���
|
|
50
|
-
assert.equal(rows[rows.length - 1].seq, 70)
|
|
51
|
-
// 正确:取最后一条 visible
|
|
52
|
-
assert.equal(pickLastVisible(rows).seq, 54)
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('没有 surface 上的用户消息 → 返回 null,不发请求', () => {
|
|
56
|
-
assert.equal(pickLastVisible([{ seq: 8, visible: false }]), null)
|
|
57
|
-
assert.equal(pickLastVisible([]), null)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('fence 判定:边界及其后都藏', () => {
|
|
61
|
-
// [0.9.4 判据(已废弃)] fences 只存在于历史实现,client.js 已不发 revertFences。
|
|
62
|
-
assert.equal(shouldHide([8], [8], new Set()), true)
|
|
63
|
-
assert.equal(shouldHide([7], [8], new Set()), false)
|
|
64
|
-
assert.equal(shouldHide([70], [70, 8], new Set()), true, '多个 fence 取并集')
|
|
65
|
-
assert.equal(shouldHide([54], [70, 8], new Set()), true)
|
|
66
|
-
assert.equal(shouldHide([7], [70, 8], new Set()), false)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('fence 为空时退回 reverted 集合', () => {
|
|
70
|
-
// [0.9.4 判据(已废弃)] reverted 集合已被 0.9.6 的 shadowed 取代。
|
|
71
|
-
assert.equal(shouldHide([54], [], new Set([54, 63])), true)
|
|
72
|
-
assert.equal(shouldHide([8], [], new Set([54, 63])), false)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('空 fences + 空 reverted → 全部可见', () => {
|
|
76
|
-
// [0.9.4 判据(已废弃)]
|
|
77
|
-
assert.equal(shouldHide([1, 2, 3], [], new Set()), false)
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
// ── 对照:现行实现(0.9.6+)用的是 shadowed,不是上面那些历史副本 ────────────
|
|
81
|
-
test('[当前判据对照] client.js 藏行判据 = idx.shadowed,本文件 shouldHide 只是历史样本', () => {
|
|
82
|
-
const src = fs.readFileSync(new URL('../src/client.js', import.meta.url), 'utf8')
|
|
83
|
-
assert.ok(/shouldHide = idx\.shadowed\.has\(hit\.seq\)/.test(src),
|
|
84
|
-
'消息/工具类判据必须是 idx.shadowed(0.9.6)')
|
|
85
|
-
assert.ok(!/\bfences\b/.test(src.match(/var applySurfaceVisibility = [\s\S]*?\n {6}\};/)?.[0] ?? ''),
|
|
86
|
-
'applySurfaceVisibility 内不得出现 fences(0.9.4 判据已废弃)')
|
|
87
|
-
assert.ok(/r\.seq != null && r\.visible/.test(src),
|
|
88
|
-
'lastRevertable 仍须用 visible 挑「可回滚目标」(与藏行判据是两套语义)')
|
|
89
|
-
})
|