Skip to content

feat(functions): Add FunctionScope and update task_queue to support kit scopes#966

Open
inlined wants to merge 6 commits into
firebase:mainfrom
inlined:antigravity-task-queue-scopes
Open

feat(functions): Add FunctionScope and update task_queue to support kit scopes#966
inlined wants to merge 6 commits into
firebase:mainfrom
inlined:antigravity-task-queue-scopes

Conversation

@inlined

@inlined inlined commented Jul 10, 2026

Copy link
Copy Markdown
Member

Explanation

This PR introduces support for explicit task queue scopes to match the design patterns introduced in firebase-admin-node#3210.

It adds the new FunctionScope class and updates the task_queue signature to accept a scope parameter (while deprecating extension_id). It also implements an automatic, rollback-safe fallback retry flow that shifts queue targets from extensions to kits upon receiving a 404 Not Found response.

Key Changes:

  • FunctionScope: Introduced a validation-enforced scope type representing 'current', 'global', 'extension', and 'kit'.
  • task_queue update: Added the scope parameter, deprecated extension_id, and bound warnings appropriately.
  • Rollback-safe Fallback Flow: Refactored enqueue and delete methods to attempt a recursive kit-retry if a hybrid scope hits a 404. If the retry fails, the scope change is safely rolled back to prevent state contamination, and the fallback warning is only logged on successful execution.

Testing Strategy

  • Modified existing unit tests to match the dynamic scope architecture.
  • Added new unit test suite (TestFunctionScope) covering constructor validations and bounds.
  • Added unit tests for environmental configurations (e.g., KIT_INSTANCE_ID).
  • Added robust fallback retry tests using MockMultiRequestAdapter verifying that 404 errors triggers fallbacks, that successful retries log user warnings, and that failures rollback scope changes.
  • Project-wide linter rate is verified at 10.00/10.

…it scopes

Added FunctionScope class, updated task_queue signature, and refactored TaskQueue to implement rollback-safe recursive fallback targeting kit queue when hitting a 404 response.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the FunctionScope class to manage task queue scopes ('current', 'global', 'extension', and 'kit') and deprecates the extension_id parameter in favor of scope. Feedback highlights critical compatibility issues with Python versions older than 3.9 due to the use of tuple[...] type hinting, a bug in the delete method's error handling that silently swallows 404 errors and breaks scope rollback logic, and thread-safety concerns from mutating self._scope concurrently. Additionally, a test case asserting incorrect 404-swallowing behavior needs to be updated.

Comment thread firebase_admin/functions.py Outdated
Comment thread firebase_admin/functions.py Outdated
Comment thread firebase_admin/functions.py Outdated
Comment thread firebase_admin/functions.py Outdated
Comment thread tests/test_functions.py Outdated
inlined added 3 commits July 9, 2026 18:01
Factored out payload and HTTP request preparation in TaskQueue.enqueue and TaskQueue.delete into private helper methods _enqueue_with_scope and _delete_with_scope. This avoids recursion, class state pollution, and rollback logic, keeping self._scope unmodified until the request successfully completes.
Updated delete to raise NotFoundError on 404 response rather than silently swallowing it, preserving original SDK behavior. Added and updated tests to assert 404 propagation and rollback.
… and ensure complete thread safety without locks

Imported Tuple from typing and updated _resolve_resource signature to support Python versions older than 3.9. Restored the accidentally removed handle_functions_error method body. Removed self._scope mutation entirely to ensure complete thread safety without locks, using a thread-safe global set _WARNED_INSTANCES to ensure each warning is only logged once.
@inlined

inlined commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the FunctionScope class to define the scope of a function in a task queue, deprecating the extension_id parameter in task_queue in favor of a more flexible scope parameter. It also implements a fallback mechanism to retry with kit scope on 404 errors and adds corresponding unit tests. The review feedback suggests supporting EXT_INSTANCE_ID in the current scope for consistency with other SDKs, and updating the deprecation warning message to recommend functions.FunctionScope to avoid potential NameErrors.

Comment thread firebase_admin/functions.py
Comment thread firebase_admin/functions.py
inlined added 2 commits July 10, 2026 08:02
…NCE_ID

Renamed the KIT_INSTANCE_ID environment variable to FIREBASE_KIT_INSTANCE_ID in both functions.py and tests.
…fallback warning suggestion

Updated _resolve_resource to support resolving current scope using EXT_INSTANCE_ID (checking it first before falling back to FIREBASE_KIT_INSTANCE_ID) for completeness and consistency. Updated _log_fallback_warning message to suggest the more idiomatic functions.FunctionScope.
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.

1 participant