Skip to content

perf(detector): Slice 2 — single-pass fused regex closes Pro vs OpenCore regression (0.574x → 1.20x) - #235

Closed
cursor[bot] wants to merge 3 commits into
mainfrom
cursor/sre-agent-protocol-0871
Closed

cursor[bot] wants to merge 3 commits into
mainfrom
cursor/sre-agent-protocol-0871

Conversation

@cursor

@cursor cursor Bot commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Description

Slice 2 of the Engineering Doctrine consolidation (driven by the Slack handoff after Slice 1 pinned the 0.574x regression as official benchmark evidence).

Result on the same synthetic 200k seed and runner:

Metric Slice 1 (frozen) Slice 2 (this PR)
opencore_seconds 0.252242 0.108942
pro_seconds 0.439419 0.091026
speedup_vs_opencore (t_open / t_pro) 0.574 (Pro slower) 1.197 (Pro faster)
opencore_hits 100000 100000
pro_hits 100000 100000
rust_worker_path true (hardcoded — wrong) false (real)

Findings parity preserved; zero database / lock impact; the Rust → Python fallback hierarchy is unchanged.

Root cause analysis (Julia Evans-style)

Three concrete bottlenecks in the Pro Python fallback path (verified by cProfile-equivalent reasoning over the call graph):

  1. Double regex pass in pro/worker_logic.py::basic_python_scan. The OpenCore CPF/e-mail regex ran over every row, then the card-shape regex ran over every non-matching row to catch Luhn-valid cards. Net effect: ~2x the regex work of OpenCore on the seeded workload — exactly what 0.574x reflected (Pro wall-clock ≈ 1 / 0.574 ≈ 1.74x OpenCore).
  2. Two re.search calls per row in core/prefilter.py. _looks_sensitive invoked the CPF regex and the e-mail regex separately on the hot path.
  3. Phantom deep_ml_analysis hop in pro/engine.py::process_chunk_worker. A no-op pass that materialised a second list and added a function-call dispatch per chunk.

Refactor (Gibson / Savage / NASA posture)

  • Fused candidate regex in core/prefilter.py — _OPENCORE_CANDIDATE_RX = CPF | e-mail as a single compiled alternation; hot loop binds .search to a local attribute and uses a list comprehension. The original _CPF_CANDIDATE_RX and _EMAIL_CANDIDATE_RX symbols stay exported for backward compatibility.
  • Single-pass Pro fallback in pro/worker_logic.py — _FUSED_PRO_RX = CPF | e-mail | card-shape with named groups. CPF and e-mail short-circuit on the first match; Luhn arithmetic only fires when the regex actually matches a card-shape span. A defensive finditer walk handles rows that carry both an invalid card-shape and a valid CPF / e-mail later in the string, so detection coverage is unchanged.
  • Honest worker pipeline in pro/engine.py — process_chunk_worker no longer routes through deep_ml_analysis; the helper stays defined for backward-compat imports.
  • Truthful artifact in tests/benchmarks/run_official_bench.py — rust_worker_path is now derived from pro.engine.RUST_AVAILABLE instead of being hardcoded True.
  • Polarity-flipped regression guard in tests/test_official_benchmark_200k_evidence.py — old assertion (speedup < 1.0, "Pro slower") was the right Slice 1 pin but would block legitimate progress; new assertion speedup >= 0.95 (5 percent noise band on slow CI) plus untouched findings parity and JSON arithmetic checks.

Doctrine alignment

  • docs/ops/inspirations/DEFENSIVE_SCANNING_MANIFESTO.md — refactor only touches in-memory pre-filter code: no SQL composition, no DB connection, no lock acquisition, no audit shape change. Same finding contract.
  • docs/ops/inspirations/THE_ART_OF_THE_FALLBACK.md — Rust → Python fallback hierarchy preserved. The faster Python fallback narrows the gap between strongest and weakest path, which is the doctrinal goal: when boar_fast_filter is unavailable in customer environments, Pro stays useful instead of being a regression.
  • Publication truthfulness — rust_worker_path flag now reflects reality.

