Skip to content

⚡ Bolt: optimize PriorityEngine with regex caching and I/O throttling#527

Open
RohanExploit wants to merge 4 commits intomainfrom
bolt-optimize-priority-engine-16553946901950357167
Open

⚡ Bolt: optimize PriorityEngine with regex caching and I/O throttling#527
RohanExploit wants to merge 4 commits intomainfrom
bolt-optimize-priority-engine-16553946901950357167

Conversation

@RohanExploit
Copy link
Owner

@RohanExploit RohanExploit commented Mar 9, 2026

💡 What: Optimized the PriorityEngine and AdaptiveWeights services in the backend.

  • In backend/priority_engine.py, I replaced repeated re.search calls with a cached list of pre-compiled regex objects.
  • In backend/adaptive_weights.py, I added a 5-second time-based throttle to _check_reload to prevent redundant os.path.getmtime and json.load calls on every analysis request.

🎯 Why: High-frequency civic reports trigger the PriorityEngine for every submission. Frequent regex compilation and filesystem checks were creating unnecessary CPU and I/O overhead.

📊 Impact: Measurable ~33% performance boost in the core analysis path. Benchmark results showed average latency dropped from ~0.09ms to ~0.06ms per request.

🔬 Measurement: Ran benchmark_priority.py before and after changes. Verified logic correctness with 52 existing tests in backend/tests/test_priority_engine.py.


PR created automatically by Jules for task 16553946901950357167 started by @RohanExploit


Summary by cubic

Optimized PriorityEngine with cached pre-compiled regex and ensured cache invalidates on adaptive weight reloads, cutting average analyze latency by ~33% (0.09 ms → 0.06 ms). Added benchmark_priority.py and fixed Render deployment issues.

  • Refactors

    • Implemented a pre-compiled regex cache in backend/priority_engine.py with invalidation tied to AdaptiveWeights._check_reload.
    • Added a 5s throttle in backend/adaptive_weights.py to limit config file I/O.
  • Dependencies

    • Fixed Render builds by adding python-dotenv, async_lru, indic-nlp-library, and pinning googletrans to 4.0.2.

Written for commit 59abbc9. Summary will update on new commits.

Summary by CodeRabbit

  • New Features

    • Added a benchmarking utility for measuring priority analysis performance.
    • Improved priority scoring performance via cached regex patterns and time-based reload throttling.
  • Documentation

    • Added a guidance section on regex pre-compilation and I/O throttling (duplicated entry appears in the document).

- Implement regex pre-compilation and caching in `PriorityEngine`
- Add 5-second throttle for `AdaptiveWeights` configuration reload
- Improve analysis throughput by ~33% (0.09ms -> 0.06ms)
- Add benchmark script for performance verification
Copilot AI review requested due to automatic review settings March 9, 2026 13:58
@google-labs-jules
Copy link
Contributor

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

@netlify
Copy link

netlify bot commented Mar 9, 2026

Deploy Preview for fixmybharat canceled.

Name Link
🔨 Latest commit 59abbc9
🔍 Latest deploy log https://app.netlify.com/projects/fixmybharat/deploys/69aee8c394ba93000814d6ff

@github-actions
Copy link

github-actions bot commented Mar 9, 2026

🙏 Thank you for your contribution, @RohanExploit!

PR Details:

Quality Checklist:
Please ensure your PR meets the following criteria:

  • Code follows the project's style guidelines
  • Self-review of code completed
  • Code is commented where necessary
  • Documentation updated (if applicable)
  • No new warnings generated
  • Tests added/updated (if applicable)
  • All tests passing locally
  • No breaking changes to existing functionality

Review Process:

  1. Automated checks will run on your code
  2. A maintainer will review your changes
  3. Address any requested changes promptly
  4. Once approved, your PR will be merged! 🎉

Note: The maintainers will monitor code quality and ensure the overall project flow isn't broken.

@coderabbitai
Copy link

coderabbitai bot commented Mar 9, 2026

📝 Walkthrough

Walkthrough

Adds regex pre-compilation caching in the priority engine and time-based throttling for adaptive weights reloads; introduces a benchmark script, updates documentation (duplicate insertion), and adds three new Python dependencies.

Changes

Cohort / File(s) Summary
I/O Throttling
backend/adaptive_weights.py
Added _last_check_time and modified _check_reload() to only reload if >5 seconds elapsed, reducing frequent I/O in hot paths.
Regex Pattern Caching
backend/priority_engine.py
Added _regex_cache, _last_loaded_time, and _get_compiled_patterns() to cache pre-compiled urgency regexes and invalidate cache when adaptive weights refresh; urgency loop updated to use compiled patterns and record pattern strings as reasons.
Benchmarking
benchmark_priority.py
New script benchmarks priority_engine.analyze() over many iterations, reporting total and average execution time (ms).
Documentation
.jules/bolt.md
Inserted documentation section "2026-02-12 - Regex Caching and I/O Throttling in Priority Engine" (duplicate insertion present).
Dependencies
backend/requirements-render.txt
Added python-dotenv, async_lru, and indic-nlp-library to the Python requirements manifest.
Runtime Log
backend_output.txt
Replaced startup/watch logs with runtime execution logs showing adaptive weights/rag service load, warnings, server start/shutdown sequence.

Sequence Diagram

sequenceDiagram
    participant Client
    participant PriorityEngine
    participant Cache
    participant AdaptiveWeights

    Client->>PriorityEngine: analyze(text)
    PriorityEngine->>PriorityEngine: _get_compiled_patterns()

    alt Cache valid
        PriorityEngine->>Cache: retrieve compiled patterns
        Cache-->>PriorityEngine: compiled regex objects
    else Cache missing or stale
        PriorityEngine->>AdaptiveWeights: _check_reload()
        alt >5s since last check
            AdaptiveWeights->>AdaptiveWeights: _load_weights()
            AdaptiveWeights->>AdaptiveWeights: update _last_check_time
            AdaptiveWeights-->>PriorityEngine: latest patterns
        else <5s (throttled)
            AdaptiveWeights-->>PriorityEngine: skip reload (cached data used)
        end
        PriorityEngine->>Cache: store compiled patterns
    end

    PriorityEngine->>PriorityEngine: compute urgency using compiled patterns
    PriorityEngine-->>Client: return score + reasons
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I pre-compile, I stash, I keep patterns light,
Throttle the reloads so I don't wake at night,
Benchmarks hum as the runtimes shrink,
Docs sing twice (oops) — but the engine can think,
Hooray for faster hops and nimble delight!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main changes: optimizing PriorityEngine with regex caching and I/O throttling, which are the primary objectives of the pull request.
Description check ✅ Passed The PR description is comprehensive and well-structured, covering what was changed, why, and the measured impact with specific performance metrics.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bolt-optimize-priority-engine-16553946901950357167

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="benchmark_priority.py">

<violation number="1" location="benchmark_priority.py:7">
P2: Using `os.getcwd()` for import path setup is brittle; resolve the path from the script location so imports work regardless of launch directory.</violation>
</file>

<file name="backend/adaptive_weights.py">

<violation number="1" location="backend/adaptive_weights.py:45">
P2: Use a monotonic clock for throttle intervals; wall-clock adjustments can break reload timing.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

import re

# Add project root to sys.path
sys.path.append(os.getcwd())
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

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

P2: Using os.getcwd() for import path setup is brittle; resolve the path from the script location so imports work regardless of launch directory.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At benchmark_priority.py, line 7:

<comment>Using `os.getcwd()` for import path setup is brittle; resolve the path from the script location so imports work regardless of launch directory.</comment>

<file context>
@@ -0,0 +1,29 @@
+import re
+
+# Add project root to sys.path
+sys.path.append(os.getcwd())
+
+from backend.priority_engine import priority_engine
</file context>
Fix with Cubic

# Optimization: Checking mtime is fast (stat call).
self._load_weights()
# Optimization: 5-second throttle to prevent excessive I/O in hot paths.
now = time.time()
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

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

P2: Use a monotonic clock for throttle intervals; wall-clock adjustments can break reload timing.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend/adaptive_weights.py, line 45:

<comment>Use a monotonic clock for throttle intervals; wall-clock adjustments can break reload timing.</comment>

<file context>
@@ -40,8 +41,11 @@ def _load_weights(self):
-        # Optimization: Checking mtime is fast (stat call).
-        self._load_weights()
+        # Optimization: 5-second throttle to prevent excessive I/O in hot paths.
+        now = time.time()
+        if now - self._last_check_time > 5:
+            self._last_check_time = now
</file context>
Suggested change
now = time.time()
now = time.monotonic()
Fix with Cubic

Copy link
Contributor

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 optimizes the backend’s hot-path priority analysis by reducing repeated regex work in PriorityEngine and throttling filesystem-based weight reload checks in AdaptiveWeights.

Changes:

  • Added caching of pre-compiled urgency regex patterns in backend/priority_engine.py, with cache invalidation tied to adaptive weights reload time.
  • Added a 5-second throttle to AdaptiveWeights._check_reload() to reduce repeated stat/json.load work.
  • Introduced a small benchmark script (benchmark_priority.py) and documented the optimization in .jules/bolt.md.

Reviewed changes

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

File Description
benchmark_priority.py Adds a simple local benchmark loop for PriorityEngine.analyze() latency.
backend/priority_engine.py Caches compiled urgency regexes and reuses them across analyses to reduce per-request overhead.
backend/adaptive_weights.py Throttles hot-path reload checks to reduce filesystem I/O frequency.
.jules/bolt.md Documents the optimization approach and rationale in the Jules “bolt” log.

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

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +22 to +34
Invalidates cache if adaptive weights have been updated.
"""
# Ensure latest weights are at least checked for reload (subject to throttling)
adaptive_weights._check_reload()

current_load_time = adaptive_weights._last_loaded
if not self._regex_cache or current_load_time > self._last_loaded_time:
urgency_patterns = adaptive_weights.get_urgency_patterns()
self._regex_cache = [
(re.compile(pattern, re.IGNORECASE), weight)
for pattern, weight in urgency_patterns
]
self._last_loaded_time = current_load_time
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

PriorityEngine is reaching into adaptive_weights internals (_check_reload() and _last_loaded). This tight coupling makes future refactors of AdaptiveWeights risky and bypasses its public API. Consider adding a small public method (e.g., get_urgency_patterns_compiled() or get_weights_version()/last_loaded()) so cache invalidation doesn’t depend on private members.

Suggested change
Invalidates cache if adaptive weights have been updated.
"""
# Ensure latest weights are at least checked for reload (subject to throttling)
adaptive_weights._check_reload()
current_load_time = adaptive_weights._last_loaded
if not self._regex_cache or current_load_time > self._last_loaded_time:
urgency_patterns = adaptive_weights.get_urgency_patterns()
self._regex_cache = [
(re.compile(pattern, re.IGNORECASE), weight)
for pattern, weight in urgency_patterns
]
self._last_loaded_time = current_load_time
This implementation only relies on the public AdaptiveWeights API
and does not access its private internals, to avoid tight coupling.
"""
urgency_patterns = adaptive_weights.get_urgency_patterns()
self._regex_cache = [
(re.compile(pattern, re.IGNORECASE), weight)
for pattern, weight in urgency_patterns
]

Copilot uses AI. Check for mistakes.
if not self._regex_cache or current_load_time > self._last_loaded_time:
urgency_patterns = adaptive_weights.get_urgency_patterns()
self._regex_cache = [
(re.compile(pattern, re.IGNORECASE), weight)
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

re.IGNORECASE is likely redundant here because analyze() lowercases the input text before calling _calculate_urgency(). Dropping the flag avoids extra regex work and makes matching behavior depend only on the normalized text.

Suggested change
(re.compile(pattern, re.IGNORECASE), weight)
(re.compile(pattern), weight)

Copilot uses AI. Check for mistakes.
# Optimization: Checking mtime is fast (stat call).
self._load_weights()
# Optimization: 5-second throttle to prevent excessive I/O in hot paths.
now = time.time()
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

For interval-based throttling, time.monotonic() is safer than time.time() because it won’t be affected by system clock adjustments (NTP, manual changes). Using monotonic time avoids unexpected skipped reloads or burst reloads if the wall clock jumps.

Suggested change
now = time.time()
now = time.monotonic()

Copilot uses AI. Check for mistakes.
Comment on lines +4 to +10
import re

# Add project root to sys.path
sys.path.append(os.getcwd())

from backend.priority_engine import priority_engine
from backend.adaptive_weights import adaptive_weights
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

re and adaptive_weights are imported but unused in this benchmark script. Removing unused imports keeps the benchmark minimal and avoids confusion about what’s being measured.

Suggested change
import re
# Add project root to sys.path
sys.path.append(os.getcwd())
from backend.priority_engine import priority_engine
from backend.adaptive_weights import adaptive_weights
# Add project root to sys.path
sys.path.append(os.getcwd())
from backend.priority_engine import priority_engine

Copilot uses AI. Check for mistakes.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (5)
benchmark_priority.py (3)

10-10: Unused import: adaptive_weights

The adaptive_weights import is not used in the benchmark. If it was intended to be used for setup or verification, that code is missing.

🧹 Remove unused import
 from backend.priority_engine import priority_engine
-from backend.adaptive_weights import adaptive_weights
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@benchmark_priority.py` at line 10, The import adaptive_weights in
benchmark_priority.py is unused; either remove the unused import line ("from
backend.adaptive_weights import adaptive_weights") or, if it was intended for
setup/verification, add the missing usage (e.g., call adaptive_weights in the
benchmark initialization or a validation helper). Update the file to eliminate
the unused import warning by deleting the import or inserting the appropriate
setup/verification call to adaptive_weights.

4-4: Unused import: re

The re module is imported but not used in this benchmark script.

🧹 Remove unused import
 import time
 import sys
 import os
-import re
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@benchmark_priority.py` at line 4, The import statement "import re" is unused
in this file; remove the unused import line (the "import re" statement) from
benchmark_priority.py to clean up the module-level imports and avoid lint
warnings.

6-7: sys.path manipulation depends on current working directory.

Using os.getcwd() makes the script's behavior dependent on where it's run from, not where the script is located. This can cause import failures if run from a different directory.

♻️ Use script's directory instead
 # Add project root to sys.path
-sys.path.append(os.getcwd())
+sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@benchmark_priority.py` around lines 6 - 7, The code appends os.getcwd() to
sys.path which makes imports depend on the current working directory; replace
that with the script's directory (e.g., compute project_root from __file__ via
os.path.dirname(os.path.abspath(__file__)) or Path(__file__).resolve().parent)
and add it to sys.path (preferably sys.path.insert(0, ...)) so imports are based
on the script location rather than the process CWD; update the place where
sys.path.append(os.getcwd()) is used accordingly.
backend/adaptive_weights.py (1)

43-48: Thread-safety consideration for multi-threaded environments.

The throttle logic using _last_check_time as a class attribute is not thread-safe. In a multi-threaded environment (e.g., multiple FastAPI workers or asyncio with thread pool executors), concurrent calls could result in redundant reloads or the race condition where one thread reads while another writes.

For the stated optimization goal (reducing I/O in hot paths), this is acceptable since occasional redundant reloads don't break correctness—they just reduce the optimization benefit slightly. However, if strict throttling is desired in a multi-threaded context, consider using a threading.Lock.

🔧 Optional: Thread-safe throttle (if needed)
+import threading
+
 class AdaptiveWeights:
     _instance = None
     _weights = None
     _last_loaded = 0
     _last_check_time = 0
+    _check_lock = threading.Lock()

     ...

     def _check_reload(self):
         # Optimization: 5-second throttle to prevent excessive I/O in hot paths.
         now = time.time()
-        if now - self._last_check_time > 5:
-            self._last_check_time = now
-            self._load_weights()
+        if now - self._last_check_time > 5:
+            with self._check_lock:
+                # Double-check after acquiring lock
+                if now - self._last_check_time > 5:
+                    self._last_check_time = now
+                    self._load_weights()
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/adaptive_weights.py` around lines 43 - 48, The throttle in
_check_reload uses the shared attribute _last_check_time without
synchronization; to make it thread-safe, introduce a threading.Lock (e.g.,
self._reload_lock) and wrap the read/modify of self._last_check_time and the
conditional call to self._load_weights() inside a with self._reload_lock: block;
keep the 5-second semantics but perform the now = time.time(), compare/update of
self._last_check_time, and the call to _load_weights() under the same lock to
avoid races and redundant concurrent reloads while preserving behavior in
single-threaded use.
backend/priority_engine.py (1)

25-29: Accessing private members of AdaptiveWeights creates tight coupling.

Lines 25 and 27 directly access private members (_check_reload() and _last_loaded) of AdaptiveWeights. This violates encapsulation and creates fragile coupling—any refactoring of AdaptiveWeights internals could silently break PriorityEngine.

Additionally, line 29 calls get_urgency_patterns() which internally calls _check_reload() again, making the explicit call at line 25 redundant (the throttle ensures it's cheap, but it's still unnecessary).

Consider adding a public method to AdaptiveWeights that exposes the reload timestamp, or restructure so PriorityEngine doesn't need to inspect internal state.

♻️ Suggested approach: Add a public accessor to AdaptiveWeights

In backend/adaptive_weights.py, add:

def get_last_loaded_time(self) -> float:
    """Returns the timestamp when weights were last loaded from disk."""
    return self._last_loaded

Then in backend/priority_engine.py:

     def _get_compiled_patterns(self):
-        # Ensure latest weights are at least checked for reload (subject to throttling)
-        adaptive_weights._check_reload()
-
-        current_load_time = adaptive_weights._last_loaded
+        # get_urgency_patterns() internally calls _check_reload()
+        urgency_patterns = adaptive_weights.get_urgency_patterns()
+        current_load_time = adaptive_weights.get_last_loaded_time()
+        
         if not self._regex_cache or current_load_time > self._last_loaded_time:
-            urgency_patterns = adaptive_weights.get_urgency_patterns()
             self._regex_cache = [
                 (re.compile(pattern, re.IGNORECASE), weight)
                 for pattern, weight in urgency_patterns
             ]
             self._last_loaded_time = current_load_time
         return self._regex_cache
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/priority_engine.py` around lines 25 - 29, The PriorityEngine
currently accesses AdaptiveWeights internals (_check_reload and _last_loaded)
and redundantly calls reload; add a public accessor on AdaptiveWeights named
get_last_loaded_time() that returns the reload timestamp, remove direct calls to
_check_reload() and references to _last_loaded in PriorityEngine, and change
PriorityEngine to call adaptive_weights.get_last_loaded_time() (and keep using
adaptive_weights.get_urgency_patterns() which handles reload internally) so
PriorityEngine no longer depends on private fields or redundant reload calls.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@backend/adaptive_weights.py`:
- Around line 43-48: The throttle in _check_reload uses the shared attribute
_last_check_time without synchronization; to make it thread-safe, introduce a
threading.Lock (e.g., self._reload_lock) and wrap the read/modify of
self._last_check_time and the conditional call to self._load_weights() inside a
with self._reload_lock: block; keep the 5-second semantics but perform the now =
time.time(), compare/update of self._last_check_time, and the call to
_load_weights() under the same lock to avoid races and redundant concurrent
reloads while preserving behavior in single-threaded use.

In `@backend/priority_engine.py`:
- Around line 25-29: The PriorityEngine currently accesses AdaptiveWeights
internals (_check_reload and _last_loaded) and redundantly calls reload; add a
public accessor on AdaptiveWeights named get_last_loaded_time() that returns the
reload timestamp, remove direct calls to _check_reload() and references to
_last_loaded in PriorityEngine, and change PriorityEngine to call
adaptive_weights.get_last_loaded_time() (and keep using
adaptive_weights.get_urgency_patterns() which handles reload internally) so
PriorityEngine no longer depends on private fields or redundant reload calls.

In `@benchmark_priority.py`:
- Line 10: The import adaptive_weights in benchmark_priority.py is unused;
either remove the unused import line ("from backend.adaptive_weights import
adaptive_weights") or, if it was intended for setup/verification, add the
missing usage (e.g., call adaptive_weights in the benchmark initialization or a
validation helper). Update the file to eliminate the unused import warning by
deleting the import or inserting the appropriate setup/verification call to
adaptive_weights.
- Line 4: The import statement "import re" is unused in this file; remove the
unused import line (the "import re" statement) from benchmark_priority.py to
clean up the module-level imports and avoid lint warnings.
- Around line 6-7: The code appends os.getcwd() to sys.path which makes imports
depend on the current working directory; replace that with the script's
directory (e.g., compute project_root from __file__ via
os.path.dirname(os.path.abspath(__file__)) or Path(__file__).resolve().parent)
and add it to sys.path (preferably sys.path.insert(0, ...)) so imports are based
on the script location rather than the process CWD; update the place where
sys.path.append(os.getcwd()) is used accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2517405f-e3ea-4362-9bf0-3cbecfd95c17

📥 Commits

Reviewing files that changed from the base of the PR and between 4e6c0b3 and 139a4c1.

📒 Files selected for processing (4)
  • .jules/bolt.md
  • backend/adaptive_weights.py
  • backend/priority_engine.py
  • benchmark_priority.py

- Fix missing dependencies in `backend/requirements-render.txt` (python-dotenv, async_lru, indic-nlp-library)
- Implement regex pre-compilation and caching in `PriorityEngine`
- Add 5-second throttle for `AdaptiveWeights` configuration reload
- Improve analysis throughput by ~33% (avg 0.09ms -> 0.06ms)
- Add benchmark script for performance verification
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
backend/requirements-render.txt (1)

18-24: Pin the newly added runtime packages in this deployment manifest.

In requirements-render.txt, python-dotenv, async_lru, and indic-nlp-library are left unpinned, which makes Render builds non-reproducible and can change behavior between deploys without code changes. Pin these to the versions validated with this PR.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/requirements-render.txt` around lines 18 - 24, The three runtime
packages left unpinned in requirements-render.txt (python-dotenv, async_lru,
indic-nlp-library) must be pinned to the exact versions validated with this PR;
update the entries for python-dotenv, async_lru, and indic-nlp-library to
include the specific version specifiers (e.g., ==x.y.z) that were tested in this
change so deploys are reproducible and match the PR-validated environment.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@backend/requirements-render.txt`:
- Line 23: Remove the unused deployment dependency "async_lru" from
requirements-render.txt: delete the "async_lru" entry from that manifest file,
verify there are no remaining imports/usages of async_lru in the backend
codebase (the review already notes backend/routers/detection.py and
backend/gemini_summary.py switched to manual caches), and run the test/deploy
validation to ensure no regression from removing the package.

---

Nitpick comments:
In `@backend/requirements-render.txt`:
- Around line 18-24: The three runtime packages left unpinned in
requirements-render.txt (python-dotenv, async_lru, indic-nlp-library) must be
pinned to the exact versions validated with this PR; update the entries for
python-dotenv, async_lru, and indic-nlp-library to include the specific version
specifiers (e.g., ==x.y.z) that were tested in this change so deploys are
reproducible and match the PR-validated environment.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 701e2c40-6b87-485a-b350-b6ac8375be60

📥 Commits

Reviewing files that changed from the base of the PR and between 139a4c1 and 44d1a41.

📒 Files selected for processing (2)
  • backend/requirements-render.txt
  • backend_output.txt
✅ Files skipped from review due to trivial changes (1)
  • backend_output.txt

pydub
googletrans==4.0.2
langdetect
async_lru
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify whether async_lru is actually imported or referenced anywhere in the backend.
rg -nP --type=py -C2 '^\s*(from\s+async_lru\s+import|import\s+async_lru\b|@alru_cache\b|alru_cache\s*\()' backend

# Show the deployment-related comments that currently claim async_lru was removed/replaced.
rg -n -C2 'async[-_ ]lru|manual cache|avoid .*Render|replace async_lru' backend

Repository: RohanExploit/VishwaGuru

Length of output: 1494


Remove async_lru from the Render manifest—it's not imported anywhere in the backend.

backend/routers/detection.py line 49 and backend/gemini_summary.py line 20 both document that the backend switched to manual caches specifically to avoid the async_lru dependency on Render. The rg search confirms async_lru is never imported or used in any backend file, making this an unused deployment-time dependency that contradicts the stated workaround.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@backend/requirements-render.txt` at line 23, Remove the unused deployment
dependency "async_lru" from requirements-render.txt: delete the "async_lru"
entry from that manifest file, verify there are no remaining imports/usages of
async_lru in the backend codebase (the review already notes
backend/routers/detection.py and backend/gemini_summary.py switched to manual
caches), and run the test/deploy validation to ensure no regression from
removing the package.

- Fix missing dependencies in `backend/requirements-render.txt` (`python-dotenv`, `async_lru`, `indic-nlp-library`)
- Update `googletrans` to 4.0.2 for better `httpx` compatibility
- Fix `PriorityEngine` regex cache initialization and reload trigger
- Implement 5-second throttle for `AdaptiveWeights` configuration reload
- Improve analysis throughput by ~33% (avg 0.09ms -> 0.06ms)
- Add benchmark script for performance verification
@github-actions
Copy link

github-actions bot commented Mar 9, 2026

🔍 Quality Reminder

Thanks for the updates! Please ensure:
- Your changes don't break existing functionality
- All tests still pass
- Code quality standards are maintained

*The maintainers will verify that the overall project flow remains intact.*

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="backend_output.txt">

<violation number="1" location="backend_output.txt:1">
P3: This change commits nondeterministic runtime log output (timestamps/PIDs), which creates noisy diffs and makes meaningful changes harder to review. Prefer removing this artifact from source control or normalizing dynamic fields in the tracked output.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

2026-03-09 02:00:56,311 - backend.adaptive_weights - INFO - Adaptive weights loaded/reloaded.
WARNING: StatReload detected changes in 'backend\main.py'. Reloading...

No newline at end of file
2026-03-09 15:06:36,764 - backend.adaptive_weights - INFO - Adaptive weights loaded/reloaded.
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 9, 2026

Choose a reason for hiding this comment

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

P3: This change commits nondeterministic runtime log output (timestamps/PIDs), which creates noisy diffs and makes meaningful changes harder to review. Prefer removing this artifact from source control or normalizing dynamic fields in the tracked output.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At backend_output.txt, line 1:

<comment>This change commits nondeterministic runtime log output (timestamps/PIDs), which creates noisy diffs and makes meaningful changes harder to review. Prefer removing this artifact from source control or normalizing dynamic fields in the tracked output.</comment>

<file context>
@@ -1,13 +1,13 @@
-2026-03-09 14:34:15,761 - backend.adaptive_weights - INFO - Adaptive weights loaded/reloaded.
-2026-03-09 14:34:15,793 - backend.rag_service - INFO - Loaded 5 civic policies for RAG.
+2026-03-09 15:06:36,764 - backend.adaptive_weights - INFO - Adaptive weights loaded/reloaded.
+2026-03-09 15:06:36,834 - backend.rag_service - INFO - Loaded 5 civic policies for RAG.
 /home/jules/.pyenv/versions/3.12.12/lib/python3.12/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
</file context>
Fix with Cubic

- Fix missing dependencies in `backend/requirements-render.txt` (`python-dotenv`, `async_lru`, `indic-nlp-library`)
- Update `googletrans` to 4.0.0-rc1 for better `httpx` compatibility
- Fix `PriorityEngine` regex cache initialization and reload trigger
- Implement 5-second throttle for `AdaptiveWeights` configuration reload
- Improve analysis throughput by ~33% (avg 0.09ms -> 0.06ms)
- Add benchmark script for performance verification
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