@furongjun1999/dsh-memory 0.4.8 → 0.4.9
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/README.md +54 -26
- package/codebuddy/CODEBUDDY.md +196 -195
- package/codebuddy/README.md +13 -1
- package/codebuddy/mcp.json +9 -0
- package/docs/GBrain/345/217/257/345/200/237/351/211/264/347/202/271_/347/201/265/346/236/242/350/220/275/347/202/271/344/272/244/346/216/245_20260919.md +169 -0
- package/docs/README.md +1 -1
- package/docs/discipline/harnesses.yaml +18 -7
- package/docs/discipline/templates/full.md.tmpl +4 -3
- package/docs/experiments/linkref_backfill/candidates_20260917.json +726 -0
- package/docs/experiments/linkref_backfill/candidates_internal_20260917.json +602 -0
- package/docs/experiments/linkref_backfill/candidates_internal_v2.json +603 -0
- package/docs/experiments/linkref_backfill/candidates_secret_20260917.json +884 -0
- package/docs/experiments/linkref_backfill/candidates_secret_v2.json +789 -0
- package/docs/hive//345/244/232/347/253/257harness/351/200/232/344/277/241/345/245/221/347/272/246_v0.1.md +42 -0
- package/docs/hive//346/243/200/347/264/242/346/224/266/346/225/233/345/256/236/346/265/213/344/270/216S1b/350/256/276/350/256/241_v0.1.md +43 -0
- package/docs/hive//346/243/200/347/264/242/350/267/257/345/276/204/344/270/216/350/256/244/347/237/245/347/273/223/346/236/204/345/245/221/347/272/246_v0.1.md +102 -0
- package/docs/hive//347/234/237/345/256/236/345/272/223/347/253/257/345/210/260/347/253/257/345/256/236/346/265/213_S1b/344/270/216/345/217/254/345/233/236/346/235/203/350/241/241_v0.1.md +57 -0
- package/docs/hive//347/234/237/345/256/236/345/272/223/347/253/257/345/210/260/347/253/257/345/256/236/346/265/213_S7/345/200/222/346/216/222/345/200/231/351/200/211/345/261/202_v0.1.md +126 -0
- package/docs/hive//350/234/202/345/267/242/345/217/214/345/256/236/344/276/213/344/272/222/351/252/214_/350/256/276/350/256/241/345/256/232/347/250/277.md +503 -0
- package/docs/mdcg/D_meta_/345/267/245/347/250/213/345/214/226/346/226/271/346/241/210_v0.2.md +216 -0
- package/docs/mdcg/README/350/257/246/347/273/206/347/211/210_v0.4.5.md +631 -625
- package/docs/mdcg//344/273/243/347/240/201/350/257/204/345/256/241/344/270/216/346/235/241/344/273/266/345/214/226/346/263/250/351/207/212_/345/245/221/347/272/246_v0.1.md +82 -0
- package/docs/mdcg//345/205/250/345/272/223/344/273/243/347/240/201/350/257/204/345/256/241/344/270/216/346/235/241/344/273/266/345/214/226/346/263/250/351/207/212_/350/256/241/345/210/222_v0.1.md +600 -0
- package/docs/mdcg//345/212/237/350/203/275/350/260/203/347/224/250/346/230/240/345/260/204/350/241/250_v0.1.md +47 -45
- package/docs/mdcg//345/255/220/344/273/243/347/220/206/351/205/215/347/275/256/346/240/207/345/207/{206_v0.4.md → 206_v0.5.md} +92 -4
- package/docs/mdcg//347/201/265/346/236/242/350/256/260/345/277/206/345/212/250/350/257/215/345/215/217/350/256/256_v1.0-draft.md +172 -0
- package/docs/mdcg//347/216/257/344/272/214_/347/231/275/347/256/261/345/241/253/345/205/205/346/265/201/346/260/264/347/272/277_v0.1.md +31 -0
- package/docs/mdcg//350/267/250/347/253/257/351/252/214/350/257/201/344/270/216/345/220/214/346/255/245/345/215/217/350/256/256_v0.1.md +93 -0
- package/docs/theory//345/271/266/345/217/221/345/277/205/347/204/266/346/200/247/347/220/206/350/256/272_v0.2.md +2 -2
- package/docs/theory//347/220/206/350/256/272_/346/234/272/345/210/266_/344/273/243/347/240/201_/345/256/236/351/252/214_/347/274/272/345/217/243/347/237/251/351/230/265_v0.1.md +3 -3
- package/docs/theory//350/256/244/347/237/245/344/273/243/347/220/206/344/270/216/346/224/266/346/225/233/347/273/223/346/236/204_/346/235/241/344/273/266/350/256/272/351/207/215/346/236/204_v0.1.md +2 -2
- package/docs//345/267/245/344/275/234/347/272/252/345/276/213_/350/256/244/347/237/245/345/233/276/346/235/241/347/233/256_v1.1.json +434 -433
- package/docs//347/201/265/346/236/242/350/207/252/346/210/221/346/224/271/350/277/233/345/267/245/344/275/234/350/256/241/345/210/222_/345/244/226/351/203/250/347/240/224/347/251/266/347/263/273/345/210/227/345/220/270/346/224/266_v1_20260919.md +286 -0
- package/dsh/README.md +33 -0
- package/dsh/cordis.yml.example +13 -7
- package/dsh/hive-mcp-probe.mjs +94 -0
- package/dsh/hive-mcp.example.yml +62 -0
- package/dsh/update-lingshu.bat +11 -0
- package/dsh/update-lingshu.ps1 +337 -0
- package/lib/hooks.d.ts +3 -0
- package/lib/hooks.js +17 -23
- package/lib/index.d.ts +4 -2
- package/lib/index.js +29 -6
- package/lib/lib/datapath.d.ts +76 -1
- package/lib/lib/datapath.js +199 -13
- package/lib/lib/mdcg_client.d.ts +6 -3
- package/lib/lib/mdcg_client.js +6 -5
- package/lib/lib/mutual.js +4 -4
- package/lib/lib/token_store.js +4 -5
- package/md_cg/audit.py +17 -2
- package/md_cg/autonomy.py +86 -15
- package/md_cg/backfill.py +36 -1
- package/md_cg/backfill_bigdomain.py +34 -0
- package/md_cg/bench6_arms.py +28 -1
- package/md_cg/bench6_common.py +10 -1
- package/md_cg/bench6_competitors.py +6 -1
- package/md_cg/bench_axis_domain.py +9 -1
- package/md_cg/bench_blind_comp.py +7 -1
- package/md_cg/bench_en_atoms_public.py +9 -0
- package/md_cg/bench_governance.py +348 -0
- package/md_cg/bench_lme_zh.py +16 -1
- package/md_cg/bench_locomo.py +2 -1
- package/md_cg/bench_locomo_zh.py +16 -1
- package/md_cg/bench_locomo_zh_public.py +4 -1
- package/md_cg/bench_longmem.py +2 -1
- package/md_cg/bench_membench.py +27 -1
- package/md_cg/bench_p0.py +4 -1
- package/md_cg/bench_progressive.py +13 -1
- package/md_cg/bench_role_views.py +238 -0
- package/md_cg/bench_task_ab.py +8 -1
- package/md_cg/bench_task_ab_llm.py +13 -1
- package/md_cg/bench_unified_en.py +6 -1
- package/md_cg/bench_zh_mad.py +20 -1
- package/md_cg/blindspot_tickets.py +123 -0
- package/md_cg/branches.py +12 -1
- package/md_cg/build_postings.py +73 -0
- package/md_cg/ccgc.py +67 -2
- package/md_cg/census.py +5 -1
- package/md_cg/chain.py +24 -3
- package/md_cg/codeindex.py +134 -17
- package/md_cg/coldverify.py +265 -0
- package/md_cg/comment_gate.py +338 -0
- package/md_cg/cond_compose.py +190 -0
- package/md_cg/cond_facts.py +155 -0
- package/md_cg/cond_template.json +107 -0
- package/md_cg/condition_anchor.py +143 -0
- package/md_cg/conformance.py +69 -4
- package/md_cg/consistency.py +24 -1
- package/md_cg/consolidate.py +53 -2
- package/md_cg/corpus.py +4 -0
- package/md_cg/crosscheck.py +42 -2
- package/md_cg/crypto.py +35 -1
- package/md_cg/d_meta.py +310 -0
- package/md_cg/datapath.py +201 -26
- package/md_cg/docindex.py +122 -1
- package/md_cg/eval_common.py +29 -1
- package/md_cg/evidence.py +27 -1
- package/md_cg/evolution.py +21 -1
- package/md_cg/export.py +11 -1
- package/md_cg/forgetting.py +23 -1
- package/md_cg/fsutil.py +18 -1
- package/md_cg/hotcache.py +214 -0
- package/md_cg/hyperedge.py +251 -0
- package/md_cg/identity.py +18 -1
- package/md_cg/insight.py +17 -1
- package/md_cg/lexicon/build_cedict_en_zh.py +9 -0
- package/md_cg/lexicon/build_standard_en.py +171 -168
- package/md_cg/lexicon/expand_en_zh.py +6 -0
- package/md_cg/lifecycle.py +12 -1
- package/md_cg/linkref.py +281 -0
- package/md_cg/links.py +29 -1
- package/md_cg/mcp_server.py +362 -43
- package/md_cg/md_whitebox.py +53 -1
- package/md_cg/mdcg.py +1003 -27
- package/md_cg/mdcos.py +558 -36
- package/md_cg/metacognition.py +37 -2
- package/md_cg/migrate.py +4 -0
- package/md_cg/migrate_aeis.py +221 -213
- package/md_cg/migrate_roleplay.py +8 -0
- package/md_cg/migrate_wisdom_graph.py +14 -1
- package/md_cg/mreview/__main__.py +3 -0
- package/md_cg/mreview/bundle.py +8 -0
- package/md_cg/mreview/candidates.py +9 -0
- package/md_cg/mreview/govern.py +21 -1
- package/md_cg/mreview/locate.py +34 -0
- package/md_cg/mreview/pipeline.py +29 -1
- package/md_cg/mreview/ruleset.py +16 -1
- package/md_cg/nodefile.py +233 -3
- package/md_cg/pooling.py +23 -1
- package/md_cg/postings.py +298 -0
- package/md_cg/predict.py +89 -9
- package/md_cg/progressive.py +3 -0
- package/md_cg/protect.py +14 -1
- package/md_cg/protocol.py +372 -0
- package/md_cg/provenance.py +262 -0
- package/md_cg/reach.py +453 -0
- package/md_cg/refindex.py +47 -2
- package/md_cg/refine.py +20 -1
- package/md_cg/roleviews.py +89 -0
- package/md_cg/routing.py +76 -0
- package/md_cg/scrub.py +63 -2
- package/md_cg/security.py +26 -1
- package/md_cg/self_state.py +64 -1
- package/md_cg/selfreport.py +151 -0
- package/md_cg/semantic/canonical.py +5 -0
- package/md_cg/semantic/en_normalizer.py +364 -355
- package/md_cg/semantic/zh_en_atoms.py +139 -136
- package/md_cg/signer.py +41 -1
- package/md_cg/sources.py +583 -547
- package/md_cg/statushdr.py +179 -0
- package/md_cg/stg.py +59 -18
- package/md_cg/subgraph.py +23 -0
- package/md_cg/sustain.py +56 -1
- package/md_cg/tasks.py +26 -2
- package/md_cg/test_autonomy.py +26 -0
- package/md_cg/test_bench_governance.py +102 -0
- package/md_cg/test_blindspot_tickets.py +166 -0
- package/md_cg/test_ccgc.py +10 -0
- package/md_cg/test_codeindex.py +338 -0
- package/md_cg/test_comment_gate.py +187 -0
- package/md_cg/test_cond_compose_anchors.py +76 -0
- package/md_cg/test_condition_anchor.py +82 -0
- package/md_cg/test_d_meta.py +412 -0
- package/md_cg/test_datapath_root.py +188 -0
- package/md_cg/test_gain_gate.py +47 -1
- package/md_cg/test_hot_cold.py +187 -0
- package/md_cg/test_hyperedge.py +245 -0
- package/md_cg/test_linkref.py +306 -0
- package/md_cg/test_md_access_parity.py +15 -3
- package/md_cg/test_mr_m1.py +108 -18
- package/md_cg/test_mr_m3.py +8 -1
- package/md_cg/test_p26_refindex.py +49 -20
- package/md_cg/test_p27_docindex.py +236 -2
- package/md_cg/test_p2_mcp.py +1 -1
- package/md_cg/test_p31_insight.py +24 -0
- package/md_cg/test_p44_md_whitebox.py +14 -1
- package/md_cg/test_protocol.py +243 -0
- package/md_cg/test_reach.py +378 -0
- package/md_cg/test_reach_keys.py +201 -0
- package/md_cg/test_reach_meta_exits.py +145 -0
- package/md_cg/test_read_clip.py +8 -4
- package/md_cg/test_retr_s1.py +340 -0
- package/md_cg/test_retr_s1b.py +209 -0
- package/md_cg/test_retr_s3.py +194 -0
- package/md_cg/test_retr_s4.py +163 -0
- package/md_cg/test_retr_s5.py +200 -0
- package/md_cg/test_retr_s6.py +157 -0
- package/md_cg/test_retr_s7.py +385 -0
- package/md_cg/test_retr_s8_time.py +316 -0
- package/md_cg/test_retr_s9_edges.py +286 -0
- package/md_cg/test_retr_s9_entity_ctx.py +175 -0
- package/md_cg/test_review_conformance.py +59 -2
- package/md_cg/test_role_views.py +354 -0
- package/md_cg/test_trust.py +361 -0
- package/md_cg/test_units_poll.py +71 -0
- package/md_cg/test_v14_fixes.py +397 -0
- package/md_cg/test_validity_filter.py +280 -0
- package/md_cg/test_wisdom_md_store.py +7 -3
- package/md_cg/test_writepipe.py +5 -1
- package/md_cg/theory.py +16 -1
- package/md_cg/tokens.py +40 -8
- package/md_cg/tool_face.py +13 -2
- package/md_cg/trust.py +943 -0
- package/md_cg/twophase.py +12 -1
- package/md_cg/units.py +129 -10
- package/md_cg/vision_evidence.py +24 -1
- package/md_cg/weights.py +24 -1
- package/md_cg/whitebox.py +32 -1
- package/md_cg/whitebox_kb/data/verify_cache.json +21210 -365
- package/md_cg/whitebox_kb/data/verify_savings.jsonl +5078 -0
- package/md_cg/whitebox_kb/wisdom/audit_log/chain_heat.json +10 -10
- package/md_cg/whitebox_kb/wisdom/code_compose.py +113 -6
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +1 -1
- package/md_cg/whitebox_kb/wisdom/verifier.py +340 -55
- package/md_cg/whitebox_kb/wisdom/wisdom-book-cloud.db +0 -0
- package/md_cg/writelimit.py +18 -4
- package/md_cg/writepipe.py +178 -7
- package/package.json +2 -2
- package/skills/skills/designer-perspective/scripts/__pycache__/designer.cpython-310.pyc +0 -0
- package/skills/skills/designer-perspective/scripts/designer.py +17 -1
- package/skills/skills/designer-perspective/tests/selftest.py +3 -1
- package/src/hooks.ts +17 -21
- package/src/index.ts +33 -6
- package/src/lib/datapath.ts +211 -13
- package/src/lib/mdcg_client.ts +12 -8
- package/src/lib/mutual.ts +411 -411
- package/src/lib/token_store.ts +4 -5
- package/zcode/AGENTS.md +196 -195
- package/zcode/README.md +4 -0
- package/md_cg/whitebox_kb/wisdom/wisdom-book-cloud.db-shm +0 -0
- package/md_cg/whitebox_kb/wisdom/wisdom-book-cloud.db-wal +0 -0
package/md_cg/codeindex.py
CHANGED
|
@@ -31,14 +31,37 @@ MAX_DOC = 400
|
|
|
31
31
|
LANG_COMPILER = "compiler"
|
|
32
32
|
LANG_WEAK = "other"
|
|
33
33
|
|
|
34
|
+
# --------------------------------------------------------------------------
|
|
35
|
+
# 渲染契约代际(版本戳)
|
|
36
|
+
# --------------------------------------------------------------------------
|
|
37
|
+
# 生效条件:`render` 的**产物形态**发生不兼容变化时 +1(形态不变的重构不 +1);
|
|
38
|
+
# 由 refindex 写入 frontmatter.code_ref.render_version(节点侧),由 selfreport
|
|
39
|
+
# 随常驻进程自报(进程侧),两侧同取本常量——**同源,无第二处硬编码**。
|
|
40
|
+
#
|
|
41
|
+
# 用途(第③道防线,防「旧契约静默覆盖重建成果」):
|
|
42
|
+
# ① 节点侧:`scripts/mdcg_verify_render_meta.py` 据 code_ref.render_version 机械
|
|
43
|
+
# 判定「节点由哪一代 render 产出」,不再只靠形态启发式(正文含元条件行);
|
|
44
|
+
# ② 进程侧:`scripts/mdcg_stale_servers.py` 据自报值判定活进程代际,堵住
|
|
45
|
+
# 「进程启动时间晚于源码 mtime 故 stale=False、却持旧 render」的盲区
|
|
46
|
+
# (AGENTS.md §5 运维注记的活体实证:旧契约把全量重建成果刷回 old_synth)。
|
|
47
|
+
#
|
|
48
|
+
# 代际史:
|
|
49
|
+
# 1 = 旧契约(npm 0.4.8 及以前):合成区产出 `# 生效条件:载体/位置:…`,
|
|
50
|
+
# 索引元条件冒用 CCG 字段名(合成即冒充)。
|
|
51
|
+
# 2 = 三分区契约(2026-09-19):源码 CCG 区置首 → 合成 CCG 区(不产出生效
|
|
52
|
+
# 条件行)→ 索引元信息区(`# 索引元条件:…` 非 CCG 字段名 + 位置行)。
|
|
53
|
+
RENDER_VERSION = 2
|
|
54
|
+
|
|
34
55
|
|
|
35
56
|
# --------------------------------------------------------------------------
|
|
36
57
|
# Python:AST 提取(精确)
|
|
37
58
|
# --------------------------------------------------------------------------
|
|
59
|
+
# 生效条件:node 传入后,取 ast.get_docstring(node, clean=True) 的返回值,若该返回值为假值则回落空串,返回 strip 后截断到模块级 MAX_DOC 的文本;
|
|
38
60
|
def _doc_of(node):
|
|
39
61
|
return (ast.get_docstring(node, clean=True) or "").strip()[:MAX_DOC]
|
|
40
62
|
|
|
41
63
|
|
|
64
|
+
# 生效条件:lines 为源码行列表、lineno 为 1-based 定义行时,从 lines[lineno-2] 向上收集连续以 "#" 起始的行,遇空行且已收集到注释即停止,遇空行且未收集到注释则跳过继续,遇非注释非空行停止,返回按物理顺序排列的注释列表;lineno<=1 或初始无匹配时返回空列表;
|
|
42
65
|
def _leading_comments(lines, lineno):
|
|
43
66
|
"""定义行前的连续注释(# ...)。"""
|
|
44
67
|
out, i = [], lineno - 2
|
|
@@ -56,6 +79,55 @@ def _leading_comments(lines, lineno):
|
|
|
56
79
|
return list(reversed(out))
|
|
57
80
|
|
|
58
81
|
|
|
82
|
+
# 生效条件:node 具有真值 body 属性时返回 body[0].lineno;否则(body 为 None/假值/缺属性)返回 node.lineno;
|
|
83
|
+
def _body_first_line(node):
|
|
84
|
+
"""符号体首个语句的行号(1-based);无体 → 定义行本身(窗口为空)。"""
|
|
85
|
+
body = getattr(node, "body", None) or []
|
|
86
|
+
return body[0].lineno if body else node.lineno
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
# 生效条件:lines 为 source.split("\n") 得到的行列表、lineno 为 1-based 定义行、body_lineno 为 1-based 体首语句行时,在 end=max(lineno, body_lineno-1) 下扫描 lines[lineno:end],收集 strip 后以 "#" 起始的行并返回;body_lineno-1 <= lineno 时返回空列表;
|
|
90
|
+
def _body_comments(lines, lineno, body_lineno):
|
|
91
|
+
"""符号**体内首个语句之前**的连续 `#` 注释(定义行紧下方,声明头区)。
|
|
92
|
+
|
|
93
|
+
这是与 `_leading_comments`(定义行**之上**)并列的**第二个窗口**:
|
|
94
|
+
|
|
95
|
+
· 既有白箱单元库 104+ 处把 CCG 注释块写在**这里**
|
|
96
|
+
(例:`md_cg/whitebox_kb/wisdom/python_code_units.py` 的模板
|
|
97
|
+
`def tokenize(src):` 下一行即 ` # 生效条件:参数 src 合法`);
|
|
98
|
+
· 本函数是 body 窗口的**唯一真源**——`whitebox_kb/wisdom/verifier._ccg_block`
|
|
99
|
+
委托此处(历史文档里那句「与 `codeindex._body_comments` 同款语义」曾是
|
|
100
|
+
**悬空引用**:该名当时并不存在)。
|
|
101
|
+
|
|
102
|
+
参数:`lines`=源码行列表(`source.split("\\n")`);`lineno`=定义行(1-based);
|
|
103
|
+
`body_lineno`=体首个语句行(1-based)。窗口 = `lines[lineno : body_lineno-1]`
|
|
104
|
+
(0-based 切片:定义行之后 → 体首语句之前),只收 `#` 起始行。
|
|
105
|
+
语法上该窗口**结构性地只可能含注释/空行/docstring**——体首语句之前的区域。
|
|
106
|
+
"""
|
|
107
|
+
start = lineno # 0-based 索引 → 定义行的下一行
|
|
108
|
+
end = max(start, body_lineno - 1)
|
|
109
|
+
out = []
|
|
110
|
+
for ln in lines[start:end]:
|
|
111
|
+
s = ln.strip()
|
|
112
|
+
if s.startswith("#"):
|
|
113
|
+
out.append(s)
|
|
114
|
+
return out
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# 生效条件:lines 为源码行列表且 node 含 lineno 时,返回 _leading_comments(lines, node.lineno) 与 _body_comments(lines, node.lineno, _body_first_line(node)) 的拼接结果(leading 在前、body 在后);
|
|
118
|
+
def _symbol_comments(lines, node):
|
|
119
|
+
"""符号的「源码 CCG 区」= leading 窗口 + body 窗口,**按物理行序**合并。
|
|
120
|
+
|
|
121
|
+
不发明额外优先级:两个窗口在源文件里的物理先后天然确定(leading 在定义行
|
|
122
|
+
之上、body 在其下),而检索侧 `mdcos._ccg_field` 取**首个**匹配——于是
|
|
123
|
+
「靠前者胜出」与「物理序」是同一件事,确定性可复算。
|
|
124
|
+
单窗口文件的行为与改造前逐字一致(只多收 body 窗口)。
|
|
125
|
+
"""
|
|
126
|
+
return (_leading_comments(lines, node.lineno)
|
|
127
|
+
+ _body_comments(lines, node.lineno, _body_first_line(node)))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# 生效条件:source 与 node 传入后,取 ast.get_source_segment(source, node) 的返回值,若抛 ValueError/TypeError 或返回假值则 seg 为空串,返回 seg.split("\n",1)[0].strip()[:200];
|
|
59
131
|
def _sig(source, node):
|
|
60
132
|
try:
|
|
61
133
|
seg = ast.get_source_segment(source, node) or ""
|
|
@@ -64,6 +136,7 @@ def _sig(source, node):
|
|
|
64
136
|
return seg.split("\n", 1)[0].strip()[:200]
|
|
65
137
|
|
|
66
138
|
|
|
139
|
+
# 生效条件:tree 为 AST 根节点时,调用嵌套 rec(tree, "") 按 ast.iter_child_nodes 源码顺序递归产出 (定义节点, 所属类名);ClassDef 自身以当前 parent 产出并对其内部递归改用类名,FunctionDef/AsyncFunctionDef 以当前 parent 产出并保持 parent,其他节点递归保持 parent;
|
|
67
140
|
def _walk_defs(tree):
|
|
68
141
|
"""按**源码顺序**产出 (定义节点, 所属类名)。
|
|
69
142
|
|
|
@@ -71,6 +144,7 @@ def _walk_defs(tree):
|
|
|
71
144
|
父级归属是「子功能」与「不适用条件」两项的判定依据(同名方法必须能区分
|
|
72
145
|
是哪个类的),不能省。
|
|
73
146
|
"""
|
|
147
|
+
# 生效条件:node 为 AST 节点、parent 为当前所属类名字符串时,按 ast.iter_child_nodes(node) 顺序递归产出 (定义节点, 所属类名):ClassDef 以 parent 产出并递归改用 child.name,FunctionDef/AsyncFunctionDef 以 parent 产出并递归保持 parent,其他节点递归保持 parent;
|
|
74
148
|
def rec(node, parent):
|
|
75
149
|
for child in ast.iter_child_nodes(node):
|
|
76
150
|
if isinstance(child, ast.ClassDef):
|
|
@@ -84,6 +158,7 @@ def _walk_defs(tree):
|
|
|
84
158
|
return rec(tree, "")
|
|
85
159
|
|
|
86
160
|
|
|
161
|
+
# 生效条件:source 可被 ast.parse 成功解析时,返回首项为 module 条目(path、name=os.path.basename(path) or "<module>"、lineno=1、end=len(source.split("\n"))、doc=_doc_of(tree))后接 _walk_defs(tree) 各定义条目的列表;source 触发 SyntaxError 时抛 ValueError(f"{path}:{exc.lineno}: {exc.msg}");
|
|
87
162
|
def _extract_python(source, path):
|
|
88
163
|
try:
|
|
89
164
|
tree = ast.parse(source)
|
|
@@ -101,7 +176,7 @@ def _extract_python(source, path):
|
|
|
101
176
|
"path": path, "name": node.name, "kind": kind, "parent": parent,
|
|
102
177
|
"lineno": node.lineno, "end": getattr(node, "end_lineno", node.lineno),
|
|
103
178
|
"sig": _sig(source, node), "doc": _doc_of(node),
|
|
104
|
-
"comments":
|
|
179
|
+
"comments": _symbol_comments(lines, node)})
|
|
105
180
|
return items
|
|
106
181
|
|
|
107
182
|
|
|
@@ -118,6 +193,7 @@ _JS_DEF = re.compile(
|
|
|
118
193
|
_JS_MODULE_SCOPE_ONLY = ("const", "type", "enum")
|
|
119
194
|
|
|
120
195
|
|
|
196
|
+
# 生效条件:lines 为源码行列表、lineno 为 1-based 定义行时,从 lines[lineno-2] 向上收集连续以 "//" 开头的行注释,或遇到 strip 后以 "*/" 结尾的行时向上收集到首个 strip 后以 "/*" 开头的行(含该行)作为块注释;lookback 默认 25 限制收集行数,lookback=0 时循环不进入并返回空列表;遇空行且已有收集即停止,空行且未收集则跳过,其他行停止;
|
|
121
197
|
def _leading_js_comments(lines, lineno, lookback=25):
|
|
122
198
|
"""定义行前的连续行注释块,或紧邻的 /** ... */ 块。"""
|
|
123
199
|
out, i = [], lineno - 2
|
|
@@ -146,6 +222,7 @@ def _leading_js_comments(lines, lineno, lookback=25):
|
|
|
146
222
|
return list(reversed(out))
|
|
147
223
|
|
|
148
224
|
|
|
225
|
+
# 生效条件:对 source 用 _JS_DEF.finditer 命中项生成条目(首项模块条目 name 为 os.path.basename(path) or "<module>"),跳过 kind 属于 _JS_MODULE_SCOPE_ONLY 且 indent 组非空的命中;其余命中按出现顺序取 lineno、kind、name、sig,end 为下一命中行号减一或 len(source.split("\n")) 且不小于 lineno,comments 由 _leading_js_comments(lines, lineno) 生成。
|
|
149
226
|
def _extract_weak(source, path):
|
|
150
227
|
"""正则弱提取:返回条目,`end` 为**上界**(到下一个定义之前),不保证精确。"""
|
|
151
228
|
lines = source.split("\n")
|
|
@@ -182,6 +259,7 @@ EXTRACTORS = {
|
|
|
182
259
|
SUFFIX = tuple(sorted(EXTRACTORS))
|
|
183
260
|
|
|
184
261
|
|
|
262
|
+
# 生效条件:lines 为行列表、lineno 与 end 为 1-based 行号时,对 "\n".join(lines[max(0, lineno-1):max(0, end)]) 的 UTF-8 字节求 sha1,返回其十六进制前 12 位;
|
|
185
263
|
def region_hash(lines, lineno, end):
|
|
186
264
|
"""被引用行的 sha1 前 12 位(变更探测用,非内容寻址)。
|
|
187
265
|
|
|
@@ -192,6 +270,7 @@ def region_hash(lines, lineno, end):
|
|
|
192
270
|
return hashlib.sha1(seg.encode("utf-8")).hexdigest()[:12]
|
|
193
271
|
|
|
194
272
|
|
|
273
|
+
# 生效条件:source 与 path 传入后,ext=suffix or os.path.splitext(path)[1].lower();当 ext 存在于模块级 EXTRACTORS 时,用对应 fn(source, path) 抽取并给每个条目补 lang/precise/basis/hash(hash 由 region_hash(lines, it["lineno"], it["end"]) 算)后返回;ext 不在 EXTRACTORS 时抛 ValueError(f"无提取器(suffix={ext or '<none>'})");
|
|
195
274
|
def extract(source, path="", suffix=None):
|
|
196
275
|
"""抽取一个文件的条目;按后缀分派提取器。语法错误抛 ValueError。
|
|
197
276
|
|
|
@@ -213,15 +292,18 @@ def extract(source, path="", suffix=None):
|
|
|
213
292
|
return items
|
|
214
293
|
|
|
215
294
|
|
|
295
|
+
# 生效条件:item 为条目字典时,path=item.get("path") or ""、top=path.split("/")[0] or ".",按 item.get("precise", True)(缺键默认 True,键存在假值走弱提取)选择 LANG_COMPILER 或 LANG_WEAK 方法文本,返回 observation_position 用 top、time_window 用 nodefile.FULL_TIME_WINDOW_MIN 与 nodefile.FULL_TIME_WINDOW_MAX、observation_tool 用方法文本、existence_constraint 含 path 的四槽字典;
|
|
216
296
|
def condition_space(item):
|
|
217
297
|
"""条目 → 条件空间四槽(纯函数,**唯一来源**)。
|
|
218
298
|
|
|
219
|
-
为什么必须与 `render` 同源:正文的 `#
|
|
299
|
+
为什么必须与 `render` 同源:正文的 `# 索引元条件:` 行与 frontmatter 的
|
|
220
300
|
`condition_space` 一旦各写一套,就会出现「正文有声明、条件空间是空的」
|
|
221
301
|
——`nodefile.condition_space_text(require_full=True)` 只看 frontmatter,
|
|
222
302
|
于是节点**存得进、判得了,条件空间却没声明**。改造前正是这样:正文写
|
|
223
303
|
「大域=X;检索…时」(第三种方言),frontmatter 只写 `observation_position`
|
|
224
304
|
**单槽**。单槽不是生效条件(见 nodefile.CONDITION_SLOTS_REQUIRED),
|
|
305
|
+
该四槽在正文里由「索引元条件」行承载(**不再**占用「生效条件」字段——
|
|
306
|
+
Phase 0 契约裁决,见 nodefile.INDEX_META_MARK),
|
|
225
307
|
故本函数按四槽齐备产出,供 `render` 与 `refindex.add_items` 共用。
|
|
226
308
|
|
|
227
309
|
时间槽用**全时窗哨兵**而非 `mdcg.add` 缺省补的「写入时刻锚定 1 小时窗」:
|
|
@@ -244,8 +326,9 @@ def condition_space(item):
|
|
|
244
326
|
}
|
|
245
327
|
|
|
246
328
|
|
|
329
|
+
# 生效条件:item 为含 "name"、"kind"、"path"、"lineno"、"end" 键的条目字典时(缺这些必需键会 KeyError),返回由 item.get("comments") 的源码 CCG 区、合成 CCG 区、索引元信息区依次拼接的正文;parent/doc/comments/sig 按 item.get 缺键或假值回落,precise 缺键默认 True、键存在假值走弱提取,item["lineno"]/item["end"] 用于 basis 与位置行;
|
|
247
330
|
def render(item):
|
|
248
|
-
"""条目 → CCG
|
|
331
|
+
"""条目 → 正文三分区:源码 CCG 区(人工优先)→ 合成 CCG 区 → 索引元信息区。
|
|
249
332
|
|
|
250
333
|
**必须渲染成 CCG 格式**,这是本模块最容易踩的坑:
|
|
251
334
|
`judge_qualification` 第一步就查 `ccg_completeness` 的 5 要素
|
|
@@ -255,6 +338,16 @@ def render(item):
|
|
|
255
338
|
`# path::name` / `# sig` / `# doc:` 这类非 CCG 行,于是**所有代码节点
|
|
256
339
|
恒定 BLINDSPOT**:存得进、判不了、检索不到(与「目标节点的 CCG 渲染」
|
|
257
340
|
是同一策略,见 mdcg.py 的对应注释)。
|
|
341
|
+
|
|
342
|
+
Phase 0 修复(行序压制 + 字段语义分家;契约见
|
|
343
|
+
docs/mdcg/代码评审与条件化注释_契约_v0.1.md):
|
|
344
|
+
① **源码 CCG 区置首**——mdcos._ccg_field 取**首个**匹配,置首即
|
|
345
|
+
「人工优先」的确定性序:人工声明的生效条件不再被合成行压制;
|
|
346
|
+
② 合成区**不再产出生效条件行**——索引元条件不是功能前置条件
|
|
347
|
+
(裁定见 nodefile.INDEX_META_MARK)。故源码未声明的条目会**诚实地
|
|
348
|
+
缺该要素(BLINDSPOT)**,而不是被元条件冒充成 DEFER;
|
|
349
|
+
③ 索引元信息区改用非 CCG 字段名(索引元条件行 + 位置行),两个语义
|
|
350
|
+
不再挤同一个字段名(可机械判:nodefile.is_ccg_mark)。
|
|
258
351
|
"""
|
|
259
352
|
name = item["name"]
|
|
260
353
|
kind = item["kind"]
|
|
@@ -270,27 +363,37 @@ def render(item):
|
|
|
270
363
|
else:
|
|
271
364
|
basis = (f"{LANG_WEAK}(正则弱提取,未过编译器;区间为**上界**,"
|
|
272
365
|
f"以 op=ref 回读为准:{path} L{item['lineno']}-L{item['end']})")
|
|
273
|
-
|
|
366
|
+
# ---- 三分区组装(顺序即语义,不许随手改)------------------------------
|
|
367
|
+
# ① 源码 CCG 区:人工/源码声明逐字保留,**置首**取得「首个匹配」优先权
|
|
368
|
+
# ② 合成 CCG 区:机械补齐 5 要素,保证 ccg_completeness 不因缺行整体失效
|
|
369
|
+
# ③ 索引元信息区:非 CCG 字段名 + 位置行(与 CCG_MARKS 零重名)
|
|
370
|
+
lines = ["# " + c for c in comments]
|
|
371
|
+
lines += [
|
|
274
372
|
f"# 功能名:{name}({kind})",
|
|
275
|
-
f"# 生效条件:{nodefile.condition_space_text(condition_space(item))}",
|
|
276
373
|
f"# 子功能:{parent + '.' if parent else ''}{sub[:MAX_DOC]}",
|
|
277
374
|
f"# 执行:{sig}",
|
|
278
375
|
f"# 验证方式:{basis}",
|
|
279
376
|
"# 不适用条件:其它大域的**同名**符号(同名不同域时以 path 区分;"
|
|
280
377
|
f"本条目属于 {path})",
|
|
378
|
+
# 索引元条件**不占用**生效条件字段:它是「条目在何处/何时可被观测」,
|
|
379
|
+
# 不是「这段代码在何种输入下正确」。合成即冒充(nodefile.INDEX_META_MARK)。
|
|
380
|
+
f"# {nodefile.INDEX_META_MARK}:"
|
|
381
|
+
+ nodefile.condition_space_text(condition_space(item),
|
|
382
|
+
require_full=False),
|
|
281
383
|
f"# 位置:{path}:{item['lineno']}-{item['end']}"
|
|
282
384
|
f"({item.get('lang')},precise={bool(item.get('precise', True))})",
|
|
283
385
|
]
|
|
284
|
-
lines.extend("# " + c for c in comments)
|
|
285
386
|
return "\n".join(lines)
|
|
286
387
|
|
|
287
388
|
|
|
389
|
+
# 生效条件:item 为含 "path" 与 "name" 键的字典时(缺任一键会 KeyError),返回 "code_" 加 (item["path"] + "::" + item["name"]).encode("utf-8") 的 sha1 十六进制前 12 位;
|
|
288
390
|
def node_id(item):
|
|
289
391
|
"""稳定 id:path::name 的短哈希(重复索引幂等)。"""
|
|
290
392
|
key = (item["path"] + "::" + item["name"]).encode("utf-8")
|
|
291
393
|
return "code_" + hashlib.sha1(key).hexdigest()[:12]
|
|
292
394
|
|
|
293
395
|
|
|
396
|
+
# 生效条件:skip_dirs 传入后,遍历 (skip_dirs or ()) 把每项 str(raw).strip().replace("\\","/").strip("/"),空串跳过;含 "/" 的加入 paths,不含 "/" 的加入 names;若归一化后无规则返回 (None, []),否则返回 (hit, rules),其中 hit(rel_dir, base) 在 base 命中 names 或 rel_dir 等于/前缀匹配 paths 中某条加 "/" 时为 True;
|
|
294
397
|
def skip_matcher(skip_dirs):
|
|
295
398
|
"""把调用方的 `skip_dirs` 编译成「该子目录是否排除」的判定 `hit(rel_dir, base)`。
|
|
296
399
|
|
|
@@ -320,6 +423,7 @@ def skip_matcher(skip_dirs):
|
|
|
320
423
|
if not rules:
|
|
321
424
|
return None, []
|
|
322
425
|
|
|
426
|
+
# 生效条件:在 skip_matcher 返回的闭包中,rel_dir 与 base 传入后,若 base 命中由 skip_dirs 归一化出的不含 "/" 的目录名集合 names 则返回 True;否则若 rel_dir 等于或以其某个含 "/" 的路径规则 paths 加 "/" 为前缀则返回 True;两者都不满足返回 False;
|
|
323
427
|
def hit(rel_dir, base):
|
|
324
428
|
if base in names:
|
|
325
429
|
return True
|
|
@@ -328,6 +432,7 @@ def skip_matcher(skip_dirs):
|
|
|
328
432
|
return hit, rules
|
|
329
433
|
|
|
330
434
|
|
|
435
|
+
# 生效条件:当 root 为可 os.walk 的目录时返回 (items, errors, stats);patterns 为 None 时按模块常量 SUFFIX 取后缀,命中 max_files 或 max_items 上限则在 stats['truncated'] 上报截断,fresh 为 None 时逐文件读盘。
|
|
331
436
|
def index_dir(root, patterns=None, max_files=500, max_items=2000,
|
|
332
437
|
fresh=None, on_file=None, skip_dirs=None):
|
|
333
438
|
"""按大域(目录)遍历代码,产出 `(items, errors, stats)`。零 LLM。
|
|
@@ -350,11 +455,25 @@ def index_dir(root, patterns=None, max_files=500, max_items=2000,
|
|
|
350
455
|
pats = tuple(patterns or SUFFIX)
|
|
351
456
|
hit_skip, skip_rules = skip_matcher(skip_dirs)
|
|
352
457
|
items, errors, files = [], [], 0
|
|
458
|
+
hit_files = 0
|
|
353
459
|
seen_suffix = set()
|
|
354
460
|
stats = {"root": root, "patterns": list(pats), "files": 0, "truncated": False,
|
|
355
461
|
"truncated_reason": "", "max_files": max_files, "max_items": max_items,
|
|
356
462
|
"skipped_suffixes": [], "skipped_unchanged": 0,
|
|
357
|
-
"skip_dirs": list(skip_rules), "skipped_dirs": []
|
|
463
|
+
"skip_dirs": list(skip_rules), "skipped_dirs": [],
|
|
464
|
+
# 截断**可复算**:命中(后缀匹配)文件数与本轮已产出条目数。
|
|
465
|
+
# 与 truncated_reason 一起读,调用方才能核对「差多少」而不是只看到"被截断了"。
|
|
466
|
+
"hit_files": 0, "indexed_items": 0}
|
|
467
|
+
|
|
468
|
+
# 生效条件:无参闭包,只在本次扫库作用域内可调用;把 files / hit_files / indexed_items / skipped_suffixes / skipped_dirs 一次性落进 stats,三处 return 共用同一口径;
|
|
469
|
+
def _snap():
|
|
470
|
+
"""把「截断相关」计数一次性落进 stats(三处 return 共用,避免各处口径漂移)。"""
|
|
471
|
+
stats["files"] = files
|
|
472
|
+
stats["hit_files"] = hit_files
|
|
473
|
+
stats["indexed_items"] = len(items)
|
|
474
|
+
stats["skipped_suffixes"] = sorted(
|
|
475
|
+
s for s in seen_suffix if s and s not in pats)[:12]
|
|
476
|
+
|
|
358
477
|
for dirpath, dirnames, filenames in os.walk(root):
|
|
359
478
|
rel_dir = os.path.relpath(dirpath, root).replace("\\", "/")
|
|
360
479
|
if rel_dir == ".":
|
|
@@ -369,21 +488,22 @@ def index_dir(root, patterns=None, max_files=500, max_items=2000,
|
|
|
369
488
|
stats["skipped_dirs"].append(child)
|
|
370
489
|
continue
|
|
371
490
|
keep.append(d)
|
|
372
|
-
dirnames
|
|
491
|
+
# 目录序**必须确定性**:os.walk 给出的 dirnames 顺序由文件系统决定,
|
|
492
|
+
# 同一次输入两次运行可能不同 → 索引结果不可复算。排序后 walk 顺序唯一。
|
|
493
|
+
dirnames[:] = sorted(keep)
|
|
373
494
|
for fn in sorted(filenames):
|
|
374
495
|
ext = os.path.splitext(fn)[1].lower()
|
|
375
496
|
seen_suffix.add(ext)
|
|
376
497
|
if not fn.lower().endswith(pats):
|
|
377
498
|
continue
|
|
499
|
+
hit_files += 1
|
|
378
500
|
if files >= max_files or len(items) >= max_items:
|
|
379
501
|
stats["truncated"] = True
|
|
380
502
|
stats["truncated_reason"] = (
|
|
381
503
|
f"files={files}>=max_files={max_files}"
|
|
382
504
|
if files >= max_files else
|
|
383
505
|
f"items={len(items)}>=max_items={max_items}")
|
|
384
|
-
|
|
385
|
-
stats["skipped_suffixes"] = sorted(
|
|
386
|
-
s for s in seen_suffix if s and s not in pats)[:12]
|
|
506
|
+
_snap()
|
|
387
507
|
return items, errors, stats
|
|
388
508
|
files += 1
|
|
389
509
|
fp = os.path.join(dirpath, fn)
|
|
@@ -404,12 +524,9 @@ def index_dir(root, patterns=None, max_files=500, max_items=2000,
|
|
|
404
524
|
stats["truncated"] = True
|
|
405
525
|
stats["truncated_reason"] = (
|
|
406
526
|
f"items={len(items)}>=max_items={max_items}")
|
|
407
|
-
|
|
408
|
-
stats["skipped_suffixes"] = sorted(
|
|
409
|
-
s for s in seen_suffix if s and s not in pats)[:12]
|
|
527
|
+
_snap()
|
|
410
528
|
return items, errors, stats
|
|
411
529
|
except (OSError, UnicodeDecodeError, ValueError) as exc:
|
|
412
530
|
errors.append(f"{rel}: {exc}")
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
return items, errors, stats
|
|
531
|
+
_snap()
|
|
532
|
+
return items, errors, stats
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""md_cg · 冷路径异步深度验证队列
|
|
3
|
+
|
|
4
|
+
【为什么】写入路径中的一跳同步传播(_after_trust → mark_dependents)
|
|
5
|
+
只做「标记存疑」——它不验证下游是否真的被影响。真正的深度验证
|
|
6
|
+
需要:(1) 重跑 judge_qualification 看条件是否仍命中;
|
|
7
|
+
(2) 检查依赖链上的验证态是否级联失效。
|
|
8
|
+
|
|
9
|
+
这些操作可重可慢,不该阻塞写入路径。故异步入队,后台消费。
|
|
10
|
+
|
|
11
|
+
【设计】
|
|
12
|
+
- enqueue(node_id, action, **kw):入队异步验证任务
|
|
13
|
+
- 后台线程消费队列,调 trust.set_state + judge_ranking
|
|
14
|
+
- 队列落盘 _coldverify.jsonl(崩溃恢复)
|
|
15
|
+
- 默认不启动后台线程(opt-in:start_worker())
|
|
16
|
+
- 也可同步消费(drain(),用于测试)
|
|
17
|
+
|
|
18
|
+
【边界】
|
|
19
|
+
- 不阻塞写入路径(_after_trust 只 enqueue 不等结果)
|
|
20
|
+
- 后台线程异常只记日志不抛(永不拖垮主进程)
|
|
21
|
+
- 队列文件可选(无文件时纯内存,适合测试)
|
|
22
|
+
|
|
23
|
+
零第三方依赖(D-005)。
|
|
24
|
+
"""
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import json
|
|
28
|
+
import os
|
|
29
|
+
import threading
|
|
30
|
+
import time
|
|
31
|
+
from collections import deque
|
|
32
|
+
|
|
33
|
+
#: 队列文件名
|
|
34
|
+
QUEUE_FILE = "_coldverify.jsonl"
|
|
35
|
+
#: 后台线程轮询间隔(秒)
|
|
36
|
+
POLL_INTERVAL = 2.0
|
|
37
|
+
#: 单次批处理上限
|
|
38
|
+
BATCH_LIMIT = 10
|
|
39
|
+
|
|
40
|
+
_VALID_ACTIONS = {"reverify", "propagate_depth", "patrol_check"}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class ColdVerifyQueue:
|
|
44
|
+
"""冷路径异步深度验证队列。
|
|
45
|
+
|
|
46
|
+
线程安全:enqueue/drain/status 可跨线程调用。
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def __init__(self, root=None):
|
|
50
|
+
self._root = root
|
|
51
|
+
self._queue: deque = deque()
|
|
52
|
+
self._lock = threading.Lock()
|
|
53
|
+
self._worker = None
|
|
54
|
+
self._stop = threading.Event()
|
|
55
|
+
self._stats = {"enqueued": 0, "processed": 0, "errors": 0,
|
|
56
|
+
"skipped": 0}
|
|
57
|
+
self._queue_path = (os.path.join(root, QUEUE_FILE)
|
|
58
|
+
if root else None)
|
|
59
|
+
self._load_persisted()
|
|
60
|
+
|
|
61
|
+
def _load_persisted(self):
|
|
62
|
+
"""从队列文件恢复未处理任务(崩溃恢复)。"""
|
|
63
|
+
if not self._queue_path or not os.path.exists(self._queue_path):
|
|
64
|
+
return
|
|
65
|
+
try:
|
|
66
|
+
with open(self._queue_path, "r", encoding="utf-8") as f:
|
|
67
|
+
for line in f:
|
|
68
|
+
line = line.strip()
|
|
69
|
+
if not line:
|
|
70
|
+
continue
|
|
71
|
+
task = json.loads(line)
|
|
72
|
+
if not task.get("_done"):
|
|
73
|
+
self._queue.append(task)
|
|
74
|
+
except Exception: # noqa: BLE001
|
|
75
|
+
pass # 崩溃恢复容错:文件损坏就不恢复
|
|
76
|
+
|
|
77
|
+
def _persist(self):
|
|
78
|
+
"""持久化当前队列(全量覆写)。"""
|
|
79
|
+
if not self._queue_path:
|
|
80
|
+
return
|
|
81
|
+
try:
|
|
82
|
+
with open(self._queue_path, "w", encoding="utf-8") as f:
|
|
83
|
+
for task in self._queue:
|
|
84
|
+
f.write(json.dumps(task, ensure_ascii=False) + "\n")
|
|
85
|
+
except Exception: # noqa: BLE001
|
|
86
|
+
pass # 持久化失败不影响内存队列
|
|
87
|
+
|
|
88
|
+
def enqueue(self, node_id: str, action: str = "reverify", **kw):
|
|
89
|
+
"""入队异步验证任务。返回 task dict。"""
|
|
90
|
+
if action not in _VALID_ACTIONS:
|
|
91
|
+
raise ValueError(f"未知 action: {action},合法: {_VALID_ACTIONS}")
|
|
92
|
+
task = {
|
|
93
|
+
"node_id": node_id,
|
|
94
|
+
"action": action,
|
|
95
|
+
"enqueued_at": time.time(),
|
|
96
|
+
"kwargs": kw,
|
|
97
|
+
}
|
|
98
|
+
with self._lock:
|
|
99
|
+
self._queue.append(task)
|
|
100
|
+
self._stats["enqueued"] += 1
|
|
101
|
+
self._persist()
|
|
102
|
+
return task
|
|
103
|
+
|
|
104
|
+
def drain(self, cg, *, limit=BATCH_LIMIT):
|
|
105
|
+
"""同步消费队列(用于测试/手动处理)。返回处理结果列表。"""
|
|
106
|
+
results = []
|
|
107
|
+
with self._lock:
|
|
108
|
+
batch = []
|
|
109
|
+
for _ in range(min(limit, len(self._queue))):
|
|
110
|
+
batch.append(self._queue.popleft())
|
|
111
|
+
for task in batch:
|
|
112
|
+
try:
|
|
113
|
+
r = self._process(cg, task)
|
|
114
|
+
results.append(r)
|
|
115
|
+
self._stats["processed"] += 1
|
|
116
|
+
except Exception as exc: # noqa: BLE001
|
|
117
|
+
self._stats["errors"] += 1
|
|
118
|
+
results.append({"node_id": task["node_id"],
|
|
119
|
+
"action": task["action"],
|
|
120
|
+
"error": str(exc),
|
|
121
|
+
"ok": False})
|
|
122
|
+
with self._lock:
|
|
123
|
+
self._persist()
|
|
124
|
+
return results
|
|
125
|
+
|
|
126
|
+
def _process(self, cg, task):
|
|
127
|
+
"""执行单个验证任务。"""
|
|
128
|
+
from . import trust
|
|
129
|
+
nid = task["node_id"]
|
|
130
|
+
action = task["action"]
|
|
131
|
+
kw = task.get("kwargs", {})
|
|
132
|
+
|
|
133
|
+
if action == "reverify":
|
|
134
|
+
# 重新验证单节点:重跑 judge_qualification
|
|
135
|
+
node = cg.get(nid)
|
|
136
|
+
if not node:
|
|
137
|
+
self._stats["skipped"] += 1
|
|
138
|
+
return {"node_id": nid, "action": action, "ok": True,
|
|
139
|
+
"skipped": "node_not_found"}
|
|
140
|
+
fm = node.get("frontmatter") or {}
|
|
141
|
+
old_state = trust.state_of(fm)
|
|
142
|
+
# 只对非 unverified 节点做深度验证
|
|
143
|
+
if old_state == "unverified":
|
|
144
|
+
self._stats["skipped"] += 1
|
|
145
|
+
return {"node_id": nid, "action": action, "ok": True,
|
|
146
|
+
"skipped": "unverified_no_action"}
|
|
147
|
+
# 检查时效
|
|
148
|
+
kind, _, _ = trust.validity(fm)
|
|
149
|
+
want, why = None, ""
|
|
150
|
+
if kind == "expired" and old_state != "expired":
|
|
151
|
+
want, why = "expired", "冷路径:时效过期"
|
|
152
|
+
elif kind == "not_yet":
|
|
153
|
+
want, why = "unverified", "冷路径:尚未生效"
|
|
154
|
+
if want is None:
|
|
155
|
+
return {"node_id": nid, "action": action, "ok": True,
|
|
156
|
+
"old_state": old_state, "new_state": old_state,
|
|
157
|
+
"changed": False}
|
|
158
|
+
# 迁移结果**必须检查**(2026-09-20 v14 缺陷 D 修复):`trust.set_state`
|
|
159
|
+
# 对非法迁移返回 ok=False(负路由不抛异常),旧实现丢弃返回值后一律
|
|
160
|
+
# 上报 `ok=True + new_state=<请求值>` —— 恰好在本队列最该说话的两条
|
|
161
|
+
# 迁移上(doubted→expired / verified→unverified,均为 TRANSITIONS
|
|
162
|
+
# 明令拒绝的跳级)**说了假话**,且 stats.errors 恒 0 把它盖住。
|
|
163
|
+
# trust 层做对了(拒绝非法迁移),队列层不得把它翻译成成功。
|
|
164
|
+
r = trust.set_state(cg, nid, want, reason=why,
|
|
165
|
+
actor="coldverify", **kw)
|
|
166
|
+
if not r.get("ok"):
|
|
167
|
+
self._stats["errors"] += 1
|
|
168
|
+
return {"node_id": nid, "action": action, "ok": False,
|
|
169
|
+
"old_state": old_state, "requested_state": want,
|
|
170
|
+
"error": "transition_rejected:%s" % r.get("error"),
|
|
171
|
+
"reason": r.get("reason") or "",
|
|
172
|
+
"detail": "非法迁移被 trust.TRANSITIONS 拒绝,状态未变"}
|
|
173
|
+
return {"node_id": nid, "action": action, "ok": True,
|
|
174
|
+
"old_state": old_state, "new_state": want,
|
|
175
|
+
"changed": bool(r.get("changed"))}
|
|
176
|
+
|
|
177
|
+
elif action == "propagate_depth":
|
|
178
|
+
# 多跳深度传播
|
|
179
|
+
r = trust.propagate(cg, apply=True, actor="coldverify",
|
|
180
|
+
**kw)
|
|
181
|
+
return {"node_id": nid, "action": action, "ok": True,
|
|
182
|
+
"propagation": r}
|
|
183
|
+
|
|
184
|
+
elif action == "patrol_check":
|
|
185
|
+
# 巡检式检查
|
|
186
|
+
rep = trust.patrol(cg, **kw)
|
|
187
|
+
return {"node_id": nid, "action": action, "ok": True,
|
|
188
|
+
"patrol": rep}
|
|
189
|
+
|
|
190
|
+
return {"node_id": nid, "action": action, "ok": False,
|
|
191
|
+
"error": "unknown_action"}
|
|
192
|
+
|
|
193
|
+
def start_worker(self, cg, *, poll_interval=POLL_INTERVAL):
|
|
194
|
+
"""启动后台消费线程。"""
|
|
195
|
+
if self._worker and self._worker.is_alive():
|
|
196
|
+
return self._worker
|
|
197
|
+
self._stop.clear()
|
|
198
|
+
|
|
199
|
+
def _run():
|
|
200
|
+
while not self._stop.is_set():
|
|
201
|
+
try:
|
|
202
|
+
if self._queue:
|
|
203
|
+
self.drain(cg, limit=BATCH_LIMIT)
|
|
204
|
+
except Exception: # noqa: BLE001
|
|
205
|
+
self._stats["errors"] += 1
|
|
206
|
+
self._stop.wait(poll_interval)
|
|
207
|
+
|
|
208
|
+
self._worker = threading.Thread(target=_run, daemon=True,
|
|
209
|
+
name="coldverify-worker")
|
|
210
|
+
self._worker.start()
|
|
211
|
+
return self._worker
|
|
212
|
+
|
|
213
|
+
def stop_worker(self, timeout=5.0):
|
|
214
|
+
"""停止后台线程。"""
|
|
215
|
+
self._stop.set()
|
|
216
|
+
if self._worker and self._worker.is_alive():
|
|
217
|
+
self._worker.join(timeout=timeout)
|
|
218
|
+
self._worker = None
|
|
219
|
+
|
|
220
|
+
def status(self):
|
|
221
|
+
"""返回队列状态。"""
|
|
222
|
+
with self._lock:
|
|
223
|
+
return {
|
|
224
|
+
"queue_size": len(self._queue),
|
|
225
|
+
"stats": dict(self._stats),
|
|
226
|
+
"worker_alive": (self._worker is not None
|
|
227
|
+
and self._worker.is_alive()),
|
|
228
|
+
"queue_file": self._queue_path,
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
def clear(self):
|
|
232
|
+
"""清空队列(测试用)。"""
|
|
233
|
+
with self._lock:
|
|
234
|
+
self._queue.clear()
|
|
235
|
+
self._persist()
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
#: 进程级默认实例
|
|
239
|
+
_DEFAULT = None
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def default_queue(root=None):
|
|
243
|
+
"""进程级默认冷路径队列单例。"""
|
|
244
|
+
global _DEFAULT
|
|
245
|
+
if _DEFAULT is None:
|
|
246
|
+
_DEFAULT = ColdVerifyQueue(root=root)
|
|
247
|
+
return _DEFAULT
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def attach(cg):
|
|
251
|
+
"""给 cg 实例挂冷路径队列(幂等)。"""
|
|
252
|
+
if not hasattr(cg, "_coldverify") or cg._coldverify is None:
|
|
253
|
+
cg._coldverify = ColdVerifyQueue(root=cg.root)
|
|
254
|
+
return cg._coldverify
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def get(cg):
|
|
258
|
+
"""取 cg 实例的冷路径队列(未挂返回 None)。"""
|
|
259
|
+
return getattr(cg, "_coldverify", None)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def enqueue(cg, node_id: str, action: str = "reverify", **kw):
|
|
263
|
+
"""快捷入口:挂队列 + 入队。"""
|
|
264
|
+
q = attach(cg)
|
|
265
|
+
return q.enqueue(node_id, action, **kw)
|