Skip to content

⚡ Bolt: Parallelize folder deletion#164

Closed
abhimehro wants to merge 1 commit intomainfrom
bolt-parallel-delete-folders-7500484463374288460
Closed

⚡ Bolt: Parallelize folder deletion#164
abhimehro wants to merge 1 commit intomainfrom
bolt-parallel-delete-folders-7500484463374288460

Conversation

@abhimehro
Copy link
Owner

⚡ Bolt: Parallelize folder deletion

💡 What:
Replaced sequential folder deletion loop with a parallel implementation using ThreadPoolExecutor (5 workers).

🎯 Why:
Deleting folders is a blocking I/O operation. Doing it sequentially for many folders (e.g. 20+) adds significant latency. Parallelizing it reduces this phase's duration from sum(latency) to max(latency) / workers.

📊 Impact:

  • Reduces folder deletion time by up to ~5x (with 5 workers).
  • Verified with tests/test_parallel_delete.py showing significant speedup over sequential mock.

🔬 Measurement:
Run pytest tests/test_parallel_delete.py.


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

- Refactored folder deletion logic into `delete_existing_folders_parallel`
- Uses `ThreadPoolExecutor` (max 5 workers) to delete folders concurrently
- Updates local `existing_folders` state in-place to ensure consistency
- Added `tests/test_parallel_delete.py` to verify speedup and correctness

This optimization reduces the time taken to clean up existing folders before re-syncing, especially when many folders need to be deleted.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
@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.

Copilot AI review requested due to automatic review settings February 5, 2026 14:49
@trunk-io
Copy link

trunk-io bot commented Feb 5, 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.

@github-actions github-actions bot added the python label Feb 5, 2026
duration = end_time - start_time

# Assertions
assert result 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.

# Check that the 10 folders were deleted
for i in range(10):
assert f"Folder {i}" not in existing_folders

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 f"Folder {i}" not in existing_folders

# Check that "Keep Me" is still there
assert "Keep Me" in existing_folders

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.

# Check that "Keep Me" is still there
assert "Keep Me" in existing_folders
assert existing_folders["Keep Me"] == "id_keep"

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 existing_folders["Keep Me"] == "id_keep"

# Check that "New Folder" is not in existing_folders (it wasn't there to begin with)
assert "New Folder" not in existing_folders

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.
client, profile_id, folder_data_list, existing_folders
)

assert result 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 result is False
assert "Folder B" in existing_folders

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.
client, profile_id, folder_data_list, existing_folders
)

assert result is True # At least one succeeded

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.
name = f"Folder {i}"
if i % 2 != 0:
# Odd: Succeeded, should be removed
assert name not in existing_folders

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 name not in existing_folders
else:
# Even: Failed, should remain
assert name in existing_folders

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.
@gemini-code-assist
Copy link

Warning

Gemini encountered an error creating the summary. You can try again by commenting /gemini summary.

duration = end_time - start_time

# Assertions
assert result is 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.

# Check that the 10 folders were deleted
for i in range(10):
assert f"Folder {i}" not in existing_folders

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.
assert f"Folder {i}" not in existing_folders

# Check that "Keep Me" is still there
assert "Keep Me" in existing_folders

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.

# Check that "Keep Me" is still there
assert "Keep Me" in existing_folders
assert existing_folders["Keep Me"] == "id_keep"

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.
assert existing_folders["Keep Me"] == "id_keep"

# Check that "New Folder" is not in existing_folders (it wasn't there to begin with)
assert "New Folder" not in existing_folders

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.
client, profile_id, folder_data_list, existing_folders
)

assert result is False

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.
)

assert result is False
assert "Folder B" in existing_folders

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.
client, profile_id, folder_data_list, existing_folders
)

assert result is True # At least one succeeded

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.
name = f"Folder {i}"
if i % 2 != 0:
# Odd: Succeeded, should be removed
assert name not in existing_folders

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.
assert name not in existing_folders
else:
# Even: Failed, should remain
assert name in existing_folders

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.
@@ -0,0 +1,124 @@

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

Missing module docstring Warning test

Missing module docstring


def sync_profile(
profile_id: str,

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
del existing_folders[name]
deletion_occurred = True
deletion_occurred = delete_existing_folders_parallel(
client, profile_id, folder_data_list, existing_folders

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
@@ -0,0 +1,124 @@

import concurrent.futures

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused import concurrent.futures Note test

Unused import concurrent.futures
import concurrent.futures
import time
from unittest.mock import MagicMock
import pytest

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused import pytest Note test

Unused import pytest

# All these folders exist in existing_folders
existing_folders = {f"Folder {i}": f"id_{i}" for i in range(10)}
original_len = len(existing_folders)

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused variable 'original_len' Note test

Unused variable 'original_len'
# Verify mock calls
assert mock_delete.call_count == 10

def test_delete_existing_folders_parallel_no_deletions(monkeypatch):

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused argument 'monkeypatch' Note test

Unused argument 'monkeypatch'

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused argument 'folder_id' Note test

Unused argument 'folder_id'

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused argument 'profile_id' Note test

Unused argument 'profile_id'

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Unused argument 'client' Note test

Unused argument 'client'
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_name = {
executor.submit(delete_folder, client, profile_id, name, fid): name
for name, fid in folders_to_delete

Check warning

Code scanning / Pylint (reported by Codacy)

Wrong hanging indentation before block (add 4 spaces). Warning

Wrong hanging indentation before block (add 4 spaces).
executor.submit(delete_folder, client, profile_id, name, fid): name
for name, fid in folders_to_delete
}

Check warning

Code scanning / Pylint (reported by Codacy)

Wrong hanging indentation before block (add 4 spaces). Warning

Wrong hanging indentation before block (add 4 spaces).
for name, fid in folders_to_delete
}

for future in concurrent.futures.as_completed(future_to_name):

Check warning

Code scanning / Pylint (reported by Codacy)

Wrong hanging indentation before block (add 4 spaces). Warning

Wrong hanging indentation before block (add 4 spaces).
@@ -0,0 +1,124 @@

Check warning

Code scanning / Pylint (reported by Codacy)

Missing module docstring Warning test

Missing module docstring

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check warning

Code scanning / Pylint (reported by Codacy)

Missing function docstring Warning test

Missing function docstring
# Mock delete_folder to simulate network delay
# Each deletion takes 0.1s
mock_delete = MagicMock()
def slow_delete(client, profile_id, name, folder_id):

Check notice

Code scanning / Pylint (reported by Codacy)

Unused argument 'client' Note test

Unused argument 'client'
# Mock delete_folder to simulate network delay
# Each deletion takes 0.1s
mock_delete = MagicMock()
def slow_delete(client, profile_id, name, folder_id):

Check notice

Code scanning / Pylint (reported by Codacy)

Unused argument 'name' Note test

Unused argument 'name'
# Mock delete_folder to simulate network delay
# Each deletion takes 0.1s
mock_delete = MagicMock()
def slow_delete(client, profile_id, name, folder_id):

Check notice

Code scanning / Pylint (reported by Codacy)

Unused argument 'folder_id' Note test

Unused argument 'folder_id'
import concurrent.futures
import time
from unittest.mock import MagicMock
import pytest

Check notice

Code scanning / Pylint (reported by Codacy)

Unused import pytest Note test

Unused import pytest
@@ -0,0 +1,124 @@

import concurrent.futures

Check notice

Code scanning / Pylint (reported by Codacy)

Unused import concurrent.futures Note test

Unused import concurrent.futures
if not no_delete:
deletion_occurred = False
for folder_data in folder_data_list:
name = folder_data["group"]["group"].strip()

Check warning

Code scanning / Prospector (reported by Codacy)

Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning

Use lazy % formatting in logging functions (logging-fstring-interpolation)
main.py Outdated

Check warning

Code scanning / Prospector (reported by Codacy)

Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning

Use lazy % formatting in logging functions (logging-fstring-interpolation)
@@ -0,0 +1,124 @@

import concurrent.futures

Check warning

Code scanning / Prospector (reported by Codacy)

Unused import concurrent.futures (unused-import) Warning test

Unused import concurrent.futures (unused-import)
import concurrent.futures
import time
from unittest.mock import MagicMock
import pytest

Check warning

Code scanning / Prospector (reported by Codacy)

Unused import pytest (unused-import) Warning test

Unused import pytest (unused-import)
import concurrent.futures
import time
from unittest.mock import MagicMock
import pytest

Check warning

Code scanning / Prospector (reported by Codacy)

Unable to import 'pytest' (import-error) Warning test

Unable to import 'pytest' (import-error)
# Verify mock calls
assert mock_delete.call_count == 10

def test_delete_existing_folders_parallel_no_deletions(monkeypatch):

Check warning

Code scanning / Prospector (reported by Codacy)

Unused argument 'monkeypatch' (unused-argument) Warning test

Unused argument 'monkeypatch' (unused-argument)

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check warning

Code scanning / Prospector (reported by Codacy)

expected 1 blank line before a nested definition, found 0 (E306) Warning test

expected 1 blank line before a nested definition, found 0 (E306)

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check warning

Code scanning / Prospector (reported by Codacy)

Unused argument 'profile_id' (unused-argument) Warning test

Unused argument 'profile_id' (unused-argument)

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check warning

Code scanning / Prospector (reported by Codacy)

Unused argument 'client' (unused-argument) Warning test

Unused argument 'client' (unused-argument)

# Mock delete_folder: Fail for even numbers
mock_delete = MagicMock()
def conditional_delete(client, profile_id, name, folder_id):

Check warning

Code scanning / Prospector (reported by Codacy)

Unused argument 'folder_id' (unused-argument) Warning test

Unused argument 'folder_id' (unused-argument)
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 parallelizes folder deletion to improve performance by replacing sequential deletion with a ThreadPoolExecutor-based implementation using 5 workers. The change reduces folder deletion time from O(sum of latencies) to O(max latency / workers), potentially achieving up to 5x speedup for scenarios with many folders.

Changes:

  • Added delete_existing_folders_parallel() function that uses ThreadPoolExecutor with 5 workers to delete folders concurrently
  • Replaced sequential folder deletion loop in sync_profile() with a call to the new parallel implementation
  • Added comprehensive test suite with 3 test cases covering happy path, no-match scenario, and partial failure handling

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
main.py Implements parallel folder deletion function and integrates it into sync_profile workflow
tests/test_parallel_delete.py Adds comprehensive tests validating parallel execution, correctness, and partial failure handling

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

@@ -0,0 +1,124 @@

import concurrent.futures
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The import of concurrent.futures is unused in this test file. It should be removed to keep the imports clean.

Suggested change
import concurrent.futures

Copilot uses AI. Check for mistakes.

# All these folders exist in existing_folders
existing_folders = {f"Folder {i}": f"id_{i}" for i in range(10)}
original_len = len(existing_folders)
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The variable original_len is assigned but never used. Consider removing it or using it in an assertion to verify the test setup.

Copilot uses AI. Check for mistakes.
# 10 tasks * 0.1s = 1.0s sequential time.
# With 5 workers, it should take roughly 0.2s + overhead.
# We assert it takes < 0.6s to be safe but prove parallelism.
assert duration < 0.6, f"Execution took {duration}s, expected < 0.6s (parallel)"
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The timing assertion could be flaky on slower systems or under heavy load. Consider either increasing the threshold (e.g., to 0.8s or 1.0s) or using a more flexible assertion that checks if the parallel version is faster than sequential without relying on absolute time, such as comparing the duration to the theoretical sequential time with a tolerance factor.

Copilot uses AI. Check for mistakes.
Comment on lines +1068 to +1071
# Using 5 workers to be safe with rate limits for write operations
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_name = {
executor.submit(delete_folder, client, profile_id, name, fid): name
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The httpx.Client instance is being shared across multiple threads (5 workers). According to httpx documentation, Client instances are not thread-safe and should not be shared across threads without synchronization. While this pattern exists elsewhere in the codebase (e.g., in get_all_existing_rules), the concurrent modifications to the client's connection pool could lead to race conditions. Consider using a lock around client operations, creating a separate client instance in each thread, or using a thread-safe connection pool pattern.

Suggested change
# Using 5 workers to be safe with rate limits for write operations
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_name = {
executor.submit(delete_folder, client, profile_id, name, fid): name
# Protect shared httpx.Client instance when used across multiple threads.
client_lock = threading.Lock()
def _threadsafe_delete_folder(profile_id_arg: str, name_arg: str, fid_arg: str) -> bool:
"""
Wrapper around delete_folder that serializes access to the shared client.
"""
with client_lock:
return delete_folder(client, profile_id_arg, name_arg, fid_arg)
# Using 5 workers to be safe with rate limits for write operations
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_name = {
executor.submit(_threadsafe_delete_folder, profile_id, name, fid): name

Copilot uses AI. Check for mistakes.
import concurrent.futures
import time
from unittest.mock import MagicMock
import pytest
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

Import of 'pytest' is not used.

Suggested change
import pytest

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>
@abhimehro abhimehro closed this Feb 9, 2026
@abhimehro abhimehro deleted the bolt-parallel-delete-folders-7500484463374288460 branch February 9, 2026 02:18
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