Skip to content

Conversation

@aneesafatima
Copy link
Contributor

@aneesafatima aneesafatima commented Jan 6, 2026

Closes #132

📝 Description

This PR does a small refactor around the cache update flow.

Initially, I misunderstood the purpose of the operation parameter and thought different logic was missing for create / update / delete. After studying the cache system more thoroughly, I realized the current behavior is intentional and there was no underlying issue.

Based on that, this PR does not change how the cache works. It only centralizes the repeated cache refresh logic into a separate helper method and improves logging/doc clarity so it’s clearer that operation is informational and does not affect behavior.

🔧 Changes Made

  • Centralized repeated cache refresh logic into a separate helper method
  • Improved log strings to clearly indicate that operation is informational
  • No functional or behavioral changes to cache updates

📷 Screenshots or Visual Changes (if applicable)

N/A (backend-only refactor)

🤝 Collaboration

Collaborated with: N/A

✅ Checklist

  • I have read the contributing guidelines.
  • I have added tests that prove my fix is effective or that my feature works.
    (No functional changes were made)
  • I have added necessary documentation (if applicable).
  • Any dependent changes have been merged and published in downstream modules.

Summary by CodeRabbit

  • Refactor

    • Centralized per-data-type cache update handling to streamline and standardize updates.
  • Bug Fixes

    • Ensured cache persistence, timestamp updates, and cleanup consistently run after successful updates.
    • Added explicit handling/logging when no existing cache is found and a full context is built.
  • Style

    • Minor formatting and newline consistency fixes.
  • Docs

    • Improved informational logging for cache build and update operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 6, 2026

📝 Walkthrough

Walkthrough

Added a per-datatype cache update handler and refactored update_cache to delegate create/update/delete operations to it; preserved full-context build when no cache exists and added logging and cleanup/persistence steps. Minor formatting fix in profile route EOF/newline.

Changes

Cohort / File(s) Summary
Cache update refactor
Backend/agent/cache.py
Added _cache_update_handler(self, datatype: str, current_cache: dict) -> bool and replaced inlined per-datatype update blocks in update_cache() with calls to this handler for create/update/delete. When no cache exists, update_cache() builds full context and persists it. On successful handler updates, last_updated is set, memory/disk caches persisted, and cleanup/sync triggered. Minor whitespace/logging adjustments and _save_cache signature spacing changed.
Formatting correction
Backend/routes/profile.py
Trailing-newline/whitespace correction in the sqlite3.OperationalError branch of update_profile(); no behavioral change.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Caller
    participant Update as update_cache()
    participant Handler as _cache_update_handler()
    participant Persist as Cache Persistence
    participant Cleanup as Cleanup/Sync

    Caller->>Update: call update_cache(user_id, datatype, operation)
    alt No existing cache
        Update->>Update: build_full_context()
        Update->>Persist: _save_cache(user_id, context_data)
        Update-->>Caller: return (early)
    else Cache exists
        Update->>Update: validate inputs & operation
        alt operation in [create, update, delete]
            Update->>Handler: _cache_update_handler(datatype, current_cache)
            Handler->>Handler: validate datatype & apply per-datatype changes
            Handler-->>Update: success / failure
            alt success
                Update->>Update: update last_updated
                Update->>Persist: persist memory and disk caches
                Update->>Cleanup: run cleanup & sync
                Update-->>Caller: return success
            else failure
                Update-->>Caller: return failure
            end
        else
            Update-->>Caller: no-op / unchanged
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through bytes and found a trail,

nibbled stale lines and left a bright new tail.
Create, update, delete — I sorted the heap,
now caches sleep sound and wake without a peep. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR addresses issue #132 but implements a refactoring instead of the requested feature. The PR centralizes logic into _cache_update_handler but does not implement operation-specific updates for create/update/delete as required by issue #132. Implement conditional logic in _cache_update_handler or update_cache to handle create/update/delete operations differently, adding incremental update logic instead of full refreshes for these operation types.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main refactoring - moving cache update logic into a helper method (_cache_update_handler), matching the primary change in the code.
Out of Scope Changes check ✅ Passed Changes are primarily in-scope refactoring of cache logic with minor formatting adjustments. The formatting-only change in Backend/routes/profile.py (newline handling) is a minor housekeeping change that does not impact functionality.
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

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

@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: 2

🤖 Fix all issues with AI Agents
In @Backend/agent/cache.py:
- Around line 320-326: The debug print exposes sensitive PII by logging full
context_data from _build_context; remove or replace the detailed print("✅ Full
context built", context_data) with a non-sensitive confirmation (e.g., a success
message or sanitized summary) and ensure any logging before calling
self.memory_cache[user_id] or self._save_cache(user_id, context_data) never
includes raw context_data; if needed implement a redaction/summarize helper that
strips PHI/PII and call that helper for logs instead.
- Around line 59-76: The function _cache_update_handler has a code path where
_get_specific_data(datatype) returns falsy and the function falls through
without returning a bool; update _cache_update_handler to explicitly return
False when data is falsy (e.g., after calling self._get_specific_data(datatype)
check if not data and return False), preserving existing behavior for profile vs
tracking_data updates and keeping the method signature -> bool.
🧹 Nitpick comments (1)
Backend/agent/cache.py (1)

