@240xu/dsh-message-ops 0.9.5 → 0.10.1

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.
@@ -0,0 +1,98 @@
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,19 +1,36 @@
1
1
  /**
2
2
  * 0.9.4 transcript 隐藏判据 —— 回归「正文几乎空白」事故。
3
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
+ *
4
17
  * 事故:会话有 19 个**离散** marker([9,9]、[10,4368]、[6830,8573] …),
5
18
  * 服务端把它们塌缩成 revertFences = range.start 的列表 [10, 6830, 9829, 11429],
6
19
  * 客户端判据 `seq >= min(fences)` 等价于 `seq >= 10`,
7
20
  * 于是 4792/4793 条全被 hidden —— 截图里正文几乎空白。
8
21
  *
9
- * 正确判据 = 模型 surface(`m.visible`)。实测它与宿主 visibleCount 精确吻合:
22
+ * 当时(0.9.4)认为的正确判据 = 模型 surface(`m.visible`)。实测它与宿主
23
+ * visibleCount 精确吻合:
10
24
  * 156b9ecf: total 4793, visibleCount 2083 → !visible 应为 2710
11
25
  * 4e10c1a2: total 5038, visibleCount 464 → !visible 应为 4574
26
+ * ⚠️ 0.9.6 又把它换成了 `shadowed`(live 路径下 tool/call 恒 visible=false,
27
+ * 拿 visible 当判据会整页误藏工具行)—— 见文件末尾的「当前判据对照」测试。
12
28
  */
13
29
  import test from 'node:test'
14
30
  import assert from 'node:assert/strict'
31
+ import fs from 'node:fs'
15
32
 
