dsh-log-contract 0.3.23 → 0.3.25
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 +5 -22
- package/README.zh.md +7 -16
- 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/contract/rules.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* contract/rules.js
|
|
3
|
-
*
|
|
4
|
-
* 规则描述符(record schema):一条契约规则在注册表里必须具备的形状。
|
|
5
|
-
*
|
|
6
|
-
* 规则描述符是平台无关的数据模型 —— 它只说明「一条规则如何被描述」,
|
|
7
|
-
* 不携带任何平台词汇。平台专属的规则内容由适配器提供,注册表负责汇总。
|
|
8
|
-
*
|
|
9
|
-
* 字段:
|
|
10
|
-
* 必填 id 规则标识(注册表主键,稳定不可复用)
|
|
11
|
-
* title 一句话说明
|
|
12
|
-
* layer 规则所属层(registry.js 的 LAYER)
|
|
13
|
-
* severity 严重度(verdict.js 的 SEVERITY)
|
|
14
|
-
* source 契约出处(人读)
|
|
15
|
-
* description 判定说明(人读)
|
|
16
|
-
* 可选 sourceVerified 出处是否已在目标宿主版本上核对(缺省视为未核对)
|
|
17
|
-
* premiseStale 判定前提是否已失效
|
|
18
|
-
* candidate 是否为候选规则(尚未进入正式判定集)
|
|
19
|
-
* invariant 该规则守护的不变量标识(contract/invariants.js 的 INVARIANTS 值)
|
|
20
|
-
*
|
|
21
|
-
* 描述符形状与校验为契约层自有实现;规则内容(含 invariant 声明)由适配器登记。
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
import { SEVERITY } from './verdict.js';
|
|
25
|
-
|
|
26
|
-
/** 规则描述符的必填字段。 */
|
|
27
|
-
export const RULE_REQUIRED_FIELDS = Object.freeze([
|
|
28
|
-
'id',
|
|
29
|
-
'title',
|
|
30
|
-
'layer',
|
|
31
|
-
'severity',
|
|
32
|
-
'source',
|
|
33
|
-
'description',
|
|
34
|
-
]);
|
|
35
|
-
|
|
36
|
-
/** 规则描述符的可选字段。 */
|
|
37
|
-
export const RULE_OPTIONAL_FIELDS = Object.freeze([
|
|
38
|
-
'sourceVerified',
|
|
39
|
-
'premiseStale',
|
|
40
|
-
'candidate',
|
|
41
|
-
'invariant',
|
|
42
|
-
]);
|
|
43
|
-
|
|
44
|
-
const REQUIRED = new Set(RULE_REQUIRED_FIELDS);
|
|
45
|
-
const KNOWN = new Set([...RULE_REQUIRED_FIELDS, ...RULE_OPTIONAL_FIELDS]);
|
|
46
|
-
const SEVERITIES = new Set(Object.values(SEVERITY));
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* 判断一个对象是否满足规则描述符的必填约束。
|
|
50
|
-
*
|
|
51
|
-
* 只做形状校验(不校验 id 是否与判定实现一一对应):
|
|
52
|
-
* 必填字段齐全、id/title/layer/source 为非空字符串、severity 属于已知枚举、多余字段报 false。
|
|
53
|
-
*
|
|
54
|
-
* @param {unknown} rule
|
|
55
|
-
* @returns {boolean}
|
|
56
|
-
*/
|
|
57
|
-
export function isRuleDescriptor(rule) {
|
|
58
|
-
if (rule === null || typeof rule !== 'object' || Array.isArray(rule)) return false;
|
|
59
|
-
for (const field of REQUIRED) {
|
|
60
|
-
if (!Object.hasOwn(rule, field)) return false;
|
|
61
|
-
}
|
|
62
|
-
for (const field of ['id', 'title', 'layer', 'source', 'description']) {
|
|
63
|
-
if (typeof rule[field] !== 'string' || rule[field] === '') return false;
|
|
64
|
-
}
|
|
65
|
-
if (!SEVERITIES.has(rule.severity)) return false;
|
|
66
|
-
for (const key of Object.keys(rule)) {
|
|
67
|
-
if (!KNOWN.has(key)) return false;
|
|
68
|
-
}
|
|
69
|
-
return true;
|
|
70
|
-
}
|
package/contract/verdict.js
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* contract/verdict.js
|
|
3
|
-
*
|
|
4
|
-
* 平台无关的「判定」模型:一条判定长什么样、严重度如何分级、如何构造。
|
|
5
|
-
*
|
|
6
|
-
* 契约层只描述判定对象的**形状**,不描述任何具体运行时的事件形状:
|
|
7
|
-
*
|
|
8
|
-
* { id, layer, severity, message, seq?, lineNo?, eventType?, source? }
|
|
9
|
-
*
|
|
10
|
-
* - id 规则标识(规则注册表主键)
|
|
11
|
-
* - layer 规则所属层(见 registry.js 的 LAYER)
|
|
12
|
-
* - severity error | warning | info
|
|
13
|
-
* - message 人读说明
|
|
14
|
-
* - source 规则出处(人读;来自注册表)
|
|
15
|
-
* - 定位信息 seq / lineNo / eventType,缺省时补空
|
|
16
|
-
*
|
|
17
|
-
* 规则元数据(severity/layer/source)从注册表按 id 解析:判定构造本身不持有规则内容。
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { ruleById } from './registry.js';
|
|
21
|
-
|
|
22
|
-
/** 判定严重度。 */
|
|
23
|
-
export const SEVERITY = Object.freeze({
|
|
24
|
-
ERROR: 'error', // 违反即不可加载 / 写入被拒(fail-loud)
|
|
25
|
-
WARNING: 'warning', // 合法但可疑
|
|
26
|
-
INFO: 'info', // 事实性观察
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 构造一条判定记录。
|
|
31
|
-
*
|
|
32
|
-
* @param {string} id 规则 id(注册表主键)
|
|
33
|
-
* @param {{seq?: number, lineNo?: number, eventType?: string}|null} eventOrLoc 定位信息
|
|
34
|
-
* @param {string} message 人读说明
|
|
35
|
-
* @param {object} [extra] 追加字段(如规则特有的机器可读读数)
|
|
36
|
-
* @returns {object}
|
|
37
|
-
*/
|
|
38
|
-
export function violation(id, eventOrLoc, message, extra = {}) {
|
|
39
|
-
const rule = ruleById(id);
|
|
40
|
-
const loc = eventOrLoc ?? {};
|
|
41
|
-
return {
|
|
42
|
-
id,
|
|
43
|
-
severity: rule?.severity ?? SEVERITY.ERROR,
|
|
44
|
-
layer: rule?.layer ?? 'unknown',
|
|
45
|
-
seq: typeof loc.seq === 'number' ? loc.seq : null,
|
|
46
|
-
lineNo: typeof loc.lineNo === 'number' ? loc.lineNo : null,
|
|
47
|
-
eventType: loc.eventType ?? null,
|
|
48
|
-
message,
|
|
49
|
-
source: rule?.source ?? null,
|
|
50
|
-
...extra,
|
|
51
|
-
};
|
|
52
|
-
}
|
package/docs/ARCHITECTURE.md
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# Architecture — platform-agnostic contract + platform adapters
|
|
2
|
-
|
|
3
|
-
This package validates session-log contracts. It is split in two layers so that a **new host platform**
|
|
4
|
-
requires only a new adapter, not a rewrite of the contract.
|
|
5
|
-
|
|
6
|
-
```
|
|
7
|
-
adapters/<platform>/ ──depends on──▶ contract/
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
## `contract/` — platform-agnostic
|
|
11
|
-
|
|
12
|
-
| Module | Responsibility |
|
|
13
|
-
|---|---|
|
|
14
|
-
| `verdict.js` | violation/verdict model: id, severity, source citation, message |
|
|
15
|
-
| `invariants.js` | platform-neutral invariants (ordinality, sequence continuity, physical order, lossless payloads, record head, empty containers, in-place restorability, …) |
|
|
16
|
-
| `rules.js` | rule-descriptor schema; every descriptor declares the invariant it enforces |
|
|
17
|
-
| `registry.js` | rule catalog installation and lookup (holds no rule content itself) |
|
|
18
|
-
| `index.js` | the contract-layer public surface |
|
|
19
|
-
|
|
20
|
-
**Hard rule:** `contract/` must contain **zero platform vocabulary** and must not import any adapter or
|
|
21
|
-
implementation module. This is machine-enforced (`scripts/check-core-purity.mjs`), run by
|
|
22
|
-
`pnpm check` and `pnpm check:strict` (also in CI and in `.githooks/pre-push`).
|
|
23
|
-
|
|
24
|
-
## `adapters/<platform>/` — platform-specific
|
|
25
|
-
|
|
26
|
-
For the reference platform (`adapters/dsh/`): event shapes (turns/steps/chunks/tool pairing),
|
|
27
|
-
format versions v0–v4 routing, zstd framing, surface/fold semantics, migration records, host probes,
|
|
28
|
-
and the vocabulary tables. Rule data lives here and is registered into the contract layer at load time.
|
|
29
|
-
|
|
30
|
-
## Adding a platform (current status: in progress)
|
|
31
|
-
|
|
32
|
-
1. Implement an adapter: event shapes, format-version routing, framing, surface operations, vocabulary.
|
|
33
|
-
2. Register its rule catalog through `contract/registry.js`.
|
|
34
|
-
3. Reuse `contract/invariants.js` unchanged; add invariants only if the guarantee is genuinely
|
|
35
|
-
platform-neutral (and every new invariant must be referenced by at least one rule).
|
|
36
|
-
|
|
37
|
-
**Honest status:** the split is extracted and machine-enforced today, but a second adapter is not yet
|
|
38
|
-
drop-in: the contract layer still emits platform rule ids for some invariants, and there is no
|
|
39
|
-
platform-neutral orchestration entry yet. Those are tracked as follow-up work; until then a second
|
|
40
|
-
adapter also needs a thin runner.
|
|
41
|
-
|
|
42
|
-
## Guarantees kept while migrating
|
|
43
|
-
|
|
44
|
-
- Same 51 rules, same verdicts, same exit codes as before the split.
|
|
45
|
-
- Zero regression on real v3 sessions (violation signatures unchanged).
|
|
46
|
-
- `contract/` and `adapters/` ship in the npm package (`files`).
|