@daogee/mdgraph 1.0.0
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.md +25 -0
- package/LICENSE +21 -0
- package/README-ZH.md +203 -0
- package/README.md +203 -0
- package/agent-pack/README.md +26 -0
- package/agent-pack/host-examples.md +71 -0
- package/agent-pack/mcp-config.example.json +14 -0
- package/agent-pack/mdgraph-agent-instructions.md +21 -0
- package/agent-pack/prompts/relationship-trace.md +12 -0
- package/agent-pack/prompts/status-doctor.md +13 -0
- package/agent-pack/prompts/task-start-context.md +15 -0
- package/dist/analysis/doctor-scope.d.ts +8 -0
- package/dist/analysis/doctor-scope.js +109 -0
- package/dist/analysis/doctor.d.ts +148 -0
- package/dist/analysis/doctor.js +919 -0
- package/dist/analysis/status-freshness.d.ts +13 -0
- package/dist/analysis/status-freshness.js +55 -0
- package/dist/benchmark/benchmark.d.ts +118 -0
- package/dist/benchmark/benchmark.js +357 -0
- package/dist/bin/mdgraph.d.ts +2 -0
- package/dist/bin/mdgraph.js +877 -0
- package/dist/bundle/bundle.d.ts +66 -0
- package/dist/bundle/bundle.js +320 -0
- package/dist/config/limits.d.ts +24 -0
- package/dist/config/limits.js +24 -0
- package/dist/config/load-config.d.ts +14 -0
- package/dist/config/load-config.js +177 -0
- package/dist/db/connection.d.ts +15 -0
- package/dist/db/connection.js +145 -0
- package/dist/db/repositories.d.ts +159 -0
- package/dist/db/repositories.js +929 -0
- package/dist/db/schema.sql +110 -0
- package/dist/db/sqlite-adapter.d.ts +18 -0
- package/dist/db/sqlite-adapter.js +60 -0
- package/dist/diff/graph-diff.d.ts +41 -0
- package/dist/diff/graph-diff.js +334 -0
- package/dist/evaluation/retrieval-eval.d.ts +115 -0
- package/dist/evaluation/retrieval-eval.js +399 -0
- package/dist/export/diagram.d.ts +11 -0
- package/dist/export/diagram.js +41 -0
- package/dist/export/graphjson.d.ts +89 -0
- package/dist/export/graphjson.js +299 -0
- package/dist/export/markdown-index.d.ts +21 -0
- package/dist/export/markdown-index.js +89 -0
- package/dist/export/source-bridge.d.ts +22 -0
- package/dist/export/source-bridge.js +83 -0
- package/dist/extraction/entity-extractor.d.ts +12 -0
- package/dist/extraction/entity-extractor.js +153 -0
- package/dist/extraction/graph-builder.d.ts +2 -0
- package/dist/extraction/graph-builder.js +168 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +28 -0
- package/dist/indexer.d.ts +19 -0
- package/dist/indexer.js +100 -0
- package/dist/mcp/debug.d.ts +7 -0
- package/dist/mcp/debug.js +41 -0
- package/dist/mcp/server-instructions.d.ts +2 -0
- package/dist/mcp/server-instructions.js +40 -0
- package/dist/mcp/server.d.ts +28 -0
- package/dist/mcp/server.js +232 -0
- package/dist/mcp/tools.d.ts +31 -0
- package/dist/mcp/tools.js +506 -0
- package/dist/mcp/transport.d.ts +50 -0
- package/dist/mcp/transport.js +88 -0
- package/dist/parser/frontmatter.d.ts +10 -0
- package/dist/parser/frontmatter.js +191 -0
- package/dist/parser/links.d.ts +6 -0
- package/dist/parser/links.js +9 -0
- package/dist/parser/markdown-parser.d.ts +2 -0
- package/dist/parser/markdown-parser.js +267 -0
- package/dist/query/context-builder.d.ts +85 -0
- package/dist/query/context-builder.js +413 -0
- package/dist/query/search.d.ts +32 -0
- package/dist/query/search.js +237 -0
- package/dist/query/trace.d.ts +10 -0
- package/dist/query/trace.js +55 -0
- package/dist/reporting/report.d.ts +49 -0
- package/dist/reporting/report.js +143 -0
- package/dist/resolution/link-resolver.d.ts +17 -0
- package/dist/resolution/link-resolver.js +113 -0
- package/dist/scanner/file-scanner.d.ts +4 -0
- package/dist/scanner/file-scanner.js +144 -0
- package/dist/semantic/local-embedding.d.ts +4 -0
- package/dist/semantic/local-embedding.js +30 -0
- package/dist/semantic/status.d.ts +19 -0
- package/dist/semantic/status.js +70 -0
- package/dist/semantic/vector-codec.d.ts +3 -0
- package/dist/semantic/vector-codec.js +35 -0
- package/dist/types.d.ts +198 -0
- package/dist/types.js +15 -0
- package/dist/utils/bounded-json.d.ts +9 -0
- package/dist/utils/bounded-json.js +57 -0
- package/dist/utils/content-risk.d.ts +5 -0
- package/dist/utils/content-risk.js +20 -0
- package/dist/utils/fts.d.ts +3 -0
- package/dist/utils/fts.js +52 -0
- package/dist/utils/id.d.ts +2 -0
- package/dist/utils/id.js +8 -0
- package/dist/utils/path-safety.d.ts +4 -0
- package/dist/utils/path-safety.js +25 -0
- package/dist/utils/text.d.ts +7 -0
- package/dist/utils/text.js +36 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +11 -0
- package/dist/watcher/file-watcher.d.ts +11 -0
- package/dist/watcher/file-watcher.js +127 -0
- package/docs/EN/Agent_Integration.md +129 -0
- package/docs/EN/Architecture.md +135 -0
- package/docs/EN/Evaluation_Questions.md +68 -0
- package/docs/EN/Output_Contracts.md +274 -0
- package/docs/EN/Public_Contracts.md +88 -0
- package/docs/EN/Public_Contracts_1.0.md +234 -0
- package/docs/EN/Release_Checklist.md +82 -0
- package/docs/ZH/Agent_Integration.md +105 -0
- package/docs/ZH/Architecture.md +124 -0
- package/docs/ZH/Evaluation_Questions.md +66 -0
- package/docs/ZH/Output_Contracts.md +269 -0
- package/docs/ZH/Public_Contracts.md +81 -0
- package/docs/ZH/Public_Contracts_1.0.md +202 -0
- package/docs/ZH/Release_Checklist.md +83 -0
- package/package.json +94 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { performance } from "node:perf_hooks";
|
|
2
|
+
import { CONTEXT_PACKING_STRATEGY, buildContext } from "../query/context-builder.js";
|
|
3
|
+
import { explainSearchGraph } from "../query/search.js";
|
|
4
|
+
import { traceNodes } from "../query/trace.js";
|
|
5
|
+
import { slugifyHeading } from "../utils/text.js";
|
|
6
|
+
export const EVALUATION_QUERY_SET_NAMES = ["alpha", "cjk"];
|
|
7
|
+
export const ALPHA_EVALUATION_CASES = [
|
|
8
|
+
{
|
|
9
|
+
id: "alpha-1",
|
|
10
|
+
query: "Why does RedisTimeoutError affect LoginFlow?",
|
|
11
|
+
expectedDocuments: ["docs/redis-cache-design.md", "docs/login-flow.md", "docs/runbooks/auth-retry-runbook.md"],
|
|
12
|
+
expectedSections: [
|
|
13
|
+
{ path: "docs/redis-cache-design.md", heading: "Timeout Handling" },
|
|
14
|
+
{ path: "docs/login-flow.md", heading: "Login Flow" },
|
|
15
|
+
{ path: "docs/runbooks/auth-retry-runbook.md", heading: "Auth Retry Runbook" }
|
|
16
|
+
],
|
|
17
|
+
expectedEntities: ["RedisTimeoutError", "LoginFlow", "AuthRetryRunbook"],
|
|
18
|
+
expectedEdges: ["REFERENCES", "DEPENDS_ON"],
|
|
19
|
+
expectedSourceRefs: ["src/cache/redis.ts", "scripts/restart-auth.ps1"],
|
|
20
|
+
trace: { from: "LoginFlow", to: "RedisTimeoutError", depth: 6, edgeKinds: ["DEPENDS_ON"] }
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "alpha-2",
|
|
24
|
+
query: "Which older decisions does Auth v2 Design depend on?",
|
|
25
|
+
expectedDocuments: ["docs/auth-v2-design.md", "docs/adr/adr-001-cache-failure-policy.md", "docs/redis-cache-design.md"],
|
|
26
|
+
expectedSections: [
|
|
27
|
+
{ path: "docs/auth-v2-design.md", heading: "Session Refresh" },
|
|
28
|
+
{ path: "docs/adr/adr-001-cache-failure-policy.md", heading: "Decision" },
|
|
29
|
+
{ path: "docs/redis-cache-design.md", heading: "Timeout Handling" }
|
|
30
|
+
],
|
|
31
|
+
expectedEntities: ["AuthService", "CacheFailurePolicy", "RedisTimeoutError"],
|
|
32
|
+
expectedEdges: ["DEPENDS_ON", "DEFINES"],
|
|
33
|
+
expectedSourceRefs: ["src/auth/AuthService.ts", "src/cache/redis.ts"],
|
|
34
|
+
trace: { from: "Auth v2 Design", to: "CacheFailurePolicy", depth: 4, edgeKinds: ["DEPENDS_ON", "DEFINES"] }
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
id: "alpha-3",
|
|
38
|
+
query: "Where is GET /api/auth/login defined and referenced?",
|
|
39
|
+
expectedDocuments: ["docs/api/login-api.md", "docs/login-flow.md", "docs/auth-v2-design.md"],
|
|
40
|
+
expectedSections: [
|
|
41
|
+
{ path: "docs/api/login-api.md", heading: "Login API" },
|
|
42
|
+
{ path: "docs/login-flow.md", heading: "Login Flow" },
|
|
43
|
+
{ path: "docs/auth-v2-design.md", heading: "Session Refresh" }
|
|
44
|
+
],
|
|
45
|
+
expectedEntities: ["GET /api/auth/login", "AuthService", "RedisTimeoutError"],
|
|
46
|
+
expectedEdges: ["DEFINES", "IMPLEMENTS", "DEPENDS_ON"],
|
|
47
|
+
expectedSourceRefs: ["src/routes/auth.ts"],
|
|
48
|
+
trace: { from: "Login API", to: "src/routes/auth.ts", depth: 2, edgeKinds: ["IMPLEMENTS"] }
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: "alpha-4",
|
|
52
|
+
query: "Which runbooks are affected by AUTH_RETRY_LIMIT?",
|
|
53
|
+
expectedDocuments: ["docs/runbooks/auth-retry-runbook.md", "docs/redis-cache-design.md", "docs/login-flow.md"],
|
|
54
|
+
expectedSections: [
|
|
55
|
+
{ path: "docs/runbooks/auth-retry-runbook.md", heading: "Auth Retry Runbook" },
|
|
56
|
+
{ path: "docs/redis-cache-design.md", heading: "Timeout Handling" },
|
|
57
|
+
{ path: "docs/login-flow.md", heading: "Login Flow" }
|
|
58
|
+
],
|
|
59
|
+
expectedEntities: ["AUTH_RETRY_LIMIT", "AuthRetryRunbook", "LoginFlow"],
|
|
60
|
+
expectedEdges: ["REFERENCES", "DEPENDS_ON"],
|
|
61
|
+
expectedSourceRefs: ["scripts/restart-auth.ps1"],
|
|
62
|
+
trace: { from: "AuthRetryRunbook", to: "LoginFlow", depth: 5, edgeKinds: ["REFERENCES", "DEPENDS_ON"] }
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
id: "alpha-5",
|
|
66
|
+
query: "Which design documents have been superseded by newer designs?",
|
|
67
|
+
expectedDocuments: ["docs/auth-v2-design.md", "docs/auth-v3-design.md"],
|
|
68
|
+
expectedSections: [
|
|
69
|
+
{ path: "docs/auth-v2-design.md", heading: "Session Refresh" },
|
|
70
|
+
{ path: "docs/auth-v3-design.md", heading: "Session Refresh" }
|
|
71
|
+
],
|
|
72
|
+
expectedEntities: ["AuthService", "AuthServiceV3"],
|
|
73
|
+
expectedEdges: ["SUPERSEDES", "DEPRECATED_BY"],
|
|
74
|
+
expectedSourceRefs: ["src/auth/AuthService.ts", "src/auth/AuthServiceV3.ts"],
|
|
75
|
+
trace: { from: "Auth v3 Design", to: "Auth v2 Design", depth: 2, edgeKinds: ["SUPERSEDES"] }
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
id: "alpha-6",
|
|
79
|
+
query: "Which design assumptions are related to the Redis Timeout Incident?",
|
|
80
|
+
expectedDocuments: ["docs/incidents/redis-timeout-incident.md", "docs/redis-cache-design.md", "docs/runbooks/auth-retry-runbook.md"],
|
|
81
|
+
expectedSections: [
|
|
82
|
+
{ path: "docs/incidents/redis-timeout-incident.md", heading: "Redis Timeout Incident" },
|
|
83
|
+
{ path: "docs/redis-cache-design.md", heading: "Timeout Handling" },
|
|
84
|
+
{ path: "docs/runbooks/auth-retry-runbook.md", heading: "Auth Retry Runbook" }
|
|
85
|
+
],
|
|
86
|
+
expectedEntities: ["RedisTimeoutError", "LoginFlow", "AuthRetryRunbook"],
|
|
87
|
+
expectedEdges: ["DEPENDS_ON", "REFERENCES"],
|
|
88
|
+
expectedSourceRefs: ["src/cache/redis.ts", "scripts/restart-auth.ps1"],
|
|
89
|
+
trace: { from: "Redis Timeout Incident", to: "AuthRetryRunbook", depth: 5, edgeKinds: ["DEPENDS_ON", "REFERENCES"] }
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
id: "alpha-7",
|
|
93
|
+
query: "Which documents correspond to src/routes/auth.ts?",
|
|
94
|
+
expectedDocuments: ["docs/api/login-api.md", "docs/login-flow.md"],
|
|
95
|
+
expectedSections: [
|
|
96
|
+
{ path: "docs/api/login-api.md", heading: "Login API" },
|
|
97
|
+
{ path: "docs/login-flow.md", heading: "Login Flow" }
|
|
98
|
+
],
|
|
99
|
+
expectedEntities: ["GET /api/auth/login", "LoginFlow"],
|
|
100
|
+
expectedEdges: ["IMPLEMENTS", "DEPENDS_ON"],
|
|
101
|
+
expectedSourceRefs: ["src/routes/auth.ts"],
|
|
102
|
+
trace: { from: "src/routes/auth.ts", to: "LoginFlow", depth: 5, edgeKinds: ["IMPLEMENTS", "DEPENDS_ON"] }
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
id: "alpha-8",
|
|
106
|
+
query: "Which documents mention RedisTimeoutError but do not link directly?",
|
|
107
|
+
expectedDocuments: ["docs/redis-cache-design.md", "docs/incidents/redis-timeout-incident.md", "docs/runbooks/auth-retry-runbook.md"],
|
|
108
|
+
expectedSections: [
|
|
109
|
+
{ path: "docs/redis-cache-design.md", heading: "Timeout Handling" },
|
|
110
|
+
{ path: "docs/incidents/redis-timeout-incident.md", heading: "Redis Timeout Incident" },
|
|
111
|
+
{ path: "docs/runbooks/auth-retry-runbook.md", heading: "Auth Retry Runbook" }
|
|
112
|
+
],
|
|
113
|
+
expectedEntities: ["RedisTimeoutError", "LoginFlow"],
|
|
114
|
+
expectedEdges: ["REFERENCES"],
|
|
115
|
+
expectedSourceRefs: ["src/cache/redis.ts", "scripts/restart-auth.ps1"],
|
|
116
|
+
trace: { from: "Redis Timeout Incident", to: "RedisTimeoutError", depth: 4, edgeKinds: ["REFERENCES", "DEPENDS_ON"] }
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
id: "alpha-9",
|
|
120
|
+
query: "Which design documents are missing source_refs?",
|
|
121
|
+
expectedDocuments: ["docs/auth-v3-design.md"],
|
|
122
|
+
expectedSections: [{ path: "docs/auth-v3-design.md", heading: "Session Refresh" }],
|
|
123
|
+
expectedEntities: ["AuthServiceV3"],
|
|
124
|
+
expectedEdges: ["IMPLEMENTS"],
|
|
125
|
+
expectedSourceRefs: ["src/auth/AuthServiceV3.ts"],
|
|
126
|
+
trace: { from: "Auth v3 Design", to: "src/auth/AuthServiceV3.ts", depth: 2, edgeKinds: ["IMPLEMENTS"] }
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: "alpha-10",
|
|
130
|
+
query: "What is the documentation chain for LoginFlow from requirement to implementation?",
|
|
131
|
+
expectedDocuments: [
|
|
132
|
+
"docs/login-flow.md",
|
|
133
|
+
"docs/api/login-api.md",
|
|
134
|
+
"docs/auth-v2-design.md",
|
|
135
|
+
"docs/redis-cache-design.md",
|
|
136
|
+
"docs/adr/adr-001-cache-failure-policy.md"
|
|
137
|
+
],
|
|
138
|
+
expectedSections: [
|
|
139
|
+
{ path: "docs/login-flow.md", heading: "Login Flow" },
|
|
140
|
+
{ path: "docs/api/login-api.md", heading: "Login API" },
|
|
141
|
+
{ path: "docs/auth-v2-design.md", heading: "Session Refresh" },
|
|
142
|
+
{ path: "docs/redis-cache-design.md", heading: "Timeout Handling" },
|
|
143
|
+
{ path: "docs/adr/adr-001-cache-failure-policy.md", heading: "Decision" }
|
|
144
|
+
],
|
|
145
|
+
expectedEntities: ["LoginFlow", "GET /api/auth/login", "AuthService", "RedisTimeoutError", "CacheFailurePolicy"],
|
|
146
|
+
expectedEdges: ["DEPENDS_ON", "IMPLEMENTS", "DEFINES"],
|
|
147
|
+
expectedSourceRefs: ["src/routes/auth.ts", "src/auth/AuthService.ts", "src/cache/redis.ts"],
|
|
148
|
+
trace: { from: "LoginFlow", to: "src/auth/AuthService.ts", depth: 6, edgeKinds: ["DEPENDS_ON", "IMPLEMENTS"] }
|
|
149
|
+
}
|
|
150
|
+
];
|
|
151
|
+
export const CJK_EVALUATION_CASES = [
|
|
152
|
+
{
|
|
153
|
+
id: "cjk-1",
|
|
154
|
+
query: "登录流程 缓存超时 认证重试",
|
|
155
|
+
expectedDocuments: ["docs/zh/login-flow.md", "docs/zh/cache-timeout-design.md", "docs/zh/runbooks/auth-retry-runbook.md"],
|
|
156
|
+
expectedSections: [
|
|
157
|
+
{ path: "docs/zh/login-flow.md", heading: "登录流程" },
|
|
158
|
+
{ path: "docs/zh/cache-timeout-design.md", heading: "缓存超时处理" },
|
|
159
|
+
{ path: "docs/zh/runbooks/auth-retry-runbook.md", heading: "认证重试运行手册" }
|
|
160
|
+
],
|
|
161
|
+
expectedEntities: ["Redis缓存超时"],
|
|
162
|
+
expectedEdges: ["DEPENDS_ON", "LINKS_TO"],
|
|
163
|
+
expectedSourceRefs: ["src/cache/redis.ts", "scripts/restart-auth.ps1"],
|
|
164
|
+
trace: { from: "登录流程", to: "缓存超时策略", depth: 6, edgeKinds: ["DEPENDS_ON"] }
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
id: "cjk-2",
|
|
168
|
+
query: "缓存超时影响登录流程的处理",
|
|
169
|
+
expectedDocuments: ["docs/zh/cache-timeout-design.md", "docs/zh/login-flow.md", "docs/zh/runbooks/auth-retry-runbook.md"],
|
|
170
|
+
expectedSections: [
|
|
171
|
+
{ path: "docs/zh/cache-timeout-design.md", heading: "缓存超时处理" },
|
|
172
|
+
{ path: "docs/zh/login-flow.md", heading: "登录流程" },
|
|
173
|
+
{ path: "docs/zh/runbooks/auth-retry-runbook.md", heading: "认证重试运行手册" }
|
|
174
|
+
],
|
|
175
|
+
expectedEntities: ["Redis缓存超时"],
|
|
176
|
+
expectedEdges: ["DEPENDS_ON"],
|
|
177
|
+
expectedSourceRefs: ["src/cache/redis.ts"],
|
|
178
|
+
trace: { from: "登录流程", to: "缓存超时策略", depth: 6, edgeKinds: ["DEPENDS_ON"] }
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
id: "cjk-3",
|
|
182
|
+
query: "AUTH_RETRY_LIMIT 会影响哪些认证运行手册",
|
|
183
|
+
expectedDocuments: ["docs/zh/runbooks/auth-retry-runbook.md", "docs/zh/cache-timeout-design.md", "docs/zh/login-flow.md"],
|
|
184
|
+
expectedSections: [
|
|
185
|
+
{ path: "docs/zh/runbooks/auth-retry-runbook.md", heading: "认证重试运行手册" },
|
|
186
|
+
{ path: "docs/zh/cache-timeout-design.md", heading: "缓存超时处理" },
|
|
187
|
+
{ path: "docs/zh/login-flow.md", heading: "登录流程" }
|
|
188
|
+
],
|
|
189
|
+
expectedEntities: ["AUTH_RETRY_LIMIT"],
|
|
190
|
+
expectedEdges: ["REFERENCES", "DEPENDS_ON"],
|
|
191
|
+
expectedSourceRefs: ["scripts/restart-auth.ps1"],
|
|
192
|
+
trace: { from: "AUTH_RETRY_LIMIT", to: "登录流程", depth: 6, edgeKinds: ["DEPENDS_ON"] }
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
id: "cjk-4",
|
|
196
|
+
query: "POST /api/zh/login 登录接口 源码",
|
|
197
|
+
expectedDocuments: ["docs/zh/api/login-api.md", "docs/zh/login-flow.md"],
|
|
198
|
+
expectedSections: [
|
|
199
|
+
{ path: "docs/zh/api/login-api.md", heading: "登录接口" },
|
|
200
|
+
{ path: "docs/zh/login-flow.md", heading: "登录流程" }
|
|
201
|
+
],
|
|
202
|
+
expectedEntities: ["POST /api/zh/login"],
|
|
203
|
+
expectedEdges: ["IMPLEMENTS", "DEPENDS_ON"],
|
|
204
|
+
expectedSourceRefs: ["src/routes/auth.ts"],
|
|
205
|
+
trace: { from: "POST /api/zh/login", to: "src/routes/auth.ts", depth: 4, edgeKinds: ["IMPLEMENTS"] }
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
id: "cjk-5",
|
|
209
|
+
query: "決済 リスク RiskCheckService 実装",
|
|
210
|
+
expectedDocuments: ["docs/ja/payment-risk.md"],
|
|
211
|
+
expectedSections: [{ path: "docs/ja/payment-risk.md", heading: "決済リスク設計" }],
|
|
212
|
+
expectedEntities: ["RiskCheckService"],
|
|
213
|
+
expectedEdges: ["IMPLEMENTS", "DEFINES"],
|
|
214
|
+
expectedSourceRefs: ["src/payment/risk.ts"],
|
|
215
|
+
trace: { from: "RiskCheckService", to: "src/payment/risk.ts", depth: 4, edgeKinds: ["IMPLEMENTS"] }
|
|
216
|
+
}
|
|
217
|
+
];
|
|
218
|
+
export function evaluationCasesForQuerySet(querySet) {
|
|
219
|
+
switch (querySet) {
|
|
220
|
+
case "alpha":
|
|
221
|
+
return ALPHA_EVALUATION_CASES;
|
|
222
|
+
case "cjk":
|
|
223
|
+
return CJK_EVALUATION_CASES;
|
|
224
|
+
default:
|
|
225
|
+
throw new Error(`Unknown evaluation query set: ${querySet}. Expected one of: ${EVALUATION_QUERY_SET_NAMES.join(", ")}.`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
export function evaluateRetrieval(repository, config, options = {}) {
|
|
229
|
+
const querySet = options.querySet ?? "alpha";
|
|
230
|
+
const queryMode = options.queryMode ?? "auto";
|
|
231
|
+
const cases = options.cases ?? evaluationCasesForQuerySet(querySet);
|
|
232
|
+
const limit = options.limit ?? config.search.defaultLimit;
|
|
233
|
+
const results = cases.map((evaluationCase) => evaluateCase(repository, config, evaluationCase, limit, queryMode));
|
|
234
|
+
return {
|
|
235
|
+
querySet,
|
|
236
|
+
limit,
|
|
237
|
+
ranking: summarizeRanking(results, queryMode),
|
|
238
|
+
generatedAt: new Date().toISOString(),
|
|
239
|
+
summary: summarize(results),
|
|
240
|
+
cases: results
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function evaluateCase(repository, config, evaluationCase, limit, queryMode) {
|
|
244
|
+
const start = performance.now();
|
|
245
|
+
const searchExplanation = explainSearchGraph(repository, config, evaluationCase.query, limit, { queryMode });
|
|
246
|
+
const searchResults = searchExplanation.results;
|
|
247
|
+
const context = buildContext(repository, config, evaluationCase.query, { debug: true, searchOptions: { queryMode } });
|
|
248
|
+
const trace = evaluationCase.trace
|
|
249
|
+
? traceNodes(repository, evaluationCase.trace.from, evaluationCase.trace.to, evaluationCase.trace.depth ?? config.search.maxDepth)
|
|
250
|
+
: undefined;
|
|
251
|
+
const latencyMs = performance.now() - start;
|
|
252
|
+
const searchDocuments = unique(searchResults.map((result) => result.document.path));
|
|
253
|
+
const contextItems = context.items.map((item) => ({ path: item.path, heading: item.heading, reason: item.reason }));
|
|
254
|
+
const matchedEntities = unique(searchResults.flatMap((result) => result.matchedEntities.map((entity) => entity.name)));
|
|
255
|
+
const resolvedEntities = resolveExpected(repository, evaluationCase.expectedEntities, "entity");
|
|
256
|
+
const resolvedSourceRefs = resolveExpected(repository, evaluationCase.expectedSourceRefs, "source_ref");
|
|
257
|
+
const edgeKinds = observedEdgeKinds(repository, trace);
|
|
258
|
+
const metrics = calculateMetrics({
|
|
259
|
+
evaluationCase,
|
|
260
|
+
searchResults,
|
|
261
|
+
context,
|
|
262
|
+
trace,
|
|
263
|
+
latencyMs,
|
|
264
|
+
resolvedEntities,
|
|
265
|
+
resolvedSourceRefs,
|
|
266
|
+
edgeKinds
|
|
267
|
+
});
|
|
268
|
+
return {
|
|
269
|
+
id: evaluationCase.id,
|
|
270
|
+
query: evaluationCase.query,
|
|
271
|
+
passed: casePassed(metrics),
|
|
272
|
+
expected: {
|
|
273
|
+
expectedDocuments: evaluationCase.expectedDocuments,
|
|
274
|
+
expectedSections: evaluationCase.expectedSections,
|
|
275
|
+
expectedEntities: evaluationCase.expectedEntities,
|
|
276
|
+
expectedEdges: evaluationCase.expectedEdges,
|
|
277
|
+
expectedSourceRefs: evaluationCase.expectedSourceRefs
|
|
278
|
+
},
|
|
279
|
+
observed: {
|
|
280
|
+
searchDocuments,
|
|
281
|
+
contextItems,
|
|
282
|
+
matchedEntities,
|
|
283
|
+
resolvedEntities,
|
|
284
|
+
resolvedSourceRefs,
|
|
285
|
+
edgeKinds,
|
|
286
|
+
trace,
|
|
287
|
+
ranking: {
|
|
288
|
+
queryMode,
|
|
289
|
+
searchFusion: searchExplanation.ranking.fusion,
|
|
290
|
+
searchChannels: searchExplanation.ranking.channels,
|
|
291
|
+
semanticActive: searchExplanation.semanticActive,
|
|
292
|
+
optionalReranker: searchExplanation.ranking.optionalReranker,
|
|
293
|
+
contextPackingStrategy: context.debug?.packingStrategy ?? CONTEXT_PACKING_STRATEGY
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
metrics
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function calculateMetrics(input) {
|
|
300
|
+
const expectedDocumentSet = new Set(input.evaluationCase.expectedDocuments);
|
|
301
|
+
const returnedDocuments = unique(input.searchResults.map((result) => result.document.path));
|
|
302
|
+
const contextMatches = input.context.items.filter((item) => expectedDocumentSet.has(item.path));
|
|
303
|
+
const observedSectionKeys = new Set(input.context.items.map((item) => sectionKey(item.path, item.heading)));
|
|
304
|
+
const traceSuccess = input.evaluationCase.trace
|
|
305
|
+
? Boolean(input.trace?.found) && expectedCovered(input.evaluationCase.trace.edgeKinds, input.trace?.steps.map((step) => step.edgeKind) ?? []) === 1
|
|
306
|
+
: null;
|
|
307
|
+
return {
|
|
308
|
+
topKDocumentRecall: expectedCovered(input.evaluationCase.expectedDocuments, returnedDocuments),
|
|
309
|
+
expectedSectionRecall: expectedCovered(input.evaluationCase.expectedSections.map(sectionExpectationKey), [...observedSectionKeys]),
|
|
310
|
+
contextPrecision: input.context.items.length ? contextMatches.length / input.context.items.length : 0,
|
|
311
|
+
entityRecall: expectedCovered(input.evaluationCase.expectedEntities, input.resolvedEntities),
|
|
312
|
+
sourceRefRecall: expectedCovered(input.evaluationCase.expectedSourceRefs, input.resolvedSourceRefs),
|
|
313
|
+
edgeKindCoverage: expectedCovered(input.evaluationCase.expectedEdges, input.edgeKinds),
|
|
314
|
+
traceSuccess,
|
|
315
|
+
latencyMs: roundMetric(input.latencyMs),
|
|
316
|
+
returnedChars: input.context.usedChars,
|
|
317
|
+
budgetFit: input.context.usedChars <= input.context.maxChars,
|
|
318
|
+
fanout: {
|
|
319
|
+
seedNodes: input.context.debug?.seedNodes ?? 0,
|
|
320
|
+
visitedNodes: input.context.debug?.visitedNodes ?? 0,
|
|
321
|
+
expandedEdges: input.context.debug?.expandedEdges ?? 0,
|
|
322
|
+
skippedByNodeLimit: input.context.debug?.skippedByNodeLimit ?? 0,
|
|
323
|
+
skippedByDepth: input.context.debug?.skippedByDepth ?? 0
|
|
324
|
+
},
|
|
325
|
+
reasonCoverage: input.searchResults.every((result) => result.reason.length > 0) && input.context.items.every((item) => item.reason.length > 0),
|
|
326
|
+
rankingReasonCoverage: input.searchResults.every((result) => result.reason.includes("RRF fusion")),
|
|
327
|
+
contextDiversity: input.context.debug?.packingDiversityRatio ?? 0
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
function summarize(results) {
|
|
331
|
+
const count = results.length || 1;
|
|
332
|
+
return {
|
|
333
|
+
cases: results.length,
|
|
334
|
+
passed: results.filter((result) => result.passed).length,
|
|
335
|
+
failed: results.filter((result) => !result.passed).length,
|
|
336
|
+
averageTopKDocumentRecall: average(results.map((result) => result.metrics.topKDocumentRecall), count),
|
|
337
|
+
averageExpectedSectionRecall: average(results.map((result) => result.metrics.expectedSectionRecall), count),
|
|
338
|
+
averageContextPrecision: average(results.map((result) => result.metrics.contextPrecision), count),
|
|
339
|
+
averageContextDiversity: average(results.map((result) => result.metrics.contextDiversity), count),
|
|
340
|
+
averageLatencyMs: average(results.map((result) => result.metrics.latencyMs), count),
|
|
341
|
+
averageReturnedChars: average(results.map((result) => result.metrics.returnedChars), count)
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function summarizeRanking(results, queryMode) {
|
|
345
|
+
const count = results.length || 1;
|
|
346
|
+
const rerankers = results.map((result) => result.observed.ranking.optionalReranker);
|
|
347
|
+
const channels = results.flatMap((result) => result.observed.ranking.searchChannels);
|
|
348
|
+
return {
|
|
349
|
+
queryMode,
|
|
350
|
+
searchFusion: "rrf",
|
|
351
|
+
contextPackingStrategy: CONTEXT_PACKING_STRATEGY,
|
|
352
|
+
optionalReranker: rerankers.includes("local-hash") ? "local-hash" : "none",
|
|
353
|
+
semanticActiveCases: results.filter((result) => result.observed.ranking.semanticActive).length,
|
|
354
|
+
searchChannels: [...new Set(channels)].sort(),
|
|
355
|
+
rankingReasonCoverage: results.every((result) => result.metrics.rankingReasonCoverage),
|
|
356
|
+
averageContextDiversity: average(results.map((result) => result.metrics.contextDiversity), count)
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function casePassed(metrics) {
|
|
360
|
+
return metrics.topKDocumentRecall === 1
|
|
361
|
+
&& metrics.expectedSectionRecall === 1
|
|
362
|
+
&& metrics.entityRecall === 1
|
|
363
|
+
&& metrics.sourceRefRecall === 1
|
|
364
|
+
&& metrics.edgeKindCoverage === 1
|
|
365
|
+
&& metrics.budgetFit
|
|
366
|
+
&& metrics.reasonCoverage
|
|
367
|
+
&& metrics.rankingReasonCoverage
|
|
368
|
+
&& metrics.traceSuccess !== false;
|
|
369
|
+
}
|
|
370
|
+
function resolveExpected(repository, values, expectedKind) {
|
|
371
|
+
return values.filter((value) => repository.resolveNode(value)?.kind === expectedKind);
|
|
372
|
+
}
|
|
373
|
+
function observedEdgeKinds(repository, trace) {
|
|
374
|
+
const graphEdgeKinds = repository.storageDiagnostics().edgeKinds.map((item) => item.kind);
|
|
375
|
+
const traceEdgeKinds = trace?.steps.map((step) => step.edgeKind) ?? [];
|
|
376
|
+
return unique([...graphEdgeKinds, ...traceEdgeKinds]);
|
|
377
|
+
}
|
|
378
|
+
function expectedCovered(expected, observed) {
|
|
379
|
+
if (!expected.length) {
|
|
380
|
+
return 1;
|
|
381
|
+
}
|
|
382
|
+
const observedSet = new Set(observed);
|
|
383
|
+
return roundMetric(expected.filter((item) => observedSet.has(item)).length / expected.length);
|
|
384
|
+
}
|
|
385
|
+
function sectionExpectationKey(section) {
|
|
386
|
+
return sectionKey(section.path, section.heading);
|
|
387
|
+
}
|
|
388
|
+
function sectionKey(path, heading) {
|
|
389
|
+
return `${path}#${slugifyHeading(heading ?? "")}`;
|
|
390
|
+
}
|
|
391
|
+
function unique(values) {
|
|
392
|
+
return [...new Set(values)];
|
|
393
|
+
}
|
|
394
|
+
function average(values, count) {
|
|
395
|
+
return roundMetric(values.reduce((total, value) => total + value, 0) / count);
|
|
396
|
+
}
|
|
397
|
+
function roundMetric(value) {
|
|
398
|
+
return Number(value.toFixed(4));
|
|
399
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TraceResult } from "../query/trace.js";
|
|
2
|
+
export interface MermaidTraceExport {
|
|
3
|
+
format: "mdgraph-mermaid";
|
|
4
|
+
formatVersion: 1;
|
|
5
|
+
diagramType: "trace";
|
|
6
|
+
found: boolean;
|
|
7
|
+
diagram: string;
|
|
8
|
+
trace: TraceResult;
|
|
9
|
+
}
|
|
10
|
+
export declare function buildMermaidTraceExport(trace: TraceResult): MermaidTraceExport;
|
|
11
|
+
export declare function formatTraceMermaid(trace: TraceResult): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export function buildMermaidTraceExport(trace) {
|
|
2
|
+
return {
|
|
3
|
+
format: "mdgraph-mermaid",
|
|
4
|
+
formatVersion: 1,
|
|
5
|
+
diagramType: "trace",
|
|
6
|
+
found: trace.found,
|
|
7
|
+
diagram: formatTraceMermaid(trace),
|
|
8
|
+
trace
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export function formatTraceMermaid(trace) {
|
|
12
|
+
if (!trace.found || !trace.steps.length) {
|
|
13
|
+
const message = trace.message ?? "No trace path found.";
|
|
14
|
+
return [`%% ${escapeMermaidComment(message)}`, "flowchart LR"].join("\n");
|
|
15
|
+
}
|
|
16
|
+
const labels = new Map();
|
|
17
|
+
for (const step of trace.steps) {
|
|
18
|
+
labels.set(step.fromId, step.fromLabel);
|
|
19
|
+
labels.set(step.toId, step.toLabel);
|
|
20
|
+
}
|
|
21
|
+
const lines = ["flowchart LR"];
|
|
22
|
+
for (const [nodeId, label] of [...labels.entries()].sort((left, right) => left[0].localeCompare(right[0]))) {
|
|
23
|
+
lines.push(` ${mermaidNodeId(nodeId)}["${escapeMermaidLabel(label)}"]`);
|
|
24
|
+
}
|
|
25
|
+
for (const step of trace.steps) {
|
|
26
|
+
const left = mermaidNodeId(step.fromId);
|
|
27
|
+
const right = mermaidNodeId(step.toId);
|
|
28
|
+
const label = `${step.edgeKind} / ${step.provenance} / ${step.confidence.toFixed(2)}`;
|
|
29
|
+
lines.push(` ${left} -- "${escapeMermaidLabel(label)}" --> ${right}`);
|
|
30
|
+
}
|
|
31
|
+
return lines.join("\n");
|
|
32
|
+
}
|
|
33
|
+
function mermaidNodeId(id) {
|
|
34
|
+
return `n_${id.replace(/[^A-Za-z0-9_]/g, "_")}`;
|
|
35
|
+
}
|
|
36
|
+
function escapeMermaidLabel(value) {
|
|
37
|
+
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\r?\n/g, " ");
|
|
38
|
+
}
|
|
39
|
+
function escapeMermaidComment(value) {
|
|
40
|
+
return value.replace(/\r?\n/g, " ").replace(/--/g, "- -");
|
|
41
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { GraphRepository, StatusCounts } from "../db/repositories.js";
|
|
2
|
+
import type { EdgeKind, Provenance } from "../types.js";
|
|
3
|
+
export declare const GRAPHJSON_FORMAT: "mdgraph-graphjson";
|
|
4
|
+
export declare const GRAPHJSON_FORMAT_VERSION: 1;
|
|
5
|
+
export type GraphJsonNode = {
|
|
6
|
+
id: string;
|
|
7
|
+
kind: "document";
|
|
8
|
+
label: string;
|
|
9
|
+
path: string;
|
|
10
|
+
documentType: string;
|
|
11
|
+
status: string;
|
|
12
|
+
trustTier: string;
|
|
13
|
+
} | {
|
|
14
|
+
id: string;
|
|
15
|
+
kind: "section";
|
|
16
|
+
label: string;
|
|
17
|
+
documentId: string;
|
|
18
|
+
anchor: string;
|
|
19
|
+
level: number;
|
|
20
|
+
lines: {
|
|
21
|
+
start: number;
|
|
22
|
+
end: number;
|
|
23
|
+
};
|
|
24
|
+
} | {
|
|
25
|
+
id: string;
|
|
26
|
+
kind: "entity";
|
|
27
|
+
label: string;
|
|
28
|
+
entityKind: string;
|
|
29
|
+
normalizedName: string;
|
|
30
|
+
namespace?: string;
|
|
31
|
+
} | {
|
|
32
|
+
id: string;
|
|
33
|
+
kind: "source_ref";
|
|
34
|
+
label: string;
|
|
35
|
+
path: string;
|
|
36
|
+
normalizedPath: string;
|
|
37
|
+
};
|
|
38
|
+
export interface GraphJsonEdge {
|
|
39
|
+
id: string;
|
|
40
|
+
fromId: string;
|
|
41
|
+
toId: string;
|
|
42
|
+
kind: EdgeKind;
|
|
43
|
+
weight: number;
|
|
44
|
+
confidence: number;
|
|
45
|
+
provenance: Provenance;
|
|
46
|
+
}
|
|
47
|
+
export interface GraphJsonExport {
|
|
48
|
+
format: typeof GRAPHJSON_FORMAT;
|
|
49
|
+
formatVersion: typeof GRAPHJSON_FORMAT_VERSION;
|
|
50
|
+
schemaVersion: number;
|
|
51
|
+
mdgraphVersion: string;
|
|
52
|
+
exportProfile: "structural";
|
|
53
|
+
graphHash: string;
|
|
54
|
+
sourceHash: string;
|
|
55
|
+
counts: StatusCounts;
|
|
56
|
+
exportedCounts: {
|
|
57
|
+
documents: number;
|
|
58
|
+
sections: number;
|
|
59
|
+
entities: number;
|
|
60
|
+
sourceRefs: number;
|
|
61
|
+
nodes: number;
|
|
62
|
+
edges: number;
|
|
63
|
+
};
|
|
64
|
+
nodes: GraphJsonNode[];
|
|
65
|
+
edges: GraphJsonEdge[];
|
|
66
|
+
}
|
|
67
|
+
export interface GraphJsonVerificationError {
|
|
68
|
+
code: string;
|
|
69
|
+
message: string;
|
|
70
|
+
evidence?: string;
|
|
71
|
+
remediation: string;
|
|
72
|
+
}
|
|
73
|
+
export interface GraphJsonVerificationResult {
|
|
74
|
+
valid: boolean;
|
|
75
|
+
errors: GraphJsonVerificationError[];
|
|
76
|
+
warnings: string[];
|
|
77
|
+
format?: string;
|
|
78
|
+
formatVersion?: number;
|
|
79
|
+
schemaVersion?: number;
|
|
80
|
+
graphHash?: string;
|
|
81
|
+
counts?: GraphJsonExport["counts"];
|
|
82
|
+
exportedCounts?: GraphJsonExport["exportedCounts"];
|
|
83
|
+
}
|
|
84
|
+
export declare function buildGraphJsonExport(projectRoot: string, repository: GraphRepository): GraphJsonExport;
|
|
85
|
+
export declare function readGraphJsonFile(filePath: string): unknown;
|
|
86
|
+
export declare function verifyGraphJsonExport(value: unknown): GraphJsonVerificationResult;
|
|
87
|
+
export declare function graphJsonHash(value: Record<string, unknown>): string;
|
|
88
|
+
export declare function formatGraphJsonVerification(result: GraphJsonVerificationResult): string;
|
|
89
|
+
export declare function stableGraphJson(value: GraphJsonExport): string;
|