dsh-log-contract 0.3.28 → 0.3.33
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 +48 -38
- package/README.zh.md +38 -38
- package/bin/dsh-log-contract.mjs +67 -15
- package/docs/CONTRACTS.md +74 -60
- package/docs/interface-versioning.md +46 -0
- package/lib/archive.js +21 -21
- package/lib/checks.js +155 -66
- package/lib/compat.js +7 -7
- package/lib/contracts.js +77 -60
- package/lib/explain.js +129 -0
- package/lib/host-probes.js +4 -4
- package/lib/index.js +1 -0
- package/lib/legacy-fold.js +5 -5
- package/lib/log-reader.js +57 -23
- package/lib/prewrite.js +6 -6
- package/lib/repair.js +34 -34
- package/lib/validate.js +27 -9
- package/lib/version-support.js +4 -4
- package/lib/vocab.js +8 -8
- package/package.json +2 -2
package/lib/archive.js
CHANGED
|
@@ -8,16 +8,16 @@
|
|
|
8
8
|
* 只有在调用方提供归档根目录时才能回答"在位可还原",否则只能回答"不可判定"——
|
|
9
9
|
* **不许**把"没查"冒充成"通过"。
|
|
10
10
|
*
|
|
11
|
-
* 口径(外部评审一手实测,
|
|
11
|
+
* 口径(外部评审一手实测,2026-10-20):
|
|
12
12
|
* - 只认**结构化字段**:`data.archiveRef` / `data.archive` /
|
|
13
13
|
* `data.externalized.archiveRef` / `data.externalized.archive`;
|
|
14
|
-
* 校验元数据取同级 `archiveLine` / `
|
|
14
|
+
* 校验元数据取同级 `archiveLine` / `sha256`。
|
|
15
15
|
* - ⛔ **不得**从自由文本 grep 路径。实测两版文本口径全错:原版给 78.26%
|
|
16
16
|
* (正文模板/示例被算成引用)、改成"只认绝对路径"后 2.04%(正文里提到的真实路径示例
|
|
17
|
-
*
|
|
18
|
-
* 出现在
|
|
17
|
+
* 又虚低)。大规模真实语料真实会话:大量结构化指针 个,自由文本 `archive 第 N 行`
|
|
18
|
+
* 出现在 大量事件里——两种口径差近 20 倍,文本 grep 不可用。
|
|
19
19
|
*
|
|
20
|
-
* 判据:目标文件存在 + `archiveLine` 行存在 + `
|
|
20
|
+
* 判据:目标文件存在 + `archiveLine` 行存在 + `sha256`(若有)匹配 = 在位可还原;
|
|
21
21
|
* 目标缺失/行缺失/sha 不符 = A1 error;绝对路径失效但同名归档在归档根可解析 = relocated
|
|
22
22
|
* warning(re-home 未回写:内容可找回,但**不在位**)。
|
|
23
23
|
*/
|
|
@@ -27,7 +27,7 @@ import crypto from 'node:crypto';
|
|
|
27
27
|
import { violation } from './checks.js';
|
|
28
28
|
import { decompressZstd } from './log-reader.js';
|
|
29
29
|
|
|
30
|
-
const ZSTD_MAGIC =
|
|
30
|
+
const ZSTD_MAGIC = 0xfd2fb528;
|
|
31
31
|
|
|
32
32
|
/** 结构化指针字段(只认这些;`content`/`pointer`/散文一律不碰)。 */
|
|
33
33
|
export const ARCHIVE_POINTER_FIELDS = Object.freeze(['archiveRef', 'archive']);
|
|
@@ -48,7 +48,7 @@ function pushPointer(out, event, lineNo, field, ref, line, sha) {
|
|
|
48
48
|
field,
|
|
49
49
|
ref,
|
|
50
50
|
archiveLine: Number.isSafeInteger(line) ? line : null,
|
|
51
|
-
|
|
51
|
+
sha256: typeof sha === 'string' && sha !== '' ? sha : null,
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ function pushPointer(out, event, lineNo, field, ref, line, sha) {
|
|
|
59
59
|
* (手搓用例/第三方直接传事件数组)。
|
|
60
60
|
*
|
|
61
61
|
* @param {Array<{event:object,lineNo?:number}|object>} events
|
|
62
|
-
* @returns {Array<{seq:number|null,lineNo:number|null,type:string|null,field:string,ref:string,archiveLine:number|null,
|
|
62
|
+
* @returns {Array<{seq:number|null,lineNo:number|null,type:string|null,field:string,ref:string,archiveLine:number|null,sha256:string|null}>}
|
|
63
63
|
*/
|
|
64
64
|
export function collectArchivePointers(events) {
|
|
65
65
|
const out = [];
|
|
@@ -71,13 +71,13 @@ export function collectArchivePointers(events) {
|
|
|
71
71
|
if (!isPlainRecord(data)) continue;
|
|
72
72
|
for (const field of ARCHIVE_POINTER_FIELDS) {
|
|
73
73
|
if (data[field] === undefined) continue;
|
|
74
|
-
pushPointer(out, event, lineNo, `data.${field}`, data[field], data.archiveLine, data.
|
|
74
|
+
pushPointer(out, event, lineNo, `data.${field}`, data[field], data.archiveLine, data.sha256 ?? data.archiveSha256);
|
|
75
75
|
}
|
|
76
76
|
const ext = data.externalized;
|
|
77
77
|
if (isPlainRecord(ext)) {
|
|
78
78
|
for (const field of EXTERNALIZED_POINTER_FIELDS) {
|
|
79
79
|
if (ext[field] === undefined) continue;
|
|
80
|
-
pushPointer(out, event, lineNo, `data.externalized.${field}`, ext[field], ext.archiveLine, ext.
|
|
80
|
+
pushPointer(out, event, lineNo, `data.externalized.${field}`, ext[field], ext.archiveLine, ext.sha256 ?? ext.archiveSha256);
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
}
|
|
@@ -136,7 +136,7 @@ function searchByBasename(dir, base, depth, index) {
|
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
138
|
* 读归档文件的**行索引**(只保留请求的行;巨型归档不能整体 `toString`:
|
|
139
|
-
* >
|
|
139
|
+
* >512MiB 明文会触发 V8 字符串上限——那是器具限制,不是内容缺失,必须避免把它误报成 A1)。
|
|
140
140
|
*
|
|
141
141
|
* @param {string} file 归档路径(.jsonl.zst 走多帧解压;.jsonl 直接 utf8)
|
|
142
142
|
* @param {Set<number>} [wantedLines] 需要的 1-based 行号
|
|
@@ -172,14 +172,14 @@ export function readArchiveLines(file) {
|
|
|
172
172
|
return lines;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
/**
|
|
176
|
-
function
|
|
177
|
-
return crypto.createHash('
|
|
175
|
+
/** sha256(十六进制);匹配时允许 16 位前缀(深 H2 以 line16 为准,sha256 作对照)。 */
|
|
176
|
+
function sha256Hex(text) {
|
|
177
|
+
return crypto.createHash('sha256').update(text).digest('hex');
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
function shaMatches(line, want) {
|
|
181
181
|
if (typeof want !== 'string' || want.length < 16) return true; // 无/过短指纹 ⇒ 不做深校验(不猜)
|
|
182
|
-
const got =
|
|
182
|
+
const got = sha256Hex(line);
|
|
183
183
|
const w = want.toLowerCase();
|
|
184
184
|
if (w.length >= 64) return got === w.slice(0, 64);
|
|
185
185
|
return got.startsWith(w) || w.startsWith(got.slice(0, w.length));
|
|
@@ -235,14 +235,14 @@ export function verifyArchivePointers(pointers, { archiveRoots = [], readLines }
|
|
|
235
235
|
if (result.details.unresolved.length < 20) result.details.unresolved.push({ seq: pointer.seq, field: pointer.field, ref: pointer.ref });
|
|
236
236
|
continue;
|
|
237
237
|
}
|
|
238
|
-
const dedupeKey = `${target}\
|
|
238
|
+
const dedupeKey = `${target}\u0000${pointer.archiveLine ?? ''}`;
|
|
239
239
|
if (seen.has(dedupeKey)) { result.duplicated++; continue; }
|
|
240
240
|
seen.add(dedupeKey);
|
|
241
241
|
unique.push({ pointer, target, relocated });
|
|
242
242
|
}
|
|
243
243
|
result.effectivePointers = unique.length;
|
|
244
244
|
// ② 按目标分组,一次扫描取回需要的行(巨型归档不能整体 toString:
|
|
245
|
-
// >
|
|
245
|
+
// >512MiB 明文会触发 V8 字符串上限,那是器具限制、不是内容缺失)。
|
|
246
246
|
const byTarget = new Map();
|
|
247
247
|
for (const item of unique) {
|
|
248
248
|
if (!byTarget.has(item.target)) byTarget.set(item.target, []);
|
|
@@ -255,7 +255,7 @@ export function verifyArchivePointers(pointers, { archiveRoots = [], readLines }
|
|
|
255
255
|
archiveIndex = (readLines ?? readArchiveIndex)(target, wanted);
|
|
256
256
|
if (Array.isArray(archiveIndex)) archiveIndex = { count: archiveIndex.length, lineAt: (n) => archiveIndex[n - 1] ?? '' };
|
|
257
257
|
} catch (err) {
|
|
258
|
-
const message = String(err?.message ?? err).slice(0,
|
|
258
|
+
const message = String(err?.message ?? err).slice(0, 120);
|
|
259
259
|
for (const { pointer } of items) {
|
|
260
260
|
result.missing++;
|
|
261
261
|
if (result.details.missing.length < 20) result.details.missing.push({ seq: pointer.seq, field: pointer.field, ref: pointer.ref, target, reason: message });
|
|
@@ -273,10 +273,10 @@ export function verifyArchivePointers(pointers, { archiveRoots = [], readLines }
|
|
|
273
273
|
continue;
|
|
274
274
|
}
|
|
275
275
|
const line = archiveIndex.lineAt(lineNo);
|
|
276
|
-
if (!shaMatches(line, pointer.
|
|
276
|
+
if (!shaMatches(line, pointer.sha256)) {
|
|
277
277
|
result.shaMismatch++;
|
|
278
|
-
if (result.details.shaMismatch.length < 20) result.details.shaMismatch.push({ seq: pointer.seq, field: pointer.field, ref: pointer.ref, target, archiveLine: lineNo, want: pointer.
|
|
279
|
-
result.violations.push(violation('A1', loc, `归档内容指纹不符:${pointer.field} → ${pointer.ref} 第 ${lineNo} 行
|
|
278
|
+
if (result.details.shaMismatch.length < 20) result.details.shaMismatch.push({ seq: pointer.seq, field: pointer.field, ref: pointer.ref, target, archiveLine: lineNo, want: pointer.sha256?.slice(0, 16), got: sha256Hex(line).slice(0, 16) });
|
|
279
|
+
result.violations.push(violation('A1', loc, `归档内容指纹不符:${pointer.field} → ${pointer.ref} 第 ${lineNo} 行 sha256 期望 ${pointer.sha256?.slice(0, 16)}…,实际 ${sha256Hex(line).slice(0, 16)}…——归档已漂移,不可在位还原`));
|
|
280
280
|
continue;
|
|
281
281
|
}
|
|
282
282
|
if (relocated) {
|