Unit 10 of 11 · about 5 hours · implement

Allocator and prefix-index state machines under randomized property tests

Single-operation tests miss interactions between fork, collision, eviction and cancellation. State-machine tests must explain which earlier transition first broke ownership, not just report a later wrong answer.

You'll be able to

  • Implement randomized allocator and prefix-index histories against an independent logical-state model.
  • Check capacity conservation, exact references, token-position correctness and leak-free cancellation after every operation.
  • Shrink a failing history and distinguish a stale-reference bug from an accounting bug.

Covers

Allocator and prefix-index state machinesRandomized operation-sequence propertiesNo double allocation, free or stale reference
1

Read

In this order, only the parts listed

0 of 3 done

  1. Mini-SGLang: allocator regressionsMini-SGLang - Radix Cache and Cache Tests

    TestAllocateEvictPageAlignment: all six tests, from test_allocate_after_evict_returns_page_aligned through test_check_integrity_passes_after_evict_cycle

  2. Mini-SGLang: what the integrity check provesMini-SGLang - Radix Cache and Cache Tests

    check_integrity, _allocate, _free and _page_to_token; compare with RadixPrefixCache.check_integrity in the radix file read earlier

    The manager check counts free plus indexed pages and checks alignment; the radix integrity method is empty at this commit. These are not proofs of unique ownership or active-request state.

  3. Inference Systems Laboratory: KV evidence contractLLM Inference Systems Laboratory - KV State

    Correctness requirements, KV physical/logical ownership; Failure-injection requirements, prefix fork/collision/eviction and request terminal phases

    Use the KV requirements only. The rest of the track capstone is not this unit's assignment.

2

Focus on

  • The independent oracle keeps request token lists, scope descriptors and expected ownership, not the implementation's tables or hashes. Recompute expected references from owners after each operation.
  • Partition physical pages into disjoint free/reclaimable and pinned sets under one convention. Check unique allocation, nonnegative references, valid table generations, immutable shared prefixes and correct valid-length gathers.
  • Generate successful and rejected allocate/append/fork/free/evict/cancel operations. Bias toward page boundaries, tiny pools, repeated terminal events, constant hashes, scope changes and reused physical IDs.
  • Record the seed and full event history, then shrink while preserving operation preconditions. Inject deliberate double-free, missing decrement and stale-index mutants; an invariant suite that cannot detect them is incomplete.
Common mistakes · 3
  • Checking only that total counts add up. One missing page and one double allocation can cancel numerically.
  • Comparing the implementation with an oracle that reads its block tables. The same mapping bug then appears in both answers.
  • Sampling only operations that succeed. Failure, duplicate cleanup and cancellation races are part of the state contract.
3

Try it yourself

Closed-book, before checking anything

Build a deterministic CPU stale-reference history: cache a full page, release its request, evict it, reuse that physical page for different tokens, then look up the old prefix. The correct result is a miss. Deliberately omit index invalidation and require the oracle to catch the stale hit. Add randomized histories around this trace, checking invariants after every event and full recovery after all owners and retained entries are removed. Keep a minimized failing trace for each injected mutant.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why is nonnegative refcount weaker than exact refcount?Answer

    A positive extra count leaks capacity; a positive but insufficient count can free state while another owner still needs it. Compare counts with the complete owner ledger.

  2. 2.Can a stale table entry be dangerous even when its physical page ID is valid?Answer

    Yes. The page may have been recycled for a new owner. A generation or equivalent lifecycle check must distinguish the old reference from the current allocation.

  3. 3.What makes a shrunk failure useful?Answer

    It remains a valid operation history, still exposes the same violated contract, and is small enough to identify the first incorrect transition.

5

Practice

  • Mini-SGLang: TestAllocateEvictPageAlignment

    Port the six named regression contracts to your allocator: post-eviction alignment, consecutive allocation non-overlap, aligned free slots, requested page count, page-to-token expansion and integrity after an eviction cycle

    The section mini-project covers the broader servelab randomized state machines, independent token/owner oracle, collision/cancellation histories and mutation checks. These deterministic tests alone do not cover them.

You're done when

Your allocator passes the six regression contracts and replayable randomized histories, detects the injected lifecycle mutants and returns every physical page after explicit final cleanup.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 12 more in the section
  • Mini-SGLang - Radix Cache and Cache TestsSGLang Project · required

    Read docs/structures.md, the cache allocation code and tests/core/test_cache_allocate.py. Compare exact prefix identity and lifecycle with Nano-vLLM, then add tenant and version fields to your own prefix index.

  • LLM Inference Systems Laboratory - KV StateRoadmap-Final · support

    Implement the paged allocator and prefix index with differential, randomized, cancellation, collision, fork and pressure tests. Retain one reuse workload that improves hit rate but not goodput.

Also in this section