Skip to content

[duplicate-code] Duplicate Code: Base64-encoded retry-artifact manifest parsing/writing duplicated across PackagedApp and Retry extensions #11568

Closed

Description

@github-actions

Summary

The recovered-retry-artifact manifest format — a tab-separated, base64-encoded path\tkind line format used to hand off recovered artifact paths between an activated AppContainer test host and the retry orchestrator — is parsed and written independently in two different projects, with duplicated size-limit constants and duplicated base64 decode/re-encode logic.

Duplication Details

Pattern: Manifest encode/decode + size-limit constants duplicated

  • Severity: Medium

  • Occurrences: 2 (parsing side) + related constant duplication

  • Locations:

    • src/Platform/Microsoft.Testing.Extensions.PackagedApp/ActivatedAppTestHostHandle.cs, method RecoverRetryArtifactManifest() (lines ~139-215) — introduced in Enable MTP for UWP and AppContainer Windows apps #11484. Redeclares MaxManifestBytes = 16L * 1024 * 1024, MaxManifestLineChars = 64 * 1024, MaxManifestRecords = 10_000 as local consts, then manually parses each line: split on first \t, base64-decode the left side with Encoding.UTF8.GetString(Convert.FromBase64String(...)), and re-encodes recovered paths the same way for the destination manifest.
    • src/Platform/Microsoft.Testing.Extensions.Retry/RetryOrchestrator.ArtifactRecovery.cs, method CollectRecoveredArtifacts() (lines ~24-115) — same commit (Enable MTP for UWP and AppContainer Windows apps #11484). Declares the same three limits as private const fields on RetryOrchestrator (MaxRecoveredArtifactManifestBytes, MaxRecoveredArtifactManifestLineBytes/records), and independently parses the identical path\tkind base64 format (including the same kind == "-" null-sentinel convention), plus adds a bounded streaming reader (BoundedManifestLineReader) that the PackagedApp side does not reuse.
  • Code Sample (writer side, ActivatedAppTestHostHandle.cs):

    string sourcePath = Path.GetFullPath(
        Encoding.UTF8.GetString(Convert.FromBase64String(line.Substring(0, separatorIndex))));
    ...
    recoveredLines.Add(
        $"{Convert.ToBase64String(Encoding.UTF8.GetBytes(Path.GetFullPath(recoveredPath)))}{line[separatorIndex..]}");

    vs. reader side, RetryOrchestrator.ArtifactRecovery.cs:

    string path = Encoding.UTF8.GetString(Convert.FromBase64String(line.Substring(0, separatorIndex)));
    string encodedKind = line.Substring(separatorIndex + 1);
    string? kind = encodedKind == "-"
        ? null
        : Encoding.UTF8.GetString(Convert.FromBase64String(encodedKind));

Impact Analysis

  • Maintainability: The manifest is a hand-rolled wire format shared implicitly between a producer (ActivatedAppTestHostHandle) and a consumer (RetryOrchestrator). Its encoding rules (tab separator, base64 fields, - sentinel for null kind, size limits) are currently documented only by the two independent implementations, so changing the format requires touching both in lockstep with no compiler-enforced link between them.
  • Bug Risk: The size-limit constants (16 MB total, 64 KB per line, 10,000 records) are duplicated as separate literals in two projects. A future tuning change to one limit without the other silently reintroduces mismatched truncation behavior between producer and consumer.
  • Code Bloat: ~90 combined lines of manifest-format logic that encodes the same tiny protocol.

Refactoring Recommendations

  1. Extract a shared manifest serializer

    • Create a small shared type (e.g. RetryArtifactManifest with WriteEntry/TryParseEntry static methods and the three limit constants) in a project referenced by both Microsoft.Testing.Extensions.PackagedApp and Microsoft.Testing.Extensions.Retry (or SharedExtensionHelpers if both already reference it).
    • Estimated effort: Medium (~2-3 hours) — needs careful handling since RetryOrchestrator.ArtifactRecovery.cs also owns the bounded streaming reader (BoundedManifestLineReader), which is more sophisticated than the File.ReadLines-based approach in ActivatedAppTestHostHandle.cs.
    • Benefits: Single source of truth for the wire format and its limits; producer and consumer can't drift out of sync; the streaming reader could be reused for the writer side too if beneficial.
  2. At minimum, share the limit constants

    • If a full serializer extraction is too invasive right now, at least move the three size-limit constants into one shared location referenced by both files, eliminating the risk of the two sides silently disagreeing.
    • Estimated effort: Low (~30 min).
    • Benefits: Removes the most bug-prone part of the duplication (mismatched limits) with minimal risk.

Implementation Checklist

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

Analysis Metadata

🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · copilot · auto · 100.8 AIC · ⌖ 7.37 AIC · ⊞ 13.4K · [◷]( · ◷)

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 Sep 30, 2026, 4:38 AM UTC

Metadata

Metadata

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