-
Notifications
You must be signed in to change notification settings - Fork 367
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
41
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
41 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 5d65aa6
chore: distribute alembic migration files. Add helper _get_owner_push…
sokoliva cbadd5b
Merge branch '1.0-dev' of https://github.com/a2aproject/a2a-python in…
sokoliva 163c8c2
Merge branch '1.0-dev' into resource-scoping
sokoliva 44ad799
fix: linter issues
sokoliva 213ebb2
Merge branch 'resource-scoping' of https://github.com/sokoliva/a2a-py…
sokoliva 5bfccf9
fix: uv run ruff format
sokoliva bde88f1
refactor: add an edge case to test_database_task_store
sokoliva db3d050
fix: uv run ruff format
sokoliva a37efe1
fix: typo
sokoliva 782fcf7
chore: add migration for `push_notification_configs` table
sokoliva b77a4f6
Merge branch '1.0-dev' into resource-scoping
sokoliva 3428956
fix: rename README to README.md
sokoliva d90b360
feat: enhance migration CLI with table and verbose options; add owner…
sokoliva efc54a9
Merge branch '1.0-dev' into resource-scoping
sokoliva 28b6cfc
feat: enhance migration CLI with SQL mode and logging; update README …
sokoliva 591fa76
Merge branch 'resource-scoping' of https://github.com/sokoliva/a2a-py…
sokoliva 94f129d
fix: uv run ruff format
sokoliva 7f9e2bc
fix: remove user existence check in resolve_user_scope function
sokoliva 59f1fd4
feat: add CLI support for alembic in migration tool and update depend…
sokoliva 31fd9c1
fix: add ImportError handling for Alembic in CLI and migration files
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
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
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,35 @@ | ||
| # A generic, single database configuration. | ||
|
|
||
| [loggers] | ||
| keys = root,sqlalchemy,alembic | ||
|
|
||
| [handlers] | ||
| keys = console | ||
|
|
||
| [formatters] | ||
| keys = generic | ||
|
|
||
| [logger_root] | ||
| level = INFO | ||
| handlers = console | ||
| qualname = | ||
|
|
||
| [logger_sqlalchemy] | ||
| level = WARNING | ||
| handlers = | ||
| qualname = sqlalchemy.engine | ||
|
|
||
| [logger_alembic] | ||
| level = WARNING | ||
| 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 |
sokoliva marked this conversation as resolved.
Show resolved
Hide resolved
|
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,145 @@ | ||
| import argparse | ||
| import logging | ||
| import os | ||
|
|
||
| from importlib.resources import files | ||
|
|
||
|
|
||
| try: | ||
| from alembic import command | ||
| from alembic.config import Config | ||
|
|
||
| except ImportError as e: | ||
| raise ImportError( | ||
| "CLI requires Alembic. Install with: 'pip install a2a-sdk[cli]'." | ||
| ) from e | ||
|
|
||
|
|
||
| def _add_shared_args( | ||
| parser: argparse.ArgumentParser, is_sub: bool = False | ||
| ) -> None: | ||
| """Add common arguments to the given parser.""" | ||
| prefix = 'sub_' if is_sub else '' | ||
| parser.add_argument( | ||
| '-u', | ||
| '--database-url', | ||
| dest=f'{prefix}database_url', | ||
| help='Database URL to use for the migrations. If not set, the DATABASE_URL environment variable will be used.', | ||
| ) | ||
| parser.add_argument( | ||
| '-t', | ||
| '--table', | ||
| dest=f'{prefix}table', | ||
| help="Specific table to update. If not set, both 'tasks' and 'push_notification_configs' are updated.", | ||
| action='append', | ||
| ) | ||
| parser.add_argument( | ||
| '-v', | ||
| '--verbose', | ||
| dest=f'{prefix}verbose', | ||
| help='Enable verbose output (sets sqlalchemy.engine logging to INFO)', | ||
| action='store_true', | ||
| ) | ||
| parser.add_argument( | ||
| '--sql', | ||
| dest=f'{prefix}sql', | ||
| help='Run migrations in sql mode (generate SQL instead of executing)', | ||
| action='store_true', | ||
| ) | ||
|
|
||
|
|
||
| def create_parser() -> argparse.ArgumentParser: | ||
| """Create the argument parser for the migration tool.""" | ||
| parser = argparse.ArgumentParser(description='A2A Database Migration Tool') | ||
|
|
||
| # Global options | ||
| parser.add_argument( | ||
| '-o', | ||
| '--owner', | ||
| help="Value for the 'owner' column (used in specific migrations). If not set defaults to 'unknown'", | ||
| ) | ||
| _add_shared_args(parser) | ||
|
|
||
| subparsers = parser.add_subparsers(dest='cmd', help='Migration command') | ||
|
|
||
| # Upgrade command | ||
| up_parser = subparsers.add_parser( | ||
| 'upgrade', help='Upgrade to a later version' | ||
| ) | ||
| up_parser.add_argument( | ||
| 'revision', | ||
| nargs='?', | ||
| default='head', | ||
| help='Revision target (default: head)', | ||
| ) | ||
| up_parser.add_argument( | ||
| '-o', '--owner', dest='sub_owner', help='Alias for top-level --owner' | ||
| ) | ||
| _add_shared_args(up_parser, is_sub=True) | ||
|
|
||
| # Downgrade command | ||
| down_parser = subparsers.add_parser( | ||
| 'downgrade', help='Revert to a previous version' | ||
| ) | ||
| down_parser.add_argument( | ||
| 'revision', | ||
| nargs='?', | ||
| default='base', | ||
| help='Revision target (e.g., -1, base or a specific ID)', | ||
| ) | ||
| _add_shared_args(down_parser, is_sub=True) | ||
|
|
||
| return parser | ||
|
|
||
|
|
||
| def run_migrations() -> None: | ||
| """CLI tool to manage database migrations.""" | ||
| # Configure logging to show INFO messages | ||
| logging.basicConfig(level=logging.INFO, format='%(levelname)s %(message)s') | ||
|
|
||
| parser = create_parser() | ||
| args = parser.parse_args() | ||
|
|
||
| # Default to upgrade head if no command is provided | ||
| if not args.cmd: | ||
| args.cmd = 'upgrade' | ||
| args.revision = 'head' | ||
|
|
||
| # Locate the bundled alembic.ini | ||
| ini_path = files('a2a').joinpath('alembic.ini') | ||
| cfg = Config(str(ini_path)) | ||
|
|
||
| # Dynamically set the script location | ||
| migrations_path = files('a2a').joinpath('migrations') | ||
| cfg.set_main_option('script_location', str(migrations_path)) | ||
|
|
||
| # Consolidate owner, db_url, tables, verbose and sql values | ||
| owner = args.owner or getattr(args, 'sub_owner', None) | ||
| db_url = args.database_url or getattr(args, 'sub_database_url', None) | ||
| tables = args.table or getattr(args, 'sub_table', None) | ||
| verbose = args.verbose or getattr(args, 'sub_verbose', False) | ||
| sql = args.sql or getattr(args, 'sub_sql', False) | ||
|
|
||
| # Pass custom arguments to the migration context | ||
| if owner: | ||
| if args.cmd == 'downgrade': | ||
| parser.error( | ||
| "The --owner option is not supported for the 'downgrade' command." | ||
| ) | ||
| cfg.set_main_option('owner', owner) | ||
| if db_url: | ||
| os.environ['DATABASE_URL'] = db_url | ||
| if tables: | ||
| cfg.set_main_option('tables', ','.join(tables)) | ||
| if verbose: | ||
| cfg.set_main_option('verbose', 'true') | ||
|
|
||
| # Execute the requested command | ||
| if args.cmd == 'upgrade': | ||
| logging.info('Upgrading database to %s', args.revision) | ||
| command.upgrade(cfg, args.revision, sql=sql) | ||
| elif args.cmd == 'downgrade': | ||
| logging.info('Downgrading database to %s', args.revision) | ||
| command.downgrade(cfg, args.revision, sql=sql) | ||
|
|
||
| logging.info('Done.') |
sokoliva marked this conversation as resolved.
Show resolved
Hide resolved
|
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,100 @@ | ||
| # A2A SDK Database Migrations | ||
|
|
||
| This directory handles the database schema updates for the A2A SDK. It uses a bundled CLI tool to simplify the migration process. | ||
|
|
||
| ## User Guide for Integrators | ||
|
|
||
| When you install the `a2a-sdk`, you get a built-in command `a2a-db` which updates your database to make it compatible with the latest version of the SDK. | ||
|
|
||
| ### 1. Recommended: Back up your database | ||
|
|
||
| Before running migrations, it is recommended to back up your database. | ||
|
|
||
| ### 2. Set your Database URL | ||
| Migrations require the `DATABASE_URL` environment variable to be set with an `async-compatible` driver. | ||
| You can set it globally with `export DATABASE_URL`. Examples for SQLite, PostgreSQL and MySQL, respectively: | ||
|
|
||
| ```bash | ||
| export DATABASE_URL="sqlite+aiosqlite://user:pass@host:port/your_database_name" | ||
|
|
||
| export DATABASE_URL="postgresql+asyncpg://user:pass@localhost/your_database_name" | ||
|
|
||
| export DATABASE_URL="mysql+aiomysql://user:pass@localhost/your_database_name" | ||
| ``` | ||
|
|
||
| Or you can use the `-u` flag to specify the database URL for a single command. | ||
|
|
||
|
|
||
| ### 3. Apply Migrations | ||
| Always run this command after installing or upgrading the SDK to ensure your database matches the required schema. This will upgrade the tables `tasks` and `push_notification_configs` in your database by adding columns `owner` and `last_updated` and an index `(owner, last_updated)` to the `tasks` table and a column `owner` to the `push_notification_configs` table. | ||
|
|
||
| ```bash | ||
| uv run a2a-db | ||
| ``` | ||
|
|
||
| ### 4. Customizing Defaults with Flags | ||
| #### -o | ||
| Allows you to pass custom values for the new `owner` column. If not set, it will default to the value `unknown`. | ||
|
|
||
| ```bash | ||
| uv run a2a-db -o "admin_user" | ||
| ``` | ||
| #### -u | ||
| You can use the `-u` flag to specify the database URL for a single command. | ||
|
|
||
| ```bash | ||
| uv run a2a-db -u "sqlite+aiosqlite:///my_database.db" | ||
| ``` | ||
| #### -t | ||
| By default, tables `tasks` and `push_notification_configs` are updated. Using `-t` flag allows you to choose which tables to update. | ||
|
|
||
| ```bash | ||
| uv run a2a-db -t "my_table1" -t "my_table2" | ||
| ``` | ||
| #### -v | ||
| Enables verbose output by setting `sqlalchemy.engine` logging to `INFO`. | ||
|
|
||
| ```bash | ||
| uv run a2a-db -v | ||
| ``` | ||
| #### --sql | ||
| Enables running migrations in `offline` mode. Migrations are generated as SQL scripts instead of being run against the database. | ||
|
|
||
| ```bash | ||
| uv run a2a-db --sql | ||
| ``` | ||
|
|
||
| ### 5. Rolling Back | ||
| If you need to revert a change: | ||
|
|
||
| ```bash | ||
| # Step back one version | ||
| uv run a2a-db downgrade -1 | ||
|
|
||
| # Downgrade to a specific revision ID | ||
| uv run a2a-db downgrade <revision_id> | ||
|
|
||
| # Revert all migrations | ||
| uv run a2a-db downgrade base | ||
| ``` | ||
|
|
||
| All flags except the `-o` flag can be used during rollbacks. | ||
|
|
||
| --- | ||
|
|
||
| ## Developer Guide for SDK Contributors | ||
|
|
||
| If you are modifying the SDK models and need to generate new migration files, use the following workflow. | ||
|
|
||
| ### Creating a New Migration | ||
| Developers should use the raw `alembic` command locally to generate migrations. Ensure you are in the project root. | ||
|
|
||
| ```bash | ||
| # Detect changes in models.py and generate a script | ||
| uv run alembic revision --autogenerate -m "description of changes" | ||
| ``` | ||
|
|
||
| ### Internal Layout | ||
| - `env.py`: Configures the migration engine and applies the mandatory `DATABASE_URL` check. | ||
| - `versions/`: Contains the migration history. | ||
| - `script.py.mako`: The template for all new migration files. |
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." |
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.
Uh oh!
There was an error while loading. Please reload this page.