Skip to content

fix(perf): resolve config once in withRepo to avoid double loadConfig#2069

Open
carlos-alm wants to merge 1 commit into
fix/issue-1938-follow-up-checknodeletedexportsinuse-missesfrom
fix/issue-1941-perf-withrepo-fn-impact-ts-has-the-same
Open

fix(perf): resolve config once in withRepo to avoid double loadConfig#2069
carlos-alm wants to merge 1 commit into
fix/issue-1938-follow-up-checknodeletedexportsinuse-missesfrom
fix/issue-1941-perf-withrepo-fn-impact-ts-has-the-same

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

  • withRepo() (backing fn-impact.ts, brief.ts, dependencies.ts, implementations.ts) opens its Repository via openRepo(), which internally resolves config via resolveDbSettings() for engine/busy-timeout selection. Its two callers that also needed analysis-tuning config — fnImpactData via resolveAnalysisOpts(), and briefData via a direct loadConfig() call — triggered a second, independent loadConfig() for the same DB path.
  • Beyond the redundant work, that second call resolved its rootDir from process.cwd() instead of the resolved --db path, so --db /other/repo/.codegraph/graph.db would silently read the wrong project's .codegraphrc.json for analysis.fnImpactDepth/analysis.briefCallerDepth — the same correctness gap already fixed for withReadonlyDb()'s callers (exports.ts, context.ts).
  • Fix: widen withRepo() to resolve config once and thread it through to both openRepo() (via a new, additive opts.config — every other openRepo() call site is unaffected) and the callback, mirroring withReadonlyDb's (db, config) => T signature. fnImpactData and briefData now reuse opts.config ?? dbConfig instead of calling loadConfig() again.
  • dependencies.ts and implementations.ts — the other two withRepo callers — don't read config, so they're unaffected; TypeScript allows their existing (repo) => T callbacks to ignore the new second parameter.

No native/Rust changes: this is a TS-only config-resolution optimization internal to db/connection.ts / domain/analysis/query-helpers.ts orchestration — the native crate has no equivalent concept to mirror.

Fixes #1941

Test plan

  • New tests/unit/withrepo-config-resolution.test.ts: spies on loadConfig to assert withRepo, fnImpactData, and briefData each call it exactly once (previously twice for the latter two); also asserts analysis.fnImpactDepth/analysis.briefCallerDepth are read from the --db path's rootDir even when process.cwd() points elsewhere. Verified these tests fail against the pre-fix code (2 loadConfig calls / config not threaded) and pass after the fix.
  • npx tsc --noEmit — clean
  • npm run lint — clean
  • codegraph diff-impact --staged -T — confirms only the intended 5 functions changed (resolveDbSettings, openRepo, briefData, fnImpactData, withRepo), 43 callers across 33 files, all backward-compatible
  • npm test — 225/259 files and 3688/4134 tests pass. The remaining 20 failing files (6 individual test failures) all share one root cause unrelated to this change: Cannot find module '@optave/codegraph-darwin-arm64' — this worktree has no built native addon, a pre-existing, already-documented environment gap for isolated worktrees, not something this diff touches.

withRepo() opened its Repository via openRepo(), which independently
resolves config for engine/busy-timeout selection, while its two callers
that also need analysis-tuning config -- fnImpactData() via
resolveAnalysisOpts(), briefData() via a direct loadConfig() call --
triggered a second, independent loadConfig() for the same DB path.

Beyond the redundant work, the second call resolved its rootDir from
process.cwd() instead of the resolved --db path, so a --db pointing at a
different repo than cwd would silently read the wrong project's
.codegraphrc.json for analysis.fnImpactDepth/analysis.briefCallerDepth --
the same correctness gap already fixed for withReadonlyDb()'s callers.

Widen withRepo() to resolve config once and thread it through to both
openRepo() (via a new, additive opts.config -- every other openRepo()
caller is unaffected) and the callback, mirroring withReadonlyDb's
(db, config) => T signature. fnImpactData and briefData now reuse
opts.config ?? dbConfig instead of loading config again. dependencies.ts
and implementations.ts, the other two withRepo callers, don't read
config, so their existing (repo) => T callbacks are unaffected.
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR resolves repository config once in the withRepo() path. The main changes are:

  • openRepo() can reuse a pre-resolved config for engine and busy-timeout selection.
  • withRepo() now passes the DB-path config to both openRepo() and its callback.
  • fnImpactData() and briefData() reuse that config instead of loading from process.cwd().
  • Regression tests cover single config loading and DB-path-based analysis settings.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.
  • Existing one-argument withRepo() callbacks remain compatible.
  • The new config path keeps normal object overrides working while removing the duplicate cwd-based load.

Important Files Changed

Filename Overview
src/db/connection.ts Adds an optional config parameter to the repository-opening path while preserving existing callers.
src/domain/analysis/query-helpers.ts withRepo() now resolves config once and passes it through to the repository and callback.
src/domain/analysis/fn-impact.ts fnImpactData() now uses the DB-path config when resolving analysis options.
src/domain/analysis/brief.ts briefData() now reads analysis config from the config supplied by withRepo().
tests/unit/withrepo-config-resolution.test.ts Adds tests for the single-load behavior and DB-path config resolution.

Reviews (1): Last reviewed commit: "fix(perf): resolve config once in withRe..." | Re-trigger Greptile

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

5 functions changed43 callers affected across 33 files

  • resolveDbSettings in src/db/connection.ts:451 (24 transitive callers)
  • openRepo in src/db/connection.ts:585 (28 transitive callers)
  • briefData in src/domain/analysis/brief.ts:105 (3 transitive callers)
  • fnImpactData in src/domain/analysis/fn-impact.ts:265 (2 transitive callers)
  • withRepo in src/domain/analysis/query-helpers.ts:38 (21 transitive callers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant