Skip to content

[duplicate-code] Length-prefixed identity-encoding algorithm duplicated between JUnit and CTRF report mergers #10589

Closed as not planned

Description

@github-actions

Analysis of commit a760075 ("Consolidate reports across retry attempts", #10542)

Assignee: @copilot

Summary

The retry-attempt consolidation feature introduced the same "length-prefixed component" identity-encoding trick independently in two different report mergers: JUnitReportMerger and CtrfReportMerger. Both need a way to build a deterministic, collision-free identity key out of several nullable string components (suite name, test name, file path, etc.), and both solve it with the identical, non-obvious encoding scheme: for each component, append length ?? -1, then :, then the component's text, so that ambiguous concatenations (e.g. "A" + "BC" vs "AB" + "C") can never collide.

Duplication Details

Pattern: Length-prefixed identity component encoding

  • Severity: Medium (subtle, correctness-critical logic — a divergence between the two copies could silently reintroduce identity collisions in one merger but not the other)

  • Occurrences: 2 independent implementations of the same algorithm

  • Locations:

    • src/Platform/Microsoft.Testing.Extensions.JUnitReport/JUnitReportMerger.cs, BuildIdentity(params string?[] components) (~lines 353-362) and its callers BuildSuiteIdentity/BuildTestIdentity (lines 339-352)
    • src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfReportMerger.RetryCollapsing.cs, AppendIdentityComponent(StringBuilder identity, string? component) (~line 157) and its use inside GetTestIdentity (lines 136-153)
  • Code Sample (JUnitReportMerger.cs):

    private static string BuildIdentity(params string?[] components)
    {
        var identity = new StringBuilder();
        foreach (string? component in components)
        {
            identity.Append(component?.Length ?? -1)
                .Append(':')
                .Append(component);
        }
    
        return identity.ToString();
    }
  • Code Sample (CtrfReportMerger.RetryCollapsing.cs):

    private static void AppendIdentityComponent(StringBuilder identity, string? component)
        => identity.Append('\u001f').Append(component?.Length ?? -1).Append(':').Append(component);

Both encode "unambiguous concatenation of variable-length nullable strings" with the exact same length-prefix technique (the CTRF version additionally prefixes a \u001f separator, which is a trivial variation). The underlying algorithm — and the reasoning documented in the CTRF XML doc comment about why plain separation can collide — is identical to what JUnitReportMerger.BuildIdentity already needed to solve for suite/test identity a few files away in the same PR.

Impact Analysis

  • Maintainability: Any future fix to the encoding scheme (e.g., switching separators, handling additional edge cases such as components containing the sentinel character) must be applied in two places; it is easy to update one and forget the other.
  • Bug Risk: Because this is a security/correctness-sensitive dedup key (used to decide whether two test rows represent the same logical test across retry attempts), a fix applied to only one copy would silently reintroduce collisions in the other merger, and there is no shared test suite that would catch the divergence.
  • Code Bloat: Small in absolute size, but the duplicated reasoning (documented at length in CTRF's XML doc comments) is real design/domain knowledge that currently lives in only one of the two copies, making the JUnit version's identical technique look uncommented and coincidental.

Refactoring Recommendations

  1. Extract a shared identity-encoding helper

    • Extract common functionality to: src/Platform/Microsoft.Testing.Platform.Extensions/ArtifactPostProcessing/IdentityKeyBuilder.cs (or a similarly-scoped internal shared utility referenced by both Microsoft.Testing.Extensions.JUnitReport and Microsoft.Testing.Extensions.CtrfReport projects, e.g. via a shared internal project or linked file)
    • Estimated effort: ~1-2 hours (small, low-risk change; both call sites already isolate the encoding behind a private static helper, so the refactor is mostly moving the method and updating call sites/tests)
    • Benefits: Single source of truth for the collision-avoidance reasoning; a future fix (e.g., handling the sentinel character appearing inside a component) needs to happen once; makes the shared design intent explicit instead of duplicated tribal knowledge.
  2. At minimum, cross-link the two implementations with comments

    • If extracting a shared type is not desired (e.g., to keep the two extension packages fully independent), add a comment in each file pointing at the other, explaining that the encoding scheme must stay in sync, and add a unit test in one project asserting parity of behavior/documentation intent.
    • Estimated effort: ~30 minutes
    • Benefits: Lower risk than a full extraction, but at least prevents silent divergence.

Implementation Checklist

  • Review duplication findings
  • Decide between full extraction vs. documented parity (recommendation 1 vs 2)
  • Create refactoring plan
  • Implement changes
  • Update/add unit tests covering both mergers' identity-encoding behavior
  • Verify no functionality broken (existing JUnitReportMergerTests / CtrfReportMergerTests still pass)

Analysis Metadata

  • Analyzed Files: JUnitArtifactPostProcessor.cs, JUnitReportMerger.cs, CtrfArtifactPostProcessor.cs, CtrfReportMerger.cs, CtrfReportMerger.RetryCollapsing.cs, CtrfReportMerger.FileMerge.cs, RetryArtifactProcessor.cs, plus other post-processors in the same commit
  • Detection Method: Semantic code analysis of recent commits
  • Commit: a760075
  • Analysis Date: 2026-08-14

🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · auto · 49.2 AIC · ⌖ 2.27 AIC · ⊞ 12.9K · [◷]( · ◷)

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
  • expires on Aug 16, 2026, 4:49 AM UTC

Activity

  1. github-actions commented on Aug 16, 2026

    @github-actions
    ContributorAuthor

    This issue was automatically closed because it expired on 2026-08-16T04:49:07.749Z.

    Closed by Workflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions