prune_stale_links
Remove stale memory graph edges and orphan nodes to maintain an optimized, lean graph structure by eliminating low-weight connections and unused elements.
Instructions
Remove stale memory graph edges whose weight has decayed below threshold via e^(-λt) formula. Also removes orphan nodes with no edges, low access count, and >7 days since last access. Keeps the graph lean.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| threshold | No | Minimum decayed weight to keep an edge. Default: 0.15. Lower = keep more edges. |
Implementation Reference
- src/core/memory-graph.ts:251-271 (handler)The core implementation of the pruneStaleLinks logic that removes edges based on decay and cleans up orphan nodes.
export async function pruneStaleLinks(rootDir: string, threshold?: number): Promise<{ removed: number; remaining: number }> { const graph = await loadGraph(rootDir); const cutoff = threshold ?? STALE_THRESHOLD; const toRemove: string[] = []; for (const [edgeId, edge] of Object.entries(graph.edges)) { if (decayWeight(edge) < cutoff) toRemove.push(edgeId); } for (const id of toRemove) delete graph.edges[id]; const orphanNodeIds = Object.keys(graph.nodes).filter(nodeId => getEdgesForNode(graph, nodeId).length === 0 && graph.nodes[nodeId].accessCount <= 1 && (Date.now() - graph.nodes[nodeId].lastAccessed) > 7 * 86_400_000 ); for (const id of orphanNodeIds) delete graph.nodes[id]; scheduleSave(rootDir); return { removed: toRemove.length + orphanNodeIds.length, remaining: Object.keys(graph.edges).length }; } - src/tools/memory-tools.ts:101-108 (handler)The MCP tool wrapper for the pruneStaleLinks function.
export async function toolPruneStaleLinks(options: PruneStaleLinksOptions): Promise<string> { const result = await pruneStaleLinks(options.rootDir, options.threshold); return [ `🧹 Pruning complete`, ` Removed: ${result.removed} stale links/orphan nodes`, ` Remaining edges: ${result.remaining}`, ].join("\n"); }