@furongjun1999/dsh-memory 0.9.23 → 0.10.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.en.md +515 -404
- package/CHANGELOG.md +642 -449
- package/README.en.md +244 -240
- package/README.md +272 -262
- package/codebuddy/CODEBUDDY.md +1 -1
- package/docs/discipline/templates/full.md.tmpl +1 -1
- package/docs/discipline/templates/rules.mdc.tmpl +1 -1
- package/docs/en/mdcg/world-model-endpoint.md +1 -1
- package/docs/mdcg/cogmap_ops.md +19 -16
- package/lib/client.js +29 -3
- package/lib/hooks.d.ts +12 -0
- package/lib/hooks.js +43 -1
- package/lib/tools.d.ts +2 -2
- package/md_cg/bucketindex.py +231 -0
- package/md_cg/bucketscope.py +357 -0
- package/md_cg/cohesion.py +337 -0
- package/md_cg/config_registry_bulk.py +24 -10
- package/md_cg/config_validate.py +14 -2
- package/md_cg/constraint_audit.py +231 -0
- package/md_cg/constraint_watch.py +206 -0
- package/md_cg/context_audit.py +240 -0
- package/md_cg/context_plan.py +142 -0
- package/md_cg/detrerank.py +137 -0
- package/md_cg/distill.py +247 -0
- package/md_cg/dossier.py +151 -0
- package/md_cg/entities.py +133 -0
- package/md_cg/fsutil.py +135 -0
- package/md_cg/govern/__init__.py +23 -0
- package/md_cg/iface/__init__.py +46 -0
- package/md_cg/iface/_verify.py +609 -0
- package/md_cg/iface/contracts.py +337 -0
- package/md_cg/judgment_manifest.py +14 -0
- package/md_cg/kernel/__init__.py +27 -0
- package/md_cg/mcp_server.py +6044 -5761
- package/md_cg/mdcg.py +501 -16
- package/md_cg/mdcos.py +546 -12
- package/md_cg/metacognition.py +66 -13
- package/md_cg/mutate_extract_shipped.py +126 -0
- package/md_cg/mutate_ledger_e2e.py +100 -0
- package/md_cg/op_table.py +22 -0
- package/md_cg/probe_c_ledger_integrity.py +258 -0
- package/md_cg/probe_d_retirement_discipline.py +263 -0
- package/md_cg/profiles.py +298 -0
- package/md_cg/protocol.py +7 -1
- package/md_cg/reldate.py +142 -0
- package/md_cg/review_cli.py +6 -1
- package/md_cg/run_tests.py +30 -1
- package/md_cg/scopeselect.py +214 -0
- package/md_cg/scrub.py +66 -9
- package/md_cg/selfstmt.py +139 -0
- package/md_cg/state_extract.py +149 -0
- package/md_cg/state_slots.py +62 -10
- package/md_cg/state_view.py +181 -0
- package/md_cg/structboost.py +179 -0
- package/md_cg/sustain.py +28 -2
- package/md_cg/test_alternatives_historical.py +165 -0
- package/md_cg/test_b2_sensitivity_landing.py +226 -223
- package/md_cg/test_b3_merge_keeps_content.py +608 -583
- package/md_cg/test_bucket_cards.py +85 -0
- package/md_cg/test_bucket_day_order.py +74 -0
- package/md_cg/test_bucket_quota.py +79 -0
- package/md_cg/test_bucket_recency_guards.py +75 -0
- package/md_cg/test_bucket_three_causes.py +83 -0
- package/md_cg/test_bucket_time_rounds.py +87 -0
- package/md_cg/test_bucketindex.py +130 -0
- package/md_cg/test_bucketscope_early.py +71 -0
- package/md_cg/test_cohesion.py +107 -0
- package/md_cg/test_cond_term_fold_gate.py +1210 -0
- package/md_cg/test_cond_term_neg_scope.py +147 -0
- package/md_cg/test_constraint_audit.py +87 -0
- package/md_cg/test_constraint_watch.py +108 -0
- package/md_cg/test_context_audit.py +110 -0
- package/md_cg/test_context_plan.py +81 -0
- package/md_cg/test_detrerank.py +157 -0
- package/md_cg/test_gate_four_kpi.py +136 -0
- package/md_cg/test_generation_guard.py +14 -2
- package/md_cg/test_issue192_move_layer_indices_guard.py +48 -0
- package/md_cg/test_issue39_utf8_stdio.py +572 -572
- package/md_cg/test_issue413_preimage_failclosed.py +147 -9
- package/md_cg/test_issue768_overwrite_time_window_anchor.py +135 -0
- package/md_cg/test_issue789_log_plaintext_probe.py +100 -0
- package/md_cg/test_issue790_verb_negation.py +77 -0
- package/md_cg/test_issue810_root_guard.py +71 -0
- package/md_cg/test_issue812_info_index_stale.py +78 -0
- package/md_cg/test_issue827_weaken_dedup.py +361 -0
- package/md_cg/test_issue829_calibration_positive_gate.py +473 -0
- package/md_cg/test_kernel_iface_contracts.py +18 -0
- package/md_cg/test_md_cg_no_hive_dep.py +238 -17
- package/md_cg/test_probe_cd_shipped.py +385 -0
- package/md_cg/test_recent_tail.py +93 -0
- package/md_cg/test_run_tests_entry_gates.py +110 -0
- package/md_cg/test_scopeselect.py +70 -0
- package/md_cg/test_scopeselect_clock.py +70 -0
- package/md_cg/test_session_attribution_consistency.py +835 -813
- package/md_cg/test_state_extract_shipped.py +162 -0
- package/md_cg/test_state_ledger_chain.py +207 -0
- package/md_cg/test_state_ledger_e2e.py +145 -0
- package/md_cg/test_structboost.py +102 -0
- package/md_cg/test_timeboost.py +83 -0
- package/md_cg/test_timeboost_relativedate.py +71 -0
- package/md_cg/test_topicagg.py +129 -0
- package/md_cg/timeboost.py +223 -0
- package/md_cg/tokens.py +1 -1
- package/md_cg/topicagg.py +142 -0
- package/md_cg/version_anchor.py +84 -1
- package/md_cg/whitebox_kb/wisdom/code_compose.py +33 -5
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +5 -1
- package/md_cg/whitebox_kb/wisdom/role_compose.py +271 -268
- package/package.json +1 -1
- package/src/hooks.ts +43 -1
- package/ui/minimal/payload/build-ui.mjs +41 -2
- package/ui/minimal/payload/lib/client.js +29 -3
- package/ui/minimal/payload/web/app.js +3 -1
- package/ui/minimal/payload/web/client.js +30 -3
- package/web/app.js +3 -1
- package/web/client.js +30 -3
- package/zcode/AGENTS.md +1 -1
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""范围选择(scopeselect)——灵枢的**状态追踪式范围锚定**。
|
|
3
|
+
|
|
4
|
+
bench 读数(rubric92 · 552 断言 · 纯确定性):
|
|
5
|
+
· 槽位共现推断时间桶:**94.20% top-1**,且**加不加章节号都是 94.20%**(对字面零依赖);
|
|
6
|
+
· 对照:词面 bigram 排序 90.76%(消融)/ 87.14%(原 query);
|
|
7
|
+
· 邻近加权(行窗 12/24/40)反而更差(92.0–93.8)⇒ **桶级共现就是最优粒度**。
|
|
8
|
+
|
|
9
|
+
结论(为什么它该做标准):
|
|
10
|
+
词面检索问的是「哪段更像这句话」;假题的错元素本身位于别处,词面必然被带走(实测 28/28)。
|
|
11
|
+
共现问的是「这些槽位值是否在同一时间桶里同时出现过」——错元素不在真桶里投票,真假自然分开。
|
|
12
|
+
|
|
13
|
+
本模块把「桶」泛化为灵枢的两种真实坐标:**日桶**(created_at)与**会话桶**(session)。
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import math
|
|
18
|
+
import os
|
|
19
|
+
import re
|
|
20
|
+
|
|
21
|
+
from . import bucketindex as _bi
|
|
22
|
+
|
|
23
|
+
#: 查询词切分(与检索面同口径:空白与中英标点,长度 ≥2)。
|
|
24
|
+
_SPLIT = re.compile(r"[\s,,、;;。!?::()()\[\]【】]+")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def terms(query):
|
|
28
|
+
return [t.lower() for t in _SPLIT.split(str(query or "")) if len(t) >= 2]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _meta_blob(store, nid):
|
|
32
|
+
"""桶内节点的**索引元数据**拼成的检索面(不读正文,避免逐节点读盘)。
|
|
33
|
+
|
|
34
|
+
代价取舍:只读盘一次建桶的成本不可接受(实测 7.7–281 秒),
|
|
35
|
+
故只用索引里已有的 tags / bucket_zh / role / content_kind / session。
|
|
36
|
+
"""
|
|
37
|
+
nodes = (getattr(store, "index", {}) or {}).get("nodes") or {}
|
|
38
|
+
rec = nodes.get(nid)
|
|
39
|
+
if not isinstance(rec, dict):
|
|
40
|
+
return ""
|
|
41
|
+
fm = rec.get("frontmatter")
|
|
42
|
+
if not isinstance(fm, dict) or not fm:
|
|
43
|
+
fm = rec
|
|
44
|
+
parts = []
|
|
45
|
+
for key in ("tags", "bucket_zh", "bucket", "role", "content_kind", "session"):
|
|
46
|
+
val = fm.get(key)
|
|
47
|
+
if isinstance(val, (list, tuple)):
|
|
48
|
+
parts.extend(str(x) for x in val)
|
|
49
|
+
elif val:
|
|
50
|
+
parts.append(str(val))
|
|
51
|
+
return " ".join(parts).lower()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
#: 桶词表缓存(按文件 mtime 失效):1.6MB JSON 每次 _load 要 0.5s,缓存后毫秒级。
|
|
55
|
+
_TABLE_CACHE = {"key": None, "table": None}
|
|
56
|
+
_SKEL_CACHE = {"key": None, "table": None}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def score_buckets(store, query, max_nodes_per_bucket=None):
|
|
60
|
+
"""给每个候选桶打分:词项命中数 × idf(读 `_bucketterms.json`,毫秒级)。
|
|
61
|
+
|
|
62
|
+
为什么用预建词表:逐节点读盘实测 7.7–281 秒、读索引元数据也要 3 秒;
|
|
63
|
+
桶词表在**建桶时**顺手落盘(主库 0.36 秒),查询期只做字典运算。
|
|
64
|
+
"""
|
|
65
|
+
ts = terms(query)
|
|
66
|
+
root = getattr(store, "root", None) or ""
|
|
67
|
+
path = (root + "/" + _bi.TERMS_FILE) if root else ""
|
|
68
|
+
try:
|
|
69
|
+
st = os.stat(path)
|
|
70
|
+
key = (path, st.st_mtime, st.st_size)
|
|
71
|
+
except OSError:
|
|
72
|
+
key = None
|
|
73
|
+
if _TABLE_CACHE["key"] != key or _TABLE_CACHE["table"] is None:
|
|
74
|
+
_TABLE_CACHE["table"] = _bi._load(store, _bi.TERMS_FILE) or {}
|
|
75
|
+
_TABLE_CACHE["key"] = key
|
|
76
|
+
table = _TABLE_CACHE["table"]
|
|
77
|
+
if not table or not ts:
|
|
78
|
+
return []
|
|
79
|
+
total = max(1, len(table))
|
|
80
|
+
doc_hits = {t: 0 for t in ts}
|
|
81
|
+
for _bucket, bag in table.items():
|
|
82
|
+
for t in ts:
|
|
83
|
+
if t in bag:
|
|
84
|
+
doc_hits[t] += 1
|
|
85
|
+
# 泛词过滤:df 超过 20% 桶的词(CCG 模板词如「功能名」「生效条件」)不参与打分,
|
|
86
|
+
# 否则 idf 虽小但累加后仍会主导排序(实测这些词出现在几乎所有节点里)。
|
|
87
|
+
cut = max(2, int(total * 0.2))
|
|
88
|
+
keep_terms = [t for t in ts if doc_hits.get(t, 0) <= cut] or ts
|
|
89
|
+
scored = []
|
|
90
|
+
for bucket, bag in table.items():
|
|
91
|
+
kind, _, key = str(bucket).partition(':')
|
|
92
|
+
s = 0.0
|
|
93
|
+
for t in keep_terms:
|
|
94
|
+
if t in bag:
|
|
95
|
+
df = max(1, doc_hits[t])
|
|
96
|
+
s += math.log(1.0 + total / df)
|
|
97
|
+
if s > 0:
|
|
98
|
+
scored.append((s, kind, key))
|
|
99
|
+
scored.sort(key=lambda x: (-x[0], x[1], x[2]))
|
|
100
|
+
return scored
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _table_and_df(store, ts):
|
|
104
|
+
"""取桶词表 + 每词的 df(多少桶含它)。两条打分路共用。"""
|
|
105
|
+
root = getattr(store, "root", None) or ""
|
|
106
|
+
path = (root + "/" + _bi.TERMS_FILE) if root else ""
|
|
107
|
+
try:
|
|
108
|
+
st = os.stat(path)
|
|
109
|
+
key = (path, st.st_mtime, st.st_size)
|
|
110
|
+
except OSError:
|
|
111
|
+
key = None
|
|
112
|
+
if _TABLE_CACHE["key"] != key or _TABLE_CACHE["table"] is None:
|
|
113
|
+
_TABLE_CACHE["table"] = _bi._load(store, _bi.TERMS_FILE) or {}
|
|
114
|
+
_TABLE_CACHE["key"] = key
|
|
115
|
+
table = _TABLE_CACHE["table"]
|
|
116
|
+
total = max(1, len(table))
|
|
117
|
+
doc_hits = {t: 0 for t in ts}
|
|
118
|
+
for _bucket, bag in table.items():
|
|
119
|
+
for t in ts:
|
|
120
|
+
if t in bag:
|
|
121
|
+
doc_hits[t] += 1
|
|
122
|
+
return table, total, doc_hits
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _bi_bucket_order(bucket):
|
|
126
|
+
"""桶序(同日/同会话按 key 升序)——只用于平局裁决,越小越优先。"""
|
|
127
|
+
return str(bucket)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def cooc_bucket_scores(store, query, min_terms=2, min_term_len=2):
|
|
131
|
+
"""**槽位共现打分(bench 同口径)**:桶得分 = 「查询里的短词」在该桶文本骨架里
|
|
132
|
+
出现的**个数**(子串语义),idf 仅作平局裁决。
|
|
133
|
+
|
|
134
|
+
为什么必须是子串而不是整词:bench(`bench_state_track.py`,99.28%)用
|
|
135
|
+
`value in chapter_text` 判共现;生产若按标点切整句再整词匹配,实测只有 34%;
|
|
136
|
+
换成汉字 n-gram 也只有 73.5%——**只有子串语义能对齐**,故按桶落一份文本骨架。
|
|
137
|
+
"""
|
|
138
|
+
ts = [t for t in terms(query) if len(t) >= min_term_len]
|
|
139
|
+
if not ts:
|
|
140
|
+
return []
|
|
141
|
+
skel = _skel_table(store)
|
|
142
|
+
table, total, _doc = _table_and_df(store, ts)
|
|
143
|
+
if not skel:
|
|
144
|
+
return []
|
|
145
|
+
df = {t: sum(1 for _b, blob in skel.items() if t in blob) for t in ts}
|
|
146
|
+
keys = [t for t in ts if df.get(t, 0) > 0]
|
|
147
|
+
# 稀有词不足时的**降档**:外测发现「江北回声那回」「我最早说…」这类题只有 1 个强实体、
|
|
148
|
+
# 没有日期词,min_terms=2 会直接返回空 ⇒ 上层回落「最近 14 天」⇒ 老锚点必被排除。
|
|
149
|
+
# 故仅剩 1 个稀有词时仍按它打分(仍要 hit ≥ 该词数,不会凭空造证据)。
|
|
150
|
+
if not keys:
|
|
151
|
+
return []
|
|
152
|
+
if len(keys) < min_terms:
|
|
153
|
+
min_terms = len(keys)
|
|
154
|
+
scored = []
|
|
155
|
+
for bucket, blob in skel.items():
|
|
156
|
+
kind, _, key = str(bucket).partition(':')
|
|
157
|
+
hit = sum(1 for t in keys if t in blob)
|
|
158
|
+
if hit < min_terms:
|
|
159
|
+
continue
|
|
160
|
+
# 平局规则与 bench 对齐:**同分取桶序最早**(bench 是取章号最小)。
|
|
161
|
+
# 早先用 idf 和做平局裁决,实测把 top-1 从 99.28% 压到 94.20%。
|
|
162
|
+
scored.append((float(hit), _bi_bucket_order(str(bucket)), kind, key))
|
|
163
|
+
# 注意:不能 sort 再 reverse——那会把平局裁决的桶序也一起翻转(实测 94% vs 99%)。
|
|
164
|
+
scored.sort(key=lambda x: (-x[0], x[1], x[2]))
|
|
165
|
+
return [(h, k, kk) for h, _t, k, kk in scored]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _skel_table(store):
|
|
169
|
+
"""读桶文本骨架(`_bucketskel.json`),带 mtime 缓存。"""
|
|
170
|
+
root = getattr(store, "root", None) or ""
|
|
171
|
+
path = (root + "/" + _bi.SKEL_FILE) if root else ""
|
|
172
|
+
try:
|
|
173
|
+
st = os.stat(path)
|
|
174
|
+
key = (path, st.st_mtime, st.st_size)
|
|
175
|
+
except OSError:
|
|
176
|
+
key = None
|
|
177
|
+
if _SKEL_CACHE["key"] != key or _SKEL_CACHE["table"] is None:
|
|
178
|
+
_SKEL_CACHE["table"] = _bi._load(store, _bi.SKEL_FILE) or {}
|
|
179
|
+
_SKEL_CACHE["key"] = key
|
|
180
|
+
return _SKEL_CACHE["table"]
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def pick_scopes(store, query, session=None, limit=3, now=None):
|
|
184
|
+
"""选范围:**声明优先**(会话 > 显式日期),否则按共现打分取前 limit 个桶。
|
|
185
|
+
|
|
186
|
+
声明优先的理由:声明是任务的一部分(「那天」「本会话」是用户给定的范围),
|
|
187
|
+
必须当过滤用;共现只在**没有声明**时才作为推断手段。
|
|
188
|
+
"""
|
|
189
|
+
picked = []
|
|
190
|
+
sid = str(session or "")
|
|
191
|
+
if sid and sid != _bi.UNATTRIBUTED and _bi.session_ids(store, sid):
|
|
192
|
+
picked.append(("session", sid, 1.0))
|
|
193
|
+
try:
|
|
194
|
+
from . import timeboost as _tb
|
|
195
|
+
# 逻辑时钟必须由调用方注入(#111 契约同纪律):早先取真实 time.time(),
|
|
196
|
+
# 外部复测把逻辑时间推进到题干 now 时,「三天前」永远指向错的那一天,
|
|
197
|
+
# 时间类增益根本测不出来(2026-11-10 测评反馈 + 修)。
|
|
198
|
+
_now = now if now is not None else __import__("time").time()
|
|
199
|
+
for day in sorted(_tb.query_dates(query, _now), reverse=True):
|
|
200
|
+
if _bi.day_ids(store, day):
|
|
201
|
+
picked.append(("day", day, 1.0))
|
|
202
|
+
except Exception: # noqa: BLE001 —— 日期解析失败不影响共现推断
|
|
203
|
+
pass
|
|
204
|
+
if len(picked) < limit:
|
|
205
|
+
ranked = score_buckets(store, query)
|
|
206
|
+
if os.environ.get("MDCG_SCOPESEL_COOC", "1") == "1":
|
|
207
|
+
ranked = cooc_bucket_scores(store, query) or ranked
|
|
208
|
+
for s, kind, key in ranked:
|
|
209
|
+
if (kind, key) in [(k, kk) for _x, k, kk in picked]:
|
|
210
|
+
continue
|
|
211
|
+
picked.append((kind, key, s))
|
|
212
|
+
if len(picked) >= limit:
|
|
213
|
+
break
|
|
214
|
+
return picked[:limit]
|
package/md_cg/scrub.py
CHANGED
|
@@ -811,11 +811,41 @@ def audit(cg, node_ids=None, *, hops: int = 1, min_severity: str = "info",
|
|
|
811
811
|
"t": now}
|
|
812
812
|
|
|
813
813
|
|
|
814
|
-
|
|
814
|
+
#: `_weaken` 的**去重哨兵**(issue #827):区分「这一条已经扣过」与
|
|
815
|
+
#: 「`cg.verify` 因节点取不到而返回的 None」——两者都是 None 就没法在调用侧
|
|
816
|
+
#: 分辨,于是本可以静默吞掉一条真实矛盾的处置。
|
|
817
|
+
WEAKEN_DEDUPED = object()
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
# 生效条件:steps 为 (kind, detail) 序列;**同一 (kind, detail) 只扣一次**——先查节点自己的 evidence_log,已有的证据串直接返回 WEAKEN_DEDUPED(issue #827);否则循环 max(1, int(times)) 次调用 cg.verify(nid, "scrub:"+kind+":"+str(detail)[:120], "weakened") 并返回最后一次结果(times≤0 按 1 次执行)。【#444】detail 会进 verify 的 evidence(→ fm 明文面 / _trust.jsonl / 对端节点 .md),故**调用方传入的 detail 不得含正文原值**——contradiction 支已改为摘要,其它支传的是键名/日期类短值。
|
|
815
821
|
def _weaken(cg, nid, kind, detail, times=1):
|
|
822
|
+
"""扣减 confidence,**同一矛盾只扣一次**(issue #827)。
|
|
823
|
+
|
|
824
|
+
为什么不能只靠 `_handled` 的账本判幂等:`_handled()` 读的是
|
|
825
|
+
`cg.root/_scrub.jsonl`,而 `sleep.iterate()` 在**影子工作树**上跑
|
|
826
|
+
`scrub.sweep`。影子每轮由 `lib.git` checkout 重建,而 `add_pathspecs`
|
|
827
|
+
只入册 `*.md`(见 sleep.add_pathspecs)——`_scrub.jsonl` 是 untracked,
|
|
828
|
+
每轮都是**空的**,于是 `_handled()` 恒返回 ∅,同一矛盾在每轮睡眠里被
|
|
829
|
+
再扣一次。实测(本仓活库 2026-11-08):407 条负向证据只有 83 条不同
|
|
830
|
+
(重复率 4.90x),单条"同键不同值"被记 52 次,confidence 被压到 0.00
|
|
831
|
+
下限——它退化成"睡眠周期跑过几轮"的计数器。
|
|
832
|
+
|
|
833
|
+
判据收在这一层而不是账本:**节点自己的 `evidence_log` 是随 `.md` 一起
|
|
834
|
+
被追踪的**,所以它跟着节点走,账本丢了也不影响去重。读不到节点(已删)
|
|
835
|
+
时退回照扣——宁可按旧口径记一次,不静默吞掉一个真实矛盾。
|
|
836
|
+
"""
|
|
837
|
+
want = f"scrub:{kind}:{str(detail)[:120]}"
|
|
838
|
+
try:
|
|
839
|
+
_fm = ((cg.get(nid) or {}).get("frontmatter") or {})
|
|
840
|
+
seen = {str(e.get("evidence") or "")
|
|
841
|
+
for e in (_fm.get("evidence_log") or [])}
|
|
842
|
+
except Exception: # noqa: BLE001
|
|
843
|
+
seen = set()
|
|
844
|
+
if want in seen:
|
|
845
|
+
return WEAKEN_DEDUPED # 这一条已经扣过 —— 幂等命中,零副作用
|
|
816
846
|
res = None
|
|
817
847
|
for _ in range(max(1, int(times))):
|
|
818
|
-
res = cg.verify(nid,
|
|
848
|
+
res = cg.verify(nid, want, "weakened")
|
|
819
849
|
return res
|
|
820
850
|
|
|
821
851
|
|
|
@@ -876,11 +906,24 @@ def decontaminate(cg, node_ids=None, *, kinds=None, dry_run: bool = True,
|
|
|
876
906
|
continue
|
|
877
907
|
|
|
878
908
|
done = []
|
|
909
|
+
_deduped = False
|
|
879
910
|
try:
|
|
880
|
-
_weaken(cg, nid, kind, issue["detail"], weaken_times)
|
|
881
|
-
|
|
911
|
+
_w = _weaken(cg, nid, kind, issue["detail"], weaken_times)
|
|
912
|
+
# #827:`_weaken` 按节点 evidence_log 去重。命中即「这条矛盾此前
|
|
913
|
+
# 已扣过」,本批**没有任何动作**——不能计进 applied(否则账本丢了
|
|
914
|
+
# 就会把每轮都记成「又处置了 N 条」,读数虚增);计入
|
|
915
|
+
# already_handled,与账本侧的幂等命中同一个口径。
|
|
916
|
+
_deduped = _w is WEAKEN_DEDUPED
|
|
917
|
+
if not _deduped:
|
|
918
|
+
done.append("weaken")
|
|
882
919
|
except Exception as exc: # noqa: BLE001
|
|
883
920
|
done.append(f"weaken_failed:{type(exc).__name__}")
|
|
921
|
+
if _deduped:
|
|
922
|
+
n_done += 1
|
|
923
|
+
actions.append({"node_id": nid, "kind": kind, "action": "skip",
|
|
924
|
+
"detail": "已处置过(节点证据账本命中,幂等)",
|
|
925
|
+
"applied": False})
|
|
926
|
+
continue
|
|
884
927
|
if demote and issue["severity"] in ("high", "low"):
|
|
885
928
|
try:
|
|
886
929
|
fm = _fm_of(cg, nid, None)
|
|
@@ -1010,16 +1053,30 @@ def _apply_offset(cg, offset, *, override=False, min_evidence=1, actor=None):
|
|
|
1010
1053
|
return adjusted, skipped
|
|
1011
1054
|
|
|
1012
1055
|
|
|
1013
|
-
# 生效条件:cg 和 apply/override/actor/max_offset/min_evidence 传入后,若 metacognition.calibration(cg) 返回 ok
|
|
1056
|
+
# 生效条件:cg 和 apply/override/actor/max_offset/min_evidence 传入后,若 metacognition.calibration(cg) 返回 ok 假或 calibration_defined 假(issue #829:分母无正例,gap 恒等于 expected、不携带方向信息),则返回 {'ok':False,'reason':...} 且 **不产出 suggested_offset、零写回**;若 ok 真且 calibration_defined 真,则用 gap=float(cal.get('gap') or 0.0) 和 max_offset 计算 offset=round(max(-max_offset,min(max_offset,-gap)),4),对 cal.get('bins') or [] 中 accuracy 非 None 的 bin 生成 bins_bias 并排序;仅当 apply 为真且 abs(offset)>1e-9 时调用 _apply_offset(cg,offset,override=override,min_evidence=min_evidence) 并写日志,最后返回 ok True 及 verdict/gap/建议 offset 等字段;
|
|
1014
1057
|
def calibrate(cg, *, apply: bool = False, override: bool = False, actor=None,
|
|
1015
1058
|
max_offset: float = MAX_OFFSET, min_evidence: int = 1) -> dict:
|
|
1016
|
-
"""校准偏差:自报置信 vs 实测正确率 → 偏置建议(`apply=True` 才写回)。
|
|
1059
|
+
"""校准偏差:自报置信 vs 实测正确率 → 偏置建议(`apply=True` 才写回)。
|
|
1060
|
+
|
|
1061
|
+
issue #829:`calibration_defined=False`(分母里正例为 0)时**拒绝出偏置**。
|
|
1062
|
+
修前:该情形的 gap 恒等于 expected(因为 actual 数学上恒 0),
|
|
1063
|
+
`apply=True` 会据此把全库 confidence 一律下调 —— 即「一个不携带信息的
|
|
1064
|
+
读数」直接改写事实层。恒红比不报更有害的同一道理,这里恒偏置比不调更有害。
|
|
1065
|
+
"""
|
|
1017
1066
|
from . import metacognition
|
|
1018
1067
|
cal = metacognition.calibration(cg)
|
|
1019
|
-
if not cal.get("ok"):
|
|
1020
|
-
|
|
1068
|
+
if not cal.get("ok") or not cal.get("calibration_defined", True):
|
|
1069
|
+
reason = (cal.get("reason") or
|
|
1070
|
+
("no_positive_evidence"
|
|
1071
|
+
if cal.get("ok") and not cal.get("calibration_defined", True)
|
|
1072
|
+
else "insufficient_data"))
|
|
1073
|
+
return {"ok": False, "reason": reason,
|
|
1021
1074
|
"calibration": cal, "applied": False, "n_adjusted": 0,
|
|
1022
|
-
"
|
|
1075
|
+
"suggested_offset": 0.0,
|
|
1076
|
+
"hint": ("分母里没有正例(无 confirmed 裁决):actual 恒 0、"
|
|
1077
|
+
"gap 不携带方向信息,拒绝出偏置。先补 verify(confirmed)"
|
|
1078
|
+
if reason == "no_positive_evidence"
|
|
1079
|
+
else "先 verify() 积累证据,校准才有样本")}
|
|
1023
1080
|
gap = float(cal.get("gap") or 0.0)
|
|
1024
1081
|
offset = round(max(-float(max_offset), min(float(max_offset), -gap)), 4)
|
|
1025
1082
|
bins_bias = []
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""自述轮次召回(零模型、与题目无关)——外测实证 12.8% → 35.9% 的那条规则。
|
|
3
|
+
|
|
4
|
+
判据(外测 2026-11-12 实验,无泄题、无 LLM):
|
|
5
|
+
> 用户消息匹配「我叫 / 我今年 / 我大学 / 我最早 / 我在…做 / 我住 / 我妈我爸 / 学徒 / 退休…」
|
|
6
|
+
> → 这些轮次直接并入 pack(不经过档案、不经过 LLM)。
|
|
7
|
+
|
|
8
|
+
结果:值覆盖 5/39 → 14/39(12.8% → 35.9%),题级全命中 1/19 → 6/19;
|
|
9
|
+
成本:每库仅 5–15 轮额外注入。
|
|
10
|
+
|
|
11
|
+
调用口径(**两种写法都认**,见 _root_of):
|
|
12
|
+
from md_cg import selfstmt as ss
|
|
13
|
+
ss.load(g) / ss.load(g.root) # 读登记数
|
|
14
|
+
ss.record(g, nid) # 单条登记(写时钩子已在 add() 里自动调)
|
|
15
|
+
ss.rebuild(g) # 存量库全量补登(建图之后、recall 之前)
|
|
16
|
+
|
|
17
|
+
不适用条件:
|
|
18
|
+
· 主语是「她/他」的亲属陈述(如「她61了,从社区超市理货员退下来」若词表未收录)不命中——需事件/关系抽取;
|
|
19
|
+
· 事件型转述(转行、兼职经历)不保证命中;
|
|
20
|
+
· 规则漏检就是漏,不做二次推断。
|
|
21
|
+
"""
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
import re
|
|
27
|
+
|
|
28
|
+
#: 自述句式(**与题目无关**;宁可少收,不加宽到泛化「我」)
|
|
29
|
+
SELF_RX = re.compile(
|
|
30
|
+
r"(?:我叫|我的名字|我今年|我\d{1,2}岁|我大学|我念|我读|我最早|后来(?:才|就)?(?:转|去|做)|"
|
|
31
|
+
r"我在[^,。]{0,12}(?:做|当|上班)|我住|我(?:妈|爸|母亲|父亲)|"
|
|
32
|
+
# 【外测 2026-11-12】「我有 + 身体状况」也是自述属性(原句:我有近视,看书时要戴眼镜…)。
|
|
33
|
+
# 负向约束:排除「我有个朋友/同事/他/她」这类第三人陈述,避免误命中。
|
|
34
|
+
r"我有[^,。朋友同事他她]{0,6}(?:近视|散光|鼻炎|胃病|胃疼|失眠|过敏|腰伤|颈椎|高血压|糖尿病)|"
|
|
35
|
+
r"我(?:近视|散光|鼻炎|失眠|过敏|腰伤)|"
|
|
36
|
+
r"学徒|退休|理货员|钳工|调酒|结构设计|兼职|写(?:悬疑|小说))"
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
_FILE = "_selfstmt.json"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# 生效条件:参数可以是库根路径,也可以是 cg 实例;返回库根路径
|
|
43
|
+
def _root_of(x):
|
|
44
|
+
"""★API 容错:既收库根路径,也收 cg 实例。
|
|
45
|
+
|
|
46
|
+
踩过的坑(外测 2026-11-12):load(g) 把实例当路径拼,文件不存在 ⇒ 静默返回 [],
|
|
47
|
+
被误读成「写时登记没生效」。
|
|
48
|
+
"""
|
|
49
|
+
if hasattr(x, "root"):
|
|
50
|
+
return str(getattr(x, "root") or "")
|
|
51
|
+
return str(x or "")
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
#: 信息密度权重(用于注入排序:先给最可能是「答案句」的轮次,而不是最新的)
|
|
55
|
+
DENSITY_PATTERNS = (
|
|
56
|
+
(3, ("我叫", "我的名字", "我大学", "我最早", "我今年")),
|
|
57
|
+
(2, ("我住", "我妈", "我爸", "母亲", "父亲", "岁", "我有", "近视", "失眠", "过敏")),
|
|
58
|
+
(1, ("学徒", "退休", "理货员", "钳工", "调酒", "结构设计", "兼职",
|
|
59
|
+
"念", "读", "做")),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# 生效条件:text 为节点正文;返回信息密度分(越高越像「答案句」)
|
|
64
|
+
def density(text):
|
|
65
|
+
"""自述轮次的信息密度分(确定性;用于注入排序)。"""
|
|
66
|
+
body = str(text or "")
|
|
67
|
+
score = 0
|
|
68
|
+
for w, pats in DENSITY_PATTERNS:
|
|
69
|
+
for p in pats:
|
|
70
|
+
if p in body:
|
|
71
|
+
score += w
|
|
72
|
+
break
|
|
73
|
+
return score
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# 生效条件:text 为节点正文;命中自述句式返回 True
|
|
77
|
+
def matches(text):
|
|
78
|
+
"""是否自述轮次(确定性正则,与查询/题目无关)。"""
|
|
79
|
+
return bool(SELF_RX.search(str(text or "")))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# 生效条件:root 为库根目录或 cg 实例;返回登记文件路径
|
|
83
|
+
def _path(root):
|
|
84
|
+
return os.path.join(_root_of(root), _FILE)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# 生效条件:root 为库根或 cg;返回已登记的节点 id 列表(读失败一律当空,不抛)
|
|
88
|
+
def load(root):
|
|
89
|
+
"""读登记表。文件不存在/损坏 ⇒ 返回 [](不让召回因它挂掉)。"""
|
|
90
|
+
try:
|
|
91
|
+
with open(_path(root), "r", encoding="utf-8") as f:
|
|
92
|
+
data = json.load(f)
|
|
93
|
+
return [str(x) for x in (data.get("ids") or [])]
|
|
94
|
+
except (OSError, ValueError):
|
|
95
|
+
return []
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# 生效条件:root 为库根或 cg、nid 为节点 id;命中则登记(幂等)
|
|
99
|
+
def record(root, nid):
|
|
100
|
+
"""写时登记:把自述轮次的 id 记入小索引(幂等、去重)。"""
|
|
101
|
+
base = _root_of(root)
|
|
102
|
+
if not base:
|
|
103
|
+
return False
|
|
104
|
+
p = os.path.join(base, _FILE)
|
|
105
|
+
ids = load(base)
|
|
106
|
+
if str(nid) in ids:
|
|
107
|
+
return False
|
|
108
|
+
ids.append(str(nid))
|
|
109
|
+
tmp = p + ".tmp"
|
|
110
|
+
try:
|
|
111
|
+
os.makedirs(os.path.dirname(p), exist_ok=True)
|
|
112
|
+
with open(tmp, "w", encoding="utf-8") as f:
|
|
113
|
+
json.dump({"ids": ids[-5000:]}, f, ensure_ascii=False)
|
|
114
|
+
os.replace(tmp, p)
|
|
115
|
+
except OSError:
|
|
116
|
+
return False
|
|
117
|
+
return True
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
# 生效条件:cg 提供 index/get;返回 {scanned, recorded, ids}
|
|
121
|
+
def rebuild(cg, dry_run=False, limit=100000):
|
|
122
|
+
"""全量重建登记表(存量库补登用;不改任何节点)。"""
|
|
123
|
+
base = _root_of(cg)
|
|
124
|
+
ids = []
|
|
125
|
+
scanned = 0
|
|
126
|
+
for nid in list((cg.index.get("nodes") or {})):
|
|
127
|
+
scanned += 1
|
|
128
|
+
if scanned > limit:
|
|
129
|
+
break
|
|
130
|
+
node = cg.get(nid) or {}
|
|
131
|
+
if matches(node.get("content") or ""):
|
|
132
|
+
ids.append(str(nid))
|
|
133
|
+
if not dry_run and base:
|
|
134
|
+
try:
|
|
135
|
+
with open(os.path.join(base, _FILE), "w", encoding="utf-8") as f:
|
|
136
|
+
json.dump({"ids": ids[-5000:]}, f, ensure_ascii=False)
|
|
137
|
+
except OSError:
|
|
138
|
+
pass
|
|
139
|
+
return {"scanned": scanned, "recorded": len(ids), "dry_run": bool(dry_run)}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""状态事件保守抽取(会话轮 → 台账)——每轮写入链的第二步,**生产方在此**。
|
|
3
|
+
|
|
4
|
+
# 功能名:会话轮的保守状态抽取(用户轮 → 状态事件台账)
|
|
5
|
+
# 生效条件:每轮写 `_recent` 窗口之后,对**同一轮**用户发言调 `extract_and_record`;
|
|
6
|
+
# 开关 `MDCG_STATE_EXTRACT` 缺省开(`=0` 关)。
|
|
7
|
+
# 子功能:① 只抽用户轮 ② 两条高精度规则(地点·所在迁移 / 情感·偏好)③ 同值去重
|
|
8
|
+
# ④ evidence/actor 可甄别 ⑤ 开关。
|
|
9
|
+
# 执行:`extract_turn(text, role, subject)` 纯函数给候选;`extract_and_record(cg, …)`
|
|
10
|
+
# 落台账(经 `state_events.append` 单点)。
|
|
11
|
+
# 验证方式:`md_cg/test_state_extract.py`(原 `scripts/test_state_extract.py`)。
|
|
12
|
+
# 不适用条件:不做计划族抽取(任务走任务台账);不做语义消歧(`not_fact` 等);
|
|
13
|
+
# 不做助手轮(助手自述不复盖真值)。
|
|
14
|
+
|
|
15
|
+
**为什么本体在 `md_cg/` 而非 `scripts/`(2026-11-02,本仓真实缺陷修复)**:
|
|
16
|
+
本模块此前落在 `scripts/state_extract.py`,而 `package.json` 的 `files` 白名单
|
|
17
|
+
只发 `scripts/workspace_index.py` ⇒ **随包发布物里根本没有本模块**。实测
|
|
18
|
+
`npm pack --dry-run --ignore-scripts` 的发货清单 1791 件中 `scripts/` 仅 1 件,
|
|
19
|
+
`state_extract.py` / `sync_zcode_session.py` 双双 MISSING。后果:装机侧
|
|
20
|
+
(`~/.dsh/profiles/*/node_modules/@furongjun1999/dsh-memory/`)没有抽取器,
|
|
21
|
+
`sync_zcode_session._load_state_extract()` 无从装载 ⇒ **每轮记账链在装机环境里
|
|
22
|
+
是断的**,生产台账 `_state_events.jsonl` 长期 `events=0`(实测)。
|
|
23
|
+
`md_cg/` 整目录在 `files` 白名单内且已随包发货,故把**生产方**移到此地;
|
|
24
|
+
`scripts/state_extract.py` 保留为**薄垫片**(只 re-export,不含第二份判据),
|
|
25
|
+
既有 import 路径与 `sync_zcode_session` 的装载点逐字不变。
|
|
26
|
+
|
|
27
|
+
使用者 2026-10-06 架构裁定:「把这个功能作为会话的 10 轮信息写入,并在写入 mdcg
|
|
28
|
+
的时候做处理……每次写入异步加了一些过程,但是好处是得到了可维护的状态,同时具备
|
|
29
|
+
了认知图和语义时空图,原始信息的关系追溯。」
|
|
30
|
+
|
|
31
|
+
定位(与写窗口同一条链路的第二步):
|
|
32
|
+
`sync_zcode_session` 每轮把原文写 `_recent` 窗口 → **本件对同一轮做保守抽取**
|
|
33
|
+
→ 命中者 append 进状态台账(`state_events.append` 的库层单点)→ `state_slots`
|
|
34
|
+
投影/`state_atlas` 视图、`_recent`/转写为原文面——**三层同源、evidence 互指**。
|
|
35
|
+
钩子在轮末以子进程运行(会话外处理、不占模型上下文时间)——即使用者说的「异步」。
|
|
36
|
+
|
|
37
|
+
保守口径(v1,写入在役库的纪律:**宁可漏,不可误**):
|
|
38
|
+
① **只抽用户轮**(真人发言=事实源;助手自述不复盖真值——沿鲸娘线口径);
|
|
39
|
+
② 仅两条**高精度**规则(探针依据:裸词形噪声高者一律不做):
|
|
40
|
+
· 地点·所在(迁移):`(我)?(搬家|搬到了?|搬去|现在住在|住在)X`
|
|
41
|
+
· 情感·偏好:`我(最|很|超|不太|不怎么)?(喜欢|爱|讨厌|不喜欢)X`
|
|
42
|
+
③ **去重**:抽取值与投影现值相同即跳过(防每轮重复记账);
|
|
43
|
+
④ 全程可追溯:`evidence` 带 `[auto] session=… turn=… 短证`、`actor="state_extract"`、
|
|
44
|
+
slot 归通用族(可用 op=state_chain 逐条审计),误记可整体甄别/清理;
|
|
45
|
+
⑤ 开关 `MDCG_STATE_EXTRACT`:**缺省开**(使用者的架构裁定)——`=0` 关闭。
|
|
46
|
+
|
|
47
|
+
不适用(如实):工程/技术会话中的"我喜欢这个方案"类语句属**真偏好**(记录无误亦非
|
|
48
|
+
无用);但"我要加个功能"这类任务性语句**不抽**(计划族规则 v1 不做——任务走任务
|
|
49
|
+
台账,不走状态台账)。
|
|
50
|
+
"""
|
|
51
|
+
from __future__ import annotations
|
|
52
|
+
|
|
53
|
+
import os
|
|
54
|
+
import re
|
|
55
|
+
|
|
56
|
+
#: 抽取主体(默认「我」——本机使用者;env `MDCG_STATE_SUBJECT` 可覆盖)
|
|
57
|
+
DEFAULT_SUBJECT = "我"
|
|
58
|
+
|
|
59
|
+
#: 高精度规则表: (正则, 数值组名, 槽位, kind)
|
|
60
|
+
#: 只收「用户轮 + 第一人称持久状态」——逐条都给得出"为什么不会误伤"。
|
|
61
|
+
RULES = [
|
|
62
|
+
# 地点·所在(迁移):搬家/搬到/搬去/现在住在/住在 + 目的地。
|
|
63
|
+
# 「在」单字不入式("我在看代码"必误伤);「住」需要完整词形。
|
|
64
|
+
(re.compile(r"(?:我)?(?:搬(?:家|到了|到|去了|去)|现在住(?:在)?|住在)"
|
|
65
|
+
r"(?P<v>[^\s,。!?,;;]{2,12})"),
|
|
66
|
+
"v", "地点·所在", "migration"),
|
|
67
|
+
# 情感·偏好:我(最/很/超/不太/不怎么)?喜欢/爱/讨厌/不喜欢 + 对象。
|
|
68
|
+
(re.compile(r"我(?:最|很|超|不太|不怎么)?(?P<p>喜欢|爱|讨厌|不喜欢)"
|
|
69
|
+
r"(?P<v>[^\s,。!?,;;]{1,10})"),
|
|
70
|
+
"v", "情感·偏好", "acquisition"),
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def enabled() -> bool:
|
|
75
|
+
"""开关:缺省开(使用者裁定);`MDCG_STATE_EXTRACT=0` 关闭。"""
|
|
76
|
+
return os.environ.get("MDCG_STATE_EXTRACT", "1") != "0"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _clean_value(val: str) -> str:
|
|
80
|
+
"""剥句尾语气助词(了/啦/呀/哦/喔/噢/嘛/呢)——「青岛了」→「青岛」。"""
|
|
81
|
+
return re.sub(r"[了啦呀哦喔噢嘛呢]+$", "", val).strip()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _short(text: str, n: int = 24) -> str:
|
|
85
|
+
t = re.sub(r"\s+", " ", str(text or "")).strip()
|
|
86
|
+
return t[:n]
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
#: 否定前缀(#169):不/没/无/别(可隔空格、可套「太/怎么」)——
|
|
90
|
+
#: 出现在正面谓词之前即判负;口径「宁可漏,不可误」:前缀位有否定标记就不作正面记。
|
|
91
|
+
_NEG_PREFIX_RE = re.compile(r"[不没无别]\s*(?:怎么|太)*")
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _neg_polarity(match_text: str, pred: str) -> bool:
|
|
95
|
+
"""极性判定:谓词自带否定(讨厌/不喜欢)或前缀位出现否定标记 => 负面。取首次出现(find):谓词在宾语中再现时不误判。"""
|
|
96
|
+
if not pred:
|
|
97
|
+
return False # 非情感规则(无 p 组):不带极性
|
|
98
|
+
if pred in ("讨厌", "不喜欢"):
|
|
99
|
+
return True
|
|
100
|
+
idx = match_text.find(pred)
|
|
101
|
+
if idx <= 0:
|
|
102
|
+
return False
|
|
103
|
+
return bool(_NEG_PREFIX_RE.search(match_text[:idx]))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def extract_turn(text: str, role: str, subject: str = None):
|
|
107
|
+
"""单轮抽取(只认用户轮)→ [候选事件 dict, ...](零命中返回 [])。"""
|
|
108
|
+
subject = subject or os.environ.get("MDCG_STATE_SUBJECT") or DEFAULT_SUBJECT
|
|
109
|
+
if role != "user" or not text:
|
|
110
|
+
return []
|
|
111
|
+
out = []
|
|
112
|
+
for rx, gp, slot, kind in RULES:
|
|
113
|
+
m = rx.search(text)
|
|
114
|
+
if not m:
|
|
115
|
+
continue
|
|
116
|
+
val = _clean_value((m.group(gp) or "").strip())
|
|
117
|
+
if not val:
|
|
118
|
+
continue
|
|
119
|
+
neg = _neg_polarity(m.group(0), m.groupdict().get("p") or "")
|
|
120
|
+
new = ("不喜欢:" + val) if neg else val
|
|
121
|
+
out.append({"subject": subject, "slot": slot, "new": new, "kind": kind,
|
|
122
|
+
"match": m.group(0)})
|
|
123
|
+
return out
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def extract_and_record(cg, session: str, turn_no, role: str, text: str,
|
|
127
|
+
subject: str = None) -> int:
|
|
128
|
+
"""抽取并 append 进状态台账(去重:与现值相同即跳过)。返回写入条数。
|
|
129
|
+
|
|
130
|
+
调用方(每轮同步链)在写 `_recent` 窗口之后调用本函数;任何异常由调用方
|
|
131
|
+
fail-soft 兜住(与既有钩子纪律一致)。
|
|
132
|
+
"""
|
|
133
|
+
from . import state_events as _se
|
|
134
|
+
from . import state_slots as _ss
|
|
135
|
+
n = 0
|
|
136
|
+
for ev in extract_turn(text, role, subject):
|
|
137
|
+
cur = _ss.project(cg, subject=ev["subject"], slot=ev["slot"],
|
|
138
|
+
include_retired=True, history=False)
|
|
139
|
+
live = cur[0].get("value") if cur else None
|
|
140
|
+
if live == ev["new"]:
|
|
141
|
+
continue # 现值相同:去重(防每轮重复记账)
|
|
142
|
+
_se.append(cg, ev["subject"], ev["slot"],
|
|
143
|
+
old=(live if live is not None else None), new=ev["new"],
|
|
144
|
+
kind=ev["kind"], seq=turn_no,
|
|
145
|
+
evidence="[auto] session=%s turn=%s %s"
|
|
146
|
+
% (session, turn_no, _short(text)),
|
|
147
|
+
actor="state_extract")
|
|
148
|
+
n += 1
|
|
149
|
+
return n
|