Type of change

  • Bug fix (performance regression)
  • New feature
  • Documentation (lab-lessons archive + post-mortem update)
  • Refactor / maintenance (single-pass fused regex)
  • Other

Checklist

  • Tests pass locally — full Python suite green (903 passed, 5 skipped). The 35 PowerShell-syntax tests in tests/test_scripts.py are pre-existing failures on this Linux Cloud Agent (no pwsh installed) and unrelated to this PR (verified with git stash baseline).
  • ruff check and ruff format --check pass on the touched files.
  • Docs updated — LAB_LESSONS_LEARNED.md hub refreshed, new dated archive lab_lessons_learned/LAB_LESSONS_LEARNED_2026_04_27_slice2.md per ADR 0042 contract, SPRINT_GREAT_LEAP_POSTMORTEM.md and pt-BR mirror updated.
  • No security-sensitive changes; no new third-party identifiers; no database / lock surface touched.

Related

  • Slack handoff (operator → SRE automation agent): "identifique o gargalo técnico no motor de processamento … traga a performance para perto de 1.0x".
  • Slice 1 frozen archive: docs/ops/lab_lessons_learned/LAB_LESSONS_LEARNED_2026_04_25.md, reading guide docs/ops/lab_lessons_learned/LAB_LESSONS_LEARNED_2026_04_27.md.
  • Slice 2 archive (this PR): docs/ops/lab_lessons_learned/LAB_LESSONS_LEARNED_2026_04_27_slice2.md.
  • Driver plan: docs/plans/PLAN_ENGINEERING_DOCTRINE_CONSOLIDATION.md.
Open in Web View Automation 

cursoragent and others added 3 commits April 27, 2026 16:24
Close the Pro-vs-OpenCore regression captured by the 200k A/B benchmark
(speedup_vs_opencore = 0.574 in Slice 1 -> ~1.20 in Slice 2 on the same
synthetic seed and runner) without changing the finding contract.

Three concrete bottlenecks were addressed:

1. core/prefilter.py: fuse CPF and e-mail patterns into a single compiled
   alternation (_OPENCORE_CANDIDATE_RX). The hot loop now binds
   .search to a local attribute and uses a list comprehension. Original
   _CPF_CANDIDATE_RX and _EMAIL_CANDIDATE_RX symbols stay exported for
   backward-compat with downstream importers and tests.

2. pro/worker_logic.py: rewrite basic_python_scan as a SINGLE pass per
   row using a fused alternation _FUSED_PRO_RX with named groups
   (cpf | email | card). CPF and e-mail short-circuit on the first match;
   Luhn arithmetic only fires when the regex actually matches a card-shape
   span. Defensive finditer fallback handles rows that carry an invalid
   card plus a valid CPF / e-mail later in the string, so detection
   coverage is unchanged.

3. pro/engine.py: process_chunk_worker no longer routes through the
   no-op deep_ml_analysis hop on the hot path. The helper stays defined
   for backward import compatibility.

Defensive posture (see docs/ops/inspirations/DEFENSIVE_SCANNING_MANIFESTO.md):
the refactor only touches in-memory pre-filter code -- no SQL composition,
no connection, no lock acquisition. Same finding contract; OpenCore <-> Pro
hits parity (100,000 == 100,000) preserved.

Resilience posture (see docs/ops/inspirations/THE_ART_OF_THE_FALLBACK.md):
the Rust -> Python fallback hierarchy is unchanged. The faster Python
fallback narrows the gap between the strongest and the weakest path so
Pro stays useful even when the boar_fast_filter extension is missing.

Co-authored-by: Fabio Tavares Leitão <[email protected]>
…arity

The Slice 1 regression guard intentionally pinned the bad direction
(test_benchmark_pro_path_is_slower_in_this_profile) so executive copy
could not silently flip the sign of the recorded 0.574x ratio.

After the Slice 2 refactor in core/prefilter.py and pro/worker_logic.py,
the same 200k A/B benchmark records speedup_vs_opencore = 1.197 (Pro
faster than OpenCore on the Cloud Agent VM, Python fallback path,
rust_worker_path = false). The guard now asserts the doctrinal
direction:

- test_benchmark_pro_path_is_at_least_as_fast: speedup >= 0.95
  (5 percent noise band on slow CI runners).
- Findings parity (opencore_hits == pro_hits) and JSON arithmetic
  (opencore_seconds / pro_seconds approx speedup_vs_opencore) stay
  non-negotiable.

Also fix a publication-truthfulness bug in run_official_bench.py: the
artifact previously hardcoded rust_worker_path = True regardless of
whether boar_fast_filter was importable. The runner now reads
pro.engine.RUST_AVAILABLE and writes the real value, so manifests and
executive decks cannot misattribute Python-fallback numbers to the Rust
path.

Co-authored-by: Fabio Tavares Leitão <[email protected]>
Add an immutable Slice 2 archive snapshot under
docs/ops/lab_lessons_learned/LAB_LESSONS_LEARNED_2026_04_27_slice2.md
recording:

- The three concrete bottlenecks in the Pro Python fallback (double
  regex pass, two re.search calls per row, no-op deep_ml_analysis hop).
- The fused-regex refactor that closed them (Julia Evans-style RCA).
- Defensive posture (zero DB locks, same finding contract, hits parity).
- Resilience posture (Rust -> Python fallback hierarchy unchanged).
- Verified before/after table (200k rows, 8 workers).
- The polarity flip on the regression guard.

Refresh docs/ops/LAB_LESSONS_LEARNED.md hub with the new latest summary
(Pro now faster) and append the new dated archive row. The 2026-04-25
and original 2026-04-27 reading-guide snapshots stay frozen per the
archive contract in ADR 0042.

Update docs/ops/SPRINT_GREAT_LEAP_POSTMORTEM.md (and pt-BR mirror) to
distinguish Slice 1 (frozen 0.574x measurement) from Slice 2 (current
1.197x measurement) and add a short note on the refactor that fixed
the regression.

Co-authored-by: Fabio Tavares Leitão <[email protected]>
@cursor
cursor Bot deleted the cursor/sre-agent-protocol-0871 branch April 28, 2026 10:10
FabioLeitao added a commit that referenced this pull request May 3, 2026
Dated audit + workflow doc updates for the Slack ts 1777370978.136399
ask: 'git branch -r' does not fit one screen, delete cursor/sre-*
that are 'integradas ou abandonadas', rule 'se nao tem PR aberto, nao
tem por que ter branch no origin'.

RCA (audit doc):
  * After 'git fetch --prune origin' at 2026-04-28T10:19Z (main 4bc0861):
    - total remote refs:                    41
    - backed by an OPEN PR (gh):            40 of 41
    - in keep-list (main):                   1
    - TRULY orphan (no PR, not in keep):     1
        fix-rust-ci-coverage-9061
  * The 40 cursor/sre-* branches are NEITHER 'integradas' NOR
    'abandonadas' -- they back live OPEN PRs (#235-#289). Deleting
    them would auto-close the PRs and lose the audit paper trail
    (FABRICATED_CLAIMS_INDEX.md cross-references).
  * Real screen-bloat lever is the open-PR backlog, not orphans
    (PR #273 covers the 'audit-PR swarm' RCA).

Doc updates:
  * BRANCH_AND_DOCKER_CLEANUP.md S3.1 (EN) / S2.1 (pt-BR): point at
    new lister, reaffirm open-PR rule + qualifier 'sidequest
    investigativa obvia progredindo'.
  * sre_audits/README.md: index sync.

Doctrine: DEFENSIVE_SCANNING_MANIFESTO S1.3 (no surprise side
effects), THE_ART_OF_THE_FALLBACK S2-S3 (monotonic ladder, diagnostic
on fall), AGENTS.md Risk posture.

No behavior change. Zero impact on database locks.

Co-authored-by: Fabio Tavares Leitão <[email protected]>
@FabioLeitao FabioLeitao closed this May 3, 2026
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.

2 participants