MCPcopy Create free account
hub / github.com/Noumena-Network/code / selectFreshToReplace

Function selectFreshToReplace

src/utils/toolResultStorage.ts:694–711  ·  view source on GitHub ↗

* 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,
)

Source from the content-addressed store, hash-verified

692 * will eventually clear them.
693 */
694function selectFreshToReplace(
695 fresh: ToolResultCandidate[],
696 frozenSize: number,
697 limit: number,
698): ToolResultCandidate[] {
699 const sorted = [...fresh].sort((a, b) => b.size - a.size)
700 const selected: ToolResultCandidate[] = []
701 let remaining = frozenSize + fresh.reduce((sum, c) => sum + c.size, 0)
702 for (const c of sorted) {
703 if (remaining <= limit) break
704 selected.push(c)
705 // We don't know the replacement size until after persist, but previews
706 // are ~2K and results hitting this path are much larger, so subtracting
707 // the full size is a close approximation for selection purposes.
708 remaining -= c.size
709 }
710 return selected
711}
712
713/**
714 * Return a new Message[] where each tool_result block whose id appears in

Callers 1

enforceToolResultBudgetFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected