Skip to content

🧹 [code health improvement] Refactor push_rules to improve maintainability#804

Open
abhimehro wants to merge 1 commit into
mainfrom
jules-4638151365976133235-ac41d747
Open

🧹 [code health improvement] Refactor push_rules to improve maintainability#804
abhimehro wants to merge 1 commit into
mainfrom
jules-4638151365976133235-ac41d747

Conversation

@abhimehro
Copy link
Copy Markdown
Owner

@abhimehro abhimehro commented May 14, 2026

🎯 What:
The push_rules function in main.py was too large and complex, handling everything from deduplication, string filtering against a restricted charset, to batch splitting and multi-threaded execution. It was broken down into _filter_rules_for_folder, _push_single_batch, and _push_rule_batches.

💡 Why:
Decomposing complex functions with multiple responsibilities ("Brain Methods") into smaller, highly cohesive helper functions reduces cyclomatic complexity, makes the core orchestrating logic easier to read at a glance, and improves modular testability.

Verification:

  • Modified push_rules locally by replacing logic blocks with calls to newly defined private helper functions
  • Verified codebase structural health by running uv tool run ruff format . && uv tool run ruff check .
  • Verified type safety using uv run mypy .
  • Ensured functional equivalence by running the full test suite (uv run pytest) and executing performance tests in tests/test_push_rules_perf.py. All tests passed successfully.

Result:
The push_rules function is now cleaner and delegates responsibilities. Existing performance optimizations, like the dictionary comprehension for _filter_rules_for_folder and ThreadPoolExecutor for API batches, have been successfully preserved and isolated.


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


Open in Devin Review

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

👋 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
Copy Markdown

trunk-io Bot commented May 14, 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.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@github-actions github-actions Bot added documentation Improvements or additions to documentation python labels May 14, 2026
Copy link
Copy Markdown

@codescene-delta-analysis codescene-delta-analysis 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 Health Improved (1 files improve in Code Health)

Gates Passed
6 Quality Gates Passed

See analysis details in CodeScene

View Improvements
File Code Health Impact Categories Improved
main.py 1.66 → 1.70 Complex Method, Complex Conditional, Bumpy Road Ahead, Overall Code Complexity

Quality Gate Profile: Pay Down Tech Debt
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.

Copy link
Copy Markdown

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

Open in Devin Review

Comment thread main.py
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 Info: Thread safety of ctx.existing_rules.update() is unchanged

In _push_rule_batches (line 2341), ctx.existing_rules.update(result) is called from the main thread inside the as_completed loop, not from worker threads. This is the same pattern as the old code. The _push_single_batch function no longer has access to ctx.existing_rules at all, which is actually slightly safer than the old closure approach. However, note that set.update() in CPython is atomic due to the GIL, so even concurrent access would not corrupt the set — but the sequential update pattern is cleaner.

(Refers to lines 2337-2347)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread main.py
Comment on lines +2215 to +2264
def _push_single_batch(
client: httpx.Client,
profile_id: str,
sanitized_folder_name: str,
str_do: str,
str_status: str,
str_group: str,
batch_idx: int,
batch_data: list[str],
) -> list[str] | None:
"""Processes a single batch of rules by sending API request."""
data = {
"do": str_do,
"status": str_status,
"group": str_group,
}
# Optimization: Use pre-calculated keys and zip for faster dict update
# strict=False is intentional: batch_data may be shorter than BATCH_KEYS for final batch
data.update(zip(BATCH_KEYS, batch_data, strict=False))

try:
_api_post_form(client, f"{API_BASE}/{profile_id}/rules", data=data)
if not USE_COLORS:
log.info(
"Folder %s – batch %d: added %d %s",
sanitized_folder_name,
batch_idx,
len(batch_data),
pluralize(len(batch_data), "rule"),
)
return batch_data
except httpx.HTTPError as e:
if USE_COLORS:
sys.stderr.write("\r\033[K")
sys.stderr.flush()
hint = ""
if isinstance(e, httpx.HTTPStatusError):
# Use a more specific name to avoid confusion with the rule "status" payload
status_code = e.response.status_code
hint = f" ({_STATUS_HINTS.get(status_code, f'HTTP {status_code}')})"
log.error(
f"Failed to push batch {batch_idx} for folder {sanitized_folder_name}{hint}: {sanitize_for_log(e)}"
)
return True
if (
hasattr(e, "response")
and e.response is not None
and log.isEnabledFor(logging.DEBUG)
):
log.debug(f"Response content: {sanitize_for_log(e.response.text)}")
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📝 Info: Closure-to-module-function extraction preserves test compatibility

The old process_batch was a nested closure inside push_rules that captured ctx, sanitized_folder_name, str_do, str_status, and str_group from the enclosing scope. The new _push_single_batch takes all of these as explicit parameters. Multiple tests (e.g., tests/test_status_hints.py:155, tests/test_security.py:21, tests/test_security_hardening.py:70) patch main._api_post_form or main.log at the module level. Since _push_single_batch is now a module-level function that resolves _api_post_form and log as globals at call time, these patches continue to work correctly. No test breakage expected.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants