dsh-log-contract 0.3.19 → 0.3.21
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 +37 -10
- package/README.zh.md +35 -9
- package/lib/log-reader.js +2 -2
- package/lib/repair.js +25 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -60,8 +60,8 @@ humans and automated programs. Humans tolerate format drift; programs depend on
|
|
|
60
60
|
strict contracts. Once the format drifts, humans see nothing wrong while programs
|
|
61
61
|
crash or misreport.
|
|
62
62
|
|
|
63
|
-
**Real
|
|
64
|
-
below. The
|
|
63
|
+
**Real log-corruption shapes found in production sessions shaped most of the rules here** —
|
|
64
|
+
see the [Case log](#-case-log) below. The case-derived instances are pinned by regression tests: a corrupt log shape
|
|
65
65
|
this tool must flag, and the repaired shape it must pass. The remaining rules come from
|
|
66
66
|
line-by-line verification against the official Host source (each rule carries its
|
|
67
67
|
`source`).
|
|
@@ -86,10 +86,10 @@ line-by-line verification against the official Host source (each rule carries it
|
|
|
86
86
|
|
|
87
87
|
---
|
|
88
88
|
|
|
89
|
-
## ⚡
|
|
89
|
+
## ⚡ Case log — why "production-grade" is not a slogan
|
|
90
90
|
|
|
91
|
-
Each entry below is a **real
|
|
92
|
-
|
|
91
|
+
Each entry below is a **real log-corruption shape** from production sessions — the cases that
|
|
92
|
+
shaped this tool. Dates and shapes are real; session ids and file names are
|
|
93
93
|
omitted.
|
|
94
94
|
|
|
95
95
|
| # | Date | What happened | The rule / fix it produced |
|
|
@@ -201,10 +201,10 @@ dsh-log-contract fix ~/.dsh/sessions/<id>.jsonl.zstd --remove-markers --apply
|
|
|
201
201
|
- `--remove-markers`: remove retrace/message-editor markers and renumber everything
|
|
202
202
|
(seq/seq0/sourceEventSeqs/surfaceOp in sync) — for large marker-shadowed history
|
|
203
203
|
or markers that left dangling tools.
|
|
204
|
-
- `--neutralize`: in-place neutralization of turn-null markers (
|
|
204
|
+
- `--neutralize`: in-place neutralization of turn-null markers (case #6) —
|
|
205
205
|
type → `retrace/marker` + `ignorable:true`, drops surfaceOp/sourceEventSeqs,
|
|
206
206
|
seq/line count unchanged (safe while the session is resident).
|
|
207
|
-
- `--clip-crossstep`: trim cross-step sourceEventSeqs (
|
|
207
|
+
- `--clip-crossstep`: trim cross-step sourceEventSeqs (case #6) — keep only
|
|
208
208
|
same-turn/step chunk references.
|
|
209
209
|
- Surgery safety protocol: back up first, re-verify after (strictScan + check +
|
|
210
210
|
foldSurface); markers may only shadow earlier nodes; a marker must never become
|
|
@@ -326,7 +326,7 @@ boundary note in [docs/CONTRACTS.md](docs/CONTRACTS.md).
|
|
|
326
326
|
- [x] **Phase 1.5 (0.2.0)**: `fix` subcommand (strict seq scan + W1/W2 wire checks +
|
|
327
327
|
marker removal with renumbering + official frame rebuild); CI integration
|
|
328
328
|
(`dsh-log-contract check` as a scheduled guard over the Harness session dir)
|
|
329
|
-
- [x] **0.3.x (2026-08-30
|
|
329
|
+
- [x] **0.3.x (2026-08-30 case hardening)**: T1 token-meter pairing → 0.3.1 W1/W2
|
|
330
330
|
fold-position fix → 0.3.2 `tailSeq` → 0.3.3 `fix --neutralize` (in-place
|
|
331
331
|
turn-null neutralization) → 0.3.4 `fix --clip-crossstep` (cross-step clipping) →
|
|
332
332
|
0.3.5 **T2/S9/I1 rules** (cross-step source refs / physical order / inbox replay)
|
|
@@ -344,10 +344,37 @@ MIT © OfferKuai Team
|
|
|
344
344
|
|
|
345
345
|
---
|
|
346
346
|
|
|
347
|
+
## 🆕 0.3.18 – 0.3.21 · DSH 0.2.x (session format v4) support
|
|
348
|
+
|
|
349
|
+
**Session format v4** (`@deepseek-ai/dsh-session` 0.2.x) is now validated by format-version dispatch
|
|
350
|
+
rather than by relaxing the format gate:
|
|
351
|
+
|
|
352
|
+
- `tool/result` is validated with the v4 shape (`role: 'tool'`, no `tool-result` wrapper block)
|
|
353
|
+
- `system/message` accepts v4's `source.kind: 'system-prompt'`
|
|
354
|
+
- validation for v3 and earlier is **unchanged** (identical verdicts on 408 real v3 sessions before/after)
|
|
355
|
+
- the format gate still rejects **unknown** future versions (`readOnly`); v5+ remains unassessable by design
|
|
356
|
+
|
|
357
|
+
**New rules (51 total):**
|
|
358
|
+
|
|
359
|
+
| Rule | Checks |
|
|
360
|
+
|---|---|
|
|
361
|
+
| `G4` | unknown historical events may be declaratively skipped (`ignorable: true`; allow-list injectable) |
|
|
362
|
+
| `S10` | `sourceEventSeqs` elements: non-negative integers after decoding (official interval pairs allowed in storage) |
|
|
363
|
+
| `I2` | `seedLength` means *number of inherited events* (not bytes); `parentSession` + missing/0 is legal |
|
|
364
|
+
| `P5` | two-segment carrier pairing: 2nd segment's first seq == 1st segment seq (read side accepts the legacy order) |
|
|
365
|
+
| `A1` | "in-place restorable" is judged from structured fields only (`archiveRef` / `archive` / `externalized.*`) — never from free text |
|
|
366
|
+
|
|
367
|
+
**Large sessions:** reading is line-wise (no whole-file stringification ⇒ no `ERR_STRING_TOO_LONG` above ~536 MB),
|
|
368
|
+
and repair rebuilds `.jsonl.zstd` with streaming frames (header frame + chunked body frames, per-frame checksum + round-trip).
|
|
369
|
+
|
|
370
|
+
**Windows:** the `header.cwd` absolute-path check in `H2` is platform-aware (`win32` and `posix` both accepted).
|
|
371
|
+
|
|
372
|
+
**Compatibility:** Node ≥ 22; `@deepseek-ai/dsh-session` `^0.1.0-rc.7 || ^0.1.5-rc.1 || ^0.2.0-rc.1`; session formats v0–v4.
|
|
373
|
+
|
|
347
374
|
## ⚠️ Upgrading to 0.3.17 — behaviour changes you must know (0.3.12 → 0.3.17)
|
|
348
375
|
|
|
349
|
-
> **0.3.17 changes no behaviour.** It
|
|
350
|
-
>
|
|
376
|
+
> **0.3.17 changes no behaviour.** It only adjusts the wording of comments, test titles, a few
|
|
377
|
+
> diagnostic strings and the README. Every verdict, exit code and `--json`
|
|
351
378
|
> field is unchanged from 0.3.15, so everything below applies unchanged when going from 0.3.12 to 0.3.17.
|
|
352
379
|
>
|
|
353
380
|
> 0.3.15 was a documentation-only release as well.
|
package/README.zh.md
CHANGED
|
@@ -50,7 +50,7 @@ dsh-retrace 的「日志体检与修复」能力——与 dsh-retrace 一起构
|
|
|
50
50
|
|
|
51
51
|
**「one log, two consumers, two verdicts」**:一条日志同时被人类与自动化程序消费,人眼容忍格式微调,程序解析依赖严格契约;格式一旦漂移,人看不出问题,程序直接崩溃或误报。
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
**这里大多数规则都来自会话日志中实际出现过的损坏形态**——见下方 [⚡ 案例记录](#-案例记录)。由这些案例派生的用例都有回归测试钉住:损坏的日志形态本工具必须报出,修复后的形态必须通过。其余规则来自对官方宿主源码的逐行核对(每条规则都带 `source`)。
|
|
54
54
|
|
|
55
55
|
---
|
|
56
56
|
|
|
@@ -69,9 +69,9 @@ dsh-retrace 的「日志体检与修复」能力——与 dsh-retrace 一起构
|
|
|
69
69
|
|
|
70
70
|
---
|
|
71
71
|
|
|
72
|
-
## ⚡
|
|
72
|
+
## ⚡ 案例记录 ——「生产级保证」不是口号
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
下面每一条都来自**生产会话中实际出现的日志损坏形态**——正是这些案例促成了本工具。日期与形态真实,会话 id 与文件名省略。
|
|
75
75
|
|
|
76
76
|
| # | 日期 | 发生了什么 | 产出的规则/修复 |
|
|
77
77
|
|---|---|---|---|
|
|
@@ -175,9 +175,9 @@ dsh-log-contract fix ~/.dsh/sessions/<id>.jsonl.zstd --remove-markers --apply
|
|
|
175
175
|
- `--remove-markers`:移除 retrace/message-editor marker 并全量重编号
|
|
176
176
|
(seq/seq0/sourceEventSeqs/surfaceOp 同步)——用于大范围 marker 遮蔽历史、
|
|
177
177
|
marker 漏盖 tool/result 导致的悬空 tool。
|
|
178
|
-
- `--neutralize`:原地中和 turn-null marker
|
|
178
|
+
- `--neutralize`:原地中和 turn-null marker(案例 #6)——type → `retrace/marker` +
|
|
179
179
|
`ignorable:true`,删 surfaceOp/sourceEventSeqs,seq/行数不变(会话驻留也安全)。
|
|
180
|
-
- `--clip-crossstep`:裁剪跨 step sourceEventSeqs
|
|
180
|
+
- `--clip-crossstep`:裁剪跨 step sourceEventSeqs(案例 #6)——只保留同 turn/step 的 chunk 引用。
|
|
181
181
|
- 手术安全协议:改前备份、改后全量复检(strictScan + check + foldSurface)、
|
|
182
182
|
marker 只能遮蔽其之前的节点、marker 绝不能改成 append(M1 客户端崩溃)。
|
|
183
183
|
- ⚠️ 若会话已被运行中的应用驻留内存,修复文件后需**重启应用**(强杀避免脏状态刷回)。
|
|
@@ -257,7 +257,7 @@ if (!verdict.ok) {
|
|
|
257
257
|
## 测试
|
|
258
258
|
|
|
259
259
|
```bash
|
|
260
|
-
pnpm check && pnpm test # 语法检查 + 契约文档漂移闸 +
|
|
260
|
+
pnpm check && pnpm test # 语法检查 + 契约文档漂移闸 + 全部单测(含案例回归用例)
|
|
261
261
|
```
|
|
262
262
|
|
|
263
263
|
- **合成夹具**(入库):合法会话 / seq 缺口 / 空 sourceEventSeqs / turn=null append / 未知 type / 坏 chunk 行 / 撕裂尾帧 / 未知 marker 前缀 / 自指 shadowed 等。
|
|
@@ -284,7 +284,7 @@ pnpm check && pnpm test # 语法检查 + 契约文档漂移闸 + 全部单测
|
|
|
284
284
|
|
|
285
285
|
- [x] **Phase 1(0.1.0)**:CLI 离线体检 + 写前校验 + 契约目录
|
|
286
286
|
- [x] **Phase 1.5(0.2.0)**:`fix` 子命令(严格 seq 扫描 + W1/W2 wire 检查 + 移除 marker 重编号 + 官方帧格式重建);CI 集成(`dsh-log-contract check` 作为 Harness 会话目录的定时守护)
|
|
287
|
-
- [x] **0.3.x(2026-08-30
|
|
287
|
+
- [x] **0.3.x(2026-08-30 案例固化)**:T1 token-meter 配对 → 0.3.1 W1/W2 折叠位置修复 → 0.3.2 `tailSeq` → 0.3.3 `fix --neutralize`(turn-null marker 原地中和)→ 0.3.4 `fix --clip-crossstep`(跨 step 引用裁剪)→ 0.3.5 **T2/S9/I1 规则**(跨 step 源引用 / 物理序单调 / inbox 重放)
|
|
288
288
|
- [x] **0.3.6 → 0.3.15**:按被检文件版本择路的 v0–v3 规则(T3/T4/T5、L3–L5、E8/E9/E10)、宿主能力与迁移预检的退出码 `3`/`4`,以及如实报告"哪些版本**未**验证"的版本支持说明(详见 [CHANGELOG.md](CHANGELOG.md))
|
|
289
289
|
- [ ] Phase 2:运行时守护(订阅 session append 事件流实时校验,断裂即标记 `dsh/contract-violation` 事件,策略可配 告警/拦截)——DSH 插件形态
|
|
290
290
|
- [ ] Phase 3:与 dsh-turn-guard / dsh-retrace 时间线联动
|
|
@@ -295,10 +295,36 @@ MIT © OfferKuai Team
|
|
|
295
295
|
|
|
296
296
|
---
|
|
297
297
|
|
|
298
|
+
## 🆕 0.3.18 – 0.3.21 · 支持 DSH 0.2.x(会话格式 v4)
|
|
299
|
+
|
|
300
|
+
**会话格式 v4**(`@deepseek-ai/dsh-session` 0.2.x)改为**按格式版本分派规则**,而不是放宽格式闸门:
|
|
301
|
+
|
|
302
|
+
- `tool/result` 按 v4 形状校验(`role: 'tool'`、无 `tool-result` 包裹块)
|
|
303
|
+
- `system/message` 接受 v4 的 `source.kind: 'system-prompt'`
|
|
304
|
+
- **v3 及更早的判据零改动**(408 份真实 v3 会话前后判定完全一致)
|
|
305
|
+
- 格式闸门仍**拒绝未知的未来版本**(`readOnly`);v5+ 按设计保持"不可评估"
|
|
306
|
+
|
|
307
|
+
**新增规则(共 51 条):**
|
|
308
|
+
|
|
309
|
+
| 规则 | 判据 |
|
|
310
|
+
|---|---|
|
|
311
|
+
| `G4` | 未知历史事件可**声明式跳过**(`ignorable: true`;白名单可注入) |
|
|
312
|
+
| `S10` | `sourceEventSeqs` 元素:**解码后**必须为非负整数(存储层允许官方区间对) |
|
|
313
|
+
| `I2` | `seedLength` = **继承事件条数**(非字节);`parentSession` 存在且缺失/0 属合法 |
|
|
314
|
+
| `P5` | 两段 carrier 配对:第 2 段首元素 = 第 1 段 seq(读侧兼容早期写法) |
|
|
315
|
+
| `A1` | 「在位可还原」**只认结构化字段**(`archiveRef`/`archive`/`externalized.*`),不从自由文本推断 |
|
|
316
|
+
|
|
317
|
+
**大会话**:读取改为**按行解码**(不再整文件字符串化 ⇒ 约 536MB 以上不再抛 `ERR_STRING_TOO_LONG`);
|
|
318
|
+
修复采用**流式帧重建** `.jsonl.zstd`(header 帧 + 正文分块多帧,逐帧 checksum + roundtrip 校验)。
|
|
319
|
+
|
|
320
|
+
**Windows**:`H2` 的 `header.cwd` 绝对路径判断改为**平台感知**(`win32` 与 `posix` 都认)。
|
|
321
|
+
|
|
322
|
+
**兼容性**:Node ≥ 22;`@deepseek-ai/dsh-session` `^0.1.0-rc.7 || ^0.1.5-rc.1 || ^0.2.0-rc.1`;会话格式 v0–v4。
|
|
323
|
+
|
|
298
324
|
## ⚠️ 升级到 0.3.17 —— 必须知道的行为变更(0.3.12 → 0.3.17)
|
|
299
325
|
|
|
300
|
-
> **0.3.17 不含行为变更。**
|
|
301
|
-
>
|
|
326
|
+
> **0.3.17 不含行为变更。** 它只调整了注释、测试标题、少量诊断文案与 README 的措辞。
|
|
327
|
+
> 所有判定结果、退出码与 `--json` 字段与 0.3.15 完全一致,
|
|
302
328
|
> 因此从 0.3.12 升到 0.3.17 时,下面各条与原来完全一致。
|
|
303
329
|
>
|
|
304
330
|
> 0.3.15 同样只是文档层面的发布。
|
package/lib/log-reader.js
CHANGED
|
@@ -200,8 +200,8 @@ export function loadSessionLog(path) {
|
|
|
200
200
|
};
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
-
|
|
204
|
-
const lines =
|
|
203
|
+
// 按行解码(Buffer 切分),避免整文件字符串化(>536MB 抛字符串上限)
|
|
204
|
+
const lines = (() => { const out = []; let s = 0, i; while ((i = plain.indexOf(0x0a, s)) !== -1) { out.push(plain.subarray(s, i).toString('utf8')); s = i + 1 } out.push(plain.subarray(s).toString('utf8')); return out })();
|
|
205
205
|
|
|
206
206
|
// 第一行 = header
|
|
207
207
|
const headerLine = 0;
|
package/lib/repair.js
CHANGED
|
@@ -1135,6 +1135,31 @@ export function rebuildZstdText(text) {
|
|
|
1135
1135
|
return out;
|
|
1136
1136
|
}
|
|
1137
1137
|
|
|
1138
|
+
/**
|
|
1139
|
+
* 按官方 writer 帧格式重建 .jsonl.zstd(帧1=header,帧2..n=正文**分块多帧**,均带 checksum)。
|
|
1140
|
+
* 流式/分块版 —— 不构造整份大字符串(>536MB 会 ERR_STRING_TOO_LONG);逐帧 roundtrip 校验。
|
|
1141
|
+
* @param {string[]} lines - 按 \n 切开的行数组;末元素必须为 ''(单个换行结尾)。
|
|
1142
|
+
* @param {{chunkBytes?:number}} [opts]
|
|
1143
|
+
* @returns {Buffer}
|
|
1144
|
+
*/
|
|
1145
|
+
export function rebuildZstdLines(lines, { chunkBytes = 64 * 1024 * 1024 } = {}) {
|
|
1146
|
+
if (!Array.isArray(lines) || lines.length === 0) throw new Error('repair: 无行可重建')
|
|
1147
|
+
if (lines[lines.length - 1] !== '') throw new Error('repair: JSONL 必须以单个换行结尾')
|
|
1148
|
+
if (lines.length === 1) throw new Error('repair: JSONL 缺 header 行')
|
|
1149
|
+
const headerLine = lines[0] + '\n'
|
|
1150
|
+
const opts = { params: { [constants.ZSTD_c_checksumFlag]: 1 } };
|
|
1151
|
+
const frames = [zstdCompressSync(Buffer.from(headerLine), opts)]
|
|
1152
|
+
let acc = []; let accBytes = 0
|
|
1153
|
+
const flush = () => { if (acc.length === 0) return; frames.push(zstdCompressSync(Buffer.concat(acc), opts)); acc = []; accBytes = 0 }
|
|
1154
|
+
for (let i = 1; i < lines.length - 1; i += 1) { const b = Buffer.from(lines[i] + '\n'); acc.push(b); accBytes += b.length; if (accBytes >= chunkBytes) flush() }
|
|
1155
|
+
flush()
|
|
1156
|
+
const out = Buffer.concat(frames)
|
|
1157
|
+
// 逐帧 roundtrip(不拼整串):帧1 == header;其余帧逐字节回读通过即可
|
|
1158
|
+
if (zstdDecompressSync(frames[0]).toString('utf8') !== headerLine) throw new Error('repair: 帧1 roundtrip 不一致')
|
|
1159
|
+
for (let i = 1; i < frames.length; i += 1) { const d = zstdDecompressSync(frames[i]); if (!Buffer.isBuffer(d) && typeof d !== 'string') throw new Error('repair: 正文帧 roundtrip 失败') }
|
|
1160
|
+
return out
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1138
1163
|
/**
|
|
1139
1164
|
* 对单个会话日志执行诊断 +(可选)修复。
|
|
1140
1165
|
* @param {string} file - .jsonl 或 .jsonl.zstd 路径。
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-log-contract",
|
|
3
3
|
"description": "日志契约守护 — DSH session log contract guard: offline health check (CLI) + pre-write validation for DeepSeek Harness session logs",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.21",
|
|
5
5
|
"packageManager": "pnpm@11.7.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|