From 113c17538423a4afdfec27b008667ab86ed9160d Mon Sep 17 00:00:00 2001 From: Tymofii Pidlisnyi Date: Sat, 28 Nov 2026 15:08:51 -0800 Subject: [PATCH] test: installed-wheel consumer, separate reserver getter cases, FrozenMapping export and docs Each reserver result getter is tested on its own. A CI job builds the wheel and runs an outside consumer against it. FrozenMapping is exported and its comparison and conversion are documented. Signed-off-by: Tymofii Pidlisnyi --- .github/workflows/tests.yml | 27 ++++ CHANGELOG.md | 11 ++ MIGRATION.md | 26 +++- scripts/verify_installed_wheel.py | 131 ++++++++++++++++++ src/agent_passport/__init__.py | 1 + .../v2/authority_delegation/__init__.py | 2 + tests/v2/test_verified_chain_boundary.py | 23 ++- 7 files changed, 212 insertions(+), 9 deletions(-) create mode 100644 scripts/verify_installed_wheel.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5c172df..2d025e5 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -64,3 +64,30 @@ jobs: - name: Run test suite run: python3 -m pytest -q + + installed-wheel: + # Builds the wheel, installs it into a fresh environment and runs an outside + # consumer from outside the source tree, so the published package is tested + # the way users import it, not through the source checkout. + name: Installed wheel consumer + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + with: + persist-credentials: false + - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0 + with: + python-version: "3.12" + - name: Build the wheel + run: | + python -m pip install --upgrade pip build + python -m build --wheel --outdir "$RUNNER_TEMP/dist" + - name: Install the wheel into a fresh environment and run the consumer + run: | + python -m venv "$RUNNER_TEMP/consumer" + "$RUNNER_TEMP/consumer/bin/python" -m pip install "$RUNNER_TEMP"/dist/*.whl + cp scripts/verify_installed_wheel.py "$RUNNER_TEMP/consumer.py" + version=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])") + cd "$RUNNER_TEMP" && "$RUNNER_TEMP/consumer/bin/python" -I consumer.py "$version" + diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d799d8..9bd01a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## Unreleased + +### Added + +- `FrozenMapping`, the read-only mapping type of verified records, is exported from the package root and from `agent_passport.v2.authority_delegation`. +- A CI job builds the wheel, installs it into a fresh environment and runs `scripts/verify_installed_wheel.py` from outside the source tree, covering the verified-chain budget API as a consumer imports it. + +### Documentation + +- MIGRATION.md states how `FrozenMapping` comparison works and shows a recursive conversion to plain data, since `dict()` converts only the top level. + ## 5.0.0 (2026-11-28) ### Breaking change diff --git a/MIGRATION.md b/MIGRATION.md index 6602ed7..2bdc38a 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -100,11 +100,29 @@ had already ended when verification completed. The ledger refuses such a handle `AUTHORITY_STALE`. Chain selection without a spend requirement also checks the handle at `verified_at` and reports `AUTHORITY_STALE` as undecided instead of `scope_covered`. -Verified records are `FrozenMapping` objects and tuples. A `FrozenMapping` compares -equal only to another `FrozenMapping`, so convert it to a `dict` before comparing it -with one. The registry keeps its own copies of the records and context, and +Verified records are `FrozenMapping` objects and tuples, nested to any depth. A +`FrozenMapping` compares equal to another `FrozenMapping` with equal contents. Compared +with any other object it returns `NotImplemented`, so Python then asks the other +operand, which may have its own equality. A `FrozenMapping` never passes its storage +to that code. `dict(record)` converts only the top level, and nested values stay +`FrozenMapping` objects and tuples. To get plain JSON-shaped data, convert recursively, +for example: + +```python +from collections.abc import Mapping + +def to_plain(value): + if isinstance(value, Mapping): + return {key: to_plain(item) for key, item in value.items()} + if isinstance(value, tuple): + return [to_plain(item) for item in value] + return value +``` + +The registry keeps its own copies of the records and context, and `consume_verified_authority_chain` returns fresh copies, so changes to the handle's -public view do not change what is checked or reserved. +public view do not change what is checked or reserved. Python privacy is still +conventional, as described below. The ledger reads its consumer clock while holding its own lock. A `now` callback must return the time and nothing else. It must not call back into the same ledger, which diff --git a/scripts/verify_installed_wheel.py b/scripts/verify_installed_wheel.py new file mode 100644 index 0000000..f9747ce --- /dev/null +++ b/scripts/verify_installed_wheel.py @@ -0,0 +1,131 @@ +# Copyright (c) 2026 Tymofii Pidlisnyi +# SPDX-License-Identifier: Apache-2.0 +"""Outside consumer of the installed wheel. + +Run with ``python -I`` from a directory outside the source tree, after installing +the built wheel into a fresh environment. It imports only from the package root, +checks that the import resolves to site-packages and that the version matches +the wheel, and exercises the verified-chain budget API the way a consumer would. +""" + +from __future__ import annotations + +import copy +import pickle +import sys +from collections.abc import Mapping + +import agent_passport as aps +from agent_passport import ( + AuthorityVerifierConfig, + FrozenMapping, + HeldChain, + InMemoryAuthorityBudgetLedger, + RequiredSpendV1, + consume_verified_authority_chain, + create_authority_chain_verifier, + generate_key_pair, + issue_authority_delegation, + select_chain_for_action, + verify_authority_chain_for_budget, +) + +expected_version = sys.argv[1] +assert "site-packages" in aps.__file__, aps.__file__ +assert aps.__version__ == expected_version, (aps.__version__, expected_version) + +START, NOW, END = ( + "2026-07-18T22:00:00.000Z", + "2026-07-18T22:10:00.000Z", + "2026-07-18T23:00:00.000Z", +) +UNIT = "iso4217:USD:minor" +key = generate_key_pair() +root = issue_authority_delegation( + { + "record_type": "aps:authority-delegation:v1", + "version": "1.0", + "parent_delegation_id": None, + "issuer": "did:example:root", + "subject": "did:example:a", + "verification_method": "did:example:root#key", + "issued_at": START, + "nonce": "1" * 32, + "authority": { + "scope": {"profile": "aps-hierarchical-v1", "grants": ["commerce:*"]}, + "spend": { + "mode": "bounded", + "unit": UNIT, + "per_action": "100", + "cumulative": "100", + }, + "depth": {"remaining": 1}, + "time": {"not_before": START, "not_after": END}, + "reputation": {"profile": "aps-score-0-100-v1", "ceiling": 100}, + "values": {"profile": "aps-values-identifiers-v1", "required": []}, + "reversibility": {"profile": "aps-tci-v1", "ceiling": "irreversible"}, + }, + }, + key["privateKey"], +) + + +def config(policy: str) -> AuthorityVerifierConfig: + return AuthorityVerifierConfig( + policy, + "local", + lambda: NOW, + lambda _issuer, _method, _at: key["publicKey"], + lambda _candidate: True, + lambda _record: "active", + ) + + +verifier = create_authority_chain_verifier(config("consumer")) +checked = verify_authority_chain_for_budget([root], verifier) +assert checked.ok, checked.result +chain = checked.chain +assert isinstance(chain.records[0], FrozenMapping) + +ledger = InMemoryAuthorityBudgetLedger(verifier, now=lambda: NOW) +assert ledger.reserve(chain, "a" * 64, UNIT, "60").code == "RESERVED" +assert ledger.reserve(chain, "b" * 64, UNIT, "60").code == "CUMULATIVE_EXCEEDED" +for forged in ( + [root], + copy.copy(chain), + copy.deepcopy(chain), + pickle.loads(pickle.dumps(chain)), +): + assert ledger.reserve(forged, "c" * 64, UNIT, "1").code == "AUTHORITY_UNVERIFIED" +other = create_authority_chain_verifier(config("consumer")) +assert ( + InMemoryAuthorityBudgetLedger(other, now=lambda: NOW) + .reserve(chain, "d" * 64, UNIT, "1") + .code + == "AUTHORITY_CONTEXT_MISMATCH" +) +assert ( + consume_verified_authority_chain(chain, verifier, now=lambda: END).code + == "AUTHORITY_STALE" +) + +picked = select_chain_for_action( + held=[HeldChain("one", [root])], + required_grants=["commerce:checkout"], + verifier=verifier, + required_spend=RequiredSpendV1(UNIT, "10", "e" * 64), + reserve_budget=InMemoryAuthorityBudgetLedger(verifier, now=lambda: NOW), +) +assert picked.selected and picked.chain_id == "one", picked + + +def to_plain(value: object) -> object: + if isinstance(value, Mapping): + return {k: to_plain(v) for k, v in value.items()} + if isinstance(value, tuple): + return [to_plain(v) for v in value] + return value + + +assert to_plain(chain.records[0]) == root +print("installed wheel consumer ok", aps.__version__) diff --git a/src/agent_passport/__init__.py b/src/agent_passport/__init__.py index 07f6c82..beffef6 100644 --- a/src/agent_passport/__init__.py +++ b/src/agent_passport/__init__.py @@ -137,6 +137,7 @@ VerifiedAuthorityRefusal, VerifiedRecords, FrozenJSON, + FrozenMapping, create_authority_chain_verifier, verify_authority_chain_for_budget, consume_verified_authority_chain, diff --git a/src/agent_passport/v2/authority_delegation/__init__.py b/src/agent_passport/v2/authority_delegation/__init__.py index cd4d474..26407b6 100644 --- a/src/agent_passport/v2/authority_delegation/__init__.py +++ b/src/agent_passport/v2/authority_delegation/__init__.py @@ -115,6 +115,7 @@ AuthorityVerifierConfig, BudgetOperationResult, FrozenJSON, + FrozenMapping, VerifiedAuthorityChain, VerifiedAuthorityConsumed, VerifiedAuthorityConsumption, @@ -150,6 +151,7 @@ "AuthorityVerifierConfig", "BudgetOperationResult", "FrozenJSON", + "FrozenMapping", "InMemoryAuthorityBudgetLedger", "VerifiedAuthorityChain", "VerifiedAuthorityConsumed", diff --git a/tests/v2/test_verified_chain_boundary.py b/tests/v2/test_verified_chain_boundary.py index 0bbea12..03e6096 100644 --- a/tests/v2/test_verified_chain_boundary.py +++ b/tests/v2/test_verified_chain_boundary.py @@ -843,17 +843,25 @@ def test_public_view_changes_do_not_reach_consumption(): assert not again.ok and again.code == "AUTHORITY_STALE" -def test_reserver_result_properties_that_raise_leave_selection_undecided(): +@pytest.mark.parametrize("prop", ["code", "ok"]) +def test_reserver_result_getter_that_raises_leaves_selection_undecided(prop): f = fixture() + reads = [] class Result: @property def code(self): - raise RuntimeError("code read") + reads.append("code") + if prop == "code": + raise RuntimeError("code read") + return "RESERVED" @property def ok(self): - raise RuntimeError("ok read") + reads.append("ok") + if prop == "ok": + raise RuntimeError("ok read") + return True class Reserver: def reserve(self, chain, ref, unit, amount): @@ -866,8 +874,13 @@ def reserve(self, chain, ref, unit, amount): required_spend=RequiredSpendV1(UNIT, "25", REF), reserve_budget=Reserver(), ) - assert not result.selected - assert [e.code for e in result.evaluations] == ["spend_ledger_unavailable"] + # The getter that raises was reached, so the guard around it is what is tested. + assert prop in reads + # Unavailable, not a refusal: the reserver may have changed its own state first. + assert not result.selected and result.code == "selection_undecided" + assert [(e.outcome, e.code) for e in result.evaluations] == [ + ("undecided", "spend_ledger_unavailable") + ] def test_presentation_check_runs_no_caller_key_equality():