← Back to Pull requests

perf(ci_visibility): make four CI Visibility and testing regexes linear #22200

Open
unknown opened this pull request with 581 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 30, 2026 16:57 UTC
Author

Description

APPSEC-72086

Four regexes in CI Visibility and the testing plugin could take time quadratic in their input on some inputs, because each failed attempt rescanned the rest of the input. They now run in linear time, with the same results on the inputs they are called with.

  • Git lock errors (testing/internal/git.py), called with .search on git's stderr: the regex is replaced by _is_git_lock_error(). It uses _contains_in_order(), which finds each part with str.find at least one character after the previous one ends, as .+ required. For Unable to create .+\.lock.+File exists, the earliest Unable to create and then the earliest .lock after it leave the most room for what follows, so checking only those is enough. The same holds for cannot lock ref .+ but expected.

  • Repository name (ci_visibility/recorder.py): the name group is [^/\n]? instead of .?, so a failed attempt cannot rescan past the next / or newline. The regex is also compiled once at module level.

  • Pytest node IDs (testing/internal/pytest/utils.py): compiled with re.DOTALL, so the name group always reaches the end and a failed name can no longer force a rescan.

  • Service name from the repository URL (testing/internal/session_manager.py): anchored with ^. Its call site uses re.match, which was already linear, so this changes nothing there. A scan of the repository's regexes, which assumes re.search, flags the unanchored pattern; the anchor keeps it linear if the call changes.
  • Customer impact

    None. These inputs are git output, repository URLs and pytest node IDs, and the results are the same. There is no release note, hence the changelog/no-changelog label.

    Testing

  • New linear-time tests, bounded at 1 s. With the previous regexes they take, locally:

  • - _is_git_lock_error on 'cannot lock ref ' 30_000: about 8 s;
    - the repository-name regex on 'a/'
    30_000 + '\nx': about 17 s;
    - nodeid_to_names on a 60 KB node ID with a newline: about 3 s.
  • New behavior tests:

  • - _is_git_lock_error on the three messages from the module comment, three near misses, and the boundaries: one character between the parts is a lock error, none is not, as with the previous regex;
    - nodeid_to_names on a node ID with a newline in its test name, the one declared behavior change.
  • Not committed: on 300,000 random inputs each, the old and new versions give the same results for the git lock check, the repository name and the service name. The node-ID regex gives the same groups for every node ID without an inner newline (300,000 inputs, plus 100,000 ending in a newline).

  • Local runs with scripts/run-tests:

  • - ci_visibility::ci_visibility, -k repository_name (Python 3.9 and 3.15);
    - ci_visibility::testing, -k "IsGitLockError or GitLockRetry or NodeIdToTestRef" (3.9 and 3.15).
  • scripts/lint checks.
  • Risks

  • Pytest node IDs that contain a newline before the end now treat it like any other character. Under the previous regex a newline was only allowed in the suite part, between the last / and ::; a newline in the module path or the test name made the match fail, so the node ID fell back to unknown_module and unknown_suite. Pytest escapes newlines in parametrize IDs, so this needs a test file path or a custom item name that contains a newline.
  • Additional Notes

  • The no-cover pragma regex in ddtrace/internal/coverage/report.py was dropped from this PR. That file is frozen for the ddtrace-coverage extraction (frozen-paths.txt, #22134), so it is left unchanged here, and the fix should land in the extracted package instead. The previous regex, ^\s(?P<command>.)\s#.\s+pragma\s:\sno\s?cover.$, is quadratic on long source lines with many #, such as '# ' n. A linear replacement with the same command on every stripped line is: find the last match of \spragma\s:\sno\s?cover, then take everything before the last # that comes before it (text[:text.rfind("#", 0, match.start())]), or no match when either is missing.

  • Not changed here: the service-name regex's (?:.git) has an unescaped . and is not optional, so a URL ending in /org/my-digit gives the service name my-d. Fixing it changes behavior, so it belongs in a separate change with a release note.

  • Follow-up to #22183, #22192 and #22194, under the same Jira ticket.
  • 🤖 Generated with Claude Code

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

    Diff: 581 lines (+435 / -146) across 7 files Reviewer: gnufede Review duration: 855m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 581 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.381
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #22200"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 49.9% (MEDIUM) · Higher = larger blast radius
    49.9%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 26.6% · Safe to merge
    26.6%