dsh-log-contract 0.3.40 → 0.3.42
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 +17 -0
- package/bin/dsh-log-contract.mjs +5 -1
- package/docs/RUN-CONTRACT.md +159 -0
- package/docs/interface-versioning.md +32 -4
- package/lib/index.js +1 -0
- package/lib/run-contract.js +293 -0
- package/lib/validate.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -114,6 +114,24 @@ the declared version promises.
|
|
|
114
114
|
|
|
115
115
|
---
|
|
116
116
|
|
|
117
|
+
## Runtime contract (R-3 / R-1 / R-2a) — "will it silently change while running?"
|
|
118
|
+
A **separate report family** (`run-contract`, exposed as `check --json` → `runContract`), again
|
|
119
|
+
outside `ok` / exit codes. It ships three assertion helpers for real run records:
|
|
120
|
+
|
|
121
|
+
| Rule | Assertion | What it guards |
|
|
122
|
+
|---|---|---|
|
|
123
|
+
| `R-3` | `assertFailClosed({ required, provided, result })` | any missing required input (field / file / context) ⇒ the verdict must be **incompatible**, with a visible reason naming the missing input — a missing input never matches (`fail-open` is red) |
|
|
124
|
+
| `R-1` | `assertFailureLatched(history)` | a failure is **latched**: after `failed: true`, no later snapshot (recover / reload / cache clear) may write `failed: false` |
|
|
125
|
+
| `R-2a` | `assertWriteOutcome({ write, artifact })` | a failed or unknown write must never leave the artifact marked `complete` — withdraw it or mark it `partial` / `unavailable` / `withdrawn` |
|
|
126
|
+
| `R-2b` | `assertWithdrawalHasReason({ withdrawals, reasonField?, reasonSupported? })` | a withdrawal response must carry a **consumer-readable reason** field; a reason that only lives in logs/telemetry is red; implementations without such a field report `ok:null` (unverifiable) |
|
|
127
|
+
| `R-6` | `assertIdempotentSideEffects({ operations, keyField? })` | side effects are grouped by a **stable key**: at most one execution per key, later attempts must be cached or explicitly "duplicate"; records without keys report `ok:null` (precondition: the key is supplied by the runtime) |
|
|
128
|
+
|
|
129
|
+
`checkRunContract({...})` runs them together (unverifiable results stay `null`) and lists the assertions you did not supply under
|
|
130
|
+
`skipped`; when **none** ran, `ok` is `null` (unverifiable — never "pass"). Usage, positive/negative examples and the open items for R-2b
|
|
131
|
+
(reason field) and R-6 (idempotency key) are in [docs/RUN-CONTRACT.md](docs/RUN-CONTRACT.md).
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
117
135
|
## Version support (host / session format)
|
|
118
136
|
**Tested baseline (the versions this rule set was verified against)**:
|
|
119
137
|
|
package/README.zh.md
CHANGED
|
@@ -91,6 +91,23 @@ dsh-retrace 的「日志体检与修复」能力——与 dsh-retrace 一起构
|
|
|
91
91
|
|
|
92
92
|
---
|
|
93
93
|
|
|
94
|
+
## 运行时契约(R-3 / R-1 / R-2a)——「跑起来会不会静默变样」
|
|
95
|
+
**独立报告域**(`family: run-contract`,`check --json` 的 `runContract` 段),同样**不进** `ok`/退出码。
|
|
96
|
+
提供三个断言函数(调用方喂真实运行记录):
|
|
97
|
+
|
|
98
|
+
| 规则 | 断言 | 守护什么 |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| `R-3` | `assertFailClosed({ required, provided, result })` | 任一必需输入(字段/文件/上下文)缺失 ⇒ 判定必须是**不兼容**且原因点名缺项;缺失永不匹配(判成兼容即红) |
|
|
101
|
+
| `R-1` | `assertFailureLatched(history)` | 失败**置位**:出现 `failed: true` 后,任何后续快照(恢复/重载/清缓存)都不得写回 `failed: false` |
|
|
102
|
+
| `R-2a` | `assertWriteOutcome({ write, artifact })` | 写失败或结果未知时,产物不得标 `complete`——必须撤回或标 `partial` / `unavailable` / `withdrawn` |
|
|
103
|
+
| `R-2b` | `assertWithdrawalHasReason({ withdrawals, reasonField?, reasonSupported? })` | 撤回类响应必须带**消费方可读的原因**字段;原因只在日志/遥测旁路 ⇒ 红;实现侧无该字段 ⇒ `ok:null`(unverifiable) |
|
|
104
|
+
| `R-6` | `assertIdempotentSideEffects({ operations, keyField? })` | 按**稳定幂等键**分组:同键副作用执行 ≤1 次,后续必须缓存或显式"重复";记录无键 ⇒ `ok:null`(缺前置:键由运行时提供) |
|
|
105
|
+
|
|
106
|
+
`checkRunContract({...})` 可一起跑(unverifiable 项保持 `null`);未提供的断言列在 `skipped`;**一条都没跑**时 `ok: null`(unverifiable,⛔ 不冒充通过)。用法、正反例,
|
|
107
|
+
以及 R-2b(reason 字段)/ R-6(幂等键)的前置项清单见 [docs/RUN-CONTRACT.md](docs/RUN-CONTRACT.md)。
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
94
111
|
## 版本支持(宿主 / 会话格式)
|
|
95
112
|
**测试基线(本规则集验证过的版本)**:
|
|
96
113
|
|
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, checkIdentity, IDENTITY_RULES, packageRoot } 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, RUN_CONTRACT_RULES } from '../lib/index.js';
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* 同步写 fd——`process.stdout.write` 在**管道**下是异步的,紧跟着的
|
|
@@ -343,6 +343,10 @@ function cmdCheck(args) {
|
|
|
343
343
|
// ⛔ 只给诊断,不进 ok/退出码;登记表缺失(npm 安装包不含该 JSON)⇒ ok:null(unverifiable)。
|
|
344
344
|
const identity = checkIdentity({ root: packageRoot() });
|
|
345
345
|
payload.identity = { ...identity, rules: IDENTITY_RULES.map((r) => ({ id: r.id, severity: r.severity, title: r.title, layer: r.layer })) };
|
|
346
|
+
// RUN CONTRACT 段(schemaVersion 4 · additive):运行时契约规则目录(R-3 / R-1 / R-2a)。
|
|
347
|
+
// 断言接口见 lib/run-contract.js(assertFailClosed / assertFailureLatched / assertWriteOutcome);
|
|
348
|
+
// 本段只给规则清单(真实判定需调用方喂运行记录),⛔ 不进 ok/退出码。
|
|
349
|
+
payload.runContract = { family: 'run-contract', rules: RUN_CONTRACT_RULES.map((r) => ({ id: r.id, severity: r.severity, title: r.title, layer: r.layer })), note: '运行时契约规则目录;断言接口见 docs/RUN-CONTRACT.md(真实判定需调用方提供运行记录)' };
|
|
346
350
|
if (notAssessable) payload.notAssessable = result.notAssessable;
|
|
347
351
|
if (resume) payload.resume = resumeVerdict(result);
|
|
348
352
|
out(JSON.stringify(payload, null, 2) + '\n');
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# 运行时契约(RUN CONTRACT)· R-3 / R-1 / R-2a
|
|
2
|
+
|
|
3
|
+
> 报告域 `family: "run-contract"`:与日志契约(`check --json` 主报告)、错误表达契约
|
|
4
|
+
> (`error-expression`)、版本同一性契约(`version-identity`)并列,按 `family` 分派。
|
|
5
|
+
> 这三条规则只做**断言**(判定函数),不改 `check` 的 `ok` / 退出码语义。
|
|
6
|
+
|
|
7
|
+
一句话:日志契约问"这份日志合法吗",版本同一性问"跑的是不是你以为的那份",
|
|
8
|
+
运行时契约问"**跑起来会不会静默变样**"。
|
|
9
|
+
|
|
10
|
+
## R-3 · 任一输入缺失 ⇒ 判不兼容(fail-closed)
|
|
11
|
+
|
|
12
|
+
**判据**:必需输入(字段 / 文件 / 必需上下文)缺失时,判定必须是**不兼容**(拒绝),
|
|
13
|
+
且拒绝原因**可见**(点出缺了哪一项)。原因可见性按**完整 token** 判定(按非字母数字切词后
|
|
14
|
+
相等),⛔ 不是子串包含:`required:['file']` + `reasons:['profile missing']` **不算**点名。
|
|
15
|
+
缺失永不匹配:声明值、解析值、期望值三者必须完全相等才算匹配;任一缺失 ⇒ 不匹配。
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { assertFailClosed } from 'dsh-log-contract';
|
|
19
|
+
|
|
20
|
+
// ✅ 合规:缺 fileB ⇒ 拒绝,且原因点名 fileB
|
|
21
|
+
assertFailClosed({
|
|
22
|
+
required: ['fieldA', 'fileB'],
|
|
23
|
+
provided: { fieldA: 1 },
|
|
24
|
+
result: { compatible: false, reasons: ['fileB missing'] },
|
|
25
|
+
}).ok === true;
|
|
26
|
+
|
|
27
|
+
// ⛔ 违规:缺 fileB 却判兼容(fail-open)
|
|
28
|
+
assertFailClosed({ required: ['fieldA', 'fileB'], provided: { fieldA: 1 }, result: { compatible: true } });
|
|
29
|
+
// ⇒ findings[0].id === 'R-3'
|
|
30
|
+
|
|
31
|
+
// ⛔ 违规:拒绝原因不可见(reasons 未点名缺项)
|
|
32
|
+
assertFailClosed({ required: ['fileB'], provided: {}, result: { compatible: false, reasons: ['bad input'] } });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`isMissingInput(v)` 的口径:缺键 / `undefined` / `null` / 空白字符串 / `NaN` 都算缺失。
|
|
36
|
+
|
|
37
|
+
## R-1 · 失败一次即置位,恢复不清零
|
|
38
|
+
|
|
39
|
+
**判据**:状态历史一旦出现 `failed: true`,其后任何快照都不得再出现 `failed: false`——
|
|
40
|
+
恢复、重载、清缓存同样禁止清零;状态只能「未失败 → 失败」单向。缺省(未声明)视为
|
|
41
|
+
未清零,只有**显式** `false` 才算违规。
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { assertFailureLatched } from 'dsh-log-contract';
|
|
45
|
+
|
|
46
|
+
// ✅ 合规:失败后恢复,但置位保持 true
|
|
47
|
+
assertFailureLatched([
|
|
48
|
+
{ phase: 'run', failed: false },
|
|
49
|
+
{ phase: 'run', failed: true },
|
|
50
|
+
{ phase: 'recover', failed: true },
|
|
51
|
+
{ phase: 'reload', failed: true },
|
|
52
|
+
]).ok === true;
|
|
53
|
+
|
|
54
|
+
// ⛔ 违规:恢复路径把置位清零
|
|
55
|
+
assertFailureLatched([
|
|
56
|
+
{ phase: 'run', failed: true },
|
|
57
|
+
{ phase: 'reload', failed: false },
|
|
58
|
+
]);
|
|
59
|
+
// ⇒ findings[0].id === 'R-1'(带 index/phase 证据)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## R-2a · 写失败不得把产物标为完整
|
|
63
|
+
|
|
64
|
+
**判据**:写入未成功(`write.ok !== true`,含 `ok` 缺省/结果未知)时,产物不得被标记为
|
|
65
|
+
完整(`artifact.complete === true` 或 `artifact.status === "complete"`);必须撤回或标
|
|
66
|
+
`partial` / `unavailable` / `withdrawn`。接口断言,不绑定具体写入实现。
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import { assertWriteOutcome } from 'dsh-log-contract';
|
|
70
|
+
|
|
71
|
+
// ✅ 合规:写成功 ⇒ 产物可标完整
|
|
72
|
+
assertWriteOutcome({ write: { ok: true }, artifact: { complete: true } }).ok === true;
|
|
73
|
+
|
|
74
|
+
// ✅ 合规:写失败 ⇒ 产物标 partial/unavailable
|
|
75
|
+
assertWriteOutcome({ write: { ok: false, error: 'EIO' }, artifact: { complete: false, status: 'partial' } }).ok === true;
|
|
76
|
+
|
|
77
|
+
// ⛔ 违规:写失败却把产物标完整
|
|
78
|
+
assertWriteOutcome({ write: { ok: false, error: 'EIO' }, artifact: { complete: true } });
|
|
79
|
+
// ⇒ findings[0].id === 'R-2a'
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## R-2b · 撤回必须带消费方可读的原因(场景①)
|
|
83
|
+
|
|
84
|
+
**判据**:撤回类响应(`kind/type` 含 withdraw、或 `withdrawn === true`、或 `status === "withdrawn"`)
|
|
85
|
+
必须携带**消费方可读**的原因字段(默认 `reason`,非空字符串)。原因只出现在 `log`、`metadata`、
|
|
86
|
+
`telemetry` 等**旁路** ⇒ 仍判红——消费方读不到日志里的原因,不等于撤回有原因。
|
|
87
|
+
实现侧**没有**原因字段概念时,传 `reasonSupported: false` ⇒ `ok: null`(unverifiable,⛔ 不冒充通过)。
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
import { assertWithdrawalHasReason } from 'dsh-log-contract';
|
|
91
|
+
|
|
92
|
+
// ✅ 合规:原因在响应本身
|
|
93
|
+
assertWithdrawalHasReason({ withdrawals: [{ kind: 'withdrawal', target: 'x', reason: 'user asked' }] }).ok === true;
|
|
94
|
+
|
|
95
|
+
// ⛔ 违规:没有 reason(原因只在日志旁路)
|
|
96
|
+
assertWithdrawalHasReason({ withdrawals: [{ withdrawn: true, target: 'x', log: 'revoked by policy' }] });
|
|
97
|
+
// ⇒ findings[0].id === 'R-2b'(sideChannels: ['log'])
|
|
98
|
+
|
|
99
|
+
// ⚪ 不可判定:实现侧没有该字段
|
|
100
|
+
assertWithdrawalHasReason({ withdrawals: [{ withdrawn: true }], reasonSupported: false }).ok === null;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
> 场景②("初始化中"被当成空值继续判断)属另一条判据(`initialized !== true` 却继续),本包暂未实现。
|
|
104
|
+
|
|
105
|
+
## R-6 · 副作用必须幂等(同键只执行一次)
|
|
106
|
+
|
|
107
|
+
**核查结论(先查再判)**:运行记录里**已有天然稳定键**——`tool/call` 的 `data.callId`
|
|
108
|
+
被持久化、且由 `tool/result` 的 `data.message.source.callId` 引用(本包 `P3` 配对即用它),
|
|
109
|
+
`turn+step`、消息 `id` 同样稳定。因此 R-6 可落为**接口级可机检**:调用方按稳定键把执行记录
|
|
110
|
+
喂进来,断言「同键副作用执行次数 ≤ 1;第二次必须返回缓存或显式"重复"」。
|
|
111
|
+
**仍缺的前置清单**:
|
|
112
|
+
|
|
113
|
+
| 缺什么 | 谁提供 | 最小补法 |
|
|
114
|
+
|---|---|---|
|
|
115
|
+
| 重试**复用同一幂等键**的声明 | 写入/调用路径(运行时) | 调用的重试沿用首次的键(tool call id 已持久化,直接复用即可) |
|
|
116
|
+
| 第二次执行**返回缓存或显式"重复"**的标记 | 副作用执行器(工具/写入实现) | 记录里写 `effect: "cached" \| "duplicate"`(或 `applied: false`),让"未重复执行"可判 |
|
|
117
|
+
|
|
118
|
+
记录不带键时无法判定 ⇒ `ok: null`(unverifiable,⛔ 不冒充通过)。
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
import { assertIdempotentSideEffects } from 'dsh-log-contract';
|
|
122
|
+
|
|
123
|
+
// ✅ 合规:同键首次执行 + 第二次返回缓存
|
|
124
|
+
assertIdempotentSideEffects({ operations: [
|
|
125
|
+
{ key: 'call-1', effect: 'applied' },
|
|
126
|
+
{ key: 'call-1', effect: 'cached' },
|
|
127
|
+
] }).ok === true;
|
|
128
|
+
|
|
129
|
+
// ⛔ 违规:同键执行两次
|
|
130
|
+
assertIdempotentSideEffects({ operations: [
|
|
131
|
+
{ key: 'call-1', applied: true },
|
|
132
|
+
{ key: 'call-1', applied: true },
|
|
133
|
+
] });
|
|
134
|
+
// ⇒ findings[0].id === 'R-6'
|
|
135
|
+
|
|
136
|
+
// ⚪ 不可判定:记录里没有稳定键(缺前置)
|
|
137
|
+
assertIdempotentSideEffects({ operations: [{ effect: 'applied' }] }).ok === null;
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## 一起跑
|
|
141
|
+
|
|
142
|
+
```js
|
|
143
|
+
import { checkRunContract } from 'dsh-log-contract';
|
|
144
|
+
const report = checkRunContract({ history, write, artifact, required, provided, result });
|
|
145
|
+
// { family: 'run-contract', rules, checks, skipped, ok, findings }
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
未提供输入的断言会列在 `skipped` 里;**一条断言都没跑**时 `ok: null`(unverifiable,⛔ 不冒充通过)。
|
|
149
|
+
|
|
150
|
+
## 前置项(未实现,列出边界)
|
|
151
|
+
|
|
152
|
+
| 候选 | 状态 | 前置项 |
|
|
153
|
+
|---|---|---|
|
|
154
|
+
| **R-2b 场景①** 撤回必须带原因 | **已落断言**(`assertWithdrawalHasReason`) | 记录里出现撤回类响应即可判;实现侧没有原因字段时标 unverifiable(`reasonSupported:false`) |
|
|
155
|
+
| **R-2b 场景②** 初始化中不得按空值继续 | 未实现 | 判据:`initialized !== true` 却按空值/不存在继续判断 ⇒ 红(不需新字段);需在初始化窗口的判定点接入 |
|
|
156
|
+
| **R-6** 副作用必须幂等 | **已落接口级断言**(`assertIdempotentSideEffects`) | 天然键已有(tool call id / turn+step / 消息 id);**仍缺**:"重试复用同键 + 第二次返回缓存/重复"的声明(记录不带键 ⇒ unverifiable) |
|
|
157
|
+
|
|
158
|
+
`check --json` 的 `runContract` 段只承载**规则目录**(`rules` + `note`);真实判定由调用方
|
|
159
|
+
把运行记录喂给上面的断言函数(与 `errorContract` 段同一口径)。
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|---|---|
|
|
11
11
|
| 字段名 | `schemaVersion` |
|
|
12
12
|
| 位置 | `check --json` 报告**顶层**;同值同步进 `validateSessionLog()` 返回对象顶层 |
|
|
13
|
-
| 当前值 | `"
|
|
13
|
+
| 当前值 | `"5"`(字符串,暂不采用 semver;见下文 v2 / v3 / v4 / v5) |
|
|
14
14
|
| 覆盖范围 | 仅 `check --json` 这一份报告结构。`prewrite --json` 是**另一份结构**,暂不带本字段(当前 CLI 亦无 `plan` 子命令) |
|
|
15
15
|
|
|
16
16
|
`schemaVersion` 标记的是**报告结构**(字段集合 / 字段类型 / 字段含义)的版本:
|
|
@@ -75,11 +75,38 @@
|
|
|
75
75
|
- **防噪音**:只改文档/测试/机制件(不改运行时代码)⇒ `identity.digest` 不变 ⇒ 绿;
|
|
76
76
|
只改代码不动版本 ⇒ `ID2` 红("同号改代码"必须换号并重新登记摘要)。
|
|
77
77
|
- 登记表 `docs/identity-registry.json` **不随 npm 包发布**(npm 只打包 `docs/**/*.md`) ⇒ 安装包内 `identity.ok = null`(unverifiable)。
|
|
78
|
-
- 换版本或改运行时代码后**重登记摘要**:`pnpm identity:register`(仅源码仓;npm 包不含 `scripts/` 目录)。登记旧摘要/伪造摘要 ⇒ `ID2`
|
|
78
|
+
- 换版本或改运行时代码后**重登记摘要**:`pnpm identity:register`(仅源码仓;npm 包不含 `scripts/` 目录)。登记旧摘要/伪造摘要 ⇒ `ID2` 红(发布前检查,不是可选项)。
|
|
79
|
+
|
|
80
|
+
## v4(本版):RUN CONTRACT 段(运行时契约 · 兼容递增)
|
|
81
|
+
|
|
82
|
+
`schemaVersion` 由 `"3"` → `"4"`。**只增顶层键**:新增 `runContract` 段(三条断言的规则目录),
|
|
83
|
+
既有键与 `ok`/退出码**语义不变**(真实判定由调用方把运行记录喂给断言函数)。
|
|
84
|
+
|
|
85
|
+
| 键 | 形状 | 说明 |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| `runContract.family` | `"run-contract"` | 报告域(与 `error-expression` / `version-identity` 并列,靠 family 分派) |
|
|
88
|
+
| `runContract.rules` | `[{id,severity,title,layer}]` | 规则目录(v5 起 5 条):`R-3` 输入缺失⇒不兼容 / `R-1` 失败置位不清零 / `R-2a` 写失败不得标完整 / `R-2b` 撤回必须带消费方可读原因 / `R-6` 副作用幂等(同键只执行一次) |
|
|
89
|
+
| `runContract.note` | `string` | 断言接口入口(`docs/RUN-CONTRACT.md`);真实判定需调用方提供运行记录 |
|
|
90
|
+
|
|
91
|
+
兼容性:**v3 消费者读 v4 报告**忽略未知键 `runContract` 即可;`ok`/`violations`/退出码语义未变。
|
|
92
|
+
断言接口(`assertFailClosed` / `assertFailureLatched` / `assertWriteOutcome` / `checkRunContract`)
|
|
93
|
+
不改变任何既有报告字段,只是"可以调用"的诊断 API。
|
|
94
|
+
|
|
95
|
+
## v5(本版):R-2b / R-6 断言(兼容递增)
|
|
96
|
+
|
|
97
|
+
`schemaVersion` 由 `"4"` → `"5"`。**只增内容**:`runContract.rules` 从 3 条扩到 5 条,新增两个断言
|
|
98
|
+
接口;顶层键集合与 `ok`/退出码语义**不变**。
|
|
99
|
+
|
|
100
|
+
| 新增 | 形状 | 说明 |
|
|
101
|
+
|---|---|---|
|
|
102
|
+
| `R-2b` | `assertWithdrawalHasReason({ withdrawals, reasonField?, reasonSupported? })` | 撤回类响应必须带**消费方可读**的原因字段(非空字符串);原因只在 `log`/`metadata`/`telemetry` 旁路 ⇒ 红;实现侧无该字段 ⇒ 传 `reasonSupported:false` ⇒ `ok:null`(unverifiable) |
|
|
103
|
+
| `R-6` | `assertIdempotentSideEffects({ operations, keyField? })` | 按**稳定幂等键**分组:同键副作用执行次数 ≤1,后续尝试必须缓存/显式"重复";结果不可判定 ⇒ 红;记录无稳定键 ⇒ `ok:null`(缺前置) |
|
|
104
|
+
|
|
105
|
+
兼容性:v4 消费者读 v5 报告忽略新增规则条目即可;既有断言与报告字段语义未变。
|
|
79
106
|
|
|
80
107
|
## 何时升版
|
|
81
108
|
|
|
82
|
-
**不兼容才升**(版本号只增,如 `"
|
|
109
|
+
**不兼容才升**(版本号只增,如 `"4"` → `"5"`):
|
|
83
110
|
|
|
84
111
|
| 变更 | 升版? |
|
|
85
112
|
|---|---|
|
|
@@ -101,7 +128,8 @@
|
|
|
101
128
|
|
|
102
129
|
| 报告域 | 命令 | 版本字段 | 备注 |
|
|
103
130
|
|---|---|---|---|
|
|
104
|
-
| 日志契约(session log contract) | `check --json` | 顶层 `schemaVersion`(当前 `"
|
|
131
|
+
| 日志契约(session log contract) | `check --json` | 顶层 `schemaVersion`(当前 `"5"`) | 本文正文主要讲这一域 |
|
|
132
|
+
| 运行时契约(run-contract) | 断言接口(`docs/RUN-CONTRACT.md`) | `family: "run-contract"`(`check --json` 的 `runContract` 段只给规则目录) | 与日志契约**互不影响**:断言不改 `ok`/退出码 |
|
|
105
133
|
| 错误表达契约(error-expression) | `error-contract --json` | 顶层 `schemaVersion`(当前 `"1"`)+ `family: "error-expression"` | 与日志契约**互不影响**:任一域升版不牵动另一域 |
|
|
106
134
|
|
|
107
135
|
- 两个域的 id 集合可能重名(都有 `E1`…),消费者**必须按 `family` 分派**,⛔ 不要按 id 字面合并。
|
package/lib/index.js
CHANGED
|
@@ -17,3 +17,4 @@ export { collectArchivePointers, verifyArchivePointers, archiveVerdict, archiveB
|
|
|
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
19
|
export { IDENTITY_RULES, IDENTITY_RULE_BY_ID, IDENTITY_FAMILY, IDENTITY_REGISTRY, RUNTIME_SOURCES, checkIdentity, computeRuntimeDigest, readIdentityRegistry, lineOfOccurrence, packageRoot } from './identity.js';
|
|
20
|
+
export { RUN_CONTRACT_RULES, RUN_CONTRACT_RULE_BY_ID, RUN_CONTRACT_FAMILY, assertFailClosed, assertFailureLatched, assertWriteOutcome, checkRunContract, isMissingInput, reasonMentions, reasonTokensOf, isWithdrawalLike, assertWithdrawalHasReason, assertIdempotentSideEffects, sideEffectState } from './run-contract.js';
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/run-contract.js —— 运行时契约(family `run-contract`):判据「跑的时候会不会静默变样」
|
|
3
|
+
*
|
|
4
|
+
* 三条可机检规则(断言接口,不绑定具体实现):
|
|
5
|
+
* R-3 任一输入缺失 ⇒ 判不兼容(fail-closed):缺字段/缺文件/缺必需上下文 ⇒ 必须拒绝且原因可见;
|
|
6
|
+
* ⛔ 不得落到"兼容/通过"或静默默认值 —— 「缺失永不匹配」:声明值、解析值、期望值三者必须完全相等。
|
|
7
|
+
* R-1 失败一次即置位(latched),恢复不清零:状态历史只能「未失败 → 失败」单向;任何
|
|
8
|
+
* 恢复/重载/清缓存路径都不得把置位写回 false。
|
|
9
|
+
* R-2a 写失败(含写入结果未知)⇒ 产物不得标记为「完整」:必须撤回或标记不可用。
|
|
10
|
+
*
|
|
11
|
+
* 用法(调用方以**真实运行记录**喂进来;本模块只做判定,不读外部状态):
|
|
12
|
+
* assertFailClosed({ required, provided, result }) → { ok, missing, findings }
|
|
13
|
+
* assertFailureLatched(history) → { ok, latchedAt, findings }
|
|
14
|
+
* assertWriteOutcome({ write, artifact }) → { ok, findings }
|
|
15
|
+
*
|
|
16
|
+
* 全部断言:合规样本 ⇒ `ok: true`;违规样本 ⇒ `ok: false` + 带证据的 finding(逐条可定位)。
|
|
17
|
+
*/
|
|
18
|
+
export const RUN_CONTRACT_FAMILY = 'run-contract';
|
|
19
|
+
|
|
20
|
+
export const RUN_CONTRACT_RULES = [
|
|
21
|
+
{
|
|
22
|
+
id: 'R-3',
|
|
23
|
+
family: RUN_CONTRACT_FAMILY,
|
|
24
|
+
layer: 'run',
|
|
25
|
+
severity: 'error',
|
|
26
|
+
title: '任一输入缺失 ⇒ 判不兼容(fail-closed)',
|
|
27
|
+
source: '与「三重全等、缺失永不匹配」同口径:声明值/解析值/期望值必须完全相等,任一缺失即不匹配',
|
|
28
|
+
description: '必需输入(字段 / 文件 / 上下文)缺失时,判定必须是**不兼容**(拒绝),且拒绝原因必须**可见**(点出缺了哪一项)。⛔ 缺输入却落到"兼容/通过"、或静默取默认值,都判红——缺失永不匹配。',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'R-1',
|
|
32
|
+
family: RUN_CONTRACT_FAMILY,
|
|
33
|
+
layer: 'run',
|
|
34
|
+
severity: 'error',
|
|
35
|
+
title: '失败一次即置位,恢复不清零',
|
|
36
|
+
source: '失败一旦发生即 latched;状态历史单向:未失败 → 失败,不得回退',
|
|
37
|
+
description: '状态历史里一旦出现 `failed: true`,其后任何快照都不得再出现 `failed: false`(恢复/重载/清缓存同样禁止清零)。缺省(未声明)视为"未清零",只有显式 `false` 才算违规。',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: 'R-2a',
|
|
41
|
+
family: RUN_CONTRACT_FAMILY,
|
|
42
|
+
layer: 'run',
|
|
43
|
+
severity: 'error',
|
|
44
|
+
title: '写失败不得把产物标为完整',
|
|
45
|
+
source: '不可信数据不得当完整数据交付:写失败(含结果未知)⇒ 撤回或标记不可用',
|
|
46
|
+
description: '写入未成功(`write.ok !== true`,含 `ok` 缺省/未知)时,产物不得被标记为完整(`artifact.complete === true` 或 `artifact.status === "complete"`);必须撤回或标 `partial` / `unavailable` / `withdrawn`。接口断言,不绑定具体写入实现。',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: 'R-2b',
|
|
50
|
+
family: RUN_CONTRACT_FAMILY,
|
|
51
|
+
layer: 'run',
|
|
52
|
+
severity: 'error',
|
|
53
|
+
title: '撤回必须带消费方可读的原因',
|
|
54
|
+
source: '撤回类响应必须含 reason 字段(不是日志/旁路):原因写进日志或遥测,消费方读不到即等于没有原因',
|
|
55
|
+
description: '撤回类响应(`kind/type` 含 withdraw、或 `withdrawn === true`、或 `status === "withdrawn"`)必须携带**消费方可读**的原因字段(默认 `reason`,非空字符串);原因只出现在 `log` / `metadata` / `telemetry` 等旁路 ⇒ 仍判红。实现侧**没有**原因字段概念时,调用方传 `reasonSupported: false` ⇒ 结果 `ok: null`(unverifiable,⛔ 不冒充通过)。',
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: 'R-6',
|
|
59
|
+
family: RUN_CONTRACT_FAMILY,
|
|
60
|
+
layer: 'run',
|
|
61
|
+
severity: 'error',
|
|
62
|
+
title: '副作用必须幂等(同键只执行一次)',
|
|
63
|
+
source: '同一稳定幂等键的副作用被重复执行 ⇒ 重复写入/重复发送等不可逆后果;第二次必须返回缓存或显式"重复"',
|
|
64
|
+
description: '运行记录按**稳定幂等键**(默认 `key`;运行时可提供 tool call id / turn+step / 消息 id 这类天然键)分组:同一键的副作用**执行次数必须 ≤ 1**;后续尝试必须返回缓存/显式"重复"(`effect/outcome ∈ deduped/cached/duplicate/replayed/skipped/noop`)——执行结果不可判定同样判红。⛔ 记录里没有稳定键 ⇒ `ok: null`(unverifiable,缺前置:键由运行时/调用方提供)。',
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
export const RUN_CONTRACT_RULE_BY_ID = Object.freeze(Object.fromEntries(RUN_CONTRACT_RULES.map((r) => [r.id, r])));
|
|
69
|
+
|
|
70
|
+
/** 输入是否"缺失":缺键 / undefined / null / 空白字符串 / NaN。 */
|
|
71
|
+
export function isMissingInput(value) {
|
|
72
|
+
if (value === undefined || value === null) return true;
|
|
73
|
+
if (typeof value === 'string' && value.trim() === '') return true;
|
|
74
|
+
if (typeof value === 'number' && Number.isNaN(value)) return true;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const finding = (id, message, extra = {}) => {
|
|
79
|
+
const rule = RUN_CONTRACT_RULE_BY_ID[id];
|
|
80
|
+
return { id, rule: id, family: RUN_CONTRACT_FAMILY, severity: rule?.severity ?? 'error', title: rule?.title ?? null, message, ...extra };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* R-3 · 输入缺失必须 fail-closed 且原因可见。
|
|
85
|
+
*
|
|
86
|
+
* @param {{required?: string[], provided?: object, result?: {compatible?: boolean, reasons?: string[]}}} input
|
|
87
|
+
* `required` 必需输入名;`provided` 实际提供值;`result.compatible` 判定结论(true=兼容);
|
|
88
|
+
* `result.reasons` 拒绝原因(必须点名缺失项)。
|
|
89
|
+
* @returns {{id:'R-3', ok:boolean, missing:string[], findings:Array}}
|
|
90
|
+
*/
|
|
91
|
+
/** 按非字母数字切词(`file.b` → ['file','b'];用于完整 token 匹配,避免子串假通过)。 */
|
|
92
|
+
export function reasonTokensOf(text) {
|
|
93
|
+
return String(text ?? '').split(/[^A-Za-z0-9_]+/).filter(Boolean);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 拒绝原因是否**点名**了某输入:完整 token 相等(非子串)。
|
|
98
|
+
* 反例:`required:['file']` + `reasons:['profile missing']` ⇒ 不点名('file' 不是 'profile' 的一部分)。
|
|
99
|
+
*/
|
|
100
|
+
export function reasonMentions(reason, name) {
|
|
101
|
+
const nameTokens = reasonTokensOf(name);
|
|
102
|
+
if (nameTokens.length === 0) return false;
|
|
103
|
+
const reasonTokens = reasonTokensOf(reason);
|
|
104
|
+
return nameTokens.every((t) => reasonTokens.includes(t));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function assertFailClosed({ required = [], provided = {}, result = {} } = {}) {
|
|
108
|
+
const findings = [];
|
|
109
|
+
const missing = required.filter((name) => !Object.hasOwn(provided ?? {}, name) || isMissingInput(provided[name]));
|
|
110
|
+
const compatible = result?.compatible;
|
|
111
|
+
const reasons = Array.isArray(result?.reasons) ? result.reasons : [];
|
|
112
|
+
for (const name of missing) {
|
|
113
|
+
if (compatible !== false) {
|
|
114
|
+
findings.push(finding('R-3', `必需输入 "${name}" 缺失,但判定未拒绝(compatible=${String(compatible)})——缺失必须判不兼容(fail-closed),不得落到兼容/通过或静默默认值`, { input: name, compatible: compatible ?? null }));
|
|
115
|
+
} else if (!reasons.some((r) => typeof r === 'string' && reasonMentions(r, name))) {
|
|
116
|
+
findings.push(finding('R-3', `必需输入 "${name}" 缺失且已拒绝,但拒绝原因不可见(reasons 未以完整 token 点名该输入)——原因必须能定位到缺项`, { input: name }));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { id: 'R-3', ok: findings.length === 0, missing, findings };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* R-1 · 失败置位后不得被清零(恢复/重载/清缓存同样禁止)。
|
|
124
|
+
*
|
|
125
|
+
* @param {Array<{phase?: string, failed?: boolean}>} history 时间序状态快照
|
|
126
|
+
* @returns {{id:'R-1', ok:boolean, latchedAt: {index:number, phase:string|null}|null, findings:Array}}
|
|
127
|
+
*/
|
|
128
|
+
export function assertFailureLatched(history = []) {
|
|
129
|
+
const findings = [];
|
|
130
|
+
let latchedAt = null;
|
|
131
|
+
(Array.isArray(history) ? history : []).forEach((snap, index) => {
|
|
132
|
+
const failed = snap?.failed;
|
|
133
|
+
if (failed === true && latchedAt === null) {
|
|
134
|
+
latchedAt = { index, phase: snap?.phase ?? null };
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (latchedAt !== null && failed === false) {
|
|
138
|
+
findings.push(finding('R-1', `失败在 ${latchedAt.phase ?? `#${latchedAt.index}`} 置位后,第 ${index} 步(phase=${snap?.phase ?? '-'})把置位清零(failed=false)——恢复/重载/清缓存不得清零`, { index, phase: snap?.phase ?? null, latchedAt }));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
return { id: 'R-1', ok: findings.length === 0, latchedAt, findings };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* R-2a · 写失败(含结果未知)⇒ 产物不得标"完整"。
|
|
146
|
+
*
|
|
147
|
+
* @param {{write?: {ok?: boolean, error?: unknown}, artifact?: {complete?: boolean, status?: string}}} input
|
|
148
|
+
* @returns {{id:'R-2a', ok:boolean, findings:Array}}
|
|
149
|
+
*/
|
|
150
|
+
export function assertWriteOutcome({ write = {}, artifact = {} } = {}) {
|
|
151
|
+
const findings = [];
|
|
152
|
+
const writeOk = write?.ok === true;
|
|
153
|
+
const markedComplete = artifact?.complete === true
|
|
154
|
+
|| artifact?.status === 'complete';
|
|
155
|
+
if (!writeOk && markedComplete) {
|
|
156
|
+
findings.push(finding('R-2a', `写入未成功(ok=${String(write?.ok)}${write?.error ? `,error=${String(write.error)}` : ''})但产物被标记为完整(${artifact?.complete === true ? 'complete=true' : `status=${String(artifact?.status)}`})——写失败必须撤回或标记不可用(partial/unavailable/withdrawn)`, { writeOk, artifact }));
|
|
157
|
+
}
|
|
158
|
+
return { id: 'R-2a', ok: findings.length === 0, findings };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 撤回类响应识别(属性口径:看语义标记,不绑定具体实现)。
|
|
163
|
+
* @param {object} item
|
|
164
|
+
* @returns {boolean}
|
|
165
|
+
*/
|
|
166
|
+
export function isWithdrawalLike(item) {
|
|
167
|
+
if (!item || typeof item !== 'object') return false;
|
|
168
|
+
if (item.kind === 'withdrawal' || item.type === 'withdrawal') return true;
|
|
169
|
+
if (item.withdrawal === true || item.withdrawn === true) return true;
|
|
170
|
+
if (item.status === 'withdrawn' || item.status === 'withdrawal') return true;
|
|
171
|
+
if (typeof item.type === 'string' && item.type.includes('withdraw')) return true;
|
|
172
|
+
if (typeof item.kind === 'string' && item.kind.includes('withdraw')) return true;
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* R-2b · 撤回类响应必须带**消费方可读**的原因(不是日志/旁路)。
|
|
178
|
+
*
|
|
179
|
+
* @param {{withdrawals?: object|object[], reasonField?: string, reasonSupported?: boolean}} input
|
|
180
|
+
* `withdrawals` 缺省 ⇒ `ok: null`(无输入,不冒充通过);
|
|
181
|
+
* `reasonSupported === false` ⇒ `ok: null`(实现侧无原因字段概念,按缺前置登记)。
|
|
182
|
+
* @returns {{id:'R-2b', ok:boolean|null, checked:number, findings:Array}}
|
|
183
|
+
*/
|
|
184
|
+
export function assertWithdrawalHasReason({ withdrawals, reasonField = 'reason', reasonSupported } = {}) {
|
|
185
|
+
if (withdrawals === undefined) return { id: 'R-2b', ok: null, checked: 0, skipped: 'no-withdrawals-input', findings: [] };
|
|
186
|
+
if (reasonSupported === false) return { id: 'R-2b', ok: null, checked: 0, skipped: 'implementation-provides-no-reason-field', findings: [] };
|
|
187
|
+
const list = (Array.isArray(withdrawals) ? withdrawals : [withdrawals]).filter(isWithdrawalLike);
|
|
188
|
+
if (list.length === 0) return { id: 'R-2b', ok: null, checked: 0, skipped: 'no-withdrawal-items', findings: [] };
|
|
189
|
+
const findings = [];
|
|
190
|
+
for (const item of list) {
|
|
191
|
+
const reason = item?.[reasonField];
|
|
192
|
+
const readable = typeof reason === 'string' && reason.trim() !== '';
|
|
193
|
+
if (readable) continue;
|
|
194
|
+
const sideChannels = ['log', 'logs', 'metadata', 'telemetry'].filter((k) => item && item[k] !== undefined);
|
|
195
|
+
findings.push(finding('R-2b', `撤回类响应缺消费方可读的 ${reasonField} 字段(原因必须是响应本身的字段,不是日志/旁路)${sideChannels.length > 0 ? `——原因若只在 ${sideChannels.join('/')} 里,消费方读不到` : ''}`, { reasonField, sideChannels }));
|
|
196
|
+
}
|
|
197
|
+
return { id: 'R-2b', ok: findings.length === 0, checked: list.length, findings };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const APPLIED_TAGS = new Set(['applied', 'executed', 'side-effect', 'write', 'committed']);
|
|
201
|
+
const DEDUPED_TAGS = new Set(['deduped', 'cached', 'duplicate', 'replayed', 'skipped', 'noop', 'not-applied', 'ignored']);
|
|
202
|
+
|
|
203
|
+
/** 单次副作用的执行状态:applied / deduped / unknown。 */
|
|
204
|
+
export function sideEffectState(op) {
|
|
205
|
+
if (op?.applied === true) return 'applied';
|
|
206
|
+
if (op?.applied === false) return 'deduped';
|
|
207
|
+
const tag = op?.effect ?? op?.outcome ?? op?.status;
|
|
208
|
+
if (APPLIED_TAGS.has(tag)) return 'applied';
|
|
209
|
+
if (DEDUPED_TAGS.has(tag)) return 'deduped';
|
|
210
|
+
return 'unknown';
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* R-6 · 副作用幂等:同键执行次数 ≤1;后续尝试必须缓存/显式"重复"。
|
|
215
|
+
*
|
|
216
|
+
* @param {{operations?: object|object[], keyField?: string}} input
|
|
217
|
+
* `operations` 缺省或**没有任何稳定键** ⇒ `ok: null`(unverifiable,缺前置)。
|
|
218
|
+
* @returns {{id:'R-6', ok:boolean|null, keys:number, findings:Array}}
|
|
219
|
+
*/
|
|
220
|
+
export function assertIdempotentSideEffects({ operations, keyField = 'key' } = {}) {
|
|
221
|
+
if (operations === undefined) return { id: 'R-6', ok: null, keys: 0, skipped: 'no-operations-input', findings: [] };
|
|
222
|
+
const list = (Array.isArray(operations) ? operations : [operations])
|
|
223
|
+
.filter((op) => op && typeof op[keyField] === 'string' && op[keyField].trim() !== '');
|
|
224
|
+
if (list.length === 0) return { id: 'R-6', ok: null, keys: 0, skipped: 'no-stable-keys', findings: [] };
|
|
225
|
+
const byKey = new Map();
|
|
226
|
+
for (const op of list) {
|
|
227
|
+
const k = op[keyField];
|
|
228
|
+
if (!byKey.has(k)) byKey.set(k, []);
|
|
229
|
+
byKey.get(k).push(op);
|
|
230
|
+
}
|
|
231
|
+
const findings = [];
|
|
232
|
+
for (const [key, ops] of byKey) {
|
|
233
|
+
const applied = ops.filter((op) => sideEffectState(op) === 'applied');
|
|
234
|
+
if (applied.length > 1) {
|
|
235
|
+
findings.push(finding('R-6', `同一幂等键 "${key}" 的副作用被执行了 ${applied.length} 次(应 ≤1)——重复执行不可逆,第二次必须返回缓存或显式"重复"`, { key, applied: applied.length }));
|
|
236
|
+
}
|
|
237
|
+
for (const op of ops) {
|
|
238
|
+
const state = sideEffectState(op);
|
|
239
|
+
if (state === 'unknown') {
|
|
240
|
+
findings.push(finding('R-6', `同一幂等键 "${key}" 的一次执行结果不可判定(effect/outcome=${String(op?.effect ?? op?.outcome ?? op?.status)})——必须显式标 applied 或缓存/重复`, { key }));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return { id: 'R-6', ok: findings.length === 0, keys: byKey.size, findings };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 断言一起跑(调用方按需提供输入;未提供的断言跳过并在 `skipped` 里可见,⛔ 不冒充通过)。
|
|
249
|
+
*
|
|
250
|
+
* @param {{required?, provided?, result?, history?, write?, artifact?}} input
|
|
251
|
+
* @returns {{family:string, rules:Array, checks:Array, skipped:string[], ok:boolean|null, findings:Array}}
|
|
252
|
+
* `ok: null` = **unverifiable**(三条断言一条都没跑:没有任何输入)——⛔ 不冒充通过。
|
|
253
|
+
*/
|
|
254
|
+
export function checkRunContract(input = {}) {
|
|
255
|
+
const checks = [];
|
|
256
|
+
const skipped = [];
|
|
257
|
+
const findings = [];
|
|
258
|
+
if (input.history !== undefined) {
|
|
259
|
+
const r = assertFailureLatched(input.history);
|
|
260
|
+
checks.push(r);
|
|
261
|
+
findings.push(...r.findings);
|
|
262
|
+
} else skipped.push('R-1');
|
|
263
|
+
if (input.write !== undefined || input.artifact !== undefined) {
|
|
264
|
+
const r = assertWriteOutcome({ write: input.write, artifact: input.artifact });
|
|
265
|
+
checks.push(r);
|
|
266
|
+
findings.push(...r.findings);
|
|
267
|
+
} else skipped.push('R-2a');
|
|
268
|
+
if (input.required !== undefined) {
|
|
269
|
+
const r = assertFailClosed({ required: input.required, provided: input.provided, result: input.result });
|
|
270
|
+
checks.push(r);
|
|
271
|
+
findings.push(...r.findings);
|
|
272
|
+
} else skipped.push('R-3');
|
|
273
|
+
if (input.withdrawals !== undefined) {
|
|
274
|
+
const r = assertWithdrawalHasReason({ withdrawals: input.withdrawals, reasonField: input.reasonField, reasonSupported: input.reasonSupported });
|
|
275
|
+
checks.push(r);
|
|
276
|
+
findings.push(...r.findings);
|
|
277
|
+
} else skipped.push('R-2b');
|
|
278
|
+
if (input.operations !== undefined) {
|
|
279
|
+
const r = assertIdempotentSideEffects({ operations: input.operations, keyField: input.keyField });
|
|
280
|
+
checks.push(r);
|
|
281
|
+
findings.push(...r.findings);
|
|
282
|
+
} else skipped.push('R-6');
|
|
283
|
+
const ran = checks.filter((c) => c.ok !== null).length > 0;
|
|
284
|
+
return {
|
|
285
|
+
family: RUN_CONTRACT_FAMILY,
|
|
286
|
+
rules: RUN_CONTRACT_RULES.map(({ id, severity, title, layer }) => ({ id, severity, title, layer })),
|
|
287
|
+
checks: checks.map(({ id, ok, findings: f }) => ({ id, ok, findings: f.length })),
|
|
288
|
+
skipped,
|
|
289
|
+
// 一条都没跑 ⇒ unverifiable(null),⛔ 不冒充通过;跑过 ⇒ 只看已跑断言的结论。
|
|
290
|
+
ok: ran ? findings.length === 0 : null,
|
|
291
|
+
findings,
|
|
292
|
+
};
|
|
293
|
+
}
|
package/lib/validate.js
CHANGED
|
@@ -66,12 +66,12 @@ export function isAbsoluteLogPath(value) {
|
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
68
|
* 报告结构版本(`schemaVersion`):`check --json` / `validateSessionLog()` 返回对象的
|
|
69
|
-
* **结构**版本(字符串,如 `"
|
|
69
|
+
* **结构**版本(字符串,如 `"5"`)。语义:只在报告结构发生不兼容增删/改名时递增,
|
|
70
70
|
* 新增可选字段不递增;消费方可据此做解析兼容判断。
|
|
71
71
|
* ⛔ 不是包版本、不是日志格式版本(那是 header.version / package.json)。
|
|
72
|
-
* 历史:`"1"` 初始 → `"2"` 加 S9 诊断字段(additive)→ `"3"` 加 `identity`
|
|
72
|
+
* 历史:`"1"` 初始 → `"2"` 加 S9 诊断字段(additive)→ `"3"` 加 `identity` 段 → `"4"` 加 `runContract` 段 → `"5"` 加 R-2b / R-6 断言(additive)。
|
|
73
73
|
*/
|
|
74
|
-
export const REPORT_SCHEMA_VERSION = '
|
|
74
|
+
export const REPORT_SCHEMA_VERSION = '5';
|
|
75
75
|
|
|
76
76
|
/** 宿主能力闸触发时**跳过**的规则(v3/被检版本语义相关)——报告里显式列出,避免"静默通过"。 */
|
|
77
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.42",
|
|
5
5
|
"packageManager": "pnpm@11.7.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|