@hupan56/wlkj 3.0.0 → 3.1.2
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/bin/cli.js +1435 -1373
- package/package.json +1 -1
- package/templates/qoder/commands/optional/wl-insight.md +6 -4
- package/templates/qoder/commands/optional/wl-report.md +20 -39
- package/templates/qoder/commands/optional/wl-status.md +4 -4
- package/templates/qoder/commands/wl-code.md +2 -2
- package/templates/qoder/commands/wl-design.md +5 -5
- package/templates/qoder/commands/wl-init.md +3 -3
- package/templates/qoder/commands/wl-prd.md +26 -13
- package/templates/qoder/commands/wl-req.md +3 -3
- package/templates/qoder/commands/wl-search.md +78 -47
- package/templates/qoder/commands/wl-task.md +343 -40
- package/templates/qoder/commands/wl-test.md +10 -10
- package/templates/qoder/config.yaml +35 -13
- package/templates/qoder/hooks/post-tool-use.py +181 -0
- package/templates/qoder/hooks/session-start.py +82 -1
- package/templates/qoder/hooks/stop-eval.py +207 -0
- package/templates/qoder/hooks/user-prompt-submit.py +139 -0
- package/templates/qoder/rules/wl-pipeline.md +70 -8
- package/templates/qoder/scripts/README.md +78 -115
- package/templates/qoder/scripts/capability/__init__.py +26 -0
- package/templates/qoder/scripts/capability/__main__.py +72 -0
- package/templates/qoder/scripts/capability/adapters/__init__.py +29 -0
- package/templates/qoder/scripts/capability/adapters/cli.py +316 -0
- package/templates/qoder/scripts/capability/adapters/mcp.py +334 -0
- package/templates/qoder/scripts/capability/adapters/qw.py +271 -0
- package/templates/qoder/scripts/capability/caps/__init__.py +27 -0
- package/templates/qoder/scripts/capability/caps/context.py +36 -0
- package/templates/qoder/scripts/capability/caps/cron.py +50 -0
- package/templates/qoder/scripts/capability/caps/identity.py +43 -0
- package/templates/qoder/scripts/capability/caps/memory.py +71 -0
- package/templates/qoder/scripts/capability/caps/notify.py +41 -0
- package/templates/qoder/scripts/capability/caps/present.py +48 -0
- package/templates/qoder/scripts/capability/caps/repo.py +31 -0
- package/templates/qoder/scripts/capability/caps/sandbox.py +43 -0
- package/templates/qoder/scripts/capability/chain.py +92 -0
- package/templates/qoder/scripts/capability/memory_chain.py +41 -0
- package/templates/qoder/scripts/capability/registry.py +246 -0
- package/templates/qoder/scripts/capability/registry_mcp.py +250 -0
- package/templates/qoder/scripts/capability/smoke_test.py +211 -0
- package/templates/qoder/scripts/capability/smoke_test_report.json +94 -0
- package/templates/qoder/scripts/{report → deployment/setup}/__init__.py +11 -11
- package/templates/qoder/scripts/{setup → deployment/setup}/carriers.py +33 -26
- package/templates/qoder/scripts/deployment/setup/carriers_verify.py +148 -0
- package/templates/qoder/scripts/{setup → deployment/setup}/init_doctor.py +40 -16
- package/templates/qoder/scripts/{setup → deployment/setup}/install_qoderwork.py +20 -12
- package/templates/qoder/scripts/{setup → deployment/setup}/platform_doctor.py +8 -2
- package/templates/qoder/scripts/{setup → deployment/setup}/repo_root.py +113 -112
- package/templates/qoder/scripts/{setup → deployment/setup}/setup.py +31 -11
- package/templates/qoder/scripts/{setup → deployment/setup}/setup_lanhu.py +13 -7
- package/templates/qoder/scripts/domain/__init__.py +0 -0
- package/templates/qoder/scripts/domain/design/__init__.py +0 -0
- package/templates/qoder/scripts/{report → domain/design}/fill_prototype.py +12 -6
- package/templates/qoder/scripts/{report → domain/design}/gen_design_doc.py +8 -2
- package/templates/qoder/scripts/{setup → domain/kg}/__init__.py +11 -11
- package/templates/qoder/scripts/domain/kg/build/__init__.py +0 -0
- package/templates/qoder/scripts/{kg → domain/kg/build}/build_entity_registry.py +7 -2
- package/templates/qoder/scripts/{kg → domain/kg/build}/build_relations.py +7 -2
- package/templates/qoder/scripts/{kg → domain/kg/build}/build_style_index.py +13 -7
- package/templates/qoder/scripts/{kg → domain/kg/build}/build_workflows.py +7 -2
- package/templates/qoder/scripts/{kg → domain/kg/build}/kg_build.py +76 -9
- package/templates/qoder/scripts/{kg → domain/kg/build}/kg_build_db.py +7 -2
- package/templates/qoder/scripts/{kg → domain/kg/build}/kg_incremental.py +11 -6
- package/templates/qoder/scripts/{report → domain/kg/build}/learn_aggregate.py +9 -4
- package/templates/qoder/scripts/domain/kg/extract/__init__.py +0 -0
- package/templates/qoder/scripts/{common → domain/kg/extract}/extract.py +430 -419
- package/templates/qoder/scripts/{common → domain/kg/extract}/test_extract.py +115 -115
- package/templates/qoder/scripts/{common → domain/kg/extract}/ts_extract.py +614 -536
- package/templates/qoder/scripts/domain/kg/graph/__init__.py +0 -0
- package/templates/qoder/scripts/{kg → domain/kg/graph}/kg_link_db.py +7 -2
- package/templates/qoder/scripts/domain/kg/graph/kg_semantic.py +683 -0
- package/templates/qoder/scripts/{kg → domain/kg}/kg.py +263 -30
- package/templates/qoder/scripts/{common → domain/kg}/kg_capabilities.py +182 -182
- package/templates/qoder/scripts/domain/kg/search/__init__.py +0 -0
- package/templates/qoder/scripts/{kg → domain/kg/search}/context_pack.py +30 -9
- package/templates/qoder/scripts/{kg → domain/kg/search}/enrich_prompt.py +10 -4
- package/templates/qoder/scripts/{kg → domain/kg/search}/prefetch.py +33 -8
- package/templates/qoder/scripts/{common → domain/kg/search}/search_engine.py +207 -205
- package/templates/qoder/scripts/{kg → domain/kg/search}/search_index.py +143 -77
- package/templates/qoder/scripts/domain/kg/server/__init__.py +0 -0
- package/templates/qoder/scripts/domain/kg/server/kgd.py +549 -0
- package/templates/qoder/scripts/domain/kg/server/perf_bench.py +197 -0
- package/templates/qoder/scripts/domain/kg/storage/__init__.py +0 -0
- package/templates/qoder/scripts/{kg → domain/kg/storage}/kg_duckdb.py +38 -14
- package/templates/qoder/scripts/domain/learning/__init__.py +0 -0
- package/templates/qoder/scripts/{report → domain/learning}/learn.py +8 -3
- package/templates/qoder/scripts/{mcp → domain/report}/__init__.py +11 -11
- package/templates/qoder/scripts/{report → domain/report}/add_session.py +9 -3
- package/templates/qoder/scripts/{report → domain/report}/export.py +10 -5
- package/templates/qoder/scripts/{report → domain/report}/report.py +9 -4
- package/templates/qoder/scripts/domain/report/report_snapshot.py +261 -0
- package/templates/qoder/scripts/{report → domain/report}/role.py +10 -4
- package/templates/qoder/scripts/{report → domain/report}/status.py +112 -9
- package/templates/qoder/scripts/domain/report/status_snapshot.py +191 -0
- package/templates/qoder/scripts/domain/requirement/__init__.py +0 -0
- package/templates/qoder/scripts/{report → domain/requirement}/archive_prd.py +8 -2
- package/templates/qoder/scripts/{report → domain/requirement}/req.py +228 -222
- package/templates/qoder/scripts/{kg → domain/task}/__init__.py +11 -11
- package/templates/qoder/scripts/{task → domain/task}/git_sync.py +40 -21
- package/templates/qoder/scripts/{task → domain/task}/syncgate.py +13 -8
- package/templates/qoder/scripts/{task → domain/task}/task.py +13 -5
- package/templates/qoder/scripts/{task → domain/task}/task_lifecycle.py +606 -596
- package/templates/qoder/scripts/{task → domain/task}/task_query.py +162 -161
- package/templates/qoder/scripts/{task → domain/task}/task_relations.py +425 -424
- package/templates/qoder/scripts/{task → domain/task}/team_sync.py +9 -4
- package/templates/qoder/scripts/domain/task/zentao_sync.py +370 -0
- package/templates/qoder/scripts/foundation/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation}/bootstrap.py +10 -10
- package/templates/qoder/scripts/foundation/core/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation/core}/cmd_registry.py +1 -1
- package/templates/qoder/scripts/{common → foundation/core}/config.py +1 -1
- package/templates/qoder/scripts/{common → foundation/core}/paths.py +706 -586
- package/templates/qoder/scripts/foundation/data/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation/data}/contract.py +2 -2
- package/templates/qoder/scripts/{common → foundation/data}/events.py +2 -2
- package/templates/qoder/scripts/{common → foundation/data}/result.py +1 -1
- package/templates/qoder/scripts/{common → foundation/data}/task_utils.py +427 -404
- package/templates/qoder/scripts/foundation/identity/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation/identity}/check_publish.py +103 -98
- package/templates/qoder/scripts/{common → foundation/identity}/developer.py +239 -239
- package/templates/qoder/scripts/{common → foundation/identity}/guard.py +2 -2
- package/templates/qoder/scripts/{common → foundation/identity}/identity.py +14 -10
- package/templates/qoder/scripts/{common → foundation/identity}/roles.py +1 -1
- package/templates/qoder/scripts/foundation/integrations/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation/integrations}/active_task.py +30 -15
- package/templates/qoder/scripts/{common → foundation/integrations}/feishu.py +2 -2
- package/templates/qoder/scripts/{common → foundation/integrations}/terms.py +3 -3
- package/templates/qoder/scripts/foundation/integrations/zentao_client.py +220 -0
- package/templates/qoder/scripts/foundation/io/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation/io}/filelock.py +1 -1
- package/templates/qoder/scripts/{common → foundation/io}/reqid.py +1 -1
- package/templates/qoder/scripts/foundation/protocol/__init__.py +0 -0
- package/templates/qoder/scripts/foundation/protocol/duckdb_conn.py +39 -0
- package/templates/qoder/scripts/{common → foundation/protocol}/mcp_base.py +1 -1
- package/templates/qoder/scripts/foundation/utils/__init__.py +0 -0
- package/templates/qoder/scripts/{common → foundation/utils}/eval_api.py +12 -1
- package/templates/qoder/scripts/{common → foundation/utils}/pip_install.py +23 -6
- package/templates/qoder/scripts/{common → foundation/utils}/platform_guard.py +1 -1
- package/templates/qoder/scripts/orchestration/__init__.py +0 -0
- package/templates/qoder/scripts/orchestration/wlkj.py +185 -0
- package/templates/qoder/scripts/protocol/__init__.py +0 -0
- package/templates/qoder/scripts/protocol/browser/README.md +207 -0
- package/templates/qoder/scripts/protocol/browser/SKILL.md +265 -0
- package/templates/qoder/scripts/protocol/browser/config.env +9 -0
- package/templates/qoder/scripts/protocol/browser/references/cdp-api.md +110 -0
- package/templates/qoder/scripts/protocol/browser/references/migration-2.5.3.md +72 -0
- package/templates/qoder/scripts/protocol/browser/references/site-patterns/.gitkeep +0 -0
- package/templates/qoder/scripts/protocol/browser/scripts/browser-discovery.mjs +138 -0
- package/templates/qoder/scripts/protocol/browser/scripts/cdp-proxy.mjs +672 -0
- package/templates/qoder/scripts/protocol/browser/scripts/check-deps.mjs +206 -0
- package/templates/qoder/scripts/protocol/browser/scripts/find-url.mjs +253 -0
- package/templates/qoder/scripts/protocol/browser/scripts/match-site.mjs +46 -0
- package/templates/qoder/scripts/protocol/browser/templates/config.env.template +9 -0
- package/templates/qoder/scripts/protocol/mcp/__init__.py +11 -0
- package/templates/qoder/scripts/protocol/mcp/kg_mcp_server.py +478 -0
- package/templates/qoder/scripts/{mcp → protocol/mcp}/lanhu_stdio_wrapper.py +11 -5
- package/templates/qoder/scripts/{mcp → protocol/mcp}/mcp_doctor.py +47 -14
- package/templates/qoder/scripts/protocol/mcp/mcp_launcher.py +600 -0
- package/templates/qoder/scripts/{mcp → protocol/mcp}/mysql_mcp_server.py +181 -67
- package/templates/qoder/scripts/protocol/mcp/zentao_mcp_server.py +1920 -0
- package/templates/qoder/scripts/protocol/transports/__init__.py +56 -0
- package/templates/qoder/scripts/protocol/transports/base.py +87 -0
- package/templates/qoder/scripts/protocol/transports/cli.py +132 -0
- package/templates/qoder/scripts/protocol/transports/http.py +141 -0
- package/templates/qoder/scripts/protocol/transports/stdio.py +293 -0
- package/templates/qoder/scripts/run_weekly_update.bat +27 -18
- package/templates/qoder/scripts/run_weekly_update.sh +22 -13
- package/templates/qoder/scripts/validation/__init__.py +0 -0
- package/templates/qoder/scripts/validation/eval/__init__.py +0 -0
- package/templates/qoder/scripts/validation/eval/perf-report-2026-06-24.md +139 -0
- package/templates/qoder/scripts/validation/eval/qwork_harness.py +617 -0
- package/templates/qoder/scripts/validation/eval/report-commands.md +220 -0
- package/templates/qoder/scripts/validation/eval/transcript_timing.py +386 -0
- package/templates/qoder/scripts/validation/metrics/__init__.py +0 -0
- package/templates/qoder/scripts/{report → validation/metrics}/eval_prd.py +17 -10
- package/templates/qoder/scripts/validation/metrics/usability_score.py +750 -0
- package/templates/qoder/scripts/validation/test/__init__.py +11 -0
- package/templates/qoder/scripts/validation/test/assertion_gen.py +551 -0
- package/templates/qoder/scripts/{test → validation/test}/autotest.py +64 -4
- package/templates/qoder/scripts/{common → validation/test}/autotest_auth.py +1 -1
- package/templates/qoder/scripts/{test → validation/test}/autotest_batch.py +16 -3
- package/templates/qoder/scripts/{test → validation/test}/autotest_data.py +17 -12
- package/templates/qoder/scripts/validation/test/autotest_opencli.py +646 -0
- package/templates/qoder/scripts/{test → validation/test}/autotest_run.py +17 -3
- package/templates/qoder/scripts/validation/test/autotest_webaccess.py +571 -0
- package/templates/qoder/scripts/{test → validation/test}/benchmark.py +12 -6
- package/templates/qoder/scripts/{test → validation/test}/kg_auto_login.py +8 -2
- package/templates/qoder/scripts/{test → validation/test}/kg_test_runner.py +7 -2
- package/templates/qoder/scripts/{test → validation/test}/page_probe.py +8 -3
- package/templates/qoder/scripts/validation/test/smoke_all_commands.py +170 -0
- package/templates/qoder/settings.json +30 -0
- package/templates/qoder/skills/design-import/SKILL.md +22 -13
- package/templates/qoder/skills/design-review/SKILL.md +19 -10
- package/templates/qoder/skills/prd-generator/SKILL.md +22 -18
- package/templates/qoder/skills/prd-review/SKILL.md +6 -4
- package/templates/qoder/skills/prototype-generator/SKILL.md +63 -14
- package/templates/qoder/skills/spec-coder/SKILL.md +22 -13
- package/templates/qoder/skills/spec-generator/SKILL.md +22 -10
- package/templates/qoder/skills/test-generator/SKILL.md +7 -5
- package/templates/qoder/skills/wl-code/SKILL.md +23 -11
- package/templates/qoder/skills/wl-commit/SKILL.md +9 -8
- package/templates/qoder/skills/wl-design/SKILL.md +5 -3
- package/templates/qoder/skills/wl-init/SKILL.md +6 -6
- package/templates/qoder/skills/wl-insight/SKILL.md +23 -10
- package/templates/qoder/skills/wl-prd-full/SKILL.md +57 -6
- package/templates/qoder/skills/wl-prd-quick/SKILL.md +3 -3
- package/templates/qoder/skills/wl-prd-review/SKILL.md +16 -4
- package/templates/qoder/skills/wl-report/SKILL.md +25 -23
- package/templates/qoder/skills/wl-search/SKILL.md +149 -78
- package/templates/qoder/skills/wl-spec/SKILL.md +7 -5
- package/templates/qoder/skills/wl-status/SKILL.md +48 -16
- package/templates/qoder/skills/wl-task/SKILL.md +92 -10
- package/templates/qoder/skills/wl-test/SKILL.md +75 -29
- package/templates/qoder/templates/prd-full-template.md +7 -0
- package/templates/root/AGENTS.md +275 -236
- package/templates/root/requirements.txt +3 -0
- package/templates/root//344/275/277/347/224/250/350/257/264/346/230/216.md +3 -3
- package/templates/root//346/226/260/346/211/213/346/214/207/345/215/227.md +2 -2
- package/templates/.qoder/.runtime/ctx-cache-5660152f1d6dd819.md +0 -23
- package/templates/.qoder/.runtime/ctx-cache-afdce0dac06b25b0.md +0 -23
- package/templates/.qoder/.runtime/search-cache-eae7644e7b122f35.txt +0 -1
- package/templates/.qoder/learning/eval-history.jsonl +0 -28
- package/templates/data/index/wiki-index.json +0 -8
- package/templates/qoder/nul +0 -4
- package/templates/qoder/scripts/kg/extract.py +0 -398
- package/templates/qoder/scripts/kg/kg_semantic.py +0 -156
- package/templates/qoder/scripts/mcp/kg_mcp_server.py +0 -606
- package/templates/qoder/scripts/mcp/mcp_launcher.py +0 -442
- package/templates/qoder/scripts/mcp/zentao_mcp_server.py +0 -384
- package/templates/qoder/scripts/task/__init__.py +0 -11
- package/templates/qoder/scripts/test/__init__.py +0 -11
- package/templates/qoder/scripts/wlkj.py +0 -116
- /package/templates/qoder/scripts/{common → deployment}/__init__.py +0 -0
- /package/templates/qoder/scripts/{common → domain/kg/graph}/graph_traverse.py +0 -0
- /package/templates/qoder/scripts/{common → domain/kg/server}/repowiki.py +0 -0
- /package/templates/qoder/scripts/{common → foundation/io}/atomicio.py +0 -0
- /package/templates/qoder/scripts/{test → validation/test}/secure-ls.js +0 -0
|
@@ -1,205 +1,207 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""
|
|
3
|
-
search_engine.py - 搜索倒排索引 + mtime 缓存 (性能优化 A2)
|
|
4
|
-
|
|
5
|
-
把 search_keywords 的 O(n) 全扫描 3267 keys 降到 O(命中数)。
|
|
6
|
-
|
|
7
|
-
设计:
|
|
8
|
-
- exact_index: {word_lower: [keyword_keys]} 精确匹配, O(1) 查找
|
|
9
|
-
- prefix/suffix 索引用 trigram (3-gram) 加速子串匹配
|
|
10
|
-
- 缓存序列化到 data/index/.inverted-cache.json, 含源 mtime
|
|
11
|
-
- 只在 code-keyword.json 变化时重建
|
|
12
|
-
|
|
13
|
-
核心 API:
|
|
14
|
-
get_inverted_index(repo_root) -> dict # 带缓存的倒排索引
|
|
15
|
-
search_keywords_fast(words, inverted, ki) -> {kw: files} # 快速搜索
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
import json
|
|
19
|
-
import os
|
|
20
|
-
import sys
|
|
21
|
-
from typing import Dict, List, Optional, Set, Tuple
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
#
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
inverted
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
""
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if
|
|
202
|
-
return True
|
|
203
|
-
if len(
|
|
204
|
-
return True
|
|
205
|
-
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
search_engine.py - 搜索倒排索引 + mtime 缓存 (性能优化 A2)
|
|
4
|
+
|
|
5
|
+
把 search_keywords 的 O(n) 全扫描 3267 keys 降到 O(命中数)。
|
|
6
|
+
|
|
7
|
+
设计:
|
|
8
|
+
- exact_index: {word_lower: [keyword_keys]} 精确匹配, O(1) 查找
|
|
9
|
+
- prefix/suffix 索引用 trigram (3-gram) 加速子串匹配
|
|
10
|
+
- 缓存序列化到 data/index/.inverted-cache.json, 含源 mtime
|
|
11
|
+
- 只在 code-keyword.json 变化时重建
|
|
12
|
+
|
|
13
|
+
核心 API:
|
|
14
|
+
get_inverted_index(repo_root) -> dict # 带缓存的倒排索引
|
|
15
|
+
search_keywords_fast(words, inverted, ki) -> {kw: files} # 快速搜索
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import sys
|
|
21
|
+
from typing import Dict, List, Optional, Set, Tuple
|
|
22
|
+
|
|
23
|
+
# v3.0 路径自举: 迁入 kg/ 后, atomicio 在 common/, 需把 common/ 加进 sys.path
|
|
24
|
+
_THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
25
|
+
_cp = os.path.join(os.path.dirname(_THIS_DIR), 'foundation')
|
|
26
|
+
if _cp not in sys.path:
|
|
27
|
+
sys.path.insert(0, _cp)
|
|
28
|
+
|
|
29
|
+
from atomicio import safe_read_json, atomic_write_json
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"get_inverted_index",
|
|
33
|
+
"search_keywords_fast",
|
|
34
|
+
"build_inverted_index",
|
|
35
|
+
"MIN_FUZZY_LEN",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
MIN_FUZZY_LEN = 4 # 与 search_index.py 保持一致
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _trigrams(s: str) -> Set[str]:
|
|
42
|
+
"""生成字符串的所有 3-gram (小写)。短于 3 字符则返回 {字符串本身}。"""
|
|
43
|
+
s = s.lower()
|
|
44
|
+
if len(s) < 3:
|
|
45
|
+
return {s}
|
|
46
|
+
return {s[i:i+3] for i in range(len(s) - 2)}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def build_inverted_index(keyword_index: Dict[str, list]) -> dict:
|
|
50
|
+
"""从 keyword_index 构建倒排索引。
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
{
|
|
54
|
+
"exact": {word_lower: [keyword_keys]}, # 精确匹配
|
|
55
|
+
"trigram": {trigram: set(keyword_keys)}, # 3-gram → 含它的 keys
|
|
56
|
+
"keywords": [所有 keyword_keys 排序] # 用于回退全扫描
|
|
57
|
+
}
|
|
58
|
+
"""
|
|
59
|
+
exact = {}
|
|
60
|
+
trigram = {}
|
|
61
|
+
all_keys = sorted(keyword_index.keys())
|
|
62
|
+
|
|
63
|
+
for kw in all_keys:
|
|
64
|
+
kl = kw.lower()
|
|
65
|
+
# 精确索引
|
|
66
|
+
exact.setdefault(kl, []).append(kw)
|
|
67
|
+
# trigram 索引 (用于子串匹配的候选集缩小)
|
|
68
|
+
for tg in _trigrams(kl):
|
|
69
|
+
trigram.setdefault(tg, set()).add(kw)
|
|
70
|
+
|
|
71
|
+
# set 转 list 便于 JSON 序列化
|
|
72
|
+
trigram_serializable = {tg: sorted(keys) for tg, keys in trigram.items()}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
"exact": exact,
|
|
76
|
+
"trigram": trigram_serializable,
|
|
77
|
+
"keywords": all_keys,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def get_inverted_index(repo_root: str) -> Optional[dict]:
|
|
82
|
+
"""获取倒排索引 (带 mtime 缓存)。
|
|
83
|
+
|
|
84
|
+
缓存逻辑:
|
|
85
|
+
- 读 .inverted-cache.json 的 source_mtime
|
|
86
|
+
- 与 code-keyword.json 当前 mtime 比对
|
|
87
|
+
- 不一致或缓存缺失则重建 + 持久化
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
倒排索引 dict, 或 None (keyword-index 不存在时)。
|
|
91
|
+
"""
|
|
92
|
+
index_dir = os.path.join(repo_root, "data", "index")
|
|
93
|
+
ki_path = os.path.join(index_dir, "code-keyword.json")
|
|
94
|
+
cache_path = os.path.join(index_dir, ".inverted-cache.json")
|
|
95
|
+
|
|
96
|
+
if not os.path.isfile(ki_path):
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
ki_mtime = os.path.getmtime(ki_path)
|
|
101
|
+
except OSError:
|
|
102
|
+
return None
|
|
103
|
+
|
|
104
|
+
# 尝试读缓存
|
|
105
|
+
cached = safe_read_json(cache_path, default=None)
|
|
106
|
+
if cached and cached.get("source_mtime") == ki_mtime:
|
|
107
|
+
# 缓存命中 (trigram 的 set 反序列化为 list, 搜索时兼容)
|
|
108
|
+
return cached
|
|
109
|
+
|
|
110
|
+
# 缓存失效或缺失, 重建
|
|
111
|
+
ki = safe_read_json(ki_path, default=None)
|
|
112
|
+
if not ki:
|
|
113
|
+
return None
|
|
114
|
+
|
|
115
|
+
inverted = build_inverted_index(ki)
|
|
116
|
+
inverted["source_mtime"] = ki_mtime
|
|
117
|
+
inverted["built_at"] = os.path.getmtime(ki_path)
|
|
118
|
+
|
|
119
|
+
# 持久化 (best-effort, 失败不阻塞搜索)
|
|
120
|
+
try:
|
|
121
|
+
atomic_write_json(cache_path, inverted)
|
|
122
|
+
except Exception:
|
|
123
|
+
pass
|
|
124
|
+
|
|
125
|
+
return inverted
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _candidate_keys(word: str, inverted: dict) -> List[str]:
|
|
129
|
+
"""用倒排索引找到候选 keyword keys (缩小扫描集)。
|
|
130
|
+
|
|
131
|
+
策略:
|
|
132
|
+
- 用 trigram 交集找候选 (子串匹配的双方至少共享一个 trigram)
|
|
133
|
+
- 精确命中必然包含在 trigram 候选里 (相同字符串共享所有 trigram)
|
|
134
|
+
- 候选为空则回退全部 keys (兜底, 罕见)
|
|
135
|
+
|
|
136
|
+
关键: 不能在精确命中后短路 —— query "insurance" 还要匹配
|
|
137
|
+
"insurancepolicy" 等 (query 是 kw 的子串), 所以必须返回 trigram 候选集。
|
|
138
|
+
"""
|
|
139
|
+
wl = word.lower()
|
|
140
|
+
tgs = _trigrams(wl)
|
|
141
|
+
trigram_index = inverted.get("trigram", {})
|
|
142
|
+
candidate_sets = []
|
|
143
|
+
for tg in tgs:
|
|
144
|
+
keys = trigram_index.get(tg)
|
|
145
|
+
if keys:
|
|
146
|
+
candidate_sets.append(set(keys))
|
|
147
|
+
|
|
148
|
+
if not candidate_sets:
|
|
149
|
+
# 词太短无 trigram (如 2 字符), 回退精确 + 全扫描兜底
|
|
150
|
+
exact = inverted.get("exact", {}).get(wl)
|
|
151
|
+
if exact:
|
|
152
|
+
return list(exact)
|
|
153
|
+
return inverted.get("keywords", [])
|
|
154
|
+
|
|
155
|
+
# 取并集: query 含 kw 或 kw 含 query, 至少一个 trigram 重叠
|
|
156
|
+
candidates = set()
|
|
157
|
+
for cs in candidate_sets:
|
|
158
|
+
candidates |= cs
|
|
159
|
+
|
|
160
|
+
return sorted(candidates)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def search_keywords_fast(
|
|
164
|
+
words: List[str],
|
|
165
|
+
inverted: Optional[dict],
|
|
166
|
+
keyword_index: Dict[str, list],
|
|
167
|
+
) -> Dict[str, list]:
|
|
168
|
+
"""快速关键词搜索 (用倒排索引缩小候选集)。
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
words: 查询词列表 (已小写化、已 CN 扩展)。
|
|
172
|
+
inverted: 倒排索引 (None 则回退到全扫描)。
|
|
173
|
+
keyword_index: 原始 keyword-index (用于取 files)。
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
{keyword_key: files} 匹配结果。
|
|
177
|
+
"""
|
|
178
|
+
matches = {}
|
|
179
|
+
|
|
180
|
+
if inverted is None:
|
|
181
|
+
# 回退: 全扫描 (旧逻辑)
|
|
182
|
+
for word in words:
|
|
183
|
+
for kw, files in keyword_index.items():
|
|
184
|
+
if _match(word, kw):
|
|
185
|
+
matches[kw] = files
|
|
186
|
+
return matches
|
|
187
|
+
|
|
188
|
+
for word in words:
|
|
189
|
+
candidates = _candidate_keys(word, inverted)
|
|
190
|
+
for kw in candidates:
|
|
191
|
+
files = keyword_index.get(kw)
|
|
192
|
+
if files and _match(word, kw):
|
|
193
|
+
matches[kw] = files
|
|
194
|
+
|
|
195
|
+
return matches
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _match(query_word: str, kw: str) -> bool:
|
|
199
|
+
"""与 search_index._match_keyword 一致的匹配逻辑。"""
|
|
200
|
+
w, k = query_word.lower(), kw.lower()
|
|
201
|
+
if w == k:
|
|
202
|
+
return True
|
|
203
|
+
if len(k) >= MIN_FUZZY_LEN and k in w:
|
|
204
|
+
return True
|
|
205
|
+
if len(w) >= MIN_FUZZY_LEN and w in k:
|
|
206
|
+
return True
|
|
207
|
+
return False
|