dsh-verify-gate 0.1.1 → 0.1.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/CHANGELOG.md +8 -0
- package/README.md +20 -0
- package/README.zh.md +18 -0
- package/package.json +57 -57
- package/scripts/repair-session.mjs +274 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.2 — 2026-11-21
|
|
4
|
+
|
|
5
|
+
- Ship `scripts/repair-session.mjs`: repairs session logs that 0.1.0 already broke
|
|
6
|
+
(`user/message` rows carrying a `system` message). Dry-run by default; `--apply` rewrites,
|
|
7
|
+
keeps a `.orig-<timestamp>` backup, verifies the rebuilt log, and leaves every untouched
|
|
8
|
+
frame byte-identical.
|
|
9
|
+
- README (en/zh): how to run the repair, and why it has to happen with DSH closed.
|
|
10
|
+
|
|
3
11
|
## 0.1.1 — 2026-11-21
|
|
4
12
|
|
|
5
13
|
- **Fix: the nudge no longer corrupts session history.** The reminder was injected as a
|
package/README.md
CHANGED
|
@@ -51,6 +51,26 @@ The injected message lands in the model's context for the very next step:
|
|
|
51
51
|
> `<system-reminder>` wrapper is the harness's own convention for mid-conversation
|
|
52
52
|
> injections, so the model still reads it as harness guidance, not user speech.
|
|
53
53
|
|
|
54
|
+
## Repairing a session broken by 0.1.0
|
|
55
|
+
|
|
56
|
+
0.1.0 could write one unloadable row into a session log. Upgrading to 0.1.1+ stops new
|
|
57
|
+
damage, but it does not repair logs that are already broken: the session keeps failing to
|
|
58
|
+
open until that row is rewritten. This package ships the repair:
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
node node_modules/dsh-verify-gate/scripts/repair-session.mjs # dry run, all sessions
|
|
62
|
+
node node_modules/dsh-verify-gate/scripts/repair-session.mjs --apply # rewrite, with backups
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
It finds `user/message` rows whose payload is a `system` message, rewrites them into the
|
|
66
|
+
canonical `system/message` event the harness itself writes (`turn`/`step` taken from the
|
|
67
|
+
preceding `step/start`), recompresses only the Zstandard frame that held the row, verifies
|
|
68
|
+
the rebuilt log before writing, keeps `session.v4.jsonl.zstd.orig-<timestamp>` beside the
|
|
69
|
+
original, and moves that session's projection cache aside so it rebuilds.
|
|
70
|
+
|
|
71
|
+
Run it **while DSH is closed** — it rewrites session artifacts on disk. `--sessions-dir`
|
|
72
|
+
points it at a different root; `--no-projcache` leaves the cache alone.
|
|
73
|
+
|
|
54
74
|
## Design choices
|
|
55
75
|
|
|
56
76
|
- **Warn, never block.** Blocking turns makes every task slower; a reminder costs one line.
|
package/README.zh.md
CHANGED
|
@@ -41,6 +41,24 @@ DSH 自带安全审阅(`dsh-experimental-auto-review`)和循环提示,
|
|
|
41
41
|
> (`... message must have role "user"`),整个会话从此打不开。提醒文本包在
|
|
42
42
|
> `<system-reminder>` 里,模型读到的仍是宿主提醒,不是用户发言。
|
|
43
43
|
|
|
44
|
+
## 修复已被 0.1.0 写坏的会话
|
|
45
|
+
|
|
46
|
+
0.1.0 可能在某条会话日志里留下一行无法加载的记录。升到 0.1.1+ 只止血,**不会修复已经坏掉的日志**:
|
|
47
|
+
在那一行被改写之前,该会话会一直打不开。包里带了修复脚本:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
node node_modules/dsh-verify-gate/scripts/repair-session.mjs # 预演,扫全部会话
|
|
51
|
+
node node_modules/dsh-verify-gate/scripts/repair-session.mjs --apply # 真正改写,自动备份
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
它找出 `user/message` 里装着 `system` 消息的行,改写成宿主自己会写的标准 `system/message`
|
|
55
|
+
事件(`turn`/`step` 取自前面最近的 `step/start`),只重压命中行所在的那一帧 zstd,改写前先
|
|
56
|
+
自检整份日志,原文件原地留一份 `session.v4.jsonl.zstd.orig-<时间戳>`,并把该会话的投影缓存
|
|
57
|
+
挪开让它重建。
|
|
58
|
+
|
|
59
|
+
**请在关闭 DSH 时运行**——它直接改盘上的会话文件。`--sessions-dir` 可指定别的根目录,
|
|
60
|
+
`--no-projcache` 表示不动缓存。
|
|
61
|
+
|
|
44
62
|
## 设计取舍
|
|
45
63
|
|
|
46
64
|
- **只警告,不阻断。** 阻断让每条任务都变慢;提醒只花一行。
|
package/package.json
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-verify-gate",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Verification gate for DeepSeek Harness — reminds once when a turn changed code but nothing checked it. 改完代码没验证就在收工处叫住你,可申报豁免。",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "index.js",
|
|
7
|
-
"exports": {
|
|
8
|
-
".": "./index.js",
|
|
9
|
-
"./lib/ledger.js": "./lib/ledger.js",
|
|
10
|
-
"./package.json": "./package.json"
|
|
11
|
-
},
|
|
12
|
-
"files": [
|
|
13
|
-
"index.js",
|
|
14
|
-
"lib/",
|
|
15
|
-
"locale/",
|
|
16
|
-
"test/",
|
|
17
|
-
"scripts/",
|
|
18
|
-
"cordis.patch.yml",
|
|
19
|
-
"README.md",
|
|
20
|
-
"README.zh.md",
|
|
21
|
-
"LICENSE",
|
|
22
|
-
"CHANGELOG.md"
|
|
23
|
-
],
|
|
24
|
-
"keywords": [
|
|
25
|
-
"dsh",
|
|
26
|
-
"dsh-plugin",
|
|
27
|
-
"deepseek-harness",
|
|
28
|
-
"deepseek",
|
|
29
|
-
"plugin",
|
|
30
|
-
"cordis",
|
|
31
|
-
"harness",
|
|
32
|
-
"verification",
|
|
33
|
-
"gate",
|
|
34
|
-
"testing",
|
|
35
|
-
"test-runner",
|
|
36
|
-
"devtools",
|
|
37
|
-
"developer-tools",
|
|
38
|
-
"agent"
|
|
39
|
-
],
|
|
40
|
-
"license": "MIT",
|
|
41
|
-
"repository": {
|
|
42
|
-
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/Raysnows/dsh-verify-gate.git"
|
|
44
|
-
},
|
|
45
|
-
"bugs": {
|
|
46
|
-
"url": "https://github.com/Raysnows/dsh-verify-gate/issues"
|
|
47
|
-
},
|
|
48
|
-
"homepage": "https://github.com/Raysnows/dsh-verify-gate#readme",
|
|
49
|
-
"engines": {
|
|
50
|
-
"node": ">=22"
|
|
51
|
-
},
|
|
52
|
-
"dsh": {
|
|
53
|
-
"bundle": {
|
|
54
|
-
"patch": "cordis.patch.yml"
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-verify-gate",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Verification gate for DeepSeek Harness — reminds once when a turn changed code but nothing checked it. 改完代码没验证就在收工处叫住你,可申报豁免。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./lib/ledger.js": "./lib/ledger.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"index.js",
|
|
14
|
+
"lib/",
|
|
15
|
+
"locale/",
|
|
16
|
+
"test/",
|
|
17
|
+
"scripts/",
|
|
18
|
+
"cordis.patch.yml",
|
|
19
|
+
"README.md",
|
|
20
|
+
"README.zh.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"CHANGELOG.md"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"dsh",
|
|
26
|
+
"dsh-plugin",
|
|
27
|
+
"deepseek-harness",
|
|
28
|
+
"deepseek",
|
|
29
|
+
"plugin",
|
|
30
|
+
"cordis",
|
|
31
|
+
"harness",
|
|
32
|
+
"verification",
|
|
33
|
+
"gate",
|
|
34
|
+
"testing",
|
|
35
|
+
"test-runner",
|
|
36
|
+
"devtools",
|
|
37
|
+
"developer-tools",
|
|
38
|
+
"agent"
|
|
39
|
+
],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/Raysnows/dsh-verify-gate.git"
|
|
44
|
+
},
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/Raysnows/dsh-verify-gate/issues"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/Raysnows/dsh-verify-gate#readme",
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=22"
|
|
51
|
+
},
|
|
52
|
+
"dsh": {
|
|
53
|
+
"bundle": {
|
|
54
|
+
"patch": "cordis.patch.yml"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Repair session logs that dsh-verify-gate 0.1.0 made unloadable.
|
|
4
|
+
*
|
|
5
|
+
* 0.1.0 injected its reminder as a \`system\`-role \`agent/pre-step\` message. DSH
|
|
6
|
+
* records every non-\`runtime-context\` pre-step message as a \`user/message\`
|
|
7
|
+
* session event *whatever the role is*, so the stored row was
|
|
8
|
+
* \`type: "user/message", data.role: "system"\`. The loader rejects that
|
|
9
|
+
* (\`session event at seq N message must have role "user"\`) and the whole
|
|
10
|
+
* session stops opening.
|
|
11
|
+
*
|
|
12
|
+
* What this does, per affected row:
|
|
13
|
+
* - rewrites it to the canonical \`system/message\` event DSH itself writes,
|
|
14
|
+
* with \`turn\`/\`step\` taken from the \`step/start\` immediately before it;
|
|
15
|
+
* - recompresses only the Zstandard frame that held the row, byte-for-byte
|
|
16
|
+
* identical everywhere else;
|
|
17
|
+
* - verifies the rebuilt log (every row parses, sequence stays dense, no
|
|
18
|
+
* role/type mismatch remains) before anything is written;
|
|
19
|
+
* - keeps the original next to it as \`.orig-<timestamp>\`;
|
|
20
|
+
* - moves the session's projection-cache document aside so it rebuilds.
|
|
21
|
+
*
|
|
22
|
+
* Usage:
|
|
23
|
+
* node scripts/repair-session.mjs # dry run, all sessions
|
|
24
|
+
* node scripts/repair-session.mjs --apply # write the repairs
|
|
25
|
+
* node scripts/repair-session.mjs --apply <id>... # only these sessions
|
|
26
|
+
* node scripts/repair-session.mjs --sessions-dir <dir>
|
|
27
|
+
* node scripts/repair-session.mjs --no-projcache # leave the cache alone
|
|
28
|
+
*
|
|
29
|
+
* Requires Node.js >= 22.15 (zlib zstd support). Exit code 0 = nothing left to
|
|
30
|
+
* repair or every repair verified; 1 = something needed attention.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { readFileSync, writeFileSync, readdirSync, statSync, existsSync, mkdirSync, copyFileSync, renameSync, rmSync } from 'node:fs';
|
|
34
|
+
import { join, basename, dirname } from 'node:path';
|
|
35
|
+
import { homedir } from 'node:os';
|
|
36
|
+
import { zstdCompressSync, zstdDecompressSync, constants as zlibConstants } from 'node:zlib';
|
|
37
|
+
|
|
38
|
+
const ZSTD_MAGIC = 0xfd2fb528;
|
|
39
|
+
const ROLE_BY_TYPE = {
|
|
40
|
+
'system/message': 'system',
|
|
41
|
+
'developer/message': 'developer',
|
|
42
|
+
'user/message': 'user',
|
|
43
|
+
'assistant/message': 'assistant',
|
|
44
|
+
'tool/result': 'tool',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const args = process.argv.slice(2);
|
|
48
|
+
const apply = args.includes('--apply');
|
|
49
|
+
const noProjcache = args.includes('--no-projcache');
|
|
50
|
+
const dirFlag = args.indexOf('--sessions-dir');
|
|
51
|
+
const sessionsDir = dirFlag >= 0 ? args[dirFlag + 1] : join(homedir(), '.dsh', 'sessions');
|
|
52
|
+
const onlyIds = args.filter((a) => !a.startsWith('--') && a !== sessionsDir);
|
|
53
|
+
const projcacheDir = join(homedir(), '.dsh', 'storages', 'session_projcache', 'sessions');
|
|
54
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
|
55
|
+
|
|
56
|
+
/** Walk a directory tree for session log artifacts. */
|
|
57
|
+
function findLogs(dir, out = []) {
|
|
58
|
+
let entries;
|
|
59
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
60
|
+
for (const entry of entries) {
|
|
61
|
+
const path = join(dir, entry.name);
|
|
62
|
+
if (entry.isDirectory()) findLogs(path, out);
|
|
63
|
+
else if (/^session.*\.jsonl(\.zstd)?$/.test(entry.name)) out.push(path);
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Split a Zstandard container into complete frame ranges without decompressing. */
|
|
69
|
+
function scanFrames(buffer) {
|
|
70
|
+
const frames = [];
|
|
71
|
+
let offset = 0;
|
|
72
|
+
while (offset < buffer.length) {
|
|
73
|
+
const start = offset;
|
|
74
|
+
if (buffer.length - offset < 4) throw new Error('torn frame magic at byte ' + offset);
|
|
75
|
+
if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) throw new Error('invalid frame magic at byte ' + offset);
|
|
76
|
+
offset += 4;
|
|
77
|
+
const descriptor = buffer.readUInt8(offset);
|
|
78
|
+
offset += 1;
|
|
79
|
+
if ((descriptor & 24) !== 0) throw new Error('reserved frame-header bit at byte ' + (offset - 1));
|
|
80
|
+
const contentSizeFlag = descriptor >>> 6;
|
|
81
|
+
const singleSegment = (descriptor & 32) !== 0;
|
|
82
|
+
const checksum = (descriptor & 4) !== 0;
|
|
83
|
+
const dictionaryFlag = descriptor & 3;
|
|
84
|
+
const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag;
|
|
85
|
+
const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : 1 << contentSizeFlag;
|
|
86
|
+
offset += (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes;
|
|
87
|
+
for (;;) {
|
|
88
|
+
const blockHeader = buffer.readUIntLE(offset, 3);
|
|
89
|
+
offset += 3;
|
|
90
|
+
const lastBlock = (blockHeader & 1) !== 0;
|
|
91
|
+
const blockType = (blockHeader >>> 1) & 3;
|
|
92
|
+
const blockSize = blockHeader >>> 3;
|
|
93
|
+
if (blockType === 3) throw new Error('reserved block type at byte ' + (offset - 3));
|
|
94
|
+
offset += blockType === 1 ? 1 : blockSize;
|
|
95
|
+
if (lastBlock) break;
|
|
96
|
+
}
|
|
97
|
+
if (checksum) offset += 4;
|
|
98
|
+
frames.push({ start, end: offset });
|
|
99
|
+
}
|
|
100
|
+
return frames;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Decode one whole container into its JSONL text. */
|
|
104
|
+
function decodeLog(buffer) {
|
|
105
|
+
const frames = scanFrames(buffer);
|
|
106
|
+
const parts = frames.map((frame) => zstdDecompressSync(buffer.subarray(frame.start, frame.end)));
|
|
107
|
+
return { frames, text: Buffer.concat(parts).toString('utf8') };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Parse JSONL text into rows; a trailing empty line is not a row. */
|
|
111
|
+
function parseRows(text) {
|
|
112
|
+
return text.split('\n').filter((line) => line.length > 0).map((line) => JSON.parse(line));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The message carried by a surface event, mirroring the loader's own rule. */
|
|
116
|
+
function messageOf(row) {
|
|
117
|
+
if (!(row.type in ROLE_BY_TYPE)) return undefined;
|
|
118
|
+
return row.type === 'user/message' ? row.data : row.data?.message;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Turn/step of the step that contains seq, from the nearest preceding boundary. */
|
|
122
|
+
function positionOf(bySeq, seq) {
|
|
123
|
+
for (let s = seq - 1; s >= 0; s -= 1) {
|
|
124
|
+
const row = bySeq.get(s);
|
|
125
|
+
if (row?.type === 'step/start') return { turn: row.data.turn, step: row.data.step };
|
|
126
|
+
if (row?.type === 'turn/start') return { turn: row.data.turn, step: 1 };
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Rewrite one corrupt row into the canonical system-message event.
|
|
133
|
+
* @returns the repaired row, or undefined when the row is not this defect.
|
|
134
|
+
*/
|
|
135
|
+
function repairRow(row, bySeq) {
|
|
136
|
+
if (row.type !== 'user/message') return undefined;
|
|
137
|
+
const message = row.data;
|
|
138
|
+
if (message?.role !== 'system' || message?.source?.kind !== 'system-prompt') return undefined;
|
|
139
|
+
const position = positionOf(bySeq, row.seq);
|
|
140
|
+
if (position === undefined) return undefined;
|
|
141
|
+
return {
|
|
142
|
+
type: 'system/message',
|
|
143
|
+
seq: row.seq,
|
|
144
|
+
time: row.time,
|
|
145
|
+
data: {
|
|
146
|
+
turn: position.turn,
|
|
147
|
+
step: position.step,
|
|
148
|
+
message: { id: message.id, role: message.role, content: message.content, source: message.source },
|
|
149
|
+
},
|
|
150
|
+
surfaceOp: row.surfaceOp ?? 'append',
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Rebuild the container with only the affected frames recompressed. */
|
|
155
|
+
function rebuild(buffer, frames, replacements) {
|
|
156
|
+
const pieces = [];
|
|
157
|
+
let cursor = 0;
|
|
158
|
+
for (const frame of frames) {
|
|
159
|
+
const text = zstdDecompressSync(buffer.subarray(frame.start, frame.end)).toString('utf8');
|
|
160
|
+
const lines = text.split('\n');
|
|
161
|
+
let changed = false;
|
|
162
|
+
const rebuilt = lines.map((line) => {
|
|
163
|
+
if (line.length === 0) return line;
|
|
164
|
+
const row = JSON.parse(line);
|
|
165
|
+
const replacement = replacements.get(row.seq);
|
|
166
|
+
if (replacement === undefined) return line;
|
|
167
|
+
changed = true;
|
|
168
|
+
return JSON.stringify(replacement);
|
|
169
|
+
});
|
|
170
|
+
if (!changed) continue;
|
|
171
|
+
pieces.push(buffer.subarray(cursor, frame.start));
|
|
172
|
+
pieces.push(zstdCompressSync(Buffer.from(rebuilt.join('\n'), 'utf8'), { params: { [zlibConstants.ZSTD_c_checksumFlag]: 1 } }));
|
|
173
|
+
cursor = frame.end;
|
|
174
|
+
}
|
|
175
|
+
pieces.push(buffer.subarray(cursor));
|
|
176
|
+
return Buffer.concat(pieces);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Verify a rebuilt container: dense seqs, parsable rows, no role/type mismatch. */
|
|
180
|
+
function verify(candidate) {
|
|
181
|
+
const { text } = decodeLog(candidate);
|
|
182
|
+
const rows = parseRows(text);
|
|
183
|
+
if (rows.length < 2) throw new Error('rebuilt log has no events');
|
|
184
|
+
const events = rows.slice(1);
|
|
185
|
+
events.forEach((row, index) => {
|
|
186
|
+
if (row.seq !== index) throw new Error('event ' + index + ' is not dense (seq ' + row.seq + ')');
|
|
187
|
+
const message = messageOf(row);
|
|
188
|
+
if (message !== undefined && message.role !== ROLE_BY_TYPE[row.type]) {
|
|
189
|
+
throw new Error('seq ' + row.seq + ' still mismatches: ' + row.type + ' / ' + message.role);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
return events.length;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const logs = findLogs(sessionsDir).filter((path) => path.endsWith('.zstd'));
|
|
196
|
+
if (logs.length === 0) {
|
|
197
|
+
console.log('no compressed session logs under ' + sessionsDir);
|
|
198
|
+
process.exit(0);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let repairedFiles = 0;
|
|
202
|
+
let repairedRows = 0;
|
|
203
|
+
let cleanFiles = 0;
|
|
204
|
+
let failed = 0;
|
|
205
|
+
|
|
206
|
+
for (const path of logs) {
|
|
207
|
+
const id = basename(dirname(path));
|
|
208
|
+
if (onlyIds.length > 0 && !onlyIds.some((needle) => id.includes(needle))) continue;
|
|
209
|
+
let buffer;
|
|
210
|
+
try { buffer = readFileSync(path); } catch (error) {
|
|
211
|
+
console.log('SKIP ' + id + ' (' + error.message + ')');
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
let frames;
|
|
215
|
+
let rows;
|
|
216
|
+
try {
|
|
217
|
+
const decoded = decodeLog(buffer);
|
|
218
|
+
frames = decoded.frames;
|
|
219
|
+
rows = parseRows(decoded.text);
|
|
220
|
+
} catch (error) {
|
|
221
|
+
console.log('FAIL ' + id + ' (' + error.message + ')');
|
|
222
|
+
failed += 1;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
const bySeq = new Map();
|
|
226
|
+
for (const row of rows) if (typeof row.seq === 'number') bySeq.set(row.seq, row);
|
|
227
|
+
const replacements = new Map();
|
|
228
|
+
for (const row of rows) {
|
|
229
|
+
const replacement = repairRow(row, bySeq);
|
|
230
|
+
if (replacement !== undefined) replacements.set(row.seq, replacement);
|
|
231
|
+
}
|
|
232
|
+
if (replacements.size === 0) { cleanFiles += 1; continue; }
|
|
233
|
+
|
|
234
|
+
const seqs = [...replacements.keys()].sort((a, b) => a - b);
|
|
235
|
+
if (!apply) {
|
|
236
|
+
console.log('WOULD REPAIR ' + id + ' seq ' + seqs.join(', '));
|
|
237
|
+
repairedFiles += 1;
|
|
238
|
+
repairedRows += seqs.length;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
const candidate = rebuild(buffer, frames, replacements);
|
|
244
|
+
const count = verify(candidate);
|
|
245
|
+
const backup = path + '.orig-' + stamp;
|
|
246
|
+
copyFileSync(path, backup);
|
|
247
|
+
const staged = path + '.repair-staged';
|
|
248
|
+
writeFileSync(staged, candidate);
|
|
249
|
+
renameSync(staged, path);
|
|
250
|
+
let cacheNote = '';
|
|
251
|
+
const cacheDoc = join(projcacheDir, id + '.json');
|
|
252
|
+
if (!noProjcache && existsSync(cacheDoc)) {
|
|
253
|
+
const cacheBackup = join(projcacheDir, id + '.json.orig-' + stamp);
|
|
254
|
+
copyFileSync(cacheDoc, cacheBackup);
|
|
255
|
+
rmSync(cacheDoc);
|
|
256
|
+
cacheNote = ' projection cache moved aside';
|
|
257
|
+
}
|
|
258
|
+
console.log('REPAIRED ' + id + ' seq ' + seqs.join(', ') + ' (' + count + ' events verified)' + cacheNote);
|
|
259
|
+
console.log(' backup: ' + basename(backup));
|
|
260
|
+
repairedFiles += 1;
|
|
261
|
+
repairedRows += seqs.length;
|
|
262
|
+
} catch (error) {
|
|
263
|
+
console.log('FAIL ' + id + ' (' + error.message + ')');
|
|
264
|
+
failed += 1;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
console.log('');
|
|
269
|
+
console.log(
|
|
270
|
+
(apply ? 'repaired ' : 'would repair ') + repairedFiles + ' session(s), ' +
|
|
271
|
+
repairedRows + ' event(s); ' + cleanFiles + ' already clean; ' + failed + ' failed',
|
|
272
|
+
);
|
|
273
|
+
if (!apply && repairedFiles > 0) console.log('dry run only — re-run with --apply to write the repairs');
|
|
274
|
+
process.exit(failed > 0 ? 1 : 0);
|