From f58d6508c1488997e27c6a4aed4e058ed36c5252 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 5 Dec 2026 10:53:33 +0000 Subject: [PATCH] docs(replay): say what replay re-checks for a revoked permit The replay contract in failure-semantics.md said replay access re-validates the permit, wallet and key binding "so a revoked permit or a different key cannot read the stored receipt". The revoked-permit half is false: validate_replay_access checks the wallet, the key binding and the signature, and by design not revocation, expiry or remaining budget. A call made before a revoke replays its stored result and receipt. Replace the sentence with the text proposed in issue #434 part 2, and add a test that pins it: after a successful governed upstream call and a revoke, the same request with the same key returns the stored result and the same receipt_id with no new dispatch or charge, and a new idempotency key is refused with permit_revoked. Refs #434 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GyhDYGi5fpiBGFHez9fhbH Signed-off-by: Claude --- gateway/docs/failure-semantics.md | 9 ++- gateway/tests/test_mcp_upstream_governed.py | 83 +++++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/gateway/docs/failure-semantics.md b/gateway/docs/failure-semantics.md index 4b94f9221..8695faab4 100644 --- a/gateway/docs/failure-semantics.md +++ b/gateway/docs/failure-semantics.md @@ -221,9 +221,12 @@ same permit-constraint snapshot bytes a live receipt would A completed or terminalized invocation replays its stored envelope without re-execution or a second charge, including the original receipt where one exists. An unfinished local manual-review record instead remains -`idempotency_in_progress` and has no receipt. Replay access re-validates the -permit↔wallet↔key binding first, so a revoked permit or a different key cannot -read the stored receipt. +`idempotency_in_progress` and has no receipt. Replay access first re-checks +that the permit belongs to the wallet, its key binding and its signature. A key +of another wallet cannot read the stored receipt, and neither can a second key +on the same wallet when the permit is bound to a key. Revocation, expiry and +the remaining budget are not re-checked, so a call made before the permit was +revoked can still be replayed. | Stored terminal state | Replay returns | |---|---| diff --git a/gateway/tests/test_mcp_upstream_governed.py b/gateway/tests/test_mcp_upstream_governed.py index 6f471bdce..bb316bacd 100644 --- a/gateway/tests/test_mcp_upstream_governed.py +++ b/gateway/tests/test_mcp_upstream_governed.py @@ -2320,6 +2320,89 @@ async def test_governed_upstream_replay_remains_bound_to_permit_api_key( get_service_registry().unregister_local(tool_name) +@pytest.mark.anyio +async def test_governed_upstream_replay_after_revocation_returns_stored_receipt( + client: AsyncClient, + clean_database: None, +) -> None: + """Replay does not re-check revocation (failure-semantics.md, replay + contract): a call made before the revoke replays its stored result and + receipt without a new dispatch or charge, while a new key is refused.""" + provisioned = await provision_agent_wallet(client) + tool_name = "partner-upstream-replay-after-revoke" + idempotency_key = "partner-upstream-replay-after-revoke-1" + executor = FakeUpstreamExecutor("success") + _register_upstream(tool_name, executor) + try: + permit = await create_tool_permit( + client, + wallet_id=provisioned["agent_wallet_id"], + key_id=provisioned["key_id"], + tool_name=tool_name, + idem_key="partner-upstream-replay-after-revoke-permit", + ) + body = _call_body( + tool_name=tool_name, + wallet_id=provisioned["agent_wallet_id"], + permit_id=permit["permit_id"], + idempotency_key=idempotency_key, + ) + first = await client.post( + "/mcp/messages", + json=body, + headers=provisioned["agent_headers"], + ) + assert first.status_code == 200 + receipt_id = first.json()["result"]["receipt"]["receipt_id"] + assert receipt_id + + revoked = await client.post( + f"/v1/permits/{permit['permit_id']}/revoke", + headers=provisioned["agent_headers"], + ) + assert revoked.status_code == 200 + + replay = await client.post( + "/mcp/messages", + json=body, + headers=provisioned["agent_headers"], + ) + assert replay.status_code == 200 + assert "error" not in replay.json() + assert replay.json()["result"]["receipt"]["receipt_id"] == receipt_id + assert executor.dispatch_count == 1 + + fresh = await client.post( + "/mcp/messages", + json=_call_body( + tool_name=tool_name, + wallet_id=provisioned["agent_wallet_id"], + permit_id=permit["permit_id"], + idempotency_key="partner-upstream-replay-after-revoke-2", + ), + headers=provisioned["agent_headers"], + ) + assert fresh.status_code == 200 + assert fresh.json()["error"]["message"] == "permit_revoked" + assert executor.dispatch_count == 1 + + persisted = await _load_persisted_invocation( + wallet_id=provisioned["agent_wallet_id"], + permit_id=permit["permit_id"], + idempotency_key=idempotency_key, + ) + _assert_linked_terminal_state( + persisted, + state="succeeded", + outcome="success", + charged=Decimal("2"), + refunded=False, + ) + assert persisted.receipt.receipt_id == receipt_id + finally: + get_service_registry().unregister_local(tool_name) + + @pytest.mark.anyio async def test_governed_upstream_conflicting_payload_reuse_never_redispatches( client: AsyncClient,