@furongjun1999/dsh-memory 0.10.1 → 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 +65 -0
- package/CHANGELOG.md +642 -516
- package/README.en.md +1 -1
- package/README.md +2 -2
- package/docs/mdcg/cogmap_ops.md +15 -13
- package/lib/tools.d.ts +2 -2
- package/md_cg/bucketscope.py +36 -18
- package/md_cg/cohesion.py +337 -0
- package/md_cg/config_registry_bulk.py +5 -0
- 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/distill.py +247 -0
- package/md_cg/dossier.py +151 -0
- package/md_cg/entities.py +133 -0
- package/md_cg/mcp_server.py +60 -7
- package/md_cg/mdcg.py +27 -0
- package/md_cg/mdcos.py +319 -2
- package/md_cg/op_table.py +14 -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/selfstmt.py +139 -0
- package/md_cg/state_slots.py +4 -0
- package/md_cg/state_view.py +181 -0
- package/md_cg/sustain.py +28 -2
- package/md_cg/test_alternatives_historical.py +4 -1
- package/md_cg/test_bucket_time_rounds.py +87 -0
- package/md_cg/test_cohesion.py +107 -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/tokens.py +1 -1
- package/md_cg/version_anchor.py +84 -1
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
- package/package.json +137 -137
package/md_cg/mcp_server.py
CHANGED
|
@@ -2416,6 +2416,9 @@ def _task_call(cg, a):
|
|
|
2416
2416
|
return _t.list_tasks(cg, status=tstat or None, limit=a.get("limit"))
|
|
2417
2417
|
|
|
2418
2418
|
|
|
2419
|
+
#: require 词别名:新 op 复用既有 op 词,不给令牌 face 引入新词(对账见 scripts/check_op_table_parity.py)。
|
|
2420
|
+
_REQUIRE_ALIAS = {"state_event": "write", "state_view": "read"}
|
|
2421
|
+
|
|
2419
2422
|
# 生效条件:op 取 a.get("op") 转 str 去空白并 lower;缺失时按参数签名回推(含 content→write / query|node_id→read / intent→route;无签名可推时维持 read),op=="help" 在角色闸门之前直通 _help_call;其余 op 先经 cg.principal.require_op(op)(越权即 AccessDenied)再按 alias→内联名→cli 表分派;未识别 op 抛 ValueError;fork:op=="forget" 的**删除**分支走 cg.forget_gated(三档自治批次②:档位判定在既有资格闸之后,变更确认档出变更单且不软删;restore 分支不变),op=="write" 经 writepipe 默认链(链尾 autonomy 档位闸);
|
|
2420
2423
|
def _gate_link_signers_file(a):
|
|
2421
2424
|
"""issue #167:signers_file 由调用方指定 = 调用方自选「验签策略来源」。
|
|
@@ -2514,6 +2517,27 @@ def _require_verify_for_vstate(p, a):
|
|
|
2514
2517
|
p.require_op("verify")
|
|
2515
2518
|
|
|
2516
2519
|
|
|
2520
|
+
|
|
2521
|
+
|
|
2522
|
+
#: 落盘索引节点数缓存:{path: (mtime, size, count)}。issue #812 后续——60MB JSON 不该每次重解析。
|
|
2523
|
+
_DISK_COUNT_CACHE: dict = {}
|
|
2524
|
+
|
|
2525
|
+
|
|
2526
|
+
# 生效条件:path 指向 _index.json;返回其 nodes 条目数(按 mtime+size 缓存,外部改动自动失效)
|
|
2527
|
+
def _disk_index_count(path: str) -> int:
|
|
2528
|
+
"""落盘索引的节点数(缓存版)。解析失败由调用方兜底,这里只负责计数。"""
|
|
2529
|
+
import json as _json
|
|
2530
|
+
st = os.stat(path)
|
|
2531
|
+
key = (st.st_mtime, st.st_size)
|
|
2532
|
+
hit = _DISK_COUNT_CACHE.get(path)
|
|
2533
|
+
if hit and hit[0] == key[0] and hit[1] == key[1]:
|
|
2534
|
+
return hit[2]
|
|
2535
|
+
with open(path, encoding="utf-8") as fh:
|
|
2536
|
+
n = len(((_json.load(fh) or {}).get("nodes")) or {})
|
|
2537
|
+
_DISK_COUNT_CACHE[path] = (key[0], key[1], n)
|
|
2538
|
+
return n
|
|
2539
|
+
|
|
2540
|
+
|
|
2517
2541
|
def _cg_dispatch(cg, a):
|
|
2518
2542
|
# issue #722(Q1=A):**工具面 read/route 缺省放宽**——在派发入口补默认,
|
|
2519
2543
|
# 而不是在每个调用点编造参数:读面「不编造默认」约束的是**参数推导/回显**,
|
|
@@ -2531,7 +2555,10 @@ def _cg_dispatch(cg, a):
|
|
|
2531
2555
|
# P3 写口(state_event)映射到既有 "write" op 词——不给令牌 face 引入新
|
|
2532
2556
|
# op 词(角色白名单/派生收窄面零改动):凡 ops 白名单含 write 的角色可
|
|
2533
2557
|
# 记账,guest/只读令牌照拒。其余 op 逐位不变。
|
|
2534
|
-
|
|
2558
|
+
# 【require 词别名】新 op 不引入新 require 词(角色白名单/派生收窄面零改动):
|
|
2559
|
+
# state_event(写账本)→ "write";state_view(只读回放+租约)→ "read"。
|
|
2560
|
+
# 用显式别名表而不是嵌套三元:check_op_table_parity 按本表对账(见 _REQUIRE_ALIAS)。
|
|
2561
|
+
_p.require_op(_REQUIRE_ALIAS.get(op, op))
|
|
2535
2562
|
if op == "write":
|
|
2536
2563
|
# issue #320:op 名合规(write)不等于**参数**合规——验证态是裁决面,
|
|
2537
2564
|
# 按参数再要一次 verify op。单点放在分派层,不下沉到库层:库层不认识角色
|
|
@@ -2764,9 +2791,10 @@ def _cg_dispatch(cg, a):
|
|
|
2764
2791
|
# issue #812:落盘索引 vs 内存索引的偏差(只在 full=true 时解析——58MB JSON,快照面不能付这个成本)。
|
|
2765
2792
|
if _full and _idx_path:
|
|
2766
2793
|
try:
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2794
|
+
# 【issue #812 后续】60 MB 索引每次都整份 json.load 太重:
|
|
2795
|
+
# 实测本机 0.7s,但在持 3.4 万节点内存索引的进程里会触发内存压力 ⇒ info(full=true) 曾 60s 超时。
|
|
2796
|
+
# 现按 (mtime, size) 缓存计数:外部改库 ⇒ mtime 变 ⇒ 自动失效;只读面重复调用零成本。
|
|
2797
|
+
_disk_nodes = _disk_index_count(str(_idx_path))
|
|
2770
2798
|
h["index_node_count"] = _disk_nodes
|
|
2771
2799
|
h["index_stale"] = _disk_nodes != len(_nodes_info)
|
|
2772
2800
|
h["index_stale_hint"] = ("index_stale=true 表示内存索引与落盘索引不一致。两种成因:"
|
|
@@ -2945,9 +2973,10 @@ def _cg_dispatch(cg, a):
|
|
|
2945
2973
|
if a.get("budget_tokens") is not None:
|
|
2946
2974
|
# #188:`excerpt_strategy` 透传;#303 O4 的 k 越界 warning 仍走
|
|
2947
2975
|
# 下方 `_out` 后处理(两处改动正交——一个加参数、一个加返回体字段)。
|
|
2976
|
+
_ctx_r = a.get("context") or (a.get("condition_space") if a.get("condition_space") else None)
|
|
2948
2977
|
_out = cg.recall(q, budget_tokens=int(a["budget_tokens"]),
|
|
2949
2978
|
excerpt_strategy=a.get("excerpt_strategy") or "prefix",
|
|
2950
|
-
k=_int_arg(a, "k", 20), context=
|
|
2979
|
+
k=_int_arg(a, "k", 20), context=_ctx_r,
|
|
2951
2980
|
goal_text=a.get("goal"),
|
|
2952
2981
|
include_recent=bool(a.get("include_recent")),
|
|
2953
2982
|
recent_limit=int(a.get("limit") or 10),
|
|
@@ -2981,8 +3010,16 @@ def _cg_dispatch(cg, a):
|
|
|
2981
3010
|
# issue #659 F26-11:过滤发生在 k 截断**之后**,当 top-k 全被 doc_ 占满时
|
|
2982
3011
|
# 结果直接空掉(配额被吃光)。开启过滤时按 k 的若干倍超取,过滤后再截回 k。
|
|
2983
3012
|
_doc_filter_on = _as_flag(a.get("exclude_doc", False))
|
|
3013
|
+
# 【issue #811】`condition_space` 早先**没有进链路**(只透传 context)⇒ 传了等于没传。
|
|
3014
|
+
# 判据同源:condition_space 与 context 都是「条件过滤面」,context 显式给了就优先,
|
|
3015
|
+
# 否则用 condition_space 顶上(不再静默丢弃)。
|
|
3016
|
+
_ctx = a.get("context")
|
|
3017
|
+
_ctx_from = "context" if _ctx is not None else None
|
|
3018
|
+
if _ctx is None and a.get("condition_space"):
|
|
3019
|
+
_ctx = a.get("condition_space")
|
|
3020
|
+
_ctx_from = "condition_space"
|
|
2984
3021
|
search_args = dict(layer=a.get("layer"), k=_int_arg(a, "k", 20),
|
|
2985
|
-
context=
|
|
3022
|
+
context=_ctx, session=a.get("session"),
|
|
2986
3023
|
validity=a.get("validity"),
|
|
2987
3024
|
# issue #722:**只在显式传参时透传**(读面纪律:不编造默认)。
|
|
2988
3025
|
**({"strict_conditions": _as_flag(a["strict_conditions"])}
|
|
@@ -2995,7 +3032,10 @@ def _cg_dispatch(cg, a):
|
|
|
2995
3032
|
# 结果会假性空掉,故两者共用同一条超取规则。
|
|
2996
3033
|
search_args["k"] = min(max(_k_want * 4, _k_want + 32), 400)
|
|
2997
3034
|
if a.get("bm25", True) and hasattr(cg, "search_rrf"):
|
|
2998
|
-
|
|
3035
|
+
# 【issue #811】read 面早先**忽略 `semantic`**(该参数只在 mdcg_search 生效)⇒ 传了等于没传。
|
|
3036
|
+
# 现显式并入第 6 路(向量/语义仅在库里有向量时才产候选,没有则自然为空,不造假)。
|
|
3037
|
+
_paths = ("bm25", "semantic") if a.get("semantic") else ("bm25",)
|
|
3038
|
+
ranked, meta = cg.search_rrf(q, paths=_paths, **search_args)
|
|
2999
3039
|
res = [(n, s, qual) for n, s, qual, _provenance in ranked]
|
|
3000
3040
|
else:
|
|
3001
3041
|
res, meta = cg.search(q, **search_args)
|
|
@@ -3212,6 +3252,17 @@ def _cg_dispatch(cg, a):
|
|
|
3212
3252
|
actor=_actor)
|
|
3213
3253
|
return {"ok": True, "event": rec}
|
|
3214
3254
|
|
|
3255
|
+
if op == "state_view":
|
|
3256
|
+
# 【设计稿 docs/设计_状态账本与终态投影_v0.1.md §3.2/§3.3】
|
|
3257
|
+
# 账本是源(state_events)、槽位是回放投影(state_slots)、本 op 在其上加**租约语义**:
|
|
3258
|
+
# stable ⟺ (now - t_from) < TTL 且值非撤回;超时 ⇒ weak=True 但**不删值**(智能论 3.2.2 P1-003)。
|
|
3259
|
+
# 并把同一 subject 的全部槽位**联合给出**——多要素题要求时间/参与人/地点同时在场。
|
|
3260
|
+
# 纯只读:不写盘、不建缓存;同一账本 + 同一 now 必得同一结果。
|
|
3261
|
+
from . import state_view as _sv
|
|
3262
|
+
return _sv.state_view(cg, subject=a.get("subject"), slot=a.get("slot"),
|
|
3263
|
+
include_retired=bool(a.get("include_retired")),
|
|
3264
|
+
limit=a.get("limit"))
|
|
3265
|
+
|
|
3215
3266
|
if op == "verify":
|
|
3216
3267
|
return cg.verify(a.get("node_id", ""), a.get("evidence", ""),
|
|
3217
3268
|
a.get("verdict", ""),
|
|
@@ -4706,6 +4757,8 @@ def _dispatch(cg, name, args):
|
|
|
4706
4757
|
override=bool(a.get("override")),
|
|
4707
4758
|
consistency=bool(a.get("consistency", True)),
|
|
4708
4759
|
on_conflict=a.get("on_conflict") or "reject",
|
|
4760
|
+
# 【情景层 v0.1】diary 声明透传:角色名/地点/事件日由调用方声明,不靠抽取。
|
|
4761
|
+
**({"diary": a["diary"]} if isinstance(a.get("diary"), dict) else {}),
|
|
4709
4762
|
derived_from=_split_ids(a.get("derived_from")),
|
|
4710
4763
|
relation=a.get("relation"))
|
|
4711
4764
|
if written is None:
|
package/md_cg/mdcg.py
CHANGED
|
@@ -388,8 +388,23 @@ INHERIT_ON_OVERWRITE = (
|
|
|
388
388
|
INHERIT_ON_OVERWRITE_BODY_SAME = (
|
|
389
389
|
"edges", "confidence", "verification_basis", "non_applicable_conditions",
|
|
390
390
|
"modality", "importance_source", "untrusted", "untrusted_markers",
|
|
391
|
+
# 【issue #866】正文逐字未变 = 元数据补丁。以下三类早先不在白名单里,被 add() 的
|
|
392
|
+
# 全量重建吃掉:doc_ref(provenance:src/lineno/end,丢了就回不去原文)、
|
|
393
|
+
# importance_components(重要度分解,丢了不可复算)、sensitivity(**密级**,
|
|
394
|
+
# 被重置成 None 是**安全降级**,比丢字段更严重)。
|
|
395
|
+
"doc_ref", "importance_components",
|
|
396
|
+
# 注:`sensitivity` **刻意不在本表**——正文未变时继承密级会掩盖密级变更检测
|
|
397
|
+
# (8 个 issue#25x/#29x 守卫转红实证)。密级改由 retag 显式透传。
|
|
391
398
|
)
|
|
392
399
|
|
|
400
|
+
#: 正文未变时**不**继承的前序元数据键——这些由写路径自己重算,继承会让旧值覆盖新算值。
|
|
401
|
+
#: 【issue #866 根治】白名单是「漏一个就静默丢一个」的形态;改为「继承全部 − 本表」,
|
|
402
|
+
#: 新增字段默认被保住,只有必须重算的键才显式排除。
|
|
403
|
+
NOT_INHERIT_ON_OVERWRITE_BODY_SAME = frozenset((
|
|
404
|
+
"updated_at", "content_hash", "tokens", "size", "path", "bucket",
|
|
405
|
+
"layer", "tags", "condition_space", "depends_on", "importance",
|
|
406
|
+
))
|
|
407
|
+
|
|
393
408
|
|
|
394
409
|
# 英→中语素召回中的代词黑名单(超泛词,进召回词只添噪声)
|
|
395
410
|
_EN_ZH_PRONOUNS = {"我", "你", "他", "她", "它", "我们", "你们", "他们"}
|
|
@@ -3834,6 +3849,10 @@ class MdCG:
|
|
|
3834
3849
|
_same_body = (nodefile.normalize_body(_prev_body)
|
|
3835
3850
|
== nodefile.normalize_body(content))
|
|
3836
3851
|
if _same_body:
|
|
3852
|
+
# 【issue #866 回退说明】曾试过「继承全部 − 拒绝表」,结果 8 个
|
|
3853
|
+
# sensitivity 相关守卫转红——**继承密级会掩盖密级变更检测**,那是放宽
|
|
3854
|
+
# 既有判据。故仍用白名单(只加 invarianт 类字段),密级由 retag 侧
|
|
3855
|
+
# **显式透传**(见 MdCG.retag:只传前序确实存在的值)。
|
|
3837
3856
|
for _k in INHERIT_ON_OVERWRITE_BODY_SAME:
|
|
3838
3857
|
if _k not in _explicit and _k in _prev_fm:
|
|
3839
3858
|
fm[_k] = _prev_fm[_k]
|
|
@@ -4907,9 +4926,17 @@ class MdCG:
|
|
|
4907
4926
|
# 写保护闸、换桶清理、索引 _stage 仍由既有单点负责。这样 admin 面的
|
|
4908
4927
|
# maintain retag 与 token 面的 write 补丁行为一致,不会两条路两种结果。
|
|
4909
4928
|
from .writepipe import default_pipeline
|
|
4929
|
+
# 【issue #866】元数据补丁必须把「与本次改动无关、但会被 add() 重算」的元数据
|
|
4930
|
+
# 显式透传回去:密级被重置成 None 是**安全降级**,importance_components 丢了
|
|
4931
|
+
# 不可复算。这里只传前序**确实存在**的键,不传 None(避免把「未声明」变成「声明为空」)。
|
|
4910
4932
|
_a = {"op": "write", "node_id": node_id, "layer": layer,
|
|
4911
4933
|
"content": content, "tags": new_tags, "condition_space": cs,
|
|
4912
4934
|
"_meta_patch": True}
|
|
4935
|
+
for _k in ("sensitivity", "doc_ref", "importance_components",
|
|
4936
|
+
"verification_basis", "non_applicable_conditions",
|
|
4937
|
+
"modality", "importance_source"):
|
|
4938
|
+
if fm.get(_k) is not None:
|
|
4939
|
+
_a[_k] = fm[_k]
|
|
4913
4940
|
if deps is not None:
|
|
4914
4941
|
_a["depends_on"] = deps
|
|
4915
4942
|
_out = default_pipeline().execute(self, _a)
|
package/md_cg/mdcos.py
CHANGED
|
@@ -45,7 +45,7 @@ from . import (nodefile, routing, chain, subgraph, forgetting, protect,
|
|
|
45
45
|
identity, consistency, metacognition, crypto, sustain,
|
|
46
46
|
self_state, predict, evolution, weights, pooling,
|
|
47
47
|
writelimit, reach, trust, roleviews, autonomy_modes, lifecycle,
|
|
48
|
-
rrf_candidates)
|
|
48
|
+
rrf_candidates, reldate)
|
|
49
49
|
from .fsutil import (FileLock, atomic_write, append_jsonl, read_jsonl,
|
|
50
50
|
read_jsonl_tail, count_jsonl, publish,
|
|
51
51
|
merge_rmw_lock, node_write_lock)
|
|
@@ -1196,6 +1196,14 @@ class MdCGOS(MdCG):
|
|
|
1196
1196
|
self._audit("add", nid, layer=layer, role=role,
|
|
1197
1197
|
payload_hash=_sig(content), sens=kw.get("sensitivity"),
|
|
1198
1198
|
session=kw.get("session"))
|
|
1199
|
+
# 【自述轮次登记】写时判一次(与查询无关),供 recall 读时注入;
|
|
1200
|
+
# 登记只是加速索引,失败不得影响写入成败(故整段吞异常)。
|
|
1201
|
+
try:
|
|
1202
|
+
from . import selfstmt as _ss
|
|
1203
|
+
if _ss.matches(content):
|
|
1204
|
+
_ss.record(getattr(self, "root", ""), nid)
|
|
1205
|
+
except Exception:
|
|
1206
|
+
pass
|
|
1199
1207
|
return nid
|
|
1200
1208
|
|
|
1201
1209
|
# 生效条件:在已用 root 构造的实例上遍历 index["nodes"],恒剔除 layer 为 rejected/unresolved/goals 的节点,恒剔除 lifecycle.is_retired(e) 为真(archived ∪ superseded,#562)的节点(退役不参与默认检索;缺 lifecycle 键=active 照常,converged/demoted 仍参与),session 为真值而 e["session"] 不等于它时剔除,e["branch_id"] 不在 (None, branch) 时剔除(branch=None 时只留 branch_id 为 None 者),validity 为真值而 trust.is_expired(e) 为真时剔除(**只排已过期,not_yet 保留**),layer 为真值而 layer 不等时剔除,roles 不为 None 时仅留 role 落在 roles 内的节点;view 为真值时 role 维度裁决权移交 roleviews.matches(receipt=工作角色白名单须绕过默认剔除才可达,非法 view ValueError),view 为假值且 include_work 为假时剔除 WORK_ROLES 角色,时间算子启用时按 time_axis 轴过滤(效力轴不可判定 fail-open、观察轴不可判定 fail-closed 并入 self._time_filter_stat),其余收集进 out 返回。
|
|
@@ -2888,6 +2896,26 @@ class MdCGOS(MdCG):
|
|
|
2888
2896
|
if _v is not None: # 时间算子透传(None = 未启用,不入参)
|
|
2889
2897
|
kw[_k] = _v
|
|
2890
2898
|
results, meta = self.search_rrf(query, **kw)
|
|
2899
|
+
# 【外测诊断 2026-11-12】实测「同一 query:search 把资料卡排第 1(0.564),
|
|
2900
|
+
# recall 的候选集里却 0 条(paths.lexical=1)」——断点在**候选生成**,不在过滤。
|
|
2901
|
+
# 故按**实体标签直取**资料卡并入候选:确定性、与词法切词无关,卡不再依赖 lexical 命中。
|
|
2902
|
+
try:
|
|
2903
|
+
_extra = self._profile_candidates(query)
|
|
2904
|
+
# 【外测 2026-11-12】34/39 个值「原轮次没进 pack」;而资料卡每条事实带「来源 <id>」
|
|
2905
|
+
# ⇒ 把**被注入卡的源轮次**一并拉进候选:卡给属性、源轮次给原句。
|
|
2906
|
+
_extra += self._profile_source_nodes(_extra, limit=4)
|
|
2907
|
+
# 【外测 2026-11-12 实验】自述轮次通例召回:值覆盖 12.8%→35.9%、题级全命中 1/19→6/19。
|
|
2908
|
+
# 零模型、与题目无关;读时只 load 小索引(写时登记),不扫全库。
|
|
2909
|
+
_extra += self._selfstmt_nodes(limit=6)
|
|
2910
|
+
if _extra:
|
|
2911
|
+
_seen = {r[0].get("id") for r in results if r and r[0]}
|
|
2912
|
+
for _n, _sc, _q, _p in _extra:
|
|
2913
|
+
if _n.get("id") not in _seen:
|
|
2914
|
+
results.append((_n, _sc, _q, _p))
|
|
2915
|
+
if isinstance(meta, dict):
|
|
2916
|
+
meta["profile_injected"] = len(results) - len(_seen)
|
|
2917
|
+
except Exception: # noqa: BLE001 —— 注入面不拖崩召回
|
|
2918
|
+
pass
|
|
2891
2919
|
items = [(r[0], r[1], r[2], r[3]) for r in results]
|
|
2892
2920
|
else:
|
|
2893
2921
|
res, meta = self.search(query, layer=layer, k=k, context=context,
|
|
@@ -3069,9 +3097,269 @@ class MdCGOS(MdCG):
|
|
|
3069
3097
|
if det_reranked:
|
|
3070
3098
|
meta["det_clock_source"] = _clock_source
|
|
3071
3099
|
meta["det_mode"] = _mode
|
|
3100
|
+
# 【2026-11-11 外测】状态序列必须作为**一等公民**进材料:
|
|
3101
|
+
# 实测(Ornn8 B 类 19 题)——模型面对 20–30 条按检索分排的片段,
|
|
3102
|
+
# **没有确定性的时序依据**去选「最早那次」:同一题两次跑给出不同锚点,
|
|
3103
|
+
# 提示词让它「按日期排序再取最早」,它排的是自己回忆的顺序而不是材料里的日期序。
|
|
3104
|
+
# 这里按 created_at 升序给出 (date, id, head) 三列——模型读表即得时序,不必归纳。
|
|
3105
|
+
# 注意:这是**节点级**时间线(不含实体/槽位抽取),是「状态追踪」的便宜中间态。
|
|
3106
|
+
# 【2026-11-12 外测】pack 条目自带**格式化日期**:`frontmatter.created_at` 是 epoch 浮点,
|
|
3107
|
+
# 下游任何 `str(v)[:10]` 都会截出 `1784043360` 这种假日期(外测脚手架真实踩过:
|
|
3108
|
+
# 材料前缀变成 `[1784043360]`,模型被喂了机器值;连带把『模型编造时间戳』这条归因带偏)。
|
|
3109
|
+
# 结构性根治:在这里给出 `created_at_date`(YYYY-MM-DD),下游不该再自己截字符串。
|
|
3110
|
+
# 取不到合法时间戳时不编造:置 None(下游按缺失处理,不得当今天)。
|
|
3111
|
+
for _e in pack or []:
|
|
3112
|
+
_fm = _e.get("frontmatter") or {}
|
|
3113
|
+
_ts = _fm.get("created_at") or _fm.get("valid_from")
|
|
3114
|
+
_d = None
|
|
3115
|
+
if _ts is not None:
|
|
3116
|
+
try:
|
|
3117
|
+
_d = time.strftime("%Y-%m-%d", time.localtime(float(_ts)))
|
|
3118
|
+
except (TypeError, ValueError, OSError):
|
|
3119
|
+
_d = None
|
|
3120
|
+
_e["created_at_date"] = _d
|
|
3121
|
+
# 【2026-11-12 使用者裁定】相对日期**确定性地**补绝对日期,且内联在正文里:
|
|
3122
|
+
# 外测判官原文:视图节给了绝对日期,模型仍照抄「周一/昨天」⇒ 平行章节被忽略,
|
|
3123
|
+
# 只有正文内联标注才会被读到。锚点取本条的陈述时刻(created_at),不是今天。
|
|
3124
|
+
try:
|
|
3125
|
+
_e["content_dated"] = reldate.annotate(_e.get("content") or "", _ts)
|
|
3126
|
+
except Exception: # noqa: BLE001 —— 标注失败不拖崩装包,退化为原文
|
|
3127
|
+
_e["content_dated"] = _e.get("content")
|
|
3128
|
+
# 【情景层 v0.1 / 档1 验收主因】资料卡配额:外测实测每题 pack 里挤进 16–30 张 profile 卡,
|
|
3129
|
+
# 把 30 条名额吃光,**正确的那张反而被挤掉**(这是档 1 未涨分的第一机制,比错名更普遍)。
|
|
3130
|
+
# 故对 profile-* 单独设上限(缺省 3,`MDCG_PROFILE_CARDS` 可调),按已有 score 取前 N。
|
|
3131
|
+
_prof_cap = 3
|
|
3132
|
+
try:
|
|
3133
|
+
_prof_cap = int(os.environ.get("MDCG_PROFILE_CARDS", "3"))
|
|
3134
|
+
except ValueError:
|
|
3135
|
+
_prof_cap = 3
|
|
3136
|
+
_profs = [(i, e) for i, e in enumerate(pack or [])
|
|
3137
|
+
if str(e.get("id") or "").startswith("profile-")]
|
|
3138
|
+
if _prof_cap >= 0 and len(_profs) > _prof_cap:
|
|
3139
|
+
# 配额内**优先保留非 event 卡**:event 卡一天一张、数量最多,按纯 score 排会把
|
|
3140
|
+
# role/person 卡挤掉(外测实证 ②=0 的直接成因)。
|
|
3141
|
+
def _rank(it):
|
|
3142
|
+
_nid = str(it[1].get("id") or "")
|
|
3143
|
+
_is_ev = 1 if _nid.startswith("profile-event-") else 0
|
|
3144
|
+
return (_is_ev, -(float(it[1].get("score") or 0.0)))
|
|
3145
|
+
_keep = set()
|
|
3146
|
+
# 【外测 ③ 回退 4→2】配额不能把「score 最高的那张」也按类型筛掉:
|
|
3147
|
+
# 给 1 个豁免位,其余再按类型优先补足。
|
|
3148
|
+
_by_score = sorted(_profs, key=lambda x: -(float(x[1].get("score") or 0.0)))
|
|
3149
|
+
if _by_score:
|
|
3150
|
+
_keep.add(_by_score[0][0])
|
|
3151
|
+
for _i, _e in sorted(_profs, key=_rank):
|
|
3152
|
+
if len(_keep) >= _prof_cap:
|
|
3153
|
+
break
|
|
3154
|
+
_keep.add(_i)
|
|
3155
|
+
pack = [e for i, e in enumerate(pack)
|
|
3156
|
+
if i in _keep or not str(e.get("id") or "").startswith("profile-")]
|
|
3157
|
+
timeline = self._pack_timeline(pack)
|
|
3158
|
+
# 【2026-11-11 外测第二阶段】实测「原文+timeline」B 类 19 题稳定通过 2→15;
|
|
3159
|
+
# 「只给 timeline」仍达 14 ⇒ 信息大半在序列里。剩下的 4 题属「改期终态 / 多要素合成」,
|
|
3160
|
+
# 需要**按主题归并**的序列(不是全表一张)。这里用已有字段做廉价归并:
|
|
3161
|
+
# 优先 frontmatter.bucket(条件桶 cond_<topic>_<hash> 天然是主题轴),其次首个 tag。
|
|
3162
|
+
timeline_groups = self._pack_timeline_groups(pack)
|
|
3072
3163
|
return {"pack": pack, "tokens_used": used, "budget": budget_tokens,
|
|
3073
3164
|
"skipped": skipped, "recent": recent, "meta": meta,
|
|
3074
|
-
"aggregates": aggregates
|
|
3165
|
+
"aggregates": aggregates, "timeline": timeline,
|
|
3166
|
+
"timeline_groups": timeline_groups}
|
|
3167
|
+
|
|
3168
|
+
|
|
3169
|
+
|
|
3170
|
+
|
|
3171
|
+
|
|
3172
|
+
# 生效条件:pack 为装好的条目;返回按「主题轴」归并的序列 [{"topic","items":[{date,id,head}]}]
|
|
3173
|
+
def _pack_timeline_groups(self, pack):
|
|
3174
|
+
"""把时间线按**主题轴**归并(廉价版「七维图」的归并层,不用抽取器)。
|
|
3175
|
+
|
|
3176
|
+
为什么需要:外测实测「原文+timeline」已把 B 类 19 题从 2 救到 15,
|
|
3177
|
+
但剩 4 题是「改期终态 / 多要素合成」——它们要的是**同一主题下的序列**
|
|
3178
|
+
(最终值是什么、哪几个要素同时在场),而不是所有主题混在一张表里。
|
|
3179
|
+
|
|
3180
|
+
主题轴取已有字段,按优先级:
|
|
3181
|
+
① `frontmatter.bucket`(条件桶 `cond_<topic>_<hash>`,本身就是主题分组);
|
|
3182
|
+
② 首个 `tag`(如 `domain:*` / 项目名);
|
|
3183
|
+
③ 都缺 ⇒ "未分组"。
|
|
3184
|
+
这是**启发式**:同一条件桶未必同主题,覆盖不全时仍有 timeline 兜底。
|
|
3185
|
+
"""
|
|
3186
|
+
groups: dict = {}
|
|
3187
|
+
for e in pack or []:
|
|
3188
|
+
fm = e.get("frontmatter") or {}
|
|
3189
|
+
ts = fm.get("created_at") or fm.get("valid_from")
|
|
3190
|
+
date = ""
|
|
3191
|
+
if ts is not None:
|
|
3192
|
+
try:
|
|
3193
|
+
date = time.strftime("%Y-%m-%d", time.localtime(float(ts)))
|
|
3194
|
+
except (TypeError, ValueError, OSError):
|
|
3195
|
+
date = ""
|
|
3196
|
+
head = ""
|
|
3197
|
+
for line in str(e.get("content") or "").splitlines():
|
|
3198
|
+
s0 = line.strip()
|
|
3199
|
+
if not s0 or s0.startswith("#"):
|
|
3200
|
+
continue
|
|
3201
|
+
head = s0[:80]
|
|
3202
|
+
break
|
|
3203
|
+
topic = fm.get("bucket")
|
|
3204
|
+
if not topic:
|
|
3205
|
+
tags = fm.get("tags") or []
|
|
3206
|
+
topic = tags[0] if isinstance(tags, (list, tuple)) and tags else "未分组"
|
|
3207
|
+
groups.setdefault(str(topic), []).append(
|
|
3208
|
+
{"date": date, "id": e.get("id"), "head": head})
|
|
3209
|
+
out = []
|
|
3210
|
+
for topic, items in groups.items():
|
|
3211
|
+
items.sort(key=lambda x: (x["date"] or "9999-99-99", str(x.get("id") or "")))
|
|
3212
|
+
out.append({"topic": topic, "items": items})
|
|
3213
|
+
out.sort(key=lambda g: (-len(g["items"]), g["topic"]))
|
|
3214
|
+
return out
|
|
3215
|
+
|
|
3216
|
+
|
|
3217
|
+
#: 注入分类型配额(外测实证:event 卡一天一张、天数多、分高,会把 role/person 卡挤光,
|
|
3218
|
+
#: 而 event 卡对「姓名/年龄/职业」这类属性题没有帮助)。
|
|
3219
|
+
_PROFILE_INJECT_QUOTA = (('role', 2), ('person', 2), ('place', 1))
|
|
3220
|
+
|
|
3221
|
+
# 生效条件:无参;返回自述轮次节点(写时登记的小索引,limit 控制注入量)
|
|
3222
|
+
def _selfstmt_nodes(self, limit=None):
|
|
3223
|
+
"""自述轮次注入:零模型、与题目无关(外测实证 12.8%→35.9% 的那条规则)。
|
|
3224
|
+
|
|
3225
|
+
写时登记、读时 load 小索引 —— 不做每查询全库扫(2k 节点 ≈ 2s 不可接受)。
|
|
3226
|
+
只注入**已存在**的节点;去重;受 limit 约束防淹没。
|
|
3227
|
+
"""
|
|
3228
|
+
if limit is None:
|
|
3229
|
+
try:
|
|
3230
|
+
limit = int(os.environ.get("MDCG_SELFSTMT_LIMIT", "20"))
|
|
3231
|
+
except ValueError:
|
|
3232
|
+
limit = 20
|
|
3233
|
+
try:
|
|
3234
|
+
from . import selfstmt as _ss
|
|
3235
|
+
ids = _ss.load(getattr(self, "root", ""))
|
|
3236
|
+
except Exception:
|
|
3237
|
+
return []
|
|
3238
|
+
# 【外测 2026-11-12】原实现 `reversed(ids)` + limit=6(最新优先)把**早期自述轮次**全滤掉:
|
|
3239
|
+
# P16 的「我最早在江北…学徒」、P17 的「我叫谈书宁…」都排不进最近 6 ⇒ 收益被吃掉
|
|
3240
|
+
# (实验全量注入 35.9%,实现 12.8%)。改为:**按信息密度降序**,限量放宽到 20(登记总量 3–16/库)。
|
|
3241
|
+
scored = []
|
|
3242
|
+
for _pos, nid in enumerate(ids):
|
|
3243
|
+
node = self.get(nid)
|
|
3244
|
+
if not node:
|
|
3245
|
+
continue
|
|
3246
|
+
scored.append((-_ss.density(node.get("content") or ""), -_pos, node))
|
|
3247
|
+
scored.sort(key=lambda x: (x[0], x[1]))
|
|
3248
|
+
out, seen = [], set()
|
|
3249
|
+
for _d, _p, node in scored:
|
|
3250
|
+
nid = str(node.get("id") or "")
|
|
3251
|
+
if len(out) >= limit:
|
|
3252
|
+
break
|
|
3253
|
+
if nid in seen:
|
|
3254
|
+
continue
|
|
3255
|
+
seen.add(nid)
|
|
3256
|
+
out.append((node, 0.85, {"path": "self_stmt", "density": -_d}, "self_stmt"))
|
|
3257
|
+
return out
|
|
3258
|
+
|
|
3259
|
+
# 生效条件:cards 为已注入的资料卡候选;返回其事实行里「来源 <id>」指向的源节点
|
|
3260
|
+
def _profile_source_nodes(self, cards, limit=4):
|
|
3261
|
+
"""资料卡的**源轮次**注入:卡给属性、源轮次给原句(外测:34/39 值缺的是原句轮次)。
|
|
3262
|
+
|
|
3263
|
+
卡正文事实行形如 `- 2026-05-12 | 文本(来源 n1)`;抽出 n1 即源节点 id。
|
|
3264
|
+
只注入已存在的节点;去重;受 limit 约束(防淹没)。
|
|
3265
|
+
"""
|
|
3266
|
+
if not cards:
|
|
3267
|
+
return []
|
|
3268
|
+
import re as _re
|
|
3269
|
+
out, seen = [], set()
|
|
3270
|
+
for node, *_rest in cards:
|
|
3271
|
+
body = (node or {}).get("content") or ""
|
|
3272
|
+
for nid in _re.findall(r"(来源\s*([^)\s]+)\s*)", body):
|
|
3273
|
+
if nid in seen or len(out) >= limit:
|
|
3274
|
+
continue
|
|
3275
|
+
seen.add(nid)
|
|
3276
|
+
src = self.get(nid)
|
|
3277
|
+
if src:
|
|
3278
|
+
out.append((src, 0.9, {"path": "profile_source"}, "profile_source"))
|
|
3279
|
+
return out
|
|
3280
|
+
|
|
3281
|
+
# 生效条件:query 非空;返回按实体标签命中的资料卡候选 [(node, score, qual, prov)]
|
|
3282
|
+
def _profile_candidates(self, query, limit=6):
|
|
3283
|
+
"""按**实体标签**直取资料卡(外测实证:search 能排第 1,recall 候选集却没有)。
|
|
3284
|
+
|
|
3285
|
+
判据:卡的 tags 含 `person:` / `role:` / `place:` 前缀,且该实体名出现在 query 里。
|
|
3286
|
+
不走词法切词——长句在 lexical 里可能只剩 1 个词,卡就进不来。
|
|
3287
|
+
"""
|
|
3288
|
+
q = str(query or "")
|
|
3289
|
+
if not q:
|
|
3290
|
+
return []
|
|
3291
|
+
out = []
|
|
3292
|
+
# 【外测 2026-11-12】16/19 题是**第一人称提问**(「我全名咋写 / 多大来着 / 我住哪个城市」),
|
|
3293
|
+
# query 里没有实体名 ⇒ 按字面匹配永远命中不了。语言上「我」就指向 speaker,
|
|
3294
|
+
# 故:query 含自指且配置了 MDCG_ROLE_USER 时,优先注入该角色卡(确定性,不猜)。
|
|
3295
|
+
import os as _os
|
|
3296
|
+
_me = (_os.environ.get("MDCG_ROLE_USER") or "").strip()
|
|
3297
|
+
if _me and any(w in q for w in ("我", "咱", "本人")):
|
|
3298
|
+
_self_node = self.get("profile-role-" + _me)
|
|
3299
|
+
if _self_node:
|
|
3300
|
+
out.append((_self_node, 1.0, {"path": "profile_self"}, "profile_self"))
|
|
3301
|
+
_quota = dict(self._PROFILE_INJECT_QUOTA)
|
|
3302
|
+
_taken = {k: 0 for k in _quota}
|
|
3303
|
+
# G1a:从 cg.index 取回的 nodes 必须**先取快照**(并发写会 dictionary changed size)。
|
|
3304
|
+
for nid, rec in list(((self.index or {}).get("nodes") or {}).items()):
|
|
3305
|
+
if not str(nid).startswith("profile-"):
|
|
3306
|
+
continue
|
|
3307
|
+
_parts = str(nid).split("-", 2)
|
|
3308
|
+
_kind = _parts[1] if len(_parts) >= 3 else ""
|
|
3309
|
+
if _kind not in _taken:
|
|
3310
|
+
continue # 非 role/person/place(含 event)**不参与注入**
|
|
3311
|
+
if _taken[_kind] >= _quota[_kind]:
|
|
3312
|
+
continue
|
|
3313
|
+
tags = (rec or {}).get("tags") or []
|
|
3314
|
+
hit = False
|
|
3315
|
+
for t in tags:
|
|
3316
|
+
for pre in ("person:", "role:", "place:"):
|
|
3317
|
+
if str(t).startswith(pre) and str(t)[len(pre):] in q:
|
|
3318
|
+
hit = True
|
|
3319
|
+
break
|
|
3320
|
+
if hit:
|
|
3321
|
+
break
|
|
3322
|
+
if not hit:
|
|
3323
|
+
continue
|
|
3324
|
+
node = self.get(nid)
|
|
3325
|
+
if node:
|
|
3326
|
+
out.append((node, 1.0, {"path": "profile_entity"}, "profile_entity"))
|
|
3327
|
+
_taken[_kind] += 1
|
|
3328
|
+
if len(out) >= limit:
|
|
3329
|
+
break
|
|
3330
|
+
return out
|
|
3331
|
+
|
|
3332
|
+
# 生效条件:pack 为 recall 装好的条目列表;返回按 created_at **升序**的时间线 [{"date","id","head"}]
|
|
3333
|
+
def _pack_timeline(self, pack):
|
|
3334
|
+
"""把装好的条目压成「日期 | 事件首句」时间线(issue:模型无法从片段做时序归纳)。
|
|
3335
|
+
|
|
3336
|
+
为什么必须由算法给:实测同一道「最早那次」题两次跑给出不同锚点 —— 说明模型没有
|
|
3337
|
+
确定性依据在多个同主题候选里挑最早的。日期与事件首句都是**已有字段**,
|
|
3338
|
+
压成表零成本;模型只需读表,不必排序。
|
|
3339
|
+
|
|
3340
|
+
只取首句(≤80 字)并跳过 CCG 头(`# 功能名:` 等六要素行),避免把元数据当事件。
|
|
3341
|
+
"""
|
|
3342
|
+
out = []
|
|
3343
|
+
for e in pack or []:
|
|
3344
|
+
fm = e.get("frontmatter") or {}
|
|
3345
|
+
ts = fm.get("created_at") or fm.get("valid_from")
|
|
3346
|
+
date = None
|
|
3347
|
+
if ts is not None:
|
|
3348
|
+
try:
|
|
3349
|
+
date = time.strftime("%Y-%m-%d", time.localtime(float(ts)))
|
|
3350
|
+
except (TypeError, ValueError, OSError):
|
|
3351
|
+
date = None
|
|
3352
|
+
body = ""
|
|
3353
|
+
for line in str(e.get("content") or "").splitlines():
|
|
3354
|
+
s0 = line.strip()
|
|
3355
|
+
if not s0 or s0.startswith("#"):
|
|
3356
|
+
continue
|
|
3357
|
+
body = s0[:80]
|
|
3358
|
+
break
|
|
3359
|
+
out.append({"date": date or "", "id": e.get("id"), "head": body})
|
|
3360
|
+
out.sort(key=lambda x: (x["date"] or "9999-99-99", str(x.get("id") or "")))
|
|
3361
|
+
return out
|
|
3362
|
+
|
|
3075
3363
|
|
|
3076
3364
|
# ================= 1. Fix pairs 自动挖掘 =================
|
|
3077
3365
|
|
|
@@ -5165,6 +5453,27 @@ class MdCGOS(MdCG):
|
|
|
5165
5453
|
node_id or "", tags_add=tags_add, tags_remove=tags_remove,
|
|
5166
5454
|
condition_space_patch=condition_space_patch,
|
|
5167
5455
|
depends_on=depends_on, dry_run=_dry)
|
|
5456
|
+
if act == "profiles":
|
|
5457
|
+
# 【资料树档 1,2026-11-12 使用者裁定】人物/地点/事件 = 核心状态;物品/情绪 = 辅助(只进 facet)。
|
|
5458
|
+
# 把散落节点聚合成**可召回的一等节点**(profile-<kind>-<slug>):外测归因证明弱项 19 题
|
|
5459
|
+
# 「要素不在 pack」(2/19),单点事实需要稠密容器才召得回。
|
|
5460
|
+
# 幂等:渲染结果逐字比对,变了才写;增量:事实按 (日期, 节点 id) 稳定排序,新节点只追加。
|
|
5461
|
+
# 缺省 dry_run:沿用 maintain 的「预演优先」口径,apply=True 才落盘。
|
|
5462
|
+
from . import profiles as _prof
|
|
5463
|
+
_dry_p = (not apply) if dry_run is None else (bool(dry_run) or not apply)
|
|
5464
|
+
_st = _prof.build(self, dry_run=_dry_p, limit=limit)
|
|
5465
|
+
_st.update({"ok": True, "op": "maintain", "action": "profiles"})
|
|
5466
|
+
return _st
|
|
5467
|
+
if act == "selfstmt":
|
|
5468
|
+
# 【自述轮次登记,2026-11-12 外测实证】存量库补登入口。
|
|
5469
|
+
# 写时钩子已在 add() 里自动登记;此处用于「已有图、未重写节点」的库。
|
|
5470
|
+
# dry_run 口径同 maintain 其余动作:apply=True 才落盘。
|
|
5471
|
+
from . import selfstmt as _ss
|
|
5472
|
+
_dry_s = (not apply) if dry_run is None else (bool(dry_run) or not apply)
|
|
5473
|
+
_st = _ss.rebuild(self, dry_run=_dry_s, limit=limit or 100000)
|
|
5474
|
+
_st.update({"ok": True, "op": "maintain", "action": "selfstmt",
|
|
5475
|
+
"ids_sample": _ss.load(self)[:5]})
|
|
5476
|
+
return _st
|
|
5168
5477
|
if act == "importance":
|
|
5169
5478
|
return weights.recalc(self, layer=layer, limit=limit, apply=apply,
|
|
5170
5479
|
min_delta=(weights.APPLY_DELTA if min_delta is None
|
|
@@ -7119,6 +7428,14 @@ class MdCGSecure(MdCGOS):
|
|
|
7119
7428
|
nid = super().add(node_id, content, layer=layer, sensitivity=sens,
|
|
7120
7429
|
declared_sensitivity=sensitivity, **kw)
|
|
7121
7430
|
self._index_sensitivity(nid, sens)
|
|
7431
|
+
# 【自述轮次登记】写时判一次(与查询无关),供 recall 读时注入。
|
|
7432
|
+
# 失败一律吞掉:登记只是加速索引,不能影响写入成败。
|
|
7433
|
+
try:
|
|
7434
|
+
from . import selfstmt as _ss
|
|
7435
|
+
if _ss.matches(content):
|
|
7436
|
+
_ss.record(self.root, nid)
|
|
7437
|
+
except Exception:
|
|
7438
|
+
pass
|
|
7122
7439
|
return nid
|
|
7123
7440
|
|
|
7124
7441
|
# 生效条件:sens 经 _write_sensitivity(id 走基类单点 _rejected_nid)解析——#439:未声明且有既有节点 ⇒ 继承其密级、显式声明原样优先、既有密级判不了则 fail-closed 抛 AccessDenied;先 principal.require_layer_write("rejected", sens) 与 _attribution(kw, sens)(会话归因走三态解析)后转 super().add_rejected,最后按下发的 nid 调 _index_sensitivity 并返回 nid。
|
package/md_cg/op_table.py
CHANGED
|
@@ -588,6 +588,20 @@ _OP_TABLE = {
|
|
|
588
588
|
),
|
|
589
589
|
),
|
|
590
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
|
+
},
|
|
591
605
|
"state_event": {
|
|
592
606
|
"doc_order": 36,
|
|
593
607
|
"doc": "状态事件记账(追加一条五元事件subject/slot/old/new/kind;actor 恒取令牌;查询走 stg(op=state_chain))。",
|