@240xu/dsh-message-ops 0.9.3 → 0.9.4

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 CHANGED
@@ -599,3 +599,45 @@ composer 输入 /und → 官方菜单出现「回滚上一条消息 undo 移除
599
599
  → 原文「分配几个子代理把模型广场的模型价格什么的做好…」回填进 composer ✅
600
600
  → transcript:24 行中 22 行 hidden,被回滚消息从正文消失 ✅
601
601
  ```
602
+
603
+ ---
604
+
605
+ ## 0.9.4 · 修「正文几乎空白」
606
+
607
+ ### 症状
608
+
609
+ 用户截图里 133 轮的会话正文几乎全空,只露出零星几个「工具已更新」标记。
610
+
611
+ ### 根因
612
+
613
+ 19 个 marker 是**离散区间**(`[9,9]`、`[10,4368]`、`[6830,8573]` …),
614
+ 服务端把它们塌缩成 `revertFences = range.start 列表 = [10, 6830, 9829, 11429]`,
615
+ 客户端判据 `seq >= fence`(任一命中)等价于 **`seq >= min(fences)` = `seq >= 10`**。
616
+
617
+ 于是 **4792/4793 条被判为已回滚** → 全部 hidden → 正文空白。
618
+
619
+ ### 量化验证(两条真实会话)
620
+
621
+ | 判据 | 隐藏数 | 与宿主 `visibleCount` 吻合? |
622
+ |---|---|---|
623
+ | `seq >= min(fence)`(事故判据)| 4792 / 4793 | ✗ 只剩 1 条可见 |
624
+ | 未恢复 marker 区间并集 | 2707 | 部分吻合 |
625
+ | **`!visible`(模型 surface)** | **2710 / 4574** | ✓ **2083 / 464,精确吻合** |
626
+
627
+ 顺带纠正一处旧断言:`tool/call` **并非**永远不在 surface 上
628
+ —— 实测 957 条 `tool/call` 是 visible 的,`visible` 对它同样有意义。
629
+
630
+ ### 修复
631
+
632
+ - **消息类行**:`hide = !visible.has(seq)`(直接问模型 surface)
633
+ - **回合 chrome**(`turn-tail` / `turn-process` / `turn-error` / `model-retry`):
634
+ `hide = !visibleTurns.has(turn)` —— 该 turn 还有可见消息就露出,
635
+ 因为 `turn/end` 本身多半不在 surface 上
636
+ - 删除 `revertFences` / `m.reverted` 两个**编码了错误判据**的字段(已无消费者)
637
+
638
+ ### 实测
639
+
640
+ ```
641
+ 修复前: 正文几乎空白(截图)
642
+ 修复后: 113 行 → 可见 67 / 隐藏 46,可见字符 36,851
643
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@240xu/dsh-message-ops",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "DSH web 插件:消息级回滚 / 删除 / 恢复(补 dsh 官方没有的部分)。分支与导出用官方内置能力。",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/client.js CHANGED
@@ -1014,15 +1014,10 @@ window.__ModuleLoader__.load({
1014
1014
  var byTurnStep = new Map();
1015
1015
  var byCallId = new Map();
1016
1016
  var byTurn = new Map();
1017
- var visible = new Set();
1018
- var reverted = new Set();
1017
+ var visible = new Set(); // 在模型 surface 上的 seq
1018
+ var visibleTurns = new Set(); // 含至少一条可见消息的 turn 号
1019
1019
  var dupMessageId = 0
1020
1020
  var dupByMessageId = new Map();
1021
- // 回合页脚/过程条锚到 turn/end 的 seq —— 那是**非消息**事件,永远不会出现在
1022
- // reverted 集合里,所以隐藏判据必须直接对 fence 比较,不能用集合成员。
1023
- var fences = Array.isArray(payload && payload.revertFences)
1024
- ? payload.revertFences.filter(function (n) { return Number.isSafeInteger(n); })
1025
- : [];
1026
1021
  for (var i = 0; i < list.length; i++) {
1027
1022
  var m = list[i];
1028
1023
  if (!m || typeof m.seq !== 'number') continue;
@@ -1039,21 +1034,20 @@ window.__ModuleLoader__.load({
1039
1034
  dupByMessageId.set(k, arr)
1040
1035
  byMessageId.set(k, m.seq) // 指向最新的
1041
1036
  } else { byMessageId.set(k, m.seq) }
1042
- byMessageId.set(k, m.seq);
1043
1037
  }
1044
1038
  if (m.type === 'assistant/message' && m.turn != null && m.step != null) {
1045
1039
  byTurnStep.set(m.turn + ':' + m.step, m.seq);
1046
1040
  }
1047
1041
  if (m.type === 'tool/call' && m.id != null) byCallId.set(String(m.id), m.seq);
1048
1042
  if (m.turn != null && !byTurn.has(String(m.turn))) byTurn.set(String(m.turn), m.seq);
1049
- if (m.visible === true) visible.add(m.seq);
1050
- // 0.9.1:reverted 才是「该藏」的口径(见 index.js 的注释:visible 会误藏
1051
- // 所有非 surface 事件行,如 tool/call)
1052
- if (m.reverted === true) reverted.add(m.seq);
1043
+ if (m.visible === true) {
1044
+ visible.add(m.seq);
1045
+ if (m.turn != null) visibleTurns.add(String(m.turn));
1046
+ }
1053
1047
  }
1054
1048
  // turnEnd 优先:它对「该 turn 内一条消息都没有」的情况仍然有效
1055
1049
  if (turnEnd) for (const k in turnEnd) if (Object.prototype.hasOwnProperty.call(turnEnd, k)) byTurn.set(k, turnEnd[k])
1056
- return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, visible, reverted, fences, dupMessageId, total: list.length };
1050
+ return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, visible, visibleTurns, dupMessageId, total: list.length };
1057
1051
  };