16
- /** 与 client.js 同构:隐藏判据 */
33
+ /** 0.9.4 判据(已废弃)的历史副本 —— 与 client.js 当前 shadowed 判据不同源,禁止合并。 */
17
34
  function decide(hit, idx) {
18
35
  if (hit === null) return null // 查不到 → 保持可见
19
36
  if (hit.turn !== undefined) return !idx.visibleTurns.has(String(hit.turn))
@@ -21,6 +38,7 @@ function decide(hit, idx) {
21
38
  }
22
39
 
23
40
  test('回归:fence 塌缩会把离散区间放大成「seq >= 最小值」', () => {
41
+ // [0.9.4 判据(已废弃)] fences 只存在于历史实现,client.js 早已不发 revertFences。
24
42
  const fences = [10, 6830, 9829, 11429] // 实测(19 个 marker 的 range.start)
25
43
  const total = 4793
26
44
  const hiddenByFence = Array.from({ length: total }, (_, i) => i + 7).filter(s => fences.some(f => s >= f))
@@ -30,6 +48,7 @@ test('回归:fence 塌缩会把离散区间放大成「seq >= 最小值」', (
30
48
  })
31
49
 
32
50
  test('正确判据与宿主 visibleCount 精确吻合(会话 156b9ecf)', () => {
51
+ // [0.9.4 判据(已废弃)] 当时的「正确判据」= m.visible;0.9.6 起真判据是 shadowed。
33
52
  const total = 4793, visibleCount = 2083
34
53
  // 构造:前 visibleCount 条可见,其余被遮蔽
35
54
  const visible = new Set()
@@ -40,6 +59,7 @@ test('正确判据与宿主 visibleCount 精确吻合(会话 156b9ecf)', ()
40
59
  })
41
60
 
42
61
  test('第二条会话同样吻合(4e10c1a2)', () => {
62
+ // [0.9.4 判据(已废弃)] 同上:visible 口径的量化样本。
43
63
  const total = 5038, visibleCount = 464
44
64
  const visible = new Set()
45
65
  for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
@@ -59,6 +79,8 @@ test('回合 chrome 按「本 turn 有无可见消息」判,不按 seq', () =>
59
79
  })
60
80
 
61
81
  test('消息类:在 surface 上就露,不在就藏', () => {
82
+ // [0.9.4 判据(已废弃)] 消息类分支用的 idx.visible 已被 0.9.6 的 shadowed 取代;
83
+ // 回合 chrome 分支(visibleTurns)仍与现行实现同源。
62
84
  const idx = { visible: new Set([8, 18]), visibleTurns: new Set() }
63
85
  assert.equal(decide({ kind: 'input-message', seq: 8, all: [8] }, idx), false, '在 surface → 露')
64
86
  assert.equal(decide({ kind: 'input-message', seq: 54, all: [54] }, idx), true, '不在 → 藏')
@@ -70,3 +92,20 @@ test('查不到一律保持可见(绝不误藏)', () => {
70
92
  const idx = { visible: new Set([1]), visibleTurns: new Set() }
71
93
  assert.equal(decide(null, idx), null)
72
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
+ })
@@ -3,15 +3,28 @@
3
3
  *
4
4
  * /undo 走官方斜杠管线 `ctx.inputTriggers.registerSource`,注册路径踩过四个坑,
5
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`)。那是另一套语义,与藏行判据无关,不要混淆。
6
17
  */
7
18
  import test from 'node:test'
8
19
  import assert from 'node:assert/strict'
20
+ import fs from 'node:fs'
9
21
 
10
22
  const bare = (id) => String(id || '').replace(/^session-/, '')
11
23
  const pickLastVisible = (rows) => {
12
24
  for (let i = rows.length - 1; i >= 0; i--) if (rows[i] && rows[i].seq != null && rows[i].visible) return rows[i]
13
25
  return null
14
26
  }
27
+ /** [0.9.4 判据(已废弃)] 历史副本 —— 现行判据是 client.js 的 idx.shadowed。 */
15
28
  const shouldHide = (cands, fences, reverted) =>
16
29
  fences.length ? cands.some((q) => fences.some((f) => q >= f)) : cands.some((q) => reverted.has(q))
17
30
 
@@ -27,6 +40,7 @@ test('回归:__currentSessionId 带前缀会让 API 请求静默失败', () =>
27
40
  })
28
41
 
29
42
  test('lastRevertable 必须挑 surface 上的那条', () => {
43
+ // [现行同源] 对齐 client.js 的 lastRevertable(visible = 模型 surface,仍在用)
30
44
  const rows = [
31
45
  { seq: 8, visible: true },
32
46
  { seq: 54, visible: true },
@@ -44,6 +58,7 @@ test('没有 surface 上的用户消息 → 返回 null,不发请求', () => {
44
58
  })
45
59
 
46
60
  test('fence 判定:边界及其后都藏', () => {
61
+ // [0.9.4 判据(已废弃)] fences 只存在于历史实现,client.js 已不发 revertFences。
47
62
  assert.equal(shouldHide([8], [8], new Set()), true)
48
63
  assert.equal(shouldHide([7], [8], new Set()), false)
49
64
  assert.equal(shouldHide([70], [70, 8], new Set()), true, '多个 fence 取并集')
@@ -52,10 +67,23 @@ test('fence 判定:边界及其后都藏', () => {
52
67
  })
53
68
 
54
69
  test('fence 为空时退回 reverted 集合', () => {
70
+ // [0.9.4 判据(已废弃)] reverted 集合已被 0.9.6 的 shadowed 取代。
55
71
  assert.equal(shouldHide([54], [], new Set([54, 63])), true)
56
72
  assert.equal(shouldHide([8], [], new Set([54, 63])), false)
57
73
  })
58
74
 
59
75
  test('空 fences + 空 reverted → 全部可见', () => {
76
+ // [0.9.4 判据(已废弃)]
60
77
  assert.equal(shouldHide([1, 2, 3], [], new Set()), false)
61
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
+ })
@@ -0,0 +1,62 @@
1
+ /**
2
+ * 0.9.6 回归:重放 assistant 必须带 v4 必填字段 `data.stream`。
3
+ *
4
+ * 事故(专家组定位):replayEventData 漏写 stream → 宿主投影层
5
+ * dsh-token-meter/lib/types/usage-projection.js:65 usageOf()
6
+ * → data.usage === undefined 回落到
7
+ * dsh-llm/lib/index.js:1412 lastAssistantStreamChunk(stream,'usage')
8
+ * → `stream.length` on undefined → TypeError
9
+ * 抛出点无 try/catch,两个后果:
10
+ * - 历史加载失败 → 正文 0 行(dsh-session-query/lib/index.js:482)
11
+ * - 控制流基线失败 → 全站 `[session-controller] control stream failed`
12
+ * (dsh-api-session-controller/lib/index.js:1178,一个坏会话打死全站)
13
+ *
14
+ * 格式闸拦不住它:v3→v4 闸对 stream 只有 Array.isArray 守卫,缺失即跳过。
15
+ * 所以必须在这里锁契约。
16
+ */
17
+ import test from 'node:test'
18
+ import assert from 'node:assert/strict'
19
+ import { replayEventData } from '../src/ops-core.js'
20
+
21
+ const TS = { turn: 4, step: 2 }
22
+ const ASSISTANT_ITEM = { type: 'assistant/message', text: '重放的助手回答' }
23
+
24
+ test('重放 assistant 必须带 stream,且是数组', () => {
25
+ const data = replayEventData(ASSISTANT_ITEM, TS, 4)
26
+ assert.ok('stream' in data, '缺 data.stream —— 宿主投影会在 stream.length 上抛 TypeError')
27
+ assert.ok(Array.isArray(data.stream), 'stream 必须是 AssistantStreamRecord[]')
28
+ })
29
+
30
+ test('stream 只能是空数组:半填充流会触发宿主 deepStrictEqual 全量校验', () => {
31
+ const data = replayEventData(ASSISTANT_ITEM, TS, 4)
32
+ // 空数组 → expandAssistantStream([])→[] → assertCurrentAssistantStreams 直接 continue
33
+ // 一旦有条目,宿主要求 blocks()/usage/replayState 与 message 全部深等,做不全=新的写坏
34
+ assert.equal(data.stream.length, 0)
35
+ })
36
+
37
+ test('user 重放保持摊平契约,不带 stream(宿主只对 assistant 要求)', () => {
38
+ const data = replayEventData({ type: 'user/message', text: '原文' }, TS, 4)
39
+ assert.equal(data.role, 'user')
40
+ assert.ok(Array.isArray(data.content), 'user 本体摊平在 data 顶层(v4 契约)')
41
+ assert.ok(!('message' in data), 'user 不应包一层 message')
42
+ })
43
+
44
+ test('assistant 本体包一层 message(与 user 的摊平相反)', () => {
45
+ const data = replayEventData(ASSISTANT_ITEM, TS, 4)
46
+ assert.ok(data.message && typeof data.message === 'object')
47
+ assert.equal(data.message.role, 'assistant')
48
+ assert.equal(data.turn, 4)
49
+ assert.equal(data.step, 2)
50
+ })
51
+
52
+ test('缺 turn/step 仍被拒绝(不能写出注定无法加载的事件)', () => {
53
+ assert.throws(() => replayEventData(ASSISTANT_ITEM, null, 4), /turn\/step/)
54
+ assert.throws(() => replayEventData(ASSISTANT_ITEM, { turn: 0, step: 1 }, 4), /turn\/step/)
55
+ })
56
+
57
+ test('回归:index.js 里不能出现 winR 笔误(live restore 会 ReferenceError)', async () => {
58
+ const { readFileSync } = await import('node:fs')
59
+ const src = readFileSync(new URL('../src/index.js', import.meta.url), 'utf8')
60
+ const winR = (src.match(/\bwinR\b/g) || []).length
61
+ assert.equal(winR, 0, `存在 ${winR} 处 winR —— opsRestore 的 live 分支会抛 ReferenceError`)
62
+ })