Repository navigation
feat(examples): Cache Guard and Negative Cache Entries for negative caching - #4
Merged
Merged
Conversation
texastony
commented
Nov 5, 2026
texastony
commented
Nov 5, 2026
texastony
left a comment
Owner
Author
There was a problem hiding this comment.
I think I approve... squash and merge with a reasonable commit message.
…aching Negative caching (negative_caching.md): while AWS KMS has Denied a Branch Key ID, its Tenant Hierarchical Keyring fails fast, without calling Amazon DynamoDB or AWS KMS, for a Negative TTL. negative_cache.py (no MPL import): - DENIAL_ERROR_CODES, is_denial, and error_code (Design Question 5). - BranchKeyNegativeCache: the Negative Cache Entry of one Branch Key ID, with record, get, and clear. One lock is held across checking expiry and discarding, so get() never erases a Denial recorded concurrently. Rejects an empty Branch Key ID and a non-positive or NaN Negative TTL. - record() logs a warning naming the Branch Key ID, the KMS error code, the Negative TTL, and the error's text. cache_guard.py: - CacheGuard, an ICryptographicMaterialsCache wrapping the cache shared by every Tenant. While Denied, it raises OpaqueWithTextError(obj=error, obj_message=str(error)) before consulting the shared cache; the Hierarchical Keyring then fails without calling the AWS KMS Key Store. Otherwise it delegates. - OpaqueWithTextError keeps the original error, the same object, and its code through the MPL's conversion to and from Dafny, for all eight Denial codes; a ComAmazonawsKms error loses both for the KMS errors the MPL models. A new wrapper per raise stops the error's traceback growing. Tests (pytest.mark.local), in examples/test/multi_tenant/: classification, expiry, replacement, logging, validation; a deterministic expiry race test; thread safety; the guard's delegation and refusal of cached hits; and end to end, a real Hierarchical Keyring on the Cache Guard, seeded with the specification's cache identifiers, failing encrypt and decrypt with zero DynamoDB and KMS calls while Denied. FakeClock and kms_error are shared in helpers.py. negative_caching.md 4.3 and 5.2 record the error wrapping, and that the first Denial (a real cache miss, confirmed against DynamoDB Local) loses the code of the KMS errors the MPL models, so the code is logged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
texastony
force-pushed
the
examples/cache-guard
branch
from
November 5, 2026 22:22
a734238 to
a017983
Compare
1 task
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue #, if available:
Description of changes:
Implements the Cache Guard and Negative Cache Entries from
examples/src/multi_tenant/negative_caching.md(#3), mainly Design Questions 4.3 and 5. CI passes.This PR was opened with the interface stubbed (
NotImplementedError), so the tests could be reviewed before the implementation. It now contains the implementation, squashed into one commit.examples/src/multi_tenant/negative_cache.py(no MPL import; the Tenant KMS Client will record Denials through it)DENIAL_ERROR_CODES: the eight codes from Design Question 5.is_denial(error)is true only for a botocoreClientErrorwith one of them.error_code(error)returns the code.BranchKeyNegativeCache(branch_key_id, negative_ttl=300, clock=time.monotonic): the Negative Cache Entry, if any, of one Branch Key ID, withrecord,get, andclear.get()never erases a Denial recorded concurrently.record()logs one warning: "AWS KMS Denied Branch Key ID … (code); failing its requests for N seconds: …", with the error's full text.examples/src/multi_tenant/cache_guard.pyCacheGuard(delegate, negative_cache), anICryptographicMaterialsCache. While Denied, it raisesOpaqueWithTextError(obj=error, obj_message=str(error))before consulting the shared cache; otherwise it delegates.OpaqueWithTextError. All eight Denial codes were compared through a real Hierarchical Keyring:ComAmazonawsKmserror keeps neither for the four KMS errors the MPL models (Disabled,KMSInvalidState,NotFound,IncorrectKey).str().str()never names the code. The MPL generatesOpaqueWithTextErrorwith an empty message, so the warning log is where an operator sees the code.Tests (
pytest.mark.local; 103 offline tests pass in CI)record()on a second thread insideget()'s discard window. An unlocked implementation fails it.Not yet: the Tenant KMS Client does not record Denials yet, and the Tenant Keyring Factory does not exist yet; the tests call
record()themselves.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Check any applicable:
🤖 Generated with Claude Code