@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.
Files changed (106) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README-ZH.md +66 -4
  3. package/README.md +84 -22
  4. package/agent-pack/README.md +2 -0
  5. package/agent-pack/mdgraph-agent-instructions.md +3 -1
  6. package/agent-pack/prompts/wiki-authoring.md +12 -0
  7. package/agent-pack/skills/wiki-authoring/SKILL.md +38 -0
  8. package/agent-pack/skills/wiki-authoring/agents/openai.yaml +4 -0
  9. package/dist/analysis/status-freshness.d.ts +8 -1
  10. package/dist/analysis/status-freshness.js +19 -6
  11. package/dist/bin/mdgraph.js +260 -25
  12. package/dist/config/limits.d.ts +2 -0
  13. package/dist/config/limits.js +3 -1
  14. package/dist/config/load-config.js +8 -2
  15. package/dist/db/connection.js +17 -1
  16. package/dist/db/repositories.d.ts +55 -4
  17. package/dist/db/repositories.js +458 -11
  18. package/dist/db/sqlite-adapter.js +8 -2
  19. package/dist/evaluation/context-packing-evaluation.d.ts +29 -0
  20. package/dist/evaluation/context-packing-evaluation.js +134 -0
  21. package/dist/evaluation/entity-extraction-evaluation.d.ts +38 -0
  22. package/dist/evaluation/entity-extraction-evaluation.js +118 -0
  23. package/dist/evaluation/historical-baseline.d.ts +70 -0
  24. package/dist/evaluation/historical-baseline.js +278 -0
  25. package/dist/evaluation/related-documents-evaluation.d.ts +26 -0
  26. package/dist/evaluation/related-documents-evaluation.js +163 -0
  27. package/dist/evaluation/retrieval-eval.d.ts +25 -5
  28. package/dist/evaluation/retrieval-eval.js +79 -7
  29. package/dist/evaluation/structured-query-evaluation.d.ts +27 -0
  30. package/dist/evaluation/structured-query-evaluation.js +156 -0
  31. package/dist/extraction/entity-extractor.js +42 -22
  32. package/dist/extraction/graph-builder.js +2 -18
  33. package/dist/index.d.ts +24 -4
  34. package/dist/index.js +12 -4
  35. package/dist/indexer.js +80 -4
  36. package/dist/mcp/server.d.ts +4 -0
  37. package/dist/mcp/server.js +21 -11
  38. package/dist/mcp/tools.d.ts +5 -1
  39. package/dist/mcp/tools.js +116 -18
  40. package/dist/parser/markdown-parser.js +2 -0
  41. package/dist/query/context-builder.d.ts +32 -2
  42. package/dist/query/context-builder.js +191 -12
  43. package/dist/query/knowledge-card.d.ts +90 -0
  44. package/dist/query/knowledge-card.js +676 -0
  45. package/dist/query/search.d.ts +5 -1
  46. package/dist/query/search.js +85 -16
  47. package/dist/query/structured-query-executor.d.ts +28 -0
  48. package/dist/query/structured-query-executor.js +138 -0
  49. package/dist/query/structured-query.d.ts +50 -0
  50. package/dist/query/structured-query.js +390 -0
  51. package/dist/relationships/derive-related.d.ts +86 -0
  52. package/dist/relationships/derive-related.js +322 -0
  53. package/dist/reporting/report.js +2 -2
  54. package/dist/semantic/embed-chunks.d.ts +2 -0
  55. package/dist/semantic/embed-chunks.js +31 -0
  56. package/dist/semantic/ollama-provider.d.ts +6 -0
  57. package/dist/semantic/ollama-provider.js +136 -0
  58. package/dist/semantic/provider-registry.d.ts +8 -0
  59. package/dist/semantic/provider-registry.js +66 -0
  60. package/dist/semantic/provider.d.ts +35 -0
  61. package/dist/semantic/provider.js +29 -0
  62. package/dist/semantic/status.d.ts +6 -1
  63. package/dist/semantic/status.js +49 -3
  64. package/dist/types.d.ts +7 -2
  65. package/dist/types.js +2 -1
  66. package/dist/utils/path-safety.d.ts +3 -0
  67. package/dist/utils/path-safety.js +27 -0
  68. package/dist/watcher/file-watcher.d.ts +4 -0
  69. package/dist/watcher/file-watcher.js +151 -17
  70. package/dist/watcher/watch-health.d.ts +34 -0
  71. package/dist/watcher/watch-health.js +75 -0
  72. package/dist/wiki/wiki-dependencies.d.ts +20 -0
  73. package/dist/wiki/wiki-dependencies.js +45 -0
  74. package/dist/wiki/wiki-domains.d.ts +13 -0
  75. package/dist/wiki/wiki-domains.js +55 -0
  76. package/dist/wiki/wiki-evidence.d.ts +27 -0
  77. package/dist/wiki/wiki-evidence.js +112 -0
  78. package/dist/wiki/wiki-plan.d.ts +121 -0
  79. package/dist/wiki/wiki-plan.js +616 -0
  80. package/dist/wiki/wiki-status.d.ts +67 -0
  81. package/dist/wiki/wiki-status.js +451 -0
  82. package/docs/EN/Agent_Integration.md +15 -5
  83. package/docs/EN/Architecture.md +52 -16
  84. package/docs/EN/Evaluation_Questions.md +35 -5
  85. package/docs/EN/Knowledge_Cards_and_Wiki_Workflow.md +97 -0
  86. package/docs/EN/Operations.md +111 -0
  87. package/docs/EN/Output_Contracts.md +83 -10
  88. package/docs/EN/Public_Contracts.md +25 -16
  89. package/docs/EN/Public_Contracts_1.0.md +32 -18
  90. package/docs/EN/README.md +24 -0
  91. package/docs/EN/Release_Checklist.md +27 -13
  92. package/docs/EN/Retrieval_and_Context.md +118 -0
  93. package/docs/EN/Structured_Query_and_Relationships.md +111 -0
  94. package/docs/ZH/Agent_Integration.md +15 -5
  95. package/docs/ZH/Architecture.md +52 -21
  96. package/docs/ZH/Evaluation_Questions.md +36 -6
  97. package/docs/ZH/Knowledge_Cards_and_Wiki_Workflow.md +99 -0
  98. package/docs/ZH/Operations.md +111 -0
  99. package/docs/ZH/Output_Contracts.md +83 -10
  100. package/docs/ZH/Public_Contracts.md +24 -15
  101. package/docs/ZH/Public_Contracts_1.0.md +31 -17
  102. package/docs/ZH/README.md +24 -0
  103. package/docs/ZH/Release_Checklist.md +27 -13
  104. package/docs/ZH/Retrieval_and_Context.md +118 -0
  105. package/docs/ZH/Structured_Query_and_Relationships.md +111 -0
  106. package/package.json +50 -19
@@ -0,0 +1,55 @@
1
+ const words = (document, values) => {
2
+ const text = `${document.title} ${document.path}`.toLowerCase();
3
+ return values.reduce((score, word) => score + (text.includes(word) ? 3 : 0), 0);
4
+ };
5
+ const basename = (document, names) => names.includes(document.path.split("/").at(-1).toLowerCase()) ? 12 : 0;
6
+ export const WIKI_DOMAINS = [
7
+ {
8
+ id: "project-overview", title: "Project Overview", path: "index.md",
9
+ purpose: "Explain what this project does, who it serves, how to start, and where to find the next useful instructions.",
10
+ audience: "New users and contributors evaluating or starting with the project.",
11
+ outline: ["Purpose and audience", "Core concepts", "Getting started", "First successful task", "Next steps"],
12
+ evidenceQueries: ["project purpose getting started"],
13
+ score: (document) => basename(document, ["readme.md", "readme-zh.md"]) + words(document, ["overview", "introduction", "quickstart", "概览", "快速开始"])
14
+ },
15
+ {
16
+ id: "architecture", title: "Architecture", path: "architecture.md",
17
+ purpose: "Explain implemented components, their responsibilities, data flow, and the decisions behind their boundaries.",
18
+ audience: "Contributors changing behavior across components.",
19
+ outline: ["System boundary", "Components", "Data flow", "Decisions", "Extension points"],
20
+ evidenceQueries: ["architecture components decisions"],
21
+ score: (document) => (document.type === "adr" ? 8 : document.type === "design" ? 5 : 0) + words(document, ["architecture", "design", "decision", "架构", "设计", "决策"])
22
+ },
23
+ {
24
+ id: "core-workflows", title: "Core Workflows", path: "core-workflows.md",
25
+ purpose: "Describe the project's main user tasks, their prerequisites, successful outcomes, and recovery steps.",
26
+ audience: "Users applying the project to real tasks.",
27
+ outline: ["Main tasks", "Prerequisites", "Steps and outcomes", "Failure recovery", "Related tasks"],
28
+ evidenceQueries: ["usage workflow tasks"],
29
+ score: (document) => (document.type === "spec" ? 2 : 0) + words(document, ["workflow", "usage", "tutorial", "guide", "工作流", "使用", "教程"])
30
+ },
31
+ {
32
+ id: "development", title: "Development Guide", path: "development.md",
33
+ purpose: "Explain contributor setup, repository conventions, checks, and the change workflow.",
34
+ audience: "Contributors implementing and reviewing changes.",
35
+ outline: ["Setup", "Conventions", "Build and test", "Change workflow", "Review"],
36
+ evidenceQueries: ["development contribution build test"],
37
+ score: (document) => basename(document, ["agents.md", "contributing.md"]) + words(document, ["development", "contributing", "testing", "开发", "贡献", "测试"])
38
+ },
39
+ {
40
+ id: "operations", title: "Operations and Troubleshooting", path: "operations.md",
41
+ purpose: "Explain how to run the project, inspect failures, and recover normal operation.",
42
+ audience: "Users and maintainers operating or troubleshooting the project.",
43
+ outline: ["Running the project", "Diagnostics", "Common failures", "Recovery", "Maintenance"],
44
+ evidenceQueries: ["operations troubleshooting recovery"],
45
+ score: (document) => (document.type === "runbook" || document.type === "incident" ? 8 : 0) + words(document, ["operations", "runbook", "incident", "troubleshoot", "运维", "排障", "故障"])
46
+ },
47
+ {
48
+ id: "reference", title: "Reference", path: "reference.md",
49
+ purpose: "Locate exact public interfaces, configuration, compatibility requirements, and input/output contracts.",
50
+ audience: "Users and integrators needing precise interface details.",
51
+ outline: ["Public interfaces", "Configuration", "Inputs and outputs", "Compatibility", "Examples"],
52
+ evidenceQueries: ["reference interfaces configuration contracts"],
53
+ score: (document) => (document.type === "api" ? 7 : 0) + words(document, ["reference", "contract", "configuration", "参考", "契约", "配置"])
54
+ }
55
+ ];
@@ -0,0 +1,27 @@
1
+ import { type StatusFreshness } from "../analysis/status-freshness.js";
2
+ import type { GraphRepository } from "../db/repositories.js";
3
+ export interface WikiEvidencePage {
4
+ id: string;
5
+ title: string;
6
+ path: string;
7
+ parentId?: string;
8
+ purpose: string;
9
+ audience: string;
10
+ outline: string[];
11
+ documentIds: string[];
12
+ sourceRefs: string[];
13
+ evidenceQueries: string[];
14
+ }
15
+ export interface WikiContentFreshnessOptions {
16
+ wikiDir?: string;
17
+ }
18
+ export interface WikiPageEvidenceStatus {
19
+ currentEvidenceHash: string;
20
+ staleDocumentPaths: string[];
21
+ }
22
+ /** Returns the strict on-disk Markdown evidence used by Wiki plan, brief, and status. */
23
+ export declare function wikiContentFreshness(projectRoot: string, repository: GraphRepository, options?: WikiContentFreshnessOptions): StatusFreshness;
24
+ export declare function calculateWikiPageEvidenceHash(projectRoot: string, repository: GraphRepository, page: WikiEvidencePage): string;
25
+ export declare function wikiPageEvidenceStatus(projectRoot: string, repository: GraphRepository, page: WikiEvidencePage, freshness: StatusFreshness): WikiPageEvidenceStatus;
26
+ export declare function sourceRefFingerprint(projectRoot: string, sourceRef: string): string;
27
+ export declare function safeProjectRelativePath(value: string): string | undefined;
@@ -0,0 +1,112 @@
1
+ import { createHash } from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { computeStatusFreshness } from "../analysis/status-freshness.js";
5
+ import { hashCanonical } from "../bundle/bundle.js";
6
+ import { loadConfig } from "../config/load-config.js";
7
+ import { isPathInsideOrEqual, resolveInsideRoot } from "../utils/path-safety.js";
8
+ import { normalizePath } from "../utils/text.js";
9
+ /** Returns the strict on-disk Markdown evidence used by Wiki plan, brief, and status. */
10
+ export function wikiContentFreshness(projectRoot, repository, options = {}) {
11
+ const wikiRelativePath = options.wikiDir && isPathInsideOrEqual(projectRoot, options.wikiDir)
12
+ ? normalizePath(path.relative(projectRoot, options.wikiDir))
13
+ : undefined;
14
+ return computeStatusFreshness(projectRoot, loadConfig(projectRoot), repository, {
15
+ strictContentHash: true,
16
+ ignorePaths: wikiRelativePath && wikiRelativePath !== "." ? [wikiRelativePath] : undefined
17
+ });
18
+ }
19
+ export function calculateWikiPageEvidenceHash(projectRoot, repository, page) {
20
+ const documentsById = new Map(repository.allDocuments().map((document) => [document.id, document]));
21
+ const documents = page.documentIds.map((documentId) => {
22
+ const document = documentsById.get(documentId);
23
+ return document
24
+ ? { id: document.id, path: document.path, hash: document.hash }
25
+ : { id: documentId, missing: true };
26
+ });
27
+ const sourceRefs = page.sourceRefs.map((sourceRef) => ({
28
+ path: sourceRef,
29
+ fingerprint: sourceRefFingerprint(projectRoot, sourceRef)
30
+ }));
31
+ return hashCanonical({
32
+ page: {
33
+ id: page.id,
34
+ title: page.title,
35
+ path: page.path,
36
+ parentId: page.parentId,
37
+ purpose: page.purpose,
38
+ audience: page.audience,
39
+ outline: page.outline,
40
+ documentIds: page.documentIds,
41
+ sourceRefs: page.sourceRefs,
42
+ evidenceQueries: page.evidenceQueries
43
+ },
44
+ documents,
45
+ sourceRefs
46
+ });
47
+ }
48
+ export function wikiPageEvidenceStatus(projectRoot, repository, page, freshness) {
49
+ const documentsById = new Map(repository.allDocuments().map((document) => [document.id, document.path]));
50
+ const stalePaths = new Set(freshness.issues
51
+ ?.filter((issue) => issue.reason === "modified" || issue.reason === "deleted")
52
+ .map((issue) => issue.path) ?? []);
53
+ return {
54
+ currentEvidenceHash: calculateWikiPageEvidenceHash(projectRoot, repository, page),
55
+ staleDocumentPaths: page.documentIds.flatMap((documentId) => {
56
+ const documentPath = documentsById.get(documentId);
57
+ return documentPath && stalePaths.has(documentPath) ? [documentPath] : [];
58
+ })
59
+ };
60
+ }
61
+ export function sourceRefFingerprint(projectRoot, sourceRef) {
62
+ const normalized = safeProjectRelativePath(sourceRef);
63
+ if (!normalized)
64
+ return "unsafe";
65
+ try {
66
+ const resolved = resolveInsideRoot(projectRoot, normalized);
67
+ if (!resolved || !fs.existsSync(resolved))
68
+ return "missing";
69
+ const stat = fs.lstatSync(resolved);
70
+ if (stat.isSymbolicLink()) {
71
+ let realPath;
72
+ try {
73
+ realPath = fs.realpathSync(resolved);
74
+ }
75
+ catch {
76
+ return "broken-symlink";
77
+ }
78
+ if (!isPathInsideOrEqual(projectRoot, realPath))
79
+ return "unsafe-symlink";
80
+ }
81
+ if (!stat.isFile() && !stat.isSymbolicLink())
82
+ return "not-file";
83
+ return hashFile(resolved);
84
+ }
85
+ catch {
86
+ return "unreadable";
87
+ }
88
+ }
89
+ export function safeProjectRelativePath(value) {
90
+ const normalized = normalizePath(value.trim()).replace(/^\.\//, "");
91
+ const segments = normalized.split("/");
92
+ return !normalized || normalized.includes("\0") || path.isAbsolute(normalized) || path.posix.isAbsolute(normalized) || /^[a-z]:\//i.test(normalized) || segments.some((segment) => !segment || segment === "." || segment === "..")
93
+ ? undefined
94
+ : normalized;
95
+ }
96
+ function hashFile(filePath) {
97
+ const hash = createHash("sha256");
98
+ const buffer = Buffer.allocUnsafe(64 * 1024);
99
+ const descriptor = fs.openSync(filePath, "r");
100
+ try {
101
+ let bytesRead = 0;
102
+ do {
103
+ bytesRead = fs.readSync(descriptor, buffer, 0, buffer.length, null);
104
+ if (bytesRead > 0)
105
+ hash.update(buffer.subarray(0, bytesRead));
106
+ } while (bytesRead > 0);
107
+ }
108
+ finally {
109
+ fs.closeSync(descriptor);
110
+ }
111
+ return hash.digest("hex");
112
+ }
@@ -0,0 +1,121 @@
1
+ import { GraphRepository } from "../db/repositories.js";
2
+ import { type StatusFreshness } from "../analysis/status-freshness.js";
3
+ import { type ContextItem } from "../query/context-builder.js";
4
+ import { type KnowledgeCard } from "../query/knowledge-card.js";
5
+ import type { GraphDocument, MDGraphConfig } from "../types.js";
6
+ import { type WikiDependencyAssessment } from "./wiki-dependencies.js";
7
+ export { calculateWikiPageEvidenceHash, safeProjectRelativePath, sourceRefFingerprint } from "./wiki-evidence.js";
8
+ export declare const WIKI_PLAN_FORMAT: "mdgraph-wiki-plan";
9
+ export declare const WIKI_PLAN_FORMAT_VERSION: 2;
10
+ export declare const WIKI_PAGE_BRIEF_FORMAT: "mdgraph-wiki-page-brief";
11
+ export declare const WIKI_PAGE_BRIEF_FORMAT_VERSION: 1;
12
+ export interface WikiPlan {
13
+ format: typeof WIKI_PLAN_FORMAT;
14
+ formatVersion: 1 | typeof WIKI_PLAN_FORMAT_VERSION;
15
+ graphHash: string;
16
+ sourceHash: string;
17
+ /** v2 records the project-relative generated Wiki root. */
18
+ wikiDir?: string;
19
+ /** Additive strict Markdown content evidence for deciding whether to use this plan. */
20
+ strictFreshness?: WikiStrictFreshness;
21
+ pages: WikiPlanPage[];
22
+ suggestions?: WikiPlanSuggestions;
23
+ gaps?: WikiPlanGap[];
24
+ }
25
+ export interface WikiPlanSuggestions {
26
+ pages: WikiPlanPage[];
27
+ sources: Array<{
28
+ pageId: string;
29
+ documentIds: string[];
30
+ sourceRefs: string[];
31
+ }>;
32
+ }
33
+ export interface WikiPlanGap {
34
+ kind: "document" | "source_ref" | "domain";
35
+ path?: string;
36
+ reason: string;
37
+ recovery: string;
38
+ }
39
+ export interface WikiPlanOptions {
40
+ from?: WikiPlan;
41
+ wikiDir?: string;
42
+ }
43
+ export interface WikiStrictFreshness {
44
+ state: StatusFreshness["state"];
45
+ recommendation: string;
46
+ issues?: NonNullable<StatusFreshness["issues"]>;
47
+ }
48
+ export interface WikiPlanPage {
49
+ id: string;
50
+ title: string;
51
+ path: string;
52
+ parentId?: string;
53
+ purpose: string;
54
+ audience: string;
55
+ outline: string[];
56
+ documentIds: string[];
57
+ sourceRefs: string[];
58
+ evidenceQueries: string[];
59
+ evidenceHash: string;
60
+ dependencySnapshot?: WikiDependencySnapshot;
61
+ gaps?: WikiPlanGap[];
62
+ }
63
+ export interface WikiDependencySnapshot {
64
+ documents: Array<{
65
+ id: string;
66
+ path?: string;
67
+ hash?: string;
68
+ missing?: true;
69
+ }>;
70
+ sourceRefs: Array<{
71
+ path: string;
72
+ fingerprint: string;
73
+ }>;
74
+ }
75
+ export interface WikiPageBrief {
76
+ format: typeof WIKI_PAGE_BRIEF_FORMAT;
77
+ formatVersion: typeof WIKI_PAGE_BRIEF_FORMAT_VERSION;
78
+ page: WikiPlanPage;
79
+ maxChars: number;
80
+ usedChars: number;
81
+ sourceDocuments: WikiBriefSourceDocument[];
82
+ supplementaryDocuments: WikiBriefSourceDocument[];
83
+ sourceInspections: Array<{
84
+ path: string;
85
+ instruction: string;
86
+ }>;
87
+ evidenceGaps: WikiPlanGap[];
88
+ dependencyEvidence: WikiDependencyAssessment;
89
+ contextItems: ContextItem[];
90
+ knowledgeCards: KnowledgeCard[];
91
+ writingRequirements: string[];
92
+ suggestedNextQueries: string[];
93
+ /** Strict Markdown content evidence available when the brief was prepared. */
94
+ strictFreshness: WikiStrictFreshness;
95
+ }
96
+ export interface WikiBriefSourceDocument {
97
+ id: string;
98
+ path: string;
99
+ title: string;
100
+ type: GraphDocument["type"];
101
+ status: string;
102
+ trustTier: GraphDocument["trustTier"];
103
+ }
104
+ export interface WikiBriefOptions {
105
+ maxChars?: number;
106
+ }
107
+ export declare class WikiPlanError extends Error {
108
+ readonly code: string;
109
+ readonly recovery: string;
110
+ constructor(code: string, message: string, recovery: string);
111
+ }
112
+ export declare function buildWikiPlan(projectRoot: string, repository: GraphRepository, options?: WikiPlanOptions): WikiPlan;
113
+ export declare function buildWikiPageBrief(projectRoot: string, repository: GraphRepository, config: MDGraphConfig, plan: WikiPlan, pageId: string, options?: WikiBriefOptions): WikiPageBrief;
114
+ export declare function stableWikiPlan(plan: WikiPlan): string;
115
+ export declare function formatWikiPlan(plan: WikiPlan): string;
116
+ export declare function formatWikiPageBrief(brief: WikiPageBrief): string;
117
+ export declare function writeWikiPlan(filePath: string, plan: WikiPlan): void;
118
+ export declare function readWikiPlan(filePath: string): WikiPlan;
119
+ export declare function validateWikiPlan(value: unknown): WikiPlan;
120
+ export declare function safeWikiPagePath(value: string): string | undefined;
121
+ export declare function wikiSourceHash(repository: GraphRepository, wikiDir?: string): string;