fix: maxNodes now correctly limits total nodes (documents + memories)

Previously maxNodes limited only the number of documents, ignoring their
memories. With ~2-6 memories per document, setting maxNodes=200 could result
in 600-1400+ actual nodes, defeating the performance guard.

Now iterates documents and accumulates total node count (1 doc + N memories),
stopping when the limit would be exceeded. This matches the documented
behavior of 'Max nodes to display'.

Addresses Sentry review comment on memory-graph.tsx#L62.
This commit is contained in:
Vorflux AI 2026-03-28 23:50:16 +00:00
parent 3caeedc5a9
commit 703ff09517

View file

@ -55,10 +55,20 @@ export function MemoryGraph({
// Used as a dependency proxy to recalculate popover positions
const [viewportVersion, setViewportVersion] = useState(0)
// Limit documents if maxNodes is set
// Limit documents so total node count (documents + their memories) stays under maxNodes
const limitedDocuments = useMemo(() => {
if (!maxNodes || documents.length <= maxNodes) return documents
return documents.slice(0, maxNodes)
if (!maxNodes || documents.length === 0) return documents
let totalNodes = 0
let cutoff = documents.length
for (let i = 0; i < documents.length; i++) {
const docNodes = 1 + (documents[i]?.memories?.length ?? 0)
if (totalNodes + docNodes > maxNodes) {
cutoff = i
break
}
totalNodes += docNodes
}
return cutoff === documents.length ? documents : documents.slice(0, cutoff)
}, [documents, maxNodes])
const { nodes, edges } = useGraphData(