| 251 | // pin, never the restore source. |
| 252 | int shallowest_ancestor = -1; |
| 253 | for (int i = 0; i < n; i++) { |
| 254 | if (i == skip_index || is_protected(i)) continue; |
| 255 | if (!is_ancestor(i)) continue; |
| 256 | if (shallowest_ancestor < 0 || |
| 257 | ids_lru[i]->size() < ids_lru[(size_t)shallowest_ancestor]->size()) { |
| 258 | shallowest_ancestor = i; |
| 259 | } |
| 260 | } |
| 261 | if (shallowest_ancestor >= 0) return shallowest_ancestor; |
| 262 | // Only the restore source and/or protected pins remain: destroying |
| 263 | // either would throw away the stable tools head or the in-flight |
| 264 | // restore, so there is no safe victim. |
| 265 | return -1; |
| 266 | } |
| 267 | if (oldest_protected_leaf >= 0) return oldest_protected_leaf; |
| 268 | return 0; // unreachable (the longest entry is always a leaf); pure-LRU fallback |
| 269 | } |
| 270 | |
| 271 | int select_inline_evict_victim(const std::vector<std::vector<int32_t>> & ids_lru, |
| 272 | const std::vector<bool> * protected_lru, |
| 273 | int skip_index) { |
| 274 | std::vector<const std::vector<int32_t> *> ptrs; |
| 275 | ptrs.reserve(ids_lru.size()); |
| 276 | for (const auto & v : ids_lru) ptrs.push_back(&v); |
| 277 | return select_inline_evict_victim(ptrs, protected_lru, skip_index); |
| 278 | } |
| 279 | |
| 280 | int select_inline_snapshot_boundary(const std::vector<int> & boundaries, |
| 281 | int restored_prefix_len, |
| 282 | bool prefer_tools_boundary) { |
| 283 | if (boundaries.empty()) return 0; |
| 284 | // Tool-heavy cold path: pin the system+tools head (first marker) before |
| 285 | // deepening into conversation turns. Matches Python thin-pin semantics. |
| 286 | if (prefer_tools_boundary) { |
| 287 | const int tools_cut = boundaries.front(); |
| 288 | if (tools_cut > restored_prefix_len) return tools_cut; |
| 289 | } |
| 290 | const int target = boundaries.size() >= 2 |
| 291 | ? boundaries[boundaries.size() - 2] |
| 292 | : boundaries.back(); |
| 293 | return target > restored_prefix_len ? target : 0; |
| 294 | } |
| 295 | |
| 296 | bool should_force_inline_snapshot_boundary( |
no test coverage detected