@optave/codegraph 3.12.0 → 3.13.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 (144) hide show
  1. package/README.md +71 -35
  2. package/dist/cli/commands/audit.d.ts.map +1 -1
  3. package/dist/cli/commands/audit.js +2 -1
  4. package/dist/cli/commands/audit.js.map +1 -1
  5. package/dist/cli/commands/batch.d.ts.map +1 -1
  6. package/dist/cli/commands/batch.js +1 -0
  7. package/dist/cli/commands/batch.js.map +1 -1
  8. package/dist/cli/commands/build.d.ts.map +1 -1
  9. package/dist/cli/commands/build.js +6 -1
  10. package/dist/cli/commands/build.js.map +1 -1
  11. package/dist/cli/commands/config.d.ts +3 -0
  12. package/dist/cli/commands/config.d.ts.map +1 -0
  13. package/dist/cli/commands/config.js +272 -0
  14. package/dist/cli/commands/config.js.map +1 -0
  15. package/dist/cli/commands/triage.js +1 -1
  16. package/dist/cli/commands/triage.js.map +1 -1
  17. package/dist/cli/index.d.ts.map +1 -1
  18. package/dist/cli/index.js +10 -0
  19. package/dist/cli/index.js.map +1 -1
  20. package/dist/cli/shared/options.d.ts +2 -1
  21. package/dist/cli/shared/options.d.ts.map +1 -1
  22. package/dist/cli/shared/options.js +11 -1
  23. package/dist/cli/shared/options.js.map +1 -1
  24. package/dist/cli/types.d.ts +2 -0
  25. package/dist/cli/types.d.ts.map +1 -1
  26. package/dist/db/migrations.js +1 -1
  27. package/dist/db/migrations.js.map +1 -1
  28. package/dist/domain/graph/builder/call-resolver.d.ts +12 -8
  29. package/dist/domain/graph/builder/call-resolver.d.ts.map +1 -1
  30. package/dist/domain/graph/builder/call-resolver.js +93 -38
  31. package/dist/domain/graph/builder/call-resolver.js.map +1 -1
  32. package/dist/domain/graph/builder/cha.d.ts +9 -1
  33. package/dist/domain/graph/builder/cha.d.ts.map +1 -1
  34. package/dist/domain/graph/builder/cha.js +17 -2
  35. package/dist/domain/graph/builder/cha.js.map +1 -1
  36. package/dist/domain/graph/builder/helpers.d.ts +8 -0
  37. package/dist/domain/graph/builder/helpers.d.ts.map +1 -1
  38. package/dist/domain/graph/builder/helpers.js +22 -3
  39. package/dist/domain/graph/builder/helpers.js.map +1 -1
  40. package/dist/domain/graph/builder/incremental.d.ts.map +1 -1
  41. package/dist/domain/graph/builder/incremental.js +1 -1
  42. package/dist/domain/graph/builder/incremental.js.map +1 -1
  43. package/dist/domain/graph/builder/pipeline.d.ts.map +1 -1
  44. package/dist/domain/graph/builder/pipeline.js +37 -2
  45. package/dist/domain/graph/builder/pipeline.js.map +1 -1
  46. package/dist/domain/graph/builder/stages/build-edges.d.ts +0 -2
  47. package/dist/domain/graph/builder/stages/build-edges.d.ts.map +1 -1
  48. package/dist/domain/graph/builder/stages/build-edges.js +88 -318
  49. package/dist/domain/graph/builder/stages/build-edges.js.map +1 -1
  50. package/dist/domain/graph/builder/stages/detect-changes.js +1 -1
  51. package/dist/domain/graph/builder/stages/detect-changes.js.map +1 -1
  52. package/dist/domain/graph/builder/stages/finalize.d.ts.map +1 -1
  53. package/dist/domain/graph/builder/stages/finalize.js +4 -0
  54. package/dist/domain/graph/builder/stages/finalize.js.map +1 -1
  55. package/dist/domain/graph/builder/stages/native-orchestrator.d.ts.map +1 -1
  56. package/dist/domain/graph/builder/stages/native-orchestrator.js +341 -82
  57. package/dist/domain/graph/builder/stages/native-orchestrator.js.map +1 -1
  58. package/dist/domain/graph/builder/stages/resolve-imports.js +1 -1
  59. package/dist/domain/graph/builder/stages/resolve-imports.js.map +1 -1
  60. package/dist/domain/parser.d.ts +4 -5
  61. package/dist/domain/parser.d.ts.map +1 -1
  62. package/dist/domain/parser.js +46 -15
  63. package/dist/domain/parser.js.map +1 -1
  64. package/dist/domain/wasm-worker-entry.js +10 -2
  65. package/dist/domain/wasm-worker-entry.js.map +1 -1
  66. package/dist/domain/wasm-worker-pool.d.ts.map +1 -1
  67. package/dist/domain/wasm-worker-pool.js +2 -0
  68. package/dist/domain/wasm-worker-pool.js.map +1 -1
  69. package/dist/domain/wasm-worker-protocol.d.ts +1 -0
  70. package/dist/domain/wasm-worker-protocol.d.ts.map +1 -1
  71. package/dist/extractors/cpp.d.ts.map +1 -1
  72. package/dist/extractors/cpp.js +42 -1
  73. package/dist/extractors/cpp.js.map +1 -1
  74. package/dist/extractors/cuda.d.ts.map +1 -1
  75. package/dist/extractors/cuda.js +42 -1
  76. package/dist/extractors/cuda.js.map +1 -1
  77. package/dist/extractors/helpers.d.ts +11 -0
  78. package/dist/extractors/helpers.d.ts.map +1 -1
  79. package/dist/extractors/helpers.js +40 -0
  80. package/dist/extractors/helpers.js.map +1 -1
  81. package/dist/extractors/java.d.ts.map +1 -1
  82. package/dist/extractors/java.js +8 -7
  83. package/dist/extractors/java.js.map +1 -1
  84. package/dist/extractors/javascript.js +137 -6
  85. package/dist/extractors/javascript.js.map +1 -1
  86. package/dist/features/structure-query.d.ts +1 -1
  87. package/dist/features/structure-query.d.ts.map +1 -1
  88. package/dist/features/structure-query.js +6 -6
  89. package/dist/features/structure-query.js.map +1 -1
  90. package/dist/index.d.ts +1 -1
  91. package/dist/index.d.ts.map +1 -1
  92. package/dist/index.js +1 -1
  93. package/dist/index.js.map +1 -1
  94. package/dist/infrastructure/config.d.ts +77 -4
  95. package/dist/infrastructure/config.d.ts.map +1 -1
  96. package/dist/infrastructure/config.js +395 -21
  97. package/dist/infrastructure/config.js.map +1 -1
  98. package/dist/infrastructure/registry.d.ts +27 -0
  99. package/dist/infrastructure/registry.d.ts.map +1 -1
  100. package/dist/infrastructure/registry.js +59 -1
  101. package/dist/infrastructure/registry.js.map +1 -1
  102. package/dist/presentation/structure.d.ts +1 -1
  103. package/dist/presentation/structure.d.ts.map +1 -1
  104. package/dist/presentation/structure.js +2 -2
  105. package/dist/presentation/structure.js.map +1 -1
  106. package/dist/types.d.ts +37 -0
  107. package/dist/types.d.ts.map +1 -1
  108. package/grammars/tree-sitter-gleam.wasm +0 -0
  109. package/package.json +7 -8
  110. package/src/cli/commands/audit.ts +2 -1
  111. package/src/cli/commands/batch.ts +1 -0
  112. package/src/cli/commands/build.ts +6 -1
  113. package/src/cli/commands/config.ts +353 -0
  114. package/src/cli/commands/triage.ts +1 -1
  115. package/src/cli/index.ts +10 -0
  116. package/src/cli/shared/options.ts +11 -1
  117. package/src/cli/types.ts +2 -0
  118. package/src/db/migrations.ts +1 -1
  119. package/src/domain/graph/builder/call-resolver.ts +99 -41
  120. package/src/domain/graph/builder/cha.ts +18 -1
  121. package/src/domain/graph/builder/helpers.ts +24 -4
  122. package/src/domain/graph/builder/incremental.ts +1 -0
  123. package/src/domain/graph/builder/pipeline.ts +49 -2
  124. package/src/domain/graph/builder/stages/build-edges.ts +130 -399
  125. package/src/domain/graph/builder/stages/detect-changes.ts +1 -1
  126. package/src/domain/graph/builder/stages/finalize.ts +4 -0
  127. package/src/domain/graph/builder/stages/native-orchestrator.ts +396 -92
  128. package/src/domain/graph/builder/stages/resolve-imports.ts +1 -1
  129. package/src/domain/parser.ts +45 -14
  130. package/src/domain/wasm-worker-entry.ts +10 -2
  131. package/src/domain/wasm-worker-pool.ts +1 -0
  132. package/src/domain/wasm-worker-protocol.ts +1 -0
  133. package/src/extractors/cpp.ts +44 -1
  134. package/src/extractors/cuda.ts +44 -1
  135. package/src/extractors/helpers.ts +43 -0
  136. package/src/extractors/java.ts +8 -7
  137. package/src/extractors/javascript.ts +127 -6
  138. package/src/features/structure-query.ts +7 -7
  139. package/src/index.ts +5 -1
  140. package/src/infrastructure/config.ts +481 -22
  141. package/src/infrastructure/registry.ts +82 -1
  142. package/src/presentation/structure.ts +3 -3
  143. package/src/types.ts +41 -0
  144. package/grammars/tree-sitter-erlang.wasm +0 -0
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,UAAU,EA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AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC5F,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EACL,UAAU,EACV,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,EACd,WAAW,EACX,OAAO,EACP,WAAW,EACX,UAAU,EACV,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC"}
@@ -12,7 +12,7 @@ export declare const DEFAULTS: {
12
12
  dbPath: string;
13
13
  driftThreshold: number;
14
14
  smallFilesThreshold: number;
15
- typescriptResolver: false;
15
+ typescriptResolver: true;
16
16
  };
17
17
  query: {
18
18
  defaultDepth: number;
@@ -114,7 +114,7 @@ export declare const DEFAULTS: {
114
114
  * Maximum fixed-point iterations for the Phase 8.3 points-to solver.
115
115
  * @reserved — currently not wired to either the WASM solver
116
116
  * (`MAX_SOLVER_ITERATIONS` in `points-to.ts`) or the native Rust solver
117
- * (`MAX_SOLVER_ITERATIONS` in `edge_builder.rs`), both of which use the
117
+ * (`MAX_SOLVER_ITERATIONS` in `stages/build_edges.rs`), both of which use the
118
118
  * same hardcoded value of 50. See the TODO comment above.
119
119
  */
120
120
  pointsToMaxIterations: number;
@@ -177,17 +177,90 @@ export declare const DEFAULTS: {
177
177
  disabledTools: string[];
178
178
  };
179
179
  };
180
+ /**
181
+ * Set the per-run user-config override from CLI flags.
182
+ * Called by the CLI preAction hook before any command executes.
183
+ * - false → --no-user-config
184
+ * - string → --user-config <path>
185
+ * - true → --user-config (bare, use default global file)
186
+ * - undefined → clear override, revert to consent-based resolution
187
+ */
188
+ export declare function setUserConfigOverride(v: string | boolean | undefined): void;
189
+ /**
190
+ * Return the canonical path where a new global config file should be written.
191
+ *
192
+ * Uses the same priority logic as resolveUserConfigPath() but always returns a
193
+ * path — it does not check whether the file exists. Used by `--init` to know
194
+ * where to scaffold the file.
195
+ *
196
+ * Priority:
197
+ * 1. CODEGRAPH_USER_CONFIG env var (used as-is)
198
+ * 2. $XDG_CONFIG_HOME/codegraph/config.json
199
+ * %APPDATA%\codegraph\config.json (Windows)
200
+ * fallback: ~/.config/codegraph/config.json
201
+ */
202
+ export declare function getDefaultUserConfigPath(): string;
203
+ /**
204
+ * Resolve the absolute path to the user-level global config file.
205
+ *
206
+ * Priority:
207
+ * 1. CODEGRAPH_USER_CONFIG env var (location override only — not forced-on)
208
+ * 2. $XDG_CONFIG_HOME/codegraph/config.json (Unix/macOS)
209
+ * %APPDATA%\codegraph\config.json (Windows)
210
+ * fallback: ~/.config/codegraph/config.json
211
+ * 3. ~/.codegraph/config.json (legacy, next to registry.json)
212
+ *
213
+ * Returns the path of the first existing file, or null if none exist.
214
+ */
215
+ export declare function resolveUserConfigPath(): string | null;
216
+ export declare function getLastAppliedGlobalPath(): string | null;
217
+ export declare function getLastAppliedGlobalConfig(): Record<string, unknown> | null;
218
+ /**
219
+ * Compute a short stable hash of the build-relevant config subset.
220
+ * Used by the pipeline to detect config changes that require a full rebuild.
221
+ */
222
+ export declare function computeConfigHash(config: CodegraphConfig): string;
223
+ /**
224
+ * When called from the build command, check whether we should prompt the user
225
+ * for global-config consent and, if so, prompt and persist the answer.
226
+ *
227
+ * Only fires when ALL of:
228
+ * - A global config file exists
229
+ * - The repo is undecided (no recorded consent)
230
+ * - Not matched by appliesTo globs
231
+ * - process.stdin.isTTY && process.stdout.isTTY
232
+ * - CI env is not set
233
+ * - No per-run --user-config / --no-user-config flag is active
234
+ */
235
+ export declare function promptForConsentIfNeeded(rootDir: string, registryPath?: string): Promise<void>;
236
+ /** Options for loadConfig. */
237
+ export interface LoadConfigOpts {
238
+ /** Per-run user-config override (from CLI flags or programmatic call). */
239
+ userConfig?: string | boolean;
240
+ /** Registry path override (mainly for tests). */
241
+ registryPath?: string;
242
+ }
180
243
  /**
181
244
  * Load project configuration from a .codegraphrc.json or similar file.
182
- * Returns merged config with defaults. Results are cached per cwd.
245
+ * Returns merged config with defaults: defaults global (if applied) → project → env → secrets.
246
+ * Results are cached per cwd + applied global path.
183
247
  */
184
- export declare function loadConfig(cwd?: string): CodegraphConfig;
248
+ export declare function loadConfig(cwd?: string, opts?: LoadConfigOpts): CodegraphConfig;
185
249
  /**
186
250
  * Clear the config cache. Intended for long-running processes that need to
187
251
  * pick up on-disk config changes, and for test isolation when tests share
188
252
  * the same cwd.
189
253
  */
190
254
  export declare function clearConfigCache(): void;
255
+ /**
256
+ * Load config and return it together with per-key provenance information.
257
+ * Used by `codegraph config --explain`.
258
+ *
259
+ * Calls loadConfig first so _lastAppliedGlobalConfig is populated, then uses
260
+ * that cached data for the global-layer provenance — avoiding a second disk
261
+ * read and eliminating the TOCTOU window between the two reads.
262
+ */
263
+ export declare function loadConfigWithProvenance(cwd?: string, opts?: LoadConfigOpts): import('../types.js').ConfigWithProvenance;
191
264
  export declare function applyEnvOverrides(config: CodegraphConfig): CodegraphConfig;
192
265
  export declare function resolveSecrets(config: CodegraphConfig): CodegraphConfig;
193
266
  interface WorkspaceEntry {
@@ -1 +1 @@
1
- {"version":3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,EACf,YAAY,GAAE,MAAsB,GACnC,OAAO,CAAC,IAAI,CAAC,CA4Cf;AAID,8BAA8B;AAC9B,MAAM,WAAW,cAAc;IAC7B,0EAA0E;IAC1E,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAC9B,iDAAiD;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,cAAc,GAAG,eAAe,CAgE/E;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,IAAI,IAAI,CAGvC;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,GAAG,CAAC,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,cAAc,GACpB,OAAO,aAAa,EAAE,oBAAoB,CA8C5C;AAQD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CAQ1E;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,eAAe,GAAG,eAAe,CA6BvE;AAwCD,UAAU,cAAc;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AA4HD,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAc7E;AAED,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAoBzB"}
@@ -1,8 +1,12 @@
1
1
  import { execFileSync } from 'node:child_process';
2
+ import { createHash } from 'node:crypto';
2
3
  import fs from 'node:fs';
4
+ import os from 'node:os';
3
5
  import path from 'node:path';
4
6
  import { ConfigError, toErrorMessage } from '../shared/errors.js';
7
+ import { compileGlobs, matchesAny } from '../shared/globs.js';
5
8
  import { debug, warn } from './logger.js';
9
+ import { getUserConfigConsent, REGISTRY_PATH, setUserConfigConsent } from './registry.js';
6
10
  export const CONFIG_FILES = [
7
11
  '.codegraphrc.json',
8
12
  '.codegraphrc',
@@ -19,7 +23,7 @@ export const DEFAULTS = {
19
23
  dbPath: '.codegraph/graph.db',
20
24
  driftThreshold: 0.2,
21
25
  smallFilesThreshold: 5,
22
- typescriptResolver: false,
26
+ typescriptResolver: true,
23
27
  },
24
28
  query: {
25
29
  defaultDepth: 3,
@@ -85,7 +89,7 @@ export const DEFAULTS = {
85
89
  * Maximum fixed-point iterations for the Phase 8.3 points-to solver.
86
90
  * @reserved — currently not wired to either the WASM solver
87
91
  * (`MAX_SOLVER_ITERATIONS` in `points-to.ts`) or the native Rust solver
88
- * (`MAX_SOLVER_ITERATIONS` in `edge_builder.rs`), both of which use the
92
+ * (`MAX_SOLVER_ITERATIONS` in `stages/build_edges.rs`), both of which use the
89
93
  * same hardcoded value of 50. See the TODO comment above.
90
94
  */
91
95
  pointsToMaxIterations: 50,
@@ -160,33 +164,352 @@ export const DEFAULTS = {
160
164
  disabledTools: [],
161
165
  },
162
166
  };
167
+ // ── Per-process user-config override (set by CLI flags) ────────────────
168
+ // Set once by the preAction hook before any command runs; cleared when changed.
169
+ let _userConfigOverride;
170
+ /**
171
+ * Set the per-run user-config override from CLI flags.
172
+ * Called by the CLI preAction hook before any command executes.
173
+ * - false → --no-user-config
174
+ * - string → --user-config <path>
175
+ * - true → --user-config (bare, use default global file)
176
+ * - undefined → clear override, revert to consent-based resolution
177
+ */
178
+ export function setUserConfigOverride(v) {
179
+ _userConfigOverride = v;
180
+ _configCache.clear();
181
+ _globalConfigCache.clear();
182
+ }
163
183
  // Per-cwd config cache — avoids re-reading the config file on every query call.
164
- // The config file rarely changes within a single process lifetime.
184
+ // Key includes the applied global path so toggled flags/consent are reflected.
165
185
  const _configCache = new Map();
186
+ // Parallel cache for the sanitized global layer — needed so loadConfigWithProvenance
187
+ // can correctly attribute global-layer keys even on a _configCache hit.
188
+ const _globalConfigCache = new Map();
189
+ // ── Global config file location ─────────────────────────────────────────
190
+ /**
191
+ * Return the canonical path where a new global config file should be written.
192
+ *
193
+ * Uses the same priority logic as resolveUserConfigPath() but always returns a
194
+ * path — it does not check whether the file exists. Used by `--init` to know
195
+ * where to scaffold the file.
196
+ *
197
+ * Priority:
198
+ * 1. CODEGRAPH_USER_CONFIG env var (used as-is)
199
+ * 2. $XDG_CONFIG_HOME/codegraph/config.json
200
+ * %APPDATA%\codegraph\config.json (Windows)
201
+ * fallback: ~/.config/codegraph/config.json
202
+ */
203
+ export function getDefaultUserConfigPath() {
204
+ const envPath = process.env.CODEGRAPH_USER_CONFIG;
205
+ if (envPath)
206
+ return envPath;
207
+ const home = os.homedir();
208
+ const xdgConfig = process.env.XDG_CONFIG_HOME;
209
+ if (xdgConfig)
210
+ return path.join(xdgConfig, 'codegraph', 'config.json');
211
+ if (process.platform === 'win32') {
212
+ const appdata = process.env.APPDATA;
213
+ return appdata
214
+ ? path.join(appdata, 'codegraph', 'config.json')
215
+ : path.join(home, '.config', 'codegraph', 'config.json');
216
+ }
217
+ return path.join(home, '.config', 'codegraph', 'config.json');
218
+ }
219
+ /**
220
+ * Resolve the absolute path to the user-level global config file.
221
+ *
222
+ * Priority:
223
+ * 1. CODEGRAPH_USER_CONFIG env var (location override only — not forced-on)
224
+ * 2. $XDG_CONFIG_HOME/codegraph/config.json (Unix/macOS)
225
+ * %APPDATA%\codegraph\config.json (Windows)
226
+ * fallback: ~/.config/codegraph/config.json
227
+ * 3. ~/.codegraph/config.json (legacy, next to registry.json)
228
+ *
229
+ * Returns the path of the first existing file, or null if none exist.
230
+ */
231
+ export function resolveUserConfigPath() {
232
+ const envPath = process.env.CODEGRAPH_USER_CONFIG;
233
+ if (envPath) {
234
+ if (fs.existsSync(envPath))
235
+ return envPath;
236
+ debug(`CODEGRAPH_USER_CONFIG points to missing file: ${envPath}`);
237
+ return null;
238
+ }
239
+ const home = os.homedir();
240
+ // XDG_CONFIG_HOME takes priority on all platforms when explicitly set.
241
+ // Falls back to %APPDATA% on Windows, or ~/.config on Unix/macOS.
242
+ let platformDefault;
243
+ const xdgConfig = process.env.XDG_CONFIG_HOME;
244
+ if (xdgConfig) {
245
+ platformDefault = path.join(xdgConfig, 'codegraph', 'config.json');
246
+ }
247
+ else if (process.platform === 'win32') {
248
+ const appdata = process.env.APPDATA;
249
+ platformDefault = appdata
250
+ ? path.join(appdata, 'codegraph', 'config.json')
251
+ : path.join(home, '.config', 'codegraph', 'config.json');
252
+ }
253
+ else {
254
+ platformDefault = path.join(home, '.config', 'codegraph', 'config.json');
255
+ }
256
+ if (fs.existsSync(platformDefault))
257
+ return platformDefault;
258
+ const legacyPath = path.join(home, '.codegraph', 'config.json');
259
+ if (fs.existsSync(legacyPath))
260
+ return legacyPath;
261
+ return null;
262
+ }
263
+ /**
264
+ * Read and parse a user-level global config file.
265
+ * Handles both plain-config and appliesTo-wrapper formats.
266
+ * Returns null on missing or malformed files (never throws).
267
+ */
268
+ function loadUserConfigFile(filePath) {
269
+ try {
270
+ const raw = fs.readFileSync(filePath, 'utf-8');
271
+ const parsed = JSON.parse(raw);
272
+ // Wrapper format: { appliesTo: [...], config: {...} }
273
+ if ('appliesTo' in parsed && typeof parsed.config === 'object' && parsed.config !== null) {
274
+ const globs = Array.isArray(parsed.appliesTo)
275
+ ? parsed.appliesTo.filter((g) => typeof g === 'string')
276
+ : [];
277
+ return { globalConfig: parsed.config, appliesToGlobs: globs };
278
+ }
279
+ // Plain config (no appliesTo wrapper)
280
+ return { globalConfig: parsed, appliesToGlobs: [] };
281
+ }
282
+ catch (err) {
283
+ debug(`Failed to load user config at ${filePath}: ${toErrorMessage(err)}`);
284
+ return null;
285
+ }
286
+ }
287
+ // ── Safety sanitisation ─────────────────────────────────────────────────
288
+ /**
289
+ * Drop any unsafe keys from the global layer before merging.
290
+ * Currently: absolute build.dbPath (would make all repos share one DB).
291
+ * Relative dbPaths resolve per-repo and are allowed through unchanged.
292
+ */
293
+ function sanitizeUserLayer(raw) {
294
+ const build = raw.build;
295
+ if (build && typeof build.dbPath === 'string' && path.isAbsolute(build.dbPath)) {
296
+ warn(`User config: build.dbPath "${build.dbPath}" is absolute and was ignored ` +
297
+ '(an absolute dbPath would share one database across all repos).');
298
+ const sanitizedBuild = { ...build };
299
+ delete sanitizedBuild.dbPath;
300
+ return { ...raw, build: sanitizedBuild };
301
+ }
302
+ return raw;
303
+ }
304
+ // ── excludeTests shorthand (per-layer) ─────────────────────────────────
305
+ /**
306
+ * Hoist a top-level `excludeTests` key from a raw layer into `query.excludeTests`.
307
+ * If the layer already has `query.excludeTests`, that value wins (no-op).
308
+ * Also removes any stale `excludeTests` key that may have leaked into `merged`.
309
+ */
310
+ function applyExcludeTestsShorthand(merged, rawLayer) {
311
+ if ('excludeTests' in rawLayer) {
312
+ // Only hoist if this layer doesn't also set query.excludeTests
313
+ if (!(rawLayer.query && 'excludeTests' in rawLayer.query)) {
314
+ merged.query.excludeTests = Boolean(rawLayer.excludeTests);
315
+ }
316
+ const result = { ...merged };
317
+ delete result.excludeTests;
318
+ return result;
319
+ }
320
+ if ('excludeTests' in merged) {
321
+ const result = { ...merged };
322
+ delete result.excludeTests;
323
+ return result;
324
+ }
325
+ return merged;
326
+ }
327
+ /**
328
+ * Resolve whether the global user config should be applied for a given repo.
329
+ * Implements the §4.1/§4.2 precedence chain from the spec.
330
+ *
331
+ * @param rootDir Absolute repo root.
332
+ * @param override Per-run override from CLI flags (_userConfigOverride).
333
+ * @param registryPath Optional registry path (for tests).
334
+ */
335
+ function resolveConsent(rootDir, override, registryPath = REGISTRY_PATH) {
336
+ // §4.1 step 1: --no-user-config
337
+ if (override === false) {
338
+ return { applied: false, globalPath: null, consentDecision: undefined };
339
+ }
340
+ // §4.1 steps 2–3: explicit path or bare --user-config
341
+ if (override !== undefined) {
342
+ const explicitPath = typeof override === 'string' ? override : resolveUserConfigPath();
343
+ if (explicitPath && fs.existsSync(explicitPath)) {
344
+ return { applied: true, globalPath: explicitPath, consentDecision: undefined };
345
+ }
346
+ if (typeof override === 'string') {
347
+ warn(`--user-config path "${override}" does not exist; skipping global layer.`);
348
+ }
349
+ return { applied: false, globalPath: null, consentDecision: undefined };
350
+ }
351
+ // §4.1 step 4: resolve global file — if none, NOT applied
352
+ const globalPath = resolveUserConfigPath();
353
+ if (!globalPath) {
354
+ return { applied: false, globalPath: null, consentDecision: undefined };
355
+ }
356
+ // §4.2: check per-repo decision
357
+ const consentDecision = getUserConfigConsent(rootDir, registryPath);
358
+ // §4.2 step 1: recorded disabled
359
+ if (consentDecision === 'disabled') {
360
+ return { applied: false, globalPath, consentDecision };
361
+ }
362
+ // §4.2 step 2: recorded enabled
363
+ if (consentDecision === 'enabled') {
364
+ return { applied: true, globalPath, consentDecision };
365
+ }
366
+ // §4.2 step 3: appliesTo glob match (dynamic, never persisted)
367
+ const parsed = loadUserConfigFile(globalPath);
368
+ if (parsed?.appliesToGlobs.length) {
369
+ const expanded = parsed.appliesToGlobs.map((g) => g.startsWith('~') ? path.join(os.homedir(), g.slice(1)) : g);
370
+ const regexes = compileGlobs(expanded);
371
+ const absRoot = path.resolve(rootDir);
372
+ if (matchesAny(regexes, absRoot)) {
373
+ return { applied: true, globalPath, consentDecision: undefined };
374
+ }
375
+ }
376
+ // §4.2 steps 4–5: undecided — caller decides whether to prompt
377
+ return { applied: false, globalPath, consentDecision: undefined };
378
+ }
379
+ // Last applied global path and parsed data — exposed so pipeline.ts and
380
+ // loadConfigWithProvenance can reuse the already-parsed file contents without a
381
+ // second disk read (eliminating the TOCTOU window between loadConfig and callers).
382
+ let _lastAppliedGlobalPath = null;
383
+ let _lastAppliedGlobalConfig = null;
384
+ export function getLastAppliedGlobalPath() {
385
+ return _lastAppliedGlobalPath;
386
+ }
387
+ export function getLastAppliedGlobalConfig() {
388
+ return _lastAppliedGlobalConfig;
389
+ }
390
+ // ── Build-relevant config hash ──────────────────────────────────────────
391
+ const BUILD_HASH_KEYS = [
392
+ 'include',
393
+ 'exclude',
394
+ 'ignoreDirs',
395
+ 'extensions',
396
+ 'aliases',
397
+ 'build',
398
+ ];
399
+ /**
400
+ * Compute a short stable hash of the build-relevant config subset.
401
+ * Used by the pipeline to detect config changes that require a full rebuild.
402
+ */
403
+ export function computeConfigHash(config) {
404
+ const subset = {};
405
+ for (const k of BUILD_HASH_KEYS) {
406
+ subset[k] = config[k];
407
+ }
408
+ return createHash('sha256').update(JSON.stringify(subset)).digest('hex').slice(0, 16);
409
+ }
410
+ // ── Interactive consent prompt ──────────────────────────────────────────
411
+ /**
412
+ * When called from the build command, check whether we should prompt the user
413
+ * for global-config consent and, if so, prompt and persist the answer.
414
+ *
415
+ * Only fires when ALL of:
416
+ * - A global config file exists
417
+ * - The repo is undecided (no recorded consent)
418
+ * - Not matched by appliesTo globs
419
+ * - process.stdin.isTTY && process.stdout.isTTY
420
+ * - CI env is not set
421
+ * - No per-run --user-config / --no-user-config flag is active
422
+ */
423
+ export async function promptForConsentIfNeeded(rootDir, registryPath = REGISTRY_PATH) {
424
+ // No-op if per-run override is active
425
+ if (_userConfigOverride !== undefined)
426
+ return;
427
+ const globalPath = resolveUserConfigPath();
428
+ if (!globalPath)
429
+ return;
430
+ const consentDecision = getUserConfigConsent(rootDir, registryPath);
431
+ if (consentDecision !== undefined)
432
+ return; // already decided
433
+ // Check appliesTo globs (dynamic consent — no prompt needed)
434
+ const parsed = loadUserConfigFile(globalPath);
435
+ if (parsed?.appliesToGlobs.length) {
436
+ const expanded = parsed.appliesToGlobs.map((g) => g.startsWith('~') ? path.join(os.homedir(), g.slice(1)) : g);
437
+ const regexes = compileGlobs(expanded);
438
+ const absRoot = path.resolve(rootDir);
439
+ if (matchesAny(regexes, absRoot))
440
+ return; // covered by appliesTo
441
+ }
442
+ // Only prompt in fully interactive sessions
443
+ if (!process.stdin.isTTY || !process.stdout.isTTY)
444
+ return;
445
+ if (process.env.CI)
446
+ return;
447
+ const { createInterface } = await import('node:readline');
448
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
449
+ const answer = await new Promise((resolve) => {
450
+ rl.question(`\nA global codegraph config was found at ${globalPath}.\n` +
451
+ `Apply settings not explicitly configured in this repo to ${path.resolve(rootDir)}? [y/N]\n` +
452
+ `(remembered per-repo; change later with \`codegraph config --enable-global|--disable-global\`)\n` +
453
+ `> `, (ans) => {
454
+ rl.close();
455
+ resolve(ans.trim().toLowerCase());
456
+ });
457
+ });
458
+ const decided = answer === 'y' || answer === 'yes' ? 'enabled' : 'disabled';
459
+ setUserConfigConsent(rootDir, decided, registryPath);
460
+ process.stderr.write(`Global config consent recorded: ${decided}\n`);
461
+ }
166
462
  /**
167
463
  * Load project configuration from a .codegraphrc.json or similar file.
168
- * Returns merged config with defaults. Results are cached per cwd.
464
+ * Returns merged config with defaults: defaults global (if applied) → project → env → secrets.
465
+ * Results are cached per cwd + applied global path.
169
466
  */
170
- export function loadConfig(cwd) {
171
- cwd = cwd || process.cwd();
172
- const cached = _configCache.get(cwd);
173
- if (cached)
467
+ export function loadConfig(cwd, opts) {
468
+ cwd = path.resolve(cwd || process.cwd());
469
+ // Determine effective override: explicit opts win over module-level variable
470
+ const override = opts?.userConfig !== undefined ? opts.userConfig : _userConfigOverride;
471
+ // Resolve consent and global path
472
+ const { applied, globalPath } = resolveConsent(cwd, override, opts?.registryPath);
473
+ // Cache key includes applied global path and override flag so toggled consent is reflected
474
+ const cacheKey = `${cwd}::${applied ? (globalPath ?? 'default') : 'none'}`;
475
+ // Always update _lastAppliedGlobalPath/_lastAppliedGlobalConfig before returning —
476
+ // on a cache hit the previous call may have been for a different repo or different
477
+ // opts, so stale values here would misbehave for programmatic callers making
478
+ // multiple buildGraph calls in the same process.
479
+ _lastAppliedGlobalPath = applied ? globalPath : null;
480
+ _lastAppliedGlobalConfig = null; // updated below if a global file is loaded
481
+ const cached = _configCache.get(cacheKey);
482
+ if (cached) {
483
+ // Restore global config so loadConfigWithProvenance gets correct provenance on cache hits.
484
+ _lastAppliedGlobalConfig = _globalConfigCache.get(cacheKey) ?? null;
174
485
  return structuredClone(cached);
486
+ }
487
+ // ── Layer 0: DEFAULTS ─────────────────────────────────────────────
488
+ let merged = DEFAULTS;
489
+ // ── Layer 1: global (if applied) ──────────────────────────────────
490
+ if (applied && globalPath) {
491
+ const userFileData = loadUserConfigFile(globalPath);
492
+ if (userFileData) {
493
+ debug(`Applying global user config from ${globalPath}`);
494
+ const sanitized = sanitizeUserLayer(userFileData.globalConfig);
495
+ // Cache the sanitized global data so pipeline.ts and loadConfigWithProvenance
496
+ // can use it without a second disk read (eliminates TOCTOU window).
497
+ _lastAppliedGlobalConfig = sanitized;
498
+ merged = mergeConfig(merged, sanitized);
499
+ merged = applyExcludeTestsShorthand(merged, sanitized);
500
+ }
501
+ }
502
+ // ── Layer 2: project ──────────────────────────────────────────────
175
503
  for (const name of CONFIG_FILES) {
176
504
  const filePath = path.join(cwd, name);
177
505
  if (fs.existsSync(filePath)) {
178
506
  try {
179
507
  const raw = fs.readFileSync(filePath, 'utf-8');
180
- const config = JSON.parse(raw);
181
- debug(`Loaded config from ${filePath}`);
182
- const merged = mergeConfig(DEFAULTS, config);
183
- if ('excludeTests' in config && !(config.query && 'excludeTests' in config.query)) {
184
- merged.query.excludeTests = Boolean(config.excludeTests);
185
- }
186
- delete merged.excludeTests;
187
- const result = resolveSecrets(applyEnvOverrides(merged));
188
- _configCache.set(cwd, structuredClone(result));
189
- return result;
508
+ const projectConfig = JSON.parse(raw);
509
+ debug(`Loaded project config from ${filePath}`);
510
+ merged = mergeConfig(merged, projectConfig);
511
+ merged = applyExcludeTestsShorthand(merged, projectConfig);
512
+ break;
190
513
  }
191
514
  catch (err) {
192
515
  if (err instanceof ConfigError)
@@ -195,9 +518,11 @@ export function loadConfig(cwd) {
195
518
  }
196
519
  }
197
520
  }
198
- const defaults = resolveSecrets(applyEnvOverrides({ ...DEFAULTS }));
199
- _configCache.set(cwd, structuredClone(defaults));
200
- return defaults;
521
+ // ── Layers 3–4: env overrides + secret resolution ─────────────────
522
+ const result = resolveSecrets(applyEnvOverrides(merged));
523
+ _configCache.set(cacheKey, structuredClone(result));
524
+ _globalConfigCache.set(cacheKey, _lastAppliedGlobalConfig);
525
+ return result;
201
526
  }
202
527
  /**
203
528
  * Clear the config cache. Intended for long-running processes that need to
@@ -206,6 +531,55 @@ export function loadConfig(cwd) {
206
531
  */
207
532
  export function clearConfigCache() {
208
533
  _configCache.clear();
534
+ _globalConfigCache.clear();
535
+ }
536
+ /**
537
+ * Load config and return it together with per-key provenance information.
538
+ * Used by `codegraph config --explain`.
539
+ *
540
+ * Calls loadConfig first so _lastAppliedGlobalConfig is populated, then uses
541
+ * that cached data for the global-layer provenance — avoiding a second disk
542
+ * read and eliminating the TOCTOU window between the two reads.
543
+ */
544
+ export function loadConfigWithProvenance(cwd, opts) {
545
+ cwd = path.resolve(cwd || process.cwd());
546
+ const override = opts?.userConfig !== undefined ? opts.userConfig : _userConfigOverride;
547
+ const { applied, globalPath, consentDecision } = resolveConsent(cwd, override, opts?.registryPath);
548
+ // Load (or return from cache) the merged config first — this also populates
549
+ // _lastAppliedGlobalConfig with the already-parsed and sanitized global layer.
550
+ const config = loadConfig(cwd, opts);
551
+ // Build provenance by tracking which layer supplies each top-level key
552
+ const provenance = {};
553
+ // Layer 0: defaults — everything starts as 'default'
554
+ for (const k of Object.keys(DEFAULTS))
555
+ provenance[k] = 'default';
556
+ // Layer 1: global — reuse the data loadConfig already parsed (no second disk read)
557
+ const globalRaw = applied && globalPath ? _lastAppliedGlobalConfig : null;
558
+ if (globalRaw) {
559
+ for (const k of Object.keys(globalRaw))
560
+ provenance[k] = 'user';
561
+ }
562
+ // Layer 2: project
563
+ for (const name of CONFIG_FILES) {
564
+ const filePath = path.join(cwd, name);
565
+ if (fs.existsSync(filePath)) {
566
+ try {
567
+ const raw = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
568
+ for (const k of Object.keys(raw))
569
+ provenance[k] = 'project';
570
+ break;
571
+ }
572
+ catch {
573
+ // ignore
574
+ }
575
+ }
576
+ }
577
+ // Layer 3+: env overrides (LLM keys)
578
+ const ENV_LLM_KEYS = ['CODEGRAPH_LLM_PROVIDER', 'CODEGRAPH_LLM_API_KEY', 'CODEGRAPH_LLM_MODEL'];
579
+ if (ENV_LLM_KEYS.some((k) => process.env[k] !== undefined)) {
580
+ provenance.llm = 'env';
581
+ }
582
+ return { config, provenance, appliedGlobalPath: applied ? globalPath : null, consentDecision };
209
583
  }
210
584
  const ENV_LLM_MAP = {
211
585
  CODEGRAPH_LLM_PROVIDER: 'provider',