worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

[ci] duplication checks

Open max-sixty opened this issue 9 months ago • 3 comments

@worktrunk-bot can you review this issue: https://github.com/max-sixty/worktrunk/pull/1256#discussion_r2886442960

why didn't the reviewer pick this up? this is the sort of thing that's really important to pick up — easy to miss, requires just reading code, looking for similar examples, tracking down the central function. Look through the logs of the reviewer, read the skills, carefully, propose some changes

max-sixty avatar Mar 05 '26 01:03 max-sixty

Analysis

The core duplication the reviewer missed: detect_pr_provider (added in src/commands/worktree/switch.rs) duplicates get_platform_for_repo in src/commands/list/ci_status/platform.rs.

Both functions do the same thing:

  1. Check a preferred remote first (primary remote vs remote hint)
  2. Fall back to scanning all remotes
  3. Parse each URL with GitRemoteUrl::parse()
  4. Check is_github() / is_azure_devops() / is_gitlab()

The only difference is the return type — one returns Box<dyn RemoteRefProvider>, the other returns CiPlatform. But the platform detection logic is identical and should be shared, with the caller mapping the result to its own type.

There are secondary duplications too: detect_azure_org in the new src/git/remote_ref/azure.rs manually shells out to git remote -v and parses stdout, instead of using repo.all_remote_urls() which already does exactly that. And get_azure_org_url / get_azure_project in src/commands/list/ci_status/azure.rs each independently iterate all remotes looking for Azure info — they could share a single pass.

Why the reviewer missed it

The pr-review skill already has the right instructions — two separate sections should have caught this:

  1. "Same pattern elsewhere" (line ~138): "When a PR fixes a bug or changes a pattern, search for the same pattern in other files." — The reviewer should have searched for existing remote-URL-to-platform detection when seeing a new one being added.

  2. "When confidence is low" (line ~237): "Does this bypass or duplicate an existing API?" — This is an LLM-generated PR from a first-time contributor to this module, which should have triggered the low-confidence path.

The reviewer noted the detect_pr_provider function positively ("a nice improvement over hardcoding GitHub") without checking whether that logic already existed elsewhere. It focused on surface-level correctness (clippy lints, comment wording, URL construction) rather than architectural fit.

Proposed skill changes

The skill already says the right things, but the instructions are passive — they describe what to check without making it a concrete step. Two changes could help:

1. Add a mandatory "duplication search" step to the review workflow

In section 3. Review, after the existing checklist items, add an explicit search step:

Duplication check (mandatory for new functions/types):

For every new public or module-level function added in the diff, search for existing functions with similar signatures or names across the codebase. LLM-generated code frequently reinvents existing APIs.

# For each new function, search for similar patterns
rg "fn detect.*platform\|fn get.*platform\|fn .*provider" --type rust
rg "all_remote_urls\|remote_url\|GitRemoteUrl::parse" --type rust

If an existing function does substantially the same thing, flag it — reuse is almost always better than a parallel implementation.

2. Strengthen the low-confidence trigger for LLM-authored PRs

The skill mentions "LLM-generated code (may duplicate existing APIs)" as a confidence-lowering factor, but it's buried in a list. Make it more explicit:

LLM-generated PRs and first-time contributors: These PRs have a high rate of duplicating existing internal APIs because the author lacks codebase context. Always run the duplication check above, and read the existing modules that the new code touches (not just the diff) before approving.

The fundamental issue is that catching duplication requires reading code that isn't in the diff — specifically, the existing platform detection module. The reviewer read the changed files but didn't search for whether the new pattern already existed elsewhere in the codebase. Making that search an explicit, mandatory step (rather than a judgment-dependent "when confidence is low" suggestion) would catch this class of issue more reliably.

worktrunk-bot avatar Mar 05 '26 01:03 worktrunk-bot

yes, OK, let's indeed make the first change.

As well as searching for similar functions, search for code with overlapping subgoals (like resolving origin) and look at the functions that it consumes, to see whether there are shared functions we could use

max-sixty avatar Mar 05 '26 02:03 max-sixty

Done — #1272 adds the duplication check to the pr-review skill.

The new step requires two searches for every new function/type in a diff:

  1. Similar names and signatures — direct search for functions that do the same thing
  2. Overlapping subgoals — identify the intermediate steps the new code performs (iterating remotes, parsing URLs, etc.), search for existing code doing those same sub-tasks, then read the functions that code consumes to find shared helpers one layer down

Also strengthened the low-confidence section to explicitly flag LLM-authored PRs and first-time contributors as requiring the duplication check, rather than leaving it as a judgment call.

worktrunk-bot avatar Mar 05 '26 02:03 worktrunk-bot