@daogee/mdgraph 1.0.0 → 1.2.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 +51 -0
- package/README-ZH.md +66 -4
- package/README.md +84 -22
- package/agent-pack/README.md +2 -0
- package/agent-pack/mdgraph-agent-instructions.md +3 -1
- package/agent-pack/prompts/wiki-authoring.md +12 -0
- package/agent-pack/skills/wiki-authoring/SKILL.md +38 -0
- package/agent-pack/skills/wiki-authoring/agents/openai.yaml +4 -0
- package/dist/analysis/status-freshness.d.ts +8 -1
- package/dist/analysis/status-freshness.js +19 -6
- package/dist/bin/mdgraph.js +260 -25
- package/dist/config/limits.d.ts +2 -0
- package/dist/config/limits.js +3 -1
- package/dist/config/load-config.js +8 -2
- package/dist/db/connection.js +17 -1
- package/dist/db/repositories.d.ts +55 -4
- package/dist/db/repositories.js +458 -11
- package/dist/db/sqlite-adapter.js +8 -2
- package/dist/evaluation/context-packing-evaluation.d.ts +29 -0
- package/dist/evaluation/context-packing-evaluation.js +134 -0
- package/dist/evaluation/entity-extraction-evaluation.d.ts +38 -0
- package/dist/evaluation/entity-extraction-evaluation.js +118 -0
- package/dist/evaluation/historical-baseline.d.ts +70 -0
- package/dist/evaluation/historical-baseline.js +278 -0
- package/dist/evaluation/related-documents-evaluation.d.ts +26 -0
- package/dist/evaluation/related-documents-evaluation.js +163 -0
- package/dist/evaluation/retrieval-eval.d.ts +25 -5
- package/dist/evaluation/retrieval-eval.js +79 -7
- package/dist/evaluation/structured-query-evaluation.d.ts +27 -0
- package/dist/evaluation/structured-query-evaluation.js +156 -0
- package/dist/extraction/entity-extractor.js +42 -22
- package/dist/extraction/graph-builder.js +2 -18
- package/dist/index.d.ts +24 -4
- package/dist/index.js +12 -4
- package/dist/indexer.js +80 -4
- package/dist/mcp/server.d.ts +4 -0
- package/dist/mcp/server.js +21 -11
- package/dist/mcp/tools.d.ts +5 -1
- package/dist/mcp/tools.js +116 -18
- package/dist/parser/markdown-parser.js +2 -0
- package/dist/query/context-builder.d.ts +32 -2
- package/dist/query/context-builder.js +191 -12
- package/dist/query/knowledge-card.d.ts +90 -0
- package/dist/query/knowledge-card.js +676 -0
- package/dist/query/search.d.ts +5 -1
- package/dist/query/search.js +85 -16
- package/dist/query/structured-query-executor.d.ts +28 -0
- package/dist/query/structured-query-executor.js +138 -0
- package/dist/query/structured-query.d.ts +50 -0
- package/dist/query/structured-query.js +390 -0
- package/dist/relationships/derive-related.d.ts +86 -0
- package/dist/relationships/derive-related.js +322 -0
- package/dist/reporting/report.js +2 -2
- package/dist/semantic/embed-chunks.d.ts +2 -0
- package/dist/semantic/embed-chunks.js +31 -0
- package/dist/semantic/ollama-provider.d.ts +6 -0
- package/dist/semantic/ollama-provider.js +136 -0
- package/dist/semantic/provider-registry.d.ts +8 -0
- package/dist/semantic/provider-registry.js +66 -0
- package/dist/semantic/provider.d.ts +35 -0
- package/dist/semantic/provider.js +29 -0
- package/dist/semantic/status.d.ts +6 -1
- package/dist/semantic/status.js +49 -3
- package/dist/types.d.ts +7 -2
- package/dist/types.js +2 -1
- package/dist/utils/path-safety.d.ts +3 -0
- package/dist/utils/path-safety.js +27 -0
- package/dist/watcher/file-watcher.d.ts +4 -0
- package/dist/watcher/file-watcher.js +151 -17
- package/dist/watcher/watch-health.d.ts +34 -0
- package/dist/watcher/watch-health.js +75 -0
- package/dist/wiki/wiki-dependencies.d.ts +20 -0
- package/dist/wiki/wiki-dependencies.js +45 -0
- package/dist/wiki/wiki-domains.d.ts +13 -0
- package/dist/wiki/wiki-domains.js +55 -0
- package/dist/wiki/wiki-evidence.d.ts +27 -0
- package/dist/wiki/wiki-evidence.js +112 -0
- package/dist/wiki/wiki-plan.d.ts +121 -0
- package/dist/wiki/wiki-plan.js +616 -0
- package/dist/wiki/wiki-status.d.ts +67 -0
- package/dist/wiki/wiki-status.js +451 -0
- package/docs/EN/Agent_Integration.md +15 -5
- package/docs/EN/Architecture.md +52 -16
- package/docs/EN/Evaluation_Questions.md +35 -5
- package/docs/EN/Knowledge_Cards_and_Wiki_Workflow.md +97 -0
- package/docs/EN/Operations.md +111 -0
- package/docs/EN/Output_Contracts.md +83 -10
- package/docs/EN/Public_Contracts.md +25 -16
- package/docs/EN/Public_Contracts_1.0.md +32 -18
- package/docs/EN/README.md +24 -0
- package/docs/EN/Release_Checklist.md +27 -13
- package/docs/EN/Retrieval_and_Context.md +118 -0
- package/docs/EN/Structured_Query_and_Relationships.md +111 -0
- package/docs/ZH/Agent_Integration.md +15 -5
- package/docs/ZH/Architecture.md +52 -21
- package/docs/ZH/Evaluation_Questions.md +36 -6
- package/docs/ZH/Knowledge_Cards_and_Wiki_Workflow.md +99 -0
- package/docs/ZH/Operations.md +111 -0
- package/docs/ZH/Output_Contracts.md +83 -10
- package/docs/ZH/Public_Contracts.md +24 -15
- package/docs/ZH/Public_Contracts_1.0.md +31 -17
- package/docs/ZH/README.md +24 -0
- package/docs/ZH/Release_Checklist.md +27 -13
- package/docs/ZH/Retrieval_and_Context.md +118 -0
- package/docs/ZH/Structured_Query_and_Relationships.md +111 -0
- package/package.json +50 -19
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
class NodeSqliteAdapter {
|
|
3
|
+
closed = false;
|
|
4
|
+
legacyEmptyGet;
|
|
3
5
|
db;
|
|
4
6
|
constructor(dbPath) {
|
|
5
7
|
const require = createRequire(import.meta.url);
|
|
6
8
|
const { DatabaseSync } = require("node:sqlite");
|
|
7
9
|
this.db = new DatabaseSync(dbPath);
|
|
10
|
+
// Early node:sqlite returns a null-filled object instead of undefined for an
|
|
11
|
+
// empty get(). Probe the behavior rather than tying compatibility to versions.
|
|
12
|
+
this.legacyEmptyGet = this.db.prepare("SELECT 1 AS value WHERE 0").get() !== undefined;
|
|
8
13
|
}
|
|
9
14
|
prepare(sql) {
|
|
10
15
|
const statement = this.db.prepare(sql);
|
|
@@ -16,7 +21,7 @@ class NodeSqliteAdapter {
|
|
|
16
21
|
lastInsertRowid: result?.lastInsertRowid ?? 0
|
|
17
22
|
};
|
|
18
23
|
},
|
|
19
|
-
get: (...params) => statement.get(...params),
|
|
24
|
+
get: (...params) => this.legacyEmptyGet ? statement.all(...params)[0] : statement.get(...params),
|
|
20
25
|
all: (...params) => statement.all(...params)
|
|
21
26
|
};
|
|
22
27
|
}
|
|
@@ -50,8 +55,9 @@ class NodeSqliteAdapter {
|
|
|
50
55
|
};
|
|
51
56
|
}
|
|
52
57
|
close() {
|
|
53
|
-
if (this.
|
|
58
|
+
if (!this.closed) {
|
|
54
59
|
this.db.close();
|
|
60
|
+
this.closed = true;
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ContextDebug, type ContextPackingStrategy } from "../query/context-builder.js";
|
|
2
|
+
export declare const CONTEXT_PACKING_EVALUATION_VERSION = 1;
|
|
3
|
+
export declare const CONTEXT_PACKING_GATES: {
|
|
4
|
+
readonly minimumRedundancyReduction: 0.2;
|
|
5
|
+
readonly maximumDocumentRecallDrop: 0.02;
|
|
6
|
+
};
|
|
7
|
+
export interface ContextPackingMeasurement {
|
|
8
|
+
packingStrategy: ContextPackingStrategy;
|
|
9
|
+
itemCount: number;
|
|
10
|
+
uniqueDocuments: number;
|
|
11
|
+
expectedDocumentRecall: number;
|
|
12
|
+
averagePairwiseJaccard: number;
|
|
13
|
+
maximumPairwiseJaccard: number;
|
|
14
|
+
nearDuplicatePairs: number;
|
|
15
|
+
budgetFit: boolean;
|
|
16
|
+
packingSimilarity?: ContextDebug["packingSimilarity"];
|
|
17
|
+
redundancySkippedItems: number;
|
|
18
|
+
}
|
|
19
|
+
export interface ContextPackingEvaluationReport {
|
|
20
|
+
evaluationVersion: typeof CONTEXT_PACKING_EVALUATION_VERSION;
|
|
21
|
+
baseline: ContextPackingMeasurement;
|
|
22
|
+
mmr: ContextPackingMeasurement;
|
|
23
|
+
redundancyReduction: number;
|
|
24
|
+
documentRecallDrop: number;
|
|
25
|
+
gates: typeof CONTEXT_PACKING_GATES;
|
|
26
|
+
passed: boolean;
|
|
27
|
+
elapsedMs: number;
|
|
28
|
+
}
|
|
29
|
+
export declare function runContextPackingEvaluation(): Promise<ContextPackingEvaluationReport>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
5
|
+
import { DEFAULT_CONFIG } from "../config/load-config.js";
|
|
6
|
+
import { openDatabase } from "../db/connection.js";
|
|
7
|
+
import { GraphRepository } from "../db/repositories.js";
|
|
8
|
+
import { indexProject } from "../indexer.js";
|
|
9
|
+
import { buildContext } from "../query/context-builder.js";
|
|
10
|
+
export const CONTEXT_PACKING_EVALUATION_VERSION = 1;
|
|
11
|
+
export const CONTEXT_PACKING_GATES = {
|
|
12
|
+
minimumRedundancyReduction: 0.2,
|
|
13
|
+
maximumDocumentRecallDrop: 0.02
|
|
14
|
+
};
|
|
15
|
+
const EXPECTED_DOCUMENTS = [
|
|
16
|
+
"docs/recovery-details.md",
|
|
17
|
+
"docs/recovery-limits.md",
|
|
18
|
+
"docs/recovery-operations.md"
|
|
19
|
+
];
|
|
20
|
+
export async function runContextPackingEvaluation() {
|
|
21
|
+
const started = performance.now();
|
|
22
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mdgraph-context-packing-"));
|
|
23
|
+
try {
|
|
24
|
+
writeFixture(root);
|
|
25
|
+
await indexProject(root, { full: true });
|
|
26
|
+
const repository = new GraphRepository(openDatabase(root));
|
|
27
|
+
try {
|
|
28
|
+
const config = {
|
|
29
|
+
...DEFAULT_CONFIG,
|
|
30
|
+
search: { ...DEFAULT_CONFIG.search, defaultLimit: 12, maxContextChars: 50_000 }
|
|
31
|
+
};
|
|
32
|
+
const baseline = measure(repository, config, "mmr-style-document-round-robin");
|
|
33
|
+
const mmr = measure(repository, config, "mmr");
|
|
34
|
+
const redundancyReduction = baseline.averagePairwiseJaccard
|
|
35
|
+
? round((baseline.averagePairwiseJaccard - mmr.averagePairwiseJaccard) / baseline.averagePairwiseJaccard)
|
|
36
|
+
: 0;
|
|
37
|
+
const documentRecallDrop = round(Math.max(0, baseline.expectedDocumentRecall - mmr.expectedDocumentRecall));
|
|
38
|
+
const passed = redundancyReduction >= CONTEXT_PACKING_GATES.minimumRedundancyReduction
|
|
39
|
+
&& documentRecallDrop <= CONTEXT_PACKING_GATES.maximumDocumentRecallDrop
|
|
40
|
+
&& mmr.budgetFit;
|
|
41
|
+
return {
|
|
42
|
+
evaluationVersion: CONTEXT_PACKING_EVALUATION_VERSION,
|
|
43
|
+
baseline,
|
|
44
|
+
mmr,
|
|
45
|
+
redundancyReduction,
|
|
46
|
+
documentRecallDrop,
|
|
47
|
+
gates: CONTEXT_PACKING_GATES,
|
|
48
|
+
passed,
|
|
49
|
+
elapsedMs: Number((performance.now() - started).toFixed(2))
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
repository.close();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function measure(repository, config, packingStrategy) {
|
|
61
|
+
const context = buildContext(repository, config, "SessionRecoveryPolicy", {
|
|
62
|
+
debug: true,
|
|
63
|
+
searchLimit: 12,
|
|
64
|
+
maxChars: 50_000,
|
|
65
|
+
packingStrategy
|
|
66
|
+
});
|
|
67
|
+
const pairwise = pairwiseJaccard(context.items.map((item) => item.content));
|
|
68
|
+
const observedDocuments = new Set(context.items.map((item) => item.path));
|
|
69
|
+
return {
|
|
70
|
+
packingStrategy,
|
|
71
|
+
itemCount: context.items.length,
|
|
72
|
+
uniqueDocuments: observedDocuments.size,
|
|
73
|
+
expectedDocumentRecall: ratio(EXPECTED_DOCUMENTS.filter((document) => observedDocuments.has(document)).length, EXPECTED_DOCUMENTS.length),
|
|
74
|
+
averagePairwiseJaccard: average(pairwise),
|
|
75
|
+
maximumPairwiseJaccard: pairwise.length ? round(Math.max(...pairwise)) : 0,
|
|
76
|
+
nearDuplicatePairs: pairwise.filter((value) => value >= 0.8).length,
|
|
77
|
+
budgetFit: context.usedChars <= context.maxChars,
|
|
78
|
+
packingSimilarity: context.debug?.packingSimilarity,
|
|
79
|
+
redundancySkippedItems: context.debug?.redundancySkippedItems ?? 0
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function writeFixture(root) {
|
|
83
|
+
writeMarkdown(root, "docs/recovery-details.md", "Recovery Details", [
|
|
84
|
+
"## First Attempt",
|
|
85
|
+
"",
|
|
86
|
+
"`SessionRecoveryPolicy` retries authentication sessions after cache failures and records recovery diagnostics.",
|
|
87
|
+
"",
|
|
88
|
+
"## Second Attempt",
|
|
89
|
+
"",
|
|
90
|
+
"`SessionRecoveryPolicy` retries authentication sessions after cache failures and records recovery diagnostics.",
|
|
91
|
+
"",
|
|
92
|
+
"## Third Attempt",
|
|
93
|
+
"",
|
|
94
|
+
"`SessionRecoveryPolicy` retries authentication sessions after cache failures and records recovery diagnostics."
|
|
95
|
+
].join("\n"));
|
|
96
|
+
writeMarkdown(root, "docs/recovery-limits.md", "Recovery Limits", "`SessionRecoveryPolicy` caps retries before returning a terminal authentication response.");
|
|
97
|
+
writeMarkdown(root, "docs/recovery-operations.md", "Recovery Operations", "`SessionRecoveryPolicy` routes exhausted attempts to operational diagnostics and an incident runbook.");
|
|
98
|
+
}
|
|
99
|
+
function writeMarkdown(root, relativePath, title, content) {
|
|
100
|
+
const target = path.join(root, relativePath);
|
|
101
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
102
|
+
fs.writeFileSync(target, `# ${title}\n\n${content}\n`, "utf8");
|
|
103
|
+
}
|
|
104
|
+
function pairwiseJaccard(contents) {
|
|
105
|
+
const tokenSets = contents.map(tokensForSimilarity);
|
|
106
|
+
const similarities = [];
|
|
107
|
+
for (let left = 0; left < tokenSets.length; left += 1) {
|
|
108
|
+
for (let right = left + 1; right < tokenSets.length; right += 1) {
|
|
109
|
+
similarities.push(jaccard(tokenSets[left], tokenSets[right]));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return similarities;
|
|
113
|
+
}
|
|
114
|
+
function tokensForSimilarity(content) {
|
|
115
|
+
return new Set(content.toLowerCase().match(/[\p{L}\p{N}_]+/gu) ?? []);
|
|
116
|
+
}
|
|
117
|
+
function jaccard(left, right) {
|
|
118
|
+
let intersection = 0;
|
|
119
|
+
for (const token of left) {
|
|
120
|
+
if (right.has(token)) {
|
|
121
|
+
intersection += 1;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return ratio(intersection, new Set([...left, ...right]).size);
|
|
125
|
+
}
|
|
126
|
+
function average(values) {
|
|
127
|
+
return values.length ? round(values.reduce((sum, value) => sum + value, 0) / values.length) : 0;
|
|
128
|
+
}
|
|
129
|
+
function ratio(numerator, denominator) {
|
|
130
|
+
return denominator ? round(numerator / denominator) : 0;
|
|
131
|
+
}
|
|
132
|
+
function round(value) {
|
|
133
|
+
return Number(value.toFixed(4));
|
|
134
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { EntityKind } from "../types.js";
|
|
2
|
+
export declare const ENTITY_EXTRACTION_EVALUATION_VERSION = 1;
|
|
3
|
+
export declare const ENTITY_EXTRACTION_GATES: {
|
|
4
|
+
readonly minimumPrecision: 0.9;
|
|
5
|
+
readonly minimumRecall: 0.8;
|
|
6
|
+
readonly minimumCjkRecall: 0.8;
|
|
7
|
+
};
|
|
8
|
+
export interface EntityExtractionExpectedEntity {
|
|
9
|
+
name: string;
|
|
10
|
+
kind: EntityKind;
|
|
11
|
+
language: "latin" | "cjk";
|
|
12
|
+
}
|
|
13
|
+
export interface EntityExtractionEvaluationReport {
|
|
14
|
+
evaluationVersion: typeof ENTITY_EXTRACTION_EVALUATION_VERSION;
|
|
15
|
+
expected: EntityExtractionExpectedEntity[];
|
|
16
|
+
extracted: Array<{
|
|
17
|
+
name: string;
|
|
18
|
+
kind: EntityKind;
|
|
19
|
+
}>;
|
|
20
|
+
truePositives: Array<{
|
|
21
|
+
name: string;
|
|
22
|
+
kind: EntityKind;
|
|
23
|
+
}>;
|
|
24
|
+
falsePositives: Array<{
|
|
25
|
+
name: string;
|
|
26
|
+
kind: EntityKind;
|
|
27
|
+
}>;
|
|
28
|
+
falseNegatives: EntityExtractionExpectedEntity[];
|
|
29
|
+
expectedNoise: string[];
|
|
30
|
+
leakedNoise: string[];
|
|
31
|
+
precision: number;
|
|
32
|
+
recall: number;
|
|
33
|
+
cjkRecall: number;
|
|
34
|
+
elapsedMs: number;
|
|
35
|
+
}
|
|
36
|
+
export declare const ENTITY_EXTRACTION_EXPECTED: EntityExtractionExpectedEntity[];
|
|
37
|
+
export declare const ENTITY_EXTRACTION_NOISE: readonly ["/auth/session.ts", "Design", "Do", "Fall", "GET", "Include", "Note", "Prefer", "Reason", "Should", "Start", "The", "Use", "When"];
|
|
38
|
+
export declare function runEntityExtractionEvaluation(): EntityExtractionEvaluationReport;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { performance } from "node:perf_hooks";
|
|
5
|
+
import { DEFAULT_CONFIG } from "../config/load-config.js";
|
|
6
|
+
import { extractEntities } from "../extraction/entity-extractor.js";
|
|
7
|
+
import { parseMarkdownDocument } from "../parser/markdown-parser.js";
|
|
8
|
+
import { normalizeEntityName } from "../utils/text.js";
|
|
9
|
+
export const ENTITY_EXTRACTION_EVALUATION_VERSION = 1;
|
|
10
|
+
export const ENTITY_EXTRACTION_GATES = {
|
|
11
|
+
minimumPrecision: 0.9,
|
|
12
|
+
minimumRecall: 0.8,
|
|
13
|
+
minimumCjkRecall: 0.8
|
|
14
|
+
};
|
|
15
|
+
export const ENTITY_EXTRACTION_EXPECTED = [
|
|
16
|
+
{ name: "AuthCoordinator", kind: "symbol", language: "latin" },
|
|
17
|
+
{ name: "AuthService", kind: "symbol", language: "latin" },
|
|
18
|
+
{ name: "GET /api/auth/login", kind: "api_route", language: "latin" },
|
|
19
|
+
{ name: "JWT_SECRET", kind: "config_key", language: "latin" },
|
|
20
|
+
{ name: "src/auth/session.ts", kind: "file_path", language: "latin" },
|
|
21
|
+
{ name: "RedisTimeoutError", kind: "error_code", language: "latin" },
|
|
22
|
+
{ name: "loadSession()", kind: "symbol", language: "latin" },
|
|
23
|
+
{ name: "SessionRepository", kind: "symbol", language: "latin" },
|
|
24
|
+
{ name: "AuthAdapter", kind: "symbol", language: "latin" },
|
|
25
|
+
{ name: "rotateToken()", kind: "symbol", language: "latin" },
|
|
26
|
+
{ name: "登录服务", kind: "symbol", language: "cjk" },
|
|
27
|
+
{ name: "POST /接口/登录", kind: "api_route", language: "cjk" },
|
|
28
|
+
{ name: "登录.认证.重试", kind: "config_key", language: "cjk" },
|
|
29
|
+
{ name: "验证登录()", kind: "symbol", language: "cjk" },
|
|
30
|
+
{ name: "認証を確認()", kind: "symbol", language: "cjk" },
|
|
31
|
+
{ name: "验证会话()", kind: "symbol", language: "cjk" }
|
|
32
|
+
];
|
|
33
|
+
export const ENTITY_EXTRACTION_NOISE = [
|
|
34
|
+
"/auth/session.ts",
|
|
35
|
+
"Design",
|
|
36
|
+
"Do",
|
|
37
|
+
"Fall",
|
|
38
|
+
"GET",
|
|
39
|
+
"Include",
|
|
40
|
+
"Note",
|
|
41
|
+
"Prefer",
|
|
42
|
+
"Reason",
|
|
43
|
+
"Should",
|
|
44
|
+
"Start",
|
|
45
|
+
"The",
|
|
46
|
+
"Use",
|
|
47
|
+
"When"
|
|
48
|
+
];
|
|
49
|
+
export function runEntityExtractionEvaluation() {
|
|
50
|
+
const started = performance.now();
|
|
51
|
+
const root = fs.mkdtempSync(path.join(os.tmpdir(), "mdgraph-entity-extraction-"));
|
|
52
|
+
try {
|
|
53
|
+
const target = path.join(root, "docs", "entity-evaluation.md");
|
|
54
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
55
|
+
fs.writeFileSync(target, evaluationDocument(), "utf8");
|
|
56
|
+
const entities = extractEntities(parseMarkdownDocument(root, target), DEFAULT_CONFIG);
|
|
57
|
+
const extracted = uniqueEntities(entities.map(({ name, kind }) => ({ name, kind })));
|
|
58
|
+
const expectedByKey = new Map(ENTITY_EXTRACTION_EXPECTED.map((entity) => [entityKey(entity), entity]));
|
|
59
|
+
const extractedByKey = new Map(extracted.map((entity) => [entityKey(entity), entity]));
|
|
60
|
+
const truePositives = extracted.filter((entity) => expectedByKey.has(entityKey(entity)));
|
|
61
|
+
const falsePositives = extracted.filter((entity) => !expectedByKey.has(entityKey(entity)));
|
|
62
|
+
const falseNegatives = ENTITY_EXTRACTION_EXPECTED.filter((entity) => !extractedByKey.has(entityKey(entity)));
|
|
63
|
+
const extractedNames = new Set(extracted.map((entity) => normalizeEntityName(entity.name)));
|
|
64
|
+
const cjkExpected = ENTITY_EXTRACTION_EXPECTED.filter((entity) => entity.language === "cjk");
|
|
65
|
+
return {
|
|
66
|
+
evaluationVersion: ENTITY_EXTRACTION_EVALUATION_VERSION,
|
|
67
|
+
expected: ENTITY_EXTRACTION_EXPECTED,
|
|
68
|
+
extracted,
|
|
69
|
+
truePositives,
|
|
70
|
+
falsePositives,
|
|
71
|
+
falseNegatives,
|
|
72
|
+
expectedNoise: [...ENTITY_EXTRACTION_NOISE],
|
|
73
|
+
leakedNoise: ENTITY_EXTRACTION_NOISE.filter((name) => extractedNames.has(normalizeEntityName(name))),
|
|
74
|
+
precision: ratio(truePositives.length, extracted.length),
|
|
75
|
+
recall: ratio(truePositives.length, ENTITY_EXTRACTION_EXPECTED.length),
|
|
76
|
+
cjkRecall: ratio(cjkExpected.filter((entity) => extractedByKey.has(entityKey(entity))).length, cjkExpected.length),
|
|
77
|
+
elapsedMs: Number((performance.now() - started).toFixed(2))
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
fs.rmSync(root, { recursive: true, force: true });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function evaluationDocument() {
|
|
85
|
+
return [
|
|
86
|
+
"---",
|
|
87
|
+
"title: Entity Extraction Evaluation",
|
|
88
|
+
"defines: [AuthCoordinator, 登录服务]",
|
|
89
|
+
"---",
|
|
90
|
+
"# Entity Extraction Evaluation",
|
|
91
|
+
"",
|
|
92
|
+
"Ordinary prose says The, When, Note, Design, and Should without declaring symbols.",
|
|
93
|
+
"",
|
|
94
|
+
"## Runtime Signals",
|
|
95
|
+
"",
|
|
96
|
+
"Use `AuthService`, `GET /api/auth/login`, `JWT_SECRET`, `src/auth/session.ts`, `RedisTimeoutError`, and `loadSession()`.",
|
|
97
|
+
"CJK code references `登录服务`, `POST /接口/登录`, `登录.认证.重试`, `验证登录()`, and `認証を確認()`.",
|
|
98
|
+
"",
|
|
99
|
+
"```ts",
|
|
100
|
+
"Do Prefer Use Fall Include Start Reason",
|
|
101
|
+
"class SessionRepository {}",
|
|
102
|
+
"interface AuthAdapter {}",
|
|
103
|
+
"function rotateToken() {}",
|
|
104
|
+
"function 验证会话() {}",
|
|
105
|
+
"```",
|
|
106
|
+
""
|
|
107
|
+
].join("\n");
|
|
108
|
+
}
|
|
109
|
+
function uniqueEntities(values) {
|
|
110
|
+
return [...new Map(values.map((entity) => [entityKey(entity), entity])).values()]
|
|
111
|
+
.sort((left, right) => entityKey(left).localeCompare(entityKey(right)));
|
|
112
|
+
}
|
|
113
|
+
function entityKey(entity) {
|
|
114
|
+
return `${entity.kind}:${normalizeEntityName(entity.name)}`;
|
|
115
|
+
}
|
|
116
|
+
function ratio(numerator, denominator) {
|
|
117
|
+
return denominator ? Number((numerator / denominator).toFixed(4)) : 0;
|
|
118
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export declare const HISTORICAL_BASELINE_VERSION = 1;
|
|
2
|
+
export interface SynonymRetrievalBaselineCase {
|
|
3
|
+
id: string;
|
|
4
|
+
query: string;
|
|
5
|
+
expectedDocument: string;
|
|
6
|
+
title: string;
|
|
7
|
+
content: string;
|
|
8
|
+
}
|
|
9
|
+
export interface WatcherFailureBaselineCase {
|
|
10
|
+
code: "ENOSPC" | "EMFILE" | "EACCES" | "INDEX_ERROR";
|
|
11
|
+
phase: "startup" | "runtime" | "indexing";
|
|
12
|
+
expectedFuturePolicy: "fatal" | "degraded";
|
|
13
|
+
reason: string;
|
|
14
|
+
}
|
|
15
|
+
export interface HistoricalBaselineReport {
|
|
16
|
+
baselineVersion: typeof HISTORICAL_BASELINE_VERSION;
|
|
17
|
+
runtime: {
|
|
18
|
+
nodeVersion: string;
|
|
19
|
+
platform: NodeJS.Platform;
|
|
20
|
+
architecture: string;
|
|
21
|
+
};
|
|
22
|
+
semantic: {
|
|
23
|
+
provider: "local-hash";
|
|
24
|
+
dimensions: number;
|
|
25
|
+
cases: Array<{
|
|
26
|
+
id: string;
|
|
27
|
+
query: string;
|
|
28
|
+
expectedDocument: string;
|
|
29
|
+
keywordRank: number | null;
|
|
30
|
+
localHashRank: number | null;
|
|
31
|
+
}>;
|
|
32
|
+
keywordRecallAt5: number;
|
|
33
|
+
localHashRecallAt5: number;
|
|
34
|
+
elapsedMs: number;
|
|
35
|
+
};
|
|
36
|
+
entities: {
|
|
37
|
+
expected: string[];
|
|
38
|
+
extracted: string[];
|
|
39
|
+
truePositives: string[];
|
|
40
|
+
falsePositives: string[];
|
|
41
|
+
falseNegatives: string[];
|
|
42
|
+
precision: number;
|
|
43
|
+
recall: number;
|
|
44
|
+
elapsedMs: number;
|
|
45
|
+
};
|
|
46
|
+
context: {
|
|
47
|
+
packingStrategy: "mmr-style-document-round-robin";
|
|
48
|
+
itemCount: number;
|
|
49
|
+
uniqueDocuments: number;
|
|
50
|
+
diversityRatio: number;
|
|
51
|
+
averagePairwiseJaccard: number;
|
|
52
|
+
maximumPairwiseJaccard: number;
|
|
53
|
+
nearDuplicatePairs: number;
|
|
54
|
+
elapsedMs: number;
|
|
55
|
+
};
|
|
56
|
+
watch: {
|
|
57
|
+
cases: WatcherFailureBaselineCase[];
|
|
58
|
+
currentPolicy: {
|
|
59
|
+
errorSignal: "onError_callback";
|
|
60
|
+
persistentHealth: false;
|
|
61
|
+
fatalRuntimeErrors: false;
|
|
62
|
+
pollingFallback: false;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export declare const SYNONYM_RETRIEVAL_BASELINE_CASES: SynonymRetrievalBaselineCase[];
|
|
67
|
+
export declare const ENTITY_EXTRACTION_BASELINE_EXPECTED: readonly ["AuthCoordinator", "AuthService", "GET /api/auth/login", "JWT_SECRET", "RedisTimeoutError", "SessionRepository", "loadSession()", "rotateToken()", "src/auth/session.ts"];
|
|
68
|
+
export declare const ENTITY_EXTRACTION_BASELINE_FALSE_POSITIVES: readonly ["/auth/session.ts", "Do", "Fall", "GET", "Include", "Prefer", "Reason", "Start", "Use"];
|
|
69
|
+
export declare const WATCHER_FAILURE_BASELINE_CASES: WatcherFailureBaselineCase[];
|
|
70
|
+
export declare function runHistoricalBaseline(): Promise<HistoricalBaselineReport>;
|