paginateFromSnapshot can report a total below the nodes it returns; truncated banner never fires
Summary
paginateFromSnapshot reports totalNodes straight from the snapshot's recorded counters, with no floor against the nodes it is actually returning. When the counter is low, the response reports fewer nodes than it ships, and truncated stays false so nothing surfaces the discrepancy.
Where
src/functions/graph.ts:144-172:
const total = filterType
? snap.stats.nodesByType[filterType] ?? 0
: snap.stats.totalNodes;
const pageNodes = filteredNodes.slice(offset, offset + limit);
...
return {
nodes: pageNodes,
totalNodes: total, // <-- trusted verbatim
truncated: total > pageNodes.length,
...
};
Impact
totalNodes is the number the viewer renders in the badge, and truncated is what gates the "showing N of M" banner. Both are derived from counters that can be stale or wrong — in particular the stats.* fields are rewritten wholesale by persistGraphDelta, so any lossy write (see the fail-open read in #1381) leaves the counters disagreeing with topNodes/topEdges.
Observed in our deployment: the endpoint returned 294 nodes with totalNodes: 101. The 343 discrepancy was never shown to the user, because truncated: total > pageNodes.length evaluated 101 > 294 → false. The banner that would have made the corruption visible was switched off by the corruption itself.
The type-filtered branch has the same shape and one extra way to go wrong: a type missing from nodesByType falls through ?? 0 and reports 0 total while still returning that type's nodes. After a lossy write nodesByType is {}, so every type-filtered view reports zero.
Note the direction matters — this is not truncated being inverted. total > pageNodes.length is correct when the counter is trustworthy. The defect is that nothing constrains the reported total from below by the candidates it is drawn from.
Proposed fix
Floor the reported total against the number of candidates being paged:
const totalRaw = filterType
? snap.stats.nodesByType[filterType] ?? 0
: snap.stats.totalNodes;
// A counter that undercuts the candidates it is drawn from is wrong, and
// reporting it hides the discrepancy - truncated is gated on total.
const total = Math.max(totalRaw, filteredNodes.length);
This is deliberately a floor only, so the existing behaviour above SNAPSHOT_TOP_NODES is unchanged: topNodes is capped at 500 by design, and a healthy 40k counter still reports 40000 with truncated: true. The floor only engages when the counter is smaller than the data actually present, which is the corrupted case.
A proper fix would also rebuild the counters from the live graph, but that is the rebuild path — out of scope here. The floor makes the wrong number visible rather than silent, which is what the banner exists for.
Happy to send this as a small PR with tests for: counter below candidate count → floor applies, banner stays off (nothing is actually hidden); healthy counter above candidate count → unchanged, banner fires; type-filtered path → same floor.