Page 4 of 8~112 min topic

Semantic cache lab

Measure false hits, hits, and token savings

Executable checks prove cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high on fixtures — including the known misshape behind CACHE-XTENANT-2.

~14 min this pageEvaluation

1Learn the idea

Read

Schema and policy checks

Add executable validation at the trust boundaries of tenant-scoped semantic cache for repeated FAQ paraphrases. Reject unknown fields where they matter, bound string sizes, and coerce only after auth/signature checks when raw bytes are security-relevant. Invariant under test: cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high. A TypeScript type or Python annotation is not runtime validation — pair them with parsers.

Read

Golden and adversarial fixtures

Automate the fixtures from setup, including a recreation of CACHE-XTENANT-2. Assert both the visible error and the absence of side effects (no provider call, no queue write, no flag flip). Where metrics matter, assert label enums stay bounded.

Read

Implementation artifact

assert cache.lookup(tenant="globex", policy="p3", question="reset wifi password") is None

Read

Gate semantics

Document which failures are client mistakes (4xx) versus operator/config mistakes (5xx/503). Oracle still stands: paraphrase hit returns cached answer with cache=HIT; different tenant never hits. Validation should make accidental “success with empty body” impossible for cost owner cutting duplicate GPT calls for Acme Corp FAQs.

Read

Stage depth

Property ideas: shuffled field order, Unicode edges, maximum-length strings, and replayed timestamps. Where money, identity, or citations matter, assertion messages should cite the field name. Do not snapshot entire provider payloads in tests; assert semantically. If validation fails open “to keep the demo working,” you have inverted the lab. Tie at least one CI job to the CACHE-XTENANT-2 fixture so main cannot regress silently. Re-read cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high after each new parser — convenience helpers love to bypass it.

Read

Field notes for `semantic-cache-lab` / `validation`

Table-drive status codes and error codes so reviewers see coverage at a glance. Include a Unicode normalization case if user text is accepted. Verify that oversized bodies fail before CPU-heavy work. Where digests or versions are pinned, assert mismatch behavior. Keep golden files small enough to read in review. CI should fail on skipped tests that mark the incident fixture as xfail without a ticket link. In this chapter the product is tenant-scoped semantic cache for repeated FAQ paraphrases, the human stakeholder is cost owner cutting duplicate GPT calls for Acme Corp FAQs, and the incident id you design against is CACHE-XTENANT-2. Re-state the oracle in your notes — paraphrase hit returns cached answer with cache=HIT; different tenant never hits — and keep the invariant visible: cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high. Track hit_rate ∈ [0.25,0.55] and cross_tenant_hits == 0 as the scoreboard. Surface under change control: POST /v1/answer. If you only have forty minutes, finish the fixture for cross-tenant near-duplicate question returns Acme's private pricing blurb before polishing UI. Promotion language stays ternary: promote, hold, or roll back based on evidence, not hope.

Read

Extra mastery block

For semantic-cache-lab, write a transfer example that differs in one constraint from the chapter scenario. Keep the quality bar fixed. Explain which check still applies.

Read

Extra mastery block

For semantic-cache-lab, write a transfer example that differs in one constraint from the chapter scenario. Keep the quality bar fixed. Explain which check still applies.

Go deeper

Before you start

Why this matters

List three fixtures: one golden success, one schema/auth reject, and one regression for CACHE-XTENANT-2. For each, write the exact assertion (status, code, metric, or citation) that must turn red if broken.

Check your understanding

Page assessment

Answer from memory. Completion is saved from this evidence, not from opening the next page.

1. Do parsers run before side effects?
2. Is CACHE-XTENANT-2 represented as a fixture?
3. Are side-effect absences asserted?

All responses are required.