@duytransipher/gitnexus 1.4.6-sipher.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 (224) hide show
  1. package/LICENSE +73 -0
  2. package/README.md +261 -0
  3. package/dist/cli/ai-context.d.ts +23 -0
  4. package/dist/cli/ai-context.js +265 -0
  5. package/dist/cli/analyze.d.ts +12 -0
  6. package/dist/cli/analyze.js +345 -0
  7. package/dist/cli/augment.d.ts +13 -0
  8. package/dist/cli/augment.js +33 -0
  9. package/dist/cli/clean.d.ts +10 -0
  10. package/dist/cli/clean.js +60 -0
  11. package/dist/cli/eval-server.d.ts +37 -0
  12. package/dist/cli/eval-server.js +389 -0
  13. package/dist/cli/index.d.ts +2 -0
  14. package/dist/cli/index.js +137 -0
  15. package/dist/cli/lazy-action.d.ts +6 -0
  16. package/dist/cli/lazy-action.js +18 -0
  17. package/dist/cli/list.d.ts +6 -0
  18. package/dist/cli/list.js +30 -0
  19. package/dist/cli/mcp.d.ts +8 -0
  20. package/dist/cli/mcp.js +36 -0
  21. package/dist/cli/serve.d.ts +4 -0
  22. package/dist/cli/serve.js +6 -0
  23. package/dist/cli/setup.d.ts +8 -0
  24. package/dist/cli/setup.js +367 -0
  25. package/dist/cli/sipher-patched.d.ts +2 -0
  26. package/dist/cli/sipher-patched.js +77 -0
  27. package/dist/cli/skill-gen.d.ts +26 -0
  28. package/dist/cli/skill-gen.js +549 -0
  29. package/dist/cli/status.d.ts +6 -0
  30. package/dist/cli/status.js +36 -0
  31. package/dist/cli/tool.d.ts +60 -0
  32. package/dist/cli/tool.js +180 -0
  33. package/dist/cli/wiki.d.ts +15 -0
  34. package/dist/cli/wiki.js +365 -0
  35. package/dist/config/ignore-service.d.ts +26 -0
  36. package/dist/config/ignore-service.js +284 -0
  37. package/dist/config/supported-languages.d.ts +15 -0
  38. package/dist/config/supported-languages.js +16 -0
  39. package/dist/core/augmentation/engine.d.ts +26 -0
  40. package/dist/core/augmentation/engine.js +240 -0
  41. package/dist/core/embeddings/embedder.d.ts +60 -0
  42. package/dist/core/embeddings/embedder.js +251 -0
  43. package/dist/core/embeddings/embedding-pipeline.d.ts +51 -0
  44. package/dist/core/embeddings/embedding-pipeline.js +356 -0
  45. package/dist/core/embeddings/index.d.ts +9 -0
  46. package/dist/core/embeddings/index.js +9 -0
  47. package/dist/core/embeddings/text-generator.d.ts +24 -0
  48. package/dist/core/embeddings/text-generator.js +182 -0
  49. package/dist/core/embeddings/types.d.ts +87 -0
  50. package/dist/core/embeddings/types.js +32 -0
  51. package/dist/core/graph/graph.d.ts +2 -0
  52. package/dist/core/graph/graph.js +66 -0
  53. package/dist/core/graph/types.d.ts +66 -0
  54. package/dist/core/graph/types.js +1 -0
  55. package/dist/core/ingestion/ast-cache.d.ts +11 -0
  56. package/dist/core/ingestion/ast-cache.js +35 -0
  57. package/dist/core/ingestion/call-processor.d.ts +23 -0
  58. package/dist/core/ingestion/call-processor.js +793 -0
  59. package/dist/core/ingestion/call-routing.d.ts +68 -0
  60. package/dist/core/ingestion/call-routing.js +129 -0
  61. package/dist/core/ingestion/cluster-enricher.d.ts +38 -0
  62. package/dist/core/ingestion/cluster-enricher.js +170 -0
  63. package/dist/core/ingestion/community-processor.d.ts +39 -0
  64. package/dist/core/ingestion/community-processor.js +312 -0
  65. package/dist/core/ingestion/constants.d.ts +16 -0
  66. package/dist/core/ingestion/constants.js +16 -0
  67. package/dist/core/ingestion/entry-point-scoring.d.ts +40 -0
  68. package/dist/core/ingestion/entry-point-scoring.js +353 -0
  69. package/dist/core/ingestion/export-detection.d.ts +18 -0
  70. package/dist/core/ingestion/export-detection.js +231 -0
  71. package/dist/core/ingestion/filesystem-walker.d.ts +28 -0
  72. package/dist/core/ingestion/filesystem-walker.js +81 -0
  73. package/dist/core/ingestion/framework-detection.d.ts +54 -0
  74. package/dist/core/ingestion/framework-detection.js +411 -0
  75. package/dist/core/ingestion/heritage-processor.d.ts +28 -0
  76. package/dist/core/ingestion/heritage-processor.js +251 -0
  77. package/dist/core/ingestion/import-processor.d.ts +34 -0
  78. package/dist/core/ingestion/import-processor.js +398 -0
  79. package/dist/core/ingestion/language-config.d.ts +46 -0
  80. package/dist/core/ingestion/language-config.js +167 -0
  81. package/dist/core/ingestion/mro-processor.d.ts +45 -0
  82. package/dist/core/ingestion/mro-processor.js +369 -0
  83. package/dist/core/ingestion/named-binding-extraction.d.ts +61 -0
  84. package/dist/core/ingestion/named-binding-extraction.js +363 -0
  85. package/dist/core/ingestion/parsing-processor.d.ts +19 -0
  86. package/dist/core/ingestion/parsing-processor.js +315 -0
  87. package/dist/core/ingestion/pipeline.d.ts +6 -0
  88. package/dist/core/ingestion/pipeline.js +401 -0
  89. package/dist/core/ingestion/process-processor.d.ts +51 -0
  90. package/dist/core/ingestion/process-processor.js +315 -0
  91. package/dist/core/ingestion/resolution-context.d.ts +53 -0
  92. package/dist/core/ingestion/resolution-context.js +132 -0
  93. package/dist/core/ingestion/resolvers/csharp.d.ts +22 -0
  94. package/dist/core/ingestion/resolvers/csharp.js +109 -0
  95. package/dist/core/ingestion/resolvers/go.d.ts +19 -0
  96. package/dist/core/ingestion/resolvers/go.js +42 -0
  97. package/dist/core/ingestion/resolvers/index.d.ts +18 -0
  98. package/dist/core/ingestion/resolvers/index.js +13 -0
  99. package/dist/core/ingestion/resolvers/jvm.d.ts +23 -0
  100. package/dist/core/ingestion/resolvers/jvm.js +87 -0
  101. package/dist/core/ingestion/resolvers/php.d.ts +15 -0
  102. package/dist/core/ingestion/resolvers/php.js +35 -0
  103. package/dist/core/ingestion/resolvers/python.d.ts +19 -0
  104. package/dist/core/ingestion/resolvers/python.js +52 -0
  105. package/dist/core/ingestion/resolvers/ruby.d.ts +12 -0
  106. package/dist/core/ingestion/resolvers/ruby.js +15 -0
  107. package/dist/core/ingestion/resolvers/rust.d.ts +15 -0
  108. package/dist/core/ingestion/resolvers/rust.js +73 -0
  109. package/dist/core/ingestion/resolvers/standard.d.ts +28 -0
  110. package/dist/core/ingestion/resolvers/standard.js +123 -0
  111. package/dist/core/ingestion/resolvers/utils.d.ts +33 -0
  112. package/dist/core/ingestion/resolvers/utils.js +122 -0
  113. package/dist/core/ingestion/structure-processor.d.ts +2 -0
  114. package/dist/core/ingestion/structure-processor.js +36 -0
  115. package/dist/core/ingestion/symbol-table.d.ts +63 -0
  116. package/dist/core/ingestion/symbol-table.js +85 -0
  117. package/dist/core/ingestion/tree-sitter-queries.d.ts +15 -0
  118. package/dist/core/ingestion/tree-sitter-queries.js +888 -0
  119. package/dist/core/ingestion/type-env.d.ts +49 -0
  120. package/dist/core/ingestion/type-env.js +613 -0
  121. package/dist/core/ingestion/type-extractors/c-cpp.d.ts +2 -0
  122. package/dist/core/ingestion/type-extractors/c-cpp.js +385 -0
  123. package/dist/core/ingestion/type-extractors/csharp.d.ts +2 -0
  124. package/dist/core/ingestion/type-extractors/csharp.js +383 -0
  125. package/dist/core/ingestion/type-extractors/go.d.ts +2 -0
  126. package/dist/core/ingestion/type-extractors/go.js +467 -0
  127. package/dist/core/ingestion/type-extractors/index.d.ts +22 -0
  128. package/dist/core/ingestion/type-extractors/index.js +31 -0
  129. package/dist/core/ingestion/type-extractors/jvm.d.ts +3 -0
  130. package/dist/core/ingestion/type-extractors/jvm.js +681 -0
  131. package/dist/core/ingestion/type-extractors/php.d.ts +2 -0
  132. package/dist/core/ingestion/type-extractors/php.js +549 -0
  133. package/dist/core/ingestion/type-extractors/python.d.ts +2 -0
  134. package/dist/core/ingestion/type-extractors/python.js +455 -0
  135. package/dist/core/ingestion/type-extractors/ruby.d.ts +2 -0
  136. package/dist/core/ingestion/type-extractors/ruby.js +389 -0
  137. package/dist/core/ingestion/type-extractors/rust.d.ts +2 -0
  138. package/dist/core/ingestion/type-extractors/rust.js +456 -0
  139. package/dist/core/ingestion/type-extractors/shared.d.ts +145 -0
  140. package/dist/core/ingestion/type-extractors/shared.js +810 -0
  141. package/dist/core/ingestion/type-extractors/swift.d.ts +2 -0
  142. package/dist/core/ingestion/type-extractors/swift.js +137 -0
  143. package/dist/core/ingestion/type-extractors/types.d.ts +127 -0
  144. package/dist/core/ingestion/type-extractors/types.js +1 -0
  145. package/dist/core/ingestion/type-extractors/typescript.d.ts +2 -0
  146. package/dist/core/ingestion/type-extractors/typescript.js +494 -0
  147. package/dist/core/ingestion/utils.d.ts +138 -0
  148. package/dist/core/ingestion/utils.js +1290 -0
  149. package/dist/core/ingestion/workers/parse-worker.d.ts +122 -0
  150. package/dist/core/ingestion/workers/parse-worker.js +1126 -0
  151. package/dist/core/ingestion/workers/worker-pool.d.ts +16 -0
  152. package/dist/core/ingestion/workers/worker-pool.js +128 -0
  153. package/dist/core/lbug/csv-generator.d.ts +33 -0
  154. package/dist/core/lbug/csv-generator.js +366 -0
  155. package/dist/core/lbug/lbug-adapter.d.ts +103 -0
  156. package/dist/core/lbug/lbug-adapter.js +769 -0
  157. package/dist/core/lbug/schema.d.ts +53 -0
  158. package/dist/core/lbug/schema.js +430 -0
  159. package/dist/core/search/bm25-index.d.ts +23 -0
  160. package/dist/core/search/bm25-index.js +96 -0
  161. package/dist/core/search/hybrid-search.d.ts +49 -0
  162. package/dist/core/search/hybrid-search.js +118 -0
  163. package/dist/core/tree-sitter/parser-loader.d.ts +5 -0
  164. package/dist/core/tree-sitter/parser-loader.js +63 -0
  165. package/dist/core/wiki/generator.d.ts +120 -0
  166. package/dist/core/wiki/generator.js +939 -0
  167. package/dist/core/wiki/graph-queries.d.ts +80 -0
  168. package/dist/core/wiki/graph-queries.js +238 -0
  169. package/dist/core/wiki/html-viewer.d.ts +10 -0
  170. package/dist/core/wiki/html-viewer.js +297 -0
  171. package/dist/core/wiki/llm-client.d.ts +43 -0
  172. package/dist/core/wiki/llm-client.js +186 -0
  173. package/dist/core/wiki/prompts.d.ts +53 -0
  174. package/dist/core/wiki/prompts.js +174 -0
  175. package/dist/lib/utils.d.ts +1 -0
  176. package/dist/lib/utils.js +3 -0
  177. package/dist/mcp/compatible-stdio-transport.d.ts +25 -0
  178. package/dist/mcp/compatible-stdio-transport.js +200 -0
  179. package/dist/mcp/core/embedder.d.ts +27 -0
  180. package/dist/mcp/core/embedder.js +108 -0
  181. package/dist/mcp/core/lbug-adapter.d.ts +57 -0
  182. package/dist/mcp/core/lbug-adapter.js +455 -0
  183. package/dist/mcp/local/local-backend.d.ts +181 -0
  184. package/dist/mcp/local/local-backend.js +1722 -0
  185. package/dist/mcp/resources.d.ts +31 -0
  186. package/dist/mcp/resources.js +411 -0
  187. package/dist/mcp/server.d.ts +23 -0
  188. package/dist/mcp/server.js +296 -0
  189. package/dist/mcp/staleness.d.ts +15 -0
  190. package/dist/mcp/staleness.js +29 -0
  191. package/dist/mcp/tools.d.ts +24 -0
  192. package/dist/mcp/tools.js +292 -0
  193. package/dist/server/api.d.ts +10 -0
  194. package/dist/server/api.js +344 -0
  195. package/dist/server/mcp-http.d.ts +13 -0
  196. package/dist/server/mcp-http.js +100 -0
  197. package/dist/storage/git.d.ts +6 -0
  198. package/dist/storage/git.js +35 -0
  199. package/dist/storage/repo-manager.d.ts +138 -0
  200. package/dist/storage/repo-manager.js +299 -0
  201. package/dist/types/pipeline.d.ts +32 -0
  202. package/dist/types/pipeline.js +18 -0
  203. package/dist/unreal/bridge.d.ts +4 -0
  204. package/dist/unreal/bridge.js +113 -0
  205. package/dist/unreal/config.d.ts +6 -0
  206. package/dist/unreal/config.js +55 -0
  207. package/dist/unreal/types.d.ts +105 -0
  208. package/dist/unreal/types.js +1 -0
  209. package/hooks/claude/gitnexus-hook.cjs +238 -0
  210. package/hooks/claude/pre-tool-use.sh +79 -0
  211. package/hooks/claude/session-start.sh +42 -0
  212. package/package.json +100 -0
  213. package/scripts/ensure-cli-executable.cjs +21 -0
  214. package/scripts/patch-tree-sitter-swift.cjs +74 -0
  215. package/scripts/setup-unreal-gitnexus.ps1 +191 -0
  216. package/skills/gitnexus-cli.md +82 -0
  217. package/skills/gitnexus-debugging.md +89 -0
  218. package/skills/gitnexus-exploring.md +78 -0
  219. package/skills/gitnexus-guide.md +64 -0
  220. package/skills/gitnexus-impact-analysis.md +97 -0
  221. package/skills/gitnexus-pr-review.md +163 -0
  222. package/skills/gitnexus-refactoring.md +121 -0
  223. package/vendor/leiden/index.cjs +355 -0
  224. package/vendor/leiden/utils.cjs +392 -0
