dsh-as-aistudio 0.5.14 → 0.5.16

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/CHANGELOG.md CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  ## 中文
6
6
 
7
+ **0.5.16** —— 跟进 dsh-log-trim 0.7.3(迟到 tool/result 归位,窗口裁剪不再被校验挡住)。
8
+
9
+ **0.5.15** —— 跟进 dsh-log-trim 0.7.2(重建可用:标题 / 工具通知 / seq 引用全部重映射)。
10
+
7
11
  **0.5.14** —— 跟进 dsh-log-trim 0.7.1(目标事件不再被折叠/重建丢掉)。
8
12
 
9
13
  - 内嵌 log-trim 升到 0.7.1:折叠与重建保留 `goal/change` 等状态事件,四条重写路径加守卫,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-as-aistudio",
3
- "version": "0.5.14",
3
+ "version": "0.5.16",
4
4
  "description": "Google-AI-Studio-style turn controls for DeepSeek Harness, in one install: edit a prompt and re-run it, re-run a reply in place, delete a turn from the model context, render sent prompts as Markdown, and trim idle session logs on boot. Self-contained: the plugin list shows this one row, and the five components (four actions + one maintenance) are vendored inside it - installing the studio installs nothing else.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/client.js CHANGED
@@ -9642,7 +9642,7 @@ window.__ModuleLoader__.load({
9642
9642
  const react = require('react')
9643
9643
  const { jsx, jsxs } = require('react/jsx-runtime')
9644
9644
 
9645
- const PLUGIN_VERSION = '0.7.1'
9645
+ const PLUGIN_VERSION = '0.7.3'
9646
9646
  const ROUTE_PREFIX = '/api/dsh-log-trim'
9647
9647
 
9648
9648
  /** The knobs this card edits, in the order they are drawn. */
@@ -10168,7 +10168,7 @@ window.__ModuleLoader__.load({
10168
10168
  const NS = 'dsh-as-aistudio'
10169
10169
 
10170
10170
  /** Keep in sync with package.json and src/components.js. */
10171
- const PLUGIN_VERSION = '0.5.14'
10171
+ const PLUGIN_VERSION = '0.5.16'
10172
10172
 
10173
10173
  /** Presence marker the live verifier reads. */
10174
10174
  const DEBUG_KEY = '__DSH_AS_AISTUDIO__'
package/src/components.js CHANGED
@@ -26,7 +26,7 @@
26
26
  export const STUDIO_ID = 'dsh-as-aistudio'
27
27
 
28
28
  /** Keep in sync with package.json and src/client.js. */
29
- export const STUDIO_VERSION = '0.5.14'
29
+ export const STUDIO_VERSION = '0.5.16'
30
30
 
31
31
  /**
32
32
  * The AI Studio action strip this studio composes, in the order the user meets
@@ -1,6 +1,6 @@
1
1
  // dsh-edit-turn host half (dsh-edit-turn.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-edit-turn/lib/index.js (v0.5.19, sha256 5a6e1fed0c1e2b64)
3
+ // Source: ../dsh-edit-turn/lib/index.js (v0.5.20, sha256 2e006e68f0591665)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -85,7 +85,7 @@ export const PLUGIN_ID = 'dsh-edit-turn'
85
85
  export const name = PLUGIN_ID
86
86
 
87
87
  /** Keep in sync with package.json and lib/client.js. */
88
- export const PLUGIN_VERSION = '0.5.19'
88
+ export const PLUGIN_VERSION = '0.5.20'
89
89
 
90
90
  // A ring of the last few requests, answering "did the browser half even ask, and
91
91
  // what did it get?" without needing its console. Read-only diagnostics: the
@@ -1,6 +1,6 @@
1
1
  // dsh-edit-turn host half (dsh-edit-turn.patch.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-edit-turn/lib/patch.js (v0.5.19, sha256 5e1e8cce1d168f6c)
3
+ // Source: ../dsh-edit-turn/lib/patch.js (v0.5.20, sha256 5e1e8cce1d168f6c)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.flatten.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/flatten.js (v0.7.1, sha256 3aa1fdcbb120ab06)
3
+ // Source: ../dsh-log-trim/lib/flatten.js (v0.7.3, sha256 3aa1fdcbb120ab06)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.goal-repair.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/goal-repair.js (v0.7.1, sha256 0801a28f35667ee7)
3
+ // Source: ../dsh-log-trim/lib/goal-repair.js (v0.7.3, sha256 0801a28f35667ee7)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.host-libs.mjs), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/host-libs.mjs (v0.7.1, sha256 5ebed01988b5bfcf)
3
+ // Source: ../dsh-log-trim/lib/host-libs.mjs (v0.7.3, sha256 5ebed01988b5bfcf)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/index.js (v0.7.1, sha256 906bd2b437e569b4)
3
+ // Source: ../dsh-log-trim/lib/index.js (v0.7.3, sha256 e35a3a2d18c9a4fa)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -44,7 +44,7 @@ import { readLedger, recordFor } from './dsh-log-trim.ledger.js'
44
44
 
45
45
  export const name = 'dsh-log-trim'
46
46
  export const PLUGIN_ID = name
47
- export const PLUGIN_VERSION = '0.7.1'
47
+ export const PLUGIN_VERSION = '0.7.3'
48
48
  export const ROUTE_PREFIX = '/api/dsh-log-trim'
49
49
 
50
50
  /**
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.ledger.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/ledger.js (v0.7.1, sha256 5ca2460b42a3a776)
3
+ // Source: ../dsh-log-trim/lib/ledger.js (v0.7.3, sha256 5ca2460b42a3a776)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.lossless.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/lossless.js (v0.7.1, sha256 1b98a61bbad8ef87)
3
+ // Source: ../dsh-log-trim/lib/lossless.js (v0.7.3, sha256 7e8478a2fd2ec10b)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -488,6 +488,9 @@ export function rebuildSurface(events) {
488
488
  // merged back in front of the first visible event that originally followed
489
489
  // them.
490
490
  const marks = new Map()
491
+ // 每条源事件真正的消息行下标:marks 指向“块起点”(turn/start 等),引用类字段
492
+ // (promptSeq / logFrom / 标题 messageSeqs)必须指向消息行本身。
493
+ const rowIndexBySeq = new Map()
491
494
  const headers = events.filter((event) => event.type === 'request/header')
492
495
  const headerBefore = (seq) => {
493
496
  let found = null
@@ -536,10 +539,26 @@ export function rebuildSurface(events) {
536
539
  return clock
537
540
  }
538
541
  const ownTime = (event) => (Number.isSafeInteger(event?.time) ? event.time : clock)
539
- for (const type of ['agent-preset/selected', 'permission/preset', 'sandbox/mode', 'approval/policy', 'session/title', 'model/selection']) {
542
+ for (const type of ['agent-preset/selected', 'permission/preset', 'sandbox/mode', 'approval/policy', 'model/selection']) {
540
543
  const event = lastOf(type)
541
544
  if (event !== undefined) out.push({ seq: 0, type, time: ownTime(event), data: event.data })
542
545
  }
546
+ // 标题要引用「更早的事件」,所以不能放在 head:重建时先记下来,最后再追加。
547
+ const titleEvent = lastOf('session/title')
548
+ // 工具定义表:`tool-addition` 通知引用的 header 必须**恰好**定义它点名的那一个工具。
549
+ // 原日志里每个 header 带的是当轮全部工具,直接复制下来会 0 个或 N 个匹配,格式层拒收。
550
+ const toolDefinitions = new Map()
551
+ for (const event of events) {
552
+ if (event.type !== 'request/header') continue
553
+ const tools = event.data?.header?.tools
554
+ if (!Array.isArray(tools)) continue
555
+ for (const tool of tools) {
556
+ if (tool && typeof tool.name === 'string') toolDefinitions.set(tool.name, tool)
557
+ }
558
+ }
559
+ // `mergeStateEvents` 会往 out 里插行,插完后面的下标就变了;headerSeq 必须
560
+ // 在合并之后按最终位置回填,否则会指向别的行。
561
+ const pendingHeaderSeqs = []
543
562
  let turn = 0
544
563
  let step = 0
545
564
  let open = false
@@ -599,24 +618,39 @@ export function rebuildSurface(events) {
599
618
  const role = message !== null && typeof message === 'object' ? message.role : undefined
600
619
  const header = headerBefore(event.seq)
601
620
  if (event.type === 'system/message') continue
621
+ // 标题统一在最后重写(引用必须更早);这里不再原样复制一份。
622
+ if (event.type === 'session/title') continue
602
623
  if (event.type === 'developer/message') {
603
624
  if (!open) openTurn(header)
604
625
  if (!stepOpen) openStep(typeof data.step === 'number' ? data.step : null)
605
626
  const next = { turn, step, message }
606
627
  const addsTools =
607
628
  Array.isArray(message?.content) && message.content.some((part) => part?.type === 'tool-addition')
608
- if (event.type === 'developer/message' && addsTools && header !== null) {
609
- // A tool-addition notice names the header that introduced that tool.
610
- headerIndex = out.length
611
- out.push({ seq: 0, type: 'request/header', time: clock, data: header.data })
612
- next.headerSeq = headerIndex
629
+ if (event.type === 'developer/message' && addsTools) {
630
+ // 通知必须引用一个「恰好定义它点名的工具」的 header:从定义表里取,
631
+ // 凑不齐就干脆不写 headerSeq(格式层只在有 headerSeq 时才校验)。
632
+ const names = [...new Set(message.content.filter((part) => part?.type === 'tool-addition' && typeof part.toolName === 'string').map((part) => part.toolName))]
633
+ const definitions = names.map((name) => toolDefinitions.get(name)).filter((definition) => definition !== undefined)
634
+ if (header !== null && definitions.length === names.length && names.length > 0) {
635
+ const data2 = { ...header.data, header: { ...header.data.header, tools: definitions } }
636
+ const headerRow = { seq: 0, type: 'request/header', time: clock, data: data2 }
637
+ headerIndex = out.length
638
+ out.push(headerRow)
639
+ next.headerSeq = headerIndex
640
+ pendingHeaderSeqs.push({ data: next, row: headerRow })
641
+ }
613
642
  }
643
+ rowIndexBySeq.set(event.seq, out.length)
614
644
  out.push({ seq: 0, type: event.type, time: ownTime(event), data: next, surfaceOp: 'append' })
615
645
  continue
616
646
  }
617
647
  if (role === 'user' || event.type === 'user/message') {
618
648
  openTurn(header)
619
- out.push({ seq: 0, type: event.type, time: ownTime(event), data: { ...message, turn }, surfaceOp: 'append' })
649
+ // 扁平载荷把 source 放在 data 上;嵌套载荷(data.message)要把它补回去,
650
+ // 否则 kind / promptSeq / rerun 归属全丢,标题也没法引用它。
651
+ const extra = data.source !== undefined && message.source === undefined ? { source: data.source } : {}
652
+ rowIndexBySeq.set(event.seq, out.length)
653
+ out.push({ seq: 0, type: event.type, time: ownTime(event), data: { ...message, ...extra, turn }, surfaceOp: 'append' })
620
654
  continue
621
655
  }
622
656
  if (!open) openTurn(header)
@@ -644,6 +678,8 @@ export function rebuildSurface(events) {
644
678
  // without it the format layer reads the row as a fork result and refuses it.
645
679
  if (data.error !== undefined) payload.error = data.error
646
680
  if (event.type === 'tool/call') payload.callId = data.callId
681
+ if (data.source !== undefined) payload.source = data.source
682
+ rowIndexBySeq.set(event.seq, out.length)
647
683
  out.push({ seq: 0, type: event.type, time: ownTime(event), data: payload, surfaceOp: 'append' })
648
684
  // A tool call is announced by its assistant message and "started" by a
649
685
  // tool/call event; without that event the result looks like a tool that
@@ -668,6 +704,68 @@ export function rebuildSurface(events) {
668
704
  }
669
705
  closeTurn()
670
706
  const merged = mergeStateEvents(out, marks, preserved)
707
+ // `source.promptSeq` 指向「这一轮是由哪条 prompt 触发的」,格式层要求它更早。
708
+ // 重建后 seq 全变了,按源 seq → 原下标 → 合并后下标 映射回来;映射不到的删掉,
709
+ // 否则整份日志会被交叉引用检查拒绝。
710
+ const outToMerged = new Map()
711
+ for (let index = 0; index < out.length; index += 1) outToMerged.set(out[index], index)
712
+ for (let index = 0; index < merged.length; index += 1) {
713
+ const row = merged[index]
714
+ const rowOutIndex = outToMerged.get(row)
715
+ if (rowOutIndex === undefined) continue
716
+ outToMerged.set(row, index)
717
+ }
718
+ const mergedIndexOfSourceSeq = (sourceSeq) => {
719
+ const outIndex = rowIndexBySeq.get(sourceSeq) ?? marks.get(sourceSeq)
720
+ if (outIndex === undefined) return undefined
721
+ return outToMerged.get(out[outIndex])
722
+ }
723
+ for (const row of merged) {
724
+ // 两种载荷形状都要处理:嵌套的 `data.message.source` 和扁平的 `data.source`
725
+ // (rerun / delete 留下的载体用的是后者)。
726
+ const sources = []
727
+ const message = row.data?.message
728
+ if (message !== null && typeof message === 'object' && message.source !== null && typeof message.source === 'object') sources.push(message.source)
729
+ if (row.data?.source !== null && typeof row.data?.source === 'object') sources.push(row.data.source)
730
+ for (const source of sources) {
731
+ for (const key of ['promptSeq', 'logFrom', 'logTo', 'rerunTurn', 'rerunTurnEnd', 'carrierSeq', 'targetSeq']) {
732
+ if (typeof source[key] !== 'number') continue
733
+ const mapped = mergedIndexOfSourceSeq(source[key])
734
+ const rowIndex = outToMerged.get(row)
735
+ if (mapped === undefined || (rowIndex !== undefined && mapped >= rowIndex)) delete source[key]
736
+ else source[key] = mapped
737
+ }
738
+ }
739
+ }
740
+ for (const pending of pendingHeaderSeqs) {
741
+ const at = merged.indexOf(pending.row)
742
+ if (at >= 0) pending.data.headerSeq = at
743
+ else delete pending.data.headerSeq
744
+ }
745
+ // 标题放在最后:它引用的 `messageSeqs` 必须是**更早**的 `user/message`(且来源是用户),
746
+ // 放在 head 时前面没有任何消息,怎么改都过不了格式层。
747
+ if (titleEvent !== undefined) {
748
+ const candidates = []
749
+ merged.forEach((row, index) => {
750
+ if (row.type !== 'user/message') return
751
+ if (row.data?.source?.kind !== 'user') return
752
+ candidates.push(index)
753
+ })
754
+ const isUserTitle = titleEvent.data?.source?.kind === 'user'
755
+ if (isUserTitle) {
756
+ // 用户自己的标题:messageSeqs 必须为空。
757
+ merged.push({ seq: 0, type: 'session/title', time: ownTime(titleEvent), data: { ...titleEvent.data, messageSeqs: [] } })
758
+ } else if (candidates.length > 0) {
759
+ // 自动标题:必须引用至少一条更早的 user/message。
760
+ const wanted = Math.max(1, Math.min(2, Array.isArray(titleEvent.data?.messageSeqs) ? titleEvent.data.messageSeqs.length : 1))
761
+ merged.push({
762
+ seq: 0,
763
+ type: 'session/title',
764
+ time: ownTime(titleEvent),
765
+ data: { ...titleEvent.data, messageSeqs: candidates.slice(-wanted) },
766
+ })
767
+ }
768
+ }
671
769
  const rebuilt = merged.map((event, index) => ({ ...event, seq: index }))
672
770
  // A fork result encodes the sequence it was appended at inside its identity
673
771
  // (`forked-tool-result-<callId>-<seq>`); renumbering has to follow, or the host
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.roots.js), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/roots.js (v0.7.1, sha256 028d9c12a395aaab)
3
+ // Source: ../dsh-log-trim/lib/roots.js (v0.7.3, sha256 028d9c12a395aaab)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.vendor.compact-session.mjs), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/vendor.compact-session.mjs (v0.7.1, sha256 0f27c0d30c2c8215)
3
+ // Source: ../dsh-log-trim/lib/vendor.compact-session.mjs (v0.7.3, sha256 1b0499e0c4c11122)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -2053,6 +2053,95 @@ export function stripOldStreams(events, keepTurns) {
2053
2053
  return { events: out, stripped, bytesSaved }
2054
2054
  }
2055
2055
 
2056
+ /**
2057
+ * 把落在「没有任何开放 step」位置的 tool/result 归位。
2058
+ *
2059
+ * 为什么会有这种行:工具调用的结果可能晚到——它挂在很早的 turn/step 上,
2060
+ * 却在很后面的位置才写进日志。原日志能装得上,但当裁剪从中间切掉一段再重编号后,
2061
+ * 这些行就会落在 step/end 之后、下一段 step 之前,宿主的格式层要求
2062
+ * 「tool/result 的 turn/step 必须等于当前打开的 turn/step」,于是整份日志被拒。
2063
+ *
2064
+ * 处理:找到它自己那一步的 step/start,把它挪进那一 step 的最后;找不到就往后
2065
+ * 找第一个 step/start 并改挂到那一步;连一个 step 都没有就删掉(此时它已经无处
2066
+ * 可挂,留着只会让整份日志装不上)。
2067
+ *
2068
+ * @param {object[]} events - 已重编号的事件。
2069
+ * @returns {{ events: object[], moved: number, dropped: number }} 处理结果。
2070
+ */
2071
+ export function repairLateToolResults(events) {
2072
+ const list = [...events]
2073
+ let moved = 0
2074
+ let dropped = 0
2075
+ // 被裁剪丢掉的调用不会再有 tool/call;它的结果留着就是「没有 advertised lifecycle」,
2076
+ // 格式层直接拒收,所以先把这种孤儿结果清掉。
2077
+ const advertised = new Set()
2078
+ for (const event of list) {
2079
+ if (event.type === 'tool/call' && typeof event.data?.callId === 'string') advertised.add(event.data.callId)
2080
+ if (event.type === 'assistant/message' && Array.isArray(event.data?.message?.content)) {
2081
+ for (const block of event.data.message.content) {
2082
+ if (block?.type === 'tool-call' && typeof block.id === 'string') advertised.add(block.id)
2083
+ }
2084
+ }
2085
+ }
2086
+ for (let index = list.length - 1; index >= 0; index -= 1) {
2087
+ const event = list[index]
2088
+ if (event.type !== 'tool/result') continue
2089
+ const callId = event.data?.message?.toolCallId ?? event.data?.message?.source?.callId ?? event.data?.toolCallId
2090
+ if (typeof callId !== 'string') continue
2091
+ if (advertised.has(callId)) continue
2092
+ list.splice(index, 1)
2093
+ dropped += 1
2094
+ }
2095
+ for (let round = 0; round < 5; round += 1) {
2096
+ // 1) 模拟当前开放的 turn/step,找出位置不对的 tool/result
2097
+ let turn = null
2098
+ let step = null
2099
+ const offenders = []
2100
+ for (let index = 0; index < list.length; index += 1) {
2101
+ const event = list[index]
2102
+ if (event.type === 'turn/start') { turn = event.data?.turn; step = null; continue }
2103
+ if (event.type === 'step/start') { step = event.data?.step; continue }
2104
+ if (event.type === 'step/end') { step = null; continue }
2105
+ if (event.type === 'turn/end') { turn = null; step = null; continue }
2106
+ if (event.type !== 'tool/result') continue
2107
+ const t = event.data?.turn
2108
+ const s = event.data?.step
2109
+ if (!Number.isSafeInteger(t) || !Number.isSafeInteger(s)) continue
2110
+ if (step !== null && t === turn && s === step) continue
2111
+ offenders.push({ index, turn: t, step: s })
2112
+ }
2113
+ if (offenders.length === 0) break
2114
+ // 2) 逐个找落点:优先自己那一步,其次往后第一个 step
2115
+ for (const offender of offenders.sort((a, b) => b.index - a.index)) {
2116
+ const row = list[offender.index]
2117
+ let target = -1
2118
+ for (let index = 0; index < list.length; index += 1) {
2119
+ const event = list[index]
2120
+ if (event.type === 'step/start' && event.data?.turn === offender.turn && event.data?.step === offender.step) { target = index; break }
2121
+ }
2122
+ if (target === -1) {
2123
+ for (let index = offender.index + 1; index < list.length; index += 1) {
2124
+ if (list[index].type === 'step/start') { target = index; break }
2125
+ }
2126
+ }
2127
+ list.splice(offender.index, 1)
2128
+ if (target === -1) { dropped += 1; continue }
2129
+ if (target > offender.index) target -= 1
2130
+ const anchor = list[target]
2131
+ let insertAfter = target
2132
+ for (let index = target + 1; index < list.length; index += 1) {
2133
+ const event = list[index]
2134
+ if (['step/end', 'turn/end', 'step/start', 'turn/start'].includes(event.type)) break
2135
+ if (event.data?.turn === anchor.data?.turn && event.data?.step === anchor.data?.step) insertAfter = index
2136
+ }
2137
+ row.data = { ...row.data, turn: anchor.data?.turn, step: anchor.data?.step }
2138
+ list.splice(insertAfter + 1, 0, row)
2139
+ moved += 1
2140
+ }
2141
+ }
2142
+ return { events: list, moved, dropped }
2143
+ }
2144
+
2056
2145
  export async function stageTrim(logFile, keep, options = {}) {
2057
2146
  if (isSessionLocked(logFile)) {
2058
2147
  throw new Error('会话正被宿主持有(session.lock 被打开),拒绝改动;请先关掉该会话或重启宿主')
@@ -2064,7 +2153,12 @@ export async function stageTrim(logFile, keep, options = {}) {
2064
2153
  const streamKeepTurns = options.streamKeepTurns === undefined ? Infinity : options.streamKeepTurns
2065
2154
  const stripped = stripOldStreams(trimmed.events, streamKeepTurns)
2066
2155
  assertNoGoalEventsDropped(original.events, stripped.events)
2067
- const renamed = renumber(stripped.events)
2156
+ let renamed = renumber(stripped.events)
2157
+ const repaired = repairLateToolResults(renamed.events)
2158
+ if (repaired.moved > 0 || repaired.dropped > 0) {
2159
+ // 挪动会改变 seq,重新编号一次让所有引用跟上。
2160
+ renamed = renumber(repaired.events)
2161
+ }
2068
2162
 
2069
2163
  const seqs = new Set(renamed.events.map((e) => e.seq))
2070
2164
  for (let i = 0; i < renamed.events.length; i += 1) {
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.verify-session-log.mjs), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/verify-session-log.mjs (v0.7.1, sha256 7cf835c053942961)
3
+ // Source: ../dsh-log-trim/lib/verify-session-log.mjs (v0.7.3, sha256 7cf835c053942961)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,6 +1,6 @@
1
1
  // dsh-log-trim host half (dsh-log-trim.worker.mjs), VENDORED by tools/vendor.mjs - DO NOT EDIT.
2
2
  //
3
- // Source: ../dsh-log-trim/lib/worker.mjs (v0.7.1, sha256 675a759a767b7e38)
3
+ // Source: ../dsh-log-trim/lib/worker.mjs (v0.7.3, sha256 675a759a767b7e38)
4
4
  // Regenerate with `npm run vendor`; test/sync.test.js fails when this is stale.
5
5
  //
6
6
  // src/shell.js imports the copy named after the component, which is why the
@@ -1,7 +1,7 @@
1
1
  {
2
- "dsh-edit-turn": "0.5.19",
2
+ "dsh-edit-turn": "0.5.20",
3
3
  "dsh-rerun-turn": "0.2.11",
4
4
  "dsh-delete-turn": "0.2.4",
5
5
  "dsh-markdown-bubble": "0.1.5",
6
- "dsh-log-trim": "0.7.1"
6
+ "dsh-log-trim": "0.7.3"
7
7
  }