dsh-log-contract 0.3.23 → 0.3.24
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 +2 -18
- package/README.zh.md +4 -11
- package/bin/dsh-log-contract.mjs +4 -4
- package/docs/CONTRACTS.md +34 -34
- package/lib/archive.js +304 -14
- package/lib/checks.js +1332 -64
- package/lib/compat.js +278 -7
- package/lib/contracts.js +522 -8
- package/lib/host-probes.js +178 -4
- package/lib/legacy-fold.js +174 -4
- package/lib/log-reader.js +295 -3
- package/lib/prewrite.js +4 -4
- package/lib/repair.js +11 -11
- package/lib/validate.js +320 -49
- package/lib/version-support.js +146 -4
- package/lib/vocab.js +227 -4
- package/package.json +5 -15
- package/adapters/dsh/archive.js +0 -135
- package/adapters/dsh/event-shapes.js +0 -639
- package/adapters/dsh/format-versions.js +0 -743
- package/adapters/dsh/frames.js +0 -331
- package/adapters/dsh/host-probes.js +0 -212
- package/adapters/dsh/index.js +0 -35
- package/adapters/dsh/rules.js +0 -560
- package/adapters/dsh/surface.js +0 -792
- package/adapters/dsh/vocab.js +0 -66
- package/contract/index.js +0 -17
- package/contract/invariants.js +0 -478
- package/contract/registry.js +0 -77
- package/contract/rules.js +0 -70
- package/contract/verdict.js +0 -52
- package/docs/ARCHITECTURE.md +0 -46
package/adapters/dsh/frames.js
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-log-contract · adapters/dsh/frames.js
|
|
3
|
-
*
|
|
4
|
-
* DSH 专属:压缩帧结构(多帧布局、尾帧、帧解码、行切分)与日志读取。
|
|
5
|
-
*
|
|
6
|
-
* 契约层的不变量是「容器由若干帧组成、每帧可解码、首帧独占记录头、重写不得改变帧布局」;
|
|
7
|
-
* 压缩格式细节与读取路径全部落在这里。
|
|
8
|
-
*/
|
|
9
|
-
import fs from 'node:fs';
|
|
10
|
-
import { zstdDecompressSync } from 'node:zlib';
|
|
11
|
-
import { violation } from '../../contract/verdict.js';
|
|
12
|
-
import { decodeStorageRecord, normalizeEventSeqRanges } from './event-shapes.js';
|
|
13
|
-
|
|
14
|
-
const ZSTD_MAGIC = 0xfd2fb528;
|
|
15
|
-
|
|
16
|
-
/** 扫描 zstd 帧边界;返回 [start, end] 列表与是否出现撕裂尾帧。 */
|
|
17
|
-
export function scanZstdFrames(buf) {
|
|
18
|
-
const frames = [];
|
|
19
|
-
let offset = 0;
|
|
20
|
-
let torn = false;
|
|
21
|
-
while (offset < buf.length) {
|
|
22
|
-
const start = offset;
|
|
23
|
-
if (buf.length - offset < 4 || buf.readUInt32LE(offset) !== ZSTD_MAGIC) {
|
|
24
|
-
torn = true;
|
|
25
|
-
break;
|
|
26
|
-
}
|
|
27
|
-
offset += 4;
|
|
28
|
-
if (offset === buf.length) {
|
|
29
|
-
torn = true;
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
const descriptor = buf.readUInt8(offset);
|
|
33
|
-
offset += 1;
|
|
34
|
-
if ((descriptor & 24) !== 0) {
|
|
35
|
-
// 保留位被置位:非法帧头
|
|
36
|
-
return { frames, torn: true, reason: 'reserved-bit' };
|
|
37
|
-
}
|
|
38
|
-
const contentSizeFlag = descriptor >>> 6;
|
|
39
|
-
const singleSegment = (descriptor & 32) !== 0;
|
|
40
|
-
const checksum = (descriptor & 4) !== 0;
|
|
41
|
-
const dictFlag = descriptor & 3;
|
|
42
|
-
const dictBytes = dictFlag === 3 ? 4 : dictFlag;
|
|
43
|
-
const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : (1 << contentSizeFlag);
|
|
44
|
-
const hdrExtra = (singleSegment ? 0 : 1) + dictBytes + contentSizeBytes;
|
|
45
|
-
if (buf.length - offset < hdrExtra) {
|
|
46
|
-
torn = true;
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
offset += hdrExtra;
|
|
50
|
-
let lastBlock = false;
|
|
51
|
-
while (!lastBlock) {
|
|
52
|
-
if (buf.length - offset < 3) {
|
|
53
|
-
torn = true;
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
const bh = buf.readUInt32LE(offset);
|
|
57
|
-
offset += 3;
|
|
58
|
-
lastBlock = (bh & 1) !== 0;
|
|
59
|
-
offset += (bh >>> 3) & 0x1fffff;
|
|
60
|
-
}
|
|
61
|
-
if (torn) break;
|
|
62
|
-
if (offset > buf.length) {
|
|
63
|
-
// 块内容越过文件末尾:最后一帧被截断,不能算完整帧
|
|
64
|
-
torn = true;
|
|
65
|
-
break;
|
|
66
|
-
}
|
|
67
|
-
if (checksum) offset += 4;
|
|
68
|
-
frames.push([start, offset]);
|
|
69
|
-
}
|
|
70
|
-
return { frames, torn, reason: torn ? 'incomplete-tail' : undefined };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** 把 zstd 多帧拼成完整明文;任一帧解码失败即抛错(N5 单帧全损语义)。 */
|
|
74
|
-
export function decompressZstd(buf, { allowTorn = true } = {}) {
|
|
75
|
-
const { frames, torn } = scanZstdFrames(buf);
|
|
76
|
-
// ── 这里的 Z2 是误报形状 ───────────────────────────────────────────────
|
|
77
|
-
// 撕裂尾帧**不是**损坏:宿主 `dsh-session-persistence-jsonl@0.1.5-rc.1`
|
|
78
|
-
// · `readZstdPrefix`(lib/index.js:2791-2849)——"Decode complete frames and retain
|
|
79
|
-
// complete JSONL records from a torn final frame",恢复并返回 `tornTruncateTo`(:2847);
|
|
80
|
-
// · 下次写入 `:226-228` 调 `truncateTornTail` 自动截断。
|
|
81
|
-
// 若对 torn 直接抛错 ⇒ `loadSessionLog` 返回 header=null/events=[]
|
|
82
|
-
// ⇒ `validate.js` 级联出 Z1(warning)+Z2(error)+H1(error)、verdict=broken —— 把宿主
|
|
83
|
-
// **本来能修**的"边写边读"健康态报成"会话坏了"。现在对齐宿主:解出全部**完整帧**,
|
|
84
|
-
// 尾帧未写完的部分丢弃,由调用方用 `frameInfo.torn` 报 Z1(warning)。
|
|
85
|
-
// `frames.length === 0`(连一个完整帧都没有,例如头帧就断了)**仍然报错** —— 宿主
|
|
86
|
-
// `readZstdPrefix:2793` 对这种情况同样抛 "empty or header-less Zstandard session log"。
|
|
87
|
-
if (frames.length === 0) throw new Error('空文件 / 无完整 zstd 帧(incomplete or header-less session log)');
|
|
88
|
-
if (torn && !allowTorn) throw new Error('zstd 尾帧撕裂(incomplete tail frame)——strict 模式(allowTorn:false)');
|
|
89
|
-
// 逐帧解码后一次性 Buffer.concat:避免每帧一次 concat 的 O(n²) 拷贝
|
|
90
|
-
const parts = new Array(frames.length);
|
|
91
|
-
for (let i = 0; i < frames.length; i++) {
|
|
92
|
-
const [s, e] = frames[i];
|
|
93
|
-
try {
|
|
94
|
-
parts[i] = zstdDecompressSync(buf.subarray(s, e));
|
|
95
|
-
} catch (err) {
|
|
96
|
-
// 完整帧解码失败才是真损坏(Z2):磁盘 bitrot / 传输截断 / 保留位非法
|
|
97
|
-
throw new Error(`zstd 帧解码失败 [${s},${e}):${err.message}`);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return Buffer.concat(parts);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 读取文件尾部最后一条事件的 seq(看门狗 R1 用,只解最后一帧,O(帧数) 内)。
|
|
105
|
-
*
|
|
106
|
-
* 语义:zstd 多帧文件取最后一个**完整**帧解压(撕裂尾帧跳过——可能正在写入),
|
|
107
|
-
* 从帧内末尾向前找第一条合法 JSON 行,用 `decodeStorageRecord` 展开取末事件 seq
|
|
108
|
-
* (chunk 行展开后末成员即该 run 的最新 seq)。明文文件直接扫末尾。
|
|
109
|
-
*
|
|
110
|
-
* @param {string} path 日志文件路径
|
|
111
|
-
* @returns {number|null} 尾部 seq;读取失败/无可解析行返回 null(调用方降级不检查)。
|
|
112
|
-
*/
|
|
113
|
-
export function tailSeq(path) {
|
|
114
|
-
let buf;
|
|
115
|
-
try {
|
|
116
|
-
buf = fs.readFileSync(path);
|
|
117
|
-
} catch {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
const isZstd = buf.length >= 4 && buf.readUInt32LE(0) === ZSTD_MAGIC;
|
|
121
|
-
let plain;
|
|
122
|
-
if (isZstd) {
|
|
123
|
-
const { frames } = scanZstdFrames(buf);
|
|
124
|
-
if (frames.length === 0) return null;
|
|
125
|
-
const [s, e] = frames[frames.length - 1];
|
|
126
|
-
try {
|
|
127
|
-
plain = zstdDecompressSync(buf.subarray(s, e));
|
|
128
|
-
} catch {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
} else {
|
|
132
|
-
plain = buf;
|
|
133
|
-
}
|
|
134
|
-
const lines = plain.toString('utf8').split('\n');
|
|
135
|
-
for (let i = lines.length - 1; i >= 0; i--) {
|
|
136
|
-
const line = lines[i];
|
|
137
|
-
if (line.trim().length === 0) continue; // 帧边界产物(空行)跳过
|
|
138
|
-
try {
|
|
139
|
-
const decoded = decodeStorageRecord(JSON.parse(line));
|
|
140
|
-
if (decoded.length > 0) return decoded[decoded.length - 1].seq;
|
|
141
|
-
} catch { /* 损坏行/不可解析:继续向前找 */ }
|
|
142
|
-
}
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 读取并解码一个会话日志文件(.jsonl / .jsonl.zstd / 任意带 zstd 魔数的文件)。
|
|
148
|
-
*
|
|
149
|
-
* @param {string} path 日志文件路径
|
|
150
|
-
* @returns {{
|
|
151
|
-
* header: object|null, headerLine: number|null,
|
|
152
|
-
* rows: Array<{lineNo:number, value:unknown, decoded:Array|null, error:Error|null}>,
|
|
153
|
-
* events: Array<{seq:number, event:object, lineNo:number}>,
|
|
154
|
-
* frameInfo: {frames:number, torn:boolean, compressedBytes:number, plaintextBytes:number},
|
|
155
|
-
* }}
|
|
156
|
-
*/
|
|
157
|
-
export function loadSessionLog(path) {
|
|
158
|
-
const buf = fs.readFileSync(path);
|
|
159
|
-
const isZstd = buf.length >= 4 && buf.readUInt32LE(0) === ZSTD_MAGIC;
|
|
160
|
-
|
|
161
|
-
let plain;
|
|
162
|
-
let frameInfo;
|
|
163
|
-
let loadError = null;
|
|
164
|
-
if (isZstd) {
|
|
165
|
-
try {
|
|
166
|
-
plain = decompressZstd(buf);
|
|
167
|
-
} catch (err) {
|
|
168
|
-
loadError = err;
|
|
169
|
-
plain = Buffer.alloc(0);
|
|
170
|
-
}
|
|
171
|
-
const { frames, torn } = scanZstdFrames(buf);
|
|
172
|
-
// 帧形状事实(Z4/Z5):官方写盘形状 = **header 独占第一帧** +
|
|
173
|
-
// 事件逐帧追加;读取端有硬断言 `assertZstdHeaderFrame`(首帧必须**恰好一行**)。
|
|
174
|
-
// 这里把该事实带出来给 validate 判:整文件被重压成单帧时,首帧装的是全部行 ⇒ 宿主必抛,
|
|
175
|
-
// 而抛点在 `listArtifacts` **遍历全库**、无 per-session 容错 ⇒ 整个客户端起不来。
|
|
176
|
-
let firstFrameOneLine = true, firstFrameLines = 1;
|
|
177
|
-
try {
|
|
178
|
-
const [s0, e0] = frames[0];
|
|
179
|
-
const first = zstdDecompressSync(buf.subarray(s0, e0)).toString('utf8');
|
|
180
|
-
const nl = first.indexOf('\n');
|
|
181
|
-
firstFrameOneLine = nl === first.length - 1 && first.length > 1;
|
|
182
|
-
firstFrameLines = first.split('\n').filter((x) => x !== '').length;
|
|
183
|
-
} catch { /* 首帧解码失败另有 Z2 报 */ }
|
|
184
|
-
frameInfo = { frames: frames.length, torn, compressedBytes: buf.length, plaintextBytes: plain.length, firstFrameOneLine, firstFrameLines };
|
|
185
|
-
} else {
|
|
186
|
-
plain = buf;
|
|
187
|
-
frameInfo = { frames: 0, torn: false, compressedBytes: 0, plaintextBytes: plain.length };
|
|
188
|
-
}
|
|
189
|
-
if (loadError) {
|
|
190
|
-
return {
|
|
191
|
-
header: null,
|
|
192
|
-
headerLine: 0,
|
|
193
|
-
rows: [],
|
|
194
|
-
events: [],
|
|
195
|
-
frameInfo: { ...frameInfo, error: loadError.message },
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// 按行解码(Buffer 切分),避免整文件字符串化(>536MB 抛字符串上限)
|
|
200
|
-
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 })();
|
|
201
|
-
|
|
202
|
-
// 第一行 = header
|
|
203
|
-
const headerLine = 0;
|
|
204
|
-
let header = null;
|
|
205
|
-
const headerRaw = lines[0];
|
|
206
|
-
try {
|
|
207
|
-
header = JSON.parse(headerRaw);
|
|
208
|
-
} catch {
|
|
209
|
-
header = null;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const rows = [];
|
|
213
|
-
const events = [];
|
|
214
|
-
for (let i = 1; i < lines.length; i++) {
|
|
215
|
-
const line = lines[i];
|
|
216
|
-
if (line.trim().length === 0) continue; // 帧边界产物(空行)跳过
|
|
217
|
-
const lineNo = i;
|
|
218
|
-
let value;
|
|
219
|
-
try {
|
|
220
|
-
value = JSON.parse(line);
|
|
221
|
-
} catch (err) {
|
|
222
|
-
rows.push({ lineNo, value: null, decoded: null, error: err });
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
let decoded;
|
|
226
|
-
try {
|
|
227
|
-
decoded = decodeStorageRecord(value);
|
|
228
|
-
} catch (err) {
|
|
229
|
-
rows.push({ lineNo, value, decoded: null, error: err });
|
|
230
|
-
continue;
|
|
231
|
-
}
|
|
232
|
-
// v3 storage-form `sourceEventSeqs` 区间编码 → 内存稠密序列(C1)。
|
|
233
|
-
// 必须在**读取层唯一入口**做:下游 S5/S6 与官方 foldSurface 都按稠密整数序列判定,
|
|
234
|
-
// 拿到含 `[start,end]` 对的未展开形态会误判真实健康会话(S5/S6/S8 + --resume broken)。
|
|
235
|
-
const normalized = decoded.map(normalizeEventSeqRanges);
|
|
236
|
-
for (const event of normalized) {
|
|
237
|
-
events.push({ seq: event.seq, event, lineNo });
|
|
238
|
-
}
|
|
239
|
-
rows.push({ lineNo, value, decoded: normalized, error: null });
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// 按 seq 排序(文件顺序即日志顺序,此处防御性排序以便下游契约检查)
|
|
243
|
-
events.sort((a, b) => a.seq - b.seq);
|
|
244
|
-
|
|
245
|
-
return { header, headerLine, rows, events, frameInfo };
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 轻量读取会话文件 header(只解第一帧,不读全文件帧)。
|
|
250
|
-
*
|
|
251
|
-
* 帧布局:帧 1 = header 行(单行 JSON,~几百字节),帧 2+ = 事件流。
|
|
252
|
-
* 短码推导(工作区 createdAt 序号)需要扫全部会话但只需要 header——
|
|
253
|
-
* 读文件前缀 64KiB 足够覆盖完整帧 1,避免全量读大文件(大会话压缩后可达
|
|
254
|
-
* 数百 MiB)。失败返回 null(调用方降级)。
|
|
255
|
-
*
|
|
256
|
-
* @param {string} path 会话日志文件路径(.jsonl.zstd 或明文 .jsonl)。
|
|
257
|
-
* @returns {object|null} header 对象;无法解析返回 null。
|
|
258
|
-
*/
|
|
259
|
-
export function readSessionHeader(path) {
|
|
260
|
-
let head;
|
|
261
|
-
try {
|
|
262
|
-
const fd = fs.openSync(path, 'r');
|
|
263
|
-
const buf = Buffer.alloc(64 * 1024);
|
|
264
|
-
const n = fs.readSync(fd, buf, 0, buf.length, 0);
|
|
265
|
-
fs.closeSync(fd);
|
|
266
|
-
head = buf.subarray(0, n);
|
|
267
|
-
} catch {
|
|
268
|
-
return null;
|
|
269
|
-
}
|
|
270
|
-
if (head.length < 4 || head.readUInt32LE(0) !== ZSTD_MAGIC) {
|
|
271
|
-
// 明文(无 zstd magic):直接取首行
|
|
272
|
-
try {
|
|
273
|
-
const line = head.toString('utf8').split('\n').find((l) => l.trim().length > 0);
|
|
274
|
-
return line ? JSON.parse(line) : null;
|
|
275
|
-
} catch {
|
|
276
|
-
return null;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
const { frames } = scanZstdFrames(head);
|
|
280
|
-
if (frames.length === 0) return null;
|
|
281
|
-
const [s, e] = frames[0];
|
|
282
|
-
let plain;
|
|
283
|
-
try {
|
|
284
|
-
plain = zstdDecompressSync(head.subarray(s, e));
|
|
285
|
-
} catch {
|
|
286
|
-
return null;
|
|
287
|
-
}
|
|
288
|
-
const line = plain.toString('utf8').split('\n').find((l) => l.trim().length > 0);
|
|
289
|
-
if (!line) return null;
|
|
290
|
-
try {
|
|
291
|
-
return JSON.parse(line);
|
|
292
|
-
} catch {
|
|
293
|
-
return null;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
// ── 帧结构判定(契约规则 Z 组)───────────────────────────────────────────────
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* 压缩帧结构判定。
|
|
302
|
-
*
|
|
303
|
-
* - Z1 尾帧撕裂(写入中的 in-flight 帧或文件被截断)
|
|
304
|
-
* - Z2 帧解码失败(该片数据全损)
|
|
305
|
-
* - Z4 首帧必须恰好承载一行记录头(客户端遍历全库时会抛)
|
|
306
|
-
* - Z5 多行日志不得压成单帧(写回必须保形)
|
|
307
|
-
*
|
|
308
|
-
* @param {object|null} frameInfo loadSessionLog 的帧事实
|
|
309
|
-
* @param {Array<{lineNo:number}>} rows 物理行
|
|
310
|
-
* @param {{layout?: boolean}} [opts] `layout=false` ⇒ 只判版本无关的 Z1/Z2(Z4/Z5 跳过)
|
|
311
|
-
* @returns {Array<object>} 判定列表
|
|
312
|
-
*/
|
|
313
|
-
export function frameViolations(frameInfo, rows, { layout = true } = {}) {
|
|
314
|
-
const out = [];
|
|
315
|
-
if (frameInfo?.torn) {
|
|
316
|
-
out.push(violation('Z1', { lineNo: null }, `zstd 尾帧撕裂:可能是写入中的 in-flight 帧或文件被截断(帧数 ${frameInfo.frames})`));
|
|
317
|
-
}
|
|
318
|
-
if (frameInfo?.error) {
|
|
319
|
-
out.push(violation('Z2', { lineNo: null }, frameInfo.error));
|
|
320
|
-
}
|
|
321
|
-
if (!layout) return out;
|
|
322
|
-
if (frameInfo?.firstFrameOneLine === false) {
|
|
323
|
-
out.push(violation('Z4', { lineNo: null },
|
|
324
|
-
`首帧不是"恰好一行 header"(实际 ${frameInfo.firstFrameLines ?? '?'} 行)——宿主 assertZstdHeaderFrame 必抛;抛点在 listArtifacts 遍历全库 ⇒ 整个客户端不可用`));
|
|
325
|
-
}
|
|
326
|
-
if (Number.isSafeInteger(frameInfo?.frames) && frameInfo.frames === 1 && Array.isArray(rows) && rows.length > 1) {
|
|
327
|
-
out.push(violation('Z5', { lineNo: null },
|
|
328
|
-
`整份日志被压成 1 帧(${rows.length} 行)——官方形状是 header 独占第一帧 + 事件逐帧追加;不要这样写回`));
|
|
329
|
-
}
|
|
330
|
-
return out;
|
|
331
|
-
}
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-log-contract · adapters/dsh/host-probes.js
|
|
3
|
-
*
|
|
4
|
-
* DSH 专属:宿主行为探针(把宿主运行时函数当 oracle,检测判定前提漂移)。
|
|
5
|
-
*
|
|
6
|
-
* 探针只做只读调用(不写盘、不联网、不改文件);异常收敛为"探针失败/跳过"。
|
|
7
|
-
*/
|
|
8
|
-
import {
|
|
9
|
-
adoptSessionEvent, foldSurface, isSurfaceEligibleType, KNOWN_SESSION_EVENT_TYPES, SESSION_FORMAT_VERSION,
|
|
10
|
-
} from '@deepseek-ai/dsh-session';
|
|
11
|
-
import { hostPackageVersion } from './format-versions.js';
|
|
12
|
-
import { SOURCE_DRIFT } from './rules.js';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 探针定义。每条:
|
|
16
|
-
* `rules` = 该探针**背书**的规则 id(不一致时这些规则被标 UNVERIFIED);
|
|
17
|
-
* `expect` = 我们对宿主行为的期望(同时就是规则的假设);
|
|
18
|
-
* `run` = 用宿主真代码判定实际行为(返回 true=与期望一致)。
|
|
19
|
-
* 说明:断言只看"宿主收/拒"这一层语义(可用 `adoptSessionEvent` 与 `foldSurface` 观察),
|
|
20
|
-
* 不复制本包任何判定逻辑——否则又变成"自己给自己打分"。
|
|
21
|
-
*/
|
|
22
|
-
export const PROBES = [
|
|
23
|
-
{
|
|
24
|
-
id: 'p1-replace-v3-fields',
|
|
25
|
-
title: 'v3 replace 只认 {op,startSeq,endSeq}(旧 {start,end} 必须拒)',
|
|
26
|
-
rules: ['S1', 'S4'],
|
|
27
|
-
expect: 'v3 形状通过、rc.7 旧形状被拒',
|
|
28
|
-
run: () => {
|
|
29
|
-
const mk = (op) => ({ type: 'user/message', seq: 1, time: 1, surfaceOp: op, sourceEventSeqs: [0], data: { id: 'u1', role: 'user', source: { kind: 'user' }, content: [] } });
|
|
30
|
-
const base = [{ type: 'user/message', seq: 0, time: 0, surfaceOp: 'append', data: { id: 'u0', role: 'user', source: { kind: 'user' }, content: [] } }];
|
|
31
|
-
const modern = rejects(() => foldSurface([...base, mk({ op: 'replace', startSeq: 0, endSeq: 0 })]));
|
|
32
|
-
const legacy = rejects(() => foldSurface([...base, mk({ op: 'replace', start: 0, end: 0 })]));
|
|
33
|
-
return modern === false && legacy === true;
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
id: 'p2-assistant-message-provenance',
|
|
38
|
-
title: 'assistant/message 不得携带 sourceEventSeqs(v3 起)',
|
|
39
|
-
rules: ['S6', 'E4', 'S8'],
|
|
40
|
-
expect: '带 provenance 的 assistant/message 被拒',
|
|
41
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
42
|
-
type: 'assistant/message', seq: 1, time: 1, surfaceOp: 'append', sourceEventSeqs: [0],
|
|
43
|
-
data: { turn: 0, step: 1, message: { id: 'a1', role: 'assistant', source: { kind: 'model', provider: 'p', model: 'm' }, content: [] } },
|
|
44
|
-
})) === true,
|
|
45
|
-
},
|
|
46
|
-
{
|
|
47
|
-
id: 'p3-system-message-source',
|
|
48
|
-
title: 'system/message 是 surface 类型;source 按宿主格式:v3 plugin / v4 system-prompt(kind=user 必须拒)',
|
|
49
|
-
rules: ['E6', 'E9'],
|
|
50
|
-
expect: 'v3 宿主:plugin 通过、system-prompt 被拒;v4 宿主:system-prompt 通过、plugin 被拒;user source 一律被拒',
|
|
51
|
-
run: () => {
|
|
52
|
-
const eligible = isSurfaceEligibleType('system/message') === true;
|
|
53
|
-
const sys = (source) => rejects(() => adoptSessionEvent({
|
|
54
|
-
type: 'system/message', seq: 0, time: 1, surfaceOp: 'append',
|
|
55
|
-
data: { turn: 1, step: 1, message: { id: 's0', role: 'system', source, content: [] } },
|
|
56
|
-
}));
|
|
57
|
-
const withPlugin = sys({ kind: 'plugin', plugin: 'p' });
|
|
58
|
-
const withSystemPrompt = sys({ kind: 'system-prompt' });
|
|
59
|
-
const withUser = sys({ kind: 'user' });
|
|
60
|
-
const expected = SESSION_FORMAT_VERSION >= 4
|
|
61
|
-
? withSystemPrompt === false && withPlugin === true
|
|
62
|
-
: withPlugin === false;
|
|
63
|
-
return eligible && expected && withUser === true;
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
id: 'p7-tool-result-role',
|
|
68
|
-
title: 'tool/result 的 role 按宿主格式:v3 user + tool-result 包装块 / v4 tool + message.toolCallId',
|
|
69
|
-
rules: ['E6'],
|
|
70
|
-
expect: 'v3 宿主:包装形状通过、v4 形状被拒;v4 宿主:v4 形状通过、包装形状与 toolCallId 不匹配被拒',
|
|
71
|
-
run: () => {
|
|
72
|
-
const tool = (message) => rejects(() => adoptSessionEvent({
|
|
73
|
-
type: 'tool/result', seq: 2, time: 1, surfaceOp: 'append', data: { turn: 1, step: 1, message },
|
|
74
|
-
}));
|
|
75
|
-
const v4Shape = tool({ id: 't1', role: 'tool', toolCallId: 'c1', source: { kind: 'tool', callId: 'c1' }, content: [] });
|
|
76
|
-
const v4Mismatch = tool({ id: 't1', role: 'tool', toolCallId: 'c2', source: { kind: 'tool', callId: 'c1' }, content: [] });
|
|
77
|
-
const v3Shape = tool({ id: 't1', role: 'user', source: { kind: 'tool', callId: 'c1' }, content: [{ type: 'tool-result', toolCallId: 'c1', content: [] }] });
|
|
78
|
-
return SESSION_FORMAT_VERSION >= 4
|
|
79
|
-
? v4Shape === false && v4Mismatch === true && v3Shape === true
|
|
80
|
-
: v3Shape === false && v4Shape === true;
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
id: 'p8-developer-message-surface',
|
|
85
|
-
title: 'developer/message 只在 v4 宿主是 surface 类型(role developer)',
|
|
86
|
-
rules: ['E6', 'S1', 'S2'],
|
|
87
|
-
expect: 'v4 宿主:surface-eligible 且 role developer 通过;v3 宿主:非 surface 类型',
|
|
88
|
-
run: () => {
|
|
89
|
-
const eligible = isSurfaceEligibleType('developer/message') === true;
|
|
90
|
-
if (SESSION_FORMAT_VERSION < 4) return eligible === false;
|
|
91
|
-
const accepted = rejects(() => adoptSessionEvent({
|
|
92
|
-
type: 'developer/message', seq: 3, time: 1, surfaceOp: 'append',
|
|
93
|
-
data: { turn: 1, step: 1, message: { id: 'd1', role: 'developer', source: { kind: 'tool-registry' }, content: [] } },
|
|
94
|
-
})) === false;
|
|
95
|
-
return eligible && accepted;
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
{
|
|
99
|
-
id: 'p4-unknown-ignorable-opaque-metadata',
|
|
100
|
-
title: '未知 + ignorable 的 opaque 元数据被刻意容忍(S2 不得报 error)',
|
|
101
|
-
rules: ['S2'],
|
|
102
|
-
expect: '未知 ignorable + surfaceOp 被接受',
|
|
103
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
104
|
-
type: 'future/thing', seq: 0, time: 1, ignorable: true, surfaceOp: 'append', data: { k: 1 },
|
|
105
|
-
})) === false,
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
id: 'p5-envelope-extra-keys-load-tolerant',
|
|
109
|
-
title: '信封多余键:load 路径(adoptSessionEvent)**容忍**,只在 seed 路径拒(E8 的语义)',
|
|
110
|
-
rules: ['E8'],
|
|
111
|
-
expect: 'adoptSessionEvent 不拒(宿主 load 路径容忍)',
|
|
112
|
-
// 这条探针**修正了一条规则的过度声称**:
|
|
113
|
-
// · 宿主 `assertSessionEventEnvelope`(dsh-session@0.1.5-rc.1 lib/index.js:849-861)确实拒多余键,
|
|
114
|
-
// 但它的**唯一调用点**是 Session 构造器的 **seed 路径**(:1063-1068)——不是 JSONL load 路径;
|
|
115
|
-
// · load 路径(`adoptSessionEvent`)**容忍**多余键。
|
|
116
|
-
// ⇒ E8 从 error 降为 **warning**、语义限定为"seed/restore 会拒"。
|
|
117
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
118
|
-
type: 'user/message', seq: 0, time: 1, surfaceOp: 'append', extra: 1,
|
|
119
|
-
data: { id: 'u0', role: 'user', source: { kind: 'user' }, content: [] },
|
|
120
|
-
})) === false,
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
id: 'p6-request-header-system',
|
|
124
|
-
title: 'request/header 必须省略 header.system(E10)',
|
|
125
|
-
rules: ['E10'],
|
|
126
|
-
expect: '带 header.system 的 request/header 被拒',
|
|
127
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
128
|
-
type: 'request/header', seq: 0, time: 1, surfaceOp: 'append', data: { header: { system: 'x' } },
|
|
129
|
-
})) === true,
|
|
130
|
-
},
|
|
131
|
-
];
|
|
132
|
-
|
|
133
|
-
/** 宿主是否 **拒** 这次调用(true = 抛错/拒绝)。 */
|
|
134
|
-
|
|
135
|
-
function rejects(fn) {
|
|
136
|
-
try {
|
|
137
|
-
fn();
|
|
138
|
-
return false;
|
|
139
|
-
} catch {
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 跑全部探针。
|
|
146
|
-
* @returns {{hostPackage:string, sessionFormatVersion:number, knownTypes:number,
|
|
147
|
-
* verified:boolean, probes:Array<{id,title,rules,expect,ok,error?}>, unverifiedRules:string[]}}
|
|
148
|
-
*/
|
|
149
|
-
export function runHostProbes(probes = PROBES) {
|
|
150
|
-
const results = [];
|
|
151
|
-
const unverified = new Set();
|
|
152
|
-
for (const probe of probes) {
|
|
153
|
-
let ok = false;
|
|
154
|
-
let error;
|
|
155
|
-
try {
|
|
156
|
-
ok = probe.run() === true;
|
|
157
|
-
} catch (err) {
|
|
158
|
-
ok = false;
|
|
159
|
-
error = String(err?.message ?? err).slice(0, 200);
|
|
160
|
-
}
|
|
161
|
-
results.push({ id: probe.id, title: probe.title, rules: probe.rules, expect: probe.expect, ok, ...(error ? { error } : {}) });
|
|
162
|
-
if (!ok) for (const r of probe.rules) unverified.add(r);
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
hostPackage: hostPackageVersion(),
|
|
166
|
-
sessionFormatVersion: SESSION_FORMAT_VERSION,
|
|
167
|
-
knownTypes: KNOWN_SESSION_EVENT_TYPES.size,
|
|
168
|
-
verified: unverified.size === 0,
|
|
169
|
-
probes: results,
|
|
170
|
-
unverifiedRules: [...unverified].sort(),
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
/** 行为探针只跑一次(宿主在同一进程内不变)。 */
|
|
176
|
-
let probesMemo;
|
|
177
|
-
export function hostProbes() {
|
|
178
|
-
if (probesMemo === undefined) {
|
|
179
|
-
try {
|
|
180
|
-
probesMemo = runHostProbes();
|
|
181
|
-
} catch (err) {
|
|
182
|
-
probesMemo = { hostPackage: 'unknown', sessionFormatVersion: null, knownTypes: 0, verified: false, probes: [], unverifiedRules: ['<probe-run-failed>'], error: String(err?.message ?? err) };
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return probesMemo;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* 合并出的**漂移报告**(`check` 抬头与 `--json` 共用)。
|
|
190
|
-
* · `unverifiedRules` —— 行为探针在本宿主上**不成立**的规则(宿主语义与规则假设不符);
|
|
191
|
-
* · `driftedSources` —— 出处停在 rc.7、未在 0.1.5 上核对(机读清单);
|
|
192
|
-
* · `premiseStale` —— 判定前提已不成立/无法判定;
|
|
193
|
-
* · `fixApplyBlocked` —— 只要任一非空,就**不得据此跑 `fix --apply`**。
|
|
194
|
-
*/
|
|
195
|
-
export function driftOf(probes) {
|
|
196
|
-
const p = probes ?? hostProbes();
|
|
197
|
-
const unverifiedRules = [...new Set(p.unverifiedRules ?? [])];
|
|
198
|
-
const driftedSources = [...SOURCE_DRIFT.drifted];
|
|
199
|
-
const premiseStale = [...SOURCE_DRIFT.premiseStale];
|
|
200
|
-
return {
|
|
201
|
-
probeVerified: p.verified === true,
|
|
202
|
-
unverifiedRules,
|
|
203
|
-
driftedSources,
|
|
204
|
-
premiseStale,
|
|
205
|
-
undecidable: [...SOURCE_DRIFT.undecidable],
|
|
206
|
-
// `fix --apply` 的**硬前提**只看行为探针(宿主语义与规则假设不一致时才禁止写入类动作);
|
|
207
|
-
// 出处/前提清单是**已知文档债**,会显式告警但不阻断修复流程 —— 否则只要历史
|
|
208
|
-
// 清单非空,"修一个坏会话"这条主路径就永久不可用(见报告"我未照做之处")。
|
|
209
|
-
fixApplyBlocked: unverifiedRules.length > 0,
|
|
210
|
-
note: SOURCE_DRIFT.note,
|
|
211
|
-
};
|
|
212
|
-
}
|
package/adapters/dsh/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-log-contract · adapters/dsh/index.js
|
|
3
|
-
*
|
|
4
|
-
* DSH 适配器公开面(DeepSeek Harness session log)。
|
|
5
|
-
*
|
|
6
|
-
* 适配器职责:把 DSH 记录形状翻译成契约层的不变量判定输入,并收口所有
|
|
7
|
-
* DSH 专属词汇(事件类型 turn/step、surface 投影、格式版本 v0–v4、zstd 帧、
|
|
8
|
-
* projcache、词表、归档指针)。
|
|
9
|
-
*
|
|
10
|
-
* 依赖方向:adapters/dsh → contract(单向)。跨平台适配器之间互不依赖;
|
|
11
|
-
* 同一适配器内的子模块可共享 `vocab.js`(词表与形状常量)与 `rules.js`(规则数据)。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export const ADAPTER = Object.freeze({
|
|
15
|
-
id: 'dsh',
|
|
16
|
-
name: 'DeepSeek Harness session log',
|
|
17
|
-
eventShapes: 'event-shapes.js',
|
|
18
|
-
formatVersions: 'format-versions.js',
|
|
19
|
-
frames: 'frames.js',
|
|
20
|
-
surface: 'surface.js',
|
|
21
|
-
vocab: 'vocab.js',
|
|
22
|
-
rules: 'rules.js',
|
|
23
|
-
archive: 'archive.js',
|
|
24
|
-
hostProbes: 'host-probes.js',
|
|
25
|
-
projcache: 'surface.js', // 客户端投影缓存:只登记归属,不做落盘判定
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
export * from './event-shapes.js';
|
|
29
|
-
export * from './format-versions.js';
|
|
30
|
-
export * from './frames.js';
|
|
31
|
-
export * from './surface.js';
|
|
32
|
-
export * from './vocab.js';
|
|
33
|
-
export * from './rules.js';
|
|
34
|
-
export * from './archive.js';
|
|
35
|
-
export * from './host-probes.js';
|