dsh-log-contract 0.3.41 → 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 +3 -1
- package/README.zh.md +3 -1
- package/docs/RUN-CONTRACT.md +61 -2
- package/docs/interface-versioning.md +16 -4
- package/lib/index.js +1 -1
- package/lib/run-contract.js +116 -2
- package/lib/validate.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -123,8 +123,10 @@ outside `ok` / exit codes. It ships three assertion helpers for real run records
|
|
|
123
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
124
|
| `R-1` | `assertFailureLatched(history)` | a failure is **latched**: after `failed: true`, no later snapshot (recover / reload / cache clear) may write `failed: false` |
|
|
125
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) |
|
|
126
128
|
|
|
127
|
-
`checkRunContract({...})` runs them together and lists the assertions you did not supply under
|
|
129
|
+
`checkRunContract({...})` runs them together (unverifiable results stay `null`) and lists the assertions you did not supply under
|
|
128
130
|
`skipped`; when **none** ran, `ok` is `null` (unverifiable — never "pass"). Usage, positive/negative examples and the open items for R-2b
|
|
129
131
|
(reason field) and R-6 (idempotency key) are in [docs/RUN-CONTRACT.md](docs/RUN-CONTRACT.md).
|
|
130
132
|
|
package/README.zh.md
CHANGED
|
@@ -100,8 +100,10 @@ dsh-retrace 的「日志体检与修复」能力——与 dsh-retrace 一起构
|
|
|
100
100
|
| `R-3` | `assertFailClosed({ required, provided, result })` | 任一必需输入(字段/文件/上下文)缺失 ⇒ 判定必须是**不兼容**且原因点名缺项;缺失永不匹配(判成兼容即红) |
|
|
101
101
|
| `R-1` | `assertFailureLatched(history)` | 失败**置位**:出现 `failed: true` 后,任何后续快照(恢复/重载/清缓存)都不得写回 `failed: false` |
|
|
102
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`(缺前置:键由运行时提供) |
|
|
103
105
|
|
|
104
|
-
`checkRunContract({...})`
|
|
106
|
+
`checkRunContract({...})` 可一起跑(unverifiable 项保持 `null`);未提供的断言列在 `skipped`;**一条都没跑**时 `ok: null`(unverifiable,⛔ 不冒充通过)。用法、正反例,
|
|
105
107
|
以及 R-2b(reason 字段)/ R-6(幂等键)的前置项清单见 [docs/RUN-CONTRACT.md](docs/RUN-CONTRACT.md)。
|
|
106
108
|
|
|
107
109
|
---
|
package/docs/RUN-CONTRACT.md
CHANGED
|
@@ -79,6 +79,64 @@ assertWriteOutcome({ write: { ok: false, error: 'EIO' }, artifact: { complete: t
|
|
|
79
79
|
// ⇒ findings[0].id === 'R-2a'
|
|
80
80
|
```
|
|
81
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
|
+
|
|
82
140
|
## 一起跑
|
|
83
141
|
|
|
84
142
|
```js
|
|
@@ -93,8 +151,9 @@ const report = checkRunContract({ history, write, artifact, required, provided,
|
|
|
93
151
|
|
|
94
152
|
| 候选 | 状态 | 前置项 |
|
|
95
153
|
|---|---|---|
|
|
96
|
-
| **R-2b
|
|
97
|
-
| **R-
|
|
154
|
+
| **R-2b 场景①** 撤回必须带原因 | **已落断言**(`assertWithdrawalHasReason`) | 记录里出现撤回类响应即可判;实现侧没有原因字段时标 unverifiable(`reasonSupported:false`) |
|
|
155
|
+
| **R-2b 场景②** 初始化中不得按空值继续 | 未实现 | 判据:`initialized !== true` 却按空值/不存在继续判断 ⇒ 红(不需新字段);需在初始化窗口的判定点接入 |
|
|
156
|
+
| **R-6** 副作用必须幂等 | **已落接口级断言**(`assertIdempotentSideEffects`) | 天然键已有(tool call id / turn+step / 消息 id);**仍缺**:"重试复用同键 + 第二次返回缓存/重复"的声明(记录不带键 ⇒ unverifiable) |
|
|
98
157
|
|
|
99
158
|
`check --json` 的 `runContract` 段只承载**规则目录**(`rules` + `note`);真实判定由调用方
|
|
100
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` 标记的是**报告结构**(字段集合 / 字段类型 / 字段含义)的版本:
|
|
@@ -85,16 +85,28 @@
|
|
|
85
85
|
| 键 | 形状 | 说明 |
|
|
86
86
|
|---|---|---|
|
|
87
87
|
| `runContract.family` | `"run-contract"` | 报告域(与 `error-expression` / `version-identity` 并列,靠 family 分派) |
|
|
88
|
-
| `runContract.rules` | `[{id,severity,title,layer}]` |
|
|
88
|
+
| `runContract.rules` | `[{id,severity,title,layer}]` | 规则目录(v5 起 5 条):`R-3` 输入缺失⇒不兼容 / `R-1` 失败置位不清零 / `R-2a` 写失败不得标完整 / `R-2b` 撤回必须带消费方可读原因 / `R-6` 副作用幂等(同键只执行一次) |
|
|
89
89
|
| `runContract.note` | `string` | 断言接口入口(`docs/RUN-CONTRACT.md`);真实判定需调用方提供运行记录 |
|
|
90
90
|
|
|
91
91
|
兼容性:**v3 消费者读 v4 报告**忽略未知键 `runContract` 即可;`ok`/`violations`/退出码语义未变。
|
|
92
92
|
断言接口(`assertFailClosed` / `assertFailureLatched` / `assertWriteOutcome` / `checkRunContract`)
|
|
93
93
|
不改变任何既有报告字段,只是"可以调用"的诊断 API。
|
|
94
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 报告忽略新增规则条目即可;既有断言与报告字段语义未变。
|
|
106
|
+
|
|
95
107
|
## 何时升版
|
|
96
108
|
|
|
97
|
-
**不兼容才升**(版本号只增,如 `"
|
|
109
|
+
**不兼容才升**(版本号只增,如 `"4"` → `"5"`):
|
|
98
110
|
|
|
99
111
|
| 变更 | 升版? |
|
|
100
112
|
|---|---|
|
|
@@ -116,7 +128,7 @@
|
|
|
116
128
|
|
|
117
129
|
| 报告域 | 命令 | 版本字段 | 备注 |
|
|
118
130
|
|---|---|---|---|
|
|
119
|
-
| 日志契约(session log contract) | `check --json` | 顶层 `schemaVersion`(当前 `"
|
|
131
|
+
| 日志契约(session log contract) | `check --json` | 顶层 `schemaVersion`(当前 `"5"`) | 本文正文主要讲这一域 |
|
|
120
132
|
| 运行时契约(run-contract) | 断言接口(`docs/RUN-CONTRACT.md`) | `family: "run-contract"`(`check --json` 的 `runContract` 段只给规则目录) | 与日志契约**互不影响**:断言不改 `ok`/退出码 |
|
|
121
133
|
| 错误表达契约(error-expression) | `error-contract --json` | 顶层 `schemaVersion`(当前 `"1"`)+ `family: "error-expression"` | 与日志契约**互不影响**:任一域升版不牵动另一域 |
|
|
122
134
|
|
package/lib/index.js
CHANGED
|
@@ -17,4 +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 } from './run-contract.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';
|
package/lib/run-contract.js
CHANGED
|
@@ -45,6 +45,24 @@ export const RUN_CONTRACT_RULES = [
|
|
|
45
45
|
source: '不可信数据不得当完整数据交付:写失败(含结果未知)⇒ 撤回或标记不可用',
|
|
46
46
|
description: '写入未成功(`write.ok !== true`,含 `ok` 缺省/未知)时,产物不得被标记为完整(`artifact.complete === true` 或 `artifact.status === "complete"`);必须撤回或标 `partial` / `unavailable` / `withdrawn`。接口断言,不绑定具体写入实现。',
|
|
47
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
|
+
},
|
|
48
66
|
];
|
|
49
67
|
|
|
50
68
|
export const RUN_CONTRACT_RULE_BY_ID = Object.freeze(Object.fromEntries(RUN_CONTRACT_RULES.map((r) => [r.id, r])));
|
|
@@ -141,7 +159,93 @@ export function assertWriteOutcome({ write = {}, artifact = {} } = {}) {
|
|
|
141
159
|
}
|
|
142
160
|
|
|
143
161
|
/**
|
|
144
|
-
*
|
|
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` 里可见,⛔ 不冒充通过)。
|
|
145
249
|
*
|
|
146
250
|
* @param {{required?, provided?, result?, history?, write?, artifact?}} input
|
|
147
251
|
* @returns {{family:string, rules:Array, checks:Array, skipped:string[], ok:boolean|null, findings:Array}}
|
|
@@ -166,7 +270,17 @@ export function checkRunContract(input = {}) {
|
|
|
166
270
|
checks.push(r);
|
|
167
271
|
findings.push(...r.findings);
|
|
168
272
|
} else skipped.push('R-3');
|
|
169
|
-
|
|
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;
|
|
170
284
|
return {
|
|
171
285
|
family: RUN_CONTRACT_FAMILY,
|
|
172
286
|
rules: RUN_CONTRACT_RULES.map(({ id, severity, title, layer }) => ({ id, severity, title, layer })),
|
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",
|