@furongjun1999/dsh-memory 0.9.6 → 0.9.8
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 +150 -662
- package/codebuddy/CODEBUDDY.md +333 -204
- package/data/policy.json +38 -38
- package/docs/README.md +18 -0
- package/docs/discipline/harnesses.yaml +11 -3
- package/docs/discipline/templates/compact.txt.tmpl +20 -18
- package/docs/discipline/templates/full.md.tmpl +63 -61
- package/docs/discipline/templates/rules.mdc.tmpl +5 -3
- package/docs/discipline/templates/skill.md.tmpl +25 -23
- package/docs/discipline/templates/spec.txt.tmpl +2 -2
- package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +30 -7
- package/lib/index.js +18 -3
- package/lib/init.js +19 -4
- package/lib/lib/hook_audit.js +64 -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/sustain.py +10 -1
- 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 +9 -3
- 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_n184_keys_concurrent_provision.py +1 -1
- 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_read_scope_b27.py +2 -2
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_security_audit_v21.py +249 -249
- 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_bad_stamp.py +178 -0
- 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 +932 -908
- package/src/index.ts +21 -6
- package/src/init.ts +16 -4
- package/src/lib/hook_audit.ts +48 -2
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
- package/zcode/AGENTS.md +333 -204
|
@@ -1,349 +1,349 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""issue #212 守卫:工具面写入的四个缺口(本波修缺口 1/3/4;缺口 2 待 #142 口径裁定)。
|
|
3
|
-
|
|
4
|
-
# 功能名:工具面写入的硬上限闸 + 尝试账本总开关 + 尝试账本归档感知读面
|
|
5
|
-
# 生效条件:改动 md_cg/mcp_server.py 的 mdcg_remember 分支、md_cg/writepipe.py 的
|
|
6
|
-
# oversize_response / _append_attempt / attempt_history、md_cg/rotate.py 的
|
|
7
|
-
# Rotator.read_records 之后必跑
|
|
8
|
-
# 子功能:① 工具面按写入硬上限硬拒,且与动词面同源(逐键相等)② 判据边界不误伤
|
|
9
|
-
# (== 上限放行 / 上限+1 拒,单元级直接钉比较算子)③ 尝试账本总开关有效
|
|
10
|
-
# (缺省开;0/false/no/off 停用)且停用不改写入语义——**走动词面测**:工具面
|
|
11
|
-
# 根本不进 writepipe 的 before 链,故它在尝试账本上恒 0 行(缺口 2 的现状,
|
|
12
|
-
# W4e 如实钉住)④ 轮转后读面不归零
|
|
13
|
-
# (归档感知),只读活动文件的旧口径为 0 行 ⑤ 判别力对照(撤掉接线 ⇒ ① 转红)
|
|
14
|
-
# ⑥ 源码级防回退
|
|
15
|
-
# 执行:python -X utf8 -m md_cg.test_issue212_writeface_gaps
|
|
16
|
-
# 验证方式:末行「通过 N / 失败 M」;红基线:修复前 main 上 ①②④ 为红
|
|
17
|
-
# 不适用条件:缺口 2(工具面对两本账零覆盖)**不在本守卫红绿面内**——它需先裁定
|
|
18
|
-
# #142 的两本账口径(工具面出口集与动词面不同,且 test_twophase ⑧2 钉
|
|
19
|
-
# 「未落盘的分支不产生 intent」);不碰在役库(全程 tempfile 隔离)
|
|
20
|
-
# 不测「恰好等于上限的正文真的落盘」——那条路会命中 audit.redact_forbidden
|
|
21
|
-
# 的二次方开销(见下),本守卫不把 16 分钟写进红绿面;比较算子改在单元级
|
|
22
|
-
# 直接钉(W3),接线另在集成级钉(W1 拒 / W3d 放行小正文)。
|
|
23
|
-
|
|
24
|
-
## 缺陷现场(issue #212)
|
|
25
|
-
同一内容走两条通路结论相反:cg(op=write) 被 MAX_WRITE_BYTES 硬拒,mdcg_remember
|
|
26
|
-
却 committed=True 落盘。根因=判据与字段集被写了两份、只有动词面那份被接线。
|
|
27
|
-
本守卫 W2 直接断言「两面响应体(按工具面键集)逐键相等」——这条比「工具面也有
|
|
28
|
-
gate 键」更强:它同时钉住「不是复制出来的第二份字段集」。
|
|
29
|
-
|
|
30
|
-
## 与二次方脱敏的关系(本波实测,另案)
|
|
31
|
-
W3 用单元级钉边界,是因为集成级的「恰好上限」正文会走 audit.redact_forbidden,
|
|
32
|
-
而该函数在**长小写字母串**上二次方退化(实测 64 KiB=3.9s / 256 KiB=61.3s,外推
|
|
33
|
-
1 MiB≈16min;嫌疑模式 = data/policy.json 的
|
|
34
|
-
形如「小写字母打头、[a-z0-9+.-]* 任意长、后接 :// 用户:口令@」的 URL 凭据规则
|
|
35
|
-
——贪心前缀吃掉整段后逐位回溯找 ://)。
|
|
36
|
-
该缺陷**不属于 #212 的四个缺口**(本波如实另案上报,不在本件改动面内)。
|
|
37
|
-
|
|
38
|
-
## 轮转口径(缺口 4,重要)
|
|
39
|
-
本账此前**只有写面、没有任何读面**(全仓对 _write_attempts 的引用只有写入点与
|
|
40
|
-
注释)。W5 的判别口径是「读面不归零」,不是「一条不丢」:超出 keep_shards 的旧
|
|
41
|
-
分片被淘汰是**有界保留=设计**;W5a 同时钉住「只读活动文件的旧口径在轮转后为 0
|
|
42
|
-
行」,故 W5b 非空不是恒真。
|
|
43
|
-
"""
|
|
44
|
-
from __future__ import annotations
|
|
45
|
-
|
|
46
|
-
import io
|
|
47
|
-
import os
|
|
48
|
-
import sys
|
|
49
|
-
import tempfile
|
|
50
|
-
|
|
51
|
-
# 沙箱必须在**任何** md_cg 子模块 import 之前设好(crypto.MASTER_KEY 等在模块导入时
|
|
52
|
-
# 求值)——与 md_cg/test_issue211_export_target.py 同款装置。子目录名不得用 aux /
|
|
53
|
-
# con / prn / nul / com1-9 / lpt1-9:Windows 保留设备名,makedirs 会抛 WinError 267。
|
|
54
|
-
_SANDBOX = tempfile.mkdtemp(prefix="i212_sandbox_")
|
|
55
|
-
os.environ["MDCG_AUX_ROOT"] = os.path.join(_SANDBOX, "auxstore")
|
|
56
|
-
os.environ["MDCG_ROOT"] = os.path.join(_SANDBOX, "libroot")
|
|
57
|
-
os.environ["MDCG_MASTER_KEY"] = os.urandom(32).hex()
|
|
58
|
-
os.environ.pop("MDCG_EXPORT_ROOT", None)
|
|
59
|
-
os.environ.pop("MDCG_TEST_LIVE_ROOT", None)
|
|
60
|
-
os.environ.pop("MDCG_WRITE_ATTEMPT_LEDGER", None)
|
|
61
|
-
os.makedirs(os.environ["MDCG_AUX_ROOT"], exist_ok=True)
|
|
62
|
-
os.makedirs(os.environ["MDCG_ROOT"], exist_ok=True)
|
|
63
|
-
|
|
64
|
-
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
65
|
-
|
|
66
|
-
if hasattr(sys.stdout, "reconfigure"):
|
|
67
|
-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
68
|
-
|
|
69
|
-
from md_cg import mcp_server, protocol, writepipe # noqa: E402
|
|
70
|
-
from md_cg.fsutil import read_jsonl # noqa: E402
|
|
71
|
-
from md_cg.mdcos import MdCGOS # noqa: E402
|
|
72
|
-
from md_cg.rotate import Rotator # noqa: E402
|
|
73
|
-
|
|
74
|
-
_HERE = os.path.dirname(os.path.abspath(__file__))
|
|
75
|
-
_PASS = []
|
|
76
|
-
_FAIL = []
|
|
77
|
-
_RUN = [0]
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def ok(cond, msg, extra=''):
|
|
81
|
-
(_PASS if cond else _FAIL).append(msg)
|
|
82
|
-
print((' PASS ' if cond else ' FAIL ') + msg
|
|
83
|
-
+ ((' <- ' + str(extra)) if (extra and not cond) else ''))
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
def _lib(tag):
|
|
87
|
-
_RUN[0] += 1
|
|
88
|
-
return MdCGOS(os.path.join(_SANDBOX, '%s_r%d' % (tag, _RUN[0])), autoflush=0)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
BODY = '# 功能名:%s' + chr(10) + chr(10) + '# 生效条件:PASSED' + chr(10)
|
|
92
|
-
LIMIT = writepipe.MAX_WRITE_BYTES
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def _args(text, nid):
|
|
96
|
-
return {'content_kind': 'text', 'content': text, 'layer': 'knowledge',
|
|
97
|
-
'node_id': nid, 'verification_basis': 'test'}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
def _ccg(tag):
|
|
101
|
-
"""动词面会过 audit 的 required 规则(CCG 六要素),故**动词面夹具必须齐六行**;
|
|
102
|
-
工具面不进审核链,短文即可。W4 用本函数,正是为了让「账本 +1」测的是开关而不是
|
|
103
|
-
审核结论(用三行短文会被 REJECT,于是账本行数不增,测出来的红是夹具缺陷)。"""
|
|
104
|
-
# 六行**取值互不相同**:一致性的自否定判据会拿「不适用条件」去比「生效条件/正文」,
|
|
105
|
-
# 六行都写同一个哑值会被判「条件自相矛盾」转 review_queue(本守卫初版即踩此坑)。
|
|
106
|
-
return ('# 功能名:%s' % tag + chr(10)
|
|
107
|
-
+ '# 生效条件:当尝试账本开关未显式停用时' + chr(10)
|
|
108
|
-
+ '# 子功能:尝试账本的记账与开关' + chr(10)
|
|
109
|
-
+ '# 执行:进程内 call_tool 写一条正文' + chr(10)
|
|
110
|
-
+ '# 验证方式:比对账本行数差' + chr(10)
|
|
111
|
-
+ '# 不适用条件:不涉及轮转与归档分片' + chr(10))
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def _pad(n):
|
|
115
|
-
"""造**恰好 n 字节**的正文。填充用大写字母:小写长串会命中
|
|
116
|
-
audit.redact_forbidden 的二次方退化(见模块 docstring),本守卫不测那个。"""
|
|
117
|
-
head = BODY % 'p'
|
|
118
|
-
return head + ('Z' * (n - len(head.encode('utf-8'))))
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
def tool_write(cg, text, nid):
|
|
122
|
-
return mcp_server.call_tool(cg, 'mdcg_remember', _args(text, nid))
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
def verb_write(cg, text, nid):
|
|
126
|
-
return mcp_server.call_tool(cg, 'cg', dict(op='write', **_args(text, nid)))
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
def _rows(cg):
|
|
130
|
-
p = os.path.join(cg.root, writepipe.ATTEMPT_LOG_FILE)
|
|
131
|
-
if not os.path.isfile(p):
|
|
132
|
-
return 0
|
|
133
|
-
with io.open(p, encoding='utf-8') as f:
|
|
134
|
-
return sum(1 for l in f if l.strip())
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
def _need(shape):
|
|
138
|
-
return set(protocol.VERB_SPECS['write']['shapes'][shape]['required'])
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
def _env_guard(key):
|
|
142
|
-
old = os.environ.get(key)
|
|
143
|
-
os.environ.pop(key, None)
|
|
144
|
-
return old
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def _env_restore(key, old):
|
|
148
|
-
if old is None:
|
|
149
|
-
os.environ.pop(key, None)
|
|
150
|
-
else:
|
|
151
|
-
os.environ[key] = old
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
def w1_tool_face_hard_limit():
|
|
155
|
-
cg = _lib('w1')
|
|
156
|
-
big = _pad(LIMIT + 8)
|
|
157
|
-
o = tool_write(cg, big, 'w1_big')
|
|
158
|
-
ok(o.get('committed') is False,
|
|
159
|
-
'W1a 工具面超限 => committed is False(修复前为 True)', o.get('committed'))
|
|
160
|
-
ok(o.get('gate') == 'writelimit_size',
|
|
161
|
-
'W1b 工具面超限 => gate=writelimit_size', o.get('gate'))
|
|
162
|
-
miss = [k for k in protocol.WRITE_TRIO if k not in o]
|
|
163
|
-
ok(not miss, 'W1c 工具面超限返回体含三键(ok/id/committed)', sorted(o))
|
|
164
|
-
need = _need('gate_hard_reject')
|
|
165
|
-
ok(not [k for k in need if k not in o],
|
|
166
|
-
'W1d 工具面超限返回体命中协议形态 gate_hard_reject', sorted(o))
|
|
167
|
-
ok('w1_big' not in cg.index['nodes'],
|
|
168
|
-
'W1e 超限内容未落盘(索引里无该节点)', list(cg.index['nodes'])[:5])
|
|
169
|
-
ok(o.get('bytes') == len(big.encode('utf-8')) and o.get('limit') == LIMIT,
|
|
170
|
-
'W1f 读数自洽:bytes = 正文实际字节数,limit = 上限',
|
|
171
|
-
(o.get('bytes'), len(big.encode('utf-8')), o.get('limit')))
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
def w2_two_faces_identical():
|
|
175
|
-
"""两条通路必须给**同一个**响应体(按工具面键集)——比「工具面也有 gate 键」
|
|
176
|
-
更强:它同时钉住「不是复制出来的第二份字段集」。
|
|
177
|
-
动词面外层多一个 action_source(动词面信封),故按工具面键集取子集比对。"""
|
|
178
|
-
cg = _lib('w2')
|
|
179
|
-
big = _pad(LIMIT + 8)
|
|
180
|
-
o_t = tool_write(cg, big, 'same_id')
|
|
181
|
-
o_v = verb_write(cg, big, 'same_id')
|
|
182
|
-
sub = {k: o_v[k] for k in o_t if k in o_v}
|
|
183
|
-
ok(o_t == sub,
|
|
184
|
-
'W2a 同内容 + 同 id:工具面响应体 == 动词面对应键集(同源,非复制)',
|
|
185
|
-
(sorted(o_t), sorted(o_v), o_t.get('gate'), o_v.get('gate')))
|
|
186
|
-
ok(set(o_t) <= set(o_v),
|
|
187
|
-
'W2b 工具面键集是动词面键集的子集(动词面只多信封键)',
|
|
188
|
-
sorted(set(o_t) - set(o_v)))
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
def w3_boundary_no_false_positive():
|
|
192
|
-
"""比较算子(> 上限 才拒)在**唯一构造点**上直接钉——不落盘,故不触发
|
|
193
|
-
redact_forbidden 的二次方开销。"""
|
|
194
|
-
a = {'content': _pad(LIMIT), 'content_kind': 'text'}
|
|
195
|
-
ok(writepipe.oversize_response('n', a) is None,
|
|
196
|
-
'W3a 恰好 == 上限(%d 字节)=> 放行(比较算子是 >,不误伤)' % LIMIT,
|
|
197
|
-
writepipe.oversize_response('n', a))
|
|
198
|
-
a2 = {'content': _pad(LIMIT + 1), 'content_kind': 'text'}
|
|
199
|
-
r2 = writepipe.oversize_response('n', a2)
|
|
200
|
-
ok(isinstance(r2, dict) and r2.get('committed') is False,
|
|
201
|
-
'W3b 上限 +1 字节 => 拒', r2)
|
|
202
|
-
ok(r2 and r2.get('bytes') == LIMIT + 1,
|
|
203
|
-
'W3c 上限 +1 的 bytes 读数 = %d' % (LIMIT + 1), r2 and r2.get('bytes'))
|
|
204
|
-
cg = _lib('w3')
|
|
205
|
-
small = BODY % '小正文'
|
|
206
|
-
o = tool_write(cg, small, 'w3_small')
|
|
207
|
-
ok(o.get('committed') is True,
|
|
208
|
-
'W3d 小正文仍正常落盘(补闸未误伤常规写入)', o)
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
def w4_ledger_switch():
|
|
212
|
-
cg = _lib('w4')
|
|
213
|
-
key = writepipe.ATTEMPT_LEDGER_ENV
|
|
214
|
-
old = _env_guard(key)
|
|
215
|
-
try:
|
|
216
|
-
b = _rows(cg)
|
|
217
|
-
o = verb_write(cg, _ccg('开关未设'), 'w4_a')
|
|
218
|
-
ok(_rows(cg) - b == 1 and o.get('committed') is True,
|
|
219
|
-
'W4a 未设 %s => 账本 +1 行(缺省开,与修复前逐位一致)' % key,
|
|
220
|
-
_rows(cg) - b)
|
|
221
|
-
for i, val in enumerate(('0', 'false', 'no', 'off', 'OFF', ' false ')):
|
|
222
|
-
os.environ[key] = val
|
|
223
|
-
b = _rows(cg)
|
|
224
|
-
o = verb_write(cg, _ccg('开关' + str(i)), 'w4_off%d' % i)
|
|
225
|
-
ok(_rows(cg) - b == 0,
|
|
226
|
-
'W4b %s=%r => 账本 +0 行(修复前该 env 无效、仍写 1 行)' % (key, val),
|
|
227
|
-
_rows(cg) - b)
|
|
228
|
-
ok(o.get('committed') is True,
|
|
229
|
-
'W4c %s=%r 停用只停留痕、不改写入语义' % (key, val), o)
|
|
230
|
-
os.environ[key] = '1'
|
|
231
|
-
b = _rows(cg)
|
|
232
|
-
verb_write(cg, _ccg('开关1'), 'w4_one')
|
|
233
|
-
ok(_rows(cg) - b == 1, 'W4d %s=1 => 账本 +1 行' % key, _rows(cg) - b)
|
|
234
|
-
# 如实钉住**缺口 2 的现状**(本件不修):工具面根本不进 writepipe 的 before
|
|
235
|
-
# 链,故它的写入在尝试账本上是 0 行。若将来 #142 的伞状裁定把工具面也接进
|
|
236
|
-
# 记账面,本项会转红——那正是「把断言升级为『两面一致』」的信号(同
|
|
237
|
-
# md_cg/test_write_return_shape.py 的 H2/H3「现状记录·待裁定」惯例)。
|
|
238
|
-
b = _rows(cg)
|
|
239
|
-
tool_write(cg, BODY % '工具面不记账', 'w4_tool')
|
|
240
|
-
ok(_rows(cg) - b == 0,
|
|
241
|
-
'W4e 现状(缺口 2,待 #142 裁定):工具面写入不产生尝试账本行',
|
|
242
|
-
_rows(cg) - b)
|
|
243
|
-
finally:
|
|
244
|
-
_env_restore(key, old)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
def w5_history_survives_rotation():
|
|
248
|
-
"""判别口径是「读面**不归零**」,不是「一条不丢」:超出 keep_shards 的旧分片
|
|
249
|
-
被淘汰是有界保留=设计。W5a 同时钉住「只读活动文件的旧口径在轮转后为 0 行」,
|
|
250
|
-
故 W5b 非空不是恒真。"""
|
|
251
|
-
cg = _lib('w5')
|
|
252
|
-
key = writepipe.ATTEMPT_LEDGER_ENV
|
|
253
|
-
old = _env_guard(key)
|
|
254
|
-
try:
|
|
255
|
-
rot = Rotator(cg.root, writepipe.ATTEMPT_BASENAME,
|
|
256
|
-
writepipe.ATTEMPT_ARCHIVE_DIR,
|
|
257
|
-
rotate_bytes=1, keep_shards=2)
|
|
258
|
-
for k in range(6):
|
|
259
|
-
for j in range(6):
|
|
260
|
-
writepipe._append_attempt(cg, 'w5_%d_%d' % (k, j),
|
|
261
|
-
{'content_kind': 'text',
|
|
262
|
-
'layer': 'knowledge'},
|
|
263
|
-
writepipe.ATTEMPT_COMMITTED,
|
|
264
|
-
out={'id': 'x'})
|
|
265
|
-
r = rot.rotate()
|
|
266
|
-
ok(r is not None, 'W5 第 %d 次轮转切出分片' % (k + 1), r)
|
|
267
|
-
active_only = list(read_jsonl(os.path.join(cg.root,
|
|
268
|
-
writepipe.ATTEMPT_LOG_FILE)))
|
|
269
|
-
hist = writepipe.attempt_history(cg, limit=0)
|
|
270
|
-
ok(len(active_only) == 0,
|
|
271
|
-
'W5a 只读活动文件的旧口径:轮转后 0 行(缺陷现场)', len(active_only))
|
|
272
|
-
ok(len(hist) > 0,
|
|
273
|
-
'W5b 归档感知读面:轮转后**不归零**', len(hist))
|
|
274
|
-
ok(len(hist) == 12,
|
|
275
|
-
'W5c 读面 = 存活分片(keep_shards=2 × 6 行)+ 活动文件', len(hist))
|
|
276
|
-
ok([r['id'] for r in hist][-1] == 'w5_5_5',
|
|
277
|
-
'W5d 时间序正确(末条 = 最后一次写入)', [r['id'] for r in hist][-1:])
|
|
278
|
-
ok(len(writepipe.attempt_history(cg, limit=3)) == 3,
|
|
279
|
-
'W5e limit 生效(取尾部 3 条)',
|
|
280
|
-
len(writepipe.attempt_history(cg, limit=3)))
|
|
281
|
-
finally:
|
|
282
|
-
_env_restore(key, old)
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
def w6_discriminator():
|
|
286
|
-
"""判别力对照:把唯一构造点撤成 no-op(等价修复前形态)⇒ 工具面又落盘成功。
|
|
287
|
-
证明 W1 不是恒真。"""
|
|
288
|
-
cg = _lib('w6')
|
|
289
|
-
big = _pad(LIMIT + 8)
|
|
290
|
-
real = writepipe.oversize_response
|
|
291
|
-
try:
|
|
292
|
-
writepipe.oversize_response = lambda nid, a: None
|
|
293
|
-
o = tool_write(cg, big, 'w6_big')
|
|
294
|
-
ok(o.get('committed') is True,
|
|
295
|
-
'W6a 撤掉接线 => 工具面又落盘成功(W1 有判别力)', o)
|
|
296
|
-
finally:
|
|
297
|
-
writepipe.oversize_response = real
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
def w7_source_no_regress():
|
|
301
|
-
src = io.open(os.path.join(_HERE, 'mcp_server.py'), encoding='utf-8').read()
|
|
302
|
-
i = src.find('if name == "mdcg_remember":')
|
|
303
|
-
j = src.find('if name == "mdcg_recall":')
|
|
304
|
-
block = src[i:j] if (i >= 0 and j > i) else ''
|
|
305
|
-
ok('oversize_response' in block,
|
|
306
|
-
'W7a 工具面分支仍调用动词面的同一实现(防回退到「各写一份判据」)',
|
|
307
|
-
'(本项红说明接线被撤)')
|
|
308
|
-
ws = io.open(os.path.join(_HERE, 'writepipe.py'), encoding='utf-8').read()
|
|
309
|
-
ok('def oversize_response' in ws,
|
|
310
|
-
'W7b writepipe 仍有唯一构造点 oversize_response')
|
|
311
|
-
ok('def attempt_history' in ws,
|
|
312
|
-
'W7c writepipe 仍有归档感知读面 attempt_history')
|
|
313
|
-
ok('def read_records' in io.open(os.path.join(_HERE, 'rotate.py'),
|
|
314
|
-
encoding='utf-8').read(),
|
|
315
|
-
'W7d Rotator 仍有归档感知读原语 read_records')
|
|
316
|
-
ok(writepipe.ATTEMPT_LEDGER_ENV == 'MDCG_WRITE_ATTEMPT_LEDGER',
|
|
317
|
-
'W7e 开关名就是 issue #212 点名的那一个(不是另起新名)',
|
|
318
|
-
writepipe.ATTEMPT_LEDGER_ENV)
|
|
319
|
-
ok(writepipe.oversize_response('n', {'content': 'x'}) is None,
|
|
320
|
-
'W7f 唯一构造点可直接调用(两面的共用入口没被私有化)')
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
_CASES = (w1_tool_face_hard_limit, w2_two_faces_identical,
|
|
324
|
-
w3_boundary_no_false_positive, w4_ledger_switch,
|
|
325
|
-
w5_history_survives_rotation, w6_discriminator,
|
|
326
|
-
w7_source_no_regress)
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
def _run_all():
|
|
330
|
-
del _PASS[:]
|
|
331
|
-
del _FAIL[:]
|
|
332
|
-
for fn in _CASES:
|
|
333
|
-
print(chr(10) + '== %s' % fn.__name__)
|
|
334
|
-
try:
|
|
335
|
-
fn()
|
|
336
|
-
except Exception as exc: # noqa: BLE001
|
|
337
|
-
import traceback
|
|
338
|
-
_FAIL.append('%s 抛异常' % fn.__name__)
|
|
339
|
-
print(' FAIL %s 抛异常:%s: %s'
|
|
340
|
-
% (fn.__name__, type(exc).__name__, exc))
|
|
341
|
-
traceback.print_exc()
|
|
342
|
-
print(chr(10) + '通过 %d / 失败 %d' % (len(_PASS), len(_FAIL)))
|
|
343
|
-
for m in _FAIL:
|
|
344
|
-
print(' - ' + m)
|
|
345
|
-
return 1 if _FAIL else 0
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if __name__ == '__main__':
|
|
349
|
-
raise SystemExit(_run_all())
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #212 守卫:工具面写入的四个缺口(本波修缺口 1/3/4;缺口 2 待 #142 口径裁定)。
|
|
3
|
+
|
|
4
|
+
# 功能名:工具面写入的硬上限闸 + 尝试账本总开关 + 尝试账本归档感知读面
|
|
5
|
+
# 生效条件:改动 md_cg/mcp_server.py 的 mdcg_remember 分支、md_cg/writepipe.py 的
|
|
6
|
+
# oversize_response / _append_attempt / attempt_history、md_cg/rotate.py 的
|
|
7
|
+
# Rotator.read_records 之后必跑
|
|
8
|
+
# 子功能:① 工具面按写入硬上限硬拒,且与动词面同源(逐键相等)② 判据边界不误伤
|
|
9
|
+
# (== 上限放行 / 上限+1 拒,单元级直接钉比较算子)③ 尝试账本总开关有效
|
|
10
|
+
# (缺省开;0/false/no/off 停用)且停用不改写入语义——**走动词面测**:工具面
|
|
11
|
+
# 根本不进 writepipe 的 before 链,故它在尝试账本上恒 0 行(缺口 2 的现状,
|
|
12
|
+
# W4e 如实钉住)④ 轮转后读面不归零
|
|
13
|
+
# (归档感知),只读活动文件的旧口径为 0 行 ⑤ 判别力对照(撤掉接线 ⇒ ① 转红)
|
|
14
|
+
# ⑥ 源码级防回退
|
|
15
|
+
# 执行:python -X utf8 -m md_cg.test_issue212_writeface_gaps
|
|
16
|
+
# 验证方式:末行「通过 N / 失败 M」;红基线:修复前 main 上 ①②④ 为红
|
|
17
|
+
# 不适用条件:缺口 2(工具面对两本账零覆盖)**不在本守卫红绿面内**——它需先裁定
|
|
18
|
+
# #142 的两本账口径(工具面出口集与动词面不同,且 test_twophase ⑧2 钉
|
|
19
|
+
# 「未落盘的分支不产生 intent」);不碰在役库(全程 tempfile 隔离)
|
|
20
|
+
# 不测「恰好等于上限的正文真的落盘」——那条路会命中 audit.redact_forbidden
|
|
21
|
+
# 的二次方开销(见下),本守卫不把 16 分钟写进红绿面;比较算子改在单元级
|
|
22
|
+
# 直接钉(W3),接线另在集成级钉(W1 拒 / W3d 放行小正文)。
|
|
23
|
+
|
|
24
|
+
## 缺陷现场(issue #212)
|
|
25
|
+
同一内容走两条通路结论相反:cg(op=write) 被 MAX_WRITE_BYTES 硬拒,mdcg_remember
|
|
26
|
+
却 committed=True 落盘。根因=判据与字段集被写了两份、只有动词面那份被接线。
|
|
27
|
+
本守卫 W2 直接断言「两面响应体(按工具面键集)逐键相等」——这条比「工具面也有
|
|
28
|
+
gate 键」更强:它同时钉住「不是复制出来的第二份字段集」。
|
|
29
|
+
|
|
30
|
+
## 与二次方脱敏的关系(本波实测,另案)
|
|
31
|
+
W3 用单元级钉边界,是因为集成级的「恰好上限」正文会走 audit.redact_forbidden,
|
|
32
|
+
而该函数在**长小写字母串**上二次方退化(实测 64 KiB=3.9s / 256 KiB=61.3s,外推
|
|
33
|
+
1 MiB≈16min;嫌疑模式 = data/policy.json 的
|
|
34
|
+
形如「小写字母打头、[a-z0-9+.-]* 任意长、后接 :// 用户:口令@」的 URL 凭据规则
|
|
35
|
+
——贪心前缀吃掉整段后逐位回溯找 ://)。
|
|
36
|
+
该缺陷**不属于 #212 的四个缺口**(本波如实另案上报,不在本件改动面内)。
|
|
37
|
+
|
|
38
|
+
## 轮转口径(缺口 4,重要)
|
|
39
|
+
本账此前**只有写面、没有任何读面**(全仓对 _write_attempts 的引用只有写入点与
|
|
40
|
+
注释)。W5 的判别口径是「读面不归零」,不是「一条不丢」:超出 keep_shards 的旧
|
|
41
|
+
分片被淘汰是**有界保留=设计**;W5a 同时钉住「只读活动文件的旧口径在轮转后为 0
|
|
42
|
+
行」,故 W5b 非空不是恒真。
|
|
43
|
+
"""
|
|
44
|
+
from __future__ import annotations
|
|
45
|
+
|
|
46
|
+
import io
|
|
47
|
+
import os
|
|
48
|
+
import sys
|
|
49
|
+
import tempfile
|
|
50
|
+
|
|
51
|
+
# 沙箱必须在**任何** md_cg 子模块 import 之前设好(crypto.MASTER_KEY 等在模块导入时
|
|
52
|
+
# 求值)——与 md_cg/test_issue211_export_target.py 同款装置。子目录名不得用 aux /
|
|
53
|
+
# con / prn / nul / com1-9 / lpt1-9:Windows 保留设备名,makedirs 会抛 WinError 267。
|
|
54
|
+
_SANDBOX = tempfile.mkdtemp(prefix="i212_sandbox_")
|
|
55
|
+
os.environ["MDCG_AUX_ROOT"] = os.path.join(_SANDBOX, "auxstore")
|
|
56
|
+
os.environ["MDCG_ROOT"] = os.path.join(_SANDBOX, "libroot")
|
|
57
|
+
os.environ["MDCG_MASTER_KEY"] = os.urandom(32).hex()
|
|
58
|
+
os.environ.pop("MDCG_EXPORT_ROOT", None)
|
|
59
|
+
os.environ.pop("MDCG_TEST_LIVE_ROOT", None)
|
|
60
|
+
os.environ.pop("MDCG_WRITE_ATTEMPT_LEDGER", None)
|
|
61
|
+
os.makedirs(os.environ["MDCG_AUX_ROOT"], exist_ok=True)
|
|
62
|
+
os.makedirs(os.environ["MDCG_ROOT"], exist_ok=True)
|
|
63
|
+
|
|
64
|
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
65
|
+
|
|
66
|
+
if hasattr(sys.stdout, "reconfigure"):
|
|
67
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
68
|
+
|
|
69
|
+
from md_cg import mcp_server, protocol, writepipe # noqa: E402
|
|
70
|
+
from md_cg.fsutil import read_jsonl # noqa: E402
|
|
71
|
+
from md_cg.mdcos import MdCGOS # noqa: E402
|
|
72
|
+
from md_cg.rotate import Rotator # noqa: E402
|
|
73
|
+
|
|
74
|
+
_HERE = os.path.dirname(os.path.abspath(__file__))
|
|
75
|
+
_PASS = []
|
|
76
|
+
_FAIL = []
|
|
77
|
+
_RUN = [0]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def ok(cond, msg, extra=''):
|
|
81
|
+
(_PASS if cond else _FAIL).append(msg)
|
|
82
|
+
print((' PASS ' if cond else ' FAIL ') + msg
|
|
83
|
+
+ ((' <- ' + str(extra)) if (extra and not cond) else ''))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _lib(tag):
|
|
87
|
+
_RUN[0] += 1
|
|
88
|
+
return MdCGOS(os.path.join(_SANDBOX, '%s_r%d' % (tag, _RUN[0])), autoflush=0)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
BODY = '# 功能名:%s' + chr(10) + chr(10) + '# 生效条件:PASSED' + chr(10)
|
|
92
|
+
LIMIT = writepipe.MAX_WRITE_BYTES
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _args(text, nid):
|
|
96
|
+
return {'content_kind': 'text', 'content': text, 'layer': 'knowledge',
|
|
97
|
+
'node_id': nid, 'verification_basis': 'test'}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _ccg(tag):
|
|
101
|
+
"""动词面会过 audit 的 required 规则(CCG 六要素),故**动词面夹具必须齐六行**;
|
|
102
|
+
工具面不进审核链,短文即可。W4 用本函数,正是为了让「账本 +1」测的是开关而不是
|
|
103
|
+
审核结论(用三行短文会被 REJECT,于是账本行数不增,测出来的红是夹具缺陷)。"""
|
|
104
|
+
# 六行**取值互不相同**:一致性的自否定判据会拿「不适用条件」去比「生效条件/正文」,
|
|
105
|
+
# 六行都写同一个哑值会被判「条件自相矛盾」转 review_queue(本守卫初版即踩此坑)。
|
|
106
|
+
return ('# 功能名:%s' % tag + chr(10)
|
|
107
|
+
+ '# 生效条件:当尝试账本开关未显式停用时' + chr(10)
|
|
108
|
+
+ '# 子功能:尝试账本的记账与开关' + chr(10)
|
|
109
|
+
+ '# 执行:进程内 call_tool 写一条正文' + chr(10)
|
|
110
|
+
+ '# 验证方式:比对账本行数差' + chr(10)
|
|
111
|
+
+ '# 不适用条件:不涉及轮转与归档分片' + chr(10))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _pad(n):
|
|
115
|
+
"""造**恰好 n 字节**的正文。填充用大写字母:小写长串会命中
|
|
116
|
+
audit.redact_forbidden 的二次方退化(见模块 docstring),本守卫不测那个。"""
|
|
117
|
+
head = BODY % 'p'
|
|
118
|
+
return head + ('Z' * (n - len(head.encode('utf-8'))))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def tool_write(cg, text, nid):
|
|
122
|
+
return mcp_server.call_tool(cg, 'mdcg_remember', _args(text, nid))
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def verb_write(cg, text, nid):
|
|
126
|
+
return mcp_server.call_tool(cg, 'cg', dict(op='write', **_args(text, nid)))
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _rows(cg):
|
|
130
|
+
p = os.path.join(cg.root, writepipe.ATTEMPT_LOG_FILE)
|
|
131
|
+
if not os.path.isfile(p):
|
|
132
|
+
return 0
|
|
133
|
+
with io.open(p, encoding='utf-8') as f:
|
|
134
|
+
return sum(1 for l in f if l.strip())
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _need(shape):
|
|
138
|
+
return set(protocol.VERB_SPECS['write']['shapes'][shape]['required'])
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _env_guard(key):
|
|
142
|
+
old = os.environ.get(key)
|
|
143
|
+
os.environ.pop(key, None)
|
|
144
|
+
return old
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _env_restore(key, old):
|
|
148
|
+
if old is None:
|
|
149
|
+
os.environ.pop(key, None)
|
|
150
|
+
else:
|
|
151
|
+
os.environ[key] = old
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def w1_tool_face_hard_limit():
|
|
155
|
+
cg = _lib('w1')
|
|
156
|
+
big = _pad(LIMIT + 8)
|
|
157
|
+
o = tool_write(cg, big, 'w1_big')
|
|
158
|
+
ok(o.get('committed') is False,
|
|
159
|
+
'W1a 工具面超限 => committed is False(修复前为 True)', o.get('committed'))
|
|
160
|
+
ok(o.get('gate') == 'writelimit_size',
|
|
161
|
+
'W1b 工具面超限 => gate=writelimit_size', o.get('gate'))
|
|
162
|
+
miss = [k for k in protocol.WRITE_TRIO if k not in o]
|
|
163
|
+
ok(not miss, 'W1c 工具面超限返回体含三键(ok/id/committed)', sorted(o))
|
|
164
|
+
need = _need('gate_hard_reject')
|
|
165
|
+
ok(not [k for k in need if k not in o],
|
|
166
|
+
'W1d 工具面超限返回体命中协议形态 gate_hard_reject', sorted(o))
|
|
167
|
+
ok('w1_big' not in cg.index['nodes'],
|
|
168
|
+
'W1e 超限内容未落盘(索引里无该节点)', list(cg.index['nodes'])[:5])
|
|
169
|
+
ok(o.get('bytes') == len(big.encode('utf-8')) and o.get('limit') == LIMIT,
|
|
170
|
+
'W1f 读数自洽:bytes = 正文实际字节数,limit = 上限',
|
|
171
|
+
(o.get('bytes'), len(big.encode('utf-8')), o.get('limit')))
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def w2_two_faces_identical():
|
|
175
|
+
"""两条通路必须给**同一个**响应体(按工具面键集)——比「工具面也有 gate 键」
|
|
176
|
+
更强:它同时钉住「不是复制出来的第二份字段集」。
|
|
177
|
+
动词面外层多一个 action_source(动词面信封),故按工具面键集取子集比对。"""
|
|
178
|
+
cg = _lib('w2')
|
|
179
|
+
big = _pad(LIMIT + 8)
|
|
180
|
+
o_t = tool_write(cg, big, 'same_id')
|
|
181
|
+
o_v = verb_write(cg, big, 'same_id')
|
|
182
|
+
sub = {k: o_v[k] for k in o_t if k in o_v}
|
|
183
|
+
ok(o_t == sub,
|
|
184
|
+
'W2a 同内容 + 同 id:工具面响应体 == 动词面对应键集(同源,非复制)',
|
|
185
|
+
(sorted(o_t), sorted(o_v), o_t.get('gate'), o_v.get('gate')))
|
|
186
|
+
ok(set(o_t) <= set(o_v),
|
|
187
|
+
'W2b 工具面键集是动词面键集的子集(动词面只多信封键)',
|
|
188
|
+
sorted(set(o_t) - set(o_v)))
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def w3_boundary_no_false_positive():
|
|
192
|
+
"""比较算子(> 上限 才拒)在**唯一构造点**上直接钉——不落盘,故不触发
|
|
193
|
+
redact_forbidden 的二次方开销。"""
|
|
194
|
+
a = {'content': _pad(LIMIT), 'content_kind': 'text'}
|
|
195
|
+
ok(writepipe.oversize_response('n', a) is None,
|
|
196
|
+
'W3a 恰好 == 上限(%d 字节)=> 放行(比较算子是 >,不误伤)' % LIMIT,
|
|
197
|
+
writepipe.oversize_response('n', a))
|
|
198
|
+
a2 = {'content': _pad(LIMIT + 1), 'content_kind': 'text'}
|
|
199
|
+
r2 = writepipe.oversize_response('n', a2)
|
|
200
|
+
ok(isinstance(r2, dict) and r2.get('committed') is False,
|
|
201
|
+
'W3b 上限 +1 字节 => 拒', r2)
|
|
202
|
+
ok(r2 and r2.get('bytes') == LIMIT + 1,
|
|
203
|
+
'W3c 上限 +1 的 bytes 读数 = %d' % (LIMIT + 1), r2 and r2.get('bytes'))
|
|
204
|
+
cg = _lib('w3')
|
|
205
|
+
small = BODY % '小正文'
|
|
206
|
+
o = tool_write(cg, small, 'w3_small')
|
|
207
|
+
ok(o.get('committed') is True,
|
|
208
|
+
'W3d 小正文仍正常落盘(补闸未误伤常规写入)', o)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def w4_ledger_switch():
|
|
212
|
+
cg = _lib('w4')
|
|
213
|
+
key = writepipe.ATTEMPT_LEDGER_ENV
|
|
214
|
+
old = _env_guard(key)
|
|
215
|
+
try:
|
|
216
|
+
b = _rows(cg)
|
|
217
|
+
o = verb_write(cg, _ccg('开关未设'), 'w4_a')
|
|
218
|
+
ok(_rows(cg) - b == 1 and o.get('committed') is True,
|
|
219
|
+
'W4a 未设 %s => 账本 +1 行(缺省开,与修复前逐位一致)' % key,
|
|
220
|
+
_rows(cg) - b)
|
|
221
|
+
for i, val in enumerate(('0', 'false', 'no', 'off', 'OFF', ' false ')):
|
|
222
|
+
os.environ[key] = val
|
|
223
|
+
b = _rows(cg)
|
|
224
|
+
o = verb_write(cg, _ccg('开关' + str(i)), 'w4_off%d' % i)
|
|
225
|
+
ok(_rows(cg) - b == 0,
|
|
226
|
+
'W4b %s=%r => 账本 +0 行(修复前该 env 无效、仍写 1 行)' % (key, val),
|
|
227
|
+
_rows(cg) - b)
|
|
228
|
+
ok(o.get('committed') is True,
|
|
229
|
+
'W4c %s=%r 停用只停留痕、不改写入语义' % (key, val), o)
|
|
230
|
+
os.environ[key] = '1'
|
|
231
|
+
b = _rows(cg)
|
|
232
|
+
verb_write(cg, _ccg('开关1'), 'w4_one')
|
|
233
|
+
ok(_rows(cg) - b == 1, 'W4d %s=1 => 账本 +1 行' % key, _rows(cg) - b)
|
|
234
|
+
# 如实钉住**缺口 2 的现状**(本件不修):工具面根本不进 writepipe 的 before
|
|
235
|
+
# 链,故它的写入在尝试账本上是 0 行。若将来 #142 的伞状裁定把工具面也接进
|
|
236
|
+
# 记账面,本项会转红——那正是「把断言升级为『两面一致』」的信号(同
|
|
237
|
+
# md_cg/test_write_return_shape.py 的 H2/H3「现状记录·待裁定」惯例)。
|
|
238
|
+
b = _rows(cg)
|
|
239
|
+
tool_write(cg, BODY % '工具面不记账', 'w4_tool')
|
|
240
|
+
ok(_rows(cg) - b == 0,
|
|
241
|
+
'W4e 现状(缺口 2,待 #142 裁定):工具面写入不产生尝试账本行',
|
|
242
|
+
_rows(cg) - b)
|
|
243
|
+
finally:
|
|
244
|
+
_env_restore(key, old)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def w5_history_survives_rotation():
|
|
248
|
+
"""判别口径是「读面**不归零**」,不是「一条不丢」:超出 keep_shards 的旧分片
|
|
249
|
+
被淘汰是有界保留=设计。W5a 同时钉住「只读活动文件的旧口径在轮转后为 0 行」,
|
|
250
|
+
故 W5b 非空不是恒真。"""
|
|
251
|
+
cg = _lib('w5')
|
|
252
|
+
key = writepipe.ATTEMPT_LEDGER_ENV
|
|
253
|
+
old = _env_guard(key)
|
|
254
|
+
try:
|
|
255
|
+
rot = Rotator(cg.root, writepipe.ATTEMPT_BASENAME,
|
|
256
|
+
writepipe.ATTEMPT_ARCHIVE_DIR,
|
|
257
|
+
rotate_bytes=1, keep_shards=2)
|
|
258
|
+
for k in range(6):
|
|
259
|
+
for j in range(6):
|
|
260
|
+
writepipe._append_attempt(cg, 'w5_%d_%d' % (k, j),
|
|
261
|
+
{'content_kind': 'text',
|
|
262
|
+
'layer': 'knowledge'},
|
|
263
|
+
writepipe.ATTEMPT_COMMITTED,
|
|
264
|
+
out={'id': 'x'})
|
|
265
|
+
r = rot.rotate()
|
|
266
|
+
ok(r is not None, 'W5 第 %d 次轮转切出分片' % (k + 1), r)
|
|
267
|
+
active_only = list(read_jsonl(os.path.join(cg.root,
|
|
268
|
+
writepipe.ATTEMPT_LOG_FILE)))
|
|
269
|
+
hist = writepipe.attempt_history(cg, limit=0)
|
|
270
|
+
ok(len(active_only) == 0,
|
|
271
|
+
'W5a 只读活动文件的旧口径:轮转后 0 行(缺陷现场)', len(active_only))
|
|
272
|
+
ok(len(hist) > 0,
|
|
273
|
+
'W5b 归档感知读面:轮转后**不归零**', len(hist))
|
|
274
|
+
ok(len(hist) == 12,
|
|
275
|
+
'W5c 读面 = 存活分片(keep_shards=2 × 6 行)+ 活动文件', len(hist))
|
|
276
|
+
ok([r['id'] for r in hist][-1] == 'w5_5_5',
|
|
277
|
+
'W5d 时间序正确(末条 = 最后一次写入)', [r['id'] for r in hist][-1:])
|
|
278
|
+
ok(len(writepipe.attempt_history(cg, limit=3)) == 3,
|
|
279
|
+
'W5e limit 生效(取尾部 3 条)',
|
|
280
|
+
len(writepipe.attempt_history(cg, limit=3)))
|
|
281
|
+
finally:
|
|
282
|
+
_env_restore(key, old)
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def w6_discriminator():
|
|
286
|
+
"""判别力对照:把唯一构造点撤成 no-op(等价修复前形态)⇒ 工具面又落盘成功。
|
|
287
|
+
证明 W1 不是恒真。"""
|
|
288
|
+
cg = _lib('w6')
|
|
289
|
+
big = _pad(LIMIT + 8)
|
|
290
|
+
real = writepipe.oversize_response
|
|
291
|
+
try:
|
|
292
|
+
writepipe.oversize_response = lambda nid, a: None
|
|
293
|
+
o = tool_write(cg, big, 'w6_big')
|
|
294
|
+
ok(o.get('committed') is True,
|
|
295
|
+
'W6a 撤掉接线 => 工具面又落盘成功(W1 有判别力)', o)
|
|
296
|
+
finally:
|
|
297
|
+
writepipe.oversize_response = real
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def w7_source_no_regress():
|
|
301
|
+
src = io.open(os.path.join(_HERE, 'mcp_server.py'), encoding='utf-8').read()
|
|
302
|
+
i = src.find('if name == "mdcg_remember":')
|
|
303
|
+
j = src.find('if name == "mdcg_recall":')
|
|
304
|
+
block = src[i:j] if (i >= 0 and j > i) else ''
|
|
305
|
+
ok('oversize_response' in block,
|
|
306
|
+
'W7a 工具面分支仍调用动词面的同一实现(防回退到「各写一份判据」)',
|
|
307
|
+
'(本项红说明接线被撤)')
|
|
308
|
+
ws = io.open(os.path.join(_HERE, 'writepipe.py'), encoding='utf-8').read()
|
|
309
|
+
ok('def oversize_response' in ws,
|
|
310
|
+
'W7b writepipe 仍有唯一构造点 oversize_response')
|
|
311
|
+
ok('def attempt_history' in ws,
|
|
312
|
+
'W7c writepipe 仍有归档感知读面 attempt_history')
|
|
313
|
+
ok('def read_records' in io.open(os.path.join(_HERE, 'rotate.py'),
|
|
314
|
+
encoding='utf-8').read(),
|
|
315
|
+
'W7d Rotator 仍有归档感知读原语 read_records')
|
|
316
|
+
ok(writepipe.ATTEMPT_LEDGER_ENV == 'MDCG_WRITE_ATTEMPT_LEDGER',
|
|
317
|
+
'W7e 开关名就是 issue #212 点名的那一个(不是另起新名)',
|
|
318
|
+
writepipe.ATTEMPT_LEDGER_ENV)
|
|
319
|
+
ok(writepipe.oversize_response('n', {'content': 'x'}) is None,
|
|
320
|
+
'W7f 唯一构造点可直接调用(两面的共用入口没被私有化)')
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
_CASES = (w1_tool_face_hard_limit, w2_two_faces_identical,
|
|
324
|
+
w3_boundary_no_false_positive, w4_ledger_switch,
|
|
325
|
+
w5_history_survives_rotation, w6_discriminator,
|
|
326
|
+
w7_source_no_regress)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def _run_all():
|
|
330
|
+
del _PASS[:]
|
|
331
|
+
del _FAIL[:]
|
|
332
|
+
for fn in _CASES:
|
|
333
|
+
print(chr(10) + '== %s' % fn.__name__)
|
|
334
|
+
try:
|
|
335
|
+
fn()
|
|
336
|
+
except Exception as exc: # noqa: BLE001
|
|
337
|
+
import traceback
|
|
338
|
+
_FAIL.append('%s 抛异常' % fn.__name__)
|
|
339
|
+
print(' FAIL %s 抛异常:%s: %s'
|
|
340
|
+
% (fn.__name__, type(exc).__name__, exc))
|
|
341
|
+
traceback.print_exc()
|
|
342
|
+
print(chr(10) + '通过 %d / 失败 %d' % (len(_PASS), len(_FAIL)))
|
|
343
|
+
for m in _FAIL:
|
|
344
|
+
print(' - ' + m)
|
|
345
|
+
return 1 if _FAIL else 0
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
if __name__ == '__main__':
|
|
349
|
+
raise SystemExit(_run_all())
|