Skip to content

refactor: adopt dead helpers identified by Titan grind#1793

Open
carlos-alm wants to merge 9 commits into
fix/titan-quality-fixes-ast-presentation-clifrom
refactor/titan-grind-adoptions
Open

refactor: adopt dead helpers identified by Titan grind#1793
carlos-alm wants to merge 9 commits into
fix/titan-quality-fixes-ast-presentation-clifrom
refactor/titan-grind-adoptions

Conversation

@carlos-alm

Copy link
Copy Markdown
Contributor

Summary

Adopt dead helpers identified by the Titan GRIND phase — wiring previously-extracted-but-unused helpers into real call sites, and promoting/deduping a few forge-phase helpers that turned out to have further adoption opportunities:

  • Adopt fget/iget from typed-array-helpers.ts in cpm.ts/modularity.ts (leiden).
  • Wire db.busyTimeoutMs and community.capacityGrowthFactor config constants through their real call sites (db/connection, builder pipeline, watcher, communities, leiden).
  • Adopt resolveMethodDefinitionName across 3 duplicate call sites in extractors/javascript.ts.
  • Dedupe forge phase-11 call-resolution helpers (unwrapTypeEntry, resolveSameClassQualifiedMethod) across build-edges.ts/incremental.ts.
  • Extract markExportedSymbols, dedupe a batch UPDATE in builder helpers.
  • Promote timeMedian/median/round1 into a shared scripts/lib/bench-timing.ts, deduping the same logic across 3 benchmark scripts.

Titan Audit Context

Changes

  • src/graph/algorithms/leiden/{cpm,modularity}.ts
  • src/db/connection.ts, src/domain/graph/builder/{pipeline,watcher}..., src/domain/graph/builder/stages/{native-db-lifecycle,native-orchestrator}.ts, src/features/communities.ts, src/graph/algorithms/leiden/{optimiser,partition}.ts, src/graph/algorithms/louvain.ts, src/infrastructure/config.ts, src/types.ts
  • src/extractors/javascript.ts
  • src/domain/graph/builder/{call-resolver,incremental}.ts, src/domain/graph/builder/stages/build-edges.ts, src/domain/graph/resolver/strategy.ts
  • src/domain/graph/builder/{helpers,stages/insert-nodes,stages/native-orchestrator}.ts
  • scripts/{benchmark,incremental-benchmark,query-benchmark,token-benchmark}.ts, new scripts/lib/bench-timing.ts

Metrics Impact

The Titan GRIND phase triaged 24 candidate dead-symbol targets: 6 were genuinely adoptable (this PR), 3 were promoted/deduped further (also this PR), and the remaining 15 were confirmed false positives caused by known codegraph role-classifier bugs (documented in generated/titan/ report and filed as GitHub issues).

Test plan

  • CI passes (lint + build + tests)
  • codegraph check --cycles --boundaries passes
  • No new functions above complexity thresholds

Split loader-hooks.mjs's instrumentSource (cognitive 34, cyclomatic 22)
into single-purpose matcher/tracker helpers. Extract the duplicated
line-scanning loop shared by native-tracer.sh's trace_rust/trace_swift/
trace_dart/trace_zig into a parameterized inject_trace_calls plus small
maybe_close_context/maybe_close_finally_scope/maybe_inject_declaration/
instrument_one_file helpers, and rename the LANG variable to TRACE_LANG
so it no longer clobbers the POSIX locale env var inherited by every
spawned compiler toolchain. Log lua-tracer.lua's swallowed pcall error
to stderr so a genuine fixture failure is visible instead of looking
like a silent successful trace.

Impact: 17 functions changed, 11 affected
…ing (docs check acknowledged)

Impact: 12 functions changed, 36 affected
…and modularity.ts

Forge phase 3 extracted fget/iget/u8get/taAdd into typed-array-helpers.ts
from adapter.ts/index.ts/partition.ts, but two sibling leiden files —
cpm.ts and modularity.ts — carried byte-for-byte identical private copies
of fget/iget (down to the "see adapter.ts for rationale" comment) that
were never migrated to the shared module. Codebase-wide grep/symbol scans
found no other duplicates of this pattern outside the leiden directory.

Both functions were unexported, file-local helpers with zero external
callers (codegraph exports confirms neither appears in either file's
export list), so replacing the private declarations with an import from
the shared module is a pure dedup with no behavioral change.

docs check acknowledged: internal helper adoption within an already-vendored
algorithm module, no user-facing feature/language/architecture-table changes.
…actor

Forge phase 7 (8fed8bc) added these as reserved DEFAULTS entries but left
their consumers on hardcoded literals.

db.busyTimeoutMs: openDb and openReadonlyOrFail in src/db/connection.ts now
take an optional busyTimeoutMs parameter defaulting to DEFAULTS.db.busyTimeoutMs.
Build and watch call sites that already hold a resolved config (pipeline,
native orchestrator, native-db-lifecycle, watcher, openRepo and
openReadonlyWithNative via the renamed resolveDbSettings helper) pass the
user-configured value through explicitly. This partially addresses issue 1749,
whose busy-locked regex dedup half remains open; remaining read-only query
call sites and the Rust connection.rs mirror are tracked in issue 1763.

community.capacityGrowthFactor is threaded through the existing maxLevels
and refinementTheta plumbing, from communitiesData through
louvainCommunities and detectClusters into makePartition, so
ensureCommCapacity in src/graph/algorithms/leiden/partition.ts reads the
configured value instead of a hardcoded 1.5 growth multiplier. It is
ignored by the native Rust Louvain path, consistent with the other options.

largeCodebaseFileThreshold, the third reserved entry from phase 7, was
already wired by a later forge commit and needed no action.

docs check acknowledged: internal config wiring only, already documented
in docs/guides/configuration.md by forge phase 7.

Impact: 31 functions changed, 171 affected
…icate sites

handleMethodCapture, handleMethodDef, and extractObjectLiteralFunctions each
inlined the identical computed_property_name unwrap-and-strip-quotes logic
that forge phase 10 already extracted into resolveMethodDefinitionName for
pushMethodDefContext. Consolidate onto the shared helper; no behavior change.

docs check acknowledged: internal-only duplicate-code consolidation, no new
languages/commands/architecture; README.md, CLAUDE.md, and ROADMAP.md are
unaffected.

Impact: 3 functions changed, 11 affected
Phase 11 decomposed build-edges.ts's resolveFallbackTargets into 15 named
helpers. The codebase-wide duplicate scan on those new helpers surfaced two
pre-existing duplicates the decomposition didn't catch (both predate phase 11
temporally, so the consolidation opportunities weren't nameable until now):

- resolveReflectionKeyExprFallback and resolveDefinePropertyAccessorFallback
  each inlined the same string/{type} typeMap-entry unwrap ternary that
  strategy.ts's unwrapTypeEntry already implements (extracted one commit
  after phase 11, in forge phase 20). Promoted unwrapTypeEntry to an export
  and wired both call sites to it.
