dsh-log-contract 0.3.22 → 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 +84 -97
- package/README.zh.md +69 -74
- 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/lib/compat.js
CHANGED
|
@@ -1,11 +1,282 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-log-contract · lib/compat.js
|
|
2
|
+
* dsh-log-contract · lib/compat.js —— 兼容层(任务 1.3 · 装即坏修复;2026-09-14 语义对齐重写)
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 背景:本包原先从 `@deepseek-ai/dsh-session` 直接导入 `decodeStorageRecord` 与 `isJsonValue`。
|
|
5
|
+
* 官方 0.1.5 树已**不再从包根导出**这两个符号(0.1.5-rc.1 `lib/index.js` 的 26 个导出里没有它们),
|
|
6
|
+
* 而本包是 plugin 的依赖 ⇒ 升级后"装即坏"(模块加载期报 does not provide an export named ...)。
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* 本文件把这两个符号**本地化**,并把语义从"近似"改为**逐条移植**(原实现是宽松近似:
|
|
9
|
+
* `decodeStorageRecord` 对损坏 chunk 行返回 `[value]` 而非抛错,导致 R2 永不触发;
|
|
10
|
+
* `isJsonValue` 不拒 `-0`、不拒稀疏数组、不查原型——与官方 lossless-JSON 边界不一致)。
|
|
11
|
+
*
|
|
12
|
+
* 移植源(一手,2026-09-14 实测):
|
|
13
|
+
* - `decodeStorageRecord` / `validateRow` / `expandRow`:
|
|
14
|
+
* `@deepseek-ai/dsh-session@0.1.0-rc.7` `lib/index.js:922-1035`(该版本是最后一个把
|
|
15
|
+
* `decodeStorageRecord` 从包根导出的版本;与 App 2.0.9 内置的
|
|
16
|
+
* `dsh-session/lib/types/chunk-rows.js`(= 0.1.2-rc.1,sha256 前 16 位 `5724c4f798ed07e7`)
|
|
17
|
+
* 校验规则一致)。**fail-loud**:损坏的 chunk 行必须抛错——它是损坏存储,静默当普通事件
|
|
18
|
+
* 处理会丢掉整段 run。
|
|
19
|
+
* - `isJsonValue`:`@deepseek-ai/dsh-session@0.1.0-rc.7` `lib/index.js:74-205`
|
|
20
|
+
* `walkJsonValue(value, false)`(lossless-JSON 边界:拒绝稀疏数组、非有限数、`-0`、
|
|
21
|
+
* 循环引用、非普通原型、symbol/不可枚举键)。
|
|
22
|
+
*
|
|
23
|
+
* 仍从官方导入的符号(升级后保留):`foldSurface` / `isSurfaceEligibleType` /
|
|
24
|
+
* `KNOWN_SESSION_EVENT_TYPES`(见 `./vocab.js` / `./checks.js`)。
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/** Whether a value is a JSON-visible record (object, not null, not array)(rc.7 `isRecord`)。 */
|
|
28
|
+
function isRecord(value) {
|
|
29
|
+
return typeof value === 'object' && value !== null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Exact-key check: `value` has every key in `keys` and nothing else(rc.7 `hasExactKeys`)。 */
|
|
33
|
+
function hasExactKeys(value, keys) {
|
|
34
|
+
return Object.keys(value).length === keys.length && keys.every((k) => Object.hasOwn(value, k));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//#region isJsonValue —— rc.7 walkJsonValue 的移植(不含 detach)
|
|
38
|
+
/** Whether a value is an object whose prototype is an intrinsic (plain) object prototype, across realms. */
|
|
39
|
+
function isIntrinsicObjectPrototype(prototype) {
|
|
40
|
+
return prototype === null || Object.getPrototypeOf(prototype) === null;
|
|
41
|
+
}
|
|
42
|
+
/** Whether an object is a plain or null-prototype record from any JavaScript realm. */
|
|
43
|
+
function hasPlainObjectPrototype(value) {
|
|
44
|
+
const prototype = Object.getPrototypeOf(value);
|
|
45
|
+
return prototype === null || (typeof prototype === 'object' && isIntrinsicObjectPrototype(prototype));
|
|
46
|
+
}
|
|
47
|
+
/** Whether an array is a plain array from any JavaScript realm (rejects subclasses/exotics). */
|
|
48
|
+
function hasPlainArrayPrototype(value) {
|
|
49
|
+
const prototype = Object.getPrototypeOf(value);
|
|
50
|
+
if (prototype === Array.prototype) return true;
|
|
51
|
+
return typeof prototype === 'object' && prototype !== null && Object.getPrototypeOf(prototype) === Object.prototype && Object.getPrototypeOf(Object.getPrototypeOf(prototype)) === null;
|
|
52
|
+
}
|
|
53
|
+
/** Return every JSON-visible object key, or reject own data JSON would discard. */
|
|
54
|
+
function enumerableStringKeys(value) {
|
|
55
|
+
const keys = Reflect.ownKeys(value);
|
|
56
|
+
if (keys.some((key) => typeof key !== 'string' || !Object.prototype.propertyIsEnumerable.call(value, key))) return undefined;
|
|
57
|
+
return keys;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Validate lossless JSON iteratively(rc.7 `walkJsonValue(value, false)`)。
|
|
62
|
+
* @param {unknown} value
|
|
63
|
+
* @returns {boolean}
|
|
64
|
+
*/
|
|
65
|
+
export function isJsonValue(value) {
|
|
66
|
+
const ancestors = new Set();
|
|
67
|
+
const tasks = [{ kind: 'visit', value }];
|
|
68
|
+
for (let task = tasks.pop(); task !== undefined; task = tasks.pop()) {
|
|
69
|
+
if (task.kind === 'leave') {
|
|
70
|
+
ancestors.delete(task.source);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (task.kind === 'array-item') {
|
|
74
|
+
if (!Object.prototype.hasOwnProperty.call(task.source, task.index)) return false;
|
|
75
|
+
tasks.push({ kind: 'visit', value: task.source[task.index] });
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (task.kind === 'object-property') {
|
|
79
|
+
tasks.push({ kind: 'visit', value: task.source[task.key] });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const current = task.value;
|
|
83
|
+
if (current === null) continue;
|
|
84
|
+
if (typeof current === 'boolean' || typeof current === 'string') continue;
|
|
85
|
+
if (typeof current === 'number') {
|
|
86
|
+
if (!Number.isFinite(current) || Object.is(current, -0)) return false;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (typeof current !== 'object') return false;
|
|
90
|
+
if (ancestors.has(current)) return false;
|
|
91
|
+
if (Array.isArray(current)) {
|
|
92
|
+
if (!hasPlainArrayPrototype(current)) return false;
|
|
93
|
+
const length = current.length;
|
|
94
|
+
if (Reflect.ownKeys(current).length !== length + 1) return false;
|
|
95
|
+
ancestors.add(current);
|
|
96
|
+
tasks.push({ kind: 'leave', source: current });
|
|
97
|
+
for (let index = length - 1; index >= 0; index--) tasks.push({ kind: 'array-item', source: current, index });
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (!hasPlainObjectPrototype(current)) return false;
|
|
101
|
+
const keys = enumerableStringKeys(current);
|
|
102
|
+
if (keys === undefined) return false;
|
|
103
|
+
ancestors.add(current);
|
|
104
|
+
tasks.push({ kind: 'leave', source: current });
|
|
105
|
+
for (let index = keys.length - 1; index >= 0; index--) {
|
|
106
|
+
const key = keys[index];
|
|
107
|
+
/* v8 ignore next -- the loop is bounded by the captured key count. */
|
|
108
|
+
if (key === undefined) return false;
|
|
109
|
+
tasks.push({ kind: 'object-property', source: current, key });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
|
|
116
|
+
//#region decodeStorageRecord —— rc.7 validateRow/expandRow 的移植
|
|
117
|
+
/** Throw the uniform malformed-row diagnostic(rc.7 `malformed`)。 */
|
|
118
|
+
function malformed(tag, why) {
|
|
119
|
+
throw new Error(`malformed ${tag} storage row: ${why}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Validate the shared run-data fields and the payload/dt arity; returns the member payload(rc.7 `validateRunData`)。 */
|
|
123
|
+
function validateRunData(tag, data, payloadKey) {
|
|
124
|
+
if (typeof data.turn !== 'number' || typeof data.step !== 'number' || typeof data.index !== 'number') malformed(tag, 'turn/step/index must be numbers');
|
|
125
|
+
const payload = data[payloadKey];
|
|
126
|
+
if (!Array.isArray(payload) || payload.length === 0 || payload.some((entry) => typeof entry !== 'string')) malformed(tag, `${payloadKey} must be a non-empty string array`);
|
|
127
|
+
const dt = data.dt;
|
|
128
|
+
if (!Array.isArray(dt) || dt.some((gap) => !Number.isSafeInteger(gap))) malformed(tag, 'dt must be an array of safe integers');
|
|
129
|
+
if (dt.length !== payload.length - 1) malformed(tag, `dt length ${dt.length} does not match ${payload.length} members`);
|
|
130
|
+
return payload;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Validate a row-tagged parsed value's envelope and data, throwing on any malformation(rc.7 `validateRow`)。 */
|
|
134
|
+
function validateRow(value, tag) {
|
|
135
|
+
if (!hasExactKeys(value, ['type', 'seq0', 'time0', 'data'])) malformed(tag, 'envelope must be exactly {type, seq0, time0, data}');
|
|
136
|
+
if (!Number.isSafeInteger(value.seq0) || value.seq0 < 0) malformed(tag, 'seq0 must be a non-negative safe integer');
|
|
137
|
+
if (!Number.isSafeInteger(value.time0)) malformed(tag, 'time0 must be a safe integer');
|
|
138
|
+
const data = value.data;
|
|
139
|
+
if (!isRecord(data)) malformed(tag, 'data must be an object');
|
|
140
|
+
let payload;
|
|
141
|
+
if (tag === 'tool-call-chunks') {
|
|
142
|
+
const withName = hasExactKeys(data, ['turn', 'step', 'index', 'id', 'name', 'dt', 'args']);
|
|
143
|
+
if (!withName && !hasExactKeys(data, ['turn', 'step', 'index', 'id', 'dt', 'args'])) malformed(tag, 'data must be exactly {turn, step, index, id, name?, dt, args}');
|
|
144
|
+
if (typeof data.id !== 'string' || (withName && typeof data.name !== 'string')) malformed(tag, 'id (and name when present) must be strings');
|
|
145
|
+
payload = validateRunData(tag, data, 'args');
|
|
146
|
+
} else {
|
|
147
|
+
if (!hasExactKeys(data, ['turn', 'step', 'index', 'dt', 'texts'])) malformed(tag, 'data must be exactly {turn, step, index, dt, texts}');
|
|
148
|
+
payload = validateRunData(tag, data, 'texts');
|
|
149
|
+
}
|
|
150
|
+
if (!Number.isSafeInteger(value.seq0 + payload.length - 1)) malformed(tag, 'member seqs must stay safe integers');
|
|
151
|
+
let time = value.time0;
|
|
152
|
+
for (const gap of data.dt) {
|
|
153
|
+
time += gap;
|
|
154
|
+
if (!Number.isSafeInteger(time)) malformed(tag, 'member times must stay safe integers');
|
|
155
|
+
}
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Expand a validated row back into its exact original events, in order(rc.7 `expandRow`)。 */
|
|
160
|
+
function expandRow(row) {
|
|
161
|
+
const members = row.type === 'tool-call-chunks' ? row.data.args : row.data.texts;
|
|
162
|
+
const events = [];
|
|
163
|
+
let time = row.time0;
|
|
164
|
+
for (let k = 0; k < members.length; k++) {
|
|
165
|
+
if (k > 0) time += row.data.dt[k - 1];
|
|
166
|
+
let chunk;
|
|
167
|
+
switch (row.type) {
|
|
168
|
+
case 'text-chunks':
|
|
169
|
+
chunk = { type: 'text-delta', index: row.data.index, text: members[k] };
|
|
170
|
+
break;
|
|
171
|
+
case 'reasoning-chunks':
|
|
172
|
+
chunk = { type: 'reasoning-delta', index: row.data.index, text: members[k] };
|
|
173
|
+
break;
|
|
174
|
+
case 'tool-call-chunks':
|
|
175
|
+
chunk = {
|
|
176
|
+
type: 'tool-call-delta',
|
|
177
|
+
index: row.data.index,
|
|
178
|
+
id: row.data.id,
|
|
179
|
+
...Object.hasOwn(row.data, 'name') ? { name: row.data.name } : {},
|
|
180
|
+
argumentsDelta: members[k],
|
|
181
|
+
};
|
|
182
|
+
break;
|
|
183
|
+
/* v8 ignore next 4 -- validateRow only returns the three row tags */
|
|
184
|
+
default:
|
|
185
|
+
throw new Error(`chunk-rows received unsupported row ${String(row)}`);
|
|
186
|
+
}
|
|
187
|
+
events.push({ type: 'assistant/chunk', seq: row.seq0 + k, time, data: { turn: row.data.turn, step: row.data.step, chunk } });
|
|
188
|
+
}
|
|
189
|
+
return events;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 存储行解码:chunk 行校验后展开为完整事件序列,其他行原样返回(rc.7 `decodeStorageRecord`)。
|
|
194
|
+
* 损坏的 chunk 行**抛错**(fail-loud)——调用方(log-reader)据此落 `row.error` → R2。
|
|
195
|
+
* @param {unknown} value 已 JSON.parse 的一行
|
|
196
|
+
* @returns {unknown[]} 事件数组
|
|
197
|
+
*/
|
|
198
|
+
export function decodeStorageRecord(value) {
|
|
199
|
+
if (!isRecord(value)) return [value];
|
|
200
|
+
const tag = value.type;
|
|
201
|
+
if (tag !== 'text-chunks' && tag !== 'reasoning-chunks' && tag !== 'tool-call-chunks') return [value];
|
|
202
|
+
return expandRow(validateRow(value, tag));
|
|
203
|
+
}
|
|
204
|
+
//#endregion
|
|
205
|
+
|
|
206
|
+
//#region decodeSeqRanges —— v3 storage-form 区间编码解码(0.1.5 `lib/types/seq-ranges.js` 逐条移植)
|
|
207
|
+
// 为什么本地化:`decodeSeqRanges` 只在 0.1.5+ 从包根导出;`0.1.0-rc.7` 实测 `undefined`
|
|
208
|
+
// (见 vocab.js 的双宿主声明)。本包 peer 允许两个版本 ⇒ 必须在两种宿主上都能解码。
|
|
209
|
+
//
|
|
210
|
+
// 语义(官方 `seq-ranges.js:34-73`):`sourceEventSeqs` 的 JSON 存储形态是"数字 + 闭区间对"
|
|
211
|
+
// 混合数组,例 `[3, [174,176], 180, …]` ⇒ 内存形态 `[3,174,175,176,180,…]`。
|
|
212
|
+
// v3 持久化层对连续段用区间对压缩(App 2.0.9 真实 v3 会话实测:1 个 `user/message` replace
|
|
213
|
+
// 的 `sourceEventSeqs` 2251 个存储项展开为 2259 个 seq)。**不解码 = 拿物理未展开的序列
|
|
214
|
+
// 喂 S5/S6 与官方 foldSurface ⇒ 真实健康会话被误判 S5/S6/S8 + `--resume` broken。**
|
|
215
|
+
|
|
216
|
+
/** 非负安全整数(rc.7/0.1.5 `assertSeq`)。 */
|
|
217
|
+
function assertSeq(value) {
|
|
218
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new TypeError('sourceEventSeqs must contain non-negative safe integers');
|
|
219
|
+
}
|
|
220
|
+
/** 带 Session 序号 brand 的断言(官方 `SessionSeq`;额外拒绝 `-0`)。 */
|
|
221
|
+
function sessionSeq(value) {
|
|
222
|
+
if (!Number.isSafeInteger(value) || value < 0 || Object.is(value, -0)) {
|
|
223
|
+
throw new TypeError(`SessionSeq must be a non-negative safe integer, got ${String(value)}`);
|
|
224
|
+
}
|
|
225
|
+
return value;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 展开 JSON 存储形态的 `sourceEventSeqs`(官方 `decodeSeqRanges` 逐条移植)。
|
|
230
|
+
* 任一形态非法即抛 `TypeError`(fail-loud:调用方据此报 S6/E4,不静默当稠密序列)。
|
|
231
|
+
* @param {unknown} value 已 JSON.parse 的 `sourceEventSeqs`
|
|
232
|
+
* @param {number} [maxEntries] 该事件允许的最大成员数
|
|
233
|
+
* @returns {number[]} 内存稠密序列
|
|
234
|
+
*/
|
|
235
|
+
export function decodeSeqRanges(value, maxEntries = Number.MAX_SAFE_INTEGER) {
|
|
236
|
+
if (!Array.isArray(value)) throw new TypeError('sourceEventSeqs must be an array');
|
|
237
|
+
const decoded = [];
|
|
238
|
+
let hasRange = false;
|
|
239
|
+
for (const entry of value) {
|
|
240
|
+
if (typeof entry === 'number') {
|
|
241
|
+
assertSeq(entry);
|
|
242
|
+
if (decoded.length >= maxEntries) throw new TypeError('sourceEventSeqs exceeds its event sequence');
|
|
243
|
+
decoded.push(sessionSeq(entry));
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (!Array.isArray(entry) || entry.length !== 2) {
|
|
247
|
+
throw new TypeError('sourceEventSeqs range entries must be [start, end] pairs');
|
|
248
|
+
}
|
|
249
|
+
const start = entry[0];
|
|
250
|
+
const end = entry[1];
|
|
251
|
+
assertSeq(start);
|
|
252
|
+
assertSeq(end);
|
|
253
|
+
if (end < start) throw new TypeError('sourceEventSeqs ranges require start <= end');
|
|
254
|
+
if (end - start + 1 > maxEntries - decoded.length) {
|
|
255
|
+
throw new TypeError('sourceEventSeqs range exceeds its event sequence');
|
|
256
|
+
}
|
|
257
|
+
for (let seq = start; seq <= end; seq += 1) decoded.push(sessionSeq(seq));
|
|
258
|
+
hasRange = true;
|
|
259
|
+
}
|
|
260
|
+
if (hasRange && !decoded.every((v, i) => i === 0 || v > decoded[i - 1])) {
|
|
261
|
+
throw new TypeError('sourceEventSeqs ranges must be strictly increasing');
|
|
262
|
+
}
|
|
263
|
+
return decoded;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 把一个已解码事件的 storage-form `sourceEventSeqs` 归一成内存稠密序列。
|
|
268
|
+
* - 无 `sourceEventSeqs` / 非对象 → 原样返回(同一引用);
|
|
269
|
+
* - 解码成功 → 返回**新对象**(不修改入参;调用方须使用返回值);
|
|
270
|
+
* - 解码失败 → 原样返回,保留存储形态交由 S6/E4 报违规(读取层不崩、不吞错)。
|
|
9
271
|
*/
|
|
10
|
-
export
|
|
11
|
-
|
|
272
|
+
export function normalizeEventSeqRanges(event) {
|
|
273
|
+
if (!isRecord(event) || event.sourceEventSeqs === undefined) return event;
|
|
274
|
+
let expanded;
|
|
275
|
+
try {
|
|
276
|
+
expanded = decodeSeqRanges(event.sourceEventSeqs);
|
|
277
|
+
} catch {
|
|
278
|
+
return event;
|
|
279
|
+
}
|
|
280
|
+
return { ...event, sourceEventSeqs: expanded };
|
|
281
|
+
}
|
|
282
|
+
//#endregion
|