Skip to content

Extract shared parseJsonObjectFromText and getConnectionFingerprint utilities#2472

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/code-duplication-fix-json-parser
Draft

Extract shared parseJsonObjectFromText and getConnectionFingerprint utilities#2472
Copilot wants to merge 2 commits into
mainfrom
copilot/code-duplication-fix-json-parser

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 15, 2026

Three LLM hooks had identical copy-pasted parseJsonObjectFromText implementations, and two hooks had divergent getConnectionFingerprint — one hashed, one returning raw credentials in React Query keys.

Shared utilities

  • src/utils/parseJsonObjectFromText.ts — Single source for LLM response JSON extraction (fence stripping, brace slicing, JSON.parse). Replaces locals in useRowSummaries, useNamespaceSummaries, usePageSlotInsights.
  • src/utils/connectionFingerprint.ts — Canonical getConnectionFingerprint using DJB2a hash. Replaces locals in useInstrumentationScore and useServiceDashboardQueries.

Behavior change

useServiceDashboardQueries previously put raw apiKey/password values into query keys. Now uses the hashed fingerprint, consistent with useInstrumentationScore. Query keys change shape on upgrade — one-time cache miss, no functional impact.

Tests

  • 9 tests for parseJsonObjectFromText (plain, fenced, nested, garbage input)
  • 6 tests for getConnectionFingerprint (null, stability, differentiation, no raw credential leakage)
Original prompt

This section details on the original issue you should resolve

<issue_title>[code-duplication] Code duplication findings (2026-03-13)</issue_title>
<issue_description>## Summary

  • Files analyzed: 200 TypeScript files (largest subset from 699 matched files)
  • Functions cataloged: 584 (lightweight inventory)
  • Clusters with issues: 2

Findings

1. Triplicated JSON-object parser in LLM hooks

Occurrences:

  • peek/src/hooks/useRowSummaries.ts:65-74
  • peek/src/hooks/useNamespaceSummaries.ts:103-112
  • peek/src/hooks/usePageSlotInsights.ts:29-38

Evidence: All three files define parseJsonObjectFromText(text: string) with the same implementation:

  • trim input
  • strip ```json fences
  • slice from first { to last }
  • JSON.parse(...)

What is wrong: The same parser is copy-pasted across three hooks.

Why it matters: Parser hardening/bug fixes can drift across hooks, causing inconsistent LLM-response handling and repeated maintenance work.

Suggested fix: Extract a shared utility (for example peek/src/utils/parseJsonObjectFromText.ts) and replace all three local implementations.

2. Duplicated getConnectionFingerprint logic with divergent semantics

Occurrences:

  • peek/src/hooks/useInstrumentationScore.ts:38-57 (hashes joined connection fields)
  • peek/src/components/services/useServiceDashboardQueries.ts:37-46 (returns raw joined connection fields)

Evidence:

  • In useInstrumentationScore, getConnectionFingerprint returns hashString([...].join("|")).
  • In useServiceDashboardQueries, getConnectionFingerprint returns raw [...].join("|").
  • Both are then inserted into React Query keys (e.g. useInstrumentationScore.ts:89-95, useServiceDashboardQueries.ts:100-106).

What is wrong: Same conceptual helper name/responsibility is duplicated with different behavior.

Why it matters: This creates drift risk in cache invalidation semantics and puts raw auth-related fields (apiKey/password) into query-key payloads on one path.

Suggested fix: Centralize one canonical fingerprint helper (hashed deterministic output) and reuse it in both hooks.

Suggested Actions

  • Extract shared parseJsonObjectFromText utility and update all LLM hooks to use it.
  • Extract shared getConnectionFingerprint utility and standardize on hashed output.
  • Add focused unit tests for parser edge-cases (fenced vs unfenced JSON) and fingerprint stability.

Analysis Metadata

  • Serena tools used: activate_project, get_symbols_overview
  • Analysis date: 2026-03-13T12:11:57.989Z

What is this? | From workflow: Code Duplication Detector

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.

  • expires on Mar 20, 2026, 12:18 PM UTC

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…tilities

Deduplicate the triplicated JSON parser from useRowSummaries, useNamespaceSummaries,
and usePageSlotInsights into a shared parseJsonObjectFromText utility.

Centralize getConnectionFingerprint with hashed output (DJB2a) to avoid leaking
raw credentials in React Query keys. Previously useServiceDashboardQueries used
raw joined fields while useInstrumentationScore used hashed output.

Add focused unit tests for both utilities.

Co-authored-by: strawgate <[email protected]>
Copilot AI changed the title [WIP] [code-duplication] Remove duplicate JSON parser from hooks Extract shared parseJsonObjectFromText and getConnectionFingerprint utilities Mar 15, 2026
Copilot AI requested a review from strawgate March 15, 2026 20:03
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.

[code-duplication] Code duplication findings (2026-03-13)

2 participants