@@ -0,0 +1,16 @@
1
+ export interface WorkerPool {
2
+ /**
3
+ * Dispatch items across workers. Items are split into chunks (one per worker),
4
+ * each worker processes its chunk via sub-batches to limit peak memory,
5
+ * and results are concatenated back in order.
6
+ */
7
+ dispatch<TInput, TResult>(items: TInput[], onProgress?: (filesProcessed: number) => void): Promise<TResult[]>;
8
+ /** Terminate all workers. Must be called when done. */
9
+ terminate(): Promise<void>;
10
+ /** Number of workers in the pool */
11
+ readonly size: number;
12
+ }
13
+ /**
14
+ * Create a pool of worker threads.
15
+ */
16
+ export declare const createWorkerPool: (workerUrl: URL, poolSize?: number) => WorkerPool;
@@ -0,0 +1,128 @@
1
+ import { Worker } from 'node:worker_threads';
2
+ import os from 'node:os';
3
+ import fs from 'node:fs';
4
+ import { fileURLToPath } from 'node:url';
5
+ /**
6
+ * Max files to send to a worker in a single postMessage.
7
+ * Keeps structured-clone memory bounded per sub-batch.
8
+ */
9
+ const SUB_BATCH_SIZE = 1500;
10
+ /** Per sub-batch timeout. If a single sub-batch takes longer than this,
11
+ * likely a pathological file (e.g. minified 50MB JS). Fail fast. */
12
+ const SUB_BATCH_TIMEOUT_MS = 30_000;
13
+ /**
14
+ * Create a pool of worker threads.
15
+ */
16
+ export const createWorkerPool = (workerUrl, poolSize) => {
17
+ // Validate worker script exists before spawning to prevent uncaught
18
+ // MODULE_NOT_FOUND crashes in worker threads (e.g. when running from src/ via vitest)
19
+ const workerPath = fileURLToPath(workerUrl);
20
+ if (!fs.existsSync(workerPath)) {
21
+ throw new Error(`Worker script not found: ${workerPath}`);
22
+ }
23
+ const size = poolSize ?? Math.min(8, Math.max(1, os.cpus().length - 1));
24
+ const workers = [];
25
+ for (let i = 0; i < size; i++) {
26
+ workers.push(new Worker(workerUrl));
27
+ }
28
+ const dispatch = (items, onProgress) => {
29
+ if (items.length === 0)
30
+ return Promise.resolve([]);
31
+ const chunkSize = Math.ceil(items.length / size);
32
+ const chunks = [];
33
+ for (let i = 0; i < items.length; i += chunkSize) {
34
+ chunks.push(items.slice(i, i + chunkSize));
35
+ }
36
+ const workerProgress = new Array(chunks.length).fill(0);
37
+ const promises = chunks.map((chunk, i) => {
38
+ const worker = workers[i];
39
+ return new Promise((resolve, reject) => {
40
+ let settled = false;
41
+ let subBatchTimer = null;
42
+ const cleanup = () => {
43
+ if (subBatchTimer)
44
+ clearTimeout(subBatchTimer);
45
+ worker.removeListener('message', handler);
46
+ worker.removeListener('error', errorHandler);
47
+ worker.removeListener('exit', exitHandler);
48
+ };
49
+ const resetSubBatchTimer = () => {
50
+ if (subBatchTimer)
51
+ clearTimeout(subBatchTimer);
52
+ subBatchTimer = setTimeout(() => {
53
+ if (!settled) {
54
+ settled = true;
55
+ cleanup();
56
+ reject(new Error(`Worker ${i} sub-batch timed out after ${SUB_BATCH_TIMEOUT_MS / 1000}s (chunk: ${chunk.length} items).`));
57
+ }
58
+ }, SUB_BATCH_TIMEOUT_MS);
59
+ };
60
+ let subBatchIdx = 0;
61
+ const sendNextSubBatch = () => {
62
+ const start = subBatchIdx * SUB_BATCH_SIZE;
63
+ if (start >= chunk.length) {
64
+ worker.postMessage({ type: 'flush' });
65
+ return;
66
+ }
67
+ const subBatch = chunk.slice(start, start + SUB_BATCH_SIZE);
68
+ subBatchIdx++;
69
+ resetSubBatchTimer();
70
+ worker.postMessage({ type: 'sub-batch', files: subBatch });
71
+ };
72
+ const handler = (msg) => {
73
+ if (settled)
74
+ return;
75
+ if (msg && msg.type === 'progress') {
76
+ workerProgress[i] = msg.filesProcessed;
77
+ if (onProgress) {
78
+ const total = workerProgress.reduce((a, b) => a + b, 0);
79
+ onProgress(total);
80
+ }
81
+ }
82
+ else if (msg && msg.type === 'sub-batch-done') {
83
+ sendNextSubBatch();
84
+ }
85
+ else if (msg && msg.type === 'error') {
86
+ settled = true;
87
+ cleanup();
88
+ reject(new Error(`Worker ${i} error: ${msg.error}`));
89
+ }
90
+ else if (msg && msg.type === 'result') {
91
+ settled = true;
92
+ cleanup();
93
+ resolve(msg.data);
94
+ }
95
+ else {
96
+ settled = true;
97
+ cleanup();
98
+ resolve(msg);
99
+ }
100
+ };
101
+ const errorHandler = (err) => {
102
+ if (!settled) {
103
+ settled = true;
104
+ cleanup();
105
+ reject(err);
106
+ }
107
+ };
108
+ const exitHandler = (code) => {
109
+ if (!settled) {
110
+ settled = true;
111
+ cleanup();
112
+ reject(new Error(`Worker ${i} exited with code ${code}. Likely OOM or native addon failure.`));
113
+ }
114
+ };
115
+ worker.on('message', handler);
116
+ worker.once('error', errorHandler);
117
+ worker.once('exit', exitHandler);
118
+ sendNextSubBatch();
119
+ });
120
+ });
121
+ return Promise.all(promises);
122
+ };
123
+ const terminate = async () => {
124
+ await Promise.all(workers.map(w => w.terminate()));
125
+ workers.length = 0;
126
+ };
127
+ return { dispatch, terminate, size };
128
+ };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * CSV Generator for LadybugDB Hybrid Schema
3
+ *
4
+ * Streams CSV rows directly to disk files in a single pass over graph nodes.
5
+ * File contents are lazy-read from disk per-node to avoid holding the entire
6
+ * repo in RAM. Rows are buffered (FLUSH_EVERY) before writing to minimize
7
+ * per-row Promise overhead.
8
+ *
9
+ * RFC 4180 Compliant:
10
+ * - Fields containing commas, double quotes, or newlines are enclosed in double quotes
11
+ * - Double quotes within fields are escaped by doubling them ("")
12
+ * - All fields are consistently quoted for safety with code content
13
+ */
14
+ import { KnowledgeGraph } from '../graph/types.js';
15
+ import { NodeTableName } from './schema.js';
16
+ export declare const sanitizeUTF8: (str: string) => string;
17
+ export declare const escapeCSVField: (value: string | number | undefined | null) => string;
18
+ export declare const escapeCSVNumber: (value: number | undefined | null, defaultValue?: number) => string;
19
+ export declare const isBinaryContent: (content: string) => boolean;
20
+ export interface StreamedCSVResult {
21
+ nodeFiles: Map<NodeTableName, {
22
+ csvPath: string;
23
+ rows: number;
24
+ }>;
25
+ relCsvPath: string;
26
+ relRows: number;
27
+ }
28
+ /**
29
+ * Stream all CSV data directly to disk files.
30
+ * Iterates graph nodes exactly ONCE — routes each node to the right writer.
31
+ * File contents are lazy-read from disk with a generous LRU cache.
32
+ */
33
+ export declare const streamAllCSVsToDisk: (graph: KnowledgeGraph, repoPath: string, csvDir: string) => Promise<StreamedCSVResult>;
@@ -0,0 +1,366 @@
1
+ /**
2
+ * CSV Generator for LadybugDB Hybrid Schema
3
+ *
4
+ * Streams CSV rows directly to disk files in a single pass over graph nodes.
5
+ * File contents are lazy-read from disk per-node to avoid holding the entire
6
+ * repo in RAM. Rows are buffered (FLUSH_EVERY) before writing to minimize
7
+ * per-row Promise overhead.
8
+ *
9
+ * RFC 4180 Compliant:
10
+ * - Fields containing commas, double quotes, or newlines are enclosed in double quotes
11
+ * - Double quotes within fields are escaped by doubling them ("")
12
+ * - All fields are consistently quoted for safety with code content
13
+ */
14
+ import fs from 'fs/promises';
15
+ import { createWriteStream } from 'fs';
16
+ import path from 'path';
17
+ /** Flush buffered rows to disk every N rows */
18
+ const FLUSH_EVERY = 500;
19
+ // ============================================================================
20
+ // CSV ESCAPE UTILITIES
21
+ // ============================================================================
22
+ export const sanitizeUTF8 = (str) => {
23
+ return str
24
+ .replace(/\r\n/g, '\n')
25
+ .replace(/\r/g, '\n')
26
+ .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '')
27
+ .replace(/[\uD800-\uDFFF]/g, '')
28
+ .replace(/[\uFFFE\uFFFF]/g, '');
29
+ };
30
+ export const escapeCSVField = (value) => {
31
+ if (value === undefined || value === null)
32
+ return '""';
33
+ let str = String(value);
34
+ str = sanitizeUTF8(str);
35
+ return `"${str.replace(/"/g, '""')}"`;
36
+ };
37
+ export const escapeCSVNumber = (value, defaultValue = -1) => {
38
+ if (value === undefined || value === null)
39
+ return String(defaultValue);
40
+ return String(value);
41
+ };
42
+ // ============================================================================
43
+ // CONTENT EXTRACTION (lazy — reads from disk on demand)
44
+ // ============================================================================
45
+ export const isBinaryContent = (content) => {
46
+ if (!content || content.length === 0)
47
+ return false;
48
+ const sample = content.slice(0, 1000);
49
+ let nonPrintable = 0;
50
+ for (let i = 0; i < sample.length; i++) {
51
+ const code = sample.charCodeAt(i);
52
+ if ((code < 9) || (code > 13 && code < 32) || code === 127)
53
+ nonPrintable++;
54
+ }
55
+ return (nonPrintable / sample.length) > 0.1;
56
+ };
57
+ /**
58
+ * LRU content cache — avoids re-reading the same source file for every
59
+ * symbol defined in it. Sized generously so most files stay cached during
60
+ * the single-pass node iteration.
61
+ */
62
+ class FileContentCache {
63
+ cache = new Map();
64
+ accessOrder = [];
65
+ maxSize;
66
+ repoPath;
67
+ constructor(repoPath, maxSize = 3000) {
68
+ this.repoPath = repoPath;
69
+ this.maxSize = maxSize;
70
+ }
71
+ async get(relativePath) {
72
+ if (!relativePath)
73
+ return '';
74
+ const cached = this.cache.get(relativePath);
75
+ if (cached !== undefined) {
76
+ // Move to end of accessOrder (LRU promotion)
77
+ const idx = this.accessOrder.indexOf(relativePath);
78
+ if (idx !== -1) {
79
+ this.accessOrder.splice(idx, 1);
80
+ this.accessOrder.push(relativePath);
81
+ }
82
+ return cached;
83
+ }
84
+ try {
85
+ const fullPath = path.join(this.repoPath, relativePath);
86
+ const content = await fs.readFile(fullPath, 'utf-8');
87
+ this.set(relativePath, content);
88
+ return content;
89
+ }
90
+ catch {
91
+ this.set(relativePath, '');
92
+ return '';
93
+ }
94
+ }
95
+ set(key, value) {
96
+ if (this.cache.size >= this.maxSize) {
97
+ const oldest = this.accessOrder.shift();
98
+ if (oldest)
99
+ this.cache.delete(oldest);
100
+ }
101
+ this.cache.set(key, value);
102
+ this.accessOrder.push(key);
103
+ }
104
+ }
105
+ const extractContent = async (node, contentCache) => {
106
+ const filePath = node.properties.filePath;
107
+ const content = await contentCache.get(filePath);
108
+ if (!content)
109
+ return '';
110
+ if (node.label === 'Folder')
111
+ return '';
112
+ if (isBinaryContent(content))
113
+ return '[Binary file - content not stored]';
114
+ if (node.label === 'File') {
115
+ const MAX_FILE_CONTENT = 10000;
116
+ return content.length > MAX_FILE_CONTENT
117
+ ? content.slice(0, MAX_FILE_CONTENT) + '\n... [truncated]'
118
+ : content;
119
+ }
120
+ const startLine = node.properties.startLine;
121
+ const endLine = node.properties.endLine;
122
+ if (startLine === undefined || endLine === undefined)
123
+ return '';
124
+ const lines = content.split('\n');
125
+ const start = Math.max(0, startLine - 2);
126
+ const end = Math.min(lines.length - 1, endLine + 2);
127
+ const snippet = lines.slice(start, end + 1).join('\n');
128
+ const MAX_SNIPPET = 5000;
129
+ return snippet.length > MAX_SNIPPET
130
+ ? snippet.slice(0, MAX_SNIPPET) + '\n... [truncated]'
131
+ : snippet;
132
+ };
133
+ // ============================================================================
134
+ // BUFFERED CSV WRITER
135
+ // ============================================================================
136
+ class BufferedCSVWriter {
137
+ ws;
138
+ buffer = [];
139
+ rows = 0;
140
+ constructor(filePath, header) {
141
+ this.ws = createWriteStream(filePath, 'utf-8');
142
+ // Large repos flush many times — raise listener cap to avoid MaxListenersExceededWarning
143
+ this.ws.setMaxListeners(50);
144
+ this.buffer.push(header);
145
+ }
146
+ addRow(row) {
147
+ this.buffer.push(row);
148
+ this.rows++;
149
+ if (this.buffer.length >= FLUSH_EVERY) {
150
+ return this.flush();
151
+ }
152
+ return Promise.resolve();
153
+ }
154
+ flush() {
155
+ if (this.buffer.length === 0)
156
+ return Promise.resolve();
157
+ const chunk = this.buffer.join('\n') + '\n';
158
+ this.buffer.length = 0;
159
+ return new Promise((resolve, reject) => {
160
+ this.ws.once('error', reject);
161
+ const ok = this.ws.write(chunk);
162
+ if (ok) {
163
+ this.ws.removeListener('error', reject);
164
+ resolve();
165
+ }
166
+ else {
167
+ this.ws.once('drain', () => {
168
+ this.ws.removeListener('error', reject);
169
+ resolve();
170
+ });
171
+ }
172
+ });
173
+ }
174
+ async finish() {
175
+ await this.flush();
176
+ return new Promise((resolve, reject) => {
177
+ this.ws.end(() => resolve());
178
+ this.ws.on('error', reject);
179
+ });
180
+ }
181
+ }
182
+ /**
183
+ * Stream all CSV data directly to disk files.
184
+ * Iterates graph nodes exactly ONCE — routes each node to the right writer.
185
+ * File contents are lazy-read from disk with a generous LRU cache.
186
+ */
187
+ export const streamAllCSVsToDisk = async (graph, repoPath, csvDir) => {
188
+ // Remove stale CSVs from previous crashed runs, then recreate
189
+ try {
190
+ await fs.rm(csvDir, { recursive: true, force: true });
191
+ }
192
+ catch { }
193
+ await fs.mkdir(csvDir, { recursive: true });
194
+ // We open ~30 concurrent write-streams; raise process limit to suppress
195
+ // MaxListenersExceededWarning (restored after all streams finish).
196
+ const prevMax = process.getMaxListeners();
197
+ process.setMaxListeners(prevMax + 40);
198
+ const contentCache = new FileContentCache(repoPath);
199
+ // Create writers for every node type up-front
200
+ const fileWriter = new BufferedCSVWriter(path.join(csvDir, 'file.csv'), 'id,name,filePath,content');
201
+ const folderWriter = new BufferedCSVWriter(path.join(csvDir, 'folder.csv'), 'id,name,filePath');
202
+ const codeElementHeader = 'id,name,filePath,startLine,endLine,isExported,content,description';
203
+ const functionWriter = new BufferedCSVWriter(path.join(csvDir, 'function.csv'), codeElementHeader);
204
+ const classWriter = new BufferedCSVWriter(path.join(csvDir, 'class.csv'), codeElementHeader);
205
+ const interfaceWriter = new BufferedCSVWriter(path.join(csvDir, 'interface.csv'), codeElementHeader);
206
+ const methodHeader = 'id,name,filePath,startLine,endLine,isExported,content,description,parameterCount,returnType';
207
+ const methodWriter = new BufferedCSVWriter(path.join(csvDir, 'method.csv'), methodHeader);
208
+ const codeElemWriter = new BufferedCSVWriter(path.join(csvDir, 'codeelement.csv'), codeElementHeader);
209
+ const communityWriter = new BufferedCSVWriter(path.join(csvDir, 'community.csv'), 'id,label,heuristicLabel,keywords,description,enrichedBy,cohesion,symbolCount');
210
+ const processWriter = new BufferedCSVWriter(path.join(csvDir, 'process.csv'), 'id,label,heuristicLabel,processType,stepCount,communities,entryPointId,terminalId');
211
+ // Multi-language node types share the same CSV shape (no isExported column)
212
+ const multiLangHeader = 'id,name,filePath,startLine,endLine,content,description';
213
+ const MULTI_LANG_TYPES = ['Struct', 'Enum', 'Macro', 'Typedef', 'Union', 'Namespace', 'Trait', 'Impl',
214
+ 'TypeAlias', 'Const', 'Static', 'Property', 'Record', 'Delegate', 'Annotation', 'Constructor', 'Template', 'Module'];
215
+ const multiLangWriters = new Map();
216
+ for (const t of MULTI_LANG_TYPES) {
217
+ multiLangWriters.set(t, new BufferedCSVWriter(path.join(csvDir, `${t.toLowerCase()}.csv`), multiLangHeader));
218
+ }
219
+ const codeWriterMap = {
220
+ 'Function': functionWriter,
221
+ 'Class': classWriter,
222
+ 'Interface': interfaceWriter,
223
+ 'CodeElement': codeElemWriter,
224
+ };
225
+ const seenFileIds = new Set();
226
+ // --- SINGLE PASS over all nodes ---
227
+ for (const node of graph.iterNodes()) {
228
+ switch (node.label) {
229
+ case 'File': {
230
+ if (seenFileIds.has(node.id))
231
+ break;
232
+ seenFileIds.add(node.id);
233
+ const content = await extractContent(node, contentCache);
234
+ await fileWriter.addRow([
235
+ escapeCSVField(node.id),
236
+ escapeCSVField(node.properties.name || ''),
237
+ escapeCSVField(node.properties.filePath || ''),
238
+ escapeCSVField(content),
239
+ ].join(','));
240
+ break;
241
+ }
242
+ case 'Folder':
243
+ await folderWriter.addRow([
244
+ escapeCSVField(node.id),
245
+ escapeCSVField(node.properties.name || ''),
246
+ escapeCSVField(node.properties.filePath || ''),
247
+ ].join(','));
248
+ break;
249
+ case 'Community': {
250
+ const keywords = node.properties.keywords || [];
251
+ const keywordsStr = `[${keywords.map((k) => `'${k.replace(/\\/g, '\\\\').replace(/'/g, "''").replace(/,/g, '\\,')}'`).join(',')}]`;
252
+ await communityWriter.addRow([
253
+ escapeCSVField(node.id),
254
+ escapeCSVField(node.properties.name || ''),
255
+ escapeCSVField(node.properties.heuristicLabel || ''),
256
+ keywordsStr,
257
+ escapeCSVField(node.properties.description || ''),
258
+ escapeCSVField(node.properties.enrichedBy || 'heuristic'),
259
+ escapeCSVNumber(node.properties.cohesion, 0),
260
+ escapeCSVNumber(node.properties.symbolCount, 0),
261
+ ].join(','));
262
+ break;
263
+ }
264
+ case 'Process': {
265
+ const communities = node.properties.communities || [];
266
+ const communitiesStr = `[${communities.map((c) => `'${c.replace(/'/g, "''")}'`).join(',')}]`;
267
+ await processWriter.addRow([
268
+ escapeCSVField(node.id),
269
+ escapeCSVField(node.properties.name || ''),
270
+ escapeCSVField(node.properties.heuristicLabel || ''),
271
+ escapeCSVField(node.properties.processType || ''),
272
+ escapeCSVNumber(node.properties.stepCount, 0),
273
+ escapeCSVField(communitiesStr),
274
+ escapeCSVField(node.properties.entryPointId || ''),
275
+ escapeCSVField(node.properties.terminalId || ''),
276
+ ].join(','));
277
+ break;
278
+ }
279
+ case 'Method': {
280
+ const content = await extractContent(node, contentCache);
281
+ await methodWriter.addRow([
282
+ escapeCSVField(node.id),
283
+ escapeCSVField(node.properties.name || ''),
284
+ escapeCSVField(node.properties.filePath || ''),
285
+ escapeCSVNumber(node.properties.startLine, -1),
286
+ escapeCSVNumber(node.properties.endLine, -1),
287
+ node.properties.isExported ? 'true' : 'false',
288
+ escapeCSVField(content),
289
+ escapeCSVField(node.properties.description || ''),
290
+ escapeCSVNumber(node.properties.parameterCount, 0),
291
+ escapeCSVField(node.properties.returnType || ''),
292
+ ].join(','));
293
+ break;
294
+ }
295
+ default: {
296
+ // Code element nodes (Function, Class, Interface, CodeElement)
297
+ const writer = codeWriterMap[node.label];
298
+ if (writer) {
299
+ const content = await extractContent(node, contentCache);
300
+ await writer.addRow([
301
+ escapeCSVField(node.id),
302
+ escapeCSVField(node.properties.name || ''),
303
+ escapeCSVField(node.properties.filePath || ''),
304
+ escapeCSVNumber(node.properties.startLine, -1),
305
+ escapeCSVNumber(node.properties.endLine, -1),
306
+ node.properties.isExported ? 'true' : 'false',
307
+ escapeCSVField(content),
308
+ escapeCSVField(node.properties.description || ''),
309
+ ].join(','));
310
+ }
311
+ else {
312
+ // Multi-language node types (Struct, Impl, Trait, Macro, etc.)
313
+ const mlWriter = multiLangWriters.get(node.label);
314
+ if (mlWriter) {
315
+ const content = await extractContent(node, contentCache);
316
+ await mlWriter.addRow([
317
+ escapeCSVField(node.id),
318
+ escapeCSVField(node.properties.name || ''),
319
+ escapeCSVField(node.properties.filePath || ''),
320
+ escapeCSVNumber(node.properties.startLine, -1),
321
+ escapeCSVNumber(node.properties.endLine, -1),
322
+ escapeCSVField(content),
323
+ escapeCSVField(node.properties.description || ''),
324
+ ].join(','));
325
+ }
326
+ }
327
+ break;
328
+ }
329
+ }
330
+ }
331
+ // Finish all node writers
332
+ const allWriters = [fileWriter, folderWriter, functionWriter, classWriter, interfaceWriter, methodWriter, codeElemWriter, communityWriter, processWriter, ...multiLangWriters.values()];
333
+ await Promise.all(allWriters.map(w => w.finish()));
334
+ // --- Stream relationship CSV ---
335
+ const relCsvPath = path.join(csvDir, 'relations.csv');
336
+ const relWriter = new BufferedCSVWriter(relCsvPath, 'from,to,type,confidence,reason,step');
337
+ for (const rel of graph.iterRelationships()) {
338
+ await relWriter.addRow([
339
+ escapeCSVField(rel.sourceId),
340
+ escapeCSVField(rel.targetId),
341
+ escapeCSVField(rel.type),
342
+ escapeCSVNumber(rel.confidence, 1.0),
343
+ escapeCSVField(rel.reason),
344
+ escapeCSVNumber(rel.step, 0),
345
+ ].join(','));
346
+ }
347
+ await relWriter.finish();
348
+ // Build result map — only include tables that have rows
349
+ const nodeFiles = new Map();
350
+ const tableMap = [
351
+ ['File', fileWriter], ['Folder', folderWriter],
352
+ ['Function', functionWriter], ['Class', classWriter],
353
+ ['Interface', interfaceWriter], ['Method', methodWriter],
354
+ ['CodeElement', codeElemWriter],
355
+ ['Community', communityWriter], ['Process', processWriter],
356
+ ...Array.from(multiLangWriters.entries()).map(([name, w]) => [name, w]),
357
+ ];
358
+ for (const [name, writer] of tableMap) {
359
+ if (writer.rows > 0) {
360
+ nodeFiles.set(name, { csvPath: path.join(csvDir, `${name.toLowerCase()}.csv`), rows: writer.rows });
361
+ }
362
+ }
363
+ // Restore original process listener limit
364
+ process.setMaxListeners(prevMax);
365
+ return { nodeFiles, relCsvPath, relRows: relWriter.rows };
366
+ };
@@ -0,0 +1,103 @@
1
+ import lbug from '@ladybugdb/core';
2
+ import { KnowledgeGraph } from '../graph/types.js';
3
+ /** Expose the current Database for pool adapter reuse in tests. */
4
+ export declare const getDatabase: () => lbug.Database | null;
5
+ export declare const initLbug: (dbPath: string) => Promise<{
6
+ db: lbug.Database;
7
+ conn: lbug.Connection;
8
+ }>;
9
+ /**
10
+ * Execute multiple queries against one repo DB atomically.
11
+ * While the callback runs, no other request can switch the active DB.
12
+ */
13
+ export declare const withLbugDb: <T>(dbPath: string, operation: () => Promise<T>) => Promise<T>;
14
+ export type LbugProgressCallback = (message: string) => void;
15
+ export declare const loadGraphToLbug: (graph: KnowledgeGraph, repoPath: string, storagePath: string, onProgress?: LbugProgressCallback) => Promise<{
16
+ success: boolean;
17
+ insertedRels: number;
18
+ skippedRels: number;
19
+ warnings: string[];
20
+ }>;
21
+ /**
22
+ * Insert a single node to LadybugDB
23
+ * @param label - Node type (File, Function, Class, etc.)
24
+ * @param properties - Node properties
25
+ * @param dbPath - Path to LadybugDB database (optional if already initialized)
26
+ */
27
+ export declare const insertNodeToLbug: (label: string, properties: Record<string, any>, dbPath?: string) => Promise<boolean>;
28
+ /**
29
+ * Batch insert multiple nodes to LadybugDB using a single connection
30
+ * @param nodes - Array of {label, properties} to insert
31
+ * @param dbPath - Path to LadybugDB database
32
+ * @returns Object with success count and error count
33
+ */
34
+ export declare const batchInsertNodesToLbug: (nodes: Array<{
35
+ label: string;
36
+ properties: Record<string, any>;
37
+ }>, dbPath: string) => Promise<{
38
+ inserted: number;
39
+ failed: number;
40
+ }>;
41
+ export declare const executeQuery: (cypher: string) => Promise<any[]>;
42
+ export declare const executeWithReusedStatement: (cypher: string, paramsList: Array<Record<string, any>>) => Promise<void>;
43
+ export declare const getLbugStats: () => Promise<{
44
+ nodes: number;
45
+ edges: number;
46
+ }>;
47
+ /**
48
+ * Load cached embeddings from LadybugDB before a rebuild.
49
+ * Returns all embedding vectors so they can be re-inserted after the graph is reloaded,
50
+ * avoiding expensive re-embedding of unchanged nodes.
51
+ */
52
+ export declare const loadCachedEmbeddings: () => Promise<{
53
+ embeddingNodeIds: Set<string>;
54
+ embeddings: Array<{
55
+ nodeId: string;
56
+ embedding: number[];
57
+ }>;
58
+ }>;
59
+ export declare const closeLbug: () => Promise<void>;
60
+ export declare const isLbugReady: () => boolean;
61
+ /**
62
+ * Delete all nodes (and their relationships) for a specific file from LadybugDB
63
+ * @param filePath - The file path to delete nodes for
64
+ * @param dbPath - Optional path to LadybugDB for per-query connection
65
+ * @returns Object with counts of deleted nodes
66
+ */
67
+ export declare const deleteNodesForFile: (filePath: string, dbPath?: string) => Promise<{
68
+ deletedNodes: number;
69
+ }>;
70
+ export declare const getEmbeddingTableName: () => string;
71
+ /**
72
+ * Load the FTS extension (required before using FTS functions).
73
+ * Safe to call multiple times — tracks loaded state via module-level ftsLoaded.
74
+ */
75
+ export declare const loadFTSExtension: () => Promise<void>;
76
+ /**
77
+ * Create a full-text search index on a table
78
+ * @param tableName - The node table name (e.g., 'File', 'CodeSymbol')
79
+ * @param indexName - Name for the FTS index
80
+ * @param properties - List of properties to index (e.g., ['name', 'code'])
81
+ * @param stemmer - Stemming algorithm (default: 'porter')
82
+ */
83
+ export declare const createFTSIndex: (tableName: string, indexName: string, properties: string[], stemmer?: string) => Promise<void>;
84
+ /**
85
+ * Query a full-text search index
86
+ * @param tableName - The node table name
87
+ * @param indexName - FTS index name
88
+ * @param query - Search query string
89
+ * @param limit - Maximum results
90
+ * @param conjunctive - If true, all terms must match (AND); if false, any term matches (OR)
91
+ * @returns Array of { node properties, score }
92
+ */
93
+ export declare const queryFTS: (tableName: string, indexName: string, query: string, limit?: number, conjunctive?: boolean) => Promise<Array<{
94
+ nodeId: string;
95
+ name: string;
96
+ filePath: string;
97
+ score: number;
98
+ [key: string]: any;
99
+ }>>;
100
+ /**
101
+ * Drop an FTS index
102
+ */
103
+ export declare const dropFTSIndex: (tableName: string, indexName: string) => Promise<void>;