← Back to Pull requests

fix(ci_visibility): encode bytearray bitmaps in payload files mode #22186

Closed
unknown closed this pull request without merging on Nov 30, 2026 16:19 UTC
View on GitHub
unknown unknown commented on Nov 30, 2026 15:01 UTC
Author

Description

In payload-files mode (Bazel offline, DD_TEST_OPTIMIZATION_PAYLOADS_IN_FILES=true), no coverage payload files were ever written: only .tmp leftovers remained, while test-event payloads were written fine.

Root cause: CoverageLines.to_bytes() returns its internal bytearray, not bytes. The isinstance(..., bytes) check in PayloadFileCoverageWriter._send_events left such bitmaps unencoded, so json.dump rejected the payload and write_payload_file dropped it (logging only a warning), never performing the atomic rename.

The check is broadened to accept bytearray alongside bytes so bitmaps are base64-encoded and the coverage payload file is written. The fix is made in the payload writer rather than in CoverageLines.to_bytes(): that method feeds the hot per-test HTTP coverage path (where msgpack handles bytearray fine), and returning real bytes would add a buffer copy per file there; the payload-files writer is the only consumer that needs bytes semantics and is cold-path.

Reproduced externally with ddtrace 4.16.1 from PyPI running pygments' test suite (5308 tests, zero coverage payload files).

Testing

  • New regression test test_coverage_bytearray_bitmaps_are_base64_encoded sources its bitmap from CoverageLines.from_list(...).to_bytes() (the actual bytearray producer) and asserts the coverage JSON file is written and decodes with the bitmap round-tripping through base64.

  • Mutation-checked: reverting the one-line fix makes the new test fail exactly as reported (no coverage JSON file, only .tmp).

  • tests/testing/internal/test_payload_files.py, test_offline_mode.py, test_bazel_offline_session_manager.py: 81 passed (Python 3.12, ci_visibility::testing suite env).

  • Full tests/testing/ suite: 1584 passed; the single failure (test_pytest_xdist.py::TestResolveInheritedManifestEnv::test_discards_manifest_generated_by_unrelated_process) reproduces on a clean tree and is unrelated/pre-existing.

  • scripts/lint fmt, typing, and checks all pass.
  • Risks

    None: the change is confined to the payload-files-mode writer, which previously produced zero usable output for coverage payloads.

    Additional Notes

    No runtime benchmarks run — the change affects only the cold offline-mode write path. Also fixes a pre-existing mypy type-arg error in the same test file that blocked the pre-commit hook.

    End-to-end verification (payload-files mode against a real test suite)

    Reproduced the reported scenario with pygments' test suite (GitHub main) on Python 3.12, two clean venvs, run with DD_TEST_OPTIMIZATION_PAYLOADS_IN_FILES=true, TEST_UNDECLARED_OUTPUTS_DIR=..., _DD_CIVISIBILITY_ITR_FORCE_ENABLE_COVERAGE=true and pytest --ddtrace (tests/contrast excluded — missing wcag_contrast_ratio dependency, unrelated):

    | Payload files | ddtrace 4.16.1 (PyPI) | This branch |
    |---|---|---|
    | tests-.json | 15 | 15 |
    | telemetry-
    .json | 2 | 2 |
    | coverage-.json | 0 | 11 |
    | coverage-
    .json.tmp leftovers | 11 | 0 |

    Baseline shows the reported warning on every coverage flush (Error writing payload file ... Object of type bytearray is not JSON serializable); the branch run has zero such warnings. All 11 coverage payload files decode as JSON with the expected version/test_session_id/test_suite_id/span_id/files structure, and every bitmap is valid strict base64 decoding to non-empty bytes (e.g. /tests/conftest.py).

    Diff: 432 lines (+324 / -108) across 5 files Reviewer: gnufede Review duration: 78m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.68) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.68 · 432 lines diff across 5 files
    0.68
    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.363
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.068
    Real PR Conversation Timeline
    1 comments
    "Review active on #22186"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    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: 68.3% (HIGH) · Higher = larger blast radius
    68.3%
    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: 27% · Safe to merge
    27%