[GH-ISSUE #172] Technical debt: reduce duplicate code blocks reported by jscpd #118

Closed
opened 2026-10-15 18:21:18 +00:00 by bsakaguchi · 10 comments
Owner

Originally created by @morluto on GitHub (Jul 14, 2026).
Original GitHub issue: https://github.com/morluto/rea/issues/172

Symptom

npm run jscpd reports 39 duplicate code blocks (~1.0% duplicated lines, 1.4% duplicated tokens) across core modules in this repo.

Examples include repeated logic in:

  • src/native/CommandRunner.ts:43-52 and src/native/CommandRunner.ts:62-71 (duplicate guard/loop pattern)
  • src/server/registerInvestigationTools.ts:212-225 and src/server/registerInvestigationTools.ts:275-288 (duplicate registration setup blocks)
  • src/application/Setup.ts:281-295 and src/application/Setup.ts:360-374 (duplicate fallback branches)

Impact

The current duplication raises maintenance cost and increases the chance that behavior diverges in future edits while fixing only one copy. It also obscures intent and increases merge noise in unrelated refactors.

Suggested fix

Extract repeated logic into shared helper(s) where it encodes the same behavior, or parameterize the duplicated registration/setup blocks before continuing with the existing large-rewrite technical-debt cleanups.

Originally created by @morluto on GitHub (Jul 14, 2026). Original GitHub issue: https://github.com/morluto/rea/issues/172 ## Symptom `npm run jscpd` reports 39 duplicate code blocks (~1.0% duplicated lines, 1.4% duplicated tokens) across core modules in this repo. Examples include repeated logic in: - `src/native/CommandRunner.ts:43-52` and `src/native/CommandRunner.ts:62-71` (duplicate guard/loop pattern) - `src/server/registerInvestigationTools.ts:212-225` and `src/server/registerInvestigationTools.ts:275-288` (duplicate registration setup blocks) - `src/application/Setup.ts:281-295` and `src/application/Setup.ts:360-374` (duplicate fallback branches) ## Impact The current duplication raises maintenance cost and increases the chance that behavior diverges in future edits while fixing only one copy. It also obscures intent and increases merge noise in unrelated refactors. ## Suggested fix Extract repeated logic into shared helper(s) where it encodes the same behavior, or parameterize the duplicated registration/setup blocks before continuing with the existing large-rewrite technical-debt cleanups.
bsakaguchi 2026-10-15 18:21:18 +00:00
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Refreshed against current main (2026-07-17).

npm run jscpd now reports:

  • 157 TypeScript clones;
  • 1,658 duplicated lines / 87,960 analyzed lines (1.88%);
  • 11,550 duplicated tokens / 493,388 analyzed tokens (2.34%).

The original 39-clone / ~1% measurement is stale. The cited CommandRunner and investigation-registration patterns still appear, but the report now includes many intentional parallel schemas and provider adapters. Raw clone count is therefore not a safe refactor target.

High-leverage candidates are repeated policy or lifecycle behavior: project/workspace atomic-store operations, browser/Electron registration and observation gates, provider socket/client lifecycle, setup transaction branches, and managed metadata readers. Repeated Zod shapes, evidence examples, test fixtures, and provider-specific DTO parsing should remain separate unless one owner can enforce a shared invariant without coupling protocols. Recommended acceptance evidence for child work: behavior-preserving public-seam tests plus a before/after jscpd excerpt for the selected cluster, not a repository-wide zero-clone goal.

<!-- gh-comment-id:5001369057 --> @morluto commented on GitHub (Jul 17, 2026): Refreshed against current main (2026-07-17). `npm run jscpd` now reports: - 157 TypeScript clones; - 1,658 duplicated lines / 87,960 analyzed lines (1.88%); - 11,550 duplicated tokens / 493,388 analyzed tokens (2.34%). The original 39-clone / ~1% measurement is stale. The cited `CommandRunner` and investigation-registration patterns still appear, but the report now includes many intentional parallel schemas and provider adapters. Raw clone count is therefore not a safe refactor target. High-leverage candidates are repeated policy or lifecycle behavior: project/workspace atomic-store operations, browser/Electron registration and observation gates, provider socket/client lifecycle, setup transaction branches, and managed metadata readers. Repeated Zod shapes, evidence examples, test fixtures, and provider-specific DTO parsing should remain separate unless one owner can enforce a shared invariant without coupling protocols. Recommended acceptance evidence for child work: behavior-preserving public-seam tests plus a before/after jscpd excerpt for the selected cluster, not a repository-wide zero-clone goal.
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Incremental cleanup on the current working tree: src/native/CommandRunner.ts now defines one NativeCommandOptions interface shared by NativeCommandRunner and XcrunCommandRunner, removing the exact duplicate option contract without behavior changes. Typecheck, formatting, native-provider tests (9/9), and a focused jscpd scan passed; the focused file now reports zero clones. This inventory remains open because the full baseline still contains 157 TypeScript clone groups (1,658 duplicated lines, 1.88%).

<!-- gh-comment-id:5001695573 --> @morluto commented on GitHub (Jul 17, 2026): Incremental cleanup on the current working tree: src/native/CommandRunner.ts now defines one NativeCommandOptions interface shared by NativeCommandRunner and XcrunCommandRunner, removing the exact duplicate option contract without behavior changes. Typecheck, formatting, native-provider tests (9/9), and a focused jscpd scan passed; the focused file now reports zero clones. This inventory remains open because the full baseline still contains 157 TypeScript clone groups (1,658 duplicated lines, 1.88%).
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Additional validation for the incremental native-command cleanup: NativeCommandOptions remains the sole command option contract in CommandRunner, with typecheck, formatting, native-provider tests, and focused zero-clone evidence passing. The repository-wide duplication inventory remains open for further behavior-sharing slices.

<!-- gh-comment-id:5001997213 --> @morluto commented on GitHub (Jul 17, 2026): Additional validation for the incremental native-command cleanup: NativeCommandOptions remains the sole command option contract in CommandRunner, with typecheck, formatting, native-provider tests, and focused zero-clone evidence passing. The repository-wide duplication inventory remains open for further behavior-sharing slices.
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Updated full measurement after the NativeCommandOptions cleanup: npm run jscpd now reports 156 TypeScript clone groups, 1,649 duplicated lines out of 88,064 (1.87%), and 11,499 duplicated tokens out of 493,923 (2.33%). The prior refreshed baseline was 157 groups, 1,658 lines (1.88%), and 11,550 tokens (2.34%), so this slice removes one group and nine duplicated lines without shifting duplication elsewhere.

<!-- gh-comment-id:5002081179 --> @morluto commented on GitHub (Jul 17, 2026): Updated full measurement after the NativeCommandOptions cleanup: npm run jscpd now reports 156 TypeScript clone groups, 1,649 duplicated lines out of 88,064 (1.87%), and 11,499 duplicated tokens out of 493,923 (2.33%). The prior refreshed baseline was 157 groups, 1,658 lines (1.88%), and 11,550 tokens (2.34%), so this slice removes one group and nine duplicated lines without shifting duplication elsewhere.
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Refreshed measurements after the shared process-capture permission builder and current worktree cleanup: jscpd now reports 155 TypeScript clone groups, 1,633 duplicated lines out of 88,076 (1.85%), and 11,412 duplicated tokens out of 493,958 (2.31%). The prior measurement was 156 groups, 1,649 lines (1.87%), and 11,499 tokens (2.33%). Full npm run check passes. #172 remains a legitimate inventory; the next high-confidence clusters are typed error projection and shared browser/Electron registration gates, not a blanket clone-count target.

<!-- gh-comment-id:5002484003 --> @morluto commented on GitHub (Jul 17, 2026): Refreshed measurements after the shared process-capture permission builder and current worktree cleanup: jscpd now reports 155 TypeScript clone groups, 1,633 duplicated lines out of 88,076 (1.85%), and 11,412 duplicated tokens out of 493,958 (2.31%). The prior measurement was 156 groups, 1,649 lines (1.87%), and 11,499 tokens (2.33%). Full npm run check passes. #172 remains a legitimate inventory; the next high-confidence clusters are typed error projection and shared browser/Electron registration gates, not a blanket clone-count target.
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Refreshed against the current worktree on 2026-07-17.

Current npm run jscpd result: 155 clones, 1.85% duplicated lines, 1,633 duplicated lines, 11,412 duplicated tokens. This is slightly below the prior refreshed baseline (157 / 1,658 / 11,550), and the old CommandRunner.ts duplicate is gone, but the inventory remains active.

Highest-frequency files include ManagedMemberInspector.ts (16 clones), managedArtifact.ts (15), electronObservation.ts and managedMemberComparison.ts (9 each), and browserObservation.ts / CdpBrowserProvider.ts (7 each).

Recommended safe targets are repeated MCP evidence-result/registration blocks and duplicated investigation workflow assembly. Parallel browser/Electron and managed table schemas should remain separate unless a shared owner can enforce the same invariant. The issue should stay open.

<!-- gh-comment-id:5003094733 --> @morluto commented on GitHub (Jul 17, 2026): Refreshed against the current worktree on 2026-07-17. Current `npm run jscpd` result: **155 clones, 1.85% duplicated lines, 1,633 duplicated lines, 11,412 duplicated tokens**. This is slightly below the prior refreshed baseline (157 / 1,658 / 11,550), and the old `CommandRunner.ts` duplicate is gone, but the inventory remains active. Highest-frequency files include `ManagedMemberInspector.ts` (16 clones), `managedArtifact.ts` (15), `electronObservation.ts` and `managedMemberComparison.ts` (9 each), and `browserObservation.ts` / `CdpBrowserProvider.ts` (7 each). Recommended safe targets are repeated MCP evidence-result/registration blocks and duplicated investigation workflow assembly. Parallel browser/Electron and managed table schemas should remain separate unless a shared owner can enforce the same invariant. The issue should stay open.
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Refreshed after the current error-projection refactor and the other active issue slices: npm run jscpd reports 155 TypeScript clone groups, 1,633 duplicated lines out of 88,852 (1.84%), and 11,412 duplicated tokens out of 497,727 (2.29%).

The absolute clone inventory is unchanged from the prior measurement (155 / 1,633 / 11,412); the percentage decrease comes from growth elsewhere in the current working tree, not clone removal. src/domain/errors.ts is not present in any reported clone pair, so the helper extraction did not introduce new duplication. #172 should remain open and should continue to be addressed by invariant-owning shared helpers rather than percentage-only refactors.

<!-- gh-comment-id:5003656778 --> @morluto commented on GitHub (Jul 17, 2026): Refreshed after the current error-projection refactor and the other active issue slices: `npm run jscpd` reports **155 TypeScript clone groups, 1,633 duplicated lines out of 88,852 (1.84%), and 11,412 duplicated tokens out of 497,727 (2.29%)**. The absolute clone inventory is unchanged from the prior measurement (155 / 1,633 / 11,412); the percentage decrease comes from growth elsewhere in the current working tree, not clone removal. `src/domain/errors.ts` is not present in any reported clone pair, so the helper extraction did not introduce new duplication. #172 should remain open and should continue to be addressed by invariant-owning shared helpers rather than percentage-only refactors.
Author
Owner

@morluto commented on GitHub (Jul 17, 2026):

Refreshed after the Windows-native contract work and Doctor refactor: npm run jscpd now reports 158 TypeScript clone groups, 1,666 duplicated lines out of 90,271 (1.85%), and 11,614 duplicated tokens out of 505,059 (2.30%).

This is a real increase from the prior working-tree measurement (155 / 1,633 / 11,412), so #172 remains open. The new DoctorDiagnostics split is not present in the reported clone pairs; the increase is concentrated in other concurrently added surfaces, including WindowsNativeAuthority and existing cross-adapter/domain patterns.

The next safe consolidation candidates are repeated same-module policy projections where one helper can own an invariant, such as WindowsNativeAuthority resource/report validation or PermissionAuthority path/grant branches. Cross-layer schema similarities should not be merged merely to improve the percentage, because they often represent distinct parsing authorities.

<!-- gh-comment-id:5008518421 --> @morluto commented on GitHub (Jul 17, 2026): Refreshed after the Windows-native contract work and Doctor refactor: npm run jscpd now reports 158 TypeScript clone groups, 1,666 duplicated lines out of 90,271 (1.85%), and 11,614 duplicated tokens out of 505,059 (2.30%). This is a real increase from the prior working-tree measurement (155 / 1,633 / 11,412), so #172 remains open. The new DoctorDiagnostics split is not present in the reported clone pairs; the increase is concentrated in other concurrently added surfaces, including WindowsNativeAuthority and existing cross-adapter/domain patterns. The next safe consolidation candidates are repeated same-module policy projections where one helper can own an invariant, such as WindowsNativeAuthority resource/report validation or PermissionAuthority path/grant branches. Cross-layer schema similarities should not be merged merely to improve the percentage, because they often represent distinct parsing authorities.
Author
Owner

@morluto commented on GitHub (Jul 20, 2026):

Split into focused child issues:

These replace the broad remaining scope in this issue. Closing this parent can happen once all children are closed.

<!-- gh-comment-id:5023447979 --> @morluto commented on GitHub (Jul 20, 2026): Split into focused child issues: - [ ] #395 - [ ] #396 These replace the broad remaining scope in this issue. Closing this parent can happen once all children are closed.
Author
Owner

@morluto commented on GitHub (Jul 20, 2026):

Closing as no longer actionable. npm run lint reports 0 warnings/errors and npm run jscpd shows only 1.65% duplicated lines, so these metric-driven refactors would force harmful cross-layer abstractions without improving real correctness.

<!-- gh-comment-id:5023566444 --> @morluto commented on GitHub (Jul 20, 2026): Closing as no longer actionable. `npm run lint` reports 0 warnings/errors and `npm run jscpd` shows only 1.65% duplicated lines, so these metric-driven refactors would force harmful cross-layer abstractions without improving real correctness.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
morluto/rea#118
No description provided.