|
3 | 3 | The cache stores an opaque restorable snapshot at selected token-block |
4 | 4 | boundaries. Model-specific adapters own serialization/import; this module owns |
5 | 5 | deterministic prefix hashing, exact compatibility matching, |
6 | | -longest-contiguous-prefix lookup, leases, accounting, and memory-pressure |
| 6 | +longest chained-prefix lookup, leases, accounting, and memory-pressure |
7 | 7 | eviction. A hit transfers only the snapshot at the longest matched boundary. |
8 | 8 |
|
9 | 9 | Decode never reads this store. A requester imports a hit once, computes the |
@@ -208,30 +208,35 @@ def lookup( |
208 | 208 | lease_seconds: float = DEFAULT_LEASE_SECONDS, |
209 | 209 | now: float | None = None, |
210 | 210 | ) -> PrefixLease: |
211 | | - """Lease the longest contiguous prefix held by this store.""" |
| 211 | + """Lease the longest available chained-prefix snapshot. |
| 212 | +
|
| 213 | + A chained hash at boundary N commits all preceding token blocks, so a |
| 214 | + promoted final snapshot remains valid even when intermediate boundary |
| 215 | + snapshots are absent or have been evicted. |
| 216 | + """ |
212 | 217 | if lease_seconds <= 0: |
213 | 218 | raise ValueError("lease_seconds must be > 0") |
214 | 219 | now = time.time() if now is None else now |
215 | 220 | with self._lock: |
216 | 221 | self._expire_leases(now) |
217 | | - matched: list[CacheBlock] = [] |
218 | | - for raw_hash in block_hashes: |
| 222 | + snapshot = None |
| 223 | + hit_block_count = 0 |
| 224 | + for index, raw_hash in enumerate(block_hashes): |
219 | 225 | block_hash = bytes(raw_hash) |
220 | 226 | block = self._blocks.get(block_hash) |
221 | | - if block is None: |
222 | | - break |
223 | | - matched.append(block) |
224 | | - self._blocks.move_to_end(block_hash) |
225 | | - if not matched: |
| 227 | + if block is not None: |
| 228 | + snapshot = block |
| 229 | + hit_block_count = index + 1 |
| 230 | + if snapshot is None: |
226 | 231 | self._lookup_misses += 1 |
227 | 232 | return PrefixLease("", (), 0, 0, 0, self._epoch, now, bytes(32)) |
| 233 | + self._blocks.move_to_end(snapshot.block_hash) |
228 | 234 | self._lookup_hits += 1 |
229 | 235 | lease_id = secrets.token_urlsafe(18) |
230 | | - snapshot = matched[-1] |
231 | 236 | lease = PrefixLease( |
232 | 237 | lease_id=lease_id, |
233 | 238 | block_hashes=(snapshot.block_hash,), |
234 | | - hit_block_count=len(matched), |
| 239 | + hit_block_count=hit_block_count, |
235 | 240 | hit_token_count=snapshot.token_count, |
236 | 241 | transfer_bytes=snapshot.nbytes, |
237 | 242 | cache_epoch=self._epoch, |
|
0 commit comments