Skip to content

Speed up default scrubbing pattern matching - #2190

Merged
dmontagu merged 3 commits into
mainfrom
codex/optimize-scrubber-regex
Aug 12, 2026
Merged

dmontagu merged 3 commits into
mainfrom
codex/optimize-scrubber-regex

Conversation

@strawgate

@strawgate strawgate commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • add a cheap start-character prefilter before the default scrubbing regex
  • keep custom patterns outside the prefilter so their syntax and precedence remain unchanged
  • verify every documented default produces the same first match as the previous expression, including Unicode case folding and custom-pattern overlap

Why

The previous flat alternation tried every default branch at every string position. Large attribute values, especially values without a sensitive match, spent most of their scrubbing time in re.Pattern.search.

The prefilter rejects positions that cannot begin a default match before evaluating the existing patterns. It does not change the default patterns themselves.

Benchmarks

Median microseconds per re.Pattern.search on synthetic JSON attributes:

Python Matching before Matching after Speedup Benign before Benign after Speedup
3.10 383.7 131.5 2.92x 1026.0 201.6 5.09x
3.12 323.1 102.3 3.16x 884.7 165.8 5.34x
3.13 328.9 103.1 3.19x 883.3 165.1 5.35x
3.14 347.6 113.6 3.06x 971.4 184.3 5.27x

A representative whole-value scrub improved from 3.77 ms to 1.15 ms, a 3.29x speedup.

Validation

  • uv run pytest tests/test_secret_scrubbing.py -q: 18 passed
  • make lint
  • make typecheck
  • pre-commit on changed files
  • full suite: 2,288 passed, 9 skipped, and 2 expected failures; one unrelated Claude Agent SDK cassette snapshot failed because its recorded directory listing differs from a clean worktree, and it reproduces with the original scrubber constructor

Review in cubic

@strawgate strawgate self-assigned this Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8e8a1eb8-ad4a-4bec-af82-3ea72cd04c63

📥 Commits

Reviewing files that changed from the base of the PR and between cfd029a and 4b4414b.

📒 Files selected for processing (1)
  • tests/test_secret_scrubbing.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/logfire (manual)
  • pydantic/pydantic-ai (manual)
  • pydantic/platform (auto-detected)
  • pydantic/pydantic (auto-detected)

📝 Walkthrough

Walkthrough

The scrubbing implementation combines default patterns into an optimized regex with a prefix character class. Scrubber uses this regex before custom patterns. Tests verify default, boundary-sensitive, Unicode, and custom-pattern matching. Additional tests compare optimized and naive results and verify each default pattern’s first match.

Possibly related PRs

  • pydantic/logfire#2236: Both PRs modify default scrubbing pattern handling in logfire/_internal/scrubbing.py; this PR optimizes the regex introduced or configured by PR #2236.

Mergeability Score: ⚪ Minimal · up to 4b441

The change adds a localized prefilter to speed default scrubbing while preserving custom-pattern behavior, with targeted and full-suite validation reported; no actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: improving default scrubbing pattern matching speed.
Description check ✅ Passed The description directly explains the optimization, compatibility requirements, benchmarks, and validation for the scrubbing changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/optimize-scrubber-regex

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread logfire/_internal/scrubbing.py Resolved
Comment thread logfire/_internal/scrubbing.py Resolved
Compare the prefiltered search against each default pattern in isolation,
so a new pattern cannot be silently skipped if its example also contains
an earlier default match.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/test_secret_scrubbing.py`:
- Around line 112-115: Update the callback in the test’s iteration flow to bind
the current scrub_matches list when the callback is created, using a default
argument or callback factory. Preserve appending each match to that iteration’s
list and returning match.value, without closing over the loop-rebound variable.
- Around line 102-103: Update the Unicode fixtures in the secret-scrubbing test
cases to represent the long s and dotless i characters with Unicode escape
sequences instead of literal characters. Preserve the existing case-folding test
values and expected behavior while ensuring Ruff RUF001 passes.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8e0b691b-1880-42a7-bd9d-009294719e28

📥 Commits

Reviewing files that changed from the base of the PR and between 0540f83 and cfd029a.

📒 Files selected for processing (1)
  • tests/test_secret_scrubbing.py
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • pydantic/logfire (manual)
  • pydantic/pydantic-ai (manual)
  • pydantic/platform (auto-detected)
  • pydantic/pydantic (auto-detected)

Comment thread tests/test_secret_scrubbing.py Resolved
Comment thread tests/test_secret_scrubbing.py Resolved
@dsfaccini
dsfaccini self-requested a review August 12, 2026 21:35
Use Unicode escapes for the long-s and dotless-i fixtures, and bind the
per-iteration match list through a callback factory.
@dmontagu
dmontagu added this pull request to the merge queue Aug 12, 2026
Merged via the queue into main with commit 2509186 Aug 12, 2026
29 checks passed
@dmontagu
dmontagu deleted the codex/optimize-scrubber-regex branch August 12, 2026 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants