@furongjun1999/dsh-memory 0.9.6 → 0.9.7
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/data/policy.json +38 -38
- package/docs/discipline/templates/spec.txt.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +29 -6
- package/lib/index.js +11 -2
- package/lib/lib/hook_audit.js +38 -2
- package/lib/lib/prompt_safety.js +14 -3
- package/lib/tools.d.ts +7 -0
- package/lib/tools.js +22 -1
- package/md_cg/_guard_kit.py +121 -121
- package/md_cg/anchor.py +114 -114
- package/md_cg/audit.py +15 -2
- package/md_cg/chain.py +3 -2
- package/md_cg/config_registry_guard.py +487 -487
- package/md_cg/conformance.py +747 -747
- package/md_cg/consolidate.py +1793 -1793
- package/md_cg/crypto.py +794 -794
- package/md_cg/hyperedge.py +262 -262
- package/md_cg/interface_schema.py +146 -146
- package/md_cg/lifecycle.py +426 -426
- package/md_cg/mdcos.py +12 -8
- package/md_cg/predict.py +1103 -1103
- package/md_cg/protect.py +654 -654
- package/md_cg/provenance.py +606 -606
- package/md_cg/queue_health.py +130 -130
- package/md_cg/refine.py +613 -613
- package/md_cg/retrieve.py +128 -128
- package/md_cg/revise.py +140 -140
- package/md_cg/rollback.py +657 -657
- package/md_cg/rollback_cli.py +124 -124
- package/md_cg/rotate.py +346 -346
- package/md_cg/routing.py +490 -490
- package/md_cg/self_surface.py +50 -50
- package/md_cg/semantic/canonical.py +144 -144
- package/md_cg/semantic/zh_en_atoms.py +141 -141
- package/md_cg/sources.py +1091 -1091
- package/md_cg/stg.py +3 -2
- package/md_cg/test_audit_rotate_parity.py +279 -279
- package/md_cg/test_autonomy_modes.py +2013 -2013
- package/md_cg/test_cn_recall_grams.py +206 -206
- package/md_cg/test_code_kind_forbidden.py +101 -0
- package/md_cg/test_condaccept.py +156 -156
- package/md_cg/test_condgate.py +141 -141
- package/md_cg/test_heartbeat_ledger.py +557 -557
- package/md_cg/test_i50e_readside_protection.py +658 -658
- package/md_cg/test_issue111_gate_order.py +76 -76
- package/md_cg/test_issue111_scoring_contract.py +86 -86
- package/md_cg/test_issue121_calibration_bounds.py +293 -293
- package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
- package/md_cg/test_issue147_pack_id_escape.py +378 -378
- package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
- package/md_cg/test_issue149_accel_default_face.py +248 -248
- package/md_cg/test_issue150_empty_query_terms.py +328 -328
- package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
- package/md_cg/test_issue152_sealed_postings.py +309 -309
- package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
- package/md_cg/test_issue157b_index_remainder.py +344 -344
- package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
- package/md_cg/test_issue191_import_trust.py +382 -382
- package/md_cg/test_issue193_write_scope.py +378 -378
- package/md_cg/test_issue212_writeface_gaps.py +349 -349
- package/md_cg/test_issue214_reject_sensitivity.py +266 -266
- package/md_cg/test_issue215_condition_source_gap.py +272 -272
- package/md_cg/test_issue216_clash_routes.py +214 -214
- package/md_cg/test_issue217_slot_routes.py +248 -248
- package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
- package/md_cg/test_issue258_pack_sensitivity.py +468 -468
- package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
- package/md_cg/test_issue274_read_node_id_none.py +341 -341
- package/md_cg/test_issue275_layer_param.py +250 -250
- package/md_cg/test_issue277_node_id_traversal.py +384 -384
- package/md_cg/test_issue278_neg_separated_tension.py +271 -271
- package/md_cg/test_issue278_neg_window.py +294 -294
- package/md_cg/test_issue278_negative_layering.py +83 -83
- package/md_cg/test_issue284_skip_not_pass.py +291 -291
- package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
- package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
- package/md_cg/test_issue303_design_boundaries.py +262 -262
- package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
- package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
- package/md_cg/test_issue321_mcp_deep_json.py +304 -304
- package/md_cg/test_issue322_node_write_lock.py +727 -727
- package/md_cg/test_issue324_write_size_fields.py +214 -214
- package/md_cg/test_issue325_audit_poison.py +229 -229
- package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
- package/md_cg/test_issue326_key_perms.py +355 -355
- package/md_cg/test_issue326_twin_parser_parity.py +323 -323
- package/md_cg/test_issue326_vm_call_frame.py +569 -569
- package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
- package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
- package/md_cg/test_issue348_insight_verify_op.py +199 -199
- package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
- package/md_cg/test_issue350_casefold_collision.py +235 -235
- package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
- package/md_cg/test_issue352_imgaudit.py +418 -418
- package/md_cg/test_issue352_pathsjson.py +537 -537
- package/md_cg/test_issue352_refindex.py +412 -412
- package/md_cg/test_issue352_rollback.py +604 -604
- package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
- package/md_cg/test_issue354_rust_index_containment.py +571 -571
- package/md_cg/test_issue354_rust_wal_containment.py +606 -606
- package/md_cg/test_issue358_truncation_observable.py +247 -247
- package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
- package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
- package/md_cg/test_issue362_neg_condition.py +468 -468
- package/md_cg/test_issue363_lexical_idf.py +275 -275
- package/md_cg/test_issue370_readcache_assembly.py +488 -488
- package/md_cg/test_issue376_heartbeat_failed.py +293 -293
- package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
- package/md_cg/test_issue378_protected_floor.py +310 -310
- package/md_cg/test_issue383_state_event_write_gate.py +462 -462
- package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
- package/md_cg/test_issue398_toolface_op_parity.py +511 -511
- package/md_cg/test_issue399_hive_json_shape.py +317 -317
- package/md_cg/test_issue401_wisdom_codegen.py +295 -295
- package/md_cg/test_issue404_heal_receipt.py +389 -389
- package/md_cg/test_issue409_compact_index_completeness.py +509 -509
- package/md_cg/test_issue410_verify_demote_write.py +366 -366
- package/md_cg/test_issue411_scrub_idempotence.py +575 -575
- package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
- package/md_cg/test_issue413_preimage_failclosed.py +239 -239
- package/md_cg/test_issue413_preimage_twin.py +339 -339
- package/md_cg/test_issue415_deps_gate_probe.py +357 -357
- package/md_cg/test_issue416_entry_key_subset.py +525 -525
- package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
- package/md_cg/test_issue425_ingest_library_path.py +331 -331
- package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
- package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
- package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
- package/md_cg/test_issue427_serve_status_shape.py +191 -191
- package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
- package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
- package/md_cg/test_issue434_435_float_gate.py +90 -90
- package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
- package/md_cg/test_issue43_default_policy.py +874 -874
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
- package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
- package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
- package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
- package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
- package/md_cg/test_issue451_audit_actor.py +313 -313
- package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
- package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
- package/md_cg/test_issue466_basis_registry.py +469 -469
- package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
- package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
- package/md_cg/test_issue470_wal_event_durable.py +427 -427
- package/md_cg/test_issue471_identity_readable.py +136 -136
- package/md_cg/test_issue472_audit_chain.py +90 -90
- package/md_cg/test_issue472_prune_ledger.py +105 -105
- package/md_cg/test_issue472_rollback_degraded.py +81 -81
- package/md_cg/test_issue491_compose_sandbox.py +460 -460
- package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
- package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
- package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
- package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
- package/md_cg/test_issue525_read_null_semantics.py +52 -52
- package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
- package/md_cg/test_issue547_audit_durable.py +96 -96
- package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
- package/md_cg/test_issue559_refine_prefix.py +69 -69
- package/md_cg/test_issue562_retired_faces.py +150 -0
- package/md_cg/test_issue71_queue_backlog.py +376 -376
- package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
- package/md_cg/test_issue82_credential_rules.py +271 -271
- package/md_cg/test_issue91_verifier_sandbox.py +314 -314
- package/md_cg/test_issue93_remember_redaction.py +282 -282
- package/md_cg/test_logref.py +1141 -1141
- package/md_cg/test_md_access_parity.py +330 -330
- package/md_cg/test_md_cg_no_hive_dep.py +139 -139
- package/md_cg/test_mutation_residue.py +119 -119
- package/md_cg/test_mutation_rollback.py +835 -835
- package/md_cg/test_n123_derive_expiry_chain.py +210 -210
- package/md_cg/test_n209_verify_write_face_gates.py +466 -466
- package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
- package/md_cg/test_neg_tail_honesty.py +733 -733
- package/md_cg/test_p21_tokens.py +294 -294
- package/md_cg/test_p27_docindex.py +792 -792
- package/md_cg/test_p28_refcheck.py +1053 -1053
- package/md_cg/test_p2_six_elements.py +476 -476
- package/md_cg/test_p33_ccg_wiring.py +300 -300
- package/md_cg/test_p3_legacy_closure.py +438 -438
- package/md_cg/test_p40_refine_worklist.py +244 -244
- package/md_cg/test_p4_freshness.py +781 -781
- package/md_cg/test_p9c_dedup_hints.py +288 -288
- package/md_cg/test_policy_required_ccg.py +444 -444
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_state_event_op.py +521 -521
- package/md_cg/test_stgidx_index_parity.py +1024 -1024
- package/md_cg/test_sustain_bounded.py +652 -652
- package/md_cg/test_trust_edge_reachability.py +405 -405
- package/md_cg/test_w7_redlines.py +659 -659
- package/md_cg/test_write_return_shape.py +919 -919
- package/md_cg/test_writepipe.py +228 -228
- package/md_cg/trust.py +1114 -1114
- package/md_cg/twophase.py +303 -303
- package/md_cg/whitebox.py +536 -536
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
- package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
- package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
- package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
- package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
- package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
- package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
- package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
- package/md_cg/writepipe.py +1337 -1337
- package/package.json +3 -2
- package/scripts/workspace_index.py +328 -0
- package/src/hooks.ts +30 -6
- package/src/index.ts +14 -5
- package/src/lib/hook_audit.ts +282 -256
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
|
@@ -1,361 +1,361 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# 功能名:工具面 identity_* 写侧密级实参透传对齐守卫(issue #467,族「实参表漏传」)
|
|
3
|
-
# 生效条件:python -u -X utf8 -m md_cg.test_issue467_identity_sensitivity_parity [--mutate];被测面=md_cg/mcp_server.py 的 IDENTITY_SENSITIVITY_PARITY 单点真源表 + _identity_call 的全部 cg.identity_* 调用点 + md_cg/identity.py 的目标函数签名
|
|
4
|
-
# 子功能:A=单点真源表四态闭集 + 正向(调用点实参 ⊇ 表要求;新增未登记即红)+ 反向(表内形参必须在目标签名里真实存在;登记却无对应形参即红)+ 过度收紧腿 + 两条非本族态各自的结构判据;E=枚举完备性自证(_guard_kit 骨架①);R=回退即绿自证(_guard_kit 骨架②)
|
|
5
|
-
# 执行:--mutate 时跑双向变异(M1 回退修复=正向腿红;M2 过度收紧=只有该腿红),再跑 _guard_kit 的回退即绿自证(都是实跑,不接受推演)
|
|
6
|
-
# 验证方式:全部 PASS 即通过(断言总数 ≥ FLOOR);--mutate 下每条变异的红项集合须与**实测**一致
|
|
7
|
-
# 不适用条件:不判「库层该不该让 anchor/trait/contract 也接受 sensitivity」(那是维护者裁定项:三类写入均不接受该形参 ⇒ 调用方无法声明密级、只能落缺省 DEFAULT_SENSITIVITY;可能是 evidence.py 注释记录的同一条链,但定性为**库层缺能力**而非写侧漏传);不判 identity 的业务语义;不覆盖 links/self_state 之外的第三方同名物
|
|
8
|
-
"""issue #467 守卫:工具面 identity_* 写侧的密级实参透传必须由**单点真源**对齐且**双向可核**。
|
|
9
|
-
|
|
10
|
-
病象(修前):cg(op=identity, action=observe) 的实参表漏了 sensitivity —— 而 identity.observe
|
|
11
|
-
**一直有**该形参。漏传不报错、回执逐字相同,调用方声明的密级被静默丢弃、只能落
|
|
12
|
-
DEFAULT_SENSITIVITY=internal。与 #397/#414 同族(实参表漏传)。
|
|
13
|
-
|
|
14
|
-
★**为什么反向断言必须是 AST 签名比对**(不是「我们选了 AST」,而是这条别的路走不通):
|
|
15
|
-
mdcos 的四个 wrapper 都是 **kw 透传形态 ——
|
|
16
|
-
def identity_anchor(self, subject_id, text, **kw): return identity.set_anchor(self, subject_id, text, **kw)
|
|
17
|
-
⇒ 给目标**不存在**该形参的写侧补一个 sensitivity=,**既不会报错、也不会生效**:它经 **kw
|
|
18
|
-
直达目标函数,于是变成 identity.set_anchor(..., sensitivity=...) ⇒ TypeError: unexpected
|
|
19
|
-
keyword argument —— **运行期才炸**,且只在那条路径被走到时炸。
|
|
20
|
-
⇒ 所以「登记了却无对应形参」这类错误:不会被 import 发现、不会被类型检查发现、也不会被
|
|
21
|
-
反射式自检发现(**kw 让签名检查通过)。**唯一能在静态面拦住它的判据,是把表里登记的形参名
|
|
22
|
-
与目标函数的 AST 签名逐名比对。** 这正是 A04 那一条腿存在的理由。
|
|
23
|
-
|
|
24
|
-
维护者若裁定「库层应让 anchor/trait/contract 也能声明密级」:把表里对应条目从
|
|
25
|
-
n/a-no-param 移成 leaked 即可 —— A04 会**立刻**要求先在 identity.py 给那些函数加形参
|
|
26
|
-
(登记了却不存在的形参即红),判据自己把顺序推成「先加能力、再补透传」。
|
|
27
|
-
"""
|
|
28
|
-
from __future__ import annotations
|
|
29
|
-
|
|
30
|
-
import ast
|
|
31
|
-
import io
|
|
32
|
-
import os
|
|
33
|
-
import re
|
|
34
|
-
import shutil
|
|
35
|
-
import subprocess
|
|
36
|
-
import sys
|
|
37
|
-
import tempfile
|
|
38
|
-
|
|
39
|
-
# 沙箱必须在**任何** md_cg 子模块 import 之前求值(crypto.MASTER_FILE 在导入期落点)。
|
|
40
|
-
_SB = tempfile.mkdtemp(prefix="i467_sandbox_")
|
|
41
|
-
os.environ["MDCG_AUX_ROOT"] = _SB
|
|
42
|
-
os.environ["MDCG_ROOT"] = os.path.join(_SB, "root")
|
|
43
|
-
os.environ["MDCG_MASTER_KEY"] = os.urandom(32).hex()
|
|
44
|
-
os.environ.pop("MDCG_POLICY_FILE", None)
|
|
45
|
-
|
|
46
|
-
from . import mcp_server as M # noqa: E402
|
|
47
|
-
import md_cg._guard_kit as _kit # noqa: E402
|
|
48
|
-
|
|
49
|
-
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
50
|
-
FLOOR = 9
|
|
51
|
-
PASS, FAIL = 0, 0
|
|
52
|
-
FAILS = []
|
|
53
|
-
|
|
54
|
-
_DISPOSITIONS = ("leaked", "n/a-no-param", "excluded-nonfamily", "import-unused")
|
|
55
|
-
#: 只读面(无写侧密级透传问题)——正向全枚举的白名单,**每条都要能说出为什么**。
|
|
56
|
-
_READ_ONLY_CALLEES = ("identity_profile", "identity_positions", "identity_history",
|
|
57
|
-
"identity_catalog", "identity_contract_record",
|
|
58
|
-
"identity_contracts")
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
def _check(name, cond, detail=""):
|
|
62
|
-
global PASS, FAIL
|
|
63
|
-
if cond:
|
|
64
|
-
PASS += 1
|
|
65
|
-
print(" PASS " + name)
|
|
66
|
-
else:
|
|
67
|
-
FAIL += 1
|
|
68
|
-
FAILS.append(name)
|
|
69
|
-
print(" FAIL " + name + " " + str(detail)[:240])
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def _read(rel):
|
|
73
|
-
with io.open(os.path.join(_REPO, rel), encoding="utf-8", newline="") as f:
|
|
74
|
-
return f.read()
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
_TBL = M.IDENTITY_SENSITIVITY_PARITY
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def _call_sites(src_text, *, func_names=None):
|
|
81
|
-
"""AST 抽 cg.identity_*(...) 调用点的**显式关键字实参名**。
|
|
82
|
-
|
|
83
|
-
返回 {callee: (显式关键字名 frozenset, 裸 splat 数)}——裸 splat(**kw)会把真源表
|
|
84
|
-
绕开,故单独计数(不可枚举=判红,不能静默放过)。
|
|
85
|
-
"""
|
|
86
|
-
out = {}
|
|
87
|
-
for node in ast.walk(ast.parse(src_text)):
|
|
88
|
-
if not isinstance(node, ast.Call):
|
|
89
|
-
continue
|
|
90
|
-
f = node.func
|
|
91
|
-
if not (isinstance(f, ast.Attribute) and isinstance(f.value, ast.Name)
|
|
92
|
-
and f.value.id == "cg" and f.attr.startswith("identity_")):
|
|
93
|
-
continue
|
|
94
|
-
if func_names is not None and f.attr not in func_names:
|
|
95
|
-
continue
|
|
96
|
-
kws = frozenset(k.arg for k in node.keywords if k.arg)
|
|
97
|
-
splat = sum(1 for k in node.keywords if k.arg is None)
|
|
98
|
-
prev = out.get(f.attr, (frozenset(), 0))
|
|
99
|
-
out[f.attr] = (prev[0] | kws, prev[1] + splat)
|
|
100
|
-
return out
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
_MCP_SRC = _read("md_cg/mcp_server.py")
|
|
104
|
-
_ID_SRC = _read("md_cg/identity.py")
|
|
105
|
-
_CALLS = _call_sites(_MCP_SRC)
|
|
106
|
-
_TOOLFACE = [e for e in _TBL if e.get("callee")]
|
|
107
|
-
_LEAKED = tuple(sorted(e["callee"] for e in _TOOLFACE
|
|
108
|
-
if e["disposition"] == "leaked"))
|
|
109
|
-
#: 正向判据**要求显式传 sensitivity**的调用点集合(单点)。
|
|
110
|
-
#: 正确值 = 表里 disposition=="leaked" 的 callee 集合;A06 断言它**恰为**该集合
|
|
111
|
-
#: —— 把要求扩到 n/a-no-param 的站点,就是「把合法缺省当漏传」的过度收紧。
|
|
112
|
-
_REQUIRE_AT = ("identity_observe",)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def _sig_params(src_text, fname):
|
|
116
|
-
"""identity.py 里 fname 的形参名集合(含 kwonly;*args/**kwargs 不计)。"""
|
|
117
|
-
for node in ast.walk(ast.parse(src_text)):
|
|
118
|
-
if isinstance(node, ast.FunctionDef) and node.name == fname:
|
|
119
|
-
a = node.args
|
|
120
|
-
return set(x.arg for x in list(a.posonlyargs) + list(a.args)
|
|
121
|
-
+ list(a.kwonlyargs))
|
|
122
|
-
return set()
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
print("== A 组:单点真源表 · 双向断言 ==")
|
|
126
|
-
_check("A01 表非空转且 disposition ∈ 四态闭集 + 每条都有非空理由(>=20 字)",
|
|
127
|
-
len(_TBL) >= 5
|
|
128
|
-
and all(e.get("disposition") in _DISPOSITIONS for e in _TBL)
|
|
129
|
-
and all(isinstance(e.get("reason"), str) and len(e["reason"]) >= 20
|
|
130
|
-
for e in _TBL),
|
|
131
|
-
[(e.get("disposition"), e.get("callee") or e.get("site")) for e in _TBL])
|
|
132
|
-
|
|
133
|
-
_unregistered = sorted(set(_CALLS) - {e["callee"] for e in _TOOLFACE}
|
|
134
|
-
- set(_READ_ONLY_CALLEES))
|
|
135
|
-
_check("A02 ★正向(全枚举):AST 扫到的每个 cg.identity_* 调用点,或在真源表内、"
|
|
136
|
-
"或在只读白名单内(新增写侧未登记即红)",
|
|
137
|
-
not _unregistered, _unregistered)
|
|
138
|
-
|
|
139
|
-
_missing = []
|
|
140
|
-
for _e in _TOOLFACE:
|
|
141
|
-
if _e["disposition"] != "leaked":
|
|
142
|
-
continue
|
|
143
|
-
_kws, _splat = _CALLS.get(_e["callee"], (frozenset(), 0))
|
|
144
|
-
_lack = [p for p in _e["params"] if p not in _kws]
|
|
145
|
-
if _lack or _splat:
|
|
146
|
-
_missing.append((_e["callee"], _lack, _splat))
|
|
147
|
-
print(" [读数] A03 调用点实参=%s" % {k: sorted(v[0]) for k, v in _CALLS.items()})
|
|
148
|
-
_check("A03 ★正向(逐条):leaked 条目的调用点必须显式传全部 params(且不得裸 splat"
|
|
149
|
-
"绕开真源表)",
|
|
150
|
-
not _missing, _missing)
|
|
151
|
-
|
|
152
|
-
_ghost = []
|
|
153
|
-
for _e in _TOOLFACE:
|
|
154
|
-
_fn = (_e["target"] or "").split(".")[-1]
|
|
155
|
-
_sig = _sig_params(_ID_SRC, _fn)
|
|
156
|
-
_bad = [p for p in _e["params"] if p not in _sig]
|
|
157
|
-
if _bad or not _sig:
|
|
158
|
-
_ghost.append((_e["callee"], _e["target"], _bad, bool(_sig)))
|
|
159
|
-
_check("A04 ★反向:表里登记的形参名必须在 identity.py 对应函数签名里**真实存在**"
|
|
160
|
-
"(登记了却无对应形参即红 —— 防「给不存在的形参补透传」)",
|
|
161
|
-
not _ghost, _ghost)
|
|
162
|
-
|
|
163
|
-
_nocall = sorted(e["callee"] for e in _TOOLFACE if e["callee"] not in _CALLS)
|
|
164
|
-
_check("A05 ★反向(另一向):表里登记的工具面条目必须真有调用点(登记了却没有即红)",
|
|
165
|
-
not _nocall, _nocall)
|
|
166
|
-
|
|
167
|
-
_widened = sorted(set(_REQUIRE_AT) - set(_LEAKED))
|
|
168
|
-
_check("A06 ★过度收紧腿:要求显式传 sensitivity 的集合**恰为** leaked 集"
|
|
169
|
-
"(把 n/a-no-param 的合法缺省也要求传=误伤)",
|
|
170
|
-
set(_REQUIRE_AT) == set(_LEAKED),
|
|
171
|
-
{"要求集": list(_REQUIRE_AT), "leaked": list(_LEAKED), "多出来的": _widened})
|
|
172
|
-
|
|
173
|
-
_links = _read("md_cg/links.py")
|
|
174
|
-
_links_observe = _sig_params(_links, "observe")
|
|
175
|
-
_check("A07 excluded-nonfamily 腿:links.py 的 observe 确实存在且**不是** "
|
|
176
|
-
"identity.observe(签名含 peer、不含 subject_id)——族枚举按同构判、不按名字收",
|
|
177
|
-
"peer" in _links_observe and "subject_id" not in _links_observe,
|
|
178
|
-
sorted(_links_observe))
|
|
179
|
-
|
|
180
|
-
_ss = _read("md_cg/self_state.py")
|
|
181
|
-
_ss_calls = re.findall(r"identity_(?:observe|anchor|trait|contract)\s*\(", _ss)
|
|
182
|
-
_check("A08 import-unused 腿:self_state.py 只导入 identity 模块、无 identity_* "
|
|
183
|
-
"调用点(⇒ 不适用;后来者不要补不需要的透传)",
|
|
184
|
-
("from . import identity" in _ss) and not _ss_calls, _ss_calls[:4])
|
|
185
|
-
|
|
186
|
-
print("")
|
|
187
|
-
print("== E 组:枚举完备性自证(_guard_kit 骨架①)==")
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
def _discover(texts):
|
|
191
|
-
"""本守卫自己的那条枚举逻辑:合成源码 → 违反「leaked 必须显式传」的调用点。"""
|
|
192
|
-
hits = []
|
|
193
|
-
for rel, src in texts.items():
|
|
194
|
-
for callee, (kws, splat) in _call_sites(src).items():
|
|
195
|
-
if callee not in _LEAKED:
|
|
196
|
-
continue
|
|
197
|
-
if splat or any(p not in kws for p in ("sensitivity",)):
|
|
198
|
-
hits.append((rel, callee))
|
|
199
|
-
return hits
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
_SRC_TMPL = ("def _identity_call(cg, a):\n"
|
|
203
|
-
" return cg.%s(\n"
|
|
204
|
-
" sid, text,\n"
|
|
205
|
-
"%s"
|
|
206
|
-
" )\n")
|
|
207
|
-
_SAMPLES = [
|
|
208
|
-
("①写侧·显式传 sensitivity(应不命中)",
|
|
209
|
-
_SRC_TMPL % ("identity_observe", " sensitivity=a.get(\"sensitivity\"),\n"),
|
|
210
|
-
False, "syn/a.py"),
|
|
211
|
-
("②写侧·漏传 sensitivity(应命中)",
|
|
212
|
-
_SRC_TMPL % ("identity_observe", " override=True,\n"),
|
|
213
|
-
True, "syn/b.py"),
|
|
214
|
-
("③写侧·裸 splat 不可枚举(应命中:绕开真源表)",
|
|
215
|
-
_SRC_TMPL % ("identity_observe", " **a,\n"),
|
|
216
|
-
True, "syn/c.py"),
|
|
217
|
-
("④非 leaked 写侧缺 sensitivity(应不命中:合法缺省,过度收紧方向)",
|
|
218
|
-
_SRC_TMPL % ("identity_anchor", " override=True,\n"),
|
|
219
|
-
False, "syn/d.py"),
|
|
220
|
-
("⑤只读调用(应不命中)",
|
|
221
|
-
"def f(cg):\n return cg.identity_profile(sid)\n",
|
|
222
|
-
False, "syn/e.py"),
|
|
223
|
-
]
|
|
224
|
-
_ok_e, _det_e = _kit.assert_enumeration_complete(_discover, _SAMPLES)
|
|
225
|
-
_check("E01 ★枚举完备性自证:5 类形态喂同一条枚举逻辑,命中与否须全符预期",
|
|
226
|
-
_ok_e, _det_e)
|
|
227
|
-
|
|
228
|
-
print("")
|
|
229
|
-
print("断言总数 %d(PASS %d / FAIL %d),FLOOR=%d" % (PASS + FAIL, PASS, FAIL, FLOOR))
|
|
230
|
-
if PASS + FAIL < FLOOR:
|
|
231
|
-
FAILS.append("FLOOR")
|
|
232
|
-
print(" FAIL 断言总数低于 FLOOR")
|
|
233
|
-
if FAILS:
|
|
234
|
-
print("issue #467 结果:%d 失败" % len(FAILS))
|
|
235
|
-
for _f in FAILS:
|
|
236
|
-
print(" - " + _f)
|
|
237
|
-
else:
|
|
238
|
-
print("issue #467 结果:全部通过")
|
|
239
|
-
|
|
240
|
-
# ---------------------------------------------------------------- 变异/回退
|
|
241
|
-
_MIRROR_DIRS = ("md_cg", "data", "scripts")
|
|
242
|
-
_SKIP = shutil.ignore_patterns("__pycache__", "*.pyc", "jobs", "target",
|
|
243
|
-
"node_modules")
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
def _mirror_build():
|
|
247
|
-
dst = tempfile.mkdtemp(prefix="i467_mut_")
|
|
248
|
-
for name in _MIRROR_DIRS:
|
|
249
|
-
src = os.path.join(_REPO, name)
|
|
250
|
-
if os.path.isdir(src):
|
|
251
|
-
shutil.copytree(src, os.path.join(dst, name), ignore=_SKIP)
|
|
252
|
-
for name in sorted(os.listdir(_REPO)):
|
|
253
|
-
p = os.path.join(_REPO, name)
|
|
254
|
-
if os.path.isfile(p) and name.endswith(".py"):
|
|
255
|
-
shutil.copy2(p, os.path.join(dst, name))
|
|
256
|
-
return dst
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
def _run_in(tree):
|
|
260
|
-
env = dict(os.environ)
|
|
261
|
-
env["PYTHONPATH"] = tree
|
|
262
|
-
env["PYTHONUTF8"] = "1"
|
|
263
|
-
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
264
|
-
pr = subprocess.run([sys.executable, "-u", "-X", "utf8", "-m",
|
|
265
|
-
"md_cg.test_issue467_identity_sensitivity_parity"],
|
|
266
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
267
|
-
errors="replace", cwd=tree, env=env, timeout=1800)
|
|
268
|
-
out = (pr.stdout or "") + (pr.stderr or "")
|
|
269
|
-
reds = [ln.split("FAIL ", 1)[1].strip()
|
|
270
|
-
for ln in out.splitlines() if ln.startswith(" FAIL ")]
|
|
271
|
-
return pr.returncode, reds, out
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
#: 回退 = **该条修复的全部改动**:把 sensitivity 透传那一行摘掉(=修复前的实参表)。
|
|
275
|
-
_REVERT = [("md_cg/mcp_server.py",
|
|
276
|
-
' sensitivity=a.get("sensitivity"),' + chr(10)
|
|
277
|
-
+ ' override=bool(a.get("override")))' + chr(10)
|
|
278
|
-
+ ' if act == "trait":',
|
|
279
|
-
' override=bool(a.get("override")))' + chr(10)
|
|
280
|
-
+ ' if act == "trait":')]
|
|
281
|
-
#: 双向变异:正向腿=回退修复;反向腿=**过度收紧**(把要求扩到全部写侧站点)。
|
|
282
|
-
_MUTATIONS = [
|
|
283
|
-
("M1 正向腿:摘掉 sensitivity 透传(=修复前形态)",
|
|
284
|
-
[("md_cg/mcp_server.py",
|
|
285
|
-
' sensitivity=a.get("sensitivity"),' + chr(10)
|
|
286
|
-
+ ' override=bool(a.get("override")))' + chr(10)
|
|
287
|
-
+ ' if act == "trait":',
|
|
288
|
-
' override=bool(a.get("override")))' + chr(10)
|
|
289
|
-
+ ' if act == "trait":')],
|
|
290
|
-
("A03",)),
|
|
291
|
-
# 反向腿=**过度收紧**:要求集从「leaked 集」扩到「全部写侧站点」——
|
|
292
|
-
# 这正是单点化后最常见的误伤(把 n/a-no-param 的合法缺省当漏传)。
|
|
293
|
-
("M2 反向腿/过度收紧:把要求集扩到全部写侧站点(合法缺省被当漏传)",
|
|
294
|
-
[("md_cg/test_issue467_identity_sensitivity_parity.py",
|
|
295
|
-
'#: —— 把要求扩到 n/a-no-param 的站点,就是「把合法缺省当漏传」的过度收紧。' + chr(10)
|
|
296
|
-
+ '_REQUIRE_AT = ("identity_observe",)',
|
|
297
|
-
'#: —— 把要求扩到 n/a-no-param 的站点,就是「把合法缺省当漏传」的过度收紧。' + chr(10)
|
|
298
|
-
+ '_REQUIRE_AT = ("identity_observe", "identity_anchor", "identity_trait",'
|
|
299
|
-
+ ' "identity_contract")')],
|
|
300
|
-
("A06",)),
|
|
301
|
-
]
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
def _apply(tree, edits):
|
|
305
|
-
for rel, old, new in edits:
|
|
306
|
-
p = os.path.join(tree, rel)
|
|
307
|
-
with io.open(p, encoding="utf-8", newline="") as f:
|
|
308
|
-
t = f.read()
|
|
309
|
-
eol = "\r\n" if "\r\n" in t else "\n"
|
|
310
|
-
o = old.replace("\n", eol)
|
|
311
|
-
if t.count(o) != 1:
|
|
312
|
-
return "锚点命中 %d 次:%s" % (t.count(o), rel)
|
|
313
|
-
with io.open(p, "w", encoding="utf-8", newline="") as f:
|
|
314
|
-
f.write(t.replace(o, new.replace("\n", eol), 1))
|
|
315
|
-
return None
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
def _mutate():
|
|
319
|
-
bad = 0
|
|
320
|
-
tree = _mirror_build()
|
|
321
|
-
try:
|
|
322
|
-
rc, reds, out = _run_in(tree)
|
|
323
|
-
print("!! 镜像基线:rc=%d 红项=%d(须 rc=0/0 红)" % (rc, len(reds)))
|
|
324
|
-
if rc != 0 or reds:
|
|
325
|
-
print(chr(10).join(out.splitlines()[-25:]))
|
|
326
|
-
bad += 1
|
|
327
|
-
for desc, edits, exp in _MUTATIONS:
|
|
328
|
-
miss = _apply(tree, edits)
|
|
329
|
-
if miss:
|
|
330
|
-
print(" ANCHOR-MISS %s —— %s" % (desc, miss))
|
|
331
|
-
bad += 1
|
|
332
|
-
continue
|
|
333
|
-
rc2, reds2, _o2 = _run_in(tree)
|
|
334
|
-
_apply(tree, [(r, n, o) for r, o, n in edits])
|
|
335
|
-
hit = sorted({e for r in reds2 for e in exp if r.startswith(e + " ")})
|
|
336
|
-
if rc2 != 0 and hit == sorted(exp) and len(reds2) == len(exp):
|
|
337
|
-
print(" OK %s 红项=%s" % (desc, hit))
|
|
338
|
-
else:
|
|
339
|
-
print(" MISMATCH %s 实际红项=%s 期望=%s" % (desc, reds2, exp))
|
|
340
|
-
bad += 1
|
|
341
|
-
finally:
|
|
342
|
-
shutil.rmtree(tree, ignore_errors=True)
|
|
343
|
-
return bad
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
def main():
|
|
347
|
-
bad = 0
|
|
348
|
-
if "--mutate" in sys.argv:
|
|
349
|
-
bad += _mutate()
|
|
350
|
-
print("!! 回退即绿自证(_guard_kit.assert_revert_reddens,实跑)")
|
|
351
|
-
_ok, _det = _kit.assert_revert_reddens(_mirror_build, _REVERT, _run_in,
|
|
352
|
-
("A03",), "md_cg/mcp_server.py")
|
|
353
|
-
print(" %s 红项=%s" % ("OK " if _ok else "FAIL", _det.get("实际红项")))
|
|
354
|
-
if not _ok:
|
|
355
|
-
print(" 不合格:" + str(_det)[:400])
|
|
356
|
-
bad += 1
|
|
357
|
-
return 1 if (FAILS or bad) else 0
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
if __name__ == "__main__":
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# 功能名:工具面 identity_* 写侧密级实参透传对齐守卫(issue #467,族「实参表漏传」)
|
|
3
|
+
# 生效条件:python -u -X utf8 -m md_cg.test_issue467_identity_sensitivity_parity [--mutate];被测面=md_cg/mcp_server.py 的 IDENTITY_SENSITIVITY_PARITY 单点真源表 + _identity_call 的全部 cg.identity_* 调用点 + md_cg/identity.py 的目标函数签名
|
|
4
|
+
# 子功能:A=单点真源表四态闭集 + 正向(调用点实参 ⊇ 表要求;新增未登记即红)+ 反向(表内形参必须在目标签名里真实存在;登记却无对应形参即红)+ 过度收紧腿 + 两条非本族态各自的结构判据;E=枚举完备性自证(_guard_kit 骨架①);R=回退即绿自证(_guard_kit 骨架②)
|
|
5
|
+
# 执行:--mutate 时跑双向变异(M1 回退修复=正向腿红;M2 过度收紧=只有该腿红),再跑 _guard_kit 的回退即绿自证(都是实跑,不接受推演)
|
|
6
|
+
# 验证方式:全部 PASS 即通过(断言总数 ≥ FLOOR);--mutate 下每条变异的红项集合须与**实测**一致
|
|
7
|
+
# 不适用条件:不判「库层该不该让 anchor/trait/contract 也接受 sensitivity」(那是维护者裁定项:三类写入均不接受该形参 ⇒ 调用方无法声明密级、只能落缺省 DEFAULT_SENSITIVITY;可能是 evidence.py 注释记录的同一条链,但定性为**库层缺能力**而非写侧漏传);不判 identity 的业务语义;不覆盖 links/self_state 之外的第三方同名物
|
|
8
|
+
"""issue #467 守卫:工具面 identity_* 写侧的密级实参透传必须由**单点真源**对齐且**双向可核**。
|
|
9
|
+
|
|
10
|
+
病象(修前):cg(op=identity, action=observe) 的实参表漏了 sensitivity —— 而 identity.observe
|
|
11
|
+
**一直有**该形参。漏传不报错、回执逐字相同,调用方声明的密级被静默丢弃、只能落
|
|
12
|
+
DEFAULT_SENSITIVITY=internal。与 #397/#414 同族(实参表漏传)。
|
|
13
|
+
|
|
14
|
+
★**为什么反向断言必须是 AST 签名比对**(不是「我们选了 AST」,而是这条别的路走不通):
|
|
15
|
+
mdcos 的四个 wrapper 都是 **kw 透传形态 ——
|
|
16
|
+
def identity_anchor(self, subject_id, text, **kw): return identity.set_anchor(self, subject_id, text, **kw)
|
|
17
|
+
⇒ 给目标**不存在**该形参的写侧补一个 sensitivity=,**既不会报错、也不会生效**:它经 **kw
|
|
18
|
+
直达目标函数,于是变成 identity.set_anchor(..., sensitivity=...) ⇒ TypeError: unexpected
|
|
19
|
+
keyword argument —— **运行期才炸**,且只在那条路径被走到时炸。
|
|
20
|
+
⇒ 所以「登记了却无对应形参」这类错误:不会被 import 发现、不会被类型检查发现、也不会被
|
|
21
|
+
反射式自检发现(**kw 让签名检查通过)。**唯一能在静态面拦住它的判据,是把表里登记的形参名
|
|
22
|
+
与目标函数的 AST 签名逐名比对。** 这正是 A04 那一条腿存在的理由。
|
|
23
|
+
|
|
24
|
+
维护者若裁定「库层应让 anchor/trait/contract 也能声明密级」:把表里对应条目从
|
|
25
|
+
n/a-no-param 移成 leaked 即可 —— A04 会**立刻**要求先在 identity.py 给那些函数加形参
|
|
26
|
+
(登记了却不存在的形参即红),判据自己把顺序推成「先加能力、再补透传」。
|
|
27
|
+
"""
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
import ast
|
|
31
|
+
import io
|
|
32
|
+
import os
|
|
33
|
+
import re
|
|
34
|
+
import shutil
|
|
35
|
+
import subprocess
|
|
36
|
+
import sys
|
|
37
|
+
import tempfile
|
|
38
|
+
|
|
39
|
+
# 沙箱必须在**任何** md_cg 子模块 import 之前求值(crypto.MASTER_FILE 在导入期落点)。
|
|
40
|
+
_SB = tempfile.mkdtemp(prefix="i467_sandbox_")
|
|
41
|
+
os.environ["MDCG_AUX_ROOT"] = _SB
|
|
42
|
+
os.environ["MDCG_ROOT"] = os.path.join(_SB, "root")
|
|
43
|
+
os.environ["MDCG_MASTER_KEY"] = os.urandom(32).hex()
|
|
44
|
+
os.environ.pop("MDCG_POLICY_FILE", None)
|
|
45
|
+
|
|
46
|
+
from . import mcp_server as M # noqa: E402
|
|
47
|
+
import md_cg._guard_kit as _kit # noqa: E402
|
|
48
|
+
|
|
49
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
50
|
+
FLOOR = 9
|
|
51
|
+
PASS, FAIL = 0, 0
|
|
52
|
+
FAILS = []
|
|
53
|
+
|
|
54
|
+
_DISPOSITIONS = ("leaked", "n/a-no-param", "excluded-nonfamily", "import-unused")
|
|
55
|
+
#: 只读面(无写侧密级透传问题)——正向全枚举的白名单,**每条都要能说出为什么**。
|
|
56
|
+
_READ_ONLY_CALLEES = ("identity_profile", "identity_positions", "identity_history",
|
|
57
|
+
"identity_catalog", "identity_contract_record",
|
|
58
|
+
"identity_contracts")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _check(name, cond, detail=""):
|
|
62
|
+
global PASS, FAIL
|
|
63
|
+
if cond:
|
|
64
|
+
PASS += 1
|
|
65
|
+
print(" PASS " + name)
|
|
66
|
+
else:
|
|
67
|
+
FAIL += 1
|
|
68
|
+
FAILS.append(name)
|
|
69
|
+
print(" FAIL " + name + " " + str(detail)[:240])
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _read(rel):
|
|
73
|
+
with io.open(os.path.join(_REPO, rel), encoding="utf-8", newline="") as f:
|
|
74
|
+
return f.read()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
_TBL = M.IDENTITY_SENSITIVITY_PARITY
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _call_sites(src_text, *, func_names=None):
|
|
81
|
+
"""AST 抽 cg.identity_*(...) 调用点的**显式关键字实参名**。
|
|
82
|
+
|
|
83
|
+
返回 {callee: (显式关键字名 frozenset, 裸 splat 数)}——裸 splat(**kw)会把真源表
|
|
84
|
+
绕开,故单独计数(不可枚举=判红,不能静默放过)。
|
|
85
|
+
"""
|
|
86
|
+
out = {}
|
|
87
|
+
for node in ast.walk(ast.parse(src_text)):
|
|
88
|
+
if not isinstance(node, ast.Call):
|
|
89
|
+
continue
|
|
90
|
+
f = node.func
|
|
91
|
+
if not (isinstance(f, ast.Attribute) and isinstance(f.value, ast.Name)
|
|
92
|
+
and f.value.id == "cg" and f.attr.startswith("identity_")):
|
|
93
|
+
continue
|
|
94
|
+
if func_names is not None and f.attr not in func_names:
|
|
95
|
+
continue
|
|
96
|
+
kws = frozenset(k.arg for k in node.keywords if k.arg)
|
|
97
|
+
splat = sum(1 for k in node.keywords if k.arg is None)
|
|
98
|
+
prev = out.get(f.attr, (frozenset(), 0))
|
|
99
|
+
out[f.attr] = (prev[0] | kws, prev[1] + splat)
|
|
100
|
+
return out
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
_MCP_SRC = _read("md_cg/mcp_server.py")
|
|
104
|
+
_ID_SRC = _read("md_cg/identity.py")
|
|
105
|
+
_CALLS = _call_sites(_MCP_SRC)
|
|
106
|
+
_TOOLFACE = [e for e in _TBL if e.get("callee")]
|
|
107
|
+
_LEAKED = tuple(sorted(e["callee"] for e in _TOOLFACE
|
|
108
|
+
if e["disposition"] == "leaked"))
|
|
109
|
+
#: 正向判据**要求显式传 sensitivity**的调用点集合(单点)。
|
|
110
|
+
#: 正确值 = 表里 disposition=="leaked" 的 callee 集合;A06 断言它**恰为**该集合
|
|
111
|
+
#: —— 把要求扩到 n/a-no-param 的站点,就是「把合法缺省当漏传」的过度收紧。
|
|
112
|
+
_REQUIRE_AT = ("identity_observe",)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _sig_params(src_text, fname):
|
|
116
|
+
"""identity.py 里 fname 的形参名集合(含 kwonly;*args/**kwargs 不计)。"""
|
|
117
|
+
for node in ast.walk(ast.parse(src_text)):
|
|
118
|
+
if isinstance(node, ast.FunctionDef) and node.name == fname:
|
|
119
|
+
a = node.args
|
|
120
|
+
return set(x.arg for x in list(a.posonlyargs) + list(a.args)
|
|
121
|
+
+ list(a.kwonlyargs))
|
|
122
|
+
return set()
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
print("== A 组:单点真源表 · 双向断言 ==")
|
|
126
|
+
_check("A01 表非空转且 disposition ∈ 四态闭集 + 每条都有非空理由(>=20 字)",
|
|
127
|
+
len(_TBL) >= 5
|
|
128
|
+
and all(e.get("disposition") in _DISPOSITIONS for e in _TBL)
|
|
129
|
+
and all(isinstance(e.get("reason"), str) and len(e["reason"]) >= 20
|
|
130
|
+
for e in _TBL),
|
|
131
|
+
[(e.get("disposition"), e.get("callee") or e.get("site")) for e in _TBL])
|
|
132
|
+
|
|
133
|
+
_unregistered = sorted(set(_CALLS) - {e["callee"] for e in _TOOLFACE}
|
|
134
|
+
- set(_READ_ONLY_CALLEES))
|
|
135
|
+
_check("A02 ★正向(全枚举):AST 扫到的每个 cg.identity_* 调用点,或在真源表内、"
|
|
136
|
+
"或在只读白名单内(新增写侧未登记即红)",
|
|
137
|
+
not _unregistered, _unregistered)
|
|
138
|
+
|
|
139
|
+
_missing = []
|
|
140
|
+
for _e in _TOOLFACE:
|
|
141
|
+
if _e["disposition"] != "leaked":
|
|
142
|
+
continue
|
|
143
|
+
_kws, _splat = _CALLS.get(_e["callee"], (frozenset(), 0))
|
|
144
|
+
_lack = [p for p in _e["params"] if p not in _kws]
|
|
145
|
+
if _lack or _splat:
|
|
146
|
+
_missing.append((_e["callee"], _lack, _splat))
|
|
147
|
+
print(" [读数] A03 调用点实参=%s" % {k: sorted(v[0]) for k, v in _CALLS.items()})
|
|
148
|
+
_check("A03 ★正向(逐条):leaked 条目的调用点必须显式传全部 params(且不得裸 splat"
|
|
149
|
+
"绕开真源表)",
|
|
150
|
+
not _missing, _missing)
|
|
151
|
+
|
|
152
|
+
_ghost = []
|
|
153
|
+
for _e in _TOOLFACE:
|
|
154
|
+
_fn = (_e["target"] or "").split(".")[-1]
|
|
155
|
+
_sig = _sig_params(_ID_SRC, _fn)
|
|
156
|
+
_bad = [p for p in _e["params"] if p not in _sig]
|
|
157
|
+
if _bad or not _sig:
|
|
158
|
+
_ghost.append((_e["callee"], _e["target"], _bad, bool(_sig)))
|
|
159
|
+
_check("A04 ★反向:表里登记的形参名必须在 identity.py 对应函数签名里**真实存在**"
|
|
160
|
+
"(登记了却无对应形参即红 —— 防「给不存在的形参补透传」)",
|
|
161
|
+
not _ghost, _ghost)
|
|
162
|
+
|
|
163
|
+
_nocall = sorted(e["callee"] for e in _TOOLFACE if e["callee"] not in _CALLS)
|
|
164
|
+
_check("A05 ★反向(另一向):表里登记的工具面条目必须真有调用点(登记了却没有即红)",
|
|
165
|
+
not _nocall, _nocall)
|
|
166
|
+
|
|
167
|
+
_widened = sorted(set(_REQUIRE_AT) - set(_LEAKED))
|
|
168
|
+
_check("A06 ★过度收紧腿:要求显式传 sensitivity 的集合**恰为** leaked 集"
|
|
169
|
+
"(把 n/a-no-param 的合法缺省也要求传=误伤)",
|
|
170
|
+
set(_REQUIRE_AT) == set(_LEAKED),
|
|
171
|
+
{"要求集": list(_REQUIRE_AT), "leaked": list(_LEAKED), "多出来的": _widened})
|
|
172
|
+
|
|
173
|
+
_links = _read("md_cg/links.py")
|
|
174
|
+
_links_observe = _sig_params(_links, "observe")
|
|
175
|
+
_check("A07 excluded-nonfamily 腿:links.py 的 observe 确实存在且**不是** "
|
|
176
|
+
"identity.observe(签名含 peer、不含 subject_id)——族枚举按同构判、不按名字收",
|
|
177
|
+
"peer" in _links_observe and "subject_id" not in _links_observe,
|
|
178
|
+
sorted(_links_observe))
|
|
179
|
+
|
|
180
|
+
_ss = _read("md_cg/self_state.py")
|
|
181
|
+
_ss_calls = re.findall(r"identity_(?:observe|anchor|trait|contract)\s*\(", _ss)
|
|
182
|
+
_check("A08 import-unused 腿:self_state.py 只导入 identity 模块、无 identity_* "
|
|
183
|
+
"调用点(⇒ 不适用;后来者不要补不需要的透传)",
|
|
184
|
+
("from . import identity" in _ss) and not _ss_calls, _ss_calls[:4])
|
|
185
|
+
|
|
186
|
+
print("")
|
|
187
|
+
print("== E 组:枚举完备性自证(_guard_kit 骨架①)==")
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _discover(texts):
|
|
191
|
+
"""本守卫自己的那条枚举逻辑:合成源码 → 违反「leaked 必须显式传」的调用点。"""
|
|
192
|
+
hits = []
|
|
193
|
+
for rel, src in texts.items():
|
|
194
|
+
for callee, (kws, splat) in _call_sites(src).items():
|
|
195
|
+
if callee not in _LEAKED:
|
|
196
|
+
continue
|
|
197
|
+
if splat or any(p not in kws for p in ("sensitivity",)):
|
|
198
|
+
hits.append((rel, callee))
|
|
199
|
+
return hits
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
_SRC_TMPL = ("def _identity_call(cg, a):\n"
|
|
203
|
+
" return cg.%s(\n"
|
|
204
|
+
" sid, text,\n"
|
|
205
|
+
"%s"
|
|
206
|
+
" )\n")
|
|
207
|
+
_SAMPLES = [
|
|
208
|
+
("①写侧·显式传 sensitivity(应不命中)",
|
|
209
|
+
_SRC_TMPL % ("identity_observe", " sensitivity=a.get(\"sensitivity\"),\n"),
|
|
210
|
+
False, "syn/a.py"),
|
|
211
|
+
("②写侧·漏传 sensitivity(应命中)",
|
|
212
|
+
_SRC_TMPL % ("identity_observe", " override=True,\n"),
|
|
213
|
+
True, "syn/b.py"),
|
|
214
|
+
("③写侧·裸 splat 不可枚举(应命中:绕开真源表)",
|
|
215
|
+
_SRC_TMPL % ("identity_observe", " **a,\n"),
|
|
216
|
+
True, "syn/c.py"),
|
|
217
|
+
("④非 leaked 写侧缺 sensitivity(应不命中:合法缺省,过度收紧方向)",
|
|
218
|
+
_SRC_TMPL % ("identity_anchor", " override=True,\n"),
|
|
219
|
+
False, "syn/d.py"),
|
|
220
|
+
("⑤只读调用(应不命中)",
|
|
221
|
+
"def f(cg):\n return cg.identity_profile(sid)\n",
|
|
222
|
+
False, "syn/e.py"),
|
|
223
|
+
]
|
|
224
|
+
_ok_e, _det_e = _kit.assert_enumeration_complete(_discover, _SAMPLES)
|
|
225
|
+
_check("E01 ★枚举完备性自证:5 类形态喂同一条枚举逻辑,命中与否须全符预期",
|
|
226
|
+
_ok_e, _det_e)
|
|
227
|
+
|
|
228
|
+
print("")
|
|
229
|
+
print("断言总数 %d(PASS %d / FAIL %d),FLOOR=%d" % (PASS + FAIL, PASS, FAIL, FLOOR))
|
|
230
|
+
if PASS + FAIL < FLOOR:
|
|
231
|
+
FAILS.append("FLOOR")
|
|
232
|
+
print(" FAIL 断言总数低于 FLOOR")
|
|
233
|
+
if FAILS:
|
|
234
|
+
print("issue #467 结果:%d 失败" % len(FAILS))
|
|
235
|
+
for _f in FAILS:
|
|
236
|
+
print(" - " + _f)
|
|
237
|
+
else:
|
|
238
|
+
print("issue #467 结果:全部通过")
|
|
239
|
+
|
|
240
|
+
# ---------------------------------------------------------------- 变异/回退
|
|
241
|
+
_MIRROR_DIRS = ("md_cg", "data", "scripts")
|
|
242
|
+
_SKIP = shutil.ignore_patterns("__pycache__", "*.pyc", "jobs", "target",
|
|
243
|
+
"node_modules")
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _mirror_build():
|
|
247
|
+
dst = tempfile.mkdtemp(prefix="i467_mut_")
|
|
248
|
+
for name in _MIRROR_DIRS:
|
|
249
|
+
src = os.path.join(_REPO, name)
|
|
250
|
+
if os.path.isdir(src):
|
|
251
|
+
shutil.copytree(src, os.path.join(dst, name), ignore=_SKIP)
|
|
252
|
+
for name in sorted(os.listdir(_REPO)):
|
|
253
|
+
p = os.path.join(_REPO, name)
|
|
254
|
+
if os.path.isfile(p) and name.endswith(".py"):
|
|
255
|
+
shutil.copy2(p, os.path.join(dst, name))
|
|
256
|
+
return dst
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _run_in(tree):
|
|
260
|
+
env = dict(os.environ)
|
|
261
|
+
env["PYTHONPATH"] = tree
|
|
262
|
+
env["PYTHONUTF8"] = "1"
|
|
263
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
264
|
+
pr = subprocess.run([sys.executable, "-u", "-X", "utf8", "-m",
|
|
265
|
+
"md_cg.test_issue467_identity_sensitivity_parity"],
|
|
266
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
267
|
+
errors="replace", cwd=tree, env=env, timeout=1800)
|
|
268
|
+
out = (pr.stdout or "") + (pr.stderr or "")
|
|
269
|
+
reds = [ln.split("FAIL ", 1)[1].strip()
|
|
270
|
+
for ln in out.splitlines() if ln.startswith(" FAIL ")]
|
|
271
|
+
return pr.returncode, reds, out
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
#: 回退 = **该条修复的全部改动**:把 sensitivity 透传那一行摘掉(=修复前的实参表)。
|
|
275
|
+
_REVERT = [("md_cg/mcp_server.py",
|
|
276
|
+
' sensitivity=a.get("sensitivity"),' + chr(10)
|
|
277
|
+
+ ' override=bool(a.get("override")))' + chr(10)
|
|
278
|
+
+ ' if act == "trait":',
|
|
279
|
+
' override=bool(a.get("override")))' + chr(10)
|
|
280
|
+
+ ' if act == "trait":')]
|
|
281
|
+
#: 双向变异:正向腿=回退修复;反向腿=**过度收紧**(把要求扩到全部写侧站点)。
|
|
282
|
+
_MUTATIONS = [
|
|
283
|
+
("M1 正向腿:摘掉 sensitivity 透传(=修复前形态)",
|
|
284
|
+
[("md_cg/mcp_server.py",
|
|
285
|
+
' sensitivity=a.get("sensitivity"),' + chr(10)
|
|
286
|
+
+ ' override=bool(a.get("override")))' + chr(10)
|
|
287
|
+
+ ' if act == "trait":',
|
|
288
|
+
' override=bool(a.get("override")))' + chr(10)
|
|
289
|
+
+ ' if act == "trait":')],
|
|
290
|
+
("A03",)),
|
|
291
|
+
# 反向腿=**过度收紧**:要求集从「leaked 集」扩到「全部写侧站点」——
|
|
292
|
+
# 这正是单点化后最常见的误伤(把 n/a-no-param 的合法缺省当漏传)。
|
|
293
|
+
("M2 反向腿/过度收紧:把要求集扩到全部写侧站点(合法缺省被当漏传)",
|
|
294
|
+
[("md_cg/test_issue467_identity_sensitivity_parity.py",
|
|
295
|
+
'#: —— 把要求扩到 n/a-no-param 的站点,就是「把合法缺省当漏传」的过度收紧。' + chr(10)
|
|
296
|
+
+ '_REQUIRE_AT = ("identity_observe",)',
|
|
297
|
+
'#: —— 把要求扩到 n/a-no-param 的站点,就是「把合法缺省当漏传」的过度收紧。' + chr(10)
|
|
298
|
+
+ '_REQUIRE_AT = ("identity_observe", "identity_anchor", "identity_trait",'
|
|
299
|
+
+ ' "identity_contract")')],
|
|
300
|
+
("A06",)),
|
|
301
|
+
]
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _apply(tree, edits):
|
|
305
|
+
for rel, old, new in edits:
|
|
306
|
+
p = os.path.join(tree, rel)
|
|
307
|
+
with io.open(p, encoding="utf-8", newline="") as f:
|
|
308
|
+
t = f.read()
|
|
309
|
+
eol = "\r\n" if "\r\n" in t else "\n"
|
|
310
|
+
o = old.replace("\n", eol)
|
|
311
|
+
if t.count(o) != 1:
|
|
312
|
+
return "锚点命中 %d 次:%s" % (t.count(o), rel)
|
|
313
|
+
with io.open(p, "w", encoding="utf-8", newline="") as f:
|
|
314
|
+
f.write(t.replace(o, new.replace("\n", eol), 1))
|
|
315
|
+
return None
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def _mutate():
|
|
319
|
+
bad = 0
|
|
320
|
+
tree = _mirror_build()
|
|
321
|
+
try:
|
|
322
|
+
rc, reds, out = _run_in(tree)
|
|
323
|
+
print("!! 镜像基线:rc=%d 红项=%d(须 rc=0/0 红)" % (rc, len(reds)))
|
|
324
|
+
if rc != 0 or reds:
|
|
325
|
+
print(chr(10).join(out.splitlines()[-25:]))
|
|
326
|
+
bad += 1
|
|
327
|
+
for desc, edits, exp in _MUTATIONS:
|
|
328
|
+
miss = _apply(tree, edits)
|
|
329
|
+
if miss:
|
|
330
|
+
print(" ANCHOR-MISS %s —— %s" % (desc, miss))
|
|
331
|
+
bad += 1
|
|
332
|
+
continue
|
|
333
|
+
rc2, reds2, _o2 = _run_in(tree)
|
|
334
|
+
_apply(tree, [(r, n, o) for r, o, n in edits])
|
|
335
|
+
hit = sorted({e for r in reds2 for e in exp if r.startswith(e + " ")})
|
|
336
|
+
if rc2 != 0 and hit == sorted(exp) and len(reds2) == len(exp):
|
|
337
|
+
print(" OK %s 红项=%s" % (desc, hit))
|
|
338
|
+
else:
|
|
339
|
+
print(" MISMATCH %s 实际红项=%s 期望=%s" % (desc, reds2, exp))
|
|
340
|
+
bad += 1
|
|
341
|
+
finally:
|
|
342
|
+
shutil.rmtree(tree, ignore_errors=True)
|
|
343
|
+
return bad
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
def main():
|
|
347
|
+
bad = 0
|
|
348
|
+
if "--mutate" in sys.argv:
|
|
349
|
+
bad += _mutate()
|
|
350
|
+
print("!! 回退即绿自证(_guard_kit.assert_revert_reddens,实跑)")
|
|
351
|
+
_ok, _det = _kit.assert_revert_reddens(_mirror_build, _REVERT, _run_in,
|
|
352
|
+
("A03",), "md_cg/mcp_server.py")
|
|
353
|
+
print(" %s 红项=%s" % ("OK " if _ok else "FAIL", _det.get("实际红项")))
|
|
354
|
+
if not _ok:
|
|
355
|
+
print(" 不合格:" + str(_det)[:400])
|
|
356
|
+
bad += 1
|
|
357
|
+
return 1 if (FAILS or bad) else 0
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
if __name__ == "__main__":
|
|
361
361
|
sys.exit(main())
|