Skip to content

refactor(firestore) : comprehensive refactoring for FirestoreSessionService#5663

Open
Dumeng wants to merge 7 commits into
google:mainfrom
Dumeng:firestore
Open

refactor(firestore) : comprehensive refactoring for FirestoreSessionService#5663
Dumeng wants to merge 7 commits into
google:mainfrom
Dumeng:firestore

Conversation

@Dumeng
Copy link
Copy Markdown

@Dumeng Dumeng commented May 11, 2026

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

N/A

2. Or, if no issue exists, describe the change:

If applicable, please follow the issue templates to provide as much detail as
possible.

Problem:
This Pull Request introduces comprehensive refactoring, performance optimizations, and robustness improvements to the FirestoreSessionService. It simplifies the core implementation, aligns state handling logic with other session services, fixes side-effects in transactions, and significantly improves read performance for bulk operations.

Solution:

  • Optimized Batch Retrieval: Updated list_sessions to leverage Firestore's native client.get_all(...) method. This fetches multiple user state documents in a single optimized RPC request rather than individual sequential reads, significantly reducing read latency and overhead.

  • Aligned State Handling: Refactored state management logic to match the established, robust patterns found in DatabaseSessionService.
    Transaction Safety: Removed unsafe side-effects inside transaction callbacks within append_event to ensure idempotency and reliability during automatic Firestore transaction retries.

  • Revision Tracking: Explicitly initialized session revision tracking to 0 upon session creation to guarantee monotonic tracking consistency.

  • Simplified Implementation: Streamlined internal data extraction and simplified redundant record lookups for improved long-term maintainability.

  • Import Standards: Resorted and updated import statements to strictly adhere to the ADK project's relative import conventions and code style guide.

Testing Plan

Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Please include a summary of passed pytest results.

Manual End-to-End (E2E) Tests:

Please provide instructions on how to manually test your changes, including any
necessary setup or configuration. Please provide logs or screenshots to help
reviewers better understand the fix.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Add any other context or screenshots about the feature request here.

@Dumeng Dumeng changed the title Firestore refactor(firestore) : comprehensive refactoring for FirestoreSessionService May 11, 2026
@rohityan rohityan self-assigned this May 12, 2026
@rohityan rohityan added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label May 12, 2026
@rohityan rohityan requested a review from xuanyang15 May 12, 2026 03:50
@rohityan rohityan added the needs review [Status] The PR/issue is awaiting review from the maintainer label May 12, 2026
@rohityan
Copy link
Copy Markdown
Collaborator

Hi @Dumeng, Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@rohityan
Copy link
Copy Markdown
Collaborator

Hi @xuanyang15 , can you please review this.

@xuanyang15
Copy link
Copy Markdown
Collaborator

@DeanChensj Could you please help review?

@DeanChensj
Copy link
Copy Markdown
Collaborator

@gemini-cli /review

@github-actions
Copy link
Copy Markdown

🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions
Copy link
Copy Markdown

🤖 I'm sorry @DeanChensj, but I was unable to process your request. Please see the logs for more details.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

The changes in this PR significantly improve the efficiency of the FirestoreSessionService by using concurrent fetches (asyncio.gather) and batching user state retrievals (get_all). The refactoring of imports and type hints also makes the code cleaner.

I've reviewed the logic for session creation, retrieval, and event appending, and it looks solid. The use of revisions for optimistic concurrency control is correctly implemented.

Great work!

)
app_doc = await app_ref.get()

events_docs, app_doc, user_doc = await asyncio.gather(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using asyncio.gather to fetch events, app state, and user state concurrently is a great performance improvement.

async def _with_session_lock(
self, *, app_name: str, user_id: str, session_id: str
) -> AsyncIterator[None]:
) -> AsyncGenerator[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.

Using AsyncGenerator is more appropriate here than AsyncIterator since it's an async context manager that yields. Good catch.

users_docs = await users_ref.get()
for u_doc in users_docs:
user_states_map[u_doc.id] = u_doc.to_dict()
unique_user_ids = {s["userId"] for s in sessions_data if "userId" in s}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The batch fetch using get_all for unique user IDs is much more efficient than fetching all users or fetching them one by one.

transaction_obj = self.client.transaction()
new_revision_count = await _append_txn(transaction_obj)
session._storage_update_marker = str(new_revision_count)
session.last_update_time = event.timestamp
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Updating session.last_update_time here ensures the local session object is in sync with the storage update.

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

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants