Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
mlxrunner: evict the active conversation's own checkpoints under the budget
Jesse Gross6137793ac495
diff --git a/x/mlxrunner/prefix_cache.go b/x/mlxrunner/prefix_cache.goindex 4ced29fb1..0f825dcd3 100644--- a/x/mlxrunner/prefix_cache.go+++ b/x/mlxrunner/prefix_cache.go@@ -606,15 +606,13 @@ func (c *prefixCache) enforceEvictionPolicy() {return}- activeSet := make(map[*trieNode]bool, len(c.activePath))- for _, n := range c.activePath {- activeSet[n] = true- }-for c.pagedOutBytes > maxPagedOutBytes {+ // Evicting the frontier's parent merges the frontier into it, so+ // resolve the frontier again after every eviction.+ frontier := c.activePath[len(c.activePath)-1]var best *trieNodewalkNodes(c.root, func(n *trieNode) bool {- if n == c.root || activeSet[n] || len(n.children) > 1 {+ if n == c.root || n == frontier || len(n.children) > 1 {return true}// Evict: oldest, then deepest, then largest.@@ -644,7 +642,11 @@ func (c *prefixCache) evictNode(node *trieNode) {// Interior node with one child: merge with child.before := c.pagedOutBytestokens := len(node.tokens)+ child := node.children[0]mergeWithChild(node, c.caches, &c.pagedOutBytes)+ if i := slices.Index(c.activePath, child); i >= 0 {+ c.activePath = slices.Delete(c.activePath, i, i+1)+ }slog.Debug("evicting interior node", "offset", node.startOffset(), "tokens", tokens, "freed", mlx.PrettyBytes(int(before-c.pagedOutBytes)))} else {panic("evictNode called on multi-child branch point")diff --git a/x/mlxrunner/prefix_cache_test.go b/x/mlxrunner/prefix_cache_test.goindex e5617534b..b870fd7ea 100644--- a/x/mlxrunner/prefix_cache_test.go+++ b/x/mlxrunner/prefix_cache_test.go@@ -881,7 +881,7 @@ func TestEvictionPreservesActiveConversations(t *testing.T) {t.Fatalf("pagedOutBytes = %d, want <= %d", pc.pagedOutBytes, maxPagedOutBytes)}- // Active path should be untouched.+ // The branch point and the frontier survive.if len(pc.activePath) < 2 {t.Fatalf("activePath should have >= 2 nodes, got %d", len(pc.activePath))}@@ -954,8 +954,26 @@ func TestUserSnapshotResistsAutoMerge(t *testing.T) {t.Fatalf("user node children = %d, want 2", len(userNode.children))}- // Inflate snapshot sizes and evict. The non-active branch should be- // evicted, leaving the user node with one child.+ // Inflate snapshot sizes so that evicting the non-active branch alone+ // brings the trie under budget, leaving the user node with one child.+ var kept, evicted int+ walkNodes(pc.root, func(n *trieNode) bool {+ for _, s := range n.snapshots {+ if s == nil {+ continue+ }+ if n.parent == userNode && !slices.Contains(pc.activePath, n) {+ evicted+++ } else {+ kept+++ }+ }+ return true+ })+ if evicted == 0 {+ t.Fatal("no snapshots on the non-active branch")+ }+ size := int(maxPagedOutBytes) / keptwalkNodes(pc.root, func(n *trieNode) bool {if !n.hasSnapshots() {return true@@ -963,7 +981,7 @@ func TestUserSnapshotResistsAutoMerge(t *testing.T) {snaps := make([]cache.Snapshot, len(n.snapshots))for i, s := range n.snapshots {if s != nil {- snaps[i] = &fakeSnapshot{byteSize: 5 * 1024 * 1024 * 1024}+ snaps[i] = &fakeSnapshot{byteSize: size}}}n.setSnapshots(snaps, &pc.pagedOutBytes)