1058
1052
 
1059
1053
  /** node-key → { seq, all }(all = 同 id 的全部 seq,处理 restore 重放副本)。 */
@@ -1067,15 +1061,15 @@ window.__ModuleLoader__.load({
1067
1061
  case 'trajectory-note': {
1068
1062
  const hit = idx.byMessageId.get(id)
1069
1063
  if (hit === undefined) return null
1070
- return { seq: hit, all: idx.dupByMessageId.get(id) || [hit] }
1064
+ return { kind: parsed.kind, seq: hit, all: idx.dupByMessageId.get(id) || [hit] }
1071
1065
  }
1072
1066
  case 'assistant-step': {
1073
1067
  const hit = idx.byTurnStep.get(id)
1074
- return hit === undefined ? null : { seq: hit, all: [hit] }
1068
+ return hit === undefined ? null : { kind: parsed.kind, seq: hit, all: [hit] }
1075
1069
  }
1076
1070
  case 'tool-call': {
1077
1071
  const hit = idx.byCallId.get(id)
1078
- return hit === undefined ? null : { seq: hit, all: [hit] }
1072
+ return hit === undefined ? null : { kind: parsed.kind, seq: hit, all: [hit] }
1079
1073
  }
1080
1074
  case 'turn-tail':
1081
1075
  case 'turn-process':
@@ -1083,7 +1077,9 @@ window.__ModuleLoader__.load({
1083
1077
  case 'turn-max-tokens':
1084
1078
  case 'model-retry': {
1085
1079
  const hit = idx.byTurn.get(id)
1086
- return hit === undefined ? null : { seq: hit, all: [hit] }
1080
+ // 这类是**回合 chrome**,id 就是 turn 号 —— 判据要按「这个 turn 还有
1081
+ // 没有可见消息」而不是按 seq,因为 turn/end 本身多半不在 surface 上。
1082
+ return hit === undefined ? null : { kind: parsed.kind, seq: hit, all: [hit], turn: id }
1087
1083
  }
1088
1084
  default:
1089
1085
  return null; // compaction / command / workflow-run 等不参与
@@ -1105,16 +1101,21 @@ window.__ModuleLoader__.load({
1105
1101
  var hidden = 0, unresolved = 0, shown = 0;
1106
1102
  for (var i = 0; i < rows.length; i++) {
1107
1103
  var row = rows[i];
1108
- // ⚠️ seqOfNodeKey 返回 `{ seq, all }`(all = 同 id 的全部 seq,
1109
- // 处理 restore 重放副本),不是标量。曾经在多 seq 改造时漏改这里,
1110
- // 导致 `{...} >= f` 恒为 false → **静默一个都不藏,也不报错**。
1104
+ // 0.9.4 判据(见下):**模型 surface**。此前用 revertFences 塌缩成
1105
+ // `seq >= min(fences)`,在一个有 19 个**离散** marker 的会话里
1106
+ // min fence=10 → seq>=10 全藏 → 4792/4793 全被隐藏 → 正文几乎空白
1107
+ // (截图复现)。改用 visible 后与宿主 visibleCount 精确吻合。
1111
1108
  var hit = seqOfNodeKey(row.getAttribute('data-chat-node-key'), idx);
1112
1109
  if (hit === null) { unresolved++; row.removeAttribute('hidden'); continue; }
1113
- var cands = (hit.all && hit.all.length) ? hit.all : [hit.seq];
1114
- var isReverted = idx.fences.length
1115
- ? cands.some(function (q) { return idx.fences.some(function (f) { return q >= f; }); })
1116
- : cands.some(function (q) { return idx.reverted.has(q); });
1117
- if (isReverted) { hidden++; row.setAttribute('hidden', 'until-found'); }
1110
+ var shouldHide;
1111
+ if (hit.turn !== undefined) {
1112
+ // 回合 chrome:这个 turn 还有可见消息就露出来
1113
+ shouldHide = !idx.visibleTurns.has(String(hit.turn));
1114
+ } else {
1115
+ // 消息类:直接问「它在模型 surface 上吗」
1116
+ shouldHide = !idx.visible.has(hit.seq);
1117
+ }
1118
+ if (shouldHide) { hidden++; row.setAttribute('hidden', 'until-found'); }
1118
1119
  else { shown++; row.removeAttribute('hidden'); }
1119
1120
  }
1120
1121
  return { hidden, unresolved, shown };
package/src/index.js CHANGED
@@ -298,22 +298,11 @@ async function opsList(targetCtx, sessionId) {
298
298
  }));
299
299
  }
300
300
 
301
- // 0.9.1:**reverted** = opencode 语义「回撤边界及其后」。
302
- //
303
- // ⚠️ 不能用 `visible` 当隐藏判据 —— `visible` 是「是否在模型 surface 上」,
304
- // 而 tool/call / turn/* / model/* **根本不是 surface 节点**(SURFACE_TYPES 只有
305
- // system|user|developer|assistant/message 与 tool/result),它们永远 visible=false。
306
- // 照抄会把**所有**工具调用行永久藏掉(实测 3/3 全被误藏)。
307
- //
308
- // 正确口径:每个**未恢复**的回撤标记 range.start 之后的全部 seq 都算已回撤 ——
309
- // 这正是 opencode 的 `messages.slice(0, boundaryIndex)`(边界自己也不显示)。
310
- const revertFences = messages
311
- .filter((m) => m && m.marker === true && m.restoreComplete === false
312
- && m.range && typeof m.range.start === 'number')
313
- .map((m) => m.range.start);
314
- for (const m of messages) {
315
- m.reverted = typeof m.seq === 'number' && revertFences.some((start) => m.seq >= start);
316
- }
301
+ // 0.9.4:原先这里发 `revertFences`(= 未恢复 marker 的 range.start 列表)和
302
+ // 逐条 `m.reverted = seq >= 某个 fence`。**两样都已删除**,因为它们把 19 个
303
+ // **离散** marker 区间塌缩成「seq >= 最小值」,在一个会话里导致
304
+ // 4792/4793 条被判为已回滚 → 客户端全藏 → 正文几乎空白(截图复现)。
305
+ // 正确判据是 `m.visible`(模型 surface),实测与下面的 visibleCount 精确吻合。
317
306
  // 0.9.1:回合页脚/过程条的 node-key id 就是 turn 号,而它们**不是消息**,
318
307
  // 无法用「首个带 turn 字段的消息」定位(该 turn 内可能一条消息都没有,
319
308
  // 实测 turn 2 就映射不到)。这里直接给 turn/start 的 seq 映射表。
@@ -333,7 +322,6 @@ async function opsList(targetCtx, sessionId) {
333
322
  total: messages.length,
334
323
  visibleCount,
335
324
  turnEndSeq,
336
- revertFences,
337
325
  messages,
338
326
  };
339
327
  }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * 0.9.4 transcript 隐藏判据 —— 回归「正文几乎空白」事故。
3
+ *
4
+ * 事故:会话有 19 个**离散** marker([9,9]、[10,4368]、[6830,8573] …),
5
+ * 服务端把它们塌缩成 revertFences = range.start 的列表 [10, 6830, 9829, 11429],
6
+ * 客户端判据 `seq >= min(fences)` 等价于 `seq >= 10`,
7
+ * 于是 4792/4793 条全被 hidden —— 截图里正文几乎空白。
8
+ *
9
+ * 正确判据 = 模型 surface(`m.visible`)。实测它与宿主 visibleCount 精确吻合:
10
+ * 156b9ecf: total 4793, visibleCount 2083 → !visible 应为 2710
11
+ * 4e10c1a2: total 5038, visibleCount 464 → !visible 应为 4574
12
+ */
13
+ import test from 'node:test'
14
+ import assert from 'node:assert/strict'
15
+
16
+ /** 与 client.js 同构:隐藏判据 */
17
+ function decide(hit, idx) {
18
+ if (hit === null) return null // 查不到 → 保持可见
19
+ if (hit.turn !== undefined) return !idx.visibleTurns.has(String(hit.turn))
20
+ return !idx.visible.has(hit.seq)
21
+ }
22
+
23
+ test('回归:fence 塌缩会把离散区间放大成「seq >= 最小值」', () => {
24
+ const fences = [10, 6830, 9829, 11429] // 实测(19 个 marker 的 range.start)
25
+ const total = 4793
26
+ const hiddenByFence = Array.from({ length: total }, (_, i) => i + 7).filter(s => fences.some(f => s >= f))
27
+ // seq 从 7 起;min fence=10 → 几乎全中
28
+ assert.ok(hiddenByFence.length >= 4790,
29
+ `fence 塌缩藏了 ${hiddenByFence.length}/${total} —— 正文必然空白`)
30
+ })
31
+
32
+ test('正确判据与宿主 visibleCount 精确吻合(会话 156b9ecf)', () => {
33
+ const total = 4793, visibleCount = 2083
34
+ // 构造:前 visibleCount 条可见,其余被遮蔽
35
+ const visible = new Set()
36
+ for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
37
+ const hidden = Array.from({ length: total }, (_, i) => i + 7).filter(s => !visible.has(s))
38
+ assert.equal(hidden.length, total - visibleCount, 'hidden 必须等于 total - visibleCount')
39
+ assert.equal(total - hidden.length, visibleCount)
40
+ })
41
+
42
+ test('第二条会话同样吻合(4e10c1a2)', () => {
43
+ const total = 5038, visibleCount = 464
44
+ const visible = new Set()
45
+ for (let i = 0; i < visibleCount; i++) visible.add(i + 7)
46
+ const hidden = Array.from({ length: total }, (_, i) => i + 7).filter(s => !visible.has(s))
47
+ assert.equal(hidden.length, 4574)
48
+ })
49
+
50
+ test('回合 chrome 按「本 turn 有无可见消息」判,不按 seq', () => {
51
+ const idx = { visible: new Set([7]), visibleTurns: new Set(['1']) }
52
+ // turn 1 有可见消息 → chrome 露出(即使 turn/end 的 seq 不在 surface 上)
53
+ assert.equal(decide({ kind: 'turn-tail', seq: 48, turn: '1' }, idx), false)
54
+ // turn 2 没有可见消息 → chrome 隐藏
55
+ assert.equal(decide({ kind: 'turn-tail', seq: 57, turn: '2' }, idx), true)
56
+ // 无可见消息的 turn 一律藏
57
+ const empty = { visible: new Set(), visibleTurns: new Set() }
58
+ assert.equal(decide({ kind: 'turn-process', seq: 9, turn: '9' }, empty), true)
59
+ })
60
+
61
+ test('消息类:在 surface 上就露,不在就藏', () => {
62
+ const idx = { visible: new Set([8, 18]), visibleTurns: new Set() }
63
+ assert.equal(decide({ kind: 'input-message', seq: 8, all: [8] }, idx), false, '在 surface → 露')
64
+ assert.equal(decide({ kind: 'input-message', seq: 54, all: [54] }, idx), true, '不在 → 藏')
65
+ assert.equal(decide({ kind: 'tool-call', seq: 19, all: [19] }, idx), true)
66
+ assert.equal(decide({ kind: 'tool-call', seq: 18, all: [18] }, idx), false, 'tool/call 也可能在 surface 上(实测 957 条 visible)')
67
+ })
68
+
69
+ test('查不到一律保持可见(绝不误藏)', () => {
70
+ const idx = { visible: new Set([1]), visibleTurns: new Set() }
71
+ assert.equal(decide(null, idx), null)
72
+ })