@furongjun1999/dsh-memory 0.9.23 → 0.10.3
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 +515 -404
- package/CHANGELOG.md +642 -449
- package/README.en.md +244 -240
- package/README.md +272 -262
- package/codebuddy/CODEBUDDY.md +1 -1
- package/docs/discipline/templates/full.md.tmpl +1 -1
- package/docs/discipline/templates/rules.mdc.tmpl +1 -1
- package/docs/en/mdcg/world-model-endpoint.md +1 -1
- package/docs/mdcg/cogmap_ops.md +19 -16
- package/lib/client.js +29 -3
- package/lib/hooks.d.ts +12 -0
- package/lib/hooks.js +43 -1
- package/lib/tools.d.ts +2 -2
- package/md_cg/bucketindex.py +231 -0
- package/md_cg/bucketscope.py +357 -0
- package/md_cg/cohesion.py +337 -0
- package/md_cg/config_registry_bulk.py +24 -10
- package/md_cg/config_validate.py +14 -2
- package/md_cg/constraint_audit.py +231 -0
- package/md_cg/constraint_watch.py +206 -0
- package/md_cg/context_audit.py +240 -0
- package/md_cg/context_plan.py +142 -0
- package/md_cg/detrerank.py +137 -0
- package/md_cg/distill.py +247 -0
- package/md_cg/dossier.py +151 -0
- package/md_cg/entities.py +133 -0
- package/md_cg/fsutil.py +135 -0
- 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 +6044 -5761
- package/md_cg/mdcg.py +501 -16
- package/md_cg/mdcos.py +546 -12
- 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/op_table.py +22 -0
- package/md_cg/probe_c_ledger_integrity.py +258 -0
- package/md_cg/probe_d_retirement_discipline.py +263 -0
- package/md_cg/profiles.py +298 -0
- package/md_cg/protocol.py +7 -1
- package/md_cg/reldate.py +142 -0
- package/md_cg/review_cli.py +6 -1
- package/md_cg/run_tests.py +30 -1
- package/md_cg/scopeselect.py +214 -0
- package/md_cg/scrub.py +66 -9
- package/md_cg/selfstmt.py +139 -0
- package/md_cg/state_extract.py +149 -0
- package/md_cg/state_slots.py +62 -10
- package/md_cg/state_view.py +181 -0
- package/md_cg/structboost.py +179 -0
- package/md_cg/sustain.py +28 -2
- package/md_cg/test_alternatives_historical.py +165 -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_bucket_time_rounds.py +87 -0
- package/md_cg/test_bucketindex.py +130 -0
- package/md_cg/test_bucketscope_early.py +71 -0
- package/md_cg/test_cohesion.py +107 -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_constraint_audit.py +87 -0
- package/md_cg/test_constraint_watch.py +108 -0
- package/md_cg/test_context_audit.py +110 -0
- package/md_cg/test_context_plan.py +81 -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 +14 -2
- package/md_cg/test_issue192_move_layer_indices_guard.py +48 -0
- package/md_cg/test_issue39_utf8_stdio.py +572 -572
- package/md_cg/test_issue413_preimage_failclosed.py +147 -9
- 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_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_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/timeboost.py +223 -0
- package/md_cg/tokens.py +1 -1
- package/md_cg/topicagg.py +142 -0
- package/md_cg/version_anchor.py +84 -1
- package/md_cg/whitebox_kb/wisdom/code_compose.py +33 -5
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +5 -1
- package/md_cg/whitebox_kb/wisdom/role_compose.py +271 -268
- package/package.json +1 -1
- package/src/hooks.ts +43 -1
- package/ui/minimal/payload/build-ui.mjs +41 -2
- package/ui/minimal/payload/lib/client.js +29 -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
package/md_cg/metacognition.py
CHANGED
|
@@ -268,7 +268,7 @@ def trace(cg, window: int = 50) -> dict:
|
|
|
268
268
|
# 观测面 2:自信校准(期望正确率 vs 实际验证通过率)
|
|
269
269
|
# --------------------------------------------------------------------------
|
|
270
270
|
|
|
271
|
-
# 生效条件:形参 cg 的 index.nodes 中满足 evidence_count>0 且 frontmatter 的正例+负例>0 的节点计数 used,在 used >= int(max_scan)(默认 2000,max_scan 为 0 时立即 break 使 used=0)时截断——used 为 0 返回 ok=False/insufficient_data
|
|
271
|
+
# 生效条件:形参 cg 的 index.nodes 中满足 evidence_count>0 且 frontmatter 的正例+负例>0 的节点计数 used,在 used >= int(max_scan)(默认 2000,max_scan 为 0 时立即 break 使 used=0)时截断——used 为 0 返回 ok=False/insufficient_data;used 达标但 **Σ正例==0**(全库无 confirmed,issue #829)时 calibrated/overconfident/underconfident 三值一律不判,verdict 落 insufficient_data 且 calibration_defined=False、reason=no_positive_evidence(此时 actual_accuracy 数学上恒 0、gap 恒等于 expected,三值读数不携带信息);正例存在时按 used 与常量 MIN_SAMPLES、期望减实际的 gap 与 GAP_OVERCONFIDENT/GAP_UNDERCONFIDENT 判定 verdict(insufficient_data/overconfident/underconfident/calibrated);两种分支的返回体都带 n_by_layer(逐层 节点/正例/负例 构成);
|
|
272
272
|
def calibration(cg, max_scan: int = 2000) -> dict:
|
|
273
273
|
"""自信校准:我说的可信吗?
|
|
274
274
|
|
|
@@ -276,6 +276,18 @@ def calibration(cg, max_scan: int = 2000) -> dict:
|
|
|
276
276
|
实际正确率 = Σ正例 / Σ(正例+负例)(来自 evidence_log 的聚合计数)。
|
|
277
277
|
gap > 0 → 过度自信;gap < 0 → 过度保守。
|
|
278
278
|
ECE = Σ(证据占比 × |桶内实际 - 桶内自信|)(期望校准误差)。
|
|
279
|
+
|
|
280
|
+
issue #829(2026-11-08):**分母里没有正例时,本函数不判方向**。
|
|
281
|
+
修前:判据是 `总正例 / (总正例 + 总负例)`,而本仓没有任何生产路径写
|
|
282
|
+
`confirmed`(`verify(verdict="confirmed")` 只有测试与外部裁决会发),
|
|
283
|
+
故活库 Σ正例 恒为 0 ⇒ `actual_accuracy` 数学上恒为 0.0、`gap` 恒等于
|
|
284
|
+
`expected_accuracy` ⇒ `verdict` **恒为 overconfident**。这不是「读数偏低」,
|
|
285
|
+
是**该字段不携带信息**;下游 `self_state` / `report` / `scrub.calibrate`
|
|
286
|
+
把它当真读数消费(scrub 会据此向节点写回 confidence 偏置)。
|
|
287
|
+
恒红比不报更有害,故:Σ正例==0 时 `verdict="insufficient_data"`、
|
|
288
|
+
`calibration_defined=False`、`reason="no_positive_evidence"`,
|
|
289
|
+
**expected/actual/gap/ece 照旧如实算并透出**(不隐藏、不编造),
|
|
290
|
+
另给 `n_by_layer` 让「人口构成」在读数面可见。
|
|
279
291
|
"""
|
|
280
292
|
keys = [f"{lo:g}-{hi:g}" for lo, hi in BINS]
|
|
281
293
|
buckets = {k: {"n": 0, "conf_sum": 0.0, "pos": 0, "neg": 0} for k in keys}
|
|
@@ -291,6 +303,10 @@ def calibration(cg, max_scan: int = 2000) -> dict:
|
|
|
291
303
|
# 只把「没进桶」这件事从静默变成可观测。
|
|
292
304
|
dropped_count = 0
|
|
293
305
|
dropped_ids = []
|
|
306
|
+
# issue #829:逐层人口构成(节点数 / 正例 / 负例)。修前 `n_by_layer`
|
|
307
|
+
# 在整个包里零命中(#827 早期正文声称「已提」是误记),故「分母为什么
|
|
308
|
+
# 没有正例」在读数面不可见。
|
|
309
|
+
pop_by_layer = {}
|
|
294
310
|
for nid, e in _nodes(cg).items():
|
|
295
311
|
if used >= int(max_scan):
|
|
296
312
|
break
|
|
@@ -306,6 +322,11 @@ def calibration(cg, max_scan: int = 2000) -> dict:
|
|
|
306
322
|
exp_sum += conf
|
|
307
323
|
total_pos += pos
|
|
308
324
|
total_neg += neg
|
|
325
|
+
_lay = str(fm.get("layer") or "unknown")
|
|
326
|
+
_b = pop_by_layer.setdefault(_lay, {"n_nodes": 0, "pos": 0, "neg": 0})
|
|
327
|
+
_b["n_nodes"] += 1
|
|
328
|
+
_b["pos"] += pos
|
|
329
|
+
_b["neg"] += neg
|
|
309
330
|
placed = False
|
|
310
331
|
for lo, hi in BINS:
|
|
311
332
|
if lo <= conf < hi:
|
|
@@ -326,6 +347,8 @@ def calibration(cg, max_scan: int = 2000) -> dict:
|
|
|
326
347
|
return {"ok": False, "reason": "insufficient_data", "n_nodes": 0,
|
|
327
348
|
"dropped": 0, "out_of_range": 0, "dropped_ids": [],
|
|
328
349
|
"dropped_ids_truncated": False,
|
|
350
|
+
"n_by_layer": {},
|
|
351
|
+
"calibration_defined": False,
|
|
329
352
|
"note": "无带证据的节点:先 verify() 积累验证记录"}
|
|
330
353
|
expected = exp_sum / used
|
|
331
354
|
actual = total_pos / max(1, total_pos + total_neg)
|
|
@@ -345,7 +368,18 @@ def calibration(cg, max_scan: int = 2000) -> dict:
|
|
|
345
368
|
"accuracy": None if acc_b is None else round(acc_b, 4)})
|
|
346
369
|
if n_ev and total_ev:
|
|
347
370
|
ece += (n_ev / total_ev) * abs(conf_b - acc_b)
|
|
348
|
-
|
|
371
|
+
# issue #829:**正例入口缺失 ⇒ 不判方向**。
|
|
372
|
+
# actual = 0/(0+neg) 恒为 0,gap 恒等于 expected,verdict 必然 overconfident。
|
|
373
|
+
# 这不是「偏过度自信」,是分母里没有正例。故 calibration_defined=False,
|
|
374
|
+
# 方向三值一律不判(verdict=insufficient_data),期望/实际/gap/ece 仍如实透出。
|
|
375
|
+
calibration_defined = used >= MIN_SAMPLES and total_pos > 0
|
|
376
|
+
if total_pos <= 0:
|
|
377
|
+
reason = "no_positive_evidence"
|
|
378
|
+
elif used < MIN_SAMPLES:
|
|
379
|
+
reason = "insufficient_samples"
|
|
380
|
+
else:
|
|
381
|
+
reason = None
|
|
382
|
+
if not calibration_defined:
|
|
349
383
|
verdict = "insufficient_data"
|
|
350
384
|
elif gap > GAP_OVERCONFIDENT:
|
|
351
385
|
verdict = "overconfident"
|
|
@@ -362,11 +396,20 @@ def calibration(cg, max_scan: int = 2000) -> dict:
|
|
|
362
396
|
"dropped_ids": list(dropped_ids),
|
|
363
397
|
"dropped_ids_truncated": dropped_count > len(dropped_ids),
|
|
364
398
|
"n_evidence": total_ev,
|
|
399
|
+
# issue #829:人口构成透出(分母为什么长这样,读数面可见)。
|
|
400
|
+
"n_positive": total_pos,
|
|
401
|
+
"n_negative": total_neg,
|
|
402
|
+
"n_by_layer": pop_by_layer,
|
|
403
|
+
"calibration_defined": calibration_defined,
|
|
365
404
|
"expected_accuracy": round(expected, 4),
|
|
366
405
|
"actual_accuracy": round(actual, 4),
|
|
367
406
|
"gap": round(gap, 4),
|
|
368
407
|
"ece": round(ece, 4),
|
|
369
408
|
"verdict": verdict,
|
|
409
|
+
**({"reason": reason} if reason else {}),
|
|
410
|
+
**({"note": "分母无正例(无任何 confirmed 裁决):actual 恒 0、"
|
|
411
|
+
"gap 恒等于 expected,方向不判。先积累 confirmed 证据"}
|
|
412
|
+
if reason == "no_positive_evidence" else {}),
|
|
370
413
|
"bins": bins,
|
|
371
414
|
}
|
|
372
415
|
|
|
@@ -491,7 +534,7 @@ def trust(cg, window: int = 100, max_scan: int = 2000) -> dict:
|
|
|
491
534
|
# 自报告:汇总 + 确定性建议
|
|
492
535
|
# --------------------------------------------------------------------------
|
|
493
536
|
|
|
494
|
-
# 生效条件:形参 tr.get("ok") 为假时产出 no_trace;否则按 tr["d2"] > D2_EMOTION 产出 diverging、state_rates["BLINDSPOT"] >= BLINDSPOT_DENSE 产出 blindspot_dense;再按 cal 的 verdict 为 overconfident/underconfident
|
|
537
|
+
# 生效条件:形参 tr.get("ok") 为假时产出 no_trace;否则按 tr["d2"] > D2_EMOTION 产出 diverging、state_rates["BLINDSPOT"] >= BLINDSPOT_DENSE 产出 blindspot_dense;再按 cal 的 verdict 为 overconfident/underconfident 各自追加(issue #829:cal.calibration_defined 为假时**不追加方向建议**,改追加一条 no_positive_evidence 提示),并按 bs.get("unresolved_count", 0) > 0 追加 open_questions;全部未触发时返回单条 stable(形参 tt 在函数体内未被使用);
|
|
495
538
|
def _advise(tr, cal, bs, tt) -> list:
|
|
496
539
|
"""确定性建议(非 LLM):每条都带触发它的证据,可复核。"""
|
|
497
540
|
out = []
|
|
@@ -510,16 +553,26 @@ def _advise(tr, cal, bs, tt) -> list:
|
|
|
510
553
|
"why": f"BLINDSPOT 占比 {rates.get('BLINDSPOT')} >= {BLINDSPOT_DENSE}",
|
|
511
554
|
"action": "为高频盲区邻域建 unresolved 节点,驱动补条件"})
|
|
512
555
|
verdict = cal.get("verdict")
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
556
|
+
# issue #829:方向不判时不产出方向建议——否则「恒 overconfident」会一路
|
|
557
|
+
# 变成「恒建议抽查复核」,把退化的读数变成退化的行动。
|
|
558
|
+
# 缺省视为可判(cal.get 无该键 = 旧调用方/单元夹具,保持既有行为)。
|
|
559
|
+
if cal.get("calibration_defined", True):
|
|
560
|
+
if verdict == "overconfident":
|
|
561
|
+
out.append({"code": "overconfident",
|
|
562
|
+
"why": (f"期望正确率 {cal['expected_accuracy']} > 实际 "
|
|
563
|
+
f"{cal['actual_accuracy']}(gap {cal['gap']})"),
|
|
564
|
+
"action": "对高 confidence 节点抽查复核;verify(weakened) 会触发降级"})
|
|
565
|
+
elif verdict == "underconfident":
|
|
566
|
+
out.append({"code": "underconfident",
|
|
567
|
+
"why": (f"期望正确率 {cal['expected_accuracy']} < 实际 "
|
|
568
|
+
f"{cal['actual_accuracy']}(gap {cal['gap']})"),
|
|
569
|
+
"action": "自信偏低:可适度提升已验证节点的召回权重"})
|
|
570
|
+
elif cal.get("reason") == "no_positive_evidence":
|
|
571
|
+
out.append({"code": "no_positive_evidence",
|
|
572
|
+
"why": (f"校准分母里正例为 0({cal.get('n_negative')} 条负例 / "
|
|
573
|
+
f"{cal.get('n_positive')} 条正例):actual 恒 0,"
|
|
574
|
+
f"方向不可判,gap={cal.get('gap')} 不构成过度自信证据"),
|
|
575
|
+
"action": "为已成立的结论补 verify(confirmed) 正例入口,再重读校准"})
|
|
523
576
|
if bs.get("unresolved_count", 0) > 0:
|
|
524
577
|
out.append({"code": "open_questions",
|
|
525
578
|
"why": f"unresolved 层有 {bs['unresolved_count']} 个未解问题",
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""定点变异自证:`md_cg/test_state_extract_shipped.py` 的每条判据是否真能红。
|
|
3
|
+
|
|
4
|
+
口径(沿本仓既有变异纪律):逐个植入「本该被抓住」的缺陷,跑守卫,断言**必须红**;
|
|
5
|
+
还原后断言**必须绿**且文件 sha256 逐字节还原。变异的对象是**仓内真实文件**
|
|
6
|
+
(先备份到内存,finally 必还原),不改在役库、不落第二份副本。
|
|
7
|
+
|
|
8
|
+
运行:python -X utf8 -m md_cg.mutate_extract_shipped
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import hashlib
|
|
13
|
+
import os
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
|
|
17
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
18
|
+
REPO = os.path.dirname(HERE)
|
|
19
|
+
CORE = os.path.join(HERE, "state_extract.py")
|
|
20
|
+
SHIM = os.path.join(REPO, "scripts", "state_extract.py")
|
|
21
|
+
PKG = os.path.join(REPO, "package.json")
|
|
22
|
+
GUARD = "md_cg.test_state_extract_shipped"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def sha(p):
|
|
26
|
+
return hashlib.sha256(open(p, "rb").read()).hexdigest()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def run_guard():
|
|
30
|
+
r = subprocess.run([sys.executable, "-X", "utf8", "-m", GUARD],
|
|
31
|
+
cwd=REPO, capture_output=True, text=True,
|
|
32
|
+
encoding="utf-8", errors="replace",
|
|
33
|
+
env={**os.environ, "PYTHONUTF8": "1"})
|
|
34
|
+
return r.returncode, (r.stdout or "") + (r.stderr or "")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def mutate(label, path, old, new, expect_hit_substr):
|
|
38
|
+
"""把 path 里 old 换成 new,跑守卫,断言必须红且命中期望断言;还原。"""
|
|
39
|
+
orig = open(path, encoding="utf-8").read()
|
|
40
|
+
backup = orig
|
|
41
|
+
digest = sha(path)
|
|
42
|
+
if old not in orig:
|
|
43
|
+
print(" SKIP %-52s (变异锚点不存在:%r)" % (label, old[:40]))
|
|
44
|
+
return None
|
|
45
|
+
try:
|
|
46
|
+
open(path, "w", encoding="utf-8", newline="\n").write(
|
|
47
|
+
orig.replace(old, new, 1))
|
|
48
|
+
rc, out = run_guard()
|
|
49
|
+
hit = expect_hit_substr in out
|
|
50
|
+
caught = rc != 0 and hit
|
|
51
|
+
print((" OK " if caught else " MISS ") + "%-52s rc=%s 命中=%s"
|
|
52
|
+
% (label, rc, hit))
|
|
53
|
+
return caught
|
|
54
|
+
finally:
|
|
55
|
+
open(path, "w", encoding="utf-8", newline="\n").write(backup)
|
|
56
|
+
assert sha(path) == digest, "!! 还原后 sha256 不一致:" + path
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def main():
|
|
60
|
+
print("== 基线(必须先绿)")
|
|
61
|
+
rc0, out0 = run_guard()
|
|
62
|
+
print(" 基线 rc=%s(0=绿)" % rc0)
|
|
63
|
+
if rc0 != 0:
|
|
64
|
+
print(out0)
|
|
65
|
+
return 1
|
|
66
|
+
|
|
67
|
+
print("== 逐条变异(每行都必须 OK=被抓住)")
|
|
68
|
+
results = []
|
|
69
|
+
|
|
70
|
+
# M1:本体删掉生产面 → A2 必红(模拟「本体没搬过来」)
|
|
71
|
+
results.append(mutate(
|
|
72
|
+
"M1 本体删 extract_and_record 定义", CORE,
|
|
73
|
+
"def extract_and_record(cg, session: str, turn_no, role: str, text: str,",
|
|
74
|
+
"def _gone_extract_and_record(cg, session: str, turn_no, role: str, text: str,",
|
|
75
|
+
"A2 本体定义了生产面 extract_and_record"))
|
|
76
|
+
|
|
77
|
+
# M2:垫片重新实现判据 → B2 必红(模拟「两份实现」复发)
|
|
78
|
+
results.append(mutate(
|
|
79
|
+
"M2 垫片重新定义 RULES(两份判据)", SHIM,
|
|
80
|
+
'from md_cg.state_extract import (',
|
|
81
|
+
'RULES = []\n\nfrom md_cg.state_extract import (',
|
|
82
|
+
"B2 垫片不得重新定义生产面符号"))
|
|
83
|
+
|
|
84
|
+
# M3:垫片包一层 wrapper → C1 必红(模拟「薄别名变成真实现」)
|
|
85
|
+
results.append(mutate(
|
|
86
|
+
"M3 垫片把 extract_turn 包成 wrapper", SHIM,
|
|
87
|
+
'from md_cg.state_extract import ( # noqa: E402,F401 —— 薄别名,非实现',
|
|
88
|
+
'from md_cg.state_extract import extract_turn as _core_extract_turn\n'
|
|
89
|
+
'def extract_turn(*a, **k):\n return _core_extract_turn(*a, **k)\n\n'
|
|
90
|
+
'from md_cg.state_extract import ( # noqa: E402,F401 —— 薄别名,非实现',
|
|
91
|
+
"C1 垫片 extract_turn 与本体是同一对象"))
|
|
92
|
+
|
|
93
|
+
# M4:白名单删掉 md_cg → D1 必红(模拟「生产方目录不可发货」)
|
|
94
|
+
results.append(mutate(
|
|
95
|
+
"M4 package.json files 删掉 md_cg", PKG,
|
|
96
|
+
'"lib",\n "src",\n "md_cg",',
|
|
97
|
+
'"lib",\n "src",',
|
|
98
|
+
"D1 生产方目录"))
|
|
99
|
+
|
|
100
|
+
# M5:extract_and_record 不落台账 → E1 必红(模拟「抽取了但不记账」)
|
|
101
|
+
# 注意:变异必须是**语义有效**的。首版把 `_se.append(...)` 那行替换成 `pass`
|
|
102
|
+
# 留下后面几行实参 ⇒ SyntaxError(rc=1 但根本没走到 E1)——那是**假捕获**,
|
|
103
|
+
# 证明不了断言有力度。改为在函数体开头插 `return 0`:语法合法、语义是
|
|
104
|
+
# 「抽了但不记账」,E1 必须以断言文本失败。
|
|
105
|
+
results.append(mutate(
|
|
106
|
+
"M5 extract_and_record 提前 return 0(不记账)", CORE,
|
|
107
|
+
" n = 0\n for ev in extract_turn(text, role, subject):",
|
|
108
|
+
" return 0 # MUTANT: 不记账\n n = 0\n for ev in extract_turn(text, role, subject):",
|
|
109
|
+
"E1 从 md_cg 本体可直接抽取并落台账"))
|
|
110
|
+
|
|
111
|
+
print("== 还原后必须复绿")
|
|
112
|
+
rc1, out1 = run_guard()
|
|
113
|
+
print(" 还原后 rc=%s(0=绿)" % rc1)
|
|
114
|
+
|
|
115
|
+
done = [r for r in results if r is not None]
|
|
116
|
+
caught = sum(1 for r in done if r)
|
|
117
|
+
print()
|
|
118
|
+
print("变异自证:%d/%d 被捕获;还原后复绿=%s"
|
|
119
|
+
% (caught, len(done), rc1 == 0))
|
|
120
|
+
okall = caught == len(done) and len(done) == 5 and rc1 == 0
|
|
121
|
+
print("结论:" + ("全部判据有力度(可红可复绿)" if okall else "存在无力度判据"))
|
|
122
|
+
return 0 if okall else 1
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
if __name__ == "__main__":
|
|
126
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# 功能名:台账双端触发点的定点变异自证(守卫真能红吗)
|
|
3
|
+
# 生效条件:python -X utf8 -m md_cg.mutate_ledger_e2e
|
|
4
|
+
# 子功能:对 mcp_server 的 recent·add 抽取块做 4 处**语义有效**变异,
|
|
5
|
+
# 每处都必须让 test_state_ledger_e2e 失败;还原后必须复绿。
|
|
6
|
+
# 执行:备份原文 → 逐条变异 → 跑守卫 → 还原 → 复跑。
|
|
7
|
+
# 验证方式:每处 mutation 都被捕获(rc≠0),全还原后 rc=0。
|
|
8
|
+
# 不适用条件:不做语法级变异(见下方「为什么不用 pass/删除行」)。
|
|
9
|
+
|
|
10
|
+
"""为什么不用「删行 / 换 pass」这类变异(本仓已栽过,2026-11-02 实录):
|
|
11
|
+
|
|
12
|
+
上一轮做 `state_extract` 变异时,我把 `_se.append(...)` 换成 `pass` 却留下后续
|
|
13
|
+
实参 → `SyntaxError`,rc=1 但**根本没走到断言**,什么也没证明。那是假捕获。
|
|
14
|
+
|
|
15
|
+
故本文件的每处变异都保持**语法有效、语义变坏**,并且断言必须
|
|
16
|
+
"因为行为不对" 而红,不是"因为文件坏了"而红。
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import os
|
|
22
|
+
import shutil
|
|
23
|
+
import subprocess
|
|
24
|
+
import sys
|
|
25
|
+
|
|
26
|
+
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
27
|
+
TARGET = os.path.join(REPO, "md_cg", "mcp_server.py")
|
|
28
|
+
GUARD = "md_cg.test_state_ledger_e2e"
|
|
29
|
+
BAK = TARGET + ".mutbak"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def run_guard():
|
|
33
|
+
p = subprocess.run([sys.executable, "-X", "utf8", "-m", GUARD],
|
|
34
|
+
cwd=REPO, capture_output=True, text=True,
|
|
35
|
+
encoding="utf-8", errors="replace")
|
|
36
|
+
return p.returncode
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
MUTS = [
|
|
40
|
+
# (名字,原文片段,替换片段)
|
|
41
|
+
("M1 抽取块整段停用(语法有效:条件恒假)",
|
|
42
|
+
" if _role == \"user\" and _text and _meta.get(\"session\"):",
|
|
43
|
+
" if False and _role == \"user\" and _text and _meta.get(\"session\"):"),
|
|
44
|
+
|
|
45
|
+
("M2 去掉「只抽用户轮」前置(助手轮也抽)",
|
|
46
|
+
" if _role == \"user\" and _text and _meta.get(\"session\"):",
|
|
47
|
+
" if _text and _meta.get(\"session\"):"),
|
|
48
|
+
|
|
49
|
+
("M3 去掉 session 前置(无归属也抽)",
|
|
50
|
+
" if _role == \"user\" and _text and _meta.get(\"session\"):",
|
|
51
|
+
" if _role == \"user\" and _text:"),
|
|
52
|
+
|
|
53
|
+
("M4 异常不再 fail-soft(改成不吞,破坏窗口写入隔离)",
|
|
54
|
+
" except Exception: # noqa: BLE001\n pass",
|
|
55
|
+
" except ZeroDivisionError: # noqa: BLE001\n pass"),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def main():
|
|
60
|
+
if not os.path.exists(TARGET):
|
|
61
|
+
print("目标不在:%s" % TARGET)
|
|
62
|
+
return 1
|
|
63
|
+
shutil.copy2(TARGET, BAK)
|
|
64
|
+
caught, missed = 0, 0
|
|
65
|
+
try:
|
|
66
|
+
base = run_guard()
|
|
67
|
+
print("基线(未变异):rc=%d %s" % (base, "OK 绿" if base == 0 else "!! 基线就红"))
|
|
68
|
+
if base != 0:
|
|
69
|
+
print("基线不绿,变异自证无意义;先修基线")
|
|
70
|
+
return 1
|
|
71
|
+
print()
|
|
72
|
+
for name, old, new in MUTS:
|
|
73
|
+
src = open(BAK, encoding="utf-8").read()
|
|
74
|
+
if old not in src:
|
|
75
|
+
print(" SKIP %-52s(原文片段未命中,变异无效)" % name)
|
|
76
|
+
missed += 1
|
|
77
|
+
continue
|
|
78
|
+
open(TARGET, "w", encoding="utf-8", newline="\n").write(
|
|
79
|
+
src.replace(old, new, 1))
|
|
80
|
+
rc = run_guard()
|
|
81
|
+
if rc != 0:
|
|
82
|
+
caught += 1
|
|
83
|
+
print(" CAUGHT %-52s rc=%d" % (name, rc))
|
|
84
|
+
else:
|
|
85
|
+
missed += 1
|
|
86
|
+
print(" MISSED %-52s rc=0(断言没兜住这个坏法)" % name)
|
|
87
|
+
finally:
|
|
88
|
+
shutil.copy2(BAK, TARGET)
|
|
89
|
+
os.remove(BAK)
|
|
90
|
+
rc = run_guard()
|
|
91
|
+
print()
|
|
92
|
+
print("还原后复跑:rc=%d %s" % (rc, "OK 复绿" if rc == 0 else "!! 未复绿"))
|
|
93
|
+
|
|
94
|
+
print()
|
|
95
|
+
print("变异自证:捕获 %d / 漏 %d(总 %d)" % (caught, missed, len(MUTS)))
|
|
96
|
+
return 0 if (missed == 0 and caught == len(MUTS)) else 1
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
if __name__ == "__main__":
|
|
100
|
+
raise SystemExit(main())
|
package/md_cg/op_table.py
CHANGED
|
@@ -59,6 +59,14 @@ _OP_TABLE = {
|
|
|
59
59
|
"dedupe",
|
|
60
60
|
"strict_conditions",
|
|
61
61
|
"exclude_doc",
|
|
62
|
+
# issue #721:检索期按标签过滤(域隔离)。三条判据显式声明,改口径须同步改注释:
|
|
63
|
+
# · 多标签 = OR —— 命中**任一**即保留(include)/ 即剔除(exclude);
|
|
64
|
+
# · 匹配 = **全等** —— 不做前缀(否则 dom:work 会误命中 dom:workshop);
|
|
65
|
+
# · 无匹配 = **返回空集,不报错** —— 空集是合法检索答案。
|
|
66
|
+
# 优先级:exclude 命中先剔除,**优先于** include。
|
|
67
|
+
# 缺省不传 => 完全跳过过滤分支,既有调用方行为逐位不变(向后兼容)。
|
|
68
|
+
"include_tags",
|
|
69
|
+
"exclude_tags",
|
|
62
70
|
"excerpt_strategy",
|
|
63
71
|
"bm25",
|
|
64
72
|
"session",
|
|
@@ -580,6 +588,20 @@ _OP_TABLE = {
|
|
|
580
588
|
),
|
|
581
589
|
),
|
|
582
590
|
},
|
|
591
|
+
"state_view": {
|
|
592
|
+
"doc_order": 37,
|
|
593
|
+
"doc": "状态视图:账本回放 + stable 租约(TTL/weak)+ 同主体多槽位联合(终态求解用)。",
|
|
594
|
+
"impl": "inline",
|
|
595
|
+
"require": "read",
|
|
596
|
+
"params": (
|
|
597
|
+
"subject",
|
|
598
|
+
"slot",
|
|
599
|
+
"limit",
|
|
600
|
+
),
|
|
601
|
+
"actions": (),
|
|
602
|
+
"default_action": None,
|
|
603
|
+
"sigs": (),
|
|
604
|
+
},
|
|
583
605
|
"state_event": {
|
|
584
606
|
"doc_order": 36,
|
|
585
607
|
"doc": "状态事件记账(追加一条五元事件subject/slot/old/new/kind;actor 恒取令牌;查询走 stg(op=state_chain))。",
|