← Back to Pull requests

perf(ci_visibility): make the coverage no-cover pragma check linear #22226

Closed
unknown closed this pull request without merging on Dec 02, 2026 09:02 UTC
View on GitHub
unknown unknown commented on Dec 01, 2026 14:57 UTC
Author

Description

APPSEC-72086

no_cover() in ddtrace/internal/coverage/report.py matches each stripped source line against ^\s(?P<command>.)\s#.\s+pragma\s:\sno\s?cover.$. The command group, the whitespace before # and the whitespace before pragma can all take the same whitespace runs, so a line with a long run of spaces on both sides of a # and no pragma takes time cubic in its length. With 2,000 spaces on each side, one match takes about 9.5 s.

NOCOVER_PRAGMA_RE is now \spragma\s:\sno\s?cover, and the new _nocover_command() returns the text before the last # that comes before the last pragma match. That is the # the greedy command group chose, so the command is the same. Two pragma matches can't overlap, so the last finditer match is the last place a pragma starts; a one-line comment above the loop says so. Lines like #pragma: no cover and pragma: no cover still don't count as pragmas, as with the old regex: this PR keeps the existing behavior.

report.py is frozen for the ddtrace-coverage extraction. It is unfrozen with the agreement of the ddtrace-coverage extraction owner (@gnufede), so this PR removes its entry from frozen-paths.txt, as the manifest header describes. frozen-paths.txt is owned by @DataDog/apm-core-python. I will add the entry back with the new hash in a follow-up PR right after this one merges:

ddtrace/internal/coverage/report.py=41539375a747d4f0167737ff8dabe4a68adf2b9898d65d020e1c90b1bd9b03cd

Customer impact

None. Since #13982 removed the old pytest plugin (v3.11.0), ModuleCodeCollector.report() and write_json_report_to_file() are only called from tests, and the result is the same. There is no release note, hence the changelog/no-changelog label.

Testing

  • New tests/coverage/test_report.py: _nocover_command on pragma: no cover and pragma: nocover, a # inside a string before the comment, text between # and pragma, a string containing a pragma before the real one (fails if the loop is replaced by search()), and four lines that are not pragmas, including a pragma with no # before it (fails if the # check is dropped).

  • New linear-time test on "# " 30_000, bounded at 1 s. It takes about 15 s with the previous regex locally, and under 1 ms now.

  • Not committed: the old and new versions return the same command on 300,000 random lines built from #, whitespace (including \x0c, \x1c and non-breaking spaces), :, pragma, no, cover, nocover, quotes and letters.

  • ci_visibility::dd_coverage suite: passed on Python 3.9 (100 passed) and 3.15 (165 passed).

  • scripts/lint checks and scripts/lint typing on report.py.
  • Risks

    Low: the result is the same for every line tested, and the code only runs when a coverage report is generated.

    Additional Notes

    This change was dropped from #22200 because of the freeze. The extracted copy of this file in DataDog/datadog-apm-python#82 (packages/ddtrace-coverage/src/_ddtrace_coverage/report.py) has the same regex; I will port the same change there separately. Follow-up to #22183, #22192, #22194, #22200, #22215 and #22216, under the same Jira ticket.

    🤖 Generated with Claude Code

    [APPSEC-72086]: https://datadoghq.atlassian.net/browse/APPSEC-72086?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

    Diff: 432 lines (+324 / -108) across 5 files Reviewer: gnufede Review duration: 1084m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 432 lines diff across 5 files
    0.45
    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
    Real PR Conversation Timeline
    1 comments
    "Review active on #22226"
    general weight: 0.35
    Reviewers
    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: 45.2% (MEDIUM) · Higher = larger blast radius
    45.2%
    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: 17.8% · Safe to merge
    17.8%