dsh-verify-gate 0.1.0 → 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 +44 -23
- package/README.md +132 -105
- package/README.zh.md +103 -80
- package/index.js +164 -122
- package/package.json +4 -4
- package/scripts/repair-session.mjs +274 -0
- package/test/wiring.test.mjs +139 -105
package/index.js
CHANGED
|
@@ -1,122 +1,164 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-verify-gate — 完成闸门(Host 半边)
|
|
3
|
-
*
|
|
4
|
-
* 背景(本机实测 49 个会话 / 937 个 turn):改过代码文件的 394 个 turn 里,
|
|
5
|
-
* 355 个(90.1%)从头到尾没跑过任何测试或类型检查。
|
|
6
|
-
*
|
|
7
|
-
* 接缝取自 dsh 0.2.2-rc.5 的真实源码(@deepseek-ai/dsh-agent-loop/lib/index.js):
|
|
8
|
-
* - tools/execute waterfall (exec, next);exec.name / exec.arguments
|
|
9
|
-
* - agent/pre-step waterfall (payload, next);可改写 decision.messages
|
|
10
|
-
* - agent/turn-stopping serial (payload)
|
|
11
|
-
*
|
|
12
|
-
* 注意:turn/start、turn/end 是 session 记录(session.append),不是 ctx 事件。
|
|
13
|
-
*
|
|
14
|
-
* 单账本说明:agent/pre-step 的 payload 不含 agent 身份(源码里只有 turn / step /
|
|
15
|
-
* signal / modelSelection),如果记账侧按 agent 分账、读取侧只能读到一份,
|
|
16
|
-
* 那写出来的"隔离"是假的。这里如实使用单账本,并发多代理时会保守地合并判断。
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { randomUUID } from 'node:crypto';
|
|
20
|
-
import {
|
|
21
|
-
createLedger, recordWrite, recordVerification, recordDeclaration,
|
|
22
|
-
verdict, resetLedger, DECLARE_TOOL,
|
|
23
|
-
} from './lib/ledger.js';
|
|
24
|
-
|
|
25
|
-
export const inject = ['tools'];
|
|
26
|
-
|
|
27
|
-
const DECLARE_HINT =
|
|
28
|
-
'Run the project tests (or a type check) before calling the task done, ' +
|
|
29
|
-
'or call verify_exemption with a concrete reason why no check applies.';
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-verify-gate — 完成闸门(Host 半边)
|
|
3
|
+
*
|
|
4
|
+
* 背景(本机实测 49 个会话 / 937 个 turn):改过代码文件的 394 个 turn 里,
|
|
5
|
+
* 355 个(90.1%)从头到尾没跑过任何测试或类型检查。
|
|
6
|
+
*
|
|
7
|
+
* 接缝取自 dsh 0.2.2-rc.5 的真实源码(@deepseek-ai/dsh-agent-loop/lib/index.js):
|
|
8
|
+
* - tools/execute waterfall (exec, next);exec.name / exec.arguments
|
|
9
|
+
* - agent/pre-step waterfall (payload, next);可改写 decision.messages
|
|
10
|
+
* - agent/turn-stopping serial (payload)
|
|
11
|
+
*
|
|
12
|
+
* 注意:turn/start、turn/end 是 session 记录(session.append),不是 ctx 事件。
|
|
13
|
+
*
|
|
14
|
+
* 单账本说明:agent/pre-step 的 payload 不含 agent 身份(源码里只有 turn / step /
|
|
15
|
+
* signal / modelSelection),如果记账侧按 agent 分账、读取侧只能读到一份,
|
|
16
|
+
* 那写出来的"隔离"是假的。这里如实使用单账本,并发多代理时会保守地合并判断。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { randomUUID } from 'node:crypto';
|
|
20
|
+
import {
|
|
21
|
+
createLedger, recordWrite, recordVerification, recordDeclaration,
|
|
22
|
+
verdict, resetLedger, DECLARE_TOOL,
|
|
23
|
+
} from './lib/ledger.js';
|
|
24
|
+
|
|
25
|
+
export const inject = ['tools'];
|
|
26
|
+
|
|
27
|
+
const DECLARE_HINT =
|
|
28
|
+
'Run the project tests (or a type check) before calling the task done, ' +
|
|
29
|
+
'or call verify_exemption with a concrete reason why no check applies.';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Refuse to inject anything the session log cannot hold.
|
|
33
|
+
*
|
|
34
|
+
* DSH persists every `agent/pre-step` message whose source is not
|
|
35
|
+
* `runtime-context` as a `user/message` event, *whatever the message role is*
|
|
36
|
+
* (agent-loop: `this.session.append("user/message", message, ...)`). A
|
|
37
|
+
* `system`-role payload therefore writes a row that the loader rejects —
|
|
38
|
+
* `session event at seq N message must have role "user"` — and the session can
|
|
39
|
+
* no longer be opened at all. This has already happened once (0.1.0); the check
|
|
40
|
+
* lives next to the message shape so a future edit cannot quietly reintroduce it.
|
|
41
|
+
*
|
|
42
|
+
* @param message - candidate message for the next step's context.
|
|
43
|
+
* @returns the same message, guaranteed to be loggable.
|
|
44
|
+
*/
|
|
45
|
+
export function assertInjectable(message) {
|
|
46
|
+
if (message?.role !== 'user') {
|
|
47
|
+
throw new Error('verify-gate: injected messages must use role "user", got ' + JSON.stringify(message?.role));
|
|
48
|
+
}
|
|
49
|
+
const kind = message?.source?.kind;
|
|
50
|
+
if (typeof kind !== 'string' || kind === '' || kind === 'plugin') {
|
|
51
|
+
throw new Error('verify-gate: injected messages need a producer-owned source kind');
|
|
52
|
+
}
|
|
53
|
+
return message;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Build the single reminder message this plugin injects.
|
|
58
|
+
* @param text - already-composed reminder body.
|
|
59
|
+
* @returns a frozen-free user-role message wrapped in <system-reminder>.
|
|
60
|
+
*/
|
|
61
|
+
function nudgeMessage(text) {
|
|
62
|
+
return assertInjectable({
|
|
63
|
+
id: randomUUID(),
|
|
64
|
+
role: 'user',
|
|
65
|
+
content: [{ type: 'text', text: '<system-reminder>\n' + text + '\n</system-reminder>' }],
|
|
66
|
+
source: { kind: 'verify-gate' },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function apply(ctx, config = {}) {
|
|
71
|
+
const enabled = config.enabled !== false;
|
|
72
|
+
const stats = { nudged: 0, clean: 0, exempted: 0 };
|
|
73
|
+
const book = { ledger: createLedger(), nudgedTurn: -1 };
|
|
74
|
+
|
|
75
|
+
// ---- 1. 观察:工具执行后记账 ----
|
|
76
|
+
ctx.effect(() => {
|
|
77
|
+
const dispose = ctx.on('tools/execute', async (exec, next) => {
|
|
78
|
+
if (!enabled) return next();
|
|
79
|
+
const name = String(exec?.name ?? exec?.tool?.name ?? '');
|
|
80
|
+
const args = exec?.arguments ?? exec?.args ?? {};
|
|
81
|
+
const result = await next();
|
|
82
|
+
try {
|
|
83
|
+
if (name === DECLARE_TOOL) recordDeclaration(book.ledger, args?.reason);
|
|
84
|
+
else recordWrite(book.ledger, name, args);
|
|
85
|
+
const text = typeof result === 'string' ? result : JSON.stringify(result ?? '');
|
|
86
|
+
recordVerification(book.ledger, name, args, text);
|
|
87
|
+
} catch {
|
|
88
|
+
// 账本自身出错绝不影响工具结果
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
});
|
|
92
|
+
return () => dispose?.();
|
|
93
|
+
}, 'verify-gate: tool observer');
|
|
94
|
+
|
|
95
|
+
// ---- 2. 下一步之前:把提醒送进模型上下文 ----
|
|
96
|
+
ctx.effect(() => {
|
|
97
|
+
const dispose = ctx.on('agent/pre-step', async (payload, next) => {
|
|
98
|
+
const decision = await next();
|
|
99
|
+
if (!enabled || !decision || decision.kind !== 'enter') return decision;
|
|
100
|
+
try {
|
|
101
|
+
const v = verdict(book.ledger);
|
|
102
|
+
if (v.action !== 'warn') return decision;
|
|
103
|
+
const turn = Number(payload?.turn ?? -1);
|
|
104
|
+
if (book.nudgedTurn === turn) return decision; // 一个 turn 最多提醒一次
|
|
105
|
+
book.nudgedTurn = turn;
|
|
106
|
+
stats.nudged += 1;
|
|
107
|
+
// A user-role reminder, never a system-role one: the harness records every
|
|
108
|
+
// agent/pre-step message that is not runtime-context as a `user/message`
|
|
109
|
+
// session event, so a system-role payload there writes a log that can never
|
|
110
|
+
// be loaded again ("message must have role \"user\""). Wrapping the text in
|
|
111
|
+
// <system-reminder> is the harness's own convention for mid-conversation
|
|
112
|
+
// injections (skill catalog, runtime context), so the model still reads it
|
|
113
|
+
// as harness guidance rather than user speech.
|
|
114
|
+
const text = (v.reason ?? '').trim() || DECLARE_HINT;
|
|
115
|
+
return {
|
|
116
|
+
...decision,
|
|
117
|
+
messages: [...decision.messages, nudgeMessage(text)],
|
|
118
|
+
};
|
|
119
|
+
} catch {
|
|
120
|
+
return decision; // 注入失败一律放行,绝不打断正常流程
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return () => dispose?.();
|
|
124
|
+
}, 'verify-gate: pre-step nudge');
|
|
125
|
+
|
|
126
|
+
// ---- 3. 收工结账 ----
|
|
127
|
+
ctx.effect(() => {
|
|
128
|
+
const dispose = ctx.on('agent/turn-stopping', () => {
|
|
129
|
+
try {
|
|
130
|
+
const v = verdict(book.ledger);
|
|
131
|
+
if (v.code === 'DECLARED') stats.exempted += 1;
|
|
132
|
+
else if (v.code === 'VERIFIED') stats.clean += 1;
|
|
133
|
+
resetLedger(book.ledger);
|
|
134
|
+
book.nudgedTurn = -1;
|
|
135
|
+
} catch { /* 结账出错不影响收工 */ }
|
|
136
|
+
});
|
|
137
|
+
return () => dispose?.();
|
|
138
|
+
}, 'verify-gate: turn settlement');
|
|
139
|
+
|
|
140
|
+
// ---- 4. 豁免工具 ----
|
|
141
|
+
if (config.exemptionTool !== false) {
|
|
142
|
+
ctx.effect(() => ctx.tools.register({
|
|
143
|
+
name: DECLARE_TOOL,
|
|
144
|
+
description:
|
|
145
|
+
"Declare that this turn's code changes need no automated check, with a concrete reason. " +
|
|
146
|
+
'Use only when no test, type check or runnable path applies (generated files, docs-only code, ' +
|
|
147
|
+
'or the check lives in another repository). A vague reason is rejected.',
|
|
148
|
+
parameters: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: { reason: { type: 'string', description: 'Why no verification applies, concretely.' } },
|
|
151
|
+
required: ['reason'],
|
|
152
|
+
additionalProperties: false,
|
|
153
|
+
},
|
|
154
|
+
output: {
|
|
155
|
+
schema: { type: 'object', properties: { accepted: { type: 'boolean' }, code: { type: 'string' } } },
|
|
156
|
+
render: (_a, v) => [{ type: 'text', text: v.accepted ? 'Exemption recorded.' : 'Exemption rejected: reason too vague (need >= 12 chars).' }],
|
|
157
|
+
},
|
|
158
|
+
async execute(args) {
|
|
159
|
+
const accepted = recordDeclaration(book.ledger, args?.reason);
|
|
160
|
+
return { accepted, code: accepted ? 'DECLARED' : 'REASON_TOO_VAGUE' };
|
|
161
|
+
},
|
|
162
|
+
}), 'verify-gate: exemption tool');
|
|
163
|
+
}
|
|
164
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-verify-gate",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Verification gate for DeepSeek Harness — reminds once when a turn changed code but nothing checked it. 改完代码没验证就在收工处叫住你,可申报豁免。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"repository": {
|
|
42
42
|
"type": "git",
|
|
43
|
-
"url": "git+https://github.com/
|
|
43
|
+
"url": "git+https://github.com/Raysnows/dsh-verify-gate.git"
|
|
44
44
|
},
|
|
45
45
|
"bugs": {
|
|
46
|
-
"url": "https://github.com/
|
|
46
|
+
"url": "https://github.com/Raysnows/dsh-verify-gate/issues"
|
|
47
47
|
},
|
|
48
|
-
"homepage": "https://github.com/
|
|
48
|
+
"homepage": "https://github.com/Raysnows/dsh-verify-gate#readme",
|
|
49
49
|
"engines": {
|
|
50
50
|
"node": ">=22"
|
|
51
51
|
},
|
|
@@ -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);
|