Skip to content

Conversation

Swiftyos
Copy link
Contributor

@Swiftyos Swiftyos commented Sep 24, 2025

Summary

  • Extends the caching implementation from Part 1 to cover all GET endpoints across the platform
  • Separates cache logic into dedicated cache.py files for better code organization and maintainability
  • Implements intelligent cache invalidation on all mutation operations (CREATE/UPDATE/DELETE)

Changes

New Cache Modules

  • backend/server/routers/cache.py - V1 API endpoint caching
  • backend/server/v2/library/cache.py - Library API caching
  • backend/server/v2/store/cache.py - Store API caching (refactored from routes)

Cached Endpoints

  • Graphs: List and details (15-30 min TTL)
  • Executions: Graph executions (5 min TTL)
  • User Data: Preferences and timezone (30-60 min TTL)
  • Library: Agents, favorites, and presets (10-30 min TTL)
  • Store: Listings and profiles (5-60 min TTL)

Key Features

  • Selective caching only for default queries (bypasses cache for filtered/searched results)
  • Proper cache invalidation using positional arguments to match function calls
  • Configurable TTLs based on data volatility
  • Built-in thundering herd protection via the @cached decorator

Test Plan

  • All 20 cache-specific tests passing
  • Cache hit/miss behavior validated
  • Cache invalidation on mutations verified
  • Integration tests confirm proper operation
  • Manual testing of affected endpoints

Performance Impact

  • Reduces database load for frequently accessed data
  • Improves response times for cached endpoints
  • Maintains data consistency through intelligent invalidation

Related

…oints (Part 2)

- Created separate cache.py modules for better code organization
  - backend/server/routers/cache.py for V1 API endpoints
  - backend/server/v2/library/cache.py for library endpoints
  - backend/server/v2/store/cache.py (refactored from routes)

- Added caching to all major GET endpoints:
  - Graphs list/details with 15-30 min TTL
  - Graph executions with 5 min TTL
  - User preferences/timezone with 30-60 min TTL
  - Library agents/favorites/presets with 10-30 min TTL
  - Store listings/profiles with 5-60 min TTL

- Implemented intelligent cache invalidation:
  - Clears relevant caches on CREATE/UPDATE/DELETE operations
  - Uses positional arguments for cache_delete to match function calls
  - Selective caching only for default queries (bypasses cache for filtered/searched results)

- Added comprehensive test coverage:
  - 20 cache-specific tests all passing
  - Validates cache hit/miss behavior
  - Verifies invalidation on mutations

- Performance improvements:
  - Reduces database load for frequently accessed data
  - Built-in thundering herd protection via @cached decorator
  - Configurable TTLs based on data volatility
@Swiftyos Swiftyos requested a review from a team as a code owner September 24, 2025 14:24
@Swiftyos Swiftyos requested review from Pwuts and kcze and removed request for a team September 24, 2025 14:24
@github-project-automation github-project-automation bot moved this to 🆕 Needs initial review in AutoGPT development kanban Sep 24, 2025
Copy link

netlify bot commented Sep 24, 2025

Deploy Preview for auto-gpt-docs-dev canceled.

Name Link
🔨 Latest commit 045009a
🔍 Latest deploy log https://app.netlify.com/projects/auto-gpt-docs-dev/deploys/68e017b1a55e1b0008183f2f

@github-actions github-actions bot added the platform/backend AutoGPT Platform - Back end label Sep 24, 2025
Copy link

netlify bot commented Sep 24, 2025

Deploy Preview for auto-gpt-docs canceled.

Name Link
🔨 Latest commit 045009a
🔍 Latest deploy log https://app.netlify.com/projects/auto-gpt-docs/deploys/68e017b1aa078d00087b48de

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Behavior Change

The list graphs endpoint previously passed a 'filter_by="active"' argument to the DB call; the new cached path omits this filter. Confirm parity with previous behavior or reintroduce the filter to avoid returning inactive graphs.

async def list_graphs(
    user_id: Annotated[str, Security(get_user_id)],
) -> Sequence[graph_db.GraphMeta]:
    paginated_result = await cache.get_cached_graphs(
        user_id=user_id,
        page=1,
        page_size=250,
    )
    return paginated_result.graphs
Export Path Bypass

The cached get_graph path does not pass include_subgraphs/for_export flags. Ensure non-export requests do not require subgraphs; otherwise, some consumers may get incomplete graph data compared to the previous always-include_subgraphs implementation.

async def get_graph(
    graph_id: str,
    user_id: Annotated[str, Security(get_user_id)],
    version: int | None = None,
    for_export: bool = False,
) -> graph_db.GraphModel:
    # Use cache for non-export requests
    if not for_export:
        graph = await cache.get_cached_graph(
            graph_id=graph_id,
            version=version,
            user_id=user_id,
        )
    else:
        graph = await graph_db.get_graph(
            graph_id,
            version,
            user_id=user_id,
            for_export=for_export,
            include_subgraphs=True,  # needed to construct full credentials input schema
        )
    if not graph:
        raise HTTPException(status_code=404, detail=f"Graph #{graph_id} not found.")
    return graph
Cache Selectivity

Caching for list_library_agents only applies when sort_by is UPDATED_AT and no search term. Consider also excluding non-default page_size or page to ensure TTL assumptions; validate that ordering defaults are identical to DB defaults to prevent subtle mismatches.

Returns:
    A LibraryAgentResponse containing agents and pagination metadata.

Raises:
    HTTPException: If a server/database error occurs.
"""
try:
    # Use cache for default queries (no search term, default sort)
    if search_term is None and sort_by == library_model.LibraryAgentSort.UPDATED_AT:
        return await library_cache.get_cached_library_agents(
            user_id=user_id,
            page=page,
            page_size=page_size,
        )
    else:
        # Direct DB query for searches and custom sorts
        return await library_db.list_library_agents(
            user_id=user_id,
            search_term=search_term,
            sort_by=sort_by,
            page=page,
            page_size=page_size,
        )
except Exception as e:

Copy link

deepsource-io bot commented Sep 24, 2025

Here's the code health analysis summary for commits 8b4eb6f..045009a. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource JavaScript LogoJavaScript✅ Success
❗ 1 occurence introduced
View Check ↗
DeepSource Python LogoPython✅ Success
❗ 41 occurences introduced
🎯 1 occurence resolved
View Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

…stead of db

The test was failing because routes now use cached functions. Updated the mock
to patch the cache function which is what the route actually calls.
@Swiftyos Swiftyos requested a review from Copilot September 24, 2025 14:40
Copy link
Contributor

@Copilot 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 comprehensive caching for all GET endpoints across the AutoGPT platform by separating cache logic into dedicated modules for better maintainability. The changes extend the caching implementation to cover graphs, executions, user data, library operations, and store listings with configurable TTLs based on data volatility.

Key changes:

  • Extracts cache functions into separate modules (cache.py files) from route handlers
  • Implements intelligent cache invalidation on all CREATE/UPDATE/DELETE operations
  • Adds comprehensive test coverage for cache behavior and invalidation patterns

Reviewed Changes

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

Show a summary per file
File Description
autogpt_platform/backend/backend/server/v2/store/routes.py Removes cache functions and imports them from new cache module
autogpt_platform/backend/backend/server/v2/store/cache.py New cache module for Store API with all caching functions
autogpt_platform/backend/backend/server/v2/library/routes_test.py Updates test to mock cache functions instead of DB functions
autogpt_platform/backend/backend/server/v2/library/routes/presets.py Integrates caching for preset operations with cache invalidation
autogpt_platform/backend/backend/server/v2/library/routes/agents.py Integrates caching for agent operations with cache invalidation
autogpt_platform/backend/backend/server/v2/library/cache_test.py New comprehensive test suite for library cache invalidation
autogpt_platform/backend/backend/server/v2/library/cache.py New cache module for Library API with all caching functions
autogpt_platform/backend/backend/server/routers/v1.py Integrates caching for V1 API endpoints with cache invalidation
autogpt_platform/backend/backend/server/routers/cache_test.py New comprehensive test suite for V1 API cache invalidation
autogpt_platform/backend/backend/server/routers/cache.py New cache module for V1 API with all caching functions

The cached graph function was missing include_subgraphs=True parameter which
is needed to construct full credentials input schema. This was causing
test_access_store_listing_graph to fail.
… permissions

When a graph is not found/accessible, we now clear the cache entry rather than
caching the None result. This prevents issues with store listing permissions
where a graph becomes accessible after approval but the cache still returns
the old 'not found' result.
@Swiftyos
Copy link
Contributor Author

unfavouriting a library agent is not working

Copy link
Contributor

This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request.

@github-actions github-actions bot added the platform/frontend AutoGPT Platform - Front end label Sep 29, 2025
@Significant-Gravitas Significant-Gravitas deleted a comment from coderabbitai bot Sep 29, 2025
@Significant-Gravitas Significant-Gravitas deleted a comment from coderabbitai bot Oct 1, 2025
@Significant-Gravitas Significant-Gravitas deleted a comment from coderabbitai bot Oct 1, 2025
Copy link

coderabbitai bot commented Oct 1, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch swiftyos/caching-pt2

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

majdyz
majdyz previously approved these changes Oct 2, 2025
merged_node_input = preset.inputs | inputs
merged_credential_inputs = preset.credentials | credential_inputs

for page in range(1, 10):
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't. get when you remove 10 pages and remove 5 pages for others, what's the factor?

result = await db.create_preset_from_graph_execution(user_id, preset)

# Clear presets list cache after creating new preset
for page in range(1, 5):
Copy link
Contributor

Choose a reason for hiding this comment

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

at this point, you can make a helper out of this :D to remove pages.

library_cache.get_cached_library_agent.cache_delete(
library_agent_id=library_agent_id, user_id=user_id
)
for page in range(1, 20):
Copy link
Contributor

Choose a reason for hiding this comment

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

here it's 20 :o

@github-project-automation github-project-automation bot moved this from 🆕 Needs initial review to 👍🏼 Mergeable in AutoGPT development kanban Oct 2, 2025
majdyz
majdyz previously approved these changes Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform/backend AutoGPT Platform - Back end platform/frontend AutoGPT Platform - Front end Review effort 3/5 size/xl
Projects
Status: 👍🏼 Mergeable
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants