dsh-log-contract 0.3.19 → 0.3.20
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 +10 -10
- package/README.zh.md +9 -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)
|
|
@@ -346,8 +346,8 @@ MIT © OfferKuai Team
|
|
|
346
346
|
|
|
347
347
|
## ⚠️ Upgrading to 0.3.17 — behaviour changes you must know (0.3.12 → 0.3.17)
|
|
348
348
|
|
|
349
|
-
> **0.3.17 changes no behaviour.** It
|
|
350
|
-
>
|
|
349
|
+
> **0.3.17 changes no behaviour.** It only adjusts the wording of comments, test titles, a few
|
|
350
|
+
> diagnostic strings and the README. Every verdict, exit code and `--json`
|
|
351
351
|
> field is unchanged from 0.3.15, so everything below applies unchanged when going from 0.3.12 to 0.3.17.
|
|
352
352
|
>
|
|
353
353
|
> 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 时间线联动
|
|
@@ -297,8 +297,8 @@ MIT © OfferKuai Team
|
|
|
297
297
|
|
|
298
298
|
## ⚠️ 升级到 0.3.17 —— 必须知道的行为变更(0.3.12 → 0.3.17)
|
|
299
299
|
|
|
300
|
-
> **0.3.17 不含行为变更。**
|
|
301
|
-
>
|
|
300
|
+
> **0.3.17 不含行为变更。** 它只调整了注释、测试标题、少量诊断文案与 README 的措辞。
|
|
301
|
+
> 所有判定结果、退出码与 `--json` 字段与 0.3.15 完全一致,
|
|
302
302
|
> 因此从 0.3.12 升到 0.3.17 时,下面各条与原来完全一致。
|
|
303
303
|
>
|
|
304
304
|
> 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.20",
|
|
5
5
|
"packageManager": "pnpm@11.7.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|