328-338: LGTM with minor style nit.

The cache update logic correctly delegates to the handler and maintains cache consistency.

Minor nit: extra space before : on line 328 (["delete"] :).

🔎 Optional style fix
-            if operation in ["update", "create", "delete"] :
+            if operation in ["update", "create", "delete"]:
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c2d9f82 and b48382d.

📒 Files selected for processing (3)
  • Backend/agent/cache.py
  • Backend/db/chromadb/chroma.sqlite3
  • Backend/routes/profile.py
🧰 Additional context used
🪛 Ruff (0.14.10)
Backend/agent/cache.py

56-56: Do not catch blind exception: Exception

(BLE001)

🔇 Additional comments (3)
Backend/routes/profile.py (2)

117-117: LGTM!

The formatting change is acceptable. This is a minor whitespace adjustment with no functional impact.


104-107: Pre-existing bug: SQL parameter mismatch will cause runtime error.

This is not introduced by the current PR, but the UPDATE statement specifies only 2 columns (due_date, user_location) while providing 6 parameters. This will raise a sqlite3.ProgrammingError at runtime.

Consider fixing this in a follow-up or as part of this PR if updating profile functionality is being tested:

🔎 Suggested fix
         db.execute(
-            'UPDATE profile SET due_date = ?, user_location = ?',
-            (lmp, cycle_length, period_length, age, weight, location)    
+            'UPDATE profile SET lmp = ?, cycleLength = ?, periodLength = ?, age = ?, weight = ?, user_location = ?, dueDate = ?',
+            (lmp, cycle_length, period_length, age, weight, location, calculate_due_date(lmp, cycle_length))
         )
Backend/agent/cache.py (1)

50-58: LGTM!

Minor formatting adjustments to the method signature and blank line are acceptable.

Copy link
Contributor

@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: 2

🤖 Fix all issues with AI Agents
In @Backend/agent/cache.py:
- Line 50: The parameter type annotations in the function signature for
_save_cache (and the other function in this file that has a Dict[str, Any]
parameter) lack a space after the colon; update the function signatures so each
parameter annotation uses a single space after the colon (e.g., change
"param:type" to "param: type") for _save_cache and the corresponding function on
line 59 to comply with PEP 8.
- Around line 321-327: The debug print is exposing PII by logging the full
context_data after _build_context; remove or redact the sensitive payload
instead of printing context_data directly in the block that populates
memory_cache and calls _save_cache (the print("✅ Full context built",
context_data) line). Replace it with a non-sensitive message (e.g., log only
that the context was built for user_id, the number of items, or a list of
top-level keys), or mask sensitive fields before any logging; keep memory_cache,
_save_cache, _build_context, user_id and context_data usage unchanged otherwise.
🧹 Nitpick comments (2)
Backend/agent/cache.py (2)

298-300: Expand docstring to explain design rationale.

The note states that the operation parameter is "purely informational," but doesn't explain why operation-specific incremental updates (as requested in issue #132) aren't implemented. Consider adding a brief explanation of the design decision to help future maintainers understand the trade-offs.

🔎 Suggested enhancement
-            Note: The operation parameter is purely informational. The cache update logic is the same regardless of operation. 
+            Note: The operation parameter is purely informational. The cache update logic performs a full refresh
+                  of the affected datatype regardless of operation. This design trades incremental update complexity
+                  for simpler, more reliable cache consistency with bounded data sizes (max_tracking_entries).

329-339: Cache update logic successfully centralized.

The refactored code effectively extracts the repeated cache update logic into _cache_update_handler, improving maintainability. The flow correctly updates the timestamp, persists to memory and disk, and triggers cleanup.

💡 Optional simplification

Since all three operations (create, update, delete) execute identical logic, consider simplifying the condition:

-            if operation in ["update", "create", "delete"] :
+            if operation and data_type:

This makes it clear that any operation with a valid data_type will trigger the cache update, and removes the need to maintain an explicit list of operation types.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b48382d and 67d6fdb.

📒 Files selected for processing (1)
  • Backend/agent/cache.py
🧰 Additional context used
🪛 Ruff (0.14.10)
Backend/agent/cache.py

56-56: Do not catch blind exception: Exception

(BLE001)

🔇 Additional comments (1)
Backend/agent/cache.py (1)

59-77: Verify that the full refresh behavior aligns with issue #132 requirements.

The _cache_update_handler method fetches all data from the database for the given datatype, regardless of the operation (create/update/delete). The operation parameter is explicitly documented as "purely informational" and the cache update logic is identical for all operation types.

However, issue #132 could not be located in the repository to verify whether its requirements have been met by this approach. Confirm whether issue #132 originally required incremental updates (appending/updating/deleting specific records) or if full refreshes are the intended solution.

continue

def _save_cache(self, user_id: str, context_data: Dict[str, Any]):
def _save_cache(self, user_id:str, context_data: Dict[str, Any]):
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Fix type annotation spacing to comply with PEP 8.

Type annotations should have a space after the colon. Lines 50 and 59 have inconsistent spacing in parameter type hints.

🔎 Proposed fix
-    def _save_cache(self, user_id:str, context_data: Dict[str, Any]):
+    def _save_cache(self, user_id: str, context_data: Dict[str, Any]):
-    def _cache_update_handler(self, datatype:str, current_cache:dict) -> bool:
+    def _cache_update_handler(self, datatype: str, current_cache: dict) -> bool:

Also applies to: 59-59

🤖 Prompt for AI Agents
In @Backend/agent/cache.py at line 50, The parameter type annotations in the
function signature for _save_cache (and the other function in this file that has
a Dict[str, Any] parameter) lack a space after the colon; update the function
signatures so each parameter annotation uses a single space after the colon
(e.g., change "param:type" to "param: type") for _save_cache and the
corresponding function on line 59 to comply with PEP 8.

Copy link
Contributor

@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: 2

🤖 Fix all issues with AI Agents
In @Backend/agent/cache.py:
- Around line 298-300: The docstring incorrectly states the operation parameter
is purely informational; update the cache update function that accepts the
operation parameter (e.g., update_cache / cache_update_handler) to implement
operation-specific behavior per issue #132: when operation == "create" append
new records to the existing cache, when operation == "update" merge/replace
matching records by key/ID in the cache, when operation == "delete" remove
matching records from the cache, and only fall back to a full refresh if the
incremental action cannot be performed; also remove or correct the misleading
note in the docstring that claimed all operations are treated the same.
- Around line 328-338: The code currently only runs updates when operation is in
["update","create","delete"] and otherwise silently skips; add an else branch
after that condition to log/warn (e.g., via print or logger) that an unexpected
or empty operation was received for user_id and data_type so skips are visible;
additionally refactor the logic so _cache_update_handler (or create three
specific helpers like _cache_create_handler, _cache_modify_handler,
_cache_delete_handler) implements operation-specific behavior instead of always
doing a full refresh via _get_specific_data — for "create" append new records
returned by _get_specific_data, for "update" find and modify matching records in
current_cache, and for "delete" remove matching records from current_cache, then
update last_updated, memory_cache[user_id], call _save_cache(user_id,
current_cache) and _check_and_cleanup_cache(user_id) only when changes were
actually applied.
🧹 Nitpick comments (1)
Backend/agent/cache.py (1)

328-328: Remove trailing space before colon.

Line 328 has a space before the colon, which is inconsistent with Python style conventions.

🔎 Proposed fix
-            if operation in ["update", "create", "delete"] :
+            if operation in ["update", "create", "delete"]:
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 67d6fdb and 093ad26.

📒 Files selected for processing (1)
  • Backend/agent/cache.py
🧰 Additional context used
🪛 Ruff (0.14.10)
Backend/agent/cache.py

56-56: Do not catch blind exception: Exception

(BLE001)

🔇 Additional comments (2)
Backend/agent/cache.py (2)

319-327: Verify early return behavior when no cache exists.

The added return statement on line 326 causes the function to exit after building full context for users without existing cache, preventing the operation-based update logic (lines 328-338) from executing. While this may be intentional (full context build makes incremental updates unnecessary), it's a behavioral change that should be confirmed.

Previously, the code would fall through to the operation handling below, but now it short-circuits. Ensure this aligns with the intended cache update flow.


59-77: Cache update handler logic is correct but doesn't implement incremental updates.

The _cache_update_handler method successfully centralizes cache update logic and properly returns False when data fetch fails (addressing a past review comment). The implementation correctly differentiates between profile updates (root-level) and tracking data updates (nested).

However, this refactor maintains the existing full-refresh behavior via _get_specific_data, rather than implementing the incremental update logic (create = append, update = modify specific, delete = remove) requested in issue #132. If the full-refresh approach is intentional, consider updating or closing issue #132 to reflect this architectural decision.

print(f" 🔄 Updating weight data...")
weight_data = self._get_specific_data("weight")
if weight_data is not None:
current_cache["tracking_data"]["weight"] = weight_data
Copy link
Contributor

Choose a reason for hiding this comment

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

why are these functions removed completely? where is the alternative approach

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A new method called cache_update_handler has been added to handle the update of all datatypes in one function. This helps prevent the repetition of code.
image

@bhavik-mangla
Copy link
Contributor

this looks good, Pls include testing going onwards
(examples of cache updating.)

@bhavik-mangla bhavik-mangla merged commit 8893922 into AOSSIE-Org:main Jan 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEATURE REQUEST: Implement operation-specific logic in update_cache() method

2 participants