@240xu/dsh-message-ops 0.9.0 → 0.9.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 +69 -0
- package/package.json +1 -1
- package/src/client.js +197 -34
- package/src/index.js +40 -1
- package/src/session-file.js +29 -1
- package/test/revert-contract.test.js +2 -2
- package/test/surface-visibility.test.js +166 -0
package/README.md
CHANGED
|
@@ -484,3 +484,72 @@ opencode 实测:展开行 = 每条被遮蔽消息一行,点行**只恢复该
|
|
|
484
484
|
|
|
485
485
|
`surfaceOp: replace` 的 notice **不会**渲染成对话行(`dsh-client-ui-chat/lib/client.js:9370`
|
|
486
486
|
只渲染 `surfaceOp==="append"`),这是设计如此 —— 回撤提示由输入框上方的贴条负责。
|
|
487
|
+
|
|
488
|
+
---
|
|
489
|
+
|
|
490
|
+
## 0.9.1 / 0.9.2 · 回滚「真的生效了」
|
|
491
|
+
|
|
492
|
+
### 问题:回滚对用户是无效的(0.9.0 及以前)
|
|
493
|
+
|
|
494
|
+
浏览器实测发现:点回滚后 **dock 计数变了,但消息在界面上���行没变**。
|
|
495
|
+
|
|
496
|
+
根因(源码 + 实测双重证实):**dsh 刻意把「模型可见」和「人类 transcript」分成两套**。
|
|
497
|
+
`surface.d.ts:51-63` 明写:
|
|
498
|
+
|
|
499
|
+
> *Append-origin events are that transcript's durable source material; **replacement copies stay model-only**.*
|
|
500
|
+
|
|
501
|
+
即 `surfaceOp: {op:'replace'}` 只折叠**模型 surface**(实测 393→13 节点,模型侧确实生效),
|
|
502
|
+
但前端 transcript **不施加遮蔽**,被遮蔽消息照常渲染且完全可见 —— 连我们的 replace
|
|
503
|
+
marker 自身都不渲染(`systemMessageDefinition.buildViewNode` 对 `surfaceOp !== 'append'`
|
|
504
|
+
直接 `return null`,`dsh-client-ui-chat/lib/client.js:9370`)。
|
|
505
|
+
|
|
506
|
+
所以之前那句「回滚已完成」是**误判**:只验了日志写入与 dock 计数,没验用户眼���。
|
|
507
|
+
|
|
508
|
+
### 解法:渲染层按 node-key 精确映射
|
|
509
|
+
|
|
510
|
+
每一行渲染节点带 `data-chat-node-key`,形态 `<len>:<kind><id>`,而 `<id>` 就是日志事件的字段:
|
|
511
|
+
|
|
512
|
+
| 行 kind | `<id>` | 查表 |
|
|
513
|
+
|---|---|---|
|
|
514
|
+
| `user` / `steering` | messageId | `byMessageId` |
|
|
515
|
+
| `assistant-step` | `<turn>:<step>` | `byTurnStep` |
|
|
516
|
+
| `tool-call` | callId | `byCallId` |
|
|
517
|
+
| `turn-tail` / `turn-process` / `turn-error` | turn 号 | `byTurn` ← **turn/end 的 seq** |
|
|
518
|
+
| `developer-message` | messageId | `byMessageId` |
|
|
519
|
+
|
|
520
|
+
配合服务端的 `revertFences` / `turnEndSeq`,客户端做**精确查表 + fence 比较**,
|
|
521
|
+
命中则 `row.setAttribute('hidden', 'until-found')`。
|
|
522
|
+
|
|
523
|
+
`hidden="until-found"` 与宿主 `useSearchableHidden` 同款:Ctrl+F 仍能搜到,
|
|
524
|
+
且宿主的分页锚点选择器 `[data-chat-paging-anchor]:not(:empty):not([hidden])`
|
|
525
|
+
会自动跳过,滚动恢复不受影响。
|
|
526
|
+
|
|
527
|
+
### 三个踩过的坑(都有回归测试锁住)
|
|
528
|
+
|
|
529
|
+
1. **不能用 `visible` 当隐藏判据**。`visible` 是「是否在模型 surface 上」,而
|
|
530
|
+
`tool/call` / `turn/*` / `model/*` **根本不是 surface 节点**,它们永远 `visible=false`
|
|
531
|
+
—— 照抄会把**所有**工具调用行永久藏掉(实测 3/3 全被误藏)。
|
|
532
|
+
正确口径是 `reverted`(= opencode 的「边界及其后」:`messages.slice(0, boundaryIndex)`)。
|
|
533
|
+
2. **回合页脚不是消息**,锚到 `turn/end` 的 seq,永远不在 `reverted` 集合里,
|
|
534
|
+
所以判据必须**直接对 fence 数组比较**。
|
|
535
|
+
且要用 `turn/end` 而非 `turn/start` —— 回撤边界常落在某个 turn **内部**。
|
|
536
|
+
3. **旧的 40 字符前缀包含匹配**实测 4 条错 2 条(「继续」命中「继续,…」),
|
|
537
|
+
已整体删除,改为 node-key 查表。
|
|
538
|
+
|
|
539
|
+
### 实测结果
|
|
540
|
+
|
|
541
|
+
```
|
|
542
|
+
回滚后: hidden 5(user + turn-process×2 + turn-tail×2)
|
|
543
|
+
被回滚消息 disappears from DOM ✅
|
|
544
|
+
该轮之后的助手回答也消失 ✅
|
|
545
|
+
更早的消息完好 ✅
|
|
546
|
+
恢复后: hidden 0,dock 消失,消息全部回来 ✅
|
|
547
|
+
查表命中率: unresolved 0 / mismatch 0 / hiddenButNotReverted 0 ✅
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### 已知限制
|
|
551
|
+
|
|
552
|
+
- **restore 是「重放」语义**(dsh 无 unshadow 原语,见 README 上文的机制说明),
|
|
553
|
+
所以「恢复后再回滚」的复合场景下,重放副本的行可能不被识别为 user 行。
|
|
554
|
+
- `restore` 后界面需要触发刷新才更新(已接 `__refreshSurface` 钩子 + MutationObserver,
|
|
555
|
+
但极少数情况下仍需重载页面)。
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -143,7 +143,12 @@ window.__ModuleLoader__.load({
|
|
|
143
143
|
var __sessionsSvc = null
|
|
144
144
|
var __inputActions = null // 0.4.0: 从 session 槽捕获(InputActions.setDraft → composer 回填)
|
|
145
145
|
var __uiWorkspace = null
|
|
146
|
-
var __currentSessionId = null
|
|
146
|
+
var __currentSessionId = null
|
|
147
|
+
// 0.9.1:视图重建后刷新 transcript 可见性的钩子。
|
|
148
|
+
// 之前 restore 路径里直接调 scan(),但 scan 定义在注入器的闭包里,
|
|
149
|
+
// dock 组件根本取不到 —— `typeof scan === 'function'` 静默跳过了,
|
|
150
|
+
// 结果「恢复后消息回来了但仍被藏 / dock 计数不变」,必须重载页面才对。
|
|
151
|
+
var __refreshSurface = null // 0.6.0:会话视图会话 id(RevertDock/MsgSlotActions 刷新)
|
|
147
152
|
// messageId→seq 索引缓存(每会话一次拉取;0.3.0 assistant-actions 槽用)
|
|
148
153
|
var __seqIndexCache = new Map()
|
|
149
154
|
// messageId→全文缓存(0.5.0 消息引用用)
|
|
@@ -840,6 +845,12 @@ window.__ModuleLoader__.load({
|
|
|
840
845
|
try {
|
|
841
846
|
if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(sessionId)
|
|
842
847
|
} catch { /* fallthrough */ }
|
|
848
|
+
// 0.9.1:重放的消息带**新 seq**,被恢复的行必须解除 hidden,
|
|
849
|
+
// 同时 dock 的权威 marker 列表要重拉。
|
|
850
|
+
try { window.dispatchEvent(new Event(CHANGED_EVENT)) } catch { /* non-browser */ }
|
|
851
|
+
// 0.9.1:重放的消息带**新 seq**,被恢复的行必须解除 hidden;
|
|
852
|
+
// 同时 dock 的 marker 列表要按权威数据重算(restoreComplete 会变 true)。
|
|
853
|
+
for (const delay of [150, 700, 1600]) setTimeout(() => { if (__refreshSurface) __refreshSurface(sessionId) }, delay)
|
|
843
854
|
const fresh = await fetch('/api/message-ops/messages?sessionId=' + encodeURIComponent(sessionId)).then((r) => (r.ok ? r.json() : null)).catch(() => null)
|
|
844
855
|
if (fresh && fresh.ok) setMarkers(activeMarkers(fresh.messages || []))
|
|
845
856
|
})
|
|
@@ -923,7 +934,7 @@ window.__ModuleLoader__.load({
|
|
|
923
934
|
var rowsCache = { sid: null, rows: null, at: 0 }
|
|
924
935
|
var norm = (x) => String(x || '').replace(/\s+/g, '')
|
|
925
936
|
var fetchRows = (sid) => {
|
|
926
|
-
if (rowsCache.sid === sid && rowsCache.rows && Date.now() - rowsCache.at < 8000) return Promise.resolve(rowsCache
|
|
937
|
+
if (rowsCache.sid === sid && rowsCache.rows && Date.now() - rowsCache.at < 8000) return Promise.resolve(rowsCache)
|
|
927
938
|
return fetch('/api/message-ops/messages?sessionId=' + encodeURIComponent(sid))
|
|
928
939
|
.then((r) => (r.ok ? r.json() : null))
|
|
929
940
|
.then((d) => {
|
|
@@ -943,11 +954,154 @@ window.__ModuleLoader__.load({
|
|
|
943
954
|
.filter((m) => m && m.role === 'user')
|
|
944
955
|
.map((m) => ({ seq: m.seq, sn: norm(m.snippet), raw: String(m.snippet == null ? '' : m.snippet) }))
|
|
945
956
|
.filter((r) => r.sn && !injected(r.raw))
|
|
946
|
-
|
|
947
|
-
|
|
957
|
+
// 0.9.1:同时产出 node-key 查表索引(隐藏 transcript 用)
|
|
958
|
+
const idx = buildIndex(d)
|
|
959
|
+
rowsCache = { sid: sid, rows: rows, idx: idx, at: Date.now() }
|
|
960
|
+
return rowsCache
|
|
948
961
|
})
|
|
949
|
-
.catch(() => rowsCache.sid === sid ? rowsCache
|
|
962
|
+
.catch(() => (rowsCache.sid === sid ? rowsCache : null))
|
|
950
963
|
}
|
|
964
|
+
|
|
965
|
+
// ═══ 0.9.1 宿主 node-key ↔ 日志 seq 的精确映射 ═══════════════════════════
|
|
966
|
+
//
|
|
967
|
+
// 背景(浏览器实测 + 源码核实):dsh 的 transcript **刻意不做 surface 折叠**。
|
|
968
|
+
// 官方 surface.d.ts:51-63 明写 "replacement copies stay model-only" ——
|
|
969
|
+
// 我们写的 surfaceOp replace 只折叠**模型 surface**(实测 393→13 节点),
|
|
970
|
+
// 被遮蔽的消息在人类 transcript 里**照常渲染且完全可见**,连 marker 本身
|
|
971
|
+
// 都不渲染(systemMessageDefinition 对 surfaceOp !== 'append' 直接 return null)。
|
|
972
|
+
// 所以要让回滚「看得见」,只能在渲染层处理。
|
|
973
|
+
//
|
|
974
|
+
// 好消息:每一行的 `data-chat-node-key` 形如 `<len>:<kind><id>`,而 <id>
|
|
975
|
+
// 就是日志事件的字段(user → messageId、assistant-step → `<turn>:<step>`、
|
|
976
|
+
// tool-call → callId、turn-* → turn 号),客户端本来就已经建了
|
|
977
|
+
// messageId → seq 索引(__seqIndexCache)。**精确查表,零启发式。**
|
|
978
|
+
//
|
|
979
|
+
// 旧实现用「40 字符前缀包含匹配」配对 DOM 与日志,实测 4 条里错 2 条
|
|
980
|
+
// ("继续" 命中了 "继续,…"),已整体删除。
|
|
981
|
+
var parseNodeKey = (k) => {
|
|
982
|
+
var m = /^(\d+):/.exec(k || '');
|
|
983
|
+
if (!m) return null;
|
|
984
|
+
var digits = m[1].length;
|
|
985
|
+
var kindLen = Number(m[1]);
|
|
986
|
+
var start = digits + 1;
|
|
987
|
+
if (!Number.isSafeInteger(kindLen) || kindLen <= 0 || k.length < start + kindLen) return null;
|
|
988
|
+
return { kind: k.slice(start, start + kindLen), id: k.slice(start + kindLen) };
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
/**
|
|
992
|
+
* 从 /api/message-ops/messages 的响应构建全部查表。
|
|
993
|
+
* 三条铁律:① 索引只认唯一 id;② 缺数据一律返回 undefined(调用方保持可见);
|
|
994
|
+
* ③ visible 集合只收显式 true。
|
|
995
|
+
*/
|
|
996
|
+
var buildIndex = (payload) => {
|
|
997
|
+
var list = (payload && payload.messages) || []
|
|
998
|
+
// 0.9.1:回合页脚/过程条不是消息,用 turn/end 的 seq 定位(边界常落在 turn 内部,
|
|
999
|
+
// 用 turn/start 会漏判该回合的页脚)
|
|
1000
|
+
var turnEnd = (payload && payload.turnEndSeq) || null;
|
|
1001
|
+
var byMessageId = new Map();
|
|
1002
|
+
var byTurnStep = new Map();
|
|
1003
|
+
var byCallId = new Map();
|
|
1004
|
+
var byTurn = new Map();
|
|
1005
|
+
var visible = new Set();
|
|
1006
|
+
var reverted = new Set();
|
|
1007
|
+
var dupMessageId = 0
|
|
1008
|
+
var dupByMessageId = new Map();
|
|
1009
|
+
// 回合页脚/过程条锚到 turn/end 的 seq —— 那是**非消息**事件,永远不会出现在
|
|
1010
|
+
// reverted 集合里,所以隐藏判据必须直接对 fence 比较,不能用集合成员。
|
|
1011
|
+
var fences = Array.isArray(payload && payload.revertFences)
|
|
1012
|
+
? payload.revertFences.filter(function (n) { return Number.isSafeInteger(n); })
|
|
1013
|
+
: [];
|
|
1014
|
+
for (var i = 0; i < list.length; i++) {
|
|
1015
|
+
var m = list[i];
|
|
1016
|
+
if (!m || typeof m.seq !== 'number') continue;
|
|
1017
|
+
if (m.id != null) {
|
|
1018
|
+
var k = String(m.id);
|
|
1019
|
+
// restore 是「重放」语义 → 原件与副本可能同 id 并存。
|
|
1020
|
+
// 0.9.1:不再「保留最早」,而是收集**全部** seq,隐藏判据取「任一 seq 越过
|
|
1021
|
+
// fence」。回放副本才是当前 surface 上的那条,用最早的 seq 判会让
|
|
1022
|
+
// 「恢复后再回滚」漏藏(实测 fence=70、副本 seq=70、原件 seq=54)。
|
|
1023
|
+
if (byMessageId.has(k)) {
|
|
1024
|
+
dupMessageId++
|
|
1025
|
+
const arr = dupByMessageId.get(k) || [byMessageId.get(k)]
|
|
1026
|
+
arr.push(m.seq)
|
|
1027
|
+
dupByMessageId.set(k, arr)
|
|
1028
|
+
byMessageId.set(k, m.seq) // 指向最新的
|
|
1029
|
+
} else { byMessageId.set(k, m.seq) }
|
|
1030
|
+
byMessageId.set(k, m.seq);
|
|
1031
|
+
}
|
|
1032
|
+
if (m.type === 'assistant/message' && m.turn != null && m.step != null) {
|
|
1033
|
+
byTurnStep.set(m.turn + ':' + m.step, m.seq);
|
|
1034
|
+
}
|
|
1035
|
+
if (m.type === 'tool/call' && m.id != null) byCallId.set(String(m.id), m.seq);
|
|
1036
|
+
if (m.turn != null && !byTurn.has(String(m.turn))) byTurn.set(String(m.turn), m.seq);
|
|
1037
|
+
if (m.visible === true) visible.add(m.seq);
|
|
1038
|
+
// 0.9.1:reverted 才是「该藏」的口径(见 index.js 的注释:visible 会误藏
|
|
1039
|
+
// 所有非 surface 事件行,如 tool/call)
|
|
1040
|
+
if (m.reverted === true) reverted.add(m.seq);
|
|
1041
|
+
}
|
|
1042
|
+
// turnEnd 优先:它对「该 turn 内一条消息都没有」的情况仍然有效
|
|
1043
|
+
if (turnEnd) for (const k in turnEnd) if (Object.prototype.hasOwnProperty.call(turnEnd, k)) byTurn.set(k, turnEnd[k])
|
|
1044
|
+
return { byMessageId, dupByMessageId, byTurnStep, byCallId, byTurn, visible, reverted, fences, dupMessageId, total: list.length };
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
/** node-key → { seq, all }(all = 同 id 的全部 seq,处理 restore 重放副本)。 */
|
|
1048
|
+
var seqOfNodeKey = (key, idx) => {
|
|
1049
|
+
var parsed = parseNodeKey(key);
|
|
1050
|
+
if (!parsed || !idx) return null;
|
|
1051
|
+
var id = parsed.id;
|
|
1052
|
+
switch (parsed.kind) {
|
|
1053
|
+
case 'input-message': // user / steering / turn-trigger
|
|
1054
|
+
case 'developer-message':
|
|
1055
|
+
case 'trajectory-note': {
|
|
1056
|
+
const hit = idx.byMessageId.get(id)
|
|
1057
|
+
if (hit === undefined) return null
|
|
1058
|
+
return { seq: hit, all: idx.dupByMessageId.get(id) || [hit] }
|
|
1059
|
+
}
|
|
1060
|
+
case 'assistant-step': {
|
|
1061
|
+
const hit = idx.byTurnStep.get(id)
|
|
1062
|
+
return hit === undefined ? null : { seq: hit, all: [hit] }
|
|
1063
|
+
}
|
|
1064
|
+
case 'tool-call': {
|
|
1065
|
+
const hit = idx.byCallId.get(id)
|
|
1066
|
+
return hit === undefined ? null : { seq: hit, all: [hit] }
|
|
1067
|
+
}
|
|
1068
|
+
case 'turn-tail':
|
|
1069
|
+
case 'turn-process':
|
|
1070
|
+
case 'turn-error':
|
|
1071
|
+
case 'turn-max-tokens':
|
|
1072
|
+
case 'model-retry': {
|
|
1073
|
+
const hit = idx.byTurn.get(id)
|
|
1074
|
+
return hit === undefined ? null : { seq: hit, all: [hit] }
|
|
1075
|
+
}
|
|
1076
|
+
default:
|
|
1077
|
+
return null; // compaction / command / workflow-run 等不参与
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
|
|
1081
|
+
/**
|
|
1082
|
+
* 把 transcript 行按模型 surface 可见性打上 hidden。
|
|
1083
|
+
* 用 `hidden="until-found"`(与宿主 useSearchableHidden 同款):Ctrl+F 仍能搜到,
|
|
1084
|
+
* 且宿主的分页锚点选择器 `[data-chat-paging-anchor]:not(:empty):not([hidden])`
|
|
1085
|
+
* 会自动跳过这些行,滚动恢复不受影响。
|
|
1086
|
+
*
|
|
1087
|
+
* ⚠️ `seq == null` ⇒ **移除 hidden**(保持可见)。把「查不到」当成「被遮蔽」
|
|
1088
|
+
* 是唯一会误藏用户内容的路径。
|
|
1089
|
+
*/
|
|
1090
|
+
var applySurfaceVisibility = (root, idx) => {
|
|
1091
|
+
if (!idx) return { hidden: 0, unresolved: 0, shown: 0 };
|
|
1092
|
+
var rows = (root || document).querySelectorAll('[data-chat-node-key]');
|
|
1093
|
+
var hidden = 0, unresolved = 0, shown = 0;
|
|
1094
|
+
for (var i = 0; i < rows.length; i++) {
|
|
1095
|
+
var row = rows[i];
|
|
1096
|
+
var seq = seqOfNodeKey(row.getAttribute('data-chat-node-key'), idx);
|
|
1097
|
+
if (seq === null) { unresolved++; row.removeAttribute('hidden'); continue; }
|
|
1098
|
+
var isReverted = idx.fences.length ? idx.fences.some(function (f) { return seq >= f; }) : idx.reverted.has(seq)
|
|
1099
|
+
if (isReverted) { hidden++; row.setAttribute('hidden', 'until-found'); }
|
|
1100
|
+
else { shown++; row.removeAttribute('hidden'); }
|
|
1101
|
+
}
|
|
1102
|
+
return { hidden, unresolved, shown };
|
|
1103
|
+
};
|
|
1104
|
+
|
|
951
1105
|
// 用户消息锚点:优先官方 Sixlwa_userRow(含 .xzv4MW_actions),回退 flow-kind 属性
|
|
952
1106
|
// 锚点 = 每条用户消息自己的容器(一个块只算一次):
|
|
953
1107
|
// 从官方动作行 .xzv4MW_actions 反查最近的消息容器,最贴近"一条消息"的粒度。
|
|
@@ -992,37 +1146,33 @@ window.__ModuleLoader__.load({
|
|
|
992
1146
|
btn.setAttribute('data-mops-unmatched', '1')
|
|
993
1147
|
}
|
|
994
1148
|
}
|
|
995
|
-
var assignSeqs = (sid,
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
for (const b of blocks) {
|
|
1149
|
+
var assignSeqs = (sid, cache) => {
|
|
1150
|
+
const idx = cache && cache.idx
|
|
1151
|
+
if (!idx) return
|
|
1152
|
+
// 0.9.1:node-key 查表,替代旧的 40 字符前缀包含匹配(实测 4 条错 2 条)
|
|
1153
|
+
for (const b of userAnchors()) {
|
|
1001
1154
|
b.removeAttribute('data-mops-seq')
|
|
1002
1155
|
b.removeAttribute('data-mops-unmatched')
|
|
1003
|
-
const
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
const a = (rows[j].sn || '').slice(0, 40)
|
|
1007
|
-
const b = (head || '').slice(0, 40)
|
|
1008
|
-
if (a && b && (head.includes(a) || rows[j].sn.includes(b))) { hit = j; break }
|
|
1009
|
-
}
|
|
1010
|
-
if (hit >= 0) { b.setAttribute('data-mops-seq', String(rows[hit].seq)); ri = hit + 1 }
|
|
1011
|
-
syncEnabled(b, b.querySelector && b.querySelector('.mopsUserRevert'))
|
|
1012
|
-
if (false) {
|
|
1013
|
-
// 0.9.0:配不上就**如实标记**而不是留一个点了才报错的装饰按钮。
|
|
1014
|
-
// 已知局限:配对是前 40 字子串包含,模型自己写的摘要与宿主渲染文本
|
|
1015
|
-
// 措辞漂移(实测 playwright/playright)就会失配。
|
|
1016
|
-
missed++
|
|
1156
|
+
const hit = seqOfNodeKey(b.getAttribute('data-chat-node-key'), idx)
|
|
1157
|
+
const seq = hit === null ? null : hit.seq
|
|
1158
|
+
if (seq === null || seq === undefined) {
|
|
1017
1159
|
b.setAttribute('data-mops-unmatched', '1')
|
|
1160
|
+
syncEnabled(b, b.querySelector && b.querySelector('.mopsUserRevert'))
|
|
1161
|
+
continue
|
|
1018
1162
|
}
|
|
1163
|
+
b.setAttribute('data-mops-seq', String(seq))
|
|
1164
|
+
syncEnabled(b, b.querySelector && b.querySelector('.mopsUserRevert'))
|
|
1019
1165
|
}
|
|
1020
|
-
return missed
|
|
1021
1166
|
}
|
|
1022
1167
|
// 按钮可用性在 assignSeqs 内部**同步**更新(与打标同一次循环)——
|
|
1023
1168
|
// 之前放在 fetch 回调里做事后全局扫描,会与 DOM 注入/重排竞态,导致
|
|
1024
1169
|
// 块已打上 seq 而按钮仍是禁用态(实测:block seq=[8,54],按钮 disabled=true)。
|
|
1025
|
-
|
|
1170
|
+
// 按钮可用性与 transcript 隐藏都在这里统一落地(同一批数据,避免竞态)
|
|
1171
|
+
var ensureAssign = (sid) => fetchRows(sid).then((cache) => {
|
|
1172
|
+
assignSeqs(sid, cache)
|
|
1173
|
+
if (cache && cache.idx) applySurfaceVisibility(document, cache.idx)
|
|
1174
|
+
return cache
|
|
1175
|
+
})
|
|
1026
1176
|
// 0.7.0:点击时先**同步完成**配对(打标是异步 fetch 的结果,
|
|
1027
1177
|
// 早先点在未打标的按钮上会静默失败 = 用户反馈的"点了没用")。
|
|
1028
1178
|
var runUserRevert = (btn, block) => {
|
|
@@ -1032,15 +1182,15 @@ window.__ModuleLoader__.load({
|
|
|
1032
1182
|
Promise.resolve()
|
|
1033
1183
|
.then(() => ensureAssign(sid))
|
|
1034
1184
|
.then(() => fetchRows(sid))
|
|
1035
|
-
.then((
|
|
1185
|
+
.then((cache) => {
|
|
1186
|
+
const rows = cache && cache.rows
|
|
1187
|
+
// 0.9.1:seq 来自 node-key 精确查表,**不再**做文本前缀匹配验证
|
|
1188
|
+
// (旧的前缀包含匹配实测 4 条错 2 条 —— "继续" 命中 "继续,…")。
|
|
1036
1189
|
const seq = Number(block.getAttribute('data-mops-seq'))
|
|
1037
|
-
const head = norm((block.innerText || '').slice(0, 240))
|
|
1038
1190
|
const row = rows && rows.find((r) => r.seq === seq)
|
|
1039
|
-
|
|
1040
|
-
const b2 = (head || '').slice(0, 40)
|
|
1041
|
-
if (!row || !head || !(head.includes(a2) || row.sn.includes(b2))) {
|
|
1191
|
+
if (!row) {
|
|
1042
1192
|
ensureAssign(sid)
|
|
1043
|
-
notifyDone(__t('errorPrefix') + 'revert target verification failed', 'error')
|
|
1193
|
+
notifyDone(__t('errorPrefix') + (__t('revert.unresolved') || 'revert target verification failed'), 'error')
|
|
1044
1194
|
return
|
|
1045
1195
|
}
|
|
1046
1196
|
btn.disabled = true
|
|
@@ -1058,6 +1208,10 @@ window.__ModuleLoader__.load({
|
|
|
1058
1208
|
try {
|
|
1059
1209
|
if (__uiWorkspace && typeof __uiWorkspace.openSession === 'function') __uiWorkspace.openSession(sid)
|
|
1060
1210
|
} catch { /* 视图重建失败不阻断 */ }
|
|
1211
|
+
// 0.9.1:视图重建后重新拉权威可见性并隐藏被遮蔽的行。
|
|
1212
|
+
// openSession 会重挂 DOM,旧节点上的 hidden 属性随之失效,必须重刷。
|
|
1213
|
+
if (rowsCache) rowsCache.at = 0
|
|
1214
|
+
for (const delay of [150, 700, 1600]) setTimeout(() => { if (__refreshSurface) __refreshSurface(sid) }, delay)
|
|
1061
1215
|
})
|
|
1062
1216
|
.catch((reason) => {
|
|
1063
1217
|
notifyDone(__t('errorPrefix') + (reason && reason.message ? reason.message : String(reason)), 'error')
|
|
@@ -1089,13 +1243,22 @@ window.__ModuleLoader__.load({
|
|
|
1089
1243
|
host.appendChild(btn)
|
|
1090
1244
|
return btn
|
|
1091
1245
|
}
|
|
1246
|
+
__refreshSurface = (sid) => {
|
|
1247
|
+
const id = sid || __currentSessionId
|
|
1248
|
+
if (!id) return
|
|
1249
|
+
if (rowsCache) rowsCache.at = 0 // 强制重取权威数据
|
|
1250
|
+
try { fetchRows(id).then((cache) => { if (cache && cache.idx) applySurfaceVisibility(document, cache.idx) }) } catch { /* noop */ }
|
|
1251
|
+
}
|
|
1092
1252
|
var scan = () => {
|
|
1093
1253
|
try {
|
|
1094
1254
|
const sid = __currentSessionId
|
|
1095
1255
|
if (!sid) return
|
|
1096
1256
|
const blocks = userAnchors()
|
|
1097
1257
|
for (const b of blocks) ensureBtn(b)
|
|
1098
|
-
|
|
1258
|
+
// 0.9.1:**始终**刷新(原先只在按钮未打标时刷)——
|
|
1259
|
+
// 打标后 transcript 再变化(openSession 重建 / 分页 / restore 重放)
|
|
1260
|
+
// 就再也不会重算隐藏,导致「界面停在旧状态,必须重载页面才对」。
|
|
1261
|
+
ensureAssign(sid)
|
|
1099
1262
|
} catch { /* 注入失败不影响会话 */ }
|
|
1100
1263
|
}
|
|
1101
1264
|
// 触发:hover / 滚动 / DOM 变化 / 变更事件
|
package/src/index.js
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
|
|
25
25
|
import { zstdCompressSync } from "node:zlib";
|
|
26
26
|
import { open as openFile, truncate as truncateFile, stat as statFile } from "node:fs/promises";
|
|
27
|
-
import { isSessionId, sessionIdVariants, findSessionDirs, readSessionFile, readSessionFileAsync, listMessages, computeShadowed, messageText } from "./session-file.js";
|
|
27
|
+
import { isSessionId, sessionIdVariants, findSessionDirs, readSessionFile, readSessionFileAsync, listMessages, computeShadowed, messageText, turnBounds } from "./session-file.js";
|
|
28
28
|
import { restoreNoticeText, planRevertFromEvents, planDeleteFromEvents, nextSeqFrom, buildMarkerEvent, buildRestoreNoticeEvent, buildReplayEvent, planNoticeWindow, noticeWindowPreamble, noticeWindowPostamble,
|
|
29
29
|
OpsError, planRevert, planDelete, planRestore, applyRestore, applySurfaceReplace,
|
|
30
30
|
restoreProgress, pendingRestoreTurns,
|
|
@@ -277,6 +277,15 @@ async function opsList(targetCtx, sessionId) {
|
|
|
277
277
|
const t = messageText(e);
|
|
278
278
|
if (t && t.trim()) seqText.set(e.seq, t.trim().replace(/\s+/g, " ").slice(0, 80));
|
|
279
279
|
}
|
|
280
|
+
// 0.9.1:**reverted** = opencode 语义「回撤边界及其后」。
|
|
281
|
+
//
|
|
282
|
+
// ⚠️ 不能用 `visible` 当隐藏判据 —— `visible` 是「是否在模型 surface 上」,
|
|
283
|
+
// 而 tool/call / turn/* / model/* **根本不是 surface 节点**(SURFACE_TYPES 只有
|
|
284
|
+
// system|user|developer|assistant/message 与 tool/result),它们永远 visible=false。
|
|
285
|
+
// 照抄会把**所有**工���调用行永久藏掉(实测 3/3 全被误藏)。
|
|
286
|
+
//
|
|
287
|
+
// 正确口径:每个活跃回撤标记的 range.start 之后的全部 seq 都算已回撤
|
|
288
|
+
// —— 这正是 opencode 的 `messages.slice(0, boundaryIndex)`(边界自己也不显示)。
|
|
280
289
|
for (const m of messages) {
|
|
281
290
|
if (!m || m.marker !== true) continue;
|
|
282
291
|
const prog = restoreProgress(events, m.seq);
|
|
@@ -288,6 +297,34 @@ async function opsList(targetCtx, sessionId) {
|
|
|
288
297
|
preview: seqText.get(t.turnSeq) || ('#' + t.turnSeq),
|
|
289
298
|
}));
|
|
290
299
|
}
|
|
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
|
+
}
|
|
317
|
+
// 0.9.1:回合页脚/过程条的 node-key id 就是 turn 号,而它们**不是消息**,
|
|
318
|
+
// 无法用「首个带 turn 字段的消息」定位(该 turn 内可能一条消息都没有,
|
|
319
|
+
// 实测 turn 2 就映射不到)。这里直接给 turn/start 的 seq 映射表。
|
|
320
|
+
const bounds = turnBounds(events);
|
|
321
|
+
// 用 **turn/end 的 seq** 而不是 turn/start:回撤边界常落在某个 turn **内部**
|
|
322
|
+
// (实测边界 seq 54 在 turn 2 的 [51..62] 之间)。用起始 seq 会漏判该回合的页脚,
|
|
323
|
+
// 用结束 seq 则「边界之前完全在前」的回合自然落选。
|
|
324
|
+
const turnEndSeq = {};
|
|
325
|
+
for (const [turn, seq] of bounds.ends) turnEndSeq[turn] = seq;
|
|
326
|
+
for (const [turn, seq] of bounds.starts) if (!(turn in turnEndSeq)) turnEndSeq[turn] = seq;
|
|
327
|
+
|
|
291
328
|
return {
|
|
292
329
|
ok: true,
|
|
293
330
|
session: { id: header.id, createdAt: header.createdAt, parentSession: header.parentSession ?? null },
|
|
@@ -295,6 +332,8 @@ async function opsList(targetCtx, sessionId) {
|
|
|
295
332
|
runningSource: __runningSource,
|
|
296
333
|
total: messages.length,
|
|
297
334
|
visibleCount,
|
|
335
|
+
turnEndSeq,
|
|
336
|
+
revertFences,
|
|
298
337
|
messages,
|
|
299
338
|
};
|
|
300
339
|
}
|
package/src/session-file.js
CHANGED
|
@@ -164,6 +164,29 @@ export function newSessionId() {
|
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
/** 从事件流提取消息级列表(user/assistant/system message + tool/call 摘要)。 */
|
|
167
|
+
/**
|
|
168
|
+
* turn 号 → 该 turn 首/末事件 seq 的映射。
|
|
169
|
+
*
|
|
170
|
+
* 0.9.1:宿主把「回合页脚/过程条/错误」渲染成 node-key 为 `9:turn-tail<turn>` /
|
|
171
|
+
* `12:turn-process<turn>` / `10:turn-error<turn>` 的行 —— 它们的 id 就是 turn 号,
|
|
172
|
+
* 不带 seq。回滚边界落在某个 turn 之后时,这些行也必须一起藏,否则会留下
|
|
173
|
+
* 「孤立的分支/复制按钮」,视觉上像"只回滚了一半"。
|
|
174
|
+
*
|
|
175
|
+
* @returns {{starts: Map<number, number>, ends: Map<number, number>}}
|
|
176
|
+
*/
|
|
177
|
+
export function turnBounds(events) {
|
|
178
|
+
const starts = new Map();
|
|
179
|
+
const ends = new Map();
|
|
180
|
+
for (const e of events) {
|
|
181
|
+
if (!e || typeof e.seq !== "number") continue;
|
|
182
|
+
const t = e.data && e.data.turn;
|
|
183
|
+
if (!Number.isSafeInteger(t)) continue;
|
|
184
|
+
if (e.type === "turn/start") starts.set(t, e.seq);
|
|
185
|
+
else if (e.type === "turn/end") ends.set(t, e.seq);
|
|
186
|
+
}
|
|
187
|
+
return { starts, ends };
|
|
188
|
+
}
|
|
189
|
+
|
|
167
190
|
export function listMessages(events) {
|
|
168
191
|
const messages = [];
|
|
169
192
|
for (const e of events) {
|
|
@@ -193,13 +216,18 @@ export function listMessages(events) {
|
|
|
193
216
|
? e.data.restoresSeq : null,
|
|
194
217
|
time: e.time ?? null,
|
|
195
218
|
turn: (e.data && e.data.turn) ?? null,
|
|
219
|
+
// 0.9.1:assistant 节点的宿主 node-key 是 `14:assistant-step<turn>:<step>`,
|
|
220
|
+
// 没有 step 就无法把渲染行映射回日志 seq → 回滚时助手回答藏不掉。
|
|
221
|
+
step: (e.data && e.data.step) ?? null,
|
|
196
222
|
});
|
|
197
223
|
} else if (e.type === "tool/call") {
|
|
198
224
|
const d = e.data || {};
|
|
199
225
|
const name = d.name || (d.call && d.call.name) || "tool";
|
|
200
226
|
messages.push({
|
|
201
227
|
seq: e.seq, type: "tool/call", role: "tool", snippet: `[tool] ${name}`,
|
|
202
|
-
time: e.time ?? null, turn: d.turn ?? null,
|
|
228
|
+
time: e.time ?? null, turn: d.turn ?? null, step: d.step ?? null,
|
|
229
|
+
// 0.9.1:工具节点的宿主 node-key 是 `9:tool-call<callId>`
|
|
230
|
+
id: d.callId ?? null,
|
|
203
231
|
});
|
|
204
232
|
}
|
|
205
233
|
}
|
|
@@ -15,8 +15,8 @@ import { zstdCompressSync } from 'node:zlib'
|
|
|
15
15
|
import { createRequire } from 'node:module'
|
|
16
16
|
import { join, dirname } from 'node:path'
|
|
17
17
|
|
|
18
|
-
import { decodeFrames, parseRows } from '/data/data/com.termux/files/home/dsh-plugins-src/
|
|
19
|
-
import { officialValidate } from '/data/data/com.termux/files/home/dsh-plugins-src/
|
|
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
20
|
import {
|
|
21
21
|
planNoticeWindow, noticeWindowPreamble, noticeWindowPostamble,
|
|
22
22
|
buildMarkerEvent, buildRestoreNoticeEvent, buildReplayEvent, planRestore, restoreProgress,
|
|
@@ -0,0 +1,166 @@
|
|
|
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
|
+
})
|