dsh-log-contract 0.3.37 → 0.3.39
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 +18 -0
- package/README.zh.md +16 -0
- package/bin/dsh-log-contract.mjs +5 -1
- package/docs/interface-versioning.md +27 -3
- package/lib/identity.js +248 -0
- package/lib/index.js +1 -0
- package/lib/validate.js +3 -2
- package/package.json +3 -8
package/README.md
CHANGED
|
@@ -96,6 +96,24 @@ line-by-line verification against the official Host source (each rule carries it
|
|
|
96
96
|
|
|
97
97
|
---
|
|
98
98
|
|
|
99
|
+
## Identity contract (ID1 / ID2) — "is what runs what you think it is?"
|
|
100
|
+
A **separate report family** (`version-identity`): not counted in the 57 log-contract rules,
|
|
101
|
+
and **not part of `ok` / exit codes** — it answers whether the artifact you run is the one
|
|
102
|
+
the declared version promises.
|
|
103
|
+
|
|
104
|
+
| Rule | What it guards |
|
|
105
|
+
|---|---|
|
|
106
|
+
| `ID1` | **One version, one digest**: the registry (`docs/identity-registry.json`) must not map one version to two different content digests ⇒ red with `file:line` evidence (duplicate entries sharing the same digest do not fire). |
|
|
107
|
+
| `ID2` | **The number is verifiable**: the digest is computed **from the artifact** — runtime identity = `lib/**/*.js` + `bin/**/*.mjs`, per-file sha256 → sorted → sha256 — and bound to `package.json.version`. Unregistered version, mismatched or **forged** digest, or *same version after a code change* ⇒ red. **Doc-only changes keep the digest unchanged ⇒ green** (no noise). |
|
|
108
|
+
|
|
109
|
+
`check --json` exposes it as the `identity` section (`family`, `rules`, `ok`, `version`,
|
|
110
|
+
`digest`, `runtime`, `registry`, `findings[]` with `file:line`, `summary`); `identity.ok` is
|
|
111
|
+
`null` (**unverifiable**, never "pass") when the registry is absent — npm tarballs ship
|
|
112
|
+
`docs/**/*.md` only. After a version bump or a runtime-code change, re-register:
|
|
113
|
+
`pnpm identity:register` (or `node scripts/identity-register.mjs [version]`).
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
99
117
|
## Version support (host / session format)
|
|
100
118
|
**Tested baseline (the versions this rule set was verified against)**:
|
|
101
119
|
|
package/README.zh.md
CHANGED
|
@@ -75,6 +75,22 @@ dsh-retrace 的「日志体检与修复」能力——与 dsh-retrace 一起构
|
|
|
75
75
|
|
|
76
76
|
---
|
|
77
77
|
|
|
78
|
+
## 身份契约(ID1 / ID2)——「跑的东西是不是你以为的那个」
|
|
79
|
+
**独立报告域**(`family: version-identity`):**不并入** 57 条日志契约计数,也**不进** `ok`/退出码——
|
|
80
|
+
它回答"你跑的产物是不是声明版本承诺的那一份"。
|
|
81
|
+
|
|
82
|
+
| 规则 | 守护什么 |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `ID1` | **一物一号**:登记表(`docs/identity-registry.json`)里同一版本不得对应两个不同内容摘要 ⇒ 判红(带 `file:line` 证据;重复登记但摘要相同不报,防噪音)。 |
|
|
85
|
+
| `ID2` | **号可验**:摘要必须由**产物**实算——运行身份 = `lib/**/*.js` + `bin/**/*.mjs` 逐文件 sha256 → 按路径排序后再 sha256——并与 `package.json.version` 绑定。未登记版本、摘要不符或**伪造**、"同号改了代码" ⇒ 判红;**只改文档/测试/机制件 ⇒ 摘要不变 ⇒ 绿**(防噪音)。 |
|
|
86
|
+
|
|
87
|
+
`check --json` 以 `identity` 段输出(`family` / `rules` / `ok` / `version` / `digest` / `runtime` /
|
|
88
|
+
`registry` / `findings[]`(含 `file:line`)/ `summary`);登记表缺失时 `identity.ok = null`
|
|
89
|
+
(**unverifiable**,不冒充通过——npm 包只含 `docs/**/*.md`)。换版本或改运行时代码后重登记:
|
|
90
|
+
`pnpm identity:register`(或 `node scripts/identity-register.mjs [version]`)。
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
78
94
|
## 版本支持(宿主 / 会话格式)
|
|
79
95
|
**测试基线(本规则集验证过的版本)**:
|
|
80
96
|
|
package/bin/dsh-log-contract.mjs
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*/
|
|
15
15
|
import fs from 'node:fs';
|
|
16
16
|
import { delimiter as pathDelimiter, join as pathJoin } from 'node:path';
|
|
17
|
-
import { loadSessionLog, validateSessionLog, resumeVerdict, migrationVerdict, assessmentScope, createPreWriter, repairSession, readSessionHeader, CONTRACT_RULES, ruleById, explainRule, extractToolOutputs, auditToolCalls, hostCapability, HOST_MAX_FILE_VERSION, detectSupport, ERROR_CONTRACT_RULES, checkErrorContract, normalizePath } from '../lib/index.js';
|
|
17
|
+
import { loadSessionLog, validateSessionLog, resumeVerdict, migrationVerdict, assessmentScope, createPreWriter, repairSession, readSessionHeader, CONTRACT_RULES, ruleById, explainRule, extractToolOutputs, auditToolCalls, hostCapability, HOST_MAX_FILE_VERSION, detectSupport, ERROR_CONTRACT_RULES, checkErrorContract, normalizePath, checkIdentity, IDENTITY_RULES, packageRoot } from '../lib/index.js';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* 同步写 fd——`process.stdout.write` 在**管道**下是异步的,紧跟着的
|
|
@@ -339,6 +339,10 @@ function cmdCheck(args) {
|
|
|
339
339
|
// 新增键(additive,同 schemaVersion 下既有键不删/不改义):错误表达契约规则目录。
|
|
340
340
|
// findings 需要源码输入,故只在 `error-contract` 子命令产出;此处只给规则清单。
|
|
341
341
|
payload.errorContract = { family: 'error-expression', rules: ERROR_CONTRACT_RULES.map((r) => ({ id: r.id, severity: r.severity, title: r.title, layer: r.layer, target: r.target, falsePositiveRisk: r.falsePositiveRisk })), note: '静态规则目录;源码扫描用 error-contract 子命令' };
|
|
342
|
+
// IDENTITY 段(schemaVersion 3 · additive):版本同一性(判据 −1:跑的东西是不是你以为的那个)。
|
|
343
|
+
// ⛔ 只给诊断,不进 ok/退出码;登记表缺失(npm 安装包不含该 JSON)⇒ ok:null(unverifiable)。
|
|
344
|
+
const identity = checkIdentity({ root: packageRoot() });
|
|
345
|
+
payload.identity = { ...identity, rules: IDENTITY_RULES.map((r) => ({ id: r.id, severity: r.severity, title: r.title, layer: r.layer })) };
|
|
342
346
|
if (notAssessable) payload.notAssessable = result.notAssessable;
|
|
343
347
|
if (resume) payload.resume = resumeVerdict(result);
|
|
344
348
|
out(JSON.stringify(payload, null, 2) + '\n');
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|---|---|
|
|
11
11
|
| 字段名 | `schemaVersion` |
|
|
12
12
|
| 位置 | `check --json` 报告**顶层**;同值同步进 `validateSessionLog()` 返回对象顶层 |
|
|
13
|
-
| 当前值 | `"
|
|
13
|
+
| 当前值 | `"3"`(字符串,暂不采用 semver;见下文 v2 / v3) |
|
|
14
14
|
| 覆盖范围 | 仅 `check --json` 这一份报告结构。`prewrite --json` 是**另一份结构**,暂不带本字段(当前 CLI 亦无 `plan` 子命令) |
|
|
15
15
|
|
|
16
16
|
`schemaVersion` 标记的是**报告结构**(字段集合 / 字段类型 / 字段含义)的版本:
|
|
@@ -53,9 +53,33 @@
|
|
|
53
53
|
"未分类诊断",按原 S9 文案处理。
|
|
54
54
|
- 分类枚举后续只增(新取值按 unknown 降级);新增取值**不**再单独升版。
|
|
55
55
|
|
|
56
|
+
## v3(本版):IDENTITY 段(版本同一性诊断 · 兼容递增)
|
|
57
|
+
|
|
58
|
+
`schemaVersion` 由 `"2"` → `"3"`。**只增顶层键**:新增 `identity` 段(判据 −1"跑的东西是不是你以为的那个"),
|
|
59
|
+
既有键与 `ok`/退出码**语义不变**(identity 是**独立诊断维度**,不进 `ok`)。
|
|
60
|
+
|
|
61
|
+
| 键 | 形状 | 说明 |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| `identity.family` | `"version-identity"` | 报告域(与 `error-expression` 并列,靠 family 分派) |
|
|
64
|
+
| `identity.rules` | `[{id,severity,title,layer}]` | IDENTITY 规则目录(ID1 一物一号 / ID2 号可验) |
|
|
65
|
+
| `identity.ok` | `true \| false \| null` | `null` = unverifiable(登记表缺失/不可读:npm 安装包不含该文件)——**不冒充通过** |
|
|
66
|
+
| `identity.version` | `string \| null` | `package.json` 声明版本(或调用方注入) |
|
|
67
|
+
| `identity.digest` | `sha256:…` | 由**产物**实算的运行身份摘要(`lib/**/*.js` + `bin/**/*.mjs` 逐文件 sha256 → 排序后再 sha256) |
|
|
68
|
+
| `identity.runtime.files` | `number` | 计入摘要的运行时代码文件数 |
|
|
69
|
+
| `identity.registry` | `{file, present, entries}` | 登记表位置与条目数 |
|
|
70
|
+
| `identity.findings[]` | `[{id,severity,title,file,line,message}]` | 判红项:`ID1`(同一版本两个摘要)/`ID2`(声明↔登记↔实算不一致、未登记、摘要伪造);**带 file:line 证据** |
|
|
71
|
+
| `identity.summary.byRule` | `object` | 按规则计数 |
|
|
72
|
+
|
|
73
|
+
兼容性与边界:
|
|
74
|
+
- **v2 消费者读 v3 报告**:忽略未知键 `identity` 即可;`ok`/`violations`/退出码语义未变。
|
|
75
|
+
- **防噪音**:只改文档/测试/机制件(不改运行时代码)⇒ `identity.digest` 不变 ⇒ 绿;
|
|
76
|
+
只改代码不动版本 ⇒ `ID2` 红("同号改代码"必须换号并重新登记摘要)。
|
|
77
|
+
- 登记表 `docs/identity-registry.json` **不随 npm 包发布**(npm 只打包 `docs/**/*.md`) ⇒ 安装包内 `identity.ok = null`(unverifiable)。
|
|
78
|
+
- 换版本或改运行时代码后**重登记摘要**:`pnpm identity:register`(或 `node scripts/identity-register.mjs [version]`)。登记旧摘要/伪造摘要 ⇒ `ID2` 红(发布前闸,不是可选项)。
|
|
79
|
+
|
|
56
80
|
## 何时升版
|
|
57
81
|
|
|
58
|
-
|
|
82
|
+
**不兼容才升**(版本号只增,如 `"2"` → `"3"`):
|
|
59
83
|
|
|
60
84
|
| 变更 | 升版? |
|
|
61
85
|
|---|---|
|
|
@@ -77,7 +101,7 @@
|
|
|
77
101
|
|
|
78
102
|
| 报告域 | 命令 | 版本字段 | 备注 |
|
|
79
103
|
|---|---|---|---|
|
|
80
|
-
| 日志契约(session log contract) | `check --json` | 顶层 `schemaVersion`(当前 `"
|
|
104
|
+
| 日志契约(session log contract) | `check --json` | 顶层 `schemaVersion`(当前 `"3"`) | 本文正文主要讲这一域 |
|
|
81
105
|
| 错误表达契约(error-expression) | `error-contract --json` | 顶层 `schemaVersion`(当前 `"1"`)+ `family: "error-expression"` | 与日志契约**互不影响**:任一域升版不牵动另一域 |
|
|
82
106
|
|
|
83
107
|
- 两个域的 id 集合可能重名(都有 `E1`…),消费者**必须按 `family` 分派**,⛔ 不要按 id 字面合并。
|
package/lib/identity.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/identity.js —— IDENTITY(版本同一性)契约:判据 −1「跑的东西是不是你以为的那个」
|
|
3
|
+
*
|
|
4
|
+
* 背景:`0.2.3-alpha.1` 全平台启动即死的真实故障 —— vendored 依赖源码更新了,但**版本号没换**
|
|
5
|
+
* (npm 又不许重发同一版本)⇒ 消费方拿到的仍是旧构建,ESM 链接崩。官方只报
|
|
6
|
+
* `released v2 row … has seq gap` 这类下游症状,指不出"病因在版本同一性"。
|
|
7
|
+
*
|
|
8
|
+
* 两条规则(家族 `version-identity`,与日志契约家族靠 `family` 区分):
|
|
9
|
+
* ID1 一物一号:登记表里每个 `版本 → 内容摘要` 必须唯一;同一版本登记两个不同摘要 ⇒ 红。
|
|
10
|
+
* ID2 号可验:发布前摘要必须**从产物计算**并与声明版本绑定(声明↔登记不一致 ⇒ 红);
|
|
11
|
+
* **防噪音**:只改文档/不改运行时代码/不动号 ⇒ 摘要不变 ⇒ 绿(不误报)。
|
|
12
|
+
* **硬反例**:同号改代码但登记摘要未更新/伪造 ⇒ 红。
|
|
13
|
+
*
|
|
14
|
+
* "产物"口径(明确定义,避免歧义):**运行身份 = 运行时代码清单**(`lib/**\/*.js` + `bin/**\/*.mjs`),
|
|
15
|
+
* 每个文件取 sha256,按相对路径排序后整体再 sha256。文档/测试/机制件**不承载运行身份**,
|
|
16
|
+
* 改它们不改变摘要 —— 这就是"只改文档必须绿"的判据边界。
|
|
17
|
+
*
|
|
18
|
+
* ⛔ 本模块只产出**诊断**(identity 段),不参与 `check` 的 ok/退出码(不改既有判定);
|
|
19
|
+
* 登记表缺失(如 npm 安装包不含该文件)⇒ `ok: null`(unverifiable),不冒充通过、不误报红。
|
|
20
|
+
*/
|
|
21
|
+
import fs from 'node:fs';
|
|
22
|
+
import path from 'node:path';
|
|
23
|
+
import crypto from 'node:crypto';
|
|
24
|
+
import { fileURLToPath } from 'node:url';
|
|
25
|
+
|
|
26
|
+
export const IDENTITY_FAMILY = 'version-identity';
|
|
27
|
+
|
|
28
|
+
/** 登记表(版本 → 内容摘要)。位于 `docs/`(随仓入库;npm 只打包 `docs/**\/*.md` ⇒ 安装包不含本文件,故为 unverifiable)。 */
|
|
29
|
+
export const IDENTITY_REGISTRY = 'docs/identity-registry.json';
|
|
30
|
+
|
|
31
|
+
/** 运行身份口径:只含运行时代码(文档/测试/机制件不计入)。 */
|
|
32
|
+
export const RUNTIME_SOURCES = Object.freeze({ dirs: ['lib', 'bin'], extensions: ['.js', '.mjs'] });
|
|
33
|
+
|
|
34
|
+
export const IDENTITY_RULES = [
|
|
35
|
+
{
|
|
36
|
+
id: 'ID1',
|
|
37
|
+
title: '一物一号:同一版本只能对应一个内容摘要',
|
|
38
|
+
family: IDENTITY_FAMILY,
|
|
39
|
+
layer: 'identity',
|
|
40
|
+
severity: 'error',
|
|
41
|
+
source: '0.2.3-alpha.1 全平台启动即死:vendored 源码更新但版本未换(npm 不许重发)⇒ 消费方拿到旧构建',
|
|
42
|
+
description: '登记表(`docs/identity-registry.json`)里同一个版本出现**两个不同内容摘要** ⇒ 判红(证据带 file:line)。同一版本重复登记但摘要相同不算违规(不产生噪音)。',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
id: 'ID2',
|
|
46
|
+
title: '号可验:摘要必须由产物计算并与声明版本绑定',
|
|
47
|
+
family: IDENTITY_FAMILY,
|
|
48
|
+
layer: 'identity',
|
|
49
|
+
severity: 'error',
|
|
50
|
+
source: '同上;判据:package.json 声明版本 ↔ 登记表登记的产物摘要 ↔ 由运行时代码实算的摘要,三者必须一致',
|
|
51
|
+
description: '发布前从**产物**计算摘要(运行时代码清单 `lib/**/*.js` + `bin/**/*.mjs` 逐文件 sha256 → 排序后再 sha256)并与声明版本绑定:`package.json.version` 在登记表无条目、或登记摘要 ≠ 实算摘要 ⇒ 判红(证据带 file:line);摘要**伪造**同样被实算拦下。**防噪音反例**:只改文档/测试/机制件(不改运行时代码)⇒ 摘要不变 ⇒ 绿;**硬反例**:同号改代码但登记未更新 ⇒ 红。',
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
export const IDENTITY_RULE_BY_ID = Object.freeze(Object.fromEntries(IDENTITY_RULES.map((r) => [r.id, r])));
|
|
56
|
+
|
|
57
|
+
const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex');
|
|
58
|
+
const toRel = (root, file) => path.relative(root, file).split(path.sep).join('/');
|
|
59
|
+
|
|
60
|
+
/** 包根(本模块位于 `<root>/lib/identity.js`)。 */
|
|
61
|
+
export function packageRoot() {
|
|
62
|
+
return path.dirname(path.dirname(fileURLToPath(import.meta.url)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function walkRuntime(root) {
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const dir of RUNTIME_SOURCES.dirs) {
|
|
68
|
+
const abs = path.join(root, dir);
|
|
69
|
+
if (!fs.existsSync(abs)) continue;
|
|
70
|
+
const stack = [abs];
|
|
71
|
+
while (stack.length > 0) {
|
|
72
|
+
const cur = stack.pop();
|
|
73
|
+
for (const name of fs.readdirSync(cur)) {
|
|
74
|
+
const full = path.join(cur, name);
|
|
75
|
+
const st = fs.statSync(full);
|
|
76
|
+
if (st.isDirectory()) stack.push(full);
|
|
77
|
+
else if (RUNTIME_SOURCES.extensions.includes(path.extname(name))) out.push(toRel(root, full));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out.sort();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 计算运行身份摘要(确定性:与目录遍历顺序无关,只依赖"路径 + 文件内容")。
|
|
86
|
+
*
|
|
87
|
+
* @param {{root?: string}} [opts] `root` 缺省 = 本包根(测试可传临时夹具根)
|
|
88
|
+
* @returns {{digest: string, algorithm: string, files: Array<{path: string, sha256: string}>}}
|
|
89
|
+
*/
|
|
90
|
+
export function computeRuntimeDigest({ root } = {}) {
|
|
91
|
+
const base = root ?? packageRoot();
|
|
92
|
+
const files = walkRuntime(base).map((rel) => ({ path: rel, sha256: sha256(fs.readFileSync(path.join(base, rel))) }));
|
|
93
|
+
const manifest = files.map((f) => `${f.path}\u0000${f.sha256}`).join('\n');
|
|
94
|
+
return { digest: `sha256:${sha256(Buffer.from(manifest, 'utf8'))}`, algorithm: 'sha256( sorted(path+sha256(file)) over lib/**/*.js + bin/**/*.mjs )', files };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 文件里第一次出现 `needle` 的行号(1-based;找不到 ⇒ null)。用于 file:line 证据。 */
|
|
98
|
+
export function lineOfOccurrence(file, needle) {
|
|
99
|
+
try {
|
|
100
|
+
const lines = fs.readFileSync(file, 'utf8').split('\n');
|
|
101
|
+
const idx = lines.findIndex((l) => l.includes(needle));
|
|
102
|
+
return idx >= 0 ? idx + 1 : null;
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 读登记表(缺文件/坏 JSON 不抛:分别记为 present:false / parseError)。
|
|
110
|
+
* @param {{root?: string, registryPath?: string}} [opts]
|
|
111
|
+
*/
|
|
112
|
+
export function readIdentityRegistry({ root, registryPath } = {}) {
|
|
113
|
+
const base = root ?? packageRoot();
|
|
114
|
+
const rel = registryPath ?? IDENTITY_REGISTRY;
|
|
115
|
+
const file = path.join(base, rel);
|
|
116
|
+
if (!fs.existsSync(file)) return { present: false, file: rel, entries: [], parseError: null };
|
|
117
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
118
|
+
let data;
|
|
119
|
+
try {
|
|
120
|
+
data = JSON.parse(text);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
return { present: true, file: rel, entries: [], parseError: String(err?.message ?? err) };
|
|
123
|
+
}
|
|
124
|
+
const lines = text.split('\n');
|
|
125
|
+
let cursor = 0;
|
|
126
|
+
const entries = (Array.isArray(data.entries) ? data.entries : []).map((e) => {
|
|
127
|
+
const marker = `"${String(e.version)}"`;
|
|
128
|
+
let line = null;
|
|
129
|
+
for (let i = cursor; i < lines.length; i++) {
|
|
130
|
+
if (lines[i].includes(marker)) { line = i + 1; cursor = i + 1; break; }
|
|
131
|
+
}
|
|
132
|
+
return { version: e.version, digest: e.digest, line };
|
|
133
|
+
});
|
|
134
|
+
return { present: true, file: rel, entries, parseError: null };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const finding = (ruleId, { file, line = null, message, extra = {} }) => {
|
|
138
|
+
const rule = IDENTITY_RULE_BY_ID[ruleId];
|
|
139
|
+
const findingObj = { id: ruleId, rule: ruleId, family: IDENTITY_FAMILY, severity: rule?.severity ?? 'error', title: rule?.title ?? null, file, line, message };
|
|
140
|
+
return Object.keys(extra).length > 0 ? { ...findingObj, extra } : findingObj;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 版本同一性检查(IDENTITY 家族报告;⛔ 不进 check 的 ok/退出码)。
|
|
145
|
+
*
|
|
146
|
+
* @param {{root?: string, version?: string, registryPath?: string}} [opts]
|
|
147
|
+
* @returns {{
|
|
148
|
+
* ok: boolean|null, // null = unverifiable(登记表缺失/损坏,不冒充通过)
|
|
149
|
+
* family: string, version: string|null,
|
|
150
|
+
* digest: string|null, algorithm: string,
|
|
151
|
+
* runtime: {files: number},
|
|
152
|
+
* registry: {file: string, present: boolean, entries: number},
|
|
153
|
+
* findings: Array<object>,
|
|
154
|
+
* summary: {byRule: object},
|
|
155
|
+
* note?: string,
|
|
156
|
+
* }}
|
|
157
|
+
*/
|
|
158
|
+
export function checkIdentity({ root, version, registryPath } = {}) {
|
|
159
|
+
const base = root ?? packageRoot();
|
|
160
|
+
const pkgFile = path.join(base, 'package.json');
|
|
161
|
+
const pkgText = fs.existsSync(pkgFile) ? fs.readFileSync(pkgFile, 'utf8') : null;
|
|
162
|
+
let declared = version ?? null;
|
|
163
|
+
if (declared === null && pkgText !== null) {
|
|
164
|
+
try {
|
|
165
|
+
declared = JSON.parse(pkgText).version ?? null;
|
|
166
|
+
} catch {
|
|
167
|
+
declared = null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const pkgVersionLine = pkgText !== null ? lineOfOccurrence(pkgFile, '"version"') : null;
|
|
171
|
+
const pkgRel = 'package.json';
|
|
172
|
+
const runtime = computeRuntimeDigest({ root: base });
|
|
173
|
+
const registry = readIdentityRegistry({ root: base, registryPath });
|
|
174
|
+
const baseReport = {
|
|
175
|
+
ok: null,
|
|
176
|
+
family: IDENTITY_FAMILY,
|
|
177
|
+
version: declared,
|
|
178
|
+
digest: runtime.digest,
|
|
179
|
+
algorithm: runtime.algorithm,
|
|
180
|
+
runtime: { files: runtime.files.length },
|
|
181
|
+
registry: { file: registry.file, present: registry.present, entries: registry.entries.length },
|
|
182
|
+
findings: [],
|
|
183
|
+
summary: { byRule: {} },
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
if (!registry.present) {
|
|
187
|
+
return { ...baseReport, note: `登记表缺失(${registry.file}):本安装不含登记表(发布包只含运行时代码与 *.md 文档)——身份校验需在源码仓/发布流水线内运行(unverifiable,不冒充通过)` };
|
|
188
|
+
}
|
|
189
|
+
if (registry.parseError) {
|
|
190
|
+
return {
|
|
191
|
+
...baseReport,
|
|
192
|
+
findings: [finding('ID2', { file: registry.file, message: `登记表不是合法 JSON:${registry.parseError}` })],
|
|
193
|
+
ok: false,
|
|
194
|
+
summary: { byRule: { ID2: 1 } },
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const findings = [];
|
|
199
|
+
// ── ID1 · 一物一号:同一版本出现两个不同摘要 ──────────────────────────
|
|
200
|
+
const byVersion = new Map();
|
|
201
|
+
for (const entry of registry.entries) {
|
|
202
|
+
if (!byVersion.has(entry.version)) byVersion.set(entry.version, []);
|
|
203
|
+
byVersion.get(entry.version).push(entry);
|
|
204
|
+
}
|
|
205
|
+
for (const [v, entries] of byVersion) {
|
|
206
|
+
const digests = [...new Set(entries.map((e) => e.digest))];
|
|
207
|
+
if (digests.length <= 1) continue; // 重复但摘要相同:不产生噪音
|
|
208
|
+
const first = entries[0];
|
|
209
|
+
for (const entry of entries.slice(1)) {
|
|
210
|
+
if (entry.digest === first.digest) continue;
|
|
211
|
+
findings.push(finding('ID1', {
|
|
212
|
+
file: registry.file,
|
|
213
|
+
line: entry.line,
|
|
214
|
+
message: `同一版本 ${v} 登记了两个不同内容摘要:${first.digest}(line ${first.line ?? '?'})与 ${entry.digest}(line ${entry.line ?? '?'})——一物多号/一物多内容(判红)`,
|
|
215
|
+
extra: { version: v, digests },
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ── ID2 · 号可验:声明版本 ↔ 登记摘要 ↔ 产物实算摘要 必须一致 ─────────────
|
|
221
|
+
if (declared === null) {
|
|
222
|
+
findings.push(finding('ID2', { file: pkgRel, line: pkgVersionLine, message: 'package.json 无法解析出版本号,无法与登记表绑定(声明↔登记不可验)' }));
|
|
223
|
+
} else {
|
|
224
|
+
const entries = registry.entries.filter((e) => e.version === declared);
|
|
225
|
+
if (entries.length === 0) {
|
|
226
|
+
findings.push(finding('ID2', {
|
|
227
|
+
file: pkgRel,
|
|
228
|
+
line: pkgVersionLine,
|
|
229
|
+
message: `声明版本 ${declared} 在登记表中没有摘要条目——发布前须把由产物实算的摘要登记进去(实算 ${runtime.digest})`,
|
|
230
|
+
extra: { version: declared, computedDigest: runtime.digest },
|
|
231
|
+
}));
|
|
232
|
+
} else {
|
|
233
|
+
const mismatched = entries.filter((e) => e.digest !== runtime.digest);
|
|
234
|
+
if (mismatched.length > 0) {
|
|
235
|
+
findings.push(finding('ID2', {
|
|
236
|
+
file: registry.file,
|
|
237
|
+
line: mismatched[0].line,
|
|
238
|
+
message: `声明版本 ${declared} 的登记摘要 ≠ 产物实算摘要(同号改代码未换号/未更新登记/摘要伪造均命中):登记 ${mismatched[0].digest},实算 ${runtime.digest}(package.json:${pkgVersionLine ?? '?'})`,
|
|
239
|
+
extra: { version: declared, registeredDigest: mismatched[0].digest, computedDigest: runtime.digest },
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const byRule = {};
|
|
246
|
+
for (const f of findings) byRule[f.id] = (byRule[f.id] ?? 0) + 1;
|
|
247
|
+
return { ...baseReport, ok: findings.length === 0, findings, summary: { byRule } };
|
|
248
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -16,3 +16,4 @@ export { tokenMeterViolations, tokenMeterSourceViolations, stepKeyViolations, nu
|
|
|
16
16
|
export { collectArchivePointers, verifyArchivePointers, archiveVerdict, archiveBasename, readArchiveLines, readArchiveIndex, ARCHIVE_POINTER_FIELDS, EXTERNALIZED_POINTER_FIELDS } from './archive.js';
|
|
17
17
|
export { auditToolCalls, extractText, extractToolOutputs, indexToolCalls, toolCommandOf } from './archaeology.js';
|
|
18
18
|
export { ERROR_CONTRACT_RULES, ERROR_CONTRACT_RULE_BY_ID, ERROR_CONTRACT_FAMILY, scanSource, scanInvariantSentences, scanDeclaredKeysNoRead, validateWhitelist, checkErrorContract, normalizePath } from './error-contract.js';
|
|
19
|
+
export { IDENTITY_RULES, IDENTITY_RULE_BY_ID, IDENTITY_FAMILY, IDENTITY_REGISTRY, RUNTIME_SOURCES, checkIdentity, computeRuntimeDigest, readIdentityRegistry, lineOfOccurrence, packageRoot } from './identity.js';
|
package/lib/validate.js
CHANGED
|
@@ -66,11 +66,12 @@ export function isAbsoluteLogPath(value) {
|
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* 报告结构版本(`schemaVersion`):`check --json` / `validateSessionLog()` 返回对象的
|
|
69
|
-
* **结构**版本(字符串,如 `"
|
|
69
|
+
* **结构**版本(字符串,如 `"3"`)。语义:只在报告结构发生不兼容增删/改名时递增,
|
|
70
70
|
* 新增可选字段不递增;消费方可据此做解析兼容判断。
|
|
71
71
|
* ⛔ 不是包版本、不是日志格式版本(那是 header.version / package.json)。
|
|
72
|
+
* 历史:`"1"` 初始 → `"2"` 加 S9 诊断字段(additive)→ `"3"` 加 `identity` 段(additive)。
|
|
72
73
|
*/
|
|
73
|
-
export const REPORT_SCHEMA_VERSION = '
|
|
74
|
+
export const REPORT_SCHEMA_VERSION = '3';
|
|
74
75
|
|
|
75
76
|
/** 宿主能力闸触发时**跳过**的规则(v3/被检版本语义相关)——报告里显式列出,避免"静默通过"。 */
|
|
76
77
|
const VERSION_DEPENDENT_RULES = ['E3', 'E7', 'S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'W1', 'W2', 'T1', 'T2', 'T3', 'T4', 'T5', 'G1', 'G2', 'G4', 'P5'];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-log-contract",
|
|
3
3
|
"description": "日志契约守护 — DSH session log contract guard: offline health check (CLI) + pre-write validation for DeepSeek Harness session logs",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.39",
|
|
5
5
|
"packageManager": "pnpm@11.7.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|
|
@@ -29,14 +29,9 @@
|
|
|
29
29
|
"README.zh.md"
|
|
30
30
|
],
|
|
31
31
|
"scripts": {
|
|
32
|
-
"check": "node scripts/
|
|
32
|
+
"check": "node scripts/gen-contracts-doc.mjs --check",
|
|
33
33
|
"test": "vitest run",
|
|
34
|
-
"
|
|
35
|
-
"surface:check": "node scripts/check-external-surface.mjs",
|
|
36
|
-
"review:check": "node scripts/check-review-record.mjs",
|
|
37
|
-
"release:check": "node scripts/check-release-discipline.mjs && node scripts/check-external-surface.mjs && node scripts/check-review-record.mjs",
|
|
38
|
-
"release": "node scripts/release.mjs",
|
|
39
|
-
"gate": "node internal/scripts/gate.mjs"
|
|
34
|
+
"identity:register": "node scripts/identity-register.mjs"
|
|
40
35
|
},
|
|
41
36
|
"keywords": [
|
|
42
37
|
"dsh",
|