dsh-log-contract 0.3.17 → 0.3.19
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/bin/dsh-log-contract.mjs +72 -7
- package/docs/CONTRACTS.md +48 -13
- package/lib/archive.js +309 -0
- package/lib/checks.js +422 -7
- package/lib/contracts.js +50 -9
- package/lib/host-probes.js +43 -9
- package/lib/index.js +2 -1
- package/lib/prewrite.js +21 -5
- package/lib/validate.js +103 -15
- package/lib/version-support.js +8 -7
- package/lib/vocab.js +20 -11
- package/package.json +4 -3
package/lib/checks.js
CHANGED
|
@@ -8,17 +8,20 @@
|
|
|
8
8
|
* 每条违规都挂 `lib/contracts.js` 中的规则 id 与官方源码出处。
|
|
9
9
|
*/
|
|
10
10
|
import { isSurfaceEligibleType } from '@deepseek-ai/dsh-session';
|
|
11
|
-
import { isJsonValue } from './compat.js';
|
|
11
|
+
import { isJsonValue, decodeSeqRanges } from './compat.js';
|
|
12
12
|
import { currentVocabulary, currentFold, V0_EVENT_TYPES } from './vocab.js';
|
|
13
13
|
import { ruleById } from './contracts.js';
|
|
14
14
|
|
|
15
15
|
export const SURFACE_TYPES = new Set(['user/message', 'assistant/message', 'tool/result']);
|
|
16
16
|
/** v3 新增 `system/message`(官方 `SURFACE_EVENT_TYPES`,0.1.5 `lib/index.js:149-154`)。 */
|
|
17
17
|
export const MODERN_SURFACE_TYPES = new Set([...SURFACE_TYPES, 'system/message']);
|
|
18
|
+
/** v4 新增 `developer/message`(官方 `SURFACE_EVENT_TYPES`,dsh-session@0.2.0-rc.2 `src/surface.ts:50-56`)。 */
|
|
19
|
+
export const V4_SURFACE_TYPES = new Set([...MODERN_SURFACE_TYPES, 'developer/message']);
|
|
18
20
|
export const CHUNK_ROW_TYPES = new Set(['text-chunks', 'reasoning-chunks', 'tool-call-chunks']);
|
|
19
21
|
|
|
20
|
-
/** 当前文件版本适用的 surface 候选类型集(v3 起含 system/message)。version = 被检文件版本。 */
|
|
22
|
+
/** 当前文件版本适用的 surface 候选类型集(v3 起含 system/message,v4 起含 developer/message)。version = 被检文件版本。 */
|
|
21
23
|
export function currentSurfaceTypes(version) {
|
|
24
|
+
if (version >= 4) return V4_SURFACE_TYPES;
|
|
22
25
|
return version >= 3 ? MODERN_SURFACE_TYPES : SURFACE_TYPES;
|
|
23
26
|
}
|
|
24
27
|
export const MARKER_PREFIXES = ['retrace', 'message-editor'];
|
|
@@ -96,15 +99,28 @@ export function envelopeViolations(event, loc, version) {
|
|
|
96
99
|
}
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
|
-
out.push(...messageShapeViolations(event, loc));
|
|
102
|
+
out.push(...messageShapeViolations(event, loc, version));
|
|
100
103
|
return out;
|
|
101
104
|
}
|
|
102
105
|
|
|
103
106
|
/** 宿主信封键白名单(assertSessionEventEnvelope,dsh-session@0.1.5-rc.1 lib/index.js:852-861)。 */
|
|
104
107
|
export const ENVELOPE_ALLOWED_KEYS = new Set(['type', 'seq', 'time', 'data', 'surfaceOp', 'sourceEventSeqs', 'ignorable']);
|
|
105
108
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
109
|
+
/** v4 消息类事件 → 期望 role(dsh-session@0.2.0-rc.2 `MESSAGE_ROLE_BY_TYPE`,`src/index.ts:323-329`)。 */
|
|
110
|
+
const V4_MESSAGE_ROLE_BY_TYPE = {
|
|
111
|
+
'system/message': 'system',
|
|
112
|
+
'developer/message': 'developer',
|
|
113
|
+
'user/message': 'user',
|
|
114
|
+
'assistant/message': 'assistant',
|
|
115
|
+
'tool/result': 'tool',
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 镜像官方 assertMessageEventShape(lib/index.js:1242-1266)。**按被检文件版本**择路:
|
|
120
|
+
* v0–v3 → 旧形状(下方原逻辑);v4 → `v4MessageShapeViolations`。
|
|
121
|
+
*/
|
|
122
|
+
export function messageShapeViolations(event, loc, version) {
|
|
123
|
+
if (version >= 4) return v4MessageShapeViolations(event, loc);
|
|
108
124
|
const type = event.type;
|
|
109
125
|
// 把 v3 新增的 `system/message` 纳入形状检查(MESSAGE_ROLE_BY_TYPE:system/message → system,
|
|
110
126
|
// dsh-session@0.1.5-rc.1 lib/index.js:917-926;source 要求 :942-944)。旧实现只查 user/assistant/tool
|
|
@@ -160,6 +176,77 @@ export function messageShapeViolations(event, loc) {
|
|
|
160
176
|
return out;
|
|
161
177
|
}
|
|
162
178
|
|
|
179
|
+
/**
|
|
180
|
+
* v4 消息形状。两层一手依据:
|
|
181
|
+
* - 运行时 load 路径:dsh-session@0.2.0-rc.2 `assertMessageEventShape`(`src/index.ts:331-381`)——
|
|
182
|
+
* role 表、system-prompt source、model source、tool source + `toolCallId` 匹配;
|
|
183
|
+
* - 文件读取路径:`dsh-session-format-v3-to-v4` 原生 v4 准入(`src/tool-role.ts:89-109`、
|
|
184
|
+
* `src/message-sources.ts:9`)——残留 `tool-result` 包装块、非布尔 `isError`、`data.error` 而
|
|
185
|
+
* `isError !== true`、`source.kind === 'plugin'` 一律 SessionFormatError ⇒ 会话从盘上读不回来。
|
|
186
|
+
* `adoptSessionEvent` 不查这几条,所以只看运行时会漏掉"写得进、读不回"的形状。
|
|
187
|
+
*
|
|
188
|
+
* 与 v3 的差异(v3→v4 迁移 `tool-role.ts` / `sources.ts` 的逐项改写):
|
|
189
|
+
* - 新增 `developer/message`(role `developer`);
|
|
190
|
+
* - `tool/result`:role `tool`;去掉 `tool-result` 包装块,content 直接是结果数组;
|
|
191
|
+
* `message.toolCallId` 必须等于 `source.callId`;`isError` 若存在必须为布尔;
|
|
192
|
+
* - `system/message`:source.kind 必须为 `system-prompt`(v3 是 plugin 包装);
|
|
193
|
+
* - 任何消息的 source.kind 都不得为 `plugin`(v4 把插件包装改写成生产者自有 kind)。
|
|
194
|
+
*/
|
|
195
|
+
function v4MessageShapeViolations(event, loc) {
|
|
196
|
+
const type = event.type;
|
|
197
|
+
const expectedRole = V4_MESSAGE_ROLE_BY_TYPE[type];
|
|
198
|
+
if (expectedRole === undefined) return [];
|
|
199
|
+
const out = [];
|
|
200
|
+
const data = event.data;
|
|
201
|
+
const record = typeof data === 'object' && data !== null ? data : undefined;
|
|
202
|
+
const message = type === 'user/message' ? record : record?.message;
|
|
203
|
+
const shape = () => `(seq ${event.seq})消息形状`;
|
|
204
|
+
if (typeof message !== 'object' || message === null) {
|
|
205
|
+
out.push(violation('E6', loc, `${shape()}:缺少 message 对象`));
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
if (typeof message.id !== 'string' || message.id === '') {
|
|
209
|
+
out.push(violation('E6', loc, `${shape()}:id 必须为非空字符串`));
|
|
210
|
+
}
|
|
211
|
+
if (message.role !== expectedRole) {
|
|
212
|
+
out.push(violation('E6', loc, `${shape()}:role 必须为 "${expectedRole}",实际 ${String(message.role)}`));
|
|
213
|
+
}
|
|
214
|
+
const source = message.source;
|
|
215
|
+
if (typeof source !== 'object' || source === null || typeof source.kind !== 'string' || source.kind === '') {
|
|
216
|
+
out.push(violation('E6', loc, `${shape()}:source.kind 缺失或非法`));
|
|
217
|
+
} else if (source.kind === 'plugin') {
|
|
218
|
+
out.push(violation('E6', loc, `${shape()}:v4 不再接受 plugin source(kind==='plugin')——v3→v4 迁移把它改写为生产者自有 kind`));
|
|
219
|
+
}
|
|
220
|
+
if (!Array.isArray(message.content)) {
|
|
221
|
+
out.push(violation('E6', loc, `${shape()}:content 必须为数组`));
|
|
222
|
+
}
|
|
223
|
+
if (type === 'assistant/message') {
|
|
224
|
+
if (source?.kind !== 'model' || typeof source.provider !== 'string' || source.provider === '' || typeof source.model !== 'string' || source.model === '') {
|
|
225
|
+
out.push(violation('E6', loc, `${shape()}:assistant/message 必须带 model source(provider/model 非空)`));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (type === 'system/message' && source?.kind !== 'system-prompt') {
|
|
229
|
+
out.push(violation('E9', loc, `${shape()}:system/message 必须带 system-prompt source(kind==='system-prompt')——dsh-session@0.2.0-rc.2 src/index.ts:360-364`));
|
|
230
|
+
}
|
|
231
|
+
if (type === 'tool/result') {
|
|
232
|
+
if (source?.kind !== 'tool' || typeof source.callId !== 'string' || source.callId === '') {
|
|
233
|
+
out.push(violation('E6', loc, `${shape()}:tool/result 必须带 tool source(callId 非空)`));
|
|
234
|
+
} else if (message.toolCallId !== source.callId) {
|
|
235
|
+
out.push(violation('E6', loc, `${shape()}:message.toolCallId 与 source.callId 不匹配`));
|
|
236
|
+
}
|
|
237
|
+
if (Array.isArray(message.content) && message.content.some((b) => typeof b === 'object' && b !== null && b.type === 'tool-result')) {
|
|
238
|
+
out.push(violation('E6', loc, `${shape()}:v4 tool/result 不得再含 tool-result 包装块(content 直接是结果数组)`));
|
|
239
|
+
}
|
|
240
|
+
if (message.isError !== undefined && typeof message.isError !== 'boolean') {
|
|
241
|
+
out.push(violation('E6', loc, `${shape()}:isError 若存在必须为布尔`));
|
|
242
|
+
}
|
|
243
|
+
if (record?.error !== undefined && message.isError !== true) {
|
|
244
|
+
out.push(violation('E6', loc, `${shape()}:带 data.error 时 message.isError 必须为 true`));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
|
|
163
250
|
/** replace 操作数精确形状(镜像旧格式 isReplaceOp,lib/index.js:300-303)。 */
|
|
164
251
|
export function isReplaceOp(op) {
|
|
165
252
|
return (
|
|
@@ -861,9 +948,10 @@ export function wireViolations(events, version) {
|
|
|
861
948
|
* @param {Array<{event:object, lineNo?:number}>} events 已展开事件(带 loc 包装)
|
|
862
949
|
* @param {number} version 被检文件版本
|
|
863
950
|
*/
|
|
864
|
-
export function migrationPrecheckViolations(events, version) {
|
|
951
|
+
export function migrationPrecheckViolations(events, version, opts = {}) {
|
|
865
952
|
const out = [];
|
|
866
953
|
if (version !== 0) return out;
|
|
954
|
+
const ignorableWhitelist = resolveIgnorableWhitelist(opts.ignorableWhitelist);
|
|
867
955
|
for (const { event, lineNo } of events) {
|
|
868
956
|
if (!event || typeof event !== 'object') continue;
|
|
869
957
|
if (event.type === 'subagent/descriptor') {
|
|
@@ -876,8 +964,13 @@ export function migrationPrecheckViolations(events, version) {
|
|
|
876
964
|
if (typeof event.type === 'string' && !V0_EVENT_TYPES.has(event.type)) {
|
|
877
965
|
// 官方对"unknown historical event"一律拒(原文 even when ignorable);E3 的 ignorable
|
|
878
966
|
// 豁免只作用于**读取路径**,这里不改它,只在迁移维度表达。
|
|
967
|
+
// 0.3.19(G4):把"是否声明可忽略 + 是否已登记"作为**机读字段**带出——事实预检(G2)
|
|
968
|
+
// 保留不变,但 migrationVerdict 据此把"已登记的可忽略事件"移入 acceptedDrops(不阻断)。
|
|
969
|
+
const ignorable = event.ignorable === true;
|
|
970
|
+
const ignorableRegistered = ignorable && isIgnorableRegistered(event.type, ignorableWhitelist);
|
|
879
971
|
out.push(violation('G2', { seq: event.seq, lineNo, eventType: event.type },
|
|
880
|
-
`type "${event.type}" 不在官方 v0 dispositions 内${
|
|
972
|
+
`type "${event.type}" 不在官方 v0 dispositions 内${ignorable ? '(即使 ignorable:true)' : ''}—— 官方迁移会拒:format v0 contains unknown historical event type`,
|
|
973
|
+
{ ignorable, ignorableRegistered }));
|
|
881
974
|
}
|
|
882
975
|
}
|
|
883
976
|
// ── G3 · session/title 系列 messageSeqs(官方 v0→v1 `assertTitleSources`,:2543-2556)──
|
|
@@ -918,3 +1011,325 @@ export function migrationPrecheckViolations(events, version) {
|
|
|
918
1011
|
}
|
|
919
1012
|
return out;
|
|
920
1013
|
}
|
|
1014
|
+
|
|
1015
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1016
|
+
// 0.3.19 · v4 契约补充最小集(外部评审一手实测,2026-10-20)
|
|
1017
|
+
// 5 条硬规则:S10 / I2 / P5 / G4 / A1。判据只新增/收严,不改既有规则的判定。
|
|
1018
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* S10 —— `sourceEventSeqs` 元素类型硬断言(评审最小集 ②)。
|
|
1022
|
+
*
|
|
1023
|
+
* 实测(外部评审一手):化石里存在嵌套 `[a,b]`,`v4test`/投影层/`restore` **全部静默
|
|
1024
|
+
* 放过** ⇒ 契约层必须判红。但 `[a,b]` 本身是 v3+ 的**官方存储形态**(`encodeSeqRanges`
|
|
1025
|
+
* 把连续 ≥3 的 run 压成闭区间对;本机 760 份真实 v3/v4 里 265 处,全部可被官方
|
|
1026
|
+
* `decodeSeqRanges` 展开)⇒ 不能把合法的区间对当违规。判红点收严为两层:
|
|
1027
|
+
* ① **存储形态**(`rawSourceEventSeqViolations`,看 `log.rows[].value`):元素只能是
|
|
1028
|
+
* 非负安全整数或 `[start,end]`(start ≤ end),且必须能被官方 `decodeSeqRanges`
|
|
1029
|
+
* 解出且严格递增;否则 S10 error(解码层失败 = 投影/restore 会把它当 seq)。
|
|
1030
|
+
* ② **解码形态**(`sourceEventSeqElementViolations`,看已归一事件):解码后仍出现
|
|
1031
|
+
* 数组/非整数 = S10 error。
|
|
1032
|
+
* 覆盖面比 S6 宽:**所有事件**(含非 surface 类型与 `ignorable` 未知插件事件)都判——
|
|
1033
|
+
* 旧实现里非 surface 事件的 `sourceEventSeqs` 只在 S2 里被顺带提一句,`ignorable` 未知
|
|
1034
|
+
* 事件连 S2 都豁免 ⇒ 这类事件上的坏 provenance 是真空区。
|
|
1035
|
+
*
|
|
1036
|
+
* @param {Array<{event:object,lineNo?:number}>} events 已归一(区间已展开)的事件
|
|
1037
|
+
* @returns S10 违规列表
|
|
1038
|
+
*/
|
|
1039
|
+
export function sourceEventSeqElementViolations(events) {
|
|
1040
|
+
const out = [];
|
|
1041
|
+
for (const { event, lineNo } of events ?? []) {
|
|
1042
|
+
if (!event || typeof event !== 'object') continue;
|
|
1043
|
+
const src = event.sourceEventSeqs;
|
|
1044
|
+
if (src === undefined) continue;
|
|
1045
|
+
const loc = { seq: event.seq, lineNo, eventType: event.type };
|
|
1046
|
+
if (!Array.isArray(src)) {
|
|
1047
|
+
out.push(violation('S10', loc, `sourceEventSeqs 必须是数组(实际 ${typeof src})——元素必须是非负整数`));
|
|
1048
|
+
continue;
|
|
1049
|
+
}
|
|
1050
|
+
for (let i = 0; i < src.length; i++) {
|
|
1051
|
+
const v = src[i];
|
|
1052
|
+
if (isSafeInt(v)) continue;
|
|
1053
|
+
if (Array.isArray(v)) {
|
|
1054
|
+
out.push(violation('S10', loc, `sourceEventSeqs[${i}] 解码后仍是嵌套数组 ${JSON.stringify(v).slice(0, 80)}——元素必须是非负整数(投影/restore/重写路径会把它静默当成 seq;实测 v4test 放过)`));
|
|
1055
|
+
} else {
|
|
1056
|
+
out.push(violation('S10', loc, `sourceEventSeqs[${i}] 不是非负安全整数(${String(v)},${typeof v})——元素必须是非负整数`));
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
return out;
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
/** 单个元素是否为官方区间对 `[start,end]`(`decodeSeqRanges` 的存储形态,start ≤ end)。 */
|
|
1064
|
+
export function isSeqRangePair(value) {
|
|
1065
|
+
return Array.isArray(value) && value.length === 2 && isSafeInt(value[0]) && isSafeInt(value[1]) && value[0] <= value[1];
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
/**
|
|
1069
|
+
* S10 · 存储形态层(见上)。只读 `loadSessionLog()` 的 `rows`(`value` = JSON.parse 原文)。
|
|
1070
|
+
* @param {Array<{lineNo:number,value:object|null}>} rows
|
|
1071
|
+
* @returns S10 违规列表
|
|
1072
|
+
*/
|
|
1073
|
+
export function rawSourceEventSeqViolations(rows) {
|
|
1074
|
+
const out = [];
|
|
1075
|
+
for (const row of rows ?? []) {
|
|
1076
|
+
const value = row?.value;
|
|
1077
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) continue;
|
|
1078
|
+
const src = value.sourceEventSeqs;
|
|
1079
|
+
if (src === undefined) continue;
|
|
1080
|
+
const loc = { seq: Number.isSafeInteger(value.seq) ? value.seq : null, lineNo: row.lineNo, eventType: value.type };
|
|
1081
|
+
if (!Array.isArray(src)) {
|
|
1082
|
+
out.push(violation('S10', loc, `sourceEventSeqs 必须是数组(实际 ${typeof src})`));
|
|
1083
|
+
continue;
|
|
1084
|
+
}
|
|
1085
|
+
let bad = null;
|
|
1086
|
+
for (let i = 0; i < src.length; i++) {
|
|
1087
|
+
const v = src[i];
|
|
1088
|
+
if (isSafeInt(v) || isSeqRangePair(v)) continue;
|
|
1089
|
+
bad = { i, v };
|
|
1090
|
+
break;
|
|
1091
|
+
}
|
|
1092
|
+
if (bad !== null) {
|
|
1093
|
+
const shape = Array.isArray(bad.v) ? `嵌套数组 ${JSON.stringify(bad.v).slice(0, 80)}` : `${String(bad.v)}(${typeof bad.v})`;
|
|
1094
|
+
out.push(violation('S10', loc, `sourceEventSeqs[${bad.i}] 存储形态非法:${shape}——只允许非负安全整数或官方区间对 [start,end](start ≤ end)`));
|
|
1095
|
+
continue;
|
|
1096
|
+
}
|
|
1097
|
+
// 形态合法但官方解码器解不出(区间对逆序 / 越界 / 非严格递增)⇒ 同样是契约违规。
|
|
1098
|
+
try {
|
|
1099
|
+
decodeSeqRanges(src);
|
|
1100
|
+
} catch (err) {
|
|
1101
|
+
out.push(violation('S10', loc, `sourceEventSeqs 无法按官方区间编码解码:${err.message}——投影/restore 会把它静默当成 seq`));
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
return out;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* I2 —— `seedLength` 语义钉死 + `parentSession ∧ seedLength∈{缺失,0}` 合法(评审最小集 ③)。
|
|
1109
|
+
*
|
|
1110
|
+
* 一手依据(`@deepseek-ai/dsh-session-format-v0-to-v1@0.2.0-rc.2`):
|
|
1111
|
+
* - `package/lib/index.js:1707` `seedLength` → `inheritedEventCount`(缺省 0);
|
|
1112
|
+
* - `:1720` `isSeeded = record["seedLength"] !== undefined`(是否 seeded **只由字段是否存在决定**);
|
|
1113
|
+
* - `:1517-1520` `inheritedEventCount > events.length` → "Session inheritedEventCount exceeds its event count";`!isSeeded && inheritedEventCount !== 0` → "unseeded Session inheritedEventCount must be 0"。
|
|
1114
|
+
*
|
|
1115
|
+
* 结论(不再猜):
|
|
1116
|
+
* - **`parentSession` 有值 ≠ seeded**:血缘关系与继承计数是两件事。本机 712/760 份真实
|
|
1117
|
+
* v3/v4 头带 `parentSession` 且**无** `seedLength`(`isSeeded:false`)仍正常加载 ⇒
|
|
1118
|
+
* `parentSession ∧ seedLength∈{缺失,0}` 判 **合法**(不告警、不拒)。
|
|
1119
|
+
* - `seedLength` 超过**展开后**事件数 ⇒ error(本机 16 份 v0 化石命中:153113/531084/683896
|
|
1120
|
+
* vs 34841/12973/20268 —— 这些值明显是字节数/其它口径,官方迁移必拒)。
|
|
1121
|
+
* - v4 运行时头**禁** `seedLength`(dsh-session@0.2.0-rc.2 lib/index.js:1040)⇒ v4 文件带该字段 error。
|
|
1122
|
+
*
|
|
1123
|
+
* @param {object|null} header
|
|
1124
|
+
* @param {Array<object>} events 展开后事件(chunk 行已展开),用于比较继承条数
|
|
1125
|
+
* @param {number} version 被检文件版本
|
|
1126
|
+
* @returns I2 违规列表
|
|
1127
|
+
*/
|
|
1128
|
+
export function seedLengthViolations(header, events, version) {
|
|
1129
|
+
const out = [];
|
|
1130
|
+
if (!header || typeof header !== 'object') return out;
|
|
1131
|
+
const has = Object.hasOwn(header, 'seedLength');
|
|
1132
|
+
if (version >= 4) {
|
|
1133
|
+
if (has) {
|
|
1134
|
+
out.push(violation('I2', {}, `v4 会话头不得带 seedLength(实际 ${String(header.seedLength)})——宿主 header 校验直接拒:"session header has invalid field \\"seedLength\\""(dsh-session@0.2.0-rc.2 lib/index.js:1040);v4 用 isSeeded + 构造器 inheritedEventCount,不落盘该字段`));
|
|
1135
|
+
}
|
|
1136
|
+
return out;
|
|
1137
|
+
}
|
|
1138
|
+
if (!has) return out;
|
|
1139
|
+
const seedLength = header.seedLength;
|
|
1140
|
+
if (!isSafeInt(seedLength)) {
|
|
1141
|
+
out.push(violation('I2', {}, `header.seedLength 必须是非负安全整数(实际 ${String(seedLength)})——语义 = 继承事件条数(官方 inheritedEventCount),不是字节数`));
|
|
1142
|
+
return out;
|
|
1143
|
+
}
|
|
1144
|
+
const count = Array.isArray(events) ? events.length : 0;
|
|
1145
|
+
if (seedLength > count) {
|
|
1146
|
+
out.push(violation('I2', {}, `header.seedLength=${seedLength} 大于展开后事件数 ${count}——seedLength 语义是**继承事件条数**(官方 inheritedEventCount),不是字节数/行数;官方解码/迁移会抛 "Session inheritedEventCount exceeds its event count"(dsh-session-format-v0-to-v1@0.2.0-rc.2 :1517-1520)`));
|
|
1147
|
+
}
|
|
1148
|
+
return out;
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
// ── P5 · carrier 两段配对(评审最小集 ④)─────────────────────────────────
|
|
1152
|
+
|
|
1153
|
+
/** 第 1 段(审计段):`compaction/prune` + `data.shadowedRange`/`shadowedSeqs`(插件两段载体)。 */
|
|
1154
|
+
export function isCarrierAuditSegment(event) {
|
|
1155
|
+
if (!event || event.type !== 'compaction/prune') return false;
|
|
1156
|
+
const data = event.data;
|
|
1157
|
+
if (!data || typeof data !== 'object') return false;
|
|
1158
|
+
const range = data.shadowedRange;
|
|
1159
|
+
return typeof range === 'object' && range !== null && isSafeInt(range.start) && isSafeInt(range.end) && Array.isArray(data.shadowedSeqs);
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/** 从 surfaceOp 取 replace 的 [start,end](按文件版本择路:v3+ startSeq/endSeq)。 */
|
|
1163
|
+
export function surfaceReplaceRange(op, version) {
|
|
1164
|
+
if (!op || typeof op !== 'object' || op.op !== 'replace') return null;
|
|
1165
|
+
if (version >= 3) {
|
|
1166
|
+
return isSafeInt(op.startSeq) && isSafeInt(op.endSeq) ? { start: op.startSeq, end: op.endSeq } : null;
|
|
1167
|
+
}
|
|
1168
|
+
return isSafeInt(op.start) && isSafeInt(op.end) ? { start: op.start, end: op.end } : null;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
/** 审计段与载体的区间是否一致(配对的结构证据)。 */
|
|
1172
|
+
export function carrierRangesMatch(audit, carrier, version) {
|
|
1173
|
+
const range = audit?.data?.shadowedRange;
|
|
1174
|
+
const op = surfaceReplaceRange(carrier?.surfaceOp, version);
|
|
1175
|
+
return Boolean(range && op && range.start === op.start && range.end === op.end);
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
/**
|
|
1179
|
+
* P5 —— carrier 两段配对(评审最小集 ④;读侧)。
|
|
1180
|
+
*
|
|
1181
|
+
* 现役写法(实测 888/888 真实载体):`sourceEventSeqs = [审计段 seq, ...被遮蔽节点]`;
|
|
1182
|
+
* 第 2 段 = 紧随审计段的 surface replace。规则:
|
|
1183
|
+
* - 审计段 seq 在 **首元素** ⇒ 通过(现役);
|
|
1184
|
+
* - 在 **末元素** ⇒ 通过(旧顺序,读侧容错),给 warning(不静默);
|
|
1185
|
+
* - 在中间位 / 载体紧邻审计段却**完全没引用**它 ⇒ error(配对失败 / 孤儿审计段)。
|
|
1186
|
+
* 官方 compaction 的 `compaction/summary` 引用是另一形状(`[compaction/start, summary, …]`),
|
|
1187
|
+
* 审计段类型限定 `compaction/prune` 天然不误报(本机 15 例实测)。
|
|
1188
|
+
*
|
|
1189
|
+
* @param {Array<{event:object,lineNo?:number}>} events 行序事件
|
|
1190
|
+
* @param {{version?:number}} [opts] 被检文件版本(决定 replace 字段名)
|
|
1191
|
+
* @returns P5 违规列表
|
|
1192
|
+
*/
|
|
1193
|
+
export function carrierPairingViolations(events, { version = 0 } = {}) {
|
|
1194
|
+
const out = [];
|
|
1195
|
+
const list = events ?? [];
|
|
1196
|
+
const audits = new Map();
|
|
1197
|
+
for (const { event, lineNo } of list) {
|
|
1198
|
+
if (isCarrierAuditSegment(event) && isSafeInt(event.seq)) audits.set(event.seq, { event, lineNo });
|
|
1199
|
+
}
|
|
1200
|
+
if (audits.size === 0) return out;
|
|
1201
|
+
for (let idx = 0; idx < list.length; idx++) {
|
|
1202
|
+
const { event, lineNo } = list[idx];
|
|
1203
|
+
if (!event || !event.surfaceOp || typeof event.surfaceOp !== 'object' || event.surfaceOp.op !== 'replace') continue;
|
|
1204
|
+
const loc = { seq: event.seq, lineNo, eventType: event.type };
|
|
1205
|
+
const src = event.sourceEventSeqs;
|
|
1206
|
+
if (!Array.isArray(src)) continue; // S6/S10 负责
|
|
1207
|
+
const refs = [];
|
|
1208
|
+
for (let i = 0; i < src.length; i++) {
|
|
1209
|
+
const audit = audits.get(src[i]);
|
|
1210
|
+
if (audit && src[i] < event.seq) refs.push({ audit, pos: i });
|
|
1211
|
+
}
|
|
1212
|
+
// 读侧**两种写法都认**(外部评审要求;两种都在真实语料里出现过):
|
|
1213
|
+
// ① 现役:`[审计段 seq, ...被遮蔽节点]`(审计段 seq 在首位);
|
|
1214
|
+
// ② 早期:`[区间起点, ...被遮蔽节点]`(**不引用**审计段 seq;"区间起点"= replace 范围
|
|
1215
|
+
// 起点:实测真实语料 1,519 个载体即此写法)。
|
|
1216
|
+
// 两种都不报;其余(审计段 seq 在中间位、或首元素既非审计段也非区间起点)= 配对失败。
|
|
1217
|
+
const range = surfaceReplaceRange(event.surfaceOp, version);
|
|
1218
|
+
const legacyStartFirst = range !== null && src[0] === range.start;
|
|
1219
|
+
if (refs.length > 0) {
|
|
1220
|
+
for (const { audit, pos } of refs) {
|
|
1221
|
+
if (pos === 0 || legacyStartFirst) continue; // 现役 / 早期写法
|
|
1222
|
+
out.push(violation('P5', loc, `carrier 两段配对:审计段 seq ${audit.event.seq} 出现在 sourceEventSeqs 第 ${pos} 位,且首元素既不是审计段 seq 也不是区间起点(${String(src[0])} ≠ ${String(range?.start)})——读侧只认两种写法:[审计段, ...](现役)或 [区间起点, ...](早期)`));
|
|
1223
|
+
}
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
const prev = idx > 0 ? list[idx - 1] : undefined;
|
|
1227
|
+
if (prev && isCarrierAuditSegment(prev.event) && carrierRangesMatch(prev.event, event, version)) {
|
|
1228
|
+
if (legacyStartFirst) continue; // 早期写法:区间起点开头、不引用审计段 ⇒ 读侧容错
|
|
1229
|
+
out.push(violation('P5', loc, `carrier 两段配对失败(孤儿审计段):紧邻的审计段 seq ${prev.event.seq}(compaction/prune,区间 ${prev.event.data.shadowedRange.start}..${prev.event.data.shadowedRange.end})既未被本载体 sourceEventSeqs 引用,首元素也不是区间起点(${String(src[0])} ≠ ${prev.event.data.shadowedRange.start})——第 2 段首元素必须是第 1 段 seq(现役)或区间起点(早期写法)`));
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
return out;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
/**
|
|
1236
|
+
* P5 —— 写侧(现役写法严格校验)。`events` = 已存在事件 + 拟写事件(拟写事件在最后)。
|
|
1237
|
+
* 判据:拟写载体若紧邻审计段,`sourceEventSeqs[0]` 必须等于该审计段 seq;任何被引用的
|
|
1238
|
+
* 审计段 seq 都必须出现在首元素。
|
|
1239
|
+
* @param {Array<object>} events 完整事件列表(拟写事件在末尾)
|
|
1240
|
+
* @param {{version?:number}} [opts]
|
|
1241
|
+
* @returns P5 违规列表(error 级 = 写入被拒)
|
|
1242
|
+
*/
|
|
1243
|
+
export function carrierWriteViolations(events, { version = 3 } = {}) {
|
|
1244
|
+
const out = [];
|
|
1245
|
+
const list = events ?? [];
|
|
1246
|
+
if (list.length === 0) return out;
|
|
1247
|
+
const candidate = list[list.length - 1];
|
|
1248
|
+
if (!candidate || !candidate.surfaceOp || typeof candidate.surfaceOp !== 'object' || candidate.surfaceOp.op !== 'replace') return out;
|
|
1249
|
+
const src = candidate.sourceEventSeqs;
|
|
1250
|
+
if (!Array.isArray(src)) return out; // S6/S10 负责
|
|
1251
|
+
const loc = { seq: candidate.seq, lineNo: null, eventType: candidate.type };
|
|
1252
|
+
const audits = new Map();
|
|
1253
|
+
for (const event of list) if (isCarrierAuditSegment(event) && isSafeInt(event.seq)) audits.set(event.seq, event);
|
|
1254
|
+
// ① 紧邻审计段(最强的配对证据):首元素必须就是它。
|
|
1255
|
+
const prev = list[list.length - 2];
|
|
1256
|
+
if (isCarrierAuditSegment(prev) && carrierRangesMatch(prev, candidate, version)) {
|
|
1257
|
+
if (src[0] !== prev.seq) {
|
|
1258
|
+
out.push(violation('P5', loc, `carrier 两段配对(写侧):紧邻审计段 seq ${prev.seq}(compaction/prune),第 2 段 sourceEventSeqs 首元素必须是它(实际首元素 ${String(src[0])})——写入被拒`));
|
|
1259
|
+
}
|
|
1260
|
+
return out;
|
|
1261
|
+
}
|
|
1262
|
+
// ② 其余情况:任何被引用的审计段 seq 都必须在首元素(写侧严格)。
|
|
1263
|
+
for (let i = 1; i < src.length; i++) {
|
|
1264
|
+
const audit = audits.get(src[i]);
|
|
1265
|
+
if (audit && src[i] < candidate.seq) {
|
|
1266
|
+
out.push(violation('P5', loc, `carrier 两段配对(写侧):审计段 seq ${audit.seq} 出现在 sourceEventSeqs 第 ${i} 位,但现役写法要求首元素 = 第 1 段 seq——写入被拒`));
|
|
1267
|
+
break;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
return out;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
// ── G4 · 未知历史事件声明式可忽略 + 白名单(评审最小集 ①)────────────────
|
|
1274
|
+
|
|
1275
|
+
/** 默认可忽略消费者白名单(与 E7 的已知插件前缀对齐;可被 opts/env 注入扩展)。 */
|
|
1276
|
+
export const DEFAULT_IGNORABLE_WHITELIST = Object.freeze(['retrace/', 'message-editor/']);
|
|
1277
|
+
|
|
1278
|
+
/**
|
|
1279
|
+
* 解析注入的白名单:数组,元素为精确 type 或以 `/` 结尾的前缀。
|
|
1280
|
+
* @param {string[]|undefined} extra
|
|
1281
|
+
* @returns {readonly string[]}
|
|
1282
|
+
*/
|
|
1283
|
+
export function resolveIgnorableWhitelist(extra) {
|
|
1284
|
+
if (!Array.isArray(extra) || extra.length === 0) return DEFAULT_IGNORABLE_WHITELIST;
|
|
1285
|
+
return Object.freeze([...DEFAULT_IGNORABLE_WHITELIST, ...extra.filter((x) => typeof x === 'string' && x !== '')]);
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/** 未知 type 是否已被登记为"可忽略消费者"。 */
|
|
1289
|
+
export function isIgnorableRegistered(type, whitelist = DEFAULT_IGNORABLE_WHITELIST) {
|
|
1290
|
+
if (typeof type !== 'string' || type === '') return false;
|
|
1291
|
+
for (const entry of whitelist ?? []) {
|
|
1292
|
+
if (typeof entry !== 'string' || entry === '') continue;
|
|
1293
|
+
if (entry.endsWith('/')) {
|
|
1294
|
+
if (type.startsWith(entry)) return true;
|
|
1295
|
+
} else if (type === entry) {
|
|
1296
|
+
return true;
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
return false;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
/**
|
|
1303
|
+
* G4 —— 未知历史事件的"声明式可忽略"策略(**迁移维度**,仅 v0 源文件)。
|
|
1304
|
+
*
|
|
1305
|
+
* 规则(评审原文):`未知类型 ∧ ignorable:true ⇒ 允许按声明丢弃并登记`(⛔ 不静默)。
|
|
1306
|
+
* - **已登记**(白名单命中)⇒ **不报违规**,进 `registered`(acceptedDrop)——迁移不再因它硬拒;
|
|
1307
|
+
* - **未登记** ⇒ G4 warning(仍阻断迁移:⛔ 不静默),提示登记到白名单;
|
|
1308
|
+
* - 未知且**无** ignorable ⇒ 不归本条(E3 读路径 error;G2 迁移事实预检)。
|
|
1309
|
+
*
|
|
1310
|
+
* 只作用于 v0(G 层是 v0→v1 迁移预检维度);v3/v4 的 ignorable 语义由 E3 表达,不改。
|
|
1311
|
+
*
|
|
1312
|
+
* @param {Array<{event:object,lineNo?:number}>} events
|
|
1313
|
+
* @param {number} version 被检文件版本
|
|
1314
|
+
* @param {{ignorableWhitelist?:string[]}} [opts]
|
|
1315
|
+
* @returns {{violations:Array, registered:Array, unregistered:Array}}
|
|
1316
|
+
*/
|
|
1317
|
+
export function declarativeIgnorableViolations(events, version, { ignorableWhitelist } = {}) {
|
|
1318
|
+
const whitelist = resolveIgnorableWhitelist(ignorableWhitelist);
|
|
1319
|
+
const registered = [];
|
|
1320
|
+
const unregistered = [];
|
|
1321
|
+
const violations = [];
|
|
1322
|
+
if (version !== 0) return { violations, registered, unregistered };
|
|
1323
|
+
for (const { event, lineNo } of events ?? []) {
|
|
1324
|
+
if (typeof event?.type !== 'string' || event.ignorable !== true) continue;
|
|
1325
|
+
if (V0_EVENT_TYPES.has(event.type)) continue; // 已知历史事件,不属本条
|
|
1326
|
+
const loc = { seq: event.seq, lineNo, eventType: event.type };
|
|
1327
|
+
if (isIgnorableRegistered(event.type, whitelist)) {
|
|
1328
|
+
registered.push({ type: event.type, seq: typeof event.seq === 'number' ? event.seq : null, lineNo: typeof lineNo === 'number' ? lineNo : null });
|
|
1329
|
+
continue;
|
|
1330
|
+
}
|
|
1331
|
+
unregistered.push({ type: event.type, seq: typeof event.seq === 'number' ? event.seq : null, lineNo: typeof lineNo === 'number' ? lineNo : null });
|
|
1332
|
+
violations.push(violation('G4', loc, `type "${event.type}" 是未知历史事件且带 ignorable:true,但不在可忽略白名单——按声明丢弃会被静默吞掉(⛔ 不静默);登记到 opts.ignorableWhitelist / DSH_LOG_CONTRACT_IGNORABLE_WHITELIST(精确 type 或以 / 结尾的前缀)后才移出迁移阻断`));
|
|
1333
|
+
}
|
|
1334
|
+
return { violations, registered, unregistered };
|
|
1335
|
+
}
|
package/lib/contracts.js
CHANGED
|
@@ -21,6 +21,7 @@ export const LAYER = {
|
|
|
21
21
|
CONCURRENCY: 'concurrency', // 并发/写入者假设
|
|
22
22
|
FRAMING: 'framing', // zstd 帧结构
|
|
23
23
|
MIGRATION: 'migration', // 迁移预检层:官方 v0/v1/v2 → 当前格式的升级路径会不会拒(**独立维度**)
|
|
24
|
+
ARCHIVE: 'archive', // 归档层:外置内容的**结构化指针**能否在位还原(**独立维度**:要归档根目录才能评估)
|
|
24
25
|
};
|
|
25
26
|
|
|
26
27
|
export const SEVERITY = { ERROR: 'error', WARNING: 'warning', INFO: 'info' };
|
|
@@ -75,8 +76,8 @@ export const CONTRACT_RULES = [
|
|
|
75
76
|
title: 'header 版本与必填字段',
|
|
76
77
|
layer: LAYER.PERSISTENCE,
|
|
77
78
|
severity: SEVERITY.ERROR,
|
|
78
|
-
source: '@deepseek-ai/dsh-session lib/index.js:1110-1125;已知格式版本 0/1/2/3(App 2.0.9 内置 v0→v1→v2→v3 迁移,SESSION_FORMAT_VERSION=3)',
|
|
79
|
-
description: 'header.version 必须为已知受支持版本(0/1/2/3);未知版本报 H2。id 为字符串;createdAt 为非负安全整数;cwd
|
|
79
|
+
source: '@deepseek-ai/dsh-session lib/index.js:1110-1125;已知格式版本 0/1/2/3/4(App 2.0.9 内置 v0→v1→v2→v3 迁移,SESSION_FORMAT_VERSION=3;DSH 0.2.x 追加 v3→v4,SESSION_FORMAT_VERSION=4)',
|
|
80
|
+
description: 'header.version 必须为已知受支持版本(0/1/2/3/4);未知版本报 H2。id 为字符串;createdAt 为非负安全整数;cwd 若存在必须为绝对路径(POSIX 与 Windows 盘符/UNC 形态均接受);origin 只能为 "subagent"。',
|
|
80
81
|
},
|
|
81
82
|
|
|
82
83
|
// ── R · 存储行 ──────────────────────────────────────────────────────────
|
|
@@ -132,6 +133,14 @@ export const CONTRACT_RULES = [
|
|
|
132
133
|
source: '2026-08-28 实锤(某真实会话):文件物理序出现回退(大→小→更大);单进程 appendCore 断言 seq==cursor+i 且按 id 串行化不可能写出',
|
|
133
134
|
description: '按文件物理行序要求展开后事件 seq 严格单调递增。E2 在排序后检查(loadSessionLog 会 sort),物理序倒退被掩盖;S9 在排序前按行序检查,非单调 = 多写入者/旧光标回放交织的直接现场证据,加载会被拒。',
|
|
134
135
|
},
|
|
136
|
+
{
|
|
137
|
+
id: 'S10',
|
|
138
|
+
title: 'sourceEventSeqs 元素必须是(解码后的)非负整数;存储形态只允许官方区间对',
|
|
139
|
+
layer: LAYER.PERSISTENCE,
|
|
140
|
+
severity: SEVERITY.ERROR,
|
|
141
|
+
source: '@deepseek-ai/dsh-session@0.2.0-rc.2 lib/index.js:963-999(encodeSeqRanges:连续 ≥3 才压缩成 `[start,end]`,即 v3+ JSONL 的存储形态)、:1000-1026(decodeSeqRanges/assertSeq:元素必须是非负安全整数,区间对只允许 `[start,end]` 且 start ≤ end、解码后严格递增)',
|
|
142
|
+
description: '所有事件的 `sourceEventSeqs`(**含非 surface 类型与 ignorable 未知插件事件**)原始存储形态只允许两种元素:非负安全整数、或官方区间对 `[start,end]`;经官方 `decodeSeqRanges` 解码后必须全部是非负安全整数。解码后仍为数组/非整数 = 契约违规(投影层/restore/重写路径会把它静默当成 seq,实测 v4test 即如此)⇒ 判红并给出该事件的 seq/行号。注意:`[a,b]` 本身是 v3+ 的**合法存储形态**(本机 760 份真实会话里 265 处,全部可解码),不得误报;判红点是「解码后仍非整数」与「非官方区间对形态」。',
|
|
143
|
+
},
|
|
135
144
|
{
|
|
136
145
|
id: 'I1',
|
|
137
146
|
sourceVerified: false,
|
|
@@ -141,6 +150,14 @@ export const CONTRACT_RULES = [
|
|
|
141
150
|
source: '@deepseek-ai/dsh-agent lib/types/inbox.js:155-178 (apply/validate);2026-08-28 实锤(某两个真实会话):fork 边界 removedCount=1 孤儿',
|
|
142
151
|
description: '从 header.seedLength 起重放 agent/inbox/spliced,next-turn/next-step 双队列;start+removedCount 不得超过队列长、不得产生重复 pending id。fork 时"移除父待处理提示词"的 splice 假设父会话 inbox,子会话 seed 相对空 inbox 上非法 → resume 被拒(invalid persisted inbox splice)。',
|
|
143
152
|
},
|
|
153
|
+
{
|
|
154
|
+
id: 'I2',
|
|
155
|
+
title: 'seedLength = 继承事件条数(不是字节数);parentSession ∧ seedLength∈{缺失,0} 合法',
|
|
156
|
+
layer: LAYER.PERSISTENCE,
|
|
157
|
+
severity: SEVERITY.ERROR,
|
|
158
|
+
source: '@deepseek-ai/dsh-session-format-v0-to-v1@0.2.0-rc.2 package/lib/index.js:1707(`seedLength` → `inheritedEventCount`,缺省 0)、:1720(`isSeeded = seedLength !== undefined`)、:1517-1520("Session inheritedEventCount exceeds its event count" / "unseeded Session inheritedEventCount must be 0");v4 运行时头禁该字段:dsh-session@0.2.0-rc.2 lib/index.js:1040("session header has invalid field \\"seedLength\\"")',
|
|
159
|
+
description: '`header.seedLength` 的语义被钉死为**继承事件条数**(官方 `inheritedEventCount`),不是字节数、不是行数。判据:① 若存在必须是非负安全整数;② `seedLength > 展开后事件数` ⇒ error(官方解码/迁移直接抛 "inheritedEventCount exceeds its event count");③ 文件版本 ≥4 时该字段必须不存在(v4 运行时 header 校验拒)。**`parentSession` 有值 ∧ `seedLength` 缺失或 0 ⇒ 合法**:`parentSession` 只记血缘,是否 seeded 由 `seedLength` 字段是否存在决定(`isSeeded = seedLength !== undefined`;只有 unseeded 才要求 inheritedEventCount === 0)——实测本机 712/760 份真实 v3/v4 头带 `parentSession` 且无 `seedLength`(`isSeeded:false`)仍正常加载,不能判违规。真实读数(本机 29 份 v0 化石):19 份带 `seedLength`(15 万/53 万/68 万/28 万/67 万量级),**全部 ≤ 展开后事件数**(约 69 万/59 万/72 万/39 万/317 万条),而同一文件的明文体积是 20.3–105.4 MB ⇒ `seedLength` 既不可能是字节数(差 1–2 个数量级),也不可能是行数(行数仅 1.3–7 万)⇒ 真实数据与"继承事件条数"一致,本机命中 0 例 `> 展开事件数`(判红分支是防"当字节数用"的护栏)。',
|
|
160
|
+
},
|
|
144
161
|
{
|
|
145
162
|
id: 'E3',
|
|
146
163
|
title: 'type 必须在已知词汇表内(或带 ignorable 标记)',
|
|
@@ -172,8 +189,8 @@ export const CONTRACT_RULES = [
|
|
|
172
189
|
title: '消息类事件消息形状',
|
|
173
190
|
layer: LAYER.PERSISTENCE,
|
|
174
191
|
severity: SEVERITY.ERROR,
|
|
175
|
-
source: '@deepseek-ai/dsh-session lib/index.js:1242-1266 (assertMessageEventShape)',
|
|
176
|
-
description: 'user/message、assistant/message、tool/result 必须携带具名 message(非空 id、正确 role、合法 source、content 数组;assistant 需 model source,tool/result 需 tool source 且 toolCallId 匹配)。',
|
|
192
|
+
source: '@deepseek-ai/dsh-session lib/index.js:1242-1266 (assertMessageEventShape);v4:dsh-session@0.2.0-rc.2 src/index.ts:331-381',
|
|
193
|
+
description: 'user/message、assistant/message、tool/result 必须携带具名 message(非空 id、正确 role、合法 source、content 数组;assistant 需 model source,tool/result 需 tool source 且 toolCallId 匹配)。v0–v3:tool/result role 为 user 且恰含一个 tool-result 块;v4:tool/result role 为 tool、不得含 tool-result 包装块、message.toolCallId 等于 source.callId,新增 developer/message(role developer),且任何消息 source.kind 不得为 plugin。',
|
|
177
194
|
},
|
|
178
195
|
|
|
179
196
|
// ── S · surface 不变量(事故核心层)────────────────────────────────────
|
|
@@ -183,7 +200,7 @@ export const CONTRACT_RULES = [
|
|
|
183
200
|
layer: LAYER.PERSISTENCE,
|
|
184
201
|
severity: SEVERITY.ERROR,
|
|
185
202
|
source: '@deepseek-ai/dsh-session lib/index.js:312-317 (surfaceOpOf)',
|
|
186
|
-
description: 'user/message、assistant/message、tool/result
|
|
203
|
+
description: 'user/message、assistant/message、tool/result(v3 起含 system/message,v4 起含 developer/message)是 surface-eligible 类型,缺 surfaceOp 即违反契约。',
|
|
187
204
|
},
|
|
188
205
|
{
|
|
189
206
|
id: 'S2',
|
|
@@ -207,7 +224,7 @@ export const CONTRACT_RULES = [
|
|
|
207
224
|
layer: LAYER.PERSISTENCE,
|
|
208
225
|
severity: SEVERITY.ERROR,
|
|
209
226
|
source: '@deepseek-ai/dsh-session lib/index.js:300-303 (isReplaceOp)、:339-350 (replacementRange)',
|
|
210
|
-
description: 'replace 必须是精确的 {op:"replace", start, end}
|
|
227
|
+
description: 'replace 必须是精确的 {op:"replace", start, end}(v0–v2)或 {op:"replace", startSeq, endSeq}(v3/v4);端点必须存在于当前 surface 节点且 startIdx ≤ endIdx。',
|
|
211
228
|
},
|
|
212
229
|
{
|
|
213
230
|
id: 'S5',
|
|
@@ -307,11 +324,11 @@ export const CONTRACT_RULES = [
|
|
|
307
324
|
},
|
|
308
325
|
{
|
|
309
326
|
id: 'E9',
|
|
310
|
-
title: 'system/message
|
|
327
|
+
title: 'system/message 的 source 契约(v3 plugin / v4 system-prompt)',
|
|
311
328
|
layer: LAYER.PERSISTENCE,
|
|
312
329
|
severity: SEVERITY.ERROR,
|
|
313
|
-
source: '@deepseek-ai/dsh-session@0.1.5-rc.1 lib/index.js:942-944("must have plugin source");角色表 :917-926',
|
|
314
|
-
description: 'v3 新增的 system/message:role 必须为 system,source.kind 必须为 plugin 且 plugin 非空。实测(反例):source.kind=\'user\' 宿主拒、旧契约 0 违规 ⇒ 漏检。',
|
|
330
|
+
source: '@deepseek-ai/dsh-session@0.1.5-rc.1 lib/index.js:942-944("must have plugin source");角色表 :917-926;v4:dsh-session@0.2.0-rc.2 src/index.ts:360-364("must have system-prompt source")',
|
|
331
|
+
description: 'v3 新增的 system/message:role 必须为 system,source.kind 必须为 plugin 且 plugin 非空。实测(反例):source.kind=\'user\' 宿主拒、旧契约 0 违规 ⇒ 漏检。v4(v3→v4 迁移把 @deepseek-ai/dsh-system-prompt 改写为生产者自有 kind):source.kind 必须为 system-prompt。',
|
|
315
332
|
candidate: true,
|
|
316
333
|
},
|
|
317
334
|
{
|
|
@@ -378,6 +395,14 @@ export const CONTRACT_RULES = [
|
|
|
378
395
|
source: 'retrace 插件 lib/client.js:393("event and never a surface node")',
|
|
379
396
|
description: 'marker 的 sourceEventSeqs(= shadowedSeqs,驱动 CSS 隐藏)不得包含 marker 自身 seq——marker 节点由隐藏逻辑跳过,出现在 shadowed 集属于自指语义错误。',
|
|
380
397
|
},
|
|
398
|
+
{
|
|
399
|
+
id: 'P5',
|
|
400
|
+
title: 'carrier 两段配对:第 2 段 sourceEventSeqs 首元素 = 第 1 段(审计段)seq',
|
|
401
|
+
layer: LAYER.PLUGIN,
|
|
402
|
+
severity: SEVERITY.ERROR,
|
|
403
|
+
source: '仓库既有两段载体形状(test/semantics-v4.test.js:108-121:`compaction/prune` 审计段 + `user/message` replace 载体);2026-10-20 本机实测 760 份真实 v3/v4:884 个载体按现役写法(审计段 seq 在 `sourceEventSeqs[0]`,全部 user/message);另有 1,519 个载体按**早期写法**(首元素 = 区间起点、不引用审计段 seq;user/message 1,022 + tool/result 497)⇒ 读侧必须两种都认;对照官方 compaction 的 `compaction/summary` 引用是另一形状(`[compaction/start, compaction/summary, …]`,15 例),不属本条',
|
|
404
|
+
description: '两段载体(第 1 段 = `compaction/prune` 审计段,带 `data.shadowedRange`/`shadowedSeqs`;第 2 段 = 紧随其后的 surface replace 载体)的配对规则:第 2 段 `sourceEventSeqs[0]` 必须等于第 1 段的 seq(现役写法),其余元素覆盖被遮蔽节点。**读侧容错(两种写法都认)**:① 现役 `[审计段 seq, ...被遮蔽节点]`;② 早期 `[区间起点, ...被遮蔽节点]`(不引用审计段 seq——实测真实语料 1,519 个载体即此写法,若判红会全成"孤儿")。只有"审计段 seq 在中间位且首元素既非审计段也非区间起点"、或"紧邻审计段却既不引用它、首元素也不是区间起点"才报 error。**写侧**(prewrite)按现役写法严格校验:拟写载体若紧邻审计段,`sourceEventSeqs[0]` 必须是该审计段 seq,否则拒绝(早期写法在写侧被要求升级)。',
|
|
405
|
+
},
|
|
381
406
|
|
|
382
407
|
// ── C · 并发 / 写入者假设 ───────────────────────────────────────────────
|
|
383
408
|
{
|
|
@@ -473,6 +498,22 @@ export const CONTRACT_RULES = [
|
|
|
473
498
|
source: '@deepseek-ai/dsh-session-format-v0-to-v1@0.1.5-rc.2 lib/index.js:2543-2556(assertTitleSources):`session/title` 的 messageSeqs 为空 ⟺ source.kind === "user";每个被引 seq 必须解析到 `user/message` 且其 `data.source.kind === "user"`,否则 "messageSeqs must cite earlier human user/message events" / "must be empty exactly for a user title"',
|
|
474
499
|
description: '文件版本 0 且事件为 session/title 或 session/title-llm-request:messageSeqs 必须是数组;session/title 的"空数组 ⟺ 用户标题"必须成立;每个被引 seq 必须是更早的 user/message 且 source.kind === "user"。违反 → 官方升级到当前格式时拒绝。**应用面说明**:官方 `assertTitleSources` 与 turn/start 状态机同在 `assertReleasedArtifactRelationships`,由 v1→v2 在**变换后的 v1/v2 artifact** 上调用(`dsh-session-format-v1-to-v2/lib/index.js:104`);对 messageSeqs 这类按 seq 索引 + 类型/source 判定的引用,变换保序保类型 ⇒ 在原始 v0 上判是必要条件的近似,实测 281 真实 v0 上 0 误报。',
|
|
475
500
|
},
|
|
501
|
+
{
|
|
502
|
+
id: 'G4',
|
|
503
|
+
title: '未知历史事件声明式可忽略(ignorable:true + 白名单)——迁移不得硬拒',
|
|
504
|
+
layer: LAYER.MIGRATION,
|
|
505
|
+
severity: SEVERITY.WARNING,
|
|
506
|
+
source: '@deepseek-ai/dsh-session-format-v0-to-v1@0.2.0-rc.2 package/lib/index.js:1513-1535(assertReleasedArtifactCoordinates:`ignorableCurrent` 分支只在 allowLegacySteering=false 时生效;:1531 原文 "migration refuses unknown historical events even when ignorable")、同处 `vocabularyNeutral` 参数("whether unknown event types remain opaque",未知类型按不透明事件保留的策略)',
|
|
507
|
+
description: '未知历史事件(不在本包 vendored v0 词表内)若带信封级 `ignorable:true`,契约层按**声明式可忽略**处置:允许按声明丢弃并登记为 `acceptedDrop`,**不得**让整条迁移硬拒(这正是"每新增一类插件事件就卡死整条迁移"的根因)。消费者白名单可注入:`opts.ignorableWhitelist` / `createPreWriter({ignorableWhitelist})` / 环境变量 `DSH_LOG_CONTRACT_IGNORABLE_WHITELIST`(逗号分隔;元素为精确 type 或以 `/` 结尾的前缀),默认 = 已知插件消费者 `retrace/`、`message-editor/`。**未登记**的未知 ignorable 事件仍报 G4 warning(⛔ 不静默),且保持迁移阻断。G2 仍**原样保留**为"官方 v0→v1 会拒"的事实预检,不放宽;本规则只把"已声明可忽略且已登记"的事件从迁移阻断移入 `acceptedDrops`。实测:本机 2 份真实 v0 化石含 `retrace/goal-marker`(ignorable),默认白名单命中 ⇒ acceptedDrops,迁移不再被它阻断。',
|
|
508
|
+
},
|
|
509
|
+
{
|
|
510
|
+
id: 'A1',
|
|
511
|
+
title: '在位可还原只认结构化归档指针(archiveRef / archive / externalized.*)',
|
|
512
|
+
layer: LAYER.ARCHIVE,
|
|
513
|
+
severity: SEVERITY.ERROR,
|
|
514
|
+
source: '归档指针口径的外部实测(2026-10-20):只认 `archiveRef`/`archive`/`externalized.*`,⛔ 不得从自由文本 grep 路径;两版文本口径实测全错(原版 78.26%、只认绝对路径 2.04%);本机 763 份真实会话实测:结构化指针 273,571 个 → 按(目标,行)去重后 271,538 条记录,**可还原 271,538(100%)**、真实落空 0、行缺失 0、sha 不符 0;同批数据自由文本 `archive 第 N 行` 出现在 144,391 个事件里(口径差近 20 倍)',
|
|
515
|
+
description: '外置内容的"在位可还原"判定**只**从结构化字段取指针:`data.archiveRef` / `data.archive` / `data.externalized.archiveRef` / `data.externalized.archive`(校验元数据取同级 `archiveLine`/`sha256`);⛔ **永不**从 `content`/`pointer`/散文模板等自由文本 grep 路径(实测两版全错:模板/示例会被算成引用,真实路径示例又被误计)。判据:目标文件存在 + `archiveLine` 行存在 + `sha256` 匹配 = 在位可还原;目标缺失/行缺失/sha 不符 = error;绝对路径失效但同名归档在归档根可解析 = `relocated` warning(re-home 未回写)。**按记录去重**:同一事件同时带 `archiveRef`(basename) 与 `archive`(绝对路径) 时按(目标,行)算一条记录——本机 2,033 个双字段指针即此类(其中 `/tmp` 绝对路径已失效者由 `archiveRef` 解析回 history 根,故真实落空 0,而不是把工具限制或旧路径算成内容缺失)。**独立维度**:需要归档根目录才能评估(`opts.archiveRoots` / `check --archive-root DIR`),未提供时结论为 `unverifiable`(不冒充通过,也不产生违规)。启用本维度后,真实缺失/行缺失/sha 不符是 **error**(红判据:在位可还原须 100%),会进 `ok` 与三档结论;`relocated` 只是 warning。',
|
|
516
|
+
},
|
|
476
517
|
];
|
|
477
518
|
|
|
478
519
|
/** 按 id 取规则。 */
|