Skip to content

[duplicate-code] Length-prefixed identity-key encoding duplicated across 3 locations in report mergers #10597

Closed as not planned

Description

@github-actions

Assignee: @copilot

Summary

Commit a760075 ("Consolidate reports across retry attempts", #10542) introduces the same "length-prefixed component" technique for building collision-safe composite identity keys in three separate places, each with its own private helper method and slightly different signature/output format (raw string vs. SHA256 hash). This is a functional duplication of a non-trivial, security/correctness-sensitive algorithm (avoiding key-collision when concatenating variable-length string components).

Duplication Details

Pattern: length-prefixed identity component encoding

  • Severity: Medium

  • Occurrences: 3

  • Locations:

    • src/Platform/Microsoft.Testing.Extensions.JUnitReport/JUnitArtifactPostProcessor.cs (AppendIdentityPart, lines ~122-131) — used by CreateMergeIdFromOrderedInputs
    • src/Platform/Microsoft.Testing.Extensions.JUnitReport/JUnitReportMerger.cs (BuildIdentity, BuildSuiteIdentity, BuildTestIdentity, lines ~339-363) — used by MergeRetryAttempts
    • src/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfReportMerger.RetryCollapsing.cs (AppendIdentityComponent, GetTestIdentity, lines ~114-161) — used by CollapseRetryAttempts
  • Code Sample (JUnitArtifactPostProcessor.cs):

    private static void AppendIdentityPart(StringBuilder builder, string? value)
    {
        if (value is null)
        {
            builder.Append("-1:");
            return;
        }
        builder.Append(value.Length.ToString(CultureInfo.InvariantCulture));
        // ... appends ':' + value
    }
  • 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);

All three implement the identical core idea — prefix each component with its length (or -1 for null) before a : separator — specifically to prevent ambiguous concatenation of variable-length strings from producing colliding keys (the JUnit merger's own doc-comment spells out exactly this collision risk). Yet the encoding details differ subtly between the three (extra \u001f separator in one, no separator in another, different null sentinels), which itself is a maintenance risk: a future contributor fixing a collision edge case in one copy is unlikely to know to fix the other two.

Impact Analysis

  • Maintainability: Any bug fix or hardening (e.g., handling additional edge cases in the encoding) must be manually replicated in three files across two different extensions.
  • Bug Risk: The subtle format differences between copies (with/without extra separator) mean the three implementations are not drop-in interchangeable despite solving the identical problem, increasing the chance that a fix applied to one is forgotten in the others.
  • Code Bloat: ~40 lines of near-identical logic spread across the codebase instead of one shared utility.

Refactoring Recommendations

  1. Extract a shared identity-key builder utility

    • Add a small shared internal helper (e.g., IdentityKeyBuilder.AppendComponent(StringBuilder, string?) or a BuildIdentityKey(params string?[] components) static method) to a common location such as Microsoft.Testing.Platform internals or a shared Microsoft.Testing.Extensions.Reporting project referenced by both JUnitReport and CtrfReport extensions (a shared reporting project already appears to be referenced by AzureDevOpsReport).
    • Estimated effort: 1-2 hours
    • Benefits: Single, well-tested implementation of the collision-safe encoding; consistent behavior across all report post-processors; easier to add future report formats (HTML, TRX) if retry-collapsing is extended to them.
  2. Add a focused unit test for the shared helper

    • Cover null components, empty strings, and strings containing the encoding's own delimiter characters to lock in the collision-avoidance property.
    • Estimated effort: <1 hour

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: ~15 (artifact post-processors and mergers touched by commit a760075)
  • Detection Method: Semantic code analysis of recent commit diff
  • Commit: a760075

🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · auto · 62.8 AIC · ⌖ 2.24 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 17, 2026, 4:22 AM UTC

Activity

  1. github-actions commented on Aug 17, 2026

    @github-actions
    ContributorAuthor

    This issue was automatically closed because it expired on 2026-08-17T04:22:23.107Z.

    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