@furongjun1999/dsh-memory 0.5.0 → 0.5.1
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 +552 -465
- package/docs/README.md +1 -0
- package/docs/eval/bench_lingshu_self/bench_self.py +140 -0
- package/docs/eval/bench_lingshu_self/self_bench_result.json +404 -0
- package/docs/eval/bench_lingshu_self//347/201/265/346/236/242/350/207/252/345/272/223/347/253/257/345/210/260/347/253/257/346/243/200/347/264/242/345/256/236/346/265/213_v1.0.md +38 -0
- package/docs/eval//344/270/215/345/217/257/351/235/240/346/200/247/350/220/275/345/234/260_P0_v1.0.md +185 -0
- package/docs/eval//346/225/205/351/232/234/346/263/250/345/205/245/345/256/236/346/265/213_v1.0.md +422 -0
- package/docs/eval//347/253/257/345/210/260/347/253/257LoCoMoQA/345/220/214/345/217/243/345/276/204/345/257/271/347/205/247_v1.0.md +100 -0
- package/docs/eval//347/253/257/345/210/260/347/253/257/345/271/262/346/211/260/346/261/240/350/257/204/346/265/213_/347/241/256/345/256/232/346/200/247/350/243/201/345/206/263vsLLM_judge_v1.1.md +197 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v1.md +156 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v10.md +210 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v11.md +227 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v12.md +203 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v13.md +233 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v14.md +191 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v15.md +213 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v16.md +214 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v17.md +199 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v2.md +156 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v3.md +152 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v4.md +128 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v5.md +114 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v6.md +192 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v7.md +187 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v8.md +207 -0
- package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v9.md +203 -0
- package/docs/hive//345/256/211/345/205/250/345/256/241/350/256/241/345/256/236/351/224/232_v0.1.md +202 -0
- package/docs/hive//350/234/202/345/267/242/345/217/214/345/256/236/344/276/213/344/272/222/351/252/214_/350/256/276/350/256/241/345/256/232/347/250/277.md +19 -3
- package/docs/images/lingshu-moonlight-covenant-poster-preview.jpg +0 -0
- package/docs/images/lingshu-moonlight-covenant-poster.png +0 -0
- package/docs/mdcg//345/212/237/350/203/275/350/260/203/347/224/250/346/230/240/345/260/204/350/241/250_v0.1.md +40 -40
- package/docs/theory//344/270/215/345/217/257/351/235/240/345/256/232/347/220/206/344/270/216/345/244/261/346/225/210/344/274/230/345/205/210/346/241/206/346/236/266_v0.3.md +365 -0
- package/docs//344/270/215/345/217/257/351/235/240/346/200/247/347/220/206/350/256/272_v0.1.md +343 -0
- package/lib/bridge.d.ts +9 -0
- package/lib/bridge.js +35 -0
- package/lib/index.js +7 -1
- package/lib/lib/roleplay_web.js +530 -443
- package/lib/lib/token_store.d.ts +7 -1
- package/lib/lib/token_store.js +12 -3
- package/md_cg/audit.py +12 -1
- package/md_cg/backfill.py +16 -15
- package/md_cg/backfill_bucket_zh.py +35 -0
- package/md_cg/bench_e2e_judge.py +532 -0
- package/md_cg/bench_e2e_locomo_qa.py +368 -0
- package/md_cg/bench_e2e_qa.py +256 -0
- package/md_cg/branches.py +18 -2
- package/md_cg/ccgc.py +3 -2
- package/md_cg/chain.py +19 -4
- package/md_cg/consolidate.py +7 -6
- package/md_cg/crosscheck.py +4 -3
- package/md_cg/crypto.py +439 -437
- package/md_cg/datapath.py +395 -335
- package/md_cg/evidence.py +582 -580
- package/md_cg/export.py +3 -1
- package/md_cg/forgetting.py +2 -2
- package/md_cg/fsutil.py +48 -0
- package/md_cg/hotcache.py +255 -238
- package/md_cg/interop.py +161 -22
- package/md_cg/judgment_manifest.py +177 -0
- package/md_cg/links.py +655 -622
- package/md_cg/mcp_server.py +280 -29
- package/md_cg/mdcg.py +616 -125
- package/md_cg/mdcos.py +430 -66
- package/md_cg/mreview/govern.py +5 -4
- package/md_cg/postings.py +4 -2
- package/md_cg/readcache.py +76 -18
- package/md_cg/reconcile.py +228 -0
- package/md_cg/review_cli.py +170 -0
- package/md_cg/routing.py +28 -0
- package/md_cg/run_tests.py +211 -0
- package/md_cg/scrub.py +862 -852
- package/md_cg/security.py +385 -275
- package/md_cg/selfreport.py +3 -2
- package/md_cg/signer.py +565 -562
- package/md_cg/sources.py +3 -2
- package/md_cg/stg.py +6 -0
- package/md_cg/sustain.py +1168 -1138
- package/md_cg/test_access_hints.py +147 -0
- package/md_cg/test_branch_discard_tombstone.py +136 -0
- package/md_cg/test_branches.py +259 -249
- package/md_cg/test_chain_read_isolate.py +168 -0
- package/md_cg/test_datapath_device_name.py +203 -0
- package/md_cg/test_emit_negtail_cache.py +156 -0
- package/md_cg/test_en_pipeline.py +186 -166
- package/md_cg/test_govern_directread.py +421 -0
- package/md_cg/test_i32_hotcache_env_key.py +218 -0
- package/md_cg/test_identity_attribution.py +228 -147
- package/md_cg/test_index_durability.py +238 -224
- package/md_cg/test_interop.py +4 -2
- package/md_cg/test_interop_judgment.py +228 -0
- package/md_cg/test_issue39_utf8_stdio.py +273 -0
- package/md_cg/test_links_concurrent_write.py +188 -0
- package/md_cg/test_merge_upsert.py +168 -0
- package/md_cg/test_n123_derive_expiry_chain.py +205 -0
- package/md_cg/test_n130_verify_falsified_protect.py +185 -0
- package/md_cg/test_n131_merge_gate.py +205 -0
- package/md_cg/test_p1x_ref_root.py +160 -0
- package/md_cg/test_p27_docindex.py +774 -765
- package/md_cg/test_p2_mcp.py +3 -0
- package/md_cg/test_p32_backfill.py +304 -298
- package/md_cg/test_p39_verify_flow.py +90 -50
- package/md_cg/test_p47_session_view.py +60 -25
- package/md_cg/test_propose_tail_index.py +157 -0
- package/md_cg/test_read_scope_b27.py +277 -0
- package/md_cg/test_readcache_default_on.py +168 -0
- package/md_cg/test_readcache_precise_inval.py +270 -0
- package/md_cg/test_readcache_prodpath.py +55 -7
- package/md_cg/test_reconcile_v0.py +294 -0
- package/md_cg/test_retr_s1.py +6 -2
- package/md_cg/test_retr_s1b.py +67 -0
- package/md_cg/test_retr_s7.py +8 -0
- package/md_cg/test_retr_s9_entity_ctx.py +181 -175
- package/md_cg/test_retr_score_once.py +208 -0
- package/md_cg/test_review_onepass.py +170 -0
- package/md_cg/test_rrf_graph_seed_cache.py +154 -0
- package/md_cg/test_security_audit.py +155 -0
- package/md_cg/test_security_audit_b26.py +161 -0
- package/md_cg/test_security_audit_v21.py +250 -0
- package/md_cg/test_semantic_canonical.py +255 -241
- package/md_cg/test_session_isolation.py +168 -0
- package/md_cg/test_snapshot_autoclose.py +187 -0
- package/md_cg/test_tail_watermark_race.py +208 -0
- package/md_cg/test_tenant_env_override_warn.py +139 -0
- package/md_cg/test_tenant_registry_corrupt_warn.py +151 -0
- package/md_cg/test_v14_fixes.py +415 -397
- package/md_cg/test_verify_dirty_reconcile.py +157 -0
- package/md_cg/theory.py +276 -273
- package/md_cg/tokens.py +734 -677
- package/md_cg/units.py +3 -2
- package/md_cg/vision_evidence.py +4 -3
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
- package/md_cg/whitebox_kb/wisdom/multilang_ir.py +128 -124
- package/md_cg/writepipe.py +554 -550
- package/package.json +6 -2
- package/src/bridge.ts +434 -401
- package/src/index.ts +526 -518
- package/src/lib/roleplay_web.ts +1019 -932
- package/src/lib/token_store.ts +202 -192
package/md_cg/theory.py
CHANGED
|
@@ -1,274 +1,277 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""md_cg · 版本层(蜂群互联 · 层0)
|
|
3
|
-
|
|
4
|
-
设计依据:`docs/swarm/蜂群互联_v0.1.md` §2.3 / §3.1 / §10。
|
|
5
|
-
|
|
6
|
-
版本更新权 = 蜂群**唯一核心权限**(严格唯一,仅 designer)。本模块只做
|
|
7
|
-
「声明 + 校验」两件事,不做跨节点证据交换(§9.1 属存在约束,留待 v0.3):
|
|
8
|
-
|
|
9
|
-
① 声明 —— `~/.mdcg/theory.json` 记录 theory_version / accepted_versions /
|
|
10
|
-
declared_by / declared_at,并以 declaration_hash 封缄(防意外损坏)。
|
|
11
|
-
② 校验 —— MCP 启动时 `check()`;不合法即 `theory_ok=False`,主体降级为只读
|
|
12
|
-
(写操作全拒,仅保留 theory 修复入口),**不拒绝启动**。
|
|
13
|
-
|
|
14
|
-
fail-closed 与令牌的差别(有意为之):
|
|
15
|
-
|
|
16
|
-
· 令牌非法 → **拒绝启动**:身份不可信,不能降级为可用;
|
|
17
|
-
· 版本非法 → **降级只读**:身份可信但公理状态未知,禁止改写事实层。
|
|
18
|
-
|
|
19
|
-
存储与 `_tokens.json` 同目录约定(0600)。零第三方依赖。
|
|
20
|
-
"""
|
|
21
|
-
from __future__ import annotations
|
|
22
|
-
|
|
23
|
-
import argparse
|
|
24
|
-
import hashlib
|
|
25
|
-
import hmac
|
|
26
|
-
import json
|
|
27
|
-
import os
|
|
28
|
-
import sys
|
|
29
|
-
import time
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
#:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
#:
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
#
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
#
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return base
|
|
156
|
-
if
|
|
157
|
-
|
|
158
|
-
base
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
if
|
|
170
|
-
base["reason"] =
|
|
171
|
-
return base
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
"""
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
"
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
#
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
ap.
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
p_d.
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
sub.add_parser("
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
_print(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""md_cg · 版本层(蜂群互联 · 层0)
|
|
3
|
+
|
|
4
|
+
设计依据:`docs/swarm/蜂群互联_v0.1.md` §2.3 / §3.1 / §10。
|
|
5
|
+
|
|
6
|
+
版本更新权 = 蜂群**唯一核心权限**(严格唯一,仅 designer)。本模块只做
|
|
7
|
+
「声明 + 校验」两件事,不做跨节点证据交换(§9.1 属存在约束,留待 v0.3):
|
|
8
|
+
|
|
9
|
+
① 声明 —— `~/.mdcg/theory.json` 记录 theory_version / accepted_versions /
|
|
10
|
+
declared_by / declared_at,并以 declaration_hash 封缄(防意外损坏)。
|
|
11
|
+
② 校验 —— MCP 启动时 `check()`;不合法即 `theory_ok=False`,主体降级为只读
|
|
12
|
+
(写操作全拒,仅保留 theory 修复入口),**不拒绝启动**。
|
|
13
|
+
|
|
14
|
+
fail-closed 与令牌的差别(有意为之):
|
|
15
|
+
|
|
16
|
+
· 令牌非法 → **拒绝启动**:身份不可信,不能降级为可用;
|
|
17
|
+
· 版本非法 → **降级只读**:身份可信但公理状态未知,禁止改写事实层。
|
|
18
|
+
|
|
19
|
+
存储与 `_tokens.json` 同目录约定(0600)。零第三方依赖。
|
|
20
|
+
"""
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import argparse
|
|
24
|
+
import hashlib
|
|
25
|
+
import hmac
|
|
26
|
+
import json
|
|
27
|
+
import os
|
|
28
|
+
import sys
|
|
29
|
+
import time
|
|
30
|
+
|
|
31
|
+
from .datapath import aux_root
|
|
32
|
+
from .fsutil import publish
|
|
33
|
+
|
|
34
|
+
THEORY_ENV = "MDCG_THEORY"
|
|
35
|
+
THEORY_FILE_ENV = "MDCG_THEORY_FILE"
|
|
36
|
+
DEFAULT_THEORY_DIR = aux_root()
|
|
37
|
+
DEFAULT_THEORY_FILE = os.path.join(DEFAULT_THEORY_DIR, "theory.json")
|
|
38
|
+
SCHEMA = 1
|
|
39
|
+
|
|
40
|
+
#: 与《智能论》当前版本对齐;仅为**默认声明值**,权威来源是声明文件本身。
|
|
41
|
+
PROTOCOL_VERSION = "3.4"
|
|
42
|
+
|
|
43
|
+
#: 被 declaration_hash 封缄的字段(改动其一即校验失败)
|
|
44
|
+
SEALED_FIELDS = ("theory_version", "accepted_versions", "declared_by", "declared_at")
|
|
45
|
+
|
|
46
|
+
#: theory_ok=False 时仍可执行的 op(唯一修复入口)
|
|
47
|
+
ESCAPE_OPS = ("theory",)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class TheoryError(Exception):
|
|
51
|
+
"""版本层声明非法 / 被篡改 / 越权声明。"""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# --------------------------------------------------------------------------
|
|
55
|
+
# 存储
|
|
56
|
+
# --------------------------------------------------------------------------
|
|
57
|
+
|
|
58
|
+
# 生效条件:path 为真值时返回 path,否则取 os.environ.get(THEORY_FILE_ENV),该值为空串/未设置时回落模块常量 DEFAULT_THEORY_FILE。
|
|
59
|
+
def theory_file(path: str = None) -> str:
|
|
60
|
+
return path or os.environ.get(THEORY_FILE_ENV) or DEFAULT_THEORY_FILE
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# 生效条件:传入可被 json 序列化的 payload(dict)时返回 json.dumps(payload, sort_keys=True, ensure_ascii=False, separators=(",", ":"))。
|
|
64
|
+
def _canon(payload: dict) -> str:
|
|
65
|
+
"""规范化 JSON:键排序 + 紧凑分隔符(哈希可复现的唯一前提)。"""
|
|
66
|
+
return json.dumps(payload, sort_keys=True, ensure_ascii=False,
|
|
67
|
+
separators=(",", ":"))
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# 生效条件:payload 可被 _canon 序列化时返回 hashlib.sha256(_canon(payload).encode("utf-8")).hexdigest() 的十六进制摘要。
|
|
71
|
+
def declaration_hash(payload: dict) -> str:
|
|
72
|
+
return hashlib.sha256(_canon(payload).encode("utf-8")).hexdigest()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# 生效条件:对 SEALED_FIELDS 中每个 k 用 decl.get(k) 取值(缺键即 None),返回同集合长度的 dict。
|
|
76
|
+
def _payload(decl: dict) -> dict:
|
|
77
|
+
return {k: decl.get(k) for k in SEALED_FIELDS}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# 生效条件:以 dict(decl) 为副本 d,写入 d["declaration_hash"]=declaration_hash(_payload(d)),返回该副本(不改动入参 decl)。
|
|
81
|
+
def seal(decl: dict) -> dict:
|
|
82
|
+
"""封缄:补上 declaration_hash。"""
|
|
83
|
+
d = dict(decl)
|
|
84
|
+
d["declaration_hash"] = declaration_hash(_payload(d))
|
|
85
|
+
return d
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# 生效条件:p=theory_file(path) 后,os.path.exists(p) 为假返回 None;open/json.load 抛 OSError 或 ValueError 也返回 None;否则 d 是 dict 时返回 d,不是 dict 返回 None。
|
|
89
|
+
def load(path: str = None):
|
|
90
|
+
p = theory_file(path)
|
|
91
|
+
if not os.path.exists(p):
|
|
92
|
+
return None
|
|
93
|
+
try:
|
|
94
|
+
with open(p, encoding="utf-8") as f:
|
|
95
|
+
d = json.load(f)
|
|
96
|
+
except (OSError, ValueError):
|
|
97
|
+
return None
|
|
98
|
+
return d if isinstance(d, dict) else None
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# 生效条件:p=theory_file(path),先 os.makedirs(os.path.dirname(p) or "."),把 decl 以 ensure_ascii=False, indent=1 写入 p+".tmp" 再 publish(带 Windows 短重试的 os.replace)到 p,os.chmod(p, 0o600) 抛 OSError 时忽略,最终返回 p。
|
|
102
|
+
def save(decl: dict, path: str = None) -> str:
|
|
103
|
+
p = theory_file(path)
|
|
104
|
+
os.makedirs(os.path.dirname(p) or ".", exist_ok=True)
|
|
105
|
+
tmp = p + ".tmp"
|
|
106
|
+
with open(tmp, "w", encoding="utf-8") as f:
|
|
107
|
+
json.dump(decl, f, ensure_ascii=False, indent=1)
|
|
108
|
+
publish(tmp, p)
|
|
109
|
+
try:
|
|
110
|
+
os.chmod(p, 0o600)
|
|
111
|
+
except OSError:
|
|
112
|
+
pass
|
|
113
|
+
return p
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
# --------------------------------------------------------------------------
|
|
117
|
+
# 声明与校验
|
|
118
|
+
# --------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
# 生效条件:v=(version or PROTOCOL_VERSION).strip(),v 为空串时抛 TheoryError;accepted 为假值(None/空)时 acc=[v],否则 list(accepted) 并剔除 strip 后为空的项,v 不在 acc 时 insert(0, v);declared_by 假值回落 "designer",declared_at=float(now if now is not None else time.time()),返回含 SCHEMA 的 dict。
|
|
121
|
+
def make_declaration(version: str = None, accepted=None,
|
|
122
|
+
declared_by: str = "designer", now: float = None) -> dict:
|
|
123
|
+
"""构造(未封缄的)版本声明。accepted 默认只含自身版本。"""
|
|
124
|
+
v = (version or PROTOCOL_VERSION).strip()
|
|
125
|
+
if not v:
|
|
126
|
+
raise TheoryError("theory_version 不能为空")
|
|
127
|
+
acc = list(accepted) if accepted else [v]
|
|
128
|
+
acc = [str(x).strip() for x in acc if str(x).strip()]
|
|
129
|
+
if v not in acc:
|
|
130
|
+
acc.insert(0, v)
|
|
131
|
+
return {"schema": SCHEMA, "theory_version": v, "accepted_versions": acc,
|
|
132
|
+
"declared_by": (declared_by or "designer").strip(),
|
|
133
|
+
"declared_at": float(now if now is not None else time.time())}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
# 生效条件:d=decl if decl is not None else load(theory_file(path));d 为假时返回 theory_ok=True、auto=True、version=PROTOCOL_VERSION 的状态;d.get("schema")!=SCHEMA、hmac.compare_digest(str(d.get("declaration_hash") or ""), declaration_hash(_payload(d))) 不通过、v=d.get("theory_version") 为假、或 v 不在 acc=[str(x) for x in (d.get("accepted_versions") or [])] 时各自返回同一个 base 且 theory_ok=False,其余情况 base 更新为 theory_ok=True、reason="ok"。
|
|
137
|
+
def check(path: str = None, decl: dict = None) -> dict:
|
|
138
|
+
"""校验版本声明 → 状态字典(永不抛异常,供启动路径安全调用)。
|
|
139
|
+
|
|
140
|
+
theory_ok=False 的四种成因互斥且各自可归因:
|
|
141
|
+
① 未声明(文件缺失) ② schema 不符 ③ 封缄不匹配(被篡改) ④ 版本不在认可集合
|
|
142
|
+
"""
|
|
143
|
+
p = theory_file(path)
|
|
144
|
+
d = decl if decl is not None else load(p)
|
|
145
|
+
base = {"source": p, "theory_ok": False, "version": None, "auto": False,
|
|
146
|
+
"accepted_versions": [], "declared_by": None, "declared_at": None,
|
|
147
|
+
"declaration_hash": None, "schema": SCHEMA, "reason": ""}
|
|
148
|
+
if not d:
|
|
149
|
+
# 未声明 ≠ 违规:既有部署升级后不应被立刻降级只读。
|
|
150
|
+
# 按默认版本放行,并由启动路径 ensure() 落盘声明(声明常态化)。
|
|
151
|
+
base.update({"theory_ok": True, "auto": True,
|
|
152
|
+
"version": PROTOCOL_VERSION,
|
|
153
|
+
"accepted_versions": [PROTOCOL_VERSION],
|
|
154
|
+
"reason": f"未声明版本层(按默认版本 {PROTOCOL_VERSION} 运行)"})
|
|
155
|
+
return base
|
|
156
|
+
if d.get("schema") != SCHEMA:
|
|
157
|
+
base["reason"] = f"声明 schema 不符:{d.get('schema')!r}(期望 {SCHEMA})"
|
|
158
|
+
return base
|
|
159
|
+
if not hmac.compare_digest(str(d.get("declaration_hash") or ""),
|
|
160
|
+
declaration_hash(_payload(d))):
|
|
161
|
+
base["reason"] = "声明被篡改(declaration_hash 不匹配)"
|
|
162
|
+
return base
|
|
163
|
+
v = d.get("theory_version")
|
|
164
|
+
acc = [str(x) for x in (d.get("accepted_versions") or [])]
|
|
165
|
+
base.update({"version": v, "accepted_versions": acc,
|
|
166
|
+
"declared_by": d.get("declared_by"),
|
|
167
|
+
"declared_at": d.get("declared_at"),
|
|
168
|
+
"declaration_hash": d.get("declaration_hash")})
|
|
169
|
+
if not v:
|
|
170
|
+
base["reason"] = "声明缺少 theory_version"
|
|
171
|
+
return base
|
|
172
|
+
if v not in acc:
|
|
173
|
+
base["reason"] = f"版本 {v} 不在认可集合 {acc}"
|
|
174
|
+
return base
|
|
175
|
+
base.update({"theory_ok": True, "reason": "ok"})
|
|
176
|
+
return base
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# 生效条件:p=theory_file(path),仅当 load(p) 为 None 时尝试 declare(PROTOCOL_VERSION, actor="system:autoinit", path=p, append_audit=False) 并吞掉 TheoryError/OSError,最终返回 check(p)。
|
|
180
|
+
def ensure(path: str = None) -> dict:
|
|
181
|
+
"""启动路径:无声明则落盘默认声明(声明常态化),再返回校验状态。
|
|
182
|
+
|
|
183
|
+
落盘失败**不阻断启动**——退回 `check()` 的判定(此时为未声明态,放行)。
|
|
184
|
+
"""
|
|
185
|
+
p = theory_file(path)
|
|
186
|
+
if load(p) is None:
|
|
187
|
+
try:
|
|
188
|
+
declare(PROTOCOL_VERSION, actor="system:autoinit", path=p,
|
|
189
|
+
append_audit=False)
|
|
190
|
+
except (TheoryError, OSError):
|
|
191
|
+
pass
|
|
192
|
+
return check(p)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
# 生效条件:version=None/空串时在 make_declaration 内回落 PROTOCOL_VERSION(仅回落结果 strip 后仍为空才抛 TheoryError),seal 后若 append_audit 为真值则把 (old or {}).get("audit") 或 [] 追加一条(仅 old 为真时追加)写入 decl["audit"],save(decl, path) 后返回 {'ok': True, 'declaration': decl, 'status': check(path)}。
|
|
196
|
+
def declare(version: str = None, accepted=None, actor: str = "designer",
|
|
197
|
+
path: str = None, append_audit: bool = True) -> dict:
|
|
198
|
+
"""写入版本声明(版本更新权的唯一物理落点)。"""
|
|
199
|
+
old = load(path)
|
|
200
|
+
decl = make_declaration(version, accepted=accepted, declared_by=actor)
|
|
201
|
+
decl = seal(decl)
|
|
202
|
+
if append_audit:
|
|
203
|
+
hist = list((old or {}).get("audit") or [])
|
|
204
|
+
if old:
|
|
205
|
+
hist.append({"at": time.time(), "from": old.get("theory_version"),
|
|
206
|
+
"to": decl["theory_version"], "by": actor})
|
|
207
|
+
decl["audit"] = hist
|
|
208
|
+
save(decl, path)
|
|
209
|
+
return {"ok": True, "declaration": decl, "status": check(path)}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
# 生效条件:以 path 调 load、check、theory_file,返回 {"declaration": load(path), "status": check(path), "theory_file": theory_file(path)}。
|
|
213
|
+
def show(path: str = None) -> dict:
|
|
214
|
+
return {"declaration": load(path), "status": check(path),
|
|
215
|
+
"theory_file": theory_file(path)}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
# 生效条件:返回含模块常量 SCHEMA、PROTOCOL_VERSION、list(SEALED_FIELDS)、list(ESCAPE_OPS) 及 theory_file() 与 THEORY_ENV/THEORY_FILE_ENV 的自描述 dict。
|
|
219
|
+
def catalog() -> dict:
|
|
220
|
+
"""版本层自描述(供 whoami / service_info 暴露)。"""
|
|
221
|
+
return {"schema": SCHEMA, "theory_file": theory_file(),
|
|
222
|
+
"protocol_version": PROTOCOL_VERSION,
|
|
223
|
+
"sealed_fields": list(SEALED_FIELDS),
|
|
224
|
+
"escape_ops": list(ESCAPE_OPS),
|
|
225
|
+
"env": {"version": THEORY_ENV, "file": THEORY_FILE_ENV}}
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
# --------------------------------------------------------------------------
|
|
229
|
+
# CLI
|
|
230
|
+
# --------------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
# 生效条件:把 json.dumps(obj, ensure_ascii=False, indent=1)+"\n" 写入 sys.stdout,无返回值。
|
|
233
|
+
def _print(obj):
|
|
234
|
+
sys.stdout.write(json.dumps(obj, ensure_ascii=False, indent=1) + "\n")
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
# 生效条件:argv 交给 ap.parse_args(argv 为 None 时 argparse 读 sys.argv),子解析器 required=True 缺 cmd 时由 argparse 报错退出;a.cmd 为 declare/check/ensure/show/catalog 之一且未抛 TheoryError 时打印并返回 0,抛出 TheoryError 时写 stderr 返回 2。
|
|
238
|
+
def main(argv=None):
|
|
239
|
+
ap = argparse.ArgumentParser(
|
|
240
|
+
prog="python -m md_cg.theory",
|
|
241
|
+
description="版本层(蜂群互联层0):版本声明与校验(版本更新权唯一)")
|
|
242
|
+
ap.add_argument("--theory-file", default=None,
|
|
243
|
+
help="声明文件路径(默认 ~/.mdcg/theory.json)")
|
|
244
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
245
|
+
|
|
246
|
+
p_d = sub.add_parser("declare", help="声明版本(仅设计者)")
|
|
247
|
+
p_d.add_argument("--version", default=None, help=f"默认 {PROTOCOL_VERSION}")
|
|
248
|
+
p_d.add_argument("--accept", default=None, help="认可版本集合,逗号分隔")
|
|
249
|
+
p_d.add_argument("--actor", default="designer")
|
|
250
|
+
|
|
251
|
+
sub.add_parser("check", help="校验声明(打印 theory_ok)")
|
|
252
|
+
sub.add_parser("ensure", help="无声明则落盘默认声明(启动路径用)")
|
|
253
|
+
sub.add_parser("show", help="打印声明与校验状态")
|
|
254
|
+
sub.add_parser("catalog", help="版本层自描述")
|
|
255
|
+
|
|
256
|
+
a = ap.parse_args(argv)
|
|
257
|
+
try:
|
|
258
|
+
if a.cmd == "declare":
|
|
259
|
+
acc = [x for x in (a.accept or "").split(",") if x.strip()] or None
|
|
260
|
+
_print(declare(a.version, accepted=acc, actor=a.actor,
|
|
261
|
+
path=a.theory_file))
|
|
262
|
+
elif a.cmd == "check":
|
|
263
|
+
_print(check(a.theory_file))
|
|
264
|
+
elif a.cmd == "ensure":
|
|
265
|
+
_print(ensure(a.theory_file))
|
|
266
|
+
elif a.cmd == "show":
|
|
267
|
+
_print(show(a.theory_file))
|
|
268
|
+
elif a.cmd == "catalog":
|
|
269
|
+
_print(catalog())
|
|
270
|
+
except TheoryError as e:
|
|
271
|
+
sys.stderr.write(f"[theory] {e}\n")
|
|
272
|
+
return 2
|
|
273
|
+
return 0
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
if __name__ == "__main__":
|
|
274
277
|
sys.exit(main())
|