Repository navigation
Description
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 byCreateMergeIdFromOrderedInputssrc/Platform/Microsoft.Testing.Extensions.JUnitReport/JUnitReportMerger.cs(BuildIdentity,BuildSuiteIdentity,BuildTestIdentity, lines ~339-363) — used byMergeRetryAttemptssrc/Platform/Microsoft.Testing.Extensions.CtrfReport/CtrfReportMerger.RetryCollapsing.cs(AppendIdentityComponent,GetTestIdentity, lines ~114-161) — used byCollapseRetryAttempts
-
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
-
Extract a shared identity-key builder utility
- Add a small shared internal helper (e.g.,
IdentityKeyBuilder.AppendComponent(StringBuilder, string?)or aBuildIdentityKey(params string?[] components)static method) to a common location such asMicrosoft.Testing.Platforminternals or a sharedMicrosoft.Testing.Extensions.Reportingproject referenced by bothJUnitReportandCtrfReportextensions (a shared reporting project already appears to be referenced byAzureDevOpsReport). - 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.
- Add a small shared internal helper (e.g.,
-
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
github-actions commentedon Aug 17, 2026
This issue was automatically closed because it expired on 2026-08-17T04:22:23.107Z.