Skip to content

Duplicate Code: IdCompressor telemetry session sampler block #26986

@github-actions

Description

@github-actions

Analysis of commit 694a71fbc34d56871dbf9e0ae00699e045fd30ad

Assignee: @copilot

Summary

A near-identical block constructs an IEventSampler for IdCompressor telemetry sampling (~5% of sessions) in multiple modules. This is real logic duplication (not boilerplate) and risks future divergence (e.g., sampling rate changes, semantics of “session” sampling, logger wiring).

Duplication Details

Pattern: “IdCompressor telemetry session sampler”

  • Severity: Medium

  • Occurrences: 2

  • Locations:

    • packages/runtime/container-runtime/src/containerRuntime.ts (around lines 1192–1203; constant body 1193–1200)
    • experimental/dds/tree/src/SharedTree.ts (around lines 570–581; constant body 571–578)
  • Code Sample (duplicated):

    const idCompressorEventSampler: IEventSampler = (() => {
        const isIdCompressorTelemetryEnabled = Math.random() < 0.05;
        return {
            sample: () => {
                return isIdCompressorTelemetryEnabled;
            },
        };
    })();

Impact Analysis

  • Maintainability: Any future updates (e.g., tuning 5% rate, sampling strategy, or adding telemetry dimensions) must be applied in multiple places.
  • Bug Risk: It’s easy for one site to drift (e.g., different sampling % or sampler lifetime), causing inconsistent telemetry and confusing analysis.
  • Code Bloat: Small but repeated logic block.

Refactoring Recommendations

  1. Extract a shared helper for “fixed-per-session” sampling

    • Example API: createFixedSessionSampler(probability: number): IEventSampler
    • Suggested location: a shared utilities package already depended on by both call sites (e.g., packages/utils/telemetry-utils/ or a small helper co-located with createSampledLogger if appropriate).
    • Benefits: single source of truth for sampling semantics and rate.
  2. (Optional) Consider deterministic sampling

    • If desired, base sampling on a stable session identifier hash rather than Math.random() to improve reproducibility across restarts.

Implementation Checklist

  • Confirm dependency direction allows both call sites to use the chosen shared helper
  • Introduce helper + unit coverage as appropriate
  • Replace both call sites
  • Validate telemetry behavior unchanged (still ~5% per session)

Analysis Metadata

  • Analyzed Files: Focused semantic scan of high-churn changed .ts/.cjs/.mjs files in the commit
  • Detection Method: Serena symbol inspection + normalized duplicate-window scan
  • Commit: 694a71fbc34d56871dbf9e0ae00699e045fd30ad
  • Analysis Date: 2026-04-09T21:51:31.360Z

Generated by Duplicate Code Detector ·

To install this agentic workflow, run

gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@94662b1dee8ce96c876ba9f33b3ab8be32de82a4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions