Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Semantic and authorization equivalence for cache reuse
- The prerequisite for a cache hit is that the new request and the old result are still equivalent in terms of task semantics, data version, and authorization scope. Neither similar text nor short-term expiration can be independently proven to be safe for reuse.
How does the mechanism work?
Step-by-step explanation
- Is it enough to just put user_id into the key?
The main question designs cache keys; explain why a user identifier alone is insufficient.
- What should I do if the permissions have changed but the cache has not expired?
Cache versioning must also handle uncertainty in revocation propagation.
- How to verify when semantic cache encounters "contains" and "does not contain"?
Semantic matching can miss decisive conditions, so provide concrete checks.
Implementation · Build it
Implement the contract · Semantic and authorization equivalence for cache reuse
Objectives of this level: Implement the current-scope cache decision contract from an empty function.
Visual guide · A hit needs current equivalence
A fresh TTL is only one test. Current authority comes first; write receipts require reconciliation rather than read-cache reuse. Select the figure to open it at full size.
Full local contract
Implement decide_cache(payload) with exactly request/principal/entry. Request has tenant/user/order/query (nonempty strings), purpose (read/create/reconcile), policy/data/now (nonnegative strict integers; bool is invalid), operation_key (string, nonempty for reconcile). Principal has tenant/user (nonempty strings), active (bool), orders (unique nonempty string list). Entry is null or exactly tenant/user/order/query, policy/data/expires, kind, complete, value; the first four are nonempty strings, the next three strict nonnegative integers, kind is read_result/write_receipt, complete is bool, value is JSON. Validate request/principal shapes first (ValueError:input_shape), then current identity→active→order ownership. Denial reasons are identity/revoked/ownership. Create always misses with effect_boundary; reconcile returns its original operation_key. Only reads inspect entry: null→absent, malformed→ValueError:entry_shape, mismatching tenant/user/order/query→key_mismatch, policy/data→version_mismatch, expires<=now→expired, receipt→effect_boundary, incomplete→incomplete, otherwise hit with an independent deep-copied value. Return {status:denied|miss,reason:...}, {status:reconcile,operation_key:...}, or {status:hit,value:...}. No other keys. Do not mutate input or reuse input containers. Shape errors precede authorization, which precedes effect/cache handling; this ordering is part of this teaching contract, not a universal cache specification.
def decide_cache(payload):
raise NotImplementedError('implement_cache_contract')
Run your function
Extract the source package so runner and cases/ stay adjacent. The first grade intentionally exits 1 for the empty template. Save that red baseline, implement your own function and rerun; a complete pass exits 0.
python3 lab_runner.py scaffold --course agent-cache-correctness --out cache_student.py
python3 lab_runner.py grade --course agent-cache-correctness --module cache_student.py --out cache-report.json
Runner · Cases · Source package
Beyond the public fixture
Add a previously unlisted query change and a nested result object; show a miss for the changed query and independent containers for the hit. Reports prove fixed local behavior of your code, not Redis/CDN integration, latency, authorization infrastructure or interview readiness.
Connect this topic to the actual project
Read the integrated project guide and source package. It follows the real get_order tool call, current authority from the independent local merchant over HTTP, persisted SQLite cache, restart/version/permission changes, and the separately committed return receipt. The project adapter states its mapping from this pure-function contract; fixed JSON labels do not authenticate a caller. Read and execute the guided baseline/integration comparison, then implement the separate learner assignment. Keep guided output, your own source diff and end-to-end evidence distinct. Role-specific examples are authored teaching examples, not observed learner scores or job certification.
code example
An empty function is a red baseline
The declared decide_cache(payload) function must return the exact decision and independently copy the nested value. Use the source package scaffold; retain its failing report before your own implementation.
Acceptance task for this level
Implement decide_cache, run the 20 public cases, add one private changed-order case and explain its result.
Reading this browser’s record…
Hide the answer and check your understanding
Why can a deep copy matter even after the hit decision is correct?
Expand reference derivation
Returning the cache-owned container lets a caller mutate evidence through aliasing. Copy the result; this local guard does not replace database/concurrent cache ownership controls.
Further explanations and practice
When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.
All linked explanations and exercises (1 )
Sources and verification scope
The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.
- OWASP: Data isolation and security boundaries in RAG
- Anthropic:Effective context engineering
- MCP 2025-11-25:Authorization
- OpenAI: Reusing prefix computation with prompt caching
- Extended practice runner
- Cache contract cases
- Extended practice source package
- OWASP authorization
- HTTP caching RFC 9111
- Integrated project guide
- Role-specific delivery examples
- Independent integration assignment
- Author-designed assessment examples
- Project integration source ZIP