@furongjun1999/dsh-memory 0.9.22 → 0.10.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/CHANGELOG.en.md +450 -0
- package/CHANGELOG.md +112 -0
- package/README.en.md +244 -0
- package/README.md +272 -260
- package/codebuddy/CODEBUDDY.md +1 -1
- package/docs/README.md +2 -0
- package/docs/discipline/templates/full.md.tmpl +1 -1
- package/docs/discipline/templates/rules.mdc.tmpl +1 -1
- package/docs/en/README.md +190 -0
- package/docs/en/TRANSLATION.md +78 -0
- package/docs/en/TRANSLATION_MANIFEST.json +170 -0
- package/docs/en/TRANSLATION_REPORT.md +11 -0
- package/docs/en/dev/README.md +10 -0
- package/docs/en/dev/coding-testing.md +72 -0
- package/docs/en/dev/switch-matrix.md +10 -0
- package/docs/en/glossary.md +97 -0
- package/docs/en/harnesses/claude.md +100 -0
- package/docs/en/harnesses/codebuddy.md +93 -0
- package/docs/en/harnesses/codex.md +88 -0
- package/docs/en/harnesses/dsh.md +84 -0
- package/docs/en/harnesses/zcode.md +42 -0
- package/docs/en/mdcg/ccg.md +32 -0
- package/docs/en/mdcg/generated-docs.md +20 -0
- package/docs/en/mdcg/guardrail-charter.md +170 -0
- package/docs/en/mdcg/mcp-tool-names.md +43 -0
- package/docs/en/mdcg/multi-harness.md +43 -0
- package/docs/en/mdcg/recall-metadata.md +30 -0
- package/docs/en/mdcg/world-model-endpoint.md +379 -0
- package/docs/en/mdcg/write-aliases.md +130 -0
- package/docs/en/ops/README.md +11 -0
- package/docs/en/ops/environment-known-issues.md +244 -0
- package/docs/en/ops/install.md +72 -0
- package/docs/en/ops/operations-troubleshooting.md +86 -0
- package/docs/en/ops/permissions-review.md +79 -0
- package/docs/en/ops/release-governance.md +49 -0
- package/docs/en/ops/tool-name-migration.md +30 -0
- package/docs/en/ops/ui-install-troubleshooting.md +53 -0
- package/docs/en/standards/ai-coding-standard-checklist.md +71 -0
- package/docs/en/standards/ai-coding-standard-lingshu.md +47 -0
- package/docs/en/standards/ai-coding-standard.md +185 -0
- package/docs/en/ui/minimal.md +62 -0
- package/docs/mdcg/cogmap_ops.md +16 -15
- package/lib/client.js +29 -3
- package/lib/hooks.d.ts +34 -0
- package/lib/hooks.js +167 -33
- package/lib/index.js +19 -3
- package/lib/lib/console_web.mjs +30 -3
- package/lib/lib/hook_audit.d.ts +3 -2
- package/lib/lib/hook_audit.js +14 -4
- package/lib/lib/mdcg_client.d.ts +5 -2
- package/lib/lib/mdcg_client.js +8 -4
- package/lib/lib/prompt_safety.d.ts +24 -0
- package/lib/lib/prompt_safety.js +77 -1
- package/lib/lib/token_store.js +5 -2
- package/lib/tools.js +5 -3
- package/md_cg/bucketindex.py +231 -0
- package/md_cg/bucketscope.py +339 -0
- package/md_cg/config_registry_bulk.py +19 -10
- package/md_cg/config_validate.py +14 -2
- package/md_cg/detrerank.py +137 -0
- package/md_cg/fsutil.py +215 -25
- package/md_cg/govern/__init__.py +23 -0
- package/md_cg/iface/__init__.py +46 -0
- package/md_cg/iface/_verify.py +609 -0
- package/md_cg/iface/contracts.py +337 -0
- package/md_cg/judgment_manifest.py +14 -0
- package/md_cg/kernel/__init__.py +27 -0
- package/md_cg/mcp_server.py +457 -23
- package/md_cg/mdcg.py +1034 -79
- package/md_cg/mdcos.py +263 -21
- package/md_cg/metacognition.py +66 -13
- package/md_cg/mutate_extract_shipped.py +126 -0
- package/md_cg/mutate_ledger_e2e.py +100 -0
- package/md_cg/nodefile.py +147 -23
- package/md_cg/op_table.py +22 -4
- package/md_cg/probe_c_ledger_integrity.py +258 -0
- package/md_cg/probe_d_retirement_discipline.py +263 -0
- package/md_cg/reconcile.py +9 -11
- package/md_cg/review_cli.py +6 -1
- package/md_cg/rollback.py +16 -1
- package/md_cg/run_tests.py +30 -1
- package/md_cg/scopeselect.py +214 -0
- package/md_cg/scrub.py +160 -23
- package/md_cg/security.py +4 -2
- package/md_cg/state_extract.py +149 -0
- package/md_cg/state_slots.py +58 -10
- package/md_cg/structboost.py +179 -0
- package/md_cg/test_alternatives_historical.py +162 -0
- package/md_cg/test_b2_sensitivity_landing.py +226 -223
- package/md_cg/test_b3_merge_keeps_content.py +608 -583
- package/md_cg/test_bucket_cards.py +85 -0
- package/md_cg/test_bucket_day_order.py +74 -0
- package/md_cg/test_bucket_quota.py +79 -0
- package/md_cg/test_bucket_recency_guards.py +75 -0
- package/md_cg/test_bucket_three_causes.py +83 -0
- package/md_cg/test_bucketindex.py +130 -0
- package/md_cg/test_bucketscope_early.py +71 -0
- package/md_cg/test_cond_term_fold.py +183 -0
- package/md_cg/test_cond_term_fold_gate.py +1210 -0
- package/md_cg/test_cond_term_neg_scope.py +147 -0
- package/md_cg/test_cond_terms_colon.py +348 -0
- package/md_cg/test_detrerank.py +157 -0
- package/md_cg/test_gate_four_kpi.py +136 -0
- package/md_cg/test_generation_guard.py +76 -43
- package/md_cg/test_issue192_move_layer_indices_guard.py +48 -0
- package/md_cg/test_issue362_neg_condition.py +44 -7
- package/md_cg/test_issue39_utf8_stdio.py +572 -572
- package/md_cg/test_issue413_preimage_failclosed.py +147 -9
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +14 -8
- package/md_cg/test_issue677_flush_batch_io.py +139 -0
- package/md_cg/test_issue677_info_fast.py +82 -0
- package/md_cg/test_issue677_remember_dedupe.py +106 -0
- package/md_cg/test_issue712_tokens_sessions_read.py +199 -0
- package/md_cg/test_issue722_strict_conditions.py +96 -0
- package/md_cg/test_issue730_narrowing_audit.py +121 -0
- package/md_cg/test_issue733_verify_override.py +193 -0
- package/md_cg/test_issue733_verify_override_and_overwrite_inherit.py +315 -0
- package/md_cg/test_issue740_filelock_fd_leak.py +218 -0
- package/md_cg/test_issue741_content_hash_same_source.py +158 -0
- package/md_cg/test_issue747_slot_contradiction.py +125 -0
- package/md_cg/test_issue754_nested_root_guard.py +197 -0
- package/md_cg/test_issue757_index_hash_compare.py +85 -0
- package/md_cg/test_issue758_disk_hash_single.py +67 -0
- package/md_cg/test_issue759_twophase_hash.py +67 -0
- package/md_cg/test_issue768_overwrite_time_window_anchor.py +135 -0
- package/md_cg/test_issue789_log_plaintext_probe.py +100 -0
- package/md_cg/test_issue790_verb_negation.py +77 -0
- package/md_cg/test_issue810_root_guard.py +71 -0
- package/md_cg/test_issue812_info_index_stale.py +78 -0
- package/md_cg/test_issue827_weaken_dedup.py +361 -0
- package/md_cg/test_issue829_calibration_positive_gate.py +473 -0
- package/md_cg/test_kernel_iface_contracts.py +18 -0
- package/md_cg/test_md_cg_no_hive_dep.py +238 -17
- package/md_cg/test_n204_n205_n226_n227_n228_n229_exit_gates.py +545 -542
- package/md_cg/test_n209_verify_write_face_gates.py +5 -2
- package/md_cg/test_neg_condition_hits.py +336 -333
- package/md_cg/test_probe_cd_shipped.py +385 -0
- package/md_cg/test_recent_tail.py +93 -0
- package/md_cg/test_run_tests_entry_gates.py +110 -0
- package/md_cg/test_scopeselect.py +70 -0
- package/md_cg/test_scopeselect_clock.py +70 -0
- package/md_cg/test_scrub_source_line_false_positive.py +64 -0
- package/md_cg/test_session_attribution_consistency.py +835 -813
- package/md_cg/test_state_extract_shipped.py +162 -0
- package/md_cg/test_state_ledger_chain.py +207 -0
- package/md_cg/test_state_ledger_e2e.py +145 -0
- package/md_cg/test_structboost.py +102 -0
- package/md_cg/test_timeboost.py +83 -0
- package/md_cg/test_timeboost_relativedate.py +71 -0
- package/md_cg/test_topicagg.py +129 -0
- package/md_cg/test_twophase.py +286 -286
- package/md_cg/timeboost.py +223 -0
- package/md_cg/tokens.py +55 -7
- package/md_cg/topicagg.py +142 -0
- package/md_cg/twophase.py +15 -6
- package/md_cg/whitebox_kb/wisdom/code_compose.py +33 -5
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +5 -1
- package/md_cg/whitebox_kb/wisdom/role_compose.py +271 -268
- package/md_cg/writepipe.py +35 -3
- package/package.json +4 -1
- package/scripts/workspace_index.py +2 -0
- package/src/hooks.ts +182 -34
- package/src/index.ts +19 -3
- package/src/lib/console_web.mjs +30 -3
- package/src/lib/hook_audit.ts +15 -5
- package/src/lib/mdcg_client.ts +8 -4
- package/src/lib/prompt_safety.ts +73 -1
- package/src/lib/token_store.ts +5 -2
- package/src/tools.ts +5 -3
- package/ui/minimal/integrity.json +2 -2
- package/ui/minimal/payload/build-ui.mjs +41 -2
- package/ui/minimal/payload/lib/client.js +29 -3
- package/ui/minimal/payload/lib/lib/console_web.mjs +30 -3
- package/ui/minimal/payload/src/lib/console_web.mjs +30 -3
- package/ui/minimal/payload/web/app.js +3 -1
- package/ui/minimal/payload/web/client.js +30 -3
- package/web/app.js +3 -1
- package/web/client.js +30 -3
- package/zcode/AGENTS.md +1 -1
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #712 签发侧接线守卫:sessions_read 的 issue/derive/verify/CLI/narrowed 全链路。
|
|
3
|
+
|
|
4
|
+
背景(2026-11-01):0.9.18 起 `Principal.sessions_read` 的**读侧执行**已落地
|
|
5
|
+
(`mdcos` 读路径按它收窄,见 test_issue712_session_window_attribution),但**签发侧**
|
|
6
|
+
没有接线——`issue()`/`derive()` 签名不带它,落盘 rec 不存它,CLI 没有 `--sessions-read`,
|
|
7
|
+
`verify_token` 重建 Principal 时也不传它。⇒ 这把锁有锁体、有锁舌,没有锁眼。
|
|
8
|
+
本套断言钉住接线后的六段语义(与 security.Principal 文档口径逐字对齐):
|
|
9
|
+
|
|
10
|
+
G1 签发回环:issue(sessions_read=[s1,s2]) → 落盘 → verify → tuple(s1,s2)
|
|
11
|
+
G2 缺省零变:不带该参数 = None = 不限读面(旧行为,同 layers 兼容口径)
|
|
12
|
+
G3 特殊值:'*' 原样保留(读面语义见 session_recall 的 recent 段)
|
|
13
|
+
G4 存量兼容:rec **没有**这个键(0.9.18~0.9.21 签发的真实令牌库)→ verify 不炸、None
|
|
14
|
+
G5 派生只能收窄(与 N123 过期夹紧同构):
|
|
15
|
+
a 未指定=继承父界 b 交集非空取交集并记 clamped c 交集空=TokenError(fail-closed)
|
|
16
|
+
d 子请求'*'而父有界=夹紧为父界 e 父'*'=视同无界,子自设界生效 f 父 None 同 e
|
|
17
|
+
G6 narrowed_principal 传导(附带 bug fix 回归钉):owner 带读面限制时,
|
|
18
|
+
as_unit 请求级收窄后的身份**必须仍带**同一限制——修复前此处漏传,静默放宽为不限。
|
|
19
|
+
G7 CLI 面:issue/derive --sessions-read 逗号解析(复用 _csv_list),list 可见读面,
|
|
20
|
+
verify 的 as_dict 输出含 sessions_read。
|
|
21
|
+
|
|
22
|
+
红基线:对未打补丁的 0.9.21 HEAD 运行本文件,G1 起即 `TypeError: unexpected keyword
|
|
23
|
+
argument 'sessions_read'` 成片转红(G4 会红在 verify 输出无读面键)——接线前这些
|
|
24
|
+
语义**没有合法来源**,断言不可能空转全绿。
|
|
25
|
+
|
|
26
|
+
运行:python -X utf8 -m md_cg.test_issue712_tokens_sessions_read
|
|
27
|
+
"""
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import contextlib
|
|
31
|
+
import io
|
|
32
|
+
import json
|
|
33
|
+
import os
|
|
34
|
+
import sys
|
|
35
|
+
import tempfile
|
|
36
|
+
|
|
37
|
+
from . import tokens
|
|
38
|
+
from .tokens import TokenError, issue, verify_token, derive, narrowed_principal, main
|
|
39
|
+
|
|
40
|
+
_PASS = []
|
|
41
|
+
_FAIL = []
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def ok(cond, msg, extra=""):
|
|
45
|
+
(_PASS if cond else _FAIL).append(msg)
|
|
46
|
+
print((" PASS " if cond else " FAIL ") + msg + ((" ← " + str(extra)) if (extra and not cond) else ""))
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _tmp_tokfile(name="t"):
|
|
50
|
+
d = tempfile.mkdtemp(prefix=f"mdcg-712-{name}-")
|
|
51
|
+
return os.path.join(d, "_tokens.json")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def g1_roundtrip():
|
|
55
|
+
print("\n== G1 签发回环 ==")
|
|
56
|
+
tf = _tmp_tokfile("g1")
|
|
57
|
+
r = issue("designer", sessions_read=["s1", "s2"], path=tf)
|
|
58
|
+
ok(r.get("sessions_read") == ["s1", "s2"], "G1a issue 返回值携带读面(list 形态)", r.get("sessions_read"))
|
|
59
|
+
p = verify_token(r["token"], path=tf)
|
|
60
|
+
ok(p.sessions_read == ("s1", "s2"), "G1b verify → Principal.sessions_read tuple 化", p.sessions_read)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def g2_default_zero():
|
|
64
|
+
print("\n== G2 缺省零变(旧行为) ==")
|
|
65
|
+
tf = _tmp_tokfile("g2")
|
|
66
|
+
r = issue("recorder", path=tf)
|
|
67
|
+
ok(r.get("sessions_read") is None, "G2a issue 不带参数 → None", r.get("sessions_read"))
|
|
68
|
+
p = verify_token(r["token"], path=tf)
|
|
69
|
+
ok(p.sessions_read is None, "G2b verify → Principal 不限读面", p.sessions_read)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def g3_star():
|
|
73
|
+
print("\n== G3 特殊值 '*' ==")
|
|
74
|
+
tf = _tmp_tokfile("g3")
|
|
75
|
+
r = issue("recorder", sessions_read=["*"], path=tf)
|
|
76
|
+
p = verify_token(r["token"], path=tf)
|
|
77
|
+
ok(p.sessions_read == ("*",), "G3 '*' 原样保留(全部读面语义在执行侧解释)", p.sessions_read)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def g4_legacy_record():
|
|
81
|
+
print("\n== G4 存量兼容(rec 无此键) ==")
|
|
82
|
+
tf = _tmp_tokfile("g4")
|
|
83
|
+
r = issue("recorder", path=tf)
|
|
84
|
+
with open(tf, encoding="utf-8") as f:
|
|
85
|
+
data = json.load(f)
|
|
86
|
+
data["tokens"][r["token_id"]].pop("sessions_read") # 模拟 0.9.21 存量库
|
|
87
|
+
with open(tf, "w", encoding="utf-8") as f:
|
|
88
|
+
json.dump(data, f, ensure_ascii=False)
|
|
89
|
+
try:
|
|
90
|
+
p = verify_token(r["token"], path=tf)
|
|
91
|
+
ok(p.sessions_read is None, "G4 缺键记录 verify 不炸且=不限(零闸变)", p.sessions_read)
|
|
92
|
+
except TokenError as e:
|
|
93
|
+
ok(False, "G4 缺键记录 verify 不炸且=不限(零闸变)", e)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def g5_derive():
|
|
97
|
+
print("\n== G5 派生只能收窄 ==")
|
|
98
|
+
tf = _tmp_tokfile("g5")
|
|
99
|
+
par = issue("designer", sessions_read=["s1", "s2"], path=tf)
|
|
100
|
+
# a 未指定 → 继承父界
|
|
101
|
+
a = derive(par["token"], "recorder", path=tf)
|
|
102
|
+
pa = verify_token(a["token"], path=tf)
|
|
103
|
+
ok(pa.sessions_read == ("s1", "s2"), "G5a 子未指定=继承父界", pa.sessions_read)
|
|
104
|
+
# b 交集
|
|
105
|
+
b = derive(par["token"], "recorder", sessions_read=["s2", "s3"], path=tf)
|
|
106
|
+
pb = verify_token(b["token"], path=tf)
|
|
107
|
+
ok(pb.sessions_read == ("s2",), "G5b 交集非空取交集", pb.sessions_read)
|
|
108
|
+
ok("sessions_read" in b.get("clamped", []), "G5b' 夹紧记录进 clamped", b.get("clamped"))
|
|
109
|
+
# c 交集空 → 拒签(fail-closed)
|
|
110
|
+
try:
|
|
111
|
+
derive(par["token"], "recorder", sessions_read=["s9"], path=tf)
|
|
112
|
+
ok(False, "G5c 交集空=TokenError(宁可拒签)")
|
|
113
|
+
except TokenError:
|
|
114
|
+
ok(True, "G5c 交集空=TokenError(宁可拒签)")
|
|
115
|
+
# d 子请求 '*' 而父有界 → 夹紧回父界
|
|
116
|
+
d = derive(par["token"], "recorder", sessions_read=["*"], path=tf)
|
|
117
|
+
pd = verify_token(d["token"], path=tf)
|
|
118
|
+
ok(pd.sessions_read == ("s1", "s2"), "G5d 子'*'遇父有界=夹紧为父界", pd.sessions_read)
|
|
119
|
+
ok("sessions_read" in d.get("clamped", []), "G5d' 夹紧记录进 clamped", d.get("clamped"))
|
|
120
|
+
# e 父 '*' → 视同无界,子自设界生效(#543 授权闸:非空库再签 designer 会被拒,
|
|
121
|
+
# 每个父令牌用独立空库首启自举——这本身也是新库既有的正确姿势)
|
|
122
|
+
tf_e = _tmp_tokfile("g5e")
|
|
123
|
+
pstar = issue("designer", sessions_read=["*"], path=tf_e)
|
|
124
|
+
e = derive(pstar["token"], "recorder", sessions_read=["s1"], path=tf_e)
|
|
125
|
+
pe = verify_token(e["token"], path=tf_e)
|
|
126
|
+
ok(pe.sessions_read == ("s1",), "G5e 父'*'=子可自设任意界", pe.sessions_read)
|
|
127
|
+
# f 父 None 同 e
|
|
128
|
+
tf_f = _tmp_tokfile("g5f")
|
|
129
|
+
pnone = issue("designer", path=tf_f)
|
|
130
|
+
f = derive(pnone["token"], "recorder", sessions_read=["s7"], path=tf_f)
|
|
131
|
+
pf = verify_token(f["token"], path=tf_f)
|
|
132
|
+
ok(pf.sessions_read == ("s7",), "G5f 父无界=子可自设任意界", pf.sessions_read)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def g6_narrowed():
|
|
136
|
+
print("\n== G6 narrowed_principal 传导(bug fix 回归钉) ==")
|
|
137
|
+
tf = _tmp_tokfile("g6")
|
|
138
|
+
r = issue("designer", sessions_read=["s1", "s2"], path=tf)
|
|
139
|
+
p = verify_token(r["token"], path=tf)
|
|
140
|
+
q = narrowed_principal(p, "record")
|
|
141
|
+
ok(q.sessions_read == ("s1", "s2"),
|
|
142
|
+
"G6a owner 带限制 → as_unit 收窄身份仍带(修复前此处静默变 None=fail-open)",
|
|
143
|
+
q.sessions_read)
|
|
144
|
+
tf_b = _tmp_tokfile("g6b")
|
|
145
|
+
r2 = issue("designer", path=tf_b)
|
|
146
|
+
p2 = verify_token(r2["token"], path=tf_b)
|
|
147
|
+
q2 = narrowed_principal(p2, "record")
|
|
148
|
+
ok(q2.sessions_read is None, "G6b owner 无限制 → 收窄后仍无限制(不误伤)", q2.sessions_read)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def g7_cli():
|
|
152
|
+
print("\n== G7 CLI 面 ==")
|
|
153
|
+
tf = _tmp_tokfile("g7")
|
|
154
|
+
buf = io.StringIO()
|
|
155
|
+
with contextlib.redirect_stdout(buf):
|
|
156
|
+
rc = main(["--token-file", tf, "issue", "--role", "designer",
|
|
157
|
+
"--sessions-read", "a,b"])
|
|
158
|
+
ok(rc == 0, "G7a issue --sessions-read rc=0", rc)
|
|
159
|
+
try:
|
|
160
|
+
j = json.loads(buf.getvalue())
|
|
161
|
+
except Exception as e: # noqa: BLE001
|
|
162
|
+
ok(False, "G7a issue 输出纯 JSON", e)
|
|
163
|
+
j = {}
|
|
164
|
+
ok(j.get("sessions_read") == ["a", "b"], "G7b issue JSON 含读面", j.get("sessions_read"))
|
|
165
|
+
buf2 = io.StringIO()
|
|
166
|
+
with contextlib.redirect_stdout(buf2):
|
|
167
|
+
rc2 = main(["--token-file", tf, "derive", "--token", j.get("token", ""),
|
|
168
|
+
"--role", "recorder", "--sessions-read", "b,c"])
|
|
169
|
+
ok(rc2 == 0, "G7c derive --sessions-read rc=0", rc2)
|
|
170
|
+
j2 = json.loads(buf2.getvalue()) if rc2 == 0 else {}
|
|
171
|
+
ok(j2.get("sessions_read") == ["b"], "G7d derive 交集语义经 CLI 生效", j2.get("sessions_read"))
|
|
172
|
+
ok("sessions_read" in (j2.get("clamped") or []), "G7e clamped 经 CLI 可见", j2.get("clamped"))
|
|
173
|
+
buf3 = io.StringIO()
|
|
174
|
+
with contextlib.redirect_stdout(buf3):
|
|
175
|
+
rc3 = main(["--token-file", tf, "list"])
|
|
176
|
+
j3 = json.loads(buf3.getvalue()) if rc3 == 0 else {}
|
|
177
|
+
rows = j3.get("tokens") or []
|
|
178
|
+
ok(rc3 == 0 and all("sessions_read" in x for x in rows),
|
|
179
|
+
"G7f list 展示读面(运维体检可见)", [list(x.keys()) and ("sessions_read" in x) for x in rows][:1])
|
|
180
|
+
buf4 = io.StringIO()
|
|
181
|
+
with contextlib.redirect_stdout(buf4):
|
|
182
|
+
rc4 = main(["--token-file", tf, "verify", "--token", j.get("token", "")])
|
|
183
|
+
j4 = json.loads(buf4.getvalue()) if rc4 == 0 else {}
|
|
184
|
+
ok(j4.get("principal", {}).get("sessions_read") == ["a", "b"],
|
|
185
|
+
"G7g verify 输出读面(as_dict 接线)", j4.get("principal", {}).get("sessions_read"))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def main_():
|
|
189
|
+
if sys.stdout.encoding and sys.stdout.encoding.lower().startswith("cp"):
|
|
190
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
191
|
+
for g in (g1_roundtrip, g2_default_zero, g3_star, g4_legacy_record,
|
|
192
|
+
g5_derive, g6_narrowed, g7_cli):
|
|
193
|
+
g()
|
|
194
|
+
print("\nissue #712 签发侧接线守卫:%d 通过,%d 失败" % (len(_PASS), len(_FAIL)))
|
|
195
|
+
return 0 if not _FAIL else 1
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
if __name__ == "__main__":
|
|
199
|
+
sys.exit(main_())
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #722:strict_conditions(Q1=A 工具面默认放宽 / Q2=A 不抬 state)。"""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import shutil
|
|
7
|
+
import sys
|
|
8
|
+
import tempfile
|
|
9
|
+
|
|
10
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
11
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
12
|
+
sys.path.insert(0, _REPO)
|
|
13
|
+
|
|
14
|
+
from md_cg import mcp_server # noqa: E402
|
|
15
|
+
from md_cg.mdcg import MdCG, STATE_BLINDSPOT, STATE_DEFER, STATE_REJECT # noqa: E402
|
|
16
|
+
|
|
17
|
+
FAILS = []
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def ok(cond, name, detail=""):
|
|
21
|
+
if cond:
|
|
22
|
+
print(" PASS " + name)
|
|
23
|
+
else:
|
|
24
|
+
print(" FAIL " + name + " " + str(detail)[:300])
|
|
25
|
+
FAILS.append(name)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
BODY = ("# 功能名:周一会议\n# 生效条件:周一上午九点开会\n# 子功能:a\n# 执行:b\n"
|
|
29
|
+
"# 验证方式:test\n# 不适用条件:无\n\n正文内容")
|
|
30
|
+
|
|
31
|
+
Q = "正文内容怎么用"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _node(fm_extra=None, content=None):
|
|
35
|
+
fm = {"node_id": "x", "layer": "knowledge"}
|
|
36
|
+
if fm_extra:
|
|
37
|
+
fm.update(fm_extra)
|
|
38
|
+
return {"frontmatter": fm, "content": BODY if content is None else content}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def main():
|
|
42
|
+
print("=" * 68)
|
|
43
|
+
print("issue #722 · strict_conditions(Q1=A / Q2=A)")
|
|
44
|
+
print("=" * 68)
|
|
45
|
+
node = _node({"verification_basis": "test"})
|
|
46
|
+
d0 = MdCG.judge_qualification(node, Q)
|
|
47
|
+
d1 = MdCG.judge_qualification(node, Q, strict_conditions=True)
|
|
48
|
+
ok(d0 == d1, "S1 不传 == 显式 True(深度相等)", (d0, d1))
|
|
49
|
+
ok(d0.get("state") == STATE_DEFER and "usable" not in d0, "S1b 默认 DEFER 且无 usable", d0)
|
|
50
|
+
dr = MdCG.judge_qualification(node, Q, strict_conditions=False)
|
|
51
|
+
ok(dr.get("state") == STATE_DEFER, "S2a 放宽后 state 仍 DEFER", dr)
|
|
52
|
+
ok(dr.get("usable") is True and dr.get("relaxed") is True
|
|
53
|
+
and dr.get("code") == "relaxed_condition", "S2b usable/relaxed/code 齐", dr)
|
|
54
|
+
ok("放宽" in str(dr.get("reason")) and "未冒充" in str(dr.get("reason")),
|
|
55
|
+
"S2c reason 明说放宽且未冒充", dr.get("reason"))
|
|
56
|
+
rej = _node({"verification_basis": "test", "non_applicable_conditions": ["正文内容"]})
|
|
57
|
+
r1 = MdCG.judge_qualification(rej, "正文内容")
|
|
58
|
+
r2 = MdCG.judge_qualification(rej, "正文内容", strict_conditions=False)
|
|
59
|
+
ok(r1.get("state") == STATE_REJECT == r2.get("state"), "S3 REJECT 不受影响", (r1, r2))
|
|
60
|
+
blind = {"frontmatter": {"node_id": "b"}, "content": "只有正文"}
|
|
61
|
+
b1 = MdCG.judge_qualification(blind, Q)
|
|
62
|
+
b2 = MdCG.judge_qualification(blind, Q, strict_conditions=False)
|
|
63
|
+
ok(b1.get("state") == STATE_BLINDSPOT == b2.get("state"), "S4 BLINDSPOT 不受影响", (b1, b2))
|
|
64
|
+
n1 = MdCG.judge_qualification(_node(), Q, strict_conditions=False)
|
|
65
|
+
ok(n1.get("state") == STATE_DEFER and not n1.get("usable"),
|
|
66
|
+
"S5 无验证基底:DEFER 且不标 usable", n1)
|
|
67
|
+
tmp = tempfile.mkdtemp(prefix="iss722_")
|
|
68
|
+
try:
|
|
69
|
+
cg = MdCG(os.path.join(tmp, "r"), autoflush=1)
|
|
70
|
+
cg.add("n722", BODY, layer="knowledge")
|
|
71
|
+
cg.index["nodes"]["n722"]["verification_basis"] = "test"
|
|
72
|
+
# 判定读的是**节点文件**里的 frontmatter(_emit 加载正文+fm),故连文件一起补。
|
|
73
|
+
_rel = cg.index["nodes"]["n722"].get("path") or "knowledge/orphan/n722.md"
|
|
74
|
+
_fp = os.path.join(cg.root, _rel)
|
|
75
|
+
_txt = open(_fp, encoding="utf-8").read().replace("verification_basis: null", "verification_basis: test")
|
|
76
|
+
open(_fp, "w", encoding="utf-8", newline="").write(_txt)
|
|
77
|
+
e1 = (mcp_server.call_tool(cg, "cg", {"op": "read", "query": Q}) or {})
|
|
78
|
+
it1 = (e1.get("results") or [{}])[0]
|
|
79
|
+
ok(it1.get("usable") is True and it1.get("state") == STATE_DEFER,
|
|
80
|
+
"S6a cg(op=read) 不传参 ⇒ 工具面默认放宽",
|
|
81
|
+
{k: it1.get(k) for k in ("state", "usable", "relaxed", "code")})
|
|
82
|
+
e2 = (mcp_server.call_tool(cg, "cg", {"op": "read", "query": Q, "strict_conditions": True}) or {})
|
|
83
|
+
it2 = (e2.get("results") or [{}])[0]
|
|
84
|
+
ok(it2.get("state") == STATE_DEFER and not it2.get("usable"),
|
|
85
|
+
"S6b 显式 strict_conditions=true ⇒ 回严格",
|
|
86
|
+
{k: it2.get(k) for k in ("state", "usable")})
|
|
87
|
+
cg.close()
|
|
88
|
+
finally:
|
|
89
|
+
shutil.rmtree(tmp, ignore_errors=True)
|
|
90
|
+
print()
|
|
91
|
+
print("issue #722:%d 项失败" % len(FAILS) if FAILS else "issue #722:全部通过")
|
|
92
|
+
return 1 if FAILS else 0
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if __name__ == "__main__":
|
|
96
|
+
sys.exit(main())
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #730(D):`narrowed_principal` 的**收窄面审计** + `sessions_read` 签发/派生回归。
|
|
3
|
+
|
|
4
|
+
## 病灶(#730)
|
|
5
|
+
`narrowed_principal` 是「as_unit 请求级身份」的唯一收窄点;它的安全性论证是
|
|
6
|
+
**单调性**(伪造 `as_unit` 最坏结果 ≤ owner 权限,不可能提权)。但收窄面是逐字段手写的——
|
|
7
|
+
`sessions_read` 就曾漏传(owner 带读面限制时,请求级收窄后读面静默变宽 = fail-open)。
|
|
8
|
+
|
|
9
|
+
## 本守卫钉什么
|
|
10
|
+
- A 签发:`issue(sessions_read=[...])` 落盘 rec 与 `verify_token` 的 Principal 都带该字段;
|
|
11
|
+
- B 派生收窄:`derive(parent, sessions_read=超集)` 得**交集**;交集为空 ⇒ `TokenError`(fail-closed,
|
|
12
|
+
绝不退化成「不限制」);
|
|
13
|
+
- C 传导:`narrowed_principal` 保留 owner 的 `sessions_read`(不放大);
|
|
14
|
+
- D 收窄面清单:`NARROWING_FIELDS` 与函数体逐项对应,且每个字段的结果 ≤ owner。
|
|
15
|
+
|
|
16
|
+
运行:python -X utf8 -m md_cg.test_issue730_narrowing_audit
|
|
17
|
+
"""
|
|
18
|
+
from __future__ import annotations
|
|
19
|
+
|
|
20
|
+
import os
|
|
21
|
+
import shutil
|
|
22
|
+
import sys
|
|
23
|
+
import tempfile
|
|
24
|
+
|
|
25
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
26
|
+
|
|
27
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
28
|
+
sys.path.insert(0, _REPO)
|
|
29
|
+
|
|
30
|
+
from md_cg import security, tokens # noqa: E402
|
|
31
|
+
|
|
32
|
+
FAILS = []
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def ok(cond, name, detail=""):
|
|
36
|
+
if cond:
|
|
37
|
+
print(" PASS " + name)
|
|
38
|
+
else:
|
|
39
|
+
print(" FAIL " + name + " " + str(detail)[:300])
|
|
40
|
+
FAILS.append(name)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _rank(level):
|
|
44
|
+
return security.SENSITIVITY_ORDER.index(level)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _unrestricted(x):
|
|
48
|
+
"""None 或含 `*` ⇒ 不限(owner 不限时,单元的任何子集都不构成放大)。"""
|
|
49
|
+
return x is None or "*" in tuple(x)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def main():
|
|
53
|
+
print("=" * 68)
|
|
54
|
+
print("issue #730 · 收窄面审计(narrowed_principal / sessions_read)")
|
|
55
|
+
print("=" * 68)
|
|
56
|
+
tmp = tempfile.mkdtemp(prefix="iss730_")
|
|
57
|
+
path = os.path.join(tmp, "tokens.json")
|
|
58
|
+
try:
|
|
59
|
+
# ---- A 签发 ----
|
|
60
|
+
tk = tokens.issue("designer", actor="t730", clearance="internal",
|
|
61
|
+
path=path, sessions_read=["s1"])["token"]
|
|
62
|
+
p = tokens.verify_token(tk, path=path)
|
|
63
|
+
ok(tuple(p.sessions_read or ()) == ("s1",), "A1 签发后 Principal.sessions_read 保留",
|
|
64
|
+
getattr(p, "sessions_read", None))
|
|
65
|
+
rec = (tokens._load(path).get("tokens") or {}).get(p.token_id) or {}
|
|
66
|
+
ok(rec.get("sessions_read") == ["s1"], "A2 落盘 rec 带 sessions_read",
|
|
67
|
+
rec.get("sessions_read"))
|
|
68
|
+
|
|
69
|
+
# ---- B 派生收窄 ----
|
|
70
|
+
tk2 = tokens.derive(tk, role="verifier", path=path,
|
|
71
|
+
sessions_read=["s1", "s2"])["token"]
|
|
72
|
+
p2 = tokens.verify_token(tk2, path=path)
|
|
73
|
+
ok(tuple(p2.sessions_read or ()) == ("s1",), "B1 派生按交集收窄(超集不得放大)",
|
|
74
|
+
p2.sessions_read)
|
|
75
|
+
raised = False
|
|
76
|
+
try:
|
|
77
|
+
tokens.derive(tk, role="verifier", path=path, sessions_read=["sX"])
|
|
78
|
+
except tokens.TokenError:
|
|
79
|
+
raised = True
|
|
80
|
+
ok(raised, "B2 交集为空 ⇒ TokenError(fail-closed,不退化成不限制)")
|
|
81
|
+
|
|
82
|
+
# ---- C 传导 ----
|
|
83
|
+
q = tokens.narrowed_principal(p, "record")
|
|
84
|
+
ok(tuple(getattr(q, "sessions_read", None) or ()) == ("s1",),
|
|
85
|
+
"C1 narrowed_principal 保留 sessions_read(不放大)",
|
|
86
|
+
getattr(q, "sessions_read", None))
|
|
87
|
+
|
|
88
|
+
# ---- D 收窄面清单 ----
|
|
89
|
+
ok(set(tokens.NARROWING_FIELDS) == {
|
|
90
|
+
"clearance", "can_write", "can_admin", "layers_allow",
|
|
91
|
+
"ops_allow", "sessions_read"},
|
|
92
|
+
"D1 NARROWING_FIELDS 覆盖全部收窄型字段", tokens.NARROWING_FIELDS)
|
|
93
|
+
ok(q.can_admin is False, "D2 can_admin 恒 False(请求级身份拿不到管理权)")
|
|
94
|
+
ok(q.can_write is False or bool(p.can_write),
|
|
95
|
+
"D3 can_write 与 owner 取与(owner 只读 ⇒ 单元不可能写)")
|
|
96
|
+
ok(_rank(q.clearance) <= _rank(p.clearance),
|
|
97
|
+
"D4 clearance ≤ owner", (q.clearance, p.clearance))
|
|
98
|
+
ok(_unrestricted(p.layers_allow)
|
|
99
|
+
or set(q.layers_allow or ()) <= set(p.layers_allow),
|
|
100
|
+
"D5 layers_allow 是 owner 的子集(owner 不限时无此约束)",
|
|
101
|
+
(q.layers_allow, p.layers_allow))
|
|
102
|
+
ok(_unrestricted(p.ops_allow)
|
|
103
|
+
or set(q.ops_allow or ()) <= set(p.ops_allow),
|
|
104
|
+
"D6 ops_allow 是 owner 的子集(owner 不限时无此约束)",
|
|
105
|
+
(q.ops_allow, p.ops_allow))
|
|
106
|
+
|
|
107
|
+
# ---- E 未设 sessions_read ⇒ None(不限制,既有行为)----
|
|
108
|
+
tk3 = tokens.issue("recorder", actor="t730b", path=path)["token"]
|
|
109
|
+
p3 = tokens.verify_token(tk3, path=path)
|
|
110
|
+
ok(getattr(p3, "sessions_read", None) is None,
|
|
111
|
+
"E1 未声明 sessions_read ⇒ None(不限制,既有调用方不变)",
|
|
112
|
+
getattr(p3, "sessions_read", None))
|
|
113
|
+
finally:
|
|
114
|
+
shutil.rmtree(tmp, ignore_errors=True)
|
|
115
|
+
print()
|
|
116
|
+
print("issue #730:%d 项失败" % len(FAILS) if FAILS else "issue #730:全部通过")
|
|
117
|
+
return 1 if FAILS else 0
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
if __name__ == "__main__":
|
|
121
|
+
sys.exit(main())
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #733 守卫:`verify(override=True)` 必须能推进受保护节点的验证态。
|
|
3
|
+
|
|
4
|
+
## 病象
|
|
5
|
+
对**受保护节点**(self/anchor 层、protected 位、importance>=0.70 按分保护)
|
|
6
|
+
发起 `verify(..., "weakened", override=True)`:
|
|
7
|
+
|
|
8
|
+
- 回执 `verification.ok == False`、`error == "protected"`;
|
|
9
|
+
- `verification_state` 停在原值(如 unverified);
|
|
10
|
+
- 而 `confidence` / `evidence_count` / `evidence_log` **已按 weakened 扣减并落盘**
|
|
11
|
+
⇒ 「证据记了、结论没落地」的盘面不自洽态。
|
|
12
|
+
|
|
13
|
+
拒绝提示语写的是「确需降级请显式 override=True」——**调用方照做却仍被拒**。
|
|
14
|
+
|
|
15
|
+
## 根因
|
|
16
|
+
`MdCG.verify` 的 `override` 只透传给了 falsified 分支的
|
|
17
|
+
`protect.guard_forget`;两条正常裁决分支(confirmed / weakened)调
|
|
18
|
+
`set_verification` 时整段没带 `override`,于是下游 `trust.set_state` →
|
|
19
|
+
`stamp` 看到的恒为缺省 `False`,受保护节点的降级迁移被无条件拒绝。
|
|
20
|
+
|
|
21
|
+
## 断言面(每条=一个可定点变异的判据)
|
|
22
|
+
- **O1 override=True 生效**:受保护节点的 weakened 裁决真正落到 doubted。
|
|
23
|
+
- **O2 override=False 仍拒**(保护语义**未放宽**):不传/传 False 时照旧
|
|
24
|
+
`ok=False`、状态不变——这是与 O1 配对的反向判据,防止「一律放行」的假修复。
|
|
25
|
+
- **O3 非保护节点不受影响**:缺省调用(不传 override)照常落到 doubted,
|
|
26
|
+
既有调用方行为逐位不变。
|
|
27
|
+
- **O4 confirmed 分支同源透传**:受保护节点 confirmed(升格方向)不因保护被拒。
|
|
28
|
+
|
|
29
|
+
运行:python -X utf8 -m md_cg.test_issue733_verify_override
|
|
30
|
+
退出码(fail-closed):0 = 全绿;1 = 有断言失败。
|
|
31
|
+
沙箱纪律:库根落守卫临时目录,绝不碰在役库与 ~/.mdcg。
|
|
32
|
+
"""
|
|
33
|
+
from __future__ import annotations
|
|
34
|
+
|
|
35
|
+
import os
|
|
36
|
+
import shutil
|
|
37
|
+
import sys
|
|
38
|
+
import tempfile
|
|
39
|
+
|
|
40
|
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
41
|
+
os.environ.setdefault("OPENBLAS_NUM_THREADS", "1")
|
|
42
|
+
|
|
43
|
+
from md_cg.mdcg import MdCG # noqa: E402
|
|
44
|
+
|
|
45
|
+
_FAILED = []
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def ok(cond, msg):
|
|
49
|
+
print((" PASS " if cond else " FAIL ") + msg)
|
|
50
|
+
if not cond:
|
|
51
|
+
_FAILED.append(msg)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
_BODY = (
|
|
55
|
+
"# 功能名:override 透传守卫节点\n"
|
|
56
|
+
"# 生效条件:守卫自检时\n"
|
|
57
|
+
"# 子功能:构造受保护 / 非保护两类节点\n"
|
|
58
|
+
"# 执行:verify 后比对 verification_state\n"
|
|
59
|
+
"# 验证方式:本条测试\n"
|
|
60
|
+
"# 不适用条件:无\n"
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _fresh_root(tag: str) -> str:
|
|
65
|
+
return tempfile.mkdtemp(prefix="mdcg733-" + tag + "-")
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _mk_protected(cg, nid):
|
|
69
|
+
"""构造受保护节点:importance>=0.70 触发按分保护。"""
|
|
70
|
+
cg.add(nid, _BODY, layer="knowledge", tags=["t"], importance=0.9)
|
|
71
|
+
fm = (cg.get(nid) or {}).get("frontmatter") or {}
|
|
72
|
+
return fm
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _mk_normal(cg, nid):
|
|
76
|
+
cg.add(nid, _BODY, layer="knowledge", tags=["t"], importance=0.2)
|
|
77
|
+
return (cg.get(nid) or {}).get("frontmatter") or {}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def o1_override_true_lands():
|
|
81
|
+
"""O1:受保护节点 weakened + override=True → 真的落到 doubted。"""
|
|
82
|
+
print("== O1 override=True 生效 ==")
|
|
83
|
+
root = _fresh_root("o1")
|
|
84
|
+
try:
|
|
85
|
+
cg = MdCG(root)
|
|
86
|
+
fm = _mk_protected(cg, "o1")
|
|
87
|
+
ok(bool(fm.get("protected")) is True,
|
|
88
|
+
"O1 前置:节点确为受保护(protected=%r importance=%r)"
|
|
89
|
+
% (fm.get("protected"), fm.get("importance")))
|
|
90
|
+
res = cg.verify("o1", "外部证据:overridden", "weakened", override=True)
|
|
91
|
+
ver = res.get("verification") or {}
|
|
92
|
+
after = (cg.get("o1") or {}).get("frontmatter") or {}
|
|
93
|
+
ok(ver.get("ok") is True,
|
|
94
|
+
"O1 verification.ok 为真(实际 %r error=%r)"
|
|
95
|
+
% (ver.get("ok"), ver.get("error")))
|
|
96
|
+
ok(after.get("verification_state") == "doubted",
|
|
97
|
+
"O1 verification_state 落到 doubted(实际 %r)"
|
|
98
|
+
% (after.get("verification_state"),))
|
|
99
|
+
finally:
|
|
100
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def o2_override_false_still_denies():
|
|
104
|
+
"""O2:不传 override 时**仍然拒绝**(保护语义不得放宽)。"""
|
|
105
|
+
print("== O2 override 缺省仍拒 ==")
|
|
106
|
+
root = _fresh_root("o2")
|
|
107
|
+
try:
|
|
108
|
+
cg = MdCG(root)
|
|
109
|
+
fm = _mk_protected(cg, "o2")
|
|
110
|
+
before = fm.get("verification_state")
|
|
111
|
+
res = cg.verify("o2", "外部证据:无 override", "weakened")
|
|
112
|
+
ver = res.get("verification") or {}
|
|
113
|
+
after = (cg.get("o2") or {}).get("frontmatter") or {}
|
|
114
|
+
ok(ver.get("ok") is False,
|
|
115
|
+
"O2 缺省 override 时 verification.ok 为假(实际 %r)" % (ver.get("ok"),))
|
|
116
|
+
ok(ver.get("error") == "protected",
|
|
117
|
+
"O2 拒绝码为 protected(实际 %r)" % (ver.get("error"),))
|
|
118
|
+
# 复核 ①(#752 采纳):`after == before` 在 before 为 None 时会退化成
|
|
119
|
+
# 「仍是 None」,分不出 unverified→None 的破坏。先钉死初态属于合法集合,
|
|
120
|
+
# 再断言 after 与之逐位相等 —— 初态将来漂移也不会假绿。
|
|
121
|
+
ok(before in (None, "unverified"),
|
|
122
|
+
"O2 前置:初态属于合法集合 {None, unverified}(实际 %r)" % (before,))
|
|
123
|
+
ok(after.get("verification_state") == before,
|
|
124
|
+
"O2 受保护节点状态未被改动(前=%r 后=%r)"
|
|
125
|
+
% (before, after.get("verification_state")))
|
|
126
|
+
finally:
|
|
127
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def o3_normal_unaffected():
|
|
131
|
+
"""O3:非保护节点缺省调用行为不变。"""
|
|
132
|
+
print("== O3 非保护节点不受影响 ==")
|
|
133
|
+
root = _fresh_root("o3")
|
|
134
|
+
try:
|
|
135
|
+
cg = MdCG(root)
|
|
136
|
+
_mk_normal(cg, "o3")
|
|
137
|
+
res = cg.verify("o3", "外部证据:常规", "weakened")
|
|
138
|
+
ver = res.get("verification") or {}
|
|
139
|
+
after = (cg.get("o3") or {}).get("frontmatter") or {}
|
|
140
|
+
ok(ver.get("ok") is True,
|
|
141
|
+
"O3 非保护节点缺省 verify 成功(实际 ok=%r)" % (ver.get("ok"),))
|
|
142
|
+
ok(after.get("verification_state") == "doubted",
|
|
143
|
+
"O3 非保护节点落到 doubted(实际 %r)"
|
|
144
|
+
% (after.get("verification_state"),))
|
|
145
|
+
finally:
|
|
146
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def o4_confirmed_branch_carries_override():
|
|
150
|
+
"""O4:confirmed 分支同样透传 override(升格方向不被保护误拦)。"""
|
|
151
|
+
print("== O4 confirmed 分支同源 ==")
|
|
152
|
+
root = _fresh_root("o4")
|
|
153
|
+
try:
|
|
154
|
+
cg = MdCG(root)
|
|
155
|
+
_mk_protected(cg, "o4")
|
|
156
|
+
# 复核 ②(#752 采纳):原写法是恒真测试——confirmed 是升格方向(verified
|
|
157
|
+
# rank 全局最小),旧码此处本就 ok=True,回退补丁也不变红。改为 monkeypatch
|
|
158
|
+
# 捕获 `set_verification` 实际收到的 override 实参:回退 ⇒ False ⇒ 红。
|
|
159
|
+
seen = {}
|
|
160
|
+
orig = cg.set_verification
|
|
161
|
+
|
|
162
|
+
def spy(node_id, state, **kw):
|
|
163
|
+
seen["override"] = kw.get("override")
|
|
164
|
+
return orig(node_id, state, **kw)
|
|
165
|
+
|
|
166
|
+
cg.set_verification = spy
|
|
167
|
+
cg.verify("o4", "外部证据:确认", "confirmed", override=True)
|
|
168
|
+
ok(seen.get("override") is True,
|
|
169
|
+
"O4 set_verification 实际收到 override=True(实收 %r)"
|
|
170
|
+
% (seen.get("override"),))
|
|
171
|
+
finally:
|
|
172
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
_GROUPS = (o1_override_true_lands, o2_override_false_still_denies,
|
|
176
|
+
o3_normal_unaffected, o4_confirmed_branch_carries_override)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def main():
|
|
180
|
+
for fn in _GROUPS:
|
|
181
|
+
fn()
|
|
182
|
+
print("=" * 68)
|
|
183
|
+
if _FAILED:
|
|
184
|
+
print("verify override 透传守卫:%d 条失败" % len(_FAILED))
|
|
185
|
+
for m in _FAILED:
|
|
186
|
+
print(" - " + m)
|
|
187
|
+
return 1
|
|
188
|
+
print("verify override 透传守卫:全部通过")
|
|
189
|
+
return 0
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
if __name__ == "__main__":
|
|
193
|
+
sys.exit(main())
|