-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconfig.js
More file actions
410 lines (384 loc) · 11.1 KB
/
Copy pathconfig.js
File metadata and controls
410 lines (384 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { debug, warn } from './logger.js';
export const CONFIG_FILES = ['.codegraphrc.json', '.codegraphrc', 'codegraph.config.json'];
export const DEFAULTS = {
include: [],
exclude: [],
ignoreDirs: [],
extensions: [],
aliases: {},
build: {
incremental: true,
dbPath: '.codegraph/graph.db',
driftThreshold: 0.2,
},
query: {
defaultDepth: 3,
defaultLimit: 20,
excludeTests: false,
},
embeddings: { model: 'nomic-v1.5', llmProvider: null },
llm: { provider: null, model: null, baseUrl: null, apiKey: null, apiKeyCommand: null },
search: { defaultMinScore: 0.2, rrfK: 60, topK: 15, similarityWarnThreshold: 0.85 },
ci: { failOnCycles: false, impactThreshold: null },
manifesto: {
rules: {
cognitive: { warn: 15 },
cyclomatic: { warn: 10 },
maxNesting: { warn: 4 },
maintainabilityIndex: { warn: 20, fail: null },
importCount: { warn: null, fail: null },
exportCount: { warn: null, fail: null },
lineCount: { warn: null, fail: null },
fanIn: { warn: null, fail: null },
fanOut: { warn: null, fail: null },
noCycles: { warn: null, fail: null },
boundaries: { warn: null, fail: null },
},
boundaries: null,
},
check: {
cycles: true,
blastRadius: null,
signatures: true,
boundaries: true,
depth: 3,
},
coChange: {
since: '1 year ago',
minSupport: 3,
minJaccard: 0.3,
maxFilesPerCommit: 50,
},
analysis: {
impactDepth: 3,
fnImpactDepth: 5,
auditDepth: 3,
sequenceDepth: 10,
falsePositiveCallers: 20,
briefCallerDepth: 5,
briefImporterDepth: 5,
briefHighRiskCallers: 10,
briefMediumRiskCallers: 3,
},
community: {
resolution: 1.0,
},
structure: {
cohesionThreshold: 0.3,
},
risk: {
weights: {
fanIn: 0.25,
complexity: 0.3,
churn: 0.2,
role: 0.15,
mi: 0.1,
},
roleWeights: {
core: 1.0,
utility: 0.9,
entry: 0.8,
adapter: 0.5,
leaf: 0.2,
'test-only': 0.1,
dead: 0.1,
'dead-leaf': 0.0,
'dead-entry': 0.3,
'dead-ffi': 0.05,
'dead-unresolved': 0.15,
},
defaultRoleWeight: 0.5,
},
display: {
maxColWidth: 40,
excerptLines: 50,
summaryMaxChars: 100,
jsdocEndScanLines: 10,
jsdocOpenScanLines: 20,
signatureGatherLines: 5,
},
mcp: {
defaults: {
list_functions: 100,
query: 10,
where: 50,
node_roles: 100,
export_graph: 500,
fn_impact: 5,
context: 5,
explain: 10,
file_deps: 20,
file_exports: 20,
diff_impact: 30,
impact_analysis: 20,
semantic_search: 20,
execution_flow: 50,
hotspots: 20,
co_changes: 20,
complexity: 30,
manifesto: 50,
communities: 20,
structure: 30,
triage: 20,
ast_query: 50,
},
},
};
// Per-cwd config cache — avoids re-reading the config file on every query call.
// The config file rarely changes within a single process lifetime.
const _configCache = new Map();
/**
* Load project configuration from a .codegraphrc.json or similar file.
* Returns merged config with defaults. Results are cached per cwd.
*/
export function loadConfig(cwd) {
cwd = cwd || process.cwd();
const cached = _configCache.get(cwd);
if (cached) return structuredClone(cached);
for (const name of CONFIG_FILES) {
const filePath = path.join(cwd, name);
if (fs.existsSync(filePath)) {
try {
const raw = fs.readFileSync(filePath, 'utf-8');
const config = JSON.parse(raw);
debug(`Loaded config from ${filePath}`);
const merged = mergeConfig(DEFAULTS, config);
if ('excludeTests' in config && !(config.query && 'excludeTests' in config.query)) {
merged.query.excludeTests = Boolean(config.excludeTests);
}
delete merged.excludeTests;
const result = resolveSecrets(applyEnvOverrides(merged));
_configCache.set(cwd, result);
return result;
} catch (err) {
debug(`Failed to parse config ${filePath}: ${err.message}`);
}
}
}
const defaults = resolveSecrets(applyEnvOverrides({ ...DEFAULTS }));
_configCache.set(cwd, defaults);
return defaults;
}
/**
* Clear the config cache. Intended for long-running processes that need to
* pick up on-disk config changes, and for test isolation when tests share
* the same cwd.
*/
export function clearConfigCache() {
_configCache.clear();
}
const ENV_LLM_MAP = {
CODEGRAPH_LLM_PROVIDER: 'provider',
CODEGRAPH_LLM_API_KEY: 'apiKey',
CODEGRAPH_LLM_MODEL: 'model',
};
export function applyEnvOverrides(config) {
for (const [envKey, field] of Object.entries(ENV_LLM_MAP)) {
if (process.env[envKey] !== undefined) {
config.llm[field] = process.env[envKey];
}
}
return config;
}
export function resolveSecrets(config) {
const cmd = config.llm.apiKeyCommand;
if (typeof cmd !== 'string' || cmd.trim() === '') return config;
const parts = cmd.trim().split(/\s+/);
const [executable, ...args] = parts;
try {
const result = execFileSync(executable, args, {
encoding: 'utf-8',
timeout: 10_000,
maxBuffer: 64 * 1024,
stdio: ['ignore', 'pipe', 'pipe'],
}).trim();
if (result) {
config.llm.apiKey = result;
}
} catch (err) {
warn(`apiKeyCommand failed: ${err.message}`);
}
return config;
}
// ── Monorepo workspace detection ─────────────────────────────────────
/**
* Expand a workspace glob pattern into matching directories.
* Supports trailing `/*` or `/**` patterns (e.g. "packages/*").
* Does not depend on an external glob library — uses fs.readdirSync.
*/
function expandWorkspaceGlob(pattern, rootDir) {
// Strip trailing /*, /**, or just *
const clean = pattern.replace(/\/?\*\*?$/, '');
const baseDir = path.resolve(rootDir, clean);
if (!fs.existsSync(baseDir)) return [];
try {
const entries = fs.readdirSync(baseDir, { withFileTypes: true });
return entries
.filter((e) => e.isDirectory())
.map((e) => path.join(baseDir, e.name))
.filter((d) => fs.existsSync(path.join(d, 'package.json')));
} catch {
return [];
}
}
/**
* Read a package.json and return its name field, or null.
*/
function readPackageName(pkgDir) {
try {
const raw = fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf-8');
const pkg = JSON.parse(raw);
return pkg.name || null;
} catch {
return null;
}
}
/**
* Resolve the entry-point source file for a workspace package.
* Checks exports → main → index file fallback.
*/
function resolveWorkspaceEntry(pkgDir) {
try {
const raw = fs.readFileSync(path.join(pkgDir, 'package.json'), 'utf-8');
const pkg = JSON.parse(raw);
// Try "source" field first (common in monorepos for pre-built packages)
if (pkg.source) {
const s = path.resolve(pkgDir, pkg.source);
if (fs.existsSync(s)) return s;
}
// Try "main" field
if (pkg.main) {
const m = path.resolve(pkgDir, pkg.main);
if (fs.existsSync(m)) return m;
}
// Index file fallback
for (const idx of [
'index.ts',
'index.tsx',
'index.js',
'index.mjs',
'src/index.ts',
'src/index.tsx',
'src/index.js',
]) {
const candidate = path.resolve(pkgDir, idx);
if (fs.existsSync(candidate)) return candidate;
}
} catch {
/* ignore */
}
return null;
}
/**
* Detect monorepo workspace packages from workspace configuration files.
*
* Checks (in order):
* 1. pnpm-workspace.yaml — `packages:` array
* 2. package.json — `workspaces` field (npm/yarn)
* 3. lerna.json — `packages` array
*
* @param {string} rootDir - Project root directory
* @returns {Map<string, { dir: string, entry: string|null }>}
* Map of package name → { absolute dir, resolved entry file }
*/
export function detectWorkspaces(rootDir) {
const workspaces = new Map();
const patterns = [];
// 1. pnpm-workspace.yaml
const pnpmPath = path.join(rootDir, 'pnpm-workspace.yaml');
if (fs.existsSync(pnpmPath)) {
try {
const raw = fs.readFileSync(pnpmPath, 'utf-8');
// Simple YAML parse for `packages:` array — no dependency needed
const packagesMatch = raw.match(/^packages:\s*\n((?:\s+-\s+.+\n?)*)/m);
if (packagesMatch) {
const lines = packagesMatch[1].match(/^\s+-\s+['"]?([^'"#\n]+)['"]?\s*$/gm);
if (lines) {
for (const line of lines) {
const m = line.match(/^\s+-\s+['"]?([^'"#\n]+?)['"]?\s*$/);
if (m) patterns.push(m[1].trim());
}
}
}
} catch {
/* ignore */
}
}
// 2. package.json workspaces (npm/yarn)
if (patterns.length === 0) {
const rootPkgPath = path.join(rootDir, 'package.json');
if (fs.existsSync(rootPkgPath)) {
try {
const raw = fs.readFileSync(rootPkgPath, 'utf-8');
const pkg = JSON.parse(raw);
const ws = pkg.workspaces;
if (Array.isArray(ws)) {
patterns.push(...ws);
} else if (ws && Array.isArray(ws.packages)) {
// Yarn classic format: { packages: [...], nohoist: [...] }
patterns.push(...ws.packages);
}
} catch {
/* ignore */
}
}
}
// 3. lerna.json
if (patterns.length === 0) {
const lernaPath = path.join(rootDir, 'lerna.json');
if (fs.existsSync(lernaPath)) {
try {
const raw = fs.readFileSync(lernaPath, 'utf-8');
const lerna = JSON.parse(raw);
if (Array.isArray(lerna.packages)) {
patterns.push(...lerna.packages);
}
} catch {
/* ignore */
}
}
}
if (patterns.length === 0) return workspaces;
// Expand glob patterns and collect packages
for (const pattern of patterns) {
// Check if pattern is a direct path (no glob) or a glob
if (pattern.includes('*')) {
for (const dir of expandWorkspaceGlob(pattern, rootDir)) {
const name = readPackageName(dir);
if (name) workspaces.set(name, { dir, entry: resolveWorkspaceEntry(dir) });
}
} else {
// Direct path like "packages/core"
const dir = path.resolve(rootDir, pattern);
if (fs.existsSync(path.join(dir, 'package.json'))) {
const name = readPackageName(dir);
if (name) workspaces.set(name, { dir, entry: resolveWorkspaceEntry(dir) });
}
}
}
if (workspaces.size > 0) {
debug(`Detected ${workspaces.size} workspace packages: ${[...workspaces.keys()].join(', ')}`);
}
return workspaces;
}
export function mergeConfig(defaults, overrides) {
const result = { ...defaults };
for (const [key, value] of Object.entries(overrides)) {
if (
value &&
typeof value === 'object' &&
!Array.isArray(value) &&
defaults[key] &&
typeof defaults[key] === 'object' &&
!Array.isArray(defaults[key])
) {
result[key] = mergeConfig(defaults[key], value);
} else {
result[key] = value;
}
}
return result;
}