- build-edges.ts's newly-named resolveSameClassQualifiedMethod was
  byte-identical to incremental.ts's pre-existing resolveThisSameClassTarget
  (incremental.ts's own docstring already noted it mirrors "the full-build
  counterpart"). Moved the shared implementation into call-resolver.ts --
  the module whose stated purpose is holding call-resolution logic shared
  between build-edges.ts and incremental.ts exactly once -- and wired both
  consumers to it.

Zero behavior change: all replaced logic was verified byte-identical before
relocating. Resolution benchmark (206/206) and full suite (3340 tests) pass
identically before/after.

Two follow-on divergences the scan also surfaced between the full-build and
incremental resolution paths (not fixed here -- would change behavior, not
just relocate it): incremental.ts has no same-class bare-call fallback
(#1765), and the Object.defineProperty same-file fallback's kind-filter
differs between the two paths (#1766).

docs check acknowledged: pure internal dedup/extract-method refactor, no
new features, commands, languages, or architecture changes -- README/CLAUDE/
ROADMAP do not need updates.

Impact: 5 functions changed, 20 affected
…docs check acknowledged)

insertDefinitionsAndExports (insert-nodes.ts) and insertBackfilledNodes
(native-orchestrator.ts) duplicated the exact same chunked-UPDATE loop for
marking exported symbols -- the latter's own comment even said "mirrors
insertDefinitionsAndExports". Extract markExportedSymbols(db, exportKeys) in
builder/helpers.ts, built on the existing getOrCreateBatchStmt/runBatchInsert
primitives from the batch-insert dedupe, and wire both call sites to it.

Impact: 4 functions changed, 20 affected
…-timing lib

Forge phase 28 extracted timeMedian in token-benchmark.ts, already wired
within that file. The codebase-wide duplicate scan found median()/round1()
duplicated verbatim across query-benchmark.ts, incremental-benchmark.ts
(twice), and benchmark.ts, plus the "loop N times, time each run, median()"
pattern repeated 8 more times across those files.

Promotes median/round1/timeMedian into scripts/lib/bench-timing.ts
(matching the existing bench-config.ts/fork-engine.ts shared-module
pattern) and wires median/round1 into all 4 scripts. Adopts timeMedian at
the 3 call sites that were already unconditionally async with no new
async boundary (incremental-benchmark.ts fullBuildMs/noopRebuildMs worker,
benchmark.ts noopRebuildMs worker).

Left 5 remaining duplicate sites unconverted since adopting timeMedian
there requires making currently-synchronous functions async, which is a
control-flow change rather than a mechanical adoption -- tracked in #1774.

Impact: 4 functions changed, 14 affected
@greptile-apps

greptile-apps Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR wires previously-extracted-but-unused helpers into real call sites across 25 files, reducing duplication without altering observable behaviour. All changes are mechanical adoptions: promoting shared helpers (fget/iget, resolveMethodDefinitionName, resolveSameClassQualifiedMethod, unwrapTypeEntry, markExportedSymbols, timeMedian/median/round1) and threading two previously-hardcoded config constants (busyTimeoutMs, capacityGrowthFactor) through their full call paths.

  • scripts/lib/bench-timing.ts (new): Canonical median/round1/timeMedian replaces four independent duplicates across the benchmark scripts; timeMedian now takes an explicit runs parameter (the old local copy in token-benchmark.ts had a default of PERF_RUNS), and all call sites pass the constant explicitly.
  • src/db/connection.ts: resolveDbEngine becomes resolveDbSettings, adding busyTimeoutMs to the returned struct so the same loadConfig() call covers both fields; all openDb invocations in pipeline, watcher, and native-orchestrator now pass the user-configured value.
  • src/graph/algorithms/leiden/partition.ts: Exports DEFAULT_CAPACITY_GROWTH_FACTOR (previously a silent triplicate in optimiser.ts, partition.ts, and config.ts), reducing drift surface; capacityGrowthFactor is now a typed field on PartitionState and flows from communitiesData() all the way to ensureCommCapacity().

Confidence Score: 5/5

Safe to merge — all changes are mechanical deduplication of helpers that were already proven correct at their original sites.

Every adoption is a like-for-like substitution: the extracted functions reproduce the exact same logic as the inlined code they replace. The busyTimeoutMs threading correctly defaults to DEFAULTS.db.busyTimeoutMs at both the parameter level and the resolveDbSettings fallback. The resolveMethodDefinitionName consolidation preserves the per-site early-exit variant (return vs continue). The markExportedSymbols refactor upgrades from a per-call Map to a module-level WeakMap cache, which is strictly better. No logic changes were made — only consolidation of duplicates.

No files require special attention beyond the minor style inconsistency in scripts/lib/bench-timing.ts (missing explicit performance import).

Important Files Changed

Filename Overview
scripts/lib/bench-timing.ts New shared timing lib extracting median/round1/timeMedian; uses global performance without an explicit import (all other scripts import from node:perf_hooks)
src/db/connection.ts Refactors resolveDbEngine → resolveDbSettings to return both engine and busyTimeoutMs in a single loadConfig call; openDb/openReadonlyOrFail now accept a busyTimeoutMs parameter defaulting to DEFAULTS.db.busyTimeoutMs
src/domain/graph/builder/call-resolver.ts Promotes the resolveSameClassQualifiedMethod helper (previously duplicated in build-edges.ts and incremental.ts) to a shared export; logic is unchanged
src/domain/graph/builder/helpers.ts Adds markExportedSymbols backed by a module-level WeakMap cache (upgrade from per-call Map), deduplicating the chunked UPDATE loop from insert-nodes.ts and native-orchestrator.ts
src/extractors/javascript.ts Three independent computed-property-name unwrapping blocks replaced by calls to the shared resolveMethodDefinitionName; behavior preserved for all three call sites (return/return/continue early-exit variants)
src/graph/algorithms/leiden/partition.ts Exports DEFAULT_CAPACITY_GROWTH_FACTOR (reducing from 3 copies to 2), adds capacityGrowthFactor to PartitionState so ensureCommCapacity uses the configurable value instead of the hardcoded 1.5 literal
src/graph/algorithms/leiden/optimiser.ts Removes its own DEFAULT_CAPACITY_GROWTH_FACTOR copy, now importing from partition.ts; threads capacityGrowthFactor through NormalizedOptions and into both makePartition call sites
src/domain/graph/builder/stages/build-edges.ts Adopts resolveSameClassQualifiedMethod from call-resolver and unwrapTypeEntry from resolver/strategy, removing two inline duplicates; logic unchanged
src/domain/graph/builder/stages/native-orchestrator.ts All openDb calls now pass ctx.config.db.busyTimeoutMs; duplicate export-marking loop replaced with markExportedSymbols
src/graph/algorithms/louvain.ts Threads capacityGrowthFactor through LouvainOptions; native Rust path now logs a warning when it is set (same pattern as the other Leiden-only options)
src/features/communities.ts Adds capacityGrowthFactor to CommunitiesDataOpts and threads opts.capacityGrowthFactor ?? config.community?.capacityGrowthFactor through to louvainCommunities

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant CD as communitiesData()
    participant LC as louvainCommunities()
    participant LJ as louvainJS() / detectClusters()
    participant MP as makePartition()
    participant EC as ensureCommCapacity()

    CD->>LC: capacityGrowthFactor (opts ?? config)
    LC->>LJ: "capacityGrowthFactor (if != null)"
    LJ->>LJ: normalizeOptions() → capacityGrowthFactor (or DEFAULT_CAPACITY_GROWTH_FACTOR)
    LJ->>MP: "makePartition(graph, { capacityGrowthFactor })"
    MP->>EC: s.capacityGrowthFactor stored in PartitionState
    EC->>EC: "Math.ceil(length * s.capacityGrowthFactor) — previously hardcoded 1.5"
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant CD as communitiesData()
    participant LC as louvainCommunities()
    participant LJ as louvainJS() / detectClusters()
    participant MP as makePartition()
    participant EC as ensureCommCapacity()

    CD->>LC: capacityGrowthFactor (opts ?? config)
    LC->>LJ: "capacityGrowthFactor (if != null)"
    LJ->>LJ: normalizeOptions() → capacityGrowthFactor (or DEFAULT_CAPACITY_GROWTH_FACTOR)
    LJ->>MP: "makePartition(graph, { capacityGrowthFactor })"
    MP->>EC: s.capacityGrowthFactor stored in PartitionState
    EC->>EC: "Math.ceil(length * s.capacityGrowthFactor) — previously hardcoded 1.5"
Loading

Reviews (3): Last reviewed commit: "fix(leiden): dedupe DEFAULT_CAPACITY_GRO..." | Re-trigger Greptile

// via .codegraphrc.json. Callers (e.g. louvain.ts) can pass overrides through options.
const DEFAULT_MAX_LEVELS: number = 50;
const DEFAULT_MAX_LOCAL_PASSES: number = 20;
const DEFAULT_CAPACITY_GROWTH_FACTOR: number = 1.5;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Three copies of the same 1.5 default now exist independently: DEFAULT_CAPACITY_GROWTH_FACTOR here, DEFAULT_CAPACITY_GROWTH_FACTOR in partition.ts, and DEFAULTS.community.capacityGrowthFactor in config.ts. The value in optimiser.ts normalises the option before it reaches makePartition, but partition.ts also keeps its own copy as a last-resort fallback for callers that skip the optimiser layer. If the tuning value ever changes, all three sites must be updated atomically — any miss silently produces mismatched growth behaviour depending on which code path ran. Importing the constant from one canonical location would close that gap.

Suggested change
const DEFAULT_CAPACITY_GROWTH_FACTOR: number = 1.5;
import { DEFAULT_CAPACITY_GROWTH_FACTOR } from './partition.js';

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — exported DEFAULT_CAPACITY_GROWTH_FACTOR from partition.ts and imported it in optimiser.ts instead of keeping an independent literal. The third copy (DEFAULTS.community.capacityGrowthFactor in config.ts) is intentionally kept separate — it follows the same established pattern as DEFAULT_MAX_LEVELS/DEFAULT_MAX_LOCAL_PASSES in this file (module-level fallback mirrored from the user-facing config default, documented via comment), so no further action needed there.

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Codegraph Impact Analysis

47 functions changed231 callers affected across 114 files

  • median in scripts/lib/bench-timing.ts:24 (13 transitive callers)
  • round1 in scripts/lib/bench-timing.ts:34 (10 transitive callers)
  • timeMedian in scripts/lib/bench-timing.ts:44 (5 transitive callers)
  • runPerfBenchmarks in scripts/token-benchmark.ts:262 (2 transitive callers)
  • openDb in src/db/connection.ts:161 (21 transitive callers)
  • openReadonlyOrFail in src/db/connection.ts:333 (126 transitive callers)
  • ResolvedDbSettings.engine in src/db/connection.ts:360 (0 transitive callers)
  • ResolvedDbSettings.busyTimeoutMs in src/db/connection.ts:361 (0 transitive callers)
  • resolveDbSettings in src/db/connection.ts:374 (24 transitive callers)
  • openRepo in src/db/connection.ts:427 (28 transitive callers)
  • openReadonlyWithNative in src/db/connection.ts:482 (8 transitive callers)
  • resolveSameClassQualifiedMethod in src/domain/graph/builder/call-resolver.ts:48 (8 transitive callers)
  • getExportStmt in src/domain/graph/builder/helpers.ts:453 (15 transitive callers)
  • markExportedSymbols in src/domain/graph/builder/helpers.ts:470 (7 transitive callers)
  • applyThisReceiverFallbacks in src/domain/graph/builder/incremental.ts:600 (5 transitive callers)
  • setupPipeline in src/domain/graph/builder/pipeline.ts:173 (4 transitive callers)
  • runPipelineStages in src/domain/graph/builder/pipeline.ts:277 (4 transitive callers)
  • resolveReflectionKeyExprFallback in src/domain/graph/builder/stages/build-edges.ts:1137 (3 transitive callers)
  • resolveDefinePropertyAccessorFallback in src/domain/graph/builder/stages/build-edges.ts:1182 (3 transitive callers)
  • insertDefinitionsAndExports in src/domain/graph/builder/stages/insert-nodes.ts:258 (3 transitive callers)

…ledged)

optimiser.ts kept its own copy of the 1.5 fallback instead of importing
partition.ts's constant, so the two could silently drift apart. Export
it from partition.ts and import it in optimiser.ts. Addresses Greptile
review feedback on #1793.
@carlos-alm

Copy link
Copy Markdown
Contributor Author

Addressed Greptile's review feedback: exported DEFAULT_CAPACITY_GROWTH_FACTOR from partition.ts and imported it in optimiser.ts (commit da0c267) instead of keeping an independently-drifting literal. See inline reply for details on why the third copy in config.ts is intentionally kept separate.

@carlos-alm

Copy link
Copy Markdown
Contributor Author

@greptileai

Base automatically changed from refactor/titan-warning-improvements to fix/titan-quality-fixes-ast-presentation-cli July 5, 2026 02:26
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