Skip to content

refactor: address SLOC warnings in domain and features layers#621

Merged
carlos-alm merged 9 commits intomainfrom
refactor/titan-sloc-warnings
Mar 26, 2026
Merged

refactor: address SLOC warnings in domain and features layers#621
carlos-alm merged 9 commits intomainfrom
refactor/titan-sloc-warnings

Conversation

@carlos-alm
Copy link
Contributor

Summary

  • Splits complexity.ts and structure.ts to extract query-building logic into dedicated modules
  • New files: complexity-query.ts, structure-query.ts
  • Addresses SLOC warnings from Titan audit (Phase 5)

Titan Audit Context

Changes

  • src/features/complexity.ts -- query logic extracted to complexity-query.ts
  • src/features/complexity-query.ts -- new: complexity data querying and formatting
  • src/features/structure.ts -- query logic extracted to structure-query.ts
  • src/features/structure-query.ts -- new: structure data querying and formatting

Metrics Impact

  • Reduces SLOC in complexity.ts and structure.ts below warning threshold
  • Blast radius: 5 (gate-validated)

Test plan

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

@claude
Copy link

claude bot commented Mar 26, 2026

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


Root Cause Analysis

Problem Being Solved: Addressing SLOC (Source Lines of Code) warnings from Titan audit Phase 5 by extracting query logic from large files (complexity.ts, structure.ts) into dedicated modules.
Why This Approach: Legitimate architectural refactoring following separation of concerns - build-time logic (AST parsing, metrics computation) remains in core files while query-time logic (DB reads, pagination, formatting) moves to -query.ts modules.
Risk Assessment: Low risk - maintains backward compatibility via re-export barrels, follows established patterns from dependency PRs #616/#617.

Backlog Compliance

  • Zero-dep: ✓ — No new runtime dependencies added
  • Foundation-aligned: ✓ — Pure refactoring, maintains all core principles
  • Problem-fit: 3 — Code organization improvement, indirectly helps AI navigation through better module boundaries
  • Breaking: No — Full backward compatibility via re-exports
  • Tier: 1

Code Quality Assessment

Architectural Soundness ⭐⭐⭐⭐
The refactoring creates clean separation:

  • complexity.ts: 553 lines → 327 lines (AST computation logic)
  • complexity-query.ts: New 371-line module (DB queries, pagination)
  • structure.ts: 511 lines → 137 lines (build-time directory metrics)
  • structure-query.ts: New 385-line module (structure queries, hotspots)

Implementation Quality ⭐⭐⭐⭐

  • Proper re-export barrels maintain API compatibility
  • Consistent naming convention (-query.ts suffix)
  • Clean imports/exports with no circular dependencies
  • Follows established patterns from impact.tsdiff-impact.ts/fn-impact.ts split

Dependency Management ⭐⭐⭐⭐⭐

  • Zero new runtime dependencies
  • Clean module boundaries
  • Proper separation of concerns (build vs query time)

Minor Concerns

  1. src/infrastructure/config.ts:40: Added emptyCatch debug logging - acceptable defensive programming
  2. src/infrastructure/native.ts:31: Enhanced libc detection - good platform support improvement
  3. src/presentation/diff-impact-mermaid.ts: New 129-line file for Mermaid diagram generation - clean extraction

Final Recommendation

  • Rating: ⭐⭐⭐⭐☆ (4/5)
  • Action: APPROVE
  • Reasoning: This is a well-executed refactoring that addresses legitimate SLOC warnings through proper architectural separation. The code maintains full backward compatibility, follows established patterns, introduces zero dependencies, and improves maintainability. The minor infrastructure improvements are beneficial. This represents solid engineering practice for managing code complexity in large modules.

Recommendation: Approve and merge. This refactoring will make the codebase more maintainable and easier for AI agents to navigate through clearer module boundaries.


@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 26, 2026

Greptile Summary

This PR is a follow-up cleanup that addresses two previously flagged issues: the domain/analysis/impact.ts domain→presentation layer inversion, and the missing 'files' case in structure-query.ts's getSortFn. A minor biome-ignore annotation is also added to visitor.test.ts.\n\nKey changes:\n- domain/analysis/impact.ts: Removes the now-incorrect diffImpactMermaid re-export, leaving only the domain-layer symbols.\n- domain/queries.ts: Takes over the backward-compat diffImpactMermaid re-export, pointing directly at presentation/diff-impact-mermaid.js.\n- features/structure-query.ts: Adds an explicit case 'files' to getSortFn that sorts by file_count descending, aligning runtime behavior with the documented default.\n- tests/unit/visitor.test.ts: Adds a scoped biome-ignore directive to silence the noExplicitAny lint rule.

Confidence Score: 5/5

Safe to merge — all previously flagged issues have been resolved and no new issues are introduced.

Both prior review concerns (domain→presentation layer inversion and missing 'files' sort case) are cleanly addressed. The changes are small, targeted, and consistent with the surrounding code patterns. No regressions or new logic risks are present.

No files require special attention.

Important Files Changed

Filename Overview
src/domain/analysis/impact.ts Removes the diffImpactMermaid re-export, fixing the previously flagged domain→presentation layer inversion.
src/domain/queries.ts Adds direct diffImpactMermaid re-export from presentation/diff-impact-mermaid.js, preserving backward-compat while fixing the layer boundary violation.
src/features/structure-query.ts Adds explicit 'files' case to getSortFn, correctly sorting by file_count descending and aligning behavior with the default sortBy value.
tests/unit/visitor.test.ts Adds a biome-ignore directive for the any-typed parse variable to suppress the linter warning without changing logic.

Reviews (3): Last reviewed commit: "fix: suppress noExplicitAny lint error i..." | Re-trigger Greptile


return lines.join('\n');
}
export { diffImpactMermaid } from '../../presentation/diff-impact-mermaid.js';
Copy link
Contributor

Choose a reason for hiding this comment

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

P2 Domain barrel re-exports from presentation layer

impact.ts now re-exports diffImpactMermaid from ../../presentation/diff-impact-mermaid.js, making a domain/analysis module depend on a presentation module. Per the architecture in CLAUDE.md, presentation/ is the output-formatting layer and should depend on domain/, not the reverse.

The move of diffImpactMermaid into presentation/ is architecturally correct; the backward-compat re-export here is what creates the inversion. If all callers of diffImpactMermaid already live in the presentation/ layer (or in CLI entry points), this re-export may not be needed and can be removed to keep the boundary clean. If some domain-layer consumers do exist, consider whether they should instead import directly from the new canonical location (presentation/diff-impact-mermaid.ts).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in ca30a5b. Removed the diffImpactMermaid re-export from the domain barrel (domain/analysis/impact.ts). Instead, domain/queries.ts now re-exports it directly from presentation/diff-impact-mermaid.ts, which keeps the dependency direction correct (queries.ts is consumed by presentation/MCP callers, not by domain-layer code).

Comment on lines +338 to +351
// Get files inside this directory
const files = (
db
.prepare(`
SELECT n.name FROM edges e
JOIN nodes n ON e.target_id = n.id
WHERE e.source_id = ? AND e.kind = 'contains' AND n.kind = 'file'
`)
.all(d.id) as { name: string }[]
).map((f) => f.name);

return {
directory: d.name,
cohesion: d.cohesion,
Copy link
Contributor

Choose a reason for hiding this comment

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

P2 Missing 'files' case in getSortFn — default sort is silently alphabetical

structureData defaults sortBy to 'files' when opts.sort is not provided (const sortBy = opts.sort || 'files'), but getSortFn has no 'files' case, so it falls through to a.name.localeCompare(b.name) (alphabetical by directory name).

This was pre-existing behavior in the original structure.ts, so this PR does not regress it — but since the function is being extracted here, it's a good moment to fix the intent or align the default:

Suggested change
// Get files inside this directory
const files = (
db
.prepare(`
SELECT n.name FROM edges e
JOIN nodes n ON e.target_id = n.id
WHERE e.source_id = ? AND e.kind = 'contains' AND n.kind = 'file'
`)
.all(d.id) as { name: string }[]
).map((f) => f.name);
return {
directory: d.name,
cohesion: d.cohesion,
default:
// 'files' and any unknown sort key: fall back to file-count descending
return (a, b) => (b.file_count || 0) - (a.file_count || 0);

Or align the default sortBy value with the actual behavior:

const sortBy = opts.sort || 'name';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a66ab96. Added an explicit \ case to \ that sorts by \ descending, which aligns with the intent of the default \ value. The alphabetical fallback in \ now only applies to truly unknown sort keys.

Remove the diffImpactMermaid re-export from domain/analysis/impact.ts
(which created a domain→presentation dependency inversion). Instead,
re-export it directly from domain/queries.ts → presentation/diff-impact-mermaid.ts.
All callers already consume through domain/queries.ts, so this is
transparent.
The default sortBy value is 'files' but getSortFn had no matching case,
causing it to silently fall through to alphabetical sort. Add explicit
'files' case that sorts by file_count descending.

Impact: 1 functions changed, 4 affected
@carlos-alm
Copy link
Contributor Author

@greptileai

@carlos-alm
Copy link
Contributor Author

@greptileai

1 similar comment
@carlos-alm
Copy link
Contributor Author

@greptileai

@carlos-alm carlos-alm merged commit ad00d59 into main Mar 26, 2026
12 checks passed
@carlos-alm carlos-alm deleted the refactor/titan-sloc-warnings branch March 26, 2026 07:35
@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 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