@furongjun1999/dsh-memory 0.9.22 → 0.10.1
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 +450 -0
- package/CHANGELOG.md +112 -0
- package/README.en.md +244 -0
- package/README.md +272 -260
- package/codebuddy/CODEBUDDY.md +1 -1
- package/docs/README.md +2 -0
- package/docs/discipline/templates/full.md.tmpl +1 -1
- package/docs/discipline/templates/rules.mdc.tmpl +1 -1
- 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 +16 -15
- package/lib/client.js +29 -3
- package/lib/hooks.d.ts +34 -0
- package/lib/hooks.js +167 -33
- 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/bucketindex.py +231 -0
- package/md_cg/bucketscope.py +339 -0
- package/md_cg/config_registry_bulk.py +19 -10
- package/md_cg/config_validate.py +14 -2
- package/md_cg/detrerank.py +137 -0
- package/md_cg/fsutil.py +215 -25
- package/md_cg/govern/__init__.py +23 -0
- package/md_cg/iface/__init__.py +46 -0
- package/md_cg/iface/_verify.py +609 -0
- package/md_cg/iface/contracts.py +337 -0
- package/md_cg/judgment_manifest.py +14 -0
- package/md_cg/kernel/__init__.py +27 -0
- package/md_cg/mcp_server.py +457 -23
- package/md_cg/mdcg.py +1034 -79
- package/md_cg/mdcos.py +263 -21
- package/md_cg/metacognition.py +66 -13
- package/md_cg/mutate_extract_shipped.py +126 -0
- package/md_cg/mutate_ledger_e2e.py +100 -0
- package/md_cg/nodefile.py +147 -23
- package/md_cg/op_table.py +22 -4
- package/md_cg/probe_c_ledger_integrity.py +258 -0
- package/md_cg/probe_d_retirement_discipline.py +263 -0
- package/md_cg/reconcile.py +9 -11
- package/md_cg/review_cli.py +6 -1
- package/md_cg/rollback.py +16 -1
- package/md_cg/run_tests.py +30 -1
- package/md_cg/scopeselect.py +214 -0
- package/md_cg/scrub.py +160 -23
- package/md_cg/security.py +4 -2
- package/md_cg/state_extract.py +149 -0
- package/md_cg/state_slots.py +58 -10
- package/md_cg/structboost.py +179 -0
- package/md_cg/test_alternatives_historical.py +162 -0
- package/md_cg/test_b2_sensitivity_landing.py +226 -223
- package/md_cg/test_b3_merge_keeps_content.py +608 -583
- package/md_cg/test_bucket_cards.py +85 -0
- package/md_cg/test_bucket_day_order.py +74 -0
- package/md_cg/test_bucket_quota.py +79 -0
- package/md_cg/test_bucket_recency_guards.py +75 -0
- package/md_cg/test_bucket_three_causes.py +83 -0
- package/md_cg/test_bucketindex.py +130 -0
- package/md_cg/test_bucketscope_early.py +71 -0
- package/md_cg/test_cond_term_fold.py +183 -0
- package/md_cg/test_cond_term_fold_gate.py +1210 -0
- package/md_cg/test_cond_term_neg_scope.py +147 -0
- package/md_cg/test_cond_terms_colon.py +348 -0
- package/md_cg/test_detrerank.py +157 -0
- package/md_cg/test_gate_four_kpi.py +136 -0
- package/md_cg/test_generation_guard.py +76 -43
- package/md_cg/test_issue192_move_layer_indices_guard.py +48 -0
- package/md_cg/test_issue362_neg_condition.py +44 -7
- package/md_cg/test_issue39_utf8_stdio.py +572 -572
- package/md_cg/test_issue413_preimage_failclosed.py +147 -9
- 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_issue768_overwrite_time_window_anchor.py +135 -0
- package/md_cg/test_issue789_log_plaintext_probe.py +100 -0
- package/md_cg/test_issue790_verb_negation.py +77 -0
- package/md_cg/test_issue810_root_guard.py +71 -0
- package/md_cg/test_issue812_info_index_stale.py +78 -0
- package/md_cg/test_issue827_weaken_dedup.py +361 -0
- package/md_cg/test_issue829_calibration_positive_gate.py +473 -0
- package/md_cg/test_kernel_iface_contracts.py +18 -0
- package/md_cg/test_md_cg_no_hive_dep.py +238 -17
- package/md_cg/test_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_probe_cd_shipped.py +385 -0
- package/md_cg/test_recent_tail.py +93 -0
- package/md_cg/test_run_tests_entry_gates.py +110 -0
- package/md_cg/test_scopeselect.py +70 -0
- package/md_cg/test_scopeselect_clock.py +70 -0
- package/md_cg/test_scrub_source_line_false_positive.py +64 -0
- package/md_cg/test_session_attribution_consistency.py +835 -813
- package/md_cg/test_state_extract_shipped.py +162 -0
- package/md_cg/test_state_ledger_chain.py +207 -0
- package/md_cg/test_state_ledger_e2e.py +145 -0
- package/md_cg/test_structboost.py +102 -0
- package/md_cg/test_timeboost.py +83 -0
- package/md_cg/test_timeboost_relativedate.py +71 -0
- package/md_cg/test_topicagg.py +129 -0
- package/md_cg/test_twophase.py +286 -286
- package/md_cg/timeboost.py +223 -0
- package/md_cg/tokens.py +55 -7
- package/md_cg/topicagg.py +142 -0
- package/md_cg/twophase.py +15 -6
- package/md_cg/whitebox_kb/wisdom/code_compose.py +33 -5
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +5 -1
- package/md_cg/whitebox_kb/wisdom/role_compose.py +271 -268
- package/md_cg/writepipe.py +35 -3
- package/package.json +4 -1
- package/scripts/workspace_index.py +2 -0
- package/src/hooks.ts +182 -34
- 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/build-ui.mjs +41 -2
- package/ui/minimal/payload/lib/client.js +29 -3
- package/ui/minimal/payload/lib/lib/console_web.mjs +30 -3
- package/ui/minimal/payload/src/lib/console_web.mjs +30 -3
- package/ui/minimal/payload/web/app.js +3 -1
- package/ui/minimal/payload/web/client.js +30 -3
- package/web/app.js +3 -1
- package/web/client.js +30 -3
- package/zcode/AGENTS.md +1 -1
|
@@ -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
|
@@ -580,11 +580,12 @@ def _authorize_issue(role: str, spec: dict, *, path=None,
|
|
|
580
580
|
% (role, len(existing), role))
|
|
581
581
|
|
|
582
582
|
|
|
583
|
-
# 生效条件:role 经 normalize_role+role_spec(未知角色抛 TokenError),clearance 为假值时取 spec["clearance_cap"] 再经 _clamp_level 收窄到该 cap,layers_allow/ops_allow 经 _narrow 与 spec 默认求交,actor 为假值时取 role,delegable is None 时取 spec["delegable"]、否则按所传值取 bool,ttl 为真值时 expires_at=now+float(ttl)、为假值(None/0)时 None;**先过 _authorize_issue() 授权闸**(issue #543:管理级角色在**凭据库非空**时须由既有管理级凭据委派或显式开关 MDCG_ALLOW_ROOT_ISSUE=1;空库首启允许自举),随后在 _store_lock(path) 临界区内把记录写入 token_file(path)(N199:读-改-写无锁会被并发写者用陈旧快照整份覆盖)并返回含明文 token
|
|
583
|
+
# 生效条件:role 经 normalize_role+role_spec(未知角色抛 TokenError),clearance 为假值时取 spec["clearance_cap"] 再经 _clamp_level 收窄到该 cap,layers_allow/ops_allow 经 _narrow 与 spec 默认求交,actor 为假值时取 role,delegable is None 时取 spec["delegable"]、否则按所传值取 bool,ttl 为真值时 expires_at=now+float(ttl)、为假值(None/0)时 None;**先过 _authorize_issue() 授权闸**(issue #543:管理级角色在**凭据库非空**时须由既有管理级凭据委派或显式开关 MDCG_ALLOW_ROOT_ISSUE=1;空库首启允许自举),随后在 _store_lock(path) 临界区内把记录写入 token_file(path)(N199:读-改-写无锁会被并发写者用陈旧快照整份覆盖)并返回含明文 token 的字典;sessions_read(issue #712 签发侧接线)为 None 时 rec 落盘 None(不限读面,旧行为零变),否则 str 化 + dict.fromkeys 保序去重后以 list 落盘,返回值同步携带。
|
|
584
584
|
def issue(role: str, actor: str = None, clearance: str = None,
|
|
585
585
|
tenant: str = "default", ttl: float = None, label: str = "",
|
|
586
586
|
issued_by: str = "root", parent: str = None, delegable: bool = None,
|
|
587
587
|
layers_allow=None, ops_allow=None, path: str = None,
|
|
588
|
+
sessions_read=None,
|
|
588
589
|
_authorized_by=None, _allow_bootstrap=None):
|
|
589
590
|
"""签发一枚令牌。明文 token 只在返回值里出现一次,不落盘。
|
|
590
591
|
|
|
@@ -642,6 +643,9 @@ def issue(role: str, actor: str = None, clearance: str = None,
|
|
|
642
643
|
"can_write": bool(spec["can_write"]),
|
|
643
644
|
"can_admin": bool(spec["can_admin"]),
|
|
644
645
|
"layers_allow": layers, "ops_allow": ops,
|
|
646
|
+
# issue #712:读面随令牌落盘(None=不限,旧行为);归一化口径与 Principal 同构。
|
|
647
|
+
"sessions_read": (None if sessions_read is None
|
|
648
|
+
else list(dict.fromkeys(str(x) for x in sessions_read))),
|
|
645
649
|
"delegable": bool(spec["delegable"] if delegable is None else delegable),
|
|
646
650
|
"parent": parent, "issued_by": issued_by, "issued_at": now,
|
|
647
651
|
"expires_at": (now + float(ttl)) if ttl else None,
|
|
@@ -657,11 +661,12 @@ def issue(role: str, actor: str = None, clearance: str = None,
|
|
|
657
661
|
return {"ok": True, "token": make_token(role, token_id, secret),
|
|
658
662
|
"token_id": token_id, "role": role, "actor": rec["actor"],
|
|
659
663
|
"clearance": clearance, "layers_allow": layers, "ops_allow": ops,
|
|
664
|
+
"sessions_read": rec["sessions_read"],
|
|
660
665
|
"expires_at": rec["expires_at"], "token_file": token_file(path),
|
|
661
666
|
"warnings": warnings}
|
|
662
667
|
|
|
663
668
|
|
|
664
|
-
# 生效条件:token 先经 parse_token(格式非法即抛 TokenError),其后 _load(path) 的 tokens 中该 token_id 无记录、rec 的 role 与解析出的 role 不等、revoked_at 为真、hash 与 _hash(secret) 经 hmac.compare_digest 不等、expires_at 为真且小于当前时间、或沿 parent 链上溯(seen 集合防环;链上父记录缺失时仅向 stderr 告警不阻断——吊销已由 revoke 级联物化,此处只补过期维度,文件写权不在令牌威胁模型内)任一祖先 expires_at 为真且小于当前时间中任一成立即抛 TokenError;否则返回 Principal,tenant=tenant or rec.get("tenant") or "default"、actor=rec.get("actor") or role、clearance=rec.get("clearance") or "internal"、can_write/can_admin 取对应 rec 值的 bool;返回前 rec["tenant"] 与非空形参 tenant 去空白后不等时先向 stderr 写「租户绑定被入参覆盖」告警;rec["tenant"] 与非空形参 tenant 去空白后不等时抛 TokenError 拒绝(N62②,2026-09-27 第 5 轮:告警改 fail-closed——原告警文案已定性「若非有意迁移请校正 MDCG_TENANT」),clearance 先取 rec 值再按登记租户上限夹紧(N62①:MDCG_TENANT_REGISTRY 登记表(与 _resolve_root 同源)在册租户 _rank(clearance) 高于其 clearance_cap 时夹紧为 cap
|
|
669
|
+
# 生效条件:token 先经 parse_token(格式非法即抛 TokenError),其后 _load(path) 的 tokens 中该 token_id 无记录、rec 的 role 与解析出的 role 不等、revoked_at 为真、hash 与 _hash(secret) 经 hmac.compare_digest 不等、expires_at 为真且小于当前时间、或沿 parent 链上溯(seen 集合防环;链上父记录缺失时仅向 stderr 告警不阻断——吊销已由 revoke 级联物化,此处只补过期维度,文件写权不在令牌威胁模型内)任一祖先 expires_at 为真且小于当前时间中任一成立即抛 TokenError;否则返回 Principal,tenant=tenant or rec.get("tenant") or "default"、actor=rec.get("actor") or role、clearance=rec.get("clearance") or "internal"、can_write/can_admin 取对应 rec 值的 bool;返回前 rec["tenant"] 与非空形参 tenant 去空白后不等时先向 stderr 写「租户绑定被入参覆盖」告警;rec["tenant"] 与非空形参 tenant 去空白后不等时抛 TokenError 拒绝(N62②,2026-09-27 第 5 轮:告警改 fail-closed——原告警文案已定性「若非有意迁移请校正 MDCG_TENANT」),clearance 先取 rec 值再按登记租户上限夹紧(N62①:MDCG_TENANT_REGISTRY 登记表(与 _resolve_root 同源)在册租户 _rank(clearance) 高于其 clearance_cap 时夹紧为 cap;未登记租户/登记表缺失或损坏零闸变);重建 Principal 另传 sessions_read=rec.get("sessions_read")(issue #712 签发侧接线:存量 rec 无此键 → None → 不限读面,行为零变)。
|
|
665
670
|
def verify_token(token: str, tenant: str = None, path: str = None) -> Principal:
|
|
666
671
|
"""校验令牌 → Principal。任何异常都抛 TokenError(fail-closed)。"""
|
|
667
672
|
role, token_id, secret = parse_token(token)
|
|
@@ -739,13 +744,15 @@ def verify_token(token: str, tenant: str = None, path: str = None) -> Principal:
|
|
|
739
744
|
can_admin=bool(rec.get("can_admin")),
|
|
740
745
|
role=role, token_id=token_id, parent=rec.get("parent"),
|
|
741
746
|
expires_at=exp, layers_allow=rec.get("layers_allow"),
|
|
742
|
-
ops_allow=rec.get("ops_allow"), auth_mode="token"
|
|
747
|
+
ops_allow=rec.get("ops_allow"), auth_mode="token",
|
|
748
|
+
# issue #712:旧库 rec 无此键 → None = 不限读面(行为零变,同 layers 兼容口径)
|
|
749
|
+
sessions_read=rec.get("sessions_read"))
|
|
743
750
|
|
|
744
751
|
|
|
745
|
-
# 生效条件:parent_token 经 verify_token(parent_token, path=path) 成功,随后在 _store_lock(path) 临界区内复核父令牌**落盘态**:父记录 delegable 为假抛 TokenError,父记录已并发吊销(revoked_at 为真,N199)同样抛 TokenError;role 经 normalize_role+role_spec,clearance 为假值时取 spec["clearance_cap"] 再 _clamp_level,若仍高于 parent.clearance 则降为 parent.clearance 并向 clamped 追加 "clearance";layers/ops 先与 spec 求交再与父记录求交;can_write/can_admin 取 spec 与父对应值的与;ttl 为真值时过期候选=now+float(ttl)、为假值(None/0)时沿用父记录 expires_at,候选与父记录 expires_at 均非空时取 min(N123:派生在时间维度同样只能收窄,父无界时取候选),被父夹紧时向 clamped 追加 "expires_at";新记录 delegable 恒 False、parent/issued_by 为 parent.token_id,_save 后返回含明文 token 与 clamped
|
|
752
|
+
# 生效条件:parent_token 经 verify_token(parent_token, path=path) 成功,随后在 _store_lock(path) 临界区内复核父令牌**落盘态**:父记录 delegable 为假抛 TokenError,父记录已并发吊销(revoked_at 为真,N199)同样抛 TokenError;role 经 normalize_role+role_spec,clearance 为假值时取 spec["clearance_cap"] 再 _clamp_level,若仍高于 parent.clearance 则降为 parent.clearance 并向 clamped 追加 "clearance";layers/ops 先与 spec 求交再与父记录求交;can_write/can_admin 取 spec 与父对应值的与;ttl 为真值时过期候选=now+float(ttl)、为假值(None/0)时沿用父记录 expires_at,候选与父记录 expires_at 均非空时取 min(N123:派生在时间维度同样只能收窄,父无界时取候选),被父夹紧时向 clamped 追加 "expires_at";新记录 delegable 恒 False、parent/issued_by 为 parent.token_id,_save 后返回含明文 token 与 clamped 的字典;sessions_read(issue #712)按「派生只能收窄」同构夹紧:子未指定=继承父界;父无界(None 或含 '*')=子自设界生效;子请求含 '*' 而父有界=夹紧为父界并记 clamped;否则取交集(保持父界序),交集为空抛 TokenError(fail-closed,宁可拒签不发什么都读不到的凭据),部分落入记 clamped;新记录与返回均含该字段。
|
|
746
753
|
def derive(parent_token: str, role: str, actor: str = None, ttl: float = None,
|
|
747
754
|
label: str = "", path: str = None, clearance: str = None,
|
|
748
|
-
layers_allow=None, ops_allow=None):
|
|
755
|
+
layers_allow=None, ops_allow=None, sessions_read=None):
|
|
749
756
|
"""设计者令牌派生受限子令牌:权限只能收窄,子令牌默认不可再派生。"""
|
|
750
757
|
parent = verify_token(parent_token, path=path)
|
|
751
758
|
# N199:本函数的「读-改-写」必须与父令牌的**落盘态复核**同处一个临界区
|
|
@@ -774,6 +781,27 @@ def derive(parent_token: str, role: str, actor: str = None, ttl: float = None,
|
|
|
774
781
|
prec.get("layers_allow"))
|
|
775
782
|
ops = _narrow(_narrow(ops_allow, spec["ops_allow"]),
|
|
776
783
|
prec.get("ops_allow"))
|
|
784
|
+
# issue #712(签发侧接线):读面「派生只能收窄」,与 N123 过期夹紧同构。
|
|
785
|
+
p_sr = prec.get("sessions_read")
|
|
786
|
+
want_sr = (None if sessions_read is None
|
|
787
|
+
else list(dict.fromkeys(str(x) for x in sessions_read)))
|
|
788
|
+
p_all = (p_sr is None) or ("*" in [str(x) for x in p_sr])
|
|
789
|
+
if want_sr is None:
|
|
790
|
+
final_sr = p_sr # 未指定 → 继承父界(不放宽)
|
|
791
|
+
elif p_all:
|
|
792
|
+
final_sr = want_sr # 父无界 → 子自设任意界
|
|
793
|
+
elif "*" in [str(x) for x in want_sr]:
|
|
794
|
+
final_sr = p_sr # 子请求「全部」而父有界 → 夹紧为父界
|
|
795
|
+
clamped.append("sessions_read")
|
|
796
|
+
else:
|
|
797
|
+
_inter = [x for x in p_sr if x in {str(y) for y in want_sr}]
|
|
798
|
+
if not _inter:
|
|
799
|
+
raise TokenError(
|
|
800
|
+
"派生拒绝:sessions_read 与父令牌读面交集为空(fail-closed——"
|
|
801
|
+
"宁可拒签,不签发一枚什么都读不到的凭据)")
|
|
802
|
+
if set(_inter) != {str(y) for y in want_sr}:
|
|
803
|
+
clamped.append("sessions_read")
|
|
804
|
+
final_sr = _inter
|
|
777
805
|
can_write = bool(spec["can_write"]) and bool(parent.can_write)
|
|
778
806
|
can_admin = bool(spec["can_admin"]) and bool(parent.can_admin)
|
|
779
807
|
# N123(2026-09-25 止血):「派生只能收窄」补上时间维度——子令牌有效期
|
|
@@ -795,6 +823,7 @@ def derive(parent_token: str, role: str, actor: str = None, ttl: float = None,
|
|
|
795
823
|
"clearance": final_clear, "can_write": can_write,
|
|
796
824
|
"can_admin": can_admin,
|
|
797
825
|
"layers_allow": layers, "ops_allow": ops,
|
|
826
|
+
"sessions_read": final_sr,
|
|
798
827
|
"delegable": False, "parent": parent.token_id,
|
|
799
828
|
"issued_by": parent.token_id, "issued_at": now,
|
|
800
829
|
"expires_at": final_exp,
|
|
@@ -805,12 +834,20 @@ def derive(parent_token: str, role: str, actor: str = None, ttl: float = None,
|
|
|
805
834
|
return {"ok": True, "token": make_token(role, token_id, secret),
|
|
806
835
|
"token_id": token_id, "role": role, "actor": rec["actor"],
|
|
807
836
|
"clearance": final_clear, "layers_allow": layers, "ops_allow": ops,
|
|
837
|
+
"sessions_read": final_sr,
|
|
808
838
|
"can_write": can_write, "can_admin": can_admin,
|
|
809
839
|
"parent": parent.token_id, "clamped": clamped,
|
|
810
840
|
"expires_at": rec["expires_at"]}
|
|
811
841
|
|
|
812
842
|
|
|
813
|
-
# 生效条件:normalize_role(unit) 结果不在 POSITION_ROLES 时抛 TokenError;否则返回 Principal:unit/role=u、can_admin 恒 False、clearance=_clamp_level(spec["clearance_cap"], p.clearance)、can_write=bool(spec["can_write"]) and bool(p.can_write)、layers_allow/ops_allow=_narrow(spec 对应值, p 对应值),tenant/actor/session/harness/token_id/parent/expires_at/auth_mode/theory 等沿用 p,session_auto 来源标记同款传导(getattr(p, "session_auto", False)
|
|
843
|
+
# 生效条件:normalize_role(unit) 结果不在 POSITION_ROLES 时抛 TokenError;否则返回 Principal:unit/role=u、can_admin 恒 False、clearance=_clamp_level(spec["clearance_cap"], p.clearance)、can_write=bool(spec["can_write"]) and bool(p.can_write)、layers_allow/ops_allow=_narrow(spec 对应值, p 对应值),tenant/actor/session/harness/token_id/parent/expires_at/auth_mode/theory 等沿用 p,session_auto 来源标记同款传导(getattr(p, "session_auto", False));sessions_read 原样传导(issue #712 附带 bug fix:此前构造 Principal 漏传,owner 带读面限制时被收窄静默放宽为不限——fail-open,与「只能变小」矛盾)。
|
|
844
|
+
#: issue #730(D):**收窄型字段**单点清单——`narrowed_principal` 必须逐项处理;
|
|
845
|
+
#: 新增任一收窄维度时**同步此表与函数体**(守卫 test_issue730_narrowing_audit 钉住)。
|
|
846
|
+
#: 判据:「交给请求方自选后,伪造的最坏结果是否仍 ≤ owner 权限」——是即收窄型。
|
|
847
|
+
NARROWING_FIELDS = ("clearance", "can_write", "can_admin", "layers_allow",
|
|
848
|
+
"ops_allow", "sessions_read")
|
|
849
|
+
|
|
850
|
+
|
|
814
851
|
def narrowed_principal(p: Principal, unit: str) -> Principal:
|
|
815
852
|
"""按「单元」收窄 principal 权限(**请求级**身份,只能变小不能变大)。
|
|
816
853
|
|
|
@@ -844,6 +881,9 @@ def narrowed_principal(p: Principal, unit: str) -> Principal:
|
|
|
844
881
|
ops_allow=_narrow(spec["ops_allow"], p.ops_allow),
|
|
845
882
|
token_id=p.token_id, parent=p.parent, expires_at=p.expires_at,
|
|
846
883
|
auth_mode=p.auth_mode,
|
|
884
|
+
# issue #712 附带 bug fix:读面限制随身份传导——此前未传,owner 带限制时
|
|
885
|
+
# as_unit 请求级收窄静默产出 sessions_read=None(不限)的 principal,fail-open。
|
|
886
|
+
sessions_read=p.sessions_read,
|
|
847
887
|
theory_ok=p.theory_ok, theory_version=p.theory_version)
|
|
848
888
|
# 2026-10-07 三态:session 值被**复制**(非空)不等于「声明来源」——来源
|
|
849
889
|
# 标记必须原样传导,否则写归因三态收口(MdCGSecure._attributed_session)
|
|
@@ -889,6 +929,7 @@ def list_tokens(path: str = None, include_revoked: bool = False):
|
|
|
889
929
|
"can_admin": r.get("can_admin"),
|
|
890
930
|
"layers_allow": r.get("layers_allow"),
|
|
891
931
|
"ops_allow": r.get("ops_allow"),
|
|
932
|
+
"sessions_read": r.get("sessions_read"),
|
|
892
933
|
"delegable": r.get("delegable"), "parent": r.get("parent"),
|
|
893
934
|
"label": r.get("label"), "issued_at": r.get("issued_at"),
|
|
894
935
|
"expires_at": r.get("expires_at"),
|
|
@@ -949,6 +990,9 @@ def main(argv=None):
|
|
|
949
990
|
"见 `roles` 子命令)")
|
|
950
991
|
p_i.add_argument("--layers-allow", dest="layers_allow", default=None,
|
|
951
992
|
help="收窄可写层白名单(逗号分隔;越界项被忽略)")
|
|
993
|
+
p_i.add_argument("--sessions-read", dest="sessions_read", default=None,
|
|
994
|
+
help="收窄会话读面(逗号分隔 session id;'*'=全部;缺省=不限,"
|
|
995
|
+
"issue #712)")
|
|
952
996
|
|
|
953
997
|
p_d = sub.add_parser("derive", help="设计者令牌派生子令牌(权限只能收窄)")
|
|
954
998
|
p_d.add_argument("--token", default=None, help="父令牌明文")
|
|
@@ -963,6 +1007,8 @@ def main(argv=None):
|
|
|
963
1007
|
help="收窄可写层白名单(逗号分隔;越界项被忽略)")
|
|
964
1008
|
p_d.add_argument("--clearance", default=None,
|
|
965
1009
|
help="收窄密级(默认取角色上限,且不超过父令牌)")
|
|
1010
|
+
p_d.add_argument("--sessions-read", dest="sessions_read", default=None,
|
|
1011
|
+
help="收窄会话读面(逗号分隔;与父读面求交,只能收窄,issue #712)")
|
|
966
1012
|
|
|
967
1013
|
p_o = sub.add_parser(
|
|
968
1014
|
"orch",
|
|
@@ -992,6 +1038,7 @@ def main(argv=None):
|
|
|
992
1038
|
tenant=a.tenant, ttl=a.ttl, label=a.label,
|
|
993
1039
|
layers_allow=_csv_list(a.layers_allow),
|
|
994
1040
|
ops_allow=_csv_list(a.ops_allow),
|
|
1041
|
+
sessions_read=_csv_list(a.sessions_read),
|
|
995
1042
|
path=a.token_file)
|
|
996
1043
|
# 覆盖面/高风险告警在**接口层**打印(库函数只返回 warnings,见 issue 注释):
|
|
997
1044
|
# stdout 保持纯 JSON(协议面),告警走 stderr(诊断面),两流不混。
|
|
@@ -1007,7 +1054,8 @@ def main(argv=None):
|
|
|
1007
1054
|
label=a.label, path=a.token_file,
|
|
1008
1055
|
clearance=a.clearance,
|
|
1009
1056
|
layers_allow=_csv_list(a.layers_allow),
|
|
1010
|
-
ops_allow=_csv_list(a.ops_allow)
|
|
1057
|
+
ops_allow=_csv_list(a.ops_allow),
|
|
1058
|
+
sessions_read=_csv_list(a.sessions_read)))
|
|
1011
1059
|
elif a.cmd == "orch":
|
|
1012
1060
|
tok = a.token
|
|
1013
1061
|
if a.token_file_in:
|
|
@@ -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]
|
package/md_cg/twophase.py
CHANGED
|
@@ -66,10 +66,11 @@ def log_path(cg) -> str:
|
|
|
66
66
|
|
|
67
67
|
# 生效条件:text 为假值(None/"")时按空串处理,否则用 text 本身,返回去尾换行后的字符串(仅剥 "\n")。
|
|
68
68
|
def _norm(text: str) -> str:
|
|
69
|
-
"""
|
|
69
|
+
"""**仅 legacy 兼容口径**(issue #759 起主路径不再用它):`nodefile.dumps` 写盘时会给正文补尾
|
|
70
70
|
换行,`loads` 读回即带它。若 intent 按入参原文、对账按读回内容各算一次指纹,
|
|
71
71
|
两者**永远不会相等** → 每一笔都误报 interrupted——比不做对账更坏(假阳性会
|
|
72
|
-
淹没真问题,且诱人去"修"
|
|
72
|
+
淹没真问题,且诱人去"修"不存在的问题)。(**#759 起主路径不再用它**:两侧都直接把原文交给 content_hash,由其 normalize_body 定口径;
|
|
73
|
+
本函数只剩 legacy 分支——对升级前的旧 intent 指纹一次性放行。)
|
|
73
74
|
"""
|
|
74
75
|
return (text or "").rstrip("\n")
|
|
75
76
|
|
|
@@ -108,7 +109,7 @@ def begin(cg, node_id: str, content: str, layer: str = None,
|
|
|
108
109
|
tok = {
|
|
109
110
|
"iid": uuid.uuid4().hex[:16],
|
|
110
111
|
"node_id": node_id,
|
|
111
|
-
"content_hash": nodefile.content_hash(
|
|
112
|
+
"content_hash": nodefile.content_hash(content or ""), # issue #759:与索引条目同源(传原文,不再先剥尾)
|
|
112
113
|
}
|
|
113
114
|
rec = {"t": time.time(), "phase": "intent", "iid": tok["iid"],
|
|
114
115
|
"id": node_id, "content_hash": tok["content_hash"]}
|
|
@@ -271,9 +272,17 @@ def _judge(cg, intent: dict):
|
|
|
271
272
|
if tgt:
|
|
272
273
|
return STATUS_COMMITTED, "%s:%s" % (R_MERGED_INTO, tgt)
|
|
273
274
|
return STATUS_INTERRUPTED, R_NODE_MISSING
|
|
274
|
-
got = nodefile.content_hash(
|
|
275
|
+
got = nodefile.content_hash(node.get("content") or "") # issue #759:与 begin/索引同源
|
|
275
276
|
if want and got != want:
|
|
276
|
-
|
|
277
|
+
# issue #741 升级兼容:content_hash 的输入基准已收口为「落盘形态」(恒补
|
|
278
|
+
# 尾换行),而升级前写入的旧版 intent 指纹是「去尾换行后直接哈希」——两者
|
|
279
|
+
# 对同一正文差一个尾换行字节。此处对**旧 intent** 一次性放行,避免把已落盘
|
|
280
|
+
# 的旧单误报成 interrupted(假阳性会污染账本);新 intent 仍是单口径。
|
|
281
|
+
# 复核:**必须复用同一函数**——手写 sha256 少一次 normalize_body(补尾换行),
|
|
282
|
+
# 与旧 begin 的 content_hash(_norm(x)) 差一个字节 ⇒ 恒不命中(等于死代码)。
|
|
283
|
+
_legacy = nodefile.content_hash(_norm(node.get("content") or ""))
|
|
284
|
+
if want != _legacy:
|
|
285
|
+
return STATUS_INTERRUPTED, R_MISMATCH
|
|
277
286
|
return STATUS_COMMITTED, R_NODE_OK
|
|
278
287
|
|
|
279
288
|
|
|
@@ -301,4 +310,4 @@ def reconcile(cg, apply: bool = True, limit: int = 2000) -> dict:
|
|
|
301
310
|
"content_hash": intent.get("content_hash")},
|
|
302
311
|
status=status, reason=code, reconciled=True,
|
|
303
312
|
actor="twophase:reconcile")
|
|
304
|
-
return rep
|
|
313
|
+
return rep
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
⑤ 固化:验证通过代码 → 固化 JSON → 同任务直出
|
|
10
10
|
零 LLM:全部白箱确定性生成 + 自校验(语法/测试是物理基底裁决)。
|
|
11
11
|
"""
|
|
12
|
-
import sys, json, os, ast
|
|
12
|
+
import sys, json, os, ast, tempfile
|
|
13
13
|
sys.stdout.reconfigure(encoding='utf-8')
|
|
14
14
|
|
|
15
15
|
|
|
@@ -596,8 +596,7 @@ def domain_solidify(question, domain=None, uid=None):
|
|
|
596
596
|
"source": "domain_solidified", "domain": domain}
|
|
597
597
|
CODE_SOLIDIFIED[key] = entry
|
|
598
598
|
try:
|
|
599
|
-
|
|
600
|
-
ensure_ascii=False, indent=1)
|
|
599
|
+
_save_solidified()
|
|
601
600
|
except Exception:
|
|
602
601
|
pass
|
|
603
602
|
return entry
|
|
@@ -802,6 +801,36 @@ if os.path.exists(_SOL_FILE):
|
|
|
802
801
|
CODE_SOLIDIFIED = {}
|
|
803
802
|
|
|
804
803
|
|
|
804
|
+
def _save_solidified():
|
|
805
|
+
"""把 `CODE_SOLIDIFIED` 落盘——**必须以 LF 写**(`newline="\\n"`)。
|
|
806
|
+
|
|
807
|
+
`code_solidified.json` 是**入库文件**,仓内它的规范换行是 LF(`\n`)。
|
|
808
|
+
文本模式 `open(..., "w")` 不声明 `newline=` 时,Windows 上 `json.dump`
|
|
809
|
+
会把每个 `\n` 写成 `\r\n` ⇒ 跑一次相关测试,`git status` 就多一条
|
|
810
|
+
「12390 行全改」的噪声(实测:6195 行 LF 全变 CRLF,JSON 语义不变)。
|
|
811
|
+
这种改动既无意义又会被误提交,故在写侧一次性钉死换行。
|
|
812
|
+
|
|
813
|
+
写入用 `atomic_write` 语义的临时文件 + `os.replace`,避免半写坏档:
|
|
814
|
+
固化表是 `code_route` 的直出面,读侧遇到坏 JSON 会静默清空(见上面
|
|
815
|
+
`except: CODE_SOLIDIFIED = {}`),那会把已固化的条目整批丢掉。
|
|
816
|
+
"""
|
|
817
|
+
d = os.path.dirname(_SOL_FILE)
|
|
818
|
+
fd, tmp = tempfile.mkstemp(prefix=".code_solidified.", suffix=".tmp", dir=d)
|
|
819
|
+
try:
|
|
820
|
+
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as fh:
|
|
821
|
+
json.dump(CODE_SOLIDIFIED, fh, ensure_ascii=False, indent=1)
|
|
822
|
+
# 不补尾部换行:入库基线 `code_solidified.json` 末尾**无换行**
|
|
823
|
+
# (`git show` 上是 "")。补一行会让每次
|
|
824
|
+
# 落盘都产生一条 1 行的 diff,等于把 CRLF 噪声换成另一种噪声。
|
|
825
|
+
os.replace(tmp, _SOL_FILE)
|
|
826
|
+
except Exception:
|
|
827
|
+
try:
|
|
828
|
+
os.unlink(tmp)
|
|
829
|
+
except OSError:
|
|
830
|
+
pass
|
|
831
|
+
raise
|
|
832
|
+
|
|
833
|
+
|
|
805
834
|
def code_solidify(question, task=None, uid=None, fn=None):
|
|
806
835
|
"""固化:组合生成 + 自校验通过 → 固化(自举纪律:未验证代码不固化)"""
|
|
807
836
|
lang = detect_language(question)
|
|
@@ -816,8 +845,7 @@ def code_solidify(question, task=None, uid=None, fn=None):
|
|
|
816
845
|
"source": "code_solidified", "lang": lang}
|
|
817
846
|
CODE_SOLIDIFIED[key] = entry
|
|
818
847
|
try:
|
|
819
|
-
|
|
820
|
-
ensure_ascii=False, indent=1)
|
|
848
|
+
_save_solidified()
|
|
821
849
|
except Exception:
|
|
822
850
|
pass
|
|
823
851
|
return entry
|