-
Notifications
You must be signed in to change notification settings - Fork 363
feat(server): implement Resource Scoping for tasks and push notifications
#709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sokoliva
wants to merge
21
commits into
a2aproject:1.0-dev
Choose a base branch
from
sokoliva:resource-scoping
base: 1.0-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2d2d31e
feat(server): implement `resource scoping` for `tasks` and `push noti…
sokoliva 6093f7f
fix: add poolclass to allow.txt
sokoliva ce1a305
Merge branch '1.0-dev' of https://github.com/a2aproject/a2a-python in…
sokoliva 6600b47
fix: test_inmemory_task_store.py merge caused error
sokoliva a212da7
Merge branch '1.0-dev' into resource-scoping
sokoliva ea89bbb
fix: - add alembic to dev field in pyproject.toml
sokoliva 9301b8c
fix: update uv.lock
sokoliva 62dce31
fix:
sokoliva 86d769e
Merge branch '1.0-dev' into resource-scoping
sokoliva 99cf89f
fix: fix linter issues
sokoliva a11d6de
Merge branch 'resource-scoping' of https://github.com/sokoliva/a2a-py…
sokoliva feb5033
Fix: fix some more linter errors
sokoliva 212ad37
fix: more linter errors fixed
sokoliva f7b5c1c
fix: make parameter `ServerCallContext` non-optional in `PushNotifica…
sokoliva 179b21a
Merge branch '1.0-dev' into resource-scoping
sokoliva 38d7df6
fix: add ServerCallContext to tests/e2e/push_notifications/agent_app.py
sokoliva 5eeb89d
Merge branch 'resource-scoping' of https://github.com/sokoliva/a2a-py…
sokoliva c4b282a
fix: small fix
sokoliva 0090ecc
fix: fix unit test error
sokoliva 0f51ef3
fi: fix
sokoliva 00e5eac
fix: fix
sokoliva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ openapiv2 | |
| opensource | ||
| otherurl | ||
| pb2 | ||
| poolclass | ||
| postgres | ||
| POSTGRES | ||
| postgresql | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # A generic, single database configuration. | ||
|
|
||
| [alembic] | ||
|
|
||
| # database URL. This is consumed by the user-maintained env.py script only. | ||
| # other means of configuring database URLs may be customized within the env.py | ||
| # file. | ||
| # IMPORTANT: This is a placeholder and an example, and should be replaced with your actual database URL. | ||
| sqlalchemy.url = sqlite+aiosqlite:///./test.db | ||
|
|
||
|
|
||
| # Logging configuration | ||
| [loggers] | ||
| keys = root,sqlalchemy,alembic | ||
|
|
||
| [handlers] | ||
| keys = console | ||
|
|
||
| [formatters] | ||
| keys = generic | ||
|
|
||
| [logger_root] | ||
| level = WARNING | ||
| handlers = console | ||
| qualname = | ||
|
|
||
| [logger_sqlalchemy] | ||
| level = WARNING | ||
| handlers = | ||
| qualname = sqlalchemy.engine | ||
|
|
||
| [logger_alembic] | ||
| level = INFO | ||
| handlers = | ||
| qualname = alembic | ||
|
|
||
| [handler_console] | ||
| class = StreamHandler | ||
| args = (sys.stderr,) | ||
| level = NOTSET | ||
| formatter = generic | ||
|
|
||
| [formatter_generic] | ||
| format = %(levelname)-5.5s [%(name)s] %(message)s | ||
| datefmt = %H:%M:%S |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Database Migrations with Alembic | ||
|
|
||
| This directory contains database migration scripts for the A2A SDK, managed by [Alembic](https://alembic.sqlalchemy.org/). | ||
|
|
||
| ## Configuration | ||
|
|
||
| - `alembic.ini`: Global configuration for Alembic, including the database URL. | ||
| - `env.py`: Python script that runs when the Alembic environment is invoked. It configures the SQLAlchemy engine and connects it to the migration context. | ||
| - `versions/`: Directory containing individual migration scripts. | ||
|
|
||
| ## Common Commands | ||
|
|
||
| All commands should be run from the project root using `uv run`. | ||
|
|
||
| ### Viewing Status | ||
| ```bash | ||
| # View current migration version of the database | ||
| uv run alembic current | ||
|
|
||
| # View migration history | ||
| uv run alembic history --verbose | ||
| ``` | ||
|
|
||
| ### Running Migrations | ||
| ```bash | ||
| # Upgrade to the latest version | ||
| uv run alembic upgrade head | ||
|
|
||
| # Downgrade by one version | ||
| uv run alembic downgrade -1 | ||
|
|
||
| # Revert all migrations | ||
| uv run alembic downgrade base | ||
| ``` | ||
|
|
||
| ### Creating Migrations | ||
| ```bash | ||
| # Create a new migration manually | ||
| uv run alembic revision -m "description of changes" | ||
|
|
||
| # Create a new migration automatically (detects changes in models.py) | ||
| uv run alembic revision --autogenerate -m "description of changes" | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### "duplicate column name" error | ||
| If you see an error like `sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) duplicate column name: owner`, it usually means the column was already created (perhaps by `Base.metadata.create_all()` in tests or development) but Alembic doesn't know about it yet. | ||
|
|
||
| To fix this, "stamp" the database to tell Alembic it is already at the latest version: | ||
| ```bash | ||
| uv run alembic stamp head | ||
| ``` | ||
|
|
||
| ## How to add a new migration | ||
| 1. Modify the models in `src/a2a/server/models.py`. | ||
| 2. Run `uv run alembic revision --autogenerate -m "Add new field to Task"`. | ||
| 3. Review the generated script in `alembic/versions/`. | ||
| 4. Apply the migration with `uv run alembic upgrade head`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| "Alembic database migration package." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import asyncio | ||
|
|
||
| from logging.config import fileConfig | ||
|
|
||
| from sqlalchemy import Connection, pool | ||
| from sqlalchemy.ext.asyncio import async_engine_from_config | ||
|
|
||
| from a2a.server.models import Base | ||
| from alembic import context | ||
|
|
||
|
|
||
| # this is the Alembic Config object, which provides | ||
| # access to the values within the .ini file in use. | ||
| config = context.config | ||
|
|
||
| # Interpret the config file for Python logging. | ||
| # This line sets up loggers basically. | ||
| if config.config_file_name is not None: | ||
| fileConfig(config.config_file_name) | ||
|
|
||
| # add your model's MetaData object here for 'autogenerate' support | ||
| target_metadata = Base.metadata | ||
|
|
||
| # other values from the config, defined by the needs of env.py, | ||
| # can be acquired: | ||
| # my_important_option = config.get_main_option("my_important_option") # noqa: ERA001 | ||
| # ... etc. | ||
|
|
||
|
|
||
| def run_migrations_offline() -> None: | ||
| """Run migrations in 'offline' mode. | ||
|
|
||
| This configures the context with just a URL | ||
| and not an Engine, though an Engine is acceptable | ||
| here as well. By skipping the Engine creation | ||
| we don't even need a DBAPI to be available. | ||
|
|
||
| Calls to context.execute() here emit the given string to the | ||
| script output. | ||
|
|
||
| """ | ||
| url = config.get_main_option('sqlalchemy.url') | ||
| context.configure( | ||
| url=url, | ||
| target_metadata=target_metadata, | ||
| literal_binds=True, | ||
| dialect_opts={'paramstyle': 'named'}, | ||
| ) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| def do_run_migrations(connection: Connection) -> None: | ||
| """Run migrations in 'online' mode. | ||
|
|
||
| This function is called within a synchronous context (via run_sync) | ||
| to configure the migration context with the provided connection | ||
| and target metadata, then execute the migrations within a transaction. | ||
|
|
||
| Args: | ||
| connection: The SQLAlchemy connection to use for the migrations. | ||
| """ | ||
| context.configure(connection=connection, target_metadata=target_metadata) | ||
|
|
||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| async def run_async_migrations() -> None: | ||
| """Run migrations using an Engine. | ||
|
|
||
| In this scenario we need to create an Engine | ||
| and associate a connection with the context. | ||
| """ | ||
| connectable = async_engine_from_config( | ||
| config.get_section(config.config_ini_section), | ||
| prefix='sqlalchemy.', | ||
| poolclass=pool.NullPool, | ||
| ) | ||
|
|
||
| async with connectable.connect() as connection: | ||
| await connection.run_sync(do_run_migrations) | ||
|
|
||
| await connectable.dispose() | ||
|
|
||
|
|
||
| def run_migrations_online() -> None: | ||
| """Run migrations in 'online' mode.""" | ||
| asyncio.run(run_async_migrations()) | ||
|
|
||
|
|
||
| if context.is_offline_mode(): | ||
| run_migrations_offline() | ||
| else: | ||
| run_migrations_online() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """${message} | ||
|
|
||
| Revision ID: ${up_revision} | ||
| Revises: ${down_revision | comma,n} | ||
| Create Date: ${create_date} | ||
|
|
||
| """ | ||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| ${imports if imports else ""} | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = ${repr(up_revision)} | ||
| down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)} | ||
| branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} | ||
| depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Upgrade schema.""" | ||
| ${upgrades if upgrades else "pass"} | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Downgrade schema.""" | ||
| ${downgrades if downgrades else "pass"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| """add_owner_to_task. | ||
|
|
||
| Revision ID: 6419d2d130f6 | ||
| Revises: | ||
| Create Date: 2026-02-17 09:23:06.758085 | ||
|
|
||
| """ | ||
|
|
||
| from collections.abc import Sequence | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from alembic import op | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = '6419d2d130f6' | ||
| down_revision: str | Sequence[str] | None = None | ||
| branch_labels: str | Sequence[str] | None = None | ||
| depends_on: str | Sequence[str] | None = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Upgrade schema.""" | ||
| op.add_column( | ||
| 'tasks', | ||
| sa.Column( | ||
| 'owner', | ||
| sa.String(255), | ||
| nullable=False, | ||
| server_default='unknown', # Set your desired default value here | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Downgrade schema.""" | ||
| op.drop_column('tasks', 'owner') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Alembic migrations scripts for the A2A project.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,6 +106,7 @@ style = "pep440" | |
|
|
||
| [dependency-groups] | ||
| dev = [ | ||
| "alembic>=1.14.0", | ||
| "mypy>=1.15.0", | ||
| "PyJWT>=2.0.0", | ||
| "pytest>=8.3.5", | ||
|
|
@@ -347,3 +348,89 @@ docstring-code-format = true | |
| docstring-code-line-length = "dynamic" | ||
| quote-style = "single" | ||
| indent-style = "space" | ||
|
|
||
|
|
||
| [tool.alembic] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I suggest linking original docs via comment here and remove commented out values as they add some noise to the file. |
||
|
|
||
| # path to migration scripts. | ||
| # this is typically a path given in POSIX (e.g. forward slashes) | ||
| # format, relative to the token %(here)s which refers to the location of this | ||
| # ini file | ||
| script_location = "%(here)s/alembic" | ||
|
|
||
| # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s | ||
| # Uncomment the line below if you want the files to be prepended with date and time | ||
| # see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file | ||
| # for all available tokens | ||
| # file_template = "%%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s" | ||
| # Or organize into date-based subdirectories (requires recursive_version_locations = true) | ||
| # file_template = "%%(year)d/%%(month).2d/%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d_%%(rev)s_%%(slug)s" | ||
|
|
||
| # additional paths to be prepended to sys.path. defaults to the current working directory. | ||
| prepend_sys_path = [ | ||
| "." | ||
| ] | ||
|
|
||
| # timezone to use when rendering the date within the migration file | ||
| # as well as the filename. | ||
| # If specified, requires the tzdata library which can be installed by adding | ||
| # `alembic[tz]` to the pip requirements. | ||
| # string value is passed to ZoneInfo() | ||
| # leave blank for localtime | ||
| # timezone = | ||
|
|
||
| # max length of characters to apply to the "slug" field | ||
| # truncate_slug_length = 40 | ||
|
|
||
| # set to 'true' to run the environment during | ||
| # the 'revision' command, regardless of autogenerate | ||
| # revision_environment = false | ||
|
|
||
| # set to 'true' to allow .pyc and .pyo files without | ||
| # a source .py file to be detected as revisions in the | ||
| # versions/ directory | ||
| # sourceless = false | ||
|
|
||
| # version location specification; This defaults | ||
| # to <script_location>/versions. When using multiple version | ||
| # directories, initial revisions must be specified with --version-path. | ||
| # version_locations = [ | ||
| # "%(here)s/alembic/versions", | ||
| # "%(here)s/foo/bar" | ||
| # ] | ||
|
|
||
|
|
||
| # set to 'true' to search source files recursively | ||
| # in each "version_locations" directory | ||
| # new in Alembic version 1.10 | ||
| # recursive_version_locations = false | ||
|
|
||
| # the output encoding used when revision files | ||
| # are written from script.py.mako | ||
| # output_encoding = "utf-8" | ||
|
|
||
| # This section defines scripts or Python functions that are run | ||
| # on newly generated revision scripts. See the documentation for further | ||
| # detail and examples | ||
| # [[tool.alembic.post_write_hooks]] | ||
| # format using "black" - use the console_scripts runner, | ||
| # against the "black" entrypoint | ||
| # name = "black" | ||
| # type = "console_scripts" | ||
| # entrypoint = "black" | ||
| # options = "-l 79 REVISION_SCRIPT_FILENAME" | ||
| # | ||
| # [[tool.alembic.post_write_hooks]] | ||
| # lint with attempts to fix using "ruff" - use the module runner, against the "ruff" module | ||
| # name = "ruff" | ||
| # type = "module" | ||
| # module = "ruff" | ||
| # options = "check --fix REVISION_SCRIPT_FILENAME" | ||
| # | ||
| # [[tool.alembic.post_write_hooks]] | ||
| # Alternatively, use the exec runner to execute a binary found on your PATH | ||
| # name = "ruff" | ||
| # type = "exec" | ||
| # executable = "ruff" | ||
| # options = "check --fix REVISION_SCRIPT_FILENAME" | ||
|
|
||
sokoliva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question on the developer experience here: if one needs to migrate their database - which steps are required? Currently readme mixes information for the package developers on how to add more migrations with the usage for integrations (how to run migrations).
I also believe that it assumes cloning the repository in order to run them, as it usually doesn't happen when one just uses package as a dependency. This creates some risk as one should make sure they switched to the appropriate commit to avoid applying migrations from
mainbranch not yet released to PyPi?Is it possible to distribute migrations together with the package itself and provide instructions on running them against the installed package version? We can also consider optional parameter which one may use to automatically apply migrations at runtime (default to
False).References: