← Back to Pull requests

fix(test-optimization): use unmocked monotonic clock for telemetry durations #11495

Closed
unknown closed this pull request without merging on Nov 30, 2026 10:34 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 14:21 UTC
Author

What does this PR do?


  • Captures process.hrtime.bigint at module load in packages/dd-trace/src/ci-visibility/telemetry.js (monotonicNow), returning elapsed milliseconds relative to a module-load origin (Number(hrtimeBigint() - clockOriginNs) / 1e6) so fake timers patching Date.now(), performance.now(), or process.hrtime (including on Node.js 18 where performance.now() reads process.hrtime()) cannot skew durations.

  • Replaces live Date.now() and performance.now() telemetry duration measurements with monotonicNow() across all Test Optimization _MS telemetry emitters:

  • - packages/dd-trace/src/ci-visibility/exporters/agentless/writer.js
    - packages/dd-trace/src/ci-visibility/exporters/agentless/coverage-writer.js
    - packages/dd-trace/src/encode/agentless-ci-visibility.js
    - packages/dd-trace/src/encode/coverage-ci-visibility.js
    - packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js
    - packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js
    - packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js
    - packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js
    - packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js
    - packages/dd-trace/src/ci-visibility/requests/upload-coverage-report.js
    - packages/dd-trace/src/plugins/util/git.js
  • Keeps Date.now() for the two 5000ms execFileSync retry deadline budgets in git.js so fake-timer deadline tests and behavior remain unchanged.

  • Adds deterministic unit tests in telemetry.spec.js, writer.spec.js, and coverage-writer.spec.js (capturing a controllable process.hrtime.bigint stub at module load and asserting exact elapsed durations across fake-timer ticks and restoration), and updates get-known-tests.spec.js to stub monotonicNow.
  • Motivation


    Test suites commonly use fake timers (sinon.useFakeTimers, Jest fake timers, Vitest fake timers) that patch global Date.now(), performance.now(), and process.hrtime to jump forward/backward in simulated time (or reset to epoch 0). When a Test Optimization request or payload encoding spans a fake-timer installation, tick, or restoration, Date.now() - startTime produces wildly skewed durations (negative values or multi-decade epochs) in
    _ms telemetry distributions. Capturing the native process.hrtime.bigint reference at module load isolates internal telemetry timing from test timer mocks across all supported Node.js versions.

    Additional Notes


    Validated on Node.js v24.14.1 (7.0.0-pre and v5 5.0.0 compatibility on all modified Test Optimization specs), eslint, and editorconfig.

    Made with Cursor

    Diff: 344 lines (+258 / -86) across 4 files Reviewer: juan-fernandez Review duration: 4093m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 344 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.350
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #11495"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%