Skip to content

⚡ Bolt: Parallelize folder deletion and optimize regex#149

Closed
abhimehro wants to merge 1 commit intomainfrom
bolt/parallel-deletion-4728167391881213641
Closed

⚡ Bolt: Parallelize folder deletion and optimize regex#149
abhimehro wants to merge 1 commit intomainfrom
bolt/parallel-deletion-4728167391881213641

Conversation

@abhimehro
Copy link
Owner

  • 💡 What:
    • Parallelized folder deletion in sync_profile using ThreadPoolExecutor (max 5 workers).
    • Pre-compiled regex pattern in is_valid_rule.
  • 🎯 Why:
    • Deleting multiple folders sequentially is a bottleneck (latency * N). Parallel execution reduces this to ~latency.
    • Pre-compiling regex avoids overhead in tight loops.
  • 📊 Impact: Faster sync process, especially during the "clean slate" phase.
  • 🔬 Measurement: Verified with test_sync_profile_parallel_deletion test case.

PR created automatically by Jules for task 4728167391881213641 started by @abhimehro

- Replaced sequential folder deletion loop with concurrent execution using `ThreadPoolExecutor`
- Pre-compiled regex pattern for `is_valid_rule`
- Added tests for parallel deletion and regex optimization

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 31, 2026 14:46
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@trunk-io
Copy link

trunk-io bot commented Jan 31, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@gemini-code-assist
Copy link

Summary of Changes

Hello @abhimehro, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant performance enhancements to the synchronization process. By parallelizing folder deletions, it transforms a sequential bottleneck into a more efficient concurrent operation. Additionally, it optimizes regex validation by pre-compiling the pattern, which reduces overhead in frequently called functions. These changes collectively aim to deliver a faster and more responsive sync experience, particularly during the initial setup or 'clean slate' phase.

Highlights

  • Parallel Folder Deletion: The sync_profile function now parallelizes folder deletion using a ThreadPoolExecutor with a maximum of 5 workers, significantly reducing the time taken for this operation.
  • Regex Optimization: The regular expression used in is_valid_rule is now pre-compiled into a global constant RULE_PATTERN, avoiding repeated compilation overhead for performance.
  • New Test Cases: Added new test cases (test_is_valid_rule_uses_regex and test_sync_profile_parallel_deletion) to verify the correct implementation and functionality of the regex optimization and parallel deletion.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

# Case 14: is_valid_rule uses pre-compiled regex
def test_is_valid_rule_uses_regex(monkeypatch):
m = reload_main_with_env(monkeypatch)
assert m.is_valid_rule("valid.rule") is True

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
def test_is_valid_rule_uses_regex(monkeypatch):
m = reload_main_with_env(monkeypatch)
assert m.is_valid_rule("valid.rule") is True
assert m.is_valid_rule("invalid rule") is False

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
m = reload_main_with_env(monkeypatch)
assert m.is_valid_rule("valid.rule") is True
assert m.is_valid_rule("invalid rule") is False
assert m.is_valid_rule("invalid;rule") is False

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert m.is_valid_rule("invalid rule") is False
assert m.is_valid_rule("invalid;rule") is False
# Check that RULE_PATTERN is available
assert hasattr(m, "RULE_PATTERN")

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
assert m.is_valid_rule("invalid;rule") is False
# Check that RULE_PATTERN is available
assert hasattr(m, "RULE_PATTERN")
assert m.RULE_PATTERN.pattern == r"^[a-zA-Z0-9.\-_:*\/]+$"

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
m.sync_profile("pid", folder_urls, no_delete=False)

# Verification
assert mock_delete.call_count == 2

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
has_deletion_pool = True
break

assert (

Check notice

Code scanning / Bandit

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
monkeypatch.setattr(m, "delete_folder", mock_delete)

# Mock other calls to prevent errors
monkeypatch.setattr(m, "get_all_existing_rules", MagicMock(return_value=set()))

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.

# Mock other calls to prevent errors
monkeypatch.setattr(m, "get_all_existing_rules", MagicMock(return_value=set()))
monkeypatch.setattr(m, "countdown_timer", MagicMock())

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
# Mock other calls to prevent errors
monkeypatch.setattr(m, "get_all_existing_rules", MagicMock(return_value=set()))
monkeypatch.setattr(m, "countdown_timer", MagicMock())
monkeypatch.setattr(m, "_process_single_folder", MagicMock(return_value=True))

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
monkeypatch.setattr(m, "countdown_timer", MagicMock())
monkeypatch.setattr(m, "_process_single_folder", MagicMock(return_value=True))

# Mock fetching

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
monkeypatch.setattr(m, "_process_single_folder", MagicMock(return_value=True))

# Mock fetching
mock_validate = MagicMock(return_value=True)

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
test_main.py Outdated

Check notice

Code scanning / Bandit (reported by Codacy)

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test

Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.
@@ -1,3 +1,4 @@
import concurrent.futures

Check warning

Code scanning / Pylint (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
# Mock delete_folder to track calls
mock_delete = MagicMock(return_value=True)
monkeypatch.setattr(m, "delete_folder", mock_delete)

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning test

Missing function docstring
mock_validate.cache_clear = MagicMock()
monkeypatch.setattr(m, "validate_folder_url", mock_validate)

def fetch_side_effect(url):

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning test

Missing function docstring
# 2. Deletion (max_workers=5)
# 3. Processing (max_workers=1)

has_deletion_pool = False

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning test

Missing function docstring
@@ -1,3 +1,4 @@
import concurrent.futures

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing module docstring Warning test

Missing module docstring
# Mock delete_folder to track calls
mock_delete = MagicMock(return_value=True)
monkeypatch.setattr(m, "delete_folder", mock_delete)

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing function or method docstring Warning test

Missing function or method docstring
mock_delete = MagicMock(return_value=True)
monkeypatch.setattr(m, "delete_folder", mock_delete)

# Mock other calls to prevent errors

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Variable name "m" doesn't conform to snake_case naming style Warning test

Variable name "m" doesn't conform to snake_case naming style
mock_validate.cache_clear = MagicMock()
monkeypatch.setattr(m, "validate_folder_url", mock_validate)

def fetch_side_effect(url):

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing function or method docstring Warning test

Missing function or method docstring
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces two solid performance optimizations: pre-compiling a regular expression and parallelizing folder deletion. The changes are well-implemented and include corresponding tests, which is great to see. The pre-compilation of the regex in is_valid_rule is a standard and effective optimization. Parallelizing the I/O-bound deletion tasks using a ThreadPoolExecutor should significantly improve the performance of the sync process, especially for profiles with many folders. I have a couple of minor suggestions to improve maintainability by replacing hardcoded values with constants.


if folders_to_delete:
# Parallel delete to speed up the "clean slate" phase
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as delete_executor:

Choose a reason for hiding this comment

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

medium

To improve maintainability and avoid magic numbers, it's best to define 5 as a constant at the top of the file (e.g., DELETE_WORKERS = 5 in the constants section) and use the constant here. This makes it easier to find and change in the future.

# In constants section
DELETE_WORKERS = 5

# In sync_profile
with concurrent.futures.ThreadPoolExecutor(max_workers=DELETE_WORKERS) as delete_executor:


has_deletion_pool = False
for call_args in calls:
if call_args.kwargs.get("max_workers") == 5:

Choose a reason for hiding this comment

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

medium

This assertion hardcodes the value 5, creating a tight coupling with the implementation. If the number of workers is extracted into a constant in main.py (as suggested in my other comment), this test should reference that constant to remain robust against future changes. For example:

if call_args.kwargs.get("max_workers") == m.DELETE_WORKERS:

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements two performance optimizations to speed up the sync process: pre-compiling a regex pattern used in rule validation and parallelizing folder deletion during the "clean slate" phase.

Changes:

  • Pre-compiled the regex pattern used in is_valid_rule to avoid repeated compilation overhead in tight loops
  • Parallelized folder deletion in sync_profile using ThreadPoolExecutor with 5 workers to reduce latency during cleanup
  • Added tests to verify the regex pre-compilation and parallel deletion behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
main.py Added pre-compiled RULE_PATTERN constant and updated is_valid_rule to use it; refactored folder deletion to use parallel execution with ThreadPoolExecutor
test_main.py Added test_is_valid_rule_uses_regex to verify regex pre-compilation; added test_sync_profile_parallel_deletion to verify parallel folder deletion behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


if folders_to_delete:
# Parallel delete to speed up the "clean slate" phase
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as delete_executor:
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

Consider using max_workers=3 for DELETE operations instead of 5, consistent with POST operations which also modify state. Line 945 mentions "aggressive rate limits" for POST operations, and DELETE operations may face similar rate limiting. While GETs use 5 workers (line 682), that's because they're "usually cheaper" on rate limits. This would maintain the parallelization benefit while being more conservative about rate limits.

Suggested change
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as delete_executor:
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as delete_executor:

Copilot uses AI. Check for mistakes.
Copilot AI added a commit that referenced this pull request Feb 8, 2026
…itization, add dry-run plan details

Incorporates the best changes from 36 Jules PRs, addressing review feedback:

Bolt (Performance) - from PR #173:
- Pre-compile PROFILE_ID_PATTERN and RULE_PATTERN at module level
- Use compiled patterns in is_valid_profile_id_format, validate_profile_id, and is_valid_rule
- Supersedes PRs: #140, #143, #152, #155, #158, #161, #167, #170, #173

Sentinel (Security) - from PR #172 with review feedback:
- Enhance sanitize_for_log to redact Basic Auth credentials in URLs
- Redact sensitive query parameters (token, key, secret, password, etc.)
- Handle fragment separators (#) per Gemini Code Assist review
- Use [^&#\s]* pattern per Copilot reviewer suggestion
- Update docstring per reviewer suggestion
- Supersedes PRs: #142, #145, #148, #151, #154, #157, #160, #169, #172

Palette (UX) - from PR #174 with lint fixes:
- Add print_plan_details function for dry-run visibility
- Fix duplicate render_progress_bar definition bug
- Supersedes PRs: #139, #141, #144, #147, #150, #153, #156, #159, #162, #165, #168, #171, #174

Also: #146, #149, #164 (parallel folder deletion) and #166 (auto-fix .env perms) are independent features not consolidated here.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
abhimehro added a commit that referenced this pull request Feb 9, 2026
…itization, add dry-run plan details

Incorporates the best changes from 36 Jules PRs, addressing review feedback:

Bolt (Performance) - from PR #173:
- Pre-compile PROFILE_ID_PATTERN and RULE_PATTERN at module level
- Use compiled patterns in is_valid_profile_id_format, validate_profile_id, and is_valid_rule
- Supersedes PRs: #140, #143, #152, #155, #158, #161, #167, #170, #173

Sentinel (Security) - from PR #172 with review feedback:
- Enhance sanitize_for_log to redact Basic Auth credentials in URLs
- Redact sensitive query parameters (token, key, secret, password, etc.)
- Handle fragment separators (#) per Gemini Code Assist review
- Use [^&#\s]* pattern per Copilot reviewer suggestion
- Update docstring per reviewer suggestion
- Supersedes PRs: #142, #145, #148, #151, #154, #157, #160, #169, #172

Palette (UX) - from PR #174 with lint fixes:
- Add print_plan_details function for dry-run visibility
- Fix duplicate render_progress_bar definition bug
- Supersedes PRs: #139, #141, #144, #147, #150, #153, #156, #159, #162, #165, #168, #171, #174

Also: #146, #149, #164 (parallel folder deletion) and #166 (auto-fix .env perms) are independent features not consolidated here.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
Copilot AI added a commit that referenced this pull request Feb 9, 2026
- Parallelize folder deletion using ThreadPoolExecutor with 3 workers
- Use conservative worker count (3) instead of 5 to respect API rate limits
- Add DELETE_WORKERS constant for easy configuration
- Sanitize both folder names and exceptions in error logging to prevent log injection
- Use lazy % formatting for logging (best practice)
- Rename exception variable from 'e' to 'exc' for clarity
- Add comprehensive test coverage for parallel deletion

Addresses feedback from PRs #146, #149

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
abhimehro added a commit that referenced this pull request Feb 9, 2026
- Parallelize folder deletion using ThreadPoolExecutor with 3 workers
- Use conservative worker count (3) instead of 5 to respect API rate limits
- Add DELETE_WORKERS constant for easy configuration
- Sanitize both folder names and exceptions in error logging to prevent log injection
- Use lazy % formatting for logging (best practice)
- Rename exception variable from 'e' to 'exc' for clarity
- Add comprehensive test coverage for parallel deletion

Addresses feedback from PRs #146, #149

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
@abhimehro abhimehro closed this Feb 9, 2026
@abhimehro abhimehro deleted the bolt/parallel-deletion-4728167391881213641 branch February 9, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants