Repository navigation
feat(migrations): port agent-middleware-api 047-050 (same revision ids) onto main's 046 #234
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c96958b
feat(migrations): port agent-middleware-api 047-050 with the same rev…
PetrefiedThunder f563483
fix(migrations): rls_bypass in env.py, boot check for reconcile columns
PetrefiedThunder e6d5f99
fix(migrations): port TENANT_RLS_MODE setting; RLS for resolution tables
PetrefiedThunder 6d977e2
feat(migrations): add opt-in TENANT_RLS_MODE=enable and PG RLS proof
PetrefiedThunder 6ea1f92
Merge remote-tracking branch 'origin/main' into fix/234-v2
PetrefiedThunder e62d99e
Revert "feat(migrations): add opt-in TENANT_RLS_MODE=enable and PG RL…
PetrefiedThunder f4f6c37
Merge remote-tracking branch 'origin/main' into fix/234-v2
PetrefiedThunder eb10c01
test(migrations): avoid head pins and cover 047/048 negative paths
PetrefiedThunder f00ff66
style(tests): ruff format 047/048 negative-path tests
PetrefiedThunder 424bee7
Merge origin/main into hermes/port-api-migrations-047-050
PetrefiedThunder 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
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
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,36 @@ | ||
| # Schema 047: dispatch reconcile lease (ported) | ||
|
|
||
| Migration `047_dispatch_reconcile_lease` revises `046_api_key_usage_events`. It is | ||
| ported from the archived agent-middleware-api chain with the same revision id, | ||
| so databases that service already migrated to 047-050 (staging and production, | ||
| both at `050_demo_key_first_used`) are at a revision this repository knows. | ||
|
|
||
| It adds nullable `reconcile_status`, `reconcile_lease_owner`, | ||
| `reconcile_lease_expires_at`, `reconcile_next_probe_at`, `reconciled_at`, and | ||
| `reconcile_probe_count` (NOT NULL, default 0) to `mcp_dispatch_attempts`, plus | ||
| three indexes. Columns and indexes are added only when missing. No row is | ||
| updated. `McpDispatchAttemptModel` maps the columns; nothing here writes them yet. | ||
|
|
||
| ## Lineage gap for databases at the api chain's 050 | ||
|
|
||
| The two chains share 001-042 and then diverge. A database migrated by the api | ||
| chain to 050 never ran this repository's `044_receipt_resolutions`, | ||
| `045_approval_gate_approvals`, or `046_api_key_usage_events`. Because 047 now | ||
| follows this repository's 046, Alembic treats those as applied, so such a | ||
| database stays at 050 without `receipt_resolutions`, `resolution_chain_heads`, | ||
| `approval_gate_approvals`, `api_key_usage_events`, or the `api_key_usage_daily` | ||
| view. It also keeps the api-only insights tables, which no model here maps. | ||
| This port does not repair that; a later idempotent migration must create the | ||
| missing objects before code that uses them serves against such a database. | ||
|
|
||
| ## Compatibility release | ||
|
|
||
| Production boot requires the database revision to match the packaged head. | ||
| Fresh databases from this chain gain 047-050 normally. Qualify against a | ||
| disposable PostgreSQL upgrade from 046 and from an api-chain 050 copy. | ||
| These release checks have not been run against staging or production. | ||
|
|
||
| ## Rollback boundary | ||
|
|
||
| Downgrade to 046 refuses while any row has a non-NULL `reconcile_status` and | ||
| otherwise drops the six columns and three indexes. |
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,39 @@ | ||
| # Schema 048: tenant RLS policies (ported) | ||
|
|
||
| Migration `048_tenant_rls` revises `047_dispatch_reconcile_lease`. It is ported | ||
| from the agent-middleware-api chain with the same revision id and is | ||
| PostgreSQL only. It creates the `amw_tenant_isolation` policy on the 15 | ||
| archived wallet-scoped tables and the `amw_delegated_issuer_read` policies. | ||
|
|
||
| The two wallet-scoped tables from this repo's 044, `receipt_resolutions` and | ||
| `resolution_chain_heads`, get the same `amw_tenant_isolation` policy when they | ||
| already exist. On an api-chain database they don't exist yet; | ||
| `053_repair_api_lineage` creates them and applies the identical policy. | ||
|
|
||
| `TENANT_RLS_MODE` is ported from the api Settings with the same default (`off`) | ||
| and the same validation (`off` or `enforce`). | ||
|
|
||
| - `off`: the policies exist, but row-level security stays disabled, so nothing | ||
| changes. This matches the original's default. | ||
| - `enforce`: running the migration enables and forces RLS on every listed table. | ||
|
|
||
| `migrations/env.py` sets `app.rls_bypass` to `on` for the migration | ||
| transaction only, as the archived env.py did. That lets data migrations, such | ||
| as the 050 backfill, see every row under FORCE with a non-superuser owner. | ||
|
|
||
| ## Compatibility release | ||
|
|
||
| There is no behaviour change in `off` mode. | ||
|
|
||
| Warning: this gateway does not yet set `app.current_wallet_id` per transaction | ||
| (the api repo's runtime tenant scope is not ported). A database that was | ||
| migrated under `enforce` therefore fails closed for a NOSUPERUSER NOBYPASSRLS | ||
| runtime role: wallet-scoped tables read as empty and writes are refused. That | ||
| includes an api-chain 050 database that already has RLS forced. Don't migrate | ||
| under `enforce`, and don't point this gateway at such a database with a | ||
| policy-bound role, until the runtime scope is ported. | ||
|
|
||
| ## Rollback | ||
|
|
||
| Downgrade drops the policies. It also runs NO FORCE / DISABLE ROW LEVEL | ||
| SECURITY on each listed table, including the two late tables when they exist. |
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,9 @@ | ||
| # Schema 049: demo key attribution (ported) | ||
|
|
||
| Migration `049_demo_key_attribution` revises `048_tenant_rls`, ported with the | ||
| same revision id. `043_demo_key_fields` already adds the UTM and referrer | ||
| columns here, so on this chain it adds only missing columns (normally none). | ||
|
|
||
| ## Rollback boundary | ||
|
|
||
| Downgrade is a no-op; the columns belong to 043, which retains them. |
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,11 @@ | ||
| # Schema 050: demo key first use (ported) | ||
|
|
||
| Migration `050_demo_key_first_used` revises `049_demo_key_attribution`, ported | ||
| with the same revision id. `043_demo_key_fields` already adds | ||
| `api_keys.first_used_at` and the `demo_key_funnel_by_source` view, so this adds | ||
| them only when missing and backfills only NULL `first_used_at` from | ||
| `last_used_at`. Staging and production are at this revision. | ||
|
|
||
| ## Rollback boundary | ||
|
|
||
| Downgrade is a no-op; the column and view belong to 043, which retains them. |
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
107 changes: 107 additions & 0 deletions
107
gateway/migrations/versions/047_dispatch_reconcile_lease.py
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,107 @@ | ||
| """Add provider-status reconciliation columns to governed MCP dispatch attempts. | ||
|
|
||
| Additive and nullable (the probe counter defaults to zero). Existing | ||
| ``delivery_uncertain`` rows upgrade to an unresolved reconcile status, which | ||
| is exactly their meaning today: parked for manual review. No data update or | ||
| backfill is performed, and ``state`` is never touched. | ||
|
|
||
| Revision ID: 047_dispatch_reconcile_lease | ||
| Revises: 046_api_key_usage_events | ||
|
|
||
| Ported from the archived agent-middleware-api chain with the same revision | ||
| id so databases already stamped at 047-050 by that service stay on a known | ||
| revision. Here it follows this repo's 046. Columns and indexes are added only | ||
| when missing. | ||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
|
|
||
| revision: str = "047_dispatch_reconcile_lease" | ||
| down_revision: Union[str, None] = "046_api_key_usage_events" | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| _COLUMNS = ( | ||
| ( | ||
| "reconcile_status", | ||
| lambda: sa.Column("reconcile_status", sa.String(length=32), nullable=True), | ||
| ), | ||
| ( | ||
| "reconcile_lease_owner", | ||
| lambda: sa.Column("reconcile_lease_owner", sa.String(length=64), nullable=True), | ||
| ), | ||
| ( | ||
| "reconcile_lease_expires_at", | ||
| lambda: sa.Column("reconcile_lease_expires_at", sa.DateTime(), nullable=True), | ||
| ), | ||
| ( | ||
| "reconcile_next_probe_at", | ||
| lambda: sa.Column("reconcile_next_probe_at", sa.DateTime(), nullable=True), | ||
| ), | ||
| ( | ||
| "reconcile_probe_count", | ||
| lambda: sa.Column( | ||
| "reconcile_probe_count", sa.Integer(), nullable=False, server_default="0" | ||
| ), | ||
| ), | ||
| ("reconciled_at", lambda: sa.Column("reconciled_at", sa.DateTime(), nullable=True)), | ||
| ) | ||
| _INDEXES = ( | ||
| ("ix_mcp_dispatch_attempts_reconcile_status", "reconcile_status"), | ||
| ( | ||
| "ix_mcp_dispatch_attempts_reconcile_lease_expires_at", | ||
| "reconcile_lease_expires_at", | ||
| ), | ||
| ("ix_mcp_dispatch_attempts_reconcile_next_probe_at", "reconcile_next_probe_at"), | ||
| ) | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| inspector = sa.inspect(op.get_bind()) | ||
| columns = {c["name"] for c in inspector.get_columns("mcp_dispatch_attempts")} | ||
| indexes = {i["name"] for i in inspector.get_indexes("mcp_dispatch_attempts")} | ||
| missing_columns = [make() for name, make in _COLUMNS if name not in columns] | ||
| missing_indexes = [(n, c) for n, c in _INDEXES if n not in indexes] | ||
| if not missing_columns and not missing_indexes: | ||
| return | ||
| with op.batch_alter_table("mcp_dispatch_attempts") as batch_op: | ||
| for column in missing_columns: | ||
| batch_op.add_column(column) | ||
| for name, column in missing_indexes: | ||
| batch_op.create_index(name, [column]) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # Any non-NULL status is a durable provider decision. A refunded row's | ||
| # refund and budget release outlive the column, and 046 accounting counts | ||
| # every delivery_uncertain receipt as consumed, so a budget rebuild would | ||
| # re-charge the released spend; a pending row may be half applied; and a | ||
| # re-upgrade would re-probe every erased decision. Refuse rather than | ||
| # silently drop any of them. | ||
| bind = op.get_bind() | ||
| decided = bind.execute( | ||
| sa.text( | ||
| "SELECT COUNT(*) FROM mcp_dispatch_attempts " | ||
| "WHERE reconcile_status IS NOT NULL" | ||
| ) | ||
| ).scalar_one() | ||
| if decided: | ||
| raise RuntimeError( | ||
| f"047 downgrade refused: {decided} dispatch attempt(s) carry a " | ||
| "reconcile_status decision that 046 cannot represent" | ||
| ) | ||
| with op.batch_alter_table("mcp_dispatch_attempts") as batch_op: | ||
| batch_op.drop_index("ix_mcp_dispatch_attempts_reconcile_next_probe_at") | ||
| batch_op.drop_index("ix_mcp_dispatch_attempts_reconcile_lease_expires_at") | ||
| batch_op.drop_index("ix_mcp_dispatch_attempts_reconcile_status") | ||
| batch_op.drop_column("reconciled_at") | ||
| batch_op.drop_column("reconcile_probe_count") | ||
| batch_op.drop_column("reconcile_next_probe_at") | ||
| batch_op.drop_column("reconcile_lease_expires_at") | ||
| batch_op.drop_column("reconcile_lease_owner") | ||
| batch_op.drop_column("reconcile_status") | ||
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.