@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.
Files changed (118) hide show
  1. package/CHANGELOG.en.md +515 -404
  2. package/CHANGELOG.md +642 -449
  3. package/README.en.md +244 -240
  4. package/README.md +272 -262
  5. package/codebuddy/CODEBUDDY.md +1 -1
  6. package/docs/discipline/templates/full.md.tmpl +1 -1
  7. package/docs/discipline/templates/rules.mdc.tmpl +1 -1
  8. package/docs/en/mdcg/world-model-endpoint.md +1 -1
  9. package/docs/mdcg/cogmap_ops.md +19 -16
  10. package/lib/client.js +29 -3
  11. package/lib/hooks.d.ts +12 -0
  12. package/lib/hooks.js +43 -1
  13. package/lib/tools.d.ts +2 -2
  14. package/md_cg/bucketindex.py +231 -0
  15. package/md_cg/bucketscope.py +357 -0
  16. package/md_cg/cohesion.py +337 -0
  17. package/md_cg/config_registry_bulk.py +24 -10
  18. package/md_cg/config_validate.py +14 -2
  19. package/md_cg/constraint_audit.py +231 -0
  20. package/md_cg/constraint_watch.py +206 -0
  21. package/md_cg/context_audit.py +240 -0
  22. package/md_cg/context_plan.py +142 -0
  23. package/md_cg/detrerank.py +137 -0
  24. package/md_cg/distill.py +247 -0
  25. package/md_cg/dossier.py +151 -0
  26. package/md_cg/entities.py +133 -0
  27. package/md_cg/fsutil.py +135 -0
  28. package/md_cg/govern/__init__.py +23 -0
  29. package/md_cg/iface/__init__.py +46 -0
  30. package/md_cg/iface/_verify.py +609 -0
  31. package/md_cg/iface/contracts.py +337 -0
  32. package/md_cg/judgment_manifest.py +14 -0
  33. package/md_cg/kernel/__init__.py +27 -0
  34. package/md_cg/mcp_server.py +6044 -5761
  35. package/md_cg/mdcg.py +501 -16
  36. package/md_cg/mdcos.py +546 -12
  37. package/md_cg/metacognition.py +66 -13
  38. package/md_cg/mutate_extract_shipped.py +126 -0
  39. package/md_cg/mutate_ledger_e2e.py +100 -0
  40. package/md_cg/op_table.py +22 -0
  41. package/md_cg/probe_c_ledger_integrity.py +258 -0
  42. package/md_cg/probe_d_retirement_discipline.py +263 -0
  43. package/md_cg/profiles.py +298 -0
  44. package/md_cg/protocol.py +7 -1
  45. package/md_cg/reldate.py +142 -0
  46. package/md_cg/review_cli.py +6 -1
  47. package/md_cg/run_tests.py +30 -1
  48. package/md_cg/scopeselect.py +214 -0
  49. package/md_cg/scrub.py +66 -9
  50. package/md_cg/selfstmt.py +139 -0
  51. package/md_cg/state_extract.py +149 -0
  52. package/md_cg/state_slots.py +62 -10
  53. package/md_cg/state_view.py +181 -0
  54. package/md_cg/structboost.py +179 -0
  55. package/md_cg/sustain.py +28 -2
  56. package/md_cg/test_alternatives_historical.py +165 -0
  57. package/md_cg/test_b2_sensitivity_landing.py +226 -223
  58. package/md_cg/test_b3_merge_keeps_content.py +608 -583
  59. package/md_cg/test_bucket_cards.py +85 -0
  60. package/md_cg/test_bucket_day_order.py +74 -0
  61. package/md_cg/test_bucket_quota.py +79 -0
  62. package/md_cg/test_bucket_recency_guards.py +75 -0
  63. package/md_cg/test_bucket_three_causes.py +83 -0
  64. package/md_cg/test_bucket_time_rounds.py +87 -0
  65. package/md_cg/test_bucketindex.py +130 -0
  66. package/md_cg/test_bucketscope_early.py +71 -0
  67. package/md_cg/test_cohesion.py +107 -0
  68. package/md_cg/test_cond_term_fold_gate.py +1210 -0
  69. package/md_cg/test_cond_term_neg_scope.py +147 -0
  70. package/md_cg/test_constraint_audit.py +87 -0
  71. package/md_cg/test_constraint_watch.py +108 -0
  72. package/md_cg/test_context_audit.py +110 -0
  73. package/md_cg/test_context_plan.py +81 -0
  74. package/md_cg/test_detrerank.py +157 -0
  75. package/md_cg/test_gate_four_kpi.py +136 -0
  76. package/md_cg/test_generation_guard.py +14 -2
  77. package/md_cg/test_issue192_move_layer_indices_guard.py +48 -0
  78. package/md_cg/test_issue39_utf8_stdio.py +572 -572
  79. package/md_cg/test_issue413_preimage_failclosed.py +147 -9
  80. package/md_cg/test_issue768_overwrite_time_window_anchor.py +135 -0
  81. package/md_cg/test_issue789_log_plaintext_probe.py +100 -0
  82. package/md_cg/test_issue790_verb_negation.py +77 -0
  83. package/md_cg/test_issue810_root_guard.py +71 -0
  84. package/md_cg/test_issue812_info_index_stale.py +78 -0
  85. package/md_cg/test_issue827_weaken_dedup.py +361 -0
  86. package/md_cg/test_issue829_calibration_positive_gate.py +473 -0
  87. package/md_cg/test_kernel_iface_contracts.py +18 -0
  88. package/md_cg/test_md_cg_no_hive_dep.py +238 -17
  89. package/md_cg/test_probe_cd_shipped.py +385 -0
  90. package/md_cg/test_recent_tail.py +93 -0
  91. package/md_cg/test_run_tests_entry_gates.py +110 -0
  92. package/md_cg/test_scopeselect.py +70 -0
  93. package/md_cg/test_scopeselect_clock.py +70 -0
  94. package/md_cg/test_session_attribution_consistency.py +835 -813
  95. package/md_cg/test_state_extract_shipped.py +162 -0
  96. package/md_cg/test_state_ledger_chain.py +207 -0
  97. package/md_cg/test_state_ledger_e2e.py +145 -0
  98. package/md_cg/test_structboost.py +102 -0
  99. package/md_cg/test_timeboost.py +83 -0
  100. package/md_cg/test_timeboost_relativedate.py +71 -0
  101. package/md_cg/test_topicagg.py +129 -0
  102. package/md_cg/timeboost.py +223 -0
  103. package/md_cg/tokens.py +1 -1
  104. package/md_cg/topicagg.py +142 -0
  105. package/md_cg/version_anchor.py +84 -1
  106. package/md_cg/whitebox_kb/wisdom/code_compose.py +33 -5
  107. package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
  108. package/md_cg/whitebox_kb/wisdom/compose_engine.py +5 -1
  109. package/md_cg/whitebox_kb/wisdom/role_compose.py +271 -268
  110. package/package.json +1 -1
  111. package/src/hooks.ts +43 -1
  112. package/ui/minimal/payload/build-ui.mjs +41 -2
  113. package/ui/minimal/payload/lib/client.js +29 -3
  114. package/ui/minimal/payload/web/app.js +3 -1
  115. package/ui/minimal/payload/web/client.js +30 -3
  116. package/web/app.js +3 -1
  117. package/web/client.js +30 -3
  118. package/zcode/AGENTS.md +1 -1
@@ -0,0 +1,83 @@
1
+ # -*- coding: utf-8 -*-
2
+ """时间桶重排(timeboost)守卫——主库「章节 = 写入日期」版结构信号。"""
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import sys
7
+ import tempfile
8
+ import time
9
+
10
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
11
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
12
+ sys.path.insert(0, _REPO)
13
+
14
+ from md_cg import timeboost as tb # noqa: E402
15
+ from md_cg.mdcos import MdCGOS # noqa: E402
16
+
17
+ FAILS = []
18
+ NOW = 1794139000.0
19
+
20
+
21
+ def ok(cond, name, detail=""):
22
+ if cond:
23
+ print(" PASS " + name)
24
+ else:
25
+ print(" FAIL " + name + " " + str(detail)[:180])
26
+ FAILS.append(name)
27
+
28
+
29
+ def node(nid, body, ts):
30
+ return {"id": nid, "content": body, "frontmatter": {"created_at": ts}}
31
+
32
+
33
+ def main():
34
+ print("=" * 66)
35
+ print("时间桶重排(timeboost)")
36
+ print("=" * 66)
37
+ d = tb.query_dates("2026-11-08 那天做了什么", NOW)
38
+ ok(d == {"2026-11-08"}, "T1 完整日期解析", d)
39
+ ok(tb.query_dates("11月8日 的记录", NOW) == {"2026-11-08"},
40
+ "T2 月日解析(年份取 now)", tb.query_dates("11月8日", NOW))
41
+ ok(len(tb.query_dates("今天写了什么", NOW)) == 1, "T3 相对词解析(今天)")
42
+ ok(tb.query_dates("浏览器 面板 白屏", NOW) == set(), "T4 无日期 ⇒ 空集合(不猜)")
43
+
44
+ ts_a = time.mktime(time.strptime("2026-11-08 12:00:00", "%Y-%m-%d %H:%M:%S"))
45
+ ts_b = time.mktime(time.strptime("2026-10-01 12:00:00", "%Y-%m-%d %H:%M:%S"))
46
+ ok(tb.node_date(node("x", "c", ts_a)) == "2026-11-08", "T5 候选日期取自 created_at")
47
+
48
+ pool = [(node("old", "无关" + "z" * 40, ts_b), 0.9, {}, []),
49
+ (node("new", "无关" + "y" * 40, ts_a), 0.5, {}, [])]
50
+ same = tb.rerank(pool, "浏览器 面板", NOW)
51
+ ok([x[0]["id"] for x in same] == ["old", "new"],
52
+ "T6 无日期查询 ⇒ 原序(零变更)", [x[0]["id"] for x in same])
53
+ ranked = tb.rerank(pool, "2026-11-08 那天", NOW)
54
+ ok(ranked[0][0]["id"] == "new", "T7 命中日期被提升", [x[0]["id"] for x in ranked])
55
+ again = tb.rerank(pool, "2026-11-08 那天", NOW)
56
+ ok([x[0]["id"] for x in ranked] == [x[0]["id"] for x in again], "T8 确定性")
57
+
58
+ os.environ.pop("MDCG_TIME_BOOST", None)
59
+ os.environ["MDCG_RECENT_TAIL"] = "0"
60
+ os.environ["MDCG_TOPIC_AGG"] = "0"
61
+ g = MdCGOS(tempfile.mkdtemp(prefix="tbg_"), actor="test")
62
+ ccg = ("# 功能名:{n}\n# 生效条件:语料\n# 子功能:s\n# 执行:e\n"
63
+ "# 验证方式:bench\n# 不适用条件:无\n\n{body}\n")
64
+ g.add("n_old", ccg.format(n="旧", s="w", e="1", body="话题乙 内容"), created_at=ts_b)
65
+ g.add("n_new", ccg.format(n="新", s="w", e="2", body="话题乙 内容"), created_at=ts_a)
66
+ _r0, m0 = g.search_rrf("话题乙", k=5, now=NOW)
67
+ ok(m0.get("time_boost") is False, "T9a 缺省关(meta.time_boost=False)", m0.get("time_boost"))
68
+ os.environ["MDCG_TIME_BOOST"] = "1"
69
+ try:
70
+ _r1, m1 = g.search_rrf("2026-11-08 话题乙", k=5, now=NOW)
71
+ ok(m1.get("time_boost") is True, "T9b 开启后 meta.time_boost=True")
72
+ finally:
73
+ os.environ.pop("MDCG_TIME_BOOST", None)
74
+ os.environ.pop("MDCG_RECENT_TAIL", None)
75
+ os.environ.pop("MDCG_TOPIC_AGG", None)
76
+
77
+ print()
78
+ print("timeboost:%d 项失败" % len(FAILS) if FAILS else "timeboost:全部通过")
79
+ return 1 if FAILS else 0
80
+
81
+
82
+ if __name__ == "__main__":
83
+ sys.exit(main())
@@ -0,0 +1,71 @@
1
+ # -*- coding: utf-8 -*-
2
+ """相对日期解析守卫(timeboost.query_dates)——Ornn8 六人物 714 题的短板项。
3
+
4
+ 背景(2026-11-10 外部测评):相对日期 19/44=43%、时间题 27/48=56%,是全部短板所在。
5
+ 根因之一是解析面只有「今天/昨天/前天」三词,且 MDCG_TIME_BOOST 默认 0。
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ import sys
11
+ import time
12
+
13
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
14
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15
+
16
+ from md_cg import timeboost as tb # noqa: E402
17
+
18
+ FAILS = []
19
+ NOW = time.mktime(time.strptime("2026-11-10 12:00:00", "%Y-%m-%d %H:%M:%S"))
20
+
21
+
22
+ def ok(cond, name, detail=""):
23
+ if cond:
24
+ print(" PASS " + name)
25
+ else:
26
+ print(" FAIL " + name + " " + str(detail)[:160])
27
+ FAILS.append(name)
28
+
29
+
30
+ def main():
31
+ print("=" * 66)
32
+ print("相对日期解析(timeboost.query_dates)")
33
+ print("=" * 66)
34
+ one = [
35
+ ("今天", "2026-11-10"),
36
+ ("昨天", "2026-11-09"),
37
+ ("前天", "2026-11-08"),
38
+ ("大前天", "2026-11-07"),
39
+ ("三天前", "2026-11-07"),
40
+ ("十天前", "2026-10-31"),
41
+ ("二十天前", "2026-10-21"),
42
+ ("12天前", "2026-10-29"),
43
+ ("上周三的约定", "2026-11-04"),
44
+ ("这周五去哪", "2026-11-13"),
45
+ ("11月8日的安排", "2026-11-08"),
46
+ ("2026-11-08 的记录", "2026-11-08"),
47
+ ]
48
+ for text, want in one:
49
+ got = sorted(tb.query_dates(text, NOW))
50
+ ok(got == [want], "T1 %s → %s" % (text, want), got)
51
+
52
+ wk = sorted(tb.query_dates("上周我们聊了什么", NOW))
53
+ ok(len(wk) == 7 and wk[0] == "2026-11-02" and wk[-1] == "2026-11-08",
54
+ "T2 上周 → 7 天(周一..周日)", wk)
55
+ mo = sorted(tb.query_dates("上个月的房租", NOW))
56
+ ok(len(mo) == 31 and mo[0] == "2026-10-01" and mo[-1] == "2026-10-31",
57
+ "T3 上个月 → 10 月整月", (len(mo), mo[:1], mo[-1:]))
58
+ ok(tb.query_dates("随便聊聊", NOW) == set(), "T4 无日期词 → 空集(不猜)")
59
+ ok(tb.query_dates("上周三", NOW) and len(tb.query_dates("上周三", NOW)) == 1,
60
+ "T5 「上周三」只取单日,不扩成整周", sorted(tb.query_dates("上周三", NOW)))
61
+ # 时钟必须显式:同一 query 在不同 now 下结果不同 ⇒ 证明没有内部 time.time()
62
+ other = NOW - 7 * 86400
63
+ ok(tb.query_dates("三天前", NOW) != tb.query_dates("三天前", other), "T6 时钟由调用方注入(可回放)")
64
+
65
+ print()
66
+ print("全部通过" if not FAILS else "失败 %d 项" % len(FAILS))
67
+ return 1 if FAILS else 0
68
+
69
+
70
+ if __name__ == "__main__":
71
+ sys.exit(main())
@@ -0,0 +1,129 @@
1
+ # -*- coding: utf-8 -*-
2
+ """读取策略改造 · A 项守卫:跨轮状态聚合(topicagg)。
3
+
4
+ 契约:同话题命中 >=2 条才成组;组内按时间正序;state 只读既有 metadata
5
+ (不自造,issue #111 同纪律);MDCG_TOPIC_AGG=0 关闭;聚合不改 pack 顺序。
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import os
11
+ import sys
12
+ import tempfile
13
+
14
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
15
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16
+ sys.path.insert(0, _REPO)
17
+
18
+ from md_cg import topicagg # noqa: E402
19
+ from md_cg.mdcos import MdCGOS # noqa: E402
20
+
21
+ FAILS = []
22
+ CCG = ("# 功能名:{n}\n# 生效条件:{c}\n# 子功能:s\n# 执行:e\n"
23
+ "# 验证方式:test\n# 不适用条件:无\n\n{b}\n")
24
+
25
+
26
+ def ok(cond, name, detail=""):
27
+ if cond:
28
+ print(" PASS " + name)
29
+ else:
30
+ print(" FAIL " + name + " " + str(detail)[:200])
31
+ FAILS.append(name)
32
+
33
+
34
+ def entry(eid, tags, t, state=None, label=None):
35
+ return {"id": eid,
36
+ "frontmatter": {"tags": tags, "created_at": t},
37
+ "metadata": {"state": state},
38
+ "content": "# 功能名:" + (label or eid) + "\n# 生效条件:x\n"}
39
+
40
+
41
+ def main():
42
+ print("=" * 66)
43
+ print("跨轮状态聚合(topicagg)")
44
+ print("=" * 66)
45
+ pack = [entry("a9", ["灯箱"], 100.0, "ACCEPT", "灯箱进度(轮9)"),
46
+ entry("a20", ["灯箱"], 200.0, "ACCEPT", "灯箱进度(轮20)"),
47
+ entry("b1", ["其它"], 150.0, None, "无关"),
48
+ entry("c1", [], 120.0, None, "无标签")]
49
+ ag = topicagg.aggregate(pack, "灯箱 进行到哪一步")
50
+ ok(len(ag) == 1, "A1 只有一个话题成组", [g["topic"] for g in ag])
51
+ ok(ag and ag[0]["topic"] == "灯箱" and ag[0]["count"] == 2,
52
+ "A2 组名/成员数正确", ag[0] if ag else None)
53
+ ok(ag and ag[0]["ids"] == ["a9", "a20"], "A3 ids 按时间正序",
54
+ ag[0]["ids"] if ag else None)
55
+ ok(ag and ag[0]["timeline"][0]["label"] == "灯箱进度(轮9)",
56
+ "A4 label 只取功能名行", ag[0]["timeline"][0] if ag else None)
57
+ ok(ag and ag[0]["timeline"][0]["state"] == "ACCEPT",
58
+ "A5 state 只读既有 metadata", ag[0]["timeline"][0] if ag else None)
59
+ ok(topicagg.aggregate([entry("x", ["灯箱"], 1.0)], "灯箱") == [],
60
+ "A6 单条不成组")
61
+ ok(topicagg.aggregate([], "灯箱") == [], "A7 空输入不炸")
62
+
63
+ root = tempfile.mkdtemp(prefix="tagg_")
64
+ g = MdCGOS(root, actor="test")
65
+ g.add("lamp9", CCG.format(n="灯箱进度(轮9)", c="灯箱 进度", b="已有报价"), tags=["灯箱"])
66
+ g.add("lamp20", CCG.format(n="灯箱进度(轮20)", c="灯箱 进度", b="仍未安排制作"), tags=["灯箱"])
67
+ pack2 = g.recall("灯箱 进行到哪一步", budget_tokens=2000, k=10)
68
+ ag2 = pack2.get("aggregates") or []
69
+ # A8 的口径在**加入跨轮闸**后变严:`add()` 连续两次写入几乎同一时刻,
70
+ # 时间跨度 < MIN_SPAN_SECONDS ⇒ 判为「同一批材料」不成组(这正是闸的目的)。
71
+ # 端到端的「成组」用例由 F5 用拉开的时间戳覆盖;此处只钉**接线**:
72
+ # aggregates 键必须存在(列表),且同批写入不产出假组。
73
+ ok("aggregates" in pack2 and isinstance(ag2, list),
74
+ "A8 recall 端到端接通 aggregates 键(列表)", type(ag2).__name__)
75
+ ok(ag2 == [], "A8b 同批写入(跨度 < 30s)不产出假组", ag2)
76
+
77
+ os.environ["MDCG_TOPIC_AGG"] = "0"
78
+ try:
79
+ pack3 = g.recall("灯箱 进行到哪一步", budget_tokens=2000, k=10)
80
+ ok(pack3.get("aggregates") == [], "A9 MDCG_TOPIC_AGG=0 关闭")
81
+ finally:
82
+ os.environ.pop("MDCG_TOPIC_AGG", None)
83
+
84
+ p4 = g.recall("灯箱 进行到哪一步", budget_tokens=2000, k=10)
85
+ ok([e["id"] for e in p4["pack"]] == [e["id"] for e in pack2["pack"]],
86
+ "A10 聚合不改 pack 顺序(纯后处理)")
87
+
88
+ # ---------- 兜底与闸(F 组):bench 读数驱动,缺省关 ----------
89
+ def entry_t(eid, content, t, tags=None):
90
+ return {"id": eid, "frontmatter": {"tags": tags or [], "created_at": t},
91
+ "metadata": {}, "content": "# 功能名:" + eid + "\n" + content}
92
+
93
+ os.environ.pop("MDCG_TOPIC_AGG_TAGFALLBACK", None)
94
+ try:
95
+ p_untagged = [entry_t("x1", "灯箱 报价", 1000.0), entry_t("x2", "灯箱 制作", 2000.0)]
96
+ ok(topicagg.aggregate(p_untagged, "灯箱") == [],
97
+ "F1 缺省关:无 tag 时不聚合(行为不变)")
98
+
99
+ os.environ["MDCG_TOPIC_AGG_TAGFALLBACK"] = "1"
100
+ ag = topicagg.aggregate(p_untagged, "灯箱")
101
+ ok(len(ag) == 1 and ag[0]["topic"] == "灯箱" and ag[0]["count"] == 2,
102
+ "F2 开启兜底:正文命中话题词即成组", ag)
103
+
104
+ p_generic = [entry_t("g1", "内容 甲乙", 1000.0), entry_t("g2", "内容 丙丁", 2000.0)]
105
+ ok(topicagg.aggregate(p_generic, "内容") == [],
106
+ "F3 泛词闸:停用词不作组名(防「泛词聚一切」)")
107
+
108
+ p_short = [entry_t("s1", "灯箱 报价", 1000.0), entry_t("s2", "灯箱 制作", 1005.0)]
109
+ ok(topicagg.aggregate(p_short, "灯箱") == [],
110
+ "F4 跨轮闸:时间跨度 < 30s 不算跨轮")
111
+
112
+ p_span = [entry_t("d1", "灯箱 报价", 1000.0), entry_t("d2", "灯箱 制作", 1300.0)]
113
+ ok(len(topicagg.aggregate(p_span, "灯箱")) == 1,
114
+ "F5 跨轮闸放行:跨度 >= 30s 正常成组")
115
+
116
+ r1 = topicagg.aggregate(p_untagged, "灯箱")
117
+ r2 = topicagg.aggregate(p_untagged, "灯箱")
118
+ ok(json.dumps(r1, ensure_ascii=False) == json.dumps(r2, ensure_ascii=False),
119
+ "F6 确定性:同输入两次结果相同")
120
+ finally:
121
+ os.environ.pop("MDCG_TOPIC_AGG_TAGFALLBACK", None)
122
+
123
+ print()
124
+ print("topicagg:%d 项失败" % len(FAILS) if FAILS else "topicagg:全部通过")
125
+ return 1 if FAILS else 0
126
+
127
+
128
+ if __name__ == "__main__":
129
+ sys.exit(main())
@@ -0,0 +1,223 @@
1
+ # -*- coding: utf-8 -*-
2
+ """时间桶重排(timeboost)——主库的「章节 = 写入日期」版结构信号。
3
+
4
+ 为什么不是章节路由(实测,2026-11-09):主库 34,218 节点里**没有章节结构**,
5
+ tag 是 `code` / `domain:*` / 项目名这类,而 `created_at` 100% 覆盖、按天聚集
6
+ (2026-11-08 一天 77 条、2026-09-29 一天 67 条)。
7
+ 使用者口径:**每日写信息的时间就是章节**。故结构信号应按天路由,而不是按标题。
8
+
9
+ 设计(与 structboost 同一纪律):
10
+ · 从查询里解析**显式日期**(2026-11-08 / 11月8日 / 11-08)与**相对词**(今天/昨天/前天);
11
+ · 命中的日期集合为空 ⇒ 本条信号**整条不生效**(绝不猜日期);
12
+ · 命中 ⇒ 该日期内的候选 × TIME_BOOST(乘性提升,不硬过滤——日期可能记错);
13
+ · 时钟必须由调用方传入(`now`),不内部取 time.time(),保证可回放。
14
+ """
15
+ from __future__ import annotations
16
+
17
+ import re
18
+ import time
19
+
20
+ #: 命中日期的提升倍数(与 structboost 的章节提升同量级)。
21
+ TIME_BOOST = 2.0
22
+
23
+ #: 相对词 → 往前几天。**长词必须排在短词之前**(「大前天」含「前天」,顺序错就会少算一天)。
24
+ _REL = {"大前天": 3, "今天": 0, "今日": 0, "昨天": 1, "昨日": 1, "前天": 2}
25
+
26
+
27
+ def _date_of(ts):
28
+ "把一个 epoch 秒转成本地日期字符串 YYYY-MM-DD。"
29
+ try:
30
+ return time.strftime("%Y-%m-%d", time.localtime(float(ts)))
31
+ except (TypeError, ValueError, OSError):
32
+ return None
33
+
34
+
35
+ _CN_NUM = {"一": 1, "两": 2, "二": 2, "三": 3, "四": 4, "五": 5,
36
+ "六": 6, "七": 7, "八": 8, "九": 9, "十": 10}
37
+
38
+
39
+ def _cn_or_int(s):
40
+ """「三」/「十三」/「二十三」/「12」→ 整数;解析不出返回 None(不猜)。"""
41
+ s = str(s or "").strip()
42
+ if not s:
43
+ return None
44
+ if s.isdigit():
45
+ return int(s)
46
+ if s in _CN_NUM:
47
+ return _CN_NUM[s]
48
+ if "十" in s:
49
+ head, _, tail = s.partition("十")
50
+ h = _CN_NUM.get(head, 1) if head else 1
51
+ t = _CN_NUM.get(tail, 0) if tail else 0
52
+ return h * 10 + t
53
+ return None
54
+
55
+
56
+ #: 中文数字星期 → 0..6(周一=0)
57
+ _WD = {"一": 0, "二": 1, "三": 2, "四": 3, "五": 4, "六": 5, "日": 6, "天": 6,
58
+ "1": 0, "2": 1, "3": 2, "4": 3, "5": 4, "6": 5, "7": 6}
59
+
60
+
61
+ def _day_start(ts):
62
+ """把 epoch 秒对齐到本地当天 00:00(用于「按天」运算)。"""
63
+ lt = time.localtime(float(ts))
64
+ return time.mktime((lt.tm_year, lt.tm_mon, lt.tm_mday, 0, 0, 0, 0, 0, -1))
65
+
66
+
67
+ def _add_days(ts, n):
68
+ return _day_start(ts) + n * 86400
69
+
70
+
71
+ def _weekday_dates(q, now):
72
+ """「上周三 / 这周三 / 本周三 / 上周天」→ 单个日期;无修饰的「周三」不解析(歧义太大)。"""
73
+ out = set()
74
+ for m in re.finditer(r"(上周|上星期|这周|这星期|本周|本星期)([一二三四五六日天1-7])", q):
75
+ wd = _WD.get(m.group(2))
76
+ if wd is None:
77
+ continue
78
+ back_weeks = 1 if m.group(1).startswith(("上周", "上星期")) else 0
79
+ today = _day_start(now)
80
+ lt = time.localtime(today)
81
+ cur_wd = lt.tm_wday # 周一=0
82
+ target = today + (wd - cur_wd) * 86400 - back_weeks * 7 * 86400
83
+ out.add(_date_of(target))
84
+ return out
85
+
86
+
87
+ def _week_range(q, now):
88
+ """「上周 / 这周 / 本周」→ 整周 7 天(周一..周日)。"""
89
+ out = set()
90
+ if not any(w in q for w in ("上周", "上星期", "这周", "这星期", "本周", "本星期")):
91
+ return out
92
+ # 与「上周三」这类**单日**写法互斥:出现星期字符时只取单日(避免把整周都塞进去)
93
+ if re.search(r"(上周|上星期|这周|这星期|本周|本星期)[一二三四五六日天1-7]", q):
94
+ return out
95
+ back = 1 if any(w in q for w in ("上周", "上星期")) else 0
96
+ today = _day_start(now)
97
+ monday = today - time.localtime(today).tm_wday * 86400 - back * 7 * 86400
98
+ for i in range(7):
99
+ out.add(_date_of(monday + i * 86400))
100
+ return out
101
+
102
+
103
+ def _month_range(q, now):
104
+ """「上个月 / 上月 / 本月 / 这个月」→ 整月日期(最多 31 天)。"""
105
+ out = set()
106
+ back = None
107
+ if any(w in q for w in ("上个月", "上月")):
108
+ back = 1
109
+ elif any(w in q for w in ("这个月", "本月")):
110
+ back = 0
111
+ if back is None:
112
+ return out
113
+ lt = time.localtime(float(now))
114
+ y, m = lt.tm_year, lt.tm_mon - back
115
+ if m <= 0:
116
+ y, m = y - 1, m + 12
117
+ for d in range(1, 32):
118
+ try:
119
+ t = time.mktime((y, m, d, 0, 0, 0, 0, 0, -1))
120
+ except (ValueError, OverflowError):
121
+ break
122
+ if time.localtime(t).tm_mon != m:
123
+ break
124
+ out.add(_date_of(t))
125
+ return out
126
+
127
+
128
+ def query_dates(query, now):
129
+ """从查询里解析目标日期集合(确定性);解析不出返回空集合。"""
130
+ q = str(query or "")
131
+ out = set()
132
+ # 1) 完整日期:2026-11-08 / 2026/11/08
133
+ for m in re.finditer(r"(20\d{2})[-/](\d{1,2})[-/](\d{1,2})", q):
134
+ out.add("%04d-%02d-%02d" % (int(m.group(1)), int(m.group(2)), int(m.group(3))))
135
+ # 2) 月日:「11月8日」——年份取 now 的年份(跨年场景留给调用方补全年份)
136
+ for m in re.finditer(r"(\d{1,2})\s*月\s*(\d{1,2})\s*[日号]", q):
137
+ y = time.localtime(float(now)).tm_year
138
+ out.add("%04d-%02d-%02d" % (y, int(m.group(1)), int(m.group(2))))
139
+ # 3) 相对词:今天/昨天/前天/大前天
140
+ # 长词优先 + **已命中的长词吸收其子串**(否则「大前天」会同时命中「前天」,多算一天)
141
+ _matched = []
142
+ for word, back in sorted(_REL.items(), key=lambda kv: -len(kv[0])):
143
+ if any(word in m for m in _matched):
144
+ continue
145
+ if word in q:
146
+ _matched.append(word)
147
+ out.add(_date_of(float(now) - back * 86400))
148
+ # 4) 「N 天前 / N 天以前」——N 支持阿拉伯数字与中文数字(「三天前」在真实语料里更常见)
149
+ for m in re.finditer(r"(\d{1,3}|[一二两三四五六七八九十]{1,3})\s*天\s*(?:以)?前", q):
150
+ n = _cn_or_int(m.group(1))
151
+ if n is not None:
152
+ out.add(_date_of(float(now) - n * 86400))
153
+ # 5) 「N 天前 / N 天后」的兄弟写法:「前 N 天」「N 天后」不解析(指向未来或无锚)
154
+ # 6) 星期:上周三 / 这周三 / 本周三 / 周三
155
+ out.update(_weekday_dates(q, float(now)))
156
+ # 7) 周区间:上周 / 这周 / 本周(整周 7 天)
157
+ out.update(_week_range(q, float(now)))
158
+ # 8) 月区间:上个月 / 上月 / 本月 / 这个月(整月,最多 31 天)
159
+ out.update(_month_range(q, float(now)))
160
+ return {d for d in out if d}
161
+
162
+
163
+ def node_date(node):
164
+ """候选节点的写入日期(created_at → 本地日期)。"""
165
+ fm = node.get("frontmatter") or {}
166
+ ts = fm.get("created_at") or fm.get("valid_from")
167
+ return _date_of(ts) if ts else None
168
+
169
+
170
+ def rerank(results, query, now, k=None):
171
+ """按「查询提到的日期」做稳定重排(同分保持原序,确定性)。
172
+
173
+ `now` 必须由调用方给(时钟显式)。日期集合为空 ⇒ 原样返回(零变更)。
174
+ """
175
+ dates = query_dates(query, now)
176
+ items = list(results or ())
177
+ if not dates or not items:
178
+ return items[:k] if k else items
179
+ scored = []
180
+ for idx, item in enumerate(items):
181
+ node = item[0]
182
+ try:
183
+ score = float(item[1])
184
+ except (TypeError, ValueError, IndexError):
185
+ score = 0.0
186
+ d = node_date(node)
187
+ factor = TIME_BOOST if (d and d in dates) else 1.0
188
+ scored.append((score * factor, idx, item))
189
+ scored.sort(key=lambda t: (-t[0], t[1]))
190
+ out = [t[2] for t in scored]
191
+ return out[:k] if k else out
192
+
193
+
194
+ def scoped_recall(store, query, now, cap=8):
195
+ """按查询提到的日期做**二次召回**(时间算子),返回额外候选(可追加到包里)。
196
+
197
+ 为什么不是「重排」:主库实测(34,218 节点)——查询带日期时 top-5 里同日期占比
198
+ 重排前 5.0%、重排后 8.3%,因为候选集里**根本没有那天的节点**;
199
+ 只有把那天窗口内的节点捞出来,日期才真正起作用。
200
+
201
+ 成本控制:只在查询**解析出日期**时才多跑一次检索(普通查询零开销)。
202
+ 返回 [(node, score, qual, prov)],与 search_rrf 同构,score 为 0.0(只补召回、不抢名次)。
203
+ """
204
+ dates = query_dates(query, now)
205
+ if not dates:
206
+ return []
207
+ day = sorted(dates)[0]
208
+ try:
209
+ start = time.mktime(time.strptime(day + " 00:00:00", "%Y-%m-%d %H:%M:%S"))
210
+ except (ValueError, OverflowError):
211
+ return []
212
+ end = start + 86400
213
+ try:
214
+ res, _meta = store.search_rrf(query, k=cap, start_time=start,
215
+ end_time=end, start_operator="gte",
216
+ end_operator="lt")
217
+ except Exception: # noqa: BLE001 —— 二次召回失败不拖崩主检索
218
+ return []
219
+ out = []
220
+ for item in res or ():
221
+ node = item[0]
222
+ out.append((node, 0.0, {"reason": "time_scope"}, []))
223
+ return out
package/md_cg/tokens.py CHANGED
@@ -58,7 +58,7 @@ ALL_LAYERS = ("anchor", "structural", "knowledge", "contextual", "self",
58
58
  # 认知图 op 全集(与 mcp_server._cg_call 对齐,供 ops_allow 收窄)
59
59
  # help 按需披露入口:工具面投影(见 md_cg/tool_face.py)后,被外置的完整
60
60
  # op/参数语义在此从真源取回;只读元信息,不经角色闸(不属于写/裁决面)
61
- ALL_OPS = ("help", "info", "route", "read", "write", "goal", "task", "recent", "verify",
61
+ ALL_OPS = ("state_view", "help", "info", "route", "read", "write", "goal", "task", "recent", "verify",
62
62
  "review", "forget", "protect", "identity", "consistency",
63
63
  "metacognition", "self_state", "evolution", "sustain", "scrub",
64
64
  "predict", "causal", "whitebox", "index_code", "index_doc", "ref",
@@ -0,0 +1,142 @@
1
+ # -*- coding: utf-8 -*-
2
+ """跨轮状态聚合(读取策略改造 A 项)——**聚合实现单点**。
3
+
4
+ 病灶(Ornn8 2026-11-07 终局报告):「上次聊灯箱进行到哪一步」的答案状态散在轮9 与轮20
5
+ 两条里,检索给了直接相关的一条,**拼不出完整状态**。报告结论:这不是存储失败,
6
+ 是**读取策略**失败——原文全在库里,缺的是「同话题多轮合并给出」的组装方式。
7
+
8
+ 设计边界(三条,都为了不越界):
9
+ 1. **不改排序、不改打分**——纯后处理,输入是已装好的 pack;
10
+ 2. **不自造 state**——state 只从条目既有 metadata 读(issue #111 同一条纪律);
11
+ 3. **不判重、不判对错**——同一话题的多条就是多条,按时间排好交给调用方判断。
12
+ """
13
+ from __future__ import annotations
14
+
15
+ import os
16
+ import re
17
+
18
+ #: 无 tag 命中的分组键(不输出——没有话题名的聚合只是噪声)
19
+ UNTAGGED = "__untagged__"
20
+
21
+ def _tagfallback(pack_blob=None) -> bool:
22
+ """tag 召回兜底开关(**调用期**读环境,缺省关)。
23
+
24
+ 为什么不在 import 期固化:模块级常量一旦固化,进程内再也切不回来,
25
+ 测试与运行时行为会不一致(#111 同源的「隐式状态」问题)。
26
+ """
27
+ return os.environ.get("MDCG_TOPIC_AGG_TAGFALLBACK", "0").strip() == "1"
28
+
29
+
30
+ def _terms(text: str) -> list:
31
+ """查询/正文的分词(与检索面同口径:按空白与中英标点切,长度 ≥2)。"""
32
+ return [t.lower() for t in re.split(r"[\s,,、;;。!?::()()\[\]【】]+", text or "")
33
+ if len(t) >= 2]
34
+
35
+
36
+ #: 泛词判据(**确定性词形规则**,不看语料统计)。
37
+ #: 为什么不看「命中占比」:查询本身就把含该词的条目筛出来了,占比恒 ≈1,
38
+ #: 那样会连「灯箱」都判成泛词(实测:8 话题全被闸掉,兜底等于没开)。
39
+ #: 判据改为:中文词长 < MIN_TERM_LEN 且 不在 STOPWORDS ⇒ 视为泛词。
40
+ MIN_TERM_LEN = 3
41
+
42
+ #: 跨轮闸:组内时间跨度(秒)小于此值 ⇒ 不是「跨轮」,不输出。
43
+ #: 批量灌库(同一秒写入)的节点会集中命中,那不是状态演变。
44
+ MIN_SPAN_SECONDS = 30.0
45
+ STOPWORDS = frozenset(
46
+ "文件 内容 问题 时间 方法 状态 加载 好的 可以 这个 那个 我们 你们 他们 什么 怎么"
47
+ "已经 还是 就是 因为 所以 但是 而且 如果 虽然 然后 现在 之后 之前 一样 一些 这样".split()
48
+ )
49
+
50
+
51
+ def _term_too_general(term: str, pack_blob=None) -> bool:
52
+ """词项是否过泛(确定性词形规则):短词且非停用词表之外的具体词。"""
53
+ t = (term or "").strip()
54
+ if not t:
55
+ return True
56
+ if re.fullmatch(r"[A-Za-z0-9_]+", t):
57
+ return len(t) < MIN_TERM_LEN
58
+ if len(t) >= MIN_TERM_LEN:
59
+ return False
60
+ return t in STOPWORDS
61
+
62
+
63
+ def _entry_topic(entry, query_terms, pack_blob=None) -> str:
64
+ """条目的聚合键:tags 里与 query 有交集的第一个 tag;无交集则 UNTAGGED。"""
65
+ fm = entry.get("frontmatter") or {}
66
+ tags = [str(t) for t in (fm.get("tags") or []) if str(t).strip()]
67
+ for tag in tags:
68
+ tl = tag.lower()
69
+ if any(t in tl or tl in t for t in query_terms):
70
+ return tag
71
+ # tag 召回兜底(MDCG_TOPIC_AGG_TAGFALLBACK=1,缺省关):
72
+ # 实测(bench/e2e/corpus 175 轮)聚合组只在「tag 恰好命中话题」时成立,
73
+ # 无 tag 覆盖的对话几乎全归 UNTAGGED ⇒ 聚合形同未启用。
74
+ # 兜底口径:条目**正文**里出现某个 query 词项,就用该词项当组名——
75
+ # 仍是确定性词面判据(不用相似度),且只在显式开启时生效,以免改变既有行为。
76
+ if _tagfallback(pack_blob):
77
+ text = str(entry.get("content") or "").lower()
78
+ for term in query_terms:
79
+ # 特异性闸:命中该词项的条目占比 > GENERAL_TERM_RATIO 视为泛词,不作组名。
80
+ # 动机(bench 实测):无闸时 8 个话题聚出 77 个成员(≈每话题 9.6 条),
81
+ # 「文件」「内容」这类泛词会把不相关条目也拉进同一组——聚合反而制造噪声。
82
+ if not term or term not in text:
83
+ continue
84
+ if _term_too_general(term, pack_blob):
85
+ continue
86
+ return term
87
+ return UNTAGGED
88
+
89
+
90
+ def aggregate(pack, query, min_members: int = 2, max_groups: int = 5) -> list:
91
+ """把 pack 按话题聚合成状态时间线。
92
+
93
+ 返回 [{topic, count, ids, timeline: [{id, state, t, snippet}]}],按成员数降序。
94
+ 只输出成员数 ≥ min_members 的组(单条没有「跨轮」可言)。
95
+ """
96
+ terms = _terms(query)
97
+ groups = {}
98
+ blob = [str(e.get("content") or "").lower() for e in (pack or ())]
99
+ for entry in pack or ():
100
+ topic = _entry_topic(entry, terms, blob)
101
+ if topic == UNTAGGED:
102
+ continue
103
+ fm = entry.get("frontmatter") or {}
104
+ meta = entry.get("metadata") or {}
105
+ t = fm.get("created_at") or fm.get("valid_from") or 0
106
+ try:
107
+ t = float(t)
108
+ except (TypeError, ValueError):
109
+ t = 0.0
110
+ snippet = str(entry.get("content") or "")
111
+ # 取正文里第一行「# 功能名:…」(有则用,无则首 60 字)。
112
+ # 注意:内容里可能是**字面**的 \n 序列(不是换行),故同时按两种分隔切。
113
+ m = re.search(r"#\s*功能名[::]\s*(.+)", snippet)
114
+ label = m.group(1) if m else snippet
115
+ for sep in ("\\n", "\n", "\r", "\t", " #"):
116
+ label = label.split(sep)[0]
117
+ label = label.strip()[:60]
118
+ groups.setdefault(topic, []).append({
119
+ "id": entry.get("id"),
120
+ "state": meta.get("state"), # 只读既有字段,不自造
121
+ "t": t,
122
+ "label": label,
123
+ })
124
+ out = []
125
+ for topic, members in groups.items():
126
+ if len(members) < min_members:
127
+ continue
128
+ # 跨轮闸:**至少两个不同时间点**才算「跨轮状态」。
129
+ # 动机(bench 实测):兜底开启后单话题一次聚到 10 条(整个 pack 一组),
130
+ # 那是「同一个时间点的一堆材料」,不是「状态在时间上演变」——
131
+ # 对「上次聊到哪一步」这类问题毫无帮助,反而挤占预算。
132
+ if (max(m["t"] for m in members) - min(m["t"] for m in members)) < MIN_SPAN_SECONDS:
133
+ continue
134
+ members.sort(key=lambda x: x["t"]) # 时间正序 = 状态演变顺序
135
+ out.append({
136
+ "topic": topic,
137
+ "count": len(members),
138
+ "ids": [m["id"] for m in members],
139
+ "timeline": members,
140
+ })
141
+ out.sort(key=lambda g: (-g["count"], g["topic"]))
142
+ return out[:max_groups]