dsh-log-contract 0.3.21 → 0.3.23
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 +114 -79
- package/README.zh.md +89 -65
- package/adapters/dsh/archive.js +135 -0
- package/adapters/dsh/event-shapes.js +639 -0
- package/adapters/dsh/format-versions.js +743 -0
- package/adapters/dsh/frames.js +331 -0
- package/adapters/dsh/host-probes.js +212 -0
- package/adapters/dsh/index.js +35 -0
- package/adapters/dsh/rules.js +560 -0
- package/adapters/dsh/surface.js +792 -0
- package/adapters/dsh/vocab.js +66 -0
- package/bin/dsh-log-contract.mjs +4 -4
- package/contract/index.js +17 -0
- package/contract/invariants.js +478 -0
- package/contract/registry.js +77 -0
- package/contract/rules.js +70 -0
- package/contract/verdict.js +52 -0
- package/docs/ARCHITECTURE.md +46 -0
- package/docs/CONTRACTS.md +34 -34
- package/lib/archive.js +14 -304
- package/lib/checks.js +64 -1332
- package/lib/compat.js +7 -278
- package/lib/contracts.js +8 -522
- package/lib/host-probes.js +4 -178
- package/lib/legacy-fold.js +4 -174
- package/lib/log-reader.js +3 -295
- package/lib/prewrite.js +4 -4
- package/lib/repair.js +11 -11
- package/lib/validate.js +49 -320
- package/lib/version-support.js +4 -146
- package/lib/vocab.js +4 -227
- package/package.json +15 -5
package/lib/host-probes.js
CHANGED
|
@@ -1,181 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-log-contract · lib/host-probes.js
|
|
3
|
-
* (2026-09-14)
|
|
2
|
+
* dsh-log-contract · lib/host-probes.js
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* **只打印不门禁**、规则的 `source` 是**不可机读的字符串** ⇒ 本次 5 类失准**没有任何机制能发现**。
|
|
8
|
-
*
|
|
9
|
-
* 本模块的回答:**不靠人重读源码,靠宿主运行时自身当 oracle**。做法是自造**微型合成日志**
|
|
10
|
-
* 喂给宿主导出的 `foldSurface` / `adoptSessionEvent`(可选 `Session.create`),断言"宿主应当
|
|
11
|
-
* 这么判",再把**本包规则的假设**与宿主实际行为逐条比对:
|
|
12
|
-
* - 一致 → 该规则组 **VERIFIED**(在**当前宿主**上);
|
|
13
|
-
* - 不一致 → 该规则组标 **UNVERIFIED**,结论抬头显式点名,并提示**勿据此跑 `fix --apply`**。
|
|
14
|
-
*
|
|
15
|
-
* 探针只做**只读调用**(不写盘、不联网、不改任何文件);任何异常都被收敛成"探针失败/跳过",
|
|
16
|
-
* 不让漂移检测把工具本身弄崩。
|
|
17
|
-
*/
|
|
18
|
-
import {
|
|
19
|
-
adoptSessionEvent, foldSurface, isSurfaceEligibleType, KNOWN_SESSION_EVENT_TYPES, SESSION_FORMAT_VERSION,
|
|
20
|
-
} from '@deepseek-ai/dsh-session';
|
|
21
|
-
import { hostPackageVersion } from './vocab.js';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 探针定义。每条:
|
|
25
|
-
* `rules` = 该探针**背书**的规则 id(不一致时这些规则被标 UNVERIFIED);
|
|
26
|
-
* `expect` = 我们对宿主行为的期望(同时就是规则的假设);
|
|
27
|
-
* `run` = 用宿主真代码判定实际行为(返回 true=与期望一致)。
|
|
28
|
-
* 说明:断言只看"宿主收/拒"这一层语义(可用 `adoptSessionEvent` 与 `foldSurface` 观察),
|
|
29
|
-
* 不复制本包任何判定逻辑——否则又变成"自己给自己打分"。
|
|
30
|
-
*/
|
|
31
|
-
export const PROBES = [
|
|
32
|
-
{
|
|
33
|
-
id: 'p1-replace-v3-fields',
|
|
34
|
-
title: 'v3 replace 只认 {op,startSeq,endSeq}(旧 {start,end} 必须拒)',
|
|
35
|
-
rules: ['S1', 'S4'],
|
|
36
|
-
expect: 'v3 形状通过、rc.7 旧形状被拒',
|
|
37
|
-
run: () => {
|
|
38
|
-
const mk = (op) => ({ type: 'user/message', seq: 1, time: 1, surfaceOp: op, sourceEventSeqs: [0], data: { id: 'u1', role: 'user', source: { kind: 'user' }, content: [] } });
|
|
39
|
-
const base = [{ type: 'user/message', seq: 0, time: 0, surfaceOp: 'append', data: { id: 'u0', role: 'user', source: { kind: 'user' }, content: [] } }];
|
|
40
|
-
const modern = rejects(() => foldSurface([...base, mk({ op: 'replace', startSeq: 0, endSeq: 0 })]));
|
|
41
|
-
const legacy = rejects(() => foldSurface([...base, mk({ op: 'replace', start: 0, end: 0 })]));
|
|
42
|
-
return modern === false && legacy === true;
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
id: 'p2-assistant-message-provenance',
|
|
47
|
-
title: 'assistant/message 不得携带 sourceEventSeqs(v3 起)',
|
|
48
|
-
rules: ['S6', 'E4', 'S8'],
|
|
49
|
-
expect: '带 provenance 的 assistant/message 被拒',
|
|
50
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
51
|
-
type: 'assistant/message', seq: 1, time: 1, surfaceOp: 'append', sourceEventSeqs: [0],
|
|
52
|
-
data: { turn: 0, step: 1, message: { id: 'a1', role: 'assistant', source: { kind: 'model', provider: 'p', model: 'm' }, content: [] } },
|
|
53
|
-
})) === true,
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
id: 'p3-system-message-source',
|
|
57
|
-
title: 'system/message 是 surface 类型;source 按宿主格式:v3 plugin / v4 system-prompt(kind=user 必须拒)',
|
|
58
|
-
rules: ['E6', 'E9'],
|
|
59
|
-
expect: 'v3 宿主:plugin 通过、system-prompt 被拒;v4 宿主:system-prompt 通过、plugin 被拒;user source 一律被拒',
|
|
60
|
-
run: () => {
|
|
61
|
-
const eligible = isSurfaceEligibleType('system/message') === true;
|
|
62
|
-
const sys = (source) => rejects(() => adoptSessionEvent({
|
|
63
|
-
type: 'system/message', seq: 0, time: 1, surfaceOp: 'append',
|
|
64
|
-
data: { turn: 1, step: 1, message: { id: 's0', role: 'system', source, content: [] } },
|
|
65
|
-
}));
|
|
66
|
-
const withPlugin = sys({ kind: 'plugin', plugin: 'p' });
|
|
67
|
-
const withSystemPrompt = sys({ kind: 'system-prompt' });
|
|
68
|
-
const withUser = sys({ kind: 'user' });
|
|
69
|
-
const expected = SESSION_FORMAT_VERSION >= 4
|
|
70
|
-
? withSystemPrompt === false && withPlugin === true
|
|
71
|
-
: withPlugin === false;
|
|
72
|
-
return eligible && expected && withUser === true;
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
{
|
|
76
|
-
id: 'p7-tool-result-role',
|
|
77
|
-
title: 'tool/result 的 role 按宿主格式:v3 user + tool-result 包装块 / v4 tool + message.toolCallId',
|
|
78
|
-
rules: ['E6'],
|
|
79
|
-
expect: 'v3 宿主:包装形状通过、v4 形状被拒;v4 宿主:v4 形状通过、包装形状与 toolCallId 不匹配被拒',
|
|
80
|
-
run: () => {
|
|
81
|
-
const tool = (message) => rejects(() => adoptSessionEvent({
|
|
82
|
-
type: 'tool/result', seq: 2, time: 1, surfaceOp: 'append', data: { turn: 1, step: 1, message },
|
|
83
|
-
}));
|
|
84
|
-
const v4Shape = tool({ id: 't1', role: 'tool', toolCallId: 'c1', source: { kind: 'tool', callId: 'c1' }, content: [] });
|
|
85
|
-
const v4Mismatch = tool({ id: 't1', role: 'tool', toolCallId: 'c2', source: { kind: 'tool', callId: 'c1' }, content: [] });
|
|
86
|
-
const v3Shape = tool({ id: 't1', role: 'user', source: { kind: 'tool', callId: 'c1' }, content: [{ type: 'tool-result', toolCallId: 'c1', content: [] }] });
|
|
87
|
-
return SESSION_FORMAT_VERSION >= 4
|
|
88
|
-
? v4Shape === false && v4Mismatch === true && v3Shape === true
|
|
89
|
-
: v3Shape === false && v4Shape === true;
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
id: 'p8-developer-message-surface',
|
|
94
|
-
title: 'developer/message 只在 v4 宿主是 surface 类型(role developer)',
|
|
95
|
-
rules: ['E6', 'S1', 'S2'],
|
|
96
|
-
expect: 'v4 宿主:surface-eligible 且 role developer 通过;v3 宿主:非 surface 类型',
|
|
97
|
-
run: () => {
|
|
98
|
-
const eligible = isSurfaceEligibleType('developer/message') === true;
|
|
99
|
-
if (SESSION_FORMAT_VERSION < 4) return eligible === false;
|
|
100
|
-
const accepted = rejects(() => adoptSessionEvent({
|
|
101
|
-
type: 'developer/message', seq: 3, time: 1, surfaceOp: 'append',
|
|
102
|
-
data: { turn: 1, step: 1, message: { id: 'd1', role: 'developer', source: { kind: 'tool-registry' }, content: [] } },
|
|
103
|
-
})) === false;
|
|
104
|
-
return eligible && accepted;
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
{
|
|
108
|
-
id: 'p4-unknown-ignorable-opaque-metadata',
|
|
109
|
-
title: '未知 + ignorable 的 opaque 元数据被刻意容忍(S2 不得报 error)',
|
|
110
|
-
rules: ['S2'],
|
|
111
|
-
expect: '未知 ignorable + surfaceOp 被接受',
|
|
112
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
113
|
-
type: 'future/thing', seq: 0, time: 1, ignorable: true, surfaceOp: 'append', data: { k: 1 },
|
|
114
|
-
})) === false,
|
|
115
|
-
},
|
|
116
|
-
{
|
|
117
|
-
id: 'p5-envelope-extra-keys-load-tolerant',
|
|
118
|
-
title: '信封多余键:load 路径(adoptSessionEvent)**容忍**,只在 seed 路径拒(E8 的口径)',
|
|
119
|
-
rules: ['E8'],
|
|
120
|
-
expect: 'adoptSessionEvent 不拒(宿主 load 路径容忍)',
|
|
121
|
-
// 这条探针**修正了一条规则的过度声称**(探针的价值实证):
|
|
122
|
-
// · 宿主 `assertSessionEventEnvelope`(dsh-session@0.1.5-rc.1 lib/index.js:849-861)确实拒多余键,
|
|
123
|
-
// 但它的**唯一调用点**是 Session 构造器的 **seed 路径**(:1063-1068)——不是 JSONL load 路径;
|
|
124
|
-
// · load 路径(`adoptSessionEvent`)实测**容忍**多余键。
|
|
125
|
-
// ⇒ E8 从 error 降为 **warning**、口径限定为"seed/restore 会拒"(原判"写入即拒"是过度声称)。
|
|
126
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
127
|
-
type: 'user/message', seq: 0, time: 1, surfaceOp: 'append', extra: 1,
|
|
128
|
-
data: { id: 'u0', role: 'user', source: { kind: 'user' }, content: [] },
|
|
129
|
-
})) === false,
|
|
130
|
-
},
|
|
131
|
-
{
|
|
132
|
-
id: 'p6-request-header-system',
|
|
133
|
-
title: 'request/header 必须省略 header.system(E10)',
|
|
134
|
-
rules: ['E10'],
|
|
135
|
-
expect: '带 header.system 的 request/header 被拒',
|
|
136
|
-
run: () => rejects(() => adoptSessionEvent({
|
|
137
|
-
type: 'request/header', seq: 0, time: 1, surfaceOp: 'append', data: { header: { system: 'x' } },
|
|
138
|
-
})) === true,
|
|
139
|
-
},
|
|
140
|
-
];
|
|
141
|
-
|
|
142
|
-
/** 宿主是否 **拒** 这次调用(true = 抛错/拒绝)。 */
|
|
143
|
-
|
|
144
|
-
function rejects(fn) {
|
|
145
|
-
try {
|
|
146
|
-
fn();
|
|
147
|
-
return false;
|
|
148
|
-
} catch {
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* 跑全部探针。
|
|
155
|
-
* @returns {{hostPackage:string, sessionFormatVersion:number, knownTypes:number,
|
|
156
|
-
* verified:boolean, probes:Array<{id,title,rules,expect,ok,error?}>, unverifiedRules:string[]}}
|
|
4
|
+
* 宿主行为探针与漂移报告的公开面(实现层):实现位于 adapters/dsh/host-probes.js。
|
|
5
|
+
* 导出名与既有版本保持一致(公开面不变)。
|
|
157
6
|
*/
|
|
158
|
-
export
|
|
159
|
-
const results = [];
|
|
160
|
-
const unverified = new Set();
|
|
161
|
-
for (const probe of probes) {
|
|
162
|
-
let ok = false;
|
|
163
|
-
let error;
|
|
164
|
-
try {
|
|
165
|
-
ok = probe.run() === true;
|
|
166
|
-
} catch (err) {
|
|
167
|
-
ok = false;
|
|
168
|
-
error = String(err?.message ?? err).slice(0, 200);
|
|
169
|
-
}
|
|
170
|
-
results.push({ id: probe.id, title: probe.title, rules: probe.rules, expect: probe.expect, ok, ...(error ? { error } : {}) });
|
|
171
|
-
if (!ok) for (const r of probe.rules) unverified.add(r);
|
|
172
|
-
}
|
|
173
|
-
return {
|
|
174
|
-
hostPackage: hostPackageVersion(),
|
|
175
|
-
sessionFormatVersion: SESSION_FORMAT_VERSION,
|
|
176
|
-
knownTypes: KNOWN_SESSION_EVENT_TYPES.size,
|
|
177
|
-
verified: unverified.size === 0,
|
|
178
|
-
probes: results,
|
|
179
|
-
unverifiedRules: [...unverified].sort(),
|
|
180
|
-
};
|
|
181
|
-
}
|
|
7
|
+
export { PROBES, runHostProbes, hostProbes, driftOf } from '../adapters/dsh/host-probes.js';
|
package/lib/legacy-fold.js
CHANGED
|
@@ -1,177 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* dsh-log-contract · lib/legacy-fold.js
|
|
2
|
+
* dsh-log-contract · lib/legacy-fold.js
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* 官方 `@deepseek-ai/dsh-session` 的 `foldSurface` 在 0.1.5-rc.1 里换成了 **v3 语义**,
|
|
7
|
-
* 它**不是** rc.7(v0 语义)的超集,而是一个**不同格式**的校验器:
|
|
8
|
-
* - replace 操作数字段改名:rc.7 `{op,start,end}` → 0.1.5 `{op,startSeq,endSeq}`
|
|
9
|
-
* (0.1.5 `lib/index.js:279`:`isReplaceOp` 要求 startSeq/endSeq ⇒ 旧 marker 直接
|
|
10
|
-
* “carries an invalid replace surfaceOp”);
|
|
11
|
-
* - provenance 收紧:0.1.5 `lib/index.js:285` 起对 `assistant/message` **一律**禁止
|
|
12
|
-
* `sourceEventSeqs`(“embeds its source stream and cannot carry sourceEventSeqs”);
|
|
13
|
-
* rc.7 允许(空数组仅限 assistant/message)。
|
|
14
|
-
*
|
|
15
|
-
* App 2.0.9 的真实文件版本分布证明旧格式仍在线上:App 内置
|
|
16
|
-
* `dsh-session-format-v0-to-v1` / `v1-to-v2` / `v2-to-v3` 三个迁移包,
|
|
17
|
-
* `SESSION_FORMAT_VERSION = 3`;且 `v2-to-v3/lib/index.js:361-371` 明示 **v2 仍是
|
|
18
|
-
* `{start,end}`**、由迁移改名为 `startSeq/endSeq`。
|
|
19
|
-
*
|
|
20
|
-
* ⇒ 结论:**用运行时的 v3 `foldSurface` 去终验 v0/v1/v2 文件必然误报**;旧格式必须用
|
|
21
|
-
* 本文件这份等价实现(rc.7 `foldSurface` 的逐条移植,语义等价、同样 fail-loud)。
|
|
22
|
-
*
|
|
23
|
-
* 移植源:`@deepseek-ai/dsh-session@0.1.0-rc.7` `lib/index.js:229-455`
|
|
24
|
-
* (`isSurfaceEligibleType` / `isEventSeq` / `isReplaceOp` / `surfaceOpOf` /
|
|
25
|
-
* `assertProvenance` / `replacementRange` / `isDeepEqualJson` / `assertToolResultRewrite` /
|
|
26
|
-
* `planSurfaceEvent` / `applySurfacePlan` / `foldSurface`)。
|
|
27
|
-
*
|
|
28
|
-
* 与官方相同的点:**抛错即拒绝**(官方加载期 `SessionPersistenceCorruptionError` 同源
|
|
29
|
-
* 判据);事件按 `index` 作为期望 seq(`baseSeq = 0`),与官方 `foldSurface(events)` 一致。
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
/** v0/v1/v2 的 surface 候选类型(官方 rc.7 `SURFACE_EVENT_TYPES`,3 条)。 */
|
|
33
|
-
const SURFACE_EVENT_TYPES = new Set(['user/message', 'assistant/message', 'tool/result']);
|
|
34
|
-
|
|
35
|
-
/** Whether an event type can join the model-visible surface(rc.7 `isSurfaceEligibleType`)。 */
|
|
36
|
-
export function isLegacySurfaceEligibleType(type) {
|
|
37
|
-
return SURFACE_EVENT_TYPES.has(type);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Whether a runtime value is a non-negative safe event sequence(rc.7 `isEventSeq`)。 */
|
|
41
|
-
function isEventSeq(value) {
|
|
42
|
-
return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Whether a runtime value is the exact positional-replacement shape(rc.7 `isReplaceOp`:start/end)。 */
|
|
46
|
-
function isReplaceOp(value) {
|
|
47
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
48
|
-
const op = value;
|
|
49
|
-
return (
|
|
50
|
-
Object.keys(op).length === 3 &&
|
|
51
|
-
Object.hasOwn(op, 'op') && Object.hasOwn(op, 'start') && Object.hasOwn(op, 'end') &&
|
|
52
|
-
op.op === 'replace' && isEventSeq(op.start) && isEventSeq(op.end)
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/** Validate event-local surface eligibility and return its operation(rc.7 `surfaceOpOf`)。 */
|
|
57
|
-
function surfaceOpOf(event) {
|
|
58
|
-
if (!SURFACE_EVENT_TYPES.has(event.type)) {
|
|
59
|
-
if (event.surfaceOp !== undefined) throw new Error(`session event "${event.type}" is not surface-eligible and cannot carry surfaceOp`);
|
|
60
|
-
if (event.sourceEventSeqs !== undefined) throw new Error(`session event "${event.type}" is not surface-eligible and cannot carry sourceEventSeqs`);
|
|
61
|
-
return undefined;
|
|
62
|
-
}
|
|
63
|
-
const op = event.surfaceOp;
|
|
64
|
-
if (op === undefined) throw new Error(`session event "${event.type}" is surface-eligible and requires a surfaceOp marker`);
|
|
65
|
-
if (op === 'append') return op;
|
|
66
|
-
if (op === null || typeof op !== 'object' || Array.isArray(op)) throw new Error(`session event "${event.type}" carries an invalid surfaceOp`);
|
|
67
|
-
if (!isReplaceOp(op)) throw new Error(`session event "${event.type}" carries an invalid replace surfaceOp`);
|
|
68
|
-
return op;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** Validate cited source-event seqs against prior log entries and the replacement range(rc.7 `assertProvenance`)。 */
|
|
72
|
-
function assertProvenance(event, shadowedSeqs) {
|
|
73
|
-
const raw = event.sourceEventSeqs;
|
|
74
|
-
const sources = new Set();
|
|
75
|
-
if (raw !== undefined) {
|
|
76
|
-
if (!Array.isArray(raw)) throw new Error(`sourceEventSeqs on event at seq ${event.seq} must be an array when present`);
|
|
77
|
-
if (raw.length === 0 && event.type !== 'assistant/message') throw new Error('sourceEventSeqs must not be empty except on assistant/message');
|
|
78
|
-
let nonEarlierSource;
|
|
79
|
-
for (const source of raw) {
|
|
80
|
-
if (!isEventSeq(source)) throw new Error(`session event "${event.type}" sourceEventSeqs must densely contain non-negative safe integers`);
|
|
81
|
-
sources.add(source);
|
|
82
|
-
if (nonEarlierSource === undefined && source >= event.seq) nonEarlierSource = source;
|
|
83
|
-
}
|
|
84
|
-
if (sources.size !== raw.length) throw new Error('sourceEventSeqs must not contain duplicates');
|
|
85
|
-
if (nonEarlierSource !== undefined) throw new Error(`sourceEventSeqs must reference earlier events: ${nonEarlierSource} >= current seq ${event.seq}`);
|
|
86
|
-
}
|
|
87
|
-
const missing = shadowedSeqs.filter((seq) => !sources.has(seq));
|
|
88
|
-
if (missing.length > 0) throw new Error(`surface replace: sourceEventSeqs must include every shadowed surface node; missing ${missing.join(', ')}`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Locate one replacement range without mutating the current fold state(rc.7 `replacementRange`)。 */
|
|
92
|
-
function replacementRange(state, op) {
|
|
93
|
-
const startIdx = state.nodes.indexOf(op.start);
|
|
94
|
-
if (startIdx === -1) throw new Error(`surface replace: start seq ${op.start} not found in surface`);
|
|
95
|
-
const endIdx = state.nodes.indexOf(op.end);
|
|
96
|
-
if (endIdx === -1) throw new Error(`surface replace: end seq ${op.end} not found in surface`);
|
|
97
|
-
if (startIdx > endIdx) throw new Error(`surface replace: start seq ${op.start} (index ${startIdx}) is after end seq ${op.end} (index ${endIdx})`);
|
|
98
|
-
return { startIdx, endIdx, shadowedSeqs: state.nodes.slice(startIdx, endIdx + 1) };
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Deep structural equality over the session-event JSON value domain(rc.7 `isDeepEqualJson`)。 */
|
|
102
|
-
function isDeepEqualJson(a, b) {
|
|
103
|
-
if (a === b) return true;
|
|
104
|
-
if (Array.isArray(a) || Array.isArray(b)) {
|
|
105
|
-
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) return false;
|
|
106
|
-
return a.every((item, i) => isDeepEqualJson(item, b[i]));
|
|
107
|
-
}
|
|
108
|
-
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
|
|
109
|
-
const aKeys = Object.keys(a);
|
|
110
|
-
if (aKeys.length !== Object.keys(b).length) return false;
|
|
111
|
-
return aKeys.every((key) => Object.hasOwn(b, key) && isDeepEqualJson(a[key], b[key]));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/** Restrict a tool-result replacement to one current result's content(rc.7 `assertToolResultRewrite`)。 */
|
|
115
|
-
function assertToolResultRewrite(event, shadowedSeqs, events, baseSeq) {
|
|
116
|
-
if (event.type !== 'tool/result') return;
|
|
117
|
-
if (shadowedSeqs.length !== 1) throw new Error('tool/result surface replacement must rewrite exactly one current node');
|
|
118
|
-
for (const originalSeq of shadowedSeqs) {
|
|
119
|
-
const original = events[originalSeq - baseSeq];
|
|
120
|
-
if (original?.type !== 'tool/result') throw new Error('tool/result surface replacement must target a current tool/result');
|
|
121
|
-
const originalRest = { ...original.data };
|
|
122
|
-
const replacementRest = { ...event.data };
|
|
123
|
-
const originalResult = original.data.message.content[0];
|
|
124
|
-
const replacementResult = event.data.message.content[0];
|
|
125
|
-
originalRest.message = { ...original.data.message, content: [{ ...originalResult, content: null }] };
|
|
126
|
-
replacementRest.message = { ...event.data.message, content: [{ ...replacementResult, content: null }] };
|
|
127
|
-
if (!isDeepEqualJson(originalRest, replacementRest)) throw new Error('tool/result surface replacement may change only content');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/** Validate one event at its replay boundary and prepare its atomic fold transition(rc.7 `planSurfaceEvent`)。 */
|
|
132
|
-
function planSurfaceEvent(state, event, expectedSeq, events, baseSeq) {
|
|
133
|
-
if (event.seq !== expectedSeq) throw new Error(`session event seq ${event.seq} is not contiguous; expected ${expectedSeq}`);
|
|
134
|
-
const surfaceOp = surfaceOpOf(event);
|
|
135
|
-
if (surfaceOp === undefined) return undefined;
|
|
136
|
-
if (surfaceOp === 'append') {
|
|
137
|
-
assertProvenance(event, []);
|
|
138
|
-
return { kind: 'append', seq: event.seq };
|
|
139
|
-
}
|
|
140
|
-
const range = replacementRange(state, surfaceOp);
|
|
141
|
-
assertProvenance(event, range.shadowedSeqs);
|
|
142
|
-
assertToolResultRewrite(event, range.shadowedSeqs, events, baseSeq);
|
|
143
|
-
return { kind: 'replace', seq: event.seq, start: surfaceOp.start, end: surfaceOp.end, ...range };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** Commit one previously validated surface transition(rc.7 `applySurfacePlan`)。 */
|
|
147
|
-
function applySurfacePlan(state, plan) {
|
|
148
|
-
if (plan?.kind === 'append') state.nodes.push(plan.seq);
|
|
149
|
-
else if (plan?.kind === 'replace') {
|
|
150
|
-
state.nodes.splice(plan.startIdx, plan.endIdx - plan.startIdx + 1, plan.seq);
|
|
151
|
-
state.replaceGeneration += 1;
|
|
152
|
-
}
|
|
153
|
-
if (plan?.kind !== 'replace') return undefined;
|
|
154
|
-
return { seq: plan.seq, start: plan.start, end: plan.end, shadowedSeqs: plan.shadowedSeqs };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** Apply one event and return replacement metadata only when one occurred(rc.7 `applySurfaceEvent`)。 */
|
|
158
|
-
function applySurfaceEvent(state, event, expectedSeq, events, baseSeq) {
|
|
159
|
-
return applySurfacePlan(state, planSurfaceEvent(state, event, expectedSeq, events, baseSeq));
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Replay a complete session log through the **v0/v1/v2** canonical surface fold.
|
|
164
|
-
*
|
|
165
|
-
* @param events - session events in contiguous seq order(seq 从 0 起,与官方 `foldSurface` 同约定)。
|
|
166
|
-
* @returns {{ nodes: number[], replacements: Array<{seq:number,start:number,end:number,shadowedSeqs:number[]}> }}
|
|
167
|
-
* @throws when an event violates surface metadata, source-event references, range, or tool-result rewrite rules.
|
|
4
|
+
* 旧格式视图终验的公开面(实现层):实现位于 adapters/dsh/surface.js。
|
|
5
|
+
* 导出名与既有版本保持一致(公开面不变)。
|
|
168
6
|
*/
|
|
169
|
-
export
|
|
170
|
-
const state = { nodes: [], replaceGeneration: 0 };
|
|
171
|
-
const replacements = [];
|
|
172
|
-
for (const [index, event] of events.entries()) {
|
|
173
|
-
const replacement = applySurfaceEvent(state, event, index, events, 0);
|
|
174
|
-
if (replacement !== undefined) replacements.push(replacement);
|
|
175
|
-
}
|
|
176
|
-
return { nodes: [...state.nodes], replacements };
|
|
177
|
-
}
|
|
7
|
+
export { isLegacySurfaceEligibleType, legacyFoldSurface } from '../adapters/dsh/surface.js';
|
package/lib/log-reader.js
CHANGED
|
@@ -1,299 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* dsh-log-contract · lib/log-reader.js
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* 契约来源:
|
|
8
|
-
* - 帧扫描/撕裂尾帧判定:按 zstd 规范逐帧扫(magic 0xFD2FB528、descriptor、block 头)。
|
|
9
|
-
* - 行解码:本地兼容层 `./compat.js` 的 `decodeStorageRecord`(原官方导出于 0.1.5 移除)
|
|
10
|
-
* (lib/index.js:1029,validateRow :922 / expandRow :973)。
|
|
11
|
-
* - 损坏语义:chunk 行损坏 = 整段 run 丢失且加载失败(dsh-session
|
|
12
|
-
* lib/index.js:1022-1024 注释明示 fail-loud,无跳过逃生舱)。
|
|
13
|
-
*/
|
|
14
|
-
import fs from 'node:fs';
|
|
15
|
-
import { zstdDecompressSync } from 'node:zlib';
|
|
16
|
-
import { decodeStorageRecord, normalizeEventSeqRanges } from './compat.js';
|
|
17
|
-
|
|
18
|
-
const ZSTD_MAGIC = 0xfd2fb528;
|
|
19
|
-
|
|
20
|
-
/** 扫描 zstd 帧边界;返回 [start, end] 列表与是否出现撕裂尾帧。 */
|
|
21
|
-
export function scanZstdFrames(buf) {
|
|
22
|
-
const frames = [];
|
|
23
|
-
let offset = 0;
|
|
24
|
-
let torn = false;
|
|
25
|
-
while (offset < buf.length) {
|
|
26
|
-
const start = offset;
|
|
27
|
-
if (buf.length - offset < 4 || buf.readUInt32LE(offset) !== ZSTD_MAGIC) {
|
|
28
|
-
torn = true;
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
offset += 4;
|
|
32
|
-
if (offset === buf.length) {
|
|
33
|
-
torn = true;
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
const descriptor = buf.readUInt8(offset);
|
|
37
|
-
offset += 1;
|
|
38
|
-
if ((descriptor & 24) !== 0) {
|
|
39
|
-
// 保留位被置位:非法帧头
|
|
40
|
-
return { frames, torn: true, reason: 'reserved-bit' };
|
|
41
|
-
}
|
|
42
|
-
const contentSizeFlag = descriptor >>> 6;
|
|
43
|
-
const singleSegment = (descriptor & 32) !== 0;
|
|
44
|
-
const checksum = (descriptor & 4) !== 0;
|
|
45
|
-
const dictFlag = descriptor & 3;
|
|
46
|
-
const dictBytes = dictFlag === 3 ? 4 : dictFlag;
|
|
47
|
-
const contentSizeBytes = contentSizeFlag === 0 ? (singleSegment ? 1 : 0) : (1 << contentSizeFlag);
|
|
48
|
-
const hdrExtra = (singleSegment ? 0 : 1) + dictBytes + contentSizeBytes;
|
|
49
|
-
if (buf.length - offset < hdrExtra) {
|
|
50
|
-
torn = true;
|
|
51
|
-
break;
|
|
52
|
-
}
|
|
53
|
-
offset += hdrExtra;
|
|
54
|
-
let lastBlock = false;
|
|
55
|
-
while (!lastBlock) {
|
|
56
|
-
if (buf.length - offset < 3) {
|
|
57
|
-
torn = true;
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
const bh = buf.readUInt32LE(offset);
|
|
61
|
-
offset += 3;
|
|
62
|
-
lastBlock = (bh & 1) !== 0;
|
|
63
|
-
offset += (bh >>> 3) & 0x1fffff;
|
|
64
|
-
}
|
|
65
|
-
if (torn) break;
|
|
66
|
-
if (offset > buf.length) {
|
|
67
|
-
// 块内容越过文件末尾:最后一帧被截断,不能算完整帧
|
|
68
|
-
torn = true;
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
if (checksum) offset += 4;
|
|
72
|
-
frames.push([start, offset]);
|
|
73
|
-
}
|
|
74
|
-
return { frames, torn, reason: torn ? 'incomplete-tail' : undefined };
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** 把 zstd 多帧拼成完整明文;任一帧解码失败即抛错(N5 单帧全损语义)。 */
|
|
78
|
-
export function decompressZstd(buf, { allowTorn = true } = {}) {
|
|
79
|
-
const { frames, torn } = scanZstdFrames(buf);
|
|
80
|
-
// ── 2026-09-14 实测:这里的 Z2 是误报 ───────────────────────────────────
|
|
81
|
-
// 撕裂尾帧**不是**损坏:宿主 `dsh-session-persistence-jsonl@0.1.5-rc.1`
|
|
82
|
-
// · `readZstdPrefix`(lib/index.js:2791-2849)——"Decode complete frames and retain
|
|
83
|
-
// complete JSONL records from a torn final frame",恢复并返回 `tornTruncateTo`(:2847);
|
|
84
|
-
// · 下次写入 `:226-228` 调 `truncateTornTail` 自动截断。
|
|
85
|
-
// 旧实现(本函数)对 torn 直接抛错 ⇒ `loadSessionLog` 返回 header=null/events=[]
|
|
86
|
-
// ⇒ `validate.js` 级联出 Z1(warning)+Z2(error)+H1(error)、verdict=broken —— 把宿主
|
|
87
|
-
// **本来能修**的"边写边读"健康态报成"会话坏了"。现在对齐宿主:解出全部**完整帧**,
|
|
88
|
-
// 尾帧未写完的部分丢弃,由调用方用 `frameInfo.torn` 报 Z1(warning)。
|
|
89
|
-
// `frames.length === 0`(连一个完整帧都没有,例如头帧就断了)**仍然报错** —— 宿主
|
|
90
|
-
// `readZstdPrefix:2793` 对这种情况同样抛 "empty or header-less Zstandard session log"。
|
|
91
|
-
if (frames.length === 0) throw new Error('空文件 / 无完整 zstd 帧(incomplete or header-less session log)');
|
|
92
|
-
if (torn && !allowTorn) throw new Error('zstd 尾帧撕裂(incomplete tail frame)——strict 模式(allowTorn:false)');
|
|
93
|
-
// 逐帧解码后一次性 Buffer.concat:避免每帧一次 concat 的 O(n²) 拷贝
|
|
94
|
-
const parts = new Array(frames.length);
|
|
95
|
-
for (let i = 0; i < frames.length; i++) {
|
|
96
|
-
const [s, e] = frames[i];
|
|
97
|
-
try {
|
|
98
|
-
parts[i] = zstdDecompressSync(buf.subarray(s, e));
|
|
99
|
-
} catch (err) {
|
|
100
|
-
// 完整帧解码失败才是真损坏(Z2):磁盘 bitrot / 传输截断 / 保留位非法
|
|
101
|
-
throw new Error(`zstd 帧解码失败 [${s},${e}):${err.message}`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return Buffer.concat(parts);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 读取文件尾部最后一条事件的 seq(看门狗 R1 用,只解最后一帧,O(帧数) 内)。
|
|
109
|
-
*
|
|
110
|
-
* 语义:zstd 多帧文件取最后一个**完整**帧解压(撕裂尾帧跳过——可能正在写入),
|
|
111
|
-
* 从帧内末尾向前找第一条合法 JSON 行,用 `decodeStorageRecord` 展开取末事件 seq
|
|
112
|
-
* (chunk 行展开后末成员即该 run 的最新 seq)。明文文件直接扫末尾。
|
|
113
|
-
*
|
|
114
|
-
* @param {string} path 日志文件路径
|
|
115
|
-
* @returns {number|null} 尾部 seq;读取失败/无可解析行返回 null(调用方降级不检查)。
|
|
116
|
-
*/
|
|
117
|
-
export function tailSeq(path) {
|
|
118
|
-
let buf;
|
|
119
|
-
try {
|
|
120
|
-
buf = fs.readFileSync(path);
|
|
121
|
-
} catch {
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
const isZstd = buf.length >= 4 && buf.readUInt32LE(0) === ZSTD_MAGIC;
|
|
125
|
-
let plain;
|
|
126
|
-
if (isZstd) {
|
|
127
|
-
const { frames } = scanZstdFrames(buf);
|
|
128
|
-
if (frames.length === 0) return null;
|
|
129
|
-
const [s, e] = frames[frames.length - 1];
|
|
130
|
-
try {
|
|
131
|
-
plain = zstdDecompressSync(buf.subarray(s, e));
|
|
132
|
-
} catch {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
plain = buf;
|
|
137
|
-
}
|
|
138
|
-
const lines = plain.toString('utf8').split('\n');
|
|
139
|
-
for (let i = lines.length - 1; i >= 0; i--) {
|
|
140
|
-
const line = lines[i];
|
|
141
|
-
if (line.trim().length === 0) continue; // 帧边界产物(空行)跳过
|
|
142
|
-
try {
|
|
143
|
-
const decoded = decodeStorageRecord(JSON.parse(line));
|
|
144
|
-
if (decoded.length > 0) return decoded[decoded.length - 1].seq;
|
|
145
|
-
} catch { /* 损坏行/不可解析:继续向前找 */ }
|
|
146
|
-
}
|
|
147
|
-
return null;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 读取并解码一个会话日志文件(.jsonl / .jsonl.zstd / 任意带 zstd 魔数的文件)。
|
|
152
|
-
*
|
|
153
|
-
* @param {string} path 日志文件路径
|
|
154
|
-
* @returns {{
|
|
155
|
-
* header: object|null, headerLine: number|null,
|
|
156
|
-
* rows: Array<{lineNo:number, value:unknown, decoded:Array|null, error:Error|null}>,
|
|
157
|
-
* events: Array<{seq:number, event:object, lineNo:number}>,
|
|
158
|
-
* frameInfo: {frames:number, torn:boolean, compressedBytes:number, plaintextBytes:number},
|
|
159
|
-
* }}
|
|
160
|
-
*/
|
|
161
|
-
export function loadSessionLog(path) {
|
|
162
|
-
const buf = fs.readFileSync(path);
|
|
163
|
-
const isZstd = buf.length >= 4 && buf.readUInt32LE(0) === ZSTD_MAGIC;
|
|
164
|
-
|
|
165
|
-
let plain;
|
|
166
|
-
let frameInfo;
|
|
167
|
-
let loadError = null;
|
|
168
|
-
if (isZstd) {
|
|
169
|
-
try {
|
|
170
|
-
plain = decompressZstd(buf);
|
|
171
|
-
} catch (err) {
|
|
172
|
-
loadError = err;
|
|
173
|
-
plain = Buffer.alloc(0);
|
|
174
|
-
}
|
|
175
|
-
const { frames, torn } = scanZstdFrames(buf);
|
|
176
|
-
// 帧形状事实(Z4/Z5,2026-09-16 真机事故):官方写盘形状 = **header 独占第一帧** +
|
|
177
|
-
// 事件逐帧追加;读取端有硬断言 `assertZstdHeaderFrame`(首帧必须**恰好一行**)。
|
|
178
|
-
// 这里把该事实带出来给 validate 判:整文件被重压成单帧时,首帧装的是全部行 ⇒ 宿主必抛,
|
|
179
|
-
// 而抛点在 `listArtifacts` **遍历全库**、无 per-session 容错 ⇒ 整个客户端起不来。
|
|
180
|
-
let firstFrameOneLine = true, firstFrameLines = 1;
|
|
181
|
-
try {
|
|
182
|
-
const [s0, e0] = frames[0];
|
|
183
|
-
const first = zstdDecompressSync(buf.subarray(s0, e0)).toString('utf8');
|
|
184
|
-
const nl = first.indexOf('\n');
|
|
185
|
-
firstFrameOneLine = nl === first.length - 1 && first.length > 1;
|
|
186
|
-
firstFrameLines = first.split('\n').filter((x) => x !== '').length;
|
|
187
|
-
} catch { /* 首帧解码失败另有 Z2 报 */ }
|
|
188
|
-
frameInfo = { frames: frames.length, torn, compressedBytes: buf.length, plaintextBytes: plain.length, firstFrameOneLine, firstFrameLines };
|
|
189
|
-
} else {
|
|
190
|
-
plain = buf;
|
|
191
|
-
frameInfo = { frames: 0, torn: false, compressedBytes: 0, plaintextBytes: plain.length };
|
|
192
|
-
}
|
|
193
|
-
if (loadError) {
|
|
194
|
-
return {
|
|
195
|
-
header: null,
|
|
196
|
-
headerLine: 0,
|
|
197
|
-
rows: [],
|
|
198
|
-
events: [],
|
|
199
|
-
frameInfo: { ...frameInfo, error: loadError.message },
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// 按行解码(Buffer 切分),避免整文件字符串化(>536MB 抛字符串上限)
|
|
204
|
-
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 })();
|
|
205
|
-
|
|
206
|
-
// 第一行 = header
|
|
207
|
-
const headerLine = 0;
|
|
208
|
-
let header = null;
|
|
209
|
-
const headerRaw = lines[0];
|
|
210
|
-
try {
|
|
211
|
-
header = JSON.parse(headerRaw);
|
|
212
|
-
} catch {
|
|
213
|
-
header = null;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const rows = [];
|
|
217
|
-
const events = [];
|
|
218
|
-
for (let i = 1; i < lines.length; i++) {
|
|
219
|
-
const line = lines[i];
|
|
220
|
-
if (line.trim().length === 0) continue; // 帧边界产物(空行)跳过
|
|
221
|
-
const lineNo = i;
|
|
222
|
-
let value;
|
|
223
|
-
try {
|
|
224
|
-
value = JSON.parse(line);
|
|
225
|
-
} catch (err) {
|
|
226
|
-
rows.push({ lineNo, value: null, decoded: null, error: err });
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
let decoded;
|
|
230
|
-
try {
|
|
231
|
-
decoded = decodeStorageRecord(value);
|
|
232
|
-
} catch (err) {
|
|
233
|
-
rows.push({ lineNo, value, decoded: null, error: err });
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
// v3 storage-form `sourceEventSeqs` 区间编码 → 内存稠密序列(C1)。
|
|
237
|
-
// 必须在**读取层唯一入口**做:下游 S5/S6 与官方 foldSurface 都按稠密整数序列判定,
|
|
238
|
-
// 拿到含 `[start,end]` 对的未展开形态会误判真实健康会话(S5/S6/S8 + --resume broken)。
|
|
239
|
-
const normalized = decoded.map(normalizeEventSeqRanges);
|
|
240
|
-
for (const event of normalized) {
|
|
241
|
-
events.push({ seq: event.seq, event, lineNo });
|
|
242
|
-
}
|
|
243
|
-
rows.push({ lineNo, value, decoded: normalized, error: null });
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// 按 seq 排序(文件顺序即日志顺序,此处防御性排序以便下游契约检查)
|
|
247
|
-
events.sort((a, b) => a.seq - b.seq);
|
|
248
|
-
|
|
249
|
-
return { header, headerLine, rows, events, frameInfo };
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* 轻量读取会话文件 header(只解第一帧,不读全文件帧)。
|
|
254
|
-
*
|
|
255
|
-
* 帧布局:帧 1 = header 行(单行 JSON,~几百字节),帧 2+ = 事件流。
|
|
256
|
-
* 短码推导(工作区 createdAt 序号)需要扫全部会话但只需要 header——
|
|
257
|
-
* 读文件前缀 64KiB 足够覆盖完整帧 1,避免全量读大文件(大会话压缩后可达
|
|
258
|
-
* 数百 MiB)。失败返回 null(调用方降级)。
|
|
259
|
-
*
|
|
260
|
-
* @param {string} path 会话日志文件路径(.jsonl.zstd 或明文 .jsonl)。
|
|
261
|
-
* @returns {object|null} header 对象;无法解析返回 null。
|
|
4
|
+
* 日志读取的公开面(实现层):实现位于 adapters/dsh/frames.js。
|
|
5
|
+
* 导出名与既有版本保持一致(公开面不变)。
|
|
262
6
|
*/
|
|
263
|
-
export
|
|
264
|
-
let head;
|
|
265
|
-
try {
|
|
266
|
-
const fd = fs.openSync(path, 'r');
|
|
267
|
-
const buf = Buffer.alloc(64 * 1024);
|
|
268
|
-
const n = fs.readSync(fd, buf, 0, buf.length, 0);
|
|
269
|
-
fs.closeSync(fd);
|
|
270
|
-
head = buf.subarray(0, n);
|
|
271
|
-
} catch {
|
|
272
|
-
return null;
|
|
273
|
-
}
|
|
274
|
-
if (head.length < 4 || head.readUInt32LE(0) !== ZSTD_MAGIC) {
|
|
275
|
-
// 明文(无 zstd magic):直接取首行
|
|
276
|
-
try {
|
|
277
|
-
const line = head.toString('utf8').split('\n').find((l) => l.trim().length > 0);
|
|
278
|
-
return line ? JSON.parse(line) : null;
|
|
279
|
-
} catch {
|
|
280
|
-
return null;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
const { frames } = scanZstdFrames(head);
|
|
284
|
-
if (frames.length === 0) return null;
|
|
285
|
-
const [s, e] = frames[0];
|
|
286
|
-
let plain;
|
|
287
|
-
try {
|
|
288
|
-
plain = zstdDecompressSync(head.subarray(s, e));
|
|
289
|
-
} catch {
|
|
290
|
-
return null;
|
|
291
|
-
}
|
|
292
|
-
const line = plain.toString('utf8').split('\n').find((l) => l.trim().length > 0);
|
|
293
|
-
if (!line) return null;
|
|
294
|
-
try {
|
|
295
|
-
return JSON.parse(line);
|
|
296
|
-
} catch {
|
|
297
|
-
return null;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
7
|
+
export { scanZstdFrames, decompressZstd, tailSeq, loadSessionLog, readSessionHeader } from '../adapters/dsh/frames.js';
|