* Pick the largest fresh results to replace until the model-visible total * (frozen + remaining fresh) is at or under budget, or fresh is exhausted. * If frozen results alone exceed budget we accept the overage — microcompact * will eventually clear them.
( fresh: ToolResultCandidate[], frozenSize: number, limit: number, )
| 673 | * will eventually clear them. |
| 674 | */ |
| 675 | function selectFreshToReplace( |
| 676 | fresh: ToolResultCandidate[], |
| 677 | frozenSize: number, |
| 678 | limit: number, |
| 679 | ): ToolResultCandidate[] { |
| 680 | const sorted = [...fresh].sort((a, b) => b.size - a.size) |
| 681 | const selected: ToolResultCandidate[] = [] |
| 682 | let remaining = frozenSize + fresh.reduce((sum, c) => sum + c.size, 0) |
| 683 | for (const c of sorted) { |
| 684 | if (remaining <= limit) break |
| 685 | selected.push(c) |
| 686 | // We don't know the replacement size until after persist, but previews |
| 687 | // are ~2K and results hitting this path are much larger, so subtracting |
| 688 | // the full size is a close approximation for selection purposes. |
| 689 | remaining -= c.size |
| 690 | } |
| 691 | return selected |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Return a new Message[] where each tool_result block whose id appears in |
no test coverage detected