Repository navigation
seq 序号重复导致对话加载失败 #1333
Replies: 2 comments
|
非常好的定位——我把你引用的 built lib 路径对应到当前 master 的源码,逐条核对后基本都成立,但有一个关键点想补充: 源码对应(built lib → src)
真正可疑的机制:跨进程并发写入
文件里出现 需要你确认的几点
给维护者的修复方向(feature request 素材)
如果你能提供后端类型 + 复现时进程拓扑,我可以继续往对应后端(JSONL append 路径或 SQLite 事务)里挖。 |
|
Adding a field case that supports @argszero's conclusion, with one detail I think narrows the search: the collision here was triggered by an interrupt, but I initially suspected the closers, then checked Environment
The records, with timestampsLines 67541–67543 are synthetic closers: three events sharing one timestamp, matching "timestamps reuse the last real event" in Why this points away from the closers and toward a second writerThe closers occupied 1037641–1037643. If the same live Instead, 3.1 seconds later, the real step-5 stream starts at 1037642 — and continues monotonically from there (I checked: exactly one regression in all 67,967 lines; the tail is internally ordered). So the writer that produced the streaming chunks never observed the closers. That is the What I can and cannot say:
If closers are written through a path that does not advance the live A separate, deterministic issue in the loaderWhatever writes the duplicate, the loader turns a 2-event overlap into total loss. In if (event.seq !== this.events.length) {
const expected = this.events.length;
this.events.length = rowStart;
this.issue = new Error(`corrupt session log: seq gap ...`);
if (decoded.some((candidate) => candidate.type === "turn/end")) throw this.issue;
return; // otherwise: keep the valid prefix
}The This part reproduces deterministically, no race required: import { decodeStorageRecord } from '@deepseek-ai/dsh-session'
function scan(rows, note) {
let events = 0, line = 0, issue
for (const r of rows) {
line++
const decoded = decodeStorageRecord(r)
if (issue) { if (decoded.some(e => e.type === 'turn/end')) throw issue; continue }
const rowStart = events
for (const ev of decoded) {
if (ev.seq !== events) {
issue = new Error(`seq gap at line ${line} (expected ${events}, got ${ev.seq})`)
events = rowStart
if (decoded.some(c => c.type === 'turn/end')) throw issue
break
}
events++
}
}
console.log(`${note}: events preserved = ${events}`)
}
const base = [
{ type: 'turn/start', seq: 0, time: 1, data: { turn: 1 } },
{ type: 'step/start', seq: 1, time: 1, data: { turn: 1, step: 1 } },
{ type: 'step/end', seq: 2, time: 1, data: { turn: 1, step: 1 } },
]
const collide = { type: 'assistant/chunk', seq: 2, time: 2,
data: { turn: 1, step: 1, chunk: { type: 'block-start', index: 0, blockType: 'reasoning' } } }
scan([...base, collide], 'collision, no later turn/end')
scan([...base, collide, { type: 'turn/end', seq: 4, time: 3, data: { turn: 1, reason: { kind: 'completed' } } }],
'collision + later turn/end')Same collision, same valid prefix, opposite outcome. Recovering the prefix and surfacing a warning would turn "lost the session" into "lost the tail" — and would also make already-damaged logs readable again, which a writer-side fix alone will not do. Recovery (verified, no data loss)The bytes are intact; only the numbering collides. Renumbering the orphaned tail restored my session completely: zstd -dc session.jsonl.zstd > s.jsonl # back up the original first
python3 - <<'PY'
import json
src = open('s.jsonl').read().splitlines()
START, OFFSET = 67544, 2 # first regressed line; (last committed seq + 1) - (first tail seq)
out = src[:START-1]
for l in src[START-1:]:
if not l.strip(): out.append(l); continue
d = json.loads(l)
for k in ('seq', 'seq0'):
if d.get(k) is not None: d[k] += OFFSET
out.append(json.dumps(d, ensure_ascii=False, separators=(',', ':')))
open('fixed.jsonl','w').write('\n'.join(out) + '\n')
PY
zstd -q -f fixed.jsonl -o session.jsonl.zstdVerified by replicating
Two caveats for anyone reusing this: |
Uh oh!
There was an error while loading. Please reload this page.
seq 序号重复问题
异常点只有一个:第 6708 条记录 seq 从 147517 回退到 147513,导致 147513–147516 出现两次。这种"序号回退 + 重复"是事件溯源系统典型的计数器与持久化状态不一致,只能由程序在分配/恢复 seq 时出错产生,不可能由磁盘或用户操作造成。
seq 的分配规则是硬编码的:append() 里 seq: this.log.length(源码 lib/index.js:1455),注释明确写着"seq = log.length 连续性契约"。
resume/fork 时,seed 边界决定 log.length 的起点:构造函数里 firstLiveSeq = this.log.length,若 seed 末尾不是 session/end-seed 就追加一条(index.js:1385-1387)。
崩溃恢复还有一条 interruptedTurnClosers() 逻辑:若日志末尾是未闭合的 turn,会重新生成合成 closer(tool/result + step/end + turn/end),seq 从 last.seq + 1 开始(index.js:664)。
All reactions