Skip to content

refactor: DRY shared abstractions in TS features#843

Merged
carlos-alm merged 3 commits intomainfrom
refactor/titan-ts-dry-abstractions
Apr 4, 2026
Merged

refactor: DRY shared abstractions in TS features#843
carlos-alm merged 3 commits intomainfrom
refactor/titan-ts-dry-abstractions

Conversation

@carlos-alm
Copy link
Copy Markdown
Contributor

Summary

  • Extract shared node-role classification logic from classifyNodeRolesFull and classifyNodeRolesIncremental (near-duplicates, both cog=27) into a single classifyNodeRole function
  • Unify buildNodeDataflowResult and buildNativeDataflowResult (near-duplicate result builders) into a single parameterized buildDataflowResult function

Titan Audit Context

  • Phase: abstraction
  • Domain: features
  • Commits: 2
  • Depends on: none

Changes

  • src/features/structure.ts -- extract shared classifyNodeRole, reducing duplication between Full and Incremental variants
  • src/features/dataflow.ts -- unify duplicate dataflow result builders into parameterized function

Metrics Impact

  • classifyNodeRolesFull: cog 27 -> 4, classifyNodeRolesIncremental: cog 27 -> 4
  • Eliminates ~90 lines of duplicated logic

Test plan

  • CI passes (lint + build + tests)
  • Structure analysis produces same results
  • Dataflow analysis produces same results for both native and WASM engines

Extract buildRoleSummary() and batchUpdateRoles() helpers shared between
classifyNodeRolesFull and classifyNodeRolesIncremental, eliminating
~80 lines of near-identical code.
Extract RawDataflowEdges interface and buildDataflowResult() as the shared
core between buildNodeDataflowResult (SQL path) and buildNativeDataflowResult
(native path), eliminating ~45 lines of duplicated filter/assembly logic.
@claude
Copy link
Copy Markdown

claude bot commented Apr 4, 2026

Claude finished @carlos-alm's task —— View job


Reviewing PR: refactor: DRY shared abstractions in TS features

Todo List:

  • Read modified files to understand the refactoring approach
  • Phase 1: Root cause analysis - determine what problem this solves
  • Phase 2: Configuration change detection - check for any rule relaxation
  • Phase 3: Test integrity - verify no test weakening
  • Phase 4: Code quality assessment
  • Phase 5: Backlog compliance evaluation
  • Provide final structured assessment

Root Cause Analysis

Problem Being Solved: Technical debt from code duplication in two critical areas:

  1. structure.ts - classifyNodeRolesFull and classifyNodeRolesIncremental had near-identical logic (both cog=27) for building role summaries and batch-updating roles
  2. dataflow.ts - buildNodeDataflowResult and buildNativeDataflowResult contained ~45 lines of duplicated filter/assembly logic for the same result shape

Why This Approach: ✓ The refactoring extracts shared logic into parameterized helper functions while preserving the existing APIs. This is a proper abstraction that reduces cognitive complexity from 27→4 and eliminates duplicate code without changing behavior.

Risk Assessment: Low risk - pure refactoring with no API changes, no configuration modifications, and only internal helper function extraction.

Backlog Compliance

  • Zero-dep: ✓ - No new runtime dependencies added
  • Foundation-aligned: ✓ - Pure internal refactoring, no principle violations
  • Problem-fit: 3/5 - Improves maintainability and reduces complexity for agent-facing features, but doesn't directly address AI token waste/hallucination
  • Breaking: No - All existing APIs preserved, purely additive internal helpers
  • Tier: 1 (zero-dep ✓ + foundation-aligned ✓)

Code Quality Assessment ✓

Abstraction Quality: Excellent - both extractions follow clean abstraction principles:

  • buildRoleSummary() + batchUpdateRoles() in structure.ts:453,491 eliminate role classification duplication
  • buildDataflowResult() in dataflow.ts:480 unifies SQL/native result building with proper parameterization

Type Safety: Strong - new interfaces RawDataflowEdges and parameterized functions maintain full type safety

Performance: Neutral/positive - eliminates code duplication without performance overhead

Maintainability: Major improvement - single source of truth for complex logic, cognitive complexity reduced from 27→4 per function

Final Recommendation

  • Rating: ⭐⭐⭐⭐☆ (4/5)
  • Action: APPROVE
  • Reasoning: Excellent refactoring that meaningfully reduces technical debt in critical graph analysis components. Clean abstractions with no API breakage. The complexity reduction (cog 27→4) and ~90 lines of duplicate code elimination provide clear maintainability wins. Problem-fit is moderate (3/5) as this improves code quality but doesn't directly solve the core AI navigation problem - however, the quality improvements to these foundational components are valuable.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 4, 2026

Greptile Summary

This PR DRYs up two pairs of near-duplicate functions: buildRoleSummary and batchUpdateRoles are extracted from classifyNodeRolesFull/classifyNodeRolesIncremental in structure.ts, while buildDataflowResult (backed by the new RawDataflowEdges interface) replaces the twin result builders in dataflow.ts. All original behaviour is faithfully preserved — transaction semantics, closure-captured SQL parameters, and the pre-existing non-filtering of mutates/mutatedBy under noTests are all unchanged.

Confidence Score: 5/5

Safe to merge — a clean structural refactor with no behaviour changes.

Both extractions are faithful: closure-captured variables resolve correctly in the resetFn lambda, transaction boundaries are identical to the originals, all edge-field mappings match their pre-refactor counterparts, and the NormalizedSymbol import resolves to a proper exported type. No P0 or P1 findings.

No files require special attention.

Important Files Changed

Filename Overview
src/features/structure.ts Extracts buildRoleSummary and batchUpdateRoles helpers to DRY up classifyNodeRolesFull and classifyNodeRolesIncremental; logic is faithfully preserved, closure-captured variables (placeholders, allAffectedFiles) correctly resolve inside the resetFn lambda, and transaction semantics are unchanged.
src/features/dataflow.ts Unifies buildNodeDataflowResult and buildNativeDataflowResult behind a shared buildDataflowResult(sym, edges, noTests) function with a typed RawDataflowEdges interface; all field mappings are correct, NormalizedSymbol import resolves, and the pre-existing behaviour of not filtering mutates/mutatedBy by isTestFile is preserved faithfully.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    subgraph structure.ts
        CNR[classifyNodeRoles] -->|full| CNRF[classifyNodeRolesFull]
        CNR -->|incremental| CNRI[classifyNodeRolesIncremental]
        CNRF --> BRS[buildRoleSummary]
        CNRI --> BRS
        CNRF --> BUR[batchUpdateRoles]
        CNRI --> BUR
        BUR -->|resetFn callback| RESET_FULL["reset: UPDATE nodes SET role = NULL"]
        BUR -->|resetFn callback| RESET_INC["reset: UPDATE nodes SET role = NULL WHERE file IN (...)"]
    end

    subgraph dataflow.ts
        DD[dataflowData] -->|native path| BNDR[buildNativeDataflowResult]
        DD -->|SQL path| BNDR2[buildNodeDataflowResult]
        BNDR --> BDR[buildDataflowResult]
        BNDR2 --> BDR
        BDR -->|noTests=true| FILTERED[filtered result]
        BDR -->|noTests=false| UNFILTERED[unfiltered result]
    end
Loading

Reviews (1): Last reviewed commit: "refactor: unify duplicate dataflow resul..." | Re-trigger Greptile

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

Codegraph Impact Analysis

13 functions changed9 callers affected across 5 files

  • RawDataflowEdges.flowsTo in src/features/dataflow.ts:446 (0 transitive callers)
  • RawDataflowEdges.flowsFrom in src/features/dataflow.ts:455 (0 transitive callers)
  • RawDataflowEdges.returnConsumers in src/features/dataflow.ts:464 (0 transitive callers)
  • RawDataflowEdges.returnedBy in src/features/dataflow.ts:471 (0 transitive callers)
  • RawDataflowEdges.mutatesTargets in src/features/dataflow.ts:472 (0 transitive callers)
  • RawDataflowEdges.mutatedBy in src/features/dataflow.ts:473 (0 transitive callers)
  • buildDataflowResult in src/features/dataflow.ts:480 (5 transitive callers)
  • buildNodeDataflowResult in src/features/dataflow.ts:509 (4 transitive callers)
  • buildNativeDataflowResult in src/features/dataflow.ts:564 (4 transitive callers)
  • buildRoleSummary in src/features/structure.ts:453 (3 transitive callers)
  • batchUpdateRoles in src/features/structure.ts:491 (3 transitive callers)
  • classifyNodeRolesFull in src/features/structure.ts:518 (1 transitive callers)
  • classifyNodeRolesIncremental in src/features/structure.ts:617 (1 transitive callers)

@carlos-alm carlos-alm merged commit 19e8a72 into main Apr 4, 2026
10 of 13 checks passed
@carlos-alm carlos-alm deleted the refactor/titan-ts-dry-abstractions branch April 4, 2026 22:08
@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant