Skip to content

ci: Detect duplicate code with jscpd - #3133

Merged
kodiakhq[bot] merged 2 commits into
mainfrom
drew/jscpd-duplicate-detection
Sep 28, 2026
Merged

kodiakhq[bot] merged 2 commits into
mainfrom
drew/jscpd-duplicate-detection

Conversation

@pulpdrew

@pulpdrew pulpdrew commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds automated duplicate code detection using jscpd.

Detection can be run locally (yarn dupes, yarn dupes:all, or make ci-lint) and runs automatically in CI. By default, detection finds duplications that are new on the branch as compared to origin/main. New duplicates are non-blocking for now, and reported on the PR:

Screenshot 2026-09-15 at 10 05 26 AM Screenshot 2026-09-15 at 10 11 19 AM

Tests and non-typescript code are ignored, as they're expected to have a higher degree of duplication.

Screenshots or video

Screenshot 2026-09-15 at 10 08 05 AM Screenshot 2026-09-15 at 10 07 53 AM

How to test on Vercel preview

Run yarn dupes, yarn dupes:all, or make ci-lint locally. No app behavior changes expected.

References

  • Linear Issue:
  • Related PRs:

@vercel

vercel Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
hyperdx-oss Ready Ready Preview Sep 28, 2026 1:17pm UTC
hyperdx-storybook Ready Ready Preview Sep 28, 2026 1:17pm UTC

Request Review

@changeset-bot

changeset-bot Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: a075c33

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Duplicate code analysis

No new duplicate code introduced by this PR.


jscpd compared this branch against origin/main
across app, api and common-utils. Only new clones are listed; the codebase
currently holds 129 in total (1.84%).
Run yarn dupes locally to reproduce, or yarn dupes:all for the full picture.

@greptile-apps

greptile-apps Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Adds a new CI check that runs on every pull request.

The PR appears safe to merge based on the reviewed changes.

Summary

This PR adds advisory duplicate-code detection for app, API, and common-utils TypeScript, with local commands and a PR report.

  • The workflow compares the branch against its base and does not block on detected duplicates.
  • No new actionable issue was identified in the changes since the previous review.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  PR[Pull request] --> CI[jscpd workflow]
  CI --> Scan[Scan against base ref]
  Scan --> Report[JSON report]
  Report --> Comment[Advisory PR comment]
Loading

Reviews (6) · Last reviewed commit: "Merge branch 'main' into drew/jscpd-dupl..."

Comment thread .github/workflows/jscpd.yml Resolved
@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 382 passed • 1 skipped • 1438s

Status Count
✅ Passed 382
❌ Failed 0
⚠️ Flaky 2
⏭️ Skipped 1

Tests ran across 4 shards in parallel.

View full report →

@pulpdrew
pulpdrew force-pushed the drew/jscpd-duplicate-detection branch from 5724c7a to d1be584 Compare September 15, 2026 14:11
Comment thread packages/common-utils/src/core/utils.ts Resolved
@pulpdrew
pulpdrew marked this pull request as ready for review September 15, 2026 14:12
@github-actions github-actions Bot added the review/tier-2 Low risk — AI review + quick human skim label Sep 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔵 Tier 2 — Low Risk

Small, isolated change with no API route or data model modifications.

Why this tier:

  • Standard feature/fix — introduces new logic or modifies core functionality

Review process: AI review + quick human skim (target: 5–15 min). Reviewer validates AI assessment and checks for domain-specific concerns.
SLA: Resolve within 4 business hours.

Stats
  • Production files changed: 5
  • Production lines changed: 163
  • Branch: drew/jscpd-duplicate-detection
  • Author: pulpdrew

To override this classification, remove the review/tier-2 label and apply a different review/tier-* label. Manual overrides are preserved on subsequent pushes.

@pulpdrew
pulpdrew force-pushed the drew/jscpd-duplicate-detection branch from d1be584 to 89bea78 Compare September 15, 2026 14:18
@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

✅ No critical issues found. No P0/P1: this is advisory CI tooling with no runtime/application code, the security surface is clean (pull_request trigger, base_ref passed via env, actions SHA-pinned, fork comment guard), and both prior review threads (action SHA-pinning; the out-of-scope replaceBacktickedIdentifiers2 helper) are resolved. Two P2 recommendations below.

🟡 P2 -- recommended

  • scripts/ci/dupes.mjs:74 -- The jscpd report is read with an unguarded JSON.parse, so a truncated or corrupt report throws an uncaught exception that exits non-zero and leaves the temp dir behind, breaking the file's own documented "always exits 0 / every exit path clears the temp dir" contract.
    • Fix: Wrap the report read and parse in a try/catch that routes failures through the existing bail() helper, mirroring the config-parse guard directly above it.
    • reliability, correctness, adversarial
  • scripts/ci/dupes.mjs:79 -- The new wrapper's branching logic (isNew filtering, --base validation, markdown rendering, the MAX_LISTED_IN_COMMENT cap, pluralization) ships with no tests and no export seam, diverging from sibling scripts/ci/ratchet.mjs, which exports pure units behind an import.meta.url main guard and has __tests__/ratchet.test.mjs.
    • Fix: Extract the render/filter functions behind a main guard and add scripts/ci/__tests__/dupes.test.mjs covering empty vs populated output, singular/plural wording, the cap overflow line, and the --base validation branch.
    • testing, project-standards, maintainability
🔵 P3 nitpicks (3)
  • scripts/ci/dupes.mjs:90 -- Passing --all together with --markdown still labels every clone as "new ... introduced by this PR" and claims a comparison against the base even though --baseline-from-ref was never run; no wired script uses this combination today.
    • Fix: Branch the header and footer wording on the all flag, or reject the --all --markdown combination.
  • .github/workflows/jscpd.yml:68 -- Because the comment uses a fixed message-id and the comment step is skipped when the report body is empty, a later run that fails to produce a report leaves the previous run's duplicate list in place, misrepresenting the current branch state.
    • Fix: On the empty-report path, update the sticky comment to a neutral "report unavailable" body instead of skipping the step.
  • scripts/ci/dupes.mjs:14 -- The human-readable scope string (app, api and common-utils) is hardcoded in three prose locations, decoupled from the PACKAGES constant, so they will silently drift apart when a package is added or removed.
    • Fix: Derive the display list from PACKAGES rather than repeating it in prose.

Reviewers (8): correctness, security, adversarial, reliability, maintainability, project-standards, testing, previous-comments.

Testing gaps:

  • No unit tests for dupes.mjs argument parsing, isNew filtering, or markdown rendering; blast radius is limited to PR-comment quality since the tool is advisory and always exits 0.
  • No verification that jscpd 5.1.2 actually emits the isNew flag the entire new-clone filter depends on; if a future version drops or renames it, the check would silently report "no new duplicates" on every PR with no error.
  • PACKAGES scans only app/api/common-utils, silently omitting the TypeScript packages cli and hdx-eval; document the exclusion or discover workspaces dynamically.
  • A hung jscpd would hit the job-level timeout-minutes: 10, which cancels the job and surfaces as a failed check — not covered by the step's continue-on-error, a narrow edge to the "never blocks a PR" intent.

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@pulpdrew
pulpdrew force-pushed the drew/jscpd-duplicate-detection branch from 89bea78 to f31239c Compare September 15, 2026 14:23
Comment thread scripts/ci/dupes.mjs Resolved
Comment thread .jscpd.json Resolved
Comment thread scripts/ci/dupes.mjs Resolved
Comment thread scripts/ci/dupes.mjs Resolved
Comment thread package.json Resolved
@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 critical or 🟠 major in code this PR adds or changes: fix before asking for review.
  • 🔵 minor in code this PR changes: your call. Fix if small, otherwise reply.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    severity. Reply with one sentence; if it is critical, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

3 finding(s): 🔴 0 critical · 🟠 0 major · 🔵 3 minor

1 posted as inline comment(s) on the changed lines. 2 listed below.

Findings outside the changed lines

2 minor
  • 🔵 scripts/ci/dupes.mjs:231 — A malformed JSON report crashes the script, which is meant to always exit 0 → The header comment says the script always exits 0, but JSON.parse(readFileSync(report)) is not guarded. A truncated or invalid jscpd-report.json throws, the script exits with code 1, and the temp dir under out is never removed. Wrap the parse in try/catch and route failures through bail(...), as the config parse above it does.
  • 🔵 scripts/ci/dupes.mjs:236 — New report script has no tests, unlike its sibling ratchet.mjs → scripts/ci/ratchet.mjs has scripts/ci/__tests__/ratchet.test.mjs, which main.yml runs. dupes.mjs has its own untested logic: filtering on isNew, rewriting paths with rel(), and capping the list at MAX_LISTED_IN_COMMENT. Split the rendering out into a pure function and add a node --test case that feeds it a sample jscpd JSON report.

Severity is the reviewer's own estimate and is used for ordering, not filtering. No finding blocks merge automatically; a maintainer decides.

@teeohhem

Copy link
Copy Markdown
Contributor

@pulpdrew let me know how this pairs with: #3061 and if we should make any adjustments. Thanks!

@pulpdrew

Copy link
Copy Markdown
Contributor Author

@pulpdrew let me know how this pairs with: #3061 and if we should make any adjustments. Thanks!

My thought: this check runs automatically and serves as a signal to authors and reviewers, regardless of whether an agent has followed the grep instructions. It also costs no tokens to run, and can help us identify existing duplicates in addition to preventing new ones.

We could potentially tell the agent that this is available to help them find duplicates, I'm not sure whether agents would use it any more effectively than grep or not. Since it runs during lint, agents that run lint should be seeing its output anyway.

@pulpdrew pulpdrew closed this Sep 18, 2026
@pulpdrew pulpdrew reopened this Sep 28, 2026
steps.report.outputs.result != '' &&
github.event.pull_request.head.repo.fork != true
uses: mshick/add-pr-comment@ec328af66588ab8f77cdeb2c264f14aba45bbf59 # v3.12.0
with:

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.

🔵 minor — Clean PRs still get a bot comment

Every non-fork PR gets a jscpd comment, even when it adds no duplicates, because dupes.mjs --markdown always prints a body ('No new duplicate code introduced by this PR.'). .github/workflows/knip.yml:98-119 handles the same case by skipping the comment and deleting any stale one. Do the same here: emit empty output when there are no new clones (or add a delete-on-status/cleanup step) so the comment only appears when there is something to act on.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

@kodiakhq
kodiakhq Bot merged commit ff6d731 into main Sep 28, 2026
30 checks passed
@kodiakhq
kodiakhq Bot deleted the drew/jscpd-duplicate-detection branch September 28, 2026 13:39

This branch was successfully deployed

2 active deployments
Preview – hyperdx-oss — a075c336 Deployed Sep 28, 2026 by vercel[bot]
Preview – hyperdx-storybook — a075c336 Deployed Sep 28, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge review/tier-2 Low risk — AI review + quick human skim

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants