Skip to content

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

Open
inlined wants to merge 7 commits into
mainfrom
antigravity-task-queue-scopes
Open

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

Conversation

@inlined

@inlined inlined commented Jul 13, 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 fallback retry flow that shifts queue targets from extensions to kits upon receiving a 404 Not Found response.

To ensure complete thread safety without requiring locks, the TaskQueue class is stateless/immutable, delegating requests to private helper functions _enqueue_with_scope and _delete_with_scope that accept the resolved scope as a parameter.

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.
  • Stateless Fallback Flow: Refactored enqueue and delete methods to use private helper functions. If a hybrid scope ('extension_or_kit') hits a 404, it retries targeting the kit scope. If the fallback kit queue deletes fail with 404, the NotFoundError exception is correctly propagated back to the user (preserving original Python SDK behavior).
  • Environment Variable Rename & Support: Renamed environment variable KIT_INSTANCE_ID to FIREBASE_KIT_INSTANCE_ID and added support for EXT_INSTANCE_ID in current scope (with extension taking precedence).
  • Type Compatibility: Added Tuple import from typing and updated signatures to support Python versions older than 3.9 (e.g. 3.7 and 3.8).

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 (FIREBASE_KIT_INSTANCE_ID, EXT_INSTANCE_ID and precedence).
  • Added robust fallback retry tests using MockMultiRequestAdapter verifying that 404 errors triggers fallbacks, that successful retries log user warnings, and that failures propagate properly.
  • Project-wide linter rate is verified at 10.00/10.
  • Checked and verified that all 3908 unit tests pass successfully.

inlined added 6 commits July 9, 2026 17:53
…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.
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.
…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.

@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 function scopes in task queues, replacing the deprecated extension_id parameter in task_queue with a new scope parameter. It also implements fallback logic to retry with 'kit' scope upon receiving a 404 error for 'extension_or_kit' scopes, and adds comprehensive unit tests. The feedback suggests refactoring the error handling in the delete method to use a guard-clause style for consistency with the enqueue method.

Comment thread firebase_admin/functions.py Outdated
@inlined inlined requested a review from lahirumaramba July 13, 2026 16:55
…e style

Refactored the delete method in TaskQueue to use a guard-clause style for handling exceptions, aligning it with enqueue and removing the redundant return statement.
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