Skip to content

feat(implementer): add cross-file duplication detection for parallel agent workflows #1187

@rjmurillo

Description

@rjmurillo

Problem

The implementer agent prompt contains anti-duplication guidance (Code Simplification rule #1: "If 3+ lines appear twice, extract or loop. Check within the file and across files touched in this PR."), but this rule is structurally unenforceable when multiple implementer agents run in parallel.

Evidence: PR rjmurillo/moq.analyzers#898

Five parallel agents were spawned to write tests for untested Common extension methods. Each agent independently created identical boilerplate in its test file:

  • CorlibReference (MetadataReference field)
  • SystemRuntimeReference (MetadataReference field)
  • CoreReferences (MetadataReference array)
  • CreateCompilation() (full method, ~15 lines)
  • CreateMoqCompilationAsync() (full method, ~20 lines)
  • GetMoqReferencesAsync() (full method, ~10 lines)

Result: 489 lines of identical code across 12 files. A reviewer (Gemini) caught it, but the initial response dismissed the feedback. After investigation, a shared CompilationHelper class was extracted, reducing net lines by 287.

Root Cause Analysis

Three structural gaps allow cross-file duplication to survive:

Gap 1: Parallel agent isolation

When multiple implementer agents run concurrently via Task tool, each agent only sees its own files. The "check across files touched in this PR" guidance is impossible to follow because Agent A has no visibility into what Agent B is writing.

The prompt assumes a single-agent workflow but the orchestrator regularly spawns parallel agents for throughput.

Gap 2: No post-consolidation step in the orchestration workflow

The orchestrator's workflow is: implementer → critic → qa → security. After parallel implementer agents complete, there is no consolidation step that reviews all outputs together for cross-cutting concerns like duplication.

The critic agent (line 59: "No code review or completed work assessment") explicitly excludes itself from reviewing implementation output. The QA agent verifies tests pass but doesn't check for code redundancy.

Gap 3: PR review agents lack cross-file duplication detection

The review-pr skill uses six agents: code-reviewer, pr-test-analyzer, silent-failure-hunter, type-design-analyzer, comment-analyzer, code-simplifier. None have explicit instructions to detect duplication across files in a PR diff.

The code-simplifier focuses on simplifying individual files. The code-reviewer checks project guidelines but doesn't scan for identical blocks across changed files.

Proposed Solutions

Option A: Add a consolidation phase to the orchestrator (Recommended)

After parallel implementer agents complete, the orchestrator should:

  1. Collect all files created/modified by parallel agents
  2. Spawn a single implementer (or a new consolidator agent) with ALL those files as context
  3. The agent scans for identical blocks (3+ lines appearing in 2+ files) and extracts shared helpers
  4. Run build + tests to verify the extraction

Add to orchestrator workflow:

parallel implementers → consolidation → critic → qa

Option B: Add cross-file duplication check to code-reviewer agent

Add an explicit step to the code-reviewer prompt:

### Cross-File Duplication Detection (REQUIRED for multi-file PRs)

For PRs touching 3+ files:
1. Identify all new methods, fields, and type definitions across changed files
2. Flag any method body or field definition that appears identically in 2+ files
3. Recommend extraction to a shared helper class
4. Severity: CRITICAL (blocks merge)

Option C: Add duplication awareness to the implementer prompt

When the implementer is spawned for a task involving parallel work, the caller should provide context about other parallel agents' scopes. Add to the implementer prompt:

### Parallel Work Awareness

When working alongside other agents on the same PR:
1. Before defining helper methods, check if a shared helper already exists (Glob for *Helper*.cs, *Utilities*.cs)
2. If you need compilation setup, test fixtures, or reference assembly configuration, check tests/*/Helpers/ first
3. Prefer importing existing helpers over defining new ones

Option D: Combination approach

  • Option A for orchestrator-level enforcement
  • Option C for agent-level awareness
  • Option B as a safety net in PR review

Impact

Without this fix, every parallel agent workflow risks introducing O(N) duplication where N is the number of parallel agents. This violates DRY at scale and creates maintenance burden.

Related

Metadata

Metadata

Assignees

Labels

RefiningNeeds human inputagent-criticPlan validation agentagent-implementerCode implementation agentagent-orchestratorTask coordination agentagent-qaTesting and verification agentagent-securitySecurity assessment agentarea-infrastructureBuild, CI/CD, configurationarea-installationInstallation scriptsarea-promptsAgent prompts and templatesarea-skillsSkills documentation and patternsarea-workflowsGitHub Actions workflowsenhancementNew feature or requestpriority:P1Important: Affects user experience significantly, high business value

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions