@furongjun1999/dsh-memory 0.9.22 → 0.9.23
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 +404 -0
- package/CHANGELOG.md +45 -0
- package/README.en.md +240 -0
- package/README.md +3 -1
- package/docs/README.md +2 -0
- package/docs/en/README.md +190 -0
- package/docs/en/TRANSLATION.md +78 -0
- package/docs/en/TRANSLATION_MANIFEST.json +170 -0
- package/docs/en/TRANSLATION_REPORT.md +11 -0
- package/docs/en/dev/README.md +10 -0
- package/docs/en/dev/coding-testing.md +72 -0
- package/docs/en/dev/switch-matrix.md +10 -0
- package/docs/en/glossary.md +97 -0
- package/docs/en/harnesses/claude.md +100 -0
- package/docs/en/harnesses/codebuddy.md +93 -0
- package/docs/en/harnesses/codex.md +88 -0
- package/docs/en/harnesses/dsh.md +84 -0
- package/docs/en/harnesses/zcode.md +42 -0
- package/docs/en/mdcg/ccg.md +32 -0
- package/docs/en/mdcg/generated-docs.md +20 -0
- package/docs/en/mdcg/guardrail-charter.md +170 -0
- package/docs/en/mdcg/mcp-tool-names.md +43 -0
- package/docs/en/mdcg/multi-harness.md +43 -0
- package/docs/en/mdcg/recall-metadata.md +30 -0
- package/docs/en/mdcg/world-model-endpoint.md +379 -0
- package/docs/en/mdcg/write-aliases.md +130 -0
- package/docs/en/ops/README.md +11 -0
- package/docs/en/ops/environment-known-issues.md +244 -0
- package/docs/en/ops/install.md +72 -0
- package/docs/en/ops/operations-troubleshooting.md +86 -0
- package/docs/en/ops/permissions-review.md +79 -0
- package/docs/en/ops/release-governance.md +49 -0
- package/docs/en/ops/tool-name-migration.md +30 -0
- package/docs/en/ops/ui-install-troubleshooting.md +53 -0
- package/docs/en/standards/ai-coding-standard-checklist.md +71 -0
- package/docs/en/standards/ai-coding-standard-lingshu.md +47 -0
- package/docs/en/standards/ai-coding-standard.md +185 -0
- package/docs/en/ui/minimal.md +62 -0
- package/docs/mdcg/cogmap_ops.md +15 -15
- package/lib/hooks.d.ts +22 -0
- package/lib/hooks.js +124 -32
- package/lib/index.js +19 -3
- package/lib/lib/console_web.mjs +30 -3
- package/lib/lib/hook_audit.d.ts +3 -2
- package/lib/lib/hook_audit.js +14 -4
- package/lib/lib/mdcg_client.d.ts +5 -2
- package/lib/lib/mdcg_client.js +8 -4
- package/lib/lib/prompt_safety.d.ts +24 -0
- package/lib/lib/prompt_safety.js +77 -1
- package/lib/lib/token_store.js +5 -2
- package/lib/tools.js +5 -3
- package/md_cg/fsutil.py +80 -25
- package/md_cg/mcp_server.py +5761 -5557
- package/md_cg/mdcg.py +570 -73
- package/md_cg/mdcos.py +36 -11
- package/md_cg/nodefile.py +147 -23
- package/md_cg/op_table.py +14 -4
- package/md_cg/reconcile.py +9 -11
- package/md_cg/rollback.py +16 -1
- package/md_cg/scrub.py +94 -14
- package/md_cg/security.py +4 -2
- package/md_cg/test_cond_term_fold.py +183 -0
- package/md_cg/test_cond_terms_colon.py +348 -0
- package/md_cg/test_generation_guard.py +63 -42
- package/md_cg/test_issue362_neg_condition.py +44 -7
- package/md_cg/test_issue39_utf8_stdio.py +3 -3
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +14 -8
- package/md_cg/test_issue677_flush_batch_io.py +139 -0
- package/md_cg/test_issue677_info_fast.py +82 -0
- package/md_cg/test_issue677_remember_dedupe.py +106 -0
- package/md_cg/test_issue712_tokens_sessions_read.py +199 -0
- package/md_cg/test_issue722_strict_conditions.py +96 -0
- package/md_cg/test_issue730_narrowing_audit.py +121 -0
- package/md_cg/test_issue733_verify_override.py +193 -0
- package/md_cg/test_issue733_verify_override_and_overwrite_inherit.py +315 -0
- package/md_cg/test_issue740_filelock_fd_leak.py +218 -0
- package/md_cg/test_issue741_content_hash_same_source.py +158 -0
- package/md_cg/test_issue747_slot_contradiction.py +125 -0
- package/md_cg/test_issue754_nested_root_guard.py +197 -0
- package/md_cg/test_issue757_index_hash_compare.py +85 -0
- package/md_cg/test_issue758_disk_hash_single.py +67 -0
- package/md_cg/test_issue759_twophase_hash.py +67 -0
- package/md_cg/test_n204_n205_n226_n227_n228_n229_exit_gates.py +545 -542
- package/md_cg/test_n209_verify_write_face_gates.py +5 -2
- package/md_cg/test_neg_condition_hits.py +336 -333
- package/md_cg/test_scrub_source_line_false_positive.py +64 -0
- package/md_cg/test_twophase.py +286 -286
- package/md_cg/tokens.py +55 -7
- package/md_cg/twophase.py +15 -6
- package/md_cg/writepipe.py +35 -3
- package/package.json +137 -134
- package/scripts/workspace_index.py +2 -0
- package/src/hooks.ts +139 -33
- package/src/index.ts +19 -3
- package/src/lib/console_web.mjs +30 -3
- package/src/lib/hook_audit.ts +15 -5
- package/src/lib/mdcg_client.ts +8 -4
- package/src/lib/prompt_safety.ts +73 -1
- package/src/lib/token_store.ts +5 -2
- package/src/tools.ts +5 -3
- package/ui/minimal/integrity.json +2 -2
- package/ui/minimal/payload/lib/lib/console_web.mjs +30 -3
- package/ui/minimal/payload/src/lib/console_web.mjs +30 -3
package/md_cg/mdcos.py
CHANGED
|
@@ -1295,7 +1295,8 @@ class MdCGOS(MdCG):
|
|
|
1295
1295
|
|
|
1296
1296
|
@staticmethod
|
|
1297
1297
|
# 生效条件:node_dict 的 frontmatter 中 ccg_exempt 为真且 content 去空白后非空时返回 DEFER 记录,否则以 (node_dict, query, context) 转 MdCG.judge_qualification;
|
|
1298
|
-
def judge_qualification(node_dict, query: str, context=None
|
|
1298
|
+
def judge_qualification(node_dict, query: str, context=None,
|
|
1299
|
+
strict_conditions: bool = True):
|
|
1299
1300
|
"""在父类四态之上支持 legacy 记忆(迁移进来的自由文本)。
|
|
1300
1301
|
|
|
1301
1302
|
迁移进来的历史记忆没有 CCG 5 要素注释。若直接按父类判 BLINDSPOT,
|
|
@@ -1308,7 +1309,7 @@ class MdCGOS(MdCG):
|
|
|
1308
1309
|
if fm.get("ccg_exempt") and content.strip():
|
|
1309
1310
|
return {"state": STATE_DEFER,
|
|
1310
1311
|
"reason": "legacy 记忆(无 CCG 5 要素):可检索;建议补生效条件/不适用条件/验证方式"}
|
|
1311
|
-
return MdCG.judge_qualification(node_dict, query, context)
|
|
1312
|
+
return MdCG.judge_qualification(node_dict, query, context, strict_conditions=strict_conditions)
|
|
1312
1313
|
|
|
1313
1314
|
# ================= 检索(含 role 过滤 + 四路召回) =================
|
|
1314
1315
|
|
|
@@ -1385,7 +1386,8 @@ class MdCGOS(MdCG):
|
|
|
1385
1386
|
roles=None, include_work: bool = False, pools=None,
|
|
1386
1387
|
session=None, branch=None, validity=None,
|
|
1387
1388
|
start_time=None, end_time=None, start_operator=None,
|
|
1388
|
-
end_operator=None, time_axis=None, view=None
|
|
1389
|
+
end_operator=None, time_axis=None, view=None,
|
|
1390
|
+
strict_conditions: bool = True):
|
|
1389
1391
|
"""在父类语义之上加 role 过滤(默认剔除工具输出/命令/编辑)。
|
|
1390
1392
|
|
|
1391
1393
|
返回 (results, meta),与 MdCG.search 完全同构(T0–T3 阶梯 + 资格判定)。
|
|
@@ -1524,7 +1526,8 @@ class MdCGOS(MdCG):
|
|
|
1524
1526
|
if valid >= min_results:
|
|
1525
1527
|
return self._emit(scored, k, tier, stat, route_bucket, record,
|
|
1526
1528
|
len(docs), judge, context, neg_coverage,
|
|
1527
|
-
big_domain, big_scores, pool_cfg
|
|
1529
|
+
big_domain, big_scores, pool_cfg,
|
|
1530
|
+
strict_conditions=strict_conditions)
|
|
1528
1531
|
return None
|
|
1529
1532
|
|
|
1530
1533
|
def _prescored(docs, scored_all):
|
|
@@ -2164,7 +2167,8 @@ class MdCGOS(MdCG):
|
|
|
2164
2167
|
session=None, branch=None, validity=None,
|
|
2165
2168
|
early_stop_threshold=None,
|
|
2166
2169
|
start_time=None, end_time=None, start_operator=None,
|
|
2167
|
-
end_operator=None, time_axis=None, view=None
|
|
2170
|
+
end_operator=None, time_axis=None, view=None,
|
|
2171
|
+
strict_conditions: bool = True):
|
|
2168
2172
|
"""并行多路召回 + RRF 融合。返回 (results, meta)。
|
|
2169
2173
|
|
|
2170
2174
|
validity:**只排除「已过期」(expired),不排除「未生效」(not_yet)**。
|
|
@@ -2521,7 +2525,8 @@ class MdCGOS(MdCG):
|
|
|
2521
2525
|
and (-upper, nid) >= (-kept[-1][1], kept[-1][0])):
|
|
2522
2526
|
break
|
|
2523
2527
|
node, s = node_by_id[nid]
|
|
2524
|
-
qual = self.judge_qualification(
|
|
2528
|
+
qual = self.judge_qualification(
|
|
2529
|
+
node, q, context, strict_conditions=strict_conditions)
|
|
2525
2530
|
quals[nid] = qual
|
|
2526
2531
|
st = qual.get("state")
|
|
2527
2532
|
if st in (STATE_REJECT, STATE_BLINDSPOT):
|
|
@@ -2585,7 +2590,8 @@ class MdCGOS(MdCG):
|
|
|
2585
2590
|
for _k in _c:
|
|
2586
2591
|
boundary_counts["counts"][_k] += _c[_k]
|
|
2587
2592
|
elif judge:
|
|
2588
|
-
_jw = MdCG.judge_with_boundary(
|
|
2593
|
+
_jw = MdCG.judge_with_boundary(
|
|
2594
|
+
node, q, context, strict_conditions=strict_conditions)
|
|
2589
2595
|
qual = dict(_jw["qualification"])
|
|
2590
2596
|
_b = boundary_mark(node, q, context)
|
|
2591
2597
|
if _b.get("hit"):
|
|
@@ -4891,7 +4897,9 @@ class MdCGOS(MdCG):
|
|
|
4891
4897
|
content=None, role=None, verification_basis=None,
|
|
4892
4898
|
importance_hint=None, node_id=None, write=False,
|
|
4893
4899
|
min_jaccard=None, ids=None, pairs=None, actor=None,
|
|
4894
|
-
sensitivity=None,
|
|
4900
|
+
sensitivity=None, tags_add=None, tags_remove=None,
|
|
4901
|
+
condition_space_patch=None, depends_on=None, dry_run=None,
|
|
4902
|
+
**extra):
|
|
4895
4903
|
"""记忆维护(P1):importance / longterm / prefeed / separate / stat。
|
|
4896
4904
|
|
|
4897
4905
|
只读 action(stat/history/longterm 预演)与写层 action(prefeed)不受
|
|
@@ -4926,6 +4934,20 @@ class MdCGOS(MdCG):
|
|
|
4926
4934
|
st = self.flush_state()
|
|
4927
4935
|
st.update({"ok": True, "op": "maintain", "action": "flush"})
|
|
4928
4936
|
return st
|
|
4937
|
+
if act == "retag":
|
|
4938
|
+
# 只改元数据(tags / condition_space / depends_on),正文与证据链原样。
|
|
4939
|
+
# 用于给存量节点补 domain: 标签以迁出 orphan 桶(issue #732 的配套):
|
|
4940
|
+
# 同 id 覆写 add() 曾重置 created_at/session 并丢 evidence_log/edges,
|
|
4941
|
+
# 故这里走 MdCG.retag 的窄路(内部仍复用 add() 的写链,只是显式保证
|
|
4942
|
+
# 「正文逐字不变」,从而命中 BODY_SAME 的继承分支)。
|
|
4943
|
+
# 缺省 dry_run:沿用 maintain 的「预演优先」口径,apply=True 才落盘。
|
|
4944
|
+
# 双开关收敛(#742 复核):`dry_run=False` 不得在 `apply=False` 时落盘
|
|
4945
|
+
# (否则「没给 apply 也写」,工具面语义反转)。落盘条件 = apply 且非 dry。
|
|
4946
|
+
_dry = (not apply) if dry_run is None else (bool(dry_run) or not apply)
|
|
4947
|
+
return self.retag(
|
|
4948
|
+
node_id or "", tags_add=tags_add, tags_remove=tags_remove,
|
|
4949
|
+
condition_space_patch=condition_space_patch,
|
|
4950
|
+
depends_on=depends_on, dry_run=_dry)
|
|
4929
4951
|
if act == "importance":
|
|
4930
4952
|
return weights.recalc(self, layer=layer, limit=limit, apply=apply,
|
|
4931
4953
|
min_delta=(weights.APPLY_DELTA if min_delta is None
|
|
@@ -6963,9 +6985,11 @@ class MdCGSecure(MdCGOS):
|
|
|
6963
6985
|
|
|
6964
6986
|
# 生效条件:verdict(转 str 去空白 lower)属于 {"confirmed","weakened","falsified"} 三态**任一**时**先做索引代际探活重载**(N196:索引缺条目 ≡ 本进程陈旧,不得据以跳过层写闸),随后自索引条目(含 _dirty 未刷条目,口径同 MdCG.get)取该条目 layer 与 sensitivity(缺省回落 DEFAULT_SENSITIVITY)调 protect.require_layer→principal.require_layer_write(N209:三态全覆盖——confirmed/weakened 同样重写被验证节点本体 frontmatter,verify 角色 layers_allow 本就只含 rejected/contextual,tokens.py verify spec forbidden 显式列明「不得改被验证内容」),与 add/add_rejected 同一闸口;随后把 override 原样转 super().verify(受保护节点的 guard_forget 快照留痕在基类 falsified 分支内)。verdict 非法(如空串)时不加闸直接把原值转 super().verify(ValueError 语义由基类维持);目标节点在重载后索引中仍无条目时同样不加闸(not_found 语义由基类维持,且基类 get 取不到即不写盘)。
|
|
6965
6987
|
def verify(self, node_id: str, evidence: str, verdict: str,
|
|
6966
|
-
override: bool = False):
|
|
6988
|
+
override: bool = False, superseded_by: str = None):
|
|
6967
6989
|
v = str(verdict or "").strip().lower()
|
|
6968
|
-
|
|
6990
|
+
# superseded 同属「对被验证内容的写」(改 fm + 推进 lifecycle),与三态
|
|
6991
|
+
# 共用同一道层写闸;漏进来就会重演 N209 的「零闸分支」形态。
|
|
6992
|
+
if v in ("confirmed", "weakened", "falsified", "superseded"):
|
|
6969
6993
|
# N196(2026-09-28):此前直读本进程内存索引且「缺条目 = 放行」——
|
|
6970
6994
|
# 他进程刚写入并 compact 的节点在本进程索引中不存在时,N130 的
|
|
6971
6995
|
# 层写闸被整段跳过:verifier(layers_allow=rejected/contextual)
|
|
@@ -6987,7 +7011,8 @@ class MdCGSecure(MdCGOS):
|
|
|
6987
7011
|
protect.require_layer(
|
|
6988
7012
|
self, node_id, layer=e.get("layer"),
|
|
6989
7013
|
sensitivity=e.get("sensitivity"))
|
|
6990
|
-
return super().verify(node_id, evidence, verdict, override=override
|
|
7014
|
+
return super().verify(node_id, evidence, verdict, override=override,
|
|
7015
|
+
superseded_by=superseded_by)
|
|
6991
7016
|
|
|
6992
7017
|
# 生效条件:sens 取 sensitivity or DEFAULT_SENSITIVITY,经 _rank(sens) 与 principal.require_write(sens) 后把 m 基于 meta 复制并 setdefault tenant 与 _attributed_session(sens)(写归因三态:env/请求声明→原值;未声明的进程自动随机→unattributed,私有档豁免见该函数)、harness 与 unit 为真值时补入,再强制 m["sensitivity"]=sens,text 经 _seal_content("_recent", text, sens) 后连 tags=tags 一起转 super().remember_event(window 为 None 时不传该参,否则带上 window)。
|
|
6993
7018
|
def remember_event(self, role: str, text: str, tags=None, meta=None,
|
package/md_cg/nodefile.py
CHANGED
|
@@ -375,9 +375,22 @@ def declares_dependency(content) -> bool:
|
|
|
375
375
|
return bool(dep_refs(val))
|
|
376
376
|
|
|
377
377
|
|
|
378
|
-
# 生效条件:content 为 None
|
|
378
|
+
# 生效条件:content 为 None 或空串时按空串处理(落盘形态即单个换行),非空时先经 normalize_body 补尾换行再编码;一律返回 sha256(utf-8) 十六进制摘要的前 12 位。基准=正文**落盘形态**(与 dumps 落盘逐字一致),frontmatter 不计入(由调用方保证)。非正文内容(query / 结构化序列化)请直接用 hashlib.sha256——本函数会把「尾换行个数」归一,对非正文是信息损失。
|
|
379
|
+
def normalize_body(content: str) -> str:
|
|
380
|
+
"""正文**落盘形态**的单点规范化(issue #741)——本函数就是从 `dumps()` 抽出的
|
|
381
|
+
那行尾换行规则。凡在**写盘前**计算 content_hash 的路径(add 内联 /
|
|
382
|
+
`_write_node` 索引同步 / `_move_layer` 经 `_node_entry`)输入都是未规范化的
|
|
383
|
+
sealed/正文,不先经它就与「读回正文」(dumps 恒补尾换行)恒差一个字节 ⇒
|
|
384
|
+
同节点同正文在索引里先后出现两个值(#741 实测 9eb1d121c59f / aaf2ffbbdd36)。
|
|
385
|
+
`dumps` 与 `content_hash` 都调本函数——规范化规则改动只许发生在这里。
|
|
386
|
+
"""
|
|
387
|
+
# 严格版(不做 None 兜底):dumps 走本函数,保持「None 正文 = 调用方错误」的 fail-fast;
|
|
388
|
+
# content_hash 侧需要 None 兜底,由它自己 `content or ""` 承担。
|
|
389
|
+
return content if content.endswith("\n") else content + "\n"
|
|
390
|
+
|
|
391
|
+
|
|
379
392
|
def content_hash(content: str) -> str:
|
|
380
|
-
"""
|
|
393
|
+
"""节点正文**落盘形态**的内容指纹(sha256 前 12 位)——全仓唯一实现。
|
|
381
394
|
|
|
382
395
|
两处共用且必须逐位一致,否则对账/变更探测会永远"匹配成功"而失去意义
|
|
383
396
|
(与 `codeindex.region_hash` 同一纪律:两侧各写一份哈希算法,漂移就悄悄失效):
|
|
@@ -389,10 +402,18 @@ def content_hash(content: str) -> str:
|
|
|
389
402
|
(importance / protected / 生命周期状态 / 时间戳),把它算进去则「内容没变、
|
|
390
403
|
元数据变了」也会判成不匹配,对账会大面积误报 interrupted。
|
|
391
404
|
"""
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
#
|
|
405
|
+
# issue #741(2026-11-03):先经 normalize_body 再哈希——指纹基准 =「落盘形态的
|
|
406
|
+
# 正文」(恒带尾换行),与重建/读回路径的输入逐位一致。修前调用方各喂各的
|
|
407
|
+
# 形态(add 内联喂原串、重建喂读回),同一节点同一正文在索引里先后出现两个
|
|
408
|
+
# 值(实测 9eb1d121c59f / aaf2ffbbdd36),对账恒报 hash_drift 假阳性。
|
|
409
|
+
# ⇒ 升级注意:存量索引条目(旧口径)首轮对账会报 hash_drift——但 reconcile
|
|
410
|
+
# 缺省开启、且对 drift 做「以盘面为准」的定向 upsert ⇒ **启动即自愈**;
|
|
411
|
+
# rebuild_index() 是等价手段(可选,非必须)。空正文的指纹随之改变
|
|
412
|
+
# (sha("") → sha("换行")),属本次口径变更的显式一面(测试 G8/G9 钉住)。
|
|
413
|
+
return hashlib.sha256(normalize_body(content or "").encode("utf-8")).hexdigest()[:12]
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
# 生效条件:frontmatter(任意 dict,键按 sorted 输出)与 content(任意 str)均无前置校验即生效;content 经 normalize_body 归一(与 content_hash 同一份规则),返回 "---" + 各键的 "{k}: {json.dumps(v, ensure_ascii=False)}" 行 + "---" 行 + 正文。
|
|
396
417
|
def dumps(frontmatter: dict, content: str) -> str:
|
|
397
418
|
lines = [_DELIM]
|
|
398
419
|
for k in sorted(frontmatter):
|
|
@@ -400,7 +421,7 @@ def dumps(frontmatter: dict, content: str) -> str:
|
|
|
400
421
|
# 一律 JSON 序列化:值内不会出现裸换行,切分才可靠;数字/字符串也保持可读
|
|
401
422
|
lines.append(f"{k}: {json.dumps(v, ensure_ascii=False)}")
|
|
402
423
|
lines.append(_DELIM)
|
|
403
|
-
body = content
|
|
424
|
+
body = normalize_body(content) # issue #741:规范化单点(原为手写规则)
|
|
404
425
|
return "\n".join(lines) + "\n" + body
|
|
405
426
|
|
|
406
427
|
|
|
@@ -760,6 +781,21 @@ FULL_TIME_WINDOW_TEXT = "全时窗(任意时刻成立)"
|
|
|
760
781
|
#: 旧口径残留行的前缀——「观测位置:X」是单槽冒充,不是生效条件
|
|
761
782
|
LEGACY_POSITION_PREFIX = "观测位置:"
|
|
762
783
|
|
|
784
|
+
#: 条件短语的已知槽标签:**唯一真源**(`MdCG._SLOT_LABELS` 只是本元组的别名)。
|
|
785
|
+
#: 只有这些词出现在冒号**前**时才当分隔符;其余冒号是条件正文的一部分
|
|
786
|
+
#: (比值 `3:1`、时间 `9:00`、URL `http://`)。
|
|
787
|
+
COND_SLOT_LABELS = tuple(label for _key, label in CONDITION_SLOTS)
|
|
788
|
+
|
|
789
|
+
#: 条件短语匹配面剥掉的**首尾标点**。
|
|
790
|
+
#: 条件常以「:。」收尾,而查询里不会带,故首尾标点不参与比较。
|
|
791
|
+
#: 注意 `'` `"` `!` **刻意不在表内**:它们在条件正文里是**代码字符**
|
|
792
|
+
#: (`''` 空字符串字面量、`!v` 逻辑非),剥掉会把有效 token 吃成空串
|
|
793
|
+
#: 或单字碎片。引号只在「成对包裹整条短语」时才剥,见 `_strip_paired_wrap`。
|
|
794
|
+
COND_EDGE_PUNCT = " \t\u3000,。;:、!?,.;:?()()【】[]「」《》`·"
|
|
795
|
+
|
|
796
|
+
#: 成对包裹时才有意义的引号(单独出现是代码字符,不剥)
|
|
797
|
+
COND_PAIRED_WRAP = ("'", '"')
|
|
798
|
+
|
|
763
799
|
|
|
764
800
|
# 生效条件:value 为 list/tuple 时用「、」连接各元素 str().strip() 后非空的部分(空元素跳过);value 为 None 时返回 "";其余类型返回 str(value).strip()。
|
|
765
801
|
def _as_slot_text(value) -> str:
|
|
@@ -863,31 +899,119 @@ def is_legacy_position_condition(text) -> bool:
|
|
|
863
899
|
return s.startswith(LEGACY_POSITION_PREFIX) and len(s) > len(LEGACY_POSITION_PREFIX)
|
|
864
900
|
|
|
865
901
|
|
|
866
|
-
# 生效条件:
|
|
867
|
-
def
|
|
868
|
-
"""
|
|
902
|
+
# 生效条件:s 为任意字符串;仅当长度≥3、首字符与末字符相同、该字符属 COND_PAIRED_WRAP、且内层首末字符都不是该引号时剥掉首尾这一对,其余原样返回(单侧引号是代码字符不剥;`''` 整体是空字符串字面量,剥了会变空;内层仍以同种引号开头/结尾说明外层不是一对真正包围整条的引号,不剥)。
|
|
903
|
+
def _strip_paired_wrap(s: str) -> str:
|
|
904
|
+
"""成对包裹整条短语的引号才剥;单独出现的引号是代码字符。
|
|
869
905
|
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
906
|
+
三条边界(各自对应一个实测失败):
|
|
907
|
+
|
|
908
|
+
* `''` / `""` 是**空字符串字面量**(长度 2),剥掉首尾会变成空串而丢失
|
|
909
|
+
⇒ 要求长度 ≥3;
|
|
910
|
+
* `'!v'` → `!v` 应当剥(引号确实成对包围整条);
|
|
911
|
+
* `''与'!v'` 内层仍是 `'…` 开头 ⇒ 外层不是一对包围整条的引号,**不剥**,
|
|
912
|
+
否则会把两个相邻的代码字面量粘成一个假短语。
|
|
913
|
+
"""
|
|
914
|
+
if len(s) >= 3 and s[0] == s[-1] and s[0] in COND_PAIRED_WRAP:
|
|
915
|
+
inner = s[1:-1]
|
|
916
|
+
if inner and inner[0] != s[0] and inner[-1] != s[0]:
|
|
917
|
+
return inner
|
|
918
|
+
return s
|
|
919
|
+
|
|
920
|
+
|
|
921
|
+
# 生效条件:text 为假值(None/空串)时按空文本处理,返回 terms 与各计数全为 0 的字典;否则按「;;」拆槽、剥 legacy「观测位置:」与已知槽标签前缀、按「,,、/()()」切短语、逐段剥首尾标点与成对引号,按 空/纯数字/空值语义哨兵/长度<2/时间维哨兵/重复 六类分流计数,terms 保序去重后返回。
|
|
922
|
+
def condition_terms_diag(text) -> dict:
|
|
923
|
+
"""生效条件声明 → {terms, 各分流计数}(**确定性切分,无语义猜测**)。
|
|
924
|
+
|
|
925
|
+
三个细节都是实测出来的,不是设计推演:
|
|
926
|
+
|
|
927
|
+
1. 冒号只在已知槽标签前才是分隔符。
|
|
928
|
+
`condition_space_text()` 的标签是固定四项(`COND_SLOT_LABELS`),而条件正文
|
|
929
|
+
里的冒号是普通字符(比值 `3:1`、时间 `9:00`、URL `http://`)。旧实现
|
|
930
|
+
剥「第一个冒号之前的一切」,会把整条条件吃成空串或砍掉前半句。
|
|
931
|
+
2. 短语首尾标点不参与比较,否则「…按此处置:」带尾冒号永远比不中;
|
|
932
|
+
但引号只在**成对包裹**时才剥 —— `''` / `!v` 是代码字符,不是标点。
|
|
933
|
+
3. 各兜底分类**分开计数**,让调用方能区分「没写条件」「写了合法空值声明」
|
|
934
|
+
与「写了但切不出可用短语」——合并成一个 `short` 会让三者不可归因。
|
|
935
|
+
|
|
936
|
+
返回键:`terms` 为匹配短语;`n_empty`(纯标点)/`n_digit`(纯数字)/
|
|
937
|
+
`n_short`(单字碎片)是**不可用**残段;`n_time_sentinel`(全时窗)与
|
|
938
|
+
`n_decl_sentinel`(`is_dep_sentinel`,如「无」「不适用」)是**合法空值声明**。
|
|
874
939
|
"""
|
|
940
|
+
text = str(text or "")
|
|
875
941
|
out, seen = [], set()
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
942
|
+
n_empty = n_digit = n_short = n_time_sentinel = n_decl_sentinel = 0
|
|
943
|
+
if not text.strip():
|
|
944
|
+
# 空条件不是「写了但切不出」:不能记成 n_empty=1,否则调用方会把
|
|
945
|
+
# 「没写条件」判成坏条件。
|
|
946
|
+
return {"terms": out, "n_empty": 0, "n_digit": 0, "n_short": 0,
|
|
947
|
+
"n_time_sentinel": 0, "n_decl_sentinel": 0}
|
|
948
|
+
for slot in re.split(r"[;;]", text):
|
|
949
|
+
s = slot.strip()
|
|
950
|
+
# legacy 单槽冒充「观测位置:X」:前缀整段剥掉,其余照常切
|
|
951
|
+
if is_legacy_position_condition(s):
|
|
952
|
+
s = s[len(LEGACY_POSITION_PREFIX):]
|
|
953
|
+
m = re.search(r"[::]", s)
|
|
954
|
+
if m and s[:m.start()].strip() in COND_SLOT_LABELS:
|
|
955
|
+
s = s[m.end():]
|
|
956
|
+
for seg in re.split(r"[,,、/()()]", s):
|
|
957
|
+
# 拆出来的**结构性空段**(split 在 `(…)`、`、` 等分隔符处产生的
|
|
958
|
+
# 空串)不是残段:它一个字符都没有,谈不上「被剥成空」。
|
|
959
|
+
# 判据是「split 之后、剥边之前就非空」,必须在**剥边之前**判 ——
|
|
960
|
+
# 剥边后再 `if not seg` 会把两者混为一谈,于是
|
|
961
|
+
# `时间:全时窗(任意时刻成立)` 这类带括号的合法条件多出 n_empty=1,
|
|
962
|
+
# 进而 unusable>0,把本应豁免的时间维哨兵误判成 DEFER。
|
|
963
|
+
if not seg:
|
|
964
|
+
continue
|
|
965
|
+
# 剥边要**两轮**,顺序是「标点 → 引号 → 标点」。
|
|
966
|
+
# 一轮不够,因为引号包裹里常带尾标点:`"key":` 先剥冒号得 `"key"`、
|
|
967
|
+
# 再剥引号才回到 `key`(与旧实现一致);只做一轮会得到 `"key"`,
|
|
968
|
+
# 把匹配面**收窄**了。反过来先剥引号则 `'拒绝访问。'` 会留住内层句号。
|
|
969
|
+
# 两轮之后 `''`(长度 2,不够成对剥)与 `'!v'`→`!v` 仍照旧救回。
|
|
970
|
+
for _ in range(2):
|
|
971
|
+
seg = seg.strip(COND_EDGE_PUNCT)
|
|
972
|
+
seg = _strip_paired_wrap(seg)
|
|
973
|
+
seg = seg.strip(COND_EDGE_PUNCT)
|
|
974
|
+
if not seg:
|
|
975
|
+
n_empty += 1
|
|
976
|
+
continue
|
|
977
|
+
if seg.isdigit():
|
|
978
|
+
n_digit += 1
|
|
979
|
+
continue
|
|
980
|
+
# 单字的空值语义哨兵(「无」「-」「—」)是**合法声明**,必须在长度
|
|
981
|
+
# 过滤**之前**判:「无」只有 1 字,若先过 len<2 就会被记成碎片,
|
|
982
|
+
# 永远救不回。多字的哨兵(「无依赖」「不适用」)**不在此列**——
|
|
983
|
+
# 它们本来就能通过长度过滤、一直作为可匹配短语存在,这里收窄口径
|
|
984
|
+
# 以免一次「修救援」把既有行为改掉。
|
|
985
|
+
if len(seg) < 2 and is_dep_sentinel(seg):
|
|
986
|
+
n_decl_sentinel += 1
|
|
987
|
+
continue
|
|
988
|
+
if len(seg) < 2:
|
|
989
|
+
n_short += 1
|
|
884
990
|
continue
|
|
885
991
|
if "全时窗" in seg or "任意时刻" in seg:
|
|
992
|
+
n_time_sentinel += 1
|
|
886
993
|
continue
|
|
887
994
|
if seg not in seen:
|
|
888
995
|
seen.add(seg)
|
|
889
996
|
out.append(seg)
|
|
890
|
-
return out
|
|
997
|
+
return {"terms": out, "n_empty": n_empty, "n_digit": n_digit,
|
|
998
|
+
"n_short": n_short, "n_time_sentinel": n_time_sentinel,
|
|
999
|
+
"n_decl_sentinel": n_decl_sentinel}
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
# 生效条件:text 为假值(None/空串)时返回 [];否则返回 condition_terms_diag 的 terms 字段(切分与过滤规则见该函数)。
|
|
1003
|
+
def cond_terms(text: str) -> list[str]:
|
|
1004
|
+
"""生效条件声明 → 匹配短语列表(确定性切分,无语义猜测)。
|
|
1005
|
+
|
|
1006
|
+
切分规则:按槽分隔「;/;」拆槽(condition_space_text 以「;」连四槽)
|
|
1007
|
+
→ 剥 legacy「观测位置:」与「槽标签:」前缀(标签是通用词,参与命中必误判)
|
|
1008
|
+
→ 槽内按「,,、/()」切短语 → 剥首尾标点(引号仅成对剥)→ 丢弃空/纯数字/
|
|
1009
|
+
空值语义哨兵/长度<2/全时窗哨兵短语(全时窗 = 时间维无信息量,不因其未命中而降级)。
|
|
1010
|
+
|
|
1011
|
+
**本函数是条件短语切分的唯一真源**:`MdCG._cond_terms` / `_cond_terms_diag`
|
|
1012
|
+
都只是它的别名,禁止在别处再写一份切分。
|
|
1013
|
+
"""
|
|
1014
|
+
return condition_terms_diag(text)["terms"]
|
|
891
1015
|
|
|
892
1016
|
|
|
893
1017
|
# ---- §5.1 六要素的**索引角色**:那张表从 4 行长成 6 行(P2-1)------------------
|
package/md_cg/op_table.py
CHANGED
|
@@ -38,6 +38,7 @@ _OP_TABLE = {
|
|
|
38
38
|
"params": (
|
|
39
39
|
"intent",
|
|
40
40
|
"view",
|
|
41
|
+
"strict_conditions",
|
|
41
42
|
),
|
|
42
43
|
"actions": (),
|
|
43
44
|
"default_action": None,
|
|
@@ -56,6 +57,7 @@ _OP_TABLE = {
|
|
|
56
57
|
"view",
|
|
57
58
|
"budget_tokens",
|
|
58
59
|
"dedupe",
|
|
60
|
+
"strict_conditions",
|
|
59
61
|
"exclude_doc",
|
|
60
62
|
"excerpt_strategy",
|
|
61
63
|
"bm25",
|
|
@@ -89,13 +91,14 @@ _OP_TABLE = {
|
|
|
89
91
|
},
|
|
90
92
|
"verify": {
|
|
91
93
|
"doc_order": 4,
|
|
92
|
-
"doc": "
|
|
94
|
+
"doc": "外部裁决回填(verdict=confirmed|weakened|falsified|superseded;superseded=被新版本取代:需 superseded_by,不删除、不动验证态);",
|
|
93
95
|
"impl": "inline",
|
|
94
96
|
"require": "verify",
|
|
95
97
|
"params": (
|
|
96
98
|
"depends_on",
|
|
97
99
|
"evidence",
|
|
98
100
|
"verdict",
|
|
101
|
+
"superseded_by",
|
|
99
102
|
"marker",
|
|
100
103
|
"fact",
|
|
101
104
|
"questions",
|
|
@@ -596,7 +599,7 @@ _OP_TABLE = {
|
|
|
596
599
|
"doc": "身份+健康+审核体系自描述;",
|
|
597
600
|
"impl": "inline",
|
|
598
601
|
"require": "info",
|
|
599
|
-
"params": ("min_severity", ),
|
|
602
|
+
"params": ("min_severity", "full"),
|
|
600
603
|
"actions": (),
|
|
601
604
|
"default_action": None,
|
|
602
605
|
"sigs": (),
|
|
@@ -614,6 +617,7 @@ _OP_TABLE = {
|
|
|
614
617
|
"max_files",
|
|
615
618
|
"max_items",
|
|
616
619
|
"incremental",
|
|
620
|
+
"allow_nested",
|
|
617
621
|
),
|
|
618
622
|
"actions": (),
|
|
619
623
|
"default_action": None,
|
|
@@ -624,7 +628,7 @@ _OP_TABLE = {
|
|
|
624
628
|
"doc": "按目录索引 md 文档的**章节**(只切 level<=3,只存标题+摘要、不存全文),doc_ref 指回原文行区间;layer 与密级显式声明(默认 knowledge/internal,路径命中私有提示降为 private);正文里的 --- 与围栏代码块内的 # 不会被误切;",
|
|
625
629
|
"impl": "inline",
|
|
626
630
|
"require": "index_doc",
|
|
627
|
-
"params": ("sensitivity", ),
|
|
631
|
+
"params": ("sensitivity", "allow_nested", ),
|
|
628
632
|
"actions": (),
|
|
629
633
|
"default_action": None,
|
|
630
634
|
"sigs": (),
|
|
@@ -845,7 +849,7 @@ _OP_TABLE = {
|
|
|
845
849
|
},
|
|
846
850
|
"maintain": {
|
|
847
851
|
"doc_order": 30,
|
|
848
|
-
"doc": "记忆维护(action=stat|history|importance|longterm|prefeed|separate|rollback|backfill|cap|exempt|vision_evidence|refine|propagate 及其 *_history;flush = **显式同步**把待落盘索引写完(issue #677 P2:写路径缺省异步落索引,要「确定已落盘」或要让索引面错误在带内暴露时用它;异常原样上抛,code/hint 可机读);reload = 显式代际探活;propagate = 验证态的**多跳**失效传播巡检(默认预演,apply 需 admin);权限按 action 分档:只读放行、prefeed 写入需 can_write、批量改写与 rollback 需 admin;dry-run
|
|
852
|
+
"doc": "记忆维护(action=stat|history|importance|longterm|prefeed|separate|rollback|backfill|cap|exempt|vision_evidence|refine|propagate|retag 及其 *_history;flush = **显式同步**把待落盘索引写完(issue #677 P2:写路径缺省异步落索引,要「确定已落盘」或要让索引面错误在带内暴露时用它;异常原样上抛,code/hint 可机读);reload = 显式代际探活;propagate = 验证态的**多跳**失效传播巡检(默认预演,apply 需 admin);权限按 action 分档:只读放行、prefeed 写入需 can_write、批量改写与 rollback 需 admin;dry-run 只出报表不改盘;retag = **只改元数据**(tags/condition_space/depends_on),正文与证据链原样,缺省 dry_run、apply 才落盘);",
|
|
849
853
|
"impl": "_maintain_call",
|
|
850
854
|
"require": "maintain",
|
|
851
855
|
"params": (
|
|
@@ -865,6 +869,11 @@ _OP_TABLE = {
|
|
|
865
869
|
"include_partial",
|
|
866
870
|
"min_conf",
|
|
867
871
|
"basis_text",
|
|
872
|
+
"tags_add",
|
|
873
|
+
"tags_remove",
|
|
874
|
+
"condition_space_patch",
|
|
875
|
+
"depends_on",
|
|
876
|
+
"dry_run",
|
|
868
877
|
),
|
|
869
878
|
"actions": (
|
|
870
879
|
"backfill",
|
|
@@ -879,6 +888,7 @@ _OP_TABLE = {
|
|
|
879
888
|
"prefeed",
|
|
880
889
|
"propagate",
|
|
881
890
|
"refine",
|
|
891
|
+
"retag",
|
|
882
892
|
"rollback",
|
|
883
893
|
"separate",
|
|
884
894
|
"stat",
|
package/md_cg/reconcile.py
CHANGED
|
@@ -64,15 +64,13 @@ def trace_path(cg) -> str:
|
|
|
64
64
|
return os.path.join(cg.root, TRACE_FILE)
|
|
65
65
|
|
|
66
66
|
|
|
67
|
-
# 生效条件:
|
|
68
|
-
def
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
#
|
|
73
|
-
|
|
74
|
-
raw = nodefile.content_hash(content or "")
|
|
75
|
-
return raw, nodefile.content_hash(_norm(content))
|
|
67
|
+
# 生效条件:content(nodefile.loads 切出的盘面正文,含尾换行)为 str 时返回 nodefile.content_hash(content)(单值)。
|
|
68
|
+
def _disk_hash(content: str) -> str:
|
|
69
|
+
# issue #758(在 #741 之后收单值):content_hash 内部已先 normalize_body(补一个尾换行)再哈希,
|
|
70
|
+
# 与索引条目口径**同源** ⇒ 「双形态接受集」的第二元素(对去尽尾换行的正文再哈希)在常盘形态下
|
|
71
|
+
# 恒等、仅在「≥2 个尾换行」时不等——而**索引侧**已无写点产出该形态(normalize_body 只补不剥) ⇒ 第二元素不可达,收成单值。
|
|
72
|
+
# 副作用(既定意图):收紧了旧容忍面——仅在「索引与盘面形态不一致」(手改/外部写者/#741 前 legacy 条目)时判 drift;经写路径落盘的 ≥2 尾换行两侧一致,不误报,旧行为靠第二元素蒙混。
|
|
73
|
+
return nodefile.content_hash(content or "")
|
|
76
74
|
|
|
77
75
|
|
|
78
76
|
# 生效条件:遍历 mdcg.LAYERS 各层目录树(os.walk,与 _scan_nodes 同序同口径),逐个 .md 读取并 nodefile.loads 切分;成功解析的以 nid(fm.get("id") 或文件名去 .md)登入 by_nid(同 nid 后到覆盖,与 _scan_nodes dict 覆盖语义一致)并登记 path_owner[relpath]=nid;读失败(OSError)/解码失败(UnicodeDecodeError)/frontmatter 无闭合(fm 为空且原文以 "---\n" 开头)的登入 problems(kind=read_error/decode_error/frontmatter_unterminated,附 relpath),不进 by_nid 与 path_owner;返回 (by_nid, path_owner, problems)。
|
|
@@ -169,9 +167,9 @@ def reconcile_state(cg, apply: bool = True) -> dict:
|
|
|
169
167
|
ghost.append(nid) # 盘面无其文件(或已归属他 id)
|
|
170
168
|
continue
|
|
171
169
|
d = by_nid[nid]
|
|
172
|
-
|
|
170
|
+
h_disk = _disk_hash(d["content"])
|
|
173
171
|
if entry.get("path") != d["path"] \
|
|
174
|
-
or entry.get("content_hash")
|
|
172
|
+
or entry.get("content_hash") != h_disk:
|
|
175
173
|
drift.append(nid)
|
|
176
174
|
|
|
177
175
|
# ② 以盘面为基准走一遍:缺索引条目(仅成功解析的真源参与)
|
package/md_cg/rollback.py
CHANGED
|
@@ -597,7 +597,22 @@ def rollback_mutation(cg, mutation, reason="", actor=None, force=False,
|
|
|
597
597
|
if cur_fp != pre_fp and not force: # 相等=幂等重放,放行(见 docstring)
|
|
598
598
|
want = mutation.get("after_hash")
|
|
599
599
|
if isinstance(want, dict):
|
|
600
|
-
|
|
600
|
+
# issue #741 升级兼容:after_hash 是**执行时点持久化**的指纹(decisions.jsonl)。
|
|
601
|
+
# 升级前执行、升级后回滚的单子,其 content_hash 是旧口径(sha 去尾原文),
|
|
602
|
+
# 与「现盘面按新口径重算」的 cur_fp 恒不等 ⇒ 会被误判 drifted_since_execute
|
|
603
|
+
# 而 fail-closed(方向保守但阻塞运维)。此处仅对**旧口径**一次性放行:
|
|
604
|
+
# fm 逐键相等且 content_hash 命中「旧口径 = sha(rstrip 正文)」时才视为未漂移;
|
|
605
|
+
# 真漂移(正文实改)仍不等,照旧 fail-closed。
|
|
606
|
+
_legacy_ok = False
|
|
607
|
+
if (want.get("fm") == cur_fp.get("fm")
|
|
608
|
+
and want.get("content_hash") != cur_fp.get("content_hash")):
|
|
609
|
+
import hashlib as _hl741
|
|
610
|
+
_node741 = _node_of(cg, tgt) or {}
|
|
611
|
+
_legacy741 = _hl741.sha256(
|
|
612
|
+
(_node741.get("content") or "").rstrip("\n").encode("utf-8")
|
|
613
|
+
).hexdigest()[:12]
|
|
614
|
+
_legacy_ok = (want.get("content_hash") == _legacy741)
|
|
615
|
+
if cur_fp != want and not _legacy_ok:
|
|
601
616
|
drift = {"ok": False, "error": "drifted_since_execute",
|
|
602
617
|
"action": act, "target": tgt, "preimage": rel,
|
|
603
618
|
"current": cur_fp, "expected_after_execute": want,
|
package/md_cg/scrub.py
CHANGED
|
@@ -484,7 +484,13 @@ _EXPIRY_KEYS = ("effective_until", "valid_until", "expires_at", "expire_at",
|
|
|
484
484
|
_SKIP_KEYS = ("功能名", "执行", "条件", "来源", "标签", "状态", "备注", "标题",
|
|
485
485
|
"描述", "name", "id", "title", "layer", "tags", "说明",
|
|
486
486
|
"假设", "否决原因", "验证")
|
|
487
|
-
|
|
487
|
+
# 引用/出处行的键形(本地实测:正文「…(源:src_x、src_y)」会被 partition(":") 切成
|
|
488
|
+
# 键「…(源」——单字「源」本来就被 2–12 长度闸过滤,真正的入口是**带括号的前缀键**)。
|
|
489
|
+
# 判据:**键里不得出现括号类标点**(字段名是标识符,不是半句话);值侧再拦「src_ 列表」形态。
|
|
490
|
+
_KEY_BAD_CHARS = "()()【】[]{}<>《》〈〉「」『』"
|
|
491
|
+
_SOURCE_VALUE_PREFIXES = ("src_", "ref_", "doc_")
|
|
492
|
+
_NEG_WORDS = ("禁止", "不得", "不要", "不能", "切勿", "避免", "不应", "不可",
|
|
493
|
+
"不会", "没有", "无须", "不必", "无需")
|
|
488
494
|
_POS_WORDS = ("应当", "建议", "必须", "需要", "可以", "允许", "推荐", "宜")
|
|
489
495
|
|
|
490
496
|
|
|
@@ -534,7 +540,11 @@ def _kv_pairs(content) -> dict:
|
|
|
534
540
|
# 互为「同键不同值」矛盾(实测 12 抽样里 9 条 high 全是这类误报,
|
|
535
541
|
# 且 apply 会把好节点 weaken+demote 并跨层移动文件)。
|
|
536
542
|
if (2 <= len(k) <= 12 and v and k not in _SKIP_KEYS
|
|
537
|
-
and not nodefile.is_ccg_mark(k)
|
|
543
|
+
and not nodefile.is_ccg_mark(k)
|
|
544
|
+
# issue(scrub 误判):括号类标点出现即不是字段名(引用/出处行)
|
|
545
|
+
and not any(ch in k for ch in _KEY_BAD_CHARS)
|
|
546
|
+
# 值侧兜底:纯引用 id 列表(src_x、ref_y)不是可断言的值
|
|
547
|
+
and not v.startswith(_SOURCE_VALUE_PREFIXES)):
|
|
538
548
|
out.setdefault(k, v)
|
|
539
549
|
break
|
|
540
550
|
return out
|
|
@@ -575,9 +585,85 @@ def _not_yet(fm, now):
|
|
|
575
585
|
return None
|
|
576
586
|
|
|
577
587
|
|
|
578
|
-
# 生效条件:对 content 取 kv_pairs
|
|
588
|
+
# 生效条件:对 content 取 kv_pairs(同键不同值,旧支)与**槽位主张**(`_slot_claims`),遍历 related
|
|
589
|
+
# 前 int(max_compare) 个 r 读对端正文;判定顺序:① 同键不同值;② **槽位比对**(`_slot_contradiction`:
|
|
590
|
+
# 同槽位自比排除 `功能名`,另含「主张 vs 对端拒绝域」交叉与 `执行↔生效条件/验证方式` 跨槽位;
|
|
591
|
+
# 两侧该槽位都非零且严格异号、共享 bigram ≥ SLOT_MIN_SHARED 且占比 ≥ 0.15(跨槽位用 Dice))。
|
|
592
|
+
# 【#444】返回的 why **只含键名 + 不可逆摘要(同键不同值)/ 槽位名 + 计数与占比(极性支)**,
|
|
593
|
+
# 两端正文原值与共享词面一律不进串(该串会经 verify 落三处明文面)。
|
|
594
|
+
#: 槽位比对的**绝对下限**(#747 复核 ①):剥模板后分母同步变小,仅看 ratio 会让 3 个巧合
|
|
595
|
+
#: 共享越过 0.15;破坏性不对称(weaken + demote + 跨层移动)故加绝对下限。
|
|
596
|
+
SLOT_MIN_SHARED = 5
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
def _slot_claims(content):
|
|
600
|
+
"""按六要素槽位切分正文:{mark: [主张行...]}(剥前缀;h2/h3 写法同样识别)。
|
|
601
|
+
|
|
602
|
+
「不适用条件」**保留**(#747 复核 ②:它是 rejection_domain,与 precondition 是同一份
|
|
603
|
+
契约的两半;整行丢掉会把最硬的一类矛盾也丢掉)。非模板行归入键 ""。
|
|
604
|
+
"""
|
|
605
|
+
out = {}
|
|
606
|
+
for raw in (content or "").splitlines():
|
|
607
|
+
line = raw.strip()
|
|
608
|
+
if not line:
|
|
609
|
+
continue
|
|
610
|
+
head = line.lstrip("#").strip()
|
|
611
|
+
mark = next((m for m in nodefile.CCG_MARKS if head.startswith(m)), None)
|
|
612
|
+
if mark is None:
|
|
613
|
+
out.setdefault("", []).append(line)
|
|
614
|
+
continue
|
|
615
|
+
rest = head[len(mark):]
|
|
616
|
+
# 分隔符校验(#747 复核):`执行成功后再验证` 这类自然语句不得算成「执行」槽。
|
|
617
|
+
if rest and rest[0] not in ":: \t -—":
|
|
618
|
+
out.setdefault("", []).append(line)
|
|
619
|
+
continue
|
|
620
|
+
claim = rest.lstrip("::\t -—").strip()
|
|
621
|
+
if claim:
|
|
622
|
+
out.setdefault(mark, []).append(claim)
|
|
623
|
+
return out
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
def _slot_contradiction(ca, cb):
|
|
627
|
+
"""按**槽位**判矛盾(#747):同槽位、或「主张 vs 对端拒绝域」两半契约。
|
|
628
|
+
|
|
629
|
+
判据:两侧该槽位**都非零且严格异号**(双值极性)+ 共享 bigram ≥ SLOT_MIN_SHARED 且占比 ≥ 0.15。
|
|
630
|
+
返回证据串或 None(证据串不含词面,见 #444)。
|
|
631
|
+
"""
|
|
632
|
+
sa, sb = _slot_claims(ca), _slot_claims(cb)
|
|
633
|
+
# 自比:**排除功能名**(它是 signature、不是主张,做矛盾主体最容易误判)。
|
|
634
|
+
pairs = [(m, m) for m in nodefile.CCG_MARKS if m != "功能名"]
|
|
635
|
+
# 拒绝域交叉:单点派生自 CCG_MARKS(不写第二份槽位枚举)。
|
|
636
|
+
for m in nodefile.CCG_MARKS:
|
|
637
|
+
if m == "不适用条件":
|
|
638
|
+
continue
|
|
639
|
+
pairs.append((m, "不适用条件"))
|
|
640
|
+
pairs.append(("不适用条件", m))
|
|
641
|
+
# 跨槽位(只有两条有契约理由):执行 ↔ 生效条件(precondition)/ 验证方式(postcondition)。
|
|
642
|
+
pairs.extend([("执行", "生效条件"), ("执行", "验证方式")])
|
|
643
|
+
for ma, mb in pairs:
|
|
644
|
+
ta = "\n".join(sa.get(ma, []))
|
|
645
|
+
tb = "\n".join(sb.get(mb, []))
|
|
646
|
+
if not ta or not tb:
|
|
647
|
+
continue
|
|
648
|
+
pa, pb = _polarity(ta), _polarity(tb)
|
|
649
|
+
if pa == 0 or pb == 0 or pa * pb > 0:
|
|
650
|
+
continue # 双值:任一侧中立不算矛盾(#747 ③)
|
|
651
|
+
ba, bb = bigrams(ta), bigrams(tb)
|
|
652
|
+
if not ba or not bb:
|
|
653
|
+
continue
|
|
654
|
+
shared = ba & bb
|
|
655
|
+
# 同槽位:短侧覆盖率;跨槽位:Dice(长度悬殊时更保守,避免新增对变误报源)。
|
|
656
|
+
if ma == mb:
|
|
657
|
+
ratio = len(shared) / max(1, min(len(ba), len(bb)))
|
|
658
|
+
else:
|
|
659
|
+
ratio = 2.0 * len(shared) / max(1, len(ba) + len(bb))
|
|
660
|
+
if len(shared) >= SLOT_MIN_SHARED and ratio >= 0.15:
|
|
661
|
+
return (f"槽位 {ma} / {mb} 极性相反且共享内容:共享片段 {len(shared)} 个"
|
|
662
|
+
f"(占比 {ratio:.2f}),词面不入证据串")
|
|
663
|
+
return None
|
|
664
|
+
|
|
579
665
|
def _contradiction(cg, content, related, max_compare=10):
|
|
580
|
-
"""与同族节点比对:同键不同值 /
|
|
666
|
+
"""与同族节点比对:同键不同值 / **按槽位**极性相反且共享内容(#747)。
|
|
581
667
|
|
|
582
668
|
issue #444(P0 密级泄漏,2026-10-17 U05c):本函数产出的 `why` 会经
|
|
583
669
|
`add("contradiction", why)` → `_weaken` → `cg.verify(..., "weakened")` 写成
|
|
@@ -592,7 +678,6 @@ def _contradiction(cg, content, related, max_compare=10):
|
|
|
592
678
|
return hashlib.sha1(str(v).encode("utf-8")).hexdigest()[:8]
|
|
593
679
|
|
|
594
680
|
kv_a = _kv_pairs(content)
|
|
595
|
-
bg_a, pol_a = bigrams(content or ""), _polarity(content)
|
|
596
681
|
for r in (related or [])[:int(max_compare)]:
|
|
597
682
|
oid = r["node_id"]
|
|
598
683
|
try:
|
|
@@ -608,15 +693,10 @@ def _contradiction(cg, content, related, max_compare=10):
|
|
|
608
693
|
return {"with": oid, "via": r.get("via"),
|
|
609
694
|
"why": f"同键不同值:{k}(本端 {_h(kv_a[k])} / "
|
|
610
695
|
f"对端 {_h(kv_b[k])})"}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
if len(shared) >= 3 and ratio >= 0.15:
|
|
616
|
-
# 同 #444:共享 bigram **词面也是正文片段**,同属泄漏面 ⇒ 只留计数与占比。
|
|
617
|
-
return {"with": oid, "via": r.get("via"),
|
|
618
|
-
"why": f"极性相反且共享内容:共享片段 {len(shared)} 个"
|
|
619
|
-
f"(占比 {ratio:.2f}),词面不入证据串"}
|
|
696
|
+
# #747:只按**槽位**比对(全文比会被六要素模板头 + 拒绝域噪声主导)。
|
|
697
|
+
slot_hit = _slot_contradiction(content or "", oc)
|
|
698
|
+
if slot_hit:
|
|
699
|
+
return {"with": oid, "via": r.get("via"), "why": slot_hit}
|
|
620
700
|
return None
|
|
621
701
|
|
|
622
702
|
|